2 * Copyright (c) 2016 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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;
40 efi_lookup_image_devpath(EFI_HANDLE handle)
42 EFI_DEVICE_PATH *devpath;
45 status = OpenProtocolByHandle(handle, &ImageDevicePathGUID,
47 if (EFI_ERROR(status))
53 efi_lookup_devpath(EFI_HANDLE handle)
55 EFI_DEVICE_PATH *devpath;
58 status = OpenProtocolByHandle(handle, &DevicePathGUID,
60 if (EFI_ERROR(status))
66 efi_devpath_name(EFI_DEVICE_PATH *devpath)
74 status = BS->LocateProtocol(&DevicePathToTextGUID, NULL,
75 (VOID **)&textProtocol);
76 if (EFI_ERROR(status))
80 if (textProtocol == NULL)
83 return (textProtocol->ConvertDevicePathToText(devpath, TRUE, TRUE));
87 efi_free_devpath_name(CHAR16 *text)
94 efi_devpath_last_node(EFI_DEVICE_PATH *devpath)
97 if (IsDevicePathEnd(devpath))
99 while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
100 devpath = NextDevicePathNode(devpath);
105 efi_devpath_trim(EFI_DEVICE_PATH *devpath)
107 EFI_DEVICE_PATH *node, *copy;
110 if ((node = efi_devpath_last_node(devpath)) == NULL)
112 prefix = (UINT8 *)node - (UINT8 *)devpath;
115 len = prefix + DevicePathNodeLength(NextDevicePathNode(node));
118 memcpy(copy, devpath, prefix);
119 node = (EFI_DEVICE_PATH *)((UINT8 *)copy + prefix);
120 SetDevicePathEndNode(node);
126 efi_devpath_handle(EFI_DEVICE_PATH *devpath)
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.
137 status = BS->LocateDevicePath(&DevicePathGUID, &devpath, &h);
138 if (EFI_ERROR(status))
144 efi_devpath_match_node(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
148 if (devpath1 == NULL || devpath2 == NULL)
150 if (DevicePathType(devpath1) != DevicePathType(devpath2) ||
151 DevicePathSubType(devpath1) != DevicePathSubType(devpath2))
153 len = DevicePathNodeLength(devpath1);
154 if (len != DevicePathNodeLength(devpath2))
156 if (memcmp(devpath1, devpath2, len) != 0)
162 efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
165 if (devpath1 == NULL || devpath2 == NULL)
169 if (!efi_devpath_match_node(devpath1, devpath2))
171 if (IsDevicePathEnd(devpath1))
173 devpath1 = NextDevicePathNode(devpath1);
174 devpath2 = NextDevicePathNode(devpath2);
180 efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path)
184 if (prefix == NULL || path == NULL)
188 if (IsDevicePathEnd(prefix))
191 if (DevicePathType(prefix) != DevicePathType(path) ||
192 DevicePathSubType(prefix) != DevicePathSubType(path))
195 len = DevicePathNodeLength(prefix);
196 if (len != DevicePathNodeLength(path))
199 if (memcmp(prefix, path, len) != 0)
202 prefix = NextDevicePathNode(prefix);
203 path = NextDevicePathNode(path);
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.
213 efi_devpath_to_media_path(EFI_DEVICE_PATH *path)
216 while (!IsDevicePathEnd(path)) {
217 if (DevicePathType(path) == MEDIA_DEVICE_PATH)
219 path = NextDevicePathNode(path);
225 efi_devpath_length(EFI_DEVICE_PATH *path)
227 EFI_DEVICE_PATH *start = path;
229 while (!IsDevicePathEnd(path))
230 path = NextDevicePathNode(path);
231 return ((UINTN)path - (UINTN)start) + DevicePathNodeLength(path);
235 efi_devpath_to_handle(EFI_DEVICE_PATH *path, EFI_HANDLE *handles, unsigned nhandles)
238 EFI_DEVICE_PATH *media, *devpath;
241 media = efi_devpath_to_media_path(path);
244 for (i = 0; i < nhandles; i++) {
246 devpath = efi_lookup_devpath(h);
249 if (!efi_devpath_match_node(media, efi_devpath_to_media_path(devpath)))