xref: /netbsd-src/sys/arch/cobalt/stand/boot/devopen.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: devopen.c,v 1.4 2007/10/30 15:07:08 tsutsui Exp $	*/
2 
3 /*-
4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Rolf Grossmann.
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 #include <lib/libsa/stand.h>
40 #include <lib/libkern/libkern.h>
41 
42 #include "boot.h"
43 
44 /*
45  * Parse a device spec.
46  *
47  * Format:
48  *  [device:][filename]
49  */
50 int
51 devparse(const char *fname, int *dev, uint8_t *unit, uint8_t *part,
52     const char **file)
53 {
54 	const char *col;
55 
56 	*unit = 0;	/* default to wd0a */
57 	*part = 0;
58 	*dev  = 0;
59 	*file = DEFKERNELNAME;
60 
61 	if (fname == NULL)
62 		return 0;
63 
64 	if ((col = strchr(fname, ':')) != NULL) {
65 		int devlen;
66 		uint8_t i, u, p;
67 		struct devsw *dp;
68 		char devname[MAXDEVNAME];
69 
70 		devlen = col - fname;
71 		if (devlen > MAXDEVNAME)
72 			return EINVAL;
73 
74 #define isnum(c)	(((c) >= '0') && ((c) <= '9'))
75 #define isalpha(c)	(((c) >= 'a') && ((c) <= 'z'))
76 
77 		/* extract device name */
78 		for (i = 0; isalpha(fname[i]) && (i < devlen); i++)
79 			devname[i] = fname[i];
80 		devname[i] = '\0';
81 
82 		if (strcmp(devname, "nfs") == 0) {
83 			/* no unit number or partition suffix on netboot */
84 			u = 0;
85 			p = 0;
86 		} else {
87 			/* parse [disk][unit][part] (ex. wd0a) strings */
88 			if (!isnum(fname[i]))
89 				return EUNIT;
90 
91 			/* device number */
92 			for (u = 0; isnum(fname[i]) && (i < devlen); i++)
93 				u = u * 10 + (fname[i] - '0');
94 
95 			if (!isalpha(fname[i]))
96 				return EPART;
97 
98 			/* partition number */
99 			if (i < devlen)
100 				p = fname[i++] - 'a';
101 
102 			if (i != devlen)
103 				return ENXIO;
104 		}
105 
106 		/* check device name */
107 		for (dp = devsw, i = 0; i < ndevs; dp++, i++) {
108 			if (dp->dv_name && !strcmp(devname, dp->dv_name))
109 				break;
110 		}
111 
112 		if (i >= ndevs)
113 			return ENXIO;
114 
115 		*unit = u;
116 		*part = p;
117 		*dev  = i;
118 		fname = ++col;
119 	}
120 
121 	if (*fname)
122 		*file = fname;
123 
124 	return 0;
125 }
126 
127 int
128 devopen(struct open_file *f, const char *fname, char **file)
129 {
130 	struct devsw *dp;
131 	uint8_t unit, part;
132 	int dev, error;
133 
134 	DPRINTF(("devopen(%s)\n", fname));
135 
136 	if ((error = devparse(fname, &dev, &unit, &part,
137 	    (const char **)file)) != 0)
138 		return error;
139 
140 	dp = &devsw[dev];
141 	if ((void *)dp->dv_open == (void *)nodev)
142 		return ENXIO;
143 
144 	f->f_dev = dp;
145 
146 	if ((error = (*dp->dv_open)(f, unit, part)) != 0)
147 	printf("%s%d%c: %d = %s\n", devsw[dev].dv_name,
148 	    unit, 'a' + part, error, strerror(error));
149 
150 	return error;
151 }
152