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