MCUboot do not find spi ext flash areas

Hi,

I am using nrf52840, NCS v2.1.2.

I add MCUboot in my project, but got ‘Failed reading sectors; BOOT_MAX_IMG_SECTORS=256 - too small?’.

I add some log in MCUboot, and found MCUboot got a wrong flash areas numbre.

Here is my log:

[00:00:00.250,640] <err> spi_nor: Device id ef 40 18 does not match config ef 40 18
[00:00:05.251,129] <inf> mcuboot: Starting bootloader
[00:00:05.251,678] <wrn> mcuboot: flash_area: 4, flash_map_entries:7
[00:00:05.252,014] <wrn> mcuboot: flash_area: 9, flash_map_entries:7
[00:00:05.252,349] <wrn> mcuboot: flash_area_get_sectors: -22
[00:00:05.252,655] <wrn> mcuboot: boot_initialize_area: -22
[00:00:05.252,990] <wrn> mcuboot: Failed reading sectors;rc=9
[00:00:05.253,295] <wrn> mcuboot: Failed reading sectors; BOOT_MAX_IMG_SECTORS=256 - too small?
[00:00:05.253,753] <wrn> mcuboot: Cannot upgrade: not a compatible amount of sectors
[00:00:05.331,512] <inf> mcuboot: Bootloader chainload address offset: 0x10000
[00:00:05.331,878] <inf> mcuboot: Jumping to the first image slot

And my added code is:

diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c
index 2a5c89cd..3433fb1c 100644
--- a/boot/bootutil/src/loader.c
+++ b/boot/bootutil/src/loader.c
@@ -320,7 +320,7 @@ boot_write_sz(struct boot_loader_state *state)
 
     return elem_sz;
 }
