• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015-2019, 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 
cleanup(void)19 static void cleanup(void)
20 {
21 	ERROR("EXIT from BL2\n");
22 	panic();
23 }
24 
25 /*
26  * mbed TLS initialization function
27  */
mbedtls_init(void)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 
58 /*
59  * The following helper function simply returns the default allocated heap.
60  * It can be used by platforms for their plat_get_mbedtls_heap() implementation.
61  */
get_mbedtls_heap_helper(void ** heap_addr,size_t * heap_size)62 int get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size)
63 {
64 	static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
65 
66 	assert(heap_addr != NULL);
67 	assert(heap_size != NULL);
68 
69 	*heap_addr = heap;
70 	*heap_size = sizeof(heap);
71 	return 0;
72 }
73