Noé Rubinstein (noe.rubinstein@gmail.com) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/285
-gerrit
commit 04c13d9b86c0b2bd50f45d328cb5f914e7e13c85 Author: Noe Rubinstein nrubinstein@avencall.com Date: Tue Oct 11 19:13:49 2011 +0200
cbfstool: Unify cbfs_find_location
Change-Id: I7db964d6211d9c6646eeed31bdeb9c204bd44b9a Signed-off-by: Noe Rubinstein nrubinstein@avencall.com --- util/cbfstool/cbfstool.c | 5 +-- util/cbfstool/common.c | 73 +++++++++++++-------------------------------- 2 files changed, 23 insertions(+), 55 deletions(-)
diff --git a/util/cbfstool/cbfstool.c b/util/cbfstool/cbfstool.c index 530712b..75edcda 100644 --- a/util/cbfstool/cbfstool.c +++ b/util/cbfstool/cbfstool.c @@ -251,11 +251,10 @@ static int cbfs_locate(int argc, char **argv) const char *filename = argv[4]; int align = strtoul(argv[5], NULL, 0); uint32_t offset = argc > 6 ? strtoul(argv[6], NULL, 0) : 0; - uint32_t loc = cbfs_find_stage_location(romname, filesize, filename, align, - offset); + uint32_t loc = cbfs_find_stage_location(romname, filesize, filename, + align, offset);
printf("%x\n", loc); - fprintf(stderr, "%x\n", loc); return 0; }
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c index 09fe5bb..80b293a 100644 --- a/util/cbfstool/common.c +++ b/util/cbfstool/common.c @@ -436,16 +436,18 @@ static int in_segment(int addr, int size, int gran) return ((addr & ~(gran - 1)) == ((addr + size) & ~(gran - 1))); }
-uint32_t cbfs_find_stage_location(const char *romfile, uint32_t filesize, - const char *filename, uint32_t alignment, - uint32_t offset) +static uint32_t cbfs_find_location_(const char *romfile, uint32_t filesize, + const char *filename, uint32_t offset, + int is_stage, uint32_t alignment) { void *rom = loadrom(romfile); int filename_size = strlen(filename);
int headersize = - sizeof(struct cbfs_file) + ALIGN(filename_size + 1, - 16) + sizeof(struct cbfs_stage); + sizeof(struct cbfs_file) + + ALIGN(filename_size + 1, 16) + + (is_stage ? sizeof(struct cbfs_stage) : 0); + int totalsize = headersize + filesize;
if (offset > phys_end - phys_start) { @@ -479,6 +481,8 @@ uint32_t cbfs_find_stage_location(const char *romfile, uint32_t filesize, && (ntohl(thisfile->len) + ntohl(thisfile->offset) - off_rel >= totalsize)) {
+ if (!is_stage) return current + headersize; + if (in_segment (current + headersize, filesize, alignment)) return current + headersize; @@ -496,9 +500,9 @@ uint32_t cbfs_find_stage_location(const char *romfile, uint32_t filesize, filesize, alignment)) return ALIGN(current, alignment) + alignment; } - current = - ALIGN(current + ntohl(thisfile->len) + - ntohl(thisfile->offset), align); + current = ALIGN(current + (ntohl(thisfile->len) + + ntohl(thisfile->offset) - off_rel), + align); } return 0; } @@ -506,49 +510,14 @@ uint32_t cbfs_find_stage_location(const char *romfile, uint32_t filesize, uint32_t cbfs_find_location(const char *romfile, uint32_t filesize, const char *filename, uint32_t offset) { - void *rom = loadrom(romfile); - int filename_size = strlen(filename); - - int headersize = - sizeof(struct cbfs_file) + ALIGN(filename_size + 1, 16); - - int totalsize = headersize + filesize; - - if (offset > phys_end - phys_start) { - fprintf(stderr, "Offset outside of ROM boundaries\n"); - exit(1); - } - - if (offset) offset = ALIGN(phys_start + offset, align); - - uint32_t current = phys_start; - while (current < phys_end) { - if (!cbfs_file_header(current)) { - current += align; - continue; - } - struct cbfs_file *thisfile = - (struct cbfs_file *)phys_to_virt(current); - - uint32_t top = - current + ntohl(thisfile->len) + ntohl(thisfile->offset); - - uint32_t off_rel = 0; - if (current < offset && offset <= top) { - off_rel = offset - current; - current = offset; - } - - if (current >= offset - && ((ntohl(thisfile->type) == 0x0) - || (ntohl(thisfile->type) == 0xffffffff)) - && (ntohl(thisfile->len) + ntohl(thisfile->offset) - - off_rel >= totalsize)) - return current + headersize; + return cbfs_find_location_(romfile, filesize, filename, offset, 0, + 0); +}
- current = ALIGN(current + (ntohl(thisfile->len) + - ntohl(thisfile->offset) - off_rel), - align); - } - return 0; +uint32_t cbfs_find_stage_location(const char *romfile, uint32_t filesize, + const char *filename, uint32_t alignment, + uint32_t offset) +{ + return cbfs_find_location_(romfile, filesize, filename, offset, 1, + alignment); }