1 /* $NetBSD: devopen.c,v 1.4 2002/04/08 16:05:58 sakamoto 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 <stand.h> 32 #include <lib/libkern/libkern.h> 33 #include <sys/param.h> 34 #include <sys/reboot.h> 35 36 #define ispart(c) ((c) >= 'a' && (c) <= 'h') 37 38 int 39 atoi(cp) 40 char *cp; 41 { 42 int val = 0; 43 44 while (isdigit(*cp)) 45 val = val * 10 + (*cp++ - '0'); 46 return (val); 47 } 48 49 int 50 devlookup(d) 51 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(fname, dev, adapt, ctlr, unit, part, file) 74 const char *fname; 75 int *dev; 76 int *adapt; 77 int *ctlr; 78 int *unit; 79 int *part; 80 char **file; 81 { 82 int argc, flag; 83 char *s, *args[3]; 84 extern char nametmp[]; 85 86 /* get device name and make lower case */ 87 strcpy(nametmp, (char *)fname); 88 for (s = nametmp; *s && *s != '('; s++) 89 if (isupper(*s)) *s = tolower(*s); 90 91 if (*s == '(') { 92 /* lookup device and get index */ 93 *s = NULL; 94 if ((*dev = devlookup(nametmp, s - nametmp)) < 0) 95 goto baddev; 96 97 /* tokenize device ident */ 98 for (++s, flag = 0, argc = 0; *s && *s != ')'; s++) { 99 if (*s != ',') { 100 if (!flag) { 101 flag++; 102 args[argc++] = s; 103 } 104 } else { 105 if (flag) { 106 *s = NULL; 107 flag = 0; 108 } 109 } 110 } 111 if (*s == ')') 112 *s = NULL; 113 114 switch (argc) { 115 case 3: 116 *part = atoi(args[2]); 117 /* FALL THROUGH */ 118 case 2: 119 *unit = atoi(args[1]); 120 /* FALL THROUGH */ 121 case 1: 122 *ctlr = atoi(args[0]); 123 break; 124 } 125 *file = ++s; 126 } else { 127 /* no device present */ 128 *file = (char *)fname; 129 } 130 return (0); 131 132 baddev: 133 return (EINVAL); 134 } 135 136 int 137 devopen(f, fname, file) 138 struct open_file *f; 139 const char *fname; 140 char **file; 141 { 142 int error; 143 int dev = 0, ctlr = 0, unit = 0, part = 0; 144 int adapt = 0; 145 struct devsw *dp = &devsw[0]; 146 147 if ((error = 148 devparse(fname, &dev, &adapt, &ctlr, &unit, &part, file)) != 0) 149 return (error); 150 151 dp = &devsw[dev]; 152 if (!dp->dv_open) 153 return (ENODEV); 154 155 f->f_dev = dp; 156 if ((error = (*dp->dv_open)(f, ctlr, unit, part)) == 0) 157 return (0); 158 159 printf("%s(%d,%d,%d): %s\n", devsw[dev].dv_name, 160 ctlr, unit, part, strerror(error)); 161 162 return (error); 163 } 164