Home | History | Annotate | Line # | Download | only in common
devopen.c revision 1.13.8.2
      1  1.13.8.2  bouyer /*	$NetBSD: devopen.c,v 1.13.8.2 2000/11/20 20:20:44 bouyer Exp $	*/
      2  1.13.8.2  bouyer 
      3  1.13.8.2  bouyer /*-
      4  1.13.8.2  bouyer  * Copyright (c) 1992, 1993
      5  1.13.8.2  bouyer  *	The Regents of the University of California.  All rights reserved.
      6  1.13.8.2  bouyer  *
      7  1.13.8.2  bouyer  * This code is derived from software contributed to Berkeley by
      8  1.13.8.2  bouyer  * Ralph Campbell.
      9  1.13.8.2  bouyer  *
     10  1.13.8.2  bouyer  * Redistribution and use in source and binary forms, with or without
     11  1.13.8.2  bouyer  * modification, are permitted provided that the following conditions
     12  1.13.8.2  bouyer  * are met:
     13  1.13.8.2  bouyer  * 1. Redistributions of source code must retain the above copyright
     14  1.13.8.2  bouyer  *    notice, this list of conditions and the following disclaimer.
     15  1.13.8.2  bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.13.8.2  bouyer  *    notice, this list of conditions and the following disclaimer in the
     17  1.13.8.2  bouyer  *    documentation and/or other materials provided with the distribution.
     18  1.13.8.2  bouyer  * 3. All advertising materials mentioning features or use of this software
     19  1.13.8.2  bouyer  *    must display the following acknowledgement:
     20  1.13.8.2  bouyer  *	This product includes software developed by the University of
     21  1.13.8.2  bouyer  *	California, Berkeley and its contributors.
     22  1.13.8.2  bouyer  * 4. Neither the name of the University nor the names of its contributors
     23  1.13.8.2  bouyer  *    may be used to endorse or promote products derived from this software
     24  1.13.8.2  bouyer  *    without specific prior written permission.
     25  1.13.8.2  bouyer  *
     26  1.13.8.2  bouyer  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.13.8.2  bouyer  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.13.8.2  bouyer  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.13.8.2  bouyer  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.13.8.2  bouyer  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.13.8.2  bouyer  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.13.8.2  bouyer  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.13.8.2  bouyer  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.13.8.2  bouyer  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.13.8.2  bouyer  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.13.8.2  bouyer  * SUCH DAMAGE.
     37  1.13.8.2  bouyer  *
     38  1.13.8.2  bouyer  *	@(#)devopen.c	8.1 (Berkeley) 6/10/93
     39  1.13.8.2  bouyer  */
     40  1.13.8.2  bouyer 
     41  1.13.8.2  bouyer #include <lib/libsa/stand.h>
     42  1.13.8.2  bouyer 
     43  1.13.8.2  bouyer /*
     44  1.13.8.2  bouyer  * Decode the string 'fname', open the device and return the remaining
     45  1.13.8.2  bouyer  * file name if any.
     46  1.13.8.2  bouyer  */
     47  1.13.8.2  bouyer int
     48  1.13.8.2  bouyer devopen(f, fname, file)
     49  1.13.8.2  bouyer 	struct open_file *f;
     50  1.13.8.2  bouyer 	const char *fname;
     51  1.13.8.2  bouyer 	char **file;	/* out */
     52  1.13.8.2  bouyer {
     53  1.13.8.2  bouyer 	int ctlr = 0, unit = 0, part = 0;
     54  1.13.8.2  bouyer 	int c, rc;
     55  1.13.8.2  bouyer 	char namebuf[20];
     56  1.13.8.2  bouyer 	const char *cp;
     57  1.13.8.2  bouyer 	char *ncp;
     58  1.13.8.2  bouyer #if !defined(LIBSA_SINGLE_DEVICE)
     59  1.13.8.2  bouyer 	int i;
     60  1.13.8.2  bouyer 	struct devsw *dp;
     61  1.13.8.2  bouyer #endif
     62  1.13.8.2  bouyer 
     63  1.13.8.2  bouyer 	cp = fname;
     64  1.13.8.2  bouyer 	ncp = namebuf;
     65  1.13.8.2  bouyer 
     66  1.13.8.2  bouyer 	/*
     67  1.13.8.2  bouyer 	 * look for a string like '5/rz0/netbsd' or '5/rz3f/netbsd
     68  1.13.8.2  bouyer 	 * or 3/tftp/netbsd
     69  1.13.8.2  bouyer 	 */
     70  1.13.8.2  bouyer 	if ((c = *cp) >= '0' && c <= '9') {
     71  1.13.8.2  bouyer 		ctlr = c - '0';
     72  1.13.8.2  bouyer 		/* skip the '/' */
     73  1.13.8.2  bouyer 		if (*++cp != '/')
     74  1.13.8.2  bouyer 			return (ENXIO);
     75  1.13.8.2  bouyer 		cp++;
     76  1.13.8.2  bouyer 		while ((c = *cp) != '\0') {
     77  1.13.8.2  bouyer 			if (c == '/')
     78  1.13.8.2  bouyer 				break;
     79  1.13.8.2  bouyer 			if (c >= '0' && c <= '9') {
     80  1.13.8.2  bouyer 				/* read unit number */
     81  1.13.8.2  bouyer 				unit = c - '0';
     82  1.13.8.2  bouyer 
     83  1.13.8.2  bouyer 				/* look for a partition */
     84  1.13.8.2  bouyer 				if ((c = *++cp) >= 'a' && c <= 'h') {
     85  1.13.8.2  bouyer 					part = c - 'a';
     86  1.13.8.2  bouyer 					c = *++cp;
     87  1.13.8.2  bouyer 				}
     88  1.13.8.2  bouyer 				if (c != '/')
     89  1.13.8.2  bouyer 					return (ENXIO);
     90  1.13.8.2  bouyer 				cp++;
     91  1.13.8.2  bouyer 				break;
     92  1.13.8.2  bouyer 			}
     93  1.13.8.2  bouyer 			if (ncp < namebuf + sizeof(namebuf) - 1)
     94  1.13.8.2  bouyer 				*ncp++ = c;
     95  1.13.8.2  bouyer 			cp++;
     96  1.13.8.2  bouyer 		}
     97  1.13.8.2  bouyer 	} else {
     98  1.13.8.2  bouyer 		/* expect a string like 'rz(0,0,0)netbsd' */
     99  1.13.8.2  bouyer 		while ((c = *cp) != '\0') {
    100  1.13.8.2  bouyer 			if (c == '(') {
    101  1.13.8.2  bouyer 				cp++;
    102  1.13.8.2  bouyer 				break;
    103  1.13.8.2  bouyer 			}
    104  1.13.8.2  bouyer 			if (ncp < namebuf + sizeof(namebuf) - 1)
    105  1.13.8.2  bouyer 				*ncp++ = c;
    106  1.13.8.2  bouyer 			cp++;
    107  1.13.8.2  bouyer 		}
    108  1.13.8.2  bouyer 
    109  1.13.8.2  bouyer 		/* get controller number */
    110  1.13.8.2  bouyer 		if ((c = *cp) >= '0' && c <= '9') {
    111  1.13.8.2  bouyer 			ctlr = c - '0';
    112  1.13.8.2  bouyer 			c = *++cp;
    113  1.13.8.2  bouyer 		}
    114  1.13.8.2  bouyer 
    115  1.13.8.2  bouyer 		if (c == ',') {
    116  1.13.8.2  bouyer 			/* get SCSI device number */
    117  1.13.8.2  bouyer 			if ((c = *++cp) >= '0' && c <= '9') {
    118  1.13.8.2  bouyer 				unit = c - '0';
    119  1.13.8.2  bouyer 				c = *++cp;
    120  1.13.8.2  bouyer 			}
    121  1.13.8.2  bouyer 
    122  1.13.8.2  bouyer 			if (c == ',') {
    123  1.13.8.2  bouyer 				/* get partition number */
    124  1.13.8.2  bouyer 				if ((c = *++cp) >= '0' && c <= '9') {
    125  1.13.8.2  bouyer 					part = c - '0';
    126  1.13.8.2  bouyer 					c = *++cp;
    127  1.13.8.2  bouyer 				}
    128  1.13.8.2  bouyer 			}
    129  1.13.8.2  bouyer 		}
    130  1.13.8.2  bouyer 		if (c != ')')
    131  1.13.8.2  bouyer 			return (ENXIO);
    132  1.13.8.2  bouyer 		cp++;
    133  1.13.8.2  bouyer 	}
    134  1.13.8.2  bouyer 	*ncp = '\0';
    135  1.13.8.2  bouyer 
    136  1.13.8.2  bouyer #ifdef LIBSA_SINGLE_DEVICE
    137  1.13.8.2  bouyer 	rc = DEV_OPEN(dp)(f, ctlr, unit, part);
    138  1.13.8.2  bouyer #else /* !LIBSA_SINGLE_DEVICE */
    139  1.13.8.2  bouyer 	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
    140  1.13.8.2  bouyer 		if (dp->dv_name && strcmp(namebuf, dp->dv_name) == 0)
    141  1.13.8.2  bouyer 			goto fnd;
    142  1.13.8.2  bouyer 	printf("Unknown device '%s'\nKnown devices are:", namebuf);
    143  1.13.8.2  bouyer 	for (dp = devsw, i = 0; i < ndevs; dp++, i++)
    144  1.13.8.2  bouyer 		if (dp->dv_name)
    145  1.13.8.2  bouyer 			printf(" %s", dp->dv_name);
    146  1.13.8.2  bouyer 	printf("\n");
    147  1.13.8.2  bouyer 	return (ENXIO);
    148  1.13.8.2  bouyer 
    149  1.13.8.2  bouyer fnd:
    150  1.13.8.2  bouyer #ifdef BOOTNET
    151  1.13.8.2  bouyer 	if (strcmp(namebuf, "tftp") == 0)
    152  1.13.8.2  bouyer 		rc = (dp->dv_open)(f, namebuf);
    153  1.13.8.2  bouyer 	else
    154  1.13.8.2  bouyer #endif /* BOOTNET */
    155  1.13.8.2  bouyer 		rc = (dp->dv_open)(f, ctlr, unit, part);
    156  1.13.8.2  bouyer #endif /* !LIBSA_SINGLE_DEVICE */
    157  1.13.8.2  bouyer 	if (rc)
    158  1.13.8.2  bouyer 		return (rc);
    159  1.13.8.2  bouyer 
    160  1.13.8.2  bouyer #ifndef LIBSA_SINGLE_DEVICE
    161  1.13.8.2  bouyer 	f->f_dev = dp;
    162  1.13.8.2  bouyer #endif
    163  1.13.8.2  bouyer 	if (file && *cp != '\0')
    164  1.13.8.2  bouyer 		*file = (char *)cp;	/* XXX */
    165  1.13.8.2  bouyer 	return (0);
    166  1.13.8.2  bouyer }
    167