Platform: Remove unused heap

- Set mbedtls_print to tfm_sp_log_printf which does not need heap.
- After that in case of no debug information, heap is not needed.
  Remove heap in link scripts.
- In case of with debug information, heap is used by GNU c lib
  functions like assert, vprintf and so on. Reserve 0x200 bytes
  heap for that.

Signed-off-by: Sherry Zhang <sherry.zhang2@arm.com>
Change-Id: I6874d9a9b4aff5fcc80ad7a14f225cb431dcb363
diff --git a/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_default.h b/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_default.h
index 732c3d8..5697b57 100644
--- a/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_default.h
+++ b/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_default.h
@@ -78,7 +78,7 @@
  *
  * Comment if your system does not support time functions
  */
-#define MBEDTLS_HAVE_TIME
+//#define MBEDTLS_HAVE_TIME
 
 /**
  * \def MBEDTLS_HAVE_TIME_DATE
@@ -99,7 +99,7 @@
  * mbedtls_platform_gmtime_r() at compile-time by using the macro
  * MBEDTLS_PLATFORM_GMTIME_R_ALT.
  */
-#define MBEDTLS_HAVE_TIME_DATE
+//#define MBEDTLS_HAVE_TIME_DATE
 
 /**
  * \def MBEDTLS_PLATFORM_MEMORY
@@ -407,6 +407,16 @@
  */
 #define MBEDTLS_PLATFORM_C
 
+#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
+#define MBEDTLS_PLATFORM_STD_MEM_HDR   <stdlib.h>
+
+#include <stdio.h>
+
+#define MBEDTLS_PLATFORM_SNPRINTF_MACRO      snprintf
+#define MBEDTLS_PLATFORM_PRINTF_ALT
+#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS  EXIT_SUCCESS
+#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE  EXIT_FAILURE
+
 /**
  * \def MBEDTLS_PSA_CRYPTO_C
  *
diff --git a/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_large.h b/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_large.h
index 3ff1815..506b6c5 100644
--- a/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_large.h
+++ b/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_large.h
@@ -408,6 +408,16 @@
  */
 #define MBEDTLS_PLATFORM_C
 
+#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
+#define MBEDTLS_PLATFORM_STD_MEM_HDR   <stdlib.h>
+
+#include <stdio.h>
+
+#define MBEDTLS_PLATFORM_SNPRINTF_MACRO      snprintf
+#define MBEDTLS_PLATFORM_PRINTF_ALT
+#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS  EXIT_SUCCESS
+#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE  EXIT_FAILURE
+
 /**
  * \def MBEDTLS_PSA_CRYPTO_C
  *
diff --git a/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_medium.h b/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_medium.h
index 245e94b..7c65247 100644
--- a/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_medium.h
+++ b/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_medium.h
@@ -478,6 +478,16 @@
  */
 #define MBEDTLS_PLATFORM_C
 
+#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
+#define MBEDTLS_PLATFORM_STD_MEM_HDR   <stdlib.h>
+
+#include <stdio.h>
+
+#define MBEDTLS_PLATFORM_SNPRINTF_MACRO      snprintf
+#define MBEDTLS_PLATFORM_PRINTF_ALT
+#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS  EXIT_SUCCESS
+#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE  EXIT_FAILURE
+
 /**
  * \def MBEDTLS_PSA_CRYPTO_C
  *
diff --git a/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_small.h b/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_small.h
index 85bf5e0..609abf5 100644
--- a/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_small.h
+++ b/lib/ext/mbedcrypto/mbedcrypto_config/tfm_mbedcrypto_config_profile_small.h
@@ -440,6 +440,16 @@
  */
 #define MBEDTLS_PLATFORM_C
 
