autoconf.c revision 1.2 1 /* $NetBSD: autoconf.c,v 1.2 1996/04/04 06:25:00 cgd Exp $ */
2
3 /*
4 * Copyright (c) 1994, 1995 Carnegie-Mellon University.
5 * All rights reserved.
6 *
7 * Authors: Keith Bostic, Chris G. Demetriou
8 *
9 * Permission to use, copy, modify and distribute this software and
10 * its documentation is hereby granted, provided that both the copyright
11 * notice and this permission notice appear in all copies of the
12 * software, derivative works or modified versions, and any portions
13 * thereof, and that both notices appear in supporting documentation.
14 *
15 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
16 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
17 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
18 *
19 * Carnegie Mellon requests users of this software to return to
20 *
21 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
22 * School of Computer Science
23 * Carnegie Mellon University
24 * Pittsburgh PA 15213-3890
25 *
26 * any improvements or extensions that they make and grant Carnegie the
27 * rights to redistribute these changes.
28 */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/buf.h>
33 #include <sys/disklabel.h>
34 #include <sys/conf.h>
35 #include <sys/reboot.h>
36 #include <sys/device.h>
37
38 #include <machine/autoconf.h>
39
40 /*
41 * configure:
42 * called at boot time, configure all devices on system
43 */
44 void
45 configure()
46 {
47 extern int cold;
48
49 (void)splhigh();
50 if (config_rootfound("mainbus", "mainbus") == NULL)
51 panic("no mainbus found");
52 (void)spl0();
53
54 #ifdef GENERIC
55 if ((boothowto & RB_ASKNAME) == 0)
56 setroot();
57 setconf();
58 #else
59 setroot();
60 #endif
61 swapconf();
62 cold = 0;
63 }
64
65 /*
66 * Configure swap space and related parameters.
67 */
68 swapconf()
69 {
70 register struct swdevt *swp;
71 register int nblks;
72
73 for (swp = swdevt; swp->sw_dev != NODEV; swp++) {
74 int maj = major(swp->sw_dev);
75
76 if (maj > nblkdev)
77 break;
78 if (bdevsw[maj].d_psize) {
79 nblks = (*bdevsw[maj].d_psize)(swp->sw_dev);
80 if (nblks != -1 &&
81 (swp->sw_nblks == 0 || swp->sw_nblks > nblks))
82 swp->sw_nblks = nblks;
83 swp->sw_nblks = ctod(dtoc(swp->sw_nblks));
84 }
85 }
86 }
87
88 #define DOSWAP /* change swdevt and dumpdev */
89 dev_t bootdev = 0; /* should be dev_t, but not until 32 bits */
90
91 static char devname[][2] = {
92 'x','x', /* 0 = XX */
93 'x','x', /* 1 = XX */
94 'x','x', /* 2 = XX */
95 'x','x', /* 3 = XX */
96 'x','x', /* 4 = XX */
97 'x','x', /* 5 = XX */
98 'x','x', /* 6 = XX */
99 'x','x', /* 7 = XX */
100 's','d', /* 8 = sd */
101 };
102
103 /*
104 * Attempt to find the device from which we were booted.
105 * If we can do so, and not instructed not to do so,
106 * change rootdev to correspond to the load device.
107 */
108 setroot()
109 {
110 int majdev, mindev, unit, part, adaptor;
111 dev_t temp, orootdev;
112 struct swdevt *swp;
113
114 /*printf("howto %x bootdev %x ", boothowto, bootdev);*/
115 if (boothowto & RB_DFLTROOT ||
116 (bootdev & B_MAGICMASK) != (u_long)B_DEVMAGIC)
117 return;
118 majdev = (bootdev >> B_TYPESHIFT) & B_TYPEMASK;
119 if (majdev > sizeof(devname) / sizeof(devname[0]))
120 return;
121 adaptor = (bootdev >> B_ADAPTORSHIFT) & B_ADAPTORMASK;
122 part = (bootdev >> B_PARTITIONSHIFT) & B_PARTITIONMASK;
123 unit = (bootdev >> B_UNITSHIFT) & B_UNITMASK;
124 mindev = (unit * MAXPARTITIONS) + part;
125 orootdev = rootdev;
126 rootdev = makedev(majdev, mindev);
127 /*
128 * If the original rootdev is the same as the one
129 * just calculated, don't need to adjust the swap configuration.
130 */
131 if (rootdev == orootdev)
132 return;
133 printf("changing root device to %c%c%d%c\n",
134 devname[majdev][0], devname[majdev][1],
135 unit, part + 'a');
136
137 #ifdef DOSWAP
138 for (swp = swdevt; swp->sw_dev != NODEV; swp++) {
139 if (majdev == major(swp->sw_dev) &&
140 (mindev / MAXPARTITIONS)
141 == (minor(swp->sw_dev) / MAXPARTITIONS)) {
142 temp = swdevt[0].sw_dev;
143 swdevt[0].sw_dev = swp->sw_dev;
144 swp->sw_dev = temp;
145 break;
146 }
147 }
148 if (swp->sw_dev == NODEV)
149 return;
150
151 /*
152 * If dumpdev was the same as the old primary swap device, move
153 * it to the new primary swap device.
154 */
155 if (temp == dumpdev)
156 dumpdev = swdevt[0].sw_dev;
157 #endif
158 }
159