17d37aa17SJuan Castillo /* 2*a8eadc51SGovindraj Raja * Copyright (c) 2015-2023, Arm Limited and Contributors. All rights reserved. 37d37aa17SJuan Castillo * 482cb2c1aSdp-arm * SPDX-License-Identifier: BSD-3-Clause 57d37aa17SJuan Castillo */ 67d37aa17SJuan Castillo 76d01a463SJohn Tsichritzis #include <assert.h> 809d40e0eSAntonio Nino Diaz #include <stddef.h> 909d40e0eSAntonio Nino Diaz 107d37aa17SJuan Castillo /* mbed TLS headers */ 11649dbf6fSJuan Castillo #include <mbedtls/memory_buffer_alloc.h> 12ab1794f5SAntonio Nino Diaz #include <mbedtls/platform.h> 13*a8eadc51SGovindraj Raja #include <mbedtls/version.h> 1409d40e0eSAntonio Nino Diaz 1509d40e0eSAntonio Nino Diaz #include <common/debug.h> 1609d40e0eSAntonio Nino Diaz #include <drivers/auth/mbedtls/mbedtls_common.h> 17*a8eadc51SGovindraj Raja 1809d40e0eSAntonio Nino Diaz #include <plat/common/platform.h> 197d37aa17SJuan Castillo cleanup(void)206c373345SRoberto Vargasstatic void cleanup(void) 216c373345SRoberto Vargas { 226c373345SRoberto Vargas ERROR("EXIT from BL2\n"); 236c373345SRoberto Vargas panic(); 246c373345SRoberto Vargas } 256c373345SRoberto Vargas 267d37aa17SJuan Castillo /* 277d37aa17SJuan Castillo * mbed TLS initialization function 287d37aa17SJuan Castillo */ mbedtls_init(void)297d37aa17SJuan Castillovoid mbedtls_init(void) 307d37aa17SJuan Castillo { 317d37aa17SJuan Castillo static int ready; 326d01a463SJohn Tsichritzis void *heap_addr; 336d01a463SJohn Tsichritzis size_t heap_size = 0; 346d01a463SJohn Tsichritzis int err; 357d37aa17SJuan Castillo 367d37aa17SJuan Castillo if (!ready) { 376c373345SRoberto Vargas if (atexit(cleanup)) 386c373345SRoberto Vargas panic(); 396c373345SRoberto Vargas 406d01a463SJohn Tsichritzis err = plat_get_mbedtls_heap(&heap_addr, &heap_size); 416d01a463SJohn Tsichritzis 426d01a463SJohn Tsichritzis /* Ensure heap setup is proper */ 436d01a463SJohn Tsichritzis if (err < 0) { 446d01a463SJohn Tsichritzis ERROR("Mbed TLS failed to get a heap\n"); 456d01a463SJohn Tsichritzis panic(); 466d01a463SJohn Tsichritzis } 476d01a463SJohn Tsichritzis assert(heap_size >= TF_MBEDTLS_HEAP_SIZE); 486d01a463SJohn Tsichritzis 497d37aa17SJuan Castillo /* Initialize the mbed TLS heap */ 506d01a463SJohn Tsichritzis mbedtls_memory_buffer_alloc_init(heap_addr, heap_size); 51ab1794f5SAntonio Nino Diaz 52c46c18c5SAntonio Nino Diaz #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT 5339b6cc66SAntonio Nino Diaz mbedtls_platform_set_snprintf(snprintf); 54c46c18c5SAntonio Nino Diaz #endif 557d37aa17SJuan Castillo ready = 1; 567d37aa17SJuan Castillo } 577d37aa17SJuan Castillo } 5817e1335cSJohn Tsichritzis 5917e1335cSJohn Tsichritzis /* 602374ab17SAmbroise Vincent * The following helper function simply returns the default allocated heap. 612374ab17SAmbroise Vincent * It can be used by platforms for their plat_get_mbedtls_heap() implementation. 6217e1335cSJohn Tsichritzis */ get_mbedtls_heap_helper(void ** heap_addr,size_t * heap_size)632374ab17SAmbroise Vincentint get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size) 6417e1335cSJohn Tsichritzis { 6517e1335cSJohn Tsichritzis static unsigned char heap[TF_MBEDTLS_HEAP_SIZE]; 6617e1335cSJohn Tsichritzis 6717e1335cSJohn Tsichritzis assert(heap_addr != NULL); 6817e1335cSJohn Tsichritzis assert(heap_size != NULL); 6917e1335cSJohn Tsichritzis 7017e1335cSJohn Tsichritzis *heap_addr = heap; 7117e1335cSJohn Tsichritzis *heap_size = sizeof(heap); 7217e1335cSJohn Tsichritzis return 0; 7317e1335cSJohn Tsichritzis } 74