mb: Set coreboot as DSDT's manufacturer model ID
[coreboot.git] / util / nvramtool / layout.c
bloba340671caa412fbe23ed139e363f892efc2f4cf1
1 /*****************************************************************************\
2 * layout.c
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 * UCRL-CODE-2003-012
8 * All rights reserved.
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
15 * distribution.
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 \*****************************************************************************/
27 #include "common.h"
28 #include "layout.h"
29 #include "cmos_lowlevel.h"
31 typedef struct cmos_entry_item_t cmos_entry_item_t;
33 struct cmos_entry_item_t {
34 cmos_entry_t item;
35 cmos_entry_item_t *next;
38 typedef struct cmos_enum_item_t cmos_enum_item_t;
40 struct cmos_enum_item_t {
41 cmos_enum_t item;
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
68 * stored.
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
82 * cmos_entry_t.
84 static cmos_entry_item_t *cmos_entry_list = NULL;
86 /* List is sorted in ascending order: first by 'config_id' and then by
87 * 'value'.
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 /****************************************************************************
94 * entries_overlap
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
138 * information.
139 ****************************************************************************/
140 void register_cmos_layout_get_fn(cmos_layout_get_fn_t fn)
142 cmos_layout_get_fn = fn;
145 /****************************************************************************
146 * get_cmos_layout
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 /****************************************************************************
156 * add_cmos_entry
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;
167 *conflict = NULL;
169 if (e->length < 1)
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;
175 if ((new_entry =
176 (cmos_entry_item_t *) malloc(sizeof(*new_entry))) == NULL)
177 out_of_memory();
179 new_entry->item = *e;
181 if (cmos_entry_list == NULL) {
182 new_entry->next = NULL;
183 cmos_entry_list = new_entry;
184 return OK;
187 /* Find place in list to insert new entry. List is sorted in ascending
188 * order.
190 for (item = cmos_entry_list, prev = NULL;
191 (item != NULL) && (item->item.bit < e->bit);
192 prev = item, item = item->next) ;
194 if (prev == NULL) {
195 if (entries_overlap(e, &cmos_entry_list->item)) {
196 *conflict = &cmos_entry_list->item;
197 goto fail;
200 new_entry->next = cmos_entry_list;
201 cmos_entry_list = new_entry;
202 return OK;
205 if (entries_overlap(&prev->item, e)) {
206 *conflict = &prev->item;
207 goto fail;
210 if ((item != NULL) && entries_overlap(e, &item->item)) {
211 *conflict = &item->item;
212 goto fail;
215 new_entry->next = item;
216 prev->next = new_entry;
217 return OK;
219 fail:
220 free(new_entry);
221 return LAYOUT_ENTRY_OVERLAP;
224 /****************************************************************************
225 * find_cmos_entry
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))
236 return &item->item;
239 return NULL;
242 /****************************************************************************
243 * first_cmos_entry
245 * Return a pointer to the first CMOS entry in our list or NULL if list is
246 * empty.
247 ****************************************************************************/
248 const cmos_entry_t *first_cmos_entry(void)
250 return (cmos_entry_list == NULL) ? NULL : &cmos_entry_list->item;
253 /****************************************************************************
254 * next_cmos_entry
256 * Return a pointer to next entry in list after 'last' or NULL if no more
257 * entries.
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 /****************************************************************************
269 * add_cmos_enum
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)
279 out_of_memory();
281 new_enum->item = *e;
283 if (cmos_enum_list == NULL) {
284 new_enum->next = NULL;
285 cmos_enum_list = new_enum;
286 return OK;
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) ;
297 if (item == NULL) {
298 new_enum->next = NULL;
299 prev->next = new_enum;
300 return OK;
303 if (item->item.config_id > e->config_id) {
304 new_enum->next = item;
306 if (prev == NULL)
307 cmos_enum_list = new_enum;
308 else
309 prev->next = new_enum;
311 return OK;
314 /* List already contains at least one enum whose 'config_id'
315 * matches 'e'. Now find proper place to insert 'e' based on
316 * 'value'.
318 while (item->item.value < e->value) {
319 prev = item;
320 item = item->next;
322 if ((item == NULL) || (item->item.config_id != e->config_id)) {
323 new_enum->next = item;
324 prev->next = new_enum;
325 return OK;
329 if (item->item.value == e->value) {
330 free(new_enum);
331 return LAYOUT_DUPLICATE_ENUM;
334 new_enum->next = item;
336 if (prev == NULL)
337 cmos_enum_list = new_enum;
338 else
339 prev->next = new_enum;
341 return OK;
344 /****************************************************************************
345 * find_cmos_enum
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)
355 return NULL;
357 while (item->item.value < value) {
358 item = item->next;
360 if ((item == NULL) || (item->item.config_id != config_id))
361 return NULL;
364 return (item->item.value == value) ? &item->item : NULL;
367 /****************************************************************************
368 * first_cmos_enum
370 * Return a pointer to the first CMOS enum in our list or NULL if list is
371 * empty.
372 ****************************************************************************/
373 const cmos_enum_t *first_cmos_enum(void)
375 return (cmos_enum_list == NULL) ? NULL : &cmos_enum_list->item;
378 /****************************************************************************
379 * next_cmos_enum
381 * Return a pointer to next enum in list after 'last' or NULL if no more
382 * enums.
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 /****************************************************************************
394 * first_cmos_enum_id
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 /****************************************************************************
408 * next_cmos_enum_id
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)) ?
419 NULL : &item->item;
422 /****************************************************************************
423 * is_checksum_name
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;
449 if (start % 8)
450 return LAYOUT_SUMMED_AREA_START_NOT_ALIGNED;
452 if ((end % 8) != 7)
453 return LAYOUT_SUMMED_AREA_END_NOT_ALIGNED;
455 if (index % 8)
456 return LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED;
458 if (end <= start)
459 return LAYOUT_INVALID_SUMMED_AREA;
461 /* Convert bit positions to byte positions. */
462 start /= 8;
463 end /= 8; /* equivalent to "end = ((end - 7) / 8)" */
464 index /= 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;
479 return OK;
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)
504 BUG();
507 /****************************************************************************
508 * areas_overlap
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);
534 item = item->next) ;
536 return ((item == NULL) || (item->item.config_id > config_id)) ?
537 NULL : item;