#!/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:
# 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
FWTAG=1.20250915
#
# EEPROM tag:
# https://github.com/raspberrypi/rpi-eeprom/releases
# Click on the 'Tags' and pick the top one.
#
# Last tested release of eeprom fw and utilities:
#EEPROM_TAG="v2025.02.12-2712"
#EEPROM_TAG="v2025.03.10-2712"
EEPROM_TAG="v2025.05.08-2712"

# EEPROM_TAG="v2023.12.06-2712"
# Tried this release but it was duff.
# They switched to symlinks in the dirs (solved), but rpi-eeprom-d always
# display '0' for latest. It worked if I moved the previous f/w back into place.
# so perhaps it's a duff release.. skipping this one.
#   CURRENT: Wed Jan 11 17:40:52 UTC 2023 (1673458852)
#    LATEST: Wed Jan 11 17:40:52 UTC 2013 (0)

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:
################################################################################

# Note: this broke with v2023.12.06-2712
# where they made the directories symlinks.

cd $TMP
wget https://github.com/raspberrypi/rpi-eeprom/archive/refs/tags/${EEPROM_TAG}.tar.gz
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
rm -rf test/ imager/ releases.md .gitignore .github/ firmware/release-notes.md
# 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
