autoconf.c revision 1.4 1 1.4 cgd /* $NetBSD: autoconf.c,v 1.4 1996/06/12 01:57:17 cgd 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.4 cgd * 3. All advertising materials mentioning features or use of this software
25 1.4 cgd * must display the following acknowledgement:
26 1.4 cgd * This product includes software developed by the University of
27 1.4 cgd * California, Berkeley and its contributors.
28 1.4 cgd * 4. Neither the name of the University nor the names of its contributors
29 1.4 cgd * may be used to endorse or promote products derived from this software
30 1.4 cgd * without specific prior written permission.
31 1.4 cgd *
32 1.4 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 1.4 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 1.4 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 1.4 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 1.4 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 1.4 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 1.4 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 1.4 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 1.4 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 1.4 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 1.4 cgd * SUCH DAMAGE.
43 1.4 cgd *
44 1.4 cgd * @(#)autoconf.c 8.4 (Berkeley) 10/1/93
45 1.1 cgd */
46 1.1 cgd
47 1.1 cgd #include <sys/param.h>
48 1.1 cgd #include <sys/systm.h>
49 1.1 cgd #include <sys/buf.h>
50 1.1 cgd #include <sys/disklabel.h>
51 1.1 cgd #include <sys/conf.h>
52 1.1 cgd #include <sys/reboot.h>
53 1.1 cgd #include <sys/device.h>
54 1.1 cgd
55 1.1 cgd #include <machine/autoconf.h>
56 1.1 cgd
57 1.4 cgd struct device *parsedisk __P((char *str, int len, int defpart, dev_t *devp));
58 1.4 cgd static struct device *getdisk __P((char *str, int len, int defpart,
59 1.4 cgd dev_t *devp));
60 1.4 cgd static int findblkmajor __P((struct device *dv));
61 1.4 cgd static int getstr __P((char *cp, int size));
62 1.4 cgd
63 1.1 cgd /*
64 1.1 cgd * configure:
65 1.1 cgd * called at boot time, configure all devices on system
66 1.1 cgd */
67 1.1 cgd void
68 1.1 cgd configure()
69 1.1 cgd {
70 1.1 cgd extern int cold;
71 1.1 cgd
72 1.1 cgd (void)splhigh();
73 1.2 cgd if (config_rootfound("mainbus", "mainbus") == NULL)
74 1.1 cgd panic("no mainbus found");
75 1.1 cgd (void)spl0();
76 1.1 cgd
77 1.1 cgd setroot();
78 1.1 cgd swapconf();
79 1.1 cgd cold = 0;
80 1.1 cgd }
81 1.1 cgd
82 1.1 cgd /*
83 1.1 cgd * Configure swap space and related parameters.
84 1.1 cgd */
85 1.1 cgd swapconf()
86 1.1 cgd {
87 1.4 cgd struct swdevt *swp;
88 1.4 cgd int nblks, maj;
89 1.1 cgd
90 1.1 cgd for (swp = swdevt; swp->sw_dev != NODEV; swp++) {
91 1.4 cgd maj = major(swp->sw_dev);
92 1.1 cgd if (maj > nblkdev)
93 1.1 cgd break;
94 1.1 cgd if (bdevsw[maj].d_psize) {
95 1.1 cgd nblks = (*bdevsw[maj].d_psize)(swp->sw_dev);
96 1.1 cgd if (nblks != -1 &&
97 1.1 cgd (swp->sw_nblks == 0 || swp->sw_nblks > nblks))
98 1.1 cgd swp->sw_nblks = nblks;
99 1.1 cgd swp->sw_nblks = ctod(dtoc(swp->sw_nblks));
100 1.1 cgd }
101 1.1 cgd }
102 1.4 cgd dumpconf();
103 1.1 cgd }
104 1.1 cgd
105 1.4 cgd struct nam2blk {
106 1.4 cgd char *name;
107 1.4 cgd int maj;
108 1.4 cgd } nam2blk[] = {
109 1.4 cgd { "st", 2 },
110 1.4 cgd { "cd", 3 },
111 1.4 cgd { "sd", 8 },
112 1.4 cgd #if 0
113 1.4 cgd { "fd", XXX },
114 1.4 cgd #endif
115 1.4 cgd };
116 1.4 cgd
117 1.4 cgd static int
118 1.4 cgd findblkmajor(dv)
119 1.4 cgd struct device *dv;
120 1.4 cgd {
121 1.4 cgd char *name = dv->dv_xname;
122 1.4 cgd register int i;
123 1.4 cgd
124 1.4 cgd for (i = 0; i < sizeof(nam2blk)/sizeof(nam2blk[0]); ++i)
125 1.4 cgd if (strncmp(name, nam2blk[i].name, strlen(nam2blk[0].name))
126 1.4 cgd == 0)
127 1.4 cgd return (nam2blk[i].maj);
128 1.4 cgd return (-1);
129 1.4 cgd }
130 1.4 cgd
131 1.4 cgd static struct device *
132 1.4 cgd getdisk(str, len, defpart, devp)
133 1.4 cgd char *str;
134 1.4 cgd int len, defpart;
135 1.4 cgd dev_t *devp;
136 1.4 cgd {
137 1.4 cgd register struct device *dv;
138 1.1 cgd
139 1.4 cgd if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
140 1.4 cgd printf("use one of:");
141 1.4 cgd for (dv = alldevs.tqh_first; dv != NULL;
142 1.4 cgd dv = dv->dv_list.tqe_next) {
143 1.4 cgd if (dv->dv_class == DV_DISK)
144 1.4 cgd printf(" %s[a-h]", dv->dv_xname);
145 1.4 cgd #ifdef NFSCLIENT
146 1.4 cgd if (dv->dv_class == DV_IFNET)
147 1.4 cgd printf(" %s", dv->dv_xname);
148 1.4 cgd #endif
149 1.4 cgd }
150 1.4 cgd printf("\n");
151 1.4 cgd }
152 1.4 cgd return (dv);
153 1.4 cgd }
154 1.4 cgd
155 1.4 cgd struct device *
156 1.4 cgd parsedisk(str, len, defpart, devp)
157 1.4 cgd char *str;
158 1.4 cgd int len, defpart;
159 1.4 cgd dev_t *devp;
160 1.4 cgd {
161 1.4 cgd register struct device *dv;
162 1.4 cgd register char *cp, c;
163 1.4 cgd int majdev, part;
164 1.4 cgd
165 1.4 cgd if (len == 0)
166 1.4 cgd return (NULL);
167 1.4 cgd cp = str + len - 1;
168 1.4 cgd c = *cp;
169 1.4 cgd if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
170 1.4 cgd part = c - 'a';
171 1.4 cgd *cp = '\0';
172 1.4 cgd } else
173 1.4 cgd part = defpart;
174 1.4 cgd
175 1.4 cgd for (dv = alldevs.tqh_first; dv != NULL; dv = dv->dv_list.tqe_next) {
176 1.4 cgd if (dv->dv_class == DV_DISK &&
177 1.4 cgd strcmp(str, dv->dv_xname) == 0) {
178 1.4 cgd majdev = findblkmajor(dv);
179 1.4 cgd if (majdev < 0)
180 1.4 cgd panic("parsedisk");
181 1.4 cgd *devp = MAKEDISKDEV(majdev, dv->dv_unit, part);
182 1.4 cgd break;
183 1.4 cgd }
184 1.4 cgd #ifdef NFSCLIENT
185 1.4 cgd if (dv->dv_class == DV_IFNET &&
186 1.4 cgd strcmp(str, dv->dv_xname) == 0) {
187 1.4 cgd *devp = NODEV;
188 1.4 cgd break;
189 1.4 cgd }
190 1.4 cgd #endif
191 1.4 cgd }
192 1.4 cgd
193 1.4 cgd *cp = c;
194 1.4 cgd return (dv);
195 1.4 cgd }
196 1.1 cgd
197 1.1 cgd /*
198 1.1 cgd * Attempt to find the device from which we were booted.
199 1.1 cgd * If we can do so, and not instructed not to do so,
200 1.1 cgd * change rootdev to correspond to the load device.
201 1.4 cgd *
202 1.4 cgd * XXX Actually, swap and root must be on the same type of device,
203 1.4 cgd * (ie. DV_DISK or DV_IFNET) because of how (*mountroot) is written.
204 1.4 cgd * That should be fixed.
205 1.1 cgd */
206 1.1 cgd setroot()
207 1.1 cgd {
208 1.1 cgd struct swdevt *swp;
209 1.4 cgd struct device *dv;
210 1.4 cgd register int len;
211 1.4 cgd dev_t nrootdev, nswapdev = NODEV;
212 1.4 cgd char buf[128];
213 1.4 cgd extern int (*mountroot) __P((void *));
214 1.4 cgd dev_t temp;
215 1.4 cgd struct device *bootdv, *rootdv, *swapdv;
216 1.4 cgd int bootpartition; /* XXX */
217 1.4 cgd #if defined(NFSCLIENT)
218 1.4 cgd extern char *nfsbootdevname;
219 1.4 cgd extern int nfs_mountroot __P((void *));
220 1.4 cgd #endif
221 1.4 cgd #if defined(FFS)
222 1.4 cgd extern int ffs_mountroot __P((void *));
223 1.4 cgd #endif
224 1.4 cgd
225 1.4 cgd bootdv = NULL; /* XXX */
226 1.4 cgd bootpartition = 0; /* XXX */
227 1.4 cgd
228 1.4 cgd /*
229 1.4 cgd * If 'swap generic' and we couldn't determine root device,
230 1.4 cgd * ask the user.
231 1.4 cgd */
232 1.4 cgd if (mountroot == NULL && bootdv == NULL)
233 1.4 cgd boothowto |= RB_ASKNAME;
234 1.4 cgd
235 1.4 cgd if (boothowto & RB_ASKNAME) {
236 1.4 cgd for (;;) {
237 1.4 cgd printf("root device");
238 1.4 cgd if (bootdv != NULL)
239 1.4 cgd printf(" (default %s%c)",
240 1.4 cgd bootdv->dv_xname,
241 1.4 cgd bootdv->dv_class == DV_DISK
242 1.4 cgd ? bootpartition + 'a' : ' ');
243 1.4 cgd printf(": ");
244 1.4 cgd len = getstr(buf, sizeof(buf));
245 1.4 cgd if (len == 0 && bootdv != NULL) {
246 1.4 cgd strcpy(buf, bootdv->dv_xname);
247 1.4 cgd len = strlen(buf);
248 1.4 cgd }
249 1.4 cgd if (len == 4 && !strcmp(buf, "halt"))
250 1.4 cgd boot(RB_HALT);
251 1.4 cgd if (len > 0 && buf[len - 1] == '*') {
252 1.4 cgd buf[--len] = '\0';
253 1.4 cgd dv = getdisk(buf, len, 1, &nrootdev);
254 1.4 cgd if (dv != NULL) {
255 1.4 cgd rootdv = dv;
256 1.4 cgd nswapdev = nrootdev;
257 1.4 cgd goto gotswap;
258 1.4 cgd }
259 1.4 cgd }
260 1.4 cgd dv = getdisk(buf, len, bootpartition, &nrootdev);
261 1.4 cgd if (dv != NULL) {
262 1.4 cgd rootdv = dv;
263 1.4 cgd break;
264 1.4 cgd }
265 1.4 cgd }
266 1.4 cgd
267 1.4 cgd /*
268 1.4 cgd * because swap must be on same device type as root, for
269 1.4 cgd * network devices this is easy.
270 1.4 cgd */
271 1.4 cgd if (rootdv->dv_class == DV_IFNET) {
272 1.4 cgd swapdv = NULL;
273 1.4 cgd goto gotswap;
274 1.4 cgd }
275 1.4 cgd for (;;) {
276 1.4 cgd printf("swap device");
277 1.4 cgd printf(" (default %s%c)", rootdv->dv_xname,
278 1.4 cgd rootdv->dv_class == DV_DISK?'b':' ');
279 1.4 cgd printf(": ");
280 1.4 cgd len = getstr(buf, sizeof(buf));
281 1.4 cgd if (len == 0) {
282 1.4 cgd switch (rootdv->dv_class) {
283 1.4 cgd case DV_IFNET:
284 1.4 cgd nswapdev = NODEV;
285 1.4 cgd break;
286 1.4 cgd case DV_DISK:
287 1.4 cgd nswapdev = MAKEDISKDEV(major(nrootdev),
288 1.4 cgd DISKUNIT(nrootdev), 1);
289 1.4 cgd break;
290 1.4 cgd case DV_TAPE:
291 1.4 cgd case DV_TTY:
292 1.4 cgd case DV_DULL:
293 1.4 cgd case DV_CPU:
294 1.4 cgd break;
295 1.4 cgd }
296 1.4 cgd swapdv = rootdv;
297 1.4 cgd break;
298 1.4 cgd }
299 1.4 cgd if (len == 4 && !strcmp(buf, "halt"))
300 1.4 cgd boot(RB_HALT);
301 1.4 cgd dv = getdisk(buf, len, 1, &nswapdev);
302 1.4 cgd if (dv) {
303 1.4 cgd if (dv->dv_class == DV_IFNET)
304 1.4 cgd nswapdev = NODEV;
305 1.4 cgd swapdv = dv;
306 1.4 cgd break;
307 1.4 cgd }
308 1.4 cgd }
309 1.4 cgd gotswap:
310 1.4 cgd rootdev = nrootdev;
311 1.4 cgd dumpdev = nswapdev;
312 1.4 cgd swdevt[0].sw_dev = nswapdev;
313 1.4 cgd swdevt[1].sw_dev = NODEV;
314 1.4 cgd } else if (mountroot == NULL) {
315 1.4 cgd int majdev;
316 1.4 cgd
317 1.4 cgd /*
318 1.4 cgd * "swap generic"
319 1.4 cgd */
320 1.4 cgd majdev = findblkmajor(bootdv);
321 1.4 cgd if (majdev >= 0) {
322 1.4 cgd /*
323 1.4 cgd * Root and swap are on a disk.
324 1.4 cgd */
325 1.4 cgd rootdv = swapdv = bootdv;
326 1.4 cgd rootdev = MAKEDISKDEV(majdev, bootdv->dv_unit,
327 1.4 cgd bootpartition);
328 1.4 cgd nswapdev = dumpdev =
329 1.4 cgd MAKEDISKDEV(majdev, bootdv->dv_unit, 1);
330 1.4 cgd } else {
331 1.4 cgd /*
332 1.4 cgd * Root and swap are on a net.
333 1.4 cgd */
334 1.4 cgd rootdv = swapdv = NULL;
335 1.4 cgd nswapdev = dumpdev = NODEV;
336 1.4 cgd }
337 1.4 cgd swdevt[0].sw_dev = nswapdev;
338 1.4 cgd swdevt[1].sw_dev = NODEV;
339 1.4 cgd } else {
340 1.4 cgd
341 1.4 cgd /*
342 1.4 cgd * `root DEV swap DEV': honour rootdev/swdevt.
343 1.4 cgd * rootdev/swdevt/mountroot already properly set.
344 1.4 cgd */
345 1.4 cgd return;
346 1.4 cgd }
347 1.1 cgd
348 1.4 cgd switch (rootdv->dv_class) {
349 1.4 cgd #if defined(NFSCLIENT)
350 1.4 cgd case DV_IFNET:
351 1.4 cgd mountroot = nfs_mountroot;
352 1.4 cgd nfsbootdevname = rootdv->dv_xname;
353 1.1 cgd return;
354 1.4 cgd #endif
355 1.4 cgd #if defined(FFS)
356 1.4 cgd case DV_DISK:
357 1.4 cgd mountroot = ffs_mountroot;
358 1.4 cgd printf("root on %s%c", rootdv->dv_xname,
359 1.4 cgd DISKPART(rootdev) + 'a');
360 1.4 cgd if (nswapdev != NODEV)
361 1.4 cgd printf(" swap on %s%c", swapdv->dv_xname,
362 1.4 cgd DISKPART(nswapdev) + 'a');
363 1.4 cgd printf("\n");
364 1.4 cgd break;
365 1.4 cgd #endif
366 1.4 cgd default:
367 1.4 cgd printf("can't figure root, hope your kernel is right\n");
368 1.1 cgd return;
369 1.4 cgd }
370 1.4 cgd
371 1.1 cgd /*
372 1.4 cgd * Make the swap partition on the root drive the primary swap.
373 1.1 cgd */
374 1.4 cgd temp = NODEV;
375 1.1 cgd for (swp = swdevt; swp->sw_dev != NODEV; swp++) {
376 1.4 cgd if (major(rootdev) == major(swp->sw_dev) &&
377 1.4 cgd DISKUNIT(rootdev) == DISKUNIT(swp->sw_dev)) {
378 1.1 cgd temp = swdevt[0].sw_dev;
379 1.1 cgd swdevt[0].sw_dev = swp->sw_dev;
380 1.1 cgd swp->sw_dev = temp;
381 1.1 cgd break;
382 1.1 cgd }
383 1.1 cgd }
384 1.1 cgd if (swp->sw_dev == NODEV)
385 1.1 cgd return;
386 1.1 cgd
387 1.1 cgd /*
388 1.1 cgd * If dumpdev was the same as the old primary swap device, move
389 1.1 cgd * it to the new primary swap device.
390 1.1 cgd */
391 1.1 cgd if (temp == dumpdev)
392 1.1 cgd dumpdev = swdevt[0].sw_dev;
393 1.4 cgd }
394 1.4 cgd
395 1.4 cgd static int
396 1.4 cgd getstr(cp, size)
397 1.4 cgd register char *cp;
398 1.4 cgd register int size;
399 1.4 cgd {
400 1.4 cgd register char *lp;
401 1.4 cgd register int c;
402 1.4 cgd register int len;
403 1.4 cgd
404 1.4 cgd lp = cp;
405 1.4 cgd len = 0;
406 1.4 cgd for (;;) {
407 1.4 cgd c = cngetc();
408 1.4 cgd switch (c) {
409 1.4 cgd case '\n':
410 1.4 cgd case '\r':
411 1.4 cgd printf("\n");
412 1.4 cgd *lp++ = '\0';
413 1.4 cgd return (len);
414 1.4 cgd case '\b':
415 1.4 cgd case '\177':
416 1.4 cgd case '#':
417 1.4 cgd if (len) {
418 1.4 cgd --len;
419 1.4 cgd --lp;
420 1.4 cgd printf("\b \b");
421 1.4 cgd }
422 1.4 cgd continue;
423 1.4 cgd case '@':
424 1.4 cgd case 'u'&037:
425 1.4 cgd len = 0;
426 1.4 cgd lp = cp;
427 1.4 cgd printf("\n");
428 1.4 cgd continue;
429 1.4 cgd default:
430 1.4 cgd if (len + 1 >= size || c < ' ') {
431 1.4 cgd printf("\007");
432 1.4 cgd continue;
433 1.4 cgd }
434 1.4 cgd printf("%c", c);
435 1.4 cgd ++len;
436 1.4 cgd *lp++ = c;
437 1.4 cgd }
438 1.4 cgd }
439 1.1 cgd }
440