xref: /netbsd-src/sys/arch/zaurus/stand/zboot/devopen.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: devopen.c,v 1.1 2009/03/02 09:33:02 nonaka 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 <sys/param.h>
32 #include <sys/disklabel.h>
33 
34 #include "boot.h"
35 #include "bootinfo.h"
36 #include "disk.h"
37 #include "unixdev.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, devname_hd) == 0) {
47 		*biosdev = 0x80 + unit;
48 		return 0;
49 	} else if (strcmp(devname, devname_mmcd) == 0) {
50 		*biosdev = 0x00 + unit;
51 		return 0;
52 	}
53 	return ENXIO;
54 }
55 
56 static int
57 devlookup(char *d)
58 {
59 	struct devsw *dp = devsw;
60 	int i;
61 
62 	for (i = 0; i < ndevs; i++, dp++) {
63 		if ((dp->dv_name != NULL) && (strcmp(dp->dv_name, d) == 0)) {
64 			return i;
65 		}
66 	}
67 
68 	printf("No such device - Configured devices are:\n");
69 	for (dp = devsw, i = 0; i < ndevs; i++, dp++) {
70 		if (dp->dv_name != NULL) {
71 			printf(" %s", dp->dv_name);
72 		}
73 	}
74 	printf("\n");
75 	return -1;
76 }
77 
78 int
79 devopen(struct open_file *f, const char *fname, char **file)
80 {
81 	struct devsw *dp;
82 	char *fsname, *devname;
83 	unsigned int dev, ctlr, unit, partition;
84 	int biosdev;
85 	int error;
86 
87 #if defined(UNIX_DEBUG)
88 	printf("devopen: fname = %s\n", fname ? fname : "(null)");
89 #endif
90 
91 	ctlr = 0;
92 	if ((error = parsebootfile(fname, &fsname, &devname, &unit, &partition,
93 	    (const char **)file)) != 0) {
94 		return error;
95 	}
96 
97 #if defined(UNIX_DEBUG)
98 	printf("devopen: devname = %s\n", devname);
99 #endif
100 	dev = devlookup(devname);
101 	if (dev == -1) {
102 #if defined(UNIX_DEBUG)
103 		printf("devopen: devlookup failed\n");
104 #endif
105 		return ENXIO;
106 	}
107 
108 	dp = &devsw[dev];
109 	if (dp->dv_open == NULL) {
110 #if defined(UNIX_DEBUG)
111 		printf("devopen: dev->dv_open() == NULL\n");
112 #endif
113 		return ENODEV;
114 	}
115 	f->f_dev = dp;
116 
117 	if (dev2bios(devname, unit, &biosdev) == 0) {
118 #if defined(UNIX_DEBUG)
119 		printf("devopen: bios disk\n");
120 #endif
121 		return (unixopen(f, biosdev, devname, unit, partition, *file));
122 	}
123 
124 #if defined(UNIX_DEBUG)
125 	printf("devopen: dev->dv_open()\n");
126 #endif
127 	if ((error = (*dp->dv_open)(f, ctlr, unit, partition)) == 0) {
128 #if defined(UNIX_DEBUG)
129 		printf("devopen: dev->dv_open() opened\n");
130 #endif
131 		return 0;
132 	}
133 
134 	printf("%s%d%c:%s : %s\n", dp->dv_name, unit, partition + 'a', *file,
135 	    strerror(error));
136 	return error;
137 }
138