Build: Reorganize BL2 configuration in cmake

This commit moves the BL2 related configuration switches from the
CommonConfig.cmake file to a separate MCUBootConfig.cmake file to
make its maintenance easier.

Change-Id: I3869528bdbd2a5a071f3b3cc4b2782d22114b1ca
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/CommonConfig.cmake b/CommonConfig.cmake
index ee2faee..a1cd01d 100644
--- a/CommonConfig.cmake
+++ b/CommonConfig.cmake
@@ -30,15 +30,7 @@
 endif()
 
 #BL2 bootloader (MCUBoot) related settings
-if(NOT DEFINED BL2)
-	set(BL2 True CACHE BOOL "Configure TF-M to use BL2 and enable building BL2")
-endif()
-if (BL2)
-	if (NOT DEFINED MCUBOOT_UPGRADE_STRATEGY)
-		set (MCUBOOT_UPGRADE_STRATEGY "OVERWRITE_ONLY" CACHE STRING "Configure BL2 which upgrade strategy to use")
-		set_property(CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS "OVERWRITE_ONLY;SWAP;NO_SWAP;RAM_LOADING")
-	endif()
-endif()
+include(${CMAKE_CURRENT_LIST_DIR}/bl2/ext/mcuboot/MCUBootConfig.cmake)
 
 set(BUILD_CMSIS_CORE Off)
 set(BUILD_RETARGET Off)
@@ -266,27 +258,9 @@
 endif()
 
 if (BL2)
