Catch attempts to queue to unregistered ISRs
[dragonfly.git] / sys / boot / i386 / libi386 / pxe.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 2000 Alfred Perlstein <alfred@freebsd.org>
3 * All rights reserved.
4 * Copyright (c) 2000 Paul Saab <ps@freebsd.org>
5 * All rights reserved.
6 * Copyright (c) 2000 John Baldwin <jhb@freebsd.org>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 *
30 * $FreeBSD: src/sys/boot/i386/libi386/pxe.c,v 1.3.2.9 2001/03/15 08:35:54 ps Exp $
e2565a42 31 * $DragonFly: src/sys/boot/i386/libi386/Attic/pxe.c,v 1.3 2003/11/09 02:22:33 dillon Exp $
984263bc
MD
32 */
33
34#include <stand.h>
35#include <string.h>
36#include <stdarg.h>
37
38#include <netinet/in_systm.h>
39#include <netinet/in.h>
40#include <netinet/udp.h>
41
42#include <net.h>
43#include <netif.h>
44#include <nfsv2.h>
45#include <iodesc.h>
46
47#include <bootp.h>
48#include <bootstrap.h>
49#include "btxv86.h"
50#include "pxe.h"
51
52/*
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.
56 */
57#define PXE_BUFFER_SIZE 0x2000
58#define PXE_TFTP_BUFFER_SIZE 512
59static char scratch_buffer[PXE_BUFFER_SIZE];
60static char data_buffer[PXE_BUFFER_SIZE];
61
62static pxenv_t *pxenv_p = NULL; /* PXENV+ */
63static pxe_t *pxe_p = NULL; /* !PXE */
64static BOOTPLAYER bootplayer; /* PXE Cached information. */
65
66static int pxe_debug = 0;
67static int pxe_sock = -1;
68static int pxe_opens = 0;
69
70void pxe_enable(void *pxeinfo);
71static void (*pxe_call)(int func);
72static void pxenv_call(int func);
73static void bangpxe_call(int func);
74
75static int pxe_init(void);
76static int pxe_strategy(void *devdata, int flag, daddr_t dblk,
77 size_t size, char *buf, size_t *rsize);
78static int pxe_open(struct open_file *f, ...);
79static int pxe_close(struct open_file *f);
80static void pxe_print(int verbose);
81static void pxe_cleanup(void);
82static void pxe_setnfshandle(char *rootpath);
83
84static void pxe_perror(int error);
85static int pxe_netif_match(struct netif *nif, void *machdep_hint);
86static int pxe_netif_probe(struct netif *nif, void *machdep_hint);
87static void pxe_netif_init(struct iodesc *desc, void *machdep_hint);
88static int pxe_netif_get(struct iodesc *desc, void *pkt, size_t len,
89 time_t timeout);
90static int pxe_netif_put(struct iodesc *desc, void *pkt, size_t len);
91static void pxe_netif_end(struct netif *nif);
92
93extern struct netif_stats pxe_st[];
94extern u_int16_t __bangpxeseg;
95extern u_int16_t __bangpxeoff;
96extern void __bangpxeentry(void);
97extern u_int16_t __pxenvseg;
98extern u_int16_t __pxenvoff;
99extern void __pxenventry(void);
100
101struct netif_dif pxe_ifs[] = {
102/* dif_unit dif_nsel dif_stats dif_private */
103 {0, 1, &pxe_st[0], 0}
104};
105
106struct netif_stats pxe_st[NENTS(pxe_ifs)];
107
108struct netif_driver pxenetif = {
109 "pxenet",
110 pxe_netif_match,
111 pxe_netif_probe,
112 pxe_netif_init,
113 pxe_netif_get,
114 pxe_netif_put,
115 pxe_netif_end,
116 pxe_ifs,
117 NENTS(pxe_ifs)
118};
119
120struct netif_driver *netif_drivers[] = {
121 &pxenetif,
122 NULL
123};
124
125struct devsw pxedisk = {
126 "pxe",
127 DEVT_NET,
128 pxe_init,
129 pxe_strategy,
130 pxe_open,
131 pxe_close,
132 noioctl,
133 pxe_print,
134 pxe_cleanup
135};
136
137/*
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
140 * PXENV+ structure.
141 */
142void
143pxe_enable(void *pxeinfo)
144{
145 pxenv_p = (pxenv_t *)pxeinfo;
146 pxe_p = (pxe_t *)PTOV(pxenv_p->PXEPtr.segment * 16 +
147 pxenv_p->PXEPtr.offset);
148 pxe_call = NULL;
149}
150
151/*
152 * return true if pxe structures are found/initialized,
153 * also figures out our IP information via the pxe cached info struct
154 */
155static int
156pxe_init(void)
157{
158 t_PXENV_GET_CACHED_INFO *gci_p;
159 int counter;
160 uint8_t checksum;
161 uint8_t *checkptr;
162
163 if(pxenv_p == NULL)
164 return (0);
165
166 /* look for "PXENV+" */
167 if (bcmp((void *)pxenv_p->Signature, S_SIZE("PXENV+"))) {
168 pxenv_p = NULL;
169 return (0);
170 }
171
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");
175 pxenv_p = NULL;
176 return (0);
177 }
178
179 /*
180 * do byte checksum:
181 * add up each byte in the structure, the total should be 0
182 */
183 checksum = 0;
184 checkptr = (uint8_t *) pxenv_p;
185 for (counter = 0; counter < pxenv_p->Length; counter++)
186 checksum += *checkptr++;
187 if (checksum != 0) {
188 printf("PXENV+ structure failed checksum, ignoring\n");
189 pxenv_p = NULL;
190 return (0);
191 }
192
193
194 /*
195 * PXENV+ passed, so use that if !PXE is not available or
196 * the checksum fails.
197 */
198 pxe_call = pxenv_call;
199 if (pxenv_p->Version >= 0x0200) {
200 for (;;) {
201 if (bcmp((void *)pxe_p->Signature, S_SIZE("!PXE"))) {
202 pxe_p = NULL;
203 break;
204 }
205 checksum = 0;
206 checkptr = (uint8_t *)pxe_p;
207 for (counter = 0; counter < pxe_p->StructLength;
208 counter++)
209 checksum += *checkptr++;
210 if (checksum != 0) {
211 pxe_p = NULL;
212 break;
213 }
214 pxe_call = bangpxe_call;
215 break;
216 }
217 }
218
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);
226 else
227 printf("@%04x:%04x\n",
228 pxenv_p->RMEntry.segment, pxenv_p->RMEntry.offset);
229
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);
236 pxe_p = NULL;
237 return (0);
238 }
239 bcopy(PTOV((gci_p->Buffer.segment << 4) + gci_p->Buffer.offset),
240 &bootplayer, gci_p->BufferSize);
241 return (1);
242}
243
244
245static int
246pxe_strategy(void *devdata, int flag, daddr_t dblk, size_t size,
247 char *buf, size_t *rsize)
248{
249 return (EIO);
250}
251
252static int
253pxe_open(struct open_file *f, ...)
254{
e2565a42 255 __va_list args;
984263bc
MD
256 char *devname; /* Device part of file name (or NULL). */
257 char temp[FNAME_SIZE];
258 int error = 0;
259 int i;
260
e2565a42
MD
261 __va_start(args, f);
262 devname = __va_arg(args, char*);
263 __va_end(args);
984263bc
MD
264
265 /* On first open, do netif open, mount, etc. */
266 if (pxe_opens == 0) {
267 /* Find network interface. */
268 if (pxe_sock < 0) {
269 pxe_sock = netif_open(devname);
270 if (pxe_sock < 0) {
271 printf("pxe_open: netif_open() failed\n");
272 return (ENXIO);
273 }
274 if (pxe_debug)
275 printf("pxe_open: netif_open() succeeded\n");
276 }
277 if (rootip.s_addr == 0) {
278 /*
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.
283 */
284 bootp(pxe_sock, BOOTP_PXE);
285 if (rootip.s_addr == 0)
286 rootip.s_addr = bootplayer.sip;
287 if (!rootpath[1])
288 strcpy(rootpath, PXENFSROOTPATH);
289
290 for (i = 0; i < FNAME_SIZE; i++)
291 if (rootpath[i] == ':')
292 break;
293 if (i && i != FNAME_SIZE) {
294 rootpath[i++] = '\0';
295 if (inet_addr(&rootpath[0]) != INADDR_NONE)
296 rootip.s_addr = inet_addr(&rootpath[0]);
297 bcopy(&rootpath[i], &temp[0], strlen(&rootpath[i])+1);
298 bcopy(&temp[0], &rootpath[0], strlen(&rootpath[i])+1);
299 }
300 printf("pxe_open: server addr: %s\n", inet_ntoa(rootip));
301 printf("pxe_open: server path: %s\n", rootpath);
302 printf("pxe_open: gateway ip: %s\n", inet_ntoa(gateip));
303
304 setenv("boot.netif.ip", inet_ntoa(myip), 1);
305 setenv("boot.netif.netmask", intoa(netmask), 1);
306 setenv("boot.netif.gateway", inet_ntoa(gateip), 1);
307 if (bootplayer.Hardware == ETHER_TYPE) {
308 sprintf(temp, "%6D", bootplayer.CAddr, ":");
309 setenv("boot.netif.hwaddr", temp, 1);
310 }
311 setenv("boot.nfsroot.server", inet_ntoa(rootip), 1);
312 setenv("boot.nfsroot.path", rootpath, 1);
313 }
314 }
315 pxe_opens++;
316 f->f_devdata = &pxe_sock;
317 return (error);
318}
319
320static int
321pxe_close(struct open_file *f)
322{
323
324#ifdef PXE_DEBUG
325 if (pxe_debug)
326 printf("pxe_close: opens=%d\n", pxe_opens);
327#endif
328
329 /* On last close, do netif close, etc. */
330 f->f_devdata = NULL;
331 /* Extra close call? */
332 if (pxe_opens <= 0)
333 return (0);
334 pxe_opens--;
335 /* Not last close? */
336 if (pxe_opens > 0)
337 return(0);
338
339 /* get an NFS filehandle for our root filesystem */
340 pxe_setnfshandle(rootpath);
341
342 if (pxe_sock >= 0) {
343
344#ifdef PXE_DEBUG
345 if (pxe_debug)
346 printf("pxe_close: calling netif_close()\n");
347#endif
348 netif_close(pxe_sock);
349 pxe_sock = -1;
350 }
351 return (0);
352}
353
354static void
355pxe_print(int verbose)
356{
357 if (pxe_call != NULL) {
358 if (*bootplayer.Sname == '\0') {
359 printf(" "IP_STR":%s\n",
360 IP_ARGS(htonl(bootplayer.sip)),
361 bootplayer.bootfile);
362 } else {
363 printf(" %s:%s\n", bootplayer.Sname,
364 bootplayer.bootfile);
365 }
366 }
367
368 return;
369}
370
371static void
372pxe_cleanup(void)
373{
374#ifdef PXE_DEBUG
375 t_PXENV_UNLOAD_STACK *unload_stack_p =
376 (t_PXENV_UNLOAD_STACK *)scratch_buffer;
377 t_PXENV_UNDI_SHUTDOWN *undi_shutdown_p =
378 (t_PXENV_UNDI_SHUTDOWN *)scratch_buffer;
379#endif
380
381 if (pxe_call == NULL)
382 return;
383
384 pxe_call(PXENV_UNDI_SHUTDOWN);
385
386#ifdef PXE_DEBUG
387 if (pxe_debug && undi_shutdown_p->Status != 0)
388 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
389 undi_shutdown_p->Status);
390#endif
391
392 pxe_call(PXENV_UNLOAD_STACK);
393
394#ifdef PXE_DEBUG
395 if (pxe_debug && unload_stack_p->Status != 0)
396 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
397 unload_stack_p->Status);
398#endif
399}
400
401void
402pxe_perror(int err)
403{
404 return;
405}
406
407/*
408 * Reach inside the libstand NFS code and dig out an NFS handle
409 * for the root filesystem.
410 */
411struct nfs_iodesc {
412 struct iodesc *iodesc;
413 off_t off;
414 u_char fh[NFS_FHSIZE];
415 /* structure truncated here */
416};
417extern struct nfs_iodesc nfs_root_node;
418
419static void
420pxe_setnfshandle(char *rootpath)
421{
422 int i;
423 u_char *fh;
424 char buf[2 * NFS_FHSIZE + 3], *cp;
425
426 fh = &nfs_root_node.fh[0];
427 buf[0] = 'X';
428 cp = &buf[1];
429 for (i = 0; i < NFS_FHSIZE; i++, cp += 2)
430 sprintf(cp, "%02x", fh[i]);
431 sprintf(cp, "X");
432 setenv("boot.nfsroot.nfshandle", buf, 1);
433}
434
435void
436pxenv_call(int func)
437{
438#ifdef PXE_DEBUG
439 if (pxe_debug)
440 printf("pxenv_call %x\n", func);
441#endif
442
443 bzero(&v86, sizeof(v86));
444 bzero(data_buffer, sizeof(data_buffer));
445
446 __pxenvseg = pxenv_p->RMEntry.segment;
447 __pxenvoff = pxenv_p->RMEntry.offset;
448
449 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
450 v86.es = VTOPSEG(scratch_buffer);
451 v86.edi = VTOPOFF(scratch_buffer);
452 v86.addr = (VTOPSEG(__pxenventry) << 16) | VTOPOFF(__pxenventry);
453 v86.ebx = func;
454 v86int();
455 v86.ctl = V86_FLAGS;
456}
457
458void
459bangpxe_call(int func)
460{
461#ifdef PXE_DEBUG
462 if (pxe_debug)
463 printf("bangpxe_call %x\n", func);
464#endif
465
466 bzero(&v86, sizeof(v86));
467 bzero(data_buffer, sizeof(data_buffer));
468
469 __bangpxeseg = pxe_p->EntryPointSP.segment;
470 __bangpxeoff = pxe_p->EntryPointSP.offset;
471
472 v86.ctl = V86_ADDR | V86_CALLF | V86_FLAGS;
473 v86.edx = VTOPSEG(scratch_buffer);
474 v86.eax = VTOPOFF(scratch_buffer);
475 v86.addr = (VTOPSEG(__bangpxeentry) << 16) | VTOPOFF(__bangpxeentry);
476 v86.ebx = func;
477 v86int();
478 v86.ctl = V86_FLAGS;
479}
480
481
482time_t
483getsecs()
484{
485 time_t n = 0;
486 time(&n);
487 return n;
488}
489
490static int
491pxe_netif_match(struct netif *nif, void *machdep_hint)
492{
493 return 1;
494}
495
496
497static int
498pxe_netif_probe(struct netif *nif, void *machdep_hint)
499{
500 t_PXENV_UDP_OPEN *udpopen_p = (t_PXENV_UDP_OPEN *)scratch_buffer;
501
502 if (pxe_call == NULL)
503 return -1;
504
505 bzero(udpopen_p, sizeof(*udpopen_p));
506 udpopen_p->src_ip = bootplayer.yip;
507 pxe_call(PXENV_UDP_OPEN);
508
509 if (udpopen_p->status != 0) {
510 printf("pxe_netif_probe: failed %x\n", udpopen_p->status);
511 return -1;
512 }
513 return 0;
514}
515
516static void
517pxe_netif_end(struct netif *nif)
518{
519 t_PXENV_UDP_CLOSE *udpclose_p = (t_PXENV_UDP_CLOSE *)scratch_buffer;
520 bzero(udpclose_p, sizeof(*udpclose_p));
521
522 pxe_call(PXENV_UDP_CLOSE);
523 if (udpclose_p->status != 0)
524 printf("pxe_end failed %x\n", udpclose_p->status);
525}
526
527static void
528pxe_netif_init(struct iodesc *desc, void *machdep_hint)
529{
530 int i;
531 for (i = 0; i < 6; ++i)
532 desc->myea[i] = bootplayer.CAddr[i];
533 desc->xid = bootplayer.ident;
534}
535
536static int
537pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t timeout)
538{
539 return len;
540}
541
542static int
543pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
544{
545 return len;
546}
547
548ssize_t
549sendudp(struct iodesc *h, void *pkt, size_t len)
550{
551 t_PXENV_UDP_WRITE *udpwrite_p = (t_PXENV_UDP_WRITE *)scratch_buffer;
552 bzero(udpwrite_p, sizeof(*udpwrite_p));
553
554 udpwrite_p->ip = h->destip.s_addr;
555 udpwrite_p->dst_port = h->destport;
556 udpwrite_p->src_port = h->myport;
557 udpwrite_p->buffer_size = len;
558 udpwrite_p->buffer.segment = VTOPSEG(pkt);
559 udpwrite_p->buffer.offset = VTOPOFF(pkt);
560
561 if (netmask == 0 || SAMENET(myip, h->destip, netmask))
562 udpwrite_p->gw = 0;
563 else
564 udpwrite_p->gw = gateip.s_addr;
565
566 pxe_call(PXENV_UDP_WRITE);
567
568#if 0
569 /* XXX - I dont know why we need this. */
570 delay(1000);
571#endif
572 if (udpwrite_p->status != 0) {
573 /* XXX: This happens a lot. It shouldn't. */
574 if (udpwrite_p->status != 1)
575 printf("sendudp failed %x\n", udpwrite_p->status);
576 return -1;
577 }
578 return len;
579}
580
581ssize_t
582readudp(struct iodesc *h, void *pkt, size_t len, time_t timeout)
583{
584 t_PXENV_UDP_READ *udpread_p = (t_PXENV_UDP_READ *)scratch_buffer;
585 struct udphdr *uh = NULL;
586
587 uh = (struct udphdr *) pkt - 1;
588 bzero(udpread_p, sizeof(*udpread_p));
589
590 udpread_p->dest_ip = h->myip.s_addr;
591 udpread_p->d_port = h->myport;
592 udpread_p->buffer_size = len;
593 udpread_p->buffer.segment = VTOPSEG(data_buffer);
594 udpread_p->buffer.offset = VTOPOFF(data_buffer);
595
596 pxe_call(PXENV_UDP_READ);
597
598#if 0
599 /* XXX - I dont know why we need this. */
600 delay(1000);
601#endif
602 if (udpread_p->status != 0) {
603 /* XXX: This happens a lot. It shouldn't. */
604 if (udpread_p->status != 1)
605 printf("readudp failed %x\n", udpread_p->status);
606 return -1;
607 }
608 bcopy(data_buffer, pkt, udpread_p->buffer_size);
609 uh->uh_sport = udpread_p->s_port;
610 return udpread_p->buffer_size;
611}