1 /* $OpenBSD: devopen.c,v 1.2 2013/10/29 21:49:07 miod Exp $ */ 2 /* $NetBSD: devopen.c,v 1.3 2013/01/16 15:46:20 tsutsui Exp $ */ 3 4 /* 5 * Copyright (c) 1992 OMRON Corporation. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * OMRON Corporation. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * @(#)conf.c 8.1 (Berkeley) 6/10/93 39 */ 40 /* 41 * Copyright (c) 1992, 1993 42 * The Regents of the University of California. All rights reserved. 43 * 44 * This code is derived from software contributed to Berkeley by 45 * OMRON Corporation. 46 * 47 * Redistribution and use in source and binary forms, with or without 48 * modification, are permitted provided that the following conditions 49 * are met: 50 * 1. Redistributions of source code must retain the above copyright 51 * notice, this list of conditions and the following disclaimer. 52 * 2. Redistributions in binary form must reproduce the above copyright 53 * notice, this list of conditions and the following disclaimer in the 54 * documentation and/or other materials provided with the distribution. 55 * 3. Neither the name of the University nor the names of its contributors 56 * may be used to endorse or promote products derived from this software 57 * without specific prior written permission. 58 * 59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 69 * SUCH DAMAGE. 70 * 71 * @(#)conf.c 8.1 (Berkeley) 6/10/93 72 */ 73 74 #include <lib/libkern/libkern.h> 75 #include <luna88k/stand/boot/samachdep.h> 76 #include <machine/disklabel.h> 77 78 #define MAXDEVNAME 16 79 80 static int make_device(const char *, int *, int *, int *, char **); 81 82 int 83 devopen(struct open_file *f, const char *fname, char **file) 84 { 85 int dev, unit, part; 86 int error; 87 struct devsw *dp; 88 int i; 89 90 if (make_device(fname, &dev, &unit, &part, file) != 0) 91 return ENXIO; 92 93 #ifdef DEBUG 94 printf("%s: %s(%d,%d)%s\n", __func__, 95 devsw[dev].dv_name, unit, part, *file); 96 #endif 97 dp = &devsw[dev]; 98 error = (*dp->dv_open)(f, unit, part); 99 if (error != 0) { 100 #ifdef DEBUG 101 printf("%s: open %s(%d,%d)%s failed (%s)\n", __func__, 102 devsw[dev].dv_name, unit, part, *file, strerror(error)); 103 #endif 104 return error; 105 } 106 107 for (i = 0; i < nfsys_disk; i++) 108 file_system[i] = file_system_disk[i]; 109 nfsys = nfsys_disk; 110 111 #ifdef SUPPORT_ETHERNET 112 if (strcmp(dp->dv_name, "le") == 0) { 113 /* XXX mixing local fs_ops on netboot could be troublesome */ 114 file_system[0] = file_system_nfs[0]; 115 nfsys = 1; 116 } 117 #endif 118 119 f->f_dev = dp; 120 121 return 0; 122 } 123 124 int 125 make_device(const char *str, int *devp, int *unitp, int *partp, char **fname) 126 { 127 const char *cp; 128 struct devsw *dp; 129 int major, unit = 0, part = 0; 130 int i; 131 char devname[MAXDEVNAME + 1]; 132 133 /* 134 * parse path strings 135 */ 136 /* find end of dev type name */ 137 for (cp = str, i = 0; *cp != '\0' && *cp != '(' && i < MAXDEVNAME; i++) 138 devname[i] = *cp++; 139 if (*cp != '(') { 140 return (-1); 141 } 142 devname[i] = '\0'; 143 /* compare dev type name */ 144 for (dp = devsw; dp->dv_name; dp++) 145 if (!strcmp(devname, dp->dv_name)) 146 break; 147 cp++; 148 if (dp->dv_name == NULL) { 149 return (-1); 150 } 151 major = dp - devsw; 152 /* get mixed controller and unit number */ 153 for (; *cp != ',' && *cp != ')'; cp++) { 154 if (*cp == '\0') 155 return -1; 156 if (*cp >= '0' && *cp <= '9') 157 unit = unit * 10 + *cp - '0'; 158 } 159 if (unit < 0 || unit >= 20 || (unit % 10) > 7) { 160 #ifdef DEBUG 161 printf("%s: invalid unit number (%d)\n", __func__, unit); 162 #endif 163 return (-1); 164 } 165 /* get optional partition number */ 166 if (*cp == ',') 167 cp++; 168 169 for (; /* *cp != ',' && */ *cp != ')'; cp++) { 170 if (*cp == '\0') 171 return -1; 172 if (*cp >= '0' && *cp <= '9') 173 part = part * 10 + *cp - '0'; 174 } 175 if (part < 0 || part >= MAXPARTITIONS) { 176 #ifdef DEBUG 177 printf("%s: invalid partition number (%d)\n", __func__, part); 178 #endif 179 return (-1); 180 } 181 /* check out end of dev spec */ 182 *devp = major; 183 *unitp = unit; 184 *partp = part; 185 cp++; 186 if (*cp == '\0') 187 *fname = "bsd"; 188 else 189 *fname = (char *)cp; /* XXX */ 190 #ifdef DEBUG 191 printf("%s: major = %d, unit = %d, part = %d, fname = %s\n", 192 __func__, major, unit, part, *fname); 193 #endif 194 195 return 0; 196 } 197