]>
Commit | Line | Data |
---|---|---|
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 | 5 | Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r |
2009f6b4 | 6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r |
97f98500 HT |
7 | \r |
8 | **/\r | |
9 | \r | |
10 | #include <Uefi.h>\r | |
fc9fa685 | 11 | #include <CrtLibSupport.h>\r |
97f98500 HT |
12 | #include <Library/UefiRuntimeServicesTableLib.h>\r |
13 | \r | |
14 | //\r | |
15 | // -- Time Management Routines --\r | |
16 | //\r | |
17 | \r | |
7c342378 MK |
18 | #define SECSPERMIN (60)\r |
19 | #define SECSPERHOUR (60 * 60)\r | |
20 | #define SECSPERDAY (24 * SECSPERHOUR)\r | |
97f98500 HT |
21 | \r |
22 | //\r | |
23 | // The arrays give the cumulative number of days up to the first of the\r | |
24 | // month number used as the index (1 -> 12) for regular and leap years.\r | |
25 | // The value at index 13 is for the whole year.\r | |
26 | //\r | |
7c342378 | 27 | UINTN CumulativeDays[2][14] = {\r |
97f98500 HT |
28 | {\r |
29 | 0,\r | |
30 | 0,\r | |
31 | 31,\r | |
32 | 31 + 28,\r | |
33 | 31 + 28 + 31,\r | |
34 | 31 + 28 + 31 + 30,\r | |
35 | 31 + 28 + 31 + 30 + 31,\r | |
36 | 31 + 28 + 31 + 30 + 31 + 30,\r | |
37 | 31 + 28 + 31 + 30 + 31 + 30 + 31,\r | |
38 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,\r | |
39 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r | |
40 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r | |
41 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r | |
42 | 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r | |
43 | },\r | |
44 | {\r | |
45 | 0,\r | |
46 | 0,\r | |
47 | 31,\r | |
48 | 31 + 29,\r | |
49 | 31 + 29 + 31,\r | |
50 | 31 + 29 + 31 + 30,\r | |
51 | 31 + 29 + 31 + 30 + 31,\r | |
52 | 31 + 29 + 31 + 30 + 31 + 30,\r | |
53 | 31 + 29 + 31 + 30 + 31 + 30 + 31,\r | |
54 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,\r | |
55 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r | |
56 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r | |
57 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r | |
630f67dd | 58 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r |
97f98500 HT |
59 | }\r |
60 | };\r | |
61 | \r | |
7de1c71d ZY |
62 | /* Check the year is leap or not. */\r |
63 | // BOOLEAN IsLeap(\r | |
64 | // INTN timer\r | |
65 | // )\r | |
66 | BOOLEAN\r | |
67 | IsLeap (\r | |
68 | time_t timer\r | |
69 | )\r | |
70 | {\r | |
71 | INT64 Remainder1;\r | |
72 | INT64 Remainder2;\r | |
73 | INT64 Remainder3;\r | |
74 | \r | |
75 | DivS64x64Remainder (timer, 4, &Remainder1);\r | |
76 | DivS64x64Remainder (timer, 100, &Remainder2);\r | |
77 | DivS64x64Remainder (timer, 400, &Remainder3);\r | |
78 | \r | |
79 | return (Remainder1 == 0 && (Remainder2 != 0 || Remainder3 == 0));\r | |
80 | }\r | |
81 | \r | |
97f98500 | 82 | /* Get the system time as seconds elapsed since midnight, January 1, 1970. */\r |
7c342378 | 83 | // INTN time(\r |
97f98500 HT |
84 | // INTN *timer\r |
85 | // )\r | |
7c342378 MK |
86 | time_t\r |
87 | time (\r | |
88 | time_t *timer\r | |
89 | )\r | |
97f98500 | 90 | {\r |
ab187ae2 LQ |
91 | EFI_STATUS Status;\r |
92 | EFI_TIME Time;\r | |
93 | time_t CalTime;\r | |
94 | UINTN Year;\r | |
97f98500 HT |
95 | \r |
96 | //\r | |
97 | // Get the current time and date information\r | |
98 | //\r | |
ab187ae2 LQ |
99 | Status = gRT->GetTime (&Time, NULL);\r |
100 | if (EFI_ERROR (Status) || (Time.Year < 1970)) {\r | |
101 | return 0;\r | |
102 | }\r | |
97f98500 HT |
103 | \r |
104 | //\r | |
105 | // Years Handling\r | |
106 | // UTime should now be set to 00:00:00 on Jan 1 of the current year.\r | |
107 | //\r | |
5e2318dd | 108 | for (Year = 1970, CalTime = 0; Year != Time.Year; Year++) {\r |
7c342378 | 109 | CalTime = CalTime + (time_t)(CumulativeDays[IsLeap (Year)][13] * SECSPERDAY);\r |
97f98500 HT |
110 | }\r |
111 | \r | |
112 | //\r | |
113 | // Add in number of seconds for current Month, Day, Hour, Minute, Seconds, and TimeZone adjustment\r | |
114 | //\r | |
630f67dd | 115 | CalTime = CalTime +\r |
5e2318dd | 116 | (time_t)((Time.TimeZone != EFI_UNSPECIFIED_TIMEZONE) ? (Time.TimeZone * 60) : 0) +\r |
7c342378 | 117 | (time_t)(CumulativeDays[IsLeap (Time.Year)][Time.Month] * SECSPERDAY) +\r |
630f67dd LG |
118 | (time_t)(((Time.Day > 0) ? Time.Day - 1 : 0) * SECSPERDAY) +\r |
119 | (time_t)(Time.Hour * SECSPERHOUR) +\r | |
120 | (time_t)(Time.Minute * 60) +\r | |
5e2318dd JW |
121 | (time_t)Time.Second;\r |
122 | \r | |
123 | if (timer != NULL) {\r | |
124 | *timer = CalTime;\r | |
125 | }\r | |
126 | \r | |
127 | return CalTime;\r | |
97f98500 | 128 | }\r |
b7d320f8 | 129 | \r |
130 | //\r | |
131 | // Convert a time value from type time_t to struct tm.\r | |
132 | //\r | |
7c342378 MK |
133 | struct tm *\r |
134 | gmtime (\r | |
135 | const time_t *timer\r | |
136 | )\r | |
b7d320f8 | 137 | {\r |
138 | struct tm *GmTime;\r | |
7de1c71d ZY |
139 | UINT64 DayNo;\r |
140 | UINT64 DayRemainder;\r | |
b7d320f8 | 141 | time_t Year;\r |
142 | time_t YearNo;\r | |
7de1c71d ZY |
143 | UINT32 TotalDays;\r |
144 | UINT32 MonthNo;\r | |
145 | INT64 Remainder;\r | |
b7d320f8 | 146 | \r |
147 | if (timer == NULL) {\r | |
148 | return NULL;\r | |
149 | }\r | |
150 | \r | |
151 | GmTime = malloc (sizeof (struct tm));\r | |
152 | if (GmTime == NULL) {\r | |
153 | return NULL;\r | |
154 | }\r | |
155 | \r | |
7c342378 | 156 | ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));\r |
b7d320f8 | 157 | \r |
7de1c71d ZY |
158 | DayNo = (UINT64)DivS64x64Remainder (*timer, SECSPERDAY, &Remainder);\r |
159 | DayRemainder = (UINT64)Remainder;\r | |
b7d320f8 | 160 | \r |
7de1c71d ZY |
161 | DivS64x64Remainder (DayRemainder, SECSPERMIN, &Remainder);\r |
162 | GmTime->tm_sec = (int)Remainder;\r | |
163 | DivS64x64Remainder (DayRemainder, SECSPERHOUR, &Remainder);\r | |
164 | GmTime->tm_min = (int)DivS64x64Remainder (Remainder, SECSPERMIN, NULL);\r | |
165 | GmTime->tm_hour = (int)DivS64x64Remainder (DayRemainder, SECSPERHOUR, NULL);\r | |
166 | DivS64x64Remainder ((DayNo + 4), 7, &Remainder);\r | |
167 | GmTime->tm_wday = (int)Remainder;\r | |
b7d320f8 | 168 | \r |
169 | for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {\r | |
7de1c71d | 170 | TotalDays = (UINT32)(IsLeap (Year) ? 366 : 365);\r |
b7d320f8 | 171 | if (DayNo >= TotalDays) {\r |
7de1c71d | 172 | DayNo = (UINT64)(DayNo - TotalDays);\r |
b7d320f8 | 173 | YearNo++;\r |
174 | } else {\r | |
175 | break;\r | |
176 | }\r | |
177 | }\r | |
178 | \r | |
7c342378 MK |
179 | GmTime->tm_year = (int)(YearNo + (1970 - 1900));\r |
180 | GmTime->tm_yday = (int)DayNo;\r | |
b7d320f8 | 181 | \r |
182 | for (MonthNo = 12; MonthNo > 1; MonthNo--) {\r | |
7c342378 | 183 | if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {\r |
7de1c71d | 184 | DayNo = (UINT64)(DayNo - (UINT32)(CumulativeDays[IsLeap (Year)][MonthNo]));\r |
b7d320f8 | 185 | break;\r |
186 | }\r | |
187 | }\r | |
188 | \r | |
7c342378 MK |
189 | GmTime->tm_mon = (int)MonthNo - 1;\r |
190 | GmTime->tm_mday = (int)DayNo + 1;\r | |
b7d320f8 | 191 | \r |
192 | GmTime->tm_isdst = 0;\r | |
193 | GmTime->tm_gmtoff = 0;\r | |
194 | GmTime->tm_zone = NULL;\r | |
195 | \r | |
196 | return GmTime;\r | |
197 | }\r |