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