of.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2012 IBM Corporation
  4. *
  5. * Author: Ashley Lai <ashleydlai@gmail.com>
  6. * Nayna Jain <nayna@linux.vnet.ibm.com>
  7. *
  8. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  9. *
  10. * Read the event log created by the firmware on PPC64
  11. */
  12. #include <linux/device.h>
  13. #include <linux/slab.h>
  14. #include <linux/io.h>
  15. #include <linux/ioport.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_reserved_mem.h>
  19. #include <linux/tpm_eventlog.h>
  20. #include "../tpm.h"
  21. #include "common.h"
  22. static int tpm_read_log_memory_region(struct tpm_chip *chip)
  23. {
  24. struct resource res;
  25. int rc;
  26. rc = of_reserved_mem_region_to_resource(chip->dev.parent->of_node, 0, &res);
  27. if (rc)
  28. return rc;
  29. chip->log.bios_event_log = devm_memremap(&chip->dev, res.start, resource_size(&res),
  30. MEMREMAP_WB);
  31. if (IS_ERR(chip->log.bios_event_log))
  32. return -ENOMEM;
  33. chip->log.bios_event_log_end = chip->log.bios_event_log + resource_size(&res);
  34. return chip->flags & TPM_CHIP_FLAG_TPM2 ? EFI_TCG2_EVENT_LOG_FORMAT_TCG_2 :
  35. EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
  36. }
  37. int tpm_read_log_of(struct tpm_chip *chip)
  38. {
  39. struct device_node *np;
  40. const u32 *sizep;
  41. const u64 *basep;
  42. struct tpm_bios_log *log;
  43. u32 size;
  44. u64 base;
  45. log = &chip->log;
  46. if (chip->dev.parent && chip->dev.parent->of_node)
  47. np = chip->dev.parent->of_node;
  48. else
  49. return -ENODEV;
  50. if (of_property_read_bool(np, "powered-while-suspended"))
  51. chip->flags |= TPM_CHIP_FLAG_ALWAYS_POWERED;
  52. sizep = of_get_property(np, "linux,sml-size", NULL);
  53. basep = of_get_property(np, "linux,sml-base", NULL);
  54. if (sizep == NULL && basep == NULL)
  55. return tpm_read_log_memory_region(chip);
  56. if (sizep == NULL || basep == NULL)
  57. return -EIO;
  58. /*
  59. * For both vtpm/tpm, firmware has log addr and log size in big
  60. * endian format. But in case of vtpm, there is a method called
  61. * sml-handover which is run during kernel init even before
  62. * device tree is setup. This sml-handover function takes care
  63. * of endianness and writes to sml-base and sml-size in little
  64. * endian format. For this reason, vtpm doesn't need conversion
  65. * but physical tpm needs the conversion.
  66. */
  67. if (of_property_match_string(np, "compatible", "IBM,vtpm") < 0 &&
  68. of_property_match_string(np, "compatible", "IBM,vtpm20") < 0) {
  69. size = be32_to_cpup((__force __be32 *)sizep);
  70. base = be64_to_cpup((__force __be64 *)basep);
  71. } else {
  72. size = *sizep;
  73. base = *basep;
  74. }
  75. if (size == 0) {
  76. dev_warn(&chip->dev, "%s: Event log area empty\n", __func__);
  77. return -EIO;
  78. }
  79. log->bios_event_log = devm_kmemdup(&chip->dev, __va(base), size, GFP_KERNEL);
  80. if (!log->bios_event_log)
  81. return -ENOMEM;
  82. log->bios_event_log_end = log->bios_event_log + size;
  83. if (chip->flags & TPM_CHIP_FLAG_TPM2)
  84. return EFI_TCG2_EVENT_LOG_FORMAT_TCG_2;
  85. return EFI_TCG2_EVENT_LOG_FORMAT_TCG_1_2;
  86. }