blob: dbf45baebd12bc216f3ea069d115590392f5fe17 [file] [log] [blame]
Juan Castillo7d37aa12015-04-02 15:44:20 +01001/*
Roberto Vargas3b941892018-02-12 12:36:17 +00002 * Copyright (c) 2015-2018, ARM Limited and Contributors. All rights reserved.
Juan Castillo7d37aa12015-04-02 15:44:20 +01003 *
dp-arm82cb2c12017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
Juan Castillo7d37aa12015-04-02 15:44:20 +01005 */
6
John Tsichritzis6d01a462018-06-07 16:31:34 +01007#include <assert.h>
Antonio Nino Diazab1794f2017-05-19 11:37:22 +01008#include <debug.h>
Juan Castillo649dbf62015-11-05 09:24:53 +00009/* mbed TLS headers */
10#include <mbedtls/memory_buffer_alloc.h>
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010011#include <mbedtls/platform.h>
Roberto Vargas3b941892018-02-12 12:36:17 +000012#include <mbedtls_common.h>
John Tsichritzis6d01a462018-06-07 16:31:34 +010013#include <mbedtls_config.h>
14#include <platform.h>
15#include <stddef.h>
Juan Castillo7d37aa12015-04-02 15:44:20 +010016
Roberto Vargas6c373342018-05-24 13:34:53 +010017static void cleanup(void)
18{
19 ERROR("EXIT from BL2\n");
20 panic();
21}
22
Juan Castillo7d37aa12015-04-02 15:44:20 +010023/*
Juan Castillo649dbf62015-11-05 09:24:53 +000024 * mbed TLS initialization function
Juan Castillo7d37aa12015-04-02 15:44:20 +010025 */
26void mbedtls_init(void)
27{
28 static int ready;
John Tsichritzis6d01a462018-06-07 16:31:34 +010029 void *heap_addr;
30 size_t heap_size = 0;
31 int err;
Juan Castillo7d37aa12015-04-02 15:44:20 +010032
33 if (!ready) {
Roberto Vargas6c373342018-05-24 13:34:53 +010034 if (atexit(cleanup))
35 panic();
36
John Tsichritzis6d01a462018-06-07 16:31:34 +010037 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
Juan Castillo649dbf62015-11-05 09:24:53 +000046 /* Initialize the mbed TLS heap */
John Tsichritzis6d01a462018-06-07 16:31:34 +010047 mbedtls_memory_buffer_alloc_init(heap_addr, heap_size);
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010048
Antonio Nino Diazc46c18c2017-06-06 10:54:39 +010049#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
Antonio Nino Diaz39b6cc62018-08-16 16:46:06 +010050 mbedtls_platform_set_snprintf(snprintf);
Antonio Nino Diazc46c18c2017-06-06 10:54:39 +010051#endif
Juan Castillo649dbf62015-11-05 09:24:53 +000052 ready = 1;
Juan Castillo7d37aa12015-04-02 15:44:20 +010053 }
54}