aboutsummaryrefslogtreecommitdiff
path: root/util/smmstoretool/main.c
blob: 4857b624ba64e2461a44f5c16328d746a7e8fb31 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include <unistd.h>

#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

#include <commonlib/bsd/helpers.h>

#include "data.h"
#include "guids.h"
#include "storage.h"
#include "udk2017.h"
#include "vs.h"

struct subcommand_t {
	const char *name;
	const char *description;
	void (*print_help)(FILE *f, const struct subcommand_t *info);
	int (*process)(int argc, char *argv[], const char store_file[]);
};

static void help_get(FILE *f, const struct subcommand_t *info);
static void help_guids(FILE *f, const struct subcommand_t *info);
static void help_help(FILE *f, const struct subcommand_t *info);
static void help_list(FILE *f, const struct subcommand_t *info);
static void help_remove(FILE *f, const struct subcommand_t *info);
static void help_set(FILE *f, const struct subcommand_t *info);
static int process_get(int argc, char *argv[], const char store_file[]);
static int process_guids(int argc, char *argv[], const char store_file[]);
static int process_help(int argc, char *argv[], const char store_file[]);
static int process_list(int argc, char *argv[], const char store_file[]);
static int process_remove(int argc, char *argv[], const char store_file[]);
static int process_set(int argc, char *argv[], const char store_file[]);

static const struct subcommand_t sub_commands[] = {
	{
		.name = "get",
		.description = "display current value of a variable",
		.print_help = &help_get,
		.process = &process_get,
	},
	{
		.name = "guids",
		.description = "show GUID to alias mapping",
		.print_help = &help_guids,
		.process = &process_guids,
	},
	{
		.name = "help",
		.description = "provide built-in help",
		.print_help = &help_help,
		.process = &process_help,
	},
	{
		.name = "list",
		.description = "list variables present in the store",
		.print_help = &help_list,
		.process = &process_list,
	},
	{
		.name = "remove",
		.description = "remove a variable from the store",
		.print_help = &help_remove,
		.process = &process_remove,
	},
	{
		.name = "set",
		.description = "add or updates a variable in the store",
		.print_help = &help_set,
		.process = &process_set,
	},
};

static const int sub_command_count = ARRAY_SIZE(sub_commands);

static const char *USAGE_FMT = "Usage: %s smm-store-file|rom sub-command\n"
			       "       %s -h|--help\n";

static const char *program_name;

static void print_program_usage(void)
{
	fprintf(stderr, USAGE_FMT, program_name, program_name);
	exit(EXIT_FAILURE);
}

static void print_sub_command_usage(const char sub_command[])
{
	fprintf(stderr, "\n");
	fprintf(stderr, USAGE_FMT, program_name, program_name);
	fprintf(stderr, "\n");

	for (int i = 0; i < sub_command_count; ++i) {
		const struct subcommand_t *cmd = &sub_commands[i];
		if (!str_eq(cmd->name, sub_command))
			continue;

		cmd->print_help(stderr, cmd);
		break;
	}

	exit(EXIT_FAILURE);
}

static void print_help(void)
{
	printf(USAGE_FMT, program_name, program_name);

	printf("\n");
	printf("Sub-commands:\n");
	for (int i = 0; i < sub_command_count; ++i) {
		const struct subcommand_t *cmd = &sub_commands[i];
		printf(" * %-6s - %s\n", cmd->name, cmd->description);
	}
}

static void print_types(FILE *f)
{
	fprintf(f, "Types and their values:\n");
	fprintf(f, " * bool (true, false)\n");
	fprintf(f, " * uint8 (0-255)\n");
	fprintf(f, " * ascii (NUL-terminated)\n");
	fprintf(f, " * unicode (widened and NUL-terminated)\n");
	fprintf(f, " * raw (output only; raw bytes on output)\n");
}

static void help_set(FILE *f, const struct subcommand_t *info)
{
	fprintf(f, "Create or update a variable:\n");
	fprintf(f, "  %s smm-store-file|rom %s \\\n", program_name, info->name);
	fprintf(f, "      -g vendor-guid \\\n");
	fprintf(f, "      -n variable-name \\\n");
	fprintf(f, "      -t variable-type \\\n");
	fprintf(f, "      -v value\n");
	fprintf(f, "\n");
	print_types(f);
}

