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