xref: /netbsd-src/sys/arch/ia64/stand/efi/libefi/devicename.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: devicename.c,v 1.8 2014/04/08 21:51:06 martin Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998 Michael Smith <msmith@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;
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 && (*cp != ':')) {
155 			err = EINVAL;
156 			goto fail;
157 		}
158 
159 		idev->d_kind.efidisk.unit = unit;
160 		idev->d_kind.efidisk.slice = slice;
161 		idev->d_kind.efidisk.partition = partition;
162 
163 		if (path != NULL)
164 			*path = (*cp == 0) ? cp : cp + 1;
165 		break;
166 
167 	case DEVT_NET:
168 		unit = 0;
169 
170 		if (*np && (*np != ':')) {
171 			unit = strtol(np, &cp, 0);	/* get unit number if present */
172 			if (cp == np) {
173 				err = EUNIT;
174 				goto fail;
175 			}
176 		}
177 		if (*cp && (*cp != ':')) {
178 			err = EINVAL;
179 			goto fail;
180 		}
181 
182 		idev->d_kind.netif.unit = unit;
183 		if (path != NULL)
184 			*path = (*cp == 0) ? cp : cp + 1;
185 		break;
186 
187 	default:
188 		err = EINVAL;
189 		goto fail;
190 	}
191 	idev->d_dev = dv;
192 	idev->d_type = dv_type;
193 	if (dev == NULL) {
194 		free(idev);
195 	} else {
196 		*dev = idev;
197 	}
198 	return(0);
199 
200  fail:
201 	free(idev);
202 	return(err);
203 }
204 
205 
206 char *
207 efi_fmtdev(void *vdev)
208 {
209 	struct efi_devdesc *dev = (struct efi_devdesc *)vdev;
210 	static char	buf[128];	/* XXX device length constant? */
211 	size_t		len, buflen = sizeof(buf);
212 
213 	switch(dev->d_type) {
214 	case DEVT_NONE:
215 		strlcpy(buf, "(no device)", sizeof(buf));
216 		break;
217 
218 	case DEVT_DISK:
219 		len = snprintf(buf, buflen, "%s%d", dev->d_dev->dv_name, dev->d_kind.efidisk.unit);
220 		if (len > buflen)
221 			len = buflen;
222 		if (dev->d_kind.efidisk.slice > 0) {
223 			len += snprintf(buf + len, buflen - len, "s%d", dev->d_kind.efidisk.slice);
224 			if (len > buflen)
225 				len = buflen;
226 		}
227 		if (dev->d_kind.efidisk.partition >= 0) {
228 			len += snprintf(buf + len, buflen - len, "%c", dev->d_kind.efidisk.partition + 'a');
229 			if (len > buflen)
230 				len = buflen;
231 		}
232 		strlcat(buf, ":", sizeof(buf) - len);
233 		break;
234 
235 	case DEVT_NET:
236 		snprintf(buf, buflen, "%s%d:", dev->d_dev->dv_name, dev->d_kind.netif.unit);
237 		break;
238 	}
239 	return(buf);
240 }
241 
242 
243 /*
244  * Set currdev to suit the value being supplied in (value)
245  */
246 int
247 efi_setcurrdev(struct env_var *ev, int flags, void *value)
248 {
249 	struct efi_devdesc *ncurr;
250 	int		rv;
251 
252 	if ((rv = efi_parsedev(&ncurr, value, NULL)) != 0)
253 		return(rv);
254 	free(ncurr);
255 	env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
256 	return(0);
257 }
258 
259