#!/bin/bash
#####################################################################################
# Script : uboot.build
# Purpose: Build u-boot for Slackware ARM's supported devices
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 15-Feb-2019
#####################################################################################
# References:
#    http://www.orangepi.org/Docs/Makingabootable.html
#    http://linux-sunxi.org/Mainline_U-Boot#Compile_U-Boot
# Inspired by https://wiki.debian.org/InstallingDebianOn/Allwinner
#####################################################################################

CWD=$PWD
TMP=/tmp/build-uboot
OUT=$PWD/../bin/

rm -rf $TMP
mkdir -p $TMP $OUT
cd $TMP

# U-Boot version:
# http://git.denx.de/?p=u-boot.git;a=summary
#UBOOTVER="2020.01-rc4"
UBOOTVER="2021.10"
SLACKBUILD=${UBOOTVER}_1 # 1=local build number

# Pull date into a string for easy reference:
#DATENOW=$( date +"%d_%b_%Y" )

function download_uboot_git() {
  # http://git.denx.de/?p=u-boot.git;a=summary
  # Download latest U-Boot 'master' source:
  git clone --depth 1 -b "${UBOOTVER}" git://git.denx.de/u-boot.git
  #git clone --depth 1  git://git.denx.de/u-boot.git
  cd u-boot*/ || exit 1
}

# U-Boot release:
# ftp://ftp.denx.de/pub/u-boot/
function download_uboot_release() {
#  wget ftp://ftp.denx.de/pub/u-boot/u-boot-${UBOOTVER}.tar.bz2
  # We're in $TMP already:
  tar xf $CWD/sources/u-boot*z*
  cd u-boot*/ || exit 1
}

# Build u-boot for the specified device:
function build_uboot() {
  local devicetype=$1
  # I want the output file names to be lower case since some are 'Orange' and others 'orange'
  # which would just makes my documentation more complex.
  local lc_devicetype=$( echo $devicetype | tr '[A-Z]' '[a-z]' )

  echo "*** Building U-Boot for $devicetype ***"

  # Wipe the existing SD card image for this device:
  rm -fv $OUT/${lc_devicetype}.{*img*,*bin*}

  # Configure:
  rm -f .config
  make ${devicetype}_defconfig oldconfig

  # Brand it:
  sed -i 's?^CONFIG_IDENT_STRING=.*?CONFIG_IDENT_STRING="  Slackware"?g' .config

  # Support some more stuff.
  # The reason it's like this rather than making some config files, is because
  # a) I don't follow U-Boot development and don't fancy keeping a config file up to date
  # b) There are lots of devices supported.
  # It's easier presently to take the default config and modify it a little.
  # Switch out some that are distinctly disabled:
  # This order was made by:
  #  $ make blah_defconfig oldconfig ; cp .config config
  #  $ make menuconfig # make changes
  #  $ diff -u config .config
# This needs a little more work since it FTBFSses..
#  sed -i 's?# CONFIG_SPL_SPI_FLASH_SUPPORT .*?CONFIG_SPL_SPI_FLASH_SUPPORT=y?g' .config
#  sed -i 's?# CONFIG_SPI_BOOT .*?CONFIG_SPI_BOOT=y?g' .config
  # Add some additional configuration in to the correct location:
#  sed -i '/CONFIG_SPL_SATA_SUPPORT/aCONFIG_SPL_SPI_FLASH_TINY=y' .config
#  sed -i '/CONFIG_SPL_SPI_FLASH_TINY/aCONFIG_SPL_SPI_LOAD=y' .config
  # We'll store the U-Boot environment on device 0 which is the MMC (SD card) since that'll be
  # the device number of the card we wrote the copy of U-Boot that this script made:
  sed -i 's?1:auto?0:auto?g' .config

  # Build:
  #make -j$( nproc ) || { echo "*** Failed to build for device: $devicetype" ; exit 1 ;}
  make $NUMJOBS || { echo "*** Failed to build for device: $devicetype" ; exit 1 ;}

  # Move the U-Boot binary in to place, and name it appropriately:
  xz -fez9 u-boot-sunxi-with-spl.bin
  install -vpm644 u-boot-sunxi-with-spl.bin.xz $OUT/${lc_devicetype}.u-boot-${SLACKBUILD}.bin.xz
  ln -rvfs $OUT/${lc_devicetype}.u-boot-${SLACKBUILD}.bin.xz $OUT/${lc_devicetype}.u-boot-latest.bin.xz
}

# Download U-Boot source:
#download_uboot_git
download_uboot_release
[ "$1" = "srconly" ] && { echo "Downloaded src only into $PWD, quitting." ; exit 0 ;}

# Build the known devices:
# Currently Slackware ARM has only been tested on the "Orange Pi H3 Plus v1.1" and the 'Orange Pi PC v1.2'
# (as seen on the board printed under the GPIO pins)
# This list is taken from the 'defconfig' file names within the U-Boot src tree:
#
for syst in \
 \
 Orangepi \
 orangepi_2 \
 orangepi_plus \
 orangepi_plus2e \
 orangepi_lite \
 Orangepi_mini \
 orangepi_pc \
 orangepi_pc_plus \
 orangepi_zero \
 orangepi_r1 \
 \
 Bananapro \
 Bananapi \
 \
 ; do build_uboot $syst
done
