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 <sys/param.h>
38 #include <net/ethernet.h>
39 #include <netinet/in_systm.h>
40 #include <netinet/in.h>
41 #include <netinet/ip.h>
42 #include <netinet/udp.h>
50 #include <bootstrap.h>
55 * Allocate the PXE buffers statically instead of sticking grimy fingers into
56 * BTX's private data area. The scratch buffer is used to send information to
57 * the PXE BIOS, and the data buffer is used to receive data from the PXE BIOS.
59 #define PXE_BUFFER_SIZE 0x2000
60 static char scratch_buffer[PXE_BUFFER_SIZE];
61 static char data_buffer[PXE_BUFFER_SIZE];
63 static pxenv_t *pxenv_p = NULL; /* PXENV+ */
64 static pxe_t *pxe_p = NULL; /* !PXE */
67 static int pxe_debug = 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_print(int verbose);
77 static void pxe_cleanup(void);
79 static void pxe_perror(int error);
80 static int pxe_netif_match(struct netif *nif, void *machdep_hint);
81 static int pxe_netif_probe(struct netif *nif, void *machdep_hint);
82 static void pxe_netif_init(struct iodesc *desc, void *machdep_hint);
83 static ssize_t pxe_netif_get(struct iodesc *, void **, time_t);
84 static ssize_t pxe_netif_put(struct iodesc *desc, void *pkt, size_t len);
85 static void pxe_netif_end(struct netif *nif);
87 extern struct netif_stats pxe_st[];
88 extern uint16_t __bangpxeseg;
89 extern uint16_t __bangpxeoff;
90 extern void __bangpxeentry(void);
91 extern uint16_t __pxenvseg;
92 extern uint16_t __pxenvoff;
93 extern void __pxenventry(void);
95 struct netif_dif pxe_ifs[] = {
96 /* dif_unit dif_nsel dif_stats dif_private */
100 struct netif_stats pxe_st[nitems(pxe_ifs)];
102 struct netif_driver pxenetif = {
103 .netif_bname = "pxenet",
104 .netif_match = pxe_netif_match,
105 .netif_probe = pxe_netif_probe,
106 .netif_init = pxe_netif_init,
107 .netif_get = pxe_netif_get,
108 .netif_put = pxe_netif_put,
109 .netif_end = pxe_netif_end,
110 .netif_ifs = pxe_ifs,
111 .netif_nifs = nitems(pxe_ifs)
114 struct netif_driver *netif_drivers[] = {
119 struct devsw pxedisk = {
123 .dv_strategy = NULL, /* Will be set in pxe_init */
124 .dv_open = NULL, /* Will be set in pxe_init */
125 .dv_close = NULL, /* Will be set in pxe_init */
127 .dv_print = pxe_print,
128 .dv_cleanup = pxe_cleanup
132 * This function is called by the loader to enable PXE support if we
133 * are booted by PXE. The passed in pointer is a pointer to the PXENV+
137 pxe_enable(void *pxeinfo)
139 pxenv_p = (pxenv_t *)pxeinfo;
140 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
141 pxenv_p->PXEPtr.offset);
146 * return true if pxe structures are found/initialized,
147 * also figures out our IP information via the pxe cached info struct
152 t_PXENV_GET_CACHED_INFO *gci_p;
156 extern struct devsw netdev;
161 /* look for "PXENV+" */
162 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
167 /* make sure the size is something we can handle */
168 if (pxenv_p->Length > sizeof(*pxenv_p)) {
169 printf("PXENV+ structure too large, ignoring\n");
176 * add up each byte in the structure, the total should be 0
179 checkptr = (uint8_t *) pxenv_p;
180 for (counter = 0; counter < pxenv_p->Length; counter++)
181 checksum += *checkptr++;
183 printf("PXENV+ structure failed checksum, ignoring\n");
189 * PXENV+ passed, so use that if !PXE is not available or
190 * the checksum fails.
192 pxe_call = pxenv_call;
193 if (pxenv_p->Version >= 0x0200) {
195 if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
200 checkptr = (uint8_t *)pxe_p;
201 for (counter = 0; counter < pxe_p->StructLength;
203 checksum += *checkptr++;
208 pxe_call = bangpxe_call;
213 pxedisk.dv_open = netdev.dv_open;
214 pxedisk.dv_close = netdev.dv_close;
215 pxedisk.dv_strategy = netdev.dv_strategy;
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 free(bootp_response);
238 if ((bootp_response = malloc(gci_p->BufferSize)) != NULL) {
239 bootp_response_size = gci_p->BufferSize;
240 bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
241 bootp_response, bootp_response_size);
247 pxe_print(int verbose)
249 if (pxe_call == NULL)
252 printf("%s devices:", pxedisk.dv_name);
253 if (pager_output("\n") != 0)
255 printf(" %s0:", pxedisk.dv_name);
257 printf(" %s:%s", inet_ntoa(rootip), rootpath);
259 return (pager_output("\n"));
266 t_PXENV_UNLOAD_STACK *unload_stack_p =
267 (t_PXENV_UNLOAD_STACK *)scratch_buffer;
268 t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p =
269 (t_PXENV_UNDI_SHUTDOWN *)scratch_buffer;
272 if (pxe_call == NULL)
275 pxe_call(PXENV_UNDI_SHUTDOWN);
278 if (pxe_debug && undi_shutdown_p->Status != 0)
279 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
280 undi_shutdown_p->Status);
283 pxe_call(PXENV_UNLOAD_STACK);
286 if (pxe_debug && unload_stack_p->Status != 0)
287 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
288 unload_stack_p->Status);
303 printf("pxenv_call %x\n", func);
306 bzero(&v86, sizeof(v86));
307 bzero(data_buffer, sizeof(data_buffer));
309 __pxenvseg = pxenv_p->RMEntry.segment;
310 __pxenvoff = pxenv_p->RMEntry.offset;
312 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
313 v86.es = VTOPSEG(scratch_buffer);
314 v86.edi = VTOPOFF(scratch_buffer);
315 v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
322 bangpxe_call(int func)
326 printf("bangpxe_call %x\n", func);
329 bzero(&v86, sizeof(v86));
330 bzero(data_buffer, sizeof(data_buffer));
332 __bangpxeseg = pxe_p->EntryPointSP.segment;
333 __bangpxeoff = pxe_p->EntryPointSP.offset;
335 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
336 v86.edx = VTOPSEG(scratch_buffer);
337 v86.eax = VTOPOFF(scratch_buffer);
338 v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
346 pxe_netif_match(struct netif *nif, void *machdep_hint)
352 pxe_netif_probe(struct netif *nif, void *machdep_hint)
354 if (pxe_call == NULL)
361 pxe_netif_end(struct netif *nif)
363 t_PXENV_UNDI_CLOSE *undi_close_p;
365 undi_close_p = (t_PXENV_UNDI_CLOSE *)scratch_buffer;
366 bzero(undi_close_p, sizeof(*undi_close_p));
367 pxe_call(PXENV_UNDI_CLOSE);
368 if (undi_close_p->Status != 0)
369 printf("undi close failed: %x\n", undi_close_p->Status);
373 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
375 t_PXENV_UNDI_GET_INFORMATION *undi_info_p;
376 t_PXENV_UNDI_OPEN *undi_open_p;
380 undi_info_p = (t_PXENV_UNDI_GET_INFORMATION *)scratch_buffer;
381 bzero(undi_info_p, sizeof(*undi_info_p));
382 pxe_call(PXENV_UNDI_GET_INFORMATION);
383 if (undi_info_p->Status != 0) {
384 printf("undi get info failed: %x\n", undi_info_p->Status);
388 /* Make sure the CurrentNodeAddress is valid. */
389 for (i = 0; i < undi_info_p->HwAddrLen; ++i) {
390 if (undi_info_p->CurrentNodeAddress[i] != 0)
393 if (i < undi_info_p->HwAddrLen) {
394 for (i = 0; i < undi_info_p->HwAddrLen; ++i) {
395 if (undi_info_p->CurrentNodeAddress[i] != 0xff)
399 if (i < undi_info_p->HwAddrLen)
400 mac = undi_info_p->CurrentNodeAddress;
402 mac = undi_info_p->PermNodeAddress;
404 len = min(sizeof (desc->myea), undi_info_p->HwAddrLen);
405 for (i = 0; i < len; ++i)
406 desc->myea[i] = mac[i];
408 if (bootp_response != NULL)
409 desc->xid = bootp_response->bp_xid;
413 undi_open_p = (t_PXENV_UNDI_OPEN *)scratch_buffer;
414 bzero(undi_open_p, sizeof(*undi_open_p));
415 undi_open_p->PktFilter = FLTR_DIRECTED | FLTR_BRDCST;
416 pxe_call(PXENV_UNDI_OPEN);
417 if (undi_open_p->Status != 0)
418 printf("undi open failed: %x\n", undi_open_p->Status);
422 pxe_netif_receive(void **pkt)
424 t_PXENV_UNDI_ISR *isr = (t_PXENV_UNDI_ISR *)scratch_buffer;
425 char *buf, *ptr, *frame;
428 bzero(isr, sizeof(*isr));
429 isr->FuncFlag = PXENV_UNDI_ISR_IN_START;
430 pxe_call(PXENV_UNDI_ISR);
431 if (isr->Status != 0)
434 bzero(isr, sizeof(*isr));
435 isr->FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
436 pxe_call(PXENV_UNDI_ISR);
437 if (isr->Status != 0)
440 while (isr->FuncFlag == PXENV_UNDI_ISR_OUT_TRANSMIT) {
442 * Wait till transmit is done.
444 bzero(isr, sizeof(*isr));
445 isr->FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
446 pxe_call(PXENV_UNDI_ISR);
447 if (isr->Status != 0 ||
448 isr->FuncFlag == PXENV_UNDI_ISR_OUT_DONE)
452 while (isr->FuncFlag != PXENV_UNDI_ISR_OUT_RECEIVE) {
453 if (isr->Status != 0 ||
454 isr->FuncFlag == PXENV_UNDI_ISR_OUT_DONE) {
457 bzero(isr, sizeof(*isr));
458 isr->FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
459 pxe_call(PXENV_UNDI_ISR);
462 size = isr->FrameLength;
463 buf = malloc(size + ETHER_ALIGN);
466 ptr = buf + ETHER_ALIGN;
469 while (rsize < size) {
470 frame = (char *)((uintptr_t)isr->Frame.segment << 4);
471 frame += isr->Frame.offset;
472 bcopy(PTOV(frame), ptr, isr->BufferLength);
473 ptr += isr->BufferLength;
474 rsize += isr->BufferLength;
476 bzero(isr, sizeof(*isr));
477 isr->FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
478 pxe_call(PXENV_UNDI_ISR);
479 if (isr->Status != 0) {
484 /* Did we got another update? */
485 if (isr->FuncFlag == PXENV_UNDI_ISR_OUT_RECEIVE)
495 pxe_netif_get(struct iodesc *desc, void **pkt, time_t timeout)
502 while ((getsecs() - t) < timeout) {
503 ret = pxe_netif_receive(&ptr);
513 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
515 t_PXENV_UNDI_TRANSMIT *trans_p;
516 t_PXENV_UNDI_TBD *tbd_p;
519 trans_p = (t_PXENV_UNDI_TRANSMIT *)scratch_buffer;
520 bzero(trans_p, sizeof(*trans_p));
521 tbd_p = (t_PXENV_UNDI_TBD *)(scratch_buffer + sizeof(*trans_p));
522 bzero(tbd_p, sizeof(*tbd_p));
524 data = scratch_buffer + sizeof(*trans_p) + sizeof(*tbd_p);
526 trans_p->TBD.segment = VTOPSEG(tbd_p);
527 trans_p->TBD.offset = VTOPOFF(tbd_p);
529 tbd_p->ImmedLength = len;
530 tbd_p->Xmit.segment = VTOPSEG(data);
531 tbd_p->Xmit.offset = VTOPOFF(data);
532 bcopy(pkt, data, len);
534 pxe_call(PXENV_UNDI_TRANSMIT);
535 if (trans_p->Status != 0) {