From: "Guilherme G. Piccoli" <gpiccoli@igalia.com>
To: linux-kernel@vger.kernel.org
Cc: mcgrof@kernel.org, russ.weight@linux.dev, dakr@kernel.org,
gregkh@linuxfoundation.org, rafael@kernel.org,
masahiroy@kernel.org, akpm@linux-foundation.org,
nathan@kernel.org, nicolas@fjasle.eu, kernel-dev@igalia.com,
kernel@gpiccoli.net, "Guilherme G. Piccoli" <gpiccoli@igalia.com>,
Thadeu Lima de Souza Cascardo <cascardo@igalia.com>
Subject: [PATCH] scripts: Add script to extract built-in firmware blobs
Date: Mon, 20 Jan 2025 16:04:26 -0300 [thread overview]
Message-ID: <20250120190436.127578-1-gpiccoli@igalia.com> (raw)
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>
---
| 31 +++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+)
create mode 100755 scripts/extract-fwblobs
--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
next reply other threads:[~2025-01-20 19:04 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-01-20 19:04 Guilherme G. Piccoli [this message]
2025-01-24 6:38 ` Andrew Morton
2025-01-24 12:31 ` Guilherme G. Piccoli
2025-01-24 23:07 ` Andrew Morton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250120190436.127578-1-gpiccoli@igalia.com \
--to=gpiccoli@igalia.com \
--cc=akpm@linux-foundation.org \
--cc=cascardo@igalia.com \
--cc=dakr@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=kernel-dev@igalia.com \
--cc=kernel@gpiccoli.net \
--cc=linux-kernel@vger.kernel.org \
--cc=masahiroy@kernel.org \
--cc=mcgrof@kernel.org \
--cc=nathan@kernel.org \
--cc=nicolas@fjasle.eu \
--cc=rafael@kernel.org \
--cc=russ.weight@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®