mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Adrian Negreanu <adrian.negreanu@nxp.com>
Subject: [PATCH v2 4/4] fs: qemu_fw_cfg: support populating environment via QEMU fw_cfg
Date: Fri,  6 Jun 2025 09:29:39 +0200	[thread overview]
Message-ID: <20250606072939.2082971-5-a.fatoum@barebox.org> (raw)
In-Reply-To: <20250606072939.2082971-1-a.fatoum@barebox.org>

QEMU fw_cfg allows passing through user defined key/values, which is
more powerful than what we currently have with sandbox as it allows
passing arbitrary files without having to mess with block devices and
their alignment.

All the added files are already available in the QEMU fw_cfg file system,
but for added convenience, let's map all files under a opt/org.barebox.env
directory to the environment of the running barebox image, thereby
allowing configuring barebox easily from the outside, e.g.

pytest --env nv/boot.default=fit --env boot/fit=@boot.sh

Will boot, after coundown, whatever is in the boot.sh script in the
working directory of QEMU.

Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
 conftest.py             | 22 ++++++++++++++++++++++
 defaultenv/defaultenv.c | 22 ++++++++++++++++++++++
 fs/qemu_fw_cfg.c        |  9 ++++++++-
 include/envfs.h         |  5 +++++
 4 files changed, 57 insertions(+), 1 deletion(-)

diff --git a/conftest.py b/conftest.py
index 941eddcb72dd..5dfefef24638 100644
--- a/conftest.py
+++ b/conftest.py
@@ -39,6 +39,9 @@ def pytest_configure(config):
         os.environ['LG_BUILDDIR'] = os.path.realpath(os.environ['LG_BUILDDIR'])
 
 def pytest_addoption(parser):
+    def assignment(arg):
+            return arg.split('=', 1)
+
     parser.addoption('--interactive', action='store_const', const='qemu_interactive',
         dest='lg_initial_state',
         help=('(for debugging) skip tests and just start Qemu interactively'))
@@ -55,6 +58,9 @@ def pytest_addoption(parser):
     parser.addoption('--blk', action='append', dest='qemu_block',
         default=[], metavar="FILE",
         help=('Pass block device to emulated barebox. Can be specified more than once'))
+    parser.addoption('--env', action='append', dest='qemu_fw_cfg',
+        default=[], metavar="[envpath=]content | [envpath=]@filepath", type=assignment,
+        help=('Pass barebox environment files to barebox. Can be specified more than once'))
     parser.addoption('--qemu', dest='qemu_arg', nargs=argparse.REMAINDER, default=[],
         help=('Pass all remaining options to QEMU as is'))
 
@@ -109,6 +115,22 @@ def strategy(request, target, pytestconfig):
         else:
             pytest.exit("--blk unsupported for target\n", 1)
 
+    for i, fw_cfg in enumerate(pytestconfig.option.qemu_fw_cfg):
+        if virtio:
+            value = fw_cfg.pop()
+            envpath = fw_cfg.pop() if fw_cfg else f"data/fw_cfg{i}"
+
+            if value.startswith('@'):
+                source = f"file='{value[1:]}'"
+            else:
+                source = f"string='{value}'"
+
+            strategy.append_qemu_args(
+                '-fw_cfg', f'name=opt/org.barebox.env/{envpath},{source}'
+            )
+        else:
+            pytest.exit("--env unsupported for target\n", 1)
+
     for arg in pytestconfig.option.qemu_arg:
         strategy.append_qemu_args(arg)
 
diff --git a/defaultenv/defaultenv.c b/defaultenv/defaultenv.c
index be04ae37c17c..2567653751cb 100644
--- a/defaultenv/defaultenv.c
+++ b/defaultenv/defaultenv.c
@@ -18,6 +18,7 @@
 #include <uncompress.h>
 #include <malloc.h>
 #include <init.h>
+#include <libfile.h>
 #include <asm/unaligned.h>
 #include "barebox_default_env.h"
 
@@ -25,6 +26,7 @@ static LIST_HEAD(defaultenv_list);
 
 struct defaultenv {
 	struct list_head list;
+	const char *srcdir;
 	const char *name;
 	void *buf;
 	size_t size;
@@ -93,6 +95,19 @@ void defaultenv_append(void *buf, unsigned int size, const char *name)
 	list_add_tail(&df->list, &defaultenv_list);
 }
 
