if ARCH_MEDIATEK || ARCH_MTMIPS

menu "MediaTek specific configurations"

menuconfig MEDIATEK_BOOTMENU
	bool "Enable bootmenu framework"
	select CMD_BOOTMENU
	select CMD_BOOTMENU_ENTER_UBOOT_CONSOLE
	select OF_SYSTEM_SETUP
	select OF_LIBFDT_OVERLAY
	select PARTITIONS
	default n
	help
	  Enable this option will allow boards to use a bootmenu containing
	  features for easy upgrading/booting.

if MEDIATEK_BOOTMENU

config MTK_BOOTMENU_MTD_COMMON
	bool
	select MTD
	select MTD_PARTITIONS

config MTK_BOOTMENU_MMC_COMMON
	bool
	select MMC
	select MMC_WRITE
	select MMC_HW_PARTITIONING

config MTK_BOOTMENU_DEFAULT
	bool "Use default bootmenu"
	default y
	help
	  Enable this is you want to use default bootmenu. Otherwise you must
	  provide your own bootmenu implementation.

choice
	prompt "Device type for bootmenu"
	default MTK_BOOTMENU_MTD
	depends on MTK_BOOTMENU_DEFAULT

config MTK_BOOTMENU_MTD
	bool "MTD (NOR/NAND/UBI)"
	select MTK_BOOTMENU_MTD_COMMON
	help
	  This is intended for raw flashes (e.g. SPI-NOR and SPI-NAND). Both
	  raw jffs2 images and UBI-based images which are built on top of MTD
	  are supported.

	  To enable legacy raw jffs2 support (not recommended), please define
	  mtdparts to contain a partition named 'firmware', and do not create
	  any partition named 'kernel' or 'ubi'.

	  To enable UBI-based image support, please select MTK_UBI_SUPPORT
	  first, and define mtdparts to contain a partition named 'ubi', and do
	  not create any partition named 'kernel' or 'firmware'.

config MTK_BOOTMENU_MTD_LEGACY
	bool "MTD (NOR/NAND/UBI) legacy"
	select MTK_BOOTMENU_MTD_COMMON
	depends on ARCH_MTMIPS || TARGET_MT7629
	help
	  Similar to MTK_BOOTMENU_MTD, but single bootloader is used.
	  e.g., MIPS platform, or ARMv7 without ATF.

config MTK_BOOTMENU_MMC
	bool "SD/eMMC"
	select MTK_BOOTMENU_MMC_COMMON
	select SUPPORT_EMMC_BOOT
	select CONFIGURABLE_FIP_LOCATION
	select SUPPORT_FIP_LOCATION_MMC
	help
	  This is intended SD and eMMC. GPT partition table must define two
	  partitions 'kernel' and 'rootfs' to support this image type.
	  Only upgrade images using tar archive are supported.

endchoice

config CONFIGURABLE_FIP_LOCATION
	bool
	default n

config SUPPORT_FIP_LOCATION_MMC
	bool
	default n

choice
	prompt "FIP location"
	depends on CONFIGURABLE_FIP_LOCATION
	default FIP_IN_SD_MMC_UDA if SUPPORT_FIP_LOCATION_MMC

config FIP_IN_SD_MMC_UDA
	bool "UDA (SD/eMMC)"
	depends on SUPPORT_FIP_LOCATION_MMC
	help
	  FIP is stored in UDA (normal user data) described by GPT

config FIP_IN_EMMC_BOOT0
	bool "BOOT0 (eMMC)"
	depends on SUPPORT_FIP_LOCATION_MMC
	help
	  FIP is stored in BOOT0, offset 1MiB. Requires BOOT0 is at lease 4MiB

endchoice

choice
	prompt "FIP2 location"
	depends on MTK_DUAL_FIP
	default FIP2_IN_SD_MMC_UDA

config FIP2_IN_SD_MMC_UDA
	bool "UDA (SD/eMMC)"
	help
	  FIP2 is stored in UDA (normal user data) described by GPT

config FIP2_IN_EMMC_BOOT1
	bool "BOOT1 (eMMC)"
	help
	  FIP2 is stored in BOOT1, offset 1MiB. Requires BOOT0 is at lease 4MiB

endchoice

config MTK_MMC_BOOT_BUS_WIDTH_1B
	bool
	default y if TARGET_MT7622
	default n

