main.c (d052e1c6909f9ccbdc4112a50796afca19094229) main.c (e4f70b7badb40598ceea31c122d7c2fb6203672a)
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Boot config tool for initrd image
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <unistd.h>
11#include <string.h>
12#include <errno.h>
13
14#include <linux/kernel.h>
15#include <linux/bootconfig.h>
16
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Boot config tool for initrd image
4 */
5#include <stdio.h>
6#include <stdlib.h>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <unistd.h>
11#include <string.h>
12#include <errno.h>
13
14#include <linux/kernel.h>
15#include <linux/bootconfig.h>
16
17static int xbc_show_value(struct xbc_node *node)
17static int xbc_show_value(struct xbc_node *node, bool semicolon)
18{
18{
19 const char *val;
19 const char *val, *eol;
20 char q;
21 int i = 0;
22
20 char q;
21 int i = 0;
22
23 eol = semicolon ? ";\n" : "\n";
23 xbc_array_for_each_value(node, val) {
24 if (strchr(val, '"'))
25 q = '\'';
26 else
27 q = '"';
24 xbc_array_for_each_value(node, val) {
25 if (strchr(val, '"'))
26 q = '\'';
27 else
28 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);
29 i++;
30 }
31 return i;
32}
33
34static void xbc_show_compact_tree(void)
35{
36 struct xbc_node *node, *cnode;

--- 11 unchanged lines hidden (view full) ---

48 }
49 if (cnode && xbc_node_is_key(cnode)) {
50 printf("%s {\n", xbc_node_get_data(node));
51 depth++;
52 node = cnode;
53 continue;
54 } else if (cnode && xbc_node_is_value(cnode)) {
55 printf("%s = ", xbc_node_get_data(node));
30 i++;
31 }
32 return i;
33}
34
35static void xbc_show_compact_tree(void)
36{
37 struct xbc_node *node, *cnode;

--- 11 unchanged lines hidden (view full) ---

49 }
50 if (cnode && xbc_node_is_key(cnode)) {
51 printf("%s {\n", xbc_node_get_data(node));
52 depth++;
53 node = cnode;
54 continue;
55 } else if (cnode && xbc_node_is_value(cnode)) {
56 printf("%s = ", xbc_node_get_data(node));
56 xbc_show_value(cnode);
57 xbc_show_value(cnode, true);
57 } else {
58 printf("%s;\n", xbc_node_get_data(node));
59 }
60
61 if (node->next) {
62 node = xbc_node_get_next(node);
63 continue;
64 }

--- 7 unchanged lines hidden (view full) ---

72 for (i = 0; i < depth; i++)
73 printf("\t");
74 printf("}\n");
75 }
76 node = xbc_node_get_next(node);
77 }
78}
79
58 } else {
59 printf("%s;\n", xbc_node_get_data(node));
60 }
61
62 if (node->next) {
63 node = xbc_node_get_next(node);
64 continue;
65 }

--- 7 unchanged lines hidden (view full) ---

