'V‘HŸ[M›žð  ,Á        ƒÀÎ Boot script                       ,¹    # This is a boot script for U-Boot
# Generate boot.scr:
# mkimage -c none -A arm -T script -d boot.cmd.default boot.scr
#
################
fitimage_name=/boot/image.ub
kernel_name=/boot/Image
ramdisk_name=/boot/petalinux-initramfs-image-eval-brd-sc-zynqmp.cpio.gz.u-boot
rootfs_name=/boot/rootfs.cpio.gz.u-boot


#Set DT selection
if test "${board_name}-${board_rev}" = "VEK280-B01" || test "${board_name}-${board_rev}" = "VEK280-B02" || test "${board_name}-${board_rev}" = "VEK280-B03"; then
        dtb_name=/boot/devicetree/system-sc-revc-zynqmp.dtb
else
        dtb_name=/boot/system.dtb
fi
setenv bootargs 'earlycon console=ttyPS0,115200 clk_ignore_unused root=/dev/ram0 rw init_fatal_sh=1 uio_pdrv_genirq.of_id=generic-uio';
for boot_target in ${boot_targets};
do
	echo "Trying to load boot images from ${boot_target}"
	if test "${boot_target}" = "jtag" ; then
		echo "JTAG boot"
		booti 0x00200000 0x04000000 0x00100000
	fi
	if test "${boot_target}" = "usb0" || test "${boot_target}" = "usb1"; then
		if test -e ${devtype} ${devnum}:${distro_bootpart} /boot/uEnv.txt; then
			echo "Importing environment(uEnv.txt) from ${boot_target}..."
			fatload ${devtype} ${devnum}:${distro_bootpart} 0x00200000 /boot/uEnv.txt;
			env import -t 0x00200000 $filesize
			if test -n $uenvcmd; then
				echo "Running uenvcmd ...";
				run uenvcmd;
			fi
		fi
		if test -e ${devtype} ${devnum}:${distro_bootpart} ${fitimage_name}; then
			echo "loading fitimage ${fitimage_name} to 0x10000000"
			fatload ${devtype} ${devnum}:${distro_bootpart} 0x10000000 ${fitimage_name};
			bootm 0x10000000;
                fi
		if test -e ${devtype} ${devnum}:${distro_bootpart} ${kernel_name}; then
			echo "loading kernel ${kernel_name} to 0x00200000"
			fatload ${devtype} ${devnum}:${distro_bootpart} 0x00200000 ${kernel_name};
		fi
		if test -e ${devtype} ${devnum}:${distro_bootpart} ${dtb_name}; then
			echo "loading device tree ${dtb_name} to 0x00100000"
			fatload ${devtype} ${devnum}:${distro_bootpart} 0x00100000 ${dtb_name};
		fi
		if test -e ${devtype} ${devnum}:${distro_bootpart} ${ramdisk_name} && test "${skip_tinyramdisk}" != "yes"; then
			echo "booting ramdisk ${ramdisk_name} at 0x04000000"
			fatload ${devtype} ${devnum}:${distro_bootpart} 0x04000000 ${ramdisk_name};
			booti 0x00200000 0x04000000 0x00100000
		fi
		if test -e ${devtype} ${devnum}:${distro_bootpart} ${rootfs_name} && test "${skip_ramdisk}" != "yes"; then
			echo "booting rootfs/ramdisk ${rootfs_name} from 0x04000000"
			fatload ${devtype} ${devnum}:${distro_bootpart} 0x04000000 ${rootfs_name};
			booti 0x00200000 0x04000000 0x00100000
		fi
		echo "booting kernel from RAM"
		booti 0x00200000 - 0x00100000
	fi
	if test "${boot_target}" = "mmc0" || test "${boot_target}" = "mmc1"; then
		# Part of any A/B support logic requires setting and saving some variables into a non-volatile
		# U-Boot environment storage (i.e. 'env save'). Unfortunately early versions of this device's
		# U-Boot were configured to store the environment in the same place where the Xilinx Boot Image
		# Recovery Tool is stored. If this script blindly does an 'env save' the user's ability to 
		# load a new image through the BIRT might go away if the user is using a new Linux rootfs image
		# (eMMC) with an older U-Boot (boot.bin in QSPI). Merely checking the U-Boot version will not
		# suffice; both before and after the fix the U-Boot version is the same. Therefore in order to
		# detect a new versus old U-Boot the boot.bin in the QSPI must be examined; if it contains
		# the string 'SC-BootFW' at offset 0x70 then we know this is a new U-Boot and can proceed
		# with the A/B logic, otherwise we have to fall back to non A/B behaviour.
		#
		# Further complicating the situation, there are 2 boot.bins, therefore the code must start by
		# figuring out which of the two is running. This can be determined by examining the values
		# in the "Persistent Registers" section (QSPI, mtd2, 0x0020.0000). Data in the "Persistent
		# Registers" section not only describes which of the two boot.bins is running, but also
		# provides the offsets for finding both images in the QSPI.
		#
		# As a further complication there is a "Persistent Registers Backup" section (QSPI, mtd3,
		# 0x00f8.0000) in case the primary is bad.
		#
		# Also, U-Boot's shell/scripting language is very primitive. It can't compare strings
		# directly, it can only compare blocks of RAM to each other. Also there is no way to place
		# a string into RAM directly (to compare against) so the compare-to string must be placed
		# in RAM one byte at a time.
		#
		# The choice to use ${kernel_addr_r} as a scratch pad is completely arbitrary.

		env set DO_RAUC
		env set CMP2
		env set FOUND_PREG
		env set PREG
		env set LAST_BOOTED_IMAGE
		env set IMAGE_OFFSET
		env set IMAGE_VERSION
		env set saved_kernel_addr_r $kernel_addr_r

		echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
		echo "*     checking to see if env save is safe     *"
		echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
		DO_RAUC=1

		# place the compare-to string 'SC-BootFW' in RAM at $kernel_addr_r + 0x20
		setexpr CMP2 $kernel_addr_r + 0x20
		mw.b $CMP2 0x53
		setexpr CMP2 $CMP2 + 1
		mw.b $CMP2 0x43
		setexpr CMP2 $CMP2 + 1
		mw.b $CMP2 0x2d
		setexpr CMP2 $CMP2 + 1
		mw.b $CMP2 0x42
		setexpr CMP2 $CMP2 + 1
		mw.b $CMP2 0x6f
		setexpr CMP2 $CMP2 + 1
		mw.b $CMP2 0x6f
		setexpr CMP2 $CMP2 + 1
		mw.b $CMP2 0x74
		setexpr CMP2 $CMP2 + 1
		mw.b $CMP2 0x46
		setexpr CMP2 $CMP2 + 1
		mw.b $CMP2 0x57
		# leave CMP2 pointing at the start of the compare-to string
		setexpr CMP2 $kernel_addr_r + 0x20

		# probe QSPI
		sf probe
		if itest $? != 0; then
			echo "QSPI probe NOT successful"
			DO_RAUC=0
		else
			echo "QSPI probe successful"
		fi

		# put 9 bytes of offset 0x70 of the active boot.bin in RAM @ $kernel_addr_r
		# the layout of the Persistent Registers area is as follows:
		# struct PREG {                                     OFFSET
		#	char idStr[4] = "ABUM"                      0x00
		#	uint version                                0x04
		#	uint length                                 0x08
		#	uint checksum                               0x0c
		#	char last_booted_image                      0x10
		#	char requested_boot_image                   0x11
		#	char image_b_bootable   <- not a typo       0x12
		#	char image_a_bootable                       0x13
		#	uint boot_image_a_offset                    0x14
		#	uint boot_image_b_offset                    0x18
		#	uing recovery_image_offset                  0x1c
		# }
		FOUND_PREG=0
		if itest $DO_RAUC == 1; then
			env set PREG
			for PREG in 0x100000 0xf80000; do                                 # check both the PREG and PREG-backup
				if itest $FOUND_PREG == 0; then
					sf read $kernel_addr_r $PREG 0x20
					echo "PREG=$PREG data:"
					md.b $kernel_addr_r 0x20

					setexpr PREG $PREG + 0x10
					sf read $kernel_addr_r $PREG 1
					setexpr.b LAST_BOOTED_IMAGE *${kernel_addr_r}
					if itest $LAST_BOOTED_IMAGE == 0; then            # IMAGE A
						echo "last booted image = A (PREG=$PREG)"
						setexpr PREG $PREG + 4                    # IMAGE A offset
						sf read $kernel_addr_r $PREG 4
						setexpr.l IMAGE_OFFSET *${kernel_addr_r}
						echo "A image offset: $IMAGE_OFFSET"
						setexpr.l IMAGE_VERSION $IMAGE_OFFSET + 0x70
						sf read $kernel_addr_r $IMAGE_VERSION 9
						FOUND_PREG=1
					else
						if itest $LAST_BOOTED_IMAGE == 1; then    # IMAGE B
							echo "last booted image = B (PREG=$PREG)"
							setexpr PREG $PREG + 8            # IMAGE B offset
							sf read $kernel_addr_r $PREG 4
							setexpr.l IMAGE_OFFSET *${kernel_addr_r}
							echo "B image offset: $IMAGE_OFFSET"
							setexpr.l IMAGE_VERSION $IMAGE_OFFSET + 0x70
							sf read $kernel_addr_r $IMAGE_VERSION 9
							FOUND_PREG=1
						else
							echo "PREG=$PREG invalid"
						fi
					fi
				fi
			done
		fi
		if itest $FOUND_PREG == 1; then
			echo "Persistent Register data found/used"
			DO_RAUC=1
		else
			echo "Persistent Register data NOT found/used"
			DO_RAUC=0
		fi

		# at this point the 9 bytes in *${kernel_addr_r} is the version of the active boot.bin
		# and *${kernel_addr_r} + 0x20 contains the compare-to string (CMP2)
		if itest $DO_RAUC == 1; then
			echo "boot.bin version:"
			md.b $kernel_addr_r 9
			echo "compare-to string:"
			md.b $CMP2 9

			cmp.b $kernel_addr_r $CMP2 9
			if itest $? == 0; then
				echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
				echo "*            boot.bin version good            *"
				echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
				DO_RAUC=1
			else
				echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
				echo "*     !!!  older boot.bin detected   !!!      *"
				echo "*     !!! rauc will not be performed !!!      *"
				echo "* falling back to booting the first partition *"
				echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
			fi
		else
			echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
			echo "*             not performing RAUC             *"
			echo "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*"
		fi

		if itest $DO_RAUC == 1; then
			# A/B support
			test -n "${BOOT_ORDER}" || env set BOOT_ORDER "A B"
			test -n "${BOOT_A_LEFT}" || env set BOOT_A_LEFT 3
			test -n "${BOOT_B_LEFT}" || env set BOOT_B_LEFT 3
			test -n "${RAUC_BOOTDEV}" || env set RAUC_BOOTDEV "mmc 0:1"

			# determine rauc slot
			env set RAUC_BOOTPART
			env set RAUC_SLOT

			for RAUC_BOOT_SLOT in "${BOOT_ORDER}"; do
				if test "x${RAUC_BOOTPART}" != "x"; then
					# skip remaining slots
				elif test "x${RAUC_BOOT_SLOT}" = "xA"; then
					if test ${BOOT_A_LEFT} -gt 0; then
						echo "using RAUC slot A"
						setexpr BOOT_A_LEFT ${BOOT_A_LEFT} - 1
						env set RAUC_SLOT "A"
						env set RAUC_BOOTPART "PARTLABEL=rootfsA"
						env set RAUC_BOOTDEV "mmc 0:1"
					fi
				elif test "x${RAUC_BOOT_SLOT}" = "xB"; then
					if test ${BOOT_B_LEFT} -gt 0; then
						echo "using RAUC slot B"
						setexpr BOOT_B_LEFT ${BOOT_B_LEFT} - 1
						env set RAUC_SLOT "B"
						env set RAUC_BOOTPART "PARTLABEL=rootfsB"
						env set RAUC_BOOTDEV "mmc 0:2"
					fi
				fi
			done

			if test -n "${RAUC_BOOTPART}"; then
				env set bootargs "earlycon console=ttyPS0,115200 clk_ignore_unused rw init_fatal_sh=1 uio_pdrv_genirq.of_id=generic-uio rootwait root=${RAUC_BOOTPART} rauc.slot=${RAUC_SLOT}"
				env save
			else
				echo "no valid RAUC slot found, resetting tries to 3"
				env set BOOT_A_LEFT 3
				env set BOOT_B_LEFT 3
				env save
				reset
			fi
		else
			env set RAUC_BOOTDEV "mmc 0:1"
			env set bootargs "earlycon console=ttyPS0,115200 clk_ignore_unused rw init_fatal_sh=1 uio_pdrv_genirq.of_id=generic-uio rootwait root=/dev/mmcblk0p1"
		fi

		# reset kernel_addr_r
		env set kernel_addr_r $saved_kernel_addr_r

		if test -e ${RAUC_BOOTDEV} ${fitimage_name}; then
			echo "booting fitimage ${fitimage_name} from ${RAUC_BOOTDEV} at 0x10000000"
			ext4load  ${RAUC_BOOTDEV} 0x10000000 ${fitimage_name};
			bootm 0x10000000;
		fi
		if test -e ${RAUC_BOOTDEV} ${kernel_name}; then
			echo "loading kernel ${kernel_name} from ${RAUC_BOOTDEV} to 0x00200000"
			ext4load ${RAUC_BOOTDEV} 0x00200000 ${kernel_name};
		fi
		if test -e ${RAUC_BOOTDEV} ${dtb_name}; then
			echo "loading devicetree ${dtb_name} from ${RAUC_BOOTDEV} to 0x00100000"
			ext4load ${RAUC_BOOTDEV} 0x00100000 ${dtb_name};
		fi
		echo "booting booti"
		booti 0x00200000 - 0x00100000
	fi
done
