#!/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
#
# Modified by Brenton Earl
# * Added EEPROM firmware and utilities
##############################################################
# Firmware tag:
# This contains the DTBs and 2nd stage boot RPi native loader
#
# https://github.com/raspberrypi/firmware/tree/master/boot
# Under 'Files' (left hand side), click on the 'master' drop down list.
# Click on the 'Tags' and pick the top one.
# ( To use the master branch - needs a little more work to date stamp it )
#   FWTAG=master
# Store last tested and working version, most recently tested is last
# in the list:
#FWTAG=1.20250127
#FWTAG=1.20250326

# This is from the master branch:
#FWTAG=1.20250915
# For testing the latest updates:
#FWTAG=next
FWTAG=master

# EEPROM tag:
# --------------
# https://github.com/raspberrypi/rpi-eeprom/releases
# Click on the 'Tags' and pick the top one.
#
# It's worth checking which version of Debian the RPi OS is based on
# https://www.raspberrypi.com/software/operating-systems/
# Presently (Nov 2025) it's Trixie. It's worth switching to the 'trixie'
# branch of EEPROM esp for the RPi5 since it's updated more frequently
# than the regular tagging.
#
# https://github.com/raspberrypi/rpi-eeprom/branches

# Last tested release of eeprom fw and utilities:
#EEPROM_TAG="v2025.12.08-2712""
#EEPROM_TAG="v2026.01.09-2711"
EEPROM_TAG=master

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

# Remove existing version from our source dir:
#rm -f assets/rpi-boot-fw-*
rm -f rpi-boot-fw-*
rm -f rpi-eeprom-fw-*

################################################################################
# Rasberry Pi EEPROM sources:
################################################################################

cd $TMP
if [ "$EEPROM_TAG" = "master" ]; then
   wget https://github.com/raspberrypi/rpi-eeprom/archive/refs/heads/master.tar.gz
 else
   wget https://github.com/raspberrypi/rpi-eeprom/archive/refs/tags/${EEPROM_TAG}.tar.gz
fi

tar xf ${EEPROM_TAG}.tar.gz
cd rpi-eeprom-*/ || exit 1
chown -R root:root .
# Remove any (older?) firmware that doesn't match the tag
# defined earlier in this script:
#rm -rf firmware-!(${EEPROM_TAG##*-})
# All firmware is contained within a single directory, 'firmware':
# Not since the firmware supports the RPi5:
#mv -fv firmware-* firmware

# We don't need to ship old firmware:
find . -name old -type d -print0 | xargs -0 rm -rf

# Clean up:
rm -rf debian/ test/ imager/ releases.md firmware/release-notes.md
find . -name '.git*' -print0 | xargs -0 rm -rf

# In older releases these were directories. Now they are symlinks, so we'll
# capture them as they are and not delete them:
#rm -rf firmware/latest firmware/default firmware/old
#rm -rf firmware/old
tar -Ixz -cf $CWD/rpi-eeprom-fw-${EEPROM_TAG}.tar.xz . || exit 1

################################################################################
# Rasberry Pi Native Boot Loader firmware, DTBs etc.
################################################################################

cd $TMP
#wget https://github.com/raspberrypi/firmware/archive/refs/tags/1.20210831.tar.gz
if [ "$FWTAG" = "master" ]; then
   git clone --depth=1 --single-branch https://github.com/raspberrypi/firmware.git
   pushd firmware
   FWTAG=$(git rev-list --count master).$(git rev-parse --short master)
   popd
   #wget https://github.com/raspberrypi/firmware/archive/refs/heads/master.tar.gz
 else
   wget https://github.com/raspberrypi/firmware/archive/${FWTAG}.tar.gz
   tar xf ${FWTAG}.tar.gz
fi

cd firmware*/boot/
# Clean up Hardware Models that SA64 won't or can't support:
rm -fv bcm2708-* *.img

# 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.
#
# The DTBs for the other RPi Hardware Models are copied to the RPi's bootware partition
# (/boot/platform/hwm_bw within the OS) by the /platform 'inst_dskimg.build' script
# (calling /platform/aarch64/bootware/src/platform/aarch64/bcm2711/dskimg.build-functions)
# and are taken from the Slackware a/kernel package and merged into the SD card's
# FAT partition during the build process, so that the SD card's FAT partition (that the RPi Native
# Boot Loader uses) contains both the DTBs from the a/kernel package and those taken from the RPi
# 'firmware' repo (https://github.com/raspberrypi/firmware) (which is packaged by the
# 'a/hwm-bw-raspberrypi' package).
#
# Once the OS is installed, these DTBs within /boot/platform/hwm_bw are updated whenever
# the Slackware Kernel package is upgraded.
# Delete all DTBs apart from the RPI3's:
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