+#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
+#define MBEDTLS_PLATFORM_STD_MEM_HDR   <stdlib.h>
+
+#include <stdio.h>
+
+#define MBEDTLS_PLATFORM_SNPRINTF_MACRO      snprintf
+#define MBEDTLS_PLATFORM_PRINTF_ALT
+#define MBEDTLS_PLATFORM_STD_EXIT_SUCCESS  EXIT_SUCCESS
+#define MBEDTLS_PLATFORM_STD_EXIT_FAILURE  EXIT_FAILURE
+
 /**
  * \def MBEDTLS_PSA_CRYPTO_C
  *
diff --git a/platform/CMakeLists.txt b/platform/CMakeLists.txt
index ccc87d5..5221b6f 100755
--- a/platform/CMakeLists.txt
+++ b/platform/CMakeLists.txt
@@ -247,4 +247,5 @@
         $<$<BOOL:${TEST_PSA_API}>:PSA_API_TEST_${TEST_PSA_API}>
         $<$<BOOL:${FORWARD_PROT_MSG}>:FORWARD_PROT_MSG=${FORWARD_PROT_MSG}>
         $<$<BOOL:${TFM_CODE_SHARING}>:CODE_SHARING>
+        $<$<OR:$<CONFIG:Debug>,$<CONFIG:relwithdebinfo>>:ENABLE_HEAP>
 )
diff --git a/platform/ext/common/armclang/tfm_common_s.sct b/platform/ext/common/armclang/tfm_common_s.sct
index 0044bf5..405a251 100644
--- a/platform/ext/common/armclang/tfm_common_s.sct
+++ b/platform/ext/common/armclang/tfm_common_s.sct
@@ -210,9 +210,6 @@
     }
 #endif
 
-    ARM_LIB_HEAP +0 ALIGN 8 EMPTY S_HEAP_SIZE {
-    }
-
     ER_TFM_DATA +0 {
         * (+RW +ZI)
     }
diff --git a/platform/ext/common/armclang/tfm_isolation_l3.sct.template b/platform/ext/common/armclang/tfm_isolation_l3.sct.template
index d054d5d..d147d0c 100644
--- a/platform/ext/common/armclang/tfm_isolation_l3.sct.template
+++ b/platform/ext/common/armclang/tfm_isolation_l3.sct.template
@@ -144,9 +144,6 @@
     ARM_LIB_STACK +0 ALIGN 32 OVERLAY EMPTY S_MSP_STACK_SIZE {
     }
 
-    ARM_LIB_HEAP +0 ALIGN 8 EMPTY S_HEAP_SIZE {
-    }
-
     ER_TFM_DATA +0 {
         * (+RW +ZI)
     }
diff --git a/platform/ext/common/gcc/tfm_common_s.ld b/platform/ext/common/gcc/tfm_common_s.ld
index b24ad7b..f423a82 100644
--- a/platform/ext/common/gcc/tfm_common_s.ld
+++ b/platform/ext/common/gcc/tfm_common_s.ld
@@ -36,7 +36,6 @@
 
 }
 
-__heap_size__  = S_HEAP_SIZE;
 #if !defined(TFM_PSA_API)
 __psp_stack_size__ = S_PSP_STACK_SIZE;
 #endif
@@ -299,7 +298,8 @@
     Image$$TFM_SECURE_STACK$$ZI$$Limit = ADDR(.TFM_SECURE_STACK) + SIZEOF(.TFM_SECURE_STACK);
 #endif /* !defined(TFM_PSA_API) */
 
-#if (TFM_LVL == 1)
+#if defined(ENABLE_HEAP)
+    __heap_size__ = S_HEAP_SIZE;
     .heap : ALIGN(8)
     {
         __end__ = .;
@@ -309,7 +309,7 @@
         __HeapLimit = .;
         __heap_limit = .; /* Add for _sbrk */
     } > RAM
-#endif /* TFM_LVL == 1 */
+#endif
 
 #if defined(CONFIG_TFM_PARTITION_META)
     .TFM_SP_META_PTR : ALIGN(32)
@@ -348,49 +348,6 @@
     /**** APPLICATION RoT DATA end here */
     Image$$TFM_APP_RW_STACK_END$$Base = .;
 
