summaryrefslogtreecommitdiff
path: root/util/lxbios/cmos_lowlevel.c
blob: ab116797a26eb50aad23c6b5fcaa70c88bb9d8bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*****************************************************************************\
 * cmos_lowlevel.c
 * $Id$
 *****************************************************************************
 *  Copyright (C) 2002-2005 The Regents of the University of California.
 *  Produced at the Lawrence Livermore National Laboratory.
 *  Written by David S. Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>.
 *  UCRL-CODE-2003-012
 *  All rights reserved.
 *
 *  This file is part of nvramtool, a utility for reading/writing coreboot
 *  parameters and displaying information from the coreboot table.
 *  For details, see http://coreboot.org/nvramtool.
 *
 *  Please also read the file DISCLAIMER which is included in this software
 *  distribution.
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License (as published by the
 *  Free Software Foundation) version 2, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the terms and
 *  conditions of the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
\*****************************************************************************/

#include <sys/io.h>
#include "common.h"
#include "cmos_lowlevel.h"

typedef struct
 { unsigned byte_index;
   unsigned bit_offset;
 }
cmos_bit_op_location_t;

static unsigned cmos_bit_op_strategy (unsigned bit, unsigned bits_left,
                                      cmos_bit_op_location_t *where);
static unsigned char cmos_read_bits (const cmos_bit_op_location_t *where,
                                     unsigned nr_bits);
static void cmos_write_bits (const cmos_bit_op_location_t *where,
                             unsigned nr_bits, unsigned char value);
static unsigned char get_bits (unsigned long long value, unsigned bit,
                               unsigned nr_bits);
static void put_bits (unsigned char value, unsigned bit, unsigned nr_bits,
                      unsigned long long *result);

/****************************************************************************
 * get_bits
 *
 * Extract a value 'nr_bits' bits wide starting at bit position 'bit' from
 * 'value' and return the result.  It is assumed that 'nr_bits' is at most 8.
 ****************************************************************************/
static inline unsigned char get_bits (unsigned long long value, unsigned bit,
                                      unsigned nr_bits)
 { return (value >> bit) & ((unsigned char) ((1 << nr_bits) - 1)); }

/****************************************************************************
 * put_bits
 *
 * Extract the low order 'nr_bits' bits from 'value' and store them in the
 * value pointed to by 'result' starting at bit position 'bit'.  The bit
 * positions in 'result' where the result is stored are assumed to be
 * initially zero.
 ****************************************************************************/
static inline void put_bits (unsigned char value, unsigned bit,
                             unsigned nr_bits, unsigned long long *result)
 { *result += (value & ((unsigned char) ((1 << nr_bits) - 1))) << bit; }

/****************************************************************************
 * cmos_read
 *
 * Read value from nonvolatile RAM at position given by 'bit' and 'length'
 * and return this value.  The I/O privilege level of the currently executing
 * process must be set appropriately.
 ****************************************************************************/
unsigned long long cmos_read (unsigned bit, unsigned length)
 { cmos_bit_op_location_t where;
   unsigned next_bit, bits_left, nr_bits;
   unsigned long long result;
   unsigned char value;

   assert(!verify_cmos_op(bit, length));
   result = 0;

   for (next_bit = 0, bits_left = length;
        bits_left;
        next_bit += nr_bits, bits_left -= nr_bits)
    { nr_bits = cmos_bit_op_strategy(bit + next_bit, bits_left, &where);
      value = cmos_read_bits(&where, nr_bits);
      put_bits(value, next_bit, nr_bits, &result);
    }

   return result;
 }

/****************************************************************************
 * cmos_write
 *
 * Write 'data' to nonvolatile RAM at position given by 'bit' and 'length'.
 * The I/O privilege level of the currently executing process must be set
 * appropriately.
 ****************************************************************************/
void cmos_write (unsigned bit, unsigned length, unsigned long long value)
 { cmos_bit_op_location_t where;
   unsigned next_bit, bits_left, nr_bits;

   assert(!verify_cmos_op(bit, length));

   for (next_bit = 0, bits_left = length;
        bits_left;
        next_bit += nr_bits, bits_left -= nr_bits)
    { nr_bits = cmos_bit_op_strategy(bit + next_bit, bits_left, &where);
      cmos_write_bits(&where, nr_bits, get_bits(value, next_bit, nr_bits));
    }
 }

/****************************************************************************
 * cmos_read_byte
 *
 * Read a byte from nonvolatile RAM at a position given by 'index' and return
 * the result.  An 'index' value of 0 represents the first byte of
 * nonvolatile RAM.
 *
 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
 *       real time clock.
 ****************************************************************************/
unsigned char cmos_read_byte (unsigned index)
 { unsigned short port_0, port_1;

   assert(!verify_cmos_byte_index(index));

   if (index < 128)
    { port_0 = 0x70;
      port_1 = 0x71;
    }
   else
    { port_0 = 0x72;
      port_1 = 0x73;
    }

   outb(index, port_0);
   return inb(port_1);
 }

