Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 2 | /* |
| 3 | * EFI device path interface |
| 4 | * |
| 5 | * Copyright (c) 2017 Leif Lindholm |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 955a321 | 2025-01-16 20:26:59 +0100 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 10 | #include <efi_loader.h> |
| 11 | |
| 12 | const efi_guid_t efi_guid_device_path_utilities_protocol = |
| 13 | EFI_DEVICE_PATH_UTILITIES_PROTOCOL_GUID; |
| 14 | |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 15 | /* |
| 16 | * Get size of a device path. |
| 17 | * |
| 18 | * This function implements the GetDevicePathSize service of the device path |
| 19 | * utilities protocol. The device path length includes the end of path tag |
| 20 | * which may be an instance end. |
| 21 | * |
| 22 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 23 | * for details. |
| 24 | * |
| 25 | * @device_path device path |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 26 | * Return: size in bytes |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 27 | */ |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 28 | static efi_uintn_t EFIAPI get_device_path_size( |
| 29 | const struct efi_device_path *device_path) |
| 30 | { |
| 31 | efi_uintn_t sz = 0; |
| 32 | |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 33 | EFI_ENTRY("%pD", device_path); |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 34 | /* size includes the END node: */ |
| 35 | if (device_path) |
| 36 | sz = efi_dp_size(device_path) + sizeof(struct efi_device_path); |
| 37 | return EFI_EXIT(sz); |
| 38 | } |
| 39 | |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 40 | /* |
| 41 | * Duplicate a device path. |
| 42 | * |
| 43 | * This function implements the DuplicateDevicePath service of the device path |
| 44 | * utilities protocol. |
| 45 | * |
| 46 | * The UEFI spec does not indicate what happens to the end tag. We follow the |
| 47 | * EDK2 logic: In case the device path ends with an end of instance tag, the |
| 48 | * copy will also end with an end of instance tag. |
| 49 | * |
| 50 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 51 | * for details. |
| 52 | * |
| 53 | * @device_path device path |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 54 | * Return: copy of the device path |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 55 | */ |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 56 | static struct efi_device_path * EFIAPI duplicate_device_path( |
| 57 | const struct efi_device_path *device_path) |
| 58 | { |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 59 | EFI_ENTRY("%pD", device_path); |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 60 | return EFI_EXIT(efi_dp_dup(device_path)); |
| 61 | } |
| 62 | |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 63 | /* |
| 64 | * Append device path. |
| 65 | * |
| 66 | * This function implements the AppendDevicePath service of the device path |
| 67 | * utilities protocol. |
| 68 | * |
| 69 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 70 | * for details. |
| 71 | * |
| 72 | * @src1 1st device path |
| 73 | * @src2 2nd device path |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 74 | * Return: concatenated device path |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 75 | */ |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 76 | static struct efi_device_path * EFIAPI append_device_path( |
| 77 | const struct efi_device_path *src1, |
| 78 | const struct efi_device_path *src2) |
| 79 | { |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 80 | EFI_ENTRY("%pD, %pD", src1, src2); |
Heinrich Schuchardt | f8de009 | 2024-05-24 14:54:26 +0200 | [diff] [blame] | 81 | return EFI_EXIT(efi_dp_concat(src1, src2, 0)); |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 84 | /* |
| 85 | * Append device path node. |
| 86 | * |
| 87 | * This function implements the AppendDeviceNode service of the device path |
| 88 | * utilities protocol. |
| 89 | * |
| 90 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 91 | * for details. |
| 92 | * |
| 93 | * @device_path device path |
| 94 | * @device_node device node |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 95 | * Return: concatenated device path |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 96 | */ |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 97 | static struct efi_device_path * EFIAPI append_device_node( |
| 98 | const struct efi_device_path *device_path, |
| 99 | const struct efi_device_path *device_node) |
| 100 | { |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 101 | EFI_ENTRY("%pD, %p", device_path, device_node); |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 102 | return EFI_EXIT(efi_dp_append_node(device_path, device_node)); |
| 103 | } |
| 104 | |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 105 | /* |
| 106 | * Append device path instance. |
| 107 | * |
| 108 | * This function implements the AppendDevicePathInstance service of the device |
| 109 | * path utilities protocol. |
| 110 | * |
| 111 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 112 | * for details. |
| 113 | * |
| 114 | * @device_path 1st device path |
| 115 | * @device_path_instance 2nd device path |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 116 | * Return: concatenated device path |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 117 | */ |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 118 | static struct efi_device_path * EFIAPI append_device_path_instance( |
| 119 | const struct efi_device_path *device_path, |
| 120 | const struct efi_device_path *device_path_instance) |
| 121 | { |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 122 | EFI_ENTRY("%pD, %pD", device_path, device_path_instance); |
| 123 | return EFI_EXIT(efi_dp_append_instance(device_path, |
| 124 | device_path_instance)); |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 127 | /* |
| 128 | * Get next device path instance. |
| 129 | * |
| 130 | * This function implements the GetNextDevicePathInstance service of the device |
| 131 | * path utilities protocol. |
| 132 | * |
| 133 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 134 | * for details. |
| 135 | * |
| 136 | * @device_path_instance next device path instance |
| 137 | * @device_path_instance_size size of the device path instance |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 138 | * Return: concatenated device path |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 139 | */ |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 140 | static struct efi_device_path * EFIAPI get_next_device_path_instance( |
| 141 | struct efi_device_path **device_path_instance, |
| 142 | efi_uintn_t *device_path_instance_size) |
| 143 | { |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 144 | EFI_ENTRY("%pD, %p", device_path_instance, device_path_instance_size); |
| 145 | return EFI_EXIT(efi_dp_get_next_instance(device_path_instance, |
| 146 | device_path_instance_size)); |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 149 | /* |
| 150 | * Check if a device path contains more than one instance. |
| 151 | * |
| 152 | * This function implements the AppendDeviceNode service of the device path |
| 153 | * utilities protocol. |
| 154 | * |
| 155 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 156 | * for details. |
| 157 | * |
| 158 | * @device_path device path |
| 159 | * @device_node device node |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 160 | * Return: concatenated device path |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 161 | */ |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 162 | static bool EFIAPI is_device_path_multi_instance( |
| 163 | const struct efi_device_path *device_path) |
| 164 | { |
Heinrich Schuchardt | cb0f7ce | 2018-04-16 07:59:09 +0200 | [diff] [blame] | 165 | EFI_ENTRY("%pD", device_path); |
| 166 | return EFI_EXIT(efi_dp_is_multi_instance(device_path)); |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 167 | } |
| 168 | |
Heinrich Schuchardt | b41c8e2 | 2018-04-16 07:59:05 +0200 | [diff] [blame] | 169 | /* |
| 170 | * Create device node. |
| 171 | * |
| 172 | * This function implements the CreateDeviceNode service of the device path |
| 173 | * utilities protocol. |
| 174 | * |
| 175 | * See the Unified Extensible Firmware Interface (UEFI) specification |
| 176 | * for details. |
| 177 | * |
| 178 | * @node_type node type |
| 179 | * @node_sub_type node sub type |
| 180 | * @node_length node length |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 181 | * Return: device path node |
Heinrich Schuchardt | b41c8e2 | 2018-04-16 07:59:05 +0200 | [diff] [blame] | 182 | */ |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 183 | static struct efi_device_path * EFIAPI create_device_node( |
| 184 | uint8_t node_type, uint8_t node_sub_type, uint16_t node_length) |
| 185 | { |
| 186 | EFI_ENTRY("%u, %u, %u", node_type, node_sub_type, node_length); |
Heinrich Schuchardt | b41c8e2 | 2018-04-16 07:59:05 +0200 | [diff] [blame] | 187 | return EFI_EXIT(efi_dp_create_device_node(node_type, node_sub_type, |
| 188 | node_length)); |
Leif Lindholm | b6e6fdc | 2018-03-09 17:43:21 +0100 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | const struct efi_device_path_utilities_protocol efi_device_path_utilities = { |
| 192 | .get_device_path_size = get_device_path_size, |
| 193 | .duplicate_device_path = duplicate_device_path, |
| 194 | .append_device_path = append_device_path, |
| 195 | .append_device_node = append_device_node, |
| 196 | .append_device_path_instance = append_device_path_instance, |
| 197 | .get_next_device_path_instance = get_next_device_path_instance, |
| 198 | .is_device_path_multi_instance = is_device_path_multi_instance, |
| 199 | .create_device_node = create_device_node, |
| 200 | }; |