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>
diff --git a/bl2/ext/mcuboot/CMakeLists.txt b/bl2/ext/mcuboot/CMakeLists.txt
index 12336fd..e4b9b41 100644
--- a/bl2/ext/mcuboot/CMakeLists.txt
+++ b/bl2/ext/mcuboot/CMakeLists.txt
@@ -59,7 +59,6 @@
 #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 952e528..ef08582 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 @@
 /* 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 @@
 
     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 29becfa..30b2eae 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 44dbac1..b8cb25a 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 4413568..0000000
--- 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 32b72c2..0000000
--- 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 153b335..0000000
--- 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);
-}