262b414
#!/bin/bash
262b414
34cd2c2
# set -x
34cd2c2
6deb24c
if [[ "$(uname -m)" == arm* || "$(uname -m)" == aarch64 ]]
6deb24c
then
262b414
COMMAND="$1"
262b414
KERNEL_VERSION="$2"
262b414
#BOOT_DIR_ABS="$3"
262b414
#KERNEL_IMAGE="$4"
262b414
5a26b77
[ -f /etc/u-boot.conf ] && source /etc/u-boot.conf || true
5a26b77
[ -z "$FIRMWAREDT" ] || FirmwareDT=$FIRMWAREDT
5a26b77
5a26b77
if [[ $FirmwareDT == "True" ]]
5a26b77
then
5a26b77
	# if we want to use firmware DT we remove symlink to current kernel DT
5a26b77
        if [ -h /boot/dtb ]; then
5a26b77
            rm -f /boot/dtb
5a26b77
        fi
5a26b77
	exit 0
5a26b77
fi
5a26b77
262b414
# Setup a /boot/dtb -> /boot/dtb-$newest_kernel_version symlink so that
262b414
# u-boot can find the correct dtb to load.
262b414
#
262b414
# If invoked to 'add' a new kernel, find the newest based on `sort`ing
262b414
# the kernel versions dtb.  If 'remove', then follow basically the same
262b414
# procedure but exclude the version currently being removed.
262b414
#
262b414
# The theory of operation here is that, while newer kernels may add new
262b414
# dtb nodes and fields, as upstreaming hw support for some particular
262b414
# device progresses, it should never make backward incompatible changes.
262b414
# So it should always be safe to use a newer dtb with an older kernel.
262b414
34cd2c2
    list_dtb_versions() {
34cd2c2
        excluded_version="$1"
34cd2c2
        for dtbdir in /boot/dtb-*; do
34cd2c2
            dtbver=${dtbdir#*-}
34cd2c2
            if [ "$dtbver" != "$excluded_version" ]; then
34cd2c2
                echo $dtbver
34cd2c2
            fi
34cd2c2
        done
34cd2c2
    }
262b414
34cd2c2
    setup_dtb_link() {
34cd2c2
        ver=`list_dtb_versions $1 | sort -r --sort=version | head -1`
34cd2c2
        if [ -h /boot/dtb ]; then
34cd2c2
            rm -f /boot/dtb
34cd2c2
        fi
34cd2c2
        ln -s dtb-$ver /boot/dtb
34cd2c2
    }
262b414
34cd2c2
    ret=0
34cd2c2
    case "$COMMAND" in
34cd2c2
        add)
6deb24c
            # If we're adding a kernel we want that version
6deb24c
            if [ -h /boot/dtb ]; then
6deb24c
                rm -f /boot/dtb
6deb24c
            fi
6deb24c
            ln -s dtb-$KERNEL_VERSION /boot/dtb
34cd2c2
            ret=$?
6deb24c
            ;;
34cd2c2
        remove)
34cd2c2
            setup_dtb_link $KERNEL_VERSION
34cd2c2
            ret=$?
6deb24c
            ;;
34cd2c2
    esac
34cd2c2
    exit $ret
c158ae2
else
c158ae2
    # Just exit on non ARM
c158ae2
    exit 0
34cd2c2
fi