static int process_set(int argc, char *argv[], const char store_file[])
{
	const char *name = NULL;
	const char *value = NULL;
	const char *type_str = NULL;
	const char *guid_str = NULL;
	int opt;
	while ((opt = getopt(argc, argv, "n:t:v:g:")) != -1) {
		switch (opt) {
		case 'n':
			name = optarg;
			break;
		case 't':
			type_str = optarg;
			break;
		case 'v':
			value = optarg;
			break;
		case 'g':
			guid_str = optarg;
			break;

		case '?': /* parsing error */
			print_sub_command_usage(argv[0]);
		}
	}

	if (argv[optind] != NULL) {
		fprintf(stderr, "First unexpected positional argument: %s\n",
			argv[optind]);
		print_sub_command_usage(argv[0]);
	}

	if (name == NULL || value == NULL || type_str == NULL ||
	    guid_str == NULL) {
		fprintf(stderr, "All options are required\n");
		print_sub_command_usage(argv[0]);
	}

	if (name[0] == '\0') {
		fprintf(stderr, "Variable name can't be empty\n");
		print_sub_command_usage(argv[0]);
	}

	EFI_GUID guid;
	if (!parse_guid(guid_str, &guid)) {
		fprintf(stderr, "Failed to parse GUID: %s\n", guid_str);
		return EXIT_FAILURE;
	}

	enum data_type type;
	if (!parse_data_type(type_str, &type)) {
		fprintf(stderr, "Failed to parse type: %s\n", type_str);
		return EXIT_FAILURE;
	}

	size_t data_size;
	void *data = make_data(value, &data_size, type);
	if (data == NULL) {
		fprintf(stderr, "Failed to parse value \"%s\" as %s\n",
			value, type_str);
		return EXIT_FAILURE;
	}

	struct storage_t storage;
	if (!storage_open(store_file, &storage, /*rw=*/true)) {
		free(data);
		return EXIT_FAILURE;
	}

	struct var_t *var = vs_find(&storage.vs, name, &guid);
	if (var == NULL) {
		var = vs_new_var(&storage.vs);
		var->name = to_uchars(name, &var->name_size);
		var->data = data;
		var->data_size = data_size;
		var->guid = guid;
	} else {
		free(var->data);
		var->data = data;
		var->data_size = data_size;
	}

	return storage_write_back(&storage) ? EXIT_SUCCESS : EXIT_FAILURE;
}

static void help_list(FILE *f, const struct subcommand_t *info)
{
	fprintf(f, "List variables in the store:\n");
	fprintf(f, "  %s smm-store-file|rom %s\n", program_name, info->name);
}

static int process_list(int argc, char *argv[], const char store_file[])
{
	if (argc != 1) {
		fprintf(stderr, "Invalid invocation\n");
		print_sub_command_usage(argv[0]);
	}

	struct storage_t storage;
	if (!storage_open(store_file, &storage, /*rw=*/false))
		return EXIT_FAILURE;

	for (struct var_t *v = storage.vs.vars; v != NULL; v = v->next) {
		char *name = to_chars(v->name, v->name_size);
		char *guid = format_guid(&v->guid, /*use_alias=*/true);

		printf("%-*s:%s (%zu %s)\n", GUID_LEN, guid, name, v->data_size,
		       v->data_size == 1 ? "byte" : "bytes");

		free(name);
		free(guid);
	}

	storage_drop(&storage);
	return EXIT_SUCCESS;
}

static void help_get(FILE *f, const struct subcommand_t *info)
{
	fprintf(f, "Read variable's value:\n");
	fprintf(f, "  %s smm-store-file|rom %s \\\n", program_name, info->name);
	fprintf(f, "      -g vendor-guid \\\n");
	fprintf(f, "      -n variable-name \\\n");
	fprintf(f, "      -t variable-type\n");
	fprintf(f, "\n");
	print_types(f);
}

static int process_get(int argc, char *argv[], const char store_file[])
{
	const char *name = NULL;
	const char *type_str = NULL;
	const char *guid_str = NULL;
	int opt;
	while ((opt = getopt(argc, argv, "n:g:t:")) != -1) {
		switch (opt) {
		case 'n':
			name = optarg;
			break;
		case 'g':
			guid_str = optarg;
			break;
		case 't':
			type_str = optarg;
			break;

		case '?': /* parsing error */
			print_sub_command_usage(argv[0]);
		}
	}

	if (name == NULL || type_str == NULL || guid_str == NULL) {
		fprintf(stderr, "All options are required\n");
		print_sub_command_usage(argv[0]);
	}

	EFI_GUID guid;
	if (!parse_guid(guid_str, &guid)) {
		fprintf(stderr, "Failed to parse GUID: %s\n", guid_str);
		return EXIT_FAILURE;
	}

	enum data_type type;
	if (!parse_data_type(type_str, &type)) {
		fprintf(stderr, "Failed to parse type: %s\n", type_str);
		return EXIT_FAILURE;
	}

	struct storage_t storage;
	if (!storage_open(store_file, &storage, /*rw=*/false))
		return EXIT_FAILURE;

	int result = EXIT_SUCCESS;

	struct var_t *var = vs_find(&storage.vs, name, &guid);
	if (var == NULL) {
		result = EXIT_FAILURE;
		fprintf(stderr, "Couldn't find variable \"%s:%s\"\n",
			guid_str, name);
	} else if (var->data_size == 0) {
		fprintf(stderr, "There is no data to show.\n");
		result = EXIT_FAILURE;
	} else {
		print_data(var->data, var->data_size, type);
	}

	storage_drop(&storage);
	return result;
}

