xref: /NextBSD/sys/boot/i386/libi386/pxe.c (revision 4557fabb34e865d7f40be64b39c9e34fa41dbb60)
1 /*-
2  * Copyright (c) 2000 Alfred Perlstein <alfred@freebsd.org>
3  * Copyright (c) 2000 Paul Saab <ps@freebsd.org>
4  * Copyright (c) 2000 John Baldwin <jhb@freebsd.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <stand.h>
33 #include <string.h>
34 #include <stdarg.h>
35 
36 #include <netinet/in_systm.h>
37 #include <netinet/in.h>
38 #include <netinet/udp.h>
39 
40 #include <net.h>
41 #include <netif.h>
42 #include <nfsv2.h>
43 #include <iodesc.h>
44 
45 #include <bootp.h>
46 #include <bootstrap.h>
47 #include "btxv86.h"
48 #include "pxe.h"
49 
50 /*
51  * Allocate the PXE buffers statically instead of sticking grimy fingers into
52  * BTX's private data area.  The scratch buffer is used to send information to
53  * the PXE BIOS, and the data buffer is used to receive data from the PXE BIOS.
54  */
55 #define	PXE_BUFFER_SIZE		0x2000
56 #define	PXE_TFTP_BUFFER_SIZE	512
57 static char	scratch_buffer[PXE_BUFFER_SIZE];
58 static char	data_buffer[PXE_BUFFER_SIZE];
59 
60 static pxenv_t	*pxenv_p = NULL;        /* PXENV+ */
61 static pxe_t	*pxe_p   = NULL;	/* !PXE */
62 static BOOTPLAYER	bootplayer;	/* PXE Cached information. */
63 
64 static int 	pxe_debug = 0;
65 static int	pxe_sock = -1;
66 static int	pxe_opens = 0;
67 
68 void		pxe_enable(void *pxeinfo);
69 static void	(*pxe_call)(int func);
70 static void	pxenv_call(int func);
71 static void	bangpxe_call(int func);
72 
73 static int	pxe_init(void);
74 static int	pxe_strategy(void *devdata, int flag, daddr_t dblk,
75 			     size_t size, char *buf, size_t *rsize);
76 static int	pxe_open(struct open_file *f, ...);
77 static int	pxe_close(struct open_file *f);
78 static void	pxe_print(int verbose);
79 static void	pxe_cleanup(void);
80 static void	pxe_setnfshandle(char *rootpath);
81 
82 static void	pxe_perror(int error);
83 static int	pxe_netif_match(struct netif *nif, void *machdep_hint);
84 static int	pxe_netif_probe(struct netif *nif, void *machdep_hint);
85 static void	pxe_netif_init(struct iodesc *desc, void *machdep_hint);
86 static int	pxe_netif_get(struct iodesc *desc, void *pkt, size_t len,
87 			      time_t timeout);
88 static int	pxe_netif_put(struct iodesc *desc, void *pkt, size_t len);
89 static void	pxe_netif_end(struct netif *nif);
90 
91 #ifdef OLD_NFSV2
92 int nfs_getrootfh(struct iodesc*, char*, u_char*);
93 #else
94 int nfs_getrootfh(struct iodesc*, char*, uint32_t*, u_char*);
95 #endif
96 
97 extern struct netif_stats	pxe_st[];
98 extern u_int16_t		__bangpxeseg;
99 extern u_int16_t		__bangpxeoff;
100 extern void			__bangpxeentry(void);
101 extern u_int16_t		__pxenvseg;
102 extern u_int16_t		__pxenvoff;
103 extern void			__pxenventry(void);
104 
105 struct netif_dif pxe_ifs[] = {
106 /*      dif_unit        dif_nsel        dif_stats       dif_private     */
107 	{0,             1,              &pxe_st[0],     0}
108 };
109 
110 struct netif_stats pxe_st[NENTS(pxe_ifs)];
111 
112 struct netif_driver pxenetif = {
113 	"pxenet",
114 	pxe_netif_match,
115 	pxe_netif_probe,
116 	pxe_netif_init,
117 	pxe_netif_get,
118 	pxe_netif_put,
119 	pxe_netif_end,
120 	pxe_ifs,
121 	NENTS(pxe_ifs)
122 };
123 
124 struct netif_driver *netif_drivers[] = {
125 	&pxenetif,
126 	NULL
127 };
128 
129 struct devsw pxedisk = {
130 	"pxe",
131 	DEVT_NET,
132 	pxe_init,
133 	pxe_strategy,
134 	pxe_open,
135 	pxe_close,
136 	noioctl,
137 	pxe_print,
138 	pxe_cleanup
139 };
140 
141 /*
142  * This function is called by the loader to enable PXE support if we
143  * are booted by PXE.  The passed in pointer is a pointer to the
144  * PXENV+ structure.
145  */
146 void
pxe_enable(void * pxeinfo)147 pxe_enable(void *pxeinfo)
148 {
149 	pxenv_p  = (pxenv_t *)pxeinfo;
150 	pxe_p    = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
151 				 pxenv_p->PXEPtr.offset);
152 	pxe_call = NULL;
153 }
154 
155 /*
156  * return true if pxe structures are found/initialized,
157  * also figures out our IP information via the pxe cached info struct
158  */
159 static int
pxe_init(void)160 pxe_init(void)
161 {
162 	t_PXENV_GET_CACHED_INFO	*gci_p;
163 	int	counter;
164 	uint8_t checksum;
165 	uint8_t *checkptr;
166 
167 	if(pxenv_p == NULL)
168 		return (0);
169 
170 	/*  look for "PXENV+" */
171 	if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
172 		pxenv_p = NULL;
173 		return (0);
174 	}
175 
176 	/* make sure the size is something we can handle */
177 	if (pxenv_p->Length > sizeof(*pxenv_p)) {
178 	  	printf("PXENV+ structure too large, ignoring\n");
179 		pxenv_p = NULL;
180 		return (0);
181 	}
182 
183 	/*
184 	 * do byte checksum:
185 	 * add up each byte in the structure, the total should be 0
186 	 */
187 	checksum = 0;
188 	checkptr = (uint8_t *) pxenv_p;
189 	for (counter = 0; counter < pxenv_p->Length; counter++)
190 		checksum += *checkptr++;
191 	if (checksum != 0) {
192 		printf("PXENV+ structure failed checksum, ignoring\n");
193 		pxenv_p = NULL;
194 		return (0);
195 	}
196 
197 
198 	/*
199 	 * PXENV+ passed, so use that if !PXE is not available or
200 	 * the checksum fails.
201 	 */
202 	pxe_call = pxenv_call;
203 	if (pxenv_p->Version >= 0x0200) {
204 		for (;;) {
205 			if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
206 				pxe_p = NULL;
207 				break;
208 			}
209 			checksum = 0;
210 			checkptr = (uint8_t *)pxe_p;
211 			for (counter = 0; counter < pxe_p->StructLength;
212 			     counter++)
213 				checksum += *checkptr++;
214 			if (checksum != 0) {
215 				pxe_p = NULL;
216 				break;
217 			}
218 			pxe_call = bangpxe_call;
219 			break;
220 		}
221 	}
222 
223 	printf("\nPXE version %d.%d, real mode entry point ",
224 	       (uint8_t) (pxenv_p->Version >> 8),
225 	       (uint8_t) (pxenv_p->Version & 0xFF));
226 	if (pxe_call == bangpxe_call)
227 		printf("@%04x:%04x\n",
228 		       pxe_p->EntryPointSP.segment,
229 		       pxe_p->EntryPointSP.offset);
230 	else
231 		printf("@%04x:%04x\n",
232 		       pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
233 
234 	gci_p = (t_PXENV_GET_CACHED_INFO *) scratch_buffer;
235 	bzero(gci_p, sizeof(*gci_p));
236 	gci_p->PacketType =  PXENV_PACKET_TYPE_BINL_REPLY;
237 	pxe_call(PXENV_GET_CACHED_INFO);
238 	if (gci_p->Status != 0) {
239 		pxe_perror(gci_p->Status);
240 		pxe_p = NULL;
241 		return (0);
242 	}
243 	bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
244 	      &bootplayer, gci_p->BufferSize);
245 	return (1);
246 }
247 
248 
249 static int
pxe_strategy(void * devdata,int flag,daddr_t dblk,size_t size,char * buf,size_t * rsize)250 pxe_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
251 		char *buf, size_t *rsize)
252 {
253 	return (EIO);
254 }
255 
256 static int
pxe_open(struct open_file * f,...)257 pxe_open(struct open_file *f, ...)
258 {
259     va_list args;
260     char *devname;		/* Device part of file name (or NULL). */
261     char temp[FNAME_SIZE];
262     int error = 0;
263     int i;
264 
265     va_start(args, f);
266     devname = va_arg(args, char*);
267     va_end(args);
268 
269     /* On first open, do netif open, mount, etc. */
270     if (pxe_opens == 0) {
271 	/* Find network interface. */
272 	if (pxe_sock < 0) {
273 	    pxe_sock = netif_open(devname);
274 	    if (pxe_sock < 0) {
275 		printf("pxe_open: netif_open() failed\n");
276 		return (ENXIO);
277 	    }
278 	    if (pxe_debug)
279 		printf("pxe_open: netif_open() succeeded\n");
280 	}
281 	if (rootip.s_addr == 0) {
282 		/*
283 		 * Do a bootp/dhcp request to find out where our
284 		 * NFS/TFTP server is.  Even if we dont get back
285 		 * the proper information, fall back to the server
286 		 * which brought us to life and a default rootpath.
287 		 */
288 		bootp(pxe_sock, BOOTP_PXE);
289 		if (rootip.s_addr == 0)
290 			rootip.s_addr = bootplayer.sip;
291 #ifdef LOADER_NFS_SUPPORT
292 		if (!rootpath[0])
293 			strcpy(rootpath, PXENFSROOTPATH);
294 #endif
295 
296 		for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
297 			if (rootpath[i] == ':')
298 				break;
299 		if (i && i != FNAME_SIZE && rootpath[i] == ':') {
300 			rootpath[i++] = '\0';
301 			if (inet_addr(&rootpath[0]) != INADDR_NONE)
302 				rootip.s_addr = inet_addr(&rootpath[0]);
303 			bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
304 			bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);
305 		}
306 		setenv("boot.netif.ip", inet_ntoa(myip), 1);
307 		setenv("boot.netif.netmask", intoa(netmask), 1);
308 		setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
309 		if (bootplayer.Hardware == ETHER_TYPE) {
310 		    sprintf(temp, "%6D", bootplayer.CAddr, ":");
311 		    setenv("boot.netif.hwaddr", temp, 1);
312 		}
313 #ifdef LOADER_NFS_SUPPORT
314 		printf("pxe_open: server addr: %s\n", inet_ntoa(rootip));
315 		printf("pxe_open: server path: %s\n", rootpath);
316 		printf("pxe_open: gateway ip:  %s\n", inet_ntoa(gateip));
317 
318 		setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
319 		setenv("boot.nfsroot.path", rootpath, 1);
320 #else
321 		setenv("boot.netif.server", inet_ntoa(rootip), 1);
322 		setenv("boot.tftproot.path", rootpath, 1);
323 #endif
324 		setenv("dhcp.host-name", hostname, 1);
325 
326 		setenv("pxeboot.ip", inet_ntoa(myip), 1);
327 		if (bootplayer.Hardware == ETHER_TYPE) {
328 		    sprintf(temp, "%6D", bootplayer.CAddr, ":");
329 		    setenv("pxeboot.hwaddr", temp, 1);
330 		}
331 	}
332     }
333     pxe_opens++;
334     f->f_devdata = &pxe_sock;
335     return (error);
336 }
337 
338 static int
pxe_close(struct open_file * f)339 pxe_close(struct open_file *f)
340 {
341 
342 #ifdef	PXE_DEBUG
343     if (pxe_debug)
344 	printf("pxe_close: opens=%d\n", pxe_opens);
345 #endif
346 
347     /* On last close, do netif close, etc. */
348     f->f_devdata = NULL;
349     /* Extra close call? */
350     if (pxe_opens <= 0)
351 	return (0);
352     pxe_opens--;
353     /* Not last close? */
354     if (pxe_opens > 0)
355 	return(0);
356 
357 #ifdef LOADER_NFS_SUPPORT
358     /* get an NFS filehandle for our root filesystem */
359     pxe_setnfshandle(rootpath);
360 #endif
361 
362     if (pxe_sock >= 0) {
363 
364 #ifdef PXE_DEBUG
365 	if (pxe_debug)
366 	    printf("pxe_close: calling netif_close()\n");
367 #endif
368 	netif_close(pxe_sock);
369 	pxe_sock = -1;
370     }
371     return (0);
372 }
373 
374 static void
pxe_print(int verbose)375 pxe_print(int verbose)
376 {
377 
378 	if (pxe_call == NULL)
379 		return;
380 
381 	printf("    pxe0:    %s:%s\n", inet_ntoa(rootip), rootpath);
382 }
383 
384 static void
pxe_cleanup(void)385 pxe_cleanup(void)
386 {
387 #ifdef PXE_DEBUG
388 	t_PXENV_UNLOAD_STACK *unload_stack_p =
389 	    (t_PXENV_UNLOAD_STACK *)scratch_buffer;
390 	t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p =
391 	    (t_PXENV_UNDI_SHUTDOWN *)scratch_buffer;
392 #endif
393 
394 	if (pxe_call == NULL)
395 		return;
396 
397 	pxe_call(PXENV_UNDI_SHUTDOWN);
398 
399 #ifdef PXE_DEBUG
400 	if (pxe_debug && undi_shutdown_p->Status != 0)
401 		printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
402 		       undi_shutdown_p->Status);
403 #endif
404 
405 	pxe_call(PXENV_UNLOAD_STACK);
406 
407 #ifdef PXE_DEBUG
408 	if (pxe_debug && unload_stack_p->Status != 0)
409 		printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
410 		    unload_stack_p->Status);
411 #endif
412 }
413 
414 void
pxe_perror(int err)415 pxe_perror(int err)
416 {
417 	return;
418 }
419 
420 #ifdef LOADER_NFS_SUPPORT
421 /*
422  * Reach inside the libstand NFS code and dig out an NFS handle
423  * for the root filesystem.
424  */
425 #ifdef OLD_NFSV2
426 struct nfs_iodesc {
427 	struct	iodesc	*iodesc;
428 	off_t	off;
429 	u_char	fh[NFS_FHSIZE];
430 	/* structure truncated here */
431 };
432 extern struct	nfs_iodesc nfs_root_node;
433 extern int      rpc_port;
434 
435 static void
pxe_rpcmountcall()436 pxe_rpcmountcall()
437 {
438 	struct	iodesc *d;
439 	int     error;
440 
441 	if (!(d = socktodesc(pxe_sock)))
442 		return;
443         d->myport = htons(--rpc_port);
444         d->destip = rootip;
445 	if ((error = nfs_getrootfh(d, rootpath, nfs_root_node.fh)) != 0)
446 		printf("NFS MOUNT RPC error: %d\n", error);
447 	nfs_root_node.iodesc = d;
448 }
449 
450 static void
pxe_setnfshandle(char * rootpath)451 pxe_setnfshandle(char *rootpath)
452 {
453 	int	i;
454 	u_char	*fh;
455 	char	buf[2 * NFS_FHSIZE + 3], *cp;
456 
457 	/*
458 	 * If NFS files were never opened, we need to do mount call
459 	 * ourselves. Use nfs_root_node.iodesc as flag indicating
460 	 * previous NFS usage.
461 	 */
462 	if (nfs_root_node.iodesc == NULL)
463 		pxe_rpcmountcall();
464 
465 	fh = &nfs_root_node.fh[0];
466 	buf[0] = 'X';
467 	cp = &buf[1];
468 	for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
469 		sprintf(cp, "%02x", fh[i]);
470 	sprintf(cp, "X");
471 	setenv("boot.nfsroot.nfshandle", buf, 1);
472 }
473 #else	/* !OLD_NFSV2 */
474 
475 #define	NFS_V3MAXFHSIZE		64
476 
477 struct nfs_iodesc {
478 	struct iodesc *iodesc;
479 	off_t off;
480 	uint32_t fhsize;
481 	u_char fh[NFS_V3MAXFHSIZE];
482 	/* structure truncated */
483 };
484 extern struct nfs_iodesc nfs_root_node;
485 extern int rpc_port;
486 
487 static void
pxe_rpcmountcall()488 pxe_rpcmountcall()
489 {
490 	struct iodesc *d;
491 	int error;
492 
493 	if (!(d = socktodesc(pxe_sock)))
494 		return;
495         d->myport = htons(--rpc_port);
496         d->destip = rootip;
497 	if ((error = nfs_getrootfh(d, rootpath, &nfs_root_node.fhsize,
498 	    nfs_root_node.fh)) != 0) {
499 		printf("NFS MOUNT RPC error: %d\n", error);
500 		nfs_root_node.fhsize = 0;
501 	}
502 	nfs_root_node.iodesc = d;
503 }
504 
505 static void
pxe_setnfshandle(char * rootpath)506 pxe_setnfshandle(char *rootpath)
507 {
508 	int i;
509 	u_char *fh;
510 	char buf[2 * NFS_V3MAXFHSIZE + 3], *cp;
511 
512 	/*
513 	 * If NFS files were never opened, we need to do mount call
514 	 * ourselves. Use nfs_root_node.iodesc as flag indicating
515 	 * previous NFS usage.
516 	 */
517 	if (nfs_root_node.iodesc == NULL)
518 		pxe_rpcmountcall();
519 
520 	fh = &nfs_root_node.fh[0];
521 	buf[0] = 'X';
522 	cp = &buf[1];
523 	for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
524 		sprintf(cp, "%02x", fh[i]);
525 	sprintf(cp, "X");
526 	setenv("boot.nfsroot.nfshandle", buf, 1);
527 	sprintf(buf, "%d", nfs_root_node.fhsize);
528 	setenv("boot.nfsroot.nfshandlelen", buf, 1);
529 }
530 #endif	/* OLD_NFSV2 */
531 #endif /* LOADER_NFS_SUPPORT */
532 
533 void
pxenv_call(int func)534 pxenv_call(int func)
535 {
536 #ifdef PXE_DEBUG
537 	if (pxe_debug)
538 		printf("pxenv_call %x\n", func);
539 #endif
540 
541 	bzero(&v86, sizeof(v86));
542 	bzero(data_buffer, sizeof(data_buffer));
543 
544 	__pxenvseg = pxenv_p->RMEntry.segment;
545 	__pxenvoff = pxenv_p->RMEntry.offset;
546 
547 	v86.ctl  = V86_ADDR | V86_CALLF | V86_FLAGS;
548 	v86.es   = VTOPSEG(scratch_buffer);
549 	v86.edi  = VTOPOFF(scratch_buffer);
550 	v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
551 	v86.ebx  = func;
552 	v86int();
553 	v86.ctl  = V86_FLAGS;
554 }
555 
556 void
bangpxe_call(int func)557 bangpxe_call(int func)
558 {
559 #ifdef PXE_DEBUG
560 	if (pxe_debug)
561 		printf("bangpxe_call %x\n", func);
562 #endif
563 
564 	bzero(&v86, sizeof(v86));
565 	bzero(data_buffer, sizeof(data_buffer));
566 
567 	__bangpxeseg = pxe_p->EntryPointSP.segment;
568 	__bangpxeoff = pxe_p->EntryPointSP.offset;
569 
570 	v86.ctl  = V86_ADDR | V86_CALLF | V86_FLAGS;
571 	v86.edx  = VTOPSEG(scratch_buffer);
572 	v86.eax  = VTOPOFF(scratch_buffer);
573 	v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
574 	v86.ebx  = func;
575 	v86int();
576 	v86.ctl  = V86_FLAGS;
577 }
578 
579 
580 time_t
getsecs()581 getsecs()
582 {
583 	time_t n = 0;
584 	time(&n);
585 	return n;
586 }
587 
588 static int
pxe_netif_match(struct netif * nif,void * machdep_hint)589 pxe_netif_match(struct netif *nif, void *machdep_hint)
590 {
591 	return 1;
592 }
593 
594 
595 static int
pxe_netif_probe(struct netif * nif,void * machdep_hint)596 pxe_netif_probe(struct netif *nif, void *machdep_hint)
597 {
598 	t_PXENV_UDP_OPEN *udpopen_p = (t_PXENV_UDP_OPEN *)scratch_buffer;
599 
600 	if (pxe_call == NULL)
601 		return -1;
602 
603 	bzero(udpopen_p, sizeof(*udpopen_p));
604 	udpopen_p->src_ip = bootplayer.yip;
605 	pxe_call(PXENV_UDP_OPEN);
606 
607 	if (udpopen_p->status != 0) {
608 		printf("pxe_netif_probe: failed %x\n", udpopen_p->status);
609 		return -1;
610 	}
611 	return 0;
612 }
613 
614 static void
pxe_netif_end(struct netif * nif)615 pxe_netif_end(struct netif *nif)
616 {
617 	t_PXENV_UDP_CLOSE *udpclose_p = (t_PXENV_UDP_CLOSE *)scratch_buffer;
618 	bzero(udpclose_p, sizeof(*udpclose_p));
619 
620 	pxe_call(PXENV_UDP_CLOSE);
621 	if (udpclose_p->status != 0)
622 		printf("pxe_end failed %x\n", udpclose_p->status);
623 }
624 
625 static void
pxe_netif_init(struct iodesc * desc,void * machdep_hint)626 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
627 {
628 	int i;
629 	for (i = 0; i < 6; ++i)
630 		desc->myea[i] = bootplayer.CAddr[i];
631 	desc->xid = bootplayer.ident;
632 }
633 
634 static int
pxe_netif_get(struct iodesc * desc,void * pkt,size_t len,time_t timeout)635 pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
636 {
637 	return len;
638 }
639 
640 static int
pxe_netif_put(struct iodesc * desc,void * pkt,size_t len)641 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
642 {
643 	return len;
644 }
645 
646 ssize_t
sendudp(struct iodesc * h,void * pkt,size_t len)647 sendudp(struct iodesc *h, void *pkt, size_t len)
648 {
649 	t_PXENV_UDP_WRITE *udpwrite_p = (t_PXENV_UDP_WRITE *)scratch_buffer;
650 	bzero(udpwrite_p, sizeof(*udpwrite_p));
651 
652 	udpwrite_p->ip             = h->destip.s_addr;
653 	udpwrite_p->dst_port       = h->destport;
654 	udpwrite_p->src_port       = h->myport;
655 	udpwrite_p->buffer_size    = len;
656 	udpwrite_p->buffer.segment = VTOPSEG(pkt);
657 	udpwrite_p->buffer.offset  = VTOPOFF(pkt);
658 
659 	if (netmask == 0 || SAMENET(myip, h->destip, netmask))
660 		udpwrite_p->gw = 0;
661 	else
662 		udpwrite_p->gw = gateip.s_addr;
663 
664 	pxe_call(PXENV_UDP_WRITE);
665 
666 #if 0
667 	/* XXX - I dont know why we need this. */
668 	delay(1000);
669 #endif
670 	if (udpwrite_p->status != 0) {
671 		/* XXX: This happens a lot.  It shouldn't. */
672 		if (udpwrite_p->status != 1)
673 			printf("sendudp failed %x\n", udpwrite_p->status);
674 		return -1;
675 	}
676 	return len;
677 }
678 
679 ssize_t
readudp(struct iodesc * h,void * pkt,size_t len,time_t timeout)680 readudp(struct iodesc *h, void *pkt, size_t len, time_t timeout)
681 {
682 	t_PXENV_UDP_READ *udpread_p = (t_PXENV_UDP_READ *)scratch_buffer;
683 	struct udphdr *uh = NULL;
684 
685 	uh = (struct udphdr *) pkt - 1;
686 	bzero(udpread_p, sizeof(*udpread_p));
687 
688 	udpread_p->dest_ip        = h->myip.s_addr;
689 	udpread_p->d_port         = h->myport;
690 	udpread_p->buffer_size    = len;
691 	udpread_p->buffer.segment = VTOPSEG(data_buffer);
692 	udpread_p->buffer.offset  = VTOPOFF(data_buffer);
693 
694 	pxe_call(PXENV_UDP_READ);
695 
696 #if 0
697 	/* XXX - I dont know why we need this. */
698 	delay(1000);
699 #endif
700 	if (udpread_p->status != 0) {
701 		/* XXX: This happens a lot.  It shouldn't. */
702 		if (udpread_p->status != 1)
703 			printf("readudp failed %x\n", udpread_p->status);
704 		return -1;
705 	}
706 	bcopy(data_buffer, pkt, udpread_p->buffer_size);
707 	uh->uh_sport = udpread_p->s_port;
708 	return udpread_p->buffer_size;
709 }
710