Build: Combine compile time MCUBoot switches

Remove multiple compile time MCUBoot switches and combine their
functionality into the 'MCUBOOT_UPGRADE_STRATEGY' switch.

Change-Id: Ibed68983ca0e2ed3c291d56514e8ad93ac0dbeba
Signed-off-by: David Vincze <david.vincze@arm.com>
diff --git a/CommonConfig.cmake b/CommonConfig.cmake
index 8f24174..9d35638 100644
--- a/CommonConfig.cmake
+++ b/CommonConfig.cmake
@@ -9,8 +9,6 @@
 	message(FATAL_ERROR "ERROR: Incomplete Configuration: REGRESSION not defined, Include this file from a Config*.cmake")
 elseif(NOT DEFINED CORE_TEST)
 	message(FATAL_ERROR "ERROR: Incomplete Configuration: CORE_TEST not defined, Include this file from a Config*.cmake")
-elseif(NOT DEFINED BL2)
-	message(FATAL_ERROR "ERROR: Incomplete Configuration: BL2 not defined, Include this file from a Config*.cmake")
 elseif(NOT DEFINED TFM_LVL)
 	message(FATAL_ERROR "ERROR: Incomplete Configuration: TFM_LVL not defined, Include this file from a Config*.cmake")
 endif()
@@ -21,6 +19,17 @@
 	message(FATAL_ERROR "ERROR: Compiler \"${COMPILER}\" is not supported.")
 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 "SWAP" 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()
+
 set(BUILD_CMSIS_CORE Off)
 set(BUILD_RETARGET Off)
 set(BUILD_NATIVE_DRIVERS Off)
@@ -217,15 +226,19 @@
 
 if (BL2)
 	add_definitions(-DBL2)
-	if (MCUBOOT_NO_SWAP)
+	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()
-	if (MCUBOOT_NO_SWAP AND MCUBOOT_RAM_LOADING)
-		message (FATAL_ERROR "Bootloader: MCUBOOT_RAM_LOADING and MCUBOOT_NO_SWAP are not supported together")
-	endif()
-else()
-	if (MCUBOOT_NO_SWAP)
-		message (FATAL_ERROR "Bootloader build is turned off, not possible to specify bootloader behavior")
+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()
 
diff --git a/ConfigCoreIPC.cmake b/ConfigCoreIPC.cmake
index c06d0c9..b330000 100644
--- a/ConfigCoreIPC.cmake
+++ b/ConfigCoreIPC.cmake
@@ -34,17 +34,4 @@
 # TF-M isolation level: 1..3
 set (TFM_LVL 1)
 
-#BL2 bootloader(MCUBoot) related settings
-if(NOT DEFINED BL2)
-	set(BL2 True)
-endif()
-
-if(NOT DEFINED MCUBOOT_NO_SWAP)
-	set(MCUBOOT_NO_SWAP False)
-endif()
-
-if(NOT DEFINED MCUBOOT_RAM_LOADING)
-	set(MCUBOOT_RAM_LOADING False)
-endif()
-
 include ("${CMAKE_CURRENT_LIST_DIR}/CommonConfig.cmake")
diff --git a/ConfigCoreIPCTfmLevel2.cmake b/ConfigCoreIPCTfmLevel2.cmake
index 8592066..dac3d5e 100644
--- a/ConfigCoreIPCTfmLevel2.cmake
+++ b/ConfigCoreIPCTfmLevel2.cmake
@@ -33,17 +33,4 @@
 # TF-M isolation level: 1..3
 set (TFM_LVL 2)
 
-#BL2 bootloader(MCUBoot) related settings
-if(NOT DEFINED BL2)
-	set(BL2 True)
-endif()
-
-if(NOT DEFINED MCUBOOT_NO_SWAP)
-	set(MCUBOOT_NO_SWAP False)
-endif()
-
-if(NOT DEFINED MCUBOOT_RAM_LOADING)
-	set(MCUBOOT_RAM_LOADING False)
-endif()
-
 include ("${CMAKE_CURRENT_LIST_DIR}/CommonConfig.cmake")
