sb/amd/agesa: Fix some white spaces issues
[coreboot.git] / src / lib / fmap.c
blob3889dd5f5d617e818a8bc2b84d49b0aefccb0bf5
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <boot_device.h>
4 #include <cbmem.h>
5 #include <console/console.h>
6 #include <fmap.h>
7 #include <metadata_hash.h>
8 #include <stddef.h>
9 #include <string.h>
10 #include <symbols.h>
11 #include <endian.h>
13 #include "fmap_config.h"
16 * See http://code.google.com/p/flashmap/ for more information on FMAP.
19 static int fmap_print_once;
20 static struct region_device fmap_cache;
22 #define print_once(...) do { \
23 if (!fmap_print_once) \
24 printk(__VA_ARGS__); \
25 } while (0)
27 uint64_t get_fmap_flash_offset(void)
29 return FMAP_OFFSET;
32 static int verify_fmap(const struct fmap *fmap)
34 if (memcmp(fmap->signature, FMAP_SIGNATURE, sizeof(fmap->signature)))
35 return -1;
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. */
41 if (metadata_hash_verify_fmap(fmap, FMAP_SIZE) != VB2_SUCCESS)
42 return -1;
44 done = true;
45 return 0;
48 static void report(const struct fmap *fmap)
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",
53 (long long)le64toh(fmap->base), le32toh(fmap->size),
54 le16toh(fmap->nareas));
55 fmap_print_once = 1;
58 static void setup_preram_cache(struct region_device *cache_rdev)
60 if (CONFIG(NO_FMAP_CACHE))
61 return;
63 /* No need to use FMAP cache in SMM */
64 if (ENV_SMM)
65 return;
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;
76 struct fmap *fmap = (struct fmap *)_fmap_cache;
77 if (!(ENV_INITIAL_STAGE)) {
78 /* NOTE: This assumes that the first stage will make
79 at least one FMAP access (usually from finding CBFS). */
80 if (!verify_fmap(fmap))
81 goto register_cache;
83 printk(BIOS_ERR, "FMAP cache corrupted?!\n");
84 if (CONFIG(TOCTOU_SAFETY))
85 die("TOCTOU safety relies on FMAP cache");
88 /* In case we fail below, make sure the cache is invalid. */
89 memset(fmap->signature, 0, sizeof(fmap->signature));
91 boot_device_init();
92 const struct region_device *boot_rdev = boot_device_ro();
93 if (!boot_rdev)
94 return;
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;
99 if (verify_fmap(fmap))
100 return;
101 report(fmap);
103 register_cache:
104 rdev_chain_mem(cache_rdev, fmap, FMAP_SIZE);
107 static int find_fmap_directory(struct region_device *fmrd)
109 const struct region_device *boot;
110 struct fmap *fmap;
111 size_t offset = FMAP_OFFSET;
113 /* Try FMAP cache first */
114 if (!region_device_sz(&fmap_cache))
115 setup_preram_cache(&fmap_cache);
116 if (region_device_sz(&fmap_cache))
117 return rdev_chain_full(fmrd, &fmap_cache);
119 boot_device_init();
120 boot = boot_device_ro();
122 if (boot == NULL)
123 return -1;
125 fmap = rdev_mmap(boot, offset, sizeof(struct fmap));
127 if (fmap == NULL)
128 return -1;
130 if (verify_fmap(fmap)) {
131 printk(BIOS_ERR, "FMAP missing or corrupted at offset 0x%zx!\n",
132 offset);
133 rdev_munmap(boot, fmap);
134 return -1;
137 report(fmap);
139 rdev_munmap(boot, fmap);
141 return rdev_chain(fmrd, boot, offset, FMAP_SIZE);
144 int fmap_locate_area_as_rdev(const char *name, struct region_device *area)
146 struct region ar;
148 if (fmap_locate_area(name, &ar))
149 return -1;
151 return boot_device_ro_subregion(&ar, area);
154 int fmap_locate_area_as_rdev_rw(const char *name, struct region_device *area)
156 struct region ar;
158 if (fmap_locate_area(name, &ar))
159 return -1;
161 return boot_device_rw_subregion(&ar, area);
164 int fmap_locate_area(const char *name, struct region *ar)
166 struct region_device fmrd;
167 size_t offset;
169 if (name == NULL || ar == NULL)
170 return -1;
172 if (find_fmap_directory(&fmrd))
173 return -1;
175 /* Start reading the areas just after fmap header. */
176 offset = sizeof(struct fmap);
178 while (1) {
179 struct fmap_area *area;
181 area = rdev_mmap(&fmrd, offset, sizeof(*area));
183 if (area == NULL)
184 return -1;
186 if (strcmp((const char *)area->name, name)) {
187 rdev_munmap(&fmrd, area);
188 offset += sizeof(struct fmap_area);
189 continue;
192 printk(BIOS_DEBUG, "FMAP: area %s found @ %x (%d bytes)\n",
193 name, le32toh(area->offset), le32toh(area->size));
195 ar->offset = le32toh(area->offset);
196 ar->size = le32toh(area->size);
198 rdev_munmap(&fmrd, area);
200 return 0;
203 printk(BIOS_DEBUG, "FMAP: area %s not found\n", name);
205 return -1;
208 int fmap_find_region_name(const struct region * const ar,
209 char name[FMAP_STRLEN])
211 struct region_device fmrd;
212 size_t offset;
214 if (name == NULL || ar == NULL)
215 return -1;
217 if (find_fmap_directory(&fmrd))
218 return -1;
220 /* Start reading the areas just after fmap header. */
221 offset = sizeof(struct fmap);
223 while (1) {
224 struct fmap_area *area;
226 area = rdev_mmap(&fmrd, offset, sizeof(*area));
228 if (area == NULL)
229 return -1;
231 if ((ar->offset != le32toh(area->offset)) ||
232 (ar->size != le32toh(area->size))) {
233 rdev_munmap(&fmrd, area);
234 offset += sizeof(struct fmap_area);
235 continue;
238 printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) found, named %s\n",
239 ar->offset, ar->size, area->name);
241 memcpy(name, area->name, FMAP_STRLEN);
243 rdev_munmap(&fmrd, area);
245 return 0;
248 printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) not found\n",
249 ar->offset, ar->size);
251 return -1;
254 ssize_t fmap_read_area(const char *name, void *buffer, size_t size)
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)));
263 ssize_t fmap_overwrite_area(const char *name, const void *buffer, size_t size)
265 struct region_device rdev;
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);
276 static void fmap_register_cbmem_cache(void)
278 const struct cbmem_entry *e;
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;
286 rdev_chain_mem(&fmap_cache, cbmem_entry_start(e), cbmem_entry_size(e));
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.
293 static void fmap_add_cbmem_cache(void)
295 struct region_device fmrd;
297 if (find_fmap_directory(&fmrd))
298 return;
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) {
304 printk(BIOS_ERR, "Failed to allocate CBMEM\n");
305 return;
308 const ssize_t ret = rdev_readat(&fmrd, fmap, 0, s);
309 if (ret != s) {
310 printk(BIOS_ERR, "Failed to read FMAP into CBMEM\n");
311 cbmem_entry_remove(cbmem_entry_find(CBMEM_ID_FMAP));
312 return;
316 static void fmap_setup_cbmem_cache(int unused)
318 if (ENV_CREATES_CBMEM)
319 fmap_add_cbmem_cache();
321 /* Finally advertise the cache for the current stage */
322 fmap_register_cbmem_cache();
325 CBMEM_READY_HOOK(fmap_setup_cbmem_cache);