Ilias Apalodimas | c1c0210 | 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> |
| 16 | #include <tpm-v2.h> |
| 17 | #include <linux/unaligned/access_ok.h> |
| 18 | #include <linux/unaligned/generic.h> |
| 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
| 22 | /* |
| 23 | * When requesting TPM2_CAP_TPM_PROPERTIES the value is on a standard offset. |
| 24 | * Since the current tpm2_get_capability() response buffers starts at |
| 25 | * 'union tpmu_capabilities data' of 'struct tpms_capability_data', calculate |
| 26 | * the response size and offset once for all consumers |
| 27 | */ |
| 28 | #define TPM2_RESPONSE_BUFFER_SIZE (sizeof(struct tpms_capability_data) - \ |
| 29 | offsetof(struct tpms_capability_data, data)) |
| 30 | #define properties_offset (offsetof(struct tpml_tagged_tpm_property, tpm_property) + \ |
| 31 | offsetof(struct tpms_tagged_property, value)) |
| 32 | |
| 33 | const efi_guid_t efi_guid_tcg2_protocol = EFI_TCG2_PROTOCOL_GUID; |
| 34 | |
| 35 | /** |
| 36 | * platform_get_tpm_device() - retrieve TPM device |
| 37 | * |
| 38 | * This function retrieves the udevice implementing a TPM |
| 39 | * |
| 40 | * This function may be overridden if special initialization is needed. |
| 41 | * |
| 42 | * @dev: udevice |
| 43 | * Return: status code |
| 44 | */ |
| 45 | __weak efi_status_t platform_get_tpm2_device(struct udevice **dev) |
| 46 | { |
| 47 | for_each_tpm_device((*dev)) { |
| 48 | if (tpm_get_version(*dev) == TPM_V2) |
| 49 | return EFI_SUCCESS; |
| 50 | } |
| 51 | return EFI_NOT_FOUND; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * tpm2_get_max_command_size() - get the supported max command size |
| 56 | * |
| 57 | * @dev: TPM device |
| 58 | * @max_command_size: output buffer for the size |
| 59 | * |
| 60 | * Return: 0 on success, -1 on error |
| 61 | */ |
| 62 | static int tpm2_get_max_command_size(struct udevice *dev, u16 *max_command_size) |
| 63 | { |
| 64 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 65 | u32 ret; |
| 66 | |
| 67 | memset(response, 0, sizeof(response)); |
| 68 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 69 | TPM2_PT_MAX_COMMAND_SIZE, response, 1); |
| 70 | if (ret) |
| 71 | return -1; |
| 72 | |
| 73 | *max_command_size = (uint16_t)get_unaligned_be32(response + |
| 74 | properties_offset); |
| 75 | |
| 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * tpm2_get_max_response_size() - get the supported max response size |
| 81 | * |
| 82 | * @dev: TPM device |
| 83 | * @max_response_size: output buffer for the size |
| 84 | * |
| 85 | * Return: 0 on success, -1 on error |
| 86 | */ |
| 87 | static int tpm2_get_max_response_size(struct udevice *dev, |
| 88 | u16 *max_response_size) |
| 89 | { |
| 90 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 91 | u32 ret; |
| 92 | |
| 93 | memset(response, 0, sizeof(response)); |
| 94 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 95 | TPM2_PT_MAX_RESPONSE_SIZE, response, 1); |
| 96 | if (ret) |
| 97 | return -1; |
| 98 | |
| 99 | *max_response_size = (uint16_t)get_unaligned_be32(response + |
| 100 | properties_offset); |
| 101 | |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * tpm2_get_manufacturer_id() - get the manufacturer ID |
| 107 | * |
| 108 | * @dev: TPM device |
| 109 | * @manufacturer_id: output buffer for the id |
| 110 | * |
| 111 | * Return: 0 on success, -1 on error |
| 112 | */ |
| 113 | static int tpm2_get_manufacturer_id(struct udevice *dev, u32 *manufacturer_id) |
| 114 | { |
| 115 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 116 | u32 ret; |
| 117 | |
| 118 | memset(response, 0, sizeof(response)); |
| 119 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 120 | TPM2_PT_MANUFACTURER, response, 1); |
| 121 | if (ret) |
| 122 | return -1; |
| 123 | |
| 124 | *manufacturer_id = get_unaligned_be32(response + properties_offset); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * tpm2_get_num_pcr() - get the number of PCRs |
| 131 | * |
| 132 | * @dev: TPM device |
| 133 | * @manufacturer_id: output buffer for the number |
| 134 | * |
| 135 | * Return: 0 on success, -1 on error |
| 136 | */ |
| 137 | static int tpm2_get_num_pcr(struct udevice *dev, u32 *num_pcr) |
| 138 | { |
| 139 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 140 | u32 ret; |
| 141 | |
| 142 | memset(response, 0, sizeof(response)); |
| 143 | ret = tpm2_get_capability(dev, TPM2_CAP_TPM_PROPERTIES, |
| 144 | TPM2_PT_PCR_COUNT, response, 1); |
| 145 | if (ret) |
| 146 | return -1; |
| 147 | |
| 148 | *num_pcr = get_unaligned_be32(response + properties_offset); |
| 149 | if (*num_pcr > TPM2_MAX_PCRS) |
| 150 | return -1; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * is_active_pcr() - Check if a supported algorithm is active |
| 157 | * |
| 158 | * @dev: TPM device |
| 159 | * @selection: struct of PCR information |
| 160 | * |
| 161 | * Return: true if PCR is active |
| 162 | */ |
| 163 | bool is_active_pcr(struct tpms_pcr_selection *selection) |
| 164 | { |
| 165 | int i; |
| 166 | /* |
| 167 | * check the pcr_select. If at least one of the PCRs supports the |
| 168 | * algorithm add it on the active ones |
| 169 | */ |
| 170 | for (i = 0; i < selection->size_of_select; i++) { |
| 171 | if (selection->pcr_select[i]) |
| 172 | return true; |
| 173 | } |
| 174 | |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * tpm2_get_pcr_info() - get the supported, active PCRs and number of banks |
| 180 | * |
| 181 | * @dev: TPM device |
| 182 | * @supported_pcr: bitmask with the algorithms supported |
| 183 | * @active_pcr: bitmask with the active algorithms |
| 184 | * @pcr_banks: number of PCR banks |
| 185 | * |
| 186 | * Return: 0 on success, -1 on error |
| 187 | */ |
| 188 | static int tpm2_get_pcr_info(struct udevice *dev, u32 *supported_pcr, |
| 189 | u32 *active_pcr, u32 *pcr_banks) |
| 190 | { |
| 191 | u8 response[TPM2_RESPONSE_BUFFER_SIZE]; |
| 192 | struct tpml_pcr_selection pcrs; |
| 193 | u32 ret, num_pcr; |
| 194 | int i, tpm_ret; |
| 195 | |
| 196 | memset(response, 0, sizeof(response)); |
| 197 | ret = tpm2_get_capability(dev, TPM2_CAP_PCRS, 0, response, 1); |
| 198 | if (ret) |
| 199 | goto out; |
| 200 | |
| 201 | pcrs.count = get_unaligned_be32(response); |
| 202 | /* |
| 203 | * We only support 5 algorithms for now so check against that |
| 204 | * instead of TPM2_NUM_PCR_BANKS |
| 205 | */ |
| 206 | if (pcrs.count > MAX_HASH_COUNT || pcrs.count < 1) |
| 207 | goto out; |
| 208 | |
| 209 | tpm_ret = tpm2_get_num_pcr(dev, &num_pcr); |
| 210 | if (tpm_ret) |
| 211 | goto out; |
| 212 | |
| 213 | for (i = 0; i < pcrs.count; i++) { |
| 214 | /* |
| 215 | * Definition of TPMS_PCR_SELECTION Structure |
| 216 | * hash: u16 |
| 217 | * size_of_select: u8 |
| 218 | * pcr_select: u8 array |
| 219 | * |
| 220 | * The offsets depend on the number of the device PCRs |
| 221 | * so we have to calculate them based on that |
| 222 | */ |
| 223 | u32 hash_offset = offsetof(struct tpml_pcr_selection, selection) + |
| 224 | i * offsetof(struct tpms_pcr_selection, pcr_select) + |
| 225 | i * ((num_pcr + 7) / 8); |
| 226 | u32 size_select_offset = |
| 227 | hash_offset + offsetof(struct tpms_pcr_selection, |
| 228 | size_of_select); |
| 229 | u32 pcr_select_offset = |
| 230 | hash_offset + offsetof(struct tpms_pcr_selection, |
| 231 | pcr_select); |
| 232 | |
| 233 | pcrs.selection[i].hash = |
| 234 | get_unaligned_be16(response + hash_offset); |
| 235 | pcrs.selection[i].size_of_select = |
| 236 | __get_unaligned_be(response + size_select_offset); |
| 237 | if (pcrs.selection[i].size_of_select > TPM2_PCR_SELECT_MAX) |
| 238 | goto out; |
| 239 | /* copy the array of pcr_select */ |
| 240 | memcpy(pcrs.selection[i].pcr_select, response + pcr_select_offset, |
| 241 | pcrs.selection[i].size_of_select); |
| 242 | } |
| 243 | |
| 244 | for (i = 0; i < pcrs.count; i++) { |
| 245 | switch (pcrs.selection[i].hash) { |
| 246 | case TPM2_ALG_SHA1: |
| 247 | *supported_pcr |= EFI_TCG2_BOOT_HASH_ALG_SHA1; |
| 248 | if (is_active_pcr(&pcrs.selection[i])) |
| 249 | *active_pcr |= EFI_TCG2_BOOT_HASH_ALG_SHA1; |
| 250 | break; |
| 251 | case TPM2_ALG_SHA256: |
| 252 | *supported_pcr |= EFI_TCG2_BOOT_HASH_ALG_SHA256; |
| 253 | if (is_active_pcr(&pcrs.selection[i])) |
| 254 | *active_pcr |= EFI_TCG2_BOOT_HASH_ALG_SHA256; |
| 255 | break; |
| 256 | case TPM2_ALG_SHA384: |
| 257 | *supported_pcr |= EFI_TCG2_BOOT_HASH_ALG_SHA384; |
| 258 | if (is_active_pcr(&pcrs.selection[i])) |
| 259 | *active_pcr |= EFI_TCG2_BOOT_HASH_ALG_SHA384; |
| 260 | break; |
| 261 | case TPM2_ALG_SHA512: |
| 262 | *supported_pcr |= EFI_TCG2_BOOT_HASH_ALG_SHA512; |
| 263 | if (is_active_pcr(&pcrs.selection[i])) |
| 264 | *active_pcr |= EFI_TCG2_BOOT_HASH_ALG_SHA512; |
| 265 | break; |
| 266 | case TPM2_ALG_SM3_256: |
| 267 | *supported_pcr |= EFI_TCG2_BOOT_HASH_ALG_SM3_256; |
| 268 | if (is_active_pcr(&pcrs.selection[i])) |
| 269 | *active_pcr |= EFI_TCG2_BOOT_HASH_ALG_SM3_256; |
| 270 | break; |
| 271 | default: |
| 272 | EFI_PRINT("Unknown algorithm %x\n", |
| 273 | pcrs.selection[i].hash); |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | *pcr_banks = pcrs.count; |
| 279 | |
| 280 | return 0; |
| 281 | out: |
| 282 | return -1; |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * get_capability() - protocol capability information and state information |
| 287 | * |
| 288 | * @this: TCG2 protocol instance |
| 289 | * @capability: caller allocated memory with size field to the size of |
| 290 | * the structure allocated |
| 291 | |
| 292 | * Return: status code |
| 293 | */ |
| 294 | static efi_status_t EFIAPI |
| 295 | get_capability(struct efi_tcg2_protocol *this, |
| 296 | struct efi_tcg2_boot_service_capability *capability) |
| 297 | { |
| 298 | struct udevice *dev; |
| 299 | efi_status_t efi_ret; |
| 300 | int ret; |
| 301 | |
| 302 | EFI_ENTRY("%p, %p", this, capability); |
| 303 | |
| 304 | if (!this || !capability) { |
| 305 | efi_ret = EFI_INVALID_PARAMETER; |
| 306 | goto out; |
| 307 | } |
| 308 | |
| 309 | if (capability->size < boot_service_capability_min) { |
| 310 | capability->size = boot_service_capability_min; |
| 311 | efi_ret = EFI_BUFFER_TOO_SMALL; |
| 312 | goto out; |
| 313 | } |
| 314 | |
| 315 | if (capability->size < sizeof(*capability)) { |
| 316 | capability->size = sizeof(*capability); |
| 317 | efi_ret = EFI_BUFFER_TOO_SMALL; |
| 318 | goto out; |
| 319 | } |
| 320 | |
| 321 | capability->structure_version.major = 1; |
| 322 | capability->structure_version.minor = 1; |
| 323 | capability->protocol_version.major = 1; |
| 324 | capability->protocol_version.minor = 1; |
| 325 | |
| 326 | efi_ret = platform_get_tpm2_device(&dev); |
| 327 | if (efi_ret != EFI_SUCCESS) { |
| 328 | capability->supported_event_logs = 0; |
| 329 | capability->hash_algorithm_bitmap = 0; |
| 330 | capability->tpm_present_flag = false; |
| 331 | capability->max_command_size = 0; |
| 332 | capability->max_response_size = 0; |
| 333 | capability->manufacturer_id = 0; |
| 334 | capability->number_of_pcr_banks = 0; |
| 335 | capability->active_pcr_banks = 0; |
| 336 | |
| 337 | efi_ret = EFI_SUCCESS; |
| 338 | goto out; |
| 339 | } |
| 340 | |
| 341 | /* We only allow a TPMv2 device to register the EFI protocol */ |
| 342 | capability->supported_event_logs = TCG2_EVENT_LOG_FORMAT_TCG_2; |
| 343 | |
| 344 | capability->tpm_present_flag = true; |
| 345 | |
| 346 | /* Supported and active PCRs */ |
| 347 | capability->hash_algorithm_bitmap = 0; |
| 348 | capability->active_pcr_banks = 0; |
| 349 | ret = tpm2_get_pcr_info(dev, &capability->hash_algorithm_bitmap, |
| 350 | &capability->active_pcr_banks, |
| 351 | &capability->number_of_pcr_banks); |
| 352 | if (ret) { |
| 353 | efi_ret = EFI_DEVICE_ERROR; |
| 354 | goto out; |
| 355 | } |
| 356 | |
| 357 | /* Max command size */ |
| 358 | ret = tpm2_get_max_command_size(dev, &capability->max_command_size); |
| 359 | if (ret) { |
| 360 | efi_ret = EFI_DEVICE_ERROR; |
| 361 | goto out; |
| 362 | } |
| 363 | |
| 364 | /* Max response size */ |
| 365 | ret = tpm2_get_max_response_size(dev, &capability->max_response_size); |
| 366 | if (ret) { |
| 367 | efi_ret = EFI_DEVICE_ERROR; |
| 368 | goto out; |
| 369 | } |
| 370 | |
| 371 | /* Manufacturer ID */ |
| 372 | ret = tpm2_get_manufacturer_id(dev, &capability->manufacturer_id); |
| 373 | if (ret) { |
| 374 | efi_ret = EFI_DEVICE_ERROR; |
| 375 | goto out; |
| 376 | } |
| 377 | |
| 378 | return EFI_EXIT(EFI_SUCCESS); |
| 379 | out: |
| 380 | return EFI_EXIT(efi_ret); |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * get_eventlog() - retrieve the the address of an event log and its last entry |
| 385 | * |
| 386 | * @this: TCG2 protocol instance |
| 387 | * @log_format: type of event log format |
| 388 | * @event_log_location: pointer to the memory address of the event log |
| 389 | * @event_log_last_entry: pointer to the address of the start of the last |
| 390 | * entry in the event log in memory, if log contains |
| 391 | * more than 1 entry |
| 392 | * @event_log_truncated: set to true, if the Event Log is missing at i |
| 393 | * least one entry |
| 394 | * |
| 395 | * Return: status code |
| 396 | */ |
| 397 | static efi_status_t EFIAPI |
| 398 | get_eventlog(struct efi_tcg2_protocol *this, |
| 399 | efi_tcg_event_log_format log_format, u64 *event_log_location, |
| 400 | u64 *event_log_last_entry, bool *event_log_truncated) |
| 401 | { |
| 402 | return EFI_UNSUPPORTED; |
| 403 | } |
| 404 | |
| 405 | /** |
| 406 | * hash_log_extend_event()- extend and optionally log events |
| 407 | * |
| 408 | * @this: TCG2 protocol instance |
| 409 | * @flags: bitmap providing additional information on the |
| 410 | * operation |
| 411 | * @data_to_hash: physical address of the start of the data buffer |
| 412 | * to be hashed |
| 413 | * @data_to_hash_len: the length in bytes of the buffer referenced by |
| 414 | * data_to_hash |
| 415 | * @efi_tcg_event: pointer to data buffer containing information |
| 416 | * about the event |
| 417 | * |
| 418 | * Return: status code |
| 419 | */ |
| 420 | static efi_status_t EFIAPI |
| 421 | hash_log_extend_event(struct efi_tcg2_protocol *this, u64 flags, |
| 422 | u64 data_to_hash, u64 data_to_hash_len, |
| 423 | struct efi_tcg2_event *efi_tcg_event) |
| 424 | { |
| 425 | return EFI_UNSUPPORTED; |
| 426 | } |
| 427 | |
| 428 | /** |
| 429 | * submit_command() - Send command to the TPM |
| 430 | * |
| 431 | * @this: TCG2 protocol instance |
| 432 | * @input_param_block_size: size of the TPM input parameter block |
| 433 | * @input_param_block: pointer to the TPM input parameter block |
| 434 | * @output_param_block_size: size of the TPM output parameter block |
| 435 | * @output_param_block: pointer to the TPM output parameter block |
| 436 | * |
| 437 | * Return: status code |
| 438 | */ |
| 439 | efi_status_t EFIAPI |
| 440 | submit_command(struct efi_tcg2_protocol *this, u32 input_param_block_size, |
| 441 | u8 *input_param_block, u32 output_param_block_size, |
| 442 | u8 *output_param_block) |
| 443 | { |
| 444 | return EFI_UNSUPPORTED; |
| 445 | } |
| 446 | |
| 447 | /** |
| 448 | * get_active_pcr_banks() - returns the currently active PCR banks |
| 449 | * |
| 450 | * @this: TCG2 protocol instance |
| 451 | * @active_pcr_banks: pointer for receiving the bitmap of currently |
| 452 | * active PCR banks |
| 453 | * |
| 454 | * Return: status code |
| 455 | */ |
| 456 | efi_status_t EFIAPI |
| 457 | get_active_pcr_banks(struct efi_tcg2_protocol *this, u32 *active_pcr_banks) |
| 458 | { |
| 459 | return EFI_UNSUPPORTED; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * set_active_pcr_banks() - sets the currently active PCR banks |
| 464 | * |
| 465 | * @this: TCG2 protocol instance |
| 466 | * @active_pcr_banks: bitmap of the requested active PCR banks |
| 467 | * |
| 468 | * Return: status code |
| 469 | */ |
| 470 | efi_status_t EFIAPI |
| 471 | set_active_pcr_banks(struct efi_tcg2_protocol *this, u32 active_pcr_banks) |
| 472 | { |
| 473 | return EFI_UNSUPPORTED; |
| 474 | } |
| 475 | |
| 476 | /** |
| 477 | * get_result_of_set_active_pcr_banks() - retrieves the result of a previous |
| 478 | * set_active_pcr_banks() |
| 479 | * |
| 480 | * @this: TCG2 protocol instance |
| 481 | * @operation_present: non-zero value to indicate a |
| 482 | * set_active_pcr_banks operation was |
| 483 | * invoked during last boot |
| 484 | * @response: result value could be returned |
| 485 | * |
| 486 | * Return: status code |
| 487 | */ |
| 488 | efi_status_t EFIAPI |
| 489 | get_result_of_set_active_pcr_banks(struct efi_tcg2_protocol *this, |
| 490 | u32 *operation_present, u32 *response) |
| 491 | { |
| 492 | return EFI_UNSUPPORTED; |
| 493 | } |
| 494 | |
| 495 | static const struct efi_tcg2_protocol efi_tcg2_protocol = { |
| 496 | .get_capability = get_capability, |
| 497 | .get_eventlog = get_eventlog, |
| 498 | .hash_log_extend_event = hash_log_extend_event, |
| 499 | .submit_command = submit_command, |
| 500 | .get_active_pcr_banks = get_active_pcr_banks, |
| 501 | .set_active_pcr_banks = set_active_pcr_banks, |
| 502 | .get_result_of_set_active_pcr_banks = get_result_of_set_active_pcr_banks, |
| 503 | }; |
| 504 | |
| 505 | /** |
| 506 | * efi_tcg2_register() - register EFI_TCG2_PROTOCOL |
| 507 | * |
| 508 | * If a TPM2 device is available, the TPM TCG2 Protocol is registered |
| 509 | * |
| 510 | * Return: An error status is only returned if adding the protocol fails. |
| 511 | */ |
| 512 | efi_status_t efi_tcg2_register(void) |
| 513 | { |
| 514 | efi_status_t ret; |
| 515 | struct udevice *dev; |
| 516 | enum tpm_version tpm_ver; |
| 517 | |
| 518 | ret = platform_get_tpm2_device(&dev); |
| 519 | if (ret != EFI_SUCCESS) |
| 520 | return EFI_SUCCESS; |
| 521 | |
| 522 | tpm_ver = tpm_get_version(dev); |
| 523 | if (tpm_ver != TPM_V2) { |
| 524 | log_warning("Only TPMv2 supported for EFI_TCG2_PROTOCOL\n"); |
| 525 | return EFI_SUCCESS; |
| 526 | } |
| 527 | |
| 528 | ret = efi_add_protocol(efi_root, &efi_guid_tcg2_protocol, |
| 529 | (void *)&efi_tcg2_protocol); |
| 530 | if (ret != EFI_SUCCESS) |
| 531 | log_err("Cannot install EFI_TCG2_PROTOCOL\n"); |
| 532 | |
| 533 | return ret; |
| 534 | } |