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