acpi.c 4.1 KB

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