###############################################################
# Helper script: /boot/platform/aarch64/helper/pkg-kernel-rpi
# Purpose......: Copy Linux Kernel assets on to the RPi's
#                Hardware Model Boot Ware partition.
#                This enables seamless Kernel package upgrades
#                for users who opt to use the RPi's native
#                Boot Loader.
# Called from..: Slackware Kernel package's post installation
#                script: /install/doinst.sh
# Author.......: Stuart Winter <mozes@slackware.com>
# Date.........: 21-Dec-2021
###############################################################

HWM_MNTPT=/boot/platform/hwm_bw
# If the Hardware Model Boot Ware partition isn't mounted,
# there's nothing to do on this Hardware Model.
mountpoint -q ${HWM_MNTPT} || exit 0

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

# Copy the assets only upon Raspberry Pi Hardware Models.
# This may be expanded in future to handle other RPi's, assuming
# the same non-standard boot process.
case "${HWM}" in
   "Raspberry Pi"*)
      # Notes:
      #       [1] The use of absolute paths works here because the
      #           Slackware Installer has /boot symlinked to within the OS
      #           mount point.  The path is also correct within a chroot.
      #       [2] The U-Boot Boot Loader, Linux Kernel and OS InitRD file
      #           names are prefixed with 'slk_'.  This is simply to
      #           easily distinguish them from other assets.
      #    *   Note: The U-boot loader is no longer deployed because we switched
      #    *         the RPi to use the native BL.
      #
      #       [3] The U-Boot loader is installed into /boot/platform/hwm_bw
      #           by the package a/hwm-bw-raspberrypi
      # Copy the Linux Kernel image:
      cp -fL /boot/Image-armv8 ${HWM_MNTPT}/slk_image-armv8 || exit 1
      # Slackware OS InitRD (Operating System Initial RAM Disk):
      cp -fL /boot/initrd-armv8 ${HWM_MNTPT}/slk_initrd-armv8 || exit 1
      # Copy the Device Tree Blobs (DTBs):
      cp -dpRfL /boot/dtb/broadcom/* ${HWM_MNTPT}/ || exit 1

      # Location of the RPi boot loader firmware/assets:
      # This belongs to the a/hwm-bw-raspberry pi package and is used below
      # to keep DTBs that aren't supplied in the mainline Kernel, in sync
      # in both /boot and /boot/platform/hwm_bw.
      HWMBL_DIR=/usr/share/hwm-bw-raspberrypi/bootloader-firmware
      ;;
esac

# Hardware Model specific settings:
case "${HWM}" in
   "Raspberry Pi 3"*)
      # Since Linux 5.18, the RPi3 needs the DTBs (which aren't shipped by the mainline Kernel)
      # in /boot/dtb/broadcom in addition to /boot/platform/hwm_bw despite U-Boot
      # not being configured to use them.
      # We're just going to go with the flow here and keep everything in sync
      # in both locations.
      [ -d ${HWMBL_DIR} ] && install -pm755 ${HWMBL_DIR}/bcm2710-*.dtb /boot/dtb/broadcom/
      ;;
esac
