xref: /rk3399_ARM-atf/drivers/auth/mbedtls/mbedtls_common.c (revision 05ca725465276726cb45f8c97afef6f066238561)
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 <debug.h>
9 /* mbed TLS headers */
10 #include <mbedtls/memory_buffer_alloc.h>
11 #include <mbedtls/platform.h>
12 #include <mbedtls_common.h>
13 #include <mbedtls_config.h>
14 #include <platform.h>
15 #include <stddef.h>
16 
17 static void cleanup(void)
18 {
19 	ERROR("EXIT from BL2\n");
20 	panic();
21 }
22 
23 /*
24  * mbed TLS initialization function
25  */
26 void mbedtls_init(void)
27 {
28 	static int ready;
29 	void *heap_addr;
30 	size_t heap_size = 0;
31 	int err;
32 
33 	if (!ready) {
34 		if (atexit(cleanup))
35 			panic();
36 
37 		err = plat_get_mbedtls_heap(&heap_addr, &heap_size);
38 
39 		/* Ensure heap setup is proper */
40 		if (err < 0) {
41 			ERROR("Mbed TLS failed to get a heap\n");
42 			panic();
43 		}
44 		assert(heap_size >= TF_MBEDTLS_HEAP_SIZE);
45 
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
52 		ready = 1;
53 	}
54 }
55