Move all system includes in vboot_firmware to sysincludes.h

This makes it much easier to port vboot_firmware to platforms (such as
UEFI) which don't have the standard include files; only sysincludes.h
needs to be altered.

Review URL: http://codereview.chromium.org/2833012
diff --git a/cgpt/Makefile b/cgpt/Makefile
index 84895a7..e48708a 100644
--- a/cgpt/Makefile
+++ b/cgpt/Makefile
@@ -6,7 +6,7 @@
 LDFLAGS += -luuid
 BUILD_ROOT := ${BUILD}/cgpt
 
-INCLUDES = -I$(FWDIR)/lib/cgptlib/include
+INCLUDES = -I$(FWDIR)/lib/cgptlib/include -I$(FWDIR)/include
 LIBS = ${FWLIB}
 
 DESTDIR ?= /usr/bin
diff --git a/utility/kernel_utility.cc b/utility/kernel_utility.cc
index 00922b7..4940112 100644
--- a/utility/kernel_utility.cc
+++ b/utility/kernel_utility.cc
@@ -20,6 +20,7 @@
 #include "file_keys.h"
 #include "kernel_image.h"
 #include "stateful_util.h"
+#include "utility.h"
 }
 
 using std::cerr;
diff --git a/vboot_firmware/include/boot_device.h b/vboot_firmware/include/boot_device.h
index 47c834f..10303ca 100644
--- a/vboot_firmware/include/boot_device.h
+++ b/vboot_firmware/include/boot_device.h
@@ -8,7 +8,7 @@
 #ifndef VBOOT_REFERENCE_BOOT_DEVICE_H_
 #define VBOOT_REFERENCE_BOOT_DEVICE_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 int BootDeviceReadLBA(uint64_t lba_start, uint64_t lba_count, void *buffer);
 /* Reads lba_count LBA sectors, starting at sector lba_start, from the current
diff --git a/vboot_firmware/include/gbb_header.h b/vboot_firmware/include/gbb_header.h
index 8f34435..18bb64b 100644
--- a/vboot_firmware/include/gbb_header.h
+++ b/vboot_firmware/include/gbb_header.h
@@ -8,7 +8,7 @@
 #ifndef VBOOT_REFERENCE_GBB_HEADER_H_
 #define VBOOT_REFERENCE_GBB_HEADER_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 #define GBB_HEADER_SIZE    (0x80)
 
diff --git a/vboot_firmware/include/load_firmware_fw.h b/vboot_firmware/include/load_firmware_fw.h
index c6693c3..8ba67cf 100644
--- a/vboot_firmware/include/load_firmware_fw.h
+++ b/vboot_firmware/include/load_firmware_fw.h
@@ -9,7 +9,7 @@
 #ifndef VBOOT_REFERENCE_LOAD_FIRMWARE_FW_H_
 #define VBOOT_REFERENCE_LOAD_FIRMWARE_FW_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 /* Recommended size of kernel_sign_key_blob in bytes, for
  * implementations which must preallocate a transfer buffer between
diff --git a/vboot_firmware/include/load_kernel_fw.h b/vboot_firmware/include/load_kernel_fw.h
index bbacbbd..4f86b40 100644
--- a/vboot_firmware/include/load_kernel_fw.h
+++ b/vboot_firmware/include/load_kernel_fw.h
@@ -9,7 +9,7 @@
 #ifndef VBOOT_REFERENCE_LOAD_KERNEL_FW_H_
 #define VBOOT_REFERENCE_LOAD_KERNEL_FW_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 /* Interface provided by verified boot library to BDS */
 
diff --git a/vboot_firmware/include/rollback_index.h b/vboot_firmware/include/rollback_index.h
index 49e2c4f..c359b4b 100644
--- a/vboot_firmware/include/rollback_index.h
+++ b/vboot_firmware/include/rollback_index.h
@@ -9,8 +9,10 @@
 #ifndef VBOOT_REFERENCE_ROLLBACK_INDEX_H_
 #define VBOOT_REFERENCE_ROLLBACK_INDEX_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 
+/* TODO: global variables won't work in the boot stub, since it runs
+   directly out of ROM. */
 extern uint16_t g_firmware_key_version;
 extern uint16_t g_firmware_version;
 extern uint16_t g_kernel_key_version;
