From 6c3733456706809d5c9fb78a9746bf2fa484fb91 Mon Sep 17 00:00:00 2001 From: Roberto Vargas Date: Thu, 24 May 2018 13:34:53 +0100 Subject: [PATCH] Add atexit function to libc We had exit but we didn't have atexit, and we were calling panic and tf_printf from exit, which generated a dependency from exit to them. Having atexit allows to set a different function pointer in every image. Change-Id: I95b9556d680d96249ed3b14da159b6f417da7661 Signed-off-by: Roberto Vargas --- bl2u/bl2u_main.c | 1 + drivers/auth/mbedtls/mbedtls_common.c | 10 ++++++++++ lib/libc/exit.c | 20 ++++++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/bl2u/bl2u_main.c b/bl2u/bl2u_main.c index a7e3fb916..b29d57e9d 100644 --- a/bl2u/bl2u_main.c +++ b/bl2u/bl2u_main.c @@ -17,6 +17,7 @@ #include #include + /******************************************************************************* * This function is responsible to: * Load SCP_BL2U if platform has defined SCP_BL2U_BASE diff --git a/drivers/auth/mbedtls/mbedtls_common.c b/drivers/auth/mbedtls/mbedtls_common.c index c048d005a..64dc1967d 100644 --- a/drivers/auth/mbedtls/mbedtls_common.c +++ b/drivers/auth/mbedtls/mbedtls_common.c @@ -5,6 +5,7 @@ */ #include +#include /* mbed TLS headers */ #include @@ -23,6 +24,12 @@ #endif static unsigned char heap[MBEDTLS_HEAP_SIZE]; +static void cleanup(void) +{ + ERROR("EXIT from BL2\n"); + panic(); +} + /* * mbed TLS initialization function */ @@ -31,6 +38,9 @@ void mbedtls_init(void) static int ready; if (!ready) { + if (atexit(cleanup)) + panic(); + /* Initialize the mbed TLS heap */ mbedtls_memory_buffer_alloc_init(heap, MBEDTLS_HEAP_SIZE); diff --git a/lib/libc/exit.c b/lib/libc/exit.c index afc3f9343..b2fde9ca2 100644 --- a/lib/libc/exit.c +++ b/lib/libc/exit.c @@ -4,11 +4,23 @@ * SPDX-License-Identifier: BSD-3-Clause */ -#include #include -void exit(int v) +static void (*exitfun)(void); + +void exit(int status) { - ERROR("EXIT\n"); - panic(); + if (exitfun) + (*exitfun)(); + for (;;) + ; +} + +int atexit(void (*fun)(void)) +{ + if (exitfun) + return -1; + exitfun = fun; + + return 0; }