xref: /netbsd/sys/arch/next68k/stand/boot/devopen.c (revision bf9ec67e)
1 /*	$NetBSD: devopen.c,v 1.2 1999/03/26 06:54:40 dbj Exp $	*/
2 /*
3  * Copyright (c) 1994 Rolf Grossmann
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *      This product includes software developed by Rolf Grossmann.
17  * 4. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <lib/libsa/stand.h>
33 #include <lib/libkern/libkern.h>
34 
35 int atoi __P((const char *cp));
36 int devlookup __P((const char *d, int len));
37 int devparse __P((const char *fname, int *dev,
38 	 char *count, char *lun, char *part, char **file));
39 
40 int
41 atoi(const char *cp)
42 {
43     int val = 0;
44 
45     while(isdigit(*cp))
46 	val = val * 10 + (*cp++ - '0');
47     return val;
48 }
49 
50 int
51 devlookup(const char *d, int len)
52 {
53     struct devsw *dp = devsw;
54     int i;
55 
56     for (i = 0; i < ndevs; i++, dp++)
57 	if (dp->dv_name && strncmp(dp->dv_name, d, len) == 0)
58 	    return i;
59 
60     for (i = 0; i < len; i++)
61 	printf("%c", d[i]);
62     printf(": no such device - Configured devices are:\n");
63     for (dp = devsw, i = 0; i < ndevs; i++, dp++)
64 	if (dp->dv_name && (void *)dp->dv_open != (void *)nodev)
65 	    printf(" %s", dp->dv_name);
66     printf("\n");
67     errno = ENXIO;
68     return -1;
69 }
70 
71 /*
72  * Parse a device spec.
73  *
74  * Format:
75  *   dev(count, lun, part)file
76  */
77 int
78 devparse(const char *fname, int *dev,
79 	 char *count, char *lun, char *part, char **file)
80 {
81     int i;
82     const char *s, *args[3];
83 
84     /* get device name */
85     for (s = fname; *s && *s != '/' && *s != '('; s++)
86 	;
87 
88     if (*s == '(') {
89 	/* lookup device and get index */
90 	if ((*dev = devlookup(fname, s - fname)) < 0)
91 	    goto baddev;
92 
93 	/* tokenize device ident */
94 	args[0] = ++s;
95 	for (i = 1; *s && *s != ')' && i<3; s++) {
96 	    if (*s == ',')
97 		args[i++] = ++s;
98 	}
99 	if (*s != ')')
100 	    goto baddev;
101 
102 	switch(i) {
103 	  case 3:
104 	      *count  = atoi(args[0]);
105 	      *lun  = atoi(args[1]);
106 	      *part  = atoi(args[2]);
107 	      break;
108 	  case 2:
109 	      *lun  = atoi(args[0]);
110 	      *part  = atoi(args[1]);
111 	      break;
112 	  case 1:
113 	      *part  = atoi(args[0]);
114 	      break;
115 	  case 0:
116 	      break;
117 	}
118 	*file = (char *)++s;	/* XXX discard const */
119     }
120     /* no device present */
121     else
122 	*file = (char *)fname;	/* XXX discard const */
123 
124     return 0;
125 
126 baddev:
127     return ENXIO;
128 }
129 
130 int
131 devopen(struct open_file *f, const char *fname, char **file)
132 {
133     int error;
134     int dev;
135     char count, lun, part;
136     struct devsw *dp;
137 
138     dev   = 0;	/* default device is first in table (usually scsi disk) */
139     count = 0;
140     lun   = 0;
141     part  = 0;
142 
143     if ((error = devparse(fname, &dev, &count, &lun, &part, file)) != 0)
144 	return error;
145 
146     dp = &devsw[dev];
147 
148     if ((void *)dp->dv_open == (void *)nodev)
149 	return ENXIO;
150 
151     f->f_dev = dp;
152 
153     if ((error = (*dp->dv_open)(f, count, lun, part)) != 0)
154 	printf("%s(%d,%d,%d): %d = %s\n", devsw[dev].dv_name,
155 	       count, lun, part, error, strerror(error));
156 
157     return error;
158 }
159