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