2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3 * Copyright (c) 2006 Marcel Moolenaar
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
33 #include <sys/disklabel.h>
34 #include <sys/param.h>
35 #include <bootstrap.h>
43 #include "loader_efi.h"
45 static int efi_parsedev(struct devdesc **, const char *, const char **);
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.
53 efi_getdev(void **vdev, const char *devspec, const char **path)
55 struct devdesc **dev = (struct devdesc **)vdev;
59 * If it looks like this is just a path and no device, then
60 * use the current device instead.
62 if (devspec == NULL || *devspec == '/' || !strchr(devspec, ':')) {
63 rv = efi_parsedev(dev, getenv("currdev"), NULL);
64 if (rv == 0 && path != NULL)
69 /* Parse the device name off the beginning of the devspec. */
70 return (efi_parsedev(dev, devspec, path));
74 * Point (dev) at an allocated device specifier matching the string version
75 * at the beginning of (devspec). Return a pointer to the remaining
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.
82 * For disk-type devices, the syntax is:
87 efi_parsedev(struct devdesc **dev, const char *devspec, const char **path)
95 /* minimum length check */
96 if (strlen(devspec) < 2)
99 /* look for a device that matches */
100 for (i = 0; devsw[i] != NULL; i++) {
102 if (!strncmp(devspec, dv->dv_name, strlen(dv->dv_name)))
105 if (devsw[i] == NULL)
108 np = devspec + strlen(dv->dv_name);
111 if (dv->dv_type == DEVT_ZFS) {
114 idev = malloc(sizeof(struct zfs_devdesc));
118 err = zfs_parsedev((struct zfs_devdesc*)idev, np, path);
124 cp = strchr(np + 1, ':');
128 idev = malloc(sizeof(struct devdesc));
133 idev->d_type = dv->dv_type;
135 if (*np != '\0' && *np != ':') {
136 idev->d_unit = strtol(np, &cp, 0);
145 if (*cp != '\0' && *cp != ':') {
151 *path = (*cp == 0) ? cp : cp + 1;
160 efi_fmtdev(void *vdev)
162 struct devdesc *dev = (struct devdesc *)vdev;
163 static char buf[SPECNAMELEN + 1];
165 switch(dev->d_type) {
168 return (zfs_fmtdev(dev));
171 strcpy(buf, "(no device)");
175 sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_unit);
183 * Set currdev to suit the value being supplied in (value)
186 efi_setcurrdev(struct env_var *ev, int flags, const void *value)
188 struct devdesc *ncurr;
191 rv = efi_parsedev(&ncurr, value, NULL);
196 env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);