#!/bin/bash
#
set +o posix
#########################################################################################
# Program: fdt-selector
# Purpose: Append the appropriate DTB file to the user's System selection from the
#          known and supported _armv5_ devices on Slackware ARM (including Marvell
#          Kirkwood devices)
# Version: 1.01
# Date...: 05-Mar-2015
# Author : Stuart Winter <mozes@slackware.com>
#########################################################################################
# Note: This script is designed to be run from /boot or slackwarearm-xx/kernels/armv5
#########################################################################################
# Changelog: #
##############
# 05-Mar-2015 - v1.01
# * Renamed 'kirkwood' Kernel file names to the new 'armv5' generic Kernel.
# 18-Dec-2014 - v1.00
#  * First version
##########################################################################################

# File names.  'mkimage' works through symlinks.
ZIMAGE=zImage-armv5
UIMAGE=uImage-armv5
TMPZIMAGE=zImage-armv5-appended

# Sanity checks:
which mkimage >/dev/null 2>&1
if [ $? -gt 0 ]; then
  case "$( uname -m )" in
    arm*) echo "Error: 'mkimage' not found.  You need to install the 'u-boot-tools'"
          echo "       package from the slackware/a/ directory." 
          exit 1;;
    *)    echo "Error: 'mkimage' not found.  You can find packages for a variety of"
          echo "       Slackware releases here:"
          echo "http://ftp.arm.slackware.com/slackwarearm/slackwarearm-devtools/u-boot-tools/packages"
          exit 1;;
  esac
fi
if [ ! -s "$ZIMAGE" ]; then
   echo "Error: Cannot find $ZIMAGE"
   echo "       You should run this script from /boot on your installed"
   echo "       Slackware ARM OS, or slackwarearm-xx/kernels/armv5"
   exit 1
fi

# Ensure that we can find the 'dtb' directory:
if [ ! -d dtb ]; then
   echo "This script should either be run from /boot on the installed Slackware ARM OS"
   echo "or from the slackwarearm-xx/kernels/armv5/ directory"
   echo "I need to find a 'dtb' directory with the list of *.dtb files within it."
   exit 1
fi

# Enumerate the list of FDTs available, and determine what system they are
# supporting:
dialog \
   --title "Processing Device Tree Blobs" "$@" \
   --infobox \
"\nPlease wait: processing the 'armv5' Device Tree Blobs in the 'dtb' directory\n\n" 6 70

# Pre-populate the list with the ability for the user to re-create the uImage
# without an FDT blob appended to the zImage.
# This is useful for users who have a SheevaPlug (that requires an appended FDT blob)
# and an OpenRD that does not.  This way we can have uImage-armv5 listed
# in the INSTALL_KIRKWOOD.TXT and the user can use this script when they want to
# boot different systems using the armv5 kernel.
Systems[0]="Re-build without an appended FDT blob"
DTBs[0]="No DTB"

# Put the most common systems at the top of the list:
Systems[1]="SheevaPlug - eSATA (Best choice - includes USB-only Sheevas)"
DTBs[1]="kirkwood-sheevaplug-esata.dtb"
Systems[2]="SheevaPlug - Original (Only use if eSATA version doesn't work)"
DTBs[2]="kirkwood-sheevaplug.dtb"
Systems[3]="GuruPlug - Standard and Server Plus"
DTBs[3]="kirkwood-guruplug-server-plus.dtb"
Systems[4]="DreamPlug"
DTBs[4]="kirkwood-dreamplug.dtb"
Systems[5]="OpenRD Base"
DTBs[5]="kirkwood-openrd-base.dtb"
Systems[6]="OpenRD Client"
DTBs[6]="kirkwood-openrd-client.dtb"
Systems[7]="OpenRD Ultimate"
DTBs[7]="kirkwood-openrd-ultimate.dtb"

cnt=7 # We've populated slots 0-7 already, so we begin at 4 (cnt++ happens before population in the 'for' loop)
#shopt -s extglob
#for i in dtb/!(kirkwood-guruplug-server-plus*|kirkwood-sheevaplug-esata*|*kirkwood-sheevaplug*).dtb ; do
while read i ; do
  ((cnt++))
  Systems[$cnt]="$( strings $i | egrep '^7' | cut -b2- )"
  DTBs[$cnt]="$i"
done< <(ls -1 dtb/kirkwood*.dtb | sed -e '/kirkwood-openrd-base.dtb/d' \
                                      -e '/kirkwood-openrd-client.dtb/d' \
                                      -e '/kirkwood-openrd-ultimate.dtb/d' \
                                      -e '/kirkwood-dreamplug.dtb/d' \
                                      -e '/kirkwood-guruplug-server-plus.dtb/d' \
                                      -e '/kirkwood-sheevaplug-esata.dtb/d' \
                                      -e '/kirkwood-sheevaplug.dtb/d')

# Dump out the list:
#for (( i=0 ; i <= $((${#DTBs[@]}-1)) ; i++ )); do
#   echo "${DTBs[$i]}" : "${Systems[$i]}" 
#done

# Build the menu:
selection=$( cat << EOF | bash 
dialog \
   --clear \
   --no-tags \
   --extra-label "Choice" \
   --stdout \
   --title "Append Flattened Device Tree Blob - Choose your ARM System" "\$@" \
   --menu "\nPlease select your ARM System from the options below." 50 70 50 \
   $( for (( i=0 ; i <= $((${#DTBs[@]}-1)) ; i++ )); do 
        echo "   \""${DTBs[$i]##*/}"\"    \""${Systems[$i]}"\"   \\"
     done )
EOF
)
if [ $? -eq 1 ]; then
   # The user pressed 'Cancel'
   exit
fi

# Append the chosen FDT file to a temporary zImage, since we need to 
# keep the virgin version to allow us to rotate uImage for any number of
# armv5 systems.
if [ "$selection" = "No DTB" ]; then
   # We want to re-create the uImage without any FDT appended:
   install -pm644 $ZIMAGE $TMPZIMAGE
  else
   # Append an FDT:
   cat $ZIMAGE dtb/$selection > $TMPZIMAGE
fi

# Match the DTB file name back with its description:
for (( i=-1 ; i <= $((${#DTBs[@]}-1)) ; i++ )); do
  if [ "${DTBs[$i]##*/}" = "$selection" ]; then
     infoline="\\nSystem: ${Systems[$i]} \\nDTB...: ${DTBs[$i]}\\n"
  fi
done
dialog \
   --title "Rebuilding uImage..." "$@" \
   --infobox "$infoline" 5 80
echo
mkimage \
   -A arm \
   -O linux \
   -T kernel \
   -C none \
   -a 0x00008000 \
   -e 0x00008000 \
   -n "Slackware on armv5" \
   -d $TMPZIMAGE \
   $UIMAGE

rm -f $TMPZIMAGE

# EOF
