]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/CCode/Source/ZeroDebugData/ZeroDebugData.c
- Fixed EDKT513 by adding existing section files into the dependency check of genffsf...
[mirror_edk2.git] / Tools / CCode / Source / ZeroDebugData / ZeroDebugData.c
CommitLineData
d25c4bf0 1/*++\r
2\r
cf171110 3Copyright (c) 2004-2006 Intel Corporation. All rights reserved\r
56fe62ce 4This program and the accompanying materials are licensed and made available\r
5under the terms and conditions of the BSD License which accompanies this\r
6distribution. The full text of the license may be found at\r
7http://opensource.org/licenses/bsd-license.php\r
8\r
9THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
10WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
11\r
d25c4bf0 12\r
d25c4bf0 13Module Name:\r
56fe62ce 14\r
d25c4bf0 15 ZeroDebugData.c\r
16\r
17Abstract:\r
56fe62ce 18\r
d25c4bf0 19 Zero the Debug Data Fields of Portable Executable (PE) format file.\r
20\r
21--*/\r
22\r
23#include <stdio.h>\r
24#include <string.h>\r
25#include <stdlib.h>\r
d25c4bf0 26\r
26355eed 27#define UTILITY_NAME "GenTEImage"\r
28#define UTILITY_MAJOR_VERSION 0\r
29#define UTILITY_MINOR_VERSION 1\r
30\r
31void\r
32Version (\r
33 void\r
34 )\r
35/*++\r
36Routine Description:\r
37 print version information for this utility\r
38\r
39Arguments:\r
40 None\r
41\r
42Returns:\r
43 None\r
44--*/\r
45// GC_TODO: void - add argument and description to function comment\r
46{\r
47 //\r
48 // print usage of command\r
49 //\r
50 printf ("%s v%d.%d -Utility to zero the Debug Data Fields of Portable Executable (PE) format file.\n", UTILITY_NAME, UTILITY_MAJOR_VERSION, UTILITY_MINOR_VERSION);\r
51 printf ("Copyright (c) 1999-2007 Intel Corporation. All rights reserved.\n");\r
52}\r
53\r
d25c4bf0 54void\r
26355eed 55Usage (\r
d25c4bf0 56 void\r
57 )\r
58/*++\r
59Routine Description:\r
60 print usage of ZeroDebugData command\r
61\r
62Arguments:\r
63 None\r
64\r
65Returns:\r
66 None\r
67--*/\r
68// GC_TODO: void - add argument and description to function comment\r
69{\r
26355eed 70 Version();\r
d25c4bf0 71 //\r
72 // print usage of command\r
73 //\r
74 printf ("\nUsage: ZeroDebugData <PE-File> [DebugData-File]\n");\r
75}\r
76\r
77int\r
78ReadFromFile (\r
79 FILE *fp,\r
80 long offset,\r
81 void *buffer,\r
82 int size\r
83 )\r
84/*++\r
85Routine Description:\r
86 read data from a specified location of file\r
87\r
88Arguments:\r
89 fp - file pointer\r
90 offset - number of bytes from beginning of file\r
91 buffer - buffer used to store data\r
92 size - size of buffer\r
93\r
94Returns:\r
95 = 0 - Success\r
96 = -1 - Failed\r
97--*/\r
98{\r
99 //\r
100 // set file pointer to the specified location of file\r
101 //\r
102 if (fseek (fp, offset, SEEK_SET) != 0) {\r
103 printf ("Error: Cannot move the current location of the file.\n");\r
104 return -1;\r
105 }\r
106 //\r
107 // read data from the file\r
108 //\r
109 if (fread (buffer, size, 1, fp) != 1) {\r
110 printf ("Error: Cannot read data from the file.\n");\r
111 return -1;\r
112 }\r
113\r
114 return 0;\r
115}\r
116\r
117int\r
118ZeroDebugData (\r
119 FILE *fp,\r
120 FILE *fpData\r
121 )\r
122/*++\r
123\r
124Routine Description:\r
125\r
126 Zero the debug data fields of the file\r
127\r
128Arguments:\r
129\r
130 fp - file pointer\r
131 fpData - pointer to output file that ZeroDebugData progress is written to\r
132\r
133Returns:\r
134\r
135 = 0 - Success\r
136 = -1 - Failed\r
137\r
138--*/\r
139{\r
140 unsigned char header[4];\r
141 unsigned long offset;\r
142 unsigned long NumberOfRvaAndSizes;\r
143 unsigned int nvalue;\r
144 unsigned long lvalue;\r
145 unsigned long Size;\r
146 unsigned long Pointer;\r
147 unsigned char *Buffer;\r
148 unsigned long Index;\r
149\r
150 //\r
151 // read the header of file\r
152 //\r
153 if (ReadFromFile (fp, 0, header, 2) != 0) {\r
154 printf ("Error: open image file\n");\r
155 return -1;\r
156 }\r
157 //\r
158 // "MZ" -- the header of image file (PE)\r
159 //\r
160 if (strncmp ((char *) header, "MZ", 2) != 0) {\r
161 printf ("Error: Invalid Image file.\n");\r
162 return -1;\r
163 }\r
164 //\r
165 // At location 0x3C, the stub has the file offset to the\r
166 // PE signature.\r
167 //\r
168 if (ReadFromFile (fp, 0x3C, &offset, 4) != 0) {\r
169 return -1;\r
170 }\r
171 //\r
172 // read the header of optional\r
173 //\r
174 if (ReadFromFile (fp, offset, header, 4) != 0) {\r
175 return -1;\r
176 }\r
177 //\r
178 // "PE\0\0" -- the signature of optional header\r
179 //\r
180 if (strncmp ((char *) header, "PE\0\0", 4) != 0) {\r
181 printf ("Error: Invalid PE format file.\n");\r
182 return -1;\r
183 }\r
184 //\r
185 // Add 16 to skip COFF file header, and get to optional header.\r
186 //\r
187 offset += 24;\r
188\r
189 //\r
190 // Check the magic field, 0x10B for PE32 and 0x20B for PE32+\r
191 //\r
192 if (ReadFromFile (fp, offset, &nvalue, 2) != 0) {\r
193 return -1;\r
194 }\r
195 //\r
196 // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.\r
197 // Else it is 108.\r
198 //\r
199 switch (nvalue & 0xFFFF) {\r
200 case 0x10B:\r
201 offset += 92;\r
202 printf ("Info: Image is PE32. ");\r
203 break;\r
204\r
205 case 0x20B:\r
206 offset += 108;\r
207 printf ("Info: Image is PE32+. ");\r
208 break;\r
209\r
210 default:\r
211 printf ("Error: Magic value is unknown.\n");\r
212 return -1;\r
213 }\r
214 //\r
215 // get the value of NumberOfRvaAndSizes\r
216 //\r
217 if (ReadFromFile (fp, offset, &NumberOfRvaAndSizes, 4) != 0) {\r
218 printf ("Error: read NumberOfRvaAndSizes error.\n");\r
219 return -1;\r
220 }\r
221 //\r
222 // printf ("Info: NumberOfRvaAndSizes = %d\n", NumberOfRvaAndSizes);\r
223 //\r
224 //\r
225 // Finding Debug Table, offset of Debug Table\r
226 // is 4 + 6 * 8 = 52.\r
227 //\r
228 if (NumberOfRvaAndSizes >= 7) {\r
229 if (ReadFromFile (fp, offset + 52, &lvalue, 4) != 0) {\r
230 return -1;\r
231 }\r
232 //\r
233 // Read the SizeOfData(16) and PointerToRawData(24)\r
234 //\r
235 if (ReadFromFile (fp, lvalue + 16, &Size, 4) != 0) {\r
236 printf ("error: Size = %d\n", Size);\r
237 return -1;\r
238 }\r
239\r
240 printf ("Debug data: size = %xh, ", Size);\r
241 fprintf (fpData, "Debug data: size = %xh, ", Size);\r
242\r
243 if (ReadFromFile (fp, lvalue + 20, &Pointer, 4) != 0) {\r
244 printf ("error: LoadOffset = %xh\n", Pointer);\r
245 return -1;\r
246 }\r
247 //\r
248 // printf ("LoadOffset = %xh, ", Pointer);\r
249 //\r
250 fprintf (fpData, "LoadOffset = %xh, ", Pointer);\r
251\r
252 if (ReadFromFile (fp, lvalue + 24, &Pointer, 4) != 0) {\r
253 printf ("error: FileOffset = %xh\n", Pointer);\r
254 return -1;\r
255 }\r
256\r
257 printf ("FileOffset = %xh, ", Pointer);\r
258 fprintf (fpData, "FileOffset = %xh, \n", Pointer);\r
259\r
260 if ((lvalue != 0) && (Pointer != 0)) {\r
261 //\r
262 // prepare buffer\r
263 //\r
264 Buffer = malloc (Size + 1);\r
265 if (Buffer == NULL) {\r
266 printf ("Error: Cannot allocate memory.\n");\r
267 return -1;\r
268 }\r
269 //\r
270 // set file pointer to the specified location of file\r
271 //\r
272 if (fseek (fp, Pointer, SEEK_SET) != 0) {\r
273 printf ("Error: Cannot move the current location of the file.\n");\r
274 free (Buffer);\r
275 return -1;\r
276 }\r
277 //\r
278 // read data from PE file\r
279 //\r
280 if (fread (Buffer, Size, 1, fp) != 1) {\r
281 printf ("Error: Cannot read data from the file.\n");\r
282 free (Buffer);\r
283 return -1;\r
284 }\r
285 //\r
286 // write to data file\r
287 //\r
288 for (Index = 0; Index < Size;) {\r
289 fprintf (fpData, "%02x ", Buffer[Index]);\r
290\r
291 Index++;\r
292 if (Index % 8 == 0) {\r
293 fprintf (fpData, "\n");\r
294 }\r
295 }\r
296\r
297 fprintf (fpData, "\n");\r
298\r
299 //\r
300 // zero buffer and write back to PE file\r
301 //\r
302 if (fseek (fp, Pointer, SEEK_SET) != 0) {\r
303 printf ("Error: Cannot move the current location of the file.\n");\r
304 free (Buffer);\r
305 return -1;\r
306 }\r
307\r
308 memset (Buffer, 0, Size);\r
309 if (fwrite (Buffer, Size, 1, fp) != 1) {\r
310 perror ("Error: Cannot write zero to the file.\n");\r
311 free (Buffer);\r
312 return -1;\r
313 }\r
314 //\r
315 // set file pointer to the specified location of file\r
316 //\r
317 if (fseek (fp, lvalue + 4, SEEK_SET) != 0) {\r
318 printf ("Error: Cannot move the current location of the file.\n");\r
319 free (Buffer);\r
320 return -1;\r
321 }\r
322\r
323 if (fwrite (Buffer, 4, 1, fp) != 1) {\r
324 perror ("Error: Cannot write zero to the file.\n");\r
325 free (Buffer);\r
326 return -1;\r
327 }\r
328\r
329 free (Buffer);\r
330 }\r
331 }\r
332\r
333 return 0;\r
334}\r
335\r
336int\r
337main (\r
338 int argc,\r
339 char *argv[]\r
340 )\r
341/*++\r
342\r
343Routine Description:\r
344\r
345 Prints the zero debug data of the PE file to the DebugData file.\r
346 Executes the ZeroDebugData function.\r
347\r
348Arguments:\r
349\r
350 argc - Standard C argument, number of command line arguments.\r
351 argv[] - Standard C argument, array of pointers to the input files,\r
352 such as the PE and DebugData files.\r
353\r
354Returns:\r
355\r
356 zero - success\r
357 nonzero - failure\r
358\r
359--*/\r
360{\r
361 FILE *fp;\r
362 FILE *fpData;\r
363 char DataFile[1024] = "";\r
364\r
26355eed 365 if (argc == 1) {\r
366 Usage();\r
d25c4bf0 367 return -1;\r
368 }\r
26355eed 369 \r
370 if ((strcmp(argv[1], "-h") == 0) || (strcmp(argv[1], "--help") == 0) ||\r
371 (strcmp(argv[1], "-?") == 0) || (strcmp(argv[1], "/?") == 0)) {\r
372 Usage();\r
373 return -1;\r
374 }\r
375 \r
376 if ((strcmp(argv[1], "-V") == 0) || (strcmp(argv[1], "--version") == 0)) {\r
377 Version();\r
378 return -1;\r
379 }\r
380\r
381 if (argc == 2) {\r
382 Usage();\r
383 return -1;\r
384 }\r
385\r
d25c4bf0 386 //\r
387 // open the DebugData file, if not exists, return\r
388 //\r
389 if (argc >= 3) {\r
390 strcpy (DataFile, argv[2]);\r
391 } else {\r
392 strcpy (DataFile, "DebugData.dat");\r
393 }\r
394\r
395 fpData = fopen (DataFile, "a+");\r
396 if (fpData == NULL) {\r
397 fpData = fopen (DataFile, "w");\r
398 if (fpData == NULL) {\r
399 printf ("Error: Cannot open the data file!\n");\r
400 return -1;\r
401 }\r
402 }\r
403 //\r
404 // open the PE file\r
405 //\r
406 fp = fopen (argv[1], "r+b");\r
407 if (fp == NULL) {\r
408 printf ("Error: Cannot open the PE file!\n");\r
409 return -1;\r
410 }\r
411 //\r
412 // Zero the Debug Data to the PE file\r
413 //\r
414 printf ("Zero Debug Data to file %s:\n", argv[1]);\r
415 fprintf (fpData, "\nZero Debug Data to file %s:\n", argv[1]);\r
416 if ((int *) ZeroDebugData (fp, fpData) != 0) {\r
417 printf ("Error: Zero Debug Data PE file\n");\r
418 fclose (fp);\r
419 return -1;\r
420 }\r
421\r
422 printf (" success\n");\r
423\r
424 //\r
425 // close the PE file\r
426 //\r
427 fflush (fpData);\r
428 fflush (fp);\r
429 fclose (fpData);\r
430 fclose (fp);\r
431\r
432 return 0;\r
433}\r