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