Patrick Georgi | 55189c9 | 2020-05-10 20:09:31 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 2 | |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 3 | #if defined(__FreeBSD__) |
| 4 | #include <fcntl.h> |
| 5 | #include <unistd.h> |
| 6 | #endif |
| 7 | |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 8 | #include "common.h" |
| 9 | #include "cmos_lowlevel.h" |
| 10 | |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame] | 11 | /* Hardware Abstraction Layer: lowlevel byte-wise write access */ |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 12 | |
Patrick Georgi | 1e916e0 | 2011-01-28 07:54:11 +0000 | [diff] [blame] | 13 | extern cmos_access_t cmos_hal, memory_hal; |
Patrick Georgi | 44a89b3 | 2012-05-01 12:10:45 +0200 | [diff] [blame] | 14 | static cmos_access_t *current_access = |
| 15 | #ifdef CMOS_HAL |
| 16 | &cmos_hal; |
| 17 | #else |
| 18 | &memory_hal; |
| 19 | #endif |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame] | 20 | |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 21 | void select_hal(hal_t hal, void *data) |
| 22 | { |
| 23 | switch(hal) { |
Patrick Georgi | 44a89b3 | 2012-05-01 12:10:45 +0200 | [diff] [blame] | 24 | #ifdef CMOS_HAL |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 25 | case HAL_CMOS: |
| 26 | current_access = &cmos_hal; |
| 27 | break; |
Patrick Georgi | 44a89b3 | 2012-05-01 12:10:45 +0200 | [diff] [blame] | 28 | #endif |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 29 | case HAL_MEMORY: |
Zheng Bao | 534e61c | 2012-09-11 12:13:39 +0800 | [diff] [blame] | 30 | default: |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 31 | current_access = &memory_hal; |
| 32 | break; |
| 33 | } |
| 34 | current_access->init(data); |
| 35 | } |
| 36 | |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame] | 37 | /* Bit-level access */ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 38 | typedef struct { |
| 39 | unsigned byte_index; |
| 40 | unsigned bit_offset; |
| 41 | } cmos_bit_op_location_t; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 42 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 43 | static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left, |
| 44 | cmos_bit_op_location_t * where); |
| 45 | static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where, |
| 46 | unsigned nr_bits); |
| 47 | static void cmos_write_bits(const cmos_bit_op_location_t * where, |
| 48 | unsigned nr_bits, unsigned char value); |
| 49 | static unsigned char get_bits(unsigned long long value, unsigned bit, |
| 50 | unsigned nr_bits); |
| 51 | static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits, |
| 52 | unsigned long long *result); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 53 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 60 | static 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 65 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 74 | static inline void put_bits(unsigned char value, unsigned bit, |
| 75 | unsigned nr_bits, unsigned long long *result) |
| 76 | { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 77 | *result += ((unsigned long long)(value & |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 78 | ((unsigned char)((1 << nr_bits) - 1)))) << bit; |
| 79 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 80 | |
| 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 Engelbrecht | e890531 | 2014-12-01 12:22:48 -0500 | [diff] [blame] | 87 | * |
| 88 | * Returned value is either (unsigned long long), or malloc()'d (char *) |
| 89 | * cast to (unsigned long long) |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 90 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 91 | unsigned 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 98 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 99 | assert(!verify_cmos_op(bit, length, e->config)); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 100 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 101 | if (e->config == CMOS_ENTRY_STRING) { |
Patrick Georgi | 59ab4e9 | 2014-12-01 22:09:33 +0100 | [diff] [blame] | 102 | int strsz = (length + 7) / 8 + 1; |
Andrew Engelbrecht | e890531 | 2014-12-01 12:22:48 -0500 | [diff] [blame] | 103 | char *newstring = malloc(strsz); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 104 | unsigned usize = (8 * sizeof(unsigned long long)); |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 105 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 106 | if (!newstring) { |
| 107 | out_of_memory(); |
| 108 | } |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 109 | |
Patrick Georgi | dd1aab9 | 2014-08-09 17:06:20 +0200 | [diff] [blame] | 110 | memset(newstring, 0, strsz); |
| 111 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 112 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 132 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 133 | return result; |
| 134 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 135 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 143 | void 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 148 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 149 | assert(!verify_cmos_op(bit, length, e->config)); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 150 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 151 | 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 Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 155 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 156 | 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 Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 168 | nr_bits = cmos_bit_op_strategy(bit + next_bit, |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 169 | bits_left, &where); |
| 170 | cmos_write_bits(&where, nr_bits, |
| 171 | get_bits(value, next_bit, nr_bits)); |
| 172 | } |
| 173 | } |
| 174 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 175 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 186 | unsigned char cmos_read_byte(unsigned index) |
| 187 | { |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame] | 188 | return current_access->read(index); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 189 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 190 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 201 | void cmos_write_byte(unsigned index, unsigned char value) |
| 202 | { |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame] | 203 | current_access->write(index, value); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 204 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 205 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 212 | void cmos_read_all(unsigned char data[]) |
| 213 | { |
| 214 | unsigned i; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 215 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 216 | for (i = 0; i < CMOS_RTC_AREA_SIZE; i++) |
| 217 | data[i] = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 218 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 219 | for (; i < CMOS_SIZE; i++) |
| 220 | data[i] = cmos_read_byte(i); |
| 221 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 222 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 230 | void cmos_write_all(unsigned char data[]) |
| 231 | { |
| 232 | unsigned i; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 233 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 234 | for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++) |
| 235 | cmos_write_byte(i, data[i]); |
| 236 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 237 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 247 | void set_iopl(int level) |
| 248 | { |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 249 | current_access->set_iopl(level); |
| 250 | } |
| 251 | |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 252 | /**************************************************************************** |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 261 | int 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 265 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 266 | if (bit < (8 * CMOS_RTC_AREA_SIZE)) |
| 267 | return CMOS_AREA_OVERLAPS_RTC; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 268 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 269 | if (config == CMOS_ENTRY_STRING) |
| 270 | return OK; |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 271 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 272 | if (length > (8 * sizeof(unsigned long long))) |
| 273 | return CMOS_AREA_TOO_WIDE; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 274 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 275 | return OK; |
| 276 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 277 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 284 | static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left, |
| 285 | cmos_bit_op_location_t * where) |
| 286 | { |
| 287 | unsigned max_bits; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 288 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 289 | 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 294 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 301 | static 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 307 | |
| 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 Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 314 | static 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 Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 318 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 319 | if (nr_bits == 8) { |
| 320 | cmos_write_byte(where->byte_index, value); |
| 321 | return; |
| 322 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 323 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 324 | 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 | } |