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