[libubootenv,v2,3/8] uboot_env: add a callback to validate a value before it is set

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

Commit Message

Dario Binacchi Nov. 13, 2026, 5:17 p.m. UTC
Add an optional callback to check a value before it is set. This allows
a storage format whose variables accept only some values to report an
invalid one as soon as it is set, instead of when the environment is
stored.

The U-Boot environment does not use it.

No functional change.

Signed-off-by: Dario Binacchi <dario.binacchi@amarulasolutions.com>
---
Changes in v2:
- New patch

 src/uboot_env.c     | 9 +++++++++
 src/uboot_private.h | 3 +++
 2 files changed, 12 insertions(+)

-- 
2.43.0
  

Patch

diff --git a/src/uboot_env.c b/src/uboot_env.c
index 16aa416da20f..1b35d898ee62 100644
--- a/src/uboot_env.c
+++ b/src/uboot_env.c
@@ -1011,6 +1011,15 @@  int libuboot_set_env(struct uboot_ctx *ctx, const char *varname, const char *val
 		if (!entryvarlist)
 			return -EPERM;
 	}
+
+	if (value && storages[ctx->format]->validate) {
+		int ret;
+
+		ret = storages[ctx->format]->validate(ctx, varname, value);
+		if (ret)
+			return ret;
+	}
+
 	return __libuboot_set_env(ctx, varname, value, entryvarlist);
 }
 
diff --git a/src/uboot_private.h b/src/uboot_private.h
index 9fd8c5f996c9..52c3fff21c42 100644
--- a/src/uboot_private.h
+++ b/src/uboot_private.h
@@ -212,6 +212,9 @@  struct env_storage {
 	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);
+	/** check a value before it is set, can be NULL */
+	int (*validate)(struct uboot_ctx *ctx, const char *varname,
+			const char *value);
 };
 
 #if defined(__FreeBSD__)