blob: 7221f2393ece02ff1c30ebd8b8389a69ab28f14e [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.
Stefan Reinauer6540ae52007-07-12 16:35:42 +000025\*****************************************************************************/
26
Andriy Gapon6a4bf982008-10-30 15:41:39 +000027#if defined(__FreeBSD__)
28#include <fcntl.h>
29#include <unistd.h>
30#endif
31
Stefan Reinauer6540ae52007-07-12 16:35:42 +000032#include "common.h"
33#include "cmos_lowlevel.h"
34
Patrick Georgibe5a1782011-01-21 07:18:20 +000035/* Hardware Abstraction Layer: lowlevel byte-wise write access */
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000036
Patrick Georgi1e916e02011-01-28 07:54:11 +000037extern cmos_access_t cmos_hal, memory_hal;
Patrick Georgi44a89b32012-05-01 12:10:45 +020038static cmos_access_t *current_access =
39#ifdef CMOS_HAL
40 &cmos_hal;
41#else
42 &memory_hal;
43#endif
Patrick Georgibe5a1782011-01-21 07:18:20 +000044
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000045void select_hal(hal_t hal, void *data)
46{
47 switch(hal) {
Patrick Georgi44a89b32012-05-01 12:10:45 +020048#ifdef CMOS_HAL
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000049 case HAL_CMOS:
50 current_access = &cmos_hal;
51 break;
Patrick Georgi44a89b32012-05-01 12:10:45 +020052#endif
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000053 case HAL_MEMORY:
Zheng Bao534e61c2012-09-11 12:13:39 +080054 default:
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000055 current_access = &memory_hal;
56 break;
57 }
58 current_access->init(data);
59}
60
Patrick Georgibe5a1782011-01-21 07:18:20 +000061/* Bit-level access */
Stefan Reinauer90b96b62010-01-13 21:00:23 +000062typedef struct {
63 unsigned byte_index;
64 unsigned bit_offset;
65} cmos_bit_op_location_t;
Stefan Reinauer6540ae52007-07-12 16:35:42 +000066
Stefan Reinauer90b96b62010-01-13 21:00:23 +000067static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
68 cmos_bit_op_location_t * where);
69static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
70 unsigned nr_bits);
71static void cmos_write_bits(const cmos_bit_op_location_t * where,
72 unsigned nr_bits, unsigned char value);
73static unsigned char get_bits(unsigned long long value, unsigned bit,
74 unsigned nr_bits);
75static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits,
76 unsigned long long *result);
Stefan Reinauer6540ae52007-07-12 16:35:42 +000077
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 Reinauer90b96b62010-01-13 21:00:23 +000084static 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 Reinauer6540ae52007-07-12 16:35:42 +000089
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 Reinauer90b96b62010-01-13 21:00:23 +000098static inline void put_bits(unsigned char value, unsigned bit,
99 unsigned nr_bits, unsigned long long *result)
100{
Stefan Reinauer14e22772010-04-27 06:56:47 +0000101 *result += ((unsigned long long)(value &
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000102 ((unsigned char)((1 << nr_bits) - 1)))) << bit;
103}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000104
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 Engelbrechte8905312014-12-01 12:22:48 -0500111 *
112 * Returned value is either (unsigned long long), or malloc()'d (char *)
113 * cast to (unsigned long long)
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000114 ****************************************************************************/
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) {
Patrick Georgi59ab4e92014-12-01 22:09:33 +0100127 int strsz = (length + 7) / 8 + 1;
Andrew Engelbrechte8905312014-12-01 12:22:48 -0500128 char *newstring = malloc(strsz);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000129 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
Patrick Georgidd1aab92014-08-09 17:06:20 +0200135 memset(newstring, 0, strsz);
136
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000137 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 Reinauer6540ae52007-07-12 16:35:42 +0000157
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000158 return result;
159}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000160
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 Reinauer90b96b62010-01-13 21:00:23 +0000168void 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 Reinauer6540ae52007-07-12 16:35:42 +0000173
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000174 assert(!verify_cmos_op(bit, length, e->config));
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000175
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000176 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 Reinauera67aab72008-09-27 10:08:28 +0000180
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000181 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 Reinauer14e22772010-04-27 06:56:47 +0000193 nr_bits = cmos_bit_op_strategy(bit + next_bit,
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000194 bits_left, &where);
195 cmos_write_bits(&where, nr_bits,
196 get_bits(value, next_bit, nr_bits));
197 }
198 }
199}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000200
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 Reinauer90b96b62010-01-13 21:00:23 +0000211unsigned char cmos_read_byte(unsigned index)
212{
Patrick Georgibe5a1782011-01-21 07:18:20 +0000213 return current_access->read(index);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000214}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000215
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 Reinauer90b96b62010-01-13 21:00:23 +0000226void cmos_write_byte(unsigned index, unsigned char value)
227{
Patrick Georgibe5a1782011-01-21 07:18:20 +0000228 current_access->write(index, value);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000229}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000230
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 Reinauer90b96b62010-01-13 21:00:23 +0000237void cmos_read_all(unsigned char data[])
238{
239 unsigned i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000240
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000241 for (i = 0; i < CMOS_RTC_AREA_SIZE; i++)
242 data[i] = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000243
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000244 for (; i < CMOS_SIZE; i++)
245 data[i] = cmos_read_byte(i);
246}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000247
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 Reinauer90b96b62010-01-13 21:00:23 +0000255void cmos_write_all(unsigned char data[])
256{
257 unsigned i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000258
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000259 for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++)
260 cmos_write_byte(i, data[i]);
261}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000262
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 Reinauer90b96b62010-01-13 21:00:23 +0000272void set_iopl(int level)
273{
Patrick Georgi9cd7eba2011-01-21 07:19:59 +0000274 current_access->set_iopl(level);
275}
276
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000277/****************************************************************************
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 Reinauer90b96b62010-01-13 21:00:23 +0000286int 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 Reinauer6540ae52007-07-12 16:35:42 +0000290
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000291 if (bit < (8 * CMOS_RTC_AREA_SIZE))
292 return CMOS_AREA_OVERLAPS_RTC;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000293
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000294 if (config == CMOS_ENTRY_STRING)
295 return OK;
Stefan Reinauera67aab72008-09-27 10:08:28 +0000296
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000297 if (length > (8 * sizeof(unsigned long long)))
298 return CMOS_AREA_TOO_WIDE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000299
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000300 return OK;
301}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000302
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 Reinauer90b96b62010-01-13 21:00:23 +0000309static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
310 cmos_bit_op_location_t * where)
311{
312 unsigned max_bits;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000313
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000314 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 Reinauer6540ae52007-07-12 16:35:42 +0000319
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 Reinauer90b96b62010-01-13 21:00:23 +0000326static 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 Reinauer6540ae52007-07-12 16:35:42 +0000332
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 Reinauer90b96b62010-01-13 21:00:23 +0000339static 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 Reinauer6540ae52007-07-12 16:35:42 +0000343
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000344 if (nr_bits == 8) {
345 cmos_write_byte(where->byte_index, value);
346 return;
347 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000348
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000349 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}