#!/bin/bash
set +o posix
shopt -s extglob

#####################################################################################
# Script : inst_dskimg.build
# Purpose: Build Slackware Installer disc images for supported Hardware Models.
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 09-Feb-2021
#####################################################################################
# Note: this script requires that the following scripts have been run.
# * ARM Trusted Firmware (atb.build)
# * U-Boot binaries (uboot.build)
#
# Copyright 2021-2025 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.
#####################################################################################

source /usr/share/slackdev/buildkit.sh

# This is packaged in the 'd' series on Slackware ARM/AArch64.
which kpartx 2>&1 > /dev/null || { echo "kpartx is required.  You can find it in the 'd' Slackware package series"; exit 1; }

# If '$PKGSTORE_OVERRIDE' exists as a directory, it means we have
# test packages outside of the main tree.
# Set $PKGSTORE to this directory to find the Kernel package:
[ -d "$PKGSTORE_OVERRIDE" ] && {
   echo "Using PKGSTORE_OVERRIDE: $PKGSTORE_OVERRIDE"
   export PKGSTORE=$PKGSTORE_OVERRIDE ;}
# This is either the 'root' of the Slackware tree, or is overridden to a pseudo 'root' dir
# where the test assets are stored:
[ -d "${ROOTDIR_OVERRIDE}" ] && rootdir="${ROOTDIR_OVERRIDE}" || rootdir=$PORTROOTDIR

TMP=/tmp/build-sdcard-images

CWD=$PWD
OUT=$CWD/../installer/$SLACKWAREVERSION
BINS=$CWD/../bin/
RECOVERYIMGDIR=$CWD/../recovery/

MNTPOINT=/tmp/sdcard-mount1
MNTPOINT1=/tmp/sdcard-mount2

rm -rf $TMP
mkdir -p $OUT $RECOVERYIMGDIR $MNTPOINT $MNTPOINT1

# Flags:  These can be overridden for each Hardware Model within their
# functions script.
#

# Don't do this anymore - it's superflous and complicates things.
# The Hardware Model Installation Guides refer to unversioned symlinks, which
# means we never know which version of the Installer image they downloaded, so
# there's no point us storing it.
#
# If '$PKGSERIES_OVERRIDE' is set, use that to locate the Slackware Kernel
# package's source directory.  This is to enable this script to find the
# WIP (Work In Progress) versions of the Kernel Module Loader and other
# assets copied from the Kernel package source directory.
#K_SRCDIR=${PKGSERIES_K_OVERRIDE:-k}
#
# Determine which Kernel version we're shipping with this build of the SD cards
# because these will be rebuilt with each Kernel, and will be used to identify
# which a user downloaded should they require support.
#
#eval $( grep -E '^export BUILD' $PORTSRC/source/$K_SRCDIR/arm/build  )
#eval $( grep -E '^export VERSION' $PORTSRC/source/$K_SRCDIR/arm/build )
#[ -z "${VERSION}" ] && { echo "Unable to determine Kernel version number from primary Kernel package build script" ; exit 1 ;}
#[ -z "${BUILD}" ] && { echo "Unable to determine Kernel package build number primary Kernel package build script" ; exit 1 ;}
#echo "Bundling SD Card with Kernel: $VERSION, build: $BUILD"

# Unset callout functions from a previous platform's build
# receipe.
# The function names are prefixed by 'hwm_' which stands for
# Hardware Model.
function unset_hwm_buildfunctions() {
  local fname
  for fname in \
   hwm_initialise \
   hwm_installbootloaderconfig \
   hwm_configure_extlinux \
   hwm_configure_nativebl \
   hwm_sdimagefile_initialise \
   hwm_sdimagefile_initialise_post \
   hwm_sdimage_partition \
   hwm_sdimage_partition_post \
   hwm_sdimage_kpartx_attach \
   hwm_sdimage_mkfs \
   hwm_sdimage_premount \
   hwm_sdimage_mount \
   hwm_sdimage_postmount \
   hwm_sdimage_install_hwm_assets \
   hwm_sdimage_install_hwm_assets_boot \
   hwm_sdimage_preumount \
   hwm_sdimage_umount \
   hwm_sdimage_postumount \
   hwm_sdimage_label \
   hwm_sdimage_fsck \
   hwm_sdimage_uboothdr \
   hwm_sdimage_predetach \
   hwm_sdimage_detach \
   hwm_sdimage_postdetach \
   hwm_sdimage_compress \
   hwm_sdimage_installtotree \
   hwm_sdimage_finalise \
   hwm_sdimage_recovery_build \
   hwm_sdimage_recovery ; do
       unset -f  $fname ; done
}

