libpayload/libcbfs: Add unverified_area APIs

This patch introduces equivalents of unverified_area CBFS access
functions added to the main coreboot tree in CB:59678

Change-Id: Ibadfd2a5cb6ad037ef1222b0a4301f90a79a127b
Signed-off-by: Jakub Czapiga <jacz@semihalf.com>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/60714
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
This commit is contained in:
Jakub Czapiga 2022-01-03 15:06:21 +00:00 committed by Felix Held
parent b3398ba562
commit ad45f681aa
3 changed files with 158 additions and 22 deletions

View File

@ -17,9 +17,13 @@
static inline size_t cbfs_load(const char *name, void *buf, size_t size); static inline size_t cbfs_load(const char *name, void *buf, size_t size);
static inline size_t cbfs_ro_load(const char *name, void *buf, size_t size); static inline size_t cbfs_ro_load(const char *name, void *buf, size_t size);
static inline size_t cbfs_unverified_area_load(const char *area, const char *name, void *buf,
size_t size);
static inline void *cbfs_map(const char *name, size_t *size_out); static inline void *cbfs_map(const char *name, size_t *size_out);
static inline void *cbfs_ro_map(const char *name, size_t *size_out); static inline void *cbfs_ro_map(const char *name, size_t *size_out);
static inline void *cbfs_unverified_area_map(const char *area, const char *name,
size_t *size_out);
void cbfs_unmap(void *mapping); void cbfs_unmap(void *mapping);
@ -39,6 +43,9 @@ ssize_t _cbfs_boot_lookup(const char *name, bool force_ro, union cbfs_mdata *mda
void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro); void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro);
void *_cbfs_unverified_area_load(const char *area, const char *name, void *buf,
size_t *size_inout);
/********************************************************************************************** /**********************************************************************************************
* INLINE IMPLEMENTATIONS * * INLINE IMPLEMENTATIONS *
**********************************************************************************************/ **********************************************************************************************/
@ -53,6 +60,12 @@ static inline void *cbfs_ro_map(const char *name, size_t *size_out)
return _cbfs_load(name, NULL, size_out, true); return _cbfs_load(name, NULL, size_out, true);
} }
static inline void *cbfs_unverified_area_map(const char *area, const char *name,
size_t *size_out)
{
return _cbfs_unverified_area_load(area, name, NULL, size_out);
}
static inline size_t cbfs_load(const char *name, void *buf, size_t size) static inline size_t cbfs_load(const char *name, void *buf, size_t size)
{ {
if (_cbfs_load(name, buf, &size, false)) if (_cbfs_load(name, buf, &size, false))
@ -69,6 +82,15 @@ static inline size_t cbfs_ro_load(const char *name, void *buf, size_t size)
return 0; return 0;
} }
static inline size_t cbfs_unverified_area_load(const char *area, const char *name, void *buf,
size_t size)
{
if (_cbfs_unverified_area_load(area, name, buf, &size))
return size;
else
return 0;
}
static inline size_t cbfs_get_size(const char *name) static inline size_t cbfs_get_size(const char *name)
{ {
union cbfs_mdata mdata; union cbfs_mdata mdata;

View File

@ -4,6 +4,7 @@
#include <arch/virtual.h> #include <arch/virtual.h>
#include <assert.h> #include <assert.h>
#include <cbfs.h> #include <cbfs.h>
#include <cbfs_glue.h>
#include <commonlib/bsd/cbfs_private.h> #include <commonlib/bsd/cbfs_private.h>
#include <commonlib/bsd/fmap_serialized.h> #include <commonlib/bsd/fmap_serialized.h>
#include <libpayload.h> #include <libpayload.h>
@ -78,9 +79,9 @@ void cbfs_unmap(void *mapping)
} }
static bool cbfs_file_hash_mismatch(const void *buffer, size_t size, static bool cbfs_file_hash_mismatch(const void *buffer, size_t size,
const union cbfs_mdata *mdata) const union cbfs_mdata *mdata, bool skip_verification)
{ {
if (!CONFIG(LP_CBFS_VERIFICATION)) if (!CONFIG(LP_CBFS_VERIFICATION) || skip_verification)
return false; return false;
const struct vb2_hash *hash = cbfs_file_hash(mdata); const struct vb2_hash *hash = cbfs_file_hash(mdata);
@ -98,7 +99,7 @@ static bool cbfs_file_hash_mismatch(const void *buffer, size_t size,
static size_t cbfs_load_and_decompress(size_t offset, size_t in_size, void *buffer, static size_t cbfs_load_and_decompress(size_t offset, size_t in_size, void *buffer,
size_t buffer_size, uint32_t compression, size_t buffer_size, uint32_t compression,
const union cbfs_mdata *mdata) const union cbfs_mdata *mdata, bool skip_verification)
{ {
void *load = buffer; void *load = buffer;
size_t out_size = 0; size_t out_size = 0;
@ -119,7 +120,7 @@ static size_t cbfs_load_and_decompress(size_t offset, size_t in_size, void *buff
goto out; goto out;
} }
if (cbfs_file_hash_mismatch(buffer, in_size, mdata)) if (cbfs_file_hash_mismatch(buffer, in_size, mdata, skip_verification))
goto out; goto out;
switch (compression) { switch (compression) {
@ -146,46 +147,37 @@ out:
return out_size; return out_size;
} }
void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro) static void *do_load(union cbfs_mdata *mdata, ssize_t offset, void *buf, size_t *size_inout,
bool skip_verification)
{ {
ssize_t offset;
size_t out_size;
union cbfs_mdata mdata;
bool malloced = false; bool malloced = false;
size_t out_size;
DEBUG("%s(name='%s', buf=%p, force_ro=%s)\n", __func__, name, buf,
force_ro ? "true" : "false");
offset = _cbfs_boot_lookup(name, force_ro, &mdata);
if (offset < 0)
return NULL;
uint32_t compression = CBFS_COMPRESS_NONE; uint32_t compression = CBFS_COMPRESS_NONE;
const struct cbfs_file_attr_compression *cattr = const struct cbfs_file_attr_compression *cattr =
cbfs_find_attr(&mdata, CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr)); cbfs_find_attr(mdata, CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*cattr));
if (cattr) { if (cattr) {
compression = be32toh(cattr->compression); compression = be32toh(cattr->compression);
out_size = be32toh(cattr->decompressed_size); out_size = be32toh(cattr->decompressed_size);
} else { } else {
out_size = be32toh(mdata.h.len); out_size = be32toh(mdata->h.len);
} }
if (buf) { if (buf) {
if (!size_inout || *size_inout < out_size) { if (!size_inout || *size_inout < out_size) {
ERROR("'%s' buffer too small", mdata.h.filename); ERROR("'%s' buffer too small", mdata->h.filename);
return NULL; return NULL;
} }
} else { } else {
buf = malloc(out_size); buf = malloc(out_size);
if (!buf) { if (!buf) {
ERROR("'%s' allocation failure", mdata.h.filename); ERROR("'%s' allocation failure", mdata->h.filename);
return NULL; return NULL;
} }
malloced = true; malloced = true;
} }
if (cbfs_load_and_decompress(offset, be32toh(mdata.h.len), buf, out_size, compression, if (cbfs_load_and_decompress(offset, be32toh(mdata->h.len), buf, out_size, compression,
&mdata) mdata, skip_verification)
!= out_size) { != out_size) {
if (malloced) if (malloced)
free(buf); free(buf);
@ -196,3 +188,38 @@ void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro)
return buf; return buf;
} }
void *_cbfs_load(const char *name, void *buf, size_t *size_inout, bool force_ro)
{
ssize_t offset;
union cbfs_mdata mdata;
DEBUG("%s(name='%s', buf=%p, force_ro=%s)\n", __func__, name, buf,
force_ro ? "true" : "false");
offset = _cbfs_boot_lookup(name, force_ro, &mdata);
if (offset < 0)
return NULL;
return do_load(&mdata, offset, buf, size_inout, false);
}
void *_cbfs_unverified_area_load(const char *area, const char *name, void *buf,
size_t *size_inout)
{
struct cbfs_dev dev;
union cbfs_mdata mdata;
size_t data_offset;
DEBUG("%s(area='%s', name='%s', buf=%p)\n", __func__, area, name, buf);
if (fmap_locate_area(area, &dev.offset, &dev.size) != CB_SUCCESS)
return NULL;
if (cbfs_lookup(&dev, name, &mdata, &data_offset, NULL)) {
ERROR("'%s' not found in '%s'\n", name, area);
return NULL;
}
return do_load(&mdata, dev.offset + data_offset, buf, size_inout, true);
}

View File

@ -598,6 +598,92 @@ static void test_cbfs_boot_device_read_failure(void **state)
assert_null(mapping); assert_null(mapping);
} }
/* This test uses RW CBFS only */
static void test_cbfs_unverified_area_map(void **state)
{
struct cbfs_test_state *s = *state;
void *mapping = NULL;
size_t size_out = 0;
const struct cbfs_test_file *cbfs_files[] = {
&test_file_int_1, &test_file_2, NULL, &test_file_int_3,
&test_file_int_2, NULL, NULL, &test_file_1,
};
uint8_t *cbfs_buf = NULL;
size_t foffset = 0;
cbfs_buf = s->cbfs_rw_buf;
set_fmap_locate_area_results((size_t)cbfs_buf, s->cbfs_rw_size, CB_SUCCESS);
create_cbfs(cbfs_files, ARRAY_SIZE(cbfs_files), s->cbfs_rw_buf, s->cbfs_rw_size);
size_out = 0;
foffset = get_created_cbfs_file_start_offset(cbfs_files, 0);
expect_cbfs_lookup(TEST_DATA_INT_1_FILENAME, CB_SUCCESS,
(const union cbfs_mdata *)&cbfs_buf[foffset],
foffset + be32toh(test_file_int_1.header.offset));
will_return(cbfs_find_attr, NULL);
mapping = cbfs_unverified_area_map("TEST_AREA", TEST_DATA_INT_1_FILENAME, &size_out);
assert_non_null(mapping);
assert_int_equal(TEST_DATA_INT_1_SIZE, size_out);
assert_memory_equal(test_data_int_1, mapping, TEST_DATA_INT_1_SIZE);
cbfs_unmap(mapping);
size_out = 0;
foffset = get_created_cbfs_file_start_offset(cbfs_files, 1);
expect_cbfs_lookup(TEST_DATA_2_FILENAME, CB_SUCCESS,
(const union cbfs_mdata *)&cbfs_buf[foffset],
foffset + be32toh(test_file_2.header.offset));
will_return(cbfs_find_attr, &test_file_2.attrs_and_data);
expect_value(ulzman, srcn, TEST_DATA_2_SIZE);
expect_value(ulzman, dstn, TEST_DATA_2_SIZE);
mapping = cbfs_unverified_area_map("TEST_AREA", TEST_DATA_2_FILENAME, &size_out);
assert_non_null(mapping);
assert_int_equal(TEST_DATA_2_SIZE, size_out);
assert_memory_equal(test_data_2, mapping, TEST_DATA_2_SIZE);
cbfs_unmap(mapping);
size_out = 0;
foffset = get_created_cbfs_file_start_offset(cbfs_files, 3);
expect_cbfs_lookup(TEST_DATA_INT_3_FILENAME, CB_SUCCESS,
(const union cbfs_mdata *)&cbfs_buf[foffset],
foffset + be32toh(test_file_int_3.header.offset));
will_return(cbfs_find_attr, &test_file_int_3.attrs_and_data);
expect_value(ulz4fn, srcn, TEST_DATA_INT_3_SIZE);
expect_value(ulz4fn, dstn, TEST_DATA_INT_3_SIZE);
mapping = cbfs_unverified_area_map("TEST_AREA", TEST_DATA_INT_3_FILENAME, &size_out);
assert_non_null(mapping);
assert_int_equal(TEST_DATA_INT_3_SIZE, size_out);
assert_memory_equal(test_data_int_3, mapping, TEST_DATA_INT_3_SIZE);
cbfs_unmap(mapping);
size_out = 0;
foffset = get_created_cbfs_file_start_offset(cbfs_files, 4);
expect_cbfs_lookup(TEST_DATA_INT_2_FILENAME, CB_SUCCESS,
(const union cbfs_mdata *)&cbfs_buf[foffset],
foffset + be32toh(test_file_int_2.header.offset));
will_return(cbfs_find_attr, NULL);
mapping = cbfs_unverified_area_map("TEST_AREA", TEST_DATA_INT_2_FILENAME, &size_out);
assert_non_null(mapping);
assert_int_equal(TEST_DATA_INT_2_SIZE, size_out);
assert_memory_equal(test_data_int_2, mapping, TEST_DATA_INT_2_SIZE);
cbfs_unmap(mapping);
size_out = 0;
foffset = get_created_cbfs_file_start_offset(cbfs_files, 7);
expect_cbfs_lookup(TEST_DATA_1_FILENAME, CB_SUCCESS,
(const union cbfs_mdata *)&cbfs_buf[foffset],
foffset + be32toh(test_file_1.header.offset));
will_return(cbfs_find_attr, NULL);
mapping = cbfs_unverified_area_map("TEST_AREA", TEST_DATA_1_FILENAME, &size_out);
assert_non_null(mapping);
assert_int_equal(TEST_DATA_1_SIZE, size_out);
assert_memory_equal(test_data_1, mapping, TEST_DATA_1_SIZE);
cbfs_unmap(mapping);
size_out = 0;
expect_cbfs_lookup("invalid_file", CB_CBFS_NOT_FOUND, 0, 0);
mapping = cbfs_unverified_area_map("TEST_AREA", "invalid_file", &size_out);
assert_null(mapping);
}
#define TEST_CBFS_NAME_ALIGN_RO_RW(fn, test_name, enable_unaligned, enable_init_ro, \ #define TEST_CBFS_NAME_ALIGN_RO_RW(fn, test_name, enable_unaligned, enable_init_ro, \
enable_init_rw) \ enable_init_rw) \
@ -636,6 +722,7 @@ int main(void)
TEST_CBFS_LOOKUP(test_cbfs_load), TEST_CBFS_LOOKUP(test_cbfs_load),
TEST_CBFS_LOOKUP(test_cbfs_map_with_mcache), TEST_CBFS_LOOKUP(test_cbfs_map_with_mcache),
TEST_CBFS_LOOKUP(test_cbfs_boot_device_read_failure), TEST_CBFS_LOOKUP(test_cbfs_boot_device_read_failure),
TEST_CBFS_LOOKUP(test_cbfs_unverified_area_map),
}; };
return lp_run_group_tests(tests, NULL, NULL); return lp_run_group_tests(tests, NULL, NULL);