Debian Codegraph @

util/smmstoretool/fv.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2
3 #include "fv.h"
4
5 #include <assert.h>
6 #include <stdbool.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <string.h>
10
11 #include "udk2017.h"
12
13 // The same as in `smmstore.h` header, which isn't in `commonlib`.
14 #ifndef SMM_BLOCK_SIZE
15 #define SMM_BLOCK_SIZE (64 * 1024)
16 #endif
17
18 static const EFI_GUID EfiVariableGuid = EFI_VARIABLE_GUID;
19
20 static const EFI_GUID EfiAuthenticatedVariableGuid =
21 EFI_AUTHENTICATED_VARIABLE_GUID;
22
23 static const EFI_GUID EfiSystemNvDataFvGuid = {
24 0xfff12b8d, 0x7696, 0x4c8b,
25 { 0xa9, 0x85, 0x27, 0x47, 0x07, 0x5b, 0x4f, 0x50 }
26 };
27
28 static uint16_t calc_checksum(const uint16_t *hdr, size_t size)
29 {
30 assert(size % 2 == 0 && "Header can't have odd length.");
31
32 uint16_t checksum = 0;
33 for (size_t i = 0; i < size / 2; ++i)
34 checksum += hdr[i];
35 return checksum;
36 }
37
38 bool fv_init(struct mem_range_t fv)
39 {
40 if (fv.length % SMM_BLOCK_SIZE != 0) {
41 fprintf(stderr,
42 "Firmware Volume size is not a multiple of the block size (%dKiB)\n",
43 SMM_BLOCK_SIZE / 1024);
44 return false;
45 }
46
47 uint32_t number_of_blocks = fv.length / SMM_BLOCK_SIZE;
48 memset(fv.start, 0xff, fv.length);
49
50 const EFI_FIRMWARE_VOLUME_HEADER vol_hdr = {
51 .FileSystemGuid = EfiSystemNvDataFvGuid,
52 .FvLength = fv.length,
53 .Signature = EFI_FVH_SIGNATURE,
54 .Attributes = EFI_FVB2_READ_ENABLED_CAP
55 | EFI_FVB2_READ_STATUS
56 | EFI_FVB2_WRITE_ENABLED_CAP
57 | EFI_FVB2_WRITE_STATUS
58 | EFI_FVB2_STICKY_WRITE
59 | EFI_FVB2_MEMORY_MAPPED
60 | EFI_FVB2_ERASE_POLARITY,
61 .HeaderLength = sizeof(vol_hdr)
62 + sizeof(EFI_FV_BLOCK_MAP_ENTRY),
63 .Revision = EFI_FVH_REVISION,
64 .BlockMap[0] = {
65 .NumBlocks = number_of_blocks,
66 .Length = SMM_BLOCK_SIZE,
67 },
68 };
69
70 EFI_FIRMWARE_VOLUME_HEADER *vol_hdr_dst = (void *)fv.start;
71 *vol_hdr_dst = vol_hdr;
72 vol_hdr_dst->BlockMap[1].NumBlocks = 0;
73 vol_hdr_dst->BlockMap[1].Length = 0;
74
75 vol_hdr_dst->Checksum =
76 ~calc_checksum((const void *)vol_hdr_dst, vol_hdr.HeaderLength);
77 ++vol_hdr_dst->Checksum;
78
79 const VARIABLE_STORE_HEADER var_store_hdr = {
80 .Signature = EfiAuthenticatedVariableGuid,
81 // Actual size of the storage is `n / 2 - 1` blocks, the rest is
82 // Fault Tolerant Write (FTW) space and the FTW spare space.
83 .Size = ((number_of_blocks / 2 - 1) * SMM_BLOCK_SIZE) - vol_hdr.HeaderLength,
84 .Format = VARIABLE_STORE_FORMATTED,
85 .State = VARIABLE_STORE_HEALTHY,
86 };
87
88 VARIABLE_STORE_HEADER *var_store_hdr_dst =
89 (void *)(fv.start + vol_hdr.HeaderLength);
90 *var_store_hdr_dst = var_store_hdr;
91
92 return true;
93 }
94
95 static bool guid_eq(const EFI_GUID *lhs, const EFI_GUID *rhs)
96 {
97 return memcmp(lhs, rhs, sizeof(*lhs)) == 0;
98 }
99
100 static bool check_fw_vol_hdr(const EFI_FIRMWARE_VOLUME_HEADER *hdr,
101 size_t max_size)
102 {
103 if (hdr->Revision != EFI_FVH_REVISION ||
104 hdr->Signature != EFI_FVH_SIGNATURE ||
105 hdr->FvLength > max_size ||
106 hdr->HeaderLength >= max_size ||
107 hdr->HeaderLength % 2 != 0) {
108 fprintf(stderr, "No firmware volume header present\n");
109 return false;
110 }
111
112 if (!guid_eq(&hdr->FileSystemGuid, &EfiSystemNvDataFvGuid)) {
113 fprintf(stderr, "Firmware volume GUID non-compatible\n");
114 return false;
115 }
116
117 uint16_t checksum = calc_checksum((const void *)hdr, hdr->HeaderLength);
118 if (checksum != 0) {
119 fprintf(stderr,
120 "Firmware Volume checksum is non-zero: 0x%04X\n",
121 checksum);
122 return false;
123 }
124
125 return true;
126 }
127
128 static bool check_var_store_hdr(const VARIABLE_STORE_HEADER *hdr,
129 size_t max_size,
130 bool *is_auth_var_store)
131 {
132 *is_auth_var_store = guid_eq(&hdr->Signature, &EfiAuthenticatedVariableGuid);
133 if (!*is_auth_var_store && !guid_eq(&hdr->Signature, &EfiVariableGuid)) {
134 fprintf(stderr, "Variable store has unexpected GUID\n");
135 return false;
136 }
137
138 if (hdr->Size > max_size) {
139 fprintf(stderr, "Variable store size is too large: %zu > %zu\n",
140 (size_t)hdr->Size, max_size);
141 return false;
142 }
143
144 if (hdr->Format != VARIABLE_STORE_FORMATTED) {
145 fprintf(stderr, "Variable store is not formatted\n");
146 return false;
147 }
148
149 if (hdr->State != VARIABLE_STORE_HEALTHY) {
150 fprintf(stderr, "Variable store is not in a healthy state\n");
151 return false;
152 }
153
154 return true;
155 }
156
157 bool fv_parse(struct mem_range_t fv, struct mem_range_t *var_store,
158 bool *is_auth_var_store)
159 {
160 const EFI_FIRMWARE_VOLUME_HEADER *vol_hdr = (void *)fv.start;
161 if (!check_fw_vol_hdr(vol_hdr, fv.length)) {
162 fprintf(stderr, "No valid firmware volume was found\n");
163 return false;
164 }
165
166 uint8_t *fw_vol_data = fv.start + vol_hdr->HeaderLength;
167 size_t volume_size = fv.length - vol_hdr->HeaderLength;
168 const VARIABLE_STORE_HEADER *var_store_hdr = (void *)fw_vol_data;
169 if (!check_var_store_hdr(var_store_hdr, volume_size, is_auth_var_store)) {
170 fprintf(stderr, "No valid variable store was found");
171 return false;
172 }
173
174 var_store->start = fw_vol_data + sizeof(*var_store_hdr);
175 var_store->length = volume_size - sizeof(*var_store_hdr);
176 return true;
177 }