devicename.c revision 1.4 1 1.4 kiyohara /* $NetBSD: devicename.c,v 1.4 2009/07/20 04:59:03 kiyohara Exp $ */
2 1.1 cherry
3 1.1 cherry /*-
4 1.1 cherry * Copyright (c) 1998 Michael Smith <msmith (at) freebsd.org>
5 1.1 cherry * All rights reserved.
6 1.1 cherry *
7 1.1 cherry * Redistribution and use in source and binary forms, with or without
8 1.1 cherry * modification, are permitted provided that the following conditions
9 1.1 cherry * are met:
10 1.1 cherry * 1. Redistributions of source code must retain the above copyright
11 1.1 cherry * notice, this list of conditions and the following disclaimer.
12 1.1 cherry * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cherry * notice, this list of conditions and the following disclaimer in the
14 1.1 cherry * documentation and/or other materials provided with the distribution.
15 1.1 cherry *
16 1.1 cherry * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 cherry * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 cherry * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 cherry * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 cherry * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 cherry * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 cherry * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 cherry * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 cherry * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 cherry * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 cherry * SUCH DAMAGE.
27 1.1 cherry */
28 1.1 cherry
29 1.1 cherry #include <sys/cdefs.h>
30 1.1 cherry
31 1.2 cherry /* __FBSDID("$FreeBSD: src/sys/boot/efi/libefi/devicename.c,v 1.3 2004/01/04 23:28:16 obrien Exp $"); */
32 1.2 cherry
33 1.1 cherry #include <lib/libsa/stand.h>
34 1.4 kiyohara #include <lib/libsa/loadfile.h>
35 1.1 cherry #include <sys/disklabel.h>
36 1.1 cherry
37 1.1 cherry #include <bootstrap.h>
38 1.1 cherry
39 1.1 cherry #include <efi.h>
40 1.1 cherry #include <efilib.h>
41 1.1 cherry #include "efiboot.h"
42 1.1 cherry
43 1.1 cherry static int efi_parsedev(struct efi_devdesc **dev, const char *devspec, const char **path);
44 1.1 cherry
45 1.1 cherry /*
46 1.1 cherry * Point (dev) at an allocated device specifier for the device matching the
47 1.1 cherry * path in (devspec). If it contains an explicit device specification,
48 1.1 cherry * use that. If not, use the default device.
49 1.1 cherry */
50 1.1 cherry int
51 1.1 cherry efi_getdev(void **vdev, const char *devspec, const char **path)
52 1.1 cherry {
53 1.1 cherry struct efi_devdesc **dev = (struct efi_devdesc **)vdev;
54 1.1 cherry int rv;
55 1.1 cherry
56 1.1 cherry /*
57 1.1 cherry * If it looks like this is just a path and no
58 1.1 cherry * device, go with the current device.
59 1.1 cherry */
60 1.1 cherry if ((devspec == NULL) ||
61 1.1 cherry (devspec[0] == '/') ||
62 1.1 cherry (strchr(devspec, ':') == NULL)) {
63 1.1 cherry
64 1.1 cherry if (((rv = efi_parsedev(dev, getenv("currdev"), NULL)) == 0) &&
65 1.1 cherry (path != NULL))
66 1.1 cherry *path = devspec;
67 1.1 cherry return(rv);
68 1.1 cherry }
69 1.1 cherry
70 1.1 cherry /*
71 1.1 cherry * Try to parse the device name off the beginning of the devspec
72 1.1 cherry */
73 1.1 cherry return(efi_parsedev(dev, devspec, path));
74 1.1 cherry }
75 1.1 cherry
76 1.1 cherry /*
77 1.1 cherry * Point (dev) at an allocated device specifier matching the string version
78 1.1 cherry * at the beginning of (devspec). Return a pointer to the remaining
79 1.1 cherry * text in (path).
80 1.1 cherry *
81 1.1 cherry * In all cases, the beginning of (devspec) is compared to the names
82 1.1 cherry * of known devices in the device switch, and then any following text
83 1.1 cherry * is parsed according to the rules applied to the device type.
84 1.1 cherry *
85 1.1 cherry * For disk-type devices, the syntax is:
86 1.1 cherry *
87 1.1 cherry * disk<unit>[s<slice>][<partition>]:
88 1.1 cherry *
89 1.1 cherry */
90 1.1 cherry static int
91 1.1 cherry efi_parsedev(struct efi_devdesc **dev, const char *devspec, const char **path)
92 1.1 cherry {
93 1.1 cherry struct efi_devdesc *idev;
94 1.1 cherry struct devsw *dv;
95 1.1 cherry int dv_type;
96 1.1 cherry int i, unit, slice, partition, err;
97 1.1 cherry char *cp;
98 1.1 cherry const char *np;
99 1.1 cherry
100 1.1 cherry /* minimum length check */
101 1.1 cherry if (strlen(devspec) < 2)
102 1.1 cherry return(EINVAL);
103 1.1 cherry
104 1.1 cherry /* look for a device that matches */
105 1.1 cherry for (i = 0, dv = NULL; i < ndevs; i++) {
106 1.1 cherry if (!strncmp(devspec, devsw[i].dv_name, strlen(devsw[i].dv_name))) {
107 1.1 cherry dv = &devsw[i];
108 1.1 cherry break;
109 1.1 cherry }
110 1.1 cherry }
111 1.1 cherry
112 1.1 cherry if (dv == NULL)
113 1.1 cherry return(ENOENT);
114 1.1 cherry idev = alloc(sizeof(struct efi_devdesc));
115 1.1 cherry err = 0;
116 1.1 cherry np = (devspec + strlen(dv->dv_name));
117 1.1 cherry
118 1.1 cherry dv_type = DEVT_NONE;
119 1.1 cherry if (!strncmp("disk", dv->dv_name, 4)) dv_type = DEVT_DISK;
120 1.1 cherry if (!strncmp("net", dv->dv_name, 3)) dv_type = DEVT_DISK;
121 1.1 cherry
122 1.1 cherry switch(dv_type) {
123 1.1 cherry case DEVT_NONE: /* XXX what to do here? Do we care? */
124 1.1 cherry break;
125 1.1 cherry
126 1.1 cherry case DEVT_DISK:
127 1.1 cherry unit = -1;
128 1.1 cherry slice = -1;
129 1.1 cherry partition = -1;
130 1.1 cherry if (*np && (*np != ':')) {
131 1.1 cherry unit = strtol(np, &cp, 10); /* next comes the unit number */
132 1.1 cherry if (cp == np) {
133 1.1 cherry err = EUNIT;
134 1.1 cherry goto fail;
135 1.1 cherry }
136 1.1 cherry if (*cp == 's') { /* got a slice number */
137 1.1 cherry np = cp + 1;
138 1.1 cherry slice = strtol(np, &cp, 10);
139 1.1 cherry if (cp == np) {
140 1.3 christos err = EPART; /* XXX : NetBSD calls a FreeBSD SLICE, a Partition! */
141 1.1 cherry goto fail;
142 1.1 cherry }
143 1.1 cherry }
144 1.1 cherry if (*cp && (*cp != ':')) {
145 1.1 cherry partition = *cp - 'a'; /* get a partition number */
146 1.1 cherry if ((partition < 0) || (partition >= MAXPARTITIONS)) {
147 1.1 cherry err = EPART;
148 1.1 cherry goto fail;
149 1.1 cherry }
150 1.1 cherry cp++;
151 1.1 cherry }
152 1.1 cherry }
153 1.1 cherry if (*cp && (*cp != ':')) {
154 1.1 cherry err = EINVAL;
155 1.1 cherry goto fail;
156 1.1 cherry }
157 1.1 cherry
158 1.1 cherry idev->d_kind.efidisk.unit = unit;
159 1.1 cherry idev->d_kind.efidisk.slice = slice;
160 1.1 cherry idev->d_kind.efidisk.partition = partition;
161 1.1 cherry
162 1.1 cherry if (path != NULL)
163 1.1 cherry *path = (*cp == 0) ? cp : cp + 1;
164 1.1 cherry break;
165 1.1 cherry
166 1.1 cherry case DEVT_NET:
167 1.1 cherry unit = 0;
168 1.1 cherry
169 1.1 cherry if (*np && (*np != ':')) {
170 1.1 cherry unit = strtol(np, &cp, 0); /* get unit number if present */
171 1.1 cherry if (cp == np) {
172 1.1 cherry err = EUNIT;
173 1.1 cherry goto fail;
174 1.1 cherry }
175 1.1 cherry }
176 1.1 cherry if (*cp && (*cp != ':')) {
177 1.1 cherry err = EINVAL;
178 1.1 cherry goto fail;
179 1.1 cherry }
180 1.1 cherry
181 1.1 cherry idev->d_kind.netif.unit = unit;
182 1.1 cherry if (path != NULL)
183 1.1 cherry *path = (*cp == 0) ? cp : cp + 1;
184 1.1 cherry break;
185 1.1 cherry
186 1.1 cherry default:
187 1.1 cherry err = EINVAL;
188 1.1 cherry goto fail;
189 1.1 cherry }
190 1.1 cherry idev->d_dev = dv;
191 1.1 cherry idev->d_type = dv_type;
192 1.1 cherry if (dev == NULL) {
193 1.1 cherry free(idev);
194 1.1 cherry } else {
195 1.1 cherry *dev = idev;
196 1.1 cherry }
197 1.1 cherry return(0);
198 1.1 cherry
199 1.1 cherry fail:
200 1.1 cherry free(idev);
201 1.1 cherry return(err);
202 1.1 cherry }
203 1.1 cherry
204 1.1 cherry
205 1.1 cherry char *
206 1.1 cherry efi_fmtdev(void *vdev)
207 1.1 cherry {
208 1.1 cherry struct efi_devdesc *dev = (struct efi_devdesc *)vdev;
209 1.1 cherry static char buf[128]; /* XXX device length constant? */
210 1.1 cherry char *cp;
211 1.1 cherry
212 1.1 cherry switch(dev->d_type) {
213 1.1 cherry case DEVT_NONE:
214 1.1 cherry strcpy(buf, "(no device)");
215 1.1 cherry break;
216 1.1 cherry
217 1.1 cherry case DEVT_DISK:
218 1.1 cherry cp = buf;
219 1.1 cherry cp += sprintf(cp, "%s%d", dev->d_dev->dv_name, dev->d_kind.efidisk.unit);
220 1.1 cherry if (dev->d_kind.efidisk.slice > 0)
221 1.1 cherry cp += sprintf(cp, "s%d", dev->d_kind.efidisk.slice);
222 1.1 cherry if (dev->d_kind.efidisk.partition >= 0)
223 1.1 cherry cp += sprintf(cp, "%c", dev->d_kind.efidisk.partition + 'a');
224 1.1 cherry strcat(cp, ":");
225 1.1 cherry break;
226 1.1 cherry
227 1.1 cherry case DEVT_NET:
228 1.1 cherry sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_kind.netif.unit);
229 1.1 cherry break;
230 1.1 cherry }
231 1.1 cherry return(buf);
232 1.1 cherry }
233 1.1 cherry
234 1.1 cherry
235 1.1 cherry /*
236 1.1 cherry * Set currdev to suit the value being supplied in (value)
237 1.1 cherry */
238 1.1 cherry int
239 1.1 cherry efi_setcurrdev(struct env_var *ev, int flags, void *value)
240 1.1 cherry {
241 1.1 cherry struct efi_devdesc *ncurr;
242 1.1 cherry int rv;
243 1.1 cherry
244 1.1 cherry if ((rv = efi_parsedev(&ncurr, value, NULL)) != 0)
245 1.1 cherry return(rv);
246 1.1 cherry free(ncurr);
247 1.1 cherry env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
248 1.1 cherry return(0);
249 1.1 cherry }
250 1.1 cherry
251