Mass storage initialization error with nsib and mcuboot

Board: nRF52840Dk

ncs: v2.3.0.

Hi, I am working on a project in which i need to update bootloader itself and application from the external flash. I also need to make partition where i can store my files using FatFs file system.

My project configuration and overlay setting are below:

CONFIG_STDOUT_CONSOLE=y

#USB related configs
CONFIG_USB_DEVICE_STACK=y
CONFIG_USB_DEVICE_PRODUCT="Zephyr MSC sample"
CONFIG_LOG=y
CONFIG_USB_DRIVER_LOG_LEVEL_ERR=y
CONFIG_USB_MASS_STORAGE=y
CONFIG_USB_DEVICE_LOG_LEVEL_ERR=y
CONFIG_USB_MASS_STORAGE_LOG_LEVEL_ERR=y

CONFIG_MAIN_STACK_SIZE=4096


#CONFIG_APP_MSC_STORAGE_FLASH_LITTLEFS=y
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n
CONFIG_APP_MSC_STORAGE_FLASH_FATFS=y
CONFIG_FS_FATFS_LFN=y

# External partitions
#CONFIG_PM_PARTITION_REGION_LITTLEFS_EXTERNAL=y
#CONFIG_PM_PARTITION_REGION_SETTINGS_STORAGE_EXTERNAL=y
#CONFIG_PM_PARTITION_REGION_NVS_STORAGE_EXTERNAL=y

CONFIG_BOOTLOADER_MCUBOOT=y
CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=y

#memory related configs
CONFIG_NVS=y
CONFIG_FLASH=y
CONFIG_FLASH_MAP=y
CONFIG_STREAM_FLASH=y
CONFIG_REBOOT=y
CONFIG_HWINFO=y
CONFIG_IMG_MANAGER=y
CONFIG_IMG_ERASE_PROGRESSIVELY=y

#immutable boot
CONFIG_SECURE_BOOT=y
CONFIG_SB_SIGNING_KEY_FILE="nsib_priv.pem"
CONFIG_BUILD_S1_VARIANT=y
# Need to lower the number of counter slots to be able to update several times. Do not know the best number yet.
CONFIG_SB_NUM_VER_COUNTER_SLOTS=120

CONFIG_FW_INFO_FIRMWARE_VERSION=1
CONFIG_MCUBOOT_IMAGE_VERSION="1.1.1"

/delete-node/ &boot_partition;
/delete-node/ &slot0_partition;
/delete-node/ &slot1_partition;

&flash0 {
	partitions {
		boot_partition: partition@0 {
			label = "mcuboot";
			reg = <0x000000000 0x00010000>;
		};
		slot0_partition: partition@10000 {
			label = "image-0";
			reg = <0x000010000 0x0000e8000>;
		};
	};
};

&mx25r64 {
	partitions {
		compatible = "fixed-partitions";
		#address-cells = <1>;
		#size-cells = <1>;

		slot1_partition: partition@0 {
			label = "image-1";
			reg = <0x000000000 0x0000e8000>;
		};
		external_flash: partition@e8000 {
			label = "external flash";
			reg = <0x0000e8000 0x000718000>;
		};
	};
};

/ {
	chosen {
		nordic,pm-ext-flash = &mx25r64;
	};
	msc_disk0 {
		compatible = "zephyr,flash-disk";
		partition = <&external_flash>;
		disk-name = "NAND";
		cache-size = <4096>;
	};
};

When i am running the code then getting error of msc init:

