MCUboot flash overflow

Hello,
I am encountering a flash overflow issue in MCUboot when enabling external flash over SPI. After configuring SPI memory in the .conf file and adding external flash support in the .dts file, the bootloader's memory usage increases. This leads to a compilation error, with the flash memory exceeding the allocated size by 7 kB. Below are the details of my setup.

Environment:

  • NCS Version: 2.7.0
  • Default MCUboot Size: 48 kB
  • Current MCUboot Memory Usage:
    • Without external flash (SPI/QSPI): ~43 kB
    • With external SPI flash defined (not used, only internal flash utilized in ): Overflow by 7 kB

Configurations and Files:

1. Device Tree (.dts)

chosen {
    zephyr,sram = &sram0;
    zephyr,flash = &flash0;
    zephyr,code-partition = &slot0_partition;
    nordic,pm-ext-flash = &mx25l12833f;
};
&spi1 {
    compatible = "nordic,nrf-spim";
    status = "okay";
    pinctrl-0 = <&spi1_default>;
    pinctrl-1 = <&spi1_sleep>;
    pinctrl-names = "default", "sleep";
    cs-gpios = <&gpio0 21 GPIO_ACTIVE_LOW>;
    mx25l12833f: mx25l12833f@0 {
        compatible = "jedec,spi-nor";
        reg = <0>;
        spi-max-frequency = <8000000>;
        sfdp-bfp = [
            e5 20 19 00  /* SFDP Header */
            ff 00 ff 00  /* Erase Command, Write Granularity */
            eb 44 55 ff  /* 4KB Erase Command, Page Size */
            ff ff ff ff  /* Other Command Set */
        ];
        jedec-id = [ c2 20 18 ];
        size = <DT_SIZE_M(128)>;
        has-dpd;
        t-enter-dpd = <10000>;
        t-exit-dpd = <5000>;
        status = "okay";
    };
};
&flash0 {
    partitions {
        compatible = "fixed-partitions";
        #address-cells = <1>;
        #size-cells = <1>;
        boot_partition: partition@0 {
            label = "mcuboot";
            reg = <0x00000000 0xC000>;
        };
        slot0_partition: partition@C000 {
            label = "image-0";
            reg = <0x0000C000 0xE0000>;
        };
        storage_partition: partition@EC000 {
            label = "storage";
            reg = <0x000EC000 0x14000>;
        };
    };
};

2. Partition Manager (pm_static.yml)

external_flash:
  address: 0x0
  size: 0x1000000  # 16 MB
  device: DT_CHOSEN(nordic_pm_ext_flash)
  region: external_flash  
app:
  address: 0xc200
  region: flash_primary
  size: 0xE0E00
mcuboot:
  address: 0x0
  region: flash_primary
  size: 0xc000
mcuboot_pad:
  address: 0xc000
  region: flash_primary
  size: 0x200  # 48 KB
mcuboot_primary:
  address: 0xc000
  orig_span: &id001
  - mcuboot_pad
  - app
  region: flash_primary
  size: 0xE0000   # 920 KB
  span: *id001
mcuboot_primary_app:
  address: 0xc200
  orig_span: &id002
  - app
  region: flash_primary
  size: 0xEC000
  span: *id002
settings_storage:
  address: 0xEC000
  region: flash_primary
  size: 0x14000
mcuboot_secondary:
  address: 0xF20000
  size: 0xE0000   # 920 KB
  device: DT_CHOSEN(nordic_pm_ext_flash)
  placement:
    align:
      start: 0x4
  region: external_flash

3. Application Configuration (prj.conf)

# FLASH usage optimization
CONFIG_SIZE_OPTIMIZATIONS=y
CONFIG_COMPILER_OPT="-DNDEBUG"
CONFIG_MAIN_STACK_SIZE=8192
CONFIG_BOOTLOADER_MCUBOOT=y
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096
CONFIG_HEAP_MEM_POOL_SIZE=2048
CONFIG_REBOOT=y
CONFIG_THREAD_NAME=y

# FPU
CONFIG_FPU=y
CONFIG_FP_HARDABI=y

CONFIG_GPIO=y
CONFIG_SPI=y
CONFIG_SERIAL=y
CONFIG_WATCHDOG=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_STREAM_FLASH=y