config MTK_FIXED_MTD_MTDPARTS
	bool "Do not allow mtdparts to be changed by user"
	depends on MTK_BOOTMENU_MTD_COMMON
	help
	  Enable this option if you do not want the user to change the
	  mtdparts which lead to unexpceted results such as missing ubi
	  partition or changing ubi partition to a different offset.

config MTK_BOOTMENU_MMC_DEV_INDEX
	int "MMC device for bootmenu"
	range 0 1
	default 0
	depends on MTK_BOOTMENU_MMC_COMMON
	help
	  The MMC device id in u-boot to be used for bootmenu.

config MEDIATEK_BOOTMENU_COUNTDOWN
	bool "Enable bootmenu countdown"
	default y
	help
	  Enable bootmenu auto-booting countdown timer.

config MEDIATEK_BOOTMENU_DELAY
	int "MediaTek bootmenu delay time (s)"
	range 1 10
	default 4
	depends on MEDIATEK_BOOTMENU_COUNTDOWN
	help
	  The dealy seconds for the bootmenu to show before running the
	  default command.

config MTK_LOAD_FROM_SD
	bool "Enable support for loading from SD card"
	default n
	select FS_FAT
	select PARTITIONS
	select DOS_PARTITION
	select EFI_PARTITION
	select HAVE_BLK
	depends on DM_MMC
	help
	  Enable support for loading from SD card for mtkload and mtkupgrade.

config MTK_SD_DEV_IDX
	int "MMC device index for SD card"
	range 0 1
	default 1 if TARGET_MT7622
	default 0
	depends on MTK_LOAD_FROM_SD
	help
	  The MMC device index used for SD card loading.

config MEDIATEK_LOAD_FROM_RAM
	bool "Enable loading from RAM support"
	default n
	help
	  Enable loading from RAM support for mtkload and mtkupgrade. This is
	  useful when using JTAG debugging and ethernet is not available.

config MTK_WEB_FAILSAFE
	bool "Enable Web-based failsafe for firmware upgrading"
	default n
	select WEBUI_FAILSAFE
	help
	  Enable Web-based failsafe for firmware upgrading. A bootmenu item
	  will be added to allow entering failsafe mode.

config MTK_WEB_FAILSAFE_AFTER_BOOT_FAILURE
	bool "Automatically start failsafe after booting failure"
	default n
	depends on MTK_WEB_FAILSAFE
	help
	  This option allow user to upgrade a workable firmware image if no
	  firmware image available for boot.
	  This requires booting system using mtkboardboot command.

config MTK_WGET
	bool "Enable wget command using mtk_tcp"
	default n
	select MTK_TCP
	default y if MTK_WEB_FAILSAFE
	depends on !CMD_WGET
	help
	  Enable mtk_tcp-based wget command for downloading file from HTTP
	  server.

config MTK_UBI_SUPPORT
	bool "Enable support for UBI-based images"
	select MTD_UBI
	select CMD_UBI
	help
	   Enable this option will allow bootmenu for mtd using UBI-based
	   images for upgrading and booting.

config MTK_DUAL_BOOT_EMERG_IMAGE_UPGRADE
	bool "Enable upgrading emergency image (Debug only)"
	depends on MTK_DUAL_BOOT_EMERG_IMAGE
	default n
	help
	  Enable upgrading emergency image for A/B Systems.
	  NOTE: This should be disabled for release build.

config MTK_DEFAULT_FIT_BOOT_CONF
	string "Default boot configuration of FIT image"
	default ""
	help
	  FIT image can store multiple boot configuration to allow one sinle
	  FIT image to be used on multiple boards.
	  If left empty, the default one specified in FIT will be used.

config MTK_ROOTDISK_OVERRIDE
	string "Override default rootdisk configuration for OpenWrt"
	default ""
	help
	  Starting from OpenWrt 24.0x, OpenWrt can support multiple rootdisk
	  configurations in one FDT. U-Boot will detect the boot device to set
	  default rootdisk for OpenWrt image. You can override it here.
	  left this configuration if you do not want to override it.

endif # MEDIATEK_BOOTMENU

