]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/EfiRom/EfiRom.c
Sync BaseTool trunk (version r2640) into EDKII BaseTools.
[mirror_edk2.git] / BaseTools / Source / C / EfiRom / EfiRom.c
index 5152f38ba0599b46e3efdfddf2252f27f4c006fd..9f7da8017ca98efc5b6feee891720ebd63d79089 100644 (file)
@@ -1,6 +1,6 @@
 /** @file\r
 \r
-Copyright (c) 1999 - 2011, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 1999 - 2013, Intel Corporation. All rights reserved.<BR>\r
 This program and the accompanying materials are licensed and made available \r
 under the terms and conditions of the BSD License which accompanies this \r
 distribution.  The full text of the license may be found at\r
@@ -237,6 +237,7 @@ Returns:
   PCI_3_0_DATA_STRUCTURE    *PciDs30;\r
   UINT32                    Index;\r
   UINT8                     ByteCheckSum;\r
+  UINT16                    CodeType;\r
  \r
   PciDs23 = NULL;\r
   PciDs30 = NULL;\r
@@ -337,8 +338,10 @@ Returns:
   //\r
   if (mOptions.Pci23 == 1) {\r
     PciDs23->ImageLength = (UINT16) (TotalSize / 512);\r
+    CodeType = PciDs23->CodeType;\r
   } else {\r
     PciDs30->ImageLength = (UINT16) (TotalSize / 512);\r
+    CodeType = PciDs30->CodeType;\r
        }\r
 \r
   //\r
@@ -359,14 +362,16 @@ Returns:
                }\r
   }\r
 \r
-  ByteCheckSum = 0;\r
-  for (Index = 0; Index < FileSize - 1; Index++) {\r
-    ByteCheckSum = (UINT8) (ByteCheckSum + Buffer[Index]);\r
-  }\r
+  if (CodeType != PCI_CODE_TYPE_EFI_IMAGE) {\r
+    ByteCheckSum = 0;\r
+    for (Index = 0; Index < FileSize - 1; Index++) {\r
+      ByteCheckSum = (UINT8) (ByteCheckSum + Buffer[Index]);\r
+    }\r
 \r
-  Buffer[FileSize - 1] = (UINT8) ((~ByteCheckSum) + 1);\r
-  if (mOptions.Verbose) {\r
-    VerboseMsg("  Checksum = %02x\n\n", Buffer[FileSize - 1]);\r
+    Buffer[FileSize - 1] = (UINT8) ((~ByteCheckSum) + 1);\r
+    if (mOptions.Verbose) {\r
+      VerboseMsg("  Checksum = %02x\n\n", Buffer[FileSize - 1]);\r
+    }\r
   }\r
 \r
   //\r
@@ -449,6 +454,8 @@ Returns:
   UINT16                        MachineType;\r
   UINT16                        SubSystem;\r
   UINT32                        HeaderPadBytes;\r
+  UINT32                        PadBytesBeforeImage;\r
+  UINT32                        PadBytesAfterImage;\r
 \r
   //\r
   // Try to open the input file\r
@@ -559,6 +566,18 @@ Returns:
     TotalSize = (TotalSize + 0x200) &~0x1ff;\r
   }\r
   //\r
+  // Workaround:\r
+  //   If compressed, put the pad bytes after the image,\r
+  //   else put the pad bytes before the image.\r
+  //\r
+  if ((InFile->FileFlags & FILE_FLAG_COMPRESS) != 0) {\r
+    PadBytesBeforeImage = 0;\r
+    PadBytesAfterImage = TotalSize - (FileSize + HeaderSize);\r
+  } else {\r
+    PadBytesBeforeImage = TotalSize - (FileSize + HeaderSize);\r
+    PadBytesAfterImage = 0;\r
+  }\r
+  //\r
   // Check size\r
   //\r
   if (TotalSize > MAX_OPTION_ROM_SIZE) {\r
@@ -581,7 +600,7 @@ Returns:
   RomHdr.EfiSignature         = EFI_PCI_EXPANSION_ROM_HEADER_EFISIGNATURE;\r
   RomHdr.EfiSubsystem         = SubSystem;\r
   RomHdr.EfiMachineType       = MachineType;\r
-  RomHdr.EfiImageHeaderOffset = (UINT16) HeaderSize;\r
+  RomHdr.EfiImageHeaderOffset = (UINT16) (HeaderSize + PadBytesBeforeImage);\r
   RomHdr.PcirOffset           = (UINT16) (sizeof (RomHdr) + HeaderPadBytes);\r
   //\r
   // Set image as compressed or not\r
@@ -686,11 +705,18 @@ Returns:
       goto BailOut;\r
     } \r
   }\r
+\r
   //\r
-  // Keep track of how many bytes left to write\r
+  // Pad head to make it a multiple of 512 bytes\r
   //\r
-  TotalSize -= HeaderSize;\r
-\r
+  while (PadBytesBeforeImage > 0) {\r
+    if (putc (~0, OutFptr) == EOF) {\r
+      Error (NULL, 0, 2000, "Failed to write trailing pad bytes output file!", NULL);\r
+      Status = STATUS_ERROR;\r
+      goto BailOut;\r
+    }\r
+    PadBytesBeforeImage--;\r
+  }\r
   //\r
   // Now dump the input file's contents to the output file\r
   //\r
@@ -700,18 +726,17 @@ Returns:
     goto BailOut;\r
   }\r
 \r
-  TotalSize -= FileSize;\r
   //\r
   // Pad the rest of the image to make it a multiple of 512 bytes\r
   //\r
-  while (TotalSize > 0) {\r
+  while (PadBytesAfterImage > 0) {\r
     if (putc (~0, OutFptr) == EOF) {\r
       Error (NULL, 0, 2000, "Failed to write trailing pad bytes output file!", NULL);\r
       Status = STATUS_ERROR;\r
       goto BailOut;\r
     }\r
 \r
-    TotalSize--;\r
+    PadBytesAfterImage--;\r
   }\r
 \r
 BailOut:\r
@@ -1205,7 +1230,7 @@ Returns:
   //\r
   // Copyright declaration\r
   // \r
-  fprintf (stdout, "Copyright (c) 2007 - 2011, Intel Corporation. All rights reserved.\n\n");\r
+  fprintf (stdout, "Copyright (c) 2007 - 2013, Intel Corporation. All rights reserved.\n\n");\r
 \r
   //\r
   // Details Option\r