diff options
author | Aaron Durbin <adurbin@chromium.org> | 2015-12-15 13:33:51 -0600 |
---|---|---|
committer | Aaron Durbin <adurbin@chromium.org> | 2016-01-06 01:11:19 +0100 |
commit | 295d58bda85ce30724a3fff87d60b323373f6e5f (patch) | |
tree | 7bdd1cf6a886bc11ab313d9b235aa5cb00bdaf8d /src/commonlib/cbfs.c | |
parent | 990ab7efe5d0eddb96542042bf5106811365e34a (diff) |
commonlib: Add common cbfs parsing logic to coreboot and cbfstool.
To continue sharing more code between the tools and
coreboot proper provide cbfs parsing logic in commonlib.
A cbfs_for_each_file() function was added to allow
one to act on each file found within a cbfs. cbfs_locate()
was updated to use that logic.
BUG=chrome-os-partner:48412
BUG=chromium:445938
BRANCH=None
TEST=Utilized and booted on glados.
Change-Id: I1f23841583e78dc3686f106de9eafe1adbef8c9f
Signed-off-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: https://review.coreboot.org/12783
Tested-by: build bot (Jenkins)
Reviewed-by: Martin Roth <martinroth@google.com>
Reviewed-by: Patrick Georgi <pgeorgi@google.com>
Diffstat (limited to 'src/commonlib/cbfs.c')
-rw-r--r-- | src/commonlib/cbfs.c | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/src/commonlib/cbfs.c b/src/commonlib/cbfs.c new file mode 100644 index 0000000000..06d31ed71e --- /dev/null +++ b/src/commonlib/cbfs.c @@ -0,0 +1,176 @@ +/* + * This file is part of the coreboot project. + * + * Copyright 2015 Google Inc. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include <console/console.h> +#include <commonlib/cbfs.h> +#include <commonlib/endian.h> +#include <commonlib/helpers.h> +#include <string.h> + +#if !defined(ERROR) +#define ERROR(x...) printk(BIOS_ERR, "CBFS: " x) +#endif +#if !defined(LOG) +#define LOG(x...) printk(BIOS_INFO, "CBFS: " x) +#endif +#if defined(IS_ENABLED) + +#if IS_ENABLED(CONFIG_DEBUG_CBFS) +#define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x) +#else +#define DEBUG(x...) +#endif + +#elif !defined(DEBUG) +#define DEBUG(x...) +#endif + +static size_t cbfs_next_offset(const struct region_device *cbfs, + const struct cbfsf *f) +{ + size_t offset; + + if (f == NULL) + return 0; + + /* The region_device objects store absolute offets over the whole + * region. Therefore a relative offset needs to be calculated. */ + offset = rdev_relative_offset(cbfs, &f->data); + offset += region_device_sz(&f->data); + + return ALIGN_UP(offset, CBFS_ALIGNMENT); +} + +static int cbfs_end(const struct region_device *cbfs, size_t offset) +{ + if (offset >= region_device_sz(cbfs)) + return 1; + + return 0; +} + +int cbfs_for_each_file(const struct region_device *cbfs, + const struct cbfsf *prev, struct cbfsf *fh) +{ + size_t offset; + + offset = cbfs_next_offset(cbfs, prev); + + /* Try to scan the entire cbfs region looking for file name. */ + while (1) { + struct cbfs_file file; + const size_t fsz = sizeof(file); + + DEBUG("Checking offset %zx\n", offset); + + /* End of region. */ + if (cbfs_end(cbfs, offset)) + return 1; + + /* Can't read file. Nothing else to do but bail out. */ + if (rdev_readat(cbfs, &file, offset, fsz) != fsz) + break; + + if (memcmp(file.magic, CBFS_FILE_MAGIC, sizeof(file.magic))) { + offset++; + offset = ALIGN_UP(offset, CBFS_ALIGNMENT); + continue; + } + + file.len = read_be32(&file.len); + file.offset = read_be32(&file.offset); + + DEBUG("File @ offset %zx size %x\n", offset, file.len); + + /* Keep track of both the metadata and the data for the file. */ + if (rdev_chain(&fh->metadata, cbfs, offset, file.offset)) + break; + + if (rdev_chain(&fh->data, cbfs, offset + file.offset, file.len)) + break; + + /* Success. */ + return 0; + } + + return -1; +} + +int cbfs_locate(struct cbfsf *fh, const struct region_device *cbfs, + const char *name, uint32_t *type) +{ + struct cbfsf *prev; + + LOG("Locating '%s'\n", name); + + prev = NULL; + + while (1) { + int ret; + char *fname; + int name_match; + const size_t fsz = sizeof(struct cbfs_file); + + ret = cbfs_for_each_file(cbfs, prev, fh); + prev = fh; + + /* Either failed to read or hit the end of the region. */ + if (ret < 0 || ret > 0) + break; + + fname = rdev_mmap(&fh->metadata, fsz, + region_device_sz(&fh->metadata) - fsz); + + if (fname == NULL) + break; + + name_match = !strcmp(fname, name); + rdev_munmap(&fh->metadata, fname); + + if (!name_match) { + DEBUG(" Unmatched '%s' at %zx\n", fname, + rdev_relative_offset(cbfs, &fh->metadata)); + continue; + } + + if (type != NULL) { + uint32_t ftype; + + if (rdev_readat(&fh->metadata, &ftype, + offsetof(struct cbfs_file, type), + sizeof(ftype)) != sizeof(ftype)) + break; + + ftype = read_be32(&ftype); + + if (*type != ftype) { + DEBUG(" Unmatched type %x at %zx\n", ftype, + rdev_relative_offset(cbfs, + &fh->metadata)); + continue; + } + } + + LOG("Found @ offset %zx size %zx\n", + rdev_relative_offset(cbfs, &fh->metadata), + region_device_sz(&fh->data)); + + /* Success. */ + return 0; + } + + LOG("'%s' not found.\n", name); + return -1; +} |