#!/bin/bash
#LAUNCH_FORK
# Note: the above token is consumed by the parent script and causes
# this script to be forked rather than sourced.
#
#################################################################################################
# Script.....: /usr/share/hwm-configure/platform/aarch64/installer/helper.scr/001-uboot-configure
# Called from: /usr/lib/setup/armedslack-postinstall-scripts/001-hwm-os-configure
# Purpose....: Configure the U-Boot Bootloader for supported Hardware Models.
# Version....: 1.01
# Date.......: 07-Apr-2026
# Author.....: Stuart Winter <mozes@slackware.com>
#################################################################################################
# Change log
# v1.01, 07-Apr-2026
# * Move extlinux templates from the ext4 partition of the Installer's disk image, into the
#   Installer initrd to enable configurations where the OS's /boot won't live on the SD card.
# v1.00, 25-Nov-2023
# * Initial version
#################################################################################################
#
# Copyright 2023-2026  Stuart Winter, Earth, Milky Way, "".
# 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.
################################################################################################

# Only install and configure U-Boot if the Kernel cmdline operator 'bl_uboot' is present.
# This enables us to easily determine whether we're meant to configure U-Boot on this Hardware Model
# rather than perform a number of tests on the presence of files, etc.
# We exit silently without error if the cmdline token isn't present, since this doesn't constitute
# an error.
cmdline_has bl_uboot || exit 0

# Location of the root file system:
T_PX=$1
# Installer's temporary location:
TMP=${TMP:-/var/log/setup/tmp}

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

# The Boot Loader configuration template:
EXTLINUX_TEMPLATES_DIR=/usr/share/hwm-configure/platform/${ARCH}/installer/assets/bootloader/u-boot
[ ! -d ${EXTLINUX_TEMPLATES_DIR} ] && exit 1

# Global settings applicable to all Hardware Models:
#
# Determine the root file system type set for this installation.
# This is stored earlier in the installation process by
# /usr/lib/setup/armedslack-SeTpartitions
[ -s ${TMP}/root_sys_type ] && conf_root_fstype="$( < ${TMP}/root_sys_type )"

# Root device's file system label:
# Note: future possibility: need to handle statically set '--label SLKroot' within
# the grub template config file. Not worth the effort unless people start going crazy
# customising stuff.
[ -s ${TMP}/SeTrootdev_labeled ] && conf_root_dev="$( < ${TMP}/SeTrootdev_labeled )"

# Config files:
# This is the config file that lives in the live OS's /boot:
ARMEDSLACK_EXTLINUXCONF=${T_PX}/boot/extlinux/extlinux.conf

# Default set of Kernel cmdline options:
APPEND_KERNELCMDLINE=""

# Set the Kernel cmdline options according to the Hardware Model:
# Typically this is only the console settings.
# Here we enable both the serial console and HDMI/Linux virtual console.
case "${HWM}" in
   "Pine64 RockPro64"*|"Pine64 Pinebook Pro"*)
      APPEND_KERNELCMDLINE=" console=ttyS2,1500000n8 console=tty1 " ;;
   "Raspberry Pi"*)
      # These are the mainline Kernel settings. The RPi Kernel fork uses ttyS0.
      # However, we no longer use U-Boot on the RPi, but this remains for reference
      # and in case we ever want to use U-Boot again.
      APPEND_KERNELCMDLINE=" console=ttyS1,115200 console=tty1 " ;;
esac

dialog \
   --backtitle "Bootware" \
   --sleep 3 \
   --infobox "Configuring Boot Loader..." 3 30

# Commented because we no longer offer this option to retain the Installer
# as it was just a bad idea.
#
# If the reached the end of the installer, elected not to erase the
# installer, and rebooted into the installer to start over, we'll
# have two copies of the OS boot config.  Let's handle that:
# There should be two markers, but we'll remove the stanza if there's >0
#if [ $( grep -c '^##SLKOS' ${ARMEDSLACK_EXTLINUXCONF} 2>/dev/null ) -gt 0 ]; then
#   #echo "...removing existing OS boot stanza (probably from a previous failed installation)"
#   sed -i '/^##SLKOS/,/^##SLKOS/d' ${ARMEDSLACK_EXTLINUXCONF}
#fi

