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/tests/cgptlib_test.c b/tests/cgptlib_test.c
index 29f91d8..5b0b9b4 100644
--- a/tests/cgptlib_test.c
+++ b/tests/cgptlib_test.c
@@ -11,6 +11,7 @@
 #include "crc32.h"
 #include "crc32_test.h"
 #include "gpt.h"
+#include "test_common.h"
 #include "utility.h"
 
 /* Testing partition layout (sector_bytes=512)
@@ -560,12 +561,12 @@
 
   /* Modify the first byte of primary entries, and expect the CRC is wrong. */
   BuildTestGptData(gpt);
-  EXPECT(0 == CheckEntries(e1, h1, gpt->drive_sectors));
-  EXPECT(0 == CheckEntries(e2, h1, gpt->drive_sectors));
+  EXPECT(0 == CheckEntries(e1, h1));
+  EXPECT(0 == CheckEntries(e2, h1));
   gpt->primary_entries[0] ^= 0xa5;  /* just XOR a non-zero value */
   gpt->secondary_entries[TOTAL_ENTRIES_SIZE-1] ^= 0x5a;
-  EXPECT(1 == CheckEntries(e1, h1, gpt->drive_sectors));
-  EXPECT(1 == CheckEntries(e2, h1, gpt->drive_sectors));
+  EXPECT(1 == CheckEntries(e1, h1));
+  EXPECT(1 == CheckEntries(e2, h1));
 
   return TEST_OK;
 }
@@ -586,26 +587,26 @@
   BuildTestGptData(gpt);
   e1[0].starting_lba = h1->first_usable_lba - 1;
   RefreshCrc32(gpt);
-  EXPECT(1 == CheckEntries(e1, h1, gpt->drive_sectors));
+  EXPECT(1 == CheckEntries(e1, h1));
 
   /* error case: entry.EndingLBA > header.LastUsableLBA */
   BuildTestGptData(gpt);
   e1[2].ending_lba = h1->last_usable_lba + 1;
   RefreshCrc32(gpt);
-  EXPECT(1 == CheckEntries(e1, h1, gpt->drive_sectors));
+  EXPECT(1 == CheckEntries(e1, h1));
 
   /* error case: entry.StartingLBA > entry.EndingLBA */
   BuildTestGptData(gpt);
   e1[3].starting_lba = e1[3].ending_lba + 1;
   RefreshCrc32(gpt);
-  EXPECT(1 == CheckEntries(e1, h1, gpt->drive_sectors));
+  EXPECT(1 == CheckEntries(e1, h1));
 
   /* case: non active entry should be ignored. */
   BuildTestGptData(gpt);
   Memset(&e1[1].type, 0, sizeof(e1[1].type));
   e1[1].starting_lba = e1[1].ending_lba + 1;
   RefreshCrc32(gpt);
-  EXPECT(0 == CheckEntries(e1, h1, gpt->drive_sectors));
+  EXPECT(0 == CheckEntries(e1, h1));
 
   return TEST_OK;
 }
@@ -667,7 +668,7 @@
     }
     RefreshCrc32(gpt);
 
-    EXPECT(cases[i].overlapped == CheckEntries(e, h, gpt->drive_sectors));
+    EXPECT(cases[i].overlapped == CheckEntries(e, h));
   }
   return TEST_OK;
 }
@@ -823,39 +824,39 @@
   GptData* gpt = GetEmptyGptData();
   GptEntry* e = (GptEntry*)(gpt->primary_entries);
 
-  e->attrs.whole = 0x0000000000000000LLU;
+  e->attrs.whole = 0x0000000000000000ULL;
   SetEntrySuccessful(e, 1);
-  EXPECT(0x0100000000000000LLU == e->attrs.whole);
+  EXPECT(0x0100000000000000ULL == e->attrs.whole);
   EXPECT(1 == GetEntrySuccessful(e));
-  e->attrs.whole = 0xFFFFFFFFFFFFFFFFLLU;
+  e->attrs.whole = 0xFFFFFFFFFFFFFFFFULL;
   SetEntrySuccessful(e, 0);
-  EXPECT(0xFEFFFFFFFFFFFFFFLLU == e->attrs.whole);
+  EXPECT(0xFEFFFFFFFFFFFFFFULL == e->attrs.whole);
   EXPECT(0 == GetEntrySuccessful(e));
 
-  e->attrs.whole = 0x0000000000000000LLU;
+  e->attrs.whole = 0x0000000000000000ULL;
   SetEntryTries(e, 15);
   EXPECT(15 == GetEntryTries(e));
