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