vboot: stop using StrnAppend and Uint64ToString

Use snprintf instead.  Remove utility_string library.
Also, prepare VbDisplayDebugInfo to handle 64-byte nvdata.

BUG=b:124141368, chromium:968464
TEST=make clean && make runtests
TEST=boot with 16-byte nvdata, check <TAB> output (one line)
TEST=boot with 64-byte nvdata, check <TAB> output (five lines)
BRANCH=none

Change-Id: If6c4b3a4e9fa7b71cb2d8ca7ccd37e4f36b97fd6
Signed-off-by: Joel Kitching <kitching@google.com>
Cq-Depend: chromium:2122061
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/vboot_reference/+/2104880
Tested-by: Joel Kitching <kitching@chromium.org>
Commit-Queue: Joel Kitching <kitching@chromium.org>
Reviewed-by: Julius Werner <jwerner@chromium.org>
diff --git a/Makefile b/Makefile
index e522050..da3bf56 100644
--- a/Makefile
+++ b/Makefile
@@ -381,7 +381,6 @@
 	firmware/lib/cgptlib/cgptlib_internal.c \
 	firmware/lib/cgptlib/crc32.c \
 	firmware/lib/gpt_misc.c \
-	firmware/lib/utility_string.c \
 	firmware/lib/vboot_api_kernel.c \
 	firmware/lib/vboot_audio.c \
 	firmware/lib/vboot_display.c \
@@ -500,7 +499,6 @@
 	firmware/lib/cgptlib/cgptlib_internal.c \
 	firmware/lib/cgptlib/crc32.c \
 	firmware/lib/gpt_misc.c \
-	firmware/lib/utility_string.c \
 	firmware/stub/tpm_lite_stub.c \
 	firmware/stub/vboot_api_stub.c \
 	firmware/stub/vboot_api_stub_disk.c \
@@ -675,7 +673,6 @@
 	tests/cgptlib_test \
 	tests/sha_benchmark \
 	tests/subprocess_tests \
-	tests/utility_string_tests \
 	tests/vboot_api_kernel4_tests \
 	tests/vboot_api_kernel_tests \
 	tests/vboot_display_tests \
@@ -1203,7 +1200,6 @@
 # tlcl_tests only works when MOCK_TPM is disabled
 	${RUNTEST} ${BUILD_RUN}/tests/tlcl_tests
 endif
-	${RUNTEST} ${BUILD_RUN}/tests/utility_string_tests
 	${RUNTEST} ${BUILD_RUN}/tests/vboot_api_kernel4_tests
 	${RUNTEST} ${BUILD_RUN}/tests/vboot_api_kernel_tests
 	${RUNTEST} ${BUILD_RUN}/tests/vboot_display_tests
diff --git a/cgpt/cgpt_add.c b/cgpt/cgpt_add.c
index e2b0b32..09a86ba 100644
--- a/cgpt/cgpt_add.c
+++ b/cgpt/cgpt_add.c
@@ -3,72 +3,46 @@
  * found in the LICENSE file.
  */
 
+#include <stdio.h>
 #include <string.h>
 
 #include "cgpt.h"
 #include "cgptlib_internal.h"
 #include "cgpt_params.h"
-#include "utility.h"
 #include "vboot_host.h"
 
