1 /* $NetBSD: devicename.c,v 1.4 2009/07/20 04:59:03 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 31 /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/devicename.c,v 1.3 2004/01/04 23:28:16 obrien Exp $"); */ 32 33 #include <lib/libsa/stand.h> 34 #include <lib/libsa/loadfile.h> 35 #include <sys/disklabel.h> 36 37 #include <bootstrap.h> 38 39 #include <efi.h> 40 #include <efilib.h> 41 #include "efiboot.h" 42 43 static int efi_parsedev(struct efi_devdesc **dev, const char *devspec, const char **path); 44 45 /* 46 * Point (dev) at an allocated device specifier for the device matching the 47 * path in (devspec). If it contains an explicit device specification, 48 * use that. If not, use the default device. 49 */ 50 int 51 efi_getdev(void **vdev, const char *devspec, const char **path) 52 { 53 struct efi_devdesc **dev = (struct efi_devdesc **)vdev; 54 int rv; 55 56 /* 57 * If it looks like this is just a path and no 58 * device, go with the current device. 59 */ 60 if ((devspec == NULL) || 61 (devspec[0] == '/') || 62 (strchr(devspec, ':') == NULL)) { 63 64 if (((rv = efi_parsedev(dev, getenv("currdev"), NULL)) == 0) && 65 (path != NULL)) 66 *path = devspec; 67 return(rv); 68 } 69 70 /* 71 * Try to parse the device name off the beginning of the devspec 72 */ 73 return(efi_parsedev(dev, devspec, path)); 74 } 75 76 /* 77 * Point (dev) at an allocated device specifier matching the string version 78 * at the beginning of (devspec). Return a pointer to the remaining 79 * text in (path). 80 * 81 * In all cases, the beginning of (devspec) is compared to the names 82 * of known devices in the device switch, and then any following text 83 * is parsed according to the rules applied to the device type. 84 * 85 * For disk-type devices, the syntax is: 86 * 87 * disk<unit>[s<slice>][<partition>]: 88 * 89 */ 90 static int 91 efi_parsedev(struct efi_devdesc **dev, const char *devspec, const char **path) 92 { 93 struct efi_devdesc *idev; 94 struct devsw *dv; 95 int dv_type; 96 int i, unit, slice, partition, err; 97 char *cp; 98 const char *np; 99 100 /* minimum length check */ 101 if (strlen(devspec) < 2) 102 return(EINVAL); 103 104 /* look for a device that matches */ 105 for (i = 0, dv = NULL; i < ndevs; i++) { 106 if (!strncmp(devspec, devsw[i].dv_name, strlen(devsw[i].dv_name))) { 107 dv = &devsw[i]; 108 break; 109 } 110 } 111 112 if (dv == NULL) 113 return(ENOENT); 114 idev = alloc(sizeof(struct efi_devdesc)); 115 err = 0; 116 np = (devspec + strlen(dv->dv_name)); 117 118 dv_type = DEVT_NONE; 119 if (!strncmp("disk", dv->dv_name, 4)) dv_type = DEVT_DISK; 120 if (!strncmp("net", dv->dv_name, 3)) dv_type = DEVT_DISK; 121 122 switch(dv_type) { 123 case DEVT_NONE: /* XXX what to do here? Do we care? */ 124 break; 125 126 case DEVT_DISK: 127 unit = -1; 128 slice = -1; 129 partition = -1; 130 if (*np && (*np != ':')) { 131 unit = strtol(np, &cp, 10); /* next comes the unit number */ 132 if (cp == np) { 133 err = EUNIT; 134 goto fail; 135 } 136 if (*cp == 's') { /* got a slice number */ 137 np = cp + 1; 138 slice = strtol(np, &cp, 10); 139 if (cp == np) { 140 err = EPART; /* XXX : NetBSD calls a FreeBSD SLICE, a Partition! */ 141 goto fail; 142 } 143 } 144 if (*cp && (*cp != ':')) { 145 partition = *cp - 'a'; /* get a partition number */ 146 if ((partition < 0) || (partition >= MAXPARTITIONS)) { 147 err = EPART; 148 goto fail; 149 } 150 cp++; 151 } 152 } 153 if (*cp && (*cp != ':')) { 154 err = EINVAL; 155 goto fail; 156 } 157 158 idev->d_kind.efidisk.unit = unit; 159 idev->d_kind.efidisk.slice = slice; 160 idev->d_kind.efidisk.partition = partition; 161 162 if (path != NULL) 163 *path = (*cp == 0) ? cp : cp + 1; 164 break; 165 166 case DEVT_NET: 167 unit = 0; 168 169 if (*np && (*np != ':')) { 170 unit = strtol(np, &cp, 0); /* get unit number if present */ 171 if (cp == np) { 172 err = EUNIT; 173 goto fail; 174 } 175 } 176 if (*cp && (*cp != ':')) { 177 err = EINVAL; 178 goto fail; 179 } 180 181 idev->d_kind.netif.unit = unit; 182 if (path != NULL) 183 *path = (*cp == 0) ? cp : cp + 1; 184 break; 185 186 default: 187 err = EINVAL; 188 goto fail; 189 } 190 idev->d_dev = dv; 191 idev->d_type = dv_type; 192 if (dev == NULL) { 193 free(idev); 194 } else { 195 *dev = idev; 196 } 197 return(0); 198 199 fail: 200 free(idev); 201 return(err); 202 } 203 204 205 char * 206 efi_fmtdev(void *vdev) 207 { 208 struct efi_devdesc *dev = (struct efi_devdesc *)vdev; 209 static char buf[128]; /* XXX device length constant? */ 210 char *cp; 211 212 switch(dev->d_type) { 213 case DEVT_NONE: 214 strcpy(buf, "(no device)"); 215 break; 216 217 case DEVT_DISK: 218 cp = buf; 219 cp += sprintf(cp, "%s%d", dev->d_dev->dv_name, dev->d_kind.efidisk.unit); 220 if (dev->d_kind.efidisk.slice > 0) 221 cp += sprintf(cp, "s%d", dev->d_kind.efidisk.slice); 222 if (dev->d_kind.efidisk.partition >= 0) 223 cp += sprintf(cp, "%c", dev->d_kind.efidisk.partition + 'a'); 224 strcat(cp, ":"); 225 break; 226 227 case DEVT_NET: 228 sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_kind.netif.unit); 229 break; 230 } 231 return(buf); 232 } 233 234 235 /* 236 * Set currdev to suit the value being supplied in (value) 237 */ 238 int 239 efi_setcurrdev(struct env_var *ev, int flags, void *value) 240 { 241 struct efi_devdesc *ncurr; 242 int rv; 243 244 if ((rv = efi_parsedev(&ncurr, value, NULL)) != 0) 245 return(rv); 246 free(ncurr); 247 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 248 return(0); 249 } 250 251