* [RFC PATCH 0/5] bootconfig: Support multiple named bootconfigs in initrd
@ 2026-11-27 14:46 Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 1/5] bootconfig: Add multi-config ID helpers and xbc_init_configs() support Masami Hiramatsu (Google)
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-27 14:46 UTC (permalink / raw)
To: snambakam, Steven Rostedt, linux-trace-kernel
Cc: linux-kernel, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Andrew Morton, Mathieu Desnoyers, linux-doc,
linux-kselftest
Hi,
This RFC series proposes support for bundling multiple named bootconfig
files in a single initrd image and selecting which subset to load at
boot time via the kernel command line.
This work is based on our discussion at LPC 2026 on supporting multiple
bootconfigs in single-image / signed Unified Kernel Image (UKI) setups.
I would appreciate your feedback on the overall design, on-disk format,
command-line semantics, and user-space CLI before finalizing this
series.
Motivation
----------
Bootconfig currently allows only a single bootconfig block appended to
an initrd. In single-image or signed UKI environments, it is often
useful to ship a default production bootconfig alongside optional
debug, tracing, or scenario-specific overlays in the same initrd image,
and enable or combine them at boot without rebuilding or re-signing the
image.
Proposed Specification (Feedback Welcome)
-----------------------------------------
1. In-band naming ("#BOOTCONFIG-ID: <id>"):
Each named bootconfig carries an optional first-line comment
"#BOOTCONFIG-ID: <id>" (1..31 bytes matching [A-Za-z_][A-Za-z0-9_.-]*),
keeping the existing 20-byte "#BOOTCONFIG" trailer format, size, and
checksum framing unchanged for backward compatibility.
2. Stacked trailers & index 0 as default:
Up to XBC_CONFIG_MAX (32) stacked bootconfig trailers can be attached
to an initrd. Block 0 (the first block immediately after initramfs)
is defined as the default bootconfig rather than the outermost
trailer, so appending optional overlays never alters what bare
"bootconfig" loads and places the default first in stack-order
merging so overlays can override keys via ":=" or "+=". All valid
trailers are always stripped from initrd_end so initramfs unpacking
is unaffected.
3. Command-line selection ("bootconfig=[+][<sel>,...]"):
Each <sel> may be a bootconfig ID, a 0-based index, or empty (index
0, e.g. "bootconfig=,debug"). A leading "+" appends to a prior
selection (or to index 0 if none preceded it). Empty "bootconfig="
clears any prior selection (for example, to override a UKI .cmdline),
unless CONFIG_BOOT_CONFIG_FORCE=y is set, which continues to force
index 0. Note that previously empty "bootconfig=" was equivalent to
bare "bootconfig". CONFIG_CMDLINE_FROM_BOOTCONFIG on x86 is updated
to follow the same selection rule.
4. User-space CLI ("tools/bootconfig -i <id[,...]>"):
Extends tools/bootconfig with "-i" across apply, delete, show, list,
and cmdline rendering, with rollback safety on write failure and
merge-conflict warnings against block 0. Five new QEMU boottime
selftests are included to exercise the selection modes.
Does this specification and command-line/tool interface look reasonable
for your use cases? Any comments or suggestions are very welcome.
Patch Overview
--------------
1. bootconfig: Add multi-config ID helpers and xbc_init_configs() support
2. bootconfig: Support stacked initrd bootconfigs and bootconfig= selection
3. tools/bootconfig: Support multiple named bootconfigs with -i option
4. selftests/ftrace: Add boottime tests for multiple bootconfig selection
5. Documentation: bootconfig: Document multiple bootconfigs and selection
Thank you,
Masami Hiramatsu (Google) (5):
bootconfig: Add multi-config ID helpers and xbc_init_configs() support
bootconfig: Support stacked initrd bootconfigs and bootconfig=
selection
tools/bootconfig: Support multiple named bootconfigs with -i option
selftests/ftrace: Add boottime tests for multiple bootconfig selection
Documentation: bootconfig: Document multiple bootconfigs and selection
Documentation/admin-guide/bootconfig.rst | 82 +-
.../admin-guide/kernel-parameters.txt | 10 +
arch/x86/kernel/setup.c | 7 +-
include/linux/bootconfig.h | 32 +
init/main.c | 261 ++++--
lib/bootconfig.c | 476 +++++++++--
tools/bootconfig/include/linux/bootconfig.h | 4 +
tools/bootconfig/main.c | 779 ++++++++++++++----
tools/bootconfig/test-bootconfig.sh | 116 +++
.../testing/selftests/ftrace/boottime/README | 4 +-
.../bootconfigs/bconf-multi-01-default.bconf | 5 +
.../bconf-multi-01-default.debug.bconf | 5 +
.../bconf-multi-02-select.alt.bconf | 5 +
.../bconf-multi-02-select.base.bconf | 7 +
.../bconf-multi-02-select.dbg.bconf | 7 +
.../bootconfigs/bconf-multi-03-append.bconf | 6 +
.../bconf-multi-03-append.dbg.bconf | 6 +
.../bootconfigs/bconf-multi-04-clear.bconf | 5 +
.../bootconfigs/bconf-multi-05-conflict.bconf | 6 +
.../ftrace/boottime/run_boottime_test.sh | 99 ++-
.../boottime/tests/bconf-multi-01-default.sh | 23 +
.../boottime/tests/bconf-multi-02-select.sh | 53 ++
.../boottime/tests/bconf-multi-03-append.sh | 26 +
.../boottime/tests/bconf-multi-04-clear.sh | 26 +
.../boottime/tests/bconf-multi-05-conflict.sh | 22 +
25 files changed, 1762 insertions(+), 310 deletions(-)
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-01-default.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-01-default.debug.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.alt.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.base.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.dbg.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-03-append.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-03-append.dbg.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-04-clear.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-05-conflict.bconf
create mode 100755 tools/testing/selftests/ftrace/boottime/tests/bconf-multi-01-default.sh
create mode 100755 tools/testing/selftests/ftrace/boottime/tests/bconf-multi-02-select.sh
create mode 100755 tools/testing/selftests/ftrace/boottime/tests/bconf-multi-03-append.sh
create mode 100755 tools/testing/selftests/ftrace/boottime/tests/bconf-multi-04-clear.sh
create mode 100755 tools/testing/selftests/ftrace/boottime/tests/bconf-multi-05-conflict.sh
base-commit: 56bb11fe6aacc7666938e8e6765b44aa0cf1fcdd
--
Masami Hiramatsu (Google) <mhiramat@kernel.org>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH 1/5] bootconfig: Add multi-config ID helpers and xbc_init_configs() support
2026-11-27 14:46 [RFC PATCH 0/5] bootconfig: Support multiple named bootconfigs in initrd Masami Hiramatsu (Google)
@ 2026-11-27 14:46 ` Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 2/5] bootconfig: Support stacked initrd bootconfigs and bootconfig= selection Masami Hiramatsu (Google)
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-27 14:46 UTC (permalink / raw)
To: snambakam, Steven Rostedt, linux-trace-kernel
Cc: linux-kernel, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Andrew Morton, Mathieu Desnoyers, linux-doc,
linux-kselftest
To allow bundling multiple bootconfig files in a single initrd (such as
in a Unified Kernel Image) and selecting a subset at boot, each attached
config needs an identifier and the parser must be able to merge multiple
selected configs into the XBC tree.
Store each named block's identifier as an in-band first-line comment
("#BOOTCONFIG-ID: <id>\n") rather than changing the 20-byte trailer
footer, keeping checksum and size framing unchanged. Restrict IDs to
1..31 bytes matching [A-Za-z_][A-Za-z0-9_.-]* so leading digits remain
unambiguously reserved for 0-based index selection and "," / "+" remain
available for command-line list syntax. Track the ID length in
struct xbc_config so xbc_parse_id_header() never needs to mutate initrd
or rodata memory in place.
Because struct xbc_node stores a 15-bit offset into a single xbc_data
buffer, xbc_init_configs() pre-calculates the combined size of all
enabled configs (up to XBC_DATA_MAX) and allocates xbc_data once without
realloc. Each config slice is separated by a NUL byte and parsed with a
fresh last_parent and brace_index so comments, quotes, and unclosed
braces cannot bleed across files, while any parse error offset is mapped
back to the offending config index and file-local byte offset.
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
include/linux/bootconfig.h | 29 ++
lib/bootconfig.c | 355 ++++++++++++++++++--
tools/bootconfig/include/linux/bootconfig.h | 4 +
tools/bootconfig/main.c | 2 -
4 files changed, 356 insertions(+), 34 deletions(-)
diff --git a/include/linux/bootconfig.h b/include/linux/bootconfig.h
index f745068c07cd..899cb7ac6b64 100644
--- a/include/linux/bootconfig.h
+++ b/include/linux/bootconfig.h
@@ -31,6 +31,21 @@ bool __init cmdline_has_extra_options(void);
#define BOOTCONFIG_FOOTER_SIZE \
(sizeof(uint32_t) * 2 + BOOTCONFIG_MAGIC_LEN)
+#define BOOTCONFIG_ID_PREFIX "#BOOTCONFIG-ID: "
+#define BOOTCONFIG_ID_PREFIX_LEN 16
+/* Maximum length of bootconfig ID including NUL */
+#define BOOTCONFIG_ID_MAX 32
+/* Maximum number of stacked bootconfig files in initrd */
+#define XBC_CONFIG_MAX 32
+
+struct xbc_config {
+ const char *name;
+ size_t name_len;
+ const char *data;
+ size_t size;
+ bool enabled;
+};
+
/**
* xbc_calc_checksum() - Calculate checksum of bootconfig
* @data: Bootconfig data.
@@ -322,6 +337,20 @@ static inline int __init xbc_node_compose_key(struct xbc_node *node,
/* XBC node initializer */
int __init xbc_init(const char *buf, size_t size, const char **emsg, int *epos);
+int __init xbc_init_configs(const struct xbc_config *cfgs, int count,
+ const char **emsg, int *epos, int *err_idx);
+
+/* Multi-config ID and selection helpers */
+bool __init xbc_valid_id(const char *id, size_t len);
+int __init xbc_parse_id_header(const char *data, size_t size,
+ struct xbc_config *cfg);
+int __init __xbc_enable_configs(struct xbc_config *cfgs, int count,
+ const char *spec, bool *seeded, bool warn);
+static inline int __init xbc_enable_configs(struct xbc_config *cfgs, int count,
+ const char *spec, bool *seeded)
+{
+ return __xbc_enable_configs(cfgs, count, spec, seeded, true);
+}
/* XBC node and size information */
int __init xbc_get_info(int *node_size, size_t *data_size);
diff --git a/lib/bootconfig.c b/lib/bootconfig.c
index 9ae9a215b1da..fe27e72c9ee5 100644
--- a/lib/bootconfig.c
+++ b/lib/bootconfig.c
@@ -1034,13 +1034,6 @@ static int __init xbc_verify_tree(void)
size_t len, wlen;
struct xbc_node *n, *m;
- /* Brace closing */
- if (brace_index) {
- n = &xbc_nodes[open_brace[brace_index - 1]];
- return xbc_parse_error("Brace is not closed",
- xbc_node_get_data(n));
- }
-
/* Empty tree */
if (xbc_node_num == 0) {
xbc_parse_error("Empty config", xbc_data);
@@ -1085,13 +1078,15 @@ static int __init xbc_verify_tree(void)
}
/* Need to setup xbc_data and xbc_nodes before call this. */
-static int __init xbc_parse_tree(void)
+static int __init xbc_parse_tree(char *data, size_t size)
{
char *p, *q;
int ret = 0, c;
+ struct xbc_node *n;
last_parent = NULL;
- p = xbc_data;
+ brace_index = 0;
+ p = data;
do {
q = strpbrk(p, "{}=+;:\n#");
if (!q) {
@@ -1134,12 +1129,219 @@ static int __init xbc_parse_tree(void)
} while (!ret);
if (!ret) {
- while (p < xbc_data + xbc_data_size - 1 && *p == '\0')
+ while (p < data + size && *p == '\0')
p++;
- if (p < xbc_data + xbc_data_size - 1)
+ if (p < data + size)
ret = xbc_parse_error("Unexpected data after null character", p);
}
+ if (!ret && brace_index) {
+ n = &xbc_nodes[open_brace[brace_index - 1]];
+ ret = xbc_parse_error("Brace is not closed",
+ xbc_node_get_data(n));
+ }
+
+ return ret;
+}
+
+/**
+ * xbc_valid_id() - Validate a bootconfig identifier string
+ * @id: Identifier string (not required to be NUL-terminated)
+ * @len: Length of @id in bytes
+ *
+ * A valid bootconfig ID has length 1..BOOTCONFIG_ID_MAX - 1, starts with an
+ * ASCII letter or underscore (digits are reserved for index numbers), and
+ * contains only ASCII letters, digits, '_', '-', or '.'.
+ *
+ * Return: true if @id is valid, false otherwise.
+ */
+bool __init xbc_valid_id(const char *id, size_t len)
+{
+ size_t i;
+
+ if (!id || len == 0 || len >= BOOTCONFIG_ID_MAX)
+ return false;
+
+ if (!(('a' <= id[0] && id[0] <= 'z') ||
+ ('A' <= id[0] && id[0] <= 'Z') ||
+ id[0] == '_'))
+ return false;
+
+ for (i = 1; i < len; i++) {
+ char c = id[i];
+
+ if (!(('a' <= c && c <= 'z') ||
+ ('A' <= c && c <= 'Z') ||
+ ('0' <= c && c <= '9') ||
+ c == '_' || c == '-' || c == '.'))
+ return false;
+ }
+
+ return true;
+}
+
+/**
+ * xbc_parse_id_header() - Parse optional #BOOTCONFIG-ID header from a block
+ * @data: Bootconfig block buffer (may have trailing NUL padding)
+ * @size: Size of @data in bytes
+ * @cfg: Output descriptor to populate
+ *
+ * Strip trailing NUL padding from @data and check if line 1 starts with
+ * "#BOOTCONFIG-ID: <id>\n". If present, validate <id> and point @cfg->name
+ * at <id> with @cfg->name_len set, and @cfg->data past the newline.
+ * Otherwise set @cfg->name to "" (@cfg->name_len = 0) and @cfg->data to @data.
+ *
+ * Return: 0 on success, or -EINVAL if a malformed ID header is present.
+ */
+int __init xbc_parse_id_header(const char *data, size_t size,
+ struct xbc_config *cfg)
+{
+ const char *id, *nl;
+
+ while (size > 0 && data[size - 1] == '\0')
+ size--;
+
+ cfg->enabled = false;
+ if (size >= BOOTCONFIG_ID_PREFIX_LEN &&
+ !memcmp(data, BOOTCONFIG_ID_PREFIX, BOOTCONFIG_ID_PREFIX_LEN)) {
+ id = data + BOOTCONFIG_ID_PREFIX_LEN;
+ nl = memchr(id, '\n', size - BOOTCONFIG_ID_PREFIX_LEN);
+ if (!nl || !xbc_valid_id(id, nl - id))
+ return -EINVAL;
+ cfg->name = id;
+ cfg->name_len = nl - id;
+ cfg->data = nl + 1;
+ cfg->size = size - (size_t)(cfg->data - data);
+ } else {
+ cfg->name = "";
+ cfg->name_len = 0;
+ cfg->data = data;
+ cfg->size = size;
+ }
+
+ return 0;
+}
+
+static int __init xbc_parse_index(const char *p, size_t len)
+{
+ unsigned int idx = 0;
+ size_t i;
+
+ for (i = 0; i < len; i++) {
+ if (p[i] < '0' || p[i] > '9')
+ return -EINVAL;
+ idx = idx * 10 + (p[i] - '0');
+ if (idx >= XBC_CONFIG_MAX)
+ return -ERANGE;
+ }
+
+ return (int)idx;
+}
+
+/**
+ * __xbc_enable_configs() - Update enabled flags on bootconfig entries from spec
+ * @cfgs: Array of bootconfig descriptors
+ * @count: Number of entries in @cfgs
+ * @spec: Selection spec (NULL for default, "" to clear, "[+]<id_or_idx>,...")
+ * @seeded: Tracks whether a prior selection has already been applied
+ * @warn: If true, log pr_err() when a requested ID or index is not found
+ *
+ * Update @cfgs[i].enabled according to @spec:
+ * - NULL: clears all entries and enables @cfgs[0] (default).
+ * - "": clears all entries.
+ * - "+...": keeps existing enabled flags (seeding @cfgs[0] if !*@seeded),
+ * then enables listed IDs/indices.
+ * - "<id_or_idx>,...": clears all entries, then enables listed IDs/indices.
+ *
+ * An empty comma-separated token selects @cfgs[0]. Numeric tokens select by
+ * 0-based index; identifier tokens match @cfgs[i].name.
+ *
+ * Return: 0 if all requested entries matched, or -ENOENT if any was missing.
+ */
+int __init __xbc_enable_configs(struct xbc_config *cfgs, int count,
+ const char *spec, bool *seeded, bool warn)
+{
+ const char *p, *comma;
+ int i, idx, ret = 0;
+ size_t len;
+
+ if (!spec) {
+ for (i = 0; i < count; i++)
+ cfgs[i].enabled = false;
+ if (count > 0)
+ cfgs[0].enabled = true;
+ if (seeded)
+ *seeded = true;
+ return 0;
+ }
+
+ if (*spec == '\0') {
+ for (i = 0; i < count; i++)
+ cfgs[i].enabled = false;
+ if (seeded)
+ *seeded = true;
+ return 0;
+ }
+
+ if (*spec == '+') {
+ spec++;
+ if ((!seeded || !*seeded) && count > 0)
+ cfgs[0].enabled = true;
+ if (seeded)
+ *seeded = true;
+ if (*spec == '\0')
+ return 0;
+ } else {
+ for (i = 0; i < count; i++)
+ cfgs[i].enabled = false;
+ if (seeded)
+ *seeded = true;
+ }
+
+ p = spec;
+ while (1) {
+ comma = strchr(p, ',');
+ len = comma ? (size_t)(comma - p) : strlen(p);
+ if (len == 0) {
+ if (count > 0) {
+ cfgs[0].enabled = true;
+ } else {
+ if (warn)
+ pr_err("bootconfig: default config not found\n");
+ ret = -ENOENT;
+ }
+ } else if (p[0] >= '0' && p[0] <= '9') {
+ idx = xbc_parse_index(p, len);
+ if (idx >= 0 && idx < count) {
+ cfgs[idx].enabled = true;
+ } else {
+ if (warn)
+ pr_err("bootconfig: '%.*s' not found\n",
+ (int)len, p);
+ ret = -ENOENT;
+ }
+ } else {
+ bool matched = false;
+
+ for (i = 0; i < count; i++) {
+ if (cfgs[i].name_len == len &&
+ !memcmp(cfgs[i].name, p, len)) {
+ cfgs[i].enabled = true;
+ matched = true;
+ }
+ }
+ if (!matched) {
+ if (warn)
+ pr_err("bootconfig: '%.*s' not found\n",
+ (int)len, p);
+ ret = -ENOENT;
+ }
+ }
+ if (!comma)
+ break;
+ p = comma + 1;
+ }
+
return ret;
}
@@ -1161,49 +1363,84 @@ void __init xbc_exit(void)
brace_index = 0;
}
+static size_t __init xbc_config_slice_size(const struct xbc_config *cfg)
+{
+ if (!cfg->enabled || !cfg->data || cfg->size == 0)
+ return 0;
+
+ return cfg->size + (cfg->data[cfg->size - 1] != '\0' ? 1 : 0);
+}
+
/**
- * xbc_init() - Parse given XBC file and build XBC internal tree
- * @data: The boot config text original data
- * @size: The size of @data
+ * xbc_init_configs() - Parse enabled XBC configs and build XBC internal tree
+ * @cfgs: Array of bootconfig descriptors
+ * @count: Number of entries in @cfgs
* @emsg: A pointer of const char * to store the error message
- * @epos: A pointer of int to store the error position
+ * @epos: A pointer of int to store the error position (local to failing config)
+ * @err_idx: If non-NULL, set to index in @cfgs of the failing config on error
*
- * This parses the boot config text in @data. @size must be smaller
- * than XBC_DATA_MAX.
- * Return the number of stored nodes (>0) if succeeded, or -errno
- * if there is any error.
- * In error cases, @emsg will be updated with an error message and
- * @epos will be updated with the error position which is the byte offset
- * of @buf. If the error is not a parser error, @epos will be -1.
+ * Concatenate all enabled entries in @cfgs into a single buffer of at most
+ * XBC_DATA_MAX bytes and parse each slice into the XBC tree.
+ * Return the number of stored nodes (>0) if succeeded, or -errno on error.
*/
-int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
+int __init xbc_init_configs(const struct xbc_config *cfgs, int count,
+ const char **emsg, int *epos, int *err_idx)
{
- int ret;
+ size_t total_size = 0, slice_size;
+ int i, ret = 0;
+ char *dst;
if (epos)
*epos = -1;
+ if (err_idx)
+ *err_idx = -1;
if (xbc_data) {
if (emsg)
*emsg = "Bootconfig is already initialized";
return -EBUSY;
}
- if (size > XBC_DATA_MAX || size == 0) {
+
+ for (i = 0; i < count; i++) {
+ if (!cfgs[i].enabled || !cfgs[i].data || cfgs[i].size == 0)
+ continue;
+ if (cfgs[i].size > XBC_DATA_MAX) {
+ if (emsg)
+ *emsg = "Config data is too big";
+ return -ERANGE;
+ }
+ slice_size = xbc_config_slice_size(&cfgs[i]);
+ if (slice_size > XBC_DATA_MAX - total_size) {
+ if (emsg)
+ *emsg = "Config data is too big";
+ return -ERANGE;
+ }
+ total_size += slice_size;
+ }
+
+ if (total_size == 0) {
if (emsg)
- *emsg = size ? "Config data is too big" :
- "Config data is empty";
+ *emsg = "Config data is empty";
return -ERANGE;
}
- xbc_data = xbc_alloc_mem(size + 1);
+ xbc_data = xbc_alloc_mem(total_size);
if (!xbc_data) {
if (emsg)
*emsg = "Failed to allocate bootconfig data";
return -ENOMEM;
}
- memcpy(xbc_data, data, size);
- xbc_data[size] = '\0';
- xbc_data_size = size + 1;
+
+ dst = xbc_data;
+ for (i = 0; i < count; i++) {
+ slice_size = xbc_config_slice_size(&cfgs[i]);
+ if (!slice_size)
+ continue;
+ memcpy(dst, cfgs[i].data, cfgs[i].size);
+ dst[slice_size - 1] = '\0';
+ dst += slice_size;
+ }
+ xbc_data_size = total_size;
xbc_nodes = xbc_alloc_mem(sizeof(struct xbc_node) * XBC_NODE_MAX);
if (!xbc_nodes) {
@@ -1213,7 +1450,17 @@ int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
return -ENOMEM;
}
- ret = xbc_parse_tree();
+ dst = xbc_data;
+ for (i = 0; i < count; i++) {
+ slice_size = xbc_config_slice_size(&cfgs[i]);
+ if (!slice_size)
+ continue;
+ ret = xbc_parse_tree(dst, cfgs[i].size);
+ if (ret < 0)
+ break;
+ dst += slice_size;
+ }
+
if (!ret)
ret = xbc_verify_tree();
#ifndef __KERNEL__
@@ -1222,6 +1469,23 @@ int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
#endif
if (ret < 0) {
+ if (xbc_err_pos >= 0) {
+ size_t off = 0;
+
+ for (i = 0; i < count; i++) {
+ slice_size = xbc_config_slice_size(&cfgs[i]);
+ if (!slice_size)
+ continue;
+ if ((size_t)xbc_err_pos >= off &&
+ (size_t)xbc_err_pos < off + slice_size) {
+ if (err_idx)
+ *err_idx = i;
+ xbc_err_pos -= (int)off;
+ break;
+ }
+ off += slice_size;
+ }
+ }
if (epos)
*epos = xbc_err_pos;
if (emsg)
@@ -1233,3 +1497,30 @@ int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
return ret;
}
+
+/**
+ * xbc_init() - Parse given XBC file and build XBC internal tree
+ * @data: The boot config text original data
+ * @size: The size of @data
+ * @emsg: A pointer of const char * to store the error message
+ * @epos: A pointer of int to store the error position
+ *
+ * This parses the boot config text in @data. @size must be smaller
+ * than XBC_DATA_MAX.
+ * Return the number of stored nodes (>0) if succeeded, or -errno
+ * if there is any error.
+ * In error cases, @emsg will be updated with an error message and
+ * @epos will be updated with the error position which is the byte offset
+ * of @buf. If the error is not a parser error, @epos will be -1.
+ */
+int __init xbc_init(const char *data, size_t size, const char **emsg, int *epos)
+{
+ struct xbc_config cfg = {
+ .name = "",
+ .data = data,
+ .size = size,
+ .enabled = true,
+ };
+
+ return xbc_init_configs(&cfg, 1, emsg, epos, NULL);
+}
diff --git a/tools/bootconfig/include/linux/bootconfig.h b/tools/bootconfig/include/linux/bootconfig.h
index 6784296a0692..ac26cf8cbf8b 100644
--- a/tools/bootconfig/include/linux/bootconfig.h
+++ b/tools/bootconfig/include/linux/bootconfig.h
@@ -49,6 +49,10 @@ static inline char *strim(char *s)
#define __init
#define __initdata
+#ifndef pr_err
+#define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
+#endif
+
#include "../../../../include/linux/bootconfig.h"
#endif
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index c85e58fcb6b0..6f68e02f6385 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -15,8 +15,6 @@
#include <linux/bootconfig.h>
-#define pr_err(fmt, ...) fprintf(stderr, fmt, ##__VA_ARGS__)
-
static void show_xbc_error(const char *data, const char *msg, int pos)
{
int lin = 1, col, i;
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH 2/5] bootconfig: Support stacked initrd bootconfigs and bootconfig= selection
2026-11-27 14:46 [RFC PATCH 0/5] bootconfig: Support multiple named bootconfigs in initrd Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 1/5] bootconfig: Add multi-config ID helpers and xbc_init_configs() support Masami Hiramatsu (Google)
@ 2026-11-27 14:46 ` Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 3/5] tools/bootconfig: Support multiple named bootconfigs with -i option Masami Hiramatsu (Google)
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-27 14:46 UTC (permalink / raw)
To: snambakam, Steven Rostedt, linux-trace-kernel
Cc: linux-kernel, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Andrew Morton, Mathieu Desnoyers, linux-doc,
linux-kselftest
Allow an initrd image to carry up to XBC_CONFIG_MAX (32) stacked
"#BOOTCONFIG" trailers and select which subset to load at boot via
bootconfig=[+][<sel>,...].
When peeling trailers backwards from initrd_end, populate a static
xbc_configs[XBC_CONFIG_MAX] array from the tail so entries land directly
in forward stack order (0 .. count - 1) without extra allocations. All
valid trailers are always stripped from initrd_end so initramfs
unpacking is unaffected even when bootconfig is not enabled or more than
XBC_CONFIG_MAX trailers are attached. If an individual block has a
malformed ID header or duplicates an earlier block's ID, only that block
is ignored so valid blocks (including block 0) can still load.
Define block 0 (the first block immediately after initramfs) as the
default bootconfig rather than the outermost trailer: this ensures that
appending optional debug/scenario overlays to an image does not alter
what bare "bootconfig" loads by default, and places the default config
first in stack-order merging so later overlays can override keys with
":=" or "+=".
Note on command-line ABI: previously "bootconfig=" with an empty value
was treated the same as bare "bootconfig". With parameterized selection,
"bootconfig=" now clears any earlier "bootconfig=..." selection on the
command line (for example, overriding a UKI's built-in .cmdline), except
when CONFIG_BOOT_CONFIG_FORCE=y is set, which continues to force block 0
so the Kconfig's unconditional-bootconfig guarantee is preserved.
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
.../admin-guide/kernel-parameters.txt | 10 +
arch/x86/kernel/setup.c | 7 +-
include/linux/bootconfig.h | 3 +
init/main.c | 261 +++++++++++++-----
lib/bootconfig.c | 121 ++++++--
5 files changed, 304 insertions(+), 98 deletions(-)
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
index 0b3c140babd9..b514ec59c810 100644
--- a/Documentation/admin-guide/kernel-parameters.txt
+++ b/Documentation/admin-guide/kernel-parameters.txt
@@ -652,8 +652,18 @@ Kernel parameters
Format: integer
bootconfig [KNL,EARLY]
+ bootconfig=[+][<sel>][,<sel>...]
Extended command line options can be added to an initrd
and this will cause the kernel to look for it.
+ When multiple bootconfig files are attached to the
+ initrd, each <sel> may be a bootconfig <id>, a 0-based
+ <index>, or empty (selecting index 0, e.g.
+ "bootconfig=,debug"). Selected configs are merged in
+ initrd stack order. Bare "bootconfig" selects index 0;
+ leading "+" appends to a prior selection (or to index 0
+ if none preceded it); empty "bootconfig=" clears any
+ prior selection (unless CONFIG_BOOT_CONFIG_FORCE=y is
+ set, which forces index 0 when no config is selected).
See Documentation/admin-guide/bootconfig.rst
diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index 19fc8e1714e6..b92482f2216b 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -927,11 +927,10 @@ void __init setup_arch(char **cmdline_p)
#ifdef CONFIG_CMDLINE_FROM_BOOTCONFIG
/*
* Prepend the build-time-rendered embedded "kernel" keys here so
- * parse_early_param() below sees them, using the same opt-in as the
- * runtime parser, plus the build-time CONFIG_BOOT_CONFIG_FORCE.
+ * parse_early_param() below sees them, using the same opt-in and
+ * index 0 selection rule as the runtime parser.
*/
- if (bootconfig_cmdline_requested(boot_command_line, NULL) ||
- IS_ENABLED(CONFIG_BOOT_CONFIG_FORCE))
+ if (bootconfig_cmdline_requested(boot_command_line, NULL))
xbc_prepend_embedded_cmdline(boot_command_line,
COMMAND_LINE_SIZE);
#endif
diff --git a/include/linux/bootconfig.h b/include/linux/bootconfig.h
index 899cb7ac6b64..9b7f77c18c90 100644
--- a/include/linux/bootconfig.h
+++ b/include/linux/bootconfig.h
@@ -371,6 +371,9 @@ static inline const char *xbc_get_embedded_bootconfig(size_t *size)
/* Bootconfig opt-in detection, shared by setup_arch() and setup_boot_config() */
#ifdef CONFIG_BOOT_CONFIG
bool __init bootconfig_cmdline_requested(const char *boot_cmdline, int *end_offset);
+bool __init xbc_select_cmdline_configs(const char *boot_cmdline,
+ struct xbc_config *cfgs, int count,
+ int *end_offset);
#endif
/* Build-time-rendered bootconfig cmdline prepended in setup_arch() */
diff --git a/init/main.c b/init/main.c
index f14780345051..a539eb5b0cd1 100644
--- a/init/main.c
+++ b/init/main.c
@@ -266,80 +266,117 @@ static int __init loglevel(char *str)
early_param("loglevel", loglevel);
#ifdef CONFIG_BLK_DEV_INITRD
-static void * __init get_boot_config_from_initrd(size_t *_size)
+static int __init get_boot_config_from_initrd(struct xbc_config *cfgs,
+ int max_cfgs, int *count)
{
u32 size, raw_size, csum;
char *data;
u8 *hdr;
- int i;
+ int i, total = 0;
- if (!initrd_end || initrd_end < initrd_start ||
- initrd_end - initrd_start < BOOTCONFIG_FOOTER_SIZE)
- return NULL;
-
- data = (char *)initrd_end - BOOTCONFIG_MAGIC_LEN;
- /*
- * Since Grub may align the size of initrd to 4, we must
- * check the preceding 3 bytes as well.
- */
- for (i = 0; i < 4; i++) {
- if (!memcmp(data, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN))
- goto found;
- data--;
- }
- return NULL;
+ if (count)
+ *count = 0;
+ while (initrd_end && initrd_end >= initrd_start &&
+ initrd_end - initrd_start >= BOOTCONFIG_FOOTER_SIZE) {
+ data = (char *)initrd_end - BOOTCONFIG_MAGIC_LEN;
+ /*
+ * Since Grub may align the size of initrd to 4, we must
+ * check the preceding 3 bytes for the outermost trailer.
+ */
+ if (total == 0) {
+ for (i = 0; i < 4; i++) {
+ if (!memcmp(data, BOOTCONFIG_MAGIC,
+ BOOTCONFIG_MAGIC_LEN))
+ goto found;
+ data--;
+ }
+ break;
+ } else if (memcmp(data, BOOTCONFIG_MAGIC,
+ BOOTCONFIG_MAGIC_LEN)) {
+ break;
+ }
found:
- hdr = (u8 *)(data - 8);
- if ((unsigned long)hdr < initrd_start)
- return NULL;
+ hdr = (u8 *)(data - 8);
+ if ((unsigned long)hdr < initrd_start)
+ break;
- raw_size = get_unaligned_le32(hdr);
- csum = get_unaligned_le32(hdr + 4);
+ raw_size = get_unaligned_le32(hdr);
+ csum = get_unaligned_le32(hdr + 4);
- if (raw_size > XBC_DATA_MAX_PADDED) {
- pr_err("bootconfig size %u is greater than max size %d\n",
- raw_size, XBC_DATA_MAX);
- return NULL;
- }
+ if (raw_size > XBC_DATA_MAX_PADDED) {
+ pr_err("bootconfig size %u is greater than max size %d\n",
+ raw_size, XBC_DATA_MAX);
+ break;
+ }
- if (raw_size > ((unsigned long)hdr - initrd_start)) {
- pr_err("bootconfig size %u is greater than initrd size %lu\n",
- raw_size, initrd_end - initrd_start);
- return NULL;
- }
+ if (raw_size > ((unsigned long)hdr - initrd_start)) {
+ pr_err("bootconfig size %u is greater than initrd size %lu\n",
+ raw_size, initrd_end - initrd_start);
+ break;
+ }
- data = ((void *)hdr) - raw_size;
+ data = ((void *)hdr) - raw_size;
- size = xbc_exclude_padding(data, raw_size);
- if (size > XBC_DATA_MAX) {
- pr_err("bootconfig size %u is greater than max size %d\n",
- size, XBC_DATA_MAX);
- return NULL;
- }
+ size = xbc_exclude_padding(data, raw_size);
+ if (size > XBC_DATA_MAX) {
+ pr_err("bootconfig size %u is greater than max size %d\n",
+ size, XBC_DATA_MAX);
+ break;
+ }
- if (xbc_calc_checksum(data, raw_size) != csum) {
- pr_err("bootconfig checksum failed\n");
- return NULL;
+ if (xbc_calc_checksum(data, raw_size) != csum) {
+ pr_err("bootconfig checksum failed\n");
+ break;
+ }
+
+ /*
+ * Remove bootconfig from initramfs/initrd. Since the initrd
+ * memory (including the bootconfig) is freed with initrd_end,
+ * save the original end to free the whole region later.
+ */
+ if (!initrd_mem_end)
+ initrd_mem_end = initrd_end;
+ initrd_end = (unsigned long)data;
+ total++;
+
+#ifdef CONFIG_BOOT_CONFIG
+ if (cfgs && max_cfgs > 0) {
+ struct xbc_config *slot;
+
+ if (total <= max_cfgs) {
+ slot = &cfgs[max_cfgs - total];
+ } else {
+ memmove(&cfgs[1], &cfgs[0],
+ (max_cfgs - 1) * sizeof(*cfgs));
+ slot = &cfgs[0];
+ }
+ if (xbc_parse_id_header(data, size, slot) < 0) {
+ slot->name = "";
+ slot->name_len = 0;
+ slot->data = NULL;
+ slot->size = 0;
+ slot->enabled = false;
+ }
+ }
+#endif
}
- /*
- * Remove bootconfig from initramfs/initrd. Since the initrd memory
- * (including the bootconfig) is freed with initrd_end, save the
- * original end to free the whole region later.
- */
- if (!initrd_mem_end)
- initrd_mem_end = initrd_end;
- initrd_end = (unsigned long)data;
- if (_size)
- *_size = size;
+ if (!cfgs || total == 0)
+ return 0;
+
+ if (count)
+ *count = total > max_cfgs ? max_cfgs : total;
- return data;
+ return total;
}
#else
-static void * __init get_boot_config_from_initrd(size_t *_size)
+static int __init get_boot_config_from_initrd(struct xbc_config *cfgs,
+ int max_cfgs, int *count)
{
- return NULL;
+ if (count)
+ *count = 0;
+ return 0;
}
#endif
@@ -444,29 +481,92 @@ static int __init warn_bootconfig(char *str)
return 0;
}
+static struct xbc_config xbc_configs[XBC_CONFIG_MAX] __initdata;
+
+static void __init drop_bad_boot_configs(struct xbc_config *cfgs, int count)
+{
+ int i, j;
+
+ for (i = 0; i < count; i++) {
+ if (!cfgs[i].data || !cfgs[i].name_len)
+ continue;
+ for (j = i + 1; j < count; j++) {
+ if (cfgs[j].data &&
+ cfgs[j].name_len == cfgs[i].name_len &&
+ !memcmp(cfgs[j].name, cfgs[i].name,
+ cfgs[i].name_len)) {
+ cfgs[j].data = NULL;
+ cfgs[j].size = 0;
+ }
+ }
+ }
+}
+
+static void __init report_bad_boot_configs(const struct xbc_config *cfgs,
+ int count, int total)
+{
+ int i;
+
+ if (total > XBC_CONFIG_MAX)
+ pr_err("bootconfig: too many bootconfigs in initrd (%d > %d), ignoring extra blocks\n",
+ total, XBC_CONFIG_MAX);
+
+ for (i = 0; i < count; i++) {
+ if (cfgs[i].data)
+ continue;
+ if (cfgs[i].name_len)
+ pr_err("bootconfig: duplicate ID '%.*s' in block %d, ignored\n",
+ (int)cfgs[i].name_len, cfgs[i].name, i);
+ else
+ pr_err("bootconfig: invalid ID header in block %d, ignored\n",
+ i);
+ }
+}
+
static void __init setup_boot_config(void)
{
- const char *msg, *data;
- int pos, ret, offs;
+ const char *msg, *embed_data;
+ struct xbc_config *cfgs = NULL;
+ int pos, ret, offs, total, count = 0, err_idx = -1, i;
size_t size;
bool from_embedded = false;
+ bool any_enabled = false;
- /* Cut out the bootconfig data even if we have no bootconfig option */
- data = get_boot_config_from_initrd(&size);
- /* If there is no bootconfig in initrd, try embedded one. */
- if (!data) {
- data = xbc_get_embedded_bootconfig(&size);
- from_embedded = true;
+ /* Cut out all bootconfig trailers even if we have no bootconfig option */
+ total = get_boot_config_from_initrd(xbc_configs, XBC_CONFIG_MAX, &count);
+ if (count > 0) {
+ cfgs = &xbc_configs[XBC_CONFIG_MAX - count];
+ } else {
+ /* If there is no bootconfig in initrd, try embedded one. */
+ embed_data = xbc_get_embedded_bootconfig(&size);
+ if (embed_data) {
+ if (xbc_parse_id_header(embed_data, size,
+ &xbc_configs[0]) < 0) {
+ xbc_configs[0].name = "";
+ xbc_configs[0].name_len = 0;
+ xbc_configs[0].data = embed_data;
+ xbc_configs[0].size = size;
+ xbc_configs[0].enabled = false;
+ }
+ cfgs = &xbc_configs[0];
+ count = 1;
+ total = 1;
+ from_embedded = true;
+ }
}
- bootconfig_found = bootconfig_cmdline_requested(boot_command_line, &offs);
+ if (count > 0)
+ drop_bad_boot_configs(cfgs, count);
+
+ bootconfig_found = xbc_select_cmdline_configs(boot_command_line,
+ cfgs, count, &offs);
if (!(bootconfig_found || IS_ENABLED(CONFIG_BOOT_CONFIG_FORCE)))
return;
/* Offset of the init arguments after a "--", located by the helper. */
initargs_offs = offs;
- if (!data) {
+ if (!count) {
/* If user intended to use bootconfig, show an error level message */
if (bootconfig_found)
pr_err("'bootconfig' found on command line, but no bootconfig found\n");
@@ -475,16 +575,35 @@ static void __init setup_boot_config(void)
return;
}
- ret = xbc_init(data, size, &msg, &pos);
+ report_bad_boot_configs(cfgs, count, total);
+
+ if (!bootconfig_found && IS_ENABLED(CONFIG_BOOT_CONFIG_FORCE))
+ cfgs[0].enabled = true;
+
+ for (i = 0; i < count; i++) {
+ if (cfgs[i].enabled && cfgs[i].data) {
+ any_enabled = true;
+ break;
+ }
+ }
+ if (!any_enabled)
+ return;
+
+ ret = xbc_init_configs(cfgs, count, &msg, &pos, &err_idx);
if (ret < 0) {
- if (pos < 0)
+ if (err_idx >= 0)
+ pr_err("Failed to parse bootconfig [%d%s%.*s]: %s at %d.\n",
+ err_idx, cfgs[err_idx].name_len ? ":" : "",
+ (int)cfgs[err_idx].name_len,
+ cfgs[err_idx].name, msg, pos);
+ else if (pos < 0)
pr_err("Failed to init bootconfig: %s.\n", msg);
else
pr_err("Failed to parse bootconfig: %s at %d.\n",
- msg, pos);
+ msg, pos);
} else {
- xbc_get_info(&ret, NULL);
- pr_info("Load bootconfig: %ld bytes %d nodes\n", (long)size, ret);
+ xbc_get_info(&ret, &size);
+ pr_info("Load bootconfig: %zu bytes %d nodes\n", size, ret);
/*
* keys starting with "kernel." are passed via cmdline. When
* this bootconfig came from the embedded source and
@@ -519,7 +638,7 @@ static void __init exit_boot_config(void)
static void __init setup_boot_config(void)
{
/* Remove bootconfig data from initrd */
- get_boot_config_from_initrd(NULL);
+ get_boot_config_from_initrd(NULL, 0, NULL);
}
static int __init warn_bootconfig(char *str)
diff --git a/lib/bootconfig.c b/lib/bootconfig.c
index fe27e72c9ee5..44177b7c0866 100644
--- a/lib/bootconfig.c
+++ b/lib/bootconfig.c
@@ -115,33 +115,54 @@ bool __init xbc_embedded_cmdline_applied(void)
}
#endif /* CONFIG_CMDLINE_FROM_BOOTCONFIG */
-/* parse_args() callback: flag when the "bootconfig" parameter is present. */
-static int __init bootconfig_optin(char *param, char *val,
- const char *unused, void *arg)
+struct xbc_cmdline_select_ctx {
+ struct xbc_config *cfgs;
+ int count;
+ bool seen;
+ bool active;
+ bool warn;
+};
+
+/* parse_args() callback: track opt-in and update enabled bootconfig entries. */
+static int __init xbc_cmdline_select_cb(char *param, char *val,
+ const char *unused, void *arg)
{
- if (!strcmp(param, "bootconfig"))
- *(bool *)arg = true;
+ struct xbc_cmdline_select_ctx *ctx = arg;
+
+ if (strcmp(param, "bootconfig"))
+ return 0;
+
+ if (!val) {
+ ctx->active = true;
+ } else if (*val == '\0') {
+ ctx->active = false;
+ } else if (*val == '+') {
+ if (!ctx->seen || val[1] != '\0')
+ ctx->active = true;
+ } else {
+ ctx->active = true;
+ }
+
+ if (ctx->cfgs)
+ __xbc_enable_configs(ctx->cfgs, ctx->count, val,
+ &ctx->seen, ctx->warn);
+ else
+ ctx->seen = true;
+
return 0;
}
-/**
- * bootconfig_cmdline_requested() - Was "bootconfig" passed on the cmdline?
- * @boot_cmdline: kernel command line to inspect (not modified)
- * @end_offset: if non-NULL, set to the offset of the init arguments that
- * follow a "--" separator, or 0 when there is none
- *
- * Parse a private copy of @boot_cmdline (parse_args() is destructive) and
- * report whether "bootconfig" is present before the "--" separator.
- * setup_arch() uses this to gate prepending the build-time embedded cmdline;
- * setup_boot_config() uses it for the runtime opt-in and to locate the init
- * arguments via @end_offset. Sharing one parser keeps the early and late
- * paths agreeing on what counts as opt-in. CONFIG_BOOT_CONFIG_FORCE is not
- * folded in here; callers apply it where they need it.
- */
-bool __init bootconfig_cmdline_requested(const char *boot_cmdline, int *end_offset)
+static bool __init xbc_parse_cmdline_bootconfig(const char *boot_cmdline,
+ struct xbc_config *cfgs,
+ int count, int *end_offset,
+ bool warn)
{
static char tmp_cmdline[COMMAND_LINE_SIZE] __initdata;
- bool found = false;
+ struct xbc_cmdline_select_ctx ctx = {
+ .cfgs = cfgs,
+ .count = count,
+ .warn = warn,
+ };
char *err;
if (end_offset)
@@ -149,7 +170,7 @@ bool __init bootconfig_cmdline_requested(const char *boot_cmdline, int *end_offs
strscpy(tmp_cmdline, boot_cmdline, COMMAND_LINE_SIZE);
err = parse_args("bootconfig", tmp_cmdline, NULL, 0, 0, 0,
- &found, bootconfig_optin);
+ &ctx, xbc_cmdline_select_cb);
if (IS_ERR(err))
return false;
@@ -157,7 +178,61 @@ bool __init bootconfig_cmdline_requested(const char *boot_cmdline, int *end_offs
if (end_offset && err)
*end_offset = err - tmp_cmdline;
- return found;
+ return ctx.active;
+}
+
+/**
+ * bootconfig_cmdline_requested() - Should embedded bootconfig cmdline apply?
+ * @boot_cmdline: kernel command line to inspect (not modified)
+ * @end_offset: if non-NULL, set to the offset of the init arguments that
+ * follow a "--" separator, or 0 when there is none
+ *
+ * Parse a private copy of @boot_cmdline (parse_args() is destructive) and
+ * report whether the default (index 0 / embedded) bootconfig is selected
+ * before the "--" separator, or forced by CONFIG_BOOT_CONFIG_FORCE when no
+ * active selection overrides it. setup_arch() uses this to gate prepending
+ * the build-time embedded cmdline, matching setup_boot_config()'s runtime
+ * selection rule.
+ */
+bool __init bootconfig_cmdline_requested(const char *boot_cmdline, int *end_offset)
+{
+ struct xbc_config emb = { .name = "", .name_len = 0, .enabled = false };
+ const char *data;
+ size_t size;
+ bool active;
+
+ data = xbc_get_embedded_bootconfig(&size);
+ if (data && xbc_parse_id_header(data, size, &emb) < 0) {
+ emb.name = "";
+ emb.name_len = 0;
+ }
+
+ active = xbc_parse_cmdline_bootconfig(boot_cmdline, &emb, 1,
+ end_offset, false);
+ if (IS_ENABLED(CONFIG_BOOT_CONFIG_FORCE) && !active)
+ return true;
+
+ return active && emb.enabled;
+}
+
+/**
+ * xbc_select_cmdline_configs() - Select bootconfig entries from cmdline
+ * @boot_cmdline: kernel command line to inspect (not modified)
+ * @cfgs: array of bootconfig descriptors from initrd or embedded fallback
+ * @count: number of entries in @cfgs
+ * @end_offset: if non-NULL, set to the offset of init arguments after "--"
+ *
+ * Evaluate all "bootconfig" parameters before "--" left-to-right and update
+ * @cfgs[i].enabled accordingly.
+ *
+ * Return: true if bootconfig remains active after parsing @boot_cmdline.
+ */
+bool __init xbc_select_cmdline_configs(const char *boot_cmdline,
+ struct xbc_config *cfgs, int count,
+ int *end_offset)
+{
+ return xbc_parse_cmdline_bootconfig(boot_cmdline, cfgs, count,
+ end_offset, true);
}
#endif /* __KERNEL__ */
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH 3/5] tools/bootconfig: Support multiple named bootconfigs with -i option
2026-11-27 14:46 [RFC PATCH 0/5] bootconfig: Support multiple named bootconfigs in initrd Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 1/5] bootconfig: Add multi-config ID helpers and xbc_init_configs() support Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 2/5] bootconfig: Support stacked initrd bootconfigs and bootconfig= selection Masami Hiramatsu (Google)
@ 2026-11-27 14:46 ` Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 4/5] selftests/ftrace: Add boottime tests for multiple bootconfig selection Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 5/5] Documentation: bootconfig: Document multiple bootconfigs and selection Masami Hiramatsu (Google)
4 siblings, 0 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-27 14:46 UTC (permalink / raw)
To: snambakam, Steven Rostedt, linux-trace-kernel
Cc: linux-kernel, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Andrew Morton, Mathieu Desnoyers, linux-doc,
linux-kselftest
Update tools/bootconfig to attach, inspect, and delete multiple stacked
bootconfigs in an initrd image via the "-i <id[,...]>" option:
- Apply ("-a <config> [-i <id>] <initrd>"):
When "-i <id>" is given, validate <id> and replace the matching block
in place or append a new named block at the tail (up to XBC_CONFIG_MAX
= 32). When "-i" is omitted, always place or move the unnamed block to
index 0 so the unnamed config is guaranteed to be the default block.
When attaching or replacing either a named overlay or the default
block [0], test-merge each overlay with block [0] and emit a warning
if a plain "=" redefinition conflict is detected.
- Delete ("-d [-i <spec>] <initrd>"):
Delete all stacked trailers when "-i" is omitted, or selectively
delete matching IDs/indices and repack the remaining blocks.
- Show / List / Cmdline ("[-l | -C] [-i <spec>] <path>"):
Preserve legacy output when an initrd has a single unnamed block; show
each block with its index and #BOOTCONFIG-ID header when multiple or
named blocks exist; or merge and render the selected subset when
"-i <spec>" is given.
To prevent a partial write or ENOSPC error from wiping out unrelated
stacked configs, write_xbc_blocks() backs up the existing trailer region
and serializes the new trailer tail in memory before writing, rolling
back to the original trailer bytes on failure.
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
tools/bootconfig/main.c | 777 ++++++++++++++++++++++------
tools/bootconfig/test-bootconfig.sh | 116 +++++
2 files changed, 726 insertions(+), 167 deletions(-)
diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index 6f68e02f6385..f3c7321e29d0 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -19,7 +19,7 @@ static void show_xbc_error(const char *data, const char *msg, int pos)
{
int lin = 1, col, i;
- if (pos < 0) {
+ if (pos < 0 || !data) {
pr_err("Error: %s.\n", msg);
return;
}
@@ -36,7 +36,7 @@ static void show_xbc_error(const char *data, const char *msg, int pos)
}
-static int init_xbc_with_error(char *buf, int len)
+static int init_xbc_with_error(const char *buf, int len)
{
const char *msg;
int ret, pos;
@@ -235,72 +235,168 @@ static int pr_errno(const char *msg, int err)
return err;
}
-static int load_xbc_from_initrd(int fd, char **buf)
+struct xbc_block {
+ struct xbc_config cfg;
+ char *raw_buf;
+};
+
+static void free_xbc_blocks(struct xbc_block *blocks, int count)
+{
+ int i;
+
+ for (i = 0; i < count; i++) {
+ free(blocks[i].raw_buf);
+ blocks[i].raw_buf = NULL;
+ }
+}
+
+static int load_xbc_list_from_initrd(int fd, struct xbc_block *blocks,
+ int max_blocks, off_t *base_size)
{
struct stat stat;
- int ret;
+ int ret, total = 0, i;
uint32_t size = 0, data_size, csum = 0, rcsum;
char magic[BOOTCONFIG_MAGIC_LEN];
+ char *buf = NULL;
+ ssize_t rlen;
+ off_t end;
+
+ memset(blocks, 0, sizeof(*blocks) * max_blocks);
ret = fstat(fd, &stat);
if (ret < 0)
return -errno;
- if (stat.st_size < BOOTCONFIG_FOOTER_SIZE)
- return 0;
+ end = stat.st_size;
+ while (end >= (off_t)BOOTCONFIG_FOOTER_SIZE) {
+ if (lseek(fd, end - (off_t)BOOTCONFIG_MAGIC_LEN, SEEK_SET) < 0) {
+ ret = pr_errno("Failed to lseek for magic", -errno);
+ goto err;
+ }
+ rlen = read(fd, magic, BOOTCONFIG_MAGIC_LEN);
+ if (rlen != BOOTCONFIG_MAGIC_LEN) {
+ ret = pr_errno("Failed to read",
+ rlen < 0 ? -errno : -EIO);
+ goto err;
+ }
- if (lseek(fd, -(off_t)BOOTCONFIG_MAGIC_LEN, SEEK_END) < 0)
- return pr_errno("Failed to lseek for magic", -errno);
+ /* Check the bootconfig magic bytes */
+ if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
+ break;
- if (read(fd, magic, BOOTCONFIG_MAGIC_LEN) < 0)
- return pr_errno("Failed to read", -errno);
+ if (lseek(fd, end - (off_t)BOOTCONFIG_FOOTER_SIZE, SEEK_SET) < 0) {
+ ret = pr_errno("Failed to lseek for size", -errno);
+ goto err;
+ }
+ rlen = read(fd, &size, sizeof(uint32_t));
+ if (rlen != sizeof(uint32_t)) {
+ ret = pr_errno("Failed to read size",
+ rlen < 0 ? -errno : -EIO);
+ goto err;
+ }
+ size = le32toh(size);
- /* Check the bootconfig magic bytes */
- if (memcmp(magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN) != 0)
- return 0;
+ rlen = read(fd, &csum, sizeof(uint32_t));
+ if (rlen != sizeof(uint32_t)) {
+ ret = pr_errno("Failed to read checksum",
+ rlen < 0 ? -errno : -EIO);
+ goto err;
+ }
+ csum = le32toh(csum);
+
+ /* Wrong size error */
+ if (size > XBC_DATA_MAX_PADDED ||
+ (off_t)size > end - (off_t)BOOTCONFIG_FOOTER_SIZE) {
+ pr_err("bootconfig size is too big\n");
+ ret = -E2BIG;
+ goto err;
+ }
- if (lseek(fd, -(off_t)BOOTCONFIG_FOOTER_SIZE, SEEK_END) < 0)
- return pr_errno("Failed to lseek for size", -errno);
+ if (lseek(fd, end - (off_t)(size + BOOTCONFIG_FOOTER_SIZE),
+ SEEK_SET) < 0) {
+ ret = pr_errno("Failed to lseek", -errno);
+ goto err;
+ }
- if (read(fd, &size, sizeof(uint32_t)) < 0)
- return pr_errno("Failed to read size", -errno);
- size = le32toh(size);
+ ret = load_xbc_fd(fd, &buf, size);
+ if (ret < 0)
+ goto err;
- if (read(fd, &csum, sizeof(uint32_t)) < 0)
- return pr_errno("Failed to read checksum", -errno);
- csum = le32toh(csum);
+ data_size = xbc_exclude_padding(buf, size);
+ if (data_size > XBC_DATA_MAX) {
+ pr_err("bootconfig size is too big\n");
+ free(buf);
+ ret = -E2BIG;
+ goto err;
+ }
- /* Wrong size error */
- if (size > XBC_DATA_MAX_PADDED ||
- size > stat.st_size - BOOTCONFIG_FOOTER_SIZE) {
- pr_err("bootconfig size is too big\n");
- return -E2BIG;
+ /* Wrong Checksum */
+ rcsum = xbc_calc_checksum(buf, size);
+ if (csum != rcsum) {
+ pr_err("checksum error: %u != %u\n", csum, rcsum);
+ free(buf);
+ ret = -EINVAL;
+ goto err;
+ }
+
+ total++;
+ if (total > max_blocks) {
+ pr_err("Too many bootconfigs in initrd (max %d)\n",
+ max_blocks);
+ free(buf);
+ ret = -E2BIG;
+ goto err;
+ }
+
+ if (xbc_parse_id_header(buf, data_size,
+ &blocks[max_blocks - total].cfg) < 0) {
+ pr_err("Invalid bootconfig ID header\n");
+ free(buf);
+ ret = -EINVAL;
+ goto err;
+ }
+ blocks[max_blocks - total].raw_buf = buf;
+ end -= (off_t)(size + BOOTCONFIG_FOOTER_SIZE);
}
- if (lseek(fd, stat.st_size - (size + BOOTCONFIG_FOOTER_SIZE),
- SEEK_SET) < 0)
- return pr_errno("Failed to lseek", -errno);
+ if (total > 0 && total < max_blocks)
+ memmove(blocks, &blocks[max_blocks - total],
+ total * sizeof(*blocks));
- ret = load_xbc_fd(fd, buf, size);
- if (ret < 0)
- return ret;
+ for (i = 0; i < total; i++) {
+ int j;
- data_size = xbc_exclude_padding(*buf, size);
- if (data_size > XBC_DATA_MAX) {
- pr_err("bootconfig size is too big\n");
- return -E2BIG;
+ if (!blocks[i].cfg.name_len)
+ continue;
+ for (j = i + 1; j < total; j++) {
+ if (blocks[i].cfg.name_len == blocks[j].cfg.name_len &&
+ !memcmp(blocks[i].cfg.name, blocks[j].cfg.name,
+ blocks[i].cfg.name_len)) {
+ pr_err("Duplicate bootconfig ID '%.*s' in initrd\n",
+ (int)blocks[i].cfg.name_len,
+ blocks[i].cfg.name);
+ ret = -EINVAL;
+ goto err_all;
+ }
+ }
}
- /* Wrong Checksum */
- rcsum = xbc_calc_checksum(*buf, size);
- if (csum != rcsum) {
- pr_err("checksum error: %u != %u\n", csum, rcsum);
- return -EINVAL;
- }
+ if (base_size)
+ *base_size = end;
- ret = init_xbc_with_error(*buf, data_size);
+ return total;
- return ret < 0 ? ret : size;
+err:
+ if (total > 0) {
+ int n = total > max_blocks ? max_blocks : total;
+
+ free_xbc_blocks(&blocks[max_blocks - n], n);
+ }
+ return ret;
+
+err_all:
+ free_xbc_blocks(blocks, total);
+ return ret;
}
static int show_xbc_kernel_cmdline(void)
@@ -337,11 +433,27 @@ static int show_xbc_kernel_cmdline(void)
return 0;
}
-static int show_xbc(const char *path, bool list, bool render_cmdline)
+static int render_xbc_output(bool list, bool render_cmdline)
{
- int ret, fd;
+ if (render_cmdline)
+ return show_xbc_kernel_cmdline();
+ if (list)
+ xbc_show_list();
+ else
+ xbc_show_compact_tree();
+ return 0;
+}
+
+static int show_xbc(const char *path, bool list, bool render_cmdline,
+ const char *id_spec)
+{
+ struct xbc_block blocks[XBC_CONFIG_MAX];
+ struct xbc_config cfgs[XBC_CONFIG_MAX];
+ int ret, fd, count, i, err_idx = -1, pos;
+ const char *msg;
char *buf = NULL;
struct stat st;
+ bool seeded = false;
ret = stat(path, &st);
if (ret < 0) {
@@ -357,41 +469,283 @@ static int show_xbc(const char *path, bool list, bool render_cmdline)
return ret;
}
- ret = load_xbc_from_initrd(fd, &buf);
+ count = load_xbc_list_from_initrd(fd, blocks, XBC_CONFIG_MAX, NULL);
close(fd);
- if (ret < 0) {
- pr_err("Failed to load a boot config from initrd: %d\n", ret);
- goto out;
+ if (count < 0) {
+ pr_err("Failed to load a boot config from initrd: %d\n", count);
+ return count;
+ }
+
+ /* Assume a bootconfig file if no trailer and small enough */
+ if (count == 0) {
+ if (id_spec) {
+ pr_err("No bootconfig found in %s\n", path);
+ return -ENOENT;
+ }
+ if (st.st_size <= XBC_DATA_MAX) {
+ int len = load_xbc_file(path, &buf);
+
+ if (len < 0) {
+ pr_err("Failed to load a boot config: %d\n", len);
+ return len;
+ }
+ if (init_xbc_with_error(buf, len) >= 0) {
+ ret = render_xbc_output(list, render_cmdline);
+ xbc_exit();
+ } else {
+ ret = len ? -EINVAL : 0;
+ }
+ free(buf);
+ return ret > 0 ? 0 : ret;
+ }
+ return render_xbc_output(list, render_cmdline);
}
- /* Assume a bootconfig file if it is enough small */
- if (ret == 0 && st.st_size <= XBC_DATA_MAX) {
- ret = load_xbc_file(path, &buf);
+
+ for (i = 0; i < count; i++)
+ cfgs[i] = blocks[i].cfg;
+
+ if (id_spec || render_cmdline) {
+ if (id_spec) {
+ bool any_enabled = false;
+
+ if (*id_spec == '\0') {
+ pr_err("Error: Empty bootconfig ID specification.\n");
+ ret = -EINVAL;
+ goto out_blocks;
+ }
+ ret = xbc_enable_configs(cfgs, count, id_spec, &seeded);
+ if (ret < 0)
+ goto out_blocks;
+ for (i = 0; i < count; i++) {
+ if (cfgs[i].enabled) {
+ any_enabled = true;
+ break;
+ }
+ }
+ if (!any_enabled) {
+ ret = -ENOENT;
+ goto out_blocks;
+ }
+ } else {
+ cfgs[0].enabled = true;
+ }
+ ret = xbc_init_configs(cfgs, count, &msg, &pos, &err_idx);
if (ret < 0) {
- pr_err("Failed to load a boot config: %d\n", ret);
- goto out;
+ if (err_idx >= 0)
+ pr_err("Error in bootconfig [%d%s%.*s]:\n",
+ err_idx,
+ cfgs[err_idx].name_len ? ":" : "",
+ (int)cfgs[err_idx].name_len,
+ cfgs[err_idx].name);
+ show_xbc_error(err_idx >= 0 ? cfgs[err_idx].data : NULL,
+ msg, pos);
+ goto out_blocks;
+ }
+ ret = render_xbc_output(list, render_cmdline);
+ xbc_exit();
+ } else if (count == 1 && !cfgs[0].name_len) {
+ ret = init_xbc_with_error(cfgs[0].data, cfgs[0].size);
+ if (ret >= 0) {
+ ret = render_xbc_output(list, false);
+ xbc_exit();
+ }
+ } else {
+ for (i = 0; i < count; i++) {
+ if (cfgs[i].name_len)
+ printf("# [%d] #BOOTCONFIG-ID: %.*s\n",
+ i, (int)cfgs[i].name_len, cfgs[i].name);
+ else
+ printf("# [%d] (default)\n", i);
+ ret = init_xbc_with_error(cfgs[i].data, cfgs[i].size);
+ if (ret < 0)
+ goto out_blocks;
+ ret = render_xbc_output(list, false);
+ xbc_exit();
+ if (ret < 0)
+ goto out_blocks;
}
- if (init_xbc_with_error(buf, ret) < 0)
- goto out;
}
- if (render_cmdline)
- ret = show_xbc_kernel_cmdline();
- else if (list)
- xbc_show_list();
- else
- xbc_show_compact_tree();
- if (ret > 0)
- ret = 0;
-out:
- free(buf);
+out_blocks:
+ free_xbc_blocks(blocks, count);
+ return ret > 0 ? 0 : ret;
+}
+
+static ssize_t serialize_xbc_blocks(char **out, off_t base_size,
+ struct xbc_block *blocks, int count)
+{
+ struct {
+ uint32_t size;
+ uint32_t csum;
+ char magic[BOOTCONFIG_MAGIC_LEN];
+ } footer;
+ size_t total_len = 0;
+ off_t cur_off = base_size;
+ char *buf = NULL, *dst;
+ int i;
+
+ static_assert(sizeof(footer) == BOOTCONFIG_FOOTER_SIZE,
+ "bootconfig footer size mismatch");
+
+ *out = NULL;
+ if (count == 0)
+ return 0;
+
+ for (i = 0; i < count; i++) {
+ size_t hdr_len = 0, body_len = blocks[i].cfg.size;
+ size_t unpadded, total_size, padded, write_size;
+ int pad;
+
+ if (blocks[i].cfg.name_len)
+ hdr_len = BOOTCONFIG_ID_PREFIX_LEN +
+ blocks[i].cfg.name_len + 1;
+
+ unpadded = hdr_len + body_len;
+ if (body_len == 0 || blocks[i].cfg.data[body_len - 1] != '\0')
+ unpadded++;
+
+ total_size = (size_t)cur_off + unpadded + BOOTCONFIG_FOOTER_SIZE;
+ pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
+ padded = unpadded + pad;
+ write_size = padded + BOOTCONFIG_FOOTER_SIZE;
+ total_len += write_size;
+ cur_off += (off_t)write_size;
+ }
+
+ buf = calloc(total_len, 1);
+ if (!buf)
+ return -ENOMEM;
+
+ dst = buf;
+ cur_off = base_size;
+ for (i = 0; i < count; i++) {
+ size_t hdr_len = 0, body_len = blocks[i].cfg.size;
+ size_t unpadded, total_size, padded, write_size;
+ uint32_t csum;
+ int pad;
+
+ if (blocks[i].cfg.name_len) {
+ memcpy(dst, BOOTCONFIG_ID_PREFIX,
+ BOOTCONFIG_ID_PREFIX_LEN);
+ memcpy(dst + BOOTCONFIG_ID_PREFIX_LEN,
+ blocks[i].cfg.name, blocks[i].cfg.name_len);
+ hdr_len = BOOTCONFIG_ID_PREFIX_LEN +
+ blocks[i].cfg.name_len;
+ dst[hdr_len++] = '\n';
+ }
+
+ if (body_len)
+ memcpy(dst + hdr_len, blocks[i].cfg.data, body_len);
+
+ unpadded = hdr_len + body_len;
+ if (body_len == 0 || blocks[i].cfg.data[body_len - 1] != '\0')
+ unpadded++;
+
+ csum = xbc_calc_checksum(dst, unpadded);
+
+ total_size = (size_t)cur_off + unpadded + BOOTCONFIG_FOOTER_SIZE;
+ pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
+ padded = unpadded + pad;
+
+ footer.size = htole32((uint32_t)padded);
+ footer.csum = htole32(csum);
+ memcpy(footer.magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
+ memcpy(dst + padded, &footer, BOOTCONFIG_FOOTER_SIZE);
+
+ write_size = padded + BOOTCONFIG_FOOTER_SIZE;
+ dst += write_size;
+ cur_off += (off_t)write_size;
+ }
+
+ *out = buf;
+ return (ssize_t)total_len;
+}
+
+static int write_xbc_blocks(int fd, off_t base_size, struct xbc_block *blocks,
+ int count, const char *path)
+{
+ struct stat st;
+ char *old_tail = NULL, *new_tail = NULL;
+ ssize_t old_len = 0, new_len, rlen, wlen;
+ int ret = 0;
+
+ if (fstat(fd, &st) < 0)
+ return pr_errno("Failed to stat initrd", -errno);
+
+ if (st.st_size > base_size) {
+ old_len = st.st_size - base_size;
+ old_tail = malloc(old_len);
+ if (!old_tail)
+ return -ENOMEM;
+ rlen = pread(fd, old_tail, old_len, base_size);
+ if (rlen != old_len) {
+ free(old_tail);
+ return pr_errno("Failed to backup existing bootconfigs",
+ rlen < 0 ? -errno : -EIO);
+ }
+ }
+
+ new_len = serialize_xbc_blocks(&new_tail, base_size, blocks, count);
+ if (new_len < 0) {
+ free(old_tail);
+ return (int)new_len;
+ }
+
+ if (new_len > 0) {
+ wlen = pwrite(fd, new_tail, new_len, base_size);
+ if (wlen != new_len) {
+ ret = wlen < 0 ? -errno : -ENOSPC;
+ pr_err("Failed to apply a boot config: %d\n", ret);
+ goto out_rollback;
+ }
+ }
+
+ if (ftruncate(fd, base_size + new_len) < 0) {
+ ret = -errno;
+ pr_err("Failed to truncate initrd: %d\n", ret);
+ goto out_rollback;
+ }
+
+ free(new_tail);
+ free(old_tail);
+ return 0;
+
+out_rollback:
+ if (old_len > 0) {
+ wlen = pwrite(fd, old_tail, old_len, base_size);
+ if (wlen != old_len) {
+ int rerr = wlen < 0 ? -errno : -ENOSPC;
+
+ pr_err("Failed to rollback the write error: %d\n", rerr);
+ pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
+ goto out_free;
+ }
+ }
+ if (ftruncate(fd, base_size + old_len) < 0) {
+ int rerr = -errno;
+
+ pr_err("Failed to rollback the write error: %d\n", rerr);
+ pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
+ }
+out_free:
+ free(new_tail);
+ free(old_tail);
return ret;
}
-static int delete_xbc(const char *path)
+static int delete_xbc(const char *path, const char *id_spec)
{
- struct stat stat;
- int ret = 0, fd, size;
- char *buf = NULL;
+ struct xbc_block blocks[XBC_CONFIG_MAX];
+ struct xbc_block kept[XBC_CONFIG_MAX];
+ struct xbc_config cfgs[XBC_CONFIG_MAX];
+ off_t base_size = 0;
+ int ret = 0, fd, count, kept_count = 0, i;
+ bool seeded = false;
+
+ if (id_spec && (*id_spec == '\0' || *id_spec == '+')) {
+ pr_err("Invalid delete ID specification '%s'\n", id_spec);
+ return -EINVAL;
+ }
fd = open(path, O_RDWR);
if (fd < 0) {
@@ -400,140 +754,224 @@ static int delete_xbc(const char *path)
return ret;
}
- size = load_xbc_from_initrd(fd, &buf);
- if (size < 0) {
- ret = size;
+ count = load_xbc_list_from_initrd(fd, blocks, XBC_CONFIG_MAX, &base_size);
+ if (count < 0) {
+ ret = count;
pr_err("Failed to load a boot config from initrd: %d\n", ret);
- } else if (size > 0) {
- ret = fstat(fd, &stat);
- if (!ret)
- ret = ftruncate(fd, stat.st_size
- - size - BOOTCONFIG_FOOTER_SIZE);
- if (ret)
+ goto out;
+ }
+
+ if (count == 0) {
+ if (id_spec) {
+ pr_err("No bootconfig found in %s\n", path);
+ ret = -ENOENT;
+ }
+ goto out;
+ }
+
+ if (!id_spec) {
+ if (ftruncate(fd, base_size) < 0)
ret = -errno;
- } /* Ignore if there is no boot config in initrd */
+ goto out_free;
+ }
- close(fd);
- free(buf);
+ for (i = 0; i < count; i++)
+ cfgs[i] = blocks[i].cfg;
+
+ ret = xbc_enable_configs(cfgs, count, id_spec, &seeded);
+ if (ret < 0)
+ goto out_free;
+
+ for (i = 0; i < count; i++) {
+ if (!cfgs[i].enabled)
+ kept[kept_count++] = blocks[i];
+ }
+
+ ret = write_xbc_blocks(fd, base_size, kept, kept_count, path);
+out_free:
+ free_xbc_blocks(blocks, count);
+out:
+ close(fd);
return ret;
}
-static int apply_xbc(const char *path, const char *xbc_path)
+static int apply_xbc(const char *path, const char *xbc_path, const char *id)
{
- struct {
- uint32_t size;
- uint32_t csum;
- char magic[BOOTCONFIG_MAGIC_LEN];
- } footer;
- char *buf, *data;
- size_t total_size;
- struct stat stat;
- uint32_t size, csum;
- int pad;
- int ret, fd;
+ struct xbc_block blocks[XBC_CONFIG_MAX];
+ char *buf = NULL;
+ off_t base_size = 0;
+ size_t hdr_len = 0, body_size, block_size;
+ uint32_t csum;
+ int ret, fd, count, i, target = -1;
+
+ if (id && !xbc_valid_id(id, strlen(id))) {
+ pr_err("Invalid bootconfig ID '%s'\n", id);
+ return -EINVAL;
+ }
ret = load_xbc_file(xbc_path, &buf);
if (ret < 0) {
pr_err("Failed to load %s : %d\n", xbc_path, ret);
return ret;
}
- size = ret;
- if (size == 0 || buf[size - 1] != '\0')
- size++;
- csum = xbc_calc_checksum(buf, size);
+ body_size = ret;
+ if (body_size >= BOOTCONFIG_ID_PREFIX_LEN &&
+ !memcmp(buf, BOOTCONFIG_ID_PREFIX, BOOTCONFIG_ID_PREFIX_LEN)) {
+ pr_err("Input file %s must not contain %s header; use -i <id>\n",
+ xbc_path, BOOTCONFIG_ID_PREFIX);
+ free(buf);
+ return -EINVAL;
+ }
+
+ if (body_size == 0 || buf[body_size - 1] != '\0')
+ body_size++;
- /* Verify the data format */
- ret = init_xbc_with_error(buf, size);
+ if (id)
+ hdr_len = BOOTCONFIG_ID_PREFIX_LEN + strlen(id) + 1;
+ block_size = hdr_len + body_size;
+ if (block_size > XBC_DATA_MAX) {
+ pr_err("%s size is too big%s\n", xbc_path,
+ id ? " with ID header" : "");
+ free(buf);
+ return -E2BIG;
+ }
+
+ /* Verify the data format standalone */
+ ret = init_xbc_with_error(buf, body_size);
if (ret < 0) {
free(buf);
return ret;
}
- /* Backup the bootconfig data */
- data = calloc(size + BOOTCONFIG_ALIGN + BOOTCONFIG_FOOTER_SIZE, 1);
- if (!data) {
- free(buf);
- return -ENOMEM;
+ csum = xbc_calc_checksum(buf, body_size);
+ if (id) {
+ char hdr[BOOTCONFIG_ID_PREFIX_LEN + BOOTCONFIG_ID_MAX + 2];
+
+ snprintf(hdr, sizeof(hdr), "%s%s\n", BOOTCONFIG_ID_PREFIX, id);
+ csum += xbc_calc_checksum(hdr, hdr_len);
}
- memcpy(data, buf, size);
printf("Apply %s to %s\n", xbc_path, path);
+ if (id)
+ printf("\tID: %s\n", id);
xbc_get_info(&ret, NULL);
printf("\tNumber of nodes: %d\n", ret);
- printf("\tSize: %u bytes\n", (unsigned int)size);
+ printf("\tSize: %zu bytes\n", block_size);
printf("\tChecksum: %u\n", (unsigned int)csum);
/* TODO: Check the options by schema */
xbc_exit();
- free(buf);
- /* Remove old boot config if exists */
- ret = delete_xbc(path);
- if (ret < 0) {
- pr_err("Failed to delete previous boot config: %d\n", ret);
- free(data);
- return ret;
- }
-
- /* Apply new one */
- fd = open(path, O_RDWR | O_APPEND);
+ fd = open(path, O_RDWR);
if (fd < 0) {
ret = -errno;
pr_err("Failed to open %s: %d\n", path, ret);
- free(data);
+ free(buf);
return ret;
}
- /* TODO: Ensure the @path is initramfs/initrd image */
- if (fstat(fd, &stat) < 0) {
- ret = -errno;
- pr_err("Failed to get the size of %s\n", path);
- goto out;
- }
-
- /* To align up the total size to BOOTCONFIG_ALIGN, get padding size */
- total_size = stat.st_size + size + BOOTCONFIG_FOOTER_SIZE;
- pad = ((total_size + BOOTCONFIG_ALIGN - 1) & (~BOOTCONFIG_ALIGN_MASK)) - total_size;
- size += pad;
-
- /* Add a footer */
- footer.size = htole32(size);
- footer.csum = htole32(csum);
- memcpy(footer.magic, BOOTCONFIG_MAGIC, BOOTCONFIG_MAGIC_LEN);
- static_assert(sizeof(footer) == BOOTCONFIG_FOOTER_SIZE,
- "bootconfig footer size mismatch");
- memcpy(data + size, &footer, BOOTCONFIG_FOOTER_SIZE);
- total_size = size + BOOTCONFIG_FOOTER_SIZE;
+ count = load_xbc_list_from_initrd(fd, blocks, XBC_CONFIG_MAX, &base_size);
+ if (count < 0) {
+ ret = count;
+ pr_err("Failed to load existing boot configs: %d\n", ret);
+ free(buf);
+ close(fd);
+ return ret;
+ }
- ret = write(fd, data, total_size);
- if (ret < 0) {
- ret = -errno;
- pr_err("Failed to apply a boot config: %d\n", ret);
- goto out;
+ if (!id) {
+ for (i = 0; i < count; i++) {
+ if (!blocks[i].cfg.name_len) {
+ target = i;
+ break;
+ }
+ }
+ if (target >= 0) {
+ free(blocks[target].raw_buf);
+ if (target > 0)
+ memmove(&blocks[1], &blocks[0],
+ target * sizeof(*blocks));
+ target = 0;
+ } else {
+ if (count >= XBC_CONFIG_MAX) {
+ pr_err("Too many bootconfigs in %s (max %d)\n",
+ path, XBC_CONFIG_MAX);
+ ret = -E2BIG;
+ goto out_err;
+ }
+ if (count > 0)
+ memmove(&blocks[1], &blocks[0],
+ count * sizeof(*blocks));
+ target = 0;
+ count++;
+ }
+ blocks[target].cfg.name = "";
+ blocks[target].cfg.name_len = 0;
+ } else {
+ size_t id_len = strlen(id);
+
+ for (i = 0; i < count; i++) {
+ if (blocks[i].cfg.name_len == id_len &&
+ !memcmp(blocks[i].cfg.name, id, id_len)) {
+ target = i;
+ break;
+ }
+ }
+ if (target >= 0) {
+ free(blocks[target].raw_buf);
+ } else {
+ if (count >= XBC_CONFIG_MAX) {
+ pr_err("Too many bootconfigs in %s (max %d)\n",
+ path, XBC_CONFIG_MAX);
+ ret = -E2BIG;
+ goto out_err;
+ }
+ target = count++;
+ }
+ blocks[target].cfg.name = id;
+ blocks[target].cfg.name_len = id_len;
}
- if ((size_t)ret != total_size) {
- pr_err("Failed to apply a boot config: %d\n", ret);
- goto out_rollback;
+
+ blocks[target].raw_buf = buf;
+ blocks[target].cfg.data = buf;
+ blocks[target].cfg.size = body_size;
+ blocks[target].cfg.enabled = false;
+ buf = NULL;
+
+ if (count > 1) {
+ int lo = target ? target : 1;
+ int hi = target ? target + 1 : count;
+
+ for (i = lo; i < hi; i++) {
+ struct xbc_config pair[2] = {
+ blocks[0].cfg,
+ blocks[i].cfg,
+ };
+ const char *emsg = NULL;
+ int epos = -1, eidx = -1;
+
+ pair[0].enabled = true;
+ pair[1].enabled = true;
+ if (xbc_init_configs(pair, 2, &emsg, &epos, &eidx) < 0)
+ fprintf(stderr,
+ "Warning: merging [%d%s%.*s] with default config [0]: %s\n",
+ i, blocks[i].cfg.name_len ? ":" : "",
+ (int)blocks[i].cfg.name_len,
+ blocks[i].cfg.name,
+ emsg ? emsg : "merge conflict");
+ else
+ xbc_exit();
+ }
}
- ret = 0;
-out:
- close(fd);
- free(data);
+ ret = write_xbc_blocks(fd, base_size, blocks, count, path);
+out_err:
+ free(buf);
+ free_xbc_blocks(blocks, count);
+ close(fd);
return ret;
-
-out_rollback:
- /* Map the partial write to -ENOSPC */
- if (ret >= 0)
- ret = -ENOSPC;
- if (ftruncate(fd, stat.st_size) < 0) {
- ret = -errno;
- pr_err("Failed to rollback the write error: %d\n", ret);
- pr_err("The initrd %s may be corrupted. Recommend to rebuild.\n", path);
- }
- goto out;
}
static int usage(void)
@@ -544,6 +982,7 @@ static int usage(void)
" Options:\n"
" -a <config>: Apply boot config to initrd\n"
" -d : Delete boot config file from initrd\n"
+ " -i <id[,...]> : Specify boot config ID or index\n"
" -l : list boot config in initrd or file\n"
" -C : render the kernel.* subtree as a flat cmdline\n"
" string (suitable for embedding in a kernel image)\n"
@@ -556,11 +995,12 @@ int main(int argc, char **argv)
{
char *path = NULL;
char *apply = NULL;
+ char *id = NULL;
bool render_cmdline = false;
bool delete = false, list = false;
int opt;
- while ((opt = getopt(argc, argv, "hda:lC")) != -1) {
+ while ((opt = getopt(argc, argv, "hda:i:lC")) != -1) {
switch (opt) {
case 'd':
delete = true;
@@ -568,6 +1008,9 @@ int main(int argc, char **argv)
case 'a':
apply = optarg;
break;
+ case 'i':
+ id = optarg;
+ break;
case 'l':
list = true;
break;
@@ -593,9 +1036,9 @@ int main(int argc, char **argv)
path = argv[optind];
if (apply)
- return apply_xbc(path, apply);
+ return apply_xbc(path, apply, id);
else if (delete)
- return delete_xbc(path);
+ return delete_xbc(path, id);
- return show_xbc(path, list, render_cmdline);
+ return show_xbc(path, list, render_cmdline, id);
}
diff --git a/tools/bootconfig/test-bootconfig.sh b/tools/bootconfig/test-bootconfig.sh
index a93a40d11d27..597beadc5ce3 100755
--- a/tools/bootconfig/test-bootconfig.sh
+++ b/tools/bootconfig/test-bootconfig.sh
@@ -279,6 +279,122 @@ for i in samples/good-* ; do
xpass diff $x $TEMPCONF
done
+echo "=== multiple bootconfig tests ==="
+dd if=/dev/zero of=$INITRD bs=4097 count=1 2>/dev/null
+orig_size=$(wc -c < $INITRD)
+
+printf 'kernel.root = /dev/sda1\nfoo = a\n' > $TEMPCONF
+echo "ID validation tests"
+xfail $BOOTCONF -i "0bad" -a $TEMPCONF $INITRD
+xfail $BOOTCONF -i "9" -a $TEMPCONF $INITRD
+xfail $BOOTCONF -i "a,b" -a $TEMPCONF $INITRD
+xfail $BOOTCONF -i "+a" -a $TEMPCONF $INITRD
+xfail $BOOTCONF -i "bad id" -a $TEMPCONF $INITRD
+xfail $BOOTCONF -i "12345678901234567890123456789012" -a $TEMPCONF $INITRD
+xfail $BOOTCONF -i "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" -a $TEMPCONF $INITRD
+xpass $BOOTCONF -i "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" -a $TEMPCONF $INITRD
+xpass $BOOTCONF -d $INITRD
+xpass test $orig_size -eq $(wc -c < $INITRD)
+
+echo "Apply named and default bootconfigs"
+printf 'kernel.loglevel = 5\nfoo = base\nbar = one\n' > $TEMPCONF
+xpass $BOOTCONF -i _alpha -a $TEMPCONF $INITRD
+printf 'kernel.loglevel := 7\nfoo := debug\nbar += two\n' > $TEMPCONF
+xpass $BOOTCONF -i beta-1.0 -a $TEMPCONF $INITRD
+printf 'kernel.quiet\nfoo = def\nbar = zero\n' > $TEMPCONF
+xpass $BOOTCONF -a $TEMPCONF $INITRD
+
+$BOOTCONF $INITRD > $OUTFILE
+xpass grep -q '\[0\][[:space:]](default)' $OUTFILE
+xpass grep -q '\[1\][[:space:]]#BOOTCONFIG-ID:[[:space:]]_alpha' $OUTFILE
+xpass grep -q '\[2\][[:space:]]#BOOTCONFIG-ID:[[:space:]]beta-1.0' $OUTFILE
+
+echo "Replace existing named bootconfig in place"
+printf 'kernel.loglevel = 4\nfoo = alpha_v2\nbar = one\n' > $TEMPCONF
+xpass $BOOTCONF -i _alpha -a $TEMPCONF $INITRD 2> $OUTFILE
+xpass grep -q 'Warning:[[:space:]]merging' $OUTFILE
+$BOOTCONF $INITRD > $OUTFILE
+xpass grep -q 'alpha_v2' $OUTFILE
+xpass grep -q '\[1\][[:space:]]#BOOTCONFIG-ID:[[:space:]]_alpha' $OUTFILE
+xpass grep -q '\[2\][[:space:]]#BOOTCONFIG-ID:[[:space:]]beta-1.0' $OUTFILE
+
+echo "Select and merge by ID, index, and +append"
+$BOOTCONF -l -i _alpha,beta-1.0 $INITRD > $OUTFILE
+xpass grep -q 'foo[[:space:]]=[[:space:]]"debug"' $OUTFILE
+xpass grep -q 'bar[[:space:]]=[[:space:]]"one",[[:space:]]"two"' $OUTFILE
+xpass grep -q 'kernel.loglevel[[:space:]]=[[:space:]]"7"' $OUTFILE
+
+$BOOTCONF -l -i 1,2 $INITRD > $TEMPCONF
+xpass cmp $OUTFILE $TEMPCONF
+
+$BOOTCONF -C -i _alpha,beta-1.0 $INITRD > $OUTFILE
+xpass grep -q 'loglevel=7' $OUTFILE
+
+$BOOTCONF -l -i ,beta-1.0 $INITRD > $OUTFILE
+xpass grep -q 'kernel.quiet[[:space:]]=[[:space:]]""' $OUTFILE
+xpass grep -q 'foo[[:space:]]=[[:space:]]"debug"' $OUTFILE
+xpass grep -q 'bar[[:space:]]=[[:space:]]"zero",[[:space:]]"two"' $OUTFILE
+
+$BOOTCONF -l -i +beta-1.0 $INITRD > $TEMPCONF
+xpass cmp $OUTFILE $TEMPCONF
+
+echo "Missing ID / index error and cross-file redefinition error"
+xfail $BOOTCONF -l -i missing $INITRD
+xfail $BOOTCONF -l -i 9 $INITRD
+xfail $BOOTCONF -l -i ,_alpha $INITRD
+$BOOTCONF -l -i ,_alpha $INITRD 2> $OUTFILE || true
+xpass grep -q 'Error[[:space:]]in[[:space:]]bootconfig[[:space:]]\[1:_alpha\]' $OUTFILE
+
+echo "Selective delete and delete all"
+xfail $BOOTCONF -d -i missing $INITRD
+xpass $BOOTCONF -d -i _alpha $INITRD
+$BOOTCONF $INITRD > $OUTFILE
+xpass grep -q '\[0\][[:space:]](default)' $OUTFILE
+xpass grep -q '\[1\][[:space:]]#BOOTCONFIG-ID:[[:space:]]beta-1.0' $OUTFILE
+xfail grep -q '_alpha' $OUTFILE
+
+xpass $BOOTCONF -d -i 0 $INITRD
+$BOOTCONF $INITRD > $OUTFILE
+xpass grep -q '\[0\][[:space:]]#BOOTCONFIG-ID:[[:space:]]beta-1.0' $OUTFILE
+xfail grep -q 'quiet' $OUTFILE
+
+xpass $BOOTCONF -d $INITRD
+xpass test $orig_size -eq $(wc -c < $INITRD)
+
+echo "Max stacked configs (32) limit check"
+i=0
+while [ $i -lt 32 ]; do
+ printf "k_%d = v_%d\n" $i $i > $TEMPCONF
+ if ! $BOOTCONF -i "cfg_$i" -a $TEMPCONF $INITRD > /dev/null; then
+ break
+ fi
+ i=$((i + 1))
+done
+xpass test $i -eq 32
+printf "k_extra = 1\n" > $TEMPCONF
+xfail $BOOTCONF -i cfg_32 -a $TEMPCONF $INITRD
+$BOOTCONF -l -i 31 $INITRD > $OUTFILE
+xpass grep -q 'k_31[[:space:]]=[[:space:]]"v_31"' $OUTFILE
+xpass $BOOTCONF -d $INITRD
+xpass test $orig_size -eq $(wc -c < $INITRD)
+
+echo "Total stacked size > 32KB with valid per-selection size"
+printf "big1 = \"" > $TEMPCONF
+dd if=/dev/zero bs=1 count=20000 2>/dev/null | tr '\0' 'a' >> $TEMPCONF
+printf "\"\n" >> $TEMPCONF
+xpass $BOOTCONF -i big1 -a $TEMPCONF $INITRD
+printf "big2 = \"" > $TEMPCONF
+dd if=/dev/zero bs=1 count=20000 2>/dev/null | tr '\0' 'b' >> $TEMPCONF
+printf "\"\n" >> $TEMPCONF
+xpass $BOOTCONF -i big2 -a $TEMPCONF $INITRD
+$BOOTCONF -l -i big1 $INITRD > $OUTFILE
+xpass grep -q 'big1[[:space:]]=' $OUTFILE
+$BOOTCONF -l -i big2 $INITRD > $OUTFILE
+xpass grep -q 'big2[[:space:]]=' $OUTFILE
+xfail $BOOTCONF -l -i big1,big2 $INITRD
+xpass $BOOTCONF -d $INITRD
+xpass test $orig_size -eq $(wc -c < $INITRD)
+
echo
echo "=== Summary ==="
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH 4/5] selftests/ftrace: Add boottime tests for multiple bootconfig selection
2026-11-27 14:46 [RFC PATCH 0/5] bootconfig: Support multiple named bootconfigs in initrd Masami Hiramatsu (Google)
` (2 preceding siblings ...)
2026-11-27 14:46 ` [RFC PATCH 3/5] tools/bootconfig: Support multiple named bootconfigs with -i option Masami Hiramatsu (Google)
@ 2026-11-27 14:46 ` Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 5/5] Documentation: bootconfig: Document multiple bootconfigs and selection Masami Hiramatsu (Google)
4 siblings, 0 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-27 14:46 UTC (permalink / raw)
To: snambakam, Steven Rostedt, linux-trace-kernel
Cc: linux-kernel, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Andrew Morton, Mathieu Desnoyers, linux-doc,
linux-kselftest
Extend the ftrace boottime QEMU test harness to attach multiple named
bootconfig files and add tests covering multi-bootconfig boot behavior:
- Update run_boottime_test.sh and README to attach named
bootconfigs/<name>.<id>.bconf files and "# BOOTCONFIGS:" headers,
dump /proc/bootconfig from /init when dump_bconf is set, and preserve
explicit bootconfig= cmdline overrides.
- Add tests verifying:
- bconf-multi-01-default: bare bootconfig loads only index 0.
- bconf-multi-02-select: bootconfig=base,dbg merges and overrides keys.
- bconf-multi-03-append: bootconfig=+dbg appends onto default index 0.
- bconf-multi-04-clear: bootconfig= clears an earlier bootconfig option.
- bconf-multi-05-conflict: conflicting "=" redefinition via
"# BOOTCONFIGS:" reports the [1:conflict] parse error in dmesg.
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
.../testing/selftests/ftrace/boottime/README | 4 +-
.../bootconfigs/bconf-multi-01-default.bconf | 5 +
.../bconf-multi-01-default.debug.bconf | 5 +
.../bconf-multi-02-select.alt.bconf | 5 +
.../bconf-multi-02-select.base.bconf | 7 ++
.../bconf-multi-02-select.dbg.bconf | 7 ++
.../bootconfigs/bconf-multi-03-append.bconf | 6 ++
.../bconf-multi-03-append.dbg.bconf | 6 ++
.../bootconfigs/bconf-multi-04-clear.bconf | 5 +
.../bootconfigs/bconf-multi-05-conflict.bconf | 6 ++
.../ftrace/boottime/run_boottime_test.sh | 99 ++++++++++++++++++-
.../boottime/tests/bconf-multi-01-default.sh | 23 +++++
.../boottime/tests/bconf-multi-02-select.sh | 53 ++++++++++
.../boottime/tests/bconf-multi-03-append.sh | 26 +++++
.../boottime/tests/bconf-multi-04-clear.sh | 26 +++++
.../boottime/tests/bconf-multi-05-conflict.sh | 22 +++++
16 files changed, 300 insertions(+), 5 deletions(-)
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-01-default.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-01-default.debug.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.alt.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.base.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.dbg.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-03-append.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-03-append.dbg.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-04-clear.bconf
create mode 100644 tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-05-conflict.bconf
create mode 100755 tools/testing/selftests/ftrace/boottime/tests/bconf-multi-01-default.sh
create mode 100755 tools/testing/selftests/ftrace/boottime/tests/bconf-multi-02-select.sh
create mode 100755 tools/testing/selftests/ftrace/boottime/tests/bconf-multi-03-append.sh
create mode 100755 tools/testing/selftests/ftrace/boottime/tests/bconf-multi-04-clear.sh
create mode 100755 tools/testing/selftests/ftrace/boottime/tests/bconf-multi-05-conflict.sh
diff --git a/tools/testing/selftests/ftrace/boottime/README b/tools/testing/selftests/ftrace/boottime/README
index 1eb0191d75d4..eff7eb2c42d9 100644
--- a/tools/testing/selftests/ftrace/boottime/README
+++ b/tools/testing/selftests/ftrace/boottime/README
@@ -16,7 +16,9 @@ results according to TAP version 13 format.
Test Specification Format
-------------------------
For each test script `tests/<name>.sh`, the harness dynamically detects:
-- `bootconfigs/<name>.bconf` or `<name>.bconf`: Bootconfig configuration file.
+- `bootconfigs/<name>.bconf` or `<name>.bconf`: Bootconfig configuration file (default / index 0).
+- `bootconfigs/<name>.<id>.bconf` or `<name>.<id>.bconf`: Named bootconfig configuration file attached with `-i <id>`.
+- `# BOOTCONFIGS: [<id>:]<file> ...` header in script: Explicit list of unnamed or named (`<id>:<file>`) bootconfig files to attach.
- `cmdlines/<name>.cmdline` or `# CMDLINE: <opts>` header in script: Kernel boot parameters.
- `qemuopts/<name>.qemuopts` or `# QEMUOPTS: <opts>` header in script: Additional QEMU CLI arguments.
- `# APPLETS: <applets>` header in script: Additional BusyBox applet symlinks to create in initramfs.
diff --git a/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-01-default.bconf b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-01-default.bconf
new file mode 100644
index 000000000000..b20e0371d43d
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-01-default.bconf
@@ -0,0 +1,5 @@
+ftrace.instance.def_inst {
+ event.sched.sched_switch {
+ enable
+ }
+}
diff --git a/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-01-default.debug.bconf b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-01-default.debug.bconf
new file mode 100644
index 000000000000..7c6d1aba99d2
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-01-default.debug.bconf
@@ -0,0 +1,5 @@
+ftrace.instance.dbg_inst {
+ event.sched.sched_wakeup {
+ enable
+ }
+}
diff --git a/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.alt.bconf b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.alt.bconf
new file mode 100644
index 000000000000..613fb26400bf
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.alt.bconf
@@ -0,0 +1,5 @@
+ftrace.instance.alt_inst {
+ event.sched.sched_switch {
+ enable
+ }
+}
diff --git a/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.base.bconf b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.base.bconf
new file mode 100644
index 000000000000..033aa3676150
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.base.bconf
@@ -0,0 +1,7 @@
+kernel.bconf_test_arg = from_base
+ftrace.instance.base_inst {
+ buffer_size = 512K
+ event.sched.sched_switch {
+ enable
+ }
+}
diff --git a/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.dbg.bconf b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.dbg.bconf
new file mode 100644
index 000000000000..3117e2b1305c
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-02-select.dbg.bconf
@@ -0,0 +1,7 @@
+kernel.bconf_test_arg := from_dbg
+ftrace.instance.base_inst.buffer_size := 2048K
+ftrace.instance.dbg_inst {
+ event.sched.sched_wakeup {
+ enable
+ }
+}
diff --git a/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-03-append.bconf b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-03-append.bconf
new file mode 100644
index 000000000000..23b54849a151
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-03-append.bconf
@@ -0,0 +1,6 @@
+kernel.bconf_append_key = base_val
+ftrace.instance.app_inst {
+ event.sched.sched_switch {
+ enable
+ }
+}
diff --git a/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-03-append.dbg.bconf b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-03-append.dbg.bconf
new file mode 100644
index 000000000000..6d4b9dfbf4c3
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-03-append.dbg.bconf
@@ -0,0 +1,6 @@
+kernel.bconf_append_key += dbg_val
+ftrace.instance.app_inst {
+ event.sched.sched_wakeup {
+ enable
+ }
+}
diff --git a/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-04-clear.bconf b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-04-clear.bconf
new file mode 100644
index 000000000000..78a1304bce05
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-04-clear.bconf
@@ -0,0 +1,5 @@
+ftrace.instance.cleared_inst {
+ event.sched.sched_switch {
+ enable
+ }
+}
diff --git a/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-05-conflict.bconf b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-05-conflict.bconf
new file mode 100644
index 000000000000..7e8bced3c10d
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/bootconfigs/bconf-multi-05-conflict.bconf
@@ -0,0 +1,6 @@
+kernel.bconf_test_arg = conflict_redef
+ftrace.instance.conflict_inst {
+ event.sched.sched_wakeup {
+ enable
+ }
+}
diff --git a/tools/testing/selftests/ftrace/boottime/run_boottime_test.sh b/tools/testing/selftests/ftrace/boottime/run_boottime_test.sh
index 6e8593d5a2c5..7cc0f2da2838 100755
--- a/tools/testing/selftests/ftrace/boottime/run_boottime_test.sh
+++ b/tools/testing/selftests/ftrace/boottime/run_boottime_test.sh
@@ -204,6 +204,25 @@ double_timeout() {
fi
}
+apply_bootconfig() {
+ local test_name="$1"
+ local initramfs="$2"
+ local b_path="$3"
+ local b_id="${4:-}"
+ local err_out
+
+ if [ -n "$b_id" ]; then
+ err_out="$("$BOOTCONFIG" -i "$b_id" -a "$b_path" "$initramfs" 2>&1)"
+ else
+ err_out="$("$BOOTCONFIG" -a "$b_path" "$initramfs" 2>&1)"
+ fi || {
+ ktap_test_fail "$test_name (failed to apply $(basename "$b_path"))"
+ [ -n "$err_out" ] && ktap_print_msg "$err_out"
+ return 1
+ }
+ return 0
+}
+
run_single_test() {
local test_script="$1"
local name
@@ -218,6 +237,19 @@ run_single_test() {
bconf_file="$dir/$name.bconf"
fi
+ local named_bconfs=()
+ local f
+ while IFS= read -r f; do
+ [ -n "$f" ] && named_bconfs+=("$f")
+ done < <(
+ for f in "$SCRIPT_DIR/bootconfigs/$name".*.bconf "$dir/$name".*.bconf; do
+ [ -f "$f" ] && printf '%s\n' "$f"
+ done | LC_ALL=C sort
+ )
+
+ local inline_bconfs
+ inline_bconfs="$(grep -E '^# *BOOTCONFIGS:' "$test_script" | sed 's/^# *BOOTCONFIGS://' | xargs || true)"
+
local test_cmdline=""
if [ -f "$SCRIPT_DIR/cmdlines/$name.cmdline" ]; then
test_cmdline="$(cat "$SCRIPT_DIR/cmdlines/$name.cmdline")"
@@ -299,11 +331,20 @@ mount -t proc proc /proc 2>/dev/null
mount -t sysfs sys /sys 2>/dev/null
mount -t tracefs nodev /sys/kernel/tracing 2>/dev/null
+if [ -f /proc/bootconfig ] && grep -q "dump_bconf" /proc/cmdline 2>/dev/null && \
+ ! grep -q "dump_bconf" /bin/check_test.sh 2>/dev/null; then
+ echo "=== /proc/bootconfig ==="
+ cat /proc/bootconfig
+ echo "========================"
+fi
+
/bin/check_test.sh
RET=$?
if [ $RET -eq 0 ]; then
echo "TEST RESULT: PASS"
+elif [ $RET -eq 4 ]; then
+ echo "TEST RESULT: SKIP"
else
echo "TEST RESULT: FAIL"
fi
@@ -320,13 +361,61 @@ EOF
local initramfs="$workdir/initramfs.cpio"
(cd "$rootfs" && find . | cpio -o -H newc --quiet) > "$initramfs"
- if [ -n "$bconf_file" ] && [ -f "$bconf_file" ]; then
- "$BOOTCONFIG" -a "$bconf_file" "$initramfs" > /dev/null
- fi
+ if [ -n "$inline_bconfs" ]; then
+ local item b_id b_file b_path
+ for item in $inline_bconfs; do
+ case "$item" in
+ *:*)
+ b_id="${item%%:*}"
+ b_file="${item#*:}"
+ ;;
+ *)
+ b_id=""
+ b_file="$item"
+ ;;
+ esac
+ if [ -f "$SCRIPT_DIR/bootconfigs/$b_file" ]; then
+ b_path="$SCRIPT_DIR/bootconfigs/$b_file"
+ elif [ -f "$dir/$b_file" ]; then
+ b_path="$dir/$b_file"
+ else
+ ktap_test_fail "$name (bootconfig file '$b_file' not found)"
+ rm -rf "$workdir"
+ trap - EXIT
+ return 0
+ fi
+ if ! apply_bootconfig "$name" "$initramfs" "$b_path" "$b_id"; then
+ rm -rf "$workdir"
+ trap - EXIT
+ return 0
+ fi
+ done
+ else
+ if [ -n "$bconf_file" ] && [ -f "$bconf_file" ]; then
+ if ! apply_bootconfig "$name" "$initramfs" "$bconf_file"; then
+ rm -rf "$workdir"
+ trap - EXIT
+ return 0
+ fi
+ fi
+ for f in "${named_bconfs[@]}"; do
+ local id
+ id="$(basename "$f" .bconf)"
+ id="${id#"$name".}"
+ if ! apply_bootconfig "$name" "$initramfs" "$f" "$id"; then
+ rm -rf "$workdir"
+ trap - EXIT
+ return 0
+ fi
+ done
+ fi
local logfile
- local base_cmdline="bootconfig console=tty0 console=ttyS0 panic=-1"
+ local base_cmdline="console=tty0 console=ttyS0 panic=-1"
+ if ! [[ "$test_cmdline" =~ (^|[[:space:]])bootconfig(=|[[:space:]]|$) ]]; then
+ base_cmdline="bootconfig $base_cmdline"
+ fi
if [ -n "$test_cmdline" ]; then
base_cmdline="$base_cmdline $test_cmdline"
fi
@@ -358,6 +447,8 @@ EOF
if grep -q "TEST RESULT: PASS" "$logfile"; then
ktap_test_pass "$name"
+ elif grep -q "TEST RESULT: SKIP" "$logfile"; then
+ ktap_test_skip "$name"
else
ktap_test_fail "$name"
ktap_print_msg "QEMU Console Output for $name:"
diff --git a/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-01-default.sh b/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-01-default.sh
new file mode 100755
index 000000000000..17a03c1bb0b0
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-01-default.sh
@@ -0,0 +1,23 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# Check bare bootconfig loads only index 0 (default) when multiple configs exist
+TRACEDIR="/sys/kernel/tracing"
+
+if [ ! -d "$TRACEDIR/instances/def_inst" ]; then
+ echo "FAIL: default instance def_inst does not exist"
+ exit 1
+fi
+
+ENABLE=$(cat "$TRACEDIR/instances/def_inst/events/sched/sched_switch/enable")
+if [ "$ENABLE" != "1" ]; then
+ echo "FAIL: def_inst sched_switch is not enabled ($ENABLE)"
+ exit 1
+fi
+
+if [ -d "$TRACEDIR/instances/dbg_inst" ]; then
+ echo "FAIL: unselected instance dbg_inst should not exist"
+ exit 1
+fi
+
+echo "PASS: bconf-multi-01-default"
+exit 0
diff --git a/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-02-select.sh b/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-02-select.sh
new file mode 100755
index 000000000000..e132d8e617d8
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-02-select.sh
@@ -0,0 +1,53 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# CMDLINE: bootconfig=base,dbg
+# Check bootconfig=<id1>,<id2> selects and merges configs with := override
+TRACEDIR="/sys/kernel/tracing"
+
+if [ ! -d "$TRACEDIR/instances/base_inst" ]; then
+ echo "FAIL: instance base_inst does not exist"
+ exit 1
+fi
+
+if [ ! -d "$TRACEDIR/instances/dbg_inst" ]; then
+ echo "FAIL: instance dbg_inst does not exist"
+ exit 1
+fi
+
+if [ -d "$TRACEDIR/instances/alt_inst" ]; then
+ echo "FAIL: unselected instance alt_inst should not exist"
+ exit 1
+fi
+
+BUF_RAW=$(cat "$TRACEDIR/instances/base_inst/buffer_size_kb")
+case "$BUF_RAW" in
+ *"expanded:"*)
+ BUFSIZE=$(echo "$BUF_RAW" | sed -n 's/.*expanded: *\([0-9]*\).*/\1/p')
+ ;;
+ *)
+ BUFSIZE="$BUF_RAW"
+ ;;
+esac
+case "$BUFSIZE" in
+ ''|*[!0-9]*)
+ echo "FAIL: base_inst buffer_size_kb is non-numeric ('$BUF_RAW')"
+ exit 1
+ ;;
+esac
+if [ "$BUFSIZE" -lt 2048 ] || [ "$BUFSIZE" -ge 2100 ]; then
+ echo "FAIL: base_inst buffer_size_kb is '$BUF_RAW', expected ~2048"
+ exit 1
+fi
+
+if ! grep -q 'ftrace.instance.base_inst.buffer_size = "2048K"' /proc/bootconfig; then
+ echo "FAIL: overridden buffer_size not in /proc/bootconfig"
+ exit 1
+fi
+
+if ! grep -q "bconf_test_arg=from_dbg" /proc/cmdline; then
+ echo "FAIL: overridden kernel.bconf_test_arg not in /proc/cmdline"
+ exit 1
+fi
+
+echo "PASS: bconf-multi-02-select"
+exit 0
diff --git a/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-03-append.sh b/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-03-append.sh
new file mode 100755
index 000000000000..64a687662dca
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-03-append.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# CMDLINE: bootconfig=+dbg
+# Check bootconfig=+id appends onto default (index 0) with += array append
+TRACEDIR="/sys/kernel/tracing"
+
+if [ ! -d "$TRACEDIR/instances/app_inst" ]; then
+ echo "FAIL: instance app_inst does not exist"
+ exit 1
+fi
+
+EN1=$(cat "$TRACEDIR/instances/app_inst/events/sched/sched_switch/enable")
+EN2=$(cat "$TRACEDIR/instances/app_inst/events/sched/sched_wakeup/enable")
+if [ "$EN1" != "1" ] || [ "$EN2" != "1" ]; then
+ echo "FAIL: expected both sched_switch ($EN1) and sched_wakeup ($EN2) enabled"
+ exit 1
+fi
+
+if ! grep -q "bconf_append_key=base_val" /proc/cmdline || \
+ ! grep -q "bconf_append_key=dbg_val" /proc/cmdline; then
+ echo "FAIL: appended kernel.bconf_append_key values missing from /proc/cmdline"
+ exit 1
+fi
+
+echo "PASS: bconf-multi-03-append"
+exit 0
diff --git a/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-04-clear.sh b/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-04-clear.sh
new file mode 100755
index 000000000000..eec9ec1206fc
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-04-clear.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# CMDLINE: bootconfig bootconfig=
+# APPLETS: zcat
+# Check explicit empty bootconfig= disables earlier bootconfig selection
+TRACEDIR="/sys/kernel/tracing"
+
+if [ -r /proc/config.gz ] && \
+ zcat /proc/config.gz 2>/dev/null | grep -q '^CONFIG_BOOT_CONFIG_FORCE=y$'; then
+ echo "SKIP: CONFIG_BOOT_CONFIG_FORCE=y forces default bootconfig"
+ exit 4
+fi
+
+if [ -d "$TRACEDIR/instances/cleared_inst" ]; then
+ echo "FAIL: cleared_inst should not exist when bootconfig= clears selection"
+ exit 1
+fi
+
+if [ -n "$(cat /proc/bootconfig)" ]; then
+ echo "FAIL: /proc/bootconfig should be empty"
+ cat /proc/bootconfig
+ exit 1
+fi
+
+echo "PASS: bconf-multi-04-clear"
+exit 0
diff --git a/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-05-conflict.sh b/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-05-conflict.sh
new file mode 100755
index 000000000000..577575d8c7b7
--- /dev/null
+++ b/tools/testing/selftests/ftrace/boottime/tests/bconf-multi-05-conflict.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+# SPDX-License-Identifier: GPL-2.0
+# BOOTCONFIGS: base:bconf-multi-02-select.base.bconf conflict:bconf-multi-05-conflict.bconf
+# CMDLINE: bootconfig=base,conflict
+# APPLETS: dmesg
+# Check conflicting '=' redefinition across configs reports [idx:id] error
+TRACEDIR="/sys/kernel/tracing"
+
+if [ -d "$TRACEDIR/instances/base_inst" ] || \
+ [ -d "$TRACEDIR/instances/conflict_inst" ]; then
+ echo "FAIL: conflicting bootconfigs should not be applied"
+ exit 1
+fi
+
+if ! dmesg | grep -q 'Failed to parse bootconfig \[1:conflict\]: Value is redefined'; then
+ echo "FAIL: expected [1:conflict] parse error in dmesg"
+ dmesg | grep -i bootconfig
+ exit 1
+fi
+
+echo "PASS: bconf-multi-05-conflict"
+exit 0
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC PATCH 5/5] Documentation: bootconfig: Document multiple bootconfigs and selection
2026-11-27 14:46 [RFC PATCH 0/5] bootconfig: Support multiple named bootconfigs in initrd Masami Hiramatsu (Google)
` (3 preceding siblings ...)
2026-11-27 14:46 ` [RFC PATCH 4/5] selftests/ftrace: Add boottime tests for multiple bootconfig selection Masami Hiramatsu (Google)
@ 2026-11-27 14:46 ` Masami Hiramatsu (Google)
4 siblings, 0 replies; 6+ messages in thread
From: Masami Hiramatsu (Google) @ 2026-11-27 14:46 UTC (permalink / raw)
To: snambakam, Steven Rostedt, linux-trace-kernel
Cc: linux-kernel, Jonathan Corbet, Shuah Khan, Randy Dunlap,
Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen, x86,
H. Peter Anvin, Andrew Morton, Mathieu Desnoyers, linux-doc,
linux-kselftest
Document stacked initrd bootconfigs, #BOOTCONFIG-ID naming rules,
index 0 default semantics, XBC_CONFIG_MAX (32) limits, backward
compatibility and single-image requirements, bootconfig= command-line
selection syntax, updated CONFIG_CMDLINE_FROM_BOOTCONFIG selection
rules, and tools/bootconfig -i usage in
Documentation/admin-guide/bootconfig.rst.
Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
---
Documentation/admin-guide/bootconfig.rst | 82 ++++++++++++++++++++++--
1 file changed, 76 insertions(+), 6 deletions(-)
diff --git a/Documentation/admin-guide/bootconfig.rst b/Documentation/admin-guide/bootconfig.rst
index 3d6412458c8b..72be0df4cb3d 100644
--- a/Documentation/admin-guide/bootconfig.rst
+++ b/Documentation/admin-guide/bootconfig.rst
@@ -199,11 +199,11 @@ to/from initrd image. You can build it by the following command::
# make -C tools/bootconfig
To add your boot config file to initrd image, run bootconfig as below
-(Old data is removed automatically if exists)::
+(Old default data is replaced if it exists)::
# tools/bootconfig/bootconfig -a your-config /boot/initrd.img-X.Y.Z
-To remove the config from the image, you can use -d option as below::
+To remove all boot config files from the image, use the -d option as below::
# tools/bootconfig/bootconfig -d /boot/initrd.img-X.Y.Z
@@ -212,6 +212,69 @@ kernel to look for the bootconfig at the end of the initrd file.
Alternatively, build your kernel with the ``CONFIG_BOOT_CONFIG_FORCE``
Kconfig option selected.
+Attaching Multiple Boot Configs to Initrd
+-----------------------------------------
+
+Multiple bootconfig files (up to 32) can be stacked at the end of an initrd
+image, which is useful for Unified Kernel Images (UKIs) that bundle a base
+configuration alongside scenario-specific or debug overlays::
+
+ [initrd][block 0 (default)][block 1 (id1)][block 2 (id2)]...
+
+Each block has its own ``#BOOTCONFIG\n`` footer. Named blocks carry an
+in-band comment header on their first line (``#BOOTCONFIG-ID: <id>\n``).
+Identifiers must be 1 to 31 characters long, start with an ASCII letter or
+underscore (``[A-Za-z_]`` -- leading digits are reserved for 0-based indices),
+and contain only ASCII letters, digits, ``_``, ``-``, or ``.``.
+
+To attach or replace a named bootconfig, pass ``-i <id>`` to ``bootconfig``::
+
+ # tools/bootconfig/bootconfig -a base.bconf /boot/initrd.img-X.Y.Z
+ # tools/bootconfig/bootconfig -i debug -a debug.bconf /boot/initrd.img-X.Y.Z
+ # tools/bootconfig/bootconfig -i trace -a trace.bconf /boot/initrd.img-X.Y.Z
+
+To inspect or preview the merged result on the host, or selectively delete
+configs::
+
+ # tools/bootconfig/bootconfig /boot/initrd.img-X.Y.Z
+ # tools/bootconfig/bootconfig -l -i ,debug /boot/initrd.img-X.Y.Z
+ # tools/bootconfig/bootconfig -d -i debug /boot/initrd.img-X.Y.Z
+
+The block at index ``0`` is the default bootconfig. ``bootconfig -a`` without
+``-i`` always places the unnamed block at index ``0``; deleting index ``0``
+(``bootconfig -d -i 0``) promotes the next block to index ``0`` (making it the
+new default). At boot time, ``bootconfig`` parameters before ``--`` are
+evaluated left-to-right:
+
+- ``bootconfig`` (bare): selects only the default config (index ``0``).
+- ``bootconfig=id1,id2`` (or ``bootconfig=0,2``): selects and merges the
+ listed IDs or 0-based indices in initrd stack order. An empty entry such as
+ ``bootconfig=,debug`` is shorthand for ``0,debug``.
+- ``bootconfig=+debug``: appends ``debug`` onto an earlier ``bootconfig=...``
+ selection on the command line (or onto index ``0`` if no prior
+ ``bootconfig`` parameter appeared).
+- ``bootconfig=`` (empty value): clears earlier selections and loads no
+ bootconfig (unless ``CONFIG_BOOT_CONFIG_FORCE=y`` is set, which forces
+ index ``0`` when no config is selected).
+
+Selected configs are merged in stack order (index ``0`` first, later overlays
+after), so overlays can use ``:=`` and ``+=`` to override or extend base keys.
+
+.. warning::
+
+ - **Older kernels**: A kernel without multi-bootconfig support strips and
+ loads only the outermost trailer (the last overlay) as the sole
+ bootconfig, leaving earlier trailers inside the initrd where
+ ``unpack_to_rootfs()`` will fail with ``Initramfs unpacking failed`` (or
+ fall back to ``CONFIG_BLK_DEV_RAM``).
+ - **Older tools**: An older ``tools/bootconfig -a/-d`` operates only on the
+ outermost trailer and leaves earlier stacked blocks behind.
+ - **Single contiguous trailer stack**: Inner trailers must end at the exact
+ start of the next trailer with no inter-image padding. All stacked
+ bootconfig blocks must therefore be attached at the tail of a single
+ initrd image via ``tools/bootconfig -i`` rather than split across
+ separately padded initrd images or UKI initrd addons.
+
Embedding a Boot Config into Kernel
-----------------------------------
@@ -254,10 +317,12 @@ an architecture that selects ``CONFIG_ARCH_SUPPORTS_CMDLINE_FROM_BOOTCONFIG``.
Currently only x86 selects it; on other architectures the embedded
bootconfig still works, but only through the late runtime parser.
-The same ``bootconfig`` opt-in applies as elsewhere: the rendered keys
-are prepended only when ``bootconfig`` (in any form) appears on the
-kernel command line, or when ``CONFIG_BOOT_CONFIG_FORCE`` is set, which
-defaults to ``y`` when ``CONFIG_BOOT_CONFIG_EMBED`` is set.
+The same ``bootconfig`` selection rules apply as elsewhere: the rendered
+keys are prepended only when index 0 (or the embedded bootconfig's ID) is
+selected via ``bootconfig`` on the kernel command line, or when
+``CONFIG_BOOT_CONFIG_FORCE`` is set (which defaults to ``y`` when
+``CONFIG_BOOT_CONFIG_EMBED`` is set) and no active ``bootconfig=...``
+selection overrides it.
For example, given::
@@ -367,6 +432,11 @@ the padding null characters.)
Anyway, since bootconfig command verifies it when appending a boot config
to initrd image, user can notice it before boot.
+When stacking multiple bootconfig files in an initrd, up to 32 files can be
+attached. Each individual file must fit within 32KB, while the combined size
+of all stacked files in the initrd may exceed 32KB as long as the subset
+selected at boot fits within 32KB and the node limit.
+
Bootconfig APIs
===============
--
2.43.0
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-11-27 14:51 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27 14:46 [RFC PATCH 0/5] bootconfig: Support multiple named bootconfigs in initrd Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 1/5] bootconfig: Add multi-config ID helpers and xbc_init_configs() support Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 2/5] bootconfig: Support stacked initrd bootconfigs and bootconfig= selection Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 3/5] tools/bootconfig: Support multiple named bootconfigs with -i option Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 4/5] selftests/ftrace: Add boottime tests for multiple bootconfig selection Masami Hiramatsu (Google)
2026-11-27 14:46 ` [RFC PATCH 5/5] Documentation: bootconfig: Document multiple bootconfigs and selection Masami Hiramatsu (Google)
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®