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

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

Commit Message

Dario Binacchi Nov. 13, 2026, 5:19 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. An update with
a namespace not defined in the libubootenv configuration is rejected
before installing anything.

The vars section with namespace-for-vars could set these variables as
well, but with one namespace for all the variables of the update, and
stored before the bootloader environment. The namespaces of bootenv
are stored after it, so the bank switch is the last step of the update.

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][PATCH v2 0/8] Add the FWU metadata storage format". It
follows the discussion in [1].

v1 was 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. v2 was tested on the same board, updating both banks and
rejecting an unknown namespace, and on the host: a missing libubootenv
configuration, the "none" bootloader and the unit tests.

Changes in v2:
- Check that the namespaces are defined in the libubootenv configuration
  before installing anything
- Document where the namespaces are defined, with a link to the
  libubootenv format, and that the bootloader environment is already
  stored when a namespace fails
- Add test cases for entries with one and two namespaces
- Explain in the commit message why bootenv is used instead of vars
- Drop RFC

Link to v1: https://groups.google.com/g/swupdate/c/pzHEhpNxCyU

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

 core/installer.c              | 39 +++++++++++++++++++++++++++
 core/parser.c                 | 20 ++++++++++++++
 core/swupdate.c               |  1 +
 core/swupdate_vars.c          | 13 +++++++++
 doc/source/sw-description.rst | 41 ++++++++++++++++++++++++++++
 include/swupdate.h            | 10 +++++++
 include/swupdate_vars.h       |  1 +
 parser/parser.c               | 51 ++++++++++++++++++++++++++++++++---
 test/test_parser.c            | 51 +++++++++++++++++++++++++++++++++++
 9 files changed, 223 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/parser.c b/core/parser.c
index 74b39d0781a3..1caa46ec923e 100644
--- a/core/parser.c
+++ b/core/parser.c
@@ -17,6 +17,7 @@ 
 #include "progress.h"
 #include "handler.h"
 #include "swupdate_crypto.h"
+#include "swupdate_vars.h"
 
 static parser_fn parsers[] = {
 	parse_cfg,
@@ -211,6 +212,25 @@  int parse(struct swupdate_cfg *sw, const char *descfile)
 		return -EINVAL;
 	}
 
