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 |
| 11 | #include <common.h> |
| 12 | #include <dm.h> |
| 13 | #include <efi_loader.h> |
| 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> |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 17 | #include <version.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 18 | #include <tpm-v2.h> |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 19 | #include <u-boot/hash-checksum.h> |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 20 | #include <u-boot/sha1.h> |
| 21 | #include <u-boot/sha256.h> |
| 22 | #include <u-boot/sha512.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 23 | #include <linux/unaligned/access_ok.h> |
| 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 | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 27 | struct event_log_buffer { |
| 28 | void *buffer; |
| 29 | void *final_buffer; |
| 30 | size_t pos; /* eventlog position */ |
| 31 | size_t final_pos; /* final events config table position */ |
| 32 | size_t last_event_size; |
| 33 | bool get_event_called; |
| 34 | bool truncated; |
| 35 | }; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 36 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 37 | static struct event_log_buffer event_log; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 38 | static bool tcg2_efi_app_invoked; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 39 | /* |
| 40 | * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset. |
| 41 | * Since the current tpm2_get_capability() response buffers starts at |
| 42 | * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate |
| 43 | * the response size and offset once for all consumers |
| 44 | */ |
| 45 | #define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \ |
| 46 | offsetof(struct tpms_capability_data, data)) |
| 47 | #define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \ |
| 48 | offsetof(struct tpms_tagged_property, value)) |
| 49 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 50 | static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID; |
| 51 | static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID; |
| 52 | |
| 53 | struct digest_info { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 54 | u16 hash_alg; |
| 55 | u32 hash_mask; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 56 | u16 hash_len; |
| 57 | }; |
| 58 | |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 59 | static const struct digest_info hash_algo_list[] = { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 60 | { |
| 61 | TPM2_ALG_SHA1, |
| 62 | EFI_TCG2_BOOT_HASH_ALG_SHA1, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 63 | TPM2_SHA1_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 64 | }, |
| 65 | { |
| 66 | TPM2_ALG_SHA256, |
| 67 | EFI_TCG2_BOOT_HASH_ALG_SHA256, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 68 | TPM2_SHA256_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 69 | }, |
| 70 | { |
| 71 | TPM2_ALG_SHA384, |
| 72 | EFI_TCG2_BOOT_HASH_ALG_SHA384, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 73 | TPM2_SHA384_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 74 | }, |
| 75 | { |
| 76 | TPM2_ALG_SHA512, |
| 77 | EFI_TCG2_BOOT_HASH_ALG_SHA512, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 78 | TPM2_SHA512_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 79 | }, |
| 80 | }; |
| 81 | |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 82 | struct variable_info { |
| 83 | u16 *name; |
| 84 | const efi_guid_t *guid; |
| 85 | }; |
| 86 | |
| 87 | static struct variable_info secure_variables[] = { |
| 88 | {L"SecureBoot", &efi_global_variable_guid}, |
| 89 | {L"PK", &efi_global_variable_guid}, |
| 90 | {L"KEK", &efi_global_variable_guid}, |
| 91 | {L"db", &efi_guid_image_security_database}, |
| 92 | {L"dbx", &efi_guid_image_security_database}, |
| 93 | }; |
| 94 | |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 95 | #define MAX_HASH_COUNT ARRAY_SIZE(hash_algo_list) |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 96 | |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 97 | /** |
| 98 | * alg_to_mask - Get a TCG hash mask for algorithms |
| 99 | * |
| 100 | * @hash_alg: TCG defined algorithm |
| 101 | * |
| 102 | * @Return: TCG hashing algorithm bitmaps, 0 if the algorithm is not supported |
| 103 | */ |
| 104 | static u32 alg_to_mask(u16 hash_alg) |
| 105 | { |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 106 | size_t i; |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 107 | |
| 108 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 109 | if (hash_algo_list[i].hash_alg == hash_alg) |
| 110 | return hash_algo_list[i].hash_mask; |
| 111 | } |
| 112 | |
| 113 | return 0; |
| 114 | } |
| 115 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 116 | /** |
| 117 | * alg_to_len - Get a TCG hash len for algorithms |
| 118 | * |
| 119 | * @hash_alg: TCG defined algorithm |
| 120 | * |
| 121 | * @Return: len of chosen algorithm, 0 if the algorithm is not supported |
| 122 | */ |
| 123 | static u16 alg_to_len(u16 hash_alg) |
| 124 | { |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 125 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 126 | |
| 127 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 128 | if (hash_algo_list[i].hash_alg == hash_alg) |
| 129 | return hash_algo_list[i].hash_len; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static u32 tcg_event_final_size(struct tpml_digest_values *digest_list) |
| 136 | { |
| 137 | u32 len; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 138 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 139 | |
| 140 | len = offsetof(struct tcg_pcr_event2, digests); |
| 141 | len += offsetof(struct tpml_digest_values, digests); |
| 142 | for (i = 0; i < digest_list->count; i++) { |
| 143 | u16 hash_alg = digest_list->digests[i].hash_alg; |
| 144 | |
| 145 | len += offsetof(struct tpmt_ha, digest); |
| 146 | len += alg_to_len(hash_alg); |
| 147 | } |
| 148 | len += sizeof(u32); /* tcg_pcr_event2 event_size*/ |
| 149 | |
| 150 | return len; |
| 151 | } |
| 152 | |
| 153 | /* tcg2_pcr_extend - Extend PCRs for a TPM2 device for a given tpml_digest_values |
| 154 | * |
| 155 | * @dev: device |
| 156 | * @digest_list: list of digest algorithms to extend |
| 157 | * |
| 158 | * @Return: status code |
| 159 | */ |
| 160 | static efi_status_t tcg2_pcr_extend(struct udevice *dev, u32 pcr_index, |
| 161 | struct tpml_digest_values *digest_list) |
| 162 | { |
| 163 | u32 rc; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 164 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 165 | |
| 166 | for (i = 0; i < digest_list->count; i++) { |
| 167 | u32 alg = digest_list->digests[i].hash_alg; |
| 168 | |
| 169 | rc = tpm2_pcr_extend(dev, pcr_index, alg, |
| 170 | (u8 *)&digest_list->digests[i].digest, |
| 171 | alg_to_len(alg)); |
| 172 | if (rc) { |
| 173 | EFI_PRINT("Failed to extend PCR\n"); |
| 174 | return EFI_DEVICE_ERROR; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return EFI_SUCCESS; |
| 179 | } |
| 180 | |
| 181 | /* tcg2_agile_log_append - Append an agile event to out eventlog |
| 182 | * |
| 183 | * @pcr_index: PCR index |
| 184 | * @event_type: type of event added |
| 185 | * @digest_list: list of digest algorithms to add |
| 186 | * @size: size of event |
| 187 | * @event: event to add |
| 188 | * |
| 189 | * @Return: status code |
| 190 | */ |
| 191 | static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type, |
| 192 | struct tpml_digest_values *digest_list, |
| 193 | u32 size, u8 event[]) |
| 194 | { |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 195 | void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 196 | size_t pos; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 197 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 198 | u32 event_size; |
| 199 | |
| 200 | if (event_log.get_event_called) |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 201 | log = (void *)((uintptr_t)event_log.final_buffer + |
| 202 | event_log.final_pos); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * size refers to the length of event[] only, we need to check against |
| 206 | * the final tcg_pcr_event2 size |
| 207 | */ |
| 208 | event_size = size + tcg_event_final_size(digest_list); |
| 209 | if (event_log.pos + event_size > TPM2_EVENT_LOG_SIZE || |
| 210 | event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE) { |
| 211 | event_log.truncated = true; |
| 212 | return EFI_VOLUME_FULL; |
| 213 | } |
| 214 | |
| 215 | put_unaligned_le32(pcr_index, log); |
| 216 | pos = offsetof(struct tcg_pcr_event2, event_type); |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 217 | put_unaligned_le32(event_type, (void *)((uintptr_t)log + pos)); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 218 | pos = offsetof(struct tcg_pcr_event2, digests); /* count */ |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 219 | put_unaligned_le32(digest_list->count, (void *)((uintptr_t)log + pos)); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 220 | |
| 221 | pos += offsetof(struct tpml_digest_values, digests); |
| 222 | for (i = 0; i < digest_list->count; i++) { |
| 223 | u16 hash_alg = digest_list->digests[i].hash_alg; |
| 224 | u8 *digest = (u8 *)&digest_list->digests[i].digest; |
| 225 | |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 226 | put_unaligned_le16(hash_alg, (void *)((uintptr_t)log + pos)); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 227 | pos += offsetof(struct tpmt_ha, digest); |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 228 | memcpy((void *)((uintptr_t)log + pos), digest, alg_to_len(hash_alg)); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 229 | pos += alg_to_len(hash_alg); |
| 230 | } |
| 231 | |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 232 | put_unaligned_le32(size, (void *)((uintptr_t)log + pos)); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 233 | pos += sizeof(u32); /* tcg_pcr_event2 event_size*/ |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 234 | memcpy((void *)((uintptr_t)log + pos), event, size); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 235 | pos += size; |
| 236 | |
| 237 | /* make sure the calculated buffer is what we checked against */ |
| 238 | if (pos != event_size) |
| 239 | return EFI_INVALID_PARAMETER; |
| 240 | |
| 241 | /* if GetEventLog hasn't been called update the normal log */ |
| 242 | if (!event_log.get_event_called) { |
| 243 | event_log.pos += pos; |
| 244 | event_log.last_event_size = pos; |
| 245 | } else { |
| 246 | /* if GetEventLog has been called update config table log */ |
| 247 | struct efi_tcg2_final_events_table *final_event; |
| 248 | |
| 249 | final_event = |
| 250 | (struct efi_tcg2_final_events_table *)(event_log.final_buffer); |
| 251 | final_event->number_of_events++; |
| 252 | event_log.final_pos += pos; |
| 253 | } |
| 254 | |
| 255 | return EFI_SUCCESS; |
| 256 | } |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 257 | |
| 258 | /** |
| 259 | * platform_get_tpm_device() - retrieve TPM device |
| 260 | * |
| 261 | * This function retrieves the udevice implementing a TPM |
| 262 | * |
| 263 | * This function may be overridden if special initialization is needed. |
| 264 | * |
| 265 | * @dev: udevice |
| 266 | * Return: status code |
| 267 | */ |
| 268 | __weak efi_status_t platform_get_tpm2_device(struct udevice **dev) |
| 269 | { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 270 | for_each_tpm_device(*dev) { |
| 271 | /* Only support TPMv2 devices */ |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 272 | if (tpm_get_version(*dev) == TPM_V2) |
| 273 | return EFI_SUCCESS; |
| 274 | } |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 275 | |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 276 | return EFI_NOT_FOUND; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * tpm2_get_max_command_size() - get the supported max command size |
| 281 | * |
| 282 | * @dev: TPM device |
| 283 | * @max_command_size: output buffer for the size |
| 284 | * |
| 285 | * Return: 0 on success, -1 on error |
| 286 | */ |
| 287 | static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size) |
| 288 | { |
| 289 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 290 | u32 ret; |
| 291 | |
| 292 | memset(response, 0, sizeof(response)); |
| 293 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 294 | TPM2_PT_MAX_COMMAND_SIZE, response, 1); |
| 295 | if (ret) |
| 296 | return -1; |
| 297 | |
| 298 | *max_command_size = (uint16_t)get_unaligned_be32(response + |
| 299 | properties_offset); |
| 300 | |
| 301 | return 0; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * tpm2_get_max_response_size() - get the supported max response size |
| 306 | * |
| 307 | * @dev: TPM device |
| 308 | * @max_response_size: output buffer for the size |
| 309 | * |
| 310 | * Return: 0 on success, -1 on error |
| 311 | */ |
| 312 | static int tpm2_get_max_response_size(struct udevice *dev, |
| 313 | u16 *max_response_size) |
| 314 | { |
| 315 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 316 | u32 ret; |
| 317 | |
| 318 | memset(response, 0, sizeof(response)); |
| 319 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 320 | TPM2_PT_MAX_RESPONSE_SIZE, response, 1); |
| 321 | if (ret) |
| 322 | return -1; |
| 323 | |
| 324 | *max_response_size = (uint16_t)get_unaligned_be32(response + |
| 325 | properties_offset); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * tpm2_get_manufacturer_id() - get the manufacturer ID |
| 332 | * |
| 333 | * @dev: TPM device |
| 334 | * @manufacturer_id: output buffer for the id |
| 335 | * |
| 336 | * Return: 0 on success, -1 on error |
| 337 | */ |
| 338 | static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id) |
| 339 | { |
| 340 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 341 | u32 ret; |
| 342 | |
| 343 | memset(response, 0, sizeof(response)); |
| 344 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 345 | TPM2_PT_MANUFACTURER, response, 1); |
| 346 | if (ret) |
| 347 | return -1; |
| 348 | |
| 349 | *manufacturer_id = get_unaligned_be32(response + properties_offset); |
| 350 | |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * tpm2_get_num_pcr() - get the number of PCRs |
| 356 | * |
| 357 | * @dev: TPM device |
| 358 | * @manufacturer_id: output buffer for the number |
| 359 | * |
| 360 | * Return: 0 on success, -1 on error |
| 361 | */ |
| 362 | static int tpm2_get_num_pcr(struct udevice *dev, u32 *num_pcr) |
| 363 | { |
| 364 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 365 | u32 ret; |
| 366 | |
| 367 | memset(response, 0, sizeof(response)); |
| 368 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 369 | TPM2_PT_PCR_COUNT, response, 1); |
| 370 | if (ret) |
| 371 | return -1; |
| 372 | |
| 373 | *num_pcr = get_unaligned_be32(response + properties_offset); |
| 374 | if (*num_pcr > TPM2_MAX_PCRS) |
| 375 | return -1; |
| 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * is_active_pcr() - Check if a supported algorithm is active |
| 382 | * |
| 383 | * @dev: TPM device |
| 384 | * @selection: struct of PCR information |
| 385 | * |
| 386 | * Return: true if PCR is active |
| 387 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 388 | static bool is_active_pcr(struct tpms_pcr_selection *selection) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 389 | { |
| 390 | int i; |
| 391 | /* |
| 392 | * check the pcr_select. If at least one of the PCRs supports the |
| 393 | * algorithm add it on the active ones |
| 394 | */ |
| 395 | for (i = 0; i < selection->size_of_select; i++) { |
| 396 | if (selection->pcr_select[i]) |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | return false; |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks |
| 405 | * |
| 406 | * @dev: TPM device |
| 407 | * @supported_pcr: bitmask with the algorithms supported |
| 408 | * @active_pcr: bitmask with the active algorithms |
| 409 | * @pcr_banks: number of PCR banks |
| 410 | * |
| 411 | * Return: 0 on success, -1 on error |
| 412 | */ |
| 413 | static int tpm2_get_pcr_info(struct udevice *dev, u32 *supported_pcr, |
| 414 | u32 *active_pcr, u32 *pcr_banks) |
| 415 | { |
| 416 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 417 | struct tpml_pcr_selection pcrs; |
| 418 | u32 ret, num_pcr; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 419 | size_t i; |
| 420 | int tpm_ret; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 421 | |
Ilias Apalodimas | 09402b1 | 2021-05-26 21:01:00 +0300 | [diff] [blame] | 422 | *supported_pcr = 0; |
| 423 | *active_pcr = 0; |
| 424 | *pcr_banks = 0; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 425 | memset(response, 0, sizeof(response)); |
| 426 | ret = tpm2_get_capability(dev, TPM2_CAP_PCRS, 0, response, 1); |
| 427 | if (ret) |
| 428 | goto out; |
| 429 | |
| 430 | pcrs.count = get_unaligned_be32(response); |
| 431 | /* |
| 432 | * We only support 5 algorithms for now so check against that |
| 433 | * instead of TPM2_NUM_PCR_BANKS |
| 434 | */ |
| 435 | if (pcrs.count > MAX_HASH_COUNT || pcrs.count < 1) |
| 436 | goto out; |
| 437 | |
| 438 | tpm_ret = tpm2_get_num_pcr(dev, &num_pcr); |
| 439 | if (tpm_ret) |
| 440 | goto out; |
| 441 | |
| 442 | for (i = 0; i < pcrs.count; i++) { |
| 443 | /* |
| 444 | * Definition of TPMS_PCR_SELECTION Structure |
| 445 | * hash: u16 |
| 446 | * size_of_select: u8 |
| 447 | * pcr_select: u8 array |
| 448 | * |
| 449 | * The offsets depend on the number of the device PCRs |
| 450 | * so we have to calculate them based on that |
| 451 | */ |
| 452 | u32 hash_offset = offsetof(struct tpml_pcr_selection, selection) + |
| 453 | i * offsetof(struct tpms_pcr_selection, pcr_select) + |
| 454 | i * ((num_pcr + 7) / 8); |
| 455 | u32 size_select_offset = |
| 456 | hash_offset + offsetof(struct tpms_pcr_selection, |
| 457 | size_of_select); |
| 458 | u32 pcr_select_offset = |
| 459 | hash_offset + offsetof(struct tpms_pcr_selection, |
| 460 | pcr_select); |
| 461 | |
| 462 | pcrs.selection[i].hash = |
| 463 | get_unaligned_be16(response + hash_offset); |
| 464 | pcrs.selection[i].size_of_select = |
| 465 | __get_unaligned_be(response + size_select_offset); |
| 466 | if (pcrs.selection[i].size_of_select > TPM2_PCR_SELECT_MAX) |
| 467 | goto out; |
| 468 | /* copy the array of pcr_select */ |
| 469 | memcpy(pcrs.selection[i].pcr_select, response + pcr_select_offset, |
| 470 | pcrs.selection[i].size_of_select); |
| 471 | } |
| 472 | |
| 473 | for (i = 0; i < pcrs.count; i++) { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 474 | u32 hash_mask = alg_to_mask(pcrs.selection[i].hash); |
| 475 | |
| 476 | if (hash_mask) { |
| 477 | *supported_pcr |= hash_mask; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 478 | if (is_active_pcr(&pcrs.selection[i])) |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 479 | *active_pcr |= hash_mask; |
| 480 | } else { |
| 481 | EFI_PRINT("Unknown algorithm %x\n", pcrs.selection[i].hash); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | *pcr_banks = pcrs.count; |
| 486 | |
| 487 | return 0; |
| 488 | out: |
| 489 | return -1; |
| 490 | } |
| 491 | |
| 492 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 493 | * __get_active_pcr_banks() - returns the currently active PCR banks |
| 494 | * |
| 495 | * @active_pcr_banks: pointer for receiving the bitmap of currently |
| 496 | * active PCR banks |
| 497 | * |
| 498 | * Return: status code |
| 499 | */ |
| 500 | static efi_status_t __get_active_pcr_banks(u32 *active_pcr_banks) |
| 501 | { |
| 502 | struct udevice *dev; |
Ilias Apalodimas | 09402b1 | 2021-05-26 21:01:00 +0300 | [diff] [blame] | 503 | u32 active = 0, supported = 0, pcr_banks = 0; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 504 | efi_status_t ret; |
| 505 | int err; |
| 506 | |
| 507 | ret = platform_get_tpm2_device(&dev); |
| 508 | if (ret != EFI_SUCCESS) |
| 509 | goto out; |
| 510 | |
| 511 | err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_banks); |
| 512 | if (err) { |
| 513 | ret = EFI_DEVICE_ERROR; |
| 514 | goto out; |
| 515 | } |
| 516 | |
| 517 | *active_pcr_banks = active; |
| 518 | |
| 519 | out: |
| 520 | return ret; |
| 521 | } |
| 522 | |
| 523 | /* tcg2_create_digest - create a list of digests of the supported PCR banks |
| 524 | * for a given memory range |
| 525 | * |
| 526 | * @input: input memory |
| 527 | * @length: length of buffer to calculate the digest |
| 528 | * @digest_list: list of digests to fill in |
| 529 | * |
| 530 | * Return: status code |
| 531 | */ |
| 532 | static efi_status_t tcg2_create_digest(const u8 *input, u32 length, |
| 533 | struct tpml_digest_values *digest_list) |
| 534 | { |
| 535 | sha1_context ctx; |
| 536 | sha256_context ctx_256; |
| 537 | sha512_context ctx_512; |
Masahisa Kojima | eb74a90 | 2021-04-14 11:55:49 +0900 | [diff] [blame] | 538 | u8 final[TPM2_SHA512_DIGEST_SIZE]; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 539 | efi_status_t ret; |
| 540 | u32 active; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 541 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 542 | |
| 543 | ret = __get_active_pcr_banks(&active); |
| 544 | if (ret != EFI_SUCCESS) |
| 545 | return ret; |
| 546 | |
| 547 | digest_list->count = 0; |
| 548 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 549 | u16 hash_alg = hash_algo_list[i].hash_alg; |
| 550 | |
| 551 | if (!(active & alg_to_mask(hash_alg))) |
| 552 | continue; |
| 553 | switch (hash_alg) { |
| 554 | case TPM2_ALG_SHA1: |
| 555 | sha1_starts(&ctx); |
| 556 | sha1_update(&ctx, input, length); |
| 557 | sha1_finish(&ctx, final); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 558 | break; |
| 559 | case TPM2_ALG_SHA256: |
| 560 | sha256_starts(&ctx_256); |
| 561 | sha256_update(&ctx_256, input, length); |
| 562 | sha256_finish(&ctx_256, final); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 563 | break; |
| 564 | case TPM2_ALG_SHA384: |
| 565 | sha384_starts(&ctx_512); |
| 566 | sha384_update(&ctx_512, input, length); |
| 567 | sha384_finish(&ctx_512, final); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 568 | break; |
| 569 | case TPM2_ALG_SHA512: |
| 570 | sha512_starts(&ctx_512); |
| 571 | sha512_update(&ctx_512, input, length); |
| 572 | sha512_finish(&ctx_512, final); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 573 | break; |
| 574 | default: |
| 575 | EFI_PRINT("Unsupported algorithm %x\n", hash_alg); |
| 576 | return EFI_INVALID_PARAMETER; |
| 577 | } |
Ilias Apalodimas | 754b3a4 | 2021-04-22 14:32:14 +0300 | [diff] [blame] | 578 | digest_list->count++; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 579 | digest_list->digests[i].hash_alg = hash_alg; |
| 580 | memcpy(&digest_list->digests[i].digest, final, (u32)alg_to_len(hash_alg)); |
| 581 | } |
| 582 | |
| 583 | return EFI_SUCCESS; |
| 584 | } |
| 585 | |
| 586 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 587 | * efi_tcg2_get_capability() - protocol capability information and state information |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 588 | * |
| 589 | * @this: TCG2 protocol instance |
| 590 | * @capability: caller allocated memory with size field to the size of |
| 591 | * the structure allocated |
| 592 | |
| 593 | * Return: status code |
| 594 | */ |
| 595 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 596 | efi_tcg2_get_capability(struct efi_tcg2_protocol *this, |
| 597 | struct efi_tcg2_boot_service_capability *capability) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 598 | { |
| 599 | struct udevice *dev; |
| 600 | efi_status_t efi_ret; |
| 601 | int ret; |
| 602 | |
| 603 | EFI_ENTRY("%p, %p", this, capability); |
| 604 | |
| 605 | if (!this || !capability) { |
| 606 | efi_ret = EFI_INVALID_PARAMETER; |
| 607 | goto out; |
| 608 | } |
| 609 | |
| 610 | if (capability->size < boot_service_capability_min) { |
| 611 | capability->size = boot_service_capability_min; |
| 612 | efi_ret = EFI_BUFFER_TOO_SMALL; |
| 613 | goto out; |
| 614 | } |
| 615 | |
| 616 | if (capability->size < sizeof(*capability)) { |
| 617 | capability->size = sizeof(*capability); |
| 618 | efi_ret = EFI_BUFFER_TOO_SMALL; |
| 619 | goto out; |
| 620 | } |
| 621 | |
| 622 | capability->structure_version.major = 1; |
| 623 | capability->structure_version.minor = 1; |
| 624 | capability->protocol_version.major = 1; |
| 625 | capability->protocol_version.minor = 1; |
| 626 | |
| 627 | efi_ret = platform_get_tpm2_device(&dev); |
| 628 | if (efi_ret != EFI_SUCCESS) { |
| 629 | capability->supported_event_logs = 0; |
| 630 | capability->hash_algorithm_bitmap = 0; |
| 631 | capability->tpm_present_flag = false; |
| 632 | capability->max_command_size = 0; |
| 633 | capability->max_response_size = 0; |
| 634 | capability->manufacturer_id = 0; |
| 635 | capability->number_of_pcr_banks = 0; |
| 636 | capability->active_pcr_banks = 0; |
| 637 | |
| 638 | efi_ret = EFI_SUCCESS; |
| 639 | goto out; |
| 640 | } |
| 641 | |
| 642 | /* We only allow a TPMv2 device to register the EFI protocol */ |
| 643 | capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2; |
| 644 | |
| 645 | capability->tpm_present_flag = true; |
| 646 | |
| 647 | /* Supported and active PCRs */ |
| 648 | capability->hash_algorithm_bitmap = 0; |
| 649 | capability->active_pcr_banks = 0; |
| 650 | ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap, |
| 651 | &capability->active_pcr_banks, |
| 652 | &capability->number_of_pcr_banks); |
| 653 | if (ret) { |
| 654 | efi_ret = EFI_DEVICE_ERROR; |
| 655 | goto out; |
| 656 | } |
| 657 | |
| 658 | /* Max command size */ |
| 659 | ret = tpm2_get_max_command_size(dev, &capability->max_command_size); |
| 660 | if (ret) { |
| 661 | efi_ret = EFI_DEVICE_ERROR; |
| 662 | goto out; |
| 663 | } |
| 664 | |
| 665 | /* Max response size */ |
| 666 | ret = tpm2_get_max_response_size(dev, &capability->max_response_size); |
| 667 | if (ret) { |
| 668 | efi_ret = EFI_DEVICE_ERROR; |
| 669 | goto out; |
| 670 | } |
| 671 | |
| 672 | /* Manufacturer ID */ |
| 673 | ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id); |
| 674 | if (ret) { |
| 675 | efi_ret = EFI_DEVICE_ERROR; |
| 676 | goto out; |
| 677 | } |
| 678 | |
| 679 | return EFI_EXIT(EFI_SUCCESS); |
| 680 | out: |
| 681 | return EFI_EXIT(efi_ret); |
| 682 | } |
| 683 | |
| 684 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 685 | * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its |
| 686 | * last entry |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 687 | * |
| 688 | * @this: TCG2 protocol instance |
| 689 | * @log_format: type of event log format |
| 690 | * @event_log_location: pointer to the memory address of the event log |
| 691 | * @event_log_last_entry: pointer to the address of the start of the last |
| 692 | * entry in the event log in memory, if log contains |
| 693 | * more than 1 entry |
| 694 | * @event_log_truncated: set to true, if the Event Log is missing at i |
| 695 | * least one entry |
| 696 | * |
| 697 | * Return: status code |
| 698 | */ |
| 699 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 700 | efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this, |
| 701 | efi_tcg_event_log_format log_format, |
| 702 | u64 *event_log_location, u64 *event_log_last_entry, |
| 703 | bool *event_log_truncated) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 704 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 705 | efi_status_t ret = EFI_SUCCESS; |
| 706 | struct udevice *dev; |
| 707 | |
| 708 | EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location, |
| 709 | event_log_last_entry, event_log_truncated); |
| 710 | |
| 711 | ret = platform_get_tpm2_device(&dev); |
| 712 | if (ret != EFI_SUCCESS) { |
| 713 | event_log_location = NULL; |
| 714 | event_log_last_entry = NULL; |
| 715 | *event_log_truncated = false; |
| 716 | ret = EFI_SUCCESS; |
| 717 | goto out; |
| 718 | } |
| 719 | *event_log_location = (uintptr_t)event_log.buffer; |
| 720 | *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos - |
| 721 | event_log.last_event_size); |
| 722 | *event_log_truncated = event_log.truncated; |
| 723 | event_log.get_event_called = true; |
| 724 | |
| 725 | out: |
| 726 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /** |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 730 | * tcg2_hash_pe_image() - calculate PE/COFF image hash |
| 731 | * |
| 732 | * @efi: pointer to the EFI binary |
| 733 | * @efi_size: size of @efi binary |
| 734 | * @digest_list: list of digest algorithms to extend |
| 735 | * |
| 736 | * Return: status code |
| 737 | */ |
| 738 | static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size, |
| 739 | struct tpml_digest_values *digest_list) |
| 740 | { |
| 741 | WIN_CERTIFICATE *wincerts = NULL; |
| 742 | size_t wincerts_len; |
| 743 | struct efi_image_regions *regs = NULL; |
| 744 | void *new_efi = NULL; |
| 745 | u8 hash[TPM2_SHA512_DIGEST_SIZE]; |
| 746 | efi_status_t ret; |
| 747 | u32 active; |
| 748 | int i; |
| 749 | |
| 750 | new_efi = efi_prepare_aligned_image(efi, &efi_size); |
| 751 | if (!new_efi) |
| 752 | return EFI_OUT_OF_RESOURCES; |
| 753 | |
| 754 | if (!efi_image_parse(new_efi, efi_size, ®s, &wincerts, |
| 755 | &wincerts_len)) { |
| 756 | log_err("Parsing PE executable image failed\n"); |
| 757 | ret = EFI_UNSUPPORTED; |
| 758 | goto out; |
| 759 | } |
| 760 | |
| 761 | ret = __get_active_pcr_banks(&active); |
| 762 | if (ret != EFI_SUCCESS) { |
| 763 | goto out; |
| 764 | } |
| 765 | |
| 766 | digest_list->count = 0; |
| 767 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 768 | u16 hash_alg = hash_algo_list[i].hash_alg; |
| 769 | |
| 770 | if (!(active & alg_to_mask(hash_alg))) |
| 771 | continue; |
| 772 | switch (hash_alg) { |
| 773 | case TPM2_ALG_SHA1: |
| 774 | hash_calculate("sha1", regs->reg, regs->num, hash); |
| 775 | break; |
| 776 | case TPM2_ALG_SHA256: |
| 777 | hash_calculate("sha256", regs->reg, regs->num, hash); |
| 778 | break; |
| 779 | case TPM2_ALG_SHA384: |
| 780 | hash_calculate("sha384", regs->reg, regs->num, hash); |
| 781 | break; |
| 782 | case TPM2_ALG_SHA512: |
| 783 | hash_calculate("sha512", regs->reg, regs->num, hash); |
| 784 | break; |
| 785 | default: |
| 786 | EFI_PRINT("Unsupported algorithm %x\n", hash_alg); |
| 787 | return EFI_INVALID_PARAMETER; |
| 788 | } |
| 789 | digest_list->digests[i].hash_alg = hash_alg; |
| 790 | memcpy(&digest_list->digests[i].digest, hash, (u32)alg_to_len(hash_alg)); |
| 791 | digest_list->count++; |
| 792 | } |
| 793 | |
| 794 | out: |
| 795 | if (new_efi != efi) |
| 796 | free(new_efi); |
| 797 | free(regs); |
| 798 | |
| 799 | return ret; |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * tcg2_measure_pe_image() - measure PE/COFF image |
| 804 | * |
| 805 | * @efi: pointer to the EFI binary |
| 806 | * @efi_size: size of @efi binary |
| 807 | * @handle: loaded image handle |
| 808 | * @loaded_image: loaded image protocol |
| 809 | * |
| 810 | * Return: status code |
| 811 | */ |
| 812 | efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size, |
| 813 | struct efi_loaded_image_obj *handle, |
| 814 | struct efi_loaded_image *loaded_image) |
| 815 | { |
| 816 | struct tpml_digest_values digest_list; |
| 817 | efi_status_t ret; |
| 818 | struct udevice *dev; |
| 819 | u32 pcr_index, event_type, event_size; |
| 820 | struct uefi_image_load_event *image_load_event; |
| 821 | struct efi_device_path *device_path; |
| 822 | u32 device_path_length; |
| 823 | IMAGE_DOS_HEADER *dos; |
| 824 | IMAGE_NT_HEADERS32 *nt; |
| 825 | struct efi_handler *handler; |
| 826 | |
| 827 | ret = platform_get_tpm2_device(&dev); |
| 828 | if (ret != EFI_SUCCESS) |
| 829 | return ret; |
| 830 | |
| 831 | switch (handle->image_type) { |
| 832 | case IMAGE_SUBSYSTEM_EFI_APPLICATION: |
| 833 | pcr_index = 4; |
| 834 | event_type = EV_EFI_BOOT_SERVICES_APPLICATION; |
| 835 | break; |
| 836 | case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: |
| 837 | pcr_index = 2; |
| 838 | event_type = EV_EFI_BOOT_SERVICES_DRIVER; |
| 839 | break; |
| 840 | case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: |
| 841 | pcr_index = 2; |
| 842 | event_type = EV_EFI_RUNTIME_SERVICES_DRIVER; |
| 843 | break; |
| 844 | default: |
| 845 | return EFI_UNSUPPORTED; |
| 846 | } |
| 847 | |
| 848 | ret = tcg2_hash_pe_image(efi, efi_size, &digest_list); |
| 849 | if (ret != EFI_SUCCESS) |
| 850 | return ret; |
| 851 | |
| 852 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 853 | if (ret != EFI_SUCCESS) |
| 854 | return ret; |
| 855 | |
| 856 | ret = EFI_CALL(efi_search_protocol(&handle->header, |
| 857 | &efi_guid_loaded_image_device_path, |
| 858 | &handler)); |
| 859 | if (ret != EFI_SUCCESS) |
| 860 | return ret; |
| 861 | |
| 862 | device_path = EFI_CALL(handler->protocol_interface); |
| 863 | device_path_length = efi_dp_size(device_path); |
| 864 | if (device_path_length > 0) { |
| 865 | /* add end node size */ |
| 866 | device_path_length += sizeof(struct efi_device_path); |
| 867 | } |
| 868 | event_size = sizeof(struct uefi_image_load_event) + device_path_length; |
| 869 | image_load_event = (struct uefi_image_load_event *)malloc(event_size); |
| 870 | if (!image_load_event) |
| 871 | return EFI_OUT_OF_RESOURCES; |
| 872 | |
| 873 | image_load_event->image_location_in_memory = (uintptr_t)efi; |
| 874 | image_load_event->image_length_in_memory = efi_size; |
| 875 | image_load_event->length_of_device_path = device_path_length; |
| 876 | |
| 877 | dos = (IMAGE_DOS_HEADER *)efi; |
| 878 | nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew); |
| 879 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
| 880 | IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt; |
| 881 | |
| 882 | image_load_event->image_link_time_address = |
| 883 | nt64->OptionalHeader.ImageBase; |
| 884 | } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
| 885 | image_load_event->image_link_time_address = |
| 886 | nt->OptionalHeader.ImageBase; |
| 887 | } else { |
| 888 | ret = EFI_INVALID_PARAMETER; |
| 889 | goto out; |
| 890 | } |
| 891 | |
| 892 | if (device_path_length > 0) { |
| 893 | memcpy(image_load_event->device_path, device_path, |
| 894 | device_path_length); |
| 895 | } |
| 896 | |
| 897 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 898 | event_size, (u8 *)image_load_event); |
| 899 | |
| 900 | out: |
| 901 | free(image_load_event); |
| 902 | |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 907 | * efi_tcg2_hash_log_extend_event() - extend and optionally log events |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 908 | * |
| 909 | * @this: TCG2 protocol instance |
| 910 | * @flags: bitmap providing additional information on the |
| 911 | * operation |
| 912 | * @data_to_hash: physical address of the start of the data buffer |
| 913 | * to be hashed |
| 914 | * @data_to_hash_len: the length in bytes of the buffer referenced by |
| 915 | * data_to_hash |
| 916 | * @efi_tcg_event: pointer to data buffer containing information |
| 917 | * about the event |
| 918 | * |
| 919 | * Return: status code |
| 920 | */ |
| 921 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 922 | efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags, |
| 923 | u64 data_to_hash, u64 data_to_hash_len, |
| 924 | struct efi_tcg2_event *efi_tcg_event) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 925 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 926 | struct udevice *dev; |
| 927 | efi_status_t ret; |
| 928 | u32 event_type, pcr_index, event_size; |
| 929 | struct tpml_digest_values digest_list; |
| 930 | |
| 931 | EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash, |
| 932 | data_to_hash_len, efi_tcg_event); |
| 933 | |
| 934 | if (!this || !data_to_hash || !efi_tcg_event) { |
| 935 | ret = EFI_INVALID_PARAMETER; |
| 936 | goto out; |
| 937 | } |
| 938 | |
| 939 | ret = platform_get_tpm2_device(&dev); |
| 940 | if (ret != EFI_SUCCESS) |
| 941 | goto out; |
| 942 | |
| 943 | if (efi_tcg_event->size < efi_tcg_event->header.header_size + |
| 944 | sizeof(u32)) { |
| 945 | ret = EFI_INVALID_PARAMETER; |
| 946 | goto out; |
| 947 | } |
| 948 | |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 949 | if (efi_tcg_event->header.pcr_index > TPM2_MAX_PCRS) { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 950 | ret = EFI_INVALID_PARAMETER; |
| 951 | goto out; |
| 952 | } |
| 953 | |
| 954 | /* |
| 955 | * if PE_COFF_IMAGE is set we need to make sure the image is not |
| 956 | * corrupted, verify it and hash the PE/COFF image in accordance with |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 957 | * the procedure specified in "Calculating the PE Image Hash" |
| 958 | * section of the "Windows Authenticode Portable Executable Signature |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 959 | * Format" |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 960 | */ |
| 961 | if (flags & PE_COFF_IMAGE) { |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 962 | IMAGE_NT_HEADERS32 *nt; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 963 | |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 964 | ret = efi_check_pe((void *)(uintptr_t)data_to_hash, |
| 965 | data_to_hash_len, (void **)&nt); |
| 966 | if (ret != EFI_SUCCESS) { |
| 967 | log_err("Not a valid PE-COFF file\n"); |
| 968 | goto out; |
| 969 | } |
| 970 | ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash, |
| 971 | data_to_hash_len, &digest_list); |
| 972 | } else { |
| 973 | ret = tcg2_create_digest((u8 *)(uintptr_t)data_to_hash, |
| 974 | data_to_hash_len, &digest_list); |
| 975 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 976 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 977 | if (ret != EFI_SUCCESS) |
| 978 | goto out; |
| 979 | |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 980 | pcr_index = efi_tcg_event->header.pcr_index; |
| 981 | event_type = efi_tcg_event->header.event_type; |
| 982 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 983 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 984 | if (ret != EFI_SUCCESS) |
| 985 | goto out; |
| 986 | |
| 987 | if (flags & EFI_TCG2_EXTEND_ONLY) { |
| 988 | if (event_log.truncated) |
| 989 | ret = EFI_VOLUME_FULL; |
| 990 | goto out; |
| 991 | } |
| 992 | |
| 993 | /* |
| 994 | * The efi_tcg_event size includes the size component and the |
| 995 | * headersize |
| 996 | */ |
| 997 | event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) - |
| 998 | efi_tcg_event->header.header_size; |
| 999 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 1000 | event_size, efi_tcg_event->event); |
| 1001 | out: |
| 1002 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1006 | * efi_tcg2_submit_command() - Send command to the TPM |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1007 | * |
| 1008 | * @this: TCG2 protocol instance |
| 1009 | * @input_param_block_size: size of the TPM input parameter block |
| 1010 | * @input_param_block: pointer to the TPM input parameter block |
| 1011 | * @output_param_block_size: size of the TPM output parameter block |
| 1012 | * @output_param_block: pointer to the TPM output parameter block |
| 1013 | * |
| 1014 | * Return: status code |
| 1015 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1016 | static efi_status_t EFIAPI |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 1017 | efi_tcg2_submit_command(__maybe_unused struct efi_tcg2_protocol *this, |
| 1018 | u32 __maybe_unused input_param_block_size, |
| 1019 | u8 __maybe_unused *input_param_block, |
| 1020 | u32 __maybe_unused output_param_block_size, |
| 1021 | u8 __maybe_unused *output_param_block) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1022 | { |
| 1023 | return EFI_UNSUPPORTED; |
| 1024 | } |
| 1025 | |
| 1026 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1027 | * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1028 | * |
| 1029 | * @this: TCG2 protocol instance |
| 1030 | * @active_pcr_banks: pointer for receiving the bitmap of currently |
| 1031 | * active PCR banks |
| 1032 | * |
| 1033 | * Return: status code |
| 1034 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1035 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1036 | efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this, |
| 1037 | u32 *active_pcr_banks) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1038 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1039 | efi_status_t ret; |
| 1040 | |
| 1041 | EFI_ENTRY("%p, %p", this, active_pcr_banks); |
| 1042 | ret = __get_active_pcr_banks(active_pcr_banks); |
| 1043 | |
| 1044 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1048 | * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1049 | * |
| 1050 | * @this: TCG2 protocol instance |
| 1051 | * @active_pcr_banks: bitmap of the requested active PCR banks |
| 1052 | * |
| 1053 | * Return: status code |
| 1054 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1055 | static efi_status_t EFIAPI |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 1056 | efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this, |
| 1057 | u32 __maybe_unused active_pcr_banks) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1058 | { |
| 1059 | return EFI_UNSUPPORTED; |
| 1060 | } |
| 1061 | |
| 1062 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1063 | * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous |
| 1064 | * set_active_pcr_banks() |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1065 | * |
| 1066 | * @this: TCG2 protocol instance |
| 1067 | * @operation_present: non-zero value to indicate a |
| 1068 | * set_active_pcr_banks operation was |
| 1069 | * invoked during last boot |
| 1070 | * @response: result value could be returned |
| 1071 | * |
| 1072 | * Return: status code |
| 1073 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1074 | static efi_status_t EFIAPI |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 1075 | efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this, |
| 1076 | u32 __maybe_unused *operation_present, |
| 1077 | u32 __maybe_unused *response) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1078 | { |
| 1079 | return EFI_UNSUPPORTED; |
| 1080 | } |
| 1081 | |
| 1082 | static const struct efi_tcg2_protocol efi_tcg2_protocol = { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1083 | .get_capability = efi_tcg2_get_capability, |
| 1084 | .get_eventlog = efi_tcg2_get_eventlog, |
| 1085 | .hash_log_extend_event = efi_tcg2_hash_log_extend_event, |
| 1086 | .submit_command = efi_tcg2_submit_command, |
| 1087 | .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks, |
| 1088 | .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks, |
| 1089 | .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] | 1090 | }; |
| 1091 | |
| 1092 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1093 | * create_specid_event() - Create the first event in the eventlog |
| 1094 | * |
| 1095 | * @dev: tpm device |
| 1096 | * @event_header: Pointer to the final event header |
| 1097 | * @event_size: final spec event size |
| 1098 | * |
| 1099 | * Return: status code |
| 1100 | */ |
| 1101 | static efi_status_t create_specid_event(struct udevice *dev, void *buffer, |
| 1102 | size_t *event_size) |
| 1103 | { |
| 1104 | struct tcg_efi_spec_id_event *spec_event; |
| 1105 | size_t spec_event_size; |
| 1106 | efi_status_t ret = EFI_DEVICE_ERROR; |
Ilias Apalodimas | 09402b1 | 2021-05-26 21:01:00 +0300 | [diff] [blame] | 1107 | u32 active = 0, supported = 0; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 1108 | int err; |
| 1109 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1110 | |
| 1111 | /* |
| 1112 | * Create Spec event. This needs to be the first event in the log |
| 1113 | * according to the TCG EFI protocol spec |
| 1114 | */ |
| 1115 | |
| 1116 | /* Setup specID event data */ |
| 1117 | spec_event = (struct tcg_efi_spec_id_event *)buffer; |
| 1118 | memcpy(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03, |
| 1119 | sizeof(spec_event->signature)); |
| 1120 | put_unaligned_le32(0, &spec_event->platform_class); /* type client */ |
| 1121 | spec_event->spec_version_minor = |
| 1122 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2; |
| 1123 | spec_event->spec_version_major = |
| 1124 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2; |
| 1125 | spec_event->spec_errata = |
| 1126 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2; |
| 1127 | spec_event->uintn_size = sizeof(efi_uintn_t) / sizeof(u32); |
| 1128 | |
| 1129 | err = tpm2_get_pcr_info(dev, &supported, &active, |
| 1130 | &spec_event->number_of_algorithms); |
| 1131 | if (err) |
| 1132 | goto out; |
| 1133 | if (spec_event->number_of_algorithms > MAX_HASH_COUNT || |
| 1134 | spec_event->number_of_algorithms < 1) |
| 1135 | goto out; |
| 1136 | |
| 1137 | for (i = 0; i < spec_event->number_of_algorithms; i++) { |
| 1138 | u16 hash_alg = hash_algo_list[i].hash_alg; |
| 1139 | u16 hash_len = hash_algo_list[i].hash_len; |
| 1140 | |
| 1141 | if (active && alg_to_mask(hash_alg)) { |
| 1142 | put_unaligned_le16(hash_alg, |
| 1143 | &spec_event->digest_sizes[i].algorithm_id); |
| 1144 | put_unaligned_le16(hash_len, |
| 1145 | &spec_event->digest_sizes[i].digest_size); |
| 1146 | } |
| 1147 | } |
| 1148 | /* |
| 1149 | * the size of the spec event and placement of vendor_info_size |
| 1150 | * depends on supported algoriths |
| 1151 | */ |
| 1152 | spec_event_size = |
| 1153 | offsetof(struct tcg_efi_spec_id_event, digest_sizes) + |
| 1154 | spec_event->number_of_algorithms * sizeof(spec_event->digest_sizes[0]); |
| 1155 | /* no vendor info for us */ |
| 1156 | memset(buffer + spec_event_size, 0, |
| 1157 | sizeof(spec_event->vendor_info_size)); |
| 1158 | spec_event_size += sizeof(spec_event->vendor_info_size); |
| 1159 | *event_size = spec_event_size; |
| 1160 | |
| 1161 | return EFI_SUCCESS; |
| 1162 | |
| 1163 | out: |
| 1164 | return ret; |
| 1165 | } |
| 1166 | |
| 1167 | /** |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1168 | * tcg2_uninit - remove the final event table and free efi memory on failures |
| 1169 | */ |
| 1170 | void tcg2_uninit(void) |
| 1171 | { |
| 1172 | efi_status_t ret; |
| 1173 | |
| 1174 | ret = efi_install_configuration_table(&efi_guid_final_events, NULL); |
| 1175 | if (ret != EFI_SUCCESS) |
| 1176 | log_err("Failed to delete final events config table\n"); |
| 1177 | |
| 1178 | efi_free_pool(event_log.buffer); |
| 1179 | event_log.buffer = NULL; |
| 1180 | efi_free_pool(event_log.final_buffer); |
| 1181 | event_log.final_buffer = NULL; |
| 1182 | } |
| 1183 | |
| 1184 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1185 | * create_final_event() - Create the final event and install the config |
| 1186 | * defined by the TCG EFI spec |
| 1187 | */ |
| 1188 | static efi_status_t create_final_event(void) |
| 1189 | { |
| 1190 | struct efi_tcg2_final_events_table *final_event; |
| 1191 | efi_status_t ret; |
| 1192 | |
| 1193 | /* |
| 1194 | * All events generated after the invocation of |
| 1195 | * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an |
| 1196 | * EFI_CONFIGURATION_TABLE |
| 1197 | */ |
| 1198 | ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE, |
| 1199 | &event_log.final_buffer); |
| 1200 | if (ret != EFI_SUCCESS) |
| 1201 | goto out; |
| 1202 | |
| 1203 | memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE); |
| 1204 | final_event = event_log.final_buffer; |
| 1205 | final_event->number_of_events = 0; |
| 1206 | final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION; |
| 1207 | event_log.final_pos = sizeof(*final_event); |
| 1208 | ret = efi_install_configuration_table(&efi_guid_final_events, |
| 1209 | final_event); |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1210 | if (ret != EFI_SUCCESS) { |
| 1211 | efi_free_pool(event_log.final_buffer); |
| 1212 | event_log.final_buffer = NULL; |
| 1213 | } |
| 1214 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1215 | out: |
| 1216 | return ret; |
| 1217 | } |
| 1218 | |
| 1219 | /** |
| 1220 | * efi_init_event_log() - initialize an eventlog |
| 1221 | */ |
| 1222 | static efi_status_t efi_init_event_log(void) |
| 1223 | { |
| 1224 | /* |
| 1225 | * vendor_info_size is currently set to 0, we need to change the length |
| 1226 | * and allocate the flexible array member if this changes |
| 1227 | */ |
| 1228 | struct tcg_pcr_event *event_header = NULL; |
| 1229 | struct udevice *dev; |
| 1230 | size_t spec_event_size; |
| 1231 | efi_status_t ret; |
| 1232 | |
| 1233 | ret = platform_get_tpm2_device(&dev); |
| 1234 | if (ret != EFI_SUCCESS) |
| 1235 | goto out; |
| 1236 | |
| 1237 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE, |
| 1238 | (void **)&event_log.buffer); |
| 1239 | if (ret != EFI_SUCCESS) |
| 1240 | goto out; |
| 1241 | |
| 1242 | /* |
| 1243 | * initialize log area as 0xff so the OS can easily figure out the |
| 1244 | * last log entry |
| 1245 | */ |
| 1246 | memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE); |
| 1247 | event_log.pos = 0; |
| 1248 | event_log.last_event_size = 0; |
| 1249 | event_log.get_event_called = false; |
| 1250 | event_log.truncated = false; |
| 1251 | |
| 1252 | /* |
| 1253 | * The log header is defined to be in SHA1 event log entry format. |
| 1254 | * Setup event header |
| 1255 | */ |
| 1256 | event_header = (struct tcg_pcr_event *)event_log.buffer; |
| 1257 | put_unaligned_le32(0, &event_header->pcr_index); |
| 1258 | put_unaligned_le32(EV_NO_ACTION, &event_header->event_type); |
| 1259 | memset(&event_header->digest, 0, sizeof(event_header->digest)); |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 1260 | ret = create_specid_event(dev, (void *)((uintptr_t)event_log.buffer + sizeof(*event_header)), |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1261 | &spec_event_size); |
| 1262 | if (ret != EFI_SUCCESS) |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1263 | goto free_pool; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1264 | put_unaligned_le32(spec_event_size, &event_header->event_size); |
| 1265 | event_log.pos = spec_event_size + sizeof(*event_header); |
| 1266 | event_log.last_event_size = event_log.pos; |
| 1267 | |
| 1268 | ret = create_final_event(); |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1269 | if (ret != EFI_SUCCESS) |
| 1270 | goto free_pool; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1271 | |
| 1272 | out: |
| 1273 | return ret; |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1274 | |
| 1275 | free_pool: |
| 1276 | efi_free_pool(event_log.buffer); |
| 1277 | event_log.buffer = NULL; |
| 1278 | return ret; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | /** |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1282 | * tcg2_measure_event() - common function to add event log and extend PCR |
| 1283 | * |
| 1284 | * @dev: TPM device |
| 1285 | * @pcr_index: PCR index |
| 1286 | * @event_type: type of event added |
| 1287 | * @size: event size |
| 1288 | * @event: event data |
| 1289 | * |
| 1290 | * Return: status code |
| 1291 | */ |
| 1292 | static efi_status_t |
| 1293 | tcg2_measure_event(struct udevice *dev, u32 pcr_index, u32 event_type, |
| 1294 | u32 size, u8 event[]) |
| 1295 | { |
| 1296 | struct tpml_digest_values digest_list; |
| 1297 | efi_status_t ret; |
| 1298 | |
| 1299 | ret = tcg2_create_digest(event, size, &digest_list); |
| 1300 | if (ret != EFI_SUCCESS) |
| 1301 | goto out; |
| 1302 | |
| 1303 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 1304 | if (ret != EFI_SUCCESS) |
| 1305 | goto out; |
| 1306 | |
| 1307 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 1308 | size, event); |
| 1309 | |
| 1310 | out: |
| 1311 | return ret; |
| 1312 | } |
| 1313 | |
| 1314 | /** |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1315 | * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the |
| 1316 | * eventlog and extend the PCRs |
| 1317 | * |
| 1318 | * @dev: TPM device |
| 1319 | * |
| 1320 | * @Return: status code |
| 1321 | */ |
| 1322 | static efi_status_t efi_append_scrtm_version(struct udevice *dev) |
| 1323 | { |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1324 | u8 ver[] = U_BOOT_VERSION_STRING; |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1325 | efi_status_t ret; |
| 1326 | |
Masahisa Kojima | 55f3ed4 | 2021-08-13 16:12:42 +0900 | [diff] [blame^] | 1327 | ret = tcg2_measure_event(dev, 0, EV_S_CRTM_VERSION, sizeof(ver), ver); |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1328 | |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1329 | return ret; |
| 1330 | } |
| 1331 | |
| 1332 | /** |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1333 | * tcg2_measure_variable() - add variable event log and extend PCR |
| 1334 | * |
| 1335 | * @dev: TPM device |
| 1336 | * @pcr_index: PCR index |
| 1337 | * @event_type: type of event added |
| 1338 | * @var_name: variable name |
| 1339 | * @guid: guid |
| 1340 | * @data_size: variable data size |
| 1341 | * @data: variable data |
| 1342 | * |
| 1343 | * Return: status code |
| 1344 | */ |
| 1345 | static efi_status_t tcg2_measure_variable(struct udevice *dev, u32 pcr_index, |
| 1346 | u32 event_type, u16 *var_name, |
| 1347 | const efi_guid_t *guid, |
| 1348 | efi_uintn_t data_size, u8 *data) |
| 1349 | { |
| 1350 | u32 event_size; |
| 1351 | efi_status_t ret; |
| 1352 | struct efi_tcg2_uefi_variable_data *event; |
| 1353 | |
| 1354 | event_size = sizeof(event->variable_name) + |
| 1355 | sizeof(event->unicode_name_length) + |
| 1356 | sizeof(event->variable_data_length) + |
| 1357 | (u16_strlen(var_name) * sizeof(u16)) + data_size; |
| 1358 | event = malloc(event_size); |
| 1359 | if (!event) |
| 1360 | return EFI_OUT_OF_RESOURCES; |
| 1361 | |
| 1362 | guidcpy(&event->variable_name, guid); |
| 1363 | event->unicode_name_length = u16_strlen(var_name); |
| 1364 | event->variable_data_length = data_size; |
| 1365 | memcpy(event->unicode_name, var_name, |
| 1366 | (event->unicode_name_length * sizeof(u16))); |
| 1367 | if (data) { |
| 1368 | memcpy((u16 *)event->unicode_name + event->unicode_name_length, |
| 1369 | data, data_size); |
| 1370 | } |
| 1371 | ret = tcg2_measure_event(dev, pcr_index, event_type, event_size, |
| 1372 | (u8 *)event); |
| 1373 | free(event); |
| 1374 | return ret; |
| 1375 | } |
| 1376 | |
| 1377 | /** |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1378 | * tcg2_measure_boot_variable() - measure boot variables |
| 1379 | * |
| 1380 | * @dev: TPM device |
| 1381 | * |
| 1382 | * Return: status code |
| 1383 | */ |
| 1384 | static efi_status_t tcg2_measure_boot_variable(struct udevice *dev) |
| 1385 | { |
| 1386 | u16 *boot_order; |
| 1387 | u16 *boot_index; |
| 1388 | u16 var_name[] = L"BootOrder"; |
| 1389 | u16 boot_name[] = L"Boot####"; |
| 1390 | u8 *bootvar; |
| 1391 | efi_uintn_t var_data_size; |
| 1392 | u32 count, i; |
| 1393 | efi_status_t ret; |
| 1394 | |
| 1395 | boot_order = efi_get_var(var_name, &efi_global_variable_guid, |
| 1396 | &var_data_size); |
| 1397 | if (!boot_order) { |
| 1398 | ret = EFI_NOT_FOUND; |
| 1399 | goto error; |
| 1400 | } |
| 1401 | |
| 1402 | ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name, |
| 1403 | &efi_global_variable_guid, var_data_size, |
| 1404 | (u8 *)boot_order); |
| 1405 | if (ret != EFI_SUCCESS) |
| 1406 | goto error; |
| 1407 | |
| 1408 | count = var_data_size / sizeof(*boot_order); |
| 1409 | boot_index = boot_order; |
| 1410 | for (i = 0; i < count; i++) { |
| 1411 | efi_create_indexed_name(boot_name, sizeof(boot_name), |
| 1412 | "Boot", *boot_index++); |
| 1413 | |
| 1414 | bootvar = efi_get_var(boot_name, &efi_global_variable_guid, |
| 1415 | &var_data_size); |
| 1416 | |
| 1417 | if (!bootvar) { |
| 1418 | log_info("%ls not found\n", boot_name); |
| 1419 | continue; |
| 1420 | } |
| 1421 | |
| 1422 | ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, |
| 1423 | boot_name, |
| 1424 | &efi_global_variable_guid, |
| 1425 | var_data_size, bootvar); |
| 1426 | free(bootvar); |
| 1427 | if (ret != EFI_SUCCESS) |
| 1428 | goto error; |
| 1429 | } |
| 1430 | |
| 1431 | error: |
| 1432 | free(boot_order); |
| 1433 | return ret; |
| 1434 | } |
| 1435 | |
| 1436 | /** |
| 1437 | * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation |
| 1438 | * |
| 1439 | * Return: status code |
| 1440 | */ |
| 1441 | efi_status_t efi_tcg2_measure_efi_app_invocation(void) |
| 1442 | { |
| 1443 | efi_status_t ret; |
| 1444 | u32 pcr_index; |
| 1445 | struct udevice *dev; |
| 1446 | u32 event = 0; |
| 1447 | |
| 1448 | if (tcg2_efi_app_invoked) |
| 1449 | return EFI_SUCCESS; |
| 1450 | |
| 1451 | ret = platform_get_tpm2_device(&dev); |
| 1452 | if (ret != EFI_SUCCESS) |
| 1453 | return ret; |
| 1454 | |
| 1455 | ret = tcg2_measure_boot_variable(dev); |
| 1456 | if (ret != EFI_SUCCESS) |
| 1457 | goto out; |
| 1458 | |
| 1459 | ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION, |
| 1460 | strlen(EFI_CALLING_EFI_APPLICATION), |
| 1461 | (u8 *)EFI_CALLING_EFI_APPLICATION); |
| 1462 | if (ret != EFI_SUCCESS) |
| 1463 | goto out; |
| 1464 | |
| 1465 | for (pcr_index = 0; pcr_index <= 7; pcr_index++) { |
| 1466 | ret = tcg2_measure_event(dev, pcr_index, EV_SEPARATOR, |
| 1467 | sizeof(event), (u8 *)&event); |
| 1468 | if (ret != EFI_SUCCESS) |
| 1469 | goto out; |
| 1470 | } |
| 1471 | |
| 1472 | tcg2_efi_app_invoked = true; |
| 1473 | out: |
| 1474 | return ret; |
| 1475 | } |
| 1476 | |
| 1477 | /** |
| 1478 | * efi_tcg2_measure_efi_app_exit() - measure efi app exit |
| 1479 | * |
| 1480 | * Return: status code |
| 1481 | */ |
| 1482 | efi_status_t efi_tcg2_measure_efi_app_exit(void) |
| 1483 | { |
| 1484 | efi_status_t ret; |
| 1485 | struct udevice *dev; |
| 1486 | |
| 1487 | ret = platform_get_tpm2_device(&dev); |
| 1488 | if (ret != EFI_SUCCESS) |
| 1489 | return ret; |
| 1490 | |
| 1491 | ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION, |
| 1492 | strlen(EFI_RETURNING_FROM_EFI_APPLICATION), |
| 1493 | (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION); |
| 1494 | return ret; |
| 1495 | } |
| 1496 | |
| 1497 | /** |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1498 | * efi_tcg2_notify_exit_boot_services() - ExitBootService callback |
| 1499 | * |
| 1500 | * @event: callback event |
| 1501 | * @context: callback context |
| 1502 | */ |
| 1503 | static void EFIAPI |
| 1504 | efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context) |
| 1505 | { |
| 1506 | efi_status_t ret; |
| 1507 | struct udevice *dev; |
| 1508 | |
| 1509 | EFI_ENTRY("%p, %p", event, context); |
| 1510 | |
| 1511 | ret = platform_get_tpm2_device(&dev); |
| 1512 | if (ret != EFI_SUCCESS) |
| 1513 | goto out; |
| 1514 | |
| 1515 | ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION, |
| 1516 | strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION), |
| 1517 | (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION); |
| 1518 | if (ret != EFI_SUCCESS) |
| 1519 | goto out; |
| 1520 | |
| 1521 | ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION, |
| 1522 | strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED), |
| 1523 | (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED); |
| 1524 | |
| 1525 | out: |
| 1526 | EFI_EXIT(ret); |
| 1527 | } |
| 1528 | |
| 1529 | /** |
| 1530 | * efi_tcg2_notify_exit_boot_services_failed() |
| 1531 | * - notify ExitBootServices() is failed |
| 1532 | * |
| 1533 | * Return: status code |
| 1534 | */ |
| 1535 | efi_status_t efi_tcg2_notify_exit_boot_services_failed(void) |
| 1536 | { |
| 1537 | struct udevice *dev; |
| 1538 | efi_status_t ret; |
| 1539 | |
| 1540 | ret = platform_get_tpm2_device(&dev); |
| 1541 | if (ret != EFI_SUCCESS) |
| 1542 | goto out; |
| 1543 | |
| 1544 | ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION, |
| 1545 | strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION), |
| 1546 | (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION); |
| 1547 | if (ret != EFI_SUCCESS) |
| 1548 | goto out; |
| 1549 | |
| 1550 | ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION, |
| 1551 | strlen(EFI_EXIT_BOOT_SERVICES_FAILED), |
| 1552 | (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED); |
| 1553 | |
| 1554 | out: |
| 1555 | return ret; |
| 1556 | } |
| 1557 | |
| 1558 | /** |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1559 | * tcg2_measure_secure_boot_variable() - measure secure boot variables |
| 1560 | * |
| 1561 | * @dev: TPM device |
| 1562 | * |
| 1563 | * Return: status code |
| 1564 | */ |
| 1565 | static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev) |
| 1566 | { |
| 1567 | u8 *data; |
| 1568 | efi_uintn_t data_size; |
| 1569 | u32 count, i; |
| 1570 | efi_status_t ret; |
| 1571 | |
| 1572 | count = ARRAY_SIZE(secure_variables); |
| 1573 | for (i = 0; i < count; i++) { |
| 1574 | /* |
| 1575 | * According to the TCG2 PC Client PFP spec, "SecureBoot", |
| 1576 | * "PK", "KEK", "db" and "dbx" variables must be measured |
| 1577 | * even if they are empty. |
| 1578 | */ |
| 1579 | data = efi_get_var(secure_variables[i].name, |
| 1580 | secure_variables[i].guid, |
| 1581 | &data_size); |
| 1582 | |
| 1583 | ret = tcg2_measure_variable(dev, 7, |
| 1584 | EV_EFI_VARIABLE_DRIVER_CONFIG, |
| 1585 | secure_variables[i].name, |
| 1586 | secure_variables[i].guid, |
| 1587 | data_size, data); |
| 1588 | free(data); |
| 1589 | if (ret != EFI_SUCCESS) |
| 1590 | goto error; |
| 1591 | } |
| 1592 | |
| 1593 | /* |
| 1594 | * TCG2 PC Client PFP spec says "dbt" and "dbr" are |
| 1595 | * measured if present and not empty. |
| 1596 | */ |
| 1597 | data = efi_get_var(L"dbt", |
| 1598 | &efi_guid_image_security_database, |
| 1599 | &data_size); |
| 1600 | if (data) { |
| 1601 | ret = tcg2_measure_variable(dev, 7, |
| 1602 | EV_EFI_VARIABLE_DRIVER_CONFIG, |
| 1603 | L"dbt", |
| 1604 | &efi_guid_image_security_database, |
| 1605 | data_size, data); |
| 1606 | free(data); |
| 1607 | } |
| 1608 | |
| 1609 | data = efi_get_var(L"dbr", |
| 1610 | &efi_guid_image_security_database, |
| 1611 | &data_size); |
| 1612 | if (data) { |
| 1613 | ret = tcg2_measure_variable(dev, 7, |
| 1614 | EV_EFI_VARIABLE_DRIVER_CONFIG, |
| 1615 | L"dbr", |
| 1616 | &efi_guid_image_security_database, |
| 1617 | data_size, data); |
| 1618 | free(data); |
| 1619 | } |
| 1620 | |
| 1621 | error: |
| 1622 | return ret; |
| 1623 | } |
| 1624 | |
| 1625 | /** |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1626 | * efi_tcg2_register() - register EFI_TCG2_PROTOCOL |
| 1627 | * |
| 1628 | * If a TPM2 device is available, the TPM TCG2 Protocol is registered |
| 1629 | * |
| 1630 | * Return: An error status is only returned if adding the protocol fails. |
| 1631 | */ |
| 1632 | efi_status_t efi_tcg2_register(void) |
| 1633 | { |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1634 | efi_status_t ret = EFI_SUCCESS; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1635 | struct udevice *dev; |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1636 | struct efi_event *event; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1637 | |
| 1638 | ret = platform_get_tpm2_device(&dev); |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1639 | if (ret != EFI_SUCCESS) { |
| 1640 | log_warning("Unable to find TPMv2 device\n"); |
Ilias Apalodimas | fa5217d | 2021-05-10 21:19:14 +0300 | [diff] [blame] | 1641 | return EFI_SUCCESS; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1642 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1643 | |
| 1644 | ret = efi_init_event_log(); |
| 1645 | if (ret != EFI_SUCCESS) |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1646 | goto fail; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1647 | |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1648 | ret = efi_append_scrtm_version(dev); |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1649 | if (ret != EFI_SUCCESS) { |
| 1650 | tcg2_uninit(); |
Ilias Apalodimas | fa5217d | 2021-05-10 21:19:14 +0300 | [diff] [blame] | 1651 | goto fail; |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1652 | } |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1653 | |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1654 | ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol, |
| 1655 | (void *)&efi_tcg2_protocol); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1656 | if (ret != EFI_SUCCESS) { |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1657 | tcg2_uninit(); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1658 | goto fail; |
| 1659 | } |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1660 | |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 1661 | ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK, |
| 1662 | efi_tcg2_notify_exit_boot_services, NULL, |
| 1663 | NULL, &event); |
| 1664 | if (ret != EFI_SUCCESS) { |
| 1665 | tcg2_uninit(); |
| 1666 | goto fail; |
| 1667 | } |
| 1668 | |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1669 | ret = tcg2_measure_secure_boot_variable(dev); |
| 1670 | if (ret != EFI_SUCCESS) { |
| 1671 | tcg2_uninit(); |
| 1672 | goto fail; |
| 1673 | } |
| 1674 | |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1675 | return ret; |
Ilias Apalodimas | fa5217d | 2021-05-10 21:19:14 +0300 | [diff] [blame] | 1676 | |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1677 | fail: |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1678 | log_err("Cannot install EFI_TCG2_PROTOCOL\n"); |
| 1679 | /* |
| 1680 | * Return EFI_SUCCESS and don't stop the EFI subsystem. |
| 1681 | * That's done for 2 reasons |
| 1682 | * - If the protocol is not installed the PCRs won't be extended. So |
| 1683 | * someone later in the boot flow will notice that and take the |
| 1684 | * necessary actions. |
| 1685 | * - The TPM sandbox is limited and we won't be able to run any efi |
| 1686 | * related tests with TCG2 enabled |
| 1687 | */ |
| 1688 | return EFI_SUCCESS; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1689 | } |