##################################################################################
# Helper script..: /boot/platform/aarch64/helper/pkg-kernel-rpi
# Purpose........: Copy Linux Kernel assets on to the RPi's Hardware Model
#                  Bootware 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
# Source location: source/k/boot-skeleton/platform/aarch64/helper/pkg-kernel-rpi
##################################################################################

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

      # 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 upstream Kernel, in sync
      # in both /boot and /boot/platform/hwm_bw.
      # 'T_PX' variable is prefixed here so that this script also works within
      # the Slackware Installer.
      HWMBL_DIR=${T_PX}/usr/share/hwm-bw-raspberrypi/bootloader-firmware

      # 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
      # Refresh all of the components of the bootware with the versions contained within
      # The 'a/hwm-bw-raspberrypi' package:
      # This includes the overlays, but we'll handle those in a moment:
      cp -dpRfL ${HWMBL_DIR}/* ${HWM_MNTPT}/ || exit 1
      # Copy the DTB Overlays:
      # If the Slackware Kernel package provides them, copy those into the RPi bootware file system:
      # Note: only the RPi Kernel fork package contains them - the upstream doesn't.
      if [ -d /boot/dtb/overlays ]; then
         # We're probably using the RPi Kernel fork package:
         cp -dpRfL /boot/dtb/overlays ${HWM_MNTPT}/ || exit 1
        else
         # Probably using the upstream kernel.org Kernel package:
         # Copy the versions from the 'a/hwm-bw-raspberrypi' package.
         # These are built for the RPi Kernel fork though, not the upstream Kernel, so
         # probably are useless as they have different symbols. Some seem to work though.
         cp -dpRfL ${HWMBL_DIR}/overlays ${HWM_MNTPT}/ || exit 1
      fi
      # Copy the Device Tree Blobs (DTBs) deployed from the Slackware Kernel package:
      # This ensures that the DTBs are aligned with the incoming Kernel,
      # since the mainline DTBs can only boot the mainline Kernel and vice versa.
      cp -dpRfL /boot/dtb/broadcom/* ${HWM_MNTPT}/ || exit 1
      ;;
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
