Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Defines APIs that allow an OS to interact with UEFI firmware to query |
| 4 | * information about the device. |
| 5 | * https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/ |
| 6 | * |
| 7 | * Copyright (c) 2020, Linaro Limited |
| 8 | */ |
| 9 | |
| 10 | #define LOG_CATEGORY LOGC_EFI |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | #include <efi_loader.h> |
Heinrich Schuchardt | 6f26e7c | 2021-09-09 08:50:01 +0200 | [diff] [blame] | 13 | #include <efi_variable.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 14 | #include <efi_tcg2.h> |
| 15 | #include <log.h> |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 16 | #include <malloc.h> |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 17 | #include <smbios.h> |
Pali Rohár | ba87ddf | 2021-08-02 15:18:31 +0200 | [diff] [blame] | 18 | #include <version_string.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 19 | #include <tpm-v2.h> |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 20 | #include <tpm_api.h> |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 21 | #include <u-boot/hash-checksum.h> |
Masahisa Kojima | d420d8d | 2021-11-03 11:04:09 +0900 | [diff] [blame] | 22 | #include <linux/unaligned/be_byteshift.h> |
| 23 | #include <linux/unaligned/le_byteshift.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 24 | #include <linux/unaligned/generic.h> |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 25 | #include <hexdump.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 26 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 27 | /** |
| 28 | * struct event_log_buffer - internal eventlog management structure |
| 29 | * |
| 30 | * @buffer: eventlog buffer |
| 31 | * @final_buffer: finalevent config table buffer |
| 32 | * @pos: current position of 'buffer' |
| 33 | * @final_pos: current position of 'final_buffer' |
| 34 | * @get_event_called: true if GetEventLog has been invoked at least once |
| 35 | * @ebs_called: true if ExitBootServices has been invoked |
| 36 | * @truncated: true if the 'buffer' is truncated |
| 37 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 38 | struct event_log_buffer { |
| 39 | void *buffer; |
| 40 | void *final_buffer; |
| 41 | size_t pos; /* eventlog position */ |
| 42 | size_t final_pos; /* final events config table position */ |
| 43 | size_t last_event_size; |
| 44 | bool get_event_called; |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 45 | bool ebs_called; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 46 | bool truncated; |
| 47 | }; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 48 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 49 | static struct event_log_buffer event_log; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 50 | static bool tcg2_efi_app_invoked; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 51 | /* |
| 52 | * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset. |
| 53 | * Since the current tpm2_get_capability() response buffers starts at |
| 54 | * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate |
| 55 | * the response size and offset once for all consumers |
| 56 | */ |
| 57 | #define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \ |
| 58 | offsetof(struct tpms_capability_data, data)) |
| 59 | #define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \ |
| 60 | offsetof(struct tpms_tagged_property, value)) |
| 61 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 62 | static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID; |
| 63 | static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID; |
| 64 | |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 65 | struct variable_info { |
| 66 | const u16 *name; |
| 67 | bool accept_empty; |
Masahisa Kojima | f3e0c55 | 2021-10-26 17:27:27 +0900 | [diff] [blame] | 68 | u32 pcr_index; |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 69 | }; |
| 70 | |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 71 | static struct variable_info secure_variables[] = { |
Masahisa Kojima | f3e0c55 | 2021-10-26 17:27:27 +0900 | [diff] [blame] | 72 | {u"SecureBoot", true, 7}, |
| 73 | {u"PK", true, 7}, |
| 74 | {u"KEK", true, 7}, |
| 75 | {u"db", true, 7}, |
| 76 | {u"dbx", true, 7}, |
| 77 | {u"dbt", false, 7}, |
| 78 | {u"dbr", false, 7}, |
| 79 | {u"DeployedMode", false, 1}, |
| 80 | {u"AuditMode", false, 1}, |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 81 | }; |
| 82 | |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 83 | static bool is_tcg2_protocol_installed(void) |
| 84 | { |
| 85 | struct efi_handler *handler; |
| 86 | efi_status_t ret; |
| 87 | |
| 88 | ret = efi_search_protocol(efi_root, &efi_guid_tcg2_protocol, &handler); |
| 89 | return ret == EFI_SUCCESS; |
| 90 | } |
| 91 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 92 | /* tcg2_agile_log_append - Append an agile event to an eventlog |
| 93 | * |
| 94 | * @pcr_index: PCR index |
| 95 | * @event_type: type of event added |
| 96 | * @digest_list: list of digest algorithms to add |
| 97 | * @size: size of event |
| 98 | * @event: event to add |
| 99 | * @log: log buffer to append the event |
| 100 | * |
| 101 | * @Return: status code |
| 102 | */ |
| 103 | static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type, |
| 104 | struct tpml_digest_values *digest_list, |
| 105 | u32 size, u8 event[]) |
| 106 | { |
| 107 | void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos); |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 108 | u32 event_size = size + tcg2_event_get_size(digest_list); |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 109 | struct efi_tcg2_final_events_table *final_event; |
| 110 | efi_status_t ret = EFI_SUCCESS; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 111 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 112 | /* if ExitBootServices hasn't been called update the normal log */ |
| 113 | if (!event_log.ebs_called) { |
| 114 | if (event_log.truncated || |
| 115 | event_log.pos + event_size > TPM2_EVENT_LOG_SIZE) { |
| 116 | event_log.truncated = true; |
| 117 | return EFI_VOLUME_FULL; |
| 118 | } |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 119 | tcg2_log_append(pcr_index, event_type, digest_list, size, event, log); |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 120 | event_log.pos += event_size; |
| 121 | event_log.last_event_size = event_size; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 122 | } |
| 123 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 124 | if (!event_log.get_event_called) |
| 125 | return ret; |
| 126 | |
| 127 | /* if GetEventLog has been called update FinalEventLog as well */ |
| 128 | if (event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE) |
| 129 | return EFI_VOLUME_FULL; |
| 130 | |
| 131 | log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos); |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 132 | tcg2_log_append(pcr_index, event_type, digest_list, size, event, log); |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 133 | |
| 134 | final_event = event_log.final_buffer; |
| 135 | final_event->number_of_events++; |
| 136 | event_log.final_pos += event_size; |
| 137 | |
| 138 | return ret; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 139 | } |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 140 | |
| 141 | /** |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 142 | * tpm2_get_max_command_size() - get the supported max command size |
| 143 | * |
| 144 | * @dev: TPM device |
| 145 | * @max_command_size: output buffer for the size |
| 146 | * |
| 147 | * Return: 0 on success, -1 on error |
| 148 | */ |
| 149 | static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size) |
| 150 | { |
| 151 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 152 | u32 ret; |
| 153 | |
| 154 | memset(response, 0, sizeof(response)); |
| 155 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 156 | TPM2_PT_MAX_COMMAND_SIZE, response, 1); |
| 157 | if (ret) |
| 158 | return -1; |
| 159 | |
| 160 | *max_command_size = (uint16_t)get_unaligned_be32(response + |
| 161 | properties_offset); |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * tpm2_get_max_response_size() - get the supported max response size |
| 168 | * |
| 169 | * @dev: TPM device |
| 170 | * @max_response_size: output buffer for the size |
| 171 | * |
| 172 | * Return: 0 on success, -1 on error |
| 173 | */ |
| 174 | static int tpm2_get_max_response_size(struct udevice *dev, |
| 175 | u16 *max_response_size) |
| 176 | { |
| 177 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 178 | u32 ret; |
| 179 | |
| 180 | memset(response, 0, sizeof(response)); |
| 181 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 182 | TPM2_PT_MAX_RESPONSE_SIZE, response, 1); |
| 183 | if (ret) |
| 184 | return -1; |
| 185 | |
| 186 | *max_response_size = (uint16_t)get_unaligned_be32(response + |
| 187 | properties_offset); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * tpm2_get_manufacturer_id() - get the manufacturer ID |
| 194 | * |
| 195 | * @dev: TPM device |
| 196 | * @manufacturer_id: output buffer for the id |
| 197 | * |
| 198 | * Return: 0 on success, -1 on error |
| 199 | */ |
| 200 | static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id) |
| 201 | { |
| 202 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 203 | u32 ret; |
| 204 | |
| 205 | memset(response, 0, sizeof(response)); |
| 206 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 207 | TPM2_PT_MANUFACTURER, response, 1); |
| 208 | if (ret) |
| 209 | return -1; |
| 210 | |
| 211 | *manufacturer_id = get_unaligned_be32(response + properties_offset); |
| 212 | |
| 213 | return 0; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 217 | * efi_tcg2_get_capability() - protocol capability information and state information |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 218 | * |
| 219 | * @this: TCG2 protocol instance |
| 220 | * @capability: caller allocated memory with size field to the size of |
| 221 | * the structure allocated |
| 222 | |
| 223 | * Return: status code |
| 224 | */ |
| 225 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 226 | efi_tcg2_get_capability(struct efi_tcg2_protocol *this, |
| 227 | struct efi_tcg2_boot_service_capability *capability) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 228 | { |
| 229 | struct udevice *dev; |
| 230 | efi_status_t efi_ret; |
| 231 | int ret; |
| 232 | |
| 233 | EFI_ENTRY("%p, %p", this, capability); |
| 234 | |
| 235 | if (!this || !capability) { |
| 236 | efi_ret = EFI_INVALID_PARAMETER; |
| 237 | goto out; |
| 238 | } |
| 239 | |
Masahisa Kojima | 9cc8293 | 2021-09-06 12:04:12 +0900 | [diff] [blame] | 240 | if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) { |
| 241 | capability->size = BOOT_SERVICE_CAPABILITY_MIN; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 242 | efi_ret = EFI_BUFFER_TOO_SMALL; |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | if (capability->size < sizeof(*capability)) { |
| 247 | capability->size = sizeof(*capability); |
| 248 | efi_ret = EFI_BUFFER_TOO_SMALL; |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | capability->structure_version.major = 1; |
| 253 | capability->structure_version.minor = 1; |
| 254 | capability->protocol_version.major = 1; |
| 255 | capability->protocol_version.minor = 1; |
| 256 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 257 | ret = tcg2_platform_get_tpm2(&dev); |
| 258 | if (ret) { |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 259 | capability->supported_event_logs = 0; |
| 260 | capability->hash_algorithm_bitmap = 0; |
| 261 | capability->tpm_present_flag = false; |
| 262 | capability->max_command_size = 0; |
| 263 | capability->max_response_size = 0; |
| 264 | capability->manufacturer_id = 0; |
| 265 | capability->number_of_pcr_banks = 0; |
| 266 | capability->active_pcr_banks = 0; |
| 267 | |
| 268 | efi_ret = EFI_SUCCESS; |
| 269 | goto out; |
| 270 | } |
| 271 | |
| 272 | /* We only allow a TPMv2 device to register the EFI protocol */ |
| 273 | capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2; |
| 274 | |
| 275 | capability->tpm_present_flag = true; |
| 276 | |
| 277 | /* Supported and active PCRs */ |
| 278 | capability->hash_algorithm_bitmap = 0; |
| 279 | capability->active_pcr_banks = 0; |
| 280 | ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap, |
| 281 | &capability->active_pcr_banks, |
| 282 | &capability->number_of_pcr_banks); |
| 283 | if (ret) { |
| 284 | efi_ret = EFI_DEVICE_ERROR; |
| 285 | goto out; |
| 286 | } |
| 287 | |
| 288 | /* Max command size */ |
| 289 | ret = tpm2_get_max_command_size(dev, &capability->max_command_size); |
| 290 | if (ret) { |
| 291 | efi_ret = EFI_DEVICE_ERROR; |
| 292 | goto out; |
| 293 | } |
| 294 | |
| 295 | /* Max response size */ |
| 296 | ret = tpm2_get_max_response_size(dev, &capability->max_response_size); |
| 297 | if (ret) { |
| 298 | efi_ret = EFI_DEVICE_ERROR; |
| 299 | goto out; |
| 300 | } |
| 301 | |
| 302 | /* Manufacturer ID */ |
| 303 | ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id); |
| 304 | if (ret) { |
| 305 | efi_ret = EFI_DEVICE_ERROR; |
| 306 | goto out; |
| 307 | } |
| 308 | |
| 309 | return EFI_EXIT(EFI_SUCCESS); |
| 310 | out: |
| 311 | return EFI_EXIT(efi_ret); |
| 312 | } |
| 313 | |
| 314 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 315 | * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its |
| 316 | * last entry |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 317 | * |
| 318 | * @this: TCG2 protocol instance |
| 319 | * @log_format: type of event log format |
| 320 | * @event_log_location: pointer to the memory address of the event log |
| 321 | * @event_log_last_entry: pointer to the address of the start of the last |
| 322 | * entry in the event log in memory, if log contains |
| 323 | * more than 1 entry |
| 324 | * @event_log_truncated: set to true, if the Event Log is missing at i |
| 325 | * least one entry |
| 326 | * |
| 327 | * Return: status code |
| 328 | */ |
| 329 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 330 | efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this, |
| 331 | efi_tcg_event_log_format log_format, |
| 332 | u64 *event_log_location, u64 *event_log_last_entry, |
| 333 | bool *event_log_truncated) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 334 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 335 | efi_status_t ret = EFI_SUCCESS; |
| 336 | struct udevice *dev; |
| 337 | |
| 338 | EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location, |
| 339 | event_log_last_entry, event_log_truncated); |
| 340 | |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 341 | if (!this || !event_log_location || !event_log_last_entry || |
| 342 | !event_log_truncated) { |
| 343 | ret = EFI_INVALID_PARAMETER; |
| 344 | goto out; |
| 345 | } |
| 346 | |
| 347 | /* Only support TPMV2 */ |
| 348 | if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) { |
| 349 | ret = EFI_INVALID_PARAMETER; |
| 350 | goto out; |
| 351 | } |
| 352 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 353 | if (tcg2_platform_get_tpm2(&dev)) { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 354 | event_log_location = NULL; |
| 355 | event_log_last_entry = NULL; |
| 356 | *event_log_truncated = false; |
| 357 | ret = EFI_SUCCESS; |
| 358 | goto out; |
| 359 | } |
| 360 | *event_log_location = (uintptr_t)event_log.buffer; |
| 361 | *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos - |
| 362 | event_log.last_event_size); |
| 363 | *event_log_truncated = event_log.truncated; |
| 364 | event_log.get_event_called = true; |
| 365 | |
| 366 | out: |
| 367 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /** |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 371 | * tcg2_hash_pe_image() - calculate PE/COFF image hash |
| 372 | * |
| 373 | * @efi: pointer to the EFI binary |
| 374 | * @efi_size: size of @efi binary |
| 375 | * @digest_list: list of digest algorithms to extend |
| 376 | * |
| 377 | * Return: status code |
| 378 | */ |
| 379 | static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size, |
| 380 | struct tpml_digest_values *digest_list) |
| 381 | { |
| 382 | WIN_CERTIFICATE *wincerts = NULL; |
| 383 | size_t wincerts_len; |
| 384 | struct efi_image_regions *regs = NULL; |
| 385 | void *new_efi = NULL; |
| 386 | u8 hash[TPM2_SHA512_DIGEST_SIZE]; |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 387 | struct udevice *dev; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 388 | efi_status_t ret = EFI_SUCCESS; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 389 | u32 active; |
| 390 | int i; |
| 391 | |
| 392 | new_efi = efi_prepare_aligned_image(efi, &efi_size); |
| 393 | if (!new_efi) |
| 394 | return EFI_OUT_OF_RESOURCES; |
| 395 | |
| 396 | if (!efi_image_parse(new_efi, efi_size, ®s, &wincerts, |
| 397 | &wincerts_len)) { |
| 398 | log_err("Parsing PE executable image failed\n"); |
| 399 | ret = EFI_UNSUPPORTED; |
| 400 | goto out; |
| 401 | } |
| 402 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 403 | if (tcg2_platform_get_tpm2(&dev)) { |
| 404 | ret = EFI_DEVICE_ERROR; |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 405 | goto out; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 406 | } |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 407 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 408 | if (tcg2_get_active_pcr_banks(dev, &active)) { |
| 409 | ret = EFI_DEVICE_ERROR; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 410 | goto out; |
| 411 | } |
| 412 | |
| 413 | digest_list->count = 0; |
Tim Harvey | 6ea1e05 | 2024-05-25 13:00:48 -0700 | [diff] [blame] | 414 | for (i = 0; i < ARRAY_SIZE(hash_algo_list); i++) { |
| 415 | u16 hash_alg = hash_algo_list[i].hash_alg; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 416 | |
Tim Harvey | 6ea1e05 | 2024-05-25 13:00:48 -0700 | [diff] [blame] | 417 | if (!(active & hash_algo_list[i].hash_mask)) |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 418 | continue; |
| 419 | switch (hash_alg) { |
| 420 | case TPM2_ALG_SHA1: |
| 421 | hash_calculate("sha1", regs->reg, regs->num, hash); |
| 422 | break; |
| 423 | case TPM2_ALG_SHA256: |
| 424 | hash_calculate("sha256", regs->reg, regs->num, hash); |
| 425 | break; |
| 426 | case TPM2_ALG_SHA384: |
| 427 | hash_calculate("sha384", regs->reg, regs->num, hash); |
| 428 | break; |
| 429 | case TPM2_ALG_SHA512: |
| 430 | hash_calculate("sha512", regs->reg, regs->num, hash); |
| 431 | break; |
| 432 | default: |
Heinrich Schuchardt | 09ec9f7 | 2023-07-31 14:11:34 +0200 | [diff] [blame] | 433 | continue; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 434 | } |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 435 | digest_list->digests[digest_list->count].hash_alg = hash_alg; |
| 436 | memcpy(&digest_list->digests[digest_list->count].digest, hash, |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 437 | (u32)tpm2_algorithm_to_len(hash_alg)); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 438 | digest_list->count++; |
| 439 | } |
| 440 | |
| 441 | out: |
| 442 | if (new_efi != efi) |
| 443 | free(new_efi); |
| 444 | free(regs); |
| 445 | |
| 446 | return ret; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * tcg2_measure_pe_image() - measure PE/COFF image |
| 451 | * |
| 452 | * @efi: pointer to the EFI binary |
| 453 | * @efi_size: size of @efi binary |
| 454 | * @handle: loaded image handle |
| 455 | * @loaded_image: loaded image protocol |
| 456 | * |
| 457 | * Return: status code |
| 458 | */ |
| 459 | efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size, |
| 460 | struct efi_loaded_image_obj *handle, |
| 461 | struct efi_loaded_image *loaded_image) |
| 462 | { |
| 463 | struct tpml_digest_values digest_list; |
| 464 | efi_status_t ret; |
| 465 | struct udevice *dev; |
| 466 | u32 pcr_index, event_type, event_size; |
| 467 | struct uefi_image_load_event *image_load_event; |
| 468 | struct efi_device_path *device_path; |
| 469 | u32 device_path_length; |
| 470 | IMAGE_DOS_HEADER *dos; |
| 471 | IMAGE_NT_HEADERS32 *nt; |
| 472 | struct efi_handler *handler; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 473 | int rc; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 474 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 475 | if (!is_tcg2_protocol_installed()) |
| 476 | return EFI_SUCCESS; |
| 477 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 478 | if (tcg2_platform_get_tpm2(&dev)) |
Masahisa Kojima | 38155ea | 2021-12-07 14:15:33 +0900 | [diff] [blame] | 479 | return EFI_SECURITY_VIOLATION; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 480 | |
| 481 | switch (handle->image_type) { |
| 482 | case IMAGE_SUBSYSTEM_EFI_APPLICATION: |
| 483 | pcr_index = 4; |
| 484 | event_type = EV_EFI_BOOT_SERVICES_APPLICATION; |
| 485 | break; |
| 486 | case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: |
| 487 | pcr_index = 2; |
| 488 | event_type = EV_EFI_BOOT_SERVICES_DRIVER; |
| 489 | break; |
| 490 | case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: |
| 491 | pcr_index = 2; |
| 492 | event_type = EV_EFI_RUNTIME_SERVICES_DRIVER; |
| 493 | break; |
| 494 | default: |
| 495 | return EFI_UNSUPPORTED; |
| 496 | } |
| 497 | |
| 498 | ret = tcg2_hash_pe_image(efi, efi_size, &digest_list); |
| 499 | if (ret != EFI_SUCCESS) |
| 500 | return ret; |
| 501 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 502 | rc = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 503 | if (rc) |
| 504 | return EFI_DEVICE_ERROR; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 505 | |
Ilias Apalodimas | 26753c0 | 2021-09-09 00:30:49 +0300 | [diff] [blame] | 506 | ret = efi_search_protocol(&handle->header, |
| 507 | &efi_guid_loaded_image_device_path, &handler); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 508 | if (ret != EFI_SUCCESS) |
| 509 | return ret; |
| 510 | |
Ilias Apalodimas | 26753c0 | 2021-09-09 00:30:49 +0300 | [diff] [blame] | 511 | device_path = handler->protocol_interface; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 512 | device_path_length = efi_dp_size(device_path); |
| 513 | if (device_path_length > 0) { |
| 514 | /* add end node size */ |
| 515 | device_path_length += sizeof(struct efi_device_path); |
| 516 | } |
| 517 | event_size = sizeof(struct uefi_image_load_event) + device_path_length; |
Ilias Apalodimas | 26753c0 | 2021-09-09 00:30:49 +0300 | [diff] [blame] | 518 | image_load_event = calloc(1, event_size); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 519 | if (!image_load_event) |
| 520 | return EFI_OUT_OF_RESOURCES; |
| 521 | |
| 522 | image_load_event->image_location_in_memory = (uintptr_t)efi; |
| 523 | image_load_event->image_length_in_memory = efi_size; |
| 524 | image_load_event->length_of_device_path = device_path_length; |
| 525 | |
| 526 | dos = (IMAGE_DOS_HEADER *)efi; |
| 527 | nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew); |
| 528 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
| 529 | IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt; |
| 530 | |
| 531 | image_load_event->image_link_time_address = |
| 532 | nt64->OptionalHeader.ImageBase; |
| 533 | } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
| 534 | image_load_event->image_link_time_address = |
| 535 | nt->OptionalHeader.ImageBase; |
| 536 | } else { |
| 537 | ret = EFI_INVALID_PARAMETER; |
| 538 | goto out; |
| 539 | } |
| 540 | |
Ilias Apalodimas | 26753c0 | 2021-09-09 00:30:49 +0300 | [diff] [blame] | 541 | /* device_path_length might be zero */ |
| 542 | memcpy(image_load_event->device_path, device_path, device_path_length); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 543 | |
| 544 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 545 | event_size, (u8 *)image_load_event); |
| 546 | |
| 547 | out: |
| 548 | free(image_load_event); |
| 549 | |
| 550 | return ret; |
| 551 | } |
| 552 | |
| 553 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 554 | * efi_tcg2_hash_log_extend_event() - extend and optionally log events |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 555 | * |
| 556 | * @this: TCG2 protocol instance |
| 557 | * @flags: bitmap providing additional information on the |
| 558 | * operation |
| 559 | * @data_to_hash: physical address of the start of the data buffer |
| 560 | * to be hashed |
| 561 | * @data_to_hash_len: the length in bytes of the buffer referenced by |
| 562 | * data_to_hash |
| 563 | * @efi_tcg_event: pointer to data buffer containing information |
| 564 | * about the event |
| 565 | * |
| 566 | * Return: status code |
| 567 | */ |
| 568 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 569 | efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags, |
| 570 | u64 data_to_hash, u64 data_to_hash_len, |
| 571 | struct efi_tcg2_event *efi_tcg_event) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 572 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 573 | struct udevice *dev; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 574 | efi_status_t ret = EFI_SUCCESS; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 575 | u32 event_type, pcr_index, event_size; |
| 576 | struct tpml_digest_values digest_list; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 577 | int rc = 0; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 578 | |
| 579 | EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash, |
| 580 | data_to_hash_len, efi_tcg_event); |
| 581 | |
| 582 | if (!this || !data_to_hash || !efi_tcg_event) { |
| 583 | ret = EFI_INVALID_PARAMETER; |
| 584 | goto out; |
| 585 | } |
| 586 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 587 | if (tcg2_platform_get_tpm2(&dev)) { |
| 588 | ret = EFI_DEVICE_ERROR; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 589 | goto out; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 590 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 591 | |
| 592 | if (efi_tcg_event->size < efi_tcg_event->header.header_size + |
| 593 | sizeof(u32)) { |
| 594 | ret = EFI_INVALID_PARAMETER; |
| 595 | goto out; |
| 596 | } |
| 597 | |
Masahisa Kojima | b807491 | 2021-09-03 10:55:52 +0900 | [diff] [blame] | 598 | if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 599 | ret = EFI_INVALID_PARAMETER; |
| 600 | goto out; |
| 601 | } |
| 602 | |
| 603 | /* |
| 604 | * if PE_COFF_IMAGE is set we need to make sure the image is not |
| 605 | * corrupted, verify it and hash the PE/COFF image in accordance with |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 606 | * the procedure specified in "Calculating the PE Image Hash" |
| 607 | * section of the "Windows Authenticode Portable Executable Signature |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 608 | * Format" |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 609 | */ |
| 610 | if (flags & PE_COFF_IMAGE) { |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 611 | IMAGE_NT_HEADERS32 *nt; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 612 | |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 613 | ret = efi_check_pe((void *)(uintptr_t)data_to_hash, |
| 614 | data_to_hash_len, (void **)&nt); |
| 615 | if (ret != EFI_SUCCESS) { |
| 616 | log_err("Not a valid PE-COFF file\n"); |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 617 | ret = EFI_UNSUPPORTED; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 618 | goto out; |
| 619 | } |
| 620 | ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash, |
| 621 | data_to_hash_len, &digest_list); |
| 622 | } else { |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 623 | rc = tcg2_create_digest(dev, (u8 *)(uintptr_t)data_to_hash, |
| 624 | data_to_hash_len, &digest_list); |
| 625 | if (rc) |
| 626 | ret = EFI_DEVICE_ERROR; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 627 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 628 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 629 | if (ret != EFI_SUCCESS) |
| 630 | goto out; |
| 631 | |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 632 | pcr_index = efi_tcg_event->header.pcr_index; |
| 633 | event_type = efi_tcg_event->header.event_type; |
| 634 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 635 | rc = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 636 | if (rc) { |
| 637 | ret = EFI_DEVICE_ERROR; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 638 | goto out; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 639 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 640 | |
| 641 | if (flags & EFI_TCG2_EXTEND_ONLY) { |
| 642 | if (event_log.truncated) |
| 643 | ret = EFI_VOLUME_FULL; |
| 644 | goto out; |
| 645 | } |
| 646 | |
| 647 | /* |
| 648 | * The efi_tcg_event size includes the size component and the |
| 649 | * headersize |
| 650 | */ |
| 651 | event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) - |
| 652 | efi_tcg_event->header.header_size; |
| 653 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 654 | event_size, efi_tcg_event->event); |
| 655 | out: |
| 656 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 660 | * efi_tcg2_submit_command() - Send command to the TPM |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 661 | * |
| 662 | * @this: TCG2 protocol instance |
| 663 | * @input_param_block_size: size of the TPM input parameter block |
| 664 | * @input_param_block: pointer to the TPM input parameter block |
| 665 | * @output_param_block_size: size of the TPM output parameter block |
| 666 | * @output_param_block: pointer to the TPM output parameter block |
| 667 | * |
| 668 | * Return: status code |
| 669 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 670 | static efi_status_t EFIAPI |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 671 | efi_tcg2_submit_command(struct efi_tcg2_protocol *this, |
| 672 | u32 input_param_block_size, |
| 673 | u8 *input_param_block, |
| 674 | u32 output_param_block_size, |
| 675 | u8 *output_param_block) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 676 | { |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 677 | struct udevice *dev; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 678 | efi_status_t ret = EFI_SUCCESS; |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 679 | u32 rc; |
| 680 | size_t resp_buf_size = output_param_block_size; |
| 681 | |
| 682 | EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size, |
| 683 | input_param_block, output_param_block_size, output_param_block); |
| 684 | |
| 685 | if (!this || !input_param_block || !input_param_block_size) { |
| 686 | ret = EFI_INVALID_PARAMETER; |
| 687 | goto out; |
| 688 | } |
| 689 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 690 | if (tcg2_platform_get_tpm2(&dev)) { |
| 691 | ret = EFI_DEVICE_ERROR; |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 692 | goto out; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 693 | } |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 694 | |
| 695 | rc = tpm2_submit_command(dev, input_param_block, |
| 696 | output_param_block, &resp_buf_size); |
| 697 | if (rc) { |
| 698 | ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR; |
| 699 | |
| 700 | goto out; |
| 701 | } |
| 702 | |
| 703 | out: |
| 704 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 708 | * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 709 | * |
| 710 | * @this: TCG2 protocol instance |
| 711 | * @active_pcr_banks: pointer for receiving the bitmap of currently |
| 712 | * active PCR banks |
| 713 | * |
| 714 | * Return: status code |
| 715 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 716 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 717 | efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this, |
| 718 | u32 *active_pcr_banks) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 719 | { |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 720 | struct udevice *dev; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 721 | efi_status_t ret = EFI_INVALID_PARAMETER; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 722 | |
Ilias Apalodimas | 918a6ea | 2023-10-24 10:43:53 -0500 | [diff] [blame] | 723 | EFI_ENTRY("%p, %p", this, active_pcr_banks); |
| 724 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 725 | if (!this || !active_pcr_banks) |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 726 | goto out; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 727 | |
| 728 | if (tcg2_platform_get_tpm2(&dev)) |
| 729 | goto out; |
| 730 | |
| 731 | /* |
| 732 | * EFI_INVALID_PARAMETER does not convey the problem type. |
| 733 | * but that's what currently the spec specifies. |
| 734 | * EFI_DEVICE_ERROR would be better |
| 735 | */ |
| 736 | if (tcg2_get_active_pcr_banks(dev, active_pcr_banks)) |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 737 | goto out; |
| 738 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 739 | ret = EFI_SUCCESS; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 740 | |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 741 | out: |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 742 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 746 | * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 747 | * |
| 748 | * @this: TCG2 protocol instance |
| 749 | * @active_pcr_banks: bitmap of the requested active PCR banks |
| 750 | * |
| 751 | * Return: status code |
| 752 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 753 | static efi_status_t EFIAPI |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 754 | efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this, |
| 755 | u32 __maybe_unused active_pcr_banks) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 756 | { |
| 757 | return EFI_UNSUPPORTED; |
| 758 | } |
| 759 | |
| 760 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 761 | * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous |
| 762 | * set_active_pcr_banks() |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 763 | * |
| 764 | * @this: TCG2 protocol instance |
| 765 | * @operation_present: non-zero value to indicate a |
| 766 | * set_active_pcr_banks operation was |
| 767 | * invoked during last boot |
| 768 | * @response: result value could be returned |
| 769 | * |
| 770 | * Return: status code |
| 771 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 772 | static efi_status_t EFIAPI |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 773 | efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this, |
| 774 | u32 __maybe_unused *operation_present, |
| 775 | u32 __maybe_unused *response) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 776 | { |
| 777 | return EFI_UNSUPPORTED; |
| 778 | } |
| 779 | |
| 780 | static const struct efi_tcg2_protocol efi_tcg2_protocol = { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 781 | .get_capability = efi_tcg2_get_capability, |
| 782 | .get_eventlog = efi_tcg2_get_eventlog, |
| 783 | .hash_log_extend_event = efi_tcg2_hash_log_extend_event, |
| 784 | .submit_command = efi_tcg2_submit_command, |
| 785 | .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks, |
| 786 | .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks, |
| 787 | .get_result_of_set_active_pcr_banks = efi_tcg2_get_result_of_set_active_pcr_banks, |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 788 | }; |
| 789 | |
| 790 | /** |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 791 | * tcg2_uninit - remove the final event table and free efi memory on failures |
| 792 | */ |
| 793 | void tcg2_uninit(void) |
| 794 | { |
| 795 | efi_status_t ret; |
| 796 | |
| 797 | ret = efi_install_configuration_table(&efi_guid_final_events, NULL); |
| 798 | if (ret != EFI_SUCCESS) |
| 799 | log_err("Failed to delete final events config table\n"); |
| 800 | |
| 801 | efi_free_pool(event_log.buffer); |
| 802 | event_log.buffer = NULL; |
| 803 | efi_free_pool(event_log.final_buffer); |
| 804 | event_log.final_buffer = NULL; |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 805 | |
| 806 | if (!is_tcg2_protocol_installed()) |
| 807 | return; |
| 808 | |
Ilias Apalodimas | 4953c99 | 2023-06-19 14:14:02 +0300 | [diff] [blame] | 809 | ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol, |
| 810 | &efi_tcg2_protocol, NULL); |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 811 | if (ret != EFI_SUCCESS) |
| 812 | log_err("Failed to remove EFI TCG2 protocol\n"); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 816 | * create_final_event() - Create the final event and install the config |
| 817 | * defined by the TCG EFI spec |
| 818 | */ |
| 819 | static efi_status_t create_final_event(void) |
| 820 | { |
| 821 | struct efi_tcg2_final_events_table *final_event; |
| 822 | efi_status_t ret; |
| 823 | |
| 824 | /* |
| 825 | * All events generated after the invocation of |
| 826 | * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an |
| 827 | * EFI_CONFIGURATION_TABLE |
| 828 | */ |
| 829 | ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE, |
| 830 | &event_log.final_buffer); |
| 831 | if (ret != EFI_SUCCESS) |
| 832 | goto out; |
| 833 | |
| 834 | memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE); |
| 835 | final_event = event_log.final_buffer; |
| 836 | final_event->number_of_events = 0; |
| 837 | final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION; |
| 838 | event_log.final_pos = sizeof(*final_event); |
| 839 | ret = efi_install_configuration_table(&efi_guid_final_events, |
| 840 | final_event); |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 841 | if (ret != EFI_SUCCESS) { |
| 842 | efi_free_pool(event_log.final_buffer); |
| 843 | event_log.final_buffer = NULL; |
| 844 | } |
| 845 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 846 | out: |
| 847 | return ret; |
| 848 | } |
| 849 | |
| 850 | /** |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 851 | * measure_event() - common function to add event log and extend PCR |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 852 | * |
| 853 | * @dev: TPM device |
| 854 | * @pcr_index: PCR index |
| 855 | * @event_type: type of event added |
| 856 | * @size: event size |
| 857 | * @event: event data |
| 858 | * |
| 859 | * Return: status code |
| 860 | */ |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 861 | static efi_status_t measure_event(struct udevice *dev, u32 pcr_index, |
| 862 | u32 event_type, u32 size, u8 event[]) |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 863 | { |
| 864 | struct tpml_digest_values digest_list; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 865 | efi_status_t ret = EFI_DEVICE_ERROR; |
| 866 | int rc; |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 867 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 868 | rc = tcg2_create_digest(dev, event, size, &digest_list); |
| 869 | if (rc) |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 870 | goto out; |
| 871 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 872 | rc = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 873 | if (rc) |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 874 | goto out; |
| 875 | |
| 876 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 877 | size, event); |
| 878 | |
| 879 | out: |
| 880 | return ret; |
| 881 | } |
| 882 | |
| 883 | /** |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 884 | * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the |
| 885 | * eventlog and extend the PCRs |
| 886 | * |
| 887 | * @dev: TPM device |
| 888 | * |
| 889 | * @Return: status code |
| 890 | */ |
| 891 | static efi_status_t efi_append_scrtm_version(struct udevice *dev) |
| 892 | { |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 893 | efi_status_t ret; |
| 894 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 895 | ret = measure_event(dev, 0, EV_S_CRTM_VERSION, |
| 896 | strlen(version_string) + 1, (u8 *)version_string); |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 897 | |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 898 | return ret; |
| 899 | } |
| 900 | |
| 901 | /** |
| 902 | * efi_init_event_log() - initialize an eventlog |
| 903 | * |
| 904 | * Return: status code |
| 905 | */ |
| 906 | static efi_status_t efi_init_event_log(void) |
| 907 | { |
| 908 | /* |
| 909 | * vendor_info_size is currently set to 0, we need to change the length |
| 910 | * and allocate the flexible array member if this changes |
| 911 | */ |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 912 | struct tcg2_event_log elog; |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 913 | struct udevice *dev; |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 914 | efi_status_t ret; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 915 | int rc; |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 916 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 917 | if (tcg2_platform_get_tpm2(&dev)) |
| 918 | return EFI_DEVICE_ERROR; |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 919 | |
| 920 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE, |
| 921 | (void **)&event_log.buffer); |
| 922 | if (ret != EFI_SUCCESS) |
| 923 | return ret; |
| 924 | |
| 925 | /* |
| 926 | * initialize log area as 0xff so the OS can easily figure out the |
| 927 | * last log entry |
| 928 | */ |
| 929 | memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE); |
| 930 | |
| 931 | /* |
| 932 | * The log header is defined to be in SHA1 event log entry format. |
| 933 | * Setup event header |
| 934 | */ |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 935 | event_log.pos = 0; |
| 936 | event_log.last_event_size = 0; |
| 937 | event_log.get_event_called = false; |
| 938 | event_log.ebs_called = false; |
| 939 | event_log.truncated = false; |
| 940 | |
| 941 | /* |
| 942 | * Check if earlier firmware have passed any eventlog. Different |
| 943 | * platforms can use different ways to do so. |
| 944 | */ |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 945 | elog.log = event_log.buffer; |
| 946 | elog.log_size = TPM2_EVENT_LOG_SIZE; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 947 | rc = tcg2_log_prepare_buffer(dev, &elog, false); |
| 948 | if (rc) { |
| 949 | ret = (rc == -ENOBUFS) ? EFI_BUFFER_TOO_SMALL : EFI_DEVICE_ERROR; |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 950 | goto free_pool; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 951 | } |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 952 | |
| 953 | event_log.pos = elog.log_position; |
| 954 | |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 955 | /* |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 956 | * Add SCRTM version to the log if previous firmmware |
| 957 | * doesn't pass an eventlog. |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 958 | */ |
Ilias Apalodimas | 12c15f5 | 2023-11-07 13:31:34 +0200 | [diff] [blame] | 959 | if (!elog.found) { |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 960 | ret = efi_append_scrtm_version(dev); |
Ilias Apalodimas | 12c15f5 | 2023-11-07 13:31:34 +0200 | [diff] [blame] | 961 | if (ret != EFI_SUCCESS) |
| 962 | goto free_pool; |
| 963 | } |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 964 | |
| 965 | ret = create_final_event(); |
| 966 | if (ret != EFI_SUCCESS) |
| 967 | goto free_pool; |
| 968 | |
| 969 | return ret; |
| 970 | |
| 971 | free_pool: |
| 972 | efi_free_pool(event_log.buffer); |
| 973 | event_log.buffer = NULL; |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 974 | return ret; |
| 975 | } |
| 976 | |
| 977 | /** |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 978 | * tcg2_measure_variable() - add variable event log and extend PCR |
| 979 | * |
| 980 | * @dev: TPM device |
| 981 | * @pcr_index: PCR index |
| 982 | * @event_type: type of event added |
| 983 | * @var_name: variable name |
| 984 | * @guid: guid |
| 985 | * @data_size: variable data size |
| 986 | * @data: variable data |
| 987 | * |
| 988 | * Return: status code |
| 989 | */ |
| 990 | static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index, |
Heinrich Schuchardt | 1ad2f0d | 2021-09-09 07:12:14 +0200 | [diff] [blame] | 991 | u32 event_type, const u16 *var_name, |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 992 | const efi_guid_t *guid, |
| 993 | efi_uintn_t data_size, u8 *data) |
| 994 | { |
| 995 | u32 event_size; |
| 996 | efi_status_t ret; |
| 997 | struct efi_tcg2_uefi_variable_data *event; |
| 998 | |
| 999 | event_size = sizeof(event->variable_name) + |
| 1000 | sizeof(event->unicode_name_length) + |
| 1001 | sizeof(event->variable_data_length) + |
| 1002 | (u16_strlen(var_name) * sizeof(u16)) + data_size; |
| 1003 | event = malloc(event_size); |
| 1004 | if (!event) |
| 1005 | return EFI_OUT_OF_RESOURCES; |
| 1006 | |
| 1007 | guidcpy(&event->variable_name, guid); |
| 1008 | event->unicode_name_length = u16_strlen(var_name); |
| 1009 | event->variable_data_length = data_size; |
| 1010 | memcpy(event->unicode_name, var_name, |
| 1011 | (event->unicode_name_length * sizeof(u16))); |
| 1012 | if (data) { |
| 1013 | memcpy((u16 *)event->unicode_name + event->unicode_name_length, |
| 1014 | data, data_size); |
| 1015 | } |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1016 | ret = measure_event(dev, pcr_index, event_type, event_size, |
| 1017 | (u8 *)event); |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1018 | free(event); |
| 1019 | return ret; |
| 1020 | } |
| 1021 | |
| 1022 | /** |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1023 | * tcg2_measure_boot_variable() - measure boot variables |
| 1024 | * |
| 1025 | * @dev: TPM device |
| 1026 | * |
| 1027 | * Return: status code |
| 1028 | */ |
| 1029 | static efi_status_t tcg2_measure_boot_variable(struct udevice *dev) |
| 1030 | { |
| 1031 | u16 *boot_order; |
| 1032 | u16 *boot_index; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1033 | u16 var_name[] = u"BootOrder"; |
| 1034 | u16 boot_name[] = u"Boot####"; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1035 | u8 *bootvar; |
| 1036 | efi_uintn_t var_data_size; |
| 1037 | u32 count, i; |
| 1038 | efi_status_t ret; |
| 1039 | |
| 1040 | boot_order = efi_get_var(var_name, &efi_global_variable_guid, |
| 1041 | &var_data_size); |
| 1042 | if (!boot_order) { |
Masahisa Kojima | d132593 | 2021-11-09 18:44:54 +0900 | [diff] [blame] | 1043 | /* If "BootOrder" is not defined, skip the boot variable measurement */ |
| 1044 | return EFI_SUCCESS; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name, |
| 1048 | &efi_global_variable_guid, var_data_size, |
| 1049 | (u8 *)boot_order); |
| 1050 | if (ret != EFI_SUCCESS) |
| 1051 | goto error; |
| 1052 | |
| 1053 | count = var_data_size / sizeof(*boot_order); |
| 1054 | boot_index = boot_order; |
| 1055 | for (i = 0; i < count; i++) { |
| 1056 | efi_create_indexed_name(boot_name, sizeof(boot_name), |
| 1057 | "Boot", *boot_index++); |
| 1058 | |
| 1059 | bootvar = efi_get_var(boot_name, &efi_global_variable_guid, |
| 1060 | &var_data_size); |
| 1061 | |
| 1062 | if (!bootvar) { |
Masahisa Kojima | aca20c8 | 2021-11-09 20:35:53 +0900 | [diff] [blame] | 1063 | log_debug("%ls not found\n", boot_name); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1064 | continue; |
| 1065 | } |
| 1066 | |
| 1067 | ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, |
| 1068 | boot_name, |
| 1069 | &efi_global_variable_guid, |
| 1070 | var_data_size, bootvar); |
| 1071 | free(bootvar); |
| 1072 | if (ret != EFI_SUCCESS) |
| 1073 | goto error; |
| 1074 | } |
| 1075 | |
| 1076 | error: |
| 1077 | free(boot_order); |
| 1078 | return ret; |
| 1079 | } |
| 1080 | |
| 1081 | /** |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1082 | * tcg2_measure_smbios() - measure smbios table |
| 1083 | * |
| 1084 | * @dev: TPM device |
| 1085 | * @entry: pointer to the smbios_entry structure |
| 1086 | * |
| 1087 | * Return: status code |
| 1088 | */ |
| 1089 | static efi_status_t |
| 1090 | tcg2_measure_smbios(struct udevice *dev, |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 1091 | const struct smbios3_entry *entry) |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1092 | { |
| 1093 | efi_status_t ret; |
| 1094 | struct smbios_header *smbios_copy; |
| 1095 | struct smbios_handoff_table_pointers2 *event = NULL; |
| 1096 | u32 event_size; |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 1097 | const char smbios3_anchor[] = "_SM3_"; |
| 1098 | |
| 1099 | /* We only support SMBIOS 3.0 Entry Point structure */ |
| 1100 | if (memcmp(entry->anchor, smbios3_anchor, sizeof(smbios3_anchor) - 1)) |
| 1101 | return EFI_UNSUPPORTED; |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1102 | |
| 1103 | /* |
| 1104 | * TCG PC Client PFP Spec says |
| 1105 | * "SMBIOS structures that contain static configuration information |
| 1106 | * (e.g. Platform Manufacturer Enterprise Number assigned by IANA, |
| 1107 | * platform model number, Vendor and Device IDs for each SMBIOS table) |
| 1108 | * that is relevant to the security of the platform MUST be measured". |
| 1109 | * Device dependent parameters such as serial number are cleared to |
| 1110 | * zero or spaces for the measurement. |
| 1111 | */ |
| 1112 | event_size = sizeof(struct smbios_handoff_table_pointers2) + |
| 1113 | FIELD_SIZEOF(struct efi_configuration_table, guid) + |
Heinrich Schuchardt | 68e948a | 2024-01-31 23:49:34 +0100 | [diff] [blame] | 1114 | entry->table_maximum_size; |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1115 | event = calloc(1, event_size); |
| 1116 | if (!event) { |
| 1117 | ret = EFI_OUT_OF_RESOURCES; |
| 1118 | goto out; |
| 1119 | } |
| 1120 | |
| 1121 | event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC); |
| 1122 | memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC, |
| 1123 | sizeof(SMBIOS_HANDOFF_TABLE_DESC)); |
| 1124 | put_unaligned_le64(1, &event->number_of_tables); |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 1125 | guidcpy(&event->table_entry[0].guid, &smbios3_guid); |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1126 | smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table); |
| 1127 | memcpy(&event->table_entry[0].table, |
| 1128 | (void *)((uintptr_t)entry->struct_table_address), |
Heinrich Schuchardt | 68e948a | 2024-01-31 23:49:34 +0100 | [diff] [blame] | 1129 | entry->table_maximum_size); |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1130 | |
| 1131 | smbios_prepare_measurement(entry, smbios_copy); |
| 1132 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1133 | ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size, |
| 1134 | (u8 *)event); |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1135 | if (ret != EFI_SUCCESS) |
| 1136 | goto out; |
| 1137 | |
| 1138 | out: |
| 1139 | free(event); |
| 1140 | |
| 1141 | return ret; |
| 1142 | } |
| 1143 | |
| 1144 | /** |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1145 | * tcg2_measure_gpt_table() - measure gpt table |
| 1146 | * |
| 1147 | * @dev: TPM device |
| 1148 | * @loaded_image: handle to the loaded image |
| 1149 | * |
| 1150 | * Return: status code |
| 1151 | */ |
| 1152 | static efi_status_t |
| 1153 | tcg2_measure_gpt_data(struct udevice *dev, |
| 1154 | struct efi_loaded_image_obj *loaded_image) |
| 1155 | { |
| 1156 | efi_status_t ret; |
| 1157 | efi_handle_t handle; |
Heinrich Schuchardt | 1100d15 | 2022-10-07 14:28:18 +0200 | [diff] [blame] | 1158 | struct efi_handler *dp_handler, *io_handler; |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1159 | struct efi_device_path *orig_device_path; |
| 1160 | struct efi_device_path *device_path; |
| 1161 | struct efi_device_path *dp; |
| 1162 | struct efi_block_io *block_io; |
| 1163 | struct efi_gpt_data *event = NULL; |
| 1164 | efi_guid_t null_guid = NULL_GUID; |
| 1165 | gpt_header *gpt_h; |
| 1166 | gpt_entry *entry = NULL; |
| 1167 | gpt_entry *gpt_e; |
| 1168 | u32 num_of_valid_entry = 0; |
| 1169 | u32 event_size; |
| 1170 | u32 i; |
| 1171 | u32 total_gpt_entry_size; |
| 1172 | |
| 1173 | ret = efi_search_protocol(&loaded_image->header, |
| 1174 | &efi_guid_loaded_image_device_path, |
| 1175 | &dp_handler); |
| 1176 | if (ret != EFI_SUCCESS) |
| 1177 | return ret; |
| 1178 | |
| 1179 | orig_device_path = dp_handler->protocol_interface; |
| 1180 | if (!orig_device_path) /* no device path, skip GPT measurement */ |
| 1181 | return EFI_SUCCESS; |
| 1182 | |
| 1183 | device_path = efi_dp_dup(orig_device_path); |
| 1184 | if (!device_path) |
| 1185 | return EFI_OUT_OF_RESOURCES; |
| 1186 | |
| 1187 | dp = search_gpt_dp_node(device_path); |
| 1188 | if (!dp) { |
| 1189 | /* no GPT device path node found, skip GPT measurement */ |
| 1190 | ret = EFI_SUCCESS; |
| 1191 | goto out1; |
| 1192 | } |
| 1193 | |
| 1194 | /* read GPT header */ |
| 1195 | dp->type = DEVICE_PATH_TYPE_END; |
| 1196 | dp->sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 1197 | dp = device_path; |
| 1198 | ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid, |
| 1199 | &dp, &handle)); |
| 1200 | if (ret != EFI_SUCCESS) |
| 1201 | goto out1; |
| 1202 | |
Heinrich Schuchardt | 1100d15 | 2022-10-07 14:28:18 +0200 | [diff] [blame] | 1203 | ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler); |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1204 | if (ret != EFI_SUCCESS) |
| 1205 | goto out1; |
Heinrich Schuchardt | 1100d15 | 2022-10-07 14:28:18 +0200 | [diff] [blame] | 1206 | block_io = io_handler->protocol_interface; |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1207 | |
| 1208 | gpt_h = memalign(block_io->media->io_align, block_io->media->block_size); |
| 1209 | if (!gpt_h) { |
| 1210 | ret = EFI_OUT_OF_RESOURCES; |
| 1211 | goto out2; |
| 1212 | } |
| 1213 | |
| 1214 | ret = block_io->read_blocks(block_io, block_io->media->media_id, 1, |
| 1215 | block_io->media->block_size, gpt_h); |
| 1216 | if (ret != EFI_SUCCESS) |
| 1217 | goto out2; |
| 1218 | |
| 1219 | /* read GPT entry */ |
| 1220 | total_gpt_entry_size = gpt_h->num_partition_entries * |
| 1221 | gpt_h->sizeof_partition_entry; |
| 1222 | entry = memalign(block_io->media->io_align, total_gpt_entry_size); |
| 1223 | if (!entry) { |
| 1224 | ret = EFI_OUT_OF_RESOURCES; |
| 1225 | goto out2; |
| 1226 | } |
| 1227 | |
| 1228 | ret = block_io->read_blocks(block_io, block_io->media->media_id, |
| 1229 | gpt_h->partition_entry_lba, |
| 1230 | total_gpt_entry_size, entry); |
| 1231 | if (ret != EFI_SUCCESS) |
| 1232 | goto out2; |
| 1233 | |
| 1234 | /* count valid GPT entry */ |
| 1235 | gpt_e = entry; |
| 1236 | for (i = 0; i < gpt_h->num_partition_entries; i++) { |
| 1237 | if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) |
| 1238 | num_of_valid_entry++; |
| 1239 | |
| 1240 | gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry); |
| 1241 | } |
| 1242 | |
| 1243 | /* prepare event data for measurement */ |
| 1244 | event_size = sizeof(struct efi_gpt_data) + |
| 1245 | (num_of_valid_entry * gpt_h->sizeof_partition_entry); |
| 1246 | event = calloc(1, event_size); |
| 1247 | if (!event) { |
| 1248 | ret = EFI_OUT_OF_RESOURCES; |
| 1249 | goto out2; |
| 1250 | } |
| 1251 | memcpy(event, gpt_h, sizeof(gpt_header)); |
| 1252 | put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions); |
| 1253 | |
| 1254 | /* copy valid GPT entry */ |
| 1255 | gpt_e = entry; |
| 1256 | num_of_valid_entry = 0; |
| 1257 | for (i = 0; i < gpt_h->num_partition_entries; i++) { |
| 1258 | if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) { |
| 1259 | memcpy((u8 *)event->partitions + |
| 1260 | (num_of_valid_entry * gpt_h->sizeof_partition_entry), |
| 1261 | gpt_e, gpt_h->sizeof_partition_entry); |
| 1262 | num_of_valid_entry++; |
| 1263 | } |
| 1264 | |
| 1265 | gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry); |
| 1266 | } |
| 1267 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1268 | ret = measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event); |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1269 | |
| 1270 | out2: |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1271 | free(gpt_h); |
| 1272 | free(entry); |
| 1273 | free(event); |
| 1274 | out1: |
| 1275 | efi_free_pool(device_path); |
| 1276 | |
| 1277 | return ret; |
| 1278 | } |
| 1279 | |
Etienne Carriere | b906435 | 2023-02-16 17:29:48 +0100 | [diff] [blame] | 1280 | /* Return the byte size of reserved map area in DTB or -1 upon error */ |
| 1281 | static ssize_t size_of_rsvmap(void *dtb) |
| 1282 | { |
| 1283 | struct fdt_reserve_entry e; |
| 1284 | ssize_t size_max; |
| 1285 | ssize_t size; |
| 1286 | u8 *rsvmap_base; |
| 1287 | |
| 1288 | rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb); |
| 1289 | size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb); |
| 1290 | size = 0; |
| 1291 | |
| 1292 | do { |
| 1293 | memcpy(&e, rsvmap_base + size, sizeof(e)); |
| 1294 | size += sizeof(e); |
| 1295 | if (size > size_max) |
| 1296 | return -1; |
| 1297 | } while (e.size); |
| 1298 | |
| 1299 | return size; |
| 1300 | } |
| 1301 | |
| 1302 | /** |
| 1303 | * efi_tcg2_measure_dtb() - measure DTB passed to the OS |
| 1304 | * |
| 1305 | * @dtb: pointer to the device tree blob |
| 1306 | * |
| 1307 | * Return: status code |
| 1308 | */ |
| 1309 | efi_status_t efi_tcg2_measure_dtb(void *dtb) |
| 1310 | { |
| 1311 | struct uefi_platform_firmware_blob2 *blob; |
| 1312 | struct fdt_header *header; |
| 1313 | sha256_context hash_ctx; |
| 1314 | struct udevice *dev; |
| 1315 | ssize_t rsvmap_size; |
| 1316 | efi_status_t ret; |
| 1317 | u32 event_size; |
| 1318 | |
| 1319 | if (!is_tcg2_protocol_installed()) |
| 1320 | return EFI_SUCCESS; |
| 1321 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 1322 | if (tcg2_platform_get_tpm2(&dev)) |
Etienne Carriere | b906435 | 2023-02-16 17:29:48 +0100 | [diff] [blame] | 1323 | return EFI_SECURITY_VIOLATION; |
| 1324 | |
| 1325 | rsvmap_size = size_of_rsvmap(dtb); |
| 1326 | if (rsvmap_size < 0) |
| 1327 | return EFI_SECURITY_VIOLATION; |
| 1328 | |
| 1329 | event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN; |
| 1330 | blob = calloc(1, event_size); |
| 1331 | if (!blob) |
| 1332 | return EFI_OUT_OF_RESOURCES; |
| 1333 | |
| 1334 | blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING); |
| 1335 | memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size); |
| 1336 | |
| 1337 | /* Measure populated areas of the DTB */ |
| 1338 | header = dtb; |
| 1339 | sha256_starts(&hash_ctx); |
| 1340 | sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header)); |
| 1341 | sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb)); |
| 1342 | sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb)); |
| 1343 | sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size); |
| 1344 | sha256_finish(&hash_ctx, blob->data + blob->blob_description_size); |
| 1345 | |
Ilias Apalodimas | c90c195 | 2024-06-14 15:09:50 +0300 | [diff] [blame] | 1346 | ret = measure_event(dev, 1, EV_POST_CODE, event_size, (u8 *)blob); |
Etienne Carriere | b906435 | 2023-02-16 17:29:48 +0100 | [diff] [blame] | 1347 | |
| 1348 | free(blob); |
| 1349 | return ret; |
| 1350 | } |
| 1351 | |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1352 | /** |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1353 | * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation |
| 1354 | * |
| 1355 | * Return: status code |
| 1356 | */ |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1357 | efi_status_t efi_tcg2_measure_efi_app_invocation(struct efi_loaded_image_obj *handle) |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1358 | { |
| 1359 | efi_status_t ret; |
| 1360 | u32 pcr_index; |
| 1361 | struct udevice *dev; |
| 1362 | u32 event = 0; |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 1363 | struct smbios3_entry *entry; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1364 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 1365 | if (!is_tcg2_protocol_installed()) |
| 1366 | return EFI_SUCCESS; |
| 1367 | |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1368 | if (tcg2_efi_app_invoked) |
| 1369 | return EFI_SUCCESS; |
| 1370 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 1371 | if (tcg2_platform_get_tpm2(&dev)) |
Masahisa Kojima | 38155ea | 2021-12-07 14:15:33 +0900 | [diff] [blame] | 1372 | return EFI_SECURITY_VIOLATION; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1373 | |
| 1374 | ret = tcg2_measure_boot_variable(dev); |
| 1375 | if (ret != EFI_SUCCESS) |
| 1376 | goto out; |
| 1377 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1378 | ret = measure_event(dev, 4, EV_EFI_ACTION, |
| 1379 | strlen(EFI_CALLING_EFI_APPLICATION), |
| 1380 | (u8 *)EFI_CALLING_EFI_APPLICATION); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1381 | if (ret != EFI_SUCCESS) |
| 1382 | goto out; |
| 1383 | |
Heinrich Schuchardt | 10899c8 | 2024-01-26 09:13:22 +0100 | [diff] [blame] | 1384 | entry = efi_get_configuration_table(&smbios3_guid); |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1385 | if (entry) { |
| 1386 | ret = tcg2_measure_smbios(dev, entry); |
| 1387 | if (ret != EFI_SUCCESS) |
| 1388 | goto out; |
| 1389 | } |
| 1390 | |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1391 | ret = tcg2_measure_gpt_data(dev, handle); |
| 1392 | if (ret != EFI_SUCCESS) |
| 1393 | goto out; |
| 1394 | |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1395 | for (pcr_index = 0; pcr_index <= 7; pcr_index++) { |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1396 | ret = measure_event(dev, pcr_index, EV_SEPARATOR, |
| 1397 | sizeof(event), (u8 *)&event); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1398 | if (ret != EFI_SUCCESS) |
| 1399 | goto out; |
| 1400 | } |
| 1401 | |
| 1402 | tcg2_efi_app_invoked = true; |
| 1403 | out: |
| 1404 | return ret; |
| 1405 | } |
| 1406 | |
| 1407 | /** |
| 1408 | * efi_tcg2_measure_efi_app_exit() - measure efi app exit |
| 1409 | * |
| 1410 | * Return: status code |
| 1411 | */ |
| 1412 | efi_status_t efi_tcg2_measure_efi_app_exit(void) |
| 1413 | { |
| 1414 | efi_status_t ret; |
| 1415 | struct udevice *dev; |
| 1416 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 1417 | if (!is_tcg2_protocol_installed()) |
| 1418 | return EFI_SUCCESS; |
| 1419 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 1420 | if (tcg2_platform_get_tpm2(&dev)) |
| 1421 | return EFI_SECURITY_VIOLATION; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1422 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1423 | ret = measure_event(dev, 4, EV_EFI_ACTION, |
| 1424 | strlen(EFI_RETURNING_FROM_EFI_APPLICATION), |
| 1425 | (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1426 | return ret; |
| 1427 | } |
| 1428 | |
| 1429 | /** |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1430 | * efi_tcg2_notify_exit_boot_services() - ExitBootService callback |
| 1431 | * |
| 1432 | * @event: callback event |
| 1433 | * @context: callback context |
| 1434 | */ |
| 1435 | static void EFIAPI |
| 1436 | efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context) |
| 1437 | { |
| 1438 | efi_status_t ret; |
| 1439 | struct udevice *dev; |
| 1440 | |
| 1441 | EFI_ENTRY("%p, %p", event, context); |
| 1442 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 1443 | event_log.ebs_called = true; |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 1444 | |
| 1445 | if (!is_tcg2_protocol_installed()) { |
| 1446 | ret = EFI_SUCCESS; |
| 1447 | goto out; |
| 1448 | } |
| 1449 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 1450 | if (tcg2_platform_get_tpm2(&dev)) { |
| 1451 | ret = EFI_SECURITY_VIOLATION; |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1452 | goto out; |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 1453 | } |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1454 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1455 | ret = measure_event(dev, 5, EV_EFI_ACTION, |
| 1456 | strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION), |
| 1457 | (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION); |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1458 | if (ret != EFI_SUCCESS) |
| 1459 | goto out; |
| 1460 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1461 | ret = measure_event(dev, 5, EV_EFI_ACTION, |
| 1462 | strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED), |
| 1463 | (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED); |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1464 | |
| 1465 | out: |
| 1466 | EFI_EXIT(ret); |
| 1467 | } |
| 1468 | |
| 1469 | /** |
| 1470 | * efi_tcg2_notify_exit_boot_services_failed() |
| 1471 | * - notify ExitBootServices() is failed |
| 1472 | * |
| 1473 | * Return: status code |
| 1474 | */ |
| 1475 | efi_status_t efi_tcg2_notify_exit_boot_services_failed(void) |
| 1476 | { |
| 1477 | struct udevice *dev; |
| 1478 | efi_status_t ret; |
| 1479 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 1480 | if (!is_tcg2_protocol_installed()) |
| 1481 | return EFI_SUCCESS; |
| 1482 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 1483 | if (tcg2_platform_get_tpm2(&dev)) |
| 1484 | return EFI_SECURITY_VIOLATION; |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1485 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1486 | ret = measure_event(dev, 5, EV_EFI_ACTION, |
| 1487 | strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION), |
| 1488 | (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION); |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1489 | if (ret != EFI_SUCCESS) |
| 1490 | goto out; |
| 1491 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1492 | ret = measure_event(dev, 5, EV_EFI_ACTION, |
| 1493 | strlen(EFI_EXIT_BOOT_SERVICES_FAILED), |
| 1494 | (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED); |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1495 | |
| 1496 | out: |
| 1497 | return ret; |
| 1498 | } |
| 1499 | |
| 1500 | /** |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1501 | * tcg2_measure_secure_boot_variable() - measure secure boot variables |
| 1502 | * |
| 1503 | * @dev: TPM device |
| 1504 | * |
| 1505 | * Return: status code |
| 1506 | */ |
| 1507 | static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev) |
| 1508 | { |
| 1509 | u8 *data; |
| 1510 | efi_uintn_t data_size; |
| 1511 | u32 count, i; |
| 1512 | efi_status_t ret; |
Masahisa Kojima | f3e0c55 | 2021-10-26 17:27:27 +0900 | [diff] [blame] | 1513 | u8 deployed_mode; |
| 1514 | efi_uintn_t size; |
| 1515 | u32 deployed_audit_pcr_index = 1; |
| 1516 | |
| 1517 | size = sizeof(deployed_mode); |
| 1518 | ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid, |
| 1519 | NULL, &size, &deployed_mode, NULL); |
| 1520 | if (ret != EFI_SUCCESS || !deployed_mode) |
| 1521 | deployed_audit_pcr_index = 7; |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1522 | |
| 1523 | count = ARRAY_SIZE(secure_variables); |
| 1524 | for (i = 0; i < count; i++) { |
Heinrich Schuchardt | 6f26e7c | 2021-09-09 08:50:01 +0200 | [diff] [blame] | 1525 | const efi_guid_t *guid; |
| 1526 | |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 1527 | guid = efi_auth_var_get_guid(secure_variables[i].name); |
Heinrich Schuchardt | 6f26e7c | 2021-09-09 08:50:01 +0200 | [diff] [blame] | 1528 | |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 1529 | data = efi_get_var(secure_variables[i].name, guid, &data_size); |
| 1530 | if (!data && !secure_variables[i].accept_empty) |
| 1531 | continue; |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1532 | |
Masahisa Kojima | f3e0c55 | 2021-10-26 17:27:27 +0900 | [diff] [blame] | 1533 | if (u16_strcmp(u"DeployedMode", secure_variables[i].name)) |
| 1534 | secure_variables[i].pcr_index = deployed_audit_pcr_index; |
| 1535 | if (u16_strcmp(u"AuditMode", secure_variables[i].name)) |
| 1536 | secure_variables[i].pcr_index = deployed_audit_pcr_index; |
| 1537 | |
| 1538 | ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index, |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1539 | EV_EFI_VARIABLE_DRIVER_CONFIG, |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 1540 | secure_variables[i].name, guid, |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1541 | data_size, data); |
| 1542 | free(data); |
| 1543 | if (ret != EFI_SUCCESS) |
| 1544 | goto error; |
| 1545 | } |
| 1546 | |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1547 | error: |
| 1548 | return ret; |
| 1549 | } |
| 1550 | |
| 1551 | /** |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1552 | * efi_tcg2_do_initial_measurement() - do initial measurement |
| 1553 | * |
| 1554 | * Return: status code |
| 1555 | */ |
| 1556 | efi_status_t efi_tcg2_do_initial_measurement(void) |
| 1557 | { |
| 1558 | efi_status_t ret; |
| 1559 | struct udevice *dev; |
| 1560 | |
| 1561 | if (!is_tcg2_protocol_installed()) |
| 1562 | return EFI_SUCCESS; |
| 1563 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 1564 | if (tcg2_platform_get_tpm2(&dev)) |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1565 | return EFI_SECURITY_VIOLATION; |
| 1566 | |
| 1567 | ret = tcg2_measure_secure_boot_variable(dev); |
| 1568 | if (ret != EFI_SUCCESS) |
| 1569 | goto out; |
| 1570 | |
| 1571 | out: |
| 1572 | return ret; |
| 1573 | } |
| 1574 | |
| 1575 | /** |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1576 | * efi_tcg2_register() - register EFI_TCG2_PROTOCOL |
| 1577 | * |
| 1578 | * If a TPM2 device is available, the TPM TCG2 Protocol is registered |
| 1579 | * |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1580 | * Return: status code |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1581 | */ |
| 1582 | efi_status_t efi_tcg2_register(void) |
| 1583 | { |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1584 | efi_status_t ret = EFI_SUCCESS; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1585 | struct udevice *dev; |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1586 | struct efi_event *event; |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 1587 | u32 err; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1588 | |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 1589 | if (tcg2_platform_get_tpm2(&dev)) { |
Ilias Apalodimas | eb1b6b4 | 2023-01-19 16:29:15 +0200 | [diff] [blame] | 1590 | log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n"); |
Ilias Apalodimas | fa5217d | 2021-05-10 21:19:14 +0300 | [diff] [blame] | 1591 | return EFI_SUCCESS; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1592 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1593 | |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 1594 | /* initialize the TPM as early as possible. */ |
Ilias Apalodimas | 0c95d22 | 2023-01-25 13:06:03 +0200 | [diff] [blame] | 1595 | err = tpm_auto_start(dev); |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 1596 | if (err) { |
Ilias Apalodimas | fa3ab34 | 2024-06-22 17:35:38 +0300 | [diff] [blame] | 1597 | ret = EFI_DEVICE_ERROR; |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 1598 | log_err("TPM startup failed\n"); |
| 1599 | goto fail; |
| 1600 | } |
| 1601 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1602 | ret = efi_init_event_log(); |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1603 | if (ret != EFI_SUCCESS) { |
| 1604 | tcg2_uninit(); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1605 | goto fail; |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1606 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1607 | |
Ilias Apalodimas | 4953c99 | 2023-06-19 14:14:02 +0300 | [diff] [blame] | 1608 | ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol, |
| 1609 | &efi_tcg2_protocol, NULL); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1610 | if (ret != EFI_SUCCESS) { |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1611 | tcg2_uninit(); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1612 | goto fail; |
| 1613 | } |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1614 | |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1615 | ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK, |
| 1616 | efi_tcg2_notify_exit_boot_services, NULL, |
| 1617 | NULL, &event); |
| 1618 | if (ret != EFI_SUCCESS) { |
| 1619 | tcg2_uninit(); |
| 1620 | goto fail; |
| 1621 | } |
| 1622 | |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1623 | return ret; |
Ilias Apalodimas | fa5217d | 2021-05-10 21:19:14 +0300 | [diff] [blame] | 1624 | |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1625 | fail: |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1626 | log_err("Cannot install EFI_TCG2_PROTOCOL\n"); |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1627 | return ret; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1628 | } |