Using MCUBoot with nRF5340

Hello,

I try to figure out, how to correctly setup MCUboot for an nrf5340, so that:

  • Application is updatable
  • BT Stack is updatable
  • Firmware Updates are encrypted and signed
  • Updates are stored in external flash memory
  • No need to update the bootloader itself
  • Updates are stored on the external flash by some application specific protocol.

For that task, I upgraded our project to nrfconnect/sdk-nrf version v2.7.99-cs2. My current understanding is, that I want to use Sysbuild and have to define a static partition. I haven't found any comprehensive documentation, that defines exactly, which partitions are required for the given setup. So, I search for something in the examples, that could come close to my setup. The result of this example `pm_static.yml` looks like this:

app:
  address: 0x10200
  region: flash_primary
  size: 0xdfe00
mcuboot:
  address: 0x0
  region: flash_primary
  size: 0x10000
mcuboot_pad:
  address: 0x10000
  region: flash_primary
  size: 0x200
mcuboot_primary:
  address: 0x10000
  region: flash_primary
  size: 0xe0000
  span: [mcuboot_pad, app]
mcuboot_primary_app:
  address: 0x10200
  region: flash_primary
  size: 0xdfe00
  span: [app]
settings_storage:
  address: 0xf0000
  region: flash_primary
  size: 0x10000
mcuboot_primary_1:
  address: 0x0
  size: 0x40000
  device: nordic_ram_flash_controller
  region: ram_flash
mcuboot_secondary:
  address: 0x00000
  size: 0xe0000
  device: DT_CHOSEN(nordic_pm_ext_flash)
  region: external_flash
mcuboot_secondary_1:
  address: 0xe0000
  size: 0x40000
  device: DT_CHOSEN(nordic_pm_ext_flash)
  region: external_flash
external_flash:
  address: 0x120000
  size: 0x6e0000
  device: DT_CHOSEN(nordic_pm_ext_flash)
  region: external_flash
pcd_sram:
  address: 0x20000000
  size: 0x2000
  region: sram_primary

However, in the resulting `partition_manager_report`:

  external_flash (0x800000 - 8192kB): 
+------------------------------------------------+
| 0x0: mcuboot_secondary (0xe0000 - 896kB)       |
| 0xe0000: mcuboot_secondary_1 (0x40000 - 256kB) |
| 0x120000: external_flash (0x6e0000 - 7040kB)   |
+------------------------------------------------+

  flash_primary (0x100000 - 1024kB): 
+--------------------------------------------------+
| 0x0: mcuboot (0x10000 - 64kB)                    |
+---0x10000: mcuboot_primary (0xe0000 - 896kB)-----+
| 0x10000: mcuboot_pad (0x200 - 512B)              |
+---0x10200: mcuboot_primary_app (0xdfe00 - 895kB)-+
| 0x10200: app (0xdfe00 - 895kB)                   |
+--------------------------------------------------+
| 0xf0000: settings_storage (0x10000 - 64kB)       |
+--------------------------------------------------+

  otp (0x2fc - 764B): 
+------------------------------+
| 0xff8100: otp (0x2fc - 764B) |
+------------------------------+

  sram_primary (0x80000 - 512kB): 
+-----------------------------------------------+
| 0x20000000: pcd_sram (0x2000 - 8kB)           |
| 0x20002000: sram_primary (0x6e000 - 440kB)    |
| 0x20070000: rpmsg_nrf53_sram (0x10000 - 64kB) |
+-----------------------------------------------+

the partition `mcuboot_primary_1` is simply missing (and later the compilation of `nrf/modules/mcuboot/hooks/nrf53_hooks.c` fails with an undeclared symbol `PM_MCUBOOT_PRIMARY_1_ADDRESS`. 

I found this warning in the DTS parsing:

warning: FLASH_SIMULATOR (defined at drivers/flash/Kconfig.simulator:6) was assigned the value 'y'
but got the value 'n'. Check these unsatisfied dependencies: DT_HAS_ZEPHYR_SIM_FLASH_ENABLED (=n).
See http://docs.zephyrproject.org/latest/kconfig.html#CONFIG_FLASH_SIMULATOR and/or look up
FLASH_SIMULATOR in the menuconfig/guiconfig interface.

The referenced documentation just contains the sentence: "Enable the flash simulator."

What do I have to do, to use MCUBoot? 

Is there any comprehensive documentation, that clearly states, what is required to completely configure the bootloader?

  • Yes, but the app.overlay is normally not applied to the mcuboot build, only the main application. I see you are also disabling the QSPI node. Is the mx25r64 not on your QSPI bus?

    The only warning I receive from the `* Running CMake for mcuboot *` is about the RTT configuration. For the application cmake run, I got this warning:

    It looks like you are missing the CONFIG_IMG_MANAGER. Did you use my configurations from peripheral_lbs_fota_enc/boards/nrf5340dk_nrf5340_cpuapp.conf?

  • Sorry, but I can not directly use your configuration (that's a general problem of this documentation-by-example approach), because I have an application, that requires certain configurations and I need to somehow merge your configurations with the ones, required by my app.

    `CONFIG_NCS_SAMPLES_DEFAULTS` was applied for testing. Later, I probably have to remove this "catch-all" option and have to figure out, which parts of this are relevant.

    `CONFIG_BT`, `CONFIG_BT_PERIPHERAL` have the same values, `CONFIG_BT_DEVICE_NAME` is set, but to a different value.

    `CONFIG_BT_LBS`, `CONFIG_BT_LBS_POLL_BUTTON` and `CONFIG_DK_LIBRARY` are not used, as I don't want to receive the firmware updates via that BLE service.

    `CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE` is set to the exact value.

    Adding `CONFIG_NCS_SAMPLES_DEFAULTS` does not solve the issue.

    I made an other observation: When I change the device name in `device: DT_CHOSEN(nordic_pm_ext_flash)` from the `pm_static.yml` into something, that not exists, I do not get any build errors.

    Does this nordic_pm_ext_flash maps to `nordic,pm-ext-flash = &mx25r64;` in the chosen section?

  • The settings you listed are not relevant to the problem. Does you have a QSPI flash device on your device? 

    Does this nordic_pm_ext_flash maps to `nordic,pm-ext-flash = &mx25r64;` in the chosen section?

    Yes. Did you manage to add this chosen for your bootloader?

  • Yes, there is a QSPI nor flash on the device and it works. No, haven't managed, to get that into the final dts file. It is in the final dts file of your example.

    Ok, got it now, I missed, that I have to add the same overlay snipped to the configuration of the bootloader (which totally makes sense). As we are not going to support different hardwares, this probably does not need to be as complicated as in your example. Will figure out, how to do that and will report progress.

    Thanks a lot for your help!!! :-) I wish you a nice and relaxing weekend!

  • Ok, got it now, I missed, that I have to add the same overlay snipped to the configuration of the bootloader (which totally makes sense). As we are not going to support different hardwares, this probably does not need to be as complicated as in your example.

    In that case, you can just add a file named 'mcuboot.overlay' in the sybuild directory which contains the lines below. Just remember to do a pristine/clean build after adding it to allow the build system to detect it. 

    / {
    	chosen {
    		nordic,pm-ext-flash = &mx25r64;
    	};
    };

    You too! thanks!

Related