[RFC] parser: allow selecting the namespace of bootenv variables

Message ID 20261108133746.2484081-1-dario.binacchi@amarulasolutions.com
State New
Headers
Series [RFC] parser: allow selecting the namespace of bootenv variables |

Commit Message

Dario Binacchi Nov. 8, 2026, 1:37 p.m. UTC
All the variables of the bootenv section go to the bootloader
environment, although libubootenv handles other namespaces. The Arm FWU
metadata is one of them, containing the variables that select the boot
bank.

Add the optional "namespace" attribute to the entries of bootenv.
Existing sw-description files are not affected, and entries specifying
a namespace are stored even with the "none" bootloader.

Link: https://groups.google.com/g/swupdate/c/kYr2Hy-f3us
Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---
This needs the FWU metadata storage format of libubootenv, sent as
"[libubootenv][RFC PATCH 0/4] Add the FWU metadata storage format". It
follows the discussion in [1].

Tested on the STM32MP257F-EV1 with TF-A and U-Boot: update of either
bank, acceptance at boot, rollback by TF-A after failed trial boots.
Unknown namespaces, rejected variables and the "none" bootloader were
also tested on the host.

[1] https://groups.google.com/g/swupdate/c/kYr2Hy-f3us

 core/installer.c              | 39 +++++++++++++++++++++++++++
 core/swupdate.c               |  1 +
 doc/source/sw-description.rst | 33 +++++++++++++++++++++++
 include/swupdate.h            | 10 +++++++
 parser/parser.c               | 51 ++++++++++++++++++++++++++++++++---
 5 files changed, 130 insertions(+), 4 deletions(-)

-- 
2.43.0
  

Patch

diff --git a/core/installer.c b/core/installer.c
index d4f08b9fb02a..f0da6c2e6cd9 100644
--- a/core/installer.c
+++ b/core/installer.c
@@ -260,6 +260,25 @@  static int update_swupdate_vars(struct swupdate_cfg *cfg, const char *script)
 	return ret;
 }
 
+static int update_bootenv_namespaces(struct swupdate_cfg *cfg, const char *script)
+{
+	struct bootenv_namespace *ns;
+	int ret;
+
+	LIST_FOREACH(ns, &cfg->bootenv_namespaces, next) {
+		ret = prepare_var_script(&ns->vars, script);
+		if (ret)
+			return ret;
+
+		if ((ret = swupdate_vars_apply_list(script, ns->name)) < 0) {
+			ERROR("Error %d updating namespace %s", ret, ns->name);
+			return ret;
+		}
+	}
+
+	return 0;
+}
+
 int run_prepost_scripts(struct imglist *list, script_fn type)
 {
 	int ret;
@@ -524,6 +543,17 @@  int install_images(struct swupdate_cfg *sw)
 		}
 	}
 
+	/*
+	 * Stored last, so that a namespace is updated only if the bootloader
+	 * environment has been stored
+	 */
+	if (!LIST_EMPTY(&sw->bootenv_namespaces)) {
+		ret = update_bootenv_namespaces(sw, script);
+		if (ret) {
+			return ret;
+		}
+	}
+
 	/*
 	 * Should we generate a list with installed software?
 	 */
