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