Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 1 | /* |
Roberto Vargas | 52f707f | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 2 | * Copyright (c) 2015-2018, 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 | 6b90f5e | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 8 | #include <debug.h> |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 9 | /* mbed TLS headers */ |
| 10 | #include <mbedtls/memory_buffer_alloc.h> |
Antonio Nino Diaz | 6b90f5e | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 11 | #include <mbedtls/platform.h> |
Roberto Vargas | 52f707f | 2018-02-12 12:36:17 +0000 | [diff] [blame] | 12 | #include <mbedtls_common.h> |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 13 | #include <mbedtls_config.h> |
| 14 | #include <platform.h> |
| 15 | #include <stddef.h> |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 16 | |
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 17 | static void cleanup(void) |
| 18 | { |
| 19 | ERROR("EXIT from BL2\n"); |
| 20 | panic(); |
| 21 | } |
| 22 | |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 23 | /* |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 24 | * mbed TLS initialization function |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 25 | */ |
| 26 | void mbedtls_init(void) |
| 27 | { |
| 28 | static int ready; |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 29 | void *heap_addr; |
| 30 | size_t heap_size = 0; |
| 31 | int err; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 32 | |
| 33 | if (!ready) { |
Roberto Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 34 | if (atexit(cleanup)) |
| 35 | panic(); |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 36 | |
| 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 Vargas | 64d4de0 | 2018-05-24 13:34:53 +0100 | [diff] [blame] | 45 | |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 46 | /* Initialize the mbed TLS heap */ |
John Tsichritzis | 30f8964 | 2018-06-07 16:31:34 +0100 | [diff] [blame] | 47 | mbedtls_memory_buffer_alloc_init(heap_addr, heap_size); |
Antonio Nino Diaz | 6b90f5e | 2017-05-19 11:37:22 +0100 | [diff] [blame] | 48 | |
Antonio Nino Diaz | f1481b2 | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 49 | #ifdef MBEDTLS_PLATFORM_SNPRINTF_ALT |
Antonio Nino Diaz | 00086e3 | 2018-08-16 16:46:06 +0100 | [diff] [blame] | 50 | mbedtls_platform_set_snprintf(snprintf); |
Antonio Nino Diaz | f1481b2 | 2017-06-06 10:54:39 +0100 | [diff] [blame] | 51 | #endif |
Juan Castillo | bae6b2a | 2015-11-05 09:24:53 +0000 | [diff] [blame] | 52 | ready = 1; |
Juan Castillo | a57a4d5 | 2015-04-02 15:44:20 +0100 | [diff] [blame] | 53 | } |
| 54 | } |