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