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>
35 #include <netinet/in_systm.h>
36 #include <netinet/in.h>
37 #include <netinet/udp.h>
45 #include <bootstrap.h>
50 * Allocate the PXE buffers statically instead of sticking grimy fingers into
51 * BTX's private data area. The scratch buffer is used to send information to
52 * the PXE BIOS, and the data buffer is used to receive data from the PXE BIOS.
54 #define PXE_BUFFER_SIZE 0x2000
55 #define PXE_TFTP_BUFFER_SIZE 512
56 static char scratch_buffer
[PXE_BUFFER_SIZE
];
57 static char data_buffer
[PXE_BUFFER_SIZE
];
59 static pxenv_t
*pxenv_p
= NULL
; /* PXENV+ */
60 static pxe_t
*pxe_p
= NULL
; /* !PXE */
61 BOOTPLAYER bootplayer
= {0}; /* PXE Cached information. */
63 static int pxe_debug
= 0;
64 static int pxe_sock
= -1;
65 static int pxe_opens
= 0;
67 void pxe_enable(void *pxeinfo
);
68 static void (*pxe_call
)(int func
);
69 static void pxenv_call(int func
);
70 static void bangpxe_call(int func
);
72 static int pxe_init(void);
73 static int pxe_strategy(void *devdata
, int flag
, daddr_t dblk
,
74 size_t offset
, size_t size
, char *buf
, size_t *rsize
);
75 static int pxe_open(struct open_file
*f
, ...);
76 static int pxe_close(struct open_file
*f
);
77 static int pxe_print(int verbose
);
78 static void pxe_cleanup(void);
79 static void pxe_setnfshandle(char *rootpath
);
81 static void pxe_perror(int error
);
82 static int pxe_netif_match(struct netif
*nif
, void *machdep_hint
);
83 static int pxe_netif_probe(struct netif
*nif
, void *machdep_hint
);
84 static void pxe_netif_init(struct iodesc
*desc
, void *machdep_hint
);
85 static int pxe_netif_get(struct iodesc
*desc
, void *pkt
, size_t len
,
87 static int pxe_netif_put(struct iodesc
*desc
, void *pkt
, size_t len
);
88 static void pxe_netif_end(struct netif
*nif
);
91 int nfs_getrootfh(struct iodesc
*, char*, u_char
*);
93 int nfs_getrootfh(struct iodesc
*, char*, uint32_t*, u_char
*);
96 extern struct netif_stats pxe_st
[];
97 extern u_int16_t __bangpxeseg
;
98 extern u_int16_t __bangpxeoff
;
99 extern void __bangpxeentry(void);
100 extern u_int16_t __pxenvseg
;
101 extern u_int16_t __pxenvoff
;
102 extern void __pxenventry(void);
104 struct netif_dif pxe_ifs
[] = {
105 /* dif_unit dif_nsel dif_stats dif_private */
106 {0, 1, &pxe_st
[0], 0}
109 struct netif_stats pxe_st
[NENTS(pxe_ifs
)];
111 struct netif_driver pxenetif
= {
123 struct netif_driver
*netif_drivers
[] = {
128 struct devsw pxedisk
= {
141 * This function is called by the loader to enable PXE support if we
142 * are booted by PXE. The passed in pointer is a pointer to the
146 pxe_enable(void *pxeinfo
)
148 pxenv_p
= (pxenv_t
*)pxeinfo
;
149 pxe_p
= (pxe_t
*)PTOV(pxenv_p
->PXEPtr
.segment
* 16 +
150 pxenv_p
->PXEPtr
.offset
);
155 * return true if pxe structures are found/initialized,
156 * also figures out our IP information via the pxe cached info struct
161 t_PXENV_GET_CACHED_INFO
*gci_p
;
169 /* look for "PXENV+" */
170 if (bcmp((void *)pxenv_p
->Signature
, S_SIZE("PXENV+"))) {
175 /* make sure the size is something we can handle */
176 if (pxenv_p
->Length
> sizeof(*pxenv_p
)) {
177 printf("PXENV+ structure too large, ignoring\n");
184 * add up each byte in the structure, the total should be 0
187 checkptr
= (uint8_t *) pxenv_p
;
188 for (counter
= 0; counter
< pxenv_p
->Length
; counter
++)
189 checksum
+= *checkptr
++;
191 printf("PXENV+ structure failed checksum, ignoring\n");
198 * PXENV+ passed, so use that if !PXE is not available or
199 * the checksum fails.
201 pxe_call
= pxenv_call
;
202 if (pxenv_p
->Version
>= 0x0200) {
204 if (bcmp((void *)pxe_p
->Signature
, S_SIZE("!PXE"))) {
209 checkptr
= (uint8_t *)pxe_p
;
210 for (counter
= 0; counter
< pxe_p
->StructLength
;
212 checksum
+= *checkptr
++;
217 pxe_call
= bangpxe_call
;
222 printf("\nPXE version %d.%d, real mode entry point ",
223 (uint8_t) (pxenv_p
->Version
>> 8),
224 (uint8_t) (pxenv_p
->Version
& 0xFF));
225 if (pxe_call
== bangpxe_call
)
226 printf("@%04x:%04x\n",
227 pxe_p
->EntryPointSP
.segment
,
228 pxe_p
->EntryPointSP
.offset
);
230 printf("@%04x:%04x\n",
231 pxenv_p
->RMEntry
.segment
, pxenv_p
->RMEntry
.offset
);
233 gci_p
= (t_PXENV_GET_CACHED_INFO
*) scratch_buffer
;
234 bzero(gci_p
, sizeof(*gci_p
));
235 gci_p
->PacketType
= PXENV_PACKET_TYPE_BINL_REPLY
;
236 pxe_call(PXENV_GET_CACHED_INFO
);
237 if (gci_p
->Status
!= 0) {
238 pxe_perror(gci_p
->Status
);
242 bcopy(PTOV((gci_p
->Buffer
.segment
<< 4) + gci_p
->Buffer
.offset
),
243 &bootplayer
, gci_p
->BufferSize
);
249 pxe_strategy(void *devdata
, int flag
, daddr_t dblk
, size_t offset
, size_t size
,
250 char *buf
, size_t *rsize
)
256 pxe_open(struct open_file
*f
, ...)
259 char *devname
; /* Device part of file name (or NULL). */
260 char temp
[FNAME_SIZE
];
265 devname
= va_arg(args
, char*);
268 /* On first open, do netif open, mount, etc. */
269 if (pxe_opens
== 0) {
270 /* Find network interface. */
272 pxe_sock
= netif_open(devname
);
274 printf("pxe_open: netif_open() failed\n");
278 printf("pxe_open: netif_open() succeeded\n");
280 if (rootip
.s_addr
== 0) {
282 * Do a bootp/dhcp request to find out where our
283 * NFS/TFTP server is. Even if we dont get back
284 * the proper information, fall back to the server
285 * which brought us to life and a default rootpath.
287 bootp(pxe_sock
, BOOTP_PXE
);
288 if (rootip
.s_addr
== 0)
289 rootip
.s_addr
= bootplayer
.sip
;
290 #ifdef LOADER_NFS_SUPPORT
292 strcpy(rootpath
, PXENFSROOTPATH
);
295 for (i
= 0; rootpath
[i
] != '\0' && i
< FNAME_SIZE
; i
++)
296 if (rootpath
[i
] == ':')
298 if (i
&& i
!= FNAME_SIZE
&& rootpath
[i
] == ':') {
299 rootpath
[i
++] = '\0';
300 if (inet_addr(&rootpath
[0]) != INADDR_NONE
)
301 rootip
.s_addr
= inet_addr(&rootpath
[0]);
302 bcopy(&rootpath
[i
], &temp
[0], strlen(&rootpath
[i
])+1);
303 bcopy(&temp
[0], &rootpath
[0], strlen(&rootpath
[i
])+1);
305 setenv("boot.netif.ip", inet_ntoa(myip
), 1);
306 setenv("boot.netif.netmask", intoa(netmask
), 1);
307 setenv("boot.netif.gateway", inet_ntoa(gateip
), 1);
308 if (bootplayer
.Hardware
== ETHER_TYPE
) {
309 sprintf(temp
, "%6D", bootplayer
.CAddr
, ":");
310 setenv("boot.netif.hwaddr", temp
, 1);
312 #ifdef LOADER_NFS_SUPPORT
313 printf("pxe_open: server addr: %s\n", inet_ntoa(rootip
));
314 printf("pxe_open: server path: %s\n", rootpath
);
315 printf("pxe_open: gateway ip: %s\n", inet_ntoa(gateip
));
317 setenv("boot.nfsroot.server", inet_ntoa(rootip
), 1);
318 setenv("boot.nfsroot.path", rootpath
, 1);
320 setenv("boot.netif.server", inet_ntoa(rootip
), 1);
321 setenv("boot.tftproot.path", rootpath
, 1);
323 setenv("dhcp.host-name", hostname
, 1);
325 setenv("pxeboot.ip", inet_ntoa(myip
), 1);
326 if (bootplayer
.Hardware
== ETHER_TYPE
) {
327 sprintf(temp
, "%6D", bootplayer
.CAddr
, ":");
328 setenv("pxeboot.hwaddr", temp
, 1);
333 f
->f_devdata
= &pxe_sock
;
338 pxe_close(struct open_file
*f
)
343 printf("pxe_close: opens=%d\n", pxe_opens
);
346 /* On last close, do netif close, etc. */
348 /* Extra close call? */
352 /* Not last close? */
356 #ifdef LOADER_NFS_SUPPORT
357 /* get an NFS filehandle for our root filesystem */
358 pxe_setnfshandle(rootpath
);
365 printf("pxe_close: calling netif_close()\n");
367 netif_close(pxe_sock
);
374 pxe_print(int verbose
)
377 if (pxe_call
== NULL
)
380 snprintf(line
, sizeof (line
), " pxe0: %s:%s\n", inet_ntoa(rootip
),
382 return (pager_output(line
));
389 t_PXENV_UNLOAD_STACK
*unload_stack_p
=
390 (t_PXENV_UNLOAD_STACK
*)scratch_buffer
;
391 t_PXENV_UNDI_SHUTDOWN
*undi_shutdown_p
=
392 (t_PXENV_UNDI_SHUTDOWN
*)scratch_buffer
;
395 if (pxe_call
== NULL
)
398 pxe_call(PXENV_UNDI_SHUTDOWN
);
401 if (pxe_debug
&& undi_shutdown_p
->Status
!= 0)
402 printf("pxe_cleanup: UNDI_SHUTDOWN failed %x\n",
403 undi_shutdown_p
->Status
);
406 pxe_call(PXENV_UNLOAD_STACK
);
409 if (pxe_debug
&& unload_stack_p
->Status
!= 0)
410 printf("pxe_cleanup: UNLOAD_STACK failed %x\n",
411 unload_stack_p
->Status
);
421 #ifdef LOADER_NFS_SUPPORT
423 * Reach inside the libstand NFS code and dig out an NFS handle
424 * for the root filesystem.
428 struct iodesc
*iodesc
;
430 u_char fh
[NFS_FHSIZE
];
431 /* structure truncated here */
433 extern struct nfs_iodesc nfs_root_node
;
442 if (!(d
= socktodesc(pxe_sock
)))
444 d
->myport
= htons(--rpc_port
);
446 if ((error
= nfs_getrootfh(d
, rootpath
, nfs_root_node
.fh
)) != 0)
447 printf("NFS MOUNT RPC error: %d\n", error
);
448 nfs_root_node
.iodesc
= d
;
452 pxe_setnfshandle(char *rootpath
)
456 char buf
[2 * NFS_FHSIZE
+ 3], *cp
;
459 * If NFS files were never opened, we need to do mount call
460 * ourselves. Use nfs_root_node.iodesc as flag indicating
461 * previous NFS usage.
463 if (nfs_root_node
.iodesc
== NULL
)
466 fh
= &nfs_root_node
.fh
[0];
469 for (i
= 0; i
< NFS_FHSIZE
; i
++, cp
+= 2)
470 sprintf(cp
, "%02x", fh
[i
]);
472 setenv("boot.nfsroot.nfshandle", buf
, 1);
474 #else /* !OLD_NFSV2 */
476 #define NFS_V3MAXFHSIZE 64
479 struct iodesc
*iodesc
;
482 u_char fh
[NFS_V3MAXFHSIZE
];
483 /* structure truncated */
485 extern struct nfs_iodesc nfs_root_node
;
494 if (!(d
= socktodesc(pxe_sock
)))
496 d
->myport
= htons(--rpc_port
);
498 if ((error
= nfs_getrootfh(d
, rootpath
, &nfs_root_node
.fhsize
,
499 nfs_root_node
.fh
)) != 0) {
500 printf("NFS MOUNT RPC error: %d\n", error
);
501 nfs_root_node
.fhsize
= 0;
503 nfs_root_node
.iodesc
= d
;
507 pxe_setnfshandle(char *rootpath
)
511 char buf
[2 * NFS_V3MAXFHSIZE
+ 3], *cp
;
514 * If NFS files were never opened, we need to do mount call
515 * ourselves. Use nfs_root_node.iodesc as flag indicating
516 * previous NFS usage.
518 if (nfs_root_node
.iodesc
== NULL
)
521 fh
= &nfs_root_node
.fh
[0];
524 for (i
= 0; i
< nfs_root_node
.fhsize
; i
++, cp
+= 2)
525 sprintf(cp
, "%02x", fh
[i
]);
527 setenv("boot.nfsroot.nfshandle", buf
, 1);
528 sprintf(buf
, "%d", nfs_root_node
.fhsize
);
529 setenv("boot.nfsroot.nfshandlelen", buf
, 1);
531 #endif /* OLD_NFSV2 */
532 #endif /* LOADER_NFS_SUPPORT */
539 printf("pxenv_call %x\n", func
);
542 bzero(&v86
, sizeof(v86
));
543 bzero(data_buffer
, sizeof(data_buffer
));
545 __pxenvseg
= pxenv_p
->RMEntry
.segment
;
546 __pxenvoff
= pxenv_p
->RMEntry
.offset
;
548 v86
.ctl
= V86_ADDR
| V86_CALLF
| V86_FLAGS
;
549 v86
.es
= VTOPSEG(scratch_buffer
);
550 v86
.edi
= VTOPOFF(scratch_buffer
);
551 v86
.addr
= (VTOPSEG(__pxenventry
) << 16) | VTOPOFF(__pxenventry
);
558 bangpxe_call(int func
)
562 printf("bangpxe_call %x\n", func
);
565 bzero(&v86
, sizeof(v86
));
566 bzero(data_buffer
, sizeof(data_buffer
));
568 __bangpxeseg
= pxe_p
->EntryPointSP
.segment
;
569 __bangpxeoff
= pxe_p
->EntryPointSP
.offset
;
571 v86
.ctl
= V86_ADDR
| V86_CALLF
| V86_FLAGS
;
572 v86
.edx
= VTOPSEG(scratch_buffer
);
573 v86
.eax
= VTOPOFF(scratch_buffer
);
574 v86
.addr
= (VTOPSEG(__bangpxeentry
) << 16) | VTOPOFF(__bangpxeentry
);
590 pxe_netif_match(struct netif
*nif
, void *machdep_hint
)
597 pxe_netif_probe(struct netif
*nif
, void *machdep_hint
)
599 t_PXENV_UDP_OPEN
*udpopen_p
= (t_PXENV_UDP_OPEN
*)scratch_buffer
;
601 if (pxe_call
== NULL
)
604 bzero(udpopen_p
, sizeof(*udpopen_p
));
605 udpopen_p
->src_ip
= bootplayer
.yip
;
606 pxe_call(PXENV_UDP_OPEN
);
608 if (udpopen_p
->status
!= 0) {
609 printf("pxe_netif_probe: failed %x\n", udpopen_p
->status
);
616 pxe_netif_end(struct netif
*nif
)
618 t_PXENV_UDP_CLOSE
*udpclose_p
= (t_PXENV_UDP_CLOSE
*)scratch_buffer
;
619 bzero(udpclose_p
, sizeof(*udpclose_p
));
621 pxe_call(PXENV_UDP_CLOSE
);
622 if (udpclose_p
->status
!= 0)
623 printf("pxe_end failed %x\n", udpclose_p
->status
);
627 pxe_netif_init(struct iodesc
*desc
, void *machdep_hint
)
630 for (i
= 0; i
< 6; ++i
)
631 desc
->myea
[i
] = bootplayer
.CAddr
[i
];
632 desc
->xid
= bootplayer
.ident
;
636 pxe_netif_get(struct iodesc
*desc
, void *pkt
, size_t len
, time_t timeout
)
642 pxe_netif_put(struct iodesc
*desc
, void *pkt
, size_t len
)
648 sendudp(struct iodesc
*h
, void *pkt
, size_t len
)
650 t_PXENV_UDP_WRITE
*udpwrite_p
= (t_PXENV_UDP_WRITE
*)scratch_buffer
;
651 bzero(udpwrite_p
, sizeof(*udpwrite_p
));
653 udpwrite_p
->ip
= h
->destip
.s_addr
;
654 udpwrite_p
->dst_port
= h
->destport
;
655 udpwrite_p
->src_port
= h
->myport
;
656 udpwrite_p
->buffer_size
= len
;
657 udpwrite_p
->buffer
.segment
= VTOPSEG(pkt
);
658 udpwrite_p
->buffer
.offset
= VTOPOFF(pkt
);
660 if (netmask
== 0 || SAMENET(myip
, h
->destip
, netmask
))
663 udpwrite_p
->gw
= gateip
.s_addr
;
665 pxe_call(PXENV_UDP_WRITE
);
668 /* XXX - I dont know why we need this. */
671 if (udpwrite_p
->status
!= 0) {
672 /* XXX: This happens a lot. It shouldn't. */
673 if (udpwrite_p
->status
!= 1)
674 printf("sendudp failed %x\n", udpwrite_p
->status
);
681 readudp(struct iodesc
*h
, void *pkt
, size_t len
, time_t timeout
)
683 t_PXENV_UDP_READ
*udpread_p
= (t_PXENV_UDP_READ
*)scratch_buffer
;
684 struct udphdr
*uh
= NULL
;
686 uh
= (struct udphdr
*) pkt
- 1;
687 bzero(udpread_p
, sizeof(*udpread_p
));
689 udpread_p
->dest_ip
= h
->myip
.s_addr
;
690 udpread_p
->d_port
= h
->myport
;
691 udpread_p
->buffer_size
= len
;
692 udpread_p
->buffer
.segment
= VTOPSEG(data_buffer
);
693 udpread_p
->buffer
.offset
= VTOPOFF(data_buffer
);
695 pxe_call(PXENV_UDP_READ
);
698 /* XXX - I dont know why we need this. */
701 if (udpread_p
->status
!= 0) {
702 /* XXX: This happens a lot. It shouldn't. */
703 if (udpread_p
->status
!= 1)
704 printf("readudp failed %x\n", udpread_p
->status
);
707 bcopy(data_buffer
, pkt
, udpread_p
->buffer_size
);
708 uh
->uh_sport
= udpread_p
->s_port
;
709 return udpread_p
->buffer_size
;