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