diff --git a/vboot_firmware/include/sysincludes.h b/vboot_firmware/include/sysincludes.h
new file mode 100644
index 0000000..5717251
--- /dev/null
+++ b/vboot_firmware/include/sysincludes.h
@@ -0,0 +1,27 @@
+/* Copyright (c) 2010 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.
+ */
+
+/* System includes for vboot reference library.  This is the ONLY
+ * place in vboot_firmware where system headers may be included via
+ * #include <...>, so that there's only one place that needs to be
+ * fixed up for platforms which don't have all the system includes.
+ *
+ * Stub files may still include system headers, because they're local
+ * implementations and will be ported to each system anyway. */
+
+#ifndef VBOOT_REFERENCE_SYSINCLUDES_H_
+#define VBOOT_REFERENCE_SYSINCLUDES_H_
+
+#include <inttypes.h>  /* For PRIu64 */
+#include <stdint.h>
+#include <stdlib.h>
+
+#if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN)
+#include <byteswap.h>
+#include <memory.h>
+#endif
+
+
+#endif  /* VBOOT_REFERENCE_SYSINCLUDES_H_ */
diff --git a/vboot_firmware/include/tlcl.h b/vboot_firmware/include/tlcl.h
index ba1647c..82947f6 100644
--- a/vboot_firmware/include/tlcl.h
+++ b/vboot_firmware/include/tlcl.h
@@ -24,10 +24,7 @@
 #ifndef TPM_LITE_TLCL_H_
 #define TPM_LITE_TLCL_H_
 
-#include <stdarg.h>
-#include <stdint.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include "sysincludes.h"
 
 /* Call this first.
  */
diff --git a/vboot_firmware/include/utility.h b/vboot_firmware/include/utility.h
index 9ce9153..4cfb076 100644
--- a/vboot_firmware/include/utility.h
+++ b/vboot_firmware/include/utility.h
@@ -10,8 +10,7 @@
 #ifndef VBOOT_REFERENCE_UTILITY_H_
 #define VBOOT_REFERENCE_UTILITY_H_
 
-#include <stdint.h>
-#include <string.h>
+#include "sysincludes.h"
 
 /* Outputs an error message and quits. */
 void error(const char *format, ...);
diff --git a/vboot_firmware/lib/cgptlib/crc32.c b/vboot_firmware/lib/cgptlib/crc32.c
index a2bb185..9dacd17 100755
--- a/vboot_firmware/lib/cgptlib/crc32.c
+++ b/vboot_firmware/lib/cgptlib/crc32.c
@@ -40,7 +40,6 @@
 /*                                                                        */
 /*  --------------------------------------------------------------------  */
 #include "crc32.h"
