blob: a12cd58e105941512e18ca6a052dcdb7ce73a986 [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>
4#include <cbfs.h>
Arthur Heymans8b04dc72018-12-01 15:28:03 +01005#include <fmap.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
12/*
13 * The region format is still not finalized, but so far it looks like this:
14 * (
15 * uint32le_t key_sz
16 * uint32le_t value_sz
17 * uint8_t key[key_sz]
18 * uint8_t value[value_sz]
19 * uint8_t active
20 * align to 4 bytes
Elyes HAOUASa342f392018-10-17 10:56:26 +020021 * )*
Patrick Georgi9360fea2018-03-14 21:11:21 +010022 * uint32le_t endmarker = 0xffffffff
23 *
24 * active needs to be set to 0x00 for the entry to be valid. This satisfies
25 * the constraint that entries are either complete or will be ignored, as long
26 * as flash is written sequentially and into a fully erased block.
27 *
28 * Future additions to the format will split the region in half with an active
29 * block marker to allow safe compaction (ie. write the new data in the unused
30 * region, mark it active after the write completed). Otherwise a well-timed
31 * crash/reboot could clear out all variables.
32 */
33
Arthur Heymansed50d852018-12-26 11:20:59 +010034static enum cb_err lookup_store_region(struct region *region)
35{
36 if (CONFIG(SMMSTORE_IN_CBFS)) {
37 struct cbfsf file;
38 if (cbfs_locate_file_in_region(&file,
39 CONFIG_SMMSTORE_REGION,
40 CONFIG_SMMSTORE_FILENAME, NULL) < 0) {
41 printk(BIOS_WARNING,
42 "smm store: Unable to find SMM store file in region '%s'\n",
43 CONFIG_SMMSTORE_REGION);
44 return CB_ERR;
45 }
46 struct region_device rdev;
47 cbfs_file_data(&rdev, &file);
48 *region = *region_device_region(&rdev);
49 } else {
50 if (fmap_locate_area(CONFIG_SMMSTORE_REGION, region)) {
51 printk(BIOS_WARNING,
52 "smm store: Unable to find SMM store FMAP region '%s'\n",
53 CONFIG_SMMSTORE_REGION);
54 return CB_ERR;
55 }
56 }
57
58 return CB_SUCCESS;
59}
60
Patrick Georgi9360fea2018-03-14 21:11:21 +010061/*
62 * Return a region device that points into the store file.
63 *
64 * It's the image builder's responsibility to make it block aligned so that
65 * erase works without destroying other data.
66 *
67 * It doesn't cache the location to cope with flash changing underneath (eg
68 * due to an update)
69 *
70 * returns 0 on success, -1 on failure
71 * outputs the valid store rdev in rstore
72 */
73static int lookup_store(struct region_device *rstore)
74{
Arthur Heymansed50d852018-12-26 11:20:59 +010075 static struct region_device read_rdev, write_rdev;
76 static struct incoherent_rdev store_irdev;
77 struct region region;
78 const struct region_device *rdev;
Patrick Georgi9360fea2018-03-14 21:11:21 +010079
Arthur Heymansed50d852018-12-26 11:20:59 +010080 if (lookup_store_region(&region) != CB_SUCCESS)
81 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +010082
Arthur Heymansed50d852018-12-26 11:20:59 +010083 if (boot_device_ro_subregion(&region, &read_rdev) < 0)
84 return -1;
85
86 if (boot_device_rw_subregion(&region, &write_rdev) < 0)
87 return -1;
88
89 rdev = incoherent_rdev_init(&store_irdev, &region, &read_rdev, &write_rdev);
90
91 if (rdev == NULL)
92 return -1;
93
94 return rdev_chain(rstore, rdev, 0, region_device_sz(rdev));
Patrick Georgi9360fea2018-03-14 21:11:21 +010095}
96
97/*
98 * Read entire store into user provided buffer
99 *
100 * returns 0 on success, -1 on failure
101 * writes up to `*bufsize` bytes into `buf` and updates `*bufsize`
102 */
103int smmstore_read_region(void *buf, ssize_t *bufsize)
104{
105 struct region_device store;
106
107 if (bufsize == NULL)
108 return -1;
109
Patrick Georgi9360fea2018-03-14 21:11:21 +0100110 if (lookup_store(&store) < 0) {
111 printk(BIOS_WARNING, "reading region failed\n");
112 return -1;
113 }
114
Arthur Heymansed50d852018-12-26 11:20:59 +0100115 ssize_t tx = MIN(*bufsize, region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100116 *bufsize = rdev_readat(&store, buf, 0, tx);
117
118 if (*bufsize < 0)
119 return -1;
120
121 return 0;
122}
123
Arthur Heymansed50d852018-12-26 11:20:59 +0100124static enum cb_err scan_end(struct region_device *store)
Patrick Georgi9360fea2018-03-14 21:11:21 +0100125{
Patrick Georgi9360fea2018-03-14 21:11:21 +0100126 /* scan for end */
127 ssize_t end = 0;
128 uint32_t k_sz, v_sz;
Arthur Heymansed50d852018-12-26 11:20:59 +0100129 const ssize_t data_sz = region_device_sz(store);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100130 while (end < data_sz) {
131 /* make odd corner cases identifiable, eg. invalid v_sz */
132 k_sz = 0;
133
Arthur Heymansed50d852018-12-26 11:20:59 +0100134 if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100135 printk(BIOS_WARNING, "failed reading key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100136 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100137 }
138
139 /* found the end */
140 if (k_sz == 0xffffffff)
141 break;
142
143 /* something is fishy here:
144 * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while
145 * other problems are covered by the loop condition
146 */
147 if (k_sz > data_sz) {
148 printk(BIOS_WARNING, "key size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100149 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100150 }
151
Arthur Heymansed50d852018-12-26 11:20:59 +0100152 if (rdev_readat(store, &v_sz, end + sizeof(k_sz), sizeof(v_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100153 printk(BIOS_WARNING, "failed reading value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100154 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100155 }
156
157 if (v_sz > data_sz) {
158 printk(BIOS_WARNING, "value size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100159 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100160 }
161
Arthur Heymansed50d852018-12-26 11:20:59 +0100162 end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100163 end = ALIGN_UP(end, sizeof(uint32_t));
164 }
165
Arthur Heymansed50d852018-12-26 11:20:59 +0100166 printk(BIOS_DEBUG, "used smm store size might be 0x%zx bytes\n", end);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100167
168 if (k_sz != 0xffffffff) {
169 printk(BIOS_WARNING,
170 "eof of data marker looks invalid: 0x%x\n", k_sz);
Arthur Heymansed50d852018-12-26 11:20:59 +0100171 return CB_ERR;
172 }
173
174 if (rdev_chain(store, store, end, data_sz - end))
175 return CB_ERR;
176
177 return CB_SUCCESS;
178
179}
180/*
181 * Append data to region
182 *
183 * Returns 0 on success, -1 on failure
184 */
185int smmstore_append_data(void *key, uint32_t key_sz, void *value,
186 uint32_t value_sz)
187{
188 struct region_device store;
189
190 if (lookup_store(&store) < 0) {
191 printk(BIOS_WARNING, "reading region failed\n");
Patrick Georgi9360fea2018-03-14 21:11:21 +0100192 return -1;
193 }
194
Arthur Heymansed50d852018-12-26 11:20:59 +0100195 ssize_t offset = 0;
196 ssize_t size;
197 uint8_t nul = 0;
198 if (scan_end(&store) != CB_SUCCESS)
199 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100200
Arthur Heymansed50d852018-12-26 11:20:59 +0100201 printk(BIOS_DEBUG, "used size looks legit\n");
202
203 printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n",
Patrick Georgi9360fea2018-03-14 21:11:21 +0100204 region_device_offset(&store), region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100205
Arthur Heymansed50d852018-12-26 11:20:59 +0100206 size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz
207 + sizeof(nul);
208 if (rdev_chain(&store, &store, 0, size)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100209 printk(BIOS_WARNING, "not enough space for new data\n");
210 return -1;
211 }
212
Arthur Heymansed50d852018-12-26 11:20:59 +0100213 if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz))
214 != sizeof(key_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100215 printk(BIOS_WARNING, "failed writing key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100216 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100217 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100218 offset += sizeof(key_sz);
219 if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz))
220 != sizeof(value_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100221 printk(BIOS_WARNING, "failed writing value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100222 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100223 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100224 offset += sizeof(value_sz);
225 if (rdev_writeat(&store, key, offset, key_sz) != key_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100226 printk(BIOS_WARNING, "failed writing key data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100227 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100228 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100229 offset += key_sz;
230 if (rdev_writeat(&store, value, offset, value_sz) != value_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100231 printk(BIOS_WARNING, "failed writing value data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100232 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100233 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100234 offset += value_sz;
235 if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100236 printk(BIOS_WARNING, "failed writing termination\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100237 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100238 }
239
240 return 0;
241}
242
243/*
244 * Clear region
245 *
246 * Returns 0 on success, -1 on failure, including partial erase
247 */
248int smmstore_clear_region(void)
249{
250 struct region_device store;
251
252 if (lookup_store(&store) < 0) {
253 printk(BIOS_WARNING, "smm store: reading region failed\n");
254 return -1;
255 }
256
257 ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store));
258 if (res != region_device_sz(&store)) {
259 printk(BIOS_WARNING, "smm store: erasing region failed\n");
260 return -1;
261 }
262
263 return 0;
264}
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200265
266
267/* Implementation of Version 2 */
268
269static bool store_initialized;
Julius Wernerc8931972021-04-16 16:48:32 -0700270static struct region_device mdev_com_buf;
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200271
272static int smmstore_rdev_chain(struct region_device *rdev)
273{
274 if (!store_initialized)
275 return -1;
276
Julius Wernerc8931972021-04-16 16:48:32 -0700277 return rdev_chain_full(rdev, &mdev_com_buf);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200278}
279
280/**
281 * Call once before using the store. In SMM this must be called through an
282 * APM SMI handler providing the communication buffer address and length.
283 */
284int smmstore_init(void *buf, size_t len)
285{
286 if (!buf || len < SMM_BLOCK_SIZE)
287 return -1;
288
289 if (store_initialized)
290 return -1;
291
Julius Wernerc8931972021-04-16 16:48:32 -0700292 rdev_chain_mem_rw(&mdev_com_buf, buf, len);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200293
294 store_initialized = true;
295
296 return 0;
297}
298
299#if ENV_RAMSTAGE
300/**
301 * Provide metadata for the coreboot tables.
302 * Must only be called in ramstage, but not in SMM.
303 */
304int smmstore_get_info(struct smmstore_params_info *out)
305{
306 struct region_device store;
307
308 if (lookup_store(&store) < 0) {
309 printk(BIOS_ERR, "smm store: lookup of store failed\n");
310 return -1;
311 }
312
313 if (!IS_ALIGNED(region_device_offset(&store), SMM_BLOCK_SIZE)) {
314 printk(BIOS_ERR, "smm store: store not aligned to block size\n");
315 return -1;
316 }
317
318 out->block_size = SMM_BLOCK_SIZE;
319 out->num_blocks = region_device_sz(&store) / SMM_BLOCK_SIZE;
320
321 /* FIXME: Broken EDK2 always assumes memory mapped Firmware Block Volumes */
322 out->mmap_addr = (uintptr_t)rdev_mmap_full(&store);
323
324 printk(BIOS_DEBUG, "smm store: %d # blocks with size 0x%x\n",
325 out->num_blocks, out->block_size);
326
327 return 0;
328}
329#endif
330
331/* Returns -1 on error, 0 on success */
332static int lookup_block_in_store(struct region_device *store, uint32_t block_id)
333{
334 if (lookup_store(store) < 0) {
335 printk(BIOS_ERR, "smm store: lookup of store failed\n");
336 return -1;
337 }
338
339 if ((block_id * SMM_BLOCK_SIZE) >= region_device_sz(store)) {
340 printk(BIOS_ERR, "smm store: block ID out of range\n");
341 return -1;
342 }
343
344 return 0;
345}
346
347/* Returns NULL on error, pointer from rdev_mmap on success */
348static void *mmap_com_buf(struct region_device *com_buf, uint32_t offset, uint32_t bufsize)
349{
350 if (smmstore_rdev_chain(com_buf) < 0) {
351 printk(BIOS_ERR, "smm store: lookup of com buffer failed\n");
352 return NULL;
353 }
354
355 if (offset >= region_device_sz(com_buf)) {
356 printk(BIOS_ERR, "smm store: offset out of range\n");
357 return NULL;
358 }
359
360 void *ptr = rdev_mmap(com_buf, offset, bufsize);
361 if (!ptr)
362 printk(BIOS_ERR, "smm store: not enough space for new data\n");
363
364 return ptr;
365}
366
367/**
368 * Reads the specified block of the SMMSTORE and places it in the communication
369 * buffer.
370 * @param block_id The id of the block to operate on
371 * @param offset Offset within the block.
372 * Must be smaller than the block size.
373 * @param bufsize Size of chunk to read within the block.
374 * Must be smaller than the block size.
375
376 * @return Returns -1 on error, 0 on success.
377 */
378int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
379{
380 struct region_device store;
381 struct region_device com_buf;
382
383 if (lookup_block_in_store(&store, block_id) < 0)
384 return -1;
385
386 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
387 if (!ptr)
388 return -1;
389
390 printk(BIOS_DEBUG, "smm store: reading %p block %d, offset=0x%x, size=%x\n",
391 ptr, block_id, offset, bufsize);
392
393 ssize_t ret = rdev_readat(&store, ptr, block_id * SMM_BLOCK_SIZE + offset, bufsize);
394 rdev_munmap(&com_buf, ptr);
395 if (ret < 0)
396 return -1;
397
398 return 0;
399}
400
401/**
402 * Writes the specified block of the SMMSTORE by reading it from the communication
403 * buffer.
404 * @param block_id The id of the block to operate on
405 * @param offset Offset within the block.
406 * Must be smaller than the block size.
407 * @param bufsize Size of chunk to read within the block.
408 * Must be smaller than the block size.
409
410 * @return Returns -1 on error, 0 on success.
411 */
412int smmstore_rawwrite_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
413{
414 struct region_device store;
415 struct region_device com_buf;
416
417 if (lookup_block_in_store(&store, block_id) < 0)
418 return -1;
419
420 if (rdev_chain(&store, &store, block_id * SMM_BLOCK_SIZE + offset, bufsize)) {
421 printk(BIOS_ERR, "smm store: not enough space for new data\n");
422 return -1;
423 }
424
425 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
426 if (!ptr)
427 return -1;
428
429 printk(BIOS_DEBUG, "smm store: writing %p block %d, offset=0x%x, size=%x\n",
430 ptr, block_id, offset, bufsize);
431
432 ssize_t ret = rdev_writeat(&store, ptr, 0, bufsize);
433 rdev_munmap(&com_buf, ptr);
434 if (ret < 0)
435 return -1;
436
437 return 0;
438}
439
440/**
441 * Erases the specified block of the SMMSTORE. The communication buffer remains untouched.
442 *
443 * @param block_id The id of the block to operate on
444 *
445 * @return Returns -1 on error, 0 on success.
446 */
447int smmstore_rawclear_region(uint32_t block_id)
448{
449 struct region_device store;
450
451 if (lookup_block_in_store(&store, block_id) < 0)
452 return -1;
453
454 ssize_t ret = rdev_eraseat(&store, block_id * SMM_BLOCK_SIZE, SMM_BLOCK_SIZE);
455 if (ret != SMM_BLOCK_SIZE) {
456 printk(BIOS_ERR, "smm store: erasing block failed\n");
457 return -1;
458 }
459
460 return 0;
461}