1 /*-
2 * Copyright (c) 2000 Alfred Perlstein <alfred@freebsd.org>
3 * Copyright (c) 2000 Paul Saab <ps@freebsd.org>
4 * All rights reserved.
5 * Copyright (c) 2000 John Baldwin <jhb@freebsd.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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.
15 *
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
26 * SUCH DAMAGE.
27 */
28
29 #include <stand.h>
30 #include <errno.h>
31 #include <stdbool.h>
32 #include <stddef.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <sys/param.h>
36
37 #include <net/ethernet.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/in.h>
40 #include <netinet/ip.h>
41 #include <netinet/udp.h>
42
43 #include <net.h>
44 #include <netif.h>
45 #include <nfsv2.h>
46 #include <iodesc.h>
47
48 #include <bootp.h>
49 #include <bootstrap.h>
50 #include "libi386.h"
51 #include "btxv86.h"
52 #include "pxe.h"
53
54 static pxenv_t *pxenv_p = NULL; /* PXENV+ */
55 static pxe_t *pxe_p = NULL; /* !PXE */
56
57 #ifdef PXE_DEBUG
58 static int pxe_debug = 0;
59 #endif
60
61 void pxe_enable(void *pxeinfo);
62 static void (*pxe_call)(int func, void *ptr);
63 static void pxenv_call(int func, void *ptr);
64 static void bangpxe_call(int func, void *ptr);
65
66 static int pxe_init(void);
67 static int pxe_print(int verbose);
68 static void pxe_cleanup(void);
69
70 static void pxe_perror(int error);
71 static int pxe_netif_match(struct netif *nif, void *machdep_hint);
72 static int pxe_netif_probe(struct netif *nif, void *machdep_hint);
73 static void pxe_netif_init(struct iodesc *desc, void *machdep_hint);
74 static ssize_t pxe_netif_get(struct iodesc *, void **, time_t);
75 static ssize_t pxe_netif_put(struct iodesc *desc, void *pkt, size_t len);
76 static void pxe_netif_end(struct netif *nif);
77
78 extern struct netif_stats pxe_st[];
79 extern uint16_t __bangpxeseg;
80 extern uint16_t __bangpxeoff;
81 extern void __bangpxeentry(void);
82 extern uint16_t __pxenvseg;
83 extern uint16_t __pxenvoff;
84 extern void __pxenventry(void);
85
86 struct netif_dif pxe_ifs[] = {
87 /* dif_unit dif_nsel dif_stats dif_private */
88 {0, 1, &pxe_st[0], 0}
89 };
90
91 struct netif_stats pxe_st[nitems(pxe_ifs)];
92
93 struct netif_driver pxenetif = {
94 .netif_bname = "pxenet",
95 .netif_match = pxe_netif_match,
96 .netif_probe = pxe_netif_probe,
97 .netif_init = pxe_netif_init,
98 .netif_get = pxe_netif_get,
99 .netif_put = pxe_netif_put,
100 .netif_end = pxe_netif_end,
101 .netif_ifs = pxe_ifs,
102 .netif_nifs = nitems(pxe_ifs)
103 };
104
105 struct netif_driver *netif_drivers[] = {
106 &pxenetif,
107 NULL
108 };
109
110 struct devsw pxedisk = {
111 .dv_name = "net",
112 .dv_type = DEVT_NET,
113 .dv_init = pxe_init,
114 .dv_strategy = NULL, /* Will be set in pxe_init */
115 .dv_open = NULL, /* Will be set in pxe_init */
116 .dv_close = NULL, /* Will be set in pxe_init */
117 .dv_ioctl = noioctl,
118 .dv_print = pxe_print,
119 .dv_cleanup = pxe_cleanup,
120 };
121
122 /*
123 * This function is called by the loader to enable PXE support if we
124 * are booted by PXE. The passed in pointer is a pointer to the PXENV+
125 * structure.
126 */
127 void
pxe_enable(void * pxeinfo)128 pxe_enable(void *pxeinfo)
129 {
130 pxenv_p = (pxenv_t *)pxeinfo;
131 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
132 pxenv_p->PXEPtr.offset);
133 pxe_call = NULL;
134 }
135
136 /*
137 * return true if pxe structures are found/initialized,
138 * also figures out our IP information via the pxe cached info struct
139 */
140 static int
pxe_init(void)141 pxe_init(void)
142 {
143 t_PXENV_GET_CACHED_INFO *gci_p;
144 int counter;
145 uint8_t checksum;
146 uint8_t *checkptr;
147 extern struct devsw netdev;
148
149 if (pxenv_p == NULL)
150 return (0);
151
152 /* RFC 4578 § 2.1: BIOS PXE is a 32-bit "Standard PC BIOS" client. */
153 bootp_client_arch = 0x0000;
154
155 /* look for "PXENV+" */
156 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
157 pxenv_p = NULL;
158 return (0);
159 }
160
161 /* make sure the size is something we can handle */
162 if (pxenv_p->Length > sizeof(*pxenv_p)) {
163 printf("PXENV+ structure too large, ignoring\n");
164 pxenv_p = NULL;
165 return (0);
166 }
167
168 /*
169 * do byte checksum:
170 * add up each byte in the structure, the total should be 0
171 */
172 checksum = 0;
173 checkptr = (uint8_t *) pxenv_p;
174 for (counter = 0; counter < pxenv_p->Length; counter++)
175 checksum += *checkptr++;
176 if (checksum != 0) {
177 printf("PXENV+ structure failed checksum, ignoring\n");
178 pxenv_p = NULL;
179 return (0);
180 }
181
182 /*
183 * PXENV+ passed, so use that if !PXE is not available or
184 * the checksum fails.
185 */
186 pxe_call = pxenv_call;
187 if (pxenv_p->Version >= 0x0200) {
188 for (;;) {
189 if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
190 pxe_p = NULL;
191 break;
192 }
193 checksum = 0;
194 checkptr = (uint8_t *)pxe_p;
195 for (counter = 0; counter < pxe_p->StructLength;
196 counter++)
197 checksum += *checkptr++;
198 if (checksum != 0) {
199 pxe_p = NULL;
200 break;
201 }
202 pxe_call = bangpxe_call;
203 break;
204 }
205 }
206
207 pxedisk.dv_open = netdev.dv_open;
208 pxedisk.dv_close = netdev.dv_close;
209 pxedisk.dv_strategy = netdev.dv_strategy;
210
211 printf("\nPXE version %d.%d, real mode entry point ",
212 (uint8_t) (pxenv_p->Version >> 8),
213 (uint8_t) (pxenv_p->Version & 0xFF));
214 if (pxe_call == bangpxe_call)
215 printf("@%04x:%04x\n",
216 pxe_p->EntryPointSP.segment,
217 pxe_p->EntryPointSP.offset);
218 else
219 printf("@%04x:%04x\n",
220 pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
221
222 gci_p = bio_alloc(sizeof(*gci_p));
223 if (gci_p == NULL) {
224 pxe_p = NULL;
225 return (0);
226 }
227 bzero(gci_p, sizeof(*gci_p));
228 gci_p->PacketType = PXENV_PACKET_TYPE_BINL_REPLY;
229 pxe_call(PXENV_GET_CACHED_INFO, gci_p);
230 if (gci_p->Status != 0) {
231 pxe_perror(gci_p->Status);
232 bio_free(gci_p, sizeof(*gci_p));
233 pxe_p = NULL;
234 return (0);
235 }
236 free(bootp_response);
237 if ((bootp_response = malloc(gci_p->BufferSize)) != NULL) {
238 bootp_response_size = gci_p->BufferSize;
239 bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
240 bootp_response, bootp_response_size);
241 }
242 bio_free(gci_p, sizeof(*gci_p));
243 return (1);
244 }
245
246 static int
pxe_print(int verbose)247 pxe_print(int verbose)
248 {
249 if (pxe_call == NULL)
250 return (0);
251
252 printf("%s devices:", pxedisk.dv_name);
253 if (pager_output("\n") != 0)
254 return (1);
255 printf(" %s0:", pxedisk.dv_name);
256 if (verbose) {
257 printf(" %s:%s", inet_ntoa(rootip), rootpath);
258 }
259 return (pager_output("\n"));
260 }
261
262 static void
pxe_cleanup(void)263 pxe_cleanup(void)
264 {
265 t_PXENV_UNLOAD_STACK *unload_stack_p;
266 t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p;
267
268 if (pxe_call == NULL)
269 return;
270
271 undi_shutdown_p = bio_alloc(sizeof(*undi_shutdown_p));
272 if (undi_shutdown_p != NULL) {
273 bzero(undi_shutdown_p, sizeof(*undi_shutdown_p));
274 pxe_call(PXENV_UNDI_SHUTDOWN, undi_shutdown_p);
275
276 #ifdef PXE_DEBUG
277 if (pxe_debug && undi_shutdown_p->Status != 0)
278 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
279 undi_shutdown_p->Status);
280 #endif
281 bio_free(undi_shutdown_p, sizeof(*undi_shutdown_p));
282 }
283
284 unload_stack_p = bio_alloc(sizeof(*unload_stack_p));
285 if (unload_stack_p != NULL) {
286 bzero(unload_stack_p, sizeof(*unload_stack_p));
287 pxe_call(PXENV_UNLOAD_STACK, unload_stack_p);
288
289 #ifdef PXE_DEBUG
290 if (pxe_debug && unload_stack_p->Status != 0)
291 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
292 unload_stack_p->Status);
293 #endif
294 bio_free(unload_stack_p, sizeof(*unload_stack_p));
295 }
296 }
297
298 void
pxe_perror(int err)299 pxe_perror(int err)
300 {
301 return;
302 }
303
304 void
pxenv_call(int func,void * ptr)305 pxenv_call(int func, void *ptr)
306 {
307 #ifdef PXE_DEBUG
308 if (pxe_debug)
309 printf("pxenv_call %x\n", func);
310 #endif
311
312 bzero(&v86, sizeof(v86));
313
314 __pxenvseg = pxenv_p->RMEntry.segment;
315 __pxenvoff = pxenv_p->RMEntry.offset;
316
317 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
318 v86.es = VTOPSEG(ptr);
319 v86.edi = VTOPOFF(ptr);
320 v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
321 v86.ebx = func;
322 v86int();
323 v86.ctl = V86_FLAGS;
324 }
325
326 void
bangpxe_call(int func,void * ptr)327 bangpxe_call(int func, void *ptr)
328 {
329 #ifdef PXE_DEBUG
330 if (pxe_debug)
331 printf("bangpxe_call %x\n", func);
332 #endif
333
334 bzero(&v86, sizeof(v86));
335
336 __bangpxeseg = pxe_p->EntryPointSP.segment;
337 __bangpxeoff = pxe_p->EntryPointSP.offset;
338
339 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
340 v86.edx = VTOPSEG(ptr);
341 v86.eax = VTOPOFF(ptr);
342 v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
343 v86.ebx = func;
344 v86int();
345 v86.ctl = V86_FLAGS;
346 }
347
348
349 static int
pxe_netif_match(struct netif * nif,void * machdep_hint)350 pxe_netif_match(struct netif *nif, void *machdep_hint)
351 {
352 return (1);
353 }
354
355 static int
pxe_netif_probe(struct netif * nif,void * machdep_hint)356 pxe_netif_probe(struct netif *nif, void *machdep_hint)
357 {
358 if (pxe_call == NULL)
359 return (-1);
360
361 return (0);
362 }
363
364 static void
pxe_netif_end(struct netif * nif)365 pxe_netif_end(struct netif *nif)
366 {
367 t_PXENV_UNDI_CLOSE *undi_close_p;
368
369 undi_close_p = bio_alloc(sizeof(*undi_close_p));
370 if (undi_close_p != NULL) {
371 bzero(undi_close_p, sizeof(*undi_close_p));
372 pxe_call(PXENV_UNDI_CLOSE, undi_close_p);
373 if (undi_close_p->Status != 0)
374 printf("undi close failed: %x\n", undi_close_p->Status);
375 bio_free(undi_close_p, sizeof(*undi_close_p));
376 }
377 }
378
379 static void
pxe_netif_init(struct iodesc * desc,void * machdep_hint)380 pxe_netif_init(struct iodesc *desc, void *machdep_hint)
381 {
382 t_PXENV_UNDI_GET_INFORMATION *undi_info_p;
383 t_PXENV_UNDI_OPEN *undi_open_p;
384 uint8_t *mac;
385 int i, len;
386
387 undi_info_p = bio_alloc(sizeof(*undi_info_p));
388 if (undi_info_p == NULL)
389 return;
390
391 bzero(undi_info_p, sizeof(*undi_info_p));
392 pxe_call(PXENV_UNDI_GET_INFORMATION, undi_info_p);
393 if (undi_info_p->Status != 0) {
394 printf("undi get info failed: %x\n", undi_info_p->Status);
395 bio_free(undi_info_p, sizeof(*undi_info_p));
396 return;
397 }
398
399 /* Make sure the CurrentNodeAddress is valid. */
400 for (i = 0; i < undi_info_p->HwAddrLen; ++i) {
401 if (undi_info_p->CurrentNodeAddress[i] != 0)
402 break;
403 }
404 if (i < undi_info_p->HwAddrLen) {
405 for (i = 0; i < undi_info_p->HwAddrLen; ++i) {
406 if (undi_info_p->CurrentNodeAddress[i] != 0xff)
407 break;
408 }
409 }
410 if (i < undi_info_p->HwAddrLen)
411 mac = undi_info_p->CurrentNodeAddress;
412 else
413 mac = undi_info_p->PermNodeAddress;
414
415 len = min(sizeof (desc->myea), undi_info_p->HwAddrLen);
416 for (i = 0; i < len; ++i)
417 desc->myea[i] = mac[i];
418
419 bio_free(undi_info_p, sizeof(*undi_info_p));
420 undi_open_p = bio_alloc(sizeof(*undi_open_p));
421 if (undi_open_p == NULL)
422 return;
423 bzero(undi_open_p, sizeof(*undi_open_p));
424 undi_open_p->PktFilter = FLTR_DIRECTED | FLTR_BRDCST;
425 pxe_call(PXENV_UNDI_OPEN, undi_open_p);
426 if (undi_open_p->Status != 0)
427 printf("undi open failed: %x\n", undi_open_p->Status);
428 bio_free(undi_open_p, sizeof(*undi_open_p));
429 }
430
431 static int
pxe_netif_receive_isr(t_PXENV_UNDI_ISR * isr,void ** pkt,ssize_t * retsize)432 pxe_netif_receive_isr(t_PXENV_UNDI_ISR *isr, void **pkt, ssize_t *retsize)
433 {
434 static bool data_pending;
435 char *buf, *ptr, *frame;
436 size_t size, rsize;
437
438 buf = NULL;
439 size = rsize = 0;
440
441 /*
442 * We can save ourselves the next two pxe calls because we already know
443 * we weren't done grabbing everything.
444 */
445 if (data_pending) {
446 data_pending = false;
447 goto nextbuf;
448 }
449
450 /*
451 * We explicitly don't check for OURS/NOT_OURS as a result of START;
452 * it's been reported that some cards are known to mishandle these.
453 */
454 bzero(isr, sizeof(*isr));
455 isr->FuncFlag = PXENV_UNDI_ISR_IN_START;
456 pxe_call(PXENV_UNDI_ISR, isr);
457 /* We could translate Status... */
458 if (isr->Status != 0) {
459 return (ENXIO);
460 }
461
462 bzero(isr, sizeof(*isr));
463 isr->FuncFlag = PXENV_UNDI_ISR_IN_PROCESS;
464 pxe_call(PXENV_UNDI_ISR, isr);
465 if (isr->Status != 0) {
466 return (ENXIO);
467 }
468 if (isr->FuncFlag == PXENV_UNDI_ISR_OUT_BUSY) {
469 /*
470 * Let the caller decide if we need to be restarted. It will
471 * currently blindly restart us, but it could check timeout in
472 * the future.
473 */
474 return (ERESTART);
475 }
476
477 /*
478 * By design, we'll hardly ever hit this terminal condition unless we
479 * pick up nothing but tx interrupts here. More frequently, we will
480 * process rx buffers until we hit the terminal condition in the middle.
481 */
482 while (isr->FuncFlag != PXENV_UNDI_ISR_OUT_DONE) {
483 /*
484 * This might have given us PXENV_UNDI_ISR_OUT_TRANSMIT, in
485 * which case we can just disregard and move on to the next
486 * buffer/frame.
487 */
488 if (isr->FuncFlag != PXENV_UNDI_ISR_OUT_RECEIVE)
489 goto nextbuf;
490
491 if (buf == NULL) {
492 /*
493 * Grab size from the first Frame that we picked up,
494 * allocate an rx buf to hold. Careful here, as we may
495 * see a fragmented frame that's spread out across
496 * multiple GET_NEXT calls.
497 */
498 size = isr->FrameLength;
499 buf = malloc(size + ETHER_ALIGN);
500 if (buf == NULL)
501 return (ENOMEM);
502
503 ptr = buf + ETHER_ALIGN;
504 }
505
506 frame = (char *)((uintptr_t)isr->Frame.segment << 4);
507 frame += isr->Frame.offset;
508 bcopy(PTOV(frame), ptr, isr->BufferLength);
509 ptr += isr->BufferLength;
510 rsize += isr->BufferLength;
511
512 /*
513 * Stop here before we risk catching the start of another frame.
514 * It would be nice to continue reading until we actually get a
515 * PXENV_UNDI_ISR_OUT_DONE, but our network stack in libsa isn't
516 * suitable for reading more than one packet at a time.
517 */
518 if (rsize >= size) {
519 data_pending = true;
520 break;
521 }
522
523 nextbuf:
524 bzero(isr, sizeof(*isr));
525 isr->FuncFlag = PXENV_UNDI_ISR_IN_GET_NEXT;
526 pxe_call(PXENV_UNDI_ISR, isr);
527 if (isr->Status != 0) {
528 free(buf);
529 return (ENXIO);
530 }
531 }
532
533 /*
534 * We may have never picked up a frame at all (all tx), in which case
535 * the caller should restart us.
536 */
537 if (rsize == 0) {
538 return (ERESTART);
539 }
540
541 *pkt = buf;
542 *retsize = rsize;
543 return (0);
544 }
545
546 static int
pxe_netif_receive(void ** pkt,ssize_t * size)547 pxe_netif_receive(void **pkt, ssize_t *size)
548 {
549 t_PXENV_UNDI_ISR *isr;
550 int ret;
551
552 isr = bio_alloc(sizeof(*isr));
553 if (isr == NULL)
554 return (ENOMEM);
555
556 /*
557 * This completely ignores the timeout specified in pxe_netif_get(), but
558 * we shouldn't be running long enough here for that to make a
559 * difference.
560 */
561 for (;;) {
562 /* We'll only really re-enter for PXENV_UNDI_ISR_OUT_BUSY. */
563 ret = pxe_netif_receive_isr(isr, pkt, size);
564 if (ret != ERESTART)
565 break;
566 }
567
568 bio_free(isr, sizeof(*isr));
569 return (ret);
570 }
571
572 static ssize_t
pxe_netif_get(struct iodesc * desc,void ** pkt,time_t timeout)573 pxe_netif_get(struct iodesc *desc, void **pkt, time_t timeout)
574 {
575 time_t t;
576 void *ptr;
577 int ret = -1;
578 ssize_t size;
579
580 t = getsecs();
581 size = 0;
582 while ((getsecs() - t) < timeout) {
583 ret = pxe_netif_receive(&ptr, &size);
584 if (ret != -1) {
585 *pkt = ptr;
586 break;
587 }
588 }
589
590 return (ret == 0 ? size : -1);
591 }
592
593 static ssize_t
pxe_netif_put(struct iodesc * desc,void * pkt,size_t len)594 pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
595 {
596 t_PXENV_UNDI_TRANSMIT *trans_p;
597 t_PXENV_UNDI_TBD *tbd_p;
598 char *data;
599 ssize_t rv = -1;
600
601 trans_p = bio_alloc(sizeof(*trans_p));
602 tbd_p = bio_alloc(sizeof(*tbd_p));
603 data = bio_alloc(len);
604
605 if (trans_p != NULL && tbd_p != NULL && data != NULL) {
606 bzero(trans_p, sizeof(*trans_p));
607 bzero(tbd_p, sizeof(*tbd_p));
608
609 trans_p->TBD.segment = VTOPSEG(tbd_p);
610 trans_p->TBD.offset = VTOPOFF(tbd_p);
611
612 tbd_p->ImmedLength = len;
613 tbd_p->Xmit.segment = VTOPSEG(data);
614 tbd_p->Xmit.offset = VTOPOFF(data);
615 bcopy(pkt, data, len);
616
617 pxe_call(PXENV_UNDI_TRANSMIT, trans_p);
618 if (trans_p->Status == 0)
619 rv = len;
620 }
621
622 bio_free(data, len);
623 bio_free(tbd_p, sizeof(*tbd_p));
624 bio_free(trans_p, sizeof(*trans_p));
625 return (rv);
626 }
627