blob: f1e07e41dd741c3157af314c69324e8af6e8609d [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) {
Patrick Rudolph4e00f152022-02-16 07:51:36 +0100100 done = 1;
101
102 if (fmap_locate_area_as_rdev_rw(SMMSTORE_REGION, &rdev)) {
103 printk(BIOS_WARNING,
104 "smm store: Unable to find SMM store FMAP region '%s'\n",
105 SMMSTORE_REGION);
106 ret = -1;
107 } else {
108 ret = 0;
109 }
110 }
111
112 *rstore = rdev;
113 return ret;
114}
Patrick Georgi9360fea2018-03-14 21:11:21 +0100115/*
116 * Read entire store into user provided buffer
117 *
118 * returns 0 on success, -1 on failure
119 * writes up to `*bufsize` bytes into `buf` and updates `*bufsize`
120 */
121int smmstore_read_region(void *buf, ssize_t *bufsize)
122{
123 struct region_device store;
124
125 if (bufsize == NULL)
126 return -1;
127
Patrick Georgi9360fea2018-03-14 21:11:21 +0100128 if (lookup_store(&store) < 0) {
129 printk(BIOS_WARNING, "reading region failed\n");
130 return -1;
131 }
132
Arthur Heymansed50d852018-12-26 11:20:59 +0100133 ssize_t tx = MIN(*bufsize, region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100134 *bufsize = rdev_readat(&store, buf, 0, tx);
135
136 if (*bufsize < 0)
137 return -1;
138
139 return 0;
140}
141
Arthur Heymansed50d852018-12-26 11:20:59 +0100142static enum cb_err scan_end(struct region_device *store)
Patrick Georgi9360fea2018-03-14 21:11:21 +0100143{
Patrick Georgi9360fea2018-03-14 21:11:21 +0100144 /* scan for end */
145 ssize_t end = 0;
146 uint32_t k_sz, v_sz;
Arthur Heymansed50d852018-12-26 11:20:59 +0100147 const ssize_t data_sz = region_device_sz(store);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100148 while (end < data_sz) {
149 /* make odd corner cases identifiable, eg. invalid v_sz */
150 k_sz = 0;
151
Arthur Heymansed50d852018-12-26 11:20:59 +0100152 if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100153 printk(BIOS_WARNING, "failed reading key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100154 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100155 }
156
157 /* found the end */
158 if (k_sz == 0xffffffff)
159 break;
160
161 /* something is fishy here:
162 * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while
163 * other problems are covered by the loop condition
164 */
165 if (k_sz > data_sz) {
166 printk(BIOS_WARNING, "key size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100167 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100168 }
169
Arthur Heymansed50d852018-12-26 11:20:59 +0100170 if (rdev_readat(store, &v_sz, end + sizeof(k_sz), sizeof(v_sz)) < 0) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100171 printk(BIOS_WARNING, "failed reading value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100172 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100173 }
174
175 if (v_sz > data_sz) {
176 printk(BIOS_WARNING, "value size out of bounds\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100177 return CB_ERR;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100178 }
179
Arthur Heymansed50d852018-12-26 11:20:59 +0100180 end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100181 end = ALIGN_UP(end, sizeof(uint32_t));
182 }
183
Arthur Heymansed50d852018-12-26 11:20:59 +0100184 printk(BIOS_DEBUG, "used smm store size might be 0x%zx bytes\n", end);
Patrick Georgi9360fea2018-03-14 21:11:21 +0100185
186 if (k_sz != 0xffffffff) {
187 printk(BIOS_WARNING,
Martin Roth3e25f852023-09-04 15:37:07 -0600188 "EOF of data marker looks invalid: 0x%x\n", k_sz);
Arthur Heymansed50d852018-12-26 11:20:59 +0100189 return CB_ERR;
190 }
191
192 if (rdev_chain(store, store, end, data_sz - end))
193 return CB_ERR;
194
195 return CB_SUCCESS;
Arthur Heymansed50d852018-12-26 11:20:59 +0100196}
197/*
198 * Append data to region
199 *
200 * Returns 0 on success, -1 on failure
201 */
202int smmstore_append_data(void *key, uint32_t key_sz, void *value,
203 uint32_t value_sz)
204{
205 struct region_device store;
206
207 if (lookup_store(&store) < 0) {
208 printk(BIOS_WARNING, "reading region failed\n");
Patrick Georgi9360fea2018-03-14 21:11:21 +0100209 return -1;
210 }
211
Arthur Heymansed50d852018-12-26 11:20:59 +0100212 ssize_t offset = 0;
213 ssize_t size;
214 uint8_t nul = 0;
215 if (scan_end(&store) != CB_SUCCESS)
216 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100217
Arthur Heymansed50d852018-12-26 11:20:59 +0100218 printk(BIOS_DEBUG, "used size looks legit\n");
219
220 printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n",
Patrick Georgi9360fea2018-03-14 21:11:21 +0100221 region_device_offset(&store), region_device_sz(&store));
Patrick Georgi9360fea2018-03-14 21:11:21 +0100222
Arthur Heymansed50d852018-12-26 11:20:59 +0100223 size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz
224 + sizeof(nul);
225 if (rdev_chain(&store, &store, 0, size)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100226 printk(BIOS_WARNING, "not enough space for new data\n");
227 return -1;
228 }
229
Arthur Heymansed50d852018-12-26 11:20:59 +0100230 if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz))
231 != sizeof(key_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100232 printk(BIOS_WARNING, "failed writing key size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100233 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100234 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100235 offset += sizeof(key_sz);
236 if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz))
237 != sizeof(value_sz)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100238 printk(BIOS_WARNING, "failed writing value size\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100239 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100240 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100241 offset += sizeof(value_sz);
242 if (rdev_writeat(&store, key, offset, key_sz) != key_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100243 printk(BIOS_WARNING, "failed writing key data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100244 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100245 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100246 offset += key_sz;
247 if (rdev_writeat(&store, value, offset, value_sz) != value_sz) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100248 printk(BIOS_WARNING, "failed writing value data\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100249 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100250 }
Arthur Heymansed50d852018-12-26 11:20:59 +0100251 offset += value_sz;
252 if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) {
Patrick Georgi9360fea2018-03-14 21:11:21 +0100253 printk(BIOS_WARNING, "failed writing termination\n");
Arthur Heymansed50d852018-12-26 11:20:59 +0100254 return -1;
Patrick Georgi9360fea2018-03-14 21:11:21 +0100255 }
256
257 return 0;
258}
259
260/*
261 * Clear region
262 *
263 * Returns 0 on success, -1 on failure, including partial erase
264 */
265int smmstore_clear_region(void)
266{
267 struct region_device store;
268
269 if (lookup_store(&store) < 0) {
270 printk(BIOS_WARNING, "smm store: reading region failed\n");
271 return -1;
272 }
273
274 ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store));
275 if (res != region_device_sz(&store)) {
276 printk(BIOS_WARNING, "smm store: erasing region failed\n");
277 return -1;
278 }
279
280 return 0;
281}
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200282
283
284/* Implementation of Version 2 */
285
Julius Wernerc8931972021-04-16 16:48:32 -0700286static struct region_device mdev_com_buf;
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200287
288static int smmstore_rdev_chain(struct region_device *rdev)
289{
Julius Wernerc8931972021-04-16 16:48:32 -0700290 return rdev_chain_full(rdev, &mdev_com_buf);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200291}
292
293/**
294 * Call once before using the store. In SMM this must be called through an
295 * APM SMI handler providing the communication buffer address and length.
296 */
297int smmstore_init(void *buf, size_t len)
298{
299 if (!buf || len < SMM_BLOCK_SIZE)
300 return -1;
301
Arthur Heymansd57d5e32023-12-27 20:54:19 +0100302 if (smm_points_to_smram(buf, len))
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200303 return -1;
304
Julius Wernerc8931972021-04-16 16:48:32 -0700305 rdev_chain_mem_rw(&mdev_com_buf, buf, len);
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200306
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200307 return 0;
308}
309
310#if ENV_RAMSTAGE
311/**
312 * Provide metadata for the coreboot tables.
313 * Must only be called in ramstage, but not in SMM.
314 */
315int smmstore_get_info(struct smmstore_params_info *out)
316{
317 struct region_device store;
318
319 if (lookup_store(&store) < 0) {
320 printk(BIOS_ERR, "smm store: lookup of store failed\n");
321 return -1;
322 }
323
324 if (!IS_ALIGNED(region_device_offset(&store), SMM_BLOCK_SIZE)) {
325 printk(BIOS_ERR, "smm store: store not aligned to block size\n");
326 return -1;
327 }
328
329 out->block_size = SMM_BLOCK_SIZE;
330 out->num_blocks = region_device_sz(&store) / SMM_BLOCK_SIZE;
331
Sean Rhodes38c99b52022-07-13 10:11:44 +0100332 /* FIXME: Broken edk2 always assumes memory mapped Firmware Block Volumes */
Patrick Rudolphbc744f52020-04-17 16:16:49 +0200333 out->mmap_addr = (uintptr_t)rdev_mmap_full(&store);
334
335 printk(BIOS_DEBUG, "smm store: %d # blocks with size 0x%x\n",
336 out->num_blocks, out->block_size);
337
338 return 0;
339}
340#endif
341
342/* Returns -1 on error, 0 on success */
343static int lookup_block_in_store(struct region_device *store, uint32_t block_id)
344{
345 if (lookup_store(store) < 0) {
346 printk(BIOS_ERR, "smm store: lookup of store failed\n");
347 return -1;
348 }
349
350 if ((block_id * SMM_BLOCK_SIZE) >= region_device_sz(store)) {
351 printk(BIOS_ERR, "smm store: block ID out of range\n");
352 return -1;
353 }
354
355 return 0;
356}
357
358/* Returns NULL on error, pointer from rdev_mmap on success */
359static void *mmap_com_buf(struct region_device *com_buf, uint32_t offset, uint32_t bufsize)
360{
361 if (smmstore_rdev_chain(com_buf) < 0) {
362 printk(BIOS_ERR, "smm store: lookup of com buffer failed\n");
363 return NULL;
364 }
365
366 if (offset >= region_device_sz(com_buf)) {
367 printk(BIOS_ERR, "smm store: offset out of range\n");
368 return NULL;
369 }
370
371 void *ptr = rdev_mmap(com_buf, offset, bufsize);
372 if (!ptr)
373 printk(BIOS_ERR, "smm store: not enough space for new data\n");
374
375 return ptr;
376}
377
378/**
379 * Reads the specified block of the SMMSTORE and places it in the communication
380 * buffer.
381 * @param block_id The id of the block to operate on
382 * @param offset Offset within the block.
383 * Must be smaller than the block size.
384 * @param bufsize Size of chunk to read within the block.
385 * Must be smaller than the block size.
386
387 * @return Returns -1 on error, 0 on success.
388 */
389int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
390{
391 struct region_device store;
392 struct region_device com_buf;
393
394 if (lookup_block_in_store(&store, block_id) < 0)
395 return -1;
396
397 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
398 if (!ptr)
399 return -1;
400
401 printk(BIOS_DEBUG, "smm store: reading %p block %d, offset=0x%x, size=%x\n",
402 ptr, block_id, offset, bufsize);
403
404 ssize_t ret = rdev_readat(&store, ptr, block_id * SMM_BLOCK_SIZE + offset, bufsize);
405 rdev_munmap(&com_buf, ptr);
406 if (ret < 0)
407 return -1;
408
409 return 0;
410}
411
412/**
413 * Writes the specified block of the SMMSTORE by reading it from the communication
414 * buffer.
415 * @param block_id The id of the block to operate on
416 * @param offset Offset within the block.
417 * Must be smaller than the block size.
418 * @param bufsize Size of chunk to read within the block.
419 * Must be smaller than the block size.
420
421 * @return Returns -1 on error, 0 on success.
422 */
423int smmstore_rawwrite_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
424{
425 struct region_device store;
426 struct region_device com_buf;
427
428 if (lookup_block_in_store(&store, block_id) < 0)
429 return -1;
430
431 if (rdev_chain(&store, &store, block_id * SMM_BLOCK_SIZE + offset, bufsize)) {
432 printk(BIOS_ERR, "smm store: not enough space for new data\n");
433 return -1;
434 }
435
436 void *ptr = mmap_com_buf(&com_buf, offset, bufsize);
437 if (!ptr)
438 return -1;
439
440 printk(BIOS_DEBUG, "smm store: writing %p block %d, offset=0x%x, size=%x\n",
441 ptr, block_id, offset, bufsize);
442
443 ssize_t ret = rdev_writeat(&store, ptr, 0, bufsize);
444 rdev_munmap(&com_buf, ptr);
445 if (ret < 0)
446 return -1;
447
448 return 0;
449}
450
451/**
452 * Erases the specified block of the SMMSTORE. The communication buffer remains untouched.
453 *
454 * @param block_id The id of the block to operate on
455 *
456 * @return Returns -1 on error, 0 on success.
457 */
458int smmstore_rawclear_region(uint32_t block_id)
459{
460 struct region_device store;
461
462 if (lookup_block_in_store(&store, block_id) < 0)
463 return -1;
464
465 ssize_t ret = rdev_eraseat(&store, block_id * SMM_BLOCK_SIZE, SMM_BLOCK_SIZE);
466 if (ret != SMM_BLOCK_SIZE) {
467 printk(BIOS_ERR, "smm store: erasing block failed\n");
468 return -1;
469 }
470
471 return 0;
472}