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