cbfstool: Allows mixed-state fmap regions to work

When using FMAP regions (with option -r) that were generated with a
master header (as done by cbfstool copy, eg. in Chrome OS' build
system), there were differences in interpretation of the master header's
fields.

Normalize for that by not sanity-checking the master header's size field
(there are enough other tests) and by dealing with region offsets
properly.

BUG=chromium:445938
BRANCH=tot
TEST=`cbfstool /build/veyron_minnie/firmware/image.dev.bin print -r
FW_MAIN_A` shows that region's directory (instead of claiming that
there's no CBFS at all, or showing an empty directory).

Change-Id: Ia840c823739d4ca144a7f861573d6d1b4113d799
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Commit-Id: 0e5364d291f45e4705e83c0331e128e35ab226d3
Original-Change-Id: Ie28edbf55ec56b7c78160000290ef3c57fda0f0e
Original-Signed-off-by: Patrick Georgi <pgeorgi@google.com>
Original-Reviewed-on: https://chromium-review.googlesource.com/312210
Original-Commit-Ready: Patrick Georgi <pgeorgi@chromium.org>
Original-Tested-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: http://review.coreboot.org/12416
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
This commit is contained in:
Patrick Georgi 2015-11-11 15:35:24 +01:00
parent 9b24f7ca9a
commit 7db2b6cacc
1 changed files with 13 additions and 8 deletions

View File

@ -1168,12 +1168,11 @@ int cbfs_walk(struct cbfs_image *image, cbfs_entry_callback callback,
return count; return count;
} }
static int cbfs_header_valid(struct cbfs_header *header, size_t size) static int cbfs_header_valid(struct cbfs_header *header)
{ {
if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) && if ((ntohl(header->magic) == CBFS_HEADER_MAGIC) &&
((ntohl(header->version) == CBFS_HEADER_VERSION1) || ((ntohl(header->version) == CBFS_HEADER_VERSION1) ||
(ntohl(header->version) == CBFS_HEADER_VERSION2)) && (ntohl(header->version) == CBFS_HEADER_VERSION2)) &&
(ntohl(header->romsize) <= size) &&
(ntohl(header->offset) < ntohl(header->romsize))) (ntohl(header->offset) < ntohl(header->romsize)))
return 1; return 1;
return 0; return 0;
@ -1190,7 +1189,7 @@ struct cbfs_header *cbfs_find_header(char *data, size_t size,
if (forced_offset < (size - sizeof(struct cbfs_header))) { if (forced_offset < (size - sizeof(struct cbfs_header))) {
/* Check if the forced header is valid. */ /* Check if the forced header is valid. */
header = (struct cbfs_header *)(data + forced_offset); header = (struct cbfs_header *)(data + forced_offset);
if (cbfs_header_valid(header, size)) if (cbfs_header_valid(header))
return header; return header;
return NULL; return NULL;
} }
@ -1202,7 +1201,7 @@ struct cbfs_header *cbfs_find_header(char *data, size_t size,
(size_t)rel_offset, (size_t)-rel_offset, offset); (size_t)rel_offset, (size_t)-rel_offset, offset);
if (offset >= size - sizeof(*header) || if (offset >= size - sizeof(*header) ||
!cbfs_header_valid((struct cbfs_header *)(data + offset), size)) { !cbfs_header_valid((struct cbfs_header *)(data + offset))) {
// Some use cases append non-CBFS data to the end of the ROM. // Some use cases append non-CBFS data to the end of the ROM.
DEBUG("relative offset seems wrong, scanning whole image...\n"); DEBUG("relative offset seems wrong, scanning whole image...\n");
offset = 0; offset = 0;
@ -1210,7 +1209,7 @@ struct cbfs_header *cbfs_find_header(char *data, size_t size,
for (; offset + sizeof(*header) < size; offset++) { for (; offset + sizeof(*header) < size; offset++) {
header = (struct cbfs_header *)(data + offset); header = (struct cbfs_header *)(data + offset);
if (!cbfs_header_valid(header, size)) if (!cbfs_header_valid(header))
continue; continue;
if (!found++) if (!found++)
result = header; result = header;
@ -1228,9 +1227,15 @@ struct cbfs_header *cbfs_find_header(char *data, size_t size,
struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image) struct cbfs_file *cbfs_find_first_entry(struct cbfs_image *image)
{ {
assert(image); assert(image);
return image->has_header ? (struct cbfs_file *)(image->buffer.data + if (image->has_header)
image->header.offset) : /* header.offset is relative to start of flash, not
(struct cbfs_file *)image->buffer.data; * start of region, so use it with the full image.
*/
return (struct cbfs_file *)
(buffer_get_original_backing(&image->buffer) +
image->header.offset);
else
return (struct cbfs_file *)buffer_get(&image->buffer);
} }
struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image, struct cbfs_file *cbfs_find_next_entry(struct cbfs_image *image,