xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_common.c (revision 9a207532f8216bf83fed0891fed9ed0bc72ca450)
1 /*
2  * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <stddef.h>
9 
10 /* mbed TLS headers */
11 #include <mbedtls/memory_buffer_alloc.h>
12 #include <mbedtls/platform.h>
13 
14 #include <common/debug.h>
15 #include <drivers/auth/mbedtls/mbedtls_common.h>
16 #include <drivers/auth/mbedtls/mbedtls_config.h>
17 #include <plat/common/platform.h>
18 
19 static void cleanup(void)
20 {
21 	ERROR("EXIT from BL2\n");
22 	panic();
23 }
24 
25 /*
26  * mbed TLS initialization function
27  */
28 void mbedtls_init(void)
29 {
30 	static int ready;
31 	void *heap_addr;
32 	size_t heap_size = 0;
33 	int err;
34 
35 	if (!ready) {
36 		if (atexit(cleanup))
37 			panic();
38 
39 		err = plat_get_mbedtls_heap(&heap_addr, &heap_size);
40 
41 		/* Ensure heap setup is proper */
42 		if (err < 0) {
43 			ERROR("Mbed TLS failed to get a heap\n");
44 			panic();
45 		}
46 		assert(heap_size >= TF_MBEDTLS_HEAP_SIZE);
47 
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
54 		ready = 1;
55 	}
56 }
57