[libubootenv,RFC,2/4] uboot_env: access the storage through callbacks

Message ID 20261108133615.2483762-3-dario.binacchi@amarulasolutions.com
State New
Headers
Series Add the FWU metadata storage format |

Commit Message

Dario Binacchi Nov. 8, 2026, 1:36 p.m. UTC
Load and store the environment through callbacks selected by the format
of the namespace, with the U-Boot environment as the default. This
allows other storage formats to be added without changing the API.

No functional change.

Suggested-by: Stefano Babic <stefano.babic@swupdate.org>
Link: https://groups.google.com/g/swupdate/c/kYr2Hy-f3us
Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---
 src/uboot_env.c     | 35 ++++++++++++++++++++++++++++-------
 src/uboot_private.h | 27 +++++++++++++++++++++++++++
 2 files changed, 55 insertions(+), 7 deletions(-)

-- 
2.43.0
  

Patch

diff --git a/src/uboot_env.c b/src/uboot_env.c
index d09cb8ead142..16aa416da20f 100644
--- a/src/uboot_env.c
+++ b/src/uboot_env.c
@@ -530,7 +530,7 @@  const struct uboot_version_info *libuboot_version_info(void)
 	return &libinfo;
 }
 
-int libuboot_env_store(struct uboot_ctx *ctx)
+static int uboot_env_store(struct uboot_ctx *ctx, const struct env_helpers *h)
 {
 	struct var_entry *entry;
 	void *image;
@@ -619,7 +619,7 @@  int libuboot_env_store(struct uboot_ctx *ctx)
 	*(uint32_t *)image = crc32(0, (uint8_t *)data, ctx->size - offsetdata);
 
 	copy = ctx->redundant ? (ctx->current ? 0 : 1) : 0;
-	ret = devwrite(ctx, copy, image);
+	ret = h->write(ctx, copy, image);
 	free(image);
 
 	if (ret == ctx->size)
@@ -636,7 +636,7 @@  int libuboot_env_store(struct uboot_ctx *ctx)
 	return ret;
 }
 
-static int libuboot_load(struct uboot_ctx *ctx)
+static int uboot_env_load(struct uboot_ctx *ctx, const struct env_helpers *h)
 {
 	int ret, i;
 	int copies = 1;
@@ -676,7 +676,7 @@  static int libuboot_load(struct uboot_ctx *ctx)
 		uint32_t crc;
 
 		dev = &ctx->envdevs[i];
-		ret = devread(ctx, i, buf[i]);
+		ret = h->read(ctx, i, buf[i]);
 		if (ret != ctx->size) {
 			free(buf[0]);
 			return -EIO;
@@ -761,7 +761,7 @@  static int libuboot_load(struct uboot_ctx *ctx)
 			if (!strcmp(line, ".flags"))
 				flagsvar = strdup(value);
 			else
-				__libuboot_set_env(ctx, line, value, NULL);
+				h->set_env(ctx, line, value, NULL);
 		}
 	}
 
@@ -790,7 +790,7 @@  static int libuboot_load(struct uboot_ctx *ctx)
 			else
 				*pnext++ = '\0';
 
-			entry = __libuboot_get_env(&ctx->varlist, pvar);
+			entry = h->get_env(&ctx->varlist, pvar);
 			set_var_access_type(entry, pval);
 			pvar = pnext;
 		}
@@ -802,6 +802,27 @@  static int libuboot_load(struct uboot_ctx *ctx)
 	return ctx->valid ? 0 : -ENODATA;
 }
 
+static const struct env_helpers helpers = {
+	.read = devread,
+	.write = devwrite,
+	.set_env = __libuboot_set_env,
+	.get_env = __libuboot_get_env,
+};
+
+static const struct env_storage uboot_env_storage = {
+	.load = uboot_env_load,
+	.store = uboot_env_store,
+};
+
+static const struct env_storage *storages[] = {
+	[FORMAT_UBOOT] = &uboot_env_storage,
+};
+
+int libuboot_env_store(struct uboot_ctx *ctx)
+{
+	return storages[ctx->format]->store(ctx, &helpers);
+}
+
 #define LINE_LENGTH 2048
 int libuboot_load_file(struct uboot_ctx *ctx, const char *filename)
 {
@@ -1139,7 +1160,7 @@  int libuboot_open(struct uboot_ctx *ctx) {
 	if (ret < 0)
 		return ret;
 
-	return libuboot_load(ctx);
+	return storages[ctx->format]->load(ctx, &helpers);
 }
 
 void libuboot_close(struct uboot_ctx *ctx) {
diff --git a/src/uboot_private.h b/src/uboot_private.h
index fd6f43197da0..9fd8c5f996c9 100644
--- a/src/uboot_private.h
+++ b/src/uboot_private.h
@@ -85,6 +85,10 @@  enum flags_type {
 	FLAGS_INCREMENTAL
 };
 
+enum env_format {
+	FORMAT_UBOOT,		/* default */
+};
+
 enum device_type {
 	DEVICE_NONE,
 	DEVICE_FILE,
@@ -185,6 +189,29 @@  struct uboot_ctx {
 	int nelem;
 	/** private pointer to list */
 	struct uboot_ctx *ctxlist;
+	/** storage format of the namespace */
+	enum env_format format;
+};
+
+/** Services of the core available to a storage format */
+struct env_helpers {
+	/** read a copy of the storage into data */
+	int (*read)(struct uboot_ctx *ctx, unsigned int copy, void *data);
+	/** write data to a copy of the storage */
+	int (*write)(struct uboot_ctx *ctx, unsigned int copy, void *data);
+	/** set a variable, applying the attributes of validate if not NULL */
+	int (*set_env)(struct uboot_ctx *ctx, const char *varname,
+		       const char *value, struct var_entry *validate);
+	/** look up a variable */
+	struct var_entry *(*get_env)(struct vars *envs, const char *varname);
+};
+
+/** Access to the storage of a namespace */
+struct env_storage {
+	/** read the storage and fill the variable list */
+	int (*load)(struct uboot_ctx *ctx, const struct env_helpers *h);
+	/** write the variable list to the storage */
+	int (*store)(struct uboot_ctx *ctx, const struct env_helpers *h);
 };
 
 #if defined(__FreeBSD__)