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>
diff --git a/util/cbfstool/common.c b/util/cbfstool/common.c
index 137aeb7..3394b7b 100644
--- a/util/cbfstool/common.c
+++ b/util/cbfstool/common.c
@@ -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 @@
 	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 @@
 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 @@
 		     sizeof(struct cbfs_header)) & 0xffffffff;
 		break;
 	default:
-		fprintf(stderr, "E: Unknown architecture\n");
+		ERROR("Unknown architecture\n");
 		exit(1);
 	}
 
@@ -185,13 +183,13 @@
 {
 	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 @@
 	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 @@
 		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 @@
 				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 @@
 						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 @@
 		}
 
 		// 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 @@
 		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 @@
 		    (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 @@
 					    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 @@
 				/* 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 @@
 				    && ((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 @@
 		    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 @@
 
 		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 *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 @@
 	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 @@
 	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 @@
 			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 @@
 
 	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 @@
 
 	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;
 	}