* [PATCH v2 1/2] environment: support DT-compatible-specific overlays
@ 2026-04-13 13:43 Ahmad Fatoum
2026-04-13 13:43 ` [PATCH v2 2/2] sandbox: add a match.of_compatible directory to exercise the feature Ahmad Fatoum
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2026-04-13 13:43 UTC (permalink / raw)
To: barebox; +Cc: mfe, Ahmad Fatoum
From: Ahmad Fatoum <a.fatoum@barebox.org>
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,imx8mm-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)
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
v1 -> v2:
- Fix typo in commit message (Marco)
- Add documentation for the feature (Marco)
---
Documentation/user/defaultenv-2.rst | 29 ++++++++++++++++++
common/startup.c | 47 +++++++++++++++++++++++++++++
2 files changed, 76 insertions(+)
diff --git a/Documentation/user/defaultenv-2.rst b/Documentation/user/defaultenv-2.rst
index e5cb4a51bfad..2564eb959fcc 100644
--- a/Documentation/user/defaultenv-2.rst
+++ b/Documentation/user/defaultenv-2.rst
@@ -155,3 +155,32 @@ This contains the files to be sourced when barebox detects that the OS
had requested a specific :ref:`reboot_mode` (via e.g. ``reboot bootloader``
under Linux). After the ``/env/init`` scripts were executed, barebox will
``source /env/bmode/${global.system.reboot_mode.prev}`` if available.
+
+/env/mach.of_compatible/
+------------------------
+
+On devicetree enabled systems, this optional directory may contain
+environment overlays that should be applied onto ``/env``.
+
+Each subdirectory should be named after a device tree root compatible
+string and they will be applied from most generic to most specific match
+after the default environment is loaded.
+The ``match.of_compatible/`` directory is removed afterwards to keep
+``/env`` clean.
+
+For example, given ``CONFIG_DEFAULT_ENVIRONMENT_PATH`` pointing
+at an external ``barebox-defaultenv/`` directory::
+
+ barebox-defaultenv/
+ nv/boot.default -> kernel-common
+ match.of_compatible/
+ fsl,imx8mm/
+ nv/boot.default -> kernel-mx8mm
+ fsl,imx8mm-evk/
+ nv/boot.default -> kernel-mx8mm-evk
+
+The final ``/env/nv/boot.default`` would be:
+
+- For ``compatible = "fsl,imx8mm-evk", "fsl,imx8mm";``, ``kernel-mx8mm-evk``
+- For ``compatible = "fsl,imx8mm-evkb", "fsl,imx8mm";``, ``kernel-mx8mm``
+- For ``compatible = "fsl,imx8mp-evk", "fsl,imx8mp";``, ``kernel-common``
diff --git a/common/startup.c b/common/startup.c
index 2e2b5f820fe9..e630033e3505 100644
--- a/common/startup.c
+++ b/common/startup.c
@@ -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;
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2 2/2] sandbox: add a match.of_compatible directory to exercise the feature
2026-04-13 13:43 [PATCH v2 1/2] environment: support DT-compatible-specific overlays Ahmad Fatoum
@ 2026-04-13 13:43 ` Ahmad Fatoum
2026-04-13 14:22 ` [PATCH v2 1/2] environment: support DT-compatible-specific overlays Marco Felsch
2026-04-13 15:10 ` Sascha Hauer
2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2026-04-13 13:43 UTC (permalink / raw)
To: barebox; +Cc: mfe, Ahmad Fatoum
From: Ahmad Fatoum <a.fatoum@barebox.org>
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.
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
v1 -> v2:
- no change
---
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
diff --git a/arch/sandbox/board/defaultenv-sandbox/data/last-match b/arch/sandbox/board/defaultenv-sandbox/data/last-match
new file mode 100644
index 000000000000..df967b96a579
--- /dev/null
+++ b/arch/sandbox/board/defaultenv-sandbox/data/last-match
@@ -0,0 +1 @@
+base
diff --git a/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,not-sandbox/data/last-match b/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,not-sandbox/data/last-match
new file mode 100644
index 000000000000..b175a503912d
--- /dev/null
+++ b/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,not-sandbox/data/last-match
@@ -0,0 +1 @@
+barebox,not-sandbox
diff --git a/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,not-sandbox/nv/autoboot b/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,not-sandbox/nv/autoboot
new file mode 100644
index 000000000000..36ef4c374d25
--- /dev/null
+++ b/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,not-sandbox/nv/autoboot
@@ -0,0 +1 @@
+boot
diff --git a/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,sandbox/data/last-match b/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,sandbox/data/last-match
new file mode 100644
index 000000000000..80fdf1459eee
--- /dev/null
+++ b/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,sandbox/data/last-match
@@ -0,0 +1 @@
+barebox,sandbox
diff --git a/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,sandbox/nv/autoboot b/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,sandbox/nv/autoboot
new file mode 100644
index 000000000000..f7d13fd04662
--- /dev/null
+++ b/arch/sandbox/board/defaultenv-sandbox/match.of_compatible/barebox,sandbox/nv/autoboot
@@ -0,0 +1 @@
+abort
diff --git a/arch/sandbox/board/defaultenv-sandbox/nv/autoboot b/arch/sandbox/board/defaultenv-sandbox/nv/autoboot
index f7d13fd04662..36ef4c374d25 100644
--- a/arch/sandbox/board/defaultenv-sandbox/nv/autoboot
+++ b/arch/sandbox/board/defaultenv-sandbox/nv/autoboot
@@ -1 +1 @@
-abort
+boot
--
2.47.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2 1/2] environment: support DT-compatible-specific overlays
2026-04-13 13:43 [PATCH v2 1/2] environment: support DT-compatible-specific overlays Ahmad Fatoum
2026-04-13 13:43 ` [PATCH v2 2/2] sandbox: add a match.of_compatible directory to exercise the feature Ahmad Fatoum
@ 2026-04-13 14:22 ` Marco Felsch
2026-04-13 15:10 ` Sascha Hauer
2 siblings, 0 replies; 4+ messages in thread
From: Marco Felsch @ 2026-04-13 14:22 UTC (permalink / raw)
To: Ahmad Fatoum; +Cc: barebox, Ahmad Fatoum
On 26-04-13, Ahmad Fatoum wrote:
> From: Ahmad Fatoum <a.fatoum@barebox.org>
>
> 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,imx8mm-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)
>
> Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
Reviewed-by: Marco Felsch <m.felsch@pengutronix.de>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH v2 1/2] environment: support DT-compatible-specific overlays
2026-04-13 13:43 [PATCH v2 1/2] environment: support DT-compatible-specific overlays Ahmad Fatoum
2026-04-13 13:43 ` [PATCH v2 2/2] sandbox: add a match.of_compatible directory to exercise the feature Ahmad Fatoum
2026-04-13 14:22 ` [PATCH v2 1/2] environment: support DT-compatible-specific overlays Marco Felsch
@ 2026-04-13 15:10 ` Sascha Hauer
2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2026-04-13 15:10 UTC (permalink / raw)
To: barebox, Ahmad Fatoum; +Cc: mfe, Ahmad Fatoum
On Mon, 13 Apr 2026 15:43:52 +0200, Ahmad Fatoum wrote:
> 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.
>
> [...]
Applied, thanks!
[1/2] environment: support DT-compatible-specific overlays
https://git.pengutronix.de/cgit/barebox/commit/?id=29bd991c06f4 (link may not be stable)
[2/2] sandbox: add a match.of_compatible directory to exercise the feature
https://git.pengutronix.de/cgit/barebox/commit/?id=b071c63dc663 (link may not be stable)
Best regards,
--
Sascha Hauer <s.hauer@pengutronix.de>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-04-13 15:10 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-04-13 13:43 [PATCH v2 1/2] environment: support DT-compatible-specific overlays Ahmad Fatoum
2026-04-13 13:43 ` [PATCH v2 2/2] sandbox: add a match.of_compatible directory to exercise the feature Ahmad Fatoum
2026-04-13 14:22 ` [PATCH v2 1/2] environment: support DT-compatible-specific overlays Marco Felsch
2026-04-13 15:10 ` Sascha Hauer
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox