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?

Parents
  • Hello,

    I've created a demo based on the peripheral_lbs sample, please see attached below.

    Demo sample

    peripheral_lbs_enc_fota.zip

    Disclaimers

    - DFU with encrypted images is not a feature that has been tested or validated by Nordic ( nRF52840 + NCS + MCUBoot, CC310-enabled image encryption? ) .

    - The cmake signing script does not sign encrypt the netcore image. This can be fixed by patching relevant cmake script.

    - The application_dfu.zip generated by the build contains signed but not encrypted images. Use the /build/peripheral_lbs_fota_enc/zephyr/zephyr.signed.encrypted.bin binary instead (application image). The netcore image which is signed but not encrypted is called signed_by_mcuboot_and_b0_ipc_radio.bin and is located in build/.

    - I have not done any extensive testing of this demo, just verified that I was able to update the appcore FW.

    Best regards,

    Vidar

  • Thank you! I will go through that example.

    An other, very annoying observation I made: In the corse to, basically "try and error", figuring out some settings that fixes build errors, I found, that the build is not reproducible!

    Currently, I have a state, where I completely delete the build folder, start the build with `--pristine` added to the `west build` command and get alternating, two different errors. The one error, is the error above, while building the application.After I received that error, I delete the build folder again and rerun the build command. Every second time, I receive a linker error, while linking the application (a missing symbol, probably not defined due to the first issue).

    As the second error is a linker error, this can not be any effect from parallel build, where the order of build steps could change. I disable the usage of `ccache`, but the behavior does not change. :-/

  • Thanks. It appears that you are missing the 'nordic,pm-ext-flash' property in your zephyr.dts. Without this, the bootloader will not know which flash device to use for accessing the partitions in 'external_flash'. In my sample, I had a board overlay to set this 'chosen' (/peripheral_lbs_fota_enc/sysbuild/mcuboot/boards/nrf5340dk_nrf5340_cpuapp.overlay) 

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

  • This is the start of my `app.overlay`:

     &zephyr_udc0 {
        cdc_acm_uart0 {
            compatible = "zephyr,cdc-acm-uart";
        };
    };
    
    / {
        chosen {
            nordic,pm-ext-flash = &mx25r64;
        };
    };
    
    /*
       Disable QSPI peripheral, to not get the Zephyr QSPI driver linked in,
       which would clash with drivers/flash_memory.cpp
     */
    &qspi {
        status = "disabled";
    };
    
    ...

    I've disabled the qspi-disable part of the overlay file, but still there are some entries in `flash_map` with `fa_dev` set to 0 (and the resulting dts still does not contain the required chosen value):

    (gdb) p/x *flash_map@flash_map_entries
    $3 = {{
        fa_id = 0x0,
        pad16 = 0x0,
        fa_off = 0x0,
        fa_size = 0x10000,
        fa_dev = 0x94b0
      }, {
        fa_id = 0x1,
        pad16 = 0x0,
        fa_off = 0x0,
        fa_size = 0x40000,
        fa_dev = 0x949c
      }, {
        fa_id = 0x2,
        pad16 = 0x0,
        fa_off = 0x0,
        fa_size = 0xe0000,
        fa_dev = 0x0
      }, {
        fa_id = 0x3,
        pad16 = 0x0,
        fa_off = 0x10000,
        fa_size = 0x200,
        fa_dev = 0x94b0
      }, {
        fa_id = 0x4,
        pad16 = 0x0,
        fa_off = 0x10000,
        fa_size = 0xe0000,
        fa_dev = 0x94b0
      }, {
        fa_id = 0x5,
        pad16 = 0x0,
        fa_off = 0x10200,
        fa_size = 0xdfe00,
        fa_dev = 0x94b0
      }, {
        fa_id = 0x6,
        pad16 = 0x0,
        fa_off = 0x10200,
        fa_size = 0xdfe00,
        fa_dev = 0x94b0
      }, {
        fa_id = 0x7,
        pad16 = 0x0,
        fa_off = 0x40000,
        fa_size = 0x0,
        fa_dev = 0x949c
      }, {
        fa_id = 0x8,
        pad16 = 0x0,
        fa_off = 0xe0000,
        fa_size = 0x40000,
        fa_dev = 0x0
      }, {
        fa_id = 0x9,
        pad16 = 0x0,
        fa_off = 0xf0000,
        fa_size = 0x10000,
        fa_dev = 0x94b0
      }, {
        fa_id = 0xa,
        pad16 = 0x0,
        fa_off = 0x120000,
        fa_size = 0x6e0000,
        fa_dev = 0x0
      }}
    

    The number of entries in the array corresponds with the number of entries in pm_static.yml, but the order seems to be different.

    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:

        warning: UPDATEABLE_IMAGE_NUMBER (defined at /Users/todi/bloomlife/zephyr-
        workspace/nrf/samples/common/mcumgr_bt_ota_dfu/Kconfig:88, subsys/dfu/Kconfig:95) was assigned the
        value '2' but got the value ''. Check these unsatisfied dependencies:
        (((BOARD_THINGY53_NRF5340_CPUAPP || BOARD_THINGY53_NRF5340_CPUAPP_NS) && SOC_SERIES_NRF53X &&
        NCS_SAMPLE_MCUMGR_BT_OTA_DFU) || (!MCUBOOT && IMG_MANAGER)) (=n). See
        http://docs.zephyrproject.org/latest/kconfig.html#CONFIG_UPDATEABLE_IMAGE_NUMBER and/or look up
        UPDATEABLE_IMAGE_NUMBER in the menuconfig/guiconfig interface. The Application Development Primer,
        Setting Configuration Values, and Kconfig - Tips and Best Practices sections of the manual might be
        helpful too.
    

    Could that be a hint as to where the problem is?

  • 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?

