futility: Merge Debug() into VB2_DEBUG()

Futility has two debug facilities: the Debug() function that can be
enabled by passing --debug on the command line, and the VB2_DEBUG()
macro (mostly in common code from the firmware/ directory that it
includes) which can only be enabled by passing DEBUG=1 at build time.
This is confusing and inconvenient, since you don't always want to
rebuild futility whenever you need that extra debug output and it's not
very obvious that you can get even more debugging beyond just passing
--debug.

This patch resolves the inconsistency by merging both facilities
together into a single VB2_DEBUG() that is output when passing --debug.
In order to make this work, we'll have to move the VBOOT_DEBUG #define
so that it only affects the stub implementation of vb2ex_printf(), and
any caller overriding the stub is in charge of their own destiny. This
should be okay since callers can still individually implement debugging
policy in their versions of vb2ex_printf() if they want to. (This may
have been useful to cut down the binary space for debugging strings, but
our firmware has always been unconditionally enabling VBOOT_DEBUG in the
past years, so that doesn't seem to be very important in practice.)

BRANCH=None
BUG=None
TEST=Ran futility --debug show, noticed I got all the extra keyblock
verification debug output I always wanted.

Change-Id: I9a5c205fc3673941b50f03f2a967b1be110a1555
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1504140
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-by: Randall Spangler <rspangler@chromium.org>
diff --git a/firmware/2lib/2stub.c b/firmware/2lib/2stub.c
index 2e08de9..0ff1997 100644
--- a/firmware/2lib/2stub.c
+++ b/firmware/2lib/2stub.c
@@ -14,12 +14,14 @@
 __attribute__((weak))
 void vb2ex_printf(const char *func, const char *fmt, ...)
 {
+#ifdef VBOOT_DEBUG
 	va_list ap;
 	va_start(ap, fmt);
 	if (func)
 		fprintf(stderr, "%s: ", func);
 	vfprintf(stderr, fmt, ap);
 	va_end(ap);
+#endif
 }
 
 __attribute__((weak))
diff --git a/firmware/2lib/include/2common.h b/firmware/2lib/include/2common.h
index 7bd8907..0a6dccd 100644
--- a/firmware/2lib/include/2common.h
+++ b/firmware/2lib/include/2common.h
@@ -30,13 +30,8 @@
 #endif
 
 /* Platform-dependent debug output macros. */
-#if defined(VBOOT_DEBUG)
-#  define VB2_DEBUG(format, args...) vb2ex_printf(__func__, format, ## args)
-#  define VB2_DEBUG_RAW(format, args...) vb2ex_printf(NULL, format, ## args)
-#else
-#  define VB2_DEBUG(format, args...)
-#  define VB2_DEBUG_RAW(format, args...)
-#endif
+#define VB2_DEBUG(format, args...) vb2ex_printf(__func__, format, ## args)
+#define VB2_DEBUG_RAW(format, args...) vb2ex_printf(NULL, format, ## args)
 
 /*
  * Define test_mockable and for mocking functions when compiled for Chrome OS
diff --git a/futility/cmd_sign.c b/futility/cmd_sign.c
index e084cb2..cc2b3e6 100644
--- a/futility/cmd_sign.c
+++ b/futility/cmd_sign.c
@@ -124,7 +124,7 @@
 		fprintf(stderr, "Unable to create kernel blob\n");
 		return 1;
 	}
-	Debug("kblob_size = 0x%x\n", kblob_size);
+	VB2_DEBUG("kblob_size = 0x%x\n", kblob_size);
 
 	vblock_data = SignKernelBlob(kblob_data, kblob_size,
 				     sign_option.padding,
@@ -138,7 +138,7 @@
 		free(kblob_data);
 		return 1;
 	}
-	Debug("vblock_size = 0x%x\n", vblock_size);
+	VB2_DEBUG("vblock_size = 0x%x\n", vblock_size);
 
 	/* We should be creating a completely new output file.
 	 * If not, something's wrong. */
@@ -225,7 +225,7 @@
 		fprintf(stderr, "Unable to sign kernel blob\n");
 		return 1;
 	}
-	Debug("vblock_size = 0x%" PRIx64 "\n", vblock_size);
+	VB2_DEBUG("vblock_size = 0x%" PRIx64 "\n", vblock_size);
 
 	if (sign_option.create_new_outfile) {
 		/* Write out what we've been asked for */
@@ -739,7 +739,7 @@
 					strerror(errno));
 				errorcnt++;
 			}
-			Debug("bootloader file size=0x%" PRIx64 "\n",
+			VB2_DEBUG("bootloader file size=0x%" PRIx64 "\n",
 			      sign_option.bootloader_size);
 			break;
 		case OPT_CONFIG:
@@ -862,7 +862,7 @@
 		case 0:				/* handled option */
 			break;
 		default:
-			Debug("i=%d\n", i);
+			VB2_DEBUG("i=%d\n", i);
 			DIE;
 		}
 	}
