ReactOS  0.4.14-dev-854-gb9426a3
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  * FILE: hal/halx86/generic/cmos.c
5  * PURPOSE: CMOS Access Routines (Real Time Clock and LastKnownGood)
6  * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7  * Eric Kohl
8  */
9 
10 /* INCLUDES ******************************************************************/
11 
12 #include <hal.h>
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 
26 UCHAR
27 NTAPI
29 {
30  /* Select the register */
32 
33  /* Query the value */
35 }
36 
37 VOID
38 NTAPI
40  IN UCHAR Value)
41 {
42  /* Select the register */
44 
45  /* Write the value */
47 }
48 
49 ULONG
50 NTAPI
53  IN PVOID Buffer,
54  IN ULONG Length)
55 {
58  ULONG Len = Length;
59 
60  /* Do nothing if we don't have a length */
61  if (!Length) return 0;
62 
63  /* Acquire CMOS Lock */
65 
66  /* Check if this is simple CMOS */
67  if (BusNumber == 0)
68  {
69  /* Loop the buffer up to 0xFF */
70  while ((Len > 0) && (Address < 0x100))
71  {
72  /* Read the data */
74 
75  /* Update position and length */
76  Ptr++;
77  Address++;
78  Len--;
79  }
80  }
81  else if (BusNumber == 1)
82  {
83  /* Loop the buffer up to 0xFFFF */
84  while ((Len > 0) && (Address < 0x10000))
85  {
86  /* Write the data */
88 
89  /* Update position and length */
90  Ptr++;
91  Address++;
92  Len--;
93  }
94  }
95 
96  /* Release CMOS Lock */
98 
99  /* Return length read */
100  return Length - Len;
101 }
102 
103 ULONG
104 NTAPI
107  IN PVOID Buffer,
108  IN ULONG Length)
109 {
110  PUCHAR Ptr = (PUCHAR)Buffer;
112  ULONG Len = Length;
113 
114  /* Do nothing if we don't have a length */
115  if (!Length) return 0;
116 
117  /* Acquire CMOS Lock */
119 
120  /* Check if this is simple CMOS */
121  if (BusNumber == 0)
122  {
123  /* Loop the buffer up to 0xFF */
124  while ((Len > 0) && (Address < 0x100))
125  {
126  /* Write the data */
128 
129  /* Update position and length */
130  Ptr++;
131  Address++;
132  Len--;
133  }
134  }
135  else if (BusNumber == 1)
136  {
137  /* Loop the buffer up to 0xFFFF */
138  while ((Len > 0) && (Address < 0x10000))
139  {
140  /* Write the data */
142 
143  /* Update position and length */
144  Ptr++;
145  Address++;
146  Len--;
147  }
148  }
149 
150  /* Release CMOS Lock */
152 
153  /* Return length read */
154  return Length - Len;
155 }
156 
157 INIT_FUNCTION
158 VOID
159 NTAPI
161 {
162  /* Set default century offset byte */
164 
165  /* No support for EISA or MCA */
167 }
168 
169 /* PUBLIC FUNCTIONS **********************************************************/
170 
171 /*
172  * @implemented
173  */
175 NTAPI
178  IN PCH Value)
179 {
180  UCHAR Val;
181 
182  /* Only variable supported on x86 */
183  if (_stricmp(Name, "LastKnownGood")) return ENOENT;
184 
185  /* Acquire CMOS Lock */
187 
188  /* Query the current value */
189  Val = HalpReadCmos(RTC_REGISTER_B) & 0x01;
190 
191  /* Release CMOS lock */
193 
194  /* Check the flag */
195  if (Val)
196  {
197  /* Return false */
198  strncpy(Value, "FALSE", ValueLength);
199  }
200  else
201  {
202  /* Return true */
203  strncpy(Value, "TRUE", ValueLength);
204  }
205 
206  /* Return success */
207  return ESUCCESS;
208 }
209 
210 /*
211  * @implemented
212  */
214 NTAPI
216  IN PCH Value)
217 {
218  UCHAR Val;
219 
220  /* Only variable supported on x86 */
221  if (_stricmp(Name, "LastKnownGood")) return ENOMEM;
222 
223  /* Check if this is true or false */
224  if (!_stricmp(Value, "TRUE"))
225  {
226  /* It's true, acquire CMOS lock */
228 
229  /* Read the current value and add the flag */
230  Val = HalpReadCmos(RTC_REGISTER_B) | 1;
231  }
232  else if (!_stricmp(Value, "FALSE"))
233  {
234  /* It's false, acquire CMOS lock */
236 
237  /* Read the current value and mask out the flag */
238  Val = HalpReadCmos(RTC_REGISTER_B) & ~1;
239  }
240  else
241  {
242  /* Fail */
243  return ENOMEM;
244  }
245 
246  /* Write new value */
248 
249  /* Release the lock and return success */
251  return ESUCCESS;
252 }
253 
254 /*
255  * @implemented
256  */
257 BOOLEAN
258 NTAPI
260 {
261  /* Acquire CMOS Lock */
263 
264  /* Loop while update is in progress */
266 
267  /* Set the time data */
268  Time->Second = BCD_INT(HalpReadCmos(0));
269  Time->Minute = BCD_INT(HalpReadCmos(2));
270  Time->Hour = BCD_INT(HalpReadCmos(4));
271  Time->Weekday = BCD_INT(HalpReadCmos(6));
272  Time->Day = BCD_INT(HalpReadCmos(7));
273  Time->Month = BCD_INT(HalpReadCmos(8));
274  Time->Year = BCD_INT(HalpReadCmos(9));
275  Time->Milliseconds = 0;
276 
277  /* FIXME: Check century byte */
278 
279  /* Compensate for the century field */
280  Time->Year += (Time->Year > 80) ? 1900: 2000;
281 
282  /* Release CMOS lock */
284 
285  /* Always return TRUE */
286  return TRUE;
287 }
288 
289 /*
290  * @implemented
291  */
292 BOOLEAN
293 NTAPI
295 {
296  /* Acquire CMOS Lock */
298 
299  /* Loop while update is in progress */
301 
302  /* Write time fields to CMOS RTC */
303  HalpWriteCmos(0, INT_BCD(Time->Second));
304  HalpWriteCmos(2, INT_BCD(Time->Minute));
305  HalpWriteCmos(4, INT_BCD(Time->Hour));
306  HalpWriteCmos(6, INT_BCD(Time->Weekday));
307  HalpWriteCmos(7, INT_BCD(Time->Day));
308  HalpWriteCmos(8, INT_BCD(Time->Month));
309  HalpWriteCmos(9, INT_BCD(Time->Year % 100));
310 
311  /* FIXME: Set the century byte */
312 
313  /* Release CMOS lock */
315 
316  /* Always return TRUE */
317  return TRUE;
318 }
_In_opt_ ULONG _Out_ PULONG Value
Definition: rtlfuncs.h:2374
#define IN
Definition: typedefs.h:38
VOID FORCEINLINE HalpWriteCmos(IN UCHAR Reg, IN UCHAR Value)
Definition: cmos.c:35
#define TRUE
Definition: types.h:120
#define CMOS_CONTROL_PORT
Definition: halp.h:14
Definition: arc.h:32
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
unsigned char * PUCHAR
Definition: retypes.h:3
UCHAR NTAPI READ_PORT_UCHAR(PUCHAR Address)
Definition: mach.c:528
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
#define BCD_INT(bcd)
Definition: hwinfo.c:13
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
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:160
CHAR * PCH
Definition: ntbasedef.h:398
unsigned char BOOLEAN
static WCHAR Address[46]
Definition: ping.c:68
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
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
_In_ GUID _In_ PVOID _In_ ULONG ValueLength
Definition: hubbusif.h:311
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
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
void WRITE_PORT_UCHAR(PUCHAR Address, UCHAR Value)
Definition: mach.c:532
#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