#!/bin/bash

#############################################################################################
# Script: isolinux/u-boot_tftp/build_tftp_uboot_scripts.sh
# Author: Stuart Winter <mozes@slackware.com>
#         With help from contributors to this thread:
# http://www.linuxquestions.org/questions/slackware-arm-108/tftp-u-boot-script-4175591119/
# Purpose: Build bootable u-boot script that will boot the Slackware installer, using the same
#          methods as described in the INSTALL_<system> documentation.
#          This saves pasting so much in to the u-boot prompt.
# Version: 0.00
# Date...: 06-Dec-2016
#############################################################################################
#
# Change log
#############################################################################################
# 06-Dec-2016 - v 0.00
#   * Initial version to test proof of concept.  Lots of unnecessary duplication.
#############################################################################################

TMP=$( mktemp -d ) || { echo "Failed to create temporary directory" ; exit 1 ;}

function compile_script() {
mkimage \
   -C none \
   -A arm \
   -T script \
   -n "SlkARM inst: $1" \
   -d $TMP/$1 \
   $1
}

##################################################################
# Build Orange Pi's
##################################################################
# Usage:  orangepi <u-boot script name> <dtb name>
#         orangepi orangepi_h3_plus sun8i-h3-orangepi-plus.dtb
#
# Note - if you want the installer to use the HDMI, add "console=tty0" to the bootargs.
# However, this means that you WON'T get any control on the serial console
# whilst the installation is on-going.  However, once the OS boots you'll have consoles
# AND a serial line because the OS has that set up in /etc/inittab.
#
function orangepi () {
local outfile=$1
local dtb=$2
cat << 'EOF' > $TMP/$outfile
setenv fdt_addr 0x43000000
setenv kernel_addr_r 0x47000000
setenv ramdisk_addr_r 0x48000000
setenv DTB $dtb
saveenv
setenv slkconsole "console=ttyS0,115200n8"
setenv bootcmd_generic 'kgdboc=kms,ttyS0,115200 debug earlyprintk'
setenv bootargs "console=ttyS0,115200n8 ${bootcmd_generic} TERM=screen-256color nic=auto:eth0:dhcp root=/dev/ram rw"
tftp ${fdt_addr} slackwarearm-15.0/dtb/$dtb
fdt addr ${fdt_addr} 0x40000
tftp ${kernel_addr_r} slackwarearm-15.0/zImage-armv7
tftp ${ramdisk_addr_r} slackwarearm-15.0/initrd-armv7.img
bootz ${kernel_addr_r} ${ramdisk_addr_r}:${filesize} ${fdt_addr}
reset
EOF
# Hack since I don't want to escape all of the quotes in the file above:
sed -i 's?\$dtb?'"$dtb"'?g' $TMP/$outfile
}

# Build Orange Pi scripts:
orangepi orangepi-a20-one   sun7i-a20-orangepi.dtb
orangepi orangepi-a20-mini  sun7i-a20-orangepi-mini.dtb
orangepi orangepi-one       sun8i-h3-orangepi-one.dtb
orangepi orangepi-2         sun8i-h3-orangepi-2.dtb
orangepi orangepi-lite      sun8i-h3-orangepi-lite.dtb
orangepi orangepi-h3-plus   sun8i-h3-orangepi-plus.dtb
orangepi orangepi-h3-plus2e sun8i-h3-orangepi-plus2e.dtb
orangepi orangepi-pc        sun8i-h3-orangepi-pc.dtb
orangepi orangepi-pc-plus   sun8i-h3-orangepi-pc-plus.dtb
orangepi orangepi-zero      sun8i-h2-plus-orangepi-zero.dtb
# Won't make the A20 versions yet - they're not so interesting.
#/boot/dtb/sun7i-a20-orangepi-mini.dtb
#/boot/dtb/sun7i-a20-orangepi.dtb


##################################################################
# Build Banana Pi u-boot script                                  #
##################################################################
cat << 'EOF' > $TMP/bananapi
setenv fdt_addr 0x43000000
setenv kernel_addr_r 0x47000000
setenv ramdisk_addr_r 0x48000000
saveenv
setenv slkconsole "console=ttyS0,115200n8"
setenv bootcmd_generic 'kgdboc=kms,ttyS0,115200 debug earlyprintk sunxi_g2d_mem_reserve=0 sunxi_ve_mem_reserve=0 disp.screen0_output_mode=EDID:1280x720p50 hdmi.audio=EDID:0'
setenv bootargs "console=ttyS0,115200n8 ${bootcmd_generic} TERM=screen-256color nic=auto:eth0:dhcp root=/dev/ram rw"
tftp ${fdt_addr} slackwarearm-15.0/dtb/sun7i-a20-bananapi.dtb
fdt addr ${fdt_addr} 0x40000
tftp ${kernel_addr_r} slackwarearm-15.0/zImage-armv7
tftp ${ramdisk_addr_r} slackwarearm-15.0/initrd-armv7.img
bootz ${kernel_addr_r} ${ramdisk_addr_r}:${filesize} ${fdt_addr}
reset
EOF

##################################################################
# Build Banana Pi Pro u-boot script                              #
##################################################################
cat << 'EOF' > $TMP/bananapipro
setenv fdt_addr 0x43000000
setenv kernel_addr_r 0x47000000
setenv ramdisk_addr_r 0x48000000
saveenv
setenv slkconsole "console=ttyS0,115200n8"
setenv bootcmd_generic 'kgdboc=kms,ttyS0,115200 debug earlyprintk sunxi_g2d_mem_reserve=0 sunxi_ve_mem_reserve=0 disp.screen0_output_mode=EDID:1280x720p50 hdmi.audio=EDID:0'
setenv bootargs "console=ttyS0,115200n8 ${bootcmd_generic} TERM=screen-256color nic=auto:eth0:dhcp root=/dev/ram rw"
tftp ${fdt_addr} slackwarearm-15.0/dtb/sun7i-a20-bananapro.dtb
fdt addr ${fdt_addr} 0x40000
tftp ${kernel_addr_r} slackwarearm-15.0/zImage-armv7
tftp ${ramdisk_addr_r} slackwarearm-15.0/initrd-armv7.img
bootz ${kernel_addr_r} ${ramdisk_addr_r}:${filesize} ${fdt_addr}
reset
EOF

##################################################################
# Build Trimslice Pro u-boot script                              #
##################################################################
cat << 'EOF' > $TMP/trimslicepro
setenv fdt_high    0x2C000000
setenv initrd_high 0x2C000000
saveenv
tftpboot 0x5880000 slackwarearm-15.0/dtb/tegra20-trimslice.dtb
tftpboot 0x01100000 slackwarearm-15.0/uinitrd-armv7.img
tftpboot 0x00800000 slackwarearm-15.0/uImage-armv7
setenv bootargs earlyprintk mem=384M@0M mem=512M@512M nvmem=128M@384M vmalloc=248M video=tegrafb kgdboc=kms,ttyS0,115200 console=ttyS0,115200n8 TERM=screen-256color nic=auto:eth0:dhcp root=/dev/ram rw
bootm 0x00800000 0x01100000 0x5880000
reset
EOF

#for syst in \
#  bananapi \
#  bananapipro \
#  trimslicepro \
#  \
#  \
#  orangepi-one orangepi-2 orangepi-mini orangepi-lite orangepi-h3-plus orangepi-h3-plus2e orangepi-pc orangepi-pc-plus \
#  \
#  \
#  ; do compile_script $syst
#done
for syst in $( ls -1 $TMP ); do
  compile_script $syst
done

# Tidy
rm -rf $TMP
