Home | History | Annotate | Line # | Download | only in efiboot
devopen.c revision 1.1.20.4
      1  1.1.20.4  pgoyette /*	$NetBSD: devopen.c,v 1.1.20.4 2018/04/16 01:59:54 pgoyette 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.20.4  pgoyette #include <lib/libsa/dev_net.h>
     60  1.1.20.4  pgoyette 
     61       1.1    nonaka #include <biosdisk.h>
     62       1.1    nonaka #include "devopen.h"
     63       1.1    nonaka #include <bootinfo.h>
     64  1.1.20.3  pgoyette #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.20.2  pgoyette 	else if (strcmp(devname, "cd") == 0)
     73  1.1.20.1  pgoyette 		*biosdev = 0x80 + get_harddrives() + unit;
     74       1.1    nonaka 	else
     75       1.1    nonaka 		return ENXIO;
     76  1.1.20.1  pgoyette 
     77       1.1    nonaka 	return 0;
     78       1.1    nonaka }
     79       1.1    nonaka 
     80       1.1    nonaka void
     81       1.1    nonaka bios2dev(int biosdev, daddr_t sector, char **devname, int *unit, int *partition)
     82       1.1    nonaka {
     83       1.1    nonaka 
     84       1.1    nonaka 	*unit = biosdev & 0x7f;
     85  1.1.20.1  pgoyette 
     86  1.1.20.4  pgoyette 	if (efi_bootdp_type == BOOT_DEVICE_TYPE_NET) {
     87  1.1.20.4  pgoyette 		*devname = "net";
     88  1.1.20.4  pgoyette 		*unit = efi_net_get_booted_interface_unit();
     89  1.1.20.4  pgoyette 		if (*unit < 0)
     90  1.1.20.4  pgoyette 			*unit = 0;
     91  1.1.20.4  pgoyette 		*partition = 0;
     92  1.1.20.4  pgoyette 		return;
     93  1.1.20.4  pgoyette 	} else if (biosdev >= 0x80 + get_harddrives()) {
     94  1.1.20.1  pgoyette 		*devname = "cd";
     95  1.1.20.1  pgoyette 		*unit -= get_harddrives();
     96  1.1.20.1  pgoyette 	} else
     97  1.1.20.1  pgoyette 		*devname = "hd";
     98  1.1.20.1  pgoyette 
     99       1.1    nonaka 	*partition = biosdisk_findpartition(biosdev, sector);
    100       1.1    nonaka }
    101       1.1    nonaka 
    102       1.1    nonaka struct btinfo_bootpath bibp;
    103       1.1    nonaka extern bool kernel_loaded;
    104       1.1    nonaka 
    105       1.1    nonaka /*
    106       1.1    nonaka  * Open the EFI disk device
    107       1.1    nonaka  */
    108       1.1    nonaka int
    109       1.1    nonaka devopen(struct open_file *f, const char *fname, char **file)
    110       1.1    nonaka {
    111  1.1.20.4  pgoyette #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
    112  1.1.20.4  pgoyette 	static const char *net_devnames[] = {
    113  1.1.20.4  pgoyette #if defined(SUPPORT_NFS)
    114  1.1.20.4  pgoyette 	    "nfs",
    115  1.1.20.4  pgoyette #endif
    116  1.1.20.4  pgoyette #if defined(SUPPORT_TFTP)
    117  1.1.20.4  pgoyette 	    "tftp",
    118  1.1.20.4  pgoyette #endif
    119  1.1.20.4  pgoyette 	};
    120  1.1.20.4  pgoyette #endif
    121  1.1.20.4  pgoyette 	struct devdesc desc;
    122  1.1.20.4  pgoyette 	struct devsw *dev;
    123       1.1    nonaka 	char *fsname, *devname;
    124       1.1    nonaka 	int unit, partition;
    125       1.1    nonaka 	int biosdev;
    126  1.1.20.4  pgoyette 	int i, n, error;
    127       1.1    nonaka 
    128  1.1.20.3  pgoyette 	error = parsebootfile(fname, &fsname, &devname, &unit, &partition,
    129  1.1.20.3  pgoyette 	    (const char **) file);
    130  1.1.20.3  pgoyette 	if (error)
    131  1.1.20.3  pgoyette 		return error;
    132  1.1.20.3  pgoyette 
    133  1.1.20.4  pgoyette 	memcpy(file_system, file_system_disk, sizeof(*file_system) * nfsys);
    134  1.1.20.4  pgoyette 	nfsys = nfsys_disk;
    135  1.1.20.4  pgoyette 
    136  1.1.20.4  pgoyette #if defined(SUPPORT_NFS) || defined(SUPPORT_TFTP)
    137  1.1.20.4  pgoyette 	for (i = 0; i < __arraycount(net_devnames); i++) {
    138  1.1.20.4  pgoyette 		if (strcmp(devname, net_devnames[i]) == 0) {
    139  1.1.20.4  pgoyette 			fsname = devname;
    140  1.1.20.4  pgoyette 			devname = "net";
    141  1.1.20.4  pgoyette 			break;
    142  1.1.20.4  pgoyette 		}
    143  1.1.20.4  pgoyette 	}
    144  1.1.20.4  pgoyette #endif
    145  1.1.20.4  pgoyette 
    146  1.1.20.4  pgoyette 	for (i = 1; i < ndevs; i++) {
    147  1.1.20.4  pgoyette 		dev = &devsw[i];
    148  1.1.20.4  pgoyette 		if (strcmp(devname, DEV_NAME(dev)) == 0) {
    149  1.1.20.4  pgoyette 			if (strcmp(devname, "net") == 0) {
    150  1.1.20.4  pgoyette 				n = 0;
    151  1.1.20.4  pgoyette #if defined(SUPPORT_NFS)
    152  1.1.20.4  pgoyette 				if (strcmp(fsname, "nfs") == 0) {
    153  1.1.20.4  pgoyette 					memcpy(&file_system[n++], &file_system_nfs,
    154  1.1.20.4  pgoyette 					    sizeof(file_system_nfs));
    155  1.1.20.4  pgoyette 				} else
    156  1.1.20.4  pgoyette #endif
    157  1.1.20.4  pgoyette #if defined(SUPPORT_TFTP)
    158  1.1.20.4  pgoyette 				if (strcmp(fsname, "tftp") == 0) {
    159  1.1.20.4  pgoyette 					memcpy(&file_system[n++], &file_system_tftp,
    160  1.1.20.4  pgoyette 					    sizeof(file_system_tftp));
    161  1.1.20.4  pgoyette 				} else
    162  1.1.20.4  pgoyette #endif
    163  1.1.20.4  pgoyette 				{
    164  1.1.20.4  pgoyette #if defined(SUPPORT_NFS)
    165  1.1.20.4  pgoyette 					memcpy(&file_system[n++], &file_system_nfs,
    166  1.1.20.4  pgoyette 					    sizeof(file_system_nfs));
    167  1.1.20.4  pgoyette #endif
    168  1.1.20.4  pgoyette #if defined(SUPPORT_TFTP)
    169  1.1.20.4  pgoyette 					memcpy(&file_system[n++], &file_system_tftp,
    170  1.1.20.4  pgoyette 					    sizeof(file_system_tftp));
    171  1.1.20.4  pgoyette #endif
    172  1.1.20.4  pgoyette 				}
    173  1.1.20.4  pgoyette 				nfsys = n;
    174  1.1.20.4  pgoyette 
    175  1.1.20.4  pgoyette 				try_bootp = 1;
    176  1.1.20.4  pgoyette 			}
    177  1.1.20.4  pgoyette 
    178  1.1.20.4  pgoyette 			memset(&desc, 0, sizeof(desc));
    179  1.1.20.4  pgoyette 			strlcpy(desc.d_name, devname, sizeof(desc.d_name));
    180  1.1.20.4  pgoyette 			desc.d_unit = unit;
    181  1.1.20.4  pgoyette 
    182  1.1.20.4  pgoyette 			f->f_dev = dev;
    183  1.1.20.4  pgoyette 			if (!kernel_loaded) {
    184  1.1.20.4  pgoyette 				strncpy(bibp.bootpath, *file,
    185  1.1.20.4  pgoyette 				    sizeof(bibp.bootpath));
    186  1.1.20.4  pgoyette 				BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
    187  1.1.20.4  pgoyette 			}
    188  1.1.20.4  pgoyette 			return DEV_OPEN(f->f_dev)(f, &desc);
    189  1.1.20.4  pgoyette 		}
    190  1.1.20.4  pgoyette 	}
    191  1.1.20.4  pgoyette 
    192  1.1.20.4  pgoyette 	/*
    193  1.1.20.4  pgoyette 	 * biosdisk
    194  1.1.20.4  pgoyette 	 */
    195  1.1.20.3  pgoyette 	if (strcmp(devname, "esp") == 0) {
    196  1.1.20.3  pgoyette 		bios2dev(boot_biosdev, boot_biossector, &devname, &unit,
    197  1.1.20.3  pgoyette 		    &partition);
    198  1.1.20.3  pgoyette 		if (efidisk_get_efi_system_partition(boot_biosdev, &partition))
    199  1.1.20.3  pgoyette 			return ENXIO;
    200  1.1.20.3  pgoyette 	}
    201  1.1.20.3  pgoyette 
    202  1.1.20.3  pgoyette 	error = dev2bios(devname, unit, &biosdev);
    203  1.1.20.3  pgoyette 	if (error)
    204       1.1    nonaka 		return error;
    205       1.1    nonaka 
    206       1.1    nonaka 	f->f_dev = &devsw[0];		/* must be biosdisk */
    207       1.1    nonaka 
    208       1.1    nonaka 	if (!kernel_loaded) {
    209       1.1    nonaka 		strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
    210       1.1    nonaka 		BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
    211       1.1    nonaka 	}
    212       1.1    nonaka 
    213       1.1    nonaka 	return biosdisk_open(f, biosdev, partition);
    214       1.1    nonaka }
    215