blob: 55b1879ff02a0435acf3464253263095151d42c7 [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:
58 current_access = &memory_hal;
59 break;
60 }
61 current_access->init(data);
62}
63
Patrick Georgibe5a1782011-01-21 07:18:20 +000064/* Bit-level access */
Stefan Reinauer90b96b62010-01-13 21:00:23 +000065typedef struct {
66 unsigned byte_index;
67 unsigned bit_offset;
68} cmos_bit_op_location_t;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000069
Stefan Reinauer90b96b62010-01-13 21:00:23 +000070static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
71 cmos_bit_op_location_t * where);
72static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
73 unsigned nr_bits);
74static void cmos_write_bits(const cmos_bit_op_location_t * where,
75 unsigned nr_bits, unsigned char value);
76static unsigned char get_bits(unsigned long long value, unsigned bit,
77 unsigned nr_bits);
78static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits,
79 unsigned long long *result);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000080
81/****************************************************************************
82 * get_bits
83 *
84 * Extract a value 'nr_bits' bits wide starting at bit position 'bit' from
85 * 'value' and return the result. It is assumed that 'nr_bits' is at most 8.
86 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +000087static inline unsigned char get_bits(unsigned long long value, unsigned bit,
88 unsigned nr_bits)
89{
90 return (value >> bit) & ((unsigned char)((1 << nr_bits) - 1));
91}
Stefan Reinauer6540ae52007-07-12 16:35:42 +000092
93/****************************************************************************
94 * put_bits
95 *
96 * Extract the low order 'nr_bits' bits from 'value' and store them in the
97 * value pointed to by 'result' starting at bit position 'bit'. The bit
98 * positions in 'result' where the result is stored are assumed to be
99 * initially zero.
100 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000101static inline void put_bits(unsigned char value, unsigned bit,
102 unsigned nr_bits, unsigned long long *result)
103{
Stefan Reinauer14e22772010-04-27 06:56:47 +0000104 *result += ((unsigned long long)(value &
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000105 ((unsigned char)((1 << nr_bits) - 1)))) << bit;
106}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000107
108/****************************************************************************
109 * cmos_read
110 *
111 * Read value from nonvolatile RAM at position given by 'bit' and 'length'
112 * and return this value. The I/O privilege level of the currently executing
113 * process must be set appropriately.
114 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000115unsigned 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 Reinauer6540ae52007-07-12 16:35:42 +0000122
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000123 assert(!verify_cmos_op(bit, length, e->config));
124 result = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000125
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000126 if (e->config == CMOS_ENTRY_STRING) {
127 char *newstring = calloc(1, (length + 7) / 8);
128 unsigned usize = (8 * sizeof(unsigned long long));
Stefan Reinauera67aab72008-09-27 10:08:28 +0000129
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000130 if (!newstring) {
131 out_of_memory();
132 }
Stefan Reinauera67aab72008-09-27 10:08:28 +0000133
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000134 for (next_bit = 0, bits_left = length;
135 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
136 nr_bits = cmos_bit_op_strategy(bit + next_bit,
137 bits_left > usize ? usize : bits_left, &where);
138 value = cmos_read_bits(&where, nr_bits);
139 put_bits(value, next_bit % usize, nr_bits,
140 &((unsigned long long *)newstring)[next_bit /
141 usize]);
142 result = (unsigned long)newstring;
143 }
144 } else {
145 for (next_bit = 0, bits_left = length;
146 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
147 nr_bits =
148 cmos_bit_op_strategy(bit + next_bit, bits_left,
149 &where);
150 value = cmos_read_bits(&where, nr_bits);
151 put_bits(value, next_bit, nr_bits, &result);
152 }
153 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000154
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000155 return result;
156}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000157
158/****************************************************************************
159 * cmos_write
160 *
161 * Write 'data' to nonvolatile RAM at position given by 'bit' and 'length'.
162 * The I/O privilege level of the currently executing process must be set
163 * appropriately.
164 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000165void cmos_write(const cmos_entry_t * e, unsigned long long value)
166{
167 cmos_bit_op_location_t where;
168 unsigned bit = e->bit, length = e->length;
169 unsigned next_bit, bits_left, nr_bits;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000170
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000171 assert(!verify_cmos_op(bit, length, e->config));
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000172
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000173 if (e->config == CMOS_ENTRY_STRING) {
174 unsigned long long *data =
175 (unsigned long long *)(unsigned long)value;
176 unsigned usize = (8 * sizeof(unsigned long long));
Stefan Reinauera67aab72008-09-27 10:08:28 +0000177
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000178 for (next_bit = 0, bits_left = length;
179 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
180 nr_bits = cmos_bit_op_strategy(bit + next_bit,
181 bits_left > usize ? usize : bits_left,
182 &where);
183 value = data[next_bit / usize];
184 cmos_write_bits(&where, nr_bits,
185 get_bits(value, next_bit % usize, nr_bits));
186 }
187 } else {
188 for (next_bit = 0, bits_left = length;
189 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000190 nr_bits = cmos_bit_op_strategy(bit + next_bit,
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000191 bits_left, &where);
192 cmos_write_bits(&where, nr_bits,
193 get_bits(value, next_bit, nr_bits));
194 }
195 }
196}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000197
198/****************************************************************************
199 * cmos_read_byte
200 *
201 * Read a byte from nonvolatile RAM at a position given by 'index' and return
202 * the result. An 'index' value of 0 represents the first byte of
203 * nonvolatile RAM.
204 *
205 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
206 * real time clock.
207 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000208unsigned char cmos_read_byte(unsigned index)
209{
Patrick Georgibe5a1782011-01-21 07:18:20 +0000210 return current_access->read(index);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000211}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000212
213/****************************************************************************
214 * cmos_write_byte
215 *
216 * Write 'value' to nonvolatile RAM at a position given by 'index'. An
217 * 'index' of 0 represents the first byte of nonvolatile RAM.
218 *
219 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
220 * real time clock. Writing to any of these bytes will therefore
221 * affect its functioning.
222 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000223void cmos_write_byte(unsigned index, unsigned char value)
224{
Patrick Georgibe5a1782011-01-21 07:18:20 +0000225 current_access->write(index, value);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000226}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000227
228/****************************************************************************
229 * cmos_read_all
230 *
231 * Read all contents of CMOS memory into array 'data'. The first 14 bytes of
232 * 'data' are set to zero since this corresponds to the real time clock area.
233 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000234void cmos_read_all(unsigned char data[])
235{
236 unsigned i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000237
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000238 for (i = 0; i < CMOS_RTC_AREA_SIZE; i++)
239 data[i] = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000240
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000241 for (; i < CMOS_SIZE; i++)
242 data[i] = cmos_read_byte(i);
243}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000244
245/****************************************************************************
246 * cmos_write_all
247 *
248 * Update all of CMOS memory with the contents of array 'data'. The first 14
249 * bytes of 'data' are ignored since this corresponds to the real time clock
250 * area.
251 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000252void cmos_write_all(unsigned char data[])
253{
254 unsigned i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000255
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000256 for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++)
257 cmos_write_byte(i, data[i]);
258}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000259
260/****************************************************************************
261 * set_iopl
262 *
263 * Set the I/O privilege level of the executing process. Root privileges are
264 * required for performing this action. A sufficient I/O privilege level
265 * allows the process to access x86 I/O address space and to disable/reenable
266 * interrupts while executing in user space. Messing with the I/O privilege
267 * level is therefore somewhat dangerous.
268 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000269void set_iopl(int level)
270{
Patrick Georgi9cd7eba2011-01-21 07:19:59 +0000271 current_access->set_iopl(level);
272}
273
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000274/****************************************************************************
275 * verify_cmos_op
276 *
277 * 'bit' represents a bit position in the nonvolatile RAM. The first bit
278 * (i.e. the lowest order bit of the first byte) of nonvolatile RAM is
279 * labeled as bit 0. 'length' represents the width in bits of a value we
280 * wish to read or write. Perform sanity checking on 'bit' and 'length'. If
281 * no problems were encountered, return OK. Else return an error code.
282 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000283int verify_cmos_op(unsigned bit, unsigned length, cmos_entry_config_t config)
284{
285 if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE)))
286 return CMOS_AREA_OUT_OF_RANGE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000287
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000288 if (bit < (8 * CMOS_RTC_AREA_SIZE))
289 return CMOS_AREA_OVERLAPS_RTC;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000290
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000291 if (config == CMOS_ENTRY_STRING)
292 return OK;
Stefan Reinauera67aab72008-09-27 10:08:28 +0000293
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000294 if (length > (8 * sizeof(unsigned long long)))
295 return CMOS_AREA_TOO_WIDE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000296
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000297 return OK;
298}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000299
300/****************************************************************************
301 * cmos_bit_op_strategy
302 *
303 * Helper function used by cmos_read() and cmos_write() to determine which
304 * bits to read or write next.
305 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000306static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
307 cmos_bit_op_location_t * where)
308{
309 unsigned max_bits;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000310
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000311 where->byte_index = bit >> 3;
312 where->bit_offset = bit & 0x07;
313 max_bits = 8 - where->bit_offset;
314 return (bits_left > max_bits) ? max_bits : bits_left;
315}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000316
317/****************************************************************************
318 * cmos_read_bits
319 *
320 * Read a chunk of bits from a byte location within CMOS memory. Return the
321 * value represented by the chunk of bits.
322 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000323static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
324 unsigned nr_bits)
325{
326 return (cmos_read_byte(where->byte_index) >> where->bit_offset) &
327 ((unsigned char)((1 << nr_bits) - 1));
328}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000329
330/****************************************************************************
331 * cmos_write_bits
332 *
333 * Write a chunk of bits (the low order 'nr_bits' bits of 'value') to an area
334 * within a particular byte of CMOS memory.
335 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000336static void cmos_write_bits(const cmos_bit_op_location_t * where,
337 unsigned nr_bits, unsigned char value)
338{
339 unsigned char n, mask;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000340
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000341 if (nr_bits == 8) {
342 cmos_write_byte(where->byte_index, value);
343 return;
344 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000345
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000346 n = cmos_read_byte(where->byte_index);
347 mask = ((unsigned char)((1 << nr_bits) - 1)) << where->bit_offset;
348 n = (n & ~mask) + ((value << where->bit_offset) & mask);
349 cmos_write_byte(where->byte_index, n);
350}