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
28 * $FreeBSD: src/sys/boot/i386/libi386/pxe.c,v 1.20 2003/08/25 23:28:31 obrien Exp $
29 * $DragonFly: src/sys/boot/pc32/libi386/pxe.c,v 1.8 2005/12/10 00:39:48 swildner Exp $
32 #include <sys/param.h>
37 #include <netinet/in_systm.h>
38 #include <netinet/in.h>
39 #include <netinet/udp.h>
40 #include <netinet/ip.h>
48 #include <bootstrap.h>
53 * Allocate the PXE buffers statically instead of sticking grimy fingers into
54 * BTX's private data area. The scratch buffer is used to send information to
55 * the PXE BIOS, and the data buffer is used to receive data from the PXE BIOS.
57 #define PXE_BUFFER_SIZE 0x2000
58 #define PXE_TFTP_BUFFER_SIZE 512
59 static char scratch_buffer[PXE_BUFFER_SIZE];
60 static char data_buffer[PXE_BUFFER_SIZE];
62 static pxenv_t *pxenv_p = NULL; /* PXENV+ */
63 static pxe_t *pxe_p = NULL; /* !PXE */
64 static BOOTPLAYER bootplayer; /* PXE Cached information. */
66 static int pxe_debug = 0;
67 static int pxe_sock = -1;
68 static int pxe_opens = 0;
70 void pxe_enable(void *pxeinfo);
71 static void (*pxe_call)(int func);
72 static void pxenv_call(int func);
73 static void bangpxe_call(int func);
75 static int pxe_init(void);
76 static int pxe_strategy(void *devdata, int flag, daddr_t dblk,
77 size_t size, char *buf, size_t *rsize);
78 static int pxe_open(struct open_file *f, ...);
79 static int pxe_close(struct open_file *f);
80 static void pxe_print(int verbose);
81 static void pxe_cleanup(void);
82 static void pxe_setnfshandle(char *rootpath);
84 static void pxe_perror(int error);
85 static int pxe_netif_match(struct netif *nif, void *machdep_hint);
86 static int pxe_netif_probe(struct netif *nif, void *machdep_hint);
87 static void pxe_netif_init(struct iodesc *desc, void *machdep_hint);
88 static int pxe_netif_get(struct iodesc *desc, void *pkt, size_t len,
90 static int pxe_netif_put(struct iodesc *desc, void *pkt, size_t len);
91 static void pxe_netif_end(struct netif *nif);
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);
101 struct netif_dif pxe_ifs[] = {
102 /* dif_unit dif_nsel dif_stats dif_private */
103 {0, 1, &pxe_st[0], 0}
106 struct netif_stats pxe_st[NENTS(pxe_ifs)];
108 struct netif_driver pxenetif = {
120 struct netif_driver *netif_drivers[] = {
125 struct devsw pxedisk = {
138 * This function is called by the loader to enable PXE support if we
139 * are booted by PXE. The passed in pointer is a pointer to the
143 pxe_enable(void *pxeinfo)
145 pxenv_p = (pxenv_t *)pxeinfo;
146 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
147 pxenv_p->PXEPtr.offset);
152 * return true if pxe structures are found/initialized,
153 * also figures out our IP information via the pxe cached info struct
158 t_PXENV_GET_CACHED_INFO *gci_p;
166 /* look for "PXENV+" */
167 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
172 /* make sure the size is something we can handle */
173 if (pxenv_p->Length > sizeof(*pxenv_p)) {
174 printf("PXENV+ structure too large, ignoring\n");
181 * add up each byte in the structure, the total should be 0
184 checkptr = (uint8_t *) pxenv_p;
185 for (counter = 0; counter < pxenv_p->Length; counter++)
186 checksum += *checkptr++;
188 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 (rootip.s_addr == 0) {
279 * Do a bootp/dhcp request to find out where our
280 * NFS/TFTP server is. Even if we dont get back
281 * the proper information, fall back to the server
282 * which brought us to life and a default rootpath.
284 bootp(pxe_sock, BOOTP_PXE);
285 if (rootip.s_addr == 0)
286 rootip.s_addr = bootplayer.sip;
288 strcpy(rootpath, PXENFSROOTPATH);
290 for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++) {
291 if (rootpath[i] == ':')
294 if (i && i != FNAME_SIZE && rootpath[i] == ':') {
295 rootpath[i++] = '\0';
296 if (inet_addr(&rootpath[0]) != INADDR_NONE)
297 rootip.s_addr = inet_addr(&rootpath[0]);
298 bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
299 bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);
301 printf("pxe_open: ip address : %s\n", inet_ntoa(myip));
302 printf("pxe_open: ip netmask : %s\n", intoa(netmask));
303 printf("pxe_open: nfs root mount: %s:%s\n", inet_ntoa(rootip), rootpath);
304 printf("pxe_open: gateway ip: %s\n", inet_ntoa(gateip));
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);
313 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
314 setenv("boot.nfsroot.path", rootpath, 1);
318 f->f_devdata = &pxe_sock;
323 pxe_close(struct open_file *f)
328 printf("pxe_close: opens=%d\n", pxe_opens);
331 /* On last close, do netif close, etc. */
333 /* Extra close call? */
337 /* Not last close? */
341 /* get an NFS filehandle for our root filesystem */
342 pxe_setnfshandle(rootpath);
348 printf("pxe_close: calling netif_close()\n");
350 netif_close(pxe_sock);
357 pxe_print(int verbose)
359 if (pxe_call != NULL) {
360 if (*bootplayer.Sname == '\0') {
361 printf(" "IP_STR":%s\n",
362 IP_ARGS(htonl(bootplayer.sip)),
363 bootplayer.bootfile);
365 printf(" %s:%s\n", bootplayer.Sname,
366 bootplayer.bootfile);
377 t_PXENV_UNLOAD_STACK *unload_stack_p =
378 (t_PXENV_UNLOAD_STACK *)scratch_buffer;
379 t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p =
380 (t_PXENV_UNDI_SHUTDOWN *)scratch_buffer;
383 if (pxe_call == NULL)
386 pxe_call(PXENV_UNDI_SHUTDOWN);
389 if (pxe_debug && undi_shutdown_p->Status != 0)
390 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
391 undi_shutdown_p->Status);
394 pxe_call(PXENV_UNLOAD_STACK);
397 if (pxe_debug && unload_stack_p->Status != 0)
398 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
399 unload_stack_p->Status);
410 * Reach inside the libstand NFS code and dig out an NFS handle
411 * for the root filesystem. If there is no nfs handle but a NFS root
412 * path was dynamically requested (not just as a default), then try
413 * to get the handle. This occurs if we are compiled for TFTP operation
414 * but still want to pass an NFS root to the kernel.
417 struct iodesc *iodesc;
419 u_char fh[NFS_FHSIZE];
420 /* structure truncated here */
422 extern struct nfs_iodesc nfs_root_node;
425 pxe_setnfshandle(char *rootpath)
429 char buf[2 * NFS_FHSIZE + 3], *cp;
431 fh = &nfs_root_node.fh[0];
434 * If no file handle exists but a root path was dynamically
435 * requested, try to get a good handle.
437 for (i = 0; i < NFS_FHSIZE; ++i) {
441 if (i != NFS_FHSIZE) {
444 for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
445 sprintf(cp, "%02x", fh[i]);
447 setenv("boot.nfsroot.nfshandle", buf, 1);
456 printf("pxenv_call %x\n", func);
459 bzero(&v86, sizeof(v86));
460 bzero(data_buffer, sizeof(data_buffer));
462 __pxenvseg = pxenv_p->RMEntry.segment;
463 __pxenvoff = pxenv_p->RMEntry.offset;
465 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
466 v86.es = VTOPSEG(scratch_buffer);
467 v86.edi = VTOPOFF(scratch_buffer);
468 v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
475 bangpxe_call(int func)
479 printf("bangpxe_call %x\n", func);
482 bzero(&v86, sizeof(v86));
483 bzero(data_buffer, sizeof(data_buffer));
485 __bangpxeseg = pxe_p->EntryPointSP.segment;
486 __bangpxeoff = pxe_p->EntryPointSP.offset;
488 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
489 v86.edx = VTOPSEG(scratch_buffer);
490 v86.eax = VTOPOFF(scratch_buffer);
491 v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
507 pxe_netif_match(struct netif *nif, void *machdep_hint)
514 pxe_netif_probe(struct netif *nif, void *machdep_hint)
516 t_PXENV_UDP_OPEN *udpopen_p = (t_PXENV_UDP_OPEN *)scratch_buffer;
518 if (pxe_call == NULL)
521 bzero(udpopen_p, sizeof(*udpopen_p));
522 udpopen_p->src_ip = bootplayer.yip;
523 pxe_call(PXENV_UDP_OPEN);
525 if (udpopen_p->status != 0) {
526 printf("pxe_netif_probe: failed %x\n", udpopen_p->status);
533 pxe_netif_end(struct netif *nif)
535 t_PXENV_UDP_CLOSE *udpclose_p = (t_PXENV_UDP_CLOSE *)scratch_buffer;
536 bzero(udpclose_p, sizeof(*udpclose_p));
538 pxe_call(PXENV_UDP_CLOSE);
539 if (udpclose_p->status != 0)
540 printf("pxe_end failed %x\n", udpclose_p->status);
544 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
547 for (i = 0; i < 6; ++i)
548 desc->myea[i] = bootplayer.CAddr[i];
549 desc->xid = bootplayer.ident;
553 pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
559 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
565 sendudp(struct iodesc *h, void *pkt, size_t len)
567 t_PXENV_UDP_WRITE *udpwrite_p = (t_PXENV_UDP_WRITE *)scratch_buffer;
568 bzero(udpwrite_p, sizeof(*udpwrite_p));
570 udpwrite_p->ip = h->destip.s_addr;
571 udpwrite_p->dst_port = h->destport;
572 udpwrite_p->src_port = h->myport;
573 udpwrite_p->buffer_size = len;
574 udpwrite_p->buffer.segment = VTOPSEG(pkt);
575 udpwrite_p->buffer.offset = VTOPOFF(pkt);
577 if (netmask == 0 || SAMENET(myip, h->destip, netmask))
580 udpwrite_p->gw = gateip.s_addr;
582 pxe_call(PXENV_UDP_WRITE);
585 /* XXX - I dont know why we need this. */
588 if (udpwrite_p->status != 0) {
589 /* XXX: This happens a lot. It shouldn't. */
590 if (udpwrite_p->status != 1)
591 printf("sendudp failed %x\n", udpwrite_p->status);
598 readudp(struct iodesc *h, void *pkt, size_t len, time_t timeout)
600 t_PXENV_UDP_READ *udpread_p = (t_PXENV_UDP_READ *)scratch_buffer;
604 uh = (struct udphdr *) pkt - 1;
605 ip = (struct ip *)uh - 1;
606 bzero(udpread_p, sizeof(*udpread_p));
608 udpread_p->dest_ip = h->myip.s_addr;
609 udpread_p->d_port = h->myport;
610 udpread_p->buffer_size = len;
611 udpread_p->buffer.segment = VTOPSEG(data_buffer);
612 udpread_p->buffer.offset = VTOPOFF(data_buffer);
614 pxe_call(PXENV_UDP_READ);
617 /* XXX - I dont know why we need this. */
620 if (udpread_p->status != 0) {
621 /* XXX: This happens a lot. It shouldn't. */
622 if (udpread_p->status != 1)
623 printf("readudp failed %x\n", udpread_p->status);
626 bcopy(data_buffer, pkt, udpread_p->buffer_size);
627 uh->uh_sport = udpread_p->s_port;
628 ip->ip_src.s_addr = udpread_p->src_ip;
629 return udpread_p->buffer_size;