Make vboot_reference build in MSVC command line environment.

This is a mostly NOOP change which modifies the source code
to compile cleanly in the MSVC command line build
environment.

A new makefile is introduced (msc/nmakefile) along with a
README.txt in the same directory explaining how to build
the code in the DOS window. As of this submission the build
is running in a 32 bit environment, the intention is to use
the same makefile for 64 bit builds in the future.

Enabling high compilation warnings level allowed to
identify a couple of bugs in the code which are being fixed.

Not all sources are being compiled in the MSVC environment,
only those in firmware/ and most of those in test/
subdirectories. The benchmark calculations require porting
of the timer facilities and are being postponed.

TEST

Built in DOS and linux environments. Ran unit tests in
linux environment.

Review URL: http://codereview.chromium.org/2809037
diff --git a/firmware/include/sysincludes.h b/firmware/include/sysincludes.h
index be7e701..ff24e4c 100644
--- a/firmware/include/sysincludes.h
+++ b/firmware/include/sysincludes.h
@@ -26,12 +26,20 @@
 #include <memory.h>
 #endif
 
+#else
+#include "biosincludes.h"
+#endif
+
+#ifndef _MSC_VER
+#define __pragma(...)
+#endif
+
+#if defined (CHROMEOS_ENVIRONMENT) || defined (TARGET_TEST_MODE)
+
 /* 64-bit operations, for platforms where they need to be function calls */
 #define UINT64_RSHIFT(v, shiftby) (((uint64_t)(v)) >> (shiftby))
 #define UINT64_MULT32(v, multby)  (((uint64_t)(v)) * ((uint32_t)(multby)))
 
-#else
-#include "biosincludes.h"
 #endif
 
 #endif  /* VBOOT_REFERENCE_SYSINCLUDES_H_ */
diff --git a/firmware/include/utility.h b/firmware/include/utility.h
index c170938..0e05ee3 100644
--- a/firmware/include/utility.h
+++ b/firmware/include/utility.h
@@ -52,10 +52,10 @@
 int Memcmp(const void* src1, const void* src2, size_t n);
 
 /* Copy [n] bytes from [src] to [dest]. */
-void* Memcpy(void* dest, const void* src, size_t n);
+void* Memcpy(void* dest, const void* src, uint64_t n);
 
 /* Set [n] bytes starting at [s] to [c]. */
-void* Memset(void *dest, const uint8_t c, size_t n);
+void* Memset(void *dest, const uint8_t c, uint64_t n);
 
 /* Compare [n] bytes starting at [s1] with [s2] and return 0 if they match,
  * 1 if they don't. Time taken to perform the comparison is only dependent on
diff --git a/firmware/lib/cgptlib/cgptlib_internal.c b/firmware/lib/cgptlib/cgptlib_internal.c
index 4e0a06a..c3fd1f3 100644
--- a/firmware/lib/cgptlib/cgptlib_internal.c
+++ b/firmware/lib/cgptlib/cgptlib_internal.c
@@ -115,7 +115,7 @@
 }
 
 
-int CheckEntries(GptEntry* entries, GptHeader* h, uint64_t drive_sectors) {
+int CheckEntries(GptEntry* entries, GptHeader* h) {
 
   GptEntry* entry;
   uint32_t crc32;
@@ -227,17 +227,17 @@
    * Note that we use the same header in both checks.  This way we'll
    * catch the case where (header1,entries1) and (header2,entries2)
    * are both valid, but (entries1 != entries2). */
-  if (0 == CheckEntries(entries1, goodhdr, gpt->drive_sectors))
+  if (0 == CheckEntries(entries1, goodhdr))
     gpt->valid_entries |= MASK_PRIMARY;