-
+extern const int flash_map_entries;
 static int
 boot_initialize_area(struct boot_loader_state *state, int flash_area)
 {
@@ -348,11 +348,13 @@ boot_initialize_area(struct boot_loader_state *state, int flash_area)
 
 #ifdef MCUBOOT_USE_FLASH_AREA_GET_SECTORS
     rc = flash_area_get_sectors(flash_area, &num_sectors, out_sectors);
+    BOOT_LOG_WRN("flash_area: %d, flash_map_entries:%d", flash_area, flash_map_entries);
 #else
     _Static_assert(sizeof(int) <= sizeof(uint32_t), "Fix needed");
     rc = flash_area_to_sectors(flash_area, (int *)&num_sectors, out_sectors);
 #endif /* defined(MCUBOOT_USE_FLASH_AREA_GET_SECTORS) */
     if (rc != 0) {
+        BOOT_LOG_WRN("flash_area_get_sectors: %d", rc);
         return rc;
     }
     *out_num_sectors = num_sectors;
@@ -380,6 +382,7 @@ boot_read_sectors(struct boot_loader_state *state)
 
     rc = boot_initialize_area(state, FLASH_AREA_IMAGE_SECONDARY(image_index));
     if (rc != 0) {
+        BOOT_LOG_WRN("boot_initialize_area: %d", rc);
         /* We need to differentiate from the primary image issue */
         return BOOT_EFLASH_SEC;
     }
@@ -1823,6 +1826,7 @@ boot_prepare_image_for_update(struct boot_loader_state *state,
     /* Determine the sector layout of the image slots and scratch area. */
     rc = boot_read_sectors(state);
     if (rc != 0) {
+        BOOT_LOG_WRN("Failed reading sectors;rc=%d", rc);
         BOOT_LOG_WRN("Failed reading sectors; BOOT_MAX_IMG_SECTORS=%d"
                      " - too small?", BOOT_MAX_IMG_SECTORS);
         /* Unable to determine sector layout, continue with next image

partitions.yml is as follow:

app:
  address: 0x10200
  end_address: 0xf0000
  region: flash_primary
  size: 0xdfe00
ble_scan_part:
  address: 0x0
  device: mx25l256
  end_address: 0x100000
  region: external_flash
  size: 0x100000
external_flash:
  address: 0x0
  device: mx25l256
  end_address: 0x1000000
  region: external_flash
  size: 0x1000000
gnss_part:
  address: 0x1e0000
  device: mx25l256
  end_address: 0x220000
  region: external_flash
  size: 0x40000
mcuboot:
  address: 0x0
  end_address: 0x10000
  region: flash_primary
  size: 0x10000
mcuboot_pad:
  address: 0x10000
  end_address: 0x10200
  region: flash_primary
  size: 0x200
mcuboot_primary:
  address: 0x10000
  end_address: 0xf0000
  orig_span: &id001
  - mcuboot_pad
  - app
  region: flash_primary
  size: 0xe0000
  span: *id001
mcuboot_primary_app:
  address: 0x10200
  end_address: 0xf0000
  orig_span: &id002
  - app
  region: flash_primary
  size: 0xdfe00
  span: *id002
mcuboot_secondary:
  address: 0x100000
  device: mx25l256
  end_address: 0x1e0000
  region: external_flash
  size: 0xe0000
settings_storage:
  address: 0xf0000
  end_address: 0xfc000
  region: flash_primary
  size: 0xc000
sram_primary:
  address: 0x20000000
  end_address: 0x20040000
  region: sram_primary
  size: 0x40000
temp_part:
  address: 0x220000
  device: mx25l256
  end_address: 0x230000
  region: external_flash
  size: 0x10000
temporary_storage:
  address: 0xfc000
  end_address: 0x100000
  region: flash_primary
  size: 0x4000

build\mcuboot\zephyr\include\generated\pm_config.h is: (LINE: 141)

/* File generated by D:/ncs/v2.1.2/nrf/scripts/partition_manager_output.py, do not modify */
#ifndef PM_CONFIG_H__
#define PM_CONFIG_H__
#define PM_BLE_SCAN_PART_OFFSET 0x0
#define PM_BLE_SCAN_PART_ADDRESS 0x0
#define PM_BLE_SCAN_PART_END_ADDRESS 0x100000
#define PM_BLE_SCAN_PART_SIZE 0x100000
#define PM_BLE_SCAN_PART_NAME ble_scan_part
#define PM_BLE_SCAN_PART_ID 0
#define PM_ble_scan_part_ID PM_BLE_SCAN_PART_ID
#define PM_ble_scan_part_IS_ENABLED 1
#define PM_0_LABEL BLE_SCAN_PART
#define PM_BLE_SCAN_PART_DEV DT_CHOSEN(nordic_pm_ext_flash)
#define PM_BLE_SCAN_PART_DEFAULT_DRIVER_KCONFIG CONFIG_NORDIC_QSPI_NOR
#define PM_EXTERNAL_FLASH_OFFSET 0x0
#define PM_EXTERNAL_FLASH_ADDRESS 0x0
#define PM_EXTERNAL_FLASH_END_ADDRESS 0x1000000
#define PM_EXTERNAL_FLASH_SIZE 0x1000000
#define PM_EXTERNAL_FLASH_NAME external_flash
#define PM_EXTERNAL_FLASH_ID 1
#define PM_external_flash_ID PM_EXTERNAL_FLASH_ID
#define PM_external_flash_IS_ENABLED 1
#define PM_1_LABEL EXTERNAL_FLASH
#define PM_EXTERNAL_FLASH_DEV DT_CHOSEN(nordic_pm_ext_flash)
#define PM_EXTERNAL_FLASH_DEFAULT_DRIVER_KCONFIG CONFIG_NORDIC_QSPI_NOR
#define PM_MCUBOOT_OFFSET 0x0
#define PM_MCUBOOT_ADDRESS 0x0
#define PM_MCUBOOT_END_ADDRESS 0x10000
#define PM_MCUBOOT_SIZE 0x10000
#define PM_MCUBOOT_NAME mcuboot
#define PM_MCUBOOT_ID 2
#define PM_mcuboot_ID PM_MCUBOOT_ID
#define PM_mcuboot_IS_ENABLED 1
#define PM_2_LABEL MCUBOOT
#define PM_MCUBOOT_DEV flash_controller
#define PM_MCUBOOT_DEFAULT_DRIVER_KCONFIG CONFIG_SOC_FLASH_NRF
#define PM_MCUBOOT_PAD_OFFSET 0x10000
#define PM_MCUBOOT_PAD_ADDRESS 0x10000
#define PM_MCUBOOT_PAD_END_ADDRESS 0x10200
#define PM_MCUBOOT_PAD_SIZE 0x200
#define PM_MCUBOOT_PAD_NAME mcuboot_pad
#define PM_MCUBOOT_PAD_ID 3
#define PM_mcuboot_pad_ID PM_MCUBOOT_PAD_ID
#define PM_mcuboot_pad_IS_ENABLED 1
#define PM_3_LABEL MCUBOOT_PAD
#define PM_MCUBOOT_PAD_DEV flash_controller
#define PM_MCUBOOT_PAD_DEFAULT_DRIVER_KCONFIG CONFIG_SOC_FLASH_NRF
#define PM_MCUBOOT_PRIMARY_OFFSET 0x10000
#define PM_MCUBOOT_PRIMARY_ADDRESS 0x10000
#define PM_MCUBOOT_PRIMARY_END_ADDRESS 0xf0000
#define PM_MCUBOOT_PRIMARY_SIZE 0xe0000
#define PM_MCUBOOT_PRIMARY_NAME mcuboot_primary
#define PM_MCUBOOT_PRIMARY_ID 4
#define PM_mcuboot_primary_ID PM_MCUBOOT_PRIMARY_ID
#define PM_mcuboot_primary_IS_ENABLED 1
#define PM_4_LABEL MCUBOOT_PRIMARY
#define PM_MCUBOOT_PRIMARY_DEV flash_controller
#define PM_MCUBOOT_PRIMARY_DEFAULT_DRIVER_KCONFIG CONFIG_SOC_FLASH_NRF
#define PM_APP_OFFSET 0x10200
#define PM_APP_ADDRESS 0x10200
#define PM_APP_END_ADDRESS 0xf0000
#define PM_APP_SIZE 0xdfe00
#define PM_APP_NAME app
#define PM_APP_ID 5
#define PM_app_ID PM_APP_ID
#define PM_app_IS_ENABLED 1
#define PM_5_LABEL APP
#define PM_APP_DEV flash_controller
#define PM_APP_DEFAULT_DRIVER_KCONFIG CONFIG_SOC_FLASH_NRF
#define PM_MCUBOOT_PRIMARY_APP_OFFSET 0x10200
#define PM_MCUBOOT_PRIMARY_APP_ADDRESS 0x10200
#define PM_MCUBOOT_PRIMARY_APP_END_ADDRESS 0xf0000
#define PM_MCUBOOT_PRIMARY_APP_SIZE 0xdfe00
#define PM_MCUBOOT_PRIMARY_APP_NAME mcuboot_primary_app
#define PM_MCUBOOT_PRIMARY_APP_ID 6
#define PM_mcuboot_primary_app_ID PM_MCUBOOT_PRIMARY_APP_ID
#define PM_mcuboot_primary_app_IS_ENABLED 1
#define PM_6_LABEL MCUBOOT_PRIMARY_APP
#define PM_MCUBOOT_PRIMARY_APP_DEV flash_controller
#define PM_MCUBOOT_PRIMARY_APP_DEFAULT_DRIVER_KCONFIG CONFIG_SOC_FLASH_NRF
#define PM_SETTINGS_STORAGE_OFFSET 0xf0000
#define PM_SETTINGS_STORAGE_ADDRESS 0xf0000
#define PM_SETTINGS_STORAGE_END_ADDRESS 0xfc000
#define PM_SETTINGS_STORAGE_SIZE 0xc000
#define PM_SETTINGS_STORAGE_NAME settings_storage
#define PM_SETTINGS_STORAGE_ID 7
#define PM_settings_storage_ID PM_SETTINGS_STORAGE_ID
#define PM_settings_storage_IS_ENABLED 1
#define PM_7_LABEL SETTINGS_STORAGE
#define PM_SETTINGS_STORAGE_DEV flash_controller
#define PM_SETTINGS_STORAGE_DEFAULT_DRIVER_KCONFIG CONFIG_SOC_FLASH_NRF
#define PM_TEMPORARY_STORAGE_OFFSET 0xfc000
#define PM_TEMPORARY_STORAGE_ADDRESS 0xfc000
#define PM_TEMPORARY_STORAGE_END_ADDRESS 0x100000
#define PM_TEMPORARY_STORAGE_SIZE 0x4000
#define PM_TEMPORARY_STORAGE_NAME temporary_storage
#define PM_TEMPORARY_STORAGE_ID 8
#define PM_temporary_storage_ID PM_TEMPORARY_STORAGE_ID
#define PM_temporary_storage_IS_ENABLED 1
#define PM_8_LABEL TEMPORARY_STORAGE
#define PM_TEMPORARY_STORAGE_DEV flash_controller
#define PM_TEMPORARY_STORAGE_DEFAULT_DRIVER_KCONFIG CONFIG_SOC_FLASH_NRF
#define PM_MCUBOOT_SECONDARY_OFFSET 0x100000
#define PM_MCUBOOT_SECONDARY_ADDRESS 0x100000
#define PM_MCUBOOT_SECONDARY_END_ADDRESS 0x1e0000
#define PM_MCUBOOT_SECONDARY_SIZE 0xe0000
#define PM_MCUBOOT_SECONDARY_NAME mcuboot_secondary
#define PM_MCUBOOT_SECONDARY_ID 9
#define PM_mcuboot_secondary_ID PM_MCUBOOT_SECONDARY_ID
#define PM_mcuboot_secondary_IS_ENABLED 1
#define PM_9_LABEL MCUBOOT_SECONDARY
#define PM_MCUBOOT_SECONDARY_DEV DT_CHOSEN(nordic_pm_ext_flash)
#define PM_MCUBOOT_SECONDARY_DEFAULT_DRIVER_KCONFIG CONFIG_NORDIC_QSPI_NOR
#define PM_GNSS_PART_OFFSET 0x1e0000
#define PM_GNSS_PART_ADDRESS 0x1e0000
#define PM_GNSS_PART_END_ADDRESS 0x220000
#define PM_GNSS_PART_SIZE 0x40000
#define PM_GNSS_PART_NAME gnss_part
#define PM_GNSS_PART_ID 10
#define PM_gnss_part_ID PM_GNSS_PART_ID
#define PM_gnss_part_IS_ENABLED 1
#define PM_10_LABEL GNSS_PART
#define PM_GNSS_PART_DEV DT_CHOSEN(nordic_pm_ext_flash)
#define PM_GNSS_PART_DEFAULT_DRIVER_KCONFIG CONFIG_NORDIC_QSPI_NOR
#define PM_TEMP_PART_OFFSET 0x220000
#define PM_TEMP_PART_ADDRESS 0x220000
#define PM_TEMP_PART_END_ADDRESS 0x230000
#define PM_TEMP_PART_SIZE 0x10000
#define PM_TEMP_PART_NAME temp_part
#define PM_TEMP_PART_ID 11
#define PM_temp_part_ID PM_TEMP_PART_ID
#define PM_temp_part_IS_ENABLED 1
#define PM_11_LABEL TEMP_PART
#define PM_TEMP_PART_DEV DT_CHOSEN(nordic_pm_ext_flash)
#define PM_TEMP_PART_DEFAULT_DRIVER_KCONFIG CONFIG_NORDIC_QSPI_NOR
#define PM_SRAM_PRIMARY_OFFSET 0x0
#define PM_SRAM_PRIMARY_ADDRESS 0x20000000
#define PM_SRAM_PRIMARY_END_ADDRESS 0x20040000
#define PM_SRAM_PRIMARY_SIZE 0x40000
#define PM_SRAM_PRIMARY_NAME sram_primary
#define PM_NUM 12
#define PM_ALL_BY_SIZE "mcuboot_pad temporary_storage settings_storage mcuboot temp_part gnss_part sram_primary app mcuboot_primary_app mcuboot_secondary mcuboot_primary ble_scan_part external_flash"
#define PM_ADDRESS 0x0
#define PM_SIZE 0x10000
#define PM_SRAM_ADDRESS 0x20000000
#define PM_SRAM_SIZE 0x40000
#endif /* PM_CONFIG_H__ */

This fix was applied: https://github.com/nrfconnect/sdk-nrf/pull/9169/files

But questions still exit. 

Related