PcRtc: Fix PcRtcInit() to not clear RegisterB Hour Format bit (BIT1) sometimes
[mirror_edk2.git] / PcAtChipsetPkg / PcatRealTimeClockRuntimeDxe / PcRtc.c
CommitLineData
fb0b259e 1/** @file\r
2 RTC Architectural Protocol GUID as defined in DxeCis 0.96.\r
8cd4d17c 3\r
1e5fff63 4Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>\r
95d48e82 5This program and the accompanying materials\r
3cfb790c 6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
8cd4d17c 12\r
fb0b259e 13**/\r
8cd4d17c 14\r
15#include "PcRtc.h"\r
16\r
8d85dc31 17/**\r
18 Compare the Hour, Minute and Second of the From time and the To time.\r
19 \r
20 Only compare H/M/S in EFI_TIME and ignore other fields here.\r
21\r
22 @param From the first time\r
23 @param To the second time\r
24\r
25 @return >0 The H/M/S of the From time is later than those of To time\r
26 @return ==0 The H/M/S of the From time is same as those of To time\r
27 @return <0 The H/M/S of the From time is earlier than those of To time\r
28**/\r
8cd4d17c 29INTN\r
30CompareHMS (\r
31 IN EFI_TIME *From,\r
32 IN EFI_TIME *To\r
33 );\r
34\r
8d85dc31 35/**\r
36 To check if second date is later than first date within 24 hours.\r
37\r
38 @param From the first date\r
39 @param To the second date\r
40\r
41 @retval TRUE From is previous to To within 24 hours.\r
42 @retval FALSE From is later, or it is previous to To more than 24 hours.\r
43**/\r
8cd4d17c 44BOOLEAN\r
45IsWithinOneDay (\r
46 IN EFI_TIME *From,\r
47 IN EFI_TIME *To\r
48 );\r
49\r
8d85dc31 50/**\r
51 Read RTC content through its registers.\r
52\r
53 @param Address Address offset of RTC. It is recommended to use macros such as\r
54 RTC_ADDRESS_SECONDS.\r
55\r
56 @return The data of UINT8 type read from RTC.\r
57**/\r
8cd4d17c 58UINT8\r
59RtcRead (\r
60 IN UINT8 Address\r
61 )\r
8cd4d17c 62{\r
63 IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & 0x80)));\r
64 return IoRead8 (PCAT_RTC_DATA_REGISTER);\r
65}\r
66\r
8d85dc31 67/**\r
68 Write RTC through its registers.\r
69\r
70 @param Address Address offset of RTC. It is recommended to use macros such as\r
71 RTC_ADDRESS_SECONDS.\r
72 @param Data The content you want to write into RTC.\r
73\r
74**/\r
8cd4d17c 75VOID\r
76RtcWrite (\r
77 IN UINT8 Address,\r
78 IN UINT8 Data\r
79 )\r
8cd4d17c 80{\r
81 IoWrite8 (PCAT_RTC_ADDRESS_REGISTER, (UINT8) (Address | (UINT8) (IoRead8 (PCAT_RTC_ADDRESS_REGISTER) & 0x80)));\r
82 IoWrite8 (PCAT_RTC_DATA_REGISTER, Data);\r
83}\r
84\r
8d85dc31 85/**\r
86 Initialize RTC.\r
87\r
88 @param Global For global use inside this module.\r
89\r
90 @retval EFI_DEVICE_ERROR Initialization failed due to device error.\r
91 @retval EFI_SUCCESS Initialization successful.\r
92\r
93**/\r
8cd4d17c 94EFI_STATUS\r
95PcRtcInit (\r
96 IN PC_RTC_MODULE_GLOBALS *Global\r
97 )\r
8cd4d17c 98{\r
99 EFI_STATUS Status;\r
100 RTC_REGISTER_A RegisterA;\r
101 RTC_REGISTER_B RegisterB;\r
102 RTC_REGISTER_D RegisterD;\r
8cd4d17c 103 EFI_TIME Time;\r
ec35e997
LG
104 UINTN DataSize;\r
105 UINT32 TimerVar;\r
d431bf6e
EL
106 BOOLEAN Enabled;\r
107 BOOLEAN Pending;\r
8cd4d17c 108\r
109 //\r
110 // Acquire RTC Lock to make access to RTC atomic\r
111 //\r
8cd4d17c 112 if (!EfiAtRuntime ()) {\r
8d85dc31 113 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 114 }\r
115 //\r
116 // Initialize RTC Register\r
117 //\r
118 // Make sure Division Chain is properly configured,\r
119 // or RTC clock won't "tick" -- time won't increment\r
120 //\r
121 RegisterA.Data = RTC_INIT_REGISTER_A;\r
122 RtcWrite (RTC_ADDRESS_REGISTER_A, RegisterA.Data);\r
123\r
124 //\r
125 // Read Register B\r
126 //\r
127 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
128\r
129 //\r
130 // Clear RTC flag register\r
131 //\r
132 RtcRead (RTC_ADDRESS_REGISTER_C);\r
133\r
134 //\r
135 // Clear RTC register D\r
136 //\r
137 RegisterD.Data = RTC_INIT_REGISTER_D;\r
138 RtcWrite (RTC_ADDRESS_REGISTER_D, RegisterD.Data);\r
139\r
140 //\r
141 // Wait for up to 0.1 seconds for the RTC to be updated\r
142 //\r
f8ea3026 143 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 144 if (EFI_ERROR (Status)) {\r
6ff84d99 145 //\r
146 // Set the variable with default value if the RTC is functioning incorrectly.\r
147 //\r
148 Global->SavedTimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
149 Global->Daylight = 0;\r
8cd4d17c 150 if (!EfiAtRuntime ()) {\r
8d85dc31 151 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 152 }\r
153 return EFI_DEVICE_ERROR;\r
154 }\r
155 //\r
156 // Get the Time/Date/Daylight Savings values.\r
157 //\r
158 Time.Second = RtcRead (RTC_ADDRESS_SECONDS);\r
159 Time.Minute = RtcRead (RTC_ADDRESS_MINUTES);\r
160 Time.Hour = RtcRead (RTC_ADDRESS_HOURS);\r
161 Time.Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
162 Time.Month = RtcRead (RTC_ADDRESS_MONTH);\r
163 Time.Year = RtcRead (RTC_ADDRESS_YEAR);\r
164\r
8cd4d17c 165 //\r
166 // Set RTC configuration after get original time\r
ec35e997 167 // The value of bit AIE should be reserved.\r
8cd4d17c 168 //\r
3e2744e4
RN
169 RegisterB.Data = RTC_INIT_REGISTER_B | (RegisterB.Data & BIT5);\r
170 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
8cd4d17c 171\r
172 //\r
173 // Release RTC Lock.\r
174 //\r
8cd4d17c 175 if (!EfiAtRuntime ()) {\r
8d85dc31 176 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 177 }\r
254ba247 178 \r
6bfa178c 179 //\r
180 // Get the data of Daylight saving and time zone, if they have been\r
181 // stored in NV variable during previous boot.\r
182 //\r
183 DataSize = sizeof (UINT32);\r
184 Status = EfiGetVariable (\r
185 L"RTC",\r
186 &gEfiCallerIdGuid,\r
187 NULL,\r
188 &DataSize,\r
189 (VOID *) &TimerVar\r
190 );\r
191 if (!EFI_ERROR (Status)) {\r
192 Time.TimeZone = (INT16) TimerVar;\r
193 Time.Daylight = (UINT8) (TimerVar >> 16);\r
194 } else {\r
195 Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
196 Time.Daylight = 0; \r
197 }\r
198\r
8cd4d17c 199 //\r
200 // Validate time fields\r
201 //\r
fe320967 202 Status = ConvertRtcTimeToEfiTime (&Time, RegisterB);\r
254ba247 203 if (!EFI_ERROR (Status)) {\r
204 Status = RtcTimeFieldsValid (&Time);\r
205 }\r
8cd4d17c 206 if (EFI_ERROR (Status)) {\r
44d52203 207 //\r
208 // Report Status Code to indicate that the RTC has bad date and time\r
209 //\r
210 REPORT_STATUS_CODE (\r
211 EFI_ERROR_CODE | EFI_ERROR_MINOR,\r
212 (EFI_SOFTWARE_DXE_RT_DRIVER | EFI_SW_EC_BAD_DATE_TIME)\r
213 );\r
8cd4d17c 214 Time.Second = RTC_INIT_SECOND;\r
215 Time.Minute = RTC_INIT_MINUTE;\r
216 Time.Hour = RTC_INIT_HOUR;\r
217 Time.Day = RTC_INIT_DAY;\r
218 Time.Month = RTC_INIT_MONTH;\r
fe320967 219 Time.Year = PcdGet16 (PcdMinimalValidYear);\r
2ba53a4f 220 Time.Nanosecond = 0;\r
9bb1fced 221 Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE;\r
222 Time.Daylight = 0;\r
8cd4d17c 223 }\r
ec35e997 224\r
ec35e997 225 //\r
8cd4d17c 226 // Reset time value according to new RTC configuration\r
227 //\r
2ba53a4f 228 Status = PcRtcSetTime (&Time, Global);\r
d431bf6e
EL
229 if (EFI_ERROR (Status)) {\r
230 return EFI_DEVICE_ERROR;\r
231 }\r
232 \r
233 //\r
234 // Reset wakeup time value to valid state when wakeup alarm is disabled and wakeup time is invalid.\r
235 // Global variable has already had valid SavedTimeZone and Daylight,\r
236 // so we can use them to get and set wakeup time.\r
237 //\r
238 Status = PcRtcGetWakeupTime (&Enabled, &Pending, &Time, Global);\r
239 if ((Enabled) || (!EFI_ERROR (Status))) {\r
2ba53a4f 240 return EFI_SUCCESS;\r
d431bf6e
EL
241 }\r
242 \r
243 //\r
244 // When wakeup time is disabled and invalid, reset wakeup time register to valid state \r
245 // but keep wakeup alarm disabled.\r
246 //\r
247 Time.Second = RTC_INIT_SECOND;\r
248 Time.Minute = RTC_INIT_MINUTE;\r
249 Time.Hour = RTC_INIT_HOUR;\r
250 Time.Day = RTC_INIT_DAY;\r
251 Time.Month = RTC_INIT_MONTH;\r
fe320967 252 Time.Year = PcdGet16 (PcdMinimalValidYear);\r
d431bf6e
EL
253 Time.Nanosecond = 0;\r
254 Time.TimeZone = Global->SavedTimeZone;\r
255 Time.Daylight = Global->Daylight;;\r
256\r
257 //\r
258 // Acquire RTC Lock to make access to RTC atomic\r
259 //\r
260 if (!EfiAtRuntime ()) {\r
261 EfiAcquireLock (&Global->RtcLock);\r
262 }\r
263 //\r
264 // Wait for up to 0.1 seconds for the RTC to be updated\r
265 //\r
266 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
267 if (EFI_ERROR (Status)) {\r
268 if (!EfiAtRuntime ()) {\r
269 EfiReleaseLock (&Global->RtcLock);\r
270 }\r
2ba53a4f 271 return EFI_DEVICE_ERROR;\r
272 }\r
fe320967
RN
273\r
274 ConvertEfiTimeToRtcTime (&Time, RegisterB);\r
d431bf6e
EL
275\r
276 //\r
277 // Set the Y/M/D info to variable as it has no corresponding hw registers.\r
278 //\r
279 Status = EfiSetVariable (\r
280 L"RTCALARM",\r
281 &gEfiCallerIdGuid,\r
282 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
283 sizeof (Time),\r
284 &Time\r
285 );\r
286 if (EFI_ERROR (Status)) {\r
287 if (!EfiAtRuntime ()) {\r
288 EfiReleaseLock (&Global->RtcLock);\r
289 }\r
290 return EFI_DEVICE_ERROR;\r
291 }\r
292 \r
293 //\r
294 // Inhibit updates of the RTC\r
295 //\r
296 RegisterB.Bits.Set = 1;\r
297 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
298 \r
299 //\r
300 // Set RTC alarm time registers\r
301 //\r
302 RtcWrite (RTC_ADDRESS_SECONDS_ALARM, Time.Second);\r
303 RtcWrite (RTC_ADDRESS_MINUTES_ALARM, Time.Minute);\r
304 RtcWrite (RTC_ADDRESS_HOURS_ALARM, Time.Hour);\r
305\r
306 //\r
307 // Allow updates of the RTC registers\r
308 //\r
309 RegisterB.Bits.Set = 0;\r
310 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
311 \r
312 //\r
313 // Release RTC Lock.\r
314 //\r
315 if (!EfiAtRuntime ()) {\r
316 EfiReleaseLock (&Global->RtcLock);\r
317 }\r
318 return EFI_SUCCESS;\r
8cd4d17c 319}\r
320\r
8d85dc31 321/**\r
322 Returns the current time and date information, and the time-keeping capabilities\r
323 of the hardware platform.\r
8cd4d17c 324\r
8d85dc31 325 @param Time A pointer to storage to receive a snapshot of the current time.\r
326 @param Capabilities An optional pointer to a buffer to receive the real time clock\r
327 device's capabilities.\r
328 @param Global For global use inside this module.\r
8cd4d17c 329\r
8d85dc31 330 @retval EFI_SUCCESS The operation completed successfully.\r
331 @retval EFI_INVALID_PARAMETER Time is NULL.\r
332 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.\r
8cd4d17c 333\r
8d85dc31 334**/\r
335EFI_STATUS\r
336PcRtcGetTime (\r
337 OUT EFI_TIME *Time,\r
338 OUT EFI_TIME_CAPABILITIES *Capabilities, OPTIONAL\r
339 IN PC_RTC_MODULE_GLOBALS *Global\r
340 )\r
8cd4d17c 341{\r
342 EFI_STATUS Status;\r
343 RTC_REGISTER_B RegisterB;\r
8cd4d17c 344\r
345 //\r
346 // Check parameters for null pointer\r
347 //\r
348 if (Time == NULL) {\r
349 return EFI_INVALID_PARAMETER;\r
350\r
351 }\r
352 //\r
353 // Acquire RTC Lock to make access to RTC atomic\r
354 //\r
8cd4d17c 355 if (!EfiAtRuntime ()) {\r
8d85dc31 356 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 357 }\r
358 //\r
359 // Wait for up to 0.1 seconds for the RTC to be updated\r
360 //\r
f8ea3026 361 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 362 if (EFI_ERROR (Status)) {\r
8cd4d17c 363 if (!EfiAtRuntime ()) {\r
8d85dc31 364 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 365 }\r
366 return Status;\r
367 }\r
368 //\r
369 // Read Register B\r
370 //\r
371 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
372\r
373 //\r
374 // Get the Time/Date/Daylight Savings values.\r
375 //\r
376 Time->Second = RtcRead (RTC_ADDRESS_SECONDS);\r
377 Time->Minute = RtcRead (RTC_ADDRESS_MINUTES);\r
378 Time->Hour = RtcRead (RTC_ADDRESS_HOURS);\r
379 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
380 Time->Month = RtcRead (RTC_ADDRESS_MONTH);\r
381 Time->Year = RtcRead (RTC_ADDRESS_YEAR);\r
382\r
8cd4d17c 383 //\r
384 // Release RTC Lock.\r
385 //\r
8cd4d17c 386 if (!EfiAtRuntime ()) {\r
8d85dc31 387 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 388 }\r
6bfa178c 389\r
8cd4d17c 390 //\r
2d4117c0 391 // Get the variable that contains the TimeZone and Daylight fields\r
8cd4d17c 392 //\r
393 Time->TimeZone = Global->SavedTimeZone;\r
394 Time->Daylight = Global->Daylight;\r
395\r
396 //\r
397 // Make sure all field values are in correct range\r
398 //\r
fe320967 399 Status = ConvertRtcTimeToEfiTime (Time, RegisterB);\r
254ba247 400 if (!EFI_ERROR (Status)) {\r
401 Status = RtcTimeFieldsValid (Time);\r
402 }\r
8cd4d17c 403 if (EFI_ERROR (Status)) {\r
404 return EFI_DEVICE_ERROR;\r
405 }\r
6bfa178c 406\r
8cd4d17c 407 //\r
408 // Fill in Capabilities if it was passed in\r
409 //\r
8d85dc31 410 if (Capabilities != NULL) {\r
8cd4d17c 411 Capabilities->Resolution = 1;\r
412 //\r
413 // 1 hertz\r
414 //\r
415 Capabilities->Accuracy = 50000000;\r
416 //\r
417 // 50 ppm\r
418 //\r
419 Capabilities->SetsToZero = FALSE;\r
420 }\r
421\r
422 return EFI_SUCCESS;\r
423}\r
424\r
8d85dc31 425/**\r
426 Sets the current local time and date information.\r
427\r
428 @param Time A pointer to the current time.\r
429 @param Global For global use inside this module.\r
430\r
431 @retval EFI_SUCCESS The operation completed successfully.\r
432 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
433 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.\r
434\r
435**/\r
8cd4d17c 436EFI_STATUS\r
437PcRtcSetTime (\r
438 IN EFI_TIME *Time,\r
439 IN PC_RTC_MODULE_GLOBALS *Global\r
440 )\r
8cd4d17c 441{\r
442 EFI_STATUS Status;\r
443 EFI_TIME RtcTime;\r
444 RTC_REGISTER_B RegisterB;\r
ec35e997 445 UINT32 TimerVar;\r
8cd4d17c 446\r
447 if (Time == NULL) {\r
448 return EFI_INVALID_PARAMETER;\r
449 }\r
450 //\r
451 // Make sure that the time fields are valid\r
452 //\r
453 Status = RtcTimeFieldsValid (Time);\r
454 if (EFI_ERROR (Status)) {\r
455 return Status;\r
456 }\r
457\r
458 CopyMem (&RtcTime, Time, sizeof (EFI_TIME));\r
459\r
460 //\r
461 // Acquire RTC Lock to make access to RTC atomic\r
462 //\r
8cd4d17c 463 if (!EfiAtRuntime ()) {\r
8d85dc31 464 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 465 }\r
466 //\r
467 // Wait for up to 0.1 seconds for the RTC to be updated\r
468 //\r
f8ea3026 469 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 470 if (EFI_ERROR (Status)) {\r
8cd4d17c 471 if (!EfiAtRuntime ()) {\r
ca08e40b 472 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 473 }\r
474 return Status;\r
475 }\r
0e991a2f
EL
476 \r
477 //\r
478 // Write timezone and daylight to RTC variable\r
479 //\r
480 TimerVar = Time->Daylight;\r
481 TimerVar = (UINT32) ((TimerVar << 16) | (UINT16)(Time->TimeZone));\r
482 Status = EfiSetVariable (\r
483 L"RTC",\r
484 &gEfiCallerIdGuid,\r
485 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
486 sizeof (TimerVar),\r
487 &TimerVar\r
488 );\r
489 if (EFI_ERROR (Status)) {\r
490 if (!EfiAtRuntime ()) {\r
491 EfiReleaseLock (&Global->RtcLock);\r
492 }\r
493 return EFI_DEVICE_ERROR;\r
494 }\r
495\r
8cd4d17c 496 //\r
497 // Read Register B, and inhibit updates of the RTC\r
498 //\r
499 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
24115e44 500 RegisterB.Bits.Set = 1;\r
8cd4d17c 501 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
502\r
fe320967 503 ConvertEfiTimeToRtcTime (&RtcTime, RegisterB);\r
8cd4d17c 504\r
505 RtcWrite (RTC_ADDRESS_SECONDS, RtcTime.Second);\r
506 RtcWrite (RTC_ADDRESS_MINUTES, RtcTime.Minute);\r
507 RtcWrite (RTC_ADDRESS_HOURS, RtcTime.Hour);\r
508 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day);\r
509 RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month);\r
510 RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year);\r
8cd4d17c 511\r
512 //\r
513 // Allow updates of the RTC registers\r
514 //\r
24115e44 515 RegisterB.Bits.Set = 0;\r
8cd4d17c 516 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
517\r
518 //\r
519 // Release RTC Lock.\r
520 //\r
8cd4d17c 521 if (!EfiAtRuntime ()) {\r
8d85dc31 522 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 523 }\r
524 //\r
2d4117c0 525 // Set the variable that contains the TimeZone and Daylight fields\r
8cd4d17c 526 //\r
527 Global->SavedTimeZone = Time->TimeZone;\r
528 Global->Daylight = Time->Daylight;\r
ec35e997 529\r
ec35e997 530 return EFI_SUCCESS;\r
8cd4d17c 531}\r
532\r
8d85dc31 533/**\r
534 Returns the current wakeup alarm clock setting.\r
535\r
536 @param Enabled Indicates if the alarm is currently enabled or disabled.\r
2d4117c0 537 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.\r
8d85dc31 538 @param Time The current alarm setting.\r
539 @param Global For global use inside this module.\r
540\r
541 @retval EFI_SUCCESS The alarm settings were returned.\r
542 @retval EFI_INVALID_PARAMETER Enabled is NULL.\r
543 @retval EFI_INVALID_PARAMETER Pending is NULL.\r
544 @retval EFI_INVALID_PARAMETER Time is NULL.\r
545 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.\r
546 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
547\r
548**/\r
8cd4d17c 549EFI_STATUS\r
550PcRtcGetWakeupTime (\r
551 OUT BOOLEAN *Enabled,\r
552 OUT BOOLEAN *Pending,\r
553 OUT EFI_TIME *Time,\r
8d85dc31 554 IN PC_RTC_MODULE_GLOBALS *Global\r
8cd4d17c 555 )\r
8cd4d17c 556{\r
557 EFI_STATUS Status;\r
558 RTC_REGISTER_B RegisterB;\r
559 RTC_REGISTER_C RegisterC;\r
7018623c 560 EFI_TIME RtcTime;\r
561 UINTN DataSize;\r
8cd4d17c 562\r
563 //\r
2d4117c0 564 // Check parameters for null pointers\r
8cd4d17c 565 //\r
566 if ((Enabled == NULL) || (Pending == NULL) || (Time == NULL)) {\r
567 return EFI_INVALID_PARAMETER;\r
568\r
569 }\r
570 //\r
571 // Acquire RTC Lock to make access to RTC atomic\r
572 //\r
8cd4d17c 573 if (!EfiAtRuntime ()) {\r
8d85dc31 574 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 575 }\r
576 //\r
577 // Wait for up to 0.1 seconds for the RTC to be updated\r
578 //\r
f8ea3026 579 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 580 if (EFI_ERROR (Status)) {\r
8cd4d17c 581 if (!EfiAtRuntime ()) {\r
582 EfiReleaseLock (&Global->RtcLock);\r
583 }\r
584 return EFI_DEVICE_ERROR;\r
585 }\r
586 //\r
587 // Read Register B and Register C\r
588 //\r
589 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
590 RegisterC.Data = RtcRead (RTC_ADDRESS_REGISTER_C);\r
591\r
592 //\r
593 // Get the Time/Date/Daylight Savings values.\r
594 //\r
24115e44 595 *Enabled = RegisterB.Bits.Aie;\r
7018623c 596 *Pending = RegisterC.Bits.Af;\r
597\r
598 Time->Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
599 Time->Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
600 Time->Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
601 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
602 Time->Month = RtcRead (RTC_ADDRESS_MONTH);\r
603 Time->Year = RtcRead (RTC_ADDRESS_YEAR);\r
604 Time->TimeZone = Global->SavedTimeZone;\r
605 Time->Daylight = Global->Daylight;\r
8cd4d17c 606\r
8cd4d17c 607 //\r
7018623c 608 // Get the alarm info from variable\r
8cd4d17c 609 //\r
ffa1e783 610 DataSize = sizeof (EFI_TIME);\r
7018623c 611 Status = EfiGetVariable (\r
612 L"RTCALARM",\r
613 &gEfiCallerIdGuid,\r
614 NULL,\r
615 &DataSize,\r
616 &RtcTime\r
617 );\r
618 if (!EFI_ERROR (Status)) {\r
619 //\r
620 // The alarm variable exists. In this case, we read variable to get info.\r
621 //\r
622 Time->Day = RtcTime.Day;\r
623 Time->Month = RtcTime.Month;\r
624 Time->Year = RtcTime.Year;\r
8cd4d17c 625 }\r
6bfa178c 626\r
627 //\r
7018623c 628 // Release RTC Lock.\r
6bfa178c 629 //\r
7018623c 630 if (!EfiAtRuntime ()) {\r
631 EfiReleaseLock (&Global->RtcLock);\r
632 }\r
6bfa178c 633\r
8cd4d17c 634 //\r
635 // Make sure all field values are in correct range\r
636 //\r
fe320967 637 Status = ConvertRtcTimeToEfiTime (Time, RegisterB);\r
254ba247 638 if (!EFI_ERROR (Status)) {\r
639 Status = RtcTimeFieldsValid (Time);\r
640 }\r
8cd4d17c 641 if (EFI_ERROR (Status)) {\r
642 return EFI_DEVICE_ERROR;\r
643 }\r
644\r
8cd4d17c 645 return EFI_SUCCESS;\r
646}\r
647\r
8d85dc31 648/**\r
649 Sets the system wakeup alarm clock time.\r
650\r
651 @param Enabled Enable or disable the wakeup alarm.\r
652 @param Time If Enable is TRUE, the time to set the wakeup alarm for.\r
653 If Enable is FALSE, then this parameter is optional, and may be NULL.\r
654 @param Global For global use inside this module.\r
655\r
656 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.\r
657 If Enable is FALSE, then the wakeup alarm was disabled.\r
658 @retval EFI_INVALID_PARAMETER A time field is out of range.\r
659 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.\r
660 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.\r
661\r
662**/\r
8cd4d17c 663EFI_STATUS\r
664PcRtcSetWakeupTime (\r
665 IN BOOLEAN Enable,\r
8d85dc31 666 IN EFI_TIME *Time, OPTIONAL\r
8cd4d17c 667 IN PC_RTC_MODULE_GLOBALS *Global\r
668 )\r
8cd4d17c 669{\r
670 EFI_STATUS Status;\r
671 EFI_TIME RtcTime;\r
672 RTC_REGISTER_B RegisterB;\r
8cd4d17c 673 EFI_TIME_CAPABILITIES Capabilities;\r
674\r
aca70749 675 ZeroMem (&RtcTime, sizeof (RtcTime));\r
7018623c 676\r
8cd4d17c 677 if (Enable) {\r
678\r
679 if (Time == NULL) {\r
680 return EFI_INVALID_PARAMETER;\r
681 }\r
682 //\r
683 // Make sure that the time fields are valid\r
684 //\r
685 Status = RtcTimeFieldsValid (Time);\r
686 if (EFI_ERROR (Status)) {\r
687 return EFI_INVALID_PARAMETER;\r
688 }\r
689 //\r
690 // Just support set alarm time within 24 hours\r
691 //\r
692 PcRtcGetTime (&RtcTime, &Capabilities, Global);\r
79864f54 693 Status = RtcTimeFieldsValid (&RtcTime);\r
694 if (EFI_ERROR (Status)) {\r
695 return EFI_DEVICE_ERROR;\r
696 }\r
8cd4d17c 697 if (!IsWithinOneDay (&RtcTime, Time)) {\r
698 return EFI_UNSUPPORTED;\r
699 }\r
700 //\r
701 // Make a local copy of the time and date\r
702 //\r
703 CopyMem (&RtcTime, Time, sizeof (EFI_TIME));\r
704\r
705 }\r
706 //\r
707 // Acquire RTC Lock to make access to RTC atomic\r
708 //\r
8cd4d17c 709 if (!EfiAtRuntime ()) {\r
8d85dc31 710 EfiAcquireLock (&Global->RtcLock);\r
8cd4d17c 711 }\r
712 //\r
713 // Wait for up to 0.1 seconds for the RTC to be updated\r
714 //\r
f8ea3026 715 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));\r
8cd4d17c 716 if (EFI_ERROR (Status)) {\r
8cd4d17c 717 if (!EfiAtRuntime ()) {\r
718 EfiReleaseLock (&Global->RtcLock);\r
719 }\r
720 return EFI_DEVICE_ERROR;\r
721 }\r
722 //\r
0e991a2f 723 // Read Register B\r
8cd4d17c 724 //\r
0e991a2f 725 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);\r
8cd4d17c 726\r
727 if (Enable) {\r
fe320967 728 ConvertEfiTimeToRtcTime (&RtcTime, RegisterB);\r
8cd4d17c 729 } else {\r
7018623c 730 //\r
731 // if the alarm is disable, record the current setting.\r
732 //\r
733 RtcTime.Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);\r
734 RtcTime.Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);\r
735 RtcTime.Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);\r
736 RtcTime.Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);\r
737 RtcTime.Month = RtcRead (RTC_ADDRESS_MONTH);\r
738 RtcTime.Year = RtcRead (RTC_ADDRESS_YEAR);\r
739 RtcTime.TimeZone = Global->SavedTimeZone;\r
740 RtcTime.Daylight = Global->Daylight;\r
8cd4d17c 741 }\r
8cd4d17c 742\r
7018623c 743 //\r
744 // Set the Y/M/D info to variable as it has no corresponding hw registers.\r
745 //\r
746 Status = EfiSetVariable (\r
747 L"RTCALARM",\r
748 &gEfiCallerIdGuid,\r
749 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,\r
750 sizeof (RtcTime),\r
751 &RtcTime\r
752 );\r
753 if (EFI_ERROR (Status)) {\r
0e991a2f
EL
754 if (!EfiAtRuntime ()) {\r
755 EfiReleaseLock (&Global->RtcLock);\r
756 }\r
7018623c 757 return EFI_DEVICE_ERROR;\r
758 }\r
0e991a2f
EL
759 \r
760 //\r
761 // Inhibit updates of the RTC\r
762 //\r
763 RegisterB.Bits.Set = 1;\r
764 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
765\r
766 if (Enable) {\r
767 //\r
768 // Set RTC alarm time\r
769 //\r
770 RtcWrite (RTC_ADDRESS_SECONDS_ALARM, RtcTime.Second);\r
771 RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute);\r
772 RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour);\r
773\r
774 RegisterB.Bits.Aie = 1;\r
775\r
776 } else {\r
777 RegisterB.Bits.Aie = 0;\r
778 }\r
779 //\r
780 // Allow updates of the RTC registers\r
781 //\r
782 RegisterB.Bits.Set = 0;\r
783 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);\r
7018623c 784\r
8cd4d17c 785 //\r
786 // Release RTC Lock.\r
787 //\r
8cd4d17c 788 if (!EfiAtRuntime ()) {\r
8d85dc31 789 EfiReleaseLock (&Global->RtcLock);\r
8cd4d17c 790 }\r
791 return EFI_SUCCESS;\r
792}\r
793\r
8cd4d17c 794\r
254ba247 795/**\r
796 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.\r
797\r
798 This function checks the 8-bit BCD value specified by Value.\r
799 If valid, the function converts it to an 8-bit value and returns it.\r
800 Otherwise, return 0xff.\r
801\r
8d85dc31 802 @param Value The 8-bit BCD value to check and convert\r
254ba247 803\r
8d85dc31 804 @return The 8-bit value converted. Or 0xff if Value is invalid.\r
254ba247 805\r
806**/\r
807UINT8\r
808CheckAndConvertBcd8ToDecimal8 (\r
809 IN UINT8 Value\r
8cd4d17c 810 )\r
254ba247 811{\r
812 if ((Value < 0xa0) && ((Value & 0xf) < 0xa)) {\r
813 return BcdToDecimal8 (Value);\r
814 }\r
8cd4d17c 815\r
254ba247 816 return 0xff;\r
817}\r
8cd4d17c 818\r
254ba247 819/**\r
820 Converts time read from RTC to EFI_TIME format defined by UEFI spec.\r
8cd4d17c 821\r
254ba247 822 This function converts raw time data read from RTC to the EFI_TIME format\r
823 defined by UEFI spec.\r
824 If data mode of RTC is BCD, then converts it to decimal,\r
825 If RTC is in 12-hour format, then converts it to 24-hour format.\r
8cd4d17c 826\r
254ba247 827 @param Time On input, the time data read from RTC to convert\r
828 On output, the time converted to UEFI format\r
254ba247 829 @param RegisterB Value of Register B of RTC, indicating data mode\r
830 and hour format.\r
8cd4d17c 831\r
8d85dc31 832 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.\r
833 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.\r
834\r
254ba247 835**/\r
836EFI_STATUS\r
837ConvertRtcTimeToEfiTime (\r
838 IN OUT EFI_TIME *Time,\r
254ba247 839 IN RTC_REGISTER_B RegisterB\r
840 )\r
8cd4d17c 841{\r
8d85dc31 842 BOOLEAN IsPM;\r
fe320967 843 UINT8 Century;\r
8cd4d17c 844\r
21176834 845 if ((Time->Hour & 0x80) != 0) {\r
8d85dc31 846 IsPM = TRUE;\r
8cd4d17c 847 } else {\r
8d85dc31 848 IsPM = FALSE;\r
8cd4d17c 849 }\r
850\r
851 Time->Hour = (UINT8) (Time->Hour & 0x7f);\r
852\r
24115e44 853 if (RegisterB.Bits.Dm == 0) {\r
254ba247 854 Time->Year = CheckAndConvertBcd8ToDecimal8 ((UINT8) Time->Year);\r
855 Time->Month = CheckAndConvertBcd8ToDecimal8 (Time->Month);\r
856 Time->Day = CheckAndConvertBcd8ToDecimal8 (Time->Day);\r
857 Time->Hour = CheckAndConvertBcd8ToDecimal8 (Time->Hour);\r
858 Time->Minute = CheckAndConvertBcd8ToDecimal8 (Time->Minute);\r
859 Time->Second = CheckAndConvertBcd8ToDecimal8 (Time->Second);\r
254ba247 860 }\r
861\r
862 if (Time->Year == 0xff || Time->Month == 0xff || Time->Day == 0xff ||\r
fe320967 863 Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff) {\r
254ba247 864 return EFI_INVALID_PARAMETER;\r
8cd4d17c 865 }\r
254ba247 866\r
fe320967
RN
867 //\r
868 // For minimal/maximum year range [1970, 2069],\r
869 // Century is 19 if RTC year >= 70,\r
870 // Century is 20 otherwise.\r
871 //\r
872 Century = (UINT8) (PcdGet16 (PcdMinimalValidYear) / 100);\r
873 if (Time->Year < PcdGet16 (PcdMinimalValidYear) % 100) {\r
874 Century++;\r
875 }\r
254ba247 876 Time->Year = (UINT16) (Century * 100 + Time->Year);\r
877\r
8cd4d17c 878 //\r
879 // If time is in 12 hour format, convert it to 24 hour format\r
880 //\r
24115e44 881 if (RegisterB.Bits.Mil == 0) {\r
8d85dc31 882 if (IsPM && Time->Hour < 12) {\r
8cd4d17c 883 Time->Hour = (UINT8) (Time->Hour + 12);\r
884 }\r
885\r
8d85dc31 886 if (!IsPM && Time->Hour == 12) {\r
8cd4d17c 887 Time->Hour = 0;\r
888 }\r
889 }\r
890\r
891 Time->Nanosecond = 0;\r
254ba247 892\r
893 return EFI_SUCCESS;\r
8cd4d17c 894}\r
895\r
8d85dc31 896/**\r
897 Wait for a period for the RTC to be ready.\r
898\r
899 @param Timeout Tell how long it should take to wait.\r
900\r
901 @retval EFI_DEVICE_ERROR RTC device error.\r
902 @retval EFI_SUCCESS RTC is updated and ready. \r
903**/\r
8cd4d17c 904EFI_STATUS\r
905RtcWaitToUpdate (\r
906 UINTN Timeout\r
907 )\r
8cd4d17c 908{\r
909 RTC_REGISTER_A RegisterA;\r
910 RTC_REGISTER_D RegisterD;\r
911\r
912 //\r
913 // See if the RTC is functioning correctly\r
914 //\r
915 RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
916\r
24115e44 917 if (RegisterD.Bits.Vrt == 0) {\r
8cd4d17c 918 return EFI_DEVICE_ERROR;\r
919 }\r
920 //\r
921 // Wait for up to 0.1 seconds for the RTC to be ready.\r
922 //\r
923 Timeout = (Timeout / 10) + 1;\r
924 RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
24115e44 925 while (RegisterA.Bits.Uip == 1 && Timeout > 0) {\r
8cd4d17c 926 MicroSecondDelay (10);\r
927 RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);\r
928 Timeout--;\r
929 }\r
930\r
931 RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);\r
24115e44 932 if (Timeout == 0 || RegisterD.Bits.Vrt == 0) {\r
8cd4d17c 933 return EFI_DEVICE_ERROR;\r
934 }\r
935\r
936 return EFI_SUCCESS;\r
937}\r
938\r
8d85dc31 939/**\r
940 See if all fields of a variable of EFI_TIME type is correct.\r
941\r
942 @param Time The time to be checked.\r
943\r
944 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.\r
945 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.\r
946\r
947**/\r
8cd4d17c 948EFI_STATUS\r
949RtcTimeFieldsValid (\r
950 IN EFI_TIME *Time\r
951 )\r
8cd4d17c 952{\r
1e5fff63
EL
953 if (Time->Year < PcdGet16 (PcdMinimalValidYear) ||\r
954 Time->Year > PcdGet16 (PcdMaximalValidYear) ||\r
8cd4d17c 955 Time->Month < 1 ||\r
956 Time->Month > 12 ||\r
6bfa178c 957 (!DayValid (Time)) ||\r
8cd4d17c 958 Time->Hour > 23 ||\r
959 Time->Minute > 59 ||\r
960 Time->Second > 59 ||\r
961 Time->Nanosecond > 999999999 ||\r
962 (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||\r
6bfa178c 963 ((Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT))) != 0)) {\r
964 return EFI_INVALID_PARAMETER;\r
8cd4d17c 965 }\r
966\r
967 return EFI_SUCCESS;\r
968}\r
969\r
8d85dc31 970/**\r
971 See if field Day of an EFI_TIME is correct.\r
972\r
973 @param Time Its Day field is to be checked.\r
974\r
975 @retval TRUE Day field of Time is correct.\r
976 @retval FALSE Day field of Time is NOT correct.\r
977**/\r
8cd4d17c 978BOOLEAN\r
979DayValid (\r
980 IN EFI_TIME *Time\r
981 )\r
8cd4d17c 982{\r
558bfebb 983 INTN DayOfMonth[12];\r
984\r
985 DayOfMonth[0] = 31;\r
986 DayOfMonth[1] = 29;\r
987 DayOfMonth[2] = 31;\r
988 DayOfMonth[3] = 30;\r
989 DayOfMonth[4] = 31;\r
990 DayOfMonth[5] = 30;\r
991 DayOfMonth[6] = 31;\r
992 DayOfMonth[7] = 31;\r
993 DayOfMonth[8] = 30;\r
994 DayOfMonth[9] = 31;\r
995 DayOfMonth[10] = 30;\r
996 DayOfMonth[11] = 31;\r
8cd4d17c 997\r
96a5ac5b 998 //\r
999 // The validity of Time->Month field should be checked before\r
1000 //\r
1001 ASSERT (Time->Month >=1);\r
1002 ASSERT (Time->Month <=12);\r
8cd4d17c 1003 if (Time->Day < 1 ||\r
1004 Time->Day > DayOfMonth[Time->Month - 1] ||\r
1005 (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))\r
1006 ) {\r
1007 return FALSE;\r
1008 }\r
1009\r
1010 return TRUE;\r
1011}\r
1012\r
8d85dc31 1013/**\r
2d4117c0 1014 Check if it is a leap year.\r
8d85dc31 1015\r
1016 @param Time The time to be checked.\r
1017\r
2d4117c0 1018 @retval TRUE It is a leap year.\r
1019 @retval FALSE It is NOT a leap year.\r
8d85dc31 1020**/\r
8cd4d17c 1021BOOLEAN\r
1022IsLeapYear (\r
1023 IN EFI_TIME *Time\r
1024 )\r
8cd4d17c 1025{\r
1026 if (Time->Year % 4 == 0) {\r
1027 if (Time->Year % 100 == 0) {\r
1028 if (Time->Year % 400 == 0) {\r
1029 return TRUE;\r
1030 } else {\r
1031 return FALSE;\r
1032 }\r
1033 } else {\r
1034 return TRUE;\r
1035 }\r
1036 } else {\r
1037 return FALSE;\r
1038 }\r
1039}\r
1040\r
8d85dc31 1041/**\r
1042 Converts time from EFI_TIME format defined by UEFI spec to RTC's.\r
8cd4d17c 1043\r
8d85dc31 1044 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.\r
1045 If data mode of RTC is BCD, then converts EFI_TIME to it.\r
1046 If RTC is in 12-hour format, then converts EFI_TIME to it.\r
8cd4d17c 1047\r
8d85dc31 1048 @param Time On input, the time data read from UEFI to convert\r
1049 On output, the time converted to RTC format\r
1050 @param RegisterB Value of Register B of RTC, indicating data mode\r
8d85dc31 1051**/\r
1052VOID\r
1053ConvertEfiTimeToRtcTime (\r
1054 IN OUT EFI_TIME *Time,\r
fe320967 1055 IN RTC_REGISTER_B RegisterB\r
8d85dc31 1056 )\r
8cd4d17c 1057{\r
8d85dc31 1058 BOOLEAN IsPM;\r
8cd4d17c 1059\r
8d85dc31 1060 IsPM = TRUE;\r
8cd4d17c 1061 //\r
8d85dc31 1062 // Adjust hour field if RTC is in 12 hour mode\r
8cd4d17c 1063 //\r
24115e44 1064 if (RegisterB.Bits.Mil == 0) {\r
8cd4d17c 1065 if (Time->Hour < 12) {\r
8d85dc31 1066 IsPM = FALSE;\r
8cd4d17c 1067 }\r
1068\r
1069 if (Time->Hour >= 13) {\r
1070 Time->Hour = (UINT8) (Time->Hour - 12);\r
1071 } else if (Time->Hour == 0) {\r
1072 Time->Hour = 12;\r
1073 }\r
1074 }\r
1075 //\r
fe320967 1076 // Set the Time/Date values.\r
8cd4d17c 1077 //\r
8cd4d17c 1078 Time->Year = (UINT16) (Time->Year % 100);\r
1079\r
24115e44 1080 if (RegisterB.Bits.Dm == 0) {\r
8cd4d17c 1081 Time->Year = DecimalToBcd8 ((UINT8) Time->Year);\r
1082 Time->Month = DecimalToBcd8 (Time->Month);\r
1083 Time->Day = DecimalToBcd8 (Time->Day);\r
1084 Time->Hour = DecimalToBcd8 (Time->Hour);\r
1085 Time->Minute = DecimalToBcd8 (Time->Minute);\r
1086 Time->Second = DecimalToBcd8 (Time->Second);\r
1087 }\r
1088 //\r
1089 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.\r
1090 //\r
24115e44 1091 if (RegisterB.Bits.Mil == 0 && IsPM) {\r
8cd4d17c 1092 Time->Hour = (UINT8) (Time->Hour | 0x80);\r
1093 }\r
1094}\r
1095\r
8d85dc31 1096/**\r
1097 Compare the Hour, Minute and Second of the From time and the To time.\r
1098 \r
1099 Only compare H/M/S in EFI_TIME and ignore other fields here.\r
1100\r
1101 @param From the first time\r
1102 @param To the second time\r
1103\r
1104 @return >0 The H/M/S of the From time is later than those of To time\r
1105 @return ==0 The H/M/S of the From time is same as those of To time\r
1106 @return <0 The H/M/S of the From time is earlier than those of To time\r
1107**/\r
8cd4d17c 1108INTN\r
1109CompareHMS (\r
1110 IN EFI_TIME *From,\r
1111 IN EFI_TIME *To\r
1112 )\r
8cd4d17c 1113{\r
1114 if ((From->Hour > To->Hour) ||\r
1115 ((From->Hour == To->Hour) && (From->Minute > To->Minute)) ||\r
1116 ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second > To->Second))) {\r
1117 return 1;\r
1118 } else if ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second == To->Second)) {\r
1119 return 0;\r
1120 } else {\r
1121 return -1;\r
1122 }\r
1123}\r
1124\r
8d85dc31 1125/**\r
1126 To check if second date is later than first date within 24 hours.\r
1127\r
1128 @param From the first date\r
1129 @param To the second date\r
1130\r
1131 @retval TRUE From is previous to To within 24 hours.\r
1132 @retval FALSE From is later, or it is previous to To more than 24 hours.\r
1133**/\r
8cd4d17c 1134BOOLEAN\r
1135IsWithinOneDay (\r
1136 IN EFI_TIME *From,\r
1137 IN EFI_TIME *To\r
1138 )\r
8cd4d17c 1139{\r
558bfebb 1140 UINT8 DayOfMonth[12];\r
1f4cf7b1 1141 BOOLEAN Adjacent;\r
1142\r
558bfebb 1143 DayOfMonth[0] = 31;\r
1144 DayOfMonth[1] = 29;\r
1145 DayOfMonth[2] = 31;\r
1146 DayOfMonth[3] = 30;\r
1147 DayOfMonth[4] = 31;\r
1148 DayOfMonth[5] = 30;\r
1149 DayOfMonth[6] = 31;\r
1150 DayOfMonth[7] = 31;\r
1151 DayOfMonth[8] = 30;\r
1152 DayOfMonth[9] = 31;\r
1153 DayOfMonth[10] = 30;\r
1154 DayOfMonth[11] = 31;\r
1155\r
1f4cf7b1 1156 Adjacent = FALSE;\r
8cd4d17c 1157\r
96a5ac5b 1158 //\r
4cfc3293 1159 // The validity of From->Month field should be checked before\r
96a5ac5b 1160 //\r
1161 ASSERT (From->Month >=1);\r
1162 ASSERT (From->Month <=12);\r
1163 \r
8cd4d17c 1164 if (From->Year == To->Year) {\r
1165 if (From->Month == To->Month) {\r
1166 if ((From->Day + 1) == To->Day) {\r
1167 if ((CompareHMS(From, To) >= 0)) {\r
1168 Adjacent = TRUE;\r
1169 }\r
1170 } else if (From->Day == To->Day) {\r
1171 if ((CompareHMS(From, To) <= 0)) {\r
1172 Adjacent = TRUE;\r
1173 }\r
1174 }\r
1175 } else if (((From->Month + 1) == To->Month) && (To->Day == 1)) {\r
1176 if ((From->Month == 2) && !IsLeapYear(From)) {\r
1177 if (From->Day == 28) {\r
1178 if ((CompareHMS(From, To) >= 0)) {\r
1179 Adjacent = TRUE;\r
1180 }\r
1181 }\r
1182 } else if (From->Day == DayOfMonth[From->Month - 1]) {\r
1183 if ((CompareHMS(From, To) >= 0)) {\r
1184 Adjacent = TRUE;\r
1185 }\r
1186 }\r
1187 }\r
1188 } else if (((From->Year + 1) == To->Year) &&\r
1189 (From->Month == 12) &&\r
1190 (From->Day == 31) &&\r
1191 (To->Month == 1) &&\r
1192 (To->Day == 1)) {\r
1193 if ((CompareHMS(From, To) >= 0)) {\r
1194 Adjacent = TRUE;\r
1195 }\r
1196 }\r
1197\r
1198 return Adjacent;\r
1199}\r
1200\r