#include <configs/platform-auto.h>
#define CONFIG_SYS_BOOTM_LEN 0xF000000

#define DFU_ALT_INFO_RAM \
                "dfu_ram_info=" \
        "setenv dfu_alt_info " \
        "image.ub ram $netstart 0x1e00000\0" \
        "dfu_ram=run dfu_ram_info && dfu 0 ram 0\0" \
        "thor_ram=run dfu_ram_info && thordown 0 ram 0\0"

#define DFU_ALT_INFO_MMC \
        "dfu_mmc_info=" \
        "set dfu_alt_info " \
        "${kernel_image} fat 0 1\\\\;" \
        "dfu_mmc=run dfu_mmc_info && dfu 0 mmc 0\0" \
        "thor_mmc=run dfu_mmc_info && thordown 0 mmc 0\0"

/*Required for uartless designs */
#ifndef CONFIG_BAUDRATE
#define CONFIG_BAUDRATE 115200
#ifdef CONFIG_DEBUG_UART
#undef CONFIG_DEBUG_UART
#endif
#endif

/* Update u-boot env.
 * AW: CONFIG_EXTRA_ENV_SETTINGS copied from platform-auto.h. Ugly, but it is the recommended way:
 * https://forums.xilinx.com/t5/Embedded-Linux/Adding-custom-environments-variables-and-kernel-offsets-in/td-p/1026242
 * https://www.xilinx.com/support/answers/69780.html
 */
