1422eb17fSToomas Soome /*
2199767f8SToomas Soome  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3199767f8SToomas Soome  * Copyright (c) 2006 Marcel Moolenaar
4199767f8SToomas Soome  * All rights reserved.
5199767f8SToomas Soome  *
6199767f8SToomas Soome  * Redistribution and use in source and binary forms, with or without
7199767f8SToomas Soome  * modification, are permitted provided that the following conditions
8199767f8SToomas Soome  * are met:
9199767f8SToomas Soome  * 1. Redistributions of source code must retain the above copyright
10199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer.
11199767f8SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
12199767f8SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
13199767f8SToomas Soome  *    documentation and/or other materials provided with the distribution.
14199767f8SToomas Soome  *
15199767f8SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16199767f8SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17199767f8SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18199767f8SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19199767f8SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20199767f8SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21199767f8SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22199767f8SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23199767f8SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24199767f8SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25199767f8SToomas Soome  * SUCH DAMAGE.
26199767f8SToomas Soome  */
27199767f8SToomas Soome 
28199767f8SToomas Soome #include <sys/cdefs.h>
29199767f8SToomas Soome 
30199767f8SToomas Soome #include <stand.h>
31199767f8SToomas Soome #include <string.h>
32199767f8SToomas Soome #include <sys/disklabel.h>
33199767f8SToomas Soome #include <sys/param.h>
34199767f8SToomas Soome #include <bootstrap.h>
35dbacaf56SToomas Soome #include <disk.h>
36199767f8SToomas Soome #include <libzfs.h>
37199767f8SToomas Soome 
38199767f8SToomas Soome #include <efi.h>
39199767f8SToomas Soome #include <efilib.h>
40199767f8SToomas Soome 
41199767f8SToomas Soome static int efi_parsedev(struct devdesc **, const char *, const char **);
42199767f8SToomas Soome 
43199767f8SToomas Soome /*
44199767f8SToomas Soome  * Point (dev) at an allocated device specifier for the device matching the
45199767f8SToomas Soome  * path in (devspec). If it contains an explicit device specification,
46199767f8SToomas Soome  * use that.  If not, use the default device.
47199767f8SToomas Soome  */
48199767f8SToomas Soome int
efi_getdev(void ** vdev,const char * devspec,const char ** path)49199767f8SToomas Soome efi_getdev(void **vdev, const char *devspec, const char **path)
50199767f8SToomas Soome {
51199767f8SToomas Soome 	struct devdesc **dev = (struct devdesc **)vdev;
52199767f8SToomas Soome 	int rv;
53199767f8SToomas Soome 
54199767f8SToomas Soome 	/*
55199767f8SToomas Soome 	 * If it looks like this is just a path and no device, then
56199767f8SToomas Soome 	 * use the current device instead.
57199767f8SToomas Soome 	 */
58199767f8SToomas Soome 	if (devspec == NULL || *devspec == '/' || !strchr(devspec, ':')) {
59199767f8SToomas Soome 		rv = efi_parsedev(dev, getenv("currdev"), NULL);
60199767f8SToomas Soome 		if (rv == 0 && path != NULL)
61199767f8SToomas Soome 			*path = devspec;
62199767f8SToomas Soome 		return (rv);
63199767f8SToomas Soome 	}
64199767f8SToomas Soome 
65199767f8SToomas Soome 	/* Parse the device name off the beginning of the devspec. */
66199767f8SToomas Soome 	return (efi_parsedev(dev, devspec, path));
67199767f8SToomas Soome }
68199767f8SToomas Soome 
69199767f8SToomas Soome /*
70199767f8SToomas Soome  * Point (dev) at an allocated device specifier matching the string version
71199767f8SToomas Soome  * at the beginning of (devspec).  Return a pointer to the remaining
72199767f8SToomas Soome  * text in (path).
73199767f8SToomas Soome  *
74199767f8SToomas Soome  * In all cases, the beginning of (devspec) is compared to the names
75199767f8SToomas Soome  * of known devices in the device switch, and then any following text
76199767f8SToomas Soome  * is parsed according to the rules applied to the device type.
77199767f8SToomas Soome  *
78199767f8SToomas Soome  * For disk-type devices, the syntax is:
79199767f8SToomas Soome  *
80199767f8SToomas Soome  * fs<unit>:
81199767f8SToomas Soome  */
82199767f8SToomas Soome static int
efi_parsedev(struct devdesc ** dev,const char * devspec,const char ** path)83199767f8SToomas Soome efi_parsedev(struct devdesc **dev, const char *devspec, const char **path)
84199767f8SToomas Soome {
85199767f8SToomas Soome 	struct devdesc *idev;
86199767f8SToomas Soome 	struct devsw *dv;
87dbacaf56SToomas Soome 	int i, unit, err;
88199767f8SToomas Soome 	char *cp;
89199767f8SToomas Soome 	const char *np;
90199767f8SToomas Soome 
91199767f8SToomas Soome 	/* minimum length check */
92199767f8SToomas Soome 	if (strlen(devspec) < 2)
93199767f8SToomas Soome 		return (EINVAL);
94199767f8SToomas Soome 
95199767f8SToomas Soome 	/* look for a device that matches */
96199767f8SToomas Soome 	for (i = 0; devsw[i] != NULL; i++) {
97199767f8SToomas Soome 		dv = devsw[i];
98422eb17fSToomas Soome 		if (strncmp(devspec, dv->dv_name, strlen(dv->dv_name)) == 0)
99199767f8SToomas Soome 			break;
100199767f8SToomas Soome 	}
101199767f8SToomas Soome 	if (devsw[i] == NULL)
102199767f8SToomas Soome 		return (ENOENT);
103199767f8SToomas Soome 
104199767f8SToomas Soome 	np = devspec + strlen(dv->dv_name);
105dbacaf56SToomas Soome 	idev = NULL;
106dbacaf56SToomas Soome 	err = 0;
107199767f8SToomas Soome 
108dbacaf56SToomas Soome 	switch (dv->dv_type) {
109dbacaf56SToomas Soome 	case DEVT_NONE:
110dbacaf56SToomas Soome 		break;
111dbacaf56SToomas Soome 
112dbacaf56SToomas Soome 	case DEVT_DISK:
113dbacaf56SToomas Soome 		idev = malloc(sizeof (struct disk_devdesc));
114dbacaf56SToomas Soome 		if (idev == NULL)
115dbacaf56SToomas Soome 			return (ENOMEM);
116dbacaf56SToomas Soome 
117dbacaf56SToomas Soome 		err = disk_parsedev((struct disk_devdesc *)idev, np, path);
118dbacaf56SToomas Soome 		if (err != 0)
119dbacaf56SToomas Soome 			goto fail;
120dbacaf56SToomas Soome 		break;
121199767f8SToomas Soome 
122dbacaf56SToomas Soome 	case DEVT_ZFS:
123422eb17fSToomas Soome 		idev = malloc(sizeof (struct zfs_devdesc));
124199767f8SToomas Soome 		if (idev == NULL)
125199767f8SToomas Soome 			return (ENOMEM);
126199767f8SToomas Soome 
127422eb17fSToomas Soome 		err = zfs_parsedev((struct zfs_devdesc *)idev, np, path);
128dbacaf56SToomas Soome 		if (err != 0)
129dbacaf56SToomas Soome 			goto fail;
130dbacaf56SToomas Soome 		break;
131dbacaf56SToomas Soome 
132dbacaf56SToomas Soome 	default:
133422eb17fSToomas Soome 		idev = malloc(sizeof (struct devdesc));
134199767f8SToomas Soome 		if (idev == NULL)
135199767f8SToomas Soome 			return (ENOMEM);
136199767f8SToomas Soome 
137dbacaf56SToomas Soome 		unit = 0;
138dbacaf56SToomas Soome 		cp = (char *)np;
139dbacaf56SToomas Soome 
140199767f8SToomas Soome 		if (*np != '\0' && *np != ':') {
141dbacaf56SToomas Soome 			/* get unit number if present */
142dbacaf56SToomas Soome 			errno = 0;
143dbacaf56SToomas Soome 			unit = strtol(np, &cp, 0);
144dbacaf56SToomas Soome 			if (errno != 0 || cp == np) {
145dbacaf56SToomas Soome 				err = EUNIT;
146dbacaf56SToomas Soome 				goto fail;
147199767f8SToomas Soome 			}
148199767f8SToomas Soome 		}
149dbacaf56SToomas Soome 		if (*cp != '\0' && *cp != ':') {
150dbacaf56SToomas Soome 			err = EINVAL;
151dbacaf56SToomas Soome 			goto fail;
152dbacaf56SToomas Soome 		}
153199767f8SToomas Soome 
154dbacaf56SToomas Soome 		idev->d_unit = unit;
155dbacaf56SToomas Soome 		if (path != NULL)
156dbacaf56SToomas Soome 			*path = (*cp == '\0') ? cp : cp + 1;
157dbacaf56SToomas Soome 		break;
158199767f8SToomas Soome 	}
159199767f8SToomas Soome 
160dbacaf56SToomas Soome 	idev->d_dev = dv;
161dbacaf56SToomas Soome 
162199767f8SToomas Soome 	if (dev != NULL)
163199767f8SToomas Soome 		*dev = idev;
164199767f8SToomas Soome 	else
165199767f8SToomas Soome 		free(idev);
166199767f8SToomas Soome 	return (0);
167dbacaf56SToomas Soome 
168dbacaf56SToomas Soome fail:
169dbacaf56SToomas Soome 	free(idev);
170dbacaf56SToomas Soome 	return (err);
171199767f8SToomas Soome }
172199767f8SToomas Soome 
173199767f8SToomas Soome char *
efi_fmtdev(void * vdev)174199767f8SToomas Soome efi_fmtdev(void *vdev)
175199767f8SToomas Soome {
176199767f8SToomas Soome 	struct devdesc *dev = (struct devdesc *)vdev;
177199767f8SToomas Soome 	static char buf[SPECNAMELEN + 1];
178199767f8SToomas Soome 
179*c142ce19SToomas Soome 	switch (dev->d_dev->dv_type) {
180199767f8SToomas Soome 	case DEVT_NONE:
181422eb17fSToomas Soome 		strlcpy(buf, "(no device)", sizeof (buf));
182199767f8SToomas Soome 		break;
183199767f8SToomas Soome 
184dbacaf56SToomas Soome 	case DEVT_DISK:
185dbacaf56SToomas Soome 		return (disk_fmtdev(vdev));
186dbacaf56SToomas Soome 
187dbacaf56SToomas Soome 	case DEVT_ZFS:
188dbacaf56SToomas Soome 		return (zfs_fmtdev(dev));
189dbacaf56SToomas Soome 
190199767f8SToomas Soome 	default:
191422eb17fSToomas Soome 		snprintf(buf, sizeof (buf), "%s%d:", dev->d_dev->dv_name,
192422eb17fSToomas Soome 		    dev->d_unit);
193199767f8SToomas Soome 		break;
194199767f8SToomas Soome 	}
195199767f8SToomas Soome 
196199767f8SToomas Soome 	return (buf);
197199767f8SToomas Soome }
198199767f8SToomas Soome 
199199767f8SToomas Soome /*
200199767f8SToomas Soome  * Set currdev to suit the value being supplied in (value)
201199767f8SToomas Soome  */
202199767f8SToomas Soome int
efi_setcurrdev(struct env_var * ev,int flags,const void * value)203199767f8SToomas Soome efi_setcurrdev(struct env_var *ev, int flags, const void *value)
204199767f8SToomas Soome {
205199767f8SToomas Soome 	struct devdesc *ncurr;
206199767f8SToomas Soome 	int rv;
207199767f8SToomas Soome 
208199767f8SToomas Soome 	rv = efi_parsedev(&ncurr, value, NULL);
209199767f8SToomas Soome 	if (rv != 0)
210199767f8SToomas Soome 		return (rv);
211199767f8SToomas Soome 
212199767f8SToomas Soome 	free(ncurr);
213199767f8SToomas Soome 	env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
214199767f8SToomas Soome 	return (0);
215199767f8SToomas Soome }
216