autoconf.c revision 1.58 1 1.58 thorpej /* $NetBSD: autoconf.c,v 1.58 2021/07/09 01:29:20 thorpej Exp $ */
2 1.1 cgd
3 1.1 cgd /*
4 1.4 cgd * Copyright (c) 1992, 1993
5 1.4 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.4 cgd * This software was developed by the Computer Systems Engineering group
8 1.4 cgd * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.4 cgd * contributed to Berkeley.
10 1.1 cgd *
11 1.4 cgd * All advertising materials mentioning features or use of this software
12 1.4 cgd * must display the following acknowledgement:
13 1.4 cgd * This product includes software developed by the University of
14 1.4 cgd * California, Lawrence Berkeley Laboratory.
15 1.4 cgd *
16 1.4 cgd * Redistribution and use in source and binary forms, with or without
17 1.4 cgd * modification, are permitted provided that the following conditions
18 1.4 cgd * are met:
19 1.4 cgd * 1. Redistributions of source code must retain the above copyright
20 1.4 cgd * notice, this list of conditions and the following disclaimer.
21 1.4 cgd * 2. Redistributions in binary form must reproduce the above copyright
22 1.4 cgd * notice, this list of conditions and the following disclaimer in the
23 1.4 cgd * documentation and/or other materials provided with the distribution.
24 1.40 agc * 3. Neither the name of the University nor the names of its contributors
25 1.4 cgd * may be used to endorse or promote products derived from this software
26 1.4 cgd * without specific prior written permission.
27 1.4 cgd *
28 1.4 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.4 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.4 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.4 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.4 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.4 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.4 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.4 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.4 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.4 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.4 cgd * SUCH DAMAGE.
39 1.4 cgd *
40 1.4 cgd * @(#)autoconf.c 8.4 (Berkeley) 10/1/93
41 1.1 cgd */
42 1.23 cgd
43 1.24 cgd #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
44 1.24 cgd
45 1.58 thorpej __KERNEL_RCSID(0, "$NetBSD: autoconf.c,v 1.58 2021/07/09 01:29:20 thorpej Exp $");
46 1.53 tsutsui
47 1.53 tsutsui #include "pci.h"
48 1.1 cgd
49 1.1 cgd #include <sys/param.h>
50 1.1 cgd #include <sys/systm.h>
51 1.1 cgd #include <sys/buf.h>
52 1.1 cgd #include <sys/disklabel.h>
53 1.1 cgd #include <sys/reboot.h>
54 1.1 cgd #include <sys/device.h>
55 1.39 gehenna #include <sys/conf.h>
56 1.16 cgd #include <dev/cons.h>
57 1.1 cgd
58 1.53 tsutsui #include <dev/pci/pcivar.h>
59 1.53 tsutsui
60 1.58 thorpej #include <net/if.h>
61 1.58 thorpej #include <net/if_ether.h>
62 1.58 thorpej
63 1.1 cgd #include <machine/autoconf.h>
64 1.32 mjacob #include <machine/alpha.h>
65 1.31 thorpej #include <machine/cpu.h>
66 1.5 cgd #include <machine/prom.h>
67 1.39 gehenna #include <machine/cpuconf.h>
68 1.37 thorpej #include <machine/intr.h>
69 1.5 cgd
70 1.5 cgd struct bootdev_data *bootdev_data;
71 1.5 cgd
72 1.58 thorpej static void parse_prom_bootdev(void);
73 1.1 cgd
74 1.1 cgd /*
75 1.35 thorpej * cpu_configure:
76 1.1 cgd * called at boot time, configure all devices on system
77 1.1 cgd */
78 1.1 cgd void
79 1.47 cegger cpu_configure(void)
80 1.1 cgd {
81 1.1 cgd
82 1.5 cgd parse_prom_bootdev();
83 1.5 cgd
84 1.25 cgd /*
85 1.25 cgd * Disable interrupts during autoconfiguration. splhigh() won't
86 1.25 cgd * work, because it simply _raises_ the IPL, so if machine checks
87 1.25 cgd * are disabled, they'll stay disabled. Machine checks are needed
88 1.25 cgd * during autoconfig.
89 1.25 cgd */
90 1.25 cgd (void)alpha_pal_swpipl(ALPHA_PSL_IPL_HIGH);
91 1.42 drochner if (config_rootfound("mainbus", NULL) == NULL)
92 1.1 cgd panic("no mainbus found");
93 1.1 cgd (void)spl0();
94 1.31 thorpej
95 1.31 thorpej /*
96 1.51 matt * Note that bootstrapping is finished, and set the HWRPB up
97 1.31 thorpej * to do restarts.
98 1.31 thorpej */
99 1.31 thorpej hwrpb_restart_setup();
100 1.20 gwr }
101 1.20 gwr
102 1.55 thorpej static void
103 1.55 thorpej qemu_find_rootdev(void)
104 1.55 thorpej {
105 1.56 thorpej char buf[32] = { 0 };
106 1.55 thorpej
107 1.55 thorpej /*
108 1.55 thorpej * Check for "rootdev=wd0".
109 1.55 thorpej */
110 1.56 thorpej if (! prom_qemu_getenv("rootdev", buf, sizeof(buf))) {
111 1.56 thorpej /*
112 1.56 thorpej * Check "root=/dev/wd0a", "root=/dev/hda1", etc.
113 1.56 thorpej */
114 1.56 thorpej if (! prom_qemu_getenv("root", buf, sizeof(buf))) {
115 1.56 thorpej printf("WARNING: no rootdev= or root= arguments "
116 1.56 thorpej "provided by Qemu\n");
117 1.56 thorpej return;
118 1.56 thorpej }
119 1.56 thorpej }
120 1.56 thorpej
121 1.56 thorpej const size_t devlen = strlen("/dev/");
122 1.56 thorpej const char *cp = buf;
123 1.56 thorpej char *ecp = &buf[sizeof(buf) - 1];
124 1.56 thorpej
125 1.56 thorpej /* Find the start of the device xname. */
126 1.56 thorpej if (strlen(cp) > devlen && strncmp(cp, "/dev/", devlen) == 0) {
127 1.56 thorpej cp += devlen;
128 1.56 thorpej }
129 1.55 thorpej
130 1.56 thorpej /* Now strip any partition letter off the end. */
131 1.56 thorpej while (ecp != cp) {
132 1.56 thorpej if (*ecp >= '0' && *ecp <= '9') {
133 1.56 thorpej break;
134 1.55 thorpej }
135 1.56 thorpej *ecp-- = '\0';
136 1.55 thorpej }
137 1.55 thorpej
138 1.56 thorpej snprintf(bootinfo.booted_dev, sizeof(bootinfo.booted_dev),
139 1.56 thorpej "root=%s", cp);
140 1.56 thorpej booted_device = device_find_by_xname(cp);
141 1.55 thorpej }
142 1.55 thorpej
143 1.58 thorpej static bool
144 1.58 thorpej parse_dec_macaddr(const char *str, uint8_t enaddr[ETHER_ADDR_LEN])
145 1.58 thorpej {
146 1.58 thorpej char *cp;
147 1.58 thorpej long long l;
148 1.58 thorpej int i;
149 1.58 thorpej
150 1.58 thorpej /*
151 1.58 thorpej * DEC Ethernet address strings are formatted like so:
152 1.58 thorpej *
153 1.58 thorpej * XX-XX-XX-XX-XX-XX
154 1.58 thorpej */
155 1.58 thorpej
156 1.58 thorpej for (i = 0; i < ETHER_ADDR_LEN; i++) {
157 1.58 thorpej l = strtoll(str, &cp, 16);
158 1.58 thorpej if (l < 0 || l > 0xff) {
159 1.58 thorpej /* Not a valid MAC address. */
160 1.58 thorpej return false;
161 1.58 thorpej }
162 1.58 thorpej if (*cp == '-') {
163 1.58 thorpej /* Octet separator. */
164 1.58 thorpej enaddr[i] = (uint8_t)l;
165 1.58 thorpej str = cp + 1;
166 1.58 thorpej continue;
167 1.58 thorpej }
168 1.58 thorpej if (*cp == ' ' || *cp == '\0') {
169 1.58 thorpej /* End of the string. */
170 1.58 thorpej enaddr[i] = (uint8_t)l;
171 1.58 thorpej return i == ETHER_ADDR_LEN - 1;
172 1.58 thorpej }
173 1.58 thorpej /* Bogus character. */
174 1.58 thorpej break;
175 1.58 thorpej }
176 1.58 thorpej
177 1.58 thorpej /* Encountered bogus character or didn't reach end of string. */
178 1.58 thorpej return false;
179 1.58 thorpej }
180 1.58 thorpej
181 1.58 thorpej static void
182 1.58 thorpej netboot_find_rootdev_planb(void)
183 1.58 thorpej {
184 1.58 thorpej struct psref psref;
185 1.58 thorpej uint8_t enaddr[ETHER_ADDR_LEN];
186 1.58 thorpej char ifname[IFNAMSIZ];
187 1.58 thorpej int i;
188 1.58 thorpej
189 1.58 thorpej if (strncasecmp(bootinfo.booted_dev, "BOOTP ", 6) != 0 &&
190 1.58 thorpej strncasecmp(bootinfo.booted_dev, "MOP ", 4) != 0) {
191 1.58 thorpej /* We weren't netbooted. */
192 1.58 thorpej return;
193 1.58 thorpej }
194 1.58 thorpej
195 1.58 thorpej for (i = 2; bootinfo.booted_dev[i] != '\0'; i++) {
196 1.58 thorpej if (bootinfo.booted_dev[i] == '-') {
197 1.58 thorpej if (parse_dec_macaddr(&bootinfo.booted_dev[i - 2],
198 1.58 thorpej enaddr)) {
199 1.58 thorpej /* Found it! */
200 1.58 thorpej break;
201 1.58 thorpej }
202 1.58 thorpej }
203 1.58 thorpej }
204 1.58 thorpej if (bootinfo.booted_dev[i] == '\0') {
205 1.58 thorpej /* No MAC address in string. */
206 1.58 thorpej return;
207 1.58 thorpej }
208 1.58 thorpej
209 1.58 thorpej /* Now try to look up the interface by the link address. */
210 1.58 thorpej struct ifnet *ifp = if_get_bylla(enaddr, ETHER_ADDR_LEN, &psref);
211 1.58 thorpej if (ifp == NULL) {
212 1.58 thorpej /* No interface attached with that MAC address. */
213 1.58 thorpej return;
214 1.58 thorpej }
215 1.58 thorpej
216 1.58 thorpej strlcpy(ifname, if_name(ifp), sizeof(ifname));
217 1.58 thorpej if_put(ifp, &psref);
218 1.58 thorpej
219 1.58 thorpej /* Ok! Now look up the device_t by name! */
220 1.58 thorpej booted_device = device_find_by_xname(ifname);
221 1.58 thorpej }
222 1.58 thorpej
223 1.20 gwr void
224 1.47 cegger cpu_rootconf(void)
225 1.20 gwr {
226 1.1 cgd
227 1.55 thorpej if (booted_device == NULL && alpha_is_qemu) {
228 1.55 thorpej qemu_find_rootdev();
229 1.55 thorpej }
230 1.55 thorpej
231 1.55 thorpej if (booted_device == NULL) {
232 1.58 thorpej /*
233 1.58 thorpej * It's possible that we netbooted from an Ethernet
234 1.58 thorpej * interface that can't be matched via the usual
235 1.58 thorpej * logic in device_register() (a DE204 in an ISA slot,
236 1.58 thorpej * for example). In these cases, the console may have
237 1.58 thorpej * provided us with a MAC address that we can use to
238 1.58 thorpej * try and find the interface, * e.g.:
239 1.58 thorpej *
240 1.58 thorpej * BOOTP 1 1 0 0 0 5 0 08-00-2B-xx-xx-xx 1
241 1.58 thorpej */
242 1.58 thorpej netboot_find_rootdev_planb();
243 1.58 thorpej }
244 1.58 thorpej
245 1.58 thorpej if (booted_device == NULL) {
246 1.14 christos printf("WARNING: can't figure what device matches \"%s\"\n",
247 1.30 cgd bootinfo.booted_dev);
248 1.55 thorpej }
249 1.52 mlelstv rootconf();
250 1.5 cgd }
251 1.5 cgd
252 1.58 thorpej static inline int
253 1.58 thorpej atoi(const char *s)
254 1.58 thorpej {
255 1.58 thorpej return (int)strtoll(s, NULL, 10);
256 1.58 thorpej }
257 1.58 thorpej
258 1.58 thorpej static void
259 1.47 cegger parse_prom_bootdev(void)
260 1.5 cgd {
261 1.5 cgd static char hacked_boot_dev[128];
262 1.5 cgd static struct bootdev_data bd;
263 1.5 cgd char *cp, *scp, *boot_fields[8];
264 1.5 cgd int i, done;
265 1.5 cgd
266 1.5 cgd booted_device = NULL;
267 1.5 cgd booted_partition = 0;
268 1.5 cgd bootdev_data = NULL;
269 1.5 cgd
270 1.38 thorpej memcpy(hacked_boot_dev, bootinfo.booted_dev,
271 1.54 riastrad uimin(sizeof bootinfo.booted_dev, sizeof hacked_boot_dev));
272 1.5 cgd #if 0
273 1.30 cgd printf("parse_prom_bootdev: boot dev = \"%s\"\n", hacked_boot_dev);
274 1.5 cgd #endif
275 1.5 cgd
276 1.5 cgd i = 0;
277 1.5 cgd scp = cp = hacked_boot_dev;
278 1.5 cgd for (done = 0; !done; cp++) {
279 1.5 cgd if (*cp != ' ' && *cp != '\0')
280 1.5 cgd continue;
281 1.5 cgd if (*cp == '\0')
282 1.5 cgd done = 1;
283 1.5 cgd
284 1.5 cgd *cp = '\0';
285 1.5 cgd boot_fields[i++] = scp;
286 1.5 cgd scp = cp + 1;
287 1.5 cgd if (i == 8)
288 1.5 cgd done = 1;
289 1.5 cgd }
290 1.5 cgd if (i != 8)
291 1.5 cgd return; /* doesn't look like anything we know! */
292 1.5 cgd
293 1.5 cgd #if 0
294 1.14 christos printf("i = %d, done = %d\n", i, done);
295 1.5 cgd for (i--; i >= 0; i--)
296 1.14 christos printf("%d = %s\n", i, boot_fields[i]);
297 1.5 cgd #endif
298 1.5 cgd
299 1.5 cgd bd.protocol = boot_fields[0];
300 1.5 cgd bd.bus = atoi(boot_fields[1]);
301 1.5 cgd bd.slot = atoi(boot_fields[2]);
302 1.5 cgd bd.channel = atoi(boot_fields[3]);
303 1.5 cgd bd.remote_address = boot_fields[4];
304 1.5 cgd bd.unit = atoi(boot_fields[5]);
305 1.5 cgd bd.boot_dev_type = atoi(boot_fields[6]);
306 1.5 cgd bd.ctrl_dev_type = boot_fields[7];
307 1.5 cgd
308 1.5 cgd #if 0
309 1.14 christos printf("parsed: proto = %s, bus = %d, slot = %d, channel = %d,\n",
310 1.5 cgd bd.protocol, bd.bus, bd.slot, bd.channel);
311 1.14 christos printf("\tremote = %s, unit = %d, dev_type = %d, ctrl_type = %s\n",
312 1.5 cgd bd.remote_address, bd.unit, bd.boot_dev_type, bd.ctrl_dev_type);
313 1.5 cgd #endif
314 1.5 cgd
315 1.5 cgd bootdev_data = &bd;
316 1.5 cgd }
317 1.5 cgd
318 1.5 cgd void
319 1.48 matt device_register(device_t dev, void *aux)
320 1.5 cgd {
321 1.53 tsutsui #if NPCI > 0
322 1.53 tsutsui device_t parent = device_parent(dev);
323 1.53 tsutsui
324 1.53 tsutsui if (parent != NULL && device_is_a(parent, "pci"))
325 1.53 tsutsui device_pci_register(dev, aux);
326 1.53 tsutsui #endif
327 1.53 tsutsui
328 1.5 cgd if (bootdev_data == NULL) {
329 1.5 cgd /*
330 1.5 cgd * There is no hope.
331 1.5 cgd */
332 1.5 cgd return;
333 1.5 cgd }
334 1.28 mjacob if (platform.device_register)
335 1.28 mjacob (*platform.device_register)(dev, aux);
336 1.1 cgd }
337