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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
36 #include <netinet/in_systm.h>
37 #include <netinet/in.h>
38 #include <netinet/udp.h>
46 #include <bootstrap.h>
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.
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];
60 static pxenv_t *pxenv_p = NULL; /* PXENV+ */
61 static pxe_t *pxe_p = NULL; /* !PXE */
62 static BOOTPLAYER bootplayer; /* PXE Cached information. */
64 static int pxe_debug = 0;
65 static int pxe_sock = -1;
66 static int pxe_opens = 0;
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);
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);
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,
88 static int pxe_netif_put(struct iodesc *desc, void *pkt, size_t len);
89 static void pxe_netif_end(struct netif *nif);
92 int nfs_getrootfh(struct iodesc*, char*, u_char*);
94 int nfs_getrootfh(struct iodesc*, char*, uint32_t*, u_char*);
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);
105 struct netif_dif pxe_ifs[] = {
106 /* dif_unit dif_nsel dif_stats dif_private */
107 {0, 1, &pxe_st[0], 0}
110 struct netif_stats pxe_st[NENTS(pxe_ifs)];
112 struct netif_driver pxenetif = {
124 struct netif_driver *netif_drivers[] = {
129 struct devsw pxedisk = {
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
147 pxe_enable(void *pxeinfo)
149 pxenv_p = (pxenv_t *)pxeinfo;
150 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
151 pxenv_p->PXEPtr.offset);
156 * return true if pxe structures are found/initialized,
157 * also figures out our IP information via the pxe cached info struct
162 t_PXENV_GET_CACHED_INFO *gci_p;
170 /* look for "PXENV+" */
171 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
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");
185 * add up each byte in the structure, the total should be 0
188 checkptr = (uint8_t *) pxenv_p;
189 for (counter = 0; counter < pxenv_p->Length; counter++)
190 checksum += *checkptr++;
192 printf("PXENV+ structure failed checksum, ignoring\n");
199 * PXENV+ passed, so use that if !PXE is not available or
200 * the checksum fails.
202 pxe_call = pxenv_call;
203 if (pxenv_p->Version >= 0x0200) {
205 if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
210 checkptr = (uint8_t *)pxe_p;
211 for (counter = 0; counter < pxe_p->StructLength;
213 checksum += *checkptr++;
218 pxe_call = bangpxe_call;
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);
231 printf("@%04x:%04x\n",
232 pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
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);
243 bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
244 &bootplayer, gci_p->BufferSize);
250 pxe_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
251 char *buf, size_t *rsize)
257 pxe_open(struct open_file *f, ...)
260 char *devname; /* Device part of file name (or NULL). */
261 char temp[FNAME_SIZE];
266 devname = va_arg(args, char*);
269 /* On first open, do netif open, mount, etc. */
270 if (pxe_opens == 0) {
271 /* Find network interface. */
273 pxe_sock = netif_open(devname);
275 printf("pxe_open: netif_open() failed\n");
279 printf("pxe_open: netif_open() succeeded\n");
281 if (rootip.s_addr == 0) {
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.
288 bootp(pxe_sock, BOOTP_PXE);
289 if (rootip.s_addr == 0)
290 rootip.s_addr = bootplayer.sip;
292 strcpy(rootpath, PXENFSROOTPATH);
294 for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
295 if (rootpath[i] == ':')
297 if (i && i != FNAME_SIZE && rootpath[i] == ':') {
298 rootpath[i++] = '\0';
299 if (inet_addr(&rootpath[0]) != INADDR_NONE)
300 rootip.s_addr = inet_addr(&rootpath[0]);
301 bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
302 bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);
304 printf("pxe_open: server addr: %s\n", inet_ntoa(rootip));
305 printf("pxe_open: server path: %s\n", rootpath);
306 printf("pxe_open: gateway ip: %s\n", inet_ntoa(gateip));
308 setenv("boot.netif.ip", inet_ntoa(myip), 1);
309 setenv("boot.netif.netmask", intoa(netmask), 1);
310 setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
311 if (bootplayer.Hardware == ETHER_TYPE) {
312 sprintf(temp, "%6D", bootplayer.CAddr, ":");
313 setenv("boot.netif.hwaddr", temp, 1);
315 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
316 setenv("boot.nfsroot.path", rootpath, 1);
317 setenv("dhcp.host-name", hostname, 1);
321 f->f_devdata = &pxe_sock;
326 pxe_close(struct open_file *f)
331 printf("pxe_close: opens=%d\n", pxe_opens);
334 /* On last close, do netif close, etc. */
336 /* Extra close call? */
340 /* Not last close? */
344 #ifdef LOADER_NFS_SUPPORT
345 /* get an NFS filehandle for our root filesystem */
346 pxe_setnfshandle(rootpath);
353 printf("pxe_close: calling netif_close()\n");
355 netif_close(pxe_sock);
362 pxe_print(int verbose)
365 if (pxe_call == NULL)
368 printf(" pxe0: %s:%s\n", inet_ntoa(rootip), rootpath);
375 t_PXENV_UNLOAD_STACK *unload_stack_p =
376 (t_PXENV_UNLOAD_STACK *)scratch_buffer;
377 t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p =
378 (t_PXENV_UNDI_SHUTDOWN *)scratch_buffer;
381 if (pxe_call == NULL)
384 pxe_call(PXENV_UNDI_SHUTDOWN);
387 if (pxe_debug && undi_shutdown_p->Status != 0)
388 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
389 undi_shutdown_p->Status);
392 pxe_call(PXENV_UNLOAD_STACK);
395 if (pxe_debug && unload_stack_p->Status != 0)
396 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
397 unload_stack_p->Status);
407 #ifdef LOADER_NFS_SUPPORT
409 * Reach inside the libstand NFS code and dig out an NFS handle
410 * for the root filesystem.
414 struct iodesc *iodesc;
416 u_char fh[NFS_FHSIZE];
417 /* structure truncated here */
419 extern struct nfs_iodesc nfs_root_node;
428 if (!(d = socktodesc(pxe_sock)))
430 d->myport = htons(--rpc_port);
432 if ((error = nfs_getrootfh(d, rootpath, nfs_root_node.fh)) != 0)
433 printf("NFS MOUNT RPC error: %d\n", error);
434 nfs_root_node.iodesc = d;
438 pxe_setnfshandle(char *rootpath)
442 char buf[2 * NFS_FHSIZE + 3], *cp;
445 * If NFS files were never opened, we need to do mount call
446 * ourselves. Use nfs_root_node.iodesc as flag indicating
447 * previous NFS usage.
449 if (nfs_root_node.iodesc == NULL)
452 fh = &nfs_root_node.fh[0];
455 for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
456 sprintf(cp, "%02x", fh[i]);
458 setenv("boot.nfsroot.nfshandle", buf, 1);
460 #else /* !OLD_NFSV2 */
462 #define NFS_V3MAXFHSIZE 64
465 struct iodesc *iodesc;
468 u_char fh[NFS_V3MAXFHSIZE];
469 /* structure truncated */
471 extern struct nfs_iodesc nfs_root_node;
480 if (!(d = socktodesc(pxe_sock)))
482 d->myport = htons(--rpc_port);
484 if ((error = nfs_getrootfh(d, rootpath, &nfs_root_node.fhsize,
485 nfs_root_node.fh)) != 0) {
486 printf("NFS MOUNT RPC error: %d\n", error);
487 nfs_root_node.fhsize = 0;
489 nfs_root_node.iodesc = d;
493 pxe_setnfshandle(char *rootpath)
497 char buf[2 * NFS_V3MAXFHSIZE + 3], *cp;
500 * If NFS files were never opened, we need to do mount call
501 * ourselves. Use nfs_root_node.iodesc as flag indicating
502 * previous NFS usage.
504 if (nfs_root_node.iodesc == NULL)
507 fh = &nfs_root_node.fh[0];
510 for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
511 sprintf(cp, "%02x", fh[i]);
513 setenv("boot.nfsroot.nfshandle", buf, 1);
514 sprintf(buf, "%d", nfs_root_node.fhsize);
515 setenv("boot.nfsroot.nfshandlelen", buf, 1);
517 #endif /* OLD_NFSV2 */
518 #endif /* LOADER_NFS_SUPPORT */
525 printf("pxenv_call %x\n", func);
528 bzero(&v86, sizeof(v86));
529 bzero(data_buffer, sizeof(data_buffer));
531 __pxenvseg = pxenv_p->RMEntry.segment;
532 __pxenvoff = pxenv_p->RMEntry.offset;
534 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
535 v86.es = VTOPSEG(scratch_buffer);
536 v86.edi = VTOPOFF(scratch_buffer);
537 v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
544 bangpxe_call(int func)
548 printf("bangpxe_call %x\n", func);
551 bzero(&v86, sizeof(v86));
552 bzero(data_buffer, sizeof(data_buffer));
554 __bangpxeseg = pxe_p->EntryPointSP.segment;
555 __bangpxeoff = pxe_p->EntryPointSP.offset;
557 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
558 v86.edx = VTOPSEG(scratch_buffer);
559 v86.eax = VTOPOFF(scratch_buffer);
560 v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
576 pxe_netif_match(struct netif *nif, void *machdep_hint)
583 pxe_netif_probe(struct netif *nif, void *machdep_hint)
585 t_PXENV_UDP_OPEN *udpopen_p = (t_PXENV_UDP_OPEN *)scratch_buffer;
587 if (pxe_call == NULL)
590 bzero(udpopen_p, sizeof(*udpopen_p));
591 udpopen_p->src_ip = bootplayer.yip;
592 pxe_call(PXENV_UDP_OPEN);
594 if (udpopen_p->status != 0) {
595 printf("pxe_netif_probe: failed %x\n", udpopen_p->status);
602 pxe_netif_end(struct netif *nif)
604 t_PXENV_UDP_CLOSE *udpclose_p = (t_PXENV_UDP_CLOSE *)scratch_buffer;
605 bzero(udpclose_p, sizeof(*udpclose_p));
607 pxe_call(PXENV_UDP_CLOSE);
608 if (udpclose_p->status != 0)
609 printf("pxe_end failed %x\n", udpclose_p->status);
613 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
616 for (i = 0; i < 6; ++i)
617 desc->myea[i] = bootplayer.CAddr[i];
618 desc->xid = bootplayer.ident;
622 pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
628 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
634 sendudp(struct iodesc *h, void *pkt, size_t len)
636 t_PXENV_UDP_WRITE *udpwrite_p = (t_PXENV_UDP_WRITE *)scratch_buffer;
637 bzero(udpwrite_p, sizeof(*udpwrite_p));
639 udpwrite_p->ip = h->destip.s_addr;
640 udpwrite_p->dst_port = h->destport;
641 udpwrite_p->src_port = h->myport;
642 udpwrite_p->buffer_size = len;
643 udpwrite_p->buffer.segment = VTOPSEG(pkt);
644 udpwrite_p->buffer.offset = VTOPOFF(pkt);
646 if (netmask == 0 || SAMENET(myip, h->destip, netmask))
649 udpwrite_p->gw = gateip.s_addr;
651 pxe_call(PXENV_UDP_WRITE);
654 /* XXX - I dont know why we need this. */
657 if (udpwrite_p->status != 0) {
658 /* XXX: This happens a lot. It shouldn't. */
659 if (udpwrite_p->status != 1)
660 printf("sendudp failed %x\n", udpwrite_p->status);
667 readudp(struct iodesc *h, void *pkt, size_t len, time_t timeout)
669 t_PXENV_UDP_READ *udpread_p = (t_PXENV_UDP_READ *)scratch_buffer;
670 struct udphdr *uh = NULL;
672 uh = (struct udphdr *) pkt - 1;
673 bzero(udpread_p, sizeof(*udpread_p));
675 udpread_p->dest_ip = h->myip.s_addr;
676 udpread_p->d_port = h->myport;
677 udpread_p->buffer_size = len;
678 udpread_p->buffer.segment = VTOPSEG(data_buffer);
679 udpread_p->buffer.offset = VTOPOFF(data_buffer);
681 pxe_call(PXENV_UDP_READ);
684 /* XXX - I dont know why we need this. */
687 if (udpread_p->status != 0) {
688 /* XXX: This happens a lot. It shouldn't. */
689 if (udpread_p->status != 1)
690 printf("readudp failed %x\n", udpread_p->status);
693 bcopy(data_buffer, pkt, udpread_p->buffer_size);
694 uh->uh_sport = udpread_p->s_port;
695 return udpread_p->buffer_size;