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