1 /*****************************************************************************\
3 *****************************************************************************
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>.
10 * This file is part of nvramtool, a utility for reading/writing coreboot
11 * parameters and displaying information from the coreboot table.
12 * For details, see http://coreboot.org/nvramtool.
14 * Please also read the file DISCLAIMER which is included in this software
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.
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.
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.,
28 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 \*****************************************************************************/
33 #include "cmos_lowlevel.h"
35 static int prepare_cmos_op_common(const cmos_entry_t
* e
);
37 /****************************************************************************
38 * prepare_cmos_op_common
40 * Perform a few checks common to both reads and writes.
41 ****************************************************************************/
42 static int prepare_cmos_op_common(const cmos_entry_t
* e
)
46 if (e
->config
== CMOS_ENTRY_RESERVED
)
47 /* Access to reserved parameters is not permitted. */
48 return CMOS_OP_RESERVED
;
50 if ((result
= verify_cmos_op(e
->bit
, e
->length
, e
->config
)) != OK
)
53 assert(e
->length
> 0);
57 /****************************************************************************
60 * The caller wishes to read a CMOS parameter represented by 'e'. Perform
61 * sanity checking on 'e'. If a problem was found with e, return an error
62 * code. Else return OK.
63 ****************************************************************************/
64 int prepare_cmos_read(const cmos_entry_t
* e
)
68 if ((result
= prepare_cmos_op_common(e
)) != OK
)
74 case CMOS_ENTRY_STRING
:
84 /****************************************************************************
87 * The caller wishes to set a CMOS parameter represented by 'e' to a value
88 * whose string representation is stored in 'value_str'. Perform sanity
89 * checking on 'value_str'. On error, return an error code. Else store the
90 * numeric equivalent of 'value_str' in '*value' and return OK.
91 ****************************************************************************/
92 int prepare_cmos_write(const cmos_entry_t
* e
, const char value_str
[],
93 unsigned long long *value
)
96 unsigned long long out
;
99 int negative
, result
, found_one
;
101 if ((result
= prepare_cmos_op_common(e
)) != OK
)
105 case CMOS_ENTRY_ENUM
:
106 /* Make sure the user's input corresponds to a valid option. */
107 for (q
= first_cmos_enum_id(e
->config_id
), found_one
= 0;
108 q
!= NULL
; q
= next_cmos_enum_id(q
)) {
111 if (!strncmp(q
->text
, value_str
, CMOS_MAX_TEXT_LENGTH
))
116 return CMOS_OP_NO_MATCHING_ENUM
;
119 return CMOS_OP_BAD_ENUM_VALUE
;
125 /* See if the first character of 'value_str' (excluding
126 * any initial whitespace) is a minus sign.
128 for (p
= value_str
; isspace((int)(unsigned char)*p
); p
++) ;
129 negative
= (*p
== '-');
131 out
= strtoull(value_str
, (char **)&p
, 0);
134 return CMOS_OP_INVALID_INT
;
136 /* If we get this far, the user specified a valid integer.
137 * However we do not currently support the use of negative
138 * numbers as CMOS parameter values.
141 return CMOS_OP_NEGATIVE_INT
;
145 case CMOS_ENTRY_STRING
:
146 if (e
->length
< (8 * strlen(value_str
)))
147 return CMOS_OP_VALUE_TOO_WIDE
;
148 memory
= malloc(e
->length
/ 8);
149 memset(memory
, 0, e
->length
/ 8);
150 strcpy(memory
, value_str
);
151 out
= (unsigned long)memory
;
158 if ((e
->length
< (8 * sizeof(*value
))) && (out
>= (1ull << e
->length
))) {
159 if (memory
) free(memory
);
160 return CMOS_OP_VALUE_TOO_WIDE
;
167 /****************************************************************************
170 * Read the checksum for the coreboot parameters stored in CMOS and return
172 ****************************************************************************/
173 uint16_t cmos_checksum_read(void)
177 /* The checksum is stored in a big-endian format. */
178 hi
= cmos_read_byte(cmos_checksum_index
);
179 lo
= cmos_read_byte(cmos_checksum_index
+ 1);
180 return (hi
<< 8) + lo
;
183 /****************************************************************************
184 * cmos_checksum_write
186 * Set the checksum for the coreboot parameters stored in CMOS to
188 ****************************************************************************/
189 void cmos_checksum_write(uint16_t checksum
)
191 unsigned char lo
, hi
;
193 /* The checksum is stored in a big-endian format. */
194 hi
= (unsigned char)(checksum
>> 8);
195 lo
= (unsigned char)(checksum
& 0x00ff);
196 cmos_write_byte(cmos_checksum_index
, hi
);
197 cmos_write_byte(cmos_checksum_index
+ 1, lo
);
200 /****************************************************************************
201 * cmos_checksum_compute
203 * Compute a checksum for the coreboot parameter values currently stored in
204 * CMOS and return this checksum.
205 ****************************************************************************/
206 uint16_t cmos_checksum_compute(void)
212 for (i
= cmos_checksum_start
; i
<= cmos_checksum_end
; i
++)
213 sum
+= cmos_read_byte(i
);
215 return (uint16_t)(sum
& 0xffff);
218 /****************************************************************************
219 * cmos_checksum_verify
221 * Verify that the coreboot CMOS checksum is valid. If checksum is not
222 * valid then print warning message and exit.
223 ****************************************************************************/
224 void cmos_checksum_verify(void)
226 uint16_t computed
, actual
;
229 computed
= cmos_checksum_compute();
230 actual
= cmos_checksum_read();
233 if (computed
!= actual
) {
234 fprintf(stderr
, "%s: Warning: Coreboot CMOS checksum is bad.\n",