]> git.proxmox.com Git - mirror_edk2.git/commitdiff
CapsulePei coalesce need add more sanity check for each Capsule Fragment.
authorjyao1 <jyao1>
Fri, 17 Jan 2014 03:51:01 +0000 (03:51 +0000)
committerjyao1 <jyao1@6f19259b-4bc3-4df7-8a09-765794883524>
Fri, 17 Jan 2014 03:51:01 +0000 (03:51 +0000)
Signed-off by: jiewen yao <jiewen.yao@intel.com>
Reviewed by: elvin li <elvin.li@intel.com>

git-svn-id: https://svn.code.sf.net/p/edk2/code/trunk/edk2@15137 6f19259b-4bc3-4df7-8a09-765794883524

MdeModulePkg/Universal/CapsulePei/Common/CapsuleCoalesce.c
MdeModulePkg/Universal/CapsulePei/Common/CommonHeader.h
MdeModulePkg/Universal/CapsulePei/UefiCapsule.c

index 7ed15ef2afd9987ad44ec5663c2a662035639393..507f862de7baf656199d4cf60847b077b010b217 100644 (file)
@@ -9,7 +9,7 @@
   CapsuleDataCoalesce() will do basic validation before coalesce capsule data\r
   into memory.\r
 \r
-Copyright (c) 2011 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -30,15 +30,9 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #include <Library/PrintLib.h>\r
 #include <Library/BaseLib.h>\r
 \r
-#define MIN_COALESCE_ADDR                     (1024 * 1024)\r
-#define MAX_SUPPORT_CAPSULE_NUM               50\r
-\r
-#define EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('C', 'a', 'p', 'D')\r
+#include "CommonHeader.h"\r
 \r
