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

@ -44,13 +44,13 @@ int parse_elf_to_payload(unsigned char *input, unsigned char **output,
int i; int i;
if(!iself(input)){ if(!iself(input)){
fprintf(stderr, "E: The payload file is not in ELF format!\n"); ERROR("The payload file is not in ELF format!\n");
return -1; return -1;
} }
if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) && if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
!((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) { !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
fprintf(stderr, "E: The payload file has the wrong architecture\n"); ERROR("The payload file has the wrong architecture\n");
return -1; return -1;
} }

View File

@ -64,13 +64,13 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
return -1; return -1;
if (!iself(input)) { if (!iself(input)) {
fprintf(stderr, "E: The stage file is not in ELF format!\n"); ERROR("The stage file is not in ELF format!\n");
return -1; return -1;
} }
if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) && if (!((ehdr->e_machine == EM_ARM) && (arch == CBFS_ARCHITECTURE_ARMV7)) &&
!((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) { !((ehdr->e_machine == EM_386) && (arch == CBFS_ARCHITECTURE_X86))) {
fprintf(stderr, "E: The stage file has the wrong architecture\n"); ERROR("The stage file has the wrong architecture\n");
return -1; return -1;
} }
@ -126,7 +126,7 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
} }
if (data_end <= data_start) { if (data_end <= data_start) {
fprintf(stderr, "E: data ends before it starts. Make sure the " ERROR("data ends before it starts. Make sure the "
"ELF file is correct and resides in ROM space.\n"); "ELF file is correct and resides in ROM space.\n");
exit(1); exit(1);
} }
@ -135,7 +135,7 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
buffer = calloc(data_end - data_start, 1); buffer = calloc(data_end - data_start, 1);
if (buffer == NULL) { if (buffer == NULL) {
fprintf(stderr, "E: Unable to allocate memory: %m\n"); ERROR("Unable to allocate memory: %m\n");
return -1; return -1;
} }
@ -165,7 +165,7 @@ int parse_elf_to_stage(unsigned char *input, unsigned char **output,
out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1); out = calloc(sizeof(struct cbfs_stage) + data_end - data_start, 1);
if (out == NULL) { if (out == NULL) {
fprintf(stderr, "E: Unable to allocate memory: %m\n"); ERROR("Unable to allocate memory: %m\n");
return -1; return -1;
} }

View File