/* Extra U-Boot Env settings */
#define CONFIG_EXTRA_ENV_SETTINGS \
	SERIAL_MULTI \
	CONSOLE_ARG \
	PSSERIAL0 \
	"nc=setenv stdout nc;setenv stdin nc;\0" \
	"bootenv=uEnv.txt\0" \
	"importbootenv=echo \"Importing environment from SD ...\"; " \
		"env import -t ${loadbootenv_addr} $filesize\0" \
	"loadbootenv=load mmc $sdbootdev:$partid ${loadbootenv_addr} ${bootenv}\0" \
	"sd_uEnvtxt_existence_test=test -e mmc $sdbootdev:$partid /uEnv.txt\0" \
	"uenvboot=" \
		"if run sd_uEnvtxt_existence_test; then " \
			"run loadbootenv; " \
			"echo Loaded environment from ${bootenv}; " \
			"run importbootenv; " \
			"fi; " \
		"if test -n $uenvcmd; then " \
			"echo Running uenvcmd ...; " \
			"run uenvcmd; " \
		"fi\0" \
	"autoload=no\0" \
	"sdbootdev=0\0" \
	"clobstart=0x10000000\0" \
	"netstart=0x10000000\0" \
	"dtbnetstart=0x23fff000\0" \
	"loadaddr=0x10000000\0" \
	"boot_img=BOOT.BIN\0" \
	"eraseenv=sf probe 0 && sf erase ${qspi_bootenvstart} ${qspi_bootenvsize}\0" \
	"jffs2_img=rootfs.jffs2\0" \
	"load_jffs2=tftpboot ${clobstart} ${jffs2_img}\0" \
	"update_jffs2=setenv img jffs2; setenv psize ${jffs2size}; setenv installcmd \"install_jffs2\"; run load_jffs2 test_img; setenv img; setenv psize; setenv installcmd\0" \
	"sd_update_jffs2=echo Updating jffs2 from SD; mmcinfo && fatload mmc ${sdbootdev}:1 ${clobstart} ${jffs2_img} && run install_jffs2\0" \
	"install_jffs2=sf probe 0 && sf erase ${jffs2start} ${jffs2size} && " \
		"sf write ${clobstart} ${jffs2start} ${filesize}\0" \
	"kernel_img=image.ub\0" \
	"dtb_img=system.dtb\0" \
	"load_dtb=tftpboot ${clobstart} ${dtb_img}\0" \
	"update_dtb=setenv img dtb; setenv psize ${dtbsize}; setenv installcmd \"install_dtb\"; run load_dtb test_img; setenv img; setenv psize; setenv installcmd\0" \
	"sd_update_dtb=echo Updating dtb from SD; mmcinfo && fatload mmc ${sdbootdev}:1 ${clobstart} ${dtb_img} && run install_dtb\0" \
	"loadbootenv_addr=0x00100000\0" \
	"fault=echo Image size is greater than allocated place - partition is NOT UPDATED\0" \
	"test_crc=if imi ${clobstart}; then run test_img; else echo ${img} Bad CRC - ${img} is NOT UPDATED; fi\0" \
	"test_img=setenv var \"if test ${filesize} -gt ${psize}\\; then run fault\\; else true\\; fi\"&& run var&& setenv var\0" \
	/* Check if ip address is assigned */ \
	"test_ip=if test -n ${ipaddr}; then true; else echo \"IP address not assigned, skip network loading\" && false; fi\0" \
	"netboot=run load_kernel_from_net && bootm\0" \
	"qspi_bootstart=0x0\0" \
	"qspi_bootsize=0x02000000\0" \
	"qspi_bootenvstart=0x02000000\0" \
	"qspi_bootenvsize=0x00040000\0" \
	"qspi_kernelstart=0x02040000\0" \
	"qspi_kernelsize=0x04000000\0" \
	"load_boot_from_net=tftpboot ${clobstart} ${boot_img}\0" \
	"load_boot_from_sd=  mmc dev 1 && mmcinfo && fatload mmc 1 ${clobstart} ${boot_img}\0" \
	"load_boot_from_emmc=mmc dev 0 && mmcinfo && fatload mmc 0 ${clobstart} ${boot_img}\0" \
	"load_kernel_from_dhcp=tftpboot ${clobstart} ${bootfile}\0" \
	"load_kernel_from_net=tftpboot ${clobstart} ${kernel_img}\0" \
	"load_kernel_from_sd=  mmc dev 1 && mmcinfo && fatload mmc 1 ${clobstart} ${kernel_img}\0" \
	"load_kernel_from_emmc=mmc dev 0 && mmcinfo && fatload mmc 0 ${clobstart} ${kernel_img}\0" \
	"load_kernel_from_qspi=sf probe 0 && sf read ${clobstart} ${qspi_kernelstart} ${qspi_kernelsize}\0" \
	"install_boot_to_qspi=sf probe 0 && sf update ${clobstart} ${qspi_bootstart} ${filesize}\0" \
	"install_boot_to_sd=  mmc dev 1 && mmcinfo && fatwrite mmc 1 ${clobstart} ${boot_img} ${filesize}\0" \
	"install_boot_to_emmc=mmc dev 0 && mmcinfo && fatwrite mmc 0 ${clobstart} ${boot_img} ${filesize}\0" \
	"install_kernel_to_qspi=sf probe 0 && sf update ${clobstart} ${qspi_kernelstart} ${filesize}\0" \
	"install_kernel_to_sd=  mmc dev 1 && mmcinfo && fatwrite mmc 1 ${clobstart} ${kernel_img} ${filesize}\0" \
	"install_kernel_to_emmc=mmc dev 0 && mmcinfo && fatwrite mmc 0 ${clobstart} ${kernel_img} ${filesize}\0" \
	"update_boot_qspi_from_net=echo Updating ${boot_img} in QSPI from network; setenv psize ${qspi_bootsize};   run load_boot_from_net test_img install_boot_to_qspi\0" \
	"update_boot_emmc_from_net=echo Updating ${boot_img} in eMMC from network;    run load_boot_from_net install_boot_to_emmc\0" \
	"update_boot_sd_from_net=  echo Updating ${boot_img} in microSD from network; run load_boot_from_net install_boot_to_sd  \0" \
	"update_boot_qspi_from_sd= echo Updating ${boot_img} in QSPI from microSD; setenv psize ${qspi_bootsize};   run load_boot_from_sd  test_img install_boot_to_qspi\0" \
	"update_boot_emmc_from_sd= echo Updating ${boot_img} in eMMC from microSD;    run load_boot_from_sd  install_boot_to_emmc\0" \
	"update_kernel_qspi_from_net=echo Updating ${kernel_img} in QSPI from network; setenv psize ${qspi_kernelsize}; run load_kernel_from_net test_img install_kernel_to_qspi\0" \
	"update_kernel_emmc_from_net=echo Updating ${kernel_img} in eMMC from network;    run load_kernel_from_net install_kernel_to_emmc\0" \
	"update_kernel_sd_from_net=  echo Updating ${kernel_img} in microSD from network; run load_kernel_from_net install_kernel_to_sd  \0" \
	"update_kernel_qspi_from_sd= echo Updating ${kernel_img} in QSPI from microSD; setenv psize ${qspi_kernelsize}; run load_kernel_from_sd test_img install_kernel_to_qspi\0" \
	"update_kernel_emmc_from_sd= echo Updating ${kernel_img} in eMMC from microSD;    run load_kernel_from_sd  install_kernel_to_emmc\0" \
	/* Try to fetch ${bootfile} image. First try to run it as a script, if fails then as regular image. */ \
	"boot_dhcp_script_and_boot=echo Try to fetch ${bootfile} pointed by DHCP from network...; run test_ip && run load_kernel_from_dhcp && echo Try as script...; && setenv uimage_source \"dhcp script\";&& source ${clobstart} || echo Try as image...; && setenv uimage_source \"dhcp image\"; && bootm ${clobstart}\0" \
	/* Try to fetch ${kernel_img} image. First try to run it as a script, if fails then as regular image. */ \
	"boot_net_script_and_boot=echo Try to fetch ${kernel_img} from network...; run test_ip && run load_kernel_from_net && echo Try as script...; && setenv uimage_source \"network script\"; && source ${clobstart} || echo Try as image...; && setenv uimage_source \"network image\"; && bootm ${clobstart}\0" \
	"boot_net_script=echo Try to boot from network script...; run load_kernel_from_net && source ${clobstart}\0" \
	"boot_net=  echo Try to boot from network...; run load_kernel_from_net  && bootm ${clobstart}\0" \
	"boot_sd=   echo Try to boot from microSD...; run load_kernel_from_sd   && setenv uimage_source \"microSD\"; && bootm ${clobstart}\0" \
	"boot_emmc= echo Try to boot from eMMC...;    run load_kernel_from_emmc && setenv uimage_source \"emmc\"; &&bootm ${clobstart}\0" \
	"boot_qspi= echo Try to boot from QSPI...;    run load_kernel_from_qspi && setenv uimage_source \"qspi\"; &&bootm ${clobstart}\0" \
	"default_bootcmd=echo Executing default boot script; run uenvboot; run boot_dhcp_script_and_boot || run boot_net_script_and_boot || run boot_sd || run boot_emmc || run boot_qspi || echo Giving up. No image found! Reset...; || sleep 3 && reset\0" \
""

/* let u-boot to fill serverip and bootfile variables after dhcp request */
#undef CONFIG_BOOTP_SERVERIP