-static const char* DumpCgptAddParams(const CgptAddParams *params) {
-  static char buf[256];
+static void PrintCgptAddParams(const CgptAddParams *params) {
   char tmp[64];
 
-  buf[0] = 0;
-  snprintf(tmp, sizeof(tmp), "-i %d ", params->partition);
-  StrnAppend(buf, tmp, sizeof(buf));
-  if (params->label) {
-    snprintf(tmp, sizeof(tmp), "-l %s ", params->label);
-    StrnAppend(buf, tmp, sizeof(buf));
-  }
-  if (params->set_begin) {
-    snprintf(tmp, sizeof(tmp), "-b %llu ", (unsigned long long)params->begin);
-    StrnAppend(buf, tmp, sizeof(buf));
-  }
-  if (params->set_size) {
-    snprintf(tmp, sizeof(tmp), "-s %llu ", (unsigned long long)params->size);
-    StrnAppend(buf, tmp, sizeof(buf));
-  }
+  fprintf(stderr, "-i %d ", params->partition);
+  if (params->label)
+    fprintf(stderr, "-l %s ", params->label);
+  if (params->set_begin)
+    fprintf(stderr, "-b %llu ", (unsigned long long)params->begin);
+  if (params->set_size)
+    fprintf(stderr, "-s %llu ", (unsigned long long)params->size);
   if (params->set_type) {
     GuidToStr(&params->type_guid, tmp, sizeof(tmp));
-    StrnAppend(buf, "-t ", sizeof(buf));
-    StrnAppend(buf, tmp, sizeof(buf));
-    StrnAppend(buf, " ", sizeof(buf));
+    fprintf(stderr, "-t %s ", tmp);
   }
   if (params->set_unique) {
     GuidToStr(&params->unique_guid, tmp, sizeof(tmp));
-    StrnAppend(buf, "-u ", sizeof(buf));
-    StrnAppend(buf, tmp, sizeof(buf));
-    StrnAppend(buf, " ", sizeof(buf));
+    fprintf(stderr, "-u %s ", tmp);
   }
-  if (params->set_successful) {
-    snprintf(tmp, sizeof(tmp), "-S %d ", params->successful);
-    StrnAppend(buf, tmp, sizeof(buf));
-  }
-  if (params->set_tries) {
-    snprintf(tmp, sizeof(tmp), "-T %d ", params->tries);
-    StrnAppend(buf, tmp, sizeof(buf));
-  }
-  if (params->set_priority) {
-    snprintf(tmp, sizeof(tmp), "-P %d ", params->priority);
-    StrnAppend(buf, tmp, sizeof(buf));
-  }
-  if (params->set_required) {
-    snprintf(tmp, sizeof(tmp), "-R %d ", params->required);
-    StrnAppend(buf, tmp, sizeof(buf));
-  }
-  if (params->set_legacy_boot) {
-    snprintf(tmp, sizeof(tmp), "-B %d ", params->legacy_boot);
-    StrnAppend(buf, tmp, sizeof(buf));
-  }
-  if (params->set_raw) {
-    snprintf(tmp, sizeof(tmp), "-A %#x ", params->raw_value);
-    StrnAppend(buf, tmp, sizeof(buf));
-  }
+  if (params->set_successful)
+    fprintf(stderr, "-S %d ", params->successful);
+  if (params->set_tries)
+    fprintf(stderr, "-T %d ", params->tries);
+  if (params->set_priority)
+    fprintf(stderr, "-P %d ", params->priority);
+  if (params->set_required)
+    fprintf(stderr, "-R %d ", params->required);
+  if (params->set_legacy_boot)
+    fprintf(stderr, "-B %d ", params->legacy_boot);
+  if (params->set_raw)
+    fprintf(stderr, "-A %#x ", params->raw_value);
 
-  StrnAppend(buf, "\n", sizeof(buf));
-  return buf;
+  fprintf(stderr, "\n");
 }
 
 // This is the implementation-specific helper function.
@@ -295,7 +269,8 @@
     // If the modified entry is illegal, recover it and return error.
     memcpy(entry, &backup, sizeof(*entry));
     Error("%s\n", GptErrorText(rv));
-    Error(DumpCgptAddParams(params));
+    Error("");
+    PrintCgptAddParams(params);
     return -1;
   }
 
diff --git a/firmware/2lib/include/2sysincludes.h b/firmware/2lib/include/2sysincludes.h
index c23054a..0671772 100644
--- a/firmware/2lib/include/2sysincludes.h
+++ b/firmware/2lib/include/2sysincludes.h
@@ -17,6 +17,7 @@
 #include <inttypes.h>  /* For PRIu64 */
 #include <stddef.h>
 #include <stdint.h>
+#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
diff --git a/firmware/include/vboot_api.h b/firmware/include/vboot_api.h
index 7e14134..d3e0581 100644
--- a/firmware/include/vboot_api.h
+++ b/firmware/include/vboot_api.h
@@ -85,7 +85,7 @@
 				  VbSelectAndLoadKernelParams *kparams);
 
 /*****************************************************************************/
-/* Timer and delay (first two from utility.h) */
+/* Timer and delay */
 
 #define VB_USEC_PER_MSEC	1000ULL
 #define VB_MSEC_PER_SEC		VB_USEC_PER_MSEC
diff --git a/firmware/lib/cgptlib/cgptlib.c b/firmware/lib/cgptlib/cgptlib.c
index db704b5..2cc77b4 100644
--- a/firmware/lib/cgptlib/cgptlib.c
+++ b/firmware/lib/cgptlib/cgptlib.c
@@ -9,7 +9,6 @@
 #include "cgptlib_internal.h"
 #include "crc32.h"
 #include "gpt.h"
-#include "utility.h"
 #include "vboot_api.h"
 
 int GptInit(GptData *gpt)
diff --git a/firmware/lib/cgptlib/cgptlib_internal.c b/firmware/lib/cgptlib/cgptlib_internal.c
index 7d214a9..593b1e2 100644
--- a/firmware/lib/cgptlib/cgptlib_internal.c
+++ b/firmware/lib/cgptlib/cgptlib_internal.c
@@ -9,7 +9,6 @@
 #include "crc32.h"
 #include "gpt.h"
 #include "gpt_misc.h"
-#include "utility.h"
 
 const static int MIN_SECTOR_SIZE = 512;
 
diff --git a/firmware/lib/gpt_misc.c b/firmware/lib/gpt_misc.c
index 6d91ec1..ae7f8a2 100644
--- a/firmware/lib/gpt_misc.c
+++ b/firmware/lib/gpt_misc.c
@@ -9,7 +9,6 @@
 #include "cgptlib_internal.h"
 #include "crc32.h"
 #include "gpt.h"
