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 */
42 void (*init)(void* data);
43 unsigned char (*read)(unsigned addr);
44 void (*write)(unsigned addr, unsigned char value);
45 void (*set_iopl)(int level);
48 static void cmos_hal_init(void* data);
49 static unsigned char cmos_hal_read(unsigned addr);
50 static void cmos_hal_write(unsigned addr, unsigned char value);
51 static void cmos_set_iopl(int level);
53 static cmos_access_t cmos_hal = {
54 .init = cmos_hal_init,
55 .read = cmos_hal_read,
56 .write = cmos_hal_write,
57 .set_iopl = cmos_set_iopl,
60 static void mem_hal_init(void* data);
61 static unsigned char mem_hal_read(unsigned addr);
62 static void mem_hal_write(unsigned addr, unsigned char value);
63 static void mem_set_iopl(int level);
65 static cmos_access_t memory_hal = {
68 .write = mem_hal_write,
69 .set_iopl = mem_set_iopl,
72 static cmos_access_t *current_access = &cmos_hal;
74 /* no need to initialize anything */
75 static void cmos_hal_init(__attribute__((unused)) void *data)
79 static unsigned char cmos_hal_read(unsigned index)
81 unsigned short port_0, port_1;
83 assert(!verify_cmos_byte_index(index));
97 static void cmos_hal_write(unsigned index, unsigned char value)
99 unsigned short port_0, port_1;
101 assert(!verify_cmos_byte_index(index));
115 static unsigned char* mem_hal_data = (unsigned char*)-1;
116 static void mem_hal_init(void *data)
121 static unsigned char mem_hal_read(unsigned index)
123 assert(mem_hal_data != (unsigned char*)-1);
124 return mem_hal_data[index];
127 static void mem_hal_write(unsigned index, unsigned char value)
129 assert(mem_hal_data != (unsigned char*)-1);
130 mem_hal_data[index] = value;
133 void select_hal(hal_t hal, void *data)
137 current_access = &cmos_hal;
140 current_access = &memory_hal;
143 current_access->init(data);
146 /* Bit-level access */
150 } cmos_bit_op_location_t;
152 static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
153 cmos_bit_op_location_t * where);
154 static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
156 static void cmos_write_bits(const cmos_bit_op_location_t * where,
157 unsigned nr_bits, unsigned char value);
158 static unsigned char get_bits(unsigned long long value, unsigned bit,
160 static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits,
161 unsigned long long *result);
163 /****************************************************************************
166 * Extract a value 'nr_bits' bits wide starting at bit position 'bit' from
167 * 'value' and return the result. It is assumed that 'nr_bits' is at most 8.
168 ****************************************************************************/
169 static inline unsigned char get_bits(unsigned long long value, unsigned bit,
172 return (value >> bit) & ((unsigned char)((1 << nr_bits) - 1));
175 /****************************************************************************
178 * Extract the low order 'nr_bits' bits from 'value' and store them in the
179 * value pointed to by 'result' starting at bit position 'bit'. The bit
180 * positions in 'result' where the result is stored are assumed to be
182 ****************************************************************************/
183 static inline void put_bits(unsigned char value, unsigned bit,
184 unsigned nr_bits, unsigned long long *result)
186 *result += ((unsigned long long)(value &
187 ((unsigned char)((1 << nr_bits) - 1)))) << bit;
190 /****************************************************************************
193 * Read value from nonvolatile RAM at position given by 'bit' and 'length'
194 * and return this value. The I/O privilege level of the currently executing
195 * process must be set appropriately.
196 ****************************************************************************/
197 unsigned long long cmos_read(const cmos_entry_t * e)
199 cmos_bit_op_location_t where;
200 unsigned bit = e->bit, length = e->length;
201 unsigned next_bit, bits_left, nr_bits;
202 unsigned long long result = 0;
205 assert(!verify_cmos_op(bit, length, e->config));
208 if (e->config == CMOS_ENTRY_STRING) {
209 char *newstring = calloc(1, (length + 7) / 8);
210 unsigned usize = (8 * sizeof(unsigned long long));
216 for (next_bit = 0, bits_left = length;
217 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
218 nr_bits = cmos_bit_op_strategy(bit + next_bit,
219 bits_left > usize ? usize : bits_left, &where);
220 value = cmos_read_bits(&where, nr_bits);
221 put_bits(value, next_bit % usize, nr_bits,
222 &((unsigned long long *)newstring)[next_bit /
224 result = (unsigned long)newstring;
227 for (next_bit = 0, bits_left = length;
228 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
230 cmos_bit_op_strategy(bit + next_bit, bits_left,
232 value = cmos_read_bits(&where, nr_bits);
233 put_bits(value, next_bit, nr_bits, &result);
240 /****************************************************************************
243 * Write 'data' to nonvolatile RAM at position given by 'bit' and 'length'.
244 * The I/O privilege level of the currently executing process must be set
246 ****************************************************************************/
247 void cmos_write(const cmos_entry_t * e, unsigned long long value)
249 cmos_bit_op_location_t where;
250 unsigned bit = e->bit, length = e->length;
251 unsigned next_bit, bits_left, nr_bits;
253 assert(!verify_cmos_op(bit, length, e->config));
255 if (e->config == CMOS_ENTRY_STRING) {
256 unsigned long long *data =
257 (unsigned long long *)(unsigned long)value;
258 unsigned usize = (8 * sizeof(unsigned long long));
260 for (next_bit = 0, bits_left = length;
261 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
262 nr_bits = cmos_bit_op_strategy(bit + next_bit,
263 bits_left > usize ? usize : bits_left,
265 value = data[next_bit / usize];
266 cmos_write_bits(&where, nr_bits,
267 get_bits(value, next_bit % usize, nr_bits));
270 for (next_bit = 0, bits_left = length;
271 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
272 nr_bits = cmos_bit_op_strategy(bit + next_bit,
274 cmos_write_bits(&where, nr_bits,
275 get_bits(value, next_bit, nr_bits));
280 /****************************************************************************
283 * Read a byte from nonvolatile RAM at a position given by 'index' and return
284 * the result. An 'index' value of 0 represents the first byte of
287 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
289 ****************************************************************************/
290 unsigned char cmos_read_byte(unsigned index)
292 return current_access->read(index);
295 /****************************************************************************
298 * Write 'value' to nonvolatile RAM at a position given by 'index'. An
299 * 'index' of 0 represents the first byte of nonvolatile RAM.
301 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
302 * real time clock. Writing to any of these bytes will therefore
303 * affect its functioning.
304 ****************************************************************************/
305 void cmos_write_byte(unsigned index, unsigned char value)
307 current_access->write(index, value);
310 /****************************************************************************
313 * Read all contents of CMOS memory into array 'data'. The first 14 bytes of
314 * 'data' are set to zero since this corresponds to the real time clock area.
315 ****************************************************************************/
316 void cmos_read_all(unsigned char data[])
320 for (i = 0; i < CMOS_RTC_AREA_SIZE; i++)
323 for (; i < CMOS_SIZE; i++)
324 data[i] = cmos_read_byte(i);
327 /****************************************************************************
330 * Update all of CMOS memory with the contents of array 'data'. The first 14
331 * bytes of 'data' are ignored since this corresponds to the real time clock
333 ****************************************************************************/
334 void cmos_write_all(unsigned char data[])
338 for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++)
339 cmos_write_byte(i, data[i]);
342 /****************************************************************************
345 * Set the I/O privilege level of the executing process. Root privileges are
346 * required for performing this action. A sufficient I/O privilege level
347 * allows the process to access x86 I/O address space and to disable/reenable
348 * interrupts while executing in user space. Messing with the I/O privilege
349 * level is therefore somewhat dangerous.
350 ****************************************************************************/
351 void set_iopl(int level)
353 current_access->set_iopl(level);
356 static void cmos_set_iopl(int level)
358 #if defined(__FreeBSD__)
359 static int io_fd = -1;
362 assert((level >= 0) && (level <= 3));
364 #if defined(__FreeBSD__)
372 io_fd = open("/dev/io", O_RDWR);
381 fprintf(stderr, "%s: iopl() system call failed. "
382 "You must be root to do this.\n", prog_name);
388 static void mem_set_iopl(__attribute__ ((unused)) int level)
392 /****************************************************************************
395 * 'bit' represents a bit position in the nonvolatile RAM. The first bit
396 * (i.e. the lowest order bit of the first byte) of nonvolatile RAM is
397 * labeled as bit 0. 'length' represents the width in bits of a value we
398 * wish to read or write. Perform sanity checking on 'bit' and 'length'. If
399 * no problems were encountered, return OK. Else return an error code.
400 ****************************************************************************/
401 int verify_cmos_op(unsigned bit, unsigned length, cmos_entry_config_t config)
403 if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE)))
404 return CMOS_AREA_OUT_OF_RANGE;
406 if (bit < (8 * CMOS_RTC_AREA_SIZE))
407 return CMOS_AREA_OVERLAPS_RTC;
409 if (config == CMOS_ENTRY_STRING)
412 if (length > (8 * sizeof(unsigned long long)))
413 return CMOS_AREA_TOO_WIDE;
418 /****************************************************************************
419 * cmos_bit_op_strategy
421 * Helper function used by cmos_read() and cmos_write() to determine which
422 * bits to read or write next.
423 ****************************************************************************/
424 static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
425 cmos_bit_op_location_t * where)
429 where->byte_index = bit >> 3;
430 where->bit_offset = bit & 0x07;
431 max_bits = 8 - where->bit_offset;
432 return (bits_left > max_bits) ? max_bits : bits_left;
435 /****************************************************************************
438 * Read a chunk of bits from a byte location within CMOS memory. Return the
439 * value represented by the chunk of bits.
440 ****************************************************************************/
441 static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
444 return (cmos_read_byte(where->byte_index) >> where->bit_offset) &
445 ((unsigned char)((1 << nr_bits) - 1));
448 /****************************************************************************
451 * Write a chunk of bits (the low order 'nr_bits' bits of 'value') to an area
452 * within a particular byte of CMOS memory.
453 ****************************************************************************/
454 static void cmos_write_bits(const cmos_bit_op_location_t * where,
455 unsigned nr_bits, unsigned char value)
457 unsigned char n, mask;
460 cmos_write_byte(where->byte_index, value);
464 n = cmos_read_byte(where->byte_index);
465 mask = ((unsigned char)((1 << nr_bits) - 1)) << where->bit_offset;
466 n = (n & ~mask) + ((value << where->bit_offset) & mask);
467 cmos_write_byte(where->byte_index, n);