1 /* $NetBSD: devicename.c,v 1.3 2009/07/20 04:59:04 kiyohara Exp $ */ 2 3 /*- 4 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 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 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 /* __FBSDID("$FreeBSD: src/sys/boot/ia64/libski/devicename.c,v 1.2 2003/09/08 09:11:32 obrien Exp $"); */ 31 32 #include <lib/libsa/stand.h> 33 #include <lib/libsa/loadfile.h> 34 #include <sys/disklabel.h> 35 36 #include <bootstrap.h> 37 #include "libski.h" 38 39 static int ski_parsedev(struct ski_devdesc **dev, const char *devspec, const char **path); 40 41 /* 42 * Point (dev) at an allocated device specifier for the device matching the 43 * path in (devspec). If it contains an explicit device specification, 44 * use that. If not, use the default device. 45 */ 46 int 47 ski_getdev(void **vdev, const char *devspec, const char **path) 48 { 49 struct ski_devdesc **dev = (struct ski_devdesc **)vdev; 50 int rv; 51 52 /* 53 * If it looks like this is just a path and no 54 * device, go with the current device. 55 */ 56 if ((devspec == NULL) || 57 (devspec[0] == '/') || 58 (strchr(devspec, ':') == NULL)) { 59 60 if (((rv = ski_parsedev(dev, getenv("currdev"), NULL)) == 0) && 61 (path != NULL)) 62 *path = devspec; 63 return(rv); 64 } 65 66 /* 67 * Try to parse the device name off the beginning of the devspec 68 */ 69 return(ski_parsedev(dev, devspec, path)); 70 } 71 72 /* 73 * Point (dev) at an allocated device specifier matching the string version 74 * at the beginning of (devspec). Return a pointer to the remaining 75 * text in (path). 76 * 77 * In all cases, the beginning of (devspec) is compared to the names 78 * of known devices in the device switch, and then any following text 79 * is parsed according to the rules applied to the device type. 80 * 81 * For disk-type devices, the syntax is: 82 * 83 * disk<unit>[s<slice>][<partition>]: 84 * 85 */ 86 static int 87 ski_parsedev(struct ski_devdesc **dev, const char *devspec, const char **path) 88 { 89 struct ski_devdesc *idev; 90 struct devsw *dv; 91 int dv_type; 92 int i, unit, slice, partition, err; 93 char *cp; 94 const char *np; 95 96 /* minimum length check */ 97 if (strlen(devspec) < 2) 98 return(EINVAL); 99 100 /* look for a device that matches */ 101 for (i = 0, dv = NULL; i < ndevs; i++) { 102 if (!strncmp(devspec, devsw[i].dv_name, strlen(devsw[i].dv_name))) { 103 dv = &devsw[i]; 104 break; 105 } 106 } 107 108 if (dv == NULL) 109 return(ENOENT); 110 idev = alloc(sizeof(struct ski_devdesc)); 111 err = 0; 112 np = (devspec + strlen(dv->dv_name)); 113 114 /* XXX: Fixme */ 115 dv_type = DEVT_DISK; 116 117 switch(dv_type) { 118 case DEVT_NONE: /* XXX what to do here? Do we care? */ 119 break; 120 121 case DEVT_DISK: /* XXX: Need to fix DEVT_DISK for NetBSD disk conventions. */ 122 unit = -1; 123 slice = -1; 124 partition = -1; 125 if (*np && (*np != ':')) { 126 unit = strtol(np, &cp, 10); /* next comes the unit number */ 127 if (cp == np) { 128 err = EUNIT; 129 goto fail; 130 } 131 if (*cp == 's') { /* got a slice number */ 132 np = cp + 1; 133 slice = strtol(np, &cp, 10); 134 if (cp == np) { 135 err = EPART; 136 goto fail; 137 } 138 } 139 if (*cp && (*cp != ':')) { 140 partition = *cp - 'a'; /* get a partition number */ 141 if ((partition < 0) || (partition >= MAXPARTITIONS)) { 142 err = EPART; 143 goto fail; 144 } 145 cp++; 146 } 147 } 148 if (*cp && (*cp != ':')) { 149 err = EINVAL; 150 goto fail; 151 } 152 153 idev->d_kind.skidisk.unit = unit; 154 idev->d_kind.skidisk.slice = slice; 155 idev->d_kind.skidisk.partition = partition; 156 157 if (path != NULL) 158 *path = (*cp == 0) ? cp : cp + 1; 159 break; 160 161 case DEVT_NET: 162 unit = 0; 163 164 if (*np && (*np != ':')) { 165 unit = strtol(np, &cp, 0); /* get unit number if present */ 166 if (cp == np) { 167 err = EUNIT; 168 goto fail; 169 } 170 } 171 if (*cp && (*cp != ':')) { 172 err = EINVAL; 173 goto fail; 174 } 175 176 idev->d_kind.netif.unit = unit; 177 if (path != NULL) 178 *path = (*cp == 0) ? cp : cp + 1; 179 break; 180 181 default: 182 err = EINVAL; 183 goto fail; 184 } 185 idev->d_dev = dv; 186 idev->d_type = dv_type; 187 if (dev == NULL) { 188 free(idev); 189 } else { 190 *dev = idev; 191 } 192 return(0); 193 194 fail: 195 free(idev); 196 return(err); 197 } 198 199 200 char * 201 ski_fmtdev(void *vdev) 202 { 203 struct ski_devdesc *dev = (struct ski_devdesc *)vdev; 204 static char buf[128]; /* XXX device length constant? */ 205 char *cp; 206 207 switch(dev->d_type) { 208 case DEVT_NONE: 209 strcpy(buf, "(no device)"); 210 break; 211 212 case DEVT_DISK: 213 cp = buf; 214 cp += sprintf(cp, "%s%d", dev->d_dev->dv_name, dev->d_kind.skidisk.unit); 215 if (dev->d_kind.skidisk.slice > 0) 216 cp += sprintf(cp, "s%d", dev->d_kind.skidisk.slice); 217 if (dev->d_kind.skidisk.partition >= 0) 218 cp += sprintf(cp, "%c", dev->d_kind.skidisk.partition + 'a'); 219 strcat(cp, ":"); 220 break; 221 222 case DEVT_NET: 223 sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_kind.netif.unit); 224 break; 225 } 226 return(buf); 227 } 228 229 230 /* 231 * Set currdev to suit the value being supplied in (value) 232 */ 233 int 234 ski_setcurrdev(struct env_var *ev, int flags, void *value) 235 { 236 struct ski_devdesc *ncurr; 237 int rv; 238 239 if ((rv = ski_parsedev(&ncurr, value, NULL)) != 0) 240 return(rv); 241 free(ncurr); 242 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 243 return(0); 244 } 245 246