]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/ZeroDebugData/ZeroDebugData.c
3 Copyright (c) 2001 - 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
17 Zero the Debug Data Fields of Portable Executable (PE) format file.
31 print usage of ZeroDebugData command
39 // GC_TODO: void - add argument and description to function comment
42 // print usage of command
44 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
56 read data from a specified location of file
60 offset - number of bytes from beginning of file
61 buffer - buffer used to store data
70 // set file pointer to the specified location of file
72 if (fseek (fp
, offset
, SEEK_SET
) != 0) {
73 printf ("Error: Cannot move the current location of the file.\n");
77 // read data from the file
79 if (fread (buffer
, size
, 1, fp
) != 1) {
80 printf ("Error: Cannot read data from the file.\n");
96 Zero the debug data fields of the file
101 fpData - pointer to output file that ZeroDebugData progress is written to
110 unsigned char header
[4];
111 unsigned long offset
;
112 unsigned long NumberOfRvaAndSizes
;
114 unsigned long lvalue
;
116 unsigned long Pointer
;
117 unsigned char *Buffer
;
121 // read the header of file
123 if (ReadFromFile (fp
, 0, header
, 2) != 0) {
124 printf ("Error: open image file\n");
128 // "MZ" -- the header of image file (PE)
130 if (strncmp ((char *) header
, "MZ", 2) != 0) {
131 printf ("Error: Invalid Image file.\n");
135 // At location 0x3C, the stub has the file offset to the
138 if (ReadFromFile (fp
, 0x3C, &offset
, 4) != 0) {
142 // read the header of optional
144 if (ReadFromFile (fp
, offset
, header
, 4) != 0) {
148 // "PE\0\0" -- the signature of optional header
150 if (strncmp ((char *) header
, "PE\0\0", 4) != 0) {
151 printf ("Error: Invalid PE format file.\n");
155 // Add 16 to skip COFF file header, and get to optional header.
160 // Check the magic field, 0x10B for PE32 and 0x20B for PE32+
162 if (ReadFromFile (fp
, offset
, &nvalue
, 2) != 0) {
166 // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.
169 switch (nvalue
& 0xFFFF) {
172 printf ("Info: Image is PE32. ");
177 printf ("Info: Image is PE32+. ");
181 printf ("Error: Magic value is unknown.\n");
185 // get the value of NumberOfRvaAndSizes
187 if (ReadFromFile (fp
, offset
, &NumberOfRvaAndSizes
, 4) != 0) {
188 printf ("Error: read NumberOfRvaAndSizes error.\n");
192 // printf ("Info: NumberOfRvaAndSizes = %d\n", NumberOfRvaAndSizes);
195 // Finding Debug Table, offset of Debug Table
196 // is 4 + 6 * 8 = 52.
198 if (NumberOfRvaAndSizes
>= 7) {
199 if (ReadFromFile (fp
, offset
+ 52, &lvalue
, 4) != 0) {
203 // Read the SizeOfData(16) and PointerToRawData(24)
205 if (ReadFromFile (fp
, lvalue
+ 16, &Size
, 4) != 0) {
206 printf ("error: Size = %d\n", Size
);
210 printf ("Debug data: size = %xh, ", Size
);
211 fprintf (fpData
, "Debug data: size = %xh, ", Size
);
213 if (ReadFromFile (fp
, lvalue
+ 20, &Pointer
, 4) != 0) {
214 printf ("error: LoadOffset = %xh\n", Pointer
);
218 // printf ("LoadOffset = %xh, ", Pointer);
220 fprintf (fpData
, "LoadOffset = %xh, ", Pointer
);
222 if (ReadFromFile (fp
, lvalue
+ 24, &Pointer
, 4) != 0) {
223 printf ("error: FileOffset = %xh\n", Pointer
);
227 printf ("FileOffset = %xh, ", Pointer
);
228 fprintf (fpData
, "FileOffset = %xh, \n", Pointer
);
230 if ((lvalue
!= 0) && (Pointer
!= 0)) {
234 Buffer
= malloc (Size
+ 1);
235 if (Buffer
== NULL
) {
236 printf ("Error: Cannot allocate memory.\n");
240 // set file pointer to the specified location of file
242 if (fseek (fp
, Pointer
, SEEK_SET
) != 0) {
243 printf ("Error: Cannot move the current location of the file.\n");
248 // read data from PE file
250 if (fread (Buffer
, Size
, 1, fp
) != 1) {
251 printf ("Error: Cannot read data from the file.\n");
256 // write to data file
258 for (Index
= 0; Index
< Size
;) {
259 fprintf (fpData
, "%02x ", Buffer
[Index
]);
262 if (Index
% 8 == 0) {
263 fprintf (fpData
, "\n");
267 fprintf (fpData
, "\n");
270 // zero buffer and write back to PE file
272 if (fseek (fp
, Pointer
, SEEK_SET
) != 0) {
273 printf ("Error: Cannot move the current location of the file.\n");
278 memset (Buffer
, 0, Size
);
279 if (fwrite (Buffer
, Size
, 1, fp
) != 1) {
280 perror ("Error: Cannot write zero to the file.\n");
285 // set file pointer to the specified location of file
287 if (fseek (fp
, lvalue
+ 4, SEEK_SET
) != 0) {
288 printf ("Error: Cannot move the current location of the file.\n");
293 if (fwrite (Buffer
, 4, 1, fp
) != 1) {
294 perror ("Error: Cannot write zero to the file.\n");
315 Prints the zero debug data of the PE file to the DebugData file.
316 Executes the ZeroDebugData function.
320 argc - Standard C argument, number of command line arguments.
321 argv[] - Standard C argument, array of pointers to the input files,
322 such as the PE and DebugData files.
333 char DataFile
[1024] = "";
336 // check the number of parameters
339 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
343 // open the DebugData file, if not exists, return
346 strcpy (DataFile
, argv
[2]);
348 strcpy (DataFile
, "DebugData.dat");
351 fpData
= fopen (DataFile
, "a+");
352 if (fpData
== NULL
) {
353 fpData
= fopen (DataFile
, "w");
354 if (fpData
== NULL
) {
355 printf ("Error: Cannot open the data file!\n");
362 fp
= fopen (argv
[1], "r+b");
364 printf ("Error: Cannot open the PE file!\n");
368 // Zero the Debug Data to the PE file
370 printf ("Zero Debug Data to file %s:\n", argv
[1]);
371 fprintf (fpData
, "\nZero Debug Data to file %s:\n", argv
[1]);
372 if ((int *) ZeroDebugData (fp
, fpData
) != 0) {
373 printf ("Error: Zero Debug Data PE file\n");
378 printf (" success\n");