/*-
* Copyright (c) 1997, 1998, 1999, 2002, 2007, 2008 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
* NASA Ames Research Center, and by Luke Mewburn.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1982, 1986, 1991, 1993
* The Regents of the University of California. All rights reserved.
* (c) UNIX System Laboratories, Inc.
* All or some portions of this file are derived from material licensed
* to the University of California by American Telephone and Telegraph
* Co. or Unix System Laboratories, Inc. and are reproduced herein with
* the permission of UNIX System Laboratories, Inc.
*
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This software was developed by the Computer Systems Engineering group
* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
* contributed to Berkeley.
*
* All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Lawrence Berkeley Laboratory.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)kern_subr.c 8.4 (Berkeley) 2/14/95
*/
#ifdef TFTPROOT
int tftproot_dhcpboot(device_t);
#endif
dev_t dumpcdev; /* for savecore */
static int
isswap(device_t dv)
{
struct dkwedge_info wi;
struct vnode *vn;
int error;
if (device_class(dv) != DV_DISK || !device_is_a(dv, "dk"))
return 0;
if ((vn = opendisk(dv)) == NULL)
return 0;
error = VOP_IOCTL(vn, DIOCGWEDGEINFO, &wi, FREAD, NOCRED);
VOP_CLOSE(vn, FREAD, NOCRED);
vput(vn);
if (error) {
#ifdef DEBUG_WEDGE
printf("%s: Get wedge info returned %d\n", device_xname(dv), error);
#endif
return 0;
}
return strcmp(wi.dkw_ptype, DKW_PTYPE_SWAP) == 0;
}
/*
* Determine the root device and, if instructed to, the root file system.
*/
#ifdef MEMORY_DISK_IS_ROOT
int md_is_root = 1;
#else
int md_is_root = 0;
#endif
/*
* The device and partition that we booted from.
*
* This data might be initialized by MD code, but is defined here.
*/
device_t booted_device;
const char *booted_method;
int booted_partition;
daddr_t booted_startblk;
uint64_t booted_nblks;
char *bootspec;
/*
* Use partition letters if it's a disk class but not a wedge or flash.
* XXX Check for wedge/flash is kinda gross.
*/
#define DEV_USES_PARTITIONS(dv) \
(device_class((dv)) == DV_DISK && \
!device_is_a((dv), "dk") && \
!device_is_a((dv), "flash"))
void
setroot(device_t bootdv, int bootpartition)
{
/*
* Let bootcode augment "rootspec".
*/
if (rootspec == NULL)
rootspec = bootspec;
/*
* force boot device to md0
*/
if (md_is_root)
rootspec = "md0";
#ifdef TFTPROOT
/*
* XXX
* if rootspec specifies an interface
* sets root_device to that interface
* reuses NFS init code to set up network
* fetch image into ram disk
*
* if successful, we change rootspec
*/
if (tftproot_dhcpboot(bootdv) == 0)
rootspec = "md0";
#endif
/*
* quirk for
* evbarm/mini2440
* hpcarm
* hpcmips
* hpcsh
*
* if rootfstype is set to NFS and the
* kernel supports NFS and the boot device
* is unknown or not a network interface
* -> chose the first network interface you find
*
* hp300 has similar MD code
*/
setroot_nfs(bootdv);
/*
* If no bootdv was found by MD code and no
* root specified ask the user.
*/
if (rootspec == NULL && bootdv == NULL)
boothowto |= RB_ASKNAME;
/*
* loop until a root device is specified
*/
do {
if (boothowto & RB_ASKNAME)
setroot_ask(bootdv, bootpartition);
else
setroot_root(bootdv, bootpartition);
if (root_device == NULL)
boothowto |= RB_ASKNAME;
} while (root_device == NULL);
}
/*
* If NFS is specified as the file system, and we found
* a DV_DISK boot device (or no boot device at all), then
* find a reasonable network interface for "rootspec".
*/
static void
setroot_nfs(device_t dv)
{
struct vfsops *vops;
struct ifnet *ifp;
vops = vfs_getopsbyname(MOUNT_NFS);
if (vops != NULL && strcmp(rootfstype, MOUNT_NFS) == 0 &&
rootspec == NULL &&
(dv == NULL || device_class(dv) != DV_IFNET)) {
int s = pserialize_read_enter();
IFNET_READER_FOREACH(ifp) {
if ((ifp->if_flags &
(IFF_LOOPBACK|IFF_POINTOPOINT)) == 0)
break;
}
if (ifp != NULL) {
/*
* Have a suitable interface; behave as if
* the user specified this interface.
*/
rootspec = (const char *)ifp->if_xname;
}
pserialize_read_exit(s);
}
if (vops != NULL)
vfs_delref(vops);
}
for (;;) {
printf("root device");
if (bootdv != NULL) {
printf(" (default %s", device_xname(bootdv));
if (DEV_USES_PARTITIONS(bootdv))
printf("%c", bootpartition + 'a');
printf(")");
}
printf(": ");
len = cngetsn(buf, sizeof(buf));
if (len == 0 && bootdv != NULL) {
strlcpy(buf, device_xname(bootdv), sizeof(buf));
len = strlen(buf);
}
if (len > 0 && buf[len - 1] == '*') {
buf[--len] = '\0';
dv = getdisk(buf, len, 1, &nrootdev, 0);
if (dv != NULL) {
rootdv = dv;
break;
}
}
dv = getdisk(buf, len, bootpartition, &nrootdev, 0);
if (dv != NULL) {
rootdv = dv;
break;
}
}
rootdev = nrootdev;
/*
* Set up the default dump device. If root is on
* a network device or a disk without partitions,
* there is no default dump device.
*/
if (DEV_USES_PARTITIONS(rootdv) == 0)
defdumpdv = NULL;
else
defdumpdv = rootdv;
ndumpdev = NODEV;
for (;;) {
printf("dump device");
if (defdumpdv != NULL) {
/*
* Note, we know it's a disk if we get here.
*/
printf(" (default %sb)", device_xname(defdumpdv));
}
printf(": ");
len = cngetsn(buf, sizeof(buf));
if (len == 0) {
if (defdumpdv != NULL) {
ndumpdev = MAKEDISKDEV(major(nrootdev),
DISKUNIT(nrootdev), 1);
}
dumpdv = defdumpdv;
break;
}
if (len == 4 && strcmp(buf, "none") == 0) {
dumpdv = NULL;
break;
}
dv = getdisk(buf, len, 1, &ndumpdev, 1);
if (dv != NULL) {
dumpdv = dv;
break;
}
}
dumpdev = ndumpdev;
/*
* configure
* dev_t rootdev
*
* return device_t or NULL if not found
*/
static void
setroot_root(device_t bootdv, int bootpartition)
{
device_t rootdv;
int majdev;
const char *rootdevname;
char buf[128];
if (rootspec == NULL) {
/*
* Wildcarded root; use the boot device.
*/
rootdv = bootdv;
if (bootdv)
majdev = devsw_name2blk(device_xname(bootdv), NULL, 0);
else
majdev = -1;
if (majdev >= 0) {
/*
* Root is on a disk. `bootpartition' is root,
* unless the device does not use partitions.
*/
if (DEV_USES_PARTITIONS(bootdv))
rootdev = MAKEDISKDEV(majdev,
device_unit(bootdv),
bootpartition);
else
rootdev = makedev(majdev, device_unit(bootdv));
}
} else {
/*
* `root on <dev> ...'
*/
/*
* If it's a network interface, we can bail out
* early.
*/
rootdv = finddevice(rootspec);
if (rootdv != NULL && device_class(rootdv) == DV_IFNET)
goto haveroot;
/*
* configure
* dev_t dumpdev
* dev_t dumpcdev
*
* set to NODEV if device not found
*/
static void
setroot_dump(device_t rootdv, device_t dumpdv)
{
device_t dv;
deviter_t di;
const char *dumpdevname;
int majdev;
char buf[128];
/*
* Now configure the dump device.
*
* If we haven't figured out the dump device, do so, with
* the following rules:
*
* (a) We already know dumpdv in the RB_ASKNAME case.
*
* (b) If dumpspec is set, try to use it. If the device
* is not available, punt.
*
* (c) If dumpspec is not set, the dump device is
* wildcarded or unspecified. If the root device
* is DV_IFNET, punt. Otherwise, use partition b
* of the root device.
*/
if (boothowto & RB_ASKNAME) { /* (a) */
if (dumpdv == NULL)
goto nodumpdev;
} else if (dumpspec != NULL) { /* (b) */
if (strcmp(dumpspec, "none") == 0 || dumpdev == NODEV) {
/*
* Operator doesn't want a dump device.
* Or looks like they tried to pick a network
* device. Oops.
*/
goto nodumpdev;
}
dumpdevname = devsw_blk2name(major(dumpdev));
if (dumpdevname == NULL)
goto nodumpdev;
memset(buf, 0, sizeof(buf));
snprintf(buf, sizeof(buf), "%s%llu", dumpdevname,
(unsigned long long)DISKUNIT(dumpdev));
dumpdv = finddevice(buf);
if (dumpdv == NULL) {
/*
* Device not configured.
*/
goto nodumpdev;
}
} else { /* (c) */
if (DEV_USES_PARTITIONS(rootdv) == 0) {
for (dv = deviter_first(&di, DEVITER_F_ROOT_FIRST);
dv != NULL;
dv = deviter_next(&di))
if (isswap(dv))
break;
deviter_release(&di);
if (dv == NULL)
goto nodumpdev;