Home | History | Annotate | Line # | Download | only in pxeboot
pxe.c revision 1.5
      1  1.5  drochner /*	$NetBSD: pxe.c,v 1.5 2003/03/11 18:29:00 drochner Exp $	*/
      2  1.1   thorpej 
      3  1.1   thorpej /*
      4  1.1   thorpej  * Copyright 2001 Wasabi Systems, Inc.
      5  1.1   thorpej  * All rights reserved.
      6  1.1   thorpej  *
      7  1.1   thorpej  * Written by Jason R. Thorpe for Wasabi Systems, Inc.
      8  1.1   thorpej  *
      9  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     10  1.1   thorpej  * modification, are permitted provided that the following conditions
     11  1.1   thorpej  * are met:
     12  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     13  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     14  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     16  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     17  1.1   thorpej  * 3. All advertising materials mentioning features or use of this software
     18  1.1   thorpej  *    must display the following acknowledgement:
     19  1.1   thorpej  *	This product includes software developed for the NetBSD Project by
     20  1.1   thorpej  *	Wasabi Systems, Inc.
     21  1.1   thorpej  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     22  1.1   thorpej  *    or promote products derived from this software without specific prior
     23  1.1   thorpej  *    written permission.
     24  1.1   thorpej  *
     25  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     26  1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     29  1.1   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1   thorpej  */
     37  1.1   thorpej 
     38  1.1   thorpej /*
     39  1.1   thorpej  * Copyright (c) 2000 Alfred Perlstein <alfred (at) freebsd.org>
     40  1.1   thorpej  * All rights reserved.
     41  1.1   thorpej  * Copyright (c) 2000 Paul Saab <ps (at) freebsd.org>
     42  1.1   thorpej  * All rights reserved.
     43  1.1   thorpej  * Copyright (c) 2000 John Baldwin <jhb (at) freebsd.org>
     44  1.1   thorpej  * All rights reserved.
     45  1.1   thorpej  *
     46  1.1   thorpej  * Redistribution and use in source and binary forms, with or without
     47  1.1   thorpej  * modification, are permitted provided that the following conditions
     48  1.1   thorpej  * are met:
     49  1.1   thorpej  * 1. Redistributions of source code must retain the above copyright
     50  1.1   thorpej  *    notice, this list of conditions and the following disclaimer.
     51  1.1   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     52  1.1   thorpej  *    notice, this list of conditions and the following disclaimer in the
     53  1.1   thorpej  *    documentation and/or other materials provided with the distribution.
     54  1.1   thorpej  *
     55  1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     56  1.1   thorpej  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     57  1.1   thorpej  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     58  1.1   thorpej  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     59  1.1   thorpej  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     60  1.1   thorpej  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     61  1.1   thorpej  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     62  1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     63  1.1   thorpej  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     64  1.1   thorpej  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     65  1.1   thorpej  * SUCH DAMAGE.
     66  1.1   thorpej  */
     67  1.1   thorpej 
     68  1.1   thorpej /*
     69  1.1   thorpej  * Support for the Intel Preboot Execution Environment (PXE).
     70  1.1   thorpej  *
     71  1.1   thorpej  * PXE provides a UDP implementation as well as a UNDI network device
     72  1.1   thorpej  * driver.  UNDI is much more complicated to use than PXE UDP, so we
     73  1.1   thorpej  * use PXE UDP as a cheap and easy way to get PXE support.
     74  1.1   thorpej  */
     75  1.1   thorpej 
     76  1.1   thorpej #include <sys/param.h>
     77  1.1   thorpej #include <sys/socket.h>
     78  1.1   thorpej 
     79  1.1   thorpej #ifdef _STANDALONE
     80  1.1   thorpej #include <lib/libkern/libkern.h>
     81  1.1   thorpej #else
     82  1.1   thorpej #include <string.h>
     83  1.1   thorpej #endif
     84  1.1   thorpej 
     85  1.1   thorpej #include <netinet/in.h>
     86  1.1   thorpej #include <netinet/in_systm.h>
     87  1.1   thorpej #include <netinet/ip.h>
     88  1.1   thorpej #include <netinet/ip_var.h>
     89  1.1   thorpej #include <netinet/udp.h>
     90  1.1   thorpej #include <netinet/udp_var.h>
     91  1.1   thorpej 
     92  1.1   thorpej #include <net/if_ether.h>
     93  1.1   thorpej 
     94  1.1   thorpej #include <lib/libsa/stand.h>
     95  1.1   thorpej #include <lib/libsa/net.h>
     96  1.1   thorpej #include <lib/libsa/netif.h>
     97  1.1   thorpej #include <lib/libsa/bootp.h>
     98  1.1   thorpej 
     99  1.1   thorpej #include <libi386.h>
    100  1.1   thorpej #include <bootinfo.h>
    101  1.1   thorpej 
    102  1.1   thorpej #include "pxeboot.h"
    103  1.1   thorpej #include "pxe.h"
    104  1.1   thorpej 
    105  1.1   thorpej void	(*pxe_call)(u_int16_t);
    106  1.1   thorpej 
    107  1.1   thorpej void	pxecall_bangpxe(u_int16_t);	/* pxe_call.S */
    108  1.1   thorpej void	pxecall_pxenv(u_int16_t);	/* pxe_call.S */
    109  1.1   thorpej 
    110  1.1   thorpej char pxe_command_buf[256];
    111  1.1   thorpej 
    112  1.1   thorpej BOOTPLAYER bootplayer;
    113  1.1   thorpej 
    114  1.1   thorpej /*****************************************************************************
    115  1.1   thorpej  * This section is a replacement for libsa/udp.c
    116  1.1   thorpej  *****************************************************************************/
    117  1.1   thorpej 
    118  1.1   thorpej /* Caller must leave room for ethernet, ip, and udp headers in front!! */
    119  1.1   thorpej ssize_t
    120  1.1   thorpej sendudp(struct iodesc *d, void *pkt, size_t len)
    121  1.1   thorpej {
    122  1.1   thorpej 	t_PXENV_UDP_WRITE *uw = (void *) pxe_command_buf;
    123  1.1   thorpej 
    124  1.1   thorpej 	uw->status = 0;
    125  1.1   thorpej 
    126  1.1   thorpej 	uw->ip = d->destip.s_addr;
    127  1.1   thorpej 	uw->gw = gateip.s_addr;
    128  1.1   thorpej 	uw->src_port = d->myport;
    129  1.1   thorpej 	uw->dst_port = d->destport;
    130  1.1   thorpej 	uw->buffer_size = len;
    131  1.1   thorpej 	uw->buffer.segment = VTOPSEG(pkt);
    132  1.1   thorpej 	uw->buffer.offset = VTOPOFF(pkt);
    133  1.1   thorpej 
    134  1.1   thorpej 	pxe_call(PXENV_UDP_WRITE);
    135  1.1   thorpej 
    136  1.1   thorpej 	if (uw->status != PXENV_STATUS_SUCCESS) {
    137  1.1   thorpej 		/* XXX This happens a lot; it shouldn't. */
    138  1.1   thorpej 		if (uw->status != PXENV_STATUS_FAILURE)
    139  1.1   thorpej 			printf("sendudp: PXENV_UDP_WRITE failed: 0x%x\n",
    140  1.1   thorpej 			    uw->status);
    141  1.1   thorpej 		return (-1);
    142  1.1   thorpej 	}
    143  1.1   thorpej 
    144  1.1   thorpej 	return (len);
    145  1.1   thorpej }
    146  1.1   thorpej 
    147  1.1   thorpej /*
    148  1.1   thorpej  * Receive a UDP packet and validate it for us.
    149  1.1   thorpej  * Caller leaves room for the headers (Ether, IP, UDP).
    150  1.1   thorpej  */
    151  1.1   thorpej ssize_t
    152  1.1   thorpej readudp(struct iodesc *d, void *pkt, size_t len, time_t tleft)
    153  1.1   thorpej {
    154  1.1   thorpej 	t_PXENV_UDP_READ *ur = (void *) pxe_command_buf;
    155  1.1   thorpej 	struct udphdr *uh;
    156  1.1   thorpej 	struct ip *ip;
    157  1.1   thorpej 
    158  1.1   thorpej 	uh = (struct udphdr *)pkt - 1;
    159  1.1   thorpej 	ip = (struct ip *)uh - 1;
    160  1.1   thorpej 
    161  1.1   thorpej 	bzero(ur, sizeof(*ur));
    162  1.1   thorpej 
    163  1.1   thorpej 	ur->dest_ip = d->myip.s_addr;
    164  1.1   thorpej 	ur->d_port = d->myport;
    165  1.1   thorpej 	ur->buffer_size = len;
    166  1.1   thorpej 	ur->buffer.segment = VTOPSEG(pkt);
    167  1.1   thorpej 	ur->buffer.offset = VTOPOFF(pkt);
    168  1.1   thorpej 
    169  1.1   thorpej 	/* XXX Timeout unused. */
    170  1.1   thorpej 
    171  1.1   thorpej 	pxe_call(PXENV_UDP_READ);
    172  1.1   thorpej 
    173  1.1   thorpej 	if (ur->status != PXENV_STATUS_SUCCESS) {
    174  1.1   thorpej 		/* XXX This happens a lot; it shouldn't. */
    175  1.1   thorpej 		if (ur->status != PXENV_STATUS_FAILURE)
    176  1.1   thorpej 			printf("readudp: PXENV_UDP_READ_failed: 0x%0x\n",
    177  1.1   thorpej 			    ur->status);
    178  1.1   thorpej 		return (-1);
    179  1.1   thorpej 	}
    180  1.1   thorpej 
    181  1.1   thorpej 	ip->ip_src.s_addr = ur->src_ip;
    182  1.1   thorpej 	uh->uh_sport = ur->s_port;
    183  1.1   thorpej 	uh->uh_dport = d->myport;
    184  1.1   thorpej 
    185  1.5  drochner 	return (ur->buffer_size);
    186  1.1   thorpej }
    187  1.1   thorpej 
    188  1.1   thorpej /*****************************************************************************
    189  1.1   thorpej  * PXE "netif" back-end
    190  1.1   thorpej  *****************************************************************************/
    191  1.1   thorpej 
    192  1.1   thorpej int	pxe_netif_match(struct netif *, void *);
    193  1.1   thorpej int	pxe_netif_probe(struct netif *, void *);
    194  1.1   thorpej void	pxe_netif_init(struct iodesc *, void *);
    195  1.1   thorpej int	pxe_netif_get(struct iodesc *, void *, size_t, time_t);
    196  1.1   thorpej int	pxe_netif_put(struct iodesc *, void *, size_t);
    197  1.1   thorpej void	pxe_netif_end(struct netif *);
    198  1.1   thorpej 
    199  1.1   thorpej struct netif_stats pxe_netif_stats;
    200  1.1   thorpej 
    201  1.1   thorpej struct netif_dif pxe0_dif = {
    202  1.1   thorpej 	0,			/* unit */
    203  1.1   thorpej 	1,			/* nsel */
    204  1.1   thorpej 	&pxe_netif_stats,
    205  1.1   thorpej 	0,
    206  1.1   thorpej 	0,
    207  1.1   thorpej };
    208  1.1   thorpej 
    209  1.1   thorpej struct netif_driver pxe_netif_driver = {
    210  1.1   thorpej 	"pxe",			/* netif_bname */
    211  1.1   thorpej 	pxe_netif_match,	/* match */
    212  1.1   thorpej 	pxe_netif_probe,	/* probe */
    213  1.1   thorpej 	pxe_netif_init,		/* init */
    214  1.1   thorpej 	pxe_netif_get,		/* get */
    215  1.1   thorpej 	pxe_netif_put,		/* put */
    216  1.1   thorpej 	pxe_netif_end,		/* end */
    217  1.1   thorpej 	&pxe0_dif,		/* netif_ifs */
    218  1.1   thorpej 	1,			/* netif_nifs */
    219  1.1   thorpej };
    220  1.1   thorpej 
    221  1.1   thorpej int
    222  1.1   thorpej pxe_netif_match(struct netif *nif, void *machdep_hint)
    223  1.1   thorpej {
    224  1.1   thorpej 
    225  1.1   thorpej 	return (1);
    226  1.1   thorpej }
    227  1.1   thorpej 
    228  1.1   thorpej int
    229  1.1   thorpej pxe_netif_probe(struct netif *nif, void *machdep_hint)
    230  1.1   thorpej {
    231  1.1   thorpej 	t_PXENV_UDP_OPEN *uo = (void *) pxe_command_buf;
    232  1.1   thorpej 
    233  1.2   thorpej 	pxe_init();
    234  1.2   thorpej 
    235  1.1   thorpej 	if (pxe_call == NULL)
    236  1.1   thorpej 		return (1);
    237  1.1   thorpej 
    238  1.1   thorpej 	bzero(uo, sizeof(*uo));
    239  1.1   thorpej 
    240  1.1   thorpej 	uo->src_ip = bootplayer.yip;
    241  1.1   thorpej 
    242  1.1   thorpej 	pxe_call(PXENV_UDP_OPEN);
    243  1.1   thorpej 
    244  1.1   thorpej 	if (uo->status != PXENV_STATUS_SUCCESS) {
    245  1.1   thorpej 		printf("pxe_netif_probe: PXENV_UDP_OPEN failed: 0x%x\n",
    246  1.1   thorpej 		    uo->status);
    247  1.2   thorpej 		pxe_fini();
    248  1.1   thorpej 		return (1);
    249  1.1   thorpej 	}
    250  1.1   thorpej 
    251  1.1   thorpej 	return (0);
    252  1.1   thorpej }
    253  1.1   thorpej 
    254  1.1   thorpej void
    255  1.1   thorpej pxe_netif_init(struct iodesc *desc, void *machdep_hint)
    256  1.1   thorpej {
    257  1.1   thorpej 	struct netif *nif = desc->io_netif;
    258  1.1   thorpej 
    259  1.1   thorpej 	bcopy(bootplayer.CAddr, desc->myea, ETHER_ADDR_LEN);
    260  1.1   thorpej 
    261  1.1   thorpej 	/*
    262  1.1   thorpej 	 * Since the PXE BIOS has already done DHCP, make sure we
    263  1.1   thorpej 	 * don't reuse any of its transaction IDs.
    264  1.1   thorpej 	 */
    265  1.1   thorpej 	desc->xid = bootplayer.ident;
    266  1.1   thorpej 
    267  1.1   thorpej 	printf("%s%d: Ethernet address %s\n", nif->nif_driver->netif_bname,
    268  1.1   thorpej 	    nif->nif_unit, ether_sprintf(desc->myea));
    269  1.1   thorpej }
    270  1.1   thorpej 
    271  1.1   thorpej int
    272  1.1   thorpej pxe_netif_put(struct iodesc *desc, void *pkt, size_t len)
    273  1.1   thorpej {
    274  1.1   thorpej 
    275  1.1   thorpej 	return (-1);
    276  1.1   thorpej }
    277  1.1   thorpej 
    278  1.1   thorpej int
    279  1.1   thorpej pxe_netif_get(struct iodesc *desc, void *pkt, size_t len, time_t tleft)
    280  1.1   thorpej {
    281  1.1   thorpej 
    282  1.1   thorpej 	return (-1);
    283  1.1   thorpej }
    284  1.1   thorpej 
    285  1.1   thorpej void
    286  1.1   thorpej pxe_netif_end(struct netif *nif)
    287  1.1   thorpej {
    288  1.1   thorpej 	t_PXENV_UDP_CLOSE *uc = (void *) pxe_command_buf;
    289  1.1   thorpej 
    290  1.1   thorpej 	uc->status = 0;
    291  1.1   thorpej 
    292  1.1   thorpej 	pxe_call(PXENV_UDP_CLOSE);
    293  1.1   thorpej 
    294  1.1   thorpej 	if (uc->status != PXENV_STATUS_SUCCESS)
    295  1.1   thorpej 		printf("pxe_netif_end: PXENV_UDP_CLOSE failed: 0x%x\n",
    296  1.1   thorpej 		    uc->status);
    297  1.2   thorpej 
    298  1.2   thorpej 	pxe_fini();
    299  1.1   thorpej }
    300  1.1   thorpej 
    301  1.1   thorpej /*****************************************************************************
    302  1.1   thorpej  * PXE initialization and support routines
    303  1.1   thorpej  *****************************************************************************/
    304  1.1   thorpej 
    305  1.1   thorpej u_int16_t pxe_command_buf_seg;
    306  1.1   thorpej u_int16_t pxe_command_buf_off;
    307  1.1   thorpej 
    308  1.1   thorpej extern u_int16_t bangpxe_off, bangpxe_seg;
    309  1.1   thorpej extern u_int16_t pxenv_off, pxenv_seg;
    310  1.1   thorpej 
    311  1.1   thorpej static struct btinfo_netif bi_netif;
    312  1.1   thorpej 
    313  1.1   thorpej int
    314  1.1   thorpej pxe_init(void)
    315  1.1   thorpej {
    316  1.1   thorpej 	t_PXENV_GET_CACHED_INFO *gci = (void *) pxe_command_buf;
    317  1.1   thorpej 	t_PXENV_UNDI_GET_NIC_TYPE *gnt = (void *) pxe_command_buf;
    318  1.1   thorpej 	pxenv_t *pxenv;
    319  1.1   thorpej 	pxe_t *pxe;
    320  1.1   thorpej 	char *cp;
    321  1.1   thorpej 	int i;
    322  1.1   thorpej 	u_int8_t cksum, *ucp;
    323  1.1   thorpej 
    324  1.1   thorpej 	/*
    325  1.1   thorpej 	 * Checking for the presence of PXE is a machine-dependent
    326  1.1   thorpej 	 * operation.  On the IA-32, this can be done two ways:
    327  1.1   thorpej 	 *
    328  1.1   thorpej 	 *	Int 0x1a function 0x5650
    329  1.1   thorpej 	 *
    330  1.1   thorpej 	 *	Scan memory for the !PXE or PXENV+ signatures
    331  1.1   thorpej 	 *
    332  1.1   thorpej 	 * We do the latter, since the Int method returns a pointer
    333  1.1   thorpej 	 * to a deprecated structure (PXENV+).
    334  1.1   thorpej 	 */
    335  1.1   thorpej 
    336  1.1   thorpej 	pxenv = NULL;
    337  1.1   thorpej 	pxe = NULL;
    338  1.1   thorpej 
    339  1.4   kanaoka 	for (cp = (char *)0xa0000; cp > (char *)0x10000; cp -= 2) {
    340  1.1   thorpej 		if (pxenv == NULL) {
    341  1.1   thorpej 			pxenv = (pxenv_t *)cp;
    342  1.1   thorpej 			if (bcmp(pxenv->Signature, S_SIZE("PXENV+")))
    343  1.1   thorpej 				pxenv = NULL;
    344  1.1   thorpej 			else {
    345  1.1   thorpej 				for (i = 0, ucp = (u_int8_t *)cp, cksum = 0;
    346  1.1   thorpej 				     i < pxenv->Length; i++)
    347  1.1   thorpej 					cksum += ucp[i];
    348  1.1   thorpej 				if (cksum != 0) {
    349  1.1   thorpej 					printf("pxe_init: bad cksum (0x%x) "
    350  1.1   thorpej 					    "for PXENV+ at 0x%lx\n", cksum,
    351  1.1   thorpej 					    (u_long) cp);
    352  1.1   thorpej 					pxenv = NULL;
    353  1.1   thorpej 				}
    354  1.1   thorpej 			}
    355  1.1   thorpej 		}
    356  1.1   thorpej 
    357  1.1   thorpej 		if (pxe == NULL) {
    358  1.1   thorpej 			pxe = (pxe_t *)cp;
    359  1.1   thorpej 			if (bcmp(pxe->Signature, S_SIZE("!PXE")))
    360  1.1   thorpej 				pxe = NULL;
    361  1.1   thorpej 			else {
    362  1.1   thorpej 				for (i = 0, ucp = (u_int8_t *)cp, cksum = 0;
    363  1.1   thorpej 				     i < pxe->StructLength; i++)
    364  1.1   thorpej 					cksum += ucp[i];
    365  1.1   thorpej 				if (cksum != 0) {
    366  1.1   thorpej 					printf("pxe_init: bad cksum (0x%x) "
    367  1.1   thorpej 					    "for !PXE at 0x%lx\n", cksum,
    368  1.1   thorpej 					    (u_long) cp);
    369  1.1   thorpej 					pxe = NULL;
    370  1.1   thorpej 				}
    371  1.1   thorpej 			}
    372  1.1   thorpej 		}
    373  1.1   thorpej 
    374  1.1   thorpej 		if (pxe != NULL && pxenv != NULL)
    375  1.1   thorpej 			break;
    376  1.1   thorpej 	}
    377  1.1   thorpej 
    378  1.1   thorpej 	if (pxe == NULL && pxenv == NULL) {
    379  1.1   thorpej 		printf("pxe_init: No PXE BIOS found.\n");
    380  1.1   thorpej 		return (1);
    381  1.1   thorpej 	}
    382  1.1   thorpej 
    383  1.1   thorpej 	if (pxenv != NULL) {
    384  1.1   thorpej 		printf("PXE BIOS Version %d.%d\n",
    385  1.1   thorpej 		    (pxenv->Version >> 8) & 0xff, pxenv->Version & 0xff);
    386  1.1   thorpej 		if (pxenv->Version >= 0x0201 && pxe != NULL) {
    387  1.1   thorpej 			/* 2.1 or greater -- don't use PXENV+ */
    388  1.1   thorpej 			pxenv = NULL;
    389  1.1   thorpej 		}
    390  1.1   thorpej 	}
    391  1.1   thorpej 
    392  1.1   thorpej 	if (pxe != NULL) {
    393  1.1   thorpej 		pxe_call = pxecall_bangpxe;
    394  1.1   thorpej 		bangpxe_off = pxe->EntryPointSP.offset;
    395  1.1   thorpej 		bangpxe_seg = pxe->EntryPointSP.segment;
    396  1.1   thorpej 	} else {
    397  1.1   thorpej 		pxe_call = pxecall_pxenv;
    398  1.1   thorpej 		pxenv_off = pxenv->RMEntry.offset;
    399  1.1   thorpej 		pxenv_seg = pxenv->RMEntry.segment;
    400  1.1   thorpej 	}
    401  1.1   thorpej 
    402  1.1   thorpej 	/*
    403  1.1   thorpej 	 * Pre-compute the segment/offset of the pxe_command_buf
    404  1.1   thorpej 	 * to make things nicer in the low-level calling glue.
    405  1.1   thorpej 	 */
    406  1.1   thorpej 	pxe_command_buf_seg = VTOPSEG(pxe_command_buf);
    407  1.1   thorpej 	pxe_command_buf_off = VTOPOFF(pxe_command_buf);
    408  1.1   thorpej 
    409  1.1   thorpej 	/*
    410  1.1   thorpej 	 * Get the cached info from the server's Discovery reply packet.
    411  1.1   thorpej 	 */
    412  1.1   thorpej 	bzero(gci, sizeof(*gci));
    413  1.1   thorpej 	gci->PacketType = PXENV_PACKET_TYPE_BINL_REPLY;
    414  1.1   thorpej 	pxe_call(PXENV_GET_CACHED_INFO);
    415  1.1   thorpej 	if (gci->Status != PXENV_STATUS_SUCCESS) {
    416  1.1   thorpej 		printf("pxe_init: PXENV_GET_CACHED_INFO failed: 0x%x\n",
    417  1.1   thorpej 		    gci->Status);
    418  1.1   thorpej 		return (1);
    419  1.1   thorpej 	}
    420  1.1   thorpej 	pvbcopy((void *)((gci->Buffer.segment << 4) + gci->Buffer.offset),
    421  1.1   thorpej 	    &bootplayer, gci->BufferSize);
    422  1.1   thorpej 
    423  1.1   thorpej 	/*
    424  1.1   thorpej 	 * Get network interface information.
    425  1.1   thorpej 	 */
    426  1.1   thorpej 	bzero(gnt, sizeof(*gnt));
    427  1.1   thorpej 	pxe_call(PXENV_UNDI_GET_NIC_TYPE);
    428  1.1   thorpej 
    429  1.1   thorpej 	if (gnt->Status != PXENV_STATUS_SUCCESS) {
    430  1.1   thorpej 		printf("pxe_init: PXENV_UNDI_GET_NIC_TYPE failed: 0x%x\n",
    431  1.1   thorpej 		    gnt->Status);
    432  1.1   thorpej 		return (0);
    433  1.1   thorpej 	}
    434  1.1   thorpej 
    435  1.1   thorpej 	switch (gnt->NicType) {
    436  1.1   thorpej 	case PCI_NIC:
    437  1.1   thorpej 	case CardBus_NIC:
    438  1.1   thorpej 		strncpy(bi_netif.ifname, "pxe", sizeof(bi_netif.ifname));
    439  1.1   thorpej 		bi_netif.bus = BI_BUS_PCI;
    440  1.1   thorpej 		bi_netif.addr.tag = gnt->info.pci.BusDevFunc;
    441  1.1   thorpej 
    442  1.1   thorpej 		BI_ADD(&bi_netif, BTINFO_NETIF, sizeof(bi_netif));
    443  1.1   thorpej 
    444  1.1   thorpej 		printf("Using %s device at bus %d device %d function %d\n",
    445  1.1   thorpej 		    gnt->NicType == PCI_NIC ? "PCI" : "CardBus",
    446  1.1   thorpej 		    (gnt->info.pci.BusDevFunc >> 8) & 0xff,
    447  1.1   thorpej 		    (gnt->info.pci.BusDevFunc >> 3) & 0x1f,
    448  1.1   thorpej 		    gnt->info.pci.BusDevFunc & 0x7);
    449  1.1   thorpej 		break;
    450  1.1   thorpej 
    451  1.1   thorpej 	case PnP_NIC:
    452  1.1   thorpej 		/* XXX Make bootinfo work with this. */
    453  1.1   thorpej 		printf("Using PnP device at 0x%x\n", gnt->info.pnp.CardSelNum);
    454  1.1   thorpej 	}
    455  1.1   thorpej 
    456  1.1   thorpej 	return (0);
    457  1.1   thorpej }
    458  1.1   thorpej 
    459  1.1   thorpej void
    460  1.1   thorpej pxe_fini(void)
    461  1.1   thorpej {
    462  1.1   thorpej 	t_PXENV_UNLOAD_STACK *unload = (void *) pxe_command_buf;
    463  1.1   thorpej 	t_PXENV_UNDI_SHUTDOWN *shutdown = (void *) pxe_command_buf;
    464  1.1   thorpej 
    465  1.1   thorpej 	if (pxe_call == NULL)
    466  1.1   thorpej 		return;
    467  1.1   thorpej 
    468  1.1   thorpej 	pxe_call(PXENV_UNDI_SHUTDOWN);
    469  1.1   thorpej 
    470  1.1   thorpej 	if (shutdown->Status != PXENV_STATUS_SUCCESS)
    471  1.1   thorpej 		printf("pxe_fini: PXENV_UNDI_SHUTDOWN failed: 0x%x\n",
    472  1.1   thorpej 		    shutdown->Status);
    473  1.1   thorpej 
    474  1.1   thorpej 	pxe_call(PXENV_UNLOAD_STACK);
    475  1.1   thorpej 
    476  1.1   thorpej 	if (unload->Status != PXENV_STATUS_SUCCESS)
    477  1.1   thorpej 		printf("pxe_fini: PXENV_UNLOAD_STACK failed: 0x%x\n",
    478  1.1   thorpej 		    unload->Status);
    479  1.1   thorpej }
    480