#!/bin/bash

###############################################################
# Script : flash-spl
# Purpose: Update the U-Boot SPL on supported Hardware Models
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 20-Apr-2022
# Version: 1.00
################################################################
# Notes:
# This code is copied from the Slackware Installer
# /usr/lib/setup/armedslack-bootloader-flash
################################################################
# Usage example:
# mkdir -p /tmp/slack ; cd /tmp/slack
# rsync -PavL --exclude '*/src/*' --exclude '*/bcm*/*' --exclude '*/installer/*' ftp.arm.slackware.com::slackwarearm/platform/aarch64/bootware .
# su -
# cd /tmp/slack/bootware/tools
# ./flash-spl
#################################################################

# Copyright 2022  Stuart Winter, Donostia, Spain.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

# Default settings:
#PLATFORMDIR=${T_PX}/boot/platform/aarch64/
PLATFORMDIR=$PWD/../bin/

UBOOTSPIBIN=spi-idbloader.img # this is a suffix
platform_detected=0

[ ! -d $PLATFORMDIR ] && { echo "Error: cannot find $PLATFORMDIR" ; exit 1 ;}

function write_flash() {
   local flash_dev=${1}
   local flash_bin=${2}

   # flashcp takes care of erase & write directly:
   printf "\nFlash device: ${flash_dev}\n"
   printf "Image file..: $( basename ${flash_bin} )\n\n"
   printf "This will take approximately two (2) minutes\n\n"
   printf "Please wait...\n\n"
   flashcp -vA ${flash_bin} ${flash_dev}
   estat=$?
   if [ $estat -gt 0 ]; then
      read -p "An error occurred.  Press ENTER to continue"
      return $estat
    else
      return 0
   fi
}

if [ -f /proc/device-tree/model ]; then
   export HWM=$( strings /proc/device-tree/model 2>/dev/null )
 else
   echo "${0}: ERROR: unable to detect system type from /proc/device-tree/model"
   echo "If your system does not require a boot loader to be installed"
   echo "to your hardware's onboard flash storage, you can ignore this message."
   echo "Exiting"
   sleep 4
   exit 1
fi

# Set the variables on a per-device basis.
case "$HWM" in
   ###########################################
   # RK3399-based Hardware Models:
   ###########################################
   #
   # Hardware Model-specific settings:
   "Pine64 Pinebook Pro"*) HWMODEL=pinebookpro ;&
   "Pine64 RockPro64"*|"Pine64 Pinebook Pro"*)
      HWMODEL=${HWMODEL:-"rockpro64"}
      platform_detected=1
      SOC=rk3399
      BOARDFLASHDEV=/dev/mtd0
      # On the RockPro64:
      #root@bladswede:~# cat /proc/mtd
      #dev:    size   erasesize  name
      #mtd0: 01000000 00001000 "spi0.0"
      BOARDFLASHNAME=spi0.0
      UBOOTSPIBIN=${SOC}_${HWMODEL}-${UBOOTSPIBIN} ;;
esac

# Sanity check:
if [ $platform_detected -ne 1 ]; then
   echo "Hardware Model not detected"
   echo "No boot loader flashing required."
   exit 0
fi

# Ensure that we can find the boot loader:
if [ ! -s ${PLATFORMDIR}/${SOC}/u-boot*/${UBOOTSPIBIN} ]; then
   echo "$0 : ERROR - unable to find boot loader image at"
   echo "${PLATFORMDIR}/${soc}/${UBOOTSPIBIN}"
   exit 4
fi

# Ensure that the flash interface exists:
if [ ! -c ${BOARDFLASHDEV} ]; then
   echo "$0 : ERROR - flash device interface ${BOARDFLASHDEV}"
   echo "     does not exist or is not of the type expected."
   exit 1
fi

echo "Hardware Model: $HWMODEL"
echo "Boot loader...: ${PLATFORMDIR}/${SOC}/u-boot*/${UBOOTSPIBIN}"

read -p "Press ENTER to begin flashing"
write_flash $BOARDFLASHDEV ${PLATFORMDIR}/${SOC}/u-boot*/${UBOOTSPIBIN}
