Skip to content

Commit 4135542

Browse files
leitaomhiramat
authored andcommitted
tools/bootconfig: render kernel.* subtree as cmdline string with -C
Add a -C option that finds the "kernel" subtree of a bootconfig file and prints it as a flat, space-separated cmdline string by calling the shared xbc_snprint_cmdline() renderer. An empty or absent kernel.* subtree produces empty output and exits successfully. This lets the kernel build embed a bootconfig file as a plain cmdline string at build time, so embedded bootconfig values can reach parse_early_param() during architecture setup without parsing the bootconfig at runtime. The renderer is intentionally limited to the kernel.* subtree: that is the only thing the kernel build needs to embed; init.* and other subtrees keep going through the runtime parser. Example of this new mode: # cat /tmp/test.bconf kernel { foo = bar baz = "hello world" arr = 1, 2 } init.foo = nope # ./tools/bootconfig/bootconfig -C /tmp/test.bconf foo=bar baz="hello world" arr=1 arr=2 % Link: https://lore.kernel.org/all/20260508-bootconfig_using_tools-v1-2-1132219aa773@debian.org/ Signed-off-by: Breno Leitao <leitao@debian.org> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
1 parent 5a643e4 commit 4135542

1 file changed

Lines changed: 52 additions & 8 deletions

File tree

tools/bootconfig/main.c

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,41 @@ static int init_xbc_with_error(char *buf, int len)
286286
return ret;
287287
}
288288

289-
static int show_xbc(const char *path, bool list)
289+
static int show_xbc_kernel_cmdline(void)
290+
{
291+
struct xbc_node *root;
292+
char *buf = NULL;
293+
int len, ret;
294+
295+
root = xbc_find_node("kernel");
296+
if (!root)
297+
return 0; /* no kernel.* keys: emit empty output */
298+
299+
len = xbc_snprint_cmdline(NULL, 0, root);
300+
if (len < 0) {
301+
pr_err("Failed to size cmdline output: %d\n", len);
302+
return len;
303+
}
304+
if (len == 0)
305+
return 0;
306+
307+
buf = malloc(len + 1);
308+
if (!buf)
309+
return -ENOMEM;
310+
311+
ret = xbc_snprint_cmdline(buf, len + 1, root);
312+
if (ret < 0) {
313+
pr_err("Failed to render cmdline output: %d\n", ret);
314+
free(buf);
315+
return ret;
316+
}
317+
318+
fputs(buf, stdout);
319+
free(buf);
320+
return 0;
321+
}
322+
323+
static int show_xbc(const char *path, bool list, bool render_cmdline)
290324
{
291325
int ret, fd;
292326
char *buf = NULL;
@@ -322,11 +356,14 @@ static int show_xbc(const char *path, bool list)
322356
if (init_xbc_with_error(buf, ret) < 0)
323357
goto out;
324358
}
325-
if (list)
359+
if (render_cmdline)
360+
ret = show_xbc_kernel_cmdline();
361+
else if (list)
326362
xbc_show_list();
327363
else
328364
xbc_show_compact_tree();
329-
ret = 0;
365+
if (ret > 0)
366+
ret = 0;
330367
out:
331368
free(buf);
332369

@@ -486,7 +523,10 @@ static int usage(void)
486523
" Options:\n"
487524
" -a <config>: Apply boot config to initrd\n"
488525
" -d : Delete boot config file from initrd\n"
489-
" -l : list boot config in initrd or file\n\n"
526+
" -l : list boot config in initrd or file\n"
527+
" -C : render the kernel.* subtree as a flat cmdline\n"
528+
" string (suitable for embedding in a kernel image)\n"
529+
" and print it to stdout\n\n"
490530
" If no option is given, show the bootconfig in the given file.\n");
491531
return -1;
492532
}
@@ -495,10 +535,11 @@ int main(int argc, char **argv)
495535
{
496536
char *path = NULL;
497537
char *apply = NULL;
538+
bool render_cmdline = false;
498539
bool delete = false, list = false;
499540
int opt;
500541

501-
while ((opt = getopt(argc, argv, "hda:l")) != -1) {
542+
while ((opt = getopt(argc, argv, "hda:lC")) != -1) {
502543
switch (opt) {
503544
case 'd':
504545
delete = true;
@@ -509,14 +550,17 @@ int main(int argc, char **argv)
509550
case 'l':
510551
list = true;
511552
break;
553+
case 'C':
554+
render_cmdline = true;
555+
break;
512556
case 'h':
513557
default:
514558
return usage();
515559
}
516560
}
517561

518-
if ((apply && delete) || (delete && list) || (apply && list)) {
519-
pr_err("Error: You can give one of -a, -d or -l at once.\n");
562+
if ((!!apply + !!delete + !!list + !!render_cmdline) > 1) {
563+
pr_err("Error: You can give one of -a, -d, -l or -C at once.\n");
520564
return usage();
521565
}
522566

@@ -532,5 +576,5 @@ int main(int argc, char **argv)
532576
else if (delete)
533577
return delete_xbc(path);
534578

535-
return show_xbc(path, list);
579+
return show_xbc(path, list, render_cmdline);
536580
}

0 commit comments

Comments
 (0)