xref: /netbsd-src/sys/arch/rs6000/stand/boot/devopen.c (revision 16b478fee75e1847dfda923deae1261d9404cee3)
1*16b478feSrin /*	$NetBSD: devopen.c,v 1.3 2022/04/30 03:59:15 rin Exp $	*/
268fe5b6fSgarbled 
368fe5b6fSgarbled /*-
468fe5b6fSgarbled  *  Copyright (c) 1993 John Brezak
568fe5b6fSgarbled  *  All rights reserved.
668fe5b6fSgarbled  *
768fe5b6fSgarbled  *  Redistribution and use in source and binary forms, with or without
868fe5b6fSgarbled  *  modification, are permitted provided that the following conditions
968fe5b6fSgarbled  *  are met:
1068fe5b6fSgarbled  *  1. Redistributions of source code must retain the above copyright
1168fe5b6fSgarbled  *     notice, this list of conditions and the following disclaimer.
1268fe5b6fSgarbled  *  2. Redistributions in binary form must reproduce the above copyright
1368fe5b6fSgarbled  *     notice, this list of conditions and the following disclaimer in the
1468fe5b6fSgarbled  *     documentation and/or other materials provided with the distribution.
1568fe5b6fSgarbled  *  3. The name of the author may not be used to endorse or promote products
1668fe5b6fSgarbled  *     derived from this software without specific prior written permission.
1768fe5b6fSgarbled  *
1868fe5b6fSgarbled  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
1968fe5b6fSgarbled  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
2068fe5b6fSgarbled  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2168fe5b6fSgarbled  * DISCLAIMED.	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
2268fe5b6fSgarbled  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
2368fe5b6fSgarbled  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2468fe5b6fSgarbled  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2568fe5b6fSgarbled  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
2668fe5b6fSgarbled  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
2768fe5b6fSgarbled  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2868fe5b6fSgarbled  * POSSIBILITY OF SUCH DAMAGE.
2968fe5b6fSgarbled  */
3068fe5b6fSgarbled 
3168fe5b6fSgarbled #include <lib/libsa/stand.h>
3268fe5b6fSgarbled #include <lib/libkern/libkern.h>
3368fe5b6fSgarbled 
3468fe5b6fSgarbled #define	ispart(c)	((c) >= 'a' && (c) <= 'h')
3568fe5b6fSgarbled 
3668fe5b6fSgarbled int devlookup(char *);
3768fe5b6fSgarbled int devparse(const char *, int *, int *, int *, int *, int *, char **);
3868fe5b6fSgarbled 
3968fe5b6fSgarbled int
devlookup(char * d)4068fe5b6fSgarbled devlookup(char *d)
4168fe5b6fSgarbled {
4268fe5b6fSgarbled 	struct devsw *dp = devsw;
4368fe5b6fSgarbled 	int i;
4468fe5b6fSgarbled 
4568fe5b6fSgarbled 	for (i = 0; i < ndevs; i++, dp++)
4668fe5b6fSgarbled 		if (dp->dv_name && strcmp(dp->dv_name, d) == 0)
4768fe5b6fSgarbled 			return (i);
4868fe5b6fSgarbled 
4968fe5b6fSgarbled 	printf("No such device - Configured devices are:\n");
5068fe5b6fSgarbled 	for (dp = devsw, i = 0; i < ndevs; i++, dp++)
5168fe5b6fSgarbled 		if (dp->dv_name)
5268fe5b6fSgarbled 			printf(" %s", dp->dv_name);
5368fe5b6fSgarbled 	printf("\n");
5468fe5b6fSgarbled 	return (-1);
5568fe5b6fSgarbled }
5668fe5b6fSgarbled 
5768fe5b6fSgarbled /*
5868fe5b6fSgarbled  * Parse a device spec in one of two forms.
5968fe5b6fSgarbled  *   dev(ctlr, unit, part)file
6068fe5b6fSgarbled  */
6168fe5b6fSgarbled int
devparse(const char * fname,int * dev,int * adapt,int * ctlr,int * unit,int * part,char ** file)6268fe5b6fSgarbled devparse(const char *fname, int *dev, int *adapt, int *ctlr, int *unit,
6368fe5b6fSgarbled 	int *part, char **file)
6468fe5b6fSgarbled {
6568fe5b6fSgarbled 	int argc, flag;
6668fe5b6fSgarbled 	char *s, *args[3];
6768fe5b6fSgarbled 	extern char nametmp[];
6868fe5b6fSgarbled 
6968fe5b6fSgarbled 	/* get device name and make lower case */
7068fe5b6fSgarbled 	strcpy(nametmp, (char *)fname);
7168fe5b6fSgarbled 	for (s = nametmp; *s && *s != '('; s++)
7268fe5b6fSgarbled 		if (isupper(*s)) *s = tolower(*s);
7368fe5b6fSgarbled 
7468fe5b6fSgarbled 	if (*s == '(') {
7568fe5b6fSgarbled 		/* lookup device and get index */
76*16b478feSrin 		*s = '\0';
7768fe5b6fSgarbled 		if ((*dev = devlookup(nametmp)) < 0)
7868fe5b6fSgarbled 		    goto baddev;
7968fe5b6fSgarbled 
8068fe5b6fSgarbled 		/* tokenize device ident */
8168fe5b6fSgarbled 		for (++s, flag = 0, argc = 0; *s && *s != ')'; s++) {
8268fe5b6fSgarbled 			if (*s != ',') {
8368fe5b6fSgarbled 				if (!flag) {
8468fe5b6fSgarbled 					flag++;
8568fe5b6fSgarbled 					args[argc++] = s;
8668fe5b6fSgarbled 				}
8768fe5b6fSgarbled 			} else {
8868fe5b6fSgarbled 				if (flag) {
89*16b478feSrin 					*s = '\0';
9068fe5b6fSgarbled 					flag = 0;
9168fe5b6fSgarbled 				}
9268fe5b6fSgarbled 			}
9368fe5b6fSgarbled 		}
9468fe5b6fSgarbled 		if (*s == ')')
95*16b478feSrin 			*s = '\0';
9668fe5b6fSgarbled 
9768fe5b6fSgarbled 		switch (argc) {
9868fe5b6fSgarbled 		case 3:
9968fe5b6fSgarbled 		    *part = atoi(args[2]);
10068fe5b6fSgarbled 		    /* FALL THROUGH */
10168fe5b6fSgarbled 		case 2:
10268fe5b6fSgarbled 		    *unit = atoi(args[1]);
10368fe5b6fSgarbled 		    /* FALL THROUGH */
10468fe5b6fSgarbled 		case 1:
10568fe5b6fSgarbled 		    *ctlr = atoi(args[0]);
10668fe5b6fSgarbled 		    break;
10768fe5b6fSgarbled 		}
10868fe5b6fSgarbled 		*file = ++s;
10968fe5b6fSgarbled 	} else {
11068fe5b6fSgarbled 		/* no device present */
11168fe5b6fSgarbled 		*file = (char *)fname;
11268fe5b6fSgarbled 	}
11368fe5b6fSgarbled 	return (0);
11468fe5b6fSgarbled 
11568fe5b6fSgarbled baddev:
11668fe5b6fSgarbled 	return (EINVAL);
11768fe5b6fSgarbled }
11868fe5b6fSgarbled 
11968fe5b6fSgarbled int
devopen(struct open_file * f,const char * fname,char ** file)12068fe5b6fSgarbled devopen(struct open_file *f, const char *fname, char **file)
12168fe5b6fSgarbled {
12268fe5b6fSgarbled 	int error;
12368fe5b6fSgarbled 	int dev = 0, ctlr = 0, unit = 0, part = 0;
12468fe5b6fSgarbled 	int adapt = 0;
12568fe5b6fSgarbled 	struct devsw *dp = &devsw[0];
12668fe5b6fSgarbled 
12768fe5b6fSgarbled 	if ((error =
12868fe5b6fSgarbled 	    devparse(fname, &dev, &adapt, &ctlr, &unit, &part, file)) != 0)
12968fe5b6fSgarbled 		return (error);
13068fe5b6fSgarbled 
13168fe5b6fSgarbled 	dp = &devsw[dev];
13268fe5b6fSgarbled 	if (!dp->dv_open)
13368fe5b6fSgarbled 		return (ENODEV);
13468fe5b6fSgarbled 
13568fe5b6fSgarbled 	f->f_dev = dp;
13668fe5b6fSgarbled 	if ((error = (*dp->dv_open)(f, ctlr, unit, part)) == 0)
13768fe5b6fSgarbled 		return (0);
13868fe5b6fSgarbled 
13968fe5b6fSgarbled 	printf("%s(%d,%d,%d): %s\n", devsw[dev].dv_name,
14068fe5b6fSgarbled 		ctlr, unit, part, strerror(error));
14168fe5b6fSgarbled 
14268fe5b6fSgarbled 	return (error);
14368fe5b6fSgarbled }
144