Boot: Make MCUBoot logging optional

Add new MCUBOOT_LOG_LEVEL CMake variable to the MCUBoot configuration.
In a debug build the MCUBoot logging can be set to several levels.
The logging can also be entirely disabled and thus the code size can be
reduced. In case of a release build the value of MCUBOOT_LOG_LEVEL is
overridden and the logging is disabled in MCUBoot.

Change-Id: I55ad8f85fdd0921f07a9c156858fadecf81a7449
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/bl2/ext/mcuboot/CMakeLists.txt b/bl2/ext/mcuboot/CMakeLists.txt
index bf29f18..603d511 100644
--- a/bl2/ext/mcuboot/CMakeLists.txt
+++ b/bl2/ext/mcuboot/CMakeLists.txt
@@ -143,10 +143,11 @@
 #Generate binary file from axf
 compiler_generate_binary_output(${PROJECT_NAME})
 
-message(STATUS "MCUBOOT_IMAGE_NUMBER is set to: '${MCUBOOT_IMAGE_NUMBER}'.")
-message(STATUS "MCUBOOT_UPGRADE_STRATEGY is set to: '${MCUBOOT_UPGRADE_STRATEGY}'.")
-message(STATUS "MCUBOOT_SIGNATURE_TYPE is set to: '${MCUBOOT_SIGNATURE_TYPE}'.")
-message(STATUS "MCUBOOT_HW_KEY is set to: '${MCUBOOT_HW_KEY}'.")
+message("- MCUBOOT_IMAGE_NUMBER: '${MCUBOOT_IMAGE_NUMBER}'.")
+message("- MCUBOOT_UPGRADE_STRATEGY: '${MCUBOOT_UPGRADE_STRATEGY}'.")
+message("- MCUBOOT_SIGNATURE_TYPE: '${MCUBOOT_SIGNATURE_TYPE}'.")
+message("- MCUBOOT_HW_KEY: '${MCUBOOT_HW_KEY}'.")
+message("- MCUBOOT_LOG_LEVEL: '${MCUBOOT_LOG_LEVEL}'.")
 
 #Set macro definitions for the project.
 target_compile_definitions(${PROJECT_NAME} PRIVATE
@@ -186,6 +187,11 @@
 			"deprecated and this feature will probably be removed from MCUBoot in the future.")
 endif()
 
+#Configure log level for MCUBoot.
+get_property(_log_levels CACHE MCUBOOT_LOG_LEVEL PROPERTY STRINGS)
+list(FIND _log_levels ${MCUBOOT_LOG_LEVEL} LOG_LEVEL_ID)
+target_compile_definitions(${PROJECT_NAME} PRIVATE BOOT_LOG_LEVEL=${LOG_LEVEL_ID})
+
 #Set install location. Keep original value to avoid overriding command line settings.
 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
 	set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "Default install location for MCUBoot." FORCE)
diff --git a/bl2/ext/mcuboot/MCUBootConfig.cmake b/bl2/ext/mcuboot/MCUBootConfig.cmake
index 67526b1c..97edcfa 100644
--- a/bl2/ext/mcuboot/MCUBootConfig.cmake
+++ b/bl2/ext/mcuboot/MCUBootConfig.cmake
@@ -27,6 +27,13 @@
 
 	set(MCUBOOT_HW_KEY On CACHE BOOL "Configure to use HW key for image verification. Otherwise key is embedded in MCUBoot image.")
 
