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 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 257 | efi_ret = tcg2_platform_get_tpm2(&dev); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 258 | if (efi_ret != EFI_SUCCESS) { |
| 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 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 353 | ret = tcg2_platform_get_tpm2(&dev); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 354 | if (ret != EFI_SUCCESS) { |
| 355 | event_log_location = NULL; |
| 356 | event_log_last_entry = NULL; |
| 357 | *event_log_truncated = false; |
| 358 | ret = EFI_SUCCESS; |
| 359 | goto out; |
| 360 | } |
| 361 | *event_log_location = (uintptr_t)event_log.buffer; |
| 362 | *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos - |
| 363 | event_log.last_event_size); |
| 364 | *event_log_truncated = event_log.truncated; |
| 365 | event_log.get_event_called = true; |
| 366 | |
| 367 | out: |
| 368 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 372 | * tcg2_hash_pe_image() - calculate PE/COFF image hash |
| 373 | * |
| 374 | * @efi: pointer to the EFI binary |
| 375 | * @efi_size: size of @efi binary |
| 376 | * @digest_list: list of digest algorithms to extend |
| 377 | * |
| 378 | * Return: status code |
| 379 | */ |
| 380 | static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size, |
| 381 | struct tpml_digest_values *digest_list) |
| 382 | { |
| 383 | WIN_CERTIFICATE *wincerts = NULL; |
| 384 | size_t wincerts_len; |
| 385 | struct efi_image_regions *regs = NULL; |
| 386 | void *new_efi = NULL; |
| 387 | u8 hash[TPM2_SHA512_DIGEST_SIZE]; |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 388 | struct udevice *dev; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 389 | efi_status_t ret; |
| 390 | u32 active; |
| 391 | int i; |
| 392 | |
| 393 | new_efi = efi_prepare_aligned_image(efi, &efi_size); |
| 394 | if (!new_efi) |
| 395 | return EFI_OUT_OF_RESOURCES; |
| 396 | |
| 397 | if (!efi_image_parse(new_efi, efi_size, ®s, &wincerts, |
| 398 | &wincerts_len)) { |
| 399 | log_err("Parsing PE executable image failed\n"); |
| 400 | ret = EFI_UNSUPPORTED; |
| 401 | goto out; |
| 402 | } |
| 403 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 404 | ret = tcg2_platform_get_tpm2(&dev); |
| 405 | if (ret != EFI_SUCCESS) |
| 406 | goto out; |
| 407 | |
| 408 | ret = tcg2_get_active_pcr_banks(dev, &active); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 409 | if (ret != EFI_SUCCESS) { |
| 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; |
| 473 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 474 | if (!is_tcg2_protocol_installed()) |
| 475 | return EFI_SUCCESS; |
| 476 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 477 | ret = tcg2_platform_get_tpm2(&dev); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 478 | if (ret != EFI_SUCCESS) |
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 | |
| 502 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 503 | if (ret != EFI_SUCCESS) |
| 504 | return ret; |
| 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; |
| 574 | efi_status_t ret; |
| 575 | u32 event_type, pcr_index, event_size; |
| 576 | struct tpml_digest_values digest_list; |
| 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 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 586 | ret = tcg2_platform_get_tpm2(&dev); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 587 | if (ret != EFI_SUCCESS) |
| 588 | goto out; |
| 589 | |
| 590 | if (efi_tcg_event->size < efi_tcg_event->header.header_size + |
| 591 | sizeof(u32)) { |
| 592 | ret = EFI_INVALID_PARAMETER; |
| 593 | goto out; |
| 594 | } |
| 595 | |
Masahisa Kojima | b807491 | 2021-09-03 10:55:52 +0900 | [diff] [blame] | 596 | if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 597 | ret = EFI_INVALID_PARAMETER; |
| 598 | goto out; |
| 599 | } |
| 600 | |
| 601 | /* |
| 602 | * if PE_COFF_IMAGE is set we need to make sure the image is not |
| 603 | * corrupted, verify it and hash the PE/COFF image in accordance with |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 604 | * the procedure specified in "Calculating the PE Image Hash" |
| 605 | * section of the "Windows Authenticode Portable Executable Signature |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 606 | * Format" |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 607 | */ |
| 608 | if (flags & PE_COFF_IMAGE) { |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 609 | IMAGE_NT_HEADERS32 *nt; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 610 | |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 611 | ret = efi_check_pe((void *)(uintptr_t)data_to_hash, |
| 612 | data_to_hash_len, (void **)&nt); |
| 613 | if (ret != EFI_SUCCESS) { |
| 614 | log_err("Not a valid PE-COFF file\n"); |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 615 | ret = EFI_UNSUPPORTED; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 616 | goto out; |
| 617 | } |
| 618 | ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash, |
| 619 | data_to_hash_len, &digest_list); |
| 620 | } else { |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 621 | ret = tcg2_create_digest(dev, (u8 *)(uintptr_t)data_to_hash, |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 622 | data_to_hash_len, &digest_list); |
| 623 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 624 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 625 | if (ret != EFI_SUCCESS) |
| 626 | goto out; |
| 627 | |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 628 | pcr_index = efi_tcg_event->header.pcr_index; |
| 629 | event_type = efi_tcg_event->header.event_type; |
| 630 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 631 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 632 | if (ret != EFI_SUCCESS) |
| 633 | goto out; |
| 634 | |
| 635 | if (flags & EFI_TCG2_EXTEND_ONLY) { |
| 636 | if (event_log.truncated) |
| 637 | ret = EFI_VOLUME_FULL; |
| 638 | goto out; |
| 639 | } |
| 640 | |
| 641 | /* |
| 642 | * The efi_tcg_event size includes the size component and the |
| 643 | * headersize |
| 644 | */ |
| 645 | event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) - |
| 646 | efi_tcg_event->header.header_size; |
| 647 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 648 | event_size, efi_tcg_event->event); |
| 649 | out: |
| 650 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 654 | * efi_tcg2_submit_command() - Send command to the TPM |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 655 | * |
| 656 | * @this: TCG2 protocol instance |
| 657 | * @input_param_block_size: size of the TPM input parameter block |
| 658 | * @input_param_block: pointer to the TPM input parameter block |
| 659 | * @output_param_block_size: size of the TPM output parameter block |
| 660 | * @output_param_block: pointer to the TPM output parameter block |
| 661 | * |
| 662 | * Return: status code |
| 663 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 664 | static efi_status_t EFIAPI |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 665 | efi_tcg2_submit_command(struct efi_tcg2_protocol *this, |
| 666 | u32 input_param_block_size, |
| 667 | u8 *input_param_block, |
| 668 | u32 output_param_block_size, |
| 669 | u8 *output_param_block) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 670 | { |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 671 | struct udevice *dev; |
| 672 | efi_status_t ret; |
| 673 | u32 rc; |
| 674 | size_t resp_buf_size = output_param_block_size; |
| 675 | |
| 676 | EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size, |
| 677 | input_param_block, output_param_block_size, output_param_block); |
| 678 | |
| 679 | if (!this || !input_param_block || !input_param_block_size) { |
| 680 | ret = EFI_INVALID_PARAMETER; |
| 681 | goto out; |
| 682 | } |
| 683 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 684 | ret = tcg2_platform_get_tpm2(&dev); |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 685 | if (ret != EFI_SUCCESS) |
| 686 | goto out; |
| 687 | |
| 688 | rc = tpm2_submit_command(dev, input_param_block, |
| 689 | output_param_block, &resp_buf_size); |
| 690 | if (rc) { |
| 691 | ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR; |
| 692 | |
| 693 | goto out; |
| 694 | } |
| 695 | |
| 696 | out: |
| 697 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 701 | * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 702 | * |
| 703 | * @this: TCG2 protocol instance |
| 704 | * @active_pcr_banks: pointer for receiving the bitmap of currently |
| 705 | * active PCR banks |
| 706 | * |
| 707 | * Return: status code |
| 708 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 709 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 710 | efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this, |
| 711 | u32 *active_pcr_banks) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 712 | { |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 713 | struct udevice *dev; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 714 | efi_status_t ret; |
| 715 | |
Ilias Apalodimas | 918a6ea | 2023-10-24 10:43:53 -0500 | [diff] [blame] | 716 | EFI_ENTRY("%p, %p", this, active_pcr_banks); |
| 717 | |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 718 | if (!this || !active_pcr_banks) { |
| 719 | ret = EFI_INVALID_PARAMETER; |
| 720 | goto out; |
| 721 | } |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 722 | ret = tcg2_platform_get_tpm2(&dev); |
| 723 | if (ret != EFI_SUCCESS) |
| 724 | goto out; |
| 725 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 726 | ret = tcg2_get_active_pcr_banks(dev, active_pcr_banks); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 727 | |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 728 | out: |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 729 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 733 | * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 734 | * |
| 735 | * @this: TCG2 protocol instance |
| 736 | * @active_pcr_banks: bitmap of the requested active PCR banks |
| 737 | * |
| 738 | * Return: status code |
| 739 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 740 | static efi_status_t EFIAPI |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 741 | efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this, |
| 742 | u32 __maybe_unused active_pcr_banks) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 743 | { |
| 744 | return EFI_UNSUPPORTED; |
| 745 | } |
| 746 | |
| 747 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 748 | * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous |
| 749 | * set_active_pcr_banks() |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 750 | * |
| 751 | * @this: TCG2 protocol instance |
| 752 | * @operation_present: non-zero value to indicate a |
| 753 | * set_active_pcr_banks operation was |
| 754 | * invoked during last boot |
| 755 | * @response: result value could be returned |
| 756 | * |
| 757 | * Return: status code |
| 758 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 759 | static efi_status_t EFIAPI |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 760 | efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this, |
| 761 | u32 __maybe_unused *operation_present, |
| 762 | u32 __maybe_unused *response) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 763 | { |
| 764 | return EFI_UNSUPPORTED; |
| 765 | } |
| 766 | |
| 767 | static const struct efi_tcg2_protocol efi_tcg2_protocol = { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 768 | .get_capability = efi_tcg2_get_capability, |
| 769 | .get_eventlog = efi_tcg2_get_eventlog, |
| 770 | .hash_log_extend_event = efi_tcg2_hash_log_extend_event, |
| 771 | .submit_command = efi_tcg2_submit_command, |
| 772 | .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks, |
| 773 | .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks, |
| 774 | .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] | 775 | }; |
| 776 | |
| 777 | /** |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 778 | * tcg2_uninit - remove the final event table and free efi memory on failures |
| 779 | */ |
| 780 | void tcg2_uninit(void) |
| 781 | { |
| 782 | efi_status_t ret; |
| 783 | |
| 784 | ret = efi_install_configuration_table(&efi_guid_final_events, NULL); |
| 785 | if (ret != EFI_SUCCESS) |
| 786 | log_err("Failed to delete final events config table\n"); |
| 787 | |
| 788 | efi_free_pool(event_log.buffer); |
| 789 | event_log.buffer = NULL; |
| 790 | efi_free_pool(event_log.final_buffer); |
| 791 | event_log.final_buffer = NULL; |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 792 | |
| 793 | if (!is_tcg2_protocol_installed()) |
| 794 | return; |
| 795 | |
Ilias Apalodimas | 4953c99 | 2023-06-19 14:14:02 +0300 | [diff] [blame] | 796 | ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol, |
| 797 | &efi_tcg2_protocol, NULL); |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 798 | if (ret != EFI_SUCCESS) |
| 799 | log_err("Failed to remove EFI TCG2 protocol\n"); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 803 | * create_final_event() - Create the final event and install the config |
| 804 | * defined by the TCG EFI spec |
| 805 | */ |
| 806 | static efi_status_t create_final_event(void) |
| 807 | { |
| 808 | struct efi_tcg2_final_events_table *final_event; |
| 809 | efi_status_t ret; |
| 810 | |
| 811 | /* |
| 812 | * All events generated after the invocation of |
| 813 | * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an |
| 814 | * EFI_CONFIGURATION_TABLE |
| 815 | */ |
| 816 | ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE, |
| 817 | &event_log.final_buffer); |
| 818 | if (ret != EFI_SUCCESS) |
| 819 | goto out; |
| 820 | |
| 821 | memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE); |
| 822 | final_event = event_log.final_buffer; |
| 823 | final_event->number_of_events = 0; |
| 824 | final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION; |
| 825 | event_log.final_pos = sizeof(*final_event); |
| 826 | ret = efi_install_configuration_table(&efi_guid_final_events, |
| 827 | final_event); |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 828 | if (ret != EFI_SUCCESS) { |
| 829 | efi_free_pool(event_log.final_buffer); |
| 830 | event_log.final_buffer = NULL; |
| 831 | } |
| 832 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 833 | out: |
| 834 | return ret; |
| 835 | } |
| 836 | |
| 837 | /** |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 838 | * measure_event() - common function to add event log and extend PCR |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 839 | * |
| 840 | * @dev: TPM device |
| 841 | * @pcr_index: PCR index |
| 842 | * @event_type: type of event added |
| 843 | * @size: event size |
| 844 | * @event: event data |
| 845 | * |
| 846 | * Return: status code |
| 847 | */ |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 848 | static efi_status_t measure_event(struct udevice *dev, u32 pcr_index, |
| 849 | u32 event_type, u32 size, u8 event[]) |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 850 | { |
| 851 | struct tpml_digest_values digest_list; |
| 852 | efi_status_t ret; |
| 853 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 854 | ret = tcg2_create_digest(dev, event, size, &digest_list); |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 855 | if (ret != EFI_SUCCESS) |
| 856 | goto out; |
| 857 | |
| 858 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 859 | if (ret != EFI_SUCCESS) |
| 860 | goto out; |
| 861 | |
| 862 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 863 | size, event); |
| 864 | |
| 865 | out: |
| 866 | return ret; |
| 867 | } |
| 868 | |
| 869 | /** |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 870 | * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the |
| 871 | * eventlog and extend the PCRs |
| 872 | * |
| 873 | * @dev: TPM device |
| 874 | * |
| 875 | * @Return: status code |
| 876 | */ |
| 877 | static efi_status_t efi_append_scrtm_version(struct udevice *dev) |
| 878 | { |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 879 | efi_status_t ret; |
| 880 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 881 | ret = measure_event(dev, 0, EV_S_CRTM_VERSION, |
| 882 | strlen(version_string) + 1, (u8 *)version_string); |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 883 | |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 884 | return ret; |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * efi_init_event_log() - initialize an eventlog |
| 889 | * |
| 890 | * Return: status code |
| 891 | */ |
| 892 | static efi_status_t efi_init_event_log(void) |
| 893 | { |
| 894 | /* |
| 895 | * vendor_info_size is currently set to 0, we need to change the length |
| 896 | * and allocate the flexible array member if this changes |
| 897 | */ |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 898 | struct tcg2_event_log elog; |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 899 | struct udevice *dev; |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 900 | efi_status_t ret; |
| 901 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 902 | ret = tcg2_platform_get_tpm2(&dev); |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 903 | if (ret != EFI_SUCCESS) |
| 904 | return ret; |
| 905 | |
| 906 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE, |
| 907 | (void **)&event_log.buffer); |
| 908 | if (ret != EFI_SUCCESS) |
| 909 | return ret; |
| 910 | |
| 911 | /* |
| 912 | * initialize log area as 0xff so the OS can easily figure out the |
| 913 | * last log entry |
| 914 | */ |
| 915 | memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE); |
| 916 | |
| 917 | /* |
| 918 | * The log header is defined to be in SHA1 event log entry format. |
| 919 | * Setup event header |
| 920 | */ |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 921 | event_log.pos = 0; |
| 922 | event_log.last_event_size = 0; |
| 923 | event_log.get_event_called = false; |
| 924 | event_log.ebs_called = false; |
| 925 | event_log.truncated = false; |
| 926 | |
| 927 | /* |
| 928 | * Check if earlier firmware have passed any eventlog. Different |
| 929 | * platforms can use different ways to do so. |
| 930 | */ |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 931 | elog.log = event_log.buffer; |
| 932 | elog.log_size = TPM2_EVENT_LOG_SIZE; |
| 933 | ret = tcg2_log_prepare_buffer(dev, &elog, false); |
| 934 | if (ret != EFI_SUCCESS) |
| 935 | goto free_pool; |
| 936 | |
| 937 | event_log.pos = elog.log_position; |
| 938 | |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 939 | /* |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 940 | * Add SCRTM version to the log if previous firmmware |
| 941 | * doesn't pass an eventlog. |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 942 | */ |
Ilias Apalodimas | 12c15f5 | 2023-11-07 13:31:34 +0200 | [diff] [blame] | 943 | if (!elog.found) { |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 944 | ret = efi_append_scrtm_version(dev); |
Ilias Apalodimas | 12c15f5 | 2023-11-07 13:31:34 +0200 | [diff] [blame] | 945 | if (ret != EFI_SUCCESS) |
| 946 | goto free_pool; |
| 947 | } |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 948 | |
| 949 | ret = create_final_event(); |
| 950 | if (ret != EFI_SUCCESS) |
| 951 | goto free_pool; |
| 952 | |
| 953 | return ret; |
| 954 | |
| 955 | free_pool: |
| 956 | efi_free_pool(event_log.buffer); |
| 957 | event_log.buffer = NULL; |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 958 | return ret; |
| 959 | } |
| 960 | |
| 961 | /** |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 962 | * tcg2_measure_variable() - add variable event log and extend PCR |
| 963 | * |
| 964 | * @dev: TPM device |
| 965 | * @pcr_index: PCR index |
| 966 | * @event_type: type of event added |
| 967 | * @var_name: variable name |
| 968 | * @guid: guid |
| 969 | * @data_size: variable data size |
| 970 | * @data: variable data |
| 971 | * |
| 972 | * Return: status code |
| 973 | */ |
| 974 | 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] | 975 | u32 event_type, const u16 *var_name, |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 976 | const efi_guid_t *guid, |
| 977 | efi_uintn_t data_size, u8 *data) |
| 978 | { |
| 979 | u32 event_size; |
| 980 | efi_status_t ret; |
| 981 | struct efi_tcg2_uefi_variable_data *event; |
| 982 | |
| 983 | event_size = sizeof(event->variable_name) + |
| 984 | sizeof(event->unicode_name_length) + |
| 985 | sizeof(event->variable_data_length) + |
| 986 | (u16_strlen(var_name) * sizeof(u16)) + data_size; |
| 987 | event = malloc(event_size); |
| 988 | if (!event) |
| 989 | return EFI_OUT_OF_RESOURCES; |
| 990 | |
| 991 | guidcpy(&event->variable_name, guid); |
| 992 | event->unicode_name_length = u16_strlen(var_name); |
| 993 | event->variable_data_length = data_size; |
| 994 | memcpy(event->unicode_name, var_name, |
| 995 | (event->unicode_name_length * sizeof(u16))); |
| 996 | if (data) { |
| 997 | memcpy((u16 *)event->unicode_name + event->unicode_name_length, |
| 998 | data, data_size); |
| 999 | } |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1000 | ret = measure_event(dev, pcr_index, event_type, event_size, |
| 1001 | (u8 *)event); |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1002 | free(event); |
| 1003 | return ret; |
| 1004 | } |
| 1005 | |
| 1006 | /** |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1007 | * tcg2_measure_boot_variable() - measure boot variables |
| 1008 | * |
| 1009 | * @dev: TPM device |
| 1010 | * |
| 1011 | * Return: status code |
| 1012 | */ |
| 1013 | static efi_status_t tcg2_measure_boot_variable(struct udevice *dev) |
| 1014 | { |
| 1015 | u16 *boot_order; |
| 1016 | u16 *boot_index; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1017 | u16 var_name[] = u"BootOrder"; |
| 1018 | u16 boot_name[] = u"Boot####"; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1019 | u8 *bootvar; |
| 1020 | efi_uintn_t var_data_size; |
| 1021 | u32 count, i; |
| 1022 | efi_status_t ret; |
| 1023 | |
| 1024 | boot_order = efi_get_var(var_name, &efi_global_variable_guid, |
| 1025 | &var_data_size); |
| 1026 | if (!boot_order) { |
Masahisa Kojima | d132593 | 2021-11-09 18:44:54 +0900 | [diff] [blame] | 1027 | /* If "BootOrder" is not defined, skip the boot variable measurement */ |
| 1028 | return EFI_SUCCESS; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1029 | } |
| 1030 | |
| 1031 | ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name, |
| 1032 | &efi_global_variable_guid, var_data_size, |
| 1033 | (u8 *)boot_order); |
| 1034 | if (ret != EFI_SUCCESS) |
| 1035 | goto error; |
| 1036 | |
| 1037 | count = var_data_size / sizeof(*boot_order); |
| 1038 | boot_index = boot_order; |
| 1039 | for (i = 0; i < count; i++) { |
| 1040 | efi_create_indexed_name(boot_name, sizeof(boot_name), |
| 1041 | "Boot", *boot_index++); |
| 1042 | |
| 1043 | bootvar = efi_get_var(boot_name, &efi_global_variable_guid, |
| 1044 | &var_data_size); |
| 1045 | |
| 1046 | if (!bootvar) { |
Masahisa Kojima | aca20c8 | 2021-11-09 20:35:53 +0900 | [diff] [blame] | 1047 | log_debug("%ls not found\n", boot_name); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1048 | continue; |
| 1049 | } |
| 1050 | |
| 1051 | ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, |
| 1052 | boot_name, |
| 1053 | &efi_global_variable_guid, |
| 1054 | var_data_size, bootvar); |
| 1055 | free(bootvar); |
| 1056 | if (ret != EFI_SUCCESS) |
| 1057 | goto error; |
| 1058 | } |
| 1059 | |
| 1060 | error: |
| 1061 | free(boot_order); |
| 1062 | return ret; |
| 1063 | } |
| 1064 | |
| 1065 | /** |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1066 | * tcg2_measure_smbios() - measure smbios table |
| 1067 | * |
| 1068 | * @dev: TPM device |
| 1069 | * @entry: pointer to the smbios_entry structure |
| 1070 | * |
| 1071 | * Return: status code |
| 1072 | */ |
| 1073 | static efi_status_t |
| 1074 | tcg2_measure_smbios(struct udevice *dev, |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 1075 | const struct smbios3_entry *entry) |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1076 | { |
| 1077 | efi_status_t ret; |
| 1078 | struct smbios_header *smbios_copy; |
| 1079 | struct smbios_handoff_table_pointers2 *event = NULL; |
| 1080 | u32 event_size; |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 1081 | const char smbios3_anchor[] = "_SM3_"; |
| 1082 | |
| 1083 | /* We only support SMBIOS 3.0 Entry Point structure */ |
| 1084 | if (memcmp(entry->anchor, smbios3_anchor, sizeof(smbios3_anchor) - 1)) |
| 1085 | return EFI_UNSUPPORTED; |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1086 | |
| 1087 | /* |
| 1088 | * TCG PC Client PFP Spec says |
| 1089 | * "SMBIOS structures that contain static configuration information |
| 1090 | * (e.g. Platform Manufacturer Enterprise Number assigned by IANA, |
| 1091 | * platform model number, Vendor and Device IDs for each SMBIOS table) |
| 1092 | * that is relevant to the security of the platform MUST be measured". |
| 1093 | * Device dependent parameters such as serial number are cleared to |
| 1094 | * zero or spaces for the measurement. |
| 1095 | */ |
| 1096 | event_size = sizeof(struct smbios_handoff_table_pointers2) + |
| 1097 | FIELD_SIZEOF(struct efi_configuration_table, guid) + |
Heinrich Schuchardt | 68e948a | 2024-01-31 23:49:34 +0100 | [diff] [blame] | 1098 | entry->table_maximum_size; |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1099 | event = calloc(1, event_size); |
| 1100 | if (!event) { |
| 1101 | ret = EFI_OUT_OF_RESOURCES; |
| 1102 | goto out; |
| 1103 | } |
| 1104 | |
| 1105 | event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC); |
| 1106 | memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC, |
| 1107 | sizeof(SMBIOS_HANDOFF_TABLE_DESC)); |
| 1108 | put_unaligned_le64(1, &event->number_of_tables); |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 1109 | guidcpy(&event->table_entry[0].guid, &smbios3_guid); |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1110 | smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table); |
| 1111 | memcpy(&event->table_entry[0].table, |
| 1112 | (void *)((uintptr_t)entry->struct_table_address), |
Heinrich Schuchardt | 68e948a | 2024-01-31 23:49:34 +0100 | [diff] [blame] | 1113 | entry->table_maximum_size); |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1114 | |
| 1115 | smbios_prepare_measurement(entry, smbios_copy); |
| 1116 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1117 | ret = measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size, |
| 1118 | (u8 *)event); |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1119 | if (ret != EFI_SUCCESS) |
| 1120 | goto out; |
| 1121 | |
| 1122 | out: |
| 1123 | free(event); |
| 1124 | |
| 1125 | return ret; |
| 1126 | } |
| 1127 | |
| 1128 | /** |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1129 | * tcg2_measure_gpt_table() - measure gpt table |
| 1130 | * |
| 1131 | * @dev: TPM device |
| 1132 | * @loaded_image: handle to the loaded image |
| 1133 | * |
| 1134 | * Return: status code |
| 1135 | */ |
| 1136 | static efi_status_t |
| 1137 | tcg2_measure_gpt_data(struct udevice *dev, |
| 1138 | struct efi_loaded_image_obj *loaded_image) |
| 1139 | { |
| 1140 | efi_status_t ret; |
| 1141 | efi_handle_t handle; |
Heinrich Schuchardt | 1100d15 | 2022-10-07 14:28:18 +0200 | [diff] [blame] | 1142 | struct efi_handler *dp_handler, *io_handler; |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1143 | struct efi_device_path *orig_device_path; |
| 1144 | struct efi_device_path *device_path; |
| 1145 | struct efi_device_path *dp; |
| 1146 | struct efi_block_io *block_io; |
| 1147 | struct efi_gpt_data *event = NULL; |
| 1148 | efi_guid_t null_guid = NULL_GUID; |
| 1149 | gpt_header *gpt_h; |
| 1150 | gpt_entry *entry = NULL; |
| 1151 | gpt_entry *gpt_e; |
| 1152 | u32 num_of_valid_entry = 0; |
| 1153 | u32 event_size; |
| 1154 | u32 i; |
| 1155 | u32 total_gpt_entry_size; |
| 1156 | |
| 1157 | ret = efi_search_protocol(&loaded_image->header, |
| 1158 | &efi_guid_loaded_image_device_path, |
| 1159 | &dp_handler); |
| 1160 | if (ret != EFI_SUCCESS) |
| 1161 | return ret; |
| 1162 | |
| 1163 | orig_device_path = dp_handler->protocol_interface; |
| 1164 | if (!orig_device_path) /* no device path, skip GPT measurement */ |
| 1165 | return EFI_SUCCESS; |
| 1166 | |
| 1167 | device_path = efi_dp_dup(orig_device_path); |
| 1168 | if (!device_path) |
| 1169 | return EFI_OUT_OF_RESOURCES; |
| 1170 | |
| 1171 | dp = search_gpt_dp_node(device_path); |
| 1172 | if (!dp) { |
| 1173 | /* no GPT device path node found, skip GPT measurement */ |
| 1174 | ret = EFI_SUCCESS; |
| 1175 | goto out1; |
| 1176 | } |
| 1177 | |
| 1178 | /* read GPT header */ |
| 1179 | dp->type = DEVICE_PATH_TYPE_END; |
| 1180 | dp->sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 1181 | dp = device_path; |
| 1182 | ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid, |
| 1183 | &dp, &handle)); |
| 1184 | if (ret != EFI_SUCCESS) |
| 1185 | goto out1; |
| 1186 | |
Heinrich Schuchardt | 1100d15 | 2022-10-07 14:28:18 +0200 | [diff] [blame] | 1187 | ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler); |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1188 | if (ret != EFI_SUCCESS) |
| 1189 | goto out1; |
Heinrich Schuchardt | 1100d15 | 2022-10-07 14:28:18 +0200 | [diff] [blame] | 1190 | block_io = io_handler->protocol_interface; |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1191 | |
| 1192 | gpt_h = memalign(block_io->media->io_align, block_io->media->block_size); |
| 1193 | if (!gpt_h) { |
| 1194 | ret = EFI_OUT_OF_RESOURCES; |
| 1195 | goto out2; |
| 1196 | } |
| 1197 | |
| 1198 | ret = block_io->read_blocks(block_io, block_io->media->media_id, 1, |
| 1199 | block_io->media->block_size, gpt_h); |
| 1200 | if (ret != EFI_SUCCESS) |
| 1201 | goto out2; |
| 1202 | |
| 1203 | /* read GPT entry */ |
| 1204 | total_gpt_entry_size = gpt_h->num_partition_entries * |
| 1205 | gpt_h->sizeof_partition_entry; |
| 1206 | entry = memalign(block_io->media->io_align, total_gpt_entry_size); |
| 1207 | if (!entry) { |
| 1208 | ret = EFI_OUT_OF_RESOURCES; |
| 1209 | goto out2; |
| 1210 | } |
| 1211 | |
| 1212 | ret = block_io->read_blocks(block_io, block_io->media->media_id, |
| 1213 | gpt_h->partition_entry_lba, |
| 1214 | total_gpt_entry_size, entry); |
| 1215 | if (ret != EFI_SUCCESS) |
| 1216 | goto out2; |
| 1217 | |
| 1218 | /* count valid GPT entry */ |
| 1219 | gpt_e = entry; |
| 1220 | for (i = 0; i < gpt_h->num_partition_entries; i++) { |
| 1221 | if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) |
| 1222 | num_of_valid_entry++; |
| 1223 | |
| 1224 | gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry); |
| 1225 | } |
| 1226 | |
| 1227 | /* prepare event data for measurement */ |
| 1228 | event_size = sizeof(struct efi_gpt_data) + |
| 1229 | (num_of_valid_entry * gpt_h->sizeof_partition_entry); |
| 1230 | event = calloc(1, event_size); |
| 1231 | if (!event) { |
| 1232 | ret = EFI_OUT_OF_RESOURCES; |
| 1233 | goto out2; |
| 1234 | } |
| 1235 | memcpy(event, gpt_h, sizeof(gpt_header)); |
| 1236 | put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions); |
| 1237 | |
| 1238 | /* copy valid GPT entry */ |
| 1239 | gpt_e = entry; |
| 1240 | num_of_valid_entry = 0; |
| 1241 | for (i = 0; i < gpt_h->num_partition_entries; i++) { |
| 1242 | if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) { |
| 1243 | memcpy((u8 *)event->partitions + |
| 1244 | (num_of_valid_entry * gpt_h->sizeof_partition_entry), |
| 1245 | gpt_e, gpt_h->sizeof_partition_entry); |
| 1246 | num_of_valid_entry++; |
| 1247 | } |
| 1248 | |
| 1249 | gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry); |
| 1250 | } |
| 1251 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1252 | 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] | 1253 | |
| 1254 | out2: |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1255 | free(gpt_h); |
| 1256 | free(entry); |
| 1257 | free(event); |
| 1258 | out1: |
| 1259 | efi_free_pool(device_path); |
| 1260 | |
| 1261 | return ret; |
| 1262 | } |
| 1263 | |
Etienne Carriere | b906435 | 2023-02-16 17:29:48 +0100 | [diff] [blame] | 1264 | /* Return the byte size of reserved map area in DTB or -1 upon error */ |
| 1265 | static ssize_t size_of_rsvmap(void *dtb) |
| 1266 | { |
| 1267 | struct fdt_reserve_entry e; |
| 1268 | ssize_t size_max; |
| 1269 | ssize_t size; |
| 1270 | u8 *rsvmap_base; |
| 1271 | |
| 1272 | rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb); |
| 1273 | size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb); |
| 1274 | size = 0; |
| 1275 | |
| 1276 | do { |
| 1277 | memcpy(&e, rsvmap_base + size, sizeof(e)); |
| 1278 | size += sizeof(e); |
| 1279 | if (size > size_max) |
| 1280 | return -1; |
| 1281 | } while (e.size); |
| 1282 | |
| 1283 | return size; |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * efi_tcg2_measure_dtb() - measure DTB passed to the OS |
| 1288 | * |
| 1289 | * @dtb: pointer to the device tree blob |
| 1290 | * |
| 1291 | * Return: status code |
| 1292 | */ |
| 1293 | efi_status_t efi_tcg2_measure_dtb(void *dtb) |
| 1294 | { |
| 1295 | struct uefi_platform_firmware_blob2 *blob; |
| 1296 | struct fdt_header *header; |
| 1297 | sha256_context hash_ctx; |
| 1298 | struct udevice *dev; |
| 1299 | ssize_t rsvmap_size; |
| 1300 | efi_status_t ret; |
| 1301 | u32 event_size; |
| 1302 | |
| 1303 | if (!is_tcg2_protocol_installed()) |
| 1304 | return EFI_SUCCESS; |
| 1305 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1306 | ret = tcg2_platform_get_tpm2(&dev); |
Etienne Carriere | b906435 | 2023-02-16 17:29:48 +0100 | [diff] [blame] | 1307 | if (ret != EFI_SUCCESS) |
| 1308 | return EFI_SECURITY_VIOLATION; |
| 1309 | |
| 1310 | rsvmap_size = size_of_rsvmap(dtb); |
| 1311 | if (rsvmap_size < 0) |
| 1312 | return EFI_SECURITY_VIOLATION; |
| 1313 | |
| 1314 | event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN; |
| 1315 | blob = calloc(1, event_size); |
| 1316 | if (!blob) |
| 1317 | return EFI_OUT_OF_RESOURCES; |
| 1318 | |
| 1319 | blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING); |
| 1320 | memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size); |
| 1321 | |
| 1322 | /* Measure populated areas of the DTB */ |
| 1323 | header = dtb; |
| 1324 | sha256_starts(&hash_ctx); |
| 1325 | sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header)); |
| 1326 | sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb)); |
| 1327 | sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb)); |
| 1328 | sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size); |
| 1329 | sha256_finish(&hash_ctx, blob->data + blob->blob_description_size); |
| 1330 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1331 | ret = measure_event(dev, 0, EV_POST_CODE, event_size, (u8 *)blob); |
Etienne Carriere | b906435 | 2023-02-16 17:29:48 +0100 | [diff] [blame] | 1332 | |
| 1333 | free(blob); |
| 1334 | return ret; |
| 1335 | } |
| 1336 | |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1337 | /** |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1338 | * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation |
| 1339 | * |
| 1340 | * Return: status code |
| 1341 | */ |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1342 | 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] | 1343 | { |
| 1344 | efi_status_t ret; |
| 1345 | u32 pcr_index; |
| 1346 | struct udevice *dev; |
| 1347 | u32 event = 0; |
Masahisa Kojima | d8733f3 | 2024-01-26 09:53:42 +0900 | [diff] [blame] | 1348 | struct smbios3_entry *entry; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1349 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 1350 | if (!is_tcg2_protocol_installed()) |
| 1351 | return EFI_SUCCESS; |
| 1352 | |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1353 | if (tcg2_efi_app_invoked) |
| 1354 | return EFI_SUCCESS; |
| 1355 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1356 | ret = tcg2_platform_get_tpm2(&dev); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1357 | if (ret != EFI_SUCCESS) |
Masahisa Kojima | 38155ea | 2021-12-07 14:15:33 +0900 | [diff] [blame] | 1358 | return EFI_SECURITY_VIOLATION; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1359 | |
| 1360 | ret = tcg2_measure_boot_variable(dev); |
| 1361 | if (ret != EFI_SUCCESS) |
| 1362 | goto out; |
| 1363 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1364 | ret = measure_event(dev, 4, EV_EFI_ACTION, |
| 1365 | strlen(EFI_CALLING_EFI_APPLICATION), |
| 1366 | (u8 *)EFI_CALLING_EFI_APPLICATION); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1367 | if (ret != EFI_SUCCESS) |
| 1368 | goto out; |
| 1369 | |
Heinrich Schuchardt | 10899c8 | 2024-01-26 09:13:22 +0100 | [diff] [blame] | 1370 | entry = efi_get_configuration_table(&smbios3_guid); |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1371 | if (entry) { |
| 1372 | ret = tcg2_measure_smbios(dev, entry); |
| 1373 | if (ret != EFI_SUCCESS) |
| 1374 | goto out; |
| 1375 | } |
| 1376 | |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 1377 | ret = tcg2_measure_gpt_data(dev, handle); |
| 1378 | if (ret != EFI_SUCCESS) |
| 1379 | goto out; |
| 1380 | |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1381 | for (pcr_index = 0; pcr_index <= 7; pcr_index++) { |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1382 | ret = measure_event(dev, pcr_index, EV_SEPARATOR, |
| 1383 | sizeof(event), (u8 *)&event); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1384 | if (ret != EFI_SUCCESS) |
| 1385 | goto out; |
| 1386 | } |
| 1387 | |
| 1388 | tcg2_efi_app_invoked = true; |
| 1389 | out: |
| 1390 | return ret; |
| 1391 | } |
| 1392 | |
| 1393 | /** |
| 1394 | * efi_tcg2_measure_efi_app_exit() - measure efi app exit |
| 1395 | * |
| 1396 | * Return: status code |
| 1397 | */ |
| 1398 | efi_status_t efi_tcg2_measure_efi_app_exit(void) |
| 1399 | { |
| 1400 | efi_status_t ret; |
| 1401 | struct udevice *dev; |
| 1402 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 1403 | if (!is_tcg2_protocol_installed()) |
| 1404 | return EFI_SUCCESS; |
| 1405 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1406 | ret = tcg2_platform_get_tpm2(&dev); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1407 | if (ret != EFI_SUCCESS) |
| 1408 | return ret; |
| 1409 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1410 | ret = measure_event(dev, 4, EV_EFI_ACTION, |
| 1411 | strlen(EFI_RETURNING_FROM_EFI_APPLICATION), |
| 1412 | (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1413 | return ret; |
| 1414 | } |
| 1415 | |
| 1416 | /** |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1417 | * efi_tcg2_notify_exit_boot_services() - ExitBootService callback |
| 1418 | * |
| 1419 | * @event: callback event |
| 1420 | * @context: callback context |
| 1421 | */ |
| 1422 | static void EFIAPI |
| 1423 | efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context) |
| 1424 | { |
| 1425 | efi_status_t ret; |
| 1426 | struct udevice *dev; |
| 1427 | |
| 1428 | EFI_ENTRY("%p, %p", event, context); |
| 1429 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 1430 | event_log.ebs_called = true; |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 1431 | |
| 1432 | if (!is_tcg2_protocol_installed()) { |
| 1433 | ret = EFI_SUCCESS; |
| 1434 | goto out; |
| 1435 | } |
| 1436 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1437 | ret = tcg2_platform_get_tpm2(&dev); |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1438 | if (ret != EFI_SUCCESS) |
| 1439 | goto out; |
| 1440 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1441 | ret = measure_event(dev, 5, EV_EFI_ACTION, |
| 1442 | strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION), |
| 1443 | (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION); |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1444 | if (ret != EFI_SUCCESS) |
| 1445 | goto out; |
| 1446 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1447 | ret = measure_event(dev, 5, EV_EFI_ACTION, |
| 1448 | strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED), |
| 1449 | (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED); |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1450 | |
| 1451 | out: |
| 1452 | EFI_EXIT(ret); |
| 1453 | } |
| 1454 | |
| 1455 | /** |
| 1456 | * efi_tcg2_notify_exit_boot_services_failed() |
| 1457 | * - notify ExitBootServices() is failed |
| 1458 | * |
| 1459 | * Return: status code |
| 1460 | */ |
| 1461 | efi_status_t efi_tcg2_notify_exit_boot_services_failed(void) |
| 1462 | { |
| 1463 | struct udevice *dev; |
| 1464 | efi_status_t ret; |
| 1465 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 1466 | if (!is_tcg2_protocol_installed()) |
| 1467 | return EFI_SUCCESS; |
| 1468 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1469 | ret = tcg2_platform_get_tpm2(&dev); |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1470 | if (ret != EFI_SUCCESS) |
| 1471 | goto out; |
| 1472 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1473 | ret = measure_event(dev, 5, EV_EFI_ACTION, |
| 1474 | strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION), |
| 1475 | (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION); |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1476 | if (ret != EFI_SUCCESS) |
| 1477 | goto out; |
| 1478 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1479 | ret = measure_event(dev, 5, EV_EFI_ACTION, |
| 1480 | strlen(EFI_EXIT_BOOT_SERVICES_FAILED), |
| 1481 | (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED); |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1482 | |
| 1483 | out: |
| 1484 | return ret; |
| 1485 | } |
| 1486 | |
| 1487 | /** |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1488 | * tcg2_measure_secure_boot_variable() - measure secure boot variables |
| 1489 | * |
| 1490 | * @dev: TPM device |
| 1491 | * |
| 1492 | * Return: status code |
| 1493 | */ |
| 1494 | static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev) |
| 1495 | { |
| 1496 | u8 *data; |
| 1497 | efi_uintn_t data_size; |
| 1498 | u32 count, i; |
| 1499 | efi_status_t ret; |
Masahisa Kojima | f3e0c55 | 2021-10-26 17:27:27 +0900 | [diff] [blame] | 1500 | u8 deployed_mode; |
| 1501 | efi_uintn_t size; |
| 1502 | u32 deployed_audit_pcr_index = 1; |
| 1503 | |
| 1504 | size = sizeof(deployed_mode); |
| 1505 | ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid, |
| 1506 | NULL, &size, &deployed_mode, NULL); |
| 1507 | if (ret != EFI_SUCCESS || !deployed_mode) |
| 1508 | deployed_audit_pcr_index = 7; |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1509 | |
| 1510 | count = ARRAY_SIZE(secure_variables); |
| 1511 | for (i = 0; i < count; i++) { |
Heinrich Schuchardt | 6f26e7c | 2021-09-09 08:50:01 +0200 | [diff] [blame] | 1512 | const efi_guid_t *guid; |
| 1513 | |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 1514 | guid = efi_auth_var_get_guid(secure_variables[i].name); |
Heinrich Schuchardt | 6f26e7c | 2021-09-09 08:50:01 +0200 | [diff] [blame] | 1515 | |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 1516 | data = efi_get_var(secure_variables[i].name, guid, &data_size); |
| 1517 | if (!data && !secure_variables[i].accept_empty) |
| 1518 | continue; |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1519 | |
Masahisa Kojima | f3e0c55 | 2021-10-26 17:27:27 +0900 | [diff] [blame] | 1520 | if (u16_strcmp(u"DeployedMode", secure_variables[i].name)) |
| 1521 | secure_variables[i].pcr_index = deployed_audit_pcr_index; |
| 1522 | if (u16_strcmp(u"AuditMode", secure_variables[i].name)) |
| 1523 | secure_variables[i].pcr_index = deployed_audit_pcr_index; |
| 1524 | |
| 1525 | ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index, |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1526 | EV_EFI_VARIABLE_DRIVER_CONFIG, |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 1527 | secure_variables[i].name, guid, |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1528 | data_size, data); |
| 1529 | free(data); |
| 1530 | if (ret != EFI_SUCCESS) |
| 1531 | goto error; |
| 1532 | } |
| 1533 | |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1534 | error: |
| 1535 | return ret; |
| 1536 | } |
| 1537 | |
| 1538 | /** |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1539 | * efi_tcg2_do_initial_measurement() - do initial measurement |
| 1540 | * |
| 1541 | * Return: status code |
| 1542 | */ |
| 1543 | efi_status_t efi_tcg2_do_initial_measurement(void) |
| 1544 | { |
| 1545 | efi_status_t ret; |
| 1546 | struct udevice *dev; |
| 1547 | |
| 1548 | if (!is_tcg2_protocol_installed()) |
| 1549 | return EFI_SUCCESS; |
| 1550 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1551 | ret = tcg2_platform_get_tpm2(&dev); |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1552 | if (ret != EFI_SUCCESS) |
| 1553 | return EFI_SECURITY_VIOLATION; |
| 1554 | |
| 1555 | ret = tcg2_measure_secure_boot_variable(dev); |
| 1556 | if (ret != EFI_SUCCESS) |
| 1557 | goto out; |
| 1558 | |
| 1559 | out: |
| 1560 | return ret; |
| 1561 | } |
| 1562 | |
| 1563 | /** |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1564 | * efi_tcg2_register() - register EFI_TCG2_PROTOCOL |
| 1565 | * |
| 1566 | * If a TPM2 device is available, the TPM TCG2 Protocol is registered |
| 1567 | * |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1568 | * Return: status code |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1569 | */ |
| 1570 | efi_status_t efi_tcg2_register(void) |
| 1571 | { |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1572 | efi_status_t ret = EFI_SUCCESS; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1573 | struct udevice *dev; |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1574 | struct efi_event *event; |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 1575 | u32 err; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1576 | |
Eddie James | 8ed7bb3 | 2023-10-24 10:43:49 -0500 | [diff] [blame] | 1577 | ret = tcg2_platform_get_tpm2(&dev); |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1578 | if (ret != EFI_SUCCESS) { |
Ilias Apalodimas | eb1b6b4 | 2023-01-19 16:29:15 +0200 | [diff] [blame] | 1579 | log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n"); |
Ilias Apalodimas | fa5217d | 2021-05-10 21:19:14 +0300 | [diff] [blame] | 1580 | return EFI_SUCCESS; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1581 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1582 | |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 1583 | /* initialize the TPM as early as possible. */ |
Ilias Apalodimas | 0c95d22 | 2023-01-25 13:06:03 +0200 | [diff] [blame] | 1584 | err = tpm_auto_start(dev); |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 1585 | if (err) { |
| 1586 | log_err("TPM startup failed\n"); |
| 1587 | goto fail; |
| 1588 | } |
| 1589 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1590 | ret = efi_init_event_log(); |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1591 | if (ret != EFI_SUCCESS) { |
| 1592 | tcg2_uninit(); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1593 | goto fail; |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1594 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1595 | |
Ilias Apalodimas | 4953c99 | 2023-06-19 14:14:02 +0300 | [diff] [blame] | 1596 | ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol, |
| 1597 | &efi_tcg2_protocol, NULL); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1598 | if (ret != EFI_SUCCESS) { |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1599 | tcg2_uninit(); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1600 | goto fail; |
| 1601 | } |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1602 | |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1603 | ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK, |
| 1604 | efi_tcg2_notify_exit_boot_services, NULL, |
| 1605 | NULL, &event); |
| 1606 | if (ret != EFI_SUCCESS) { |
| 1607 | tcg2_uninit(); |
| 1608 | goto fail; |
| 1609 | } |
| 1610 | |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1611 | return ret; |
Ilias Apalodimas | fa5217d | 2021-05-10 21:19:14 +0300 | [diff] [blame] | 1612 | |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1613 | fail: |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1614 | log_err("Cannot install EFI_TCG2_PROTOCOL\n"); |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1615 | return ret; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1616 | } |