Home | History | Annotate | Line # | Download | only in boot
devopen.c revision 1.7
      1  1.7        ad /*	$NetBSD: devopen.c,v 1.7 2008/05/02 15:26:38 ad Exp $	 */
      2  1.4  junyoung 
      3  1.4  junyoung /*-
      4  1.4  junyoung  * Copyright (c) 2005 The NetBSD Foundation, Inc.
      5  1.4  junyoung  * All rights reserved.
      6  1.4  junyoung  *
      7  1.4  junyoung  * This code is derived from software contributed to The NetBSD Foundation
      8  1.4  junyoung  * by Bang Jun-Young.
      9  1.4  junyoung  *
     10  1.4  junyoung  * Redistribution and use in source and binary forms, with or without
     11  1.4  junyoung  * modification, are permitted provided that the following conditions
     12  1.4  junyoung  * are met:
     13  1.4  junyoung  * 1. Redistributions of source code must retain the above copyright
     14  1.4  junyoung  *    notice, this list of conditions and the following disclaimer.
     15  1.4  junyoung  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.4  junyoung  *    notice, this list of conditions and the following disclaimer in the
     17  1.4  junyoung  *    documentation and/or other materials provided with the distribution.
     18  1.4  junyoung  *
     19  1.4  junyoung  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.4  junyoung  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.4  junyoung  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.4  junyoung  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.4  junyoung  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.4  junyoung  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.4  junyoung  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.4  junyoung  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.4  junyoung  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.4  junyoung  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.4  junyoung  * POSSIBILITY OF SUCH DAMAGE.
     30  1.4  junyoung  */
     31  1.1       dsl 
     32  1.1       dsl /*
     33  1.1       dsl  * Copyright (c) 1996, 1997
     34  1.1       dsl  *	Matthias Drochner.  All rights reserved.
     35  1.1       dsl  *
     36  1.1       dsl  * Redistribution and use in source and binary forms, with or without
     37  1.1       dsl  * modification, are permitted provided that the following conditions
     38  1.1       dsl  * are met:
     39  1.1       dsl  * 1. Redistributions of source code must retain the above copyright
     40  1.1       dsl  *    notice, this list of conditions and the following disclaimer.
     41  1.1       dsl  * 2. Redistributions in binary form must reproduce the above copyright
     42  1.1       dsl  *    notice, this list of conditions and the following disclaimer in the
     43  1.1       dsl  *    documentation and/or other materials provided with the distribution.
     44  1.1       dsl  *
     45  1.1       dsl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     46  1.1       dsl  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     47  1.1       dsl  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     48  1.1       dsl  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     49  1.1       dsl  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     50  1.1       dsl  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     51  1.1       dsl  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     52  1.1       dsl  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     53  1.1       dsl  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     54  1.1       dsl  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     55  1.1       dsl  */
     56  1.1       dsl 
     57  1.1       dsl 
     58  1.1       dsl #include <sys/types.h>
     59  1.1       dsl 
     60  1.1       dsl #include <lib/libsa/stand.h>
     61  1.1       dsl #include <lib/libkern/libkern.h>
     62  1.1       dsl 
     63  1.1       dsl #include <libi386.h>
     64  1.1       dsl #include <biosdisk.h>
     65  1.1       dsl #include "devopen.h"
     66  1.1       dsl #ifdef _STANDALONE
     67  1.1       dsl #include <bootinfo.h>
     68  1.1       dsl #endif
     69  1.1       dsl #ifdef SUPPORT_PS2
     70  1.1       dsl #include <biosmca.h>
     71  1.1       dsl #endif
     72  1.1       dsl 
     73  1.4  junyoung static int dev2bios(char *, int, int *);
     74  1.1       dsl 
     75  1.3  junyoung static int
     76  1.4  junyoung dev2bios(char *devname, int unit, int *biosdev)
     77  1.1       dsl {
     78  1.4  junyoung 
     79  1.1       dsl 	if (strcmp(devname, "hd") == 0)
     80  1.1       dsl 		*biosdev = 0x80 + unit;
     81  1.1       dsl 	else if (strcmp(devname, "fd") == 0)
     82  1.1       dsl 		*biosdev = 0x00 + unit;
     83  1.4  junyoung 	else if (strcmp(devname, "cd") == 0)
     84  1.4  junyoung 		*biosdev = boot_biosdev;
     85  1.1       dsl 	else
     86  1.3  junyoung 		return ENXIO;
     87  1.1       dsl 
     88  1.3  junyoung 	return 0;
     89  1.1       dsl }
     90  1.1       dsl 
     91  1.4  junyoung void
     92  1.4  junyoung bios2dev(int biosdev, u_int sector, char **devname, int *unit, int *partition)
     93  1.1       dsl {
     94  1.1       dsl 
     95  1.4  junyoung 	/* set default */
     96  1.1       dsl 	*unit = biosdev & 0x7f;
     97  1.1       dsl 
     98  1.4  junyoung 	if (biosdev & 0x80) {
     99  1.4  junyoung 		/*
    100  1.4  junyoung 		 * There seems to be no standard way of numbering BIOS
    101  1.4  junyoung 		 * CD-ROM drives. The following method is a little tricky
    102  1.4  junyoung 		 * but works nicely.
    103  1.4  junyoung 		 */
    104  1.4  junyoung 		if (biosdev >= 0x80 + get_harddrives()) {
    105  1.4  junyoung 			*devname = "cd";
    106  1.4  junyoung 			*unit = 0;		/* override default */
    107  1.4  junyoung 		} else
    108  1.4  junyoung 			*devname = "hd";
    109  1.4  junyoung 	} else
    110  1.4  junyoung 		*devname = "fd";
    111  1.4  junyoung 
    112  1.4  junyoung 	*partition = biosdisk_findpartition(biosdev, sector);
    113  1.1       dsl }
    114  1.1       dsl 
    115  1.1       dsl #ifdef _STANDALONE
    116  1.1       dsl struct btinfo_bootpath bibp;
    117  1.7        ad extern bool kernel_loaded;
    118  1.1       dsl #endif
    119  1.1       dsl 
    120  1.1       dsl /*
    121  1.1       dsl  * Open the BIOS disk device
    122  1.1       dsl  */
    123  1.1       dsl int
    124  1.3  junyoung devopen(struct open_file *f, const char *fname, char **file)
    125  1.1       dsl {
    126  1.3  junyoung 	char *fsname, *devname;
    127  1.4  junyoung 	int unit, partition;
    128  1.3  junyoung 	int biosdev;
    129  1.3  junyoung 	int error;
    130  1.1       dsl 
    131  1.1       dsl 	if ((error = parsebootfile(fname, &fsname, &devname,
    132  1.1       dsl 				   &unit, &partition, (const char **) file))
    133  1.1       dsl 	    || (error = dev2bios(devname, unit, &biosdev)))
    134  1.3  junyoung 		return error;
    135  1.1       dsl 
    136  1.3  junyoung 	f->f_dev = &devsw[0];		/* must be biosdisk */
    137  1.1       dsl 
    138  1.1       dsl #ifdef _STANDALONE
    139  1.7        ad 	if (!kernel_loaded) {
    140  1.7        ad 		strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
    141  1.7        ad 		BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
    142  1.7        ad 	}
    143  1.1       dsl #endif
    144  1.1       dsl 
    145  1.4  junyoung 	return biosdisk_open(f, biosdev, partition);
    146  1.1       dsl }
    147