# Template file for 'linux-firmware'
pkgname=linux-firmware
version=20240811
revision=1
hostmakedepends="rdfind which"
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://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/snapshot/linux-firmware-${version}.tar.gz"
checksum=b1c672868e36c19d51f943898d0fdb5534759dc649af72fe51b04be47663d153
python_version=3
nostrip=yes

do_install() {
	make install FIRMWAREDIR=/usr/lib/firmware DESTDIR=$DESTDIR

	for _l in LICEN* WHENCE
	do
		vlicense "$_l"
	done

	# XXX: should some firmwares, like carl9170, be pruned?
}

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 CPU/GPU microcode"
	nostrip=yes
	pkg_install() {
		vmove usr/lib/firmware/i915
		vmove usr/share/licenses/linux-firmware/LICENSE.i915
	}
}

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() {
		vmove usr/lib/firmware/3com
		vmove "usr/lib/firmware/rt*"
		vmove "usr/lib/firmware/ath*"
		vmove "usr/lib/firmware/ar*"
		vmove "usr/lib/firmware/bnx2*"
		vmove "usr/lib/firmware/cxgb*"
		vmove "usr/lib/firmware/htc*"
		vmove "usr/lib/firmware/iwl*"
		vmove "usr/lib/firmware/intel*"
		vmove usr/lib/firmware/kaweth
		vmove usr/lib/firmware/libertas
		vmove usr/lib/firmware/mrvl
		vmove "usr/lib/firmware/mt7*.bin"
		# don't move mediatek SoC firmware
		( shopt -s extglob; vmove "usr/lib/firmware/mediatek/!(mt7981*|mt7986*|mt81*|sof*)" )
		vmove usr/lib/firmware/ueagle-atm
		vmove usr/lib/firmware/ti-connectivity
		vmove usr/lib/firmware/dpaa2
		vmove usr/lib/firmware/qca
	}
}

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