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 | |
Patrick Rudolph | 4e00f15 | 2022-02-16 07:51:36 +0100 | [diff] [blame] | 91 | /* this function is non reentrant */ |
| 92 | int smmstore_lookup_region(struct region_device *rstore) |
| 93 | { |
| 94 | static int done; |
| 95 | static int ret; |
| 96 | static struct region_device rdev; |
| 97 | |
| 98 | if (!done) { |
| 99 | |
| 100 | done = 1; |
| 101 | |
| 102 | if (fmap_locate_area_as_rdev_rw(SMMSTORE_REGION, &rdev)) { |
| 103 | printk(BIOS_WARNING, |
| 104 | "smm store: Unable to find SMM store FMAP region '%s'\n", |
| 105 | SMMSTORE_REGION); |
| 106 | ret = -1; |
| 107 | } else { |
| 108 | ret = 0; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | *rstore = rdev; |
| 113 | return ret; |
| 114 | } |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 115 | /* |
| 116 | * Read entire store into user provided buffer |
| 117 | * |
| 118 | * returns 0 on success, -1 on failure |
| 119 | * writes up to `*bufsize` bytes into `buf` and updates `*bufsize` |
| 120 | */ |
| 121 | int smmstore_read_region(void *buf, ssize_t *bufsize) |
| 122 | { |
| 123 | struct region_device store; |
| 124 | |
| 125 | if (bufsize == NULL) |
| 126 | return -1; |
| 127 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 128 | if (lookup_store(&store) < 0) { |
| 129 | printk(BIOS_WARNING, "reading region failed\n"); |
| 130 | return -1; |
| 131 | } |
| 132 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 133 | ssize_t tx = MIN(*bufsize, region_device_sz(&store)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 134 | *bufsize = rdev_readat(&store, buf, 0, tx); |
| 135 | |
| 136 | if (*bufsize < 0) |
| 137 | return -1; |
| 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 142 | static enum cb_err scan_end(struct region_device *store) |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 143 | { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 144 | /* scan for end */ |
| 145 | ssize_t end = 0; |
| 146 | uint32_t k_sz, v_sz; |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 147 | const ssize_t data_sz = region_device_sz(store); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 148 | while (end < data_sz) { |
| 149 | /* make odd corner cases identifiable, eg. invalid v_sz */ |
| 150 | k_sz = 0; |
| 151 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 152 | if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 153 | printk(BIOS_WARNING, "failed reading key 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 | /* found the end */ |
| 158 | if (k_sz == 0xffffffff) |
| 159 | break; |
| 160 | |
| 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 |
| 164 | */ |
| 165 | if (k_sz > data_sz) { |
| 166 | printk(BIOS_WARNING, "key size out of bounds\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 167 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 168 | } |
| 169 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 170 | 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] | 171 | printk(BIOS_WARNING, "failed reading value size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 172 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | if (v_sz > data_sz) { |
| 176 | printk(BIOS_WARNING, "value size out of bounds\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 177 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 180 | end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 181 | end = ALIGN_UP(end, sizeof(uint32_t)); |
| 182 | } |
| 183 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 184 | 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] | 185 | |
| 186 | if (k_sz != 0xffffffff) { |
| 187 | printk(BIOS_WARNING, |
Martin Roth | 3e25f85 | 2023-09-04 15:37:07 -0600 | [diff] [blame] | 188 | "EOF of data marker looks invalid: 0x%x\n", k_sz); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 189 | return CB_ERR; |
| 190 | } |
| 191 | |
| 192 | if (rdev_chain(store, store, end, data_sz - end)) |
| 193 | return CB_ERR; |
| 194 | |
| 195 | return CB_SUCCESS; |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 196 | } |
| 197 | /* |
| 198 | * Append data to region |
| 199 | * |
| 200 | * Returns 0 on success, -1 on failure |
| 201 | */ |
| 202 | int smmstore_append_data(void *key, uint32_t key_sz, void *value, |
| 203 | uint32_t value_sz) |
| 204 | { |
| 205 | struct region_device store; |
| 206 | |
| 207 | if (lookup_store(&store) < 0) { |
| 208 | printk(BIOS_WARNING, "reading region failed\n"); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 209 | return -1; |
| 210 | } |
| 211 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 212 | ssize_t offset = 0; |
| 213 | ssize_t size; |
| 214 | uint8_t nul = 0; |
| 215 | if (scan_end(&store) != CB_SUCCESS) |
| 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 | printk(BIOS_DEBUG, "used size looks legit\n"); |
| 219 | |
| 220 | printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n", |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 221 | region_device_offset(&store), region_device_sz(&store)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 222 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 223 | size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz |
| 224 | + sizeof(nul); |
| 225 | if (rdev_chain(&store, &store, 0, size)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 226 | printk(BIOS_WARNING, "not enough space for new data\n"); |
| 227 | return -1; |
| 228 | } |
| 229 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 230 | if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz)) |
| 231 | != sizeof(key_sz)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 232 | printk(BIOS_WARNING, "failed writing key size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 233 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 234 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 235 | offset += sizeof(key_sz); |
| 236 | if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz)) |
| 237 | != sizeof(value_sz)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 238 | printk(BIOS_WARNING, "failed writing value size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 239 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 240 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 241 | offset += sizeof(value_sz); |
| 242 | if (rdev_writeat(&store, key, offset, key_sz) != key_sz) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 243 | printk(BIOS_WARNING, "failed writing key data\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 244 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 245 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 246 | offset += key_sz; |
| 247 | if (rdev_writeat(&store, value, offset, value_sz) != value_sz) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 248 | printk(BIOS_WARNING, "failed writing value data\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 249 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 250 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 251 | offset += value_sz; |
| 252 | if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 253 | printk(BIOS_WARNING, "failed writing termination\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame] | 254 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Clear region |
| 262 | * |
| 263 | * Returns 0 on success, -1 on failure, including partial erase |
| 264 | */ |
| 265 | int smmstore_clear_region(void) |
| 266 | { |
| 267 | struct region_device store; |
| 268 | |
| 269 | if (lookup_store(&store) < 0) { |
| 270 | printk(BIOS_WARNING, "smm store: reading region failed\n"); |
| 271 | return -1; |
| 272 | } |
| 273 | |
| 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"); |
| 277 | return -1; |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 282 | |
| 283 | |
| 284 | /* Implementation of Version 2 */ |
| 285 | |
| 286 | static bool store_initialized; |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 287 | static struct region_device mdev_com_buf; |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 288 | |
| 289 | static int smmstore_rdev_chain(struct region_device *rdev) |
| 290 | { |
| 291 | if (!store_initialized) |
| 292 | return -1; |
| 293 | |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 294 | return rdev_chain_full(rdev, &mdev_com_buf); |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | /** |
| 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. |
| 300 | */ |
| 301 | int smmstore_init(void *buf, size_t len) |
| 302 | { |
| 303 | if (!buf || len < SMM_BLOCK_SIZE) |
| 304 | return -1; |
| 305 | |
| 306 | if (store_initialized) |
| 307 | return -1; |
| 308 | |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 309 | rdev_chain_mem_rw(&mdev_com_buf, buf, len); |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 310 | |
| 311 | store_initialized = true; |
| 312 | |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | #if ENV_RAMSTAGE |
| 317 | /** |
| 318 | * Provide metadata for the coreboot tables. |
| 319 | * Must only be called in ramstage, but not in SMM. |
| 320 | */ |
| 321 | int smmstore_get_info(struct smmstore_params_info *out) |
| 322 | { |
| 323 | struct region_device store; |
| 324 | |
| 325 | if (lookup_store(&store) < 0) { |
| 326 | printk(BIOS_ERR, "smm store: lookup of store failed\n"); |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | if (!IS_ALIGNED(region_device_offset(&store), SMM_BLOCK_SIZE)) { |
| 331 | printk(BIOS_ERR, "smm store: store not aligned to block size\n"); |
| 332 | return -1; |
| 333 | } |
| 334 | |
| 335 | out->block_size = SMM_BLOCK_SIZE; |
| 336 | out->num_blocks = region_device_sz(&store) / SMM_BLOCK_SIZE; |
| 337 | |
Sean Rhodes | 38c99b5 | 2022-07-13 10:11:44 +0100 | [diff] [blame] | 338 | /* FIXME: Broken edk2 always assumes memory mapped Firmware Block Volumes */ |
Patrick Rudolph | bc744f5 | 2020-04-17 16:16:49 +0200 | [diff] [blame] | 339 | out->mmap_addr = (uintptr_t)rdev_mmap_full(&store); |
| 340 | |
| 341 | printk(BIOS_DEBUG, "smm store: %d # blocks with size 0x%x\n", |
| 342 | out->num_blocks, out->block_size); |
| 343 | |
| 344 | return 0; |
| 345 | } |
| 346 | #endif |
| 347 | |
| 348 | /* Returns -1 on error, 0 on success */ |
| 349 | static int lookup_block_in_store(struct region_device *store, uint32_t block_id) |
| 350 | { |
| 351 | if (lookup_store(store) < 0) { |
| 352 | printk(BIOS_ERR, "smm store: lookup of store failed\n"); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | if ((block_id * SMM_BLOCK_SIZE) >= region_device_sz(store)) { |
| 357 | printk(BIOS_ERR, "smm store: block ID out of range\n"); |
| 358 | return -1; |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 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) |
| 366 | { |
| 367 | if (smmstore_rdev_chain(com_buf) < 0) { |
| 368 | printk(BIOS_ERR, "smm store: lookup of com buffer failed\n"); |
| 369 | return NULL; |
| 370 | } |
| 371 | |
| 372 | if (offset >= region_device_sz(com_buf)) { |
| 373 | printk(BIOS_ERR, "smm store: offset out of range\n"); |
| 374 | return NULL; |
| 375 | } |
| 376 | |
| 377 | void *ptr = rdev_mmap(com_buf, offset, bufsize); |
| 378 | if (!ptr) |
| 379 | printk(BIOS_ERR, "smm store: not enough space for new data\n"); |
| 380 | |
| 381 | return ptr; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Reads the specified block of the SMMSTORE and places it in the communication |
| 386 | * buffer. |
| 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. |
| 392 | |
| 393 | * @return Returns -1 on error, 0 on success. |
| 394 | */ |
| 395 | int smmstore_rawread_region(uint32_t block_id, uint32_t offset, uint32_t bufsize) |
| 396 | { |
| 397 | struct region_device store; |
| 398 | struct region_device com_buf; |
| 399 | |
| 400 | if (lookup_block_in_store(&store, block_id) < 0) |
| 401 | return -1; |
| 402 | |
| 403 | void *ptr = mmap_com_buf(&com_buf, offset, bufsize); |
| 404 | if (!ptr) |
| 405 | return -1; |
| 406 | |
| 407 | printk(BIOS_DEBUG, "smm store: reading %p block %d, offset=0x%x, size=%x\n", |
| 408 | ptr, block_id, offset, bufsize); |
| 409 | |
| 410 | ssize_t ret = rdev_readat(&store, ptr, block_id * SMM_BLOCK_SIZE + offset, bufsize); |
| 411 | rdev_munmap(&com_buf, ptr); |
| 412 | if (ret < 0) |
| 413 | return -1; |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * Writes the specified block of the SMMSTORE by reading it from the communication |
| 420 | * buffer. |
| 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. |
| 426 | |
| 427 | * @return Returns -1 on error, 0 on success. |
| 428 | */ |
| 429 | int smmstore_rawwrite_region(uint32_t block_id, uint32_t offset, uint32_t bufsize) |
| 430 | { |
| 431 | struct region_device store; |
| 432 | struct region_device com_buf; |
| 433 | |
| 434 | if (lookup_block_in_store(&store, block_id) < 0) |
| 435 | return -1; |
| 436 | |
| 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"); |
| 439 | return -1; |
| 440 | } |
| 441 | |
| 442 | void *ptr = mmap_com_buf(&com_buf, offset, bufsize); |
| 443 | if (!ptr) |
| 444 | return -1; |
| 445 | |
| 446 | printk(BIOS_DEBUG, "smm store: writing %p block %d, offset=0x%x, size=%x\n", |
| 447 | ptr, block_id, offset, bufsize); |
| 448 | |
| 449 | ssize_t ret = rdev_writeat(&store, ptr, 0, bufsize); |
| 450 | rdev_munmap(&com_buf, ptr); |
| 451 | if (ret < 0) |
| 452 | return -1; |
| 453 | |
| 454 | return 0; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * Erases the specified block of the SMMSTORE. The communication buffer remains untouched. |
| 459 | * |
| 460 | * @param block_id The id of the block to operate on |
| 461 | * |
| 462 | * @return Returns -1 on error, 0 on success. |
| 463 | */ |
| 464 | int smmstore_rawclear_region(uint32_t block_id) |
| 465 | { |
| 466 | struct region_device store; |
| 467 | |
| 468 | if (lookup_block_in_store(&store, block_id) < 0) |
| 469 | return -1; |
| 470 | |
| 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"); |
| 474 | return -1; |
| 475 | } |
| 476 | |
| 477 | return 0; |
| 478 | } |