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.4 2003/11/10 06:08:36 dillon Exp $
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);
91 extern struct netif_stats pxe_st[];
92 extern u_int16_t __bangpxeseg;
93 extern u_int16_t __bangpxeoff;
94 extern void __bangpxeentry(void);
95 extern u_int16_t __pxenvseg;
96 extern u_int16_t __pxenvoff;
97 extern void __pxenventry(void);
99 struct netif_dif pxe_ifs[] = {
100 /* dif_unit dif_nsel dif_stats dif_private */
101 {0, 1, &pxe_st[0], 0}
104 struct netif_stats pxe_st[NENTS(pxe_ifs)];
106 struct netif_driver pxenetif = {
118 struct netif_driver *netif_drivers[] = {
123 struct devsw pxedisk = {
136 * This function is called by the loader to enable PXE support if we
137 * are booted by PXE. The passed in pointer is a pointer to the
141 pxe_enable(void *pxeinfo)
143 pxenv_p = (pxenv_t *)pxeinfo;
144 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
145 pxenv_p->PXEPtr.offset);
150 * return true if pxe structures are found/initialized,
151 * also figures out our IP information via the pxe cached info struct
156 t_PXENV_GET_CACHED_INFO *gci_p;
164 /* look for "PXENV+" */
165 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
170 /* make sure the size is something we can handle */
171 if (pxenv_p->Length > sizeof(*pxenv_p)) {
172 printf("PXENV+ structure too large, ignoring\n");
179 * add up each byte in the structure, the total should be 0
182 checkptr = (uint8_t *) pxenv_p;
183 for (counter = 0; counter < pxenv_p->Length; counter++)
184 checksum += *checkptr++;
186 printf("PXENV+ structure failed checksum, ignoring\n");
193 * PXENV+ passed, so use that if !PXE is not available or
194 * the checksum fails.
196 pxe_call = pxenv_call;
197 if (pxenv_p->Version >= 0x0200) {
199 if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
204 checkptr = (uint8_t *)pxe_p;
205 for (counter = 0; counter < pxe_p->StructLength;
207 checksum += *checkptr++;
212 pxe_call = bangpxe_call;
217 printf("\nPXE version %d.%d, real mode entry point ",
218 (uint8_t) (pxenv_p->Version >> 8),
219 (uint8_t) (pxenv_p->Version & 0xFF));
220 if (pxe_call == bangpxe_call)
221 printf("@%04x:%04x\n",
222 pxe_p->EntryPointSP.segment,
223 pxe_p->EntryPointSP.offset);
225 printf("@%04x:%04x\n",
226 pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
228 gci_p = (t_PXENV_GET_CACHED_INFO *) scratch_buffer;
229 bzero(gci_p, sizeof(*gci_p));
230 gci_p->PacketType = PXENV_PACKET_TYPE_BINL_REPLY;
231 pxe_call(PXENV_GET_CACHED_INFO);
232 if (gci_p->Status != 0) {
233 pxe_perror(gci_p->Status);
237 bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
238 &bootplayer, gci_p->BufferSize);
244 pxe_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
245 char *buf, size_t *rsize)
251 pxe_open(struct open_file *f, ...)
254 char *devname; /* Device part of file name (or NULL). */
255 char temp[FNAME_SIZE];
260 devname = va_arg(args, char*);
263 /* On first open, do netif open, mount, etc. */
264 if (pxe_opens == 0) {
265 /* Find network interface. */
267 pxe_sock = netif_open(devname);
269 printf("pxe_open: netif_open() failed\n");
273 printf("pxe_open: netif_open() succeeded\n");
275 if (rootip.s_addr == 0) {
277 * Do a bootp/dhcp request to find out where our
278 * NFS/TFTP server is. Even if we dont get back
279 * the proper information, fall back to the server
280 * which brought us to life and a default rootpath.
282 bootp(pxe_sock, BOOTP_PXE);
283 if (rootip.s_addr == 0)
284 rootip.s_addr = bootplayer.sip;
286 strcpy(rootpath, PXENFSROOTPATH);
288 for (i = 0; rootpath[i] != '\0' && i < FNAME_SIZE; i++)
289 if (rootpath[i] == ':')
291 if (i && i != FNAME_SIZE && rootpath[i] == ':') {
292 rootpath[i++] = '\0';
293 if (inet_addr(&rootpath[0]) != INADDR_NONE)
294 rootip.s_addr = inet_addr(&rootpath[0]);
295 bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
296 bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);
298 printf("pxe_open: server addr: %s\n", inet_ntoa(rootip));
299 printf("pxe_open: server path: %s\n", rootpath);
300 printf("pxe_open: gateway ip: %s\n", inet_ntoa(gateip));
302 setenv("boot.netif.ip", inet_ntoa(myip), 1);
303 setenv("boot.netif.netmask", intoa(netmask), 1);
304 setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
305 if (bootplayer.Hardware == ETHER_TYPE) {
306 sprintf(temp, "%6D", bootplayer.CAddr, ":");
307 setenv("boot.netif.hwaddr", temp, 1);
309 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
310 setenv("boot.nfsroot.path", rootpath, 1);
314 f->f_devdata = &pxe_sock;
319 pxe_close(struct open_file *f)
324 printf("pxe_close: opens=%d\n", pxe_opens);
327 /* On last close, do netif close, etc. */
329 /* Extra close call? */
333 /* Not last close? */
337 /* get an NFS filehandle for our root filesystem */
338 pxe_setnfshandle(rootpath);
344 printf("pxe_close: calling netif_close()\n");
346 netif_close(pxe_sock);
353 pxe_print(int verbose)
355 if (pxe_call != NULL) {
356 if (*bootplayer.Sname == '\0') {
357 printf(" "IP_STR":%s\n",
358 IP_ARGS(htonl(bootplayer.sip)),
359 bootplayer.bootfile);
361 printf(" %s:%s\n", bootplayer.Sname,
362 bootplayer.bootfile);
373 t_PXENV_UNLOAD_STACK *unload_stack_p =
374 (t_PXENV_UNLOAD_STACK *)scratch_buffer;
375 t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p =
376 (t_PXENV_UNDI_SHUTDOWN *)scratch_buffer;
379 if (pxe_call == NULL)
382 pxe_call(PXENV_UNDI_SHUTDOWN);
385 if (pxe_debug && undi_shutdown_p->Status != 0)
386 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
387 undi_shutdown_p->Status);
390 pxe_call(PXENV_UNLOAD_STACK);
393 if (pxe_debug && unload_stack_p->Status != 0)
394 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
395 unload_stack_p->Status);
406 * Reach inside the libstand NFS code and dig out an NFS handle
407 * for the root filesystem.
410 struct iodesc *iodesc;
412 u_char fh[NFS_FHSIZE];
413 /* structure truncated here */
415 extern struct nfs_iodesc nfs_root_node;
418 pxe_setnfshandle(char *rootpath)
422 char buf[2 * NFS_FHSIZE + 3], *cp;
424 fh = &nfs_root_node.fh[0];
427 for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
428 sprintf(cp, "%02x", fh[i]);
430 setenv("boot.nfsroot.nfshandle", buf, 1);
438 printf("pxenv_call %x\n", func);
441 bzero(&v86, sizeof(v86));
442 bzero(data_buffer, sizeof(data_buffer));
444 __pxenvseg = pxenv_p->RMEntry.segment;
445 __pxenvoff = pxenv_p->RMEntry.offset;
447 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
448 v86.es = VTOPSEG(scratch_buffer);
449 v86.edi = VTOPOFF(scratch_buffer);
450 v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
457 bangpxe_call(int func)
461 printf("bangpxe_call %x\n", func);
464 bzero(&v86, sizeof(v86));
465 bzero(data_buffer, sizeof(data_buffer));
467 __bangpxeseg = pxe_p->EntryPointSP.segment;
468 __bangpxeoff = pxe_p->EntryPointSP.offset;
470 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
471 v86.edx = VTOPSEG(scratch_buffer);
472 v86.eax = VTOPOFF(scratch_buffer);
473 v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
489 pxe_netif_match(struct netif *nif, void *machdep_hint)
496 pxe_netif_probe(struct netif *nif, void *machdep_hint)
498 t_PXENV_UDP_OPEN *udpopen_p = (t_PXENV_UDP_OPEN *)scratch_buffer;
500 if (pxe_call == NULL)
503 bzero(udpopen_p, sizeof(*udpopen_p));
504 udpopen_p->src_ip = bootplayer.yip;
505 pxe_call(PXENV_UDP_OPEN);
507 if (udpopen_p->status != 0) {
508 printf("pxe_netif_probe: failed %x\n", udpopen_p->status);
515 pxe_netif_end(struct netif *nif)
517 t_PXENV_UDP_CLOSE *udpclose_p = (t_PXENV_UDP_CLOSE *)scratch_buffer;
518 bzero(udpclose_p, sizeof(*udpclose_p));
520 pxe_call(PXENV_UDP_CLOSE);
521 if (udpclose_p->status != 0)
522 printf("pxe_end failed %x\n", udpclose_p->status);
526 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
529 for (i = 0; i < 6; ++i)
530 desc->myea[i] = bootplayer.CAddr[i];
531 desc->xid = bootplayer.ident;
535 pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
541 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
547 sendudp(struct iodesc *h, void *pkt, size_t len)
549 t_PXENV_UDP_WRITE *udpwrite_p = (t_PXENV_UDP_WRITE *)scratch_buffer;
550 bzero(udpwrite_p, sizeof(*udpwrite_p));
552 udpwrite_p->ip = h->destip.s_addr;
553 udpwrite_p->dst_port = h->destport;
554 udpwrite_p->src_port = h->myport;
555 udpwrite_p->buffer_size = len;
556 udpwrite_p->buffer.segment = VTOPSEG(pkt);
557 udpwrite_p->buffer.offset = VTOPOFF(pkt);
559 if (netmask == 0 || SAMENET(myip, h->destip, netmask))
562 udpwrite_p->gw = gateip.s_addr;
564 pxe_call(PXENV_UDP_WRITE);
567 /* XXX - I dont know why we need this. */
570 if (udpwrite_p->status != 0) {
571 /* XXX: This happens a lot. It shouldn't. */
572 if (udpwrite_p->status != 1)
573 printf("sendudp failed %x\n", udpwrite_p->status);
580 readudp(struct iodesc *h, void *pkt, size_t len, time_t timeout)
582 t_PXENV_UDP_READ *udpread_p = (t_PXENV_UDP_READ *)scratch_buffer;
583 struct udphdr *uh = NULL;
585 uh = (struct udphdr *) pkt - 1;
586 bzero(udpread_p, sizeof(*udpread_p));
588 udpread_p->dest_ip = h->myip.s_addr;
589 udpread_p->d_port = h->myport;
590 udpread_p->buffer_size = len;
591 udpread_p->buffer.segment = VTOPSEG(data_buffer);
592 udpread_p->buffer.offset = VTOPOFF(data_buffer);
594 pxe_call(PXENV_UDP_READ);
597 /* XXX - I dont know why we need this. */
600 if (udpread_p->status != 0) {
601 /* XXX: This happens a lot. It shouldn't. */
602 if (udpread_p->status != 1)
603 printf("readudp failed %x\n", udpread_p->status);
606 bcopy(data_buffer, pkt, udpread_p->buffer_size);
607 uh->uh_sport = udpread_p->s_port;
608 return udpread_p->buffer_size;