menuconfig MTK_SECURE_BOOT
	bool "Enable secure boot framework"
	select FIT_SIGNATURE
	select RSA
	select RSA_VERIFY
	select ENV_IS_NOWHERE if !MTK_DUAL_BOOT
	select CHAIN_OF_TRUST if !MTK_DUAL_BOOT
	select IMAGE_FORCED_VERIFY
	select FDT_NO_BOOTARGS_OVERRIDE
	depends on ARCH_MEDIATEK
	default n
	help
	  Enable this option will allow boards to verify Linux and Filesystem.

if MTK_SECURE_BOOT

config CHAIN_OF_TRUST
	bool
	default n

config MTK_ANTI_ROLLBACK
	bool "Enable MediaTek anti-rollback framework"
	default n
	help
	  Enable this option will allow boards to verify Firmware Anti-rollback
	  Version.

config MTK_FSEK
	bool "Enable MediaTek FS encryption key framework"
	default n
	help
	  Enable this option will allow boards to decrypt rootfs-key and set
	  roofs-key to kernel bootargs, and set a device-unique key in FDT

choice
	prompt "Device type for secure environment variables"
	default MTK_SEC_ENV_MTD
	depends on MTK_FSEK

config MTK_SEC_ENV_MTD
	bool "MTD(NOR/NAND/UBI)"
	depends on MTK_BOOTMENU_MTD_COMMON
	help
	This is intended for raw flashes (e.g. SPI-NOR and SPI-NAND).

config MTK_SEC_ENV_MMC
	bool "SD/eMMC"
	depends on MTK_BOOTMENU_MMC_COMMON
	help
	  This is intended SD and eMMC.
endchoice

endif # MTK_SECURE_BOOT

menuconfig MTK_KERNEL_ENCRYPT
	bool "Enable kernel encryption"
	select TEE
	select OPTEE
	select FIT_CIPHER
	default n
	help
	  Enable this option will allow boards to encrypt kernel and device tree.

if MTK_KERNEL_ENCRYPT

config MTK_FW_ENC_ALGO
	string "MTK kernel encryption algorithm"
	default "tee_aes256"
	help
	  Describe the algorithm in firmware encryption.
	  It will add a new node in cipher node and the format is `key-$(algo)-$(key_name)`.
	  The $(algo) depends on this input.

config MTK_FW_ENC_KEY_NAME
	string "MTK kernel encryption key name"
	default "fw_enc"
	help
	  Describe the key name in firmware encryption.
	  It will add a new node in cipher node and the format is `key-$(algo)-$(key_name)`.
	  The $(key_name) depends on this input.

endif # MTK_KERNEL_ENCRYPT

menuconfig MTK_DUAL_BOOT
	bool "Enable A/B System Updates"
	help
	  A/B system updates ensure a workable booting system remains on the
	  flash device during an firmware upgrading.

if MTK_DUAL_BOOT

config MTK_DUAL_BOOT_ITB_IMAGE
	bool "Use FIT itb image"
	help
	  Use FIT itb image for A/B system updates.
	  The FIT itb image combines both kernel and rootfs along with other
	  dtb blobs into a single FIT image. It has the advantage that the
	  image can be stored in a single partition/volume, comparing that the
	  legacy FIT image needs two partitions/volumes to store kernel and
	  rootfs separately. Besides, all binary blobs, including kernel and
	  rootfs can be verified once, without the need of modifying the FIT
	  tree to insert the hashes of rootfs.

config MTK_DUAL_BOOT_ENABLE_RETRY
	bool "Enable boot retry"
	default n
	depends on ARCH_MEDIATEK
	help
	  Enable boot retry functionality. U-Boot will keep a persist
	  boot-count value which will only be reset by a cold reset.
	  The boot-count will be increased by one before each boot attempt.
	  If the image boots up normally, it must reset the boot-count value
	  to zero.
	  Once U-Boot found that boot-count is larger than max retry count, the
	  current slot will be marked invalid.

config MTK_DUAL_BOOT_MAX_RETRY_COUNT
	int "Max boot retry count"
	range 2 127
	default 3
	depends on MTK_DUAL_BOOT_ENABLE_RETRY
	help
	  The maximum boot retry count

config MTK_DUAL_BOOT_SLOT_1_FIRMWARE_NAME
	string "Name of firmware image slot 1"
	depends on MTK_DUAL_BOOT_ITB_IMAGE
	default "firmware2"
	help
	  The partition/volume name for firmware image slot 1

