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 https://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.
25 \*****************************************************************************/
29 #include "cmos_lowlevel.h"
31 typedef struct cmos_entry_item_t cmos_entry_item_t
;
33 struct cmos_entry_item_t
{
35 cmos_entry_item_t
*next
;
38 typedef struct cmos_enum_item_t cmos_enum_item_t
;
40 struct cmos_enum_item_t
{
42 cmos_enum_item_t
*next
;
45 static void default_cmos_layout_get_fn(void);
46 static int areas_overlap(unsigned area_0_start
, unsigned area_0_length
,
47 unsigned area_1_start
, unsigned area_1_length
);
48 static int entries_overlap(const cmos_entry_t
* p
, const cmos_entry_t
* q
);
49 static const cmos_enum_item_t
*find_first_cmos_enum_id(unsigned config_id
);
51 const char checksum_param_name
[] = "check_sum";
53 /* Newer versions of coreboot store the 3 pieces of information below in the
54 * coreboot table so we don't have to rely on hardcoded values.
57 /* This is the offset from the start of CMOS of the first byte that the
58 * checksum is calculated over.
60 #define CMOS_CHECKSUM_START 49
62 /* This is the offset from the start of CMOS of the last byte that the
63 * checksum is calculated over.
65 #define CMOS_CHECKSUM_END 125
67 /* This is the offset from the start of CMOS where the coreboot checksum is
70 #define CMOS_CHECKSUM_INDEX 126
72 /* index of first byte of checksummed area */
73 unsigned cmos_checksum_start
= CMOS_CHECKSUM_START
;
75 /* index of last byte of checksummed area */
76 unsigned cmos_checksum_end
= CMOS_CHECKSUM_END
;
78 /* index of first byte of CMOS checksum (a big-endian 16-bit value) */
79 unsigned cmos_checksum_index
= CMOS_CHECKSUM_INDEX
;
81 /* List is sorted in ascending order according to 'bit' field in
84 static cmos_entry_item_t
*cmos_entry_list
= NULL
;
86 /* List is sorted in ascending order: first by 'config_id' and then by
89 static cmos_enum_item_t
*cmos_enum_list
= NULL
;
91 static cmos_layout_get_fn_t cmos_layout_get_fn
= default_cmos_layout_get_fn
;
93 /****************************************************************************
96 * Return 1 if cmos entries 'p' and 'q' overlap. Else return 0.
97 ****************************************************************************/
98 static inline int entries_overlap(const cmos_entry_t
* p
,
99 const cmos_entry_t
* q
)
101 return areas_overlap(p
->bit
, p
->length
, q
->bit
, q
->length
);
104 /****************************************************************************
105 * cmos_entry_to_const_item
107 * Return a pointer to the cmos_entry_item_t that 'p' is embedded within.
108 ****************************************************************************/
109 static inline const cmos_entry_item_t
*cmos_entry_to_const_item
110 (const cmos_entry_t
* p
) {
111 static const cmos_entry_t
*pos
= &((cmos_entry_item_t
*) 0)->item
;
112 unsigned long offset
, address
;
114 offset
= (unsigned long)pos
;
115 address
= ((unsigned long)p
) - offset
;
116 return (const cmos_entry_item_t
*)address
;
119 /****************************************************************************
120 * cmos_enum_to_const_item
122 * Return a pointer to the cmos_enum_item_t that 'p' is embedded within.
123 ****************************************************************************/
124 static inline const cmos_enum_item_t
*cmos_enum_to_const_item
125 (const cmos_enum_t
* p
) {
126 static const cmos_enum_t
*pos
= &((cmos_enum_item_t
*) 0)->item
;
127 unsigned long offset
, address
;
129 offset
= (unsigned long)pos
;
130 address
= ((unsigned long)p
) - offset
;
131 return (const cmos_enum_item_t
*)address
;
134 /****************************************************************************
135 * register_cmos_layout_get_fn
137 * Set 'fn' as the function that will be called to retrieve CMOS layout
139 ****************************************************************************/
140 void register_cmos_layout_get_fn(cmos_layout_get_fn_t fn
)
142 cmos_layout_get_fn
= fn
;
145 /****************************************************************************
148 * Retrieve CMOS layout information and store it in our internal repository.
149 ****************************************************************************/
150 void get_cmos_layout(void)
152 cmos_layout_get_fn();
155 /****************************************************************************
158 * Attempt to add CMOS entry 'e' to our internal repository of layout
159 * information. Return OK on success or an error code on failure. If
160 * operation fails because 'e' overlaps an existing CMOS entry, '*conflict'
161 * will be set to point to the overlapping entry.
162 ****************************************************************************/
163 int add_cmos_entry(const cmos_entry_t
* e
, const cmos_entry_t
** conflict
)
165 cmos_entry_item_t
*item
, *prev
, *new_entry
;
170 return LAYOUT_ENTRY_BAD_LENGTH
;
172 if (e
->bit
% 8 && e
->bit
/ 8 != (e
->bit
+ e
->length
- 1) / 8)
173 return LAYOUT_MULTIBYTE_ENTRY_NOT_ALIGNED
;
176 (cmos_entry_item_t
*) malloc(sizeof(*new_entry
))) == NULL
)
179 new_entry
->item
= *e
;
181 if (cmos_entry_list
== NULL
) {
182 new_entry
->next
= NULL
;
183 cmos_entry_list
= new_entry
;
187 /* Find place in list to insert new entry. List is sorted in ascending
190 for (item
= cmos_entry_list
, prev
= NULL
;
191 (item
!= NULL
) && (item
->item
.bit
< e
->bit
);
192 prev
= item
, item
= item
->next
) ;
195 if (entries_overlap(e
, &cmos_entry_list
->item
)) {
196 *conflict
= &cmos_entry_list
->item
;
200 new_entry
->next
= cmos_entry_list
;
201 cmos_entry_list
= new_entry
;
205 if (entries_overlap(&prev
->item
, e
)) {
206 *conflict
= &prev
->item
;
210 if ((item
!= NULL
) && entries_overlap(e
, &item
->item
)) {
211 *conflict
= &item
->item
;
215 new_entry
->next
= item
;
216 prev
->next
= new_entry
;
221 return LAYOUT_ENTRY_OVERLAP
;
224 /****************************************************************************
227 * Search for a CMOS entry whose name is 'name'. Return pointer to matching
228 * entry or NULL if entry not found.
229 ****************************************************************************/
230 const cmos_entry_t
*find_cmos_entry(const char name
[])
232 cmos_entry_item_t
*item
;
234 for (item
= cmos_entry_list
; item
!= NULL
; item
= item
->next
) {
235 if (!strcmp(item
->item
.name
, name
))
242 /****************************************************************************
245 * Return a pointer to the first CMOS entry in our list or NULL if list is
247 ****************************************************************************/
248 const cmos_entry_t
*first_cmos_entry(void)
250 return (cmos_entry_list
== NULL
) ? NULL
: &cmos_entry_list
->item
;
253 /****************************************************************************
256 * Return a pointer to next entry in list after 'last' or NULL if no more
258 ****************************************************************************/
259 const cmos_entry_t
*next_cmos_entry(const cmos_entry_t
* last
)
261 const cmos_entry_item_t
*last_item
, *next_item
;
263 last_item
= cmos_entry_to_const_item(last
);
264 next_item
= last_item
->next
;
265 return (next_item
== NULL
) ? NULL
: &next_item
->item
;
268 /****************************************************************************
271 * Attempt to add CMOS enum 'e' to our internal repository of layout
272 * information. Return OK on success or an error code on failure.
273 ****************************************************************************/
274 int add_cmos_enum(const cmos_enum_t
* e
)
276 cmos_enum_item_t
*item
, *prev
, *new_enum
;
278 if ((new_enum
= (cmos_enum_item_t
*) malloc(sizeof(*new_enum
))) == NULL
)
283 if (cmos_enum_list
== NULL
) {
284 new_enum
->next
= NULL
;
285 cmos_enum_list
= new_enum
;
289 /* The list of enums is sorted in ascending order, first by
290 * 'config_id' and then by 'value'. Look for the first enum
291 * whose 'config_id' field matches 'e'.
293 for (item
= cmos_enum_list
, prev
= NULL
;
294 (item
!= NULL
) && (item
->item
.config_id
< e
->config_id
);
295 prev
= item
, item
= item
->next
) ;
298 new_enum
->next
= NULL
;
299 prev
->next
= new_enum
;
303 if (item
->item
.config_id
> e
->config_id
) {
304 new_enum
->next
= item
;
307 cmos_enum_list
= new_enum
;
309 prev
->next
= new_enum
;
314 /* List already contains at least one enum whose 'config_id'
315 * matches 'e'. Now find proper place to insert 'e' based on
318 while (item
->item
.value
< e
->value
) {
322 if ((item
== NULL
) || (item
->item
.config_id
!= e
->config_id
)) {
323 new_enum
->next
= item
;
324 prev
->next
= new_enum
;
329 if (item
->item
.value
== e
->value
) {
331 return LAYOUT_DUPLICATE_ENUM
;
334 new_enum
->next
= item
;
337 cmos_enum_list
= new_enum
;
339 prev
->next
= new_enum
;
344 /****************************************************************************
347 * Search for an enum that matches 'config_id' and 'value'. If found, return
348 * a pointer to the mathcing enum. Else return NULL.
349 ****************************************************************************/
350 const cmos_enum_t
*find_cmos_enum(unsigned config_id
, unsigned long long value
)
352 const cmos_enum_item_t
*item
;
354 if ((item
= find_first_cmos_enum_id(config_id
)) == NULL
)
357 while (item
->item
.value
< value
) {
360 if ((item
== NULL
) || (item
->item
.config_id
!= config_id
))
364 return (item
->item
.value
== value
) ? &item
->item
: NULL
;
367 /****************************************************************************
370 * Return a pointer to the first CMOS enum in our list or NULL if list is
372 ****************************************************************************/
373 const cmos_enum_t
*first_cmos_enum(void)
375 return (cmos_enum_list
== NULL
) ? NULL
: &cmos_enum_list
->item
;
378 /****************************************************************************
381 * Return a pointer to next enum in list after 'last' or NULL if no more
383 ****************************************************************************/
384 const cmos_enum_t
*next_cmos_enum(const cmos_enum_t
* last
)
386 const cmos_enum_item_t
*last_item
, *next_item
;
388 last_item
= cmos_enum_to_const_item(last
);
389 next_item
= last_item
->next
;
390 return (next_item
== NULL
) ? NULL
: &next_item
->item
;
393 /****************************************************************************
396 * Return a pointer to the first CMOS enum in our list that matches
397 * 'config_id' or NULL if there are no matching enums.
398 ****************************************************************************/
399 const cmos_enum_t
*first_cmos_enum_id(unsigned config_id
)
401 const cmos_enum_item_t
*item
;
403 item
= find_first_cmos_enum_id(config_id
);
404 return (item
== NULL
) ? NULL
: &item
->item
;
407 /****************************************************************************
410 * Return a pointer to next enum in list after 'last' that matches the
411 * 'config_id' field of 'last' or NULL if there are no more matching enums.
412 ****************************************************************************/
413 const cmos_enum_t
*next_cmos_enum_id(const cmos_enum_t
* last
)
415 const cmos_enum_item_t
*item
;
417 item
= cmos_enum_to_const_item(last
)->next
;
418 return ((item
== NULL
) || (item
->item
.config_id
!= last
->config_id
)) ?
422 /****************************************************************************
425 * Return 1 if 'name' matches the name of the parameter representing the CMOS
426 * checksum. Else return 0.
427 ****************************************************************************/
428 int is_checksum_name(const char name
[])
430 return !strcmp(name
, checksum_param_name
);
433 /****************************************************************************
434 * checksum_layout_to_bytes
436 * On entry, '*layout' contains checksum-related layout information expressed
437 * in bits. Perform sanity checking on the information and convert it from
438 * bit positions to byte positions. Return OK on success or an error code if
439 * a sanity check fails.
440 ****************************************************************************/
441 int checksum_layout_to_bytes(cmos_checksum_layout_t
* layout
)
443 unsigned start
, end
, index
;
445 start
= layout
->summed_area_start
;
446 end
= layout
->summed_area_end
;
447 index
= layout
->checksum_at
;
450 return LAYOUT_SUMMED_AREA_START_NOT_ALIGNED
;
453 return LAYOUT_SUMMED_AREA_END_NOT_ALIGNED
;
456 return LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED
;
459 return LAYOUT_INVALID_SUMMED_AREA
;
461 /* Convert bit positions to byte positions. */
463 end
/= 8; /* equivalent to "end = ((end - 7) / 8)" */
466 if (verify_cmos_byte_index(start
) || verify_cmos_byte_index(end
))
467 return LAYOUT_SUMMED_AREA_OUT_OF_RANGE
;
469 if (verify_cmos_byte_index(index
))
470 return LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE
;
472 /* checksum occupies 16 bits */
473 if (areas_overlap(start
, end
- start
+ 1, index
, index
+ 1))
474 return LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA
;
476 layout
->summed_area_start
= start
;
477 layout
->summed_area_end
= end
;
478 layout
->checksum_at
= index
;
482 /****************************************************************************
483 * checksum_layout_to_bits
485 * On entry, '*layout' contains checksum-related layout information expressed
486 * in bytes. Convert this information to bit positions.
487 ****************************************************************************/
488 void checksum_layout_to_bits(cmos_checksum_layout_t
* layout
)
490 layout
->summed_area_start
*= 8;
491 layout
->summed_area_end
= (layout
->summed_area_end
* 8) + 7;
492 layout
->checksum_at
*= 8;
495 /****************************************************************************
496 * default_cmos_layout_get_fn
498 * If this function is ever called, it means that an appropriate callback for
499 * obtaining CMOS layout information was not set before attempting to
500 * retrieve layout information.
501 ****************************************************************************/
502 static void default_cmos_layout_get_fn(void)
507 /****************************************************************************
510 * Return 1 if the two given areas overlap. Else return 0.
511 ****************************************************************************/
512 static int areas_overlap(unsigned area_0_start
, unsigned area_0_length
,
513 unsigned area_1_start
, unsigned area_1_length
)
515 unsigned area_0_end
, area_1_end
;
517 area_0_end
= area_0_start
+ area_0_length
- 1;
518 area_1_end
= area_1_start
+ area_1_length
- 1;
519 return ((area_1_start
<= area_0_end
) && (area_0_start
<= area_1_end
));
522 /****************************************************************************
523 * find_first_cmos_enum_id
525 * Return a pointer to the first item in our list of enums that matches
526 * 'config_id'. Return NULL if there is no matching enum.
527 ****************************************************************************/
528 static const cmos_enum_item_t
*find_first_cmos_enum_id(unsigned config_id
)
530 cmos_enum_item_t
*item
;
532 for (item
= cmos_enum_list
;
533 (item
!= NULL
) && (item
->item
.config_id
< config_id
);
536 return ((item
== NULL
) || (item
->item
.config_id
> config_id
)) ?