# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2017-present Team LibreELEC (https://libreelec.tv)

if [ -f "$RELEASE_DIR/3rdparty/bootloader/u-boot.img" ]; then
  echo "image: writing u-boot.img to $(basename $DISK)"
  dd if="$RELEASE_DIR/3rdparty/bootloader/u-boot.img" of="$DISK" bs=1K seek=69 conv=fsync,notrunc >"$SAVE_ERROR" 2>&1 || show_error
fi

if [ -f "$RELEASE_DIR/3rdparty/bootloader/SPL" ]; then
  echo "image: writing SPL to $(basename $DISK)"
  dd if="$RELEASE_DIR/3rdparty/bootloader/SPL" of="$DISK" bs=1K seek=1 conv=fsync,notrunc >"$SAVE_ERROR" 2>&1 || show_error
fi

echo "image: copying device trees"
mcopy -s -o "${RELEASE_DIR}/3rdparty/bootloader"/imx6d*${UBOOT_SYSTEM}*.dtb ::
mcopy -s -o "${RELEASE_DIR}/3rdparty/bootloader"/imx6q*${UBOOT_SYSTEM}*.dtb ::

echo "image: copying exlinux.conf"
mkdir -p "${LE_TMP}/extlinux"
cat << EOF > "${LE_TMP}/extlinux/extlinux.conf"
LABEL ${DISTRO}
  LINUX /${KERNEL_NAME}
  FDTDIR /
  APPEND boot=UUID=${UUID_SYSTEM} disk=UUID=${UUID_STORAGE} quiet ${EXTRA_CMDLINE}
EOF
mcopy -s -o "${LE_TMP}/extlinux" ::
