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 <debug.h> 8 #include <stdlib.h> 9 10 /* mbed TLS headers */ 11 #include <mbedtls/memory_buffer_alloc.h> 12 #include <mbedtls/platform.h> 13 #include <mbedtls_config.h> 14 #include <mbedtls_common.h> 15 16 /* 17 * mbed TLS heap 18 */ 19 #if (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_ECDSA) \ 20 || (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA_AND_ECDSA) 21 #define MBEDTLS_HEAP_SIZE (13*1024) 22 #elif (TF_MBEDTLS_KEY_ALG_ID == TF_MBEDTLS_RSA) 23 #define MBEDTLS_HEAP_SIZE (7*1024) 24 #endif 25 static unsigned char heap[MBEDTLS_HEAP_SIZE]; 26 27 static void cleanup(void) 28 { 29 ERROR("EXIT from BL2\n"); 30 panic(); 31 } 32 33 /* 34 * mbed TLS initialization function 35 */ 36 void mbedtls_init(void) 37 { 38 static int ready; 39 40 if (!ready) { 41 if (atexit(cleanup)) 42 panic(); 43 44 /* Initialize the mbed TLS heap */ 45 mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE); 46 47 #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT 48 /* Use reduced version of snprintf to save space. */ 49 mbedtls_platform_set_snprintf(tf_snprintf); 50 #endif 51 52 ready = 1; 53 } 54 } 55