blob: fd9cdf596ff74075b6af2f3904562d7f6afbf1aa [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Patrick Georgi9360fea2018-03-14 21:11:21 +01002
3#include <boot_device.h>
Elyes HAOUAS2195f7a2019-06-21 07:20:12 +02004#include <commonlib/helpers.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +01005#include <commonlib/region.h>
6#include <console/console.h>
Arthur Heymansd57d5e32023-12-27 20:54:19 +01007#include <cpu/x86/smm.h>
8#include <fmap.h>
9#include <fmap_config.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +010010#include <smmstore.h>
Elyes HAOUASede8dd02019-06-23 06:57:53 +020011#include <types.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +010012
Julius Werner6eeba882021-08-10 16:38:43 -070013#define SMMSTORE_REGION "SMMSTORE"
14
Arthur Heymans1ba60492022-02-02 20:09:27 +010015
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
Patrick Georgi9360fea2018-03-14 21:11:21 +010022/*
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
Elyes HAOUASa342f392018-10-17 10:56:26 +020031 * )*
Patrick Georgi9360fea2018-03-14 21:11:21 +010032 * 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
Arthur Heymansed50d852018-12-26 11:20:59 +010044static enum cb_err lookup_store_region(struct region *region)
45{
Julius Werner6eeba882021-08-10 16:38:43 -070046 if (fmap_locate_area(SMMSTORE_REGION, region)) {
47 printk(BIOS_WARNING,
48 "smm store: Unable to find SMM store FMAP region '%s'\n",
49 SMMSTORE_REGION);
50 return CB_ERR;
Arthur Heymansed50d852018-12-26 11:20:59 +010051 }
52
53 return CB_SUCCESS;
54}
55
Patrick Georgi9360fea2018-03-14 21:11:21 +010056/*
57 * Return a region device that points into the store file.
58 *
59 * It's the image builder's responsibility to make it block aligned so that
60 * erase works without destroying other data.
61 *
62 * It doesn't cache the location to cope with flash changing underneath (eg
63 * due to an update)
64 *
65 * returns 0 on success, -1 on failure
66 * outputs the valid store rdev in rstore
67 */
68static int lookup_store(struct region_device *rstore)
69{
Arthur Heymansed50d852018-12-26 11:20:59 +010070 static struct region_device read_rdev, write_rdev;
71 static struct incoherent_rdev store_irdev;
72 struct region region;
73 const struct region_device *rdev;
Patrick Georgi9360fea2018-03-14 21:11:21 +010074
Arthur Heymansed50d852018-12-26 11:20:59 +010075 if (lookup_store_region(&region) != CB_SUCCESS)
76 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +010077
Arthur Heymansed50d852018-12-26 11:20:59 +010078 if (boot_device_ro_subregion(&region, &read_rdev) < 0)
79 return -1;
80
81 if (boot_device_rw_subregion(&region, &write_rdev) < 0)
82 return -1;
83
84 rdev = incoherent_rdev_init(&store_irdev, &region, &read_rdev, &write_rdev);
85
86 if (rdev == NULL)
87 return -1;
88
89 return rdev_chain(rstore, rdev, 0, region_device_sz(rdev));
Patrick Georgi9360fea2018-03-14 21:11:21 +010090}
91
Patrick Rudolph4e00f152022-02-16 07:51:36 +010092 /* this function is non reentrant */
93int smmstore_lookup_region(struct region_device *rstore)
94{
95 static int done;
96 static int ret;
97 static struct region_device rdev;
98
99 if (!done) {
100
101 done = 1;
102
103 if (fmap_locate_area_as_rdev_rw(SMMSTORE_REGION, &rdev)) {
104 printk(BIOS_WARNING,
105 "smm store: Unable to find SMM store FMAP region '%s'\n",
106 SMMSTORE_REGION);
107 ret = -1;
108 } else {
109 ret = 0;
110 }
111 }
112
113 *rstore = rdev;
114 return ret;
115}
Patrick Georgi9360fea2018-03-14 21:11:21 +0100116/*
117 * Read entire store into user provided buffer
118 *
119 * returns 0 on success, -1 on failure
120 * writes up to `*bufsize` bytes into `buf` and updates `*bufsize`
121 */
122int smmstore_read_region(void *buf, ssize_t *bufsize)
123{
124 struct region_device store;
125
126 if (bufsize == NULL)
127 return -1;
128
Patrick Georgi9360fea2018-03-14 21:11:21 +0100129 if (lookup_store(&store) < 0) {
130 printk(BIOS_WARNING, "reading region failed\n");
131 return -1;
132 }
133
Arthur Heymansed50d852018-12-26 11:20:59 +0100134 ssize_t tx = MIN(*bufsize, region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100135 *bufsize = rdev_readat(&store, buf, 0, tx);
136
137 if (*bufsize < 0)
138 return -1;
139
140 return 0;
141}
142
Arthur Heymansed50d852018-12-26 11:20:59 +0100143static enum cb_err scan_end(struct region_device *store)
Patrick Georgi9360fea2018-03-14 21:11:21 +0100144{
Patrick Georgi9360fea2018-03-14 21:11:21 +0100145 /* scan for end */
146 ssize_t end = 0;
147 uint32_t k_sz, v_sz;
Arthur Heymansed50d852018-12-26 11:20:59 +0100148 const ssize_t data_sz = region_device_sz(store);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100149 while (end < data_sz) {
150 /* make odd corner cases identifiable, eg. invalid v_sz */
151 k_sz = 0;
152
Arthur Heymansed50d852018-12-26 11:20:59 +0100153 if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100154 printk(BIOS_WARNING, "failed reading key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100155 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100156 }
157
158 /* found the end */
159 if (k_sz == 0xffffffff)
160 break;
161
162 /* something is fishy here:
163 * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while
164 * other problems are covered by the loop condition
165 */
166 if (k_sz > data_sz) {
167 printk(BIOS_WARNING, "key size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100168 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100169 }
170
Arthur Heymansed50d852018-12-26 11:20:59 +0100171 if (rdev_readat(store, &v_sz, end + sizeof(k_sz), sizeof(v_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100172 printk(BIOS_WARNING, "failed reading value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100173 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100174 }
175
176 if (v_sz > data_sz) {
177 printk(BIOS_WARNING, "value size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100178 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100179 }
180
Arthur Heymansed50d852018-12-26 11:20:59 +0100181 end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100182 end = ALIGN_UP(end, sizeof(uint32_t));
183 }
184
Arthur Heymansed50d852018-12-26 11:20:59 +0100185 printk(BIOS_DEBUG, "used smm store size might be 0x%zx bytes\n", end);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100186
187 if (k_sz != 0xffffffff) {
188 printk(BIOS_WARNING,
Martin Roth3e25f852023-09-04 15:37:07 -0600189 "EOF of data marker looks invalid: 0x%x\n", k_sz);
Arthur Heymansed50d852018-12-26 11:20:59 +0100190 return CB_ERR;
191 }
192
193 if (rdev_chain(store, store, end, data_sz - end))
194 return CB_ERR;
195
196 return CB_SUCCESS;
Arthur Heymansed50d852018-12-26 11:20:59 +0100197}
198/*
199 * Append data to region
200 *
201 * Returns 0 on success, -1 on failure
202 */
203int smmstore_append_data(void *key, uint32_t key_sz, void *value,
204 uint32_t value_sz)
205{
206 struct region_device store;
207
208 if (lookup_store(&store) < 0) {
209 printk(BIOS_WARNING, "reading region failed\n");
Patrick Georgi9360fea2018-03-14 21:11:21 +0100210 return -1;
211 }
212
Arthur Heymansed50d852018-12-26 11:20:59 +0100213 ssize_t offset = 0;
214 ssize_t size;
215 uint8_t nul = 0;
216 if (scan_end(&store) != CB_SUCCESS)
217 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100218
Arthur Heymansed50d852018-12-26 11:20:59 +0100219 printk(BIOS_DEBUG, "used size looks legit\n");
220
221 printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n",
Patrick Georgi9360fea2018-03-14 21:11:21 +0100222 region_device_offset(&store), region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100223
Arthur Heymansed50d852018-12-26 11:20:59 +0100224 size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz
225 + sizeof(nul);
226 if (rdev_chain(&store, &store, 0, size)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100227 printk(BIOS_WARNING, "not enough space for new data\n");
228 return -1;
229 }
230
Arthur Heymansed50d852018-12-26 11:20:59 +0100231 if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz))
232 != sizeof(key_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100233 printk(BIOS_WARNING, "failed writing key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100234 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100235 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100236 offset += sizeof(key_sz);
237 if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz))
238 != sizeof(value_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100239 printk(BIOS_WARNING, "failed writing value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100240 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100241 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100242 offset += sizeof(value_sz);
243 if (rdev_writeat(&store, key, offset, key_sz) != key_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100244 printk(BIOS_WARNING, "failed writing key data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100245 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100246 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100247 offset += key_sz;
248 if (rdev_writeat(&store, value, offset, value_sz) != value_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100249 printk(BIOS_WARNING, "failed writing value data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100250 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100251 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100252 offset += value_sz;
253 if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100254 printk(BIOS_WARNING, "failed writing termination\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100255 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100256 }
257
258 return 0;
259}
260
261/*
262 * Clear region
263 *
264 * Returns 0 on success, -1 on failure, including partial erase
265 */
266int smmstore_clear_region(void)
267{
268 struct region_device store;
269
270 if (lookup_store(&store) < 0) {
271 printk(BIOS_WARNING, "smm store: reading region failed\n");
272 return -1;
273 }
274
275 ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store));
276 if (res != region_device_sz(&store)) {
277 printk(BIOS_WARNING, "smm store: erasing region failed\n");
278 return -1;
279 }
280
281 return 0;
282}
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200283
284
285/* Implementation of Version 2 */
286
Julius Wernerc8931972021-04-16 16:48:32 -0700287static struct region_device mdev_com_buf;
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200288
289static int smmstore_rdev_chain(struct region_device *rdev)
290{
Julius Wernerc8931972021-04-16 16:48:32 -0700291 return rdev_chain_full(rdev, &mdev_com_buf);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200292}
293
294/**
295 * Call once before using the store. In SMM this must be called through an
296 * APM SMI handler providing the communication buffer address and length.
297 */
298int smmstore_init(void *buf, size_t len)
299{
300 if (!buf || len < SMM_BLOCK_SIZE)
301 return -1;
302
Arthur Heymansd57d5e32023-12-27 20:54:19 +0100303 if (smm_points_to_smram(buf, len))
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200304 return -1;
305
Julius Wernerc8931972021-04-16 16:48:32 -0700306 rdev_chain_mem_rw(&mdev_com_buf, buf, len);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200307
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200308 return 0;
309}
310
311#if ENV_RAMSTAGE
312/**
313 * Provide metadata for the coreboot tables.
314 * Must only be called in ramstage, but not in SMM.
315 */
316int smmstore_get_info(struct smmstore_params_info *out)
317{
318 struct region_device store;
319
320 if (lookup_store(&store) < 0) {
321 printk(BIOS_ERR, "smm store: lookup of store failed\n");
322 return -1;
323 }
324
325 if (!IS_ALIGNED(region_device_offset(&store), SMM_BLOCK_SIZE)) {
326 printk(BIOS_ERR, "smm store: store not aligned to block size\n");
327 return -1;
328 }
329
330 out->block_size = SMM_BLOCK_SIZE;
331 out->num_blocks = region_device_sz(&store) / SMM_BLOCK_SIZE;
332
Sean Rhodes38c99b52022-07-13 10:11:44 +0100333 /* FIXME: Broken edk2 always assumes memory mapped Firmware Block Volumes */
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200334 out->mmap_addr = (uintptr_t)rdev_mmap_full(&store);
335
336 printk(BIOS_DEBUG, "smm store: %d # blocks with size 0x%x\n",
337 out->num_blocks, out->block_size);
338
339 return 0;
340}
341#endif
342
343/* Returns -1 on error, 0 on success */
344static int lookup_block_in_store(struct region_device *store, uint32_t block_id)
345{
346 if (lookup_store(store) < 0) {
347 printk(BIOS_ERR, "smm store: lookup of store failed\n");
348 return -1;
349 }
350
351 if ((block_id * SMM_BLOCK_SIZE) >= region_device_sz(store)) {
352 printk(BIOS_ERR, "smm store: block ID out of range\n");
353 return -1;
354 }
355
356 return 0;
357}
358
359/* Returns NULL on error, pointer from rdev_mmap on success */
360static void *mmap_com_buf(struct region_device *com_buf, uint32_t offset, uint32_t bufsize)
361{
362 if (smmstore_rdev_chain(com_buf) < 0) {
363 printk(BIOS_ERR, "smm store: lookup of com buffer failed\n");
364 return NULL;
365 }
366
367 if (offset >= region_device_sz(com_buf)) {
368 printk(BIOS_ERR, "smm store: offset out of range\n");
369 return NULL;
370 }
371
372 void *ptr = rdev_mmap(com_buf, offset, bufsize);
373 if (!ptr)
374 printk(BIOS_ERR, "smm store: not enough space for new data\n");
375
376 return ptr;
377}
378
379/**
380 * Reads the specified block of the SMMSTORE and places it in the communication
381 * buffer.
382 * @param block_id The id of the block to operate on
383 * @param offset Offset within the block.
384 * Must be smaller than the block size.
385 * @param bufsize Size of chunk to read within the block.
386 * Must be smaller than the block size.
387
388 * @return Returns -1 on error, 0 on success.
389 */
390int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
391{
392 struct region_device store;
393 struct region_device com_buf;
394
395 if (lookup_block_in_store(&store, block_id) < 0)
396 return -1;
397
398 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
399 if (!ptr)
400 return -1;
401
402 printk(BIOS_DEBUG, "smm store: reading %p block %d, offset=0x%x, size=%x\n",
403 ptr, block_id, offset, bufsize);
404
405 ssize_t ret = rdev_readat(&store, ptr, block_id * SMM_BLOCK_SIZE + offset, bufsize);
406 rdev_munmap(&com_buf, ptr);
407 if (ret < 0)
408 return -1;
409
410 return 0;
411}
412
413/**
414 * Writes the specified block of the SMMSTORE by reading it from 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 */
424int smmstore_rawwrite_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 if (rdev_chain(&store, &store, block_id * SMM_BLOCK_SIZE + offset, bufsize)) {
433 printk(BIOS_ERR, "smm store: not enough space for new data\n");
434 return -1;
435 }
436
437 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
438 if (!ptr)
439 return -1;
440
441 printk(BIOS_DEBUG, "smm store: writing %p block %d, offset=0x%x, size=%x\n",
442 ptr, block_id, offset, bufsize);
443
444 ssize_t ret = rdev_writeat(&store, ptr, 0, bufsize);
445 rdev_munmap(&com_buf, ptr);
446 if (ret < 0)
447 return -1;
448
449 return 0;
450}
451
452/**
453 * Erases the specified block of the SMMSTORE. The communication buffer remains untouched.
454 *
455 * @param block_id The id of the block to operate on
456 *
457 * @return Returns -1 on error, 0 on success.
458 */
459int smmstore_rawclear_region(uint32_t block_id)
460{
461 struct region_device store;
462
463 if (lookup_block_in_store(&store, block_id) < 0)
464 return -1;
465
466 ssize_t ret = rdev_eraseat(&store, block_id * SMM_BLOCK_SIZE, SMM_BLOCK_SIZE);
467 if (ret != SMM_BLOCK_SIZE) {
468 printk(BIOS_ERR, "smm store: erasing block failed\n");
469 return -1;
470 }
471
472 return 0;
473}