Debian Codegraph @

src/drivers/smmstore/store.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <boot_device.h>
4 #include <commonlib/helpers.h>
5 #include <commonlib/region.h>
6 #include <console/console.h>
7 #include <cpu/x86/smm.h>
8 #include <fmap.h>
9 #include <fmap_config.h>
10 #include <smmstore.h>
11 #include <types.h>
12
13 #define SMMSTORE_REGION "SMMSTORE"
14
15
16 _Static_assert(IS_ALIGNED(FMAP_SECTION_SMMSTORE_START, SMM_BLOCK_SIZE),
17 "SMMSTORE FMAP region not aligned to 64K");
18
19 _Static_assert(SMM_BLOCK_SIZE <= FMAP_SECTION_SMMSTORE_SIZE,
20 "SMMSTORE FMAP region must be at least 64K");
21
22 /*
23 * The region format is still not finalized, but so far it looks like this:
24 * (
25 * uint32le_t key_sz
26 * uint32le_t value_sz
27 * uint8_t key[key_sz]
28 * uint8_t value[value_sz]
29 * uint8_t active
30 * align to 4 bytes
31 * )*
32 * uint32le_t endmarker = 0xffffffff
33 *
34 * active needs to be set to 0x00 for the entry to be valid. This satisfies
35 * the constraint that entries are either complete or will be ignored, as long
36 * as flash is written sequentially and into a fully erased block.
37 *
38 * Future additions to the format will split the region in half with an active
39 * block marker to allow safe compaction (ie. write the new data in the unused
40 * region, mark it active after the write completed). Otherwise a well-timed
41 * crash/reboot could clear out all variables.
42 */
43
44 static int smmstore_use_full_flash;
45 static int has_capsules = -1;
46
47 int smmstore_preprocess_cmd(uint8_t *cmd, void *param)
48 {
49 if (CONFIG(DRIVERS_EFI_UPDATE_CAPSULES)) {
50 if (has_capsules == -1 && *cmd == SMMSTORE_CMD_USE_FULL_FLASH) {
51 has_capsules = !!(uintptr_t)param;
52 /*
53 * If we have capsules, return success, otherwise let smmstore_exec()
54 * fail on !param check, which will be 0 in that case. This informs
55 * the caller whether capsule handling was enabled or not.
56 */
57 return has_capsules;
58 } else if (has_capsules == 1 && *cmd & SMMSTORE_CMD_USE_FULL_FLASH) {
59 smmstore_use_full_flash = 1;
60 *cmd &= ~SMMSTORE_CMD_USE_FULL_FLASH;
61 } else {
62 smmstore_use_full_flash = 0;
63 }
64 }
65
66 return 0;
67 }
68
69 static enum cb_err lookup_store_region(struct region *region)
70 {
71 if (CONFIG(DRIVERS_EFI_UPDATE_CAPSULES) && smmstore_use_full_flash) {
72 const struct region_device *rdev = boot_device_rw();
73
74 if (rdev == NULL)
75 return CB_ERR;
76
77 *region = *region_device_region(rdev);
78 return CB_SUCCESS;
79 }
80
81 if (fmap_locate_area(SMMSTORE_REGION, region)) {
82 printk(BIOS_WARNING,
83 "smm store: Unable to find SMM store FMAP region '%s'\n",
84 SMMSTORE_REGION);
85 return CB_ERR;
86 }
87
88 return CB_SUCCESS;
89 }
90
91 /*
92 * Return a region device that points into the store file.
93 *
94 * It's the image builder's responsibility to make it block aligned so that
95 * erase works without destroying other data.
96 *
97 * It doesn't cache the location to cope with flash changing underneath (eg
98 * due to an update)
99 *
100 * returns 0 on success, -1 on failure
101 * outputs the valid store rdev in rstore
102 */
103 static int lookup_store(struct region_device *rstore)
104 {
105 static struct region_device read_rdev, write_rdev;
106 static struct incoherent_rdev store_irdev;
107 struct region region;
108 const struct region_device *rdev;
109
110 if (lookup_store_region(&region) != CB_SUCCESS)
111 return -1;
112
113 if (boot_device_ro_subregion(&region, &read_rdev) < 0)
114 return -1;
115
116 if (boot_device_rw_subregion(&region, &write_rdev) < 0)
117 return -1;
118
119 rdev = incoherent_rdev_init(&store_irdev, &region, &read_rdev, &write_rdev);
120
121 if (rdev == NULL)
122 return -1;
123
124 return rdev_chain(rstore, rdev, 0, region_device_sz(rdev));
125 }
126
127 /* this function is non reentrant */
128 int smmstore_lookup_region(struct region_device *rstore)
129 {
130 static int done;
131 static int ret;
132 static struct region_device rdev;
133
134 if (!done) {
135 done = 1;
136
137 if (fmap_locate_area_as_rdev_rw(SMMSTORE_REGION, &rdev)) {
138 printk(BIOS_WARNING,
139 "smm store: Unable to find SMM store FMAP region '%s'\n",
140 SMMSTORE_REGION);
141 ret = -1;
142 } else {
143 ret = 0;
144 }
145 }
146
147 *rstore = rdev;
148 return ret;
149 }
150 /*
151 * Read entire store into user provided buffer
152 *
153 * returns 0 on success, -1 on failure
154 * writes up to `*bufsize` bytes into `buf` and updates `*bufsize`
155 */
156 int smmstore_read_region(void *buf, ssize_t *bufsize)
157 {
158 struct region_device store;
159
160 if (bufsize == NULL)
161 return -1;
162
163 if (lookup_store(&store) < 0) {
164 printk(BIOS_WARNING, "reading region failed\n");
165 return -1;
166 }
167
168 ssize_t tx = MIN(*bufsize, region_device_sz(&store));
169 *bufsize = rdev_readat(&store, buf, 0, tx);
170
171 if (*bufsize < 0)
172 return -1;
173
174 return 0;
175 }
176
177 static enum cb_err scan_end(struct region_device *store)
178 {
179 /* scan for end */
180 ssize_t end = 0;
181 uint32_t k_sz, v_sz;
182 const ssize_t data_sz = region_device_sz(store);
183 while (end < data_sz) {
184 /* make odd corner cases identifiable, eg. invalid v_sz */
185 k_sz = 0;
186
187 if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) {
188 printk(BIOS_WARNING, "failed reading key size\n");
189 return CB_ERR;
190 }
191
192 /* found the end */
193 if (k_sz == 0xffffffff)
194 break;
195
196 /* something is fishy here:
197 * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while
198 * other problems are covered by the loop condition
199 */
200 if (k_sz > data_sz) {
201 printk(BIOS_WARNING, "key size out of bounds\n");
202 return CB_ERR;
203 }
204
205 if (rdev_readat(store, &v_sz, end + sizeof(k_sz), sizeof(v_sz)) < 0) {
206 printk(BIOS_WARNING, "failed reading value size\n");
207 return CB_ERR;
208 }
209
210 if (v_sz > data_sz) {
211 printk(BIOS_WARNING, "value size out of bounds\n");
212 return CB_ERR;
213 }
214
215 end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1;
216 end = ALIGN_UP(end, sizeof(uint32_t));
217 }
218
219 printk(BIOS_DEBUG, "used smm store size might be 0x%zx bytes\n", end);
220
221 if (k_sz != 0xffffffff) {
222 printk(BIOS_WARNING,
223 "EOF of data marker looks invalid: 0x%x\n", k_sz);
224 return CB_ERR;
225 }
226
227 if (rdev_chain(store, store, end, data_sz - end))
228 return CB_ERR;
229
230 return CB_SUCCESS;
231 }
232 /*
233 * Append data to region
234 *
235 * Returns 0 on success, -1 on failure
236 */
237 int smmstore_append_data(void *key, uint32_t key_sz, void *value,
238 uint32_t value_sz)
239 {
240 struct region_device store;
241
242 if (lookup_store(&store) < 0) {
243 printk(BIOS_WARNING, "reading region failed\n");
244 return -1;
245 }
246
247 ssize_t offset = 0;
248 ssize_t size;
249 uint8_t nul = 0;
250 if (scan_end(&store) != CB_SUCCESS)
251 return -1;
252
253 printk(BIOS_DEBUG, "used size looks legit\n");
254
255 printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n",
256 region_device_offset(&store), region_device_sz(&store));
257
258 size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz
259 + sizeof(nul);
260 if (rdev_chain(&store, &store, 0, size)) {
261 printk(BIOS_WARNING, "not enough space for new data\n");
262 return -1;
263 }
264
265 if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz))
266 != sizeof(key_sz)) {
267 printk(BIOS_WARNING, "failed writing key size\n");
268 return -1;
269 }
270 offset += sizeof(key_sz);
271 if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz))
272 != sizeof(value_sz)) {
273 printk(BIOS_WARNING, "failed writing value size\n");
274 return -1;
275 }
276 offset += sizeof(value_sz);
277 if (rdev_writeat(&store, key, offset, key_sz) != key_sz) {
278 printk(BIOS_WARNING, "failed writing key data\n");
279 return -1;
280 }
281 offset += key_sz;
282 if (rdev_writeat(&store, value, offset, value_sz) != value_sz) {
283 printk(BIOS_WARNING, "failed writing value data\n");
284 return -1;
285 }
286 offset += value_sz;
287 if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) {
288 printk(BIOS_WARNING, "failed writing termination\n");
289 return -1;
290 }
291
292 return 0;
293 }
294
295 /*
296 * Clear region
297 *
298 * Returns 0 on success, -1 on failure, including partial erase
299 */
300 int smmstore_clear_region(void)
301 {
302 struct region_device store;
303
304 if (lookup_store(&store) < 0) {
305 printk(BIOS_WARNING, "smm store: reading region failed\n");
306 return -1;
307 }
308
309 ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store));
310 if (res != region_device_sz(&store)) {
311 printk(BIOS_WARNING, "smm store: erasing region failed\n");
312 return -1;
313 }
314
315 return 0;
316 }
317
318
319 /* Implementation of Version 2 */
320
321 static struct region_device mdev_com_buf;
322
323 static int smmstore_rdev_chain(struct region_device *rdev)
324 {
325 return rdev_chain_full(rdev, &mdev_com_buf);
326 }
327
328 /**
329 * Call once before using the store. In SMM this must be called through an
330 * APM SMI handler providing the communication buffer address and length.
331 */
332 int smmstore_init(void *buf, size_t len)
333 {
334 if (!buf || len < SMM_BLOCK_SIZE)
335 return -1;
336
337 if (smm_points_to_smram(buf, len))
338 return -1;
339
340 rdev_chain_mem_rw(&mdev_com_buf, buf, len);
341
342 return 0;
343 }
344
345 #if ENV_RAMSTAGE
346 /**
347 * Provide metadata for the coreboot tables.
348 * Must only be called in ramstage, but not in SMM.
349 */
350 int smmstore_get_info(struct smmstore_params_info *out)
351 {
352 struct region_device store;
353
354 if (lookup_store(&store) < 0) {
355 printk(BIOS_ERR, "smm store: lookup of store failed\n");
356 return -1;
357 }
358
359 if (!IS_ALIGNED(region_device_offset(&store), SMM_BLOCK_SIZE)) {
360 printk(BIOS_ERR, "smm store: store not aligned to block size\n");
361 return -1;
362 }
363
364 out->block_size = SMM_BLOCK_SIZE;
365 out->num_blocks = region_device_sz(&store) / SMM_BLOCK_SIZE;
366
367 /* FIXME: Broken edk2 always assumes memory mapped Firmware Block Volumes */
368 out->mmap_addr = (uintptr_t)rdev_mmap_full(&store);
369
370 printk(BIOS_DEBUG, "smm store: %d # blocks with size 0x%x\n",
371 out->num_blocks, out->block_size);
372
373 return 0;
374 }
375 #endif
376
377 /* Returns -1 on error, 0 on success */
378 static int lookup_block_in_store(struct region_device *store, uint32_t block_id)
379 {
380 if (lookup_store(store) < 0) {
381 printk(BIOS_ERR, "smm store: lookup of store failed\n");
382 return -1;
383 }
384
385 if ((block_id * SMM_BLOCK_SIZE) >= region_device_sz(store)) {
386 printk(BIOS_ERR, "smm store: block ID out of range\n");
387 return -1;
388 }
389
390 return 0;
391 }
392
393 /* Returns NULL on error, pointer from rdev_mmap on success */
394 static void *mmap_com_buf(struct region_device *com_buf, uint32_t offset, uint32_t bufsize)
395 {
396 if (smmstore_rdev_chain(com_buf) < 0) {
397 printk(BIOS_ERR, "smm store: lookup of com buffer failed\n");
398 return NULL;
399 }
400
401 if (offset >= region_device_sz(com_buf)) {
402 printk(BIOS_ERR, "smm store: offset out of range\n");
403 return NULL;
404 }
405
406 void *ptr = rdev_mmap(com_buf, offset, bufsize);
407 if (!ptr)
408 printk(BIOS_ERR, "smm store: not enough space for new data\n");
409
410 return ptr;
411 }
412
413 /**
414 * Reads the specified block of the SMMSTORE and places it in the communication
415 * buffer.
416 * @param block_id The id of the block to operate on
417 * @param offset Offset within the block.
418 * Must be smaller than the block size.
419 * @param bufsize Size of chunk to read within the block.
420 * Must be smaller than the block size.
421
422 * @return Returns -1 on error, 0 on success.
423 */
424 int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
425 {
426 struct region_device store;
427 struct region_device com_buf;
428
429 if (lookup_block_in_store(&store, block_id) < 0)
430 return -1;
431
432 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
433 if (!ptr)
434 return -1;
435
436 printk(BIOS_DEBUG, "smm store: reading %p block %d, offset=0x%x, size=%x\n",
437 ptr, block_id, offset, bufsize);
438
439 ssize_t ret = rdev_readat(&store, ptr, block_id * SMM_BLOCK_SIZE + offset, bufsize);
440 rdev_munmap(&com_buf, ptr);
441 if (ret < 0)
442 return -1;
443
444 return 0;
445 }
446
447 /**
448 * Writes the specified block of the SMMSTORE by reading it from the communication
449 * buffer.
450 * @param block_id The id of the block to operate on
451 * @param offset Offset within the block.
452 * Must be smaller than the block size.
453 * @param bufsize Size of chunk to read within the block.
454 * Must be smaller than the block size.
455
456 * @return Returns -1 on error, 0 on success.
457 */
458 int smmstore_rawwrite_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
459 {
460 struct region_device store;
461 struct region_device com_buf;
462
463 if (lookup_block_in_store(&store, block_id) < 0)
464 return -1;
465
466 if (rdev_chain(&store, &store, block_id * SMM_BLOCK_SIZE + offset, bufsize)) {
467 printk(BIOS_ERR, "smm store: not enough space for new data\n");
468 return -1;
469 }
470
471 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
472 if (!ptr)
473 return -1;
474
475 printk(BIOS_DEBUG, "smm store: writing %p block %d, offset=0x%x, size=%x\n",
476 ptr, block_id, offset, bufsize);
477
478 ssize_t ret = rdev_writeat(&store, ptr, 0, bufsize);
479 rdev_munmap(&com_buf, ptr);
480 if (ret < 0)
481 return -1;
482
483 return 0;
484 }
485
486 /**
487 * Erases the specified block of the SMMSTORE. The communication buffer remains untouched.
488 *
489 * @param block_id The id of the block to operate on
490 *
491 * @return Returns -1 on error, 0 on success.
492 */
493 int smmstore_rawclear_region(uint32_t block_id)
494 {
495 struct region_device store;
496
497 if (lookup_block_in_store(&store, block_id) < 0)
498 return -1;
499
500 ssize_t ret = rdev_eraseat(&store, block_id * SMM_BLOCK_SIZE, SMM_BLOCK_SIZE);
501 if (ret != SMM_BLOCK_SIZE) {
502 printk(BIOS_ERR, "smm store: erasing block failed\n");
503 return -1;
504 }
505
506 return 0;
507 }