static void help_help(FILE *f, const struct subcommand_t *info)
{
	fprintf(f, "Display generic help:\n");
	fprintf(f, "  %s smm-store-file|rom %s\n", program_name, info->name);
	fprintf(f, "\n");
	fprintf(f, "Display sub-command help:\n");
	fprintf(f, "  %s smm-store-file|rom %s sub-command\n",
		program_name, info->name);
}

static int process_help(int argc, char *argv[], const char store_file[])
{
	(void)store_file;

	if (argc == 1) {
		print_help();
		return EXIT_SUCCESS;
	}

	if (argc != 2) {
		fprintf(stderr, "Invalid invocation\n");
		print_sub_command_usage(argv[0]);
		return EXIT_FAILURE;
	}

	const char *sub_command = argv[1];

	for (int i = 0; i < sub_command_count; ++i) {
		const struct subcommand_t *cmd = &sub_commands[i];
		if (!str_eq(cmd->name, sub_command))
			continue;

		cmd->print_help(stdout, cmd);
		return EXIT_SUCCESS;
	}

	fprintf(stderr, "Unknown sub-command: %s\n", sub_command);
	print_help();
	return EXIT_FAILURE;
}

static void help_remove(FILE *f, const struct subcommand_t *info)
{
	fprintf(f, "Remove a variable:\n");
	fprintf(f, "  %s smm-store-file|rom %s \\\n", program_name, info->name);
	fprintf(f, "      -g vendor-guid \\\n");
	fprintf(f, "      -n variable-name\n");
}

static int process_remove(int argc, char *argv[], const char store_file[])
{
	const char *name = NULL;
	const char *guid_str = NULL;
	int opt;
	while ((opt = getopt(argc, argv, "n:g:")) != -1) {
		switch (opt) {
		case 'n':
			name = optarg;
			break;
		case 'g':
			guid_str = optarg;
			break;

		case '?': /* parsing error */
			print_sub_command_usage(argv[0]);
		}
	}

	if (name == NULL || guid_str == NULL) {
		fprintf(stderr, "All options are required\n");
		print_sub_command_usage(argv[0]);
	}

	EFI_GUID guid;
	if (!parse_guid(guid_str, &guid)) {
		fprintf(stderr, "Failed to parse GUID: %s\n", guid_str);
		return EXIT_FAILURE;
	}

	struct storage_t storage;
	if (!storage_open(store_file, &storage, /*rw=*/true))
		return EXIT_FAILURE;

	int result = EXIT_SUCCESS;

	struct var_t *var = vs_find(&storage.vs, name, &guid);
	if (var == NULL) {
		result = EXIT_FAILURE;
		fprintf(stderr, "Couldn't find variable \"%s:%s\"\n",
			guid_str, name);
	} else {
		vs_delete(&storage.vs, var);
	}

	storage_write_back(&storage);
	return result;
}

static void help_guids(FILE *f, const struct subcommand_t *info)
{
	fprintf(f, "List recognized GUIDS:\n");
	fprintf(f, "  %s smm-store-file|rom %s\n", program_name, info->name);
}

static int process_guids(int argc, char *argv[], const char store_file[])
{
	(void)store_file;

	if (argc != 1) {
		fprintf(stderr, "Invalid invocation\n");
		print_sub_command_usage(argv[0]);
	}

	for (int i = 0; i < known_guid_count; ++i) {
		char *guid = format_guid(&known_guids[i].guid,
					 /*use_alias=*/false);
		printf("%-10s -> %s\n", known_guids[i].alias, guid);
		free(guid);
	}
	return EXIT_SUCCESS;
}

int main(int argc, char *argv[])
{
	program_name = argv[0];

	if (argc > 1 && (str_eq(argv[1], "-h") || str_eq(argv[1], "--help"))) {
		print_help();
		exit(EXIT_SUCCESS);
	}

	if (argc < 3)
		print_program_usage();

	const char *store_file = argv[1];
	const char *sub_command = argv[2];

	int sub_command_argc = argc - 2;
	char **sub_command_argv = argv + 2;

	for (int i = 0; i < sub_command_count; ++i) {
		const struct subcommand_t *cmd = &sub_commands[i];
		if (!str_eq(cmd->name, sub_command))
			continue;

		return cmd->process(sub_command_argc,
				    sub_command_argv,
				    store_file);
	}

	fprintf(stderr, "Unknown sub-command: %s\n", sub_command);
	print_help();
	return EXIT_FAILURE;
}