diff --git a/ConfigCoreTest.cmake b/ConfigCoreTest.cmake
index 72f7b49..e636b7e 100644
--- a/ConfigCoreTest.cmake
+++ b/ConfigCoreTest.cmake
@@ -29,17 +29,4 @@
 # TF-M isolation level: 1..3
 set (TFM_LVL 3)
 
-#BL2 bootloader(MCUBoot) related settings
-if(NOT DEFINED BL2)
-	set(BL2 True)
-endif()
-
-if(NOT DEFINED MCUBOOT_NO_SWAP)
-	set(MCUBOOT_NO_SWAP False)
-endif()
-
-if(NOT DEFINED MCUBOOT_RAM_LOADING)
-	set(MCUBOOT_RAM_LOADING False)
-endif()
-
 include ("${CMAKE_CURRENT_LIST_DIR}/CommonConfig.cmake")
diff --git a/ConfigDefault.cmake b/ConfigDefault.cmake
index 371df19..5824127 100644
--- a/ConfigDefault.cmake
+++ b/ConfigDefault.cmake
@@ -33,17 +33,4 @@
 # TF-M isolation level: 1..3
 set (TFM_LVL 1)
 
-#BL2 bootloader(MCUBoot) related settings
-if(NOT DEFINED BL2)
-	set(BL2 True)
-endif()
-
-if(NOT DEFINED MCUBOOT_NO_SWAP)
-	set(MCUBOOT_NO_SWAP False)
-endif()
-
-if(NOT DEFINED MCUBOOT_RAM_LOADING)
-	set(MCUBOOT_RAM_LOADING False)
-endif()
-
 include ("${CMAKE_CURRENT_LIST_DIR}/CommonConfig.cmake")
diff --git a/ConfigPsaApiTest.cmake b/ConfigPsaApiTest.cmake
index a449aaf..23edbd3 100644
--- a/ConfigPsaApiTest.cmake
+++ b/ConfigPsaApiTest.cmake
@@ -34,19 +34,6 @@
 #TF-M isolation level: 1..3
 set (TFM_LVL 1)
 
-#BL2 bootloader(MCUBoot) related settings
-if(NOT DEFINED BL2)
-	set(BL2 True)
-endif()
-
-if(NOT DEFINED MCUBOOT_NO_SWAP)
-	set(MCUBOOT_NO_SWAP False)
-endif()
-
-if(NOT DEFINED MCUBOOT_RAM_LOADING)
-	set(MCUBOOT_RAM_LOADING False)
-endif()
-
 #Service specific configuration for the PSA API Compliance test requirements
 if(PSA_API_TEST_CRYPTO)
 	set(CRYPTO_ENGINE_BUF_SIZE 20480)
diff --git a/ConfigRegression.cmake b/ConfigRegression.cmake
index ad1d583..7fdeb93 100644
--- a/ConfigRegression.cmake
+++ b/ConfigRegression.cmake
@@ -33,17 +33,4 @@
 # TF-M isolation level: 1..3
 set (TFM_LVL 1)
 
-#BL2 bootloader(MCUBoot) related settings
-if(NOT DEFINED BL2)
-	set(BL2 True)
-endif()
-
-if(NOT DEFINED MCUBOOT_NO_SWAP)
-	set(MCUBOOT_NO_SWAP False)
-endif()
-
-if(NOT DEFINED MCUBOOT_RAM_LOADING)
-	set(MCUBOOT_RAM_LOADING False)
-endif()
-
 include ("${CMAKE_CURRENT_LIST_DIR}/CommonConfig.cmake")
