ReactOS  0.4.15-dev-3187-ge372f2b
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 /* GLOBALS *******************************************************************/
17 
19 
20 /* PRIVATE FUNCTIONS *********************************************************/
21 
23 UCHAR
24 NTAPI
26 {
27  /* Select the register */
29 
30  /* Query the value */
32 }
33 
35 VOID
36 NTAPI
38  IN UCHAR Value)
39 {
40  /* Select the register */
42 
43  /* Write the value */
45 }
46 
47 ULONG
48 NTAPI
54 {
57  ULONG Len = Length;
58 
59  /* Do nothing if we don't have a length */
60  if (!Length) return 0;
61 
62  /* Acquire CMOS Lock */
64 
65  /* Check if this is simple CMOS */
66  if (BusNumber == 0)
67  {
68  /* Loop the buffer up to 0xFF */
69  while ((Len > 0) && (Address < 0x100))
70  {
71  /* Read the data */
73 
74  /* Update position and length */
75  Ptr++;
76  Address++;
77  Len--;
78  }
79  }
80  else if (BusNumber == 1)
81  {
82  /* Loop the buffer up to 0xFFFF */
83  while ((Len > 0) && (Address < 0x10000))
84  {
85  /* Write the data */
87 
88  /* Update position and length */
89  Ptr++;
90  Address++;
91  Len--;
92  }
93  }
94 
95  /* Release CMOS Lock */
97 
98  /* Return length read */
99  return Length - Len;
100 }
101 
102 ULONG
103 NTAPI
106  IN PVOID Buffer,
107  IN ULONG Length)
108 {
109  PUCHAR Ptr = (PUCHAR)Buffer;
111  ULONG Len = Length;
112 
113  /* Do nothing if we don't have a length */
114  if (!Length) return 0;
115 
116  /* Acquire CMOS Lock */
118 
119  /* Check if this is simple CMOS */
120  if (BusNumber == 0)
121  {
122  /* Loop the buffer up to 0xFF */
123  while ((Len > 0) && (Address < 0x100))
124  {
125  /* Write the data */
127 
128  /* Update position and length */
129  Ptr++;
130  Address++;
131  Len--;
132  }
133  }
134  else if (BusNumber == 1)
135  {
136  /* Loop the buffer up to 0xFFFF */
137  while ((Len > 0) && (Address < 0x10000))
138  {
139  /* Write the data */
141 
142  /* Update position and length */
143  Ptr++;
144  Address++;
145  Len--;
146  }
147  }
148 
149  /* Release CMOS Lock */
151 
152  /* Return length read */
153  return Length - Len;
154 }
155 
156 CODE_SEG("INIT")
157 VOID
158 NTAPI
160 {
161  /* Set default century offset byte */
163 
164  /* No support for EISA or MCA */
166 }
167 
168 /* PUBLIC FUNCTIONS **********************************************************/
169 
170 /*
171  * @implemented
172  */
174 NTAPI
176  _In_ PCH Name,
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_ WDFIORESREQLIST _In_ ULONG SlotNumber
Definition: wdfresource.h:65
#define IN
Definition: typedefs.h:39
VOID NTAPI HalpWriteCmos(_In_ UCHAR Reg, _In_ UCHAR Value)
Definition: cmos.c:132
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
Definition: arc.h:32
#define READ_PORT_UCHAR(p)
Definition: pc98vid.h:22
#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
BOOLEAN NTAPI HalSetRealTimeClock(IN PTIME_FIELDS Time)
Definition: cmos.c:294
ULONG NTAPI HalpGetCmosData(_In_ ULONG BusNumber, _In_ ULONG SlotNumber, _Out_writes_bytes_(Length) PVOID Buffer, _In_ ULONG Length)
Definition: cmos.c:49
static UCHAR BCD_INT(_In_ UCHAR Bcd)
Definition: cmos.c:39
ULONG ARC_STATUS
Definition: arc.h:4
ARC_STATUS NTAPI HalSetEnvironmentVariable(IN PCH Name, IN PCH Value)
Definition: cmos.c:215
#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
ULONG HalpBusType
Definition: pcibus.c:18
VOID NTAPI HalpReleaseCmosSpinLock(VOID)
Definition: spinlock.c:229
CHAR * PCH
Definition: ntbasedef.h:391
unsigned char BOOLEAN
static WCHAR Address[46]
Definition: ping.c:68
#define _In_
Definition: ms_sal.h:308
Definition: bufpool.h:45
BOOLEAN NTAPI HalQueryRealTimeClock(OUT PTIME_FIELDS Time)
Definition: cmos.c:259
KSPIN_LOCK HalpSystemHardwareLock
Definition: spinlock.c:25
#define CMOS_CONTROL_PORT
Definition: halhw.h:11
_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:22
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _Out_opt_ PUSHORT _Inout_opt_ PUNICODE_STRING Value
Definition: wdfregistry.h:406
#define ASSERT(a)
Definition: mode.c:44
#define MACHINE_TYPE_ISA
Definition: ketypes.h:52
#define Len
Definition: deflate.h:82
ARC_STATUS NTAPI HalGetEnvironmentVariable(_In_ PCH Name, _In_ USHORT ValueLength, _Out_writes_z_(ValueLength) PCH Value)
Definition: cmos.c:175
VOID NTAPI HalpAcquireCmosSpinLock(VOID)
Definition: spinlock.c:212
unsigned char UCHAR
Definition: xmlstorage.h:181
#define WRITE_PORT_UCHAR(p, d)
Definition: pc98vid.h:21
#define _Out_writes_z_(size)
Definition: ms_sal.h:352
#define RTC_REGISTER_B
Definition: halhw.h:15
unsigned short USHORT
Definition: pedump.c:61
UCHAR HalpCmosCenturyOffset
Definition: cmos.c:18
#define CMOS_DATA_PORT
Definition: halhw.h:12
UCHAR NTAPI HalpReadCmos(_In_ UCHAR Reg)
Definition: cmos.c:123
Definition: arc.h:46
ULONG NTAPI HalpSetCmosData(IN ULONG BusNumber, IN ULONG SlotNumber, IN PVOID Buffer, IN ULONG Length)
Definition: cmos.c:104
#define RTC_REGISTER_A
Definition: xboxrtc.c:21
#define OUT
Definition: typedefs.h:40
unsigned int ULONG
Definition: retypes.h:1
#define INT_BCD(int)
Definition: halp.h:59
VOID NTAPI HalpInitializeCmos(VOID)
Definition: cmos.c:159
#define _Out_writes_bytes_(size)
Definition: ms_sal.h:350
static CODE_SEG("PAGE")
Definition: isapnp.c:1482
#define RTC_REG_A_UIP
Definition: xboxrtc.c:22
static PLARGE_INTEGER Time
Definition: time.c:105
_Must_inspect_result_ _In_ WDFKEY _In_ PCUNICODE_STRING _In_ ULONG ValueLength
Definition: wdfregistry.h:271