QcomModulePkg: Compile time flag to enable/disable parallel download flash feature

Parallel download and flash feature is enabled by default. However,
we can have a compile time flag to disable/enable it.

Change-Id: I48d378dd08aa653c92d9d6e2ab3c14fd7475e9f5
diff --git a/AndroidBoot.mk b/AndroidBoot.mk
index ba448df..f3f28f6 100644
--- a/AndroidBoot.mk
+++ b/AndroidBoot.mk
@@ -12,6 +12,7 @@
   ABL_USE_SDLLVM := true
 endif
 
+DISABLE_PARALLEL_DOWNLOAD_FLASH := DISABLE_PARALLEL_DOWNLOAD_FLASH=0
 ifeq ($(PRODUCTS.$(INTERNAL_PRODUCT).PRODUCT_SUPPORTS_VERITY),true)
 	VERIFIED_BOOT := VERIFIED_BOOT=1
 else
@@ -81,6 +82,7 @@
 		$(VERIFIED_BOOT_2) \
 		$(VERIFIED_BOOT_LE) \
 		$(USER_BUILD_VARIANT) \
+		$(DISABLE_PARALLEL_DOWNLOAD_FLASH) \
 		CLANG_BIN=$(CLANG_BIN) \
 		CLANG_PREFIX=$(CLANG35_PREFIX)\
 		ABL_USE_SDLLVM=$(ABL_USE_SDLLVM) \
diff --git a/QcomModulePkg/Library/FastbootLib/FastbootCmds.c b/QcomModulePkg/Library/FastbootLib/FastbootCmds.c
index a86c776..8adcac6 100644
--- a/QcomModulePkg/Library/FastbootLib/FastbootCmds.c
+++ b/QcomModulePkg/Library/FastbootLib/FastbootCmds.c
@@ -181,6 +181,18 @@
   VOID *Data;
 } CmdInfo;
 
+#ifdef DISABLE_PARALLEL_DOWNLOAD_FLASH
+BOOLEAN IsDisableParallelDownloadFlash (VOID)
+{
+  return TRUE;
+}
+#else
+BOOLEAN IsDisableParallelDownloadFlash (VOID)
+{
+  return FALSE;
+}
+#endif
+
 /* Clean up memory for the getvar variables during exit */
 STATIC EFI_STATUS FastbootUnInit (VOID)
 {
@@ -1640,7 +1652,8 @@
     PartitionSize = (BlockIo->Media->LastBlock + 1)
                         * (BlockIo->Media->BlockSize);
 
-    if (PartitionSize > MAX_DOWNLOAD_SIZE) {
+    if ((PartitionSize > MAX_DOWNLOAD_SIZE) &&
+         !IsDisableParallelDownloadFlash ()) {
       Status = HandleUsbEventsInTimer ();
       if (EFI_ERROR (Status)) {
         DEBUG ((EFI_D_ERROR, "Failed to handle usb event: %r\n", Status));
@@ -1679,7 +1692,8 @@
   if ((sparse_header->magic != SPARSE_HEADER_MAGIC) ||
         (PartitionSize < MAX_DOWNLOAD_SIZE) ||
         ((PartitionSize > MAX_DOWNLOAD_SIZE) &&
-        (Status != EFI_SUCCESS))) {
+        (IsDisableParallelDownloadFlash () ||
+        (Status != EFI_SUCCESS)))) {
     if (EFI_ERROR (FlashResult)) {
       if (FlashResult == EFI_NOT_FOUND) {
         AsciiSPrint (FlashResultStr, MAX_RSP_SIZE, "(%s) No such partition",
@@ -2602,29 +2616,32 @@
     Size = MAX_FASTBOOT_COMMAND_SIZE;
   Data[Size] = '\0';
 
-  /* Wait for flash finished before next command */
-  if (AsciiStrnCmp (Data, "download", AsciiStrLen ("download"))) {
-    StopUsbTimer ();
-    if (!IsFlashComplete) {
-      Status = AcceptCmdTimerInit (Size, Data);
-      if (Status == EFI_SUCCESS)
-        return;
-    }
-  }
-
   DEBUG ((EFI_D_INFO, "Handling Cmd: %a\n", Data));
 
-  /* Check last flash result */
-  if (FlashResult != EFI_SUCCESS) {
-    AsciiSPrint (FlashResultStr, MAX_RSP_SIZE, "%a : %r",
+  if (!IsDisableParallelDownloadFlash ()) {
+    /* Wait for flash finished before next command */
+    if (AsciiStrnCmp (Data, "download", AsciiStrLen ("download"))) {
+      StopUsbTimer ();
+      if (!IsFlashComplete) {
+        Status = AcceptCmdTimerInit (Size, Data);
+        if (Status == EFI_SUCCESS) {
+          return;
+        }
+      }
+    }
+
+    /* Check last flash result */
+    if (FlashResult != EFI_SUCCESS) {
+      AsciiSPrint (FlashResultStr, MAX_RSP_SIZE, "%a : %r",
                  "Error: Last flash failed", FlashResult);
 
-    DEBUG ((EFI_D_ERROR, "%a\n", FlashResultStr));
-    if (!AsciiStrnCmp (Data, "flash", AsciiStrLen ("flash")) ||
-        !AsciiStrnCmp (Data, "download", AsciiStrLen ("download"))) {
-      FastbootFail (FlashResultStr);
-      FlashResult = EFI_SUCCESS;
-      return;
+      DEBUG ((EFI_D_ERROR, "%a\n", FlashResultStr));
+      if (!AsciiStrnCmp (Data, "flash", AsciiStrLen ("flash")) ||
+          !AsciiStrnCmp (Data, "download", AsciiStrLen ("download"))) {
+        FastbootFail (FlashResultStr);
+        FlashResult = EFI_SUCCESS;
+        return;
+      }
     }
   }
 
diff --git a/QcomModulePkg/QcomModulePkg.dsc b/QcomModulePkg/QcomModulePkg.dsc
index 134d38e..9ed7209 100644
--- a/QcomModulePkg/QcomModulePkg.dsc
+++ b/QcomModulePkg/QcomModulePkg.dsc
@@ -116,6 +116,9 @@
   !if $(BUILD_SYSTEM_ROOT_IMAGE)
       GCC:*_*_*_CC_FLAGS = -DBUILD_SYSTEM_ROOT_IMAGE
   !endif
+  !if $(DISABLE_PARALLEL_DOWNLOAD_FLASH) == 1
+      GCC:*_*_*_CC_FLAGS = -DDISABLE_PARALLEL_DOWNLOAD_FLASH
+  !endif
 
 [PcdsFixedAtBuild.common]
 
diff --git a/makefile b/makefile
index 3f36507..f8da648 100644
--- a/makefile
+++ b/makefile
@@ -90,6 +90,7 @@
 	-D VERIFIED_BOOT_2=$(VERIFIED_BOOT_2) \
 	-D VERIFIED_BOOT_LE=$(VERIFIED_BOOT_LE) \
 	-D USER_BUILD_VARIANT=$(USER_BUILD_VARIANT) \
+	-D DISABLE_PARALLEL_DOWNLOAD_FLASH=$(DISABLE_PARALLEL_DOWNLOAD_FLASH) \
 	-D ENABLE_LE_VARIANT=$(ENABLE_LE_VARIANT) \
 	-D UBSAN_UEFI_GCC_FLAG_UNDEFINED=$(UBSAN_GCC_FLAG_UNDEFINED) \
 	-D UBSAN_UEFI_GCC_FLAG_ALIGNMENT=$(UBSAN_GCC_FLAG_ALIGNMENT) \