mbedtls_common.c 1.08 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
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

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

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

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

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

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