+void defaultenv_append_runtime_directory(const char *srcdir)
+{
+	struct defaultenv *df;
+
+	defaultenv_add_base();
+
+	df = xzalloc(sizeof(*df));
+	df->srcdir = srcdir;
+	df->name = srcdir;
+
+	list_add_tail(&df->list, &defaultenv_list);
+}
+
 static int defaultenv_load_one(struct defaultenv *df, const char *dir,
 		unsigned flags)
 {
@@ -104,6 +119,13 @@ static int defaultenv_load_one(struct defaultenv *df, const char *dir,
 
 	pr_debug("loading %s\n", df->name);
 
+	if (df->srcdir) {
+		int cpflags = 0;
+		if (flags & ENV_FLAG_NO_OVERWRITE)
+			cpflags |= COPY_FILE_NO_OVERWRITE;
+		return copy_recursive(df->srcdir, dir, cpflags);
+	}
+
 	if (!IS_ENABLED(CONFIG_DEFAULT_COMPRESSION_NONE) &&
 			ft != filetype_barebox_env) {
 		size = get_unaligned_le32(df->buf + df->size - 4);
diff --git a/fs/qemu_fw_cfg.c b/fs/qemu_fw_cfg.c
index 1aa44fb52c70..caf541574649 100644
--- a/fs/qemu_fw_cfg.c
+++ b/fs/qemu_fw_cfg.c
@@ -13,6 +13,7 @@
 #include <errno.h>
 #include <linux/stat.h>
 #include <xfuncs.h>
+#include <envfs.h>
 #include <fcntl.h>
 #include <linux/qemu_fw_cfg.h>
 #include <wchar.h>
@@ -370,7 +371,13 @@ static int fw_cfg_fs_probe(struct device *dev)
 	 */
 	fsdev->cdev = cdev_by_name(devpath_to_name(fsdev->backingstore));
 
-	return fw_cfg_fs_parse(sb);
+	ret = fw_cfg_fs_parse(sb);
+	if (ret)
+		goto free_data;
+
+	defaultenv_append_runtime_directory("/mnt/fw_cfg/by_name/opt/org.barebox.env");
+
+	return 0;
 free_data:
 	free(data);
 	return ret;
diff --git a/include/envfs.h b/include/envfs.h
index a74f574722fa..e21f2b52368a 100644
--- a/include/envfs.h
+++ b/include/envfs.h
@@ -128,12 +128,17 @@ static inline const char *of_env_get_device_alias_by_path(const char *of_path)
 
 #ifdef CONFIG_DEFAULT_ENVIRONMENT
 void defaultenv_append(void *buf, unsigned int size, const char *name);
+void defaultenv_append_runtime_directory(const char *srcdir);
 int defaultenv_load(const char *dir, unsigned flags);
 #else
 static inline void defaultenv_append(void *buf, unsigned int size, const char *name)
 {
 }
 
+static inline void defaultenv_append_runtime_directory(const char *srcdir)
+{
+}
+
 static inline int defaultenv_load(const char *dir, unsigned flags)
 {
 	return -ENOSYS;
-- 
2.39.5




      parent reply	other threads:[~2025-06-06  7:35 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-06  7:29 [PATCH v2 0/4] firmware: qemu_fw_cfg: implement file system Ahmad Fatoum
2025-06-06  7:29 ` [PATCH v2 1/4] fs: add qemu_fw_cfg " Ahmad Fatoum
2025-06-06  7:29 ` [PATCH v2 2/4] firmware: qemu_fw_cfg: register at device initcall level Ahmad Fatoum
2025-06-06  7:29 ` [PATCH v2 3/4] video: ramfb: use new qemu fw_cfg FS Ahmad Fatoum
2025-06-06  7:29 ` 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=20250606072939.2082971-5-a.fatoum@barebox.org \
    --to=a.fatoum@barebox.org \
    --cc=adrian.negreanu@nxp.com \
    --cc=barebox@lists.infradead.org \
    /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