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