]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
CorebootPayloadPkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / CryptoPkg / Library / BaseCryptLib / SysCall / TimerWrapper.c
CommitLineData
97f98500
HT
1/** @file\r
2 C Run-Time Libraries (CRT) Time Management Routines Wrapper Implementation\r
3 for OpenSSL-based Cryptographic Library (used in DXE & RUNTIME).\r
4\r
ab187ae2 5Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r
97f98500
HT
6This program and the accompanying materials\r
7are licensed and made available under the terms and conditions of the BSD License\r
8which accompanies this distribution. The full text of the license may be found at\r
9http://opensource.org/licenses/bsd-license.php\r
10\r
11THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13\r
14**/\r
15\r
16#include <Uefi.h>\r
fc9fa685 17#include <CrtLibSupport.h>\r
97f98500
HT
18#include <Library/UefiRuntimeServicesTableLib.h>\r
19\r
20//\r
21// -- Time Management Routines --\r
22//\r
23\r
24#define IsLeap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))\r
b7d320f8 25#define SECSPERMIN (60)\r
97f98500
HT
26#define SECSPERHOUR (60 * 60)\r
27#define SECSPERDAY (24 * SECSPERHOUR)\r
28\r
29//\r
30// The arrays give the cumulative number of days up to the first of the\r
31// month number used as the index (1 -> 12) for regular and leap years.\r
32// The value at index 13 is for the whole year.\r
33//\r
34UINTN CumulativeDays[2][14] = {\r
35 {\r
36 0,\r
37 0,\r
38 31,\r
39 31 + 28,\r
40 31 + 28 + 31,\r
41 31 + 28 + 31 + 30,\r
42 31 + 28 + 31 + 30 + 31,\r
43 31 + 28 + 31 + 30 + 31 + 30,\r
44 31 + 28 + 31 + 30 + 31 + 30 + 31,\r
45 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,\r
46 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r
47 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r
48 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r
49 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r
50 },\r
51 {\r
52 0,\r
53 0,\r
54 31,\r
55 31 + 29,\r
56 31 + 29 + 31,\r
57 31 + 29 + 31 + 30,\r
58 31 + 29 + 31 + 30 + 31,\r
59 31 + 29 + 31 + 30 + 31 + 30,\r
60 31 + 29 + 31 + 30 + 31 + 30 + 31,\r
61 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,\r
62 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r
63 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r
64 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r
630f67dd 65 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r
97f98500
HT
66 }\r
67};\r
68\r
69/* Get the system time as seconds elapsed since midnight, January 1, 1970. */\r
70//INTN time(\r
71// INTN *timer\r
72// )\r
73time_t time (time_t *timer)\r
74{\r
ab187ae2
LQ
75 EFI_STATUS Status;\r
76 EFI_TIME Time;\r
77 time_t CalTime;\r
78 UINTN Year;\r
97f98500
HT
79\r
80 //\r
81 // Get the current time and date information\r
82 //\r
ab187ae2
LQ
83 Status = gRT->GetTime (&Time, NULL);\r
84 if (EFI_ERROR (Status) || (Time.Year < 1970)) {\r
85 return 0;\r
86 }\r
97f98500
HT
87\r
88 //\r
89 // Years Handling\r
90 // UTime should now be set to 00:00:00 on Jan 1 of the current year.\r
91 //\r
5e2318dd
JW
92 for (Year = 1970, CalTime = 0; Year != Time.Year; Year++) {\r
93 CalTime = CalTime + (time_t)(CumulativeDays[IsLeap(Year)][13] * SECSPERDAY);\r
97f98500
HT
94 }\r
95\r
96 //\r
97 // Add in number of seconds for current Month, Day, Hour, Minute, Seconds, and TimeZone adjustment\r
98 //\r
630f67dd 99 CalTime = CalTime +\r
5e2318dd 100 (time_t)((Time.TimeZone != EFI_UNSPECIFIED_TIMEZONE) ? (Time.TimeZone * 60) : 0) +\r
630f67dd
LG
101 (time_t)(CumulativeDays[IsLeap(Time.Year)][Time.Month] * SECSPERDAY) +\r
102 (time_t)(((Time.Day > 0) ? Time.Day - 1 : 0) * SECSPERDAY) +\r
103 (time_t)(Time.Hour * SECSPERHOUR) +\r
104 (time_t)(Time.Minute * 60) +\r
5e2318dd
JW
105 (time_t)Time.Second;\r
106\r
107 if (timer != NULL) {\r
108 *timer = CalTime;\r
109 }\r
110\r
111 return CalTime;\r
97f98500 112}\r
b7d320f8 113\r
114//\r
115// Convert a time value from type time_t to struct tm.\r
116//\r
117struct tm * gmtime (const time_t *timer)\r
118{\r
119 struct tm *GmTime;\r
120 UINT16 DayNo;\r
121 UINT16 DayRemainder;\r
122 time_t Year;\r
123 time_t YearNo;\r
124 UINT16 TotalDays;\r
125 UINT16 MonthNo;\r
126\r
127 if (timer == NULL) {\r
128 return NULL;\r
129 }\r
130\r
131 GmTime = malloc (sizeof (struct tm));\r
132 if (GmTime == NULL) {\r
133 return NULL;\r
134 }\r
135\r
136 ZeroMem ((VOID *) GmTime, (UINTN) sizeof (struct tm));\r
137\r
138 DayNo = (UINT16) (*timer / SECSPERDAY);\r
139 DayRemainder = (UINT16) (*timer % SECSPERDAY);\r
140\r
141 GmTime->tm_sec = (int) (DayRemainder % SECSPERMIN);\r
142 GmTime->tm_min = (int) ((DayRemainder % SECSPERHOUR) / SECSPERMIN);\r
143 GmTime->tm_hour = (int) (DayRemainder / SECSPERHOUR);\r
144 GmTime->tm_wday = (int) ((DayNo + 4) % 7);\r
145\r
146 for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {\r
147 TotalDays = (UINT16) (IsLeap (Year) ? 366 : 365);\r
148 if (DayNo >= TotalDays) {\r
149 DayNo = (UINT16) (DayNo - TotalDays);\r
150 YearNo++;\r
151 } else {\r
152 break;\r
153 }\r
154 }\r
155\r
156 GmTime->tm_year = (int) (YearNo + (1970 - 1900));\r
157 GmTime->tm_yday = (int) DayNo;\r
158\r
159 for (MonthNo = 12; MonthNo > 1; MonthNo--) {\r
04a3cfa7 160 if (DayNo >= CumulativeDays[IsLeap(Year)][MonthNo]) {\r
b7d320f8 161 DayNo = (UINT16) (DayNo - (UINT16) (CumulativeDays[IsLeap(Year)][MonthNo]));\r
162 break;\r
163 }\r
164 }\r
165\r
04a3cfa7
GCPL
166 GmTime->tm_mon = (int) MonthNo - 1;\r
167 GmTime->tm_mday = (int) DayNo + 1;\r
b7d320f8 168\r
169 GmTime->tm_isdst = 0;\r
170 GmTime->tm_gmtoff = 0;\r
171 GmTime->tm_zone = NULL;\r
172\r
173 return GmTime;\r
174}\r