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