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