]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/ZeroDebugData/ZeroDebugData.c
6e454f2de2526e0a22b5d90cef247a584d542e6f
3 Copyright (c) 2004-2006 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
16 Zero the Debug Data Fields of Portable Executable (PE) format file.
30 print usage of ZeroDebugData command
38 // GC_TODO: void - add argument and description to function comment
41 // print usage of command
43 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
55 read data from a specified location of file
59 offset - number of bytes from beginning of file
60 buffer - buffer used to store data
69 // set file pointer to the specified location of file
71 if (fseek (fp
, offset
, SEEK_SET
) != 0) {
72 printf ("Error: Cannot move the current location of the file.\n");
76 // read data from the file
78 if (fread (buffer
, size
, 1, fp
) != 1) {
79 printf ("Error: Cannot read data from the file.\n");
95 Zero the debug data fields of the file
100 fpData - pointer to output file that ZeroDebugData progress is written to
109 unsigned char header
[4];
110 unsigned long offset
;
111 unsigned long NumberOfRvaAndSizes
;
113 unsigned long lvalue
;
115 unsigned long Pointer
;
116 unsigned char *Buffer
;
120 // read the header of file
122 if (ReadFromFile (fp
, 0, header
, 2) != 0) {
123 printf ("Error: open image file\n");
127 // "MZ" -- the header of image file (PE)
129 if (strncmp ((char *) header
, "MZ", 2) != 0) {
130 printf ("Error: Invalid Image file.\n");
134 // At location 0x3C, the stub has the file offset to the
137 if (ReadFromFile (fp
, 0x3C, &offset
, 4) != 0) {
141 // read the header of optional
143 if (ReadFromFile (fp
, offset
, header
, 4) != 0) {
147 // "PE\0\0" -- the signature of optional header
149 if (strncmp ((char *) header
, "PE\0\0", 4) != 0) {
150 printf ("Error: Invalid PE format file.\n");
154 // Add 16 to skip COFF file header, and get to optional header.
159 // Check the magic field, 0x10B for PE32 and 0x20B for PE32+
161 if (ReadFromFile (fp
, offset
, &nvalue
, 2) != 0) {
165 // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.
168 switch (nvalue
& 0xFFFF) {
171 printf ("Info: Image is PE32. ");
176 printf ("Info: Image is PE32+. ");
180 printf ("Error: Magic value is unknown.\n");
184 // get the value of NumberOfRvaAndSizes
186 if (ReadFromFile (fp
, offset
, &NumberOfRvaAndSizes
, 4) != 0) {
187 printf ("Error: read NumberOfRvaAndSizes error.\n");
191 // printf ("Info: NumberOfRvaAndSizes = %d\n", NumberOfRvaAndSizes);
194 // Finding Debug Table, offset of Debug Table
195 // is 4 + 6 * 8 = 52.
197 if (NumberOfRvaAndSizes
>= 7) {
198 if (ReadFromFile (fp
, offset
+ 52, &lvalue
, 4) != 0) {
202 // Read the SizeOfData(16) and PointerToRawData(24)
204 if (ReadFromFile (fp
, lvalue
+ 16, &Size
, 4) != 0) {
205 printf ("error: Size = %d\n", Size
);
209 printf ("Debug data: size = %xh, ", Size
);
210 fprintf (fpData
, "Debug data: size = %xh, ", Size
);
212 if (ReadFromFile (fp
, lvalue
+ 20, &Pointer
, 4) != 0) {
213 printf ("error: LoadOffset = %xh\n", Pointer
);
217 // printf ("LoadOffset = %xh, ", Pointer);
219 fprintf (fpData
, "LoadOffset = %xh, ", Pointer
);
221 if (ReadFromFile (fp
, lvalue
+ 24, &Pointer
, 4) != 0) {
222 printf ("error: FileOffset = %xh\n", Pointer
);
226 printf ("FileOffset = %xh, ", Pointer
);
227 fprintf (fpData
, "FileOffset = %xh, \n", Pointer
);
229 if ((lvalue
!= 0) && (Pointer
!= 0)) {
233 Buffer
= malloc (Size
+ 1);
234 if (Buffer
== NULL
) {
235 printf ("Error: Cannot allocate memory.\n");
239 // set file pointer to the specified location of file
241 if (fseek (fp
, Pointer
, SEEK_SET
) != 0) {
242 printf ("Error: Cannot move the current location of the file.\n");
247 // read data from PE file
249 if (fread (Buffer
, Size
, 1, fp
) != 1) {
250 printf ("Error: Cannot read data from the file.\n");
255 // write to data file
257 for (Index
= 0; Index
< Size
;) {
258 fprintf (fpData
, "%02x ", Buffer
[Index
]);
261 if (Index
% 8 == 0) {
262 fprintf (fpData
, "\n");
266 fprintf (fpData
, "\n");
269 // zero buffer and write back to PE file
271 if (fseek (fp
, Pointer
, SEEK_SET
) != 0) {
272 printf ("Error: Cannot move the current location of the file.\n");
277 memset (Buffer
, 0, Size
);
278 if (fwrite (Buffer
, Size
, 1, fp
) != 1) {
279 perror ("Error: Cannot write zero to the file.\n");
284 // set file pointer to the specified location of file
286 if (fseek (fp
, lvalue
+ 4, SEEK_SET
) != 0) {
287 printf ("Error: Cannot move the current location of the file.\n");
292 if (fwrite (Buffer
, 4, 1, fp
) != 1) {
293 perror ("Error: Cannot write zero to the file.\n");
314 Prints the zero debug data of the PE file to the DebugData file.
315 Executes the ZeroDebugData function.
319 argc - Standard C argument, number of command line arguments.
320 argv[] - Standard C argument, array of pointers to the input files,
321 such as the PE and DebugData files.
332 char DataFile
[1024] = "";
335 // check the number of parameters
338 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
342 // open the DebugData file, if not exists, return
345 strcpy (DataFile
, argv
[2]);
347 strcpy (DataFile
, "DebugData.dat");
350 fpData
= fopen (DataFile
, "a+");
351 if (fpData
== NULL
) {
352 fpData
= fopen (DataFile
, "w");
353 if (fpData
== NULL
) {
354 printf ("Error: Cannot open the data file!\n");
361 fp
= fopen (argv
[1], "r+b");
363 printf ("Error: Cannot open the PE file!\n");
367 // Zero the Debug Data to the PE file
369 printf ("Zero Debug Data to file %s:\n", argv
[1]);
370 fprintf (fpData
, "\nZero Debug Data to file %s:\n", argv
[1]);
371 if ((int *) ZeroDebugData (fp
, fpData
) != 0) {
372 printf ("Error: Zero Debug Data PE file\n");
377 printf (" success\n");