From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Adrian Negreanu <adrian.negreanu@nxp.com>,
Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 11/11] fs: qemu_fw_cfg: support populating environment via QEMU fw_cfg
Date: Thu, 13 Mar 2025 11:17:28 +0100 [thread overview]
Message-ID: <20250313101728.3546902-12-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250313101728.3546902-1-a.fatoum@pengutronix.de>
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 <a.fatoum@pengutronix.de>
---
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 055475eb4756..3585660fbc09 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 7f7350e67e64..ef2a1008ee5d 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>
@@ -363,7 +364,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 a6614c0a15ee..1a194c8bff2f 100644
--- a/include/envfs.h
+++ b/include/envfs.h
@@ -130,12 +130,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
prev parent reply other threads:[~2025-03-13 10:37 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-13 10:17 [PATCH 00/11] firmware: qemu_fw_cfg: implement file system Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 01/11] video: ramfb: fix frame buffer screen size Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 02/11] firmware: qemu_fw_cfg: drop duplicate definitions Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 03/11] firmware: qemu_fw_cfg: add support for seeking Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 04/11] firmware: qemu_fw_cfg: rename from /dev/fw_cfg0 to /dev/fw_cfg Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 05/11] fs: add qemu_fw_cfg file system Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 06/11] firmware: qemu_fw_cfg: register at device initcall level Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 07/11] video: ramfb: use new qemu fw_cfg FS Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 08/11] libfile: give copy_file a flags parameter Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 09/11] libfile: pass copy_file flags through copy_recursive Ahmad Fatoum
2025-03-13 10:17 ` [PATCH 10/11] libfile: add support for not clobbering files in copy_file Ahmad Fatoum
2025-03-13 10:17 ` 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=20250313101728.3546902-12-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--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