acpi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2005 IBM Corporation
  4. *
  5. * Authors:
  6. * Seiji Munetoh <[email protected]>
  7. * Stefan Berger <[email protected]>
  8. * Reiner Sailer <[email protected]>
  9. * Kylene Hall <[email protected]>
  10. * Nayna Jain <[email protected]>
  11. *
  12. * Maintained by: <[email protected]>
  13. *
  14. * Access to the event log extended by the TCG BIOS of PC platform
  15. */
  16. #include <linux/seq_file.h>
  17. #include <linux/fs.h>
  18. #include <linux/security.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/acpi.h>
  22. #include <linux/tpm_eventlog.h>
  23. #include "../tpm.h"
  24. #include "common.h"
  25. struct acpi_tcpa {
  26. struct acpi_table_header hdr;
  27. u16 platform_class;
  28. union {
  29. struct client_hdr {
  30. u32 log_max_len __packed;
  31. u64 log_start_addr __packed;
  32. } client;
  33. struct server_hdr {
  34. u16 reserved;
  35. u64 log_max_len __packed;
  36. u64 log_start_addr __packed;
  37. } server;
  38. };
  39. };
  40. /* Check that the given log is indeed a TPM2 log. */
  41. static bool tpm_is_tpm2_log(void *bios_event_log, u64 len)
  42. {
  43. struct tcg_efi_specid_event_head *efispecid;
  44. struct tcg_pcr_event *event_header;
  45. int n;
  46. if (len < sizeof(*event_header))
  47. return false;
  48. len -= sizeof(*event_header);
  49. event_header = bios_event_log;
  50. if (len < sizeof(*efispecid))
  51. return false;
  52. efispecid = (struct tcg_efi_specid_event_head *)event_header->event;
  53. n = memcmp(efispecid->signature, TCG_SPECID_SIG,
  54. sizeof(TCG_SPECID_SIG));
  55. return n == 0;
  56. }
  57. /* read binary bios log */
  58. int tpm_read_log_acpi(struct tpm_chip *chip)
  59. {
  60. struct acpi_tcpa *buff;
  61. acpi_status status;
  62. void __iomem *virt;
  63. u64 len, start;
  64. struct tpm_bios_log *log;
  65. struct acpi_table_tpm2 *tbl;
  66. struct acpi_tpm2_phy *tpm2_phy;
  67. int format;
  68. int ret;
  69. log = &chip->log;
  70. /* Unfortuntely ACPI does not associate the event log with a specific
  71. * TPM, like PPI. Thus all ACPI TPMs will read the same log.
  72. */
  73. if (!chip->acpi_dev_handle)
  74. return -ENODEV;
  75. if (chip->flags & TPM_CHIP_FLAG_TPM2) {
  76. status = acpi_get_table("TPM2", 1,
  77. (struct acpi_table_header **)&tbl);
  78. if (ACPI_FAILURE(status))
  79. return -ENODEV;
  80. if (tbl->header.length <
  81. sizeof(*tbl) + sizeof(struct acpi_tpm2_phy)) {
  82. acpi_put_table((struct acpi_table_header *)tbl);
  83. return -ENODEV;
  84. }
  85. tpm2_phy = (void *)tbl + sizeof(*tbl);
  86. len = tpm2_phy->log_area_minimum_length;
  87. start = tpm2_phy->log_area_start_address;
  88. if (!start || !len) {
  89. acpi_put_table((struct acpi_table_header *)tbl);
  90. return -ENODEV;
  91. }
  92. acpi_put_table((struct acpi_table_header *)tbl);
  93. format = EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
  94. } else {
  95. /* Find TCPA entry in RSDT (ACPI_LOGICAL_ADDRESSING) */
  96. status = acpi_get_table(ACPI_SIG_TCPA, 1,
  97. (struct acpi_table_header **)&buff);
  98. if (ACPI_FAILURE(status))
  99. return -ENODEV;
  100. switch (buff->platform_class) {
  101. case BIOS_SERVER:
  102. len = buff->server.log_max_len;
  103. start = buff->server.log_start_addr;
  104. break;
  105. case BIOS_CLIENT:
  106. default:
  107. len = buff->client.log_max_len;
  108. start = buff->client.log_start_addr;
  109. break;
  110. }
  111. acpi_put_table((struct acpi_table_header *)buff);
  112. format = EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
  113. }
  114. if (!len) {
  115. dev_warn(&chip->dev, "%s: TCPA log area empty\n", __func__);
  116. return -EIO;
  117. }
  118. /* malloc EventLog space */
  119. log->bios_event_log = kmalloc(len, GFP_KERNEL);
  120. if (!log->bios_event_log)
  121. return -ENOMEM;
  122. log->bios_event_log_end = log->bios_event_log + len;
  123. ret = -EIO;
  124. virt = acpi_os_map_iomem(start, len);
  125. if (!virt) {
  126. dev_warn(&chip->dev, "%s: Failed to map ACPI memory\n", __func__);
  127. /* try EFI log next */
  128. ret = -ENODEV;
  129. goto err;
  130. }
  131. memcpy_fromio(log->bios_event_log, virt, len);
  132. acpi_os_unmap_iomem(virt, len);
  133. if (chip->flags & TPM_CHIP_FLAG_TPM2 &&
  134. !tpm_is_tpm2_log(log->bios_event_log, len)) {
  135. /* try EFI log next */
  136. ret = -ENODEV;
  137. goto err;
  138. }
  139. return format;
  140. err:
  141. kfree(log->bios_event_log);
  142. log->bios_event_log = NULL;
  143. return ret;
  144. }