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