blob: cdb504295f40bff299f0d556b02f7a7fe93dde69 [file] [log] [blame]
Juan Castilloa57a4d52015-04-02 15:44:20 +01001/*
John Tsichritzis69c88d12019-02-28 11:14:03 +00002 * Copyright (c) 2015-2019, 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>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000013
14#include <common/debug.h>
15#include <drivers/auth/mbedtls/mbedtls_common.h>
16#include <drivers/auth/mbedtls/mbedtls_config.h>
17#include <plat/common/platform.h>
Juan Castilloa57a4d52015-04-02 15:44:20 +010018
John Tsichritzis69c88d12019-02-28 11:14:03 +000019#pragma weak plat_get_mbedtls_heap
20
Roberto Vargas64d4de02018-05-24 13:34:53 +010021static void cleanup(void)
22{
23 ERROR("EXIT from BL2\n");
24 panic();
25}
26
Juan Castilloa57a4d52015-04-02 15:44:20 +010027/*
Juan Castillobae6b2a2015-11-05 09:24:53 +000028 * mbed TLS initialization function
Juan Castilloa57a4d52015-04-02 15:44:20 +010029 */
30void mbedtls_init(void)
31{
32 static int ready;
John Tsichritzis30f89642018-06-07 16:31:34 +010033 void *heap_addr;
34 size_t heap_size = 0;
35 int err;
Juan Castilloa57a4d52015-04-02 15:44:20 +010036
37 if (!ready) {
Roberto Vargas64d4de02018-05-24 13:34:53 +010038 if (atexit(cleanup))
39 panic();
John Tsichritzis30f89642018-06-07 16:31:34 +010040
41 err = plat_get_mbedtls_heap(&heap_addr, &heap_size);
42
43 /* Ensure heap setup is proper */
44 if (err < 0) {
45 ERROR("Mbed TLS failed to get a heap\n");
46 panic();
47 }
48 assert(heap_size >= TF_MBEDTLS_HEAP_SIZE);
Roberto Vargas64d4de02018-05-24 13:34:53 +010049
Juan Castillobae6b2a2015-11-05 09:24:53 +000050 /* Initialize the mbed TLS heap */
John Tsichritzis30f89642018-06-07 16:31:34 +010051 mbedtls_memory_buffer_alloc_init(heap_addr, heap_size);
Antonio Nino Diaz6b90f5e2017-05-19 11:37:22 +010052
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010053#ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT
Antonio Nino Diaz00086e32018-08-16 16:46:06 +010054 mbedtls_platform_set_snprintf(snprintf);
Antonio Nino Diazf1481b22017-06-06 10:54:39 +010055#endif
Juan Castillobae6b2a2015-11-05 09:24:53 +000056 ready = 1;
Juan Castilloa57a4d52015-04-02 15:44:20 +010057 }
58}
John Tsichritzis69c88d12019-02-28 11:14:03 +000059
60/*
61 * The following default implementation of the function simply returns the
62 * by default allocated heap.
63 */
64int plat_get_mbedtls_heap(void **heap_addr, size_t *heap_size)
65{
66 static unsigned char heap[TF_MBEDTLS_HEAP_SIZE];
67
68 assert(heap_addr != NULL);
69 assert(heap_size != NULL);
70
71 *heap_addr = heap;
72 *heap_size = sizeof(heap);
73 return 0;
74}