mbedtls_common.c 1.01 KB
Newer Older
1
/*
Roberto Vargas's avatar
Roberto Vargas committed
2
 * Copyright (c) 2015-2018, 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
#include <debug.h>
Juan Castillo's avatar
Juan Castillo committed
9
10
/* mbed TLS headers */
#include <mbedtls/memory_buffer_alloc.h>
11
#include <mbedtls/platform.h>
Roberto Vargas's avatar
Roberto Vargas committed
12
#include <mbedtls_common.h>
13
14
15
#include <mbedtls_config.h>
#include <platform.h>
#include <stddef.h>
16

Roberto Vargas's avatar
Roberto Vargas committed
17
18
19
20
21
22
static void cleanup(void)
{
	ERROR("EXIT from BL2\n");
	panic();
}

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

	if (!ready) {
Roberto Vargas's avatar
Roberto Vargas committed
34
35
36
		if (atexit(cleanup))
			panic();

37
38
39
40
41
42
43
44
45
		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
46
		/* Initialize the mbed TLS heap */
47
		mbedtls_memory_buffer_alloc_init(heap_addr, heap_size);
48

49
#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
50
		mbedtls_platform_set_snprintf(snprintf);
51
#endif
Juan Castillo's avatar
Juan Castillo committed
52
		ready = 1;
53
54
	}
}