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_UBI
	bool "UBI (Whole SPI-NAND)"
	select MTK_BOOTMENU_MTD_COMMON
	select MTK_UBI_SUPPORT
	depends on !ENABLE_NAND_NMBM
	help
	  This is intended for raw NAND flashes. The entire NAND flash will
	  have only two partitions: BL2 and UBI.

	  The BL2 partition will have multiple copies of bl2 to ensure maximum
	  reliability. Its size can be set up to 2MB.

	  The rest space of the flash will be fully used for UBI. All other
	  data, such as firmware image, environment, rf data will all be stored
	  as volumes in UBI.

	  To use this bootmenu, please define mtdparts to contain only two
	  partitions named 'bl2' and '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
	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.

config MTK_BOOTMENU_SNOR_EMMC
	bool "SPI-NOR + eMMC"
	select MTK_BOOTMENU_MTD_COMMON
	select MTK_BOOTMENU_MMC_COMMON
	depends on MTK_MISC_CUSTOMIZED
	help
	  This is intended for boot from SPI-NOR and all other parts are stored
	  in eMMC. FIP can be optionally stored in either SPI-NOR or eMMC.

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_UBI_RESERVED_VOLUMES
	string "Reserved volumes in UBI"
	depends on MTK_UBI_SUPPORT
	default "factory=2m" if MTK_BOOTMENU_UBI
	help
	  This option records all extra volumes which should be reserved in
	  UBI. During firmware image upgrading, these volumes will be checked
	  for existance, and will be created if not existed.
	  The format of this option is: <name>=<size>;...

config MTK_UBI_FIP_MIN_SIZE
	hex "Minimum size reserved for FIP volume"
	depends on MTK_BOOTMENU_UBI
	default 0x200000
	help
	  The minimum size reserved for FIP volume. This is used to prevent
	  fip upgrading failure due to no enough space caused by rootfs_data
	  used out all free space.

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

config MEDIATEK_MULTI_MTD_LAYOUT
	bool "Enable multi mtd layout support"
	select SYS_MTDPARTS_RUNTIME
	select CMD_SHOW_MTD_LAYOUT
	depends on !MTK_BOOTMENU_MMC
	depends on !MTK_DUAL_BOOT
	default n

menuconfig MTK_SECURE_BOOT
	bool "Enable secure boot framework"
	select FIT_SIGNATURE
	select FIT_RSASSA_PSS
	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.

endif # MTK_SECURE_BOOT

menuconfig MTK_FW_ENCRYPT
	bool "Enable firmware encryption"
	select FIT_CIPHER
	default n
	help
	  Enable this option will allow boards to encrypt kernel and device tree and initrd.

if MTK_FW_ENCRYPT

config MTK_FW_ENCRYPT_VIA_BL31
	bool "Enable firmware encryption via bl31"
	select FIT_CIPHER
	default n
	help
	  Enable this option will decrypt in bl31


config MTK_FW_ENCRYPT_VIA_OPTEE
	bool "Enable firmware encryption via optee"
	select FIT_CIPHER
	select TEE
	select OPTEE
	default y
	help
	  Enable this option will decrypt in OPTEE

endif # MTK_FW_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

if ARCH_MEDIATEK

config MTK_BSPCONF_SUPPORT
	bool
	help
	  This feature is used to store configurations used by MediaTek BSP.
	  (e.g. Dual FIP)

menuconfig MTK_DUAL_FIP
	bool "Enable Dual FIP support"
	select BOARD_LATE_INIT
	select MTK_BSPCONF_SUPPORT
	depends on MTK_BOOTMENU_UBI || MTK_BOOTMENU_MMC
	help
	  This is similar to the "A/B System Updates", with the target
	  changed to FIP.
	  The board must reserve another volume/partition for backup FIP.

	  ATF BL2 will try to load current FIP slot then another one if
	  current one is unavailable. ATF will pass the loading result to
	  u-boot, and u-boot will then write the result to persistent storage,
	  managed by "boot configuration".

	  Upon ATF upgrading, u-boot will write new FIP to the slot other than
	  current slot, and update the boot configuration by switching current
	  slot to the new one.

	  The FIP must be created with modified fiptool that supports inserting
	  MediaTek's specific ToC entry for data integration (CRC32 and SHA256).

if MTK_DUAL_FIP

choice
	prompt "FIP location"
	depends on MTK_BOOTMENU_MMC
	default FIP_IN_UDA

config FIP_IN_UDA
	bool "UDA"
	help
	  FIP is stored in UDA (normal user data) described by GPT.
	  The partition name must be "fip"

config FIP_IN_BOOT0
	bool "BOOT0"
	help
	  FIP is stored in BOOT0, offset 1MiB. Requires BOOT0 is at lease 4MiB

endchoice

choice
	prompt "FIP2 location"
	depends on MTK_BOOTMENU_MMC
	default FIP2_IN_UDA

config FIP2_IN_UDA
	bool "UDA"
	help
	  FIP2 is stored in UDA (normal user data) described by GPT.
	  The partition name must be "fip2"

config FIP2_IN_BOOT1
	bool "BOOT1"
	help
	  FIP2 is stored in BOOT1, offset 1MiB. Requires BOOT0 is at lease 4MiB

endchoice

endif # MTK_DUAL_FIP

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
	select SHA256
	select MD5
	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
