tpm.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. u32 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. unsigned int tbl_size;
  37. int final_tbl_size;
  38. int ret = 0;
  39. if (efi.tpm_log == EFI_INVALID_TABLE_ADDR) {
  40. /*
  41. * We can't calculate the size of the final events without the
  42. * first entry in the TPM log, so bail here.
  43. */
  44. return 0;
  45. }
  46. log_tbl = early_memremap(efi.tpm_log, sizeof(*log_tbl));
  47. if (!log_tbl) {
  48. pr_err("Failed to map TPM Event Log table @ 0x%lx\n",
  49. efi.tpm_log);
  50. efi.tpm_log = EFI_INVALID_TABLE_ADDR;
  51. return -ENOMEM;
  52. }
  53. tbl_size = sizeof(*log_tbl) + log_tbl->size;
  54. if (memblock_reserve(efi.tpm_log, tbl_size)) {
  55. pr_err("TPM Event Log memblock reserve fails (0x%lx, 0x%x)\n",
  56. efi.tpm_log, tbl_size);
  57. ret = -ENOMEM;
  58. goto out;
  59. }
  60. if (efi.tpm_final_log == EFI_INVALID_TABLE_ADDR) {
  61. pr_info("TPM Final Events table not present\n");
  62. goto out;
  63. } else if (log_tbl->version != EFI_TCG2_EVENT_LOG_FORMAT_TCG_2) {
  64. pr_warn(FW_BUG "TPM Final Events table invalid\n");
  65. goto out;
  66. }
  67. final_tbl = early_memremap(efi.tpm_final_log, sizeof(*final_tbl));
  68. if (!final_tbl) {
  69. pr_err("Failed to map TPM Final Event Log table @ 0x%lx\n",
  70. efi.tpm_final_log);
  71. efi.tpm_final_log = EFI_INVALID_TABLE_ADDR;
  72. ret = -ENOMEM;
  73. goto out;
  74. }
  75. final_tbl_size = 0;
  76. if (final_tbl->nr_events != 0) {
  77. void *events = (void *)efi.tpm_final_log
  78. + sizeof(final_tbl->version)
  79. + sizeof(final_tbl->nr_events);
  80. final_tbl_size = tpm2_calc_event_log_size(events,
  81. final_tbl->nr_events,
  82. log_tbl->log);
  83. }
  84. if (final_tbl_size < 0) {
  85. pr_err(FW_BUG "Failed to parse event in TPM Final Events Log\n");
  86. ret = -EINVAL;
  87. goto out_calc;
  88. }
  89. memblock_reserve(efi.tpm_final_log,
  90. final_tbl_size + sizeof(*final_tbl));
  91. efi_tpm_final_log_size = final_tbl_size;
  92. out_calc:
  93. early_memunmap(final_tbl, sizeof(*final_tbl));
  94. out:
  95. early_memunmap(log_tbl, sizeof(*log_tbl));
  96. return ret;
  97. }