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 static pxenv_t *pxenv_p = NULL; /* PXENV+ */
56 static pxe_t *pxe_p = NULL; /* !PXE */
59 static int pxe_debug = 0;
62 void pxe_enable(void *pxeinfo);
63 static void (*pxe_call)(int func, void *ptr);
64 static void pxenv_call(int func, void *ptr);
65 static void bangpxe_call(int func, void *ptr);
67 static int pxe_init(void);
68 static int pxe_print(int verbose);
69 static void pxe_cleanup(void);
71 static void pxe_perror(int error);
72 static int pxe_netif_match(struct netif *nif, void *machdep_hint);
73 static int pxe_netif_probe(struct netif *nif, void *machdep_hint);
74 static void pxe_netif_init(struct iodesc *desc, void *machdep_hint);
75 static ssize_t pxe_netif_get(struct iodesc *, void **, time_t);
76 static ssize_t pxe_netif_put(struct iodesc *desc, void *pkt, size_t len);
77 static void pxe_netif_end(struct netif *nif);
79 extern struct netif_stats pxe_st[];
80 extern uint16_t __bangpxeseg;
81 extern uint16_t __bangpxeoff;
82 extern void __bangpxeentry(void);
83 extern uint16_t __pxenvseg;
84 extern uint16_t __pxenvoff;
85 extern void __pxenventry(void);
87 struct netif_dif pxe_ifs[] = {
88 /* dif_unit dif_nsel dif_stats dif_private */
92 struct netif_stats pxe_st[nitems(pxe_ifs)];
94 struct netif_driver pxenetif = {
95 .netif_bname = "pxenet",
96 .netif_match = pxe_netif_match,
97 .netif_probe = pxe_netif_probe,
98 .netif_init = pxe_netif_init,
99 .netif_get = pxe_netif_get,
100 .netif_put = pxe_netif_put,
101 .netif_end = pxe_netif_end,
102 .netif_ifs = pxe_ifs,
103 .netif_nifs = nitems(pxe_ifs)
106 struct netif_driver *netif_drivers[] = {
111 struct devsw pxedisk = {
115 .dv_strategy = NULL, /* Will be set in pxe_init */
116 .dv_open = NULL, /* Will be set in pxe_init */
117 .dv_close = NULL, /* Will be set in pxe_init */
119 .dv_print = pxe_print,
120 .dv_cleanup = pxe_cleanup
124 * This function is called by the loader to enable PXE support if we
125 * are booted by PXE. The passed in pointer is a pointer to the PXENV+
129 pxe_enable(void *pxeinfo)
131 pxenv_p = (pxenv_t *)pxeinfo;
132 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
133 pxenv_p->PXEPtr.offset);
138 * return true if pxe structures are found/initialized,
139 * also figures out our IP information via the pxe cached info struct
144 t_PXENV_GET_CACHED_INFO *gci_p;
148 extern struct devsw netdev;
153 /* look for "PXENV+" */
154 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
159 /* make sure the size is something we can handle */
160 if (pxenv_p->Length > sizeof(*pxenv_p)) {
161 printf("PXENV+ structure too large, ignoring\n");
168 * add up each byte in the structure, the total should be 0
171 checkptr = (uint8_t *) pxenv_p;
172 for (counter = 0; counter < pxenv_p->Length; counter++)
173 checksum += *checkptr++;
175 printf("PXENV+ structure failed checksum, ignoring\n");
181 * PXENV+ passed, so use that if !PXE is not available or
182 * the checksum fails.
184 pxe_call = pxenv_call;
185 if (pxenv_p->Version >= 0x0200) {
187 if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
192 checkptr = (uint8_t *)pxe_p;
193 for (counter = 0; counter < pxe_p->StructLength;
195 checksum += *checkptr++;
200 pxe_call = bangpxe_call;
205 pxedisk.dv_open = netdev.dv_open;
206 pxedisk.dv_close = netdev.dv_close;
207 pxedisk.dv_strategy = netdev.dv_strategy;
209 printf("\nPXE version %d.%d, real mode entry point ",
210 (uint8_t) (pxenv_p->Version >> 8),
211 (uint8_t) (pxenv_p->Version & 0xFF));
212 if (pxe_call == bangpxe_call)
213 printf("@%04x:%04x\n",
214 pxe_p->EntryPointSP.segment,
215 pxe_p->EntryPointSP.offset);
217 printf("@%04x:%04x\n",
218 pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
220 gci_p = bio_alloc(sizeof(*gci_p));
225 bzero(gci_p, sizeof(*gci_p));
226 gci_p->PacketType = PXENV_PACKET_TYPE_BINL_REPLY;
227 pxe_call(PXENV_GET_CACHED_INFO, gci_p);
228 if (gci_p->Status != 0) {
229 pxe_perror(gci_p->Status);
230 bio_free(gci_p, sizeof(*gci_p));
234 free(bootp_response);
235 if ((bootp_response = malloc(gci_p->BufferSize)) != NULL) {
236 bootp_response_size = gci_p->BufferSize;
237 bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
238 bootp_response, bootp_response_size);
240 bio_free(gci_p, sizeof(*gci_p));
245 pxe_print(int verbose)
247 if (pxe_call == NULL)
250 printf("%s devices:", pxedisk.dv_name);
251 if (pager_output("\n") != 0)
253 printf(" %s0:", pxedisk.dv_name);
255 printf(" %s:%s", inet_ntoa(rootip), rootpath);
257 return (pager_output("\n"));
263 t_PXENV_UNLOAD_STACK *unload_stack_p;
264 t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p;
266 if (pxe_call == NULL)
269 undi_shutdown_p = bio_alloc(sizeof(*undi_shutdown_p));
270 if (undi_shutdown_p != NULL) {
271 bzero(undi_shutdown_p, sizeof(*undi_shutdown_p));
272 pxe_call(PXENV_UNDI_SHUTDOWN, undi_shutdown_p);
275 if (pxe_debug && undi_shutdown_p->Status != 0)
276 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
277 undi_shutdown_p->Status);
279 bio_free(undi_shutdown_p, sizeof(*undi_shutdown_p));
282 unload_stack_p = bio_alloc(sizeof(*unload_stack_p));
283 if (unload_stack_p != NULL) {
284 bzero(unload_stack_p, sizeof(*unload_stack_p));
285 pxe_call(PXENV_UNLOAD_STACK, unload_stack_p);
288 if (pxe_debug && unload_stack_p->Status != 0)
289 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
290 unload_stack_p->Status);
292 bio_free(unload_stack_p, sizeof(*unload_stack_p));
303 pxenv_call(int func, void *ptr)
307 printf("pxenv_call %x\n", func);
310 bzero(&v86, sizeof(v86));
312 __pxenvseg = pxenv_p->RMEntry.segment;
313 __pxenvoff = pxenv_p->RMEntry.offset;
315 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
316 v86.es = VTOPSEG(ptr);
317 v86.edi = VTOPOFF(ptr);
318 v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
325 bangpxe_call(int func, void *ptr)
329 printf("bangpxe_call %x\n", func);
332 bzero(&v86, sizeof(v86));
334 __bangpxeseg = pxe_p->EntryPointSP.segment;
335 __bangpxeoff = pxe_p->EntryPointSP.offset;
337 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
338 v86.edx = VTOPSEG(ptr);
339 v86.eax = VTOPOFF(ptr);
340 v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
348 pxe_netif_match(struct netif *nif, void *machdep_hint)
354 pxe_netif_probe(struct netif *nif, void *machdep_hint)
356 if (pxe_call == NULL)
363 pxe_netif_end(struct netif *nif)
365 t_PXENV_UNDI_CLOSE *undi_close_p;
367 undi_close_p = bio_alloc(sizeof(*undi_close_p));
368 if (undi_close_p != NULL) {
369 bzero(undi_close_p, sizeof(*undi_close_p));
370 pxe_call(PXENV_UNDI_CLOSE, undi_close_p);
371 if (undi_close_p->Status != 0)
372 printf("undi close failed: %x\n", undi_close_p->Status);
373 bio_free(undi_close_p, sizeof(*undi_close_p));
378 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
380 t_PXENV_UNDI_GET_INFORMATION *undi_info_p;
381 t_PXENV_UNDI_OPEN *undi_open_p;
385 undi_info_p = bio_alloc(sizeof(*undi_info_p));
386 if (undi_info_p == NULL)
389 bzero(undi_info_p, sizeof(*undi_info_p));
390 pxe_call(PXENV_UNDI_GET_INFORMATION, undi_info_p);
391 if (undi_info_p->Status != 0) {
392 printf("undi get info failed: %x\n", undi_info_p->Status);
393 bio_free(undi_info_p, sizeof(*undi_info_p));
397 /* Make sure the CurrentNodeAddress is valid. */
398 for (i = 0; i < undi_info_p->HwAddrLen; ++i) {
399 if (undi_info_p->CurrentNodeAddress[i] != 0)
402 if (i < undi_info_p->HwAddrLen) {
403 for (i = 0; i < undi_info_p->HwAddrLen; ++i) {
404 if (undi_info_p->CurrentNodeAddress[i] != 0xff)
408 if (i < undi_info_p->HwAddrLen)
409 mac = undi_info_p->CurrentNodeAddress;
411 mac = undi_info_p->PermNodeAddress;
413 len = min(sizeof (desc->myea), undi_info_p->HwAddrLen);
414 for (i = 0; i < len; ++i)
415 desc->myea[i] = mac[i];
417 if (bootp_response != NULL)
418 desc->xid = bootp_response->bp_xid;
422 bio_free(undi_info_p, sizeof(*undi_info_p));
423 undi_open_p = bio_alloc(sizeof(*undi_open_p));
424 if (undi_open_p == NULL)
426 bzero(undi_open_p, sizeof(*undi_open_p));
427 undi_open_p->PktFilter = FLTR_DIRECTED | FLTR_BRDCST;
428 pxe_call(PXENV_UNDI_OPEN, undi_open_p);
429 if (undi_open_p->Status != 0)
430 printf("undi open failed: %x\n", undi_open_p->Status);
431 bio_free(undi_open_p, sizeof(*undi_open_p));
435 pxe_netif_receive(void **pkt)
437 t_PXENV_UNDI_ISR *isr;
438 char *buf, *ptr, *frame;
441 isr = bio_alloc(sizeof(*isr));
445 bzero(isr, sizeof(*isr));
446 isr->FuncFlag = PXENV_UNDI_ISR_IN_START;
447 pxe_call(PXENV_UNDI_ISR, isr);
448 if (isr->Status != 0) {
449 bio_free(isr, sizeof(*isr));
453 bzero(isr, sizeof(*isr));
454 isr->FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
455 pxe_call(PXENV_UNDI_ISR, isr);
456 if (isr->Status != 0) {
457 bio_free(isr, sizeof(*isr));
461 while (isr->FuncFlag == PXENV_UNDI_ISR_OUT_TRANSMIT) {
463 * Wait till transmit is done.
465 bzero(isr, sizeof(*isr));
466 isr->FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
467 pxe_call(PXENV_UNDI_ISR, isr);
468 if (isr->Status != 0 ||
469 isr->FuncFlag == PXENV_UNDI_ISR_OUT_DONE) {
470 bio_free(isr, sizeof(*isr));
475 while (isr->FuncFlag != PXENV_UNDI_ISR_OUT_RECEIVE) {
476 if (isr->Status != 0 ||
477 isr->FuncFlag == PXENV_UNDI_ISR_OUT_DONE) {
478 bio_free(isr, sizeof(*isr));
481 bzero(isr, sizeof(*isr));
482 isr->FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
483 pxe_call(PXENV_UNDI_ISR, isr);
486 size = isr->FrameLength;
487 buf = malloc(size + ETHER_ALIGN);
489 bio_free(isr, sizeof(*isr));
492 ptr = buf + ETHER_ALIGN;
495 while (rsize < size) {
496 frame = (char *)((uintptr_t)isr->Frame.segment << 4);
497 frame += isr->Frame.offset;
498 bcopy(PTOV(frame), ptr, isr->BufferLength);
499 ptr += isr->BufferLength;
500 rsize += isr->BufferLength;
502 bzero(isr, sizeof(*isr));
503 isr->FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
504 pxe_call(PXENV_UNDI_ISR, isr);
505 if (isr->Status != 0) {
506 bio_free(isr, sizeof(*isr));
511 /* Did we got another update? */
512 if (isr->FuncFlag == PXENV_UNDI_ISR_OUT_RECEIVE)
518 bio_free(isr, sizeof(*isr));
523 pxe_netif_get(struct iodesc *desc, void **pkt, time_t timeout)
530 while ((getsecs() - t) < timeout) {
531 ret = pxe_netif_receive(&ptr);
541 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
543 t_PXENV_UNDI_TRANSMIT *trans_p;
544 t_PXENV_UNDI_TBD *tbd_p;
548 trans_p = bio_alloc(sizeof(*trans_p));
549 tbd_p = bio_alloc(sizeof(*tbd_p));
550 data = bio_alloc(len);
552 if (trans_p != NULL && tbd_p != NULL && data != NULL) {
553 bzero(trans_p, sizeof(*trans_p));
554 bzero(tbd_p, sizeof(*tbd_p));
556 trans_p->TBD.segment = VTOPSEG(tbd_p);
557 trans_p->TBD.offset = VTOPOFF(tbd_p);
559 tbd_p->ImmedLength = len;
560 tbd_p->Xmit.segment = VTOPSEG(data);
561 tbd_p->Xmit.offset = VTOPOFF(data);
562 bcopy(pkt, data, len);
564 pxe_call(PXENV_UNDI_TRANSMIT, trans_p);
565 if (trans_p->Status == 0)
570 bio_free(tbd_p, sizeof(*tbd_p));
571 bio_free(trans_p, sizeof(*trans_p));