fvp_bl2_measured_boot.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdint.h>
  7. #include <common/tbbr/tbbr_img_def.h>
  8. #include <drivers/measured_boot/event_log/event_log.h>
  9. #include <drivers/measured_boot/rss/rss_measured_boot.h>
  10. #include <tools_share/tbbr_oid.h>
  11. #include <fvp_critical_data.h>
  12. #include <plat/arm/common/plat_arm.h>
  13. #include <plat/common/common_def.h>
  14. /* Event Log data */
  15. static uint64_t event_log_base;
  16. /* FVP table with platform specific image IDs, names and PCRs */
  17. const event_log_metadata_t fvp_event_log_metadata[] = {
  18. { BL31_IMAGE_ID, EVLOG_BL31_STRING, PCR_0 },
  19. { BL32_IMAGE_ID, EVLOG_BL32_STRING, PCR_0 },
  20. { BL32_EXTRA1_IMAGE_ID, EVLOG_BL32_EXTRA1_STRING, PCR_0 },
  21. { BL32_EXTRA2_IMAGE_ID, EVLOG_BL32_EXTRA2_STRING, PCR_0 },
  22. { BL33_IMAGE_ID, EVLOG_BL33_STRING, PCR_0 },
  23. { HW_CONFIG_ID, EVLOG_HW_CONFIG_STRING, PCR_0 },
  24. { NT_FW_CONFIG_ID, EVLOG_NT_FW_CONFIG_STRING, PCR_0 },
  25. { SCP_BL2_IMAGE_ID, EVLOG_SCP_BL2_STRING, PCR_0 },
  26. { SOC_FW_CONFIG_ID, EVLOG_SOC_FW_CONFIG_STRING, PCR_0 },
  27. { TOS_FW_CONFIG_ID, EVLOG_TOS_FW_CONFIG_STRING, PCR_0 },
  28. { RMM_IMAGE_ID, EVLOG_RMM_STRING, PCR_0},
  29. #if defined(SPD_spmd)
  30. { SP_PKG1_ID, EVLOG_SP1_STRING, PCR_0 },
  31. { SP_PKG2_ID, EVLOG_SP2_STRING, PCR_0 },
  32. { SP_PKG3_ID, EVLOG_SP3_STRING, PCR_0 },
  33. { SP_PKG4_ID, EVLOG_SP4_STRING, PCR_0 },
  34. { SP_PKG5_ID, EVLOG_SP5_STRING, PCR_0 },
  35. { SP_PKG6_ID, EVLOG_SP6_STRING, PCR_0 },
  36. { SP_PKG7_ID, EVLOG_SP7_STRING, PCR_0 },
  37. { SP_PKG8_ID, EVLOG_SP8_STRING, PCR_0 },
  38. #endif
  39. { CRITICAL_DATA_ID, EVLOG_CRITICAL_DATA_STRING, PCR_1 },
  40. { EVLOG_INVALID_ID, NULL, (unsigned int)(-1) } /* Terminator */
  41. };
  42. /* FVP table with platform specific image IDs and metadata. Intentionally not a
  43. * const struct, some members might set by bootloaders during trusted boot.
  44. */
  45. struct rss_mboot_metadata fvp_rss_mboot_metadata[] = {
  46. {
  47. .id = BL31_IMAGE_ID,
  48. .slot = U(9),
  49. .signer_id_size = SIGNER_ID_MIN_SIZE,
  50. .sw_type = RSS_MBOOT_BL31_STRING,
  51. .lock_measurement = true },
  52. {
  53. .id = HW_CONFIG_ID,
  54. .slot = U(10),
  55. .signer_id_size = SIGNER_ID_MIN_SIZE,
  56. .sw_type = RSS_MBOOT_HW_CONFIG_STRING,
  57. .lock_measurement = true },
  58. {
  59. .id = SOC_FW_CONFIG_ID,
  60. .slot = U(11),
  61. .signer_id_size = SIGNER_ID_MIN_SIZE,
  62. .sw_type = RSS_MBOOT_SOC_FW_CONFIG_STRING,
  63. .lock_measurement = true },
  64. {
  65. .id = RMM_IMAGE_ID,
  66. .slot = U(12),
  67. .signer_id_size = SIGNER_ID_MIN_SIZE,
  68. .sw_type = RSS_MBOOT_RMM_STRING,
  69. .lock_measurement = true },
  70. {
  71. .id = RSS_MBOOT_INVALID_ID }
  72. };
  73. void bl2_plat_mboot_init(void)
  74. {
  75. uint8_t *event_log_start;
  76. uint8_t *event_log_finish;
  77. size_t bl1_event_log_size;
  78. int rc;
  79. rc = arm_get_tb_fw_info(&event_log_base, &bl1_event_log_size);
  80. if (rc != 0) {
  81. ERROR("%s(): Unable to get Event Log info from TB_FW_CONFIG\n",
  82. __func__);
  83. /*
  84. * It is a fatal error because on FVP platform, BL2 software
  85. * assumes that a valid Event Log buffer exist and it will use
  86. * same Event Log buffer to append image measurements.
  87. */
  88. panic();
  89. }
  90. /*
  91. * BL1 and BL2 share the same Event Log buffer and that BL2 will
  92. * append its measurements after BL1's
  93. */
  94. event_log_start = (uint8_t *)((uintptr_t)event_log_base +
  95. bl1_event_log_size);
  96. event_log_finish = (uint8_t *)((uintptr_t)event_log_base +
  97. PLAT_ARM_EVENT_LOG_MAX_SIZE);
  98. event_log_init((uint8_t *)event_log_start, event_log_finish);
  99. rss_measured_boot_init();
  100. }
  101. int plat_mboot_measure_critical_data(unsigned int critical_data_id,
  102. const void *base, size_t size)
  103. {
  104. /*
  105. * It is very unlikely that the critical data size would be
  106. * bigger than 2^32 bytes
  107. */
  108. assert(size < UINT32_MAX);
  109. assert(base != NULL);
  110. /* Calculate image hash and record data in Event Log */
  111. int err = event_log_measure_and_record((uintptr_t)base, (uint32_t)size,
  112. critical_data_id,
  113. fvp_event_log_metadata);
  114. if (err != 0) {
  115. ERROR("%s%s critical data (%i)\n",
  116. "Failed to ", "record", err);
  117. return err;
  118. }
  119. return 0;
  120. }
  121. #if TRUSTED_BOARD_BOOT
  122. static int fvp_populate_critical_data(struct fvp_critical_data *critical_data)
  123. {
  124. char *nv_ctr_oids[MAX_NV_CTR_IDS] = {
  125. [TRUSTED_NV_CTR_ID] = TRUSTED_FW_NVCOUNTER_OID,
  126. [NON_TRUSTED_NV_CTR_ID] = NON_TRUSTED_FW_NVCOUNTER_OID,
  127. };
  128. for (int i = 0; i < MAX_NV_CTR_IDS; i++) {
  129. int rc = plat_get_nv_ctr(nv_ctr_oids[i],
  130. &critical_data->nv_ctr[i]);
  131. if (rc != 0) {
  132. return rc;
  133. }
  134. }
  135. return 0;
  136. }
  137. #endif /* TRUSTED_BOARD_BOOT */
  138. static int fvp_populate_and_measure_critical_data(void)
  139. {
  140. int rc = 0;
  141. /*
  142. * FVP platform only measures 'platform NV-counter' and hence its
  143. * measurement makes sense during Trusted-Boot flow only.
  144. */
  145. #if TRUSTED_BOARD_BOOT
  146. struct fvp_critical_data populate_critical_data;
  147. rc = fvp_populate_critical_data(&populate_critical_data);
  148. if (rc == 0) {
  149. rc = plat_mboot_measure_critical_data(CRITICAL_DATA_ID,
  150. &populate_critical_data,
  151. sizeof(populate_critical_data));
  152. }
  153. #endif /* TRUSTED_BOARD_BOOT */
  154. return rc;
  155. }
  156. void bl2_plat_mboot_finish(void)
  157. {
  158. int rc;
  159. /* Event Log address in Non-Secure memory */
  160. uintptr_t ns_log_addr;
  161. /* Event Log filled size */
  162. size_t event_log_cur_size;
  163. rc = fvp_populate_and_measure_critical_data();
  164. if (rc != 0) {
  165. panic();
  166. }
  167. event_log_cur_size = event_log_get_cur_size((uint8_t *)event_log_base);
  168. rc = arm_set_nt_fw_info(
  169. #ifdef SPD_opteed
  170. (uintptr_t)event_log_base,
  171. #endif
  172. event_log_cur_size, &ns_log_addr);
  173. if (rc != 0) {
  174. ERROR("%s(): Unable to update %s_FW_CONFIG\n",
  175. __func__, "NT");
  176. /*
  177. * It is a fatal error because on FVP secure world software
  178. * assumes that a valid event log exists and will use it to
  179. * record the measurements into the fTPM.
  180. * Note: In FVP platform, OP-TEE uses nt_fw_config to get the
  181. * secure Event Log buffer address.
  182. */
  183. panic();
  184. }
  185. /* Copy Event Log to Non-secure memory */
  186. (void)memcpy((void *)ns_log_addr, (const void *)event_log_base,
  187. event_log_cur_size);
  188. /* Ensure that the Event Log is visible in Non-secure memory */
  189. flush_dcache_range(ns_log_addr, event_log_cur_size);
  190. #if defined(SPD_tspd) || defined(SPD_spmd)
  191. /* Set Event Log data in TOS_FW_CONFIG */
  192. rc = arm_set_tos_fw_info((uintptr_t)event_log_base,
  193. event_log_cur_size);
  194. if (rc != 0) {
  195. ERROR("%s(): Unable to update %s_FW_CONFIG\n",
  196. __func__, "TOS");
  197. panic();
  198. }
  199. #endif /* defined(SPD_tspd) || defined(SPD_spmd) */
  200. dump_event_log((uint8_t *)event_log_base, event_log_cur_size);
  201. }