Home | History | Annotate | Line # | Download | only in boot
      1 /*	$NetBSD: devopen.c,v 1.1 2006/09/01 21:26:18 uwe Exp $	*/
      2 
      3 /*-
      4  *  Copyright (c) 1993 John Brezak
      5  *  All rights reserved.
      6  *
      7  *  Redistribution and use in source and binary forms, with or without
      8  *  modification, are permitted provided that the following conditions
      9  *  are met:
     10  *  1. Redistributions of source code must retain the above copyright
     11  *     notice, this list of conditions and the following disclaimer.
     12  *  2. Redistributions in binary form must reproduce the above copyright
     13  *     notice, this list of conditions and the following disclaimer in the
     14  *     documentation and/or other materials provided with the distribution.
     15  *  3. The name of the author may not be used to endorse or promote products
     16  *     derived from this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
     19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     21  * DISCLAIMED.	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     22  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     27  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #include <lib/libsa/stand.h>
     32 #include <lib/libkern/libkern.h>
     33 
     34 #include "biosdisk.h"
     35 
     36 #include "boot.h"
     37 #include "bootinfo.h"
     38 
     39 static int dev2bios(char *devname, unsigned int unit, int *biosdev);
     40 static int devlookup(char *d);
     41 
     42 static int
     43 dev2bios(char *devname, unsigned int unit, int *biosdev)
     44 {
     45 
     46 	if (strcmp(devname, "hd") == 0) {
     47 		if (unit == 0 || unit == 1) {
     48 			*biosdev = 0x40 + (unit << 4);
     49 			return (0);
     50 		}
     51 	}
     52 	return (ENXIO);
     53 }
     54 
     55 int
     56 bios2dev(int biosdev, char **devname, u_int *unit, u_int sector, u_int *ptnp)
     57 {
     58 
     59 	*devname = "hd";
     60 	*unit = (biosdev >> 4) & 1;
     61 	*ptnp = biosdisk_findptn(biosdev, sector);
     62 	return (0);
     63 }
     64 
     65 static int
     66 devlookup(char *d)
     67 {
     68 	struct devsw *dp = devsw;
     69 	int i;
     70 
     71 	for (i = 0; i < ndevs; i++, dp++) {
     72 		if ((dp->dv_name != NULL) && (strcmp(dp->dv_name, d) == 0)) {
     73 			return (i);
     74 		}
     75 	}
     76 
     77 	printf("No such device - Configured devices are:\n");
     78 	for (dp = devsw, i = 0; i < ndevs; i++, dp++) {
     79 		if (dp->dv_name != NULL) {
     80 			printf(" %s", dp->dv_name);
     81 		}
     82 	}
     83 	printf("\n");
     84 	return (-1);
     85 }
     86 
     87 int
     88 devopen(struct open_file *f, const char *fname, char **file)
     89 {
     90 	static struct btinfo_bootpath bibp;
     91 	struct devsw *dp;
     92 	char *devname;
     93 	unsigned int dev, ctlr, unit, partition;
     94 	int biosdev;
     95 	int error;
     96 
     97 #if defined(DEBUG)
     98 	printf("devopen: fname = %s\n", fname);
     99 #endif
    100 
    101 	ctlr = 0;
    102 	if ((error = parsebootfile(fname, &devname, &unit, &partition,
    103 	    (const char **)file)) != 0) {
    104 		return (error);
    105 	}
    106 
    107 #if defined(DEBUG)
    108 	printf("devopen: devname = %s\n", devname);
    109 #endif
    110 	dev = devlookup(devname);
    111 	if (dev == -1) {
    112 #if defined(DEBUG)
    113 		printf("devopen: devlookup failed\n");
    114 #endif
    115 		return (ENXIO);
    116 	}
    117 
    118 	dp = &devsw[dev];
    119 	if (dp->dv_open == NULL) {
    120 #if defined(DEBUG)
    121 		printf("devopen: dev->dv_open() == NULL\n");
    122 #endif
    123 		return (ENODEV);
    124 	}
    125 	f->f_dev = dp;
    126 
    127 	strncpy(bibp.bootpath, *file, sizeof(bibp.bootpath));
    128 	BI_ADD(&bibp, BTINFO_BOOTPATH, sizeof(bibp));
    129 
    130 	if (dev2bios(devname, unit, &biosdev) == 0) {
    131 #if defined(DEBUG)
    132 		printf("devopen: bios disk\n");
    133 #endif
    134 		return (biosdisk_open(f, biosdev, partition));
    135 	}
    136 
    137 #if defined(DEBUG)
    138 	printf("devopen: dev->dv_open()\n");
    139 #endif
    140 	if ((error = (*dp->dv_open)(f, ctlr, unit, partition)) == 0) {
    141 #if defined(DEBUG)
    142 		printf("devopen: dev->dv_open() opened\n");
    143 #endif
    144 		return (0);
    145 	}
    146 
    147 	printf("%s%d%c:%s : %s\n", dp->dv_name, unit, partition + 'a', *file,
    148 	    strerror(error));
    149 	return (error);
    150 }
    151