# Install the base 'extlinux.conf' that contains the Kernel cmdline options, other
# useful information and the default extlinux configs:
install -Dpm644 ${EXTLINUX_TEMPLATES_DIR}/extlinux.conf ${ARMEDSLACK_EXTLINUXCONF}

# Add the Slackware OS boot configuration to extlinux.conf:
# This was an experiment using the 'INCLUDE' feature, but I found it buggy
# specifically that it included a single menu entry twice.
#echo 'INCLUDE slkos' >> ${ARMEDSLACK_EXTLINUXCONF}
# Set the device that contains the root filesystem, and its filesystem type:
#sed -i 's?%ROOTDEV%?'"${conf_root_dev}"'?g' ${ARMEDSLACKOS_EXTLINUXCONF}
#sed -i 's?%ROOTFSTYPE%?'"${conf_root_fstype}"'?g' ${ARMEDSLACKOS_EXTLINUXCONF}
# If the user wants to enable the await root device feature, they can
# set a flag from the shell ( $ touch /.enableawaitrootdev ) and we'll add it:
if [ -f /.enableawaitrootdev ]; then
   #echo "...enabling awaitrootdev OS InitRD feature"
   sed -e 's?APPEND ?APPEND awaitrootdev ?g' ${EXTLINUX_TEMPLATES_DIR}/slkos.generic >> ${ARMEDSLACK_EXTLINUXCONF}
 else
   # Add the default OS boot stanza:
   # Note: The reason the OS stanza is held in a separate file is in case we need
   # divergence for any future Hardware Model.
   cat ${EXTLINUX_TEMPLATES_DIR}/slkos.generic >> ${ARMEDSLACK_EXTLINUXCONF}
fi
# This remains, otherwise if you reboot into the installer again from this
# SD image, this file won't exist and the machine won't configure U-Boot for
# booting the OS.
# This applies to the ability to retain the installer image on the SD card.
# The 'slkos.sample' was the file name used when the u-boot config files were
# shipped in the ext4 partition of the Installer.
#rm -f ${T_PX}/boot/extlinux/slkos.sample

# Apply any Kernel cmdline options, substituting the place holder:
# Here we typically configure the console= settings for the particular Hardware Model.
sed -i 's?%POST_INST_OS_CMDLINE_APPEND%?'"${APPEND_KERNELCMDLINE}"'?g' ${ARMEDSLACK_EXTLINUXCONF}

# This assumes a single OS installation:
#echo "...setting root filesystem"
sed -i 's?%ROOTDEV%?'"${conf_root_dev}"'?g' ${ARMEDSLACK_EXTLINUXCONF}
sed -i 's?%ROOTFSTYPE%?'"${conf_root_fstype}"'?g' ${ARMEDSLACK_EXTLINUXCONF}
# No longer necessary since we populate an OS-specific extlinux.conf now, rather than
# re-using the version shipped on the SD card's ext4 file system.
# We ship the SD cards with the default boot option as
# the installer.  Let's switch it to the OS:
#echo "...setting the default boot option to Slackware OS"
#sed -i 's?^DEFAULT Installer?DEFAULT OS?g' ${ARMEDSLACK_EXTLINUXCONF}

# Set the boot path prefix for kernel, initrd, and FDT entries in extlinux.conf.
# When booting from the OS root filesystem, /boot is a subdirectory, so U-Boot
# needs the full path e.g. /boot/Image-armv8:
BOOTDIR_LOC="/boot"
# When the SLKboot filesystem (the installer SD card's boot partition) is mounted,
# these files sit at the partition root — U-Boot sees them as e.g. /Image-armv8.
# The paths in the template contain the '/' prefix, so we just need to remove
# the place holder tokens:
# If the user has opted to handle /boot themselves, /usr/lib/setup/armedslack-SeTpartitions
# will have already umounted it.
findmnt -rn -S LABEL=SLKboot > /dev/null 2>&1 && BOOTDIR_LOC=""
# Switch the template's placeholder with the location where U-Boot can find
# them:
sed -i "s?%BOOTDIR_LOC%?$BOOTDIR_LOC?g" ${ARMEDSLACK_EXTLINUXCONF}
sleep 2
clear
