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