1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <boot_device.h>
5 #include <console/console.h>
7 #include <metadata_hash.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__); \
27 uint64_t get_fmap_flash_offset(void)
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
);
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");
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
));
63 static void setup_preram_cache(struct region_device
*cache_rdev
)
65 if (CONFIG(NO_FMAP_CACHE
))
68 /* No need to use FMAP cache in SMM */
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");
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
))
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
));
96 const struct region_device
*boot_rdev
= boot_device_ro();
100 /* memlayout statically guarantees that the FMAP_CACHE is big enough. */
101 if (rdev_readat(boot_rdev
, fmap
, FMAP_OFFSET
, FMAP_SIZE
) != FMAP_SIZE
)
103 if (verify_fmap(fmap
))
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
;
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
)
128 boot
= boot_device_ro();
133 fmap
= rdev_mmap(boot
, offset
,
134 CONFIG(CBFS_VERIFICATION
) ? FMAP_SIZE
: sizeof(struct fmap
));
139 if (verify_fmap(fmap
)) {
140 rdev_munmap(boot
, 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
)
155 if (fmap_locate_area(name
, &ar
))
158 return boot_device_ro_subregion(&ar
, area
);
161 int fmap_locate_area_as_rdev_rw(const char *name
, struct region_device
*area
)
165 if (fmap_locate_area(name
, &ar
))
168 return boot_device_rw_subregion(&ar
, area
);
171 int fmap_locate_area(const char *name
, struct region
*ar
)
173 struct region_device fmrd
;
176 if (name
== NULL
|| ar
== NULL
)
179 if (find_fmap_directory(&fmrd
))
182 /* Start reading the areas just after fmap header. */
183 offset
= sizeof(struct fmap
);
186 struct fmap_area
*area
;
188 area
= rdev_mmap(&fmrd
, offset
, sizeof(*area
));
193 if (strcmp((const char *)area
->name
, name
)) {
194 rdev_munmap(&fmrd
, area
);
195 offset
+= sizeof(struct fmap_area
);
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
);
210 printk(BIOS_DEBUG
, "FMAP: area %s not found\n", name
);
215 int fmap_find_region_name(const struct region
* const ar
,
216 char name
[FMAP_STRLEN
])
218 struct region_device fmrd
;
221 if (name
== NULL
|| ar
== NULL
)
224 if (find_fmap_directory(&fmrd
))
227 /* Start reading the areas just after fmap header. */
228 offset
= sizeof(struct fmap
);
231 struct fmap_area
*area
;
233 area
= rdev_mmap(&fmrd
, offset
, sizeof(*area
));
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
);
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
);
255 printk(BIOS_DEBUG
, "FMAP: area (%zx, %zx) not found\n",
256 region_offset(ar
), region_sz(ar
));
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
))
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
))
276 if (size
> region_device_sz(&rdev
))
278 if (rdev_eraseat(&rdev
, 0, region_device_sz(&rdev
)) < 0)
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 */
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
))
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
);
311 printk(BIOS_ERR
, "Failed to allocate CBMEM\n");
315 const ssize_t ret
= rdev_readat(&fmrd
, fmap
, 0, s
);
317 printk(BIOS_ERR
, "Failed to read FMAP into CBMEM\n");
318 cbmem_entry_remove(cbmem_entry_find(CBMEM_ID_FMAP
));
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
);