Home | History | Annotate | Line # | Download | only in common
devopen.c revision 1.4.6.5
      1  1.4.6.5     skrll /*	$NetBSD: devopen.c,v 1.4.6.5 2005/11/10 13:56:09 skrll Exp $	*/
      2      1.2   thorpej 
      3      1.2   thorpej /*-
      4      1.2   thorpej  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5      1.2   thorpej  * All rights reserved.
      6      1.2   thorpej  *
      7      1.2   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8      1.2   thorpej  * by Jason R. Thorpe.
      9      1.2   thorpej  *
     10      1.2   thorpej  * Redistribution and use in source and binary forms, with or without
     11      1.2   thorpej  * modification, are permitted provided that the following conditions
     12      1.2   thorpej  * are met:
     13      1.2   thorpej  * 1. Redistributions of source code must retain the above copyright
     14      1.2   thorpej  *    notice, this list of conditions and the following disclaimer.
     15      1.2   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16      1.2   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17      1.2   thorpej  *    documentation and/or other materials provided with the distribution.
     18      1.2   thorpej  * 3. All advertising materials mentioning features or use of this software
     19      1.2   thorpej  *    must display the following acknowledgement:
     20      1.2   thorpej  *	This product includes software developed by the NetBSD
     21      1.2   thorpej  *	Foundation, Inc. and its contributors.
     22      1.2   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23      1.2   thorpej  *    contributors may be used to endorse or promote products derived
     24      1.2   thorpej  *    from this software without specific prior written permission.
     25      1.2   thorpej  *
     26      1.2   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27      1.2   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28      1.2   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29      1.2   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30      1.2   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31      1.2   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32      1.2   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33      1.2   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34      1.2   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35      1.2   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36      1.2   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37      1.2   thorpej  */
     38      1.1   thorpej 
     39      1.1   thorpej /*-
     40      1.1   thorpej  *  Copyright (c) 1993 John Brezak
     41      1.1   thorpej  *  All rights reserved.
     42      1.1   thorpej  *
     43      1.1   thorpej  *  Redistribution and use in source and binary forms, with or without
     44      1.1   thorpej  *  modification, are permitted provided that the following conditions
     45      1.1   thorpej  *  are met:
     46      1.1   thorpej  *  1. Redistributions of source code must retain the above copyright
     47      1.1   thorpej  *     notice, this list of conditions and the following disclaimer.
     48      1.1   thorpej  *  2. Redistributions in binary form must reproduce the above copyright
     49      1.1   thorpej  *     notice, this list of conditions and the following disclaimer in the
     50      1.1   thorpej  *     documentation and/or other materials provided with the distribution.
     51      1.1   thorpej  *  3. The name of the author may not be used to endorse or promote products
     52      1.1   thorpej  *     derived from this software without specific prior written permission.
     53      1.1   thorpej  *
     54      1.1   thorpej  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
     55      1.1   thorpej  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     56      1.1   thorpej  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     57      1.1   thorpej  * DISCLAIMED.	IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     58      1.1   thorpej  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     59      1.1   thorpej  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     60      1.1   thorpej  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     61      1.1   thorpej  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     62      1.1   thorpej  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     63      1.1   thorpej  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     64      1.1   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     65      1.1   thorpej  */
     66      1.1   thorpej 
     67      1.1   thorpej #include <sys/param.h>
     68      1.1   thorpej #include <sys/reboot.h>
     69      1.3  gmcgarry 
     70      1.3  gmcgarry #include <lib/libkern/libkern.h>
     71      1.1   thorpej 
     72      1.1   thorpej #include <lib/libsa/stand.h>
     73      1.1   thorpej #include <hp300/stand/common/samachdep.h>
     74      1.1   thorpej 
     75      1.1   thorpej u_int opendev;
     76      1.1   thorpej 
     77      1.1   thorpej #define ispart(c)	((c) >= 'a' && (c) <= 'h')
     78      1.1   thorpej 
     79  1.4.6.1     skrll static void usage(void);
     80  1.4.6.1     skrll static int devlookup(const char * ,int);
     81  1.4.6.1     skrll static int devparse(const char *, int *, int*, int*, int*, int*, char **);
     82  1.4.6.1     skrll 
     83  1.4.6.1     skrll int
     84      1.1   thorpej atoi(char *cp)
     85      1.1   thorpej {
     86  1.4.6.1     skrll 	int val = 0;
     87  1.4.6.1     skrll 
     88  1.4.6.5     skrll 	while (isdigit((unsigned char)*cp))
     89  1.4.6.1     skrll 		val = val * 10 + (*cp++ - '0');
     90  1.4.6.1     skrll 	return val;
     91      1.1   thorpej }
     92      1.1   thorpej 
     93  1.4.6.1     skrll void
     94  1.4.6.1     skrll usage(void)
     95      1.1   thorpej {
     96  1.4.6.1     skrll 
     97  1.4.6.1     skrll 	printf("Usage: device(adaptor, controller, drive, partition)file\n"
     98  1.4.6.1     skrll 	    "       <device><unit><partitionletter>:file\n");
     99      1.1   thorpej }
    100      1.1   thorpej 
    101  1.4.6.1     skrll static int
    102  1.4.6.4     skrll devlookup(const char *d, int len)
    103      1.1   thorpej {
    104  1.4.6.1     skrll 	struct devsw *dp = devsw;
    105  1.4.6.1     skrll 	int i;
    106      1.1   thorpej 
    107  1.4.6.1     skrll 	for (i = 0; i < ndevs; i++, dp++) {
    108  1.4.6.1     skrll 		if (dp->dv_name && strncmp(dp->dv_name, d, len) == 0) {
    109  1.4.6.1     skrll 			/*
    110  1.4.6.1     skrll 			 * Set the filesystem and startup up according to
    111  1.4.6.1     skrll 			 * the device being opened.
    112  1.4.6.1     skrll 			 */
    113  1.4.6.1     skrll 			switch (i) {
    114  1.4.6.1     skrll 			case 0:	/* ct */
    115  1.4.6.1     skrll 				memcpy(file_system, file_system_rawfs,
    116  1.4.6.1     skrll 				    sizeof(struct fs_ops));
    117  1.4.6.1     skrll 				break;
    118  1.4.6.1     skrll 
    119  1.4.6.1     skrll 			case 2:	/* rd */
    120  1.4.6.1     skrll 			case 4:	/* sd */
    121  1.4.6.1     skrll 				memcpy(file_system, file_system_ufs,
    122  1.4.6.1     skrll 				    sizeof(struct fs_ops));
    123  1.4.6.1     skrll 				break;
    124  1.4.6.1     skrll 
    125  1.4.6.1     skrll 			case 6:	/* le */
    126  1.4.6.1     skrll 				memcpy(file_system, file_system_nfs,
    127  1.4.6.1     skrll 				    sizeof(struct fs_ops));
    128  1.4.6.1     skrll 				break;
    129  1.4.6.1     skrll 
    130  1.4.6.1     skrll 			default:
    131  1.4.6.1     skrll 				/* Agh!  What happened?! */
    132  1.4.6.1     skrll 				goto bad;
    133  1.4.6.1     skrll 			}
    134  1.4.6.1     skrll 		return i;
    135  1.4.6.1     skrll 		}
    136      1.1   thorpej 	}
    137      1.1   thorpej 
    138      1.1   thorpej  bad:
    139  1.4.6.1     skrll 	printf("No such device - Configured devices are:\n");
    140  1.4.6.1     skrll 	for (dp = devsw, i = 0; i < ndevs; i++, dp++)
    141  1.4.6.1     skrll 		if (dp->dv_name)
    142  1.4.6.1     skrll 			printf(" %s", dp->dv_name);
    143  1.4.6.1     skrll 	printf("\n");
    144  1.4.6.1     skrll 	errno = ENODEV;
    145  1.4.6.1     skrll 	return -1;
    146      1.1   thorpej }
    147      1.1   thorpej 
    148      1.1   thorpej /*
    149      1.1   thorpej  * Parse a device spec in one of two forms.
    150      1.1   thorpej  *
    151      1.1   thorpej  * dev(adapt, ctlr, unit, part)file
    152      1.1   thorpej  * [A-Za-z]*[0-9]*[A-Za-z]:file
    153      1.1   thorpej  *    dev   unit  part
    154      1.1   thorpej  */
    155  1.4.6.1     skrll static int
    156  1.4.6.4     skrll devparse(const char *fname, int *dev, int *adapt, int *ctlr, int *unit,
    157  1.4.6.4     skrll     int *part, char **file)
    158      1.1   thorpej {
    159  1.4.6.1     skrll 	int i;
    160  1.4.6.1     skrll 	char *s, *args[4];
    161      1.1   thorpej 
    162  1.4.6.1     skrll 	/* get device name */
    163  1.4.6.1     skrll 	for (s = (char *)fname; *s && *s != '/' && *s != ':' && *s != '('; s++)
    164  1.4.6.1     skrll 		;
    165  1.4.6.1     skrll 
    166  1.4.6.1     skrll 	/* first form */
    167  1.4.6.1     skrll 	if (*s == '(') {
    168  1.4.6.1     skrll 		/* lookup device and get index */
    169  1.4.6.1     skrll 		if ((*dev = devlookup(fname, s - fname)) < 0)
    170  1.4.6.1     skrll 			goto baddev;
    171  1.4.6.1     skrll 
    172  1.4.6.1     skrll 		/* tokenize device ident */
    173  1.4.6.1     skrll 		args[0] = ++s;
    174  1.4.6.1     skrll 		for (args[0] = s, i = 1; *s && *s != ')'; s++) {
    175  1.4.6.1     skrll 			if (*s == ',')
    176  1.4.6.1     skrll 				args[i++] = ++s;
    177  1.4.6.1     skrll 		}
    178  1.4.6.1     skrll 		switch(i) {
    179  1.4.6.1     skrll 		case 4:
    180  1.4.6.1     skrll 			*adapt = atoi(args[0]);
    181  1.4.6.1     skrll 			*ctlr  = atoi(args[1]);
    182  1.4.6.1     skrll 			*unit  = atoi(args[2]);
    183  1.4.6.1     skrll 			*part  = atoi(args[3]);
    184  1.4.6.1     skrll 			break;
    185  1.4.6.1     skrll 		case 3:
    186  1.4.6.1     skrll 			*ctlr  = atoi(args[0]);
    187  1.4.6.1     skrll 			*unit  = atoi(args[1]);
    188  1.4.6.1     skrll 			*part  = atoi(args[2]);
    189  1.4.6.1     skrll 			break;
    190  1.4.6.1     skrll 		case 2:
    191  1.4.6.1     skrll 			*unit  = atoi(args[0]);
    192  1.4.6.1     skrll 			*part  = atoi(args[1]);
    193  1.4.6.1     skrll 			break;
    194  1.4.6.1     skrll 		case 1:
    195  1.4.6.1     skrll 			*part  = atoi(args[0]);
    196  1.4.6.1     skrll 			break;
    197  1.4.6.1     skrll 		case 0:
    198  1.4.6.1     skrll 			break;
    199  1.4.6.1     skrll 		}
    200  1.4.6.1     skrll 		*file = ++s;
    201      1.1   thorpej 	}
    202      1.1   thorpej 
    203  1.4.6.1     skrll 	/* second form */
    204  1.4.6.1     skrll 	else if (*s == ':') {
    205  1.4.6.1     skrll 		int temp;
    206      1.1   thorpej 
    207  1.4.6.1     skrll 		/* isolate device */
    208  1.4.6.1     skrll 		for (s = (char *)fname; *s != ':' && !isdigit(*s); s++);
    209      1.1   thorpej 
    210  1.4.6.1     skrll 			/* lookup device and get index */
    211  1.4.6.1     skrll 			if ((*dev = devlookup(fname, s - fname)) < 0)
    212  1.4.6.1     skrll 				goto baddev;
    213  1.4.6.1     skrll 
    214  1.4.6.1     skrll 		/* isolate unit */
    215  1.4.6.1     skrll 		if ((temp = atoi(s)) > 255)
    216  1.4.6.1     skrll 			goto bad;
    217  1.4.6.1     skrll 		*adapt = temp / 8;
    218  1.4.6.1     skrll 		*ctlr = temp % 8;
    219  1.4.6.1     skrll 		for (; isdigit(*s); s++)
    220  1.4.6.1     skrll 			;
    221      1.1   thorpej 
    222  1.4.6.1     skrll 		/* translate partition */
    223  1.4.6.1     skrll 		if (!ispart(*s))
    224  1.4.6.1     skrll 			goto bad;
    225      1.1   thorpej 
    226  1.4.6.1     skrll 		*part = *s++ - 'a';
    227  1.4.6.1     skrll 		if (*s != ':')
    228  1.4.6.1     skrll 			goto bad;
    229  1.4.6.1     skrll 		*file = ++s;
    230  1.4.6.1     skrll 	}
    231  1.4.6.1     skrll 
    232  1.4.6.1     skrll 	/* no device present */
    233  1.4.6.1     skrll 	else
    234  1.4.6.1     skrll 		*file = (char *)fname;
    235      1.1   thorpej 
    236  1.4.6.1     skrll 	/* return the remaining unparsed part as the file to boot */
    237  1.4.6.1     skrll 	return 0;
    238      1.1   thorpej 
    239      1.1   thorpej  bad:
    240  1.4.6.1     skrll 	usage();
    241      1.1   thorpej 
    242      1.1   thorpej  baddev:
    243  1.4.6.1     skrll 	return -1;
    244      1.1   thorpej }
    245      1.1   thorpej 
    246      1.1   thorpej 
    247  1.4.6.1     skrll int
    248  1.4.6.4     skrll devopen(struct open_file *f, const char *fname, char **file)
    249      1.1   thorpej {
    250  1.4.6.1     skrll 	int error;
    251      1.1   thorpej 	int dev, adapt, ctlr, unit, part;
    252      1.1   thorpej 	struct devsw *dp = &devsw[0];
    253      1.1   thorpej 
    254      1.1   thorpej 	dev   = B_TYPE(bootdev);
    255      1.1   thorpej 	adapt = B_ADAPTOR(bootdev);
    256      1.1   thorpej 	ctlr  = B_CONTROLLER(bootdev);
    257      1.1   thorpej 	unit  = B_UNIT(bootdev);
    258      1.1   thorpej 	part  = B_PARTITION(bootdev);
    259      1.1   thorpej 
    260  1.4.6.1     skrll 	if ((error = devparse(fname, &dev, &adapt, &ctlr, &unit, &part, file))
    261  1.4.6.1     skrll 	    != 0)
    262  1.4.6.1     skrll 		return error;
    263      1.1   thorpej 
    264      1.1   thorpej 	/*
    265      1.1   thorpej 	 * Set up filesystem type based on what device we're opening.
    266      1.1   thorpej 	 */
    267      1.1   thorpej 	switch (dev) {
    268      1.1   thorpej 	case 0:		/* ct */
    269  1.4.6.1     skrll 		memcpy(file_system, file_system_rawfs, sizeof(struct fs_ops));
    270      1.1   thorpej 		break;
    271      1.1   thorpej 
    272      1.1   thorpej 	case 2:		/* rd */
    273      1.1   thorpej 	case 4:		/* sd */
    274  1.4.6.1     skrll 		memcpy(file_system, file_system_ufs, sizeof(struct fs_ops));
    275      1.1   thorpej 		break;
    276      1.1   thorpej 
    277      1.1   thorpej 	case 6:		/* le */
    278  1.4.6.1     skrll 		memcpy(file_system, file_system_nfs, sizeof(struct fs_ops));
    279      1.1   thorpej 		break;
    280      1.1   thorpej 
    281      1.1   thorpej 	default:
    282      1.1   thorpej 		/* XXX what else should we do here? */
    283      1.1   thorpej 		printf("WARNING: BOGUS BOOT DEV TYPE 0x%x!\n", dev);
    284  1.4.6.1     skrll 		return EIO;
    285      1.1   thorpej 	}
    286      1.1   thorpej 
    287      1.1   thorpej 	dp = &devsw[dev];
    288      1.1   thorpej 
    289      1.1   thorpej 	if (!dp->dv_open)
    290  1.4.6.1     skrll 		return ENODEV;
    291      1.1   thorpej 
    292      1.1   thorpej 	f->f_dev = dp;
    293      1.1   thorpej 
    294      1.1   thorpej 	if ((error = (*dp->dv_open)(f, adapt, ctlr, part)) == 0) {
    295      1.1   thorpej 		if ((error =
    296      1.1   thorpej 		    (*punitsw[dev].p_punit)(adapt, ctlr, &unit)) != 0) {
    297      1.1   thorpej 			goto bad;
    298      1.1   thorpej 		}
    299      1.1   thorpej 		opendev = MAKEBOOTDEV(dev, adapt, ctlr, unit, part);
    300  1.4.6.1     skrll 		return 0;
    301      1.1   thorpej 	}
    302      1.1   thorpej 
    303      1.1   thorpej  bad:
    304      1.1   thorpej 	printf("%s(%d,%d,%d,%d): %s\n", devsw[dev].dv_name,
    305      1.1   thorpej 	    adapt, ctlr, unit, part, strerror(error));
    306      1.1   thorpej 
    307  1.4.6.1     skrll 	return error;
    308      1.1   thorpej }
    309