Cypress: fix flash_area_write, warnings, improve make, readme

Signed-off-by: Bohdan Kovalchuk <bohd@cypress.com>
diff --git a/boot/cypress/BlinkyApp/Readme.md b/boot/cypress/BlinkyApp/Readme.md
index 5f8ca86..5486088 100644
--- a/boot/cypress/BlinkyApp/Readme.md
+++ b/boot/cypress/BlinkyApp/Readme.md
@@ -26,6 +26,9 @@
 Default values set for currently supported targets:
 * PSOC_062_2M in `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.**
@@ -71,7 +74,7 @@
 
 **How to build upgrade image for external memory:**
 
-For prepare MCUBoot to work with external memory please refer to MCUBoot/ExternalMemory.md
+To prepare MCUBootApp for work with external memory please refer to `MCUBootApp/ExternalMemory.md`.
 
 For build BlinkyApp upgarde image for external memory use command:
 
diff --git a/boot/cypress/MCUBootApp/ExternalMemory.md b/boot/cypress/MCUBootApp/ExternalMemory.md
index d2da582..8cd7367 100644
--- a/boot/cypress/MCUBootApp/ExternalMemory.md
+++ b/boot/cypress/MCUBootApp/ExternalMemory.md
@@ -23,10 +23,30 @@
 This requirement is accepted for code simplicity.
 
 The default flash map implemented is the following:
-* [0x10000000, 0x10018000] - MCUBootApp (bootloader) area;
-* [0x10018000, 0x10028000] - primary slot for BlinkyApp;
-* [0x18000000, 0x18010000] - secondary slot for BlinkyApp;
-* [0x10038000, 0x10039000] - scratch area (not used);
+
+Single-image mode.
+
+`[0x10000000, 0x10018000]` - MCUBootApp (bootloader) area;
+
+`[0x10018000, 0x10028000]` - primary slot for BlinkyApp;
+
+`[0x18000000, 0x18010000]` - secondary slot for BlinkyApp;
+
+`[0x10038000, 0x10039000]` - scratch area (not used);
+
+Multi(dual)-image mode.
+
+`[0x10000000, 0x10018000]` - MCUBootApp (bootloader) area;
+
+`[0x10018000, 0x10028000]` - primary1 slot for BlinkyApp;
+
+`[0x18000000, 0x18010000]` - secondary1 slot for BlinkyApp;
+
+`[0x10038000, 0x10048000]` - primary2 slot for user app ;
+
+`[0x18040000, 0x18050000]` - secondary2 slot for user app;
+
+`[0x10058000, 0x10059000]` - scratch area (not used);
 
 Size of slots `0x10000` - 64kB
 
@@ -47,6 +67,8 @@
 3. Define which slave select is used for external memory on a board by setting `smif_id` value in `main.c`.
 4. Build MCUBootApp as described in `Readme.md`.
 
+**Note 3**: External memory code is developed basing on PDL and can be run on CM0p core only. It may require modifications if used on CM4.
+
 **How to build upgrade image for external memory:**
 
     make app APP_NAME=BlinkyApp PLATFORM=PSOC_062_2M IMG_TYPE=UPGRADE HEADER_OFFSET=0x7FE8000 ERASED_VALUE=0xff
diff --git a/boot/cypress/MCUBootApp/MCUBootApp.mk b/boot/cypress/MCUBootApp/MCUBootApp.mk
index 7cd9450..a760848 100644
--- a/boot/cypress/MCUBootApp/MCUBootApp.mk
+++ b/boot/cypress/MCUBootApp/MCUBootApp.mk
@@ -41,6 +41,9 @@
 include $(CUR_APP_PATH)/libs.mk
 include $(CUR_APP_PATH)/toolchains.mk
 
+# default slot size is 0x10000, 512bytes per row/sector, so 128 sectors
+MAX_IMG_SECTORS ?= 128
+
 # Application-specific DEFINES
 DEFINES_APP := -DMBEDTLS_CONFIG_FILE="\"mcuboot_crypto_config.h\""
 DEFINES_APP += -DECC256_KEY_FILE="\"keys/$(SIGN_KEY_FILE).pub\""
@@ -49,6 +52,7 @@
 ifeq ($(USE_EXTERNAL_FLASH), 1)
 DEFINES_APP += -DCY_BOOT_USE_EXTERNAL_FLASH
 endif
+DEFINES_APP += -DMCUBOOT_MAX_IMG_SECTORS=$(MAX_IMG_SECTORS)
 
 ifeq ($(USE_CRYPTO_HW), 1)
 DEFINES_APP += -DMBEDTLS_USER_CONFIG_FILE="\"mcuboot_crypto_acc_config.h\""
diff --git a/boot/cypress/MCUBootApp/README.md b/boot/cypress/MCUBootApp/README.md
index 9db4b4b..2da2d7c 100644
--- a/boot/cypress/MCUBootApp/README.md
+++ b/boot/cypress/MCUBootApp/README.md
@@ -9,19 +9,26 @@
 * BlinkyApp - simple PSoC6 blinking LED application which is a target of BOOT/UPGRADE;
 
 The demonstration device is CY8CPROTO-062-4343W board which is PSoC6 device with 2M of Flash available.
-
 The default flash map implemented is the following:
 
-* [0x10000000, 0x10018000] - MCUBootApp (bootloader) area;
-* [0x10018000, 0x10028000] - primary slot for BlinkyApp;
-* [0x10028000, 0x10038000] - secondary slot for BlinkyApp;
-* [0x10038000, 0x10039000] - scratch area;
+Single-image mode.
+
+`[0x10000000, 0x10018000]` - MCUBootApp (bootloader) area;
+
+`[0x10018000, 0x10028000]` - primary slot for BlinkyApp;
+
+`[0x10028000, 0x10038000]` - secondary slot for BlinkyApp;
+
+`[0x10038000, 0x10039000]` - scratch area (not used);
 
 Size of slots `0x10000` - 64kb
 
+MCUBootApp checks image integrity with SHA256, image authenticity with EC256 digital signature verification and uses completely SW implementation of cryptographic functions based on mbedTLS Library.
+
 **Important**: make sure primary, secondary slot and bootloader app sizes are appropriate and correspond to flash area size defined in Applications' linker files.
 
-MCUBootApp checks image integrity with SHA256, image authenticity with EC256 digital signature verification and uses completely SW implementation of cryptographic functions based on mbedTLS Library.
+**Important**: make sure RAM areas of CM0p-based MCUBootApp bootloader and CM4-based BlinkyApp do not overlap.
+Memory (stack) corruption of CM0p application can cause failure if SystemCall-served operations invoked from CM4.
 
 **Hardware cryptography acceleration:**
 
@@ -72,7 +79,7 @@
 
 Multi-image operation considers upgrading and verification of more then one image on the device.
 
-To enable multi-image operation define `MCUBOOT_IMAGE_NUMBER` in `MCUBootApp/mcuboot_config.h` file should be set to 2 (only dual-image is supported at the moment). This could also be done on build time by passing `MCUBOOT_IMAGE_NUMBER=2` as parameter to `make`.
+To enable multi-image operation define `MCUBOOT_IMAGE_NUMBER` in `MCUBootApp/config/mcuboot_config.h` file should be set to 2 (only dual-image is supported at the moment). This could also be done on build time by passing `MCUBOOT_IMAGE_NUMBER=2` as parameter to `make`.
 
 Default value of `MCUBOOT_IMAGE_NUMBER` is 1, which corresponds to single image configuratios.
 
@@ -98,10 +105,13 @@
 
 `0x10048000 - 0x10058000` - Secondary_2 (UPGRADE) slot of Bootloader
 
-`0x10058000 - 0x10058100` - Scratch of Bootloader
+`0x10058000 - 0x10059000` - Scratch of Bootloader
 
 Size of slots `0x10000` - 64kb
 
+__Note:__ It is also possible to place secondary (upgrade) slots in external memory module so resulting image size can be doubled.
+For more details about External Memory usage, please refer to separate guiding document `ExternalMemory.md`.
+
 **Downloading Solution's Assets**
 
 There is a set assets required:
diff --git a/boot/cypress/MCUBootApp/main.c b/boot/cypress/MCUBootApp/main.c
index 968f0b6..8976d31 100644
--- a/boot/cypress/MCUBootApp/main.c
+++ b/boot/cypress/MCUBootApp/main.c
@@ -48,6 +48,7 @@
     app_addr = (rsp->br_image_off + rsp->br_hdr->ih_hdr_size);
 
     BOOT_LOG_INF("Starting User Application on CM4 (wait)...");
+    BOOT_LOG_INF("Start Address: 0x%08x", app_addr);
     Cy_SysLib_Delay(100);
 
     Cy_SysEnableCM4(app_addr);
@@ -60,8 +61,7 @@
 
 int main(void)
 {
-    struct boot_rsp rsp ;
-    cy_rslt_t rc = !CY_RSLT_SUCCESS;
+    struct boot_rsp rsp;
 
     init_cycfg_clocks();
     init_cycfg_peripherals();
@@ -75,6 +75,13 @@
     BOOT_LOG_INF("MCUBoot Bootloader Started");
 
 #ifdef CY_BOOT_USE_EXTERNAL_FLASH
+    cy_rslt_t rc = !CY_RSLT_SUCCESS;
+
+    #undef MCUBOOT_MAX_IMG_SECTORS
+    /* redefine number of sectors as there 2MB will be
+     * available on PSoC062-2M in case of external
+     * memory usage */
+    #define MCUBOOT_MAX_IMG_SECTORS 4096
     int smif_id = 1; /* Assume SlaveSelect_0 is used for External Memory */
     /* Acceptable values are:
     * 0 - SMIF disabled (no external memory);
diff --git a/boot/cypress/MCUBootApp/platforms.mk b/boot/cypress/MCUBootApp/platforms.mk
index d4a70c5..d454a90 100644
--- a/boot/cypress/MCUBootApp/platforms.mk
+++ b/boot/cypress/MCUBootApp/platforms.mk
@@ -48,9 +48,11 @@
 # Collect C source files for PLATFORM
 SOURCES_PLATFORM += $(wildcard $(PLATFORM_PATH)/*.c)
 SOURCES_PLATFORM := $(filter-out %/system_psoc6_cm4.c, $(SOURCES_PLATFORM))
+SOURCES_PLATFORM += $(wildcard $(PLATFORM_PATH)/retarget_io_pdl/*.c)
 
 # Collect dirrectories containing headers for PLATFORM
 INCLUDE_DIRS_PLATFORM := $(PLATFORM_PATH)
+INCLUDE_DIRS_PLATFORM += $(PLATFORM_PATH)/retarget_io_pdl
 # Collect Assembler files for PLATFORM
 # Include _01_, _02_ or _03_ PLATFORM_SUFFIX depending on device family.
 STARTUP_FILE := $(PLATFORM_PATH)/$(PLATFORM)/$(CORE)/$(COMPILER)/startup_psoc6_$(PLATFORM_SUFFIX)_cm0plus
diff --git a/boot/cypress/README.md b/boot/cypress/README.md
index 1d87605..66f9597 100644
--- a/boot/cypress/README.md
+++ b/boot/cypress/README.md
@@ -17,6 +17,9 @@
 
 The flash map is defined through sysflash.h and cy_flash_map.c.
 
+It is also possible to place secondary (upgrade) slots in external memory module so resulting image size can be doubled.
+For more details about External Memory usage, please refer to separate guiding document `MCUBootApp/ExternalMemory.md`.
+
 MCUBootApp checks image integrity with SHA256, image authenticity with EC256 digital signature verification and uses completely SW implementation of cryptographic functions based on mbedTLS Library.
 
 **Downloading Solution's Assets**
diff --git a/boot/cypress/cy_flash_pal/cy_flash_map.c b/boot/cypress/cy_flash_pal/cy_flash_map.c
index 5bfbb2b..2e56031 100644
--- a/boot/cypress/cy_flash_pal/cy_flash_map.c
+++ b/boot/cypress/cy_flash_pal/cy_flash_map.c
@@ -34,6 +34,7 @@
 #include <stdio.h>
 #include <stdint.h>
 
+#include "mcuboot_config/mcuboot_config.h"
 #include "flash_map_backend/flash_map_backend.h"
 #include <sysflash/sysflash.h>
 
@@ -122,7 +123,7 @@
 static struct flash_area secondary_2 =
 {
     .fa_id = FLASH_AREA_IMAGE_SECONDARY(1),
-    /* TODO: it is for external flash memory
+    /* it is for external flash memory
     .fa_device_id = FLASH_DEVICE_EXTERNAL_FLASH(CY_BOOT_EXTERNAL_DEVICE_INDEX), */
 #ifndef CY_BOOT_USE_EXTERNAL_FLASH
     .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
@@ -138,6 +139,9 @@
     .fa_size = CY_BOOT_SECONDARY_2_SIZE
 };
 #endif
+#endif
+
+#ifdef MCUBOOT_SWAP_USING_SCRATCH
 static struct flash_area scratch =
 {
     .fa_id = FLASH_AREA_IMAGE_SCRATCH,
@@ -172,7 +176,9 @@
     &primary_2,
     &secondary_2,
 #endif
+#ifdef MCUBOOT_SWAP_USING_SCRATCH
     &scratch,
+#endif
     NULL
 };
 #endif
@@ -205,8 +211,6 @@
         }
         i++;
     }
-
-
     return ret;
 }
 
@@ -277,18 +281,18 @@
         uint32_t row_addr = 0;
 
         assert(!(len % CY_FLASH_SIZEOF_ROW));
+        assert(!(write_start_addr % CY_FLASH_SIZEOF_ROW));
 
         row_number = (write_end_addr - write_start_addr) / CY_FLASH_SIZEOF_ROW;
         row_addr = write_start_addr;
 
         row_ptr = (uint32_t *) src;
 
-        for (uint32_t i = 1; i <= row_number + 1; i++){
-
+        for (uint32_t i = 0; i < row_number; i++)
+        {
             rc = Cy_Flash_WriteRow(row_addr, row_ptr);
-            row_addr = write_start_addr + i * (uint32_t) CY_FLASH_SIZEOF_ROW;
 
-            row_number--;
+            row_addr += (uint32_t) CY_FLASH_SIZEOF_ROW;
             row_ptr = row_ptr + CY_FLASH_SIZEOF_ROW / 4;
         }
     }
diff --git a/boot/cypress/MCUBootApp/cy_retarget_io_pdl.c b/boot/cypress/platforms/retarget_io_pdl/cy_retarget_io_pdl.c
similarity index 100%
rename from boot/cypress/MCUBootApp/cy_retarget_io_pdl.c
rename to boot/cypress/platforms/retarget_io_pdl/cy_retarget_io_pdl.c
diff --git a/boot/cypress/MCUBootApp/cy_retarget_io_pdl.h b/boot/cypress/platforms/retarget_io_pdl/cy_retarget_io_pdl.h
similarity index 100%
rename from boot/cypress/MCUBootApp/cy_retarget_io_pdl.h
rename to boot/cypress/platforms/retarget_io_pdl/cy_retarget_io_pdl.h