@ -59,30 +59,30 @@ static int cbfs_add(void)
void *rom, *filedata, *cbfsfile; void *rom, *filedata, *cbfsfile;
if (!param.filename) { if (!param.filename) {
fprintf(stderr, "E: You need to specify -f/--filename.\n"); ERROR("You need to specify -f/--filename.\n");
return 1; return 1;
} }
if (!param.name) { if (!param.name) {
fprintf(stderr, "E: You need to specify -n/--name.\n"); ERROR("You need to specify -n/--name.\n");
return 1; return 1;
} }
if (param.type == 0) { if (param.type == 0) {
fprintf(stderr, "E: You need to specify a valid -t/--type.\n"); ERROR("You need to specify a valid -t/--type.\n");
return 1; return 1;
} }
rom = loadrom(param.cbfs_name); rom = loadrom(param.cbfs_name);
if (rom == NULL) { if (rom == NULL) {
fprintf(stderr, "E: Could not load ROM image '%s'.\n", ERROR("Could not load ROM image '%s'.\n",
param.cbfs_name); param.cbfs_name);
return 1; return 1;
} }
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET); filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
if (filedata == NULL) { if (filedata == NULL) {
fprintf(stderr, "E: Could not load file '%s'.\n", ERROR("Could not load file '%s'.\n",
param.filename); param.filename);
free(rom); free(rom);
return 1; return 1;
@ -93,7 +93,7 @@ static int cbfs_add(void)
free(filedata); free(filedata);
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) { if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
fprintf(stderr, "E: Adding file '%s' failed.\n", param.filename); ERROR("Adding file '%s' failed.\n", param.filename);
free(cbfsfile); free(cbfsfile);
free(rom); free(rom);
return 1; return 1;
@ -116,25 +116,25 @@ static int cbfs_add_payload(void)
unsigned char *payload; unsigned char *payload;
if (!param.filename) { if (!param.filename) {
fprintf(stderr, "E: You need to specify -f/--filename.\n"); ERROR("You need to specify -f/--filename.\n");
return 1; return 1;
} }
if (!param.name) { if (!param.name) {
fprintf(stderr, "E: You need to specify -n/--name.\n"); ERROR("You need to specify -n/--name.\n");
return 1; return 1;
} }
rom = loadrom(param.cbfs_name); rom = loadrom(param.cbfs_name);
if (rom == NULL) { if (rom == NULL) {
fprintf(stderr, "E: Could not load ROM image '%s'.\n", ERROR("Could not load ROM image '%s'.\n",
param.cbfs_name); param.cbfs_name);
return 1; return 1;
} }
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET); filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
if (filedata == NULL) { if (filedata == NULL) {
fprintf(stderr, "E: Could not load file '%s'.\n", ERROR("Could not load file '%s'.\n",
param.filename); param.filename);
free(rom); free(rom);
return 1; return 1;
@ -142,7 +142,7 @@ static int cbfs_add_payload(void)
filesize = parse_elf_to_payload(filedata, &payload, param.algo); filesize = parse_elf_to_payload(filedata, &payload, param.algo);
if (filesize <= 0) { if (filesize <= 0) {
fprintf(stderr, "E: Adding payload '%s' failed.\n", ERROR("Adding payload '%s' failed.\n",
param.filename); param.filename);
free(rom); free(rom);
return 1; return 1;
@ -155,7 +155,7 @@ static int cbfs_add_payload(void)
free(payload); free(payload);
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) { if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
fprintf(stderr, "E: Adding payload '%s' failed.\n", ERROR("Adding payload '%s' failed.\n",
param.filename); param.filename);
free(cbfsfile); free(cbfsfile);
free(rom); free(rom);
@ -180,25 +180,25 @@ static int cbfs_add_stage(void)
unsigned char *stage; unsigned char *stage;
if (!param.filename) { if (!param.filename) {
fprintf(stderr, "E: You need to specify -f/--filename.\n"); ERROR("You need to specify -f/--filename.\n");
return 1; return 1;
} }
if (!param.name) { if (!param.name) {
fprintf(stderr, "E: You need to specify -n/--name.\n"); ERROR("You need to specify -n/--name.\n");
return 1; return 1;
} }
rom = loadrom(param.cbfs_name); rom = loadrom(param.cbfs_name);
if (rom == NULL) { if (rom == NULL) {
fprintf(stderr, "E: Could not load ROM image '%s'.\n", ERROR("Could not load ROM image '%s'.\n",
param.cbfs_name); param.cbfs_name);
return 1; return 1;
} }
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET); filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
if (filedata == NULL) { if (filedata == NULL) {
fprintf(stderr, "E: Could not load file '%s'.\n", ERROR("Could not load file '%s'.\n",
param.filename); param.filename);
free(rom); free(rom);
return 1; return 1;
@ -213,7 +213,7 @@ static int cbfs_add_stage(void)
free(stage); free(stage);
if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) { if (add_file_to_cbfs(cbfsfile, filesize, param.baseaddress)) {
fprintf(stderr, "E: Adding stage '%s' failed.\n", ERROR("Adding stage '%s' failed.\n",
param.filename); param.filename);
free(cbfsfile); free(cbfsfile);
free(rom); free(rom);
@ -242,23 +242,23 @@ static int cbfs_add_flat_binary(void)
int doffset, len = 0; int doffset, len = 0;
if (!param.filename) { if (!param.filename) {
fprintf(stderr, "E: You need to specify -f/--filename.\n"); ERROR("You need to specify -f/--filename.\n");
return 1; return 1;
} }
if (!param.name) { if (!param.name) {
fprintf(stderr, "E: You need to specify -n/--name.\n"); ERROR("You need to specify -n/--name.\n");
return 1; return 1;
} }
if (param.loadaddress == 0) { if (param.loadaddress == 0) {
fprintf(stderr, "E: You need to specify a valid " ERROR("You need to specify a valid "
"-l/--load-address.\n"); "-l/--load-address.\n");
return 1; return 1;
} }
if (param.entrypoint == 0) { if (param.entrypoint == 0) {
fprintf(stderr, "E: You need to specify a valid " ERROR("You need to specify a valid "
"-e/--entry-point.\n"); "-e/--entry-point.\n");
return 1; return 1;
} }
@ -269,14 +269,14 @@ static int cbfs_add_flat_binary(void)
rom = loadrom(param.cbfs_name); rom = loadrom(param.cbfs_name);
if (rom == NULL) { if (rom == NULL) {
fprintf(stderr, "E: Could not load ROM image '%s'.\n", ERROR("Could not load ROM image '%s'.\n",
param.cbfs_name); param.cbfs_name);
return 1; return 1;
} }
filedata = loadfile(param.filename, &filesize, 0, SEEK_SET); filedata = loadfile(param.filename, &filesize, 0, SEEK_SET);
if (filedata == NULL) { if (filedata == NULL) {
fprintf(stderr, "E: Could not load file '%s'.\n", ERROR("Could not load file '%s'.\n",
param.filename); param.filename);
free(rom); free(rom);
return 1; return 1;
@ -285,7 +285,7 @@ static int cbfs_add_flat_binary(void)
/* FIXME compressed file size might be bigger than original file */ /* FIXME compressed file size might be bigger than original file */
payload = calloc((2 * sizeof(struct cbfs_payload_segment)) + filesize, 1); payload = calloc((2 * sizeof(struct cbfs_payload_segment)) + filesize, 1);
if (payload == NULL) { if (payload == NULL) {
fprintf(stderr, "E: Could not allocate memory.\n"); ERROR("Could not allocate memory.\n");
free(filedata); free(filedata);
free(rom); free(rom);
return 1; return 1;
@ -323,7 +323,7 @@ static int cbfs_add_flat_binary(void)
free(payload); free(payload);
if (add_file_to_cbfs(cbfsfile, final_size, param.baseaddress)) { if (add_file_to_cbfs(cbfsfile, final_size, param.baseaddress)) {
fprintf(stderr, "E: Adding payload '%s' failed.\n", ERROR("Adding payload '%s' failed.\n",
param.filename); param.filename);
free(cbfsfile); free(cbfsfile);
free(rom); free(rom);
@ -345,19 +345,19 @@ static int cbfs_remove(void)
void *rom; void *rom;
if (!param.name) { if (!param.name) {
fprintf(stderr, "E: You need to specify -n/--name.\n"); ERROR("You need to specify -n/--name.\n");
return 1; return 1;
} }
rom = loadrom(param.cbfs_name); rom = loadrom(param.cbfs_name);
if (rom == NULL) { if (rom == NULL) {
fprintf(stderr, "E: Could not load ROM image '%s'.\n", ERROR("Could not load ROM image '%s'.\n",
param.cbfs_name); param.cbfs_name);
return 1; return 1;
} }
if (remove_file_from_cbfs(param.name)) { if (remove_file_from_cbfs(param.name)) {
fprintf(stderr, "E: Removing file '%s' failed.\n", ERROR("Removing file '%s' failed.\n",
param.name); param.name);
free(rom); free(rom);
return 1; return 1;
@ -375,17 +375,17 @@ static int cbfs_remove(void)
static int cbfs_create(void) static int cbfs_create(void)
{ {
if (param.size == 0) { if (param.size == 0) {
fprintf(stderr, "E: You need to specify a valid -s/--size.\n"); ERROR("You need to specify a valid -s/--size.\n");
return 1; return 1;
} }
if (!param.bootblock) { if (!param.bootblock) {
fprintf(stderr, "E: You need to specify -b/--bootblock.\n"); ERROR("You need to specify -b/--bootblock.\n");
return 1; return 1;
} }
if (arch == CBFS_ARCHITECTURE_UNKNOWN) { if (arch == CBFS_ARCHITECTURE_UNKNOWN) {
fprintf(stderr, "E: You need to specify -m/--machine arch\n"); ERROR("You need to specify -m/--machine arch\n");
return 1; return 1;
} }
@ -398,12 +398,12 @@ static int cbfs_locate(void)
uint32_t filesize, location; uint32_t filesize, location;
if (!param.filename) { if (!param.filename) {
fprintf(stderr, "E: You need to specify -f/--filename.\n"); ERROR("You need to specify -f/--filename.\n");
return 1; return 1;
} }
if (!param.name) { if (!param.name) {
fprintf(stderr, "E: You need to specify -n/--name.\n"); ERROR("You need to specify -n/--name.\n");
return 1; return 1;
} }
@ -422,7 +422,7 @@ static int cbfs_print(void)
rom = loadrom(param.cbfs_name); rom = loadrom(param.cbfs_name);
if (rom == NULL) { if (rom == NULL) {
fprintf(stderr, "E: Could not load ROM image '%s'.\n", ERROR("Could not load ROM image '%s'.\n",
param.cbfs_name); param.cbfs_name);
return 1; return 1;
} }
@ -439,18 +439,18 @@ static int cbfs_extract(void)
int ret; int ret;
if (!param.filename) { if (!param.filename) {
fprintf(stderr, "E: You need to specify -f/--filename.\n"); ERROR("You need to specify -f/--filename.\n");
return 1; return 1;
} }
if (!param.name) { if (!param.name) {
fprintf(stderr, "E: You need to specify -n/--name.\n"); ERROR("You need to specify -n/--name.\n");
return 1; return 1;
} }
rom = loadrom(param.cbfs_name); rom = loadrom(param.cbfs_name);
if (rom == NULL) { if (rom == NULL) {
fprintf(stderr, "E: Could not load ROM image '%s'.\n", ERROR("Could not load ROM image '%s'.\n",
param.cbfs_name); param.cbfs_name);
return 1; return 1;
} }
@ -462,15 +462,15 @@ static int cbfs_extract(void)
} }
static const struct command commands[] = { static const struct command commands[] = {
{"add", "f:n:t:b:h?", cbfs_add}, {"add", "f:n:t:b:vh?", cbfs_add},
{"add-payload", "f:n:t:c:b:h?", cbfs_add_payload}, {"add-payload", "f:n:t:c:b:vh?", cbfs_add_payload},
{"add-stage", "f:n:t:c:b:h?", cbfs_add_stage}, {"add-stage", "f:n:t:c:b:vh?", cbfs_add_stage},
{"add-flat-binary", "f:n:l:e:c:b:h?", cbfs_add_flat_binary}, {"add-flat-binary", "f:n:l:e:c:b:vh?", cbfs_add_flat_binary},
{"remove", "n:h?", cbfs_remove}, {"remove", "n:vh?", cbfs_remove},
{"create", "s:B:a:o:m:h?", cbfs_create}, {"create", "s:B:a:o:m:vh?", cbfs_create},
{"locate", "f:n:a:h?", cbfs_locate}, {"locate", "f:n:a:vh?", cbfs_locate},
{"print", "h?", cbfs_print}, {"print", "vh?", cbfs_print},
{"extract", "n:f:h?", cbfs_extract}, {"extract", "n:f:vh?", cbfs_extract},
}; };
static struct option long_options[] = { static struct option long_options[] = {
@ -496,8 +496,9 @@ static void usage(char *name)
printf printf
("cbfstool: Management utility for CBFS formatted ROM images\n\n" ("cbfstool: Management utility for CBFS formatted ROM images\n\n"
"USAGE:\n" " %s [-h]\n" "USAGE:\n" " %s [-h]\n"
" %s FILE COMMAND [PARAMETERS]...\n\n" "OPTIONs:\n" " %s FILE COMMAND [-v] [PARAMETERS]...\n\n" "OPTIONs:\n"
" -h Display this help message\n\n" " -v Provide verbose output\n"
" -h Display this help message\n\n"
"COMMANDs:\n" "COMMANDs:\n"
" add -f FILE -n NAME -t TYPE [-b base-address] " " add -f FILE -n NAME -t TYPE [-b base-address] "
"Add a component\n" "Add a component\n"
@ -570,8 +571,8 @@ int main(int argc, char **argv)
/* filter out illegal long options */ /* filter out illegal long options */
if (strchr(commands[i].optstring, c) == NULL) { if (strchr(commands[i].optstring, c) == NULL) {
/* TODO maybe print actual long option instead */ /* TODO maybe print actual long option instead */
printf("%s: invalid option -- '%c'\n", ERROR("%s: invalid option -- '%c'\n",
argv[0], c); argv[0], c);
c = '?'; c = '?';
} }
@ -585,7 +586,7 @@ int main(int argc, char **argv)
else else
param.type = strtoul(optarg, NULL, 0); param.type = strtoul(optarg, NULL, 0);
if (param.type == 0) if (param.type == 0)
printf("W: Unknown type '%s' ignored\n", WARN("Unknown type '%s' ignored\n",
optarg); optarg);
break; break;
case 'c': case 'c':
@ -594,8 +595,8 @@ int main(int argc, char **argv)
else if (!strncasecmp(optarg, "none", 5)) else if (!strncasecmp(optarg, "none", 5))
param.algo = CBFS_COMPRESS_NONE; param.algo = CBFS_COMPRESS_NONE;
else else
printf("W: Unknown compression '%s'" WARN("Unknown compression '%s'"
" ignored.\n", optarg); " ignored.\n", optarg);
break; break;
case 'b': case 'b':
param.baseaddress = strtoul(optarg, NULL, 0); param.baseaddress = strtoul(optarg, NULL, 0);
@ -645,7 +646,7 @@ int main(int argc, char **argv)
return commands[i].function(); return commands[i].function();
} }
printf("Unknown command '%s'.\n", cmd); ERROR("Unknown command '%s'.\n", cmd);
usage(argv[0]); usage(argv[0]);
return 1; return 1;
} }

View File

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

View File

@ -29,6 +29,14 @@
#define ntohll(x) (host_bigendian?(x):swab64(x)) #define ntohll(x) (host_bigendian?(x):swab64(x))
#define htonll(x) (host_bigendian?(x):swab64(x)) #define htonll(x) (host_bigendian?(x):swab64(x))
/* Message output */
extern int verbose;
#define ERROR(x...) { fprintf(stderr, "E: " x); }
#define WARN(x...) { fprintf(stderr, "W: " x); }
#define LOG(x...) { fprintf(stderr, x); }
#define INFO(x...) { if (verbose > 0) fprintf(stderr, "INFO: " x); }
#define DEBUG(x...) { if (verbose > 1) fprintf(stderr, "DEBUG: " x); }
extern void *offset; extern void *offset;
extern uint32_t romsize; extern uint32_t romsize;
extern int host_bigendian; extern int host_bigendian;

View File

@ -50,7 +50,7 @@ comp_func_ptr compression_function(comp_algo algo)
compress = lzma_compress; compress = lzma_compress;
break; break;
default: default:
fprintf(stderr, "E: Unknown compression algorithm %d!\n", algo); ERROR("Unknown compression algorithm %d!\n", algo);
return NULL; return NULL;
} }
return compress; return compress;