Reply Children
  • 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!

  • Hi Vidar,

    I managed to add the used external NOR flash chip to the configuration (I had to name it mx25r64, even thought, it is a totally different chip. Otherwise, I got linker errors: `undefined reference to `__device_dts_ord_142'`).

    Now, all binaries (Application, Network, MCUBoot and b0n) are building. The bootloader starts, but refuses to start the binary in the primary slot:

    00> *** Booting MCUboot v2.1.0-dev-f30dce197f34 ***
    00> *** Using nRF Connect SDK v2.7.99-cs2-d73b286ff6d9 ***
    00> *** Using Zephyr OS v3.6.99-1b696c108812 ***
    00> [00:00:00.324,035] <inf> mcuboot: Starting bootloader
    00> [00:00:00.324,432] <wrn> mcuboot: Failed reading sectors; BOOT_MAX_IMG_SECTORS=128 - too small?
    00> [00:00:00.325,195] <inf> mcuboot: Image index: 1, Swap type: none
    00> [00:00:00.325,500] <err> mcuboot: Image in the primary slot is not valid!
    00> [00:00:00.325,531] <err> mcuboot: Unable to find bootable image

    At the primaries start address, there is an application (at least it looks like a vector table). So I guess, that MCUBoot is not able to correctly verify the image.

    During the build of MCUboot, there are log messages, that indicate, that the bootloader is build with the correct keys embedded:

    MCUBoot bootloader key file: <absoulte path to pem file>
    MCUBoot bootloader encryption key file: <absoulte path to pem file>

    While during the build of the application, I see this warning, that let me suspect, that maybe for the build of the application an other key is used.

          --------------------------------------------------------------
          --- WARNING: Using generated NSIB public/private key-pair. ---
          --- It should not be used for production.                  ---
          --- See SB_CONFIG_SECURE_BOOT_SIGNING_KEY_FILE             ---
          --------------------------------------------------------------

    Interesting enough, I saw the same discrepancy with your example. So, I toke an nRF5340 eval board and installed your example. But in that case, the application is running (I see a blinking LED and a connectable BLE service).

    As the primary is stored in internal flash memory, I suspect, that it could not be a problem of the configuration of the external flash memory.

    Any idea, as to what could be the problem here?

    best regards

    Torsten

  • Hi Torsten,

    Torsten Robitzki said:
    I managed to add the used external NOR flash chip to the configuration (I had to name it mx25r64, even thought, it is a totally different chip.

    The chosen must point to the flash device you have in your QSPI devictree node if it is not the same as the one on the DK.

    Torsten Robitzki said:
    Now, all binaries (Application, Network, MCUBoot and b0n) are building. The bootloader starts, but refuses to start the binary in the primary slot:

    Please create a new file named mcuboot.conf in your sysbuild folder that contain the following lines:

    # Configure MCUboot features
    CONFIG_BOOT_MAX_IMG_SECTORS=256
    CONFIG_MCUBOOT_DOWNGRADE_PREVENTION=y
    CONFIG_BOOT_ECDSA_TINYCRYPT=y
    
    # Enable flash simulator
    CONFIG_PCD_APP=y
    CONFIG_FLASH_SIMULATOR=y
    CONFIG_FLASH_SIMULATOR_DOUBLE_WRITES=y
    CONFIG_FLASH_SIMULATOR_STATS=n
    
    CONFIG_NORDIC_QSPI_NOR=y
    CONFIG_NORDIC_QSPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096
    
    CONFIG_BOOT_IMAGE_ACCESS_HOOKS=y
    CONFIG_BOOT_IMAGE_ACCESS_HOOK_NRF5340=y
    
    CONFIG_MULTITHREADING=y

    The do a pristine build.

    Best regards,

    Vidar

  • Hi Vidar,

    my idea, was to define some device, similar to the mx25r64 (with a changed jedec-id, frequency, etc.) and then to pick that device with the `nordic,pm-ext-flash` in the chosen section:

    &qspi {
        flash_ext: MT25QU01GBBB@0 {
            compatible = "nordic,qspi-nor";
            reg = < 0x0 >;
            writeoc = "pp4io";
            readoc = "read4io";
            sck-frequency = < 90000000 >;
            jedec-id = [ 20 BB 21 ];
            sfdp-bfp = [ E5 20 F1 FF FF FF FF 03 44 EB 08 6B 08 3B 04 BB EE FF FF FF FF FF 00 FF FF FF 00 FF 0C 20 0F 52 10 D8 00 FF 23 72 F5 00 82 ED 04 CC 44 83 68 44 30 B0 30 B0 F7 C4 D5 5C 00 BE 29 FF F0 D0 FF FF ];
            size = < 0x4000000 >;
            has-dpd;
            t-enter-dpd = < 0x2710 >;
            t-exit-dpd = < 0x88b8 >;
        };
    };
    
    / {
        chosen {
            nordic,pm-ext-flash = &flash_ext;
        };
    };
    

    but this results in a linker error: "undefined reference to `__device_dts_ord_142`"

    When I rename the device above to `mx25r64: mx25r6435f@0`, everything works fine (including the jedec test in the driver). So anyway, I have a solution. I will add some comment to the configuration.

    For the bootloader itself: I found out, after some lengthy debugging, that the primary image was flashed with a RSA3072 hash, while MCUBoot was looking for a RSA2048 hash. The MCUBoot error message was: "Image in the primary slot is not valid!".

    After changing algorithm and key to ECDSA_P256, the bootloader is starting the application.

    Thanks a lot for your help,

    Torsten

Related