mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 2/2] globalvar: add helpers for stashing global variables
Date: Thu, 12 Mar 2026 15:43:16 +0100	[thread overview]
Message-ID: <20260312144325.2157379-2-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20260312144325.2157379-1-a.fatoum@pengutronix.de>

Configuring a FIT image boot with overlays involves setting not only
global.bootm.* variables, but also global.of.overlay.* variables.

For use in custom boot entry functions, provide functions for stashing
and restoring a set of variables. This allows dry run and boot fallback
behavior to not affect later boot attempts.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/globalvar.c  | 77 +++++++++++++++++++++++++++++++++++++++++++++
 include/globalvar.h |  3 ++
 2 files changed, 80 insertions(+)

diff --git a/common/globalvar.c b/common/globalvar.c
index c2b0d5b4bbad..e168d6055347 100644
--- a/common/globalvar.c
+++ b/common/globalvar.c
@@ -461,6 +461,83 @@ int globalvar_set(const char *name, const char *val)
 	return dev_set_param(&global_device, name, val);
 }
 
+struct globalvar_stashed {
+	const char *key;
+	const char *value;
+	struct list_head list;
+};
+
+static void globalvar_stashed_free(struct globalvar_stashed *elem)
+{
+	list_del(&elem->list);
+	free_const(elem->key);
+	free_const(elem->value);
+	free(elem);
+}
+
+int globalvar_stash_push(struct list_head *stash, ...)
+{
+	struct globalvar_stashed *elem, *safe;
+	const char *name;
+	int ret = -ENOMEM;
+	va_list args;
+	LIST_HEAD(tmp);
+
+	va_start(args, stash);
+
+	while ((name = va_arg(args, const char *))) {
+		const char *value;
+
+		elem = calloc(1, sizeof(*elem));
+		if (!elem)
+			goto out;
+
+		list_add_tail(&elem->list, &tmp);
+
+		elem->key = strdup_const(name);
+		if (!elem->key)
+			goto out;
+
+		value = globalvar_get(name);
+		if (!value) {
+			ret = -EINVAL;
+			goto out;
+		}
+
+		elem->value = strdup_const(value);
+		if (!elem->value)
+			goto out;
+	}
+
+	list_splice_tail(&tmp, stash);
+
+	ret = 0;
+out:
+	va_end(args);
+
+	if (ret) {
+		list_for_each_entry_safe(elem, safe, &tmp, list)
+			globalvar_stashed_free(elem);
+	}
+
+	return ret;
+}
+
+int globalvar_stash_pop(struct list_head *stash)
+{
+	struct globalvar_stashed *elem, *tmp;
+	int err = 0;
+
+	list_for_each_entry_safe(elem, tmp, stash, list) {
+		int ret = globalvar_set(elem->key, elem->value);
+		if (ret)
+			err = ret;
+		globalvar_stashed_free(elem);
+	}
+
+	return err;
+}
+
 static int globalvar_simple_set(struct bobject *bobj, struct param_d *p,
 				const char *val)
 {
diff --git a/include/globalvar.h b/include/globalvar.h
index 413cf72002a0..1d39849ddbab 100644
--- a/include/globalvar.h
+++ b/include/globalvar.h
@@ -24,6 +24,9 @@ char *globalvar_get_match(const char *match, const char *separator);
 void globalvar_set_match(const char *match, const char *val);
 int globalvar_set(const char *name, const char *val);
 
+int globalvar_stash_push(struct list_head *stash, ...) __attribute__((sentinel));
+int globalvar_stash_pop(struct list_head *stash);
+
 int globalvar_add_simple_string(const char *name, char **value);
 int globalvar_add_simple_int(const char *name, int *value,
 			     const char *format);
-- 
2.47.3




  reply	other threads:[~2026-03-12 14:44 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-12 14:43 [PATCH 1/2] globalvar: propagate dev_set_param return value from globalvar_set Ahmad Fatoum
2026-03-12 14:43 ` Ahmad Fatoum [this message]
2026-03-18  8:12   ` [PATCH 2/2] globalvar: add helpers for stashing global variables Sascha Hauer
2026-03-18  9:22     ` Ahmad Fatoum

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=20260312144325.2157379-2-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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