Debian Codegraph @

util/smmstoretool/main.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include <unistd.h>
4
5 #include <errno.h>
6 #include <stdbool.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10
11 #include <commonlib/bsd/helpers.h>
12
13 #include "data.h"
14 #include "guids.h"
15 #include "storage.h"
16 #include "udk2017.h"
17 #include "vs.h"
18
19 struct subcommand_t {
20 const char *name;
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[]);
24 };
25
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[]);
38
39 static const struct subcommand_t sub_commands[] = {
40 {
41 .name = "get",
42 .description = "display current value of a variable",
43 .print_help = &help_get,
44 .process = &process_get,
45 },
46 {
47 .name = "guids",
48 .description = "show GUID to alias mapping",
49 .print_help = &help_guids,
50 .process = &process_guids,
51 },
52 {
53 .name = "help",
54 .description = "provide built-in help",
55 .print_help = &help_help,
56 .process = &process_help,
57 },
58 {
59 .name = "list",
60 .description = "list variables present in the store",
61 .print_help = &help_list,
62 .process = &process_list,
63 },
64 {
65 .name = "remove",
66 .description = "remove a variable from the store",
67 .print_help = &help_remove,
68 .process = &process_remove,
69 },
70 {
71 .name = "set",
72 .description = "add or updates a variable in the store",
73 .print_help = &help_set,
74 .process = &process_set,
75 },
76 };
77
78 static const int sub_command_count = ARRAY_SIZE(sub_commands);
79
80 static const char *USAGE_FMT = "Usage: %s smm-store-file|rom sub-command\n"
81 " %s -h|--help\n";
82
83 static const char *program_name;
84
85 static void print_program_usage(void)
86 {
87 fprintf(stderr, USAGE_FMT, program_name, program_name);
88 exit(EXIT_FAILURE);
89 }
90
91 static void print_sub_command_usage(const char sub_command[])
92 {
93 fprintf(stderr, "\n");
94 fprintf(stderr, USAGE_FMT, program_name, program_name);
95 fprintf(stderr, "\n");
96
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))
100 continue;
101
102 cmd->print_help(stderr, cmd);
103 break;
104 }
105
106 exit(EXIT_FAILURE);
107 }
108
109 static void print_help(void)
110 {
111 printf(USAGE_FMT, program_name, program_name);
112
113 printf("\n");
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);
118 }
119 }
120
121 static void print_types(FILE *f)
122 {
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, " * uint16 (0..65535)\n");
127 fprintf(f, " * uint32 (0..4294967295)\n");
128 fprintf(f, " * uint64 (0..2^64-1)\n");
129 fprintf(f, " * ascii (NUL-terminated)\n");
130 fprintf(f, " * unicode (widened and NUL-terminated)\n");
131 fprintf(f, " * file (input only; file contents as variable)\n");
132 fprintf(f, " * raw (output only; raw bytes on output)\n");
133 }
134
135 static void help_set(FILE *f, const struct subcommand_t *info)
136 {
137 fprintf(f, "Create or update a variable:\n");
138 fprintf(f, " %s smm-store-file|rom %s \\\n", program_name, info->name);
139 fprintf(f, " -g vendor-guid \\\n");
140 fprintf(f, " -n variable-name \\\n");
141 fprintf(f, " -t variable-type \\\n");
142 fprintf(f, " -v value\n");
143 fprintf(f, " -a\n");
144 fprintf(f, "\n");
145 print_types(f);
146 }
147
148 static int process_set(int argc, char *argv[], const char store_file[])
149 {
150 const char *name = NULL;
151 const char *value = NULL;
152 const char *type_str = NULL;
153 const char *guid_str = NULL;
154 bool authenticated_var = false;
155 int opt;
156 while ((opt = getopt(argc, argv, "n:t:v:g:a::")) != -1) {
157 switch (opt) {
158 case 'n':
159 name = optarg;
160 break;
161 case 't':
162 type_str = optarg;
163 break;
164 case 'v':
165 value = optarg;
166 break;
167 case 'g':
168 guid_str = optarg;
169 break;
170 case 'a':
171 authenticated_var = true;
172 break;
173
174 case '?': /* parsing error */
175 print_sub_command_usage(argv[0]);
176 }
177 }
178
179 if (argv[optind] != NULL) {
180 fprintf(stderr, "First unexpected positional argument: %s\n",
181 argv[optind]);
182 print_sub_command_usage(argv[0]);
183 }
184
185 if (name == NULL || value == NULL || type_str == NULL ||
186 guid_str == NULL) {
187 fprintf(stderr, "All options are required\n");
188 print_sub_command_usage(argv[0]);
189 }
190
191 if (name[0] == '\0') {
192 fprintf(stderr, "Variable name can't be empty\n");
193 print_sub_command_usage(argv[0]);
194 }
195
196 EFI_GUID guid;
197 if (!parse_guid(guid_str, &guid)) {
198 fprintf(stderr, "Failed to parse GUID: %s\n", guid_str);
199 return EXIT_FAILURE;
200 }
201
202 enum data_type type;
203 if (!parse_data_type(type_str, &type)) {
204 fprintf(stderr, "Failed to parse type: %s\n", type_str);
205 return EXIT_FAILURE;
206 }
207
208 size_t data_size;
209 void *data = make_data(value, &data_size, type);
210 if (data == NULL) {
211 fprintf(stderr, "Failed to parse value \"%s\" as %s\n",
212 value, type_str);
213 return EXIT_FAILURE;
214 }
215
216 struct storage_t storage;
217 if (!storage_open(store_file, &storage, /*rw=*/true)) {
218 free(data);
219 return EXIT_FAILURE;
220 }
221
222 struct var_t *var = vs_find(&storage.vs, name, &guid);
223 if (var == NULL) {
224 var = vs_new_var(&storage.vs);
225 var->attrs |= authenticated_var ? EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS : 0;
226 var->name = to_uchars(name, &var->name_size);
227 var->data = data;
228 var->data_size = data_size;
229 var->guid = guid;
230 } else {
231 free(var->data);
232 var->data = data;
233 var->data_size = data_size;
234 }
235
236 return storage_write_back(&storage) ? EXIT_SUCCESS : EXIT_FAILURE;
237 }
238
239 static void help_list(FILE *f, const struct subcommand_t *info)
240 {
241 fprintf(f, "List variables in the store:\n");
242 fprintf(f, " %s smm-store-file|rom %s\n", program_name, info->name);
243 }
244
245 static int process_list(int argc, char *argv[], const char store_file[])
246 {
247 if (argc != 1) {
248 fprintf(stderr, "Invalid invocation\n");
249 print_sub_command_usage(argv[0]);
250 }
251
252 struct storage_t storage;
253 if (!storage_open(store_file, &storage, /*rw=*/false))
254 return EXIT_FAILURE;
255
256 for (struct var_t *v = storage.vs.vars; v != NULL; v = v->next) {
257 char *name = to_chars(v->name, v->name_size);
258 char *guid = format_guid(&v->guid, /*use_alias=*/true);
259
260 printf("%-*s:%s (%zu %s)\n", GUID_LEN, guid, name, v->data_size,
261 v->data_size == 1 ? "byte" : "bytes");
262
263 free(name);
264 free(guid);
265 }
266
267 storage_drop(&storage);
268 return EXIT_SUCCESS;
269 }
270
271 static void help_get(FILE *f, const struct subcommand_t *info)
272 {
273 fprintf(f, "Read variable's value:\n");
274 fprintf(f, " %s smm-store-file|rom %s \\\n", program_name, info->name);
275 fprintf(f, " -g vendor-guid \\\n");
276 fprintf(f, " -n variable-name \\\n");
277 fprintf(f, " -t variable-type\n");
278 fprintf(f, "\n");
279 print_types(f);
280 }
281
282 static int process_get(int argc, char *argv[], const char store_file[])
283 {
284 const char *name = NULL;
285 const char *type_str = NULL;
286 const char *guid_str = NULL;
287 int opt;
288 while ((opt = getopt(argc, argv, "n:g:t:")) != -1) {
289 switch (opt) {
290 case 'n':
291 name = optarg;
292 break;
293 case 'g':
294 guid_str = optarg;
295 break;
296 case 't':
297 type_str = optarg;
298 break;
299
300 case '?': /* parsing error */
301 print_sub_command_usage(argv[0]);
302 }
303 }
304
305 if (name == NULL || type_str == NULL || guid_str == NULL) {
306 fprintf(stderr, "All options are required\n");
307 print_sub_command_usage(argv[0]);
308 }
309
310 EFI_GUID guid;
311 if (!parse_guid(guid_str, &guid)) {
312 fprintf(stderr, "Failed to parse GUID: %s\n", guid_str);
313 return EXIT_FAILURE;
314 }
315
316 enum data_type type;
317 if (!parse_data_type(type_str, &type)) {
318 fprintf(stderr, "Failed to parse type: %s\n", type_str);
319 return EXIT_FAILURE;
320 }
321
322 struct storage_t storage;
323 if (!storage_open(store_file, &storage, /*rw=*/false))
324 return EXIT_FAILURE;
325
326 int result = EXIT_SUCCESS;
327
328 struct var_t *var = vs_find(&storage.vs, name, &guid);
329 if (var == NULL) {
330 result = EXIT_FAILURE;
331 fprintf(stderr, "Couldn't find variable \"%s:%s\"\n",
332 guid_str, name);
333 } else if (var->data_size == 0) {
334 fprintf(stderr, "There is no data to show.\n");
335 result = EXIT_FAILURE;
336 } else {
337 print_data(var->data, var->data_size, type);
338 }
339
340 storage_drop(&storage);
341 return result;
342 }
343
344 static void help_help(FILE *f, const struct subcommand_t *info)
345 {
346 fprintf(f, "Display generic help:\n");
347 fprintf(f, " %s smm-store-file|rom %s\n", program_name, info->name);
348 fprintf(f, "\n");
349 fprintf(f, "Display sub-command help:\n");
350 fprintf(f, " %s smm-store-file|rom %s sub-command\n",
351 program_name, info->name);
352 }
353
354 static int process_help(int argc, char *argv[], const char store_file[])
355 {
356 (void)store_file;
357
358 if (argc == 1) {
359 print_help();
360 return EXIT_SUCCESS;
361 }
362
363 if (argc != 2) {
364 fprintf(stderr, "Invalid invocation\n");
365 print_sub_command_usage(argv[0]);
366 return EXIT_FAILURE;
367 }
368
369 const char *sub_command = argv[1];
370
371 for (int i = 0; i < sub_command_count; ++i) {
372 const struct subcommand_t *cmd = &sub_commands[i];
373 if (!str_eq(cmd->name, sub_command))
374 continue;
375
376 cmd->print_help(stdout, cmd);
377 return EXIT_SUCCESS;
378 }
379
380 fprintf(stderr, "Unknown sub-command: %s\n", sub_command);
381 print_help();
382 return EXIT_FAILURE;
383 }
384
385 static void help_remove(FILE *f, const struct subcommand_t *info)
386 {
387 fprintf(f, "Remove a variable:\n");
388 fprintf(f, " %s smm-store-file|rom %s \\\n", program_name, info->name);
389 fprintf(f, " -g vendor-guid \\\n");
390 fprintf(f, " -n variable-name\n");
391 }
392
393 static int process_remove(int argc, char *argv[], const char store_file[])
394 {
395 const char *name = NULL;
396 const char *guid_str = NULL;
397 int opt;
398 while ((opt = getopt(argc, argv, "n:g:")) != -1) {
399 switch (opt) {
400 case 'n':
401 name = optarg;
402 break;
403 case 'g':
404 guid_str = optarg;
405 break;
406
407 case '?': /* parsing error */
408 print_sub_command_usage(argv[0]);
409 }
410 }
411
412 if (name == NULL || guid_str == NULL) {
413 fprintf(stderr, "All options are required\n");
414 print_sub_command_usage(argv[0]);
415 }
416
417 EFI_GUID guid;
418 if (!parse_guid(guid_str, &guid)) {
419 fprintf(stderr, "Failed to parse GUID: %s\n", guid_str);
420 return EXIT_FAILURE;
421 }
422
423 struct storage_t storage;
424 if (!storage_open(store_file, &storage, /*rw=*/true))
425 return EXIT_FAILURE;
426
427 int result = EXIT_SUCCESS;
428
429 struct var_t *var = vs_find(&storage.vs, name, &guid);
430 if (var == NULL) {
431 result = EXIT_FAILURE;
432 fprintf(stderr, "Couldn't find variable \"%s:%s\"\n",
433 guid_str, name);
434 } else {
435 vs_delete(&storage.vs, var);
436 }
437
438 storage_write_back(&storage);
439 return result;
440 }
441
442 static void help_guids(FILE *f, const struct subcommand_t *info)
443 {
444 fprintf(f, "List recognized GUIDS:\n");
445 fprintf(f, " %s smm-store-file|rom %s\n", program_name, info->name);
446 }
447
448 static int process_guids(int argc, char *argv[], const char store_file[])
449 {
450 (void)store_file;
451
452 if (argc != 1) {
453 fprintf(stderr, "Invalid invocation\n");
454 print_sub_command_usage(argv[0]);
455 }
456
457 for (int i = 0; i < known_guid_count; ++i) {
458 char *guid = format_guid(&known_guids[i].guid,
459 /*use_alias=*/false);
460 printf("%-10s -> %s\n", known_guids[i].alias, guid);
461 free(guid);
462 }
463 return EXIT_SUCCESS;
464 }
465
466 int main(int argc, char *argv[])
467 {
468 program_name = argv[0];
469
470 if (argc > 1 && (str_eq(argv[1], "-h") || str_eq(argv[1], "--help"))) {
471 print_help();
472 exit(EXIT_SUCCESS);
473 }
474
475 if (argc < 3)
476 print_program_usage();
477
478 const char *store_file = argv[1];
479 const char *sub_command = argv[2];
480
481 int sub_command_argc = argc - 2;
482 char **sub_command_argv = argv + 2;
483
484 for (int i = 0; i < sub_command_count; ++i) {
485 const struct subcommand_t *cmd = &sub_commands[i];
486 if (!str_eq(cmd->name, sub_command))
487 continue;
488
489 return cmd->process(sub_command_argc,
490 sub_command_argv,
491 store_file);
492 }
493
494 fprintf(stderr, "Unknown sub-command: %s\n", sub_command);
495 print_help();
496 return EXIT_FAILURE;
497 }