Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 1 | /*****************************************************************************\ |
| 2 | * cmos_lowlevel.c |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 3 | ***************************************************************************** |
| 4 | * Copyright (C) 2002-2005 The Regents of the University of California. |
| 5 | * Produced at the Lawrence Livermore National Laboratory. |
| 6 | * Written by David S. Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>. |
| 7 | * UCRL-CODE-2003-012 |
| 8 | * All rights reserved. |
| 9 | * |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 10 | * This file is part of nvramtool, a utility for reading/writing coreboot |
Stefan Reinauer | f527e70 | 2008-01-18 15:33:49 +0000 | [diff] [blame] | 11 | * parameters and displaying information from the coreboot table. |
Uwe Hermann | 6e56594 | 2008-03-01 19:06:32 +0000 | [diff] [blame] | 12 | * For details, see http://coreboot.org/nvramtool. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 13 | * |
| 14 | * Please also read the file DISCLAIMER which is included in this software |
| 15 | * distribution. |
| 16 | * |
| 17 | * This program is free software; you can redistribute it and/or modify it |
| 18 | * under the terms of the GNU General Public License (as published by the |
| 19 | * Free Software Foundation) version 2, dated June 1991. |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, but |
| 22 | * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and |
| 24 | * conditions of the GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License along |
| 27 | * with this program; if not, write to the Free Software Foundation, Inc., |
Stefan Reinauer | ac7a2d2 | 2009-09-23 21:53:25 +0000 | [diff] [blame] | 28 | * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 29 | \*****************************************************************************/ |
| 30 | |
Andriy Gapon | 6a4bf98 | 2008-10-30 15:41:39 +0000 | [diff] [blame] | 31 | #if defined(__FreeBSD__) |
| 32 | #include <fcntl.h> |
| 33 | #include <unistd.h> |
| 34 | #endif |
| 35 | |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 36 | #include "common.h" |
| 37 | #include "cmos_lowlevel.h" |
| 38 | |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame] | 39 | /* Hardware Abstraction Layer: lowlevel byte-wise write access */ |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 40 | |
Patrick Georgi | 1e916e0 | 2011-01-28 07:54:11 +0000 | [diff] [blame] | 41 | extern cmos_access_t cmos_hal, memory_hal; |
Patrick Georgi | 44a89b3 | 2012-05-01 12:10:45 +0200 | [diff] [blame] | 42 | static cmos_access_t *current_access = |
| 43 | #ifdef CMOS_HAL |
| 44 | &cmos_hal; |
| 45 | #else |
| 46 | &memory_hal; |
| 47 | #endif |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame] | 48 | |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 49 | void select_hal(hal_t hal, void *data) |
| 50 | { |
| 51 | switch(hal) { |
Patrick Georgi | 44a89b3 | 2012-05-01 12:10:45 +0200 | [diff] [blame] | 52 | #ifdef CMOS_HAL |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 53 | case HAL_CMOS: |
| 54 | current_access = &cmos_hal; |
| 55 | break; |
Patrick Georgi | 44a89b3 | 2012-05-01 12:10:45 +0200 | [diff] [blame] | 56 | #endif |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 57 | case HAL_MEMORY: |
Zheng Bao | 534e61c | 2012-09-11 12:13:39 +0800 | [diff] [blame] | 58 | default: |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 59 | current_access = &memory_hal; |
| 60 | break; |
| 61 | } |
| 62 | current_access->init(data); |
| 63 | } |
| 64 | |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame] | 65 | /* Bit-level access */ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 66 | typedef struct { |
| 67 | unsigned byte_index; |
| 68 | unsigned bit_offset; |
| 69 | } cmos_bit_op_location_t; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 70 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 71 | static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left, |
| 72 | cmos_bit_op_location_t * where); |
| 73 | static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where, |
| 74 | unsigned nr_bits); |
| 75 | static void cmos_write_bits(const cmos_bit_op_location_t * where, |
| 76 | unsigned nr_bits, unsigned char value); |
| 77 | static unsigned char get_bits(unsigned long long value, unsigned bit, |
| 78 | unsigned nr_bits); |
| 79 | static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits, |
| 80 | unsigned long long *result); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 81 | |
| 82 | /**************************************************************************** |
| 83 | * get_bits |
| 84 | * |
| 85 | * Extract a value 'nr_bits' bits wide starting at bit position 'bit' from |
| 86 | * 'value' and return the result. It is assumed that 'nr_bits' is at most 8. |
| 87 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 88 | static inline unsigned char get_bits(unsigned long long value, unsigned bit, |
| 89 | unsigned nr_bits) |
| 90 | { |
| 91 | return (value >> bit) & ((unsigned char)((1 << nr_bits) - 1)); |
| 92 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 93 | |
| 94 | /**************************************************************************** |
| 95 | * put_bits |
| 96 | * |
| 97 | * Extract the low order 'nr_bits' bits from 'value' and store them in the |
| 98 | * value pointed to by 'result' starting at bit position 'bit'. The bit |
| 99 | * positions in 'result' where the result is stored are assumed to be |
| 100 | * initially zero. |
| 101 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 102 | static inline void put_bits(unsigned char value, unsigned bit, |
| 103 | unsigned nr_bits, unsigned long long *result) |
| 104 | { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 105 | *result += ((unsigned long long)(value & |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 106 | ((unsigned char)((1 << nr_bits) - 1)))) << bit; |
| 107 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 108 | |
| 109 | /**************************************************************************** |
| 110 | * cmos_read |
| 111 | * |
| 112 | * Read value from nonvolatile RAM at position given by 'bit' and 'length' |
| 113 | * and return this value. The I/O privilege level of the currently executing |
| 114 | * process must be set appropriately. |
Andrew Engelbrecht | e890531 | 2014-12-01 12:22:48 -0500 | [diff] [blame] | 115 | * |
| 116 | * Returned value is either (unsigned long long), or malloc()'d (char *) |
| 117 | * cast to (unsigned long long) |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 118 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 119 | unsigned long long cmos_read(const cmos_entry_t * e) |
| 120 | { |
| 121 | cmos_bit_op_location_t where; |
| 122 | unsigned bit = e->bit, length = e->length; |
| 123 | unsigned next_bit, bits_left, nr_bits; |
| 124 | unsigned long long result = 0; |
| 125 | unsigned char value; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 126 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 127 | assert(!verify_cmos_op(bit, length, e->config)); |
| 128 | result = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 129 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 130 | if (e->config == CMOS_ENTRY_STRING) { |
Patrick Georgi | dd1aab9 | 2014-08-09 17:06:20 +0200 | [diff] [blame] | 131 | int strsz = (length + 7) / 8; |
Andrew Engelbrecht | e890531 | 2014-12-01 12:22:48 -0500 | [diff] [blame] | 132 | char *newstring = malloc(strsz); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 133 | unsigned usize = (8 * sizeof(unsigned long long)); |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 134 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 135 | if (!newstring) { |
| 136 | out_of_memory(); |
| 137 | } |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 138 | |
Patrick Georgi | dd1aab9 | 2014-08-09 17:06:20 +0200 | [diff] [blame] | 139 | memset(newstring, 0, strsz); |
| 140 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 141 | for (next_bit = 0, bits_left = length; |
| 142 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 143 | nr_bits = cmos_bit_op_strategy(bit + next_bit, |
| 144 | bits_left > usize ? usize : bits_left, &where); |
| 145 | value = cmos_read_bits(&where, nr_bits); |
| 146 | put_bits(value, next_bit % usize, nr_bits, |
| 147 | &((unsigned long long *)newstring)[next_bit / |
| 148 | usize]); |
| 149 | result = (unsigned long)newstring; |
| 150 | } |
| 151 | } else { |
| 152 | for (next_bit = 0, bits_left = length; |
| 153 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 154 | nr_bits = |
| 155 | cmos_bit_op_strategy(bit + next_bit, bits_left, |
| 156 | &where); |
| 157 | value = cmos_read_bits(&where, nr_bits); |
| 158 | put_bits(value, next_bit, nr_bits, &result); |
| 159 | } |
| 160 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 161 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 162 | return result; |
| 163 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 164 | |
| 165 | /**************************************************************************** |
| 166 | * cmos_write |
| 167 | * |
| 168 | * Write 'data' to nonvolatile RAM at position given by 'bit' and 'length'. |
| 169 | * The I/O privilege level of the currently executing process must be set |
| 170 | * appropriately. |
| 171 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 172 | void cmos_write(const cmos_entry_t * e, unsigned long long value) |
| 173 | { |
| 174 | cmos_bit_op_location_t where; |
| 175 | unsigned bit = e->bit, length = e->length; |
| 176 | unsigned next_bit, bits_left, nr_bits; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 177 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 178 | assert(!verify_cmos_op(bit, length, e->config)); |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 179 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 180 | if (e->config == CMOS_ENTRY_STRING) { |
| 181 | unsigned long long *data = |
| 182 | (unsigned long long *)(unsigned long)value; |
| 183 | unsigned usize = (8 * sizeof(unsigned long long)); |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 184 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 185 | for (next_bit = 0, bits_left = length; |
| 186 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
| 187 | nr_bits = cmos_bit_op_strategy(bit + next_bit, |
| 188 | bits_left > usize ? usize : bits_left, |
| 189 | &where); |
| 190 | value = data[next_bit / usize]; |
| 191 | cmos_write_bits(&where, nr_bits, |
| 192 | get_bits(value, next_bit % usize, nr_bits)); |
| 193 | } |
| 194 | } else { |
| 195 | for (next_bit = 0, bits_left = length; |
| 196 | bits_left; next_bit += nr_bits, bits_left -= nr_bits) { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 197 | nr_bits = cmos_bit_op_strategy(bit + next_bit, |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 198 | bits_left, &where); |
| 199 | cmos_write_bits(&where, nr_bits, |
| 200 | get_bits(value, next_bit, nr_bits)); |
| 201 | } |
| 202 | } |
| 203 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 204 | |
| 205 | /**************************************************************************** |
| 206 | * cmos_read_byte |
| 207 | * |
| 208 | * Read a byte from nonvolatile RAM at a position given by 'index' and return |
| 209 | * the result. An 'index' value of 0 represents the first byte of |
| 210 | * nonvolatile RAM. |
| 211 | * |
| 212 | * Note: the first 14 bytes of nonvolatile RAM provide an interface to the |
| 213 | * real time clock. |
| 214 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 215 | unsigned char cmos_read_byte(unsigned index) |
| 216 | { |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame] | 217 | return current_access->read(index); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 218 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 219 | |
| 220 | /**************************************************************************** |
| 221 | * cmos_write_byte |
| 222 | * |
| 223 | * Write 'value' to nonvolatile RAM at a position given by 'index'. An |
| 224 | * 'index' of 0 represents the first byte of nonvolatile RAM. |
| 225 | * |
| 226 | * Note: the first 14 bytes of nonvolatile RAM provide an interface to the |
| 227 | * real time clock. Writing to any of these bytes will therefore |
| 228 | * affect its functioning. |
| 229 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 230 | void cmos_write_byte(unsigned index, unsigned char value) |
| 231 | { |
Patrick Georgi | be5a178 | 2011-01-21 07:18:20 +0000 | [diff] [blame] | 232 | current_access->write(index, value); |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 233 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 234 | |
| 235 | /**************************************************************************** |
| 236 | * cmos_read_all |
| 237 | * |
| 238 | * Read all contents of CMOS memory into array 'data'. The first 14 bytes of |
| 239 | * 'data' are set to zero since this corresponds to the real time clock area. |
| 240 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 241 | void cmos_read_all(unsigned char data[]) |
| 242 | { |
| 243 | unsigned i; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 244 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 245 | for (i = 0; i < CMOS_RTC_AREA_SIZE; i++) |
| 246 | data[i] = 0; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 247 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 248 | for (; i < CMOS_SIZE; i++) |
| 249 | data[i] = cmos_read_byte(i); |
| 250 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 251 | |
| 252 | /**************************************************************************** |
| 253 | * cmos_write_all |
| 254 | * |
| 255 | * Update all of CMOS memory with the contents of array 'data'. The first 14 |
| 256 | * bytes of 'data' are ignored since this corresponds to the real time clock |
| 257 | * area. |
| 258 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 259 | void cmos_write_all(unsigned char data[]) |
| 260 | { |
| 261 | unsigned i; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 262 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 263 | for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++) |
| 264 | cmos_write_byte(i, data[i]); |
| 265 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 266 | |
| 267 | /**************************************************************************** |
| 268 | * set_iopl |
| 269 | * |
| 270 | * Set the I/O privilege level of the executing process. Root privileges are |
| 271 | * required for performing this action. A sufficient I/O privilege level |
| 272 | * allows the process to access x86 I/O address space and to disable/reenable |
| 273 | * interrupts while executing in user space. Messing with the I/O privilege |
| 274 | * level is therefore somewhat dangerous. |
| 275 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 276 | void set_iopl(int level) |
| 277 | { |
Patrick Georgi | 9cd7eba | 2011-01-21 07:19:59 +0000 | [diff] [blame] | 278 | current_access->set_iopl(level); |
| 279 | } |
| 280 | |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 281 | /**************************************************************************** |
| 282 | * verify_cmos_op |
| 283 | * |
| 284 | * 'bit' represents a bit position in the nonvolatile RAM. The first bit |
| 285 | * (i.e. the lowest order bit of the first byte) of nonvolatile RAM is |
| 286 | * labeled as bit 0. 'length' represents the width in bits of a value we |
| 287 | * wish to read or write. Perform sanity checking on 'bit' and 'length'. If |
| 288 | * no problems were encountered, return OK. Else return an error code. |
| 289 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 290 | int verify_cmos_op(unsigned bit, unsigned length, cmos_entry_config_t config) |
| 291 | { |
| 292 | if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE))) |
| 293 | return CMOS_AREA_OUT_OF_RANGE; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 294 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 295 | if (bit < (8 * CMOS_RTC_AREA_SIZE)) |
| 296 | return CMOS_AREA_OVERLAPS_RTC; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 297 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 298 | if (config == CMOS_ENTRY_STRING) |
| 299 | return OK; |
Stefan Reinauer | a67aab7 | 2008-09-27 10:08:28 +0000 | [diff] [blame] | 300 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 301 | if (length > (8 * sizeof(unsigned long long))) |
| 302 | return CMOS_AREA_TOO_WIDE; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 303 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 304 | return OK; |
| 305 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 306 | |
| 307 | /**************************************************************************** |
| 308 | * cmos_bit_op_strategy |
| 309 | * |
| 310 | * Helper function used by cmos_read() and cmos_write() to determine which |
| 311 | * bits to read or write next. |
| 312 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 313 | static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left, |
| 314 | cmos_bit_op_location_t * where) |
| 315 | { |
| 316 | unsigned max_bits; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 317 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 318 | where->byte_index = bit >> 3; |
| 319 | where->bit_offset = bit & 0x07; |
| 320 | max_bits = 8 - where->bit_offset; |
| 321 | return (bits_left > max_bits) ? max_bits : bits_left; |
| 322 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 323 | |
| 324 | /**************************************************************************** |
| 325 | * cmos_read_bits |
| 326 | * |
| 327 | * Read a chunk of bits from a byte location within CMOS memory. Return the |
| 328 | * value represented by the chunk of bits. |
| 329 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 330 | static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where, |
| 331 | unsigned nr_bits) |
| 332 | { |
| 333 | return (cmos_read_byte(where->byte_index) >> where->bit_offset) & |
| 334 | ((unsigned char)((1 << nr_bits) - 1)); |
| 335 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 336 | |
| 337 | /**************************************************************************** |
| 338 | * cmos_write_bits |
| 339 | * |
| 340 | * Write a chunk of bits (the low order 'nr_bits' bits of 'value') to an area |
| 341 | * within a particular byte of CMOS memory. |
| 342 | ****************************************************************************/ |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 343 | static void cmos_write_bits(const cmos_bit_op_location_t * where, |
| 344 | unsigned nr_bits, unsigned char value) |
| 345 | { |
| 346 | unsigned char n, mask; |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 347 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 348 | if (nr_bits == 8) { |
| 349 | cmos_write_byte(where->byte_index, value); |
| 350 | return; |
| 351 | } |
Stefan Reinauer | 6540ae5 | 2007-07-12 16:35:42 +0000 | [diff] [blame] | 352 | |
Stefan Reinauer | 90b96b6 | 2010-01-13 21:00:23 +0000 | [diff] [blame] | 353 | n = cmos_read_byte(where->byte_index); |
| 354 | mask = ((unsigned char)((1 << nr_bits) - 1)) << where->bit_offset; |
| 355 | n = (n & ~mask) + ((value << where->bit_offset) & mask); |
| 356 | cmos_write_byte(where->byte_index, n); |
| 357 | } |