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