[4/5] tools: Add dumpfwumdata tool for FWU metadata image

Message ID 20251228151824.25667-5-dario.binacchi@amarulasolutions.com
State New
Headers show
Series
  • Add dumpfwumdata tool for FWU metadata image
Related show

Commit Message

Dario Binacchi Dec. 28, 2025, 3:17 p.m. UTC
Allow reading FWU metadata from userspace.

According to [1], after updating the software on bank B, setting
active_index to point to it (i. e. 1) and marking it as a Valid bank, it
must be possible to read this information from userspace on the next
boot to verify whether the boot chain of bank B succeeded.

The metadata must then be updated again, marking the bank as INVALID if
the active_index points to bank A (i. e. 0) or as VALID if the boot was
successful.

To allow reading the active_index and bank state, this new tool has been
added.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---

 tools/Kconfig        |   7 +++
 tools/Makefile       |   4 ++
 tools/dumpfwumdata.c | 128 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 139 insertions(+)
 create mode 100644 tools/dumpfwumdata.c

Patch

diff --git a/tools/Kconfig b/tools/Kconfig
index 652b0f225579..9de7eed1bbbb 100644
--- a/tools/Kconfig
+++ b/tools/Kconfig
@@ -194,6 +194,13 @@  config LUT_SEQUENCE
 	help
 	  Look Up Table Sequence
 
+config TOOLS_DUMPFWUMDATA
+	bool "Build dumpfwumdata command"
+	default y if FWU_MULTI_BANK_UPDATE
+	help
+	  This command allows users to read and display the raw FWU
+	  metadata. The exact access method depends on the platform.
+
 config TOOLS_MKFWUMDATA
 	bool "Build mkfwumdata command"
 	default y if FWU_MULTI_BANK_UPDATE
diff --git a/tools/Makefile b/tools/Makefile
index ae6a30526466..bc105a086927 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -271,6 +271,10 @@  mkeficapsule-objs := generated/lib/uuid.o \
 	mkeficapsule.o
 hostprogs-$(CONFIG_TOOLS_MKEFICAPSULE) += mkeficapsule
 
+dumpfwumdata-objs := dumpfwumdata.o generated/lib/crc32.o
+HOSTLDLIBS_dumpfwumdata += -luuid
+hostprogs-$(CONFIG_TOOLS_DUMPFWUMDATA) += dumpfwumdata
+
 mkfwumdata-objs := mkfwumdata.o generated/lib/crc32.o
 HOSTLDLIBS_mkfwumdata += -luuid
 hostprogs-$(CONFIG_TOOLS_MKFWUMDATA) += mkfwumdata
diff --git a/tools/dumpfwumdata.c b/tools/dumpfwumdata.c
new file mode 100644
index 000000000000..4965484df1be
--- /dev/null
+++ b/tools/dumpfwumdata.c
@@ -0,0 +1,128 @@ 
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Copyright (c) 2025, Amarula Solutions
+ */
+
+#include <errno.h>
+#include <getopt.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <generated/autoconf.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <u-boot/crc.h>
+#include <uuid/uuid.h>
+
+typedef uint8_t u8;
+typedef int16_t s16;
+typedef uint16_t u16;
+typedef uint32_t u32;
+typedef uint64_t u64;
+
+#undef CONFIG_FWU_NUM_BANKS
+#undef CONFIG_FWU_NUM_IMAGES_PER_BANK
+
+/* This will dynamically allocate the fwu_mdata */
+#define CONFIG_FWU_NUM_BANKS		0
+#define CONFIG_FWU_NUM_IMAGES_PER_BANK	0
+
+#include <fwu_mdata.h>
+
+static const char *opts_short = "h";
+
+static struct option options[] = {
+	{"help", no_argument, NULL, 'h'},
+	{NULL, 0, NULL, 0},
+};
+
+static void print_usage(void)
+{
+	fprintf(stderr, "Usage: dumpfwumdata [options] <fwumdata file>\n");
+	fprintf(stderr, "Options:\n"
+		"\t-h, --help                  print a help message\n"
+		);
+}
+
+static void print_mdata(struct fwu_mdata *data)
+{
+	int i;
+
+	fprintf(stdout, "FWU Metadata\n");
+	fprintf(stdout, "crc32: %#x\n", data->crc32);
+	fprintf(stdout, "version: %#x\n", data->version);
+	fprintf(stdout, "active_index: %#x\n", data->active_index);
+	fprintf(stdout, "previous_active_index: %#x\n",
+		data->previous_active_index);
+
+	if (data->version == 2) {
+		uint8_t state;
+		char cstate;
+		for (i = 0; i < 4; i++) {
+			state = data->bank_state[i];
+			if (state == FWU_BANK_ACCEPTED)
+				cstate = 'A';
+			else if (state == FWU_BANK_VALID)
+				cstate = 'V';
+			else if (state == FWU_BANK_INVALID)
+				cstate = 'I';
+			else
+				cstate = '?';
+
+			printf("bank_state[%d]: %c\n", i, cstate);
+		}
+	}
+}
+
+static int fwu_read_mdata(struct fwu_mdata *mdata, const char *mdata_file)
+{
+	FILE *mfile = NULL;
+	size_t ret, size = sizeof(struct fwu_mdata);
+
+	mfile = fopen(mdata_file, "r");
+	if (!mfile) {
+		fprintf(stderr, "Error: Failed to open %s\n",
+			mdata_file);
+		return -1;
+	}
+
+	ret = fread(mdata, 1, size, mfile);
+	fclose(mfile);
+	if (ret != size) {
+		fprintf(stderr, "Error: Failed to read from %s\n",
+			mdata_file);
+		return -1;
+	}
+
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	int c, ret;
+	struct fwu_mdata mdata;
+
+	if (argc < 3) {
+		print_usage();
+		return -EINVAL;
+	}
+
+	do {
+		c = getopt_long(argc, argv, opts_short, options, NULL);
+		switch (c) {
+		case 'h':
+			print_usage();
+			return 0;
+		}
+	} while (c != -1);
+
+	ret = fwu_read_mdata(&mdata, argv[argc - 1]);
+	if (ret)
+		return ret;
+
+	print_mdata(&mdata);
+	return 0;
+}