Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 1 | ### Port of MCUBoot library to be used with Cypress targets |
| 2 | |
| 3 | **Solution Description** |
| 4 | |
| 5 | Given solution demonstrates operation of MCUBoot on Cypress' PSoC6 device. |
| 6 | |
| 7 | There are two applications implemented: |
| 8 | * MCUBootApp - PSoC6 MCUBoot-based bootloading application; |
| 9 | * BlinkyApp - simple PSoC6 blinking LED application which is a target of BOOT/UPGRADE; |
| 10 | |
| 11 | The demonstration device is CY8CPROTO-062-4343W board which is PSoC6 device with 2M of Flash available. |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 12 | The default flash map implemented is the following: |
| 13 | |
Bohdan Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame^] | 14 | Single-image mode. |
| 15 | |
| 16 | `[0x10000000, 0x10018000]` - MCUBootApp (bootloader) area; |
| 17 | |
| 18 | `[0x10018000, 0x10028000]` - primary slot for BlinkyApp; |
| 19 | |
| 20 | `[0x10028000, 0x10038000]` - secondary slot for BlinkyApp; |
| 21 | |
| 22 | `[0x10038000, 0x10039000]` - scratch area (not used); |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 23 | |
| 24 | Size of slots `0x10000` - 64kb |
| 25 | |
Bohdan Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame^] | 26 | 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. |
| 27 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 28 | **Important**: make sure primary, secondary slot and bootloader app sizes are appropriate and correspond to flash area size defined in Applications' linker files. |
| 29 | |
Bohdan Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame^] | 30 | **Important**: make sure RAM areas of CM0p-based MCUBootApp bootloader and CM4-based BlinkyApp do not overlap. |
| 31 | Memory (stack) corruption of CM0p application can cause failure if SystemCall-served operations invoked from CM4. |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 32 | |
Roman Okhrimenko | 4c1c5b0 | 2020-01-31 16:22:39 +0200 | [diff] [blame] | 33 | **Hardware cryptography acceleration:** |
| 34 | |
| 35 | Cypress PSOC6 MCU family supports hardware acceleration of cryptography based on mbedTLS Library via shim layer. Implementation of this layer is supplied as separate submodule `cy-mbedtls-acceleration`. HW acceleration of cryptography shortens boot time more then 4 times, comparing to software implementation (observation results). |
| 36 | |
| 37 | To enable hardware acceleration in `MCUBootApp` pass flag `USE_CRYPTO_HW=1` to `make` while build. |
| 38 | |
| 39 | Hardware acceleration of cryptography is enabled for PSOC6 devices by default. |
| 40 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 41 | **How to modify Flash map:** |
| 42 | |
| 43 | __Option 1.__ |
| 44 | |
| 45 | Navigate to `sysflash.h` and modify the flash area(s) / slots sizes to meet your needs. |
| 46 | |
| 47 | __Option 2.__ |
| 48 | |
| 49 | Navigate to `sysflash.h`, uncomment `CY_FLASH_MAP_EXT_DESC` definition. |
| 50 | Now define and initialize `struct flash_area *boot_area_descs[]` with flash memory addresses and sizes you need at the beginning of application, so flash APIs from `cy_flash_map.c` will use it. |
| 51 | |
| 52 | __Note:__ for both options make sure you have updated `MCUBOOT_MAX_IMG_SECTORS` appropriatery with sector size assumed to be 512. |
| 53 | |
| 54 | **How to override the flash map values during build process:** |
| 55 | |
| 56 | Navigate to MCUBootApp.mk, find section `DEFINES_APP +=` |
| 57 | Update this line and or add similar for flash map parameters to override. |
| 58 | |
| 59 | The possible list could be: |
| 60 | |
| 61 | * MCUBOOT_MAX_IMG_SECTORS |
| 62 | * CY_FLASH_MAP_EXT_DESC |
| 63 | * CY_BOOT_SCRATCH_SIZE |
| 64 | * CY_BOOT_BOOTLOADER_SIZE |
| 65 | * CY_BOOT_PRIMARY_1_SIZE |
| 66 | * CY_BOOT_SECONDARY_1_SIZE |
| 67 | * CY_BOOT_PRIMARY_2_SIZE |
| 68 | * CY_BOOT_SECONDARY_2_SIZE |
| 69 | |
| 70 | As an example in a makefile it should look like following: |
| 71 | |
| 72 | `DEFINES_APP +=-DCY_FLASH_MAP_EXT_DESC` |
| 73 | |
| 74 | `DEFINES_APP +=-DMCUBOOT_MAX_IMG_SECTORS=512` |
| 75 | |
| 76 | `DEFINES_APP +=-DCY_BOOT_PRIMARY_1_SIZE=0x15000` |
| 77 | |
| 78 | **Multi-Image Operation** |
| 79 | |
| 80 | Multi-image operation considers upgrading and verification of more then one image on the device. |
| 81 | |
Bohdan Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame^] | 82 | 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`. |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 83 | |
| 84 | Default value of `MCUBOOT_IMAGE_NUMBER` is 1, which corresponds to single image configuratios. |
| 85 | |
| 86 | In multi-image operation (two images are considered for simplicity) MCUBoot Bootloader application operates as following: |
| 87 | |
| 88 | * Verifies Primary_1 and Primary_2 images; |
| 89 | * Verifies Secondary_1 and Secondary_2 images; |
| 90 | * Upgrades Secondary to Primary if valid images found; |
| 91 | * Boots image from Primary_1 slot only; |
| 92 | * Boots Primary_1 only if both - Primary_1 and Primary_2 are present and valid; |
| 93 | |
| 94 | This ensures two dependent applications can be accepted by device only in case both images are valid. |
| 95 | |
| 96 | **Default Flash map for Multi-Image operation:** |
| 97 | |
| 98 | `0x10000000 - 0x10018000` - MCUBoot Bootloader |
| 99 | |
| 100 | `0x10018000 - 0x10028000` - Primary_1 (BOOT) slot of Bootloader |
| 101 | |
| 102 | `0x10028000 - 0x10038000` - Secondary_1 (UPGRADE) slot of Bootloader |
| 103 | |
| 104 | `0x10038000 - 0x10048000` - Primary_2 (BOOT) slot of Bootloader |
| 105 | |
| 106 | `0x10048000 - 0x10058000` - Secondary_2 (UPGRADE) slot of Bootloader |
| 107 | |
Bohdan Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame^] | 108 | `0x10058000 - 0x10059000` - Scratch of Bootloader |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 109 | |
| 110 | Size of slots `0x10000` - 64kb |
| 111 | |
Bohdan Kovalchuk | a333a45 | 2020-07-09 16:55:58 +0300 | [diff] [blame^] | 112 | __Note:__ It is also possible to place secondary (upgrade) slots in external memory module so resulting image size can be doubled. |
| 113 | For more details about External Memory usage, please refer to separate guiding document `ExternalMemory.md`. |
| 114 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 115 | **Downloading Solution's Assets** |
| 116 | |
| 117 | There is a set assets required: |
| 118 | |
| 119 | * MCUBooot Library (root repository) |
| 120 | * PSoC6 HAL Library |
| 121 | * PSoC6 Peripheral Drivers Library (PDL) |
| 122 | * mbedTLS Cryptographic Library |
| 123 | |
| 124 | To get submodules - run the following command: |
| 125 | |
| 126 | git submodule update --init --recursive |
| 127 | |
| 128 | **Building Solution** |
| 129 | |
| 130 | This folder contains make files infrastructure for building MCUBoot Bootloader. Same approach used in sample BlinkyLedApp application. Example command are provided below for couple different build configurations. |
| 131 | |
| 132 | * Build MCUBootApp in `Debug` for signle image use case. |
| 133 | |
| 134 | make app APP_NAME=MCUBootApp PLATFORM=PSOC_062_2M BUILDCFG=Debug MCUBOOT_IMAGE_NUMBER=1 |
| 135 | |
| 136 | * Build MCUBootApp in `Release` for multi image use case. |
| 137 | |
| 138 | make app APP_NAME=MCUBootApp PLATFORM=PSOC_062_2M BUILDCFG=Release MCUBOOT_IMAGE_NUMBER=2 |
| 139 | |
| 140 | Root directory for build is **boot/cypress.** |
| 141 | |
Bohdan Kovalchuk | 7725652 | 2020-04-15 18:03:43 +0300 | [diff] [blame] | 142 | **Programming solution** |
| 143 | |
| 144 | There are couple ways of programming hex of MCUBootApp and BlinkyApp. Following instructions assume one of Cypress development kits, for example `CY8CPROTO_062_4343W`. |
| 145 | |
| 146 | 1. Direct usage of OpenOCD. |
| 147 | OpenOCD package is supplied with ModuToolbox IDE and can be found in installation folder under `./tools_2.1/openocd`. |
| 148 | Open terminal application - and execute following command after substitution `PATH_TO_APPLICATION.hex` and `OPENOCD` paths. |
| 149 | |
| 150 | Connect a board to your computer. Switch Kitprog3 to DAP-BULK mode by pressing `SW3 MODE` button until `LED2 STATUS` constantly shines. |
| 151 | |
| 152 | export OPENOCD=/Applications/ModusToolbox/tools_2.1/openocd |
| 153 | |
| 154 | ${OPENOCD}/bin/openocd -s ${OPENOCD}/scripts \ |
| 155 | -f ${OPENOCD}/scripts/interface/kitprog3.cfg \ |
| 156 | -f ${OPENOCD}/scripts/target/psoc6_2m.cfg \ |
| 157 | -c "init; reset init; program PATH_TO_APPLICATION.hex" \ |
| 158 | -c "resume; reset; exit" |
| 159 | |
| 160 | 2. Using GUI tool `Cypress Programmer` - follow [link](https://www.cypress.com/products/psoc-programming-solutions) to download. |
| 161 | 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. |
| 162 | |
| 163 | 3. Using `DAPLINK`. |
| 164 | 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. |
| 165 | |
| 166 | |
| 167 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 168 | **Currently supported platforms:** |
| 169 | |
| 170 | * PSOC_062_2M |
| 171 | |
| 172 | **Build environment troubleshooting:** |
| 173 | |
| 174 | Regular shell/terminal combination on Linux and MacOS. |
| 175 | |
| 176 | On Windows: |
| 177 | |
| 178 | * Cygwin |
| 179 | * Msys2 |
| 180 | |
| 181 | Also IDE may be used: |
| 182 | * Eclipse / ModusToolbox ("makefile project from existing source") |
| 183 | |
| 184 | *Make* - make sure it is added to system's `PATH` variable and correct path is first in the list; |
| 185 | |
| 186 | *Python/Python3* - make sure you have correct path referenced in `PATH`; |
| 187 | |
| 188 | *Msys2* - to use systems PATH navigate to msys2 folder, open `msys2_shell.cmd`, uncomment set `MSYS2_PATH_TYPE=inherit`, restart MSYS2 shell. |
| 189 | |
Roman Okhrimenko | aa7c021 | 2020-03-24 23:33:00 +0200 | [diff] [blame] | 190 | *Cygwin* - add following to build command `CURDIR=pwd | cygpath --mixed -f -` so that build command looks like that: |
| 191 | |
| 192 | make app APP_NAME=MCUBootApp PLATFORM=PSOC_062_2M CURDIR=`pwd | cygpath --mixed -f -` |
| 193 | |
Roman Okhrimenko | 89ecdac | 2020-02-28 17:05:55 +0200 | [diff] [blame] | 194 | This will iherit system's PATH so should find `python3.7` installed in regular way as well as imgtool and its dependencies. |
| 195 | |