Sergii Dmytruk | 04bd965 | 2023-11-17 19:31:20 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 | |
| 3 | #include "storage.h" |
| 4 | |
| 5 | #include <assert.h> |
| 6 | #include <stdio.h> |
| 7 | |
Sergii Dmytruk | 89e056b | 2024-03-02 17:02:00 +0200 | [diff] [blame] | 8 | #include "commonlib/bsd/compiler.h" |
| 9 | #include "fmap.h" |
| 10 | |
Sergii Dmytruk | 04bd965 | 2023-11-17 19:31:20 +0200 | [diff] [blame] | 11 | #include "fv.h" |
| 12 | #include "utils.h" |
| 13 | |
| 14 | bool storage_open(const char store_file[], struct storage_t *storage, bool rw) |
| 15 | { |
| 16 | storage->rw = rw; |
| 17 | |
| 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", |
| 21 | store_file); |
| 22 | return false; |
| 23 | } |
| 24 | |
Sergii Dmytruk | 89e056b | 2024-03-02 17:02:00 +0200 | [diff] [blame] | 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; |
| 28 | |
| 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"); |
| 33 | if (area == NULL) { |
| 34 | fprintf(stderr, |
| 35 | "Found FMAP without SMMSTORE in \"%s\"\n", |
| 36 | store_file); |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | storage->region.start += area->offset; |
| 41 | storage->region.length = area->size; |
| 42 | } |
| 43 | |
Sergii Dmytruk | 04bd965 | 2023-11-17 19:31:20 +0200 | [diff] [blame] | 44 | bool auth_vars; |
Sergii Dmytruk | 89e056b | 2024-03-02 17:02:00 +0200 | [diff] [blame] | 45 | if (!fv_parse(storage->region, &storage->store_area, &auth_vars)) { |
Sergii Dmytruk | 04bd965 | 2023-11-17 19:31:20 +0200 | [diff] [blame] | 46 | if (!rw) { |
| 47 | fprintf(stderr, |
| 48 | "Failed to find variable store in \"%s\"\n", |
| 49 | store_file); |
| 50 | goto error; |
| 51 | } |
| 52 | |
Sergii Dmytruk | 89e056b | 2024-03-02 17:02:00 +0200 | [diff] [blame] | 53 | if (!fv_init(storage->region)) { |
Sergii Dmytruk | 04bd965 | 2023-11-17 19:31:20 +0200 | [diff] [blame] | 54 | fprintf(stderr, |
| 55 | "Failed to create variable store in \"%s\"\n", |
| 56 | store_file); |
| 57 | goto error; |
| 58 | } |
| 59 | |
Sergii Dmytruk | 89e056b | 2024-03-02 17:02:00 +0200 | [diff] [blame] | 60 | if (!fv_parse(storage->region, &storage->store_area, &auth_vars)) { |
Sergii Dmytruk | 04bd965 | 2023-11-17 19:31:20 +0200 | [diff] [blame] | 61 | fprintf(stderr, |
| 62 | "Failed to parse newly formatted store in \"%s\"\n", |
| 63 | store_file); |
| 64 | goto error; |
| 65 | } |
| 66 | |
| 67 | fprintf(stderr, |
| 68 | "Successfully created variable store in \"%s\"\n", |
| 69 | store_file); |
| 70 | } |
| 71 | |
| 72 | storage->vs = vs_load(storage->store_area, auth_vars); |
| 73 | return true; |
| 74 | |
| 75 | error: |
| 76 | unmap_file(storage->file); |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | bool storage_write_back(struct storage_t *storage) |
| 81 | { |
| 82 | assert(storage->rw && "Only RW storage can be updated."); |
| 83 | |
| 84 | bool success = vs_store(&storage->vs, storage->store_area); |
| 85 | if (!success) |
| 86 | fprintf(stderr, "Failed to update variable store\n"); |
| 87 | storage_drop(storage); |
| 88 | return success; |
| 89 | } |
| 90 | |
| 91 | void storage_drop(struct storage_t *storage) |
| 92 | { |
| 93 | unmap_file(storage->file); |
| 94 | vs_free(&storage->vs); |
| 95 | } |