#!/bin/bash
shopt -s extglob
##############################################################
# Script : get-firmware.sh
# Purpose: Download the RPi firmware binaries and re-pack only
#          what's required for Slackware.
# Author : Stuart Winter <mozes@slackware.com>
# Date...: 11-Dec-2021
##############################################################
# Firmware tag:
# https://github.com/raspberrypi/firmware/tree/master/boot

# Store last tested and working version:
#FWTAG=1.20211118
FWTAG=1.20220120

CWD=$PWD
TMP=/tmp/rpifw
rm -rf $TMP
#mkdir -p $TMP assets
mkdir -p $TMP

# Remove existing version:
#rm -f assets/rpi-boot-fw-*
rm -f rpi-boot-fw-*

cd $TMP
#wget https://github.com/raspberrypi/firmware/archive/refs/tags/1.20210831.tar.gz
wget https://github.com/raspberrypi/firmware/archive/${FWTAG}.tar.gz
tar xf *z
cd firmware-*/boot/
# Clean up Hardware Models that SA64 won't or can't support:
rm -fv bcm2708-* *.img
# Within the sdcards.build script we install our own DTBs from the Slackware Kernel package,
# not that it matters since these DTBs aren't actually used (unless you migrate to the
# RPi's Native Boot Loader); but it feels good to replace it with our own ;-)
# These DTBs are updated whenever the Slackware Kernel package is upgraded.
# Delete all of them apart from the Raspberry Pi3 DTBs.  This is because although the
# Slackware Kernel has support for the RPi3, the DTBs of this name are not created.
# So, to satisfy the RPi Boot Loader, we'll retain them here:
# This will change if/when they get built by the Slackware Kernel, or we discover that we can
# simply copy another SoC's DTB in place.
rm -rf !(*bcm2710-rpi-3*).dtb

tar -Ixz -cf $CWD/rpi-boot-fw-${FWTAG}.tar.xz .
#tar -Ixz -cf $CWD/assets/rpi-boot-fw-${FWTAG}.tar.xz .
rm -rf $TMP

#bcm2710-rpi-3-b.dtb