-  if (0 == CheckEntries(entries2, goodhdr, gpt->drive_sectors))
+  if (0 == CheckEntries(entries2, goodhdr))
     gpt->valid_entries |= MASK_SECONDARY;
 
   /* If both headers are good but neither entries were good, check the
    * entries with the secondary header. */
   if (MASK_BOTH == gpt->valid_headers && !gpt->valid_entries) {
-    if (0 == CheckEntries(entries1, header2, gpt->drive_sectors))
+    if (0 == CheckEntries(entries1, header2))
       gpt->valid_entries |= MASK_PRIMARY;
-    if (0 == CheckEntries(entries2, header2, gpt->drive_sectors))
+    if (0 == CheckEntries(entries2, header2))
       gpt->valid_entries |= MASK_SECONDARY;
     if (gpt->valid_entries) {
       /* Sure enough, header2 had a good CRC for one of the entries.  Mark
diff --git a/firmware/lib/cgptlib/include/cgptlib_internal.h b/firmware/lib/cgptlib/include/cgptlib_internal.h
index 5252b6a..95e5b4e 100644
--- a/firmware/lib/cgptlib/include/cgptlib_internal.h
+++ b/firmware/lib/cgptlib/include/cgptlib_internal.h
@@ -83,7 +83,7 @@
 /* Check entries.
  *
  * Returns 0 if entries are valid, 1 if invalid. */
-int CheckEntries(GptEntry* entries, GptHeader* h, uint64_t drive_sectors);
+int CheckEntries(GptEntry* entries, GptHeader* h);
 
 /* Check GptData, headers, entries.
  *
diff --git a/firmware/lib/cgptlib/include/gpt.h b/firmware/lib/cgptlib/include/gpt.h
index 1d5cb58..b55f921 100644
--- a/firmware/lib/cgptlib/include/gpt.h
+++ b/firmware/lib/cgptlib/include/gpt.h
@@ -12,9 +12,7 @@
 
 #include "sysincludes.h"
 
-#ifdef _MSC_VER
-#pragma pack(push,1) /* Support packing for MSVC. */
-#endif
+__pragma(pack(push,1)) /* Support packing for MSVC. */
 
 #define GPT_HEADER_SIGNATURE "EFI PART"
 #define GPT_HEADER_SIGNATURE_SIZE sizeof(GPT_HEADER_SIGNATURE)
@@ -117,8 +115,6 @@
 
 #define GPTENTRY_EXPECTED_SIZE 128
 
-#ifdef _MSC_VER
-#pragma pack(pop) /* Support packing for MSVC. */
-#endif
+__pragma(pack(pop)) /* Support packing for MSVC. */
 
 #endif  /* VBOOT_REFERENCE_CGPTLIB_GPT_H_ */
diff --git a/firmware/lib/include/vboot_struct.h b/firmware/lib/include/vboot_struct.h
index e6240c7..975d6f6 100644
--- a/firmware/lib/include/vboot_struct.h
+++ b/firmware/lib/include/vboot_struct.h
@@ -11,9 +11,7 @@
 
 #include "sysincludes.h"
 
-#ifdef _MSC_VER
-#pragma pack(push, 1) /* Support packing for MSVC. */
-#endif
+__pragma(pack(push, 1)) /* Support packing for MSVC. */
 
 /* Public key data */
 typedef struct VbPublicKey {
@@ -133,8 +131,6 @@
 
 #define EXPECTED_VBKERNELPREAMBLEHEADER_SIZE 96
 
-#ifdef _MSC_VER
-#pragma pack(pop) /* Support packing for MSVC. */
-#endif
+__pragma(pack(pop)) /* Support packing for MSVC. */
 
 #endif  /* VBOOT_REFERENCE_VBOOT_STRUCT_H_ */
diff --git a/firmware/lib/rollback_index.c b/firmware/lib/rollback_index.c
index ebc721d..4fb7ec9 100644
--- a/firmware/lib/rollback_index.c
+++ b/firmware/lib/rollback_index.c
@@ -17,6 +17,9 @@
 uint16_t g_kernel_key_version = 0;
 uint16_t g_kernel_version = 0;
 
+/* disable MSVC warning on const logical expression (as in } while(0);) */
+__pragma(warning (disable: 4127))
+
 #define RETURN_ON_FAILURE(tpm_command) do {             \
     uint32_t result;                                    \
     if ((result = (tpm_command)) != TPM_SUCCESS) {      \
@@ -369,7 +372,8 @@
   return TlclLockPhysicalPresence();
 }
 
-
+/* disable MSVC warnings on unused arguments */
+__pragma(warning (disable: 4100))
 
 /* NEW APIS!  HELP ME LUIGI, YOU'RE MY ONLY HOPE! */
 
diff --git a/firmware/lib/vboot_kernel.c b/firmware/lib/vboot_kernel.c
index e6658d7..17cd925 100644
--- a/firmware/lib/vboot_kernel.c
+++ b/firmware/lib/vboot_kernel.c
@@ -108,6 +108,8 @@
   return 0;
 }
 
+/* disable MSVC warning on const logical expression (as in } while(0);) */
+__pragma(warning(disable: 4127))
 
 int LoadKernel(LoadKernelParams* params) {
 
diff --git a/firmware/stub/boot_device_stub.c b/firmware/stub/boot_device_stub.c
index c7bb86f..1316e01 100644
--- a/firmware/stub/boot_device_stub.c
+++ b/firmware/stub/boot_device_stub.c
@@ -7,6 +7,9 @@
 
 #include "boot_device.h"
 
+/* disable MSVC warnings on unused arguments */
+__pragma(warning (disable: 4100))
+
 int BootDeviceReadLBA(uint64_t lba_start, uint64_t lba_count, void *buffer) {
   return 1;
 }
diff --git a/firmware/stub/include/biosincludes.h b/firmware/stub/include/biosincludes.h
index 9ed4eab..ac0159d 100644
--- a/firmware/stub/include/biosincludes.h
+++ b/firmware/stub/include/biosincludes.h
@@ -11,4 +11,24 @@
  * CHROMEOS_ENVIRONMENT is not defined at compilation time.
  */
 
+#ifdef TARGET_TEST_MODE
+
+typedef unsigned long long uint64_t;
+typedef long long int64_t;
+typedef unsigned int uint32_t;
+typedef unsigned short uint16_t;
+typedef unsigned char uint8_t;
+typedef unsigned size_t;
+
+#ifndef NULL
+#define NULL ((void*) 0)
+#endif
+
+#define UINT64_C(x) ((uint64_t)x)
+#define __attribute__(x)
+#define PRIu64 "%ll"
+extern void debug(const char *format, ...);
+
+#endif
+
 #endif /*CHROMEOS_SRC_PLATFORM_VBOOT_REFERENCE_FIRMWARE_STUB_BIOSINCLUDES_H_*/
diff --git a/firmware/stub/load_firmware_stub.c b/firmware/stub/load_firmware_stub.c
index 9453856..9d99b4a 100644
--- a/firmware/stub/load_firmware_stub.c
+++ b/firmware/stub/load_firmware_stub.c
@@ -35,9 +35,13 @@
     case 0:
       size = ci->firmwareA_size;
       fw = ci->firmwareA;
+      break;
+
     case 1:
       size = ci->firmwareB_size;
       fw = ci->firmwareB;
+      break;
+
     default:
       /* Anything else is invalid */
       return 1;
@@ -68,6 +72,7 @@
   int rv;
 
   CallerInternal ci;
+  LoadFirmwareParams p;
 
   /* Copy the firmware volume pointers to our global variables. */
   ci.firmwareA = firmwareA;
@@ -78,7 +83,6 @@
   ci.firmwareB_size = 0;
 
   /* Set up the params for LoadFirmware() */
-  LoadFirmwareParams p;
   p.caller_internal = &ci;
   p.firmware_root_key_blob = root_key_blob;
   p.verification_block_0 = verification_headerA;
diff --git a/firmware/stub/tlcl.c b/firmware/stub/tlcl.c
index 23f0f09..cad286a 100644
--- a/firmware/stub/tlcl.c
+++ b/firmware/stub/tlcl.c
@@ -7,6 +7,9 @@
 
 #include "tss_constants.h"
 
+/* disable MSVC warnings on unused arguments */
+__pragma(warning (disable: 4100))
+
 void TlclLibInit(void) { return; }
 uint32_t TlclStartup(void) { return TPM_SUCCESS; }
 uint32_t TlclSelftestfull(void) { return TPM_SUCCESS; }
diff --git a/firmware/stub/utility_stub.c b/firmware/stub/utility_stub.c
index a41e3a3..1445008 100644
--- a/firmware/stub/utility_stub.c
+++ b/firmware/stub/utility_stub.c
@@ -48,13 +48,14 @@
   return memcmp(src1, src2, n);
 }
 
-void* Memcpy(void* dest, const void* src, size_t n) {
-  return memcpy(dest, src, n);
+void* Memcpy(void* dest, const void* src, uint64_t n) {
+  return memcpy(dest, src, (size_t)n);
 }
 
-void* Memset(void* dest, const uint8_t c, size_t n) {
+void* Memset(void* d, const uint8_t c, uint64_t n) {
+  uint8_t *dest = d; /* the only way to keep both cl and gcc happy */
   while (n--) {
-    *((uint8_t*)dest++) = c;
+    *dest++ = c;
   }
   return dest;
 }
diff --git a/firmware/version.c b/firmware/version.c
index faadaba..5fb57f5 100644
--- a/firmware/version.c
+++ b/firmware/version.c
@@ -1 +1 @@
-char* VbootVersion = "VBOOv=c6976ffa";
+char* VbootVersion = "VBOOv=0c991073";