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