blob: 8b561642848a7cc8457c0bcc4342df51291248b6 [file] [log] [blame]
Patrick Georgi55189c92020-05-10 20:09:31 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Stefan Reinauer6540ae52007-07-12 16:35:42 +00002
Andriy Gapon6a4bf982008-10-30 15:41:39 +00003#if defined(__FreeBSD__)
4#include <fcntl.h>
5#include <unistd.h>
6#endif
7
Stefan Reinauer6540ae52007-07-12 16:35:42 +00008#include "common.h"
9#include "cmos_lowlevel.h"
10
Patrick Georgibe5a1782011-01-21 07:18:20 +000011/* Hardware Abstraction Layer: lowlevel byte-wise write access */
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000012
Patrick Georgi1e916e02011-01-28 07:54:11 +000013extern cmos_access_t cmos_hal, memory_hal;
Patrick Georgi44a89b32012-05-01 12:10:45 +020014static cmos_access_t *current_access =
15#ifdef CMOS_HAL
16 &cmos_hal;
17#else
18 &memory_hal;
19#endif
Patrick Georgibe5a1782011-01-21 07:18:20 +000020
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000021void select_hal(hal_t hal, void *data)
22{
23 switch(hal) {
Patrick Georgi44a89b32012-05-01 12:10:45 +020024#ifdef CMOS_HAL
Felix Singer68a4c2a2023-12-08 11:11:56 +010025 case HAL_CMOS:
26 current_access = &cmos_hal;
27 break;
Patrick Georgi44a89b32012-05-01 12:10:45 +020028#endif
Felix Singer68a4c2a2023-12-08 11:11:56 +010029 case HAL_MEMORY:
30 default:
31 current_access = &memory_hal;
32 break;
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000033 }
34 current_access->init(data);
35}
36
Patrick Georgibe5a1782011-01-21 07:18:20 +000037/* Bit-level access */
Stefan Reinauer90b96b62010-01-13 21:00:23 +000038typedef struct {
39 unsigned byte_index;
40 unsigned bit_offset;
41} cmos_bit_op_location_t;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000042
Stefan Reinauer90b96b62010-01-13 21:00:23 +000043static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
44 cmos_bit_op_location_t * where);
45static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
46 unsigned nr_bits);
47static void cmos_write_bits(const cmos_bit_op_location_t * where,
48 unsigned nr_bits, unsigned char value);
49static unsigned char get_bits(unsigned long long value, unsigned bit,
50 unsigned nr_bits);
51static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits,
52 unsigned long long *result);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000053
54/****************************************************************************
55 * get_bits
56 *
57 * Extract a value 'nr_bits' bits wide starting at bit position 'bit' from
58 * 'value' and return the result. It is assumed that 'nr_bits' is at most 8.
59 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +000060static inline unsigned char get_bits(unsigned long long value, unsigned bit,
61 unsigned nr_bits)
62{
63 return (value >> bit) & ((unsigned char)((1 << nr_bits) - 1));
64}
Stefan Reinauer6540ae52007-07-12 16:35:42 +000065
66/****************************************************************************
67 * put_bits
68 *
69 * Extract the low order 'nr_bits' bits from 'value' and store them in the
70 * value pointed to by 'result' starting at bit position 'bit'. The bit
71 * positions in 'result' where the result is stored are assumed to be
72 * initially zero.
73 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +000074static inline void put_bits(unsigned char value, unsigned bit,
75 unsigned nr_bits, unsigned long long *result)
76{
Stefan Reinauer14e22772010-04-27 06:56:47 +000077 *result += ((unsigned long long)(value &
Stefan Reinauer90b96b62010-01-13 21:00:23 +000078 ((unsigned char)((1 << nr_bits) - 1)))) << bit;
79}
Stefan Reinauer6540ae52007-07-12 16:35:42 +000080
81/****************************************************************************
82 * cmos_read
83 *
84 * Read value from nonvolatile RAM at position given by 'bit' and 'length'
85 * and return this value. The I/O privilege level of the currently executing
86 * process must be set appropriately.
Andrew Engelbrechte8905312014-12-01 12:22:48 -050087 *
88 * Returned value is either (unsigned long long), or malloc()'d (char *)
89 * cast to (unsigned long long)
Stefan Reinauer6540ae52007-07-12 16:35:42 +000090 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +000091unsigned long long cmos_read(const cmos_entry_t * e)
92{
93 cmos_bit_op_location_t where;
94 unsigned bit = e->bit, length = e->length;
95 unsigned next_bit, bits_left, nr_bits;
96 unsigned long long result = 0;
97 unsigned char value;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000098
Stefan Reinauer90b96b62010-01-13 21:00:23 +000099 assert(!verify_cmos_op(bit, length, e->config));
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000100
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000101 if (e->config == CMOS_ENTRY_STRING) {
Patrick Georgi59ab4e92014-12-01 22:09:33 +0100102 int strsz = (length + 7) / 8 + 1;
Andrew Engelbrechte8905312014-12-01 12:22:48 -0500103 char *newstring = malloc(strsz);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000104 unsigned usize = (8 * sizeof(unsigned long long));
Stefan Reinauera67aab72008-09-27 10:08:28 +0000105
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000106 if (!newstring) {
107 out_of_memory();
108 }
Stefan Reinauera67aab72008-09-27 10:08:28 +0000109
Patrick Georgidd1aab92014-08-09 17:06:20 +0200110 memset(newstring, 0, strsz);
111
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000112 for (next_bit = 0, bits_left = length;
113 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
114 nr_bits = cmos_bit_op_strategy(bit + next_bit,
115 bits_left > usize ? usize : bits_left, &where);
116 value = cmos_read_bits(&where, nr_bits);
117 put_bits(value, next_bit % usize, nr_bits,
118 &((unsigned long long *)newstring)[next_bit /
119 usize]);
120 result = (unsigned long)newstring;
121 }
122 } else {
123 for (next_bit = 0, bits_left = length;
124 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
125 nr_bits =
126 cmos_bit_op_strategy(bit + next_bit, bits_left,
127 &where);
128 value = cmos_read_bits(&where, nr_bits);
129 put_bits(value, next_bit, nr_bits, &result);
130 }
131 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000132
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000133 return result;
134}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000135
136/****************************************************************************
137 * cmos_write
138 *
139 * Write 'data' to nonvolatile RAM at position given by 'bit' and 'length'.
140 * The I/O privilege level of the currently executing process must be set
141 * appropriately.
142 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000143void cmos_write(const cmos_entry_t * e, unsigned long long value)
144{
145 cmos_bit_op_location_t where;
146 unsigned bit = e->bit, length = e->length;
147 unsigned next_bit, bits_left, nr_bits;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000148
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000149 assert(!verify_cmos_op(bit, length, e->config));
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000150
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000151 if (e->config == CMOS_ENTRY_STRING) {
152 unsigned long long *data =
153 (unsigned long long *)(unsigned long)value;
154 unsigned usize = (8 * sizeof(unsigned long long));
Stefan Reinauera67aab72008-09-27 10:08:28 +0000155
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000156 for (next_bit = 0, bits_left = length;
157 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
158 nr_bits = cmos_bit_op_strategy(bit + next_bit,
159 bits_left > usize ? usize : bits_left,
160 &where);
161 value = data[next_bit / usize];
162 cmos_write_bits(&where, nr_bits,
163 get_bits(value, next_bit % usize, nr_bits));
164 }
165 } else {
166 for (next_bit = 0, bits_left = length;
167 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000168 nr_bits = cmos_bit_op_strategy(bit + next_bit,
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000169 bits_left, &where);
170 cmos_write_bits(&where, nr_bits,
171 get_bits(value, next_bit, nr_bits));
172 }
173 }
174}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000175
176/****************************************************************************
177 * cmos_read_byte
178 *
179 * Read a byte from nonvolatile RAM at a position given by 'index' and return
180 * the result. An 'index' value of 0 represents the first byte of
181 * nonvolatile RAM.
182 *
183 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
184 * real time clock.
185 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000186unsigned char cmos_read_byte(unsigned index)
187{
Patrick Georgibe5a1782011-01-21 07:18:20 +0000188 return current_access->read(index);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000189}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000190
191/****************************************************************************
192 * cmos_write_byte
193 *
194 * Write 'value' to nonvolatile RAM at a position given by 'index'. An
195 * 'index' of 0 represents the first byte of nonvolatile RAM.
196 *
197 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
198 * real time clock. Writing to any of these bytes will therefore
199 * affect its functioning.
200 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000201void cmos_write_byte(unsigned index, unsigned char value)
202{
Patrick Georgibe5a1782011-01-21 07:18:20 +0000203 current_access->write(index, value);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000204}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000205
206/****************************************************************************
207 * cmos_read_all
208 *
209 * Read all contents of CMOS memory into array 'data'. The first 14 bytes of
210 * 'data' are set to zero since this corresponds to the real time clock area.
211 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000212void cmos_read_all(unsigned char data[])
213{
214 unsigned i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000215
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000216 for (i = 0; i < CMOS_RTC_AREA_SIZE; i++)
217 data[i] = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000218
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000219 for (; i < CMOS_SIZE; i++)
220 data[i] = cmos_read_byte(i);
221}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000222
223/****************************************************************************
224 * cmos_write_all
225 *
226 * Update all of CMOS memory with the contents of array 'data'. The first 14
227 * bytes of 'data' are ignored since this corresponds to the real time clock
228 * area.
229 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000230void cmos_write_all(unsigned char data[])
231{
232 unsigned i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000233
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000234 for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++)
235 cmos_write_byte(i, data[i]);
236}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000237
238/****************************************************************************
239 * set_iopl
240 *
241 * Set the I/O privilege level of the executing process. Root privileges are
242 * required for performing this action. A sufficient I/O privilege level
243 * allows the process to access x86 I/O address space and to disable/reenable
244 * interrupts while executing in user space. Messing with the I/O privilege
245 * level is therefore somewhat dangerous.
246 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000247void set_iopl(int level)
248{
Patrick Georgi9cd7eba2011-01-21 07:19:59 +0000249 current_access->set_iopl(level);
250}
251
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000252/****************************************************************************
253 * verify_cmos_op
254 *
255 * 'bit' represents a bit position in the nonvolatile RAM. The first bit
256 * (i.e. the lowest order bit of the first byte) of nonvolatile RAM is
257 * labeled as bit 0. 'length' represents the width in bits of a value we
258 * wish to read or write. Perform sanity checking on 'bit' and 'length'. If
259 * no problems were encountered, return OK. Else return an error code.
260 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000261int verify_cmos_op(unsigned bit, unsigned length, cmos_entry_config_t config)
262{
263 if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE)))
264 return CMOS_AREA_OUT_OF_RANGE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000265
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000266 if (bit < (8 * CMOS_RTC_AREA_SIZE))
267 return CMOS_AREA_OVERLAPS_RTC;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000268
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000269 if (config == CMOS_ENTRY_STRING)
270 return OK;
Stefan Reinauera67aab72008-09-27 10:08:28 +0000271
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000272 if (length > (8 * sizeof(unsigned long long)))
273 return CMOS_AREA_TOO_WIDE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000274
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000275 return OK;
276}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000277
278/****************************************************************************
279 * cmos_bit_op_strategy
280 *
281 * Helper function used by cmos_read() and cmos_write() to determine which
282 * bits to read or write next.
283 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000284static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
285 cmos_bit_op_location_t * where)
286{
287 unsigned max_bits;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000288
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000289 where->byte_index = bit >> 3;
290 where->bit_offset = bit & 0x07;
291 max_bits = 8 - where->bit_offset;
292 return (bits_left > max_bits) ? max_bits : bits_left;
293}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000294
295/****************************************************************************
296 * cmos_read_bits
297 *
298 * Read a chunk of bits from a byte location within CMOS memory. Return the
299 * value represented by the chunk of bits.
300 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000301static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
302 unsigned nr_bits)
303{
304 return (cmos_read_byte(where->byte_index) >> where->bit_offset) &
305 ((unsigned char)((1 << nr_bits) - 1));
306}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000307
308/****************************************************************************
309 * cmos_write_bits
310 *
311 * Write a chunk of bits (the low order 'nr_bits' bits of 'value') to an area
312 * within a particular byte of CMOS memory.
313 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000314static void cmos_write_bits(const cmos_bit_op_location_t * where,
315 unsigned nr_bits, unsigned char value)
316{
317 unsigned char n, mask;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000318
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000319 if (nr_bits == 8) {
320 cmos_write_byte(where->byte_index, value);
321 return;
322 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000323
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000324 n = cmos_read_byte(where->byte_index);
325 mask = ((unsigned char)((1 << nr_bits) - 1)) << where->bit_offset;
326 n = (n & ~mask) + ((value << where->bit_offset) & mask);
327 cmos_write_byte(where->byte_index, n);
328}