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