-	add_definitions(-DBL2)
-	if (NOT ${MCUBOOT_SIGNATURE_TYPE} STREQUAL "RSA-2048" AND NOT ${MCUBOOT_SIGNATURE_TYPE} STREQUAL "RSA-3072")
-		message(FATAL_ERROR "MCUBoot only supports RSA-2048 and RSA-3072 signature")
-	endif()
-	if (NOT DEFINED MCUBOOT_SIGNATURE_TYPE)
-		set(MCUBOOT_SIGNATURE_TYPE "RSA-3072")
-	endif()
-	if (NOT ${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "OVERWRITE_ONLY" AND
-		NOT ${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "SWAP" AND
-		NOT ${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "NO_SWAP" AND
-		NOT ${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "RAM_LOADING")
-		message(FATAL_ERROR "ERROR: MCUBoot supports OVERWRITE_ONLY, SWAP, NO_SWAP and RAM_LOADING upgrade strategies only.")
-	endif()
 	if (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "NO_SWAP")
 		set(LINK_TO_BOTH_MEMORY_REGION ON)
 	endif()
-else() #BL2 is turned off
-	if (DEFINED MCUBOOT_UPGRADE_STRATEGY)
-		message (WARNING "Ignoring value of MCUBOOT_UPGRADE_STRATEGY as BL2 option is set to False.")
-		unset (MCUBOOT_UPGRADE_STRATEGY)
-	endif()
 endif()
 
 ##Set Mbed TLS compiler flags and variables for audit log and crypto
diff --git a/bl2/ext/mcuboot/CMakeLists.txt b/bl2/ext/mcuboot/CMakeLists.txt
index 3b4bf10..6f8cad8 100644
--- a/bl2/ext/mcuboot/CMakeLists.txt
+++ b/bl2/ext/mcuboot/CMakeLists.txt
@@ -129,6 +129,9 @@
 #Generate binary file from axf
 compiler_generate_binary_output(${PROJECT_NAME})
 
+message(STATUS "MCUBOOT_UPGRADE_STRATEGY is set to: '${MCUBOOT_UPGRADE_STRATEGY}'.")
+message(STATUS "MCUBOOT_SIGNATURE_TYPE is set to: '${MCUBOOT_SIGNATURE_TYPE}'.")
+
 #Set macro definitions for the project.
 target_compile_definitions(${PROJECT_NAME} PRIVATE
 							MCUBOOT_VALIDATE_SLOT0
@@ -153,7 +156,8 @@
 elseif (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "SWAP")
 	#No compile definition needs to be specified for this upgrade strategy
 else()
-	message(FATAL_ERROR "ERROR: MCUBoot supports OVERWRITE_ONLY, SWAP, NO_SWAP and RAM_LOADING upgrade strategies only.")
+	get_property(_upgrade_strategies CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS)
+	message(FATAL_ERROR "ERROR: MCUBoot supports the ${_upgrade_strategies} upgrade strategies only.")
 endif()
 
 #Set install location. Keep original value to avoid overriding command line settings.
diff --git a/bl2/ext/mcuboot/MCUBoot.cmake b/bl2/ext/mcuboot/MCUBoot.cmake
index f685121..5187694 100644
--- a/bl2/ext/mcuboot/MCUBoot.cmake
+++ b/bl2/ext/mcuboot/MCUBoot.cmake
@@ -55,7 +55,7 @@
 		message(FATAL_ERROR "${MCUBOOT_SIGNATURE_TYPE} is not supported as firmware signing algorithm")
 	endif()
 
-	if (DEFINED SECURITY_COUNTER)
+	if (SECURITY_COUNTER)
 		set (ADD_SECURITY_COUNTER "-s ${SECURITY_COUNTER}")
 	else()
 		set (ADD_SECURITY_COUNTER "")
diff --git a/bl2/ext/mcuboot/MCUBootConfig.cmake b/bl2/ext/mcuboot/MCUBootConfig.cmake
new file mode 100644
index 0000000..406d214
--- /dev/null
+++ b/bl2/ext/mcuboot/MCUBootConfig.cmake
@@ -0,0 +1,33 @@
+#-------------------------------------------------------------------------------
+# Copyright (c) 2019, Arm Limited. All rights reserved.
+#
+# SPDX-License-Identifier: BSD-3-Clause
+#
+#-------------------------------------------------------------------------------
+
+set(BL2 True CACHE BOOL "Configure TF-M to use BL2 and enable building BL2")
+
+if (BL2)
+	add_definitions(-DBL2)
+
+	set(MCUBOOT_UPGRADE_STRATEGY "OVERWRITE_ONLY" CACHE STRING "Configure BL2 which upgrade strategy to use")
+	set_property(CACHE MCUBOOT_UPGRADE_STRATEGY PROPERTY STRINGS "OVERWRITE_ONLY;SWAP;NO_SWAP;RAM_LOADING")
+	validate_cache_value(MCUBOOT_UPGRADE_STRATEGY)
+
+	set(MCUBOOT_SIGNATURE_TYPE "RSA-3072" CACHE STRING "Algorithm used by MCUBoot to validate signatures.")
+	set_property(CACHE MCUBOOT_SIGNATURE_TYPE PROPERTY STRINGS "RSA-3072;RSA-2048")
+	validate_cache_value(MCUBOOT_SIGNATURE_TYPE)
+
+else() #BL2 is turned off
+	if (DEFINED MCUBOOT_UPGRADE_STRATEGY OR
+		DEFINED MCUBOOT_SIGNATURE_TYPE)
+		message(WARNING "Ignoring the values of MCUBOOT_* variables as BL2 option is set to False.")
+		set(MCUBOOT_UPGRADE_STRATEGY "")
+		set(MCUBOOT_SIGNATURE_TYPE "")
+	endif()
+
+	if (DEFINED SECURITY_COUNTER)
+		message(WARNING "Ignoring the value of SECURITY_COUNTER variable as BL2 option is set to False.")
+		set(SECURITY_COUNTER "")
+	endif()
+endif()
diff --git a/cmake/Common/BuildSys.cmake b/cmake/Common/BuildSys.cmake
index 4de9f3b..396e131 100644
--- a/cmake/Common/BuildSys.cmake
+++ b/cmake/Common/BuildSys.cmake
@@ -159,6 +159,37 @@
 		endif()
 endfunction()
 
+#Check the value of a cache variable whether it is valid.
+#
+# This function currently only supports 'STRING' type variables and uses
+# the 'STRINGS' cache entry property as the validation list.
+#
+#Examples:
+#  validate_cache_value(MCUBOOT_SIGNATURE_TYPE)
+#
+#INPUTS:
+#  variable_name - (mandatory) - Name of the cache variable to be checked.
+#
+#OUTPUTS:
+#  n/a
+#
+function(validate_cache_value variable_name)
+	#Check if the type of the variable is STRING.
+	get_property(_type CACHE ${variable_name} PROPERTY TYPE)
+	if(NOT ${_type} STREQUAL "STRING")
+		message(FATAL_ERROR "validate_cache_value: type of CACHE variable must be 'STRING', the type of '${variable_name}' variable is '${_type}'.")
+	endif()
+	get_property(_validation_list CACHE ${variable_name} PROPERTY STRINGS)
+	#Check if validation list is set.
+	if (NOT _validation_list)
+		message(FATAL_ERROR "validate_cache_value: CACHE variable '${variable_name}' has no 'STRINGS' validation list set.")
+	endif()
+	#See if the value of the variable is in the list.
+	if (NOT ${${variable_name}} IN_LIST _validation_list)
+		message(FATAL_ERROR "validate_cache_value: variable value '${${variable_name}}' of variable '${variable_name}' is not matching the validation list ${_validation_list}.")
+	endif()
+endfunction()
+
 #Specify an include path for the compiler
 #
 # Specify a global include directory for all non external targets in the current
diff --git a/platform/ext/musca_a.cmake b/platform/ext/musca_a.cmake
index 8813780..b818c06 100644
--- a/platform/ext/musca_a.cmake
+++ b/platform/ext/musca_a.cmake
@@ -35,10 +35,6 @@
 set (FLASH_LAYOUT          "${PLATFORM_DIR}/target/musca_a/partition/flash_layout.h")
 set (PLATFORM_LINK_INCLUDES "${PLATFORM_DIR}/target/musca_a/partition")
 
-if (BL2)
-  set (BL2_LINKER_CONFIG ${BL2_SCATTER_FILE_NAME})
-endif()
-
 embedded_include_directories(PATH "${PLATFORM_DIR}/cmsis" ABSOLUTE)
 embedded_include_directories(PATH "${PLATFORM_DIR}/target/musca_a" ABSOLUTE)
 embedded_include_directories(PATH "${PLATFORM_DIR}/target/musca_a/CMSIS_Driver/Config" ABSOLUTE)
@@ -177,8 +173,12 @@
 
 if (NOT BL2)
     message(WARNING "BL2 is mandatory on target '${TARGET_PLATFORM}'. Your choice was overriden.")
+    add_definitions(-DBL2)
     set(BL2 True)
     set(MCUBOOT_UPGRADE_STRATEGY "RAM_LOADING")
+    message(STATUS "MCUBOOT_UPGRADE_STRATEGY was not set, using the mandatory value for '${TARGET_PLATFORM}': ${MCUBOOT_UPGRADE_STRATEGY}.")
+    set(MCUBOOT_SIGNATURE_TYPE "RSA-3072")
+    message(STATUS "MCUBOOT_SIGNATURE_TYPE was not set, using default value: ${MCUBOOT_SIGNATURE_TYPE}.")
 else() #BL2 is True
     if (NOT ${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "RAM_LOADING")
         message(WARNING "RAM_LOADING upgrade strategy is mandatory on target '${TARGET_PLATFORM}'. Your choice was overriden.")
@@ -186,6 +186,10 @@
     endif()
 endif()
 
+if (BL2)
+    set(BL2_LINKER_CONFIG ${BL2_SCATTER_FILE_NAME})
+endif()
+
 if (NOT DEFINED BUILD_BOOT_SEED)
   message(FATAL_ERROR "Configuration variable BUILD_BOOT_SEED (true|false) is undefined!")
 elseif(BUILD_BOOT_SEED)