# Template file for 'rpi-firmware'
pkgname=rpi-firmware
version=20230925
revision=1
_rpi_fw=6b37a457122714aa14b2c7df0926455173fd04f1
_rpi_bt=9556b08ace2a1735127894642cc8ea6529c04c90
_rpi_brcm=2c3a8701193ba23d0ef85cdf0d0c9e47baf03dfc
create_wrksrc=yes
archs="armv6l* armv7l* aarch64*"
short_desc="Firmware files for the Raspberry Pi"
maintainer="Piraty <mail@piraty.dev>"
license="BSD-3-Clause, custom:Cypress"
homepage="https://github.com/raspberrypi/firmware"
distfiles="
 https://github.com/raspberrypi/firmware/archive/${_rpi_fw}.tar.gz
 https://github.com/RPi-Distro/firmware-nonfree/archive/${_rpi_brcm}.tar.gz
 https://github.com/RPi-Distro/bluez-firmware/raw/${_rpi_bt}/broadcom/BCM43430A1.hcd
 https://github.com/RPi-Distro/bluez-firmware/raw/${_rpi_bt}/broadcom/BCM4345C0.hcd
 https://github.com/RPi-Distro/bluez-firmware/raw/${_rpi_bt}/broadcom/BCM43430B0.hcd
 https://github.com/RPi-Distro/bluez-firmware/raw/${_rpi_bt}/broadcom/BCM4345C5.hcd
 https://github.com/RPi-Distro/bluez-firmware/raw/${_rpi_bt}/synaptics/SYN43430A1.hcd>BCM43430A1.raspberrypi,model-zero-2-w.hcd
 https://github.com/RPi-Distro/bluez-firmware/raw/${_rpi_bt}/synaptics/SYN43430B0.hcd>BCM43430B0.raspberrypi,model-zero-2-w.hcd
 https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/plain/LICENCE.cypress>LICENCE.cypress"
checksum="913986b1be22b8dc30a3e6a9b5a28316b1bbee7bec90040ddefff0d7512560f4
 9156d90116b921d2e7938b52fad84fbe1d96df622a66c5db09569c735832387a
 c096ad4a5c3f06ed7d69eba246bf89ada9acba64a5b6f51b1e9c12f99bb1e1a7
 51c45e77ddad91a19e96dc8fb75295b2087c279940df2634b23baf71b6dea42c
 338c2c6631131f516bfc7e64ef0872bd0402e1f98ef9d0c900eef0c814d90a25
 fb9f4ec2df5301bd35f416384e103c012c5983024c49aa72fbbaf90177512caa
 55071227c94d86369d04f9aff3bbfd4197a78a53dc350295123e1a8b048bba8f
 338c2c6631131f516bfc7e64ef0872bd0402e1f98ef9d0c900eef0c814d90a25
 ae0db6cc4db33941148df0f67de53e76a77b1b5a46b3165edb7040aa2750015f"

skip_extraction="BCM43430A1.hcd BCM4345C0.hcd BCM43430B0.hcd BCM4345C5.hcd
 BCM43430A1.raspberrypi,model-zero-2-w.hcd BCM43430B0.raspberrypi,model-zero-2-w.hcd
 LICENCE.cypress"

provides="linux-firmware-broadcom-${version}_${revision}"
replaces="linux-firmware-broadcom>=0"

conf_files="/boot/cmdline.txt /boot/config.txt"
nostrip=yes

do_install() {
	# bootloader
	vlicense "firmware-${_rpi_fw}/boot/LICENCE.broadcom"
	rm -rf "firmware-${_rpi_fw}/boot/"*.img \
		"firmware-${_rpi_fw}/boot/overlays" \
		"firmware-${_rpi_fw}/boot/"*.dtb \
		"firmware-${_rpi_fw}/boot/COPYING.linux" \
		"firmware-${_rpi_fw}/boot/LICENCE.broadcom"

	vmkdir boot
	vcopy "firmware-${_rpi_fw}/boot/*" boot

	# Install configuration files.
	vinstall "${FILESDIR}/cmdline.txt" 644 boot
	vinstall "${FILESDIR}/config.txt" 644 boot

	# firmware
	# we can use either the -standard or -minimal 43455 file so use standard by default
	ln -sfv cyfmac43455-sdio-standard.bin "firmware-nonfree-${_rpi_brcm}/debian/config/brcm80211/cypress/cyfmac43455-sdio.bin"
	vlicense "${XBPS_SRCDISTDIR}/${pkgname}-${version}/LICENCE.cypress"

	# add original Pi Foundation firmware files
	# install deref's symlinks and copies a whole file, which makes rpi use duplicated variants specifically from downstream firmware
	install -Dm644 \
		"firmware-nonfree-${_rpi_brcm}/debian/config/brcm80211/brcm/"brcmfmac*raspberrypi* \
		-t "${DESTDIR}/usr/lib/firmware/brcm"

	# but 43436/43436s/43456 is not in linux-firmware, so copy it normally
	vcopy "firmware-nonfree-${_rpi_brcm}/debian/config/brcm80211/brcm/brcmfmac43456"* usr/lib/firmware/brcm
	vcopy "firmware-nonfree-${_rpi_brcm}/debian/config/brcm80211/brcm/brcmfmac43436"* usr/lib/firmware/brcm

	for file in "${XBPS_SRCDISTDIR}/${pkgname}-${version}/"*.hcd; do
		vinstall "$file" 0644 usr/lib/firmware/brcm
	done
}