Attempting to boot slot 0.
Attempting to boot from address 0x9200.
Verifying signature against key 0.
Hash: 0x8f...3b
Firmware signature verified.
Firmware version 5
Setting monotonic counter (version: 5, slot: 0)
*** Booting Zephyr OS build v3.2.99-ncs2-3159-g1e1697d881a9 ***
[00:00:00.376,220] <inf> mcuboot: Starting bootloader
[00:00:00.376,983] <inf> mcuboot: Primary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.377,349] <inf> mcuboot: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.377,349] <inf> mcuboot: Boot source: none
[00:00:00.377,838] <inf> mcuboot: Swap type: none
[00:00:00.378,295] <inf> mcuboot: Primary image: magic=bad, swap_type=0x1, copy_done=0x2, image_ok=0x2
[00:00:00.378,662] <inf> mcuboot: Secondary image: magic=unset, swap_type=0x1, copy_done=0x3, image_ok=0x3
[00:00:00.378,692] <inf> mcuboot: Boot source: none
[00:00:00.379,150] <inf> mcuboot: Swap type: none
*** Booting Zephyr OS build v3.2.99-ncs2-3159-g1e1697d881a9 ***
[00:00:00.000,396] <inf> flashdisk: Initialize device NAND
[00:00:00.000,427] <inf> flashdisk: offset e8000, sector size 512, page size 4096, volume size 7438336
[00:00:00.000,488] <err> flashdisk: Error -22 getting page info at offset 100000
[00:00:00.000,488] <err> usb_msc: Storage init ERROR !!!! - Aborting USB init
[00:00:00.000,518] <wrn> main: Image build time: Jul 10 2023 09:44:53
[00:00:00.000,579] <inf> main: Image is confirmed OK

Please help me, what I am doing wrong.

Thanks.

Parents Reply Children
  • Hi Sigvartmh,

    Did you get a chance to investigate the issue?

     

  • Seems it's trying to do a trailer erase out of bounds within the storage space of the new bootloader.

    I assume you tried to investigate some more with your configuration? If not here is one with a larger FS you can still change the PM_static file just make sure you align the sizes.

    nsib_mcuboot_with_log_larger_fs.zip

    I was able to get it working with scratch at least if you apply this patch to mcuboot

    diff --git a/boot/bootutil/src/swap_misc.c b/boot/bootutil/src/swap_misc.c
    index 733a3974..39e89582 100644
    --- a/boot/bootutil/src/swap_misc.c
    +++ b/boot/bootutil/src/swap_misc.c
    @@ -66,15 +66,17 @@ swap_erase_trailer_sectors(const struct boot_loader_state *state,
         sector = boot_img_num_sectors(state, slot) - 1;
         trailer_sz = boot_trailer_sz(BOOT_WRITE_SZ(state));
         total_sz = 0;
    -    do {
    -        sz = boot_img_sector_size(state, slot, sector);
    -        off = boot_img_sector_off(state, slot, sector);
    -        rc = boot_erase_region(fap, off, sz);
    -        assert(rc == 0);
    -
    -        sector--;
    -        total_sz += sz;
    -    } while (total_sz < trailer_sz);
    +    if(!(fa_id_primary == PM_S1_ID || fa_id_primary == PM_S0_ID)) {
    +	    do {
    +		    sz = boot_img_sector_size(state, slot, sector);
    +		    off = boot_img_sector_off(state, slot, sector);
    +		    rc = boot_erase_region(fap, off, sz);
    +		    assert(rc == 0);
    +
    +		    sector--;
    +		    total_sz += sz;
    +	    } while (total_sz < trailer_sz);
    +    }
    
         return rc;
     }

    If you want the PM with scratch are I have it here:

    EMPTY_0:
      address: 0x19200
      end_address: 0x1a000
      placement:
        before:
        - s1_pad
      region: flash_primary
      size: 0xe00
    EMPTY_1:
      address: 0x2a200
      end_address: 0x2b000
      placement:
        before:
        - mcuboot_pad
      region: flash_primary
      size: 0xe00
    app:
      address: 0x2b200
      end_address: 0xfa000
      region: flash_primary
      size: 0xcee00
    app_image:
      address: 0x2b200
      end_address: 0xfa000
      orig_span: &id001
      - app
      region: flash_primary
      size: 0xcee00
      span: *id001
    b0:
      address: 0x0
      end_address: 0x8000
      placement:
        after:
        - start
      region: flash_primary
      size: 0x8000
    b0_container:
      address: 0x0
      end_address: 0x9000
      orig_span: &id002
      - b0
      - provision
      region: flash_primary
      size: 0x9000
      span: *id002
    fatfs_storage:
      address: 0xb1000
      affiliation:
      - disk
      device: DT_CHOSEN(nordic_pm_ext_flash)
      extra_params:
        disk_cache_size: 0x1000
        disk_name: NAND
        disk_read_only: 0x0
        disk_sector_size: 0x200
      placement:
        align:
          start: 0x1000
        before:
        - tfm_storage
        - end
      region: external_flash
      size: 0x74f000
    mcuboot:
      address: 0x9200
      end_address: 0x19200
      placement:
        before:
        - mcuboot_primary
      region: flash_primary
      sharers: 0x1
      size: 0x10000
    mcuboot_pad:
      address: 0x2b000
      end_address: 0x2b200
      placement:
        align:
          start: 0x1000
        before:
        - mcuboot_primary_app
      region: flash_primary
      sharers: 0x2
      size: 0x200
    mcuboot_primary:
      address: 0x2b000
      end_address: 0xfa000
      orig_span: &id003
      - app
      - mcuboot_pad
      region: flash_primary
      size: 0xb1000
      span: *id003
    mcuboot_primary_app:
      address: 0x2b200
      end_address: 0xfa000
      orig_span: &id004
      - app
      region: flash_primary
      size: 0xcee00
      span: *id004
    mcuboot_secondary:
      address: 0x0
      device: DT_CHOSEN(nordic_pm_ext_flash)
      placement:
        align:
          start: 0x4
      region: external_flash
      share_size:
      - mcuboot_primary
      size: 0xb1000
    nvs_storage:
      address: 0xfa000
      end_address: 0x100000
      placement:
        before:
        - end
      region: flash_primary
      size: 0x6000
    provision:
      address: 0x8000
      end_address: 0x9000
      placement:
        after:
        - b0
        align:
          start: 0x1000
      region: flash_primary
      size: 0x1000
    s0:
      address: 0x9000
      end_address: 0x19200
      orig_span: &id005
      - mcuboot
      - s0_pad
      region: flash_primary
      size: 0x10200
      span: *id005
    s0_image:
      address: 0x9200
      end_address: 0x19200
      orig_span: &id006
      - mcuboot
      region: flash_primary
      size: 0x10000
      span: *id006
    s0_pad:
      address: 0x9000
      end_address: 0x9200
      placement:
        after:
        - b0_container
        align:
          start: 0x1000
      region: flash_primary
      share_size:
      - mcuboot_pad
      size: 0x200
    s1:
      address: 0x1a000
      end_address: 0x2a200
      orig_span: &id007
      - s1_pad
      - s1_image
      region: flash_primary
      size: 0x10200
      span: *id007
    s1_image:
      address: 0x1a200
      end_address: 0x2a200
      placement:
        after:
        - s1_pad
        - s0
      region: flash_primary
      share_size:
      - mcuboot
      size: 0x10000
    s1_pad:
      address: 0x1a000
      end_address: 0x1a200
      placement:
        after:
        - s0
        align:
          start: 0x1000
      region: flash_primary
      share_size:
      - mcuboot_pad
      size: 0x200
    mcuboot_scratch:
      address: 0xdc000
      end_address: 0xfa000
      placement:
        after:
        - app
        align:
          start: 0x1000
      region: flash_primary
      size: 0x1e000
    sram_primary:
      address: 0x20000000
      end_address: 0x20040000
      region: sram_primary
      size: 0x40000

    I think the patch should work with MCUBoot using move however I haven't tested this.

  • Thanks, Sigvartmh, Now I am able to update the mcuboot.

    We need separated binary files for updating slot S0 and S1, but I prefer not to monitor these for FOTA updates.

    I found a solution for the same:

    But When I am writing "dfu_mcuboot.zip" file to flash, I don't see any updates.

    Please help me, do I need to write any parser for this or It will write same as  signed_by_mcuboot_and_b0_s0_image_update.bin and  signed_by_mcuboot_and_b0_s1_image_update.bin file.

  • You cannot just write the zip file to Flash, unfortunately, as MCUBoot does not support this. Also, MCUBoot has no system to receive both updates in the secondary slot and select the correct one.

    I think you have misinterpreted the documentation:

    >This file can be used by FOTA servers (for example, nRF Cloud) to serve both s0 and s1 to the device. The device can then select the firmware file for the slot that is currently not in use.

    This tries to explain that the device needs to request either S0 or S1 from the server, depending on which slot is active. So you cannot just provide it with both (in Flash), and it will select the correct one. Your FOTA solution could provide both files and then the device could select the correct one based on the active slot.

    As an example, the nRF Cloud server uses the manifest.json file to provide the device with the correct file according to what it requests.


  • Thank you for the clarification.

    For the mcuboot update we need an extra reboot the device, how can we know that we need to reboot the device again  to change the slot for mcuboot?

Related