blob: 96cb761e4036c0e7792df6d3ca162133bdc94c16 [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>
Arthur Heymans8b04dc72018-12-01 15:28:03 +01004#include <fmap.h>
Arthur Heymans1ba60492022-02-02 20:09:27 +01005#include <fmap_config.h>
Elyes HAOUAS2195f7a2019-06-21 07:20:12 +02006#include <commonlib/helpers.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +01007#include <commonlib/region.h>
8#include <console/console.h>
9#include <smmstore.h>
Elyes HAOUASede8dd02019-06-23 06:57:53 +020010#include <types.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +010011
Julius Werner6eeba882021-08-10 16:38:43 -070012#define SMMSTORE_REGION "SMMSTORE"
13
Arthur Heymans1ba60492022-02-02 20:09:27 +010014
15_Static_assert(IS_ALIGNED(FMAP_SECTION_SMMSTORE_START, SMM_BLOCK_SIZE),
16 "SMMSTORE FMAP region not aligned to 64K");
17
18_Static_assert(SMM_BLOCK_SIZE <= FMAP_SECTION_SMMSTORE_SIZE,
19 "SMMSTORE FMAP region must be at least 64K");
20
Patrick Georgi9360fea2018-03-14 21:11:21 +010021/*
22 * The region format is still not finalized, but so far it looks like this:
23 * (
24 * uint32le_t key_sz
25 * uint32le_t value_sz
26 * uint8_t key[key_sz]
27 * uint8_t value[value_sz]
28 * uint8_t active
29 * align to 4 bytes
Elyes HAOUASa342f392018-10-17 10:56:26 +020030 * )*
Patrick Georgi9360fea2018-03-14 21:11:21 +010031 * uint32le_t endmarker = 0xffffffff
32 *
33 * active needs to be set to 0x00 for the entry to be valid. This satisfies
34 * the constraint that entries are either complete or will be ignored, as long
35 * as flash is written sequentially and into a fully erased block.
36 *
37 * Future additions to the format will split the region in half with an active
38 * block marker to allow safe compaction (ie. write the new data in the unused
39 * region, mark it active after the write completed). Otherwise a well-timed
40 * crash/reboot could clear out all variables.
41 */
42
Arthur Heymansed50d852018-12-26 11:20:59 +010043static enum cb_err lookup_store_region(struct region *region)
44{
Julius Werner6eeba882021-08-10 16:38:43 -070045 if (fmap_locate_area(SMMSTORE_REGION, region)) {
46 printk(BIOS_WARNING,
47 "smm store: Unable to find SMM store FMAP region '%s'\n",
48 SMMSTORE_REGION);
49 return CB_ERR;
Arthur Heymansed50d852018-12-26 11:20:59 +010050 }
51
52 return CB_SUCCESS;
53}
54
Patrick Georgi9360fea2018-03-14 21:11:21 +010055/*
56 * Return a region device that points into the store file.
57 *
58 * It's the image builder's responsibility to make it block aligned so that
59 * erase works without destroying other data.
60 *
61 * It doesn't cache the location to cope with flash changing underneath (eg
62 * due to an update)
63 *
64 * returns 0 on success, -1 on failure
65 * outputs the valid store rdev in rstore
66 */
67static int lookup_store(struct region_device *rstore)
68{
Arthur Heymansed50d852018-12-26 11:20:59 +010069 static struct region_device read_rdev, write_rdev;
70 static struct incoherent_rdev store_irdev;
71 struct region region;
72 const struct region_device *rdev;
Patrick Georgi9360fea2018-03-14 21:11:21 +010073
Arthur Heymansed50d852018-12-26 11:20:59 +010074 if (lookup_store_region(&region) != CB_SUCCESS)
75 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +010076
Arthur Heymansed50d852018-12-26 11:20:59 +010077 if (boot_device_ro_subregion(&region, &read_rdev) < 0)
78 return -1;
79
80 if (boot_device_rw_subregion(&region, &write_rdev) < 0)
81 return -1;
82
83 rdev = incoherent_rdev_init(&store_irdev, &region, &read_rdev, &write_rdev);
84
85 if (rdev == NULL)
86 return -1;
87
88 return rdev_chain(rstore, rdev, 0, region_device_sz(rdev));
Patrick Georgi9360fea2018-03-14 21:11:21 +010089}
90
91/*
92 * Read entire store into user provided buffer
93 *
94 * returns 0 on success, -1 on failure
95 * writes up to `*bufsize` bytes into `buf` and updates `*bufsize`
96 */
97int smmstore_read_region(void *buf, ssize_t *bufsize)
98{
99 struct region_device store;
100
101 if (bufsize == NULL)
102 return -1;
103
Patrick Georgi9360fea2018-03-14 21:11:21 +0100104 if (lookup_store(&store) < 0) {
105 printk(BIOS_WARNING, "reading region failed\n");
106 return -1;
107 }
108
Arthur Heymansed50d852018-12-26 11:20:59 +0100109 ssize_t tx = MIN(*bufsize, region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100110 *bufsize = rdev_readat(&store, buf, 0, tx);
111
112 if (*bufsize < 0)
113 return -1;
114
115 return 0;
116}
117
Arthur Heymansed50d852018-12-26 11:20:59 +0100118static enum cb_err scan_end(struct region_device *store)
Patrick Georgi9360fea2018-03-14 21:11:21 +0100119{
Patrick Georgi9360fea2018-03-14 21:11:21 +0100120 /* scan for end */
121 ssize_t end = 0;
122 uint32_t k_sz, v_sz;
Arthur Heymansed50d852018-12-26 11:20:59 +0100123 const ssize_t data_sz = region_device_sz(store);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100124 while (end < data_sz) {
125 /* make odd corner cases identifiable, eg. invalid v_sz */
126 k_sz = 0;
127
Arthur Heymansed50d852018-12-26 11:20:59 +0100128 if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100129 printk(BIOS_WARNING, "failed reading key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100130 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100131 }
132
133 /* found the end */
134 if (k_sz == 0xffffffff)
135 break;
136
137 /* something is fishy here:
138 * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while
139 * other problems are covered by the loop condition
140 */
141 if (k_sz > data_sz) {
142 printk(BIOS_WARNING, "key size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100143 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100144 }
145
Arthur Heymansed50d852018-12-26 11:20:59 +0100146 if (rdev_readat(store, &v_sz, end + sizeof(k_sz), sizeof(v_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100147 printk(BIOS_WARNING, "failed reading value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100148 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100149 }
150
151 if (v_sz > data_sz) {
152 printk(BIOS_WARNING, "value size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100153 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100154 }
155
Arthur Heymansed50d852018-12-26 11:20:59 +0100156 end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100157 end = ALIGN_UP(end, sizeof(uint32_t));
158 }
159
Arthur Heymansed50d852018-12-26 11:20:59 +0100160 printk(BIOS_DEBUG, "used smm store size might be 0x%zx bytes\n", end);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100161
162 if (k_sz != 0xffffffff) {
163 printk(BIOS_WARNING,
164 "eof of data marker looks invalid: 0x%x\n", k_sz);
Arthur Heymansed50d852018-12-26 11:20:59 +0100165 return CB_ERR;
166 }
167
168 if (rdev_chain(store, store, end, data_sz - end))
169 return CB_ERR;
170
171 return CB_SUCCESS;
Arthur Heymansed50d852018-12-26 11:20:59 +0100172}
173/*
174 * Append data to region
175 *
176 * Returns 0 on success, -1 on failure
177 */
178int smmstore_append_data(void *key, uint32_t key_sz, void *value,
179 uint32_t value_sz)
180{
181 struct region_device store;
182
183 if (lookup_store(&store) < 0) {
184 printk(BIOS_WARNING, "reading region failed\n");
Patrick Georgi9360fea2018-03-14 21:11:21 +0100185 return -1;
186 }
187
Arthur Heymansed50d852018-12-26 11:20:59 +0100188 ssize_t offset = 0;
189 ssize_t size;
190 uint8_t nul = 0;
191 if (scan_end(&store) != CB_SUCCESS)
192 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100193
Arthur Heymansed50d852018-12-26 11:20:59 +0100194 printk(BIOS_DEBUG, "used size looks legit\n");
195
196 printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n",
Patrick Georgi9360fea2018-03-14 21:11:21 +0100197 region_device_offset(&store), region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100198
Arthur Heymansed50d852018-12-26 11:20:59 +0100199 size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz
200 + sizeof(nul);
201 if (rdev_chain(&store, &store, 0, size)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100202 printk(BIOS_WARNING, "not enough space for new data\n");
203 return -1;
204 }
205
Arthur Heymansed50d852018-12-26 11:20:59 +0100206 if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz))
207 != sizeof(key_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100208 printk(BIOS_WARNING, "failed writing key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100209 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100210 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100211 offset += sizeof(key_sz);
212 if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz))
213 != sizeof(value_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100214 printk(BIOS_WARNING, "failed writing value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100215 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100216 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100217 offset += sizeof(value_sz);
218 if (rdev_writeat(&store, key, offset, key_sz) != key_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100219 printk(BIOS_WARNING, "failed writing key data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100220 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100221 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100222 offset += key_sz;
223 if (rdev_writeat(&store, value, offset, value_sz) != value_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100224 printk(BIOS_WARNING, "failed writing value data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100225 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100226 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100227 offset += value_sz;
228 if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100229 printk(BIOS_WARNING, "failed writing termination\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100230 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100231 }
232
233 return 0;
234}
235
236/*
237 * Clear region
238 *
239 * Returns 0 on success, -1 on failure, including partial erase
240 */
241int smmstore_clear_region(void)
242{
243 struct region_device store;
244
245 if (lookup_store(&store) < 0) {
246 printk(BIOS_WARNING, "smm store: reading region failed\n");
247 return -1;
248 }
249
250 ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store));
251 if (res != region_device_sz(&store)) {
252 printk(BIOS_WARNING, "smm store: erasing region failed\n");
253 return -1;
254 }
255
256 return 0;
257}
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200258
259
260/* Implementation of Version 2 */
261
262static bool store_initialized;
Julius Wernerc8931972021-04-16 16:48:32 -0700263static struct region_device mdev_com_buf;
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200264
265static int smmstore_rdev_chain(struct region_device *rdev)
266{
267 if (!store_initialized)
268 return -1;
269
Julius Wernerc8931972021-04-16 16:48:32 -0700270 return rdev_chain_full(rdev, &mdev_com_buf);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200271}
272
273/**
274 * Call once before using the store. In SMM this must be called through an
275 * APM SMI handler providing the communication buffer address and length.
276 */
277int smmstore_init(void *buf, size_t len)
278{
279 if (!buf || len < SMM_BLOCK_SIZE)
280 return -1;
281
282 if (store_initialized)
283 return -1;
284
Julius Wernerc8931972021-04-16 16:48:32 -0700285 rdev_chain_mem_rw(&mdev_com_buf, buf, len);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200286
287 store_initialized = true;
288
289 return 0;
290}
291
292#if ENV_RAMSTAGE
293/**
294 * Provide metadata for the coreboot tables.
295 * Must only be called in ramstage, but not in SMM.
296 */
297int smmstore_get_info(struct smmstore_params_info *out)
298{
299 struct region_device store;
300
301 if (lookup_store(&store) < 0) {
302 printk(BIOS_ERR, "smm store: lookup of store failed\n");
303 return -1;
304 }
305
306 if (!IS_ALIGNED(region_device_offset(&store), SMM_BLOCK_SIZE)) {
307 printk(BIOS_ERR, "smm store: store not aligned to block size\n");
308 return -1;
309 }
310
311 out->block_size = SMM_BLOCK_SIZE;
312 out->num_blocks = region_device_sz(&store) / SMM_BLOCK_SIZE;
313
Sean Rhodes38c99b52022-07-13 10:11:44 +0100314 /* FIXME: Broken edk2 always assumes memory mapped Firmware Block Volumes */
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200315 out->mmap_addr = (uintptr_t)rdev_mmap_full(&store);
316
317 printk(BIOS_DEBUG, "smm store: %d # blocks with size 0x%x\n",
318 out->num_blocks, out->block_size);
319
320 return 0;
321}
322#endif
323
324/* Returns -1 on error, 0 on success */
325static int lookup_block_in_store(struct region_device *store, uint32_t block_id)
326{
327 if (lookup_store(store) < 0) {
328 printk(BIOS_ERR, "smm store: lookup of store failed\n");
329 return -1;
330 }
331
332 if ((block_id * SMM_BLOCK_SIZE) >= region_device_sz(store)) {
333 printk(BIOS_ERR, "smm store: block ID out of range\n");
334 return -1;
335 }
336
337 return 0;
338}
339
340/* Returns NULL on error, pointer from rdev_mmap on success */
341static void *mmap_com_buf(struct region_device *com_buf, uint32_t offset, uint32_t bufsize)
342{
343 if (smmstore_rdev_chain(com_buf) < 0) {
344 printk(BIOS_ERR, "smm store: lookup of com buffer failed\n");
345 return NULL;
346 }
347
348 if (offset >= region_device_sz(com_buf)) {
349 printk(BIOS_ERR, "smm store: offset out of range\n");
350 return NULL;
351 }
352
353 void *ptr = rdev_mmap(com_buf, offset, bufsize);
354 if (!ptr)
355 printk(BIOS_ERR, "smm store: not enough space for new data\n");
356
357 return ptr;
358}
359
360/**
361 * Reads the specified block of the SMMSTORE and places it in the communication
362 * buffer.
363 * @param block_id The id of the block to operate on
364 * @param offset Offset within the block.
365 * Must be smaller than the block size.
366 * @param bufsize Size of chunk to read within the block.
367 * Must be smaller than the block size.
368
369 * @return Returns -1 on error, 0 on success.
370 */
371int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
372{
373 struct region_device store;
374 struct region_device com_buf;
375
376 if (lookup_block_in_store(&store, block_id) < 0)
377 return -1;
378
379 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
380 if (!ptr)
381 return -1;
382
383 printk(BIOS_DEBUG, "smm store: reading %p block %d, offset=0x%x, size=%x\n",
384 ptr, block_id, offset, bufsize);
385
386 ssize_t ret = rdev_readat(&store, ptr, block_id * SMM_BLOCK_SIZE + offset, bufsize);
387 rdev_munmap(&com_buf, ptr);
388 if (ret < 0)
389 return -1;
390
391 return 0;
392}
393
394/**
395 * Writes the specified block of the SMMSTORE by reading it from the communication
396 * buffer.
397 * @param block_id The id of the block to operate on
398 * @param offset Offset within the block.
399 * Must be smaller than the block size.
400 * @param bufsize Size of chunk to read within the block.
401 * Must be smaller than the block size.
402
403 * @return Returns -1 on error, 0 on success.
404 */
405int smmstore_rawwrite_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
406{
407 struct region_device store;
408 struct region_device com_buf;
409
410 if (lookup_block_in_store(&store, block_id) < 0)
411 return -1;
412
413 if (rdev_chain(&store, &store, block_id * SMM_BLOCK_SIZE + offset, bufsize)) {
414 printk(BIOS_ERR, "smm store: not enough space for new data\n");
415 return -1;
416 }
417
418 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
419 if (!ptr)
420 return -1;
421
422 printk(BIOS_DEBUG, "smm store: writing %p block %d, offset=0x%x, size=%x\n",
423 ptr, block_id, offset, bufsize);
424
425 ssize_t ret = rdev_writeat(&store, ptr, 0, bufsize);
426 rdev_munmap(&com_buf, ptr);
427 if (ret < 0)
428 return -1;
429
430 return 0;
431}
432
433/**
434 * Erases the specified block of the SMMSTORE. The communication buffer remains untouched.
435 *
436 * @param block_id The id of the block to operate on
437 *
438 * @return Returns -1 on error, 0 on success.
439 */
440int smmstore_rawclear_region(uint32_t block_id)
441{
442 struct region_device store;
443
444 if (lookup_block_in_store(&store, block_id) < 0)
445 return -1;
446
447 ssize_t ret = rdev_eraseat(&store, block_id * SMM_BLOCK_SIZE, SMM_BLOCK_SIZE);
448 if (ret != SMM_BLOCK_SIZE) {
449 printk(BIOS_ERR, "smm store: erasing block failed\n");
450 return -1;
451 }
452
453 return 0;
454}