Skip to content

Commit 4f7e890

Browse files
committed
Merge tag 'bootconfig-v7.2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace
Pull bootconfig updates from Masami Hiramatsu: - bootconfig: move xbc_snprint_cmdline() to lib/bootconfig.c Move the xbc_snprint_cmdline() function and its buffer from main.c to the shared lib/bootconfig.c parser library so it can be reused by userspace tools. - render kernel.* subtree as cmdline string with -C Add a new -C option to print the kernel.* subtree as a flat command-line string at build time, allowing early parameter injection without runtime parsing. * tag 'bootconfig-v7.2' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: tools/bootconfig: render kernel.* subtree as cmdline string with -C bootconfig: move xbc_snprint_cmdline() to lib/bootconfig.c
2 parents 8b308f9 + 4135542 commit 4f7e890

4 files changed

Lines changed: 111 additions & 53 deletions

File tree

include/linux/bootconfig.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ static inline struct xbc_node * __init xbc_node_get_subkey(struct xbc_node *node
265265
int __init xbc_node_compose_key_after(struct xbc_node *root,
266266
struct xbc_node *node, char *buf, size_t size);
267267

268+
/* Render key/value pairs under @root as a flat cmdline string */
269+
int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root);
270+
268271
/**
269272
* xbc_node_compose_key() - Compose full key string of the XBC node
270273
* @node: An XBC node.

init/main.c

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -324,51 +324,6 @@ static void * __init get_boot_config_from_initrd(size_t *_size)
324324

325325
#ifdef CONFIG_BOOT_CONFIG
326326

327-
static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
328-
329-
#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
330-
331-
static int __init xbc_snprint_cmdline(char *buf, size_t size,
332-
struct xbc_node *root)
333-
{
334-
struct xbc_node *knode, *vnode;
335-
char *end = buf + size;
336-
const char *val, *q;
337-
int ret;
338-
339-
xbc_node_for_each_key_value(root, knode, val) {
340-
ret = xbc_node_compose_key_after(root, knode,
341-
xbc_namebuf, XBC_KEYLEN_MAX);
342-
if (ret < 0)
343-
return ret;
344-
345-
vnode = xbc_node_get_child(knode);
346-
if (!vnode) {
347-
ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
348-
if (ret < 0)
349-
return ret;
350-
buf += ret;
351-
continue;
352-
}
353-
xbc_array_for_each_value(vnode, val) {
354-
/*
355-
* For prettier and more readable /proc/cmdline, only
356-
* quote the value when necessary, i.e. when it contains
357-
* whitespace.
358-
*/
359-
q = strpbrk(val, " \t\r\n") ? "\"" : "";
360-
ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
361-
xbc_namebuf, q, val, q);
362-
if (ret < 0)
363-
return ret;
364-
buf += ret;
365-
}
366-
}
367-
368-
return buf - (end - size);
369-
}
370-
#undef rest
371-
372327
/* Make an extra command line under given key word */
373328
static char * __init xbc_make_cmdline(const char *key)
374329
{

lib/bootconfig.c

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,62 @@ const char * __init xbc_node_find_next_key_value(struct xbc_node *root,
408408
return ""; /* No value key */
409409
}
410410

411+
static char xbc_namebuf[XBC_KEYLEN_MAX] __initdata;
412+
413+
#define rest(dst, end) ((end) > (dst) ? (end) - (dst) : 0)
414+
415+
/**
416+
* xbc_snprint_cmdline() - Render bootconfig keys under @root as a cmdline string
417+
* @buf: Destination buffer (may be NULL when @size is 0 to query the length)
418+
* @size: Size of @buf in bytes
419+
* @root: Subtree root whose key=value pairs should be rendered
420+
*
421+
* Walk all key/value pairs under @root and emit them as a space-separated
422+
* cmdline string into @buf. Values containing whitespace are quoted with
423+
* double quotes. Returns the number of bytes that would be written if @buf
424+
* were large enough (matching snprintf semantics), or a negative errno on
425+
* failure.
426+
*/
427+
int __init xbc_snprint_cmdline(char *buf, size_t size, struct xbc_node *root)
428+
{
429+
struct xbc_node *knode, *vnode;
430+
char *end = buf + size;
431+
const char *val, *q;
432+
int ret;
433+
434+
xbc_node_for_each_key_value(root, knode, val) {
435+
ret = xbc_node_compose_key_after(root, knode,
436+
xbc_namebuf, XBC_KEYLEN_MAX);
437+
if (ret < 0)
438+
return ret;
439+
440+
vnode = xbc_node_get_child(knode);
441+
if (!vnode) {
442+
ret = snprintf(buf, rest(buf, end), "%s ", xbc_namebuf);
443+
if (ret < 0)
444+
return ret;
445+
buf += ret;
446+
continue;
447+
}
448+
xbc_array_for_each_value(vnode, val) {
449+
/*
450+
* For prettier and more readable /proc/cmdline, only
451+
* quote the value when necessary, i.e. when it contains
452+
* whitespace.
453+
*/
454+
q = strpbrk(val, " \t\r\n") ? "\"" : "";
455+
ret = snprintf(buf, rest(buf, end), "%s=%s%s%s ",
456+
xbc_namebuf, q, val, q);
457+
if (ret < 0)
458+
return ret;
459+
buf += ret;
460+
}
461+
}
462+
463+
return buf - (end - size);
464+
}
465+
#undef rest
466+
411467
/* XBC parse and tree build */
412468

413469
static int __init xbc_init_node(struct xbc_node *node, char *data, uint16_t flag)

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

@@ -488,7 +525,10 @@ static int usage(void)
488525
" Options:\n"
489526
" -a <config>: Apply boot config to initrd\n"
490527
" -d : Delete boot config file from initrd\n"
491-
" -l : list boot config in initrd or file\n\n"
528+
" -l : list boot config in initrd or file\n"
529+
" -C : render the kernel.* subtree as a flat cmdline\n"
530+
" string (suitable for embedding in a kernel image)\n"
531+
" and print it to stdout\n\n"
492532
" If no option is given, show the bootconfig in the given file.\n");
493533
return -1;
494534
}
@@ -497,10 +537,11 @@ int main(int argc, char **argv)
497537
{
498538
char *path = NULL;
499539
char *apply = NULL;
540+
bool render_cmdline = false;
500541
bool delete = false, list = false;
501542
int opt;
502543

503-
while ((opt = getopt(argc, argv, "hda:l")) != -1) {
544+
while ((opt = getopt(argc, argv, "hda:lC")) != -1) {
504545
switch (opt) {
505546
case 'd':
506547
delete = true;
@@ -511,14 +552,17 @@ int main(int argc, char **argv)
511552
case 'l':
512553
list = true;
513554
break;
555+
case 'C':
556+
render_cmdline = true;
557+
break;
514558
case 'h':
515559
default:
516560
return usage();
517561
}
518562
}
519563

520-
if ((apply && delete) || (delete && list) || (apply && list)) {
521-
pr_err("Error: You can give one of -a, -d or -l at once.\n");
564+
if ((!!apply + !!delete + !!list + !!render_cmdline) > 1) {
565+
pr_err("Error: You can give one of -a, -d, -l or -C at once.\n");
522566
return usage();
523567
}
524568

@@ -534,5 +578,5 @@ int main(int argc, char **argv)
534578
else if (delete)
535579
return delete_xbc(path);
536580

537-
return show_xbc(path, list);
581+
return show_xbc(path, list, render_cmdline);
538582
}

0 commit comments

Comments
 (0)