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 Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>
7 * and Stefan Reinauer <stepan@openbios.org>.
11 * This file is part of nvramtool, a utility for reading/writing coreboot
12 * parameters and displaying information from the coreboot table.
13 * For details, see http://coreboot.org/nvramtool.
15 * Please also read the file DISCLAIMER which is included in this software
18 * This program is free software; you can redistribute it and/or modify it
19 * under the terms of the GNU General Public License (as published by the
20 * Free Software Foundation) version 2, dated June 1991.
22 * This program is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the terms and
25 * conditions of the GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License along
28 * with this program; if not, write to the Free Software Foundation, Inc.,
29 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
30 \*****************************************************************************/
32 #include <arpa/inet.h>
36 #include "coreboot_tables.h"
37 #include "ip_checksum.h"
40 #include "cmos_lowlevel.h"
44 static void process_cmos_table(void);
45 static void get_cmos_checksum_info(void);
46 static void try_convert_checksum_layout(cmos_checksum_layout_t * layout);
47 static void try_add_cmos_table_enum(cmos_enum_t * cmos_enum);
48 static void try_add_cmos_table_entry(cmos_entry_t * cmos_entry);
49 static const struct cmos_entries *first_cmos_table_entry(void);
50 static const struct cmos_entries *next_cmos_table_entry(const struct
52 static const struct cmos_enums *first_cmos_table_enum(void);
53 static const struct cmos_enums *next_cmos_table_enum
54 (const struct cmos_enums *last);
55 static const struct lb_record *first_cmos_rec(uint32_t tag);
56 static const struct lb_record *next_cmos_rec(const struct lb_record *last,
59 /* The CMOS option table is located within the coreboot table. It tells us
60 * where the CMOS parameters are located in the nonvolatile RAM.
62 static const struct cmos_option_table *cmos_table = NULL;
64 void process_layout(void)
66 if ((cmos_table) == NULL) {
68 "%s: CMOS option table not found in coreboot table. "
69 "Apparently, the coreboot installed on this system was "
70 "built without specifying CONFIG_HAVE_OPTION_TABLE.\n",
76 get_cmos_checksum_info();
79 void get_layout_from_cbfs_file(void)
82 cmos_table = cbfs_find_file("cmos_layout.bin", CBFS_COMPONENT_CMOS_LAYOUT, &len);
86 /****************************************************************************
87 * get_layout_from_cmos_table
89 * Find the CMOS table which is stored within the coreboot table and set the
90 * global variable cmos_table to point to it.
91 ****************************************************************************/
92 void get_layout_from_cmos_table(void)
95 cmos_table = (const struct cmos_option_table *)
96 find_lbrec(LB_TAG_CMOS_OPTION_TABLE);
100 /****************************************************************************
103 * Extract layout information from the CMOS option table and store it in our
104 * internal repository.
105 ****************************************************************************/
106 static void process_cmos_table(void)
108 const struct cmos_enums *p;
109 const struct cmos_entries *q;
110 cmos_enum_t cmos_enum;
111 cmos_entry_t cmos_entry;
113 /* First add the enums. */
114 for (p = first_cmos_table_enum(); p != NULL;
115 p = next_cmos_table_enum(p)) {
116 cmos_enum.config_id = p->config_id;
117 cmos_enum.value = p->value;
118 strncpy(cmos_enum.text, (char *)p->text, CMOS_MAX_TEXT_LENGTH);
119 cmos_enum.text[CMOS_MAX_TEXT_LENGTH] = '\0';
120 try_add_cmos_table_enum(&cmos_enum);
123 /* Now add the entries. We must add the entries after the enums because
124 * the entries are sanity checked against the enums as they are added.
126 for (q = first_cmos_table_entry(); q != NULL;
127 q = next_cmos_table_entry(q)) {
128 cmos_entry.bit = q->bit;
129 cmos_entry.length = q->length;
133 cmos_entry.config = CMOS_ENTRY_ENUM;
137 cmos_entry.config = CMOS_ENTRY_HEX;
141 cmos_entry.config = CMOS_ENTRY_RESERVED;
145 cmos_entry.config = CMOS_ENTRY_STRING;
150 "%s: Entry in CMOS option table has unknown config "
151 "value.\n", prog_name);
155 cmos_entry.config_id = q->config_id;
156 strncpy(cmos_entry.name, (char *)q->name, CMOS_MAX_NAME_LENGTH);
157 cmos_entry.name[CMOS_MAX_NAME_LENGTH] = '\0';
158 try_add_cmos_table_entry(&cmos_entry);
162 /****************************************************************************
163 * get_cmos_checksum_info
165 * Get layout information for CMOS checksum.
166 ****************************************************************************/
167 static void get_cmos_checksum_info(void)
169 const cmos_entry_t *e;
170 struct cmos_checksum *checksum;
171 cmos_checksum_layout_t layout;
172 unsigned index, index2;
174 checksum = (struct cmos_checksum *)next_cmos_rec((const struct lb_record *)first_cmos_table_enum(), LB_TAG_OPTION_CHECKSUM);
176 if (checksum != NULL) { /* We are lucky. The coreboot table hints us to the checksum.
177 * We might have to check the type field here though.
179 layout.summed_area_start = checksum->range_start;
180 layout.summed_area_end = checksum->range_end;
181 layout.checksum_at = checksum->location;
182 try_convert_checksum_layout(&layout);
183 cmos_checksum_start = layout.summed_area_start;
184 cmos_checksum_end = layout.summed_area_end;
185 cmos_checksum_index = layout.checksum_at;
189 if ((e = find_cmos_entry(checksum_param_name)) == NULL)
192 /* If we get here, we are unlucky. The CMOS option table contains the
193 * location of the CMOS checksum. However, there is no information
194 * regarding which bytes of the CMOS area the checksum is computed over.
195 * Thus we have to hope our presets will be fine.
200 "%s: Error: CMOS checksum is not byte-aligned.\n",
206 index2 = index + 1; /* The CMOS checksum occupies 16 bits. */
208 if (verify_cmos_byte_index(index) || verify_cmos_byte_index(index2)) {
210 "%s: Error: CMOS checksum location out of range.\n",
215 if (((index >= cmos_checksum_start) && (index <= cmos_checksum_end)) ||
216 (((index2) >= cmos_checksum_start)
217 && ((index2) <= cmos_checksum_end))) {
219 "%s: Error: CMOS checksum overlaps checksummed area.\n",
224 cmos_checksum_index = index;
227 /****************************************************************************
228 * try_convert_checksum_layout
230 * Perform sanity checking on CMOS checksum layout information and attempt to
231 * convert information from bit positions to byte positions. Return OK on
232 * success or an error code on failure.
233 ****************************************************************************/
234 static void try_convert_checksum_layout(cmos_checksum_layout_t * layout)
236 switch (checksum_layout_to_bytes(layout)) {
240 case LAYOUT_SUMMED_AREA_START_NOT_ALIGNED:
242 "%s: CMOS checksummed area start is not byte-aligned.\n",
246 case LAYOUT_SUMMED_AREA_END_NOT_ALIGNED:
248 "%s: CMOS checksummed area end is not byte-aligned.\n",
252 case LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED:
254 "%s: CMOS checksum location is not byte-aligned.\n",
258 case LAYOUT_INVALID_SUMMED_AREA:
260 "%s: CMOS checksummed area end must be greater than "
261 "CMOS checksummed area start.\n", prog_name);
264 case LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA:
266 "%s: CMOS checksum overlaps checksummed area.\n",
270 case LAYOUT_SUMMED_AREA_OUT_OF_RANGE:
272 "%s: CMOS checksummed area out of range.\n", prog_name);
275 case LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE:
277 "%s: CMOS checksum location out of range.\n",
288 /****************************************************************************
289 * try_add_cmos_table_enum
291 * Attempt to add a CMOS enum to our internal repository. Exit with an error
292 * message on failure.
293 ****************************************************************************/
294 static void try_add_cmos_table_enum(cmos_enum_t * cmos_enum)
296 switch (add_cmos_enum(cmos_enum)) {
300 case LAYOUT_DUPLICATE_ENUM:
301 fprintf(stderr, "%s: Duplicate enum %s found in CMOS option "
302 "table.\n", prog_name, cmos_enum->text);
312 /****************************************************************************
313 * try_add_cmos_table_entry
315 * Attempt to add a CMOS entry to our internal repository. Exit with an
316 * error message on failure.
317 ****************************************************************************/
318 static void try_add_cmos_table_entry(cmos_entry_t * cmos_entry)
320 const cmos_entry_t *conflict;
322 switch (add_cmos_entry(cmos_entry, &conflict)) {
326 case CMOS_AREA_OUT_OF_RANGE:
328 "%s: Bad CMOS option layout in CMOS option table entry "
329 "%s.\n", prog_name, cmos_entry->name);
332 case CMOS_AREA_TOO_WIDE:
334 "%s: Area too wide for CMOS option table entry %s.\n",
335 prog_name, cmos_entry->name);
338 case LAYOUT_ENTRY_OVERLAP:
340 "%s: CMOS option table entries %s and %s have overlapping "
341 "layouts.\n", prog_name, cmos_entry->name,
345 case LAYOUT_ENTRY_BAD_LENGTH:
346 /* Silently ignore entries with zero length. Although this should
347 * never happen in practice, we should handle the case in a
348 * reasonable manner just to be safe.
359 /****************************************************************************
360 * first_cmos_table_entry
362 * Return a pointer to the first entry in the CMOS table that represents a
363 * CMOS parameter. Return NULL if CMOS table is empty.
364 ****************************************************************************/
365 static const struct cmos_entries *first_cmos_table_entry(void)
367 return (const struct cmos_entries *)first_cmos_rec(LB_TAG_OPTION);
370 /****************************************************************************
371 * next_cmos_table_entry
373 * Return a pointer to the next entry after 'last' in the CMOS table that
374 * represents a CMOS parameter. Return NULL if there are no more parameters.
375 ****************************************************************************/
376 static const struct cmos_entries *next_cmos_table_entry(const struct
379 return (const struct cmos_entries *)
380 next_cmos_rec((const struct lb_record *)last, LB_TAG_OPTION);
383 /****************************************************************************
384 * first_cmos_table_enum
386 * Return a pointer to the first entry in the CMOS table that represents a
387 * possible CMOS parameter value. Return NULL if the table does not contain
389 ****************************************************************************/
390 static const struct cmos_enums *first_cmos_table_enum(void)
392 return (const struct cmos_enums *)first_cmos_rec(LB_TAG_OPTION_ENUM);
395 /****************************************************************************
396 * next_cmos_table_enum
398 * Return a pointer to the next entry after 'last' in the CMOS table that
399 * represents a possible CMOS parameter value. Return NULL if there are no
400 * more parameter values.
401 ****************************************************************************/
402 static const struct cmos_enums *next_cmos_table_enum
403 (const struct cmos_enums *last) {
404 return (const struct cmos_enums *)
405 next_cmos_rec((const struct lb_record *)last, LB_TAG_OPTION_ENUM);
408 /****************************************************************************
411 * Return a pointer to the first entry in the CMOS table whose type matches
412 * 'tag'. Return NULL if CMOS table contains no such entry.
414 * Possible values for 'tag' are as follows:
416 * LB_TAG_OPTION: The entry represents a CMOS parameter.
417 * LB_TAG_OPTION_ENUM: The entry represents a possible value for a CMOS
418 * parameter of type 'enum'.
420 * The CMOS table tells us where in the nonvolatile RAM to look for CMOS
421 * parameter values and specifies their types as 'enum', 'hex', or
423 ****************************************************************************/
424 static const struct lb_record *first_cmos_rec(uint32_t tag)
427 uint32_t bytes_processed, bytes_for_entries;
428 const struct lb_record *lbrec;
430 p = ((const char *)cmos_table) + cmos_table->header_length;
431 bytes_for_entries = cmos_table->size - cmos_table->header_length;
433 for (bytes_processed = 0;
434 bytes_processed < bytes_for_entries;
435 bytes_processed += lbrec->size) {
436 lbrec = (const struct lb_record *)&p[bytes_processed];
438 if (lbrec->tag == tag)
445 /****************************************************************************
448 * Return a pointer to the next entry after 'last' in the CMOS table whose
449 * type matches 'tag'. Return NULL if the table contains no more entries of
451 ****************************************************************************/
452 static const struct lb_record *next_cmos_rec(const struct lb_record *last,
456 uint32_t bytes_processed, bytes_for_entries, last_offset;
457 const struct lb_record *lbrec;
459 p = ((const char *)cmos_table) + cmos_table->header_length;
460 bytes_for_entries = cmos_table->size - cmos_table->header_length;
461 last_offset = ((const char *)last) - p;
463 for (bytes_processed = last_offset + last->size;
464 bytes_processed < bytes_for_entries;
465 bytes_processed += lbrec->size) {
466 lbrec = (const struct lb_record *)&p[bytes_processed];
468 if (lbrec->tag == tag)