]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/GenFv/GenFvInternalLib.c
Sync basetools' source and binary files with r1707 of the basetools project.
[mirror_edk2.git] / BaseTools / Source / C / GenFv / GenFvInternalLib.c
index ad3e7a6e080ccf2022f6be12f7d2e09fcc0b44e8..917baa11e83d775d115c96235e869a8f34939166 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 \r
-Copyright (c) 2004 - 2008, Intel Corporation                                                         \r
+Copyright (c) 2004 - 2009, Intel Corporation                                                         \r
 All rights reserved. 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
@@ -184,7 +184,8 @@ Returns:
 {\r
   CHAR8       Value[_MAX_PATH];\r
   UINT64      Value64;\r
-  UINTN       Index, Number, Index1;\r
+  UINTN       Index;\r
+  UINTN       Number;\r
   EFI_STATUS  Status;\r
   EFI_GUID    GuidValue;\r
 \r
@@ -371,7 +372,7 @@ Returns:
       // Add the file\r
       //\r
       strcpy (FvInfo->FvFiles[Number + Index], Value);\r
-      DebugMsg (NULL, 0, 9, "FV component file", "the %dth name is %s", Index, Value);\r
+      DebugMsg (NULL, 0, 9, "FV component file", "the %uth name is %s", (unsigned) Index, Value);\r
     } else {\r
       break;\r
     }\r
