]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenFv/GenFvInternalLib.c
BaseTools: Clean up source files
[mirror_edk2.git] / BaseTools / Source / C / GenFv / GenFvInternalLib.c
index dd0e9ec3be6ae53dedc4ac863da387d686898a31..6a874f4e944fd56f6f286f1b91bfbaa78b8b6b02 100644 (file)
@@ -1,30 +1,29 @@
 /** @file\r
+This file contains the internal functions required to generate a Firmware Volume.\r
 \r
-Copyright (c) 2004 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 2004 - 2018, Intel Corporation. All rights reserved.<BR>\r
 Portions Copyright (c) 2011 - 2013, ARM Ltd. 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
-http://opensource.org/licenses/bsd-license.php                                            \r
-                                                                                          \r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,                     \r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.             \r
+Portions Copyright (c) 2016 HP Development Company, L.P.<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
+http://opensource.org/licenses/bsd-license.php\r
 \r
-Module Name:\r
-\r
-  GenFvInternalLib.c\r
-\r
-Abstract:\r
-\r
-  This file contains the internal functions required to generate a Firmware Volume.\r
+THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
 **/\r
 \r
 //\r
 // Include files\r
 //\r
-#ifdef __GNUC__\r
+\r
+#if defined(__FreeBSD__)\r
+#include <uuid.h>\r
+#elif defined(__GNUC__)\r
 #include <uuid/uuid.h>\r
+#endif\r
+#ifdef __GNUC__\r
 #include <sys/stat.h>\r
 #endif\r
 #include <string.h>\r
@@ -33,70 +32,77 @@ Abstract:
 #endif\r
 #include <assert.h>\r
 \r
+#include <Guid/FfsSectionAlignmentPadding.h>\r
+\r
+#include "WinNtInclude.h"\r
 #include "GenFvInternalLib.h"\r
 #include "FvLib.h"\r
 #include "PeCoffLib.h"\r
-#include "WinNtInclude.h"\r
+\r
+#define ARMT_UNCONDITIONAL_JUMP_INSTRUCTION       0xEB000000\r
+#define ARM64_UNCONDITIONAL_JUMP_INSTRUCTION      0x14000000\r
 \r
 BOOLEAN mArm = FALSE;\r
 STATIC UINT32   MaxFfsAlignment = 0;\r
+BOOLEAN VtfFileFlag = FALSE;\r
 \r
-EFI_GUID  mEfiFirmwareVolumeTopFileGuid = EFI_FFS_VOLUME_TOP_FILE_GUID;\r
+EFI_GUID  mEfiFirmwareVolumeTopFileGuid       = EFI_FFS_VOLUME_TOP_FILE_GUID;\r
 EFI_GUID  mFileGuidArray [MAX_NUMBER_OF_FILES_IN_FV];\r
-EFI_GUID  mZeroGuid                 = {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};\r
-EFI_GUID  mDefaultCapsuleGuid       = {0x3B6686BD, 0x0D76, 0x4030, { 0xB7, 0x0E, 0xB5, 0x51, 0x9E, 0x2F, 0xC5, 0xA0 }};\r
+EFI_GUID  mZeroGuid                           = {0x0, 0x0, 0x0, {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}};\r
+EFI_GUID  mDefaultCapsuleGuid                 = {0x3B6686BD, 0x0D76, 0x4030, { 0xB7, 0x0E, 0xB5, 0x51, 0x9E, 0x2F, 0xC5, 0xA0 }};\r
+EFI_GUID  mEfiFfsSectionAlignmentPaddingGuid  = EFI_FFS_SECTION_ALIGNMENT_PADDING_GUID;\r
 \r
 CHAR8      *mFvbAttributeName[] = {\r
-  EFI_FVB2_READ_DISABLED_CAP_STRING, \r
-  EFI_FVB2_READ_ENABLED_CAP_STRING,  \r
-  EFI_FVB2_READ_STATUS_STRING,       \r
+  EFI_FVB2_READ_DISABLED_CAP_STRING,\r
+  EFI_FVB2_READ_ENABLED_CAP_STRING,\r
+  EFI_FVB2_READ_STATUS_STRING,\r
   EFI_FVB2_WRITE_DISABLED_CAP_STRING,\r
-  EFI_FVB2_WRITE_ENABLED_CAP_STRING, \r
-  EFI_FVB2_WRITE_STATUS_STRING,      \r
-  EFI_FVB2_LOCK_CAP_STRING,          \r
-  EFI_FVB2_LOCK_STATUS_STRING,       \r
+  EFI_FVB2_WRITE_ENABLED_CAP_STRING,\r
+  EFI_FVB2_WRITE_STATUS_STRING,\r
+  EFI_FVB2_LOCK_CAP_STRING,\r
+  EFI_FVB2_LOCK_STATUS_STRING,\r
   NULL,\r
-  EFI_FVB2_STICKY_WRITE_STRING,      \r
-  EFI_FVB2_MEMORY_MAPPED_STRING,     \r
-  EFI_FVB2_ERASE_POLARITY_STRING,    \r
-  EFI_FVB2_READ_LOCK_CAP_STRING,     \r
-  EFI_FVB2_READ_LOCK_STATUS_STRING,  \r
-  EFI_FVB2_WRITE_LOCK_CAP_STRING,    \r
-  EFI_FVB2_WRITE_LOCK_STATUS_STRING \r
+  EFI_FVB2_STICKY_WRITE_STRING,\r
+  EFI_FVB2_MEMORY_MAPPED_STRING,\r
+  EFI_FVB2_ERASE_POLARITY_STRING,\r
+  EFI_FVB2_READ_LOCK_CAP_STRING,\r
+  EFI_FVB2_READ_LOCK_STATUS_STRING,\r
+  EFI_FVB2_WRITE_LOCK_CAP_STRING,\r
+  EFI_FVB2_WRITE_LOCK_STATUS_STRING\r
 };\r
 \r
 CHAR8      *mFvbAlignmentName[] = {\r
-  EFI_FVB2_ALIGNMENT_1_STRING,   \r
-  EFI_FVB2_ALIGNMENT_2_STRING,   \r
-  EFI_FVB2_ALIGNMENT_4_STRING,   \r
-  EFI_FVB2_ALIGNMENT_8_STRING,   \r
-  EFI_FVB2_ALIGNMENT_16_STRING,  \r
-  EFI_FVB2_ALIGNMENT_32_STRING,  \r
-  EFI_FVB2_ALIGNMENT_64_STRING,  \r
-  EFI_FVB2_ALIGNMENT_128_STRING, \r
-  EFI_FVB2_ALIGNMENT_256_STRING, \r
-  EFI_FVB2_ALIGNMENT_512_STRING, \r
-  EFI_FVB2_ALIGNMENT_1K_STRING,  \r
-  EFI_FVB2_ALIGNMENT_2K_STRING,  \r
-  EFI_FVB2_ALIGNMENT_4K_STRING,  \r
-  EFI_FVB2_ALIGNMENT_8K_STRING,  \r
-  EFI_FVB2_ALIGNMENT_16K_STRING, \r
-  EFI_FVB2_ALIGNMENT_32K_STRING, \r
-  EFI_FVB2_ALIGNMENT_64K_STRING, \r
+  EFI_FVB2_ALIGNMENT_1_STRING,\r
+  EFI_FVB2_ALIGNMENT_2_STRING,\r
+  EFI_FVB2_ALIGNMENT_4_STRING,\r
+  EFI_FVB2_ALIGNMENT_8_STRING,\r
+  EFI_FVB2_ALIGNMENT_16_STRING,\r
+  EFI_FVB2_ALIGNMENT_32_STRING,\r
+  EFI_FVB2_ALIGNMENT_64_STRING,\r
+  EFI_FVB2_ALIGNMENT_128_STRING,\r
+  EFI_FVB2_ALIGNMENT_256_STRING,\r
+  EFI_FVB2_ALIGNMENT_512_STRING,\r
+  EFI_FVB2_ALIGNMENT_1K_STRING,\r
+  EFI_FVB2_ALIGNMENT_2K_STRING,\r
+  EFI_FVB2_ALIGNMENT_4K_STRING,\r
+  EFI_FVB2_ALIGNMENT_8K_STRING,\r
+  EFI_FVB2_ALIGNMENT_16K_STRING,\r
+  EFI_FVB2_ALIGNMENT_32K_STRING,\r
+  EFI_FVB2_ALIGNMENT_64K_STRING,\r
   EFI_FVB2_ALIGNMENT_128K_STRING,\r
   EFI_FVB2_ALIGNMENT_256K_STRING,\r
   EFI_FVB2_ALIGNMENT_512K_STRING,\r
-  EFI_FVB2_ALIGNMENT_1M_STRING,  \r
-  EFI_FVB2_ALIGNMENT_2M_STRING,  \r
-  EFI_FVB2_ALIGNMENT_4M_STRING,  \r
-  EFI_FVB2_ALIGNMENT_8M_STRING,  \r
-  EFI_FVB2_ALIGNMENT_16M_STRING, \r
-  EFI_FVB2_ALIGNMENT_32M_STRING, \r
-  EFI_FVB2_ALIGNMENT_64M_STRING, \r
+  EFI_FVB2_ALIGNMENT_1M_STRING,\r
+  EFI_FVB2_ALIGNMENT_2M_STRING,\r
+  EFI_FVB2_ALIGNMENT_4M_STRING,\r
+  EFI_FVB2_ALIGNMENT_8M_STRING,\r
+  EFI_FVB2_ALIGNMENT_16M_STRING,\r
+  EFI_FVB2_ALIGNMENT_32M_STRING,\r
+  EFI_FVB2_ALIGNMENT_64M_STRING,\r
   EFI_FVB2_ALIGNMENT_128M_STRING,\r
   EFI_FVB2_ALIGNMENT_256M_STRING,\r
   EFI_FVB2_ALIGNMENT_512M_STRING,\r
-  EFI_FVB2_ALIGNMENT_1G_STRING,  \r
+  EFI_FVB2_ALIGNMENT_1G_STRING,\r
   EFI_FVB2_ALIGNMENT_2G_STRING\r
 };\r
 \r
@@ -159,6 +165,7 @@ UINT8                                   m64kRecoveryStartupApDataArray[SIZEOF_ST
 \r
 FV_INFO                     mFvDataInfo;\r
 CAP_INFO                    mCapDataInfo;\r
+BOOLEAN                     mIsLargeFfs = FALSE;\r
 \r
 EFI_PHYSICAL_ADDRESS mFvBaseAddress[0x10];\r
 UINT32               mFvBaseAddressNumber = 0;\r
@@ -186,7 +193,7 @@ Returns:
   EFI_NOT_FOUND     A required string was not found in the INF file.\r
 --*/\r
 {\r
-  CHAR8       Value[_MAX_PATH];\r
+  CHAR8       Value[MAX_LONG_FILE_PATH];\r
   UINT64      Value64;\r
   UINTN       Index;\r
   UINTN       Number;\r
@@ -251,7 +258,7 @@ Returns:
     //\r
     strcpy (FvInfo->FvName, Value);\r
   }\r
-  \r
+\r
   //\r
   // Read Fv Attribute\r
   //\r
@@ -280,6 +287,19 @@ Returns:
     }\r
   }\r
 \r
+  //\r
+  // Read weak alignment flag\r
+  //\r
+  Status = FindToken (InfFile, ATTRIBUTES_SECTION_STRING, EFI_FV_WEAK_ALIGNMENT_STRING, 0, Value);\r
+  if (Status == EFI_SUCCESS) {\r
+    if ((strcmp (Value, TRUE_STRING) == 0) || (strcmp (Value, ONE_STRING) == 0)) {\r
+      FvInfo->FvAttributes |= EFI_FVB2_WEAK_ALIGNMENT;\r
+    } else if ((strcmp (Value, FALSE_STRING) != 0) && (strcmp (Value, ZERO_STRING) != 0)) {\r
+      Error (NULL, 0, 2000, "Invalid parameter", "Weak alignment value expected one of TRUE, FALSE, 1 or 0.");\r
+      return EFI_ABORTED;\r
+    }\r
+  }\r
+\r
   //\r
   // Read block maps\r
   //\r
@@ -355,7 +375,7 @@ Returns:
     }\r
   }\r
 \r
