1 /* SPDX-License-Identifier: GPL-2.0-or-later */
8 #include "commonlib/bsd/compiler.h"
14 bool storage_open(const char store_file
[], struct storage_t
*storage
, bool rw
)
18 storage
->file
= map_file(store_file
, rw
);
19 if (storage
->file
.start
== NULL
) {
20 fprintf(stderr
, "Failed to load smm-store-file \"%s\"\n",
25 /* If we won't find FMAP with SMMSTORE, use the whole file, but fail if
26 * FMAP is there without SMMSTORE. */
27 storage
->region
= storage
->file
;
29 long fmap_offset
= fmap_find(storage
->file
.start
, storage
->file
.length
);
30 if (fmap_offset
>= 0) {
31 struct fmap
*fmap
= (void *)(storage
->file
.start
+ fmap_offset
);
32 const struct fmap_area
*area
= fmap_find_area(fmap
, "SMMSTORE");
35 "Found FMAP without SMMSTORE in \"%s\"\n",
40 storage
->region
.start
+= area
->offset
;
41 storage
->region
.length
= area
->size
;
45 if (!fv_parse(storage
->region
, &storage
->store_area
, &auth_vars
)) {
48 "Failed to find variable store in \"%s\"\n",
53 if (!fv_init(storage
->region
)) {
55 "Failed to create variable store in \"%s\"\n",
60 if (!fv_parse(storage
->region
, &storage
->store_area
, &auth_vars
)) {
62 "Failed to parse newly formatted store in \"%s\"\n",
68 "Successfully created variable store in \"%s\"\n",
72 storage
->vs
= vs_load(storage
->store_area
, auth_vars
);
76 unmap_file(storage
->file
);
80 bool storage_write_back(struct storage_t
*storage
)
82 assert(storage
->rw
&& "Only RW storage can be updated.");
84 bool success
= vs_store(&storage
->vs
, storage
->store_area
);
86 fprintf(stderr
, "Failed to update variable store\n");
87 storage_drop(storage
);
91 void storage_drop(struct storage_t
*storage
)
93 unmap_file(storage
->file
);
94 vs_free(&storage
->vs
);