diff mbox series

[V2] bootloader handler: flag to not override bootenv values

Message ID 20221028100524.131772-1-sbabic@denx.de
State Accepted
Headers show
Series [V2] bootloader handler: flag to not override bootenv values | expand

Commit Message

Stefano Babic Oct. 28, 2022, 10:05 a.m. UTC
The handler sets all variable found in the file, independently if they
were also set in the bootenv section of sw-description. Add a
"nooverride" flag to avoid that variables set in sw-description can be
overwritten.

Add documentation for the handler.

Signed-off-by: Stefano Babic <sbabic@denx.de>
Reported-by: Frieder Schrempf <fschrempf@gmail.com>
---

Changes since V1:
	- make code readable (F. Schrempf)

 doc/source/handlers.rst | 36 ++++++++++++++++++++++++++++++++++++
 handlers/boot_handler.c | 13 +++++++++++++
 2 files changed, 49 insertions(+)
diff mbox series

Patch

diff --git a/doc/source/handlers.rst b/doc/source/handlers.rst
index 67fb2c7..5f686fe 100644
--- a/doc/source/handlers.rst
+++ b/doc/source/handlers.rst
@@ -829,6 +829,42 @@  is called twice.
         }
 
 
+Bootloader handler
+------------------
+
+The bootloader handler allows to set bootloader's environment with a file. The file shold have the format:
+
+::
+
+        # Comments are allowed using the hash char
+
+        varname=value
+
+Empty lines are skipped. This simplifies the update of the whole environment instead of setting each variable inside the
+"bootenv" section in sw-description. The property *nooverride* allows to skip variables that are already set in sw-description. If
+not set, variables set in bootenv are overwritten.
+
+
+::
+
+        images: (
+                {
+                        filename = "uEnv.txt";
+                        type = "bootloader";
+                        properties: {
+                                nooverride = "true";
+                        }
+                }
+        );
+
+        bootenv: (
+        {
+                name = "bootenv01key";
+                value = "SOME VALUE";
+        });
+
+In the example above, bootenv01key is not overwritten by a value in uEnv.txt because the flag "nooverride" is set.
+
 Archive handler
 ---------------
 
diff --git a/handlers/boot_handler.c b/handlers/boot_handler.c
index 73c9116..1324015 100644
--- a/handlers/boot_handler.c
+++ b/handlers/boot_handler.c
@@ -33,6 +33,7 @@  static int install_boot_environment(struct img_type *img,
 	char *buf;
 	char filename[MAX_IMAGE_FNAME];
 	struct stat statbuf;
+	bool no_override = false;	/* do not override variables in bootenv */
 
 	if (snprintf(filename, sizeof(filename), "%s%s", get_tmpdirscripts(),
 		     img->fname) >= (int)sizeof(filename)) {
@@ -75,6 +76,7 @@  static int install_boot_environment(struct img_type *img,
 		return -ENOMEM;
 	}
 
+	no_override = strtobool(dict_get_value(&img->properties, "nooverride"));
 	while (fgets(buf, MAX_BOOT_SCRIPT_LINE_LENGTH, fp)) {
 		char **pair = NULL;
 		unsigned int cnt;
@@ -90,6 +92,17 @@  static int install_boot_environment(struct img_type *img,
 		pair = string_split(buf, '=');
 		cnt = count_string_array((const char **)pair);
 
+		/*
+		 * Skip if a variable was already parsed in the bootenv
+		 * section, in that case it is already present in the dictionary.
+		 * This avoid to override with the value in file
+		 */
+		if (cnt && no_override && dict_get_value(img->bootloader, pair[0])) {
+			TRACE("Variable %s already set, skipping..", pair[0]);
+			free_string_array(pair);
+			continue;
+		}
+
 		switch (cnt) {
 		case 2:
 			TRACE("name = %s value = %s", pair[0], pair[1]);