blob: bf2f15643d54cb4e6357819f64cc2e4982b99b91 [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
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
11#include <common/desc_image_load.h>
12#include <common/fdt_wrappers.h>
13
14#include <arm_dyn_cfg_helpers.h>
Soby Mathewcc364842018-02-21 01:16:39 +000015#include <plat_arm.h>
Soby Mathew96a1c6b2018-01-15 14:45:33 +000016
John Tsichritzisc34341a2018-07-30 13:41:52 +010017#define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr"
18#define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size"
Soby Mathewb6814842018-04-04 09:40:32 +010019
20typedef struct config_load_info_prop {
21 unsigned int config_id;
22 const char *config_addr;
23 const char *config_max_size;
24} config_load_info_prop_t;
25
26static const config_load_info_prop_t prop_names[] = {
27 {HW_CONFIG_ID, "hw_config_addr", "hw_config_max_size"},
28 {SOC_FW_CONFIG_ID, "soc_fw_config_addr", "soc_fw_config_max_size"},
29 {TOS_FW_CONFIG_ID, "tos_fw_config_addr", "tos_fw_config_max_size"},
30 {NT_FW_CONFIG_ID, "nt_fw_config_addr", "nt_fw_config_max_size"}
31};
32
Soby Mathew96a1c6b2018-01-15 14:45:33 +000033/*******************************************************************************
Soby Mathewb6814842018-04-04 09:40:32 +010034 * Helper to read the load information corresponding to the `config_id` in
35 * TB_FW_CONFIG. This function expects the following properties to be defined :
36 * <config>_addr size : 2 cells
37 * <config>_max_size size : 1 cell
Soby Mathew96a1c6b2018-01-15 14:45:33 +000038 *
39 * Arguments:
40 * void *dtb - pointer to the TB_FW_CONFIG in memory
41 * int node - The node offset to appropriate node in the
42 * DTB.
Soby Mathewb6814842018-04-04 09:40:32 +010043 * unsigned int config_id - The configuration id
44 * uint64_t *config_addr - Returns the `config` load address if read
Soby Mathew96a1c6b2018-01-15 14:45:33 +000045 * is successful.
Soby Mathewb6814842018-04-04 09:40:32 +010046 * uint32_t *config_size - Returns the `config` size if read is
Soby Mathew96a1c6b2018-01-15 14:45:33 +000047 * successful.
48 *
49 * Returns 0 on success and -1 on error.
50 ******************************************************************************/
Soby Mathewb6814842018-04-04 09:40:32 +010051int arm_dyn_get_config_load_info(void *dtb, int node, unsigned int config_id,
52 uint64_t *config_addr, uint32_t *config_size)
Soby Mathew96a1c6b2018-01-15 14:45:33 +000053{
54 int err;
Soby Mathewb6814842018-04-04 09:40:32 +010055 unsigned int i;
Soby Mathew96a1c6b2018-01-15 14:45:33 +000056
Soby Mathewcc364842018-02-21 01:16:39 +000057 assert(dtb != NULL);
Soby Mathewb6814842018-04-04 09:40:32 +010058 assert(config_addr != NULL);
59 assert(config_size != NULL);
60
61 for (i = 0; i < ARRAY_SIZE(prop_names); i++) {
62 if (prop_names[i].config_id == config_id)
63 break;
64 }
65
66 if (i == ARRAY_SIZE(prop_names)) {
67 WARN("Invalid config id %d\n", config_id);
68 return -1;
69 }
Soby Mathew96a1c6b2018-01-15 14:45:33 +000070
71 /* Check if the pointer to DT is correct */
72 assert(fdt_check_header(dtb) == 0);
73
74 /* Assert the node offset point to "arm,tb_fw" compatible property */
75 assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw"));
76
Soby Mathewb6814842018-04-04 09:40:32 +010077 err = fdtw_read_cells(dtb, node, prop_names[i].config_addr, 2,
78 (void *) config_addr);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000079 if (err < 0) {
Soby Mathewb6814842018-04-04 09:40:32 +010080 WARN("Read cell failed for %s\n", prop_names[i].config_addr);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000081 return -1;
82 }
83
Soby Mathewb6814842018-04-04 09:40:32 +010084 err = fdtw_read_cells(dtb, node, prop_names[i].config_max_size, 1,
85 (void *) config_size);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000086 if (err < 0) {
Soby Mathewb6814842018-04-04 09:40:32 +010087 WARN("Read cell failed for %s\n", prop_names[i].config_max_size);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000088 return -1;
89 }
90
Soby Mathewb6814842018-04-04 09:40:32 +010091 VERBOSE("Dyn cfg: Read config_id %d load info from TB_FW_CONFIG 0x%llx 0x%x\n",
92 config_id, (unsigned long long)*config_addr, *config_size);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000093
94 return 0;
95}
96
97/*******************************************************************************
Soby Mathew45e39e22018-03-26 15:16:46 +010098 * Helper to read the `disable_auth` property in config DTB. This function
99 * expects the following properties to be present in the config DTB.
100 * name : disable_auth size : 1 cell
101 *
102 * Arguments:
103 * void *dtb - pointer to the TB_FW_CONFIG in memory
104 * int node - The node offset to appropriate node in the
105 * DTB.
106 * uint64_t *disable_auth - The value of `disable_auth` property on
107 * successful read. Must be 0 or 1.
108 *
109 * Returns 0 on success and -1 on error.
110 ******************************************************************************/
111int arm_dyn_get_disable_auth(void *dtb, int node, uint32_t *disable_auth)
112{
113 int err;
114
115 assert(dtb != NULL);
116 assert(disable_auth != NULL);
117
118 /* Check if the pointer to DT is correct */
119 assert(fdt_check_header(dtb) == 0);
120
121 /* Assert the node offset point to "arm,tb_fw" compatible property */
122 assert(node == fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw"));
123
124 /* Locate the disable_auth cell and read the value */
125 err = fdtw_read_cells(dtb, node, "disable_auth", 1, disable_auth);
126 if (err < 0) {
127 WARN("Read cell failed for `disable_auth`\n");
128 return -1;
129 }
130
131 /* Check if the value is boolean */
Soby Mathewb6814842018-04-04 09:40:32 +0100132 if ((*disable_auth != 0U) && (*disable_auth != 1U)) {
Soby Mathew45e39e22018-03-26 15:16:46 +0100133 WARN("Invalid value for `disable_auth` cell %d\n", *disable_auth);
134 return -1;
135 }
136
137 VERBOSE("Dyn cfg: `disable_auth` cell found with value = %d\n",
138 *disable_auth);
139 return 0;
140}
141
142/*******************************************************************************
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000143 * Validate the tb_fw_config is a valid DTB file and returns the node offset
144 * to "arm,tb_fw" property.
145 * Arguments:
146 * void *dtb - pointer to the TB_FW_CONFIG in memory
147 * int *node - Returns the node offset to "arm,tb_fw" property if found.
148 *
149 * Returns 0 on success and -1 on error.
150 ******************************************************************************/
151int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
152{
Soby Mathewcc364842018-02-21 01:16:39 +0000153 assert(dtb != NULL);
154 assert(node != NULL);
Soby Mathew96a1c6b2018-01-15 14:45:33 +0000155
156 /* Check if the pointer to DT is correct */
157 if (fdt_check_header(dtb) != 0) {
158 WARN("Invalid DTB file passed as TB_FW_CONFIG\n");
159 return -1;
160 }
161
162 /* Assert the node offset point to "arm,tb_fw" compatible property */
163 *node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw");
164 if (*node < 0) {
165 WARN("The compatible property `arm,tb_fw` not found in the config\n");
166 return -1;
167 }
168
169 VERBOSE("Dyn cfg: Found \"arm,tb_fw\" in the config\n");
170 return 0;
171}
John Tsichritzisc34341a2018-07-30 13:41:52 +0100172
John Tsichritzisc34341a2018-07-30 13:41:52 +0100173/*
174 * Reads and returns the Mbed TLS shared heap information from the DTB.
175 * This function is supposed to be called *only* when a DTB is present.
176 * This function is supposed to be called only by BL2.
177 *
178 * Returns:
179 * 0 = success
180 * -1 = error. In this case the values of heap_addr, heap_size should be
181 * considered as garbage by the caller.
182 */
183int arm_get_dtb_mbedtls_heap_info(void *dtb, void **heap_addr,
184 size_t *heap_size)
185{
186 int err, dtb_root;
187
188 /* Verify the DTB is valid and get the root node */
189 err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
190 if (err < 0) {
John Tsichritzis3a20a732018-09-07 10:38:01 +0100191 ERROR("Invalid TB_FW_CONFIG. Cannot retrieve Mbed TLS heap information from DTB\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +0100192 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) {
John Tsichritzis3a20a732018-09-07 10:38:01 +0100199 ERROR("Error while reading %s from DTB\n",
John Tsichritzisc34341a2018-07-30 13:41:52 +0100200 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) {
John Tsichritzis3a20a732018-09-07 10:38:01 +0100206 ERROR("Error while reading %s from DTB\n",
John Tsichritzisc34341a2018-07-30 13:41:52 +0100207 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 */
227int 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) {
John Tsichritzis3a20a732018-09-07 10:38:01 +0100237 ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +0100238 return -1;
239 }
240
241 /*
242 * Write the heap address and size in the DTB.
243 *
244 * NOTE: The variables heap_addr and heap_size are corrupted
245 * by the "fdtw_write_inplace_cells" function. After the
246 * function calls they must NOT be reused.
247 */
248 err = fdtw_write_inplace_cells(dtb, dtb_root,
249 DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr);
250 if (err < 0) {
John Tsichritzis3a20a732018-09-07 10:38:01 +0100251 ERROR("Unable to write DTB property %s\n",
252 DTB_PROP_MBEDTLS_HEAP_ADDR);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100253 return -1;
254 }
255
256 err = fdtw_write_inplace_cells(dtb, dtb_root,
257 DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size);
258 if (err < 0) {
John Tsichritzis3a20a732018-09-07 10:38:01 +0100259 ERROR("Unable to write DTB property %s\n",
260 DTB_PROP_MBEDTLS_HEAP_SIZE);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100261 return -1;
262 }
263
264 return 0;
265}