#
# Copyright (c) 2021 Nordic Semiconductor ASA
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

# This file is sourced by the MCUboot sample Kconfig file
# See [mcuboot_dir]/boot/zephyr/Kconfig

config BOOT_USE_MIN_PARTITION_SIZE
	bool "Build for a minimal image size"

partition=MCUBOOT_SCRATCH
partition-size=0x1e000
source "${ZEPHYR_BASE}/../nrf/subsys/partition_manager/Kconfig.template.partition_config"

partition=MCUBOOT_PAD
if SOC_SERIES_NRF54LX
	partition-size=0x800
source "${ZEPHYR_BASE}/../nrf/subsys/partition_manager/Kconfig.template.partition_config"
endif
if !SOC_SERIES_NRF54LX
	partition-size=0x200
source "${ZEPHYR_BASE}/../nrf/subsys/partition_manager/Kconfig.template.partition_config"
endif

config PM_PARTITION_SIZE_MCUBOOT
	hex
	default 0x3e00
	depends on (BOOT_NRF_EXTERNAL_CRYPTO && BOOT_USE_MIN_PARTITION_SIZE)

config PM_PARTITION_SIZE_MCUBOOT
	hex
	default FPROTECT_BLOCK_SIZE if SOC_SERIES_NRF91X || SOC_NRF5340_CPUAPP
	default 0x7c00
	depends on BOOT_USE_MIN_PARTITION_SIZE && !BOOT_NRF_EXTERNAL_CRYPTO

config PM_PARTITION_SIZE_MCUBOOT
	hex "Flash space allocated for the MCUboot partition" if !BOOT_USE_MIN_PARTITION_SIZE
	default 0xc000
	help
	  Flash space set aside for the MCUboot partition.

config BOOT_USE_NRF_CC310_BL
	bool
	select NRF_CC310_BL

config BOOT_USE_NRF_EXTERNAL_CRYPTO
	bool
	# Hidden option
	default n
	# When building for ECDSA, we use our own copy of mbedTLS, so the
	# Zephyr one must not be enabled or the MBEDTLS_CONFIG_FILE macros
	# will collide.
	depends on ! MBEDTLS
	help
	  Use Shared crypto for crypto primitives.

config MCUBOOT_NRF_CLEANUP_PERIPHERAL
	bool "Perform peripheral cleanup before chain-load the application"
	depends on SOC_FAMILY_NORDIC_NRF
	default y

config MCUBOOT_NRF_CLEANUP_NONSECURE_RAM
	bool "Perform non-secure RAM cleanup before chain-load the application"
	depends on SOC_FAMILY_NORDIC_NRF && ARM_TRUSTZONE_M
	default y if MCUBOOT_USE_ALL_AVAILABLE_RAM

config BOOT_SIGNATURE_KEY_FILE
	string "MCUBoot PEM key file"
	depends on !MCUBOOT_BUILD_STRATEGY_FROM_SOURCE
	help
	  Absolute path to PEM key file containing the private key corresponding
	  to the public key included in MCUBoot. This will be used to sign the
	  image so that it can be verified by MCUBoot. Since MCUBoot is not
	  built from source, it is not possible for the build system to deduce
	  what key was used when compiling it. Hence, it is required that the
	  key is passed to the build system through this option.

choice BOOT_SIGNATURE_TYPE
	default BOOT_SIGNATURE_TYPE_ECDSA_P256 if HAS_HW_NRF_CC310 && !BOARD_THINGY91_NRF9160 && !BOARD_THINGY91_NRF52840

endchoice

choice BOOT_ECDSA_IMPLEMENTATION
	default BOOT_NRF_EXTERNAL_CRYPTO if SECURE_BOOT
	default BOOT_ECDSA_CC310 if HAS_HW_NRF_CC310

config BOOT_NRF_EXTERNAL_CRYPTO
	bool "Use Shared Crypto from bootloader"
	select BOOT_USE_NRF_EXTERNAL_CRYPTO

endchoice