# Build the SD card image:
function build_sdcard() {

  local assetname=$1
  local soc=$2
  local bootconfig=$3
  local slkportarch=$4

  # Prepare the generic SD root filesystem:
  prepare_sdroot

  # Load the build functions if there are any for this SoC/chipset.
  unset_hwm_buildfunctions
  [ -s $CWD/platform/${slkportarch}/${soc}/dskimg.build-functions ] && \
     { echo "Loading build functions for ${soc}"
       . $CWD/platform/${slkportarch}/${soc}/dskimg.build-functions ;}

  # Call a Hardware Model initialisation function.
  # The purpose of this is to override any settings such as the version
  # of U-Boot, should your Hardware Model be using a different version.
  fnexists hwm_initialise && { hwm_initialise || \
    { echo "*** Failed Hardware Model Initialisation function for $soc" ; exit 1 ;} ;}

  ################################################################################################
  # U-Boot/Extlinux configuration file installation and adjustment
  ################################################################################################

  # Call a Hardware Model build function if present, otherwise
  # build using the standard method:
  errmsg="*** Failed to install boot loader config for device: $soc"
  fnexists hwm_installbootloaderconfig && { hwm_installbootloaderconfig || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Installing standard boot loader config ***"
       # Copy the non-packaged parts for U-Boot's 'sysboot' feature:
       mkdir -vpm755 $TMP/sdroot/extlinux
       # The SD card image contains a single extlinux.conf that boots the Installer.
       # The extlinux.conf capable of booting the OS is configured from within the Installer's
       # U-Boot post install script:
       install -vpm644 $CWD/dskimg-skeleton/extlinux/extlinux.conf $TMP/sdroot/extlinux/ \
       || { echo "$errmsg" ; exit 1 ;} ;}

  # Substitute any changes to the standard extlinux config:
  errmsg="*** Failed to configure extlinux config for device: $soc"
  fnexists hwm_configure_extlinux && { hwm_configure_extlinux || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Configuring extlinux with standard changes ***"
        # We don't need any extra settings as standard:
        sed -i 's?%CMDLINE%??g' $TMP/sdroot/extlinux/* \
       || { echo "$errmsg" ; exit 1 ;} ;}

  # Non-U-Boot configuration. Presently this is only for the RPi.
  # There are no standard actions for this - everything will be contained within
  # the HWM helper script:
  # Currently unused - these changes can be made elsewhere in the helper plugin.
  #errmsg="*** Failed to configure alternate boot loader config for device: $soc"
  #fnexists hwm_configure_nativebl && { hwm_configure_nativebl || { echo "$errmsg" ; exit 1 ;} ;}

  ################################################################################################
  # SD Image file initialisation
  ################################################################################################

  # Call a Hardware Model function to perform any tasks at this stage.
  errmsg="*** Failed SD image file initialisation function for device: soc"
  fnexists hwm_sdimagefile_initialise && { hwm_sdimagefile_initialise || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Initialising SD card image using standard settings ***"
        # Create an empty image file:
        IMAGESIZE=4GB
        # The padding method seems to cause problems where images are >700MB.
        # After excercising the filesystem (making changes to the content),
        # U-Boot often fails: the boot ROM loads but then SPL cannot find the SD card.
        #  dd if=/dev/zero bs=1k of=$TMP/${assetname}.img seek=$((${IMAGESIZE}-1)) count=1
        #dd if=/dev/zero bs=1k of=$TMP/${assetname}.img count=$IMAGESIZE conv=notrunc status=progress
        fallocate -l ${IMAGESIZE} $TMP/${assetname}.img || exit 1

        # Add it as a device and find out where it's gone (probably /dev/loop0):
        DEVLOC=$( basename $( losetup -f --show $TMP/${assetname}.img ) )
        echo "Image file device location: $DEVLOC" \
        || { echo "$errmsg" ; exit 1 ;} ;}


  # Call a Hardware Model post image file creation function:
  fnexists hwm_sdimagefile_initialise_post && { hwm_sdimagefile_initialise_post || \
    { echo "*** Failed Hardware Model sd image post creation function for $soc" ; exit 1 ;} ;}

  ################################################################################################
  # SD Image file initialisation: create partition table
  ################################################################################################

  # Call a Hardware Model function to perform any tasks at this stage.
  errmsg="*** Failed partition table function for device: $soc"
  if ! fnexists hwm_sdimage_partition ; then
     echo "*** Configuring standard partition table ***"
     # A single ext4 partition, starting at 3Mbytes in, to provide
     # space for U-Boot et al (not presntly used since it became unstable -
     # it seemed to corrupt U-Boot, I think the ATF was failing after writes
     # to the filesystem):
     #1M,,,*
     #,,L
   cat <<EOF | sfdisk /dev/${DEVLOC} || { echo "$errmsg" ; exit 1 ;}
3M,,,*
EOF
   else
     # Call the Hardware Model's function:
     hwm_sdimage_partition || { echo "$errmsg" ; exit 1 ;}
  fi

  sync

  # If any changes are required post partitioning, do them now:
  # Typically you'd only use this function if the Hardware Model can use the
  # default partitioning scheme (above), but wants to change an attribute such
  # as the partition being 'bootable', for example.
  errmsg="*** Failed SD card image post function for device: $soc"
  fnexists hwm_sdimage_partition_post && { hwm_sdimage_partition_post || { echo "$errmsg" ; exit 1 ;} ;}

  # Make the loop devices available:
  errmsg="*** Failed to attach SD card image with kpartx for device: $soc"
  fnexists hwm_sdimage_kpartx_attach && { hwm_sdimage_kpartx_attach || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Attaching to SD card image using standard options ***"
        kpartx -av /dev/${DEVLOC} || { echo "$errmsg" ; exit 1 ;} ;}

  # Wait a moment for the devices to settle/appear:
  sync
  sleep 3

  # Make the file system to contain the initrd and Kernel:
  errmsg="*** Failed to attach SD card image with kpartx for device: $soc"
  fnexists hwm_sdimage_mkfs && { hwm_sdimage_mkfs || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Making standard filesystem on SD card ***"
        mkfs.ext4 /dev/mapper/${DEVLOC}p1 || { echo "$errmsg" ; exit 1 ;} ;}

  # Pre-mout hook prior to mounting the new SD card image as loopback:
  errmsg="*** Failed pre mount function for SD card image for device: $soc"
  fnexists hwm_sdimage_premount && { hwm_sdimage_premount || { echo "$errmsg" ; exit 1 ;} ;}

  # Mount the SD card image as loopback so that the assets may be installed:
  errmsg="*** Failed to mount SD card image for device: $soc"
  fnexists hwm_sdimage_mount && { hwm_sdimage_mount || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Mounting SD card image filesystem using standard options ***"
        mount -v /dev/mapper/${DEVLOC}p1 $MNTPOINT || { echo "$errmsg" ; exit 1 ;} ;}

  # Post-mount hook post mounting the new SD card image as loopback:
  errmsg="*** Failed post mount function for SD card image for device: $soc"
  fnexists hwm_sdimage_postmount && { hwm_sdimage_postmount || { echo "$errmsg" ; exit 1 ;} ;}

  ################################################################################################
  # Install assets into the holding area.
  # These assets will be copied into the SD card image.
  ################################################################################################

  # Mount the SD card image as loopback so that the assets may be installed:
  errmsg="*** Failed to copy Hardware Model assets into SD card image for device: $soc"
  fnexists hwm_sdimage_install_hwm_assets && { hwm_sdimage_install_hwm_assets || { echo "$errmsg" ; exit 1 ;} ;}

  # Install the DTBs, Kernel, initrds for the OS and Slackware installer
  # and extlinux configuration, unless the function has been redefined by a helper script:
  if fnexists hwm_sdimage_install_hwm_assets_boot ; then
     # The RPi unsets this since its image does not have an ext formatted /boot since migrating to native BL:
     hwm_sdimage_install_hwm_assets_boot
   else
    # Standard image - e.g. rk3399:
    #
    # Install the DTBs, Kernel, initrds for the OS and Slackware installer
    # and extlinux configuration.
    cp -fav $TMP/sdroot/* $MNTPOINT/
    # Set ownerships:
    chown -R root:root $MNTPOINT/
  fi

  # Perform any Hardware Model-specific prior to mounting the SD card image:
  errmsg="*** Failed pre umount function for SD card image for device: $soc"
  fnexists hwm_sdimage_preumount && { hwm_sdimage_preumount || { echo "$errmsg" ; exit 1 ;} ;}

  ################################################################################################
  # SD Card image finalisation
  ################################################################################################

  # Umount it:
  # There is a 2nd mount point $MNTPOINT1 but the standard boot images have only
  # 1 partition.
  errmsg="*** Failed umount function for SD card image for device: $soc"
  fnexists hwm_sdimage_umount && { hwm_sdimage_umount || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Umounting SD card image filesystem using standard options ***"
        mountpoint -q $MNTPOINT1 && { umount -v $MNTPOINT1 || { echo "$errmsg" ; exit 1 ;} ;}
        mountpoint -q $MNTPOINT  && { umount -v $MNTPOINT  || { echo "$errmsg" ; exit 1 ;} ;} ;}

  # Perform any Hardware Model-specific post umounting the SD card image:
  errmsg="*** Failed post umount function for SD card image for device: $soc"
  fnexists hwm_sdimage_postumount && { hwm_sdimage_postumount || { echo "$errmsg" ; exit 1 ;} ;}

  # Label the device.  This is used within the Slackware ARM installer
  # to locate this image and mount it within both the running OS
  # and within the installer itself, so that the Kernel packages install
  # into the SD card's filesystem (mounted as /boot within the OS).
  # Name is max 16 chars.
  errmsg="*** Failed label function for SD card image for device: $soc"
  fnexists hwm_sdimage_label && { hwm_sdimage_label || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Labeling SD card image filesystem using standard options ***"
        e2label /dev/mapper/${DEVLOC}p1 "SLKboot" || { echo "$errmsg" ; exit 1 ;} ;}

  # Check the filesystem:
  errmsg="*** Failed fsck function for SD card image for device: $soc"
  fnexists hwm_sdimage_fsck && { hwm_sdimage_fsck || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Fsck on SD card image filesystem using standard options ***"
        e2fsck -yf /dev/mapper/${DEVLOC}p1 || { echo "$errmsg" ; exit 1 ;} ;}

  # Add U-Boot as a header to the SD card image.
  # This allows RockPro64's (that are shipped with no U-Boot in the SPI flash)
  # to boot off the Slackware Installer SD card.
  errmsg="*** Failed U-Boot header function for SD card image for device: $soc"
  fnexists hwm_sdimage_uboothdr && { hwm_sdimage_uboothdr || { echo "$errmsg" ; exit 1 ;} ;}

  # Perform any Hardware Model-specific pre-detaching the image:
  errmsg="*** Failed pre SD card image detach function for device: $soc"
  fnexists hwm_sdimage_predetach && { hwm_sdimage_predetach || { echo "$errmsg" ; exit 1 ;} ;}

  # Detach:
  # Substitute any changes to the standard extlinux config:
  errmsg="*** Failed to detach SD card image for device: $soc"
  fnexists hwm_sdimage_detach && { hwm_sdimage_detach || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Detaching SD card image using standard settings ***"
        kpartx -d /dev/${DEVLOC}
        losetup -d /dev/${DEVLOC} \
        || { echo "$errmsg" ; exit 1 ;} ;}

  # Perform any Hardware Model-specific post detaching the image:
  errmsg="*** Failed post SD card image detach function for device: $soc"
  fnexists hwm_sdimage_postdetach && { hwm_sdimage_postdetach || { echo "$errmsg" ; exit 1 ;} ;}

  ################################################################################################
  # Compress SD card image
  ################################################################################################

  # Output information about the just-created SD card image, as we'll get to read it for about 40 mins.
  #echo "Installer image for Kernel........: ${VERSION}"
  echo "Installer disk image asset name: ${assetname}"
  echo "SoC............................: ${soc}"

  errmsg="*** Failed compress function for SD card image for device: $soc"
  fnexists hwm_sdimage_compress && { hwm_sdimage_compress || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Compressing SD card image filesystem using standard options ***"
        sdcompext=xz # used in file names
        xz --threads 0 -vvzef9 $TMP/${assetname}.img || { echo "$errmsg" ; exit 1 ;} ;}

  # Move SD card image into place within the tree:
  errmsg="*** Failed to install SD card image into tree for device: $soc"
  fnexists hwm_sdimage_installtotree && { hwm_sdimage_installtotree || { echo "$errmsg" ; exit 1 ;} ;} || \
      { echo "*** Installing SD card image into the tree using standard settings ***"
       # Remove previous versions for this hardware model:
       rm -fv $OUT/${assetname}-*img*
       #mv -fv $TMP/${assetname}.img.${sdcompext} $OUT/${assetname}-sdimg_${VERSION}_${BUILD}.img.${sdcompext}
       mv -fv $TMP/${assetname}.img.${sdcompext} $OUT/${assetname}-sdimg.img.${sdcompext}
       #ln -rvfs $OUT/${assetname}-sdimg_${VERSION}_${BUILD}.img.${sdcompext} $OUT/${assetname}.sdimg_latest.img.${sdcompext}
       # We setup the symlink in advance of signing it. These will be broken
       # symlinks after this build script completes - this is intentional and expected.
       #ln -rvfs $OUT/${assetname}-sdimg_${VERSION}_${BUILD}.img.${sdcompext}.asc $OUT/${assetname}.sdimg_latest.img.${sdcompext}.asc
       #( cd $OUT ; openssl md5 -r ${assetname}-sdimg_${VERSION}_${BUILD}.img.${sdcompext} | awk '{print $1}'  > ${assetname}-sdimg_${VERSION}_${BUILD}.img.${sdcompext}.md5 ) \
       ( cd $OUT ; openssl md5 -r ${assetname}-sdimg.img.${sdcompext} | awk '{print $1}'  > ${assetname}-sdimg.img.${sdcompext}.md5 ) \
       || { echo "$errmsg" ; exit 1 ;} ;}

  # Any Hardware Model final tasks before completion:
  errmsg="*** Failed finalisation function for device: $soc"
  fnexists hwm_sdimage_finalise && { hwm_sdimage_finalise || { echo "$errmsg" ; exit 1 ;} ;}

  echo "*** SD Card build complete for $soc"
}

# Unpack generic Kernel package:
# Note: this may require breaking out into callout helpers to support other Hardware Models
# in the future.
function prepare_sdroot(){
   rm -rf $TMP/{sdroot,extract-pkgs}
   mkdir -p $TMP/{sdroot,extract-pkgs}
   pushd $TMP/extract-pkgs
      explodepkg $PKGSTORE/a/kernel_*-+([^-])-+([^-])-+([^-]).t[gblx]z
      # Create the symlinks for the initrd, Kernel, System.map and dtb:
      # We're extracting only the symlink creation code incase we add anything sexy
      # into the doinst.st script in the future:
      grep -E '^\( cd boot.*(rm|ln)' install/doinst.sh | bash
      # The OS InitRD doesn't need to be within this image, as
      # it will be installed during the OS installation.
      rm -fv boot/initrd-*
      # In Slackware AArch64, /boot (at least on the RockPro64) will be
      # an ext4 FS on the SD card.
      # Let's move the files relative to that configuration:
      mv -fv boot/* $TMP/sdroot/
      # Copy the installer initrd:
      install -vpm644 $rootdir/installer/initrd-*.img $TMP/sdroot/
   popd
}

# Build the SD card Recovery Image for a specific Hardware Model.
# This will typically be an SD card with a U-Boot header, which will be booted
# then deploy an SPI-capable version too the machine's SPI flash.
function build_sdcard_recovery() {

  local soc=$1
  local slkportarch=$2

  # Load the build functions if there are any for this SoC/chipset.
  unset_hwm_buildfunctions
  if [ -s $CWD/platform/${slkportarch}/${soc}/dskimg_sdrecovery.build-functions ]; then
     echo "Loading build functions for ${soc}"
     . $CWD/platform/${slkportarch}/${soc}/dskimg_sdrecovery.build-functions
   else
     echo "ERROR: Cannot find recovery image configuration script for SoC ${soc}"
     return 1
  fi

  # Launch the Hardware Model's recovery function:
  fnexists hwm_sdimage_recovery && { hwm_sdimage_recovery || { echo "*** Failed SD Recovery Image for SoC $soc" ; exit 1 ;} ;}

}

##################### Build the SD card images ########################################

# Build SD cards for the supported Hardware Models:
# Note: The Pinebook Pro uses the 'rk3399' assets.
# Format:
# <output asset name> <chipset/soc name> <extlinux boot config file name> <architecture>

# Moved to iso.build ###
# Remain as a reference - possibly for booting on other environments that don't
# need an ISO image.
#
# Generic Installer image, used for:
# Virtualization platforms: QEMU, KVM, Apple Virtualization
# Note: "GRUB" is specified as the Boot Loader config.
#
# Hack: The generic sdcard helper script re-defines the extlinux config
# function to prevent installing unnecessary U-Boot assets.
#build_sdcard generic generic GRUB aarch64 || \
#   { echo "Failed build for Generic platforms" ; exit 1 ;}
#####################################################################

# Build the RK3399's U-Boot installation & recovery image:
# This supports the Pinebook Pro, RockPro64.
# By default we don't rebuild these - they can be older versions of U-Boot - we just
# need the loader to boot the Slackware Installer image, from which they are offered
# to flash a newer version if available.
# This will be built very seldom.
#build_sdcard_recovery rk3399 aarch64
#exit 0

# Generic AArch64 SD card Installer image for Hardware Models
# that load U-Boot from SPI flash or some other location than the
# SD card.
# Hardware Models using this are: RockPro64 and Pinebook Pro.
build_sdcard uboot_generic uboot_generic generic aarch64 || \
   { echo "Failed build for generic U-Boot-enabled Hardware Models" ; exit 1 ;}

# Raspberry Pi:
#build_sdcard bcm2711_rpi4 bcm2711 generic aarch64 || \
#   { echo "Failed build for bcm2711 rpi4" ; exit 1 ;}
build_sdcard rpi_generic rpi_generic generic aarch64 || \
   { echo "Failed build for generic Raspberry Pi" ; exit 1 ;}
# July-2024: The RPi3 is no longer supported - 1GB RAM is insufficient for
# the Installer.  The RPi3 support will remain within the OS though, since
# stripping it out requires effort, and it's a useful for future reference.
# Oct-2025: The 1GB RAM comment was prior to stripping the Kernel modules.
# The RPi3 could potentially use the "generic" Slackware Installer image
# created above. However, it's no longer tested as nobody has an RPi3.
# Raspberry Pi 3:
# Despite using a different SoC, the Raspberry Pi assets are all included within the RPi4 SD image,
# and the same U-Boot binary on both Hardware Models.  Therefore we'll symlink the image:
#ln -rvfs $OUT/bcm2711_rpi4-sdimg_${VERSION}_${BUILD}.img.xz $OUT/bcm2837_rpi3.sdimg_latest.img.xz

#
# **** Note: if this changes, the 'sa64-instwrite' script needs to be updated. *****
#
# However, if it requires a separate version of any config or binary
# (U-Boot for example), we'll need to build its own version.
#build_sdcard bcm2837_rpi3 bcm2837 generic aarch64 || \
#   { echo "Failed build for bcm2837 rpi3" ; exit 1 ;}

# Example for other Hardware Models
# The Pinebook Pro (whilst using the same RK3399 SoC as
# the RockPro64) requires divergence: create a separate SD card image:
#build_sdcard rk3399_pinebookpro rk3399 generic aarch64 || exit 1

# Solid-Run Honeycomb LX2160A
# Moved to generic ISO.
# Note: "GRUB" is specified as the Boot Loader config.
#
# Hack: The Honeycomb sdcard helper script re-defines the extlinux config
# function to prevent installing unnecessary U-Boot assets.
# At some point we can enhance inst_dskimg.build to handle config files for
# other Boot Loaders, but for the time being this hack is good enough.
#build_sdcard honeycomb_lx2160acex7 lx2160acex7 GRUB aarch64 || \
#{ echo "Failed build for Honeycomb lx2160acex7" ; exit 1 ;}

# Lenovo Thinkpad X31s
# sc8280xp-lenovo-thinkpad-x13s
# Moved to use generic ISO.
#build_sdcard sc8280xp-lenovo-thinkpad-x13s sc8280xp-lenovo-thinkpad-x13s GRUB aarch64 || \
#  { echo "Failed build for Lenovo Thinkpad X13s (sc8280xp)" ; exit 1 ;}

echo "*** dskimg.build : complete for all Hardware Models"
