mbedtls_common.c 1.45 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
 */

7
#include <assert.h>
8
9
#include <stddef.h>

Juan Castillo's avatar
Juan Castillo committed
10
11
/* mbed TLS headers */
#include <mbedtls/memory_buffer_alloc.h>
12
#include <mbedtls/platform.h>
13
14
15
16
17

#include <common/debug.h>
#include <drivers/auth/mbedtls/mbedtls_common.h>
#include <drivers/auth/mbedtls/mbedtls_config.h>
#include <plat/common/platform.h>
18

19
20
#pragma weak plat_get_mbedtls_heap

Roberto Vargas's avatar
Roberto Vargas committed
21
22
23
24
25
26
static void cleanup(void)
{
	ERROR("EXIT from BL2\n");
	panic();
}

27
/*
Juan Castillo's avatar
Juan Castillo committed
28
 * mbed TLS initialization function
29
30
31
32
 */
void mbedtls_init(void)
{
	static int ready;
33
34
35
	void *heap_addr;
	size_t heap_size = 0;
	int err;
36
37

	if (!ready) {
Roberto Vargas's avatar
Roberto Vargas committed
38
39
40
		if (atexit(cleanup))
			panic();

41
42
43
44
45
46
47
48
49
		err = plat_get_mbedtls_heap(&heap_addr, &heap_size);

		/* Ensure heap setup is proper */
		if (err < 0) {
			ERROR("Mbed TLS failed to get a heap\n");
			panic();
		}
		assert(heap_size >= TF_MBEDTLS_HEAP_SIZE);

Juan Castillo's avatar
Juan Castillo committed
50
		/* Initialize the mbed TLS heap */
51
		mbedtls_memory_buffer_alloc_init(heap_addr, heap_size);
52

53
#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
54
		mbedtls_platform_set_snprintf(snprintf);
55
#endif
Juan Castillo's avatar
Juan Castillo committed
56
		ready = 1;
57
58
	}
}
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74

/*
 * The following default implementation of the function simply returns the
 * by default allocated heap.
 */
int plat_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
{
	static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];

	assert(heap_addr != NULL);
	assert(heap_size != NULL);

	*heap_addr = heap;
	*heap_size = sizeof(heap);
	return 0;
}