1 /* 2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3 * Copyright (c) 2006 Marcel Moolenaar 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 30 #include <stand.h> 31 #include <string.h> 32 #include <sys/disklabel.h> 33 #include <sys/param.h> 34 #include <bootstrap.h> 35 #include <disk.h> 36 #include <libzfs.h> 37 38 #include <efi.h> 39 #include <efilib.h> 40 41 static int efi_parsedev(struct devdesc **, const char *, const char **); 42 43 /* 44 * Point (dev) at an allocated device specifier for the device matching the 45 * path in (devspec). If it contains an explicit device specification, 46 * use that. If not, use the default device. 47 */ 48 int 49 efi_getdev(void **vdev, const char *devspec, const char **path) 50 { 51 struct devdesc **dev = (struct devdesc **)vdev; 52 int rv; 53 54 /* 55 * If it looks like this is just a path and no device, then 56 * use the current device instead. 57 */ 58 if (devspec == NULL || *devspec == '/' || !strchr(devspec, ':')) { 59 rv = efi_parsedev(dev, getenv("currdev"), NULL); 60 if (rv == 0 && path != NULL) 61 *path = devspec; 62 return (rv); 63 } 64 65 /* Parse the device name off the beginning of the devspec. */ 66 return (efi_parsedev(dev, devspec, path)); 67 } 68 69 /* 70 * Point (dev) at an allocated device specifier matching the string version 71 * at the beginning of (devspec). Return a pointer to the remaining 72 * text in (path). 73 * 74 * In all cases, the beginning of (devspec) is compared to the names 75 * of known devices in the device switch, and then any following text 76 * is parsed according to the rules applied to the device type. 77 * 78 * For disk-type devices, the syntax is: 79 * 80 * fs<unit>: 81 */ 82 static int 83 efi_parsedev(struct devdesc **dev, const char *devspec, const char **path) 84 { 85 struct devdesc *idev; 86 struct devsw *dv; 87 int i, unit, err; 88 char *cp; 89 const char *np; 90 91 /* minimum length check */ 92 if (strlen(devspec) < 2) 93 return (EINVAL); 94 95 /* look for a device that matches */ 96 for (i = 0; devsw[i] != NULL; i++) { 97 dv = devsw[i]; 98 if (strncmp(devspec, dv->dv_name, strlen(dv->dv_name)) == 0) 99 break; 100 } 101 if (devsw[i] == NULL) 102 return (ENOENT); 103 104 np = devspec + strlen(dv->dv_name); 105 idev = NULL; 106 err = 0; 107 108 switch (dv->dv_type) { 109 case DEVT_NONE: 110 break; 111 112 case DEVT_DISK: 113 idev = malloc(sizeof (struct disk_devdesc)); 114 if (idev == NULL) 115 return (ENOMEM); 116 117 err = disk_parsedev((struct disk_devdesc *)idev, np, path); 118 if (err != 0) 119 goto fail; 120 break; 121 122 case DEVT_ZFS: 123 idev = malloc(sizeof (struct zfs_devdesc)); 124 if (idev == NULL) 125 return (ENOMEM); 126 127 err = zfs_parsedev((struct zfs_devdesc *)idev, np, path); 128 if (err != 0) 129 goto fail; 130 break; 131 132 default: 133 idev = malloc(sizeof (struct devdesc)); 134 if (idev == NULL) 135 return (ENOMEM); 136 137 unit = 0; 138 cp = (char *)np; 139 140 if (*np != '\0' && *np != ':') { 141 /* get unit number if present */ 142 errno = 0; 143 unit = strtol(np, &cp, 0); 144 if (errno != 0 || cp == np) { 145 err = EUNIT; 146 goto fail; 147 } 148 } 149 if (*cp != '\0' && *cp != ':') { 150 err = EINVAL; 151 goto fail; 152 } 153 154 idev->d_unit = unit; 155 if (path != NULL) 156 *path = (*cp == '\0') ? cp : cp + 1; 157 break; 158 } 159 160 idev->d_dev = dv; 161 162 if (dev != NULL) 163 *dev = idev; 164 else 165 free(idev); 166 return (0); 167 168 fail: 169 free(idev); 170 return (err); 171 } 172 173 char * 174 efi_fmtdev(void *vdev) 175 { 176 struct devdesc *dev = (struct devdesc *)vdev; 177 static char buf[SPECNAMELEN + 1]; 178 179 switch (dev->d_dev->dv_type) { 180 case DEVT_NONE: 181 strlcpy(buf, "(no device)", sizeof (buf)); 182 break; 183 184 case DEVT_DISK: 185 return (disk_fmtdev(vdev)); 186 187 case DEVT_ZFS: 188 return (zfs_fmtdev(dev)); 189 190 default: 191 snprintf(buf, sizeof (buf), "%s%d:", dev->d_dev->dv_name, 192 dev->d_unit); 193 break; 194 } 195 196 return (buf); 197 } 198 199 /* 200 * Set currdev to suit the value being supplied in (value) 201 */ 202 int 203 efi_setcurrdev(struct env_var *ev, int flags, const void *value) 204 { 205 struct devdesc *ncurr; 206 int rv; 207 208 rv = efi_parsedev(&ncurr, value, NULL); 209 if (rv != 0) 210 return (rv); 211 212 free(ncurr); 213 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 214 return (0); 215 } 216