#!/bin/bash
###############################################################################################################
# Script......: /usr/share/hwm-configure/platform/aarch64/installer/helper.scr/999-rpi-vendor-kernel-transition
# Called from : /usr/lib/setup/armedslack-postinstall-scripts/001-hwm-os-configure (sourced, not forked)
# Purpose....: Offer to transition to the Raspberrty Pi vendor fork Kernel, if available within the AiO
#              Installer package repo.
#              Configure the OS and Boot Loader configuration accordingly.
# Version....: 1.00
# Date.......: 12-Feb-2026
# Author.....: Stuart Winter <mozes@slackware.com>
###############################################################################################################
#
# Copyright 2026  Stuart Winter, Surrey, UK.
# 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.

# This script is intended for booting the generic RPi Slackware Installer using mainline Kernel.
# If we're running the Raspberry Pi Kernel fork variant of the Installer (created by SAIR)
# then we should exit, since it already configures everything.
[ -f /.is-rpi-kernel-fork ] && return 0

# Quit silently if we're not running on a Raspberry Pi.
# At some point we might need to differentiate between RPi models, but for the moment
# the settings apply to all variants.
[[ "${HWM}" != "Raspberry Pi"* ]] && return 0

# Serial console address for Raspberry Pi variants using the RPi Kernel fork:
case "$HWM" in
   "Raspberry Pi 5"*)
   RPIVER=rpi5
   SERIALDEV=ttyAMA0;;
   "Raspberry Pi 4"*)
   RPIVER=rpi4
   SERIALDEV=ttyS0;;
   "Raspberry Pi 3"*)
   RPIVER=rpi3
   SERIALDEV=ttyS0;;
esac

# We don't provide an RPi3 vendor fork Kernel, so let's abort if we're found running on one:
[[ "${RPIVER}" == "rpi3" ]] && return 0

# The newly installed OS' /etc/inittab:
inittabfile=${T_PX}/etc/inittab

# slackpkg's blacklist:
slackpkgblacklistfile=${T_PX}/etc/slackpkg/blacklist

# Raspberry Pi Native BL config.txt:
slk_rpi_bl_cmdline=${T_PX}/boot/platform/hwm_bw/cmdline.txt
# Silently quit if the cmdline file isn't present:
[ ! -f "${slk_rpi_bl_cmdline}" ] && return 0

# Location of the RPi Kernel fork packages stored within the AiO Installer:
# This would have been mounted early on in the Installer journey.
AIOPKGDIR=/slack-all-in-one/experimental/rpi-kernel-fork
[ ! -d "${AIOPKGDIR}" ] && return 0

# Ensure that the correct RPi Kernel packages are present. Exit silently if not found.
KERNEL_PKG_BASE=$(ls ${AIOPKGDIR}/kernel_armv*-*_"${RPIVER}".txz 2>/dev/null | head -n1)
[ -z "$KERNEL_PKG_BASE" ] && return 0
KERNEL_PKG_SRC=$(ls ${AIOPKGDIR}/kernel-source*_"${RPIVER}".txz 2>/dev/null | head -n1)
[ -z "$KERNEL_PKG_SRC" ] && return 0

dialog \
   --backtitle "Linux Kernel Choice" \
   --title "Raspberry Pi Kernel Selection" \
   --defaultno \
   --yesno \
"\nSupport for Raspberry Pi hardware in the upstream mainline Linux kernel is actively \
developed but can occasionally be incomplete or unstable. \
\n\nPlease see the Slackware Installation guide for more information and to help \
decide whether you may want to switch to the Raspberry Pi vendor Kernel fork. \
\n\n \
Please note - the RPi vendor kernel: \n \
   * Must be upgraded manually\n \
   * Is not maintained for stable releases (only the development branch)\n \
   * Does not have feature parity with the generic mainline kernel\n \
\n \
Would you like to switch to the Raspberry Pi vendor kernel fork now?\n \
\nRecommendation is 'No'" 21 80
# User chose 'no'? quit without error
[ $? = 1 ] && return 0

dialog \
     --backtitle "Linux Kernel Choice" \
     --infobox "\nPreparing to transition to vendor Kernel fork..." 5 55

