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