Stefan Reinauer has submitted this change. ( https://review.coreboot.org/c/coreboot/+/77095?usp=email )
Change subject: libpayload/libcbfs: Support file type in unverified_area APIs ......................................................................
libpayload/libcbfs: Support file type in unverified_area APIs
These APIs do not currently support obtaining the file type from CBFS. This is important since it is difficult to run a payload without it.
Expand the APIs to include this.
Note: we might consider reducing the number of combinations, since there is not a lot of usage of some of these functions.
BUG=b:270062696 BRANCH=none TEST=build for Brya
Change-Id: I286addab7c435a47e5d22161be84e9fc559de4b9 Signed-off-by: Simon Glass sjg@chromium.org Reviewed-on: https://review.coreboot.org/c/coreboot/+/77095 Reviewed-by: Julius Werner jwerner@chromium.org Reviewed-by: Stefan Reinauer stefan.reinauer@coreboot.org Tested-by: build bot (Jenkins) no-reply@coreboot.org --- M src/include/cbfs.h M src/lib/cbfs.c 2 files changed, 73 insertions(+), 22 deletions(-)
Approvals: Julius Werner: Looks good to me, approved build bot (Jenkins): Verified Stefan Reinauer: Looks good to me, approved
diff --git a/src/include/cbfs.h b/src/include/cbfs.h index f0ae7a8..a10efb7 100644 --- a/src/include/cbfs.h +++ b/src/include/cbfs.h @@ -83,6 +83,8 @@ enum cbfs_type *type); static inline size_t cbfs_unverified_area_load(const char *area, const char *name, void *buf, size_t size); +static inline size_t cbfs_unverified_area_type_load(const char *area, const char *name, + void *buf, size_t size, enum cbfs_type *type);
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); @@ -90,6 +92,8 @@ static inline void *cbfs_ro_type_map(const char *name, size_t *size_out, enum cbfs_type *type); static inline void *cbfs_unverified_area_map(const char *area, const char *name, size_t *size_out); +static inline void *cbfs_unverified_area_type_map(const char *area, const char *name, + size_t *size_out, enum cbfs_type *type);
static inline void *cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg, size_t *size_out); @@ -102,6 +106,9 @@ static inline void *cbfs_unverified_area_alloc(const char *area, const char *name, cbfs_allocator_t allocator, void *arg, size_t *size_out); +static inline void *cbfs_unverified_area_type_alloc(const char *area, const char *name, + cbfs_allocator_t allocator, void *arg, + size_t *size_out, enum cbfs_type *type);
static inline void *cbfs_cbmem_alloc(const char *name, uint32_t cbmem_id, size_t *size_out); static inline void *cbfs_ro_cbmem_alloc(const char *name, uint32_t cbmem_id, size_t *size_out); @@ -198,8 +205,9 @@ void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg, size_t *size_out, bool force_ro, enum cbfs_type *type);
-void *_cbfs_unverified_area_alloc(const char *area, const char *name, - cbfs_allocator_t allocator, void *arg, size_t *size_out); +void *_cbfs_unverified_area_type_alloc(const char *area, const char *name, + cbfs_allocator_t allocator, void *arg, size_t *size_out, + enum cbfs_type *type);
struct _cbfs_default_allocator_arg { void *buf; @@ -240,7 +248,14 @@ cbfs_allocator_t allocator, void *arg, size_t *size_out) { - return _cbfs_unverified_area_alloc(area, name, allocator, arg, size_out); + return _cbfs_unverified_area_type_alloc(area, name, allocator, arg, size_out, NULL); +} + +static inline void *cbfs_unverified_area_type_alloc(const char *area, const char *name, + cbfs_allocator_t allocator, void *arg, + size_t *size_out, enum cbfs_type *type) +{ + return _cbfs_unverified_area_type_alloc(area, name, allocator, arg, size_out, type); }
static inline void *cbfs_map(const char *name, size_t *size_out) @@ -263,10 +278,16 @@ return cbfs_ro_type_alloc(name, NULL, NULL, size_out, type); }
+static inline void *cbfs_unverified_area_type_map(const char *area, const char *name, + size_t *size_out, enum cbfs_type *type) +{ + return _cbfs_unverified_area_type_alloc(area, name, NULL, NULL, size_out, type); +} + static inline void *cbfs_unverified_area_map(const char *area, const char *name, size_t *size_out) { - return _cbfs_unverified_area_alloc(area, name, NULL, NULL, size_out); + return cbfs_unverified_area_type_map(area, name, size_out, NULL); }
static inline size_t _cbfs_load(const char *name, void *buf, size_t size, bool force_ro, @@ -301,16 +322,23 @@ return _cbfs_load(name, buf, size, true, type); }
-static inline size_t cbfs_unverified_area_load(const char *area, const char *name, - void *buf, size_t size) +static inline size_t cbfs_unverified_area_type_load(const char *area, const char *name, + void *buf, size_t size, enum cbfs_type *type) { struct _cbfs_default_allocator_arg arg = { .buf = buf, .buf_size = size }; - if (_cbfs_unverified_area_alloc(area, name, _cbfs_default_allocator, &arg, &size)) + if (_cbfs_unverified_area_type_alloc(area, name, _cbfs_default_allocator, &arg, &size, + type)) return size; else return 0; }
+static inline size_t cbfs_unverified_area_load(const char *area, const char *name, + void *buf, size_t size) +{ + return cbfs_unverified_area_type_load(area, name, buf, size, NULL); +} + static inline void *cbfs_cbmem_alloc(const char *name, uint32_t cbmem_id, size_t *size_out) { return cbfs_type_cbmem_alloc(name, cbmem_id, size_out, NULL); @@ -338,8 +366,8 @@ static inline void *cbfs_unverified_area_cbmem_alloc(const char *area, const char *name, uint32_t cbmem_id, size_t *size_out) { - return _cbfs_unverified_area_alloc(area, name, _cbfs_cbmem_allocator, - (void *)(uintptr_t)cbmem_id, size_out); + return _cbfs_unverified_area_type_alloc(area, name, _cbfs_cbmem_allocator, + (void *)(uintptr_t)cbmem_id, size_out, NULL); }
static inline size_t cbfs_get_size(const char *name) diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c index 78eeb3b..aa42961 100644 --- a/src/lib/cbfs.c +++ b/src/lib/cbfs.c @@ -467,6 +467,32 @@ return loc; }
+/** + * type_mismatch() - Check if any provided type matches the metadata + * + * @mdata: Metadata to check + * @type: NULL if any type is acceptable, CBFS_TYPE_QUERY to query the type + * and update *type, otherwise check *type against metadata and produce an + * error if they don't match + * Return: true if OK, false if there is a type mismatch + */ +static bool type_mismatch(union cbfs_mdata *mdata, enum cbfs_type *type) +{ + const enum cbfs_type real_type = be32toh(mdata->h.type); + + if (!type) + ; /* nothing to check */ + else if (*type == CBFS_TYPE_QUERY) + *type = real_type; + else if (*type != real_type) { + ERROR("'%s' type mismatch (is %u, expected %u)\n", + mdata->h.filename, real_type, *type); + return true; + } + + return false; +} + void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg, size_t *size_out, bool force_ro, enum cbfs_type *type) { @@ -480,16 +506,8 @@ if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev)) return NULL;
- if (type) { - const enum cbfs_type real_type = be32toh(mdata.h.type); - if (*type == CBFS_TYPE_QUERY) - *type = real_type; - else if (*type != real_type) { - ERROR("'%s' type mismatch (is %u, expected %u)\n", - mdata.h.filename, real_type, *type); - return NULL; - } - } + if (type_mismatch(&mdata, type)) + return NULL;
/* Update the rdev with the preload content */ if (!force_ro && get_preload_rdev(&rdev, name) == CB_SUCCESS) @@ -505,14 +523,16 @@ return ret; }
-void *_cbfs_unverified_area_alloc(const char *area, const char *name, - cbfs_allocator_t allocator, void *arg, size_t *size_out) +void *_cbfs_unverified_area_type_alloc(const char *area, const char *name, + cbfs_allocator_t allocator, void *arg, size_t *size_out, + enum cbfs_type *type) { struct region_device area_rdev, file_rdev; union cbfs_mdata mdata; size_t data_offset;
- DEBUG("%s(area='%s', name='%s', alloc=%p(%p))\n", __func__, area, name, allocator, arg); + DEBUG("%s(area='%s', name='%s', alloc=%p(%p), type=%d)\n", __func__, + area, name, allocator, arg, type ? *type : -1);
if (fmap_locate_area_as_rdev(area, &area_rdev)) return NULL; @@ -525,6 +545,9 @@ if (rdev_chain(&file_rdev, &area_rdev, data_offset, be32toh(mdata.h.len))) return NULL;
+ if (type_mismatch(&mdata, type)) + return NULL; + return do_alloc(&mdata, &file_rdev, allocator, arg, size_out, true); }