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