# Upgrade packages: kernel_source and kernel_armvX (base pkg)
# We ensure that T_PX is exported because the RPi helper script
# ('/boot/platform/aarch64/helper/pkg-kernel-rpi') within the a/kernel_armvX base
# package requires access to the /usr file system of the OS in order to copy the
# RPi firmware/bootware into place.
export T_PX

#INFOBOX=0 ROOT=${T_PX}/ upgradepkg --reinstall --install-new ${KERNEL_PKG_BASE}
# Determine the Kernel base package name. This one day may be armv9, so we need to discover it.
# There shouldn't be >1 base Kernel package.. if there is, we'll need to fix that in a few places.
# or make this script select the correct one for the rpi.
# Problems for the future ;-)
kbasepkg_filename=( ${T_PX}/var/lib/pkgtools/packages/kernel_armv?-*-* )
KERNEL_BASEPKG_FILENAME=${kbasepkg_filename[0]##*/}; KERNEL_BASEPKG_FILENAME=${KERNEL_BASEPKG_FILENAME%%-*}
ROOT=${T_PX}/ removepkg ${KERNEL_BASEPKG_FILENAME} > /dev/null 2>&1

# If the mainline Kernel-source package is installed:
if [ -f ${T_PX}/var/lib/pkgtools/packages/kernel-source-*-* ]; then
   # Remove it from the OS:
   # The Kernel source package has a static name:
   ROOT=${T_PX}/ removepkg kernel-source > /dev/null 2>&1
   # Install the vendor fork Kernel source package:
   ROOT=${T_PX}/ installpkg -infobox ${KERNEL_PKG_SRC}
fi

# Install the vendor fork base package:
ROOT=${T_PX}/ installpkg -infobox ${KERNEL_PKG_BASE}

# Configure the OS and Boot Loader:
dialog \
   --backtitle "Hardware Model Variant Customizations" \
   --sleep 4 \
   --infobox "\nAdjusting configurations for Raspberry Pi Kernel fork ..." 6 50

# /etc/inittab:
# ttyS1 is set by the 'a/sysvinit-scripts' package's doinst.sh - all RPi's are currently
# configured to set it to ttyS1.
[ -f ${inittabfile} ] && sed -iE '/^s0:12345:/ s/ttyS1/'"${SERIALDEV}"'/' ${inittabfile}

# Switch the Linux Kernel console serial settings for use with the RPi Kernel fork:
# This exits the RPi Boot Loader's cmdline.txt:
sed -Ei 's/(console=)tty[^ ,]*,/\1serial0,/g' ${slk_rpi_bl_cmdline}

# Edit the inittab to open a getty on the correct serial interface:
sed -Ei "/^# Local serial lines:$/,/^#/{
  /^[^#]/ s/(agetty.*[[:space:]])tty[^[:space:]]+/\1${SERIALDEV}/
}" ${inittabfile}

# Prevent os-initrd-mgr from running within the Installer, since it'll dump some warnings anyway,
# and the generic Kernel will suffice to boot:
touch /.os-initrd-no-fw-sync
# There's also another file bundled within the Slackware ARM Installer, /.norun-os-initrd-mgr
# This instructs os-initrd-mgr (bundled within the Kernel package) to exit immediately.
# The file above instructs one of the Slackware Installer scripts: /usr/lib/setup/armedslack-postinstall-scripts/os-initrd-mgr
# to exit immediately. The reason why that script runs is to incorporate any firmware.
# /.norun-os-initrd-mgr is also used by the Kernel's doinst.sh script which will query the
# root file system (in this case the Installer's root fs), and run os-initrd-mgr within a chroot if that
# flag file is absent.

# Black list the regular Slackware Kernel to avoid them replacing the RPi Kernel fork:
if [ -f ${slackpkgblacklistfile} ]; then
cat << EOF >> ${slackpkgblacklistfile}

# Blacklist the kernel from slackpkg's config file:
# Blacklist the official Linux Kernel Slackware packages so that the
# Raspberry Pi Kernel fork packages can remain installed.
kernel_armv8
kernel-source
# The kernel-headers package should not be blacklisted. The official Slackware
# package should remain - there's no need to install an RPi specific version.
#kernel-headers
EOF
fi