-#if TFM_LVL != 1
-#ifdef TFM_PARTITION_TEST_SECURE_SERVICES
-    .TFM_SP_SECURE_TEST_PARTITION_LINKER_DATA : ALIGN(32)
-    {
-        *libc_nano*:*(.data*)
-        . = ALIGN(32);
-    } > RAM AT> FLASH
-
-    .TFM_SP_SECURE_TEST_PARTITION_LINKER_BSS : ALIGN(32)
-    {
-        /* FixMe:
-         * Test framework use printf to print out test result. Implementation of
-         * printf in GCC libc use static data and heap as well. To be able to
-         * execute test suites with TFM_LVL=3 this workaround is needed to
-         * allocate libc static data and heap within the data section of secure
-         * test partition. This can be removed if test service will be executed
-         * in privileged mode.
-         */
-        *libc_nano*:*(.bss*)
-        *libc_nano*:*(COMMON)
-
-        __end__ = .;
-        PROVIDE(end = .);
-        __HeapBase = .;
-        . += __heap_size__;
-        __HeapLimit = .;
-        __heap_limit = .; /* Add for _sbrk */
-
-        . = ALIGN(32);
-    } > RAM AT> RAM
-#else /* TFM_PARTITION_TEST_SECURE_SERVICES */
-    .heap : ALIGN(8)
-    {
-        __end__ = .;
-        PROVIDE(end = .);
-        __HeapBase = .;
-        . += __heap_size__;
-        __HeapLimit = .;
-        __heap_limit = .; /* Add for _sbrk */
-    } > RAM AT> RAM
-#endif /* TFM_PARTITION_TEST_SECURE_SERVICES */
-#endif /* TFM_LVL != 1 */
-
 #ifdef TFM_MULTI_CORE_TOPOLOGY
 #ifdef S_DATA_PRIV_START
     /**** Privileged data area base address specified by multi-core platform */
diff --git a/platform/ext/common/gcc/tfm_isolation_l3.ld.template b/platform/ext/common/gcc/tfm_isolation_l3.ld.template
index e9ca778..6c15057 100644
--- a/platform/ext/common/gcc/tfm_isolation_l3.ld.template
+++ b/platform/ext/common/gcc/tfm_isolation_l3.ld.template
@@ -33,7 +33,6 @@
 #endif
 }
 
-__heap_size__  = S_HEAP_SIZE;
 __msp_init_stack_size__ = S_MSP_STACK_SIZE_INIT;
 
 /* Library configurations */
@@ -275,6 +274,19 @@
     Image$$ARM_LIB_STACK$$ZI$$Base = ADDR(.msp_stack);
     Image$$ARM_LIB_STACK$$ZI$$Limit = ADDR(.msp_stack) + SIZEOF(.msp_stack);
 
+#if defined(ENABLE_HEAP)
+    __heap_size__ = S_HEAP_SIZE;
+    .heap : ALIGN(8)
+    {
+        __end__ = .;
+        PROVIDE(end = .);
+        __HeapBase = .;
+        . += __heap_size__;
+        __HeapLimit = .;
+        __heap_limit = .; /* Add for _sbrk */
+    } > RAM
+#endif
+
     /**** PSA RoT DATA start here */
 {% for partition in partitions %}
     {% if partition.manifest.type == 'PSA-ROT' %}
@@ -394,47 +406,6 @@
     . = ALIGN(32);
     Image$$PT_APP_RWZI_END$$Base = .;
 
