xref: /openbsd-src/sys/arch/loongson/stand/boot/devopen.c (revision e923757c9379d5a4fb9fac49faa2e1bfb150170a)
1*e923757cSmiod /*	$OpenBSD: devopen.c,v 1.1 2010/02/14 22:39:33 miod Exp $	*/
2*e923757cSmiod 
3*e923757cSmiod /*-
4*e923757cSmiod  * Copyright (c) 2003 The NetBSD Foundation, Inc.
5*e923757cSmiod  * All rights reserved.
6*e923757cSmiod  *
7*e923757cSmiod  * This code is derived from software contributed to The NetBSD Foundation
8*e923757cSmiod  * by Rolf Grossmann.
9*e923757cSmiod  *
10*e923757cSmiod  * Redistribution and use in source and binary forms, with or without
11*e923757cSmiod  * modification, are permitted provided that the following conditions
12*e923757cSmiod  * are met:
13*e923757cSmiod  * 1. Redistributions of source code must retain the above copyright
14*e923757cSmiod  *    notice, this list of conditions and the following disclaimer.
15*e923757cSmiod  * 2. Redistributions in binary form must reproduce the above copyright
16*e923757cSmiod  *    notice, this list of conditions and the following disclaimer in the
17*e923757cSmiod  *    documentation and/or other materials provided with the distribution.
18*e923757cSmiod  *
19*e923757cSmiod  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*e923757cSmiod  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*e923757cSmiod  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*e923757cSmiod  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*e923757cSmiod  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*e923757cSmiod  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*e923757cSmiod  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*e923757cSmiod  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*e923757cSmiod  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*e923757cSmiod  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*e923757cSmiod  * POSSIBILITY OF SUCH DAMAGE.
30*e923757cSmiod  */
31*e923757cSmiod 
32*e923757cSmiod #include "libsa.h"
33*e923757cSmiod 
34*e923757cSmiod #define MAXDEVNAME      16
35*e923757cSmiod 
36*e923757cSmiod /*
37*e923757cSmiod  * Parse a device spec.
38*e923757cSmiod  *
39*e923757cSmiod  * [A-Za-z]*[0-9]*[A-Za-z]:file
40*e923757cSmiod  *    dev   uint    part
41*e923757cSmiod  */
42*e923757cSmiod int
devparse(const char * fname,int * dev,int * unit,int * part,const char ** file)43*e923757cSmiod devparse(const char *fname, int *dev, int *unit, int *part, const char **file)
44*e923757cSmiod {
45*e923757cSmiod 	const char *s;
46*e923757cSmiod 
47*e923757cSmiod 	*unit = 0;	/* default to wd0a */
48*e923757cSmiod 	*part = 0;
49*e923757cSmiod 	*dev  = 0;
50*e923757cSmiod 
51*e923757cSmiod 	s = strchr(fname, ':');
52*e923757cSmiod 	if (s != NULL) {
53*e923757cSmiod 		int devlen;
54*e923757cSmiod 		int i, u, p = 0;
55*e923757cSmiod 		struct devsw *dp;
56*e923757cSmiod 		char devname[MAXDEVNAME];
57*e923757cSmiod 
58*e923757cSmiod 		devlen = s - fname;
59*e923757cSmiod 		if (devlen > MAXDEVNAME)
60*e923757cSmiod 			return (EINVAL);
61*e923757cSmiod 
62*e923757cSmiod 		/* extract device name */
63*e923757cSmiod 		for (i = 0; isalpha(fname[i]) && (i < devlen); i++)
64*e923757cSmiod 			devname[i] = fname[i];
65*e923757cSmiod 		devname[i] = 0;
66*e923757cSmiod 
67*e923757cSmiod 		if (!isdigit(fname[i]))
68*e923757cSmiod 			return (EUNIT);
69*e923757cSmiod 
70*e923757cSmiod 		/* device number */
71*e923757cSmiod 		for (u = 0; isdigit(fname[i]) && (i < devlen); i++)
72*e923757cSmiod 			u = u * 10 + (fname[i] - '0');
73*e923757cSmiod 
74*e923757cSmiod 		if (!isalpha(fname[i]))
75*e923757cSmiod 			return (EPART);
76*e923757cSmiod 
77*e923757cSmiod 		/* partition number */
78*e923757cSmiod 		if (i < devlen)
79*e923757cSmiod 			p = fname[i++] - 'a';
80*e923757cSmiod 
81*e923757cSmiod 		if (i != devlen)
82*e923757cSmiod 			return (ENXIO);
83*e923757cSmiod 
84*e923757cSmiod 		/* check device name */
85*e923757cSmiod 		for (dp = devsw, i = 0; i < ndevs; dp++, i++) {
86*e923757cSmiod 			if (dp->dv_name && !strcmp(devname, dp->dv_name))
87*e923757cSmiod 				break;
88*e923757cSmiod 		}
89*e923757cSmiod 
90*e923757cSmiod 		if (i >= ndevs)
91*e923757cSmiod 			return (ENXIO);
92*e923757cSmiod 
93*e923757cSmiod 		*unit = u;
94*e923757cSmiod 		*part = p;
95*e923757cSmiod 		*dev  = i;
96*e923757cSmiod 		fname = ++s;
97*e923757cSmiod 	}
98*e923757cSmiod 
99*e923757cSmiod 	*file = fname;
100*e923757cSmiod 
101*e923757cSmiod 	return (0);
102*e923757cSmiod }
103*e923757cSmiod 
104*e923757cSmiod int
devopen(struct open_file * f,const char * fname,char ** file)105*e923757cSmiod devopen(struct open_file *f, const char *fname, char **file)
106*e923757cSmiod {
107*e923757cSmiod 	struct devsw *dp;
108*e923757cSmiod 	int dev, unit, part, error;
109*e923757cSmiod 
110*e923757cSmiod 	error = devparse(fname, &dev, &unit, &part, (const char **)file);
111*e923757cSmiod 	if (error)
112*e923757cSmiod 		return (error);
113*e923757cSmiod 
114*e923757cSmiod 	dp = &devsw[dev];
115*e923757cSmiod 	if ((void *)dp->dv_open == (void *)nodev)
116*e923757cSmiod 		return (ENXIO);
117*e923757cSmiod 
118*e923757cSmiod 	f->f_dev = dp;
119*e923757cSmiod 
120*e923757cSmiod 	return (*dp->dv_open)(f, unit, part);
121*e923757cSmiod }
122