kern_subr.c revision 1.228 1 1.228 thorpej /* $NetBSD: kern_subr.c,v 1.228 2020/01/01 22:57:17 thorpej Exp $ */
2 1.31 thorpej
3 1.31 thorpej /*-
4 1.185 ad * Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
5 1.31 thorpej * All rights reserved.
6 1.31 thorpej *
7 1.31 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.31 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.48 lukem * NASA Ames Research Center, and by Luke Mewburn.
10 1.31 thorpej *
11 1.31 thorpej * Redistribution and use in source and binary forms, with or without
12 1.31 thorpej * modification, are permitted provided that the following conditions
13 1.31 thorpej * are met:
14 1.31 thorpej * 1. Redistributions of source code must retain the above copyright
15 1.31 thorpej * notice, this list of conditions and the following disclaimer.
16 1.31 thorpej * 2. Redistributions in binary form must reproduce the above copyright
17 1.31 thorpej * notice, this list of conditions and the following disclaimer in the
18 1.31 thorpej * documentation and/or other materials provided with the distribution.
19 1.31 thorpej *
20 1.31 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.31 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.31 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.31 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.31 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.31 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.31 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.31 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.31 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.31 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.31 thorpej * POSSIBILITY OF SUCH DAMAGE.
31 1.31 thorpej */
32 1.12 cgd
33 1.9 cgd /*
34 1.10 cgd * Copyright (c) 1982, 1986, 1991, 1993
35 1.10 cgd * The Regents of the University of California. All rights reserved.
36 1.9 cgd * (c) UNIX System Laboratories, Inc.
37 1.9 cgd * All or some portions of this file are derived from material licensed
38 1.9 cgd * to the University of California by American Telephone and Telegraph
39 1.9 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
40 1.9 cgd * the permission of UNIX System Laboratories, Inc.
41 1.9 cgd *
42 1.18 thorpej * Copyright (c) 1992, 1993
43 1.18 thorpej * The Regents of the University of California. All rights reserved.
44 1.18 thorpej *
45 1.18 thorpej * This software was developed by the Computer Systems Engineering group
46 1.18 thorpej * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
47 1.18 thorpej * contributed to Berkeley.
48 1.18 thorpej *
49 1.18 thorpej * All advertising materials mentioning features or use of this software
50 1.18 thorpej * must display the following acknowledgement:
51 1.18 thorpej * This product includes software developed by the University of
52 1.18 thorpej * California, Lawrence Berkeley Laboratory.
53 1.18 thorpej *
54 1.9 cgd * Redistribution and use in source and binary forms, with or without
55 1.9 cgd * modification, are permitted provided that the following conditions
56 1.9 cgd * are met:
57 1.9 cgd * 1. Redistributions of source code must retain the above copyright
58 1.9 cgd * notice, this list of conditions and the following disclaimer.
59 1.9 cgd * 2. Redistributions in binary form must reproduce the above copyright
60 1.9 cgd * notice, this list of conditions and the following disclaimer in the
61 1.9 cgd * documentation and/or other materials provided with the distribution.
62 1.103 agc * 3. Neither the name of the University nor the names of its contributors
63 1.9 cgd * may be used to endorse or promote products derived from this software
64 1.9 cgd * without specific prior written permission.
65 1.9 cgd *
66 1.9 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
67 1.9 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
68 1.9 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
69 1.9 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
70 1.9 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
71 1.9 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
72 1.9 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73 1.9 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
74 1.9 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75 1.9 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
76 1.9 cgd * SUCH DAMAGE.
77 1.9 cgd *
78 1.38 fvdl * @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
79 1.9 cgd */
80 1.77 lukem
81 1.77 lukem #include <sys/cdefs.h>
82 1.228 thorpej __KERNEL_RCSID(0, "$NetBSD: kern_subr.c,v 1.228 2020/01/01 22:57:17 thorpej Exp $");
83 1.34 mrg
84 1.78 thorpej #include "opt_ddb.h"
85 1.43 jonathan #include "opt_md.h"
86 1.157 manu #include "opt_tftproot.h"
87 1.9 cgd
88 1.9 cgd #include <sys/param.h>
89 1.9 cgd #include <sys/systm.h>
90 1.9 cgd #include <sys/proc.h>
91 1.18 thorpej #include <sys/mount.h>
92 1.18 thorpej #include <sys/device.h>
93 1.18 thorpej #include <sys/reboot.h>
94 1.18 thorpej #include <sys/conf.h>
95 1.159 dyoung #include <sys/disk.h>
96 1.18 thorpej #include <sys/disklabel.h>
97 1.10 cgd #include <sys/queue.h>
98 1.119 reinoud #include <sys/fcntl.h>
99 1.160 christos #include <sys/kauth.h>
100 1.208 hannken #include <sys/stat.h>
101 1.160 christos #include <sys/vnode.h>
102 1.194 ad #include <sys/module.h>
103 1.9 cgd
104 1.18 thorpej #include <dev/cons.h>
105 1.18 thorpej
106 1.18 thorpej #include <net/if.h>
107 1.18 thorpej
108 1.18 thorpej /* XXX these should eventually move to subr_autoconf.c */
109 1.200 dyoung static device_t finddevice(const char *);
110 1.224 mlelstv static device_t getdisk(const char *, int, int, dev_t *, int);
111 1.224 mlelstv static device_t parsedisk(const char *, int, int, dev_t *);
112 1.159 dyoung static const char *getwedgename(const char *, int);
113 1.18 thorpej
114 1.221 mlelstv static void setroot_nfs(device_t);
115 1.222 mlelstv static void setroot_md(device_t *);
116 1.221 mlelstv static void setroot_ask(device_t, int);
117 1.221 mlelstv static void setroot_root(device_t, int);
118 1.221 mlelstv static void setroot_dump(device_t, device_t);
119 1.221 mlelstv
120 1.221 mlelstv
121 1.157 manu #ifdef TFTPROOT
122 1.200 dyoung int tftproot_dhcpboot(device_t);
123 1.157 manu #endif
124 1.157 manu
125 1.175 ad dev_t dumpcdev; /* for savecore */
126 1.18 thorpej
127 1.160 christos static int
128 1.200 dyoung isswap(device_t dv)
129 1.160 christos {
130 1.160 christos struct dkwedge_info wi;
131 1.160 christos struct vnode *vn;
132 1.160 christos int error;
133 1.160 christos
134 1.160 christos if (device_class(dv) != DV_DISK || !device_is_a(dv, "dk"))
135 1.160 christos return 0;
136 1.160 christos
137 1.160 christos if ((vn = opendisk(dv)) == NULL)
138 1.160 christos return 0;
139 1.160 christos
140 1.166 pooka error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
141 1.166 pooka VOP_CLOSE(vn, FREAD, NOCRED);
142 1.160 christos vput(vn);
143 1.160 christos if (error) {
144 1.160 christos #ifdef DEBUG_WEDGE
145 1.184 cegger printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
146 1.160 christos #endif
147 1.160 christos return 0;
148 1.160 christos }
149 1.160 christos return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
150 1.160 christos }
151 1.160 christos
152 1.18 thorpej /*
153 1.18 thorpej * Determine the root device and, if instructed to, the root file system.
154 1.18 thorpej */
155 1.18 thorpej
156 1.108 christos #ifdef MEMORY_DISK_IS_ROOT
157 1.187 ad int md_is_root = 1;
158 1.187 ad #else
159 1.187 ad int md_is_root = 0;
160 1.108 christos #endif
161 1.108 christos
162 1.113 thorpej /*
163 1.213 mlelstv * The device and partition that we booted from.
164 1.221 mlelstv *
165 1.221 mlelstv * This data might be initialized by MD code, but is defined here.
166 1.113 thorpej */
167 1.200 dyoung device_t booted_device;
168 1.218 christos const char *booted_method;
169 1.113 thorpej int booted_partition;
170 1.213 mlelstv daddr_t booted_startblk;
171 1.213 mlelstv uint64_t booted_nblks;
172 1.216 mlelstv char *bootspec;
173 1.113 thorpej
174 1.113 thorpej /*
175 1.219 jmcneill * Use partition letters if it's a disk class but not a wedge or flash.
176 1.219 jmcneill * XXX Check for wedge/flash is kinda gross.
177 1.113 thorpej */
178 1.113 thorpej #define DEV_USES_PARTITIONS(dv) \
179 1.124 thorpej (device_class((dv)) == DV_DISK && \
180 1.219 jmcneill !device_is_a((dv), "dk") && \
181 1.219 jmcneill !device_is_a((dv), "flash"))
182 1.113 thorpej
183 1.18 thorpej void
184 1.200 dyoung setroot(device_t bootdv, int bootpartition)
185 1.18 thorpej {
186 1.221 mlelstv
187 1.221 mlelstv /*
188 1.224 mlelstv * Let bootcode augment "rootspec", ensure that
189 1.224 mlelstv * rootdev is invalid to avoid confusion.
190 1.221 mlelstv */
191 1.224 mlelstv if (rootspec == NULL) {
192 1.221 mlelstv rootspec = bootspec;
193 1.224 mlelstv rootdev = NODEV;
194 1.224 mlelstv }
195 1.221 mlelstv
196 1.221 mlelstv /*
197 1.221 mlelstv * force boot device to md0
198 1.221 mlelstv */
199 1.221 mlelstv if (md_is_root)
200 1.222 mlelstv setroot_md(&bootdv);
201 1.18 thorpej
202 1.157 manu #ifdef TFTPROOT
203 1.221 mlelstv /*
204 1.221 mlelstv * XXX
205 1.221 mlelstv * if rootspec specifies an interface
206 1.221 mlelstv * sets root_device to that interface
207 1.221 mlelstv * reuses NFS init code to set up network
208 1.221 mlelstv * fetch image into ram disk
209 1.221 mlelstv *
210 1.222 mlelstv * if successful, we change boot device
211 1.221 mlelstv */
212 1.221 mlelstv if (tftproot_dhcpboot(bootdv) == 0)
213 1.222 mlelstv setroot_md(&bootdv);
214 1.157 manu #endif
215 1.221 mlelstv
216 1.208 hannken /*
217 1.221 mlelstv * quirk for
218 1.221 mlelstv * evbarm/mini2440
219 1.221 mlelstv * hpcarm
220 1.221 mlelstv * hpcmips
221 1.221 mlelstv * hpcsh
222 1.221 mlelstv *
223 1.221 mlelstv * if rootfstype is set to NFS and the
224 1.221 mlelstv * kernel supports NFS and the boot device
225 1.221 mlelstv * is unknown or not a network interface
226 1.221 mlelstv * -> chose the first network interface you find
227 1.221 mlelstv *
228 1.221 mlelstv * hp300 has similar MD code
229 1.208 hannken */
230 1.221 mlelstv setroot_nfs(bootdv);
231 1.18 thorpej
232 1.18 thorpej /*
233 1.221 mlelstv * If no bootdv was found by MD code and no
234 1.221 mlelstv * root specified ask the user.
235 1.216 mlelstv */
236 1.221 mlelstv if (rootspec == NULL && bootdv == NULL)
237 1.221 mlelstv boothowto |= RB_ASKNAME;
238 1.216 mlelstv
239 1.216 mlelstv /*
240 1.221 mlelstv * loop until a root device is specified
241 1.18 thorpej */
242 1.221 mlelstv do {
243 1.221 mlelstv if (boothowto & RB_ASKNAME)
244 1.221 mlelstv setroot_ask(bootdv, bootpartition);
245 1.221 mlelstv else
246 1.221 mlelstv setroot_root(bootdv, bootpartition);
247 1.221 mlelstv
248 1.221 mlelstv if (root_device == NULL)
249 1.221 mlelstv boothowto |= RB_ASKNAME;
250 1.221 mlelstv } while (root_device == NULL);
251 1.221 mlelstv }
252 1.221 mlelstv
253 1.221 mlelstv /*
254 1.221 mlelstv * If NFS is specified as the file system, and we found
255 1.221 mlelstv * a DV_DISK boot device (or no boot device at all), then
256 1.221 mlelstv * find a reasonable network interface for "rootspec".
257 1.221 mlelstv */
258 1.221 mlelstv static void
259 1.221 mlelstv setroot_nfs(device_t dv)
260 1.221 mlelstv {
261 1.221 mlelstv struct vfsops *vops;
262 1.221 mlelstv struct ifnet *ifp;
263 1.221 mlelstv
264 1.197 pgoyette vops = vfs_getopsbyname(MOUNT_NFS);
265 1.197 pgoyette if (vops != NULL && strcmp(rootfstype, MOUNT_NFS) == 0 &&
266 1.18 thorpej rootspec == NULL &&
267 1.221 mlelstv (dv == NULL || device_class(dv) != DV_IFNET)) {
268 1.217 ozaki int s = pserialize_read_enter();
269 1.217 ozaki IFNET_READER_FOREACH(ifp) {
270 1.18 thorpej if ((ifp->if_flags &
271 1.18 thorpej (IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
272 1.18 thorpej break;
273 1.83 matt }
274 1.221 mlelstv if (ifp != NULL) {
275 1.18 thorpej /*
276 1.18 thorpej * Have a suitable interface; behave as if
277 1.18 thorpej * the user specified this interface.
278 1.18 thorpej */
279 1.18 thorpej rootspec = (const char *)ifp->if_xname;
280 1.18 thorpej }
281 1.217 ozaki pserialize_read_exit(s);
282 1.18 thorpej }
283 1.165 ad if (vops != NULL)
284 1.165 ad vfs_delref(vops);
285 1.221 mlelstv }
286 1.24 thorpej
287 1.222 mlelstv /*
288 1.222 mlelstv * Change boot device to md0
289 1.222 mlelstv *
290 1.222 mlelstv * md0 only exists when it is opened once.
291 1.222 mlelstv */
292 1.222 mlelstv static void
293 1.222 mlelstv setroot_md(device_t *dvp)
294 1.222 mlelstv {
295 1.222 mlelstv int md_major;
296 1.222 mlelstv dev_t md_dev;
297 1.222 mlelstv
298 1.222 mlelstv md_major = devsw_name2blk("md", NULL, 0);
299 1.222 mlelstv if (md_major >= 0) {
300 1.222 mlelstv md_dev = MAKEDISKDEV(md_major, 0, RAW_PART);
301 1.222 mlelstv if (bdev_open(md_dev, FREAD, S_IFBLK, curlwp) == 0)
302 1.222 mlelstv *dvp = device_find_by_xname("md0");
303 1.222 mlelstv }
304 1.222 mlelstv }
305 1.222 mlelstv
306 1.221 mlelstv static void
307 1.221 mlelstv setroot_ask(device_t bootdv, int bootpartition)
308 1.221 mlelstv {
309 1.221 mlelstv device_t dv, defdumpdv, rootdv, dumpdv;
310 1.221 mlelstv dev_t nrootdev, ndumpdev;
311 1.221 mlelstv struct vfsops *vops;
312 1.221 mlelstv const char *deffsname;
313 1.221 mlelstv int len;
314 1.221 mlelstv char buf[128];
315 1.18 thorpej
316 1.221 mlelstv for (;;) {
317 1.221 mlelstv printf("root device");
318 1.221 mlelstv if (bootdv != NULL) {
319 1.221 mlelstv printf(" (default %s", device_xname(bootdv));
320 1.221 mlelstv if (DEV_USES_PARTITIONS(bootdv))
321 1.221 mlelstv printf("%c", bootpartition + 'a');
322 1.221 mlelstv printf(")");
323 1.221 mlelstv }
324 1.221 mlelstv printf(": ");
325 1.221 mlelstv len = cngetsn(buf, sizeof(buf));
326 1.221 mlelstv if (len == 0 && bootdv != NULL) {
327 1.221 mlelstv strlcpy(buf, device_xname(bootdv), sizeof(buf));
328 1.221 mlelstv len = strlen(buf);
329 1.221 mlelstv }
330 1.221 mlelstv if (len > 0 && buf[len - 1] == '*') {
331 1.221 mlelstv buf[--len] = '\0';
332 1.221 mlelstv dv = getdisk(buf, len, 1, &nrootdev, 0);
333 1.18 thorpej if (dv != NULL) {
334 1.18 thorpej rootdv = dv;
335 1.18 thorpej break;
336 1.18 thorpej }
337 1.18 thorpej }
338 1.221 mlelstv dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
339 1.221 mlelstv if (dv != NULL) {
340 1.221 mlelstv rootdv = dv;
341 1.221 mlelstv break;
342 1.221 mlelstv }
343 1.221 mlelstv }
344 1.221 mlelstv rootdev = nrootdev;
345 1.18 thorpej
346 1.221 mlelstv /*
347 1.221 mlelstv * Set up the default dump device. If root is on
348 1.221 mlelstv * a network device or a disk without partitions,
349 1.221 mlelstv * there is no default dump device.
350 1.221 mlelstv */
351 1.221 mlelstv if (DEV_USES_PARTITIONS(rootdv) == 0)
352 1.221 mlelstv defdumpdv = NULL;
353 1.221 mlelstv else
354 1.221 mlelstv defdumpdv = rootdv;
355 1.221 mlelstv
356 1.221 mlelstv ndumpdev = NODEV;
357 1.221 mlelstv for (;;) {
358 1.221 mlelstv printf("dump device");
359 1.221 mlelstv if (defdumpdv != NULL) {
360 1.221 mlelstv /*
361 1.221 mlelstv * Note, we know it's a disk if we get here.
362 1.221 mlelstv */
363 1.221 mlelstv printf(" (default %sb)", device_xname(defdumpdv));
364 1.221 mlelstv }
365 1.221 mlelstv printf(": ");
366 1.221 mlelstv len = cngetsn(buf, sizeof(buf));
367 1.221 mlelstv if (len == 0) {
368 1.26 thorpej if (defdumpdv != NULL) {
369 1.221 mlelstv ndumpdev = MAKEDISKDEV(major(nrootdev),
370 1.221 mlelstv DISKUNIT(nrootdev), 1);
371 1.26 thorpej }
372 1.221 mlelstv dumpdv = defdumpdv;
373 1.221 mlelstv break;
374 1.221 mlelstv }
375 1.221 mlelstv if (len == 4 && strcmp(buf, "none") == 0) {
376 1.221 mlelstv dumpdv = NULL;
377 1.221 mlelstv break;
378 1.221 mlelstv }
379 1.221 mlelstv dv = getdisk(buf, len, 1, &ndumpdev, 1);
380 1.221 mlelstv if (dv != NULL) {
381 1.221 mlelstv dumpdv = dv;
382 1.221 mlelstv break;
383 1.26 thorpej }
384 1.221 mlelstv }
385 1.221 mlelstv dumpdev = ndumpdev;
386 1.26 thorpej
387 1.221 mlelstv for (vops = LIST_FIRST(&vfs_list); vops != NULL;
388 1.221 mlelstv vops = LIST_NEXT(vops, vfs_list)) {
389 1.221 mlelstv if (vops->vfs_mountroot != NULL &&
390 1.221 mlelstv strcmp(rootfstype, vops->vfs_name) == 0)
391 1.221 mlelstv break;
392 1.221 mlelstv }
393 1.221 mlelstv
394 1.221 mlelstv if (vops == NULL) {
395 1.221 mlelstv deffsname = "generic";
396 1.221 mlelstv } else
397 1.221 mlelstv deffsname = vops->vfs_name;
398 1.18 thorpej
399 1.221 mlelstv for (;;) {
400 1.221 mlelstv printf("file system (default %s): ", deffsname);
401 1.221 mlelstv len = cngetsn(buf, sizeof(buf));
402 1.221 mlelstv if (len == 0) {
403 1.221 mlelstv if (strcmp(deffsname, "generic") == 0)
404 1.221 mlelstv rootfstype = ROOT_FSTYPE_ANY;
405 1.38 fvdl break;
406 1.18 thorpej }
407 1.221 mlelstv if (len == 4 && strcmp(buf, "halt") == 0)
408 1.228 thorpej kern_reboot(RB_HALT, NULL);
409 1.221 mlelstv else if (len == 6 && strcmp(buf, "reboot") == 0)
410 1.228 thorpej kern_reboot(0, NULL);
411 1.78 thorpej #if defined(DDB)
412 1.221 mlelstv else if (len == 3 && strcmp(buf, "ddb") == 0) {
413 1.221 mlelstv console_debugger();
414 1.221 mlelstv }
415 1.78 thorpej #endif
416 1.221 mlelstv else if (len == 7 && strcmp(buf, "generic") == 0) {
417 1.221 mlelstv rootfstype = ROOT_FSTYPE_ANY;
418 1.221 mlelstv break;
419 1.221 mlelstv }
420 1.221 mlelstv vops = vfs_getopsbyname(buf);
421 1.221 mlelstv if (vops == NULL || vops->vfs_mountroot == NULL) {
422 1.221 mlelstv printf("use one of: generic");
423 1.221 mlelstv for (vops = LIST_FIRST(&vfs_list);
424 1.221 mlelstv vops != NULL;
425 1.221 mlelstv vops = LIST_NEXT(vops, vfs_list)) {
426 1.221 mlelstv if (vops->vfs_mountroot != NULL)
427 1.221 mlelstv printf(" %s", vops->vfs_name);
428 1.18 thorpej }
429 1.221 mlelstv if (vops != NULL)
430 1.221 mlelstv vfs_delref(vops);
431 1.78 thorpej #if defined(DDB)
432 1.221 mlelstv printf(" ddb");
433 1.78 thorpej #endif
434 1.221 mlelstv printf(" halt reboot\n");
435 1.221 mlelstv } else {
436 1.221 mlelstv /*
437 1.221 mlelstv * XXX If *vops gets freed between here and
438 1.221 mlelstv * the call to mountroot(), rootfstype will
439 1.221 mlelstv * point to something unexpected. But in
440 1.221 mlelstv * this case the system will fail anyway.
441 1.221 mlelstv */
442 1.221 mlelstv rootfstype = vops->vfs_name;
443 1.221 mlelstv vfs_delref(vops);
444 1.221 mlelstv break;
445 1.18 thorpej }
446 1.221 mlelstv }
447 1.221 mlelstv
448 1.221 mlelstv root_device = rootdv;
449 1.221 mlelstv setroot_dump(root_device, dumpdv);
450 1.221 mlelstv }
451 1.221 mlelstv
452 1.221 mlelstv /*
453 1.221 mlelstv * configure
454 1.224 mlelstv * device_t root_device
455 1.224 mlelstv * dev_t rootdev (for disks)
456 1.221 mlelstv *
457 1.221 mlelstv */
458 1.221 mlelstv static void
459 1.221 mlelstv setroot_root(device_t bootdv, int bootpartition)
460 1.221 mlelstv {
461 1.221 mlelstv device_t rootdv;
462 1.221 mlelstv int majdev;
463 1.221 mlelstv const char *rootdevname;
464 1.221 mlelstv char buf[128];
465 1.224 mlelstv dev_t nrootdev;
466 1.221 mlelstv
467 1.221 mlelstv if (rootspec == NULL) {
468 1.18 thorpej
469 1.18 thorpej /*
470 1.18 thorpej * Wildcarded root; use the boot device.
471 1.18 thorpej */
472 1.26 thorpej rootdv = bootdv;
473 1.26 thorpej
474 1.188 christos if (bootdv)
475 1.188 christos majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
476 1.188 christos else
477 1.188 christos majdev = -1;
478 1.18 thorpej if (majdev >= 0) {
479 1.18 thorpej /*
480 1.113 thorpej * Root is on a disk. `bootpartition' is root,
481 1.113 thorpej * unless the device does not use partitions.
482 1.18 thorpej */
483 1.113 thorpej if (DEV_USES_PARTITIONS(bootdv))
484 1.135 thorpej rootdev = MAKEDISKDEV(majdev,
485 1.135 thorpej device_unit(bootdv),
486 1.135 thorpej bootpartition);
487 1.113 thorpej else
488 1.135 thorpej rootdev = makedev(majdev, device_unit(bootdv));
489 1.18 thorpej }
490 1.18 thorpej } else {
491 1.18 thorpej
492 1.18 thorpej /*
493 1.25 mrg * `root on <dev> ...'
494 1.18 thorpej */
495 1.18 thorpej
496 1.18 thorpej /*
497 1.224 mlelstv * If rootspec can be parsed, just use it.
498 1.18 thorpej */
499 1.224 mlelstv rootdv = parsedisk(rootspec, strlen(rootspec), 0, &nrootdev);
500 1.224 mlelstv if (rootdv != NULL) {
501 1.224 mlelstv rootdev = nrootdev;
502 1.220 mlelstv goto haveroot;
503 1.220 mlelstv }
504 1.159 dyoung
505 1.224 mlelstv /*
506 1.224 mlelstv * Fall back to rootdev, compute rootdv for it
507 1.224 mlelstv */
508 1.88 gehenna rootdevname = devsw_blk2name(major(rootdev));
509 1.18 thorpej if (rootdevname == NULL) {
510 1.198 christos printf("unknown device major 0x%llx\n",
511 1.198 christos (unsigned long long)rootdev);
512 1.221 mlelstv return;
513 1.18 thorpej }
514 1.41 perry memset(buf, 0, sizeof(buf));
515 1.198 christos snprintf(buf, sizeof(buf), "%s%llu", rootdevname,
516 1.198 christos (unsigned long long)DISKUNIT(rootdev));
517 1.18 thorpej
518 1.56 enami rootdv = finddevice(buf);
519 1.26 thorpej if (rootdv == NULL) {
520 1.198 christos printf("device %s (0x%llx) not configured\n",
521 1.198 christos buf, (unsigned long long)rootdev);
522 1.221 mlelstv return;
523 1.18 thorpej }
524 1.26 thorpej }
525 1.18 thorpej
526 1.221 mlelstv haveroot:
527 1.124 thorpej switch (device_class(rootdv)) {
528 1.18 thorpej case DV_IFNET:
529 1.139 christos case DV_DISK:
530 1.184 cegger aprint_normal("root on %s", device_xname(rootdv));
531 1.140 christos if (DEV_USES_PARTITIONS(rootdv))
532 1.198 christos aprint_normal("%c", (int)DISKPART(rootdev) + 'a');
533 1.18 thorpej break;
534 1.18 thorpej default:
535 1.18 thorpej printf("can't determine root device\n");
536 1.221 mlelstv return;
537 1.18 thorpej }
538 1.26 thorpej
539 1.221 mlelstv root_device = rootdv;
540 1.221 mlelstv setroot_dump(rootdv, NULL);
541 1.221 mlelstv }
542 1.221 mlelstv
543 1.221 mlelstv /*
544 1.221 mlelstv * configure
545 1.221 mlelstv * dev_t dumpdev
546 1.221 mlelstv * dev_t dumpcdev
547 1.221 mlelstv *
548 1.221 mlelstv * set to NODEV if device not found
549 1.221 mlelstv */
550 1.221 mlelstv static void
551 1.221 mlelstv setroot_dump(device_t rootdv, device_t dumpdv)
552 1.221 mlelstv {
553 1.221 mlelstv device_t dv;
554 1.221 mlelstv deviter_t di;
555 1.221 mlelstv const char *dumpdevname;
556 1.221 mlelstv int majdev;
557 1.221 mlelstv char buf[128];
558 1.221 mlelstv
559 1.26 thorpej /*
560 1.26 thorpej * Now configure the dump device.
561 1.55 enami *
562 1.26 thorpej * If we haven't figured out the dump device, do so, with
563 1.26 thorpej * the following rules:
564 1.26 thorpej *
565 1.26 thorpej * (a) We already know dumpdv in the RB_ASKNAME case.
566 1.26 thorpej *
567 1.26 thorpej * (b) If dumpspec is set, try to use it. If the device
568 1.26 thorpej * is not available, punt.
569 1.26 thorpej *
570 1.26 thorpej * (c) If dumpspec is not set, the dump device is
571 1.26 thorpej * wildcarded or unspecified. If the root device
572 1.26 thorpej * is DV_IFNET, punt. Otherwise, use partition b
573 1.26 thorpej * of the root device.
574 1.26 thorpej */
575 1.26 thorpej
576 1.55 enami if (boothowto & RB_ASKNAME) { /* (a) */
577 1.55 enami if (dumpdv == NULL)
578 1.55 enami goto nodumpdev;
579 1.55 enami } else if (dumpspec != NULL) { /* (b) */
580 1.55 enami if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
581 1.26 thorpej /*
582 1.55 enami * Operator doesn't want a dump device.
583 1.55 enami * Or looks like they tried to pick a network
584 1.26 thorpej * device. Oops.
585 1.26 thorpej */
586 1.26 thorpej goto nodumpdev;
587 1.26 thorpej }
588 1.26 thorpej
589 1.88 gehenna dumpdevname = devsw_blk2name(major(dumpdev));
590 1.26 thorpej if (dumpdevname == NULL)
591 1.26 thorpej goto nodumpdev;
592 1.41 perry memset(buf, 0, sizeof(buf));
593 1.198 christos snprintf(buf, sizeof(buf), "%s%llu", dumpdevname,
594 1.198 christos (unsigned long long)DISKUNIT(dumpdev));
595 1.26 thorpej
596 1.56 enami dumpdv = finddevice(buf);
597 1.56 enami if (dumpdv == NULL) {
598 1.26 thorpej /*
599 1.26 thorpej * Device not configured.
600 1.26 thorpej */
601 1.26 thorpej goto nodumpdev;
602 1.26 thorpej }
603 1.55 enami } else { /* (c) */
604 1.160 christos if (DEV_USES_PARTITIONS(rootdv) == 0) {
605 1.203 dyoung for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
606 1.203 dyoung dv != NULL;
607 1.203 dyoung dv = deviter_next(&di))
608 1.160 christos if (isswap(dv))
609 1.160 christos break;
610 1.203 dyoung deviter_release(&di);
611 1.160 christos if (dv == NULL)
612 1.160 christos goto nodumpdev;
613 1.160 christos
614 1.184 cegger majdev = devsw_name2blk(device_xname(dv), NULL, 0);
615 1.160 christos if (majdev < 0)
616 1.160 christos goto nodumpdev;
617 1.160 christos dumpdv = dv;
618 1.160 christos dumpdev = makedev(majdev, device_unit(dumpdv));
619 1.160 christos } else {
620 1.55 enami dumpdv = rootdv;
621 1.55 enami dumpdev = MAKEDISKDEV(major(rootdev),
622 1.135 thorpej device_unit(dumpdv), 1);
623 1.55 enami }
624 1.26 thorpej }
625 1.26 thorpej
626 1.175 ad dumpcdev = devsw_blk2chr(dumpdev);
627 1.184 cegger aprint_normal(" dumps on %s", device_xname(dumpdv));
628 1.140 christos if (DEV_USES_PARTITIONS(dumpdv))
629 1.198 christos aprint_normal("%c", (int)DISKPART(dumpdev) + 'a');
630 1.140 christos aprint_normal("\n");
631 1.26 thorpej return;
632 1.26 thorpej
633 1.26 thorpej nodumpdev:
634 1.26 thorpej dumpdev = NODEV;
635 1.175 ad dumpcdev = NODEV;
636 1.100 thorpej aprint_normal("\n");
637 1.18 thorpej }
638 1.18 thorpej
639 1.200 dyoung static device_t
640 1.117 thorpej finddevice(const char *name)
641 1.56 enami {
642 1.159 dyoung const char *wname;
643 1.108 christos
644 1.159 dyoung if ((wname = getwedgename(name, strlen(name))) != NULL)
645 1.159 dyoung return dkwedge_find_by_wname(wname);
646 1.159 dyoung
647 1.179 joerg return device_find_by_xname(name);
648 1.56 enami }
649 1.56 enami
650 1.200 dyoung static device_t
651 1.224 mlelstv getdisk(const char *str, int len, int defpart, dev_t *devp, int isdump)
652 1.18 thorpej {
653 1.200 dyoung device_t dv;
654 1.203 dyoung deviter_t di;
655 1.18 thorpej
656 1.224 mlelstv if (len == 4 && strcmp(str, "halt") == 0)
657 1.228 thorpej kern_reboot(RB_HALT, NULL);
658 1.224 mlelstv else if (len == 6 && strcmp(str, "reboot") == 0)
659 1.228 thorpej kern_reboot(0, NULL);
660 1.224 mlelstv #if defined(DDB)
661 1.224 mlelstv else if (len == 3 && strcmp(str, "ddb") == 0) {
662 1.224 mlelstv console_debugger();
663 1.224 mlelstv return NULL;
664 1.224 mlelstv }
665 1.224 mlelstv #endif
666 1.224 mlelstv
667 1.51 thorpej if ((dv = parsedisk(str, len, defpart, devp)) == NULL) {
668 1.18 thorpej printf("use one of:");
669 1.203 dyoung for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST); dv != NULL;
670 1.203 dyoung dv = deviter_next(&di)) {
671 1.113 thorpej if (DEV_USES_PARTITIONS(dv))
672 1.184 cegger printf(" %s[a-%c]", device_xname(dv),
673 1.19 cgd 'a' + MAXPARTITIONS - 1);
674 1.124 thorpej else if (device_class(dv) == DV_DISK)
675 1.184 cegger printf(" %s", device_xname(dv));
676 1.124 thorpej if (isdump == 0 && device_class(dv) == DV_IFNET)
677 1.184 cegger printf(" %s", device_xname(dv));
678 1.18 thorpej }
679 1.203 dyoung deviter_release(&di);
680 1.159 dyoung dkwedge_print_wnames();
681 1.26 thorpej if (isdump)
682 1.26 thorpej printf(" none");
683 1.78 thorpej #if defined(DDB)
684 1.78 thorpej printf(" ddb");
685 1.78 thorpej #endif
686 1.76 thorpej printf(" halt reboot\n");
687 1.18 thorpej }
688 1.159 dyoung return dv;
689 1.159 dyoung }
690 1.159 dyoung
691 1.159 dyoung static const char *
692 1.159 dyoung getwedgename(const char *name, int namelen)
693 1.159 dyoung {
694 1.227 manu const char *wpfx1 = "wedge:";
695 1.227 manu const char *wpfx2 = "NAME=";
696 1.227 manu const int wpfx1len = strlen(wpfx1);
697 1.227 manu const int wpfx2len = strlen(wpfx2);
698 1.159 dyoung
699 1.227 manu if (namelen > wpfx1len && strncmp(name, wpfx1, wpfx1len) == 0)
700 1.227 manu return name + wpfx1len;
701 1.227 manu
702 1.227 manu if (namelen > wpfx2len && strncasecmp(name, wpfx2, wpfx2len) == 0)
703 1.227 manu return name + wpfx2len;
704 1.159 dyoung
705 1.227 manu return NULL;
706 1.18 thorpej }
707 1.18 thorpej
708 1.200 dyoung static device_t
709 1.224 mlelstv parsedisk(const char *str, int len, int defpart, dev_t *devp)
710 1.18 thorpej {
711 1.200 dyoung device_t dv;
712 1.159 dyoung const char *wname;
713 1.224 mlelstv char c;
714 1.29 drochner int majdev, part;
715 1.224 mlelstv char xname[16]; /* same size as dv_xname */
716 1.224 mlelstv
717 1.18 thorpej if (len == 0)
718 1.18 thorpej return (NULL);
719 1.18 thorpej
720 1.159 dyoung if ((wname = getwedgename(str, len)) != NULL) {
721 1.159 dyoung if ((dv = dkwedge_find_by_wname(wname)) == NULL)
722 1.159 dyoung return NULL;
723 1.159 dyoung part = defpart;
724 1.159 dyoung goto gotdisk;
725 1.224 mlelstv }
726 1.224 mlelstv
727 1.224 mlelstv c = str[len-1];
728 1.224 mlelstv if (c >= 'a' && c <= ('a' + MAXPARTITIONS - 1)) {
729 1.18 thorpej part = c - 'a';
730 1.224 mlelstv len--;
731 1.224 mlelstv if (len > sizeof(xname)-1)
732 1.224 mlelstv return NULL;
733 1.224 mlelstv memcpy(xname, str, len);
734 1.224 mlelstv xname[len] = '\0';
735 1.224 mlelstv str = xname;
736 1.18 thorpej } else
737 1.18 thorpej part = defpart;
738 1.18 thorpej
739 1.56 enami dv = finddevice(str);
740 1.56 enami if (dv != NULL) {
741 1.124 thorpej if (device_class(dv) == DV_DISK) {
742 1.18 thorpej gotdisk:
743 1.184 cegger majdev = devsw_name2blk(device_xname(dv), NULL, 0);
744 1.18 thorpej if (majdev < 0)
745 1.18 thorpej panic("parsedisk");
746 1.113 thorpej if (DEV_USES_PARTITIONS(dv))
747 1.135 thorpej *devp = MAKEDISKDEV(majdev, device_unit(dv),
748 1.135 thorpej part);
749 1.113 thorpej else
750 1.135 thorpej *devp = makedev(majdev, device_unit(dv));
751 1.18 thorpej }
752 1.18 thorpej
753 1.124 thorpej if (device_class(dv) == DV_IFNET)
754 1.18 thorpej *devp = NODEV;
755 1.18 thorpej }
756 1.18 thorpej
757 1.18 thorpej return (dv);
758 1.48 lukem }
759