-#include "utility.h"
 #include "vboot_api.h"
 
 /**
diff --git a/firmware/lib/include/utility.h b/firmware/lib/include/utility.h
deleted file mode 100644
index bca4982..0000000
--- a/firmware/lib/include/utility.h
+++ /dev/null
@@ -1,39 +0,0 @@
-/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- *
- * Helper functions/wrappers for memory allocations, manipulation and
- * comparison.
- */
-
-#ifndef VBOOT_REFERENCE_UTILITY_H_
-#define VBOOT_REFERENCE_UTILITY_H_
-
-#include "2common.h"
-#include "2sysincludes.h"
-#include "vboot_api.h"
-
-/*
- * Buffer size required to hold the longest possible output of Uint64ToString()
- * - that is, Uint64ToString(~0, 2).
- */
-#define UINT64_TO_STRING_MAX 65
-
-/**
- * Convert a value to a string in the specified radix (2=binary, 10=decimal,
- * 16=hex) and store it in <buf>, which is <bufsize> chars long.  If
- * <zero_pad_width>, left-pads the string to at least that width with '0'.
- * Returns the length of the stored string, not counting the terminating null.
- */
-uint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value,
-			uint32_t radix, uint32_t zero_pad_width);
-
-/**
- * Concatenate <src> onto <dest>, which has space for <destlen> characters
- * including the terminating null.  Note that <dest> will always be
- * null-terminated if <destlen> > 0.  Returns the number of characters used in
- * <dest>, not counting the terminating null.
- */
-uint32_t StrnAppend(char *dest, const char *src, uint32_t destlen);
-
-#endif  /* VBOOT_REFERENCE_UTILITY_H_ */
diff --git a/firmware/lib/tpm2_lite/marshaling.c b/firmware/lib/tpm2_lite/marshaling.c
index 90ce04c..ef3606b 100644
--- a/firmware/lib/tpm2_lite/marshaling.c
+++ b/firmware/lib/tpm2_lite/marshaling.c
@@ -6,7 +6,6 @@
 #include "2common.h"
 #include "2sysincludes.h"
 #include "tpm2_marshaling.h"
-#include "utility.h"
 
 static uint16_t tpm_tag;  /* Depends on the command type. */
 static int ph_disabled;   /* Platform hierarchy disabled. */
diff --git a/firmware/lib/tpm2_lite/tlcl.c b/firmware/lib/tpm2_lite/tlcl.c
index dca8fac..9bacbb6 100644
--- a/firmware/lib/tpm2_lite/tlcl.c
+++ b/firmware/lib/tpm2_lite/tlcl.c
@@ -10,7 +10,7 @@
 #include "2sysincludes.h"
 #include "tlcl.h"
 #include "tpm2_marshaling.h"
-#include "utility.h"
+#include "vboot_api.h"
 
 /*
  * TODO(chromium:1032930): Originally accessed by including secdata_tpm.h.
diff --git a/firmware/lib/tpm_lite/mocked_tlcl.c b/firmware/lib/tpm_lite/mocked_tlcl.c
index d0f6120..838b5e7 100644
--- a/firmware/lib/tpm_lite/mocked_tlcl.c
+++ b/firmware/lib/tpm_lite/mocked_tlcl.c
@@ -6,7 +6,6 @@
 #include "2sysincludes.h"
 #include "tlcl.h"
 #include "tlcl_internal.h"
-#include "utility.h"
 #include "vboot_api.h"
 
 uint32_t TlclLibInit(void)
diff --git a/firmware/lib/tpm_lite/tlcl.c b/firmware/lib/tpm_lite/tlcl.c
index b6685bf..28c0bb6 100644
--- a/firmware/lib/tpm_lite/tlcl.c
+++ b/firmware/lib/tpm_lite/tlcl.c
@@ -21,7 +21,6 @@
 #include "tlcl.h"
 #include "tlcl_internal.h"
 #include "tlcl_structures.h"
-#include "utility.h"
 #include "vboot_api.h"
 
 /* Sets the size field of a TPM command. */
diff --git a/firmware/lib/utility_string.c b/firmware/lib/utility_string.c
deleted file mode 100644
index 299bbaa..0000000
--- a/firmware/lib/utility_string.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- *
- * String utility functions that need to be built as part of the firmware.
- */
-
-#include "2sysincludes.h"
-#include "utility.h"
-
-uint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value,
-			uint32_t radix, uint32_t zero_pad_width)
-{
-	char ibuf[UINT64_TO_STRING_MAX];
-	char *s;
-	uint32_t usedsize = 1;
-
-	if (!buf)
-		return 0;
-
-	/* Clear output buffer in case of error */
-	*buf = '\0';
-
-	/* Sanity-check input args */
-	if (radix < 2 || radix > 36 || zero_pad_width >= UINT64_TO_STRING_MAX)
-		return 0;
-
-	/* Start at end of string and work backwards */
-	s = ibuf + UINT64_TO_STRING_MAX - 1;
-	*(s) = '\0';
-	do {
-		int v = value % radix;
-		value /= radix;
-
-		*(--s) = (char)(v < 10 ? v + '0' : v + 'a' - 10);
-		if (++usedsize > bufsize)
-			return 0;  /* Result won't fit in buffer */
-	} while (value);
-
-	/* Zero-pad if necessary */
-	while (usedsize <= zero_pad_width) {
-		*(--s) = '0';
-		if (++usedsize > bufsize)
-			return 0;  /* Result won't fit in buffer */
-	}
-
-	/* Now copy the string back to the input buffer. */
-	memcpy(buf, s, usedsize);
-
-	/* Don't count the terminating null in the bytes used */
-	return usedsize - 1;
-}
-
-uint32_t StrnAppend(char *dest, const char *src, uint32_t destlen)
-{
-	uint32_t used = 0;
-
-	if (!dest || !src || !destlen)
-		return 0;
-
-	/* Skip past existing string in destination.*/
-	while (dest[used] && used < destlen - 1)
-		used++;
-
-	/* Now copy source */
-	while (*src && used < destlen - 1)
-		dest[used++] = *src++;
-
-	/* Terminate destination and return count of non-null characters */
-	dest[used] = 0;
-	return used;
-}
diff --git a/firmware/lib/vboot_api_kernel.c b/firmware/lib/vboot_api_kernel.c
index 4f86046..6111eee 100644
--- a/firmware/lib/vboot_api_kernel.c
+++ b/firmware/lib/vboot_api_kernel.c
@@ -15,7 +15,6 @@
 #include "2sysincludes.h"
 #include "2ui.h"
 #include "load_kernel_fw.h"