+	set(MCUBOOT_LOG_LEVEL "LOG_LEVEL_INFO" CACHE STRING "Configure the level of logging in MCUBoot.")
+	set_property(CACHE MCUBOOT_LOG_LEVEL PROPERTY STRINGS "LOG_LEVEL_OFF;LOG_LEVEL_ERROR;LOG_LEVEL_WARNING;LOG_LEVEL_INFO;LOG_LEVEL_DEBUG")
+	if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
+		set(MCUBOOT_LOG_LEVEL "LOG_LEVEL_OFF")
+	endif()
+	validate_cache_value(MCUBOOT_LOG_LEVEL)
+
 	if ((${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "NO_SWAP" OR
 		 ${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "RAM_LOADING") AND
 		NOT (MCUBOOT_IMAGE_NUMBER EQUAL 1))
@@ -40,12 +47,14 @@
 	if (DEFINED MCUBOOT_IMAGE_NUMBER OR
 		DEFINED MCUBOOT_UPGRADE_STRATEGY OR
 		DEFINED MCUBOOT_SIGNATURE_TYPE OR
-		DEFINED MCUBOOT_HW_KEY)
+		DEFINED MCUBOOT_HW_KEY OR
+		DEFINED MCUBOOT_LOG_LEVEL)
 		message(WARNING "Ignoring the values of MCUBOOT_* variables as BL2 option is set to False.")
 		set(MCUBOOT_IMAGE_NUMBER "")
 		set(MCUBOOT_UPGRADE_STRATEGY "")
 		set(MCUBOOT_SIGNATURE_TYPE "")
 		set(MCUBOOT_HW_KEY "")
+		set(MCUBOOT_LOG_LEVEL "")
 	endif()
 
 	if (DEFINED SECURITY_COUNTER OR
diff --git a/bl2/ext/mcuboot/bl2_main.c b/bl2/ext/mcuboot/bl2_main.c
index f86c4c2..081d88f 100644
--- a/bl2/ext/mcuboot/bl2_main.c
+++ b/bl2/ext/mcuboot/bl2_main.c
@@ -19,10 +19,8 @@
 #include "bl2_util.h"
 #include "target.h"
 #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"
 #include "bootutil/bootutil.h"
@@ -30,6 +28,9 @@
 #include "bl2/include/boot_record.h"
 #include "security_cnt.h"
 #include "bl2/include/boot_hal.h"
+#if BOOT_LOG_LEVEL > BOOT_LOG_LEVEL_OFF
+#include "uart_stdout.h"
+#endif
 
 /* Avoids the semihosting issue */
 #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
@@ -129,7 +130,9 @@
         BOOT_LOG_ERR("Error while uninitializing Flash Interface");
     }
 
+#if BOOT_LOG_LEVEL > BOOT_LOG_LEVEL_OFF
     stdio_uninit();
+#endif
 
 #if defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8M_BASE__)
     /* Restore the Main Stack Pointer Limit register's reset value
@@ -159,7 +162,9 @@
     __set_MSPLIM(msp_stack_bottom);
 #endif
 
+#if BOOT_LOG_LEVEL > BOOT_LOG_LEVEL_OFF
     stdio_init();
+#endif
 
     BOOT_LOG_INF("Starting bootloader");
 
diff --git a/bl2/ext/mcuboot/bootutil/src/loader.c b/bl2/ext/mcuboot/bootutil/src/loader.c
index a24e332..3e0ba90 100644
--- a/bl2/ext/mcuboot/bootutil/src/loader.c
+++ b/bl2/ext/mcuboot/bootutil/src/loader.c
@@ -39,13 +39,11 @@
 #include "bootutil/bootutil.h"
 #include "bootutil/image.h"
 #include "bootutil_priv.h"
+#include "bootutil/bootutil_log.h"
 #include "bl2/include/tfm_boot_status.h"
 #include "bl2/include/boot_record.h"
 #include "security_cnt.h"
 
-#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
-#include "bootutil/bootutil_log.h"
-
 static struct boot_loader_state boot_data;
 uint8_t current_image = 0;
 
diff --git a/bl2/ext/mcuboot/flash_map.c b/bl2/ext/mcuboot/flash_map.c
index 2d865a7..dbaf94a 100644
--- a/bl2/ext/mcuboot/flash_map.c
+++ b/bl2/ext/mcuboot/flash_map.c
@@ -32,8 +32,6 @@
 #include "Driver_Flash.h"
 
 #include <flash_map/flash_map.h>
-
-#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
 #include "bootutil/bootutil_log.h"
 
 /* Flash device name must be specified by target */