coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
store.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <boot_device.h>
4 #include <fmap.h>
5 #include <fmap_config.h>
6 #include <commonlib/helpers.h>
7 #include <commonlib/region.h>
8 #include <console/console.h>
9 #include <smmstore.h>
10 #include <types.h>
11 
12 #define SMMSTORE_REGION "SMMSTORE"
13 
14 
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 
21 /*
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
30  * )*
31  * 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 
43 static enum cb_err lookup_store_region(struct region *region)
44 {
47  "smm store: Unable to find SMM store FMAP region '%s'\n",
49  return CB_ERR;
50  }
51 
52  return CB_SUCCESS;
53 }
54 
55 /*
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  */
67 static int lookup_store(struct region_device *rstore)
68 {
69  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;
73 
75  return -1;
76 
77  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));
89 }
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  */
97 int smmstore_read_region(void *buf, ssize_t *bufsize)
98 {
99  struct region_device store;
100 
101  if (bufsize == NULL)
102  return -1;
103 
104  if (lookup_store(&store) < 0) {
105  printk(BIOS_WARNING, "reading region failed\n");
106  return -1;
107  }
108 
109  ssize_t tx = MIN(*bufsize, region_device_sz(&store));
110  *bufsize = rdev_readat(&store, buf, 0, tx);
111 
112  if (*bufsize < 0)
113  return -1;
114 
115  return 0;
116 }
117 
118 static enum cb_err scan_end(struct region_device *store)
119 {
120  /* scan for end */
121  ssize_t end = 0;
122  uint32_t k_sz, v_sz;
123  const ssize_t data_sz = region_device_sz(store);
124  while (end < data_sz) {
125  /* make odd corner cases identifiable, eg. invalid v_sz */
126  k_sz = 0;
127 
128  if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) {
129  printk(BIOS_WARNING, "failed reading key size\n");
130  return CB_ERR;
131  }
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");
143  return CB_ERR;
144  }
145 
146  if (rdev_readat(store, &v_sz, end + sizeof(k_sz), sizeof(v_sz)) < 0) {
147  printk(BIOS_WARNING, "failed reading value size\n");
148  return CB_ERR;
149  }
150 
151  if (v_sz > data_sz) {
152  printk(BIOS_WARNING, "value size out of bounds\n");
153  return CB_ERR;
154  }
155 
156  end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1;
157  end = ALIGN_UP(end, sizeof(uint32_t));
158  }
159 
160  printk(BIOS_DEBUG, "used smm store size might be 0x%zx bytes\n", end);
161 
162  if (k_sz != 0xffffffff) {
164  "eof of data marker looks invalid: 0x%x\n", k_sz);
165  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  */
179 int 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");
186  return -1;
187  }
188 
189  ssize_t offset = 0;
190  ssize_t size;
191  uint8_t nul = 0;
192  if (scan_end(&store) != CB_SUCCESS)
193  return -1;
194 
195  printk(BIOS_DEBUG, "used size looks legit\n");
196 
197  printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n",
198  region_device_offset(&store), region_device_sz(&store));
199 
200  size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz
201  + sizeof(nul);
202  if (rdev_chain(&store, &store, 0, size)) {
203  printk(BIOS_WARNING, "not enough space for new data\n");
204  return -1;
205  }
206 
207  if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz))
208  != sizeof(key_sz)) {
209  printk(BIOS_WARNING, "failed writing key size\n");
210  return -1;
211  }
212  offset += sizeof(key_sz);
213  if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz))
214  != sizeof(value_sz)) {
215  printk(BIOS_WARNING, "failed writing value size\n");
216  return -1;
217  }
218  offset += sizeof(value_sz);
219  if (rdev_writeat(&store, key, offset, key_sz) != key_sz) {
220  printk(BIOS_WARNING, "failed writing key data\n");
221  return -1;
222  }
223  offset += key_sz;
224  if (rdev_writeat(&store, value, offset, value_sz) != value_sz) {
225  printk(BIOS_WARNING, "failed writing value data\n");
226  return -1;
227  }
228  offset += value_sz;
229  if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) {
230  printk(BIOS_WARNING, "failed writing termination\n");
231  return -1;
232  }
233 
234  return 0;
235 }
236 
237 /*
238  * Clear region
239  *
240  * Returns 0 on success, -1 on failure, including partial erase
241  */
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 }
259 
260 
261 /* Implementation of Version 2 */
262 
263 static bool store_initialized;
264 static struct region_device mdev_com_buf;
265 
267 {
268  if (!store_initialized)
269  return -1;
270 
272 }
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  */
278 int 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 
287 
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  */
298 int 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 
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 */
326 static 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 */
342 static 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  */
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  */
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  */
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 }
pte_t value
Definition: mmu.c:91
int boot_device_rw_subregion(const struct region *sub, struct region_device *subrd)
Definition: boot_device.c:36
int boot_device_ro_subregion(const struct region *sub, struct region_device *subrd)
Definition: boot_device.c:27
#define IS_ALIGNED(x, a)
Definition: helpers.h:19
#define MIN(a, b)
Definition: helpers.h:37
#define ALIGN_UP(x, a)
Definition: helpers.h:17
cb_err
coreboot error codes
Definition: cb_err.h:15
@ CB_ERR
Generic error code.
Definition: cb_err.h:17
@ CB_SUCCESS
Call completed successfully.
Definition: cb_err.h:16
#define printk(level,...)
Definition: stdlib.h:16
static struct region_device rdev
Definition: flashconsole.c:14
static size_t offset
Definition: flashconsole.c:16
int fmap_locate_area(const char *name, struct region *r)
Definition: fmap.c:164
#define BIOS_DEBUG
BIOS_DEBUG - Verbose output.
Definition: loglevel.h:128
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
#define BIOS_WARNING
BIOS_WARNING - Bad configuration.
Definition: loglevel.h:86
static uint8_t * buf
Definition: uart.c:7
ssize_t rdev_eraseat(const struct region_device *rd, size_t offset, size_t size)
Definition: region.c:114
int rdev_munmap(const struct region_device *rd, void *mapping)
Definition: region.c:65
static size_t region_device_sz(const struct region_device *rdev)
Definition: region.h:132
static int rdev_chain_full(struct region_device *child, const struct region_device *parent)
Definition: region.h:153
ssize_t rdev_writeat(const struct region_device *rd, const void *b, size_t offset, size_t size)
Definition: region.c:94
static void * rdev_mmap_full(const struct region_device *rd)
Definition: region.h:148
void * rdev_mmap(const struct region_device *rd, size_t offset, size_t size)
Definition: region.c:46
int rdev_chain_mem_rw(struct region_device *child, void *base, size_t size)
Definition: region.c:298
int rdev_chain(struct region_device *child, const struct region_device *parent, size_t offset, size_t size)
Definition: region.c:136
ssize_t rdev_readat(const struct region_device *rd, void *b, size_t offset, size_t size)
Definition: region.c:77
const struct region_device * incoherent_rdev_init(struct incoherent_rdev *irdev, const struct region *r, const struct region_device *read, const struct region_device *write)
Definition: region.c:517
static size_t region_device_offset(const struct region_device *rdev)
Definition: region.h:137
#define SMM_BLOCK_SIZE
Definition: smmstore.h:44
#define NULL
Definition: stddef.h:19
__SIZE_TYPE__ ssize_t
Definition: stddef.h:13
unsigned int uint32_t
Definition: stdint.h:14
unsigned long uintptr_t
Definition: stdint.h:21
unsigned char uint8_t
Definition: stdint.h:8
int smmstore_init(void *buf, size_t len)
Call once before using the store.
Definition: store.c:278
static int lookup_block_in_store(struct region_device *store, uint32_t block_id)
Definition: store.c:326
static void * mmap_com_buf(struct region_device *com_buf, uint32_t offset, uint32_t bufsize)
Definition: store.c:342
static enum cb_err lookup_store_region(struct region *region)
Definition: store.c:43
int smmstore_append_data(void *key, uint32_t key_sz, void *value, uint32_t value_sz)
Definition: store.c:179
int smmstore_read_region(void *buf, ssize_t *bufsize)
Definition: store.c:97
static int lookup_store(struct region_device *rstore)
Definition: store.c:67
_Static_assert(IS_ALIGNED(FMAP_SECTION_SMMSTORE_START, SMM_BLOCK_SIZE), "SMMSTORE FMAP region not aligned to 64K")
static enum cb_err scan_end(struct region_device *store)
Definition: store.c:118
int smmstore_clear_region(void)
Definition: store.c:242
int smmstore_rawclear_region(uint32_t block_id)
Erases the specified block of the SMMSTORE.
Definition: store.c:441
int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
Reads the specified block of the SMMSTORE and places it in the communication buffer.
Definition: store.c:372
int smmstore_rawwrite_region(uint32_t block_id, uint32_t offset, uint32_t bufsize)
Writes the specified block of the SMMSTORE by reading it from the communication buffer.
Definition: store.c:406
static bool store_initialized
Definition: store.c:263
static struct region_device mdev_com_buf
Definition: store.c:264
#define SMMSTORE_REGION
Definition: store.c:12
static int smmstore_rdev_chain(struct region_device *rdev)
Definition: store.c:266
Definition: region.h:76
uint32_t num_blocks
Definition: smmstore.h:60
uint32_t block_size
Definition: smmstore.h:61
uint32_t mmap_addr
Definition: smmstore.h:62