1 /*- 2 * Copyright (c) 2016 John Baldwin <jhb@FreeBSD.org> 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 * 40 efi_lookup_image_devpath(EFI_HANDLE handle) 41 { 42 EFI_DEVICE_PATH *devpath; 43 EFI_STATUS status; 44 45 status = BS->HandleProtocol(handle, &ImageDevicePathGUID, 46 (VOID **)&devpath); 47 if (EFI_ERROR(status)) 48 devpath = NULL; 49 return (devpath); 50 } 51 52 EFI_DEVICE_PATH * 53 efi_lookup_devpath(EFI_HANDLE handle) 54 { 55 EFI_DEVICE_PATH *devpath; 56 EFI_STATUS status; 57 58 status = BS->HandleProtocol(handle, &DevicePathGUID, (VOID **)&devpath); 59 if (EFI_ERROR(status)) 60 devpath = NULL; 61 return (devpath); 62 } 63 64 CHAR16 * 65 efi_devpath_name(EFI_DEVICE_PATH *devpath) 66 { 67 static int once = 1; 68 EFI_STATUS status; 69 70 if (devpath == NULL) 71 return (NULL); 72 if (once) { 73 status = BS->LocateProtocol(&DevicePathToTextGUID, NULL, 74 (VOID **)&textProtocol); 75 if (EFI_ERROR(status)) 76 textProtocol = NULL; 77 once = 0; 78 } 79 if (textProtocol == NULL) 80 return (NULL); 81 82 return (textProtocol->ConvertDevicePathToText(devpath, TRUE, TRUE)); 83 } 84 85 void 86 efi_free_devpath_name(CHAR16 *text) 87 { 88 89 BS->FreePool(text); 90 } 91 92 EFI_DEVICE_PATH * 93 efi_devpath_last_node(EFI_DEVICE_PATH *devpath) 94 { 95 96 if (IsDevicePathEnd(devpath)) 97 return (NULL); 98 while (!IsDevicePathEnd(NextDevicePathNode(devpath))) 99 devpath = NextDevicePathNode(devpath); 100 return (devpath); 101 } 102 103 EFI_DEVICE_PATH * 104 efi_devpath_trim(EFI_DEVICE_PATH *devpath) 105 { 106 EFI_DEVICE_PATH *node, *copy; 107 size_t prefix, len; 108 109 if ((node = efi_devpath_last_node(devpath)) == NULL) 110 return (NULL); 111 prefix = (UINT8 *)node - (UINT8 *)devpath; 112 if (prefix == 0) 113 return (NULL); 114 len = prefix + DevicePathNodeLength(NextDevicePathNode(node)); 115 copy = malloc(len); 116 if (copy != NULL) { 117 memcpy(copy, devpath, prefix); 118 node = (EFI_DEVICE_PATH *)((UINT8 *)copy + prefix); 119 SetDevicePathEndNode(node); 120 } 121 return (copy); 122 } 123 124 EFI_HANDLE 125 efi_devpath_handle(EFI_DEVICE_PATH *devpath) 126 { 127 EFI_STATUS status; 128 EFI_HANDLE h; 129 130 /* 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. 135 */ 136 status = BS->LocateDevicePath(&DevicePathGUID, &devpath, &h); 137 if (EFI_ERROR(status)) 138 return (NULL); 139 return (h); 140 } 141 142 bool 143 efi_devpath_match_node(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2) 144 { 145 size_t len; 146 147 if (devpath1 == NULL || devpath2 == NULL) 148 return (false); 149 if (DevicePathType(devpath1) != DevicePathType(devpath2) || 150 DevicePathSubType(devpath1) != DevicePathSubType(devpath2)) 151 return (false); 152 len = DevicePathNodeLength(devpath1); 153 if (len != DevicePathNodeLength(devpath2)) 154 return (false); 155 if (memcmp(devpath1, devpath2, len) != 0) 156 return (false); 157 return (true); 158 } 159 160 bool 161 efi_devpath_match(EFI_DEVICE_PATH *devpath1, EFI_DEVICE_PATH *devpath2) 162 { 163 164 if (devpath1 == NULL || devpath2 == NULL) 165 return (false); 166 167 while (true) { 168 if (!efi_devpath_match_node(devpath1, devpath2)) 169 return false; 170 if (IsDevicePathEnd(devpath1)) 171 break; 172 devpath1 = NextDevicePathNode(devpath1); 173 devpath2 = NextDevicePathNode(devpath2); 174 } 175 return (true); 176 } 177 178 bool 179 efi_devpath_is_prefix(EFI_DEVICE_PATH *prefix, EFI_DEVICE_PATH *path) 180 { 181 size_t len; 182 183 if (prefix == NULL || path == NULL) 184 return (false); 185 186 while (1) { 187 if (IsDevicePathEnd(prefix)) 188 break; 189 190 if (DevicePathType(prefix) != DevicePathType(path) || 191 DevicePathSubType(prefix) != DevicePathSubType(path)) 192 return (false); 193 194 len = DevicePathNodeLength(prefix); 195 if (len != DevicePathNodeLength(path)) 196 return (false); 197 198 if (memcmp(prefix, path, len) != 0) 199 return (false); 200 201 prefix = NextDevicePathNode(prefix); 202 path = NextDevicePathNode(path); 203 } 204 return (true); 205 } 206 207 /* 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. 210 */ 211 EFI_DEVICE_PATH * 212 efi_devpath_to_media_path(EFI_DEVICE_PATH *path) 213 { 214 215 while (!IsDevicePathEnd(path)) { 216 if (DevicePathType(path) == MEDIA_DEVICE_PATH) 217 return (path); 218 path = NextDevicePathNode(path); 219 } 220 return (NULL); 221 } 222 223 UINTN 224 efi_devpath_length(EFI_DEVICE_PATH *path) 225 { 226 EFI_DEVICE_PATH *start = path; 227 228 while (!IsDevicePathEnd(path)) 229 path = NextDevicePathNode(path); 230 return ((UINTN)path - (UINTN)start) + DevicePathNodeLength(path); 231 } 232