blob: daf0f0a6387feee08545192cc776f0c14b778d65 [file] [log] [blame]
Soby Mathew96a1c6b2018-01-15 14:45:33 +00001/*
Zelalem87675d42020-02-03 14:56:42 -06002 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
Soby Mathew96a1c6b2018-01-15 14:45:33 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7#include <assert.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00008
Soby Mathew96a1c6b2018-01-15 14:45:33 +00009#include <libfdt.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000011#include <common/fdt_wrappers.h>
Antonio Nino Diazbd7b7402019-01-25 14:30:04 +000012#include <plat/arm/common/arm_dyn_cfg_helpers.h>
13#include <plat/arm/common/plat_arm.h>
Soby Mathew96a1c6b2018-01-15 14:45:33 +000014
John Tsichritzisc34341a2018-07-30 13:41:52 +010015#define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr"
16#define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size"
Soby Mathewb6814842018-04-04 09:40:32 +010017
18typedef 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
24static 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 Mathew96a1c6b2018-01-15 14:45:33 +000031/*******************************************************************************
Soby Mathewb6814842018-04-04 09:40:32 +010032 * 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 Mathew96a1c6b2018-01-15 14:45:33 +000036 *
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 Mathewb6814842018-04-04 09:40:32 +010041 * unsigned int config_id - The configuration id
42 * uint64_t *config_addr - Returns the `config` load address if read
Soby Mathew96a1c6b2018-01-15 14:45:33 +000043 * is successful.
Soby Mathewb6814842018-04-04 09:40:32 +010044 * uint32_t *config_size - Returns the `config` size if read is
Soby Mathew96a1c6b2018-01-15 14:45:33 +000045 * successful.
46 *
47 * Returns 0 on success and -1 on error.
48 ******************************************************************************/
Soby Mathewb6814842018-04-04 09:40:32 +010049int arm_dyn_get_config_load_info(void *dtb, int node, unsigned int config_id,
50 uint64_t *config_addr, uint32_t *config_size)
Soby Mathew96a1c6b2018-01-15 14:45:33 +000051{
52 int err;
Soby Mathewb6814842018-04-04 09:40:32 +010053 unsigned int i;
Soby Mathew96a1c6b2018-01-15 14:45:33 +000054
Soby Mathewcc364842018-02-21 01:16:39 +000055 assert(dtb != NULL);
Soby Mathewb6814842018-04-04 09:40:32 +010056 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 Mathew96a1c6b2018-01-15 14:45:33 +000068
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 Mathewb6814842018-04-04 09:40:32 +010075 err = fdtw_read_cells(dtb, node, prop_names[i].config_addr, 2,
76 (void *) config_addr);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000077 if (err < 0) {
Soby Mathewb6814842018-04-04 09:40:32 +010078 WARN("Read cell failed for %s\n", prop_names[i].config_addr);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000079 return -1;
80 }
81
Soby Mathewb6814842018-04-04 09:40:32 +010082 err = fdtw_read_cells(dtb, node, prop_names[i].config_max_size, 1,
83 (void *) config_size);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000084 if (err < 0) {
Soby Mathewb6814842018-04-04 09:40:32 +010085 WARN("Read cell failed for %s\n", prop_names[i].config_max_size);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000086 return -1;
87 }
88
Soby Mathewb6814842018-04-04 09:40:32 +010089 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 Mathew96a1c6b2018-01-15 14:45:33 +000091
92 return 0;
93}
94
95/*******************************************************************************
Soby Mathew45e39e22018-03-26 15:16:46 +010096 * 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 ******************************************************************************/
109int 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 Mathewb6814842018-04-04 09:40:32 +0100130 if ((*disable_auth != 0U) && (*disable_auth != 1U)) {
Soby Mathew45e39e22018-03-26 15:16:46 +0100131 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 Mathew96a1c6b2018-01-15 14:45:33 +0000141 * 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 ******************************************************************************/
149int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
150{
Soby Mathewcc364842018-02-21 01:16:39 +0000151 assert(dtb != NULL);
152 assert(node != NULL);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000153
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 Tsichritzisc34341a2018-07-30 13:41:52 +0100170
John Tsichritzisc34341a2018-07-30 13:41:52 +0100171/*
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 */
181int 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100189 ERROR("Invalid TB_FW_CONFIG. Cannot retrieve Mbed TLS heap information from DTB\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +0100190 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100197 ERROR("Error while reading %s from DTB\n",
John Tsichritzisc34341a2018-07-30 13:41:52 +0100198 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100204 ERROR("Error while reading %s from DTB\n",
John Tsichritzisc34341a2018-07-30 13:41:52 +0100205 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 */
225int 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100235 ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +0100236 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100249 ERROR("Unable to write DTB property %s\n",
250 DTB_PROP_MBEDTLS_HEAP_ADDR);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100251 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 Tsichritzis3a20a732018-09-07 10:38:01 +0100257 ERROR("Unable to write DTB property %s\n",
258 DTB_PROP_MBEDTLS_HEAP_SIZE);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100259 return -1;
260 }
261
262 return 0;
263}