diff options
author | Stefan Reinauer <reinauer@chromium.org> | 2012-10-29 16:52:36 -0700 |
---|---|---|
committer | Stefan Reinauer <stefan.reinauer@coreboot.org> | 2012-11-12 18:38:03 +0100 |
commit | 632175802e3d6c3265aa6f511a5aa400d00953d1 (patch) | |
tree | c4d8656a9293eec0b8bd87ee4a541c346ca668d6 /util/cbfstool/common.c | |
parent | e5a0a5d6df99eb78fbf6469eff35e6d415ec2d54 (diff) |
cbfstool: Rework to use getopt style parameters
- Adding more and more optional and non-optional parameters
bloated cbfstool and made the code hard to read with a lot
of parsing in the actual cbfs handling functions. This change
switches over to use getopt style options for everything but
command and cbfs file name.
- This allows us to simplify the coreboot Makefiles a bit
- Also, add guards to include files
- Fix some 80+ character lines
- Add more detailed error reporting
- Free memory we're allocating
Signed-off-by: Stefan Reinauer <reinauer@google.com>
Change-Id: Ia9137942deb8d26bbb30068e6de72466afe9b0a7
Reviewed-on: http://review.coreboot.org/1800
Tested-by: build bot (Jenkins)
Reviewed-by: Ronald G. Minnich <rminnich@gmail.com>
Diffstat (limited to 'util/cbfstool/common.c')
-rw-r--r-- | util/cbfstool/common.c | 95 |
1 files changed, 66 insertions, 29 deletions
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c index 23f3ecfa87..6c67c396f4 100644 --- a/util/cbfstool/common.c +++ b/util/cbfstool/common.c @@ -28,10 +28,13 @@ #define dprintf(x...) -uint32_t getfilesize(const char *filename) +size_t getfilesize(const char *filename) { - uint32_t size; + size_t size; FILE *file = fopen(filename, "rb"); + if (file == NULL) + return -1; + fseek(file, 0, SEEK_END); size = ftell(file); fclose(file); @@ -44,6 +47,7 @@ void *loadfile(const char *filename, uint32_t * romsize_p, void *content, FILE *file = fopen(filename, "rb"); if (file == NULL) return NULL; + fseek(file, 0, SEEK_END); *romsize_p = ftell(file); fseek(file, 0, SEEK_SET); @@ -58,15 +62,16 @@ void *loadfile(const char *filename, uint32_t * romsize_p, void *content, content -= *romsize_p; if (!fread(content, *romsize_p, 1, file)) { - printf("failed to read %s\n", filename); + printf("Failed to read %s\n", filename); return NULL; } fclose(file); return content; } -struct cbfs_header *master_header; -uint32_t phys_start, phys_end, align, romsize; +static struct cbfs_header *master_header; +static uint32_t phys_start, phys_end, align; +uint32_t romsize; void *offset; void recalculate_rom_geometry(void *romarea) @@ -448,11 +453,15 @@ void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize, int create_cbfs_image(const char *romfile, uint32_t _romsize, const char *bootblock, uint32_t align, uint32_t offs) { + uint32_t bootblocksize = 0; + struct cbfs_header *master_header; + unsigned char *romarea, *bootblk; + romsize = _romsize; - unsigned char *romarea = malloc(romsize); + romarea = malloc(romsize); if (!romarea) { - printf("Could not get %d bytes of memory for CBFS image.\n", - romsize); + fprintf(stderr, "E: Could not get %d bytes of memory" + " for CBFS image.\n", romsize); exit(1); } memset(romarea, 0xff, romsize); @@ -463,9 +472,16 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize, if (align == 0) align = 64; - uint32_t bootblocksize = 0; - loadfile(bootblock, &bootblocksize, romarea + romsize, SEEK_END); - struct cbfs_header *master_header = + bootblk = loadfile(bootblock, &bootblocksize, + romarea + romsize, SEEK_END); + if (!bootblk) { + fprintf(stderr, "E: Could not load bootblock %s.\n", + bootblock); + free(romarea); + return 1; + } + + master_header = (struct cbfs_header *)(romarea + romsize - bootblocksize - sizeof(struct cbfs_header)); master_header->magic = ntohl(0x4f524243); @@ -485,6 +501,7 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize, sizeof(struct cbfs_file) - 16); writerom(romfile, romarea, romsize); + free(romarea); return 0; } @@ -496,49 +513,69 @@ static int in_segment(int addr, int size, int gran) uint32_t cbfs_find_location(const char *romfile, uint32_t filesize, const char *filename, uint32_t alignment) { - loadrom(romfile); - size_t filename_size = strlen(filename); + void *rom; + size_t filename_size, headersize, totalsize; + int ret = 0; + uint32_t current; + + rom = loadrom(romfile); + if (rom == NULL) { + fprintf(stderr, "E: Could not load ROM image '%s'.\n", + romfile); + return 0; + } - size_t headersize = - sizeof(struct cbfs_file) + ALIGN(filename_size + 1, - 16) + sizeof(struct cbfs_stage); - size_t totalsize = headersize + filesize; + filename_size = strlen(filename); + headersize = sizeof(struct cbfs_file) + ALIGN(filename_size + 1, 16) + + sizeof(struct cbfs_stage); + totalsize = headersize + filesize; - uint32_t current = phys_start; + current = phys_start; while (current < phys_end) { + uint32_t top; + struct cbfs_file *thisfile; + 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); + thisfile = (struct cbfs_file *)phys_to_virt(current); + + top = current + ntohl(thisfile->len) + ntohl(thisfile->offset); + if (((ntohl(thisfile->type) == 0x0) || (ntohl(thisfile->type) == 0xffffffff)) && (ntohl(thisfile->len) + ntohl(thisfile->offset) >= totalsize)) { if (in_segment - (current + headersize, filesize, alignment)) - return current + headersize; + (current + headersize, filesize, alignment)) { + ret = current + headersize; + break; + } if ((ALIGN(current, alignment) + filesize < top) && (ALIGN(current, alignment) - headersize > current) && in_segment(ALIGN(current, alignment), filesize, - alignment)) - return ALIGN(current, alignment); + alignment)) { + ret = ALIGN(current, alignment); + break; + } if ((ALIGN(current, alignment) + alignment + filesize < top) && (ALIGN(current, alignment) + alignment - headersize > current) && in_segment(ALIGN(current, alignment) + alignment, - filesize, alignment)) - return ALIGN(current, alignment) + alignment; + filesize, alignment)) { + ret = ALIGN(current, alignment) + alignment; + break; + } } current = ALIGN(current + ntohl(thisfile->len) + ntohl(thisfile->offset), align); } - return 0; + + free(rom); + return ret; } |