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 14*ba597da7SJohn Tsichritzis #define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr" 15*ba597da7SJohn 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 } 169*ba597da7SJohn Tsichritzis 170*ba597da7SJohn Tsichritzis 171*ba597da7SJohn Tsichritzis #if TRUSTED_BOARD_BOOT && LOAD_IMAGE_V2 172*ba597da7SJohn Tsichritzis /* 173*ba597da7SJohn Tsichritzis * Reads and returns the Mbed TLS shared heap information from the DTB. 174*ba597da7SJohn Tsichritzis * This function is supposed to be called *only* when a DTB is present. 175*ba597da7SJohn Tsichritzis * This function is supposed to be called only by BL2. 176*ba597da7SJohn Tsichritzis * 177*ba597da7SJohn Tsichritzis * Returns: 178*ba597da7SJohn Tsichritzis * 0 = success 179*ba597da7SJohn Tsichritzis * -1 = error. In this case the values of heap_addr, heap_size should be 180*ba597da7SJohn Tsichritzis * considered as garbage by the caller. 181*ba597da7SJohn Tsichritzis */ 182*ba597da7SJohn Tsichritzis int arm_get_dtb_mbedtls_heap_info(void *dtb, void **heap_addr, 183*ba597da7SJohn Tsichritzis size_t *heap_size) 184*ba597da7SJohn Tsichritzis { 185*ba597da7SJohn Tsichritzis int err, dtb_root; 186*ba597da7SJohn Tsichritzis 187*ba597da7SJohn Tsichritzis /* Verify the DTB is valid and get the root node */ 188*ba597da7SJohn Tsichritzis err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root); 189*ba597da7SJohn Tsichritzis if (err < 0) { 190*ba597da7SJohn Tsichritzis ERROR("%s: Invalid TB_FW_CONFIG. Cannot retrieve Mbed TLS " 191*ba597da7SJohn Tsichritzis "heap information from DTB\n", __func__); 192*ba597da7SJohn Tsichritzis return -1; 193*ba597da7SJohn Tsichritzis } 194*ba597da7SJohn Tsichritzis 195*ba597da7SJohn Tsichritzis /* Retrieve the Mbed TLS heap details from the DTB */ 196*ba597da7SJohn Tsichritzis err = fdtw_read_cells(dtb, dtb_root, 197*ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR, 2, heap_addr); 198*ba597da7SJohn Tsichritzis if (err < 0) { 199*ba597da7SJohn Tsichritzis ERROR("%s: error while reading %s from DTB\n", __func__, 200*ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR); 201*ba597da7SJohn Tsichritzis return -1; 202*ba597da7SJohn Tsichritzis } 203*ba597da7SJohn Tsichritzis err = fdtw_read_cells(dtb, dtb_root, 204*ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE, 1, heap_size); 205*ba597da7SJohn Tsichritzis if (err < 0) { 206*ba597da7SJohn Tsichritzis ERROR("%s: error while reading %s from DTB\n", __func__, 207*ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE); 208*ba597da7SJohn Tsichritzis return -1; 209*ba597da7SJohn Tsichritzis } 210*ba597da7SJohn Tsichritzis return 0; 211*ba597da7SJohn Tsichritzis } 212*ba597da7SJohn Tsichritzis 213*ba597da7SJohn Tsichritzis 214*ba597da7SJohn Tsichritzis /* 215*ba597da7SJohn Tsichritzis * This function writes the Mbed TLS heap address and size in the DTB. When it 216*ba597da7SJohn Tsichritzis * is called, it is guaranteed that a DTB is available. However it is not 217*ba597da7SJohn Tsichritzis * guaranteed that the shared Mbed TLS heap implementation is used. Thus we 218*ba597da7SJohn Tsichritzis * return error code from here and it's the responsibility of the caller to 219*ba597da7SJohn Tsichritzis * determine the action upon error. 220*ba597da7SJohn Tsichritzis * 221*ba597da7SJohn Tsichritzis * This function is supposed to be called only by BL1. 222*ba597da7SJohn Tsichritzis * 223*ba597da7SJohn Tsichritzis * Returns: 224*ba597da7SJohn Tsichritzis * 0 = success 225*ba597da7SJohn Tsichritzis * 1 = error 226*ba597da7SJohn Tsichritzis */ 227*ba597da7SJohn Tsichritzis int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size) 228*ba597da7SJohn Tsichritzis { 229*ba597da7SJohn Tsichritzis int err, dtb_root; 230*ba597da7SJohn Tsichritzis 231*ba597da7SJohn Tsichritzis /* 232*ba597da7SJohn Tsichritzis * Verify that the DTB is valid, before attempting to write to it, 233*ba597da7SJohn Tsichritzis * and get the DTB root node. 234*ba597da7SJohn Tsichritzis */ 235*ba597da7SJohn Tsichritzis err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root); 236*ba597da7SJohn Tsichritzis if (err < 0) { 237*ba597da7SJohn Tsichritzis ERROR("%s: Invalid TB_FW_CONFIG loaded. Unable to get " 238*ba597da7SJohn Tsichritzis "root node\n", __func__); 239*ba597da7SJohn Tsichritzis return -1; 240*ba597da7SJohn Tsichritzis } 241*ba597da7SJohn Tsichritzis 242*ba597da7SJohn Tsichritzis /* 243*ba597da7SJohn Tsichritzis * Write the heap address and size in the DTB. 244*ba597da7SJohn Tsichritzis * 245*ba597da7SJohn Tsichritzis * NOTE: The variables heap_addr and heap_size are corrupted 246*ba597da7SJohn Tsichritzis * by the "fdtw_write_inplace_cells" function. After the 247*ba597da7SJohn Tsichritzis * function calls they must NOT be reused. 248*ba597da7SJohn Tsichritzis */ 249*ba597da7SJohn Tsichritzis err = fdtw_write_inplace_cells(dtb, dtb_root, 250*ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr); 251*ba597da7SJohn Tsichritzis if (err < 0) { 252*ba597da7SJohn Tsichritzis ERROR("%s: unable to write DTB property %s\n", 253*ba597da7SJohn Tsichritzis __func__, DTB_PROP_MBEDTLS_HEAP_ADDR); 254*ba597da7SJohn Tsichritzis return -1; 255*ba597da7SJohn Tsichritzis } 256*ba597da7SJohn Tsichritzis 257*ba597da7SJohn Tsichritzis err = fdtw_write_inplace_cells(dtb, dtb_root, 258*ba597da7SJohn Tsichritzis DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size); 259*ba597da7SJohn Tsichritzis if (err < 0) { 260*ba597da7SJohn Tsichritzis ERROR("%s: unable to write DTB property %s\n", 261*ba597da7SJohn Tsichritzis __func__, DTB_PROP_MBEDTLS_HEAP_SIZE); 262*ba597da7SJohn Tsichritzis return -1; 263*ba597da7SJohn Tsichritzis } 264*ba597da7SJohn Tsichritzis 265*ba597da7SJohn Tsichritzis return 0; 266*ba597da7SJohn Tsichritzis } 267*ba597da7SJohn Tsichritzis #endif /* TRUSTED_BOARD_BOOT && LOAD_IMAGE_V2 */ 268