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> |
Arthur Heymans | 1ba6049 | 2022-02-02 20:09:27 +0100 | [diff] [blame] | 5 | #include <fmap_config.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 | |
Julius Werner | 6eeba88 | 2021-08-10 16:38:43 -0700 | [diff] [blame] | 12 | #define SMMSTORE_REGION "SMMSTORE" |
| 13 | |
Arthur Heymans | 1ba6049 | 2022-02-02 20:09:27 +0100 | [diff] [blame] | 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 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 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 |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 30 | * )* |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 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 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 43 | static enum cb_err lookup_store_region(struct region *region) |
| 44 | { |
Julius Werner | 6eeba88 | 2021-08-10 16:38:43 -0700 | [diff] [blame] | 45 | if (fmap_locate_area(SMMSTORE_REGION, region)) { |
| 46 | printk(BIOS_WARNING, |
| 47 | "smm store: Unable to find SMM store FMAP region '%s'\n", |
| 48 | SMMSTORE_REGION); |
| 49 | return CB_ERR; |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return CB_SUCCESS; |
| 53 | } |
| 54 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 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 | { |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 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; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 73 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 74 | if (lookup_store_region(®ion) != CB_SUCCESS) |
| 75 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 76 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 77 | if (boot_device_ro_subregion(®ion, &read_rdev) < 0) |
| 78 | return -1; |
| 79 | |
| 80 | if (boot_device_rw_subregion(®ion, &write_rdev) < 0) |
| 81 | return -1; |
| 82 | |
| 83 | rdev = incoherent_rdev_init(&store_irdev, ®ion, &read_rdev, &write_rdev); |
| 84 | |
| 85 | if (rdev == NULL) |
| 86 | return -1; |
| 87 | |
| 88 | return rdev_chain(rstore, rdev, 0, region_device_sz(rdev)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 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 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 104 | if (lookup_store(&store) < 0) { |
| 105 | printk(BIOS_WARNING, "reading region failed\n"); |
| 106 | return -1; |
| 107 | } |
| 108 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 109 | ssize_t tx = MIN(*bufsize, region_device_sz(&store)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 110 | *bufsize = rdev_readat(&store, buf, 0, tx); |
| 111 | |
| 112 | if (*bufsize < 0) |
| 113 | return -1; |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 118 | static enum cb_err scan_end(struct region_device *store) |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 119 | { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 120 | /* scan for end */ |
| 121 | ssize_t end = 0; |
| 122 | uint32_t k_sz, v_sz; |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 123 | const ssize_t data_sz = region_device_sz(store); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 124 | while (end < data_sz) { |
| 125 | /* make odd corner cases identifiable, eg. invalid v_sz */ |
| 126 | k_sz = 0; |
| 127 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 128 | if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 129 | printk(BIOS_WARNING, "failed reading key size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 130 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 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"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 143 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 144 | } |
| 145 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 146 | 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] | 147 | printk(BIOS_WARNING, "failed reading value size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 148 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (v_sz > data_sz) { |
| 152 | printk(BIOS_WARNING, "value size out of bounds\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 153 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 156 | end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 157 | end = ALIGN_UP(end, sizeof(uint32_t)); |
| 158 | } |
| 159 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 160 | 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] | 161 | |
| 162 | if (k_sz != 0xffffffff) { |
| 163 | printk(BIOS_WARNING, |
| 164 | "eof of data marker looks invalid: 0x%x\n", k_sz); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 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; |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 172 | } |
| 173 | /* |
| 174 | * Append data to region |
| 175 | * |
| 176 | * Returns 0 on success, -1 on failure |
| 177 | */ |
| 178 | int smmstore_append_data(void *key, uint32_t key_sz, void *value, |
| 179 | uint32_t value_sz) |
| 180 | { |
| 181 | struct region_device store; |
| 182 | |
| 183 | if (lookup_store(&store) < 0) { |
| 184 | printk(BIOS_WARNING, "reading region failed\n"); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 188 | ssize_t offset = 0; |
| 189 | ssize_t size; |
| 190 | uint8_t nul = 0; |
| 191 | if (scan_end(&store) != CB_SUCCESS) |
| 192 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 193 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 194 | printk(BIOS_DEBUG, "used size looks legit\n"); |
| 195 | |
| 196 | printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n", |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 197 | region_device_offset(&store), region_device_sz(&store)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 198 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 199 | size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz |
| 200 | + sizeof(nul); |
| 201 | if (rdev_chain(&store, &store, 0, size)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 202 | printk(BIOS_WARNING, "not enough space for new data\n"); |
| 203 | return -1; |
| 204 | } |
| 205 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 206 | if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz)) |
| 207 | != sizeof(key_sz)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 208 | printk(BIOS_WARNING, "failed writing key size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 209 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 210 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 211 | offset += sizeof(key_sz); |
| 212 | if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz)) |
| 213 | != sizeof(value_sz)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 214 | printk(BIOS_WARNING, "failed writing value size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 215 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 216 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 217 | offset += sizeof(value_sz); |
| 218 | if (rdev_writeat(&store, key, offset, key_sz) != key_sz) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 219 | printk(BIOS_WARNING, "failed writing key data\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 220 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 221 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 222 | offset += key_sz; |
| 223 | if (rdev_writeat(&store, value, offset, value_sz) != value_sz) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 224 | printk(BIOS_WARNING, "failed writing value data\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 225 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 226 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 227 | offset += value_sz; |
| 228 | if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 229 | printk(BIOS_WARNING, "failed writing termination\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 230 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | /* |
| 237 | * Clear region |
| 238 | * |
| 239 | * Returns 0 on success, -1 on failure, including partial erase |
| 240 | */ |
| 241 | int smmstore_clear_region(void) |
| 242 | { |
| 243 | struct region_device store; |
| 244 | |
| 245 | if (lookup_store(&store) < 0) { |
| 246 | printk(BIOS_WARNING, "smm store: reading region failed\n"); |
| 247 | return -1; |
| 248 | } |
| 249 | |
| 250 | ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store)); |
| 251 | if (res != region_device_sz(&store)) { |
| 252 | printk(BIOS_WARNING, "smm store: erasing region failed\n"); |
| 253 | return -1; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 258 | |
| 259 | |
| 260 | /* Implementation of Version 2 */ |
| 261 | |
| 262 | static bool store_initialized; |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 263 | static struct region_device mdev_com_buf; |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 264 | |
| 265 | static int smmstore_rdev_chain(struct region_device *rdev) |
| 266 | { |
| 267 | if (!store_initialized) |
| 268 | return -1; |
| 269 | |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 270 | return rdev_chain_full(rdev, &mdev_com_buf); |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Call once before using the store. In SMM this must be called through an |
| 275 | * APM SMI handler providing the communication buffer address and length. |
| 276 | */ |
| 277 | int smmstore_init(void *buf, size_t len) |
| 278 | { |
| 279 | if (!buf || len < SMM_BLOCK_SIZE) |
| 280 | return -1; |
| 281 | |
| 282 | if (store_initialized) |
| 283 | return -1; |
| 284 | |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 285 | rdev_chain_mem_rw(&mdev_com_buf, buf, len); |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 286 | |
| 287 | store_initialized = true; |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | |
| 292 | #if ENV_RAMSTAGE |
| 293 | /** |
| 294 | * Provide metadata for the coreboot tables. |
| 295 | * Must only be called in ramstage, but not in SMM. |
| 296 | */ |
| 297 | int smmstore_get_info(struct smmstore_params_info *out) |
| 298 | { |
| 299 | struct region_device store; |
| 300 | |
| 301 | if (lookup_store(&store) < 0) { |
| 302 | printk(BIOS_ERR, "smm store: lookup of store failed\n"); |
| 303 | return -1; |
| 304 | } |
| 305 | |
| 306 | if (!IS_ALIGNED(region_device_offset(&store), SMM_BLOCK_SIZE)) { |
| 307 | printk(BIOS_ERR, "smm store: store not aligned to block size\n"); |
| 308 | return -1; |
| 309 | } |
| 310 | |
| 311 | out->block_size = SMM_BLOCK_SIZE; |
| 312 | out->num_blocks = region_device_sz(&store) / SMM_BLOCK_SIZE; |
| 313 | |
| 314 | /* FIXME: Broken EDK2 always assumes memory mapped Firmware Block Volumes */ |
| 315 | out->mmap_addr = (uintptr_t)rdev_mmap_full(&store); |
| 316 | |
| 317 | printk(BIOS_DEBUG, "smm store: %d # blocks with size 0x%x\n", |
| 318 | out->num_blocks, out->block_size); |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | /* Returns -1 on error, 0 on success */ |
| 325 | static int lookup_block_in_store(struct region_device *store, uint32_t block_id) |
| 326 | { |
| 327 | if (lookup_store(store) < 0) { |
| 328 | printk(BIOS_ERR, "smm store: lookup of store failed\n"); |
| 329 | return -1; |
| 330 | } |
| 331 | |
| 332 | if ((block_id * SMM_BLOCK_SIZE) >= region_device_sz(store)) { |
| 333 | printk(BIOS_ERR, "smm store: block ID out of range\n"); |
| 334 | return -1; |
| 335 | } |
| 336 | |
| 337 | return 0; |
| 338 | } |
| 339 | |
| 340 | /* Returns NULL on error, pointer from rdev_mmap on success */ |
| 341 | static void *mmap_com_buf(struct region_device *com_buf, uint32_t offset, uint32_t bufsize) |
| 342 | { |
| 343 | if (smmstore_rdev_chain(com_buf) < 0) { |
| 344 | printk(BIOS_ERR, "smm store: lookup of com buffer failed\n"); |
| 345 | return NULL; |
| 346 | } |
| 347 | |
| 348 | if (offset >= region_device_sz(com_buf)) { |
| 349 | printk(BIOS_ERR, "smm store: offset out of range\n"); |
| 350 | return NULL; |
| 351 | } |
| 352 | |
| 353 | void *ptr = rdev_mmap(com_buf, offset, bufsize); |
| 354 | if (!ptr) |
| 355 | printk(BIOS_ERR, "smm store: not enough space for new data\n"); |
| 356 | |
| 357 | return ptr; |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * Reads the specified block of the SMMSTORE and places it in the communication |
| 362 | * buffer. |
| 363 | * @param block_id The id of the block to operate on |
| 364 | * @param offset Offset within the block. |
| 365 | * Must be smaller than the block size. |
| 366 | * @param bufsize Size of chunk to read within the block. |
| 367 | * Must be smaller than the block size. |
| 368 | |
| 369 | * @return Returns -1 on error, 0 on success. |
| 370 | */ |
| 371 | int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize) |
| 372 | { |
| 373 | struct region_device store; |
| 374 | struct region_device com_buf; |
| 375 | |
| 376 | if (lookup_block_in_store(&store, block_id) < 0) |
| 377 | return -1; |
| 378 | |
| 379 | void *ptr = mmap_com_buf(&com_buf, offset, bufsize); |
| 380 | if (!ptr) |
| 381 | return -1; |
| 382 | |
| 383 | printk(BIOS_DEBUG, "smm store: reading %p block %d, offset=0x%x, size=%x\n", |
| 384 | ptr, block_id, offset, bufsize); |
| 385 | |
| 386 | ssize_t ret = rdev_readat(&store, ptr, block_id * SMM_BLOCK_SIZE + offset, bufsize); |
| 387 | rdev_munmap(&com_buf, ptr); |
| 388 | if (ret < 0) |
| 389 | return -1; |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Writes the specified block of the SMMSTORE by reading it from the communication |
| 396 | * buffer. |
| 397 | * @param block_id The id of the block to operate on |
| 398 | * @param offset Offset within the block. |
| 399 | * Must be smaller than the block size. |
| 400 | * @param bufsize Size of chunk to read within the block. |
| 401 | * Must be smaller than the block size. |
| 402 | |
| 403 | * @return Returns -1 on error, 0 on success. |
| 404 | */ |
| 405 | int smmstore_rawwrite_region(uint32_t block_id, uint32_t offset, uint32_t bufsize) |
| 406 | { |
| 407 | struct region_device store; |
| 408 | struct region_device com_buf; |
| 409 | |
| 410 | if (lookup_block_in_store(&store, block_id) < 0) |
| 411 | return -1; |
| 412 | |
| 413 | if (rdev_chain(&store, &store, block_id * SMM_BLOCK_SIZE + offset, bufsize)) { |
| 414 | printk(BIOS_ERR, "smm store: not enough space for new data\n"); |
| 415 | return -1; |
| 416 | } |
| 417 | |
| 418 | void *ptr = mmap_com_buf(&com_buf, offset, bufsize); |
| 419 | if (!ptr) |
| 420 | return -1; |
| 421 | |
| 422 | printk(BIOS_DEBUG, "smm store: writing %p block %d, offset=0x%x, size=%x\n", |
| 423 | ptr, block_id, offset, bufsize); |
| 424 | |
| 425 | ssize_t ret = rdev_writeat(&store, ptr, 0, bufsize); |
| 426 | rdev_munmap(&com_buf, ptr); |
| 427 | if (ret < 0) |
| 428 | return -1; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * Erases the specified block of the SMMSTORE. The communication buffer remains untouched. |
| 435 | * |
| 436 | * @param block_id The id of the block to operate on |
| 437 | * |
| 438 | * @return Returns -1 on error, 0 on success. |
| 439 | */ |
| 440 | int smmstore_rawclear_region(uint32_t block_id) |
| 441 | { |
| 442 | struct region_device store; |
| 443 | |
| 444 | if (lookup_block_in_store(&store, block_id) < 0) |
| 445 | return -1; |
| 446 | |
| 447 | ssize_t ret = rdev_eraseat(&store, block_id * SMM_BLOCK_SIZE, SMM_BLOCK_SIZE); |
| 448 | if (ret != SMM_BLOCK_SIZE) { |
| 449 | printk(BIOS_ERR, "smm store: erasing block failed\n"); |
| 450 | return -1; |
| 451 | } |
| 452 | |
| 453 | return 0; |
| 454 | } |