aboutsummaryrefslogtreecommitdiff
path: root/util/smmstoretool/storage.c
blob: abc6840b67668ec182ae7a4a3127e8cf394fab70 (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
/* SPDX-License-Identifier: GPL-2.0-or-later */

#include "storage.h"

#include <assert.h>
#include <stdio.h>

#include "fv.h"
#include "utils.h"

bool storage_open(const char store_file[], struct storage_t *storage, bool rw)
{
	storage->rw = rw;

	storage->file = map_file(store_file, rw);
	if (storage->file.start == NULL) {
		fprintf(stderr, "Failed to load smm-store-file \"%s\"\n",
			store_file);
		return false;
	}

	bool auth_vars;
	if (!fv_parse(storage->file, &storage->store_area, &auth_vars)) {
		if (!rw) {
			fprintf(stderr,
				"Failed to find variable store in \"%s\"\n",
				store_file);
			goto error;
		}

		if (!fv_init(storage->file)) {
			fprintf(stderr,
				"Failed to create variable store in \"%s\"\n",
				store_file);
			goto error;
		}

		if (!fv_parse(storage->file, &storage->store_area, &auth_vars)) {
			fprintf(stderr,
				"Failed to parse newly formatted store in \"%s\"\n",
				store_file);
			goto error;
		}

		fprintf(stderr,
			"Successfully created variable store in \"%s\"\n",
			store_file);
	}

	storage->vs = vs_load(storage->store_area, auth_vars);
	return true;

error:
	unmap_file(storage->file);
	return false;
}

bool storage_write_back(struct storage_t *storage)
{
	assert(storage->rw && "Only RW storage can be updated.");

	bool success = vs_store(&storage->vs, storage->store_area);
	if (!success)
		fprintf(stderr, "Failed to update variable store\n");
	storage_drop(storage);
	return success;
}

void storage_drop(struct storage_t *storage)
{
	unmap_file(storage->file);
	vs_free(&storage->vs);
}