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