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> |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 21 | #include <tpm_api.h> |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 22 | #include <u-boot/hash-checksum.h> |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 23 | #include <u-boot/sha1.h> |
| 24 | #include <u-boot/sha256.h> |
| 25 | #include <u-boot/sha512.h> |
Masahisa Kojima | d420d8d | 2021-11-03 11:04:09 +0900 | [diff] [blame] | 26 | #include <linux/unaligned/be_byteshift.h> |
| 27 | #include <linux/unaligned/le_byteshift.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 28 | #include <linux/unaligned/generic.h> |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 29 | #include <hexdump.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 30 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 31 | /** |
| 32 | * struct event_log_buffer - internal eventlog management structure |
| 33 | * |
| 34 | * @buffer: eventlog buffer |
| 35 | * @final_buffer: finalevent config table buffer |
| 36 | * @pos: current position of 'buffer' |
| 37 | * @final_pos: current position of 'final_buffer' |
| 38 | * @get_event_called: true if GetEventLog has been invoked at least once |
| 39 | * @ebs_called: true if ExitBootServices has been invoked |
| 40 | * @truncated: true if the 'buffer' is truncated |
| 41 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 42 | struct event_log_buffer { |
| 43 | void *buffer; |
| 44 | void *final_buffer; |
| 45 | size_t pos; /* eventlog position */ |
| 46 | size_t final_pos; /* final events config table position */ |
| 47 | size_t last_event_size; |
| 48 | bool get_event_called; |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 49 | bool ebs_called; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 50 | bool truncated; |
| 51 | }; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 52 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 53 | static struct event_log_buffer event_log; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 54 | static bool tcg2_efi_app_invoked; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 55 | /* |
| 56 | * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset. |
| 57 | * Since the current tpm2_get_capability() response buffers starts at |
| 58 | * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate |
| 59 | * the response size and offset once for all consumers |
| 60 | */ |
| 61 | #define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \ |
| 62 | offsetof(struct tpms_capability_data, data)) |
| 63 | #define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \ |
| 64 | offsetof(struct tpms_tagged_property, value)) |
| 65 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 66 | static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID; |
| 67 | static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID; |
| 68 | |
| 69 | struct digest_info { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 70 | u16 hash_alg; |
| 71 | u32 hash_mask; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 72 | u16 hash_len; |
| 73 | }; |
| 74 | |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 75 | static const struct digest_info hash_algo_list[] = { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 76 | { |
| 77 | TPM2_ALG_SHA1, |
| 78 | EFI_TCG2_BOOT_HASH_ALG_SHA1, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 79 | TPM2_SHA1_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 80 | }, |
| 81 | { |
| 82 | TPM2_ALG_SHA256, |
| 83 | EFI_TCG2_BOOT_HASH_ALG_SHA256, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 84 | TPM2_SHA256_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 85 | }, |
| 86 | { |
| 87 | TPM2_ALG_SHA384, |
| 88 | EFI_TCG2_BOOT_HASH_ALG_SHA384, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 89 | TPM2_SHA384_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 90 | }, |
| 91 | { |
| 92 | TPM2_ALG_SHA512, |
| 93 | EFI_TCG2_BOOT_HASH_ALG_SHA512, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 94 | TPM2_SHA512_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 95 | }, |
| 96 | }; |
| 97 | |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 98 | struct variable_info { |
| 99 | const u16 *name; |
| 100 | bool accept_empty; |
Masahisa Kojima | f3e0c55 | 2021-10-26 17:27:27 +0900 | [diff] [blame] | 101 | u32 pcr_index; |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 102 | }; |
| 103 | |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 104 | static struct variable_info secure_variables[] = { |
Masahisa Kojima | f3e0c55 | 2021-10-26 17:27:27 +0900 | [diff] [blame] | 105 | {u"SecureBoot", true, 7}, |
| 106 | {u"PK", true, 7}, |
| 107 | {u"KEK", true, 7}, |
| 108 | {u"db", true, 7}, |
| 109 | {u"dbx", true, 7}, |
| 110 | {u"dbt", false, 7}, |
| 111 | {u"dbr", false, 7}, |
| 112 | {u"DeployedMode", false, 1}, |
| 113 | {u"AuditMode", false, 1}, |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 114 | }; |
| 115 | |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 116 | #define MAX_HASH_COUNT ARRAY_SIZE(hash_algo_list) |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 117 | |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 118 | /** |
| 119 | * alg_to_mask - Get a TCG hash mask for algorithms |
| 120 | * |
| 121 | * @hash_alg: TCG defined algorithm |
| 122 | * |
| 123 | * @Return: TCG hashing algorithm bitmaps, 0 if the algorithm is not supported |
| 124 | */ |
| 125 | static u32 alg_to_mask(u16 hash_alg) |
| 126 | { |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 127 | size_t i; |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 128 | |
| 129 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 130 | if (hash_algo_list[i].hash_alg == hash_alg) |
| 131 | return hash_algo_list[i].hash_mask; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 137 | /** |
| 138 | * alg_to_len - Get a TCG hash len for algorithms |
| 139 | * |
| 140 | * @hash_alg: TCG defined algorithm |
| 141 | * |
| 142 | * @Return: len of chosen algorithm, 0 if the algorithm is not supported |
| 143 | */ |
| 144 | static u16 alg_to_len(u16 hash_alg) |
| 145 | { |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 146 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 147 | |
| 148 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 149 | if (hash_algo_list[i].hash_alg == hash_alg) |
| 150 | return hash_algo_list[i].hash_len; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 156 | static bool is_tcg2_protocol_installed(void) |
| 157 | { |
| 158 | struct efi_handler *handler; |
| 159 | efi_status_t ret; |
| 160 | |
| 161 | ret = efi_search_protocol(efi_root, &efi_guid_tcg2_protocol, &handler); |
| 162 | return ret == EFI_SUCCESS; |
| 163 | } |
| 164 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 165 | static u32 tcg_event_final_size(struct tpml_digest_values *digest_list) |
| 166 | { |
| 167 | u32 len; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 168 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 169 | |
| 170 | len = offsetof(struct tcg_pcr_event2, digests); |
| 171 | len += offsetof(struct tpml_digest_values, digests); |
| 172 | for (i = 0; i < digest_list->count; i++) { |
| 173 | u16 hash_alg = digest_list->digests[i].hash_alg; |
| 174 | |
| 175 | len += offsetof(struct tpmt_ha, digest); |
| 176 | len += alg_to_len(hash_alg); |
| 177 | } |
| 178 | len += sizeof(u32); /* tcg_pcr_event2 event_size*/ |
| 179 | |
| 180 | return len; |
| 181 | } |
| 182 | |
| 183 | /* tcg2_pcr_extend - Extend PCRs for a TPM2 device for a given tpml_digest_values |
| 184 | * |
| 185 | * @dev: device |
| 186 | * @digest_list: list of digest algorithms to extend |
| 187 | * |
| 188 | * @Return: status code |
| 189 | */ |
| 190 | static efi_status_t tcg2_pcr_extend(struct udevice *dev, u32 pcr_index, |
| 191 | struct tpml_digest_values *digest_list) |
| 192 | { |
| 193 | u32 rc; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 194 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 195 | |
| 196 | for (i = 0; i < digest_list->count; i++) { |
| 197 | u32 alg = digest_list->digests[i].hash_alg; |
| 198 | |
| 199 | rc = tpm2_pcr_extend(dev, pcr_index, alg, |
| 200 | (u8 *)&digest_list->digests[i].digest, |
| 201 | alg_to_len(alg)); |
| 202 | if (rc) { |
| 203 | EFI_PRINT("Failed to extend PCR\n"); |
| 204 | return EFI_DEVICE_ERROR; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return EFI_SUCCESS; |
| 209 | } |
| 210 | |
Ruchika Gupta | 9d0b5d0 | 2021-11-29 13:09:46 +0530 | [diff] [blame] | 211 | /* tcg2_pcr_read - Read PCRs for a TPM2 device for a given tpml_digest_values |
| 212 | * |
| 213 | * @dev: device |
| 214 | * @pcr_index: PCR index |
| 215 | * @digest_list: list of digest algorithms to extend |
| 216 | * |
| 217 | * @Return: status code |
| 218 | */ |
| 219 | static efi_status_t tcg2_pcr_read(struct udevice *dev, u32 pcr_index, |
| 220 | struct tpml_digest_values *digest_list) |
| 221 | { |
| 222 | struct tpm_chip_priv *priv; |
| 223 | unsigned int updates, pcr_select_min; |
| 224 | u32 rc; |
| 225 | size_t i; |
| 226 | |
| 227 | priv = dev_get_uclass_priv(dev); |
| 228 | if (!priv) |
| 229 | return EFI_DEVICE_ERROR; |
| 230 | |
| 231 | pcr_select_min = priv->pcr_select_min; |
| 232 | |
| 233 | for (i = 0; i < digest_list->count; i++) { |
| 234 | u16 hash_alg = digest_list->digests[i].hash_alg; |
| 235 | u8 *digest = (u8 *)&digest_list->digests[i].digest; |
| 236 | |
| 237 | rc = tpm2_pcr_read(dev, pcr_index, pcr_select_min, |
| 238 | hash_alg, digest, alg_to_len(hash_alg), |
| 239 | &updates); |
| 240 | if (rc) { |
| 241 | EFI_PRINT("Failed to read PCR\n"); |
| 242 | return EFI_DEVICE_ERROR; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | return EFI_SUCCESS; |
| 247 | } |
| 248 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 249 | /* put_event - Append an agile event to an eventlog |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 250 | * |
| 251 | * @pcr_index: PCR index |
| 252 | * @event_type: type of event added |
| 253 | * @digest_list: list of digest algorithms to add |
| 254 | * @size: size of event |
| 255 | * @event: event to add |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 256 | * @log: log buffer to append the event |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 257 | * |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 258 | */ |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 259 | static void put_event(u32 pcr_index, u32 event_type, |
| 260 | struct tpml_digest_values *digest_list, u32 size, |
| 261 | u8 event[], void *log) |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 262 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 263 | size_t pos; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 264 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 265 | u32 event_size; |
| 266 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 267 | /* |
| 268 | * size refers to the length of event[] only, we need to check against |
| 269 | * the final tcg_pcr_event2 size |
| 270 | */ |
| 271 | event_size = size + tcg_event_final_size(digest_list); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 272 | |
| 273 | put_unaligned_le32(pcr_index, log); |
| 274 | pos = offsetof(struct tcg_pcr_event2, event_type); |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 275 | put_unaligned_le32(event_type, (void *)((uintptr_t)log + pos)); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 276 | pos = offsetof(struct tcg_pcr_event2, digests); /* count */ |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 277 | put_unaligned_le32(digest_list->count, (void *)((uintptr_t)log + pos)); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 278 | |
| 279 | pos += offsetof(struct tpml_digest_values, digests); |
| 280 | for (i = 0; i < digest_list->count; i++) { |
| 281 | u16 hash_alg = digest_list->digests[i].hash_alg; |
| 282 | u8 *digest = (u8 *)&digest_list->digests[i].digest; |
| 283 | |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 284 | put_unaligned_le16(hash_alg, (void *)((uintptr_t)log + pos)); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 285 | pos += offsetof(struct tpmt_ha, digest); |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 286 | memcpy((void *)((uintptr_t)log + pos), digest, alg_to_len(hash_alg)); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 287 | pos += alg_to_len(hash_alg); |
| 288 | } |
| 289 | |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 290 | put_unaligned_le32(size, (void *)((uintptr_t)log + pos)); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 291 | pos += sizeof(u32); /* tcg_pcr_event2 event_size*/ |
Ilias Apalodimas | 4390e76 | 2021-03-30 00:42:36 +0300 | [diff] [blame] | 292 | memcpy((void *)((uintptr_t)log + pos), event, size); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 293 | pos += size; |
| 294 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 295 | /* |
| 296 | * make sure the calculated buffer is what we checked against |
| 297 | * This check should never fail. It checks the code above is |
| 298 | * calculating the right length for the event we are adding |
| 299 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 300 | if (pos != event_size) |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 301 | log_err("Appending to the EventLog failed\n"); |
| 302 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 303 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 304 | /* tcg2_agile_log_append - Append an agile event to an eventlog |
| 305 | * |
| 306 | * @pcr_index: PCR index |
| 307 | * @event_type: type of event added |
| 308 | * @digest_list: list of digest algorithms to add |
| 309 | * @size: size of event |
| 310 | * @event: event to add |
| 311 | * @log: log buffer to append the event |
| 312 | * |
| 313 | * @Return: status code |
| 314 | */ |
| 315 | static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type, |
| 316 | struct tpml_digest_values *digest_list, |
| 317 | u32 size, u8 event[]) |
| 318 | { |
| 319 | void *log = (void *)((uintptr_t)event_log.buffer + event_log.pos); |
| 320 | u32 event_size = size + tcg_event_final_size(digest_list); |
| 321 | struct efi_tcg2_final_events_table *final_event; |
| 322 | efi_status_t ret = EFI_SUCCESS; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 323 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 324 | /* if ExitBootServices hasn't been called update the normal log */ |
| 325 | if (!event_log.ebs_called) { |
| 326 | if (event_log.truncated || |
| 327 | event_log.pos + event_size > TPM2_EVENT_LOG_SIZE) { |
| 328 | event_log.truncated = true; |
| 329 | return EFI_VOLUME_FULL; |
| 330 | } |
| 331 | put_event(pcr_index, event_type, digest_list, size, event, log); |
| 332 | event_log.pos += event_size; |
| 333 | event_log.last_event_size = event_size; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 334 | } |
| 335 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 336 | if (!event_log.get_event_called) |
| 337 | return ret; |
| 338 | |
| 339 | /* if GetEventLog has been called update FinalEventLog as well */ |
| 340 | if (event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE) |
| 341 | return EFI_VOLUME_FULL; |
| 342 | |
| 343 | log = (void *)((uintptr_t)event_log.final_buffer + event_log.final_pos); |
| 344 | put_event(pcr_index, event_type, digest_list, size, event, log); |
| 345 | |
| 346 | final_event = event_log.final_buffer; |
| 347 | final_event->number_of_events++; |
| 348 | event_log.final_pos += event_size; |
| 349 | |
| 350 | return ret; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 351 | } |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 352 | |
| 353 | /** |
| 354 | * platform_get_tpm_device() - retrieve TPM device |
| 355 | * |
| 356 | * This function retrieves the udevice implementing a TPM |
| 357 | * |
| 358 | * This function may be overridden if special initialization is needed. |
| 359 | * |
| 360 | * @dev: udevice |
| 361 | * Return: status code |
| 362 | */ |
| 363 | __weak efi_status_t platform_get_tpm2_device(struct udevice **dev) |
| 364 | { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 365 | for_each_tpm_device(*dev) { |
| 366 | /* Only support TPMv2 devices */ |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 367 | if (tpm_get_version(*dev) == TPM_V2) |
| 368 | return EFI_SUCCESS; |
| 369 | } |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 370 | |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 371 | return EFI_NOT_FOUND; |
| 372 | } |
| 373 | |
| 374 | /** |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 375 | * platform_get_eventlog() - retrieve the eventlog address and size |
| 376 | * |
| 377 | * This function retrieves the eventlog address and size if the underlying |
| 378 | * firmware has done some measurements and passed them. |
| 379 | * |
| 380 | * This function may be overridden based on platform specific method of |
| 381 | * passing the eventlog address and size. |
| 382 | * |
| 383 | * @dev: udevice |
| 384 | * @addr: eventlog address |
| 385 | * @sz: eventlog size |
| 386 | * Return: status code |
| 387 | */ |
| 388 | __weak efi_status_t platform_get_eventlog(struct udevice *dev, u64 *addr, |
| 389 | u32 *sz) |
| 390 | { |
| 391 | const u64 *basep; |
| 392 | const u32 *sizep; |
| 393 | |
| 394 | basep = dev_read_prop(dev, "tpm_event_log_addr", NULL); |
| 395 | if (!basep) |
| 396 | return EFI_NOT_FOUND; |
| 397 | |
| 398 | *addr = be64_to_cpup((__force __be64 *)basep); |
| 399 | |
| 400 | sizep = dev_read_prop(dev, "tpm_event_log_size", NULL); |
| 401 | if (!sizep) |
| 402 | return EFI_NOT_FOUND; |
| 403 | |
| 404 | *sz = be32_to_cpup((__force __be32 *)sizep); |
| 405 | if (*sz == 0) { |
| 406 | log_debug("event log empty\n"); |
| 407 | return EFI_NOT_FOUND; |
| 408 | } |
| 409 | |
| 410 | return EFI_SUCCESS; |
| 411 | } |
| 412 | |
| 413 | /** |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 414 | * tpm2_get_max_command_size() - get the supported max command size |
| 415 | * |
| 416 | * @dev: TPM device |
| 417 | * @max_command_size: output buffer for the size |
| 418 | * |
| 419 | * Return: 0 on success, -1 on error |
| 420 | */ |
| 421 | static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size) |
| 422 | { |
| 423 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 424 | u32 ret; |
| 425 | |
| 426 | memset(response, 0, sizeof(response)); |
| 427 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 428 | TPM2_PT_MAX_COMMAND_SIZE, response, 1); |
| 429 | if (ret) |
| 430 | return -1; |
| 431 | |
| 432 | *max_command_size = (uint16_t)get_unaligned_be32(response + |
| 433 | properties_offset); |
| 434 | |
| 435 | return 0; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * tpm2_get_max_response_size() - get the supported max response size |
| 440 | * |
| 441 | * @dev: TPM device |
| 442 | * @max_response_size: output buffer for the size |
| 443 | * |
| 444 | * Return: 0 on success, -1 on error |
| 445 | */ |
| 446 | static int tpm2_get_max_response_size(struct udevice *dev, |
| 447 | u16 *max_response_size) |
| 448 | { |
| 449 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 450 | u32 ret; |
| 451 | |
| 452 | memset(response, 0, sizeof(response)); |
| 453 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 454 | TPM2_PT_MAX_RESPONSE_SIZE, response, 1); |
| 455 | if (ret) |
| 456 | return -1; |
| 457 | |
| 458 | *max_response_size = (uint16_t)get_unaligned_be32(response + |
| 459 | properties_offset); |
| 460 | |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * tpm2_get_manufacturer_id() - get the manufacturer ID |
| 466 | * |
| 467 | * @dev: TPM device |
| 468 | * @manufacturer_id: output buffer for the id |
| 469 | * |
| 470 | * Return: 0 on success, -1 on error |
| 471 | */ |
| 472 | static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id) |
| 473 | { |
| 474 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 475 | u32 ret; |
| 476 | |
| 477 | memset(response, 0, sizeof(response)); |
| 478 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 479 | TPM2_PT_MANUFACTURER, response, 1); |
| 480 | if (ret) |
| 481 | return -1; |
| 482 | |
| 483 | *manufacturer_id = get_unaligned_be32(response + properties_offset); |
| 484 | |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * tpm2_get_num_pcr() - get the number of PCRs |
| 490 | * |
| 491 | * @dev: TPM device |
| 492 | * @manufacturer_id: output buffer for the number |
| 493 | * |
| 494 | * Return: 0 on success, -1 on error |
| 495 | */ |
| 496 | static int tpm2_get_num_pcr(struct udevice *dev, u32 *num_pcr) |
| 497 | { |
| 498 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 499 | u32 ret; |
| 500 | |
| 501 | memset(response, 0, sizeof(response)); |
| 502 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 503 | TPM2_PT_PCR_COUNT, response, 1); |
| 504 | if (ret) |
| 505 | return -1; |
| 506 | |
| 507 | *num_pcr = get_unaligned_be32(response + properties_offset); |
| 508 | if (*num_pcr > TPM2_MAX_PCRS) |
| 509 | return -1; |
| 510 | |
| 511 | return 0; |
| 512 | } |
| 513 | |
| 514 | /** |
| 515 | * is_active_pcr() - Check if a supported algorithm is active |
| 516 | * |
| 517 | * @dev: TPM device |
| 518 | * @selection: struct of PCR information |
| 519 | * |
| 520 | * Return: true if PCR is active |
| 521 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 522 | static bool is_active_pcr(struct tpms_pcr_selection *selection) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 523 | { |
| 524 | int i; |
| 525 | /* |
| 526 | * check the pcr_select. If at least one of the PCRs supports the |
| 527 | * algorithm add it on the active ones |
| 528 | */ |
| 529 | for (i = 0; i < selection->size_of_select; i++) { |
| 530 | if (selection->pcr_select[i]) |
| 531 | return true; |
| 532 | } |
| 533 | |
| 534 | return false; |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks |
| 539 | * |
| 540 | * @dev: TPM device |
| 541 | * @supported_pcr: bitmask with the algorithms supported |
| 542 | * @active_pcr: bitmask with the active algorithms |
| 543 | * @pcr_banks: number of PCR banks |
| 544 | * |
| 545 | * Return: 0 on success, -1 on error |
| 546 | */ |
| 547 | static int tpm2_get_pcr_info(struct udevice *dev, u32 *supported_pcr, |
| 548 | u32 *active_pcr, u32 *pcr_banks) |
| 549 | { |
| 550 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 551 | struct tpml_pcr_selection pcrs; |
| 552 | u32 ret, num_pcr; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 553 | size_t i; |
| 554 | int tpm_ret; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 555 | |
Ilias Apalodimas | 09402b1 | 2021-05-26 21:01:00 +0300 | [diff] [blame] | 556 | *supported_pcr = 0; |
| 557 | *active_pcr = 0; |
| 558 | *pcr_banks = 0; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 559 | memset(response, 0, sizeof(response)); |
| 560 | ret = tpm2_get_capability(dev, TPM2_CAP_PCRS, 0, response, 1); |
| 561 | if (ret) |
| 562 | goto out; |
| 563 | |
| 564 | pcrs.count = get_unaligned_be32(response); |
| 565 | /* |
| 566 | * We only support 5 algorithms for now so check against that |
| 567 | * instead of TPM2_NUM_PCR_BANKS |
| 568 | */ |
| 569 | if (pcrs.count > MAX_HASH_COUNT || pcrs.count < 1) |
| 570 | goto out; |
| 571 | |
| 572 | tpm_ret = tpm2_get_num_pcr(dev, &num_pcr); |
| 573 | if (tpm_ret) |
| 574 | goto out; |
| 575 | |
| 576 | for (i = 0; i < pcrs.count; i++) { |
| 577 | /* |
| 578 | * Definition of TPMS_PCR_SELECTION Structure |
| 579 | * hash: u16 |
| 580 | * size_of_select: u8 |
| 581 | * pcr_select: u8 array |
| 582 | * |
| 583 | * The offsets depend on the number of the device PCRs |
| 584 | * so we have to calculate them based on that |
| 585 | */ |
| 586 | u32 hash_offset = offsetof(struct tpml_pcr_selection, selection) + |
| 587 | i * offsetof(struct tpms_pcr_selection, pcr_select) + |
| 588 | i * ((num_pcr + 7) / 8); |
| 589 | u32 size_select_offset = |
| 590 | hash_offset + offsetof(struct tpms_pcr_selection, |
| 591 | size_of_select); |
| 592 | u32 pcr_select_offset = |
| 593 | hash_offset + offsetof(struct tpms_pcr_selection, |
| 594 | pcr_select); |
| 595 | |
| 596 | pcrs.selection[i].hash = |
| 597 | get_unaligned_be16(response + hash_offset); |
| 598 | pcrs.selection[i].size_of_select = |
| 599 | __get_unaligned_be(response + size_select_offset); |
| 600 | if (pcrs.selection[i].size_of_select > TPM2_PCR_SELECT_MAX) |
| 601 | goto out; |
| 602 | /* copy the array of pcr_select */ |
| 603 | memcpy(pcrs.selection[i].pcr_select, response + pcr_select_offset, |
| 604 | pcrs.selection[i].size_of_select); |
| 605 | } |
| 606 | |
| 607 | for (i = 0; i < pcrs.count; i++) { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 608 | u32 hash_mask = alg_to_mask(pcrs.selection[i].hash); |
| 609 | |
| 610 | if (hash_mask) { |
| 611 | *supported_pcr |= hash_mask; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 612 | if (is_active_pcr(&pcrs.selection[i])) |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 613 | *active_pcr |= hash_mask; |
| 614 | } else { |
| 615 | EFI_PRINT("Unknown algorithm %x\n", pcrs.selection[i].hash); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | *pcr_banks = pcrs.count; |
| 620 | |
| 621 | return 0; |
| 622 | out: |
| 623 | return -1; |
| 624 | } |
| 625 | |
| 626 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 627 | * __get_active_pcr_banks() - returns the currently active PCR banks |
| 628 | * |
| 629 | * @active_pcr_banks: pointer for receiving the bitmap of currently |
| 630 | * active PCR banks |
| 631 | * |
| 632 | * Return: status code |
| 633 | */ |
| 634 | static efi_status_t __get_active_pcr_banks(u32 *active_pcr_banks) |
| 635 | { |
| 636 | struct udevice *dev; |
Ilias Apalodimas | 09402b1 | 2021-05-26 21:01:00 +0300 | [diff] [blame] | 637 | u32 active = 0, supported = 0, pcr_banks = 0; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 638 | efi_status_t ret; |
| 639 | int err; |
| 640 | |
| 641 | ret = platform_get_tpm2_device(&dev); |
| 642 | if (ret != EFI_SUCCESS) |
| 643 | goto out; |
| 644 | |
| 645 | err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_banks); |
| 646 | if (err) { |
| 647 | ret = EFI_DEVICE_ERROR; |
| 648 | goto out; |
| 649 | } |
| 650 | |
| 651 | *active_pcr_banks = active; |
| 652 | |
| 653 | out: |
| 654 | return ret; |
| 655 | } |
| 656 | |
| 657 | /* tcg2_create_digest - create a list of digests of the supported PCR banks |
| 658 | * for a given memory range |
| 659 | * |
| 660 | * @input: input memory |
| 661 | * @length: length of buffer to calculate the digest |
| 662 | * @digest_list: list of digests to fill in |
| 663 | * |
| 664 | * Return: status code |
| 665 | */ |
| 666 | static efi_status_t tcg2_create_digest(const u8 *input, u32 length, |
| 667 | struct tpml_digest_values *digest_list) |
| 668 | { |
| 669 | sha1_context ctx; |
| 670 | sha256_context ctx_256; |
| 671 | sha512_context ctx_512; |
Masahisa Kojima | eb74a90 | 2021-04-14 11:55:49 +0900 | [diff] [blame] | 672 | u8 final[TPM2_SHA512_DIGEST_SIZE]; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 673 | efi_status_t ret; |
| 674 | u32 active; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 675 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 676 | |
| 677 | ret = __get_active_pcr_banks(&active); |
| 678 | if (ret != EFI_SUCCESS) |
| 679 | return ret; |
| 680 | |
| 681 | digest_list->count = 0; |
| 682 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 683 | u16 hash_alg = hash_algo_list[i].hash_alg; |
| 684 | |
| 685 | if (!(active & alg_to_mask(hash_alg))) |
| 686 | continue; |
| 687 | switch (hash_alg) { |
| 688 | case TPM2_ALG_SHA1: |
| 689 | sha1_starts(&ctx); |
| 690 | sha1_update(&ctx, input, length); |
| 691 | sha1_finish(&ctx, final); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 692 | break; |
| 693 | case TPM2_ALG_SHA256: |
| 694 | sha256_starts(&ctx_256); |
| 695 | sha256_update(&ctx_256, input, length); |
| 696 | sha256_finish(&ctx_256, final); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 697 | break; |
| 698 | case TPM2_ALG_SHA384: |
| 699 | sha384_starts(&ctx_512); |
| 700 | sha384_update(&ctx_512, input, length); |
| 701 | sha384_finish(&ctx_512, final); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 702 | break; |
| 703 | case TPM2_ALG_SHA512: |
| 704 | sha512_starts(&ctx_512); |
| 705 | sha512_update(&ctx_512, input, length); |
| 706 | sha512_finish(&ctx_512, final); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 707 | break; |
| 708 | default: |
Heinrich Schuchardt | 09ec9f7 | 2023-07-31 14:11:34 +0200 | [diff] [blame] | 709 | continue; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 710 | } |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 711 | digest_list->digests[digest_list->count].hash_alg = hash_alg; |
| 712 | memcpy(&digest_list->digests[digest_list->count].digest, final, |
| 713 | (u32)alg_to_len(hash_alg)); |
Ilias Apalodimas | 754b3a4 | 2021-04-22 14:32:14 +0300 | [diff] [blame] | 714 | digest_list->count++; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | return EFI_SUCCESS; |
| 718 | } |
| 719 | |
| 720 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 721 | * efi_tcg2_get_capability() - protocol capability information and state information |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 722 | * |
| 723 | * @this: TCG2 protocol instance |
| 724 | * @capability: caller allocated memory with size field to the size of |
| 725 | * the structure allocated |
| 726 | |
| 727 | * Return: status code |
| 728 | */ |
| 729 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 730 | efi_tcg2_get_capability(struct efi_tcg2_protocol *this, |
| 731 | struct efi_tcg2_boot_service_capability *capability) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 732 | { |
| 733 | struct udevice *dev; |
| 734 | efi_status_t efi_ret; |
| 735 | int ret; |
| 736 | |
| 737 | EFI_ENTRY("%p, %p", this, capability); |
| 738 | |
| 739 | if (!this || !capability) { |
| 740 | efi_ret = EFI_INVALID_PARAMETER; |
| 741 | goto out; |
| 742 | } |
| 743 | |
Masahisa Kojima | 9cc8293 | 2021-09-06 12:04:12 +0900 | [diff] [blame] | 744 | if (capability->size < BOOT_SERVICE_CAPABILITY_MIN) { |
| 745 | capability->size = BOOT_SERVICE_CAPABILITY_MIN; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 746 | efi_ret = EFI_BUFFER_TOO_SMALL; |
| 747 | goto out; |
| 748 | } |
| 749 | |
| 750 | if (capability->size < sizeof(*capability)) { |
| 751 | capability->size = sizeof(*capability); |
| 752 | efi_ret = EFI_BUFFER_TOO_SMALL; |
| 753 | goto out; |
| 754 | } |
| 755 | |
| 756 | capability->structure_version.major = 1; |
| 757 | capability->structure_version.minor = 1; |
| 758 | capability->protocol_version.major = 1; |
| 759 | capability->protocol_version.minor = 1; |
| 760 | |
| 761 | efi_ret = platform_get_tpm2_device(&dev); |
| 762 | if (efi_ret != EFI_SUCCESS) { |
| 763 | capability->supported_event_logs = 0; |
| 764 | capability->hash_algorithm_bitmap = 0; |
| 765 | capability->tpm_present_flag = false; |
| 766 | capability->max_command_size = 0; |
| 767 | capability->max_response_size = 0; |
| 768 | capability->manufacturer_id = 0; |
| 769 | capability->number_of_pcr_banks = 0; |
| 770 | capability->active_pcr_banks = 0; |
| 771 | |
| 772 | efi_ret = EFI_SUCCESS; |
| 773 | goto out; |
| 774 | } |
| 775 | |
| 776 | /* We only allow a TPMv2 device to register the EFI protocol */ |
| 777 | capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2; |
| 778 | |
| 779 | capability->tpm_present_flag = true; |
| 780 | |
| 781 | /* Supported and active PCRs */ |
| 782 | capability->hash_algorithm_bitmap = 0; |
| 783 | capability->active_pcr_banks = 0; |
| 784 | ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap, |
| 785 | &capability->active_pcr_banks, |
| 786 | &capability->number_of_pcr_banks); |
| 787 | if (ret) { |
| 788 | efi_ret = EFI_DEVICE_ERROR; |
| 789 | goto out; |
| 790 | } |
| 791 | |
| 792 | /* Max command size */ |
| 793 | ret = tpm2_get_max_command_size(dev, &capability->max_command_size); |
| 794 | if (ret) { |
| 795 | efi_ret = EFI_DEVICE_ERROR; |
| 796 | goto out; |
| 797 | } |
| 798 | |
| 799 | /* Max response size */ |
| 800 | ret = tpm2_get_max_response_size(dev, &capability->max_response_size); |
| 801 | if (ret) { |
| 802 | efi_ret = EFI_DEVICE_ERROR; |
| 803 | goto out; |
| 804 | } |
| 805 | |
| 806 | /* Manufacturer ID */ |
| 807 | ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id); |
| 808 | if (ret) { |
| 809 | efi_ret = EFI_DEVICE_ERROR; |
| 810 | goto out; |
| 811 | } |
| 812 | |
| 813 | return EFI_EXIT(EFI_SUCCESS); |
| 814 | out: |
| 815 | return EFI_EXIT(efi_ret); |
| 816 | } |
| 817 | |
| 818 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 819 | * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its |
| 820 | * last entry |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 821 | * |
| 822 | * @this: TCG2 protocol instance |
| 823 | * @log_format: type of event log format |
| 824 | * @event_log_location: pointer to the memory address of the event log |
| 825 | * @event_log_last_entry: pointer to the address of the start of the last |
| 826 | * entry in the event log in memory, if log contains |
| 827 | * more than 1 entry |
| 828 | * @event_log_truncated: set to true, if the Event Log is missing at i |
| 829 | * least one entry |
| 830 | * |
| 831 | * Return: status code |
| 832 | */ |
| 833 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 834 | efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this, |
| 835 | efi_tcg_event_log_format log_format, |
| 836 | u64 *event_log_location, u64 *event_log_last_entry, |
| 837 | bool *event_log_truncated) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 838 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 839 | efi_status_t ret = EFI_SUCCESS; |
| 840 | struct udevice *dev; |
| 841 | |
| 842 | EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location, |
| 843 | event_log_last_entry, event_log_truncated); |
| 844 | |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 845 | if (!this || !event_log_location || !event_log_last_entry || |
| 846 | !event_log_truncated) { |
| 847 | ret = EFI_INVALID_PARAMETER; |
| 848 | goto out; |
| 849 | } |
| 850 | |
| 851 | /* Only support TPMV2 */ |
| 852 | if (log_format != TCG2_EVENT_LOG_FORMAT_TCG_2) { |
| 853 | ret = EFI_INVALID_PARAMETER; |
| 854 | goto out; |
| 855 | } |
| 856 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 857 | ret = platform_get_tpm2_device(&dev); |
| 858 | if (ret != EFI_SUCCESS) { |
| 859 | event_log_location = NULL; |
| 860 | event_log_last_entry = NULL; |
| 861 | *event_log_truncated = false; |
| 862 | ret = EFI_SUCCESS; |
| 863 | goto out; |
| 864 | } |
| 865 | *event_log_location = (uintptr_t)event_log.buffer; |
| 866 | *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos - |
| 867 | event_log.last_event_size); |
| 868 | *event_log_truncated = event_log.truncated; |
| 869 | event_log.get_event_called = true; |
| 870 | |
| 871 | out: |
| 872 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | /** |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 876 | * tcg2_hash_pe_image() - calculate PE/COFF image hash |
| 877 | * |
| 878 | * @efi: pointer to the EFI binary |
| 879 | * @efi_size: size of @efi binary |
| 880 | * @digest_list: list of digest algorithms to extend |
| 881 | * |
| 882 | * Return: status code |
| 883 | */ |
| 884 | static efi_status_t tcg2_hash_pe_image(void *efi, u64 efi_size, |
| 885 | struct tpml_digest_values *digest_list) |
| 886 | { |
| 887 | WIN_CERTIFICATE *wincerts = NULL; |
| 888 | size_t wincerts_len; |
| 889 | struct efi_image_regions *regs = NULL; |
| 890 | void *new_efi = NULL; |
| 891 | u8 hash[TPM2_SHA512_DIGEST_SIZE]; |
| 892 | efi_status_t ret; |
| 893 | u32 active; |
| 894 | int i; |
| 895 | |
| 896 | new_efi = efi_prepare_aligned_image(efi, &efi_size); |
| 897 | if (!new_efi) |
| 898 | return EFI_OUT_OF_RESOURCES; |
| 899 | |
| 900 | if (!efi_image_parse(new_efi, efi_size, ®s, &wincerts, |
| 901 | &wincerts_len)) { |
| 902 | log_err("Parsing PE executable image failed\n"); |
| 903 | ret = EFI_UNSUPPORTED; |
| 904 | goto out; |
| 905 | } |
| 906 | |
| 907 | ret = __get_active_pcr_banks(&active); |
| 908 | if (ret != EFI_SUCCESS) { |
| 909 | goto out; |
| 910 | } |
| 911 | |
| 912 | digest_list->count = 0; |
| 913 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 914 | u16 hash_alg = hash_algo_list[i].hash_alg; |
| 915 | |
| 916 | if (!(active & alg_to_mask(hash_alg))) |
| 917 | continue; |
| 918 | switch (hash_alg) { |
| 919 | case TPM2_ALG_SHA1: |
| 920 | hash_calculate("sha1", regs->reg, regs->num, hash); |
| 921 | break; |
| 922 | case TPM2_ALG_SHA256: |
| 923 | hash_calculate("sha256", regs->reg, regs->num, hash); |
| 924 | break; |
| 925 | case TPM2_ALG_SHA384: |
| 926 | hash_calculate("sha384", regs->reg, regs->num, hash); |
| 927 | break; |
| 928 | case TPM2_ALG_SHA512: |
| 929 | hash_calculate("sha512", regs->reg, regs->num, hash); |
| 930 | break; |
| 931 | default: |
Heinrich Schuchardt | 09ec9f7 | 2023-07-31 14:11:34 +0200 | [diff] [blame] | 932 | continue; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 933 | } |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 934 | digest_list->digests[digest_list->count].hash_alg = hash_alg; |
| 935 | memcpy(&digest_list->digests[digest_list->count].digest, hash, |
| 936 | (u32)alg_to_len(hash_alg)); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 937 | digest_list->count++; |
| 938 | } |
| 939 | |
| 940 | out: |
| 941 | if (new_efi != efi) |
| 942 | free(new_efi); |
| 943 | free(regs); |
| 944 | |
| 945 | return ret; |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * tcg2_measure_pe_image() - measure PE/COFF image |
| 950 | * |
| 951 | * @efi: pointer to the EFI binary |
| 952 | * @efi_size: size of @efi binary |
| 953 | * @handle: loaded image handle |
| 954 | * @loaded_image: loaded image protocol |
| 955 | * |
| 956 | * Return: status code |
| 957 | */ |
| 958 | efi_status_t tcg2_measure_pe_image(void *efi, u64 efi_size, |
| 959 | struct efi_loaded_image_obj *handle, |
| 960 | struct efi_loaded_image *loaded_image) |
| 961 | { |
| 962 | struct tpml_digest_values digest_list; |
| 963 | efi_status_t ret; |
| 964 | struct udevice *dev; |
| 965 | u32 pcr_index, event_type, event_size; |
| 966 | struct uefi_image_load_event *image_load_event; |
| 967 | struct efi_device_path *device_path; |
| 968 | u32 device_path_length; |
| 969 | IMAGE_DOS_HEADER *dos; |
| 970 | IMAGE_NT_HEADERS32 *nt; |
| 971 | struct efi_handler *handler; |
| 972 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 973 | if (!is_tcg2_protocol_installed()) |
| 974 | return EFI_SUCCESS; |
| 975 | |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 976 | ret = platform_get_tpm2_device(&dev); |
| 977 | if (ret != EFI_SUCCESS) |
Masahisa Kojima | 38155ea | 2021-12-07 14:15:33 +0900 | [diff] [blame] | 978 | return EFI_SECURITY_VIOLATION; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 979 | |
| 980 | switch (handle->image_type) { |
| 981 | case IMAGE_SUBSYSTEM_EFI_APPLICATION: |
| 982 | pcr_index = 4; |
| 983 | event_type = EV_EFI_BOOT_SERVICES_APPLICATION; |
| 984 | break; |
| 985 | case IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER: |
| 986 | pcr_index = 2; |
| 987 | event_type = EV_EFI_BOOT_SERVICES_DRIVER; |
| 988 | break; |
| 989 | case IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER: |
| 990 | pcr_index = 2; |
| 991 | event_type = EV_EFI_RUNTIME_SERVICES_DRIVER; |
| 992 | break; |
| 993 | default: |
| 994 | return EFI_UNSUPPORTED; |
| 995 | } |
| 996 | |
| 997 | ret = tcg2_hash_pe_image(efi, efi_size, &digest_list); |
| 998 | if (ret != EFI_SUCCESS) |
| 999 | return ret; |
| 1000 | |
| 1001 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 1002 | if (ret != EFI_SUCCESS) |
| 1003 | return ret; |
| 1004 | |
Ilias Apalodimas | 26753c0 | 2021-09-09 00:30:49 +0300 | [diff] [blame] | 1005 | ret = efi_search_protocol(&handle->header, |
| 1006 | &efi_guid_loaded_image_device_path, &handler); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 1007 | if (ret != EFI_SUCCESS) |
| 1008 | return ret; |
| 1009 | |
Ilias Apalodimas | 26753c0 | 2021-09-09 00:30:49 +0300 | [diff] [blame] | 1010 | device_path = handler->protocol_interface; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 1011 | device_path_length = efi_dp_size(device_path); |
| 1012 | if (device_path_length > 0) { |
| 1013 | /* add end node size */ |
| 1014 | device_path_length += sizeof(struct efi_device_path); |
| 1015 | } |
| 1016 | event_size = sizeof(struct uefi_image_load_event) + device_path_length; |
Ilias Apalodimas | 26753c0 | 2021-09-09 00:30:49 +0300 | [diff] [blame] | 1017 | image_load_event = calloc(1, event_size); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 1018 | if (!image_load_event) |
| 1019 | return EFI_OUT_OF_RESOURCES; |
| 1020 | |
| 1021 | image_load_event->image_location_in_memory = (uintptr_t)efi; |
| 1022 | image_load_event->image_length_in_memory = efi_size; |
| 1023 | image_load_event->length_of_device_path = device_path_length; |
| 1024 | |
| 1025 | dos = (IMAGE_DOS_HEADER *)efi; |
| 1026 | nt = (IMAGE_NT_HEADERS32 *)(efi + dos->e_lfanew); |
| 1027 | if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) { |
| 1028 | IMAGE_NT_HEADERS64 *nt64 = (IMAGE_NT_HEADERS64 *)nt; |
| 1029 | |
| 1030 | image_load_event->image_link_time_address = |
| 1031 | nt64->OptionalHeader.ImageBase; |
| 1032 | } else if (nt->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) { |
| 1033 | image_load_event->image_link_time_address = |
| 1034 | nt->OptionalHeader.ImageBase; |
| 1035 | } else { |
| 1036 | ret = EFI_INVALID_PARAMETER; |
| 1037 | goto out; |
| 1038 | } |
| 1039 | |
Ilias Apalodimas | 26753c0 | 2021-09-09 00:30:49 +0300 | [diff] [blame] | 1040 | /* device_path_length might be zero */ |
| 1041 | memcpy(image_load_event->device_path, device_path, device_path_length); |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 1042 | |
| 1043 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 1044 | event_size, (u8 *)image_load_event); |
| 1045 | |
| 1046 | out: |
| 1047 | free(image_load_event); |
| 1048 | |
| 1049 | return ret; |
| 1050 | } |
| 1051 | |
| 1052 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1053 | * efi_tcg2_hash_log_extend_event() - extend and optionally log events |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1054 | * |
| 1055 | * @this: TCG2 protocol instance |
| 1056 | * @flags: bitmap providing additional information on the |
| 1057 | * operation |
| 1058 | * @data_to_hash: physical address of the start of the data buffer |
| 1059 | * to be hashed |
| 1060 | * @data_to_hash_len: the length in bytes of the buffer referenced by |
| 1061 | * data_to_hash |
| 1062 | * @efi_tcg_event: pointer to data buffer containing information |
| 1063 | * about the event |
| 1064 | * |
| 1065 | * Return: status code |
| 1066 | */ |
| 1067 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1068 | efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags, |
| 1069 | u64 data_to_hash, u64 data_to_hash_len, |
| 1070 | struct efi_tcg2_event *efi_tcg_event) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1071 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1072 | struct udevice *dev; |
| 1073 | efi_status_t ret; |
| 1074 | u32 event_type, pcr_index, event_size; |
| 1075 | struct tpml_digest_values digest_list; |
| 1076 | |
| 1077 | EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash, |
| 1078 | data_to_hash_len, efi_tcg_event); |
| 1079 | |
| 1080 | if (!this || !data_to_hash || !efi_tcg_event) { |
| 1081 | ret = EFI_INVALID_PARAMETER; |
| 1082 | goto out; |
| 1083 | } |
| 1084 | |
| 1085 | ret = platform_get_tpm2_device(&dev); |
| 1086 | if (ret != EFI_SUCCESS) |
| 1087 | goto out; |
| 1088 | |
| 1089 | if (efi_tcg_event->size < efi_tcg_event->header.header_size + |
| 1090 | sizeof(u32)) { |
| 1091 | ret = EFI_INVALID_PARAMETER; |
| 1092 | goto out; |
| 1093 | } |
| 1094 | |
Masahisa Kojima | b807491 | 2021-09-03 10:55:52 +0900 | [diff] [blame] | 1095 | if (efi_tcg_event->header.pcr_index > EFI_TCG2_MAX_PCR_INDEX) { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1096 | ret = EFI_INVALID_PARAMETER; |
| 1097 | goto out; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * if PE_COFF_IMAGE is set we need to make sure the image is not |
| 1102 | * corrupted, verify it and hash the PE/COFF image in accordance with |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 1103 | * the procedure specified in "Calculating the PE Image Hash" |
| 1104 | * section of the "Windows Authenticode Portable Executable Signature |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1105 | * Format" |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1106 | */ |
| 1107 | if (flags & PE_COFF_IMAGE) { |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 1108 | IMAGE_NT_HEADERS32 *nt; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1109 | |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 1110 | ret = efi_check_pe((void *)(uintptr_t)data_to_hash, |
| 1111 | data_to_hash_len, (void **)&nt); |
| 1112 | if (ret != EFI_SUCCESS) { |
| 1113 | log_err("Not a valid PE-COFF file\n"); |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 1114 | ret = EFI_UNSUPPORTED; |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 1115 | goto out; |
| 1116 | } |
| 1117 | ret = tcg2_hash_pe_image((void *)(uintptr_t)data_to_hash, |
| 1118 | data_to_hash_len, &digest_list); |
| 1119 | } else { |
| 1120 | ret = tcg2_create_digest((u8 *)(uintptr_t)data_to_hash, |
| 1121 | data_to_hash_len, &digest_list); |
| 1122 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1123 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1124 | if (ret != EFI_SUCCESS) |
| 1125 | goto out; |
| 1126 | |
Masahisa Kojima | 70be5a6 | 2021-05-26 12:09:58 +0900 | [diff] [blame] | 1127 | pcr_index = efi_tcg_event->header.pcr_index; |
| 1128 | event_type = efi_tcg_event->header.event_type; |
| 1129 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1130 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 1131 | if (ret != EFI_SUCCESS) |
| 1132 | goto out; |
| 1133 | |
| 1134 | if (flags & EFI_TCG2_EXTEND_ONLY) { |
| 1135 | if (event_log.truncated) |
| 1136 | ret = EFI_VOLUME_FULL; |
| 1137 | goto out; |
| 1138 | } |
| 1139 | |
| 1140 | /* |
| 1141 | * The efi_tcg_event size includes the size component and the |
| 1142 | * headersize |
| 1143 | */ |
| 1144 | event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) - |
| 1145 | efi_tcg_event->header.header_size; |
| 1146 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 1147 | event_size, efi_tcg_event->event); |
| 1148 | out: |
| 1149 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1153 | * efi_tcg2_submit_command() - Send command to the TPM |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1154 | * |
| 1155 | * @this: TCG2 protocol instance |
| 1156 | * @input_param_block_size: size of the TPM input parameter block |
| 1157 | * @input_param_block: pointer to the TPM input parameter block |
| 1158 | * @output_param_block_size: size of the TPM output parameter block |
| 1159 | * @output_param_block: pointer to the TPM output parameter block |
| 1160 | * |
| 1161 | * Return: status code |
| 1162 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1163 | static efi_status_t EFIAPI |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 1164 | efi_tcg2_submit_command(struct efi_tcg2_protocol *this, |
| 1165 | u32 input_param_block_size, |
| 1166 | u8 *input_param_block, |
| 1167 | u32 output_param_block_size, |
| 1168 | u8 *output_param_block) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1169 | { |
Masahisa Kojima | 06ef6b6 | 2021-11-04 22:59:16 +0900 | [diff] [blame] | 1170 | struct udevice *dev; |
| 1171 | efi_status_t ret; |
| 1172 | u32 rc; |
| 1173 | size_t resp_buf_size = output_param_block_size; |
| 1174 | |
| 1175 | EFI_ENTRY("%p, %u, %p, %u, %p", this, input_param_block_size, |
| 1176 | input_param_block, output_param_block_size, output_param_block); |
| 1177 | |
| 1178 | if (!this || !input_param_block || !input_param_block_size) { |
| 1179 | ret = EFI_INVALID_PARAMETER; |
| 1180 | goto out; |
| 1181 | } |
| 1182 | |
| 1183 | ret = platform_get_tpm2_device(&dev); |
| 1184 | if (ret != EFI_SUCCESS) |
| 1185 | goto out; |
| 1186 | |
| 1187 | rc = tpm2_submit_command(dev, input_param_block, |
| 1188 | output_param_block, &resp_buf_size); |
| 1189 | if (rc) { |
| 1190 | ret = (rc == -ENOSPC) ? EFI_OUT_OF_RESOURCES : EFI_DEVICE_ERROR; |
| 1191 | |
| 1192 | goto out; |
| 1193 | } |
| 1194 | |
| 1195 | out: |
| 1196 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1200 | * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1201 | * |
| 1202 | * @this: TCG2 protocol instance |
| 1203 | * @active_pcr_banks: pointer for receiving the bitmap of currently |
| 1204 | * active PCR banks |
| 1205 | * |
| 1206 | * Return: status code |
| 1207 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1208 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1209 | efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this, |
| 1210 | u32 *active_pcr_banks) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1211 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1212 | efi_status_t ret; |
| 1213 | |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 1214 | if (!this || !active_pcr_banks) { |
| 1215 | ret = EFI_INVALID_PARAMETER; |
| 1216 | goto out; |
| 1217 | } |
| 1218 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1219 | EFI_ENTRY("%p, %p", this, active_pcr_banks); |
| 1220 | ret = __get_active_pcr_banks(active_pcr_banks); |
| 1221 | |
Masahisa Kojima | 7c5fccd | 2021-09-03 10:55:50 +0900 | [diff] [blame] | 1222 | out: |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1223 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1227 | * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1228 | * |
| 1229 | * @this: TCG2 protocol instance |
| 1230 | * @active_pcr_banks: bitmap of the requested active PCR banks |
| 1231 | * |
| 1232 | * Return: status code |
| 1233 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1234 | static efi_status_t EFIAPI |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 1235 | efi_tcg2_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this, |
| 1236 | u32 __maybe_unused active_pcr_banks) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1237 | { |
| 1238 | return EFI_UNSUPPORTED; |
| 1239 | } |
| 1240 | |
| 1241 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1242 | * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous |
| 1243 | * set_active_pcr_banks() |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1244 | * |
| 1245 | * @this: TCG2 protocol instance |
| 1246 | * @operation_present: non-zero value to indicate a |
| 1247 | * set_active_pcr_banks operation was |
| 1248 | * invoked during last boot |
| 1249 | * @response: result value could be returned |
| 1250 | * |
| 1251 | * Return: status code |
| 1252 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1253 | static efi_status_t EFIAPI |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 1254 | efi_tcg2_get_result_of_set_active_pcr_banks(__maybe_unused struct efi_tcg2_protocol *this, |
| 1255 | u32 __maybe_unused *operation_present, |
| 1256 | u32 __maybe_unused *response) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1257 | { |
| 1258 | return EFI_UNSUPPORTED; |
| 1259 | } |
| 1260 | |
| 1261 | static const struct efi_tcg2_protocol efi_tcg2_protocol = { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1262 | .get_capability = efi_tcg2_get_capability, |
| 1263 | .get_eventlog = efi_tcg2_get_eventlog, |
| 1264 | .hash_log_extend_event = efi_tcg2_hash_log_extend_event, |
| 1265 | .submit_command = efi_tcg2_submit_command, |
| 1266 | .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks, |
| 1267 | .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks, |
| 1268 | .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] | 1269 | }; |
| 1270 | |
| 1271 | /** |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 1272 | * parse_event_log_header() - Parse and verify the event log header fields |
| 1273 | * |
| 1274 | * @buffer: Pointer to the start of the eventlog |
| 1275 | * @size: Size of the eventlog |
| 1276 | * @pos: Return offset of the next event in buffer right |
| 1277 | * after the event header i.e specID |
| 1278 | * |
| 1279 | * Return: status code |
| 1280 | */ |
| 1281 | static efi_status_t parse_event_log_header(void *buffer, u32 size, u32 *pos) |
| 1282 | { |
| 1283 | struct tcg_pcr_event *event_header = (struct tcg_pcr_event *)buffer; |
| 1284 | int i = 0; |
| 1285 | |
| 1286 | if (size < sizeof(*event_header)) |
| 1287 | return EFI_COMPROMISED_DATA; |
| 1288 | |
| 1289 | if (get_unaligned_le32(&event_header->pcr_index) != 0 || |
| 1290 | get_unaligned_le32(&event_header->event_type) != EV_NO_ACTION) |
| 1291 | return EFI_COMPROMISED_DATA; |
| 1292 | |
| 1293 | for (i = 0; i < sizeof(event_header->digest); i++) { |
| 1294 | if (event_header->digest[i]) |
| 1295 | return EFI_COMPROMISED_DATA; |
| 1296 | } |
| 1297 | |
| 1298 | *pos += sizeof(*event_header); |
| 1299 | |
| 1300 | return EFI_SUCCESS; |
| 1301 | } |
| 1302 | |
| 1303 | /** |
| 1304 | * parse_specid_event() - Parse and verify the specID Event in the eventlog |
| 1305 | * |
| 1306 | * @dev: udevice |
| 1307 | * @buffer: Pointer to the start of the eventlog |
| 1308 | * @log_size: Size of the eventlog |
| 1309 | * @pos: [in] Offset of specID event in the eventlog buffer |
| 1310 | * [out] Return offset of the next event in the buffer |
| 1311 | * after the specID |
| 1312 | * @digest_list: list of digests in the event |
| 1313 | * |
| 1314 | * Return: status code |
| 1315 | * @pos Offset in the eventlog where the specID event ends |
| 1316 | * @digest_list: list of digests in the event |
| 1317 | */ |
| 1318 | static efi_status_t parse_specid_event(struct udevice *dev, void *buffer, |
| 1319 | u32 log_size, u32 *pos, |
| 1320 | struct tpml_digest_values *digest_list) |
| 1321 | { |
| 1322 | struct tcg_efi_spec_id_event *spec_event; |
| 1323 | struct tcg_pcr_event *event_header = (struct tcg_pcr_event *)buffer; |
| 1324 | size_t spec_event_size; |
| 1325 | u32 active = 0, supported = 0, pcr_count = 0, alg_count = 0; |
| 1326 | u32 spec_active = 0; |
| 1327 | u16 hash_alg; |
| 1328 | u8 vendor_sz; |
| 1329 | int err, i; |
| 1330 | |
| 1331 | if (*pos >= log_size || (*pos + sizeof(*spec_event)) > log_size) |
| 1332 | return EFI_COMPROMISED_DATA; |
| 1333 | |
| 1334 | /* Check specID event data */ |
| 1335 | spec_event = (struct tcg_efi_spec_id_event *)((uintptr_t)buffer + *pos); |
| 1336 | /* Check for signature */ |
| 1337 | if (memcmp(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03, |
| 1338 | sizeof(TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03))) { |
| 1339 | log_err("specID Event: Signature mismatch\n"); |
| 1340 | return EFI_COMPROMISED_DATA; |
| 1341 | } |
| 1342 | |
| 1343 | if (spec_event->spec_version_minor != |
| 1344 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2 || |
| 1345 | spec_event->spec_version_major != |
| 1346 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2) |
| 1347 | return EFI_COMPROMISED_DATA; |
| 1348 | |
| 1349 | if (spec_event->number_of_algorithms > MAX_HASH_COUNT || |
| 1350 | spec_event->number_of_algorithms < 1) { |
| 1351 | log_err("specID Event: Number of algorithms incorrect\n"); |
| 1352 | return EFI_COMPROMISED_DATA; |
| 1353 | } |
| 1354 | |
| 1355 | alg_count = spec_event->number_of_algorithms; |
| 1356 | |
| 1357 | err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_count); |
| 1358 | if (err) |
| 1359 | return EFI_DEVICE_ERROR; |
| 1360 | |
| 1361 | digest_list->count = 0; |
| 1362 | /* |
| 1363 | * We have to take care that the sequence of algorithms that we record |
| 1364 | * in digest_list matches the sequence in eventlog. |
| 1365 | */ |
| 1366 | for (i = 0; i < alg_count; i++) { |
| 1367 | hash_alg = |
| 1368 | get_unaligned_le16(&spec_event->digest_sizes[i].algorithm_id); |
| 1369 | |
| 1370 | if (!(supported & alg_to_mask(hash_alg))) { |
| 1371 | log_err("specID Event: Unsupported algorithm\n"); |
| 1372 | return EFI_COMPROMISED_DATA; |
| 1373 | } |
| 1374 | digest_list->digests[digest_list->count++].hash_alg = hash_alg; |
| 1375 | |
| 1376 | spec_active |= alg_to_mask(hash_alg); |
| 1377 | } |
| 1378 | |
| 1379 | /* |
| 1380 | * TCG specification expects the event log to have hashes for all |
| 1381 | * active PCR's |
| 1382 | */ |
| 1383 | if (spec_active != active) { |
| 1384 | /* |
| 1385 | * Previous stage bootloader should know all the active PCR's |
| 1386 | * and use them in the Eventlog. |
| 1387 | */ |
| 1388 | log_err("specID Event: All active hash alg not present\n"); |
| 1389 | return EFI_COMPROMISED_DATA; |
| 1390 | } |
| 1391 | |
| 1392 | /* |
| 1393 | * the size of the spec event and placement of vendor_info_size |
| 1394 | * depends on supported algoriths |
| 1395 | */ |
| 1396 | spec_event_size = |
| 1397 | offsetof(struct tcg_efi_spec_id_event, digest_sizes) + |
| 1398 | alg_count * sizeof(spec_event->digest_sizes[0]); |
| 1399 | |
| 1400 | if (*pos + spec_event_size >= log_size) |
| 1401 | return EFI_COMPROMISED_DATA; |
| 1402 | |
| 1403 | vendor_sz = *(uint8_t *)((uintptr_t)buffer + *pos + spec_event_size); |
| 1404 | |
| 1405 | spec_event_size += sizeof(vendor_sz) + vendor_sz; |
| 1406 | *pos += spec_event_size; |
| 1407 | |
| 1408 | if (get_unaligned_le32(&event_header->event_size) != spec_event_size) { |
| 1409 | log_err("specID event: header event size mismatch\n"); |
| 1410 | /* Right way to handle this can be to call SetActive PCR's */ |
| 1411 | return EFI_COMPROMISED_DATA; |
| 1412 | } |
| 1413 | |
| 1414 | return EFI_SUCCESS; |
| 1415 | } |
| 1416 | |
| 1417 | /** |
| 1418 | * tcg2_parse_event() - Parse the event in the eventlog |
| 1419 | * |
| 1420 | * @dev: udevice |
| 1421 | * @buffer: Pointer to the start of the eventlog |
| 1422 | * @log_size: Size of the eventlog |
| 1423 | * @offset: [in] Offset of the event in the eventlog buffer |
| 1424 | * [out] Return offset of the next event in the buffer |
| 1425 | * @digest_list: list of digests in the event |
| 1426 | * @pcr Index of the PCR in the event |
| 1427 | * |
| 1428 | * Return: status code |
| 1429 | */ |
| 1430 | static efi_status_t tcg2_parse_event(struct udevice *dev, void *buffer, |
| 1431 | u32 log_size, u32 *offset, |
| 1432 | struct tpml_digest_values *digest_list, |
| 1433 | u32 *pcr) |
| 1434 | { |
| 1435 | struct tcg_pcr_event2 *event = NULL; |
| 1436 | u32 count, size, event_size; |
| 1437 | size_t pos; |
| 1438 | |
| 1439 | event_size = tcg_event_final_size(digest_list); |
| 1440 | if (*offset >= log_size || *offset + event_size > log_size) { |
| 1441 | log_err("Event exceeds log size\n"); |
| 1442 | return EFI_COMPROMISED_DATA; |
| 1443 | } |
| 1444 | |
| 1445 | event = (struct tcg_pcr_event2 *)((uintptr_t)buffer + *offset); |
| 1446 | *pcr = get_unaligned_le32(&event->pcr_index); |
| 1447 | |
| 1448 | /* get the count */ |
| 1449 | count = get_unaligned_le32(&event->digests.count); |
| 1450 | if (count != digest_list->count) |
| 1451 | return EFI_COMPROMISED_DATA; |
| 1452 | |
| 1453 | pos = offsetof(struct tcg_pcr_event2, digests); |
| 1454 | pos += offsetof(struct tpml_digest_values, digests); |
| 1455 | |
| 1456 | for (int i = 0; i < digest_list->count; i++) { |
| 1457 | u16 alg; |
| 1458 | u16 hash_alg = digest_list->digests[i].hash_alg; |
| 1459 | u8 *digest = (u8 *)&digest_list->digests[i].digest; |
| 1460 | |
| 1461 | alg = get_unaligned_le16((void *)((uintptr_t)event + pos)); |
| 1462 | |
| 1463 | if (alg != hash_alg) |
| 1464 | return EFI_COMPROMISED_DATA; |
| 1465 | |
| 1466 | pos += offsetof(struct tpmt_ha, digest); |
| 1467 | memcpy(digest, (void *)((uintptr_t)event + pos), alg_to_len(hash_alg)); |
| 1468 | pos += alg_to_len(hash_alg); |
| 1469 | } |
| 1470 | |
| 1471 | size = get_unaligned_le32((void *)((uintptr_t)event + pos)); |
| 1472 | event_size += size; |
| 1473 | pos += sizeof(u32); /* tcg_pcr_event2 event_size*/ |
| 1474 | pos += size; |
| 1475 | |
| 1476 | /* make sure the calculated buffer is what we checked against */ |
| 1477 | if (pos != event_size) |
| 1478 | return EFI_COMPROMISED_DATA; |
| 1479 | |
| 1480 | if (pos > log_size) |
| 1481 | return EFI_COMPROMISED_DATA; |
| 1482 | |
| 1483 | *offset += pos; |
| 1484 | |
| 1485 | return EFI_SUCCESS; |
| 1486 | } |
| 1487 | |
| 1488 | /** |
| 1489 | * tcg2_get_fw_eventlog() - Get the eventlog address and size |
| 1490 | * |
| 1491 | * If the previous firmware has passed some eventlog, this function get it's |
| 1492 | * location and check for it's validity. |
| 1493 | * |
| 1494 | * @dev: udevice |
| 1495 | * @log_buffer: eventlog address |
| 1496 | * @log_sz: eventlog size |
| 1497 | * |
| 1498 | * Return: status code |
| 1499 | */ |
| 1500 | static efi_status_t tcg2_get_fw_eventlog(struct udevice *dev, void *log_buffer, |
| 1501 | size_t *log_sz) |
| 1502 | { |
| 1503 | struct tpml_digest_values digest_list; |
| 1504 | void *buffer; |
| 1505 | efi_status_t ret; |
| 1506 | u32 pcr, pos; |
| 1507 | u64 base; |
| 1508 | u32 sz; |
Ruchika Gupta | 9d0b5d0 | 2021-11-29 13:09:46 +0530 | [diff] [blame] | 1509 | bool extend_pcr = false; |
| 1510 | int i; |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 1511 | |
| 1512 | ret = platform_get_eventlog(dev, &base, &sz); |
| 1513 | if (ret != EFI_SUCCESS) |
| 1514 | return ret; |
| 1515 | |
| 1516 | if (sz > TPM2_EVENT_LOG_SIZE) |
| 1517 | return EFI_VOLUME_FULL; |
| 1518 | |
| 1519 | buffer = (void *)(uintptr_t)base; |
| 1520 | pos = 0; |
| 1521 | /* Parse the eventlog to check for its validity */ |
| 1522 | ret = parse_event_log_header(buffer, sz, &pos); |
| 1523 | if (ret) |
| 1524 | return ret; |
| 1525 | |
| 1526 | ret = parse_specid_event(dev, buffer, sz, &pos, &digest_list); |
| 1527 | if (ret) { |
| 1528 | log_err("Error parsing SPEC ID Event\n"); |
| 1529 | return ret; |
| 1530 | } |
| 1531 | |
Ruchika Gupta | 9d0b5d0 | 2021-11-29 13:09:46 +0530 | [diff] [blame] | 1532 | ret = tcg2_pcr_read(dev, 0, &digest_list); |
| 1533 | if (ret) { |
| 1534 | log_err("Error reading PCR 0\n"); |
| 1535 | return ret; |
| 1536 | } |
| 1537 | |
| 1538 | /* |
| 1539 | * If PCR0 is 0, previous firmware didn't have the capability |
| 1540 | * to extend the PCR. In this scenario, extend the PCR as |
| 1541 | * the eventlog is parsed. |
| 1542 | */ |
| 1543 | for (i = 0; i < digest_list.count; i++) { |
| 1544 | u8 hash_buf[TPM2_SHA512_DIGEST_SIZE] = { 0 }; |
| 1545 | u16 hash_alg = digest_list.digests[i].hash_alg; |
| 1546 | |
| 1547 | if (!memcmp((u8 *)&digest_list.digests[i].digest, hash_buf, |
| 1548 | alg_to_len(hash_alg))) |
| 1549 | extend_pcr = true; |
| 1550 | } |
| 1551 | |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 1552 | while (pos < sz) { |
| 1553 | ret = tcg2_parse_event(dev, buffer, sz, &pos, &digest_list, |
| 1554 | &pcr); |
| 1555 | if (ret) { |
| 1556 | log_err("Error parsing event\n"); |
| 1557 | return ret; |
| 1558 | } |
Ruchika Gupta | 9d0b5d0 | 2021-11-29 13:09:46 +0530 | [diff] [blame] | 1559 | if (extend_pcr) { |
| 1560 | ret = tcg2_pcr_extend(dev, pcr, &digest_list); |
| 1561 | if (ret != EFI_SUCCESS) { |
| 1562 | log_err("Error in extending PCR\n"); |
| 1563 | return ret; |
| 1564 | } |
| 1565 | |
| 1566 | /* Clear the digest for next event */ |
| 1567 | for (i = 0; i < digest_list.count; i++) { |
| 1568 | u16 hash_alg = digest_list.digests[i].hash_alg; |
| 1569 | u8 *digest = |
| 1570 | (u8 *)&digest_list.digests[i].digest; |
| 1571 | |
| 1572 | memset(digest, 0, alg_to_len(hash_alg)); |
| 1573 | } |
| 1574 | } |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | memcpy(log_buffer, buffer, sz); |
| 1578 | *log_sz = sz; |
| 1579 | |
| 1580 | return ret; |
| 1581 | } |
| 1582 | |
| 1583 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1584 | * create_specid_event() - Create the first event in the eventlog |
| 1585 | * |
| 1586 | * @dev: tpm device |
| 1587 | * @event_header: Pointer to the final event header |
| 1588 | * @event_size: final spec event size |
| 1589 | * |
| 1590 | * Return: status code |
| 1591 | */ |
| 1592 | static efi_status_t create_specid_event(struct udevice *dev, void *buffer, |
| 1593 | size_t *event_size) |
| 1594 | { |
| 1595 | struct tcg_efi_spec_id_event *spec_event; |
| 1596 | size_t spec_event_size; |
| 1597 | efi_status_t ret = EFI_DEVICE_ERROR; |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 1598 | u32 active = 0, supported = 0, pcr_count = 0, alg_count = 0; |
Ilias Apalodimas | 190b0a2 | 2021-05-25 14:35:31 +0300 | [diff] [blame] | 1599 | int err; |
| 1600 | size_t i; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1601 | |
| 1602 | /* |
| 1603 | * Create Spec event. This needs to be the first event in the log |
| 1604 | * according to the TCG EFI protocol spec |
| 1605 | */ |
| 1606 | |
| 1607 | /* Setup specID event data */ |
| 1608 | spec_event = (struct tcg_efi_spec_id_event *)buffer; |
| 1609 | memcpy(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03, |
| 1610 | sizeof(spec_event->signature)); |
| 1611 | put_unaligned_le32(0, &spec_event->platform_class); /* type client */ |
| 1612 | spec_event->spec_version_minor = |
| 1613 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2; |
| 1614 | spec_event->spec_version_major = |
| 1615 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2; |
| 1616 | spec_event->spec_errata = |
| 1617 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2; |
| 1618 | spec_event->uintn_size = sizeof(efi_uintn_t) / sizeof(u32); |
| 1619 | |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 1620 | err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_count); |
| 1621 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1622 | if (err) |
| 1623 | goto out; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1624 | |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 1625 | for (i = 0; i < pcr_count; i++) { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1626 | u16 hash_alg = hash_algo_list[i].hash_alg; |
| 1627 | u16 hash_len = hash_algo_list[i].hash_len; |
| 1628 | |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 1629 | if (active & alg_to_mask(hash_alg)) { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1630 | put_unaligned_le16(hash_alg, |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 1631 | &spec_event->digest_sizes[alg_count].algorithm_id); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1632 | put_unaligned_le16(hash_len, |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 1633 | &spec_event->digest_sizes[alg_count].digest_size); |
| 1634 | alg_count++; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1635 | } |
| 1636 | } |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 1637 | |
| 1638 | spec_event->number_of_algorithms = alg_count; |
| 1639 | if (spec_event->number_of_algorithms > MAX_HASH_COUNT || |
| 1640 | spec_event->number_of_algorithms < 1) |
| 1641 | goto out; |
| 1642 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1643 | /* |
| 1644 | * the size of the spec event and placement of vendor_info_size |
| 1645 | * depends on supported algoriths |
| 1646 | */ |
| 1647 | spec_event_size = |
| 1648 | offsetof(struct tcg_efi_spec_id_event, digest_sizes) + |
| 1649 | spec_event->number_of_algorithms * sizeof(spec_event->digest_sizes[0]); |
| 1650 | /* no vendor info for us */ |
Ruchika Gupta | e53007b | 2021-09-14 12:14:31 +0530 | [diff] [blame] | 1651 | memset(buffer + spec_event_size, 0, 1); |
| 1652 | /* add a byte for vendor_info_size in the spec event */ |
| 1653 | spec_event_size += 1; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1654 | *event_size = spec_event_size; |
| 1655 | |
| 1656 | return EFI_SUCCESS; |
| 1657 | |
| 1658 | out: |
| 1659 | return ret; |
| 1660 | } |
| 1661 | |
| 1662 | /** |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1663 | * tcg2_uninit - remove the final event table and free efi memory on failures |
| 1664 | */ |
| 1665 | void tcg2_uninit(void) |
| 1666 | { |
| 1667 | efi_status_t ret; |
| 1668 | |
| 1669 | ret = efi_install_configuration_table(&efi_guid_final_events, NULL); |
| 1670 | if (ret != EFI_SUCCESS) |
| 1671 | log_err("Failed to delete final events config table\n"); |
| 1672 | |
| 1673 | efi_free_pool(event_log.buffer); |
| 1674 | event_log.buffer = NULL; |
| 1675 | efi_free_pool(event_log.final_buffer); |
| 1676 | event_log.final_buffer = NULL; |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1677 | |
| 1678 | if (!is_tcg2_protocol_installed()) |
| 1679 | return; |
| 1680 | |
Ilias Apalodimas | 4953c99 | 2023-06-19 14:14:02 +0300 | [diff] [blame] | 1681 | ret = efi_uninstall_multiple_protocol_interfaces(efi_root, &efi_guid_tcg2_protocol, |
| 1682 | &efi_tcg2_protocol, NULL); |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 1683 | if (ret != EFI_SUCCESS) |
| 1684 | log_err("Failed to remove EFI TCG2 protocol\n"); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1688 | * create_final_event() - Create the final event and install the config |
| 1689 | * defined by the TCG EFI spec |
| 1690 | */ |
| 1691 | static efi_status_t create_final_event(void) |
| 1692 | { |
| 1693 | struct efi_tcg2_final_events_table *final_event; |
| 1694 | efi_status_t ret; |
| 1695 | |
| 1696 | /* |
| 1697 | * All events generated after the invocation of |
| 1698 | * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an |
| 1699 | * EFI_CONFIGURATION_TABLE |
| 1700 | */ |
| 1701 | ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE, |
| 1702 | &event_log.final_buffer); |
| 1703 | if (ret != EFI_SUCCESS) |
| 1704 | goto out; |
| 1705 | |
| 1706 | memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE); |
| 1707 | final_event = event_log.final_buffer; |
| 1708 | final_event->number_of_events = 0; |
| 1709 | final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION; |
| 1710 | event_log.final_pos = sizeof(*final_event); |
| 1711 | ret = efi_install_configuration_table(&efi_guid_final_events, |
| 1712 | final_event); |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 1713 | if (ret != EFI_SUCCESS) { |
| 1714 | efi_free_pool(event_log.final_buffer); |
| 1715 | event_log.final_buffer = NULL; |
| 1716 | } |
| 1717 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1718 | out: |
| 1719 | return ret; |
| 1720 | } |
| 1721 | |
| 1722 | /** |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1723 | * tcg2_measure_event() - common function to add event log and extend PCR |
| 1724 | * |
| 1725 | * @dev: TPM device |
| 1726 | * @pcr_index: PCR index |
| 1727 | * @event_type: type of event added |
| 1728 | * @size: event size |
| 1729 | * @event: event data |
| 1730 | * |
| 1731 | * Return: status code |
| 1732 | */ |
| 1733 | static efi_status_t |
| 1734 | tcg2_measure_event(struct udevice *dev, u32 pcr_index, u32 event_type, |
| 1735 | u32 size, u8 event[]) |
| 1736 | { |
| 1737 | struct tpml_digest_values digest_list; |
| 1738 | efi_status_t ret; |
| 1739 | |
| 1740 | ret = tcg2_create_digest(event, size, &digest_list); |
| 1741 | if (ret != EFI_SUCCESS) |
| 1742 | goto out; |
| 1743 | |
| 1744 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 1745 | if (ret != EFI_SUCCESS) |
| 1746 | goto out; |
| 1747 | |
| 1748 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 1749 | size, event); |
| 1750 | |
| 1751 | out: |
| 1752 | return ret; |
| 1753 | } |
| 1754 | |
| 1755 | /** |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1756 | * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the |
| 1757 | * eventlog and extend the PCRs |
| 1758 | * |
| 1759 | * @dev: TPM device |
| 1760 | * |
| 1761 | * @Return: status code |
| 1762 | */ |
| 1763 | static efi_status_t efi_append_scrtm_version(struct udevice *dev) |
| 1764 | { |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1765 | efi_status_t ret; |
| 1766 | |
Pali Rohár | 144d642 | 2021-08-02 15:18:30 +0200 | [diff] [blame] | 1767 | ret = tcg2_measure_event(dev, 0, EV_S_CRTM_VERSION, |
| 1768 | strlen(version_string) + 1, |
| 1769 | (u8 *)version_string); |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1770 | |
Ruchika Gupta | bc9495c | 2021-11-29 13:09:44 +0530 | [diff] [blame] | 1771 | return ret; |
| 1772 | } |
| 1773 | |
| 1774 | /** |
| 1775 | * efi_init_event_log() - initialize an eventlog |
| 1776 | * |
| 1777 | * Return: status code |
| 1778 | */ |
| 1779 | static efi_status_t efi_init_event_log(void) |
| 1780 | { |
| 1781 | /* |
| 1782 | * vendor_info_size is currently set to 0, we need to change the length |
| 1783 | * and allocate the flexible array member if this changes |
| 1784 | */ |
| 1785 | struct tcg_pcr_event *event_header = NULL; |
| 1786 | struct udevice *dev; |
| 1787 | size_t spec_event_size; |
| 1788 | efi_status_t ret; |
| 1789 | |
| 1790 | ret = platform_get_tpm2_device(&dev); |
| 1791 | if (ret != EFI_SUCCESS) |
| 1792 | return ret; |
| 1793 | |
| 1794 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE, |
| 1795 | (void **)&event_log.buffer); |
| 1796 | if (ret != EFI_SUCCESS) |
| 1797 | return ret; |
| 1798 | |
| 1799 | /* |
| 1800 | * initialize log area as 0xff so the OS can easily figure out the |
| 1801 | * last log entry |
| 1802 | */ |
| 1803 | memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE); |
| 1804 | |
| 1805 | /* |
| 1806 | * The log header is defined to be in SHA1 event log entry format. |
| 1807 | * Setup event header |
| 1808 | */ |
| 1809 | event_header = (struct tcg_pcr_event *)event_log.buffer; |
| 1810 | event_log.pos = 0; |
| 1811 | event_log.last_event_size = 0; |
| 1812 | event_log.get_event_called = false; |
| 1813 | event_log.ebs_called = false; |
| 1814 | event_log.truncated = false; |
| 1815 | |
| 1816 | /* |
| 1817 | * Check if earlier firmware have passed any eventlog. Different |
| 1818 | * platforms can use different ways to do so. |
| 1819 | */ |
| 1820 | ret = tcg2_get_fw_eventlog(dev, event_log.buffer, &event_log.pos); |
| 1821 | /* |
| 1822 | * If earlier firmware hasn't passed any eventlog, go ahead and |
| 1823 | * create the eventlog header. |
| 1824 | */ |
| 1825 | if (ret == EFI_NOT_FOUND) { |
| 1826 | put_unaligned_le32(0, &event_header->pcr_index); |
| 1827 | put_unaligned_le32(EV_NO_ACTION, &event_header->event_type); |
| 1828 | memset(&event_header->digest, 0, sizeof(event_header->digest)); |
| 1829 | ret = create_specid_event(dev, |
| 1830 | (void *)((uintptr_t)event_log.buffer + |
| 1831 | sizeof(*event_header)), |
| 1832 | &spec_event_size); |
| 1833 | if (ret != EFI_SUCCESS) |
| 1834 | goto free_pool; |
| 1835 | put_unaligned_le32(spec_event_size, &event_header->event_size); |
| 1836 | event_log.pos = spec_event_size + sizeof(*event_header); |
| 1837 | event_log.last_event_size = event_log.pos; |
| 1838 | |
| 1839 | /* |
| 1840 | * Add SCRTM version to the log if previous firmmware |
| 1841 | * doesn't pass an eventlog. |
| 1842 | */ |
| 1843 | ret = efi_append_scrtm_version(dev); |
| 1844 | } |
| 1845 | |
| 1846 | if (ret != EFI_SUCCESS) |
| 1847 | goto free_pool; |
| 1848 | |
| 1849 | ret = create_final_event(); |
| 1850 | if (ret != EFI_SUCCESS) |
| 1851 | goto free_pool; |
| 1852 | |
| 1853 | return ret; |
| 1854 | |
| 1855 | free_pool: |
| 1856 | efi_free_pool(event_log.buffer); |
| 1857 | event_log.buffer = NULL; |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1858 | return ret; |
| 1859 | } |
| 1860 | |
| 1861 | /** |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1862 | * tcg2_measure_variable() - add variable event log and extend PCR |
| 1863 | * |
| 1864 | * @dev: TPM device |
| 1865 | * @pcr_index: PCR index |
| 1866 | * @event_type: type of event added |
| 1867 | * @var_name: variable name |
| 1868 | * @guid: guid |
| 1869 | * @data_size: variable data size |
| 1870 | * @data: variable data |
| 1871 | * |
| 1872 | * Return: status code |
| 1873 | */ |
| 1874 | 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] | 1875 | u32 event_type, const u16 *var_name, |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 1876 | const efi_guid_t *guid, |
| 1877 | efi_uintn_t data_size, u8 *data) |
| 1878 | { |
| 1879 | u32 event_size; |
| 1880 | efi_status_t ret; |
| 1881 | struct efi_tcg2_uefi_variable_data *event; |
| 1882 | |
| 1883 | event_size = sizeof(event->variable_name) + |
| 1884 | sizeof(event->unicode_name_length) + |
| 1885 | sizeof(event->variable_data_length) + |
| 1886 | (u16_strlen(var_name) * sizeof(u16)) + data_size; |
| 1887 | event = malloc(event_size); |
| 1888 | if (!event) |
| 1889 | return EFI_OUT_OF_RESOURCES; |
| 1890 | |
| 1891 | guidcpy(&event->variable_name, guid); |
| 1892 | event->unicode_name_length = u16_strlen(var_name); |
| 1893 | event->variable_data_length = data_size; |
| 1894 | memcpy(event->unicode_name, var_name, |
| 1895 | (event->unicode_name_length * sizeof(u16))); |
| 1896 | if (data) { |
| 1897 | memcpy((u16 *)event->unicode_name + event->unicode_name_length, |
| 1898 | data, data_size); |
| 1899 | } |
| 1900 | ret = tcg2_measure_event(dev, pcr_index, event_type, event_size, |
| 1901 | (u8 *)event); |
| 1902 | free(event); |
| 1903 | return ret; |
| 1904 | } |
| 1905 | |
| 1906 | /** |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1907 | * tcg2_measure_boot_variable() - measure boot variables |
| 1908 | * |
| 1909 | * @dev: TPM device |
| 1910 | * |
| 1911 | * Return: status code |
| 1912 | */ |
| 1913 | static efi_status_t tcg2_measure_boot_variable(struct udevice *dev) |
| 1914 | { |
| 1915 | u16 *boot_order; |
| 1916 | u16 *boot_index; |
Simon Glass | 9097537 | 2022-01-23 12:55:12 -0700 | [diff] [blame] | 1917 | u16 var_name[] = u"BootOrder"; |
| 1918 | u16 boot_name[] = u"Boot####"; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1919 | u8 *bootvar; |
| 1920 | efi_uintn_t var_data_size; |
| 1921 | u32 count, i; |
| 1922 | efi_status_t ret; |
| 1923 | |
| 1924 | boot_order = efi_get_var(var_name, &efi_global_variable_guid, |
| 1925 | &var_data_size); |
| 1926 | if (!boot_order) { |
Masahisa Kojima | d132593 | 2021-11-09 18:44:54 +0900 | [diff] [blame] | 1927 | /* If "BootOrder" is not defined, skip the boot variable measurement */ |
| 1928 | return EFI_SUCCESS; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1929 | } |
| 1930 | |
| 1931 | ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, var_name, |
| 1932 | &efi_global_variable_guid, var_data_size, |
| 1933 | (u8 *)boot_order); |
| 1934 | if (ret != EFI_SUCCESS) |
| 1935 | goto error; |
| 1936 | |
| 1937 | count = var_data_size / sizeof(*boot_order); |
| 1938 | boot_index = boot_order; |
| 1939 | for (i = 0; i < count; i++) { |
| 1940 | efi_create_indexed_name(boot_name, sizeof(boot_name), |
| 1941 | "Boot", *boot_index++); |
| 1942 | |
| 1943 | bootvar = efi_get_var(boot_name, &efi_global_variable_guid, |
| 1944 | &var_data_size); |
| 1945 | |
| 1946 | if (!bootvar) { |
Masahisa Kojima | aca20c8 | 2021-11-09 20:35:53 +0900 | [diff] [blame] | 1947 | log_debug("%ls not found\n", boot_name); |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 1948 | continue; |
| 1949 | } |
| 1950 | |
| 1951 | ret = tcg2_measure_variable(dev, 1, EV_EFI_VARIABLE_BOOT2, |
| 1952 | boot_name, |
| 1953 | &efi_global_variable_guid, |
| 1954 | var_data_size, bootvar); |
| 1955 | free(bootvar); |
| 1956 | if (ret != EFI_SUCCESS) |
| 1957 | goto error; |
| 1958 | } |
| 1959 | |
| 1960 | error: |
| 1961 | free(boot_order); |
| 1962 | return ret; |
| 1963 | } |
| 1964 | |
| 1965 | /** |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 1966 | * tcg2_measure_smbios() - measure smbios table |
| 1967 | * |
| 1968 | * @dev: TPM device |
| 1969 | * @entry: pointer to the smbios_entry structure |
| 1970 | * |
| 1971 | * Return: status code |
| 1972 | */ |
| 1973 | static efi_status_t |
| 1974 | tcg2_measure_smbios(struct udevice *dev, |
| 1975 | const struct smbios_entry *entry) |
| 1976 | { |
| 1977 | efi_status_t ret; |
| 1978 | struct smbios_header *smbios_copy; |
| 1979 | struct smbios_handoff_table_pointers2 *event = NULL; |
| 1980 | u32 event_size; |
| 1981 | |
| 1982 | /* |
| 1983 | * TCG PC Client PFP Spec says |
| 1984 | * "SMBIOS structures that contain static configuration information |
| 1985 | * (e.g. Platform Manufacturer Enterprise Number assigned by IANA, |
| 1986 | * platform model number, Vendor and Device IDs for each SMBIOS table) |
| 1987 | * that is relevant to the security of the platform MUST be measured". |
| 1988 | * Device dependent parameters such as serial number are cleared to |
| 1989 | * zero or spaces for the measurement. |
| 1990 | */ |
| 1991 | event_size = sizeof(struct smbios_handoff_table_pointers2) + |
| 1992 | FIELD_SIZEOF(struct efi_configuration_table, guid) + |
| 1993 | entry->struct_table_length; |
| 1994 | event = calloc(1, event_size); |
| 1995 | if (!event) { |
| 1996 | ret = EFI_OUT_OF_RESOURCES; |
| 1997 | goto out; |
| 1998 | } |
| 1999 | |
| 2000 | event->table_description_size = sizeof(SMBIOS_HANDOFF_TABLE_DESC); |
| 2001 | memcpy(event->table_description, SMBIOS_HANDOFF_TABLE_DESC, |
| 2002 | sizeof(SMBIOS_HANDOFF_TABLE_DESC)); |
| 2003 | put_unaligned_le64(1, &event->number_of_tables); |
| 2004 | guidcpy(&event->table_entry[0].guid, &smbios_guid); |
| 2005 | smbios_copy = (struct smbios_header *)((uintptr_t)&event->table_entry[0].table); |
| 2006 | memcpy(&event->table_entry[0].table, |
| 2007 | (void *)((uintptr_t)entry->struct_table_address), |
| 2008 | entry->struct_table_length); |
| 2009 | |
| 2010 | smbios_prepare_measurement(entry, smbios_copy); |
| 2011 | |
| 2012 | ret = tcg2_measure_event(dev, 1, EV_EFI_HANDOFF_TABLES2, event_size, |
| 2013 | (u8 *)event); |
| 2014 | if (ret != EFI_SUCCESS) |
| 2015 | goto out; |
| 2016 | |
| 2017 | out: |
| 2018 | free(event); |
| 2019 | |
| 2020 | return ret; |
| 2021 | } |
| 2022 | |
| 2023 | /** |
| 2024 | * find_smbios_table() - find smbios table |
| 2025 | * |
| 2026 | * Return: pointer to the smbios table |
| 2027 | */ |
| 2028 | static void *find_smbios_table(void) |
| 2029 | { |
| 2030 | u32 i; |
| 2031 | |
| 2032 | for (i = 0; i < systab.nr_tables; i++) { |
| 2033 | if (!guidcmp(&smbios_guid, &systab.tables[i].guid)) |
| 2034 | return systab.tables[i].table; |
| 2035 | } |
| 2036 | |
| 2037 | return NULL; |
| 2038 | } |
| 2039 | |
| 2040 | /** |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 2041 | * tcg2_measure_gpt_table() - measure gpt table |
| 2042 | * |
| 2043 | * @dev: TPM device |
| 2044 | * @loaded_image: handle to the loaded image |
| 2045 | * |
| 2046 | * Return: status code |
| 2047 | */ |
| 2048 | static efi_status_t |
| 2049 | tcg2_measure_gpt_data(struct udevice *dev, |
| 2050 | struct efi_loaded_image_obj *loaded_image) |
| 2051 | { |
| 2052 | efi_status_t ret; |
| 2053 | efi_handle_t handle; |
Heinrich Schuchardt | 1100d15 | 2022-10-07 14:28:18 +0200 | [diff] [blame] | 2054 | struct efi_handler *dp_handler, *io_handler; |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 2055 | struct efi_device_path *orig_device_path; |
| 2056 | struct efi_device_path *device_path; |
| 2057 | struct efi_device_path *dp; |
| 2058 | struct efi_block_io *block_io; |
| 2059 | struct efi_gpt_data *event = NULL; |
| 2060 | efi_guid_t null_guid = NULL_GUID; |
| 2061 | gpt_header *gpt_h; |
| 2062 | gpt_entry *entry = NULL; |
| 2063 | gpt_entry *gpt_e; |
| 2064 | u32 num_of_valid_entry = 0; |
| 2065 | u32 event_size; |
| 2066 | u32 i; |
| 2067 | u32 total_gpt_entry_size; |
| 2068 | |
| 2069 | ret = efi_search_protocol(&loaded_image->header, |
| 2070 | &efi_guid_loaded_image_device_path, |
| 2071 | &dp_handler); |
| 2072 | if (ret != EFI_SUCCESS) |
| 2073 | return ret; |
| 2074 | |
| 2075 | orig_device_path = dp_handler->protocol_interface; |
| 2076 | if (!orig_device_path) /* no device path, skip GPT measurement */ |
| 2077 | return EFI_SUCCESS; |
| 2078 | |
| 2079 | device_path = efi_dp_dup(orig_device_path); |
| 2080 | if (!device_path) |
| 2081 | return EFI_OUT_OF_RESOURCES; |
| 2082 | |
| 2083 | dp = search_gpt_dp_node(device_path); |
| 2084 | if (!dp) { |
| 2085 | /* no GPT device path node found, skip GPT measurement */ |
| 2086 | ret = EFI_SUCCESS; |
| 2087 | goto out1; |
| 2088 | } |
| 2089 | |
| 2090 | /* read GPT header */ |
| 2091 | dp->type = DEVICE_PATH_TYPE_END; |
| 2092 | dp->sub_type = DEVICE_PATH_SUB_TYPE_END; |
| 2093 | dp = device_path; |
| 2094 | ret = EFI_CALL(systab.boottime->locate_device_path(&efi_block_io_guid, |
| 2095 | &dp, &handle)); |
| 2096 | if (ret != EFI_SUCCESS) |
| 2097 | goto out1; |
| 2098 | |
Heinrich Schuchardt | 1100d15 | 2022-10-07 14:28:18 +0200 | [diff] [blame] | 2099 | ret = efi_search_protocol(handle, &efi_block_io_guid, &io_handler); |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 2100 | if (ret != EFI_SUCCESS) |
| 2101 | goto out1; |
Heinrich Schuchardt | 1100d15 | 2022-10-07 14:28:18 +0200 | [diff] [blame] | 2102 | block_io = io_handler->protocol_interface; |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 2103 | |
| 2104 | gpt_h = memalign(block_io->media->io_align, block_io->media->block_size); |
| 2105 | if (!gpt_h) { |
| 2106 | ret = EFI_OUT_OF_RESOURCES; |
| 2107 | goto out2; |
| 2108 | } |
| 2109 | |
| 2110 | ret = block_io->read_blocks(block_io, block_io->media->media_id, 1, |
| 2111 | block_io->media->block_size, gpt_h); |
| 2112 | if (ret != EFI_SUCCESS) |
| 2113 | goto out2; |
| 2114 | |
| 2115 | /* read GPT entry */ |
| 2116 | total_gpt_entry_size = gpt_h->num_partition_entries * |
| 2117 | gpt_h->sizeof_partition_entry; |
| 2118 | entry = memalign(block_io->media->io_align, total_gpt_entry_size); |
| 2119 | if (!entry) { |
| 2120 | ret = EFI_OUT_OF_RESOURCES; |
| 2121 | goto out2; |
| 2122 | } |
| 2123 | |
| 2124 | ret = block_io->read_blocks(block_io, block_io->media->media_id, |
| 2125 | gpt_h->partition_entry_lba, |
| 2126 | total_gpt_entry_size, entry); |
| 2127 | if (ret != EFI_SUCCESS) |
| 2128 | goto out2; |
| 2129 | |
| 2130 | /* count valid GPT entry */ |
| 2131 | gpt_e = entry; |
| 2132 | for (i = 0; i < gpt_h->num_partition_entries; i++) { |
| 2133 | if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) |
| 2134 | num_of_valid_entry++; |
| 2135 | |
| 2136 | gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry); |
| 2137 | } |
| 2138 | |
| 2139 | /* prepare event data for measurement */ |
| 2140 | event_size = sizeof(struct efi_gpt_data) + |
| 2141 | (num_of_valid_entry * gpt_h->sizeof_partition_entry); |
| 2142 | event = calloc(1, event_size); |
| 2143 | if (!event) { |
| 2144 | ret = EFI_OUT_OF_RESOURCES; |
| 2145 | goto out2; |
| 2146 | } |
| 2147 | memcpy(event, gpt_h, sizeof(gpt_header)); |
| 2148 | put_unaligned_le64(num_of_valid_entry, &event->number_of_partitions); |
| 2149 | |
| 2150 | /* copy valid GPT entry */ |
| 2151 | gpt_e = entry; |
| 2152 | num_of_valid_entry = 0; |
| 2153 | for (i = 0; i < gpt_h->num_partition_entries; i++) { |
| 2154 | if (guidcmp(&null_guid, &gpt_e->partition_type_guid)) { |
| 2155 | memcpy((u8 *)event->partitions + |
| 2156 | (num_of_valid_entry * gpt_h->sizeof_partition_entry), |
| 2157 | gpt_e, gpt_h->sizeof_partition_entry); |
| 2158 | num_of_valid_entry++; |
| 2159 | } |
| 2160 | |
| 2161 | gpt_e = (gpt_entry *)((u8 *)gpt_e + gpt_h->sizeof_partition_entry); |
| 2162 | } |
| 2163 | |
| 2164 | ret = tcg2_measure_event(dev, 5, EV_EFI_GPT_EVENT, event_size, (u8 *)event); |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 2165 | |
| 2166 | out2: |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 2167 | free(gpt_h); |
| 2168 | free(entry); |
| 2169 | free(event); |
| 2170 | out1: |
| 2171 | efi_free_pool(device_path); |
| 2172 | |
| 2173 | return ret; |
| 2174 | } |
| 2175 | |
Etienne Carriere | b906435 | 2023-02-16 17:29:48 +0100 | [diff] [blame] | 2176 | /* Return the byte size of reserved map area in DTB or -1 upon error */ |
| 2177 | static ssize_t size_of_rsvmap(void *dtb) |
| 2178 | { |
| 2179 | struct fdt_reserve_entry e; |
| 2180 | ssize_t size_max; |
| 2181 | ssize_t size; |
| 2182 | u8 *rsvmap_base; |
| 2183 | |
| 2184 | rsvmap_base = (u8 *)dtb + fdt_off_mem_rsvmap(dtb); |
| 2185 | size_max = fdt_totalsize(dtb) - fdt_off_mem_rsvmap(dtb); |
| 2186 | size = 0; |
| 2187 | |
| 2188 | do { |
| 2189 | memcpy(&e, rsvmap_base + size, sizeof(e)); |
| 2190 | size += sizeof(e); |
| 2191 | if (size > size_max) |
| 2192 | return -1; |
| 2193 | } while (e.size); |
| 2194 | |
| 2195 | return size; |
| 2196 | } |
| 2197 | |
| 2198 | /** |
| 2199 | * efi_tcg2_measure_dtb() - measure DTB passed to the OS |
| 2200 | * |
| 2201 | * @dtb: pointer to the device tree blob |
| 2202 | * |
| 2203 | * Return: status code |
| 2204 | */ |
| 2205 | efi_status_t efi_tcg2_measure_dtb(void *dtb) |
| 2206 | { |
| 2207 | struct uefi_platform_firmware_blob2 *blob; |
| 2208 | struct fdt_header *header; |
| 2209 | sha256_context hash_ctx; |
| 2210 | struct udevice *dev; |
| 2211 | ssize_t rsvmap_size; |
| 2212 | efi_status_t ret; |
| 2213 | u32 event_size; |
| 2214 | |
| 2215 | if (!is_tcg2_protocol_installed()) |
| 2216 | return EFI_SUCCESS; |
| 2217 | |
| 2218 | ret = platform_get_tpm2_device(&dev); |
| 2219 | if (ret != EFI_SUCCESS) |
| 2220 | return EFI_SECURITY_VIOLATION; |
| 2221 | |
| 2222 | rsvmap_size = size_of_rsvmap(dtb); |
| 2223 | if (rsvmap_size < 0) |
| 2224 | return EFI_SECURITY_VIOLATION; |
| 2225 | |
| 2226 | event_size = sizeof(*blob) + sizeof(EFI_DTB_EVENT_STRING) + SHA256_SUM_LEN; |
| 2227 | blob = calloc(1, event_size); |
| 2228 | if (!blob) |
| 2229 | return EFI_OUT_OF_RESOURCES; |
| 2230 | |
| 2231 | blob->blob_description_size = sizeof(EFI_DTB_EVENT_STRING); |
| 2232 | memcpy(blob->data, EFI_DTB_EVENT_STRING, blob->blob_description_size); |
| 2233 | |
| 2234 | /* Measure populated areas of the DTB */ |
| 2235 | header = dtb; |
| 2236 | sha256_starts(&hash_ctx); |
| 2237 | sha256_update(&hash_ctx, (u8 *)header, sizeof(struct fdt_header)); |
| 2238 | sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_struct(dtb), fdt_size_dt_strings(dtb)); |
| 2239 | sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_dt_strings(dtb), fdt_size_dt_struct(dtb)); |
| 2240 | sha256_update(&hash_ctx, (u8 *)dtb + fdt_off_mem_rsvmap(dtb), rsvmap_size); |
| 2241 | sha256_finish(&hash_ctx, blob->data + blob->blob_description_size); |
| 2242 | |
| 2243 | ret = tcg2_measure_event(dev, 0, EV_POST_CODE, event_size, (u8 *)blob); |
| 2244 | |
| 2245 | free(blob); |
| 2246 | return ret; |
| 2247 | } |
| 2248 | |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 2249 | /** |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 2250 | * efi_tcg2_measure_efi_app_invocation() - measure efi app invocation |
| 2251 | * |
| 2252 | * Return: status code |
| 2253 | */ |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 2254 | 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] | 2255 | { |
| 2256 | efi_status_t ret; |
| 2257 | u32 pcr_index; |
| 2258 | struct udevice *dev; |
| 2259 | u32 event = 0; |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 2260 | struct smbios_entry *entry; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 2261 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 2262 | if (!is_tcg2_protocol_installed()) |
| 2263 | return EFI_SUCCESS; |
| 2264 | |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 2265 | if (tcg2_efi_app_invoked) |
| 2266 | return EFI_SUCCESS; |
| 2267 | |
| 2268 | ret = platform_get_tpm2_device(&dev); |
| 2269 | if (ret != EFI_SUCCESS) |
Masahisa Kojima | 38155ea | 2021-12-07 14:15:33 +0900 | [diff] [blame] | 2270 | return EFI_SECURITY_VIOLATION; |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 2271 | |
| 2272 | ret = tcg2_measure_boot_variable(dev); |
| 2273 | if (ret != EFI_SUCCESS) |
| 2274 | goto out; |
| 2275 | |
| 2276 | ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION, |
| 2277 | strlen(EFI_CALLING_EFI_APPLICATION), |
| 2278 | (u8 *)EFI_CALLING_EFI_APPLICATION); |
| 2279 | if (ret != EFI_SUCCESS) |
| 2280 | goto out; |
| 2281 | |
Masahisa Kojima | cd1fe7d | 2021-10-26 17:27:24 +0900 | [diff] [blame] | 2282 | entry = (struct smbios_entry *)find_smbios_table(); |
| 2283 | if (entry) { |
| 2284 | ret = tcg2_measure_smbios(dev, entry); |
| 2285 | if (ret != EFI_SUCCESS) |
| 2286 | goto out; |
| 2287 | } |
| 2288 | |
Masahisa Kojima | 6460c3e | 2021-10-26 17:27:25 +0900 | [diff] [blame] | 2289 | ret = tcg2_measure_gpt_data(dev, handle); |
| 2290 | if (ret != EFI_SUCCESS) |
| 2291 | goto out; |
| 2292 | |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 2293 | for (pcr_index = 0; pcr_index <= 7; pcr_index++) { |
| 2294 | ret = tcg2_measure_event(dev, pcr_index, EV_SEPARATOR, |
| 2295 | sizeof(event), (u8 *)&event); |
| 2296 | if (ret != EFI_SUCCESS) |
| 2297 | goto out; |
| 2298 | } |
| 2299 | |
| 2300 | tcg2_efi_app_invoked = true; |
| 2301 | out: |
| 2302 | return ret; |
| 2303 | } |
| 2304 | |
| 2305 | /** |
| 2306 | * efi_tcg2_measure_efi_app_exit() - measure efi app exit |
| 2307 | * |
| 2308 | * Return: status code |
| 2309 | */ |
| 2310 | efi_status_t efi_tcg2_measure_efi_app_exit(void) |
| 2311 | { |
| 2312 | efi_status_t ret; |
| 2313 | struct udevice *dev; |
| 2314 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 2315 | if (!is_tcg2_protocol_installed()) |
| 2316 | return EFI_SUCCESS; |
| 2317 | |
Masahisa Kojima | 8173cd4 | 2021-08-13 16:12:40 +0900 | [diff] [blame] | 2318 | ret = platform_get_tpm2_device(&dev); |
| 2319 | if (ret != EFI_SUCCESS) |
| 2320 | return ret; |
| 2321 | |
| 2322 | ret = tcg2_measure_event(dev, 4, EV_EFI_ACTION, |
| 2323 | strlen(EFI_RETURNING_FROM_EFI_APPLICATION), |
| 2324 | (u8 *)EFI_RETURNING_FROM_EFI_APPLICATION); |
| 2325 | return ret; |
| 2326 | } |
| 2327 | |
| 2328 | /** |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 2329 | * efi_tcg2_notify_exit_boot_services() - ExitBootService callback |
| 2330 | * |
| 2331 | * @event: callback event |
| 2332 | * @context: callback context |
| 2333 | */ |
| 2334 | static void EFIAPI |
| 2335 | efi_tcg2_notify_exit_boot_services(struct efi_event *event, void *context) |
| 2336 | { |
| 2337 | efi_status_t ret; |
| 2338 | struct udevice *dev; |
| 2339 | |
| 2340 | EFI_ENTRY("%p, %p", event, context); |
| 2341 | |
Ilias Apalodimas | 24e841a | 2021-11-18 09:03:39 +0200 | [diff] [blame] | 2342 | event_log.ebs_called = true; |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 2343 | |
| 2344 | if (!is_tcg2_protocol_installed()) { |
| 2345 | ret = EFI_SUCCESS; |
| 2346 | goto out; |
| 2347 | } |
| 2348 | |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 2349 | ret = platform_get_tpm2_device(&dev); |
| 2350 | if (ret != EFI_SUCCESS) |
| 2351 | goto out; |
| 2352 | |
| 2353 | ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION, |
| 2354 | strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION), |
| 2355 | (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION); |
| 2356 | if (ret != EFI_SUCCESS) |
| 2357 | goto out; |
| 2358 | |
| 2359 | ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION, |
| 2360 | strlen(EFI_EXIT_BOOT_SERVICES_SUCCEEDED), |
| 2361 | (u8 *)EFI_EXIT_BOOT_SERVICES_SUCCEEDED); |
| 2362 | |
| 2363 | out: |
| 2364 | EFI_EXIT(ret); |
| 2365 | } |
| 2366 | |
| 2367 | /** |
| 2368 | * efi_tcg2_notify_exit_boot_services_failed() |
| 2369 | * - notify ExitBootServices() is failed |
| 2370 | * |
| 2371 | * Return: status code |
| 2372 | */ |
| 2373 | efi_status_t efi_tcg2_notify_exit_boot_services_failed(void) |
| 2374 | { |
| 2375 | struct udevice *dev; |
| 2376 | efi_status_t ret; |
| 2377 | |
Masahisa Kojima | fd19a7e | 2021-12-07 14:15:32 +0900 | [diff] [blame] | 2378 | if (!is_tcg2_protocol_installed()) |
| 2379 | return EFI_SUCCESS; |
| 2380 | |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 2381 | ret = platform_get_tpm2_device(&dev); |
| 2382 | if (ret != EFI_SUCCESS) |
| 2383 | goto out; |
| 2384 | |
| 2385 | ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION, |
| 2386 | strlen(EFI_EXIT_BOOT_SERVICES_INVOCATION), |
| 2387 | (u8 *)EFI_EXIT_BOOT_SERVICES_INVOCATION); |
| 2388 | if (ret != EFI_SUCCESS) |
| 2389 | goto out; |
| 2390 | |
| 2391 | ret = tcg2_measure_event(dev, 5, EV_EFI_ACTION, |
| 2392 | strlen(EFI_EXIT_BOOT_SERVICES_FAILED), |
| 2393 | (u8 *)EFI_EXIT_BOOT_SERVICES_FAILED); |
| 2394 | |
| 2395 | out: |
| 2396 | return ret; |
| 2397 | } |
| 2398 | |
| 2399 | /** |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 2400 | * tcg2_measure_secure_boot_variable() - measure secure boot variables |
| 2401 | * |
| 2402 | * @dev: TPM device |
| 2403 | * |
| 2404 | * Return: status code |
| 2405 | */ |
| 2406 | static efi_status_t tcg2_measure_secure_boot_variable(struct udevice *dev) |
| 2407 | { |
| 2408 | u8 *data; |
| 2409 | efi_uintn_t data_size; |
| 2410 | u32 count, i; |
| 2411 | efi_status_t ret; |
Masahisa Kojima | f3e0c55 | 2021-10-26 17:27:27 +0900 | [diff] [blame] | 2412 | u8 deployed_mode; |
| 2413 | efi_uintn_t size; |
| 2414 | u32 deployed_audit_pcr_index = 1; |
| 2415 | |
| 2416 | size = sizeof(deployed_mode); |
| 2417 | ret = efi_get_variable_int(u"DeployedMode", &efi_global_variable_guid, |
| 2418 | NULL, &size, &deployed_mode, NULL); |
| 2419 | if (ret != EFI_SUCCESS || !deployed_mode) |
| 2420 | deployed_audit_pcr_index = 7; |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 2421 | |
| 2422 | count = ARRAY_SIZE(secure_variables); |
| 2423 | for (i = 0; i < count; i++) { |
Heinrich Schuchardt | 6f26e7c | 2021-09-09 08:50:01 +0200 | [diff] [blame] | 2424 | const efi_guid_t *guid; |
| 2425 | |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 2426 | guid = efi_auth_var_get_guid(secure_variables[i].name); |
Heinrich Schuchardt | 6f26e7c | 2021-09-09 08:50:01 +0200 | [diff] [blame] | 2427 | |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 2428 | data = efi_get_var(secure_variables[i].name, guid, &data_size); |
| 2429 | if (!data && !secure_variables[i].accept_empty) |
| 2430 | continue; |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 2431 | |
Masahisa Kojima | f3e0c55 | 2021-10-26 17:27:27 +0900 | [diff] [blame] | 2432 | if (u16_strcmp(u"DeployedMode", secure_variables[i].name)) |
| 2433 | secure_variables[i].pcr_index = deployed_audit_pcr_index; |
| 2434 | if (u16_strcmp(u"AuditMode", secure_variables[i].name)) |
| 2435 | secure_variables[i].pcr_index = deployed_audit_pcr_index; |
| 2436 | |
| 2437 | ret = tcg2_measure_variable(dev, secure_variables[i].pcr_index, |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 2438 | EV_EFI_VARIABLE_DRIVER_CONFIG, |
Masahisa Kojima | 2168452 | 2021-10-26 17:27:26 +0900 | [diff] [blame] | 2439 | secure_variables[i].name, guid, |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 2440 | data_size, data); |
| 2441 | free(data); |
| 2442 | if (ret != EFI_SUCCESS) |
| 2443 | goto error; |
| 2444 | } |
| 2445 | |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 2446 | error: |
| 2447 | return ret; |
| 2448 | } |
| 2449 | |
| 2450 | /** |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 2451 | * efi_tcg2_do_initial_measurement() - do initial measurement |
| 2452 | * |
| 2453 | * Return: status code |
| 2454 | */ |
| 2455 | efi_status_t efi_tcg2_do_initial_measurement(void) |
| 2456 | { |
| 2457 | efi_status_t ret; |
| 2458 | struct udevice *dev; |
| 2459 | |
| 2460 | if (!is_tcg2_protocol_installed()) |
| 2461 | return EFI_SUCCESS; |
| 2462 | |
| 2463 | ret = platform_get_tpm2_device(&dev); |
| 2464 | if (ret != EFI_SUCCESS) |
| 2465 | return EFI_SECURITY_VIOLATION; |
| 2466 | |
| 2467 | ret = tcg2_measure_secure_boot_variable(dev); |
| 2468 | if (ret != EFI_SUCCESS) |
| 2469 | goto out; |
| 2470 | |
| 2471 | out: |
| 2472 | return ret; |
| 2473 | } |
| 2474 | |
| 2475 | /** |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 2476 | * efi_tcg2_register() - register EFI_TCG2_PROTOCOL |
| 2477 | * |
| 2478 | * If a TPM2 device is available, the TPM TCG2 Protocol is registered |
| 2479 | * |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 2480 | * Return: status code |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 2481 | */ |
| 2482 | efi_status_t efi_tcg2_register(void) |
| 2483 | { |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 2484 | efi_status_t ret = EFI_SUCCESS; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 2485 | struct udevice *dev; |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 2486 | struct efi_event *event; |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 2487 | u32 err; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 2488 | |
| 2489 | ret = platform_get_tpm2_device(&dev); |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 2490 | if (ret != EFI_SUCCESS) { |
Ilias Apalodimas | eb1b6b4 | 2023-01-19 16:29:15 +0200 | [diff] [blame] | 2491 | log_warning("Missing TPMv2 device for EFI_TCG_PROTOCOL\n"); |
Ilias Apalodimas | fa5217d | 2021-05-10 21:19:14 +0300 | [diff] [blame] | 2492 | return EFI_SUCCESS; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 2493 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 2494 | |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 2495 | /* initialize the TPM as early as possible. */ |
Ilias Apalodimas | 0c95d22 | 2023-01-25 13:06:03 +0200 | [diff] [blame] | 2496 | err = tpm_auto_start(dev); |
Ilias Apalodimas | 1d16f1e | 2021-11-18 10:13:42 +0200 | [diff] [blame] | 2497 | if (err) { |
| 2498 | log_err("TPM startup failed\n"); |
| 2499 | goto fail; |
| 2500 | } |
| 2501 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 2502 | ret = efi_init_event_log(); |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 2503 | if (ret != EFI_SUCCESS) { |
| 2504 | tcg2_uninit(); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 2505 | goto fail; |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 2506 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 2507 | |
Ilias Apalodimas | 4953c99 | 2023-06-19 14:14:02 +0300 | [diff] [blame] | 2508 | ret = efi_install_multiple_protocol_interfaces(&efi_root, &efi_guid_tcg2_protocol, |
| 2509 | &efi_tcg2_protocol, NULL); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 2510 | if (ret != EFI_SUCCESS) { |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 2511 | tcg2_uninit(); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 2512 | goto fail; |
| 2513 | } |
Masahisa Kojima | 1d2a656 | 2021-08-13 16:12:39 +0900 | [diff] [blame] | 2514 | |
Masahisa Kojima | 1ac19bb | 2021-08-13 16:12:41 +0900 | [diff] [blame] | 2515 | ret = efi_create_event(EVT_SIGNAL_EXIT_BOOT_SERVICES, TPL_CALLBACK, |
| 2516 | efi_tcg2_notify_exit_boot_services, NULL, |
| 2517 | NULL, &event); |
| 2518 | if (ret != EFI_SUCCESS) { |
| 2519 | tcg2_uninit(); |
| 2520 | goto fail; |
| 2521 | } |
| 2522 | |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 2523 | return ret; |
Ilias Apalodimas | fa5217d | 2021-05-10 21:19:14 +0300 | [diff] [blame] | 2524 | |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 2525 | fail: |
Ilias Apalodimas | 5a2baf9 | 2021-05-12 00:03:41 +0300 | [diff] [blame] | 2526 | log_err("Cannot install EFI_TCG2_PROTOCOL\n"); |
Masahisa Kojima | 0fd4379 | 2021-12-07 14:15:31 +0900 | [diff] [blame] | 2527 | return ret; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 2528 | } |