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