Clean up comments, whitespace, and copyright date in the AMD HT code.
[coreboot.git] / util / nvramtool / layout.c
1 /*****************************************************************************\
2  * layout.c
3  * $Id$
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>.
8  *  UCRL-CODE-2003-012
9  *  All rights reserved.
10  *
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.
14  *
15  *  Please also read the file DISCLAIMER which is included in this software
16  *  distribution.
17  *
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.
21  *
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.
26  *
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 \*****************************************************************************/
31
32 #include "common.h"
33 #include "layout.h"
34 #include "cmos_lowlevel.h"
35
36 typedef struct cmos_entry_item_t cmos_entry_item_t;
37
38 struct cmos_entry_item_t
39  { cmos_entry_t item;
40    cmos_entry_item_t *next;
41  };
42
43 typedef struct cmos_enum_item_t cmos_enum_item_t;
44
45 struct cmos_enum_item_t
46  { cmos_enum_t item;
47    cmos_enum_item_t *next;
48  };
49
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);
55
56 const char checksum_param_name[] = "check_sum";
57
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.
60  */
61
62 /* This is the offset from the start of CMOS of the first byte that the
63  * checksum is calculated over.
64  */
65 #define CMOS_CHECKSUM_START 49
66
67 /* This is the offset from the start of CMOS of the last byte that the
68  * checksum is calculated over.
69  */
70 #define CMOS_CHECKSUM_END 125
71
72 /* This is the offset from the start of CMOS where the coreboot checksum is
73  * stored.
74  */
75 #define CMOS_CHECKSUM_INDEX 126
76
77 /* index of first byte of checksummed area */
78 unsigned cmos_checksum_start = CMOS_CHECKSUM_START;
79
80 /* index of last byte of checksummed area */
81 unsigned cmos_checksum_end = CMOS_CHECKSUM_END;
82
83 /* index of first byte of CMOS checksum (a big-endian 16-bit value) */
84 unsigned cmos_checksum_index = CMOS_CHECKSUM_INDEX;
85
86 /* List is sorted in ascending order according to 'bit' field in
87  * cmos_entry_t.
88  */
89 static cmos_entry_item_t *cmos_entry_list = NULL;
90
91 /* List is sorted in ascending order: first by 'config_id' and then by
92  * 'value'.
93  */
94 static cmos_enum_item_t *cmos_enum_list = NULL;
95
96 static cmos_layout_get_fn_t cmos_layout_get_fn = default_cmos_layout_get_fn;
97
98 /****************************************************************************
99  * entries_overlap
100  *
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); }
106
107 /****************************************************************************
108  * cmos_entry_to_const_item
109  *
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;
116
117    offset = (unsigned long) pos;
118    address = ((unsigned long) p) - offset;
119    return (const cmos_entry_item_t *) address;
120  }
121
122 /****************************************************************************
123  * cmos_enum_to_const_item
124  *
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;
131
132    offset = (unsigned long) pos;
133    address = ((unsigned long) p) - offset;
134    return (const cmos_enum_item_t *) address;
135  }
136
137 /****************************************************************************
138  * register_cmos_layout_get_fn
139  *
140  * Set 'fn' as the function that will be called to retrieve CMOS layout
141  * information.
142  ****************************************************************************/
143 void register_cmos_layout_get_fn (cmos_layout_get_fn_t fn)
144  { cmos_layout_get_fn = fn; }
145
146 /****************************************************************************
147  * get_cmos_layout
148  *
149  * Retrieve CMOS layout information and store it in our internal repository.
150  ****************************************************************************/
151 void get_cmos_layout (void)
152  { cmos_layout_get_fn(); }
153
154 /****************************************************************************
155  * add_cmos_entry
156  *
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;
164
165    *conflict = NULL;
166
167    if (e->length < 1)
168       return LAYOUT_ENTRY_BAD_LENGTH;
169
170    if ((new_entry = (cmos_entry_item_t *) malloc(sizeof(*new_entry))) == NULL)
171       out_of_memory();
172
173    new_entry->item = *e;
174
175    if (cmos_entry_list == NULL)
176     { new_entry->next = NULL;
177       cmos_entry_list = new_entry;
178       return OK;
179     }
180
181    /* Find place in list to insert new entry.  List is sorted in ascending
182     * order.
183     */
184    for (item = cmos_entry_list, prev = NULL;
185         (item != NULL) && (item->item.bit < e->bit);
186         prev = item, item = item->next);
187
188    if (prev == NULL)
189     { if (entries_overlap(e, &cmos_entry_list->item))
190        { *conflict = &cmos_entry_list->item;
191          goto fail;
192        }
193
194       new_entry->next = cmos_entry_list;
195       cmos_entry_list = new_entry;
196       return OK;
197     }
198
199    if (entries_overlap(&prev->item, e))
200     { *conflict = &prev->item;
201       goto fail;
202     }
203
204    if ((item != NULL) && entries_overlap(e, &item->item))
205     { *conflict = &item->item;
206       goto fail;
207     }
208
209    new_entry->next = item;
210    prev->next = new_entry;
211    return OK;
212
213 fail:
214    free(new_entry);
215    return LAYOUT_ENTRY_OVERLAP;
216  }
217
218 /****************************************************************************
219  * find_cmos_entry
220  *
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;
226
227    for (item = cmos_entry_list; item != NULL; item = item->next)
228     { if (!strcmp(item->item.name, name))
229          return &item->item;
230     }
231
232    return NULL;
233  }
234
235 /****************************************************************************
236  * first_cmos_entry
237  *
238  * Return a pointer to the first CMOS entry in our list or NULL if list is
239  * empty.
240  ****************************************************************************/
241 const cmos_entry_t * first_cmos_entry (void)
242  { return (cmos_entry_list == NULL) ? NULL : &cmos_entry_list->item; }
243
244 /****************************************************************************
245  * next_cmos_entry
246  *
247  * Return a pointer to next entry in list after 'last' or NULL if no more
248  * entries.
249  ****************************************************************************/
250 const cmos_entry_t * next_cmos_entry (const cmos_entry_t *last)
251  { const cmos_entry_item_t *last_item, *next_item;
252
253    last_item = cmos_entry_to_const_item(last);
254    next_item = last_item->next;
255    return (next_item == NULL) ? NULL : &next_item->item;
256  }
257
258 /****************************************************************************
259  * add_cmos_enum
260  *
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;
266
267    if ((new_enum = (cmos_enum_item_t *) malloc(sizeof(*new_enum))) == NULL)
268       out_of_memory();
269
270    new_enum->item = *e;
271
272    if (cmos_enum_list == NULL)
273     { new_enum->next = NULL;
274       cmos_enum_list = new_enum;
275       return OK;
276     }
277
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
280     * matches 'e'.
281     */
282    for (item = cmos_enum_list, prev = NULL;
283         (item != NULL) && (item->item.config_id < e->config_id);
284         prev = item, item = item->next);
285
286    if (item == NULL)
287     { new_enum->next = NULL;
288       prev->next = new_enum;
289       return OK;
290     }
291
292    if (item->item.config_id > e->config_id)
293     { new_enum->next = item;
294
295       if (prev == NULL)
296          cmos_enum_list = new_enum;
297       else
298          prev->next = new_enum;
299
300       return OK;
301     }
302
303    /* List already contains at least one enum whose 'config_id' matches 'e'.
304     * Now find proper place to insert 'e' based on 'value'.
305     */
306    while (item->item.value < e->value)
307     { prev = item;
308       item = item->next;
309
310       if ((item == NULL) || (item->item.config_id != e->config_id))
311        { new_enum->next = item;
312          prev->next = new_enum;
313          return OK;
314        }
315     }
316
317    if (item->item.value == e->value)
318     { free(new_enum);
319       return LAYOUT_DUPLICATE_ENUM;
320     }
321
322    new_enum->next = item;
323
324    if (prev == NULL)
325       cmos_enum_list = new_enum;
326    else
327       prev->next = new_enum;
328
329    return OK;
330  }
331
332 /****************************************************************************
333  * find_cmos_enum
334  *
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;
341
342    if ((item = find_first_cmos_enum_id(config_id)) == NULL)
343       return NULL;
344
345    while (item->item.value < value)
346     { item = item->next;
347
348       if ((item == NULL) || (item->item.config_id != config_id))
349          return NULL;
350     }
351
352    return (item->item.value == value) ? &item->item : NULL;
353  }
354
355 /****************************************************************************
356  * first_cmos_enum
357  *
358  * Return a pointer to the first CMOS enum in our list or NULL if list is
359  * empty.
360  ****************************************************************************/
361 const cmos_enum_t * first_cmos_enum (void)
362  { return (cmos_enum_list == NULL) ? NULL : &cmos_enum_list->item; }
363
364 /****************************************************************************
365  * next_cmos_enum
366  *
367  * Return a pointer to next enum in list after 'last' or NULL if no more
368  * enums.
369  ****************************************************************************/
370 const cmos_enum_t * next_cmos_enum (const cmos_enum_t *last)
371  { const cmos_enum_item_t *last_item, *next_item;
372
373    last_item = cmos_enum_to_const_item(last);
374    next_item = last_item->next;
375    return (next_item == NULL) ? NULL : &next_item->item;
376  }
377
378 /****************************************************************************
379  * first_cmos_enum_id
380  *
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;
386
387    item = find_first_cmos_enum_id(config_id);
388    return (item == NULL) ? NULL : &item->item;
389  }
390
391 /****************************************************************************
392  * next_cmos_enum_id
393  *
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;
399
400    item = cmos_enum_to_const_item(last)->next;
401    return ((item == NULL) || (item->item.config_id != last->config_id)) ?
402           NULL : &item->item;
403  }
404
405 /****************************************************************************
406  * is_checksum_name
407  *
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); }
413
414 /****************************************************************************
415  * checksum_layout_to_bytes
416  *
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;
424
425    start = layout->summed_area_start;
426    end = layout->summed_area_end;
427    index = layout->checksum_at;
428
429    if (start % 8)
430       return LAYOUT_SUMMED_AREA_START_NOT_ALIGNED;
431
432    if ((end % 8) != 7)
433       return LAYOUT_SUMMED_AREA_END_NOT_ALIGNED;
434
435    if (index % 8)
436       return LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED;
437
438    if (end <= start)
439       return LAYOUT_INVALID_SUMMED_AREA;
440
441    /* Convert bit positions to byte positions. */
442    start /= 8;
443    end /= 8;  /* equivalent to "end = ((end - 7) / 8)" */
444    index /= 8;
445
446    if (verify_cmos_byte_index(start) || verify_cmos_byte_index(end))
447       return LAYOUT_SUMMED_AREA_OUT_OF_RANGE;
448
449    if (verify_cmos_byte_index(index))
450       return LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE;
451
452    /* checksum occupies 16 bits */
453    if (areas_overlap(start, end - start + 1, index, index + 1))
454       return LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA;
455
456    layout->summed_area_start = start;
457    layout->summed_area_end = end;
458    layout->checksum_at = index;
459    return OK;
460  }
461
462 /****************************************************************************
463  * checksum_layout_to_bits
464  *
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;
472  }
473
474 /****************************************************************************
475  * default_cmos_layout_get_fn
476  *
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)
482  { BUG(); }
483
484 /****************************************************************************
485  * areas_overlap
486  *
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;
492
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));
496  }
497
498 /****************************************************************************
499  * find_first_cmos_enum_id
500  *
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;
506
507    for (item = cmos_enum_list;
508         (item != NULL) && (item->item.config_id < config_id);
509         item = item->next);
510
511    return ((item == NULL) || (item->item.config_id > config_id)) ?
512           NULL : item;
513  }