1 /* 2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <arm_dyn_cfg_helpers.h> 8 #include <assert.h> 9 #include <desc_image_load.h> 10 #include <fdt_wrappers.h> 11 #include <libfdt.h> 12 #include <plat_arm.h> 13 14 #define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr" 15 #define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size" 16 17 typedef struct config_load_info_prop { 18 unsigned int config_id; 19 const char *config_addr; 20 const char *config_max_size; 21 } config_load_info_prop_t; 22 23 static const config_load_info_prop_t prop_names[] = { 24 {HW_CONFIG_ID, "hw_config_addr", "hw_config_max_size"}, 25 {SOC_FW_CONFIG_ID, "soc_fw_config_addr", "soc_fw_config_max_size"}, 26 {TOS_FW_CONFIG_ID, "tos_fw_config_addr", "tos_fw_config_max_size"}, 27 {NT_FW_CONFIG_ID, "nt_fw_config_addr", "nt_fw_config_max_size"} 28 }; 29 30 /******************************************************************************* 31 * Helper to read the load information corresponding to the `config_id` in 32 * TB_FW_CONFIG. This function expects the following properties to be defined : 33 * <config>_addr size : 2 cells 34 * <config>_max_size size : 1 cell 35 * 36 * Arguments: 37 * void *dtb - pointer to the TB_FW_CONFIG in memory 38 * int node - The node offset to appropriate node in the 39 * DTB. 40 * unsigned int config_id - The configuration id 41 * uint64_t *config_addr - Returns the `config` load address if read 42 * is successful. 43 * uint32_t *config_size - Returns the `config` size if read is 44 * successful. 45 * 46 * Returns 0 on success and -1 on error. 47 ******************************************************************************/ 48 int arm_dyn_get_config_load_info(void *dtb, int node, unsigned int config_id, 49 uint64_t *config_addr, uint32_t *config_size) 50 { 51 int err; 52 unsigned int i; 53 54 assert(dtb != NULL); 55 assert(config_addr != NULL); 56 assert(config_size != NULL); 57 58 for (i = 0; i < ARRAY_SIZE(prop_names); i++) { 59 if (prop_names[i].config_id == config_id) 60 break; 61 } 62 63 if (i == ARRAY_SIZE(prop_names)) { 64 WARN("Invalid config id %d\n", config_id); 65 return -1; 66 } 67 68 /* Check if the pointer to DT is correct */ 69 assert(fdt_check_header(dtb) == 0); 70 71 /* Assert the node offset point to "arm,tb_fw" compatible property */ 72 assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw")); 73 74 err = fdtw_read_cells(dtb, node, prop_names[i].config_addr, 2, 75 (void *) config_addr); 76 if (err < 0) { 77 WARN("Read cell failed for %s\n", prop_names[i].config_addr); 78 return -1; 79 } 80 81 err = fdtw_read_cells(dtb, node, prop_names[i].config_max_size, 1, 82 (void *) config_size); 83 if (err < 0) { 84 WARN("Read cell failed for %s\n", prop_names[i].config_max_size); 85 return -1; 86 } 87 88 VERBOSE("Dyn cfg: Read config_id %d load info from TB_FW_CONFIG 0x%llx 0x%x\n", 89 config_id, (unsigned long long)*config_addr, *config_size); 90 91 return 0; 92 } 93 94 /******************************************************************************* 95 * Helper to read the `disable_auth` property in config DTB. This function 96 * expects the following properties to be present in the config DTB. 97 * name : disable_auth size : 1 cell 98 * 99 * Arguments: 100 * void *dtb - pointer to the TB_FW_CONFIG in memory 101 * int node - The node offset to appropriate node in the 102 * DTB. 103 * uint64_t *disable_auth - The value of `disable_auth` property on 104 * successful read. Must be 0 or 1. 105 * 106 * Returns 0 on success and -1 on error. 107 ******************************************************************************/ 108 int arm_dyn_get_disable_auth(void *dtb, int node, uint32_t *disable_auth) 109 { 110 int err; 111 112 assert(dtb != NULL); 113 assert(disable_auth != NULL); 114 115 /* Check if the pointer to DT is correct */ 116 assert(fdt_check_header(dtb) == 0); 117 118 /* Assert the node offset point to "arm,tb_fw" compatible property */ 119 assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw")); 120 121 /* Locate the disable_auth cell and read the value */ 122 err = fdtw_read_cells(dtb, node, "disable_auth", 1, disable_auth); 123 if (err < 0) { 124 WARN("Read cell failed for `disable_auth`\n"); 125 return -1; 126 } 127 128 /* Check if the value is boolean */ 129 if ((*disable_auth != 0U) && (*disable_auth != 1U)) { 130 WARN("Invalid value for `disable_auth` cell %d\n", *disable_auth); 131 return -1; 132 } 133 134 VERBOSE("Dyn cfg: `disable_auth` cell found with value = %d\n", 135 *disable_auth); 136 return 0; 137 } 138 139 /******************************************************************************* 140 * Validate the tb_fw_config is a valid DTB file and returns the node offset 141 * to "arm,tb_fw" property. 142 * Arguments: 143 * void *dtb - pointer to the TB_FW_CONFIG in memory 144 * int *node - Returns the node offset to "arm,tb_fw" property if found. 145 * 146 * Returns 0 on success and -1 on error. 147 ******************************************************************************/ 148 int arm_dyn_tb_fw_cfg_init(void *dtb, int *node) 149 { 150 assert(dtb != NULL); 151 assert(node != NULL); 152 153 /* Check if the pointer to DT is correct */ 154 if (fdt_check_header(dtb) != 0) { 155 WARN("Invalid DTB file passed as TB_FW_CONFIG\n"); 156 return -1; 157 } 158 159 /* Assert the node offset point to "arm,tb_fw" compatible property */ 160 *node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw"); 161 if (*node < 0) { 162 WARN("The compatible property `arm,tb_fw` not found in the config\n"); 163 return -1; 164 } 165 166 VERBOSE("Dyn cfg: Found \"arm,tb_fw\" in the config\n"); 167 return 0; 168 } 169 170 /* 171 * Reads and returns the Mbed TLS shared heap information from the DTB. 172 * This function is supposed to be called *only* when a DTB is present. 173 * This function is supposed to be called only by BL2. 174 * 175 * Returns: 176 * 0 = success 177 * -1 = error. In this case the values of heap_addr, heap_size should be 178 * considered as garbage by the caller. 179 */ 180 int arm_get_dtb_mbedtls_heap_info(void *dtb, void **heap_addr, 181 size_t *heap_size) 182 { 183 int err, dtb_root; 184 185 /* Verify the DTB is valid and get the root node */ 186 err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root); 187 if (err < 0) { 188 ERROR("Invalid TB_FW_CONFIG. Cannot retrieve Mbed TLS heap information from DTB\n"); 189 return -1; 190 } 191 192 /* Retrieve the Mbed TLS heap details from the DTB */ 193 err = fdtw_read_cells(dtb, dtb_root, 194 DTB_PROP_MBEDTLS_HEAP_ADDR, 2, heap_addr); 195 if (err < 0) { 196 ERROR("Error while reading %s from DTB\n", 197 DTB_PROP_MBEDTLS_HEAP_ADDR); 198 return -1; 199 } 200 err = fdtw_read_cells(dtb, dtb_root, 201 DTB_PROP_MBEDTLS_HEAP_SIZE, 1, heap_size); 202 if (err < 0) { 203 ERROR("Error while reading %s from DTB\n", 204 DTB_PROP_MBEDTLS_HEAP_SIZE); 205 return -1; 206 } 207 return 0; 208 } 209 210 211 /* 212 * This function writes the Mbed TLS heap address and size in the DTB. When it 213 * is called, it is guaranteed that a DTB is available. However it is not 214 * guaranteed that the shared Mbed TLS heap implementation is used. Thus we 215 * return error code from here and it's the responsibility of the caller to 216 * determine the action upon error. 217 * 218 * This function is supposed to be called only by BL1. 219 * 220 * Returns: 221 * 0 = success 222 * 1 = error 223 */ 224 int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size) 225 { 226 int err, dtb_root; 227 228 /* 229 * Verify that the DTB is valid, before attempting to write to it, 230 * and get the DTB root node. 231 */ 232 err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root); 233 if (err < 0) { 234 ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n"); 235 return -1; 236 } 237 238 /* 239 * Write the heap address and size in the DTB. 240 * 241 * NOTE: The variables heap_addr and heap_size are corrupted 242 * by the "fdtw_write_inplace_cells" function. After the 243 * function calls they must NOT be reused. 244 */ 245 err = fdtw_write_inplace_cells(dtb, dtb_root, 246 DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr); 247 if (err < 0) { 248 ERROR("Unable to write DTB property %s\n", 249 DTB_PROP_MBEDTLS_HEAP_ADDR); 250 return -1; 251 } 252 253 err = fdtw_write_inplace_cells(dtb, dtb_root, 254 DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size); 255 if (err < 0) { 256 ERROR("Unable to write DTB property %s\n", 257 DTB_PROP_MBEDTLS_HEAP_SIZE); 258 return -1; 259 } 260 261 return 0; 262 } 263