-  EXPECT(0x00F0000000000000LLU == e->attrs.whole);
-  e->attrs.whole = 0xFFFFFFFFFFFFFFFFLLU;
+  EXPECT(0x00F0000000000000ULL == e->attrs.whole);
+  e->attrs.whole = 0xFFFFFFFFFFFFFFFFULL;
   SetEntryTries(e, 0);
-  EXPECT(0xFF0FFFFFFFFFFFFFLLU == e->attrs.whole);
+  EXPECT(0xFF0FFFFFFFFFFFFFULL == e->attrs.whole);
   EXPECT(0 == GetEntryTries(e));
 
-  e->attrs.whole = 0x0000000000000000LLU;
+  e->attrs.whole = 0x0000000000000000ULL;
   SetEntryPriority(e, 15);
-  EXPECT(0x000F000000000000LLU == e->attrs.whole);
+  EXPECT(0x000F000000000000ULL == e->attrs.whole);
   EXPECT(15 == GetEntryPriority(e));
-  e->attrs.whole = 0xFFFFFFFFFFFFFFFFLLU;
+  e->attrs.whole = 0xFFFFFFFFFFFFFFFFULL;
   SetEntryPriority(e, 0);
-  EXPECT(0xFFF0FFFFFFFFFFFFLLU == e->attrs.whole);
+  EXPECT(0xFFF0FFFFFFFFFFFFULL == e->attrs.whole);
   EXPECT(0 == GetEntryPriority(e));
 
-  e->attrs.whole = 0xFFFFFFFFFFFFFFFFLLU;
+  e->attrs.whole = 0xFFFFFFFFFFFFFFFFULL;
   EXPECT(1 == GetEntrySuccessful(e));
   EXPECT(15 == GetEntryPriority(e));
   EXPECT(15 == GetEntryTries(e));
 
-  e->attrs.whole = 0x0123000000000000LLU;
+  e->attrs.whole = 0x0123000000000000ULL;
   EXPECT(1 == GetEntrySuccessful(e));
   EXPECT(2 == GetEntryTries(e));
   EXPECT(3 == GetEntryPriority(e));
@@ -1091,6 +1092,8 @@
   return TEST_OK;
 }
 