73 for (i = 0; i < depth; i++)
74 printf("\t");
75 printf("}\n");
76 }
77 node = xbc_node_get_next(node);
78 }
79}
80
81static 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
80/* Simple real checksum */
81int checksum(unsigned char *buf, int len)
82{
83 int i, sum = 0;
84
85 for (i = 0; i < len; i++)
86 sum += buf[i];
87

--- 140 unchanged lines hidden (view full) ---

228 ret = xbc_init(buf, &msg, &pos);
229 if (ret < 0)
230 show_xbc_error(copy, msg, pos);
231 free(copy);
232
233 return ret;
234}
235
101/* Simple real checksum */
102int checksum(unsigned char *buf, int len)
103{
104 int i, sum = 0;
105
106 for (i = 0; i < len; i++)
107 sum += buf[i];
108

--- 140 unchanged lines hidden (view full) ---

249 ret = xbc_init(buf, &msg, &pos);
250 if (ret < 0)
251 show_xbc_error(copy, msg, pos);
252 free(copy);
253
254 return ret;
255}
256
236int show_xbc(const char *path)
257int show_xbc(const char *path, bool list)
237{
238 int ret, fd;
239 char *buf = NULL;
240 struct stat st;
241
242 ret = stat(path, &st);
243 if (ret < 0) {
244 pr_err("Failed to stat %s: %d\n", path, -errno);

--- 17 unchanged lines hidden (view full) ---

262 ret = load_xbc_file(path, &buf);
263 if (ret < 0) {
264 pr_err("Failed to load a boot config: %d\n", ret);
265 goto out;
266 }
267 if (init_xbc_with_error(buf, ret) < 0)
268 goto out;
269 }
258{
259 int ret, fd;
260 char *buf = NULL;
261 struct stat st;
262
263 ret = stat(path, &st);
264 if (ret < 0) {
265 pr_err("Failed to stat %s: %d\n", path, -errno);

--- 17 unchanged lines hidden (view full) ---

283 ret = load_xbc_file(path, &buf);
284 if (ret < 0) {
285 pr_err("Failed to load a boot config: %d\n", ret);
286 goto out;
287 }
288 if (init_xbc_with_error(buf, ret) < 0)
289 goto out;
290 }
270 xbc_show_compact_tree();
291 if (list)
292 xbc_show_list();
293 else
294 xbc_show_compact_tree();
271 ret = 0;
272out:
273 free(buf);
274
275 return ret;
276}
277
278int delete_xbc(const char *path)

--- 106 unchanged lines hidden (view full) ---

385
386int usage(void)
387{
388 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
389 "Or bootconfig <CONFIG>\n"
390 " Apply, delete or show boot config to initrd.\n"
391 " Options:\n"
392 " -a <config>: Apply boot config to initrd\n"
295 ret = 0;
296out:
297 free(buf);
298
299 return ret;
300}
301
302int delete_xbc(const char *path)

--- 106 unchanged lines hidden (view full) ---

409
410int usage(void)
411{
412 printf("Usage: bootconfig [OPTIONS] <INITRD>\n"
413 "Or bootconfig <CONFIG>\n"
414 " Apply, delete or show boot config to initrd.\n"
415 " Options:\n"
416 " -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"
394 " If no option is given, show the bootconfig in the given file.\n");
395 return -1;
396}
397
398int main(int argc, char **argv)
399{
400 char *path = NULL;
401 char *apply = NULL;
419 " If no option is given, show the bootconfig in the given file.\n");
420 return -1;
421}
422
423int main(int argc, char **argv)
424{
425 char *path = NULL;
426 char *apply = NULL;
402 bool delete = false;
427 bool delete = false, list = false;
403 int opt;
404
428 int opt;
429
405 while ((opt = getopt(argc, argv, "hda:")) != -1) {
430 while ((opt = getopt(argc, argv, "hda:l")) != -1) {
406 switch (opt) {
407 case 'd':
408 delete = true;
409 break;
410 case 'a':
411 apply = optarg;
412 break;
431 switch (opt) {
432 case 'd':
433 delete = true;
434 break;
435 case 'a':
436 apply = optarg;
437 break;
438 case 'l':
439 list = true;
440 break;
413 case 'h':
414 default:
415 return usage();
416 }
417 }
418
441 case 'h':
442 default:
443 return usage();
444 }
445 }
446
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");
421 return usage();
422 }
423
424 if (optind >= argc) {
425 pr_err("Error: No initrd is specified.\n");
426 return usage();
427 }
428
429 path = argv[optind];
430
431 if (apply)
432 return apply_xbc(path, apply);
433 else if (delete)
434 return delete_xbc(path);
435
449 return usage();
450 }
451
452 if (optind >= argc) {
453 pr_err("Error: No initrd is specified.\n");
454 return usage();
455 }
456
457 path = argv[optind];
458
459 if (apply)
460 return apply_xbc(path, apply);
461 else if (delete)
462 return delete_xbc(path);
463
436 return show_xbc(path);
464 return show_xbc(path, list);
437}
465}