blob: ef0c3832b133c53fa2168378333d31731daccb5c [file] [log] [blame]
Stefan Reinauer6540ae52007-07-12 16:35:42 +00001/*****************************************************************************\
2 * cmos_lowlevel.c
Stefan Reinauer6540ae52007-07-12 16:35:42 +00003 *****************************************************************************
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 Hermann6e565942008-03-01 19:06:32 +000010 * This file is part of nvramtool, a utility for reading/writing coreboot
Stefan Reinauerf527e702008-01-18 15:33:49 +000011 * parameters and displaying information from the coreboot table.
Uwe Hermann6e565942008-03-01 19:06:32 +000012 * For details, see http://coreboot.org/nvramtool.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000013 *
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 Reinauerac7a2d22009-09-23 21:53:25 +000028 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000029\*****************************************************************************/
30
Andriy Gapon6a4bf982008-10-30 15:41:39 +000031#if defined(__FreeBSD__)
32#include <fcntl.h>
33#include <unistd.h>
34#endif
35
Stefan Reinauer6540ae52007-07-12 16:35:42 +000036#include "common.h"
37#include "cmos_lowlevel.h"
38
Patrick Georgibe5a1782011-01-21 07:18:20 +000039/* Hardware Abstraction Layer: lowlevel byte-wise write access */
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000040
Patrick Georgi1e916e02011-01-28 07:54:11 +000041extern cmos_access_t cmos_hal, memory_hal;
Patrick Georgi44a89b32012-05-01 12:10:45 +020042static cmos_access_t *current_access =
43#ifdef CMOS_HAL
44 &cmos_hal;
45#else
46 &memory_hal;
47#endif
Patrick Georgibe5a1782011-01-21 07:18:20 +000048
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000049void select_hal(hal_t hal, void *data)
50{
51 switch(hal) {
Patrick Georgi44a89b32012-05-01 12:10:45 +020052#ifdef CMOS_HAL
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000053 case HAL_CMOS:
54 current_access = &cmos_hal;
55 break;
Patrick Georgi44a89b32012-05-01 12:10:45 +020056#endif
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000057 case HAL_MEMORY:
Zheng Bao534e61c2012-09-11 12:13:39 +080058 default:
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000059 current_access = &memory_hal;
60 break;
61 }
62 current_access->init(data);
63}
64
Patrick Georgibe5a1782011-01-21 07:18:20 +000065/* Bit-level access */
Stefan Reinauer90b96b62010-01-13 21:00:23 +000066typedef struct {
67 unsigned byte_index;
68 unsigned bit_offset;
69} cmos_bit_op_location_t;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000070
Stefan Reinauer90b96b62010-01-13 21:00:23 +000071static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
72 cmos_bit_op_location_t * where);
73static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
74 unsigned nr_bits);
75static void cmos_write_bits(const cmos_bit_op_location_t * where,
76 unsigned nr_bits, unsigned char value);
77static unsigned char get_bits(unsigned long long value, unsigned bit,
78 unsigned nr_bits);
79static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits,
80 unsigned long long *result);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000081
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 Reinauer90b96b62010-01-13 21:00:23 +000088static 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 Reinauer6540ae52007-07-12 16:35:42 +000093
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 Reinauer90b96b62010-01-13 21:00:23 +0000102static inline void put_bits(unsigned char value, unsigned bit,
103 unsigned nr_bits, unsigned long long *result)
104{
Stefan Reinauer14e22772010-04-27 06:56:47 +0000105 *result += ((unsigned long long)(value &
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000106 ((unsigned char)((1 << nr_bits) - 1)))) << bit;
107}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000108
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.
115 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000116unsigned long long cmos_read(const cmos_entry_t * e)
117{
118 cmos_bit_op_location_t where;
119 unsigned bit = e->bit, length = e->length;
120 unsigned next_bit, bits_left, nr_bits;
121 unsigned long long result = 0;
122 unsigned char value;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000123
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000124 assert(!verify_cmos_op(bit, length, e->config));
125 result = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000126
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000127 if (e->config == CMOS_ENTRY_STRING) {
128 char *newstring = calloc(1, (length + 7) / 8);
129 unsigned usize = (8 * sizeof(unsigned long long));
Stefan Reinauera67aab72008-09-27 10:08:28 +0000130
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000131 if (!newstring) {
132 out_of_memory();
133 }
Stefan Reinauera67aab72008-09-27 10:08:28 +0000134
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000135 for (next_bit = 0, bits_left = length;
136 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
137 nr_bits = cmos_bit_op_strategy(bit + next_bit,
138 bits_left > usize ? usize : bits_left, &where);
139 value = cmos_read_bits(&where, nr_bits);
140 put_bits(value, next_bit % usize, nr_bits,
141 &((unsigned long long *)newstring)[next_bit /
142 usize]);
143 result = (unsigned long)newstring;
144 }
145 } else {
146 for (next_bit = 0, bits_left = length;
147 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
148 nr_bits =
149 cmos_bit_op_strategy(bit + next_bit, bits_left,
150 &where);
151 value = cmos_read_bits(&where, nr_bits);
152 put_bits(value, next_bit, nr_bits, &result);
153 }
154 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000155
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000156 return result;
157}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000158
159/****************************************************************************
160 * cmos_write
161 *
162 * Write 'data' to nonvolatile RAM at position given by 'bit' and 'length'.
163 * The I/O privilege level of the currently executing process must be set
164 * appropriately.
165 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000166void cmos_write(const cmos_entry_t * e, unsigned long long value)
167{
168 cmos_bit_op_location_t where;
169 unsigned bit = e->bit, length = e->length;
170 unsigned next_bit, bits_left, nr_bits;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000171
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000172 assert(!verify_cmos_op(bit, length, e->config));
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000173
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000174 if (e->config == CMOS_ENTRY_STRING) {
175 unsigned long long *data =
176 (unsigned long long *)(unsigned long)value;
177 unsigned usize = (8 * sizeof(unsigned long long));
Stefan Reinauera67aab72008-09-27 10:08:28 +0000178
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000179 for (next_bit = 0, bits_left = length;
180 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
181 nr_bits = cmos_bit_op_strategy(bit + next_bit,
182 bits_left > usize ? usize : bits_left,
183 &where);
184 value = data[next_bit / usize];
185 cmos_write_bits(&where, nr_bits,
186 get_bits(value, next_bit % usize, nr_bits));
187 }
188 } else {
189 for (next_bit = 0, bits_left = length;
190 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000191 nr_bits = cmos_bit_op_strategy(bit + next_bit,
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000192 bits_left, &where);
193 cmos_write_bits(&where, nr_bits,
194 get_bits(value, next_bit, nr_bits));
195 }
196 }
197}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000198
199/****************************************************************************
200 * cmos_read_byte
201 *
202 * Read a byte from nonvolatile RAM at a position given by 'index' and return
203 * the result. An 'index' value of 0 represents the first byte of
204 * nonvolatile RAM.
205 *
206 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
207 * real time clock.
208 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000209unsigned char cmos_read_byte(unsigned index)
210{
Patrick Georgibe5a1782011-01-21 07:18:20 +0000211 return current_access->read(index);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000212}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000213
214/****************************************************************************
215 * cmos_write_byte
216 *
217 * Write 'value' to nonvolatile RAM at a position given by 'index'. An
218 * 'index' of 0 represents the first byte of nonvolatile RAM.
219 *
220 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
221 * real time clock. Writing to any of these bytes will therefore
222 * affect its functioning.
223 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000224void cmos_write_byte(unsigned index, unsigned char value)
225{
Patrick Georgibe5a1782011-01-21 07:18:20 +0000226 current_access->write(index, value);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000227}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000228
229/****************************************************************************
230 * cmos_read_all
231 *
232 * Read all contents of CMOS memory into array 'data'. The first 14 bytes of
233 * 'data' are set to zero since this corresponds to the real time clock area.
234 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000235void cmos_read_all(unsigned char data[])
236{
237 unsigned i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000238
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000239 for (i = 0; i < CMOS_RTC_AREA_SIZE; i++)
240 data[i] = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000241
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000242 for (; i < CMOS_SIZE; i++)
243 data[i] = cmos_read_byte(i);
244}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000245
246/****************************************************************************
247 * cmos_write_all
248 *
249 * Update all of CMOS memory with the contents of array 'data'. The first 14
250 * bytes of 'data' are ignored since this corresponds to the real time clock
251 * area.
252 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000253void cmos_write_all(unsigned char data[])
254{
255 unsigned i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000256
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000257 for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++)
258 cmos_write_byte(i, data[i]);
259}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000260
261/****************************************************************************
262 * set_iopl
263 *
264 * Set the I/O privilege level of the executing process. Root privileges are
265 * required for performing this action. A sufficient I/O privilege level
266 * allows the process to access x86 I/O address space and to disable/reenable
267 * interrupts while executing in user space. Messing with the I/O privilege
268 * level is therefore somewhat dangerous.
269 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000270void set_iopl(int level)
271{
Patrick Georgi9cd7eba2011-01-21 07:19:59 +0000272 current_access->set_iopl(level);
273}
274
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000275/****************************************************************************
276 * verify_cmos_op
277 *
278 * 'bit' represents a bit position in the nonvolatile RAM. The first bit
279 * (i.e. the lowest order bit of the first byte) of nonvolatile RAM is
280 * labeled as bit 0. 'length' represents the width in bits of a value we
281 * wish to read or write. Perform sanity checking on 'bit' and 'length'. If
282 * no problems were encountered, return OK. Else return an error code.
283 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000284int verify_cmos_op(unsigned bit, unsigned length, cmos_entry_config_t config)
285{
286 if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE)))
287 return CMOS_AREA_OUT_OF_RANGE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000288
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000289 if (bit < (8 * CMOS_RTC_AREA_SIZE))
290 return CMOS_AREA_OVERLAPS_RTC;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000291
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000292 if (config == CMOS_ENTRY_STRING)
293 return OK;
Stefan Reinauera67aab72008-09-27 10:08:28 +0000294
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000295 if (length > (8 * sizeof(unsigned long long)))
296 return CMOS_AREA_TOO_WIDE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000297
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000298 return OK;
299}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000300
301/****************************************************************************
302 * cmos_bit_op_strategy
303 *
304 * Helper function used by cmos_read() and cmos_write() to determine which
305 * bits to read or write next.
306 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000307static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
308 cmos_bit_op_location_t * where)
309{
310 unsigned max_bits;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000311
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000312 where->byte_index = bit >> 3;
313 where->bit_offset = bit & 0x07;
314 max_bits = 8 - where->bit_offset;
315 return (bits_left > max_bits) ? max_bits : bits_left;
316}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000317
318/****************************************************************************
319 * cmos_read_bits
320 *
321 * Read a chunk of bits from a byte location within CMOS memory. Return the
322 * value represented by the chunk of bits.
323 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000324static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
325 unsigned nr_bits)
326{
327 return (cmos_read_byte(where->byte_index) >> where->bit_offset) &
328 ((unsigned char)((1 << nr_bits) - 1));
329}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000330
331/****************************************************************************
332 * cmos_write_bits
333 *
334 * Write a chunk of bits (the low order 'nr_bits' bits of 'value') to an area
335 * within a particular byte of CMOS memory.
336 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000337static void cmos_write_bits(const cmos_bit_op_location_t * where,
338 unsigned nr_bits, unsigned char value)
339{
340 unsigned char n, mask;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000341
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000342 if (nr_bits == 8) {
343 cmos_write_byte(where->byte_index, value);
344 return;
345 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000346
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000347 n = cmos_read_byte(where->byte_index);
348 mask = ((unsigned char)((1 << nr_bits) - 1)) << where->bit_offset;
349 n = (n & ~mask) + ((value << where->bit_offset) & mask);
350 cmos_write_byte(where->byte_index, n);
351}