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

[ -z "${SYSTEM_ROOT}" ] && SYSTEM_ROOT=""
[ -z "${BOOT_ROOT}" ] && BOOT_ROOT="/flash"
[ -z "${BOOT_PART}" ] && BOOT_PART=$(df "${BOOT_ROOT}" | tail -1 | awk {' print $1 '})
if [ -z "${BOOT_DISK}" ]; then
  case ${BOOT_PART} in
    /dev/sd[a-z][0-9]*)
      BOOT_DISK=$(echo ${BOOT_PART} | sed -e "s,[0-9]*,,g")
      ;;
    /dev/mmcblk*)
      BOOT_DISK=$(echo ${BOOT_PART} | sed -e "s,p[0-9]*,,g")
      ;;
  esac
fi

# mount ${BOOT_ROOT} r/w
  mount -o remount,rw ${BOOT_ROOT}

# update device tree
  for all_dtb in ${BOOT_ROOT}/*.dtb; do
    [ -f "$all_dtb" ] || continue
    dtb=$(basename ${all_dtb})

    # device tree mappings for update from vendor to mainline kernel
    case "${dtb}" in
      rk3288-miniarm.dtb)
        new_dtb=rk3288-tinker-s.dtb
        ;;
      rk3328-box.dtb|rk3328-box-trn9.dtb|rk3328-box-z28.dtb|rk3328-rockbox.dtb)
        new_dtb=rk3328-a1.dtb
        ;;
      rk3399-rock-pi-4.dtb)
        new_dtb=rk3399-rock-pi-4b.dtb
        ;;
      *)
        new_dtb="${dtb}"
        ;;
    esac

    if [ "${dtb}" != "${new_dtb}" -a -f ${SYSTEM_ROOT}/usr/share/bootloader/${new_dtb} ]; then
      echo -n "Replacing ${dtb} with ${new_dtb}... "
      cp -p ${SYSTEM_ROOT}/usr/share/bootloader/${new_dtb} ${BOOT_ROOT} && \
      sed -e "s/FDT \/${dtb}/FDT \/${new_dtb}/g" \
          -i ${BOOT_ROOT}/extlinux/extlinux.conf && \
      rm -f ${BOOT_ROOT}/${dtb}
      echo "done"
    else
      if [ -f ${SYSTEM_ROOT}/usr/share/bootloader/${dtb} ]; then
        echo -n "Updating ${dtb}... "
        cp -p ${SYSTEM_ROOT}/usr/share/bootloader/${dtb} ${BOOT_ROOT}
        echo "done"
      elif [ "$(grep -c "FDT /${dtb}" ${BOOT_ROOT}/extlinux/extlinux.conf)" -ne 0 ]; then
        non_existent_dtb="${dtb}"
      fi
    fi
  done

# update /rockchip device tree blobs
  if [ -d ${BOOT_ROOT}/rockchip ]; then
    echo -n "Updating Device Tree Blobs... "
    for all_dtb in ${BOOT_ROOT}/rockchip/*.dtb; do
      [ -f "$all_dtb" ] || continue
      dtb=$(basename ${all_dtb})
      if [ -f ${SYSTEM_ROOT}/usr/share/bootloader/${dtb} ]; then
        cp -p ${SYSTEM_ROOT}/usr/share/bootloader/${dtb} ${BOOT_ROOT}/rockchip
      fi
    done
    echo "done"
  fi

# update bootloader
 if [ -f ${SYSTEM_ROOT}/usr/share/bootloader/u-boot-rockchip.bin ]; then
    echo -n "Updating U-Boot... "
    dd if=${SYSTEM_ROOT}/usr/share/bootloader/u-boot-rockchip.bin of=${BOOT_DISK} bs=32k seek=1 conv=fsync &>/dev/null
    echo "done"
  fi

# mount ${BOOT_ROOT} r/o
  sync
  mount -o remount,ro ${BOOT_ROOT}

# warn if device tree was not updated
  if [ -n "${non_existent_dtb}" ]; then
    echo "WARNING: The device-tree file ${non_existent_dtb} was not found in the update"
    echo "package and has not been updated. Your device may not boot or work correctly!"
    sleep 10
  fi
