cbfs: Port cbfs_load() and cbfs_map() to new API

This patch adapts cbfs_load() and cbfs_map() to use the new CBFS API
directly, rather than through cbfs_boot_locate(). For cbfs_load() this
means that attribute metadata does not need to be read twice.

Change-Id: I754cc34b1c1471129e15475aa0f1891e02439a02
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/39305
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Aaron Durbin <adurbin@chromium.org>
This commit is contained in:
Julius Werner 2020-10-01 18:25:49 -07:00 committed by Patrick Georgi
parent 834b3ecd7c
commit d17ce41e29
3 changed files with 55 additions and 18 deletions

View File

@ -159,3 +159,35 @@ cb_err_t cbfs_lookup(cbfs_dev_t dev, const char *name, union cbfs_mdata *mdata_o
}; };
return cbfs_walk(dev, lookup_walker, &args, metadata_hash, 0); return cbfs_walk(dev, lookup_walker, &args, metadata_hash, 0);
} }
const void *cbfs_find_attr(const union cbfs_mdata *mdata, uint32_t attr_tag, size_t size_check)
{
uint32_t offset = be32toh(mdata->h.attributes_offset);
uint32_t end = be32toh(mdata->h.offset);
if (!offset)
return NULL;
while (offset + sizeof(struct cbfs_file_attribute) <= end) {
const struct cbfs_file_attribute *attr = (const void *)mdata->raw + offset;
const uint32_t tag = be32toh(attr->tag);
const uint32_t len = be32toh(attr->len);
if (offset + len > end) {
ERROR("Attribute %s[%u] overflows end of metadata\n",
mdata->filename, tag);
return NULL;
}
if (tag == attr_tag) {
if (size_check && len != size_check) {
ERROR("Attribute %s[%u] size mismatch: %u != %zu\n",
mdata->filename, tag, len, size_check);
return NULL;
}
return attr;
}
offset += len;
}
return NULL;
}

View File

@ -134,4 +134,9 @@ cb_err_t cbfs_mcache_lookup(const void *mcache, size_t mcache_size, const char *
/* Returns the amount of bytes actually used by the CBFS metadata cache in |mcache|. */ /* Returns the amount of bytes actually used by the CBFS metadata cache in |mcache|. */
size_t cbfs_mcache_real_size(const void *mcache, size_t mcache_size); size_t cbfs_mcache_real_size(const void *mcache, size_t mcache_size);
/* Finds a CBFS attribute in a metadata block. Attribute returned as-is (still big-endian).
If |size| is not 0, will check that it matches the length of the attribute (if found)...
else caller is responsible for checking the |len| field to avoid reading out-of-bounds. */
const void *cbfs_find_attr(const union cbfs_mdata *mdata, uint32_t attr_tag, size_t size_check);
#endif /* _COMMONLIB_BSD_CBFS_PRIVATE_H_ */ #endif /* _COMMONLIB_BSD_CBFS_PRIVATE_H_ */

View File

@ -72,18 +72,16 @@ int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
void *cbfs_map(const char *name, size_t *size_out) void *cbfs_map(const char *name, size_t *size_out)
{ {
struct cbfsf fh; struct region_device rdev;
size_t fsize; union cbfs_mdata mdata;
if (cbfs_boot_locate(&fh, name, NULL)) if (cbfs_boot_lookup(name, false, &mdata, &rdev))
return NULL; return NULL;
fsize = region_device_sz(&fh.data);
if (size_out != NULL) if (size_out != NULL)
*size_out = fsize; *size_out = region_device_sz(&rdev);
return rdev_mmap(&fh.data, 0, fsize); return rdev_mmap_full(&rdev);
} }
int cbfs_unmap(void *mapping) int cbfs_unmap(void *mapping)
@ -285,21 +283,23 @@ void *cbfs_boot_map_optionrom_revision(uint16_t vendor, uint16_t device, uint8_t
size_t cbfs_load(const char *name, void *buf, size_t buf_size) size_t cbfs_load(const char *name, void *buf, size_t buf_size)
{ {
struct cbfsf fh; struct region_device rdev;
uint32_t compression_algo; union cbfs_mdata mdata;
size_t decompressed_size;
if (cbfs_boot_locate(&fh, name, NULL) < 0) if (cbfs_boot_lookup(name, false, &mdata, &rdev))
return 0; return 0;
if (cbfsf_decompression_info(&fh, &compression_algo, uint32_t compression = CBFS_COMPRESS_NONE;
&decompressed_size) const struct cbfs_file_attr_compression *attr = cbfs_find_attr(&mdata,
< 0 CBFS_FILE_ATTR_TAG_COMPRESSION, sizeof(*attr));
|| decompressed_size > buf_size) if (attr) {
compression = be32toh(attr->compression);
if (buf_size < be32toh(attr->decompressed_size))
return 0; return 0;
}
return cbfs_load_and_decompress(&fh.data, 0, region_device_sz(&fh.data), return cbfs_load_and_decompress(&rdev, 0, region_device_sz(&rdev),
buf, buf_size, compression_algo); buf, buf_size, compression);
} }
int cbfs_prog_stage_load(struct prog *pstage) int cbfs_prog_stage_load(struct prog *pstage)