blob: 39b4b1cb96292c83f8407f39b0b8789ebabbac00 [file] [log] [blame]
Juan Castillo7d37aa12015-04-02 15:44:20 +01001/*
John Tsichritzis17e13352019-02-28 11:14:03 +00002 * Copyright (c) 2015-2019, 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 Diaz09d40e02018-12-14 00:18:21 +00008#include <stddef.h>
9
Juan Castillo649dbf62015-11-05 09:24:53 +000010/* mbed TLS headers */
11#include <mbedtls/memory_buffer_alloc.h>
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010012#include <mbedtls/platform.h>
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000013
14#include <common/debug.h>
15#include <drivers/auth/mbedtls/mbedtls_common.h>
Lucian Paul-Trifu3519afe2022-03-08 15:02:31 +000016#include MBEDTLS_CONFIG_FILE
Antonio Nino Diaz09d40e02018-12-14 00:18:21 +000017#include <plat/common/platform.h>
Juan Castillo7d37aa12015-04-02 15:44:20 +010018
Roberto Vargas6c373342018-05-24 13:34:53 +010019static void cleanup(void)
20{
21 ERROR("EXIT from BL2\n");
22 panic();
23}
24
Juan Castillo7d37aa12015-04-02 15:44:20 +010025/*
Juan Castillo649dbf62015-11-05 09:24:53 +000026 * mbed TLS initialization function
Juan Castillo7d37aa12015-04-02 15:44:20 +010027 */
28void mbedtls_init(void)
29{
30 static int ready;
John Tsichritzis6d01a462018-06-07 16:31:34 +010031 void *heap_addr;
32 size_t heap_size = 0;
33 int err;
Juan Castillo7d37aa12015-04-02 15:44:20 +010034
35 if (!ready) {
Roberto Vargas6c373342018-05-24 13:34:53 +010036 if (atexit(cleanup))
37 panic();
38
Lucian Paul-Trifu3519afe2022-03-08 15:02:31 +000039#if DRTM_SUPPORT && defined(IMAGE_BL31)
40 /*
41 * XXX-LPT: Short-circuit the mbedtls heap linkage for DRTM.
42 * The heap linkage should ideally be integrated with the other sub-
43 * systems that require it (e.g. trusted board boot).
44 */
45 err = get_mbedtls_heap_helper(&heap_addr, &heap_size);
46#else
John Tsichritzis6d01a462018-06-07 16:31:34 +010047 err = plat_get_mbedtls_heap(&heap_addr, &heap_size);
Lucian Paul-Trifu3519afe2022-03-08 15:02:31 +000048#endif
John Tsichritzis6d01a462018-06-07 16:31:34 +010049
50 /* Ensure heap setup is proper */
51 if (err < 0) {
52 ERROR("Mbed TLS failed to get a heap\n");
53 panic();
54 }
55 assert(heap_size >= TF_MBEDTLS_HEAP_SIZE);
56
Juan Castillo649dbf62015-11-05 09:24:53 +000057 /* Initialize the mbed TLS heap */
John Tsichritzis6d01a462018-06-07 16:31:34 +010058 mbedtls_memory_buffer_alloc_init(heap_addr, heap_size);
Antonio Nino Diazab1794f2017-05-19 11:37:22 +010059
Antonio Nino Diazc46c18c2017-06-06 10:54:39 +010060#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
Antonio Nino Diaz39b6cc62018-08-16 16:46:06 +010061 mbedtls_platform_set_snprintf(snprintf);
Antonio Nino Diazc46c18c2017-06-06 10:54:39 +010062#endif
Juan Castillo649dbf62015-11-05 09:24:53 +000063 ready = 1;
Juan Castillo7d37aa12015-04-02 15:44:20 +010064 }
65}
John Tsichritzis17e13352019-02-28 11:14:03 +000066
67/*
Ambroise Vincent2374ab12019-04-10 12:50:27 +010068 * The following helper function simply returns the default allocated heap.
69 * It can be used by platforms for their plat_get_mbedtls_heap() implementation.
John Tsichritzis17e13352019-02-28 11:14:03 +000070 */
Ambroise Vincent2374ab12019-04-10 12:50:27 +010071int get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size)
John Tsichritzis17e13352019-02-28 11:14:03 +000072{
73 static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
74
75 assert(heap_addr != NULL);
76 assert(heap_size != NULL);
77
78 *heap_addr = heap;
79 *heap_size = sizeof(heap);
80 return 0;
81}