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