arm_dyn_cfg_helpers.c 3.45 KB
Newer Older
1
/*
2
 * Copyright (c) 2018-2020, ARM Limited and Contributors. All rights reserved.
3
4
5
6
7
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
8

9
#include <libfdt.h>
10
11

#include <common/fdt_wrappers.h>
12
13
#include <plat/arm/common/arm_dyn_cfg_helpers.h>
#include <plat/arm/common/plat_arm.h>
14

15
16
#define DTB_PROP_MBEDTLS_HEAP_ADDR "mbedtls_heap_addr"
#define DTB_PROP_MBEDTLS_HEAP_SIZE "mbedtls_heap_size"
17

18
19
20
21
22
23
#if MEASURED_BOOT
#define DTB_PROP_BL2_HASH_DATA	"bl2_hash_data"

static int dtb_root = -1;
#endif /* MEASURED_BOOT */

24
25
26
27
28
29
30
31
32
33
34
/*******************************************************************************
 * Validate the tb_fw_config is a valid DTB file and returns the node offset
 * to "arm,tb_fw" property.
 * Arguments:
 *	void *dtb - pointer to the TB_FW_CONFIG in memory
 *	int *node - Returns the node offset to "arm,tb_fw" property if found.
 *
 * Returns 0 on success and -1 on error.
 ******************************************************************************/
int arm_dyn_tb_fw_cfg_init(void *dtb, int *node)
{
Soby Mathew's avatar
Soby Mathew committed
35
36
	assert(dtb != NULL);
	assert(node != NULL);
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53

	/* Check if the pointer to DT is correct */
	if (fdt_check_header(dtb) != 0) {
		WARN("Invalid DTB file passed as TB_FW_CONFIG\n");
		return -1;
	}

	/* Assert the node offset point to "arm,tb_fw" compatible property */
	*node = fdt_node_offset_by_compatible(dtb, -1, "arm,tb_fw");
	if (*node < 0) {
		WARN("The compatible property `arm,tb_fw` not found in the config\n");
		return -1;
	}

	VERBOSE("Dyn cfg: Found \"arm,tb_fw\" in the config\n");
	return 0;
}
54
55
56
57
58
59
60
61
62
63
64
65

/*
 * This function writes the Mbed TLS heap address and size in the DTB. When it
 * is called, it is guaranteed that a DTB is available. However it is not
 * guaranteed that the shared Mbed TLS heap implementation is used. Thus we
 * return error code from here and it's the responsibility of the caller to
 * determine the action upon error.
 *
 * This function is supposed to be called only by BL1.
 *
 * Returns:
 *	0 = success
66
 *     -1 = error
67
68
69
 */
int arm_set_dtb_mbedtls_heap_info(void *dtb, void *heap_addr, size_t heap_size)
{
70
71
72
#if !MEASURED_BOOT
	int dtb_root;
#endif
73
74
75
76
	/*
	 * Verify that the DTB is valid, before attempting to write to it,
	 * and get the DTB root node.
	 */
77
	int err = arm_dyn_tb_fw_cfg_init(dtb, &dtb_root);
78
	if (err < 0) {
79
		ERROR("Invalid TB_FW_CONFIG loaded. Unable to get root node\n");
80
81
82
83
84
85
86
87
88
89
90
91
92
		return -1;
	}

	/*
	 * Write the heap address and size in the DTB.
	 *
	 * NOTE: The variables heap_addr and heap_size are corrupted
	 * by the "fdtw_write_inplace_cells" function. After the
	 * function calls they must NOT be reused.
	 */
	err = fdtw_write_inplace_cells(dtb, dtb_root,
		DTB_PROP_MBEDTLS_HEAP_ADDR, 2, &heap_addr);
	if (err < 0) {
93
94
		ERROR("Unable to write DTB property %s\n",
			DTB_PROP_MBEDTLS_HEAP_ADDR);
95
96
97
98
99
100
		return -1;
	}

	err = fdtw_write_inplace_cells(dtb, dtb_root,
		DTB_PROP_MBEDTLS_HEAP_SIZE, 1, &heap_size);
	if (err < 0) {
101
102
		ERROR("Unable to write DTB property %s\n",
			DTB_PROP_MBEDTLS_HEAP_SIZE);
103
104
105
106
107
		return -1;
	}

	return 0;
}
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130

#if MEASURED_BOOT
/*
 * This function writes the BL2 hash data in HW_FW_CONFIG DTB.
 * When it is called, it is guaranteed that a DTB is available.
 *
 * This function is supposed to be called only by BL1.
 *
 * Returns:
 *	0 = success
 *    < 0 = error
 */
int arm_set_bl2_hash_info(void *dtb, void *data)
{
	assert(dtb_root >= 0);

	/*
	 * Write the BL2 hash data in the DTB.
	 */
	return fdtw_write_inplace_bytes(dtb, dtb_root, DTB_PROP_BL2_HASH_DATA,
					TCG_DIGEST_SIZE, data);
}
#endif /* MEASURED_BOOT */