-#include <stdint.h>
 
 static uint32_t crc32_tab[] = {
   0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U,
diff --git a/vboot_firmware/lib/cgptlib/include/cgptlib.h b/vboot_firmware/lib/cgptlib/include/cgptlib.h
index 320b5ed..4eadc81 100644
--- a/vboot_firmware/lib/cgptlib/include/cgptlib.h
+++ b/vboot_firmware/lib/cgptlib/include/cgptlib.h
@@ -6,7 +6,7 @@
 #ifndef VBOOT_REFERENCE_CGPTLIB_H_
 #define VBOOT_REFERENCE_CGPTLIB_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 enum {
   GPT_SUCCESS = 0,
diff --git a/vboot_firmware/lib/cgptlib/include/cgptlib_internal.h b/vboot_firmware/lib/cgptlib/include/cgptlib_internal.h
index b8d85aa..f4a4d19 100644
--- a/vboot_firmware/lib/cgptlib/include/cgptlib_internal.h
+++ b/vboot_firmware/lib/cgptlib/include/cgptlib_internal.h
@@ -6,7 +6,7 @@
 #ifndef VBOOT_REFERENCE_CGPTLIB_INTERNAL_H_
 #define VBOOT_REFERENCE_CGPTLIB_INTERNAL_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 #include "cgptlib.h"
 #include "gpt.h"
 
diff --git a/vboot_firmware/lib/cgptlib/include/crc32.h b/vboot_firmware/lib/cgptlib/include/crc32.h
index 93dec3e..2336113 100644
--- a/vboot_firmware/lib/cgptlib/include/crc32.h
+++ b/vboot_firmware/lib/cgptlib/include/crc32.h
@@ -5,7 +5,7 @@
 #ifndef VBOOT_REFERENCE_GPT_CRC32_H_
 #define VBOOT_REFERENCE_GPT_CRC32_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 uint32_t Crc32(const void *buffer, uint32_t len);
 
diff --git a/vboot_firmware/lib/cgptlib/include/gpt.h b/vboot_firmware/lib/cgptlib/include/gpt.h
index 4ba016c..a65317f 100644
--- a/vboot_firmware/lib/cgptlib/include/gpt.h
+++ b/vboot_firmware/lib/cgptlib/include/gpt.h
@@ -10,7 +10,7 @@
 #ifndef VBOOT_REFERENCE_CGPTLIB_GPT_H_
 #define VBOOT_REFERENCE_CGPTLIB_GPT_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 #define GPT_HEADER_SIGNATURE "EFI PART"
 #define GPT_HEADER_SIGNATURE_SIZE sizeof(GPT_HEADER_SIGNATURE)
diff --git a/vboot_firmware/lib/cryptolib/include/padding.h b/vboot_firmware/lib/cryptolib/include/padding.h
index 8d8fc95..59b8dc3 100644
--- a/vboot_firmware/lib/cryptolib/include/padding.h
+++ b/vboot_firmware/lib/cryptolib/include/padding.h
@@ -10,7 +10,7 @@
 #error "Do not include this file directly. Use cryptolib.h instead."
 #endif
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 extern const uint8_t paddingRSA1024_SHA1[];
 extern const uint8_t paddingRSA1024_SHA256[];
diff --git a/vboot_firmware/lib/cryptolib/include/rsa.h b/vboot_firmware/lib/cryptolib/include/rsa.h
index 4ec5ea9..2d3ee95 100644
--- a/vboot_firmware/lib/cryptolib/include/rsa.h
+++ b/vboot_firmware/lib/cryptolib/include/rsa.h
@@ -10,7 +10,7 @@
 #error "Do not include this file directly. Use cryptolib.h instead."
 #endif
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 #define RSA1024NUMBYTES 128  /* 1024 bit key length */
 #define RSA2048NUMBYTES 256  /* 2048 bit key length */
diff --git a/vboot_firmware/lib/cryptolib/include/sha.h b/vboot_firmware/lib/cryptolib/include/sha.h
index 1686894..46e417d 100644
--- a/vboot_firmware/lib/cryptolib/include/sha.h
+++ b/vboot_firmware/lib/cryptolib/include/sha.h
@@ -12,7 +12,7 @@
 #error "Do not include this file directly. Use cryptolib.h instead."
 #endif
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 #define SHA1_DIGEST_SIZE 20
 #define SHA1_BLOCK_SIZE 64
diff --git a/vboot_firmware/lib/cryptolib/rsa_utility.c b/vboot_firmware/lib/cryptolib/rsa_utility.c
index 2a823a4..f15b97e 100644
--- a/vboot_firmware/lib/cryptolib/rsa_utility.c
+++ b/vboot_firmware/lib/cryptolib/rsa_utility.c
@@ -7,6 +7,7 @@
 
 #include "cryptolib.h"
 #include "stateful_util.h"
+#include "utility.h"
 
 int RSAProcessedKeySize(int algorithm) {
   int key_len = siglen_map[algorithm];  /* Key length in
diff --git a/vboot_firmware/lib/cryptolib/sha1.c b/vboot_firmware/lib/cryptolib/sha1.c
index 41b729b..70653ba 100644
--- a/vboot_firmware/lib/cryptolib/sha1.c
+++ b/vboot_firmware/lib/cryptolib/sha1.c
@@ -16,9 +16,6 @@
 
 #if defined(HAVE_ENDIAN_H) && defined(HAVE_LITTLE_ENDIAN)
 
-#include <byteswap.h>
-#include <memory.h>
-
 /* This version is about 28% faster than the generic version below,
  * but assumes little-endianness.
  */
diff --git a/vboot_firmware/lib/include/stateful_util.h b/vboot_firmware/lib/include/stateful_util.h
index 246aeab..e782ed8 100644
--- a/vboot_firmware/lib/include/stateful_util.h
+++ b/vboot_firmware/lib/include/stateful_util.h
@@ -10,10 +10,7 @@
 #ifndef VBOOT_FIRMWARE_LIB_UTILITY_H_
 #define VBOOT_FIRMWARE_LIB_UTILITY_H_
 
-#include <stdint.h>
-#include <string.h>
-
-#include "utility.h"
+#include "sysincludes.h"
 
 /* Track remaining data to be read in a buffer. */
 typedef struct MemcpyState {
diff --git a/vboot_firmware/lib/include/tss_constants.h b/vboot_firmware/lib/include/tss_constants.h
index ecbdf48..a237148 100644
--- a/vboot_firmware/lib/include/tss_constants.h
+++ b/vboot_firmware/lib/include/tss_constants.h
@@ -18,7 +18,7 @@
 #ifndef TPM_LITE_TSS_CONSTANTS_H_
 #define TPM_LITE_TSS_CONSTANTS_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 #define TPM_MAX_COMMAND_SIZE 4096
 #define TPM_LARGE_ENOUGH_COMMAND_SIZE 256  /* saves space in the firmware */
diff --git a/vboot_firmware/lib/include/vboot_common.h b/vboot_firmware/lib/include/vboot_common.h
index 3c0bfd2..b7998a7 100644
--- a/vboot_firmware/lib/include/vboot_common.h
+++ b/vboot_firmware/lib/include/vboot_common.h
@@ -8,8 +8,6 @@
 #ifndef VBOOT_REFERENCE_VBOOT_COMMON_H_
 #define VBOOT_REFERENCE_VBOOT_COMMON_H_
 
-#include <stdint.h>
-
 #include "cryptolib.h"
 #include "vboot_struct.h"
 
diff --git a/vboot_firmware/lib/include/vboot_kernel.h b/vboot_firmware/lib/include/vboot_kernel.h
index 03b640a..9cb7f02 100644
--- a/vboot_firmware/lib/include/vboot_kernel.h
+++ b/vboot_firmware/lib/include/vboot_kernel.h
@@ -9,8 +9,6 @@
 #ifndef VBOOT_REFERENCE_VBOOT_KERNEL_H_
 #define VBOOT_REFERENCE_VBOOT_KERNEL_H_
 
-#include <stdint.h>
-
 #include "cgptlib.h"
 
 /* Allocates and reads GPT data from the drive.  The sector_bytes and
diff --git a/vboot_firmware/lib/include/vboot_struct.h b/vboot_firmware/lib/include/vboot_struct.h
index 98366fb..a60615c 100644
--- a/vboot_firmware/lib/include/vboot_struct.h
+++ b/vboot_firmware/lib/include/vboot_struct.h
@@ -9,7 +9,7 @@
 #ifndef VBOOT_REFERENCE_VBOOT_STRUCT_H_
 #define VBOOT_REFERENCE_VBOOT_STRUCT_H_
 
-#include <stdint.h>
+#include "sysincludes.h"
 
 
 /* Public key data */
diff --git a/vboot_firmware/lib/rollback_index.c b/vboot_firmware/lib/rollback_index.c
index 1fed860..cb86e89 100644
--- a/vboot_firmware/lib/rollback_index.c
+++ b/vboot_firmware/lib/rollback_index.c
@@ -8,11 +8,9 @@
 
 #include "rollback_index.h"
 
-#include <stdint.h>
-
-#include "utility.h"
 #include "tlcl.h"
 #include "tss_constants.h"
+#include "utility.h"
 
 uint16_t g_firmware_key_version = 0;
 uint16_t g_firmware_version = 0;
diff --git a/vboot_firmware/lib/stateful_util.c b/vboot_firmware/lib/stateful_util.c
index 389961b..137ea77 100644
--- a/vboot_firmware/lib/stateful_util.c
+++ b/vboot_firmware/lib/stateful_util.c
@@ -8,9 +8,7 @@
 
 #include "stateful_util.h"
 
-#include <stdarg.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include "utility.h"
 
 void* StatefulSkip(MemcpyState* state, uint64_t len) {
   if (state->overrun)
diff --git a/vboot_firmware/lib/vboot_kernel.c b/vboot_firmware/lib/vboot_kernel.c
index cdf653d..1440eb4 100644
--- a/vboot_firmware/lib/vboot_kernel.c
+++ b/vboot_firmware/lib/vboot_kernel.c
@@ -8,7 +8,6 @@
 
 #include "vboot_kernel.h"
 
-#include <inttypes.h>  /* For PRIu64 */
 #include "boot_device.h"
 #include "cgptlib.h"
 #include "load_kernel_fw.h"
diff --git a/vboot_firmware/linktest/main.c b/vboot_firmware/linktest/main.c
index 6be093b..e6659a9 100644
--- a/vboot_firmware/linktest/main.c
+++ b/vboot_firmware/linktest/main.c
@@ -1,4 +1,3 @@
-#include <stdio.h>
 
 #include "cgptlib.h"
 #include "load_firmware_fw.h"
diff --git a/vboot_firmware/stub/utility_stub.c b/vboot_firmware/stub/utility_stub.c
index 3a53810..a41e3a3 100644
--- a/vboot_firmware/stub/utility_stub.c
+++ b/vboot_firmware/stub/utility_stub.c
@@ -12,6 +12,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 void error(const char *format, ...) {
   va_list ap;
diff --git a/vkernel/Makefile b/vkernel/Makefile
index 875fc6c..5ada47e 100644
--- a/vkernel/Makefile
+++ b/vkernel/Makefile
@@ -3,6 +3,7 @@
 # found in the LICENSE file.
 
 INCLUDES += -I./include \
+		-I$(FWDIR)/include \
 		-I$(FWDIR)/lib/include \
 		-I$(FWDIR)/lib/cgptlib/include \
 		-I$(FWDIR)/lib/cryptolib/include \
diff --git a/vkernel/kernel_image.c b/vkernel/kernel_image.c
index f78f0f4..392ee1f 100644
--- a/vkernel/kernel_image.c
+++ b/vkernel/kernel_image.c
@@ -20,6 +20,7 @@
 #include "rollback_index.h"
 #include "signature_digest.h"
 #include "stateful_util.h"
+#include "utility.h"
 
 /* Macro to determine the size of a field structure in the KernelImage
  * structure. */