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