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