Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Defines APIs that allow an OS to interact with UEFI firmware to query |
| 4 | * information about the device. |
| 5 | * https://trustedcomputinggroup.org/resource/tcg-efi-protocol-specification/ |
| 6 | * |
| 7 | * Copyright (c) 2020, Linaro Limited |
| 8 | */ |
| 9 | |
| 10 | #define LOG_CATEGORY LOGC_EFI |
| 11 | #include <common.h> |
| 12 | #include <dm.h> |
| 13 | #include <efi_loader.h> |
| 14 | #include <efi_tcg2.h> |
| 15 | #include <log.h> |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 16 | #include <version.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 17 | #include <tpm-v2.h> |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 18 | #include <u-boot/sha1.h> |
| 19 | #include <u-boot/sha256.h> |
| 20 | #include <u-boot/sha512.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 21 | #include <linux/unaligned/access_ok.h> |
| 22 | #include <linux/unaligned/generic.h> |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 23 | #include <hexdump.h> |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 24 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 25 | struct event_log_buffer { |
| 26 | void *buffer; |
| 27 | void *final_buffer; |
| 28 | size_t pos; /* eventlog position */ |
| 29 | size_t final_pos; /* final events config table position */ |
| 30 | size_t last_event_size; |
| 31 | bool get_event_called; |
| 32 | bool truncated; |
| 33 | }; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 34 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 35 | static struct event_log_buffer event_log; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 36 | /* |
| 37 | * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset. |
| 38 | * Since the current tpm2_get_capability() response buffers starts at |
| 39 | * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate |
| 40 | * the response size and offset once for all consumers |
| 41 | */ |
| 42 | #define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \ |
| 43 | offsetof(struct tpms_capability_data, data)) |
| 44 | #define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \ |
| 45 | offsetof(struct tpms_tagged_property, value)) |
| 46 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 47 | static const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID; |
| 48 | static const efi_guid_t efi_guid_final_events = EFI_TCG2_FINAL_EVENTS_TABLE_GUID; |
| 49 | |
| 50 | struct digest_info { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 51 | u16 hash_alg; |
| 52 | u32 hash_mask; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 53 | u16 hash_len; |
| 54 | }; |
| 55 | |
| 56 | const static struct digest_info hash_algo_list[] = { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 57 | { |
| 58 | TPM2_ALG_SHA1, |
| 59 | EFI_TCG2_BOOT_HASH_ALG_SHA1, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 60 | TPM2_SHA1_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 61 | }, |
| 62 | { |
| 63 | TPM2_ALG_SHA256, |
| 64 | EFI_TCG2_BOOT_HASH_ALG_SHA256, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 65 | TPM2_SHA256_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 66 | }, |
| 67 | { |
| 68 | TPM2_ALG_SHA384, |
| 69 | EFI_TCG2_BOOT_HASH_ALG_SHA384, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 70 | TPM2_SHA384_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 71 | }, |
| 72 | { |
| 73 | TPM2_ALG_SHA512, |
| 74 | EFI_TCG2_BOOT_HASH_ALG_SHA512, |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 75 | TPM2_SHA512_DIGEST_SIZE, |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 76 | }, |
| 77 | }; |
| 78 | |
| 79 | #define MAX_HASH_COUNT ARRAY_SIZE(hash_algo_list) |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 80 | |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 81 | /** |
| 82 | * alg_to_mask - Get a TCG hash mask for algorithms |
| 83 | * |
| 84 | * @hash_alg: TCG defined algorithm |
| 85 | * |
| 86 | * @Return: TCG hashing algorithm bitmaps, 0 if the algorithm is not supported |
| 87 | */ |
| 88 | static u32 alg_to_mask(u16 hash_alg) |
| 89 | { |
| 90 | int i; |
| 91 | |
| 92 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 93 | if (hash_algo_list[i].hash_alg == hash_alg) |
| 94 | return hash_algo_list[i].hash_mask; |
| 95 | } |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 100 | /** |
| 101 | * alg_to_len - Get a TCG hash len for algorithms |
| 102 | * |
| 103 | * @hash_alg: TCG defined algorithm |
| 104 | * |
| 105 | * @Return: len of chosen algorithm, 0 if the algorithm is not supported |
| 106 | */ |
| 107 | static u16 alg_to_len(u16 hash_alg) |
| 108 | { |
| 109 | int i; |
| 110 | |
| 111 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 112 | if (hash_algo_list[i].hash_alg == hash_alg) |
| 113 | return hash_algo_list[i].hash_len; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static u32 tcg_event_final_size(struct tpml_digest_values *digest_list) |
| 120 | { |
| 121 | u32 len; |
| 122 | int i; |
| 123 | |
| 124 | len = offsetof(struct tcg_pcr_event2, digests); |
| 125 | len += offsetof(struct tpml_digest_values, digests); |
| 126 | for (i = 0; i < digest_list->count; i++) { |
| 127 | u16 hash_alg = digest_list->digests[i].hash_alg; |
| 128 | |
| 129 | len += offsetof(struct tpmt_ha, digest); |
| 130 | len += alg_to_len(hash_alg); |
| 131 | } |
| 132 | len += sizeof(u32); /* tcg_pcr_event2 event_size*/ |
| 133 | |
| 134 | return len; |
| 135 | } |
| 136 | |
| 137 | /* tcg2_pcr_extend - Extend PCRs for a TPM2 device for a given tpml_digest_values |
| 138 | * |
| 139 | * @dev: device |
| 140 | * @digest_list: list of digest algorithms to extend |
| 141 | * |
| 142 | * @Return: status code |
| 143 | */ |
| 144 | static efi_status_t tcg2_pcr_extend(struct udevice *dev, u32 pcr_index, |
| 145 | struct tpml_digest_values *digest_list) |
| 146 | { |
| 147 | u32 rc; |
| 148 | int i; |
| 149 | |
| 150 | for (i = 0; i < digest_list->count; i++) { |
| 151 | u32 alg = digest_list->digests[i].hash_alg; |
| 152 | |
| 153 | rc = tpm2_pcr_extend(dev, pcr_index, alg, |
| 154 | (u8 *)&digest_list->digests[i].digest, |
| 155 | alg_to_len(alg)); |
| 156 | if (rc) { |
| 157 | EFI_PRINT("Failed to extend PCR\n"); |
| 158 | return EFI_DEVICE_ERROR; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | return EFI_SUCCESS; |
| 163 | } |
| 164 | |
| 165 | /* tcg2_agile_log_append - Append an agile event to out eventlog |
| 166 | * |
| 167 | * @pcr_index: PCR index |
| 168 | * @event_type: type of event added |
| 169 | * @digest_list: list of digest algorithms to add |
| 170 | * @size: size of event |
| 171 | * @event: event to add |
| 172 | * |
| 173 | * @Return: status code |
| 174 | */ |
| 175 | static efi_status_t tcg2_agile_log_append(u32 pcr_index, u32 event_type, |
| 176 | struct tpml_digest_values *digest_list, |
| 177 | u32 size, u8 event[]) |
| 178 | { |
| 179 | void *log = event_log.buffer + event_log.pos; |
| 180 | size_t pos; |
| 181 | int i; |
| 182 | u32 event_size; |
| 183 | |
| 184 | if (event_log.get_event_called) |
| 185 | log = event_log.final_buffer + event_log.final_pos; |
| 186 | |
| 187 | /* |
| 188 | * size refers to the length of event[] only, we need to check against |
| 189 | * the final tcg_pcr_event2 size |
| 190 | */ |
| 191 | event_size = size + tcg_event_final_size(digest_list); |
| 192 | if (event_log.pos + event_size > TPM2_EVENT_LOG_SIZE || |
| 193 | event_log.final_pos + event_size > TPM2_EVENT_LOG_SIZE) { |
| 194 | event_log.truncated = true; |
| 195 | return EFI_VOLUME_FULL; |
| 196 | } |
| 197 | |
| 198 | put_unaligned_le32(pcr_index, log); |
| 199 | pos = offsetof(struct tcg_pcr_event2, event_type); |
| 200 | put_unaligned_le32(event_type, log + pos); |
| 201 | pos = offsetof(struct tcg_pcr_event2, digests); /* count */ |
| 202 | put_unaligned_le32(digest_list->count, log + pos); |
| 203 | |
| 204 | pos += offsetof(struct tpml_digest_values, digests); |
| 205 | for (i = 0; i < digest_list->count; i++) { |
| 206 | u16 hash_alg = digest_list->digests[i].hash_alg; |
| 207 | u8 *digest = (u8 *)&digest_list->digests[i].digest; |
| 208 | |
| 209 | put_unaligned_le16(hash_alg, log + pos); |
| 210 | pos += offsetof(struct tpmt_ha, digest); |
| 211 | memcpy(log + pos, digest, alg_to_len(hash_alg)); |
| 212 | pos += alg_to_len(hash_alg); |
| 213 | } |
| 214 | |
| 215 | put_unaligned_le32(size, log + pos); |
| 216 | pos += sizeof(u32); /* tcg_pcr_event2 event_size*/ |
| 217 | memcpy(log + pos, event, size); |
| 218 | pos += size; |
| 219 | |
| 220 | /* make sure the calculated buffer is what we checked against */ |
| 221 | if (pos != event_size) |
| 222 | return EFI_INVALID_PARAMETER; |
| 223 | |
| 224 | /* if GetEventLog hasn't been called update the normal log */ |
| 225 | if (!event_log.get_event_called) { |
| 226 | event_log.pos += pos; |
| 227 | event_log.last_event_size = pos; |
| 228 | } else { |
| 229 | /* if GetEventLog has been called update config table log */ |
| 230 | struct efi_tcg2_final_events_table *final_event; |
| 231 | |
| 232 | final_event = |
| 233 | (struct efi_tcg2_final_events_table *)(event_log.final_buffer); |
| 234 | final_event->number_of_events++; |
| 235 | event_log.final_pos += pos; |
| 236 | } |
| 237 | |
| 238 | return EFI_SUCCESS; |
| 239 | } |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 240 | |
| 241 | /** |
| 242 | * platform_get_tpm_device() - retrieve TPM device |
| 243 | * |
| 244 | * This function retrieves the udevice implementing a TPM |
| 245 | * |
| 246 | * This function may be overridden if special initialization is needed. |
| 247 | * |
| 248 | * @dev: udevice |
| 249 | * Return: status code |
| 250 | */ |
| 251 | __weak efi_status_t platform_get_tpm2_device(struct udevice **dev) |
| 252 | { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 253 | for_each_tpm_device(*dev) { |
| 254 | /* Only support TPMv2 devices */ |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 255 | if (tpm_get_version(*dev) == TPM_V2) |
| 256 | return EFI_SUCCESS; |
| 257 | } |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 258 | |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 259 | return EFI_NOT_FOUND; |
| 260 | } |
| 261 | |
| 262 | /** |
| 263 | * tpm2_get_max_command_size() - get the supported max command size |
| 264 | * |
| 265 | * @dev: TPM device |
| 266 | * @max_command_size: output buffer for the size |
| 267 | * |
| 268 | * Return: 0 on success, -1 on error |
| 269 | */ |
| 270 | static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size) |
| 271 | { |
| 272 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 273 | u32 ret; |
| 274 | |
| 275 | memset(response, 0, sizeof(response)); |
| 276 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 277 | TPM2_PT_MAX_COMMAND_SIZE, response, 1); |
| 278 | if (ret) |
| 279 | return -1; |
| 280 | |
| 281 | *max_command_size = (uint16_t)get_unaligned_be32(response + |
| 282 | properties_offset); |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * tpm2_get_max_response_size() - get the supported max response size |
| 289 | * |
| 290 | * @dev: TPM device |
| 291 | * @max_response_size: output buffer for the size |
| 292 | * |
| 293 | * Return: 0 on success, -1 on error |
| 294 | */ |
| 295 | static int tpm2_get_max_response_size(struct udevice *dev, |
| 296 | u16 *max_response_size) |
| 297 | { |
| 298 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 299 | u32 ret; |
| 300 | |
| 301 | memset(response, 0, sizeof(response)); |
| 302 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 303 | TPM2_PT_MAX_RESPONSE_SIZE, response, 1); |
| 304 | if (ret) |
| 305 | return -1; |
| 306 | |
| 307 | *max_response_size = (uint16_t)get_unaligned_be32(response + |
| 308 | properties_offset); |
| 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * tpm2_get_manufacturer_id() - get the manufacturer ID |
| 315 | * |
| 316 | * @dev: TPM device |
| 317 | * @manufacturer_id: output buffer for the id |
| 318 | * |
| 319 | * Return: 0 on success, -1 on error |
| 320 | */ |
| 321 | static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id) |
| 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_MANUFACTURER, response, 1); |
| 329 | if (ret) |
| 330 | return -1; |
| 331 | |
| 332 | *manufacturer_id = get_unaligned_be32(response + properties_offset); |
| 333 | |
| 334 | return 0; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * tpm2_get_num_pcr() - get the number of PCRs |
| 339 | * |
| 340 | * @dev: TPM device |
| 341 | * @manufacturer_id: output buffer for the number |
| 342 | * |
| 343 | * Return: 0 on success, -1 on error |
| 344 | */ |
| 345 | static int tpm2_get_num_pcr(struct udevice *dev, u32 *num_pcr) |
| 346 | { |
| 347 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 348 | u32 ret; |
| 349 | |
| 350 | memset(response, 0, sizeof(response)); |
| 351 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 352 | TPM2_PT_PCR_COUNT, response, 1); |
| 353 | if (ret) |
| 354 | return -1; |
| 355 | |
| 356 | *num_pcr = get_unaligned_be32(response + properties_offset); |
| 357 | if (*num_pcr > TPM2_MAX_PCRS) |
| 358 | return -1; |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * is_active_pcr() - Check if a supported algorithm is active |
| 365 | * |
| 366 | * @dev: TPM device |
| 367 | * @selection: struct of PCR information |
| 368 | * |
| 369 | * Return: true if PCR is active |
| 370 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 371 | static bool is_active_pcr(struct tpms_pcr_selection *selection) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 372 | { |
| 373 | int i; |
| 374 | /* |
| 375 | * check the pcr_select. If at least one of the PCRs supports the |
| 376 | * algorithm add it on the active ones |
| 377 | */ |
| 378 | for (i = 0; i < selection->size_of_select; i++) { |
| 379 | if (selection->pcr_select[i]) |
| 380 | return true; |
| 381 | } |
| 382 | |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks |
| 388 | * |
| 389 | * @dev: TPM device |
| 390 | * @supported_pcr: bitmask with the algorithms supported |
| 391 | * @active_pcr: bitmask with the active algorithms |
| 392 | * @pcr_banks: number of PCR banks |
| 393 | * |
| 394 | * Return: 0 on success, -1 on error |
| 395 | */ |
| 396 | static int tpm2_get_pcr_info(struct udevice *dev, u32 *supported_pcr, |
| 397 | u32 *active_pcr, u32 *pcr_banks) |
| 398 | { |
| 399 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 400 | struct tpml_pcr_selection pcrs; |
| 401 | u32 ret, num_pcr; |
| 402 | int i, tpm_ret; |
| 403 | |
| 404 | memset(response, 0, sizeof(response)); |
| 405 | ret = tpm2_get_capability(dev, TPM2_CAP_PCRS, 0, response, 1); |
| 406 | if (ret) |
| 407 | goto out; |
| 408 | |
| 409 | pcrs.count = get_unaligned_be32(response); |
| 410 | /* |
| 411 | * We only support 5 algorithms for now so check against that |
| 412 | * instead of TPM2_NUM_PCR_BANKS |
| 413 | */ |
| 414 | if (pcrs.count > MAX_HASH_COUNT || pcrs.count < 1) |
| 415 | goto out; |
| 416 | |
| 417 | tpm_ret = tpm2_get_num_pcr(dev, &num_pcr); |
| 418 | if (tpm_ret) |
| 419 | goto out; |
| 420 | |
| 421 | for (i = 0; i < pcrs.count; i++) { |
| 422 | /* |
| 423 | * Definition of TPMS_PCR_SELECTION Structure |
| 424 | * hash: u16 |
| 425 | * size_of_select: u8 |
| 426 | * pcr_select: u8 array |
| 427 | * |
| 428 | * The offsets depend on the number of the device PCRs |
| 429 | * so we have to calculate them based on that |
| 430 | */ |
| 431 | u32 hash_offset = offsetof(struct tpml_pcr_selection, selection) + |
| 432 | i * offsetof(struct tpms_pcr_selection, pcr_select) + |
| 433 | i * ((num_pcr + 7) / 8); |
| 434 | u32 size_select_offset = |
| 435 | hash_offset + offsetof(struct tpms_pcr_selection, |
| 436 | size_of_select); |
| 437 | u32 pcr_select_offset = |
| 438 | hash_offset + offsetof(struct tpms_pcr_selection, |
| 439 | pcr_select); |
| 440 | |
| 441 | pcrs.selection[i].hash = |
| 442 | get_unaligned_be16(response + hash_offset); |
| 443 | pcrs.selection[i].size_of_select = |
| 444 | __get_unaligned_be(response + size_select_offset); |
| 445 | if (pcrs.selection[i].size_of_select > TPM2_PCR_SELECT_MAX) |
| 446 | goto out; |
| 447 | /* copy the array of pcr_select */ |
| 448 | memcpy(pcrs.selection[i].pcr_select, response + pcr_select_offset, |
| 449 | pcrs.selection[i].size_of_select); |
| 450 | } |
| 451 | |
| 452 | for (i = 0; i < pcrs.count; i++) { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 453 | u32 hash_mask = alg_to_mask(pcrs.selection[i].hash); |
| 454 | |
| 455 | if (hash_mask) { |
| 456 | *supported_pcr |= hash_mask; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 457 | if (is_active_pcr(&pcrs.selection[i])) |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 458 | *active_pcr |= hash_mask; |
| 459 | } else { |
| 460 | EFI_PRINT("Unknown algorithm %x\n", pcrs.selection[i].hash); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | *pcr_banks = pcrs.count; |
| 465 | |
| 466 | return 0; |
| 467 | out: |
| 468 | return -1; |
| 469 | } |
| 470 | |
| 471 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 472 | * __get_active_pcr_banks() - returns the currently active PCR banks |
| 473 | * |
| 474 | * @active_pcr_banks: pointer for receiving the bitmap of currently |
| 475 | * active PCR banks |
| 476 | * |
| 477 | * Return: status code |
| 478 | */ |
| 479 | static efi_status_t __get_active_pcr_banks(u32 *active_pcr_banks) |
| 480 | { |
| 481 | struct udevice *dev; |
| 482 | u32 active, supported, pcr_banks; |
| 483 | efi_status_t ret; |
| 484 | int err; |
| 485 | |
| 486 | ret = platform_get_tpm2_device(&dev); |
| 487 | if (ret != EFI_SUCCESS) |
| 488 | goto out; |
| 489 | |
| 490 | err = tpm2_get_pcr_info(dev, &supported, &active, &pcr_banks); |
| 491 | if (err) { |
| 492 | ret = EFI_DEVICE_ERROR; |
| 493 | goto out; |
| 494 | } |
| 495 | |
| 496 | *active_pcr_banks = active; |
| 497 | |
| 498 | out: |
| 499 | return ret; |
| 500 | } |
| 501 | |
| 502 | /* tcg2_create_digest - create a list of digests of the supported PCR banks |
| 503 | * for a given memory range |
| 504 | * |
| 505 | * @input: input memory |
| 506 | * @length: length of buffer to calculate the digest |
| 507 | * @digest_list: list of digests to fill in |
| 508 | * |
| 509 | * Return: status code |
| 510 | */ |
| 511 | static efi_status_t tcg2_create_digest(const u8 *input, u32 length, |
| 512 | struct tpml_digest_values *digest_list) |
| 513 | { |
| 514 | sha1_context ctx; |
| 515 | sha256_context ctx_256; |
| 516 | sha512_context ctx_512; |
| 517 | u8 final[TPM2_ALG_SHA512]; |
| 518 | efi_status_t ret; |
| 519 | u32 active; |
| 520 | int i; |
| 521 | |
| 522 | ret = __get_active_pcr_banks(&active); |
| 523 | if (ret != EFI_SUCCESS) |
| 524 | return ret; |
| 525 | |
| 526 | digest_list->count = 0; |
| 527 | for (i = 0; i < MAX_HASH_COUNT; i++) { |
| 528 | u16 hash_alg = hash_algo_list[i].hash_alg; |
| 529 | |
| 530 | if (!(active & alg_to_mask(hash_alg))) |
| 531 | continue; |
| 532 | switch (hash_alg) { |
| 533 | case TPM2_ALG_SHA1: |
| 534 | sha1_starts(&ctx); |
| 535 | sha1_update(&ctx, input, length); |
| 536 | sha1_finish(&ctx, final); |
| 537 | digest_list->count++; |
| 538 | break; |
| 539 | case TPM2_ALG_SHA256: |
| 540 | sha256_starts(&ctx_256); |
| 541 | sha256_update(&ctx_256, input, length); |
| 542 | sha256_finish(&ctx_256, final); |
| 543 | digest_list->count++; |
| 544 | break; |
| 545 | case TPM2_ALG_SHA384: |
| 546 | sha384_starts(&ctx_512); |
| 547 | sha384_update(&ctx_512, input, length); |
| 548 | sha384_finish(&ctx_512, final); |
| 549 | digest_list->count++; |
| 550 | break; |
| 551 | case TPM2_ALG_SHA512: |
| 552 | sha512_starts(&ctx_512); |
| 553 | sha512_update(&ctx_512, input, length); |
| 554 | sha512_finish(&ctx_512, final); |
| 555 | digest_list->count++; |
| 556 | break; |
| 557 | default: |
| 558 | EFI_PRINT("Unsupported algorithm %x\n", hash_alg); |
| 559 | return EFI_INVALID_PARAMETER; |
| 560 | } |
| 561 | digest_list->digests[i].hash_alg = hash_alg; |
| 562 | memcpy(&digest_list->digests[i].digest, final, (u32)alg_to_len(hash_alg)); |
| 563 | } |
| 564 | |
| 565 | return EFI_SUCCESS; |
| 566 | } |
| 567 | |
| 568 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 569 | * efi_tcg2_get_capability() - protocol capability information and state information |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 570 | * |
| 571 | * @this: TCG2 protocol instance |
| 572 | * @capability: caller allocated memory with size field to the size of |
| 573 | * the structure allocated |
| 574 | |
| 575 | * Return: status code |
| 576 | */ |
| 577 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 578 | efi_tcg2_get_capability(struct efi_tcg2_protocol *this, |
| 579 | struct efi_tcg2_boot_service_capability *capability) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 580 | { |
| 581 | struct udevice *dev; |
| 582 | efi_status_t efi_ret; |
| 583 | int ret; |
| 584 | |
| 585 | EFI_ENTRY("%p, %p", this, capability); |
| 586 | |
| 587 | if (!this || !capability) { |
| 588 | efi_ret = EFI_INVALID_PARAMETER; |
| 589 | goto out; |
| 590 | } |
| 591 | |
| 592 | if (capability->size < boot_service_capability_min) { |
| 593 | capability->size = boot_service_capability_min; |
| 594 | efi_ret = EFI_BUFFER_TOO_SMALL; |
| 595 | goto out; |
| 596 | } |
| 597 | |
| 598 | if (capability->size < sizeof(*capability)) { |
| 599 | capability->size = sizeof(*capability); |
| 600 | efi_ret = EFI_BUFFER_TOO_SMALL; |
| 601 | goto out; |
| 602 | } |
| 603 | |
| 604 | capability->structure_version.major = 1; |
| 605 | capability->structure_version.minor = 1; |
| 606 | capability->protocol_version.major = 1; |
| 607 | capability->protocol_version.minor = 1; |
| 608 | |
| 609 | efi_ret = platform_get_tpm2_device(&dev); |
| 610 | if (efi_ret != EFI_SUCCESS) { |
| 611 | capability->supported_event_logs = 0; |
| 612 | capability->hash_algorithm_bitmap = 0; |
| 613 | capability->tpm_present_flag = false; |
| 614 | capability->max_command_size = 0; |
| 615 | capability->max_response_size = 0; |
| 616 | capability->manufacturer_id = 0; |
| 617 | capability->number_of_pcr_banks = 0; |
| 618 | capability->active_pcr_banks = 0; |
| 619 | |
| 620 | efi_ret = EFI_SUCCESS; |
| 621 | goto out; |
| 622 | } |
| 623 | |
| 624 | /* We only allow a TPMv2 device to register the EFI protocol */ |
| 625 | capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2; |
| 626 | |
| 627 | capability->tpm_present_flag = true; |
| 628 | |
| 629 | /* Supported and active PCRs */ |
| 630 | capability->hash_algorithm_bitmap = 0; |
| 631 | capability->active_pcr_banks = 0; |
| 632 | ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap, |
| 633 | &capability->active_pcr_banks, |
| 634 | &capability->number_of_pcr_banks); |
| 635 | if (ret) { |
| 636 | efi_ret = EFI_DEVICE_ERROR; |
| 637 | goto out; |
| 638 | } |
| 639 | |
| 640 | /* Max command size */ |
| 641 | ret = tpm2_get_max_command_size(dev, &capability->max_command_size); |
| 642 | if (ret) { |
| 643 | efi_ret = EFI_DEVICE_ERROR; |
| 644 | goto out; |
| 645 | } |
| 646 | |
| 647 | /* Max response size */ |
| 648 | ret = tpm2_get_max_response_size(dev, &capability->max_response_size); |
| 649 | if (ret) { |
| 650 | efi_ret = EFI_DEVICE_ERROR; |
| 651 | goto out; |
| 652 | } |
| 653 | |
| 654 | /* Manufacturer ID */ |
| 655 | ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id); |
| 656 | if (ret) { |
| 657 | efi_ret = EFI_DEVICE_ERROR; |
| 658 | goto out; |
| 659 | } |
| 660 | |
| 661 | return EFI_EXIT(EFI_SUCCESS); |
| 662 | out: |
| 663 | return EFI_EXIT(efi_ret); |
| 664 | } |
| 665 | |
| 666 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 667 | * efi_tcg2_get_eventlog() - retrieve the the address of an event log and its |
| 668 | * last entry |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 669 | * |
| 670 | * @this: TCG2 protocol instance |
| 671 | * @log_format: type of event log format |
| 672 | * @event_log_location: pointer to the memory address of the event log |
| 673 | * @event_log_last_entry: pointer to the address of the start of the last |
| 674 | * entry in the event log in memory, if log contains |
| 675 | * more than 1 entry |
| 676 | * @event_log_truncated: set to true, if the Event Log is missing at i |
| 677 | * least one entry |
| 678 | * |
| 679 | * Return: status code |
| 680 | */ |
| 681 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 682 | efi_tcg2_get_eventlog(struct efi_tcg2_protocol *this, |
| 683 | efi_tcg_event_log_format log_format, |
| 684 | u64 *event_log_location, u64 *event_log_last_entry, |
| 685 | bool *event_log_truncated) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 686 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 687 | efi_status_t ret = EFI_SUCCESS; |
| 688 | struct udevice *dev; |
| 689 | |
| 690 | EFI_ENTRY("%p, %u, %p, %p, %p", this, log_format, event_log_location, |
| 691 | event_log_last_entry, event_log_truncated); |
| 692 | |
| 693 | ret = platform_get_tpm2_device(&dev); |
| 694 | if (ret != EFI_SUCCESS) { |
| 695 | event_log_location = NULL; |
| 696 | event_log_last_entry = NULL; |
| 697 | *event_log_truncated = false; |
| 698 | ret = EFI_SUCCESS; |
| 699 | goto out; |
| 700 | } |
| 701 | *event_log_location = (uintptr_t)event_log.buffer; |
| 702 | *event_log_last_entry = (uintptr_t)(event_log.buffer + event_log.pos - |
| 703 | event_log.last_event_size); |
| 704 | *event_log_truncated = event_log.truncated; |
| 705 | event_log.get_event_called = true; |
| 706 | |
| 707 | out: |
| 708 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 712 | * efi_tcg2_hash_log_extend_event() - extend and optionally log events |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 713 | * |
| 714 | * @this: TCG2 protocol instance |
| 715 | * @flags: bitmap providing additional information on the |
| 716 | * operation |
| 717 | * @data_to_hash: physical address of the start of the data buffer |
| 718 | * to be hashed |
| 719 | * @data_to_hash_len: the length in bytes of the buffer referenced by |
| 720 | * data_to_hash |
| 721 | * @efi_tcg_event: pointer to data buffer containing information |
| 722 | * about the event |
| 723 | * |
| 724 | * Return: status code |
| 725 | */ |
| 726 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 727 | efi_tcg2_hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags, |
| 728 | u64 data_to_hash, u64 data_to_hash_len, |
| 729 | struct efi_tcg2_event *efi_tcg_event) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 730 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 731 | struct udevice *dev; |
| 732 | efi_status_t ret; |
| 733 | u32 event_type, pcr_index, event_size; |
| 734 | struct tpml_digest_values digest_list; |
| 735 | |
| 736 | EFI_ENTRY("%p, %llu, %llu, %llu, %p", this, flags, data_to_hash, |
| 737 | data_to_hash_len, efi_tcg_event); |
| 738 | |
| 739 | if (!this || !data_to_hash || !efi_tcg_event) { |
| 740 | ret = EFI_INVALID_PARAMETER; |
| 741 | goto out; |
| 742 | } |
| 743 | |
| 744 | ret = platform_get_tpm2_device(&dev); |
| 745 | if (ret != EFI_SUCCESS) |
| 746 | goto out; |
| 747 | |
| 748 | if (efi_tcg_event->size < efi_tcg_event->header.header_size + |
| 749 | sizeof(u32)) { |
| 750 | ret = EFI_INVALID_PARAMETER; |
| 751 | goto out; |
| 752 | } |
| 753 | |
| 754 | if (efi_tcg_event->header.pcr_index < 0 || |
| 755 | efi_tcg_event->header.pcr_index > TPM2_MAX_PCRS) { |
| 756 | ret = EFI_INVALID_PARAMETER; |
| 757 | goto out; |
| 758 | } |
| 759 | |
| 760 | /* |
| 761 | * if PE_COFF_IMAGE is set we need to make sure the image is not |
| 762 | * corrupted, verify it and hash the PE/COFF image in accordance with |
| 763 | * the procedure specified in "Calculating the PE Image Hash" |
| 764 | * section of the "Windows Authenticode Portable Executable Signature |
| 765 | * Format" |
| 766 | * Not supported for now |
| 767 | */ |
| 768 | if (flags & PE_COFF_IMAGE) { |
| 769 | ret = EFI_UNSUPPORTED; |
| 770 | goto out; |
| 771 | } |
| 772 | |
| 773 | pcr_index = efi_tcg_event->header.pcr_index; |
| 774 | event_type = efi_tcg_event->header.event_type; |
| 775 | |
| 776 | ret = tcg2_create_digest((u8 *)data_to_hash, data_to_hash_len, |
| 777 | &digest_list); |
| 778 | if (ret != EFI_SUCCESS) |
| 779 | goto out; |
| 780 | |
| 781 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 782 | if (ret != EFI_SUCCESS) |
| 783 | goto out; |
| 784 | |
| 785 | if (flags & EFI_TCG2_EXTEND_ONLY) { |
| 786 | if (event_log.truncated) |
| 787 | ret = EFI_VOLUME_FULL; |
| 788 | goto out; |
| 789 | } |
| 790 | |
| 791 | /* |
| 792 | * The efi_tcg_event size includes the size component and the |
| 793 | * headersize |
| 794 | */ |
| 795 | event_size = efi_tcg_event->size - sizeof(efi_tcg_event->size) - |
| 796 | efi_tcg_event->header.header_size; |
| 797 | ret = tcg2_agile_log_append(pcr_index, event_type, &digest_list, |
| 798 | event_size, efi_tcg_event->event); |
| 799 | out: |
| 800 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 804 | * efi_tcg2_submit_command() - Send command to the TPM |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 805 | * |
| 806 | * @this: TCG2 protocol instance |
| 807 | * @input_param_block_size: size of the TPM input parameter block |
| 808 | * @input_param_block: pointer to the TPM input parameter block |
| 809 | * @output_param_block_size: size of the TPM output parameter block |
| 810 | * @output_param_block: pointer to the TPM output parameter block |
| 811 | * |
| 812 | * Return: status code |
| 813 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 814 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 815 | efi_tcg2_submit_command(struct efi_tcg2_protocol *this, |
| 816 | u32 input_param_block_size, u8 *input_param_block, |
| 817 | u32 output_param_block_size, u8 *output_param_block) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 818 | { |
| 819 | return EFI_UNSUPPORTED; |
| 820 | } |
| 821 | |
| 822 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 823 | * efi_tcg2_get_active_pcr_banks() - returns the currently active PCR banks |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 824 | * |
| 825 | * @this: TCG2 protocol instance |
| 826 | * @active_pcr_banks: pointer for receiving the bitmap of currently |
| 827 | * active PCR banks |
| 828 | * |
| 829 | * Return: status code |
| 830 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 831 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 832 | efi_tcg2_get_active_pcr_banks(struct efi_tcg2_protocol *this, |
| 833 | u32 *active_pcr_banks) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 834 | { |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 835 | efi_status_t ret; |
| 836 | |
| 837 | EFI_ENTRY("%p, %p", this, active_pcr_banks); |
| 838 | ret = __get_active_pcr_banks(active_pcr_banks); |
| 839 | |
| 840 | return EFI_EXIT(ret); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 844 | * efi_tcg2_set_active_pcr_banks() - sets the currently active PCR banks |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 845 | * |
| 846 | * @this: TCG2 protocol instance |
| 847 | * @active_pcr_banks: bitmap of the requested active PCR banks |
| 848 | * |
| 849 | * Return: status code |
| 850 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 851 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 852 | efi_tcg2_set_active_pcr_banks(struct efi_tcg2_protocol *this, |
| 853 | u32 active_pcr_banks) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 854 | { |
| 855 | return EFI_UNSUPPORTED; |
| 856 | } |
| 857 | |
| 858 | /** |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 859 | * efi_tcg2_get_result_of_set_active_pcr_banks() - retrieve result for previous |
| 860 | * set_active_pcr_banks() |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 861 | * |
| 862 | * @this: TCG2 protocol instance |
| 863 | * @operation_present: non-zero value to indicate a |
| 864 | * set_active_pcr_banks operation was |
| 865 | * invoked during last boot |
| 866 | * @response: result value could be returned |
| 867 | * |
| 868 | * Return: status code |
| 869 | */ |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 870 | static efi_status_t EFIAPI |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 871 | efi_tcg2_get_result_of_set_active_pcr_banks(struct efi_tcg2_protocol *this, |
| 872 | u32 *operation_present, u32 *response) |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 873 | { |
| 874 | return EFI_UNSUPPORTED; |
| 875 | } |
| 876 | |
| 877 | static const struct efi_tcg2_protocol efi_tcg2_protocol = { |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 878 | .get_capability = efi_tcg2_get_capability, |
| 879 | .get_eventlog = efi_tcg2_get_eventlog, |
| 880 | .hash_log_extend_event = efi_tcg2_hash_log_extend_event, |
| 881 | .submit_command = efi_tcg2_submit_command, |
| 882 | .get_active_pcr_banks = efi_tcg2_get_active_pcr_banks, |
| 883 | .set_active_pcr_banks = efi_tcg2_set_active_pcr_banks, |
| 884 | .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] | 885 | }; |
| 886 | |
| 887 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 888 | * create_specid_event() - Create the first event in the eventlog |
| 889 | * |
| 890 | * @dev: tpm device |
| 891 | * @event_header: Pointer to the final event header |
| 892 | * @event_size: final spec event size |
| 893 | * |
| 894 | * Return: status code |
| 895 | */ |
| 896 | static efi_status_t create_specid_event(struct udevice *dev, void *buffer, |
| 897 | size_t *event_size) |
| 898 | { |
| 899 | struct tcg_efi_spec_id_event *spec_event; |
| 900 | size_t spec_event_size; |
| 901 | efi_status_t ret = EFI_DEVICE_ERROR; |
| 902 | u32 active, supported; |
| 903 | int err, i; |
| 904 | |
| 905 | /* |
| 906 | * Create Spec event. This needs to be the first event in the log |
| 907 | * according to the TCG EFI protocol spec |
| 908 | */ |
| 909 | |
| 910 | /* Setup specID event data */ |
| 911 | spec_event = (struct tcg_efi_spec_id_event *)buffer; |
| 912 | memcpy(spec_event->signature, TCG_EFI_SPEC_ID_EVENT_SIGNATURE_03, |
| 913 | sizeof(spec_event->signature)); |
| 914 | put_unaligned_le32(0, &spec_event->platform_class); /* type client */ |
| 915 | spec_event->spec_version_minor = |
| 916 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MINOR_TPM2; |
| 917 | spec_event->spec_version_major = |
| 918 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_MAJOR_TPM2; |
| 919 | spec_event->spec_errata = |
| 920 | TCG_EFI_SPEC_ID_EVENT_SPEC_VERSION_ERRATA_TPM2; |
| 921 | spec_event->uintn_size = sizeof(efi_uintn_t) / sizeof(u32); |
| 922 | |
| 923 | err = tpm2_get_pcr_info(dev, &supported, &active, |
| 924 | &spec_event->number_of_algorithms); |
| 925 | if (err) |
| 926 | goto out; |
| 927 | if (spec_event->number_of_algorithms > MAX_HASH_COUNT || |
| 928 | spec_event->number_of_algorithms < 1) |
| 929 | goto out; |
| 930 | |
| 931 | for (i = 0; i < spec_event->number_of_algorithms; i++) { |
| 932 | u16 hash_alg = hash_algo_list[i].hash_alg; |
| 933 | u16 hash_len = hash_algo_list[i].hash_len; |
| 934 | |
| 935 | if (active && alg_to_mask(hash_alg)) { |
| 936 | put_unaligned_le16(hash_alg, |
| 937 | &spec_event->digest_sizes[i].algorithm_id); |
| 938 | put_unaligned_le16(hash_len, |
| 939 | &spec_event->digest_sizes[i].digest_size); |
| 940 | } |
| 941 | } |
| 942 | /* |
| 943 | * the size of the spec event and placement of vendor_info_size |
| 944 | * depends on supported algoriths |
| 945 | */ |
| 946 | spec_event_size = |
| 947 | offsetof(struct tcg_efi_spec_id_event, digest_sizes) + |
| 948 | spec_event->number_of_algorithms * sizeof(spec_event->digest_sizes[0]); |
| 949 | /* no vendor info for us */ |
| 950 | memset(buffer + spec_event_size, 0, |
| 951 | sizeof(spec_event->vendor_info_size)); |
| 952 | spec_event_size += sizeof(spec_event->vendor_info_size); |
| 953 | *event_size = spec_event_size; |
| 954 | |
| 955 | return EFI_SUCCESS; |
| 956 | |
| 957 | out: |
| 958 | return ret; |
| 959 | } |
| 960 | |
| 961 | /** |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 962 | * tcg2_uninit - remove the final event table and free efi memory on failures |
| 963 | */ |
| 964 | void tcg2_uninit(void) |
| 965 | { |
| 966 | efi_status_t ret; |
| 967 | |
| 968 | ret = efi_install_configuration_table(&efi_guid_final_events, NULL); |
| 969 | if (ret != EFI_SUCCESS) |
| 970 | log_err("Failed to delete final events config table\n"); |
| 971 | |
| 972 | efi_free_pool(event_log.buffer); |
| 973 | event_log.buffer = NULL; |
| 974 | efi_free_pool(event_log.final_buffer); |
| 975 | event_log.final_buffer = NULL; |
| 976 | } |
| 977 | |
| 978 | /** |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 979 | * create_final_event() - Create the final event and install the config |
| 980 | * defined by the TCG EFI spec |
| 981 | */ |
| 982 | static efi_status_t create_final_event(void) |
| 983 | { |
| 984 | struct efi_tcg2_final_events_table *final_event; |
| 985 | efi_status_t ret; |
| 986 | |
| 987 | /* |
| 988 | * All events generated after the invocation of |
| 989 | * EFI_TCG2_GET_EVENT_LOGS need to be stored in an instance of an |
| 990 | * EFI_CONFIGURATION_TABLE |
| 991 | */ |
| 992 | ret = efi_allocate_pool(EFI_ACPI_MEMORY_NVS, TPM2_EVENT_LOG_SIZE, |
| 993 | &event_log.final_buffer); |
| 994 | if (ret != EFI_SUCCESS) |
| 995 | goto out; |
| 996 | |
| 997 | memset(event_log.final_buffer, 0xff, TPM2_EVENT_LOG_SIZE); |
| 998 | final_event = event_log.final_buffer; |
| 999 | final_event->number_of_events = 0; |
| 1000 | final_event->version = EFI_TCG2_FINAL_EVENTS_TABLE_VERSION; |
| 1001 | event_log.final_pos = sizeof(*final_event); |
| 1002 | ret = efi_install_configuration_table(&efi_guid_final_events, |
| 1003 | final_event); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1004 | out: |
| 1005 | return ret; |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * efi_init_event_log() - initialize an eventlog |
| 1010 | */ |
| 1011 | static efi_status_t efi_init_event_log(void) |
| 1012 | { |
| 1013 | /* |
| 1014 | * vendor_info_size is currently set to 0, we need to change the length |
| 1015 | * and allocate the flexible array member if this changes |
| 1016 | */ |
| 1017 | struct tcg_pcr_event *event_header = NULL; |
| 1018 | struct udevice *dev; |
| 1019 | size_t spec_event_size; |
| 1020 | efi_status_t ret; |
| 1021 | |
| 1022 | ret = platform_get_tpm2_device(&dev); |
| 1023 | if (ret != EFI_SUCCESS) |
| 1024 | goto out; |
| 1025 | |
| 1026 | ret = efi_allocate_pool(EFI_BOOT_SERVICES_DATA, TPM2_EVENT_LOG_SIZE, |
| 1027 | (void **)&event_log.buffer); |
| 1028 | if (ret != EFI_SUCCESS) |
| 1029 | goto out; |
| 1030 | |
| 1031 | /* |
| 1032 | * initialize log area as 0xff so the OS can easily figure out the |
| 1033 | * last log entry |
| 1034 | */ |
| 1035 | memset(event_log.buffer, 0xff, TPM2_EVENT_LOG_SIZE); |
| 1036 | event_log.pos = 0; |
| 1037 | event_log.last_event_size = 0; |
| 1038 | event_log.get_event_called = false; |
| 1039 | event_log.truncated = false; |
| 1040 | |
| 1041 | /* |
| 1042 | * The log header is defined to be in SHA1 event log entry format. |
| 1043 | * Setup event header |
| 1044 | */ |
| 1045 | event_header = (struct tcg_pcr_event *)event_log.buffer; |
| 1046 | put_unaligned_le32(0, &event_header->pcr_index); |
| 1047 | put_unaligned_le32(EV_NO_ACTION, &event_header->event_type); |
| 1048 | memset(&event_header->digest, 0, sizeof(event_header->digest)); |
| 1049 | ret = create_specid_event(dev, event_log.buffer + sizeof(*event_header), |
| 1050 | &spec_event_size); |
| 1051 | if (ret != EFI_SUCCESS) |
| 1052 | goto out; |
| 1053 | put_unaligned_le32(spec_event_size, &event_header->event_size); |
| 1054 | event_log.pos = spec_event_size + sizeof(*event_header); |
| 1055 | event_log.last_event_size = event_log.pos; |
| 1056 | |
| 1057 | ret = create_final_event(); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1058 | if (ret != EFI_SUCCESS) |
| 1059 | goto out; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1060 | |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1061 | return EFI_SUCCESS; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1062 | out: |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1063 | tcg2_uninit(); |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1064 | return ret; |
| 1065 | } |
| 1066 | |
| 1067 | /** |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1068 | * efi_append_scrtm_version - Append an S-CRTM EV_S_CRTM_VERSION event on the |
| 1069 | * eventlog and extend the PCRs |
| 1070 | * |
| 1071 | * @dev: TPM device |
| 1072 | * |
| 1073 | * @Return: status code |
| 1074 | */ |
| 1075 | static efi_status_t efi_append_scrtm_version(struct udevice *dev) |
| 1076 | { |
| 1077 | struct tpml_digest_values digest_list; |
| 1078 | u8 ver[] = U_BOOT_VERSION_STRING; |
| 1079 | const int pcr_index = 0; |
| 1080 | efi_status_t ret; |
| 1081 | |
| 1082 | ret = tcg2_create_digest(ver, sizeof(ver), &digest_list); |
| 1083 | if (ret != EFI_SUCCESS) |
| 1084 | goto out; |
| 1085 | |
| 1086 | ret = tcg2_pcr_extend(dev, pcr_index, &digest_list); |
| 1087 | if (ret != EFI_SUCCESS) |
| 1088 | goto out; |
| 1089 | |
| 1090 | ret = tcg2_agile_log_append(pcr_index, EV_S_CRTM_VERSION, &digest_list, |
| 1091 | sizeof(ver), ver); |
| 1092 | |
| 1093 | out: |
| 1094 | return ret; |
| 1095 | } |
| 1096 | |
| 1097 | /** |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1098 | * efi_tcg2_register() - register EFI_TCG2_PROTOCOL |
| 1099 | * |
| 1100 | * If a TPM2 device is available, the TPM TCG2 Protocol is registered |
| 1101 | * |
| 1102 | * Return: An error status is only returned if adding the protocol fails. |
| 1103 | */ |
| 1104 | efi_status_t efi_tcg2_register(void) |
| 1105 | { |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1106 | efi_status_t ret = EFI_SUCCESS; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1107 | struct udevice *dev; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1108 | |
| 1109 | ret = platform_get_tpm2_device(&dev); |
Ilias Apalodimas | c67fef6 | 2020-11-16 08:52:41 +0200 | [diff] [blame] | 1110 | if (ret != EFI_SUCCESS) { |
| 1111 | log_warning("Unable to find TPMv2 device\n"); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1112 | ret = EFI_SUCCESS; |
| 1113 | goto out; |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1114 | } |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1115 | |
| 1116 | ret = efi_init_event_log(); |
| 1117 | if (ret != EFI_SUCCESS) |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1118 | goto fail; |
Ilias Apalodimas | 967650d | 2020-11-30 11:47:40 +0200 | [diff] [blame] | 1119 | |
Ilias Apalodimas | f576f7d | 2021-03-24 16:50:46 +0200 | [diff] [blame] | 1120 | ret = efi_append_scrtm_version(dev); |
| 1121 | if (ret != EFI_SUCCESS) |
| 1122 | goto out; |
| 1123 | |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1124 | ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol, |
| 1125 | (void *)&efi_tcg2_protocol); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1126 | if (ret != EFI_SUCCESS) { |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1127 | log_err("Cannot install EFI_TCG2_PROTOCOL\n"); |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1128 | goto fail; |
| 1129 | } |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1130 | |
Ilias Apalodimas | 1b278e6 | 2021-03-25 13:31:45 +0200 | [diff] [blame] | 1131 | out: |
| 1132 | return ret; |
| 1133 | fail: |
| 1134 | tcg2_uninit(); |
Ilias Apalodimas | 590fef6 | 2020-11-11 11:18:11 +0200 | [diff] [blame] | 1135 | return ret; |
| 1136 | } |