]>
Commit | Line | Data |
---|---|---|
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 | |
5 | Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>\r | |
6 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
7 | \r | |
8 | **/\r | |
9 | \r | |
10 | #include <Uefi.h>\r | |
11 | #include <CrtLibSupport.h>\r | |
12 | #include <Library/UefiRuntimeServicesTableLib.h>\r | |
13 | \r | |
14 | //\r | |
15 | // -- Time Management Routines --\r | |
16 | //\r | |
17 | \r | |
18 | #define SECSPERMIN (60)\r | |
19 | #define SECSPERHOUR (60 * 60)\r | |
20 | #define SECSPERDAY (24 * SECSPERHOUR)\r | |
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 | |
27 | UINTN CumulativeDays[2][14] = {\r | |
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 | |
58 | 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r | |
59 | }\r | |
60 | };\r | |
61 | \r | |
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 | |
82 | /* Get the system time as seconds elapsed since midnight, January 1, 1970. */\r | |
83 | // INTN time(\r | |
84 | // INTN *timer\r | |
85 | // )\r | |
86 | time_t\r | |
87 | time (\r | |
88 | time_t *timer\r | |
89 | )\r | |
90 | {\r | |
91 | EFI_STATUS Status;\r | |
92 | EFI_TIME Time;\r | |
93 | time_t CalTime;\r | |
94 | UINTN Year;\r | |
95 | \r | |
96 | //\r | |
97 | // Get the current time and date information\r | |
98 | //\r | |
99 | Status = gRT->GetTime (&Time, NULL);\r | |
100 | if (EFI_ERROR (Status) || (Time.Year < 1970)) {\r | |
101 | return 0;\r | |
102 | }\r | |
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 | |
108 | for (Year = 1970, CalTime = 0; Year != Time.Year; Year++) {\r | |
109 | CalTime = CalTime + (time_t)(CumulativeDays[IsLeap (Year)][13] * SECSPERDAY);\r | |
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 | |
115 | CalTime = CalTime +\r | |
116 | (time_t)((Time.TimeZone != EFI_UNSPECIFIED_TIMEZONE) ? (Time.TimeZone * 60) : 0) +\r | |
117 | (time_t)(CumulativeDays[IsLeap (Time.Year)][Time.Month] * SECSPERDAY) +\r | |
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 | |
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 | |
128 | }\r | |
129 | \r | |
130 | //\r | |
131 | // Convert a time value from type time_t to struct tm.\r | |
132 | //\r | |
133 | struct tm *\r | |
134 | gmtime (\r | |
135 | const time_t *timer\r | |
136 | )\r | |
137 | {\r | |
138 | struct tm *GmTime;\r | |
139 | UINT64 DayNo;\r | |
140 | UINT64 DayRemainder;\r | |
141 | time_t Year;\r | |
142 | time_t YearNo;\r | |
143 | UINT32 TotalDays;\r | |
144 | UINT32 MonthNo;\r | |
145 | INT64 Remainder;\r | |
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 | |
156 | ZeroMem ((VOID *)GmTime, (UINTN)sizeof (struct tm));\r | |
157 | \r | |
158 | DayNo = (UINT64)DivS64x64Remainder (*timer, SECSPERDAY, &Remainder);\r | |
159 | DayRemainder = (UINT64)Remainder;\r | |
160 | \r | |
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 | |
168 | \r | |
169 | for (Year = 1970, YearNo = 0; DayNo > 0; Year++) {\r | |
170 | TotalDays = (UINT32)(IsLeap (Year) ? 366 : 365);\r | |
171 | if (DayNo >= TotalDays) {\r | |
172 | DayNo = (UINT64)(DayNo - TotalDays);\r | |
173 | YearNo++;\r | |
174 | } else {\r | |
175 | break;\r | |
176 | }\r | |
177 | }\r | |
178 | \r | |
179 | GmTime->tm_year = (int)(YearNo + (1970 - 1900));\r | |
180 | GmTime->tm_yday = (int)DayNo;\r | |
181 | \r | |
182 | for (MonthNo = 12; MonthNo > 1; MonthNo--) {\r | |
183 | if (DayNo >= CumulativeDays[IsLeap (Year)][MonthNo]) {\r | |
184 | DayNo = (UINT64)(DayNo - (UINT32)(CumulativeDays[IsLeap (Year)][MonthNo]));\r | |
185 | break;\r | |
186 | }\r | |
187 | }\r | |
188 | \r | |
189 | GmTime->tm_mon = (int)MonthNo - 1;\r | |
190 | GmTime->tm_mday = (int)DayNo + 1;\r | |
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 |