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 int 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);
91 int nfs_getrootfh(struct iodesc*, char*, uint32_t*, u_char*);
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;
102 struct netif_dif pxe_ifs[] = {
103 /* dif_unit dif_nsel dif_stats dif_private */
104 {0, 1, &pxe_st[0], 0}
107 struct netif_stats pxe_st[NENTS(pxe_ifs)];
109 struct netif_driver pxenetif = {
121 struct netif_driver *netif_drivers[] = {
126 struct devsw pxedisk = {
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+
144 pxe_enable(void *pxeinfo)
146 pxenv_p = (pxenv_t *)pxeinfo;
147 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
148 pxenv_p->PXEPtr.offset);
153 * return true if pxe structures are found/initialized,
154 * also figures out our IP information via the pxe cached info struct
159 t_PXENV_GET_CACHED_INFO *gci_p;
167 /* look for "PXENV+" */
168 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
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");
182 * add up each byte in the structure, the total should be 0
185 checkptr = (uint8_t *) pxenv_p;
186 for (counter = 0; counter < pxenv_p->Length; counter++)
187 checksum += *checkptr++;
189 printf("PXENV+ structure failed checksum, ignoring\n");
195 * PXENV+ passed, so use that if !PXE is not available or
196 * the checksum fails.
198 pxe_call = pxenv_call;
199 if (pxenv_p->Version >= 0x0200) {
201 if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
206 checkptr = (uint8_t *)pxe_p;
207 for (counter = 0; counter < pxe_p->StructLength;
209 checksum += *checkptr++;
214 pxe_call = bangpxe_call;
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);
227 printf("@%04x:%04x\n",
228 pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
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);
239 bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
240 &bootplayer, gci_p->BufferSize);
246 pxe_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
247 char *buf, size_t *rsize)
253 pxe_open(struct open_file *f, ...)
256 char *devname; /* Device part of file name (or NULL). */
257 char temp[FNAME_SIZE];
262 devname = va_arg(args, char*);
265 /* On first open, do netif open, mount, etc. */
266 if (pxe_opens == 0) {
267 /* Find network interface. */
269 pxe_sock = netif_open(devname);
271 printf("pxe_open: netif_open() failed\n");
275 printf("pxe_open: netif_open() succeeded\n");
277 if (socktodesc(pxe_sock) == NULL) {
278 printf("pxe_open: bad socket %d\n", pxe_sock);
283 if (rootip.s_addr == 0) {
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.
292 if (dhcp_try_rfc1048(bootplayer.vendor.d, BOOTP_DHCPVEND) < 0) {
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");
300 #ifdef LOADER_TFTP_SUPPORT
301 bootp(pxe_sock, BOOTP_PXE);
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)
313 if (tftpip.s_addr != 0) {
315 rootip.s_addr = tftpip.s_addr;
318 if (netproto == NET_NFS && !rootpath[0])
319 strcpy(rootpath, PXENFSROOTPATH);
321 for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
322 if (rootpath[i] == ':')
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);
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);
341 snprintf(sizeof(mtu), mtu, "%u", intf_mtu);
342 setenv("boot.netif.mtu", mtu, 1);
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));
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);
358 setenv("dhcp.host-name", hostname, 1);
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);
368 f->f_devdata = &pxe_sock;
373 pxe_close(struct open_file *f)
378 printf("pxe_close: opens=%d\n", pxe_opens);
381 /* On last close, do netif close, etc. */
383 /* Extra close call? */
387 /* Not last close? */
391 if (netproto == NET_NFS) {
392 /* get an NFS filehandle for our root filesystem */
393 pxe_setnfshandle(rootpath);
400 printf("pxe_close: calling netif_close()\n");
402 netif_close(pxe_sock);
409 pxe_print(int verbose)
412 if (pxe_call == NULL)
415 printf("%s devices:", pxedisk.dv_name);
416 if (pager_output("\n") != 0)
419 snprintf(line, sizeof(line), " pxe0: %s:%s\n",
420 inet_ntoa(rootip), rootpath);
422 snprintf(line, sizeof(line), " pxe0:\n");
424 return (pager_output(line));
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;
437 if (pxe_call == NULL)
440 pxe_call(PXENV_UNDI_SHUTDOWN);
443 if (pxe_debug && undi_shutdown_p->Status != 0)
444 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
445 undi_shutdown_p->Status);
448 pxe_call(PXENV_UNLOAD_STACK);
451 if (pxe_debug && unload_stack_p->Status != 0)
452 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
453 unload_stack_p->Status);
464 * Reach inside the libstand NFS code and dig out an NFS handle
465 * for the root filesystem.
467 #define NFS_V3MAXFHSIZE 64
470 struct iodesc *iodesc;
473 u_char fh[NFS_V3MAXFHSIZE];
474 /* structure truncated */
476 extern struct nfs_iodesc nfs_root_node;
485 if (!(d = socktodesc(pxe_sock)))
487 d->myport = htons(--rpc_port);
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;
494 nfs_root_node.iodesc = d;
498 pxe_setnfshandle(char *rootpath)
502 char buf[2 * NFS_V3MAXFHSIZE + 3], *cp;
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.
509 if (nfs_root_node.iodesc == NULL)
512 fh = &nfs_root_node.fh[0];
515 for (i = 0; i < nfs_root_node.fhsize; i++, cp += 2)
516 sprintf(cp, "%02x", fh[i]);
518 setenv("boot.nfsroot.nfshandle", buf, 1);
519 sprintf(buf, "%d", nfs_root_node.fhsize);
520 setenv("boot.nfsroot.nfshandlelen", buf, 1);
528 printf("pxenv_call %x\n", func);
531 bzero(&v86, sizeof(v86));
532 bzero(data_buffer, sizeof(data_buffer));
534 __pxenvseg = pxenv_p->RMEntry.segment;
535 __pxenvoff = pxenv_p->RMEntry.offset;
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);
547 bangpxe_call(int func)
551 printf("bangpxe_call %x\n", func);
554 bzero(&v86, sizeof(v86));
555 bzero(data_buffer, sizeof(data_buffer));
557 __bangpxeseg = pxe_p->EntryPointSP.segment;
558 __bangpxeoff = pxe_p->EntryPointSP.offset;
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);
571 pxe_netif_match(struct netif *nif, void *machdep_hint)
577 pxe_netif_probe(struct netif *nif, void *machdep_hint)
579 t_PXENV_UDP_OPEN *udpopen_p = (t_PXENV_UDP_OPEN *)scratch_buffer;
581 if (pxe_call == NULL)
584 bzero(udpopen_p, sizeof(*udpopen_p));
585 udpopen_p->src_ip = bootplayer.yip;
586 pxe_call(PXENV_UDP_OPEN);
588 if (udpopen_p->status != 0) {
589 printf("pxe_netif_probe: failed %x\n", udpopen_p->status);
596 pxe_netif_end(struct netif *nif)
598 t_PXENV_UDP_CLOSE *udpclose_p = (t_PXENV_UDP_CLOSE *)scratch_buffer;
599 bzero(udpclose_p, sizeof(*udpclose_p));
601 pxe_call(PXENV_UDP_CLOSE);
602 if (udpclose_p->status != 0)
603 printf("pxe_end failed %x\n", udpclose_p->status);
607 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
610 for (i = 0; i < 6; ++i)
611 desc->myea[i] = bootplayer.CAddr[i];
612 desc->xid = bootplayer.ident;
616 pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
622 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
628 sendudp(struct iodesc *h, void *pkt, size_t len)
630 t_PXENV_UDP_WRITE *udpwrite_p = (t_PXENV_UDP_WRITE *)scratch_buffer;
631 bzero(udpwrite_p, sizeof(*udpwrite_p));
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);
640 if (netmask == 0 || SAMENET(myip, h->destip, netmask))
643 udpwrite_p->gw = gateip.s_addr;
645 pxe_call(PXENV_UDP_WRITE);
648 /* XXX - I dont know why we need this. */
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);
661 readudp(struct iodesc *h, void *pkt, size_t len, time_t timeout)
663 t_PXENV_UDP_READ *udpread_p = (t_PXENV_UDP_READ *)scratch_buffer;
664 struct udphdr *uh = NULL;
666 uh = (struct udphdr *) pkt - 1;
667 bzero(udpread_p, sizeof(*udpread_p));
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);
675 pxe_call(PXENV_UDP_READ);
678 /* XXX - I dont know why we need this. */
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);
687 bcopy(data_buffer, pkt, udpread_p->buffer_size);
688 uh->uh_sport = udpread_p->s_port;
689 return udpread_p->buffer_size;