]> git.proxmox.com Git - mirror_edk2.git/blob - PcAtChipsetPkg/PcatRealTimeClockRuntimeDxe/PcRtc.c
Did proper error handling when SetVariable failed, and put RTC write operation at...
[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 - 2014, 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 //
208 // Report Status Code to indicate that the RTC has bad date and time
209 //
210 REPORT_STATUS_CODE (
211 EFI_ERROR_CODE | EFI_ERROR_MINOR,
212 (EFI_SOFTWARE_DXE_RT_DRIVER | EFI_SW_EC_BAD_DATE_TIME)
213 );
214 Time.Second = RTC_INIT_SECOND;
215 Time.Minute = RTC_INIT_MINUTE;
216 Time.Hour = RTC_INIT_HOUR;
217 Time.Day = RTC_INIT_DAY;
218 Time.Month = RTC_INIT_MONTH;
219 Time.Year = RTC_INIT_YEAR;
220 Time.Nanosecond = 0;
221 Time.TimeZone = EFI_UNSPECIFIED_TIMEZONE;
222 Time.Daylight = 0;
223 }
224
225 //
226 // Reset time value according to new RTC configuration
227 //
228 Status = PcRtcSetTime (&Time, Global);
229 if(!EFI_ERROR (Status)) {
230 return EFI_SUCCESS;
231 } else {
232 return EFI_DEVICE_ERROR;
233 }
234 }
235
236 /**
237 Returns the current time and date information, and the time-keeping capabilities
238 of the hardware platform.
239
240 @param Time A pointer to storage to receive a snapshot of the current time.
241 @param Capabilities An optional pointer to a buffer to receive the real time clock
242 device's capabilities.
243 @param Global For global use inside this module.
244
245 @retval EFI_SUCCESS The operation completed successfully.
246 @retval EFI_INVALID_PARAMETER Time is NULL.
247 @retval EFI_DEVICE_ERROR The time could not be retrieved due to hardware error.
248
249 **/
250 EFI_STATUS
251 PcRtcGetTime (
252 OUT EFI_TIME *Time,
253 OUT EFI_TIME_CAPABILITIES *Capabilities, OPTIONAL
254 IN PC_RTC_MODULE_GLOBALS *Global
255 )
256 {
257 EFI_STATUS Status;
258 RTC_REGISTER_B RegisterB;
259 UINT8 Century;
260
261 //
262 // Check parameters for null pointer
263 //
264 if (Time == NULL) {
265 return EFI_INVALID_PARAMETER;
266
267 }
268 //
269 // Acquire RTC Lock to make access to RTC atomic
270 //
271 if (!EfiAtRuntime ()) {
272 EfiAcquireLock (&Global->RtcLock);
273 }
274 //
275 // Wait for up to 0.1 seconds for the RTC to be updated
276 //
277 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));
278 if (EFI_ERROR (Status)) {
279 if (!EfiAtRuntime ()) {
280 EfiReleaseLock (&Global->RtcLock);
281 }
282 return Status;
283 }
284 //
285 // Read Register B
286 //
287 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);
288
289 //
290 // Get the Time/Date/Daylight Savings values.
291 //
292 Time->Second = RtcRead (RTC_ADDRESS_SECONDS);
293 Time->Minute = RtcRead (RTC_ADDRESS_MINUTES);
294 Time->Hour = RtcRead (RTC_ADDRESS_HOURS);
295 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);
296 Time->Month = RtcRead (RTC_ADDRESS_MONTH);
297 Time->Year = RtcRead (RTC_ADDRESS_YEAR);
298
299 Century = RtcRead (RTC_ADDRESS_CENTURY);
300
301 //
302 // Release RTC Lock.
303 //
304 if (!EfiAtRuntime ()) {
305 EfiReleaseLock (&Global->RtcLock);
306 }
307
308 //
309 // Get the variable that contains the TimeZone and Daylight fields
310 //
311 Time->TimeZone = Global->SavedTimeZone;
312 Time->Daylight = Global->Daylight;
313
314 //
315 // Make sure all field values are in correct range
316 //
317 Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB);
318 if (!EFI_ERROR (Status)) {
319 Status = RtcTimeFieldsValid (Time);
320 }
321 if (EFI_ERROR (Status)) {
322 return EFI_DEVICE_ERROR;
323 }
324
325 //
326 // Fill in Capabilities if it was passed in
327 //
328 if (Capabilities != NULL) {
329 Capabilities->Resolution = 1;
330 //
331 // 1 hertz
332 //
333 Capabilities->Accuracy = 50000000;
334 //
335 // 50 ppm
336 //
337 Capabilities->SetsToZero = FALSE;
338 }
339
340 return EFI_SUCCESS;
341 }
342
343 /**
344 Sets the current local time and date information.
345
346 @param Time A pointer to the current time.
347 @param Global For global use inside this module.
348
349 @retval EFI_SUCCESS The operation completed successfully.
350 @retval EFI_INVALID_PARAMETER A time field is out of range.
351 @retval EFI_DEVICE_ERROR The time could not be set due due to hardware error.
352
353 **/
354 EFI_STATUS
355 PcRtcSetTime (
356 IN EFI_TIME *Time,
357 IN PC_RTC_MODULE_GLOBALS *Global
358 )
359 {
360 EFI_STATUS Status;
361 EFI_TIME RtcTime;
362 RTC_REGISTER_B RegisterB;
363 UINT8 Century;
364 UINT32 TimerVar;
365
366 if (Time == NULL) {
367 return EFI_INVALID_PARAMETER;
368 }
369 //
370 // Make sure that the time fields are valid
371 //
372 Status = RtcTimeFieldsValid (Time);
373 if (EFI_ERROR (Status)) {
374 return Status;
375 }
376
377 CopyMem (&RtcTime, Time, sizeof (EFI_TIME));
378
379 //
380 // Acquire RTC Lock to make access to RTC atomic
381 //
382 if (!EfiAtRuntime ()) {
383 EfiAcquireLock (&Global->RtcLock);
384 }
385 //
386 // Wait for up to 0.1 seconds for the RTC to be updated
387 //
388 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));
389 if (EFI_ERROR (Status)) {
390 if (!EfiAtRuntime ()) {
391 EfiReleaseLock (&Global->RtcLock);
392 }
393 return Status;
394 }
395
396 //
397 // Write timezone and daylight to RTC variable
398 //
399 TimerVar = Time->Daylight;
400 TimerVar = (UINT32) ((TimerVar << 16) | (UINT16)(Time->TimeZone));
401 Status = EfiSetVariable (
402 L"RTC",
403 &gEfiCallerIdGuid,
404 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
405 sizeof (TimerVar),
406 &TimerVar
407 );
408 if (EFI_ERROR (Status)) {
409 if (!EfiAtRuntime ()) {
410 EfiReleaseLock (&Global->RtcLock);
411 }
412 return EFI_DEVICE_ERROR;
413 }
414
415 //
416 // Read Register B, and inhibit updates of the RTC
417 //
418 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);
419 RegisterB.Bits.Set = 1;
420 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);
421
422 ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);
423
424 RtcWrite (RTC_ADDRESS_SECONDS, RtcTime.Second);
425 RtcWrite (RTC_ADDRESS_MINUTES, RtcTime.Minute);
426 RtcWrite (RTC_ADDRESS_HOURS, RtcTime.Hour);
427 RtcWrite (RTC_ADDRESS_DAY_OF_THE_MONTH, RtcTime.Day);
428 RtcWrite (RTC_ADDRESS_MONTH, RtcTime.Month);
429 RtcWrite (RTC_ADDRESS_YEAR, (UINT8) RtcTime.Year);
430 RtcWrite (RTC_ADDRESS_CENTURY, Century);
431
432 //
433 // Allow updates of the RTC registers
434 //
435 RegisterB.Bits.Set = 0;
436 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);
437
438 //
439 // Release RTC Lock.
440 //
441 if (!EfiAtRuntime ()) {
442 EfiReleaseLock (&Global->RtcLock);
443 }
444 //
445 // Set the variable that contains the TimeZone and Daylight fields
446 //
447 Global->SavedTimeZone = Time->TimeZone;
448 Global->Daylight = Time->Daylight;
449
450 return EFI_SUCCESS;
451 }
452
453 /**
454 Returns the current wakeup alarm clock setting.
455
456 @param Enabled Indicates if the alarm is currently enabled or disabled.
457 @param Pending Indicates if the alarm signal is pending and requires acknowledgment.
458 @param Time The current alarm setting.
459 @param Global For global use inside this module.
460
461 @retval EFI_SUCCESS The alarm settings were returned.
462 @retval EFI_INVALID_PARAMETER Enabled is NULL.
463 @retval EFI_INVALID_PARAMETER Pending is NULL.
464 @retval EFI_INVALID_PARAMETER Time is NULL.
465 @retval EFI_DEVICE_ERROR The wakeup time could not be retrieved due to a hardware error.
466 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
467
468 **/
469 EFI_STATUS
470 PcRtcGetWakeupTime (
471 OUT BOOLEAN *Enabled,
472 OUT BOOLEAN *Pending,
473 OUT EFI_TIME *Time,
474 IN PC_RTC_MODULE_GLOBALS *Global
475 )
476 {
477 EFI_STATUS Status;
478 RTC_REGISTER_B RegisterB;
479 RTC_REGISTER_C RegisterC;
480 UINT8 Century;
481 EFI_TIME RtcTime;
482 UINTN DataSize;
483
484 //
485 // Check parameters for null pointers
486 //
487 if ((Enabled == NULL) || (Pending == NULL) || (Time == NULL)) {
488 return EFI_INVALID_PARAMETER;
489
490 }
491 //
492 // Acquire RTC Lock to make access to RTC atomic
493 //
494 if (!EfiAtRuntime ()) {
495 EfiAcquireLock (&Global->RtcLock);
496 }
497 //
498 // Wait for up to 0.1 seconds for the RTC to be updated
499 //
500 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));
501 if (EFI_ERROR (Status)) {
502 if (!EfiAtRuntime ()) {
503 EfiReleaseLock (&Global->RtcLock);
504 }
505 return EFI_DEVICE_ERROR;
506 }
507 //
508 // Read Register B and Register C
509 //
510 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);
511 RegisterC.Data = RtcRead (RTC_ADDRESS_REGISTER_C);
512
513 //
514 // Get the Time/Date/Daylight Savings values.
515 //
516 *Enabled = RegisterB.Bits.Aie;
517 *Pending = RegisterC.Bits.Af;
518
519 Time->Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);
520 Time->Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);
521 Time->Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);
522 Time->Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);
523 Time->Month = RtcRead (RTC_ADDRESS_MONTH);
524 Time->Year = RtcRead (RTC_ADDRESS_YEAR);
525 Time->TimeZone = Global->SavedTimeZone;
526 Time->Daylight = Global->Daylight;
527
528 Century = RtcRead (RTC_ADDRESS_CENTURY);
529
530 //
531 // Get the alarm info from variable
532 //
533 DataSize = sizeof (EFI_TIME);
534 Status = EfiGetVariable (
535 L"RTCALARM",
536 &gEfiCallerIdGuid,
537 NULL,
538 &DataSize,
539 &RtcTime
540 );
541 if (!EFI_ERROR (Status)) {
542 //
543 // The alarm variable exists. In this case, we read variable to get info.
544 //
545 Time->Day = RtcTime.Day;
546 Time->Month = RtcTime.Month;
547 Time->Year = RtcTime.Year;
548 }
549
550 //
551 // Release RTC Lock.
552 //
553 if (!EfiAtRuntime ()) {
554 EfiReleaseLock (&Global->RtcLock);
555 }
556
557 //
558 // Make sure all field values are in correct range
559 //
560 Status = ConvertRtcTimeToEfiTime (Time, Century, RegisterB);
561 if (!EFI_ERROR (Status)) {
562 Status = RtcTimeFieldsValid (Time);
563 }
564 if (EFI_ERROR (Status)) {
565 return EFI_DEVICE_ERROR;
566 }
567
568 return EFI_SUCCESS;
569 }
570
571 /**
572 Sets the system wakeup alarm clock time.
573
574 @param Enabled Enable or disable the wakeup alarm.
575 @param Time If Enable is TRUE, the time to set the wakeup alarm for.
576 If Enable is FALSE, then this parameter is optional, and may be NULL.
577 @param Global For global use inside this module.
578
579 @retval EFI_SUCCESS If Enable is TRUE, then the wakeup alarm was enabled.
580 If Enable is FALSE, then the wakeup alarm was disabled.
581 @retval EFI_INVALID_PARAMETER A time field is out of range.
582 @retval EFI_DEVICE_ERROR The wakeup time could not be set due to a hardware error.
583 @retval EFI_UNSUPPORTED A wakeup timer is not supported on this platform.
584
585 **/
586 EFI_STATUS
587 PcRtcSetWakeupTime (
588 IN BOOLEAN Enable,
589 IN EFI_TIME *Time, OPTIONAL
590 IN PC_RTC_MODULE_GLOBALS *Global
591 )
592 {
593 EFI_STATUS Status;
594 EFI_TIME RtcTime;
595 RTC_REGISTER_B RegisterB;
596 UINT8 Century;
597 EFI_TIME_CAPABILITIES Capabilities;
598
599 ZeroMem (&RtcTime, sizeof (RtcTime));
600
601 if (Enable) {
602
603 if (Time == NULL) {
604 return EFI_INVALID_PARAMETER;
605 }
606 //
607 // Make sure that the time fields are valid
608 //
609 Status = RtcTimeFieldsValid (Time);
610 if (EFI_ERROR (Status)) {
611 return EFI_INVALID_PARAMETER;
612 }
613 //
614 // Just support set alarm time within 24 hours
615 //
616 PcRtcGetTime (&RtcTime, &Capabilities, Global);
617 Status = RtcTimeFieldsValid (&RtcTime);
618 if (EFI_ERROR (Status)) {
619 return EFI_DEVICE_ERROR;
620 }
621 if (!IsWithinOneDay (&RtcTime, Time)) {
622 return EFI_UNSUPPORTED;
623 }
624 //
625 // Make a local copy of the time and date
626 //
627 CopyMem (&RtcTime, Time, sizeof (EFI_TIME));
628
629 }
630 //
631 // Acquire RTC Lock to make access to RTC atomic
632 //
633 if (!EfiAtRuntime ()) {
634 EfiAcquireLock (&Global->RtcLock);
635 }
636 //
637 // Wait for up to 0.1 seconds for the RTC to be updated
638 //
639 Status = RtcWaitToUpdate (PcdGet32 (PcdRealTimeClockUpdateTimeout));
640 if (EFI_ERROR (Status)) {
641 if (!EfiAtRuntime ()) {
642 EfiReleaseLock (&Global->RtcLock);
643 }
644 return EFI_DEVICE_ERROR;
645 }
646 //
647 // Read Register B
648 //
649 RegisterB.Data = RtcRead (RTC_ADDRESS_REGISTER_B);
650
651 if (Enable) {
652 ConvertEfiTimeToRtcTime (&RtcTime, RegisterB, &Century);
653 } else {
654 //
655 // if the alarm is disable, record the current setting.
656 //
657 RtcTime.Second = RtcRead (RTC_ADDRESS_SECONDS_ALARM);
658 RtcTime.Minute = RtcRead (RTC_ADDRESS_MINUTES_ALARM);
659 RtcTime.Hour = RtcRead (RTC_ADDRESS_HOURS_ALARM);
660 RtcTime.Day = RtcRead (RTC_ADDRESS_DAY_OF_THE_MONTH);
661 RtcTime.Month = RtcRead (RTC_ADDRESS_MONTH);
662 RtcTime.Year = RtcRead (RTC_ADDRESS_YEAR);
663 RtcTime.TimeZone = Global->SavedTimeZone;
664 RtcTime.Daylight = Global->Daylight;
665 }
666
667 //
668 // Set the Y/M/D info to variable as it has no corresponding hw registers.
669 //
670 Status = EfiSetVariable (
671 L"RTCALARM",
672 &gEfiCallerIdGuid,
673 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_NON_VOLATILE,
674 sizeof (RtcTime),
675 &RtcTime
676 );
677 if (EFI_ERROR (Status)) {
678 if (!EfiAtRuntime ()) {
679 EfiReleaseLock (&Global->RtcLock);
680 }
681 return EFI_DEVICE_ERROR;
682 }
683
684 //
685 // Inhibit updates of the RTC
686 //
687 RegisterB.Bits.Set = 1;
688 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);
689
690 if (Enable) {
691 //
692 // Set RTC alarm time
693 //
694 RtcWrite (RTC_ADDRESS_SECONDS_ALARM, RtcTime.Second);
695 RtcWrite (RTC_ADDRESS_MINUTES_ALARM, RtcTime.Minute);
696 RtcWrite (RTC_ADDRESS_HOURS_ALARM, RtcTime.Hour);
697
698 RegisterB.Bits.Aie = 1;
699
700 } else {
701 RegisterB.Bits.Aie = 0;
702 }
703 //
704 // Allow updates of the RTC registers
705 //
706 RegisterB.Bits.Set = 0;
707 RtcWrite (RTC_ADDRESS_REGISTER_B, RegisterB.Data);
708
709 //
710 // Release RTC Lock.
711 //
712 if (!EfiAtRuntime ()) {
713 EfiReleaseLock (&Global->RtcLock);
714 }
715 return EFI_SUCCESS;
716 }
717
718
719 /**
720 Checks an 8-bit BCD value, and converts to an 8-bit value if valid.
721
722 This function checks the 8-bit BCD value specified by Value.
723 If valid, the function converts it to an 8-bit value and returns it.
724 Otherwise, return 0xff.
725
726 @param Value The 8-bit BCD value to check and convert
727
728 @return The 8-bit value converted. Or 0xff if Value is invalid.
729
730 **/
731 UINT8
732 CheckAndConvertBcd8ToDecimal8 (
733 IN UINT8 Value
734 )
735 {
736 if ((Value < 0xa0) && ((Value & 0xf) < 0xa)) {
737 return BcdToDecimal8 (Value);
738 }
739
740 return 0xff;
741 }
742
743 /**
744 Converts time read from RTC to EFI_TIME format defined by UEFI spec.
745
746 This function converts raw time data read from RTC to the EFI_TIME format
747 defined by UEFI spec.
748 If data mode of RTC is BCD, then converts it to decimal,
749 If RTC is in 12-hour format, then converts it to 24-hour format.
750
751 @param Time On input, the time data read from RTC to convert
752 On output, the time converted to UEFI format
753 @param Century Value of century read from RTC.
754 @param RegisterB Value of Register B of RTC, indicating data mode
755 and hour format.
756
757 @retval EFI_INVALID_PARAMETER Parameters passed in are invalid.
758 @retval EFI_SUCCESS Convert RTC time to EFI time successfully.
759
760 **/
761 EFI_STATUS
762 ConvertRtcTimeToEfiTime (
763 IN OUT EFI_TIME *Time,
764 IN UINT8 Century,
765 IN RTC_REGISTER_B RegisterB
766 )
767 {
768 BOOLEAN IsPM;
769
770 if ((Time->Hour & 0x80) != 0) {
771 IsPM = TRUE;
772 } else {
773 IsPM = FALSE;
774 }
775
776 Time->Hour = (UINT8) (Time->Hour & 0x7f);
777
778 if (RegisterB.Bits.Dm == 0) {
779 Time->Year = CheckAndConvertBcd8ToDecimal8 ((UINT8) Time->Year);
780 Time->Month = CheckAndConvertBcd8ToDecimal8 (Time->Month);
781 Time->Day = CheckAndConvertBcd8ToDecimal8 (Time->Day);
782 Time->Hour = CheckAndConvertBcd8ToDecimal8 (Time->Hour);
783 Time->Minute = CheckAndConvertBcd8ToDecimal8 (Time->Minute);
784 Time->Second = CheckAndConvertBcd8ToDecimal8 (Time->Second);
785 }
786 Century = CheckAndConvertBcd8ToDecimal8 (Century);
787
788 if (Time->Year == 0xff || Time->Month == 0xff || Time->Day == 0xff ||
789 Time->Hour == 0xff || Time->Minute == 0xff || Time->Second == 0xff ||
790 Century == 0xff) {
791 return EFI_INVALID_PARAMETER;
792 }
793
794 Time->Year = (UINT16) (Century * 100 + Time->Year);
795
796 //
797 // If time is in 12 hour format, convert it to 24 hour format
798 //
799 if (RegisterB.Bits.Mil == 0) {
800 if (IsPM && Time->Hour < 12) {
801 Time->Hour = (UINT8) (Time->Hour + 12);
802 }
803
804 if (!IsPM && Time->Hour == 12) {
805 Time->Hour = 0;
806 }
807 }
808
809 Time->Nanosecond = 0;
810
811 return EFI_SUCCESS;
812 }
813
814 /**
815 Wait for a period for the RTC to be ready.
816
817 @param Timeout Tell how long it should take to wait.
818
819 @retval EFI_DEVICE_ERROR RTC device error.
820 @retval EFI_SUCCESS RTC is updated and ready.
821 **/
822 EFI_STATUS
823 RtcWaitToUpdate (
824 UINTN Timeout
825 )
826 {
827 RTC_REGISTER_A RegisterA;
828 RTC_REGISTER_D RegisterD;
829
830 //
831 // See if the RTC is functioning correctly
832 //
833 RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);
834
835 if (RegisterD.Bits.Vrt == 0) {
836 return EFI_DEVICE_ERROR;
837 }
838 //
839 // Wait for up to 0.1 seconds for the RTC to be ready.
840 //
841 Timeout = (Timeout / 10) + 1;
842 RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);
843 while (RegisterA.Bits.Uip == 1 && Timeout > 0) {
844 MicroSecondDelay (10);
845 RegisterA.Data = RtcRead (RTC_ADDRESS_REGISTER_A);
846 Timeout--;
847 }
848
849 RegisterD.Data = RtcRead (RTC_ADDRESS_REGISTER_D);
850 if (Timeout == 0 || RegisterD.Bits.Vrt == 0) {
851 return EFI_DEVICE_ERROR;
852 }
853
854 return EFI_SUCCESS;
855 }
856
857 /**
858 See if all fields of a variable of EFI_TIME type is correct.
859
860 @param Time The time to be checked.
861
862 @retval EFI_INVALID_PARAMETER Some fields of Time are not correct.
863 @retval EFI_SUCCESS Time is a valid EFI_TIME variable.
864
865 **/
866 EFI_STATUS
867 RtcTimeFieldsValid (
868 IN EFI_TIME *Time
869 )
870 {
871 if (Time->Year < 1998 ||
872 Time->Year > 2099 ||
873 Time->Month < 1 ||
874 Time->Month > 12 ||
875 (!DayValid (Time)) ||
876 Time->Hour > 23 ||
877 Time->Minute > 59 ||
878 Time->Second > 59 ||
879 Time->Nanosecond > 999999999 ||
880 (!(Time->TimeZone == EFI_UNSPECIFIED_TIMEZONE || (Time->TimeZone >= -1440 && Time->TimeZone <= 1440))) ||
881 ((Time->Daylight & (~(EFI_TIME_ADJUST_DAYLIGHT | EFI_TIME_IN_DAYLIGHT))) != 0)) {
882 return EFI_INVALID_PARAMETER;
883 }
884
885 return EFI_SUCCESS;
886 }
887
888 /**
889 See if field Day of an EFI_TIME is correct.
890
891 @param Time Its Day field is to be checked.
892
893 @retval TRUE Day field of Time is correct.
894 @retval FALSE Day field of Time is NOT correct.
895 **/
896 BOOLEAN
897 DayValid (
898 IN EFI_TIME *Time
899 )
900 {
901 INTN DayOfMonth[12];
902
903 DayOfMonth[0] = 31;
904 DayOfMonth[1] = 29;
905 DayOfMonth[2] = 31;
906 DayOfMonth[3] = 30;
907 DayOfMonth[4] = 31;
908 DayOfMonth[5] = 30;
909 DayOfMonth[6] = 31;
910 DayOfMonth[7] = 31;
911 DayOfMonth[8] = 30;
912 DayOfMonth[9] = 31;
913 DayOfMonth[10] = 30;
914 DayOfMonth[11] = 31;
915
916 //
917 // The validity of Time->Month field should be checked before
918 //
919 ASSERT (Time->Month >=1);
920 ASSERT (Time->Month <=12);
921 if (Time->Day < 1 ||
922 Time->Day > DayOfMonth[Time->Month - 1] ||
923 (Time->Month == 2 && (!IsLeapYear (Time) && Time->Day > 28))
924 ) {
925 return FALSE;
926 }
927
928 return TRUE;
929 }
930
931 /**
932 Check if it is a leap year.
933
934 @param Time The time to be checked.
935
936 @retval TRUE It is a leap year.
937 @retval FALSE It is NOT a leap year.
938 **/
939 BOOLEAN
940 IsLeapYear (
941 IN EFI_TIME *Time
942 )
943 {
944 if (Time->Year % 4 == 0) {
945 if (Time->Year % 100 == 0) {
946 if (Time->Year % 400 == 0) {
947 return TRUE;
948 } else {
949 return FALSE;
950 }
951 } else {
952 return TRUE;
953 }
954 } else {
955 return FALSE;
956 }
957 }
958
959 /**
960 Converts time from EFI_TIME format defined by UEFI spec to RTC's.
961
962 This function converts time from EFI_TIME format defined by UEFI spec to RTC's.
963 If data mode of RTC is BCD, then converts EFI_TIME to it.
964 If RTC is in 12-hour format, then converts EFI_TIME to it.
965
966 @param Time On input, the time data read from UEFI to convert
967 On output, the time converted to RTC format
968 @param RegisterB Value of Register B of RTC, indicating data mode
969 @param Century It is set according to EFI_TIME Time.
970
971 **/
972 VOID
973 ConvertEfiTimeToRtcTime (
974 IN OUT EFI_TIME *Time,
975 IN RTC_REGISTER_B RegisterB,
976 OUT UINT8 *Century
977 )
978 {
979 BOOLEAN IsPM;
980
981 IsPM = TRUE;
982 //
983 // Adjust hour field if RTC is in 12 hour mode
984 //
985 if (RegisterB.Bits.Mil == 0) {
986 if (Time->Hour < 12) {
987 IsPM = FALSE;
988 }
989
990 if (Time->Hour >= 13) {
991 Time->Hour = (UINT8) (Time->Hour - 12);
992 } else if (Time->Hour == 0) {
993 Time->Hour = 12;
994 }
995 }
996 //
997 // Set the Time/Date/Daylight Savings values.
998 //
999 *Century = DecimalToBcd8 ((UINT8) (Time->Year / 100));
1000
1001 Time->Year = (UINT16) (Time->Year % 100);
1002
1003 if (RegisterB.Bits.Dm == 0) {
1004 Time->Year = DecimalToBcd8 ((UINT8) Time->Year);
1005 Time->Month = DecimalToBcd8 (Time->Month);
1006 Time->Day = DecimalToBcd8 (Time->Day);
1007 Time->Hour = DecimalToBcd8 (Time->Hour);
1008 Time->Minute = DecimalToBcd8 (Time->Minute);
1009 Time->Second = DecimalToBcd8 (Time->Second);
1010 }
1011 //
1012 // If we are in 12 hour mode and PM is set, then set bit 7 of the Hour field.
1013 //
1014 if (RegisterB.Bits.Mil == 0 && IsPM) {
1015 Time->Hour = (UINT8) (Time->Hour | 0x80);
1016 }
1017 }
1018
1019 /**
1020 Compare the Hour, Minute and Second of the From time and the To time.
1021
1022 Only compare H/M/S in EFI_TIME and ignore other fields here.
1023
1024 @param From the first time
1025 @param To the second time
1026
1027 @return >0 The H/M/S of the From time is later than those of To time
1028 @return ==0 The H/M/S of the From time is same as those of To time
1029 @return <0 The H/M/S of the From time is earlier than those of To time
1030 **/
1031 INTN
1032 CompareHMS (
1033 IN EFI_TIME *From,
1034 IN EFI_TIME *To
1035 )
1036 {
1037 if ((From->Hour > To->Hour) ||
1038 ((From->Hour == To->Hour) && (From->Minute > To->Minute)) ||
1039 ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second > To->Second))) {
1040 return 1;
1041 } else if ((From->Hour == To->Hour) && (From->Minute == To->Minute) && (From->Second == To->Second)) {
1042 return 0;
1043 } else {
1044 return -1;
1045 }
1046 }
1047
1048 /**
1049 To check if second date is later than first date within 24 hours.
1050
1051 @param From the first date
1052 @param To the second date
1053
1054 @retval TRUE From is previous to To within 24 hours.
1055 @retval FALSE From is later, or it is previous to To more than 24 hours.
1056 **/
1057 BOOLEAN
1058 IsWithinOneDay (
1059 IN EFI_TIME *From,
1060 IN EFI_TIME *To
1061 )
1062 {
1063 UINT8 DayOfMonth[12];
1064 BOOLEAN Adjacent;
1065
1066 DayOfMonth[0] = 31;
1067 DayOfMonth[1] = 29;
1068 DayOfMonth[2] = 31;
1069 DayOfMonth[3] = 30;
1070 DayOfMonth[4] = 31;
1071 DayOfMonth[5] = 30;
1072 DayOfMonth[6] = 31;
1073 DayOfMonth[7] = 31;
1074 DayOfMonth[8] = 30;
1075 DayOfMonth[9] = 31;
1076 DayOfMonth[10] = 30;
1077 DayOfMonth[11] = 31;
1078
1079 Adjacent = FALSE;
1080
1081 //
1082 // The validity of From->Month field should be checked before
1083 //
1084 ASSERT (From->Month >=1);
1085 ASSERT (From->Month <=12);
1086
1087 if (From->Year == To->Year) {
1088 if (From->Month == To->Month) {
1089 if ((From->Day + 1) == To->Day) {
1090 if ((CompareHMS(From, To) >= 0)) {
1091 Adjacent = TRUE;
1092 }
1093 } else if (From->Day == To->Day) {
1094 if ((CompareHMS(From, To) <= 0)) {
1095 Adjacent = TRUE;
1096 }
1097 }
1098 } else if (((From->Month + 1) == To->Month) && (To->Day == 1)) {
1099 if ((From->Month == 2) && !IsLeapYear(From)) {
1100 if (From->Day == 28) {
1101 if ((CompareHMS(From, To) >= 0)) {
1102 Adjacent = TRUE;
1103 }
1104 }
1105 } else if (From->Day == DayOfMonth[From->Month - 1]) {
1106 if ((CompareHMS(From, To) >= 0)) {
1107 Adjacent = TRUE;
1108 }
1109 }
1110 }
1111 } else if (((From->Year + 1) == To->Year) &&
1112 (From->Month == 12) &&
1113 (From->Day == 31) &&
1114 (To->Month == 1) &&
1115 (To->Day == 1)) {
1116 if ((CompareHMS(From, To) >= 0)) {
1117 Adjacent = TRUE;
1118 }
1119 }
1120
1121 return Adjacent;
1122 }
1123