]>
git.proxmox.com Git - mirror_edk2.git/blob - EdkCompatibilityPkg/Sample/Tools/Source/SetStamp/SetStamp.c
677797381027c04a241e7369ae5e94ae229991de
3 Copyright (c) 2004 - 2010, Intel Corporation. All rights reserved.<BR>
4 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
25 #define UTILITY_NAME "SetStamp"
26 #define UTILITY_VERSION "v1.0"
33 print usage of setstamp command
44 UTILITY_NAME
" "UTILITY_VERSION
" - Intel Set Time Stamp Utility",
45 " Copyright (C), 2004 - 2008 Intel Corporation",
47 #if ( defined(UTILITY_BUILD) && defined(UTILITY_VENDOR) )
48 " Built from "UTILITY_BUILD
", project of "UTILITY_VENDOR
,
52 " "UTILITY_NAME
" PEFILE TIMEFILE",
54 " Set Date/Time Stamp of Portable Executable (PE) format file",
57 for (Index
= 0; Str
[Index
] != NULL
; Index
++) {
58 fprintf (stdout
, "%s\n", Str
[Index
]);
70 Read the date and time from TIME file. If the date/time string is
71 "NOW NOW", write the current date and time to TIME file and set it to
72 ltime. Else, set the date and time of TIME file to ltime.
75 fp - The pointer of TIME file
83 char buffer
[LINE_MAXLEN
];
87 if (fgets (buffer
, LINE_MAXLEN
, fp
) == NULL
) {
88 printf ("Error: Cannot read TIME file.\n");
92 // compare the value with "NOW NOW", write TIME file if equal
94 if (strncmp (buffer
, "NOW NOW", 7) == 0) {
96 // get system current time and date
100 now
= localtime (ltime
);
102 printf ("Error: Cannot get local time.\n");
106 if (strftime (buffer
, LINE_MAXLEN
, "%Y-%m-%d %H:%M:%S", now
) == 0) {
107 printf ("Error: Cannot format time string.\n");
113 if (fseek (fp
, 0, SEEK_SET
) != 0) {
114 printf ("Error: Cannot move location of TIME file.\n");
118 if (fputs (buffer
, fp
) == EOF
) {
119 printf ("Error: Cannot write time string to TIME file.\n");
123 // ltime has been set as current time and date, return
128 // get the date and time from buffer
140 printf ("Error: Invaild date or time!\n");
144 // in struct, Month (0 - 11; Jan = 0). So decrease 1 from it
149 // in struct, Year (current year minus 1900)
150 // and only the dates can be handled from Jan 1, 1970 to Jan 18, 2038
153 // convert 0 -> 100 (2000), 1 -> 101 (2001), ..., 38 -> 138 (2038)
155 if (stime
.tm_year
<= 38) {
156 stime
.tm_year
+= 100;
159 // convert 1970 -> 70, 2000 -> 100, ...
161 else if (stime
.tm_year
>= 1970) {
162 stime
.tm_year
-= 1900;
165 // convert the date and time to time_t format
167 *ltime
= mktime (&stime
);
168 if (*ltime
== (time_t) - 1) {
169 printf ("Error: Invalid date or time!\n");
185 read data from a specified location of file
189 offset - number of bytes from beginning of file
190 buffer - buffer used to store data
191 size - size of buffer
199 // set file pointer to the specified location of file
201 if (fseek (fp
, offset
, SEEK_SET
) != 0) {
202 printf ("Error: Cannot move the current location of the file.\n");
206 // read data from the file
208 if (fread (buffer
, size
, 1, fp
) != 1) {
209 printf ("Error: Cannot read data from the file.\n");
225 write data to a specified location of file
229 offset - number of bytes from beginning of file
230 buffer - buffer used to store data
231 size - size of buffer
239 // set file pointer to the specified location of file
241 if (fseek (fp
, offset
, SEEK_SET
) != 0) {
242 printf ("Error: Cannot move the current location of the file.\n");
246 // write data to the file
248 if (fwrite (buffer
, size
, 1, fp
) != 1) {
249 perror ("Error: Cannot write data to the file.\n");
263 set Date/Time Stamp of the file
267 ltime - time and date
274 unsigned char header
[4];
275 unsigned long offset
;
276 unsigned long NumberOfRvaAndSizes
;
278 unsigned long lvalue
;
281 // read the header of file
283 if (ReadFromFile (fp
, 0, header
, 2) != 0) {
287 // "MZ" -- the header of image file (PE)
289 if (strncmp ((char *) header
, "MZ", 2) != 0) {
290 printf ("Error: Invalid Image file.\n");
294 // At location 0x3C, the stub has the file offset to the
297 if (ReadFromFile (fp
, 0x3C, &offset
, 4) != 0) {
301 // read the header of optional
303 if (ReadFromFile (fp
, offset
, header
, 4) != 0) {
307 // "PE\0\0" -- the signature of optional header
309 if (strncmp ((char *) header
, "PE\0\0", 4) != 0) {
310 printf ("Error: Invalid PE format file.\n");
314 // Add 8 to skip PE signature (4-byte), Machine (2-byte) and
315 // NumberOfSection (2-byte)
319 if (WriteToFile (fp
, offset
, <ime
, 4) != 0) {
323 // Add 16 to skip COFF file header, and get to optional header.
328 // Check the magic field, 0x10B for PE32 and 0x20B for PE32+
330 if (ReadFromFile (fp
, offset
, &nvalue
, 2) != 0) {
334 // If this is PE32 image file, offset of NumberOfRvaAndSizes is 92.
337 switch (nvalue
& 0xFFFF) {
347 printf ("Error: Sorry! The Magic value is unknown.\n");
351 // get the value of NumberOfRvaAndSizes
353 if (ReadFromFile (fp
, offset
, &NumberOfRvaAndSizes
, 4) != 0) {
357 // Date/time stamp exists in Export Table, Import Table, Resource Table,
358 // Debug Table and Delay Import Table. And in Import Table and Delay Import
359 // Table, it will be set when bound. So here only set the date/time stamp
360 // of Export Table, Resource Table and Debug Table.
363 // change date/time stamp of Export Table, the offset of Export Table
364 // is 4 + 0 * 8 = 4. And the offset of stamp is 4.
366 if (NumberOfRvaAndSizes
>= 1) {
367 if (ReadFromFile (fp
, offset
+ 4, &lvalue
, 4) != 0) {
372 if (WriteToFile (fp
, lvalue
+ 4, <ime
, 4) != 0) {
378 // change date/time stamp of Resource Table, the offset of Resource Table
379 // is 4 + 2 * 8 = 20. And the offset of stamp is 4.
381 if (NumberOfRvaAndSizes
>= 3) {
382 if (ReadFromFile (fp
, offset
+ 20, &lvalue
, 4) != 0) {
387 if (WriteToFile (fp
, lvalue
+ 4, <ime
, 4) != 0) {
393 // change date/time stamp of Debug Table, offset of Debug Table
394 // is 4 + 6 * 8 = 52. And the offset of stamp is 4.
396 if (NumberOfRvaAndSizes
>= 7) {
397 if (ReadFromFile (fp
, offset
+ 52, &lvalue
, 4) != 0) {
402 if (WriteToFile (fp
, lvalue
+ 4, <ime
, 4) != 0) {
407 // change the date/time stamp of Debug Data
409 if (ReadFromFile (fp
, lvalue
+ 24, &lvalue
, 4) != 0) {
413 // get the signature of debug data
415 if (ReadFromFile (fp
, lvalue
, header
, 2) != 0) {
419 // "NB" - the signature of Debug Data
420 // Need Review: (From Spec. is "NB05", From .dll is "NB10")
422 if (strncmp ((char *) header
, "NB", 2) == 0) {
423 if (WriteToFile (fp
, lvalue
+ 8, <ime
, 4) != 0) {
442 // check the number of parameters
449 // open the TIME file, if not exists, return
451 fp
= fopen (argv
[2], "r+");
456 // get time and date from file
458 if (GetDateTime (fp
, <ime
) != 0) {
463 // close the TIME file
470 fp
= fopen (argv
[1], "r+b");
472 printf ("Error: Cannot open the PE file!\n");
476 // set time and date stamp to the PE file
478 if (SetStamp (fp
, ltime
) != 0) {
483 printf ("Set Date/Time Stamp to %s", ctime (<ime
));