@@ -573,6 +603,8 @@  void cleanup_files(struct swupdate_cfg *software) {
 	struct img_type *img_tmp;
 	struct hw_type *hw;
 	struct hw_type *hw_tmp;
+	struct bootenv_namespace *ns;
+	struct bootenv_namespace *ns_tmp;
 	const char* TMPDIR = get_tmpdir();
 	struct imglist *list[] = {&software->scripts};
 
@@ -604,6 +636,13 @@  void cleanup_files(struct swupdate_cfg *software) {
 	dict_drop_db(&software->bootloader);
 	dict_drop_db(&software->vars);
 
+	LIST_FOREACH_SAFE(ns, &software->bootenv_namespaces, next, ns_tmp) {
+		LIST_REMOVE(ns, next);
+		dict_drop_db(&ns->vars);
+		free(ns->name);
+		free(ns);
+	}
+
 	/*
 	 * Drop per update URLs
 	 */
diff --git a/core/swupdate.c b/core/swupdate.c
index d11faba2458c..e7112cb1683b 100644
--- a/core/swupdate.c
+++ b/core/swupdate.c
@@ -304,6 +304,7 @@  static void swupdate_init(struct swupdate_cfg *sw)
 	LIST_INIT(&sw->hardware);
 	LIST_INIT(&sw->scripts);
 	LIST_INIT(&sw->bootloader);
+	LIST_INIT(&sw->bootenv_namespaces);
 	LIST_INIT(&sw->extprocs);
 	LIST_INIT(&sw->swupdate_types);
 	LIST_INIT(&sw->external_urls);
diff --git a/doc/source/sw-description.rst b/doc/source/sw-description.rst
index 6933f4c7a672..a9204382dd31 100644
--- a/doc/source/sw-description.rst
+++ b/doc/source/sw-description.rst
@@ -1015,6 +1015,39 @@  that must be changed:
 SWUpdate will internally generate a script that will be passed to the
 bootloader handler for adjusting the environment.
 
+The optional attribute *namespace* stores a variable in another namespace of
+the `libubootenv` configuration instead of the bootloader environment, for
+example in the Arm FWU metadata:
+
+::
+
+	bootenv: (
+		{
+			name = "bootcount";
+			value = "0";
+		},
+		{
+			name = "active_index";
+			value = "1";
+			namespace = "fwu";
+		},
+		{
+			name = "previous_active_index";
+			value = "0";
+			namespace = "fwu";
+		},
+		{
+			name = "bank1_state";
+			value = "valid";
+			namespace = "fwu";
+		}
+	)
+
+Each namespace is stored once, after the bootloader environment and in the
+order in which it first appears. The update fails if a namespace or one of its
+variables is rejected. The update state is always stored in the bootloader
+environment. The attribute is not supported by the *bootloader* image type.
+
 For backward compatibility with previously built `.swu` images, the
 "uboot" group name is still supported as an alias. However, its usage
 is deprecated.
diff --git a/include/swupdate.h b/include/swupdate.h
index c1289bfac040..5620a7bcbfe5 100644
--- a/include/swupdate.h
+++ b/include/swupdate.h
@@ -72,6 +72,15 @@  struct swupdate_parms {
 	struct swupdate_type_cfg *type;
 };
 
+/* bootenv variables stored in a libubootenv namespace other than the default */
+struct bootenv_namespace {
+	char *name;
+	struct dict vars;
+	LIST_ENTRY(bootenv_namespace) next;
+};
+
+LIST_HEAD(bootenv_namespaces, bootenv_namespace);
+
 struct swupdate_cfg {
 	char name[SWUPDATE_GENERAL_STRING_SIZE];
 	char description[SWUPDATE_UPDATE_DESCRIPTION_STRING_SIZE];
@@ -109,6 +118,7 @@  struct swupdate_cfg {
 	struct imglist images;
 	struct imglist scripts;
 	struct dict bootloader;
+	struct bootenv_namespaces bootenv_namespaces;
 	struct dict vars;
 	struct dict accepted_set;
 	struct dict external_urls;
diff --git a/parser/parser.c b/parser/parser.c
index a5a6590b9402..c98ac0c048f3 100644
--- a/parser/parser.c
+++ b/parser/parser.c
@@ -834,11 +834,42 @@  static int parse_scripts(parsertype p, void *cfg, struct swupdate_cfg *swcfg, lu
 	return _parse_scripts(p, cfg, setting, nodes, swcfg, L);
 }
 
+static struct dict *get_bootenv_namespace(struct swupdate_cfg *swcfg, const char *name)
+{
+	struct bootenv_namespace *ns;
+
+	/* Entries are read backwards, move it first to keep the file order */
+	LIST_FOREACH(ns, &swcfg->bootenv_namespaces, next) {
+		if (!strcmp(ns->name, name)) {
+			LIST_REMOVE(ns, next);
+			LIST_INSERT_HEAD(&swcfg->bootenv_namespaces, ns, next);
+			return &ns->vars;
+		}
+	}
+
+	ns = calloc(1, sizeof(*ns));
+	if (!ns)
+		return NULL;
+
+	ns->name = strdup(name);
+	if (!ns->name) {
+		free(ns);
+		return NULL;
+	}
+
+	LIST_INIT(&ns->vars);
+	LIST_INSERT_HEAD(&swcfg->bootenv_namespaces, ns, next);
+
+	return &ns->vars;
+}
+
 static int _parse_bootloader(parsertype p, void *cfg, void *setting, const char **nodes, struct swupdate_cfg *swcfg, lua_State *L)
 {
 	void *elem;
 	int count, i, skip, err;
 	struct img_type dummy;
+	char namespace[SWUPDATE_GENERAL_STRING_SIZE];
+	struct dict *vars;
 
 	if (setting == NULL) {
 		return 0;
@@ -875,15 +906,26 @@  static int _parse_bootloader(parsertype p, void *cfg, void *setting, const char
 		 */
 		GET_FIELD_STRING(p, elem, "name", dummy.id.name);
 		GET_FIELD_STRING(p, elem, "value", dummy.id.version);
+		GET_FIELD_STRING_RESET(p, elem, "namespace", namespace);
 		skip = run_embscript(p, elem, &dummy, L, swcfg->embscript);
 		if (skip < 0) {
 			return -1;
 		}
 		if (!skip) {
-			dict_set_value(&swcfg->bootloader, dummy.id.name, dummy.id.version);
-			TRACE("Bootloader var: %s = %s",
+			vars = &swcfg->bootloader;
+			if (strlen(namespace)) {
+				vars = get_bootenv_namespace(swcfg, namespace);
+				if (!vars) {
+					ERROR("No memory for namespace %s", namespace);
+					return -ENOMEM;
+				}
+			}
+			dict_set_value(vars, dummy.id.name, dummy.id.version);
+			TRACE("Bootloader var: %s = %s%s%s",
 				dummy.id.name,
-				dict_get_value(&swcfg->bootloader, dummy.id.name));
+				dict_get_value(vars, dummy.id.name),
+				strlen(namespace) ? ", namespace " : "",
+				namespace);
 		}
 	}
 
@@ -1224,7 +1266,8 @@  static int parser(parsertype p, void *cfg, struct swupdate_cfg *swcfg)
 
 	if (LIST_EMPTY(&swcfg->images) &&
 	    LIST_EMPTY(&swcfg->scripts) &&
-	    LIST_EMPTY(&swcfg->bootloader)) {
+	    LIST_EMPTY(&swcfg->bootloader) &&
+	    LIST_EMPTY(&swcfg->bootenv_namespaces)) {
 		ERROR("Found nothing to install");
 		return -1;
 	}