Skip to contentSkip to conversation

environment: support DT-compatible-specific overlays

PATCH In review Ahmad Fatoum 2 participants 3 messages Apr 13

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 environment: support DT-compatible-specific overlays

Board-specific environments are registered from board code via defaultenv_append_directory(), but this isn't possible when using CONFIG_DEFAULT_ENVIRONMENT_PATH with an external environment.

Add support for a match.of_compatible/ directory convention within the default environment. Subdirectories named after DT root compatible strings are overlaid onto /env from most generic to most specific after the default environment is loaded. The match.of_compatible/ directory is removed afterwards to keep /env clean.

Example layout in CONFIG_DEFAULT_ENVIRONMENT_PATH:

barebox-defaultenv/

    nv/boot.default
    match.of_compatible/
      fsl,imx8mm/
        nv/boot.default
      fsl,imx8mq-evk/
        nv/boot.default

For compatible = "fsl,imx8mm-evk", "fsl,imx8mm":
1. fsl,imx8mm/ overlay applied first (most generic)
2. fsl,imx8mm-evk/ overlay applied second (most specific)

---
 common/startup.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 47 insertions(+)
common/startup.c +47 -0
index 2e2b5f820fe9..e630033e3505 100644
@@ -46,6 +46,7 @@
 #include <pbl/handoff-data.h>
 #include <libfile.h>
 #include <fuzz.h>
+#include <of.h>
 
 extern initcall_t __barebox_initcalls_start[], __barebox_early_initcalls_end[],
 		  __barebox_initcalls_end[];
@@ -95,6 +96,50 @@ void autoload_external_env(bool endis)
 }
 #endif
 
+static void env_merge_of_compat_overlays(const char *dir)
+{
+	struct device_node *root;
+	const char *compat;
+	struct stat s;
+	char *ofdir, *src;
+	int count;
+
+	root = of_get_root_node();
+	if (!root)
+		return;
+
+	ofdir = xasprintf("%s/match.of_compatible", dir);
+
+	if (stat(ofdir, &s) || !S_ISDIR(s.st_mode))
+		goto out;
+
+	count = of_property_count_strings(root, "compatible");
+
+	/*
+	 * Apply overlays from most generic to most specific, so that
+	 * more specific entries take precedence.
+	 */
+	for (int i = count - 1; i >= 0; i--) {
+		if (of_property_read_string_index(root, "compatible",
+						  i, &compat))
+			continue;
+
+		src = xasprintf("%s/%s", ofdir, compat);
+
+		if (stat(src, &s) == 0 && S_ISDIR(s.st_mode)) {
+			pr_debug("Applying '%s'-compatible environment overlay\n",
+				 compat);
+			copy_recursive(src, dir, 0);
+		}
+
+		free(src);
+	}
+
+	unlink_recursive(ofdir, NULL);
+out:
+	free(ofdir);
+}
+
 static int load_environment(void)
 {
 	const char *default_environment_path;
@@ -115,6 +160,8 @@ static int load_environment(void)
 		pr_info("external environment support %s. Using default environment\n",
 			IS_ENABLED(CONFIG_ENV_HANDLING) ? "disallowed" : "disabled");
 
+	env_merge_of_compat_overlays("/env");
+
 	nvvar_load();
 
 	return 0;
Signature · 1 line
2.47.3
#1 lore raw

PATCH 2/2 sandbox: add a match.of_compatible directory to exercise the feature

The match.of_compatible feature is mostly meant to be used with environments originating outside barebox.

Nevertheless, switch over sandbox to use it so it gets some coverage.

---
 arch/sandbox/board/defaultenv-sandbox/data/last-match           | 1 +
 .../match.of_compatible/barebox,not-sandbox/data/last-match     | 1 +
 .../match.of_compatible/barebox,not-sandbox/nv/autoboot         | 1 +
 .../match.of_compatible/barebox,sandbox/data/last-match         | 1 +
 .../match.of_compatible/barebox,sandbox/nv/autoboot             | 1 +
 arch/sandbox/board/defaultenv-sandbox/nv/autoboot               | 2 +-
 6 files changed, 6 insertions(+), 1 deletion(-)
 create mode 100644 arch/sandbox/board/defaultenv-sandbox/data/last-match
 create mode 100644 arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,not-sandbox/data/last-match
 create mode 100644 arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,not-sandbox/nv/autoboot
 create mode 100644 arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,sandbox/data/last-match
 create mode 100644 arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,sandbox/nv/autoboot
arch/sandbox/board/defaultenv-sandbox/data/last-match new file +1 -0
new file mode 100644
index 000000000000..df967b96a579
@@ -0,0 +1 @@
+base
arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,not-sandbox/data/last-match new file +1 -0
new file mode 100644
index 000000000000..b175a503912d
@@ -0,0 +1 @@
+barebox,not-sandbox
arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,not-sandbox/nv/autoboot new file +1 -0
new file mode 100644
index 000000000000..36ef4c374d25
@@ -0,0 +1 @@
+boot
arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,sandbox/data/last-match new file +1 -0
new file mode 100644
index 000000000000..80fdf1459eee
@@ -0,0 +1 @@
+barebox,sandbox
arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,sandbox/nv/autoboot new file +1 -0
new file mode 100644
index 000000000000..f7d13fd04662
@@ -0,0 +1 @@
+abort
arch/sandbox/board/defaultenv-sandbox/nv/autoboot +1 -1
index f7d13fd04662..36ef4c374d25 100644
@@ -1 +1 @@
-abort
+boot
Signature · 1 line
2.47.3
#2 lore raw
In reply to Ahmad Fatoum

Hi Ahmad,

Quoted 9 lines · Ahmad Fatoum

Board-specific environments are registered from board code via defaultenv_append_directory(), but this isn't possible when using CONFIG_DEFAULT_ENVIRONMENT_PATH with an external environment.

Add support for a match.of_compatible/ directory convention within the default environment. Subdirectories named after DT root compatible strings are overlaid onto /env from most generic to most specific after the default environment is loaded. The match.of_compatible/ directory is removed afterwards to keep /env clean.

That's a nice feature!

Quoted 8 lines

Example layout in CONFIG_DEFAULT_ENVIRONMENT_PATH:

barebox-defaultenv/

    nv/boot.default
    match.of_compatible/
      fsl,imx8mm/
        nv/boot.default
      fsl,imx8mq-evk/
		^
        fsl,imx8mm-evk/ ?
Quoted 9 lines

nv/boot.default

For compatible = "fsl,imx8mm-evk", "fsl,imx8mm":
1. fsl,imx8mm/ overlay applied first (most generic)
2. fsl,imx8mm-evk/ overlay applied second (most specific)

---
 common/startup.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++

Can we have a Documentation update for this mechanism please incl. the search order? :)