-  for (Index = 0; Index < MAX_NUMBER_OF_FILES_IN_FV; Index++) {\r
+  for (Index = 0; Number + Index < MAX_NUMBER_OF_FILES_IN_FV; Index++) {\r
     //\r
     // Read the FFS file list\r
     //\r
@@ -389,7 +409,7 @@ UpdateFfsFileState (
 Routine Description:\r
 \r
   This function changes the FFS file attributes based on the erase polarity\r
-  of the FV. Update the reserved bits of State to EFI_FVB2_ERASE_POLARITY. \r
+  of the FV. Update the reserved bits of State to EFI_FVB2_ERASE_POLARITY.\r
 \r
 Arguments:\r
 \r
@@ -444,58 +464,98 @@ Returns:
 \r
   case 0:\r
     //\r
-    // 8 byte alignment, mini alignment requirement for FFS file. \r
+    // 1 byte alignment\r
+    //if bit 1 have set, 128K byte alignmnet\r
     //\r
-    *Alignment = 3;\r
+    if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {\r
+      *Alignment = 17;\r
+    } else {\r
+      *Alignment = 0;\r
+    }\r
     break;\r
 \r
   case 1:\r
     //\r
     // 16 byte alignment\r
+    //if bit 1 have set, 256K byte alignment\r
     //\r
-    *Alignment = 4;\r
+    if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {\r
+      *Alignment = 18;\r
+    } else {\r
+      *Alignment = 4;\r
+    }\r
     break;\r
 \r
   case 2:\r
     //\r
     // 128 byte alignment\r
+    //if bit 1 have set, 512K byte alignment\r
     //\r
-    *Alignment = 7;\r
+    if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {\r
+      *Alignment = 19;\r
+    } else {\r
+      *Alignment = 7;\r
+    }\r
     break;\r
 \r
   case 3:\r
     //\r
     // 512 byte alignment\r
+    //if bit 1 have set, 1M byte alignment\r
     //\r
-    *Alignment = 9;\r
+    if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {\r
+      *Alignment = 20;\r
+    } else {\r
+      *Alignment = 9;\r
+    }\r
     break;\r
 \r
   case 4:\r
     //\r
     // 1K byte alignment\r
+    //if bit 1 have set, 2M byte alignment\r
     //\r
-    *Alignment = 10;\r
+    if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {\r
+      *Alignment = 21;\r
+    } else {\r
+      *Alignment = 10;\r
+    }\r
     break;\r
 \r
   case 5:\r
     //\r
     // 4K byte alignment\r
+    //if bit 1 have set, 4M byte alignment\r
     //\r
-    *Alignment = 12;\r
+    if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {\r
+      *Alignment = 22;\r
+    } else {\r
+      *Alignment = 12;\r
+    }\r
     break;\r
 \r
   case 6:\r
     //\r
     // 32K byte alignment\r
+    //if bit 1 have set , 8M byte alignment\r
     //\r
-    *Alignment = 15;\r
+    if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {\r
+      *Alignment = 23;\r
+    } else {\r
+      *Alignment = 15;\r
+    }\r
     break;\r
 \r
   case 7:\r
     //\r
     // 64K byte alignment\r
+    //if bit 1 have set, 16M alignment\r
     //\r
-    *Alignment = 16;\r
+    if (FfsFile->Attributes & FFS_ATTRIB_DATA_ALIGNMENT2) {\r
+      *Alignment = 24;\r
+    } else {\r
+      *Alignment = 16;\r
+    }\r
     break;\r
 \r
   default:\r
@@ -510,7 +570,8 @@ AddPadFile (
   IN OUT MEMORY_FILE  *FvImage,\r
   IN UINT32           DataAlignment,\r
   IN VOID             *FvEnd,\r
-  IN EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtHeader\r
+  IN EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtHeader,\r
+  IN UINT32           NextFfsSize\r
   )\r
 /*++\r
 \r
@@ -525,7 +586,7 @@ Arguments:
                   The current offset must be valid.\r
   DataAlignment   The data alignment of the next FFS file.\r
   FvEnd           End of the empty data in FvImage.\r
-  ExtHeader       PI FvExtHeader Optional \r
+  ExtHeader       PI FvExtHeader Optional\r
 \r
 Returns:\r
 \r
@@ -538,7 +599,12 @@ Returns:
 {\r
   EFI_FFS_FILE_HEADER *PadFile;\r
   UINTN               PadFileSize;\r
+  UINT32              NextFfsHeaderSize;\r
+  UINT32              CurFfsHeaderSize;\r
+  UINT32              Index;\r
 \r
+  Index = 0;\r
+  CurFfsHeaderSize = sizeof (EFI_FFS_FILE_HEADER);\r
   //\r
   // Verify input parameters.\r
   //\r
@@ -546,43 +612,45 @@ Returns:
     return EFI_INVALID_PARAMETER;\r
   }\r
 \r
-  //\r
-  // Check if a pad file is necessary\r
-  //\r
-  if ((ExtHeader == NULL) && (((UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + sizeof (EFI_FFS_FILE_HEADER)) % DataAlignment == 0)) {\r
-    return EFI_SUCCESS;\r
-  }\r
-\r
   //\r
   // Calculate the pad file size\r
   //\r
-  //\r
-  // This is the earliest possible valid offset (current plus pad file header\r
-  // plus the next file header)\r
-  //\r
-  PadFileSize = (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + (sizeof (EFI_FFS_FILE_HEADER) * 2);\r
 \r
-  //\r
-  // Add whatever it takes to get to the next aligned address\r
-  //\r
-  while ((PadFileSize % DataAlignment) != 0) {\r
-    PadFileSize++;\r
-  }\r
-  //\r
-  // Subtract the next file header size\r
-  //\r
-  PadFileSize -= sizeof (EFI_FFS_FILE_HEADER);\r
-\r
-  //\r
-  // Subtract the starting offset to get size\r
-  //\r
-  PadFileSize -= (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage;\r
-  \r
   //\r
   // Append extension header size\r
   //\r
   if (ExtHeader != NULL) {\r
-    PadFileSize = PadFileSize + ExtHeader->ExtHeaderSize;\r
+    PadFileSize = ExtHeader->ExtHeaderSize;\r
+    if (PadFileSize + sizeof (EFI_FFS_FILE_HEADER) >= MAX_FFS_SIZE) {\r
+      CurFfsHeaderSize = sizeof (EFI_FFS_FILE_HEADER2);\r
+    }\r
+    PadFileSize += CurFfsHeaderSize;\r
+  } else {\r
+    NextFfsHeaderSize = sizeof (EFI_FFS_FILE_HEADER);\r
+    if (NextFfsSize >= MAX_FFS_SIZE) {\r
+      NextFfsHeaderSize = sizeof (EFI_FFS_FILE_HEADER2);\r
+    }\r
+    //\r
+    // Check if a pad file is necessary\r
+    //\r
+    if (((UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + NextFfsHeaderSize) % DataAlignment == 0) {\r
+      return EFI_SUCCESS;\r
+    }\r
+    PadFileSize = (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + sizeof (EFI_FFS_FILE_HEADER) + NextFfsHeaderSize;\r
+    //\r
+    // Add whatever it takes to get to the next aligned address\r
+    //\r
+    while ((PadFileSize % DataAlignment) != 0) {\r
+      PadFileSize++;\r
+    }\r
+    //\r
+    // Subtract the next file header size\r
+    //\r
+    PadFileSize -= NextFfsHeaderSize;\r
+    //\r
+    // Subtract the starting offset to get size\r
+    //\r
+    PadFileSize -= (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage;\r
   }\r
 \r
   //\r
@@ -606,9 +674,15 @@ Returns:
   //\r
   // Write pad file size (calculated size minus next file header size)\r
   //\r
-  PadFile->Size[0]  = (UINT8) (PadFileSize & 0xFF);\r
-  PadFile->Size[1]  = (UINT8) ((PadFileSize >> 8) & 0xFF);\r
-  PadFile->Size[2]  = (UINT8) ((PadFileSize >> 16) & 0xFF);\r
+  if (PadFileSize >= MAX_FFS_SIZE) {\r
+    memset(PadFile->Size, 0, sizeof(UINT8) * 3);\r
+    ((EFI_FFS_FILE_HEADER2 *)PadFile)->ExtendedSize = PadFileSize;\r
+    PadFile->Attributes |= FFS_ATTRIB_LARGE_FILE;\r
+  } else {\r
+    PadFile->Size[0]  = (UINT8) (PadFileSize & 0xFF);\r
+    PadFile->Size[1]  = (UINT8) ((PadFileSize >> 8) & 0xFF);\r
+    PadFile->Size[2]  = (UINT8) ((PadFileSize >> 16) & 0xFF);\r
+  }\r
 \r
   //\r
   // Fill in checksums and state, they must be 0 for checksumming.\r
@@ -616,7 +690,7 @@ Returns:
   PadFile->IntegrityCheck.Checksum.Header = 0;\r
   PadFile->IntegrityCheck.Checksum.File   = 0;\r
   PadFile->State                          = 0;\r
-  PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, sizeof (EFI_FFS_FILE_HEADER));\r
+  PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, CurFfsHeaderSize);\r
   PadFile->IntegrityCheck.Checksum.File   = FFS_FIXED_CHECKSUM;\r
 \r
   PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;\r
@@ -634,14 +708,27 @@ Returns:
     //\r
     // Copy Fv Extension Header and Set Fv Extension header offset\r
     //\r
-    memcpy (PadFile + 1, ExtHeader, ExtHeader->ExtHeaderSize);\r
-    ((EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage)->ExtHeaderOffset = (UINT16) ((UINTN) (PadFile + 1) - (UINTN) FvImage->FileImage);\r
-         //\r
-         // Make next file start at QWord Boundry\r
-         //\r
-         while (((UINTN) FvImage->CurrentFilePointer & (EFI_FFS_FILE_HEADER_ALIGNMENT - 1)) != 0) {\r
-           FvImage->CurrentFilePointer++;\r
-         }\r
+    if (ExtHeader->ExtHeaderSize > sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER)) {\r
+      for (Index = sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER); Index < ExtHeader->ExtHeaderSize;) {\r
+        if (((EFI_FIRMWARE_VOLUME_EXT_ENTRY *)((UINT8 *)ExtHeader + Index))-> ExtEntryType == EFI_FV_EXT_TYPE_USED_SIZE_TYPE) {\r
+          if (VtfFileFlag) {\r
+            ((EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *)((UINT8 *)ExtHeader + Index))->UsedSize = mFvTotalSize;\r
+          } else {\r
+            ((EFI_FIRMWARE_VOLUME_EXT_ENTRY_USED_SIZE_TYPE *)((UINT8 *)ExtHeader + Index))->UsedSize = mFvTakenSize;\r
+          }\r
+          break;\r
+        }\r
+        Index += ((EFI_FIRMWARE_VOLUME_EXT_ENTRY *)((UINT8 *)ExtHeader + Index))-> ExtEntrySize;\r
+      }\r
+    }\r
+    memcpy ((UINT8 *)PadFile + CurFfsHeaderSize, ExtHeader, ExtHeader->ExtHeaderSize);\r
+    ((EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage)->ExtHeaderOffset = (UINT16) ((UINTN) ((UINT8 *)PadFile + CurFfsHeaderSize) - (UINTN) FvImage->FileImage);\r
+    //\r
+    // Make next file start at QWord Boundry\r
+    //\r
+    while (((UINTN) FvImage->CurrentFilePointer & (EFI_FFS_FILE_HEADER_ALIGNMENT - 1)) != 0) {\r
+      FvImage->CurrentFilePointer++;\r
+    }\r
   }\r
 \r
   return EFI_SUCCESS;\r
@@ -679,7 +766,7 @@ EFI_STATUS
 WriteMapFile (\r
   IN OUT FILE                  *FvMapFile,\r
   IN     CHAR8                 *FileName,\r
-  IN     EFI_FFS_FILE_HEADER   *FfsFile, \r
+  IN     EFI_FFS_FILE_HEADER   *FfsFile,\r
   IN     EFI_PHYSICAL_ADDRESS  ImageBaseAddress,\r
   IN     PE_COFF_LOADER_IMAGE_CONTEXT *pImageContext\r
   )\r
@@ -704,7 +791,7 @@ Returns:
 \r
 --*/\r
 {\r
-  CHAR8                               PeMapFileName [_MAX_PATH];\r
+  CHAR8                               PeMapFileName [MAX_LONG_FILE_PATH];\r
   CHAR8                               *Cptr, *Cptr2;\r
   CHAR8                               FileGuidName [MAX_LINE_LEN];\r
   FILE                                *PeMapFile;\r
@@ -720,7 +807,7 @@ Returns:
   EFI_IMAGE_OPTIONAL_HEADER_UNION     *ImgHdr;\r
   EFI_TE_IMAGE_HEADER                 *TEImageHeader;\r
   EFI_IMAGE_SECTION_HEADER            *SectionHeader;\r
-  unsigned long long                  TempLongAddress;\r
+  long long                           TempLongAddress;\r
   UINT32                              TextVirtualAddress;\r
   UINT32                              DataVirtualAddress;\r
   EFI_PHYSICAL_ADDRESS                LinkTimeBaseAddress;\r
@@ -730,15 +817,19 @@ Returns:
   //\r
   FunctionType = 0;\r
   //\r
-  // Print FileGuid to string buffer. \r
+  // Print FileGuid to string buffer.\r
   //\r
   PrintGuidToBuffer (&FfsFile->Name, (UINT8 *)FileGuidName, MAX_LINE_LEN, TRUE);\r
-  \r
+\r
   //\r
-  // Construct Map file Name \r
+  // Construct Map file Name\r
   //\r
-  strcpy (PeMapFileName, FileName);\r
-  \r
+  if (strlen (FileName) >= MAX_LONG_FILE_PATH) {\r
+    return EFI_ABORTED;\r
+  }\r
+  strncpy (PeMapFileName, FileName, MAX_LONG_FILE_PATH - 1);\r
+  PeMapFileName[MAX_LONG_FILE_PATH - 1] = 0;\r
+\r
   //\r
   // Change '\\' to '/', unified path format.\r
   //\r
@@ -749,10 +840,10 @@ Returns:
     }\r
     Cptr ++;\r
   }\r
-  \r
+\r
   //\r
   // Get Map file\r
-  // \r
+  //\r
   Cptr = PeMapFileName + strlen (PeMapFileName);\r
   while ((*Cptr != '.') && (Cptr >= PeMapFileName)) {\r
     Cptr --;\r
@@ -773,26 +864,30 @@ Returns:
   while ((*Cptr != FILE_SEP_CHAR) && (Cptr >= PeMapFileName)) {\r
     Cptr --;\r
   }\r
-       *Cptr2 = '\0';\r
-       strcpy (KeyWord, Cptr + 1);\r
-       *Cptr2 = '.';\r
+  *Cptr2 = '\0';\r
+  if (strlen (Cptr + 1) >= MAX_LINE_LEN) {\r
+    return EFI_ABORTED;\r
+  }\r
+  strncpy (KeyWord, Cptr + 1, MAX_LINE_LEN - 1);\r
+  KeyWord[MAX_LINE_LEN - 1] = 0;\r
+  *Cptr2 = '.';\r
 \r
   //\r
   // AddressOfEntryPoint and Offset in Image\r
   //\r
   if (!pImageContext->IsTeImage) {\r
-       ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINT8 *) pImageContext->Handle + pImageContext->PeCoffHeaderOffset);\r
-       AddressOfEntryPoint = ImgHdr->Pe32.OptionalHeader.AddressOfEntryPoint;\r
-       Offset = 0;\r
+    ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINT8 *) pImageContext->Handle + pImageContext->PeCoffHeaderOffset);\r
+    AddressOfEntryPoint = ImgHdr->Pe32.OptionalHeader.AddressOfEntryPoint;\r
+    Offset = 0;\r
     SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (\r
                        (UINT8 *) ImgHdr +\r
-                       sizeof (UINT32) + \r
-                       sizeof (EFI_IMAGE_FILE_HEADER) +  \r
+                       sizeof (UINT32) +\r
+                       sizeof (EFI_IMAGE_FILE_HEADER) +\r
                        ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
                        );\r
     Index = ImgHdr->Pe32.FileHeader.NumberOfSections;\r
   } else {\r
-       TEImageHeader = (EFI_TE_IMAGE_HEADER *) pImageContext->Handle;\r
+    TEImageHeader = (EFI_TE_IMAGE_HEADER *) pImageContext->Handle;\r
     AddressOfEntryPoint = TEImageHeader->AddressOfEntryPoint;\r
     Offset = TEImageHeader->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER);\r
     SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (TEImageHeader + 1);\r
@@ -812,41 +907,41 @@ Returns:
 \r
   if (FfsFile->Type != EFI_FV_FILETYPE_SECURITY_CORE && pImageContext->Machine == EFI_IMAGE_MACHINE_IA64) {\r
     //\r
-    // Process IPF PLABEL to get the real address after the image has been rebased. \r
+    // Process IPF PLABEL to get the real address after the image has been rebased.\r
     // PLABEL structure is got by AddressOfEntryPoint offset to ImageBuffer stored in pImageContext->Handle.\r
     //\r
     fprintf (FvMapFile, "EntryPoint=0x%010llx", (unsigned long long) (*(UINT64 *)((UINTN) pImageContext->Handle + (UINTN) AddressOfEntryPoint)));\r
   } else {\r
     fprintf (FvMapFile, "EntryPoint=0x%010llx", (unsigned long long) (ImageBaseAddress + AddressOfEntryPoint));\r
   }\r
-  fprintf (FvMapFile, ")\n"); \r
-  \r
+  fprintf (FvMapFile, ")\n");\r
+\r
   fprintf (FvMapFile, "(GUID=%s", FileGuidName);\r
   TextVirtualAddress = 0;\r
   DataVirtualAddress = 0;\r
   for (; Index > 0; Index --, SectionHeader ++) {\r
     if (stricmp ((CHAR8 *)SectionHeader->Name, ".text") == 0) {\r
-               TextVirtualAddress = SectionHeader->VirtualAddress;\r
-       } else if (stricmp ((CHAR8 *)SectionHeader->Name, ".data") == 0) {\r
-         DataVirtualAddress = SectionHeader->VirtualAddress;\r
-       } else if (stricmp ((CHAR8 *)SectionHeader->Name, ".sdata") == 0) {\r
-         DataVirtualAddress = SectionHeader->VirtualAddress;\r
-       }\r
+      TextVirtualAddress = SectionHeader->VirtualAddress;\r
+    } else if (stricmp ((CHAR8 *)SectionHeader->Name, ".data") == 0) {\r
+      DataVirtualAddress = SectionHeader->VirtualAddress;\r
+    } else if (stricmp ((CHAR8 *)SectionHeader->Name, ".sdata") == 0) {\r
+      DataVirtualAddress = SectionHeader->VirtualAddress;\r
+    }\r
   }\r
   fprintf (FvMapFile, " .textbaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress + TextVirtualAddress));\r
   fprintf (FvMapFile, " .databaseaddress=0x%010llx", (unsigned long long) (ImageBaseAddress + DataVirtualAddress));\r
   fprintf (FvMapFile, ")\n\n");\r
-   \r
+\r
   //\r
   // Open PeMapFile\r
   //\r
-  PeMapFile = fopen (PeMapFileName, "r");\r
+  PeMapFile = fopen (LongFilePath (PeMapFileName), "r");\r
   if (PeMapFile == NULL) {\r
     // fprintf (stdout, "can't open %s file to reading\n", PeMapFileName);\r
     return EFI_ABORTED;\r
   }\r
   VerboseMsg ("The map file is %s", PeMapFileName);\r
-  \r
+\r
   //\r
   // Output Functions information into Fv Map file\r
   //\r
@@ -861,7 +956,7 @@ Returns:
     }\r
     //\r
     // By Address and Static keyword\r
