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 __FBSDID("$FreeBSD$"); 30 31 #include <stand.h> 32 #include <string.h> 33 #include <sys/disklabel.h> 34 #include <sys/param.h> 35 #include <bootstrap.h> 36 #include <disk.h> 37 38 #include <efi.h> 39 #include <efilib.h> 40 #include <efizfs.h> 41 42 static int efi_parsedev(struct devdesc **, const char *, const char **); 43 44 /* 45 * Point (dev) at an allocated device specifier for the device matching the 46 * path in (devspec). If it contains an explicit device specification, 47 * use that. If not, use the default device. 48 */ 49 int 50 efi_getdev(void **vdev, const char *devspec, const char **path) 51 { 52 struct devdesc **dev = (struct devdesc **)vdev; 53 int rv; 54 55 /* 56 * If it looks like this is just a path and no device, then 57 * use the current device instead. 58 */ 59 if (devspec == NULL || *devspec == '/' || !strchr(devspec, ':')) { 60 rv = efi_parsedev(dev, getenv("currdev"), NULL); 61 if (rv == 0 && path != NULL) 62 *path = devspec; 63 return (rv); 64 } 65 66 /* Parse the device name off the beginning of the devspec. */ 67 return (efi_parsedev(dev, devspec, path)); 68 } 69 70 /* 71 * Point (dev) at an allocated device specifier matching the string version 72 * at the beginning of (devspec). Return a pointer to the remaining 73 * text in (path). 74 * 75 * In all cases, the beginning of (devspec) is compared to the names 76 * of known devices in the device switch, and then any following text 77 * is parsed according to the rules applied to the device type. 78 * 79 * For disk-type devices, the syntax is: 80 * 81 * fs<unit>: 82 */ 83 static int 84 efi_parsedev(struct devdesc **dev, const char *devspec, const char **path) 85 { 86 struct devdesc *idev; 87 struct devsw *dv; 88 int i, unit, err; 89 char *cp; 90 const char *np; 91 92 /* minimum length check */ 93 if (strlen(devspec) < 2) 94 return (EINVAL); 95 96 /* look for a device that matches */ 97 for (i = 0; devsw[i] != NULL; i++) { 98 dv = devsw[i]; 99 if (!strncmp(devspec, dv->dv_name, strlen(dv->dv_name))) 100 break; 101 } 102 if (devsw[i] == NULL) 103 return (ENOENT); 104 105 np = devspec + strlen(dv->dv_name); 106 idev = NULL; 107 err = 0; 108 109 switch (dv->dv_type) { 110 case DEVT_NONE: 111 break; 112 113 case DEVT_DISK: 114 idev = malloc(sizeof(struct disk_devdesc)); 115 if (idev == NULL) 116 return (ENOMEM); 117 118 err = disk_parsedev((struct disk_devdesc *)idev, np, path); 119 if (err != 0) 120 goto fail; 121 break; 122 123 #ifdef EFI_ZFS_BOOT 124 case DEVT_ZFS: 125 idev = malloc(sizeof(struct zfs_devdesc)); 126 if (idev == NULL) 127 return (ENOMEM); 128 129 err = zfs_parsedev((struct zfs_devdesc*)idev, np, path); 130 if (err != 0) 131 goto fail; 132 break; 133 #endif 134 default: 135 idev = malloc(sizeof(struct devdesc)); 136 if (idev == NULL) 137 return (ENOMEM); 138 139 unit = 0; 140 cp = (char *)np; 141 142 if (*np != '\0' && *np != ':') { 143 errno = 0; 144 unit = strtol(np, &cp, 0); 145 if (errno != 0 || cp == np) { 146 err = EUNIT; 147 goto fail; 148 } 149 } 150 if (*cp != '\0' && *cp != ':') { 151 err = EINVAL; 152 goto fail; 153 } 154 155 idev->d_unit = unit; 156 if (path != NULL) 157 *path = (*cp == 0) ? cp : cp + 1; 158 break; 159 } 160 161 idev->d_dev = dv; 162 163 if (dev != NULL) 164 *dev = idev; 165 else 166 free(idev); 167 return (0); 168 169 fail: 170 free(idev); 171 return (err); 172 } 173 174 char * 175 efi_fmtdev(void *vdev) 176 { 177 struct devdesc *dev = (struct devdesc *)vdev; 178 static char buf[SPECNAMELEN + 1]; 179 180 switch(dev->d_dev->dv_type) { 181 case DEVT_NONE: 182 strcpy(buf, "(no device)"); 183 break; 184 185 case DEVT_DISK: 186 return (disk_fmtdev(vdev)); 187 188 #ifdef EFI_ZFS_BOOT 189 case DEVT_ZFS: 190 return (zfs_fmtdev(dev)); 191 #endif 192 default: 193 sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_unit); 194 break; 195 } 196 197 return (buf); 198 } 199 200 /* 201 * Set currdev to suit the value being supplied in (value) 202 */ 203 int 204 efi_setcurrdev(struct env_var *ev, int flags, const void *value) 205 { 206 struct devdesc *ncurr; 207 int rv; 208 209 rv = efi_parsedev(&ncurr, value, NULL); 210 if (rv != 0) 211 return (rv); 212 free(ncurr); 213 214 return (mount_currdev(ev, flags, value)); 215 } 216