Merge "QcomModulePkg: add protocol headfiles used for LE kernel verification"
diff --git a/QcomModulePkg/Include/Library/BootLinux.h b/QcomModulePkg/Include/Library/BootLinux.h
index 4d1e3d3..ef6c532 100644
--- a/QcomModulePkg/Include/Library/BootLinux.h
+++ b/QcomModulePkg/Include/Library/BootLinux.h
@@ -78,6 +78,12 @@
 #define DECOMPRESS_SIZE_FACTOR 8
 #define ALIGNMENT_MASK_4KB 4096
 #define MAX_NUMBER_OF_LOADED_IMAGES 32
+/* Size of the header that is used in case the boot image has
+ * a uncompressed kernel + appended dtb */
+#define PATCHED_KERNEL_HEADER_SIZE 20
+/* String used to determine if the boot image has
+ * a uncompressed kernel + appended dtb */
+#define PATCHED_KERNEL_MAGIC "UNCOMPRESSED_IMG"
 
 typedef VOID (*LINUX_KERNEL) (UINT64 ParametersBase,
                               UINT64 Reserved0,
@@ -117,6 +123,7 @@
   UINT64 RamdiskLoadAddr;
   UINT32 RamdiskSize;
   UINT32 RamdiskOffset;
+  UINT32 PatchedKernelHdrSize;
   CHAR8 *FinalCmdLine;
   CHAR8 *CmdLine;
   BOOLEAN BootingWith32BitKernel;
diff --git a/QcomModulePkg/Library/BootLib/BootLinux.c b/QcomModulePkg/Library/BootLib/BootLinux.c
index 2be5bef..8e569c6 100644
--- a/QcomModulePkg/Library/BootLib/BootLinux.c
+++ b/QcomModulePkg/Library/BootLib/BootLinux.c
@@ -183,10 +183,11 @@
 if (!DtboImgInvalid) {
     // appended device tree
     Dtb = DeviceTreeAppended ((VOID *)(BootParamlistPtr->ImageBuffer +
-                              BootParamlistPtr->PageSize),
-                                BootParamlistPtr->KernelSize,
-                                DtbOffset,
-                                (VOID *)BootParamlistPtr->DeviceTreeLoadAddr);
+                             BootParamlistPtr->PageSize +
+                             BootParamlistPtr->PatchedKernelHdrSize),
+                             BootParamlistPtr->KernelSize,
+                             DtbOffset,
+                             (VOID *)BootParamlistPtr->DeviceTreeLoadAddr);
     if (!Dtb) {
       if (DtbOffset >= BootParamlistPtr->KernelSize) {
         DEBUG ((EFI_D_ERROR, "Dtb offset goes beyond the kernel size\n"));
@@ -229,7 +230,8 @@
      /*It is the case of DTB overlay Get the Soc specific dtb */
       FinalDtbHdr = SocDtb =
       GetSocDtb ((VOID *)(BootParamlistPtr->ImageBuffer +
-                 BootParamlistPtr->PageSize),
+                 BootParamlistPtr->PageSize +
+                 BootParamlistPtr->PatchedKernelHdrSize),
                  BootParamlistPtr->KernelSize,
                  DtbOffset,
                  (VOID *)BootParamlistPtr->DeviceTreeLoadAddr);
@@ -332,9 +334,28 @@
 
     Kptr = (struct kernel64_hdr *)*KernelLoadAddr;
   } else {
-    Kptr = BootParamlistPtr->ImageBuffer + BootParamlistPtr->PageSize;
+    Kptr = (struct kernel64_hdr *)(BootParamlistPtr->ImageBuffer
+                         + BootParamlistPtr->PageSize);
+    DEBUG ((EFI_D_INFO, "Uncompressed kernel in use\n"));
+    /* Patch kernel support only for 64-bit */
+    if (!AsciiStrnCmp ((char*)(BootParamlistPtr->ImageBuffer
+                 + BootParamlistPtr->PageSize), PATCHED_KERNEL_MAGIC,
+                 sizeof (PATCHED_KERNEL_MAGIC) - 1)) {
+      DEBUG ((EFI_D_VERBOSE, "Patched kernel detected\n"));
 
-    /* Uncompress kernel - zImage*/
+      /* The size of the kernel is stored at start of kernel image + 16
+       * The dtb would start just after the kernel */
+      gBS->CopyMem ((VOID *)DtbOffset, (VOID *) (BootParamlistPtr->ImageBuffer
+                 + BootParamlistPtr->PageSize + sizeof (PATCHED_KERNEL_MAGIC)
+                 - 1), sizeof (*DtbOffset));
+
+      BootParamlistPtr->PatchedKernelHdrSize = PATCHED_KERNEL_HEADER_SIZE;
+      Kptr = (struct kernel64_hdr *)((VOID *)Kptr +
+                 BootParamlistPtr->PatchedKernelHdrSize);
+      gBS->CopyMem ((VOID *)*KernelLoadAddr, (VOID *)Kptr,
+                 BootParamlistPtr->KernelSize);
+    }
+
     if (Kptr->magic_64 != KERNEL64_HDR_MAGIC) {
       *KernelLoadAddr =
       (EFI_PHYSICAL_ADDRESS) (BootParamlistPtr->BaseMemory |
diff --git a/QcomModulePkg/Library/FastbootLib/FastbootCmds.c b/QcomModulePkg/Library/FastbootLib/FastbootCmds.c
index bd3181f..b9d6dbd 100644
--- a/QcomModulePkg/Library/FastbootLib/FastbootCmds.c
+++ b/QcomModulePkg/Library/FastbootLib/FastbootCmds.c
@@ -1699,6 +1699,17 @@
   }
 
 out:
+  if (!AsciiStrnCmp (arg, "system", AsciiStrLen ("system")) &&
+    GetAVBVersion () == AVB_1 &&
+    !IsEnforcing () &&
+    (FlashResult == EFI_SUCCESS)) {
+     // reset dm_verity mode to enforcing
+    Status = EnableEnforcingMode (TRUE);
+    if (Status != EFI_SUCCESS) {
+      DEBUG ((EFI_D_ERROR, "failed to update verity mode:  %r\n", Status));
+    }
+  }
+
   LunSet = FALSE;
 }