1 /* SPDX-License-Identifier: GPL-2.0-or-later */
11 #include <commonlib/bsd/helpers.h>
21 const char *description
;
22 void (*print_help
)(FILE *f
, const struct subcommand_t
*info
);
23 int (*process
)(int argc
, char *argv
[], const char store_file
[]);
26 static void help_get(FILE *f
, const struct subcommand_t
*info
);
27 static void help_guids(FILE *f
, const struct subcommand_t
*info
);
28 static void help_help(FILE *f
, const struct subcommand_t
*info
);
29 static void help_list(FILE *f
, const struct subcommand_t
*info
);
30 static void help_remove(FILE *f
, const struct subcommand_t
*info
);
31 static void help_set(FILE *f
, const struct subcommand_t
*info
);
32 static int process_get(int argc
, char *argv
[], const char store_file
[]);
33 static int process_guids(int argc
, char *argv
[], const char store_file
[]);
34 static int process_help(int argc
, char *argv
[], const char store_file
[]);
35 static int process_list(int argc
, char *argv
[], const char store_file
[]);
36 static int process_remove(int argc
, char *argv
[], const char store_file
[]);
37 static int process_set(int argc
, char *argv
[], const char store_file
[]);
39 static const struct subcommand_t sub_commands
[] = {
42 .description
= "display current value of a variable",
43 .print_help
= &help_get
,
44 .process
= &process_get
,
48 .description
= "show GUID to alias mapping",
49 .print_help
= &help_guids
,
50 .process
= &process_guids
,
54 .description
= "provide built-in help",
55 .print_help
= &help_help
,
56 .process
= &process_help
,
60 .description
= "list variables present in the store",
61 .print_help
= &help_list
,
62 .process
= &process_list
,
66 .description
= "remove a variable from the store",
67 .print_help
= &help_remove
,
68 .process
= &process_remove
,
72 .description
= "add or updates a variable in the store",
73 .print_help
= &help_set
,
74 .process
= &process_set
,
78 static const int sub_command_count
= ARRAY_SIZE(sub_commands
);
80 static const char *USAGE_FMT
= "Usage: %s smm-store-file|rom sub-command\n"
83 static const char *program_name
;
85 static void print_program_usage(void)
87 fprintf(stderr
, USAGE_FMT
, program_name
, program_name
);
91 static void print_sub_command_usage(const char sub_command
[])
93 fprintf(stderr
, "\n");
94 fprintf(stderr
, USAGE_FMT
, program_name
, program_name
);
95 fprintf(stderr
, "\n");
97 for (int i
= 0; i
< sub_command_count
; ++i
) {
98 const struct subcommand_t
*cmd
= &sub_commands
[i
];
99 if (!str_eq(cmd
->name
, sub_command
))
102 cmd
->print_help(stderr
, cmd
);
109 static void print_help(void)
111 printf(USAGE_FMT
, program_name
, program_name
);
114 printf("Sub-commands:\n");
115 for (int i
= 0; i
< sub_command_count
; ++i
) {
116 const struct subcommand_t
*cmd
= &sub_commands
[i
];
117 printf(" * %-6s - %s\n", cmd
->name
, cmd
->description
);
121 static void print_types(FILE *f
)
123 fprintf(f
, "Types and their values:\n");
124 fprintf(f
, " * bool (true, false)\n");
125 fprintf(f
, " * uint8 (0-255)\n");
126 fprintf(f
, " * ascii (NUL-terminated)\n");
127 fprintf(f
, " * unicode (widened and NUL-terminated)\n");
128 fprintf(f
, " * raw (output only; raw bytes on output)\n");
131 static void help_set(FILE *f
, const struct subcommand_t
*info
)
133 fprintf(f
, "Create or update a variable:\n");
134 fprintf(f
, " %s smm-store-file|rom %s \\\n", program_name
, info
->name
);
135 fprintf(f
, " -g vendor-guid \\\n");
136 fprintf(f
, " -n variable-name \\\n");
137 fprintf(f
, " -t variable-type \\\n");
138 fprintf(f
, " -v value\n");
143 static int process_set(int argc
, char *argv
[], const char store_file
[])
145 const char *name
= NULL
;
146 const char *value
= NULL
;
147 const char *type_str
= NULL
;
148 const char *guid_str
= NULL
;
150 while ((opt
= getopt(argc
, argv
, "n:t:v:g:")) != -1) {
165 case '?': /* parsing error */
166 print_sub_command_usage(argv
[0]);
170 if (argv
[optind
] != NULL
) {
171 fprintf(stderr
, "First unexpected positional argument: %s\n",
173 print_sub_command_usage(argv
[0]);
176 if (name
== NULL
|| value
== NULL
|| type_str
== NULL
||
178 fprintf(stderr
, "All options are required\n");
179 print_sub_command_usage(argv
[0]);
182 if (name
[0] == '\0') {
183 fprintf(stderr
, "Variable name can't be empty\n");
184 print_sub_command_usage(argv
[0]);
188 if (!parse_guid(guid_str
, &guid
)) {
189 fprintf(stderr
, "Failed to parse GUID: %s\n", guid_str
);
194 if (!parse_data_type(type_str
, &type
)) {
195 fprintf(stderr
, "Failed to parse type: %s\n", type_str
);
200 void *data
= make_data(value
, &data_size
, type
);
202 fprintf(stderr
, "Failed to parse value \"%s\" as %s\n",
207 struct storage_t storage
;
208 if (!storage_open(store_file
, &storage
, /*rw=*/true)) {
213 struct var_t
*var
= vs_find(&storage
.vs
, name
, &guid
);
215 var
= vs_new_var(&storage
.vs
);
216 var
->name
= to_uchars(name
, &var
->name_size
);
218 var
->data_size
= data_size
;
223 var
->data_size
= data_size
;
226 return storage_write_back(&storage
) ? EXIT_SUCCESS
: EXIT_FAILURE
;
229 static void help_list(FILE *f
, const struct subcommand_t
*info
)
231 fprintf(f
, "List variables in the store:\n");
232 fprintf(f
, " %s smm-store-file|rom %s\n", program_name
, info
->name
);
235 static int process_list(int argc
, char *argv
[], const char store_file
[])
238 fprintf(stderr
, "Invalid invocation\n");
239 print_sub_command_usage(argv
[0]);
242 struct storage_t storage
;
243 if (!storage_open(store_file
, &storage
, /*rw=*/false))
246 for (struct var_t
*v
= storage
.vs
.vars
; v
!= NULL
; v
= v
->next
) {
247 char *name
= to_chars(v
->name
, v
->name_size
);
248 char *guid
= format_guid(&v
->guid
, /*use_alias=*/true);
250 printf("%-*s:%s (%zu %s)\n", GUID_LEN
, guid
, name
, v
->data_size
,
251 v
->data_size
== 1 ? "byte" : "bytes");
257 storage_drop(&storage
);
261 static void help_get(FILE *f
, const struct subcommand_t
*info
)
263 fprintf(f
, "Read variable's value:\n");
264 fprintf(f
, " %s smm-store-file|rom %s \\\n", program_name
, info
->name
);
265 fprintf(f
, " -g vendor-guid \\\n");
266 fprintf(f
, " -n variable-name \\\n");
267 fprintf(f
, " -t variable-type\n");
272 static int process_get(int argc
, char *argv
[], const char store_file
[])
274 const char *name
= NULL
;
275 const char *type_str
= NULL
;
276 const char *guid_str
= NULL
;
278 while ((opt
= getopt(argc
, argv
, "n:g:t:")) != -1) {
290 case '?': /* parsing error */
291 print_sub_command_usage(argv
[0]);
295 if (name
== NULL
|| type_str
== NULL
|| guid_str
== NULL
) {
296 fprintf(stderr
, "All options are required\n");
297 print_sub_command_usage(argv
[0]);
301 if (!parse_guid(guid_str
, &guid
)) {
302 fprintf(stderr
, "Failed to parse GUID: %s\n", guid_str
);
307 if (!parse_data_type(type_str
, &type
)) {
308 fprintf(stderr
, "Failed to parse type: %s\n", type_str
);
312 struct storage_t storage
;
313 if (!storage_open(store_file
, &storage
, /*rw=*/false))
316 int result
= EXIT_SUCCESS
;
318 struct var_t
*var
= vs_find(&storage
.vs
, name
, &guid
);
320 result
= EXIT_FAILURE
;
321 fprintf(stderr
, "Couldn't find variable \"%s:%s\"\n",
323 } else if (var
->data_size
== 0) {
324 fprintf(stderr
, "There is no data to show.\n");
325 result
= EXIT_FAILURE
;
327 print_data(var
->data
, var
->data_size
, type
);
330 storage_drop(&storage
);
334 static void help_help(FILE *f
, const struct subcommand_t
*info
)
336 fprintf(f
, "Display generic help:\n");
337 fprintf(f
, " %s smm-store-file|rom %s\n", program_name
, info
->name
);
339 fprintf(f
, "Display sub-command help:\n");
340 fprintf(f
, " %s smm-store-file|rom %s sub-command\n",
341 program_name
, info
->name
);
344 static int process_help(int argc
, char *argv
[], const char store_file
[])
354 fprintf(stderr
, "Invalid invocation\n");
355 print_sub_command_usage(argv
[0]);
359 const char *sub_command
= argv
[1];
361 for (int i
= 0; i
< sub_command_count
; ++i
) {
362 const struct subcommand_t
*cmd
= &sub_commands
[i
];
363 if (!str_eq(cmd
->name
, sub_command
))
366 cmd
->print_help(stdout
, cmd
);
370 fprintf(stderr
, "Unknown sub-command: %s\n", sub_command
);
375 static void help_remove(FILE *f
, const struct subcommand_t
*info
)
377 fprintf(f
, "Remove a variable:\n");
378 fprintf(f
, " %s smm-store-file|rom %s \\\n", program_name
, info
->name
);
379 fprintf(f
, " -g vendor-guid \\\n");
380 fprintf(f
, " -n variable-name\n");
383 static int process_remove(int argc
, char *argv
[], const char store_file
[])
385 const char *name
= NULL
;
386 const char *guid_str
= NULL
;
388 while ((opt
= getopt(argc
, argv
, "n:g:")) != -1) {
397 case '?': /* parsing error */
398 print_sub_command_usage(argv
[0]);
402 if (name
== NULL
|| guid_str
== NULL
) {
403 fprintf(stderr
, "All options are required\n");
404 print_sub_command_usage(argv
[0]);
408 if (!parse_guid(guid_str
, &guid
)) {
409 fprintf(stderr
, "Failed to parse GUID: %s\n", guid_str
);
413 struct storage_t storage
;
414 if (!storage_open(store_file
, &storage
, /*rw=*/true))
417 int result
= EXIT_SUCCESS
;
419 struct var_t
*var
= vs_find(&storage
.vs
, name
, &guid
);
421 result
= EXIT_FAILURE
;
422 fprintf(stderr
, "Couldn't find variable \"%s:%s\"\n",
425 vs_delete(&storage
.vs
, var
);
428 storage_write_back(&storage
);
432 static void help_guids(FILE *f
, const struct subcommand_t
*info
)
434 fprintf(f
, "List recognized GUIDS:\n");
435 fprintf(f
, " %s smm-store-file|rom %s\n", program_name
, info
->name
);
438 static int process_guids(int argc
, char *argv
[], const char store_file
[])
443 fprintf(stderr
, "Invalid invocation\n");
444 print_sub_command_usage(argv
[0]);
447 for (int i
= 0; i
< known_guid_count
; ++i
) {
448 char *guid
= format_guid(&known_guids
[i
].guid
,
449 /*use_alias=*/false);
450 printf("%-10s -> %s\n", known_guids
[i
].alias
, guid
);
456 int main(int argc
, char *argv
[])
458 program_name
= argv
[0];
460 if (argc
> 1 && (str_eq(argv
[1], "-h") || str_eq(argv
[1], "--help"))) {
466 print_program_usage();
468 const char *store_file
= argv
[1];
469 const char *sub_command
= argv
[2];
471 int sub_command_argc
= argc
- 2;
472 char **sub_command_argv
= argv
+ 2;
474 for (int i
= 0; i
< sub_command_count
; ++i
) {
475 const struct subcommand_t
*cmd
= &sub_commands
[i
];
476 if (!str_eq(cmd
->name
, sub_command
))
479 return cmd
->process(sub_command_argc
,
484 fprintf(stderr
, "Unknown sub-command: %s\n", sub_command
);