+	/*
+	 * The namespaces are stored after the images are installed, check
+	 * them before installing anything
+	 */
+	const char *fwenv_config = get_fwenv_config();
+	if (!LIST_EMPTY(&sw->bootenv_namespaces) && !fwenv_config) {
+		ERROR("No libubootenv configuration for bootenv namespaces");
+		return -EINVAL;
+	}
+
+	struct bootenv_namespace *ns;
+	LIST_FOREACH(ns, &sw->bootenv_namespaces, next) {
+		if (!swupdate_vars_namespace_exists(ns->name)) {
+			ERROR("Namespace %s not found in %s", ns->name,
+			      fwenv_config);
+			return -EINVAL;
+		}
+	}
+
 #ifdef CONFIG_SIGNED_IMAGES
 
 	/*
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/core/swupdate_vars.c b/core/swupdate_vars.c
index 1762713f2afc..95e0255491da 100644
--- a/core/swupdate_vars.c
+++ b/core/swupdate_vars.c
@@ -182,6 +182,19 @@  int swupdate_vars_apply_list(const char *filename, const char *namespace)
 	return ret;
 }
 
+bool swupdate_vars_namespace_exists(const char *namespace)
+{
+	struct uboot_ctx *ctx = NULL;
+	bool exists = false;
+
+	if (!libuboot_read_config_ext(&ctx, get_fwenv_config()))
+		exists = libuboot_get_namespace(ctx, namespace) != NULL;
+
+	libuboot_exit(ctx);
+
+	return exists;
+}
+
 bool swupdate_set_default_namespace(const char *namespace)
 {
 	if (namespace_default)
diff --git a/doc/source/sw-description.rst b/doc/source/sw-description.rst
index 6933f4c7a672..7f7ea749f4db 100644
--- a/doc/source/sw-description.rst
+++ b/doc/source/sw-description.rst
@@ -1015,6 +1015,47 @@  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";
+		}
+	)
+
+The namespace must be defined in the `libubootenv` configuration, set by
+*fwenv-config-location* in swupdate.cfg. See the `libubootenv documentation
+<https://github.com/sbabic/libubootenv/blob/master/docs/fw_env_config.md>`_
+for its format. An update with a namespace that is not defined is rejected
+before installing anything.
+
+Each namespace is stored once, after the bootloader environment and in the
+order in which it first appears. If a namespace cannot be stored, for example
+because the FWU metadata rejects a value, the update fails but the bootloader
+environment is already stored. The update state is always stored in the
+bootloader environment. Variables set with a file of type *bootloader* always
+go to the bootloader environment.
+
 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/include/swupdate_vars.h b/include/swupdate_vars.h
index 1dcab9729fe8..e816926b0723 100644
--- a/include/swupdate_vars.h
+++ b/include/swupdate_vars.h
@@ -15,4 +15,5 @@  int swupdate_vars_apply_list(const char *filename, const char *namespace);
 char *swupdate_vars_get(const char *name, const char *namespace);
 int swupdate_vars_set(const char *name, const char *value, const char *namespace);
 int swupdate_vars_unset(const char *name, const char *namespace);
+bool swupdate_vars_namespace_exists(const char *namespace);
 bool swupdate_set_default_namespace(const char *namespace);
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;
 	}
diff --git a/test/test_parser.c b/test/test_parser.c
index 20cafde57190..4f0a1f8e89ee 100644
--- a/test/test_parser.c
+++ b/test/test_parser.c
@@ -66,6 +66,7 @@  static void sw_init(void)
 	LIST_INIT(&sw.hardware);
 	LIST_INIT(&sw.scripts);
 	LIST_INIT(&sw.bootloader);
+	LIST_INIT(&sw.bootenv_namespaces);
 	LIST_INIT(&sw.vars);
 	LIST_INIT(&sw.extprocs);
 	LIST_INIT(&sw.swupdate_types);
@@ -625,6 +626,25 @@  static void test_parser_bootenv_vars(void **state)
 		.cfg = "bootenv: ( { value = \"1\"; } );",
 		.json = "\"bootenv\": [ { \"value\": \"1\" } ]",
 	};
+	static const struct swd ns_single = {
+		.cfg = "bootenv: ( { name = \"a\"; value = \"1\"; },"
+		       " { name = \"x\"; value = \"2\"; namespace = \"fwu\"; },"
+		       " { name = \"y\"; namespace = \"fwu\"; } );",
+		.json = "\"bootenv\": [ { \"name\": \"a\", \"value\": \"1\" },"
+			" { \"name\": \"x\", \"value\": \"2\", \"namespace\": \"fwu\" },"
+			" { \"name\": \"y\", \"namespace\": \"fwu\" } ]",
+	};
+	static const struct swd ns_multi = {
+		.cfg = "bootenv: ( { name = \"x\"; value = \"1\"; namespace = \"fwu\"; },"
+		       " { name = \"a\"; value = \"2\"; },"
+		       " { name = \"y\"; value = \"3\"; namespace = \"app\"; },"
+		       " { name = \"z\"; value = \"4\"; namespace = \"fwu\"; } );",
+		.json = "\"bootenv\": [ { \"name\": \"x\", \"value\": \"1\", \"namespace\": \"fwu\" },"
+			" { \"name\": \"a\", \"value\": \"2\" },"
+			" { \"name\": \"y\", \"value\": \"3\", \"namespace\": \"app\" },"
+			" { \"name\": \"z\", \"value\": \"4\", \"namespace\": \"fwu\" } ]",
+	};
+	struct bootenv_namespace *ns;
 
 	FOR_EACH_FORMAT(f) {
 		sw_init();
@@ -637,6 +657,37 @@  static void test_parser_bootenv_vars(void **state)
 		/* a link in vars adds vars, not bootloader variables */
 		assert_dict_value(&sw.vars, "w", "y");
 		assert_null(dict_get_value(&sw.bootloader, "w"));
+		assert_true(LIST_EMPTY(&sw.bootenv_namespaces));
+		sw_free();
+
+		sw_init();
+		assert_int_equal(parse_swd(&ns_single, f), 0);
+		assert_dict_value(&sw.bootloader, "a", "1");
+		assert_null(dict_get_value(&sw.bootloader, "x"));
+		ns = LIST_FIRST(&sw.bootenv_namespaces);
+		assert_non_null(ns);
+		assert_string_equal(ns->name, "fwu");
+		assert_dict_value(&ns->vars, "x", "2");
+		assert_dict_value(&ns->vars, "y", "");
+		assert_null(LIST_NEXT(ns, next));
+		sw_free();
+
+		/* namespaces keep the order in which they first appear */
+		sw_init();
+		assert_int_equal(parse_swd(&ns_multi, f), 0);
+		assert_dict_value(&sw.bootloader, "a", "2");
+		assert_null(dict_get_value(&sw.bootloader, "x"));
+		ns = LIST_FIRST(&sw.bootenv_namespaces);
+		assert_non_null(ns);
+		assert_string_equal(ns->name, "fwu");
+		assert_dict_value(&ns->vars, "x", "1");
+		assert_dict_value(&ns->vars, "z", "4");
+		assert_null(dict_get_value(&ns->vars, "y"));
+		ns = LIST_NEXT(ns, next);
+		assert_non_null(ns);
+		assert_string_equal(ns->name, "app");
+		assert_dict_value(&ns->vars, "y", "3");
+		assert_null(LIST_NEXT(ns, next));
 		sw_free();
 
 		sw_init();