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