Regards,
Marco

Quoted 79 lines
 1 file changed, 47 insertions(+)
common/startup.c +47 -0
index 2e2b5f820fe9..e630033e3505 100644
@@ -46,6 +46,7 @@
 #include <pbl/handoff-data.h>
 #include <libfile.h>
 #include <fuzz.h>
+#include <of.h>
 
 extern initcall_t __barebox_initcalls_start[], __barebox_early_initcalls_end[],
 		  __barebox_initcalls_end[];
@@ -95,6 +96,50 @@ void autoload_external_env(bool endis)
 }
 #endif
 
+static void env_merge_of_compat_overlays(const char *dir)
+{
+	struct device_node *root;
+	const char *compat;
+	struct stat s;
+	char *ofdir, *src;
+	int count;
+
+	root = of_get_root_node();
+	if (!root)
+		return;
+
+	ofdir = xasprintf("%s/match.of_compatible", dir);
+
+	if (stat(ofdir, &s) || !S_ISDIR(s.st_mode))
+		goto out;
+
+	count = of_property_count_strings(root, "compatible");
+
+	/*
+	 * Apply overlays from most generic to most specific, so that
+	 * more specific entries take precedence.
+	 */
+	for (int i = count - 1; i >= 0; i--) {
+		if (of_property_read_string_index(root, "compatible",
+						  i, &compat))
+			continue;
+
+		src = xasprintf("%s/%s", ofdir, compat);
+
+		if (stat(src, &s) == 0 && S_ISDIR(s.st_mode)) {
+			pr_debug("Applying '%s'-compatible environment overlay\n",
+				 compat);
+			copy_recursive(src, dir, 0);
+		}
+
+		free(src);
+	}
+
+	unlink_recursive(ofdir, NULL);
+out:
+	free(ofdir);
+}
+
 static int load_environment(void)
 {
 	const char *default_environment_path;
@@ -115,6 +160,8 @@ static int load_environment(void)
 		pr_info("external environment support %s. Using default environment\n",
 			IS_ENABLED(CONFIG_ENV_HANDLING) ? "disallowed" : "disabled");
 
+	env_merge_of_compat_overlays("/env");
+
 	nvvar_load();
 
 	return 0;
Signature · 1 line
2.47.3
Signature · 7 lines
#gernperDu 
#CallMeByMyFirstName

Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | https://www.pengutronix.de/ |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-9    |