+++ /dev/null
-/*++\r
-\r
-Copyright (c) 2004-2006 Intel Corporation. All rights reserved\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
-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
-\r
-Module Name:\r
-\r
- GenAcpiTable.c\r
- \r
-Abstract:\r
-\r
- A utility that extracts the .DATA section from a PE/COFF image.\r
-\r
---*/\r
-\r
-#include <stdio.h>\r
-#include <string.h>\r
-#include <stdlib.h>\r
-\r
-#include <Common/UefiBaseTypes.h>\r
-#include <Common/EfiImage.h> // for PE32 structure definitions\r
-\r
-#include "CommonLib.h"\r
-#include "EfiUtilityMsgs.h"\r
-\r
-//\r
-// Version of this utility\r
-//\r
-#define UTILITY_NAME "GenAcpiTable"\r
-#define UTILITY_VERSION "v0.11"\r
-\r
-//\r
-// Define the max length of a filename\r
-//\r
-#define MAX_PATH 256\r
-#define DEFAULT_OUTPUT_EXTENSION ".acpi"\r
-\r
-//\r
-// Use this to track our command-line options and globals\r
-//\r
-struct {\r
- INT8 OutFileName[MAX_PATH];\r
- INT8 InFileName[MAX_PATH];\r
-} mOptions;\r
-\r
-//\r
-// Use these to convert from machine type value to a named type\r
-//\r
-typedef struct {\r
- UINT16 Value;\r
- INT8 *Name;\r
-} STRING_LOOKUP;\r
-\r
-static STRING_LOOKUP mMachineTypes[] = {\r
- EFI_IMAGE_MACHINE_IA32,\r
- "IA32",\r
- EFI_IMAGE_MACHINE_IA64,\r
- "IA64",\r
- EFI_IMAGE_MACHINE_EBC,\r
- "EBC",\r
- 0,\r
- NULL\r
-};\r
-\r
-static STRING_LOOKUP mSubsystemTypes[] = {\r
- EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION,\r
- "EFI application",\r
- EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER,\r
- "EFI boot service driver",\r
- EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER,\r
- "EFI runtime driver",\r
- 0,\r
- NULL\r
-};\r
-//\r
-// Function prototypes\r
-//\r
-static\r
-void\r
-Usage (\r
- VOID\r
- );\r
-\r
-static\r
-STATUS\r
-ParseCommandLine (\r
- int Argc,\r
- char *Argv[]\r
- );\r
-\r
-static\r
-STATUS\r
-CheckPE32File (\r
- INT8 *FileName,\r
- FILE *Fptr,\r
- UINT16 *MachineType,\r
- UINT16 *SubSystem\r
- );\r
-\r
-static\r
-STATUS\r
-ProcessFile (\r
- INT8 *InFileName,\r
- INT8 *OutFileName\r
- );\r
-\r
-static\r
-void\r
-DumpImage (\r
- INT8 *FileName\r
- );\r
-\r
-main (\r
- int Argc,\r
- char *Argv[]\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
-\r
-Arguments:\r
-\r
- Argc - standard C main() argument count\r
-\r
- Argv - standard C main() argument list\r
-\r
-Returns:\r
-\r
- 0 success\r
- non-zero otherwise\r
-\r
---*/\r
-// GC_TODO: ] - add argument and description to function comment\r
-{\r
- UINT32 Status;\r
-\r
- SetUtilityName (UTILITY_NAME);\r
- //\r
- // Parse the command line arguments\r
- //\r
- if (ParseCommandLine (Argc, Argv)) {\r
- return STATUS_ERROR;\r
- }\r
- //\r
- // Make sure we don't have the same filename for input and output files\r
- //\r
- if (stricmp (mOptions.OutFileName, mOptions.InFileName) == 0) {\r
- Error (NULL, 0, 0, mOptions.OutFileName, "input and output file names must be different");\r
- goto Finish;\r
- }\r
- //\r
- // Process the file\r
- //\r
- ProcessFile (mOptions.InFileName, mOptions.OutFileName);\r
-Finish:\r
- Status = GetUtilityStatus ();\r
- return Status;\r
-}\r
-\r
-static\r
-STATUS\r
-ProcessFile (\r
- INT8 *InFileName,\r
- INT8 *OutFileName\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Process a PE32 EFI file.\r
-\r
-Arguments:\r
-\r
- InFileName - Name of the PE32 EFI file to process.\r
- OutFileName - Name of the output file for the processed data.\r
-\r
-Returns:\r
-\r
- 0 - successful\r
-\r
---*/\r
-{\r
- STATUS Status;\r
- UINTN Index;\r
- FILE *InFptr;\r
- FILE *OutFptr;\r
- UINT16 MachineType;\r
- UINT16 SubSystem;\r
- UINT32 PESigOffset;\r
- EFI_IMAGE_FILE_HEADER FileHeader;\r
- EFI_IMAGE_OPTIONAL_HEADER32 OptionalHeader32;\r
- EFI_IMAGE_OPTIONAL_HEADER64 OptionalHeader64;\r
- EFI_IMAGE_SECTION_HEADER SectionHeader;\r
- UINT8 *Buffer;\r
- long SaveFilePosition;\r
-\r
- InFptr = NULL;\r
- OutFptr = NULL;\r
- Buffer = NULL;\r
- Status = STATUS_ERROR;\r
- //\r
- // Try to open the input file\r
- //\r
- if ((InFptr = fopen (InFileName, "rb")) == NULL) {\r
- Error (NULL, 0, 0, InFileName, "failed to open input file for reading");\r
- return STATUS_ERROR;\r
- }\r
- //\r
- // Double-check the file to make sure it's what we expect it to be\r
- //\r
- if (CheckPE32File (InFileName, InFptr, &MachineType, &SubSystem) != STATUS_SUCCESS) {\r
- goto Finish;\r
- }\r
- //\r
- // Per the PE/COFF specification, at offset 0x3C in the file is a 32-bit\r
- // offset (from the start of the file) to the PE signature, which always\r
- // follows the MSDOS stub. The PE signature is immediately followed by the\r
- // COFF file header.\r
- //\r
- //\r
- if (fseek (InFptr, 0x3C, SEEK_SET) != 0) {\r
- Error (NULL, 0, 0, InFileName, "failed to seek to PE signature in file", NULL);\r
- goto Finish;\r
- }\r
-\r
- if (fread (&PESigOffset, sizeof (PESigOffset), 1, InFptr) != 1) {\r
- Error (NULL, 0, 0, InFileName, "failed to read PE signature offset from file");\r
- goto Finish;\r
- }\r
-\r
- if (fseek (InFptr, PESigOffset + 4, SEEK_SET) != 0) {\r
- Error (NULL, 0, 0, InFileName, "failed to seek to PE signature");\r
- goto Finish;\r
- }\r
- //\r
- // We should now be at the COFF file header. Read it in and verify it's\r
- // of an image type we support.\r
- //\r
- if (fread (&FileHeader, sizeof (EFI_IMAGE_FILE_HEADER), 1, InFptr) != 1) {\r
- Error (NULL, 0, 0, InFileName, "failed to read file header from image");\r
- goto Finish;\r
- }\r
-\r
- if ((FileHeader.Machine != EFI_IMAGE_MACHINE_IA32) && (FileHeader.Machine != EFI_IMAGE_MACHINE_IA64) && (FileHeader.Machine != EFI_IMAGE_MACHINE_X64)) {\r
- Error (NULL, 0, 0, InFileName, "image is of an unsupported machine type 0x%X", (UINT32) FileHeader.Machine);\r
- goto Finish;\r
- }\r
- //\r
- // Read in the optional header. Assume PE32, and if not, then re-read as PE32+\r
- //\r
- SaveFilePosition = ftell (InFptr);\r
- if (fread (&OptionalHeader32, sizeof (EFI_IMAGE_OPTIONAL_HEADER32), 1, InFptr) != 1) {\r
- Error (NULL, 0, 0, InFileName, "failed to read optional header from input file");\r
- goto Finish;\r
- }\r
-\r
- if (OptionalHeader32.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
- if (fseek (InFptr, SaveFilePosition, SEEK_SET) != 0) {\r
- Error (NULL, 0, 0, InFileName, "failed to seek to .data section");\r
- goto Finish;\r
- }\r
-\r
- if (fread (&OptionalHeader64, sizeof (EFI_IMAGE_OPTIONAL_HEADER64), 1, InFptr) != 1) {\r
- Error (NULL, 0, 0, InFileName, "failed to read optional header from input file");\r
- goto Finish;\r
- }\r
- }\r
- //\r
- // Search for the ".data" section\r
- //\r
- for (Index = 0; Index < FileHeader.NumberOfSections; Index++) {\r
- if (fread (&SectionHeader, sizeof (EFI_IMAGE_SECTION_HEADER), 1, InFptr) != 1) {\r
- Error (NULL, 0, 0, InFileName, "failed to read optional header from input file");\r
- goto Finish;\r
- }\r
-\r
- if (strcmp (SectionHeader.Name, ".data") == 0) {\r
- if (fseek (InFptr, SectionHeader.PointerToRawData, SEEK_SET) != 0) {\r
- Error (NULL, 0, 0, InFileName, "failed to seek to .data section");\r
- goto Finish;\r
- }\r
-\r
- Buffer = (UINT8 *) malloc (SectionHeader.Misc.VirtualSize);\r
- if (Buffer == NULL) {\r
- Status = EFI_OUT_OF_RESOURCES;\r
- goto Finish;\r
- }\r
- if (fread (Buffer, SectionHeader.Misc.VirtualSize, 1, InFptr) != 1) {\r
- Error (NULL, 0, 0, InFileName, "failed to .data section");\r
- goto Finish;\r
- }\r
- //\r
- // Now open our output file\r
- //\r
- if ((OutFptr = fopen (OutFileName, "wb")) == NULL) {\r
- Error (NULL, 0, 0, OutFileName, "failed to open output file for writing");\r
- goto Finish;\r
- }\r
-\r
- if (fwrite (Buffer, SectionHeader.Misc.VirtualSize, 1, OutFptr) != 1) {\r
- Error (NULL, 0, 0, OutFileName, "failed to write .data section");\r
- goto Finish;\r
- }\r
-\r
- Status = STATUS_SUCCESS;\r
- goto Finish;\r
- }\r
- }\r
-\r
- Status = STATUS_ERROR;\r
-\r
-Finish:\r
- if (InFptr != NULL) {\r
- fclose (InFptr);\r
- }\r
- //\r
- // Close the output file. If there was an error, delete the output file so\r
- // that a subsequent build will rebuild it.\r
- //\r
- if (OutFptr != NULL) {\r
- fclose (OutFptr);\r
- if (GetUtilityStatus () == STATUS_ERROR) {\r
- remove (OutFileName);\r
- }\r
- }\r
-\r
- //\r
- // Free up our buffer\r
- //\r
- if (Buffer != NULL) {\r
- free (Buffer);\r
- }\r
-\r
- return Status;\r
-}\r
-\r
-static\r
-STATUS\r
-CheckPE32File (\r
- INT8 *FileName,\r
- FILE *Fptr,\r
- UINT16 *MachineType,\r
- UINT16 *SubSystem\r
- )\r
-/*++\r
-\r
-Routine Description:\r
-\r
- GC_TODO: Add function description\r
-\r
-Arguments:\r
-\r
- FileName - GC_TODO: add argument description\r
- Fptr - GC_TODO: add argument description\r
- MachineType - GC_TODO: add argument description\r
- SubSystem - GC_TODO: add argument description\r
-\r
-Returns:\r
-\r
- GC_TODO: add return values\r
-\r
---*/\r
-{\r
- /*++\r
-\r
-Routine Description:\r
- \r
- Given a file pointer to a supposed PE32 image file, verify that it is indeed a\r
- PE32 image file, and then return the machine type in the supplied pointer.\r
-\r
-Arguments:\r
-\r
- Fptr File pointer to the already-opened PE32 file\r
- MachineType Location to stuff the machine type of the PE32 file. This is needed\r
- because the image may be Itanium-based, IA32, or EBC.\r
-\r
-Returns:\r
-\r
- 0 success\r
- non-zero otherwise\r
-\r
---*/\r
- EFI_IMAGE_DOS_HEADER DosHeader;\r
- EFI_IMAGE_FILE_HEADER FileHdr;\r
- EFI_IMAGE_OPTIONAL_HEADER OptionalHdr;\r
- UINT32 PESig;\r
- STATUS Status;\r
-\r
- Status = STATUS_ERROR;\r
- //\r
- // Position to the start of the file\r
- //\r
- fseek (Fptr, 0, SEEK_SET);\r
- //\r
- // Read the DOS header\r
- //\r
- if (fread (&DosHeader, sizeof (DosHeader), 1, Fptr) != 1) {\r
- Error (NULL, 0, 0, FileName, "failed to read the DOS stub from the input file");\r
- goto Finish;\r
- }\r
- //\r
- // Check the magic number (0x5A4D)\r
- //\r
- if (DosHeader.e_magic != EFI_IMAGE_DOS_SIGNATURE) {\r
- Error (NULL, 0, 0, FileName, "input file does not appear to be a PE32 image (magic number)");\r
- goto Finish;\r
- }\r
- //\r
- // Position into the file and check the PE signature\r
- //\r
- fseek (Fptr, (long) DosHeader.e_lfanew, SEEK_SET);\r
- if (fread (&PESig, sizeof (PESig), 1, Fptr) != 1) {\r
- Error (NULL, 0, 0, FileName, "failed to read PE signature bytes");\r
- goto Finish;\r
- }\r
- //\r
- // Check the PE signature in the header "PE\0\0"\r
- //\r
- if (PESig != EFI_IMAGE_NT_SIGNATURE) {\r
- Error (NULL, 0, 0, FileName, "file does not appear to be a PE32 image (signature)");\r
- goto Finish;\r
- }\r
- //\r
- // Read the file header\r
- //\r
- if (fread (&FileHdr, sizeof (FileHdr), 1, Fptr) != 1) {\r
- Error (NULL, 0, 0, FileName, "failed to read PE file header from input file");\r
- goto Finish;\r
- }\r
- //\r
- // Read the optional header so we can get the subsystem\r
- //\r
- if (fread (&OptionalHdr, sizeof (OptionalHdr), 1, Fptr) != 1) {\r
- Error (NULL, 0, 0, FileName, "failed to read COFF optional header from input file");\r
- goto Finish;\r
- }\r
-\r
- *SubSystem = OptionalHdr.Subsystem;\r
- //\r
- // Good to go\r
- //\r
- Status = STATUS_SUCCESS;\r
-Finish:\r
- fseek (Fptr, 0, SEEK_SET);\r
- return Status;\r
-}\r
-\r
-static\r
-int\r
-ParseCommandLine (\r
- int Argc,\r
- char *Argv[]\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Given the Argc/Argv program arguments, and a pointer to an options structure,\r
- parse the command-line options and check their validity.\r
-\r
-\r
-Arguments:\r
-\r
- Argc - standard C main() argument count\r
- Argv - standard C main() argument list\r
-\r
-Returns:\r
-\r
- STATUS_SUCCESS success\r
- non-zero otherwise\r
-\r
---*/\r
-// GC_TODO: ] - add argument and description to function comment\r
-{\r
- //\r
- // Clear out the options\r
- //\r
- memset ((char *) &mOptions, 0, sizeof (mOptions));\r
- //\r
- // Skip over the program name\r
- //\r
- Argc--;\r
- Argv++;\r
-\r
- if (Argc != 2) {\r
- Usage ();\r
- return STATUS_ERROR;\r
- }\r
-\r
- strcpy (mOptions.InFileName, Argv[0]);\r
- //\r
- // Next argument\r
- //\r
- Argv++;\r
- Argc--;\r
-\r
- strcpy (mOptions.OutFileName, Argv[0]);\r
-\r
- return STATUS_SUCCESS;\r
-}\r
-\r
-static\r
-void\r
-Usage (\r
- VOID\r
- )\r
-/*++\r
-\r
-Routine Description:\r
- \r
- Print usage information for this utility.\r
-\r
-Arguments:\r
-\r
- None.\r
-\r
-Returns:\r
-\r
- Nothing.\r
-\r
---*/\r
-{\r
- int Index;\r
- static const char *Msg[] = {\r
- UTILITY_NAME " version "UTILITY_VERSION " - Generate ACPI Table image utility",\r
- " Generate an ACPI Table image from an EFI PE32 image",\r
- " Usage: "UTILITY_NAME " InFileName OutFileName",\r
- " where:",\r
- " InFileName - name of the input PE32 file",\r
- " OutFileName - to write output to OutFileName rather than InFileName"DEFAULT_OUTPUT_EXTENSION,\r
- "",\r
- NULL\r
- };\r
- for (Index = 0; Msg[Index] != NULL; Index++) {\r
- fprintf (stdout, "%s\n", Msg[Index]);\r
- }\r
-}\r