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