/* $NetBSD: devopen.c,v 1.14 2023/06/20 07:46:03 rin Exp $ */
/*-
* Copyright (c) 2005 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Bang Jun-Young.
*
* 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) 1996, 1997
* Matthias Drochner. All rights reserved.
*
* 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 AUTHOR ``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 AUTHOR 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.
*/
/* Search by GPT label or raidframe name */
if (strstr(devname, "NAME=") == devname)
xname = devname;
if (strstr(devname, "raid") == devname)
xname = fname;
if (xname != NULL) {
f->f_dev = &devsw[0]; /* must be biosdisk */
if (!kernel_loaded) {
strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
}
/*
* Network
*/
#if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
nf = netboot_fstab_find(devname);
if (nf != NULL) {
n = 0;
if (strcmp(devname, "net") == 0) {
for (i = 0; i < nnetboot_fstab; i++) {
memcpy(&file_system[n++], netboot_fstab[i].ops,
sizeof(struct fs_ops));
}
} else {
memcpy(&file_system[n++], nf->ops,
sizeof(struct fs_ops));
}
nfsys = n;
#ifdef SUPPORT_BOOTP
try_bootp = 1;
#endif
/* If we got passed a filename, pass it to the BOOTP server. */
if (fname) {
filename = strchr(fname, ':');
if (filename != NULL)
filename++;
else
filename = (char *)fname;
strlcpy(bootfile, filename, sizeof(bootfile));
}
f->f_dev = &devsw[1]; /* must be net */
if (!kernel_loaded) {
strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
}
error = DEV_OPEN(f->f_dev)(f, &desc);
if (error)
return error;
/*
* If the DHCP server provided a file name:
* - If it contains a ":", assume it points to a NetBSD kernel.
* - If not, assume that the DHCP server was not able to pass
* a separate filename for the kernel. (The name probably was
* the same as used to load "efiboot".) Ignore it and use
* the default in this case.
* So we cater to simple DHCP servers while being able to use
* the power of conditional behaviour in modern ones.
*/
filename = strchr(bootfile, ':');
if (filename != NULL) {
fname = bootfile;
fsnamelen = filename - fname;
nf = netboot_fstab_findn(fname, fsnamelen);
if (nf == NULL ||
strncmp(fname, "net", fsnamelen) == 0) {
printf("Invalid file system type specified in "
"%s\n", fname);
error = EINVAL;
goto neterr;
}