1cab0b5b0SSoby Mathew /* 2cab0b5b0SSoby Mathew * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. 3cab0b5b0SSoby Mathew * 4cab0b5b0SSoby Mathew * SPDX-License-Identifier: BSD-3-Clause 5cab0b5b0SSoby Mathew */ 6cab0b5b0SSoby Mathew 735a3eeb6SRoberto Vargas #include <arm_dyn_cfg_helpers.h> 8cab0b5b0SSoby Mathew #include <assert.h> 9cab0b5b0SSoby Mathew #include <desc_image_load.h> 10cab0b5b0SSoby Mathew #include <fdt_wrappers.h> 11cab0b5b0SSoby Mathew #include <libfdt.h> 12da5f2745SSoby Mathew #include <plat_arm.h> 13cab0b5b0SSoby Mathew 14ba597da7SJohn Tsichritzis #define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr" 15ba597da7SJohn Tsichritzis #define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size" 161d71ba14SSoby Mathew 171d71ba14SSoby Mathew typedef struct config_load_info_prop { 181d71ba14SSoby Mathew unsigned int config_id; 191d71ba14SSoby Mathew const char *config_addr; 201d71ba14SSoby Mathew const char *config_max_size; 211d71ba14SSoby Mathew } config_load_info_prop_t; 221d71ba14SSoby Mathew 231d71ba14SSoby Mathew static const config_load_info_prop_t prop_names[] = { 241d71ba14SSoby Mathew {HW_CONFIG_ID, "hw_config_addr", "hw_config_max_size"}, 251d71ba14SSoby Mathew {SOC_FW_CONFIG_ID, "soc_fw_config_addr", "soc_fw_config_max_size"}, 261d71ba14SSoby Mathew {TOS_FW_CONFIG_ID, "tos_fw_config_addr", "tos_fw_config_max_size"}, 271d71ba14SSoby Mathew {NT_FW_CONFIG_ID, "nt_fw_config_addr", "nt_fw_config_max_size"} 281d71ba14SSoby Mathew }; 291d71ba14SSoby Mathew 30cab0b5b0SSoby Mathew /******************************************************************************* 311d71ba14SSoby Mathew * Helper to read the load information corresponding to the `config_id` in 321d71ba14SSoby Mathew * TB_FW_CONFIG. This function expects the following properties to be defined : 331d71ba14SSoby Mathew * <config>_addr size : 2 cells 341d71ba14SSoby Mathew * <config>_max_size size : 1 cell 35cab0b5b0SSoby Mathew * 36cab0b5b0SSoby Mathew * Arguments: 37cab0b5b0SSoby Mathew * void *dtb - pointer to the TB_FW_CONFIG in memory 38cab0b5b0SSoby Mathew * int node - The node offset to appropriate node in the 39cab0b5b0SSoby Mathew * DTB. 401d71ba14SSoby Mathew * unsigned int config_id - The configuration id 411d71ba14SSoby Mathew * uint64_t *config_addr - Returns the `config` load address if read 42cab0b5b0SSoby Mathew * is successful. 431d71ba14SSoby Mathew * uint32_t *config_size - Returns the `config` size if read is 44cab0b5b0SSoby Mathew * successful. 45cab0b5b0SSoby Mathew * 46cab0b5b0SSoby Mathew * Returns 0 on success and -1 on error. 47cab0b5b0SSoby Mathew ******************************************************************************/ 481d71ba14SSoby Mathew int arm_dyn_get_config_load_info(void *dtb, int node, unsigned int config_id, 491d71ba14SSoby Mathew uint64_t *config_addr, uint32_t *config_size) 50cab0b5b0SSoby Mathew { 51cab0b5b0SSoby Mathew int err; 521d71ba14SSoby Mathew unsigned int i; 53cab0b5b0SSoby Mathew 54da5f2745SSoby Mathew assert(dtb != NULL); 551d71ba14SSoby Mathew assert(config_addr != NULL); 561d71ba14SSoby Mathew assert(config_size != NULL); 571d71ba14SSoby Mathew 581d71ba14SSoby Mathew for (i = 0; i < ARRAY_SIZE(prop_names); i++) { 591d71ba14SSoby Mathew if (prop_names[i].config_id == config_id) 601d71ba14SSoby Mathew break; 611d71ba14SSoby Mathew } 621d71ba14SSoby Mathew 631d71ba14SSoby Mathew if (i == ARRAY_SIZE(prop_names)) { 641d71ba14SSoby Mathew WARN("Invalid config id %d\n", config_id); 651d71ba14SSoby Mathew return -1; 661d71ba14SSoby Mathew } 67cab0b5b0SSoby Mathew 68cab0b5b0SSoby Mathew /* Check if the pointer to DT is correct */ 69cab0b5b0SSoby Mathew assert(fdt_check_header(dtb) == 0); 70cab0b5b0SSoby Mathew 71cab0b5b0SSoby Mathew /* Assert the node offset point to "arm,tb_fw" compatible property */ 72cab0b5b0SSoby Mathew assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw")); 73cab0b5b0SSoby Mathew 741d71ba14SSoby Mathew err = fdtw_read_cells(dtb, node, prop_names[i].config_addr, 2, 751d71ba14SSoby Mathew (void *) config_addr); 76cab0b5b0SSoby Mathew if (err < 0) { 771d71ba14SSoby Mathew WARN("Read cell failed for %s\n", prop_names[i].config_addr); 78cab0b5b0SSoby Mathew return -1; 79cab0b5b0SSoby Mathew } 80cab0b5b0SSoby Mathew 811d71ba14SSoby Mathew err = fdtw_read_cells(dtb, node, prop_names[i].config_max_size, 1, 821d71ba14SSoby Mathew (void *) config_size); 83cab0b5b0SSoby Mathew if (err < 0) { 841d71ba14SSoby Mathew WARN("Read cell failed for %s\n", prop_names[i].config_max_size); 85cab0b5b0SSoby Mathew return -1; 86cab0b5b0SSoby Mathew } 87cab0b5b0SSoby Mathew 881d71ba14SSoby Mathew VERBOSE("Dyn cfg: Read config_id %d load info from TB_FW_CONFIG 0x%llx 0x%x\n", 891d71ba14SSoby Mathew config_id, (unsigned long long)*config_addr, *config_size); 90cab0b5b0SSoby Mathew 91cab0b5b0SSoby Mathew return 0; 92cab0b5b0SSoby Mathew } 93cab0b5b0SSoby Mathew 94cab0b5b0SSoby Mathew /******************************************************************************* 956e79f9fdSSoby Mathew * Helper to read the `disable_auth` property in config DTB. This function 966e79f9fdSSoby Mathew * expects the following properties to be present in the config DTB. 976e79f9fdSSoby Mathew * name : disable_auth size : 1 cell 986e79f9fdSSoby Mathew * 996e79f9fdSSoby Mathew * Arguments: 1006e79f9fdSSoby Mathew * void *dtb - pointer to the TB_FW_CONFIG in memory 1016e79f9fdSSoby Mathew * int node - The node offset to appropriate node in the 1026e79f9fdSSoby Mathew * DTB. 1036e79f9fdSSoby Mathew * uint64_t *disable_auth - The value of `disable_auth` property on 1046e79f9fdSSoby Mathew * successful read. Must be 0 or 1. 1056e79f9fdSSoby Mathew * 1066e79f9fdSSoby Mathew * Returns 0 on success and -1 on error. 1076e79f9fdSSoby Mathew ******************************************************************************/ 1086e79f9fdSSoby Mathew int arm_dyn_get_disable_auth(void *dtb, int node, uint32_t *disable_auth) 1096e79f9fdSSoby Mathew { 1106e79f9fdSSoby Mathew int err; 1116e79f9fdSSoby Mathew 1126e79f9fdSSoby Mathew assert(dtb != NULL); 1136e79f9fdSSoby Mathew assert(disable_auth != NULL); 1146e79f9fdSSoby Mathew 1156e79f9fdSSoby Mathew /* Check if the pointer to DT is correct */ 1166e79f9fdSSoby Mathew assert(fdt_check_header(dtb) == 0); 1176e79f9fdSSoby Mathew 1186e79f9fdSSoby Mathew /* Assert the node offset point to "arm,tb_fw" compatible property */ 1196e79f9fdSSoby Mathew assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw")); 1206e79f9fdSSoby Mathew 1216e79f9fdSSoby Mathew /* Locate the disable_auth cell and read the value */ 1226e79f9fdSSoby Mathew err = fdtw_read_cells(dtb, node, "disable_auth", 1, disable_auth); 1236e79f9fdSSoby Mathew if (err < 0) { 1246e79f9fdSSoby Mathew WARN("Read cell failed for `disable_auth`\n"); 1256e79f9fdSSoby Mathew return -1; 1266e79f9fdSSoby Mathew } 1276e79f9fdSSoby Mathew 1286e79f9fdSSoby Mathew /* Check if the value is boolean */ 1291d71ba14SSoby Mathew if ((*disable_auth != 0U) && (*disable_auth != 1U)) { 1306e79f9fdSSoby Mathew WARN("Invalid value for `disable_auth` cell %d\n", *disable_auth); 1316e79f9fdSSoby Mathew return -1; 1326e79f9fdSSoby Mathew } 1336e79f9fdSSoby Mathew 1346e79f9fdSSoby Mathew VERBOSE("Dyn cfg: `disable_auth` cell found with value = %d\n", 1356e79f9fdSSoby Mathew *disable_auth); 1366e79f9fdSSoby Mathew return 0; 1376e79f9fdSSoby Mathew } 1386e79f9fdSSoby Mathew 1396e79f9fdSSoby Mathew /******************************************************************************* 140cab0b5b0SSoby Mathew * Validate the tb_fw_config is a valid DTB file and returns the node offset 141cab0b5b0SSoby Mathew * to "arm,tb_fw" property. 142cab0b5b0SSoby Mathew * Arguments: 143cab0b5b0SSoby Mathew * void *dtb - pointer to the TB_FW_CONFIG in memory 144cab0b5b0SSoby Mathew * int *node - Returns the node offset to "arm,tb_fw" property if found. 145cab0b5b0SSoby Mathew * 146cab0b5b0SSoby Mathew * Returns 0 on success and -1 on error. 147cab0b5b0SSoby Mathew ******************************************************************************/ 148cab0b5b0SSoby Mathew int arm_dyn_tb_fw_cfg_init(void *dtb, int *node) 149cab0b5b0SSoby Mathew { 150da5f2745SSoby Mathew assert(dtb != NULL); 151da5f2745SSoby Mathew assert(node != NULL); 152cab0b5b0SSoby Mathew 153cab0b5b0SSoby Mathew /* Check if the pointer to DT is correct */ 154cab0b5b0SSoby Mathew if (fdt_check_header(dtb) != 0) { 155cab0b5b0SSoby Mathew WARN("Invalid DTB file passed as TB_FW_CONFIG\n"); 156cab0b5b0SSoby Mathew return -1; 157cab0b5b0SSoby Mathew } 158cab0b5b0SSoby Mathew 159cab0b5b0SSoby Mathew /* Assert the node offset point to "arm,tb_fw" compatible property */ 160cab0b5b0SSoby Mathew *node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw"); 161cab0b5b0SSoby Mathew if (*node < 0) { 162cab0b5b0SSoby Mathew WARN("The compatible property `arm,tb_fw` not found in the config\n"); 163cab0b5b0SSoby Mathew return -1; 164cab0b5b0SSoby Mathew } 165cab0b5b0SSoby Mathew 166cab0b5b0SSoby Mathew VERBOSE("Dyn cfg: Found \"arm,tb_fw\" in the config\n"); 167cab0b5b0SSoby Mathew return 0; 168cab0b5b0SSoby Mathew } 169ba597da7SJohn Tsichritzis 170ba597da7SJohn Tsichritzis /* 171ba597da7SJohn Tsichritzis * Reads and returns the Mbed TLS shared heap information from the DTB. 172ba597da7SJohn Tsichritzis * This function is supposed to be called *only* when a DTB is present. 173ba597da7SJohn Tsichritzis * This function is supposed to be called only by BL2. 174ba597da7SJohn Tsichritzis * 175ba597da7SJohn Tsichritzis * Returns: 176ba597da7SJohn Tsichritzis * 0 = success 177ba597da7SJohn Tsichritzis * -1 = error. In this case the values of heap_addr, heap_size should be 178ba597da7SJohn Tsichritzis * considered as garbage by the caller. 179ba597da7SJohn Tsichritzis */ 180ba597da7SJohn Tsichritzis int arm_get_dtb_mbedtls_heap_info(void *dtb, void **heap_addr, 181ba597da7SJohn Tsichritzis size_t *heap_size) 182ba597da7SJohn Tsichritzis { 183ba597da7SJohn Tsichritzis int err, dtb_root; 184ba597da7SJohn Tsichritzis 185ba597da7SJohn Tsichritzis /* Verify the DTB is valid and get the root node */ 186ba597da7SJohn Tsichritzis err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root); 187ba597da7SJohn Tsichritzis if (err < 0) { 188*7af2dd2eSJohn Tsichritzis ERROR("Invalid TB_FW_CONFIG. Cannot retrieve Mbed TLS heap information from DTB\n"); 189ba597da7SJohn Tsichritzis return -1; 190ba597da7SJohn Tsichritzis } 191ba597da7SJohn Tsichritzis 192ba597da7SJohn Tsichritzis /* Retrieve the Mbed TLS heap details from the DTB */ 193ba597da7SJohn Tsichritzis err = fdtw_read_cells(dtb, dtb_root, 194ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR, 2, heap_addr); 195ba597da7SJohn Tsichritzis if (err < 0) { 196*7af2dd2eSJohn Tsichritzis ERROR("Error while reading %s from DTB\n", 197ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR); 198ba597da7SJohn Tsichritzis return -1; 199ba597da7SJohn Tsichritzis } 200ba597da7SJohn Tsichritzis err = fdtw_read_cells(dtb, dtb_root, 201ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE, 1, heap_size); 202ba597da7SJohn Tsichritzis if (err < 0) { 203*7af2dd2eSJohn Tsichritzis ERROR("Error while reading %s from DTB\n", 204ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE); 205ba597da7SJohn Tsichritzis return -1; 206ba597da7SJohn Tsichritzis } 207ba597da7SJohn Tsichritzis return 0; 208ba597da7SJohn Tsichritzis } 209ba597da7SJohn Tsichritzis 210ba597da7SJohn Tsichritzis 211ba597da7SJohn Tsichritzis /* 212ba597da7SJohn Tsichritzis * This function writes the Mbed TLS heap address and size in the DTB. When it 213ba597da7SJohn Tsichritzis * is called, it is guaranteed that a DTB is available. However it is not 214ba597da7SJohn Tsichritzis * guaranteed that the shared Mbed TLS heap implementation is used. Thus we 215ba597da7SJohn Tsichritzis * return error code from here and it's the responsibility of the caller to 216ba597da7SJohn Tsichritzis * determine the action upon error. 217ba597da7SJohn Tsichritzis * 218ba597da7SJohn Tsichritzis * This function is supposed to be called only by BL1. 219ba597da7SJohn Tsichritzis * 220ba597da7SJohn Tsichritzis * Returns: 221ba597da7SJohn Tsichritzis * 0 = success 222ba597da7SJohn Tsichritzis * 1 = error 223ba597da7SJohn Tsichritzis */ 224ba597da7SJohn Tsichritzis int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size) 225ba597da7SJohn Tsichritzis { 226ba597da7SJohn Tsichritzis int err, dtb_root; 227ba597da7SJohn Tsichritzis 228ba597da7SJohn Tsichritzis /* 229ba597da7SJohn Tsichritzis * Verify that the DTB is valid, before attempting to write to it, 230ba597da7SJohn Tsichritzis * and get the DTB root node. 231ba597da7SJohn Tsichritzis */ 232ba597da7SJohn Tsichritzis err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root); 233ba597da7SJohn Tsichritzis if (err < 0) { 234*7af2dd2eSJohn Tsichritzis ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n"); 235ba597da7SJohn Tsichritzis return -1; 236ba597da7SJohn Tsichritzis } 237ba597da7SJohn Tsichritzis 238ba597da7SJohn Tsichritzis /* 239ba597da7SJohn Tsichritzis * Write the heap address and size in the DTB. 240ba597da7SJohn Tsichritzis * 241ba597da7SJohn Tsichritzis * NOTE: The variables heap_addr and heap_size are corrupted 242ba597da7SJohn Tsichritzis * by the "fdtw_write_inplace_cells" function. After the 243ba597da7SJohn Tsichritzis * function calls they must NOT be reused. 244ba597da7SJohn Tsichritzis */ 245ba597da7SJohn Tsichritzis err = fdtw_write_inplace_cells(dtb, dtb_root, 246ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr); 247ba597da7SJohn Tsichritzis if (err < 0) { 248*7af2dd2eSJohn Tsichritzis ERROR("Unable to write DTB property %s\n", 249*7af2dd2eSJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR); 250ba597da7SJohn Tsichritzis return -1; 251ba597da7SJohn Tsichritzis } 252ba597da7SJohn Tsichritzis 253ba597da7SJohn Tsichritzis err = fdtw_write_inplace_cells(dtb, dtb_root, 254ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size); 255ba597da7SJohn Tsichritzis if (err < 0) { 256*7af2dd2eSJohn Tsichritzis ERROR("Unable to write DTB property %s\n", 257*7af2dd2eSJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE); 258ba597da7SJohn Tsichritzis return -1; 259ba597da7SJohn Tsichritzis } 260ba597da7SJohn Tsichritzis 261ba597da7SJohn Tsichritzis return 0; 262ba597da7SJohn Tsichritzis } 263