-#include "utility.h"
 #include "vb2_common.h"
 #include "vboot_api.h"
 #include "vboot_kernel.h"
diff --git a/firmware/lib/vboot_audio.c b/firmware/lib/vboot_audio.c
index 20b1437..5e44606 100644
--- a/firmware/lib/vboot_audio.c
+++ b/firmware/lib/vboot_audio.c
@@ -8,7 +8,6 @@
 #include "2common.h"
 #include "2misc.h"
 #include "2sysincludes.h"
-#include "utility.h"
 #include "vboot_api.h"
 #include "vboot_audio.h"
 
diff --git a/firmware/lib/vboot_display.c b/firmware/lib/vboot_display.c
index 258619f..07e1da0 100644
--- a/firmware/lib/vboot_display.c
+++ b/firmware/lib/vboot_display.c
@@ -10,7 +10,6 @@
 #include "2nvstorage.h"
 #include "2sha.h"
 #include "2sysincludes.h"
-#include "utility.h"
 #include "vboot_api.h"
 #include "vboot_display.h"
 #include "vboot_kernel.h"
@@ -216,7 +215,12 @@
 	return "Unknown or deprecated error code";
 }
 
-#define DEBUG_INFO_SIZE 512
+#define DEBUG_INFO_SIZE 1024
+#define DEBUG_INFO_APPEND(format, args...) do { \
+	if (used < DEBUG_INFO_SIZE) \
+		used += snprintf(buf + used, DEBUG_INFO_SIZE - used, format, \
+				 ## args); \
+} while (0)
 
 vb2_error_t VbDisplayDebugInfo(struct vb2_context *ctx)
 {
@@ -225,7 +229,7 @@
 	struct vb2_workbuf wb;
 	char buf[DEBUG_INFO_SIZE] = "";
 	char sha1sum[VB2_SHA1_DIGEST_SIZE * 2 + 1];
-	uint32_t used = 0;
+	int32_t used = 0;
 	vb2_error_t ret;
 	uint32_t i;
 
@@ -238,83 +242,53 @@
 		ret = vb2api_gbb_read_hwid(ctx, hwid, &size);
 		if (ret)
 			strcpy(hwid, "{INVALID}");
-		used += StrnAppend(buf + used, "HWID: ",
-				   DEBUG_INFO_SIZE - used);
-		used += StrnAppend(buf + used, hwid, DEBUG_INFO_SIZE - used);
+		DEBUG_INFO_APPEND("HWID: %s", hwid);
 	}
 
 	/* Add recovery reason and subcode */
 	i = vb2_nv_get(ctx, VB2_NV_RECOVERY_SUBCODE);
-	used += StrnAppend(buf + used,
-			"\nrecovery_reason: 0x", DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
-			       sd->recovery_reason, 16, 2);
-	used += StrnAppend(buf + used, " / 0x", DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 16, 2);
-	used += StrnAppend(buf + used, "  ", DEBUG_INFO_SIZE - used);
-	used += StrnAppend(buf + used,
-			RecoveryReasonString(sd->recovery_reason),
-			DEBUG_INFO_SIZE - used);
+	DEBUG_INFO_APPEND("\nrecovery_reason: %#.2x / %#.2x  %s",
+			  sd->recovery_reason, i,
+			  RecoveryReasonString(sd->recovery_reason));
 
 	/* Add vb2_context and vb2_shared_data flags */
-	used += StrnAppend(buf + used, "\ncontext.flags: 0x",
-			   DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
-			       ctx->flags, 16, 16);
-	used += StrnAppend(buf + used, "\nshared_data.flags: 0x",
-			   DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
-			       sd->flags, 16, 8);
-	used += StrnAppend(buf + used, "\nshared_data.status: 0x",
-			   DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
-			       sd->status, 16, 8);
+	DEBUG_INFO_APPEND("\ncontext.flags: %#.16" PRIx64, ctx->flags);
+	DEBUG_INFO_APPEND("\nshared_data.flags: %#.8x", sd->flags);
+	DEBUG_INFO_APPEND("\nshared_data.status: %#.8x", sd->status);
 
-	/* Add raw contents of VbNvStorage */
-	used += StrnAppend(buf + used, "\nVbNv.raw:", DEBUG_INFO_SIZE - used);
+	/* Add raw contents of nvdata */
+	DEBUG_INFO_APPEND("\nnvdata:");
+	if (vb2_nv_get_size(ctx) > 16)  /* Multi-line starts on next line */
+		DEBUG_INFO_APPEND("\n  ");
 	for (i = 0; i < vb2_nv_get_size(ctx); i++) {
-		used += StrnAppend(buf + used, " ", DEBUG_INFO_SIZE - used);
-		used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
-				       ctx->nvdata[i], 16, 2);
+		/* Split into 16-byte blocks */
+		if (i > 0 && i % 16 == 0)
+			DEBUG_INFO_APPEND("\n  ");
+		DEBUG_INFO_APPEND(" %02x", ctx->nvdata[i]);
 	}
 
 	/* Add dev_boot_usb flag */
 	i = vb2_nv_get(ctx, VB2_NV_DEV_BOOT_USB);
