2 * Copyright (c) 2016 John Baldwin <jhb@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
32 static EFI_GUID ImageDevicePathGUID =
33 EFI_LOADED_IMAGE_DEVICE_PATH_PROTOCOL_GUID;
34 static EFI_GUID DevicePathGUID = DEVICE_PATH_PROTOCOL;
35 static EFI_GUID DevicePathToTextGUID = EFI_DEVICE_PATH_TO_TEXT_PROTOCOL_GUID;
36 static EFI_DEVICE_PATH_TO_TEXT_PROTOCOL *textProtocol;
39 efi_lookup_image_devpath(EFI_HANDLE handle)
41 EFI_DEVICE_PATH *devpath;
44 status = BS->HandleProtocol(handle, &ImageDevicePathGUID,
46 if (EFI_ERROR(status))
52 efi_lookup_devpath(EFI_HANDLE handle)
54 EFI_DEVICE_PATH *devpath;
57 status = BS->HandleProtocol(handle, &DevicePathGUID, (VOID **)&devpath);
58 if (EFI_ERROR(status))
64 efi_devpath_name(EFI_DEVICE_PATH *devpath)
72 status = BS->LocateProtocol(&DevicePathToTextGUID, NULL,
73 (VOID **)&textProtocol);
74 if (EFI_ERROR(status))
78 if (textProtocol == NULL)
81 return (textProtocol->ConvertDevicePathToText(devpath, TRUE, TRUE));
85 efi_free_devpath_name(CHAR16 *text)
92 efi_devpath_last_node(EFI_DEVICE_PATH *devpath)
95 if (IsDevicePathEnd(devpath))
97 while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
98 devpath = NextDevicePathNode(devpath);
103 efi_devpath_trim(EFI_DEVICE_PATH *devpath)
105 EFI_DEVICE_PATH *node, *copy;
108 if ((node = efi_devpath_last_node(devpath)) == NULL)
110 prefix = (UINT8 *)node - (UINT8 *)devpath;
113 len = prefix + DevicePathNodeLength(NextDevicePathNode(node));
116 memcpy(copy, devpath, prefix);
117 node = (EFI_DEVICE_PATH *)((UINT8 *)copy + prefix);
118 SetDevicePathEndNode(node);
124 efi_devpath_handle(EFI_DEVICE_PATH *devpath)
130 * There isn't a standard way to locate a handle for a given
131 * device path. However, querying the EFI_DEVICE_PATH protocol
132 * for a given device path should give us a handle for the
133 * closest node in the path to the end that is valid.
135 status = BS->LocateDevicePath(&DevicePathGUID, &devpath, &h);
136 if (EFI_ERROR(status))
142 efi_devpath_match_node(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
146 if (devpath1 == NULL || devpath2 == NULL)
148 if (DevicePathType(devpath1) != DevicePathType(devpath2) ||
149 DevicePathSubType(devpath1) != DevicePathSubType(devpath2))
151 len = DevicePathNodeLength(devpath1);
152 if (len != DevicePathNodeLength(devpath2))
154 if (memcmp(devpath1, devpath2, len) != 0)
160 efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
163 if (devpath1 == NULL || devpath2 == NULL)
167 if (!efi_devpath_match_node(devpath1, devpath2))
169 if (IsDevicePathEnd(devpath1))
171 devpath1 = NextDevicePathNode(devpath1);
172 devpath2 = NextDevicePathNode(devpath2);
178 efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path)
182 if (prefix == NULL || path == NULL)
186 if (IsDevicePathEnd(prefix))
189 if (DevicePathType(prefix) != DevicePathType(path) ||
190 DevicePathSubType(prefix) != DevicePathSubType(path))
193 len = DevicePathNodeLength(prefix);
194 if (len != DevicePathNodeLength(path))
197 if (memcmp(prefix, path, len) != 0)
200 prefix = NextDevicePathNode(prefix);
201 path = NextDevicePathNode(path);
207 * Skip over the 'prefix' part of path and return the part of the path
208 * that starts with the first node that's a MEDIA_DEVICE_PATH.
211 efi_devpath_to_media_path(EFI_DEVICE_PATH *path)
214 while (!IsDevicePathEnd(path)) {
215 if (DevicePathType(path) == MEDIA_DEVICE_PATH)
217 path = NextDevicePathNode(path);
223 efi_devpath_length(EFI_DEVICE_PATH *path)
225 EFI_DEVICE_PATH *start = path;
227 while (!IsDevicePathEnd(path))
228 path = NextDevicePathNode(path);
229 return ((UINTN)path - (UINTN)start) + DevicePathNodeLength(path);