blob: d12d09c7f2503f21de6fba8ab55c54e5ca227f9b [file] [log] [blame]
Soby Mathew96a1c6b2018-01-15 14:45:33 +00001/*
2 * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Roberto Vargas3c98ce42018-02-12 12:36:17 +00007#include <arm_dyn_cfg_helpers.h>
Soby Mathew96a1c6b2018-01-15 14:45:33 +00008#include <assert.h>
9#include <desc_image_load.h>
10#include <fdt_wrappers.h>
11#include <libfdt.h>
Soby Mathewcc364842018-02-21 01:16:39 +000012#include <plat_arm.h>
Soby Mathew96a1c6b2018-01-15 14:45:33 +000013
John Tsichritzisc34341a2018-07-30 13:41:52 +010014#define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr"
15#define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size"
Soby Mathewb6814842018-04-04 09:40:32 +010016
17typedef 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
23static 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 Mathew96a1c6b2018-01-15 14:45:33 +000030/*******************************************************************************
Soby Mathewb6814842018-04-04 09:40:32 +010031 * 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 Mathew96a1c6b2018-01-15 14:45:33 +000035 *
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 Mathewb6814842018-04-04 09:40:32 +010040 * unsigned int config_id - The configuration id
41 * uint64_t *config_addr - Returns the `config` load address if read
Soby Mathew96a1c6b2018-01-15 14:45:33 +000042 * is successful.
Soby Mathewb6814842018-04-04 09:40:32 +010043 * uint32_t *config_size - Returns the `config` size if read is
Soby Mathew96a1c6b2018-01-15 14:45:33 +000044 * successful.
45 *
46 * Returns 0 on success and -1 on error.
47 ******************************************************************************/
Soby Mathewb6814842018-04-04 09:40:32 +010048int arm_dyn_get_config_load_info(void *dtb, int node, unsigned int config_id,
49 uint64_t *config_addr, uint32_t *config_size)
Soby Mathew96a1c6b2018-01-15 14:45:33 +000050{
51 int err;
Soby Mathewb6814842018-04-04 09:40:32 +010052 unsigned int i;
Soby Mathew96a1c6b2018-01-15 14:45:33 +000053
Soby Mathewcc364842018-02-21 01:16:39 +000054 assert(dtb != NULL);
Soby Mathewb6814842018-04-04 09:40:32 +010055 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 Mathew96a1c6b2018-01-15 14:45:33 +000067
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 Mathewb6814842018-04-04 09:40:32 +010074 err = fdtw_read_cells(dtb, node, prop_names[i].config_addr, 2,
75 (void *) config_addr);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000076 if (err < 0) {
Soby Mathewb6814842018-04-04 09:40:32 +010077 WARN("Read cell failed for %s\n", prop_names[i].config_addr);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000078 return -1;
79 }
80
Soby Mathewb6814842018-04-04 09:40:32 +010081 err = fdtw_read_cells(dtb, node, prop_names[i].config_max_size, 1,
82 (void *) config_size);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000083 if (err < 0) {
Soby Mathewb6814842018-04-04 09:40:32 +010084 WARN("Read cell failed for %s\n", prop_names[i].config_max_size);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000085 return -1;
86 }
87
Soby Mathewb6814842018-04-04 09:40:32 +010088 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 Mathew96a1c6b2018-01-15 14:45:33 +000090
91 return 0;
92}
93
94/*******************************************************************************
Soby Mathew45e39e22018-03-26 15:16:46 +010095 * 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 ******************************************************************************/
108int 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 Mathewb6814842018-04-04 09:40:32 +0100129 if ((*disable_auth != 0U) && (*disable_auth != 1U)) {
Soby Mathew45e39e22018-03-26 15:16:46 +0100130 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 Mathew96a1c6b2018-01-15 14:45:33 +0000140 * 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 ******************************************************************************/
148int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
149{
Soby Mathewcc364842018-02-21 01:16:39 +0000150 assert(dtb != NULL);
151 assert(node != NULL);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000152
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 Tsichritzisc34341a2018-07-30 13:41:52 +0100169
John Tsichritzisc34341a2018-07-30 13:41:52 +0100170/*
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 */
180int 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100188 ERROR("Invalid TB_FW_CONFIG. Cannot retrieve Mbed TLS heap information from DTB\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +0100189 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100196 ERROR("Error while reading %s from DTB\n",
John Tsichritzisc34341a2018-07-30 13:41:52 +0100197 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100203 ERROR("Error while reading %s from DTB\n",
John Tsichritzisc34341a2018-07-30 13:41:52 +0100204 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 */
224int 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100234 ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +0100235 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100248 ERROR("Unable to write DTB property %s\n",
249 DTB_PROP_MBEDTLS_HEAP_ADDR);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100250 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100256 ERROR("Unable to write DTB property %s\n",
257 DTB_PROP_MBEDTLS_HEAP_SIZE);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100258 return -1;
259 }
260
261 return 0;
262}