xref: /netbsd/sys/arch/alpha/stand/netboot/if_prom.c (revision a8a5c538)
1*a8a5c538Sriastradh /* $NetBSD: if_prom.c,v 1.21 2018/09/03 16:29:22 riastradh Exp $ */
28644dce0Scgd 
38644dce0Scgd /*
4e2bd92c0Scgd  * Copyright (c) 1997 Christopher G. Demetriou.  All rights reserved.
58644dce0Scgd  * Copyright (c) 1993 Adam Glass
68644dce0Scgd  * All rights reserved.
78644dce0Scgd  *
88644dce0Scgd  * Redistribution and use in source and binary forms, with or without
98644dce0Scgd  * modification, are permitted provided that the following conditions
108644dce0Scgd  * are met:
118644dce0Scgd  * 1. Redistributions of source code must retain the above copyright
128644dce0Scgd  *    notice, this list of conditions and the following disclaimer.
138644dce0Scgd  * 2. Redistributions in binary form must reproduce the above copyright
148644dce0Scgd  *    notice, this list of conditions and the following disclaimer in the
158644dce0Scgd  *    documentation and/or other materials provided with the distribution.
168644dce0Scgd  * 3. All advertising materials mentioning features or use of this software
178644dce0Scgd  *    must display the following acknowledgement:
188644dce0Scgd  *	This product includes software developed by Adam Glass.
198644dce0Scgd  * 4. The name of the Author may not be used to endorse or promote products
208644dce0Scgd  *    derived from this software without specific prior written permission.
218644dce0Scgd  *
228644dce0Scgd  * THIS SOFTWARE IS PROVIDED BY Adam Glass ``AS IS'' AND
238644dce0Scgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
248644dce0Scgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
258644dce0Scgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
268644dce0Scgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
278644dce0Scgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
288644dce0Scgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
298644dce0Scgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
308644dce0Scgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
318644dce0Scgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
328644dce0Scgd  * SUCH DAMAGE.
338644dce0Scgd  */
348644dce0Scgd 
358644dce0Scgd #include <sys/param.h>
368644dce0Scgd #include <sys/types.h>
378644dce0Scgd 
388644dce0Scgd #include <netinet/in.h>
398644dce0Scgd #include <netinet/in_systm.h>
408644dce0Scgd 
4128139858Sdrochner #include <lib/libsa/stand.h>
4228139858Sdrochner #include <lib/libsa/net.h>
43653e0e50Scgd #include <lib/libsa/netif.h>
4428139858Sdrochner #include <machine/prom.h>
45728d6723Sthorpej #include <lib/libkern/libkern.h>
468644dce0Scgd 
4728139858Sdrochner #include "stand/common/common.h"
4821ad9905Scgd #include "stand/common/bbinfo.h"
49e2bd92c0Scgd 
508644dce0Scgd extern struct netif_stats	prom_stats[];
518644dce0Scgd 
528644dce0Scgd struct netif_dif prom_ifs[] = {
538644dce0Scgd /*	dif_unit	dif_nsel	dif_stats	dif_private	*/
548644dce0Scgd {	0,		1,		&prom_stats[0],	0,		},
558644dce0Scgd };
567d2a35f7Sdrochner #define NPROM_IFS (sizeof(prom_ifs) / sizeof(prom_ifs[0]))
578644dce0Scgd 
587d2a35f7Sdrochner struct netif_stats prom_stats[NPROM_IFS];
598644dce0Scgd 
60e2bd92c0Scgd struct netbbinfo netbbinfo = {
61e2bd92c0Scgd 	0xfeedbabedeadbeef,			/* magic number */
62e2bd92c0Scgd 	0,					/* set */
6328139858Sdrochner 	{0, 0, 0, 0, 0, 0},			/* ether address */
64e2bd92c0Scgd 	0,					/* force */
65e2bd92c0Scgd 	{ 0, },					/* pad2 */
66e2bd92c0Scgd 	0,					/* cksum */
67e2bd92c0Scgd 	0xfeedbeefdeadbabe,			/* magic number */
68e2bd92c0Scgd };
69e2bd92c0Scgd 
70f0bdf503Scgd int broken_firmware;
718644dce0Scgd 
726774aca1Sthorpej static int
prom_match(struct netif * nif,void * machdep_hint)736774aca1Sthorpej prom_match(struct netif *nif, void *machdep_hint)
748644dce0Scgd {
758644dce0Scgd 
768644dce0Scgd 	return (1);
778644dce0Scgd }
788644dce0Scgd 
796774aca1Sthorpej static int
prom_probe(struct netif * nif,void * machdep_hint)806774aca1Sthorpej prom_probe(struct netif *nif, void *machdep_hint)
818644dce0Scgd {
828644dce0Scgd 
838644dce0Scgd 	return 0;
848644dce0Scgd }
858644dce0Scgd 
866774aca1Sthorpej static int
prom_put(struct iodesc * desc,void * pkt,size_t len)876774aca1Sthorpej prom_put(struct iodesc *desc, void *pkt, size_t len)
888644dce0Scgd {
898644dce0Scgd 
90f0bdf503Scgd 	prom_write(booted_dev_fd, len, pkt, 0);
918644dce0Scgd 
928644dce0Scgd 	return len;
938644dce0Scgd }
948644dce0Scgd 
956774aca1Sthorpej static int
prom_get(struct iodesc * desc,void * pkt,size_t len,saseconds_t timeout)9669cf32a7Stsutsui prom_get(struct iodesc *desc, void *pkt, size_t len, saseconds_t timeout)
978644dce0Scgd {
988644dce0Scgd 	prom_return_t ret;
9969cf32a7Stsutsui 	satime_t t;
1008644dce0Scgd 	int cc;
1018644dce0Scgd 	char hate[2000];
1028644dce0Scgd 
1038644dce0Scgd 	t = getsecs();
1048644dce0Scgd 	cc = 0;
1058644dce0Scgd 	while (((getsecs() - t) < timeout) && !cc) {
106e2bd92c0Scgd 		if (broken_firmware)
107f0bdf503Scgd 			ret.bits = prom_read(booted_dev_fd, 0, hate, 0);
108e2bd92c0Scgd 		else
109f0bdf503Scgd 			ret.bits = prom_read(booted_dev_fd, sizeof hate, hate, 0);
1108644dce0Scgd 		if (ret.u.status == 0)
111e2bd92c0Scgd 			cc = ret.u.retval;
1128644dce0Scgd 	}
113e2bd92c0Scgd 	if (broken_firmware)
114*a8a5c538Sriastradh 		cc = uimin(cc, len);
115e2bd92c0Scgd 	else
1168644dce0Scgd 		cc = len;
117684d06baSwiz 	memcpy(pkt, hate, cc);
1188644dce0Scgd 
1198644dce0Scgd 	return cc;
1208644dce0Scgd }
1218644dce0Scgd 
1226774aca1Sthorpej static void
prom_init(struct iodesc * desc,void * machdep_hint)1236774aca1Sthorpej prom_init(struct iodesc *desc, void *machdep_hint)
1248644dce0Scgd {
125f0bdf503Scgd 	int i, netbbinfovalid;
126f0bdf503Scgd 	const char *enet_addr;
127e2bd92c0Scgd 	u_int64_t *qp, csum;
128e2bd92c0Scgd 
129e2bd92c0Scgd 	broken_firmware = 0;
130e2bd92c0Scgd 
131e2bd92c0Scgd 	csum = 0;
132e2bd92c0Scgd 	for (i = 0, qp = (u_int64_t *)&netbbinfo;
133e2bd92c0Scgd 	    i < (sizeof netbbinfo / sizeof (u_int64_t)); i++, qp++)
134e2bd92c0Scgd 		csum += *qp;
135e2bd92c0Scgd 	netbbinfovalid = (csum == 0);
136e2bd92c0Scgd 	if (netbbinfovalid)
137e2bd92c0Scgd 		netbbinfovalid = netbbinfo.set;
138e2bd92c0Scgd 
139e2bd92c0Scgd #if 0
140e2bd92c0Scgd 	printf("netbbinfo ");
141e2bd92c0Scgd 	if (!netbbinfovalid)
142e2bd92c0Scgd 		printf("invalid\n");
143e2bd92c0Scgd 	else
144e2bd92c0Scgd 		printf("valid: force = %d, ea = %s\n", netbbinfo.force,
145e2bd92c0Scgd 		    ether_sprintf(netbbinfo.ether_addr));
146e2bd92c0Scgd #endif
1478644dce0Scgd 
1483bd3e625Scgd 	/* Ethernet address is the 9th component of the booted_dev string. */
149f0bdf503Scgd 	enet_addr = booted_dev_name;
1503bd3e625Scgd 	for (i = 0; i < 8; i++) {
1513bd3e625Scgd 		enet_addr = strchr(enet_addr, ' ');
1523bd3e625Scgd 		if (enet_addr == NULL) {
153f6409bfaScgd 			printf("boot: boot device name does not contain ethernet address.\n");
1540783bd28Scgd 			goto punt;
1553bd3e625Scgd 		}
1563bd3e625Scgd 		enet_addr++;
1573bd3e625Scgd 	}
1583bd3e625Scgd 	if (enet_addr != NULL) {
1593bd3e625Scgd 		int hv, lv;
1603bd3e625Scgd 
1613bd3e625Scgd #define	dval(c)	(((c) >= '0' && (c) <= '9') ? ((c) - '0') : \
1623bd3e625Scgd 		 (((c) >= 'A' && (c) <= 'F') ? (10 + (c) - 'A') : \
1633bd3e625Scgd 		  (((c) >= 'a' && (c) <= 'f') ? (10 + (c) - 'a') : -1)))
1643bd3e625Scgd 
1653bd3e625Scgd 		for (i = 0; i < 6; i++) {
1663bd3e625Scgd 			hv = dval(*enet_addr); enet_addr++;
1673bd3e625Scgd 			lv = dval(*enet_addr); enet_addr++;
1683bd3e625Scgd 			enet_addr++;
1693bd3e625Scgd 
1703bd3e625Scgd 			if (hv == -1 || lv == -1) {
171f6409bfaScgd 				printf("boot: boot device name contains bogus ethernet address.\n");
1720783bd28Scgd 				goto punt;
1733bd3e625Scgd 			}
1743bd3e625Scgd 
1753bd3e625Scgd 			desc->myea[i] = (hv << 4) | lv;
1763bd3e625Scgd 		}
1773bd3e625Scgd #undef dval
1783bd3e625Scgd 	}
1793bd3e625Scgd 
180e2bd92c0Scgd 	if (netbbinfovalid && netbbinfo.force) {
181f6409bfaScgd 		printf("boot: using hard-coded ethernet address (forced).\n");
182684d06baSwiz 		memcpy(desc->myea, netbbinfo.ether_addr, sizeof desc->myea);
183e2bd92c0Scgd 	}
184e2bd92c0Scgd 
185e2bd92c0Scgd gotit:
1860783bd28Scgd 	printf("boot: ethernet address: %s\n", ether_sprintf(desc->myea));
1870783bd28Scgd 	return;
1888644dce0Scgd 
1890783bd28Scgd punt:
190e2bd92c0Scgd 	broken_firmware = 1;
191e2bd92c0Scgd         if (netbbinfovalid) {
192f6409bfaScgd                 printf("boot: using hard-coded ethernet address.\n");
193684d06baSwiz                 memcpy(desc->myea, netbbinfo.ether_addr, sizeof desc->myea);
194e2bd92c0Scgd                 goto gotit;
195e2bd92c0Scgd         }
196e2bd92c0Scgd 
197f6409bfaScgd 	printf("\n");
198f0bdf503Scgd 	printf("Boot device name was: \"%s\"\n", booted_dev_name);
1990783bd28Scgd 	printf("\n");
2005dd4815bScgd 	printf("Your firmware may be too old to network-boot NetBSD/alpha,\n");
201e2bd92c0Scgd 	printf("or you might have to hard-code an ethernet address into\n");
202e2bd92c0Scgd 	printf("your network boot block with setnetbootinfo(8).\n");
203f0bdf503Scgd 
204f0bdf503Scgd 	booted_dev_close();
2050783bd28Scgd 	halt();
2068644dce0Scgd }
2078644dce0Scgd 
2086774aca1Sthorpej static void
prom_end(struct netif * nif)2096774aca1Sthorpej prom_end(struct netif *nif)
2108644dce0Scgd {
2118644dce0Scgd 
212f0bdf503Scgd 	/* nothing to do */
2135bc91073Sross }
2146774aca1Sthorpej 
2156774aca1Sthorpej struct netif_driver prom_netif_driver = {
2166774aca1Sthorpej 	"prom",			/* netif_bname */
2176774aca1Sthorpej 	prom_match,		/* netif_match */
2186774aca1Sthorpej 	prom_probe,		/* netif_probe */
2196774aca1Sthorpej 	prom_init,		/* netif_init */
2206774aca1Sthorpej 	prom_get,		/* netif_get */
2216774aca1Sthorpej 	prom_put,		/* netif_put */
2226774aca1Sthorpej 	prom_end,		/* netif_end */
2236774aca1Sthorpej 	prom_ifs,		/* netif_ifs */
2247d2a35f7Sdrochner 	NPROM_IFS		/* netif_nifs */
2256774aca1Sthorpej };
226