xref: /freebsd/sys/contrib/dev/rtw88/zzz_fw_ports_fwget.sh (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1#!/bin/sh
2#-
3# SPDX-License-Identifier: BSD-2-Clause
4#
5# Copyright (c) 2024 The FreeBSD Foundation
6#
7# This software was developed by Björn Zeeb
8# under sponsorship from the FreeBSD Foundation.
9#
10# This is neither efficient nor elegant but we need it few times
11# a year and it does the job.
12#
13#
14# USAGE: please check out the correct tag/hash for ports in the
15# linux-firmware.git repository you point this script to.
16#
17
18set -e
19
20DRIVER=rtw88
21CHECKFILE=rtw8822c.c
22
23################################################################################
24#
25# Check pre-reqs
26#
27if [ $# -ne 1 ]; then
28	printf "USAGE: %s /path/to/linux-firmware.git\n" $0 >&2
29	exit 1
30fi
31
32if [ ! -e ${CHECKFILE} ]; then
33	printf "ERROR: run from %s driver directory; no %s.c here\n" ${DRIVER} ${CHECKFILE} >&2
34	exit 1
35fi
36
37LFWDIR=${1}
38if test ! -d ${LFWDIR} -o ! -e ${LFWDIR}/WHENCE; then
39	printf "ERROR: cannot find linux-firmware.git at '%s'\n" ${LFWDIR} >&2
40	exit 1
41fi
42
43
44################################################################################
45#
46# Helper functions.
47#
48list_fw()
49{
50	for f in `ls -1 rtw?????.c rtw?????e.c`; do
51
52		l=$(awk '/^MODULE_FIRMWARE\(/ { gsub(/"/, ""); gsub("\\);$", ""); gsub("^MODULE_FIRMWARE\\(", ""); printf "%s\n", $0; }' ${f} | sort -n | uniq)
53		lx=""
54		for fx in ${l}; do
55			if test -e ${LFWDIR}/${fx}; then
56				lx="${lx} ${fx}"
57			# else
58			#	printf "NOTICE: no firmware file found for '%s'\n" ${fx} >&2
59			fi
60		done
61
62		# Get a count so we can automatically add \\ apart from the last line.
63		fn=$(echo "${lx}" | wc -w | awk '{ print $1 }')
64
65		# echo "==> ${f} :: ${fn} :: ${lx}"
66
67		if test ${fn} -gt 0; then
68
69			# Ports FLAVOR names are [a-z0-9_].  If needed add more mangling magic here.
70			flav=`echo ${f%%.c} | awk '{ printf "%s", tolower($0); }'`
71
72			echo "FWS ${flav}"
73			echo "DISTFILES_${flav}= \\"
74			for fz in ${lx}; do echo "${fz}"; done | \
75			awk -v fn=$fn -v fwg=${flav} -v drv=${DRIVER} '{
76				if (FNR == fn) { x="" } else { x=" \\" };
77				gsub("^" drv "/", "${FWSUBDIR}/");
78				printf "\t%s${DISTURL_SUFFIX}%s\n", $0, x;
79			}'
80		fi
81
82	done
83}
84
85################################################################################
86#
87# Generate the PORTS file template.
88#
89
90fwsl=$(list_fw | grep ^FWS | awk '{ print $2 }')
91# Get a count so we can automatically add \\ apart from the last line.
92fn=$(echo "${fwsl}" | wc -w | awk '{ print $1 }')
93
94if test ${fn} -gt 0; then
95
96	portsfile=$(mktemp -p /tmp ${DRIVER}-fwport.XXXXXX)
97
98	:> ${portsfile}
99	(
100	echo "FWSUBS= \\"
101	for sz in ${fwsl}; do echo "${sz}"; done | \
102	awk -v fn=$fn '{
103		if (FNR == fn) { x="" } else { x=" \\" };
104		printf "\t%s%s\n", $0, x;
105	}'
106
107	echo
108	list_fw | grep -v ^FWS
109
110	echo
111	echo "DISTFILES_\${FWDRV}= \\"
112	for sz in ${fwsl}; do echo "${sz}"; done | \
113	awk -v fn=$fn '{
114		if (FNR == fn) { x="" } else { x=" \\" };
115		printf "\t${DISTFILES_%s}%s\n", $0, x;
116	}'
117	) >> ${portsfile}
118
119	printf "INFO: wifi-firmware-%s-kmod template at %s\n" ${DRIVER} ${portsfile} >&2
120fi
121
122################################################################################
123#
124# Generate the fwget(8) case pattern table (PCI device ID -> fw port flavor).
125#
126
127fwgetfile=$(mktemp -p /tmp ${DRIVER}-fwget.XXXXXX)
128:> ${fwgetfile}
129
130for f in `ls -1 rtw?????.c rtw?????e.c`; do
131
132	# Ports FLAVOR names are [a-z0-9_].  If needed add more mangling magic here.
133	n=${f%.c};
134	n=${n%e};
135
136	awk -v n=${n} -v drv=${DRIVER} '/PCI_DEVICE\(PCI_VENDOR_ID_REALTEK,/ {
137		gsub(").*", "", $2);
138		printf "\t%s)\taddpkg \"wifi-firmware-%s-kmod-%s\"; return 1 ;;\n",
139		    tolower($2), drv, tolower(n);
140	}' ${f}
141done >> ${fwgetfile}
142
143printf "INFO: fwget pci_network_realtek %s template at %s\n" ${DRIVER} ${fwgetfile} >&2
144
145# end
146