-    //  \r
+    //\r
     if (FunctionType == 0) {\r
       sscanf (Line, "%s", KeyWord);\r
       if (stricmp (KeyWord, "Address") == 0) {\r
@@ -906,10 +1001,157 @@ Returns:
   //\r
   fprintf (FvMapFile, "\n\n");\r
   fclose (PeMapFile);\r
-  \r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
+STATIC\r
+BOOLEAN\r
+AdjustInternalFfsPadding (\r
+  IN OUT  EFI_FFS_FILE_HEADER   *FfsFile,\r
+  IN OUT  MEMORY_FILE           *FvImage,\r
+  IN      UINTN                 Alignment,\r
+  IN OUT  UINTN                 *FileSize\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  This function looks for a dedicated alignment padding section in the FFS, and\r
+  shrinks it to the size required to line up subsequent sections correctly.\r
+\r
+Arguments:\r
+\r
+  FfsFile               A pointer to Ffs file image.\r
+  FvImage               The memory image of the FV to adjust it to.\r
+  Alignment             Current file alignment\r
+  FileSize              Reference to a variable holding the size of the FFS file\r
+\r
+Returns:\r
+\r
+  TRUE                  Padding section was found and updated successfully\r
+  FALSE                 Otherwise\r
+\r
+--*/\r
+{\r
+  EFI_FILE_SECTION_POINTER  PadSection;\r
+  UINT8                     *Remainder;\r
+  EFI_STATUS                Status;\r
+  UINT32                    FfsHeaderLength;\r
+  UINT32                    FfsFileLength;\r
+  UINT32                    PadSize;\r
+  UINTN                     Misalignment;\r
+  EFI_FFS_INTEGRITY_CHECK   *IntegrityCheck;\r
+\r
+  //\r
+  // Figure out the misalignment: all FFS sections are aligned relative to the\r
+  // start of the FFS payload, so use that as the base of the misalignment\r
+  // computation.\r
+  //\r
+  FfsHeaderLength = GetFfsHeaderLength(FfsFile);\r
+  Misalignment = (UINTN) FvImage->CurrentFilePointer -\r
+                 (UINTN) FvImage->FileImage + FfsHeaderLength;\r
+  Misalignment &= Alignment - 1;\r
+  if (Misalignment == 0) {\r
+    // Nothing to do, return success\r
+    return TRUE;\r
+  }\r
+\r
+  //\r
+  // We only apply this optimization to FFS files with the FIXED attribute set,\r
+  // since the FFS will not be loadable at arbitrary offsets anymore after\r
+  // we adjust the size of the padding section.\r
+  //\r
+  if ((FfsFile->Attributes & FFS_ATTRIB_FIXED) == 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Look for a dedicated padding section that we can adjust to compensate\r
+  // for the misalignment. If such a padding section exists, it precedes all\r
+  // sections with alignment requirements, and so the adjustment will correct\r
+  // all of them.\r
+  //\r
+  Status = GetSectionByType (FfsFile, EFI_SECTION_FREEFORM_SUBTYPE_GUID, 1,\r
+             &PadSection);\r
+  if (EFI_ERROR (Status) ||\r
+      CompareGuid (&PadSection.FreeformSubtypeSection->SubTypeGuid,\r
+        &mEfiFfsSectionAlignmentPaddingGuid) != 0) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Find out if the size of the padding section is sufficient to compensate\r
+  // for the misalignment.\r
+  //\r
+  PadSize = GetSectionFileLength (PadSection.CommonHeader);\r
+  if (Misalignment > PadSize - sizeof (EFI_FREEFORM_SUBTYPE_GUID_SECTION)) {\r
+    return FALSE;\r
+  }\r
+\r
+  //\r
+  // Move the remainder of the FFS file towards the front, and adjust the\r
+  // file size output parameter.\r
+  //\r
+  Remainder = (UINT8 *) PadSection.CommonHeader + PadSize;\r
+  memmove (Remainder - Misalignment, Remainder,\r
+           *FileSize - (UINTN) (Remainder - (UINTN) FfsFile));\r
+  *FileSize -= Misalignment;\r
+\r
+  //\r
+  // Update the padding section's length with the new values. Note that the\r
+  // padding is always < 64 KB, so we can ignore EFI_COMMON_SECTION_HEADER2\r
+  // ExtendedSize.\r
+  //\r
+  PadSize -= Misalignment;\r
+  PadSection.CommonHeader->Size[0] = (UINT8) (PadSize & 0xff);\r
+  PadSection.CommonHeader->Size[1] = (UINT8) ((PadSize & 0xff00) >> 8);\r
+  PadSection.CommonHeader->Size[2] = (UINT8) ((PadSize & 0xff0000) >> 16);\r
+\r
+  //\r
+  // Update the FFS header with the new overall length\r
+  //\r
+  FfsFileLength = GetFfsFileLength (FfsFile) - Misalignment;\r
+  if (FfsHeaderLength > sizeof(EFI_FFS_FILE_HEADER)) {\r
+    ((EFI_FFS_FILE_HEADER2 *)FfsFile)->ExtendedSize = FfsFileLength;\r
+  } else {\r
+    FfsFile->Size[0] = (UINT8) (FfsFileLength & 0x000000FF);\r
+    FfsFile->Size[1] = (UINT8) ((FfsFileLength & 0x0000FF00) >> 8);\r
+    FfsFile->Size[2] = (UINT8) ((FfsFileLength & 0x00FF0000) >> 16);\r
+  }\r
+\r
+  //\r
+  // Clear the alignment bits: these have become meaningless now that we have\r
+  // adjusted the padding section.\r
+  //\r
+  FfsFile->Attributes &= ~(FFS_ATTRIB_DATA_ALIGNMENT | FFS_ATTRIB_DATA_ALIGNMENT2);\r
+\r
+  //\r
+  // Recalculate the FFS header checksum. Instead of setting Header and State\r
+  // both to zero, set Header to (UINT8)(-State) so State preserves its original\r
+  // value\r
+  //\r
+  IntegrityCheck = &FfsFile->IntegrityCheck;\r
+  IntegrityCheck->Checksum.Header = (UINT8) (0x100 - FfsFile->State);\r
+  IntegrityCheck->Checksum.File = 0;\r
+\r
+  IntegrityCheck->Checksum.Header = CalculateChecksum8 (\r
+                                      (UINT8 *) FfsFile, FfsHeaderLength);\r
+\r
+  if (FfsFile->Attributes & FFS_ATTRIB_CHECKSUM) {\r
+    //\r
+    // Ffs header checksum = zero, so only need to calculate ffs body.\r
+    //\r
+    IntegrityCheck->Checksum.File = CalculateChecksum8 (\r
+                                      (UINT8 *) FfsFile + FfsHeaderLength,\r
+                                      FfsFileLength - FfsHeaderLength);\r
+  } else {\r
+    IntegrityCheck->Checksum.File = FFS_FIXED_CHECKSUM;\r
+  }\r
+\r
+  return TRUE;\r
+}\r
+\r
 EFI_STATUS\r
 AddFile (\r
   IN OUT MEMORY_FILE          *FvImage,\r
@@ -954,7 +1196,7 @@ Returns:
   EFI_STATUS            Status;\r
   UINTN                 Index1;\r
   UINT8                 FileGuidString[PRINTED_GUID_BUFFER_SIZE];\r
-  \r
+\r
   Index1 = 0;\r
   //\r
   // Verify input parameters.\r
@@ -966,7 +1208,7 @@ Returns:
   //\r
   // Read the file to add\r
   //\r
-  NewFile = fopen (FvInfo->FvFiles[Index], "rb");\r
+  NewFile = fopen (LongFilePath (FvInfo->FvFiles[Index]), "rb");\r
 \r
   if (NewFile == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", FvInfo->FvFiles[Index]);\r
@@ -983,6 +1225,7 @@ Returns:
   //\r
   FileBuffer = malloc (FileSize);\r
   if (FileBuffer == NULL) {\r
+    fclose (NewFile);\r
     Error (NULL, 0, 4001, "Resouce", "memory cannot be allocated!");\r
     return EFI_OUT_OF_RESOURCES;\r
   }\r
@@ -993,7 +1236,7 @@ Returns:
   // Done with the file, from this point on we will just use the buffer read.\r
   //\r
   fclose (NewFile);\r
-  \r
+\r
   //\r
   // Verify read successful\r
   //\r
@@ -1002,20 +1245,20 @@ Returns:
     Error (NULL, 0, 0004, "Error reading file", FvInfo->FvFiles[Index]);\r
     return EFI_ABORTED;\r
   }\r
-  \r
+\r
   //\r
   // For None PI Ffs file, directly add them into FvImage.\r
   //\r
   if (!FvInfo->IsPiFvImage) {\r
     memcpy (FvImage->CurrentFilePointer, FileBuffer, FileSize);\r
     if (FvInfo->SizeofFvFiles[Index] > FileSize) {\r
-       FvImage->CurrentFilePointer += FvInfo->SizeofFvFiles[Index];\r
+      FvImage->CurrentFilePointer += FvInfo->SizeofFvFiles[Index];\r
     } else {\r
-       FvImage->CurrentFilePointer += FileSize;\r
+      FvImage->CurrentFilePointer += FileSize;\r
     }\r
     goto Done;\r
   }\r
-  \r
+\r
   //\r
   // Verify Ffs file\r
   //\r
@@ -1042,6 +1285,7 @@ Returns:
     if (CompareGuid ((EFI_GUID *) FileBuffer, &mFileGuidArray [Index1]) == 0) {\r
       Error (NULL, 0, 2000, "Invalid parameter", "the %dth file and %uth file have the same file GUID.", (unsigned) Index1 + 1, (unsigned) Index + 1);\r
       PrintGuid ((EFI_GUID *) FileBuffer);\r
+      free (FileBuffer);\r
       return EFI_INVALID_PARAMETER;\r
     }\r
   }\r
@@ -1059,7 +1303,7 @@ Returns:
   // Check if alignment is required\r
   //\r
   ReadFfsAlignment ((EFI_FFS_FILE_HEADER *) FileBuffer, &CurrentFileAlignment);\r
-  \r
+\r
   //\r
   // Find the largest alignment of all the FFS files in the FV\r
   //\r
@@ -1078,26 +1322,26 @@ Returns:
       //\r
       // Sanity check. The file MUST align appropriately\r
       //\r
-      if (((UINTN) *VtfFileImage + sizeof (EFI_FFS_FILE_HEADER) - (UINTN) FvImage->FileImage) % (1 << CurrentFileAlignment)) {\r
+      if (((UINTN) *VtfFileImage + GetFfsHeaderLength((EFI_FFS_FILE_HEADER *)FileBuffer) - (UINTN) FvImage->FileImage) % (1 << CurrentFileAlignment)) {\r
         Error (NULL, 0, 3000, "Invalid", "VTF file cannot be aligned on a %u-byte boundary.", (unsigned) (1 << CurrentFileAlignment));\r
         free (FileBuffer);\r
         return EFI_ABORTED;\r
       }\r
       //\r
-      // Rebase the PE or TE image in FileBuffer of FFS file for XIP \r
+      // Rebase the PE or TE image in FileBuffer of FFS file for XIP\r
       // Rebase for the debug genfvmap tool\r
       //\r
       Status = FfsRebase (FvInfo, FvInfo->FvFiles[Index], (EFI_FFS_FILE_HEADER *) FileBuffer, (UINTN) *VtfFileImage - (UINTN) FvImage->FileImage, FvMapFile);\r
       if (EFI_ERROR (Status)) {\r
         Error (NULL, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo->FvFiles[Index]);\r
         return Status;\r
-      }          \r
+      }\r
       //\r
       // copy VTF File\r
       //\r
       memcpy (*VtfFileImage, FileBuffer, FileSize);\r
-      \r
-      PrintGuidToBuffer ((EFI_GUID *) FileBuffer, FileGuidString, sizeof (FileGuidString), TRUE); \r
+\r
+      PrintGuidToBuffer ((EFI_GUID *) FileBuffer, FileGuidString, sizeof (FileGuidString), TRUE);\r
       fprintf (FvReportFile, "0x%08X %s\n", (unsigned)(UINTN) (((UINT8 *)*VtfFileImage) - (UINTN)FvImage->FileImage), FileGuidString);\r
 \r
       free (FileBuffer);\r
@@ -1116,30 +1360,33 @@ Returns:
   //\r
   // Add pad file if necessary\r
   //\r
-  Status = AddPadFile (FvImage, 1 << CurrentFileAlignment, *VtfFileImage, NULL);\r
-  if (EFI_ERROR (Status)) {\r
-    Error (NULL, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");\r
-    free (FileBuffer);\r
-    return EFI_ABORTED;\r
+  if (!AdjustInternalFfsPadding ((EFI_FFS_FILE_HEADER *) FileBuffer, FvImage,\r
+         1 << CurrentFileAlignment, &FileSize)) {\r
+    Status = AddPadFile (FvImage, 1 << CurrentFileAlignment, *VtfFileImage, NULL, FileSize);\r
+    if (EFI_ERROR (Status)) {\r
+      Error (NULL, 0, 4002, "Resource", "FV space is full, could not add pad file for data alignment property.");\r
+      free (FileBuffer);\r
+      return EFI_ABORTED;\r
+    }\r
   }\r
   //\r
   // Add file\r
   //\r
   if ((UINTN) (FvImage->CurrentFilePointer + FileSize) <= (UINTN) (*VtfFileImage)) {\r
     //\r
-    // Rebase the PE or TE image in FileBuffer of FFS file for XIP. \r
+    // Rebase the PE or TE image in FileBuffer of FFS file for XIP.\r
     // Rebase Bs and Rt drivers for the debug genfvmap tool.\r
     //\r
     Status = FfsRebase (FvInfo, FvInfo->FvFiles[Index], (EFI_FFS_FILE_HEADER *) FileBuffer, (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage, FvMapFile);\r
-       if (EFI_ERROR (Status)) {\r
-         Error (NULL, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo->FvFiles[Index]);\r
-         return Status;\r
-       }               \r
+  if (EFI_ERROR (Status)) {\r
+    Error (NULL, 0, 3000, "Invalid", "Could not rebase %s.", FvInfo->FvFiles[Index]);\r
+    return Status;\r
+  }\r
     //\r
     // Copy the file\r
     //\r
     memcpy (FvImage->CurrentFilePointer, FileBuffer, FileSize);\r
-    PrintGuidToBuffer ((EFI_GUID *) FileBuffer, FileGuidString, sizeof (FileGuidString), TRUE); \r
+    PrintGuidToBuffer ((EFI_GUID *) FileBuffer, FileGuidString, sizeof (FileGuidString), TRUE);\r
     fprintf (FvReportFile, "0x%08X %s\n", (unsigned) (FvImage->CurrentFilePointer - FvImage->FileImage), FileGuidString);\r
     FvImage->CurrentFilePointer += FileSize;\r
   } else {\r
@@ -1154,7 +1401,7 @@ Returns:
     FvImage->CurrentFilePointer++;\r
   }\r
 \r
-Done: \r
+Done:\r
   //\r
   // Free allocated memory.\r
   //\r
@@ -1190,6 +1437,7 @@ Returns:
 {\r
   EFI_FFS_FILE_HEADER *PadFile;\r
   UINTN               FileSize;\r
+  UINT32              FfsHeaderSize;\r
 \r
   //\r
   // If there is no VTF or the VTF naturally follows the previous file without a\r
@@ -1210,7 +1458,7 @@ Returns:
   PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;\r
 \r
   //\r
-  // write PadFile FFS header with PadType, don't need to set PAD file guid in its header. \r
+  // write PadFile FFS header with PadType, don't need to set PAD file guid in its header.\r
   //\r
   PadFile->Type       = EFI_FV_FILETYPE_FFS_PAD;\r
   PadFile->Attributes = 0;\r
@@ -1219,9 +1467,18 @@ Returns:
   // FileSize includes the EFI_FFS_FILE_HEADER\r
   //\r
   FileSize          = (UINTN) VtfFileImage - (UINTN) FvImage->CurrentFilePointer;\r
-  PadFile->Size[0]  = (UINT8) (FileSize & 0x000000FF);\r
-  PadFile->Size[1]  = (UINT8) ((FileSize & 0x0000FF00) >> 8);\r
-  PadFile->Size[2]  = (UINT8) ((FileSize & 0x00FF0000) >> 16);\r
+  if (FileSize >= MAX_FFS_SIZE) {\r
+    PadFile->Attributes |= FFS_ATTRIB_LARGE_FILE;\r
+    memset(PadFile->Size, 0, sizeof(UINT8) * 3);\r
+    ((EFI_FFS_FILE_HEADER2 *)PadFile)->ExtendedSize = FileSize;\r
+    FfsHeaderSize = sizeof(EFI_FFS_FILE_HEADER2);\r
+    mIsLargeFfs = TRUE;\r
+  } else {\r
+    PadFile->Size[0]  = (UINT8) (FileSize & 0x000000FF);\r
+    PadFile->Size[1]  = (UINT8) ((FileSize & 0x0000FF00) >> 8);\r
+    PadFile->Size[2]  = (UINT8) ((FileSize & 0x00FF0000) >> 16);\r
+    FfsHeaderSize = sizeof(EFI_FFS_FILE_HEADER);\r
+  }\r
 \r
   //\r
   // Fill in checksums and state, must be zero during checksum calculation.\r
@@ -1229,7 +1486,7 @@ Returns:
   PadFile->IntegrityCheck.Checksum.Header = 0;\r
   PadFile->IntegrityCheck.Checksum.File   = 0;\r
   PadFile->State                          = 0;\r
-  PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, sizeof (EFI_FFS_FILE_HEADER));\r
+  PadFile->IntegrityCheck.Checksum.Header = CalculateChecksum8 ((UINT8 *) PadFile, FfsHeaderSize);\r
   PadFile->IntegrityCheck.Checksum.File   = FFS_FIXED_CHECKSUM;\r
 \r
   PadFile->State = EFI_FILE_HEADER_CONSTRUCTION | EFI_FILE_HEADER_VALID | EFI_FILE_DATA_VALID;\r
@@ -1297,6 +1554,8 @@ Returns:
   UINT64                    FitAddress;\r
   FIT_TABLE                 *FitTablePtr;\r
   BOOLEAN                   Vtf0Detected;\r
+  UINT32                    FfsHeaderSize;\r
+  UINT32                    SecHeaderSize;\r
 \r
   //\r
   // Verify input parameters\r
@@ -1359,8 +1618,9 @@ Returns:
     return EFI_ABORTED;\r
   }\r
 \r
+  SecHeaderSize = GetSectionHeaderLength(Pe32Section.CommonHeader);\r
   Status = GetPe32Info (\r
-            (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),\r
+            (VOID *) ((UINTN) Pe32Section.Pe32Section + SecHeaderSize),\r
             &EntryPoint,\r
             &BaseOfCode,\r
             &MachineType\r
@@ -1369,7 +1629,7 @@ Returns:
   if (EFI_ERROR (Status)) {\r
     Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");\r
     return EFI_ABORTED;\r
-  }  \r
+  }\r
 \r
   if (\r
        Vtf0Detected &&\r
@@ -1388,9 +1648,9 @@ Returns:
   // Physical address is FV base + offset of PE32 + offset of the entry point\r
   //\r
   SecCorePhysicalAddress = FvInfo->BaseAddress;\r
-  SecCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;\r
+  SecCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + SecHeaderSize - (UINTN) FvImage->FileImage;\r
   SecCorePhysicalAddress += EntryPoint;\r
-  DebugMsg (NULL, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress); \r
+  DebugMsg (NULL, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress);\r
 \r
   //\r
   // Find the PEI Core\r
@@ -1413,8 +1673,9 @@ Returns:
     return EFI_ABORTED;\r
   }\r
 \r
+  SecHeaderSize = GetSectionHeaderLength(Pe32Section.CommonHeader);\r
   Status = GetPe32Info (\r
-            (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),\r
+            (VOID *) ((UINTN) Pe32Section.Pe32Section + SecHeaderSize),\r
             &EntryPoint,\r
             &BaseOfCode,\r
             &MachineType\r
@@ -1428,7 +1689,7 @@ Returns:
   // Physical address is FV base + offset of PE32 + offset of the entry point\r
   //\r
   PeiCorePhysicalAddress = FvInfo->BaseAddress;\r
-  PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;\r
+  PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + SecHeaderSize - (UINTN) FvImage->FileImage;\r
   PeiCorePhysicalAddress += EntryPoint;\r
   DebugMsg (NULL, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress);\r
 \r
@@ -1497,18 +1758,18 @@ Returns:
     // Write lower 32 bits of physical address for Pei Core entry\r
     //\r
     *Ia32ResetAddressPtr = (UINT32) PeiCorePhysicalAddress;\r
-    \r
+\r
     //\r
     // Write SecCore Entry point relative address into the jmp instruction in reset vector.\r
-    // \r
+    //\r
     Ia32ResetAddressPtr  = (UINT32 *) ((UINTN) FvImage->Eof - IA32_SEC_CORE_ENTRY_OFFSET);\r
-    \r
+\r
     Ia32SecEntryOffset   = (INT32) (SecCorePhysicalAddress - (FV_IMAGES_TOP_ADDRESS - IA32_SEC_CORE_ENTRY_OFFSET + 2));\r
     if (Ia32SecEntryOffset <= -65536) {\r
       Error (NULL, 0, 3000, "Invalid", "The SEC EXE file size is too large, it must be less than 64K.");\r
       return STATUS_ERROR;\r
     }\r
-    \r
+\r
     *(UINT16 *) Ia32ResetAddressPtr = (UINT16) Ia32SecEntryOffset;\r
 \r
     //\r
@@ -1558,9 +1819,9 @@ Returns:
     //\r
     WordPointer   = (UINT16 *) (BytePointer + SIZEOF_STARTUP_DATA_ARRAY - 2);\r
     *WordPointer  = (UINT16) (0x10000 - (UINT32) CheckSum);\r
-    \r
+\r
     //\r
-    // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV. \r
+    // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV.\r
     //\r
     IpiVector  = (UINT32) (FV_IMAGES_TOP_ADDRESS - ((UINTN) FvImage->Eof - (UINTN) BytePointer));\r
     DebugMsg (NULL, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", (unsigned) IpiVector);\r
@@ -1598,20 +1859,259 @@ Returns:
   VtfFile->IntegrityCheck.Checksum.File = 0;\r
   VtfFile->State                        = 0;\r
   if (VtfFile->Attributes & FFS_ATTRIB_CHECKSUM) {\r
+    FfsHeaderSize = GetFfsHeaderLength(VtfFile);\r
     VtfFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (\r
-                                              (UINT8 *) (VtfFile + 1),\r
-                                              GetLength (VtfFile->Size) - sizeof (EFI_FFS_FILE_HEADER)\r
+                                              (UINT8 *) ((UINT8 *)VtfFile + FfsHeaderSize),\r
+                                              GetFfsFileLength (VtfFile) - FfsHeaderSize\r
                                               );\r
   } else {\r
     VtfFile->IntegrityCheck.Checksum.File = FFS_FIXED_CHECKSUM;\r
   }\r
 \r
-  VtfFile->State = SavedState;\r
+  VtfFile->State = SavedState;\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+FindCorePeSection(\r
+  IN VOID                       *FvImageBuffer,\r
+  IN UINT64                     FvSize,\r
+  IN EFI_FV_FILETYPE            FileType,\r
+  OUT EFI_FILE_SECTION_POINTER  *Pe32Section\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Recursively searches the FV for the FFS file of specified type (typically\r
+  SEC or PEI core) and extracts the PE32 section for further processing.\r
+\r
+Arguments:\r
+\r
+  FvImageBuffer   Buffer containing FV data\r
+  FvSize          Size of the FV\r
+  FileType        Type of FFS file to search for\r
+  Pe32Section     PE32 section pointer when FFS file is found.\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS             Function Completed successfully.\r
+  EFI_ABORTED             Error encountered.\r
+  EFI_INVALID_PARAMETER   A required parameter was NULL.\r
+  EFI_NOT_FOUND           Core file not found.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                  Status;\r
+  EFI_FIRMWARE_VOLUME_HEADER  *OrigFvHeader;\r
+  UINT32                      OrigFvLength;\r
+  EFI_FFS_FILE_HEADER         *CoreFfsFile;\r
+  UINTN                       FvImageFileCount;\r
+  EFI_FFS_FILE_HEADER         *FvImageFile;\r
+  UINTN                       EncapFvSectionCount;\r
+  EFI_FILE_SECTION_POINTER    EncapFvSection;\r
+  EFI_FIRMWARE_VOLUME_HEADER  *EncapsulatedFvHeader;\r
+\r
+  if (Pe32Section == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  //\r
+  // Initialize FV library, saving previous values\r
+  //\r
+  OrigFvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)NULL;\r
+  GetFvHeader (&OrigFvHeader, &OrigFvLength);\r
+  InitializeFvLib(FvImageBuffer, (UINT32)FvSize);\r
+\r
+  //\r
+  // First see if we can obtain the file directly in outer FV\r
+  //\r
+  Status = GetFileByType(FileType, 1, &CoreFfsFile);\r
+  if (!EFI_ERROR(Status) && (CoreFfsFile != NULL) ) {\r
+\r
+    //\r
+    // Core found, now find PE32 or TE section\r
+    //\r
+    Status = GetSectionByType(CoreFfsFile, EFI_SECTION_PE32, 1, Pe32Section);\r
+    if (EFI_ERROR(Status)) {\r
+      Status = GetSectionByType(CoreFfsFile, EFI_SECTION_TE, 1, Pe32Section);\r
+    }\r
+\r
+    if (EFI_ERROR(Status)) {\r
+      Error(NULL, 0, 3000, "Invalid", "could not find a PE32 section in the core file.");\r
+      return EFI_ABORTED;\r
+    }\r
+\r
+    //\r
+    // Core PE/TE section, found, return\r
+    //\r
+    Status = EFI_SUCCESS;\r
+    goto EarlyExit;\r
+  }\r
+\r
+  //\r
+  // File was not found, look for FV Image file\r
+  //\r
+\r
+  // iterate through all FV image files in outer FV\r
+  for (FvImageFileCount = 1;; FvImageFileCount++) {\r
+\r
+    Status = GetFileByType(EFI_FV_FILETYPE_FIRMWARE_VOLUME_IMAGE, FvImageFileCount, &FvImageFile);\r
+\r
+    if (EFI_ERROR(Status) || (FvImageFile == NULL) ) {\r
+      // exit FV image file loop, no more found\r
+      break;\r
+    }\r
+\r
+    // Found an fv image file, look for an FV image section.  The PI spec does not\r
+    // preclude multiple FV image sections so we loop accordingly.\r
+    for (EncapFvSectionCount = 1;; EncapFvSectionCount++) {\r
+\r
+      // Look for the next FV image section.  The section search code will\r
+      // iterate into encapsulation sections.  For example, it will iterate\r
+      // into an EFI_SECTION_GUID_DEFINED encapsulation section to find the\r
+      // EFI_SECTION_FIRMWARE_VOLUME_IMAGE sections contained therein.\r
+      Status = GetSectionByType(FvImageFile, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, EncapFvSectionCount, &EncapFvSection);\r
+\r
+      if (EFI_ERROR(Status)) {\r
+        // exit section inner loop, no more found\r
+        break;\r
+      }\r
+\r
+      EncapsulatedFvHeader = (EFI_FIRMWARE_VOLUME_HEADER *)((UINT8 *)EncapFvSection.FVImageSection + GetSectionHeaderLength(EncapFvSection.FVImageSection));\r
+\r
+      // recurse to search the encapsulated FV for this core file type\r
+      Status = FindCorePeSection(EncapsulatedFvHeader, EncapsulatedFvHeader->FvLength, FileType, Pe32Section);\r
+\r
+      if (!EFI_ERROR(Status)) {\r
+        // we found the core in the capsulated image, success\r
+        goto EarlyExit;\r
+      }\r
+\r
+    } // end encapsulated fv image section loop\r
+  } // end fv image file loop\r
+\r
+  // core was not found\r
+  Status = EFI_NOT_FOUND;\r
+\r
+EarlyExit:\r
+\r
+  // restore FV lib values\r
+  if(OrigFvHeader != NULL) {\r
+    InitializeFvLib(OrigFvHeader, OrigFvLength);\r
+  }\r
+\r
+  return Status;\r
+}\r
+\r
+EFI_STATUS\r
+GetCoreMachineType(\r
+  IN  EFI_FILE_SECTION_POINTER     Pe32Section,\r
+  OUT UINT16                      *CoreMachineType\r
+  )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Returns the machine type of a P32 image, typically SEC or PEI core.\r
+\r
+Arguments:\r
+\r
+  Pe32Section       PE32 section data\r
+  CoreMachineType   The extracted machine type\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS             Function Completed successfully.\r
+  EFI_ABORTED             Error encountered.\r
+  EFI_INVALID_PARAMETER   A required parameter was NULL.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                  Status;\r
+  UINT32                      EntryPoint;\r
+  UINT32                      BaseOfCode;\r
+\r
+  if (CoreMachineType == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status = GetPe32Info(\r
+    (VOID *)((UINTN)Pe32Section.Pe32Section + GetSectionHeaderLength(Pe32Section.CommonHeader)),\r
+    &EntryPoint,\r
+    &BaseOfCode,\r
+    CoreMachineType\r
+    );\r
+  if (EFI_ERROR(Status)) {\r
+    Error(NULL, 0, 3000, "Invalid", "could not get the PE32 machine type for the core.");\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  return EFI_SUCCESS;\r
+}\r
+\r
+EFI_STATUS\r
+GetCoreEntryPointAddress(\r
+  IN VOID                         *FvImageBuffer,\r
+  IN FV_INFO                      *FvInfo,\r
+  IN  EFI_FILE_SECTION_POINTER     Pe32Section,\r
+  OUT EFI_PHYSICAL_ADDRESS        *CoreEntryAddress\r
+)\r
+/*++\r
+\r
+Routine Description:\r
+\r
+  Returns the physical address of the core (SEC or PEI) entry point.\r
+\r
+Arguments:\r
+\r
+  FvImageBuffer     Pointer to buffer containing FV data\r
+  FvInfo            Info for the parent FV\r
+  Pe32Section       PE32 section data\r
+  CoreEntryAddress  The extracted core entry physical address\r
+\r
+Returns:\r
+\r
+  EFI_SUCCESS             Function Completed successfully.\r
+  EFI_ABORTED             Error encountered.\r
+  EFI_INVALID_PARAMETER   A required parameter was NULL.\r
+\r
+--*/\r
+{\r
+  EFI_STATUS                  Status;\r
+  UINT32                      EntryPoint;\r
+  UINT32                      BaseOfCode;\r
+  UINT16                      MachineType;\r
+  EFI_PHYSICAL_ADDRESS        EntryPhysicalAddress;\r
+\r
+  if (CoreEntryAddress == NULL) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
+  Status = GetPe32Info(\r
+    (VOID *)((UINTN)Pe32Section.Pe32Section + GetSectionHeaderLength(Pe32Section.CommonHeader)),\r
+    &EntryPoint,\r
+    &BaseOfCode,\r
+    &MachineType\r
+    );\r
+  if (EFI_ERROR(Status)) {\r
+    Error(NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the core.");\r
+    return EFI_ABORTED;\r
+  }\r
+\r
+  //\r
+  // Physical address is FV base + offset of PE32 + offset of the entry point\r
+  //\r
+  EntryPhysicalAddress = FvInfo->BaseAddress;\r
+  EntryPhysicalAddress += (UINTN)Pe32Section.Pe32Section + GetSectionHeaderLength(Pe32Section.CommonHeader) - (UINTN)FvImageBuffer;\r
+  EntryPhysicalAddress += EntryPoint;\r
+\r
+  *CoreEntryAddress = EntryPhysicalAddress;\r
 \r
   return EFI_SUCCESS;\r
 }\r
 \r
-\r
 EFI_STATUS\r
 UpdateArmResetVectorIfNeeded (\r
   IN MEMORY_FILE            *FvImage,\r
@@ -1620,15 +2120,15 @@ UpdateArmResetVectorIfNeeded (
 /*++\r
 \r
 Routine Description:\r
-  This parses the FV looking for SEC and patches that address into the \r
+  This parses the FV looking for SEC and patches that address into the\r
   beginning of the FV header.\r
 \r
   For ARM32 the reset vector is at 0x00000000 or 0xFFFF0000.\r
   For AArch64 the reset vector is at 0x00000000.\r
 \r
-  This would commonly map to the first entry in the ROM. \r
+  This would commonly map to the first entry in the ROM.\r
   ARM32 Exceptions:\r
-  Reset            +0    \r
+  Reset            +0\r
   Undefined        +4\r
   SWI              +8\r
   Prefetch Abort   +12\r
@@ -1638,15 +2138,15 @@ Routine Description:
 \r
   We support two schemes on ARM.\r
   1) Beginning of the FV is the reset vector\r
-  2) Reset vector is data bytes FDF file and that code branches to reset vector \r
+  2) Reset vector is data bytes FDF file and that code branches to reset vector\r
     in the beginning of the FV (fixed size offset).\r
 \r
   Need to have the jump for the reset vector at location zero.\r
   We also need to store the address or PEI (if it exists).\r
-  We stub out a return from interrupt in case the debugger \r
+  We stub out a return from interrupt in case the debugger\r
    is using SWI (not done for AArch64, not enough space in struct).\r
-  The optional entry to the common exception handler is \r
-   to support full featured exception handling from ROM and is currently \r
+  The optional entry to the common exception handler is\r
+   to support full featured exception handling from ROM and is currently\r
     not support by this tool.\r
 \r
 Arguments:\r
@@ -1662,23 +2162,15 @@ Returns:
 \r
 --*/\r
 {\r
-  EFI_FFS_FILE_HEADER       *PeiCoreFile;\r
-  EFI_FFS_FILE_HEADER       *SecCoreFile;\r
-  EFI_STATUS                Status;\r
-  EFI_FILE_SECTION_POINTER  Pe32Section;\r
-  UINT32                    EntryPoint;\r
-  UINT32                    BaseOfCode;\r
-  UINT16                    MachineType;\r
-  EFI_PHYSICAL_ADDRESS      PeiCorePhysicalAddress;\r
-  EFI_PHYSICAL_ADDRESS      SecCorePhysicalAddress;\r
-  INT32                     ResetVector[4]; // ARM32:\r
-                                            // 0 - is branch relative to SEC entry point\r
-                                            // 1 - PEI Entry Point\r
-                                            // 2 - movs pc,lr for a SWI handler\r
-                                            // 3 - Place holder for Common Exception Handler\r
-                                            // AArch64: Used as UINT64 ResetVector[2]\r
-                                            // 0 - is branch relative to SEC entry point\r
-                                            // 1 - PEI Entry Point\r
+  EFI_STATUS                  Status;\r
+  EFI_FILE_SECTION_POINTER    SecPe32;\r
+  EFI_FILE_SECTION_POINTER    PeiPe32;\r
+  BOOLEAN                     UpdateVectorSec = FALSE;\r
+  BOOLEAN                     UpdateVectorPei = FALSE;\r
+  UINT16                      MachineType = 0;\r
+  EFI_PHYSICAL_ADDRESS        SecCoreEntryAddress = 0;\r
+  UINT16                      PeiMachineType = 0;\r
+  EFI_PHYSICAL_ADDRESS        PeiCoreEntryAddress = 0;\r
 \r
   //\r
   // Verify input parameters\r
@@ -1686,180 +2178,126 @@ Returns:
   if (FvImage == NULL || FvInfo == NULL) {\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  //\r
-  // Initialize FV library\r
-  //\r
-  InitializeFvLib (FvImage->FileImage, FvInfo->Size);\r
 \r
   //\r
-  // Find the Sec Core\r
+  // Locate an SEC Core instance and if found extract the machine type and entry point address\r
   //\r
-  Status = GetFileByType (EFI_FV_FILETYPE_SECURITY_CORE, 1, &SecCoreFile);\r
-  if (EFI_ERROR (Status) || SecCoreFile == NULL) {\r
-    //\r
-    // Maybe hardware does SEC job and we only have PEI Core?\r
-    //\r
-\r
-    //\r
-    // Find the PEI Core. It may not exist if SEC loads DXE core directly\r
-    //\r
-    PeiCorePhysicalAddress = 0;\r
-    Status = GetFileByType (EFI_FV_FILETYPE_PEI_CORE, 1, &PeiCoreFile);\r
-    if (!EFI_ERROR (Status) && PeiCoreFile != NULL) {\r
-      //\r
-      // PEI Core found, now find PE32 or TE section\r
-      //\r
-      Status = GetSectionByType (PeiCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);\r
-      if (Status == EFI_NOT_FOUND) {\r
-        Status = GetSectionByType (PeiCoreFile, EFI_SECTION_TE, 1, &Pe32Section);\r
-      }\r
-    \r
-      if (EFI_ERROR (Status)) {\r
-        Error (NULL, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");\r
-        return EFI_ABORTED;\r
-      }\r
-    \r
-      Status = GetPe32Info (\r
-                (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),\r
-                &EntryPoint,\r
-                &BaseOfCode,\r
-                &MachineType\r
-                );\r
-    \r
-      if (EFI_ERROR (Status)) {\r
-        Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");\r
-        return EFI_ABORTED;\r
-      }\r
-      //\r
-      // Physical address is FV base + offset of PE32 + offset of the entry point\r
-      //\r
-      PeiCorePhysicalAddress = FvInfo->BaseAddress;\r
-      PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;\r
-      PeiCorePhysicalAddress += EntryPoint;\r
-      DebugMsg (NULL, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress);\r
-\r
-      if (MachineType == EFI_IMAGE_MACHINE_ARMT || MachineType == EFI_IMAGE_MACHINE_AARCH64) {\r
-        memset (ResetVector, 0, sizeof (ResetVector));\r
-        // Address of PEI Core, if we have one\r
-        ResetVector[1] = (UINT32)PeiCorePhysicalAddress;\r
-      }\r
-      \r
-      //\r
-      // Copy to the beginning of the FV \r
-      //\r
-      memcpy ((UINT8 *) ((UINTN) FvImage->FileImage), ResetVector, sizeof (ResetVector));\r
+  Status = FindCorePeSection(FvImage->FileImage, FvInfo->Size, EFI_FV_FILETYPE_SECURITY_CORE, &SecPe32);\r
+  if (!EFI_ERROR(Status)) {\r
 \r
+    Status = GetCoreMachineType(SecPe32, &MachineType);\r
+    if (EFI_ERROR(Status)) {\r
+      Error(NULL, 0, 3000, "Invalid", "Could not get the PE32 machine type for SEC Core.");\r
+      return EFI_ABORTED;\r
     }\r
 \r
-    return EFI_SUCCESS;\r
-  }\r
-  \r
-  //\r
-  // Sec Core found, now find PE32 section\r
-  //\r
-  Status = GetSectionByType (SecCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);\r
-  if (Status == EFI_NOT_FOUND) {\r
-    Status = GetSectionByType (SecCoreFile, EFI_SECTION_TE, 1, &Pe32Section);\r
-  }\r
+    Status = GetCoreEntryPointAddress(FvImage->FileImage, FvInfo, SecPe32, &SecCoreEntryAddress);\r
+    if (EFI_ERROR(Status)) {\r
+      Error(NULL, 0, 3000, "Invalid", "Could not get the PE32 entry point address for SEC Core.");\r
+      return EFI_ABORTED;\r
+    }\r
 \r
-  if (EFI_ERROR (Status)) {\r
-    Error (NULL, 0, 3000, "Invalid", "could not find a PE32 section in the SEC core file.");\r
-    return EFI_ABORTED;\r
+    VerboseMsg("UpdateArmResetVectorIfNeeded found SEC core entry at 0x%llx", (unsigned long long)SecCoreEntryAddress);\r
+    UpdateVectorSec = TRUE;\r
   }\r
 \r
-  Status = GetPe32Info (\r
-            (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),\r
-            &EntryPoint,\r
-            &BaseOfCode,\r
-            &MachineType\r
-            );\r
-  if (EFI_ERROR (Status)) {\r
-    Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the SEC core.");\r
-    return EFI_ABORTED;\r
-  }\r
-  \r
-  if ((MachineType != EFI_IMAGE_MACHINE_ARMT) && (MachineType != EFI_IMAGE_MACHINE_AARCH64)) {\r
-    //\r
-    // If SEC is not ARM we have nothing to do\r
-    //\r
-    return EFI_SUCCESS;\r
-  }\r
-  \r
   //\r
-  // Physical address is FV base + offset of PE32 + offset of the entry point\r
+  // Locate a PEI Core instance and if found extract the machine type and entry point address\r
   //\r
-  SecCorePhysicalAddress = FvInfo->BaseAddress;\r
-  SecCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;\r
-  SecCorePhysicalAddress += EntryPoint;\r
-  DebugMsg (NULL, 0, 9, "SecCore physical entry point address", "Address = 0x%llX", (unsigned long long) SecCorePhysicalAddress); \r
+  Status = FindCorePeSection(FvImage->FileImage, FvInfo->Size, EFI_FV_FILETYPE_PEI_CORE, &PeiPe32);\r
+  if (!EFI_ERROR(Status)) {\r
 \r
-  //\r
-  // Find the PEI Core. It may not exist if SEC loads DXE core directly\r
-  //\r
-  PeiCorePhysicalAddress = 0;\r
-  Status = GetFileByType (EFI_FV_FILETYPE_PEI_CORE, 1, &PeiCoreFile);\r
-  if (!EFI_ERROR (Status) && PeiCoreFile != NULL) {\r
-    //\r
-    // PEI Core found, now find PE32 or TE section\r
-    //\r
-    Status = GetSectionByType (PeiCoreFile, EFI_SECTION_PE32, 1, &Pe32Section);\r
-    if (Status == EFI_NOT_FOUND) {\r
-      Status = GetSectionByType (PeiCoreFile, EFI_SECTION_TE, 1, &Pe32Section);\r
+    Status = GetCoreMachineType(PeiPe32, &PeiMachineType);\r
+    if (EFI_ERROR(Status)) {\r
+      Error(NULL, 0, 3000, "Invalid", "Could not get the PE32 machine type for PEI Core.");\r
+      return EFI_ABORTED;\r
     }\r
-  \r
-    if (EFI_ERROR (Status)) {\r
-      Error (NULL, 0, 3000, "Invalid", "could not find either a PE32 or a TE section in PEI core file!");\r
+\r
+    Status = GetCoreEntryPointAddress(FvImage->FileImage, FvInfo, PeiPe32, &PeiCoreEntryAddress);\r
+    if (EFI_ERROR(Status)) {\r
+      Error(NULL, 0, 3000, "Invalid", "Could not get the PE32 entry point address for PEI Core.");\r
       return EFI_ABORTED;\r
     }\r
-  \r
-    Status = GetPe32Info (\r
-              (VOID *) ((UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32)),\r
-              &EntryPoint,\r
-              &BaseOfCode,\r
-              &MachineType\r
-              );\r
-  \r
-    if (EFI_ERROR (Status)) {\r
-      Error (NULL, 0, 3000, "Invalid", "could not get the PE32 entry point for the PEI core!");\r
+\r
+    VerboseMsg("UpdateArmResetVectorIfNeeded found PEI core entry at 0x%llx", (unsigned long long)PeiCoreEntryAddress);\r
+\r
+    // if we previously found an SEC Core make sure machine types match\r
+    if (UpdateVectorSec && (MachineType != PeiMachineType)) {\r
+      Error(NULL, 0, 3000, "Invalid", "SEC and PEI machine types do not match, can't update reset vector");\r
       return EFI_ABORTED;\r
     }\r
-    //\r
-    // Physical address is FV base + offset of PE32 + offset of the entry point\r
-    //\r
-    PeiCorePhysicalAddress = FvInfo->BaseAddress;\r
-    PeiCorePhysicalAddress += (UINTN) Pe32Section.Pe32Section + sizeof (EFI_SECTION_PE32) - (UINTN) FvImage->FileImage;\r
-    PeiCorePhysicalAddress += EntryPoint;\r
-    DebugMsg (NULL, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress);\r
+    else {\r
+      MachineType = PeiMachineType;\r
+    }\r
+\r
+    UpdateVectorPei = TRUE;\r
   }\r
-  \r
+\r
+  if (!UpdateVectorSec && !UpdateVectorPei) {\r
+    return EFI_SUCCESS;\r
+  }\r
+\r
   if (MachineType == EFI_IMAGE_MACHINE_ARMT) {\r
-    // B SecEntryPoint - signed_immed_24 part +/-32MB offset\r
-    // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8\r
-    ResetVector[0] = (INT32)(SecCorePhysicalAddress - FvInfo->BaseAddress - 8) >> 2;\r
+    // ARM: Array of 4 UINT32s:\r
+    // 0 - is branch relative to SEC entry point\r
+    // 1 - PEI Entry Point\r
+    // 2 - movs pc,lr for a SWI handler\r
+    // 3 - Place holder for Common Exception Handler\r
+    UINT32                      ResetVector[4];\r
 \r
-    if (ResetVector[0] > 0x00FFFFFF) {\r
-      Error (NULL, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");\r
-      return EFI_ABORTED;\r
+    memset(ResetVector, 0, sizeof (ResetVector));\r
+\r
+    // if we found an SEC core entry point then generate a branch instruction\r
+    // to it and populate a debugger SWI entry as well\r
+    if (UpdateVectorSec) {\r
+\r
+      VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM SEC vector");\r
+\r
+      // B SecEntryPoint - signed_immed_24 part +/-32MB offset\r
+      // on ARM, the PC is always 8 ahead, so we're not really jumping from the base address, but from base address + 8\r
+      ResetVector[0] = (INT32)(SecCoreEntryAddress - FvInfo->BaseAddress - 8) >> 2;\r
+\r
+      if (ResetVector[0] > 0x00FFFFFF) {\r
+        Error(NULL, 0, 3000, "Invalid", "SEC Entry point must be within 32MB of the start of the FV");\r
+        return EFI_ABORTED;\r
+      }\r
+\r
+      // Add opcode for an uncondional branch with no link. i.e.: " B SecEntryPoint"\r
+      ResetVector[0] |= ARMT_UNCONDITIONAL_JUMP_INSTRUCTION;\r
+\r
+      // SWI handler movs   pc,lr. Just in case a debugger uses SWI\r
+      ResetVector[2] = 0xE1B0F07E;\r
+\r
+      // Place holder to support a common interrupt handler from ROM.\r
+      // Currently not suppprted. For this to be used the reset vector would not be in this FV\r
+      // and the exception vectors would be hard coded in the ROM and just through this address\r
+      // to find a common handler in the a module in the FV.\r
+      ResetVector[3] = 0;\r
+    }\r
+\r
+    // if a PEI core entry was found place its address in the vector area\r
+    if (UpdateVectorPei) {\r
+\r
+      VerboseMsg("UpdateArmResetVectorIfNeeded updating ARM PEI address");\r
+\r
+      // Address of PEI Core, if we have one\r
+      ResetVector[1] = (UINT32)PeiCoreEntryAddress;\r
     }\r
-  \r
-    // Add opcode for an uncondional branch with no link. AKA B SecEntryPoint\r
-    ResetVector[0] |= 0xEB000000;\r
-  \r
-  \r
-    // Address of PEI Core, if we have one\r
-    ResetVector[1] = (UINT32)PeiCorePhysicalAddress;\r
-  \r
-    // SWI handler movs   pc,lr. Just in case a debugger uses SWI\r
-    ResetVector[2] = 0xE1B0F07E;\r
-  \r
-    // Place holder to support a common interrupt handler from ROM.\r
-    // Currently not suppprted. For this to be used the reset vector would not be in this FV\r
-    // and the exception vectors would be hard coded in the ROM and just through this address\r
-    // to find a common handler in the a module in the FV.\r
-    ResetVector[3] = 0;\r
+\r
+    //\r
+    // Copy to the beginning of the FV\r
+    //\r
+    memcpy(FvImage->FileImage, ResetVector, sizeof (ResetVector));\r
+\r
   } else if (MachineType == EFI_IMAGE_MACHINE_AARCH64) {\r
+    // AArch64: Used as UINT64 ResetVector[2]\r
+    // 0 - is branch relative to SEC entry point\r
+    // 1 - PEI Entry Point\r
+    UINT64                      ResetVector[2];\r
 \r
-  /* NOTE:\r
+    memset(ResetVector, 0, sizeof (ResetVector));\r
+\r
+    /* NOTE:\r
     ARMT above has an entry in ResetVector[2] for SWI. The way we are using the ResetVector\r
     array at the moment, for AArch64, does not allow us space for this as the header only\r
     allows for a fixed amount of bytes at the start. If we are sure that UEFI will live\r
@@ -1867,33 +2305,43 @@ Returns:
     layout as above. But for the moment we replace the four 32bit vectors with two 64bit\r
     vectors in the same area of the Image heasder. This allows UEFI to start from a 64bit\r
     base.\r
-  */\r
+    */\r
 \r
-    ((UINT64*)ResetVector)[0] = (UINT64)(SecCorePhysicalAddress - FvInfo->BaseAddress) >> 2;\r
+    // if we found an SEC core entry point then generate a branch instruction to it\r
+    if (UpdateVectorSec) {\r
 \r
-    // B SecEntryPoint - signed_immed_26 part +/-128MB offset\r
-    if ( ((UINT64*)ResetVector)[0] > 0x03FFFFFF) {\r
-      Error (NULL, 0, 3000, "Invalid", "SEC Entry point must be within 128MB of the start of the FV");\r
-      return EFI_ABORTED;\r
+      VerboseMsg("UpdateArmResetVectorIfNeeded updating AArch64 SEC vector");\r
+\r
+      ResetVector[0] = (UINT64)(SecCoreEntryAddress - FvInfo->BaseAddress) >> 2;\r
+\r
+      // B SecEntryPoint - signed_immed_26 part +/-128MB offset\r
+      if (ResetVector[0] > 0x03FFFFFF) {\r
+        Error(NULL, 0, 3000, "Invalid", "SEC Entry point must be within 128MB of the start of the FV");\r
+        return EFI_ABORTED;\r
+      }\r
+      // Add opcode for an uncondional branch with no link. i.e.: " B SecEntryPoint"\r
+      ResetVector[0] |= ARM64_UNCONDITIONAL_JUMP_INSTRUCTION;\r
+    }\r
+\r
+    // if a PEI core entry was found place its address in the vector area\r
+    if (UpdateVectorPei) {\r
+\r
+      VerboseMsg("UpdateArmResetVectorIfNeeded updating AArch64 PEI address");\r
+\r
+      // Address of PEI Core, if we have one\r
+      ResetVector[1] = (UINT64)PeiCoreEntryAddress;\r
     }\r
-    // Add opcode for an uncondional branch with no link. AKA B SecEntryPoint\r
-    ((UINT64*)ResetVector)[0] |= 0x14000000;\r
 \r
-    // Address of PEI Core, if we have one\r
-    ((UINT64*)ResetVector)[1] = (UINT64)PeiCorePhysicalAddress;\r
+    //\r
+    // Copy to the beginning of the FV\r
+    //\r
+    memcpy(FvImage->FileImage, ResetVector, sizeof (ResetVector));\r
 \r
   } else {\r
-    Error (NULL, 0, 3000, "Invalid", "Unknown ARM machine type");\r
+    Error(NULL, 0, 3000, "Invalid", "Unknown machine type");\r
     return EFI_ABORTED;\r
   }\r
 \r
-  //\r
-  // Copy to the beginning of the FV \r
-  //\r
-  memcpy ((UINT8 *) ((UINTN) FvImage->FileImage), ResetVector, sizeof (ResetVector));\r
-\r
-  DebugMsg (NULL, 0, 9, "Update Reset vector in FV Header", NULL);\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -1908,8 +2356,8 @@ GetPe32Info (
 \r
 Routine Description:\r
 \r
-  Retrieves the PE32 entry point offset and machine type from PE image or TeImage.  \r
-  See EfiImage.h for machine types.  The entry point offset is from the beginning \r
+  Retrieves the PE32 entry point offset and machine type from PE image or TeImage.\r
+  See EfiImage.h for machine types.  The entry point offset is from the beginning\r
   of the PE32 buffer passed in.\r
 \r
 Arguments:\r
@@ -1951,13 +2399,13 @@ Returns:
     *BaseOfCode   = TeHeader->BaseOfCode + sizeof (EFI_TE_IMAGE_HEADER) - TeHeader->StrippedSize;\r
     *MachineType  = TeHeader->Machine;\r
   } else {\r
-  \r
+\r
     //\r
-    // Then check whether \r
+    // Then check whether\r
     // First is the DOS header\r
     //\r
     DosHeader = (EFI_IMAGE_DOS_HEADER *) Pe32;\r
-  \r
+\r
     //\r
     // Verify DOS header is expected\r
     //\r
@@ -1969,7 +2417,7 @@ Returns:
     // Immediately following is the NT header.\r
     //\r
     ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *) ((UINTN) Pe32 + DosHeader->e_lfanew);\r
-  \r
+\r
     //\r
     // Verify NT header is expected\r
     //\r
@@ -1988,7 +2436,7 @@ Returns:
   //\r
   // Verify machine type is supported\r
   //\r
-  if ((*MachineType != EFI_IMAGE_MACHINE_IA32) && (*MachineType != EFI_IMAGE_MACHINE_IA64) && (*MachineType != EFI_IMAGE_MACHINE_X64) && (*MachineType != EFI_IMAGE_MACHINE_EBC) && \r
+  if ((*MachineType != EFI_IMAGE_MACHINE_IA32) && (*MachineType != EFI_IMAGE_MACHINE_IA64) && (*MachineType != EFI_IMAGE_MACHINE_X64) && (*MachineType != EFI_IMAGE_MACHINE_EBC) &&\r
       (*MachineType != EFI_IMAGE_MACHINE_ARMT) && (*MachineType != EFI_IMAGE_MACHINE_AARCH64)) {\r
     Error (NULL, 0, 3000, "Invalid", "Unrecognized machine type in the PE32 file.");\r
     return EFI_UNSUPPORTED;\r
@@ -2036,17 +2484,19 @@ Returns:
   UINT8                           *FvImage;\r
   UINTN                           FvImageSize;\r
   FILE                            *FvFile;\r
-  CHAR8                           FvMapName [_MAX_PATH];\r
+  CHAR8                           *FvMapName;\r
   FILE                            *FvMapFile;\r
   EFI_FIRMWARE_VOLUME_EXT_HEADER  *FvExtHeader;\r
   FILE                            *FvExtHeaderFile;\r
   UINTN                           FileSize;\r
-  CHAR8                           FvReportName[_MAX_PATH];\r
+  CHAR8                           *FvReportName;\r
   FILE                            *FvReportFile;\r
 \r
   FvBufferHeader = NULL;\r
   FvFile         = NULL;\r
+  FvMapName      = NULL;\r
   FvMapFile      = NULL;\r
+  FvReportName   = NULL;\r
   FvReportFile   = NULL;\r
 \r
   if (InfFileImage != NULL) {\r
@@ -2056,7 +2506,7 @@ Returns:
     InfMemoryFile.FileImage           = InfFileImage;\r
     InfMemoryFile.CurrentFilePointer  = InfFileImage;\r
     InfMemoryFile.Eof                 = InfFileImage + InfFileSize;\r
-  \r
+\r
     //\r
     // Parse the FV inf file for header information\r
     //\r
@@ -2078,17 +2528,17 @@ Returns:
     Error (NULL, 0, 1001, "Missing option", "Output file name");\r
     return EFI_ABORTED;\r
   }\r
-  \r
+\r
   if (mFvDataInfo.FvBlocks[0].Length == 0) {\r
     Error (NULL, 0, 1001, "Missing required argument", "Block Size");\r
     return EFI_ABORTED;\r
   }\r
-  \r
+\r
   //\r
   // Debug message Fv File System Guid\r
   //\r
   if (mFvDataInfo.FvFileSystemGuidSet) {\r
-    DebugMsg (NULL, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", \r
+    DebugMsg (NULL, 0, 9, "FV File System Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",\r
                   (unsigned) mFvDataInfo.FvFileSystemGuid.Data1,\r
                   mFvDataInfo.FvFileSystemGuid.Data2,\r
                   mFvDataInfo.FvFileSystemGuid.Data3,\r
@@ -2111,7 +2561,11 @@ Returns:
     //\r
     // Open the FV Extension Header file\r
     //\r
-    FvExtHeaderFile = fopen (mFvDataInfo.FvExtHeaderFile, "rb");\r
+    FvExtHeaderFile = fopen (LongFilePath (mFvDataInfo.FvExtHeaderFile), "rb");\r
+    if (FvExtHeaderFile == NULL) {\r
+      Error (NULL, 0, 0001, "Error opening file", mFvDataInfo.FvExtHeaderFile);\r
+      return EFI_ABORTED;\r
+    }\r
 \r
     //\r
     // Get the file size\r
@@ -2157,7 +2611,7 @@ Returns:
   // Debug message Fv Name Guid\r
   //\r
   if (mFvDataInfo.FvNameGuidSet) {\r
-      DebugMsg (NULL, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", \r
+      DebugMsg (NULL, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X",\r
                   (unsigned) mFvDataInfo.FvNameGuid.Data1,\r
                   mFvDataInfo.FvNameGuid.Data2,\r
                   mFvDataInfo.FvNameGuid.Data3,\r
@@ -2171,7 +2625,8 @@ Returns:
                   mFvDataInfo.FvNameGuid.Data4[7]);\r
   }\r
 \r
-  if (CompareGuid (&mFvDataInfo.FvFileSystemGuid, &mEfiFirmwareFileSystem2Guid) == 0) {\r
+  if (CompareGuid (&mFvDataInfo.FvFileSystemGuid, &mEfiFirmwareFileSystem2Guid) == 0 ||\r
+    CompareGuid (&mFvDataInfo.FvFileSystemGuid, &mEfiFirmwareFileSystem3Guid) == 0) {\r
     mFvDataInfo.IsPiFvImage = TRUE;\r
   }\r
 \r
@@ -2179,8 +2634,34 @@ Returns:
   // FvMap file to log the function address of all modules in one Fvimage\r
   //\r
   if (MapFileName != NULL) {\r
+    if (strlen (MapFileName) > MAX_LONG_FILE_PATH - 1) {\r
+      Error (NULL, 0, 1003, "Invalid option value", "MapFileName %s is too long!", MapFileName);\r
+      Status = EFI_ABORTED;\r
+      goto Finish;\r
+    }\r
+\r
+    FvMapName = malloc (strlen (MapFileName) + 1);\r
+    if (FvMapName == NULL) {\r
+      Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Finish;\r
+    }\r
+\r
     strcpy (FvMapName, MapFileName);\r
   } else {\r
+    if (strlen (FvFileName) + strlen (".map") > MAX_LONG_FILE_PATH - 1) {\r
+      Error (NULL, 0, 1003, "Invalid option value", "FvFileName %s is too long!", FvFileName);\r
+      Status = EFI_ABORTED;\r
+      goto Finish;\r
+    }\r
+\r
+    FvMapName = malloc (strlen (FvFileName) + strlen (".map") + 1);\r
+    if (FvMapName == NULL) {\r
+      Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+      Status = EFI_OUT_OF_RESOURCES;\r
+      goto Finish;\r
+    }\r
+\r
     strcpy (FvMapName, FvFileName);\r
     strcat (FvMapName, ".map");\r
   }\r
@@ -2189,6 +2670,19 @@ Returns:
   //\r
   // FvReport file to log the FV information in one Fvimage\r
   //\r
+  if (strlen (FvFileName) + strlen (".txt") > MAX_LONG_FILE_PATH - 1) {\r
+    Error (NULL, 0, 1003, "Invalid option value", "FvFileName %s is too long!", FvFileName);\r
+    Status = EFI_ABORTED;\r
+    goto Finish;\r
+  }\r
+\r
+  FvReportName = malloc (strlen (FvFileName) + strlen (".txt") + 1);\r
+  if (FvReportName == NULL) {\r
+    Error (NULL, 0, 4001, "Resource", "memory cannot be allocated!");\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Finish;\r
+  }\r
+\r
   strcpy (FvReportName, FvFileName);\r
   strcat (FvReportName, ".txt");\r
 \r
@@ -2198,10 +2692,10 @@ Returns:
   //\r
   Status = CalculateFvSize (&mFvDataInfo);\r
   if (EFI_ERROR (Status)) {\r
-    return Status;    \r
+    goto Finish;\r
   }\r
   VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo.Size);\r
-  \r
+\r
   //\r
   // support fv image and empty fv image\r
   //\r
@@ -2212,7 +2706,8 @@ Returns:
   //\r
   FvBufferHeader = malloc (FvImageSize + sizeof (UINT64));\r
   if (FvBufferHeader == NULL) {\r
-    return EFI_OUT_OF_RESOURCES;\r
+    Status = EFI_OUT_OF_RESOURCES;\r
+    goto Finish;\r
   }\r
   FvImage = (UINT8 *) (((UINTN) FvBufferHeader + 7) & ~7);\r
 \r
@@ -2221,7 +2716,7 @@ Returns:
   //\r
   if (mFvDataInfo.FvAttributes == 0) {\r
     //\r
-    // Set Default Fv Attribute \r
+    // Set Default Fv Attribute\r
     //\r
     mFvDataInfo.FvAttributes = FV_DEFAULT_ATTRIBUTE;\r
   }\r
@@ -2252,7 +2747,7 @@ Returns:
   FvHeader->Revision        = EFI_FVH_REVISION;\r
   FvHeader->ExtHeaderOffset = 0;\r
   FvHeader->Reserved[0]     = 0;\r
-  \r
+\r
   //\r
   // Copy firmware block map\r
   //\r
@@ -2301,19 +2796,21 @@ Returns:
   //\r
   // Open FvMap file\r
   //\r
-  FvMapFile = fopen (FvMapName, "w");\r
+  FvMapFile = fopen (LongFilePath (FvMapName), "w");\r
   if (FvMapFile == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", FvMapName);\r
-    return EFI_ABORTED;\r
+    Status = EFI_ABORTED;\r
+    goto Finish;\r
   }\r
-  \r
+\r
   //\r
   // Open FvReport file\r
   //\r
-  FvReportFile = fopen(FvReportName, "w");\r
+  FvReportFile = fopen (LongFilePath (FvReportName), "w");\r
   if (FvReportFile == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", FvReportName);\r
-    return EFI_ABORTED;\r
+    Status = EFI_ABORTED;\r
+    goto Finish;\r
   }\r
   //\r
   // record FV size information into FvMap file.\r
@@ -2344,7 +2841,7 @@ Returns:
     //\r
     // Add FV Extended Header contents to the FV as a PAD file\r
     //\r
-    AddPadFile (&FvImageMemoryFile, 4, VtfFileImage, FvExtHeader);\r
+    AddPadFile (&FvImageMemoryFile, 4, VtfFileImage, FvExtHeader, 0);\r
 \r
     //\r
     // Fv Extension header change update Fv Header Check sum\r
@@ -2386,40 +2883,43 @@ Returns:
     if (!mArm) {\r
       //\r
       // Update reset vector (SALE_ENTRY for IPF)\r
-      // Now for IA32 and IA64 platform, the fv which has bsf file must have the \r
-      // EndAddress of 0xFFFFFFFF. Thus, only this type fv needs to update the   \r
-      // reset vector. If the PEI Core is found, the VTF file will probably get  \r
-      // corrupted by updating the entry point.                                  \r
+      // Now for IA32 and IA64 platform, the fv which has bsf file must have the\r
+      // EndAddress of 0xFFFFFFFF (unless the section was rebased).\r
+      // Thus, only this type fv needs to update the  reset vector.\r
+      // If the PEI Core is found, the VTF file will probably get\r
+      // corrupted by updating the entry point.\r
       //\r
-      if ((mFvDataInfo.BaseAddress + mFvDataInfo.Size) == FV_IMAGES_TOP_ADDRESS) {       \r
+      if (mFvDataInfo.ForceRebase == 1 ||\r
+          (mFvDataInfo.BaseAddress + mFvDataInfo.Size) == FV_IMAGES_TOP_ADDRESS) {\r
         Status = UpdateResetVector (&FvImageMemoryFile, &mFvDataInfo, VtfFileImage);\r
-        if (EFI_ERROR(Status)) {                                               \r
+        if (EFI_ERROR(Status)) {\r
           Error (NULL, 0, 3000, "Invalid", "Could not update the reset vector.");\r
-          goto Finish;                                              \r
+          goto Finish;\r
         }\r
         DebugMsg (NULL, 0, 9, "Update Reset vector in VTF file", NULL);\r
       }\r
     }\r
-  } \r
+  }\r
 \r
   if (mArm) {\r
     Status = UpdateArmResetVectorIfNeeded (&FvImageMemoryFile, &mFvDataInfo);\r
-    if (EFI_ERROR (Status)) {                                               \r
+    if (EFI_ERROR (Status)) {\r
       Error (NULL, 0, 3000, "Invalid", "Could not update the reset vector.");\r
-      goto Finish;                                              \r
-    }  \r
-    \r
+      goto Finish;\r
+    }\r
+\r
     //\r
     // Update Checksum for FvHeader\r
     //\r
     FvHeader->Checksum = 0;\r
     FvHeader->Checksum = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));\r
   }\r
-  \r
+\r
   //\r
   // Update FV Alignment attribute to the largest alignment of all the FFS files in the FV\r
   //\r
-  if ((((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16)) < MaxFfsAlignment) {\r
+  if (((FvHeader->Attributes & EFI_FVB2_WEAK_ALIGNMENT) != EFI_FVB2_WEAK_ALIGNMENT) &&\r
+      (((FvHeader->Attributes & EFI_FVB2_ALIGNMENT) >> 16)) < MaxFfsAlignment) {\r
     FvHeader->Attributes = ((MaxFfsAlignment << 16) | (FvHeader->Attributes & 0xFFFF));\r
     //\r
     // Update Checksum for FvHeader\r
@@ -2428,11 +2928,20 @@ Returns:
     FvHeader->Checksum      = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));\r
   }\r
 \r
-WriteFile: \r
+  //\r
+  // If there are large FFS in FV, the file system GUID should set to system 3 GUID.\r
+  //\r
+  if (mIsLargeFfs && CompareGuid (&FvHeader->FileSystemGuid, &mEfiFirmwareFileSystem2Guid) == 0) {\r
+    memcpy (&FvHeader->FileSystemGuid, &mEfiFirmwareFileSystem3Guid, sizeof (EFI_GUID));\r
+    FvHeader->Checksum      = 0;\r
+    FvHeader->Checksum      = CalculateChecksum16 ((UINT16 *) FvHeader, FvHeader->HeaderLength / sizeof (UINT16));\r
+  }\r
+\r
+WriteFile:\r
   //\r
   // Write fv file\r
   //\r
-  FvFile = fopen (FvFileName, "wb");\r
+  FvFile = fopen (LongFilePath (FvFileName), "wb");\r
   if (FvFile == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", FvFileName);\r
     Status = EFI_ABORTED;\r
@@ -2453,12 +2962,20 @@ Finish:
   if (FvExtHeader != NULL) {\r
     free (FvExtHeader);\r
   }\r
-  \r
+\r
+  if (FvMapName != NULL) {\r
+    free (FvMapName);\r
+  }\r
+\r
+  if (FvReportName != NULL) {\r
+    free (FvReportName);\r
+  }\r
+\r
   if (FvFile != NULL) {\r
     fflush (FvFile);\r
     fclose (FvFile);\r
   }\r
-  \r
+\r
   if (FvMapFile != NULL) {\r
     fflush (FvMapFile);\r
     fclose (FvMapFile);\r
@@ -2564,13 +3081,12 @@ Returns:
   UINTN               FfsFileSize;\r
   UINTN               FvExtendHeaderSize;\r
   UINT32              FfsAlignment;\r
+  UINT32              FfsHeaderSize;\r
   EFI_FFS_FILE_HEADER FfsHeader;\r
-  BOOLEAN             VtfFileFlag;\r
   UINTN               VtfFileSize;\r
-  \r
+\r
   FvExtendHeaderSize = 0;\r
   VtfFileSize = 0;\r
-  VtfFileFlag = FALSE;\r
   fpin  = NULL;\r
   Index = 0;\r
 \r
@@ -2581,31 +3097,36 @@ Returns:
   for (Index = 0; FvInfoPtr->FvBlocks[Index].NumBlocks > 0 && FvInfoPtr->FvBlocks[Index].Length > 0; Index++) {\r
     FvInfoPtr->Size += FvInfoPtr->FvBlocks[Index].NumBlocks * FvInfoPtr->FvBlocks[Index].Length;\r
   }\r
-  \r
+\r
   //\r
-  // Caculate the required sizes for all FFS files.\r
+  // Calculate the required sizes for all FFS files.\r
   //\r
   CurrentOffset = sizeof (EFI_FIRMWARE_VOLUME_HEADER);\r
-  \r
+\r
   for (Index = 1;; Index ++) {\r
     CurrentOffset += sizeof (EFI_FV_BLOCK_MAP_ENTRY);\r
     if (FvInfoPtr->FvBlocks[Index].NumBlocks == 0 || FvInfoPtr->FvBlocks[Index].Length == 0) {\r
       break;\r
     }\r
   }\r
-  \r
+\r
   //\r
   // Calculate PI extension header\r
   //\r
   if (mFvDataInfo.FvExtHeaderFile[0] != '\0') {\r
-    fpin = fopen (mFvDataInfo.FvExtHeaderFile, "rb");\r
+    fpin = fopen (LongFilePath (mFvDataInfo.FvExtHeaderFile), "rb");\r
     if (fpin == NULL) {\r
       Error (NULL, 0, 0001, "Error opening file", mFvDataInfo.FvExtHeaderFile);\r
       return EFI_ABORTED;\r
     }\r
     FvExtendHeaderSize = _filelength (fileno (fpin));\r
     fclose (fpin);\r
-    CurrentOffset += sizeof (EFI_FFS_FILE_HEADER) + FvExtendHeaderSize;\r
+    if (sizeof (EFI_FFS_FILE_HEADER) + FvExtendHeaderSize >= MAX_FFS_SIZE) {\r
+      CurrentOffset += sizeof (EFI_FFS_FILE_HEADER2) + FvExtendHeaderSize;\r
+      mIsLargeFfs = TRUE;\r
+    } else {\r
+      CurrentOffset += sizeof (EFI_FFS_FILE_HEADER) + FvExtendHeaderSize;\r
+    }\r
     CurrentOffset = (CurrentOffset + 7) & (~7);\r
   } else if (mFvDataInfo.FvNameGuidSet) {\r
     CurrentOffset += sizeof (EFI_FFS_FILE_HEADER) + sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER);\r
@@ -2620,7 +3141,7 @@ Returns:
     // Open FFS file\r
     //\r
     fpin = NULL;\r
-    fpin = fopen (FvInfoPtr->FvFiles[Index], "rb");\r
+    fpin = fopen (LongFilePath (FvInfoPtr->FvFiles[Index]), "rb");\r
     if (fpin == NULL) {\r
       Error (NULL, 0, 0001, "Error opening file", FvInfoPtr->FvFiles[Index]);\r
       return EFI_ABORTED;\r
@@ -2629,6 +3150,12 @@ Returns:
     // Get the file size\r
     //\r
     FfsFileSize = _filelength (fileno (fpin));\r
+    if (FfsFileSize >= MAX_FFS_SIZE) {\r
+      FfsHeaderSize = sizeof(EFI_FFS_FILE_HEADER2);\r
+      mIsLargeFfs = TRUE;\r
+    } else {\r
+      FfsHeaderSize = sizeof(EFI_FFS_FILE_HEADER);\r
+    }\r
     //\r
     // Read Ffs File header\r
     //\r
@@ -2637,57 +3164,61 @@ Returns:
     // close file\r
     //\r
     fclose (fpin);\r
-    \r
+\r
     if (FvInfoPtr->IsPiFvImage) {\r
-           //\r
-           // Check whether this ffs file is vtf file\r
-           //\r
-           if (IsVtfFile (&FfsHeader)) {\r
-             if (VtfFileFlag) {\r
-               //\r
-               // One Fv image can't have two vtf files.\r
-               //\r
-               return EFI_ABORTED;\r
-             }\r
-             VtfFileFlag = TRUE;\r
+        //\r
+        // Check whether this ffs file is vtf file\r
+        //\r
+        if (IsVtfFile (&FfsHeader)) {\r
+          if (VtfFileFlag) {\r
+            //\r
+            // One Fv image can't have two vtf files.\r
+            //\r
+            Error (NULL, 0, 3000,"Invalid", "One Fv image can't have two vtf files.");\r
+            return EFI_ABORTED;\r
+          }\r
+          VtfFileFlag = TRUE;\r
         VtfFileSize = FfsFileSize;\r
         continue;\r
       }\r
 \r
       //\r
-      // Get the alignment of FFS file \r
+      // Get the alignment of FFS file\r
       //\r
       ReadFfsAlignment (&FfsHeader, &FfsAlignment);\r
       FfsAlignment = 1 << FfsAlignment;\r
       //\r
       // Add Pad file\r
       //\r
-      if (((CurrentOffset + sizeof (EFI_FFS_FILE_HEADER)) % FfsAlignment) != 0) {\r
-        CurrentOffset = (CurrentOffset + sizeof (EFI_FFS_FILE_HEADER) * 2 + FfsAlignment - 1) & ~(FfsAlignment - 1);\r
-        CurrentOffset -= sizeof (EFI_FFS_FILE_HEADER);\r
+      if (((CurrentOffset + FfsHeaderSize) % FfsAlignment) != 0) {\r
+        //\r
+        // Only EFI_FFS_FILE_HEADER is needed for a pad section.\r
+        //\r
+        CurrentOffset = (CurrentOffset + FfsHeaderSize + sizeof(EFI_FFS_FILE_HEADER) + FfsAlignment - 1) & ~(FfsAlignment - 1);\r
+        CurrentOffset -= FfsHeaderSize;\r
       }\r
-         }\r
+    }\r
 \r
     //\r
     // Add ffs file size\r
     //\r
     if (FvInfoPtr->SizeofFvFiles[Index] > FfsFileSize) {\r
-       CurrentOffset += FvInfoPtr->SizeofFvFiles[Index];\r
+      CurrentOffset += FvInfoPtr->SizeofFvFiles[Index];\r
     } else {\r
-       CurrentOffset += FfsFileSize;\r
+      CurrentOffset += FfsFileSize;\r
     }\r
-       \r
+\r
     //\r
     // Make next ffs file start at QWord Boundry\r
     //\r
     if (FvInfoPtr->IsPiFvImage) {\r
-       CurrentOffset = (CurrentOffset + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1);\r
+      CurrentOffset = (CurrentOffset + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1);\r
     }\r
   }\r
   CurrentOffset += VtfFileSize;\r
-  DebugMsg (NULL, 0, 9, "FvImage size", "The caculated fv image size is 0x%x and the current set fv image size is 0x%x", (unsigned) CurrentOffset, (unsigned) FvInfoPtr->Size);\r
-  \r
-  if (FvInfoPtr->Size == 0) { \r
+  DebugMsg (NULL, 0, 9, "FvImage size", "The calculated fv image size is 0x%x and the current set fv image size is 0x%x", (unsigned) CurrentOffset, (unsigned) FvInfoPtr->Size);\r
+\r
+  if (FvInfoPtr->Size == 0) {\r
     //\r
     // Update FvInfo data\r
     //\r
@@ -2702,7 +3233,7 @@ Returns:
     Error (NULL, 0, 3000, "Invalid", "the required fv image size 0x%x exceeds the set fv image size 0x%x", (unsigned) CurrentOffset, (unsigned) FvInfoPtr->Size);\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
   // Set Fv Size Information\r
   //\r
@@ -2757,7 +3288,7 @@ Returns:
 \r
 EFI_STATUS\r
 GetChildFvFromFfs (\r
-  IN      FV_INFO               *FvInfo, \r
+  IN      FV_INFO               *FvInfo,\r
   IN      EFI_FFS_FILE_HEADER   *FfsFile,\r
   IN      UINTN                 XipOffset\r
   )\r
@@ -2783,16 +3314,42 @@ Returns:
   EFI_FILE_SECTION_POINTER            SubFvSection;\r
   EFI_FIRMWARE_VOLUME_HEADER          *SubFvImageHeader;\r
   EFI_PHYSICAL_ADDRESS                SubFvBaseAddress;\r
+  EFI_FILE_SECTION_POINTER            CorePe32;\r
+  UINT16                              MachineType;\r
 \r
   for (Index = 1;; Index++) {\r
     //\r
-    // Find FV section \r
+    // Find FV section\r
     //\r
     Status = GetSectionByType (FfsFile, EFI_SECTION_FIRMWARE_VOLUME_IMAGE, Index, &SubFvSection);\r
     if (EFI_ERROR (Status)) {\r
       break;\r
     }\r
-    SubFvImageHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINT8 *) SubFvSection.FVImageSection + sizeof (EFI_FIRMWARE_VOLUME_IMAGE_SECTION));\r
+    SubFvImageHeader = (EFI_FIRMWARE_VOLUME_HEADER *) ((UINT8 *) SubFvSection.FVImageSection + GetSectionHeaderLength(SubFvSection.FVImageSection));\r
+\r
+    //\r
+    // See if there's an SEC core in the child FV\r
+    Status = FindCorePeSection(SubFvImageHeader, SubFvImageHeader->FvLength, EFI_FV_FILETYPE_SECURITY_CORE, &CorePe32);\r
+\r
+    // if we couldn't find the SEC core, look for a PEI core\r
+    if (EFI_ERROR(Status)) {\r
+      Status = FindCorePeSection(SubFvImageHeader, SubFvImageHeader->FvLength, EFI_FV_FILETYPE_PEI_CORE, &CorePe32);\r
+    }\r
+\r
+    if (!EFI_ERROR(Status)) {\r
+      Status = GetCoreMachineType(CorePe32, &MachineType);\r
+      if (EFI_ERROR(Status)) {\r
+        Error(NULL, 0, 3000, "Invalid", "Could not get the PE32 machine type for SEC/PEI Core.");\r
+        return EFI_ABORTED;\r
+      }\r
+\r
+      // machine type is ARM, set a flag so ARM reset vector procesing occurs\r
+      if ((MachineType == EFI_IMAGE_MACHINE_ARMT) || (MachineType == EFI_IMAGE_MACHINE_AARCH64)) {\r
+        VerboseMsg("Located ARM/AArch64 SEC/PEI core in child FV");\r
+        mArm = TRUE;\r
+      }\r
+    }\r
+\r
     //\r
     // Rebase on Flash\r
     //\r
@@ -2804,9 +3361,9 @@ Returns:
 }\r
 \r
 EFI_STATUS\r
-FfsRebase ( \r
-  IN OUT  FV_INFO               *FvInfo, \r
-  IN      CHAR8                 *FileName,           \r
+FfsRebase (\r
+  IN OUT  FV_INFO               *FvInfo,\r
+  IN      CHAR8                 *FileName,\r
   IN OUT  EFI_FFS_FILE_HEADER   *FfsFile,\r
   IN      UINTN                 XipOffset,\r
   IN      FILE                  *FvMapFile\r
@@ -2819,7 +3376,7 @@ Routine Description:
   rebase any PE32 sections found in the file using the base address.\r
 \r
 Arguments:\r
-  \r
+\r
   FvInfo            A pointer to FV_INFO struture.\r
   FileName          Ffs File PathName\r
   FfsFile           A pointer to Ffs file image.\r
@@ -2838,7 +3395,7 @@ Returns:
 {\r
   EFI_STATUS                            Status;\r
   PE_COFF_LOADER_IMAGE_CONTEXT          ImageContext;\r
-  PE_COFF_LOADER_IMAGE_CONTEXT          OrigImageContext;  \r
+  PE_COFF_LOADER_IMAGE_CONTEXT          OrigImageContext;\r
   EFI_PHYSICAL_ADDRESS                  XipBase;\r
   EFI_PHYSICAL_ADDRESS                  NewPe32BaseAddress;\r
   UINTN                                 Index;\r
@@ -2848,14 +3405,16 @@ Returns:
   EFI_TE_IMAGE_HEADER                   *TEImageHeader;\r
   UINT8                                 *MemoryImagePointer;\r
   EFI_IMAGE_SECTION_HEADER              *SectionHeader;\r
-  CHAR8                                 PeFileName [_MAX_PATH];\r
+  CHAR8                                 PeFileName [MAX_LONG_FILE_PATH];\r
   CHAR8                                 *Cptr;\r
   FILE                                  *PeFile;\r
   UINT8                                 *PeFileBuffer;\r
   UINT32                                PeFileSize;\r
   CHAR8                                 *PdbPointer;\r
+  UINT32                                FfsHeaderSize;\r
+  UINT32                                CurSecHdrSize;\r
 \r
-  Index              = 0;  \r
+  Index              = 0;\r
   MemoryImagePointer = NULL;\r
   TEImageHeader      = NULL;\r
   ImgHdr             = NULL;\r
@@ -2870,7 +3429,7 @@ Returns:
   if ((FvInfo->BaseAddress == 0) && (FvInfo->ForceRebase == -1)) {\r
     return EFI_SUCCESS;\r
   }\r
-  \r
+\r
   //\r
   // If ForceRebase Flag specified to FALSE, will always not take rebase action.\r
   //\r
@@ -2905,6 +3464,8 @@ Returns:
     default:\r
       return EFI_SUCCESS;\r
   }\r
+\r
+  FfsHeaderSize = GetFfsHeaderLength(FfsFile);\r
   //\r
   // Rebase each PE32 section\r
   //\r
@@ -2914,7 +3475,7 @@ Returns:
     // Init Value\r
     //\r
     NewPe32BaseAddress = 0;\r
-    \r
+\r
     //\r
     // Find Pe Image\r
     //\r
@@ -2922,12 +3483,13 @@ Returns:
     if (EFI_ERROR (Status)) {\r
       break;\r
     }\r
+    CurSecHdrSize = GetSectionHeaderLength(CurrentPe32Section.CommonHeader);\r
 \r
     //\r
     // Initialize context\r
     //\r
     memset (&ImageContext, 0, sizeof (ImageContext));\r
-    ImageContext.Handle     = (VOID *) ((UINTN) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION));\r
+    ImageContext.Handle     = (VOID *) ((UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize);\r
     ImageContext.ImageRead  = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;\r
     Status                  = PeCoffLoaderGetImageInfo (&ImageContext);\r
     if (EFI_ERROR (Status)) {\r
@@ -2944,7 +3506,7 @@ Returns:
     // Keep Image Context for PE image in FV\r
     //\r
     memcpy (&OrigImageContext, &ImageContext, sizeof (ImageContext));\r
-    \r
+\r
     //\r
     // Get File PdbPointer\r
     //\r
@@ -2953,7 +3515,7 @@ Returns:
     //\r
     // Get PeHeader pointer\r
     //\r
-    ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((UINTN) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION) + ImageContext.PeCoffHeaderOffset);\r
+    ImgHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize + ImageContext.PeCoffHeaderOffset);\r
 \r
     //\r
     // Calculate the PE32 base address, based on file type\r
@@ -2970,17 +3532,22 @@ Returns:
           //\r
           // Xip module has the same section alignment and file alignment.\r
           //\r
-          Error (NULL, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName);\r
+          Error (NULL, 0, 3000, "Invalid", "PE image Section-Alignment and File-Alignment do not match : %s.", FileName);\r
           return EFI_ABORTED;\r
         }\r
         //\r
-        // PeImage has no reloc section. It will try to get reloc data from the original EFI image. \r
+        // PeImage has no reloc section. It will try to get reloc data from the original EFI image.\r
         //\r
         if (ImageContext.RelocationsStripped) {\r
           //\r
-          // Construct the original efi file Name \r
+          // Construct the original efi file Name\r
           //\r
-          strcpy (PeFileName, FileName);\r
+          if (strlen (FileName) >= MAX_LONG_FILE_PATH) {\r
+            Error (NULL, 0, 2000, "Invalid", "The file name %s is too long.", FileName);\r
+            return EFI_ABORTED;\r
+          }\r
+          strncpy (PeFileName, FileName, MAX_LONG_FILE_PATH - 1);\r
+          PeFileName[MAX_LONG_FILE_PATH - 1] = 0;\r
           Cptr = PeFileName + strlen (PeFileName);\r
           while (*Cptr != '.') {\r
             Cptr --;\r
@@ -2994,7 +3561,7 @@ Returns:
             *(Cptr + 3) = 'i';\r
             *(Cptr + 4) = '\0';\r
           }\r
-          PeFile = fopen (PeFileName, "rb");\r
+          PeFile = fopen (LongFilePath (PeFileName), "rb");\r
           if (PeFile == NULL) {\r
             Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);\r
             //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);\r
@@ -3007,6 +3574,7 @@ Returns:
           PeFileSize = _filelength (fileno (PeFile));\r
           PeFileBuffer = (UINT8 *) malloc (PeFileSize);\r
           if (PeFileBuffer == NULL) {\r
+            fclose (PeFile);\r
             Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);\r
             return EFI_OUT_OF_RESOURCES;\r
           }\r
@@ -3030,7 +3598,7 @@ Returns:
           ImageContext.RelocationsStripped = FALSE;\r
         }\r
 \r
-        NewPe32BaseAddress = XipBase + (UINTN) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION) - (UINTN)FfsFile;\r
+        NewPe32BaseAddress = XipBase + (UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize - (UINTN)FfsFile;\r
         break;\r
 \r
       case EFI_FV_FILETYPE_DRIVER:\r
@@ -3042,10 +3610,10 @@ Returns:
           //\r
           // Xip module has the same section alignment and file alignment.\r
           //\r
-          Error (NULL, 0, 3000, "Invalid", "Section-Alignment and File-Alignment do not match : %s.", FileName);\r
+          Error (NULL, 0, 3000, "Invalid", "PE image Section-Alignment and File-Alignment do not match : %s.", FileName);\r
           return EFI_ABORTED;\r
         }\r
-        NewPe32BaseAddress = XipBase + (UINTN) CurrentPe32Section.Pe32Section + sizeof (EFI_PE32_SECTION) - (UINTN)FfsFile;\r
+        NewPe32BaseAddress = XipBase + (UINTN) CurrentPe32Section.Pe32Section + CurSecHdrSize - (UINTN)FfsFile;\r
         break;\r
 \r
       default:\r
@@ -3054,7 +3622,7 @@ Returns:
         //\r
         return EFI_SUCCESS;\r
     }\r
-    \r
+\r
     //\r
     // Relocation doesn't exist\r
     //\r
@@ -3076,14 +3644,14 @@ Returns:
     }\r
     memset ((VOID *) MemoryImagePointer, 0, (UINTN) ImageContext.ImageSize + ImageContext.SectionAlignment);\r
     ImageContext.ImageAddress = ((UINTN) MemoryImagePointer + ImageContext.SectionAlignment - 1) & (~((UINTN) ImageContext.SectionAlignment - 1));\r
-    \r
+\r
     Status =  PeCoffLoaderLoadImage (&ImageContext);\r
     if (EFI_ERROR (Status)) {\r
       Error (NULL, 0, 3000, "Invalid", "LocateImage() call failed on rebase of %s", FileName);\r
       free ((VOID *) MemoryImagePointer);\r
       return Status;\r
     }\r
-         \r
+\r
     ImageContext.DestinationAddress = NewPe32BaseAddress;\r
     Status                          = PeCoffLoaderRelocateImage (&ImageContext);\r
     if (EFI_ERROR (Status)) {\r
@@ -3097,15 +3665,15 @@ Returns:
     //\r
     SectionHeader = (EFI_IMAGE_SECTION_HEADER *) (\r
                        (UINTN) ImgHdr +\r
-                       sizeof (UINT32) + \r
-                       sizeof (EFI_IMAGE_FILE_HEADER) +  \r
+                       sizeof (UINT32) +\r
+                       sizeof (EFI_IMAGE_FILE_HEADER) +\r
                        ImgHdr->Pe32.FileHeader.SizeOfOptionalHeader\r
                        );\r
-    \r
+\r
     for (Index = 0; Index < ImgHdr->Pe32.FileHeader.NumberOfSections; Index ++, SectionHeader ++) {\r
       CopyMem (\r
-        (UINT8 *) CurrentPe32Section.Pe32Section + sizeof (EFI_COMMON_SECTION_HEADER) + SectionHeader->PointerToRawData, \r
-        (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress), \r
+        (UINT8 *) CurrentPe32Section.Pe32Section + CurSecHdrSize + SectionHeader->PointerToRawData,\r
+        (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress),\r
         SectionHeader->SizeOfRawData\r
         );\r
     }\r
@@ -3116,7 +3684,7 @@ Returns:
       free (PeFileBuffer);\r
       PeFileBuffer = NULL;\r
     }\r
-    \r
+\r
     //\r
     // Update Image Base Address\r
     //\r
@@ -3140,8 +3708,8 @@ Returns:
       FfsFile->IntegrityCheck.Checksum.File = 0;\r
       FfsFile->State                        = 0;\r
       FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (\r
-                                                (UINT8 *) (FfsFile + 1),\r
-                                                GetLength (FfsFile->Size) - sizeof (EFI_FFS_FILE_HEADER)\r
+                                                (UINT8 *) ((UINT8 *)FfsFile + FfsHeaderSize),\r
+                                                GetFfsFileLength (FfsFile) - FfsHeaderSize\r
                                                 );\r
       FfsFile->State = SavedState;\r
     }\r
@@ -3171,13 +3739,13 @@ Returns:
     //\r
     return EFI_SUCCESS;\r
   }\r
-  \r
+\r
   //\r
   // Now process TE sections\r
   //\r
   for (Index = 1;; Index++) {\r
     NewPe32BaseAddress = 0;\r
-    \r
+\r
     //\r
     // Find Te Image\r
     //\r
@@ -3185,12 +3753,14 @@ Returns:
     if (EFI_ERROR (Status)) {\r
       break;\r
     }\r
-    \r
+\r
+    CurSecHdrSize = GetSectionHeaderLength(CurrentPe32Section.CommonHeader);\r
+\r
     //\r
     // Calculate the TE base address, the FFS file base plus the offset of the TE section less the size stripped off\r
     // by GenTEImage\r
     //\r
-    TEImageHeader = (EFI_TE_IMAGE_HEADER *) ((UINT8 *) CurrentPe32Section.Pe32Section + sizeof (EFI_COMMON_SECTION_HEADER));\r
+    TEImageHeader = (EFI_TE_IMAGE_HEADER *) ((UINT8 *) CurrentPe32Section.Pe32Section + CurSecHdrSize);\r
 \r
     //\r
     // Initialize context, load image info.\r
@@ -3230,9 +3800,14 @@ Returns:
     //\r
     if (ImageContext.RelocationsStripped) {\r
       //\r
-      // Construct the original efi file name \r
+      // Construct the original efi file name\r
       //\r
-      strcpy (PeFileName, FileName);\r
+      if (strlen (FileName) >= MAX_LONG_FILE_PATH) {\r
+        Error (NULL, 0, 2000, "Invalid", "The file name %s is too long.", FileName);\r
+        return EFI_ABORTED;\r
+      }\r
+      strncpy (PeFileName, FileName, MAX_LONG_FILE_PATH - 1);\r
+      PeFileName[MAX_LONG_FILE_PATH - 1] = 0;\r
       Cptr = PeFileName + strlen (PeFileName);\r
       while (*Cptr != '.') {\r
         Cptr --;\r
@@ -3248,7 +3823,7 @@ Returns:
         *(Cptr + 4) = '\0';\r
       }\r
 \r
-      PeFile = fopen (PeFileName, "rb");\r
+      PeFile = fopen (LongFilePath (PeFileName), "rb");\r
       if (PeFile == NULL) {\r
         Warning (NULL, 0, 0, "Invalid", "The file %s has no .reloc section.", FileName);\r
         //Error (NULL, 0, 3000, "Invalid", "The file %s has no .reloc section.", FileName);\r
@@ -3260,6 +3835,7 @@ Returns:
         PeFileSize = _filelength (fileno (PeFile));\r
         PeFileBuffer = (UINT8 *) malloc (PeFileSize);\r
         if (PeFileBuffer == NULL) {\r
+          fclose (PeFile);\r
           Error (NULL, 0, 4001, "Resource", "memory cannot be allocated on rebase of %s", FileName);\r
           return EFI_OUT_OF_RESOURCES;\r
         }\r
@@ -3313,7 +3889,7 @@ Returns:
     }\r
     //\r
     // Reloacate TeImage\r
-    // \r
+    //\r
     ImageContext.DestinationAddress = NewPe32BaseAddress;\r
     Status                          = PeCoffLoaderRelocateImage (&ImageContext);\r
     if (EFI_ERROR (Status)) {\r
@@ -3321,7 +3897,7 @@ Returns:
       free ((VOID *) MemoryImagePointer);\r
       return Status;\r
     }\r
-    \r
+\r
     //\r
     // Copy the relocated image into raw image file.\r
     //\r
@@ -3329,19 +3905,19 @@ Returns:
     for (Index = 0; Index < TEImageHeader->NumberOfSections; Index ++, SectionHeader ++) {\r
       if (!ImageContext.IsTeImage) {\r
         CopyMem (\r
-          (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->PointerToRawData, \r
-          (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress), \r
+          (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->PointerToRawData,\r
+          (VOID*) (UINTN) (ImageContext.ImageAddress + SectionHeader->VirtualAddress),\r
           SectionHeader->SizeOfRawData\r
           );\r
       } else {\r
         CopyMem (\r
-          (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->PointerToRawData, \r
-          (VOID*) (UINTN) (ImageContext.ImageAddress + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->VirtualAddress), \r
+          (UINT8 *) TEImageHeader + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->PointerToRawData,\r
+          (VOID*) (UINTN) (ImageContext.ImageAddress + sizeof (EFI_TE_IMAGE_HEADER) - TEImageHeader->StrippedSize + SectionHeader->VirtualAddress),\r
           SectionHeader->SizeOfRawData\r
           );\r
       }\r
     }\r
-    \r
+\r
     //\r
     // Free the allocated memory resource\r
     //\r
@@ -3351,7 +3927,7 @@ Returns:
       free (PeFileBuffer);\r
       PeFileBuffer = NULL;\r
     }\r
-    \r
+\r
     //\r
     // Update Image Base Address\r
     //\r
@@ -3365,8 +3941,8 @@ Returns:
       FfsFile->IntegrityCheck.Checksum.File = 0;\r
       FfsFile->State                        = 0;\r
       FfsFile->IntegrityCheck.Checksum.File = CalculateChecksum8 (\r
-                                                (UINT8 *)(FfsFile + 1),\r
-                                                GetLength (FfsFile->Size) - sizeof (EFI_FFS_FILE_HEADER)\r
+                                                (UINT8 *)((UINT8 *)FfsFile + FfsHeaderSize),\r
+                                                GetFfsFileLength (FfsFile) - FfsHeaderSize\r
                                                 );\r
       FfsFile->State = SavedState;\r
     }\r
@@ -3382,14 +3958,14 @@ Returns:
     }\r
 \r
     WriteMapFile (\r
-      FvMapFile, \r
-      PdbPointer, \r
+      FvMapFile,\r
+      PdbPointer,\r
       FfsFile,\r
-      NewPe32BaseAddress, \r
+      NewPe32BaseAddress,\r
       &OrigImageContext\r
       );\r
   }\r
\r
+\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -3436,12 +4012,12 @@ Returns:
     //\r
     // Get Pad file size.\r
     //\r
-    FileLength = (*(UINT32 *)(PadFile->Size)) & 0x00FFFFFF;\r
-    FileLength = (FileLength + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1); \r
+    FileLength = GetFfsFileLength(PadFile);\r
+    FileLength = (FileLength + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1);\r
     //\r
     // FixPoint must be align on 0x1000 relative to FvImage Header\r
     //\r
-    FixPoint = (UINT8*) PadFile + sizeof (EFI_FFS_FILE_HEADER);\r
+    FixPoint = (UINT8*) PadFile + GetFfsHeaderLength(PadFile);\r
     FixPoint = FixPoint + 0x1000 - (((UINTN) FixPoint - (UINTN) FvImage->FileImage) & 0xFFF);\r
     //\r
     // FixPoint be larger at the last place of one fv image.\r
@@ -3450,15 +4026,15 @@ Returns:
       FixPoint += 0x1000;\r
     }\r
     FixPoint -= 0x1000;\r
-    \r
-    if ((UINTN) FixPoint < ((UINTN) PadFile + sizeof (EFI_FFS_FILE_HEADER))) {\r
+\r
+    if ((UINTN) FixPoint < ((UINTN) PadFile + GetFfsHeaderLength(PadFile))) {\r
       //\r
       // No alignment FixPoint in this Pad File.\r
       //\r
       continue;\r
     }\r
 \r
-    if ((UINTN) FvImage->Eof - (UINTN)FixPoint <= 0x20000) {    \r
+    if ((UINTN) FvImage->Eof - (UINTN)FixPoint <= 0x20000) {\r
       //\r
       // Find the position to place ApResetVector\r
       //\r
@@ -3466,7 +4042,7 @@ Returns:
       return EFI_SUCCESS;\r
     }\r
   }\r
-  \r
+\r
   return EFI_NOT_FOUND;\r
 }\r
 \r
@@ -3493,7 +4069,7 @@ Returns:
   EFI_NOT_FOUND     A required string was not found in the INF file.\r
 --*/\r
 {\r
-  CHAR8       Value[_MAX_PATH];\r
+  CHAR8       Value[MAX_LONG_FILE_PATH];\r
   UINT64      Value64;\r
   UINTN       Index, Number;\r
   EFI_STATUS  Status;\r
@@ -3545,7 +4121,7 @@ Returns:
         CapInfo->Flags |= CAPSULE_FLAGS_INITIATE_RESET;\r
       }\r
     } else if (strstr (Value, "PersistAcrossReset") != NULL) {\r
-      CapInfo->Flags |= CAPSULE_FLAGS_PERSIST_ACROSS_RESET; \r
+      CapInfo->Flags |= CAPSULE_FLAGS_PERSIST_ACROSS_RESET;\r
       if (strstr (Value, "InitiateReset") != NULL) {\r
         CapInfo->Flags |= CAPSULE_FLAGS_INITIATE_RESET;\r
       }\r
@@ -3556,6 +4132,19 @@ Returns:
     DebugMsg (NULL, 0, 9, "Capsule Flag", Value);\r
   }\r
 \r
+  Status = FindToken (InfFile, OPTIONS_SECTION_STRING, EFI_OEM_CAPSULE_FLAGS_STRING, 0, Value);\r
+  if (Status == EFI_SUCCESS) {\r
+    Status = AsciiStringToUint64 (Value, FALSE, &Value64);\r
+    if (EFI_ERROR (Status) || Value64 > 0xffff) {\r
+      Error (NULL, 0, 2000, "Invalid parameter",\r
+        "invalid Flag setting for %s. Must be integer value between 0x0000 and 0xffff.",\r
+        EFI_OEM_CAPSULE_FLAGS_STRING);\r
+      return EFI_ABORTED;\r
+    }\r
+    CapInfo->Flags |= Value64;\r
+    DebugMsg (NULL, 0, 9, "Capsule Extend Flag", Value);\r
+  }\r
+\r
   //\r
   // Read Capsule File name\r
   //\r
@@ -3585,12 +4174,12 @@ Returns:
       // Add the file\r
       //\r
       strcpy (CapInfo->CapFiles[Index], Value);\r
-      DebugMsg (NULL, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index, CapInfo->CapFiles[Index]); \r
+      DebugMsg (NULL, 0, 9, "Capsule component file", "the %uth file name is %s", (unsigned) Index, CapInfo->CapFiles[Index]);\r
     } else {\r
       break;\r
     }\r
   }\r
-  \r
+\r
   if (Index == 0) {\r
     Warning (NULL, 0, 0, "Capsule components are not specified.", NULL);\r
   }\r
@@ -3633,7 +4222,7 @@ Returns:
   UINT32                Index;\r
   FILE                  *fpin, *fpout;\r
   EFI_STATUS            Status;\r
-  \r
+\r
   if (InfFileImage != NULL) {\r
     //\r
     // Initialize file structures\r
@@ -3641,7 +4230,7 @@ Returns:
     InfMemoryFile.FileImage           = InfFileImage;\r
     InfMemoryFile.CurrentFilePointer  = InfFileImage;\r
     InfMemoryFile.Eof                 = InfFileImage + InfFileSize;\r
-  \r
+\r
     //\r
     // Parse the Cap inf file for header information\r
     //\r
@@ -3650,7 +4239,7 @@ Returns:
       return Status;\r
     }\r
   }\r
-  \r
+\r
   if (mCapDataInfo.HeaderSize == 0) {\r
     //\r
     // make header size align 16 bytes.\r
@@ -3663,16 +4252,16 @@ Returns:
     Error (NULL, 0, 2000, "Invalid parameter", "The specified HeaderSize cannot be less than the size of EFI_CAPSULE_HEADER.");\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   if (CapFileName == NULL && mCapDataInfo.CapName[0] != '\0') {\r
     CapFileName = mCapDataInfo.CapName;\r
   }\r
-  \r
+\r
   if (CapFileName == NULL) {\r
     Error (NULL, 0, 2001, "Missing required argument", "Output Capsule file name");\r
     return EFI_INVALID_PARAMETER;\r
   }\r
-  \r
+\r
   //\r
   // Set Default Capsule Guid value\r
   //\r
@@ -3686,7 +4275,7 @@ Returns:
   FileSize = 0;\r
   CapSize  = mCapDataInfo.HeaderSize;\r
   while (mCapDataInfo.CapFiles [Index][0] != '\0') {\r
-    fpin = fopen (mCapDataInfo.CapFiles[Index], "rb");\r
+    fpin = fopen (LongFilePath (mCapDataInfo.CapFiles[Index]), "rb");\r
     if (fpin == NULL) {\r
       Error (NULL, 0, 0001, "Error opening file", mCapDataInfo.CapFiles[Index]);\r
       return EFI_ABORTED;\r
@@ -3710,7 +4299,7 @@ Returns:
   // Initialize the capsule header to zero\r
   //\r
   memset (CapBuffer, 0, mCapDataInfo.HeaderSize);\r
-  \r
+\r
   //\r
   // create capsule header and get capsule body\r
   //\r
@@ -3724,7 +4313,7 @@ Returns:
   FileSize = 0;\r
   CapSize  = CapsuleHeader->HeaderSize;\r
   while (mCapDataInfo.CapFiles [Index][0] != '\0') {\r
-    fpin = fopen (mCapDataInfo.CapFiles[Index], "rb");\r
+    fpin = fopen (LongFilePath (mCapDataInfo.CapFiles[Index]), "rb");\r
     if (fpin == NULL) {\r
       Error (NULL, 0, 0001, "Error opening file", mCapDataInfo.CapFiles[Index]);\r
       free (CapBuffer);\r
@@ -3736,11 +4325,11 @@ Returns:
     Index ++;\r
     CapSize += FileSize;\r
   }\r
-  \r
+\r
   //\r
   // write capsule data into the output file\r
   //\r
-  fpout = fopen (CapFileName, "wb");\r
+  fpout = fopen (LongFilePath (CapFileName), "wb");\r
   if (fpout == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", CapFileName);\r
     free (CapBuffer);\r
@@ -3749,7 +4338,8 @@ Returns:
 \r
   fwrite (CapBuffer, 1, CapSize, fpout);\r
   fclose (fpout);\r
-  \r
+  free (CapBuffer);\r
+\r
   VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize);\r
 \r
   return EFI_SUCCESS;\r