@@ -911,7 +911,7 @@
 			sign_option.type = FILE_TYPE_RAW_FIRMWARE;
 	}
 
-	Debug("type=%s\n", futil_file_type_name(sign_option.type));
+	VB2_DEBUG("type=%s\n", futil_file_type_name(sign_option.type));
 
 	/* Check the arguments for the type of thing we want to sign */
 	switch (sign_option.type) {
@@ -985,10 +985,11 @@
 		break;
 	}
 
-	Debug("infile=%s\n", infile);
-	Debug("sign_option.inout_file_count=%d\n", sign_option.inout_file_count);
-	Debug("sign_option.create_new_outfile=%d\n",
-	      sign_option.create_new_outfile);
+	VB2_DEBUG("infile=%s\n", infile);
+	VB2_DEBUG("sign_option.inout_file_count=%d\n",
+		  sign_option.inout_file_count);
+	VB2_DEBUG("sign_option.create_new_outfile=%d\n",
+		  sign_option.create_new_outfile);
 
 	/* Make sure we have an output file if one is needed */
 	if (!sign_option.outfile) {
@@ -1001,7 +1002,7 @@
 		}
 	}
 
-	Debug("sign_option.outfile=%s\n", sign_option.outfile);
+	VB2_DEBUG("sign_option.outfile=%s\n", sign_option.outfile);
 
 	if (argc - optind > 0) {
 		errorcnt++;
@@ -1014,7 +1015,7 @@
 	if (sign_option.create_new_outfile) {
 		/* The input is read-only, the output is write-only. */
 		mapping = MAP_RO;
-		Debug("open RO %s\n", infile);
+		VB2_DEBUG("open RO %s\n", infile);
 		ifd = open(infile, O_RDONLY);
 		if (ifd < 0) {
 			errorcnt++;
@@ -1027,7 +1028,7 @@
 		mapping = MAP_RW;
 		if (sign_option.inout_file_count > 1)
 			futil_copy_file_or_die(infile, sign_option.outfile);
-		Debug("open RW %s\n", sign_option.outfile);
+		VB2_DEBUG("open RW %s\n", sign_option.outfile);
 		infile = sign_option.outfile;
 		ifd = open(sign_option.outfile, O_RDWR);
 		if (ifd < 0) {
diff --git a/futility/cmd_vbutil_kernel.c b/futility/cmd_vbutil_kernel.c
index 0f6502a..a5bacdd 100644
--- a/futility/cmd_vbutil_kernel.c
+++ b/futility/cmd_vbutil_kernel.c
@@ -195,11 +195,11 @@
 	} else {
 		file_size = statbuf.st_size;
 	}
-	Debug("%s size is 0x%x\n", filename, file_size);
+	VB2_DEBUG("%s size is 0x%x\n", filename, file_size);
 	if (file_size < opt_pad)
 		Fatal("%s is too small to be a valid kernel blob\n", filename);
 
-	Debug("Reading %s\n", filename);
+	VB2_DEBUG("Reading %s\n", filename);
 	fp = fopen(filename, "rb");
 	if (!fp)
 		Fatal("Unable to open file %s: %s\n", filename,
@@ -415,7 +415,7 @@
 		if (!config_file)
 			Fatal("Missing required config file.\n");
 
-		Debug("Reading %s\n", config_file);
+		VB2_DEBUG("Reading %s\n", config_file);
 		t_config_data =
 			ReadConfigFile(config_file, &t_config_size);
 		if (!t_config_data)
@@ -424,23 +424,23 @@
 		if (!bootloader_file)
 			Fatal("Missing required bootloader file.\n");
 
-		Debug("Reading %s\n", bootloader_file);
+		VB2_DEBUG("Reading %s\n", bootloader_file);
 
 		if (VB2_SUCCESS != vb2_read_file(bootloader_file,
 						 &t_bootloader_data,
 						 &t_bootloader_size))
 			Fatal("Error reading bootloader file.\n");
-		Debug(" bootloader file size=0x%x\n", t_bootloader_size);
+		VB2_DEBUG(" bootloader file size=0x%x\n", t_bootloader_size);
 
 		if (!vmlinuz_file)
 			Fatal("Missing required vmlinuz file.\n");
 
-		Debug("Reading %s\n", vmlinuz_file);
+		VB2_DEBUG("Reading %s\n", vmlinuz_file);
 		if (VB2_SUCCESS !=
 		    vb2_read_file(vmlinuz_file, &vmlinuz_buf, &vmlinuz_size))
 			Fatal("Error reading vmlinuz file.\n");
 
-		Debug(" vmlinuz file size=0x%x\n", vmlinuz_size);
+		VB2_DEBUG(" vmlinuz file size=0x%x\n", vmlinuz_size);
 		if (!vmlinuz_size)
 			Fatal("Empty vmlinuz file\n");
 
@@ -453,7 +453,7 @@
 		if (!kblob_data)
 			Fatal("Unable to create kernel blob\n");
 
-		Debug("kblob_size = 0x%x\n", kblob_size);
+		VB2_DEBUG("kblob_size = 0x%x\n", kblob_size);
 
 		vblock_data = SignKernelBlob(kblob_data, kblob_size, opt_pad,
 					     version, kernel_body_load_address,
@@ -462,7 +462,7 @@
 		if (!vblock_data)
 			Fatal("Unable to sign kernel blob\n");
 
-		Debug("vblock_size = 0x%x\n", vblock_size);
+		VB2_DEBUG("vblock_size = 0x%x\n", vblock_size);
 
 		if (opt_vblockonly)
 			rv = WriteSomeParts(filename,
@@ -513,7 +513,7 @@
 
 		/* Update the config if asked */
 		if (config_file) {
-			Debug("Reading %s\n", config_file);
+			VB2_DEBUG("Reading %s\n", config_file);
 			t_config_data =
 				ReadConfigFile(config_file, &t_config_size);
 			if (!t_config_data)
diff --git a/futility/file_type_bios.c b/futility/file_type_bios.c
index ce6371d..e9e0b77 100644
--- a/futility/file_type_bios.c
+++ b/futility/file_type_bios.c
@@ -42,9 +42,8 @@
 {
 	uint32_t sum = ah->area_offset + ah->area_size;
 	if (sum < ah->area_size || sum > len) {
-		Debug("%s(%s) 0x%x + 0x%x > 0x%x\n",
-		      __func__, ah->area_name,
-		      ah->area_offset, ah->area_size, len);
+		VB2_DEBUG("%s 0x%x + 0x%x > 0x%x\n",
+			  ah->area_name, ah->area_offset, ah->area_size, len);
 		ah->area_offset = 0;
 		ah->area_size = 0;
 	}
@@ -222,10 +221,9 @@
 			state.area[c].buf = buf + ah->area_offset;
 			state.area[c].len = ah->area_size;
 
-			Debug("%s() showing FMAP area %d (%s),"
-			      " offset=0x%08x len=0x%08x\n",
-			      __func__, c, ah_name,
-			      ah->area_offset, ah->area_size);
+			VB2_DEBUG("showing FMAP area %d (%s),"
+				  " offset=0x%08x len=0x%08x\n",
+				  c, ah_name, ah->area_offset, ah->area_size);
 
 			/* Go look at it. */
 			if (fmap_show_fn[c])
@@ -483,10 +481,9 @@
 			state.area[c].buf = buf + ah->area_offset;
 			state.area[c].len = ah->area_size;
 
-			Debug("%s() examining FMAP area %d (%s),"
-			      " offset=0x%08x len=0x%08x\n",
-			      __func__, c, ah_name,
-			      ah->area_offset, ah->area_size);
+			VB2_DEBUG("%s() examining FMAP area %d (%s),"
+				  " offset=0x%08x len=0x%08x\n",
+				  c, ah_name, ah->area_offset, ah->area_size);
 
 			/* Go look at it, but abort on error */
 			if (fmap_sign_fn[c])
diff --git a/futility/file_type_rwsig.c b/futility/file_type_rwsig.c
index e2288f1..0776f26 100644
--- a/futility/file_type_rwsig.c
+++ b/futility/file_type_rwsig.c
@@ -75,11 +75,10 @@
 	FmapHeader *fmap;
 	int i;
 
-	Debug("%s(): name %s\n", __func__, name);
-	Debug("%s(): len  0x%08x (%d)\n", __func__, len, len);
+	VB2_DEBUG("name %s len 0x%08x (%d)\n", name, len, len);
 
 	/* Am I just looking at a signature file? */
-	Debug("Looking for signature at 0x0\n");
+	VB2_DEBUG("Looking for signature at 0x0\n");
 	sig = (const struct vb21_signature *)buf;
 	if (VB2_SUCCESS == vb21_verify_signature(sig, len)) {
 		show_sig(name, sig);
@@ -94,7 +93,7 @@
 		/* This looks like a full image. */
 		FmapAreaHeader *fmaparea;
 
-		Debug("Found an FMAP!\n");
+		VB2_DEBUG("Found an FMAP!\n");
 
 		/* If no public key is provided, use the one packed in RO
 		 * image, and print that. */
@@ -110,14 +109,14 @@
 		sig = (const struct vb21_signature *)
 			fmap_find_by_name(buf, len, fmap, "SIG_RW", &fmaparea);
 		if (!sig) {
-			Debug("No SIG_RW in FMAP.\n");
+			VB2_DEBUG("No SIG_RW in FMAP.\n");
 			return 1;
 		}
 
 		sig_size = fmaparea->area_size;
 
-		Debug("Looking for signature at 0x%x (0x%x)\n",
-			(uint8_t*)sig - buf, sig_size);
+		VB2_DEBUG("Looking for signature at 0x%x (0x%x)\n",
+			  (uint8_t*)sig - buf, sig_size);
 
 		if (VB2_SUCCESS != vb21_verify_signature(sig, sig_size))
 			return 1;
@@ -132,7 +131,7 @@
 		total_data_size = fmaparea->area_size-sig_size;
 
 		if (!data) {
-			Debug("No EC_RW in FMAP.\n");
+			VB2_DEBUG("No EC_RW in FMAP.\n");
 			return 1;
 		}
 	} else {
@@ -141,10 +140,10 @@
 		if (show_option.sig_size)
 			sig_size = show_option.sig_size;
 
-		Debug("Looking for signature at 0x%x\n", len - sig_size);
+		VB2_DEBUG("Looking for signature at 0x%x\n", len - sig_size);
 
 		if (len < sig_size) {
-			Debug("File is too small\n");
+			VB2_DEBUG("File is too small\n");
 			return 1;
 		}
 
@@ -168,12 +167,12 @@
 	if (vb21_unpack_key(&key,
 			    (const uint8_t *)pkey,
 			    pkey->c.total_size)) {
-		Debug("Can't unpack pubkey\n");
+		VB2_DEBUG("Can't unpack pubkey\n");
 		return 1;
 	}
 
 	if (data_size > total_data_size) {
-		Debug("Invalid signature data_size: bigger than total area size.\n");
+		VB2_DEBUG("Invalid signature data_size: bigger than total area size.\n");
 		return 1;
 	}
 
@@ -219,8 +218,7 @@
 	FmapAreaHeader *fmaparea;
 	struct vb21_signature *old_sig = 0;
 
-	Debug("%s(): name %s\n", __func__, name);
-	Debug("%s(): len  0x%08x (%d)\n", __func__, len, len);
+	VB2_DEBUG("name %s len  0x%08x (%d)\n", name, len, len);
 
 	/* If we don't have a distinct OUTFILE, look for an existing sig */
 	if (sign_option.inout_file_count < 2) {
@@ -228,25 +226,25 @@
 
 		if (fmap) {
 			/* This looks like a full image. */
-			Debug("Found an FMAP!\n");
+			VB2_DEBUG("Found an FMAP!\n");
 
 			old_sig = (struct vb21_signature *)
 				fmap_find_by_name(buf, len, fmap, "SIG_RW",
 						  &fmaparea);
 			if (!old_sig) {
-				Debug("No SIG_RW in FMAP.\n");
+				VB2_DEBUG("No SIG_RW in FMAP.\n");
 				goto done;
 			}
 
 			sig_size = fmaparea->area_size;
 
-			Debug("Looking for signature at 0x%x (0x%x)\n",
-				(uint8_t*)old_sig - buf, sig_size);
+			VB2_DEBUG("Looking for signature at 0x%x (0x%x)\n",
+				  (uint8_t*)old_sig - buf, sig_size);
 
 			data = fmap_find_by_name(buf, len, fmap, "EC_RW",
 						 &fmaparea);
 			if (!data) {
-				Debug("No EC_RW in FMAP.\n");
+				VB2_DEBUG("No EC_RW in FMAP.\n");
 				goto done;
 			}
 		} else {
@@ -255,8 +253,8 @@
 			if (sign_option.sig_size)
 				sig_size = sign_option.sig_size;
 
-			Debug("Looking for old signature at 0x%x\n",
-				len - sig_size);
+			VB2_DEBUG("Looking for old signature at 0x%x\n",
+				  len - sig_size);
 
 			if (len < sig_size) {
 				fprintf(stderr, "File is too small\n");
@@ -276,8 +274,8 @@
 		/* Use the same extent again */
 		data_size = old_sig->data_size;
 
-		Debug("Found sig: data_size is 0x%x (%d)\n", data_size,
-		      data_size);
+		VB2_DEBUG("Found sig: data_size is 0x%x (%d)\n", data_size,
+			  data_size);
 	}
 
 	/* Unless overridden */
@@ -294,7 +292,7 @@
 			goto done;
 		}
 	} else {
-		Debug("Private key not provided. Copying previous signature\n");
+		VB2_DEBUG("Private key not provided. Copying previous signature\n");
 		if (!old_sig) {
 			/* This isn't necessary because no prikey mode runs only
 			 * for fmap input or RW input */
@@ -314,12 +312,12 @@
 				tmp_sig->c.total_size, sig_size);
 			goto done;
 		}
-		Debug("Replacing old signature with new one\n");
+		VB2_DEBUG("Replacing old signature with new one\n");
 		memset(old_sig, 0xff, sig_size);
 		memcpy(old_sig, tmp_sig, tmp_sig->c.total_size);
 		if (fmap) {
-			Debug("Writing %s (size=%d)\n",
-			      EC_RW_FILENAME, fmaparea->area_size);
+			VB2_DEBUG("Writing %s (size=%d)\n",
+				  EC_RW_FILENAME, fmaparea->area_size);
 			if (vb2_write_file(EC_RW_FILENAME,
 					   data, fmaparea->area_size))
 				goto done;
@@ -383,7 +381,7 @@
 		new_pubkey = fmap_find_by_name(buf, len, fmap, "KEY_RO",
 					&fmaparea);
 		if (!new_pubkey) {
-			Debug("No KEY_RO in FMAP.\n");
+			VB2_DEBUG("No KEY_RO in FMAP.\n");
 			goto done;
 		}
 		/* Overwrite the old signature */
diff --git a/futility/file_type_usbpd1.c b/futility/file_type_usbpd1.c
index 131e003..d285ff8 100644
--- a/futility/file_type_usbpd1.c
+++ b/futility/file_type_usbpd1.c
@@ -61,10 +61,10 @@
 	if (sign_option.rw_offset != 0xffffffff)
 		rw_offset = sign_option.rw_offset;
 
-	Debug("ro_size     0x%08x\n", ro_size);
-	Debug("ro_offset   0x%08x\n", ro_offset);
-	Debug("rw_size     0x%08x\n", rw_size);
-	Debug("rw_offset   0x%08x\n", rw_offset);
+	VB2_DEBUG("ro_size     0x%08x\n", ro_size);
+	VB2_DEBUG("ro_offset   0x%08x\n", ro_offset);
+	VB2_DEBUG("rw_size     0x%08x\n", rw_size);
+	VB2_DEBUG("rw_offset   0x%08x\n", rw_offset);
 
 	/* Now let's do some sanity checks. */
 	if (ro_size > len || ro_offset > len - ro_size ||
@@ -98,8 +98,7 @@
 	uint32_t rw_offset;
 	uint32_t r;
 
-	Debug("%s(): name %s\n", __func__, name);
-	Debug("%s(): len  0x%08x (%d)\n", __func__, len, len);
+	VB2_DEBUG("%s(): name %s len  0x%08x (%d)\n", name, len, len);
 
 	/* Get image locations */
 	if (!parse_size_opts(len, &ro_size, &rw_size, &ro_offset, &rw_offset))
@@ -131,10 +130,10 @@
 	rw_size -= sig_size;
 	sig_offset = rw_offset + rw_size;
 
-	Debug("rw_size   => 0x%08x\n", rw_size);
-	Debug("rw_offset => 0x%08x\n", rw_offset);
-	Debug("sig_size     0x%08x\n", sig_size);
-	Debug("sig_offset   0x%08x\n", sig_offset);
+	VB2_DEBUG("rw_size   => 0x%08x\n", rw_size);
+	VB2_DEBUG("rw_offset => 0x%08x\n", rw_offset);
+	VB2_DEBUG("sig_size     0x%08x\n", sig_size);
+	VB2_DEBUG("sig_offset   0x%08x\n", sig_offset);
 
 	/* Sign the blob */
 	r = vb21_sign_data(&sig_ptr, buf + rw_offset, rw_size, key_ptr, "Bah");
@@ -171,7 +170,7 @@
 		fprintf(stderr, "Couldn't extract the public key\n");
 		goto done;
 	}
-	Debug("keyb_size is 0x%x (%d):\n", keyb_size, keyb_size);
+	VB2_DEBUG("keyb_size is 0x%x (%d):\n", keyb_size, keyb_size);
 
 	/*
 	 * Of course the packed public key format is different. Why would you
@@ -217,11 +216,11 @@
 	uint32_t dst_ofs_rr = dst_ofs_n + nbytes;
 	uint32_t dst_ofs_n0inv = dst_ofs_rr + nbytes;
 
-	Debug("len 0x%08x ro_size 0x%08x ro_offset 0x%08x\n",
-	       len, ro_size, ro_offset);
-	Debug("pub_size 0x%08x pub_offset 0x%08x nbytes 0x%08x\n",
-	       pub_size, pub_offset, nbytes);
-	Debug("pub_pad 0x%08x\n", pub_pad);
+	VB2_DEBUG("len 0x%08x ro_size 0x%08x ro_offset 0x%08x\n",
+		  len, ro_size, ro_offset);
+	VB2_DEBUG("pub_size 0x%08x pub_offset 0x%08x nbytes 0x%08x\n",
+		  pub_size, pub_offset, nbytes);
+	VB2_DEBUG("pub_pad 0x%08x\n", pub_pad);
 
 	/* Copy n[nwords] */
 	memcpy(buf + dst_ofs_n,
@@ -435,8 +434,7 @@
 	uint32_t ro_size, rw_size, ro_offset, rw_offset;
 	int s, h;
 
-	Debug("%s(): name %s\n", __func__, name);
-	Debug("%s(): len  0x%08x (%d)\n", __func__, len, len);
+	VB2_DEBUG("name %s len  0x%08x (%d)\n", name, len, len);
 
 	/* Get image locations */
 	if (!parse_size_opts(len, &ro_size, &rw_size, &ro_offset, &rw_offset))
diff --git a/futility/futility.c b/futility/futility.c
index 6cea681..f0a42e2 100644
--- a/futility/futility.c
+++ b/futility/futility.c
@@ -220,9 +220,9 @@
 static int run_command(const struct futil_cmd_t *cmd, int argc, char *argv[])
 {
 	int i;
-	Debug("%s(\"%s\") ...\n", __func__, cmd->name);
+	VB2_DEBUG("\"%s\" ...\n", cmd->name);
 	for (i = 0; i < argc; i++)
-		Debug("  argv[%d] = \"%s\"\n", i, argv[i]);
+		VB2_DEBUG("  argv[%d] = \"%s\"\n", i, argv[i]);
 
 	return cmd->handler(argc, argv);
 }
@@ -347,7 +347,7 @@
 		case 0:				/* handled option */
 			break;
 		default:
-			Debug("i=%d\n", i);
+			VB2_DEBUG("i=%d\n", i);
 			DIE;
 		}
 	}
diff --git a/futility/futility.h b/futility/futility.h
index f5f6877..d6f0bac 100644
--- a/futility/futility.h
+++ b/futility/futility.h
@@ -96,7 +96,6 @@
 
 /* Debug output (off by default) */
 extern int debugging_enabled;
-void Debug(const char *format, ...);
 
 /* Returns true if this looks enough like a GBB header to proceed. */
 int futil_looks_like_gbb(GoogleBinaryBlockHeader *gbb, uint32_t len);
diff --git a/futility/misc.c b/futility/misc.c
index af291a4..cef04b0 100644
--- a/futility/misc.c
+++ b/futility/misc.c
@@ -34,14 +34,14 @@
 enum vboot_version vboot_version = VBOOT_VERSION_ALL;
 
 int debugging_enabled;
-void Debug(const char *format, ...)
+void vb2ex_printf(const char *func, const char *format, ...)
 {
 	if (!debugging_enabled)
 		return;
 
 	va_list ap;
 	va_start(ap, format);
-	fprintf(stdout, "DEBUG: ");
+	fprintf(stdout, "DEBUG:%s() ", func);
 	vfprintf(stdout, format, ap);
 	va_end(ap);
 }
@@ -215,7 +215,7 @@
 	pid_t pid;
 	int status;
 
-	Debug("%s(%s, %s)\n", __func__, infile, outfile);
+	VB2_DEBUG("%s -> %s\n", infile, outfile);
 
 	pid = fork();
 
diff --git a/futility/vb1_helper.c b/futility/vb1_helper.c
index 18caf1f..92cdda1 100644
--- a/futility/vb1_helper.c
+++ b/futility/vb1_helper.c
@@ -71,7 +71,7 @@
 
 	if (VB2_SUCCESS != vb2_read_file(config_file, &config_buf, config_size))
 		return NULL;
-	Debug(" config file size=0x%x\n", *config_size);
+	VB2_DEBUG(" config file size=0x%x\n", *config_size);
 	if (CROS_CONFIG_SIZE <= *config_size) {	/* room for trailing '\0' */
 		fprintf(stderr, "Config file %s is too large (>= %d bytes)\n",
 			config_file, CROS_CONFIG_SIZE);
@@ -147,7 +147,7 @@
 	 * a real-mode boot stub. We only want the 32-bit part. */
 	lh = (struct linux_kernel_params *)kernel_buf;
 	if (lh->header != VMLINUZ_HEADER_SIG) {
-		Debug("Not a linux kernel image\n");
+		VB2_DEBUG("Not a linux kernel image\n");
 		return kernel_size;
 	}
 	kernel32_start = (lh->setup_sects + 1) << 9;
@@ -176,7 +176,7 @@
 		 * a real-mode boot stub. We only want the 32-bit part. */
 		lh = (struct linux_kernel_params *)kernel_buf;
 		if (lh->header != VMLINUZ_HEADER_SIG) {
-			Debug("Not a linux kernel image\n");
+			VB2_DEBUG("Not a linux kernel image\n");
 			break;
 		}
 		kernel32_start = (lh->setup_sects + 1) << 9;
@@ -186,8 +186,8 @@
 		}
 		kernel32_size = kernel_size - kernel32_start;
 
-		Debug(" kernel16_start=0x%" PRIx64 "\n", 0);
-		Debug(" kernel16_size=0x%" PRIx64 "\n", kernel32_start);
+		VB2_DEBUG(" kernel16_start=0x%" PRIx64 "\n", 0);
+		VB2_DEBUG(" kernel16_size=0x%" PRIx64 "\n", kernel32_start);
 
 		/* Copy the original zeropage data from kernel_buf into
 		 * g_param_data, then tweak a few fields for our purposes */
@@ -204,10 +204,11 @@
 		params->cmd_line_ptr = kernel_body_load_address +
 			roundup(kernel32_size, CROS_ALIGN) +
 			find_cmdline_start(g_config_data, g_config_size);
-		Debug(" cmdline_addr=0x%x\n", params->cmd_line_ptr);
-		Debug(" version=0x%x\n", params->version);
-		Debug(" kernel_alignment=0x%x\n", params->kernel_alignment);
-		Debug(" relocatable_kernel=0x%x\n", params->relocatable_kernel);
+		VB2_DEBUG(" cmdline_addr=0x%x\n", params->cmd_line_ptr);
+		VB2_DEBUG(" version=0x%x\n", params->version);
+		VB2_DEBUG(" kernel_alignment=0x%x\n", params->kernel_alignment);
+		VB2_DEBUG(" relocatable_kernel=0x%x\n",
+			  params->relocatable_kernel);
 		/* Add a fake e820 memory map with 2 entries. */
 		params->n_e820_entry = 2;
 		params->e820_entries[0].start_addr = 0x00000000;
@@ -221,8 +222,8 @@
 		break;
 	}
 
-	Debug(" kernel32_start=0x%" PRIx64 "\n", kernel32_start);
-	Debug(" kernel32_size=0x%" PRIx64 "\n", kernel32_size);
+	VB2_DEBUG(" kernel32_start=0x%" PRIx64 "\n", kernel32_start);
+	VB2_DEBUG(" kernel32_size=0x%" PRIx64 "\n", kernel32_size);
 
 	/* Keep just the 32-bit kernel. */
 	if (kernel32_size) {
@@ -255,9 +256,9 @@
 		g_vmlinuz_header_size = vmlinuz_header_size;
 		g_vmlinuz_header_data = kernel_blob_data + now;
 
-		Debug("vmlinuz_header_size     = 0x%x\n",
-		      g_vmlinuz_header_size);
-		Debug("vmlinuz_header_ofs      = 0x%x\n", now);
+		VB2_DEBUG("vmlinuz_header_size     = 0x%x\n",
+			  g_vmlinuz_header_size);
+		VB2_DEBUG("vmlinuz_header_ofs      = 0x%x\n", now);
 	}
 
 	/* Where does the bootloader stub begin? */
@@ -268,25 +269,25 @@
 	g_bootloader_data = kernel_blob_data + now;
 	/* TODO: What to do if this is beyond the end of the blob? */
 
-	Debug("bootloader_size     = 0x%x\n", g_bootloader_size);
-	Debug("bootloader_ofs      = 0x%x\n", now);
+	VB2_DEBUG("bootloader_size     = 0x%x\n", g_bootloader_size);
+	VB2_DEBUG("bootloader_ofs      = 0x%x\n", now);
 
 	/* Before that is the params */
 	now -= CROS_PARAMS_SIZE;
 	g_param_size = CROS_PARAMS_SIZE;
 	g_param_data = kernel_blob_data + now;
-	Debug("param_ofs           = 0x%x\n", now);
+	VB2_DEBUG("param_ofs           = 0x%x\n", now);
 
 	/* Before that is the config */
 	now -= CROS_CONFIG_SIZE;
 	g_config_size = CROS_CONFIG_SIZE;
 	g_config_data = kernel_blob_data + now;
-	Debug("config_ofs          = 0x%x\n", now);
+	VB2_DEBUG("config_ofs          = 0x%x\n", now);
 
 	/* The kernel starts at offset 0 and extends up to the config */
 	g_kernel_data = kernel_blob_data;
 	g_kernel_size = now;
-	Debug("kernel_size         = 0x%x\n", g_kernel_size);
+	VB2_DEBUG("kernel_size         = 0x%x\n", g_kernel_size);
 }
 
 
@@ -324,7 +325,7 @@
 
 	/* Sanity-check the keyblock */
 	struct vb2_keyblock *keyblock = (struct vb2_keyblock *)kpart_data;
-	Debug("Keyblock is 0x%x bytes\n", keyblock->keyblock_size);
+	VB2_DEBUG("Keyblock is 0x%x bytes\n", keyblock->keyblock_size);
 	now += keyblock->keyblock_size;
 	if (now > kpart_size) {
 		fprintf(stderr,
@@ -343,7 +344,7 @@
 
 	/* And the preamble */
 	preamble = (struct vb2_kernel_preamble *)(kpart_data + now);
-	Debug("Preamble is 0x%x bytes\n", preamble->preamble_size);
+	VB2_DEBUG("Preamble is 0x%x bytes\n", preamble->preamble_size);
 	now += preamble->preamble_size;
 	if (now > kpart_size) {
 		fprintf(stderr,
@@ -356,14 +357,15 @@
 		return NULL;
 	}
 	/* LGTM */
-	Debug(" kernel_version = %d\n", preamble->kernel_version);
-	Debug(" bootloader_address = 0x%" PRIx64 "\n",
-	      preamble->bootloader_address);
-	Debug(" bootloader_size = 0x%x\n", preamble->bootloader_size);
-	Debug(" kern_blob_size = 0x%x\n", preamble->body_signature.data_size);
+	VB2_DEBUG(" kernel_version = %d\n", preamble->kernel_version);
+	VB2_DEBUG(" bootloader_address = 0x%" PRIx64 "\n",
+		  preamble->bootloader_address);
+	VB2_DEBUG(" bootloader_size = 0x%x\n", preamble->bootloader_size);
+	VB2_DEBUG(" kern_blob_size = 0x%x\n",
+		  preamble->body_signature.data_size);
 
 	uint32_t flags = vb2_kernel_get_flags(preamble);
-	Debug(" flags = 0x%x\n", flags);
+	VB2_DEBUG(" flags = 0x%x\n", flags);
 
 	g_preamble = preamble;
 	g_ondisk_bootloader_addr = g_preamble->bootloader_address;
@@ -372,13 +374,13 @@
 				      &vmlinuz_header_address,
 				      &vmlinuz_header_size);
 	if (vmlinuz_header_size) {
-		Debug(" vmlinuz_header_address = 0x%" PRIx64 "\n",
-		      vmlinuz_header_address);
-		Debug(" vmlinuz_header_size = 0x%x\n", vmlinuz_header_size);
+		VB2_DEBUG(" vmlinuz_header_address = 0x%" PRIx64 "\n",
+			  vmlinuz_header_address);
+		VB2_DEBUG(" vmlinuz_header_size = 0x%x\n", vmlinuz_header_size);
 		g_ondisk_vmlinuz_header_addr = vmlinuz_header_address;
 	}
 
-	Debug("kernel blob is at offset 0x%x\n", now);
+	VB2_DEBUG("kernel blob is at offset 0x%x\n", now);
 	g_kernel_blob_data = kpart_data + now;
 	g_kernel_blob_size = preamble->body_signature.data_size;
 
@@ -460,8 +462,8 @@
 	FILE *f;
 
 	/* Write the output file */
-	Debug("writing %s with 0x%" PRIx64 ", 0x%" PRIx64 "\n",
-	      outfile, part1_size, part2_size);
+	VB2_DEBUG("writing %s with 0x%" PRIx64 ", 0x%" PRIx64 "\n",
+		  outfile, part1_size, part2_size);
 
 	f = fopen(outfile, "wb");
 	if (!f) {
@@ -666,7 +668,7 @@
 		g_param_size                       +
 		g_bootloader_size                  +
 		g_vmlinuz_header_size;
-	Debug("g_kernel_blob_size  0x%" PRIx64 "\n", g_kernel_blob_size);
+	VB2_DEBUG("g_kernel_blob_size  0x%" PRIx64 "\n", g_kernel_blob_size);
 
 	/* Allocate space for the blob. */
 	g_kernel_blob_data = malloc(g_kernel_blob_size);
@@ -674,38 +676,38 @@
 
 	/* Assign the sub-pointers */
 	g_kernel_data = g_kernel_blob_data + now;
-	Debug("g_kernel_size       0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
-	      g_kernel_size, now);
+	VB2_DEBUG("g_kernel_size       0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
+		  g_kernel_size, now);
 	now += roundup(g_kernel_size, CROS_ALIGN);
 
 	g_config_data = g_kernel_blob_data + now;
-	Debug("g_config_size       0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
-	      g_config_size, now);
+	VB2_DEBUG("g_config_size       0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
+		  g_config_size, now);
 	now += g_config_size;
 
 	g_param_data = g_kernel_blob_data + now;
-	Debug("g_param_size        0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
-	      g_param_size, now);
+	VB2_DEBUG("g_param_size        0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
+		  g_param_size, now);
 	now += g_param_size;
 
 	g_bootloader_data = g_kernel_blob_data + now;
-	Debug("g_bootloader_size   0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
-	      g_bootloader_size, now);
+	VB2_DEBUG("g_bootloader_size   0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
+		  g_bootloader_size, now);
 	g_ondisk_bootloader_addr = kernel_body_load_address + now;
-	Debug("g_ondisk_bootloader_addr   0x%" PRIx64 "\n",
-	      g_ondisk_bootloader_addr);
+	VB2_DEBUG("g_ondisk_bootloader_addr   0x%" PRIx64 "\n",
+		  g_ondisk_bootloader_addr);
 	now += g_bootloader_size;
 
 	if (g_vmlinuz_header_size) {
 		g_vmlinuz_header_data = g_kernel_blob_data + now;
-		Debug("g_vmlinuz_header_size 0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
-		      g_vmlinuz_header_size, now);
+		VB2_DEBUG("g_vmlinuz_header_size 0x%" PRIx64 " ofs 0x%" PRIx64 "\n",
+			  g_vmlinuz_header_size, now);
 		g_ondisk_vmlinuz_header_addr = kernel_body_load_address + now;
-		Debug("g_ondisk_vmlinuz_header_addr   0x%" PRIx64 "\n",
-		      g_ondisk_vmlinuz_header_addr);
+		VB2_DEBUG("g_ondisk_vmlinuz_header_addr   0x%" PRIx64 "\n",
+			  g_ondisk_vmlinuz_header_addr);
 	}
 
-	Debug("end of kern_blob at kern_blob+0x%" PRIx64 "\n", now);
+	VB2_DEBUG("end of kern_blob at kern_blob+0x%" PRIx64 "\n", now);
 
 	/* Copy the kernel and params bits into the correct places */
 	if (0 != PickApartVmlinuz(vmlinuz_buf, vmlinuz_size,