From 295d58bda85ce30724a3fff87d60b323373f6e5f Mon Sep 17 00:00:00 2001 From: Aaron Durbin Date: Tue, 15 Dec 2015 13:33:51 -0600 Subject: 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 Reviewed-on: https://review.coreboot.org/12783 Tested-by: build bot (Jenkins) Reviewed-by: Martin Roth Reviewed-by: Patrick Georgi --- src/commonlib/Makefile.inc | 6 ++ src/commonlib/cbfs.c | 176 +++++++++++++++++++++++++++++++++ src/commonlib/include/commonlib/cbfs.h | 55 +++++++++++ 3 files changed, 237 insertions(+) create mode 100644 src/commonlib/cbfs.c create mode 100644 src/commonlib/include/commonlib/cbfs.h (limited to 'src/commonlib') diff --git a/src/commonlib/Makefile.inc b/src/commonlib/Makefile.inc index 929bf8edc0..19f9ba3b8d 100644 --- a/src/commonlib/Makefile.inc +++ b/src/commonlib/Makefile.inc @@ -10,3 +10,9 @@ ramstage-y += region.c smm-y += region.c ramstage-$(CONFIG_PLATFORM_USES_FSP1_1) += fsp1_1_relocate.c + +bootblock-y += cbfs.c +verstage-y += cbfs.c +romstage-y += cbfs.c +ramstage-y += cbfs.c +smm-y += cbfs.c 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 +#include +#include +#include +#include + +#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; +} diff --git a/src/commonlib/include/commonlib/cbfs.h b/src/commonlib/include/commonlib/cbfs.h new file mode 100644 index 0000000000..b0a468cdc9 --- /dev/null +++ b/src/commonlib/include/commonlib/cbfs.h @@ -0,0 +1,55 @@ +/* + * 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. + */ + +#ifndef _COMMONLIB_CBFS_H_ +#define _COMMONLIB_CBFS_H_ + +#include +#include + +/* Object representing cbfs files. */ +struct cbfsf { + struct region_device metadata; + struct region_device data; +}; + +/* Locate file by name and optional type. Returns 0 on succcess else < 0 on + * error.*/ +int cbfs_locate(struct cbfsf *fh, const struct region_device *cbfs, + const char *name, uint32_t *type); + +static inline void cbfs_file_data(struct region_device *data, + const struct cbfsf *file) +{ + rdev_chain(data, &file->data, 0, region_device_sz(&file->data)); +} + +static inline void cbfs_file_metadata(struct region_device *metadata, + const struct cbfsf *file) +{ + rdev_chain(metadata, &file->metadata, 0, + region_device_sz(&file->metadata)); +} + +/* + * Provide a handle to each cbfs file within a cbfs. The prev pointer represents + * the previous file (NULL on first invocation). The next object gets filled + * out with the next file. This returns < 0 on error, 0 on finding the next + * file, and > 0 at end of cbfs. + */ +int cbfs_for_each_file(const struct region_device *cbfs, + const struct cbfsf *prev, struct cbfsf *fh); + +#endif -- cgit v1.2.3