Home | History | Annotate | Line # | Download | only in netboot
if_prom.c revision 1.19.108.1
      1 /* $NetBSD: if_prom.c,v 1.19.108.1 2009/05/04 08:10:31 yamt 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/stand.h>
     42 #include <lib/libsa/net.h>
     43 #include <lib/libsa/netif.h>
     44 #include <machine/prom.h>
     45 #include <lib/libkern/libkern.h>
     46 
     47 #include "stand/common/common.h"
     48 #include "stand/common/bbinfo.h"
     49 
     50 extern struct netif_stats	prom_stats[];
     51 
     52 struct netif_dif prom_ifs[] = {
     53 /*	dif_unit	dif_nsel	dif_stats	dif_private	*/
     54 {	0,		1,		&prom_stats[0],	0,		},
     55 };
     56 #define NPROM_IFS (sizeof(prom_ifs) / sizeof(prom_ifs[0]))
     57 
     58 struct netif_stats prom_stats[NPROM_IFS];
     59 
     60 struct netbbinfo netbbinfo = {
     61 	0xfeedbabedeadbeef,			/* magic number */
     62 	0,					/* set */
     63 	{0, 0, 0, 0, 0, 0},			/* ether address */
     64 	0,					/* force */
     65 	{ 0, },					/* pad2 */
     66 	0,					/* cksum */
     67 	0xfeedbeefdeadbabe,			/* magic number */
     68 };
     69 
     70 int broken_firmware;
     71 
     72 static int
     73 prom_match(struct netif *nif, void *machdep_hint)
     74 {
     75 
     76 	return (1);
     77 }
     78 
     79 static int
     80 prom_probe(struct netif *nif, void *machdep_hint)
     81 {
     82 
     83 	return 0;
     84 }
     85 
     86 static int
     87 prom_put(struct iodesc *desc, void *pkt, size_t len)
     88 {
     89 
     90 	prom_write(booted_dev_fd, len, pkt, 0);
     91 
     92 	return len;
     93 }
     94 
     95 static int
     96 prom_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
     97 {
     98 	prom_return_t ret;
     99 	satime_t t;
    100 	int cc;
    101 	char hate[2000];
    102 
    103 	t = getsecs();
    104 	cc = 0;
    105 	while (((getsecs() - t) < timeout) && !cc) {
    106 		if (broken_firmware)
    107 			ret.bits = prom_read(booted_dev_fd, 0, hate, 0);
    108 		else
    109 			ret.bits = prom_read(booted_dev_fd, sizeof hate, hate, 0);
    110 		if (ret.u.status == 0)
    111 			cc = ret.u.retval;
    112 	}
    113 	if (broken_firmware)
    114 		cc = min(cc, len);
    115 	else
    116 		cc = len;
    117 	memcpy(pkt, hate, cc);
    118 
    119 	return cc;
    120 }
    121 
    122 static void
    123 prom_init(struct iodesc *desc, void *machdep_hint)
    124 {
    125 	int i, netbbinfovalid;
    126 	const char *enet_addr;
    127 	u_int64_t *qp, csum;
    128 
    129 	broken_firmware = 0;
    130 
    131 	csum = 0;
    132 	for (i = 0, qp = (u_int64_t *)&netbbinfo;
    133 	    i < (sizeof netbbinfo / sizeof (u_int64_t)); i++, qp++)
    134 		csum += *qp;
    135 	netbbinfovalid = (csum == 0);
    136 	if (netbbinfovalid)
    137 		netbbinfovalid = netbbinfo.set;
    138 
    139 #if 0
    140 	printf("netbbinfo ");
    141 	if (!netbbinfovalid)
    142 		printf("invalid\n");
    143 	else
    144 		printf("valid: force = %d, ea = %s\n", netbbinfo.force,
    145 		    ether_sprintf(netbbinfo.ether_addr));
    146 #endif
    147 
    148 	/* Ethernet address is the 9th component of the booted_dev string. */
    149 	enet_addr = booted_dev_name;
    150 	for (i = 0; i < 8; i++) {
    151 		enet_addr = strchr(enet_addr, ' ');
    152 		if (enet_addr == NULL) {
    153 			printf("boot: boot device name does not contain ethernet address.\n");
    154 			goto punt;
    155 		}
    156 		enet_addr++;
    157 	}
    158 	if (enet_addr != NULL) {
    159 		int hv, lv;
    160 
    161 #define	dval(c)	(((c) >= '0' && (c) <= '9') ? ((c) - '0') : \
    162 		 (((c) >= 'A' && (c) <= 'F') ? (10 + (c) - 'A') : \
    163 		  (((c) >= 'a' && (c) <= 'f') ? (10 + (c) - 'a') : -1)))
    164 
    165 		for (i = 0; i < 6; i++) {
    166 			hv = dval(*enet_addr); enet_addr++;
    167 			lv = dval(*enet_addr); enet_addr++;
    168 			enet_addr++;
    169 
    170 			if (hv == -1 || lv == -1) {
    171 				printf("boot: boot device name contains bogus ethernet address.\n");
    172 				goto punt;
    173 			}
    174 
    175 			desc->myea[i] = (hv << 4) | lv;
    176 		}
    177 #undef dval
    178 	}
    179 
    180 	if (netbbinfovalid && netbbinfo.force) {
    181 		printf("boot: using hard-coded ethernet address (forced).\n");
    182 		memcpy(desc->myea, netbbinfo.ether_addr, sizeof desc->myea);
    183 	}
    184 
    185 gotit:
    186 	printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea));
    187 	return;
    188 
    189 punt:
    190 	broken_firmware = 1;
    191         if (netbbinfovalid) {
    192                 printf("boot: using hard-coded ethernet address.\n");
    193                 memcpy(desc->myea, netbbinfo.ether_addr, sizeof desc->myea);
    194                 goto gotit;
    195         }
    196 
    197 	printf("\n");
    198 	printf("Boot device name was: \"%s\"\n", booted_dev_name);
    199 	printf("\n");
    200 	printf("Your firmware may be too old to network-boot NetBSD/alpha,\n");
    201 	printf("or you might have to hard-code an ethernet address into\n");
    202 	printf("your network boot block with setnetbootinfo(8).\n");
    203 
    204 	booted_dev_close();
    205 	halt();
    206 }
    207 
    208 static void
    209 prom_end(struct netif *nif)
    210 {
    211 
    212 	/* nothing to do */
    213 }
    214 
    215 struct netif_driver prom_netif_driver = {
    216 	"prom",			/* netif_bname */
    217 	prom_match,		/* netif_match */
    218 	prom_probe,		/* netif_probe */
    219 	prom_init,		/* netif_init */
    220 	prom_get,		/* netif_get */
    221 	prom_put,		/* netif_put */
    222 	prom_end,		/* netif_end */
    223 	prom_ifs,		/* netif_ifs */
    224 	NPROM_IFS		/* netif_nifs */
    225 };
    226