diff --git a/ConfigRegressionTfmLevel3.cmake b/ConfigRegressionTfmLevel3.cmake
index 334a4d5..89231b5 100644
--- a/ConfigRegressionTfmLevel3.cmake
+++ b/ConfigRegressionTfmLevel3.cmake
@@ -33,17 +33,4 @@
 # TF-M isolation level: 1..3
 set (TFM_LVL 3)
 
-#BL2 bootloader(MCUBoot) related settings
-if(NOT DEFINED BL2)
-	set(BL2 True)
-endif()
-
-if(NOT DEFINED MCUBOOT_NO_SWAP)
-	set(MCUBOOT_NO_SWAP False)
-endif()
-
-if(NOT DEFINED MCUBOOT_RAM_LOADING)
-	set(MCUBOOT_RAM_LOADING False)
-endif()
-
 include ("${CMAKE_CURRENT_LIST_DIR}/CommonConfig.cmake")
diff --git a/bl2/ext/mcuboot/CMakeLists.txt b/bl2/ext/mcuboot/CMakeLists.txt
index a8c5440..71bf456 100644
--- a/bl2/ext/mcuboot/CMakeLists.txt
+++ b/bl2/ext/mcuboot/CMakeLists.txt
@@ -1,5 +1,5 @@
 #------------------------------------------------------------------------------
-# Copyright (c) 2017-2018, Arm Limited. All rights reserved.
+# Copyright (c) 2017-2019, Arm Limited. All rights reserved.
 #
 # SPDX-License-Identifier: BSD-3-Clause
 #
@@ -135,12 +135,16 @@
 							MBEDTLS_CONFIG_FILE="config-boot.h"
 							MCUBOOT_TARGET_CONFIG="flash_layout.h")
 
-if (MCUBOOT_NO_SWAP)
+if (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "OVERWRITE_ONLY")
+	target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_OVERWRITE_ONLY)
+elseif (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "NO_SWAP")
 	target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_NO_SWAP)
-endif()
-
-if (MCUBOOT_RAM_LOADING)
+elseif (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "RAM_LOADING")
 	target_compile_definitions(${PROJECT_NAME} PRIVATE MCUBOOT_RAM_LOADING)
+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.")
 endif()
 
 #Set install location. Keep original value to avoid overriding command line settings.
diff --git a/platform/ext/Mps2AN519.cmake b/platform/ext/Mps2AN519.cmake
index 9d3c612..e392047 100644
--- a/platform/ext/Mps2AN519.cmake
+++ b/platform/ext/Mps2AN519.cmake
@@ -38,6 +38,9 @@
 
 if (BL2)
   set (BL2_LINKER_CONFIG ${BL2_SCATTER_FILE_NAME})
+  if (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "RAM_LOADING")
+      message(FATAL_ERROR "ERROR: RAM_LOADING upgrade strategy is not supported on target '${TARGET_PLATFORM}'.")
+  endif()
 endif()
 
 embedded_include_directories(PATH "${PLATFORM_DIR}/cmsis" ABSOLUTE)
@@ -166,10 +169,6 @@
   embedded_include_directories(PATH "${PLATFORM_DIR}/target/mps2/an519/cmsis_drivers" ABSOLUTE)
 endif()
 
-if (MCUBOOT_RAM_LOADING)
-	message (FATAL_ERROR "MCUBOOT_RAM_LOADING is not supported on " ${TARGET_PLATFORM})
-endif()
-
 if (NOT DEFINED BUILD_BOOT_SEED)
   message(FATAL_ERROR "Configuration variable BUILD_BOOT_SEED (true|false) is undefined!")
 elseif(BUILD_BOOT_SEED)
diff --git a/platform/ext/Mps2AN521.cmake b/platform/ext/Mps2AN521.cmake
index 80fe871..413bb1c 100644
--- a/platform/ext/Mps2AN521.cmake
+++ b/platform/ext/Mps2AN521.cmake
@@ -39,6 +39,9 @@
 
 if (BL2)
   set (BL2_LINKER_CONFIG ${BL2_SCATTER_FILE_NAME})
