xref: /netbsd-src/sys/arch/next68k/stand/boot/devopen.c (revision 783bf7199cac9069640f0160391cee3cc6f37264)
1 /*	$NetBSD: devopen.c,v 1.8 2023/02/12 08:25:09 tsutsui 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 devlookup(const char *, int);
36 int devparse(const char *, int *, int *, int *, int *, char **);
37 
38 int
devlookup(const char * d,int len)39 devlookup(const char *d, int len)
40 {
41     struct devsw *dp = devsw;
42     int i;
43 
44     for (i = 0; i < ndevs; i++, dp++)
45 	if (dp->dv_name && strncmp(dp->dv_name, d, len) == 0)
46 	    return i;
47 
48     for (i = 0; i < len; i++)
49 	printf("%c", d[i]);
50     printf(": no such device - Configured devices are:\n");
51     for (dp = devsw, i = 0; i < ndevs; i++, dp++)
52 	if (dp->dv_name && (void *)dp->dv_open != (void *)nodev)
53 	    printf(" %s", dp->dv_name);
54     printf("\n");
55     errno = ENXIO;
56     return -1;
57 }
58 
59 /*
60  * Parse a device spec.
61  *
62  * Format:
63  *   dev(count, lun, part)file
64  */
65 int
devparse(const char * fname,int * dev,int * count,int * lun,int * part,char ** file)66 devparse(const char *fname, int *dev,
67 	 int *count, int *lun, int *part, char **file)
68 {
69     int i;
70     const char *s, *args[3];
71 
72     /* get device name */
73     for (s = fname; *s && *s != '/' && *s != '('; s++)
74 	;
75 
76     if (*s == '(') {
77 	/* lookup device and get index */
78 	if ((*dev = devlookup(fname, s - fname)) < 0)
79 	    goto baddev;
80 
81 	/* tokenize device ident */
82 	args[0] = ++s;
83 	for (i = 1; *s && *s != ')' && i<3; s++) {
84 	    if (*s == ',')
85 		args[i++] = ++s;
86 	}
87 	if (*s != ')')
88 	    goto baddev;
89 
90 	switch(i) {
91 	  case 3:
92 	      *count  = atoi(args[0]);
93 	      *lun  = atoi(args[1]);
94 	      *part  = atoi(args[2]);
95 	      break;
96 	  case 2:
97 	      *lun  = atoi(args[0]);
98 	      *part  = atoi(args[1]);
99 	      break;
100 	  case 1:
101 	      *part  = atoi(args[0]);
102 	      break;
103 	  case 0:
104 	      break;
105 	}
106 	*file = (char *)++s;	/* XXX discard const */
107     }
108     /* no device present */
109     else
110 	*file = (char *)fname;	/* XXX discard const */
111 
112     return 0;
113 
114 baddev:
115     return ENXIO;
116 }
117 
118 int
devopen(struct open_file * f,const char * fname,char ** file)119 devopen(struct open_file *f, const char *fname, char **file)
120 {
121     int error;
122     int dev;
123     int count, lun, part;
124     struct devsw *dp;
125 
126     dev   = 0;	/* default device is first in table (usually scsi disk) */
127     count = 0;
128     lun   = 0;
129     part  = 0;
130 
131     if ((error = devparse(fname, &dev, &count, &lun, &part, file)) != 0)
132 	return error;
133 
134     dp = &devsw[dev];
135 
136     if ((void *)dp->dv_open == (void *)nodev)
137 	return ENXIO;
138 
139     f->f_dev = dp;
140 
141     if ((error = (*dp->dv_open)(f, count, lun, part)) != 0)
142 	printf("%s(%d,%d,%d): %d = %s\n", devsw[dev].dv_name,
143 	       count, lun, part, error, strerror(error));
144 
145     return error;
146 }
147