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