1 /*****************************************************************************\
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>.
10 * This file is part of nvramtool, a utility for reading/writing coreboot
11 * parameters and displaying information from the coreboot table.
12 * For details, see http://coreboot.org/nvramtool.
14 * Please also read the file DISCLAIMER which is included in this software
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.
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.
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.,
28 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 \*****************************************************************************/
31 #if defined(__FreeBSD__)
37 #include "cmos_lowlevel.h"
39 /* Hardware Abstraction Layer: lowlevel byte-wise write access */
41 extern cmos_access_t cmos_hal, memory_hal;
42 static cmos_access_t *current_access = &cmos_hal;
44 void select_hal(hal_t hal, void *data)
48 current_access = &cmos_hal;
51 current_access = &memory_hal;
54 current_access->init(data);
57 /* Bit-level access */
61 } cmos_bit_op_location_t;
63 static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
64 cmos_bit_op_location_t * where);
65 static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
67 static void cmos_write_bits(const cmos_bit_op_location_t * where,
68 unsigned nr_bits, unsigned char value);
69 static unsigned char get_bits(unsigned long long value, unsigned bit,
71 static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits,
72 unsigned long long *result);
74 /****************************************************************************
77 * Extract a value 'nr_bits' bits wide starting at bit position 'bit' from
78 * 'value' and return the result. It is assumed that 'nr_bits' is at most 8.
79 ****************************************************************************/
80 static inline unsigned char get_bits(unsigned long long value, unsigned bit,
83 return (value >> bit) & ((unsigned char)((1 << nr_bits) - 1));
86 /****************************************************************************
89 * Extract the low order 'nr_bits' bits from 'value' and store them in the
90 * value pointed to by 'result' starting at bit position 'bit'. The bit
91 * positions in 'result' where the result is stored are assumed to be
93 ****************************************************************************/
94 static inline void put_bits(unsigned char value, unsigned bit,
95 unsigned nr_bits, unsigned long long *result)
97 *result += ((unsigned long long)(value &
98 ((unsigned char)((1 << nr_bits) - 1)))) << bit;
101 /****************************************************************************
104 * Read value from nonvolatile RAM at position given by 'bit' and 'length'
105 * and return this value. The I/O privilege level of the currently executing
106 * process must be set appropriately.
107 ****************************************************************************/
108 unsigned long long cmos_read(const cmos_entry_t * e)
110 cmos_bit_op_location_t where;
111 unsigned bit = e->bit, length = e->length;
112 unsigned next_bit, bits_left, nr_bits;
113 unsigned long long result = 0;
116 assert(!verify_cmos_op(bit, length, e->config));
119 if (e->config == CMOS_ENTRY_STRING) {
120 char *newstring = calloc(1, (length + 7) / 8);
121 unsigned usize = (8 * sizeof(unsigned long long));
127 for (next_bit = 0, bits_left = length;
128 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
129 nr_bits = cmos_bit_op_strategy(bit + next_bit,
130 bits_left > usize ? usize : bits_left, &where);
131 value = cmos_read_bits(&where, nr_bits);
132 put_bits(value, next_bit % usize, nr_bits,
133 &((unsigned long long *)newstring)[next_bit /
135 result = (unsigned long)newstring;
138 for (next_bit = 0, bits_left = length;
139 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
141 cmos_bit_op_strategy(bit + next_bit, bits_left,
143 value = cmos_read_bits(&where, nr_bits);
144 put_bits(value, next_bit, nr_bits, &result);
151 /****************************************************************************
154 * Write 'data' to nonvolatile RAM at position given by 'bit' and 'length'.
155 * The I/O privilege level of the currently executing process must be set
157 ****************************************************************************/
158 void cmos_write(const cmos_entry_t * e, unsigned long long value)
160 cmos_bit_op_location_t where;
161 unsigned bit = e->bit, length = e->length;
162 unsigned next_bit, bits_left, nr_bits;
164 assert(!verify_cmos_op(bit, length, e->config));
166 if (e->config == CMOS_ENTRY_STRING) {
167 unsigned long long *data =
168 (unsigned long long *)(unsigned long)value;
169 unsigned usize = (8 * sizeof(unsigned long long));
171 for (next_bit = 0, bits_left = length;
172 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
173 nr_bits = cmos_bit_op_strategy(bit + next_bit,
174 bits_left > usize ? usize : bits_left,
176 value = data[next_bit / usize];
177 cmos_write_bits(&where, nr_bits,
178 get_bits(value, next_bit % usize, nr_bits));
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,
185 cmos_write_bits(&where, nr_bits,
186 get_bits(value, next_bit, nr_bits));
191 /****************************************************************************
194 * Read a byte from nonvolatile RAM at a position given by 'index' and return
195 * the result. An 'index' value of 0 represents the first byte of
198 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
200 ****************************************************************************/
201 unsigned char cmos_read_byte(unsigned index)
203 return current_access->read(index);
206 /****************************************************************************
209 * Write 'value' to nonvolatile RAM at a position given by 'index'. An
210 * 'index' of 0 represents the first byte of nonvolatile RAM.
212 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
213 * real time clock. Writing to any of these bytes will therefore
214 * affect its functioning.
215 ****************************************************************************/
216 void cmos_write_byte(unsigned index, unsigned char value)
218 current_access->write(index, value);
221 /****************************************************************************
224 * Read all contents of CMOS memory into array 'data'. The first 14 bytes of
225 * 'data' are set to zero since this corresponds to the real time clock area.
226 ****************************************************************************/
227 void cmos_read_all(unsigned char data[])
231 for (i = 0; i < CMOS_RTC_AREA_SIZE; i++)
234 for (; i < CMOS_SIZE; i++)
235 data[i] = cmos_read_byte(i);
238 /****************************************************************************
241 * Update all of CMOS memory with the contents of array 'data'. The first 14
242 * bytes of 'data' are ignored since this corresponds to the real time clock
244 ****************************************************************************/
245 void cmos_write_all(unsigned char data[])
249 for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++)
250 cmos_write_byte(i, data[i]);
253 /****************************************************************************
256 * Set the I/O privilege level of the executing process. Root privileges are
257 * required for performing this action. A sufficient I/O privilege level
258 * allows the process to access x86 I/O address space and to disable/reenable
259 * interrupts while executing in user space. Messing with the I/O privilege
260 * level is therefore somewhat dangerous.
261 ****************************************************************************/
262 void set_iopl(int level)
264 current_access->set_iopl(level);
267 /****************************************************************************
270 * 'bit' represents a bit position in the nonvolatile RAM. The first bit
271 * (i.e. the lowest order bit of the first byte) of nonvolatile RAM is
272 * labeled as bit 0. 'length' represents the width in bits of a value we
273 * wish to read or write. Perform sanity checking on 'bit' and 'length'. If
274 * no problems were encountered, return OK. Else return an error code.
275 ****************************************************************************/
276 int verify_cmos_op(unsigned bit, unsigned length, cmos_entry_config_t config)
278 if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE)))
279 return CMOS_AREA_OUT_OF_RANGE;
281 if (bit < (8 * CMOS_RTC_AREA_SIZE))
282 return CMOS_AREA_OVERLAPS_RTC;
284 if (config == CMOS_ENTRY_STRING)
287 if (length > (8 * sizeof(unsigned long long)))
288 return CMOS_AREA_TOO_WIDE;
293 /****************************************************************************
294 * cmos_bit_op_strategy
296 * Helper function used by cmos_read() and cmos_write() to determine which
297 * bits to read or write next.
298 ****************************************************************************/
299 static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
300 cmos_bit_op_location_t * where)
304 where->byte_index = bit >> 3;
305 where->bit_offset = bit & 0x07;
306 max_bits = 8 - where->bit_offset;
307 return (bits_left > max_bits) ? max_bits : bits_left;
310 /****************************************************************************
313 * Read a chunk of bits from a byte location within CMOS memory. Return the
314 * value represented by the chunk of bits.
315 ****************************************************************************/
316 static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
319 return (cmos_read_byte(where->byte_index) >> where->bit_offset) &
320 ((unsigned char)((1 << nr_bits) - 1));
323 /****************************************************************************
326 * Write a chunk of bits (the low order 'nr_bits' bits of 'value') to an area
327 * within a particular byte of CMOS memory.
328 ****************************************************************************/
329 static void cmos_write_bits(const cmos_bit_op_location_t * where,
330 unsigned nr_bits, unsigned char value)
332 unsigned char n, mask;
335 cmos_write_byte(where->byte_index, value);
339 n = cmos_read_byte(where->byte_index);
340 mask = ((unsigned char)((1 << nr_bits) - 1)) << where->bit_offset;
341 n = (n & ~mask) + ((value << where->bit_offset) & mask);
342 cmos_write_byte(where->byte_index, n);