# Flash occupancy optimization
CONFIG_TIMESLICING=n
CONFIG_LOG=n
CONFIG_PRINTK=n
CONFIG_NCS_BOOT_BANNER=n
CONFIG_BOOT_BANNER=n
CONFIG_ASSERT=n
CONFIG_CONSOLE=n
CONFIG_EARLY_CONSOLE=n
CONFIG_DYNAMIC_INTERRUPTS=n

# Enable MCUMGR 
CONFIG_MCUMGR=y
CONFIG_IMG_MANAGER=y
CONFIG_ZCBOR=y

CONFIG_DISABLE_FLASH_PATCH=y

CONFIG_SPI_NOR_IDLE_IN_DPD=y
CONFIG_SPI_NOR=y
CONFIG_SPI_NOR_FLASH_LAYOUT_PAGE_SIZE=4096

# SYS CONFIG
CONFIG_SETTINGS=y
CONFIG_CPU_LOAD=y

# TIMER
CONFIG_NRFX_TIMER1=n

CONFIG_USB_CDC_TERMINAL=y
CONFIG_UART_ASYNC_API=n
CONFIG_USB_DEVICE_STACK=y
CONFIG_UART_LINE_CTRL=y
CONFIG_USB_DEVICE_INITIALIZE_AT_BOOT=n

# Enable nordic security backend and PSA APIs
CONFIG_NRF_SECURITY=y
CONFIG_MBEDTLS_PSA_CRYPTO_C=y
CONFIG_MBEDTLS_ENABLE_HEAP=y
CONFIG_MBEDTLS_HEAP_SIZE=8192
CONFIG_PSA_WANT_GENERATE_RANDOM=y
CONFIG_PSA_WANT_KEY_TYPE_AES=y
CONFIG_PSA_WANT_ALG_CBC_NO_PADDING=y
CONFIG_PSA_WANT_ALG_ECB_NO_PADDING=y

# Using hardware crypto accelerator
CONFIG_PSA_CRYPTO_DRIVER_OBERON=n
CONFIG_PSA_CRYPTO_DRIVER_CC3XX=y

4. MCUboot Configuration (mcuboot.conf)

CONFIG_MULTITHREADING=y
CONFIG_CONSOLE=n
CONFIG_UART_CONSOLE=n
CONFIG_CONSOLE_HANDLER=n
CONFIG_FLASH=y


Problem: Defining external SPI flash in the configuration (even without actively using it for MCUboot) causes the bootloader size to increase by ~9 kB. This results in a compilation error due to flash memory overflow by ~7 kB. I suspect the issue lies in additional driver inclusion or configuration conflicts caused by enabling SPI flash.

Even with configuration of memory layout like below without using external memory for mcuboot, defining external memory over SPI in .dts causes flash overflow:

&flash0 {

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

		boot_partition: partition@0 {
			label = "mcuboot";
			reg = <0x000000000 0xC000>;
		};
		slot0_partition: partition@C000 {
			label = "image-0";
			reg = <0x0000C000 0x77000>;
		};
		slot1_partition: partition@83000 {
			label = "image-1";
			reg = <0x00083000 0x77000>;
		};
		storage_partition: partition@FA000 {
			label = "storage";
			reg = <0x000FA000 0x00006000>;
		};
	};
};

app:
  address: 0xc200
  region: flash_primary
  size: 0x77E00
mcuboot:
  address: 0x0
  region: flash_primary
  size: 0xc000
mcuboot_pad:
  address: 0xc000
  region: flash_primary
  size: 0x200
mcuboot_primary:
  address: 0xc000
  orig_span: &id001
  - mcuboot_pad
  - app
  region: flash_primary
  size: 0x77000
  span: *id001
mcuboot_primary_app:
  address: 0xc200
  orig_span: &id002
  - app
  region: flash_primary
  size: 0x83000
  span: *id002
mcuboot_secondary:
  address: 0x83000
  region: flash_primary
  size: 0x77000
settings_storage:
  address: 0xFA000
  region: flash_primary
  size: 0x6000

Questions:

  1. What could be causing this significant increase in MCUboot memory usage?
  2. Are there optimization steps or alternative configurations to mitigate this flash memory usage?
Related