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