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 = BS->HandleProtocol(handle, &ImageDevicePathGUID,
47 if (EFI_ERROR(status))
53 efi_lookup_devpath(EFI_HANDLE handle)
55 EFI_DEVICE_PATH *devpath;
58 status = BS->HandleProtocol(handle, &DevicePathGUID, (VOID **)&devpath);
59 if (EFI_ERROR(status))
65 efi_devpath_name(EFI_DEVICE_PATH *devpath)
73 status = BS->LocateProtocol(&DevicePathToTextGUID, NULL,
74 (VOID **)&textProtocol);
75 if (EFI_ERROR(status))
79 if (textProtocol == NULL)
82 return (textProtocol->ConvertDevicePathToText(devpath, TRUE, TRUE));
86 efi_free_devpath_name(CHAR16 *text)
93 efi_devpath_last_node(EFI_DEVICE_PATH *devpath)
96 if (IsDevicePathEnd(devpath))
98 while (!IsDevicePathEnd(NextDevicePathNode(devpath)))
99 devpath = NextDevicePathNode(devpath);
104 efi_devpath_trim(EFI_DEVICE_PATH *devpath)
106 EFI_DEVICE_PATH *node, *copy;
109 if ((node = efi_devpath_last_node(devpath)) == NULL)
111 prefix = (UINT8 *)node - (UINT8 *)devpath;
114 len = prefix + DevicePathNodeLength(NextDevicePathNode(node));
117 memcpy(copy, devpath, prefix);
118 node = (EFI_DEVICE_PATH *)((UINT8 *)copy + prefix);
119 SetDevicePathEndNode(node);
125 efi_devpath_handle(EFI_DEVICE_PATH *devpath)
131 * There isn't a standard way to locate a handle for a given
132 * device path. However, querying the EFI_DEVICE_PATH protocol
133 * for a given device path should give us a handle for the
134 * closest node in the path to the end that is valid.
136 status = BS->LocateDevicePath(&DevicePathGUID, &devpath, &h);
137 if (EFI_ERROR(status))
143 efi_devpath_match_node(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
147 if (devpath1 == NULL || devpath2 == NULL)
149 if (DevicePathType(devpath1) != DevicePathType(devpath2) ||
150 DevicePathSubType(devpath1) != DevicePathSubType(devpath2))
152 len = DevicePathNodeLength(devpath1);
153 if (len != DevicePathNodeLength(devpath2))
155 if (memcmp(devpath1, devpath2, len) != 0)
161 efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2)
164 if (devpath1 == NULL || devpath2 == NULL)
168 if (!efi_devpath_match_node(devpath1, devpath2))
170 if (IsDevicePathEnd(devpath1))
172 devpath1 = NextDevicePathNode(devpath1);
173 devpath2 = NextDevicePathNode(devpath2);
179 efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path)
183 if (prefix == NULL || path == NULL)
187 if (IsDevicePathEnd(prefix))
190 if (DevicePathType(prefix) != DevicePathType(path) ||
191 DevicePathSubType(prefix) != DevicePathSubType(path))
194 len = DevicePathNodeLength(prefix);
195 if (len != DevicePathNodeLength(path))
198 if (memcmp(prefix, path, len) != 0)
201 prefix = NextDevicePathNode(prefix);
202 path = NextDevicePathNode(path);
208 * Skip over the 'prefix' part of path and return the part of the path
209 * that starts with the first node that's a MEDIA_DEVICE_PATH.
212 efi_devpath_to_media_path(EFI_DEVICE_PATH *path)
215 while (!IsDevicePathEnd(path)) {
216 if (DevicePathType(path) == MEDIA_DEVICE_PATH)
218 path = NextDevicePathNode(path);
224 efi_devpath_length(EFI_DEVICE_PATH *path)
226 EFI_DEVICE_PATH *start = path;
228 while (!IsDevicePathEnd(path))
229 path = NextDevicePathNode(path);
230 return ((UINTN)path - (UINTN)start) + DevicePathNodeLength(path);