ReactOS  0.4.15-dev-1049-g1062a29
cmos.c
Go to the documentation of this file.
1 /*
2  * PROJECT: ReactOS HAL
3  * LICENSE: GPL - See COPYING in the top level directory
4  * PURPOSE: CMOS Access Routines (Real Time Clock and LastKnownGood)
5  * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
6  * Eric Kohl
7  */
8 
9 /* INCLUDES ******************************************************************/
10 
11 #include <hal.h>
12 
13 #define NDEBUG
14 #include <debug.h>
15 
16 #if defined(ALLOC_PRAGMA) && !defined(_MINIHAL_)
17 #pragma alloc_text(INIT, HalpInitializeCmos)
18 #endif
19 
20 /* GLOBALS *******************************************************************/
21 
23 
24 /* PRIVATE FUNCTIONS *********************************************************/
25 
27 UCHAR
28 NTAPI
30 {
31  /* Select the register */
33 
34  /* Query the value */
36 }
37 
39 VOID
40 NTAPI
42  IN UCHAR Value)
43 {
44  /* Select the register */
46 
47  /* Write the value */
49 }
50 
51 ULONG
52 NTAPI
58 {
61  ULONG Len = Length;
62 
63  /* Do nothing if we don't have a length */
64  if (!Length) return 0;
65 
66  /* Acquire CMOS Lock */
68 
69  /* Check if this is simple CMOS */
70  if (BusNumber == 0)
71  {
72  /* Loop the buffer up to 0xFF */
73  while ((Len > 0) && (Address < 0x100))
74  {
75  /* Read the data */
77 
78  /* Update position and length */
79  Ptr++;
80  Address++;
81  Len--;
82  }
83  }
84  else if (BusNumber == 1)
85  {
86  /* Loop the buffer up to 0xFFFF */
87  while ((Len > 0) && (Address < 0x10000))
88  {
89  /* Write the data */
91 
92  /* Update position and length */
93  Ptr++;
94  Address++;
95  Len--;
96  }
97  }
98 
99  /* Release CMOS Lock */
101 
102  /* Return length read */
103  return Length - Len;
104 }
105 
106 ULONG
107 NTAPI
110  IN PVOID Buffer,
111  IN ULONG Length)
112 {
113  PUCHAR Ptr = (PUCHAR)Buffer;
115  ULONG Len = Length;
116 
117  /* Do nothing if we don't have a length */
118  if (!Length) return 0;
119 
120  /* Acquire CMOS Lock */
122 
123  /* Check if this is simple CMOS */
124  if (BusNumber == 0)
125  {
126  /* Loop the buffer up to 0xFF */
127  while ((Len > 0) && (Address < 0x100))
128  {
129  /* Write the data */
131 
132  /* Update position and length */
133  Ptr++;
134  Address++;
135  Len--;
136  }
137  }
138  else if (BusNumber == 1)
139  {
140  /* Loop the buffer up to 0xFFFF */
141  while ((Len > 0) && (Address < 0x10000))
142  {
143  /* Write the data */
145 
146  /* Update position and length */
147  Ptr++;
148  Address++;
149  Len--;
150  }
151  }
152 
153  /* Release CMOS Lock */
155 
156  /* Return length read */
157  return Length - Len;
158 }
159 
160 INIT_FUNCTION
161 VOID
162 NTAPI
164 {
165  /* Set default century offset byte */
167 
168  /* No support for EISA or MCA */
170 }
171 
172 /* PUBLIC FUNCTIONS **********************************************************/
173 
174 /*
175  * @implemented
176  */
178 NTAPI
180  _In_ PCH Name,
183 {
184  UCHAR Val;
185 
186  /* Only variable supported on x86 */
187  if (_stricmp(Name, "LastKnownGood")) return ENOENT;
188 
189  /* Acquire CMOS Lock */
191 
192  /* Query the current value */
193  Val = HalpReadCmos(RTC_REGISTER_B) & 0x01;
194 
195  /* Release CMOS lock */
197 
198  /* Check the flag */
199  if (Val)
200  {
201  /* Return false */
202  strncpy(Value, "FALSE", ValueLength);
203  }
204  else
205  {
206  /* Return true */
207  strncpy(Value, "TRUE", ValueLength);
208  }
209 
210  /* Return success */
211  return ESUCCESS;
212 }
213 
214 /*
215  * @implemented
216  */
218 NTAPI
220  IN PCH Value)
221 {
222  UCHAR Val;
223 
224  /* Only variable supported on x86 */
225  if (_stricmp(Name, "LastKnownGood")) return ENOMEM;
226 
227  /* Check if this is true or false */
228  if (!_stricmp(Value, "TRUE"))
229  {
230  /* It's true, acquire CMOS lock */
232 
233  /* Read the current value and add the flag */
234  Val = HalpReadCmos(RTC_REGISTER_B) | 1;
235  }
236  else if (!_stricmp(Value, "FALSE"))
237  {
238  /* It's false, acquire CMOS lock */
240 
241  /* Read the current value and mask out the flag */
242  Val = HalpReadCmos(RTC_REGISTER_B) & ~1;
243  }
244  else
245  {
246  /* Fail */
247  return ENOMEM;
248  }
249 
250  /* Write new value */
252 
253  /* Release the lock and return success */
255  return ESUCCESS;
256 }
257 
258 /*
259  * @implemented
260  */
261 BOOLEAN
262 NTAPI
264 {
265  /* Acquire CMOS Lock */
267 
268  /* Loop while update is in progress */
270 
271  /* Set the time data */
272  Time->Second = BCD_INT(HalpReadCmos(0));
273  Time->Minute = BCD_INT(HalpReadCmos(2));
274  Time->Hour = BCD_INT(HalpReadCmos(4));
275  Time->Weekday = BCD_INT(HalpReadCmos(6));
276  Time->Day = BCD_INT(HalpReadCmos(7));
277  Time->Month = BCD_INT(HalpReadCmos(8));
278  Time->Year = BCD_INT(HalpReadCmos(9));
279  Time->Milliseconds = 0;
280 
281  /* FIXME: Check century byte */
282 
283  /* Compensate for the century field */
284  Time->Year += (Time->Year > 80) ? 1900: 2000;
285 
286  /* Release CMOS lock */
288 
289  /* Always return TRUE */
290  return TRUE;
291 }
292 
293 /*
294  * @implemented
295  */
296 BOOLEAN
297 NTAPI
299 {
300  /* Acquire CMOS Lock */
302 
303  /* Loop while update is in progress */
305 
306  /* Write time fields to CMOS RTC */
307  HalpWriteCmos(0, INT_BCD(Time->Second));
308  HalpWriteCmos(2, INT_BCD(Time->Minute));
309  HalpWriteCmos(4, INT_BCD(Time->Hour));
310  HalpWriteCmos(6, INT_BCD(Time->Weekday));
311  HalpWriteCmos(7, INT_BCD(Time->Day));
312  HalpWriteCmos(8, INT_BCD(Time->Month));
313  HalpWriteCmos(9, INT_BCD(Time->Year % 100));
314 
315  /* FIXME: Set the century byte */
316 
317  /* Release CMOS lock */
319 
320  /* Always return TRUE */
321  return TRUE;
322 }
#define IN
Definition: typedefs.h:39
VOID FORCEINLINE HalpWriteCmos(IN UCHAR Reg, IN UCHAR Value)
Definition: cmos.c:35
#define CMOS_CONTROL_PORT
Definition: halp.h:14
Definition: arc.h:32
#define READ_PORT_UCHAR(p)
Definition: pc98vid.h:21
#define TRUE
Definition: types.h:120
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
unsigned char * PUCHAR
Definition: retypes.h:3
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
static UCHAR BCD_INT(_In_ UCHAR Bcd)
Definition: cmos.c:43
ULONG ARC_STATUS
Definition: arc.h:4
ULONG NTAPI HalpGetCmosData(IN ULONG BusNumber, IN ULONG SlotNumber, IN PVOID Buffer, IN ULONG Length)
Definition: cmos.c:47
#define _stricmp
Definition: cat.c:22
Definition: arc.h:48
IN UCHAR Value
Definition: halp.h:394
#define _Out_writes_z_(size)
Definition: no_sal2.h:384
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
VOID NTAPI HalpReleaseCmosSpinLock(VOID)
Definition: spinlock.c:244
INIT_FUNCTION VOID NTAPI HalpInitializeCmos(VOID)
Definition: cmos.c:163
CHAR * PCH
Definition: ntbasedef.h:392
unsigned char BOOLEAN
static WCHAR Address[46]
Definition: ping.c:68
#define _Out_writes_bytes_(size)
Definition: no_sal2.h:370
UCHAR FORCEINLINE HalpReadCmos(IN UCHAR Reg)
Definition: cmos.c:24
Definition: bufpool.h:45
_In_opt_ PUNICODE_STRING _In_ PDRIVER_OBJECT _In_ PDEVICE_OBJECT _In_ INTERFACE_TYPE _In_ ULONG BusNumber
Definition: halfuncs.h:156
_Requires_lock_held_(HalpSystemHardwareLock)
Definition: cmos.c:26
ULONG HalpBusType
Definition: bus.c:17
BOOLEAN NTAPI HalSetRealTimeClock(IN PTIME_FIELDS Time)
Definition: cmos.c:267
#define CMOS_DATA_PORT
Definition: halp.h:15
#define MACHINE_TYPE_ISA
Definition: ketypes.h:52
#define Len
Definition: deflate.h:82
VOID NTAPI HalpAcquireCmosSpinLock(VOID)
Definition: spinlock.c:227
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel) ?(CompletionRoutine !=NULL) :TRUE)
#define RTC_REGISTER_B
Definition: halp.h:17
_In_opt_ PUNICODE_STRING _In_ PDRIVER_OBJECT _In_ PDEVICE_OBJECT _In_ INTERFACE_TYPE _In_ ULONG _In_ ULONG SlotNumber
Definition: halfuncs.h:156
unsigned char UCHAR
Definition: xmlstorage.h:181
#define WRITE_PORT_UCHAR(p, d)
Definition: pc98vid.h:20
_In_ GUID _In_ PVOID _In_ ULONG ValueLength
Definition: hubbusif.h:311
#define _In_
Definition: no_sal2.h:204
unsigned short USHORT
Definition: pedump.c:61
ARC_STATUS NTAPI HalSetEnvironmentVariable(IN PCH Name, IN PCH Value)
Definition: cmos.c:193
UCHAR HalpCmosCenturyOffset
Definition: cmos.c:22
ARC_STATUS NTAPI HalGetEnvironmentVariable(IN PCH Name, IN USHORT ValueLength, IN PCH Value)
Definition: cmos.c:156
Definition: arc.h:46
BOOLEAN NTAPI HalQueryRealTimeClock(OUT PTIME_FIELDS Time)
Definition: cmos.c:234
#define RTC_REGISTER_A
Definition: xboxrtc.c:21
KSPIN_LOCK HalpSystemHardwareLock
Definition: cmos.c:18
#define OUT
Definition: typedefs.h:40
unsigned int ULONG
Definition: retypes.h:1
#define INT_BCD(int)
Definition: halp.h:31
#define RTC_REG_A_UIP
Definition: xboxrtc.c:22
static PLARGE_INTEGER Time
Definition: time.c:105
ULONG NTAPI HalpSetCmosData(IN ULONG BusNumber, IN ULONG SlotNumber, IN PVOID Buffer, IN ULONG Length)
Definition: cmos.c:99