if !MTK_DUAL_BOOT_ITB_IMAGE
config MTK_DUAL_BOOT_IMAGE_ROOTFS_VERIFY
	bool "Verify rootfs part"
	default y
	help
	  Enable this option will force to verify the rootfs part of the image.
	  The rootfs hash should be embedded into the FIT image.
	  If no such hash found, verification will fail.

config MTK_DUAL_BOOT_SLOT_1_KERNEL_NAME
	string "Name for kernel of image slot 1"
	default "kernel2"
	help
	  The partition/volume name for kernel of image slot 1

config MTK_DUAL_BOOT_SLOT_1_ROOTFS_NAME
	string "Name for rootfs of image slot 1"
	default "rootfs2"
	help
	  The partition/volume name for rootfs of image slot 1
endif # !MTK_DUAL_BOOT_ITB_IMAGE

config MTK_DUAL_BOOT_SHARED_ROOTFS_DATA
	bool "Use shared rootfs_data for both images"
	default n if MTK_DUAL_BOOT_ITB_IMAGE
	default y
	help
	  For UBI based images, this option is valid only if itb image is used,
	  otherwise it's always enabled.

config MTK_DUAL_BOOT_ROOTFS_DATA_NAME
	string "Name for rootfs_data of both image slots (eMMC only)"
	depends on MTK_DUAL_BOOT_SHARED_ROOTFS_DATA
	default "rootfs_data"
	help
	  This option only applies for eMMC based images.
	  It's always rootfs_data for UBI base images.

config MTK_DUAL_BOOT_SLOT_1_ROOTFS_DATA_NAME
	string "Name for rootfs_data of image slot 1 (UBI only)"
	depends on MTK_DUAL_BOOT_ITB_IMAGE
	depends on !MTK_DUAL_BOOT_SHARED_ROOTFS_DATA && CMD_UBI
	default "rootfs_data2"
	help
	  This option only applies for UBI based images.

config MTK_DUAL_BOOT_ROOTFS_DATA_SIZE_LIMIT
	bool "Limit the size of rootfs_data volume (UBI only)"
	default y
	depends on MTK_DUAL_BOOT_ITB_IMAGE && MTK_DUAL_BOOT_SHARED_ROOTFS_DATA && MTD_UBI
	help
	  This option allows user to reserve space for other use in UBI.

config MTK_DUAL_BOOT_ROOTFS_DATA_SIZE
	int "Size of rootfs_data volume (MiB)"
	range 1 128
	default 8
	depends on MTK_DUAL_BOOT_ROOTFS_DATA_SIZE_LIMIT || !MTK_DUAL_BOOT_ITB_IMAGE || !MTK_DUAL_BOOT_SHARED_ROOTFS_DATA

config MTK_DUAL_BOOT_RESERVE_ROOTFS_DATA
	bool "Reserve rootfs_data during firmware upgrading"
	default n
	help
	  Enable this option will make rootfs_data completely untouched
	  during OpenWrt sysupgrade, even if 'keep settings' is deselected.
	  This option is valid only if rootfs_data is shared. For UBI, the
	  shared rootfs_data must also have limited size.

config MTK_DUAL_BOOT_EMERG_IMAGE
	bool "Enable booting emergency image if both slots failed"
	depends on MTK_BOOTMENU_MMC
	default n
	help
	  Define a emergency image which is only intended for emergency recovery
	  when both slots are valid. This image is provided in factory and
	  should never be upgraded.
	  A/B System parameters will not be passed to emergency image.

config MTK_DUAL_BOOT_EMERG_FIRMWARE_NAME
	string "Name of emergency firmware image"
	depends on MTK_DUAL_BOOT_EMERG_IMAGE
	depends on MTK_DUAL_BOOT_ITB_IMAGE
	default "firmware_emerg"
	help
	  The partition name for of emergency firmware image

if !MTK_DUAL_BOOT_ITB_IMAGE
config MTK_DUAL_BOOT_EMERG_IMAGE_KERNEL_NAME
	string "Name for kernel of emergency image"
	depends on MTK_DUAL_BOOT_EMERG_IMAGE
	default "kernel_emerg"
	help
	  The partition name for kernel of emergency image

config MTK_DUAL_BOOT_EMERG_IMAGE_ROOTFS_NAME
	string "Name for rootfs of emergency image"
	depends on MTK_DUAL_BOOT_EMERG_IMAGE
	default "rootfs_emerg"
	help
	  The partition name for rootfs of emergency image
endif # !MTK_DUAL_BOOT_ITB_IMAGE

