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