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