]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/TianoTools/SetStamp/SetStamp.c
3 Copyright (c) 2004, Intel Corporation
4 All rights reserved. This program and the accompanying materials
5 are licensed and made available under the terms and conditions of the BSD License
6 which accompanies this distribution. The full text of the license may be found at
7 http://opensource.org/licenses/bsd-license.php
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.
16 Set Date/Time Stamp of Portable Executable (PE) format file
24 #define LINE_MAXLEN 80
32 print usage of setstamp command
42 // print usage of command
44 printf ("Usage: SetStamp <PE-File> <TIME-File>\n");
54 Read the date and time from TIME file. If the date/time string is
55 "NOW NOW", write the current date and time to TIME file and set it to
56 ltime. Else, set the date and time of TIME file to ltime.
59 fp - The pointer of TIME file
67 char buffer
[LINE_MAXLEN
];
71 if (fgets (buffer
, LINE_MAXLEN
, fp
) == NULL
) {
72 printf ("Error: Cannot read TIME file.\n");
76 // compare the value with "NOW NOW", write TIME file if equal
78 if (strncmp (buffer
, "NOW NOW", 7) == 0) {
80 // get system current time and date
84 now
= localtime (ltime
);
86 printf ("Error: Cannot get local time.\n");
90 if (strftime (buffer
, LINE_MAXLEN
, "%Y-%m-%d %H:%M:%S", now
) == 0) {
91 printf ("Error: Cannot format time string.\n");
97 if (fseek (fp
, 0, SEEK_SET
) != 0) {
98 printf ("Error: Cannot move location of TIME file.\n");
102 if (fputs (buffer
, fp
) == EOF
) {
103 printf ("Error: Cannot write time string to TIME file.\n");
107 // ltime has been set as current time and date, return
112 // get the date and time from buffer
124 printf ("Error: Invaild date or time!\n");
128 // in struct, Month (0 - 11; Jan = 0). So decrease 1 from it
133 // in struct, Year (current year minus 1900)
134 // and only the dates can be handled from Jan 1, 1970 to Jan 18, 2038
137 // convert 0 -> 100 (2000), 1 -> 101 (2001), ..., 38 -> 138 (2038)
139 if (stime
.tm_year
<= 38) {
140 stime
.tm_year
+= 100;
143 // convert 1970 -> 70, 2000 -> 100, ...
145 else if (stime
.tm_year
>= 1970) {
146 stime
.tm_year
-= 1900;
149 // convert the date and time to time_t format
151 *ltime
= mktime (&stime
);
152 if (*ltime
== (time_t) - 1) {
153 printf ("Error: Invalid date or time!\n");
169 read data from a specified location of file
173 offset - number of bytes from beginning of file
174 buffer - buffer used to store data
175 size - size of buffer
183 // set file pointer to the specified location of file
185 if (fseek (fp
, offset
, SEEK_SET
) != 0) {
186 printf ("Error: Cannot move the current location of the file.\n");
190 // read data from the file
192 if (fread (buffer
, size
, 1, fp
) != 1) {
193 printf ("Error: Cannot read data from the file.\n");
209 write data to a specified location of file
213 offset - number of bytes from beginning of file
214 buffer - buffer used to store data
215 size - size of buffer
223 // set file pointer to the specified location of file
225 if (fseek (fp
, offset
, SEEK_SET
) != 0) {
226 printf ("Error: Cannot move the current location of the file.\n");
230 // write data to the file
232 if (fwrite (buffer
, size
, 1, fp
) != 1) {
233 perror ("Error: Cannot write data to the file.\n");
247 set Date/Time Stamp of the file
251 ltime - time and date
258 unsigned char header
[4];
259 unsigned long offset
;
260 unsigned long NumberOfRvaAndSizes
;
262 unsigned long lvalue
;
265 // read the header of file
267 if (ReadFromFile (fp
, 0, header
, 2) != 0) {
271 // "MZ" -- the header of image file (PE)
273 if (strncmp ((char *) header
, "MZ", 2) != 0) {
274 printf ("Error: Invalid Image file.\n");
278 // At location 0x3C, the stub has the file offset to the
281 if (ReadFromFile (fp
, 0x3C, &offset
, 4) != 0) {
285 // read the header of optional
287 if (ReadFromFile (fp
, offset
, header
, 4) != 0) {
291 // "PE\0\0" -- the signature of optional header
293 if (strncmp ((char *) header
, "PE\0\0", 4) != 0) {
294 printf ("Error: Invalid PE format file.\n");
298 // Add 8 to skip PE signature (4-byte), Machine (2-byte) and
299 // NumberOfSection (2-byte)
303 if (WriteToFile (fp
, offset
, <ime
, 4) != 0) {
307 // Add 16 to skip COFF file header, and get to optional header.
312 // Check the magic field, 0x10B for PE32 and 0x20B for PE32+
314 if (ReadFromFile (fp
, offset
, &nvalue
, 2) != 0) {
318 // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.
321 switch (nvalue
& 0xFFFF) {
331 printf ("Error: Sorry! The Magic value is unknown.\n");
335 // get the value of NumberOfRvaAndSizes
337 if (ReadFromFile (fp
, offset
, &NumberOfRvaAndSizes
, 4) != 0) {
341 // Date/time stamp exists in Export Table, Import Table, Resource Table,
342 // Debug Table and Delay Import Table. And in Import Table and Delay Import
343 // Table, it will be set when bound. So here only set the date/time stamp
344 // of Export Table, Resource Table and Debug Table.
347 // change date/time stamp of Export Table, the offset of Export Table
348 // is 4 + 0 * 8 = 4. And the offset of stamp is 4.
350 if (NumberOfRvaAndSizes
>= 1) {
351 if (ReadFromFile (fp
, offset
+ 4, &lvalue
, 4) != 0) {
356 if (WriteToFile (fp
, lvalue
+ 4, <ime
, 4) != 0) {
362 // change date/time stamp of Resource Table, the offset of Resource Table
363 // is 4 + 2 * 8 = 20. And the offset of stamp is 4.
365 if (NumberOfRvaAndSizes
>= 3) {
366 if (ReadFromFile (fp
, offset
+ 20, &lvalue
, 4) != 0) {
371 if (WriteToFile (fp
, lvalue
+ 4, <ime
, 4) != 0) {
377 // change date/time stamp of Debug Table, offset of Debug Table
378 // is 4 + 6 * 8 = 52. And the offset of stamp is 4.
380 if (NumberOfRvaAndSizes
>= 7) {
381 if (ReadFromFile (fp
, offset
+ 52, &lvalue
, 4) != 0) {
386 if (WriteToFile (fp
, lvalue
+ 4, <ime
, 4) != 0) {
391 // change the date/time stamp of Debug Data
393 if (ReadFromFile (fp
, lvalue
+ 24, &lvalue
, 4) != 0) {
397 // get the signature of debug data
399 if (ReadFromFile (fp
, lvalue
, header
, 2) != 0) {
403 // "NB" - the signature of Debug Data
404 // Need Review: (From Spec. is "NB05", From .dll is "NB10")
406 if (strncmp ((char *) header
, "NB", 2) == 0) {
407 if (WriteToFile (fp
, lvalue
+ 8, <ime
, 4) != 0) {
426 // check the number of parameters
433 // open the TIME file, if not exists, return
435 fp
= fopen (argv
[2], "r+");
440 // get time and date from file
442 if (GetDateTime (fp
, <ime
) != 0) {
447 // close the TIME file
454 fp
= fopen (argv
[1], "r+b");
456 printf ("Error: Cannot open the PE file!\n");
460 // set time and date stamp to the PE file
462 if (SetStamp (fp
, ltime
) != 0) {
467 printf ("Set Date/Time Stamp to %s", ctime (<ime
));