blob: 6ba3f5369590920fdeed53c428620186b84ed977 [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;
172
173}
174/*
175 * Append data to region
176 *
177 * Returns 0 on success, -1 on failure
178 */
179int smmstore_append_data(void *key, uint32_t key_sz, void *value,
180 uint32_t value_sz)
181{
182 struct region_device store;
183
184 if (lookup_store(&store) < 0) {
185 printk(BIOS_WARNING, "reading region failed\n");
Patrick Georgi9360fea2018-03-14 21:11:21 +0100186 return -1;
187 }
188
Arthur Heymansed50d852018-12-26 11:20:59 +0100189 ssize_t offset = 0;
190 ssize_t size;
191 uint8_t nul = 0;
192 if (scan_end(&store) != CB_SUCCESS)
193 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100194
Arthur Heymansed50d852018-12-26 11:20:59 +0100195 printk(BIOS_DEBUG, "used size looks legit\n");
196
197 printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n",
Patrick Georgi9360fea2018-03-14 21:11:21 +0100198 region_device_offset(&store), region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100199
Arthur Heymansed50d852018-12-26 11:20:59 +0100200 size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz
201 + sizeof(nul);
202 if (rdev_chain(&store, &store, 0, size)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100203 printk(BIOS_WARNING, "not enough space for new data\n");
204 return -1;
205 }
206
Arthur Heymansed50d852018-12-26 11:20:59 +0100207 if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz))
208 != sizeof(key_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100209 printk(BIOS_WARNING, "failed writing key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100210 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100211 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100212 offset += sizeof(key_sz);
213 if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz))
214 != sizeof(value_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100215 printk(BIOS_WARNING, "failed writing value 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(value_sz);
219 if (rdev_writeat(&store, key, offset, key_sz) != key_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100220 printk(BIOS_WARNING, "failed writing key data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100221 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100222 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100223 offset += key_sz;
224 if (rdev_writeat(&store, value, offset, value_sz) != value_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100225 printk(BIOS_WARNING, "failed writing value data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100226 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100227 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100228 offset += value_sz;
229 if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100230 printk(BIOS_WARNING, "failed writing termination\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100231 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100232 }
233
234 return 0;
235}
236
237/*
238 * Clear region
239 *
240 * Returns 0 on success, -1 on failure, including partial erase
241 */
242int smmstore_clear_region(void)
243{
244 struct region_device store;
245
246 if (lookup_store(&store) < 0) {
247 printk(BIOS_WARNING, "smm store: reading region failed\n");
248 return -1;
249 }
250
251 ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store));
252 if (res != region_device_sz(&store)) {
253 printk(BIOS_WARNING, "smm store: erasing region failed\n");
254 return -1;
255 }
256
257 return 0;
258}
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200259
260
261/* Implementation of Version 2 */
262
263static bool store_initialized;
Julius Wernerc8931972021-04-16 16:48:32 -0700264static struct region_device mdev_com_buf;
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200265
266static int smmstore_rdev_chain(struct region_device *rdev)
267{
268 if (!store_initialized)
269 return -1;
270
Julius Wernerc8931972021-04-16 16:48:32 -0700271 return rdev_chain_full(rdev, &mdev_com_buf);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200272}
273
274/**
275 * Call once before using the store. In SMM this must be called through an
276 * APM SMI handler providing the communication buffer address and length.
277 */
278int smmstore_init(void *buf, size_t len)
279{
280 if (!buf || len < SMM_BLOCK_SIZE)
281 return -1;
282
283 if (store_initialized)
284 return -1;
285
Julius Wernerc8931972021-04-16 16:48:32 -0700286 rdev_chain_mem_rw(&mdev_com_buf, buf, len);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200287
288 store_initialized = true;
289
290 return 0;
291}
292
293#if ENV_RAMSTAGE
294/**
295 * Provide metadata for the coreboot tables.
296 * Must only be called in ramstage, but not in SMM.
297 */
298int smmstore_get_info(struct smmstore_params_info *out)
299{
300 struct region_device store;
301
302 if (lookup_store(&store) < 0) {
303 printk(BIOS_ERR, "smm store: lookup of store failed\n");
304 return -1;
305 }
306
307 if (!IS_ALIGNED(region_device_offset(&store), SMM_BLOCK_SIZE)) {
308 printk(BIOS_ERR, "smm store: store not aligned to block size\n");
309 return -1;
310 }
311
312 out->block_size = SMM_BLOCK_SIZE;
313 out->num_blocks = region_device_sz(&store) / SMM_BLOCK_SIZE;
314
315 /* FIXME: Broken EDK2 always assumes memory mapped Firmware Block Volumes */
316 out->mmap_addr = (uintptr_t)rdev_mmap_full(&store);
317
318 printk(BIOS_DEBUG, "smm store: %d # blocks with size 0x%x\n",
319 out->num_blocks, out->block_size);
320
321 return 0;
322}
323#endif
324
325/* Returns -1 on error, 0 on success */
326static int lookup_block_in_store(struct region_device *store, uint32_t block_id)
327{
328 if (lookup_store(store) < 0) {
329 printk(BIOS_ERR, "smm store: lookup of store failed\n");
330 return -1;
331 }
332
333 if ((block_id * SMM_BLOCK_SIZE) >= region_device_sz(store)) {
334 printk(BIOS_ERR, "smm store: block ID out of range\n");
335 return -1;
336 }
337
338 return 0;
339}
340
341/* Returns NULL on error, pointer from rdev_mmap on success */
342static void *mmap_com_buf(struct region_device *com_buf, uint32_t offset, uint32_t bufsize)
343{
344 if (smmstore_rdev_chain(com_buf) < 0) {
345 printk(BIOS_ERR, "smm store: lookup of com buffer failed\n");
346 return NULL;
347 }
348
349 if (offset >= region_device_sz(com_buf)) {
350 printk(BIOS_ERR, "smm store: offset out of range\n");
351 return NULL;
352 }
353
354 void *ptr = rdev_mmap(com_buf, offset, bufsize);
355 if (!ptr)
356 printk(BIOS_ERR, "smm store: not enough space for new data\n");
357
358 return ptr;
359}
360
361/**
362 * Reads the specified block of the SMMSTORE and places it in the communication
363 * buffer.
364 * @param block_id The id of the block to operate on
365 * @param offset Offset within the block.
366 * Must be smaller than the block size.
367 * @param bufsize Size of chunk to read within the block.
368 * Must be smaller than the block size.
369
370 * @return Returns -1 on error, 0 on success.
371 */
372int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
373{
374 struct region_device store;
375 struct region_device com_buf;
376
377 if (lookup_block_in_store(&store, block_id) < 0)
378 return -1;
379
380 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
381 if (!ptr)
382 return -1;
383
384 printk(BIOS_DEBUG, "smm store: reading %p block %d, offset=0x%x, size=%x\n",
385 ptr, block_id, offset, bufsize);
386
387 ssize_t ret = rdev_readat(&store, ptr, block_id * SMM_BLOCK_SIZE + offset, bufsize);
388 rdev_munmap(&com_buf, ptr);
389 if (ret < 0)
390 return -1;
391
392 return 0;
393}
394
395/**
396 * Writes the specified block of the SMMSTORE by reading it from the communication
397 * buffer.
398 * @param block_id The id of the block to operate on
399 * @param offset Offset within the block.
400 * Must be smaller than the block size.
401 * @param bufsize Size of chunk to read within the block.
402 * Must be smaller than the block size.
403
404 * @return Returns -1 on error, 0 on success.
405 */
406int smmstore_rawwrite_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
407{
408 struct region_device store;
409 struct region_device com_buf;
410
411 if (lookup_block_in_store(&store, block_id) < 0)
412 return -1;
413
414 if (rdev_chain(&store, &store, block_id * SMM_BLOCK_SIZE + offset, bufsize)) {
415 printk(BIOS_ERR, "smm store: not enough space for new data\n");
416 return -1;
417 }
418
419 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
420 if (!ptr)
421 return -1;
422
423 printk(BIOS_DEBUG, "smm store: writing %p block %d, offset=0x%x, size=%x\n",
424 ptr, block_id, offset, bufsize);
425
426 ssize_t ret = rdev_writeat(&store, ptr, 0, bufsize);
427 rdev_munmap(&com_buf, ptr);
428 if (ret < 0)
429 return -1;
430
431 return 0;
432}
433
434/**
435 * Erases the specified block of the SMMSTORE. The communication buffer remains untouched.
436 *
437 * @param block_id The id of the block to operate on
438 *
439 * @return Returns -1 on error, 0 on success.
440 */
441int smmstore_rawclear_region(uint32_t block_id)
442{
443 struct region_device store;
444
445 if (lookup_block_in_store(&store, block_id) < 0)
446 return -1;
447
448 ssize_t ret = rdev_eraseat(&store, block_id * SMM_BLOCK_SIZE, SMM_BLOCK_SIZE);
449 if (ret != SMM_BLOCK_SIZE) {
450 printk(BIOS_ERR, "smm store: erasing block failed\n");
451 return -1;
452 }
453
454 return 0;
455}