diff options
-rw-r--r-- | src/include/cbfs.h | 72 | ||||
-rw-r--r-- | src/include/cbfs_private.h | 23 | ||||
-rw-r--r-- | src/lib/cbfs.c | 16 |
3 files changed, 80 insertions, 31 deletions
diff --git a/src/include/cbfs.h b/src/include/cbfs.h index f6309a3e30..43d8123454 100644 --- a/src/include/cbfs.h +++ b/src/include/cbfs.h @@ -7,6 +7,7 @@ #include <commonlib/bsd/cbfs_mdata.h> #include <commonlib/cbfs.h> #include <commonlib/mem_pool.h> +#include <commonlib/region.h> #include <program_loading.h> #include <types.h> #include <vb2_sha.h> @@ -119,6 +120,20 @@ void cbfs_unmap(void *mapping); /* Load stage into memory filling in prog. Return 0 on success. < 0 on error. */ int cbfs_prog_stage_load(struct prog *prog); +/* Returns the size of a CBFS file, or 0 on error. Avoid using this function to allocate space, + and instead use cbfs_alloc() so the file only needs to be looked up once. */ +static inline size_t cbfs_get_size(const char *name); +static inline size_t cbfs_ro_get_size(const char *name); + +/* Returns the type of a CBFS file, or CBFS_TYPE_NULL on error. Use cbfs_type_load() instead of + this where possible to avoid looking up the file more than once. */ +static inline enum cbfs_type cbfs_get_type(const char *name); +static inline enum cbfs_type cbfs_ro_get_type(const char *name); + +/* Check whether a CBFS file exists. */ +static inline bool cbfs_file_exists(const char *name); +static inline bool cbfs_ro_file_exists(const char *name); + /********************************************************************************************** * BOOT DEVICE HELPER APIs * @@ -173,6 +188,9 @@ int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name, /********************************************************************************************** * INTERNAL HELPERS FOR INLINES, DO NOT USE. * **********************************************************************************************/ +cb_err_t _cbfs_boot_lookup(const char *name, bool force_ro, + union cbfs_mdata *mdata, struct region_device *rdev); + void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg, size_t *size_out, bool force_ro, enum cbfs_type *type); @@ -287,4 +305,58 @@ static inline void *cbfs_ro_type_cbmem_alloc(const char *name, uint32_t cbmem_id size_out, type); } +static inline size_t cbfs_get_size(const char *name) +{ + union cbfs_mdata mdata; + struct region_device rdev; + if (_cbfs_boot_lookup(name, false, &mdata, &rdev) != CB_SUCCESS) + return 0; + return be32toh(mdata.h.len); +} + +static inline size_t cbfs_ro_get_size(const char *name) +{ + union cbfs_mdata mdata; + struct region_device rdev; + if (_cbfs_boot_lookup(name, true, &mdata, &rdev) != CB_SUCCESS) + return 0; + return be32toh(mdata.h.len); +} + +static inline enum cbfs_type cbfs_get_type(const char *name) +{ + union cbfs_mdata mdata; + struct region_device rdev; + if (_cbfs_boot_lookup(name, false, &mdata, &rdev) != CB_SUCCESS) + return CBFS_TYPE_NULL; + return be32toh(mdata.h.type); +} + +static inline enum cbfs_type cbfs_ro_get_type(const char *name) +{ + union cbfs_mdata mdata; + struct region_device rdev; + if (_cbfs_boot_lookup(name, true, &mdata, &rdev) != CB_SUCCESS) + return CBFS_TYPE_NULL; + return be32toh(mdata.h.type); +} + +static inline bool cbfs_file_exists(const char *name) +{ + union cbfs_mdata mdata; + struct region_device rdev; + if (_cbfs_boot_lookup(name, false, &mdata, &rdev) != CB_SUCCESS) + return false; + return true; +} + +static inline bool cbfs_ro_file_exists(const char *name) +{ + union cbfs_mdata mdata; + struct region_device rdev; + if (_cbfs_boot_lookup(name, true, &mdata, &rdev) != CB_SUCCESS) + return false; + return true; +} + #endif diff --git a/src/include/cbfs_private.h b/src/include/cbfs_private.h deleted file mode 100644 index 8e9803616f..0000000000 --- a/src/include/cbfs_private.h +++ /dev/null @@ -1,23 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-only */ - -#ifndef _CBFS_PRIVATE_H_ -#define _CBFS_PRIVATE_H_ - -#include <commonlib/bsd/cbfs_private.h> -#include <commonlib/region.h> -#include <types.h> - -/* - * This header contains low-level CBFS APIs that should only be used by code - * that really needs this level of access. Most code (particularly platform - * code) should use the higher-level CBFS APIs in <cbfs.h>. Code using these - * APIs needs to take special care to ensure CBFS file data is verified (in a - * TOCTOU-safe manner) before access (TODO: add details on how to do this once - * file verification code is in). - */ - -/* Find by name, load metadata into |mdata| and chain file data to |rdev|. */ -cb_err_t cbfs_boot_lookup(const char *name, bool force_ro, - union cbfs_mdata *mdata, struct region_device *rdev); - -#endif diff --git a/src/lib/cbfs.c b/src/lib/cbfs.c index dbb3b1a913..1ffc69508a 100644 --- a/src/lib/cbfs.c +++ b/src/lib/cbfs.c @@ -3,8 +3,8 @@ #include <assert.h> #include <boot_device.h> #include <cbfs.h> -#include <cbfs_private.h> #include <cbmem.h> +#include <commonlib/bsd/cbfs_private.h> #include <commonlib/bsd/compression.h> #include <commonlib/endian.h> #include <console/console.h> @@ -35,8 +35,8 @@ static void switch_to_postram_cache(int unused) } ROMSTAGE_CBMEM_INIT_HOOK(switch_to_postram_cache); -cb_err_t cbfs_boot_lookup(const char *name, bool force_ro, - union cbfs_mdata *mdata, struct region_device *rdev) +cb_err_t _cbfs_boot_lookup(const char *name, bool force_ro, + union cbfs_mdata *mdata, struct region_device *rdev) { const struct cbfs_boot_device *cbd = cbfs_get_boot_device(force_ro); if (!cbd) @@ -65,7 +65,7 @@ cb_err_t cbfs_boot_lookup(const char *name, bool force_ro, if (CONFIG(VBOOT_ENABLE_CBFS_FALLBACK) && !force_ro && err == CB_CBFS_NOT_FOUND) { printk(BIOS_INFO, "CBFS: Fall back to RO region for %s\n", name); - return cbfs_boot_lookup(name, true, mdata, rdev); + return _cbfs_boot_lookup(name, true, mdata, rdev); } if (err) { if (err == CB_CBFS_NOT_FOUND) @@ -90,7 +90,7 @@ cb_err_t cbfs_boot_lookup(const char *name, bool force_ro, int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type) { - if (cbfs_boot_lookup(name, false, &fh->mdata, &fh->data)) + if (_cbfs_boot_lookup(name, false, &fh->mdata, &fh->data)) return -1; size_t msize = be32toh(fh->mdata.h.offset); @@ -330,7 +330,7 @@ void cbfs_preload(const char *name) DEBUG("%s(name='%s')\n", __func__, name); - if (cbfs_boot_lookup(name, force_ro, &mdata, &rdev)) + if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev)) return; size = region_device_sz(&rdev); @@ -422,7 +422,7 @@ void *_cbfs_alloc(const char *name, cbfs_allocator_t allocator, void *arg, DEBUG("%s(name='%s', alloc=%p(%p), force_ro=%s, type=%d)\n", __func__, name, allocator, arg, force_ro ? "true" : "false", type ? *type : -1); - if (cbfs_boot_lookup(name, force_ro, &mdata, &rdev)) + if (_cbfs_boot_lookup(name, force_ro, &mdata, &rdev)) return NULL; if (type) { @@ -525,7 +525,7 @@ cb_err_t cbfs_prog_stage_load(struct prog *pstage) prog_locate_hook(pstage); - if ((err = cbfs_boot_lookup(prog_name(pstage), false, &mdata, &rdev))) + if ((err = _cbfs_boot_lookup(prog_name(pstage), false, &mdata, &rdev))) return err; assert(be32toh(mdata.h.type) == CBFS_TYPE_STAGE); |