################################################################################
# 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...: 28-Apr-2026
################################################################################
# Change log
# 20-Dec-2021: First version.
# 19-Oct-2025: No longer deploy U-Boot, since we moved to the RPi native BL.
# 28-Apr-2026: Updated to handle the RPi fat bootware file system living on
#              storage other than the SD card.
################################################################################
# Copyright 2021-2027  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

# RPi firmware config file:
RPIFWCONF=etc/rpi-eeprom-update

# Install the RPi EEPROM firmware tool's config:
# It doesn't matter if the Hardware Model installing this package isn't
# a Raspberry Pi, as the tool won't be used on those systems.
# We just install the config file.
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 5"*)
      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

# Don't actually copy U-Boot into place since we migrated to the native BL.
# U-Boot code remains in place should we ever want to return.
#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
