1 /* $NetBSD: devopen.c,v 1.1 2003/06/25 17:24:22 cdi Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Rolf Grossmann. 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 NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <lib/libsa/stand.h> 40 #include <lib/libkern/libkern.h> 41 42 #include "boot.h" 43 44 /* 45 * Parse a device spec. 46 * 47 * Format: 48 * [device:][filename] 49 */ 50 int 51 devparse(const char *fname, int *dev, u_int8_t *unit, 52 u_int8_t *part, const char **file) 53 { 54 const char *col; 55 56 *unit = 0; /* default to wd0a */ 57 *part = 0; 58 *dev = 0; 59 *file = DEFKERNELNAME; 60 61 if (fname == NULL) 62 return (0); 63 64 if ( (col = strchr(fname, ':')) != NULL) { 65 int devlen; 66 u_int8_t i, u, p; 67 struct devsw *dp; 68 char devname[MAXDEVNAME]; 69 70 devlen = col - fname; 71 if (devlen > MAXDEVNAME) 72 return (EINVAL); 73 74 #define isnum(c) (((c) >= '0') && ((c) <= '9')) 75 #define isalpha(c) (((c) >= 'a') && ((c) <= 'z')) 76 77 /* extract device name */ 78 for (i = 0; isalpha(fname[i]) && (i < devlen); i++) 79 devname[i] = fname[i]; 80 devname[i] = 0; 81 82 if (!isnum(fname[i])) 83 return (EUNIT); 84 85 /* device number */ 86 for (u = 0; isnum(fname[i]) && (i < devlen); i++) 87 u = u * 10 + (fname[i] - '0'); 88 89 if (!isalpha(fname[i])) 90 return (EPART); 91 92 /* partition number */ 93 if (i < devlen) 94 p = fname[i++] - 'a'; 95 96 if (i != devlen) 97 return (ENXIO); 98 99 /* check device name */ 100 for (dp = devsw, i = 0; i < ndevs; dp++, i++) { 101 if (dp->dv_name && !strcmp(devname, dp->dv_name)) 102 break; 103 } 104 105 if (i >= ndevs) 106 return (ENXIO); 107 108 *unit = u; 109 *part = p; 110 *dev = i; 111 fname = ++col; 112 } 113 114 if (*fname) 115 *file = fname; 116 117 return (0); 118 } 119 120 int 121 devopen(struct open_file *f, const char *fname, char **file) 122 { 123 struct devsw *dp; 124 u_int8_t unit, part; 125 int dev, error; 126 127 DPRINTF(("devopen(%s)\n", fname)); 128 129 if ( (error = devparse(fname, &dev, &unit, &part, 130 (const char **)file)) != 0) 131 return error; 132 133 dp = &devsw[dev]; 134 if ((void *)dp->dv_open == (void *)nodev) 135 return ENXIO; 136 137 f->f_dev = dp; 138 139 if ( (error = (*dp->dv_open)(f, unit, part)) != 0) 140 printf("%s%d%c: %d = %s\n", devsw[dev].dv_name, 141 unit, 'a' + part, error, strerror(error)); 142 143 return error; 144 } 145