Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; version 2 of the License. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <boot_device.h> |
| 15 | #include <cbfs.h> |
Arthur Heymans | 8b04dc7 | 2018-12-01 15:28:03 +0100 | [diff] [blame] | 16 | #include <fmap.h> |
Elyes HAOUAS | 2195f7a | 2019-06-21 07:20:12 +0200 | [diff] [blame] | 17 | #include <commonlib/helpers.h> |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 18 | #include <commonlib/region.h> |
| 19 | #include <console/console.h> |
| 20 | #include <smmstore.h> |
Elyes HAOUAS | ede8dd0 | 2019-06-23 06:57:53 +0200 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <types.h> |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 23 | |
| 24 | /* |
| 25 | * The region format is still not finalized, but so far it looks like this: |
| 26 | * ( |
| 27 | * uint32le_t key_sz |
| 28 | * uint32le_t value_sz |
| 29 | * uint8_t key[key_sz] |
| 30 | * uint8_t value[value_sz] |
| 31 | * uint8_t active |
| 32 | * align to 4 bytes |
Elyes HAOUAS | a342f39 | 2018-10-17 10:56:26 +0200 | [diff] [blame] | 33 | * )* |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 34 | * uint32le_t endmarker = 0xffffffff |
| 35 | * |
| 36 | * active needs to be set to 0x00 for the entry to be valid. This satisfies |
| 37 | * the constraint that entries are either complete or will be ignored, as long |
| 38 | * as flash is written sequentially and into a fully erased block. |
| 39 | * |
| 40 | * Future additions to the format will split the region in half with an active |
| 41 | * block marker to allow safe compaction (ie. write the new data in the unused |
| 42 | * region, mark it active after the write completed). Otherwise a well-timed |
| 43 | * crash/reboot could clear out all variables. |
| 44 | */ |
| 45 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 46 | static enum cb_err lookup_store_region(struct region *region) |
| 47 | { |
| 48 | if (CONFIG(SMMSTORE_IN_CBFS)) { |
| 49 | struct cbfsf file; |
| 50 | if (cbfs_locate_file_in_region(&file, |
| 51 | CONFIG_SMMSTORE_REGION, |
| 52 | CONFIG_SMMSTORE_FILENAME, NULL) < 0) { |
| 53 | printk(BIOS_WARNING, |
| 54 | "smm store: Unable to find SMM store file in region '%s'\n", |
| 55 | CONFIG_SMMSTORE_REGION); |
| 56 | return CB_ERR; |
| 57 | } |
| 58 | struct region_device rdev; |
| 59 | cbfs_file_data(&rdev, &file); |
| 60 | *region = *region_device_region(&rdev); |
| 61 | } else { |
| 62 | if (fmap_locate_area(CONFIG_SMMSTORE_REGION, region)) { |
| 63 | printk(BIOS_WARNING, |
| 64 | "smm store: Unable to find SMM store FMAP region '%s'\n", |
| 65 | CONFIG_SMMSTORE_REGION); |
| 66 | return CB_ERR; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | return CB_SUCCESS; |
| 71 | } |
| 72 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 73 | /* |
| 74 | * Return a region device that points into the store file. |
| 75 | * |
| 76 | * It's the image builder's responsibility to make it block aligned so that |
| 77 | * erase works without destroying other data. |
| 78 | * |
| 79 | * It doesn't cache the location to cope with flash changing underneath (eg |
| 80 | * due to an update) |
| 81 | * |
| 82 | * returns 0 on success, -1 on failure |
| 83 | * outputs the valid store rdev in rstore |
| 84 | */ |
| 85 | static int lookup_store(struct region_device *rstore) |
| 86 | { |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 87 | static struct region_device read_rdev, write_rdev; |
| 88 | static struct incoherent_rdev store_irdev; |
| 89 | struct region region; |
| 90 | const struct region_device *rdev; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 91 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 92 | if (lookup_store_region(®ion) != CB_SUCCESS) |
| 93 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 94 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 95 | if (boot_device_ro_subregion(®ion, &read_rdev) < 0) |
| 96 | return -1; |
| 97 | |
| 98 | if (boot_device_rw_subregion(®ion, &write_rdev) < 0) |
| 99 | return -1; |
| 100 | |
| 101 | rdev = incoherent_rdev_init(&store_irdev, ®ion, &read_rdev, &write_rdev); |
| 102 | |
| 103 | if (rdev == NULL) |
| 104 | return -1; |
| 105 | |
| 106 | return rdev_chain(rstore, rdev, 0, region_device_sz(rdev)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Read entire store into user provided buffer |
| 111 | * |
| 112 | * returns 0 on success, -1 on failure |
| 113 | * writes up to `*bufsize` bytes into `buf` and updates `*bufsize` |
| 114 | */ |
| 115 | int smmstore_read_region(void *buf, ssize_t *bufsize) |
| 116 | { |
| 117 | struct region_device store; |
| 118 | |
| 119 | if (bufsize == NULL) |
| 120 | return -1; |
| 121 | |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 122 | if (lookup_store(&store) < 0) { |
| 123 | printk(BIOS_WARNING, "reading region failed\n"); |
| 124 | return -1; |
| 125 | } |
| 126 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 127 | ssize_t tx = MIN(*bufsize, region_device_sz(&store)); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 128 | *bufsize = rdev_readat(&store, buf, 0, tx); |
| 129 | |
| 130 | if (*bufsize < 0) |
| 131 | return -1; |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 136 | static enum cb_err scan_end(struct region_device *store) |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 137 | { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 138 | /* scan for end */ |
| 139 | ssize_t end = 0; |
| 140 | uint32_t k_sz, v_sz; |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 141 | const ssize_t data_sz = region_device_sz(store); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 142 | while (end < data_sz) { |
| 143 | /* make odd corner cases identifiable, eg. invalid v_sz */ |
| 144 | k_sz = 0; |
| 145 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 146 | if (rdev_readat(store, &k_sz, end, sizeof(k_sz)) < 0) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 147 | printk(BIOS_WARNING, "failed reading key 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 | /* found the end */ |
| 152 | if (k_sz == 0xffffffff) |
| 153 | break; |
| 154 | |
| 155 | /* something is fishy here: |
| 156 | * Avoid wrapping (since data_size < MAX_UINT32_T / 2) while |
| 157 | * other problems are covered by the loop condition |
| 158 | */ |
| 159 | if (k_sz > data_sz) { |
| 160 | printk(BIOS_WARNING, "key size out of bounds\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 161 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 162 | } |
| 163 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 164 | 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] | 165 | printk(BIOS_WARNING, "failed reading value size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 166 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | if (v_sz > data_sz) { |
| 170 | printk(BIOS_WARNING, "value size out of bounds\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 171 | return CB_ERR; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 172 | } |
| 173 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 174 | end += sizeof(k_sz) + sizeof(v_sz) + k_sz + v_sz + 1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 175 | end = ALIGN_UP(end, sizeof(uint32_t)); |
| 176 | } |
| 177 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 178 | 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] | 179 | |
| 180 | if (k_sz != 0xffffffff) { |
| 181 | printk(BIOS_WARNING, |
| 182 | "eof of data marker looks invalid: 0x%x\n", k_sz); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 183 | return CB_ERR; |
| 184 | } |
| 185 | |
| 186 | if (rdev_chain(store, store, end, data_sz - end)) |
| 187 | return CB_ERR; |
| 188 | |
| 189 | return CB_SUCCESS; |
| 190 | |
| 191 | } |
| 192 | /* |
| 193 | * Append data to region |
| 194 | * |
| 195 | * Returns 0 on success, -1 on failure |
| 196 | */ |
| 197 | int smmstore_append_data(void *key, uint32_t key_sz, void *value, |
| 198 | uint32_t value_sz) |
| 199 | { |
| 200 | struct region_device store; |
| 201 | |
| 202 | if (lookup_store(&store) < 0) { |
| 203 | printk(BIOS_WARNING, "reading region failed\n"); |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 204 | return -1; |
| 205 | } |
| 206 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 207 | ssize_t offset = 0; |
| 208 | ssize_t size; |
| 209 | uint8_t nul = 0; |
| 210 | if (scan_end(&store) != CB_SUCCESS) |
| 211 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 212 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 213 | printk(BIOS_DEBUG, "used size looks legit\n"); |
| 214 | |
| 215 | printk(BIOS_DEBUG, "open (%zx, %zx) for writing\n", |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 216 | region_device_offset(&store), region_device_sz(&store)); |
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 | size = sizeof(key_sz) + sizeof(value_sz) + key_sz + value_sz |
| 219 | + sizeof(nul); |
| 220 | if (rdev_chain(&store, &store, 0, size)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 221 | printk(BIOS_WARNING, "not enough space for new data\n"); |
| 222 | return -1; |
| 223 | } |
| 224 | |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 225 | if (rdev_writeat(&store, &key_sz, offset, sizeof(key_sz)) |
| 226 | != sizeof(key_sz)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 227 | printk(BIOS_WARNING, "failed writing key size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 228 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 229 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 230 | offset += sizeof(key_sz); |
| 231 | if (rdev_writeat(&store, &value_sz, offset, sizeof(value_sz)) |
| 232 | != sizeof(value_sz)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 233 | printk(BIOS_WARNING, "failed writing value size\n"); |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 234 | return -1; |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 235 | } |
Arthur Heymans | ed50d85 | 2018-12-26 11:20:59 +0100 | [diff] [blame^] | 236 | offset += sizeof(value_sz); |
| 237 | if (rdev_writeat(&store, key, offset, key_sz) != key_sz) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 238 | printk(BIOS_WARNING, "failed writing key data\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 += key_sz; |
| 242 | if (rdev_writeat(&store, value, offset, value_sz) != value_sz) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 243 | printk(BIOS_WARNING, "failed writing value 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 += value_sz; |
| 247 | if (rdev_writeat(&store, &nul, offset, sizeof(nul)) != sizeof(nul)) { |
Patrick Georgi | 9360fea | 2018-03-14 21:11:21 +0100 | [diff] [blame] | 248 | printk(BIOS_WARNING, "failed writing termination\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 | } |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Clear region |
| 257 | * |
| 258 | * Returns 0 on success, -1 on failure, including partial erase |
| 259 | */ |
| 260 | int smmstore_clear_region(void) |
| 261 | { |
| 262 | struct region_device store; |
| 263 | |
| 264 | if (lookup_store(&store) < 0) { |
| 265 | printk(BIOS_WARNING, "smm store: reading region failed\n"); |
| 266 | return -1; |
| 267 | } |
| 268 | |
| 269 | ssize_t res = rdev_eraseat(&store, 0, region_device_sz(&store)); |
| 270 | if (res != region_device_sz(&store)) { |
| 271 | printk(BIOS_WARNING, "smm store: erasing region failed\n"); |
| 272 | return -1; |
| 273 | } |
| 274 | |
| 275 | return 0; |
| 276 | } |