Home | History | Annotate | Line # | Download | only in efiboot
devopen.c revision 1.2
      1  1.2  nonaka /*	$NetBSD: devopen.c,v 1.2 2018/03/08 10:34:33 nonaka 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  nonaka #include <biosdisk.h>
     60  1.1  nonaka #include "devopen.h"
     61  1.1  nonaka #include <bootinfo.h>
     62  1.1  nonaka 
     63  1.1  nonaka static int
     64  1.1  nonaka dev2bios(char *devname, int unit, int *biosdev)
     65  1.1  nonaka {
     66  1.1  nonaka 
     67  1.1  nonaka 	if (strcmp(devname, "hd") == 0)
     68  1.1  nonaka 		*biosdev = 0x80 + unit;
     69  1.2  nonaka 	if (strcmp(devname, "cd") == 0)
     70  1.2  nonaka 		*biosdev = 0x80 + get_harddrives() + unit;
     71  1.1  nonaka 	else
     72  1.1  nonaka 		return ENXIO;
     73  1.2  nonaka 
     74  1.1  nonaka 	return 0;
     75  1.1  nonaka }
     76  1.1  nonaka 
     77  1.1  nonaka void
     78  1.1  nonaka bios2dev(int biosdev, daddr_t sector, char **devname, int *unit, int *partition)
     79  1.1  nonaka {
     80  1.1  nonaka 
     81  1.1  nonaka 	*unit = biosdev & 0x7f;
     82  1.2  nonaka 
     83  1.2  nonaka 	if (biosdev >= 0x80 + get_harddrives()) {
     84  1.2  nonaka 		*devname = "cd";
     85  1.2  nonaka 		*unit -= get_harddrives();
     86  1.2  nonaka 	} else
     87  1.2  nonaka 		*devname = "hd";
     88  1.2  nonaka 
     89  1.1  nonaka 	*partition = biosdisk_findpartition(biosdev, sector);
     90  1.1  nonaka }
     91  1.1  nonaka 
     92  1.1  nonaka struct btinfo_bootpath bibp;
     93  1.1  nonaka extern bool kernel_loaded;
     94  1.1  nonaka 
     95  1.1  nonaka /*
     96  1.1  nonaka  * Open the EFI disk device
     97  1.1  nonaka  */
     98  1.1  nonaka int
     99  1.1  nonaka devopen(struct open_file *f, const char *fname, char **file)
    100  1.1  nonaka {
    101  1.1  nonaka 	char *fsname, *devname;
    102  1.1  nonaka 	int unit, partition;
    103  1.1  nonaka 	int biosdev;
    104  1.1  nonaka 	int error;
    105  1.1  nonaka 
    106  1.1  nonaka 	if ((error = parsebootfile(fname, &fsname, &devname,
    107  1.1  nonaka 				   &unit, &partition, (const char **) file))
    108  1.1  nonaka 	    || (error = dev2bios(devname, unit, &biosdev)))
    109  1.1  nonaka 		return error;
    110  1.1  nonaka 
    111  1.1  nonaka 	f->f_dev = &devsw[0];		/* must be biosdisk */
    112  1.1  nonaka 
    113  1.1  nonaka 	if (!kernel_loaded) {
    114  1.1  nonaka 		strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
    115  1.1  nonaka 		BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
    116  1.1  nonaka 	}
    117  1.1  nonaka 
    118  1.1  nonaka 	return biosdisk_open(f, biosdev, partition);
    119  1.1  nonaka }
    120