1 /* 2 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <assert.h> 8 9 #include <libfdt.h> 10 11 #include <common/fdt_wrappers.h> 12 #include <plat/arm/common/arm_dyn_cfg_helpers.h> 13 #include <plat/arm/common/plat_arm.h> 14 15 #define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr" 16 #define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size" 17 18 /******************************************************************************* 19 * Validate the tb_fw_config is a valid DTB file and returns the node offset 20 * to "arm,tb_fw" property. 21 * Arguments: 22 * void *dtb - pointer to the TB_FW_CONFIG in memory 23 * int *node - Returns the node offset to "arm,tb_fw" property if found. 24 * 25 * Returns 0 on success and -1 on error. 26 ******************************************************************************/ 27 int arm_dyn_tb_fw_cfg_init(void *dtb, int *node) 28 { 29 assert(dtb != NULL); 30 assert(node != NULL); 31 32 /* Check if the pointer to DT is correct */ 33 if (fdt_check_header(dtb) != 0) { 34 WARN("Invalid DTB file passed as TB_FW_CONFIG\n"); 35 return -1; 36 } 37 38 /* Assert the node offset point to "arm,tb_fw" compatible property */ 39 *node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw"); 40 if (*node < 0) { 41 WARN("The compatible property `arm,tb_fw` not found in the config\n"); 42 return -1; 43 } 44 45 VERBOSE("Dyn cfg: Found \"arm,tb_fw\" in the config\n"); 46 return 0; 47 } 48 49 /* 50 * This function writes the Mbed TLS heap address and size in the DTB. When it 51 * is called, it is guaranteed that a DTB is available. However it is not 52 * guaranteed that the shared Mbed TLS heap implementation is used. Thus we 53 * return error code from here and it's the responsibility of the caller to 54 * determine the action upon error. 55 * 56 * This function is supposed to be called only by BL1. 57 * 58 * Returns: 59 * 0 = success 60 * 1 = error 61 */ 62 int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size) 63 { 64 int err, dtb_root; 65 66 /* 67 * Verify that the DTB is valid, before attempting to write to it, 68 * and get the DTB root node. 69 */ 70 err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root); 71 if (err < 0) { 72 ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n"); 73 return -1; 74 } 75 76 /* 77 * Write the heap address and size in the DTB. 78 * 79 * NOTE: The variables heap_addr and heap_size are corrupted 80 * by the "fdtw_write_inplace_cells" function. After the 81 * function calls they must NOT be reused. 82 */ 83 err = fdtw_write_inplace_cells(dtb, dtb_root, 84 DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr); 85 if (err < 0) { 86 ERROR("Unable to write DTB property %s\n", 87 DTB_PROP_MBEDTLS_HEAP_ADDR); 88 return -1; 89 } 90 91 err = fdtw_write_inplace_cells(dtb, dtb_root, 92 DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size); 93 if (err < 0) { 94 ERROR("Unable to write DTB property %s\n", 95 DTB_PROP_MBEDTLS_HEAP_SIZE); 96 return -1; 97 } 98 99 return 0; 100 } 101