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