]> git.ozlabs.org Git - petitboot/blob - utils/pb-config.c
discover/platform-powerpc: Set IPMI OS boot sensor
[petitboot] / utils / pb-config.c
1
2 #define _GNU_SOURCE
3
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <stdio.h>
7 #include <getopt.h>
8 #include <string.h>
9
10 #include <log/log.h>
11 #include <pb-config/pb-config.h>
12 #include <types/types.h>
13 #include <waiter/waiter.h>
14 #include <process/process.h>
15 #include <talloc/talloc.h>
16
17 extern struct config *config_get(void);
18 extern int platform_init(void);
19
20 static const struct option options[] = {
21         {
22                 .name = "list",
23                 .val = 'l',
24         },
25         { 0 },
26 };
27
28 static void usage(const char *progname)
29 {
30         fprintf(stderr, "Usage:\t%1$s <var>\n"
31                               "\t%1$s --list\n", progname);
32 }
33
34 static void print_one_config(void *ctx, const char *req, const char *name,
35                 const char *fmt, ...)
36 {
37         bool use_prefix = !req;
38         char *val, *sep;
39         va_list ap;
40
41         if (req && strcmp(req, name))
42                 return;
43
44         va_start(ap, fmt);
45         val = talloc_vasprintf(ctx, fmt, ap);
46         va_end(ap);
47
48         if (!strcmp(val, "(null)")) {
49                 talloc_free(val);
50                 if (!use_prefix)
51                         return;
52                 val = talloc_strdup(ctx, "");
53         }
54
55         sep = use_prefix ? ": " : "";
56
57         printf("%s%s%s\n", use_prefix ? name : "", sep, val);
58
59         talloc_free(val);
60 }
61
62 static void print_config(void *ctx, struct config *config, const char *var)
63 {
64         print_one_config(ctx, var, "bootdev", "%s", config->boot_device);
65         print_one_config(ctx, var, "autoboot", "%s",
66                         config->autoboot_enabled ? "enabled" : "disabled");
67         print_one_config(ctx, var, "timeout", "%d",
68                         config->autoboot_timeout_sec);
69         print_one_config(ctx, var, "safe-mode", "%s",
70                         config->safe_mode ? "enabled" : "disabled");
71         print_one_config(ctx, var, "debug", "%s",
72                         config->debug ? "enabled" : "disabled");
73 }
74
75 int main(int argc, char **argv)
76 {
77         struct waitset *waitset;
78         struct config *config;
79         void *ctx;
80         bool list;
81
82         list = false;
83
84         for (;;) {
85                 int opt = getopt_long(argc, argv, "l", options, NULL);
86                 if (opt == -1)
87                         break;
88
89                 switch (opt) {
90                 case 'l':
91                         list = true;
92                         break;
93                 default:
94                         usage(argv[0]);
95                         return EXIT_FAILURE;
96                 }
97         }
98
99         ctx = talloc_new(NULL);
100
101         waitset = waitset_create(ctx);
102
103         process_init(ctx, waitset, false);
104
105         platform_init();
106
107         pb_log_init(stderr);
108
109         config = config_get();
110
111         if (list) {
112                 print_config(ctx, config, NULL);
113         } else {
114                 int i;
115
116                 for (i = optind; i < argc; i++)
117                         print_config(ctx, config, argv[i]);
118         }
119
120         talloc_free(ctx);
121
122         return EXIT_SUCCESS;
123 }