1 /* $NetBSD: devopen.c,v 1.4 2012/05/19 14:40:13 kiyohara Exp $ */ 2 3 /*- 4 * Copyright (c) 1993 John Brezak 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <lib/libsa/stand.h> 32 #include <lib/libkern/libkern.h> 33 34 #define ispart(c) ((c) >= 'a' && (c) <= 'h') 35 36 int atoi(char *); 37 int devlookup(char *); 38 int devparse(const char *, int *, int *, int *, int *, int *, char **); 39 40 int 41 atoi(char *cp) 42 { 43 int val = 0; 44 45 while (isdigit(*cp)) 46 val = val * 10 + (*cp++ - '0'); 47 return (val); 48 } 49 50 int 51 devlookup(char *d) 52 { 53 struct devsw *dp = devsw; 54 int i; 55 56 for (i = 0; i < ndevs; i++, dp++) 57 if (dp->dv_name && strcmp(dp->dv_name, d) == 0) 58 return (i); 59 60 printf("No such device - Configured devices are:\n"); 61 for (dp = devsw, i = 0; i < ndevs; i++, dp++) 62 if (dp->dv_name) 63 printf(" %s", dp->dv_name); 64 printf("\n"); 65 return (-1); 66 } 67 68 /* 69 * Parse a device spec in one of two forms. 70 * dev(ctlr, unit, part)file 71 */ 72 int 73 devparse(const char *fname, int *dev, int *adapt, int *ctlr, int *unit, 74 int *part, char **file) 75 { 76 int argc, flag; 77 char *s, *args[3]; 78 extern char nametmp[]; 79 80 /* get device name and make lower case */ 81 strcpy(nametmp, (char *)fname); 82 for (s = nametmp; *s && *s != '('; s++) 83 if (isupper(*s)) *s = tolower(*s); 84 85 if (*s == '(') { 86 /* lookup device and get index */ 87 *s = NULL; 88 if ((*dev = devlookup(nametmp)) < 0) 89 goto baddev; 90 91 /* tokenize device ident */ 92 for (++s, flag = 0, argc = 0; *s && *s != ')'; s++) { 93 if (*s != ',') { 94 if (!flag) { 95 flag++; 96 args[argc++] = s; 97 } 98 } else { 99 if (flag) { 100 *s = NULL; 101 flag = 0; 102 } 103 } 104 } 105 if (*s == ')') 106 *s = NULL; 107 108 switch (argc) { 109 case 3: 110 *part = atoi(args[2]); 111 /* FALL THROUGH */ 112 case 2: 113 *unit = atoi(args[1]); 114 /* FALL THROUGH */ 115 case 1: 116 *ctlr = atoi(args[0]); 117 break; 118 } 119 *file = ++s; 120 } else { 121 /* no device present */ 122 *file = (char *)fname; 123 } 124 return (0); 125 126 baddev: 127 return (EINVAL); 128 } 129 130 int 131 devopen(struct open_file *f, const char *fname, char **file) 132 { 133 int error; 134 int dev = 0, ctlr = 0, unit = 0, part = 0; 135 int adapt = 0; 136 struct devsw *dp = &devsw[0]; 137 extern struct devsw pseudo_devsw; 138 139 error = devparse(fname, &dev, &adapt, &ctlr, &unit, &part, file); 140 if (error == 0) { 141 dp = &devsw[dev]; 142 if (!dp->dv_open) 143 return (ENODEV); 144 } else { 145 if (strcmp(fname, "in()") == 0) { 146 /* special case: kernel in memory */ 147 dp = &pseudo_devsw; 148 printf("in memory kernel\n"); 149 } else 150 return (error); 151 } 152 153 f->f_dev = dp; 154 if ((error = (*dp->dv_open)(f, ctlr, unit, part)) == 0) 155 return (0); 156 157 if (strcmp(fname, "in()") == 0) 158 printf("in(): %s\n", strerror(error)); 159 else 160 printf("%s(%d,%d,%d): %s\n", dp->dv_name, 161 ctlr, unit, part, strerror(error)); 162 163 return (error); 164 } 165