MCUboot crashes with USAGE FAULT in bootutil_max_image_size() when built with LTO (secure boot + firmware updater)

Environment

  • nRF Connect SDK v3.4.2, nRF54LM20A (nRF54LM20 DK), MCUboot from nrfconnect/sdk-mcuboot
  • sysbuild conf:
    SB_CONFIG_SECURE_BOOT_APPCORE=y
    SB_CONFIG_BOOTLOADER_MCUBOOT=y
    SB_CONFIG_MCUBOOT_MODE_FIRMWARE_UPDATER=y
    SB_CONFIG_MCUBOOT_MODE_FIRMWARE_UPDATER_BOOT_MODE_ENTRANCE=y
  • mcuboot conf:
    CONFIG_LTO=y
    CONFIG_ISR_TABLES_LOCAL_DECLARATION=y
    CONFIG_BOOT_BANNER=n
    CONFIG_NCS_BOOT_BANNER=n

Problem


NSIB validates and boots MCUboot, then MCUboot faults before chainloading:

I: Starting bootloader
I: LCS-awareness disabled, skipping LCS check
E: ***** USAGE FAULT *****
E: Attempt to execute undefined instruction
E: Faulting instruction address (r15/pc): 0x14290
E: >>> ZEPHYR FATAL ERROR 36: Unknown error on CPU 0

0x14290 resolves to bootutil_max_image_size() (boot/bootutil/src/bootutil_misc.c:336), reached via validate_image_slot() → boot_image_validate() → bootutil_img_validate().


Analysis
boot_image_validate() in boot/zephyr/firmware_loader.c calls bootutil_img_validate(NULL, …), i.e. passes state == NULL. That is safe upstream, where bootutil_max_image_size() starts with #if defined(MCUBOOT_FIRMWARE_LOADER) … (void) state; and never touches it.

The [nrf noup] block added in af9d2860 (“treewide: Add support for sysbuild assigned images”) sits above that guard and evaluates BOOT_CURR_IMG(state) → state->curr_img_idx. With secure boot, CONFIG_UPDATEABLE_IMAGE_NUMBER=2, so BOOT_IMAGE_NUMBER > 1 and the macro really does dereference state.
Without LTO it only reads a stray flash byte at NULL + offsetof(curr_img_idx) (UB, but “works”); with LTO the compiler turns it into udf.

Suggested fix

if (state != NULL && BOOT_CURR_IMG(state) == CONFIG_MCUBOOT_MCUBOOT_IMAGE_NUMBER) {

or move the NSIB block below the (void) state; in the MCUBOOT_FIRMWARE_LOADER branch.

Related