xref: /freebsd/sys/contrib/dev/rtw89/zzz_fw_ports_fwget.sh (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
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=rtw89
21CHECKFILE=rtw8922a.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#
48# This uses a hack (cpp) to expand some macros for us and parses out the result
49# which is the firmware name with the maximum FW version supported for that
50# firmware (where applicable).
51#
52list_fw()
53{
54	for f in `ls -1 rtw?????.c rtw?????e.c`; do
55
56		l=$(cpp ${f} 2>&1 | awk '/^MODULE_FIRMWARE\(/ { gsub(/"/, ""); gsub("__stringify\\(", ""); gsub("\\);$", ""); gsub("\\)", ""); gsub("^MODULE_FIRMWARE\\(", ""); gsub(" ", ""); printf "%s\n", $0; }' | sort -n | uniq)
57		if test "${l}" == ""; then
58			continue
59		fi
60		lx=""
61		for fx in ${l}; do
62			if test -e ${LFWDIR}/${fx}; then
63				lx="${lx} ${fx}"
64			# else
65			#	printf "NOTICE: no firmware file found for '%s'\n" ${fx} >&2
66			fi
67		done
68
69		# Get a count so we can automatically add \\ apart from the last line.
70		fn=$(echo "${lx}" | wc -w | awk '{ print $1 }')
71
72		# echo "==> ${f} :: ${fn} :: ${lx}"
73
74		if test ${fn} -gt 0; then
75
76			# Ports FLAVOR names are [a-z0-9_].  If needed add more mangling magic here.
77			flav=`echo ${f%%.c} | awk '{ printf "%s", tolower($0); }'`
78
79			echo "FWS ${flav}"
80			echo "DISTFILES_${flav}= \\"
81			for fz in ${lx}; do echo "${fz}"; done | \
82			awk -v fn=$fn -v fwg=${flav} -v drv=${DRIVER} '{
83				if (FNR == fn) { x="" } else { x=" \\" };
84				gsub("^" drv "/", "${FWSUBDIR}/");
85				printf "\t%s${DISTURL_SUFFIX}%s\n", $0, x;
86			}'
87		fi
88
89	done
90}
91
92################################################################################
93#
94# Generate the PORTS file template.
95#
96
97fwsl=$(list_fw | grep ^FWS | awk '{ print $2 }')
98# Get a count so we can automatically add \\ apart from the last line.
99fn=$(echo "${fwsl}" | wc -w | awk '{ print $1 }')
100
101if test ${fn} -gt 0; then
102
103	portsfile=$(mktemp -p /tmp ${DRIVER}-fwport.XXXXXX)
104
105	:> ${portsfile}
106	(
107	echo "FWSUBS= \\"
108	for sz in ${fwsl}; do echo "${sz}"; done | \
109	awk -v fn=$fn '{
110		if (FNR == fn) { x="" } else { x=" \\" };
111		printf "\t%s%s\n", $0, x;
112	}'
113
114	echo
115	list_fw | grep -v ^FWS
116
117	echo
118	echo "DISTFILES_\${FWDRV}= \\"
119	for sz in ${fwsl}; do echo "${sz}"; done | \
120	awk -v fn=$fn '{
121		if (FNR == fn) { x="" } else { x=" \\" };
122		printf "\t${DISTFILES_%s}%s\n", $0, x;
123	}'
124	) >> ${portsfile}
125
126	printf "INFO: wifi-firmware-%s-kmod template at %s\n" ${DRIVER} ${portsfile} >&2
127fi
128
129################################################################################
130#
131# Generate the fwget(8) case pattern table (PCI device ID -> fw port flavor).
132#
133
134fwgetfile=$(mktemp -p /tmp ${DRIVER}-fwget.XXXXXX)
135:> ${fwgetfile}
136
137for f in `ls -1 rtw?????.c rtw?????e.c`; do
138
139	# Ports FLAVOR names are [a-z0-9_].  If needed add more mangling magic here.
140	n=${f%.c};
141	n=${n%e};
142
143	awk -v n=${n} -v drv=${DRIVER} '/PCI_DEVICE\(PCI_VENDOR_ID_REALTEK,/ {
144		gsub(").*", "", $2);
145		printf "\t%s)\taddpkg \"wifi-firmware-%s-kmod-%s\"; return 1 ;;\n",
146		    tolower($2), drv, tolower(n);
147	}' ${f}
148done >> ${fwgetfile}
149
150printf "INFO: fwget pci_network_realtek %s template at %s\n" ${DRIVER} ${fwgetfile} >&2
151
152# end
153