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