blob: 24e8a88edd2e1dacf0351a7ee1e1df98fc15613a [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>
Elyes HAOUAS2195f7a2019-06-21 07:20:12 +02005#include <commonlib/helpers.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +01006#include <commonlib/region.h>
7#include <console/console.h>
8#include <smmstore.h>
Elyes HAOUASede8dd02019-06-23 06:57:53 +02009#include <types.h>
Patrick Georgi9360fea2018-03-14 21:11:21 +010010
Julius Werner6eeba882021-08-10 16:38:43 -070011#define SMMSTORE_REGION "SMMSTORE"
12
Patrick Georgi9360fea2018-03-14 21:11:21 +010013/*
14 * The region format is still not finalized, but so far it looks like this:
15 * (
16 * uint32le_t key_sz
17 * uint32le_t value_sz
18 * uint8_t key[key_sz]
19 * uint8_t value[value_sz]
20 * uint8_t active
21 * align to 4 bytes
Elyes HAOUASa342f392018-10-17 10:56:26 +020022 * )*
Patrick Georgi9360fea2018-03-14 21:11:21 +010023 * uint32le_t endmarker = 0xffffffff
24 *
25 * active needs to be set to 0x00 for the entry to be valid. This satisfies
26 * the constraint that entries are either complete or will be ignored, as long
27 * as flash is written sequentially and into a fully erased block.
28 *
29 * Future additions to the format will split the region in half with an active
30 * block marker to allow safe compaction (ie. write the new data in the unused
31 * region, mark it active after the write completed). Otherwise a well-timed
32 * crash/reboot could clear out all variables.
33 */
34
Arthur Heymansed50d852018-12-26 11:20:59 +010035static enum cb_err lookup_store_region(struct region *region)
36{
Julius Werner6eeba882021-08-10 16:38:43 -070037 if (fmap_locate_area(SMMSTORE_REGION, region)) {
38 printk(BIOS_WARNING,
39 "smm store: Unable to find SMM store FMAP region '%s'\n",
40 SMMSTORE_REGION);
41 return CB_ERR;
Arthur Heymansed50d852018-12-26 11:20:59 +010042 }
43
44 return CB_SUCCESS;
45}
46
Patrick Georgi9360fea2018-03-14 21:11:21 +010047/*
48 * Return a region device that points into the store file.
49 *
50 * It's the image builder's responsibility to make it block aligned so that
51 * erase works without destroying other data.
52 *
53 * It doesn't cache the location to cope with flash changing underneath (eg
54 * due to an update)
55 *
56 * returns 0 on success, -1 on failure
57 * outputs the valid store rdev in rstore
58 */
59static int lookup_store(struct region_device *rstore)
60{
Arthur Heymansed50d852018-12-26 11:20:59 +010061 static struct region_device read_rdev, write_rdev;
62 static struct incoherent_rdev store_irdev;
63 struct region region;
64 const struct region_device *rdev;
Patrick Georgi9360fea2018-03-14 21:11:21 +010065
Arthur Heymansed50d852018-12-26 11:20:59 +010066 if (lookup_store_region(&region) != CB_SUCCESS)
67 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +010068
Arthur Heymansed50d852018-12-26 11:20:59 +010069 if (boot_device_ro_subregion(&region, &read_rdev) < 0)
70 return -1;
71
72 if (boot_device_rw_subregion(&region, &write_rdev) < 0)
73 return -1;
74
75 rdev = incoherent_rdev_init(&store_irdev, &region, &read_rdev, &write_rdev);
76
77 if (rdev == NULL)
78 return -1;
79
80 return rdev_chain(rstore, rdev, 0, region_device_sz(rdev));
Patrick Georgi9360fea2018-03-14 21:11:21 +010081}
82
83/*
84 * Read entire store into user provided buffer
85 *
86 * returns 0 on success, -1 on failure
87 * writes up to `*bufsize` bytes into `buf` and updates `*bufsize`
88 */
89int smmstore_read_region(void *buf, ssize_t *bufsize)
90{
91 struct region_device store;
92
93 if (bufsize == NULL)
94 return -1;
95
Patrick Georgi9360fea2018-03-14 21:11:21 +010096 if (lookup_store(&store) < 0) {
97 printk(BIOS_WARNING, "reading region failed\n");
98 return -1;
99 }
100
Arthur Heymansed50d852018-12-26 11:20:59 +0100101 ssize_t tx = MIN(*bufsize, region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100102 *bufsize = rdev_readat(&store, buf, 0, tx);
103
104 if (*bufsize < 0)
105 return -1;
106
107 return 0;
108}
109
Arthur Heymansed50d852018-12-26 11:20:59 +0100110static enum cb_err scan_end(struct region_device *store)
Patrick Georgi9360fea2018-03-14 21:11:21 +0100111{
Patrick Georgi9360fea2018-03-14 21:11:21 +0100112 /* scan for end */
113 ssize_t end = 0;
114 uint32_t k_sz, v_sz;
Arthur Heymansed50d852018-12-26 11:20:59 +0100115 const ssize_t data_sz = region_device_sz(store);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100116 while (end < data_sz) {
117 /* make odd corner cases identifiable, eg. invalid v_sz */
118 k_sz = 0;
119
Arthur Heymansed50d852018-12-26 11:20:59 +0100120 if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100121 printk(BIOS_WARNING, "failed reading key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100122 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100123 }
124
125 /* found the end */
126 if (k_sz == 0xffffffff)
127 break;
128
129 /* something is fishy here:
130 * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while
131 * other problems are covered by the loop condition
132 */
133 if (k_sz > data_sz) {
134 printk(BIOS_WARNING, "key size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100135 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100136 }
137
Arthur Heymansed50d852018-12-26 11:20:59 +0100138 if (rdev_readat(store, &v_sz, end + sizeof(k_sz), sizeof(v_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100139 printk(BIOS_WARNING, "failed reading value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100140 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100141 }
142
143 if (v_sz > data_sz) {
144 printk(BIOS_WARNING, "value size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100145 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100146 }
147
Arthur Heymansed50d852018-12-26 11:20:59 +0100148 end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100149 end = ALIGN_UP(end, sizeof(uint32_t));
150 }
151
Arthur Heymansed50d852018-12-26 11:20:59 +0100152 printk(BIOS_DEBUG, "used smm store size might be 0x%zx bytes\n", end);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100153
154 if (k_sz != 0xffffffff) {
155 printk(BIOS_WARNING,
156 "eof of data marker looks invalid: 0x%x\n", k_sz);
Arthur Heymansed50d852018-12-26 11:20:59 +0100157 return CB_ERR;
158 }
159
160 if (rdev_chain(store, store, end, data_sz - end))
161 return CB_ERR;
162
163 return CB_SUCCESS;
164
165}
166/*
167 * Append data to region
168 *
169 * Returns 0 on success, -1 on failure
170 */
171int smmstore_append_data(void *key, uint32_t key_sz, void *value,
172 uint32_t value_sz)
173{
174 struct region_device store;
175
176 if (lookup_store(&store) < 0) {
177 printk(BIOS_WARNING, "reading region failed\n");
Patrick Georgi9360fea2018-03-14 21:11:21 +0100178 return -1;
179 }
180
Arthur Heymansed50d852018-12-26 11:20:59 +0100181 ssize_t offset = 0;
182 ssize_t size;
183 uint8_t nul = 0;
184 if (scan_end(&store) != CB_SUCCESS)
185 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100186
Arthur Heymansed50d852018-12-26 11:20:59 +0100187 printk(BIOS_DEBUG, "used size looks legit\n");
188
189 printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n",
Patrick Georgi9360fea2018-03-14 21:11:21 +0100190 region_device_offset(&store), region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100191
Arthur Heymansed50d852018-12-26 11:20:59 +0100192 size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz
193 + sizeof(nul);
194 if (rdev_chain(&store, &store, 0, size)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100195 printk(BIOS_WARNING, "not enough space for new data\n");
196 return -1;
197 }
198
Arthur Heymansed50d852018-12-26 11:20:59 +0100199 if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz))
200 != sizeof(key_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100201 printk(BIOS_WARNING, "failed writing key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100202 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100203 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100204 offset += sizeof(key_sz);
205 if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz))
206 != sizeof(value_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100207 printk(BIOS_WARNING, "failed writing value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100208 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100209 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100210 offset += sizeof(value_sz);
211 if (rdev_writeat(&store, key, offset, key_sz) != key_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100212 printk(BIOS_WARNING, "failed writing key data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100213 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100214 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100215 offset += key_sz;
216 if (rdev_writeat(&store, value, offset, value_sz) != value_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100217 printk(BIOS_WARNING, "failed writing value data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100218 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100219 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100220 offset += value_sz;
221 if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100222 printk(BIOS_WARNING, "failed writing termination\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100223 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100224 }
225
226 return 0;
227}
228
229/*
230 * Clear region
231 *
232 * Returns 0 on success, -1 on failure, including partial erase
233 */
234int smmstore_clear_region(void)
235{
236 struct region_device store;
237
238 if (lookup_store(&store) < 0) {
239 printk(BIOS_WARNING, "smm store: reading region failed\n");
240 return -1;
241 }
242
243 ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store));
244 if (res != region_device_sz(&store)) {
245 printk(BIOS_WARNING, "smm store: erasing region failed\n");
246 return -1;
247 }
248
249 return 0;
250}
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200251
252
253/* Implementation of Version 2 */
254
255static bool store_initialized;
Julius Wernerc8931972021-04-16 16:48:32 -0700256static struct region_device mdev_com_buf;
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200257
258static int smmstore_rdev_chain(struct region_device *rdev)
259{
260 if (!store_initialized)
261 return -1;
262
Julius Wernerc8931972021-04-16 16:48:32 -0700263 return rdev_chain_full(rdev, &mdev_com_buf);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200264}
265
266/**
267 * Call once before using the store. In SMM this must be called through an
268 * APM SMI handler providing the communication buffer address and length.
269 */
270int smmstore_init(void *buf, size_t len)
271{
272 if (!buf || len < SMM_BLOCK_SIZE)
273 return -1;
274
275 if (store_initialized)
276 return -1;
277
Julius Wernerc8931972021-04-16 16:48:32 -0700278 rdev_chain_mem_rw(&mdev_com_buf, buf, len);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200279
280 store_initialized = true;
281
282 return 0;
283}
284
285#if ENV_RAMSTAGE
286/**
287 * Provide metadata for the coreboot tables.
288 * Must only be called in ramstage, but not in SMM.
289 */
290int smmstore_get_info(struct smmstore_params_info *out)
291{
292 struct region_device store;
293
294 if (lookup_store(&store) < 0) {
295 printk(BIOS_ERR, "smm store: lookup of store failed\n");
296 return -1;
297 }
298
299 if (!IS_ALIGNED(region_device_offset(&store), SMM_BLOCK_SIZE)) {
300 printk(BIOS_ERR, "smm store: store not aligned to block size\n");
301 return -1;
302 }
303
304 out->block_size = SMM_BLOCK_SIZE;
305 out->num_blocks = region_device_sz(&store) / SMM_BLOCK_SIZE;
306
307 /* FIXME: Broken EDK2 always assumes memory mapped Firmware Block Volumes */
308 out->mmap_addr = (uintptr_t)rdev_mmap_full(&store);
309
310 printk(BIOS_DEBUG, "smm store: %d # blocks with size 0x%x\n",
311 out->num_blocks, out->block_size);
312
313 return 0;
314}
315#endif
316
317/* Returns -1 on error, 0 on success */
318static int lookup_block_in_store(struct region_device *store, uint32_t block_id)
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 ((block_id * SMM_BLOCK_SIZE) >= region_device_sz(store)) {
326 printk(BIOS_ERR, "smm store: block ID out of range\n");
327 return -1;
328 }
329
330 return 0;
331}
332
333/* Returns NULL on error, pointer from rdev_mmap on success */
334static void *mmap_com_buf(struct region_device *com_buf, uint32_t offset, uint32_t bufsize)
335{
336 if (smmstore_rdev_chain(com_buf) < 0) {
337 printk(BIOS_ERR, "smm store: lookup of com buffer failed\n");
338 return NULL;
339 }
340
341 if (offset >= region_device_sz(com_buf)) {
342 printk(BIOS_ERR, "smm store: offset out of range\n");
343 return NULL;
344 }
345
346 void *ptr = rdev_mmap(com_buf, offset, bufsize);
347 if (!ptr)
348 printk(BIOS_ERR, "smm store: not enough space for new data\n");
349
350 return ptr;
351}
352
353/**
354 * Reads the specified block of the SMMSTORE and places it in the communication
355 * buffer.
356 * @param block_id The id of the block to operate on
357 * @param offset Offset within the block.
358 * Must be smaller than the block size.
359 * @param bufsize Size of chunk to read within the block.
360 * Must be smaller than the block size.
361
362 * @return Returns -1 on error, 0 on success.
363 */
364int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
365{
366 struct region_device store;
367 struct region_device com_buf;
368
369 if (lookup_block_in_store(&store, block_id) < 0)
370 return -1;
371
372 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
373 if (!ptr)
374 return -1;
375
376 printk(BIOS_DEBUG, "smm store: reading %p block %d, offset=0x%x, size=%x\n",
377 ptr, block_id, offset, bufsize);
378
379 ssize_t ret = rdev_readat(&store, ptr, block_id * SMM_BLOCK_SIZE + offset, bufsize);
380 rdev_munmap(&com_buf, ptr);
381 if (ret < 0)
382 return -1;
383
384 return 0;
385}
386
387/**
388 * Writes the specified block of the SMMSTORE by reading it from the communication
389 * buffer.
390 * @param block_id The id of the block to operate on
391 * @param offset Offset within the block.
392 * Must be smaller than the block size.
393 * @param bufsize Size of chunk to read within the block.
394 * Must be smaller than the block size.
395
396 * @return Returns -1 on error, 0 on success.
397 */
398int smmstore_rawwrite_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
399{
400 struct region_device store;
401 struct region_device com_buf;
402
403 if (lookup_block_in_store(&store, block_id) < 0)
404 return -1;
405
406 if (rdev_chain(&store, &store, block_id * SMM_BLOCK_SIZE + offset, bufsize)) {
407 printk(BIOS_ERR, "smm store: not enough space for new data\n");
408 return -1;
409 }
410
411 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
412 if (!ptr)
413 return -1;
414
415 printk(BIOS_DEBUG, "smm store: writing %p block %d, offset=0x%x, size=%x\n",
416 ptr, block_id, offset, bufsize);
417
418 ssize_t ret = rdev_writeat(&store, ptr, 0, bufsize);
419 rdev_munmap(&com_buf, ptr);
420 if (ret < 0)
421 return -1;
422
423 return 0;
424}
425
426/**
427 * Erases the specified block of the SMMSTORE. The communication buffer remains untouched.
428 *
429 * @param block_id The id of the block to operate on
430 *
431 * @return Returns -1 on error, 0 on success.
432 */
433int smmstore_rawclear_region(uint32_t block_id)
434{
435 struct region_device store;
436
437 if (lookup_block_in_store(&store, block_id) < 0)
438 return -1;
439
440 ssize_t ret = rdev_eraseat(&store, block_id * SMM_BLOCK_SIZE, SMM_BLOCK_SIZE);
441 if (ret != SMM_BLOCK_SIZE) {
442 printk(BIOS_ERR, "smm store: erasing block failed\n");
443 return -1;
444 }
445
446 return 0;
447}