devicename.c revision 96423
1/*- 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD: head/sys/boot/ofw/libofw/devicename.c 96423 2002-05-11 21:30:46Z jake $ 27 */ 28 29#include <stand.h> 30#include <sys/disklabel.h> 31#include "libofw.h" 32 33static int ofw_parsedev(struct ofw_devdesc **, const char *, const char **); 34 35/* 36 * Point (dev) at an allocated device specifier for the device matching the 37 * path in (devspec). If it contains an explicit device specification, 38 * use that. If not, use the default device. 39 */ 40int 41ofw_getdev(void **vdev, const char *devspec, const char **path) 42{ 43 struct ofw_devdesc **dev = (struct ofw_devdesc **)vdev; 44 int rv; 45 46 /* 47 * If it looks like this is just a path and no 48 * device, go with the current device. 49 */ 50 if ((devspec == NULL) || 51 (devspec[0] == '/') || 52 (strchr(devspec, ':') == NULL)) { 53 54 if (((rv = ofw_parsedev(dev, getenv("currdev"), NULL)) == 0) && 55 (path != NULL)) 56 *path = devspec; 57 return(rv); 58 } 59 60 /* 61 * Try to parse the device name off the beginning of the devspec 62 */ 63 return(ofw_parsedev(dev, devspec, path)); 64} 65 66/* 67 * Point (dev) at an allocated device specifier matching the string version 68 * at the beginning of (devspec). Return a pointer to the remaining 69 * text in (path). 70 * 71 * In all cases, the beginning of (devspec) is compared to the names 72 * of known devices in the device switch, and then any following text 73 * is parsed according to the rules applied to the device type. 74 * 75 * For disk-type devices, the syntax is: 76 * 77 * disk<unit>[s<slice>][<partition>]: 78 * 79 */ 80static int 81ofw_parsedev(struct ofw_devdesc **dev, const char *devspec, const char **path) 82{ 83 struct ofw_devdesc *idev; 84 struct devsw *dv; 85 int i, unit, slice, partition, err; 86 char *cp; 87 const char *np; 88 89 /* minimum length check */ 90 if (strlen(devspec) < 2) 91 return(EINVAL); 92 93 /* look for a device that matches */ 94 for (i = 0, dv = NULL; devsw[i] != NULL; i++) { 95 if (!strncmp(devspec, devsw[i]->dv_name, strlen(devsw[i]->dv_name))) { 96 dv = devsw[i]; 97 break; 98 } 99 } 100 101 if (dv == NULL) 102 return(ENOENT); 103 idev = malloc(sizeof(struct ofw_devdesc)); 104 err = 0; 105 np = (devspec + strlen(dv->dv_name)); 106 107 switch(dv->dv_type) { 108 case DEVT_NONE: /* XXX what to do here? Do we care? */ 109 break; 110 111 case DEVT_DISK: 112 unit = -1; 113 slice = -1; 114 partition = -1; 115 if (*np && (*np != ':')) { 116 unit = strtol(np, &cp, 10); /* next comes the unit number */ 117 if (cp == np) { 118 err = EUNIT; 119 goto fail; 120 } 121 if (*cp == 's') { /* got a slice number */ 122 np = cp + 1; 123 slice = strtol(np, &cp, 10); 124 if (cp == np) { 125 err = ESLICE; 126 goto fail; 127 } 128 } 129 if (*cp && (*cp != ':')) { 130 partition = *cp - 'a'; /* get a partition number */ 131 if ((partition < 0) || (partition >= MAXPARTITIONS)) { 132 err = EPART; 133 goto fail; 134 } 135 cp++; 136 } 137 } 138 if (*cp && (*cp != ':')) { 139 err = EINVAL; 140 goto fail; 141 } 142 143 idev->d_kind.ofwdisk.unit = unit; 144 idev->d_kind.ofwdisk.slice = slice; 145 idev->d_kind.ofwdisk.partition = partition; 146 if (path != NULL) 147 *path = (*cp == 0) ? cp : cp + 1; 148 break; 149 150 case DEVT_NET: 151 unit = 0; 152 153 if (*np && (*np != ':')) { 154 unit = strtol(np, &cp, 0); /* get unit number if present */ 155 if (cp == np) { 156 err = EUNIT; 157 goto fail; 158 } 159 } 160 if (*cp && (*cp != ':')) { 161 err = EINVAL; 162 goto fail; 163 } 164 165 idev->d_kind.netif.unit = unit; 166 if (path != NULL) 167 *path = (*cp == 0) ? cp : cp + 1; 168 break; 169 170 default: 171 err = EINVAL; 172 goto fail; 173 } 174 idev->d_dev = dv; 175 idev->d_type = dv->dv_type; 176 if (dev == NULL) { 177 free(idev); 178 } else { 179 *dev = idev; 180 } 181 return(0); 182 183 fail: 184 free(idev); 185 return(err); 186} 187 188/* 189 * Hack to correctly parse bootpath for currdev. Also, enter the device into 190 * the device array in the case of lazy probing (i.e. on sparc64). This has the 191 * effect that the disk the loader was loaded from will always be the first 192 * in the list, but it saves lots of time. 193 */ 194int 195ofw_parseofwdev(struct ofw_devdesc *dev, const char *devspec) 196{ 197 char *cp, *ep; 198 int i; 199 struct devsw *dv; 200 201#ifdef __sparc64__ 202 ofwd_enter_dev(devspec); 203#endif 204 if ((dev->d_kind.ofwdisk.unit = ofwd_getunit(devspec)) == -1) 205 return EUNIT; 206 if ((cp = strrchr(devspec, ',')) == 0) 207 return EINVAL; 208 cp++; 209 if (*cp != ',') 210 return ESLICE; 211 ep = ++cp; 212 dev->d_kind.ofwdisk.slice = strtol(cp, &cp, 10) + 1; 213 if (cp == ep) 214 return ESLICE; 215 if (*cp != ':') 216 return EPART; 217 dev->d_kind.ofwdisk.partition = *++cp - 'a'; 218} 219 220char * 221ofw_fmtdev(void *vdev) 222{ 223 struct ofw_devdesc *dev = (struct ofw_devdesc *)vdev; 224 static char buf[128]; 225 char *cp; 226 227 switch(dev->d_type) { 228 case DEVT_NONE: 229 strcpy(buf, "(no device)"); 230 break; 231 232 case DEVT_DISK: 233 sprintf(buf, "%s%ds%d%c:", dev->d_dev->dv_name, 234 dev->d_kind.ofwdisk.unit, dev->d_kind.ofwdisk.slice, 235 dev->d_kind.ofwdisk.partition + 'a'); 236 break; 237 238 case DEVT_NET: 239 sprintf(buf, "%s%d:", dev->d_dev->dv_name, 240 dev->d_kind.netif.unit); 241 break; 242 } 243 244 return buf; 245} 246 247int 248ofw_setcurrdev(struct env_var *ev, int flags, void *value) 249{ 250 struct ofw_devdesc *ncurr; 251 int rv; 252 253 if ((rv = ofw_parsedev(&ncurr, value, NULL)) != 0) 254 return rv; 255 256 free(ncurr); 257 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 258 return 0; 259} 260