Home | History | Annotate | Line # | Download | only in efiboot
devopen.c revision 1.1.12.5
      1  1.1.12.5  martin /*	$NetBSD: devopen.c,v 1.1.12.5 2019/09/17 18:26:53 martin Exp $	 */
      2       1.1  nonaka 
      3       1.1  nonaka /*-
      4       1.1  nonaka  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5       1.1  nonaka  * All rights reserved.
      6       1.1  nonaka  *
      7       1.1  nonaka  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1  nonaka  * by Bang Jun-Young.
      9       1.1  nonaka  *
     10       1.1  nonaka  * Redistribution and use in source and binary forms, with or without
     11       1.1  nonaka  * modification, are permitted provided that the following conditions
     12       1.1  nonaka  * are met:
     13       1.1  nonaka  * 1. Redistributions of source code must retain the above copyright
     14       1.1  nonaka  *    notice, this list of conditions and the following disclaimer.
     15       1.1  nonaka  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1  nonaka  *    notice, this list of conditions and the following disclaimer in the
     17       1.1  nonaka  *    documentation and/or other materials provided with the distribution.
     18       1.1  nonaka  *
     19       1.1  nonaka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20       1.1  nonaka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21       1.1  nonaka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22       1.1  nonaka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23       1.1  nonaka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24       1.1  nonaka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25       1.1  nonaka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26       1.1  nonaka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27       1.1  nonaka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28       1.1  nonaka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29       1.1  nonaka  * POSSIBILITY OF SUCH DAMAGE.
     30       1.1  nonaka  */
     31       1.1  nonaka 
     32       1.1  nonaka /*
     33       1.1  nonaka  * Copyright (c) 1996, 1997
     34       1.1  nonaka  *	Matthias Drochner.  All rights reserved.
     35       1.1  nonaka  *
     36       1.1  nonaka  * Redistribution and use in source and binary forms, with or without
     37       1.1  nonaka  * modification, are permitted provided that the following conditions
     38       1.1  nonaka  * are met:
     39       1.1  nonaka  * 1. Redistributions of source code must retain the above copyright
     40       1.1  nonaka  *    notice, this list of conditions and the following disclaimer.
     41       1.1  nonaka  * 2. Redistributions in binary form must reproduce the above copyright
     42       1.1  nonaka  *    notice, this list of conditions and the following disclaimer in the
     43       1.1  nonaka  *    documentation and/or other materials provided with the distribution.
     44       1.1  nonaka  *
     45       1.1  nonaka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     46       1.1  nonaka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     47       1.1  nonaka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     48       1.1  nonaka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     49       1.1  nonaka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     50       1.1  nonaka  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     51       1.1  nonaka  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     52       1.1  nonaka  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     53       1.1  nonaka  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     54       1.1  nonaka  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     55       1.1  nonaka  */
     56       1.1  nonaka 
     57       1.1  nonaka #include "efiboot.h"
     58       1.1  nonaka 
     59  1.1.12.4  martin #include <lib/libsa/dev_net.h>
     60  1.1.12.4  martin 
     61       1.1  nonaka #include <biosdisk.h>
     62       1.1  nonaka #include "devopen.h"
     63       1.1  nonaka #include <bootinfo.h>
     64  1.1.12.3  martin #include "efidisk.h"
     65       1.1  nonaka 
     66       1.1  nonaka static int
     67       1.1  nonaka dev2bios(char *devname, int unit, int *biosdev)
     68       1.1  nonaka {
     69       1.1  nonaka 
     70       1.1  nonaka 	if (strcmp(devname, "hd") == 0)
     71       1.1  nonaka 		*biosdev = 0x80 + unit;
     72  1.1.12.2  martin 	else if (strcmp(devname, "cd") == 0)
     73  1.1.12.1  martin 		*biosdev = 0x80 + get_harddrives() + unit;
     74       1.1  nonaka 	else
     75       1.1  nonaka 		return ENXIO;
     76  1.1.12.1  martin 
     77       1.1  nonaka 	return 0;
     78       1.1  nonaka }
     79       1.1  nonaka 
     80       1.1  nonaka void
     81  1.1.12.5  martin bios2dev(int biosdev, daddr_t sector, char **devname, int *unit,
     82  1.1.12.5  martin 	 int *partition, const char **part_name)
     83       1.1  nonaka {
     84  1.1.12.5  martin 	static char savedevname[MAXDEVNAME+1];
     85       1.1  nonaka 
     86       1.1  nonaka 	*unit = biosdev & 0x7f;
     87  1.1.12.1  martin 
     88  1.1.12.4  martin 	if (efi_bootdp_type == BOOT_DEVICE_TYPE_NET) {
     89  1.1.12.4  martin 		*devname = "net";
     90  1.1.12.4  martin 		*unit = efi_net_get_booted_interface_unit();
     91  1.1.12.4  martin 		if (*unit < 0)
     92  1.1.12.4  martin 			*unit = 0;
     93  1.1.12.4  martin 		*partition = 0;
     94  1.1.12.4  martin 		return;
     95  1.1.12.4  martin 	} else if (biosdev >= 0x80 + get_harddrives()) {
     96  1.1.12.1  martin 		*devname = "cd";
     97  1.1.12.1  martin 		*unit -= get_harddrives();
     98  1.1.12.1  martin 	} else
     99  1.1.12.1  martin 		*devname = "hd";
    100  1.1.12.1  martin 
    101  1.1.12.5  martin 	(void)biosdisk_findpartition(biosdev, sector, partition, part_name);
    102  1.1.12.5  martin 	if (*part_name != NULL) {
    103  1.1.12.5  martin 		snprintf(savedevname, sizeof(savedevname),
    104  1.1.12.5  martin 		    "NAME=%s", *part_name);
    105  1.1.12.5  martin 			*devname = savedevname;
    106  1.1.12.5  martin 	}
    107       1.1  nonaka }
    108       1.1  nonaka 
    109       1.1  nonaka struct btinfo_bootpath bibp;
    110       1.1  nonaka extern bool kernel_loaded;
    111       1.1  nonaka 
    112       1.1  nonaka /*
    113       1.1  nonaka  * Open the EFI disk device
    114       1.1  nonaka  */
    115       1.1  nonaka int
    116       1.1  nonaka devopen(struct open_file *f, const char *fname, char **file)
    117       1.1  nonaka {
    118  1.1.12.4  martin #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
    119  1.1.12.4  martin 	static const char *net_devnames[] = {
    120  1.1.12.4  martin #if defined(SUPPORT_NFS)
    121  1.1.12.4  martin 	    "nfs",
    122  1.1.12.4  martin #endif
    123  1.1.12.4  martin #if defined(SUPPORT_TFTP)
    124  1.1.12.4  martin 	    "tftp",
    125  1.1.12.4  martin #endif
    126  1.1.12.4  martin 	};
    127  1.1.12.4  martin #endif
    128  1.1.12.4  martin 	struct devdesc desc;
    129  1.1.12.4  martin 	struct devsw *dev;
    130       1.1  nonaka 	char *fsname, *devname;
    131       1.1  nonaka 	int unit, partition;
    132       1.1  nonaka 	int biosdev;
    133  1.1.12.4  martin 	int i, n, error;
    134       1.1  nonaka 
    135  1.1.12.3  martin 	error = parsebootfile(fname, &fsname, &devname, &unit, &partition,
    136  1.1.12.3  martin 	    (const char **) file);
    137  1.1.12.3  martin 	if (error)
    138  1.1.12.3  martin 		return error;
    139  1.1.12.3  martin 
    140  1.1.12.5  martin 	/* Search by GPT label or raidframe name */
    141  1.1.12.5  martin 	if ((strstr(devname, "NAME=") == devname) ||
    142  1.1.12.5  martin 	    (strstr(devname, "raid") == devname)) {
    143  1.1.12.5  martin 		f->f_dev = &devsw[0];		/* must be biosdisk */
    144  1.1.12.5  martin 
    145  1.1.12.5  martin 		if (!kernel_loaded) {
    146  1.1.12.5  martin 			strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
    147  1.1.12.5  martin 			BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
    148  1.1.12.5  martin 		}
    149  1.1.12.5  martin 
    150  1.1.12.5  martin 		error = biosdisk_open_name(f, devname);
    151  1.1.12.5  martin 		return error;
    152  1.1.12.5  martin 	}
    153  1.1.12.5  martin 
    154  1.1.12.4  martin 	memcpy(file_system, file_system_disk, sizeof(*file_system) * nfsys);
    155  1.1.12.4  martin 	nfsys = nfsys_disk;
    156  1.1.12.4  martin 
    157  1.1.12.4  martin #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
    158  1.1.12.4  martin 	for (i = 0; i < __arraycount(net_devnames); i++) {
    159  1.1.12.4  martin 		if (strcmp(devname, net_devnames[i]) == 0) {
    160  1.1.12.4  martin 			fsname = devname;
    161  1.1.12.4  martin 			devname = "net";
    162  1.1.12.4  martin 			break;
    163  1.1.12.4  martin 		}
    164  1.1.12.4  martin 	}
    165  1.1.12.4  martin #endif
    166  1.1.12.4  martin 
    167  1.1.12.4  martin 	for (i = 1; i < ndevs; i++) {
    168  1.1.12.4  martin 		dev = &devsw[i];
    169  1.1.12.4  martin 		if (strcmp(devname, DEV_NAME(dev)) == 0) {
    170  1.1.12.4  martin 			if (strcmp(devname, "net") == 0) {
    171  1.1.12.4  martin 				n = 0;
    172  1.1.12.4  martin #if defined(SUPPORT_NFS)
    173  1.1.12.4  martin 				if (strcmp(fsname, "nfs") == 0) {
    174  1.1.12.4  martin 					memcpy(&file_system[n++], &file_system_nfs,
    175  1.1.12.4  martin 					    sizeof(file_system_nfs));
    176  1.1.12.4  martin 				} else
    177  1.1.12.4  martin #endif
    178  1.1.12.4  martin #if defined(SUPPORT_TFTP)
    179  1.1.12.4  martin 				if (strcmp(fsname, "tftp") == 0) {
    180  1.1.12.4  martin 					memcpy(&file_system[n++], &file_system_tftp,
    181  1.1.12.4  martin 					    sizeof(file_system_tftp));
    182  1.1.12.4  martin 				} else
    183  1.1.12.4  martin #endif
    184  1.1.12.4  martin 				{
    185  1.1.12.4  martin #if defined(SUPPORT_NFS)
    186  1.1.12.4  martin 					memcpy(&file_system[n++], &file_system_nfs,
    187  1.1.12.4  martin 					    sizeof(file_system_nfs));
    188  1.1.12.4  martin #endif
    189  1.1.12.4  martin #if defined(SUPPORT_TFTP)
    190  1.1.12.4  martin 					memcpy(&file_system[n++], &file_system_tftp,
    191  1.1.12.4  martin 					    sizeof(file_system_tftp));
    192  1.1.12.4  martin #endif
    193  1.1.12.4  martin 				}
    194  1.1.12.4  martin 				nfsys = n;
    195  1.1.12.4  martin 
    196  1.1.12.4  martin 				try_bootp = 1;
    197  1.1.12.4  martin 			}
    198  1.1.12.4  martin 
    199  1.1.12.4  martin 			memset(&desc, 0, sizeof(desc));
    200  1.1.12.4  martin 			strlcpy(desc.d_name, devname, sizeof(desc.d_name));
    201  1.1.12.4  martin 			desc.d_unit = unit;
    202  1.1.12.4  martin 
    203  1.1.12.4  martin 			f->f_dev = dev;
    204  1.1.12.4  martin 			if (!kernel_loaded) {
    205  1.1.12.4  martin 				strncpy(bibp.bootpath, *file,
    206  1.1.12.4  martin 				    sizeof(bibp.bootpath));
    207  1.1.12.4  martin 				BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
    208  1.1.12.4  martin 			}
    209  1.1.12.4  martin 			return DEV_OPEN(f->f_dev)(f, &desc);
    210  1.1.12.4  martin 		}
    211  1.1.12.4  martin 	}
    212  1.1.12.4  martin 
    213  1.1.12.4  martin 	/*
    214  1.1.12.4  martin 	 * biosdisk
    215  1.1.12.4  martin 	 */
    216  1.1.12.3  martin 	if (strcmp(devname, "esp") == 0) {
    217  1.1.12.3  martin 		bios2dev(boot_biosdev, boot_biossector, &devname, &unit,
    218  1.1.12.5  martin 		    &partition, NULL);
    219  1.1.12.3  martin 		if (efidisk_get_efi_system_partition(boot_biosdev, &partition))
    220  1.1.12.3  martin 			return ENXIO;
    221  1.1.12.3  martin 	}
    222  1.1.12.3  martin 
    223  1.1.12.3  martin 	error = dev2bios(devname, unit, &biosdev);
    224  1.1.12.3  martin 	if (error)
    225       1.1  nonaka 		return error;
    226       1.1  nonaka 
    227       1.1  nonaka 	f->f_dev = &devsw[0];		/* must be biosdisk */
    228       1.1  nonaka 
    229       1.1  nonaka 	if (!kernel_loaded) {
    230       1.1  nonaka 		strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
    231       1.1  nonaka 		BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
    232       1.1  nonaka 	}
    233       1.1  nonaka 
    234       1.1  nonaka 	return biosdisk_open(f, biosdev, partition);
    235       1.1  nonaka }
    236