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