Skip to content

Commit e4f70b7

Browse files
mhiramatrostedt
authored andcommitted
tools/bootconfig: Add list option
Add list option (-l) to show the bootconfig in the list style. This is same output of /proc/bootconfig. So users can check how their bootconfig will be shown in procfs. This will help them to write a user-space script to parse the /proc/bootconfig. Link: https://lkml.kernel.org/r/159704849087.175360.8761890802048625207.stgit@devnote2 Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent d052e1c commit e4f70b7

1 file changed

Lines changed: 40 additions & 12 deletions

File tree

‎tools/bootconfig/main.c‎

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@
1414
#include <linux/kernel.h>
1515
#include <linux/bootconfig.h>
1616

17-
static int xbc_show_value(struct xbc_node *node)
17+
static int xbc_show_value(struct xbc_node *node, bool semicolon)
1818
{
19-
const char *val;
19+
const char *val, *eol;
2020
char q;
2121
int i = 0;
2222

23+
eol = semicolon ? ";\n" : "\n";
2324
xbc_array_for_each_value(node, val) {
2425
if (strchr(val, '"'))
2526
q = '\'';
2627
else
2728
q = '"';
28-
printf("%c%s%c%s", q, val, q, node->next ? ", " : ";\n");
29+
printf("%c%s%c%s", q, val, q, node->next ? ", " : eol);
2930
i++;
3031
}
3132
return i;
@@ -53,7 +54,7 @@ static void xbc_show_compact_tree(void)
5354
continue;
5455
} else if (cnode && xbc_node_is_value(cnode)) {
5556
printf("%s = ", xbc_node_get_data(node));
56-
xbc_show_value(cnode);
57+
xbc_show_value(cnode, true);
5758
} else {
5859
printf("%s;\n", xbc_node_get_data(node));
5960
}
@@ -77,6 +78,26 @@ static void xbc_show_compact_tree(void)
7778
}
7879
}
7980

81+
static void xbc_show_list(void)
82+
{
83+
char key[XBC_KEYLEN_MAX];
84+
struct xbc_node *leaf;
85+
const char *val;
86+
int ret = 0;
87+
88+
xbc_for_each_key_value(leaf, val) {
89+
ret = xbc_node_compose_key(leaf, key, XBC_KEYLEN_MAX);
90+
if (ret < 0)
91+
break;
92+
printf("%s = ", key);
93+
if (!val || val[0] == '\0') {
94+
printf("\"\"\n");
95+
continue;
96+
}
97+
xbc_show_value(xbc_node_get_child(leaf), false);
98+
}
99+
}
100+
80101
/* Simple real checksum */
81102
int checksum(unsigned char *buf, int len)
82103
{
@@ -233,7 +254,7 @@ static int init_xbc_with_error(char *buf, int len)
233254
return ret;
234255
}
235256

236-
int show_xbc(const char *path)
257+
int show_xbc(const char *path, bool list)
237258
{
238259
int ret, fd;
239260
char *buf = NULL;
@@ -267,7 +288,10 @@ int show_xbc(const char *path)
267288
if (init_xbc_with_error(buf, ret) < 0)
268289
goto out;
269290
}
270-
xbc_show_compact_tree();
291+
if (list)
292+
xbc_show_list();
293+
else
294+
xbc_show_compact_tree();
271295
ret = 0;
272296
out:
273297
free(buf);
@@ -390,7 +414,8 @@ int usage(void)
390414
" Apply, delete or show boot config to initrd.\n"
391415
" Options:\n"
392416
" -a <config>: Apply boot config to initrd\n"
393-
" -d : Delete boot config file from initrd\n\n"
417+
" -d : Delete boot config file from initrd\n"
418+
" -l : list boot config in initrd or file\n\n"
394419
" If no option is given, show the bootconfig in the given file.\n");
395420
return -1;
396421
}
@@ -399,25 +424,28 @@ int main(int argc, char **argv)
399424
{
400425
char *path = NULL;
401426
char *apply = NULL;
402-
bool delete = false;
427+
bool delete = false, list = false;
403428
int opt;
404429

405-
while ((opt = getopt(argc, argv, "hda:")) != -1) {
430+
while ((opt = getopt(argc, argv, "hda:l")) != -1) {
406431
switch (opt) {
407432
case 'd':
408433
delete = true;
409434
break;
410435
case 'a':
411436
apply = optarg;
412437
break;
438+
case 'l':
439+
list = true;
440+
break;
413441
case 'h':
414442
default:
415443
return usage();
416444
}
417445
}
418446

419-
if (apply && delete) {
420-
pr_err("Error: You can not specify both -a and -d at once.\n");
447+
if ((apply && delete) || (delete && list) || (apply && list)) {
448+
pr_err("Error: You can give one of -a, -d or -l at once.\n");
421449
return usage();
422450
}
423451

@@ -433,5 +461,5 @@ int main(int argc, char **argv)
433461
else if (delete)
434462
return delete_xbc(path);
435463

436-
return show_xbc(path);
464+
return show_xbc(path, list);
437465
}

0 commit comments

Comments
 (0)