]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/boot/efi/loader/devicename.c
Merge clang trunk r300422 and resolve conflicts.
[FreeBSD/FreeBSD.git] / sys / boot / efi / loader / devicename.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * Copyright (c) 2006 Marcel Moolenaar
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <stand.h>
32 #include <string.h>
33 #include <sys/disklabel.h>
34 #include <sys/param.h>
35 #include <bootstrap.h>
36 #include <disk.h>
37 #ifdef EFI_ZFS_BOOT
38 #include <libzfs.h>
39 #endif
40
41 #include <efi.h>
42 #include <efilib.h>
43
44 #include "loader_efi.h"
45
46 static int efi_parsedev(struct devdesc **, const char *, const char **);
47
48 /*
49  * Point (dev) at an allocated device specifier for the device matching the
50  * path in (devspec). If it contains an explicit device specification,
51  * use that.  If not, use the default device.
52  */
53 int
54 efi_getdev(void **vdev, const char *devspec, const char **path)
55 {
56         struct devdesc **dev = (struct devdesc **)vdev;
57         int rv;
58
59         /*
60          * If it looks like this is just a path and no device, then
61          * use the current device instead.
62          */
63         if (devspec == NULL || *devspec == '/' || !strchr(devspec, ':')) {
64                 rv = efi_parsedev(dev, getenv("currdev"), NULL);
65                 if (rv == 0 && path != NULL)
66                         *path = devspec;
67                 return (rv);
68         }
69
70         /* Parse the device name off the beginning of the devspec. */
71         return (efi_parsedev(dev, devspec, path));
72 }
73
74 /*
75  * Point (dev) at an allocated device specifier matching the string version
76  * at the beginning of (devspec).  Return a pointer to the remaining
77  * text in (path).
78  *
79  * In all cases, the beginning of (devspec) is compared to the names
80  * of known devices in the device switch, and then any following text
81  * is parsed according to the rules applied to the device type.
82  *
83  * For disk-type devices, the syntax is:
84  *
85  * fs<unit>:
86  */
87 static int
88 efi_parsedev(struct devdesc **dev, const char *devspec, const char **path)
89 {
90         struct devdesc *idev;
91         struct devsw *dv;
92         char *cp;
93         const char *np;
94         int i, err;
95
96         /* minimum length check */
97         if (strlen(devspec) < 2)
98                 return (EINVAL);
99
100         /* look for a device that matches */
101         for (i = 0; devsw[i] != NULL; i++) {
102                 dv = devsw[i];
103                 if (!strncmp(devspec, dv->dv_name, strlen(dv->dv_name)))
104                         break;
105         }
106         if (devsw[i] == NULL)
107                 return (ENOENT);
108
109         np = devspec + strlen(dv->dv_name);
110         err = 0;
111
112         switch (dv->dv_type) {
113         case DEVT_NONE:
114                 break;
115
116         case DEVT_DISK:
117                 idev = malloc(sizeof(struct disk_devdesc));
118                 if (idev == NULL)
119                         return (ENOMEM);
120
121                 err = disk_parsedev((struct disk_devdesc *)idev, np, path);
122                 if (err != 0) {
123                         free(idev);
124                         return (err);
125                 }
126                 break;
127
128 #ifdef EFI_ZFS_BOOT
129         case DEVT_ZFS:
130                 idev = malloc(sizeof(struct zfs_devdesc));
131                 if (idev == NULL)
132                         return (ENOMEM);
133
134                 err = zfs_parsedev((struct zfs_devdesc*)idev, np, path);
135                 if (err != 0) {
136                         free(idev);
137                         return (err);
138                 }
139                 break;
140 #endif
141         default:
142                 idev = malloc(sizeof(struct devdesc));
143                 if (idev == NULL)
144                         return (ENOMEM);
145
146                 idev->d_unit = -1;
147                 cp = (char *)np;
148                 if (*np != '\0' && *np != ':') {
149                         idev->d_unit = strtol(np, &cp, 0);
150                         if (cp == np) {
151                                 free(idev);
152                                 return (EUNIT);
153                         }
154                 }
155                 if (*cp != '\0' && *cp != ':') {
156                         free(idev);
157                         return (EINVAL);
158                 }
159
160                 if (path != NULL)
161                         *path = (*cp == 0) ? cp : cp + 1;
162                 break;
163         }
164
165         idev->d_dev = dv;
166         idev->d_type = dv->dv_type;
167
168         if (dev != NULL)
169                 *dev = idev;
170         else
171                 free(idev);
172         return (0);
173 }
174
175 char *
176 efi_fmtdev(void *vdev)
177 {
178         struct devdesc *dev = (struct devdesc *)vdev;
179         static char buf[SPECNAMELEN + 1];
180
181         switch(dev->d_type) {
182         case DEVT_NONE:
183                 strcpy(buf, "(no device)");
184                 break;
185
186         case DEVT_DISK:
187                 return (disk_fmtdev(vdev));
188
189 #ifdef EFI_ZFS_BOOT
190         case DEVT_ZFS:
191                 return (zfs_fmtdev(dev));
192 #endif
193         default:
194                 sprintf(buf, "%s%d:", dev->d_dev->dv_name, dev->d_unit);
195                 break;
196         }
197
198         return (buf);
199 }
200
201 /*
202  * Set currdev to suit the value being supplied in (value)
203  */
204 int
205 efi_setcurrdev(struct env_var *ev, int flags, const void *value)
206 {
207         struct devdesc *ncurr;
208         int rv;
209
210         rv = efi_parsedev(&ncurr, value, NULL);
211         if (rv != 0)
212                 return (rv);
213
214         free(ncurr);
215         env_setenv(ev->ev_name, flags | EV_NOHOOK, value, NULL, NULL);
216         return (0);
217 }