3 Copyright (c) 2004-2006 Intel Corporation. All rights reserved
\r
4 This program and the accompanying materials are licensed and made available
\r
5 under the terms and conditions of the BSD License which accompanies this
\r
6 distribution. The full text of the license may be found at
\r
7 http://opensource.org/licenses/bsd-license.php
\r
9 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
\r
10 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
\r
18 A utility that extracts the .DATA section from a PE/COFF image.
\r
26 #include <Common/UefiBaseTypes.h>
\r
27 #include <Common/EfiImage.h> // for PE32 structure definitions
\r
29 #include "CommonLib.h"
\r
30 #include "EfiUtilityMsgs.h"
\r
33 // Version of this utility
\r
35 #define UTILITY_NAME "GenAcpiTable"
\r
36 #define UTILITY_MAJOR_VERSION 0
\r
37 #define UTILITY_MINOR_VERSION 11
\r
40 // Define the max length of a filename
\r
42 #define MAX_PATH 256
\r
43 #define DEFAULT_OUTPUT_EXTENSION ".acpi"
\r
46 // Use this to track our command-line options and globals
\r
49 INT8 OutFileName[MAX_PATH];
\r
50 INT8 InFileName[MAX_PATH];
\r
54 // Use these to convert from machine type value to a named type
\r
61 static STRING_LOOKUP mMachineTypes[] = {
\r
62 EFI_IMAGE_MACHINE_IA32,
\r
64 EFI_IMAGE_MACHINE_IA64,
\r
66 EFI_IMAGE_MACHINE_EBC,
\r
72 static STRING_LOOKUP mSubsystemTypes[] = {
\r
73 EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION,
\r
75 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER,
\r
76 "EFI boot service driver",
\r
77 EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER,
\r
78 "EFI runtime driver",
\r
83 // Function prototypes
\r
109 UINT16 *MachineType,
\r
132 Routine Description:
\r
137 Argc - standard C main() argument count
\r
139 Argv - standard C main() argument list
\r
147 // GC_TODO: ] - add argument and description to function comment
\r
151 SetUtilityName (UTILITY_NAME);
\r
153 // Parse the command line arguments
\r
155 if (ParseCommandLine (Argc, Argv)) {
\r
156 return STATUS_ERROR;
\r
159 // Make sure we don't have the same filename for input and output files
\r
161 if (stricmp (mOptions.OutFileName, mOptions.InFileName) == 0) {
\r
162 Error (NULL, 0, 0, mOptions.OutFileName, "input and output file names must be different");
\r
166 // Process the file
\r
168 ProcessFile (mOptions.InFileName, mOptions.OutFileName);
\r
170 Status = GetUtilityStatus ();
\r
182 Routine Description:
\r
184 Process a PE32 EFI file.
\r
188 InFileName - Name of the PE32 EFI file to process.
\r
189 OutFileName - Name of the output file for the processed data.
\r
201 UINT16 MachineType;
\r
203 UINT32 PESigOffset;
\r
204 EFI_IMAGE_FILE_HEADER FileHeader;
\r
205 EFI_IMAGE_OPTIONAL_HEADER32 OptionalHeader32;
\r
206 EFI_IMAGE_OPTIONAL_HEADER64 OptionalHeader64;
\r
207 EFI_IMAGE_SECTION_HEADER SectionHeader;
\r
209 long SaveFilePosition;
\r
214 Status = STATUS_ERROR;
\r
216 // Try to open the input file
\r
218 if ((InFptr = fopen (InFileName, "rb")) == NULL) {
\r
219 Error (NULL, 0, 0, InFileName, "failed to open input file for reading");
\r
220 return STATUS_ERROR;
\r
223 // Double-check the file to make sure it's what we expect it to be
\r
225 if (CheckPE32File (InFileName, InFptr, &MachineType, &SubSystem) != STATUS_SUCCESS) {
\r
229 // Per the PE/COFF specification, at offset 0x3C in the file is a 32-bit
\r
230 // offset (from the start of the file) to the PE signature, which always
\r
231 // follows the MSDOS stub. The PE signature is immediately followed by the
\r
232 // COFF file header.
\r
235 if (fseek (InFptr, 0x3C, SEEK_SET) != 0) {
\r
236 Error (NULL, 0, 0, InFileName, "failed to seek to PE signature in file", NULL);
\r
240 if (fread (&PESigOffset, sizeof (PESigOffset), 1, InFptr) != 1) {
\r
241 Error (NULL, 0, 0, InFileName, "failed to read PE signature offset from file");
\r
245 if (fseek (InFptr, PESigOffset + 4, SEEK_SET) != 0) {
\r
246 Error (NULL, 0, 0, InFileName, "failed to seek to PE signature");
\r
250 // We should now be at the COFF file header. Read it in and verify it's
\r
251 // of an image type we support.
\r
253 if (fread (&FileHeader, sizeof (EFI_IMAGE_FILE_HEADER), 1, InFptr) != 1) {
\r
254 Error (NULL, 0, 0, InFileName, "failed to read file header from image");
\r
258 if ((FileHeader.Machine != EFI_IMAGE_MACHINE_IA32) && (FileHeader.Machine != EFI_IMAGE_MACHINE_IA64) && (FileHeader.Machine != EFI_IMAGE_MACHINE_X64)) {
\r
259 Error (NULL, 0, 0, InFileName, "image is of an unsupported machine type 0x%X", (UINT32) FileHeader.Machine);
\r
263 // Read in the optional header. Assume PE32, and if not, then re-read as PE32+
\r
265 SaveFilePosition = ftell (InFptr);
\r
266 if (fread (&OptionalHeader32, sizeof (EFI_IMAGE_OPTIONAL_HEADER32), 1, InFptr) != 1) {
\r
267 Error (NULL, 0, 0, InFileName, "failed to read optional header from input file");
\r
271 if (OptionalHeader32.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
\r
272 if (fseek (InFptr, SaveFilePosition, SEEK_SET) != 0) {
\r
273 Error (NULL, 0, 0, InFileName, "failed to seek to .data section");
\r
277 if (fread (&OptionalHeader64, sizeof (EFI_IMAGE_OPTIONAL_HEADER64), 1, InFptr) != 1) {
\r
278 Error (NULL, 0, 0, InFileName, "failed to read optional header from input file");
\r
283 // Search for the ".data" section
\r
285 for (Index = 0; Index < FileHeader.NumberOfSections; Index++) {
\r
286 if (fread (&SectionHeader, sizeof (EFI_IMAGE_SECTION_HEADER), 1, InFptr) != 1) {
\r
287 Error (NULL, 0, 0, InFileName, "failed to read optional header from input file");
\r
291 if (strcmp (SectionHeader.Name, ".data") == 0 || strcmp (SectionHeader.Name, ".sdata") == 0) {
\r
292 if (fseek (InFptr, SectionHeader.PointerToRawData, SEEK_SET) != 0) {
\r
293 Error (NULL, 0, 0, InFileName, "failed to seek to .data section");
\r
297 Buffer = (UINT8 *) malloc (SectionHeader.Misc.VirtualSize);
\r
298 if (Buffer == NULL) {
\r
299 Status = EFI_OUT_OF_RESOURCES;
\r
302 if (fread (Buffer, SectionHeader.Misc.VirtualSize, 1, InFptr) != 1) {
\r
303 Error (NULL, 0, 0, InFileName, "failed to .data section");
\r
307 // Now open our output file
\r
309 if ((OutFptr = fopen (OutFileName, "wb")) == NULL) {
\r
310 Error (NULL, 0, 0, OutFileName, "failed to open output file for writing");
\r
314 if (fwrite (Buffer, SectionHeader.Misc.VirtualSize, 1, OutFptr) != 1) {
\r
315 Error (NULL, 0, 0, OutFileName, "failed to write .data section");
\r
319 Status = STATUS_SUCCESS;
\r
324 Status = STATUS_ERROR;
\r
327 if (InFptr != NULL) {
\r
331 // Close the output file. If there was an error, delete the output file so
\r
332 // that a subsequent build will rebuild it.
\r
334 if (OutFptr != NULL) {
\r
336 if (GetUtilityStatus () == STATUS_ERROR) {
\r
337 remove (OutFileName);
\r
342 // Free up our buffer
\r
344 if (Buffer != NULL) {
\r
356 UINT16 *MachineType,
\r
361 Routine Description:
\r
363 GC_TODO: Add function description
\r
367 FileName - GC_TODO: add argument description
\r
368 Fptr - GC_TODO: add argument description
\r
369 MachineType - GC_TODO: add argument description
\r
370 SubSystem - GC_TODO: add argument description
\r
374 GC_TODO: add return values
\r
380 Routine Description:
\r
382 Given a file pointer to a supposed PE32 image file, verify that it is indeed a
\r
383 PE32 image file, and then return the machine type in the supplied pointer.
\r
387 Fptr File pointer to the already-opened PE32 file
\r
388 MachineType Location to stuff the machine type of the PE32 file. This is needed
\r
389 because the image may be Itanium-based, IA32, or EBC.
\r
397 EFI_IMAGE_DOS_HEADER DosHeader;
\r
398 EFI_IMAGE_FILE_HEADER FileHdr;
\r
399 EFI_IMAGE_OPTIONAL_HEADER OptionalHdr;
\r
403 Status = STATUS_ERROR;
\r
405 // Position to the start of the file
\r
407 fseek (Fptr, 0, SEEK_SET);
\r
409 // Read the DOS header
\r
411 if (fread (&DosHeader, sizeof (DosHeader), 1, Fptr) != 1) {
\r
412 Error (NULL, 0, 0, FileName, "failed to read the DOS stub from the input file");
\r
416 // Check the magic number (0x5A4D)
\r
418 if (DosHeader.e_magic != EFI_IMAGE_DOS_SIGNATURE) {
\r
419 Error (NULL, 0, 0, FileName, "input file does not appear to be a PE32 image (magic number)");
\r
423 // Position into the file and check the PE signature
\r
425 fseek (Fptr, (long) DosHeader.e_lfanew, SEEK_SET);
\r
426 if (fread (&PESig, sizeof (PESig), 1, Fptr) != 1) {
\r
427 Error (NULL, 0, 0, FileName, "failed to read PE signature bytes");
\r
431 // Check the PE signature in the header "PE\0\0"
\r
433 if (PESig != EFI_IMAGE_NT_SIGNATURE) {
\r
434 Error (NULL, 0, 0, FileName, "file does not appear to be a PE32 image (signature)");
\r
438 // Read the file header
\r
440 if (fread (&FileHdr, sizeof (FileHdr), 1, Fptr) != 1) {
\r
441 Error (NULL, 0, 0, FileName, "failed to read PE file header from input file");
\r
445 // Read the optional header so we can get the subsystem
\r
447 if (fread (&OptionalHdr, sizeof (OptionalHdr), 1, Fptr) != 1) {
\r
448 Error (NULL, 0, 0, FileName, "failed to read COFF optional header from input file");
\r
452 *SubSystem = OptionalHdr.Subsystem;
\r
456 Status = STATUS_SUCCESS;
\r
458 fseek (Fptr, 0, SEEK_SET);
\r
470 Routine Description:
\r
472 Given the Argc/Argv program arguments, and a pointer to an options structure,
\r
473 parse the command-line options and check their validity.
\r
478 Argc - standard C main() argument count
\r
479 Argv - standard C main() argument list
\r
483 STATUS_SUCCESS success
\r
487 // GC_TODO: ] - add argument and description to function comment
\r
490 // Clear out the options
\r
492 memset ((char *) &mOptions, 0, sizeof (mOptions));
\r
494 // Skip over the program name
\r
501 return STATUS_ERROR;
\r
504 if ((strcmp(Argv[0], "-h") == 0) || (strcmp(Argv[0], "--help") == 0) ||
\r
505 (strcmp(Argv[0], "-?") == 0) || (strcmp(Argv[0], "/?") == 0)) {
\r
507 return STATUS_ERROR;
\r
510 if ((strcmp(Argv[0], "-V") == 0) || (strcmp(Argv[0], "--version") == 0)) {
\r
512 return STATUS_ERROR;
\r
517 return STATUS_ERROR;
\r
520 strcpy (mOptions.InFileName, Argv[0]);
\r
527 strcpy (mOptions.OutFileName, Argv[0]);
\r
529 return STATUS_SUCCESS;
\r
538 printf ("%s v%d.%d -EDK Utility for generating ACPI Table image from an EFI PE32 image.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);
\r
539 printf ("Copyright (c) 1999-2006 Intel Corporation. All rights reserved.\n");
\r
550 Routine Description:
\r
552 Print usage information for this utility.
\r
565 static const char *Msg[] = {
\r
566 "\nUsage: "UTILITY_NAME " {-h|--help|-?|/?|-V|--version} InFileName OutFileName",
\r
568 " InFileName - name of the input PE32 file",
\r
569 " OutFileName - to write output to OutFileName rather than InFileName"DEFAULT_OUTPUT_EXTENSION,
\r
574 for (Index = 0; Msg[Index] != NULL; Index++) {
\r
575 fprintf (stdout, "%s\n", Msg[Index]);
\r