@@ -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
*/
@@ -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
/*
@@ -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);
@@ -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)
@@ -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.
@@ -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;
@@ -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);
@@ -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;
}
@@ -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();