-	used += StrnAppend(buf + used, "\ndev_boot_usb: ", DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
+	DEBUG_INFO_APPEND("\ndev_boot_usb: %d", i);
 
 	/* Add dev_boot_legacy flag */
 	i = vb2_nv_get(ctx, VB2_NV_DEV_BOOT_LEGACY);
-	used += StrnAppend(buf + used,
-			"\ndev_boot_legacy: ", DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
+	DEBUG_INFO_APPEND("\ndev_boot_legacy: %d", i);
 
 	/* Add dev_default_boot flag */
 	i = vb2_nv_get(ctx, VB2_NV_DEV_DEFAULT_BOOT);
-	used += StrnAppend(buf + used,
-			"\ndev_default_boot: ", DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
+	DEBUG_INFO_APPEND("\ndev_default_boot: %d", i);
 
 	/* Add dev_boot_signed_only flag */
 	i = vb2_nv_get(ctx, VB2_NV_DEV_BOOT_SIGNED_ONLY);
-	used += StrnAppend(buf + used, "\ndev_boot_signed_only: ",
-			DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used, i, 10, 0);
+	DEBUG_INFO_APPEND("\ndev_boot_signed_only: %d", i);
 
 	/* Add TPM versions */
-	used += StrnAppend(buf + used,
-			   "\nTPM: fwver=0x", DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
-			       sd->fw_version_secdata, 16, 8);
-	used += StrnAppend(buf + used, " kernver=0x", DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
-			       sd->kernel_version_secdata, 16, 8);
+	DEBUG_INFO_APPEND("\nTPM: fwver=%#.8x kernver=%#.8x",
+			  sd->fw_version_secdata, sd->kernel_version_secdata);
 
 	/* Add GBB flags */
-	used += StrnAppend(buf + used,
-			   "\ngbb.flags: 0x", DEBUG_INFO_SIZE - used);
-	used += Uint64ToString(buf + used, DEBUG_INFO_SIZE - used,
-			       gbb->flags, 16, 8);
+	DEBUG_INFO_APPEND("\ngbb.flags: %#.8x", gbb->flags);
 
 	/* Add sha1sum for Root & Recovery keys */
 	{
@@ -323,10 +297,7 @@
 		ret = vb2_gbb_read_root_key(ctx, &key, NULL, &wblocal);
 		if (!ret) {
 			FillInSha1Sum(sha1sum, key);
-			used += StrnAppend(buf + used, "\ngbb.rootkey: ",
-					   DEBUG_INFO_SIZE - used);
-			used += StrnAppend(buf + used, sha1sum,
-					   DEBUG_INFO_SIZE - used);
+			DEBUG_INFO_APPEND("\ngbb.rootkey: %s", sha1sum);
 		}
 	}
 
@@ -336,10 +307,7 @@
 		ret = vb2_gbb_read_recovery_key(ctx, &key, NULL, &wblocal);
 		if (!ret) {
 			FillInSha1Sum(sha1sum, key);
-			used += StrnAppend(buf + used, "\ngbb.recovery_key: ",
-					   DEBUG_INFO_SIZE - used);
-			used += StrnAppend(buf + used, sha1sum,
-					   DEBUG_INFO_SIZE - used);
+			DEBUG_INFO_APPEND("\ngbb.recovery_key: %s", sha1sum);
 		}
 	}
 
@@ -349,13 +317,11 @@
 		struct vb2_packed_key *key =
 			vb2_member_of(sd, sd->kernel_key_offset);
 		FillInSha1Sum(sha1sum, key);
-		used += StrnAppend(buf + used,
-				"\nkernel_subkey: ", DEBUG_INFO_SIZE - used);
-		used += StrnAppend(buf + used, sha1sum, DEBUG_INFO_SIZE - used);
+		DEBUG_INFO_APPEND("\nkernel_subkey: %s", sha1sum);
 	}
 
 	/* Make sure we finish with a newline */
-	used += StrnAppend(buf + used, "\n", DEBUG_INFO_SIZE - used);
+	DEBUG_INFO_APPEND("\n");
 
 	/* TODO: add more interesting data:
 	 * - Information on current disks */
diff --git a/firmware/lib/vboot_kernel.c b/firmware/lib/vboot_kernel.c
index 8118599..7420a69 100644
--- a/firmware/lib/vboot_kernel.c
+++ b/firmware/lib/vboot_kernel.c
@@ -17,7 +17,6 @@
 #include "cgptlib_internal.h"
 #include "gpt_misc.h"
 #include "load_kernel_fw.h"
-#include "utility.h"
 #include "vb2_common.h"
 #include "vboot_api.h"
 #include "vboot_kernel.h"
diff --git a/firmware/lib/vboot_ui_legacy_clamshell.c b/firmware/lib/vboot_ui_legacy_clamshell.c
index 959981d..c510d58 100644
--- a/firmware/lib/vboot_ui_legacy_clamshell.c
+++ b/firmware/lib/vboot_ui_legacy_clamshell.c
@@ -13,7 +13,6 @@
 #include "2sysincludes.h"
 #include "load_kernel_fw.h"
 #include "tlcl.h"
-#include "utility.h"
 #include "vb2_common.h"
 #include "vboot_api.h"
 #include "vboot_audio.h"
diff --git a/firmware/lib/vboot_ui_legacy_menu.c b/firmware/lib/vboot_ui_legacy_menu.c
index b0cfb43..48eaabe9 100644
--- a/firmware/lib/vboot_ui_legacy_menu.c
+++ b/firmware/lib/vboot_ui_legacy_menu.c
@@ -12,7 +12,6 @@
 #include "2secdata.h"
 #include "2sysincludes.h"
 #include "load_kernel_fw.h"
-#include "utility.h"
 #include "vb2_common.h"
 #include "vboot_api.h"
 #include "vboot_audio.h"
diff --git a/firmware/stub/tpm_lite_stub.c b/firmware/stub/tpm_lite_stub.c
index 5fd673f..54accb5 100644
--- a/firmware/stub/tpm_lite_stub.c
+++ b/firmware/stub/tpm_lite_stub.c
@@ -23,7 +23,6 @@
 #include "2sysincludes.h"
 #include "tlcl.h"
 #include "tlcl_internal.h"
-#include "utility.h"
 #include "vboot_api.h"
 
 #define TPM_DEVICE_PATH "/dev/tpm0"
diff --git a/futility/cmd_update.c b/futility/cmd_update.c
index 70e699a..e83095f 100644
--- a/futility/cmd_update.c
+++ b/futility/cmd_update.c
@@ -11,7 +11,6 @@
 
 #include "futility.h"
 #include "updater.h"
-#include "utility.h"
 
 enum {
 	OPT_DUMMY = 0x100,
diff --git a/host/arch/x86/lib/crossystem_arch.c b/host/arch/x86/lib/crossystem_arch.c
index a57fcde..eba9661 100644
--- a/host/arch/x86/lib/crossystem_arch.c
+++ b/host/arch/x86/lib/crossystem_arch.c
@@ -21,7 +21,6 @@
 #include "crossystem.h"
 #include "crossystem_vbnv.h"
 #include "host_common.h"
-#include "utility.h"
 #include "vboot_struct.h"
 
 /* ACPI constants from Chrome OS Main Processor Firmware Spec */
diff --git a/host/lib/crossystem.c b/host/lib/crossystem.c
index 2ba21e6..cf7600e 100644
--- a/host/lib/crossystem.c
+++ b/host/lib/crossystem.c
@@ -17,7 +17,6 @@
 #include "crossystem_vbnv.h"
 #include "host_common.h"
 #include "subprocess.h"
-#include "utility.h"
 #include "vboot_struct.h"
 
 /* Filename for kernel command line */
diff --git a/host/lib/host_common.c b/host/lib/host_common.c
index 00f9100..63b45f2 100644
--- a/host/lib/host_common.c
+++ b/host/lib/host_common.c
@@ -12,7 +12,6 @@
 #include "2sysincludes.h"
 #include "host_common.h"
 #include "host_key21.h"
-#include "utility.h"
 #include "vb2_common.h"
 
 struct vb2_fw_preamble *vb2_create_fw_preamble(
diff --git a/host/lib/include/host_common.h b/host/lib/include/host_common.h
index 5fcc5c5..43cb025 100644
--- a/host/lib/include/host_common.h
+++ b/host/lib/include/host_common.h
@@ -13,7 +13,6 @@
 #include "host_keyblock.h"
 #include "host_misc.h"
 #include "host_signature.h"
-#include "utility.h"
 #include "vboot_api.h"
 #include "vboot_struct.h"
 
diff --git a/host/lib/include/host_misc.h b/host/lib/include/host_misc.h
index 1aed20a..9759d44 100644
--- a/host/lib/include/host_misc.h
+++ b/host/lib/include/host_misc.h
@@ -8,7 +8,6 @@
 #ifndef VBOOT_REFERENCE_HOST_MISC_H_
 #define VBOOT_REFERENCE_HOST_MISC_H_
 
-#include "utility.h"
 #include "vboot_struct.h"
 
 /* Copy up to dest_size-1 characters from src to dest, ensuring null
diff --git a/host/lib/include/host_signature.h b/host/lib/include/host_signature.h
index 682840c..72160fd 100644
--- a/host/lib/include/host_signature.h
+++ b/host/lib/include/host_signature.h
@@ -9,7 +9,6 @@
 #define VBOOT_REFERENCE_HOST_SIGNATURE_H_
 
 #include "host_key.h"
-#include "utility.h"
 #include "vboot_struct.h"
 
 struct vb2_private_key;
diff --git a/tests/cgptlib_test.c b/tests/cgptlib_test.c
index ee7cb9d..e522592 100644
--- a/tests/cgptlib_test.c
+++ b/tests/cgptlib_test.c
@@ -13,7 +13,6 @@
 #include "crc32_test.h"
 #include "gpt.h"
 #include "test_common.h"
-#include "utility.h"
 
 /*
  * Testing partition layout (sector_bytes=512)
diff --git a/tests/crc32_test.c b/tests/crc32_test.c
index aeb48b3..1d1d8cd 100644
--- a/tests/crc32_test.c
+++ b/tests/crc32_test.c
@@ -7,7 +7,6 @@
 #include "crc32.h"
 #include "crc32_test.h"
 #include "test_common.h"
-#include "utility.h"
 
 #define MAX_VECTOR_LEN 256
 
diff --git a/tests/tpm_lite/readonly.c b/tests/tpm_lite/readonly.c
index c3940e1..68de06b 100644
--- a/tests/tpm_lite/readonly.c
+++ b/tests/tpm_lite/readonly.c
@@ -14,7 +14,6 @@
 #include <stdlib.h>
 
 #include "tlcl.h"
-#include "utility.h"
 
 /* These index values are used to create NVRAM spaces.  They only need to be
  * unique.
diff --git a/tests/tpm_lite/tpmtest_earlynvram.c b/tests/tpm_lite/tpmtest_earlynvram.c
index d5ff77f..db44862 100644
--- a/tests/tpm_lite/tpmtest_earlynvram.c
+++ b/tests/tpm_lite/tpmtest_earlynvram.c
@@ -12,7 +12,6 @@
 
 #include "tlcl.h"
 #include "tlcl_tests.h"
-#include "utility.h"
 
 #define INDEX0 0xcafe
 
diff --git a/tests/tpm_lite/tpmtest_earlynvram2.c b/tests/tpm_lite/tpmtest_earlynvram2.c
index 88f6a6b..9307a58 100644
--- a/tests/tpm_lite/tpmtest_earlynvram2.c
+++ b/tests/tpm_lite/tpmtest_earlynvram2.c
@@ -12,7 +12,6 @@
 
 #include "tlcl.h"
 #include "tlcl_tests.h"
-#include "utility.h"
 
 int main(int argc, char** argv) {
 	uint32_t x;
diff --git a/tests/tpm_lite/tpmtest_testsetup.c b/tests/tpm_lite/tpmtest_testsetup.c
index ce36099..a6331d9 100644
--- a/tests/tpm_lite/tpmtest_testsetup.c
+++ b/tests/tpm_lite/tpmtest_testsetup.c
@@ -12,7 +12,6 @@
 
 #include "tlcl.h"
 #include "tlcl_tests.h"
-#include "utility.h"
 
 int main(int argc, char** argv) {
 	uint32_t perm;
diff --git a/tests/tpm_lite/tpmtest_timing.c b/tests/tpm_lite/tpmtest_timing.c
index 1ec14a2..eaba52f 100644
--- a/tests/tpm_lite/tpmtest_timing.c
+++ b/tests/tpm_lite/tpmtest_timing.c
@@ -16,7 +16,6 @@
 
 #include "tlcl.h"
 #include "tlcl_tests.h"
-#include "utility.h"
 
 /* Runs [op] and ensures it returns success and doesn't run longer than
  * [time_limit] in milliseconds.
diff --git a/tests/utility_string_tests.c b/tests/utility_string_tests.c
deleted file mode 100644
index d41cbe8..0000000
--- a/tests/utility_string_tests.c
+++ /dev/null
@@ -1,123 +0,0 @@
-/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- *
- * Tests for string utility functions.
- */
-
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "test_common.h"
-#include "utility.h"
-
-/* Test string concatenation */
-static void StrncatTest(void) {
-	char dest[128];
-
-	/* Null inputs */
-	TEST_EQ(0, StrnAppend(dest, NULL, sizeof(dest)),
-		"StrnAppend('', null)");
-	TEST_EQ(0, StrnAppend(NULL, "Hey!", sizeof(dest)),
-		"StrnAppend(null, '')");
-
-	/* Empty <-- empty */
-	*dest = 0;
-	TEST_EQ(0, StrnAppend(dest, "", sizeof(dest)), "StrnAppend('', '')");
-	TEST_EQ(0, strcmp(dest, ""), "StrnAppend('', '') result");
-
-	/* Nonempty <-- empty */
-	strcpy(dest, "Bob");
-	TEST_EQ(3, StrnAppend(dest, "", sizeof(dest)), "StrnAppend(B, '')");
-	TEST_EQ(0, strcmp(dest, "Bob"), "StrnAppend(B, '') result");
-
-	/* Empty <-- nonempty */
-	*dest = 0;
-	TEST_EQ(5, StrnAppend(dest, "Alice", sizeof(dest)),
-		"StrnAppend('', A)");
-	TEST_EQ(0, strcmp(dest, "Alice"), "StrnAppend('', A) result");
-
-	/* Nonempty <-- nonempty */
-	strcpy(dest, "Tigre");
-	TEST_EQ(10, StrnAppend(dest, "Bunny", sizeof(dest)),
-		"StrnAppend(T, B)");
-	TEST_EQ(0, strcmp(dest, "TigreBunny"), "StrnAppend(T, B) result");
-
-	/* Test clipping */
-	strcpy(dest, "YesI");
-	TEST_EQ(7, StrnAppend(dest, "Can't", 8), "StrnAppend(Y, over)");
-	TEST_EQ(0, strcmp(dest, "YesICan"), "StrnAppend(Y, over) result");
-
-	/* Test clipping if dest already overflows its claimed length */
-	strcpy(dest, "BudgetDeficit");
-	TEST_EQ(6, StrnAppend(dest, "Spending", 7), "StrnAppend(over, over)");
-	TEST_EQ(0, strcmp(dest, "Budget"), "StrnAppend(over, over) result");
-}
-
-
-static void TestU64ToS(uint64_t value, uint32_t radix, uint32_t zero_pad_width,
-		       const char *expect) {
-	char dest[UINT64_TO_STRING_MAX];
-
-	TEST_EQ(strlen(expect),
-		Uint64ToString(dest, sizeof(dest), value, radix,
-			       zero_pad_width),
-		"Uint64ToString");
-	printf("Uint64ToString expect %s got %s\n", expect, dest);
-	TEST_EQ(0, strcmp(dest, expect), "Uint64ToString result");
-}
-
-
-/* Test uint64 to string conversion */
-static void Uint64ToStringTest(void) {
-	char dest[UINT64_TO_STRING_MAX];
-
-	/* Test invalid inputs */
-	TEST_EQ(0, Uint64ToString(NULL, 8, 123, 10, 8),
-		"Uint64ToString null dest");
-	TestU64ToS(0, 1, 0, "");
-	TestU64ToS(0, 37, 0, "");
-
-	/* Binary */
-	TestU64ToS(0, 2, 0, "0");
-	TestU64ToS(0x9A, 2, 0, "10011010");
-	TestU64ToS(0x71, 2, 12, "000001110001");
-	TestU64ToS(~0ULL, 2, 0,
-	"1111111111111111111111111111111111111111111111111111111111111111");
-
-	/* Decimal */
-	TestU64ToS(0, 10, 0, "0");
-	TestU64ToS(12345, 10, 0, "12345");
-	TestU64ToS(67890, 10, 8, "00067890");
-	TestU64ToS(~0ULL, 10, 0, "18446744073709551615");
-
-	/* Hex */
-	TestU64ToS(0, 16, 0, "0");
-	TestU64ToS(0x12345678, 16, 0, "12345678");
-	TestU64ToS(0x9ABCDEF, 16, 8, "09abcdef");
-	TestU64ToS(~0ULL, 16, 0, "ffffffffffffffff");
-
-	/* Zero pad corner cases */
-	/* Don't pad if over length */
-	TestU64ToS(0x1234567890ULL, 16, 8, "1234567890");
-	/* Fail if padding won't fit in buffer */
-	TEST_EQ(0, Uint64ToString(dest, 8, 123, 10, 8),
-		"Uint64ToString bad pad");
-	TEST_EQ(0, strcmp(dest, ""), "Uint64ToString bad pad result");
-
-}
-
-
-int main(int argc, char* argv[]) {
-	int error_code = 0;
-
-	StrncatTest();
-	Uint64ToStringTest();
-
-	if (!gTestSuccess)
-		error_code = 255;
-
-	return error_code;
-}
diff --git a/tests/vb20_rsa_padding_tests.c b/tests/vb20_rsa_padding_tests.c
index be9d91b..8a0d58a 100644
--- a/tests/vb20_rsa_padding_tests.c
+++ b/tests/vb20_rsa_padding_tests.c
@@ -12,7 +12,6 @@
 #include "host_key.h"
 #include "rsa_padding_test.h"
 #include "test_common.h"
-#include "utility.h"
 #include "vb2_common.h"
 
 /**
diff --git a/tests/vb2_rsa_utility_tests.c b/tests/vb2_rsa_utility_tests.c
index 989b401..cc856e8 100644
--- a/tests/vb2_rsa_utility_tests.c
+++ b/tests/vb2_rsa_utility_tests.c
@@ -12,7 +12,6 @@
 #include "file_keys.h"
 #include "rsa_padding_test.h"
 #include "test_common.h"
-#include "utility.h"
 #include "vboot_api.h"
 #include "vboot_test.h"
 
diff --git a/tests/vboot_api_kernel_tests.c b/tests/vboot_api_kernel_tests.c
index d7c3235..bcb5d04 100644
--- a/tests/vboot_api_kernel_tests.c
+++ b/tests/vboot_api_kernel_tests.c
@@ -11,7 +11,6 @@
 #include "2sysincludes.h"
 #include "load_kernel_fw.h"
 #include "test_common.h"
-#include "utility.h"
 #include "vboot_api.h"
 #include "vboot_kernel.h"
 #include "vboot_test.h"
diff --git a/tests/verify_kernel.c b/tests/verify_kernel.c
index 28813c4..9aaad72 100644
--- a/tests/verify_kernel.c
+++ b/tests/verify_kernel.c
@@ -5,12 +5,9 @@
  * Routines for verifying a kernel or disk image
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
 #include "2sysincludes.h"
 #include "2api.h"
+#include "2common.h"
 #include "2misc.h"
 #include "2nvstorage.h"
 #include "host_common.h"
diff --git a/utility/load_kernel_test.c b/utility/load_kernel_test.c
index 609a9a3..c5dc7cb 100644
--- a/utility/load_kernel_test.c
+++ b/utility/load_kernel_test.c
@@ -11,6 +11,7 @@
 #include <unistd.h>
 
 #include "2api.h"
+#include "2common.h"
 #include "2misc.h"
 #include "2sysincludes.h"
 #include "host_common.h"