1 /*****************************************************************************\
4 *****************************************************************************
5 * Copyright (C) 2002-2005 The Regents of the University of California.
6 * Produced at the Lawrence Livermore National Laboratory.
7 * Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>.
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 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
30 \*****************************************************************************/
34 #include "cmos_lowlevel.h"
36 typedef struct cmos_entry_item_t cmos_entry_item_t;
38 struct cmos_entry_item_t
40 cmos_entry_item_t *next;
43 typedef struct cmos_enum_item_t cmos_enum_item_t;
45 struct cmos_enum_item_t
47 cmos_enum_item_t *next;
50 static void default_cmos_layout_get_fn (void);
51 static int areas_overlap (unsigned area_0_start, unsigned area_0_length,
52 unsigned area_1_start, unsigned area_1_length);
53 static int entries_overlap (const cmos_entry_t *p, const cmos_entry_t *q);
54 static const cmos_enum_item_t * find_first_cmos_enum_id (unsigned config_id);
56 const char checksum_param_name[] = "check_sum";
58 /* Newer versions of coreboot store the 3 pieces of information below in the
59 * coreboot table so we don't have to rely on hardcoded values.
62 /* This is the offset from the start of CMOS of the first byte that the
63 * checksum is calculated over.
65 #define CMOS_CHECKSUM_START 49
67 /* This is the offset from the start of CMOS of the last byte that the
68 * checksum is calculated over.
70 #define CMOS_CHECKSUM_END 125
72 /* This is the offset from the start of CMOS where the coreboot checksum is
75 #define CMOS_CHECKSUM_INDEX 126
77 /* index of first byte of checksummed area */
78 unsigned cmos_checksum_start = CMOS_CHECKSUM_START;
80 /* index of last byte of checksummed area */
81 unsigned cmos_checksum_end = CMOS_CHECKSUM_END;
83 /* index of first byte of CMOS checksum (a big-endian 16-bit value) */
84 unsigned cmos_checksum_index = CMOS_CHECKSUM_INDEX;
86 /* List is sorted in ascending order according to 'bit' field in
89 static cmos_entry_item_t *cmos_entry_list = NULL;
91 /* List is sorted in ascending order: first by 'config_id' and then by
94 static cmos_enum_item_t *cmos_enum_list = NULL;
96 static cmos_layout_get_fn_t cmos_layout_get_fn = default_cmos_layout_get_fn;
98 /****************************************************************************
101 * Return 1 if cmos entries 'p' and 'q' overlap. Else return 0.
102 ****************************************************************************/
103 static inline int entries_overlap (const cmos_entry_t *p,
104 const cmos_entry_t *q)
105 { return areas_overlap(p->bit, p->length, q->bit, q->length); }
107 /****************************************************************************
108 * cmos_entry_to_const_item
110 * Return a pointer to the cmos_entry_item_t that 'p' is embedded within.
111 ****************************************************************************/
112 static inline const cmos_entry_item_t * cmos_entry_to_const_item
113 (const cmos_entry_t *p)
114 { static const cmos_entry_t *pos = &((cmos_entry_item_t *) 0)->item;
115 unsigned long offset, address;
117 offset = (unsigned long) pos;
118 address = ((unsigned long) p) - offset;
119 return (const cmos_entry_item_t *) address;
122 /****************************************************************************
123 * cmos_enum_to_const_item
125 * Return a pointer to the cmos_enum_item_t that 'p' is embedded within.
126 ****************************************************************************/
127 static inline const cmos_enum_item_t * cmos_enum_to_const_item
128 (const cmos_enum_t *p)
129 { static const cmos_enum_t *pos = &((cmos_enum_item_t *) 0)->item;
130 unsigned long offset, address;
132 offset = (unsigned long) pos;
133 address = ((unsigned long) p) - offset;
134 return (const cmos_enum_item_t *) address;
137 /****************************************************************************
138 * register_cmos_layout_get_fn
140 * Set 'fn' as the function that will be called to retrieve CMOS layout
142 ****************************************************************************/
143 void register_cmos_layout_get_fn (cmos_layout_get_fn_t fn)
144 { cmos_layout_get_fn = fn; }
146 /****************************************************************************
149 * Retrieve CMOS layout information and store it in our internal repository.
150 ****************************************************************************/
151 void get_cmos_layout (void)
152 { cmos_layout_get_fn(); }
154 /****************************************************************************
157 * Attempt to add CMOS entry 'e' to our internal repository of layout
158 * information. Return OK on success or an error code on failure. If
159 * operation fails because 'e' overlaps an existing CMOS entry, '*conflict'
160 * will be set to point to the overlapping entry.
161 ****************************************************************************/
162 int add_cmos_entry (const cmos_entry_t *e, const cmos_entry_t **conflict)
163 { cmos_entry_item_t *item, *prev, *new_entry;
168 return LAYOUT_ENTRY_BAD_LENGTH;
170 if ((new_entry = (cmos_entry_item_t *) malloc(sizeof(*new_entry))) == NULL)
173 new_entry->item = *e;
175 if (cmos_entry_list == NULL)
176 { new_entry->next = NULL;
177 cmos_entry_list = new_entry;
181 /* Find place in list to insert new entry. List is sorted in ascending
184 for (item = cmos_entry_list, prev = NULL;
185 (item != NULL) && (item->item.bit < e->bit);
186 prev = item, item = item->next);
189 { if (entries_overlap(e, &cmos_entry_list->item))
190 { *conflict = &cmos_entry_list->item;
194 new_entry->next = cmos_entry_list;
195 cmos_entry_list = new_entry;
199 if (entries_overlap(&prev->item, e))
200 { *conflict = &prev->item;
204 if ((item != NULL) && entries_overlap(e, &item->item))
205 { *conflict = &item->item;
209 new_entry->next = item;
210 prev->next = new_entry;
215 return LAYOUT_ENTRY_OVERLAP;
218 /****************************************************************************
221 * Search for a CMOS entry whose name is 'name'. Return pointer to matching
222 * entry or NULL if entry not found.
223 ****************************************************************************/
224 const cmos_entry_t * find_cmos_entry (const char name[])
225 { cmos_entry_item_t *item;
227 for (item = cmos_entry_list; item != NULL; item = item->next)
228 { if (!strcmp(item->item.name, name))
235 /****************************************************************************
238 * Return a pointer to the first CMOS entry in our list or NULL if list is
240 ****************************************************************************/
241 const cmos_entry_t * first_cmos_entry (void)
242 { return (cmos_entry_list == NULL) ? NULL : &cmos_entry_list->item; }
244 /****************************************************************************
247 * Return a pointer to next entry in list after 'last' or NULL if no more
249 ****************************************************************************/
250 const cmos_entry_t * next_cmos_entry (const cmos_entry_t *last)
251 { const cmos_entry_item_t *last_item, *next_item;
253 last_item = cmos_entry_to_const_item(last);
254 next_item = last_item->next;
255 return (next_item == NULL) ? NULL : &next_item->item;
258 /****************************************************************************
261 * Attempt to add CMOS enum 'e' to our internal repository of layout
262 * information. Return OK on success or an error code on failure.
263 ****************************************************************************/
264 int add_cmos_enum (const cmos_enum_t *e)
265 { cmos_enum_item_t *item, *prev, *new_enum;
267 if ((new_enum = (cmos_enum_item_t *) malloc(sizeof(*new_enum))) == NULL)
272 if (cmos_enum_list == NULL)
273 { new_enum->next = NULL;
274 cmos_enum_list = new_enum;
278 /* The list of enums is sorted in ascending order, first by 'config_id' and
279 * then by 'value'. Look for the first enum whose 'config_id' field
282 for (item = cmos_enum_list, prev = NULL;
283 (item != NULL) && (item->item.config_id < e->config_id);
284 prev = item, item = item->next);
287 { new_enum->next = NULL;
288 prev->next = new_enum;
292 if (item->item.config_id > e->config_id)
293 { new_enum->next = item;
296 cmos_enum_list = new_enum;
298 prev->next = new_enum;
303 /* List already contains at least one enum whose 'config_id' matches 'e'.
304 * Now find proper place to insert 'e' based on 'value'.
306 while (item->item.value < e->value)
310 if ((item == NULL) || (item->item.config_id != e->config_id))
311 { new_enum->next = item;
312 prev->next = new_enum;
317 if (item->item.value == e->value)
319 return LAYOUT_DUPLICATE_ENUM;
322 new_enum->next = item;
325 cmos_enum_list = new_enum;
327 prev->next = new_enum;
332 /****************************************************************************
335 * Search for an enum that matches 'config_id' and 'value'. If found, return
336 * a pointer to the mathcing enum. Else return NULL.
337 ****************************************************************************/
338 const cmos_enum_t * find_cmos_enum (unsigned config_id,
339 unsigned long long value)
340 { const cmos_enum_item_t *item;
342 if ((item = find_first_cmos_enum_id(config_id)) == NULL)
345 while (item->item.value < value)
348 if ((item == NULL) || (item->item.config_id != config_id))
352 return (item->item.value == value) ? &item->item : NULL;
355 /****************************************************************************
358 * Return a pointer to the first CMOS enum in our list or NULL if list is
360 ****************************************************************************/
361 const cmos_enum_t * first_cmos_enum (void)
362 { return (cmos_enum_list == NULL) ? NULL : &cmos_enum_list->item; }
364 /****************************************************************************
367 * Return a pointer to next enum in list after 'last' or NULL if no more
369 ****************************************************************************/
370 const cmos_enum_t * next_cmos_enum (const cmos_enum_t *last)
371 { const cmos_enum_item_t *last_item, *next_item;
373 last_item = cmos_enum_to_const_item(last);
374 next_item = last_item->next;
375 return (next_item == NULL) ? NULL : &next_item->item;
378 /****************************************************************************
381 * Return a pointer to the first CMOS enum in our list that matches
382 * 'config_id' or NULL if there are no matching enums.
383 ****************************************************************************/
384 const cmos_enum_t * first_cmos_enum_id (unsigned config_id)
385 { const cmos_enum_item_t *item;
387 item = find_first_cmos_enum_id(config_id);
388 return (item == NULL) ? NULL : &item->item;
391 /****************************************************************************
394 * Return a pointer to next enum in list after 'last' that matches the
395 * 'config_id' field of 'last' or NULL if there are no more matching enums.
396 ****************************************************************************/
397 const cmos_enum_t * next_cmos_enum_id (const cmos_enum_t *last)
398 { const cmos_enum_item_t *item;
400 item = cmos_enum_to_const_item(last)->next;
401 return ((item == NULL) || (item->item.config_id != last->config_id)) ?
405 /****************************************************************************
408 * Return 1 if 'name' matches the name of the parameter representing the CMOS
409 * checksum. Else return 0.
410 ****************************************************************************/
411 int is_checksum_name (const char name[])
412 { return !strcmp(name, checksum_param_name); }
414 /****************************************************************************
415 * checksum_layout_to_bytes
417 * On entry, '*layout' contains checksum-related layout information expressed
418 * in bits. Perform sanity checking on the information and convert it from
419 * bit positions to byte positions. Return OK on success or an error code if
420 * a sanity check fails.
421 ****************************************************************************/
422 int checksum_layout_to_bytes (cmos_checksum_layout_t *layout)
423 { unsigned start, end, index;
425 start = layout->summed_area_start;
426 end = layout->summed_area_end;
427 index = layout->checksum_at;
430 return LAYOUT_SUMMED_AREA_START_NOT_ALIGNED;
433 return LAYOUT_SUMMED_AREA_END_NOT_ALIGNED;
436 return LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED;
439 return LAYOUT_INVALID_SUMMED_AREA;
441 /* Convert bit positions to byte positions. */
443 end /= 8; /* equivalent to "end = ((end - 7) / 8)" */
446 if (verify_cmos_byte_index(start) || verify_cmos_byte_index(end))
447 return LAYOUT_SUMMED_AREA_OUT_OF_RANGE;
449 if (verify_cmos_byte_index(index))
450 return LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE;
452 /* checksum occupies 16 bits */
453 if (areas_overlap(start, end - start + 1, index, index + 1))
454 return LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA;
456 layout->summed_area_start = start;
457 layout->summed_area_end = end;
458 layout->checksum_at = index;
462 /****************************************************************************
463 * checksum_layout_to_bits
465 * On entry, '*layout' contains checksum-related layout information expressed
466 * in bytes. Convert this information to bit positions.
467 ****************************************************************************/
468 void checksum_layout_to_bits (cmos_checksum_layout_t *layout)
469 { layout->summed_area_start *= 8;
470 layout->summed_area_end = (layout->summed_area_end * 8) + 7;
471 layout->checksum_at *= 8;
474 /****************************************************************************
475 * default_cmos_layout_get_fn
477 * If this function is ever called, it means that an appropriate callback for
478 * obtaining CMOS layout information was not set before attempting to
479 * retrieve layout information.
480 ****************************************************************************/
481 static void default_cmos_layout_get_fn (void)
484 /****************************************************************************
487 * Return 1 if the two given areas overlap. Else return 0.
488 ****************************************************************************/
489 static int areas_overlap (unsigned area_0_start, unsigned area_0_length,
490 unsigned area_1_start, unsigned area_1_length)
491 { unsigned area_0_end, area_1_end;
493 area_0_end = area_0_start + area_0_length - 1;
494 area_1_end = area_1_start + area_1_length - 1;
495 return ((area_1_start <= area_0_end) && (area_0_start <= area_1_end));
498 /****************************************************************************
499 * find_first_cmos_enum_id
501 * Return a pointer to the first item in our list of enums that matches
502 * 'config_id'. Return NULL if there is no matching enum.
503 ****************************************************************************/
504 static const cmos_enum_item_t * find_first_cmos_enum_id (unsigned config_id)
505 { cmos_enum_item_t *item;
507 for (item = cmos_enum_list;
508 (item != NULL) && (item->item.config_id < config_id);
511 return ((item == NULL) || (item->item.config_id > config_id)) ?