#!/bin/bash -e

# usage message
usage() {
	echo "
Change boot mode for Raspberry Pi 4
Use u-boot and extlinux.conf.
Do not load dtb file by u-boot.
Load dtb file by firmware and transfer
the modified dtb in memory to the kernel through u-boot.

Usage:
$(basename ${0}) [<kernel flavour>]
 Configure boot kernel <kernel flavour>
$(basename ${0}) --help
 Print this message
"
}

if [ "$1" == "--help" ]
then
	usage
	exit 0
fi

. /usr/sbin/rpi4-boot-common

if [ "$systemarch" != "armh" ]; then
	echo "Error: This script changes the boot mode to armv7l (armh) only."
	echo "To change boot mode to aarch64 use rpi4-boot-uboot"
	exit 1
fi

kernel_info_get $1
dtbdirname_get

set_boot_uboot
cp_dtb_ovl

# Back up the file extlinux.conf before changing
cp -f "$extlinuxconf" "$extlinuxconf.bkp"

# Test labels in extlinux.conf and find last label with required flavour
change_default_label $1

# Add # to all fdtdir strings for do not loading dtb file by u-boot
sed -i 's/^\s\+fdtdir/#\tfdtdir/' "$extlinuxconf"
