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