Home | History | Annotate | Line # | Download | only in libsa
devopen.c revision 1.1
      1  1.1  minoura /*	$NetBSD: devopen.c,v 1.1 2001/09/27 10:03:28 minoura Exp $	*/
      2  1.1  minoura 
      3  1.1  minoura /*
      4  1.1  minoura  * Copyright (c) 2001 Minoura Makoto
      5  1.1  minoura  * All rights reserved.
      6  1.1  minoura  *
      7  1.1  minoura  * Redistribution and use in source and binary forms, with or without
      8  1.1  minoura  * modification, are permitted provided that the following conditions
      9  1.1  minoura  * are met:
     10  1.1  minoura  * 1. Redistributions of source code must retain the above copyright
     11  1.1  minoura  *    notice, this list of conditions and the following disclaimer.
     12  1.1  minoura  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  minoura  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  minoura  *    documentation and/or other materials provided with the distribution.
     15  1.1  minoura  *
     16  1.1  minoura  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  1.1  minoura  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  1.1  minoura  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  1.1  minoura  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  1.1  minoura  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  1.1  minoura  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  1.1  minoura  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  1.1  minoura  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  1.1  minoura  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  1.1  minoura  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  1.1  minoura  * SUCH DAMAGE.
     27  1.1  minoura  */
     28  1.1  minoura 
     29  1.1  minoura #include <sys/param.h>
     30  1.1  minoura #include <sys/disklabel.h>
     31  1.1  minoura #include <lib/libkern/libkern.h>
     32  1.1  minoura #include <lib/libsa/stand.h>
     33  1.1  minoura #include "libx68k.h"
     34  1.1  minoura 
     35  1.1  minoura extern struct devspec devspec[]; /* defined in conf.c */
     36  1.1  minoura 
     37  1.1  minoura /*
     38  1.1  minoura  * Parse a device spec.
     39  1.1  minoura  *
     40  1.1  minoura  * sd<unit><part>:<file>
     41  1.1  minoura  *  unit - 0-7
     42  1.1  minoura  *  part - a-p
     43  1.1  minoura  */
     44  1.1  minoura int
     45  1.1  minoura devparse(const char *fname, int *dev, int *unit, int *part, char **file)
     46  1.1  minoura {
     47  1.1  minoura 	char const *s;
     48  1.1  minoura 	int i;
     49  1.1  minoura 
     50  1.1  minoura 	s = fname;
     51  1.1  minoura 	for (i = 0; devspec[i].ds_name != 0; i++) {
     52  1.1  minoura 		if (strncmp (devspec[i].ds_name, s,
     53  1.1  minoura 			     strlen(devspec[i].ds_name)) == 0)
     54  1.1  minoura 			break;
     55  1.1  minoura 	}
     56  1.1  minoura 
     57  1.1  minoura 	if (devspec[i].ds_name == 0)
     58  1.1  minoura 		return (ENODEV);
     59  1.1  minoura 	s += strlen(devspec[i].ds_name);
     60  1.1  minoura 	*dev = devspec[i].ds_dev;
     61  1.1  minoura 
     62  1.1  minoura 	*unit = *s++ - '0';
     63  1.1  minoura 	if (*unit < 0 || *unit > devspec[i].ds_maxunit)
     64  1.1  minoura 		/* bad unit */
     65  1.1  minoura 		return (ENODEV);
     66  1.1  minoura 	*part = *s++ - 'a';
     67  1.1  minoura 	if (*part < 0 || *part > MAXPARTITIONS)
     68  1.1  minoura 		/* bad partition */
     69  1.1  minoura 		return (ENODEV);
     70  1.1  minoura 
     71  1.1  minoura 	if (*s++ != ':')
     72  1.1  minoura 		return (ENODEV);
     73  1.1  minoura 
     74  1.1  minoura 	if (*s == '/')
     75  1.1  minoura 		s++;
     76  1.1  minoura 	(const char*) *file = s;
     77  1.1  minoura 
     78  1.1  minoura 	return 0;
     79  1.1  minoura }
     80  1.1  minoura 
     81  1.1  minoura int
     82  1.1  minoura devopen(struct open_file *f, const char *fname, char **file)
     83  1.1  minoura {
     84  1.1  minoura 	int error;
     85  1.1  minoura 	int dev, unit, part;
     86  1.1  minoura 	struct devsw *dp = &devsw[0];
     87  1.1  minoura 
     88  1.1  minoura 	error = devparse(fname, &dev, &unit, &part, file);
     89  1.1  minoura 	if (error)
     90  1.1  minoura 	    return(error);
     91  1.1  minoura 
     92  1.1  minoura 	dp = &devsw[dev];
     93  1.1  minoura 
     94  1.1  minoura 	if (!dp->dv_open)
     95  1.1  minoura 		return(ENODEV);
     96  1.1  minoura 
     97  1.1  minoura 	f->f_dev = dp;
     98  1.1  minoura 
     99  1.1  minoura 	if ((error = (*dp->dv_open)(f, unit, part)) == 0)
    100  1.1  minoura 	    return(0);
    101  1.1  minoura 
    102  1.1  minoura 	return(error);
    103  1.1  minoura }
    104