config BOOT_ERASE_PROGRESSIVELY
	bool "Erase flash progressively when receiving new firmware"
	default y if SOC_FAMILY_NORDIC_NRF
	help
	 If enabled, flash is erased as necessary when receiving new firmware,
	 instead of erasing the whole image slot at once. This is necessary
	 on some hardware that has long erase times, to prevent long wait
	 times at the beginning of the DFU process.

config BOOT_IMAGE_ACCESS_HOOKS
	bool
	default y if UPDATEABLE_IMAGE_NUMBER > 1 && SOC_NRF5340_CPUAPP && PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY
	depends on MCUBOOT

config BOOT_IMAGE_ACCESS_HOOK_NRF5340
	bool "Enable default nRF5340 hook"
	default y if BOOT_IMAGE_ACCESS_HOOKS
	depends on BOOT_IMAGE_ACCESS_HOOKS
	help
	  This enables the default nRF5340 image hook for MCUboot, which
	  supports upgrading the network core image from the application core.
	  To disable or override this feature, disable this option and add a
	  custom file using CMake.

DT_COMPAT_SIM_FLASH:= zephyr,sim-flash
DT_SIM_FLASH_PATH := $(dt_nodelabel_path,flash_sim0)

config NRF53_MULTI_IMAGE_UPDATE
	bool "Support updating app core and net core in a single operation"
	default y
	depends on UPDATEABLE_IMAGE_NUMBER > 1
	depends on SOC_NRF5340_CPUAPP
	depends on BOOT_IMAGE_ACCESS_HOOKS
	depends on FLASH_SIMULATOR
	depends on FLASH_SIMULATOR_DOUBLE_WRITES
	depends on !FLASH_SIMULATOR_STATS
	depends on PM_EXTERNAL_FLASH_MCUBOOT_SECONDARY
	depends on BOOT_UPGRADE_ONLY || USE_NRF53_MULTI_IMAGE_WITHOUT_UPGRADE_ONLY
	depends on $(dt_compat_enabled,$(DT_COMPAT_SIM_FLASH))
	select NRF53_MCUBOOT_PRIMARY_1_RAM_FLASH

config NRF53_RECOVERY_NETWORK_CORE
	bool "Serial recovery for the NET-core in the one image pair mode"
	depends on !NRF53_MULTI_IMAGE_UPDATE
	depends on SOC_NRF5340_CPUAPP
	depends on BOOT_IMAGE_ACCESS_HOOKS
	depends on FLASH_SIMULATOR
	depends on FLASH_SIMULATOR_DOUBLE_WRITES
	depends on !FLASH_SIMULATOR_STATS
	depends on MCUBOOT_SERIAL_DIRECT_IMAGE_UPLOAD
	depends on $(dt_compat_enabled,$(DT_COMPAT_SIM_FLASH))
	select NRF53_MCUBOOT_PRIMARY_1_RAM_FLASH
	help
	  If enabled, allows to upload the firmware to the network core
	  using serial recovery despite the fact that NRF53_MULTI_IMAGE_UPDATE
	  is not selected.

# hidden property for enabling the network core ram flash partition in PM
config NRF53_MCUBOOT_PRIMARY_1_RAM_FLASH
	bool

config NRF53_RAM_FLASH_SIZE
	hex
	depends on NRF53_MCUBOOT_PRIMARY_1_RAM_FLASH
	default $(dt_node_reg_size_hex,$(DT_SIM_FLASH_PATH))
	help
	  When performing a multi image update on the nRF53 the net core data
	  is read from external flash, and buffered in  RAM. This option is used
	  to define forward the size of this RAM region to the partition manager
	  configuration.

config MCUBOOT_CLEANUP_UNUSABLE_SECONDARY
	bool "Clean-up unusable secondary slots"
	default y if FLASH_LOAD_OFFSET != 0 || SOC_NRF5340_CPUAPP
	help
	  If enbales, the mcuboot performs clean-up content of all the secondary slots
	  which contains valid header but couldn't be assigned to any of
	  supported primary images.
	  This behaviour is desired when configuration allows to use one secondary
	  slot for collecting image for multiple primary slots.
