mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] scripts: Add script to extract built-in firmware blobs
@ 2025-01-20 19:04 Guilherme G. Piccoli
  2025-01-24  6:38 ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Guilherme G. Piccoli @ 2025-01-20 19:04 UTC (permalink / raw)
  To: linux-kernel
  Cc: mcgrof, russ.weight, dakr, gregkh, rafael, masahiroy, akpm,
	nathan, nicolas, kernel-dev, kernel, Guilherme G. Piccoli,
	Thadeu Lima de Souza Cascardo

Through the options CONFIG_EXTRA_FIRMWARE{_DIR} one is able to build
a kernel including firmware blobs in a built-in fashion. This is
usually the case of built-in drivers that require some blobs in
order to work properly, for example, like in non-initrd based systems.

Add hereby a script to extract these blobs from a non-stripped vmlinux,
similar to the idea of "extract-ikconfig". The firmware loader interface
saves such built-in blobs as rodata entries, having a field for the FW
name as "_fw_<module_name>_<firmware_name>_bin"; the tool extracts files
named "<module_name>_<firmware_name>" for each rodata firmware entry
detected. It makes use of awk, bash, dd and readelf, pretty standard
tooling for Linux development.

Suggested-and-reviewed-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
Signed-off-by: Guilherme G. Piccoli <gpiccoli@igalia.com>
---
 scripts/extract-fwblobs | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
 create mode 100755 scripts/extract-fwblobs

diff --git a/scripts/extract-fwblobs b/scripts/extract-fwblobs
new file mode 100755
index 000000000000..15e836e47368
--- /dev/null
+++ b/scripts/extract-fwblobs
@@ -0,0 +1,31 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+#
+# -----------------------------------------------------------------------------
+# Extracts the vmlinux built-in firmware blobs - requires a non-stripped image
+# -----------------------------------------------------------------------------
+
+if [ -z "$1" ]; then
+	echo "Must provide a non-stripped vmlinux as argument"
+	exit 1
+fi
+
+read -r RD_ADDR_HEX RD_OFF_HEX <<< "$( readelf -SW "$1" |\
+grep -w rodata | awk '{print "0x"$5" 0x"$6}' )"
+
+FW_SYMS="$(readelf -sW "$1" |\
+awk -n '/fw_end/ { end=$2 ; print name " 0x" start " 0x" end; } { start=$2; name=$8; }')"
+
+while IFS= read -r entry; do
+	read -r FW_NAME FW_ADDR_ST_HEX FW_ADDR_END_HEX <<< "$entry"
+
+	# Notice kernel prepends _fw_ and appends _bin to the FW name
+	# in rodata; hence we hereby filter that out.
+	FW_NAME=${FW_NAME:4:-4}
+
+	FW_OFFSET="$(printf "%d" $((FW_ADDR_ST_HEX - RD_ADDR_HEX + RD_OFF_HEX)))"
+	FW_SIZE="$(printf "%d" $((FW_ADDR_END_HEX - FW_ADDR_ST_HEX)))"
+
+	dd if="$1" of="./${FW_NAME}" bs="${FW_SIZE}" count=1 iflag=skip_bytes skip="${FW_OFFSET}"
+done <<< "${FW_SYMS}"
+
-- 
2.47.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] scripts: Add script to extract built-in firmware blobs
  2025-01-20 19:04 [PATCH] scripts: Add script to extract built-in firmware blobs Guilherme G. Piccoli
@ 2025-01-24  6:38 ` Andrew Morton
  2025-01-24 12:31   ` Guilherme G. Piccoli
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Morton @ 2025-01-24  6:38 UTC (permalink / raw)
  To: Guilherme G. Piccoli
  Cc: linux-kernel, mcgrof, russ.weight, dakr, gregkh, rafael,
	masahiroy, nathan, nicolas, kernel-dev, kernel,
	Thadeu Lima de Souza Cascardo

On Mon, 20 Jan 2025 16:04:26 -0300 "Guilherme G. Piccoli" <gpiccoli@igalia.com> wrote:

> Through the options CONFIG_EXTRA_FIRMWARE{_DIR} one is able to build
> a kernel including firmware blobs in a built-in fashion. This is
> usually the case of built-in drivers that require some blobs in
> order to work properly, for example, like in non-initrd based systems.
> 
> Add hereby a script to extract these blobs from a non-stripped vmlinux,
> similar to the idea of "extract-ikconfig". The firmware loader interface
> saves such built-in blobs as rodata entries, having a field for the FW
> name as "_fw_<module_name>_<firmware_name>_bin"; the tool extracts files
> named "<module_name>_<firmware_name>" for each rodata firmware entry
> detected. It makes use of awk, bash, dd and readelf, pretty standard
> tooling for Linux development.

