cbfs: Pull handling of the CBFS_CACHE mem_pool into CBFS core

This patch pulls control of the memory pool serving allocations from the
CBFS_CACHE memlayout area into cbfs.c and makes it a core part of the
CBFS API. Previously, platforms would independently instantiate this as
part of boot_device_ro() (mostly through cbfs_spi.c). The new cbfs_cache
pool is exported as a global so these platforms can still use it to
directly back rdev_mmap() on their boot device, but the cbfs_cache can
now also use it to directly make allocations itself. This is used to
allow transparent decompression support in cbfs_map().

Signed-off-by: Julius Werner <jwerner@chromium.org>
Change-Id: I0d52b6a8f582a81a19fd0fd663bb89eab55a49d9
Reviewed-on: https://review.coreboot.org/c/coreboot/+/49333
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
This commit is contained in:
Julius Werner
2020-12-30 17:30:12 -08:00
parent 11075fc80e
commit 9b1f3cc6fb
10 changed files with 89 additions and 49 deletions

View File

@ -18,6 +18,18 @@
#include <symbols.h>
#include <timestamp.h>
#if CBFS_CACHE_AVAILABLE
struct mem_pool cbfs_cache = MEM_POOL_INIT(_cbfs_cache, REGION_SIZE(cbfs_cache));
static void switch_to_postram_cache(int unused)
{
if (_preram_cbfs_cache != _postram_cbfs_cache)
mem_pool_init(&cbfs_cache, _postram_cbfs_cache,
REGION_SIZE(postram_cbfs_cache));
}
ROMSTAGE_CBMEM_INIT_HOOK(switch_to_postram_cache);
#endif
cb_err_t cbfs_boot_lookup(const char *name, bool force_ro,
union cbfs_mdata *mdata, struct region_device *rdev)
{
@ -101,14 +113,48 @@ void *_cbfs_map(const char *name, size_t *size_out, bool force_ro)
if (size_out != NULL)
*size_out = region_device_sz(&rdev);
return rdev_mmap_full(&rdev);
uint32_t compression = CBFS_COMPRESS_NONE;
const struct cbfs_file_attr_compression *attr = cbfs_find_attr(&mdata,
CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*attr));
if (attr)
compression = be32toh(attr->compression);
if (compression == CBFS_COMPRESS_NONE)
return rdev_mmap_full(&rdev);
if (!CBFS_CACHE_AVAILABLE) {
ERROR("Cannot map compressed file %s on x86\n", mdata.h.filename);
return NULL;
}
size_t size = be32toh(attr->decompressed_size);
void *buf = mem_pool_alloc(&cbfs_cache, size);
if (!buf) {
ERROR("CBFS cache out of memory when mapping %s\n", mdata.h.filename);
return NULL;
}
size = cbfs_load_and_decompress(&rdev, 0, region_device_sz(&rdev), buf, size,
compression);
if (!size)
return NULL;
if (size_out != NULL)
*size_out = size;
return buf;
}
int cbfs_unmap(void *mapping)
void cbfs_unmap(void *mapping)
{
/* This works because munmap() only works on the root rdev and never cares about which
chained subregion something was mapped from. */
return rdev_munmap(boot_device_ro(), mapping);
/*
* This is save to call with mappings that weren't allocated in the cache (e.g. x86
* direct mappings) -- mem_pool_free() just does nothing for addresses it doesn't
* recognize. This hardcodes the assumption that if platforms implement an rdev_mmap()
* that requires a free() for the boot_device, they need to implement it via the
* cbfs_cache mem_pool.
*/
if (CBFS_CACHE_AVAILABLE)
mem_pool_free(&cbfs_cache, mapping);
}
int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
@ -158,6 +204,9 @@ static inline bool cbfs_lz4_enabled(void)
if ((ENV_BOOTBLOCK || ENV_SEPARATE_VERSTAGE) && !CONFIG(COMPRESS_PRERAM_STAGES))
return false;
if (ENV_SMM)
return false;
return true;
}
@ -174,6 +223,8 @@ static inline bool cbfs_lzma_enabled(void)
return false;
if ((ENV_ROMSTAGE || ENV_POSTCAR) && !CONFIG(COMPRESS_RAMSTAGE))
return false;
if (ENV_SMM)
return false;
return true;
}