Commit 18fee817 authored by Jamie McCrae's avatar Jamie McCrae Committed by Jamie
Browse files

zephyr: Add support for swap using offset mode



Adds support for using this mode to zephyr

Signed-off-by: default avatarJamie McCrae <jamie.mccrae@nordicsemi.no>
parent 4af73169
Loading
Loading
Loading
Loading
+57 −24
Original line number Diff line number Diff line
@@ -125,11 +125,17 @@ zephyr_library_sources(
  )
endif()

if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_SINGLE_APPLICATION_SLOT_RAM_LOAD)
if(CONFIG_SINGLE_APPLICATION_SLOT)
  zephyr_library_sources(
    ${BOOT_DIR}/zephyr/single_loader.c
    )
  zephyr_library_include_directories(${BOOT_DIR}/bootutil/src)
elseif(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_SINGLE_APPLICATION_SLOT_RAM_LOAD)
  zephyr_library_sources(
    ${BOOT_DIR}/zephyr/single_loader.c
    ${BOOT_DIR}/bootutil/src/ram_load.c
    )
  zephyr_library_include_directories(${BOOT_DIR}/bootutil/src)
elseif(CONFIG_BOOT_FIRMWARE_LOADER)
  zephyr_library_sources(
    ${BOOT_DIR}/zephyr/firmware_loader.c
@@ -139,17 +145,29 @@ else()
  zephyr_library_sources(
    ${BOOT_DIR}/bootutil/src/loader.c
    ${BOOT_DIR}/bootutil/src/swap_misc.c
  ${BOOT_DIR}/bootutil/src/swap_scratch.c
  ${BOOT_DIR}/bootutil/src/swap_move.c
    ${BOOT_DIR}/bootutil/src/caps.c
    )
endif()

  if(CONFIG_BOOT_SWAP_USING_MOVE)
    zephyr_library_sources(
      ${BOOT_DIR}/bootutil/src/swap_move.c
      )
  elseif(CONFIG_BOOT_SWAP_USING_OFFSET)
    zephyr_library_sources(
      ${BOOT_DIR}/bootutil/src/swap_offset.c
      )
  else()
    zephyr_library_sources(
      ${BOOT_DIR}/bootutil/src/swap_scratch.c
      )

    if(CONFIG_BOOT_RAM_LOAD OR CONFIG_SINGLE_APPLICATION_SLOT_RAM_LOAD)
      zephyr_library_sources(
        ${BOOT_DIR}/bootutil/src/ram_load.c
        )
    endif()
  endif()
endif()

if(CONFIG_BOOT_SIGNATURE_TYPE_ECDSA_P256 OR CONFIG_BOOT_ENCRYPT_EC256)
  if(MBEDTLS_ASN1_DIR)
@@ -427,7 +445,7 @@ dt_get_parent(slot0_flash)
dt_prop(erase_size_slot0 PATH "${slot0_flash}" PROPERTY "erase-block-size")
dt_prop(write_size_slot0 PATH "${slot0_flash}" PROPERTY "write-block-size")

if(CONFIG_BOOT_SWAP_USING_MOVE)
if(CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_SWAP_USING_OFFSET)
  if(DEFINED erase_size_slot0)
    zephyr_compile_definitions("MCUBOOT_SLOT0_EXPECTED_ERASE_SIZE=${erase_size_slot0}")
  endif()
@@ -445,7 +463,7 @@ if(NOT CONFIG_SINGLE_APPLICATION_SLOT AND NOT CONFIG_SINGLE_APPLICATION_SLOT_RAM
  dt_prop(erase_size_slot1 PATH "${slot1_flash}" PROPERTY "erase-block-size")
  dt_prop(write_size_slot1 PATH "${slot1_flash}" PROPERTY "write-block-size")

  if(CONFIG_BOOT_SWAP_USING_MOVE)
  if(CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_SWAP_USING_OFFSET)
    if(DEFINED erase_size_slot1)
      zephyr_compile_definitions("MCUBOOT_SLOT1_EXPECTED_ERASE_SIZE=${erase_size_slot1}")
    endif()
@@ -487,12 +505,12 @@ if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO)
  endif()
endif()

if((CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE) AND (DEFINED write_size_slot0 OR DEFINED write_size_slot1))
if((CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_SWAP_USING_OFFSET) AND (DEFINED write_size_slot0 OR DEFINED write_size_slot1))
  zephyr_library_sources(flash_check.c)
endif()

if(SYSBUILD)
  if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER OR CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_UPGRADE_ONLY OR CONFIG_BOOT_DIRECT_XIP OR CONFIG_BOOT_RAM_LOAD)
  if(CONFIG_SINGLE_APPLICATION_SLOT OR CONFIG_BOOT_FIRMWARE_LOADER OR CONFIG_BOOT_SWAP_USING_SCRATCH OR CONFIG_BOOT_SWAP_USING_MOVE OR CONFIG_BOOT_SWAP_USING_OFFSET OR CONFIG_BOOT_UPGRADE_ONLY OR CONFIG_BOOT_DIRECT_XIP OR CONFIG_BOOT_RAM_LOAD)
    # TODO: RAM LOAD support
    dt_nodelabel(slot0_flash NODELABEL "slot0_partition")
    dt_get_parent(slot0_flash)