+  if (${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "RAM_LOADING")
+      message(FATAL_ERROR "ERROR: RAM_LOADING upgrade strategy is not supported on target '${TARGET_PLATFORM}'.")
+  endif()
 endif()
 
 embedded_include_directories(PATH "${PLATFORM_DIR}/cmsis" ABSOLUTE)
@@ -167,10 +170,6 @@
   embedded_include_directories(PATH "${PLATFORM_DIR}/target/mps2/an521/cmsis_drivers" ABSOLUTE)
 endif()
 
-if (MCUBOOT_RAM_LOADING)
-	message (FATAL_ERROR "MCUBOOT_RAM_LOADING is not supported on " ${TARGET_PLATFORM})
-endif()
-
 if (NOT DEFINED BUILD_BOOT_SEED)
   message(FATAL_ERROR "Configuration variable BUILD_BOOT_SEED (true|false) is undefined!")
 elseif(BUILD_BOOT_SEED)
diff --git a/platform/ext/musca_a.cmake b/platform/ext/musca_a.cmake
index f3bd20d..b0d0e0a 100644
--- a/platform/ext/musca_a.cmake
+++ b/platform/ext/musca_a.cmake
@@ -168,17 +168,14 @@
 endif()
 
 if (NOT BL2)
-	message(STATUS "WARNING: BL2 is mandatory on target \"${TARGET_PLATFORM}\" Your choice was override.")
-	set(BL2 True)
-endif()
-
-if (NOT MCUBOOT_RAM_LOADING)
-	message(STATUS "WARNING: MCUBOOT_RAM_LOADING is mandatory on target \"${TARGET_PLATFORM}\" Your choice was override.")
-	set(MCUBOOT_RAM_LOADING True)
-endif()
-
-if (MCUBOOT_NO_SWAP)
-	message (FATAL_ERROR "MCUBOOT_NO_SWAP configuration is not supported on " ${TARGET_PLATFORM})
+    message(WARNING "BL2 is mandatory on target '${TARGET_PLATFORM}'. Your choice was overriden.")
+    set(BL2 True)
+    set(MCUBOOT_UPGRADE_STRATEGY "RAM_LOADING")
+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.")
+        set(MCUBOOT_UPGRADE_STRATEGY "RAM_LOADING")
+    endif()
 endif()
 
 if (NOT DEFINED BUILD_BOOT_SEED)
diff --git a/platform/ext/musca_b1.cmake b/platform/ext/musca_b1.cmake
index 3b63df3..28452dd 100644
--- a/platform/ext/musca_b1.cmake
+++ b/platform/ext/musca_b1.cmake
@@ -38,6 +38,10 @@
 
 if (BL2)
     set(BL2_LINKER_CONFIG ${BL2_SCATTER_FILE_NAME})
+    if (NOT ${MCUBOOT_UPGRADE_STRATEGY} STREQUAL "NO_SWAP")
+        message(WARNING "NO_SWAP upgrade strategy is mandatory on target '${TARGET_PLATFORM}'. Your choice was overriden.")
+        set(MCUBOOT_UPGRADE_STRATEGY "NO_SWAP")
+    endif()
 endif()
 
 embedded_include_directories(PATH "${PLATFORM_DIR}/cmsis" ABSOLUTE)
@@ -164,10 +168,6 @@
     embedded_include_directories(PATH "${PLATFORM_DIR}/driver" ABSOLUTE)
 endif()
 
-if (MCUBOOT_RAM_LOADING)
-    message(FATAL_ERROR "MCUBOOT_RAM_LOADING is not supported on " ${TARGET_PLATFORM})
-endif()
-
 if (NOT DEFINED BUILD_BOOT_SEED)
   message(FATAL_ERROR "Configuration variable BUILD_BOOT_SEED (true|false) is undefined!")
 elseif(BUILD_BOOT_SEED)