-#ifdef TFM_PARTITION_TEST_SECURE_SERVICES
-    .TFM_SP_SECURE_TEST_PARTITION_LINKER_DATA : ALIGN(32)
-    {
-        *libc_nano*:*(.data*)
-        . = ALIGN(32);
-    } > RAM AT> FLASH
-
-    .TFM_SP_SECURE_TEST_PARTITION_LINKER_BSS : ALIGN(32)
-    {
-        /* FixMe:
-         * Test framework use printf to print out test result. Implementation of
-         * printf in GCC libc use static data and heap as well. To be able to
-         * execute test suites with TFM_LVL=3 this workaround is needed to
-         * allocate libc static data and heap within the data section of secure
-         * test partition. This can be removed if test service will be executed
-         * in privileged mode.
-         */
-        *libc_nano*:*(.bss*)
-        *libc_nano*:*(COMMON)
-
-        __end__ = .;
-        PROVIDE(end = .);
-        __HeapBase = .;
-        . += __heap_size__;
-        __HeapLimit = .;
-        __heap_limit = .; /* Add for _sbrk */
-
-        . = ALIGN(32);
-    } > RAM AT> RAM
-#else /* TFM_PARTITION_TEST_SECURE_SERVICES */
-    .heap : ALIGN(8)
-    {
-        __end__ = .;
-        PROVIDE(end = .);
-        __HeapBase = .;
-        . += __heap_size__;
-        __HeapLimit = .;
-        __heap_limit = .; /* Add for _sbrk */
-    } > RAM AT> RAM
-#endif /* TFM_PARTITION_TEST_SECURE_SERVICES */
-
     .TFM_DATA : ALIGN(4)
     {
         *(.data*)
diff --git a/platform/ext/common/iar/tfm_common_s.icf b/platform/ext/common/iar/tfm_common_s.icf
index 20657fb..cee8e22 100644
--- a/platform/ext/common/iar/tfm_common_s.icf
+++ b/platform/ext/common/iar/tfm_common_s.icf
@@ -176,9 +176,11 @@
 keep {block TFM_SHARED_DATA, block ARM_LIB_STACK};
 #endif
 
-define block HEAP         with alignment = 8, size = S_HEAP_SIZE { };
-define block ARM_LIB_HEAP {block HEAP};
-keep {block HEAP, block ARM_LIB_HEAP};
+#if defined(ENABLE_HEAP)
+    define block HEAP         with alignment = 8, size = S_HEAP_SIZE { };
+    define block ARM_LIB_HEAP {block HEAP};
+    keep {block HEAP, block ARM_LIB_HEAP};
+#endif
 
 define block ER_TFM_DATA          with alignment = 8 {readwrite};
 
@@ -351,7 +353,9 @@
     overlay STACK_DATA,
 #endif
 
+#if defined(ENABLE_HEAP)
     block ARM_LIB_HEAP,
+#endif
 
     block ER_TFM_DATA,
 
diff --git a/platform/ext/common/iar/tfm_isolation_l3.icf.template b/platform/ext/common/iar/tfm_isolation_l3.icf.template
index 37055fb..ed69e5f 100644
--- a/platform/ext/common/iar/tfm_isolation_l3.icf.template
+++ b/platform/ext/common/iar/tfm_isolation_l3.icf.template
@@ -119,9 +119,11 @@
 define block TFM_SHARED_DATA with alignment = 32, size = BOOT_TFM_SHARED_DATA_SIZE { };
 define block ARM_LIB_STACK with alignment = 32, size = S_MSP_STACK_SIZE { };
 
-define block HEAP         with alignment = 8, size = S_HEAP_SIZE { };
-define block ARM_LIB_HEAP {block HEAP};
-keep {block HEAP, block ARM_LIB_HEAP};
+#if defined(ENABLE_HEAP)
+    define block HEAP         with alignment = 8, size = S_HEAP_SIZE { };
+    define block ARM_LIB_HEAP {block HEAP};
+    keep {block HEAP, block ARM_LIB_HEAP};
+#endif
 
 define block ER_TFM_DATA          with alignment = 8 {readwrite};
 
@@ -257,7 +259,9 @@
     block PT_PRIV_RWZI_START,
     block TFM_SHARED_DATA,
     block ARM_LIB_STACK,
+#if defined(ENABLE_HEAP)
     block ARM_LIB_HEAP,
+#endif
     block ER_TFM_DATA,
     block ER_PART_RT_POOL,
     block ER_SERV_RT_POOL,
diff --git a/platform/ext/target/arm/corstone1000/partition/region_defs.h b/platform/ext/target/arm/corstone1000/partition/region_defs.h
index 9399411..11a52f0 100644
--- a/platform/ext/target/arm/corstone1000/partition/region_defs.h
+++ b/platform/ext/target/arm/corstone1000/partition/region_defs.h
@@ -26,7 +26,10 @@
 #define BL1_HEAP_SIZE           (0x0001000)
 #define BL1_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0000200)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 
diff --git a/platform/ext/target/arm/mps2/an519/partition/region_defs.h b/platform/ext/target/arm/mps2/an519/partition/region_defs.h
index b084ab7..fd4ea7f 100755
--- a/platform/ext/target/arm/mps2/an519/partition/region_defs.h
+++ b/platform/ext/target/arm/mps2/an519/partition/region_defs.h
@@ -22,7 +22,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/arm/mps2/an521/partition/region_defs.h b/platform/ext/target/arm/mps2/an521/partition/region_defs.h
index 34858a6..afadb66 100755
--- a/platform/ext/target/arm/mps2/an521/partition/region_defs.h
+++ b/platform/ext/target/arm/mps2/an521/partition/region_defs.h
@@ -22,7 +22,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/arm/mps3/an524/partition/region_defs.h b/platform/ext/target/arm/mps3/an524/partition/region_defs.h
index a43c3ba..64b0b95 100755
--- a/platform/ext/target/arm/mps3/an524/partition/region_defs.h
+++ b/platform/ext/target/arm/mps3/an524/partition/region_defs.h
@@ -22,7 +22,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/arm/mps3/an547/partition/region_defs.h b/platform/ext/target/arm/mps3/an547/partition/region_defs.h
index 0732bf3..630bf45 100755
--- a/platform/ext/target/arm/mps3/an547/partition/region_defs.h
+++ b/platform/ext/target/arm/mps3/an547/partition/region_defs.h
@@ -23,7 +23,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/arm/mps3/an552/partition/region_defs.h b/platform/ext/target/arm/mps3/an552/partition/region_defs.h
index 0732bf3..630bf45 100755
--- a/platform/ext/target/arm/mps3/an552/partition/region_defs.h
+++ b/platform/ext/target/arm/mps3/an552/partition/region_defs.h
@@ -23,7 +23,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/arm/mps3/corstone310_fvp/partition/region_defs.h b/platform/ext/target/arm/mps3/corstone310_fvp/partition/region_defs.h
index eadb99c..e00cbfb 100644
--- a/platform/ext/target/arm/mps3/corstone310_fvp/partition/region_defs.h
+++ b/platform/ext/target/arm/mps3/corstone310_fvp/partition/region_defs.h
@@ -23,7 +23,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/arm/musca_b1/sse_200/partition/region_defs.h b/platform/ext/target/arm/musca_b1/sse_200/partition/region_defs.h
index fc9eb20..81e5e22 100755
--- a/platform/ext/target/arm/musca_b1/sse_200/partition/region_defs.h
+++ b/platform/ext/target/arm/musca_b1/sse_200/partition/region_defs.h
@@ -22,7 +22,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+#define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/arm/musca_s1/partition/region_defs.h b/platform/ext/target/arm/musca_s1/partition/region_defs.h
index 4425187..efa0800 100755
--- a/platform/ext/target/arm/musca_s1/partition/region_defs.h
+++ b/platform/ext/target/arm/musca_s1/partition/region_defs.h
@@ -22,7 +22,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/cypress/psoc64/partition/region_defs.h b/platform/ext/target/cypress/psoc64/partition/region_defs.h
index a42b7cb..fd9daef 100644
--- a/platform/ext/target/cypress/psoc64/partition/region_defs.h
+++ b/platform/ext/target/cypress/psoc64/partition/region_defs.h
@@ -33,7 +33,10 @@
    system allocation */
 #define TOTAL_RAM_SIZE (0x000E8000) /* CY_SRAM_SIZE - 96KB */
 
-#define S_HEAP_SIZE             0x0001000
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   0x0000400
 #define S_MSP_STACK_SIZE        0x0000800
 
diff --git a/platform/ext/target/lairdconnectivity/common/bl5340/partition/region_defs.h b/platform/ext/target/lairdconnectivity/common/bl5340/partition/region_defs.h
index 3ea2087..133c652 100755
--- a/platform/ext/target/lairdconnectivity/common/bl5340/partition/region_defs.h
+++ b/platform/ext/target/lairdconnectivity/common/bl5340/partition/region_defs.h
@@ -24,7 +24,10 @@
 #define BL2_HEAP_SIZE           (0x00001000)
 #define BL2_MSP_STACK_SIZE      (0x00002000)
 
-#define S_HEAP_SIZE             (0x00001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x00000400)
 #define S_MSP_STACK_SIZE        (0x00000800)
 #define S_PSP_STACK_SIZE        (0x00000800)
diff --git a/platform/ext/target/nordic_nrf/common/nrf5340/partition/region_defs.h b/platform/ext/target/nordic_nrf/common/nrf5340/partition/region_defs.h
index 646fc77..13a47aa 100755
--- a/platform/ext/target/nordic_nrf/common/nrf5340/partition/region_defs.h
+++ b/platform/ext/target/nordic_nrf/common/nrf5340/partition/region_defs.h
@@ -23,7 +23,10 @@
 #define BL2_HEAP_SIZE           (0x00001000)
 #define BL2_MSP_STACK_SIZE      (0x00001800)
 
-#define S_HEAP_SIZE             (0x00001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x00000400)
 #define S_MSP_STACK_SIZE        (0x00000800)
 #define S_PSP_STACK_SIZE        (0x00000800)
diff --git a/platform/ext/target/nordic_nrf/common/nrf9160/partition/region_defs.h b/platform/ext/target/nordic_nrf/common/nrf9160/partition/region_defs.h
index a79546a..59b887c 100755
--- a/platform/ext/target/nordic_nrf/common/nrf9160/partition/region_defs.h
+++ b/platform/ext/target/nordic_nrf/common/nrf9160/partition/region_defs.h
@@ -23,7 +23,10 @@
 #define BL2_HEAP_SIZE           (0x00001000)
 #define BL2_MSP_STACK_SIZE      (0x00001800)
 
-#define S_HEAP_SIZE             (0x00001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x00000400)
 #define S_MSP_STACK_SIZE        (0x00000800)
 #define S_PSP_STACK_SIZE        (0x00000800)
diff --git a/platform/ext/target/nuvoton/m2351/partition/region_defs.h b/platform/ext/target/nuvoton/m2351/partition/region_defs.h
index ee3705f..76adbc8 100644
--- a/platform/ext/target/nuvoton/m2351/partition/region_defs.h
+++ b/platform/ext/target/nuvoton/m2351/partition/region_defs.h
@@ -23,7 +23,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/nuvoton/m2354/partition/region_defs.h b/platform/ext/target/nuvoton/m2354/partition/region_defs.h
index b69c066..b244382 100644
--- a/platform/ext/target/nuvoton/m2354/partition/region_defs.h
+++ b/platform/ext/target/nuvoton/m2354/partition/region_defs.h
@@ -23,7 +23,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/nxp/lpcxpresso55s69/partition/region_defs.h b/platform/ext/target/nxp/lpcxpresso55s69/partition/region_defs.h
index 4e45998..7ed1103 100755
--- a/platform/ext/target/nxp/lpcxpresso55s69/partition/region_defs.h
+++ b/platform/ext/target/nxp/lpcxpresso55s69/partition/region_defs.h
@@ -23,7 +23,10 @@
 #define BL2_HEAP_SIZE           (0x0001000)
 #define BL2_MSP_STACK_SIZE      (0x0001800)
 
-#define S_HEAP_SIZE             (0x0001000)
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   (0x0000400)
 #define S_MSP_STACK_SIZE        (0x0000800)
 #define S_PSP_STACK_SIZE        (0x0000800)
diff --git a/platform/ext/target/stm/b_u585i_iot02a/include/region_defs.h b/platform/ext/target/stm/b_u585i_iot02a/include/region_defs.h
index 2fee3c4..3f58cdb 100644
--- a/platform/ext/target/stm/b_u585i_iot02a/include/region_defs.h
+++ b/platform/ext/target/stm/b_u585i_iot02a/include/region_defs.h
@@ -30,7 +30,10 @@
 #define LOADER_S_PSP_STACK_SIZE 0x0000400
 
 
-#define S_HEAP_SIZE             0x0001000
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   0x0000400
 #define S_MSP_STACK_SIZE        0x0000800
 #define S_PSP_STACK_SIZE        0x0000800
diff --git a/platform/ext/target/stm/nucleo_l552ze_q/include/region_defs.h b/platform/ext/target/stm/nucleo_l552ze_q/include/region_defs.h
index 40218ee..6c5d5dc 100644
--- a/platform/ext/target/stm/nucleo_l552ze_q/include/region_defs.h
+++ b/platform/ext/target/stm/nucleo_l552ze_q/include/region_defs.h
@@ -21,7 +21,10 @@
 #define BL2_HEAP_SIZE           0x0001000
 #define BL2_MSP_STACK_SIZE      0x0001800
 
-#define S_HEAP_SIZE             0x0001000
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   0x0000400
 #define S_MSP_STACK_SIZE        0x0000800
 #define S_PSP_STACK_SIZE        0x0000800
diff --git a/platform/ext/target/stm/stm32l562e_dk/include/region_defs.h b/platform/ext/target/stm/stm32l562e_dk/include/region_defs.h
index 497c756..e756ce9 100644
--- a/platform/ext/target/stm/stm32l562e_dk/include/region_defs.h
+++ b/platform/ext/target/stm/stm32l562e_dk/include/region_defs.h
@@ -21,7 +21,10 @@
 #define BL2_HEAP_SIZE           0x0001000
 #define BL2_MSP_STACK_SIZE      0x0001800
 
-#define S_HEAP_SIZE             0x0001000
+#ifdef ENABLE_HEAP
+    #define S_HEAP_SIZE             (0x0000200)
+#endif
+
 #define S_MSP_STACK_SIZE_INIT   0x0000400
 #define S_MSP_STACK_SIZE        0x0000800
 #define S_PSP_STACK_SIZE        0x0000800
diff --git a/platform/include/mbedtls_entropy_nv_seed_config.h b/platform/include/mbedtls_entropy_nv_seed_config.h
index 1ea442b..34258d7 100644
--- a/platform/include/mbedtls_entropy_nv_seed_config.h
+++ b/platform/include/mbedtls_entropy_nv_seed_config.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021, Arm Limited. All rights reserved.
+ * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  *
@@ -13,7 +13,6 @@
 extern "C" {
 #endif /* __cplusplus */
 
-#undef MBEDTLS_PLATFORM_NO_STD_FUNCTIONS
 #undef MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES
 
 #define MBEDTLS_ENTROPY_NV_SEED
diff --git a/secure_fw/partitions/crypto/crypto_init.c b/secure_fw/partitions/crypto/crypto_init.c
index 077fceb..ab02882 100644
--- a/secure_fw/partitions/crypto/crypto_init.c
+++ b/secure_fw/partitions/crypto/crypto_init.c
@@ -18,6 +18,8 @@
  */
 #include "mbedtls/memory_buffer_alloc.h"
 
+#include "mbedtls/platform.h"
+
 #ifdef CRYPTO_NV_SEED
 #include "tfm_plat_crypto_nv_seed.h"
 #endif /* CRYPTO_NV_SEED */
@@ -343,6 +345,11 @@
     mbedtls_memory_buffer_alloc_init(mbedtls_mem_buf,
                                      TFM_CRYPTO_ENGINE_BUF_SIZE);
 
+    /* mbedtls_printf is used to print messages including error information. */
+#if (TFM_PARTITION_LOG_LEVEL >= TFM_PARTITION_LOG_LEVEL_ERROR)
+    mbedtls_platform_set_printf(tfm_sp_log_printf);
+#endif
+
     /* Initialise the crypto accelerator if one is enabled */
 #ifdef CRYPTO_HW_ACCELERATOR
     LOG_INFFMT("[INF][Crypto] Initialising HW accelerator... ");