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