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