Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 1 | /* |
John Tsichritzis | 69c88d1 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved. |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 10 | /* mbed TLS headers */ |
| 11 | #include <mbedtls/memory_buffer_alloc.h> |
Antonio Nino Diaz | 6b90f5e | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 12 | #include <mbedtls/platform.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 13 | |
| 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 Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 18 | |
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 19 | static void cleanup(void) |
| 20 | { |
| 21 | ERROR("EXIT from BL2\n"); |
| 22 | panic(); |
| 23 | } |
| 24 | |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 25 | /* |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 26 | * mbed TLS initialization function |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 27 | */ |
| 28 | void mbedtls_init(void) |
| 29 | { |
| 30 | static int ready; |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 31 | void *heap_addr; |
| 32 | size_t heap_size = 0; |
| 33 | int err; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 34 | |
| 35 | if (!ready) { |
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 36 | if (atexit(cleanup)) |
| 37 | panic(); |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 38 | |
| 39 | err = plat_get_mbedtls_heap(&heap_addr, &heap_size); |
| 40 | |
| 41 | /* Ensure heap setup is proper */ |
| 42 | if (err < 0) { |
| 43 | ERROR("Mbed TLS failed to get a heap\n"); |
| 44 | panic(); |
| 45 | } |
| 46 | assert(heap_size >= TF_MBEDTLS_HEAP_SIZE); |
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 47 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 48 | /* Initialize the mbed TLS heap */ |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 49 | mbedtls_memory_buffer_alloc_init(heap_addr, heap_size); |
Antonio Nino Diaz | 6b90f5e | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 50 | |
Antonio Nino Diaz | f1481b2 | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 51 | #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 52 | mbedtls_platform_set_snprintf(snprintf); |
Antonio Nino Diaz | f1481b2 | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 53 | #endif |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 54 | ready = 1; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 55 | } |
| 56 | } |
John Tsichritzis | 69c88d1 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 57 | |
| 58 | /* |
Ambroise Vincent | d207f56 | 2019-04-10 12:50:27 +0100 | [diff] [blame] | 59 | * The following helper function simply returns the default allocated heap. |
| 60 | * It can be used by platforms for their plat_get_mbedtls_heap() implementation. |
John Tsichritzis | 69c88d1 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 61 | */ |
Ambroise Vincent | d207f56 | 2019-04-10 12:50:27 +0100 | [diff] [blame] | 62 | int get_mbedtls_heap_helper(void **heap_addr, size_t *heap_size) |
John Tsichritzis | 69c88d1 | 2019-02-28 11:14:03 +0000 | [diff] [blame] | 63 | { |
| 64 | static unsigned char heap[TF_MBEDTLS_HEAP_SIZE]; |
| 65 | |
| 66 | assert(heap_addr != NULL); |
| 67 | assert(heap_size != NULL); |
| 68 | |
| 69 | *heap_addr = heap; |
| 70 | *heap_size = sizeof(heap); |
| 71 | return 0; |
| 72 | } |