]> git.proxmox.com Git - mirror_edk2.git/blobdiff - BaseTools/Source/C/EfiRom/EfiRom.c
License header updated to match correct format.
[mirror_edk2.git] / BaseTools / Source / C / EfiRom / EfiRom.c
index 9f71b19323e9eea951bf984691e5cc73aa97c2bd..7a57912516989ec10509a2f303018365a44c0562 100644 (file)
@@ -1,6 +1,7 @@
 /** @file\r
+Utility program to create an EFI option ROM image from binary and EFI PE32 files.\r
 \r
-Copyright (c) 1999 - 2010, Intel Corporation. All rights reserved.<BR>\r
+Copyright (c) 1999 - 2014, 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
@@ -9,15 +10,6 @@ http://opensource.org/licenses/bsd-license.php
 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
-Module Name:\r
-\r
-  EfiRom.c\r
-  \r
-Abstract:\r
-\r
-  Utility program to create an EFI option ROM image from binary and \r
-  EFI PE32 files.\r
-\r
 **/\r
 \r
 #include "EfiUtilityMsgs.h"\r
@@ -135,7 +127,7 @@ Returns:
   //\r
   // Now open our output file\r
   //\r
-  if ((FptrOut = fopen (mOptions.OutFileName, "wb")) == NULL) {\r
+  if ((FptrOut = fopen (LongFilePath (mOptions.OutFileName), "wb")) == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", "Error opening file %s", mOptions.OutFileName);\r
     goto BailOut;\r
   }\r
@@ -237,6 +229,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
@@ -245,7 +238,7 @@ Returns:
   //\r
   // Try to open the input file\r
   //\r
-  if ((InFptr = fopen (InFile->FileName, "rb")) == NULL) {\r
+  if ((InFptr = fopen (LongFilePath (InFile->FileName), "rb")) == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", InFile->FileName);\r
     return STATUS_ERROR;\r
   }\r
@@ -337,8 +330,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 +354,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,11 +446,13 @@ 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
   //\r
-  if ((InFptr = fopen (InFile->FileName, "rb")) == NULL) {\r
+  if ((InFptr = fopen (LongFilePath (InFile->FileName), "rb")) == NULL) {\r
     Error (NULL, 0, 0001, "Open file error", "Error opening file: %s", InFile->FileName);\r
     return STATUS_ERROR;\r
   }\r
@@ -559,6 +558,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 +592,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 +697,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 +718,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
@@ -1200,19 +1217,19 @@ Returns:
   //\r
   // Summary usage\r
   //\r
-  fprintf (stdout, "Usage: %s [options] [file name<s>] \n\n", UTILITY_NAME);\r
+  fprintf (stdout, "Usage: %s -f VendorId -i DeviceId [options] [file name<s>] \n\n", UTILITY_NAME);\r
   \r
   //\r
   // Copyright declaration\r
   // \r
-  fprintf (stdout, "Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.\n\n");\r
+  fprintf (stdout, "Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.\n\n");\r
 \r
   //\r
   // Details Option\r
   //\r
   fprintf (stdout, "Options:\n");\r
   fprintf (stdout, "  -o FileName, --output FileName\n\\r
-            File will be created to store the ouput content.\n");\r
+            File will be created to store the output content.\n");\r
   fprintf (stdout, "  -e EfiFileName\n\\r
             EFI PE32 image files.\n");\r
   fprintf (stdout, "  -ec EfiFileName\n\\r
@@ -1224,9 +1241,9 @@ Returns:
   fprintf (stdout, "  -r Rev    Hex Revision in the PCI data structure header.\n");\r
   fprintf (stdout, "  -n        Not to automatically set the LAST bit in the last file.\n");\r
   fprintf (stdout, "  -f VendorId\n\\r
-            Hex PCI Vendor ID for the device OpROM.\n");\r
+            Hex PCI Vendor ID for the device OpROM, must be specified\n");\r
   fprintf (stdout, "  -i DeviceId\n\\r
-            Hex PCI Device ID for the device OpROM.\n");\r
+            Hex PCI Device ID for the device OpROM, must be specified\n");\r
   fprintf (stdout, "  -p, --pci23\n\\r
             Default layout meets PCI 3.0 specifications\n\\r
             specifying this flag will for a PCI 2.3 layout.\n");\r
@@ -1275,7 +1292,7 @@ Returns:
   //\r
   // Open the input file\r
   //\r
-  if ((InFptr = fopen (InFile->FileName, "rb")) == NULL) {\r
+  if ((InFptr = fopen (LongFilePath (InFile->FileName), "rb")) == NULL) {\r
     Error (NULL, 0, 0001, "Error opening file", InFile->FileName);\r
     return ;\r
   }\r