################################################################################
# Package: a/hwm-bw-raspberrypi
# Script : install/doinst.sh
# Purpose: Position the initial Boot Loader firmware
#          for the Raspberry Pi into its partition.
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 15-Mar-2024
################################################################################
# Change log
# 20-Dec-2021: First version.
# 15-Mar-2024: If the directory '/boot/platform/hwm_bw' is not found at package
# install time, the RPi EEPROM management tool's configuration file will be
# adjusted to use the boot file system location at '/boot'.  This is to provide
# compatibility with SARPi.
################################################################################
# Copyright 2021, 2022,2024  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.

config() {
  NEW="$1"
  OLD="`dirname $NEW`/`basename $NEW .new`"
  # If there's no config file by that name, mv it over:
  if [ ! -r $OLD ]; then
    mv $NEW $OLD
  elif cmp -s $NEW $OLD; then # toss the redundant copy
    rm $NEW
  fi
  # Otherwise, we leave the .new copy for the admin to consider...
}

# If we're installing this package from within the Slackware Installer
# environment, we have a different mount point.
# The installer symlinks /boot to /mnt/boot, so we don't need this at the moment.
#if [ -f /.installer-version ]; then
#   # The Hardware Model's initial Boot Loader is
#   # mounted within the Slackware Installer:
#   HWMBW_MNT=/mnt/boot/platform/hwm_bw
# else
#   # Mounted within the Slackware OS:
#   HWMBW_MNT=/boot/platform/hwm_bw
#fi
#echo "Mount point: $HWMBW_MNT"
HWMBW_MNT=/boot/platform/hwm_bw

# Location of the RPi boot loader firmware/assets:
HWMBL_DIR=usr/share/hwm-bw-raspberrypi/bootloader-firmware

# In the absence of detection for the Slackware AArch64 standard RPi
# Native Boot Loader location, our assumption will be that we are
# operating within a SARPi installation. Consequently, we will adapt
# the configuration to use '/boot':
RPIFWCONF=etc/rpi-eeprom-update
# This will change it for non-RPi devices also, but that's acceptable
# since this is an RPi-only tool.  For a proper Slackware install on the
# RPi, this file system will be mounted at boot.
mountpoint -q ${HWMBW_MNT} || sed -i 's?'"$HWMBW_MNT"'?/boot?g' ${RPIFWCONF}.new

# Install the RPi EEPROM firmware tool's config:
config ${RPIFWCONF}.new

# Determine the Hardware Model name:
export HWM=$( slk-hwm-discover )

# Exit silently if we don't detect the Hardware Model.
# This avoids warnings/errors.
# We may need to surface these at some point but for the moment it's not
# a concern.
[ -z "${HWM}" ] && exit 0

case "${HWM}" in
   "Raspberry Pi"*)
      HWM_DETECTED=Yes ;;
esac

# Exit silently if we found the Hardware Model type, but it's not
# the RPi.  This avoids unsightly messages for Hardware Models that
# don't need this Boot Loader firmware, but have the package installed.
[ -z "${HWM_DETECTED}" ] && exit 0

# Check it's mounted.
# At this point we do need to surface errors as this ought to be working
# for the Hardware Model.
mountpoint -q ${HWMBW_MNT} || \
  { echo "Error: ${HWMBW_MNT} is not mounted. Cannot install firmware."
    echo "       Your system may not boot."
    exit 1 ;}

# Copy/update the firmware to the mounted Hardware Model bootware partition:
# Yes this does mean that we'll most likely end up with old assets on the
# Hardware Model bootware partition, but I can't see this being an issue.
# If cruft build up becomes a problem I'll look into solutions.
cp -fa ${HWMBL_DIR}/* ${HWMBW_MNT}/ || exit 1

# Select the version of the U-Boot Boot Loader for the particular Raspberry Pi
# Hardware Models:
case "${HWM}" in
   "Raspberry Pi 4"*)
      UBOOTBINVER=bcm2711_rpi4 ;;

   "Raspberry Pi 3"*)
      # Sync the RPi3 DTBs.
      # But these DTBs are only shipped by this 'a/hwm-bw-raspberrypi' package as
      # they don't exist in mainline Kernel.
      # This installation is duplicated by the a/kernel package because it's
      # installed subsequent to the 'hwm-bw-raspberrypi' package, yet it's the Kernel
      # that sets up the /boot/dtb symlink.
      # Syncing the DTBs here enables the most recent DTBs to be available at boot time,
      # without relying on a Kernel package upgrade to deploy them.
      [ -d /boot/dtb/broadcom ] && install -pm755 ${HWMBL_DIR}/bcm2710-*.dtb /boot/dtb/broadcom/

      # Presently the RPi3 can use the same U-Boot binary as for the RPi4:
      # In fact we don't build a U-Boot binary for the bcm2837 SoC.
      # If we need to diverge at some point in the future, or for another
      # Hardware Model, this is how we can achieve it:
      UBOOTBINVER=bcm2711_rpi4 ;;
esac

if [ ! -z "${UBOOTBINVER}" ]; then
   # Copy the appropriate Hardware Model-specific U-Boot 2nd stage Boot Loader:
   #cp -dpRfL /usr/share/hwm-bw-raspberrypi/${UBOOTBINVER}-u-boot.bin ${HWM_MNTPT}/u-boot.bin
   # Move into place:
   # This binary is copied by the 'cp' above:
   ( cd ${HWMBW_MNT} && mv -f ${UBOOTBINVER}-u-boot.bin slk_u-boot.bin )
fi
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711 ; rm -rf beta )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711 ; ln -sf latest/ beta )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711 ; rm -rf critical )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711 ; ln -sf default/ critical )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/critical ; rm -rf pieeprom-2019-07-15.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/critical ; ln -sf ../beta/pieeprom-2019-07-15.bin pieeprom-2019-07-15.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/critical ; rm -rf pieeprom-2019-09-10.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/critical ; ln -sf ../beta/pieeprom-2019-09-10.bin pieeprom-2019-09-10.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/critical ; rm -rf recovery.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/critical ; ln -sf ../beta/recovery.bin recovery.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/critical ; rm -rf vl805-00013701.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/critical ; ln -sf ../beta/vl805-00013701.bin vl805-00013701.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/critical ; rm -rf vl805-000137ab.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/critical ; ln -sf ../beta/vl805-000137ab.bin vl805-000137ab.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/stable ; rm -rf pieeprom-2020-01-17.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/stable ; ln -sf ../beta/pieeprom-2020-01-17.bin pieeprom-2020-01-17.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/stable ; rm -rf pieeprom-2020-03-19.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/stable ; ln -sf ../beta/pieeprom-2020-03-19.bin pieeprom-2020-03-19.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/stable ; rm -rf recovery.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711/old/stable ; ln -sf ../beta/recovery.bin recovery.bin )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711 ; rm -rf stable )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2711 ; ln -sf latest/ stable )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2712 ; rm -rf beta )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2712 ; ln -sf latest beta )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2712 ; rm -rf critical )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2712 ; ln -sf default critical )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2712 ; rm -rf stable )
( cd usr/share/hwm-bw-raspberrypi/eeprom-firmware/bootloader/firmware-2712 ; ln -sf latest stable )
