1 /* 2 * Copyright (c) 2015-2023, 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 #include <mbedtls/version.h> 14 15 #include <common/debug.h> 16 #include <drivers/auth/mbedtls/mbedtls_common.h> 17 18 #include <plat/common/platform.h> 19 cleanup(void)20static void cleanup(void) 21 { 22 ERROR("EXIT from BL2\n"); 23 panic(); 24 } 25 26 /* 27 * mbed TLS initialization function 28 */ mbedtls_init(void)29void mbedtls_init(void) 30 { 31 static int ready; 32 void *heap_addr; 33 size_t heap_size = 0; 34 int err; 35 36 if (!ready) { 37 if (atexit(cleanup)) 38 panic(); 39 40 err = plat_get_mbedtls_heap(&heap_addr, &heap_size); 41 42 /* Ensure heap setup is proper */ 43 if (err < 0) { 44 ERROR("Mbed TLS failed to get a heap\n"); 45 panic(); 46 } 47 assert(heap_size >= TF_MBEDTLS_HEAP_SIZE); 48 49 /* Initialize the mbed TLS heap */ 50 mbedtls_memory_buffer_alloc_init(heap_addr, heap_size); 51 52 #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT 53 mbedtls_platform_set_snprintf(snprintf); 54 #endif 55 ready = 1; 56 } 57 } 58 59 /* 60 * The following helper function simply returns the default allocated heap. 61 * It can be used by platforms for their plat_get_mbedtls_heap() implementation. 62 */ get_mbedtls_heap_helper(void ** heap_addr,size_t * heap_size)63int get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size) 64 { 65 static unsigned char heap[TF_MBEDTLS_HEAP_SIZE]; 66 67 assert(heap_addr != NULL); 68 assert(heap_size != NULL); 69 70 *heap_addr = heap; 71 *heap_size = sizeof(heap); 72 return 0; 73 } 74