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