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