From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 7/7] commands: boot: add support for overriding boot artifacts
Date: Fri, 14 Feb 2025 11:48:17 +0100 [thread overview]
Message-ID: <20250214104817.2975052-8-a.fatoum@pengutronix.de> (raw)
In-Reply-To: <20250214104817.2975052-1-a.fatoum@pengutronix.de>
Replacing part of the boot artifacts is a common development use case,
e.g. to load the kernel from the network, but otherwise boot the
bootloader spec file as-is or to inject an initrd into an existing boot
flow (e.g. rsinit to enable usb9pfs boot).
Add a simple to use way to achieve that.
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
Documentation/user/booting-linux.rst | 8 ++
commands/boot.c | 53 +++++++++++++-
common/Kconfig | 7 ++
common/bootm.c | 106 +++++++++++++++++++++------
include/bootm.h | 18 +++++
5 files changed, 169 insertions(+), 23 deletions(-)
diff --git a/Documentation/user/booting-linux.rst b/Documentation/user/booting-linux.rst
index b26ada943a59..b49b2c93b1f3 100644
--- a/Documentation/user/booting-linux.rst
+++ b/Documentation/user/booting-linux.rst
@@ -161,6 +161,14 @@ This entry can be booted with ``boot mmc``. It can also be made the default by
setting the ``global.boot.default`` variable to ``mmc`` and then calling
``boot`` without arguments.
+Especially for development, it can be useful to override only parts of
+the images used in a boot. To do so, set ``CONFIG_BOOT_OVERRIDE=y``
+and configure the overrides as arguments to the ``boot`` command::
+
+.. code-block:: sh
+
+ boot -o bootm.image=/mnt/tftp/oftree mmc
+
.. _bootloader_spec:
Boot Loader Specification
diff --git a/commands/boot.c b/commands/boot.c
index e4699520e8f5..820708380191 100644
--- a/commands/boot.c
+++ b/commands/boot.c
@@ -6,6 +6,7 @@
#include <getopt.h>
#include <malloc.h>
#include <boot.h>
+#include <bootm.h>
#include <complete.h>
#include <linux/stat.h>
@@ -23,6 +24,42 @@ static char *next_word(void *context)
return strsep(context, " ");
}
+static int boot_add_override(struct bootm_overrides *overrides, char *var)
+{
+ const char *val;
+
+ if (!IS_ENABLED(CONFIG_BOOT_OVERRIDE))
+ return -ENOSYS;
+
+ var += str_has_prefix(var, "global.");
+
+ val = parse_assignment(var);
+ if (!val) {
+ val = globalvar_get(var);
+ if (isempty(val))
+ val = NULL;
+ }
+
+ if (!strcmp(var, "bootm.image")) {
+ if (isempty(val))
+ return -EINVAL;
+ overrides->os_file = val;
+ } else if (!strcmp(var, "bootm.oftree")) {
+ overrides->oftree_file = val;
+ } else if (!strcmp(var, "bootm.initrd")) {
+ overrides->initrd_file = val;
+ } else {
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static inline bool boot_has_overrides(const struct bootm_overrides *overrides)
+{
+ return overrides->os_file || overrides->oftree_file || overrides->initrd_file;
+}
+
static int do_boot(int argc, char *argv[])
{
char *freep = NULL;
@@ -31,11 +68,12 @@ static int do_boot(int argc, char *argv[])
unsigned default_menu_entry = 0;
struct bootentries *entries;
struct bootentry *entry;
+ struct bootm_overrides overrides = {};
void *handle;
const char *name;
char *(*next)(void *);
- while ((opt = getopt(argc, argv, "vldmM:t:w:")) > 0) {
+ while ((opt = getopt(argc, argv, "vldmM:t:w:o:")) > 0) {
switch (opt) {
case 'v':
verbose++;
@@ -65,6 +103,11 @@ static int do_boot(int argc, char *argv[])
case 'w':
boot_set_watchdog_timeout(simple_strtoul(optarg, NULL, 0));
break;
+ case 'o':
+ ret = boot_add_override(&overrides, optarg);
+ if (ret)
+ return ret;
+ break;
default:
return COMMAND_ERROR_USAGE;
}
@@ -85,6 +128,8 @@ static int do_boot(int argc, char *argv[])
}
entries = bootentries_alloc();
+ if (boot_has_overrides(&overrides))
+ bootm_set_overrides(&overrides);
while ((name = next(&handle)) != NULL) {
if (!*name)
@@ -119,6 +164,8 @@ static int do_boot(int argc, char *argv[])
ret = 0;
out:
+ if (boot_has_overrides(&overrides))
+ bootm_unset_overrides();
bootentries_free(entries);
free(freep);
@@ -150,6 +197,10 @@ BAREBOX_CMD_HELP_OPT ("-l","List available boot sources")
BAREBOX_CMD_HELP_OPT ("-m","Show a menu with boot options")
BAREBOX_CMD_HELP_OPT ("-M INDEX","Show a menu with boot options with entry INDEX preselected")
BAREBOX_CMD_HELP_OPT ("-w SECS","Start watchdog with timeout SECS before booting")
+#ifdef CONFIG_BOOT_OVERRIDE
+BAREBOX_CMD_HELP_OPT ("-o VAR[=VAL]","override VAR (bootm.{image,oftree,initrd}) with VAL")
+BAREBOX_CMD_HELP_OPT (" ","if VAL is not specified, the value of VAR is taken")
+#endif
BAREBOX_CMD_HELP_OPT ("-t SECS","specify timeout in SECS")
BAREBOX_CMD_HELP_END
diff --git a/common/Kconfig b/common/Kconfig
index b5fba72390d8..0ce99e98286c 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -599,6 +599,13 @@ menuconfig BOOTM
default y if COMMAND_SUPPORT
bool "bootm support"
+config BOOT_OVERRIDE
+ bool "Support partial override of boot entries"
+ depends on BOOTM
+ help
+ Allow overriding of bootm image, oftree and initrd by passing
+ -o as an argument to the boot command.
+
config BOOTM_SHOW_TYPE
bool
depends on BOOTM && UIMAGE
diff --git a/common/bootm.c b/common/bootm.c
index d2373f321c19..80905d4cf1ce 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -17,6 +17,8 @@
static LIST_HEAD(handler_list);
+static __maybe_unused struct bootm_overrides bootm_overrides;
+
int register_image_handler(struct image_handler *handler)
{
list_add_tail(&handler->list, &handler_list);
@@ -141,6 +143,23 @@ static inline bool image_is_uimage(struct image_data *data)
return IS_ENABLED(CONFIG_BOOTM_UIMAGE) && data->os;
}
+static bool bootm_get_override(char **oldpath, const char *newpath)
+{
+ if (!IS_ENABLED(CONFIG_BOOT_OVERRIDE))
+ return false;
+ if (bootm_signed_images_are_forced())
+ return false;
+ if (!newpath)
+ return false;
+
+ if (oldpath && strcmp(*oldpath, newpath)) {
+ free(*oldpath);
+ *oldpath = *newpath ? xstrdup(newpath) : NULL;
+ }
+
+ return true;
+}
+
/*
* bootm_load_os() - load OS to RAM
*
@@ -155,6 +174,12 @@ static inline bool image_is_uimage(struct image_data *data)
*/
int bootm_load_os(struct image_data *data, unsigned long load_address)
{
+ if (bootm_get_override(&data->os_file, bootm_overrides.os_file)) {
+ if (load_address == UIMAGE_INVALID_ADDRESS)
+ return -EINVAL;
+ goto os_file;
+ }
+
if (data->os_res)
return 0;
@@ -194,15 +219,15 @@ int bootm_load_os(struct image_data *data, unsigned long load_address)
if (IS_ENABLED(CONFIG_ELF) && data->elf)
return elf_load(data->elf);
- if (data->os_file) {
- data->os_res = file_to_sdram(data->os_file, load_address);
- if (!data->os_res)
- return -ENOMEM;
+os_file:
+ if (!data->os_file)
+ return -EINVAL;
- return 0;
- }
+ data->os_res = file_to_sdram(data->os_file, load_address);
+ if (!data->os_res)
+ return -ENOMEM;
- return -EINVAL;
+ return 0;
}
static bool fitconfig_has_ramdisk(struct image_data *data)
@@ -263,6 +288,9 @@ bootm_load_initrd(struct image_data *data, unsigned long load_address)
if (!IS_ENABLED(CONFIG_BOOTM_INITRD))
return NULL;
+ if (bootm_get_override(&data->initrd_file, bootm_overrides.initrd_file))
+ goto initrd_file;
+
if (data->initrd_res)
return data->initrd_res;
@@ -292,6 +320,7 @@ bootm_load_initrd(struct image_data *data, unsigned long load_address)
goto done1;
}
+initrd_file:
if (!data->initrd_file)
return NULL;
@@ -404,12 +433,17 @@ void *bootm_get_devicetree(struct image_data *data)
{
enum filetype type;
struct fdt_header *oftree;
+ bool from_fit = false;
int ret;
if (!IS_ENABLED(CONFIG_OFTREE))
return ERR_PTR(-ENOSYS);
- if (fitconfig_has_fdt(data)) {
+ from_fit = fitconfig_has_fdt(data);
+ if (bootm_get_override(&data->oftree_file, bootm_overrides.oftree_file))
+ from_fit = false;
+
+ if (from_fit) {
const void *of_tree;
unsigned long of_size;
@@ -530,24 +564,29 @@ int bootm_load_devicetree(struct image_data *data, void *fdt,
int bootm_get_os_size(struct image_data *data)
{
+ const char *os_file;
+ struct stat s;
int ret;
- if (data->elf)
- return elf_get_mem_size(data->elf);
- if (image_is_uimage(data))
- return uimage_get_size(data->os, uimage_part_num(data->os_part));
- if (data->os_fit)
- return data->fit_kernel_size;
-
- if (data->os_file) {
- struct stat s;
- ret = stat(data->os_file, &s);
- if (ret)
- return ret;
- return s.st_size;
+ if (bootm_get_override(NULL, bootm_overrides.os_file)) {
+ os_file = bootm_overrides.os_file;
+ } else {
+ if (data->elf)
+ return elf_get_mem_size(data->elf);
+ if (image_is_uimage(data))
+ return uimage_get_size(data->os, uimage_part_num(data->os_part));
+ if (data->os_fit)
+ return data->fit_kernel_size;
+ if (!data->os_file)
+ return -EINVAL;
+ os_file = data->os_file;
}
- return -EINVAL;
+ ret = stat(os_file, &s);
+ if (ret)
+ return ret;
+
+ return s.st_size;
}
static int bootm_open_os_uimage(struct image_data *data)
@@ -909,6 +948,22 @@ int bootm_boot(struct bootm_data *bootm_data)
printf("Passing control to %s handler\n", handler->name);
}
+ if (bootm_get_override(&data->os_file, bootm_overrides.os_file)) {
+ if (data->os_res) {
+ release_sdram_region(data->os_res);
+ data->os_res = NULL;
+ }
+ }
+
+ bootm_get_override(&data->oftree_file, bootm_overrides.oftree_file);
+
+ if (bootm_get_override(&data->initrd_file, bootm_overrides.initrd_file)) {
+ if (data->initrd_res) {
+ release_sdram_region(data->initrd_res);
+ data->initrd_res = NULL;
+ }
+ }
+
ret = handler->bootm(data);
if (data->dryrun)
pr_info("Dryrun. Aborted\n");
@@ -946,6 +1001,13 @@ int bootm_boot(struct bootm_data *bootm_data)
return ret;
}
+#ifdef CONFIG_BOOT_OVERRIDE
+void bootm_set_overrides(const struct bootm_overrides *overrides)
+{
+ bootm_overrides = *overrides;
+}
+#endif
+
static int do_bootm_compressed(struct image_data *img_data)
{
struct bootm_data bootm_data = {
diff --git a/include/bootm.h b/include/bootm.h
index b86d06b0f55d..0e5e99773a26 100644
--- a/include/bootm.h
+++ b/include/bootm.h
@@ -160,4 +160,22 @@ void bootm_force_signed_images(void);
void *booti_load_image(struct image_data *data, phys_addr_t *oftree);
+struct bootm_overrides {
+ const char *os_file;
+ const char *oftree_file;
+ const char *initrd_file;
+};
+
+#ifdef CONFIG_BOOT_OVERRIDE
+void bootm_set_overrides(const struct bootm_overrides *overrides);
+#else
+static inline void bootm_set_overrides(const struct bootm_overrides *overrides) {}
+#endif
+
+static inline void bootm_unset_overrides(void)
+{
+ struct bootm_overrides overrides = {};
+ bootm_set_overrides(&overrides);
+}
+
#endif /* __BOOTM_H */
--
2.39.5
next prev parent reply other threads:[~2025-02-14 10:49 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-02-14 10:48 [PATCH 0/7] " Ahmad Fatoum
2025-02-14 10:48 ` [PATCH 1/7] bootm: add helper functions for checking if FIT image is used Ahmad Fatoum
2025-02-14 10:48 ` [PATCH 2/7] boot: move global.linux.bootargs.dyn. to common code Ahmad Fatoum
2025-02-14 10:48 ` [PATCH 3/7] blspec: don't clobber bootm.image on boot attempt Ahmad Fatoum
2025-02-14 10:48 ` [PATCH 4/7] fastboot: drop useless bootm.image clobber Ahmad Fatoum
2025-02-14 10:48 ` [PATCH 5/7] bootm: don't clobber global.bootm. variables after script boot fails Ahmad Fatoum
2025-02-14 10:48 ` [PATCH 6/7] bootm: retire bootm_has_initrd Ahmad Fatoum
2025-02-14 20:02 ` [PATCH] fixup! " Ahmad Fatoum
2025-02-14 10:48 ` Ahmad Fatoum [this message]
2025-02-15 12:29 ` [PATCH] fixup! commands: boot: add support for overriding boot artifacts Ahmad Fatoum
2025-02-17 9:27 ` [PATCH 0/7] " Sascha Hauer
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=20250214104817.2975052-8-a.fatoum@pengutronix.de \
--to=a.fatoum@pengutronix.de \
--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