]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/BaseCryptLib/SysCall/TimerWrapper.c
CryptoPkg: Fix the potential system hang issue
[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
5e2318dd 5Copyright (c) 2010 - 2016, 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
17#include <OpenSslSupport.h>\r
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
65 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 \r
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
75 EFI_TIME Time;\r
5e2318dd 76 time_t CalTime;\r
97f98500
HT
77 UINTN Year;\r
78\r
79 //\r
80 // Get the current time and date information\r
81 //\r
82 gRT->GetTime (&Time, NULL);\r
83\r
84 //\r
85 // Years Handling\r
86 // UTime should now be set to 00:00:00 on Jan 1 of the current year.\r
87 //\r
5e2318dd
JW
88 for (Year = 1970, CalTime = 0; Year != Time.Year; Year++) {\r
89 CalTime = CalTime + (time_t)(CumulativeDays[IsLeap(Year)][13] * SECSPERDAY);\r
97f98500
HT
90 }\r
91\r
92 //\r
93 // Add in number of seconds for current Month, Day, Hour, Minute, Seconds, and TimeZone adjustment\r
94 //\r
5e2318dd
JW
95 CalTime = CalTime + \r
96 (time_t)((Time.TimeZone != EFI_UNSPECIFIED_TIMEZONE) ? (Time.TimeZone * 60) : 0) +\r
97 (time_t)(CumulativeDays[IsLeap(Time.Year)][Time.Month] * SECSPERDAY) + \r
98 (time_t)(((Time.Day > 0) ? Time.Day - 1 : 0) * SECSPERDAY) + \r
99 (time_t)(Time.Hour * SECSPERHOUR) + \r
100 (time_t)(Time.Minute * 60) + \r
101 (time_t)Time.Second;\r
102\r
103 if (timer != NULL) {\r
104 *timer = CalTime;\r
105 }\r
106\r
107 return CalTime;\r
97f98500 108}\r
b7d320f8 109\r
110//\r
111// Convert a time value from type time_t to struct tm.\r
112//\r
113struct tm * gmtime (const time_t *timer)\r
114{\r
115 struct tm *GmTime;\r
116 UINT16 DayNo;\r
117 UINT16 DayRemainder;\r
118 time_t Year;\r
119 time_t YearNo;\r
120 UINT16 TotalDays;\r
121 UINT16 MonthNo;\r
122\r
123 if (timer == NULL) {\r
124 return NULL;\r
125 }\r
126\r
127 GmTime = malloc (sizeof (struct tm));\r
128 if (GmTime == NULL) {\r
129 return NULL;\r
130 }\r
131\r
132 ZeroMem ((VOID *) GmTime, (UINTN) sizeof (struct tm));\r
133\r
134 DayNo = (UINT16) (*timer / SECSPERDAY);\r
135 DayRemainder = (UINT16) (*timer % SECSPERDAY);\r
136\r
137 GmTime->tm_sec = (int) (DayRemainder % SECSPERMIN);\r
138 GmTime->tm_min = (int) ((DayRemainder % SECSPERHOUR) / SECSPERMIN);\r
139 GmTime->tm_hour = (int) (DayRemainder / SECSPERHOUR);\r
140 GmTime->tm_wday = (int) ((DayNo + 4) % 7);\r
141\r
142 for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {\r
143 TotalDays = (UINT16) (IsLeap (Year) ? 366 : 365);\r
144 if (DayNo >= TotalDays) {\r
145 DayNo = (UINT16) (DayNo - TotalDays);\r
146 YearNo++;\r
147 } else {\r
148 break;\r
149 }\r
150 }\r
151\r
152 GmTime->tm_year = (int) (YearNo + (1970 - 1900));\r
153 GmTime->tm_yday = (int) DayNo;\r
154\r
155 for (MonthNo = 12; MonthNo > 1; MonthNo--) {\r
04a3cfa7 156 if (DayNo >= CumulativeDays[IsLeap(Year)][MonthNo]) {\r
b7d320f8 157 DayNo = (UINT16) (DayNo - (UINT16) (CumulativeDays[IsLeap(Year)][MonthNo]));\r
158 break;\r
159 }\r
160 }\r
161\r
04a3cfa7
GCPL
162 GmTime->tm_mon = (int) MonthNo - 1;\r
163 GmTime->tm_mday = (int) DayNo + 1;\r
b7d320f8 164\r
165 GmTime->tm_isdst = 0;\r
166 GmTime->tm_gmtoff = 0;\r
167 GmTime->tm_zone = NULL;\r
168\r
169 return GmTime;\r
170}\r