@@ -514,6 +515,7 @@ EFI_STATUS
 AddPadFile (\r
   IN OUT MEMORY_FILE  *FvImage,\r
   IN UINT32           DataAlignment,\r
+  IN VOID             *FvEnd,\r
   IN EFI_FIRMWARE_VOLUME_EXT_HEADER *ExtHeader\r
   )\r
 /*++\r
@@ -525,9 +527,10 @@ Routine Description:
 \r
 Arguments:\r
 \r
-  FvImage         The memory image of the FV to add it to.  The current offset\r
-                  must be valid.\r
+  FvImage         The memory image of the FV to add it to.\r
+                  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
 \r
 Returns:\r
@@ -556,30 +559,6 @@ Returns:
     return EFI_SUCCESS;\r
   }\r
 \r
-  //\r
-  // Write pad file header\r
-  //\r
-  PadFile = (EFI_FFS_FILE_HEADER *) FvImage->CurrentFilePointer;\r
-\r
-  //\r
-  // Verify that we have enough space for the file header\r
-  //\r
-  if (ExtHeader != NULL) {\r
-    if ((UINTN) (PadFile + sizeof (EFI_FFS_FILE_HEADER) + ExtHeader->ExtHeaderSize) >= (UINTN) FvImage->Eof) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }    \r
-  } else {\r
-    if ((UINTN) (PadFile + sizeof (EFI_FFS_FILE_HEADER)) >= (UINTN) FvImage->Eof) {\r
-      return EFI_OUT_OF_RESOURCES;\r
-    }\r
-  }\r
-\r
-  //\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
-\r
   //\r
   // Calculate the pad file size\r
   //\r
@@ -587,11 +566,7 @@ Returns:
   // This is the earliest possible valid offset (current plus pad file header\r
   // plus the next file header)\r
   //\r
-  if (ExtHeader != NULL) {\r
-    PadFileSize = (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + (sizeof (EFI_FFS_FILE_HEADER) * 2) + ExtHeader->ExtHeaderSize;\r
-  } else {\r
-    PadFileSize = (UINTN) FvImage->CurrentFilePointer - (UINTN) FvImage->FileImage + (sizeof (EFI_FFS_FILE_HEADER) * 2);\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
@@ -609,6 +584,31 @@ Returns:
   //\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
+  }\r
+\r
+  //\r
+  // Verify that we have enough space for the file header\r
+  //\r
+  if (((UINTN) FvImage->CurrentFilePointer + PadFileSize) > (UINTN) FvEnd) {\r
+    return EFI_OUT_OF_RESOURCES;\r
+  }\r
+\r
+  //\r
+  // Write pad file header\r
+  //\r
+  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
+  //\r
+  PadFile->Type       = EFI_FV_FILETYPE_FFS_PAD;\r
+  PadFile->Attributes = 0;\r
+\r
   //\r
   // Write pad file size (calculated size minus next file header size)\r
   //\r
@@ -630,26 +630,26 @@ Returns:
     (EFI_FFS_FILE_HEADER *) PadFile,\r
     (EFI_FIRMWARE_VOLUME_HEADER *) FvImage->FileImage\r
     );\r
-  \r
+\r
+  //\r
+  // Update the current FV pointer\r
+  //\r
+  FvImage->CurrentFilePointer += PadFileSize;\r
+\r
   if (ExtHeader != NULL) {\r
     //\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
   }\r
 \r
-  //\r
-  // Verify that we have enough space (including the padding)\r
-  //\r
-  if (((UINTN)PadFile + PadFileSize) >= (UINTN) FvImage->Eof) {\r
-    return EFI_OUT_OF_RESOURCES;\r
-  }\r
-  //\r
-  // Update the current FV pointer\r
-  //\r
-  FvImage->CurrentFilePointer += PadFileSize;\r
-\r
   return EFI_SUCCESS;\r
 }\r
 \r
@@ -726,7 +726,7 @@ Returns:
   EFI_IMAGE_OPTIONAL_HEADER_UNION     *ImgHdr;\r
   EFI_TE_IMAGE_HEADER                 *TEImageHeader;\r
   EFI_IMAGE_SECTION_HEADER            *SectionHeader;\r
-  \r
+  unsigned long long                  TempLongAddress;\r
   //\r
   // Init local variable\r
   //\r
@@ -806,20 +806,20 @@ Returns:
   //\r
   if (ImageBaseAddress == 0) {\r
     fprintf (FvMapFile, "%s (dummy) (", KeyWord);\r
-    fprintf (FvMapFile, "BaseAddress=%08lx, ", ImageBaseAddress);\r
+    fprintf (FvMapFile, "BaseAddress=%08llx, ", (unsigned long long) ImageBaseAddress);\r
   } else {\r
     fprintf (FvMapFile, "%s (", KeyWord);\r
-    fprintf (FvMapFile, "BaseAddress=%08lx, ", ImageBaseAddress + Offset);\r
+    fprintf (FvMapFile, "BaseAddress=%08llx, ", (unsigned long long) (ImageBaseAddress + Offset));\r
   }\r
-  fprintf (FvMapFile, "EntryPoint=%08lx, ", ImageBaseAddress + AddressOfEntryPoint);\r
+  fprintf (FvMapFile, "EntryPoint=%08llx, ", (unsigned long long) (ImageBaseAddress + AddressOfEntryPoint));\r
   fprintf (FvMapFile, "GUID=%s", FileGuidName);\r
   fprintf (FvMapFile, ")\n"); \r
   \r
   for (; Index > 0; Index --, SectionHeader ++) {\r
         if (stricmp ((CHAR8 *)SectionHeader->Name, ".text") == 0) {\r
-               fprintf (FvMapFile, ".textbaseaddress=%08lx ",ImageBaseAddress + SectionHeader->VirtualAddress);\r
+               fprintf (FvMapFile, ".textbaseaddress=%08llx ", (unsigned long long) (ImageBaseAddress + SectionHeader->VirtualAddress));\r
        } else if (stricmp ((CHAR8 *)SectionHeader->Name, ".data") == 0) {\r
-         fprintf (FvMapFile, ".databaseaddress=%08lx ",ImageBaseAddress + SectionHeader->VirtualAddress);\r
+         fprintf (FvMapFile, ".databaseaddress=%08llx ", (unsigned long long) (ImageBaseAddress + SectionHeader->VirtualAddress));\r
        }\r
   }\r
   fprintf (FvMapFile, "\n\n"); \r
@@ -869,25 +869,27 @@ Returns:
     // Printf Function Information\r
     //\r
     if (FunctionType == 1) {\r
-      sscanf (Line, "%s %s %lx %s", KeyWord, FunctionName, &FunctionAddress, FunctionTypeName);\r
+      sscanf (Line, "%s %s %llx %s", KeyWord, FunctionName, &TempLongAddress, FunctionTypeName);\r
+      FunctionAddress = (UINT64) TempLongAddress;\r
       if (FunctionTypeName [1] == '\0' && (FunctionTypeName [0] == 'f' || FunctionTypeName [0] == 'F')) {\r
-        fprintf (FvMapFile, "  %016lx ", ImageBaseAddress + FunctionAddress);\r
-        fprintf (FvMapFile, "(%08lx) F  ", FunctionAddress - Offset);\r
+        fprintf (FvMapFile, "  %016llx ", (unsigned long long) (ImageBaseAddress + FunctionAddress));\r
+        fprintf (FvMapFile, "(%08llx) F  ", (unsigned long long) (FunctionAddress - Offset));\r
         fprintf (FvMapFile, "%s\n", FunctionName);\r
     } else {\r
-        fprintf (FvMapFile, "  %016lx ", ImageBaseAddress + FunctionAddress);\r
-        fprintf (FvMapFile, "(%08lx)    ", FunctionAddress - Offset);\r
+        fprintf (FvMapFile, "  %016llx ", (unsigned long long) (ImageBaseAddress + FunctionAddress));\r
+        fprintf (FvMapFile, "(%08llx)    ", (unsigned long long) (FunctionAddress - Offset));\r
         fprintf (FvMapFile, "%s\n", FunctionName);\r
       }\r
     } else if (FunctionType == 2) {\r
-      sscanf (Line, "%s %s %lx %s", KeyWord, FunctionName, &FunctionAddress, FunctionTypeName);\r
+      sscanf (Line, "%s %s %llx %s", KeyWord, FunctionName, &TempLongAddress, FunctionTypeName);\r
+      FunctionAddress = (UINT64) TempLongAddress;\r
       if (FunctionTypeName [1] == '\0' && (FunctionTypeName [0] == 'f' || FunctionTypeName [0] == 'F')) {\r
-        fprintf (FvMapFile, "  %016lx ", ImageBaseAddress + FunctionAddress);\r
-        fprintf (FvMapFile, "(%08lx) FS ", FunctionAddress - Offset);\r
+        fprintf (FvMapFile, "  %016llx ", (unsigned long long) (ImageBaseAddress + FunctionAddress));\r
+        fprintf (FvMapFile, "(%08llx) FS ", (unsigned long long) (FunctionAddress - Offset));\r
         fprintf (FvMapFile, "%s\n", FunctionName);\r
       } else {\r
-        fprintf (FvMapFile, "  %016lx ", ImageBaseAddress + FunctionAddress);\r
-        fprintf (FvMapFile, "(%08lx)    ", FunctionAddress - Offset);\r
+        fprintf (FvMapFile, "  %016llx ", (unsigned long long) (ImageBaseAddress + FunctionAddress));\r
+        fprintf (FvMapFile, "(%08llx)    ", (unsigned long long) (FunctionAddress - Offset));\r
         fprintf (FvMapFile, "%s\n", FunctionName);\r
       }\r
     }\r
@@ -1028,7 +1030,7 @@ Returns:
   //\r
   for (Index1 = 0; Index1 < Index; Index1 ++) {\r
     if (CompareGuid ((EFI_GUID *) FileBuffer, &mFileGuidArray [Index1]) == 0) {\r
-      Error (NULL, 0, 2000, "Invalid parameter", "the %dth file and %dth file have the same file GUID.", Index1 + 1, Index + 1);\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
       return EFI_INVALID_PARAMETER;\r
     }\r
@@ -1067,7 +1069,7 @@ Returns:
       // Sanity check. The file MUST align appropriately\r
       //\r
       if (((UINTN) *VtfFileImage + sizeof (EFI_FFS_FILE_HEADER) - (UINTN) FvImage->FileImage) % (1 << CurrentFileAlignment)) {\r
-        Error (NULL, 0, 3000, "Invalid", "VTF file cannot be aligned on a %d-byte boundary.", 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
@@ -1096,7 +1098,7 @@ Returns:
   //\r
   // Add pad file if necessary\r
   //\r
-  Status = AddPadFile (FvImage, 1 << CurrentFileAlignment, NULL);\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
@@ -1105,7 +1107,7 @@ Returns:
   //\r
   // Add file\r
   //\r
-  if ((FvImage->CurrentFilePointer + FileSize) < FvImage->Eof) {\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 Bs and Rt drivers for the debug genfvmap tool.\r
@@ -1174,6 +1176,10 @@ Returns:
     return EFI_SUCCESS;\r
   }\r
 \r
+  if ((UINTN) VtfFileImage < (UINTN) FvImage->CurrentFilePointer) {\r
+    return EFI_INVALID_PARAMETER;\r
+  }\r
+\r
   //\r
   // Pad file starts at beginning of free space\r
   //\r
@@ -1261,11 +1267,11 @@ Returns:
   UINT8                     *BytePointer2;\r
   UINT16                    *WordPointer;\r
   UINT16                    CheckSum;\r
+  UINT32                    IpiVector;\r
   UINTN                     Index;\r
   EFI_FFS_FILE_STATE        SavedState;\r
   UINT64                    FitAddress;\r
   FIT_TABLE                 *FitTablePtr;\r
-  UINT32                    IpiVector;\r
 \r
   //\r
   // Verify input parameters\r
@@ -1325,7 +1331,7 @@ Returns:
   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%X", 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
@@ -1365,7 +1371,7 @@ Returns:
   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%X", PeiCorePhysicalAddress);\r
+  DebugMsg (NULL, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress);\r
 \r
   if (MachineType == EFI_IMAGE_MACHINE_IA64) {\r
     //\r
@@ -1381,8 +1387,8 @@ Returns:
     //\r
     if (PeiCorePhysicalAddress & 0xF) {\r
       Error (NULL, 0, 3000, "Invalid",\r
-        "PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %Xh.",\r
-        PeiCorePhysicalAddress\r
+        "PEI_CORE entry point is not aligned on a 16 byte boundary, address specified is %llXh.",\r
+        (unsigned long long) PeiCorePhysicalAddress\r
         );\r
       return EFI_ABORTED;\r
     }\r
@@ -1411,8 +1417,8 @@ Returns:
     //\r
     if (SecCorePhysicalAddress & 0xF) {\r
       Error (NULL, 0, 3000, "Invalid",\r
-        "SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %Xh.",\r
-        SecCorePhysicalAddress\r
+        "SALE_ENTRY entry point is not aligned on a 16 byte boundary, address specified is %llXh.",\r
+        (unsigned long long) SecCorePhysicalAddress\r
         );\r
       return EFI_ABORTED;\r
     }\r
@@ -1448,7 +1454,7 @@ Returns:
     // \r
     Ia32ResetAddressPtr  = (UINT32 *) ((UINTN) FvImage->Eof - IA32_SEC_CORE_ENTRY_OFFSET);\r
     \r
-    Ia32SecEntryOffset   = SecCorePhysicalAddress - (FV_IMAGES_TOP_ADDRESS - IA32_SEC_CORE_ENTRY_OFFSET + 2);\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
@@ -1461,7 +1467,7 @@ Returns:
     //\r
     Ia32ResetAddressPtr   = (UINT32 *) ((UINTN) FvImage->Eof - 4);\r
     *Ia32ResetAddressPtr  = (UINT32) (FvInfo->BaseAddress);\r
-    DebugMsg (NULL, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%X.", FvInfo->BaseAddress);\r
+    DebugMsg (NULL, 0, 9, "update BFV base address in the top FV image", "BFV base address = 0x%llX.", (unsigned long long) FvInfo->BaseAddress);\r
 \r
     //\r
     // Update the Startup AP in the FVH header block ZeroVector region.\r
@@ -1507,9 +1513,9 @@ Returns:
     //\r
     // IpiVector at the 4k aligned address in the top 2 blocks in the PEI FV. \r
     //\r
-    IpiVector  = FV_IMAGES_TOP_ADDRESS - ((UINTN) FvImage->Eof - (UINTN) BytePointer);\r
-    DebugMsg (NULL, 0, 9, "Startup AP Vector address", "IpiVector at 0x%X", IpiVector);\r
-    if (IpiVector & 0xFFF != 0) {\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
+    if ((IpiVector & 0xFFF) != 0) {\r
       Error (NULL, 0, 3000, "Invalid", "Startup AP Vector address are not 4K aligned, because the FV size is not 4K aligned");\r
       return EFI_ABORTED;\r
     }\r
@@ -1527,7 +1533,7 @@ Returns:
     // Volume Top File, but if you have one for some reason don't crash...\r
     //\r
   } else {\r
-    Error (NULL, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", (UINT32) MachineType);\r
+    Error (NULL, 0, 3000, "Invalid", "machine type=0x%X in PEI core.", MachineType);\r
     return EFI_ABORTED;\r
   }\r
 \r
@@ -1575,7 +1581,7 @@ Routine Description:
   FIQ              +24\r
 \r
   We support two schemes on ARM.\r
-  1) Begining of the FV is the reset vector\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
     in the beginning of the FV (fixed size offset).\r
 \r
@@ -1671,7 +1677,7 @@ Returns:
       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%X", PeiCorePhysicalAddress);\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) {\r
         memset (ResetVector, 0, sizeof (ResetVector));\r
@@ -1726,7 +1732,7 @@ Returns:
   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%X", 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. It may not exist if SEC loads DXE core directly\r
@@ -1764,7 +1770,7 @@ Returns:
     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%X", PeiCorePhysicalAddress);\r
+    DebugMsg (NULL, 0, 9, "PeiCore physical entry point address", "Address = 0x%llX", (unsigned long long) PeiCorePhysicalAddress);\r
   }\r
   \r
   \r
@@ -1880,7 +1886,7 @@ Returns:
     // Verify NT header is expected\r
     //\r
     if (ImgHdr->Pe32.Signature != EFI_IMAGE_NT_SIGNATURE) {\r
-      Error (NULL, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", ImgHdr->Pe32.Signature);\r
+      Error (NULL, 0, 3000, "Invalid", "Unrecognized image signature 0x%08X.", (unsigned) ImgHdr->Pe32.Signature);\r
       return EFI_UNSUPPORTED;\r
     }\r
     //\r
@@ -1990,7 +1996,7 @@ Returns:
   //\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
-                  mFvDataInfo.FvFileSystemGuid.Data1,\r
+                  (unsigned) mFvDataInfo.FvFileSystemGuid.Data1,\r
                   mFvDataInfo.FvFileSystemGuid.Data2,\r
                   mFvDataInfo.FvFileSystemGuid.Data3,\r
                   mFvDataInfo.FvFileSystemGuid.Data4[0],\r
@@ -2007,7 +2013,7 @@ Returns:
   //\r
   if (mFvDataInfo.FvNameGuidSet) {\r
       DebugMsg (NULL, 0, 9, "FV Name Guid", "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", \r
-                  mFvDataInfo.FvNameGuid.Data1,\r
+                  (unsigned) mFvDataInfo.FvNameGuid.Data1,\r
                   mFvDataInfo.FvNameGuid.Data2,\r
                   mFvDataInfo.FvNameGuid.Data3,\r
                   mFvDataInfo.FvNameGuid.Data4[0],\r
@@ -2043,7 +2049,7 @@ Returns:
   if (EFI_ERROR (Status)) {\r
     return Status;    \r
   }\r
-  VerboseMsg ("the generated FV image size is %d bytes", mFvDataInfo.Size);\r
+  VerboseMsg ("the generated FV image size is %u bytes", (unsigned) mFvDataInfo.Size);\r
   \r
   //\r
   // support fv image and empty fv image\r
@@ -2120,7 +2126,7 @@ Returns:
   //\r
   // If there is no FFS file, generate one empty FV\r
   //\r
-  if (mFvDataInfo.FvFiles[0][0] == 0) {\r
+  if (mFvDataInfo.FvFiles[0][0] == 0 && !mFvDataInfo.FvNameGuidSet) {\r
     goto WriteFile;\r
   }\r
 \r
@@ -2155,15 +2161,15 @@ Returns:
   //\r
   if (mFvTotalSize != 0) {\r
     fprintf (FvMapFile, EFI_FV_TOTAL_SIZE_STRING);\r
-    fprintf (FvMapFile, " = 0x%x\n", mFvTotalSize);\r
+    fprintf (FvMapFile, " = 0x%x\n", (unsigned) mFvTotalSize);\r
   }\r
   if (mFvTakenSize != 0) {\r
     fprintf (FvMapFile, EFI_FV_TAKEN_SIZE_STRING);\r
-    fprintf (FvMapFile, " = 0x%x\n", mFvTakenSize);\r
+    fprintf (FvMapFile, " = 0x%x\n", (unsigned) mFvTakenSize);\r
   }\r
   if (mFvTotalSize != 0 && mFvTakenSize != 0) {\r
     fprintf (FvMapFile, EFI_FV_SPACE_SIZE_STRING);\r
-    fprintf (FvMapFile, " = 0x%x\n\n", mFvTotalSize - mFvTakenSize);\r
+    fprintf (FvMapFile, " = 0x%x\n\n", (unsigned) (mFvTotalSize - mFvTakenSize));\r
   }\r
 \r
   //\r
@@ -2172,7 +2178,7 @@ Returns:
   if (mFvDataInfo.FvNameGuidSet) {\r
     memcpy (&FvExtHeader.FvName, &mFvDataInfo.FvNameGuid, sizeof (EFI_GUID));\r
     FvExtHeader.ExtHeaderSize = sizeof (EFI_FIRMWARE_VOLUME_EXT_HEADER);\r
-    AddPadFile (&FvImageMemoryFile, 8, &FvExtHeader);\r
+    AddPadFile (&FvImageMemoryFile, 4, VtfFileImage, &FvExtHeader);\r
     //\r
     // Fv Extension header change update Fv Header Check sum\r
     //\r
@@ -2382,7 +2388,9 @@ Returns:
   UINT32              FfsAlignment;\r
   EFI_FFS_FILE_HEADER FfsHeader;\r
   BOOLEAN             VtfFileFlag;\r
+  UINTN               VtfFileSize;\r
   \r
+  VtfFileSize = 0;\r
   VtfFileFlag = FALSE;\r
   fpin  = NULL;\r
   Index = 0;\r
@@ -2453,24 +2461,22 @@ Returns:
                return EFI_ABORTED;\r
              }\r
              VtfFileFlag = TRUE;\r
-             //\r
-             // The space between Vft File and the latest file must be able to contain \r
-             // one ffs file header in order to add one pad file.\r
-             //\r
-             CurrentOffset += sizeof (EFI_FFS_FILE_HEADER);\r
-           }\r
-           //\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
-           }\r
+        VtfFileSize = FfsFileSize;\r
+        continue;\r
+      }\r
+\r
+      //\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
+      }\r
          }\r
 \r
     //\r
@@ -2489,8 +2495,8 @@ Returns:
        CurrentOffset = (CurrentOffset + EFI_FFS_FILE_HEADER_ALIGNMENT - 1) & ~(EFI_FFS_FILE_HEADER_ALIGNMENT - 1);\r
     }\r
   }\r
-  \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", CurrentOffset, FvInfoPtr->Size);\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
     //\r
@@ -2504,7 +2510,7 @@ Returns:
     //\r
     // Not invalid\r
     //\r
-    Error (NULL, 0, 3000, "Invalid", "the required fv image size 0x%x exceeds the set fv image size 0x%x", CurrentOffset, FvInfoPtr->Size);\r
+    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
@@ -2627,8 +2633,8 @@ Returns:
   //\r
   // Check XipAddress, BootAddress and RuntimeAddress\r
   //\r
-  Flags = 0;\r
-\r
+  Flags   = 0;\r
+  XipBase = 0;\r
   if (FvInfo->BaseAddress != 0) {\r
     Flags  |= REBASE_XIP_FILE;\r
     XipBase = FvInfo->BaseAddress + XipOffset;\r
@@ -2686,7 +2692,7 @@ Returns:
     ImageContext.ImageRead  = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;\r
     Status                  = PeCoffLoaderGetImageInfo (&ImageContext);\r
     if (EFI_ERROR (Status)) {\r
-      Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, Status);\r
+      Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, (int) Status);\r
       return Status;\r
     }\r
 \r
@@ -2785,7 +2791,7 @@ Returns:
           ImageContext.Handle = PeFileBuffer;\r
           Status              = PeCoffLoaderGetImageInfo (&ImageContext);\r
           if (EFI_ERROR (Status)) {\r
-            Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, Status);\r
+            Error (NULL, 0, 3000, "Invalid PeImage", "The input file is %s and the return status is %x", FileName, (int) Status);\r
             return Status;\r
           }\r
           ImageContext.RelocationsStripped = FALSE;\r
@@ -2935,7 +2941,7 @@ Returns:
       ImgHdr->Pe32Plus.OptionalHeader.ImageBase     = NewPe32BaseAddress;\r
     } else {\r
       Error (NULL, 0, 3000, "Invalid", "unknown PE magic signature %X in PE32 image %s",\r
-        (UINT32) ImgHdr->Pe32.OptionalHeader.Magic,\r
+        ImgHdr->Pe32.OptionalHeader.Magic,\r
         FileName\r
         );\r
       return EFI_ABORTED;\r
@@ -3018,7 +3024,7 @@ WritePeMap:
     ImageContext.ImageRead  = (PE_COFF_LOADER_READ_FILE) FfsRebaseImageRead;\r
     Status                  = PeCoffLoaderGetImageInfo (&ImageContext);\r
     if (EFI_ERROR (Status)) {\r
-      Error (NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, Status);\r
+      Error (NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, (int) Status);\r
       return Status;\r
     }\r
 \r
@@ -3101,7 +3107,7 @@ WritePeMap:
         ImageContext.Handle = PeFileBuffer;\r
         Status              = PeCoffLoaderGetImageInfo (&ImageContext);\r
         if (EFI_ERROR (Status)) {\r
-          Error (NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, Status);\r
+          Error (NULL, 0, 3000, "Invalid TeImage", "The input file is %s and the return status is %x", FileName, (int) Status);\r
           return Status;\r
         }\r
         ImageContext.RelocationsStripped = FALSE;\r
@@ -3365,8 +3371,14 @@ Returns:
   if (Status == EFI_SUCCESS) {\r
     if (strstr (Value, "PopulateSystemTable") != NULL) {\r
       CapInfo->Flags |= CAPSULE_FLAGS_PERSIST_ACROSS_RESET | CAPSULE_FLAGS_POPULATE_SYSTEM_TABLE;\r
+      if (strstr (Value, "InitiateReset") != NULL) {\r
+        CapInfo->Flags |= CAPSULE_FLAGS_INITIATE_RESET;\r
+      }\r
     } else if (strstr (Value, "PersistAcrossReset") != NULL) {\r
       CapInfo->Flags |= CAPSULE_FLAGS_PERSIST_ACROSS_RESET; \r
+      if (strstr (Value, "InitiateReset") != NULL) {\r
+        CapInfo->Flags |= CAPSULE_FLAGS_INITIATE_RESET;\r
+      }\r
     } else {\r
       Error (NULL, 0, 2000, "Invalid parameter", "invalid Flag setting for %s.", EFI_CAPSULE_FLAGS_STRING);\r
       return EFI_ABORTED;\r
@@ -3403,7 +3415,7 @@ Returns:
       // Add the file\r
       //\r
       strcpy (CapInfo->CapFiles[Index], Value);\r
-      DebugMsg (NULL, 0, 9, "Capsule component file", "the %dth file name is %s", 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
@@ -3568,7 +3580,7 @@ Returns:
   fwrite (CapBuffer, 1, CapSize, fpout);\r
   fclose (fpout);\r
   \r
-  VerboseMsg ("The size of the generated capsule image is %d bytes", CapSize);\r
+  VerboseMsg ("The size of the generated capsule image is %u bytes", (unsigned) CapSize);\r
 \r
   return EFI_SUCCESS;\r
 }\r