Xeon-SP boards: Factor out OCP VPD `get_cxl_mode()` impl
[coreboot2.git] / src / lib / fmap.c
blob75c5a9fb1c6f3e13ef35fc8113a8c24de6f131c5
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 if (ENV_INITIAL_STAGE)
36 printk(BIOS_ERR, "Invalid FMAP at %#x\n", FMAP_OFFSET);
37 return -1;
40 static bool done = false;
41 if (!CONFIG(CBFS_VERIFICATION) || !ENV_INITIAL_STAGE || done)
42 return 0; /* Only need to check hash in first stage. */
44 /* On error we need to die right here, lest we risk a TOCTOU attack where the cache is
45 filled with a tampered FMAP but the later fallback path is fed a valid one. */
46 if (metadata_hash_verify_fmap(fmap, FMAP_SIZE) != VB2_SUCCESS)
47 die("FMAP verification failure");
49 done = true;
50 return 0;
53 static void report(const struct fmap *fmap)
55 print_once(BIOS_DEBUG, "FMAP: Found \"%s\" version %d.%d at %#x.\n",
56 fmap->name, fmap->ver_major, fmap->ver_minor, FMAP_OFFSET);
57 print_once(BIOS_DEBUG, "FMAP: base = %#llx size = %#x #areas = %d\n",
58 (long long)le64toh(fmap->base), le32toh(fmap->size),
59 le16toh(fmap->nareas));
60 fmap_print_once = 1;
63 static void setup_preram_cache(struct region_device *cache_rdev)
65 if (CONFIG(NO_FMAP_CACHE))
66 return;
68 /* No need to use FMAP cache in SMM */
69 if (ENV_SMM)
70 return;
72 if (!ENV_ROMSTAGE_OR_BEFORE) {
73 /* We get here if ramstage makes an FMAP access before calling
74 cbmem_initialize(). We should avoid letting it come to that,
75 so print a warning. */
76 print_once(BIOS_WARNING,
77 "WARNING: Post-RAM FMAP access too early for cache!\n");
78 return;
81 struct fmap *fmap = (struct fmap *)_fmap_cache;
82 if (!(ENV_INITIAL_STAGE)) {
83 /* NOTE: This assumes that the first stage will make
84 at least one FMAP access (usually from finding CBFS). */
85 if (!verify_fmap(fmap))
86 goto register_cache;
88 /* This shouldn't happen, so no point providing a fallback path here. */
89 die("FMAP cache corrupted?!\n");
92 /* In case we fail below, make sure the cache is invalid. */
93 memset(fmap->signature, 0, sizeof(fmap->signature));
95 boot_device_init();
96 const struct region_device *boot_rdev = boot_device_ro();
97 if (!boot_rdev)
98 return;
100 /* memlayout statically guarantees that the FMAP_CACHE is big enough. */
101 if (rdev_readat(boot_rdev, fmap, FMAP_OFFSET, FMAP_SIZE) != FMAP_SIZE)
102 return;
103 if (verify_fmap(fmap))
104 return;
105 report(fmap);
107 register_cache:
108 rdev_chain_mem(cache_rdev, fmap, FMAP_SIZE);
111 static int find_fmap_directory(struct region_device *fmrd)
113 const struct region_device *boot;
114 struct fmap *fmap;
115 size_t offset = FMAP_OFFSET;
117 /* Try FMAP cache first */
118 if (!region_device_sz(&fmap_cache))
119 setup_preram_cache(&fmap_cache);
120 if (region_device_sz(&fmap_cache))
121 return rdev_chain_full(fmrd, &fmap_cache);
123 /* Cache setup in pre-RAM stages can't fail, unless flash I/O in general failed. */
124 if (!CONFIG(NO_FMAP_CACHE) && ENV_ROMSTAGE_OR_BEFORE)
125 return -1;
127 boot_device_init();
128 boot = boot_device_ro();
130 if (boot == NULL)
131 return -1;
133 fmap = rdev_mmap(boot, offset,
134 CONFIG(CBFS_VERIFICATION) ? FMAP_SIZE : sizeof(struct fmap));
136 if (fmap == NULL)
137 return -1;
139 if (verify_fmap(fmap)) {
140 rdev_munmap(boot, fmap);
141 return -1;
144 report(fmap);
146 rdev_munmap(boot, fmap);
148 return rdev_chain(fmrd, boot, offset, FMAP_SIZE);
151 int fmap_locate_area_as_rdev(const char *name, struct region_device *area)
153 struct region ar;
155 if (fmap_locate_area(name, &ar))
156 return -1;
158 return boot_device_ro_subregion(&ar, area);
161 int fmap_locate_area_as_rdev_rw(const char *name, struct region_device *area)
163 struct region ar;
165 if (fmap_locate_area(name, &ar))
166 return -1;
168 return boot_device_rw_subregion(&ar, area);
171 int fmap_locate_area(const char *name, struct region *ar)
173 struct region_device fmrd;
174 size_t offset;
176 if (name == NULL || ar == NULL)
177 return -1;
179 if (find_fmap_directory(&fmrd))
180 return -1;
182 /* Start reading the areas just after fmap header. */
183 offset = sizeof(struct fmap);
185 while (1) {
186 struct fmap_area *area;
188 area = rdev_mmap(&fmrd, offset, sizeof(*area));
190 if (area == NULL)
191 return -1;
193 if (strcmp((const char *)area->name, name)) {
194 rdev_munmap(&fmrd, area);
195 offset += sizeof(struct fmap_area);
196 continue;
199 printk(BIOS_DEBUG, "FMAP: area %s found @ %x (%d bytes)\n",
200 name, le32toh(area->offset), le32toh(area->size));
202 ar->offset = le32toh(area->offset);
203 ar->size = le32toh(area->size);
205 rdev_munmap(&fmrd, area);
207 return 0;
210 printk(BIOS_DEBUG, "FMAP: area %s not found\n", name);
212 return -1;
215 int fmap_find_region_name(const struct region * const ar,
216 char name[FMAP_STRLEN])
218 struct region_device fmrd;
219 size_t offset;
221 if (name == NULL || ar == NULL)
222 return -1;
224 if (find_fmap_directory(&fmrd))
225 return -1;
227 /* Start reading the areas just after fmap header. */
228 offset = sizeof(struct fmap);
230 while (1) {
231 struct fmap_area *area;
233 area = rdev_mmap(&fmrd, offset, sizeof(*area));
235 if (area == NULL)
236 return -1;
238 if (region_offset(ar) != le32toh(area->offset) ||
239 region_sz(ar) != le32toh(area->size)) {
240 rdev_munmap(&fmrd, area);
241 offset += sizeof(struct fmap_area);
242 continue;
245 printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) found, named %s\n",
246 region_offset(ar), region_sz(ar), area->name);
248 memcpy(name, area->name, FMAP_STRLEN);
250 rdev_munmap(&fmrd, area);
252 return 0;
255 printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) not found\n",
256 region_offset(ar), region_sz(ar));
258 return -1;
261 ssize_t fmap_read_area(const char *name, void *buffer, size_t size)
263 struct region_device rdev;
264 if (fmap_locate_area_as_rdev(name, &rdev))
265 return -1;
266 return rdev_readat(&rdev, buffer, 0,
267 MIN(size, region_device_sz(&rdev)));
270 ssize_t fmap_overwrite_area(const char *name, const void *buffer, size_t size)
272 struct region_device rdev;
274 if (fmap_locate_area_as_rdev_rw(name, &rdev))
275 return -1;
276 if (size > region_device_sz(&rdev))
277 return -1;
278 if (rdev_eraseat(&rdev, 0, region_device_sz(&rdev)) < 0)
279 return -1;
280 return rdev_writeat(&rdev, buffer, 0, size);
283 static void fmap_register_cbmem_cache(void)
285 const struct cbmem_entry *e;
287 /* Find the FMAP cache installed by previous stage */
288 e = cbmem_entry_find(CBMEM_ID_FMAP);
289 /* Don't set fmap_cache so that find_fmap_directory will use regular path */
290 if (!e)
291 return;
293 rdev_chain_mem(&fmap_cache, cbmem_entry_start(e), cbmem_entry_size(e));
297 * The main reason to copy the FMAP into CBMEM is to make it available to the
298 * OS on every architecture. As side effect use the CBMEM copy as cache.
300 static void fmap_add_cbmem_cache(void)
302 struct region_device fmrd;
304 if (find_fmap_directory(&fmrd))
305 return;
307 /* Reloads the FMAP even on ACPI S3 resume */
308 const size_t s = region_device_sz(&fmrd);
309 struct fmap *fmap = cbmem_add(CBMEM_ID_FMAP, s);
310 if (!fmap) {
311 printk(BIOS_ERR, "Failed to allocate CBMEM\n");
312 return;
315 const ssize_t ret = rdev_readat(&fmrd, fmap, 0, s);
316 if (ret != s) {
317 printk(BIOS_ERR, "Failed to read FMAP into CBMEM\n");
318 cbmem_entry_remove(cbmem_entry_find(CBMEM_ID_FMAP));
319 return;
323 static void fmap_setup_cbmem_cache(int unused)
325 if (ENV_CREATES_CBMEM)
326 fmap_add_cbmem_cache();
328 /* Finally advertise the cache for the current stage */
329 fmap_register_cbmem_cache();
332 CBMEM_READY_HOOK(fmap_setup_cbmem_cache);