xref: /netbsd/sys/arch/sbmips/stand/netboot/devopen.c (revision c4a72b64)
1 /* $NetBSD: devopen.c,v 1.1 2002/11/09 06:20:41 cgd Exp $ */
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Ralph Campbell.
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 University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *	@(#)devopen.c	8.1 (Berkeley) 6/10/93
39  */
40 
41 #include <lib/libsa/stand.h>
42 
43 /*
44  * Decode the string 'fname', open the device and return the remaining
45  * file name if any.
46  */
47 int
48 devopen(f, fname, file)
49 	struct open_file *f;
50 	const char *fname;
51 	char **file;	/* out */
52 {
53 	register char *cp;
54 	register char *ncp;
55 	register struct devsw *dp;
56 #if 0
57 	register int c, i;
58 #endif
59 	int ctlr = 0, unit = 0, part = 0;
60 	char namebuf[20];
61 	int rc;
62 
63 	cp = (char *)fname;
64 	ncp = namebuf;
65 
66 #if 0
67 	/* look for a string like '5/rz0/vmunix' or '5/rz3f/vmunix */
68 	if ((c = *cp) >= '0' && c <= '9') {
69 		ctlr = c - '0';
70 		/* skip the '/' */
71 		if (*++cp != '/')
72 			return (ENXIO);
73 		cp++;
74 		while ((c = *cp) != '\0') {
75 			if (c == '/')
76 				break;
77 			if (c >= '0' && c <= '9') {
78 				/* read unit number */
79 				unit = c - '0';
80 
81 				/* look for a partition */
82 				if ((c = *++cp) >= 'a' && c <= 'h') {
83 					part = c - 'a';
84 					c = *++cp;
85 				}
86 				if (c != '/')
87 					return (ENXIO);
88 				break;
89 			}
90 			if (ncp < namebuf + sizeof(namebuf) - 1)
91 				*ncp++ = c;
92 			cp++;
93 		}
94 		*ncp = '\0';
95 	/*
96 	 * XXX
97 	 * pulling strchr from the C library, should pull from libkern.
98 	 */
99 	} else if (strchr(cp, '(')) {
100 		/* expect a string like 'rz(0,0,0)vmunix' */
101 		while ((c = *cp) != '\0') {
102 			if (c == '(') {
103 				cp++;
104 				break;
105 			}
106 			if (ncp < namebuf + sizeof(namebuf) - 1)
107 				*ncp++ = c;
108 			cp++;
109 		}
110 
111 		/* get controller number */
112 		if ((c = *cp) >= '0' && c <= '9') {
113 			ctlr = c - '0';
114 			c = *++cp;
115 		}
116 
117 		if (c == ',') {
118 			/* get SCSI device number */
119 			if ((c = *++cp) >= '0' && c <= '9') {
120 				unit = c - '0';
121 				c = *++cp;
122 			}
123 
124 			if (c == ',') {
125 				/* get partition number */
126 				if ((c = *++cp) >= '0' && c <= '9') {
127 					part = c - '0';
128 					c = *++cp;
129 				}
130 			}
131 		}
132 		if (c != ')')
133 			return (ENXIO);
134 		cp++;
135 		*ncp = '\0';
136 	} else {
137 #endif
138 		dp = devsw;
139 		ctlr = unit = part = 0;
140 		goto fnd;
141 #if 0
142 	}
143 
144 	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
145 		if (dp->dv_name && strcmp(namebuf, dp->dv_name) == 0)
146 			goto fnd;
147 	printf("Unknown device '%s'\nKnown devices are:", namebuf);
148 	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
149 		if (dp->dv_name)
150 			printf(" %s", dp->dv_name);
151 	printf("\n");
152 	return (ENXIO);
153 #endif
154 
155 fnd:
156 	rc = (dp->dv_open)(f, ctlr, unit, part);
157 	if (rc)
158 		return (rc);
159 
160 	f->f_dev = dp;
161 	if (file && *cp != '\0')
162 		*file = cp;
163 	return (0);
164 }
165