fmap: Eliminate some impossible code paths

When the FMAP cache is enabled, it cannot fail in pre-RAM stages unless
flash I/O in general doesn't work. Therefore, it is unnecessary and a
waste of binary size to also link a fallback path for this case.
Similarly, once the cache is written to CAR/SRAM/CBMEM there should be
no way for it to become magically corrupted between boot stages. Many
other parts of coreboot blindly assume that persistent memory stays
valid between stages so there is no reason why this code should link in
extra fallback paths in case it doesn't.

This saves a little over 200 bytes per affected (uncompressed) stage on
aarch64.

Change-Id: I7b8251dd6b34fe4f63865ebc44b9a8a103f32a57
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/78904
Reviewed-by: Yu-Ping Wu <yupingso@google.com>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
This commit is contained in:
Julius Werner 2023-11-02 16:20:17 -07:00
parent 682cb3b564
commit ca71588620
1 changed files with 10 additions and 6 deletions

View File

@ -31,8 +31,11 @@ uint64_t get_fmap_flash_offset(void)
static int verify_fmap(const struct fmap *fmap) static int verify_fmap(const struct fmap *fmap)
{ {
if (memcmp(fmap->signature, FMAP_SIGNATURE, sizeof(fmap->signature))) if (memcmp(fmap->signature, FMAP_SIGNATURE, sizeof(fmap->signature))) {
if (ENV_INITIAL_STAGE)
printk(BIOS_ERR, "Invalid FMAP at %#x\n", FMAP_OFFSET);
return -1; return -1;
}
static bool done = false; static bool done = false;
if (!CONFIG(CBFS_VERIFICATION) || !ENV_INITIAL_STAGE || done) if (!CONFIG(CBFS_VERIFICATION) || !ENV_INITIAL_STAGE || done)
@ -82,9 +85,8 @@ static void setup_preram_cache(struct region_device *cache_rdev)
if (!verify_fmap(fmap)) if (!verify_fmap(fmap))
goto register_cache; goto register_cache;
printk(BIOS_ERR, "FMAP cache corrupted?!\n"); /* This shouldn't happen, so no point providing a fallback path here. */
if (CONFIG(TOCTOU_SAFETY)) die("FMAP cache corrupted?!\n");
die("TOCTOU safety relies on FMAP cache");
} }
/* In case we fail below, make sure the cache is invalid. */ /* In case we fail below, make sure the cache is invalid. */
@ -118,6 +120,10 @@ static int find_fmap_directory(struct region_device *fmrd)
if (region_device_sz(&fmap_cache)) if (region_device_sz(&fmap_cache))
return rdev_chain_full(fmrd, &fmap_cache); return rdev_chain_full(fmrd, &fmap_cache);
/* Cache setup in pre-RAM stages can't fail, unless flash I/O in general failed. */
if (!CONFIG(NO_FMAP_CACHE) && ENV_ROMSTAGE_OR_BEFORE)
return -1;
boot_device_init(); boot_device_init();
boot = boot_device_ro(); boot = boot_device_ro();
@ -130,8 +136,6 @@ static int find_fmap_directory(struct region_device *fmrd)
return -1; return -1;
if (verify_fmap(fmap)) { if (verify_fmap(fmap)) {
printk(BIOS_ERR, "FMAP missing or corrupted at offset 0x%zx!\n",
offset);
rdev_munmap(boot, fmap); rdev_munmap(boot, fmap);
return -1; return -1;
} }