cbfstool: Add -v (verbose) output.

Add -v (verbose) to every command, and allow printing debug messages.

Revise logging and debugging functions (fprintf(stderr,...), dprintf...)
and verbose message printing with following macros:
	ERROR(xxx):	E: xxx
	WARN(xxx)	W: xxx
	LOG(xxx)	xxx
	INFO(...)	INFO: xxx  (only when runs with -v )
	DEBUG(...)	DEBUG: xxx (only when runs with more than one -v)

Example:
	cbfstool coreboot.rom print -v
	cbfstool coreboot.rom add -f file -n file -t raw -v -v

Normal output (especially for parsing) should use printf, not any of these
macros (see usage() and cbfs_locate(), cbfs_print_directory() for example).

Change-Id: I167617da1a6eea2b07075b0eb38e3c9d85ea75dc
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: http://review.coreboot.org/2196
Tested-by: build bot (Jenkins)
Reviewed-by: David Hendricks <dhendrix@chromium.org>
This commit is contained in:
Hung-Te Lin
2013-01-28 14:39:43 +08:00
committed by David Hendricks
parent 7fb692bd86
commit 4d87d4e09b
6 changed files with 108 additions and 104 deletions

View File

@ -27,8 +27,6 @@
#include "cbfs.h"
#include "elf.h"
#define dprintf(x...)
size_t getfilesize(const char *filename)
{
size_t size;
@ -55,15 +53,15 @@ void *loadfile(const char *filename, uint32_t * romsize_p, void *content,
if (!content) {
content = malloc(*romsize_p);
if (!content) {
fprintf(stderr, "E: Could not get %d bytes for file %s\n",
*romsize_p, filename);
ERROR("Could not get %d bytes for file %s\n",
*romsize_p, filename);
exit(1);
}
} else if (place == SEEK_END)
content -= *romsize_p;
if (!fread(content, *romsize_p, 1, file)) {
fprintf(stderr, "E: Failed to read %s\n", filename);
ERROR("Failed to read %s\n", filename);
return NULL;
}
fclose(file);
@ -138,14 +136,14 @@ int find_master_header(void *romarea, size_t size)
void recalculate_rom_geometry(void *romarea)
{
if (find_master_header(romarea, romsize)) {
fprintf(stderr, "E: Cannot find master header\n");
ERROR("Cannot find master header\n");
exit(1);
}
/* Update old headers */
if (master_header->version == CBFS_HEADER_VERSION1 &&
ntohl(master_header->architecture) == CBFS_ARCHITECTURE_UNKNOWN) {
dprintf("Updating CBFS master header to version 2\n");
DEBUG("Updating CBFS master header to version 2\n");
master_header->architecture = htonl(CBFS_ARCHITECTURE_X86);
}
@ -165,7 +163,7 @@ void recalculate_rom_geometry(void *romarea)
sizeof(struct cbfs_header)) & 0xffffffff;
break;
default:
fprintf(stderr, "E: Unknown architecture\n");
ERROR("Unknown architecture\n");
exit(1);
}
@ -185,13 +183,13 @@ int writerom(const char *filename, void *start, uint32_t size)
{
FILE *file = fopen(filename, "wb");
if (!file) {
fprintf(stderr, "Could not open '%s' for writing: ", filename);
ERROR("Could not open '%s' for writing: ", filename);
perror("");
return 1;
}
if (fwrite(start, size, 1, file) != 1) {
fprintf(stderr, "Could not write to '%s': ", filename);
ERROR("Could not write to '%s': ", filename);
perror("");
return 1;
}
@ -247,9 +245,9 @@ void print_supported_filetypes(void)
int i, number = ARRAY_SIZE(filetypes);
for (i=0; i<number; i++) {
printf(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
LOG(" %s%c", filetypes[i].name, (i==(number-1))?'\n':',');
if ((i%8) == 7)
printf("\n");
LOG("\n");
}
}
@ -303,7 +301,7 @@ void print_cbfs_directory(const char *filename)
case CBFS_COMPONENT_STAGE:
{
struct cbfs_stage *stage = CBFS_SUBHEADER(thisfile);
dprintf(" %s compression, entry: 0x%llx, load: 0x%llx, length: %d/%d\n",
INFO(" %s compression, entry: 0x%llx, load: 0x%llx, length: %d/%d\n",
stage->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no",
(unsigned long long)stage->entry,
(unsigned long long)stage->load,
@ -318,7 +316,7 @@ void print_cbfs_directory(const char *filename)
switch(payload->type) {
case PAYLOAD_SEGMENT_CODE:
case PAYLOAD_SEGMENT_DATA:
dprintf(" %s (%s compression, offset: 0x%x, load: 0x%llx, length: %d/%d)\n",
INFO(" %s (%s compression, offset: 0x%x, load: 0x%llx, length: %d/%d)\n",
payload->type == PAYLOAD_SEGMENT_CODE ? "code " : "data" ,
payload->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no",
ntohl(payload->offset),
@ -326,16 +324,16 @@ void print_cbfs_directory(const char *filename)
ntohl(payload->len), ntohl(payload->mem_len));
break;
case PAYLOAD_SEGMENT_ENTRY:
dprintf(" entry (0x%llx)\n", (unsigned long long)ntohll(payload->load_addr));
INFO(" entry (0x%llx)\n", (unsigned long long)ntohll(payload->load_addr));
break;
case PAYLOAD_SEGMENT_BSS:
dprintf(" BSS (address 0x%016llx, length 0x%x)\n", (unsigned long long)ntohll(payload->load_addr), ntohl(payload->len));
INFO(" BSS (address 0x%016llx, length 0x%x)\n", (unsigned long long)ntohll(payload->load_addr), ntohl(payload->len));
break;
case PAYLOAD_SEGMENT_PARAMS:
dprintf(" parameters\n");
INFO(" parameters\n");
break;
default:
dprintf(" %x (%s compression, offset: 0x%x, load: 0x%llx, length: %d/%d\n",
INFO(" %x (%s compression, offset: 0x%x, load: 0x%llx, length: %d/%d\n",
payload->type,
payload->compression == CBFS_COMPRESS_LZMA ? "LZMA" : "no",
ntohl(payload->offset),
@ -389,7 +387,7 @@ int extract_file_from_cbfs(const char *filename, const char *payloadname, const
}
// Else, it's our file.
printf("Found file %.30s at 0x%x, type %.12s, size %d\n", fname,
LOG("Found file %.30s at 0x%x, type %.12s, size %d\n", fname,
current - phys_start, strfiletype(ntohl(thisfile->type)),
length);
@ -397,25 +395,25 @@ int extract_file_from_cbfs(const char *filename, const char *payloadname, const
outfile = fopen(outpath, "wb");
if (!outfile)
{
fprintf(stderr, "E: Could not open the file %s for writing.\n", outpath);
ERROR("Could not open the file %s for writing.\n", outpath);
return 1;
}
if (ntohl(thisfile->type) != CBFS_COMPONENT_RAW)
{
fprintf(stderr, "W: Only 'raw' files are safe to extract.\n");
WARN("Only 'raw' files are safe to extract.\n");
}
fwrite(((char *)thisfile)
+ ntohl(thisfile->offset), length, 1, outfile);
fclose(outfile);
printf("Successfully dumped the file.\n");
LOG("Successfully dumped the file.\n");
// We'll only dump one file.
return 0;
}
fprintf(stderr, "E: File %s not found.\n", payloadname);
ERROR("File %s not found.\n", payloadname);
return 1;
}
@ -432,18 +430,17 @@ int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
(struct cbfs_file *)phys_to_virt(current);
uint32_t length = ntohl(thisfile->len);
dprintf("at %x, %x bytes\n", current, length);
DEBUG("at %x, %x bytes\n", current, length);
/* Is this a free chunk? */
if ((thisfile->type == CBFS_COMPONENT_DELETED)
|| (thisfile->type == CBFS_COMPONENT_NULL)) {
dprintf("null||deleted at %x, %x bytes\n", current,
DEBUG("null||deleted at %x, %x bytes\n", current,
length);
/* if this is the right size, and if specified, the right location, use it */
if ((contentsize <= length)
&& ((location == 0) || (current == location))) {
if (contentsize < length) {
dprintf
("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
DEBUG("this chunk is %x bytes, we need %x. create a new chunk at %x with %x bytes\n",
length, contentsize,
ALIGN(current + contentsize,
align),
@ -456,7 +453,7 @@ int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
sizeof(struct cbfs_file);
cbfs_create_empty_file(start, size);
}
dprintf("copying data\n");
DEBUG("copying data\n");
memcpy(phys_to_virt(current), content,
contentsize);
return 0;
@ -465,8 +462,7 @@ int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
/* CBFS has the constraint that the chain always moves up in memory. so once
we're past the place we seek, we don't need to look any further */
if (current > location) {
fprintf
(stderr, "E: The requested space is not available\n");
ERROR("The requested space is not available\n");
return 1;
}
@ -475,7 +471,7 @@ int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
&& ((location + contentsize) <=
(current + length))) {
/* Split it up. In the next iteration the code will be at the right place. */
dprintf("split up. new length: %x\n",
DEBUG("split up. new length: %x\n",
location - current -
ntohl(thisfile->offset));
thisfile->len =
@ -492,8 +488,8 @@ int add_file_to_cbfs(void *content, uint32_t contentsize, uint32_t location)
ALIGN(current + ntohl(thisfile->len) +
ntohl(thisfile->offset), align);
}
fprintf(stderr, "E: Could not add the file to CBFS, it's probably too big.\n");
fprintf(stderr, "E: File size: %d bytes (%d KB).\n", contentsize, contentsize/1024);
ERROR("Could not add the file to CBFS, it's probably too big.\n");
ERROR("File size: %d bytes (%d KB).\n", contentsize, contentsize/1024);
return 1;
}
@ -548,7 +544,7 @@ int remove_file_from_cbfs(const char *filename)
return 0;
}
fprintf(stderr, "E: CBFS file %s not found.\n", filename);
ERROR("CBFS file %s not found.\n", filename);
return 1;
}
@ -574,7 +570,7 @@ void *create_cbfs_file(const char *filename, void *data, uint32_t * datasize,
}
void *newdata = malloc(*datasize + headersize);
if (!newdata) {
fprintf(stderr, "E: Could not get %d bytes for CBFS file.\n", *datasize +
ERROR("Could not get %d bytes for CBFS file.\n", *datasize +
headersize);
exit(1);
}
@ -601,7 +597,7 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize,
romsize = _romsize;
romarea = malloc(romsize);
if (!romarea) {
fprintf(stderr, "E: Could not get %d bytes of memory"
ERROR("Could not get %d bytes of memory"
" for CBFS image.\n", romsize);
exit(1);
}
@ -613,7 +609,7 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize,
bootblk = loadfile(bootblock, &bootblocksize,
romarea + romsize, SEEK_END);
if (!bootblk) {
fprintf(stderr, "E: Could not load bootblock %s.\n",
ERROR("Could not load bootblock %s.\n",
bootblock);
free(romarea);
return 1;
@ -643,8 +639,8 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize,
if (*(uint32_t *)p == 0xdeadbeef)
break;
if (p >= (romarea + _romsize)) {
fprintf(stderr, "E: Could not determine CBFS "
"header location.\n", bootblock);
ERROR("Could not determine CBFS "
"header location.\n");
return 1;
}
p += (sizeof(unsigned int));
@ -702,7 +698,7 @@ int create_cbfs_image(const char *romfile, uint32_t _romsize,
default:
// Should not happen.
fprintf(stderr, "E: You found a bug in cbfstool.\n");
ERROR("You found a bug in cbfstool.\n");
exit(1);
}
@ -726,8 +722,7 @@ uint32_t cbfs_find_location(const char *romfile, uint32_t filesize,
rom = loadrom(romfile);
if (rom == NULL) {
fprintf(stderr, "E: Could not load ROM image '%s'.\n",
romfile);
ERROR("Could not load ROM image '%s'.\n", romfile);
return 0;
}