Angel Pons | 118a9c7 | 2020-04-02 23:48:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 2 | |
| 3 | #include <boot_device.h> |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 4 | #include <cbmem.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <fmap.h> |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 7 | #include <metadata_hash.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | #include <string.h> |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 10 | #include <symbols.h> |
Krystian Hebel | 93f6b8a | 2020-09-30 18:23:14 +0200 | [diff] [blame] | 11 | #include <endian.h> |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 12 | |
Aaron Durbin | bf1e481 | 2016-05-10 15:12:08 -0500 | [diff] [blame] | 13 | #include "fmap_config.h" |
| 14 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 15 | /* |
| 16 | * See http://code.google.com/p/flashmap/ for more information on FMAP. |
| 17 | */ |
| 18 | |
Arthur Heymans | dba22d2 | 2019-11-20 19:57:49 +0100 | [diff] [blame] | 19 | static int fmap_print_once; |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 20 | static struct region_device fmap_cache; |
Duncan Laurie | bc2c0a3 | 2016-02-09 09:17:56 -0800 | [diff] [blame] | 21 | |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 22 | #define print_once(...) do { \ |
Arthur Heymans | dba22d2 | 2019-11-20 19:57:49 +0100 | [diff] [blame] | 23 | if (!fmap_print_once) \ |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 24 | printk(__VA_ARGS__); \ |
| 25 | } while (0) |
| 26 | |
Furquan Shaikh | b33a2b0 | 2019-09-26 23:51:46 -0700 | [diff] [blame] | 27 | uint64_t get_fmap_flash_offset(void) |
| 28 | { |
| 29 | return FMAP_OFFSET; |
| 30 | } |
| 31 | |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 32 | static int verify_fmap(const struct fmap *fmap) |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 33 | { |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 34 | if (memcmp(fmap->signature, FMAP_SIGNATURE, sizeof(fmap->signature))) |
| 35 | return -1; |
| 36 | |
| 37 | static bool done = false; |
| 38 | if (!CONFIG(CBFS_VERIFICATION) || !ENV_INITIAL_STAGE || done) |
| 39 | return 0; /* Only need to check hash in first stage. */ |
| 40 | |
| 41 | if (metadata_hash_verify_fmap(fmap, FMAP_SIZE) != VB2_SUCCESS) |
| 42 | return -1; |
| 43 | |
| 44 | done = true; |
| 45 | return 0; |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static void report(const struct fmap *fmap) |
| 49 | { |
| 50 | print_once(BIOS_DEBUG, "FMAP: Found \"%s\" version %d.%d at %#x.\n", |
| 51 | fmap->name, fmap->ver_major, fmap->ver_minor, FMAP_OFFSET); |
| 52 | print_once(BIOS_DEBUG, "FMAP: base = %#llx size = %#x #areas = %d\n", |
Krystian Hebel | 93f6b8a | 2020-09-30 18:23:14 +0200 | [diff] [blame] | 53 | (long long)le64toh(fmap->base), le32toh(fmap->size), |
| 54 | le16toh(fmap->nareas)); |
Arthur Heymans | dba22d2 | 2019-11-20 19:57:49 +0100 | [diff] [blame] | 55 | fmap_print_once = 1; |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 58 | static void setup_preram_cache(struct region_device *cache_rdev) |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 59 | { |
Julius Werner | 7fc9286 | 2019-11-18 13:01:06 -0800 | [diff] [blame] | 60 | if (CONFIG(NO_FMAP_CACHE)) |
| 61 | return; |
| 62 | |
Josie Nordrum | c3cc158 | 2020-09-09 12:57:13 -0600 | [diff] [blame] | 63 | /* No need to use FMAP cache in SMM */ |
| 64 | if (ENV_SMM) |
| 65 | return; |
| 66 | |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 67 | if (!ENV_ROMSTAGE_OR_BEFORE) { |
| 68 | /* We get here if ramstage makes an FMAP access before calling |
| 69 | cbmem_initialize(). We should avoid letting it come to that, |
| 70 | so print a warning. */ |
| 71 | print_once(BIOS_WARNING, |
| 72 | "WARNING: Post-RAM FMAP access too early for cache!\n"); |
| 73 | return; |
| 74 | } |
| 75 | |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 76 | struct fmap *fmap = (struct fmap *)_fmap_cache; |
Martin Roth | 1594e8f | 2020-07-15 13:57:54 -0600 | [diff] [blame] | 77 | if (!(ENV_INITIAL_STAGE)) { |
| 78 | /* NOTE: This assumes that the first stage will make |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 79 | at least one FMAP access (usually from finding CBFS). */ |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 80 | if (!verify_fmap(fmap)) |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 81 | goto register_cache; |
| 82 | |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 83 | printk(BIOS_ERR, "FMAP cache corrupted?!\n"); |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 84 | if (CONFIG(TOCTOU_SAFETY)) |
| 85 | die("TOCTOU safety relies on FMAP cache"); |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | /* In case we fail below, make sure the cache is invalid. */ |
| 89 | memset(fmap->signature, 0, sizeof(fmap->signature)); |
| 90 | |
| 91 | boot_device_init(); |
| 92 | const struct region_device *boot_rdev = boot_device_ro(); |
| 93 | if (!boot_rdev) |
| 94 | return; |
| 95 | |
| 96 | /* memlayout statically guarantees that the FMAP_CACHE is big enough. */ |
| 97 | if (rdev_readat(boot_rdev, fmap, FMAP_OFFSET, FMAP_SIZE) != FMAP_SIZE) |
| 98 | return; |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 99 | if (verify_fmap(fmap)) |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 100 | return; |
| 101 | report(fmap); |
| 102 | |
| 103 | register_cache: |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 104 | rdev_chain_mem(cache_rdev, fmap, FMAP_SIZE); |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 105 | } |
| 106 | |
Furquan Shaikh | b33a2b0 | 2019-09-26 23:51:46 -0700 | [diff] [blame] | 107 | static int find_fmap_directory(struct region_device *fmrd) |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 108 | { |
| 109 | const struct region_device *boot; |
| 110 | struct fmap *fmap; |
Aaron Durbin | bf1e481 | 2016-05-10 15:12:08 -0500 | [diff] [blame] | 111 | size_t offset = FMAP_OFFSET; |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 112 | |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 113 | /* Try FMAP cache first */ |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 114 | if (!region_device_sz(&fmap_cache)) |
Arthur Heymans | dba22d2 | 2019-11-20 19:57:49 +0100 | [diff] [blame] | 115 | setup_preram_cache(&fmap_cache); |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 116 | if (region_device_sz(&fmap_cache)) |
| 117 | return rdev_chain_full(fmrd, &fmap_cache); |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 118 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 119 | boot_device_init(); |
| 120 | boot = boot_device_ro(); |
| 121 | |
| 122 | if (boot == NULL) |
| 123 | return -1; |
| 124 | |
Julius Werner | 5bc5b1d | 2023-11-06 16:51:27 -0800 | [diff] [blame^] | 125 | fmap = rdev_mmap(boot, offset, FMAP_SIZE); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 126 | |
| 127 | if (fmap == NULL) |
| 128 | return -1; |
| 129 | |
Julius Werner | fdabf3f | 2020-05-06 17:06:35 -0700 | [diff] [blame] | 130 | if (verify_fmap(fmap)) { |
| 131 | printk(BIOS_ERR, "FMAP missing or corrupted at offset 0x%zx!\n", |
| 132 | offset); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 133 | rdev_munmap(boot, fmap); |
| 134 | return -1; |
| 135 | } |
| 136 | |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 137 | report(fmap); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 138 | |
| 139 | rdev_munmap(boot, fmap); |
| 140 | |
Julius Werner | cefe89e | 2019-11-06 19:29:44 -0800 | [diff] [blame] | 141 | return rdev_chain(fmrd, boot, offset, FMAP_SIZE); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | int fmap_locate_area_as_rdev(const char *name, struct region_device *area) |
| 145 | { |
| 146 | struct region ar; |
| 147 | |
| 148 | if (fmap_locate_area(name, &ar)) |
| 149 | return -1; |
| 150 | |
| 151 | return boot_device_ro_subregion(&ar, area); |
| 152 | } |
| 153 | |
Aaron Durbin | bccaab8 | 2016-08-12 12:42:04 -0500 | [diff] [blame] | 154 | int fmap_locate_area_as_rdev_rw(const char *name, struct region_device *area) |
| 155 | { |
| 156 | struct region ar; |
| 157 | |
| 158 | if (fmap_locate_area(name, &ar)) |
| 159 | return -1; |
| 160 | |
| 161 | return boot_device_rw_subregion(&ar, area); |
| 162 | } |
| 163 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 164 | int fmap_locate_area(const char *name, struct region *ar) |
| 165 | { |
| 166 | struct region_device fmrd; |
| 167 | size_t offset; |
| 168 | |
Jakub Czapiga | 5446bdb | 2020-12-10 12:21:52 +0100 | [diff] [blame] | 169 | if (name == NULL || ar == NULL) |
| 170 | return -1; |
| 171 | |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 172 | if (find_fmap_directory(&fmrd)) |
| 173 | return -1; |
| 174 | |
| 175 | /* Start reading the areas just after fmap header. */ |
| 176 | offset = sizeof(struct fmap); |
| 177 | |
| 178 | while (1) { |
| 179 | struct fmap_area *area; |
| 180 | |
| 181 | area = rdev_mmap(&fmrd, offset, sizeof(*area)); |
| 182 | |
| 183 | if (area == NULL) |
| 184 | return -1; |
| 185 | |
| 186 | if (strcmp((const char *)area->name, name)) { |
| 187 | rdev_munmap(&fmrd, area); |
| 188 | offset += sizeof(struct fmap_area); |
| 189 | continue; |
| 190 | } |
| 191 | |
Duncan Laurie | bc2c0a3 | 2016-02-09 09:17:56 -0800 | [diff] [blame] | 192 | printk(BIOS_DEBUG, "FMAP: area %s found @ %x (%d bytes)\n", |
Krystian Hebel | 93f6b8a | 2020-09-30 18:23:14 +0200 | [diff] [blame] | 193 | name, le32toh(area->offset), le32toh(area->size)); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 194 | |
Krystian Hebel | 93f6b8a | 2020-09-30 18:23:14 +0200 | [diff] [blame] | 195 | ar->offset = le32toh(area->offset); |
| 196 | ar->size = le32toh(area->size); |
Aaron Durbin | 0424c95 | 2015-03-28 23:56:22 -0500 | [diff] [blame] | 197 | |
| 198 | rdev_munmap(&fmrd, area); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | printk(BIOS_DEBUG, "FMAP: area %s not found\n", name); |
| 204 | |
| 205 | return -1; |
| 206 | } |
Patrick Georgi | 9952690 | 2015-07-09 11:27:44 +0200 | [diff] [blame] | 207 | |
| 208 | int fmap_find_region_name(const struct region * const ar, |
| 209 | char name[FMAP_STRLEN]) |
| 210 | { |
| 211 | struct region_device fmrd; |
| 212 | size_t offset; |
| 213 | |
Jakub Czapiga | 5446bdb | 2020-12-10 12:21:52 +0100 | [diff] [blame] | 214 | if (name == NULL || ar == NULL) |
| 215 | return -1; |
| 216 | |
Patrick Georgi | 9952690 | 2015-07-09 11:27:44 +0200 | [diff] [blame] | 217 | if (find_fmap_directory(&fmrd)) |
| 218 | return -1; |
| 219 | |
| 220 | /* Start reading the areas just after fmap header. */ |
| 221 | offset = sizeof(struct fmap); |
| 222 | |
| 223 | while (1) { |
| 224 | struct fmap_area *area; |
| 225 | |
| 226 | area = rdev_mmap(&fmrd, offset, sizeof(*area)); |
| 227 | |
| 228 | if (area == NULL) |
| 229 | return -1; |
| 230 | |
Krystian Hebel | 93f6b8a | 2020-09-30 18:23:14 +0200 | [diff] [blame] | 231 | if ((ar->offset != le32toh(area->offset)) || |
| 232 | (ar->size != le32toh(area->size))) { |
Patrick Georgi | 9952690 | 2015-07-09 11:27:44 +0200 | [diff] [blame] | 233 | rdev_munmap(&fmrd, area); |
| 234 | offset += sizeof(struct fmap_area); |
| 235 | continue; |
| 236 | } |
| 237 | |
| 238 | printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) found, named %s\n", |
| 239 | ar->offset, ar->size, area->name); |
| 240 | |
| 241 | memcpy(name, area->name, FMAP_STRLEN); |
| 242 | |
| 243 | rdev_munmap(&fmrd, area); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | |
| 248 | printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) not found\n", |
| 249 | ar->offset, ar->size); |
| 250 | |
| 251 | return -1; |
| 252 | } |
T Michael Turney | 19fcc89 | 2019-03-20 14:37:34 -0700 | [diff] [blame] | 253 | |
| 254 | ssize_t fmap_read_area(const char *name, void *buffer, size_t size) |
| 255 | { |
| 256 | struct region_device rdev; |
| 257 | if (fmap_locate_area_as_rdev(name, &rdev)) |
| 258 | return -1; |
| 259 | return rdev_readat(&rdev, buffer, 0, |
| 260 | MIN(size, region_device_sz(&rdev))); |
| 261 | } |
| 262 | |
| 263 | ssize_t fmap_overwrite_area(const char *name, const void *buffer, size_t size) |
| 264 | { |
| 265 | struct region_device rdev; |
| 266 | |
| 267 | if (fmap_locate_area_as_rdev_rw(name, &rdev)) |
| 268 | return -1; |
| 269 | if (size > region_device_sz(&rdev)) |
| 270 | return -1; |
| 271 | if (rdev_eraseat(&rdev, 0, region_device_sz(&rdev)) < 0) |
| 272 | return -1; |
| 273 | return rdev_writeat(&rdev, buffer, 0, size); |
| 274 | } |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 275 | |
Kyösti Mälkki | 845f232 | 2022-04-06 10:53:17 +0300 | [diff] [blame] | 276 | static void fmap_register_cbmem_cache(void) |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 277 | { |
| 278 | const struct cbmem_entry *e; |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 279 | |
| 280 | /* Find the FMAP cache installed by previous stage */ |
| 281 | e = cbmem_entry_find(CBMEM_ID_FMAP); |
| 282 | /* Don't set fmap_cache so that find_fmap_directory will use regular path */ |
| 283 | if (!e) |
| 284 | return; |
| 285 | |
Julius Werner | c893197 | 2021-04-16 16:48:32 -0700 | [diff] [blame] | 286 | rdev_chain_mem(&fmap_cache, cbmem_entry_start(e), cbmem_entry_size(e)); |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | /* |
| 290 | * The main reason to copy the FMAP into CBMEM is to make it available to the |
| 291 | * OS on every architecture. As side effect use the CBMEM copy as cache. |
| 292 | */ |
Kyösti Mälkki | 845f232 | 2022-04-06 10:53:17 +0300 | [diff] [blame] | 293 | static void fmap_add_cbmem_cache(void) |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 294 | { |
| 295 | struct region_device fmrd; |
| 296 | |
| 297 | if (find_fmap_directory(&fmrd)) |
| 298 | return; |
| 299 | |
| 300 | /* Reloads the FMAP even on ACPI S3 resume */ |
| 301 | const size_t s = region_device_sz(&fmrd); |
| 302 | struct fmap *fmap = cbmem_add(CBMEM_ID_FMAP, s); |
| 303 | if (!fmap) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 304 | printk(BIOS_ERR, "Failed to allocate CBMEM\n"); |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 305 | return; |
| 306 | } |
| 307 | |
| 308 | const ssize_t ret = rdev_readat(&fmrd, fmap, 0, s); |
| 309 | if (ret != s) { |
Julius Werner | e966595 | 2022-01-21 17:06:20 -0800 | [diff] [blame] | 310 | printk(BIOS_ERR, "Failed to read FMAP into CBMEM\n"); |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 311 | cbmem_entry_remove(cbmem_entry_find(CBMEM_ID_FMAP)); |
| 312 | return; |
| 313 | } |
Kyösti Mälkki | 845f232 | 2022-04-06 10:53:17 +0300 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static void fmap_setup_cbmem_cache(int unused) |
| 317 | { |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 318 | if (ENV_CREATES_CBMEM) |
Kyösti Mälkki | 845f232 | 2022-04-06 10:53:17 +0300 | [diff] [blame] | 319 | fmap_add_cbmem_cache(); |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 320 | |
| 321 | /* Finally advertise the cache for the current stage */ |
Kyösti Mälkki | 845f232 | 2022-04-06 10:53:17 +0300 | [diff] [blame] | 322 | fmap_register_cbmem_cache(); |
Patrick Rudolph | 6d787c2 | 2019-09-12 13:21:37 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Kyösti Mälkki | fa3bc04 | 2022-03-31 07:40:10 +0300 | [diff] [blame] | 325 | CBMEM_READY_HOOK(fmap_setup_cbmem_cache); |