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