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