imx8m_dyn_cfg_helpers.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2022, ARM Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2022, Linaro.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <assert.h>
  8. #include <arch_helpers.h>
  9. #if MEASURED_BOOT
  10. #include <common/desc_image_load.h>
  11. #endif
  12. #include <common/fdt_wrappers.h>
  13. #include <libfdt.h>
  14. #include <platform_def.h>
  15. #define DTB_PROP_HW_LOG_ADDR "tpm_event_log_addr"
  16. #define DTB_PROP_HW_LOG_SIZE "tpm_event_log_size"
  17. #if MEASURED_BOOT
  18. static int imx8m_event_log_fdt_init_overlay(uintptr_t dt_base, int dt_size)
  19. {
  20. int ret;
  21. int offset;
  22. void *dtb = (void *)dt_base;
  23. ret = fdt_create_empty_tree(dtb, dt_size);
  24. if (ret < 0) {
  25. ERROR("cannot create empty dtb tree: %s\n",
  26. fdt_strerror(ret));
  27. return ret;
  28. }
  29. offset = fdt_path_offset(dtb, "/");
  30. if (offset < 0) {
  31. ERROR("cannot find root of the tree: %s\n",
  32. fdt_strerror(offset));
  33. return offset;
  34. }
  35. offset = fdt_add_subnode(dtb, offset, "fragment@0");
  36. if (offset < 0) {
  37. ERROR("cannot add fragment node: %s\n",
  38. fdt_strerror(offset));
  39. return offset;
  40. }
  41. ret = fdt_setprop_string(dtb, offset, "target-path", "/");
  42. if (ret < 0) {
  43. ERROR("cannot set target-path property: %s\n",
  44. fdt_strerror(ret));
  45. return ret;
  46. }
  47. offset = fdt_add_subnode(dtb, offset, "__overlay__");
  48. if (offset < 0) {
  49. ERROR("cannot add __overlay__ node: %s\n",
  50. fdt_strerror(offset));
  51. return ret;
  52. }
  53. offset = fdt_add_subnode(dtb, offset, "tpm_event_log");
  54. if (offset < 0) {
  55. ERROR("cannot add tpm_event_log node: %s\n",
  56. fdt_strerror(offset));
  57. return offset;
  58. }
  59. ret = fdt_setprop_string(dtb, offset, "compatible",
  60. "arm,tpm_event_log");
  61. if (ret < 0) {
  62. ERROR("cannot set compatible property: %s\n",
  63. fdt_strerror(ret));
  64. return ret;
  65. }
  66. ret = fdt_setprop_u64(dtb, offset, "tpm_event_log_addr", 0);
  67. if (ret < 0) {
  68. ERROR("cannot set tpm_event_log_addr property: %s\n",
  69. fdt_strerror(ret));
  70. return ret;
  71. }
  72. ret = fdt_setprop_u32(dtb, offset, "tpm_event_log_size", 0);
  73. if (ret < 0) {
  74. ERROR("cannot set tpm_event_log_size property: %s\n",
  75. fdt_strerror(ret));
  76. return ret;
  77. }
  78. return ret;
  79. }
  80. /*
  81. * Write the Event Log address and its size in the DTB.
  82. *
  83. * This function is supposed to be called only by BL2.
  84. *
  85. * Returns:
  86. * 0 = success
  87. * < 0 = error
  88. */
  89. static int imx8m_set_event_log_info(uintptr_t config_base,
  90. uintptr_t log_addr, size_t log_size)
  91. {
  92. /* As libfdt uses void *, we can't avoid this cast */
  93. void *dtb = (void *)config_base;
  94. const char *compatible_tpm = "arm,tpm_event_log";
  95. uint64_t base = cpu_to_fdt64(log_addr);
  96. uint32_t sz = cpu_to_fdt32(log_size);
  97. int err, node;
  98. err = fdt_open_into(dtb, dtb, PLAT_IMX8M_DTO_MAX_SIZE);
  99. if (err < 0) {
  100. ERROR("Invalid Device Tree at %p: error %d\n", dtb, err);
  101. return err;
  102. }
  103. /*
  104. * Verify that the DTB is valid, before attempting to write to it,
  105. * and get the DTB root node.
  106. */
  107. /* Check if the pointer to DT is correct */
  108. err = fdt_check_header(dtb);
  109. if (err < 0) {
  110. WARN("Invalid DTB file passed\n");
  111. return err;
  112. }
  113. /*
  114. * Find the TPM node in device tree.
  115. */
  116. node = fdt_node_offset_by_compatible(dtb, -1, compatible_tpm);
  117. if (node < 0) {
  118. ERROR("The compatible property '%s' not%s", compatible_tpm,
  119. " found in the config\n");
  120. return node;
  121. }
  122. err = fdt_setprop(dtb, node, DTB_PROP_HW_LOG_ADDR, &base, 8);
  123. if (err < 0) {
  124. ERROR("Failed to add log addr err %d\n", err);
  125. return err;
  126. }
  127. err = fdt_setprop(dtb, node, DTB_PROP_HW_LOG_SIZE, &sz, 4);
  128. if (err < 0) {
  129. ERROR("Failed to add log addr err %d\n", err);
  130. return err;
  131. }
  132. err = fdt_pack(dtb);
  133. if (err < 0) {
  134. ERROR("Failed to pack Device Tree at %p: error %d\n", dtb, err);
  135. return err;
  136. }
  137. /*
  138. * Ensure that the info written to the DTB is visible
  139. * to other images.
  140. */
  141. flush_dcache_range(config_base, fdt_totalsize(dtb));
  142. return err;
  143. }
  144. /*
  145. * This function writes the Event Log address and its size
  146. * in the QEMU DTB.
  147. *
  148. * This function is supposed to be called only by BL2.
  149. *
  150. * Returns:
  151. * 0 = success
  152. * < 0 = error
  153. */
  154. int imx8m_set_nt_fw_info(size_t log_size, uintptr_t *ns_log_addr)
  155. {
  156. uintptr_t ns_addr;
  157. int err;
  158. assert(ns_log_addr != NULL);
  159. ns_addr = PLAT_IMX8M_DTO_BASE + PLAT_IMX8M_DTO_MAX_SIZE;
  160. imx8m_event_log_fdt_init_overlay(PLAT_IMX8M_DTO_BASE,
  161. PLAT_IMX8M_DTO_MAX_SIZE);
  162. /* Write the Event Log address and its size in the DTB */
  163. err = imx8m_set_event_log_info(PLAT_IMX8M_DTO_BASE,
  164. ns_addr, log_size);
  165. /* Return Event Log address in Non-secure memory */
  166. *ns_log_addr = (err < 0) ? 0UL : ns_addr;
  167. return err;
  168. }
  169. #endif /* MEASURED_BOOT */