Home | History | Annotate | Line # | Download | only in netboot
if_prom.c revision 1.7
      1 /*	$NetBSD: if_prom.c,v 1.7 1997/01/18 01:49:29 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
      5  * Copyright (c) 1993 Adam Glass
      6  * All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by Adam Glass.
     19  * 4. The name of the Author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
     23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32  * SUCH DAMAGE.
     33  */
     34 
     35 #include <sys/param.h>
     36 #include <sys/types.h>
     37 
     38 #include <netinet/in.h>
     39 #include <netinet/in_systm.h>
     40 
     41 #include <lib/libsa/netif.h>
     42 #include "include/prom.h"
     43 #include <lib/libkern/libkern.h>
     44 
     45 #include "../bbinfo.h"
     46 
     47 int prom_probe();
     48 int prom_match();
     49 void prom_init();
     50 int prom_get();
     51 int prom_put();
     52 void prom_end();
     53 
     54 extern struct netif_stats	prom_stats[];
     55 
     56 struct netif_dif prom_ifs[] = {
     57 /*	dif_unit	dif_nsel	dif_stats	dif_private	*/
     58 {	0,		1,		&prom_stats[0],	0,		},
     59 };
     60 
     61 struct netif_stats prom_stats[NENTS(prom_ifs)];
     62 
     63 struct netbbinfo netbbinfo = {
     64 	0xfeedbabedeadbeef,			/* magic number */
     65 	0,					/* set */
     66 	0, 0, 0, 0, 0, 0,			/* ether address */
     67 	0,					/* force */
     68 	{ 0, },					/* pad2 */
     69 	0,					/* cksum */
     70 	0xfeedbeefdeadbabe,			/* magic number */
     71 };
     72 
     73 struct netif_driver prom_netif_driver = {
     74 	"prom",			/* netif_bname */
     75 	prom_match,		/* netif_match */
     76 	prom_probe,		/* netif_probe */
     77 	prom_init,		/* netif_init */
     78 	prom_get,		/* netif_get */
     79 	prom_put,		/* netif_put */
     80 	prom_end,		/* netif_end */
     81 	prom_ifs,		/* netif_ifs */
     82 	NENTS(prom_ifs)		/* netif_nifs */
     83 };
     84 
     85 int netfd, broken_firmware;
     86 
     87 int
     88 prom_match(nif, machdep_hint)
     89 	struct netif *nif;
     90 	void *machdep_hint;
     91 {
     92 
     93 	return (1);
     94 }
     95 
     96 int
     97 prom_probe(nif, machdep_hint)
     98 	struct netif *nif;
     99 	void *machdep_hint;
    100 {
    101 
    102 	return 0;
    103 }
    104 
    105 int
    106 prom_put(desc, pkt, len)
    107 	struct iodesc *desc;
    108 	void *pkt;
    109 	int len;
    110 {
    111 
    112 	prom_write(netfd, len, pkt, 0);
    113 
    114 	return len;
    115 }
    116 
    117 
    118 int
    119 prom_get(desc, pkt, len, timeout)
    120 	struct iodesc *desc;
    121 	void *pkt;
    122 	int len;
    123 	time_t timeout;
    124 {
    125 	prom_return_t ret;
    126 	time_t t;
    127 	int cc;
    128 	char hate[2000];
    129 
    130 	t = getsecs();
    131 	cc = 0;
    132 	while (((getsecs() - t) < timeout) && !cc) {
    133 		if (broken_firmware)
    134 			ret.bits = prom_read(netfd, 0, hate, 0);
    135 		else
    136 			ret.bits = prom_read(netfd, sizeof hate, hate, 0);
    137 		if (ret.u.status == 0)
    138 			cc = ret.u.retval;
    139 	}
    140 	if (broken_firmware)
    141 		cc = min(cc, len);
    142 	else
    143 		cc = len;
    144 	bcopy(hate, pkt, cc);
    145 
    146 	return cc;
    147 }
    148 
    149 extern char *strchr();
    150 
    151 void
    152 prom_init(desc, machdep_hint)
    153 	struct iodesc *desc;
    154 	void *machdep_hint;
    155 {
    156 	prom_return_t ret;
    157 	char devname[64];
    158 	int devlen, i, netbbinfovalid;
    159 	char *enet_addr;
    160 	u_int64_t *qp, csum;
    161 
    162 	broken_firmware = 0;
    163 
    164 	csum = 0;
    165 	for (i = 0, qp = (u_int64_t *)&netbbinfo;
    166 	    i < (sizeof netbbinfo / sizeof (u_int64_t)); i++, qp++)
    167 		csum += *qp;
    168 	netbbinfovalid = (csum == 0);
    169 	if (netbbinfovalid)
    170 		netbbinfovalid = netbbinfo.set;
    171 
    172 #if 0
    173 	printf("netbbinfo ");
    174 	if (!netbbinfovalid)
    175 		printf("invalid\n");
    176 	else
    177 		printf("valid: force = %d, ea = %s\n", netbbinfo.force,
    178 		    ether_sprintf(netbbinfo.ether_addr));
    179 #endif
    180 
    181 	ret.bits = prom_getenv(PROM_E_BOOTED_DEV, devname, sizeof(devname));
    182 	devlen = ret.u.retval;
    183 
    184 	/* Ethernet address is the 9th component of the booted_dev string. */
    185 	enet_addr = devname;
    186 	for (i = 0; i < 8; i++) {
    187 		enet_addr = strchr(enet_addr, ' ');
    188 		if (enet_addr == NULL) {
    189 			printf("boot: boot device name does not contain ethernet address.\n");
    190 			goto punt;
    191 		}
    192 		enet_addr++;
    193 	}
    194 	if (enet_addr != NULL) {
    195 		int hv, lv;
    196 
    197 #define	dval(c)	(((c) >= '0' && (c) <= '9') ? ((c) - '0') : \
    198 		 (((c) >= 'A' && (c) <= 'F') ? (10 + (c) - 'A') : \
    199 		  (((c) >= 'a' && (c) <= 'f') ? (10 + (c) - 'a') : -1)))
    200 
    201 		for (i = 0; i < 6; i++) {
    202 			hv = dval(*enet_addr); enet_addr++;
    203 			lv = dval(*enet_addr); enet_addr++;
    204 			enet_addr++;
    205 
    206 			if (hv == -1 || lv == -1) {
    207 				printf("boot: boot device name contains bogus ethernet address.\n");
    208 				goto punt;
    209 			}
    210 
    211 			desc->myea[i] = (hv << 4) | lv;
    212 		}
    213 #undef dval
    214 	}
    215 
    216 	if (netbbinfovalid && netbbinfo.force) {
    217 		printf("boot: using hard-coded ethernet address (forced).\n");
    218 		bcopy(netbbinfo.ether_addr, desc->myea, sizeof desc->myea);
    219 	}
    220 
    221 gotit:
    222 	printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea));
    223 
    224 	ret.bits = prom_open(devname, devlen + 1);
    225 	if (ret.u.status) {
    226 		printf("prom_init: open failed: %d\n", ret.u.status);
    227 		goto reallypunt;
    228 	}
    229 	netfd = ret.u.retval;
    230 	return;
    231 
    232 punt:
    233 	broken_firmware = 1;
    234         if (netbbinfovalid) {
    235                 printf("boot: using hard-coded ethernet address.\n");
    236                 bcopy(netbbinfo.ether_addr, desc->myea, sizeof desc->myea);
    237                 goto gotit;
    238         }
    239 
    240 reallypunt:
    241 	printf("\n");
    242 	printf("Boot device name was: \"%s\"\n", devname);
    243 	printf("\n");
    244 	printf("Your firmware may be too old to network-boot NetBSD/Alpha,\n");
    245 	printf("or you might have to hard-code an ethernet address into\n");
    246 	printf("your network boot block with setnetbootinfo(8).\n");
    247 	halt();
    248 }
    249 
    250 void
    251 prom_end(nif)
    252 	struct netif *nif;
    253 {
    254 
    255 	prom_close(netfd);
    256 }
    257