# Template file for 'linux-firmware'
pkgname=linux-firmware
version=20260810
revision=1
hostmakedepends="rdfind which $(vopt_if compressed zstd)"
depends="linux-firmware-amd>=${version}_${revision} linux-firmware-network>=${version}_${revision}"
short_desc="Binary firmware blobs for the Linux kernel"
maintainer="classabbyamp <void@placeviolette.net>"
license="custom:see /usr/share/licenses/linux-firmware"
homepage="https://www.kernel.org/"
changelog="https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/log/"
distfiles="https://gitlab.com/kernel-firmware/linux-firmware/-/archive/${version}/linux-firmware-${version}.tar.gz"
checksum=b78dd0475df5aabafe1e515dc988b17de8c5f5afce16573453275f5f855a2cb2
python_version=3
nostrip=yes

build_options="compressed"
build_options_default="compressed"
desc_option_compressed="compress firmware with zstd"

do_install() {
	if [ -n "$build_option_compressed" ]; then
		env ZSTD_CLEVEL=9 ZSTD_NBTHREADS="$XBPS_MAKEJOBS" \
			make install-zst dedup FIRMWAREDIR=/usr/lib/firmware DESTDIR="$DESTDIR"
	else
		make install dedup FIRMWAREDIR=/usr/lib/firmware DESTDIR="$DESTDIR"
	fi

	for _l in LICENSE WHENCE LICENSES/LICEN*
	do
		vlicense "$_l"
	done
}

linux-firmware-amd_package() {
	short_desc+=" - AMD CPU/GPU microcode"
	nostrip=yes
	pkg_install() {
		vmove usr/lib/firmware/amd*
		vmove usr/lib/firmware/radeon
		vmove usr/share/licenses/linux-firmware/LICENSE.amdgpu
		vmove usr/share/licenses/linux-firmware/LICENSE.amd-ucode
		vmove usr/share/licenses/linux-firmware/LICENSE.radeon
		vmkdir usr/lib/dracut/dracut.conf.d
		echo "early_microcode=yes" >> ${PKGDESTDIR}/usr/lib/dracut/dracut.conf.d/amd_ucode.conf
	}
}

linux-firmware-broadcom_package() {
	short_desc+=" - Broadcom network blobs"
	nostrip=yes
	pkg_install() {
		vmove usr/lib/firmware/brcm
		vmove usr/share/licenses/linux-firmware/LICENCE.broadcom_bcm43xx
		# firmware/brcm contains multiple symlinks to ../cypress/
		vmove usr/lib/firmware/cypress
		vmove usr/share/licenses/linux-firmware/LICENCE.cypress
		# provided by rpi-firmware
		find "${PKGDESTDIR}"/usr/lib/firmware/brcm -iname '*raspberry*.txt' -delete
	}
}

linux-firmware-intel_package() {
	short_desc+=" - Intel GPU microcode"
	nostrip=yes
	pkg_install() {
		for d in i915 xe; do
			vmove "usr/lib/firmware/$d"
			vmove "usr/share/licenses/linux-firmware/LICENSE.$d"
		done
	}
}

linux-firmware-nvidia_package() {
	short_desc+=" NVIDIA GPU microcode"
	nostrip=yes
	pkg_install() {
		vmove usr/lib/firmware/nvidia
		vmove usr/share/licenses/linux-firmware/LICENCE.nvidia*
	}
}

linux-firmware-network_package() {
	short_desc+=" - network"
	depends="linux-firmware-broadcom>=${version}_${revision}"
	case "$XBPS_TARGET_MACHINE" in
		armv7*|aarch64*)
			depends+=" linux-firmware-qualcomm>=${version}_${revision}"
			;;
	esac
	nostrip=yes
	pkg_install() {
		_pats=(
			# Airoha Ethernet PHY
			airoha
			# Amlogic bluetooth
			"amlogic/aml_w*_bt_uart.bin*"
			# Atheros WLAN/bluetooth
			"ath*" "ar*" "carl9170*" "htc*"
			# Atmel WLAN
			atmel
			# ATUSB transceiver
			atusb
			# BNX2 Ethernet
			"bnx2*"
			# Chelsio Ethernet
			"cxgb*"
			# ST-E WLAN
			"wsm_22.bin*"
			# Intel WLAN/Ethernet/bluetooth
			e100 "hfi1_*.fw*" "intel*" "iwlwifi*"
			# Orinoco WLAN
			"agere*"
			# IXP4xx
			ixp4xx
			# KL5KUSB101 Ethernet
			kaweth
			# Cavium LiquidIO NIC
			liquidio
			# Mediatek WLAN/bluetooth
			"mt7*.bin*"
			# Mellanox ethernet
			mellanox
			# Marvell WLAN/bluetooth
			mrvl "lbtf_usb.bin*" libertas mwl8k
			# Multi-Tech modem
			"mts_*.fw*"
			# Myricom ethernet
			"myri10ge*"
			# NXP UWB/bluetooth
			"nxp/sr150_fw.bin*" "nxp/helper_uart*.bin*" "nxp/uartiw*.bin*" "nxp/uartspi*.se*" "nxp/uartuart*"
			# NXP WLAN
			"nxp/sd_w*"
			# Qualcomm WLAN/bluetooth
			qca "wil6210*"
			# QLogic HBA
			"cbfw*.bin*" "ct2fw*.bin*" "ctfw*.bin*" "qed" "ql2*.bin*" "qlogic" "phanfw.bin*"
			# Ralink WLAN
			"rt*.bin*"
			# Redpine WLAN/bluetooth
			"rsi*"
			# Realtek bluetooth/ethernet/WLAN
			"rtl*" "rtw*"
			# Sagrad WLAN
			"sdd_sagrad_*.bin*"
			# Slicoss NICs
			slicoss
			# Adaptec ethernet
			"adaptec/starfire*"
			# Tehuti NICs
			tehuti
			# TI WLAN/bluetooth
			ti-connectivity
			# Tigon ethernet
			tigon
			# 3Com ethernet
			3com
			# Eagle USB modem
			ueagle-atm
			# Via USB WLAN
			"vntwusb.fw*"
			# Microchip Ethernet
			"microchip/mscc_vsc85*.bin*"
			# Exar Ethernet
			vxge
			# SiLabs WLAN
			wfx
		)

		for pat in "${_pats[@]}"; do
			vmove "usr/lib/firmware/$pat"
		done

		# don't move mediatek SoC firmware
		( shopt -s extglob; vmove "usr/lib/firmware/mediatek/!(mt7981*|mt7986*|mt81*|sof*)" )
	}
}

linux-firmware-qualcomm_package() {
	short_desc+=" - Qualcomm SoC blobs"
	nostrip=yes
	pkg_install() {
		vmove usr/lib/firmware/qcom
	}
}
