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>.
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 typedef struct cmos_entry_item_t cmos_entry_item_t;
37 struct cmos_entry_item_t {
39 cmos_entry_item_t *next;
42 typedef struct cmos_enum_item_t cmos_enum_item_t;
44 struct cmos_enum_item_t {
46 cmos_enum_item_t *next;
49 static void default_cmos_layout_get_fn(void);
50 static int areas_overlap(unsigned area_0_start, unsigned area_0_length,
51 unsigned area_1_start, unsigned area_1_length);
52 static int entries_overlap(const cmos_entry_t * p, const cmos_entry_t * q);
53 static const cmos_enum_item_t *find_first_cmos_enum_id(unsigned config_id);
55 const char checksum_param_name[] = "check_sum";
57 /* Newer versions of coreboot store the 3 pieces of information below in the
58 * coreboot table so we don't have to rely on hardcoded values.
61 /* This is the offset from the start of CMOS of the first byte that the
62 * checksum is calculated over.
64 #define CMOS_CHECKSUM_START 49
66 /* This is the offset from the start of CMOS of the last byte that the
67 * checksum is calculated over.
69 #define CMOS_CHECKSUM_END 125
71 /* This is the offset from the start of CMOS where the coreboot checksum is
74 #define CMOS_CHECKSUM_INDEX 126
76 /* index of first byte of checksummed area */
77 unsigned cmos_checksum_start = CMOS_CHECKSUM_START;
79 /* index of last byte of checksummed area */
80 unsigned cmos_checksum_end = CMOS_CHECKSUM_END;
82 /* index of first byte of CMOS checksum (a big-endian 16-bit value) */
83 unsigned cmos_checksum_index = CMOS_CHECKSUM_INDEX;
85 /* List is sorted in ascending order according to 'bit' field in
88 static cmos_entry_item_t *cmos_entry_list = NULL;
90 /* List is sorted in ascending order: first by 'config_id' and then by
93 static cmos_enum_item_t *cmos_enum_list = NULL;
95 static cmos_layout_get_fn_t cmos_layout_get_fn = default_cmos_layout_get_fn;
97 /****************************************************************************
100 * Return 1 if cmos entries 'p' and 'q' overlap. Else return 0.
101 ****************************************************************************/
102 static inline int entries_overlap(const cmos_entry_t * p,
103 const cmos_entry_t * q)
105 return areas_overlap(p->bit, p->length, q->bit, q->length);
108 /****************************************************************************
109 * cmos_entry_to_const_item
111 * Return a pointer to the cmos_entry_item_t that 'p' is embedded within.
112 ****************************************************************************/
113 static inline const cmos_entry_item_t *cmos_entry_to_const_item
114 (const cmos_entry_t * p) {
115 static const cmos_entry_t *pos = &((cmos_entry_item_t *) 0)->item;
116 unsigned long offset, address;
118 offset = (unsigned long)pos;
119 address = ((unsigned long)p) - offset;
120 return (const cmos_entry_item_t *)address;
123 /****************************************************************************
124 * cmos_enum_to_const_item
126 * Return a pointer to the cmos_enum_item_t that 'p' is embedded within.
127 ****************************************************************************/
128 static inline const cmos_enum_item_t *cmos_enum_to_const_item
129 (const cmos_enum_t * p) {
130 static const cmos_enum_t *pos = &((cmos_enum_item_t *) 0)->item;
131 unsigned long offset, address;
133 offset = (unsigned long)pos;
134 address = ((unsigned long)p) - offset;
135 return (const cmos_enum_item_t *)address;
138 /****************************************************************************
139 * register_cmos_layout_get_fn
141 * Set 'fn' as the function that will be called to retrieve CMOS layout
143 ****************************************************************************/
144 void register_cmos_layout_get_fn(cmos_layout_get_fn_t fn)
146 cmos_layout_get_fn = fn;
149 /****************************************************************************
152 * Retrieve CMOS layout information and store it in our internal repository.
153 ****************************************************************************/
154 void get_cmos_layout(void)
156 cmos_layout_get_fn();
159 /****************************************************************************
162 * Attempt to add CMOS entry 'e' to our internal repository of layout
163 * information. Return OK on success or an error code on failure. If
164 * operation fails because 'e' overlaps an existing CMOS entry, '*conflict'
165 * will be set to point to the overlapping entry.
166 ****************************************************************************/
167 int add_cmos_entry(const cmos_entry_t * e, const cmos_entry_t ** conflict)
169 cmos_entry_item_t *item, *prev, *new_entry;
174 return LAYOUT_ENTRY_BAD_LENGTH;
177 (cmos_entry_item_t *) malloc(sizeof(*new_entry))) == NULL)
180 new_entry->item = *e;
182 if (cmos_entry_list == NULL) {
183 new_entry->next = NULL;
184 cmos_entry_list = new_entry;
188 /* Find place in list to insert new entry. List is sorted in ascending
191 for (item = cmos_entry_list, prev = NULL;
192 (item != NULL) && (item->item.bit < e->bit);
193 prev = item, item = item->next) ;
196 if (entries_overlap(e, &cmos_entry_list->item)) {
197 *conflict = &cmos_entry_list->item;
201 new_entry->next = cmos_entry_list;
202 cmos_entry_list = new_entry;
206 if (entries_overlap(&prev->item, e)) {
207 *conflict = &prev->item;
211 if ((item != NULL) && entries_overlap(e, &item->item)) {
212 *conflict = &item->item;
216 new_entry->next = item;
217 prev->next = new_entry;
222 return LAYOUT_ENTRY_OVERLAP;
225 /****************************************************************************
228 * Search for a CMOS entry whose name is 'name'. Return pointer to matching
229 * entry or NULL if entry not found.
230 ****************************************************************************/
231 const cmos_entry_t *find_cmos_entry(const char name[])
233 cmos_entry_item_t *item;
235 for (item = cmos_entry_list; item != NULL; item = item->next) {
236 if (!strcmp(item->item.name, name))
243 /****************************************************************************
246 * Return a pointer to the first CMOS entry in our list or NULL if list is
248 ****************************************************************************/
249 const cmos_entry_t *first_cmos_entry(void)
251 return (cmos_entry_list == NULL) ? NULL : &cmos_entry_list->item;
254 /****************************************************************************
257 * Return a pointer to next entry in list after 'last' or NULL if no more
259 ****************************************************************************/
260 const cmos_entry_t *next_cmos_entry(const cmos_entry_t * last)
262 const cmos_entry_item_t *last_item, *next_item;
264 last_item = cmos_entry_to_const_item(last);
265 next_item = last_item->next;
266 return (next_item == NULL) ? NULL : &next_item->item;
269 /****************************************************************************
272 * Attempt to add CMOS enum 'e' to our internal repository of layout
273 * information. Return OK on success or an error code on failure.
274 ****************************************************************************/
275 int add_cmos_enum(const cmos_enum_t * e)
277 cmos_enum_item_t *item, *prev, *new_enum;
279 if ((new_enum = (cmos_enum_item_t *) malloc(sizeof(*new_enum))) == NULL)
284 if (cmos_enum_list == NULL) {
285 new_enum->next = NULL;
286 cmos_enum_list = new_enum;
290 /* The list of enums is sorted in ascending order, first by
291 * 'config_id' and then by 'value'. Look for the first enum
292 * whose 'config_id' field matches 'e'.
294 for (item = cmos_enum_list, prev = NULL;
295 (item != NULL) && (item->item.config_id < e->config_id);
296 prev = item, item = item->next) ;
299 new_enum->next = NULL;
300 prev->next = new_enum;
304 if (item->item.config_id > e->config_id) {
305 new_enum->next = item;
308 cmos_enum_list = new_enum;
310 prev->next = new_enum;
315 /* List already contains at least one enum whose 'config_id'
316 * matches 'e'. Now find proper place to insert 'e' based on
319 while (item->item.value < e->value) {
323 if ((item == NULL) || (item->item.config_id != e->config_id)) {
324 new_enum->next = item;
325 prev->next = new_enum;
330 if (item->item.value == e->value) {
332 return LAYOUT_DUPLICATE_ENUM;
335 new_enum->next = item;
338 cmos_enum_list = new_enum;
340 prev->next = new_enum;
345 /****************************************************************************
348 * Search for an enum that matches 'config_id' and 'value'. If found, return
349 * a pointer to the mathcing enum. Else return NULL.
350 ****************************************************************************/
351 const cmos_enum_t *find_cmos_enum(unsigned config_id, unsigned long long value)
353 const cmos_enum_item_t *item;
355 if ((item = find_first_cmos_enum_id(config_id)) == NULL)
358 while (item->item.value < value) {
361 if ((item == NULL) || (item->item.config_id != config_id))
365 return (item->item.value == value) ? &item->item : NULL;
368 /****************************************************************************
371 * Return a pointer to the first CMOS enum in our list or NULL if list is
373 ****************************************************************************/
374 const cmos_enum_t *first_cmos_enum(void)
376 return (cmos_enum_list == NULL) ? NULL : &cmos_enum_list->item;
379 /****************************************************************************
382 * Return a pointer to next enum in list after 'last' or NULL if no more
384 ****************************************************************************/
385 const cmos_enum_t *next_cmos_enum(const cmos_enum_t * last)
387 const cmos_enum_item_t *last_item, *next_item;
389 last_item = cmos_enum_to_const_item(last);
390 next_item = last_item->next;
391 return (next_item == NULL) ? NULL : &next_item->item;
394 /****************************************************************************
397 * Return a pointer to the first CMOS enum in our list that matches
398 * 'config_id' or NULL if there are no matching enums.
399 ****************************************************************************/
400 const cmos_enum_t *first_cmos_enum_id(unsigned config_id)
402 const cmos_enum_item_t *item;
404 item = find_first_cmos_enum_id(config_id);
405 return (item == NULL) ? NULL : &item->item;
408 /****************************************************************************
411 * Return a pointer to next enum in list after 'last' that matches the
412 * 'config_id' field of 'last' or NULL if there are no more matching enums.
413 ****************************************************************************/
414 const cmos_enum_t *next_cmos_enum_id(const cmos_enum_t * last)
416 const cmos_enum_item_t *item;
418 item = cmos_enum_to_const_item(last)->next;
419 return ((item == NULL) || (item->item.config_id != last->config_id)) ?
423 /****************************************************************************
426 * Return 1 if 'name' matches the name of the parameter representing the CMOS
427 * checksum. Else return 0.
428 ****************************************************************************/
429 int is_checksum_name(const char name[])
431 return !strcmp(name, checksum_param_name);
434 /****************************************************************************
435 * checksum_layout_to_bytes
437 * On entry, '*layout' contains checksum-related layout information expressed
438 * in bits. Perform sanity checking on the information and convert it from
439 * bit positions to byte positions. Return OK on success or an error code if
440 * a sanity check fails.
441 ****************************************************************************/
442 int checksum_layout_to_bytes(cmos_checksum_layout_t * layout)
444 unsigned start, end, index;
446 start = layout->summed_area_start;
447 end = layout->summed_area_end;
448 index = layout->checksum_at;
451 return LAYOUT_SUMMED_AREA_START_NOT_ALIGNED;
454 return LAYOUT_SUMMED_AREA_END_NOT_ALIGNED;
457 return LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED;
460 return LAYOUT_INVALID_SUMMED_AREA;
462 /* Convert bit positions to byte positions. */
464 end /= 8; /* equivalent to "end = ((end - 7) / 8)" */
467 if (verify_cmos_byte_index(start) || verify_cmos_byte_index(end))
468 return LAYOUT_SUMMED_AREA_OUT_OF_RANGE;
470 if (verify_cmos_byte_index(index))
471 return LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE;
473 /* checksum occupies 16 bits */
474 if (areas_overlap(start, end - start + 1, index, index + 1))
475 return LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA;
477 layout->summed_area_start = start;
478 layout->summed_area_end = end;
479 layout->checksum_at = index;
483 /****************************************************************************
484 * checksum_layout_to_bits
486 * On entry, '*layout' contains checksum-related layout information expressed
487 * in bytes. Convert this information to bit positions.
488 ****************************************************************************/
489 void checksum_layout_to_bits(cmos_checksum_layout_t * layout)
491 layout->summed_area_start *= 8;
492 layout->summed_area_end = (layout->summed_area_end * 8) + 7;
493 layout->checksum_at *= 8;
496 /****************************************************************************
497 * default_cmos_layout_get_fn
499 * If this function is ever called, it means that an appropriate callback for
500 * obtaining CMOS layout information was not set before attempting to
501 * retrieve layout information.
502 ****************************************************************************/
503 static void default_cmos_layout_get_fn(void)
508 /****************************************************************************
511 * Return 1 if the two given areas overlap. Else return 0.
512 ****************************************************************************/
513 static int areas_overlap(unsigned area_0_start, unsigned area_0_length,
514 unsigned area_1_start, unsigned area_1_length)
516 unsigned area_0_end, area_1_end;
518 area_0_end = area_0_start + area_0_length - 1;
519 area_1_end = area_1_start + area_1_length - 1;
520 return ((area_1_start <= area_0_end) && (area_0_start <= area_1_end));
523 /****************************************************************************
524 * find_first_cmos_enum_id
526 * Return a pointer to the first item in our list of enums that matches
527 * 'config_id'. Return NULL if there is no matching enum.
528 ****************************************************************************/
529 static const cmos_enum_item_t *find_first_cmos_enum_id(unsigned config_id)
531 cmos_enum_item_t *item;
533 for (item = cmos_enum_list;
534 (item != NULL) && (item->item.config_id < config_id);
537 return ((item == NULL) || (item->item.config_id > config_id)) ?