-typedef struct {\r
-  UINT32  Signature;\r
-  UINT32  CapsuleSize;\r
-} EFI_CAPSULE_PEIM_PRIVATE_DATA;\r
+#define MIN_COALESCE_ADDR                     (1024 * 1024)\r
 \r
 /**\r
   Given a pointer to the capsule block list, info on the available system\r
@@ -87,10 +81,11 @@ ValidateCapsuleIntegrity (
   they are relocated into memory to turn them into a contiguous (null\r
   terminated) array.\r
 \r
-  @param PeiServices pointer to PEI services table\r
-  @param BlockList   pointer to the capsule block descriptors\r
-  @param MemBase     base of system memory in which we can work\r
-  @param MemSize     size of the system memory pointed to by MemBase\r
+  @param PeiServices    pointer to PEI services table\r
+  @param BlockList      pointer to the capsule block descriptors\r
+  @param NumDescriptors number of capsule data block descriptors, whose Length is non-zero.\r
+  @param MemBase        base of system memory in which we can work\r
+  @param MemSize        size of the system memory pointed to by MemBase\r
 \r
   @retval NULL    could not relocate the descriptors\r
   @retval Pointer to the base of the successfully-relocated block descriptors. \r
@@ -100,6 +95,7 @@ EFI_CAPSULE_BLOCK_DESCRIPTOR *
 RelocateBlockDescriptors (\r
   IN EFI_PEI_SERVICES                  **PeiServices,\r
   IN EFI_CAPSULE_BLOCK_DESCRIPTOR      *BlockList,\r
+  IN UINTN                              NumDescriptors,\r
   IN UINT8                             *MemBase,\r
   IN UINTN                             MemSize\r
   );\r
@@ -144,10 +140,9 @@ IsOverlapped (
   refers to is.\r
 \r
   @param Desc            Pointer to the capsule block descriptors\r
-                         NumDescriptors  - optional pointer to where to return the number of descriptors\r
-                         CapsuleSize     - optional pointer to where to return the capsule size\r
-  @param NumDescriptors  Optional pointer to where to return the number of descriptors\r
-  @param CapsuleSize     Optional pointer to where to return the capsule size\r
+  @param NumDescriptors  Optional pointer to where to return the number of capsule data descriptors, whose Length is non-zero.\r
+  @param CapsuleSize     Optional pointer to where to return the capsule image size\r
+  @param CapsuleNumber   Optional pointer to where to return the number of capsule\r
 \r
   @retval EFI_NOT_FOUND   No descriptors containing data in the list\r
   @retval EFI_SUCCESS     Return data is valid\r
@@ -157,7 +152,8 @@ EFI_STATUS
 GetCapsuleInfo (\r
   IN EFI_CAPSULE_BLOCK_DESCRIPTOR   *Desc,\r
   IN OUT UINTN                      *NumDescriptors OPTIONAL,\r
-  IN OUT UINTN                      *CapsuleSize OPTIONAL\r
+  IN OUT UINTN                      *CapsuleSize OPTIONAL,\r
+  IN OUT UINTN                      *CapsuleNumber OPTIONAL\r
   );\r
 \r
 /**\r
@@ -266,34 +262,69 @@ ValidateCapsuleIntegrity (
 {\r
   EFI_CAPSULE_HEADER             *CapsuleHeader;\r
   UINT64                         CapsuleSize;\r
-  UINT32                         CapsuleCount;\r
+  UINT                         CapsuleCount;\r
   EFI_CAPSULE_BLOCK_DESCRIPTOR   *Ptr;\r
 \r
+  DEBUG ((EFI_D_INFO, "ValidateCapsuleIntegrity\n"));\r
+\r
   //\r
   // Go through the list to look for inconsistencies. Check for:\r
   //   * misaligned block descriptors.\r
   //   * The first capsule header guid\r
   //   * The first capsule header flag\r
-  //   * Data + Length < Data (wrap)\r
+  //   * The first capsule header HeaderSize\r
+  //   * Length > MAX_ADDRESS\r
+  //   * ContinuationPointer > MAX_ADDRESS\r
+  //   * DataBlock + Length > MAX_ADDRESS\r
+  //\r
   CapsuleSize  = 0;\r
   CapsuleCount = 0;\r
   Ptr = BlockList;\r
+\r
+  DEBUG ((EFI_D_INFO, "Ptr - 0x%x\n", Ptr));\r
+  DEBUG ((EFI_D_INFO, "Ptr->Length - 0x%x\n", Ptr->Length));\r
+  DEBUG ((EFI_D_INFO, "Ptr->Union - 0x%x\n", Ptr->Union.ContinuationPointer));\r
   while ((Ptr->Length != 0) || (Ptr->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r
     //\r
     // Make sure the descriptor is aligned at UINT64 in memory\r
     //\r
-    if ((UINTN) Ptr & 0x07) {\r
-      DEBUG ((EFI_D_ERROR, "BlockList address failed alignment check\n"));\r
+    if ((UINTN) Ptr & (sizeof(UINT64) - 1)) {\r
+      DEBUG ((EFI_D_ERROR, "ERROR: BlockList address failed alignment check\n"));\r
+      return NULL;\r
+    }\r
+    //\r
+    // Sanity Check\r
+    //\r
+    if (Ptr->Length > MAX_ADDRESS) {\r
+      DEBUG ((EFI_D_ERROR, "ERROR: Ptr->Length(0x%lx) > MAX_ADDRESS\n", Ptr->Length));\r
       return NULL;\r
     }\r
 \r
     if (Ptr->Length == 0) {\r
+      //\r
+      // Sanity Check\r
+      //\r
+      if (Ptr->Union.ContinuationPointer > MAX_ADDRESS) {\r
+        DEBUG ((EFI_D_ERROR, "ERROR: Ptr->Union.ContinuationPointer(0x%lx) > MAX_ADDRESS\n", Ptr->Union.ContinuationPointer));\r
+        return NULL;\r
+      }\r
       //\r
       // Descriptor points to another list of block descriptors somewhere\r
       // else.\r
       //\r
       Ptr = (EFI_CAPSULE_BLOCK_DESCRIPTOR  *) (UINTN) Ptr->Union.ContinuationPointer;\r
+      DEBUG ((EFI_D_INFO, "Ptr(C) - 0x%x\n", Ptr));\r
+      DEBUG ((EFI_D_INFO, "Ptr->Length - 0x%x\n", Ptr->Length));\r
+      DEBUG ((EFI_D_INFO, "Ptr->Union - 0x%x\n", Ptr->Union.ContinuationPointer));\r
     } else {\r
+      //\r
+      // Sanity Check\r
+      //\r
+      if (Ptr->Union.DataBlock > (MAX_ADDRESS - (UINTN)Ptr->Length)) {\r
+        DEBUG ((EFI_D_ERROR, "ERROR: Ptr->Union.DataBlock(0x%lx) > (MAX_ADDRESS - (UINTN)Ptr->Length(0x%lx))\n", Ptr->Union.DataBlock, Ptr->Length));\r
+        return NULL;\r
+      }\r
+\r
       //\r
       //To enhance the reliability of check-up, the first capsule's header is checked here.\r
       //More reliabilities check-up will do later.\r
@@ -303,6 +334,20 @@ ValidateCapsuleIntegrity (
         //Move to the first capsule to check its header.\r
         //\r
         CapsuleHeader = (EFI_CAPSULE_HEADER*)((UINTN)Ptr->Union.DataBlock);\r
+        //\r
+        // Sanity check\r
+        //\r
+        if (Ptr->Length < sizeof(EFI_CAPSULE_HEADER)) {\r
+          DEBUG ((EFI_D_ERROR, "ERROR: Ptr->Length(0x%lx) < sizeof(EFI_CAPSULE_HEADER)\n", Ptr->Length));\r
+          return NULL;\r
+        }\r
+        //\r
+        // Make sure HeaderSize field is valid\r
+        //\r
+        if (CapsuleHeader->HeaderSize > CapsuleHeader->CapsuleImageSize) {\r
+          DEBUG ((EFI_D_ERROR, "ERROR: CapsuleHeader->HeaderSize(0x%x) > CapsuleHeader->CapsuleImageSize(0x%x)\n", CapsuleHeader->HeaderSize, CapsuleHeader->CapsuleImageSize));\r
+          return NULL;\r
+        }\r
         if (IsCapsuleCorrupted (CapsuleHeader)) {\r
           return NULL;\r
         }\r
@@ -313,20 +358,36 @@ ValidateCapsuleIntegrity (
       if (CapsuleSize >= Ptr->Length) {\r
         CapsuleSize = CapsuleSize - Ptr->Length;\r
       } else {\r
-        CapsuleSize = 0;\r
+        DEBUG ((EFI_D_ERROR, "ERROR: CapsuleSize(0x%lx) < Ptr->Length(0x%lx)\n", CapsuleSize, Ptr->Length));\r
+        //\r
+        // Sanity check\r
+        //\r
+        return NULL;\r
       }\r
 \r
       //\r
       // Move to next BLOCK descriptor\r
       //\r
       Ptr++;\r
+      DEBUG ((EFI_D_INFO, "Ptr(B) - 0x%x\n", Ptr));\r
+      DEBUG ((EFI_D_INFO, "Ptr->Length - 0x%x\n", Ptr->Length));\r
+      DEBUG ((EFI_D_INFO, "Ptr->Union - 0x%x\n", Ptr->Union.ContinuationPointer));\r
     }\r
   }\r
 \r
-  if ((CapsuleCount == 0) || (CapsuleSize != 0)) {\r
+  if (CapsuleCount == 0) {\r
     //\r
-    // No any capsule is found in BlockList or capsule data is corrupted.\r
+    // No any capsule is found in BlockList\r
     //\r
+    DEBUG ((EFI_D_ERROR, "ERROR: CapsuleCount(0x%x) == 0\n", CapsuleCount));\r
+    return NULL;\r
+  }\r
+\r
+  if (CapsuleSize != 0) {\r
+    //\r
+    // Capsule data is incomplete.\r
+    //\r
+    DEBUG ((EFI_D_ERROR, "ERROR: CapsuleSize(0x%lx) != 0\n", CapsuleSize));\r
     return NULL;\r
   }\r
 \r
@@ -342,10 +403,11 @@ ValidateCapsuleIntegrity (
   they are relocated into memory to turn them into a contiguous (null\r
   terminated) array.\r
 \r
-  @param PeiServices pointer to PEI services table\r
-  @param BlockList   pointer to the capsule block descriptors\r
-  @param MemBase     base of system memory in which we can work\r
-  @param MemSize     size of the system memory pointed to by MemBase\r
+  @param PeiServices    pointer to PEI services table\r
+  @param BlockList      pointer to the capsule block descriptors\r
+  @param NumDescriptors number of capsule data block descriptors, whose Length is non-zero.\r
+  @param MemBase        base of system memory in which we can work\r
+  @param MemSize        size of the system memory pointed to by MemBase\r
 \r
   @retval NULL    could not relocate the descriptors\r
   @retval Pointer to the base of the successfully-relocated block descriptors. \r
@@ -355,6 +417,7 @@ EFI_CAPSULE_BLOCK_DESCRIPTOR  *
 RelocateBlockDescriptors (\r
   IN EFI_PEI_SERVICES                   **PeiServices,\r
   IN EFI_CAPSULE_BLOCK_DESCRIPTOR       *BlockList,\r
+  IN UINTN                              NumDescriptors,\r
   IN UINT8                              *MemBase,\r
   IN UINTN                              MemSize\r
   )\r
@@ -363,21 +426,17 @@ RelocateBlockDescriptors (
   EFI_CAPSULE_BLOCK_DESCRIPTOR   *CurrBlockDescHead;\r
   EFI_CAPSULE_BLOCK_DESCRIPTOR   *TempBlockDesc;\r
   EFI_CAPSULE_BLOCK_DESCRIPTOR   *PrevBlockDescTail;\r
-  UINTN                          NumDescriptors;\r
   UINTN                          BufferSize;\r
   UINT8                          *RelocBuffer;\r
   UINTN                          BlockListSize;\r
+\r
   //\r
   // Get the info on the blocks and descriptors. Since we're going to move\r
   // the descriptors low in memory, adjust the base/size values accordingly here.\r
-  // GetCapsuleInfo() returns the number of legit descriptors, so add one for\r
-  // a terminator.\r
+  // NumDescriptors is the number of legit data descriptors, so add one for\r
+  // a terminator. (Already done by caller, no check is needed.)\r
   //\r
-  if (GetCapsuleInfo (BlockList, &NumDescriptors, NULL) != EFI_SUCCESS) {\r
-    return NULL;\r
-  }\r
 \r
-  NumDescriptors++;\r
   BufferSize    = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r
   NewBlockList  = (EFI_CAPSULE_BLOCK_DESCRIPTOR *) MemBase;\r
   if (MemSize < BufferSize) {\r
@@ -416,7 +475,7 @@ RelocateBlockDescriptors (
         }\r
 \r
         CopyMem ((VOID *) RelocBuffer, (VOID *) (UINTN) TempBlockDesc->Union.DataBlock, (UINTN) TempBlockDesc->Length);\r
-        DEBUG ((EFI_D_INFO, "Capsule relocate descriptors from/to/size  0x%X 0x%X 0x%X\n", (UINT32)(UINTN)TempBlockDesc->Union.DataBlock, (UINT32)(UINTN)RelocBuffer, (UINT32)(UINTN)TempBlockDesc->Length));\r
+        DEBUG ((EFI_D_INFO, "Capsule relocate descriptors from/to/size  0x%lX 0x%lX 0x%lX\n", TempBlockDesc->Union.DataBlock, (UINT64)(UINTN)RelocBuffer, TempBlockDesc->Length));\r
         TempBlockDesc->Union.DataBlock = (EFI_PHYSICAL_ADDRESS) (UINTN) RelocBuffer;\r
       }\r
       TempBlockDesc++;\r
@@ -530,10 +589,9 @@ IsOverlapped (
   refers to is.\r
 \r
   @param Desc            Pointer to the capsule block descriptors\r
-                         NumDescriptors  - optional pointer to where to return the number of descriptors\r
-                         CapsuleSize     - optional pointer to where to return the capsule size\r
-  @param NumDescriptors  Optional pointer to where to return the number of descriptors\r
-  @param CapsuleSize     Optional pointer to where to return the capsule size\r
+  @param NumDescriptors  Optional pointer to where to return the number of capsule data descriptors, whose Length is non-zero.\r
+  @param CapsuleSize     Optional pointer to where to return the capsule image size\r
+  @param CapsuleNumber   Optional pointer to where to return the number of capsule\r
 \r
   @retval EFI_NOT_FOUND   No descriptors containing data in the list\r
   @retval EFI_SUCCESS     Return data is valid\r
@@ -543,16 +601,24 @@ EFI_STATUS
 GetCapsuleInfo (\r
   IN EFI_CAPSULE_BLOCK_DESCRIPTOR   *Desc,\r
   IN OUT UINTN                      *NumDescriptors OPTIONAL,\r
-  IN OUT UINTN                      *CapsuleSize OPTIONAL\r
+  IN OUT UINTN                      *CapsuleSize OPTIONAL,\r
+  IN OUT UINTN                      *CapsuleNumber OPTIONAL\r
   )\r
 {\r
-  UINTN Count;\r
-  UINTN Size;\r
+  UINTN                          Count;\r
+  UINTN                          Size;\r
+  UINTN                          Number;\r
+  UINTN                          ThisCapsuleImageSize;\r
+  EFI_CAPSULE_HEADER             *CapsuleHeader;\r
+\r
+  DEBUG ((EFI_D_INFO, "GetCapsuleInfo enter\n"));\r
 \r
   ASSERT (Desc != NULL);\r
 \r
   Count = 0;\r
   Size  = 0;\r
+  Number = 0;\r
+  ThisCapsuleImageSize = 0;\r
 \r
   while (Desc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL) {\r
     if (Desc->Length == 0) {\r
@@ -561,8 +627,39 @@ GetCapsuleInfo (
       //\r
       Desc = (EFI_CAPSULE_BLOCK_DESCRIPTOR  *) (UINTN) Desc->Union.ContinuationPointer;\r
     } else {\r
+      //\r
+      // Sanity Check\r
+      // It is needed, because ValidateCapsuleIntegrity() only validate one individual capsule Size.\r
+      // While here we need check all capsules size.\r
+      //\r
+      if (Desc->Length >= (MAX_ADDRESS - Size)) {\r
+        DEBUG ((EFI_D_ERROR, "ERROR: Desc->Length(0x%lx) >= (MAX_ADDRESS - Size(0x%x))\n", Desc->Length, Size));\r
+        return EFI_OUT_OF_RESOURCES;\r
+      }\r
       Size += (UINTN) Desc->Length;\r
       Count++;\r
+\r
+      //\r
+      // See if this is first capsule's header\r
+      //\r
+      if (ThisCapsuleImageSize == 0) {\r
+        CapsuleHeader = (EFI_CAPSULE_HEADER*)((UINTN)Desc->Union.DataBlock);\r
+        //\r
+        // This has been checked in ValidateCapsuleIntegrity()\r
+        //\r
+        Number ++;\r
+        ThisCapsuleImageSize = CapsuleHeader->CapsuleImageSize;\r
+      }\r
+\r
+      //\r
+      // This has been checked in ValidateCapsuleIntegrity()\r
+      //\r
+      ASSERT (ThisCapsuleImageSize >= Desc->Length);\r
+      ThisCapsuleImageSize = (UINTN)(ThisCapsuleImageSize - Desc->Length);\r
+\r
+      //\r
+      // Move to next\r
+      //\r
       Desc++;\r
     }\r
   }\r
@@ -570,9 +667,15 @@ GetCapsuleInfo (
   // If no descriptors, then fail\r
   //\r
   if (Count == 0) {\r
+    DEBUG ((EFI_D_ERROR, "ERROR: Count == 0\n"));\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
+  //\r
+  // checked in ValidateCapsuleIntegrity()\r
+  //\r
+  ASSERT (ThisCapsuleImageSize == 0);\r
+\r
   if (NumDescriptors != NULL) {\r
     *NumDescriptors = Count;\r
   }\r
@@ -581,6 +684,10 @@ GetCapsuleInfo (
     *CapsuleSize = Size;\r
   }\r
 \r
+  if (CapsuleNumber != NULL) {\r
+    *CapsuleNumber = Number;\r
+  }\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -639,6 +746,9 @@ CapsuleTestPatternPreCoalesce (
   UINT32  *TestPtr;\r
   UINT32  TestCounter;\r
   UINT32  TestSize;\r
+\r
+  DEBUG ((EFI_D_INFO, "CapsuleTestPatternPreCoalesce\n"));\r
+\r
   //\r
   // Find first data descriptor\r
   //\r
@@ -721,6 +831,8 @@ BuildCapsuleDescriptors (
   EFI_CAPSULE_BLOCK_DESCRIPTOR     *TempBlock;\r
   EFI_CAPSULE_BLOCK_DESCRIPTOR     *HeadBlock;\r
 \r
+  DEBUG ((EFI_D_INFO, "BuildCapsuleDescriptors enter\n"));\r
+\r
   LastBlock         = NULL;\r
   HeadBlock         = NULL;\r
   TempBlock         = NULL;\r
@@ -730,23 +842,27 @@ BuildCapsuleDescriptors (
     //\r
     // Test integrity of descriptors.\r
     //\r
-    TempBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index]);\r
-    if (TempBlock != NULL) {\r
-      if (LastBlock == NULL) {\r
-        LastBlock = TempBlock;\r
+    if (BlockListBuffer[Index] < MAX_ADDRESS) {\r
+      TempBlock = ValidateCapsuleIntegrity ((EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index]);\r
+      if (TempBlock != NULL) {\r
+        if (LastBlock == NULL) {\r
+          LastBlock = TempBlock;\r
 \r
-        //\r
-        // Return the base of the block descriptors\r
-        //\r
-        HeadBlock = (EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index];\r
-      } else {\r
-        //\r
-        // Combine the different BlockList into single BlockList.\r
-        //\r
-        LastBlock->Union.DataBlock = (EFI_PHYSICAL_ADDRESS)(UINTN)BlockListBuffer[Index];\r
-        LastBlock->Length          = 0;\r
-        LastBlock                  = TempBlock;\r
+          //\r
+          // Return the base of the block descriptors\r
+          //\r
+          HeadBlock = (EFI_CAPSULE_BLOCK_DESCRIPTOR *)(UINTN)BlockListBuffer[Index];\r
+        } else {\r
+          //\r
+          // Combine the different BlockList into single BlockList.\r
+          //\r
+          LastBlock->Union.DataBlock = (EFI_PHYSICAL_ADDRESS)(UINTN)BlockListBuffer[Index];\r
+          LastBlock->Length          = 0;\r
+          LastBlock                  = TempBlock;\r
+        }\r
       }\r
+    } else {\r
+      DEBUG ((EFI_D_ERROR, "ERROR: BlockListBuffer[Index](0x%lx) < MAX_ADDRESS\n", BlockListBuffer[Index]));\r
     }\r
     Index ++;\r
   }\r
@@ -763,26 +879,32 @@ BuildCapsuleDescriptors (
 \r
   Memory Map for coalesced capsule:\r
   MemBase +   ---->+---------------------------+<-----------+\r
-  MemSize          |    CapsuleOffset[49]      |            |\r
-                   +---------------------------+            |\r
-                   |    ................       |            |\r
-                   +---------------------------+            |\r
-                   |    CapsuleOffset[2]       |            |\r
-                   +---------------------------+            |\r
-                   |    CapsuleOffset[1]       |            |\r
-                   +---------------------------+            |\r
-                   |    CapsuleOffset[0]       |       CapsuleSize     \r
-                   +---------------------------+            |\r
-                   |    CapsuleNumber          |            |\r
-                   +---------------------------+            |\r
-                   |                           |            |       \r
-                   |                           |            |       \r
+  MemSize          | ------------------------- |            |\r
+                   | |  Capsule [Num-1]      | |            |\r
+                   | ------------------------- |            |\r
+                   | |  ................     | |            |\r
+                   | ------------------------- |            |\r
+                   | |  Capsule [1]          | |            |\r
+                   | ------------------------- |            |\r
+                   | |  Capsule [0]          | |            |\r
+                   | ------------------------- |            |\r
                    |    Capsule Image          |            |   \r
-                   |                           |            |       \r
-                   |                           |            |       \r
-                   +---------------------------+            |\r
+CapsuleImageBase-->+---------------------------+\r
+                   | ------------------------- |            |\r
+                   | |  CapsuleOffset[Num-1] | |            |\r
+                   | ------------------------- |            |\r
+                   | |  ................     | |        CapsuleSize\r
+                   | ------------------------- |            |\r
+                   | |  CapsuleOffset[1]     | |            |\r
+                   | ------------------------- |            |\r
+                   | |  CapsuleOffset[0]     | |            |\r
+                   |---------------------------|            |\r
+                   | |  CapsuleNumber        | |            |\r
+                   | ------------------------- |            |\r
+                   | |  CapsuleAllImageSize  | |            |\r
+                   | ------------------------- |            |\r
                    |    PrivateData            |            |\r
-   DestPtr  ---->  +---------------------------+<-----------+\r
+     DestPtr  ---->+---------------------------+<-----------+\r
                    |                           |            |\r
                    |     FreeMem               |        FreeMemSize\r
                    |                           |            |\r
@@ -832,21 +954,21 @@ CapsuleDataCoalesce (
   )\r
 {\r
   VOID                           *NewCapsuleBase;\r
-  VOID                           *DataPtr;\r
-  UINT8                          CapsuleIndex;\r
+  VOID                           *CapsuleImageBase;\r
+  UINTN                          CapsuleIndex;\r
   UINT8                          *FreeMemBase;\r
   UINT8                          *DestPtr;\r
+  UINTN                          DestLength;\r
   UINT8                          *RelocPtr;\r
-  UINT32                         CapsuleOffset[MAX_SUPPORT_CAPSULE_NUM]; \r
-  UINT32                         *AddDataPtr;\r
-  UINT32                         CapsuleTimes; \r
+  UINT64                         *AddDataPtr;\r
+  UINTN                          CapsuleTimes; \r
   UINT64                         SizeLeft; \r
   UINT64                         CapsuleImageSize; \r
   UINTN                          CapsuleSize;\r
+  UINTN                          CapsuleNumber;\r
   UINTN                          DescriptorsSize;\r
   UINTN                          FreeMemSize;\r
   UINTN                          NumDescriptors;\r
-  BOOLEAN                        IsCorrupted;\r
   BOOLEAN                        CapsuleBeginFlag;\r
   EFI_STATUS                     Status;\r
   EFI_CAPSULE_HEADER             *CapsuleHeader;\r
@@ -857,6 +979,8 @@ CapsuleDataCoalesce (
   EFI_CAPSULE_BLOCK_DESCRIPTOR   *TempBlockDesc;\r
   EFI_CAPSULE_BLOCK_DESCRIPTOR   PrivateDataDesc[2];\r
 \r
+  DEBUG ((EFI_D_INFO, "CapsuleDataCoalesce enter\n"));\r
+\r
   CapsuleIndex     = 0;\r
   SizeLeft         = 0;\r
   CapsuleTimes     = 0;\r
@@ -865,10 +989,9 @@ CapsuleDataCoalesce (
   AddDataPtr       = NULL;\r
   CapsuleHeader    = NULL;\r
   CapsuleBeginFlag = TRUE;\r
-  IsCorrupted      = TRUE;\r
   CapsuleSize      = 0;\r
   NumDescriptors   = 0;\r
-  \r
+\r
   //\r
   // Build capsule descriptors list\r
   //\r
@@ -886,30 +1009,71 @@ CapsuleDataCoalesce (
   // returns the number of descriptors that actually point to data, so add\r
   // one for a terminator. Do that below.\r
   //\r
-  GetCapsuleInfo (BlockList, &NumDescriptors, &CapsuleSize);\r
-  if ((CapsuleSize == 0) || (NumDescriptors == 0)) {\r
+  Status = GetCapsuleInfo (BlockList, &NumDescriptors, &CapsuleSize, &CapsuleNumber);\r
+  if (EFI_ERROR (Status)) {\r
+    return Status;\r
+  }\r
+  DEBUG ((EFI_D_INFO, "CapsuleSize - 0x%x\n", CapsuleSize));\r
+  DEBUG ((EFI_D_INFO, "CapsuleNumber - 0x%x\n", CapsuleNumber));\r
+  DEBUG ((EFI_D_INFO, "NumDescriptors - 0x%x\n", NumDescriptors));\r
+  if ((CapsuleSize == 0) || (NumDescriptors == 0) || (CapsuleNumber == 0)) {\r
     return EFI_NOT_FOUND;\r
   }\r
 \r
+  if (CapsuleNumber - 1 >= (MAX_ADDRESS - (sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA)  + sizeof(UINT64))) / sizeof(UINT64)) {\r
+    DEBUG ((EFI_D_ERROR, "ERROR: CapsuleNumber - 0x%x\n", CapsuleNumber));\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+\r
   //\r
   // Initialize our local copy of private data. When we're done, we'll create a\r
   // descriptor for it as well so that it can be put into free memory without\r
   // trashing anything.\r
   //\r
-  PrivateData.Signature     = EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE;\r
-  PrivateData.CapsuleSize   = (UINT32) CapsuleSize;\r
+  PrivateData.Signature           = EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE;\r
+  PrivateData.CapsuleAllImageSize = (UINT64) CapsuleSize;\r
+  PrivateData.CapsuleNumber       = (UINT64) CapsuleNumber;\r
+  PrivateData.CapsuleOffset[0]    = 0;\r
+  //\r
+  // NOTE: Only data in sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) is valid, CapsuleOffset field is unitialized at this moment.\r
+  // The code sets partial length here for Descriptor.Length check, but later it will use full length to reserve those PrivateData region.\r
+  //\r
   PrivateDataDesc[0].Union.DataBlock  = (EFI_PHYSICAL_ADDRESS) (UINTN) &PrivateData;\r
   PrivateDataDesc[0].Length           = sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA);\r
   PrivateDataDesc[1].Union.DataBlock  = (EFI_PHYSICAL_ADDRESS) (UINTN) BlockList;\r
   PrivateDataDesc[1].Length           = 0;\r
   //\r
-  // In addition to PrivateDataDesc[1:0], one terminator is added\r
-  // See below RelocateBlockDescriptors()\r
+  // Add PrivateDataDesc[0] in beginning beginning, as it is new descriptor. PrivateDataDesc[1] is NOT needed.\r
+  // In addition, one NULL terminator is added in the end. See RelocateBlockDescriptors().\r
+  //\r
+  NumDescriptors  += 2;\r
   //\r
-  NumDescriptors  += 3;\r
-  CapsuleSize     += sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + sizeof(CapsuleOffset) + sizeof(UINT32);\r
+  // Sandity check\r
+  //\r
+  if (CapsuleSize >= (MAX_ADDRESS - (sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + (CapsuleNumber - 1) * sizeof(UINT64) + sizeof(UINT64)))) {\r
+    DEBUG ((EFI_D_ERROR, "ERROR: CapsuleSize - 0x%x\n", CapsuleSize));\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
+  //\r
+  // Need add sizeof(UINT64) for PrivateData alignment\r
+  //\r
+  CapsuleSize     += sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + (CapsuleNumber - 1) * sizeof(UINT64) + sizeof(UINT64);\r
   BlockList        = PrivateDataDesc;\r
+  //\r
+  // Sandity check\r
+  //\r
+  if (NumDescriptors >= (MAX_ADDRESS / sizeof(EFI_CAPSULE_BLOCK_DESCRIPTOR))) {\r
+    DEBUG ((EFI_D_ERROR, "ERROR: NumDescriptors - 0x%x\n", NumDescriptors));\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
   DescriptorsSize  = NumDescriptors * sizeof (EFI_CAPSULE_BLOCK_DESCRIPTOR);\r
+  //\r
+  // Sandity check\r
+  //\r
+  if (DescriptorsSize >= (MAX_ADDRESS - CapsuleSize)) {\r
+    DEBUG ((EFI_D_ERROR, "ERROR: DescriptorsSize - 0x%lx, CapsuleSize - 0x%lx\n", (UINT64)DescriptorsSize, (UINT64)CapsuleSize));\r
+    return EFI_BUFFER_TOO_SMALL;\r
+  }\r
 \r
   //\r
   // Don't go below some min address. If the base is below it,\r
@@ -918,6 +1082,7 @@ CapsuleDataCoalesce (
   DEBUG ((EFI_D_INFO, "Capsule Memory range from 0x%8X to 0x%8X\n", (UINTN) *MemoryBase, (UINTN)*MemoryBase + *MemorySize));\r
   if ((UINTN)*MemoryBase < (UINTN) MIN_COALESCE_ADDR) {\r
     if (((UINTN)*MemoryBase + *MemorySize) < (UINTN) MIN_COALESCE_ADDR) {\r
+      DEBUG ((EFI_D_ERROR, "ERROR: *MemoryBase + *MemorySize - 0x%x\n", (UINTN)*MemoryBase + *MemorySize));\r
       return EFI_BUFFER_TOO_SMALL;\r
     } else {\r
       *MemorySize = *MemorySize - ((UINTN) MIN_COALESCE_ADDR - (UINTN) *MemoryBase);\r
@@ -926,6 +1091,7 @@ CapsuleDataCoalesce (
   }\r
 \r
   if (*MemorySize <= (CapsuleSize + DescriptorsSize)) {\r
+    DEBUG ((EFI_D_ERROR, "ERROR: CapsuleSize + DescriptorsSize - 0x%x\n", CapsuleSize + DescriptorsSize));\r
     return EFI_BUFFER_TOO_SMALL;\r
   }\r
 \r
@@ -937,7 +1103,7 @@ CapsuleDataCoalesce (
   // Relocate all the block descriptors to low memory to make further\r
   // processing easier.\r
   //\r
-  BlockList = RelocateBlockDescriptors (PeiServices, BlockList, FreeMemBase, FreeMemSize);\r
+  BlockList = RelocateBlockDescriptors (PeiServices, BlockList, NumDescriptors, FreeMemBase, FreeMemSize);\r
   if (BlockList == NULL) {\r
     //\r
     // Not enough room to relocate the descriptors\r
@@ -946,13 +1112,16 @@ CapsuleDataCoalesce (
   }\r
 \r
   //\r
-  // Take the top of memory for the capsule. Naturally align.\r
+  // Take the top of memory for the capsule. UINT64 align up.\r
   //\r
   DestPtr         = FreeMemBase + FreeMemSize - CapsuleSize;\r
-  DestPtr         = (UINT8 *) ((UINTN) DestPtr &~ (UINTN) (sizeof (UINTN) - 1));\r
+  DestPtr         = (UINT8 *) (((UINTN)DestPtr + sizeof (UINT64) - 1) & ~(sizeof (UINT64) - 1));\r
   FreeMemBase     = (UINT8 *) BlockList + DescriptorsSize;\r
   FreeMemSize     = (UINTN) DestPtr - (UINTN) FreeMemBase;\r
   NewCapsuleBase  = (VOID *) DestPtr;\r
+  CapsuleImageBase = (UINT8 *)NewCapsuleBase + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (CapsuleNumber - 1) * sizeof(UINT64);\r
+\r
+  PrivateDataPtr = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) NewCapsuleBase;\r
 \r
   //\r
   // Move all the blocks to the top (high) of memory.\r
@@ -961,6 +1130,16 @@ CapsuleDataCoalesce (
   //\r
   CurrentBlockDesc = BlockList;\r
   while ((CurrentBlockDesc->Length != 0) || (CurrentBlockDesc->Union.ContinuationPointer != (EFI_PHYSICAL_ADDRESS) (UINTN) NULL)) {\r
+    if (CapsuleTimes == 0) {\r
+      //\r
+      // The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.\r
+      // CapsuleOffset field is uninitialized at this time. No need copy it, but need to reserve for future use.\r
+      //\r
+      ASSERT (CurrentBlockDesc->Union.DataBlock == (UINT64)(UINTN)&PrivateData);\r
+      DestLength = sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + (CapsuleNumber - 1) * sizeof(UINT64);\r
+    } else {\r
+      DestLength = (UINTN)CurrentBlockDesc->Length;\r
+    }\r
     //\r
     // See if any of the remaining capsule blocks are in the way\r
     //\r
@@ -971,7 +1150,7 @@ CapsuleDataCoalesce (
       //\r
       if (IsOverlapped (\r
             (UINT8 *) DestPtr,\r
-            (UINTN) CurrentBlockDesc->Length,\r
+            (UINTN) DestLength,\r
             (UINT8 *) (UINTN) TempBlockDesc->Union.DataBlock,\r
             (UINTN) TempBlockDesc->Length\r
             )) {\r
@@ -1011,56 +1190,50 @@ CapsuleDataCoalesce (
         CapsuleBeginFlag  = FALSE;\r
         CapsuleHeader     = (EFI_CAPSULE_HEADER*)(UINTN)CurrentBlockDesc->Union.DataBlock;\r
         SizeLeft          = CapsuleHeader->CapsuleImageSize;\r
-        if (!IsCapsuleCorrupted (CapsuleHeader)) {\r
 \r
-          if (CapsuleIndex > (MAX_SUPPORT_CAPSULE_NUM - 1)) {\r
-            DEBUG ((EFI_D_ERROR, "Capsule number exceeds the max number of %d!\n", MAX_SUPPORT_CAPSULE_NUM));\r
-            return  EFI_BUFFER_TOO_SMALL;\r
-          }\r
+        //\r
+        // No more check here is needed, because IsCapsuleCorrupted() already in ValidateCapsuleIntegrity()\r
+        //\r
+        ASSERT (CapsuleIndex < CapsuleNumber);\r
 \r
-          //\r
-          // Relocate this valid capsule\r
-          //\r
-          IsCorrupted  = FALSE;\r
-          CapsuleImageSize += SizeLeft;\r
-          //\r
-          // Cache the begin offset of this capsule\r
-          //\r
-          CapsuleOffset[CapsuleIndex++] = (UINT32) (UINTN) DestPtr - (UINT32)(UINTN)NewCapsuleBase - (UINT32)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA);\r
-        }\r
+        //\r
+        // Relocate this capsule\r
+        //\r
+        CapsuleImageSize += SizeLeft;\r
+        //\r
+        // Cache the begin offset of this capsule\r
+        //\r
+        ASSERT (PrivateDataPtr->Signature == EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE);\r
+        ASSERT ((UINTN)DestPtr >= (UINTN)CapsuleImageBase);\r
+        PrivateDataPtr->CapsuleOffset[CapsuleIndex++] = (UINT64)((UINTN)DestPtr - (UINTN)CapsuleImageBase);\r
       }\r
 \r
-      if (CurrentBlockDesc->Length < SizeLeft) {\r
-        if (!IsCorrupted) {\r
-          CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) (CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);\r
-          DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,\r
-                 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN)CurrentBlockDesc->Length));\r
-          DestPtr += CurrentBlockDesc->Length;\r
-        }\r
-        SizeLeft -= CurrentBlockDesc->Length;\r
-      } else {\r
+      //\r
+      // Below ASSERT is checked in ValidateCapsuleIntegrity()\r
+      //\r
+      ASSERT (CurrentBlockDesc->Length <= SizeLeft);\r
+\r
+      CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) (CurrentBlockDesc->Union.DataBlock), (UINTN)CurrentBlockDesc->Length);\r
+      DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%lX from 0x%lX to 0x%lX with size 0x%lX\n",(UINT64)CapsuleTimes,\r
+             CurrentBlockDesc->Union.DataBlock, (UINT64)(UINTN)DestPtr, CurrentBlockDesc->Length));\r
+      DestPtr += CurrentBlockDesc->Length;\r
+      SizeLeft -= CurrentBlockDesc->Length;\r
+\r
+      if (SizeLeft == 0) {\r
         //\r
         //Here is the end of the current capsule image.\r
         //\r
-        if (!IsCorrupted) {\r
-          CopyMem ((VOID *) DestPtr, (VOID *)(UINTN)(CurrentBlockDesc->Union.DataBlock), (UINTN) SizeLeft);\r
-          DEBUG ((EFI_D_INFO, "Capsule coalesce block no.0x%8X from 0x%8lX to 0x%8lX with size 0x%8X\n",CapsuleTimes,\r
-                 (UINTN)CurrentBlockDesc->Union.DataBlock, (UINTN)DestPtr, (UINTN) SizeLeft));\r
-          DestPtr += SizeLeft;\r
-        }\r
-        //\r
-        // Start the next cycle\r
-        //\r
-        SizeLeft = 0;\r
-        IsCorrupted = TRUE;\r
         CapsuleBeginFlag = TRUE; \r
       }\r
     } else {\r
       //\r
-      //The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.\r
+      // The first entry is the block descriptor for EFI_CAPSULE_PEIM_PRIVATE_DATA.\r
+      // CapsuleOffset field is uninitialized at this time. No need copy it, but need to reserve for future use.\r
       //\r
+      ASSERT (CurrentBlockDesc->Length == sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA));\r
+      ASSERT ((UINTN)DestPtr == (UINTN)NewCapsuleBase);\r
       CopyMem ((VOID *) DestPtr, (VOID *) (UINTN) CurrentBlockDesc->Union.DataBlock, (UINTN) CurrentBlockDesc->Length);\r
-      DestPtr += CurrentBlockDesc->Length;\r
+      DestPtr += sizeof (EFI_CAPSULE_PEIM_PRIVATE_DATA) + (CapsuleNumber - 1) * sizeof(UINT64);\r
     }\r
     //\r
     //Walk through the block descriptor list.\r
@@ -1074,18 +1247,9 @@ CapsuleDataCoalesce (
   *MemorySize = (UINTN) CapsuleSize;\r
   *MemoryBase = (VOID *) NewCapsuleBase;\r
 \r
-  //\r
-  //Append the offsets of mutiply capsules to the continous buffer\r
-  //\r
-  DataPtr    = (VOID*)((UINTN)NewCapsuleBase + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (UINTN)CapsuleImageSize);\r
-  AddDataPtr = (UINT32*)(((UINTN) DataPtr + sizeof(UINT32) - 1) &~ (UINT32) (sizeof (UINT32) - 1));\r
-\r
-  *AddDataPtr++ = CapsuleIndex;\r
-\r
-  CopyMem (AddDataPtr, &CapsuleOffset[0], sizeof (UINT32) * CapsuleIndex);\r
-\r
-  PrivateDataPtr = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) NewCapsuleBase;\r
-  PrivateDataPtr->CapsuleSize = (UINT32) CapsuleImageSize;\r
+  ASSERT (PrivateDataPtr->Signature == EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE);\r
+  ASSERT (PrivateDataPtr->CapsuleAllImageSize == CapsuleImageSize);\r
+  ASSERT (PrivateDataPtr->CapsuleNumber == CapsuleIndex);\r
 \r
   return EFI_SUCCESS;\r
 }\r
index 0f7f7aea41cb556a307208e1e3f67033337f4fc7..d1ed24a572307c2e0446c8d92bbce2b19fa10487 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Common header file.\r
 \r
-Copyright (c) 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions of the BSD License\r
 which accompanies this distribution.  The full text of the license may be found at\r
@@ -19,12 +19,16 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 // This capsule PEIM puts its private data at the start of the\r
 // coalesced capsule. Here's the structure definition.\r
 //\r
-#define EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('C', 'a', 'p', 'D')\r
+#define EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE SIGNATURE_32 ('C', 'a', 'p', 'P')\r
 \r
+#pragma pack(1)\r
 typedef struct {\r
-  UINT32  Signature;\r
-  UINTN   CapsuleSize;\r
+  UINT64  Signature;\r
+  UINT64  CapsuleAllImageSize;\r
+  UINT64  CapsuleNumber;\r
+  UINT64  CapsuleOffset[1];\r
 } EFI_CAPSULE_PEIM_PRIVATE_DATA;\r
+#pragma pack()\r
 \r
 #define CAPSULE_TEST_SIGNATURE SIGNATURE_32('T', 'E', 'S', 'T')\r
 \r
index f5dd85b2b60072e839fa38e3b95745cfe6556357..37ba341e608d308742491a9b4eb33c23ca4c0b89 100644 (file)
@@ -1,7 +1,7 @@
 /** @file\r
   Capsule update PEIM for UEFI2.0\r
 \r
-Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2006 - 2014, Intel Corporation. All rights reserved.<BR>\r
 \r
 This program and the accompanying materials\r
 are licensed and made available under the terms and conditions\r
@@ -987,25 +987,28 @@ CreateState (
   EFI_CAPSULE_PEIM_PRIVATE_DATA *PrivateData;\r
   UINTN                         Size;\r
   EFI_PHYSICAL_ADDRESS          NewBuffer;\r
-  UINT32                        *DataPtr;\r
-  UINT32                        CapsuleNumber;\r
+  UINTN                         CapsuleNumber;\r
   UINT32                        Index;\r
   EFI_PHYSICAL_ADDRESS          BaseAddress;\r
   UINT64                        Length;\r
  \r
-  DataPtr        = NULL;\r
-  CapsuleNumber  = 0;\r
   PrivateData    = (EFI_CAPSULE_PEIM_PRIVATE_DATA *) CapsuleBase;\r
   if (PrivateData->Signature != EFI_CAPSULE_PEIM_PRIVATE_DATA_SIGNATURE) {\r
     return EFI_VOLUME_CORRUPTED;\r
   }\r
+  if (PrivateData->CapsuleAllImageSize >= MAX_ADDRESS) {\r
+    DEBUG ((EFI_D_ERROR, "CapsuleAllImageSize too big - 0x%lx\n", PrivateData->CapsuleAllImageSize));\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+  if (PrivateData->CapsuleNumber >= MAX_ADDRESS) {\r
+    DEBUG ((EFI_D_ERROR, "CapsuleNumber too big - 0x%lx\n", PrivateData->CapsuleNumber));\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
   //\r
   // Capsule Number and Capsule Offset is in the tail of Capsule data.\r
   //\r
-  Size    = (UINTN) PrivateData->CapsuleSize;\r
-  DataPtr = (UINT32*)((UINTN)CapsuleBase + (UINTN)sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA)+ Size);\r
-  DataPtr = (UINT32*)(((UINTN) DataPtr + sizeof(UINT32) - 1) & ~(sizeof (UINT32) - 1));\r
-  CapsuleNumber = *DataPtr++;\r
+  Size          = (UINTN)PrivateData->CapsuleAllImageSize;\r
+  CapsuleNumber = (UINTN)PrivateData->CapsuleNumber;\r
   //\r
   // Allocate the memory so that it gets preserved into DXE\r
   //\r
@@ -1022,8 +1025,8 @@ CreateState (
   //\r
   // Copy to our new buffer for DXE\r
   //\r
-  DEBUG ((EFI_D_INFO, "Capsule copy from 0x%8X to 0x%8X with size 0x%8X\n", (UINTN) (PrivateData + 1), (UINTN) NewBuffer, Size));\r
-  CopyMem ((VOID *) (UINTN) NewBuffer, (VOID *) (UINTN) (PrivateData + 1), Size);\r
+  DEBUG ((EFI_D_INFO, "Capsule copy from 0x%8X to 0x%8X with size 0x%8X\n", (UINTN)((UINT8 *)PrivateData + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (CapsuleNumber - 1) * sizeof(UINT64)), (UINTN) NewBuffer, Size));\r
+  CopyMem ((VOID *) (UINTN) NewBuffer, (VOID *) (UINTN) ((UINT8 *)PrivateData + sizeof(EFI_CAPSULE_PEIM_PRIVATE_DATA) + (CapsuleNumber - 1) * sizeof(UINT64)), Size);\r
   //\r
   // Check for test data pattern. If it is the test pattern, then we'll\r
   // test it ans still create the HOB so that it can be used to verify\r
@@ -1039,7 +1042,7 @@ CreateState (
   // Build the UEFI Capsule Hob for each capsule image.\r
   //\r
   for (Index = 0; Index < CapsuleNumber; Index ++) {\r
-    BaseAddress = NewBuffer + DataPtr[Index];\r
+    BaseAddress = NewBuffer + PrivateData->CapsuleOffset[Index];\r
     Length      = ((EFI_CAPSULE_HEADER *)((UINTN) BaseAddress))->CapsuleImageSize;\r
 \r
     BuildCvHob (BaseAddress, Length);\r