blob: 4f30d82773ce4dc60c5d721afe955ef72b1ca8c1 [file] [log] [blame]
Juan Castilloa57a4d52015-04-02 15:44:20 +01001/*
Govindraj Raja9c7dfb02023-01-11 18:34:58 +00002 * Copyright (c) 2015-2023, 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 Diaze0f90632018-12-14 00:18:21 +00008#include <stddef.h>
9
Juan Castillobae6b2a2015-11-05 09:24:53 +000010/* mbed TLS headers */
11#include <mbedtls/memory_buffer_alloc.h>
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010012#include <mbedtls/platform.h>
Govindraj Raja9c7dfb02023-01-11 18:34:58 +000013#include <mbedtls/version.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000014
15#include <common/debug.h>
16#include <drivers/auth/mbedtls/mbedtls_common.h>
Govindraj Raja9c7dfb02023-01-11 18:34:58 +000017
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000018#include <plat/common/platform.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +010019
Roberto Vargas64d4de02018-05-24 13:34:53 +010020static void cleanup(void)
21{
22 ERROR("EXIT from BL2\n");
23 panic();
24}
25
Juan Castilloa57a4d52015-04-02 15:44:20 +010026/*
Juan Castillobae6b2a2015-11-05 09:24:53 +000027 * mbed TLS initialization function
Juan Castilloa57a4d52015-04-02 15:44:20 +010028 */
29void mbedtls_init(void)
30{
31 static int ready;
John Tsichritzis30f89642018-06-07 16:31:34 +010032 void *heap_addr;
33 size_t heap_size = 0;
34 int err;
Juan Castilloa57a4d52015-04-02 15:44:20 +010035
36 if (!ready) {
Roberto Vargas64d4de02018-05-24 13:34:53 +010037 if (atexit(cleanup))
38 panic();
John Tsichritzis30f89642018-06-07 16:31:34 +010039
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);
Roberto Vargas64d4de02018-05-24 13:34:53 +010048
Juan Castillobae6b2a2015-11-05 09:24:53 +000049 /* Initialize the mbed TLS heap */
John Tsichritzis30f89642018-06-07 16:31:34 +010050 mbedtls_memory_buffer_alloc_init(heap_addr, heap_size);
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010051
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010052#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010053 mbedtls_platform_set_snprintf(snprintf);
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010054#endif
Juan Castillobae6b2a2015-11-05 09:24:53 +000055 ready = 1;
Juan Castilloa57a4d52015-04-02 15:44:20 +010056 }
57}
John Tsichritzis69c88d12019-02-28 11:14:03 +000058
59/*
Ambroise Vincentd207f562019-04-10 12:50:27 +010060 * The following helper function simply returns the default allocated heap.
61 * It can be used by platforms for their plat_get_mbedtls_heap() implementation.
John Tsichritzis69c88d12019-02-28 11:14:03 +000062 */
Ambroise Vincentd207f562019-04-10 12:50:27 +010063int get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size)
John Tsichritzis69c88d12019-02-28 11:14:03 +000064{
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}