Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 2 | |
| 3 | #include <boot_device.h> |
Arthur Heymans | 8b04dc7 | 2018-12-01 15:28:03 +0100 | [diff] [blame] | 4 | #include <fmap.h> |
Elyes HAOUAS | 2195f7a | 2019-06-21 07:20:12 +0200 | [diff] [blame] | 5 | #include <commonlib/helpers.h> |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 6 | #include <commonlib/region.h> |
| 7 | #include <console/console.h> |
| 8 | #include <smmstore.h> |
Elyes HAOUAS | ede8dd0 | 2019-06-23 06:57:53 +0200 | [diff] [blame] | 9 | #include <types.h> |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 10 | |
Julius Werner | 6eeba88 | 2021-08-10 16:38:43 -0700 | [diff] [blame^] | 11 | #define SMMSTORE_REGION "SMMSTORE" |
| 12 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 13 | /* |
| 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 HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 22 | * )* |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 23 | * 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 Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 35 | static enum cb_err lookup_store_region(struct region *region) |
| 36 | { |
Julius Werner | 6eeba88 | 2021-08-10 16:38:43 -0700 | [diff] [blame^] | 37 | 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 Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | return CB_SUCCESS; |
| 45 | } |
| 46 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 47 | /* |
| 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 | */ |
| 59 | static int lookup_store(struct region_device *rstore) |
| 60 | { |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 61 | 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 Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 65 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 66 | if (lookup_store_region(®ion) != CB_SUCCESS) |
| 67 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 68 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 69 | if (boot_device_ro_subregion(®ion, &read_rdev) < 0) |
| 70 | return -1; |
| 71 | |
| 72 | if (boot_device_rw_subregion(®ion, &write_rdev) < 0) |
| 73 | return -1; |
| 74 | |
| 75 | rdev = incoherent_rdev_init(&store_irdev, ®ion, &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 Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 81 | } |
| 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 | */ |
| 89 | int smmstore_read_region(void *buf, ssize_t *bufsize) |
| 90 | { |
| 91 | struct region_device store; |
| 92 | |
| 93 | if (bufsize == NULL) |
| 94 | return -1; |
| 95 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 96 | if (lookup_store(&store) < 0) { |
| 97 | printk(BIOS_WARNING, "reading region failed\n"); |
| 98 | return -1; |
| 99 | } |
| 100 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 101 | ssize_t tx = MIN(*bufsize, region_device_sz(&store)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 102 | *bufsize = rdev_readat(&store, buf, 0, tx); |
| 103 | |
| 104 | if (*bufsize < 0) |
| 105 | return -1; |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 110 | static enum cb_err scan_end(struct region_device *store) |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 111 | { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 112 | /* scan for end */ |
| 113 | ssize_t end = 0; |
| 114 | uint32_t k_sz, v_sz; |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 115 | const ssize_t data_sz = region_device_sz(store); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 116 | while (end < data_sz) { |
| 117 | /* make odd corner cases identifiable, eg. invalid v_sz */ |
| 118 | k_sz = 0; |
| 119 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 120 | if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 121 | printk(BIOS_WARNING, "failed reading key size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 122 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 123 | } |
| 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 Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 135 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 138 | if (rdev_readat(store, &v_sz, end + sizeof(k_sz), sizeof(v_sz)) < 0) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 139 | printk(BIOS_WARNING, "failed reading value size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 140 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | if (v_sz > data_sz) { |
| 144 | printk(BIOS_WARNING, "value size out of bounds\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 145 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 146 | } |
| 147 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 148 | end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 149 | end = ALIGN_UP(end, sizeof(uint32_t)); |
| 150 | } |
| 151 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 152 | printk(BIOS_DEBUG, "used smm store size might be 0x%zx bytes\n", end); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 153 | |
| 154 | if (k_sz != 0xffffffff) { |
| 155 | printk(BIOS_WARNING, |
| 156 | "eof of data marker looks invalid: 0x%x\n", k_sz); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 157 | 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 | */ |
| 171 | int 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 Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 178 | return -1; |
| 179 | } |
| 180 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 181 | ssize_t offset = 0; |
| 182 | ssize_t size; |
| 183 | uint8_t nul = 0; |
| 184 | if (scan_end(&store) != CB_SUCCESS) |
| 185 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 186 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 187 | printk(BIOS_DEBUG, "used size looks legit\n"); |
| 188 | |
| 189 | printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n", |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 190 | region_device_offset(&store), region_device_sz(&store)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 191 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 192 | size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz |
| 193 | + sizeof(nul); |
| 194 | if (rdev_chain(&store, &store, 0, size)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 195 | printk(BIOS_WARNING, "not enough space for new data\n"); |
| 196 | return -1; |
| 197 | } |
| 198 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 199 | if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz)) |
| 200 | != sizeof(key_sz)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 201 | printk(BIOS_WARNING, "failed writing key size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 202 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 203 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 204 | offset += sizeof(key_sz); |
| 205 | if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz)) |
| 206 | != sizeof(value_sz)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 207 | printk(BIOS_WARNING, "failed writing value size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 208 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 209 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 210 | offset += sizeof(value_sz); |
| 211 | if (rdev_writeat(&store, key, offset, key_sz) != key_sz) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 212 | printk(BIOS_WARNING, "failed writing key data\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 213 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 214 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 215 | offset += key_sz; |
| 216 | if (rdev_writeat(&store, value, offset, value_sz) != value_sz) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 217 | printk(BIOS_WARNING, "failed writing value data\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 218 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 219 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 220 | offset += value_sz; |
| 221 | if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 222 | printk(BIOS_WARNING, "failed writing termination\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 223 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * Clear region |
| 231 | * |
| 232 | * Returns 0 on success, -1 on failure, including partial erase |
| 233 | */ |
| 234 | int 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 Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 251 | |
| 252 | |
| 253 | /* Implementation of Version 2 */ |
| 254 | |
| 255 | static bool store_initialized; |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 256 | static struct region_device mdev_com_buf; |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 257 | |
| 258 | static int smmstore_rdev_chain(struct region_device *rdev) |
| 259 | { |
| 260 | if (!store_initialized) |
| 261 | return -1; |
| 262 | |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 263 | return rdev_chain_full(rdev, &mdev_com_buf); |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 264 | } |
| 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 | */ |
| 270 | int 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 Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 278 | rdev_chain_mem_rw(&mdev_com_buf, buf, len); |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 279 | |
| 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 | */ |
| 290 | int 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 */ |
| 318 | static 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 */ |
| 334 | static 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 | */ |
| 364 | int 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 | */ |
| 398 | int 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 | */ |
| 433 | int 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 | } |