Implement new swap scheme for devices with large erase size using scratch with status area
diff --git a/boot/cypress/BlinkyApp/BlinkyApp.md b/boot/cypress/BlinkyApp/BlinkyApp.md
new file mode 100644
index 0000000..d380673
--- /dev/null
+++ b/boot/cypress/BlinkyApp/BlinkyApp.md
@@ -0,0 +1,265 @@
+### Blinking LED Test Application For Mcubootapp Bootloading Application
+
+### Description
+
+Implements simple Blinky LED CM4 application to demonstrate MCUBootApp bootloading application operation in terms of boot and upgrade processes.
+
+It is validated and started by MCUBootApp which is running on CM0p core of PSoC 6 device.
+
+Functionality:
+
+* Blinks RED led with 2 different rates, depending on type of image - BOOT or UPGRADE.
+* Prints debug info and version of itself to terminal at 115200 baud rate.
+* Manages watchdog timer started in MCUBootApp as one of confirmation mechanisms.
+* Sets special bit in flash to confirm image is operable (UPGRADE image).
+* Can be built for BOOT slot or UPGRADE slot of bootloader.
+* Can be used to evaluate `swap` and `overwrite only` upgrade modes.
+
+### Hardware Limitations
+
+Since this application is created to demonstrate MCUBoot library features and not as reference examples some considerations are taken.
+
+1. Port/pin `P5_0` and `P5_1` used to configure serial port for debug prints. These pins are the most commonly used for serial port connection among available Cypress PSoC 6 kits. If you try to use custom hardware with this application - change definitions of `CY_DEBUG_UART_TX` and `CY_DEBUG_UART_RX` in `main.c` of BlinkyApp to port/pin pairs corresponding to your design.
+2. Port `GPIO_PRT13` pin `7U` used to define user connection LED. This pin is the most commonly used for USER_LED connection among available Cypress PSoC 6 kits. If you try to use custom hardware with this application - change definitions of `LED_PORT` and `LED_PIN` in `main.c` of BlinkyApp to port/pin pairs corresponding to your design.
+
+### Pre-build Action
+
+Pre-build action is implemented to define start address and size of flash, as well as RAM start address and size for BlinkyApp.
+
+These values are set by specifing following macros (default values shown):
+`SLOT_SIZE ?= 0x10000` - for slot located in internal flash 
+`SLOT_SIZE ?= 0x40200` - for slot located in external flash 
+
+For PSoC 6 2M devices:
+`DEFINES_APP += -DRAM_START=0x08040000`
+`DEFINES_APP += -DRAM_SIZE=0x10000`
+
+For PSoC 6 1M and 512K devices:
+`DEFINES_APP += -DRAM_START=0x08020000`
+`DEFINES_APP += -DRAM_SIZE=0x10000`
+
+For all devices:
+`DEFINES_APP += -DUSER_APP_START=0x10018000`
+
+in `boot/cypress/BlinkyApp.mk`.
+
+Pre-build action calls GCC preprocessor which replaces defines to particular values in `BlinkyApp_template.ld`.
+
+**Important**: make sure RAM areas of CM4-based BlinkyApp and CM0p-based MCUBootApp bootloader do not overlap.
+
+Memory (stack) corruption of CM0p application can cause failure if SystemCall-served operations invoked from CM4.
+
+### Building An Application
+
+Supported platforms:
+
+* PSOC_062_2M
+* PSOC_062_1M
+* PSOC_062_512K
+
+Root directory for build is **boot/cypress.**
+
+**Single image**
+
+The following command will build regular HEX file of a BlinkyApp for primary (BOOT) slot:
+
+    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=BOOT
+
+_Note: HEADER_OFFSET=%SLOT_SIZE%_
+
+To build image for secondary (UPGRADE) slot to use in `swap` upgrade:
+
+    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x10000 SWAP_UPGRADE=1
+
+To build image for secondary (UPGRADE) slot to use in `overwrite only` upgrade:
+
+    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x10000 SWAP_UPGRADE=0
+
+To build image for primary (BOOT) image for custom slot size `0x70000`:
+
+    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=BOOT SLOT_SIZE=0x70000
+
+To build image for secondary (UPGRADE) image for custom slot size `0x70000` to use in `swap` upgrade:
+
+    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE SLOT_SIZE=0x70000 HEADER_OFFSET=0x70000 SWAP_UPGRADE=1
+
+**Multi image**
+
+`BlinkyApp` can be built to use in multi image bootloader configuration.
+
+To obtain appropriate hex files to use with multi image MCUBootApp, makefile flag `HEADER_OFFSET=` can be used.
+
+Example usage:
+
+Considering default config:
+
+* first image BOOT (PRIMARY) slot starts `0x10018000`
+* slot size `0x10000`
+* second image BOOT (PRIMARY) slot starts at `0x10038000`
+
+To obtain appropriate artifact for second image PRIMARY slot run this command:
+
+    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=BOOT HEADER_OFFSET=0x20000
+
+*Note:* only 2 images are supported at the moment.
+
+**Upgrade mode dependency**
+
+`MCUBootApp` can upgrade image either by overwriting an image from a secondary slot to a primary slot or by swapping the two images.
+
+To build `BlinkyApp` for different upgrade mode `SWAP_UPGRADE` flag is used.
+
+`SWAP_UPGRADE=0` - for overwrite mode.
+`SWAP_UPGRADE=1` - for swap upgrade mode (default).
+
+**Upgrade image for external memory**
+
+To prepare MCUBootApp for work with external memory refer to `MCUBootApp/ExternalMemory.md`.
+
+To build `BlinkyApp` upgrade image for external memory use command:
+
+    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x7FE8000 ERASED_VALUE=0xff USE_EXTERNAL_FLASH=1
+
+`HEADER_OFFSET` defines the offset from original boot image address. This one in line above suggests secondary slot will start from `0x18000000`, which is a start of external memory related addreses on PSoC 6 devices.
+
+`ERASED_VALUE` defines the memory cell contents in erased state. It is `0x00` for PSoC 6 internal Flash and `0xff` for S25FL512S.
+
+In case of using muti-image configuration, upgrade image for second application can be built using next command:
+
+    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x8228000 ERASED_VALUE=0xff USE_EXTERNAL_FLASH=1
+
+Note: for S25FL512S block address should be mutiple of 0x40000.
+
+**Encrypted upgrade image**
+
+To prepare MCUBootApp for work with encrypted upgrade image please refer to `MCUBootApp/Readme.md`.
+
+To obtain encrypted upgrade image of BlinkyApp extra flag ENC_IMG=1 should be passed in command line, for example:
+
+    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x20000 ENC_IMG=1
+
+This also suggests user already placed corresponing *.pem key in \keys folder. The key variables are defined in root Makefile as SIGN_KEY_FILE and ENC_KEY_FILE
+
+### Complete Build Flags Description
+- `BUILDCFG` - configuration type
+    - Release 
+    - Debug
+- `MAKEINFO` - build verbosity level
+    - 0 (default) - less build info
+    - 1 - verbose output of compilation
+- `PLATFORM`
+    - `PSOC_062_2M` - only supported now
+- `SLOT_SIZE` - size of primary/secondary slot of MCUBootApp this app will be used with
+    - 0x%VALUE%
+- `HEADER_OFFSET` - shift start address of image by value
+    - 0 (default) - no offset of output hex file
+    - 0x%VALUE% - offset for output hex file
+- `IMG_TYPE` - for which slot of MCUBootApp image is build
+    - `BOOT` (default) - build image for primary (BOOT) slot
+    - `UPGRADE` - build image for secondary (UPGRADE) slot
+- `SWAP_UPGRADE` - define upgrade mode type on `MCUBootApp` this app will be used with
+    - `0` - for overwrite mode.
+    - `1` - (default) for swap upgrade mode
+- `ERASED_VALUE` - define memory cell contents in erased state
+    - `0x0` - internal memory
+    - `0xff` - external memory
+- `TOOLCHAIN_PATH` - path to gcc compiler to use for build
+    - Example: TOOLCHAIN_PATH=/home/fw-security/ModusToolbox/tools_2.0/gcc-7.2.1
+    - Example: TOOLCHAIN_PATH=C:\gcc
+
+### Post-Build
+
+Post build action is executed at compile time for `BlinkyApp`. In case of build for `PSOC_062_2M`, `PSOC_062_1M`, `PSOC_062_512K` platforms it calls `imgtool` from `MCUBoot` scripts and adds signature to compiled image.
+
+Flags passed to `imgtool` for signature are defined in `SIGN_ARGS` variable in BlinkyApp.mk.
+
+### How To Program An Application
+
+There are couple ways of programming BlinkyApp firmware. Following instructions assume usage of one of Cypress development kits, for example `CY8CPROTO_062_4343W`.
+
+1. Direct usage of OpenOCD.
+
+OpenOCD package is supplied with ModusToolbox IDE and can be found in installation folder under `./tools_2.1/openocd`.
+
+Open terminal application -  and execute following command after substitution `PATH_TO_APPLICATION.hex` and `OPENOCD` paths.
+
+Connect a board to your computer. Switch Kitprog3 to DAP-BULK mode by pressing `SW3 MODE` button until `LED2 STATUS` constantly shines.
+
+        export OPENOCD=/Applications/ModusToolbox/tools_2.1/openocd 
+
+        ${OPENOCD}/bin/openocd -s ${OPENOCD}/scripts \
+                            -f ${OPENOCD}/scripts/interface/kitprog3.cfg \
+                            -f ${OPENOCD}/scripts/target/psoc6_2m.cfg \
+                            -c "init; reset init; program PATH_TO_APPLICATION.hex" \
+                            -c "resume; reset; exit" 
+
+2. Using GUI tool `Cypress Programmer`
+
+Follow [link](https://www.cypress.com/products/psoc-programming-solutions) to download.
+
+Connect board to your computer. Switch Kitprog3 to DAP-BULK mode by pressing `SW3 MODE` button until `LED2 STATUS` constantly shines. Open `Cypress Programmer` and click `Connect`, then choose hex file: `MCUBootApp.hex` or `BlinkyApp.hex` and click `Program`.  Check log to ensure programming success. Reset board.
+
+3. Using `DAPLINK`.
+
+Connect board to your computer. Switch embeded  Kitprog3 to `DAPLINK` mode by pressing `SW3 MODE` button until `LED2 STATUS` blinks fast and mass storage device appeared in OS. Drag and drop `hex` files you wish to program to `DAPLINK` drive in your OS.
+
+**Hex file names to use for programming**
+
+`BlinkyApp` always produce build artifacts in 2 separate folders: `BlinkyApp/out/PSOC_062_2M/Debug/boot` and `BlinkyApp/out/PSOC_062_2M/Debug/upgrade`.
+
+These files are ready to be flashed to the board:
+
+* **BlinkyApp.hex** from `boot` folder
+* **BlinkyApp_upgrade.hex** from `upgrade` folder 
+
+`BlinkyApp_unsigned.hex` hex file is also preserved in both cases for possible troubleshooting.
+
+**Important: When swap status upgrade mode used**
+
+ In case of using this application in a system with `swap` type of upgrade refer first to `MCUBootApp.md` section **SWAP/Expected lifecycle**.
+
+**BlinkyApp.hex** should be programmed to a device once. All firmware upgrades should be delivered using secondary (UPGRADE) slot thus **BlinkyApp_upgrade.hex** image.
+
+If user for some reason tries to program **BlinkyApp.hex** to primary slot directly second time - **system state should be reset**.
+
+To reset system state at least `swap status partition` area in flash should be erased - see addresses in `MCUBootApp.md` paragraph **Memory maps**.
+
+To erase swap status partition area in MCUBootApp with a single image configuration with default memory map using `OpenOCD` execute command:
+
+    $OPENOCD_PATH/bin/openocd -s "$OPENOCD_PATH/scripts" -f "$OPENOCD_PATH/ scripts/interface/kitprog3.cfg" -f "$OPENOCD_PATH/scripts/target/psoc6_2m.cfg" -c "init; reset init" -c "flash erase_address 0x10038000 0x1000" -c "reset; shutdown"
+
+To erase swap status partition area in MCUBootApp with a multi image configuration with default memory map using `OpenOCD` execute command:
+
+    $OPENOCD_PATH/bin/openocd -s "$OPENOCD_PATH/scripts" -f "$OPENOCD_PATH/ scripts/interface/kitprog3.cfg" -f "$OPENOCD_PATH/scripts/target/psoc6_2m.cfg" -c "init; reset init" -c "flash erase_address 0x10078000 0x2000" -c "reset; shutdown"
+
+In both cases it is easier to erase all device flash or all flash after MCUBootApp. This command erases all flash after MCUBootApp including primary, secondary and swap status partiton.
+
+    $OPENOCD_PATH/bin/openocd -s "$OPENOCD_PATH/scripts" -f "$OPENOCD_PATH/ scripts/interface/kitprog3.cfg" -f "$OPENOCD_PATH/scripts/target/psoc6_2m.cfg" -c "init; reset init" -c "flash erase_address 0x10018000" -c "reset; shutdown"
+
+### Example Terminal Output
+
+When user application programmed in BOOT slot:
+
+    ===========================
+    [BlinkyApp] BlinkyApp v1.0 [CM4]
+    ===========================
+    [BlinkyApp] GPIO initialized 
+    [BlinkyApp] UART initialized 
+    [BlinkyApp] Retarget I/O set to 115200 baudrate 
+    [BlinkyApp] Red led blinks with 1 sec period
+    [BlinkyApp] Update watchdog timer started in MCUBootApp to mark sucessful start of user app
+    [BlinkyApp] Turn off watchdog timer
+
+When user application programmed in UPRADE slot and upgrade procedure was successful:
+
+    ===========================
+    [BlinkyApp] BlinkyApp v2.0 [+]
+    ===========================
+    [BlinkyApp] GPIO initialized 
+    [BlinkyApp] UART initialized 
+    [BlinkyApp] Retarget I/O set to 115200 baudrate 
+    [BlinkyApp] Red led blinks with 0.25 sec period
+    [BlinkyApp] Update watchdog timer started in MCUBootApp to mark sucessful start of user app
+    [BlinkyApp] Turn off watchdog timer
+    [BlinkyApp] Try to set img_ok to confirm upgrade image
+    [BlinkyApp] SWAP Status : Image OK was set at 0x10027fe8.
diff --git a/boot/cypress/BlinkyApp/BlinkyApp.mk b/boot/cypress/BlinkyApp/BlinkyApp.mk
index 5ff2080..edfaed6 100644
--- a/boot/cypress/BlinkyApp/BlinkyApp.mk
+++ b/boot/cypress/BlinkyApp/BlinkyApp.mk
@@ -39,6 +39,10 @@
 # image type can be BOOT or UPGRADE
 IMG_TYPES = BOOT UPGRADE
 
+# use SWAP_UPGRADE = 0 for overwrite only mode
+# use SWAP_UPGRADE = 1 for swap upgrade mode
+SWAP_UPGRADE ?= 1
+
 # possible values are 0 and 0xff
 # internal Flash by default
 ERASED_VALUE ?= 0
@@ -58,6 +62,7 @@
 	DEFINES_APP := -DBOOT_IMG
 else
 	DEFINES_APP := -DUPGRADE_IMG
+	DEFINES_APP += -DSWAP_ENABLED=$(SWAP_UPGRADE)
 endif
 
 # Define start of application, RAM start and size, slot size
@@ -71,12 +76,15 @@
 	DEFINES_APP += -DRAM_START=0x08020000
 	DEFINES_APP += -DRAM_SIZE=0x10000
 endif
+ifeq ($(USE_EXTERNAL_FLASH), 1)
+$(warning You are trying to build BlinkyApp for MCUBootApp with external memory support. Ensure you build MCUBootApp with USE_EXTERNAL_FLASH=1 flag!)
+	SLOT_SIZE ?= 0x40200
+else
+	SLOT_SIZE ?= 0x10000
+endif
 
-
-DEFINES_APP += -DRAM_SIZE=0x10000
+DEFINES_APP += -DUSER_APP_SIZE=$(SLOT_SIZE)
 DEFINES_APP += -DUSER_APP_START=0x10018000
-SLOT_SIZE ?= 0x10000
-
 
 # Collect Test Application sources
 SOURCES_APP_SRC := $(wildcard $(CUR_APP_PATH)/*.c)
@@ -100,7 +108,12 @@
 # We still need this for MCUBoot apps signing
 IMGTOOL_PATH ?=	../../scripts/imgtool.py
 
-SIGN_ARGS := sign --header-size 1024 --pad-header --align 8 -v "2.0" -S $(SLOT_SIZE) -M 512 --overwrite-only -R $(ERASED_VALUE) -k keys/$(SIGN_KEY_FILE).pem
+# add flag to imgtool if not using swap for upgrade
+ifeq ($(SWAP_UPGRADE), 0)
+UPGRADE_TYPE := --overwrite-only
+endif
+
+SIGN_ARGS := sign --header-size 1024 --pad-header --align 8 -v "2.0" -S $(SLOT_SIZE) -M 512 $(UPGRADE_TYPE) -R $(ERASED_VALUE) -k keys/$(SIGN_KEY_FILE).pem
 
 # Output folder
 OUT := $(APP_NAME)/out
@@ -113,6 +126,7 @@
 ifeq ($(IMG_TYPE), UPGRADE)
 	ifeq ($(ENC_IMG), 1)
 		SIGN_ARGS += --encrypt ../../$(ENC_KEY_FILE).pem
+		SIGN_ARGS += --use-random-iv
 	endif
 	SIGN_ARGS += --pad
 	UPGRADE_SUFFIX :=_upgrade
diff --git a/boot/cypress/BlinkyApp/Readme.md b/boot/cypress/BlinkyApp/Readme.md
deleted file mode 100644
index b10dda4..0000000
--- a/boot/cypress/BlinkyApp/Readme.md
+++ /dev/null
@@ -1,165 +0,0 @@
-### Blinking LED test application for MCUBoot Bootloader
-
-### Description
-
-Implements simple Blinky LED CM4 application to demonstrate MCUBoot Application operation in terms of BOOT and UPGRADE process.
-
-It is started by MCUBoot Application which is running on CM0p.
-
-Functionality:
-
-* Blinks RED led with 2 different rates, depending on type of image - BOOT or UPGRADE.
-* Prints debug info and version of itself to terminal at 115200 baud.
-* Can be built for BOOT slot or UPGRADE slot of bootloader.
-
-Currently supported platforms
-
-* PSOC_062_2M
-* PSOC_062_1M
-* PSOC_062_512K
-
-### Hardware limitations
-
-Since this application is created to demonstrate MCUBoot library features and not as reference examples some considerations are taken.
-
-1. Port/pin `P5_0` and `P5_1` used to configure serial port for debug prints. These pins are the most commonly used for serial port connection among available Cypress PSoC 6 kits. If you try to use custom hardware with this application - change definitions of `CY_DEBUG_UART_TX` and `CY_DEBUG_UART_RX` in `main.c` of BlinkyApp to port/pin pairs corresponding to your design.
-2. Port `GPIO_PRT13` pin `7U` used to define user connection LED. This pin is the most commonly used for USER_LED connection among available Cypress PSoC 6 kits. If you try to use custom hardware with this application - change definitions of `LED_PORT` and `LED_PIN` in `main.c` of BlinkyApp to port/pin pairs corresponding to your design.
-
-### Pre-build action
-
-Pre-build action is implemented for defining start address and size of flash, as well as RAM start address and size for BlinkyApp.
-These values are set by specifing following macros: `-DUSER_APP_SIZE`, `-DUSER_APP_START`, `-DRAM_SIZE`, `-DRAM_START` in makefile.
-
-Pre-build action calls GCC preprocessor which intantiates defines for particular values in `BlinkyApp_template.ld`.
-
-Default values set for currently supported targets:
-* `BlinkyApp.mk` to `-DUSER_APP_START=0x10018000`
-
-**Important**: make sure RAM areas of CM4-based BlinkyApp and CM0p-based MCUBootApp bootloader do not overlap.
-Memory (stack) corruption of CM0p application can cause failure if SystemCall-served operations invoked from CM4.
-
-### Building an application
-
-Root directory for build is **boot/cypress.**
-
-The following command will build regular HEX file of a BlinkyApp for BOOT slot. Substitute `PLATFORM=` to a paltform name you use in all following commands.
-
-    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=BOOT
-
-This have following defaults suggested:
-
-    BUILDCFG=Debug
-    IMG_TYPE=BOOT
-
-To build UPGRADE image use following command:
-
-    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x10000
-
-    Note: HEADER_OFFSET=%SLOT_SIZE%
-
-Example command-line for single-image:
-
-    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=BOOT
-
-**Building Multi-Image**
-
-`BlinkyApp` can be built to use in multi-image bootloader configuration.
-
-To get appropriate artifacts to use with multi image MCUBootApp, makefile flag `HEADER_OFFSET=` can be used.
-
-Example usage:
-
-Considering default config:
-
-* first image BOOT (PRIMARY) slot start `0x10018000`
-* slot size `0x10000`
-* second image BOOT (PRIMARY) slot start `0x10038000`
-
-To get appropriate artifact for second image PRIMARY slot run this command:
-
-    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=BOOT HEADER_OFFSET=0x20000
-
-*Note:* only 2 images are supported at the moment.
-
-**How to build upgrade image for external memory:**
-
-To prepare MCUBootApp for work with external memory please refer to `MCUBootApp/ExternalMemory.md`.
-
-For build BlinkyApp upgrade image for external memory use command:
-
-    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x7FE8000 ERASED_VALUE=0xff
-
-`HEADER_OFFSET` defines the offset from original boot image address. This one in line above suggests secondary slot will start from `0x18000000`.
-
-`ERASED_VALUE` defines the memory cell contents in erased state. It is `0x00` for PSoC6's internal Flash and `0xff` for S25FL512S.
-
-In case of using muti-image configuration, upgrade image for second application can be built using next command:
-
-    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x8028000 ERASED_VALUE=0xff
-
-    Note: for S25FL512S block address shuld be mutiple by 0x40000
-
-**How to build encrypted upgrade image :**
-
-To prepare MCUBootApp for work with encrypted upgrade image please refer to `MCUBootApp/Readme.md`.
-
-To obtain encrypted upgrade image of BlinkyApp extra flag `ENC_IMG=1` should be passed in command line, for example:
-
-    make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x20000 ENC_IMG=1
-
-This also suggests user already placed corresponing `*.pem` key in `\keys` folder. The key variables are defined in root `Makefile` as `SIGN_KEY_FILE` and `ENC_KEY_FILE`
-
-### Post-Build
-
-Post build action is executed at compile time for `BlinkyApp`. In case of build for `PSOC_062_2M` platform it calls `imgtool` from `MCUBoot` scripts and adds signature to compiled image.
-
-Flags passed to `imgtool` for signature are defined in `SIGN_ARGS` variable in BlinkyApp.mk.
-
-### How to program an application
-
-Use any preferred tool for programming hex files.
-
-Hex file names to use for programming:
-
-`BlinkyApp` always produce build artifacts in 2 separate folders - `boot` and `upgrade`.
-
-`BlinkyApp` built to run with `MCUBootApp` produces files with name BlinkyApp.hex in `boot` directory and `BlinkyApp_upgrade.hex` in `upgrade` folder. These files are ready to be flashed to the board.
-
-`BlinkyApp_unsigned.hex` hex file is also preserved in both cases for possible troubleshooting.
-
-Files to use for programming are:
-
-`BOOT` - boot/BlinkyApp.hex
-`UPGRADE` - upgrade/BlinkyApp_upgrade.hex
-
-**Flags:**
-- `BUILDCFG` - configuration **Release** or **Debug**
-- `MAKEINFO` - 0 (default) - less build info, 1 - verbose output of compilation.
-- `HEADER_OFFSET` - 0 (default) - no offset of output hex file, 0x%VALUE% - offset for output hex file. Value 0x10000 is slot size MCUBoot Bootloader in this example.
-- `IMG_TYPE` - `BOOT` (default) - build image for BOOT slot of MCUBoot Bootloader, `UPGRADE` - build image for UPGRADE slot of MCUBoot Bootloader.
-- `ENC_IMG` - 0 (default) - build regular upgrade image, `1` - build encrypted upgrade image (MCUBootApp should also be built with this flash set 1)
-
-**NOTE**: In case of `UPGRADE` image `HEADER_OFFSET` should be set to MCUBoot Bootloader slot size.
-
-### Example terminal output
-
-When user application programmed in BOOT slot:
-
-    ===========================
-    [BlinkyApp] BlinkyApp v1.0 [CM4]
-    ===========================
-    [BlinkyApp] GPIO initialized
-    [BlinkyApp] UART initialized
-    [BlinkyApp] Retarget I/O set to 115200 baudrate
-    [BlinkyApp] Red led blinks with 1 sec period
-
-When user application programmed in UPRADE slot and upgrade procedure was successful:
-
-    ===========================
-    [BlinkyApp] BlinkyApp v2.0 [+]
-    ===========================
-
-    [BlinkyApp] GPIO initialized
-    [BlinkyApp] UART initialized
-    [BlinkyApp] Retarget I/O set to 115200 baudrate
-    [BlinkyApp] Red led blinks with 0.25 sec period
diff --git a/boot/cypress/BlinkyApp/libs.mk b/boot/cypress/BlinkyApp/libs.mk
index 7994d13..6e1ba7a 100644
--- a/boot/cypress/BlinkyApp/libs.mk
+++ b/boot/cypress/BlinkyApp/libs.mk
@@ -58,3 +58,4 @@
 INCLUDE_DIRS_LIBS += $(addprefix -I,$(INCLUDE_DIRS_RETARGET_IO))
 INCLUDE_DIRS_LIBS += $(addprefix -I,$(INCLUDE_DIRS_WATCHDOG))
 INCLUDE_DIRS_LIBS += $(addprefix -I,$(INCLUDE_DIRS_HAL))
+INCLUDE_DIRS_LIBS += $(addprefix -I,$(INCLUDE_DIRS_WATCHDOG))
diff --git a/boot/cypress/BlinkyApp/main.c b/boot/cypress/BlinkyApp/main.c
index 97e1c84..1679bf2 100644
--- a/boot/cypress/BlinkyApp/main.c
+++ b/boot/cypress/BlinkyApp/main.c
@@ -89,6 +89,31 @@
     }
 }
 
+/*
+* Writes 1 byte `src` into flash memory at `address`
+* It does a sequence of RD/Modify/WR of data in a Flash Row.
+ */
+int flash_write_byte(uint32_t address, uint8_t src)
+{
+    cy_en_flashdrv_status_t rc = CY_FLASH_DRV_SUCCESS;
+    uint32_t row_addr = 0;
+    uint8_t row_buff[512];
+
+    /* accepting arbitrary address */
+    row_addr = (address/CY_FLASH_SIZEOF_ROW)*CY_FLASH_SIZEOF_ROW;
+
+    /* preserving Row */
+    memcpy(row_buff, (void *)row_addr, sizeof(row_buff));
+
+    /* Modifying the target byte */
+    row_buff[address%CY_FLASH_SIZEOF_ROW] = src;
+
+    /* Programming updated row back */
+    rc = Cy_Flash_WriteRow(row_addr, (const uint32_t *)row_buff);
+
+    return (int) rc;
+}
+
 void test_app_init_hardware(void)
 {
     /* enable interrupts */
@@ -124,6 +149,35 @@
     printf(WATCHDOG_FREE_MESSAGE);
     cy_wdg_free();
 
+#if defined(SWAP_ENABLED) && defined(UPGRADE_IMG)
+
+    #define USER_SWAP_IMAGE_OK_OFFS (24)
+    #define USER_SWAP_IMAGE_OK      (1)
+    uint32_t img_ok_addr;
+    int rc;
+
+    printf("[BlinkyApp] Try to set img_ok to confirm upgrade image\r\n");
+
+    /* Write Image OK flag to the slot trailer, so MCUBoot-loader
+     * will not revert new image */
+    img_ok_addr = USER_APP_START + USER_APP_SIZE - USER_SWAP_IMAGE_OK_OFFS;
+    if (*((uint8_t *)img_ok_addr) != USER_SWAP_IMAGE_OK)
+    {
+        rc = flash_write_byte(img_ok_addr, USER_SWAP_IMAGE_OK);
+        if (0 == rc)
+        {
+            printf("[BlinkyApp] SWAP Status : Image OK was set at 0x%08lx.\r\n", img_ok_addr);
+        }
+        else
+        {
+            printf("[BlinkyApp] SWAP Status : Failed to set Image OK.\r\n");
+        }
+    } else
+    {
+        printf("[BlinkyApp] Img_ok is already set in trailer\r\n");
+    }
+#endif
+
     for (;;)
     {
         /* Toggle the user LED periodically */