+++ /dev/null
-/** @file\r
- C Run-Time Libraries (CRT) Time Management Routines Wrapper Implementation\r
- for OpenSSL-based Cryptographic Library (used in SMM).\r
-\r
-Copyright (c) 2010 - 2011, Intel Corporation. All rights reserved.<BR>\r
-This program and the accompanying materials\r
-are licensed and made available under the terms and conditions of the BSD License\r
-which accompanies this distribution. The full text of the license may be found at\r
-http://opensource.org/licenses/bsd-license.php\r
-\r
-THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
-WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
-\r
-**/\r
-\r
-#include <Library/BaseLib.h>\r
-#include <Library/IoLib.h>\r
-#include <OpenSslSupport.h>\r
-\r
-#define PCAT_RTC_ADDRESS_REGISTER 0x70\r
-#define PCAT_RTC_DATA_REGISTER 0x71\r
-\r
-#define RTC_ADDRESS_SECONDS 0 // R/W Range 0..59\r
-#define RTC_ADDRESS_SECONDS_ALARM 1 // R/W Range 0..59\r
-#define RTC_ADDRESS_MINUTES 2 // R/W Range 0..59\r
-#define RTC_ADDRESS_MINUTES_ALARM 3 // R/W Range 0..59\r
-#define RTC_ADDRESS_HOURS 4 // R/W Range 1..12 or 0..23 Bit 7 is AM/PM\r
-#define RTC_ADDRESS_HOURS_ALARM 5 // R/W Range 1..12 or 0..23 Bit 7 is AM/PM\r
-#define RTC_ADDRESS_DAY_OF_THE_WEEK 6 // R/W Range 1..7\r
-#define RTC_ADDRESS_DAY_OF_THE_MONTH 7 // R/W Range 1..31\r
-#define RTC_ADDRESS_MONTH 8 // R/W Range 1..12\r
-#define RTC_ADDRESS_YEAR 9 // R/W Range 0..99\r
-#define RTC_ADDRESS_REGISTER_A 10 // R/W[0..6] R0[7]\r
-#define RTC_ADDRESS_REGISTER_B 11 // R/W\r
-#define RTC_ADDRESS_REGISTER_C 12 // RO\r
-#define RTC_ADDRESS_REGISTER_D 13 // RO\r
-#define RTC_ADDRESS_CENTURY 50 // R/W Range 19..20 Bit 8 is R/W\r
-\r
-//\r
-// Register A\r
-//\r
-typedef struct {\r
- UINT8 RS : 4; // Rate Selection Bits\r
- UINT8 DV : 3; // Divisor\r
- UINT8 UIP : 1; // Update in progress\r
-} RTC_REGISTER_A_BITS;\r
-\r
-typedef union {\r
- RTC_REGISTER_A_BITS Bits;\r
- UINT8 Data;\r
-} RTC_REGISTER_A;\r
-\r
-//\r
-// Register B\r
-//\r
-typedef struct {\r
- UINT8 DSE : 1; // 0 - Daylight saving disabled 1 - Daylight savings enabled\r
- UINT8 MIL : 1; // 0 - 12 hour mode 1 - 24 hour mode\r
- UINT8 DM : 1; // 0 - BCD Format 1 - Binary Format\r
- UINT8 SQWE : 1; // 0 - Disable SQWE output 1 - Enable SQWE output\r
- UINT8 UIE : 1; // 0 - Update INT disabled 1 - Update INT enabled\r
- UINT8 AIE : 1; // 0 - Alarm INT disabled 1 - Alarm INT Enabled\r
- UINT8 PIE : 1; // 0 - Periodic INT disabled 1 - Periodic INT Enabled\r
- UINT8 SET : 1; // 0 - Normal operation. 1 - Updates inhibited\r
-} RTC_REGISTER_B_BITS;\r
-\r
-typedef union {\r
- RTC_REGISTER_B_BITS Bits;\r
- UINT8 Data;\r
-} RTC_REGISTER_B;\r
-\r
-//\r
-// -- Time Management Routines --\r
-//\r
-\r
-#define IsLeap(y) (((y) % 4) == 0 && (((y) % 100) != 0 || ((y) % 400) == 0))\r
-#define SECSPERMIN (60)\r
-#define SECSPERHOUR (60 * 60)\r
-#define SECSPERDAY (24 * SECSPERHOUR)\r
-\r
-//\r
-// The arrays give the cumulative number of days up to the first of the\r
-// month number used as the index (1 -> 12) for regular and leap years.\r
-// The value at index 13 is for the whole year.\r
-//\r
-UINTN CumulativeDays[2][14] = {\r
- {\r
- 0,\r
- 0,\r
- 31,\r
- 31 + 28,\r
- 31 + 28 + 31,\r
- 31 + 28 + 31 + 30,\r
- 31 + 28 + 31 + 30 + 31,\r
- 31 + 28 + 31 + 30 + 31 + 30,\r
- 31 + 28 + 31 + 30 + 31 + 30 + 31,\r
- 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31,\r
- 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r
- 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r
- 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r
- 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31\r
- },\r
- {\r
- 0,\r
- 0,\r
- 31,\r
- 31 + 29,\r
- 31 + 29 + 31,\r
- 31 + 29 + 31 + 30,\r
- 31 + 29 + 31 + 30 + 31,\r
- 31 + 29 + 31 + 30 + 31 + 30,\r
- 31 + 29 + 31 + 30 + 31 + 30 + 31,\r
- 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31,\r
- 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30,\r
- 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31,\r
- 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30,\r
- 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + 31 \r
- }\r
-};\r
-\r
-/**\r
- Read RTC content through its registers.\r
-\r
- @param Address Address offset of RTC. It is recommended to use macros such as\r
- RTC_ADDRESS_SECONDS.\r
-\r
- @return The data of UINT8 type read from RTC.\r
-**/\r
-UINT8\r
-RtcRead (\r
- IN UINT8 Address\r
- )\r
-{\r
- IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & BIT7)));\r
- return IoRead8 (PCAT_RTC_DATA_REGISTER);\r
-}\r
-\r
-/* Get the system time as seconds elapsed since midnight, January 1, 1970. */\r
-//INTN time(\r
-// INTN *timer\r
-// )\r
-time_t time (time_t *timer)\r
-{\r
- UINT16 Year;\r
- UINT8 Month;\r
- UINT8 Day;\r
- UINT8 Hour;\r
- UINT8 Minute;\r
- UINT8 Second;\r
- UINT8 Century;\r
- RTC_REGISTER_A RegisterA;\r
- RTC_REGISTER_B RegisterB;\r
- BOOLEAN IsPM;\r
- UINT16 YearIndex;\r
-\r
- RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
- while (RegisterA.Bits.UIP == 1) {\r
- CpuPause();\r
- RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
- }\r
-\r
- Second = RtcRead (RTC_ADDRESS_SECONDS);\r
- Minute = RtcRead (RTC_ADDRESS_MINUTES);\r
- Hour = RtcRead (RTC_ADDRESS_HOURS);\r
- Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
- Month = RtcRead (RTC_ADDRESS_MONTH);\r
- Year = RtcRead (RTC_ADDRESS_YEAR);\r
- Century = RtcRead (RTC_ADDRESS_CENTURY);\r
-\r
- RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
-\r
- if ((Hour & BIT7) != 0) {\r
- IsPM = TRUE;\r
- } else {\r
- IsPM = FALSE;\r
- }\r
- Hour = (UINT8) (Hour & 0x7f);\r
-\r
- if (RegisterB.Bits.DM == 0) {\r
- Year = BcdToDecimal8 ((UINT8) Year);\r
- Month = BcdToDecimal8 (Month);\r
- Day = BcdToDecimal8 (Day);\r
- Hour = BcdToDecimal8 (Hour);\r
- Minute = BcdToDecimal8 (Minute);\r
- Second = BcdToDecimal8 (Second);\r
- }\r
- Century = BcdToDecimal8 (Century);\r
-\r
- Year = (UINT16) (Century * 100 + Year);\r
-\r
- //\r
- // If time is in 12 hour format, convert it to 24 hour format\r
- //\r
- if (RegisterB.Bits.MIL == 0) {\r
- if (IsPM && Hour < 12) {\r
- Hour = (UINT8) (Hour + 12);\r
- }\r
- if (!IsPM && Hour == 12) {\r
- Hour = 0;\r
- }\r
- }\r
-\r
- //\r
- // Years Handling\r
- // UTime should now be set to 00:00:00 on Jan 1 of the current year.\r
- //\r
- for (YearIndex = 1970, *timer = 0; YearIndex != Year; YearIndex++) {\r
- *timer = *timer + (time_t)(CumulativeDays[IsLeap(YearIndex)][13] * SECSPERDAY);\r
- }\r
-\r
- //\r
- // Add in number of seconds for current Month, Day, Hour, Minute, Seconds, and TimeZone adjustment\r
- //\r
- ASSERT (Month <= 12);\r
- *timer = *timer + \r
- (time_t)(CumulativeDays[IsLeap(Year)][Month] * SECSPERDAY) + \r
- (time_t)((Day - 1) * SECSPERDAY) + \r
- (time_t)(Hour * SECSPERHOUR) + \r
- (time_t)(Minute * 60) + \r
- (time_t)Second;\r
-\r
- return *timer;\r
-}\r
-\r
-//\r
-// Convert a time value from type time_t to struct tm.\r
-//\r
-struct tm * gmtime (const time_t *timer)\r
-{\r
- struct tm *GmTime;\r
- UINT16 DayNo;\r
- UINT16 DayRemainder;\r
- time_t Year;\r
- time_t YearNo;\r
- UINT16 TotalDays;\r
- UINT16 MonthNo;\r
-\r
- if (timer == NULL) {\r
- return NULL;\r
- }\r
-\r
- GmTime = malloc (sizeof (struct tm));\r
- if (GmTime == NULL) {\r
- return NULL;\r
- }\r
-\r
- ZeroMem ((VOID *) GmTime, (UINTN) sizeof (struct tm));\r
-\r
- DayNo = (UINT16) (*timer / SECSPERDAY);\r
- DayRemainder = (UINT16) (*timer % SECSPERDAY);\r
-\r
- GmTime->tm_sec = (int) (DayRemainder % SECSPERMIN);\r
- GmTime->tm_min = (int) ((DayRemainder % SECSPERHOUR) / SECSPERMIN);\r
- GmTime->tm_hour = (int) (DayRemainder / SECSPERHOUR);\r
- GmTime->tm_wday = (int) ((DayNo + 4) % 7);\r
-\r
- for (Year = 1970, YearNo = 0; DayNo > 0; Year++) { \r
- TotalDays = (UINT16) (IsLeap (Year) ? 366 : 365);\r
- if (DayNo >= TotalDays) {\r
- DayNo = (UINT16) (DayNo - TotalDays);\r
- YearNo++;\r
- } else {\r
- break;\r
- }\r
- }\r
-\r
- GmTime->tm_year = (int) (YearNo + (1970 - 1900));\r
- GmTime->tm_yday = (int) DayNo;\r
-\r
- for (MonthNo = 12; MonthNo > 1; MonthNo--) {\r
- if (DayNo > CumulativeDays[IsLeap(Year)][MonthNo]) {\r
- DayNo = (UINT16) (DayNo - (UINT16) (CumulativeDays[IsLeap(Year)][MonthNo]));\r
- break;\r
- }\r
- }\r
-\r
- GmTime->tm_mon = (int) MonthNo;\r
- GmTime->tm_mday = (int) DayNo;\r
-\r
- GmTime->tm_isdst = 0;\r
- GmTime->tm_gmtoff = 0;\r
- GmTime->tm_zone = NULL;\r
-\r
- return GmTime;\r
-}\r
-\r