+/* disable MSVC warnings on unused arguments */
+__pragma(warning (disable: 4100))
 
 int main(int argc, char *argv[]) {
   int i;
diff --git a/tests/cgptlib_test.h b/tests/cgptlib_test.h
index 9812e77..5f8b042 100644
--- a/tests/cgptlib_test.h
+++ b/tests/cgptlib_test.h
@@ -6,6 +6,7 @@
 #define VBOOT_REFERENCE_CGPTLIB_TEST_H_
 
 #include <stdio.h>
+#include "sysincludes.h"
 
 enum {
   TEST_FAIL = -1,
@@ -13,14 +14,12 @@
 };
 
 #define TEST_CASE(func) #func, func
-typedef int (*test_func)(void);
+typedef int (*test_func)();
 
 #define ARRAY_SIZE(array) (sizeof(array) / sizeof(array[0]))
 
-/* ANSI Color coding sequences. */
-#define COL_GREEN "\e[1;32m"
-#define COL_RED "\e[0;31m"
-#define COL_STOP "\e[m"
+/* disable MSVC warning on const logical expression (as in } while(0);) */
+__pragma(warning (disable: 4127))
 
 #define EXPECT(expr) \
   do { \
diff --git a/tests/crc32_test.c b/tests/crc32_test.c
index bb7e460..01eb5ac 100644
--- a/tests/crc32_test.c
+++ b/tests/crc32_test.c
@@ -6,6 +6,7 @@
 #include "crc32_test.h"
 #include "cgptlib_test.h"
 #include "crc32.h"
+#include "test_common.h"
 #include "utility.h"
 
 #define MAX_VECTOR_LEN 256
diff --git a/tests/rollback_index_mock.c b/tests/rollback_index_mock.c
index 6c62993..f12402b 100644
--- a/tests/rollback_index_mock.c
+++ b/tests/rollback_index_mock.c
@@ -8,7 +8,6 @@
 #include "rollback_index.h"
 #include "tss_constants.h"
 
-#include <stdint.h>
 #include <stdio.h>
 
 uint16_t g_firmware_key_version = 0;
@@ -16,6 +15,9 @@
 uint16_t g_kernel_key_version = 0;
 uint16_t g_kernel_version = 0;
 
+/* disable MSVC warnings on unused arguments */
+__pragma(warning (disable: 4100))
+
 uint32_t SetupTPM(int mode, int developer_flag) {
 #ifndef NDEBUG
   debug("Rollback Index Library Mock: TPM initialized.\n");
diff --git a/tests/sha_tests.c b/tests/sha_tests.c
index 2b75a03..65cbb46 100644
--- a/tests/sha_tests.c
+++ b/tests/sha_tests.c
@@ -78,6 +78,9 @@
   return success;
 }
 
+/* disable MSVC warnings on unused arguments */
+__pragma(warning (disable: 4100))
+
 int main(int argc, char* argv[]) {
   int success = 1;
   /* Initialize long_msg with 'a' x 1,000,000 */
diff --git a/tests/test_common.c b/tests/test_common.c
index e53f596..e7e5493 100644
--- a/tests/test_common.c
+++ b/tests/test_common.c
@@ -13,11 +13,6 @@
 #include "file_keys.h"
 #include "utility.h"
 
-/* ANSI Color coding sequences. */
-#define COL_GREEN "\e[1;32m"
-#define COL_RED "\e[0;31m"
-#define COL_STOP "\e[m"
-
 /* Global test success flag. */
 int gTestSuccess = 1;
 
diff --git a/tests/test_common.h b/tests/test_common.h
index d62071f..ed3e603 100644
--- a/tests/test_common.h
+++ b/tests/test_common.h
@@ -7,8 +7,6 @@
 #ifndef VBOOT_REFERENCE_TEST_COMMON_H_
 #define VBOOT_REFERENCE_TEST_COMMON_H_
 
-#include <stdint.h>
-
 extern int gTestSuccess;
 
 /* Return 1 if result is equal to expected_result, else return 0.
@@ -18,4 +16,12 @@
  * Also update the global gTestSuccess flag if test fails. */
 int TEST_NEQ(int result, int not_expected_result, char* testname);
 
+/* ANSI Color coding sequences.
+ *
+ * Don't use \e as MSC does not recognize it as a valid escape sequence.
+ */
+#define COL_GREEN "\x1b[1;32m"
+#define COL_RED "\x1b[0;31m"
+#define COL_STOP "\x1b[m"
+
 #endif  /* VBOOT_REFERENCE_TEST_COMMON_H_ */
diff --git a/tests/timer_utils.h b/tests/timer_utils.h
index 95eb8c7..f0d6b29 100644
--- a/tests/timer_utils.h
+++ b/tests/timer_utils.h
@@ -6,7 +6,10 @@
 #ifndef VBOOT_REFERENCE_TIMER_UTILS_H_
 #define VBOOT_REFERENCE_TIMER_UTILS_H_
 
+#ifndef _MSC_VER
 #include <inttypes.h>
+#endif
+
 #include <time.h>
 
 typedef struct ClockTimer {
diff --git a/tests/vboot_common2_tests.c b/tests/vboot_common2_tests.c
index b4c9096..9fee525 100644
--- a/tests/vboot_common2_tests.c
+++ b/tests/vboot_common2_tests.c
@@ -34,7 +34,8 @@
   rsa = PublicKeyToRSA(orig_key);
   TEST_NEQ((size_t)rsa, 0, "PublicKeyToRSA() ok");
   if (rsa) {
-    TEST_EQ(rsa->algorithm, key->algorithm, "PublicKeyToRSA() algorithm");
+    TEST_EQ((int)rsa->algorithm, (int)key->algorithm,
+            "PublicKeyToRSA() algorithm");
     RSAPublicKeyFree(rsa);
   }
 }
@@ -77,7 +78,7 @@
 
   sig = CalculateSignature(test_data, sizeof(test_data), private_key);
   rsa = PublicKeyToRSA(public_key);
-  digest = DigestBuf(test_data, sizeof(test_data), public_key->algorithm);
+  digest = DigestBuf(test_data, sizeof(test_data), (int)public_key->algorithm);
   TEST_NEQ(sig && rsa && digest, 0, "VerifyData() prerequisites");
   if (!sig || !rsa || !digest)
     return;
@@ -109,7 +110,7 @@
   VbKernelPreambleHeader *hdr;
   VbKernelPreambleHeader *h;
   RSAPublicKey* rsa;
-  uint64_t hsize;
+  unsigned hsize;
 
   /* Create a dummy signature */
   VbSignature *body_sig = SignatureAlloc(56, 78);
@@ -120,7 +121,7 @@
   TEST_NEQ(hdr && rsa, 0, "VerifyKernelPreamble2() prerequisites");
   if (!hdr)
     return;
-  hsize = hdr->preamble_size;
+  hsize = (unsigned) hdr->preamble_size;
   h = (VbKernelPreambleHeader*)Malloc(hsize + 16384);
 
   TEST_EQ(VerifyKernelPreamble2(hdr, hsize, rsa), 0,
diff --git a/tests/vboot_common3_tests.c b/tests/vboot_common3_tests.c
index e094d63..ff75906 100644
--- a/tests/vboot_common3_tests.c
+++ b/tests/vboot_common3_tests.c
@@ -31,13 +31,13 @@
 
   VbKeyBlockHeader *hdr;
   VbKeyBlockHeader *h;
-  uint64_t hsize;
+  unsigned hsize;
 
   hdr = KeyBlockCreate(data_key, private_key, 0x1234);
   TEST_NEQ((size_t)hdr, 0, "KeyBlockVerify() prerequisites");
   if (!hdr)
     return;
-  hsize = hdr->key_block_size;
+  hsize = (unsigned) hdr->key_block_size;
   h = (VbKeyBlockHeader*)Malloc(hsize + 1024);
 
   TEST_EQ(KeyBlockVerify(hdr, hsize, NULL), 0,
@@ -149,7 +149,7 @@
   VbFirmwarePreambleHeader *hdr;
   VbFirmwarePreambleHeader *h;
   RSAPublicKey* rsa;
-  uint64_t hsize;
+  unsigned hsize;
 
   /* Create a dummy signature */
   VbSignature *body_sig = SignatureAlloc(56, 78);
@@ -159,7 +159,7 @@
   TEST_NEQ(hdr && rsa, 0, "VerifyFirmwarePreamble2() prerequisites");
   if (!hdr)
     return;
-  hsize = hdr->preamble_size;
+  hsize = (unsigned) hdr->preamble_size;
   h = (VbFirmwarePreambleHeader*)Malloc(hsize + 16384);
 
   TEST_EQ(VerifyFirmwarePreamble2(hdr, hsize, rsa), 0,
diff --git a/tests/vboot_common_tests.c b/tests/vboot_common_tests.c
index e707daf..d01b2f9 100644
--- a/tests/vboot_common_tests.c
+++ b/tests/vboot_common_tests.c
@@ -31,24 +31,24 @@
 
   {
     uint8_t p[1];
-    TEST_EQ(OffsetOf(p, p), 0, "OffsetOf() equal");
-    TEST_EQ(OffsetOf(p, p+10), 10, "OffsetOf() positive");
-    TEST_EQ(OffsetOf(p, p+0x12345678), 0x12345678, "OffsetOf() large");
+    TEST_EQ((int)OffsetOf(p, p), 0, "OffsetOf() equal");
+    TEST_EQ((int)OffsetOf(p, p+10), 10, "OffsetOf() positive");
+    TEST_EQ((int)OffsetOf(p, p+0x12345678), 0x12345678, "OffsetOf() large");
   }
 
   {
     VbPublicKey k = {sizeof(k), 2, 3, 4};
-    TEST_EQ(OffsetOf(&k, GetPublicKeyData(&k)), sizeof(k),
+    TEST_EQ((int)OffsetOf(&k, GetPublicKeyData(&k)), sizeof(k),
             "GetPublicKeyData() adjacent");
-    TEST_EQ(OffsetOf(&k, GetPublicKeyDataC(&k)), sizeof(k),
+    TEST_EQ((int)OffsetOf(&k, GetPublicKeyDataC(&k)), sizeof(k),
             "GetPublicKeyDataC() adjacent");
   }
 
   {
     VbPublicKey k = {123, 2, 3, 4};
-    TEST_EQ(OffsetOf(&k, GetPublicKeyData(&k)), 123,
+    TEST_EQ((int)OffsetOf(&k, GetPublicKeyData(&k)), 123,
             "GetPublicKeyData() spaced");
-    TEST_EQ(OffsetOf(&k, GetPublicKeyDataC(&k)), 123,
+    TEST_EQ((int)OffsetOf(&k, GetPublicKeyDataC(&k)), 123,
             "GetPublicKeyDataC() spaced");
   }
 
@@ -64,7 +64,7 @@
             "MemberInside member too big");
     TEST_EQ(VerifyMemberInside(p, 20, p, 4, 21, 0), 1,
             "MemberInside data after parent");
-    TEST_EQ(VerifyMemberInside(p, 20, p, 4, -1, 0), 1,
+    TEST_EQ(VerifyMemberInside(p, 20, p, 4, (uint64_t)-1, 0), 1,
             "MemberInside data before parent");
     TEST_EQ(VerifyMemberInside(p, 20, p, 4, 4, 17), 1,
             "MemberInside data too big");
@@ -101,6 +101,8 @@
 
 }
 
+/* disable MSVC warnings on unused arguments */
+__pragma(warning (disable: 4100))
 
 int main(int argc, char* argv[]) {
   int error_code = 0;