blob: 8e19f515aa70743dadd57c0cd48111f30cd6abcd [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 Georgibe5a1782011-01-21 07:18:20 +000041typedef struct {
42 void (*init)(void* data);
43 unsigned char (*read)(unsigned addr);
44 void (*write)(unsigned addr, unsigned char value);
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000045 void (*set_iopl)(int level);
Patrick Georgibe5a1782011-01-21 07:18:20 +000046} cmos_access_t;
47
48static void cmos_hal_init(void* data);
49static unsigned char cmos_hal_read(unsigned addr);
50static void cmos_hal_write(unsigned addr, unsigned char value);
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000051static void cmos_set_iopl(int level);
Patrick Georgibe5a1782011-01-21 07:18:20 +000052
53static cmos_access_t cmos_hal = {
54 .init = cmos_hal_init,
55 .read = cmos_hal_read,
Patrick Georgi9cd7eba2011-01-21 07:19:59 +000056 .write = cmos_hal_write,
57 .set_iopl = cmos_set_iopl,
58};
59
60static void mem_hal_init(void* data);
61static unsigned char mem_hal_read(unsigned addr);
62static void mem_hal_write(unsigned addr, unsigned char value);
63static void mem_set_iopl(int level);
64
65static cmos_access_t memory_hal = {
66 .init = mem_hal_init,
67 .read = mem_hal_read,
68 .write = mem_hal_write,
69 .set_iopl = mem_set_iopl,
Patrick Georgibe5a1782011-01-21 07:18:20 +000070};
71
72static cmos_access_t *current_access = &cmos_hal;
73
74/* no need to initialize anything */
75static void cmos_hal_init(__attribute__((unused)) void *data)
76{
77}
78
79static unsigned char cmos_hal_read(unsigned index)
80{
81 unsigned short port_0, port_1;
82
83 assert(!verify_cmos_byte_index(index));
84
85 if (index < 128) {
86 port_0 = 0x70;
87 port_1 = 0x71;
88 } else {
89 port_0 = 0x72;
90 port_1 = 0x73;
91 }
92
93 OUTB(index, port_0);
94 return INB(port_1);
95}
96
97static void cmos_hal_write(unsigned index, unsigned char value)
98{
99 unsigned short port_0, port_1;
100
101 assert(!verify_cmos_byte_index(index));
102
103 if (index < 128) {
104 port_0 = 0x70;
105 port_1 = 0x71;
106 } else {
107 port_0 = 0x72;
108 port_1 = 0x73;
109 }
110
111 OUTB(index, port_0);
112 OUTB(value, port_1);
113}
114
Patrick Georgi9cd7eba2011-01-21 07:19:59 +0000115static unsigned char* mem_hal_data = (unsigned char*)-1;
116static void mem_hal_init(void *data)
117{
118 mem_hal_data = data;
119}
120
121static unsigned char mem_hal_read(unsigned index)
122{
123 assert(mem_hal_data != (unsigned char*)-1);
124 return mem_hal_data[index];
125}
126
127static void mem_hal_write(unsigned index, unsigned char value)
128{
129 assert(mem_hal_data != (unsigned char*)-1);
130 mem_hal_data[index] = value;
131}
132
133void select_hal(hal_t hal, void *data)
134{
135 switch(hal) {
136 case HAL_CMOS:
137 current_access = &cmos_hal;
138 break;
139 case HAL_MEMORY:
140 current_access = &memory_hal;
141 break;
142 }
143 current_access->init(data);
144}
145
Patrick Georgibe5a1782011-01-21 07:18:20 +0000146/* Bit-level access */
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000147typedef struct {
148 unsigned byte_index;
149 unsigned bit_offset;
150} cmos_bit_op_location_t;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000151
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000152static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
153 cmos_bit_op_location_t * where);
154static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
155 unsigned nr_bits);
156static void cmos_write_bits(const cmos_bit_op_location_t * where,
157 unsigned nr_bits, unsigned char value);
158static unsigned char get_bits(unsigned long long value, unsigned bit,
159 unsigned nr_bits);
160static void put_bits(unsigned char value, unsigned bit, unsigned nr_bits,
161 unsigned long long *result);
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000162
163/****************************************************************************
164 * get_bits
165 *
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 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000169static inline unsigned char get_bits(unsigned long long value, unsigned bit,
170 unsigned nr_bits)
171{
172 return (value >> bit) & ((unsigned char)((1 << nr_bits) - 1));
173}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000174
175/****************************************************************************
176 * put_bits
177 *
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
181 * initially zero.
182 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000183static inline void put_bits(unsigned char value, unsigned bit,
184 unsigned nr_bits, unsigned long long *result)
185{
Stefan Reinauer14e22772010-04-27 06:56:47 +0000186 *result += ((unsigned long long)(value &
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000187 ((unsigned char)((1 << nr_bits) - 1)))) << bit;
188}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000189
190/****************************************************************************
191 * cmos_read
192 *
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 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000197unsigned long long cmos_read(const cmos_entry_t * e)
198{
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;
203 unsigned char value;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000204
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000205 assert(!verify_cmos_op(bit, length, e->config));
206 result = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000207
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000208 if (e->config == CMOS_ENTRY_STRING) {
209 char *newstring = calloc(1, (length + 7) / 8);
210 unsigned usize = (8 * sizeof(unsigned long long));
Stefan Reinauera67aab72008-09-27 10:08:28 +0000211
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000212 if (!newstring) {
213 out_of_memory();
214 }
Stefan Reinauera67aab72008-09-27 10:08:28 +0000215
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000216 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 /
223 usize]);
224 result = (unsigned long)newstring;
225 }
226 } else {
227 for (next_bit = 0, bits_left = length;
228 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
229 nr_bits =
230 cmos_bit_op_strategy(bit + next_bit, bits_left,
231 &where);
232 value = cmos_read_bits(&where, nr_bits);
233 put_bits(value, next_bit, nr_bits, &result);
234 }
235 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000236
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000237 return result;
238}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000239
240/****************************************************************************
241 * cmos_write
242 *
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
245 * appropriately.
246 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000247void cmos_write(const cmos_entry_t * e, unsigned long long value)
248{
249 cmos_bit_op_location_t where;
250 unsigned bit = e->bit, length = e->length;
251 unsigned next_bit, bits_left, nr_bits;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000252
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000253 assert(!verify_cmos_op(bit, length, e->config));
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000254
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000255 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));
Stefan Reinauera67aab72008-09-27 10:08:28 +0000259
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000260 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,
264 &where);
265 value = data[next_bit / usize];
266 cmos_write_bits(&where, nr_bits,
267 get_bits(value, next_bit % usize, nr_bits));
268 }
269 } else {
270 for (next_bit = 0, bits_left = length;
271 bits_left; next_bit += nr_bits, bits_left -= nr_bits) {
Stefan Reinauer14e22772010-04-27 06:56:47 +0000272 nr_bits = cmos_bit_op_strategy(bit + next_bit,
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000273 bits_left, &where);
274 cmos_write_bits(&where, nr_bits,
275 get_bits(value, next_bit, nr_bits));
276 }
277 }
278}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000279
280/****************************************************************************
281 * cmos_read_byte
282 *
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
285 * nonvolatile RAM.
286 *
287 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
288 * real time clock.
289 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000290unsigned char cmos_read_byte(unsigned index)
291{
Patrick Georgibe5a1782011-01-21 07:18:20 +0000292 return current_access->read(index);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000293}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000294
295/****************************************************************************
296 * cmos_write_byte
297 *
298 * Write 'value' to nonvolatile RAM at a position given by 'index'. An
299 * 'index' of 0 represents the first byte of nonvolatile RAM.
300 *
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 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000305void cmos_write_byte(unsigned index, unsigned char value)
306{
Patrick Georgibe5a1782011-01-21 07:18:20 +0000307 current_access->write(index, value);
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000308}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000309
310/****************************************************************************
311 * cmos_read_all
312 *
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 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000316void cmos_read_all(unsigned char data[])
317{
318 unsigned i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000319
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000320 for (i = 0; i < CMOS_RTC_AREA_SIZE; i++)
321 data[i] = 0;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000322
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000323 for (; i < CMOS_SIZE; i++)
324 data[i] = cmos_read_byte(i);
325}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000326
327/****************************************************************************
328 * cmos_write_all
329 *
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
332 * area.
333 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000334void cmos_write_all(unsigned char data[])
335{
336 unsigned i;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000337
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000338 for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++)
339 cmos_write_byte(i, data[i]);
340}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000341
342/****************************************************************************
343 * set_iopl
344 *
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 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000351void set_iopl(int level)
352{
Patrick Georgi9cd7eba2011-01-21 07:19:59 +0000353 current_access->set_iopl(level);
354}
355
356static void cmos_set_iopl(int level)
357{
Andriy Gapon6a4bf982008-10-30 15:41:39 +0000358#if defined(__FreeBSD__)
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000359 static int io_fd = -1;
Andriy Gapon6a4bf982008-10-30 15:41:39 +0000360#endif
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000361
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000362 assert((level >= 0) && (level <= 3));
Andriy Gapon6a4bf982008-10-30 15:41:39 +0000363
364#if defined(__FreeBSD__)
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000365 if (level == 0) {
366 if (io_fd != -1) {
367 close(io_fd);
368 io_fd = -1;
369 }
370 } else {
371 if (io_fd == -1) {
372 io_fd = open("/dev/io", O_RDWR);
373 if (io_fd < 0) {
374 perror("/dev/io");
375 exit(1);
376 }
377 }
378 }
Andriy Gapon6a4bf982008-10-30 15:41:39 +0000379#else
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000380 if (iopl(level)) {
381 fprintf(stderr, "%s: iopl() system call failed. "
382 "You must be root to do this.\n", prog_name);
383 exit(1);
384 }
Andriy Gapon6a4bf982008-10-30 15:41:39 +0000385#endif
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000386}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000387
Patrick Georgi9cd7eba2011-01-21 07:19:59 +0000388static void mem_set_iopl(__attribute__ ((unused)) int level)
389{
390}
391
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000392/****************************************************************************
393 * verify_cmos_op
394 *
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 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000401int verify_cmos_op(unsigned bit, unsigned length, cmos_entry_config_t config)
402{
403 if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE)))
404 return CMOS_AREA_OUT_OF_RANGE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000405
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000406 if (bit < (8 * CMOS_RTC_AREA_SIZE))
407 return CMOS_AREA_OVERLAPS_RTC;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000408
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000409 if (config == CMOS_ENTRY_STRING)
410 return OK;
Stefan Reinauera67aab72008-09-27 10:08:28 +0000411
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000412 if (length > (8 * sizeof(unsigned long long)))
413 return CMOS_AREA_TOO_WIDE;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000414
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000415 return OK;
416}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000417
418/****************************************************************************
419 * cmos_bit_op_strategy
420 *
421 * Helper function used by cmos_read() and cmos_write() to determine which
422 * bits to read or write next.
423 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000424static unsigned cmos_bit_op_strategy(unsigned bit, unsigned bits_left,
425 cmos_bit_op_location_t * where)
426{
427 unsigned max_bits;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000428
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000429 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;
433}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000434
435/****************************************************************************
436 * cmos_read_bits
437 *
438 * Read a chunk of bits from a byte location within CMOS memory. Return the
439 * value represented by the chunk of bits.
440 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000441static unsigned char cmos_read_bits(const cmos_bit_op_location_t * where,
442 unsigned nr_bits)
443{
444 return (cmos_read_byte(where->byte_index) >> where->bit_offset) &
445 ((unsigned char)((1 << nr_bits) - 1));
446}
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000447
448/****************************************************************************
449 * cmos_write_bits
450 *
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 ****************************************************************************/
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000454static void cmos_write_bits(const cmos_bit_op_location_t * where,
455 unsigned nr_bits, unsigned char value)
456{
457 unsigned char n, mask;
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000458
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000459 if (nr_bits == 8) {
460 cmos_write_byte(where->byte_index, value);
461 return;
462 }
Stefan Reinauer6540ae52007-07-12 16:35:42 +0000463
Stefan Reinauer90b96b62010-01-13 21:00:23 +0000464 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);
468}