pkgname=firmware-qcom-db410c
pkgver=1036.1
pkgrel=1
pkgdesc="Firmware for DragonBoard 410c"
url="https://releases.linaro.org/96boards/dragonboard410c/qualcomm/firmware/"
subpackages="
	$pkgname-doc
	$pkgname-wcnss-nv:wcnss_nv
	$pkgname-modem
	$pkgname-venus
"
arch="aarch64 armv7"
license="custom"
options="!check !strip !archcheck !tracedeps pmb:cross-native"
makedepends="mtools pil-squasher"
source="https://releases.linaro.org/96boards/dragonboard410c/qualcomm/firmware/linux-board-support-package-r$pkgver.zip"
builddir="$srcdir/linux-board-support-package-r$pkgver"

# This package is a replacement for linux-firmware-qcom (packaging only necessary
# firmware plus some missing ones), so it's packaged in /qcom, not /postmarketos.
replaces="linux-firmware-qcom"
_fwdir="/lib/firmware/qcom"

unpack() {
	default_unpack

	# Extract firmware files from FAT file system image
	mcopy -i "$builddir"/bootloaders-linux/NON-HLOS.bin -p -m -v \
		::image/mba.mbn ::image/modem.* "$builddir"
}

build() {
	pil-squasher modem.mbn modem.mdt
	pil-squasher venus.mbn proprietary-linux/venus.mdt
}

package() {
	install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
	install -Dm644 NOTICE.txt "$pkgdir"/usr/share/licenses/$pkgname/NOTICE
}

modem() {
	pkgdesc="DragonBoard 410c modem firmware"
	install -Dm644 "$builddir"/mba.mbn -t "$subpkgdir/$_fwdir"/apq8016/
	install -Dm644 "$builddir"/modem.mbn -t "$subpkgdir/$_fwdir"/apq8016/
}

# For some reason the wcnss firmware in r1036.1 is actually older than the one
# in r1034.2.1, so firmware-qcom-db410c-wcnss is packaged separately.

wcnss_nv() {
	pkgdesc="DragonBoard 410c WiFi/BT firmware configuration"
	install -Dm644 "$builddir"/proprietary-linux/wlan/prima/WCNSS_qcom_wlan_nv.bin \
		"$subpkgdir/$_fwdir"/apq8016/WCNSS_qcom_wlan_nv_sbc.bin
}

venus() {
	pkgdesc="DragonBoard 410c video firmware"

	# Use this firmware by default for devices that can use unsigned firmware
	provides="firmware-qcom-msm8916-venus"
	provider_priority=100

	# Linux detects squashed (.mbn) vs non-squashed (.mdt + .b*) based on
	# file contents, so it is fine to link venus.mbn to venus.mdt
	# Older kernels look for venus.mdt, newer ones directly for venus.mbn
	install -Dm644 "$builddir"/venus.mbn "$subpkgdir/$_fwdir"/venus-1.8/venus.mbn
	ln -s venus.mbn "$subpkgdir/$_fwdir"/venus-1.8/venus.mdt
}

sha512sums="
99316352669c488b26d93353e0408c0b761809f41b9e13630fdf4de393e36f72477847bd2f8e893e8c98bb94c82a615d3fc4a615db1055df7314269c514bae0e  linux-board-support-package-r1036.1.zip
"
