1 /*-
2 * Copyright (c) 2016 John Baldwin <[email protected]>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <efi.h>
31 #include <efilib.h>
32
33 static EFI_GUID ImageDevicePathGUID =
34 EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
35 static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
36 static EFI_GUID DevicePathToTextGUID = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
37 static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *textProtocol;
38
39 EFI_DEVICE_PATH *
efi_lookup_image_devpath(EFI_HANDLE handle)40 efi_lookup_image_devpath(EFI_HANDLE handle)
41 {
42 EFI_DEVICE_PATH *devpath;
43 EFI_STATUS status;
44
45 status = OpenProtocolByHandle(handle, &ImageDevicePathGUID,
46 (void **)&devpath);
47 if (EFI_ERROR(status))
48 devpath = NULL;
49 return (devpath);
50 }
51
52 EFI_DEVICE_PATH *
efi_lookup_devpath(EFI_HANDLE handle)53 efi_lookup_devpath(EFI_HANDLE handle)
54 {
55 EFI_DEVICE_PATH *devpath;
56 EFI_STATUS status;
57
58 status = OpenProtocolByHandle(handle, &DevicePathGUID,
59 (void **)&devpath);
60 if (EFI_ERROR(status))
61 devpath = NULL;
62 return (devpath);
63 }
64
65 CHAR16 *
efi_devpath_name(EFI_DEVICE_PATH * devpath)66 efi_devpath_name(EFI_DEVICE_PATH *devpath)
67 {
68 static int once = 1;
69 EFI_STATUS status;
70
71 if (devpath == NULL)
72 return (NULL);
73 if (once) {
74 status = BS->LocateProtocol(&DevicePathToTextGUID, NULL,
75 (VOID **)&textProtocol);
76 if (EFI_ERROR(status))
77 textProtocol = NULL;
78 once = 0;
79 }
80 if (textProtocol == NULL)
81 return (NULL);
82
83 return (textProtocol->ConvertDevicePathToText(devpath, TRUE, TRUE));
84 }
85
86 void
efi_free_devpath_name(CHAR16 * text)87 efi_free_devpath_name(CHAR16 *text)
88 {
89
90 BS->FreePool(text);
91 }
92
93 EFI_DEVICE_PATH *
efi_devpath_last_node(EFI_DEVICE_PATH * devpath)94 efi_devpath_last_node(EFI_DEVICE_PATH *devpath)
95 {
96
97 if (IsDevicePathEnd(devpath))
98 return (NULL);
99 while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
100 devpath = NextDevicePathNode(devpath);
101 return (devpath);
102 }
103
104 EFI_DEVICE_PATH *
efi_devpath_trim(EFI_DEVICE_PATH * devpath)105 efi_devpath_trim(EFI_DEVICE_PATH *devpath)
106 {
107 EFI_DEVICE_PATH *node, *copy;
108 size_t prefix, len;
109
110 if ((node = efi_devpath_last_node(devpath)) == NULL)
111 return (NULL);
112 prefix = (UINT8 *)node - (UINT8 *)devpath;
113 if (prefix == 0)
114 return (NULL);
115 len = prefix + DevicePathNodeLength(NextDevicePathNode(node));
116 copy = malloc(len);
117 if (copy != NULL) {
118 memcpy(copy, devpath, prefix);
119 node = (EFI_DEVICE_PATH *)((UINT8 *)copy + prefix);
120 SetDevicePathEndNode(node);
121 }
122 return (copy);
123 }
124
125 EFI_HANDLE
efi_devpath_handle(EFI_DEVICE_PATH * devpath)126 efi_devpath_handle(EFI_DEVICE_PATH *devpath)
127 {
128 EFI_STATUS status;
129 EFI_HANDLE h;
130
131 /*
132 * There isn't a standard way to locate a handle for a given
133 * device path. However, querying the EFI_DEVICE_PATH protocol
134 * for a given device path should give us a handle for the
135 * closest node in the path to the end that is valid.
136 */
137 status = BS->LocateDevicePath(&DevicePathGUID, &devpath, &h);
138 if (EFI_ERROR(status))
139 return (NULL);
140 return (h);
141 }
142
143 bool
efi_devpath_match_node(EFI_DEVICE_PATH * devpath1,EFI_DEVICE_PATH * devpath2)144 efi_devpath_match_node(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
145 {
146 size_t len;
147
148 if (devpath1 == NULL || devpath2 == NULL)
149 return (false);
150 if (DevicePathType(devpath1) != DevicePathType(devpath2) ||
151 DevicePathSubType(devpath1) != DevicePathSubType(devpath2))
152 return (false);
153 len = DevicePathNodeLength(devpath1);
154 if (len != DevicePathNodeLength(devpath2))
155 return (false);
156 if (memcmp(devpath1, devpath2, len) != 0)
157 return (false);
158 return (true);
159 }
160
161 bool
efi_devpath_match(EFI_DEVICE_PATH * devpath1,EFI_DEVICE_PATH * devpath2)162 efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
163 {
164
165 if (devpath1 == NULL || devpath2 == NULL)
166 return (false);
167
168 while (true) {
169 if (!efi_devpath_match_node(devpath1, devpath2))
170 return false;
171 if (IsDevicePathEnd(devpath1))
172 break;
173 devpath1 = NextDevicePathNode(devpath1);
174 devpath2 = NextDevicePathNode(devpath2);
175 }
176 return (true);
177 }
178
179 bool
efi_devpath_is_prefix(EFI_DEVICE_PATH * prefix,EFI_DEVICE_PATH * path)180 efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path)
181 {
182 size_t len;
183
184 if (prefix == NULL || path == NULL)
185 return (false);
186
187 while (1) {
188 if (IsDevicePathEnd(prefix))
189 break;
190
191 if (DevicePathType(prefix) != DevicePathType(path) ||
192 DevicePathSubType(prefix) != DevicePathSubType(path))
193 return (false);
194
195 len = DevicePathNodeLength(prefix);
196 if (len != DevicePathNodeLength(path))
197 return (false);
198
199 if (memcmp(prefix, path, len) != 0)
200 return (false);
201
202 prefix = NextDevicePathNode(prefix);
203 path = NextDevicePathNode(path);
204 }
205 return (true);
206 }
207
208 /*
209 * Skip over the 'prefix' part of path and return the part of the path
210 * that starts with the first node that's a MEDIA_DEVICE_PATH.
211 */
212 EFI_DEVICE_PATH *
efi_devpath_to_media_path(EFI_DEVICE_PATH * path)213 efi_devpath_to_media_path(EFI_DEVICE_PATH *path)
214 {
215
216 while (!IsDevicePathEnd(path)) {
217 if (DevicePathType(path) == MEDIA_DEVICE_PATH)
218 return (path);
219 path = NextDevicePathNode(path);
220 }
221 return (NULL);
222 }
223
224 UINTN
efi_devpath_length(EFI_DEVICE_PATH * path)225 efi_devpath_length(EFI_DEVICE_PATH *path)
226 {
227 EFI_DEVICE_PATH *start = path;
228
229 while (!IsDevicePathEnd(path))
230 path = NextDevicePathNode(path);
231 return ((UINTN)path - (UINTN)start) + DevicePathNodeLength(path);
232 }
233
234 EFI_HANDLE
efi_devpath_to_handle(EFI_DEVICE_PATH * path,EFI_HANDLE * handles,unsigned nhandles)235 efi_devpath_to_handle(EFI_DEVICE_PATH *path, EFI_HANDLE *handles, unsigned nhandles)
236 {
237 unsigned i;
238 EFI_DEVICE_PATH *media, *devpath;
239 EFI_HANDLE h;
240
241 media = efi_devpath_to_media_path(path);
242 if (media == NULL)
243 return (NULL);
244 for (i = 0; i < nhandles; i++) {
245 h = handles[i];
246 devpath = efi_lookup_devpath(h);
247 if (devpath == NULL)
248 continue;
249 if (!efi_devpath_match_node(media, efi_devpath_to_media_path(devpath)))
250 continue;
251 return (h);
252 }
253 return (NULL);
254 }
255