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