Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Roberto Vargas | 3c98ce4 | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 7 | #include <arm_dyn_cfg_helpers.h> |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 8 | #include <assert.h> |
| 9 | #include <desc_image_load.h> |
| 10 | #include <fdt_wrappers.h> |
| 11 | #include <libfdt.h> |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 12 | #include <plat_arm.h> |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 13 | |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 14 | #define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr" |
| 15 | #define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size" |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 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 | |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 30 | /******************************************************************************* |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 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 |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 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. |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 40 | * unsigned int config_id - The configuration id |
| 41 | * uint64_t *config_addr - Returns the `config` load address if read |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 42 | * is successful. |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 43 | * uint32_t *config_size - Returns the `config` size if read is |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 44 | * successful. |
| 45 | * |
| 46 | * Returns 0 on success and -1 on error. |
| 47 | ******************************************************************************/ |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 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) |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 50 | { |
| 51 | int err; |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 52 | unsigned int i; |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 53 | |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 54 | assert(dtb != NULL); |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 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 | } |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 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 | |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 74 | err = fdtw_read_cells(dtb, node, prop_names[i].config_addr, 2, |
| 75 | (void *) config_addr); |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 76 | if (err < 0) { |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 77 | WARN("Read cell failed for %s\n", prop_names[i].config_addr); |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 78 | return -1; |
| 79 | } |
| 80 | |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 81 | err = fdtw_read_cells(dtb, node, prop_names[i].config_max_size, 1, |
| 82 | (void *) config_size); |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 83 | if (err < 0) { |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 84 | 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] | 85 | return -1; |
| 86 | } |
| 87 | |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 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); |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | /******************************************************************************* |
Soby Mathew | 45e39e2 | 2018-03-26 15:16:46 +0100 | [diff] [blame] | 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 */ |
Soby Mathew | b681484 | 2018-04-04 09:40:32 +0100 | [diff] [blame] | 129 | if ((*disable_auth != 0U) && (*disable_auth != 1U)) { |
Soby Mathew | 45e39e2 | 2018-03-26 15:16:46 +0100 | [diff] [blame] | 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 | /******************************************************************************* |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 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 | { |
Soby Mathew | cc36484 | 2018-02-21 01:16:39 +0000 | [diff] [blame] | 150 | assert(dtb != NULL); |
| 151 | assert(node != NULL); |
Soby Mathew | 96a1c6b | 2018-01-15 14:45:33 +0000 | [diff] [blame] | 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 | } |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 169 | |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 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) { |
John Tsichritzis | 3a20a73 | 2018-09-07 10:38:01 +0100 | [diff] [blame] | 188 | 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] | 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) { |
John Tsichritzis | 3a20a73 | 2018-09-07 10:38:01 +0100 | [diff] [blame] | 196 | ERROR("Error while reading %s from DTB\n", |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 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) { |
John Tsichritzis | 3a20a73 | 2018-09-07 10:38:01 +0100 | [diff] [blame] | 203 | ERROR("Error while reading %s from DTB\n", |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 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) { |
John Tsichritzis | 3a20a73 | 2018-09-07 10:38:01 +0100 | [diff] [blame] | 234 | ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n"); |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 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) { |
John Tsichritzis | 3a20a73 | 2018-09-07 10:38:01 +0100 | [diff] [blame] | 248 | ERROR("Unable to write DTB property %s\n", |
| 249 | DTB_PROP_MBEDTLS_HEAP_ADDR); |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 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) { |
John Tsichritzis | 3a20a73 | 2018-09-07 10:38:01 +0100 | [diff] [blame] | 256 | ERROR("Unable to write DTB property %s\n", |
| 257 | DTB_PROP_MBEDTLS_HEAP_SIZE); |
John Tsichritzis | c34341a | 2018-07-30 13:41:52 +0100 | [diff] [blame] | 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |