Error when adding mcuboot for a custom nrf5340 board in the get_flash_area_from_id and dynamic partitioning

I am using a custom board with a nrf5340 and vs code with sdk/toolchain version 3.0.2 and trying to get mcuboot working for FOTA. I have been stepping through mcuboot with my segger and have breakpoints in mcuboot/main.c

I am using dynamic partitioning as recommended.

The error is happening on the second pass through this code within context_boot_go in loader.c:

        for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
            fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
            rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
            assert(rc == 0);

            if (rc != 0) {
                BOOT_LOG_ERR("Failed to open flash area ID %d (image %d slot %d): %d, "
                             "cannot continue", fa_id, image_index, (int8_t)slot, rc);
                FIH_PANIC;
            }
        }

  • slot = 1
  • flash_map_entries = 5
  • fa_id = 6

which calls:

int flash_area_open(uint8_t id, const struct flash_area **fap)
{
	const struct flash_area *area;

	if (flash_map == NULL) {
		return -EACCES;
	}

	area = get_flash_area_from_id(id);
	if (area == NULL) {
		return -ENOENT;
	}

	if (!device_is_ready(area->fa_dev)) {
		return -ENODEV;
	}

	*fap = area;

	return 0;
}

  • id = 6

which calls flash_map_priv.h:

static inline struct flash_area const *get_flash_area_from_id(int idx)
{
	for (int i = 0; i < flash_map_entries; i++) {
		if (flash_map[i].fa_id == idx) {
			return &flash_map[i];
		}
	}

	return NULL;
}

  • i=4
  • flash_map[i].fa_id = 5

This call returns a -2:

rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));

  • slot = 1

and finally dies on this call in nrf53_cpunet_mgmt.c 

static void onoff_stop(struct onoff_manager *mgr, onoff_notify_fn notify)
{
	nrf_reset_network_force_off(NRF_RESET, true);

	notify(mgr, 0);
}

Here is the flash:

When I have SB_CONFIG_BOOTLOADER_MCUBOOT=n it runs

I have read Simultaneous updates for both cores of the nRF5340, but get errors when I get to:

 # STEP 7.2 - Add b0n image
SB_CONFIG_SECURE_BOOT_NETCORE=y


the error is:
In file included from /opt/nordic/ncs/v3.0.2/nrf/include/dfu/pcd.h:28,
                 from /opt/nordic/ncs/v3.0.2/bootloader/mcuboot/boot/zephyr/main.c:96:
/opt/nordic/ncs/v3.0.2/nrf/include/dfu/pcd_common.h: In function 'pcd_write_cmd_lock_debug':
/opt/nordic/ncs/v3.0.2/nrf/include/dfu/pcd_common.h:39:25: error: 'PM__PCD_SRAM_ADDRESS' undeclared (first use in this function); did you mean 'PM_SRAM_ADDRESS'?

I have also looked at Exercise 5 – FOTA over Bluetooth Low Energy and loosly followed it, but there are a lot of differences with my using a custom board and spi external flash. 

My regions.yml looks like this when I build successfully:

external_flash:
  base_address: 0x0
  default_driver_kconfig: CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK
  device: DT_CHOSEN(nordic_pm_ext_flash)
  dynamic_partition: null
  name: external_flash
  placement_strategy: start_to_end
  size: 0x1000000
flash_primary:
  base_address: 0x0
  default_driver_kconfig: CONFIG_SOC_FLASH_NRF
  device: flash_controller
  dynamic_partition: null
  name: flash_primary
  placement_strategy: complex
  size: 0x100000
otp:
  base_address: 0xff8100
  default_driver_kconfig: ''
  device: ''
  dynamic_partition: null
  name: otp
  placement_strategy: start_to_end
  size: 0x2fc
sram_primary:
  base_address: 0x20000000
  default_driver_kconfig: ''
  device: ''
  dynamic_partition: sram_primary
  name: sram_primary
  placement_strategy: complex
  size: 0x80000

and my partitions.yml look like this:

app:
  address: 0xc200
  end_address: 0x100000
  region: flash_primary
  size: 0xf3e00
external_flash:
  address: 0xcf4000
  end_address: 0x1000000
  region: external_flash
  size: 0x30c000
littlefs_storage:
  address: 0x0
  device: DT_CHOSEN(nordic_pm_ext_flash)
  end_address: 0xc00000
  placement:
    before:
    - tfm_storage
    - end
  region: external_flash
  size: 0xc00000
mcuboot:
  address: 0x0
  end_address: 0xc000
  placement:
    align:
      end: 0x1000
    before:
    - mcuboot_primary
  region: flash_primary
  size: 0xc000
mcuboot_pad:
  address: 0xc000
  end_address: 0xc200
  placement:
    before:
    - mcuboot_primary_app
  region: flash_primary
  size: 0x200
mcuboot_primary:
  address: 0xc000
  end_address: 0x100000
  orig_span: &id001
  - mcuboot_pad
  - app
  region: flash_primary
  size: 0xf4000
  span: *id001
mcuboot_primary_app:
  address: 0xc200
  end_address: 0x100000
  orig_span: &id002
  - app
  region: flash_primary
  size: 0xf3e00
  span: *id002
mcuboot_secondary:
  address: 0xc00000
  device: DT_CHOSEN(nordic_pm_ext_flash)
  end_address: 0xcf4000
  placement:
    align:
      start: 0x4
  region: external_flash
  share_size:
  - mcuboot_primary
  size: 0xf4000
otp:
  address: 0xff8100
  end_address: 0xff83fc
  region: otp
  size: 0x2fc
rpmsg_nrf53_sram:
  address: 0x20070000
  end_address: 0x20080000
  placement:
    before:
    - end
  region: sram_primary
  size: 0x10000
sram_primary:
  address: 0x20000000
  end_address: 0x20070000
  region: sram_primary
  size: 0x70000

Parents
  • Hi,

    I recommend you follow the same approach as here (which you write you had loosly followed) more or less too the point. This should be valid also for any custom board provdied you have the external flash correctly configured. If that is the problem, can you back-track a bit and make sure you have yor SPI flash configured and working on your custom board with your custom baord files withotu DFU? (see this thread for that)

  • I do have the custom board working with external flash when SB_CONFIG_BOOTLOADER_MCUBOOT=n:

    00> [00:00:02.291,046] <inf> littlefs: littlefs partition at /lfs1
    00> [00:00:02.291,076] <inf> littlefs: LittleFS version 2.9, disk version 2.1
    00> [00:00:02.293,182] <inf> littlefs: FS at w25q128@0:0x0 is 3072 0x1000-byte blocks with 512 cycle
    00> [00:00:02.293,182] <inf> littlefs: partition sizes: rd 512 ; pr 512 ; ca 512 ; la 4096
    00> [00:00:02.309,234] <inf> littlefs: Automount /lfs1 succeeded

    Also, I did mention I followed the article you reference, but got an error when adding this:

     # STEP 7.2 - Add b0n image
    SB_CONFIG_SECURE_BOOT_NETCORE=y

    Here is the error:

    In file included from /opt/nordic/ncs/v3.0.2/nrf/include/dfu/pcd.h:28,
                     from /opt/nordic/ncs/v3.0.2/bootloader/mcuboot/boot/zephyr/main.c:96:
    /opt/nordic/ncs/v3.0.2/nrf/include/dfu/pcd_common.h: In function 'pcd_write_cmd_lock_debug':
    /opt/nordic/ncs/v3.0.2/nrf/include/dfu/pcd_common.h:39:25: error: 'PM__PCD_SRAM_ADDRESS' undeclared (first use in this function); did you mean 'PM_SRAM_ADDRESS'?
       39 | #define PCD_CMD_ADDRESS PM__PCD_SRAM_ADDRESS

    Also since I'm not using qspi, I cannot use this:

    # STEP 6.2 - MCUboot should use external flash
    CONFIG_NORDIC_QSPI_NOR=y
    

    Is there something else I use in it's place?

    When I have all of these:

    SB_CONFIG_NETCORE_HCI_IPC=y
    SB_CONFIG_BOOTLOADER_MCUBOOT=y
    SB_CONFIG_PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY=y
    
    SB_CONFIG_SECURE_BOOT_NETCORE=y
    SB_CONFIG_MCUBOOT_UPDATEABLE_IMAGES=2
    SB_CONFIG_NETCORE_APP_UPDATE=y
    SB_CONFIG_MCUBOOT_NRF53_MULTI_IMAGE_UPDATE=y
    SB_CONFIG_MCUBOOT_MODE_OVERWRITE_ONLY=y

    I get this error:

    /opt/nordic/ncs/toolchains/ef4fc6722e/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd: zephyr/zephyr_pre0.elf section `datas' will not fit in region `FLASH'
    /opt/nordic/ncs/toolchains/ef4fc6722e/opt/zephyr-sdk/arm-zephyr-eabi/bin/../lib/gcc/arm-zephyr-eabi/12.2.0/../../../../arm-zephyr-eabi/bin/ld.bfd: region `FLASH' overflowed by 560 bytes

  • Ok. I will give it a shot. We are essentially back to the first post I had. I think it's partitioning, since the error happens in get_flash_area_from_id, but I'm not managing the partitioning (except for specifying the littlefs size) because I'm using dynamic partitioning (as recommened by one of your engineers who I trust).

  • I read this post MCUboot warnings + app errors when using external flash which said:


    I think I "fixed" external flash set up by adding
    CONFIG_PM_OVERRIDE_EXTERNAL_DRIVER_CHECK=y
    (I added it in both prj.conf and in bootloader overlay config)


    I did have it in the prj.conf but needed to add it to the mcuboot.conf. This got me through the get_flash_area_from_id error, but I ran into an "Unable to find bootable image" error:

    which seems to happen in the boot_validate_slot call:

  • Hi,

    That points to a valid image not found, which again points to a partitioning / possible memory layout issue. As mentionned earlier though I am having troubble getting an overview of this.

    Have you had a look at the nrf7002 DK board files and Matter sample overlays MCUboot for it (for instance here)? It can be a good reference for part of this as it use external SPI flash instead of QSPI (as that is used for communication between the nRF5340 and nRF7002).

    If you still have no luck, can you upload your project and board files as a zip so that I can get a better overview?

  • I see this error when using the link: "Unfortunately, the page you've requested no longer exists. Please use the search form above to locate the information you're interested in." In the url it is marked as private.

    I do have external SPI flash is working. Partitioning is likely the problem, I agree, but I am using dynamic partitioning. In that case isn't this a dynamic partitioning issue? I have been using Adding Device Firmware Update (DFU/FOTA) Support in nRF Connect SDK as gospel since it is one of the clearest and most complete explanations I've seen and in it you "recommend while developing you use dynamic partitioning". It makes sense and that's what I'm trying to do.

    I have options. I could switch to static partitioning, but I think we would be ducking the issue.

    If you think I should close this ticket and open another because I have resolved the "get_flash_area_from_id" issue and am now looking at addressing an "Unable to find bootable image" issue, I can do that.

    The partitions.yml in the build folder looks like:

    app:
      address: 0xc200
      end_address: 0x100000
      region: flash_primary
      size: 0xf3e00
    external_flash:
      address: 0xcf4000
      end_address: 0x1000000
      region: external_flash
      size: 0x30c000
    littlefs_storage:
      address: 0x0
      device: DT_CHOSEN(nordic_pm_ext_flash)
      end_address: 0xc00000
      placement:
        before:
        - tfm_storage
        - end
      region: external_flash
      size: 0xc00000
    mcuboot:
      address: 0x0
      end_address: 0xc000
      placement:
        align:
          end: 0x1000
        before:
        - mcuboot_primary
      region: flash_primary
      size: 0xc000
    mcuboot_pad:
      address: 0xc000
      end_address: 0xc200
      placement:
        align:
          start: 0x4000
        before:
        - mcuboot_primary_app
      region: flash_primary
      size: 0x200
    mcuboot_primary:
      address: 0xc000
      end_address: 0x100000
      orig_span: &id001
      - app
      - mcuboot_pad
      region: flash_primary
      size: 0xf4000
      span: *id001
    mcuboot_primary_app:
      address: 0xc200
      end_address: 0x100000
      orig_span: &id002
      - app
      region: flash_primary
      size: 0xf3e00
      span: *id002
    mcuboot_secondary:
      address: 0xc00000
      device: DT_CHOSEN(nordic_pm_ext_flash)
      end_address: 0xcf4000
      placement:
        align:
          start: 0x4
      region: external_flash
      share_size:
      - mcuboot_primary
      size: 0xf4000
    otp:
      address: 0xff8100
      end_address: 0xff83fc
      region: otp
      size: 0x2fc
    rpmsg_nrf53_sram:
      address: 0x20070000
      end_address: 0x20080000
      placement:
        before:
        - end
      region: sram_primary
      size: 0x10000
    sram_primary:
      address: 0x20000000
      end_address: 0x20070000
      region: sram_primary
      size: 0x70000
    

  • Hi,

    I am not able to see exactly what the problem is. DFU on the nRF53 with external flash is tricky and there are many details that needs to be in place. So I would recommend following a working example to the point. That woudl specifically be doing as demonstrated in Exercise 3 – DFU with external flash and using the soulution for the nRF7002 DK, which used external SPI flash.

    If you still face issues, can you please share the following so that we can get a full understanding?

    • Full build log
    • Full project folder including build folder
    • Custom board files
Reply Children
  • I continue to have issues. I can get it working, I create another demo for another purpose and flash my board. Then I come back to this project and I have the same problem. It is always:

    • flash area id: 6
    • slot: 1
    • image_index: 0
    • return code: -19

    The error happens in flash_area_open of loader.c:

            /* Open primary and secondary image areas for the duration
             * of this call.
             */
            for (slot = 0; slot < BOOT_NUM_SLOTS; slot++) {
                fa_id = flash_area_id_from_multi_image_slot(image_index, slot);
                rc = flash_area_open(fa_id, &BOOT_IMG_AREA(state, slot));
                assert(rc == 0);
    
                if (rc != 0) {
                    BOOT_LOG_ERR("Failed to open flash area ID %d (image %d slot %d): %d, "
                                 "cannot continue", fa_id, image_index, (int8_t)slot, rc);
                    FIH_PANIC;
                }
            }

    It doesn't seem like leaving it up to dynamic partitioning is a good idea. I know it's the recommendation, but this is a persistant problem. The flash works, we've been through that. It's clearly the partition manager.

  • Hi,

    Could it be that the w25q128 external flash is in a state where it is not powered on time after reset when MCUboot runs, as was a theory here (though it seems it was never conclucded)?

Related