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