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: releng/10.3/sys/boot/efi/loader/devicename.c 294999 2016-01-28 17:24:40Z smh $"); 30 31#include <stand.h> 32#include <string.h> 33#include <sys/disklabel.h> 34#include <sys/param.h> 35#include <bootstrap.h> 36#ifdef EFI_ZFS_BOOT 37#include <libzfs.h> 38#endif 39 40#include <efi.h> 41#include <efilib.h> 42 43#include "loader_efi.h" 44 45static int efi_parsedev(struct devdesc **, const char *, const char **); 46 47/* 48 * Point (dev) at an allocated device specifier for the device matching the 49 * path in (devspec). If it contains an explicit device specification, 50 * use that. If not, use the default device. 51 */ 52int 53efi_getdev(void **vdev, const char *devspec, const char **path) 54{ 55 struct devdesc **dev = (struct devdesc **)vdev; 56 int rv; 57 58 /* 59 * If it looks like this is just a path and no device, then 60 * use the current device instead. 61 */ 62 if (devspec == NULL || *devspec == '/' || !strchr(devspec, ':')) { 63 rv = efi_parsedev(dev, getenv("currdev"), NULL); 64 if (rv == 0 && path != NULL) 65 *path = devspec; 66 return (rv); 67 } 68 69 /* Parse the device name off the beginning of the devspec. */ 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 * fs<unit>: 85 */ 86static int 87efi_parsedev(struct devdesc **dev, const char *devspec, const char **path) 88{ 89 struct devdesc *idev; 90 struct devsw *dv; 91 char *cp; 92 const char *np; 93 int i; 94 95 /* minimum length check */ 96 if (strlen(devspec) < 2) 97 return (EINVAL); 98 99 /* look for a device that matches */ 100 for (i = 0; devsw[i] != NULL; i++) { 101 dv = devsw[i]; 102 if (!strncmp(devspec, dv->dv_name, strlen(dv->dv_name))) 103 break; 104 } 105 if (devsw[i] == NULL) 106 return (ENOENT); 107 108 np = devspec + strlen(dv->dv_name); 109 110#ifdef EFI_ZFS_BOOT 111 if (dv->dv_type == DEVT_ZFS) { 112 int err; 113 114 idev = malloc(sizeof(struct zfs_devdesc)); 115 if (idev == NULL) 116 return (ENOMEM); 117 118 err = zfs_parsedev((struct zfs_devdesc*)idev, np, path); 119 if (err != 0) { 120 free(idev); 121 return (err); 122 } 123 *dev = idev; 124 cp = strchr(np + 1, ':'); 125 } else 126#endif 127 { 128 idev = malloc(sizeof(struct devdesc)); 129 if (idev == NULL) 130 return (ENOMEM); 131 132 idev->d_dev = dv; 133 idev->d_type = dv->dv_type; 134 idev->d_unit = -1; 135 if (*np != '\0' && *np != ':') { 136 idev->d_unit = strtol(np, &cp, 0); 137 if (cp == np) { 138 idev->d_unit = -1; 139 free(idev); 140 return (EUNIT); 141 } 142 } 143 } 144 145 if (*cp != '\0' && *cp != ':') { 146 free(idev); 147 return (EINVAL); 148 } 149 150 if (path != NULL) 151 *path = (*cp == 0) ? cp : cp + 1; 152 if (dev != NULL) 153 *dev = idev; 154 else 155 free(idev); 156 return (0); 157} 158 159char * 160efi_fmtdev(void *vdev) 161{ 162 struct devdesc *dev = (struct devdesc *)vdev; 163 static char buf[SPECNAMELEN + 1]; 164 165 switch(dev->d_type) { 166#ifdef EFI_ZFS_BOOT 167 case DEVT_ZFS: 168 return (zfs_fmtdev(dev)); 169#endif 170 case DEVT_NONE: 171 strcpy(buf, "(no device)"); 172 break; 173 174 default: 175 sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_unit); 176 break; 177 } 178 179 return (buf); 180} 181 182/* 183 * Set currdev to suit the value being supplied in (value) 184 */ 185int 186efi_setcurrdev(struct env_var *ev, int flags, const void *value) 187{ 188 struct devdesc *ncurr; 189 int rv; 190 191 rv = efi_parsedev(&ncurr, value, NULL); 192 if (rv != 0) 193 return (rv); 194 195 free(ncurr); 196 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 197 return (0); 198} 199