@@ -617,6 +635,17 @@ if(SYSBUILD)
          math(EXPR boot_status_data_size "128 * (3 * ${write_size})")
        endif()
      endif()
    elseif(CONFIG_BOOT_SWAP_USING_OFFSET)
      if(CONFIG_BOOT_MAX_IMG_SECTORS_AUTO AND DEFINED slot_min_sectors AND "${slot_min_sectors}" GREATER "0")
        math(EXPR boot_status_data_size "${slot_min_sectors} * (2 * ${write_size})")
      else()
        if(CONFIG_BOOT_MAX_IMG_SECTORS)
          math(EXPR boot_status_data_size "${CONFIG_BOOT_MAX_IMG_SECTORS} * (2 * ${write_size})")
        else()
          message(WARNING "CONFIG_BOOT_MAX_IMG_SECTORS is not defined, falling back to 128 sector default. Please set CONFIG_BOOT_MAX_IMG_SECTORS to the required value")
          math(EXPR boot_status_data_size "128 * (2 * ${write_size})")
        endif()
      endif()
    else()
      set(boot_status_data_size 0)
    endif()
@@ -634,6 +663,10 @@ if(SYSBUILD)
    if(CONFIG_BOOT_SWAP_USING_MOVE)
      math(EXPR required_size "${required_size} + ${erase_size}")
      math(EXPR required_upgrade_size "${required_upgrade_size} + ${erase_size}")
    elseif(CONFIG_BOOT_SWAP_USING_OFFSET)
#todo: check how different slot sizes are...
#      math(EXPR required_size "${required_size} + ${erase_size}")
#      math(EXPR required_upgrade_size "${required_upgrade_size} + ${erase_size}")
    endif()
  else()
    set(required_size 0)
+24 −4
Original line number Diff line number Diff line
@@ -310,6 +310,13 @@ config BOOT_VALIDATE_SLOT0_ONCE
	  low end devices with as a compromise lowering the security level.
	  If unsure, leave at the default value.

config BOOT_PREFER_SWAP_OFFSET
	bool "Prefer the newer swap offset algorithm"
	help
	  If y, the BOOT_IMAGE_UPGRADE_MODE will default to using "offset" instead of "scratch".
	  This is a separate bool config option, because Kconfig doesn't allow defaults to be
	  overridden in choice options. Most devices should be using swap using offset mode.

config BOOT_PREFER_SWAP_MOVE
	bool "Prefer the newer swap move algorithm"
	default y if SOC_FAMILY_NORDIC_NRF
@@ -318,12 +325,14 @@ config BOOT_PREFER_SWAP_MOVE
	  If y, the BOOT_IMAGE_UPGRADE_MODE will default to using
	  "move" instead of "scratch".  This is a separate bool config
	  option, because Kconfig doesn't allow defaults to be
	  overridden in choice options.  Most devices should be using
	  swap move.
	  overridden in choice options. This mode has been superceded
	  by swap using offset, but is kept to allow existing projects
	  to make use of it.

