2008-04-10 10:49:28 +02:00
|
|
|
/** @file
|
|
|
|
RTC Architectural Protocol GUID as defined in DxeCis 0.96.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2011-11-17 07:43:01 +01:00
|
|
|
Copyright (c) 2006 - 2011, Intel Corporation. All rights reserved.<BR>
|
2010-04-24 14:07:51 +02:00
|
|
|
This program and the accompanying materials
|
2007-07-24 12:52:02 +02:00
|
|
|
are licensed and made available under the terms and conditions of the BSD License
|
|
|
|
which accompanies this distribution. The full text of the license may be found at
|
|
|
|
http://opensource.org/licenses/bsd-license.php
|
|
|
|
|
|
|
|
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-04-10 10:49:28 +02:00
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
|
|
|
|
#include "PcRtc.h"
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Compare the Hour, Minute and Second of the From time and the To time.
|
|
|
|
|
|
|
|
Only compare H/M/S in EFI_TIME and ignore other fields here.
|
|
|
|
|
|
|
|
@param From the first time
|
|
|
|
@param To the second time
|
|
|
|
|
|
|
|
@return >0 The H/M/S of the From time is later than those of To time
|
|
|
|
@return ==0 The H/M/S of the From time is same as those of To time
|
|
|
|
@return <0 The H/M/S of the From time is earlier than those of To time
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
INTN
|
|
|
|
CompareHMS (
|
|
|
|
IN EFI_TIME *From,
|
|
|
|
IN EFI_TIME *To
|
|
|
|
);
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
To check if second date is later than first date within 24 hours.
|
|
|
|
|
|
|
|
@param From the first date
|
|
|
|
@param To the second date
|
|
|
|
|
|
|
|
@retval TRUE From is previous to To within 24 hours.
|
|
|
|
@retval FALSE From is later, or it is previous to To more than 24 hours.
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
BOOLEAN
|
|
|
|
IsWithinOneDay (
|
|
|
|
IN EFI_TIME *From,
|
|
|
|
IN EFI_TIME *To
|
|
|
|
);
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Read RTC content through its registers.
|
|
|
|
|
|
|
|
@param Address Address offset of RTC. It is recommended to use macros such as
|
|
|
|
RTC_ADDRESS_SECONDS.
|
|
|
|
|
|
|
|
@return The data of UINT8 type read from RTC.
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
UINT8
|
|
|
|
RtcRead (
|
|
|
|
IN UINT8 Address
|
|
|
|
)
|
|
|
|
{
|
|
|
|
IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & 0x80)));
|
|
|
|
return IoRead8 (PCAT_RTC_DATA_REGISTER);
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Write RTC through its registers.
|
|
|
|
|
|
|
|
@param Address Address offset of RTC. It is recommended to use macros such as
|
|
|
|
RTC_ADDRESS_SECONDS.
|
|
|
|
@param Data The content you want to write into RTC.
|
|
|
|
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
VOID
|
|
|
|
RtcWrite (
|
|
|
|
IN UINT8 Address,
|
|
|
|
IN UINT8 Data
|
|
|
|
)
|
|
|
|
{
|
|
|
|
IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & 0x80)));
|
|
|
|
IoWrite8 (PCAT_RTC_DATA_REGISTER, Data);
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Initialize RTC.
|
|
|
|
|
|
|
|
@param Global For global use inside this module.
|
|
|
|
|
|
|
|
@retval EFI_DEVICE_ERROR Initialization failed due to device error.
|
|
|
|
@retval EFI_SUCCESS Initialization successful.
|
|
|
|
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
EFI_STATUS
|
|
|
|
PcRtcInit (
|
|
|
|
IN PC_RTC_MODULE_GLOBALS *Global
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
RTC_REGISTER_A RegisterA;
|
|
|
|
RTC_REGISTER_B RegisterB;
|
|
|
|
RTC_REGISTER_D RegisterD;
|
|
|
|
UINT8 Century;
|
|
|
|
EFI_TIME Time;
|
2008-05-06 07:38:06 +02:00
|
|
|
UINTN DataSize;
|
|
|
|
UINT32 TimerVar;
|
2007-07-19 12:26:16 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Acquire RTC Lock to make access to RTC atomic
|
|
|
|
//
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiAcquireLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Initialize RTC Register
|
|
|
|
//
|
|
|
|
// Make sure Division Chain is properly configured,
|
|
|
|
// or RTC clock won't "tick" -- time won't increment
|
|
|
|
//
|
|
|
|
RegisterA.Data = RTC_INIT_REGISTER_A;
|
|
|
|
RtcWrite (RTC_ADDRESS_REGISTER_A, RegisterA.Data);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Read Register B
|
|
|
|
//
|
|
|
|
RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Clear RTC flag register
|
|
|
|
//
|
|
|
|
RtcRead (RTC_ADDRESS_REGISTER_C);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Clear RTC register D
|
|
|
|
//
|
|
|
|
RegisterD.Data = RTC_INIT_REGISTER_D;
|
|
|
|
RtcWrite (RTC_ADDRESS_REGISTER_D, RegisterD.Data);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Wait for up to 0.1 seconds for the RTC to be updated
|
|
|
|
//
|
2008-07-16 13:50:02 +02:00
|
|
|
Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));
|
2007-07-19 12:26:16 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
2011-11-22 07:11:16 +01:00
|
|
|
//
|
|
|
|
// Set the variable with default value if the RTC is functioning incorrectly.
|
|
|
|
//
|
|
|
|
Global->SavedTimeZone = EFI_UNSPECIFIED_TIMEZONE;
|
|
|
|
Global->Daylight = 0;
|
2007-07-19 12:26:16 +02:00
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiReleaseLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Get the Time/Date/Daylight Savings values.
|
|
|
|
//
|
|
|
|
Time.Second = RtcRead (RTC_ADDRESS_SECONDS);
|
|
|
|
Time.Minute = RtcRead (RTC_ADDRESS_MINUTES);
|
|
|
|
Time.Hour = RtcRead (RTC_ADDRESS_HOURS);
|
|
|
|
Time.Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);
|
|
|
|
Time.Month = RtcRead (RTC_ADDRESS_MONTH);
|
|
|
|
Time.Year = RtcRead (RTC_ADDRESS_YEAR);
|
|
|
|
|
2009-07-01 08:05:41 +02:00
|
|
|
Century = RtcRead (RTC_ADDRESS_CENTURY);
|
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
|
|
|
// Set RTC configuration after get original time
|
2008-05-06 07:38:06 +02:00
|
|
|
// The value of bit AIE should be reserved.
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
2008-05-08 18:53:42 +02:00
|
|
|
RtcWrite (RTC_ADDRESS_REGISTER_B, (UINT8)(RTC_INIT_REGISTER_B | (RegisterB.Data & BIT5)));
|
2007-07-19 12:26:16 +02:00
|
|
|
|
|
|
|
//
|
|
|
|
// Release RTC Lock.
|
|
|
|
//
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiReleaseLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
2008-05-23 04:29:11 +02:00
|
|
|
|
2009-07-15 08:17:57 +02:00
|
|
|
//
|
|
|
|
// Get the data of Daylight saving and time zone, if they have been
|
|
|
|
// stored in NV variable during previous boot.
|
|
|
|
//
|
|
|
|
DataSize = sizeof (UINT32);
|
|
|
|
Status = EfiGetVariable (
|
|
|
|
L"RTC",
|
|
|
|
&gEfiCallerIdGuid,
|
|
|
|
NULL,
|
|
|
|
&DataSize,
|
|
|
|
(VOID *) &TimerVar
|
|
|
|
);
|
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
Time.TimeZone = (INT16) TimerVar;
|
|
|
|
Time.Daylight = (UINT8) (TimerVar >> 16);
|
|
|
|
} else {
|
|
|
|
Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE;
|
|
|
|
Time.Daylight = 0;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
|
|
|
// Validate time fields
|
|
|
|
//
|
2008-05-23 04:29:11 +02:00
|
|
|
Status = ConvertRtcTimeToEfiTime (&Time, Century, RegisterB);
|
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
Status = RtcTimeFieldsValid (&Time);
|
|
|
|
}
|
2007-07-19 12:26:16 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
2012-10-30 05:25:20 +01:00
|
|
|
//
|
|
|
|
// Report Status Code to indicate that the RTC has bad date and time
|
|
|
|
//
|
|
|
|
REPORT_STATUS_CODE (
|
|
|
|
EFI_ERROR_CODE | EFI_ERROR_MINOR,
|
|
|
|
(EFI_SOFTWARE_DXE_RT_DRIVER | EFI_SW_EC_BAD_DATE_TIME)
|
|
|
|
);
|
2007-07-19 12:26:16 +02:00
|
|
|
Time.Second = RTC_INIT_SECOND;
|
|
|
|
Time.Minute = RTC_INIT_MINUTE;
|
|
|
|
Time.Hour = RTC_INIT_HOUR;
|
|
|
|
Time.Day = RTC_INIT_DAY;
|
|
|
|
Time.Month = RTC_INIT_MONTH;
|
|
|
|
Time.Year = RTC_INIT_YEAR;
|
2009-07-27 11:00:41 +02:00
|
|
|
Time.Nanosecond = 0;
|
2011-11-17 09:35:08 +01:00
|
|
|
Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE;
|
|
|
|
Time.Daylight = 0;
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
2008-05-06 07:38:06 +02:00
|
|
|
|
|
|
|
//
|
2007-07-19 12:26:16 +02:00
|
|
|
// Reset time value according to new RTC configuration
|
|
|
|
//
|
2009-07-27 11:00:41 +02:00
|
|
|
Status = PcRtcSetTime (&Time, Global);
|
|
|
|
if(!EFI_ERROR (Status)) {
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
} else {
|
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
}
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Returns the current time and date information, and the time-keeping capabilities
|
|
|
|
of the hardware platform.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
@param Time A pointer to storage to receive a snapshot of the current time.
|
|
|
|
@param Capabilities An optional pointer to a buffer to receive the real time clock
|
|
|
|
device's capabilities.
|
|
|
|
@param Global For global use inside this module.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
@retval EFI_SUCCESS The operation completed successfully.
|
|
|
|
@retval EFI_INVALID_PARAMETER Time is NULL.
|
|
|
|
@retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
PcRtcGetTime (
|
|
|
|
OUT EFI_TIME *Time,
|
|
|
|
OUT EFI_TIME_CAPABILITIES *Capabilities, OPTIONAL
|
|
|
|
IN PC_RTC_MODULE_GLOBALS *Global
|
|
|
|
)
|
2007-07-19 12:26:16 +02:00
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
RTC_REGISTER_B RegisterB;
|
|
|
|
UINT8 Century;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Check parameters for null pointer
|
|
|
|
//
|
|
|
|
if (Time == NULL) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Acquire RTC Lock to make access to RTC atomic
|
|
|
|
//
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiAcquireLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Wait for up to 0.1 seconds for the RTC to be updated
|
|
|
|
//
|
2008-07-16 13:50:02 +02:00
|
|
|
Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));
|
2007-07-19 12:26:16 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiReleaseLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Read Register B
|
|
|
|
//
|
|
|
|
RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the Time/Date/Daylight Savings values.
|
|
|
|
//
|
|
|
|
Time->Second = RtcRead (RTC_ADDRESS_SECONDS);
|
|
|
|
Time->Minute = RtcRead (RTC_ADDRESS_MINUTES);
|
|
|
|
Time->Hour = RtcRead (RTC_ADDRESS_HOURS);
|
|
|
|
Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);
|
|
|
|
Time->Month = RtcRead (RTC_ADDRESS_MONTH);
|
|
|
|
Time->Year = RtcRead (RTC_ADDRESS_YEAR);
|
|
|
|
|
2009-07-01 08:05:41 +02:00
|
|
|
Century = RtcRead (RTC_ADDRESS_CENTURY);
|
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
|
|
|
// Release RTC Lock.
|
|
|
|
//
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiReleaseLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
2009-07-15 08:17:57 +02:00
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
2008-12-30 08:06:28 +01:00
|
|
|
// Get the variable that contains the TimeZone and Daylight fields
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
|
|
|
Time->TimeZone = Global->SavedTimeZone;
|
|
|
|
Time->Daylight = Global->Daylight;
|
|
|
|
|
|
|
|
//
|
|
|
|
// Make sure all field values are in correct range
|
|
|
|
//
|
2008-05-23 04:29:11 +02:00
|
|
|
Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB);
|
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
Status = RtcTimeFieldsValid (Time);
|
|
|
|
}
|
2007-07-19 12:26:16 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
}
|
2009-07-15 08:17:57 +02:00
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
|
|
|
// Fill in Capabilities if it was passed in
|
|
|
|
//
|
2008-11-07 08:44:21 +01:00
|
|
|
if (Capabilities != NULL) {
|
2007-07-19 12:26:16 +02:00
|
|
|
Capabilities->Resolution = 1;
|
|
|
|
//
|
|
|
|
// 1 hertz
|
|
|
|
//
|
|
|
|
Capabilities->Accuracy = 50000000;
|
|
|
|
//
|
|
|
|
// 50 ppm
|
|
|
|
//
|
|
|
|
Capabilities->SetsToZero = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Sets the current local time and date information.
|
|
|
|
|
|
|
|
@param Time A pointer to the current time.
|
|
|
|
@param Global For global use inside this module.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The operation completed successfully.
|
|
|
|
@retval EFI_INVALID_PARAMETER A time field is out of range.
|
|
|
|
@retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
|
|
|
|
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
EFI_STATUS
|
|
|
|
PcRtcSetTime (
|
|
|
|
IN EFI_TIME *Time,
|
|
|
|
IN PC_RTC_MODULE_GLOBALS *Global
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_TIME RtcTime;
|
|
|
|
RTC_REGISTER_B RegisterB;
|
|
|
|
UINT8 Century;
|
2008-05-06 07:38:06 +02:00
|
|
|
UINT32 TimerVar;
|
2007-07-19 12:26:16 +02:00
|
|
|
|
|
|
|
if (Time == NULL) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Make sure that the time fields are valid
|
|
|
|
//
|
|
|
|
Status = RtcTimeFieldsValid (Time);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
|
|
|
|
CopyMem (&RtcTime, Time, sizeof (EFI_TIME));
|
|
|
|
|
|
|
|
//
|
|
|
|
// Acquire RTC Lock to make access to RTC atomic
|
|
|
|
//
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiAcquireLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Wait for up to 0.1 seconds for the RTC to be updated
|
|
|
|
//
|
2008-07-16 13:50:02 +02:00
|
|
|
Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));
|
2007-07-19 12:26:16 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-12-19 03:15:47 +01:00
|
|
|
EfiReleaseLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
return Status;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Read Register B, and inhibit updates of the RTC
|
|
|
|
//
|
|
|
|
RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);
|
2010-09-17 12:42:10 +02:00
|
|
|
RegisterB.Bits.Set = 1;
|
2007-07-19 12:26:16 +02:00
|
|
|
RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);
|
|
|
|
|
|
|
|
ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);
|
|
|
|
|
|
|
|
RtcWrite (RTC_ADDRESS_SECONDS, RtcTime.Second);
|
|
|
|
RtcWrite (RTC_ADDRESS_MINUTES, RtcTime.Minute);
|
|
|
|
RtcWrite (RTC_ADDRESS_HOURS, RtcTime.Hour);
|
|
|
|
RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day);
|
|
|
|
RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month);
|
|
|
|
RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year);
|
|
|
|
RtcWrite (RTC_ADDRESS_CENTURY, Century);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Allow updates of the RTC registers
|
|
|
|
//
|
2010-09-17 12:42:10 +02:00
|
|
|
RegisterB.Bits.Set = 0;
|
2007-07-19 12:26:16 +02:00
|
|
|
RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Release RTC Lock.
|
|
|
|
//
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiReleaseLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
//
|
2008-12-30 08:06:28 +01:00
|
|
|
// Set the variable that contains the TimeZone and Daylight fields
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
|
|
|
Global->SavedTimeZone = Time->TimeZone;
|
|
|
|
Global->Daylight = Time->Daylight;
|
2008-05-06 07:38:06 +02:00
|
|
|
|
|
|
|
TimerVar = Time->Daylight;
|
2011-11-17 07:43:01 +01:00
|
|
|
TimerVar = (UINT32) ((TimerVar << 16) | (UINT16)(Time->TimeZone));
|
2008-05-06 07:38:06 +02:00
|
|
|
Status = EfiSetVariable (
|
2009-02-23 04:17:27 +01:00
|
|
|
L"RTC",
|
|
|
|
&gEfiCallerIdGuid,
|
2008-05-06 07:38:06 +02:00
|
|
|
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
|
|
|
|
sizeof (TimerVar),
|
|
|
|
&TimerVar
|
|
|
|
);
|
|
|
|
ASSERT_EFI_ERROR (Status);
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Returns the current wakeup alarm clock setting.
|
|
|
|
|
|
|
|
@param Enabled Indicates if the alarm is currently enabled or disabled.
|
2008-12-30 08:06:28 +01:00
|
|
|
@param Pending Indicates if the alarm signal is pending and requires acknowledgment.
|
2008-11-07 08:44:21 +01:00
|
|
|
@param Time The current alarm setting.
|
|
|
|
@param Global For global use inside this module.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS The alarm settings were returned.
|
|
|
|
@retval EFI_INVALID_PARAMETER Enabled is NULL.
|
|
|
|
@retval EFI_INVALID_PARAMETER Pending is NULL.
|
|
|
|
@retval EFI_INVALID_PARAMETER Time is NULL.
|
|
|
|
@retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
|
|
|
|
@retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
|
|
|
|
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
EFI_STATUS
|
|
|
|
PcRtcGetWakeupTime (
|
|
|
|
OUT BOOLEAN *Enabled,
|
|
|
|
OUT BOOLEAN *Pending,
|
|
|
|
OUT EFI_TIME *Time,
|
2008-11-07 08:44:21 +01:00
|
|
|
IN PC_RTC_MODULE_GLOBALS *Global
|
2007-07-19 12:26:16 +02:00
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
RTC_REGISTER_B RegisterB;
|
|
|
|
RTC_REGISTER_C RegisterC;
|
|
|
|
UINT8 Century;
|
2010-11-10 04:18:21 +01:00
|
|
|
EFI_TIME RtcTime;
|
|
|
|
UINTN DataSize;
|
2007-07-19 12:26:16 +02:00
|
|
|
|
|
|
|
//
|
2008-12-30 08:06:28 +01:00
|
|
|
// Check parameters for null pointers
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
|
|
|
if ((Enabled == NULL) || (Pending == NULL) || (Time == NULL)) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Acquire RTC Lock to make access to RTC atomic
|
|
|
|
//
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiAcquireLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Wait for up to 0.1 seconds for the RTC to be updated
|
|
|
|
//
|
2008-07-16 13:50:02 +02:00
|
|
|
Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));
|
2007-07-19 12:26:16 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
if (!EfiAtRuntime ()) {
|
|
|
|
EfiReleaseLock (&Global->RtcLock);
|
|
|
|
}
|
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Read Register B and Register C
|
|
|
|
//
|
|
|
|
RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);
|
|
|
|
RegisterC.Data = RtcRead (RTC_ADDRESS_REGISTER_C);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Get the Time/Date/Daylight Savings values.
|
|
|
|
//
|
2010-09-17 12:42:10 +02:00
|
|
|
*Enabled = RegisterB.Bits.Aie;
|
2010-11-10 04:18:21 +01:00
|
|
|
*Pending = RegisterC.Bits.Af;
|
|
|
|
|
|
|
|
Time->Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);
|
|
|
|
Time->Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);
|
|
|
|
Time->Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);
|
|
|
|
Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);
|
|
|
|
Time->Month = RtcRead (RTC_ADDRESS_MONTH);
|
|
|
|
Time->Year = RtcRead (RTC_ADDRESS_YEAR);
|
|
|
|
Time->TimeZone = Global->SavedTimeZone;
|
|
|
|
Time->Daylight = Global->Daylight;
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2009-07-01 08:05:41 +02:00
|
|
|
Century = RtcRead (RTC_ADDRESS_CENTURY);
|
2010-11-10 04:18:21 +01:00
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
2010-11-10 04:18:21 +01:00
|
|
|
// Get the alarm info from variable
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
2012-08-15 06:38:15 +02:00
|
|
|
DataSize = sizeof (EFI_TIME);
|
2010-11-10 04:18:21 +01:00
|
|
|
Status = EfiGetVariable (
|
|
|
|
L"RTCALARM",
|
|
|
|
&gEfiCallerIdGuid,
|
|
|
|
NULL,
|
|
|
|
&DataSize,
|
|
|
|
&RtcTime
|
|
|
|
);
|
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
//
|
|
|
|
// The alarm variable exists. In this case, we read variable to get info.
|
|
|
|
//
|
|
|
|
Time->Day = RtcTime.Day;
|
|
|
|
Time->Month = RtcTime.Month;
|
|
|
|
Time->Year = RtcTime.Year;
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
2009-07-15 08:17:57 +02:00
|
|
|
|
|
|
|
//
|
2010-11-10 04:18:21 +01:00
|
|
|
// Release RTC Lock.
|
2009-07-15 08:17:57 +02:00
|
|
|
//
|
2010-11-10 04:18:21 +01:00
|
|
|
if (!EfiAtRuntime ()) {
|
|
|
|
EfiReleaseLock (&Global->RtcLock);
|
|
|
|
}
|
2009-07-15 08:17:57 +02:00
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
|
|
|
// Make sure all field values are in correct range
|
|
|
|
//
|
2008-05-23 04:29:11 +02:00
|
|
|
Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB);
|
|
|
|
if (!EFI_ERROR (Status)) {
|
|
|
|
Status = RtcTimeFieldsValid (Time);
|
|
|
|
}
|
2007-07-19 12:26:16 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Sets the system wakeup alarm clock time.
|
|
|
|
|
|
|
|
@param Enabled Enable or disable the wakeup alarm.
|
|
|
|
@param Time If Enable is TRUE, the time to set the wakeup alarm for.
|
|
|
|
If Enable is FALSE, then this parameter is optional, and may be NULL.
|
|
|
|
@param Global For global use inside this module.
|
|
|
|
|
|
|
|
@retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.
|
|
|
|
If Enable is FALSE, then the wakeup alarm was disabled.
|
|
|
|
@retval EFI_INVALID_PARAMETER A time field is out of range.
|
|
|
|
@retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
|
|
|
|
@retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
|
|
|
|
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
EFI_STATUS
|
|
|
|
PcRtcSetWakeupTime (
|
|
|
|
IN BOOLEAN Enable,
|
2008-11-07 08:44:21 +01:00
|
|
|
IN EFI_TIME *Time, OPTIONAL
|
2007-07-19 12:26:16 +02:00
|
|
|
IN PC_RTC_MODULE_GLOBALS *Global
|
|
|
|
)
|
|
|
|
{
|
|
|
|
EFI_STATUS Status;
|
|
|
|
EFI_TIME RtcTime;
|
|
|
|
RTC_REGISTER_B RegisterB;
|
|
|
|
UINT8 Century;
|
|
|
|
EFI_TIME_CAPABILITIES Capabilities;
|
|
|
|
|
2010-11-10 08:23:12 +01:00
|
|
|
ZeroMem (&RtcTime, sizeof (RtcTime));
|
2010-11-10 04:18:21 +01:00
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
if (Enable) {
|
|
|
|
|
|
|
|
if (Time == NULL) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Make sure that the time fields are valid
|
|
|
|
//
|
|
|
|
Status = RtcTimeFieldsValid (Time);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Just support set alarm time within 24 hours
|
|
|
|
//
|
|
|
|
PcRtcGetTime (&RtcTime, &Capabilities, Global);
|
2009-01-20 05:17:05 +01:00
|
|
|
Status = RtcTimeFieldsValid (&RtcTime);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
}
|
2007-07-19 12:26:16 +02:00
|
|
|
if (!IsWithinOneDay (&RtcTime, Time)) {
|
|
|
|
return EFI_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Make a local copy of the time and date
|
|
|
|
//
|
|
|
|
CopyMem (&RtcTime, Time, sizeof (EFI_TIME));
|
|
|
|
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Acquire RTC Lock to make access to RTC atomic
|
|
|
|
//
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiAcquireLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Wait for up to 0.1 seconds for the RTC to be updated
|
|
|
|
//
|
2008-07-16 13:50:02 +02:00
|
|
|
Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));
|
2007-07-19 12:26:16 +02:00
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
if (!EfiAtRuntime ()) {
|
|
|
|
EfiReleaseLock (&Global->RtcLock);
|
|
|
|
}
|
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Read Register B, and inhibit updates of the RTC
|
|
|
|
//
|
|
|
|
RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);
|
|
|
|
|
2010-09-17 12:42:10 +02:00
|
|
|
RegisterB.Bits.Set = 1;
|
2007-07-19 12:26:16 +02:00
|
|
|
RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);
|
|
|
|
|
|
|
|
if (Enable) {
|
|
|
|
ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);
|
|
|
|
|
|
|
|
//
|
|
|
|
// Set RTC alarm time
|
|
|
|
//
|
|
|
|
RtcWrite (RTC_ADDRESS_SECONDS_ALARM, RtcTime.Second);
|
|
|
|
RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute);
|
|
|
|
RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour);
|
|
|
|
|
2010-09-17 12:42:10 +02:00
|
|
|
RegisterB.Bits.Aie = 1;
|
2007-07-19 12:26:16 +02:00
|
|
|
|
|
|
|
} else {
|
2010-09-17 12:42:10 +02:00
|
|
|
RegisterB.Bits.Aie = 0;
|
2010-11-10 04:18:21 +01:00
|
|
|
//
|
|
|
|
// if the alarm is disable, record the current setting.
|
|
|
|
//
|
|
|
|
RtcTime.Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);
|
|
|
|
RtcTime.Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);
|
|
|
|
RtcTime.Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);
|
|
|
|
RtcTime.Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);
|
|
|
|
RtcTime.Month = RtcRead (RTC_ADDRESS_MONTH);
|
|
|
|
RtcTime.Year = RtcRead (RTC_ADDRESS_YEAR);
|
|
|
|
RtcTime.TimeZone = Global->SavedTimeZone;
|
|
|
|
RtcTime.Daylight = Global->Daylight;
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
//
|
|
|
|
// Allow updates of the RTC registers
|
|
|
|
//
|
2010-09-17 12:42:10 +02:00
|
|
|
RegisterB.Bits.Set = 0;
|
2007-07-19 12:26:16 +02:00
|
|
|
RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);
|
|
|
|
|
2010-11-10 04:18:21 +01:00
|
|
|
//
|
|
|
|
// Set the Y/M/D info to variable as it has no corresponding hw registers.
|
|
|
|
//
|
|
|
|
Status = EfiSetVariable (
|
|
|
|
L"RTCALARM",
|
|
|
|
&gEfiCallerIdGuid,
|
|
|
|
EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
|
|
|
|
sizeof (RtcTime),
|
|
|
|
&RtcTime
|
|
|
|
);
|
|
|
|
if (EFI_ERROR (Status)) {
|
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
}
|
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
|
|
|
// Release RTC Lock.
|
|
|
|
//
|
|
|
|
if (!EfiAtRuntime ()) {
|
2008-11-07 08:44:21 +01:00
|
|
|
EfiReleaseLock (&Global->RtcLock);
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-23 04:29:11 +02:00
|
|
|
/**
|
|
|
|
Checks an 8-bit BCD value, and converts to an 8-bit value if valid.
|
|
|
|
|
|
|
|
This function checks the 8-bit BCD value specified by Value.
|
|
|
|
If valid, the function converts it to an 8-bit value and returns it.
|
|
|
|
Otherwise, return 0xff.
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
@param Value The 8-bit BCD value to check and convert
|
2008-05-23 04:29:11 +02:00
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
@return The 8-bit value converted. Or 0xff if Value is invalid.
|
2008-05-23 04:29:11 +02:00
|
|
|
|
|
|
|
**/
|
|
|
|
UINT8
|
|
|
|
CheckAndConvertBcd8ToDecimal8 (
|
|
|
|
IN UINT8 Value
|
2007-07-19 12:26:16 +02:00
|
|
|
)
|
2008-05-23 04:29:11 +02:00
|
|
|
{
|
|
|
|
if ((Value < 0xa0) && ((Value & 0xf) < 0xa)) {
|
|
|
|
return BcdToDecimal8 (Value);
|
|
|
|
}
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-05-23 04:29:11 +02:00
|
|
|
return 0xff;
|
|
|
|
}
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-05-23 04:29:11 +02:00
|
|
|
/**
|
|
|
|
Converts time read from RTC to EFI_TIME format defined by UEFI spec.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-05-23 04:29:11 +02:00
|
|
|
This function converts raw time data read from RTC to the EFI_TIME format
|
|
|
|
defined by UEFI spec.
|
|
|
|
If data mode of RTC is BCD, then converts it to decimal,
|
|
|
|
If RTC is in 12-hour format, then converts it to 24-hour format.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-05-23 04:29:11 +02:00
|
|
|
@param Time On input, the time data read from RTC to convert
|
|
|
|
On output, the time converted to UEFI format
|
|
|
|
@param Century Value of century read from RTC.
|
|
|
|
@param RegisterB Value of Register B of RTC, indicating data mode
|
|
|
|
and hour format.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
@retval EFI_INVALID_PARAMETER Parameters passed in are invalid.
|
|
|
|
@retval EFI_SUCCESS Convert RTC time to EFI time successfully.
|
|
|
|
|
2008-05-23 04:29:11 +02:00
|
|
|
**/
|
|
|
|
EFI_STATUS
|
|
|
|
ConvertRtcTimeToEfiTime (
|
|
|
|
IN OUT EFI_TIME *Time,
|
|
|
|
IN UINT8 Century,
|
|
|
|
IN RTC_REGISTER_B RegisterB
|
|
|
|
)
|
2007-07-19 12:26:16 +02:00
|
|
|
{
|
2008-11-07 08:44:21 +01:00
|
|
|
BOOLEAN IsPM;
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-11-12 07:41:40 +01:00
|
|
|
if ((Time->Hour & 0x80) != 0) {
|
2008-11-07 08:44:21 +01:00
|
|
|
IsPM = TRUE;
|
2007-07-19 12:26:16 +02:00
|
|
|
} else {
|
2008-11-07 08:44:21 +01:00
|
|
|
IsPM = FALSE;
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Time->Hour = (UINT8) (Time->Hour & 0x7f);
|
|
|
|
|
2010-09-17 12:42:10 +02:00
|
|
|
if (RegisterB.Bits.Dm == 0) {
|
2008-05-23 04:29:11 +02:00
|
|
|
Time->Year = CheckAndConvertBcd8ToDecimal8 ((UINT8) Time->Year);
|
|
|
|
Time->Month = CheckAndConvertBcd8ToDecimal8 (Time->Month);
|
|
|
|
Time->Day = CheckAndConvertBcd8ToDecimal8 (Time->Day);
|
|
|
|
Time->Hour = CheckAndConvertBcd8ToDecimal8 (Time->Hour);
|
|
|
|
Time->Minute = CheckAndConvertBcd8ToDecimal8 (Time->Minute);
|
|
|
|
Time->Second = CheckAndConvertBcd8ToDecimal8 (Time->Second);
|
|
|
|
}
|
2009-07-06 06:46:50 +02:00
|
|
|
Century = CheckAndConvertBcd8ToDecimal8 (Century);
|
2008-05-23 04:29:11 +02:00
|
|
|
|
|
|
|
if (Time->Year == 0xff || Time->Month == 0xff || Time->Day == 0xff ||
|
|
|
|
Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff ||
|
|
|
|
Century == 0xff) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
2008-05-23 04:29:11 +02:00
|
|
|
|
|
|
|
Time->Year = (UINT16) (Century * 100 + Time->Year);
|
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
|
|
|
// If time is in 12 hour format, convert it to 24 hour format
|
|
|
|
//
|
2010-09-17 12:42:10 +02:00
|
|
|
if (RegisterB.Bits.Mil == 0) {
|
2008-11-07 08:44:21 +01:00
|
|
|
if (IsPM && Time->Hour < 12) {
|
2007-07-19 12:26:16 +02:00
|
|
|
Time->Hour = (UINT8) (Time->Hour + 12);
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
if (!IsPM && Time->Hour == 12) {
|
2007-07-19 12:26:16 +02:00
|
|
|
Time->Hour = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Time->Nanosecond = 0;
|
2008-05-23 04:29:11 +02:00
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Wait for a period for the RTC to be ready.
|
|
|
|
|
|
|
|
@param Timeout Tell how long it should take to wait.
|
|
|
|
|
|
|
|
@retval EFI_DEVICE_ERROR RTC device error.
|
|
|
|
@retval EFI_SUCCESS RTC is updated and ready.
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
EFI_STATUS
|
|
|
|
RtcWaitToUpdate (
|
|
|
|
UINTN Timeout
|
|
|
|
)
|
|
|
|
{
|
|
|
|
RTC_REGISTER_A RegisterA;
|
|
|
|
RTC_REGISTER_D RegisterD;
|
|
|
|
|
|
|
|
//
|
|
|
|
// See if the RTC is functioning correctly
|
|
|
|
//
|
|
|
|
RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);
|
|
|
|
|
2010-09-17 12:42:10 +02:00
|
|
|
if (RegisterD.Bits.Vrt == 0) {
|
2007-07-19 12:26:16 +02:00
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Wait for up to 0.1 seconds for the RTC to be ready.
|
|
|
|
//
|
|
|
|
Timeout = (Timeout / 10) + 1;
|
|
|
|
RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);
|
2010-09-17 12:42:10 +02:00
|
|
|
while (RegisterA.Bits.Uip == 1 && Timeout > 0) {
|
2007-07-19 12:26:16 +02:00
|
|
|
MicroSecondDelay (10);
|
|
|
|
RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);
|
|
|
|
Timeout--;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);
|
2010-09-17 12:42:10 +02:00
|
|
|
if (Timeout == 0 || RegisterD.Bits.Vrt == 0) {
|
2007-07-19 12:26:16 +02:00
|
|
|
return EFI_DEVICE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
See if all fields of a variable of EFI_TIME type is correct.
|
|
|
|
|
|
|
|
@param Time The time to be checked.
|
|
|
|
|
|
|
|
@retval EFI_INVALID_PARAMETER Some fields of Time are not correct.
|
|
|
|
@retval EFI_SUCCESS Time is a valid EFI_TIME variable.
|
|
|
|
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
EFI_STATUS
|
|
|
|
RtcTimeFieldsValid (
|
|
|
|
IN EFI_TIME *Time
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (Time->Year < 1998 ||
|
|
|
|
Time->Year > 2099 ||
|
|
|
|
Time->Month < 1 ||
|
|
|
|
Time->Month > 12 ||
|
2009-07-15 08:17:57 +02:00
|
|
|
(!DayValid (Time)) ||
|
2007-07-19 12:26:16 +02:00
|
|
|
Time->Hour > 23 ||
|
|
|
|
Time->Minute > 59 ||
|
|
|
|
Time->Second > 59 ||
|
|
|
|
Time->Nanosecond > 999999999 ||
|
|
|
|
(!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||
|
2009-07-15 08:17:57 +02:00
|
|
|
((Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT))) != 0)) {
|
|
|
|
return EFI_INVALID_PARAMETER;
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return EFI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
See if field Day of an EFI_TIME is correct.
|
|
|
|
|
|
|
|
@param Time Its Day field is to be checked.
|
|
|
|
|
|
|
|
@retval TRUE Day field of Time is correct.
|
|
|
|
@retval FALSE Day field of Time is NOT correct.
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
BOOLEAN
|
|
|
|
DayValid (
|
|
|
|
IN EFI_TIME *Time
|
|
|
|
)
|
|
|
|
{
|
2009-07-27 11:08:49 +02:00
|
|
|
INTN DayOfMonth[12];
|
|
|
|
|
|
|
|
DayOfMonth[0] = 31;
|
|
|
|
DayOfMonth[1] = 29;
|
|
|
|
DayOfMonth[2] = 31;
|
|
|
|
DayOfMonth[3] = 30;
|
|
|
|
DayOfMonth[4] = 31;
|
|
|
|
DayOfMonth[5] = 30;
|
|
|
|
DayOfMonth[6] = 31;
|
|
|
|
DayOfMonth[7] = 31;
|
|
|
|
DayOfMonth[8] = 30;
|
|
|
|
DayOfMonth[9] = 31;
|
|
|
|
DayOfMonth[10] = 30;
|
|
|
|
DayOfMonth[11] = 31;
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2009-02-11 08:05:43 +01:00
|
|
|
//
|
|
|
|
// The validity of Time->Month field should be checked before
|
|
|
|
//
|
|
|
|
ASSERT (Time->Month >=1);
|
|
|
|
ASSERT (Time->Month <=12);
|
2007-07-19 12:26:16 +02:00
|
|
|
if (Time->Day < 1 ||
|
|
|
|
Time->Day > DayOfMonth[Time->Month - 1] ||
|
|
|
|
(Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))
|
|
|
|
) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
2008-12-30 08:06:28 +01:00
|
|
|
Check if it is a leap year.
|
2008-11-07 08:44:21 +01:00
|
|
|
|
|
|
|
@param Time The time to be checked.
|
|
|
|
|
2008-12-30 08:06:28 +01:00
|
|
|
@retval TRUE It is a leap year.
|
|
|
|
@retval FALSE It is NOT a leap year.
|
2008-11-07 08:44:21 +01:00
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
BOOLEAN
|
|
|
|
IsLeapYear (
|
|
|
|
IN EFI_TIME *Time
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if (Time->Year % 4 == 0) {
|
|
|
|
if (Time->Year % 100 == 0) {
|
|
|
|
if (Time->Year % 400 == 0) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Converts time from EFI_TIME format defined by UEFI spec to RTC's.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
This function converts time from EFI_TIME format defined by UEFI spec to RTC's.
|
|
|
|
If data mode of RTC is BCD, then converts EFI_TIME to it.
|
|
|
|
If RTC is in 12-hour format, then converts EFI_TIME to it.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
@param Time On input, the time data read from UEFI to convert
|
|
|
|
On output, the time converted to RTC format
|
|
|
|
@param RegisterB Value of Register B of RTC, indicating data mode
|
|
|
|
@param Century It is set according to EFI_TIME Time.
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
**/
|
|
|
|
VOID
|
|
|
|
ConvertEfiTimeToRtcTime (
|
|
|
|
IN OUT EFI_TIME *Time,
|
|
|
|
IN RTC_REGISTER_B RegisterB,
|
|
|
|
OUT UINT8 *Century
|
|
|
|
)
|
2007-07-19 12:26:16 +02:00
|
|
|
{
|
2008-11-07 08:44:21 +01:00
|
|
|
BOOLEAN IsPM;
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
IsPM = TRUE;
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
2008-11-07 08:44:21 +01:00
|
|
|
// Adjust hour field if RTC is in 12 hour mode
|
2007-07-19 12:26:16 +02:00
|
|
|
//
|
2010-09-17 12:42:10 +02:00
|
|
|
if (RegisterB.Bits.Mil == 0) {
|
2007-07-19 12:26:16 +02:00
|
|
|
if (Time->Hour < 12) {
|
2008-11-07 08:44:21 +01:00
|
|
|
IsPM = FALSE;
|
2007-07-19 12:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Time->Hour >= 13) {
|
|
|
|
Time->Hour = (UINT8) (Time->Hour - 12);
|
|
|
|
} else if (Time->Hour == 0) {
|
|
|
|
Time->Hour = 12;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// Set the Time/Date/Daylight Savings values.
|
|
|
|
//
|
|
|
|
*Century = DecimalToBcd8 ((UINT8) (Time->Year / 100));
|
|
|
|
|
|
|
|
Time->Year = (UINT16) (Time->Year % 100);
|
|
|
|
|
2010-09-17 12:42:10 +02:00
|
|
|
if (RegisterB.Bits.Dm == 0) {
|
2007-07-19 12:26:16 +02:00
|
|
|
Time->Year = DecimalToBcd8 ((UINT8) Time->Year);
|
|
|
|
Time->Month = DecimalToBcd8 (Time->Month);
|
|
|
|
Time->Day = DecimalToBcd8 (Time->Day);
|
|
|
|
Time->Hour = DecimalToBcd8 (Time->Hour);
|
|
|
|
Time->Minute = DecimalToBcd8 (Time->Minute);
|
|
|
|
Time->Second = DecimalToBcd8 (Time->Second);
|
|
|
|
}
|
|
|
|
//
|
|
|
|
// If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.
|
|
|
|
//
|
2010-09-17 12:42:10 +02:00
|
|
|
if (RegisterB.Bits.Mil == 0 && IsPM) {
|
2007-07-19 12:26:16 +02:00
|
|
|
Time->Hour = (UINT8) (Time->Hour | 0x80);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
Compare the Hour, Minute and Second of the From time and the To time.
|
|
|
|
|
|
|
|
Only compare H/M/S in EFI_TIME and ignore other fields here.
|
|
|
|
|
|
|
|
@param From the first time
|
|
|
|
@param To the second time
|
|
|
|
|
|
|
|
@return >0 The H/M/S of the From time is later than those of To time
|
|
|
|
@return ==0 The H/M/S of the From time is same as those of To time
|
|
|
|
@return <0 The H/M/S of the From time is earlier than those of To time
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
INTN
|
|
|
|
CompareHMS (
|
|
|
|
IN EFI_TIME *From,
|
|
|
|
IN EFI_TIME *To
|
|
|
|
)
|
|
|
|
{
|
|
|
|
if ((From->Hour > To->Hour) ||
|
|
|
|
((From->Hour == To->Hour) && (From->Minute > To->Minute)) ||
|
|
|
|
((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second > To->Second))) {
|
|
|
|
return 1;
|
|
|
|
} else if ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second == To->Second)) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-07 08:44:21 +01:00
|
|
|
/**
|
|
|
|
To check if second date is later than first date within 24 hours.
|
|
|
|
|
|
|
|
@param From the first date
|
|
|
|
@param To the second date
|
|
|
|
|
|
|
|
@retval TRUE From is previous to To within 24 hours.
|
|
|
|
@retval FALSE From is later, or it is previous to To more than 24 hours.
|
|
|
|
**/
|
2007-07-19 12:26:16 +02:00
|
|
|
BOOLEAN
|
|
|
|
IsWithinOneDay (
|
|
|
|
IN EFI_TIME *From,
|
|
|
|
IN EFI_TIME *To
|
|
|
|
)
|
|
|
|
{
|
2009-07-27 11:08:49 +02:00
|
|
|
UINT8 DayOfMonth[12];
|
2007-09-04 11:24:56 +02:00
|
|
|
BOOLEAN Adjacent;
|
|
|
|
|
2009-07-27 11:08:49 +02:00
|
|
|
DayOfMonth[0] = 31;
|
|
|
|
DayOfMonth[1] = 29;
|
|
|
|
DayOfMonth[2] = 31;
|
|
|
|
DayOfMonth[3] = 30;
|
|
|
|
DayOfMonth[4] = 31;
|
|
|
|
DayOfMonth[5] = 30;
|
|
|
|
DayOfMonth[6] = 31;
|
|
|
|
DayOfMonth[7] = 31;
|
|
|
|
DayOfMonth[8] = 30;
|
|
|
|
DayOfMonth[9] = 31;
|
|
|
|
DayOfMonth[10] = 30;
|
|
|
|
DayOfMonth[11] = 31;
|
|
|
|
|
2007-09-04 11:24:56 +02:00
|
|
|
Adjacent = FALSE;
|
2007-07-19 12:26:16 +02:00
|
|
|
|
2009-02-11 08:05:43 +01:00
|
|
|
//
|
2009-02-11 08:09:19 +01:00
|
|
|
// The validity of From->Month field should be checked before
|
2009-02-11 08:05:43 +01:00
|
|
|
//
|
|
|
|
ASSERT (From->Month >=1);
|
|
|
|
ASSERT (From->Month <=12);
|
|
|
|
|
2007-07-19 12:26:16 +02:00
|
|
|
if (From->Year == To->Year) {
|
|
|
|
if (From->Month == To->Month) {
|
|
|
|
if ((From->Day + 1) == To->Day) {
|
|
|
|
if ((CompareHMS(From, To) >= 0)) {
|
|
|
|
Adjacent = TRUE;
|
|
|
|
}
|
|
|
|
} else if (From->Day == To->Day) {
|
|
|
|
if ((CompareHMS(From, To) <= 0)) {
|
|
|
|
Adjacent = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (((From->Month + 1) == To->Month) && (To->Day == 1)) {
|
|
|
|
if ((From->Month == 2) && !IsLeapYear(From)) {
|
|
|
|
if (From->Day == 28) {
|
|
|
|
if ((CompareHMS(From, To) >= 0)) {
|
|
|
|
Adjacent = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (From->Day == DayOfMonth[From->Month - 1]) {
|
|
|
|
if ((CompareHMS(From, To) >= 0)) {
|
|
|
|
Adjacent = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (((From->Year + 1) == To->Year) &&
|
|
|
|
(From->Month == 12) &&
|
|
|
|
(From->Day == 31) &&
|
|
|
|
(To->Month == 1) &&
|
|
|
|
(To->Day == 1)) {
|
|
|
|
if ((CompareHMS(From, To) >= 0)) {
|
|
|
|
Adjacent = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Adjacent;
|
|
|
|
}
|
|
|
|
|