diff options
author | Tamas Ban <tamas.ban@arm.com> | 2018-01-26 15:45:03 +0000 |
---|---|---|
committer | Marc Moreno Berengue <marc.morenoberengue@arm.com> | 2018-05-17 10:22:37 +0100 |
commit | bd3f75159f5c83a8f64758172d595c8def291c93 (patch) | |
tree | 619c8d7bf927ec74ead9128731bb91b251934e1e | |
parent | c3828850de8ae26af973f9c699c008c1e470f2f2 (diff) | |
download | trusted-firmware-m-bd3f75159f5c83a8f64758172d595c8def291c93.tar.gz |
Boot: remove heap usage from BL2
Mbedtls is initialised with a static memory buffer to
allocate dynamic memory from this buffer rather than
using the heap.
Change-Id: I1369948b2b8ecf78db4bdd4cd2051d1f32d61e7f
Signed-off-by: Tamas Ban <tamas.ban@arm.com>
-rw-r--r-- | bl2/ext/mcuboot/CMakeLists.txt | 1 | ||||
-rw-r--r-- | bl2/ext/mcuboot/bl2_main.c | 11 | ||||
-rw-r--r-- | bl2/ext/mcuboot/bootutil/src/loader.c | 1 | ||||
-rw-r--r-- | bl2/ext/mcuboot/include/config-boot.h | 5 | ||||
-rw-r--r-- | bl2/ext/mcuboot/include/os/os_heap.h | 38 | ||||
-rw-r--r-- | bl2/ext/mcuboot/include/os/os_malloc.h | 42 | ||||
-rw-r--r-- | bl2/ext/mcuboot/os.c | 57 | ||||
-rw-r--r-- | platform/ext/target/mps2/an519/armclang/startup_cmsdk_mps2_an519_bl2.s | 2 | ||||
-rw-r--r-- | platform/ext/target/mps2/an521/armclang/startup_cmsdk_mps2_an521_bl2.s | 2 |
9 files changed, 10 insertions, 149 deletions
diff --git a/bl2/ext/mcuboot/CMakeLists.txt b/bl2/ext/mcuboot/CMakeLists.txt index 12336fd021..e4b9b416f6 100644 --- a/bl2/ext/mcuboot/CMakeLists.txt +++ b/bl2/ext/mcuboot/CMakeLists.txt @@ -59,7 +59,6 @@ endif() #Append all our source files to global lists. list(APPEND ALL_SRC_C "${MCUBOOT_DIR}/bl2_main.c" "${MCUBOOT_DIR}/flash_map.c" - "${MCUBOOT_DIR}/os.c" "${MCUBOOT_DIR}/keys.c" "${MCUBOOT_DIR}/bootutil/src/loader.c" "${MCUBOOT_DIR}/bootutil/src/bootutil_misc.c" diff --git a/bl2/ext/mcuboot/bl2_main.c b/bl2/ext/mcuboot/bl2_main.c index 952e52801e..ef08582dd0 100644 --- a/bl2/ext/mcuboot/bl2_main.c +++ b/bl2/ext/mcuboot/bl2_main.c @@ -21,7 +21,7 @@ #include "cmsis.h" #include "uart_stdout.h" #include "Driver_Flash.h" - +#include "mbedtls/memory_buffer_alloc.h" #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO #include "bootutil/bootutil_log.h" #include "bootutil/image.h" @@ -36,7 +36,9 @@ __asm(" .global __ARM_use_no_argv\n"); /* Flash device name must be specified by target */ extern ARM_DRIVER_FLASH FLASH_DEV_NAME; -void os_heap_init(void); +#define BL2_MBEDTLS_MEM_BUF_LEN 0x2000 +/* Static buffer to be used by mbedtls for memory allocation */ +static uint8_t mbedtls_mem_buf[BL2_MBEDTLS_MEM_BUF_LEN]; struct arm_vector_table { uint32_t msp; @@ -82,7 +84,10 @@ int main(void) BOOT_LOG_INF("Starting bootloader"); - os_heap_init(); + /* Initialise the mbedtls static memory allocator so that mbedtls allocates + * memory from the provided static buffer instead of from the heap. + */ + mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf, BL2_MBEDTLS_MEM_BUF_LEN); /* Initialize Flash driver */ FLASH_DEV_NAME.Initialize(NULL); diff --git a/bl2/ext/mcuboot/bootutil/src/loader.c b/bl2/ext/mcuboot/bootutil/src/loader.c index 29becfa433..30b2eaecd5 100644 --- a/bl2/ext/mcuboot/bootutil/src/loader.c +++ b/bl2/ext/mcuboot/bootutil/src/loader.c @@ -35,7 +35,6 @@ #include <stdlib.h> #include <string.h> #include "flash_map/flash_map.h" -#include <os/os_malloc.h> #include "bootutil/bootutil.h" #include "bootutil/image.h" #include "bootutil_priv.h" diff --git a/bl2/ext/mcuboot/include/config-boot.h b/bl2/ext/mcuboot/include/config-boot.h index 44dbac16e4..b8cb25a924 100644 --- a/bl2/ext/mcuboot/include/config-boot.h +++ b/bl2/ext/mcuboot/include/config-boot.h @@ -37,16 +37,11 @@ #define MBEDTLS_PLATFORM_C #define MBEDTLS_PLATFORM_MEMORY #define MBEDTLS_MEMORY_BUFFER_ALLOC_C -#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS #define MBEDTLS_PLATFORM_EXIT_ALT #define MBEDTLS_NO_PLATFORM_ENTROPY #define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES #define MBEDTLS_PLATFORM_PRINTF_ALT -#if !defined(CONFIG_ARM) -#define MBEDTLS_HAVE_ASM -#endif - #if defined(CONFIG_MBEDTLS_TEST) #define MBEDTLS_SELF_TEST #define MBEDTLS_DEBUG_C diff --git a/bl2/ext/mcuboot/include/os/os_heap.h b/bl2/ext/mcuboot/include/os/os_heap.h deleted file mode 100644 index 4413568e4e..0000000000 --- a/bl2/ext/mcuboot/include/os/os_heap.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef H_OS_HEAP_ -#define H_OS_HEAP_ - -#include <stddef.h> - -#ifdef __cplusplus -extern "C" { -#endif - -void *os_malloc(size_t size); -void os_free(void *mem); -void *os_realloc(void *ptr, size_t size); - -#ifdef __cplusplus -} -#endif - -#endif - diff --git a/bl2/ext/mcuboot/include/os/os_malloc.h b/bl2/ext/mcuboot/include/os/os_malloc.h deleted file mode 100644 index 32b72c2a60..0000000000 --- a/bl2/ext/mcuboot/include/os/os_malloc.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -#ifndef H_OS_MALLOC_ -#define H_OS_MALLOC_ - -#include "os/os_heap.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#undef malloc -#define malloc os_malloc - -#undef free -#define free os_free - -#undef realloc -#define realloc os_realloc - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/bl2/ext/mcuboot/os.c b/bl2/ext/mcuboot/os.c deleted file mode 100644 index 153b335252..0000000000 --- a/bl2/ext/mcuboot/os.c +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - Original code taken from mcuboot project at: - https://github.com/runtimeco/mcuboot - Modifications are Copyright (c) 2018 Arm Limited. - */ - -#include <stdlib.h> -#include <string.h> - -#include "os/os_heap.h" -#include <mbedtls/platform.h> - -/* D(void *os_malloc(size_t size)) */ -void *os_calloc(size_t nelem, size_t size) -{ - /* Note that this doesn't check for overflow. Assume the - * calls only come from within the app. */ - size_t total = nelem * size; - void *buf = malloc(total); - - if (buf) { - memset(buf, 0, total); - } - return buf; -} - -void os_free(void *ptr) -{ - free(ptr); -} - -/* - * Initialize mbedtls to be able to use the local heap. - */ -void os_heap_init(void) -{ - mbedtls_platform_set_calloc_free(os_calloc, os_free); -} diff --git a/platform/ext/target/mps2/an519/armclang/startup_cmsdk_mps2_an519_bl2.s b/platform/ext/target/mps2/an519/armclang/startup_cmsdk_mps2_an519_bl2.s index e80b1a0357..9948f336e4 100644 --- a/platform/ext/target/mps2/an519/armclang/startup_cmsdk_mps2_an519_bl2.s +++ b/platform/ext/target/mps2/an519/armclang/startup_cmsdk_mps2_an519_bl2.s @@ -38,7 +38,7 @@ __initial_sp EQU __initial_msp - MSP_STACK_SIZE ; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8> ; </h> -Heap_Size EQU 0x00010000 +Heap_Size EQU 0x00001000 AREA HEAP, NOINIT, READWRITE, ALIGN=3 __heap_base diff --git a/platform/ext/target/mps2/an521/armclang/startup_cmsdk_mps2_an521_bl2.s b/platform/ext/target/mps2/an521/armclang/startup_cmsdk_mps2_an521_bl2.s index 250a02d5f5..bedeec0079 100644 --- a/platform/ext/target/mps2/an521/armclang/startup_cmsdk_mps2_an521_bl2.s +++ b/platform/ext/target/mps2/an521/armclang/startup_cmsdk_mps2_an521_bl2.s @@ -34,7 +34,7 @@ Stack_Mem SPACE Stack_Size __initial_msp __initial_sp EQU __initial_msp - MSP_STACK_SIZE -Heap_Size EQU 0x00010000 +Heap_Size EQU 0x00001000 AREA HEAP, NOINIT, READWRITE, ALIGN=3 __heap_base |