Please describe the use case(s) for this.  ie, why are you suggesting
this be added to Linux?


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] scripts: Add script to extract built-in firmware blobs
  2025-01-24  6:38 ` Andrew Morton
@ 2025-01-24 12:31   ` Guilherme G. Piccoli
  2025-01-24 23:07     ` Andrew Morton
  0 siblings, 1 reply; 4+ messages in thread
From: Guilherme G. Piccoli @ 2025-01-24 12:31 UTC (permalink / raw)
  To: Andrew Morton
  Cc: linux-kernel, mcgrof, russ.weight, dakr, gregkh, rafael,
	masahiroy, nathan, nicolas, kernel-dev, kernel,
	Thadeu Lima de Souza Cascardo

On 24/01/2025 03:38, Andrew Morton wrote:
> On Mon, 20 Jan 2025 16:04:26 -0300 "Guilherme G. Piccoli" <gpiccoli@igalia.com> wrote:
> 
>> Through the options CONFIG_EXTRA_FIRMWARE{_DIR} one is able to build
>> a kernel including firmware blobs in a built-in fashion. This is
>> usually the case of built-in drivers that require some blobs in
>> order to work properly, for example, like in non-initrd based systems.
>>
>> Add hereby a script to extract these blobs from a non-stripped vmlinux,
>> similar to the idea of "extract-ikconfig". The firmware loader interface
>> saves such built-in blobs as rodata entries, having a field for the FW
>> name as "_fw_<module_name>_<firmware_name>_bin"; the tool extracts files
>> named "<module_name>_<firmware_name>" for each rodata firmware entry
>> detected. It makes use of awk, bash, dd and readelf, pretty standard
>> tooling for Linux development.
> 
> Please describe the use case(s) for this.  ie, why are you suggesting
> this be added to Linux?
> 

Hi Andrew, there is currently no tool to extract a firmware blob that is
built-in on vmlinux to the best of my knowledge. So if we have a kernel
image containing the blobs, and we want to rebuild the kernel with some
debug patches for example (and given that the image also has
IKCONFIG=y), we currently can't do that for the same versions for all
the firmware blobs, _unless_ we have exact commits of linux-firmware for
the specific versions for each firmware included.

With this tool, we can blindly extract the FWs and easily re-add them in
the new debug kernel build, allowing a more deterministic testing
without the burden of "hunting down" the proper version of each firmware
binary.

Makes sense to you?
Cheers,


Guilherme

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] scripts: Add script to extract built-in firmware blobs
  2025-01-24 12:31   ` Guilherme G. Piccoli
@ 2025-01-24 23:07     ` Andrew Morton
  0 siblings, 0 replies; 4+ messages in thread
From: Andrew Morton @ 2025-01-24 23:07 UTC (permalink / raw)
  To: Guilherme G. Piccoli
  Cc: linux-kernel, mcgrof, russ.weight, dakr, gregkh, rafael,
	masahiroy, nathan, nicolas, kernel-dev, kernel,
	Thadeu Lima de Souza Cascardo

On Fri, 24 Jan 2025 09:31:07 -0300 "Guilherme G. Piccoli" <gpiccoli@igalia.com> wrote:

> On 24/01/2025 03:38, Andrew Morton wrote:
> > On Mon, 20 Jan 2025 16:04:26 -0300 "Guilherme G. Piccoli" <gpiccoli@igalia.com> wrote:
> > 
> >> Through the options CONFIG_EXTRA_FIRMWARE{_DIR} one is able to build
> >> a kernel including firmware blobs in a built-in fashion. This is
> >> usually the case of built-in drivers that require some blobs in
> >> order to work properly, for example, like in non-initrd based systems.
> >>
> >> Add hereby a script to extract these blobs from a non-stripped vmlinux,
> >> similar to the idea of "extract-ikconfig". The firmware loader interface
> >> saves such built-in blobs as rodata entries, having a field for the FW
> >> name as "_fw_<module_name>_<firmware_name>_bin"; the tool extracts files
> >> named "<module_name>_<firmware_name>" for each rodata firmware entry
> >> detected. It makes use of awk, bash, dd and readelf, pretty standard
> >> tooling for Linux development.
> > 
> > Please describe the use case(s) for this.  ie, why are you suggesting
> > this be added to Linux?
> > 
> 
> Hi Andrew, there is currently no tool to extract a firmware blob that is
> built-in on vmlinux to the best of my knowledge. So if we have a kernel
> image containing the blobs, and we want to rebuild the kernel with some
> debug patches for example (and given that the image also has
> IKCONFIG=y), we currently can't do that for the same versions for all
> the firmware blobs, _unless_ we have exact commits of linux-firmware for
> the specific versions for each firmware included.
> 
> With this tool, we can blindly extract the FWs and easily re-add them in
> the new debug kernel build, allowing a more deterministic testing
> without the burden of "hunting down" the proper version of each firmware
> binary.
> 
> Makes sense to you?

OK, thanks.  I (badly) pasted the above into the changelog.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-01-24 23:07 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-01-20 19:04 [PATCH] scripts: Add script to extract built-in firmware blobs Guilherme G. Piccoli
2025-01-24  6:38 ` Andrew Morton
2025-01-24 12:31   ` Guilherme G. Piccoli
2025-01-24 23:07     ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®