if !SINGLE_APPLICATION_SLOT
choice BOOT_IMAGE_UPGRADE_MODE
	prompt "Image upgrade modes"
	default BOOT_SWAP_USING_OFFSET if BOOT_PREFER_SWAP_OFFSET
	default BOOT_SWAP_USING_MOVE if BOOT_PREFER_SWAP_MOVE
	default BOOT_SWAP_USING_SCRATCH

@@ -340,8 +349,19 @@ config BOOT_UPGRADE_ONLY
	  of swapping them. This prevents the fallback recovery, but
	  uses a much simpler code path.

config BOOT_SWAP_USING_OFFSET
	bool "Swap using offset mode without scratch partition"
	help
	  If y, the swap upgrade is done by each sector X+1 in the secondary slot moved index X in
	  the primary slot, then the sector at X+1 in the primary is moved to index X in the
	  secondary.
	  This allows a swap upgrade without using a scratch partition, but is currently limited
	  to all sectors in both slots being of the same size. This mode offers faster swap times
	  with less flash endurance usage than swap using move, firmware updates must be placed at
	  the second sector in the second slot instead of the first.

config BOOT_SWAP_USING_MOVE
	bool "Swap mode that can run without a scratch partition"
	bool "Swap using mode mode without scratch partition"
	help
	  If y, the swap upgrade is done in two steps, where first every
	  sector of the primary slot is moved up one sector, then for
@@ -779,7 +799,7 @@ config MCUBOOT_DOWNGRADE_PREVENTION
config MCUBOOT_DOWNGRADE_PREVENTION_SECURITY_COUNTER
	bool "Use image security counter instead of version number"
	depends on MCUBOOT_DOWNGRADE_PREVENTION
	depends on (BOOT_SWAP_USING_MOVE || BOOT_SWAP_USING_SCRATCH)
	depends on (BOOT_SWAP_USING_MOVE || BOOT_SWAP_USING_SCRATCH || BOOT_SWAP_USING_OFFSET)
	help
       Security counter is used for version eligibility check instead of pure
       version.  When this option is set, any upgrade must have greater or
+4 −0
Original line number Diff line number Diff line
@@ -84,6 +84,10 @@
#define MCUBOOT_SWAP_USING_MOVE 1
#endif

#ifdef CONFIG_BOOT_SWAP_USING_OFFSET
#define MCUBOOT_SWAP_USING_OFFSET 1
#endif

#ifdef CONFIG_BOOT_DIRECT_XIP
#define MCUBOOT_DIRECT_XIP
#endif
+1 −1
Original line number Diff line number Diff line
@@ -54,7 +54,7 @@ static inline uint32_t __flash_area_ids_for_slot(int img, int slot)
#define FLASH_AREA_IMAGE_PRIMARY(x) __flash_area_ids_for_slot(x, 0)
#define FLASH_AREA_IMAGE_SECONDARY(x) __flash_area_ids_for_slot(x, 1)

#if !defined(CONFIG_BOOT_SWAP_USING_MOVE)
#if !defined(CONFIG_BOOT_SWAP_USING_MOVE) && !defined(CONFIG_BOOT_SWAP_USING_OFFSET)
#define FLASH_AREA_IMAGE_SCRATCH    FIXED_PARTITION_ID(scratch_partition)
#endif

+6 −0
Original line number Diff line number Diff line
@@ -90,3 +90,9 @@ tests:
    platform_allow: mimxrt1020_evk
    integration_platforms:
      - mimxrt1020_evk
  sample.bootloader.mcuboot.swap_offset:
    extra_args: EXTRA_CONF_FILE=./swap_offset.conf
    platform_allow:  nrf52840dk/nrf52840
    integration_platforms:
      - nrf52840dk/nrf52840
    tags: bootloader_mcuboot
Loading