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 7cab0b5b0SSoby Mathew #include <assert.h> 809d40e0eSAntonio Nino Diaz 9cab0b5b0SSoby Mathew #include <libfdt.h> 1009d40e0eSAntonio Nino Diaz 1109d40e0eSAntonio Nino Diaz #include <common/desc_image_load.h> 1209d40e0eSAntonio Nino Diaz #include <common/fdt_wrappers.h> 13*bd9344f6SAntonio Nino Diaz #include <plat/arm/common/arm_dyn_cfg_helpers.h> 14*bd9344f6SAntonio Nino Diaz #include <plat/arm/common/plat_arm.h> 15cab0b5b0SSoby Mathew 16ba597da7SJohn Tsichritzis #define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr" 17ba597da7SJohn Tsichritzis #define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size" 181d71ba14SSoby Mathew 191d71ba14SSoby Mathew typedef struct config_load_info_prop { 201d71ba14SSoby Mathew unsigned int config_id; 211d71ba14SSoby Mathew const char *config_addr; 221d71ba14SSoby Mathew const char *config_max_size; 231d71ba14SSoby Mathew } config_load_info_prop_t; 241d71ba14SSoby Mathew 251d71ba14SSoby Mathew static const config_load_info_prop_t prop_names[] = { 261d71ba14SSoby Mathew {HW_CONFIG_ID, "hw_config_addr", "hw_config_max_size"}, 271d71ba14SSoby Mathew {SOC_FW_CONFIG_ID, "soc_fw_config_addr", "soc_fw_config_max_size"}, 281d71ba14SSoby Mathew {TOS_FW_CONFIG_ID, "tos_fw_config_addr", "tos_fw_config_max_size"}, 291d71ba14SSoby Mathew {NT_FW_CONFIG_ID, "nt_fw_config_addr", "nt_fw_config_max_size"} 301d71ba14SSoby Mathew }; 311d71ba14SSoby Mathew 32cab0b5b0SSoby Mathew /******************************************************************************* 331d71ba14SSoby Mathew * Helper to read the load information corresponding to the `config_id` in 341d71ba14SSoby Mathew * TB_FW_CONFIG. This function expects the following properties to be defined : 351d71ba14SSoby Mathew * <config>_addr size : 2 cells 361d71ba14SSoby Mathew * <config>_max_size size : 1 cell 37cab0b5b0SSoby Mathew * 38cab0b5b0SSoby Mathew * Arguments: 39cab0b5b0SSoby Mathew * void *dtb - pointer to the TB_FW_CONFIG in memory 40cab0b5b0SSoby Mathew * int node - The node offset to appropriate node in the 41cab0b5b0SSoby Mathew * DTB. 421d71ba14SSoby Mathew * unsigned int config_id - The configuration id 431d71ba14SSoby Mathew * uint64_t *config_addr - Returns the `config` load address if read 44cab0b5b0SSoby Mathew * is successful. 451d71ba14SSoby Mathew * uint32_t *config_size - Returns the `config` size if read is 46cab0b5b0SSoby Mathew * successful. 47cab0b5b0SSoby Mathew * 48cab0b5b0SSoby Mathew * Returns 0 on success and -1 on error. 49cab0b5b0SSoby Mathew ******************************************************************************/ 501d71ba14SSoby Mathew int arm_dyn_get_config_load_info(void *dtb, int node, unsigned int config_id, 511d71ba14SSoby Mathew uint64_t *config_addr, uint32_t *config_size) 52cab0b5b0SSoby Mathew { 53cab0b5b0SSoby Mathew int err; 541d71ba14SSoby Mathew unsigned int i; 55cab0b5b0SSoby Mathew 56da5f2745SSoby Mathew assert(dtb != NULL); 571d71ba14SSoby Mathew assert(config_addr != NULL); 581d71ba14SSoby Mathew assert(config_size != NULL); 591d71ba14SSoby Mathew 601d71ba14SSoby Mathew for (i = 0; i < ARRAY_SIZE(prop_names); i++) { 611d71ba14SSoby Mathew if (prop_names[i].config_id == config_id) 621d71ba14SSoby Mathew break; 631d71ba14SSoby Mathew } 641d71ba14SSoby Mathew 651d71ba14SSoby Mathew if (i == ARRAY_SIZE(prop_names)) { 661d71ba14SSoby Mathew WARN("Invalid config id %d\n", config_id); 671d71ba14SSoby Mathew return -1; 681d71ba14SSoby Mathew } 69cab0b5b0SSoby Mathew 70cab0b5b0SSoby Mathew /* Check if the pointer to DT is correct */ 71cab0b5b0SSoby Mathew assert(fdt_check_header(dtb) == 0); 72cab0b5b0SSoby Mathew 73cab0b5b0SSoby Mathew /* Assert the node offset point to "arm,tb_fw" compatible property */ 74cab0b5b0SSoby Mathew assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw")); 75cab0b5b0SSoby Mathew 761d71ba14SSoby Mathew err = fdtw_read_cells(dtb, node, prop_names[i].config_addr, 2, 771d71ba14SSoby Mathew (void *) config_addr); 78cab0b5b0SSoby Mathew if (err < 0) { 791d71ba14SSoby Mathew WARN("Read cell failed for %s\n", prop_names[i].config_addr); 80cab0b5b0SSoby Mathew return -1; 81cab0b5b0SSoby Mathew } 82cab0b5b0SSoby Mathew 831d71ba14SSoby Mathew err = fdtw_read_cells(dtb, node, prop_names[i].config_max_size, 1, 841d71ba14SSoby Mathew (void *) config_size); 85cab0b5b0SSoby Mathew if (err < 0) { 861d71ba14SSoby Mathew WARN("Read cell failed for %s\n", prop_names[i].config_max_size); 87cab0b5b0SSoby Mathew return -1; 88cab0b5b0SSoby Mathew } 89cab0b5b0SSoby Mathew 901d71ba14SSoby Mathew VERBOSE("Dyn cfg: Read config_id %d load info from TB_FW_CONFIG 0x%llx 0x%x\n", 911d71ba14SSoby Mathew config_id, (unsigned long long)*config_addr, *config_size); 92cab0b5b0SSoby Mathew 93cab0b5b0SSoby Mathew return 0; 94cab0b5b0SSoby Mathew } 95cab0b5b0SSoby Mathew 96cab0b5b0SSoby Mathew /******************************************************************************* 976e79f9fdSSoby Mathew * Helper to read the `disable_auth` property in config DTB. This function 986e79f9fdSSoby Mathew * expects the following properties to be present in the config DTB. 996e79f9fdSSoby Mathew * name : disable_auth size : 1 cell 1006e79f9fdSSoby Mathew * 1016e79f9fdSSoby Mathew * Arguments: 1026e79f9fdSSoby Mathew * void *dtb - pointer to the TB_FW_CONFIG in memory 1036e79f9fdSSoby Mathew * int node - The node offset to appropriate node in the 1046e79f9fdSSoby Mathew * DTB. 1056e79f9fdSSoby Mathew * uint64_t *disable_auth - The value of `disable_auth` property on 1066e79f9fdSSoby Mathew * successful read. Must be 0 or 1. 1076e79f9fdSSoby Mathew * 1086e79f9fdSSoby Mathew * Returns 0 on success and -1 on error. 1096e79f9fdSSoby Mathew ******************************************************************************/ 1106e79f9fdSSoby Mathew int arm_dyn_get_disable_auth(void *dtb, int node, uint32_t *disable_auth) 1116e79f9fdSSoby Mathew { 1126e79f9fdSSoby Mathew int err; 1136e79f9fdSSoby Mathew 1146e79f9fdSSoby Mathew assert(dtb != NULL); 1156e79f9fdSSoby Mathew assert(disable_auth != NULL); 1166e79f9fdSSoby Mathew 1176e79f9fdSSoby Mathew /* Check if the pointer to DT is correct */ 1186e79f9fdSSoby Mathew assert(fdt_check_header(dtb) == 0); 1196e79f9fdSSoby Mathew 1206e79f9fdSSoby Mathew /* Assert the node offset point to "arm,tb_fw" compatible property */ 1216e79f9fdSSoby Mathew assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw")); 1226e79f9fdSSoby Mathew 1236e79f9fdSSoby Mathew /* Locate the disable_auth cell and read the value */ 1246e79f9fdSSoby Mathew err = fdtw_read_cells(dtb, node, "disable_auth", 1, disable_auth); 1256e79f9fdSSoby Mathew if (err < 0) { 1266e79f9fdSSoby Mathew WARN("Read cell failed for `disable_auth`\n"); 1276e79f9fdSSoby Mathew return -1; 1286e79f9fdSSoby Mathew } 1296e79f9fdSSoby Mathew 1306e79f9fdSSoby Mathew /* Check if the value is boolean */ 1311d71ba14SSoby Mathew if ((*disable_auth != 0U) && (*disable_auth != 1U)) { 1326e79f9fdSSoby Mathew WARN("Invalid value for `disable_auth` cell %d\n", *disable_auth); 1336e79f9fdSSoby Mathew return -1; 1346e79f9fdSSoby Mathew } 1356e79f9fdSSoby Mathew 1366e79f9fdSSoby Mathew VERBOSE("Dyn cfg: `disable_auth` cell found with value = %d\n", 1376e79f9fdSSoby Mathew *disable_auth); 1386e79f9fdSSoby Mathew return 0; 1396e79f9fdSSoby Mathew } 1406e79f9fdSSoby Mathew 1416e79f9fdSSoby Mathew /******************************************************************************* 142cab0b5b0SSoby Mathew * Validate the tb_fw_config is a valid DTB file and returns the node offset 143cab0b5b0SSoby Mathew * to "arm,tb_fw" property. 144cab0b5b0SSoby Mathew * Arguments: 145cab0b5b0SSoby Mathew * void *dtb - pointer to the TB_FW_CONFIG in memory 146cab0b5b0SSoby Mathew * int *node - Returns the node offset to "arm,tb_fw" property if found. 147cab0b5b0SSoby Mathew * 148cab0b5b0SSoby Mathew * Returns 0 on success and -1 on error. 149cab0b5b0SSoby Mathew ******************************************************************************/ 150cab0b5b0SSoby Mathew int arm_dyn_tb_fw_cfg_init(void *dtb, int *node) 151cab0b5b0SSoby Mathew { 152da5f2745SSoby Mathew assert(dtb != NULL); 153da5f2745SSoby Mathew assert(node != NULL); 154cab0b5b0SSoby Mathew 155cab0b5b0SSoby Mathew /* Check if the pointer to DT is correct */ 156cab0b5b0SSoby Mathew if (fdt_check_header(dtb) != 0) { 157cab0b5b0SSoby Mathew WARN("Invalid DTB file passed as TB_FW_CONFIG\n"); 158cab0b5b0SSoby Mathew return -1; 159cab0b5b0SSoby Mathew } 160cab0b5b0SSoby Mathew 161cab0b5b0SSoby Mathew /* Assert the node offset point to "arm,tb_fw" compatible property */ 162cab0b5b0SSoby Mathew *node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw"); 163cab0b5b0SSoby Mathew if (*node < 0) { 164cab0b5b0SSoby Mathew WARN("The compatible property `arm,tb_fw` not found in the config\n"); 165cab0b5b0SSoby Mathew return -1; 166cab0b5b0SSoby Mathew } 167cab0b5b0SSoby Mathew 168cab0b5b0SSoby Mathew VERBOSE("Dyn cfg: Found \"arm,tb_fw\" in the config\n"); 169cab0b5b0SSoby Mathew return 0; 170cab0b5b0SSoby Mathew } 171ba597da7SJohn Tsichritzis 172ba597da7SJohn Tsichritzis /* 173ba597da7SJohn Tsichritzis * Reads and returns the Mbed TLS shared heap information from the DTB. 174ba597da7SJohn Tsichritzis * This function is supposed to be called *only* when a DTB is present. 175ba597da7SJohn Tsichritzis * This function is supposed to be called only by BL2. 176ba597da7SJohn Tsichritzis * 177ba597da7SJohn Tsichritzis * Returns: 178ba597da7SJohn Tsichritzis * 0 = success 179ba597da7SJohn Tsichritzis * -1 = error. In this case the values of heap_addr, heap_size should be 180ba597da7SJohn Tsichritzis * considered as garbage by the caller. 181ba597da7SJohn Tsichritzis */ 182ba597da7SJohn Tsichritzis int arm_get_dtb_mbedtls_heap_info(void *dtb, void **heap_addr, 183ba597da7SJohn Tsichritzis size_t *heap_size) 184ba597da7SJohn Tsichritzis { 185ba597da7SJohn Tsichritzis int err, dtb_root; 186ba597da7SJohn Tsichritzis 187ba597da7SJohn Tsichritzis /* Verify the DTB is valid and get the root node */ 188ba597da7SJohn Tsichritzis err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root); 189ba597da7SJohn Tsichritzis if (err < 0) { 1907af2dd2eSJohn Tsichritzis ERROR("Invalid TB_FW_CONFIG. Cannot retrieve Mbed TLS heap information from DTB\n"); 191ba597da7SJohn Tsichritzis return -1; 192ba597da7SJohn Tsichritzis } 193ba597da7SJohn Tsichritzis 194ba597da7SJohn Tsichritzis /* Retrieve the Mbed TLS heap details from the DTB */ 195ba597da7SJohn Tsichritzis err = fdtw_read_cells(dtb, dtb_root, 196ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR, 2, heap_addr); 197ba597da7SJohn Tsichritzis if (err < 0) { 1987af2dd2eSJohn Tsichritzis ERROR("Error while reading %s from DTB\n", 199ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR); 200ba597da7SJohn Tsichritzis return -1; 201ba597da7SJohn Tsichritzis } 202ba597da7SJohn Tsichritzis err = fdtw_read_cells(dtb, dtb_root, 203ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE, 1, heap_size); 204ba597da7SJohn Tsichritzis if (err < 0) { 2057af2dd2eSJohn Tsichritzis ERROR("Error while reading %s from DTB\n", 206ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE); 207ba597da7SJohn Tsichritzis return -1; 208ba597da7SJohn Tsichritzis } 209ba597da7SJohn Tsichritzis return 0; 210ba597da7SJohn Tsichritzis } 211ba597da7SJohn Tsichritzis 212ba597da7SJohn Tsichritzis 213ba597da7SJohn Tsichritzis /* 214ba597da7SJohn Tsichritzis * This function writes the Mbed TLS heap address and size in the DTB. When it 215ba597da7SJohn Tsichritzis * is called, it is guaranteed that a DTB is available. However it is not 216ba597da7SJohn Tsichritzis * guaranteed that the shared Mbed TLS heap implementation is used. Thus we 217ba597da7SJohn Tsichritzis * return error code from here and it's the responsibility of the caller to 218ba597da7SJohn Tsichritzis * determine the action upon error. 219ba597da7SJohn Tsichritzis * 220ba597da7SJohn Tsichritzis * This function is supposed to be called only by BL1. 221ba597da7SJohn Tsichritzis * 222ba597da7SJohn Tsichritzis * Returns: 223ba597da7SJohn Tsichritzis * 0 = success 224ba597da7SJohn Tsichritzis * 1 = error 225ba597da7SJohn Tsichritzis */ 226ba597da7SJohn Tsichritzis int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size) 227ba597da7SJohn Tsichritzis { 228ba597da7SJohn Tsichritzis int err, dtb_root; 229ba597da7SJohn Tsichritzis 230ba597da7SJohn Tsichritzis /* 231ba597da7SJohn Tsichritzis * Verify that the DTB is valid, before attempting to write to it, 232ba597da7SJohn Tsichritzis * and get the DTB root node. 233ba597da7SJohn Tsichritzis */ 234ba597da7SJohn Tsichritzis err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root); 235ba597da7SJohn Tsichritzis if (err < 0) { 2367af2dd2eSJohn Tsichritzis ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n"); 237ba597da7SJohn Tsichritzis return -1; 238ba597da7SJohn Tsichritzis } 239ba597da7SJohn Tsichritzis 240ba597da7SJohn Tsichritzis /* 241ba597da7SJohn Tsichritzis * Write the heap address and size in the DTB. 242ba597da7SJohn Tsichritzis * 243ba597da7SJohn Tsichritzis * NOTE: The variables heap_addr and heap_size are corrupted 244ba597da7SJohn Tsichritzis * by the "fdtw_write_inplace_cells" function. After the 245ba597da7SJohn Tsichritzis * function calls they must NOT be reused. 246ba597da7SJohn Tsichritzis */ 247ba597da7SJohn Tsichritzis err = fdtw_write_inplace_cells(dtb, dtb_root, 248ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr); 249ba597da7SJohn Tsichritzis if (err < 0) { 2507af2dd2eSJohn Tsichritzis ERROR("Unable to write DTB property %s\n", 2517af2dd2eSJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR); 252ba597da7SJohn Tsichritzis return -1; 253ba597da7SJohn Tsichritzis } 254ba597da7SJohn Tsichritzis 255ba597da7SJohn Tsichritzis err = fdtw_write_inplace_cells(dtb, dtb_root, 256ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size); 257ba597da7SJohn Tsichritzis if (err < 0) { 2587af2dd2eSJohn Tsichritzis ERROR("Unable to write DTB property %s\n", 2597af2dd2eSJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE); 260ba597da7SJohn Tsichritzis return -1; 261ba597da7SJohn Tsichritzis } 262ba597da7SJohn Tsichritzis 263ba597da7SJohn Tsichritzis return 0; 264ba597da7SJohn Tsichritzis } 265