tpm.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2017 Google, Inc.
  4. * Thiebaud Weksteen <tweek@google.com>
  5. */
  6. #define TPM_MEMREMAP(start, size) early_memremap(start, size)
  7. #define TPM_MEMUNMAP(start, size) early_memunmap(start, size)
  8. #include <asm/early_ioremap.h>
  9. #include <linux/efi.h>
  10. #include <linux/init.h>
  11. #include <linux/memblock.h>
  12. #include <linux/tpm_eventlog.h>
  13. int efi_tpm_final_log_size;
  14. EXPORT_SYMBOL(efi_tpm_final_log_size);
  15. static int __init tpm2_calc_event_log_size(void *data, int count, void *size_info)
  16. {
  17. struct tcg_pcr_event2_head *header;
  18. int event_size, size = 0;
  19. while (count > 0) {
  20. header = data + size;
  21. event_size = __calc_tpm2_event_size(header, size_info, true);
  22. if (event_size == 0)
  23. return -1;
  24. size += event_size;
  25. count--;
  26. }
  27. return size;
  28. }
  29. /*
  30. * Reserve the memory associated with the TPM Event Log configuration table.
  31. */
  32. int __init efi_tpm_eventlog_init(void)
  33. {
  34. struct linux_efi_tpm_eventlog *log_tbl;
  35. struct efi_tcg2_final_events_table *final_tbl;
  36. int tbl_size;
  37. int ret = 0;
  38. if (efi.tpm_log == EFI_INVALID_TABLE_ADDR) {
  39. /*
  40. * We can't calculate the size of the final events without the
  41. * first entry in the TPM log, so bail here.
  42. */
  43. return 0;
  44. }
  45. log_tbl = early_memremap(efi.tpm_log, sizeof(*log_tbl));
  46. if (!log_tbl) {
  47. pr_err("Failed to map TPM Event Log table @ 0x%lx\n",
  48. efi.tpm_log);
  49. efi.tpm_log = EFI_INVALID_TABLE_ADDR;
  50. return -ENOMEM;
  51. }
  52. tbl_size = sizeof(*log_tbl) + log_tbl->size;
  53. memblock_reserve(efi.tpm_log, tbl_size);
  54. if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
  55. pr_info("TPM Final Events table not present\n");
  56. goto out;
  57. } else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
  58. pr_warn(FW_BUG "TPM Final Events table invalid\n");
  59. goto out;
  60. }
  61. final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl));
  62. if (!final_tbl) {
  63. pr_err("Failed to map TPM Final Event Log table @ 0x%lx\n",
  64. efi.tpm_final_log);
  65. efi.tpm_final_log = EFI_INVALID_TABLE_ADDR;
  66. ret = -ENOMEM;
  67. goto out;
  68. }
  69. tbl_size = 0;
  70. if (final_tbl->nr_events != 0) {
  71. void *events = (void *)efi.tpm_final_log
  72. + sizeof(final_tbl->version)
  73. + sizeof(final_tbl->nr_events);
  74. tbl_size = tpm2_calc_event_log_size(events,
  75. final_tbl->nr_events,
  76. log_tbl->log);
  77. }
  78. if (tbl_size < 0) {
  79. pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n");
  80. ret = -EINVAL;
  81. goto out_calc;
  82. }
  83. memblock_reserve((unsigned long)final_tbl,
  84. tbl_size + sizeof(*final_tbl));
  85. efi_tpm_final_log_size = tbl_size;
  86. out_calc:
  87. early_memunmap(final_tbl, sizeof(*final_tbl));
  88. out:
  89. early_memunmap(log_tbl, sizeof(*log_tbl));
  90. return ret;
  91. }