1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <boot_device.h>
5 #include <fmap_config.h>
6 #include <commonlib/helpers.h>
7 #include <commonlib/region.h>
8 #include <console/console.h>
12 #define SMMSTORE_REGION "SMMSTORE"
15 _Static_assert(IS_ALIGNED(FMAP_SECTION_SMMSTORE_START
, SMM_BLOCK_SIZE
),
16 "SMMSTORE FMAP region not aligned to 64K");
18 _Static_assert(SMM_BLOCK_SIZE
<= FMAP_SECTION_SMMSTORE_SIZE
,
19 "SMMSTORE FMAP region must be at least 64K");
22 * The region format is still not finalized, but so far it looks like this:
27 * uint8_t value[value_sz]
31 * uint32le_t endmarker = 0xffffffff
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.
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.
43 static enum cb_err
lookup_store_region(struct region
*region
)
45 if (fmap_locate_area(SMMSTORE_REGION
, region
)) {
47 "smm store: Unable to find SMM store FMAP region '%s'\n",
56 * Return a region device that points into the store file.
58 * It's the image builder's responsibility to make it block aligned so that
59 * erase works without destroying other data.
61 * It doesn't cache the location to cope with flash changing underneath (eg
64 * returns 0 on success, -1 on failure
65 * outputs the valid store rdev in rstore
67 static int lookup_store(struct region_device
*rstore
)
69 static struct region_device read_rdev
, write_rdev
;
70 static struct incoherent_rdev store_irdev
;
72 const struct region_device
*rdev
;
74 if (lookup_store_region(®ion
) != CB_SUCCESS
)
77 if (boot_device_ro_subregion(®ion
, &read_rdev
) < 0)
80 if (boot_device_rw_subregion(®ion
, &write_rdev
) < 0)
83 rdev
= incoherent_rdev_init(&store_irdev
, ®ion
, &read_rdev
, &write_rdev
);
88 return rdev_chain(rstore
, rdev
, 0, region_device_sz(rdev
));
91 /* this function is non reentrant */
92 int smmstore_lookup_region(struct region_device
*rstore
)
96 static struct region_device rdev
;
102 if (fmap_locate_area_as_rdev_rw(SMMSTORE_REGION
, &rdev
)) {
104 "smm store: Unable to find SMM store FMAP region '%s'\n",
116 * Read entire store into user provided buffer
118 * returns 0 on success, -1 on failure
119 * writes up to `*bufsize` bytes into `buf` and updates `*bufsize`
121 int smmstore_read_region(void *buf
, ssize_t
*bufsize
)
123 struct region_device store
;
128 if (lookup_store(&store
) < 0) {
129 printk(BIOS_WARNING
, "reading region failed\n");
133 ssize_t tx
= MIN(*bufsize
, region_device_sz(&store
));
134 *bufsize
= rdev_readat(&store
, buf
, 0, tx
);
142 static enum cb_err
scan_end(struct region_device
*store
)
147 const ssize_t data_sz
= region_device_sz(store
);
148 while (end
< data_sz
) {
149 /* make odd corner cases identifiable, eg. invalid v_sz */
152 if (rdev_readat(store
, &k_sz
, end
, sizeof(k_sz
)) < 0) {
153 printk(BIOS_WARNING
, "failed reading key size\n");
158 if (k_sz
== 0xffffffff)
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
165 if (k_sz
> data_sz
) {
166 printk(BIOS_WARNING
, "key size out of bounds\n");
170 if (rdev_readat(store
, &v_sz
, end
+ sizeof(k_sz
), sizeof(v_sz
)) < 0) {
171 printk(BIOS_WARNING
, "failed reading value size\n");
175 if (v_sz
> data_sz
) {
176 printk(BIOS_WARNING
, "value size out of bounds\n");
180 end
+= sizeof(k_sz
) + sizeof(v_sz
) + k_sz
+ v_sz
+ 1;
181 end
= ALIGN_UP(end
, sizeof(uint32_t));
184 printk(BIOS_DEBUG
, "used smm store size might be 0x%zx bytes\n", end
);
186 if (k_sz
!= 0xffffffff) {
188 "eof of data marker looks invalid: 0x%x\n", k_sz
);
192 if (rdev_chain(store
, store
, end
, data_sz
- end
))
198 * Append data to region
200 * Returns 0 on success, -1 on failure
202 int smmstore_append_data(void *key
, uint32_t key_sz
, void *value
,
205 struct region_device store
;
207 if (lookup_store(&store
) < 0) {
208 printk(BIOS_WARNING
, "reading region failed\n");
215 if (scan_end(&store
) != CB_SUCCESS
)
218 printk(BIOS_DEBUG
, "used size looks legit\n");
220 printk(BIOS_DEBUG
, "open (%zx, %zx) for writing\n",
221 region_device_offset(&store
), region_device_sz(&store
));
223 size
= sizeof(key_sz
) + sizeof(value_sz
) + key_sz
+ value_sz
225 if (rdev_chain(&store
, &store
, 0, size
)) {
226 printk(BIOS_WARNING
, "not enough space for new data\n");
230 if (rdev_writeat(&store
, &key_sz
, offset
, sizeof(key_sz
))
232 printk(BIOS_WARNING
, "failed writing key size\n");
235 offset
+= sizeof(key_sz
);
236 if (rdev_writeat(&store
, &value_sz
, offset
, sizeof(value_sz
))
237 != sizeof(value_sz
)) {
238 printk(BIOS_WARNING
, "failed writing value size\n");
241 offset
+= sizeof(value_sz
);
242 if (rdev_writeat(&store
, key
, offset
, key_sz
) != key_sz
) {
243 printk(BIOS_WARNING
, "failed writing key data\n");
247 if (rdev_writeat(&store
, value
, offset
, value_sz
) != value_sz
) {
248 printk(BIOS_WARNING
, "failed writing value data\n");
252 if (rdev_writeat(&store
, &nul
, offset
, sizeof(nul
)) != sizeof(nul
)) {
253 printk(BIOS_WARNING
, "failed writing termination\n");
263 * Returns 0 on success, -1 on failure, including partial erase
265 int smmstore_clear_region(void)
267 struct region_device store
;
269 if (lookup_store(&store
) < 0) {
270 printk(BIOS_WARNING
, "smm store: reading region failed\n");
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");
284 /* Implementation of Version 2 */
286 static bool store_initialized
;
287 static struct region_device mdev_com_buf
;
289 static int smmstore_rdev_chain(struct region_device
*rdev
)
291 if (!store_initialized
)
294 return rdev_chain_full(rdev
, &mdev_com_buf
);
298 * Call once before using the store. In SMM this must be called through an
299 * APM SMI handler providing the communication buffer address and length.
301 int smmstore_init(void *buf
, size_t len
)
303 if (!buf
|| len
< SMM_BLOCK_SIZE
)
306 if (store_initialized
)
309 rdev_chain_mem_rw(&mdev_com_buf
, buf
, len
);
311 store_initialized
= true;
318 * Provide metadata for the coreboot tables.
319 * Must only be called in ramstage, but not in SMM.
321 int smmstore_get_info(struct smmstore_params_info
*out
)
323 struct region_device store
;
325 if (lookup_store(&store
) < 0) {
326 printk(BIOS_ERR
, "smm store: lookup of store failed\n");
330 if (!IS_ALIGNED(region_device_offset(&store
), SMM_BLOCK_SIZE
)) {
331 printk(BIOS_ERR
, "smm store: store not aligned to block size\n");
335 out
->block_size
= SMM_BLOCK_SIZE
;
336 out
->num_blocks
= region_device_sz(&store
) / SMM_BLOCK_SIZE
;
338 /* FIXME: Broken edk2 always assumes memory mapped Firmware Block Volumes */
339 out
->mmap_addr
= (uintptr_t)rdev_mmap_full(&store
);
341 printk(BIOS_DEBUG
, "smm store: %d # blocks with size 0x%x\n",
342 out
->num_blocks
, out
->block_size
);
348 /* Returns -1 on error, 0 on success */
349 static int lookup_block_in_store(struct region_device
*store
, uint32_t block_id
)
351 if (lookup_store(store
) < 0) {
352 printk(BIOS_ERR
, "smm store: lookup of store failed\n");
356 if ((block_id
* SMM_BLOCK_SIZE
) >= region_device_sz(store
)) {
357 printk(BIOS_ERR
, "smm store: block ID out of range\n");
364 /* Returns NULL on error, pointer from rdev_mmap on success */
365 static void *mmap_com_buf(struct region_device
*com_buf
, uint32_t offset
, uint32_t bufsize
)
367 if (smmstore_rdev_chain(com_buf
) < 0) {
368 printk(BIOS_ERR
, "smm store: lookup of com buffer failed\n");
372 if (offset
>= region_device_sz(com_buf
)) {
373 printk(BIOS_ERR
, "smm store: offset out of range\n");
377 void *ptr
= rdev_mmap(com_buf
, offset
, bufsize
);
379 printk(BIOS_ERR
, "smm store: not enough space for new data\n");
385 * Reads the specified block of the SMMSTORE and places it in the communication
387 * @param block_id The id of the block to operate on
388 * @param offset Offset within the block.
389 * Must be smaller than the block size.
390 * @param bufsize Size of chunk to read within the block.
391 * Must be smaller than the block size.
393 * @return Returns -1 on error, 0 on success.
395 int smmstore_rawread_region(uint32_t block_id
, uint32_t offset
, uint32_t bufsize
)
397 struct region_device store
;
398 struct region_device com_buf
;
400 if (lookup_block_in_store(&store
, block_id
) < 0)
403 void *ptr
= mmap_com_buf(&com_buf
, offset
, bufsize
);
407 printk(BIOS_DEBUG
, "smm store: reading %p block %d, offset=0x%x, size=%x\n",
408 ptr
, block_id
, offset
, bufsize
);
410 ssize_t ret
= rdev_readat(&store
, ptr
, block_id
* SMM_BLOCK_SIZE
+ offset
, bufsize
);
411 rdev_munmap(&com_buf
, ptr
);
419 * Writes the specified block of the SMMSTORE by reading it from the communication
421 * @param block_id The id of the block to operate on
422 * @param offset Offset within the block.
423 * Must be smaller than the block size.
424 * @param bufsize Size of chunk to read within the block.
425 * Must be smaller than the block size.
427 * @return Returns -1 on error, 0 on success.
429 int smmstore_rawwrite_region(uint32_t block_id
, uint32_t offset
, uint32_t bufsize
)
431 struct region_device store
;
432 struct region_device com_buf
;
434 if (lookup_block_in_store(&store
, block_id
) < 0)
437 if (rdev_chain(&store
, &store
, block_id
* SMM_BLOCK_SIZE
+ offset
, bufsize
)) {
438 printk(BIOS_ERR
, "smm store: not enough space for new data\n");
442 void *ptr
= mmap_com_buf(&com_buf
, offset
, bufsize
);
446 printk(BIOS_DEBUG
, "smm store: writing %p block %d, offset=0x%x, size=%x\n",
447 ptr
, block_id
, offset
, bufsize
);
449 ssize_t ret
= rdev_writeat(&store
, ptr
, 0, bufsize
);
450 rdev_munmap(&com_buf
, ptr
);
458 * Erases the specified block of the SMMSTORE. The communication buffer remains untouched.
460 * @param block_id The id of the block to operate on
462 * @return Returns -1 on error, 0 on success.
464 int smmstore_rawclear_region(uint32_t block_id
)
466 struct region_device store
;
468 if (lookup_block_in_store(&store
, block_id
) < 0)
471 ssize_t ret
= rdev_eraseat(&store
, block_id
* SMM_BLOCK_SIZE
, SMM_BLOCK_SIZE
);
472 if (ret
!= SMM_BLOCK_SIZE
) {
473 printk(BIOS_ERR
, "smm store: erasing block failed\n");