]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/GenAcpiTable/GenAcpiTable.c
Adding Additional Tools that are needed for Platform Image creation.
[mirror_edk2.git] / Tools / Source / TianoTools / GenAcpiTable / GenAcpiTable.c
1 /*++
2
3 Copyright (c) 2002 Intel Corporation. All rights reserved
4 This software and associated documentation (if any) is furnished
5 under a license and may only be used or copied in accordance
6 with the terms of the license. Except as permitted by such
7 license, no part of this software or documentation may be
8 reproduced, stored in a retrieval system, or transmitted in any
9 form or by any means without the express written consent of
10 Intel Corporation.
11
12 Module Name:
13
14 GenAcpiTable.c
15
16 Abstract:
17
18 A utility that extracts the .DATA section from a PE/COFF image.
19
20 --*/
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "Tiano.h"
27 #include "TianoCommon.h"
28 #include "EfiImage.h" // for PE32 structure definitions
29 #include "EfiUtilityMsgs.h"
30
31 //
32 // Version of this utility
33 //
34 #define UTILITY_NAME "GenAcpiTable"
35 #define UTILITY_VERSION "v0.11"
36
37 //
38 // Define the max length of a filename
39 //
40 #define MAX_PATH 256
41 #define DEFAULT_OUTPUT_EXTENSION ".acpi"
42
43 //
44 // Use this to track our command-line options and globals
45 //
46 struct {
47 INT8 OutFileName[MAX_PATH];
48 INT8 InFileName[MAX_PATH];
49 } mOptions;
50
51 //
52 // Use these to convert from machine type value to a named type
53 //
54 typedef struct {
55 UINT16 Value;
56 INT8 *Name;
57 } STRING_LOOKUP;
58
59 static STRING_LOOKUP mMachineTypes[] = {
60 EFI_IMAGE_MACHINE_IA32,
61 "IA32",
62 EFI_IMAGE_MACHINE_IA64,
63 "IA64",
64 EFI_IMAGE_MACHINE_EBC,
65 "EBC",
66 0,
67 NULL
68 };
69
70 static STRING_LOOKUP mSubsystemTypes[] = {
71 EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION,
72 "EFI application",
73 EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER,
74 "EFI boot service driver",
75 EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER,
76 "EFI runtime driver",
77 0,
78 NULL
79 };
80 //
81 // Function prototypes
82 //
83 static
84 void
85 Usage (
86 VOID
87 );
88
89 static
90 STATUS
91 ParseCommandLine (
92 int Argc,
93 char *Argv[]
94 );
95
96 static
97 STATUS
98 CheckPE32File (
99 INT8 *FileName,
100 FILE *Fptr,
101 UINT16 *MachineType,
102 UINT16 *SubSystem
103 );
104
105 static
106 STATUS
107 ProcessFile (
108 INT8 *InFileName,
109 INT8 *OutFileName
110 );
111
112 static
113 void
114 DumpImage (
115 INT8 *FileName
116 );
117
118 main (
119 int Argc,
120 char *Argv[]
121 )
122 /*++
123
124 Routine Description:
125
126
127 Arguments:
128
129 Argc - standard C main() argument count
130
131 Argv - standard C main() argument list
132
133 Returns:
134
135 0 success
136 non-zero otherwise
137
138 --*/
139 // GC_TODO: ] - add argument and description to function comment
140 {
141 UINT32 Status;
142
143 SetUtilityName (UTILITY_NAME);
144 //
145 // Parse the command line arguments
146 //
147 if (ParseCommandLine (Argc, Argv)) {
148 return STATUS_ERROR;
149 }
150 //
151 // Make sure we don't have the same filename for input and output files
152 //
153 if (stricmp (mOptions.OutFileName, mOptions.InFileName) == 0) {
154 Error (NULL, 0, 0, mOptions.OutFileName, "input and output file names must be different");
155 goto Finish;
156 }
157 //
158 // Process the file
159 //
160 ProcessFile (mOptions.InFileName, mOptions.OutFileName);
161 Finish:
162 Status = GetUtilityStatus ();
163 return Status;
164 }
165
166 static
167 STATUS
168 ProcessFile (
169 INT8 *InFileName,
170 INT8 *OutFileName
171 )
172 /*++
173
174 Routine Description:
175
176 Process a PE32 EFI file.
177
178 Arguments:
179
180 InFileName - Name of the PE32 EFI file to process.
181 OutFileName - Name of the output file for the processed data.
182
183 Returns:
184
185 0 - successful
186
187 --*/
188 {
189 STATUS Status;
190 UINTN Index;
191 FILE *InFptr;
192 FILE *OutFptr;
193 UINT16 MachineType;
194 UINT16 SubSystem;
195 UINT32 PESigOffset;
196 EFI_IMAGE_FILE_HEADER FileHeader;
197 EFI_IMAGE_OPTIONAL_HEADER32 OptionalHeader32;
198 EFI_IMAGE_SECTION_HEADER SectionHeader;
199 UINT8 *Buffer;
200 long SaveFilePosition;
201
202 InFptr = NULL;
203 OutFptr = NULL;
204 Buffer = NULL;
205 Status = STATUS_ERROR;
206 //
207 // Try to open the input file
208 //
209 if ((InFptr = fopen (InFileName, "rb")) == NULL) {
210 Error (NULL, 0, 0, InFileName, "failed to open input file for reading");
211 return STATUS_ERROR;
212 }
213 //
214 // Double-check the file to make sure it's what we expect it to be
215 //
216 if (CheckPE32File (InFileName, InFptr, &MachineType, &SubSystem) != STATUS_SUCCESS) {
217 goto Finish;
218 }
219 //
220 // Per the PE/COFF specification, at offset 0x3C in the file is a 32-bit
221 // offset (from the start of the file) to the PE signature, which always
222 // follows the MSDOS stub. The PE signature is immediately followed by the
223 // COFF file header.
224 //
225 //
226 if (fseek (InFptr, 0x3C, SEEK_SET) != 0) {
227 Error (NULL, 0, 0, InFileName, "failed to seek to PE signature in file", NULL);
228 goto Finish;
229 }
230
231 if (fread (&PESigOffset, sizeof (PESigOffset), 1, InFptr) != 1) {
232 Error (NULL, 0, 0, InFileName, "failed to read PE signature offset from file");
233 goto Finish;
234 }
235
236 if (fseek (InFptr, PESigOffset + 4, SEEK_SET) != 0) {
237 Error (NULL, 0, 0, InFileName, "failed to seek to PE signature");
238 goto Finish;
239 }
240 //
241 // We should now be at the COFF file header. Read it in and verify it's
242 // of an image type we support.
243 //
244 if (fread (&FileHeader, sizeof (EFI_IMAGE_FILE_HEADER), 1, InFptr) != 1) {
245 Error (NULL, 0, 0, InFileName, "failed to read file header from image");
246 goto Finish;
247 }
248
249 if ((FileHeader.Machine != EFI_IMAGE_MACHINE_IA32) && (FileHeader.Machine != EFI_IMAGE_MACHINE_IA64) && (FileHeader.Machine != EFI_IMAGE_MACHINE_X64)) {
250 Error (NULL, 0, 0, InFileName, "image is of an unsupported machine type 0x%X", (UINT32) FileHeader.Machine);
251 goto Finish;
252 }
253 //
254 // Read in the optional header. Assume PE32, and if not, then re-read as PE32+
255 //
256 SaveFilePosition = ftell (InFptr);
257 if (fread (&OptionalHeader32, sizeof (EFI_IMAGE_OPTIONAL_HEADER32), 1, InFptr) != 1) {
258 Error (NULL, 0, 0, InFileName, "failed to read optional header from input file");
259 goto Finish;
260 }
261
262 if (OptionalHeader32.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
263 if (fseek (InFptr, SaveFilePosition, SEEK_SET) != 0) {
264 Error (NULL, 0, 0, InFileName, "failed to seek to .data section");
265 goto Finish;
266 }
267
268 if (fread (&OptionalHeader32, sizeof (EFI_IMAGE_OPTIONAL_HEADER64), 1, InFptr) != 1) {
269 Error (NULL, 0, 0, InFileName, "failed to read optional header from input file");
270 goto Finish;
271 }
272 }
273 //
274 // Search for the ".data" section
275 //
276 for (Index = 0; Index < FileHeader.NumberOfSections; Index++) {
277 if (fread (&SectionHeader, sizeof (EFI_IMAGE_SECTION_HEADER), 1, InFptr) != 1) {
278 Error (NULL, 0, 0, InFileName, "failed to read optional header from input file");
279 goto Finish;
280 }
281
282 if (strcmp (SectionHeader.Name, ".data") == 0) {
283 if (fseek (InFptr, SectionHeader.PointerToRawData, SEEK_SET) != 0) {
284 Error (NULL, 0, 0, InFileName, "failed to seek to .data section");
285 goto Finish;
286 }
287
288 Buffer = (UINT8 *) malloc (SectionHeader.Misc.VirtualSize);
289 if (Buffer == NULL) {
290 Status = EFI_OUT_OF_RESOURCES;
291 goto Finish;
292 }
293 if (fread (Buffer, SectionHeader.Misc.VirtualSize, 1, InFptr) != 1) {
294 Error (NULL, 0, 0, InFileName, "failed to .data section");
295 goto Finish;
296 }
297 //
298 // Now open our output file
299 //
300 if ((OutFptr = fopen (OutFileName, "wb")) == NULL) {
301 Error (NULL, 0, 0, OutFileName, "failed to open output file for writing");
302 goto Finish;
303 }
304
305 if (fwrite (Buffer, SectionHeader.Misc.VirtualSize, 1, OutFptr) != 1) {
306 Error (NULL, 0, 0, OutFileName, "failed to write .data section");
307 goto Finish;
308 }
309
310 Status = STATUS_SUCCESS;
311 goto Finish;
312 }
313 }
314
315 Status = STATUS_ERROR;
316
317 Finish:
318 if (InFptr != NULL) {
319 fclose (InFptr);
320 }
321 //
322 // Close the output file. If there was an error, delete the output file so
323 // that a subsequent build will rebuild it.
324 //
325 if (OutFptr != NULL) {
326 fclose (OutFptr);
327 if (GetUtilityStatus () == STATUS_ERROR) {
328 remove (OutFileName);
329 }
330 }
331
332 //
333 // Free up our buffer
334 //
335 if (Buffer != NULL) {
336 free (Buffer);
337 }
338
339 return Status;
340 }
341
342 static
343 STATUS
344 CheckPE32File (
345 INT8 *FileName,
346 FILE *Fptr,
347 UINT16 *MachineType,
348 UINT16 *SubSystem
349 )
350 /*++
351
352 Routine Description:
353
354 GC_TODO: Add function description
355
356 Arguments:
357
358 FileName - GC_TODO: add argument description
359 Fptr - GC_TODO: add argument description
360 MachineType - GC_TODO: add argument description
361 SubSystem - GC_TODO: add argument description
362
363 Returns:
364
365 GC_TODO: add return values
366
367 --*/
368 {
369 /*++
370
371 Routine Description:
372
373 Given a file pointer to a supposed PE32 image file, verify that it is indeed a
374 PE32 image file, and then return the machine type in the supplied pointer.
375
376 Arguments:
377
378 Fptr File pointer to the already-opened PE32 file
379 MachineType Location to stuff the machine type of the PE32 file. This is needed
380 because the image may be Itanium-based, IA32, or EBC.
381
382 Returns:
383
384 0 success
385 non-zero otherwise
386
387 --*/
388 EFI_IMAGE_DOS_HEADER DosHeader;
389 EFI_IMAGE_FILE_HEADER FileHdr;
390 EFI_IMAGE_OPTIONAL_HEADER OptionalHdr;
391 UINT32 PESig;
392 STATUS Status;
393
394 Status = STATUS_ERROR;
395 //
396 // Position to the start of the file
397 //
398 fseek (Fptr, 0, SEEK_SET);
399 //
400 // Read the DOS header
401 //
402 if (fread (&DosHeader, sizeof (DosHeader), 1, Fptr) != 1) {
403 Error (NULL, 0, 0, FileName, "failed to read the DOS stub from the input file");
404 goto Finish;
405 }
406 //
407 // Check the magic number (0x5A4D)
408 //
409 if (DosHeader.e_magic != EFI_IMAGE_DOS_SIGNATURE) {
410 Error (NULL, 0, 0, FileName, "input file does not appear to be a PE32 image (magic number)");
411 goto Finish;
412 }
413 //
414 // Position into the file and check the PE signature
415 //
416 fseek (Fptr, (long) DosHeader.e_lfanew, SEEK_SET);
417 if (fread (&PESig, sizeof (PESig), 1, Fptr) != 1) {
418 Error (NULL, 0, 0, FileName, "failed to read PE signature bytes");
419 goto Finish;
420 }
421 //
422 // Check the PE signature in the header "PE\0\0"
423 //
424 if (PESig != EFI_IMAGE_NT_SIGNATURE) {
425 Error (NULL, 0, 0, FileName, "file does not appear to be a PE32 image (signature)");
426 goto Finish;
427 }
428 //
429 // Read the file header
430 //
431 if (fread (&FileHdr, sizeof (FileHdr), 1, Fptr) != 1) {
432 Error (NULL, 0, 0, FileName, "failed to read PE file header from input file");
433 goto Finish;
434 }
435 //
436 // Read the optional header so we can get the subsystem
437 //
438 if (fread (&OptionalHdr, sizeof (OptionalHdr), 1, Fptr) != 1) {
439 Error (NULL, 0, 0, FileName, "failed to read COFF optional header from input file");
440 goto Finish;
441 }
442
443 *SubSystem = OptionalHdr.Subsystem;
444 //
445 // Good to go
446 //
447 Status = STATUS_SUCCESS;
448 Finish:
449 fseek (Fptr, 0, SEEK_SET);
450 return Status;
451 }
452
453 static
454 int
455 ParseCommandLine (
456 int Argc,
457 char *Argv[]
458 )
459 /*++
460
461 Routine Description:
462
463 Given the Argc/Argv program arguments, and a pointer to an options structure,
464 parse the command-line options and check their validity.
465
466
467 Arguments:
468
469 Argc - standard C main() argument count
470 Argv - standard C main() argument list
471
472 Returns:
473
474 STATUS_SUCCESS success
475 non-zero otherwise
476
477 --*/
478 // GC_TODO: ] - add argument and description to function comment
479 {
480 //
481 // Clear out the options
482 //
483 memset ((char *) &mOptions, 0, sizeof (mOptions));
484 //
485 // Skip over the program name
486 //
487 Argc--;
488 Argv++;
489
490 if (Argc != 2) {
491 Usage ();
492 return STATUS_ERROR;
493 }
494
495 strcpy (mOptions.InFileName, Argv[0]);
496 //
497 // Next argument
498 //
499 Argv++;
500 Argc--;
501
502 strcpy (mOptions.OutFileName, Argv[0]);
503
504 return STATUS_SUCCESS;
505 }
506
507 static
508 void
509 Usage (
510 VOID
511 )
512 /*++
513
514 Routine Description:
515
516 Print usage information for this utility.
517
518 Arguments:
519
520 None.
521
522 Returns:
523
524 Nothing.
525
526 --*/
527 {
528 int Index;
529 static const char *Msg[] = {
530 UTILITY_NAME " version "UTILITY_VERSION " - Generate ACPI Table image utility",
531 " Generate an ACPI Table image from an EFI PE32 image",
532 " Usage: "UTILITY_NAME " InFileName OutFileName",
533 " where:",
534 " InFileName - name of the input PE32 file",
535 " OutFileName - to write output to OutFileName rather than InFileName"DEFAULT_OUTPUT_EXTENSION,
536 "",
537 NULL
538 };
539 for (Index = 0; Msg[Index] != NULL; Index++) {
540 fprintf (stdout, "%s\n", Msg[Index]);
541 }
542 }