]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/ZeroDebugData/ZeroDebugData.c
Porting several more tools to edk2.
[mirror_edk2.git] / Tools / Source / TianoTools / ZeroDebugData / ZeroDebugData.c
1 /*++
2
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
10 Intel Corporation.
11
12
13 Module Name:
14 ZeroDebugData.c
15
16 Abstract:
17 Zero the Debug Data Fields of Portable Executable (PE) format file.
18
19 --*/
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 void
26 PrintUsage (
27 void
28 )
29 /*++
30 Routine Description:
31 print usage of ZeroDebugData command
32
33 Arguments:
34 None
35
36 Returns:
37 None
38 --*/
39 // GC_TODO: void - add argument and description to function comment
40 {
41 //
42 // print usage of command
43 //
44 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
45 }
46
47 int
48 ReadFromFile (
49 FILE *fp,
50 long offset,
51 void *buffer,
52 int size
53 )
54 /*++
55 Routine Description:
56 read data from a specified location of file
57
58 Arguments:
59 fp - file pointer
60 offset - number of bytes from beginning of file
61 buffer - buffer used to store data
62 size - size of buffer
63
64 Returns:
65 = 0 - Success
66 = -1 - Failed
67 --*/
68 {
69 //
70 // set file pointer to the specified location of file
71 //
72 if (fseek (fp, offset, SEEK_SET) != 0) {
73 printf ("Error: Cannot move the current location of the file.\n");
74 return -1;
75 }
76 //
77 // read data from the file
78 //
79 if (fread (buffer, size, 1, fp) != 1) {
80 printf ("Error: Cannot read data from the file.\n");
81 return -1;
82 }
83
84 return 0;
85 }
86
87 int
88 ZeroDebugData (
89 FILE *fp,
90 FILE *fpData
91 )
92 /*++
93
94 Routine Description:
95
96 Zero the debug data fields of the file
97
98 Arguments:
99
100 fp - file pointer
101 fpData - pointer to output file that ZeroDebugData progress is written to
102
103 Returns:
104
105 = 0 - Success
106 = -1 - Failed
107
108 --*/
109 {
110 unsigned char header[4];
111 unsigned long offset;
112 unsigned long NumberOfRvaAndSizes;
113 unsigned int nvalue;
114 unsigned long lvalue;
115 unsigned long Size;
116 unsigned long Pointer;
117 unsigned char *Buffer;
118 unsigned long Index;
119
120 //
121 // read the header of file
122 //
123 if (ReadFromFile (fp, 0, header, 2) != 0) {
124 printf ("Error: open image file\n");
125 return -1;
126 }
127 //
128 // "MZ" -- the header of image file (PE)
129 //
130 if (strncmp ((char *) header, "MZ", 2) != 0) {
131 printf ("Error: Invalid Image file.\n");
132 return -1;
133 }
134 //
135 // At location 0x3C, the stub has the file offset to the
136 // PE signature.
137 //
138 if (ReadFromFile (fp, 0x3C, &offset, 4) != 0) {
139 return -1;
140 }
141 //
142 // read the header of optional
143 //
144 if (ReadFromFile (fp, offset, header, 4) != 0) {
145 return -1;
146 }
147 //
148 // "PE\0\0" -- the signature of optional header
149 //
150 if (strncmp ((char *) header, "PE\0\0", 4) != 0) {
151 printf ("Error: Invalid PE format file.\n");
152 return -1;
153 }
154 //
155 // Add 16 to skip COFF file header, and get to optional header.
156 //
157 offset += 24;
158
159 //
160 // Check the magic field, 0x10B for PE32 and 0x20B for PE32+
161 //
162 if (ReadFromFile (fp, offset, &nvalue, 2) != 0) {
163 return -1;
164 }
165 //
166 // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.
167 // Else it is 108.
168 //
169 switch (nvalue & 0xFFFF) {
170 case 0x10B:
171 offset += 92;
172 printf ("Info: Image is PE32. ");
173 break;
174
175 case 0x20B:
176 offset += 108;
177 printf ("Info: Image is PE32+. ");
178 break;
179
180 default:
181 printf ("Error: Magic value is unknown.\n");
182 return -1;
183 }
184 //
185 // get the value of NumberOfRvaAndSizes
186 //
187 if (ReadFromFile (fp, offset, &NumberOfRvaAndSizes, 4) != 0) {
188 printf ("Error: read NumberOfRvaAndSizes error.\n");
189 return -1;
190 }
191 //
192 // printf ("Info: NumberOfRvaAndSizes = %d\n", NumberOfRvaAndSizes);
193 //
194 //
195 // Finding Debug Table, offset of Debug Table
196 // is 4 + 6 * 8 = 52.
197 //
198 if (NumberOfRvaAndSizes >= 7) {
199 if (ReadFromFile (fp, offset + 52, &lvalue, 4) != 0) {
200 return -1;
201 }
202 //
203 // Read the SizeOfData(16) and PointerToRawData(24)
204 //
205 if (ReadFromFile (fp, lvalue + 16, &Size, 4) != 0) {
206 printf ("error: Size = %d\n", Size);
207 return -1;
208 }
209
210 printf ("Debug data: size = %xh, ", Size);
211 fprintf (fpData, "Debug data: size = %xh, ", Size);
212
213 if (ReadFromFile (fp, lvalue + 20, &Pointer, 4) != 0) {
214 printf ("error: LoadOffset = %xh\n", Pointer);
215 return -1;
216 }
217 //
218 // printf ("LoadOffset = %xh, ", Pointer);
219 //
220 fprintf (fpData, "LoadOffset = %xh, ", Pointer);
221
222 if (ReadFromFile (fp, lvalue + 24, &Pointer, 4) != 0) {
223 printf ("error: FileOffset = %xh\n", Pointer);
224 return -1;
225 }
226
227 printf ("FileOffset = %xh, ", Pointer);
228 fprintf (fpData, "FileOffset = %xh, \n", Pointer);
229
230 if ((lvalue != 0) && (Pointer != 0)) {
231 //
232 // prepare buffer
233 //
234 Buffer = malloc (Size + 1);
235 if (Buffer == NULL) {
236 printf ("Error: Cannot allocate memory.\n");
237 return -1;
238 }
239 //
240 // set file pointer to the specified location of file
241 //
242 if (fseek (fp, Pointer, SEEK_SET) != 0) {
243 printf ("Error: Cannot move the current location of the file.\n");
244 free (Buffer);
245 return -1;
246 }
247 //
248 // read data from PE file
249 //
250 if (fread (Buffer, Size, 1, fp) != 1) {
251 printf ("Error: Cannot read data from the file.\n");
252 free (Buffer);
253 return -1;
254 }
255 //
256 // write to data file
257 //
258 for (Index = 0; Index < Size;) {
259 fprintf (fpData, "%02x ", Buffer[Index]);
260
261 Index++;
262 if (Index % 8 == 0) {
263 fprintf (fpData, "\n");
264 }
265 }
266
267 fprintf (fpData, "\n");
268
269 //
270 // zero buffer and write back to PE file
271 //
272 if (fseek (fp, Pointer, SEEK_SET) != 0) {
273 printf ("Error: Cannot move the current location of the file.\n");
274 free (Buffer);
275 return -1;
276 }
277
278 memset (Buffer, 0, Size);
279 if (fwrite (Buffer, Size, 1, fp) != 1) {
280 perror ("Error: Cannot write zero to the file.\n");
281 free (Buffer);
282 return -1;
283 }
284 //
285 // set file pointer to the specified location of file
286 //
287 if (fseek (fp, lvalue + 4, SEEK_SET) != 0) {
288 printf ("Error: Cannot move the current location of the file.\n");
289 free (Buffer);
290 return -1;
291 }
292
293 if (fwrite (Buffer, 4, 1, fp) != 1) {
294 perror ("Error: Cannot write zero to the file.\n");
295 free (Buffer);
296 return -1;
297 }
298
299 free (Buffer);
300 }
301 }
302
303 return 0;
304 }
305
306 int
307 main (
308 int argc,
309 char *argv[]
310 )
311 /*++
312
313 Routine Description:
314
315 Prints the zero debug data of the PE file to the DebugData file.
316 Executes the ZeroDebugData function.
317
318 Arguments:
319
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.
323
324 Returns:
325
326 zero - success
327 nonzero - failure
328
329 --*/
330 {
331 FILE *fp;
332 FILE *fpData;
333 char DataFile[1024] = "";
334
335 //
336 // check the number of parameters
337 //
338 if (argc < 2) {
339 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");
340 return -1;
341 }
342 //
343 // open the DebugData file, if not exists, return
344 //
345 if (argc >= 3) {
346 strcpy (DataFile, argv[2]);
347 } else {
348 strcpy (DataFile, "DebugData.dat");
349 }
350
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");
356 return -1;
357 }
358 }
359 //
360 // open the PE file
361 //
362 fp = fopen (argv[1], "r+b");
363 if (fp == NULL) {
364 printf ("Error: Cannot open the PE file!\n");
365 return -1;
366 }
367 //
368 // Zero the Debug Data to the PE file
369 //
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");
374 fclose (fp);
375 return -1;
376 }
377
378 printf (" success\n");
379
380 //
381 // close the PE file
382 //
383 fflush (fpData);
384 fflush (fp);
385 fclose (fpData);
386 fclose (fp);
387
388 return 0;
389 }