/****************************************************************************
 * cmos_write_byte
 *
 * Write 'value' to nonvolatile RAM at a position given by 'index'.  An
 * 'index' of 0 represents the first byte of nonvolatile RAM.
 *
 * Note: the first 14 bytes of nonvolatile RAM provide an interface to the
 *       real time clock.  Writing to any of these bytes will therefore
 *       affect its functioning.
 ****************************************************************************/
void cmos_write_byte (unsigned index, unsigned char value)
 { unsigned short port_0, port_1;

   assert(!verify_cmos_byte_index(index));

   if (index < 128)
    { port_0 = 0x70;
      port_1 = 0x71;
    }
   else
    { port_0 = 0x72;
      port_1 = 0x73;
    }

   outb(index, port_0);
   outb(value, port_1);
 }

/****************************************************************************
 * cmos_read_all
 *
 * Read all contents of CMOS memory into array 'data'.  The first 14 bytes of
 * 'data' are set to zero since this corresponds to the real time clock area.
 ****************************************************************************/
void cmos_read_all (unsigned char data[])
 { unsigned i;

   for (i = 0; i < CMOS_RTC_AREA_SIZE; i++)
      data[i] = 0;

   for (; i < CMOS_SIZE; i++)
      data[i] = cmos_read_byte(i);
 }

/****************************************************************************
 * cmos_write_all
 *
 * Update all of CMOS memory with the contents of array 'data'.  The first 14
 * bytes of 'data' are ignored since this corresponds to the real time clock
 * area.
 ****************************************************************************/
void cmos_write_all (unsigned char data[])
 { unsigned i;

   for (i = CMOS_RTC_AREA_SIZE; i < CMOS_SIZE; i++)
      cmos_write_byte(i, data[i]);
 }

/****************************************************************************
 * set_iopl
 *
 * Set the I/O privilege level of the executing process.  Root privileges are
 * required for performing this action.  A sufficient I/O privilege level
 * allows the process to access x86 I/O address space and to disable/reenable
 * interrupts while executing in user space.  Messing with the I/O privilege
 * level is therefore somewhat dangerous.
 ****************************************************************************/
void set_iopl (int level)
 { assert((level >= 0) && (level <= 3));

   if (iopl(level))
    { fprintf(stderr,
              "%s: iopl() system call failed.  You must be root to do "
              "this.\n",
              prog_name);
      exit(1);
    }
 }

/****************************************************************************
 * verify_cmos_op
 *
 * 'bit' represents a bit position in the nonvolatile RAM.  The first bit
 * (i.e. the lowest order bit of the first byte) of nonvolatile RAM is
 * labeled as bit 0.  'length' represents the width in bits of a value we
 * wish to read or write.  Perform sanity checking on 'bit' and 'length'.  If
 * no problems were encountered, return OK.  Else return an error code.
 ****************************************************************************/
int verify_cmos_op (unsigned bit, unsigned length)
 { if ((bit >= (8 * CMOS_SIZE)) || ((bit + length) > (8 * CMOS_SIZE)))
      return CMOS_AREA_OUT_OF_RANGE;

   if (bit < (8 * CMOS_RTC_AREA_SIZE))
      return CMOS_AREA_OVERLAPS_RTC;

   if (length > (8 * sizeof(unsigned long long)))
      return CMOS_AREA_TOO_WIDE;

   return OK;
 }

/****************************************************************************
 * cmos_bit_op_strategy
 *
 * Helper function used by cmos_read() and cmos_write() to determine which
 * bits to read or write next.
 ****************************************************************************/
static unsigned cmos_bit_op_strategy (unsigned bit, unsigned bits_left,
                                      cmos_bit_op_location_t *where)
 { unsigned max_bits;

   where->byte_index = bit >> 3;
   where->bit_offset = bit & 0x07;
   max_bits = 8 - where->bit_offset;
   return (bits_left > max_bits) ? max_bits : bits_left;
 }

/****************************************************************************
 * cmos_read_bits
 *
 * Read a chunk of bits from a byte location within CMOS memory.  Return the
 * value represented by the chunk of bits.
 ****************************************************************************/
static unsigned char cmos_read_bits (const cmos_bit_op_location_t *where,
                                     unsigned nr_bits)
 { return (cmos_read_byte(where->byte_index) >> where->bit_offset) &
          ((unsigned char) ((1 << nr_bits) - 1));
 }

/****************************************************************************
 * cmos_write_bits
 *
 * Write a chunk of bits (the low order 'nr_bits' bits of 'value') to an area
 * within a particular byte of CMOS memory.
 ****************************************************************************/
static void cmos_write_bits (const cmos_bit_op_location_t *where,
                             unsigned nr_bits, unsigned char value)
 { unsigned char n, mask;

   if (nr_bits == 8)
    { cmos_write_byte(where->byte_index, value);
      return;
    }

   n = cmos_read_byte(where->byte_index);
   mask = ((unsigned char) ((1 << nr_bits) - 1)) << where->bit_offset;
   n = (n & ~mask) + ((value << where->bit_offset) & mask);
   cmos_write_byte(where->byte_index, n);
 }