diff options
author | Julius Werner <jwerner@chromium.org> | 2015-09-29 13:51:35 -0700 |
---|---|---|
committer | Julius Werner <jwerner@chromium.org> | 2016-02-22 21:38:37 +0100 |
commit | 09f2921b5dacaf79b391652cecd606be4dd69f50 (patch) | |
tree | 581236de48a22240cb1b823195a79493f037fb64 /src/lib/cbfs.c | |
parent | 0e3d7de7410c4ff7c9465261b58524675a0329e2 (diff) |
cbfs: Add LZ4 in-place decompression support for pre-RAM stages
This patch ports the LZ4 decompression code that debuted in libpayload
last year to coreboot for use in CBFS stages (upgrading the base
algorithm to LZ4's dev branch to access the new in-place decompression
checks). This is especially useful for pre-RAM stages in constrained
SRAM-based systems, which previously could not be compressed due to
the size requirements of the LZMA scratchpad and bounce buffer. The
LZ4 algorithm offers a very lean decompressor function and in-place
decompression support to achieve roughly the same boot speed gains
(trading compression ratio for decompression time) with nearly no
memory overhead.
For now we only activate it for the stages that had previously not been
compressed at all on non-XIP (read: non-x86) boards. In the future we
may also consider replacing LZMA completely for certain boards, since
which algorithm wins out on boot speed depends on board-specific
parameters (architecture, processor speed, SPI transfer rate, etc.).
BRANCH=None
BUG=None
TEST=Built and booted Oak, Jerry, Nyan and Falco. Measured boot time on
Oak to be about ~20ms faster (cutting load times for affected stages
almost in half).
Change-Id: Iec256c0e6d585d1b69985461939884a54e3ab900
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/13638
Tested-by: build bot (Jenkins)
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Diffstat (limited to 'src/lib/cbfs.c')
-rw-r--r-- | src/lib/cbfs.c | 75 |
1 files changed, 54 insertions, 21 deletions
diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c index 55a8536a36..82bfa2d8ad 100644 --- a/src/lib/cbfs.c +++ b/src/lib/cbfs.c @@ -19,9 +19,11 @@ #include <stdlib.h> #include <boot_device.h> #include <cbfs.h> +#include <commonlib/compression.h> #include <endian.h> #include <lib.h> #include <symbols.h> +#include <timestamp.h> #define ERROR(x...) printk(BIOS_ERR, "CBFS: " x) #define LOG(x...) printk(BIOS_INFO, "CBFS: " x) @@ -68,13 +70,57 @@ void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size) return rdev_mmap(&fh.data, 0, fsize); } -static size_t inflate(void *src, void *dst) +size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset, + size_t in_size, void *buffer, size_t buffer_size, uint32_t compression) { - if (ENV_BOOTBLOCK || ENV_VERSTAGE) - return 0; - if (ENV_ROMSTAGE && !IS_ENABLED(CONFIG_COMPRESS_RAMSTAGE)) + size_t out_size; + + switch (compression) { + case CBFS_COMPRESS_NONE: + if (rdev_readat(rdev, buffer, offset, in_size) != in_size) + return 0; + return in_size; + + case CBFS_COMPRESS_LZ4: + if ((ENV_BOOTBLOCK || ENV_VERSTAGE) && + !IS_ENABLED(CONFIG_COMPRESS_PRERAM_STAGES)) + return 0; + + /* Load the compressed image to the end of the available memory + * area for in-place decompression. It is the responsibility of + * the caller to ensure that buffer_size is large enough + * (see compression.h, guaranteed by cbfstool for stages). */ + void *compr_start = buffer + buffer_size - in_size; + if (rdev_readat(rdev, compr_start, offset, in_size) != in_size) + return 0; + + timestamp_add_now(TS_START_ULZ4F); + out_size = ulz4fn(compr_start, in_size, buffer, buffer_size); + timestamp_add_now(TS_END_ULZ4F); + return out_size; + + case CBFS_COMPRESS_LZMA: + if (ENV_BOOTBLOCK || ENV_VERSTAGE) + return 0; + if (ENV_ROMSTAGE && !IS_ENABLED(CONFIG_COMPRESS_RAMSTAGE)) + return 0; + + void *map = rdev_mmap(rdev, offset, in_size); + if (map == NULL) + return 0; + + /* Note: timestamp not useful for memory-mapped media (x86) */ + timestamp_add_now(TS_START_ULZMA); + out_size = ulzman(map, in_size, buffer, buffer_size); + timestamp_add_now(TS_END_ULZMA); + + rdev_munmap(rdev, map); + + return out_size; + + default: return 0; - return ulzma(src, dst); + } } static inline int tohex4(unsigned int c) @@ -152,22 +198,9 @@ int cbfs_prog_stage_load(struct prog *pstage) goto out; } - if (stage.compression == CBFS_COMPRESS_NONE) { - if (rdev_readat(fh, load, foffset, fsize) != fsize) - return -1; - } else if (stage.compression == CBFS_COMPRESS_LZMA) { - void *map = rdev_mmap(fh, foffset, fsize); - - if (map == NULL) - return -1; - - fsize = inflate(map, load); - - rdev_munmap(fh, map); - - if (!fsize) - return -1; - } else + fsize = cbfs_load_and_decompress(fh, foffset, fsize, load, + stage.memlen, stage.compression); + if (!fsize) return -1; /* Clear area not covered by file. */ |