devicename.c revision 294981
191094Sdes/*- 2115619Sdes * Copyright (c) 1998 Michael Smith <msmith@freebsd.org> 3228690Sdes * Copyright (c) 2006 Marcel Moolenaar 491094Sdes * All rights reserved. 591094Sdes * 691094Sdes * Redistribution and use in source and binary forms, with or without 799158Sdes * modification, are permitted provided that the following conditions 899158Sdes * are met: 999158Sdes * 1. Redistributions of source code must retain the above copyright 1091094Sdes * notice, this list of conditions and the following disclaimer. 1191094Sdes * 2. Redistributions in binary form must reproduce the above copyright 1291094Sdes * notice, this list of conditions and the following disclaimer in the 1391094Sdes * documentation and/or other materials provided with the distribution. 1491094Sdes * 1591094Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1691094Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1791094Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1891094Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1991094Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2091094Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2191094Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2291094Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2391094Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2491094Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2591094Sdes * SUCH DAMAGE. 2691094Sdes */ 2791094Sdes 2891094Sdes#include <sys/cdefs.h> 2991094Sdes__FBSDID("$FreeBSD: stable/10/sys/boot/efi/loader/devicename.c 294981 2016-01-28 12:11:42Z smh $"); 3091094Sdes 3191094Sdes#include <stand.h> 3291094Sdes#include <string.h> 3391094Sdes#include <sys/disklabel.h> 3491094Sdes#include <bootstrap.h> 35255376Sdes 3691094Sdes#include <efi.h> 3791094Sdes#include <efilib.h> 38228690Sdes 39228690Sdes#include "loader_efi.h" 40228690Sdes 41228690Sdesstatic int efi_parsedev(struct devdesc **, const char *, const char **); 4291094Sdes 4391094Sdes/* 4491100Sdes * Point (dev) at an allocated device specifier for the device matching the 4591100Sdes * path in (devspec). If it contains an explicit device specification, 4691100Sdes * use that. If not, use the default device. 4791100Sdes */ 4891100Sdesint 4991100Sdesefi_getdev(void **vdev, const char *devspec, const char **path) 5091100Sdes{ 5191094Sdes struct devdesc **dev = (struct devdesc **)vdev; 5291094Sdes int rv; 5391094Sdes 5491094Sdes /* 5591094Sdes * If it looks like this is just a path and no device, then 5691094Sdes * use the current device instead. 5791094Sdes */ 5891094Sdes if (devspec == NULL || *devspec == '/' || !strchr(devspec, ':')) { 5991094Sdes rv = efi_parsedev(dev, getenv("currdev"), NULL); 6091094Sdes if (rv == 0 && path != NULL) 61107937Sdes *path = devspec; 62107937Sdes return (rv); 6391094Sdes } 6491100Sdes 6591100Sdes /* Parse the device name off the beginning of the devspec. */ 6691100Sdes return (efi_parsedev(dev, devspec, path)); 6791100Sdes} 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 */ 82static int 83efi_parsedev(struct devdesc **dev, const char *devspec, const char **path) 84{ 85 struct devdesc *idev; 86 struct devsw *dv; 87 char *cp; 88 const char *np; 89 int i, err; 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))) 99 break; 100 } 101 if (devsw[i] == NULL) 102 return (ENOENT); 103 104 idev = malloc(sizeof(struct devdesc)); 105 if (idev == NULL) 106 return (ENOMEM); 107 108 idev->d_dev = dv; 109 idev->d_type = dv->dv_type; 110 idev->d_unit = -1; 111 112 err = 0; 113 np = devspec + strlen(dv->dv_name); 114 if (*np != '\0' && *np != ':') { 115 idev->d_unit = strtol(np, &cp, 0); 116 if (cp == np) { 117 idev->d_unit = -1; 118 free(idev); 119 return (EUNIT); 120 } 121 } 122 if (*cp != '\0' && *cp != ':') { 123 free(idev); 124 return (EINVAL); 125 } 126 127 if (path != NULL) 128 *path = (*cp == 0) ? cp : cp + 1; 129 if (dev != NULL) 130 *dev = idev; 131 else 132 free(idev); 133 return (0); 134} 135 136char * 137efi_fmtdev(void *vdev) 138{ 139 struct devdesc *dev = (struct devdesc *)vdev; 140 static char buf[32]; /* XXX device length constant? */ 141 142 switch(dev->d_type) { 143 case DEVT_NONE: 144 strcpy(buf, "(no device)"); 145 break; 146 147 default: 148 sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_unit); 149 break; 150 } 151 152 return (buf); 153} 154 155/* 156 * Set currdev to suit the value being supplied in (value) 157 */ 158int 159efi_setcurrdev(struct env_var *ev, int flags, const void *value) 160{ 161 struct devdesc *ncurr; 162 int rv; 163 164 rv = efi_parsedev(&ncurr, value, NULL); 165 if (rv != 0) 166 return (rv); 167 168 free(ncurr); 169 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL); 170 return (0); 171} 172