+/*++\r
+\r
+Copyright (c) 2006, Intel Corporation \r
+All rights reserved. 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
+Module Name:\r
+\r
+ RealTimeClock.c\r
+\r
+Abstract:\r
+\r
+ NT Emulation Architectural Protocol Driver as defined in Tiano\r
+\r
+--*/\r
+\r
+\r
+\r
+//\r
+// Include common header file for this module.\r
+//\r
+#include "CommonHeader.h"\r
+\r
+BOOLEAN\r
+DayValid (\r
+ IN EFI_TIME *Time\r
+ );\r
+\r
+BOOLEAN\r
+IsLeapYear (\r
+ IN EFI_TIME *Time\r
+ );\r
+\r
+EFI_STATUS\r
+RtcTimeFieldsValid (\r
+ IN EFI_TIME *Time\r
+ );\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeRealTimeClock (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ );\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+WinNtGetTime (\r
+ OUT EFI_TIME *Time,\r
+ OUT EFI_TIME_CAPABILITIES *Capabilities OPTIONAL\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Service routine for RealTimeClockInstance->GetTime \r
+\r
+Arguments:\r
+\r
+ Time - A pointer to storage that will receive a snapshot of the current time.\r
+\r
+ Capabilities - A pointer to storage that will receive the capabilities of the real time clock\r
+ in the platform. This includes the real time clock's resolution and accuracy. \r
+ All reported device capabilities are rounded up. This is an OPTIONAL argument.\r
+\r
+Returns:\r
+\r
+ EFI_SUCEESS - The underlying GetSystemTime call occurred and returned\r
+ Note that in the NT32 emulation, the GetSystemTime call has no return value\r
+ thus you will always receive a EFI_SUCCESS on this.\r
+\r
+--*/\r
+// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
+{\r
+ SYSTEMTIME SystemTime;\r
+ TIME_ZONE_INFORMATION TimeZone;\r
+\r
+ //\r
+ // Check parameter for null pointer\r
+ //\r
+ if (Time == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+\r
+ }\r
+\r
+ gWinNt->GetLocalTime (&SystemTime);\r
+ gWinNt->GetTimeZoneInformation (&TimeZone);\r
+\r
+ Time->Year = (UINT16) SystemTime.wYear;\r
+ Time->Month = (UINT8) SystemTime.wMonth;\r
+ Time->Day = (UINT8) SystemTime.wDay;\r
+ Time->Hour = (UINT8) SystemTime.wHour;\r
+ Time->Minute = (UINT8) SystemTime.wMinute;\r
+ Time->Second = (UINT8) SystemTime.wSecond;\r
+ Time->Nanosecond = (UINT32) (SystemTime.wMilliseconds * 1000000);\r
+ Time->TimeZone = (INT16) TimeZone.Bias;\r
+\r
+ if (Capabilities != NULL) {\r
+ Capabilities->Resolution = 1;\r
+ Capabilities->Accuracy = 50000000;\r
+ Capabilities->SetsToZero = FALSE;\r
+ }\r
+\r
+ Time->Daylight = 0;\r
+ if (TimeZone.StandardDate.wMonth) {\r
+ Time->Daylight = EFI_TIME_ADJUST_DAYLIGHT;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+WinNtSetTime (\r
+ IN EFI_TIME *Time\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Service routine for RealTimeClockInstance->SetTime \r
+\r
+Arguments:\r
+\r
+ Time - A pointer to storage containing the time and date information to\r
+ program into the real time clock.\r
+\r
+Returns:\r
+\r
+ EFI_SUCEESS - The operation completed successfully.\r
+ \r
+ EFI_INVALID_PARAMETER - One of the fields in Time is out of range.\r
+\r
+ EFI_DEVICE_ERROR - The operation could not be complete due to a device error.\r
+\r
+--*/\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+{\r
+ TIME_ZONE_INFORMATION TimeZone;\r
+ EFI_STATUS Status;\r
+ SYSTEMTIME SystemTime;\r
+ BOOL Flag;\r
+\r
+ if (Time == NULL) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+ //\r
+ // Make sure that the time fields are valid\r
+ //\r
+ Status = RtcTimeFieldsValid (Time);\r
+ if (EFI_ERROR (Status)) {\r
+ return Status;\r
+ }\r
+ //\r
+ // Set Daylight savings time information and Time Zone\r
+ //\r
+ gWinNt->GetTimeZoneInformation (&TimeZone);\r
+ TimeZone.StandardDate.wMonth = Time->Daylight;\r
+ TimeZone.Bias = Time->TimeZone;\r
+ gWinNt->SetTimeZoneInformation (&TimeZone);\r
+\r
+ SystemTime.wYear = Time->Year;\r
+ SystemTime.wMonth = Time->Month;\r
+ SystemTime.wDay = Time->Day;\r
+ SystemTime.wHour = Time->Hour;\r
+ SystemTime.wMinute = Time->Minute;\r
+ SystemTime.wSecond = Time->Second;\r
+ SystemTime.wMilliseconds = (INT16) (Time->Nanosecond / 1000000);\r
+\r
+ Flag = gWinNt->SetLocalTime (&SystemTime);\r
+\r
+ if (!Flag) {\r
+ return EFI_DEVICE_ERROR;\r
+ } else {\r
+ return EFI_SUCCESS;\r
+ }\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+WinNtGetWakeupTime (\r
+ OUT BOOLEAN *Enabled,\r
+ OUT BOOLEAN *Pending,\r
+ OUT EFI_TIME *Time\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Service routine for RealTimeClockInstance->GetWakeupTime\r
+\r
+Arguments:\r
+ This - Indicates the protocol instance structure.\r
+\r
+ Enabled - Indicates if the alarm is currently enabled or disabled.\r
+\r
+ Pending - Indicates if the alarm signal is pending and requires\r
+ acknowledgement.\r
+\r
+ Time - The current alarm setting.\r
+\r
+Returns:\r
+\r
+ EFI_SUCEESS - The operation completed successfully.\r
+ \r
+ EFI_DEVICE_ERROR - The operation could not be complete due to a device error.\r
+\r
+ EFI_UNSUPPORTED - The operation is not supported on this platform.\r
+\r
+--*/\r
+{\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+STATIC\r
+EFI_STATUS\r
+EFIAPI\r
+WinNtSetWakeupTime (\r
+ IN BOOLEAN Enable,\r
+ OUT EFI_TIME *Time\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Service routine for RealTimeClockInstance->SetWakeupTime\r
+\r
+Arguments:\r
+\r
+ Enabled - Enable or disable the wakeup alarm.\r
+\r
+ Time - If enable is TRUE, the time to set the wakup alarm for.\r
+ If enable is FALSE, then this parameter is optional, and\r
+ may be NULL.\r
+\r
+Returns:\r
+\r
+ EFI_SUCEESS - The operation completed successfully.\r
+ \r
+ EFI_DEVICE_ERROR - The operation could not be complete due to a device error.\r
+\r
+ EFI_INVALID_PARAMETER - A field in Time is out of range.\r
+\r
+ EFI_UNSUPPORTED - The operation is not supported on this platform.\r
+\r
+--*/\r
+{\r
+ return EFI_UNSUPPORTED;\r
+}\r
+\r
+EFI_STATUS\r
+EFIAPI\r
+InitializeRealTimeClock (\r
+ IN EFI_HANDLE ImageHandle,\r
+ IN EFI_SYSTEM_TABLE *SystemTable\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+ Install Real Time Clock Protocol \r
+\r
+Arguments:\r
+ (Standard EFI Image entry - EFI_IMAGE_ENTRY_POINT)\r
+\r
+Returns:\r
+\r
+ EFI_SUCEESS - Real Time Clock Services are installed into the Runtime Services Table\r
+\r
+--*/\r
+// TODO: ImageHandle - add argument and description to function comment\r
+// TODO: SystemTable - add argument and description to function comment\r
+{\r
+ EFI_STATUS Status;\r
+ EFI_HANDLE Handle;\r
+\r
+\r
+ SystemTable->RuntimeServices->GetTime = WinNtGetTime;\r
+ SystemTable->RuntimeServices->SetTime = WinNtSetTime;\r
+ SystemTable->RuntimeServices->GetWakeupTime = WinNtGetWakeupTime;\r
+ SystemTable->RuntimeServices->SetWakeupTime = WinNtSetWakeupTime;\r
+\r
+ Handle = NULL;\r
+ Status = gBS->InstallMultipleProtocolInterfaces (\r
+ &Handle,\r
+ &gEfiRealTimeClockArchProtocolGuid,\r
+ NULL,\r
+ NULL\r
+ );\r
+ return Status;\r
+}\r
+\r
+EFI_STATUS\r
+RtcTimeFieldsValid (\r
+ IN EFI_TIME *Time\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ Arguments:\r
+ \r
+ Returns: \r
+--*/\r
+// TODO: Time - add argument and description to function comment\r
+// TODO: EFI_INVALID_PARAMETER - add return value to function comment\r
+// TODO: EFI_SUCCESS - add return value to function comment\r
+{\r
+ if (Time->Year < 1998 ||\r
+ Time->Year > 2099 ||\r
+ Time->Month < 1 ||\r
+ Time->Month > 12 ||\r
+ (!DayValid (Time)) ||\r
+ Time->Hour > 23 ||\r
+ Time->Minute > 59 ||\r
+ Time->Second > 59 ||\r
+ Time->Nanosecond > 999999999 ||\r
+ (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||\r
+ (Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT)))\r
+ ) {\r
+ return EFI_INVALID_PARAMETER;\r
+ }\r
+\r
+ return EFI_SUCCESS;\r
+}\r
+\r
+BOOLEAN\r
+DayValid (\r
+ IN EFI_TIME *Time\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ TODO: Add function description\r
+\r
+Arguments:\r
+\r
+ Time - TODO: add argument description\r
+\r
+Returns:\r
+\r
+ TODO: add return values\r
+\r
+--*/\r
+{\r
+\r
+ INTN DayOfMonth[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };\r
+\r
+ if (Time->Day < 1 ||\r
+ Time->Day > DayOfMonth[Time->Month - 1] ||\r
+ (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))\r
+ ) {\r
+ return FALSE;\r
+ }\r
+\r
+ return TRUE;\r
+}\r
+\r
+BOOLEAN\r
+IsLeapYear (\r
+ IN EFI_TIME *Time\r
+ )\r
+/*++\r
+\r
+Routine Description:\r
+\r
+ TODO: Add function description\r
+\r
+Arguments:\r
+\r
+ Time - TODO: add argument description\r
+\r
+Returns:\r
+\r
+ TODO: add return values\r
+\r
+--*/\r
+{\r
+ if (Time->Year % 4 == 0) {\r
+ if (Time->Year % 100 == 0) {\r
+ if (Time->Year % 400 == 0) {\r
+ return TRUE;\r
+ } else {\r
+ return FALSE;\r
+ }\r
+ } else {\r
+ return TRUE;\r
+ }\r
+ } else {\r
+ return FALSE;\r
+ }\r
+}\r