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> |
| 4 | #include <cbfs.h> |
Arthur Heymans | 8b04dc7 | 2018-12-01 15:28:03 +0100 | [diff] [blame] | 5 | #include <fmap.h> |
Elyes HAOUAS | 2195f7a | 2019-06-21 07:20:12 +0200 | [diff] [blame] | 6 | #include <commonlib/helpers.h> |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 7 | #include <commonlib/region.h> |
| 8 | #include <console/console.h> |
| 9 | #include <smmstore.h> |
Elyes HAOUAS | ede8dd0 | 2019-06-23 06:57:53 +0200 | [diff] [blame] | 10 | #include <types.h> |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * The region format is still not finalized, but so far it looks like this: |
| 14 | * ( |
| 15 | * uint32le_t key_sz |
| 16 | * uint32le_t value_sz |
| 17 | * uint8_t key[key_sz] |
| 18 | * uint8_t value[value_sz] |
| 19 | * uint8_t active |
| 20 | * align to 4 bytes |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 21 | * )* |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 22 | * uint32le_t endmarker = 0xffffffff |
| 23 | * |
| 24 | * active needs to be set to 0x00 for the entry to be valid. This satisfies |
| 25 | * the constraint that entries are either complete or will be ignored, as long |
| 26 | * as flash is written sequentially and into a fully erased block. |
| 27 | * |
| 28 | * Future additions to the format will split the region in half with an active |
| 29 | * block marker to allow safe compaction (ie. write the new data in the unused |
| 30 | * region, mark it active after the write completed). Otherwise a well-timed |
| 31 | * crash/reboot could clear out all variables. |
| 32 | */ |
| 33 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 34 | static enum cb_err lookup_store_region(struct region *region) |
| 35 | { |
| 36 | if (CONFIG(SMMSTORE_IN_CBFS)) { |
| 37 | struct cbfsf file; |
| 38 | if (cbfs_locate_file_in_region(&file, |
| 39 | CONFIG_SMMSTORE_REGION, |
| 40 | CONFIG_SMMSTORE_FILENAME, NULL) < 0) { |
| 41 | printk(BIOS_WARNING, |
| 42 | "smm store: Unable to find SMM store file in region '%s'\n", |
| 43 | CONFIG_SMMSTORE_REGION); |
| 44 | return CB_ERR; |
| 45 | } |
| 46 | struct region_device rdev; |
| 47 | cbfs_file_data(&rdev, &file); |
| 48 | *region = *region_device_region(&rdev); |
| 49 | } else { |
| 50 | if (fmap_locate_area(CONFIG_SMMSTORE_REGION, region)) { |
| 51 | printk(BIOS_WARNING, |
| 52 | "smm store: Unable to find SMM store FMAP region '%s'\n", |
| 53 | CONFIG_SMMSTORE_REGION); |
| 54 | return CB_ERR; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return CB_SUCCESS; |
| 59 | } |
| 60 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 61 | /* |
| 62 | * Return a region device that points into the store file. |
| 63 | * |
| 64 | * It's the image builder's responsibility to make it block aligned so that |
| 65 | * erase works without destroying other data. |
| 66 | * |
| 67 | * It doesn't cache the location to cope with flash changing underneath (eg |
| 68 | * due to an update) |
| 69 | * |
| 70 | * returns 0 on success, -1 on failure |
| 71 | * outputs the valid store rdev in rstore |
| 72 | */ |
| 73 | static int lookup_store(struct region_device *rstore) |
| 74 | { |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 75 | static struct region_device read_rdev, write_rdev; |
| 76 | static struct incoherent_rdev store_irdev; |
| 77 | struct region region; |
| 78 | const struct region_device *rdev; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 79 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 80 | if (lookup_store_region(®ion) != CB_SUCCESS) |
| 81 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 82 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 83 | if (boot_device_ro_subregion(®ion, &read_rdev) < 0) |
| 84 | return -1; |
| 85 | |
| 86 | if (boot_device_rw_subregion(®ion, &write_rdev) < 0) |
| 87 | return -1; |
| 88 | |
| 89 | rdev = incoherent_rdev_init(&store_irdev, ®ion, &read_rdev, &write_rdev); |
| 90 | |
| 91 | if (rdev == NULL) |
| 92 | return -1; |
| 93 | |
| 94 | return rdev_chain(rstore, rdev, 0, region_device_sz(rdev)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Read entire store into user provided buffer |
| 99 | * |
| 100 | * returns 0 on success, -1 on failure |
| 101 | * writes up to `*bufsize` bytes into `buf` and updates `*bufsize` |
| 102 | */ |
| 103 | int smmstore_read_region(void *buf, ssize_t *bufsize) |
| 104 | { |
| 105 | struct region_device store; |
| 106 | |
| 107 | if (bufsize == NULL) |
| 108 | return -1; |
| 109 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 110 | if (lookup_store(&store) < 0) { |
| 111 | printk(BIOS_WARNING, "reading region failed\n"); |
| 112 | return -1; |
| 113 | } |
| 114 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 115 | ssize_t tx = MIN(*bufsize, region_device_sz(&store)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 116 | *bufsize = rdev_readat(&store, buf, 0, tx); |
| 117 | |
| 118 | if (*bufsize < 0) |
| 119 | return -1; |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 124 | static enum cb_err scan_end(struct region_device *store) |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 125 | { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 126 | /* scan for end */ |
| 127 | ssize_t end = 0; |
| 128 | uint32_t k_sz, v_sz; |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 129 | const ssize_t data_sz = region_device_sz(store); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 130 | while (end < data_sz) { |
| 131 | /* make odd corner cases identifiable, eg. invalid v_sz */ |
| 132 | k_sz = 0; |
| 133 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 134 | if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 135 | printk(BIOS_WARNING, "failed reading key size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 136 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* found the end */ |
| 140 | if (k_sz == 0xffffffff) |
| 141 | break; |
| 142 | |
| 143 | /* something is fishy here: |
| 144 | * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while |
| 145 | * other problems are covered by the loop condition |
| 146 | */ |
| 147 | if (k_sz > data_sz) { |
| 148 | printk(BIOS_WARNING, "key size out of bounds\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 149 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 150 | } |
| 151 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 152 | 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] | 153 | printk(BIOS_WARNING, "failed reading value size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 154 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (v_sz > data_sz) { |
| 158 | printk(BIOS_WARNING, "value size out of bounds\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 159 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 160 | } |
| 161 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 162 | end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 163 | end = ALIGN_UP(end, sizeof(uint32_t)); |
| 164 | } |
| 165 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 166 | 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] | 167 | |
| 168 | if (k_sz != 0xffffffff) { |
| 169 | printk(BIOS_WARNING, |
| 170 | "eof of data marker looks invalid: 0x%x\n", k_sz); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 171 | return CB_ERR; |
| 172 | } |
| 173 | |
| 174 | if (rdev_chain(store, store, end, data_sz - end)) |
| 175 | return CB_ERR; |
| 176 | |
| 177 | return CB_SUCCESS; |
| 178 | |
| 179 | } |
| 180 | /* |
| 181 | * Append data to region |
| 182 | * |
| 183 | * Returns 0 on success, -1 on failure |
| 184 | */ |
| 185 | int smmstore_append_data(void *key, uint32_t key_sz, void *value, |
| 186 | uint32_t value_sz) |
| 187 | { |
| 188 | struct region_device store; |
| 189 | |
| 190 | if (lookup_store(&store) < 0) { |
| 191 | printk(BIOS_WARNING, "reading region failed\n"); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 192 | return -1; |
| 193 | } |
| 194 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 195 | ssize_t offset = 0; |
| 196 | ssize_t size; |
| 197 | uint8_t nul = 0; |
| 198 | if (scan_end(&store) != CB_SUCCESS) |
| 199 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 200 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 201 | printk(BIOS_DEBUG, "used size looks legit\n"); |
| 202 | |
| 203 | printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n", |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 204 | region_device_offset(&store), region_device_sz(&store)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 205 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 206 | size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz |
| 207 | + sizeof(nul); |
| 208 | if (rdev_chain(&store, &store, 0, size)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 209 | printk(BIOS_WARNING, "not enough space for new data\n"); |
| 210 | return -1; |
| 211 | } |
| 212 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 213 | if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz)) |
| 214 | != sizeof(key_sz)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 215 | printk(BIOS_WARNING, "failed writing key size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 216 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 217 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 218 | offset += sizeof(key_sz); |
| 219 | if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz)) |
| 220 | != sizeof(value_sz)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 221 | printk(BIOS_WARNING, "failed writing value size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 222 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 223 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 224 | offset += sizeof(value_sz); |
| 225 | if (rdev_writeat(&store, key, offset, key_sz) != key_sz) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 226 | printk(BIOS_WARNING, "failed writing key data\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 227 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 228 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 229 | offset += key_sz; |
| 230 | if (rdev_writeat(&store, value, offset, value_sz) != value_sz) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 231 | printk(BIOS_WARNING, "failed writing value data\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 232 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 233 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 234 | offset += value_sz; |
| 235 | if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 236 | printk(BIOS_WARNING, "failed writing termination\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 237 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | return 0; |
| 241 | } |
| 242 | |
| 243 | /* |
| 244 | * Clear region |
| 245 | * |
| 246 | * Returns 0 on success, -1 on failure, including partial erase |
| 247 | */ |
| 248 | int smmstore_clear_region(void) |
| 249 | { |
| 250 | struct region_device store; |
| 251 | |
| 252 | if (lookup_store(&store) < 0) { |
| 253 | printk(BIOS_WARNING, "smm store: reading region failed\n"); |
| 254 | return -1; |
| 255 | } |
| 256 | |
| 257 | ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store)); |
| 258 | if (res != region_device_sz(&store)) { |
| 259 | printk(BIOS_WARNING, "smm store: erasing region failed\n"); |
| 260 | return -1; |
| 261 | } |
| 262 | |
| 263 | return 0; |
| 264 | } |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 265 | |
| 266 | |
| 267 | /* Implementation of Version 2 */ |
| 268 | |
| 269 | static bool store_initialized; |
| 270 | static struct mem_region_device mdev_com_buf; |
| 271 | |
| 272 | static int smmstore_rdev_chain(struct region_device *rdev) |
| 273 | { |
| 274 | if (!store_initialized) |
| 275 | return -1; |
| 276 | |
| 277 | return rdev_chain_full(rdev, &mdev_com_buf.rdev); |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Call once before using the store. In SMM this must be called through an |
| 282 | * APM SMI handler providing the communication buffer address and length. |
| 283 | */ |
| 284 | int smmstore_init(void *buf, size_t len) |
| 285 | { |
| 286 | if (!buf || len < SMM_BLOCK_SIZE) |
| 287 | return -1; |
| 288 | |
| 289 | if (store_initialized) |
| 290 | return -1; |
| 291 | |
| 292 | mem_region_device_rw_init(&mdev_com_buf, buf, len); |
| 293 | |
| 294 | store_initialized = true; |
| 295 | |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | #if ENV_RAMSTAGE |
| 300 | /** |
| 301 | * Provide metadata for the coreboot tables. |
| 302 | * Must only be called in ramstage, but not in SMM. |
| 303 | */ |
| 304 | int smmstore_get_info(struct smmstore_params_info *out) |
| 305 | { |
| 306 | struct region_device store; |
| 307 | |
| 308 | if (lookup_store(&store) < 0) { |
| 309 | printk(BIOS_ERR, "smm store: lookup of store failed\n"); |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | if (!IS_ALIGNED(region_device_offset(&store), SMM_BLOCK_SIZE)) { |
| 314 | printk(BIOS_ERR, "smm store: store not aligned to block size\n"); |
| 315 | return -1; |
| 316 | } |
| 317 | |
| 318 | out->block_size = SMM_BLOCK_SIZE; |
| 319 | out->num_blocks = region_device_sz(&store) / SMM_BLOCK_SIZE; |
| 320 | |
| 321 | /* FIXME: Broken EDK2 always assumes memory mapped Firmware Block Volumes */ |
| 322 | out->mmap_addr = (uintptr_t)rdev_mmap_full(&store); |
| 323 | |
| 324 | printk(BIOS_DEBUG, "smm store: %d # blocks with size 0x%x\n", |
| 325 | out->num_blocks, out->block_size); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | #endif |
| 330 | |
| 331 | /* Returns -1 on error, 0 on success */ |
| 332 | static int lookup_block_in_store(struct region_device *store, uint32_t block_id) |
| 333 | { |
| 334 | if (lookup_store(store) < 0) { |
| 335 | printk(BIOS_ERR, "smm store: lookup of store failed\n"); |
| 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | if ((block_id * SMM_BLOCK_SIZE) >= region_device_sz(store)) { |
| 340 | printk(BIOS_ERR, "smm store: block ID out of range\n"); |
| 341 | return -1; |
| 342 | } |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | |
| 347 | /* Returns NULL on error, pointer from rdev_mmap on success */ |
| 348 | static void *mmap_com_buf(struct region_device *com_buf, uint32_t offset, uint32_t bufsize) |
| 349 | { |
| 350 | if (smmstore_rdev_chain(com_buf) < 0) { |
| 351 | printk(BIOS_ERR, "smm store: lookup of com buffer failed\n"); |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | if (offset >= region_device_sz(com_buf)) { |
| 356 | printk(BIOS_ERR, "smm store: offset out of range\n"); |
| 357 | return NULL; |
| 358 | } |
| 359 | |
| 360 | void *ptr = rdev_mmap(com_buf, offset, bufsize); |
| 361 | if (!ptr) |
| 362 | printk(BIOS_ERR, "smm store: not enough space for new data\n"); |
| 363 | |
| 364 | return ptr; |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Reads the specified block of the SMMSTORE and places it in the communication |
| 369 | * buffer. |
| 370 | * @param block_id The id of the block to operate on |
| 371 | * @param offset Offset within the block. |
| 372 | * Must be smaller than the block size. |
| 373 | * @param bufsize Size of chunk to read within the block. |
| 374 | * Must be smaller than the block size. |
| 375 | |
| 376 | * @return Returns -1 on error, 0 on success. |
| 377 | */ |
| 378 | int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize) |
| 379 | { |
| 380 | struct region_device store; |
| 381 | struct region_device com_buf; |
| 382 | |
| 383 | if (lookup_block_in_store(&store, block_id) < 0) |
| 384 | return -1; |
| 385 | |
| 386 | void *ptr = mmap_com_buf(&com_buf, offset, bufsize); |
| 387 | if (!ptr) |
| 388 | return -1; |
| 389 | |
| 390 | printk(BIOS_DEBUG, "smm store: reading %p block %d, offset=0x%x, size=%x\n", |
| 391 | ptr, block_id, offset, bufsize); |
| 392 | |
| 393 | ssize_t ret = rdev_readat(&store, ptr, block_id * SMM_BLOCK_SIZE + offset, bufsize); |
| 394 | rdev_munmap(&com_buf, ptr); |
| 395 | if (ret < 0) |
| 396 | return -1; |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Writes the specified block of the SMMSTORE by reading it from the communication |
| 403 | * buffer. |
| 404 | * @param block_id The id of the block to operate on |
| 405 | * @param offset Offset within the block. |
| 406 | * Must be smaller than the block size. |
| 407 | * @param bufsize Size of chunk to read within the block. |
| 408 | * Must be smaller than the block size. |
| 409 | |
| 410 | * @return Returns -1 on error, 0 on success. |
| 411 | */ |
| 412 | int smmstore_rawwrite_region(uint32_t block_id, uint32_t offset, uint32_t bufsize) |
| 413 | { |
| 414 | struct region_device store; |
| 415 | struct region_device com_buf; |
| 416 | |
| 417 | if (lookup_block_in_store(&store, block_id) < 0) |
| 418 | return -1; |
| 419 | |
| 420 | if (rdev_chain(&store, &store, block_id * SMM_BLOCK_SIZE + offset, bufsize)) { |
| 421 | printk(BIOS_ERR, "smm store: not enough space for new data\n"); |
| 422 | return -1; |
| 423 | } |
| 424 | |
| 425 | void *ptr = mmap_com_buf(&com_buf, offset, bufsize); |
| 426 | if (!ptr) |
| 427 | return -1; |
| 428 | |
| 429 | printk(BIOS_DEBUG, "smm store: writing %p block %d, offset=0x%x, size=%x\n", |
| 430 | ptr, block_id, offset, bufsize); |
| 431 | |
| 432 | ssize_t ret = rdev_writeat(&store, ptr, 0, bufsize); |
| 433 | rdev_munmap(&com_buf, ptr); |
| 434 | if (ret < 0) |
| 435 | return -1; |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Erases the specified block of the SMMSTORE. The communication buffer remains untouched. |
| 442 | * |
| 443 | * @param block_id The id of the block to operate on |
| 444 | * |
| 445 | * @return Returns -1 on error, 0 on success. |
| 446 | */ |
| 447 | int smmstore_rawclear_region(uint32_t block_id) |
| 448 | { |
| 449 | struct region_device store; |
| 450 | |
| 451 | if (lookup_block_in_store(&store, block_id) < 0) |
| 452 | return -1; |
| 453 | |
| 454 | ssize_t ret = rdev_eraseat(&store, block_id * SMM_BLOCK_SIZE, SMM_BLOCK_SIZE); |
| 455 | if (ret != SMM_BLOCK_SIZE) { |
| 456 | printk(BIOS_ERR, "smm store: erasing block failed\n"); |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | return 0; |
| 461 | } |