blob: db6f2607313f0c30f99790ebebb5224e0ae862c7 [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
Soby Mathew96a1c6b2018-01-15 14:45:33 +000018/*******************************************************************************
19 * Validate the tb_fw_config is a valid DTB file and returns the node offset
20 * to "arm,tb_fw" property.
21 * Arguments:
22 * void *dtb - pointer to the TB_FW_CONFIG in memory
23 * int *node - Returns the node offset to "arm,tb_fw" property if found.
24 *
25 * Returns 0 on success and -1 on error.
26 ******************************************************************************/
27int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
28{
Soby Mathewcc364842018-02-21 01:16:39 +000029 assert(dtb != NULL);
30 assert(node != NULL);
Soby Mathew96a1c6b2018-01-15 14:45:33 +000031
32 /* Check if the pointer to DT is correct */
33 if (fdt_check_header(dtb) != 0) {
34 WARN("Invalid DTB file passed as TB_FW_CONFIG\n");
35 return -1;
36 }
37
38 /* Assert the node offset point to "arm,tb_fw" compatible property */
39 *node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw");
40 if (*node < 0) {
41 WARN("The compatible property `arm,tb_fw` not found in the config\n");
42 return -1;
43 }
44
45 VERBOSE("Dyn cfg: Found \"arm,tb_fw\" in the config\n");
46 return 0;
47}
John Tsichritzisc34341a2018-07-30 13:41:52 +010048
John Tsichritzisc34341a2018-07-30 13:41:52 +010049/*
50 * Reads and returns the Mbed TLS shared heap information from the DTB.
51 * This function is supposed to be called *only* when a DTB is present.
52 * This function is supposed to be called only by BL2.
53 *
54 * Returns:
55 * 0 = success
56 * -1 = error. In this case the values of heap_addr, heap_size should be
57 * considered as garbage by the caller.
58 */
59int arm_get_dtb_mbedtls_heap_info(void *dtb, void **heap_addr,
60 size_t *heap_size)
61{
62 int err, dtb_root;
63
64 /* Verify the DTB is valid and get the root node */
65 err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
66 if (err < 0) {
John Tsichritzis3a20a732018-09-07 10:38:01 +010067 ERROR("Invalid TB_FW_CONFIG. Cannot retrieve Mbed TLS heap information from DTB\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +010068 return -1;
69 }
70
71 /* Retrieve the Mbed TLS heap details from the DTB */
72 err = fdtw_read_cells(dtb, dtb_root,
73 DTB_PROP_MBEDTLS_HEAP_ADDR, 2, heap_addr);
74 if (err < 0) {
John Tsichritzis3a20a732018-09-07 10:38:01 +010075 ERROR("Error while reading %s from DTB\n",
John Tsichritzisc34341a2018-07-30 13:41:52 +010076 DTB_PROP_MBEDTLS_HEAP_ADDR);
77 return -1;
78 }
79 err = fdtw_read_cells(dtb, dtb_root,
80 DTB_PROP_MBEDTLS_HEAP_SIZE, 1, heap_size);
81 if (err < 0) {
John Tsichritzis3a20a732018-09-07 10:38:01 +010082 ERROR("Error while reading %s from DTB\n",
John Tsichritzisc34341a2018-07-30 13:41:52 +010083 DTB_PROP_MBEDTLS_HEAP_SIZE);
84 return -1;
85 }
86 return 0;
87}
88
89
90/*
91 * This function writes the Mbed TLS heap address and size in the DTB. When it
92 * is called, it is guaranteed that a DTB is available. However it is not
93 * guaranteed that the shared Mbed TLS heap implementation is used. Thus we
94 * return error code from here and it's the responsibility of the caller to
95 * determine the action upon error.
96 *
97 * This function is supposed to be called only by BL1.
98 *
99 * Returns:
100 * 0 = success
101 * 1 = error
102 */
103int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size)
104{
105 int err, dtb_root;
106
107 /*
108 * Verify that the DTB is valid, before attempting to write to it,
109 * and get the DTB root node.
110 */
111 err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
112 if (err < 0) {
John Tsichritzis3a20a732018-09-07 10:38:01 +0100113 ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n");
John Tsichritzisc34341a2018-07-30 13:41:52 +0100114 return -1;
115 }
116
117 /*
118 * Write the heap address and size in the DTB.
119 *
120 * NOTE: The variables heap_addr and heap_size are corrupted
121 * by the "fdtw_write_inplace_cells" function. After the
122 * function calls they must NOT be reused.
123 */
124 err = fdtw_write_inplace_cells(dtb, dtb_root,
125 DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr);
126 if (err < 0) {
John Tsichritzis3a20a732018-09-07 10:38:01 +0100127 ERROR("Unable to write DTB property %s\n",
128 DTB_PROP_MBEDTLS_HEAP_ADDR);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100129 return -1;
130 }
131
132 err = fdtw_write_inplace_cells(dtb, dtb_root,
133 DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size);
134 if (err < 0) {
John Tsichritzis3a20a732018-09-07 10:38:01 +0100135 ERROR("Unable to write DTB property %s\n",
136 DTB_PROP_MBEDTLS_HEAP_SIZE);
John Tsichritzisc34341a2018-07-30 13:41:52 +0100137 return -1;
138 }
139
140 return 0;
141}