]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/i386/libi386/pxe.c
MFC Loader Fixes 2017q1: r311458,r312237,r312314,r312374,r312947,r313042,
[FreeBSD/FreeBSD.git] / sys / boot / i386 / libi386 / pxe.c
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 int      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 int nfs_getrootfh(struct iodesc*, char*, uint32_t*, u_char*);
92
93 extern struct netif_stats       pxe_st[];
94 extern u_int16_t                __bangpxeseg;
95 extern u_int16_t                __bangpxeoff;
96 extern void                     __bangpxeentry(void);
97 extern u_int16_t                __pxenvseg;
98 extern u_int16_t                __pxenvoff;
99 extern void                     __pxenventry(void);
100 extern struct in_addr           servip;
101
102 struct netif_dif pxe_ifs[] = {
103 /*      dif_unit        dif_nsel        dif_stats       dif_private     */
104         {0,             1,              &pxe_st[0],     0}
105 };
106
107 struct netif_stats pxe_st[NENTS(pxe_ifs)];
108
109 struct netif_driver pxenetif = {
110         "pxenet",
111         pxe_netif_match,
112         pxe_netif_probe,
113         pxe_netif_init,
114         pxe_netif_get,
115         pxe_netif_put,
116         pxe_netif_end,
117         pxe_ifs,
118         NENTS(pxe_ifs)
119 };
120
121 struct netif_driver *netif_drivers[] = {
122         &pxenetif,
123         NULL
124 };
125
126 struct devsw pxedisk = {
127         "pxe",
128         DEVT_NET,
129         pxe_init,
130         pxe_strategy,
131         pxe_open,
132         pxe_close,
133         noioctl,
134         pxe_print,
135         pxe_cleanup
136 };
137
138 /*
139  * This function is called by the loader to enable PXE support if we
140  * are booted by PXE. The passed in pointer is a pointer to the PXENV+
141  * structure.
142  */
143 void
144 pxe_enable(void *pxeinfo)
145 {
146         pxenv_p  = (pxenv_t *)pxeinfo;
147         pxe_p    = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
148                                  pxenv_p->PXEPtr.offset);
149         pxe_call = NULL;
150 }
151
152 /*
153  * return true if pxe structures are found/initialized,
154  * also figures out our IP information via the pxe cached info struct
155  */
156 static int
157 pxe_init(void)
158 {
159         t_PXENV_GET_CACHED_INFO *gci_p;
160         int counter;
161         uint8_t checksum;
162         uint8_t *checkptr;
163
164         if (pxenv_p == NULL)
165                 return (0);
166
167         /* look for "PXENV+" */
168         if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
169                 pxenv_p = NULL;
170                 return (0);
171         }
172
173         /* make sure the size is something we can handle */
174         if (pxenv_p->Length > sizeof(*pxenv_p)) {
175                 printf("PXENV+ structure too large, ignoring\n");
176                 pxenv_p = NULL;
177                 return (0);
178         }
179
180         /*
181          * do byte checksum:
182          * add up each byte in the structure, the total should be 0
183          */
184         checksum = 0;
185         checkptr = (uint8_t *) pxenv_p;
186         for (counter = 0; counter < pxenv_p->Length; counter++)
187                 checksum += *checkptr++;
188         if (checksum != 0) {
189                 printf("PXENV+ structure failed checksum, ignoring\n");
190                 pxenv_p = NULL;
191                 return (0);
192         }
193
194         /*
195          * PXENV+ passed, so use that if !PXE is not available or
196          * the checksum fails.
197          */
198         pxe_call = pxenv_call;
199         if (pxenv_p->Version >= 0x0200) {
200                 for (;;) {
201                         if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
202                                 pxe_p = NULL;
203                                 break;
204                         }
205                         checksum = 0;
206                         checkptr = (uint8_t *)pxe_p;
207                         for (counter = 0; counter < pxe_p->StructLength;
208                             counter++)
209                                 checksum += *checkptr++;
210                         if (checksum != 0) {
211                                 pxe_p = NULL;
212                                 break;
213                         }
214                         pxe_call = bangpxe_call;
215                         break;
216                 }
217         }
218         
219         printf("\nPXE version %d.%d, real mode entry point ",
220             (uint8_t) (pxenv_p->Version >> 8),
221             (uint8_t) (pxenv_p->Version & 0xFF));
222         if (pxe_call == bangpxe_call)
223                 printf("@%04x:%04x\n",
224                     pxe_p->EntryPointSP.segment,
225                     pxe_p->EntryPointSP.offset);
226         else
227                 printf("@%04x:%04x\n",
228                     pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
229
230         gci_p = (t_PXENV_GET_CACHED_INFO *) scratch_buffer;
231         bzero(gci_p, sizeof(*gci_p));
232         gci_p->PacketType = PXENV_PACKET_TYPE_BINL_REPLY;
233         pxe_call(PXENV_GET_CACHED_INFO);
234         if (gci_p->Status != 0) {
235                 pxe_perror(gci_p->Status);
236                 pxe_p = NULL;
237                 return (0);
238         }
239         bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
240             &bootplayer, gci_p->BufferSize);
241         return (1);
242 }
243
244
245 static int
246 pxe_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
247     char *buf, size_t *rsize)
248 {
249         return (EIO);
250 }
251
252 static int
253 pxe_open(struct open_file *f, ...)
254 {
255         va_list args;
256         char *devname;          /* Device part of file name (or NULL). */
257         char temp[FNAME_SIZE];
258         int error = 0;
259         int i;
260
261         va_start(args, f);
262         devname = va_arg(args, char*);
263         va_end(args);
264
265         /* On first open, do netif open, mount, etc. */
266         if (pxe_opens == 0) {
267                 /* Find network interface. */
268                 if (pxe_sock < 0) {
269                         pxe_sock = netif_open(devname);
270                         if (pxe_sock < 0) {
271                                 printf("pxe_open: netif_open() failed\n");
272                                 return (ENXIO);
273                         }
274                         if (pxe_debug)
275                                 printf("pxe_open: netif_open() succeeded\n");
276
277                         if (socktodesc(pxe_sock) == NULL) {
278                                 printf("pxe_open: bad socket %d\n", pxe_sock);
279                                 return (ENXIO);
280                         }
281
282                 }
283                 if (rootip.s_addr == 0) {
284                         /*
285                          * Try to extract the RFC1048 data from PXE.
286                          * If fail do a bootp/dhcp request to find out where our
287                          * NFS/TFTP server is. Even if we dont get back
288                          * the proper information, fall back to the server
289                          * which brought us to life and a default rootpath.
290                          */
291
292                         if (dhcp_try_rfc1048(bootplayer.vendor.d, BOOTP_DHCPVEND) < 0) {
293                                 if (pxe_debug)
294                                         printf("pxe_open: no RFC1048 data in PXE Cache\n");
295                                 bootp(pxe_sock, BOOTP_PXE);
296                         } else if (pxe_debug) {
297                                 printf("pxe_open: loaded RFC1048 data from PXE Cache\n");
298                         }
299
300 #ifdef LOADER_TFTP_SUPPORT
301                         bootp(pxe_sock, BOOTP_PXE);
302 #endif
303                         if (rootip.s_addr == 0)
304                                 rootip.s_addr = bootplayer.sip;
305                         if (gateip.s_addr == 0)
306                                 gateip.s_addr = bootplayer.gip;
307                         if (myip.s_addr == 0)
308                                 myip.s_addr = bootplayer.yip;
309                         if (servip.s_addr == 0)
310                                 servip = rootip;
311
312                         netproto = NET_NFS;
313                         if (tftpip.s_addr != 0) {
314                                 netproto = NET_TFTP;
315                                 rootip.s_addr = tftpip.s_addr;
316                         }
317
318                         if (netproto == NET_NFS && !rootpath[0])
319                                 strcpy(rootpath, PXENFSROOTPATH);
320
321                         for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
322                                 if (rootpath[i] == ':')
323                                         break;
324                         if (i && i != FNAME_SIZE && rootpath[i] == ':') {
325                                 rootpath[i++] = '\0';
326                                 if (inet_addr(&rootpath[0]) != INADDR_NONE)
327                                         rootip.s_addr = inet_addr(&rootpath[0]);
328                                 bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i]) + 1);
329                                 bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i]) + 1);
330                         }
331                         setenv("boot.netif.ip", inet_ntoa(myip), 1);
332                         setenv("boot.netif.netmask", intoa(netmask), 1);
333                         setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
334                         setenv("boot.netif.server", inet_ntoa(rootip), 1);
335                         if (bootplayer.Hardware == ETHER_TYPE) {
336                                 sprintf(temp, "%6D", bootplayer.CAddr, ":");
337                                 setenv("boot.netif.hwaddr", temp, 1);
338                         }
339                         if (intf_mtu != 0) {
340                                 char mtu[16];
341                                 snprintf(sizeof(mtu), mtu, "%u", intf_mtu);
342                                 setenv("boot.netif.mtu", mtu, 1);
343                         }
344                         printf("pxe_open: server addr: %s\n", inet_ntoa(rootip));
345                         printf("pxe_open: server path: %s\n", rootpath);
346                         printf("pxe_open: gateway ip:  %s\n", inet_ntoa(gateip));
347                         printf("pxe_open: my ip:       %s\n", inet_ntoa(myip));
348                         printf("pxe_open: netmask:     %s\n", intoa(netmask));
349                         printf("pxe_open: servip:      %s\n", inet_ntoa(servip));
350
351                         if (netproto == NET_TFTP) {
352                                 setenv("boot.tftproot.server", inet_ntoa(rootip), 1);
353                                 setenv("boot.tftproot.path", rootpath, 1);
354                         } else if (netproto == NET_NFS) {
355                                 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
356                                 setenv("boot.nfsroot.path", rootpath, 1);
357                         }
358                         setenv("dhcp.host-name", hostname, 1);
359
360                         setenv("pxeboot.ip", inet_ntoa(myip), 1);
361                         if (bootplayer.Hardware == ETHER_TYPE) {
362                                 sprintf(temp, "%6D", bootplayer.CAddr, ":");
363                                 setenv("pxeboot.hwaddr", temp, 1);
364                         }
365                 }
366         }
367         pxe_opens++;
368         f->f_devdata = &pxe_sock;
369         return (error);
370 }
371
372 static int
373 pxe_close(struct open_file *f)
374 {
375
376 #ifdef  PXE_DEBUG
377         if (pxe_debug)
378                 printf("pxe_close: opens=%d\n", pxe_opens);
379 #endif
380
381         /* On last close, do netif close, etc. */
382         f->f_devdata = NULL;
383         /* Extra close call? */
384         if (pxe_opens <= 0)
385                 return (0);
386         pxe_opens--;
387         /* Not last close? */
388         if (pxe_opens > 0)
389                 return (0);
390
391         if (netproto == NET_NFS) {
392                 /* get an NFS filehandle for our root filesystem */
393                 pxe_setnfshandle(rootpath);
394         }
395
396         if (pxe_sock >= 0) {
397
398 #ifdef PXE_DEBUG
399         if (pxe_debug)
400                 printf("pxe_close: calling netif_close()\n");
401 #endif
402         netif_close(pxe_sock);
403         pxe_sock = -1;
404         }
405         return (0);
406 }
407
408 static int
409 pxe_print(int verbose)
410 {
411         char line[255];
412         if (pxe_call == NULL)
413                 return (0);
414
415         printf("%s devices:", pxedisk.dv_name);
416         if (pager_output("\n") != 0)
417                 return (1);
418         if (verbose) {
419                 snprintf(line, sizeof(line), "    pxe0:    %s:%s\n",
420                     inet_ntoa(rootip), rootpath);
421         } else {
422                 snprintf(line, sizeof(line), "    pxe0:\n");
423         }
424         return (pager_output(line));
425 }
426
427 static void
428 pxe_cleanup(void)
429 {
430 #ifdef PXE_DEBUG
431         t_PXENV_UNLOAD_STACK *unload_stack_p =
432                 (t_PXENV_UNLOAD_STACK *)scratch_buffer;
433         t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p =
434                 (t_PXENV_UNDI_SHUTDOWN *)scratch_buffer;
435 #endif
436
437         if (pxe_call == NULL)
438                 return;
439
440         pxe_call(PXENV_UNDI_SHUTDOWN);
441
442 #ifdef PXE_DEBUG
443         if (pxe_debug && undi_shutdown_p->Status != 0)
444                 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
445                     undi_shutdown_p->Status);
446 #endif
447
448         pxe_call(PXENV_UNLOAD_STACK);
449
450 #ifdef PXE_DEBUG
451         if (pxe_debug && unload_stack_p->Status != 0)
452                 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
453                     unload_stack_p->Status);
454 #endif
455 }
456
457 void
458 pxe_perror(int err)
459 {
460         return;
461 }
462
463 /*
464  * Reach inside the libstand NFS code and dig out an NFS handle
465  * for the root filesystem.
466  */
467 #define NFS_V3MAXFHSIZE         64
468
469 struct nfs_iodesc {
470         struct iodesc *iodesc;
471         off_t off;
472         uint32_t fhsize;
473         u_char fh[NFS_V3MAXFHSIZE];
474         /* structure truncated */
475 };
476 extern struct nfs_iodesc nfs_root_node;
477 extern int rpc_port;
478
479 static void
480 pxe_rpcmountcall()
481 {
482         struct iodesc *d;
483         int error;
484
485         if (!(d = socktodesc(pxe_sock)))
486                 return;
487         d->myport = htons(--rpc_port);
488         d->destip = rootip;
489         if ((error = nfs_getrootfh(d, rootpath, &nfs_root_node.fhsize,
490                 nfs_root_node.fh)) != 0) {
491                 printf("NFS MOUNT RPC error: %d\n", error);
492                 nfs_root_node.fhsize = 0;
493         }
494         nfs_root_node.iodesc = d;
495 }
496
497 static void
498 pxe_setnfshandle(char *rootpath)
499 {
500         int i;
501         u_char *fh;
502         char buf[2 * NFS_V3MAXFHSIZE + 3], *cp;
503
504         /*
505          * If NFS files were never opened, we need to do mount call
506          * ourselves. Use nfs_root_node.iodesc as flag indicating
507          * previous NFS usage.
508          */
509         if (nfs_root_node.iodesc == NULL)
510                 pxe_rpcmountcall();
511
512         fh = &nfs_root_node.fh[0];
513         buf[0] = 'X';
514         cp = &buf[1];
515         for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
516                 sprintf(cp, "%02x", fh[i]);
517         sprintf(cp, "X");
518         setenv("boot.nfsroot.nfshandle", buf, 1);
519         sprintf(buf, "%d", nfs_root_node.fhsize);
520         setenv("boot.nfsroot.nfshandlelen", buf, 1);
521 }
522
523 void
524 pxenv_call(int func)
525 {
526 #ifdef PXE_DEBUG
527         if (pxe_debug)
528                 printf("pxenv_call %x\n", func);
529 #endif
530         
531         bzero(&v86, sizeof(v86));
532         bzero(data_buffer, sizeof(data_buffer));
533
534         __pxenvseg = pxenv_p->RMEntry.segment;
535         __pxenvoff = pxenv_p->RMEntry.offset;
536         
537         v86.ctl  = V86_ADDR | V86_CALLF | V86_FLAGS;
538         v86.es   = VTOPSEG(scratch_buffer);
539         v86.edi  = VTOPOFF(scratch_buffer);
540         v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
541         v86.ebx  = func;
542         v86int();
543         v86.ctl  = V86_FLAGS;
544 }
545
546 void
547 bangpxe_call(int func)
548 {
549 #ifdef PXE_DEBUG
550         if (pxe_debug)
551                 printf("bangpxe_call %x\n", func);
552 #endif
553
554         bzero(&v86, sizeof(v86));
555         bzero(data_buffer, sizeof(data_buffer));
556
557         __bangpxeseg = pxe_p->EntryPointSP.segment;
558         __bangpxeoff = pxe_p->EntryPointSP.offset;
559
560         v86.ctl  = V86_ADDR | V86_CALLF | V86_FLAGS;
561         v86.edx  = VTOPSEG(scratch_buffer);
562         v86.eax  = VTOPOFF(scratch_buffer);
563         v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
564         v86.ebx  = func;
565         v86int();
566         v86.ctl  = V86_FLAGS;
567 }
568
569
570 static int
571 pxe_netif_match(struct netif *nif, void *machdep_hint)
572 {
573         return 1;
574 }
575
576 static int
577 pxe_netif_probe(struct netif *nif, void *machdep_hint)
578 {
579         t_PXENV_UDP_OPEN *udpopen_p = (t_PXENV_UDP_OPEN *)scratch_buffer;
580
581         if (pxe_call == NULL)
582                 return -1;
583
584         bzero(udpopen_p, sizeof(*udpopen_p));
585         udpopen_p->src_ip = bootplayer.yip;
586         pxe_call(PXENV_UDP_OPEN);
587
588         if (udpopen_p->status != 0) {
589                 printf("pxe_netif_probe: failed %x\n", udpopen_p->status);
590                 return -1;
591         }
592         return 0;
593 }
594
595 static void
596 pxe_netif_end(struct netif *nif)
597 {
598         t_PXENV_UDP_CLOSE *udpclose_p = (t_PXENV_UDP_CLOSE *)scratch_buffer;
599         bzero(udpclose_p, sizeof(*udpclose_p));
600
601         pxe_call(PXENV_UDP_CLOSE);
602         if (udpclose_p->status != 0)
603                 printf("pxe_end failed %x\n", udpclose_p->status);
604 }
605
606 static void
607 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
608 {
609         int i;
610         for (i = 0; i < 6; ++i)
611                 desc->myea[i] = bootplayer.CAddr[i];
612         desc->xid = bootplayer.ident;
613 }
614
615 static int
616 pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
617 {
618         return len;
619 }
620
621 static int
622 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
623 {
624         return len;
625 }
626
627 ssize_t
628 sendudp(struct iodesc *h, void *pkt, size_t len)
629 {
630         t_PXENV_UDP_WRITE *udpwrite_p = (t_PXENV_UDP_WRITE *)scratch_buffer;
631         bzero(udpwrite_p, sizeof(*udpwrite_p));
632
633         udpwrite_p->ip             = h->destip.s_addr;
634         udpwrite_p->dst_port       = h->destport;
635         udpwrite_p->src_port       = h->myport;
636         udpwrite_p->buffer_size    = len;
637         udpwrite_p->buffer.segment = VTOPSEG(pkt);
638         udpwrite_p->buffer.offset  = VTOPOFF(pkt);
639
640         if (netmask == 0 || SAMENET(myip, h->destip, netmask))
641                 udpwrite_p->gw = 0;
642         else
643                 udpwrite_p->gw = gateip.s_addr;
644
645         pxe_call(PXENV_UDP_WRITE);
646
647 #if 0
648         /* XXX - I dont know why we need this. */
649         delay(1000);
650 #endif
651         if (udpwrite_p->status != 0) {
652                 /* XXX: This happens a lot. It shouldn't. */
653                 if (udpwrite_p->status != 1)
654                         printf("sendudp failed %x\n", udpwrite_p->status);
655                 return -1;
656         }
657         return len;
658 }
659
660 ssize_t
661 readudp(struct iodesc *h, void *pkt, size_t len, time_t timeout)
662 {
663         t_PXENV_UDP_READ *udpread_p = (t_PXENV_UDP_READ *)scratch_buffer;
664         struct udphdr *uh = NULL;
665
666         uh = (struct udphdr *) pkt - 1;
667         bzero(udpread_p, sizeof(*udpread_p));
668
669         udpread_p->dest_ip        = h->myip.s_addr;
670         udpread_p->d_port         = h->myport;
671         udpread_p->buffer_size    = len;
672         udpread_p->buffer.segment = VTOPSEG(data_buffer);
673         udpread_p->buffer.offset  = VTOPOFF(data_buffer);
674
675         pxe_call(PXENV_UDP_READ);
676
677 #if 0
678         /* XXX - I dont know why we need this. */
679         delay(1000);
680 #endif
681         if (udpread_p->status != 0) {
682                 /* XXX: This happens a lot. It shouldn't. */
683                 if (udpread_p->status != 1)
684                         printf("readudp failed %x\n", udpread_p->status);
685                 return -1;
686         }
687         bcopy(data_buffer, pkt, udpread_p->buffer_size);
688         uh->uh_sport = udpread_p->s_port;
689         return udpread_p->buffer_size;
690 }