loader: updates from review
[unleashed.git] / usr / src / boot / sys / boot / i386 / libi386 / pxe.c
blobcddf8d244e96647830ee429eb9e4443d03d2acb5
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.
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.
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.
29 #include <sys/cdefs.h>
31 #include <stand.h>
32 #include <string.h>
33 #include <stdarg.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/in.h>
37 #include <netinet/udp.h>
39 #include <net.h>
40 #include <netif.h>
41 #include <nfsv2.h>
42 #include <iodesc.h>
44 #include <bootp.h>
45 #include <bootstrap.h>
46 #include "btxv86.h"
47 #include "pxe.h"
50 * Allocate the PXE buffers statically instead of sticking grimy fingers into
51 * BTX's private data area. The scratch buffer is used to send information to
52 * the PXE BIOS, and the data buffer is used to receive data from the PXE BIOS.
54 #define PXE_BUFFER_SIZE 0x2000
55 #define PXE_TFTP_BUFFER_SIZE 512
56 static char scratch_buffer[PXE_BUFFER_SIZE];
57 static char data_buffer[PXE_BUFFER_SIZE];
59 static pxenv_t *pxenv_p = NULL; /* PXENV+ */
60 static pxe_t *pxe_p = NULL; /* !PXE */
61 BOOTPLAYER bootplayer = {0}; /* PXE Cached information. */
63 static int pxe_debug = 0;
64 static int pxe_sock = -1;
65 static int pxe_opens = 0;
67 void pxe_enable(void *pxeinfo);
68 static void (*pxe_call)(int func);
69 static void pxenv_call(int func);
70 static void bangpxe_call(int func);
72 static int pxe_init(void);
73 static int pxe_strategy(void *devdata, int flag, daddr_t dblk,
74 size_t offset, size_t size, char *buf, size_t *rsize);
75 static int pxe_open(struct open_file *f, ...);
76 static int pxe_close(struct open_file *f);
77 static int pxe_print(int verbose);
78 static void pxe_cleanup(void);
79 static void pxe_setnfshandle(char *rootpath);
81 static void pxe_perror(int error);
82 static int pxe_netif_match(struct netif *nif, void *machdep_hint);
83 static int pxe_netif_probe(struct netif *nif, void *machdep_hint);
84 static void pxe_netif_init(struct iodesc *desc, void *machdep_hint);
85 static int pxe_netif_get(struct iodesc *desc, void *pkt, size_t len,
86 time_t timeout);
87 static int pxe_netif_put(struct iodesc *desc, void *pkt, size_t len);
88 static void pxe_netif_end(struct netif *nif);
90 #ifdef OLD_NFSV2
91 int nfs_getrootfh(struct iodesc*, char*, u_char*);
92 #else
93 int nfs_getrootfh(struct iodesc*, char*, uint32_t*, u_char*);
94 #endif
96 extern struct netif_stats pxe_st[];
97 extern u_int16_t __bangpxeseg;
98 extern u_int16_t __bangpxeoff;
99 extern void __bangpxeentry(void);
100 extern u_int16_t __pxenvseg;
101 extern u_int16_t __pxenvoff;
102 extern void __pxenventry(void);
104 struct netif_dif pxe_ifs[] = {
105 /* dif_unit dif_nsel dif_stats dif_private */
106 {0, 1, &pxe_st[0], 0}
109 struct netif_stats pxe_st[NENTS(pxe_ifs)];
111 struct netif_driver pxenetif = {
112 "pxenet",
113 pxe_netif_match,
114 pxe_netif_probe,
115 pxe_netif_init,
116 pxe_netif_get,
117 pxe_netif_put,
118 pxe_netif_end,
119 pxe_ifs,
120 NENTS(pxe_ifs)
123 struct netif_driver *netif_drivers[] = {
124 &pxenetif,
125 NULL
128 struct devsw pxedisk = {
129 "pxe",
130 DEVT_NET,
131 pxe_init,
132 pxe_strategy,
133 pxe_open,
134 pxe_close,
135 noioctl,
136 pxe_print,
137 pxe_cleanup
141 * This function is called by the loader to enable PXE support if we
142 * are booted by PXE. The passed in pointer is a pointer to the
143 * PXENV+ structure.
145 void
146 pxe_enable(void *pxeinfo)
148 pxenv_p = (pxenv_t *)pxeinfo;
149 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
150 pxenv_p->PXEPtr.offset);
151 pxe_call = NULL;
155 * return true if pxe structures are found/initialized,
156 * also figures out our IP information via the pxe cached info struct
158 static int
159 pxe_init(void)
161 t_PXENV_GET_CACHED_INFO *gci_p;
162 int counter;
163 uint8_t checksum;
164 uint8_t *checkptr;
166 if(pxenv_p == NULL)
167 return (0);
169 /* look for "PXENV+" */
170 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
171 pxenv_p = NULL;
172 return (0);
175 /* make sure the size is something we can handle */
176 if (pxenv_p->Length > sizeof(*pxenv_p)) {
177 printf("PXENV+ structure too large, ignoring\n");
178 pxenv_p = NULL;
179 return (0);
183 * do byte checksum:
184 * add up each byte in the structure, the total should be 0
186 checksum = 0;
187 checkptr = (uint8_t *) pxenv_p;
188 for (counter = 0; counter < pxenv_p->Length; counter++)
189 checksum += *checkptr++;
190 if (checksum != 0) {
191 printf("PXENV+ structure failed checksum, ignoring\n");
192 pxenv_p = NULL;
193 return (0);
198 * PXENV+ passed, so use that if !PXE is not available or
199 * the checksum fails.
201 pxe_call = pxenv_call;
202 if (pxenv_p->Version >= 0x0200) {
203 for (;;) {
204 if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
205 pxe_p = NULL;
206 break;
208 checksum = 0;
209 checkptr = (uint8_t *)pxe_p;
210 for (counter = 0; counter < pxe_p->StructLength;
211 counter++)
212 checksum += *checkptr++;
213 if (checksum != 0) {
214 pxe_p = NULL;
215 break;
217 pxe_call = bangpxe_call;
218 break;
222 printf("\nPXE version %d.%d, real mode entry point ",
223 (uint8_t) (pxenv_p->Version >> 8),
224 (uint8_t) (pxenv_p->Version & 0xFF));
225 if (pxe_call == bangpxe_call)
226 printf("@%04x:%04x\n",
227 pxe_p->EntryPointSP.segment,
228 pxe_p->EntryPointSP.offset);
229 else
230 printf("@%04x:%04x\n",
231 pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
233 gci_p = (t_PXENV_GET_CACHED_INFO *) scratch_buffer;
234 bzero(gci_p, sizeof(*gci_p));
235 gci_p->PacketType = PXENV_PACKET_TYPE_BINL_REPLY;
236 pxe_call(PXENV_GET_CACHED_INFO);
237 if (gci_p->Status != 0) {
238 pxe_perror(gci_p->Status);
239 pxe_p = NULL;
240 return (0);
242 bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
243 &bootplayer, gci_p->BufferSize);
244 return (1);
248 static int
249 pxe_strategy(void *devdata, int flag, daddr_t dblk, size_t offset, size_t size,
250 char *buf, size_t *rsize)
252 return (EIO);
255 static int
256 pxe_open(struct open_file *f, ...)
258 va_list args;
259 char *devname; /* Device part of file name (or NULL). */
260 char temp[FNAME_SIZE];
261 int error = 0;
262 int i;
264 va_start(args, f);
265 devname = va_arg(args, char*);
266 va_end(args);
268 /* On first open, do netif open, mount, etc. */
269 if (pxe_opens == 0) {
270 /* Find network interface. */
271 if (pxe_sock < 0) {
272 pxe_sock = netif_open(devname);
273 if (pxe_sock < 0) {
274 printf("pxe_open: netif_open() failed\n");
275 return (ENXIO);
277 if (pxe_debug)
278 printf("pxe_open: netif_open() succeeded\n");
280 if (rootip.s_addr == 0) {
282 * Do a bootp/dhcp request to find out where our
283 * NFS/TFTP server is. Even if we dont get back
284 * the proper information, fall back to the server
285 * which brought us to life and a default rootpath.
287 bootp(pxe_sock, BOOTP_PXE);
288 if (rootip.s_addr == 0)
289 rootip.s_addr = bootplayer.sip;
290 #ifdef LOADER_NFS_SUPPORT
291 if (!rootpath[0])
292 strcpy(rootpath, PXENFSROOTPATH);
293 #endif
295 for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
296 if (rootpath[i] == ':')
297 break;
298 if (i && i != FNAME_SIZE && rootpath[i] == ':') {
299 rootpath[i++] = '\0';
300 if (inet_addr(&rootpath[0]) != INADDR_NONE)
301 rootip.s_addr = inet_addr(&rootpath[0]);
302 bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
303 bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);
305 setenv("boot.netif.ip", inet_ntoa(myip), 1);
306 setenv("boot.netif.netmask", intoa(netmask), 1);
307 setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
308 if (bootplayer.Hardware == ETHER_TYPE) {
309 sprintf(temp, "%6D", bootplayer.CAddr, ":");
310 setenv("boot.netif.hwaddr", temp, 1);
312 #ifdef LOADER_NFS_SUPPORT
313 printf("pxe_open: server addr: %s\n", inet_ntoa(rootip));
314 printf("pxe_open: server path: %s\n", rootpath);
315 printf("pxe_open: gateway ip: %s\n", inet_ntoa(gateip));
317 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
318 setenv("boot.nfsroot.path", rootpath, 1);
319 #else
320 setenv("boot.netif.server", inet_ntoa(rootip), 1);
321 setenv("boot.tftproot.path", rootpath, 1);
322 #endif
323 setenv("dhcp.host-name", hostname, 1);
325 setenv("pxeboot.ip", inet_ntoa(myip), 1);
326 if (bootplayer.Hardware == ETHER_TYPE) {
327 sprintf(temp, "%6D", bootplayer.CAddr, ":");
328 setenv("pxeboot.hwaddr", temp, 1);
332 pxe_opens++;
333 f->f_devdata = &pxe_sock;
334 return (error);
337 static int
338 pxe_close(struct open_file *f)
341 #ifdef PXE_DEBUG
342 if (pxe_debug)
343 printf("pxe_close: opens=%d\n", pxe_opens);
344 #endif
346 /* On last close, do netif close, etc. */
347 f->f_devdata = NULL;
348 /* Extra close call? */
349 if (pxe_opens <= 0)
350 return (0);
351 pxe_opens--;
352 /* Not last close? */
353 if (pxe_opens > 0)
354 return(0);
356 #ifdef LOADER_NFS_SUPPORT
357 /* get an NFS filehandle for our root filesystem */
358 pxe_setnfshandle(rootpath);
359 #endif
361 if (pxe_sock >= 0) {
363 #ifdef PXE_DEBUG
364 if (pxe_debug)
365 printf("pxe_close: calling netif_close()\n");
366 #endif
367 netif_close(pxe_sock);
368 pxe_sock = -1;
370 return (0);
373 static int
374 pxe_print(int verbose)
376 char line[255];
377 if (pxe_call == NULL)
378 return (0);
380 snprintf(line, sizeof (line), " pxe0: %s:%s\n", inet_ntoa(rootip),
381 rootpath);
382 return (pager_output(line));
385 static void
386 pxe_cleanup(void)
388 #ifdef PXE_DEBUG
389 t_PXENV_UNLOAD_STACK *unload_stack_p =
390 (t_PXENV_UNLOAD_STACK *)scratch_buffer;
391 t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p =
392 (t_PXENV_UNDI_SHUTDOWN *)scratch_buffer;
393 #endif
395 if (pxe_call == NULL)
396 return;
398 pxe_call(PXENV_UNDI_SHUTDOWN);
400 #ifdef PXE_DEBUG
401 if (pxe_debug && undi_shutdown_p->Status != 0)
402 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
403 undi_shutdown_p->Status);
404 #endif
406 pxe_call(PXENV_UNLOAD_STACK);
408 #ifdef PXE_DEBUG
409 if (pxe_debug && unload_stack_p->Status != 0)
410 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
411 unload_stack_p->Status);
412 #endif
415 void
416 pxe_perror(int err)
418 return;
421 #ifdef LOADER_NFS_SUPPORT
423 * Reach inside the libstand NFS code and dig out an NFS handle
424 * for the root filesystem.
426 #ifdef OLD_NFSV2
427 struct nfs_iodesc {
428 struct iodesc *iodesc;
429 off_t off;
430 u_char fh[NFS_FHSIZE];
431 /* structure truncated here */
433 extern struct nfs_iodesc nfs_root_node;
434 extern int rpc_port;
436 static void
437 pxe_rpcmountcall()
439 struct iodesc *d;
440 int error;
442 if (!(d = socktodesc(pxe_sock)))
443 return;
444 d->myport = htons(--rpc_port);
445 d->destip = rootip;
446 if ((error = nfs_getrootfh(d, rootpath, nfs_root_node.fh)) != 0)
447 printf("NFS MOUNT RPC error: %d\n", error);
448 nfs_root_node.iodesc = d;
451 static void
452 pxe_setnfshandle(char *rootpath)
454 int i;
455 u_char *fh;
456 char buf[2 * NFS_FHSIZE + 3], *cp;
459 * If NFS files were never opened, we need to do mount call
460 * ourselves. Use nfs_root_node.iodesc as flag indicating
461 * previous NFS usage.
463 if (nfs_root_node.iodesc == NULL)
464 pxe_rpcmountcall();
466 fh = &nfs_root_node.fh[0];
467 buf[0] = 'X';
468 cp = &buf[1];
469 for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
470 sprintf(cp, "%02x", fh[i]);
471 sprintf(cp, "X");
472 setenv("boot.nfsroot.nfshandle", buf, 1);
474 #else /* !OLD_NFSV2 */
476 #define NFS_V3MAXFHSIZE 64
478 struct nfs_iodesc {
479 struct iodesc *iodesc;
480 off_t off;
481 uint32_t fhsize;
482 u_char fh[NFS_V3MAXFHSIZE];
483 /* structure truncated */
485 extern struct nfs_iodesc nfs_root_node;
486 extern int rpc_port;
488 static void
489 pxe_rpcmountcall()
491 struct iodesc *d;
492 int error;
494 if (!(d = socktodesc(pxe_sock)))
495 return;
496 d->myport = htons(--rpc_port);
497 d->destip = rootip;
498 if ((error = nfs_getrootfh(d, rootpath, &nfs_root_node.fhsize,
499 nfs_root_node.fh)) != 0) {
500 printf("NFS MOUNT RPC error: %d\n", error);
501 nfs_root_node.fhsize = 0;
503 nfs_root_node.iodesc = d;
506 static void
507 pxe_setnfshandle(char *rootpath)
509 int i;
510 u_char *fh;
511 char buf[2 * NFS_V3MAXFHSIZE + 3], *cp;
514 * If NFS files were never opened, we need to do mount call
515 * ourselves. Use nfs_root_node.iodesc as flag indicating
516 * previous NFS usage.
518 if (nfs_root_node.iodesc == NULL)
519 pxe_rpcmountcall();
521 fh = &nfs_root_node.fh[0];
522 buf[0] = 'X';
523 cp = &buf[1];
524 for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
525 sprintf(cp, "%02x", fh[i]);
526 sprintf(cp, "X");
527 setenv("boot.nfsroot.nfshandle", buf, 1);
528 sprintf(buf, "%d", nfs_root_node.fhsize);
529 setenv("boot.nfsroot.nfshandlelen", buf, 1);
531 #endif /* OLD_NFSV2 */
532 #endif /* LOADER_NFS_SUPPORT */
534 void
535 pxenv_call(int func)
537 #ifdef PXE_DEBUG
538 if (pxe_debug)
539 printf("pxenv_call %x\n", func);
540 #endif
542 bzero(&v86, sizeof(v86));
543 bzero(data_buffer, sizeof(data_buffer));
545 __pxenvseg = pxenv_p->RMEntry.segment;
546 __pxenvoff = pxenv_p->RMEntry.offset;
548 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
549 v86.es = VTOPSEG(scratch_buffer);
550 v86.edi = VTOPOFF(scratch_buffer);
551 v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
552 v86.ebx = func;
553 v86int();
554 v86.ctl = V86_FLAGS;
557 void
558 bangpxe_call(int func)
560 #ifdef PXE_DEBUG
561 if (pxe_debug)
562 printf("bangpxe_call %x\n", func);
563 #endif
565 bzero(&v86, sizeof(v86));
566 bzero(data_buffer, sizeof(data_buffer));
568 __bangpxeseg = pxe_p->EntryPointSP.segment;
569 __bangpxeoff = pxe_p->EntryPointSP.offset;
571 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
572 v86.edx = VTOPSEG(scratch_buffer);
573 v86.eax = VTOPOFF(scratch_buffer);
574 v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
575 v86.ebx = func;
576 v86int();
577 v86.ctl = V86_FLAGS;
581 time_t
582 getsecs()
584 time_t n = 0;
585 time(&n);
586 return n;
589 static int
590 pxe_netif_match(struct netif *nif, void *machdep_hint)
592 return 1;
596 static int
597 pxe_netif_probe(struct netif *nif, void *machdep_hint)
599 t_PXENV_UDP_OPEN *udpopen_p = (t_PXENV_UDP_OPEN *)scratch_buffer;
601 if (pxe_call == NULL)
602 return -1;
604 bzero(udpopen_p, sizeof(*udpopen_p));
605 udpopen_p->src_ip = bootplayer.yip;
606 pxe_call(PXENV_UDP_OPEN);
608 if (udpopen_p->status != 0) {
609 printf("pxe_netif_probe: failed %x\n", udpopen_p->status);
610 return -1;
612 return 0;
615 static void
616 pxe_netif_end(struct netif *nif)
618 t_PXENV_UDP_CLOSE *udpclose_p = (t_PXENV_UDP_CLOSE *)scratch_buffer;
619 bzero(udpclose_p, sizeof(*udpclose_p));
621 pxe_call(PXENV_UDP_CLOSE);
622 if (udpclose_p->status != 0)
623 printf("pxe_end failed %x\n", udpclose_p->status);
626 static void
627 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
629 int i;
630 for (i = 0; i < 6; ++i)
631 desc->myea[i] = bootplayer.CAddr[i];
632 desc->xid = bootplayer.ident;
635 static int
636 pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
638 return len;
641 static int
642 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
644 return len;
647 ssize_t
648 sendudp(struct iodesc *h, void *pkt, size_t len)
650 t_PXENV_UDP_WRITE *udpwrite_p = (t_PXENV_UDP_WRITE *)scratch_buffer;
651 bzero(udpwrite_p, sizeof(*udpwrite_p));
653 udpwrite_p->ip = h->destip.s_addr;
654 udpwrite_p->dst_port = h->destport;
655 udpwrite_p->src_port = h->myport;
656 udpwrite_p->buffer_size = len;
657 udpwrite_p->buffer.segment = VTOPSEG(pkt);
658 udpwrite_p->buffer.offset = VTOPOFF(pkt);
660 if (netmask == 0 || SAMENET(myip, h->destip, netmask))
661 udpwrite_p->gw = 0;
662 else
663 udpwrite_p->gw = gateip.s_addr;
665 pxe_call(PXENV_UDP_WRITE);
667 #if 0
668 /* XXX - I dont know why we need this. */
669 delay(1000);
670 #endif
671 if (udpwrite_p->status != 0) {
672 /* XXX: This happens a lot. It shouldn't. */
673 if (udpwrite_p->status != 1)
674 printf("sendudp failed %x\n", udpwrite_p->status);
675 return -1;
677 return len;
680 ssize_t
681 readudp(struct iodesc *h, void *pkt, size_t len, time_t timeout)
683 t_PXENV_UDP_READ *udpread_p = (t_PXENV_UDP_READ *)scratch_buffer;
684 struct udphdr *uh = NULL;
686 uh = (struct udphdr *) pkt - 1;
687 bzero(udpread_p, sizeof(*udpread_p));
689 udpread_p->dest_ip = h->myip.s_addr;
690 udpread_p->d_port = h->myport;
691 udpread_p->buffer_size = len;
692 udpread_p->buffer.segment = VTOPSEG(data_buffer);
693 udpread_p->buffer.offset = VTOPOFF(data_buffer);
695 pxe_call(PXENV_UDP_READ);
697 #if 0
698 /* XXX - I dont know why we need this. */
699 delay(1000);
700 #endif
701 if (udpread_p->status != 0) {
702 /* XXX: This happens a lot. It shouldn't. */
703 if (udpread_p->status != 1)
704 printf("readudp failed %x\n", udpread_p->status);
705 return -1;
707 bcopy(data_buffer, pkt, udpread_p->buffer_size);
708 uh->uh_sport = udpread_p->s_port;
709 return udpread_p->buffer_size;