endif # MTK_DUAL_BOOT

config FIRMWARE_SIZE
	bool "Enable Firmware Size"
	help
	  Enable Firmware Size

if ARCH_MEDIATEK

config MTK_DUAL_FIP
	bool "Enable Dual FIP support"
	select BOARD_LATE_INIT
	depends on MTK_BOOTMENU_MMC
	depends on MTK_SECURE_BOOT
	help
	  Reserve another FIP to make increase the reliability of the system.
	  ATF BL2 will try to load FIP1 then FIP2.
	  u-boot will upgrade new FIP to another FIP slot first, then the
	  current slot.
	  Secure Boot must be enabled as a prerequisite to enable FIP data
	  validation in ATF BL2.

config MTK_BOARDINFO
	bool "Enable mediatek boardinfo support"
	default n
	help
	  Enable this option will allow mediatek boards to show extend
	  information when boot-up.

config MTK_FIP_SUPPORT
	bool "Enable ATF FIP utilities"
	select MTK_BOARDINFO
	default n
	help
	  Enable this option will allow boards to use ATF related
	  firmware image package (FIP) commands and APIs.

config MTK_UPGRADE_BL2_VERIFY
	bool "Verify bl2 image before upgrading"
	select MTK_BOARDINFO
	default n
	help
	  Enable this option will force to verify the BL2 image being
	  upgraded when 'bl2_verify' node is configured in the device tree.
	  Upgrading broken BL2 image and the image that is not suitable for
	  the current flash type will be rejected.

config MTK_UPGRADE_FIP_VERIFY
	bool "Verify fip image before upgrading"
	select MTK_FIP_SUPPORT
	default n
	help
	  Enable this option will force to verify the FIP image being
	  upgraded. Upgrading broken FIP image and the image that is not
	  suitable for the current flash type will be rejected.

endif # ARCH_MEDIATEK

config MTK_UPGRADE_IMAGE_VERIFY
	bool "Verify firmware image before upgrading"
	select FIT
	default y
	help
	  Enable this option will force to verify the image being upgraded.
	  Upgrading UBI raw partition image and UBI hybrid image will be
	  rejected.

config MTK_UPGRADE_IMAGE_LEGACY_VERIFY
	bool "Allow verifying legacy image"
	depends on ARCH_MTMIPS
	select LEGACY_IMAGE_FORMAT
	default n
	help
	  Enable this option will allow legacy image be upgraded. Only kernel
	  portion of the image can be verified.

config MTK_UPGRADE_IMAGE_ROOTFS_VERIFY
	bool "Verify rootfs part"
	depends on MTK_UPGRADE_IMAGE_VERIFY
	default y if MTK_DUAL_BOOT
	default n
	help
	  Enable this option will force to verify the rootfs part of the image.
	  The rootfs hash should be embedded into the FIT image.
	  If no such hash found, verification will fail.

config ENABLE_NAND_NMBM
	bool "Enable NAND bad block mapping management"
	default n
	select MTD_DEVICE
	select NMBM
	select NMBM_MTD
	help
	  Select this if you want to use bad block management to handle
	  bad blocks marked druing factory production, and to handle new
	  bad blocks during use.
	  Enabling this option will create a MTD translation layer on
	  the original MTD of the NAND device.

if ENABLE_NAND_NMBM

config NMBM_LOWER_MTD
	string "Lower MTD device name"
	default "spi-nand0"
	help
	  The name of lower mtd device on which nmbm will be created

config NMBM_MAX_RATIO
	int "Default ratio for reserved block for management (N/16)"
	range 1 8
	default 1
	help
	  This option determines how many blocks at the high adress of NAND
	  can be used for NMBM. For a large size NAND, 1/16 of total blocks
	  are still too large for NMBM. For this situation please set
	  CONFIG_NMBM_MAX_BLOCKS to a proper value to limit the maximum
	  reserved blocks.

config NMBM_MAX_BLOCKS
	int "Maximum blocks allowed for reserved block for management"
	range 32 2147483647
	default 256
	help
	  This option is applied after NMBM_MAX_RATIO to ensure maximum
	  blocks reserved block for NMBM will not exceed the value set by
	  this option.

endif # ENABLE_NAND_NMBM

source "board/mediatek/common/customizations/Kconfig"

endmenu

endif # ARCH_MEDIATEK
