arm_dyn_cfg.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2018-2024, Arm Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <string.h>
  8. #include <libfdt.h>
  9. #if CRYPTO_SUPPORT
  10. #include <mbedtls/version.h>
  11. #endif /* CRYPTO_SUPPORT */
  12. #include <common/debug.h>
  13. #include <common/desc_image_load.h>
  14. #include <common/tbbr/tbbr_img_def.h>
  15. #include <lib/fconf/fconf.h>
  16. #include <lib/fconf/fconf_dyn_cfg_getter.h>
  17. #include <lib/fconf/fconf_tbbr_getter.h>
  18. #include <plat/arm/common/arm_dyn_cfg_helpers.h>
  19. #include <plat/arm/common/plat_arm.h>
  20. #include <platform_def.h>
  21. #if CRYPTO_SUPPORT && !TRANSFER_LIST
  22. static void *mbedtls_heap_addr;
  23. static size_t mbedtls_heap_size;
  24. /*
  25. * This function is the implementation of the shared Mbed TLS heap between
  26. * BL1 and BL2 for Arm platforms. The shared heap address is passed from BL1
  27. * to BL2 with a pointer. This pointer resides inside the TB_FW_CONFIG file
  28. * which is a DTB.
  29. *
  30. * This function is placed inside an #if directive for the below reasons:
  31. * - To allocate space for the Mbed TLS heap --only if-- Trusted Board Boot
  32. * is enabled.
  33. * - This implementation requires the DTB to be present so that BL1 has a
  34. * mechanism to pass the pointer to BL2.
  35. */
  36. int arm_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
  37. {
  38. assert(heap_addr != NULL);
  39. assert(heap_size != NULL);
  40. #if defined(IMAGE_BL1) || RESET_TO_BL2 || defined(IMAGE_BL31)
  41. /* If in BL1 or RESET_TO_BL2 define a heap */
  42. static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
  43. *heap_addr = heap;
  44. *heap_size = sizeof(heap);
  45. mbedtls_heap_addr = heap;
  46. mbedtls_heap_size = sizeof(heap);
  47. #elif defined(IMAGE_BL2)
  48. /* If in BL2, retrieve the already allocated heap's info from DTB */
  49. *heap_addr = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_addr);
  50. *heap_size = FCONF_GET_PROPERTY(tbbr, dyn_config, mbedtls_heap_size);
  51. #endif
  52. return 0;
  53. }
  54. /*
  55. * Puts the shared Mbed TLS heap information to the DTB.
  56. * Executed only from BL1.
  57. */
  58. void arm_bl1_set_mbedtls_heap(void)
  59. {
  60. int err;
  61. uintptr_t tb_fw_cfg_dtb;
  62. const struct dyn_cfg_dtb_info_t *tb_fw_config_info;
  63. /*
  64. * If tb_fw_cfg_dtb==NULL then DTB is not present for the current
  65. * platform. As such, we don't attempt to write to the DTB at all.
  66. *
  67. * If mbedtls_heap_addr==NULL, then it means we are using the default
  68. * heap implementation. As such, BL2 will have its own heap for sure
  69. * and hence there is no need to pass any information to the DTB.
  70. *
  71. * In the latter case, if we still wanted to write in the DTB the heap
  72. * information, we would need to call plat_get_mbedtls_heap to retrieve
  73. * the default heap's address and size.
  74. */
  75. tb_fw_config_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, TB_FW_CONFIG_ID);
  76. assert(tb_fw_config_info != NULL);
  77. tb_fw_cfg_dtb = tb_fw_config_info->config_addr;
  78. if ((tb_fw_cfg_dtb != 0UL) && (mbedtls_heap_addr != NULL)) {
  79. /* As libfdt uses void *, we can't avoid this cast */
  80. void *dtb = (void *)tb_fw_cfg_dtb;
  81. err = arm_set_dtb_mbedtls_heap_info(dtb,
  82. mbedtls_heap_addr, mbedtls_heap_size);
  83. if (err < 0) {
  84. ERROR("%swrite shared Mbed TLS heap information%s",
  85. "BL1: unable to ", " to DTB\n");
  86. panic();
  87. }
  88. #if !MEASURED_BOOT
  89. /*
  90. * Ensure that the info written to the DTB is visible to other
  91. * images. It's critical because BL2 won't be able to proceed
  92. * without the heap info.
  93. *
  94. * In MEASURED_BOOT case flushing is done in a function which
  95. * is called after heap information is written in the DTB.
  96. */
  97. flush_dcache_range(tb_fw_cfg_dtb, fdt_totalsize(dtb));
  98. #endif /* !MEASURED_BOOT */
  99. }
  100. }
  101. #endif /* CRYPTO_SUPPORT && !TRANSFER_LIST */
  102. #if IMAGE_BL2
  103. /*
  104. * BL2 utility function to initialize dynamic configuration specified by
  105. * FW_CONFIG. Populate the bl_mem_params_node_t of other FW_CONFIGs if
  106. * specified in FW_CONFIG.
  107. */
  108. void arm_bl2_dyn_cfg_init(void)
  109. {
  110. unsigned int i;
  111. bl_mem_params_node_t *cfg_mem_params = NULL;
  112. uintptr_t image_base;
  113. uint32_t image_size;
  114. unsigned int error_config_id = MAX_IMAGE_IDS;
  115. const unsigned int config_ids[] = {
  116. HW_CONFIG_ID,
  117. SOC_FW_CONFIG_ID,
  118. NT_FW_CONFIG_ID,
  119. TOS_FW_CONFIG_ID
  120. };
  121. const struct dyn_cfg_dtb_info_t *dtb_info;
  122. /* Iterate through all the fw config IDs */
  123. for (i = 0; i < ARRAY_SIZE(config_ids); i++) {
  124. /* Get the config load address and size */
  125. cfg_mem_params = get_bl_mem_params_node(config_ids[i]);
  126. if (cfg_mem_params == NULL) {
  127. VERBOSE("%sconfig_id = %d in bl_mem_params_node\n",
  128. "Couldn't find ", config_ids[i]);
  129. continue;
  130. }
  131. dtb_info = FCONF_GET_PROPERTY(dyn_cfg, dtb, config_ids[i]);
  132. if (dtb_info == NULL) {
  133. VERBOSE("%sconfig_id %d load info in FW_CONFIG\n",
  134. "Couldn't find ", config_ids[i]);
  135. continue;
  136. }
  137. image_base = dtb_info->config_addr;
  138. image_size = dtb_info->config_max_size;
  139. /*
  140. * Do some runtime checks on the load addresses of soc_fw_config,
  141. * tos_fw_config, nt_fw_config. This is not a comprehensive check
  142. * of all invalid addresses but to prevent trivial porting errors.
  143. */
  144. if (config_ids[i] != HW_CONFIG_ID) {
  145. if (check_uptr_overflow(image_base, image_size)) {
  146. VERBOSE("%s=%d as its %s is overflowing uptr\n",
  147. "skip loading of firmware config",
  148. config_ids[i],
  149. "load-address");
  150. error_config_id = config_ids[i];
  151. continue;
  152. }
  153. #ifdef BL31_BASE
  154. /* Ensure the configs don't overlap with BL31 */
  155. if ((image_base >= BL31_BASE) &&
  156. (image_base <= BL31_LIMIT)) {
  157. VERBOSE("%s=%d as its %s is overlapping BL31\n",
  158. "skip loading of firmware config",
  159. config_ids[i],
  160. "load-address");
  161. error_config_id = config_ids[i];
  162. continue;
  163. }
  164. #endif
  165. /* Ensure the configs are loaded in a valid address */
  166. if (image_base < ARM_BL_RAM_BASE) {
  167. VERBOSE("%s=%d as its %s is invalid\n",
  168. "skip loading of firmware config",
  169. config_ids[i],
  170. "load-address");
  171. error_config_id = config_ids[i];
  172. continue;
  173. }
  174. #ifdef BL32_BASE
  175. /*
  176. * If BL32 is present, ensure that the configs don't
  177. * overlap with it.
  178. */
  179. if ((image_base >= BL32_BASE) &&
  180. (image_base <= BL32_LIMIT)) {
  181. VERBOSE("%s=%d as its %s is overlapping BL32\n",
  182. "skip loading of firmware config",
  183. config_ids[i],
  184. "load-address");
  185. error_config_id = config_ids[i];
  186. continue;
  187. }
  188. #endif
  189. }
  190. cfg_mem_params->image_info.image_base = image_base;
  191. cfg_mem_params->image_info.image_max_size = (uint32_t)image_size;
  192. /*
  193. * Remove the IMAGE_ATTRIB_SKIP_LOADING attribute from
  194. * HW_CONFIG or FW_CONFIG nodes
  195. */
  196. cfg_mem_params->image_info.h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
  197. }
  198. if (error_config_id != MAX_IMAGE_IDS) {
  199. ERROR("Invalid config file %u\n", error_config_id);
  200. panic();
  201. }
  202. }
  203. #endif /* IMAGE_BL2 */