####################################################
# 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...: 20-Dec-2021
####################################################
# 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.

HWM=$( strings /proc/device-tree/model 2>/dev/null )
# 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

# 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

# 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 usr/share/hwm-bw-raspberrypi/* ${HWMBW_MNT}/

# 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 ;;
   # 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:
   "Raspberry Pi 3"*)
      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
