From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: "Jan Lübbe" <j.luebbe@pengutronix.de>,
"Ahmad Fatoum" <a.fatoum@pengutronix.de>
Subject: [PATCH 3/3] defaultenv: add base and external defaultenv in pure_initcall
Date: Mon, 24 Nov 2025 18:59:42 +0100 [thread overview]
Message-ID: <20251124175947.1725563-3-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20251124175947.1725563-1-a.fatoum@pengutronix.de>
We currently depend on the order of the calls to defaultenv_append() to
decide which overlays to apply first.
To ensure the base environment is always loaded first, all of
defaultenv_append(), defaultenv_append_runtime_directory() and
defaultenv_load() call defaultenv_add_base() first.
Simplify this by adding base and external (i.e. from the BSP) defaultenv
in the earliest initcall.
This has a welcome side effect: Previously, fw_cfg env was applied
before an external defaultenv, but now it's applied afterwards and thus
can override it.
Reported-by: Jan Lübbe <j.luebbe@pengutronix.de>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
defaultenv/defaultenv.c | 78 +++++++++++++++--------------------------
1 file changed, 28 insertions(+), 50 deletions(-)
diff --git a/defaultenv/defaultenv.c b/defaultenv/defaultenv.c
index 8407a2457630..6891d95e301e 100644
--- a/defaultenv/defaultenv.c
+++ b/defaultenv/defaultenv.c
@@ -33,48 +33,6 @@ struct defaultenv {
size_t size;
};
-static void defaultenv_add_base(void)
-{
- static int base_added;
-
- if (base_added)
- return;
-
- base_added = 1;
-
- if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW))
- defaultenv_append_directory(defaultenv_2_base);
- if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_MENU))
- defaultenv_append_directory(defaultenv_2_menu);
- if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_DFU))
- defaultenv_append_directory(defaultenv_2_dfu);
- if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_REBOOT_MODE))
- defaultenv_append_directory(defaultenv_2_reboot_mode);
- if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_SECURITY_POLICY))
- defaultenv_append_directory(defaultenv_2_security_policy);
- if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_IKCONFIG))
- defaultenv_append_directory(defaultenv_2_ikconfig);
- if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC))
- defaultenv_append_directory(defaultenv_1);
-}
-
-static void defaultenv_add_external(void)
-{
- static int external_added;
-
- if (external_added)
- return;
-
- external_added = 1;
-
- /*
- * The traditional or external environment given with
- * CONFIG_DEFAULT_ENVIRONMENT_PATH.
- */
- defaultenv_append((void *)default_environment,
- default_environment_size, "defaultenv");
-}
-
/*
* defaultenv_append - append a envfs buffer to the default environment
* @buf: The buffer containing the binary environment. If it is
@@ -88,8 +46,6 @@ void defaultenv_append(void *buf, unsigned int size, const char *name)
{
struct defaultenv *df;
- defaultenv_add_base();
-
df = xzalloc(sizeof(*df));
df->buf = buf;
df->size = size;
@@ -106,8 +62,6 @@ void defaultenv_append_runtime_directory(const char *srcdir)
if (stat(srcdir, &s) || !S_ISDIR(s.st_mode))
return;
- defaultenv_add_base();
-
df = xzalloc(sizeof(*df));
df->srcdir = srcdir;
df->name = srcdir;
@@ -179,10 +133,6 @@ int defaultenv_load(const char *dir, unsigned flags)
struct defaultenv *df;
int err = 0;
- defaultenv_add_base();
-
- defaultenv_add_external();
-
list_for_each_entry(df, &defaultenv_list, list) {
int ret = defaultenv_load_one(df, dir, flags);
if (ret)
@@ -191,3 +141,31 @@ int defaultenv_load(const char *dir, unsigned flags)
return err;
}
+
+static int defaultenv_init(void)
+{
+ if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW))
+ defaultenv_append_directory(defaultenv_2_base);
+ if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_MENU))
+ defaultenv_append_directory(defaultenv_2_menu);
+ if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_DFU))
+ defaultenv_append_directory(defaultenv_2_dfu);
+ if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_REBOOT_MODE))
+ defaultenv_append_directory(defaultenv_2_reboot_mode);
+ if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_SECURITY_POLICY))
+ defaultenv_append_directory(defaultenv_2_security_policy);
+ if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC_NEW_IKCONFIG))
+ defaultenv_append_directory(defaultenv_2_ikconfig);
+ if (IS_ENABLED(CONFIG_DEFAULT_ENVIRONMENT_GENERIC))
+ defaultenv_append_directory(defaultenv_1);
+
+ /*
+ * The traditional or external environment given with
+ * CONFIG_DEFAULT_ENVIRONMENT_PATH.
+ */
+ defaultenv_append((void *)default_environment,
+ default_environment_size, "defaultenv");
+
+ return 0;
+}
+pure_initcall(defaultenv_init);
--
2.47.3
prev parent reply other threads:[~2025-11-24 18:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-24 17:59 [PATCH 1/3] defaultenv: skip non-existent dirs in defaultenv_append_runtime_directory Ahmad Fatoum
2025-11-24 17:59 ` [PATCH 2/3] defaultenv: don't abort defaultenv_load silently on error Ahmad Fatoum
2025-11-24 17:59 ` Ahmad Fatoum [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251124175947.1725563-3-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--cc=barebox@lists.infradead.org \
--cc=j.luebbe@pengutronix.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox