commit svn:externals for last commit (what a pain) (1/2)
[coreboot.git] / util / nvramtool / lbtable.c
1 /*****************************************************************************\
2  * lbtable.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  *  and Stefan Reinauer <stepan@openbios.org>.
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 <string.h>
33 #include <sys/mman.h>
34 #include "common.h"
35 #include "coreboot_tables.h"
36 #include "ip_checksum.h"
37 #include "lbtable.h"
38 #include "layout.h"
39 #include "cmos_lowlevel.h"
40 #include "hexdump.h"
41
42 typedef void (*lbtable_print_fn_t) (const struct lb_record *rec);
43
44 /* This structure represents an item in the coreboot table that may be
45  * displayed using the -l option.
46  */
47 typedef struct
48  { uint32_t tag;
49    const char *name;
50    const char *description;
51    const char *nofound_msg;
52    lbtable_print_fn_t print_fn;
53  }
54 lbtable_choice_t;
55
56 typedef struct
57  { unsigned long start;  /* address of first byte of memory range */
58    unsigned long end;  /* address of last byte of memory range */
59  }
60 mem_range_t;
61
62 static const struct lb_header * lbtable_scan (unsigned long start,
63                                               unsigned long end,
64                                               int *bad_header_count,
65                                               int *bad_table_count);
66 static void process_cmos_table (void);
67 static void get_cmos_checksum_info (void);
68 static void try_convert_checksum_layout (cmos_checksum_layout_t *layout);
69 static void try_add_cmos_table_enum (cmos_enum_t *cmos_enum);
70 static void try_add_cmos_table_entry (cmos_entry_t *cmos_entry);
71 static const struct lb_record * find_lbrec (uint32_t tag);
72 static const char * lbrec_tag_to_str (uint32_t tag);
73 static const struct cmos_entries * first_cmos_table_entry (void);
74 static const struct cmos_entries *
75       next_cmos_table_entry (const struct cmos_entries *last);
76 static const struct cmos_enums * first_cmos_table_enum (void);
77 static const struct cmos_enums * next_cmos_table_enum
78       (const struct cmos_enums *last);
79 static const struct lb_record * first_cmos_rec (uint32_t tag);
80 static const struct lb_record * next_cmos_rec (const struct lb_record *last,
81                                                uint32_t tag);
82 static void memory_print_fn (const struct lb_record *rec);
83 static void mainboard_print_fn (const struct lb_record *rec);
84 static void cmos_opt_table_print_fn (const struct lb_record *rec);
85 static void print_option_record (const struct cmos_entries *cmos_entry);
86 static void print_enum_record (const struct cmos_enums *cmos_enum);
87 static void print_defaults_record (const struct cmos_defaults *cmos_defaults);
88 static void print_unknown_record (const struct lb_record *cmos_item);
89 static void option_checksum_print_fn (const struct lb_record *rec);
90 static void string_print_fn (const struct lb_record *rec);
91 static void uint64_to_hex_string (char str[], uint64_t n);
92
93 static const char memory_desc[] =
94 "    This shows information about system memory.\n";
95
96 static const char mainboard_desc[] =
97 "    This shows information about your mainboard.\n";
98
99 static const char version_desc[] =
100 "    This shows coreboot version information.\n";
101
102 static const char extra_version_desc[] =
103 "    This shows extra coreboot version information.\n";
104
105 static const char build_desc[] =
106 "    This shows coreboot build information.\n";
107
108 static const char compile_time_desc[] =
109 "    This shows when coreboot was compiled.\n";
110
111 static const char compile_by_desc[] =
112 "    This shows who compiled coreboot.\n";
113
114 static const char compile_host_desc[] =
115 "    This shows the name of the machine that compiled coreboot.\n";
116
117 static const char compile_domain_desc[] =
118 "    This shows the domain name of the machine that compiled coreboot.\n";
119
120 static const char compiler_desc[] =
121 "    This shows the name of the compiler used to build coreboot.\n";
122
123 static const char linker_desc[] =
124 "    This shows the name of the linker used to build coreboot.\n";
125
126 static const char assembler_desc[] =
127 "    This shows the name of the assembler used to build coreboot.\n";
128
129 static const char cmos_opt_table_desc[] =
130 "    This does a low-level dump of the CMOS option table.  The table "
131 "contains\n"
132 "    information about the layout of the values that coreboot stores in\n"
133 "    nonvolatile RAM.\n";
134
135 static const char option_checksum_desc[] =
136 "    This shows the location of the CMOS checksum and the area over which it "
137 "is\n"
138 "    calculated.\n";
139
140 static const char generic_nofound_msg[] =
141 "%s: Item %s not found in coreboot table.\n";
142
143 static const char nofound_msg_cmos_opt_table[] =
144 "%s: Item %s not found in coreboot table.  Apparently, the "
145 "coreboot installed on this system was built without specifying "
146 "CONFIG_HAVE_OPTION_TABLE.\n";
147
148 static const char nofound_msg_option_checksum[] =
149 "%s: Item %s not found in coreboot table. Apparently, you are "
150 "using coreboot v1.\n";
151
152 int fd;
153
154 /* This is the number of items from the coreboot table that may be displayed
155  * using the -l option.
156  */
157 #define NUM_LBTABLE_CHOICES 14
158
159 /* These represent the various items from the coreboot table that may be
160  * displayed using the -l option.
161  */
162 static const lbtable_choice_t lbtable_choices[NUM_LBTABLE_CHOICES] =
163  { { LB_TAG_MEMORY,            "memory",
164      memory_desc,              generic_nofound_msg,
165      memory_print_fn
166    },
167    { LB_TAG_MAINBOARD,         "mainboard",
168      mainboard_desc,           generic_nofound_msg,
169      mainboard_print_fn
170    },
171    { LB_TAG_VERSION,           "version",
172      version_desc,             generic_nofound_msg,
173      string_print_fn
174    },
175    { LB_TAG_EXTRA_VERSION,     "extra_version",
176      extra_version_desc,       generic_nofound_msg,
177      string_print_fn
178    },
179    { LB_TAG_BUILD,             "build",
180      build_desc,               generic_nofound_msg,
181      string_print_fn
182    },
183    { LB_TAG_COMPILE_TIME,      "compile_time",
184      compile_time_desc,        generic_nofound_msg,
185      string_print_fn
186    },
187    { LB_TAG_COMPILE_BY,        "compile_by",
188      compile_by_desc,          generic_nofound_msg,
189      string_print_fn
190    },
191    { LB_TAG_COMPILE_HOST,      "compile_host",
192      compile_host_desc,        generic_nofound_msg,
193      string_print_fn
194    },
195    { LB_TAG_COMPILE_DOMAIN,    "compile_domain",
196      compile_domain_desc,      generic_nofound_msg,
197      string_print_fn
198    },
199    { LB_TAG_COMPILER,          "compiler",
200      compiler_desc,            generic_nofound_msg,
201      string_print_fn
202    },
203    { LB_TAG_LINKER,            "linker",
204      linker_desc,              generic_nofound_msg,
205      string_print_fn
206    },
207    { LB_TAG_ASSEMBLER,         "assembler",
208      assembler_desc,           generic_nofound_msg,
209      string_print_fn
210    },
211    { LB_TAG_CMOS_OPTION_TABLE, "cmos_opt_table",
212      cmos_opt_table_desc,      nofound_msg_cmos_opt_table,
213      cmos_opt_table_print_fn
214    },
215    { LB_TAG_OPTION_CHECKSUM, "option_checksum",
216      option_checksum_desc,     nofound_msg_option_checksum,
217      option_checksum_print_fn
218    }
219  };
220
221 /* The coreboot table resides in low physical memory, which we access using
222  * /dev/mem.  These are ranges of physical memory that should be scanned for a
223  * coreboot table.
224  */
225
226 #define NUM_MEM_RANGES 2
227
228 static const mem_range_t mem_ranges[NUM_MEM_RANGES] =
229  { { 0x00000000, 0x00000fff },
230    { 0x000f0000, 0x000fffff }
231  };
232
233 /* This is the number of bytes of physical memory to map, starting at physical
234  * address 0.  This value must be large enough to contain all memory ranges
235  * specified in mem_ranges above plus the maximum possible size of the
236  * coreboot table (since the start of the table could potentially occur at
237  * the end of the last memory range).
238  */
239 static const size_t BYTES_TO_MAP = (1024 * 1024);
240
241 /* Pointer to low physical memory that we access by calling mmap() on
242  * /dev/mem.
243  */
244 static const void *low_phys_mem;
245 static unsigned long low_phys_base = 0;
246
247 /* Pointer to coreboot table. */
248 static const struct lb_header *lbtable = NULL;
249
250 /* The CMOS option table is located within the coreboot table.  It tells us
251  * where the CMOS parameters are located in the nonvolatile RAM.
252  */
253 static const struct cmos_option_table *cmos_table = NULL;
254
255 static const hexdump_format_t format =
256  { 12, 4, "            ", " | ", " ", " | ", '.', NULL };
257
258 /****************************************************************************
259  * vtophys
260  *
261  * Convert a virtual address to a physical address.  'vaddr' is a virtual
262  * address in the address space of the current process.  It points to
263  * somewhere in the chunk of memory that we mapped by calling mmap() on
264  * /dev/mem.  This macro converts 'vaddr' to a physical address.
265  ****************************************************************************/
266 #define vtophys(vaddr) (((unsigned long) vaddr) -       \
267                         ((unsigned long) low_phys_mem) + low_phys_base)
268
269 /****************************************************************************
270  * phystov
271  *
272  * Convert a physical address to a virtual address.  'paddr' is a physical
273  * address.  This macro converts 'paddr' to a virtual address in the address
274  * space of the current process.  The virtual to physical mapping was set up
275  * by calling mmap() on /dev/mem.
276  ****************************************************************************/
277 #define phystov(paddr) (((unsigned long) low_phys_mem) + \
278                         ((unsigned long) paddr) - low_phys_base)
279
280 /****************************************************************************
281  * get_lbtable
282  *
283  * Find the coreboot table and set global variable lbtable to point to it.
284  ****************************************************************************/
285 void get_lbtable (void)
286  { int i, bad_header_count, bad_table_count, bad_headers, bad_tables;
287
288    if (lbtable != NULL)
289       return;
290
291    /* The coreboot table is located in low physical memory, which may be
292     * conveniently accessed by calling mmap() on /dev/mem.
293     */
294
295    if ((fd = open("/dev/mem", O_RDONLY, 0)) < 0)
296     { fprintf(stderr, "%s: Can not open /dev/mem for reading: %s\n",
297               prog_name, strerror(errno));
298       exit(1);
299     }
300
301    if ((low_phys_mem = mmap(NULL, BYTES_TO_MAP, PROT_READ, MAP_SHARED, fd, 0))
302        == MAP_FAILED)
303     { fprintf(stderr, "%s: Failed to mmap /dev/mem: %s\n", prog_name,
304               strerror(errno));
305       exit(1);
306     }
307
308    bad_header_count = 0;
309    bad_table_count = 0;
310
311    for (i = 0; i < NUM_MEM_RANGES; i++)
312     { lbtable = lbtable_scan(phystov(mem_ranges[i].start),
313                              phystov(mem_ranges[i].end),
314                              &bad_headers, &bad_tables);
315
316       if (lbtable != NULL)
317          return; /* success: we found it! */
318
319       bad_header_count += bad_headers;
320       bad_table_count += bad_tables;
321     }
322
323    fprintf(stderr,
324            "%s: coreboot table not found.  coreboot does not appear to\n"
325            "        be installed on this system.  Scanning for the table "
326            "produced the\n"
327            "        following results:\n\n"
328            "            %d valid signatures were found with bad header "
329            "checksums.\n"
330            "            %d valid headers were found with bad table "
331            "checksums.\n",
332            prog_name, bad_header_count, bad_table_count);
333    exit(1);
334  }
335
336 /****************************************************************************
337  * get_layout_from_cmos_table
338  *
339  * Find the CMOS table which is stored within the coreboot table and set the
340  * global variable cmos_table to point to it.
341  ****************************************************************************/
342 void get_layout_from_cmos_table (void)
343  {
344
345    get_lbtable();
346    cmos_table = (const struct cmos_option_table *)
347                 find_lbrec(LB_TAG_CMOS_OPTION_TABLE);
348
349    if ((cmos_table) == NULL)
350     { fprintf(stderr,
351               "%s: CMOS option table not found in coreboot table.  "
352               "Apparently, the coreboot installed on this system was "
353               "built without specifying CONFIG_HAVE_OPTION_TABLE.\n",
354               prog_name);
355       exit(1);
356     }
357
358    process_cmos_table();
359    get_cmos_checksum_info();
360  }
361
362 /****************************************************************************
363  * dump_lbtable
364  *
365  * Do a low-level dump of the coreboot table.
366  ****************************************************************************/
367 void dump_lbtable (void)
368  { const char *p, *data;
369    uint32_t bytes_processed;
370    const struct lb_record *lbrec;
371
372    p = ((const char *) lbtable) + lbtable->header_bytes;
373    printf("Coreboot table at physical address 0x%lx:\n"
374           "    signature:       0x%x (ASCII: %c%c%c%c)\n"
375           "    header_bytes:    0x%x (decimal: %d)\n"
376           "    header_checksum: 0x%x (decimal: %d)\n"
377           "    table_bytes:     0x%x (decimal: %d)\n"
378           "    table_checksum:  0x%x (decimal: %d)\n"
379           "    table_entries:   0x%x (decimal: %d)\n\n",
380           vtophys(lbtable), *((uint32_t *) lbtable->signature),
381           lbtable->signature[0], lbtable->signature[1],lbtable->signature[2],
382           lbtable->signature[3], lbtable->header_bytes, lbtable->header_bytes,
383           lbtable->header_checksum, lbtable->header_checksum,
384           lbtable->table_bytes, lbtable->table_bytes, lbtable->table_checksum,
385           lbtable->table_checksum, lbtable->table_entries,
386           lbtable->table_entries);
387
388    if ((lbtable->table_bytes == 0) != (lbtable->table_entries == 0))
389     { printf("Inconsistent values for table_bytes and table_entries!!!\n"
390              "They should be either both 0 or both nonzero.\n");
391       return;
392     }
393
394    if (lbtable->table_bytes == 0)
395     { printf("The coreboot table is empty!!!\n");
396       return;
397     }
398
399    for (bytes_processed = 0; ; )
400     { lbrec = (const struct lb_record *) &p[bytes_processed];
401       printf("    %s record at physical address 0x%lx:\n"
402              "        tag:  0x%x (decimal: %d)\n"
403              "        size: 0x%x (decimal: %d)\n"
404              "        data:\n",
405              lbrec_tag_to_str(lbrec->tag), vtophys(lbrec), lbrec->tag,
406              lbrec->tag, lbrec->size, lbrec->size);
407
408       data = ((const char *) lbrec) + sizeof(*lbrec);
409       hexdump(data, lbrec->size - sizeof(*lbrec), vtophys(data), stdout,
410               &format);
411
412       bytes_processed += lbrec->size;
413
414       if (bytes_processed >= lbtable->table_bytes)
415          break;
416
417       printf("\n");
418     }
419  }
420
421 /****************************************************************************
422  * list_lbtable_choices
423  *
424  * List names and informational blurbs for items from the coreboot table
425  * that may be displayed using the -l option.
426  ****************************************************************************/
427 void list_lbtable_choices (void)
428  { int i;
429
430    for (i = 0; ; )
431     { printf("%s:\n%s",
432              lbtable_choices[i].name, lbtable_choices[i].description);
433
434       if (++i >= NUM_LBTABLE_CHOICES)
435          break;
436
437       printf("\n");
438     }
439  }
440
441 /****************************************************************************
442  * list_lbtable_item
443  *
444  * Show the coreboot table item specified by 'item'.
445  ****************************************************************************/
446 void list_lbtable_item (const char item[])
447  { int i;
448    const struct lb_record *rec;
449
450    for (i = 0; i < NUM_LBTABLE_CHOICES; i++)
451     { if (strcmp(item, lbtable_choices[i].name) == 0)
452          break;
453     }
454
455    if (i == NUM_LBTABLE_CHOICES)
456     { fprintf(stderr, "%s: Invalid coreboot table item %s.\n", prog_name,
457               item);
458       exit(1);
459     }
460
461    if ((rec = find_lbrec(lbtable_choices[i].tag)) == NULL)
462     { fprintf(stderr, lbtable_choices[i].nofound_msg, prog_name,
463               lbtable_choices[i].name);
464       exit(1);
465     }
466
467    lbtable_choices[i].print_fn(rec);
468  }
469
470 /****************************************************************************
471  * lbtable_scan
472  *
473  * Scan the chunk of memory specified by 'start' and 'end' for a coreboot
474  * table.  The first 4 bytes of the table are marked by the signature
475  * { 'L', 'B', 'I', 'O' }.  'start' and 'end' indicate the addresses of the
476  * first and last bytes of the chunk of memory to be scanned.  For instance,
477  * values of 0x10000000 and 0x1000ffff for 'start' and 'end' specify a 64k
478  * chunk of memory starting at address 0x10000000.  'start' and 'end' are
479  * virtual addresses in the address space of the current process.  They
480  * represent a chunk of memory obtained by calling mmap() on /dev/mem.
481  *
482  * If a coreboot table is found, return a pointer to it.  Otherwise return
483  * NULL.  On return, *bad_header_count and *bad_table_count are set as
484  * follows:
485  *
486  *     *bad_header_count:
487  *         Indicates the number of times in which a valid signature was found
488  *         but the header checksum was invalid.
489  *
490  *     *bad_table_count:
491  *         Indicates the number of times in which a header with a valid
492  *         checksum was found but the table checksum was invalid.
493  ****************************************************************************/
494 static const struct lb_header * lbtable_scan (unsigned long start,
495                                               unsigned long end,
496                                               int *bad_header_count,
497                                               int *bad_table_count)
498  { static const char signature[] = { 'L', 'B', 'I', 'O' };
499    const struct lb_header *table;
500    const struct lb_forward *forward;
501    const uint32_t *p;
502    uint32_t sig;
503
504    assert(end >= start);
505    sig = (*((const uint32_t *) signature));
506    table = NULL;
507    *bad_header_count = 0;
508    *bad_table_count = 0;
509
510    /* Look for signature.  Table is aligned on 16-byte boundary.  Therefore
511     * only check every fourth 32-bit memory word.  As the loop is coded below,
512     * this function will behave in a reasonable manner for ALL possible values
513     * for 'start' and 'end': even weird boundary cases like 0x00000000 and
514     * 0xffffffff on a 32-bit architecture.
515     */
516    for (p = (const uint32_t *) start;
517         (((unsigned long) p) <= end) &&
518         ((end - (unsigned long) p) >= (sizeof(uint32_t) - 1));
519         p += 4)
520     { if (*p != sig)
521          continue;
522
523       /* We found a valid signature. */
524       table = (const struct lb_header *) p;
525
526       /* validate header checksum */
527       if (compute_ip_checksum((void *) table, sizeof(*table)))
528        { (*bad_header_count)++;
529          continue;
530        }
531
532       /* validate table checksum */
533       if (table->table_checksum !=
534           compute_ip_checksum(((char *) table) + sizeof(*table),
535                               table->table_bytes))
536        { (*bad_table_count)++;
537          continue;
538        }
539
540       /* checksums are ok: we found it! */
541       /* But it may just be a forwarding table, so look if there's a forwarder */
542       lbtable = table;
543       forward = (struct lb_forward *)find_lbrec(LB_TAG_FORWARD);
544       lbtable = NULL;
545
546       if (forward) {
547         uint64_t new_phys = forward->forward;
548
549         new_phys &= ~(getpagesize()-1);
550         
551         munmap((void *)low_phys_mem, BYTES_TO_MAP);
552         if ((low_phys_mem = mmap(NULL, BYTES_TO_MAP, PROT_READ, MAP_SHARED, fd, (off_t)new_phys)) == MAP_FAILED)
553         { fprintf(stderr, "%s: Failed to mmap /dev/mem: %s\n", prog_name,
554               strerror(errno));
555           exit(1);
556         }
557         low_phys_base = new_phys;
558         table = lbtable_scan(phystov(low_phys_base), phystov(low_phys_base + BYTES_TO_MAP), bad_header_count, bad_table_count);
559       }
560       return table;
561     }
562
563    return NULL;
564  }
565
566 /****************************************************************************
567  * process_cmos_table
568  *
569  * Extract layout information from the CMOS option table and store it in our
570  * internal repository.
571  ****************************************************************************/
572 static void process_cmos_table (void)
573  { const struct cmos_enums *p;
574    const struct cmos_entries *q;
575    cmos_enum_t cmos_enum;
576    cmos_entry_t cmos_entry;
577
578    /* First add the enums. */
579    for (p = first_cmos_table_enum(); p != NULL; p = next_cmos_table_enum(p))
580     { cmos_enum.config_id = p->config_id;
581       cmos_enum.value = p->value;
582       strncpy(cmos_enum.text, (char *)p->text, CMOS_MAX_TEXT_LENGTH);
583       cmos_enum.text[CMOS_MAX_TEXT_LENGTH] = '\0';
584       try_add_cmos_table_enum(&cmos_enum);
585     }
586
587    /* Now add the entries.  We must add the entries after the enums because
588     * the entries are sanity checked against the enums as they are added.
589     */
590    for (q = first_cmos_table_entry(); q != NULL; q = next_cmos_table_entry(q))
591     { cmos_entry.bit = q->bit;
592       cmos_entry.length = q->length;
593
594       switch (q->config)
595        { case 'e':
596             cmos_entry.config = CMOS_ENTRY_ENUM;
597             break;
598
599          case 'h':
600             cmos_entry.config = CMOS_ENTRY_HEX;
601             break;
602
603          case 'r':
604             cmos_entry.config = CMOS_ENTRY_RESERVED;
605             break;
606
607          case 's':
608             cmos_entry.config = CMOS_ENTRY_STRING;
609             break;
610
611          default:
612             fprintf(stderr,
613                     "%s: Entry in CMOS option table has unknown config "
614                     "value.\n", prog_name);
615             exit(1);
616        }
617
618       cmos_entry.config_id = q->config_id;
619       strncpy(cmos_entry.name, (char *)q->name, CMOS_MAX_NAME_LENGTH);
620       cmos_entry.name[CMOS_MAX_NAME_LENGTH] = '\0';
621       try_add_cmos_table_entry(&cmos_entry);
622     }
623  }
624
625 /****************************************************************************
626  * get_cmos_checksum_info
627  *
628  * Get layout information for CMOS checksum.
629  ****************************************************************************/
630 static void get_cmos_checksum_info (void)
631  { const cmos_entry_t *e;
632    struct cmos_checksum *checksum;
633    cmos_checksum_layout_t layout;
634    unsigned index, index2;
635
636    checksum = (struct cmos_checksum *) find_lbrec(LB_TAG_OPTION_CHECKSUM);
637
638    if (checksum != NULL)
639     { /* We are lucky.  The coreboot table hints us to the checksum.
640        * We might have to check the type field here though.
641        */
642       layout.summed_area_start = checksum->range_start;
643       layout.summed_area_end = checksum->range_end;
644       layout.checksum_at = checksum->location;
645       try_convert_checksum_layout(&layout);
646       cmos_checksum_start = layout.summed_area_start;
647       cmos_checksum_end = layout.summed_area_end;
648       cmos_checksum_index = layout.checksum_at;
649       return;
650     }
651
652    if ((e = find_cmos_entry(checksum_param_name)) == NULL)
653       return;
654
655    /* If we get here, we are unlucky.  The CMOS option table contains the
656     * location of the CMOS checksum.  However, there is no information
657     * regarding which bytes of the CMOS area the checksum is computed over.
658     * Thus we have to hope our presets will be fine.
659     */
660
661    if (e->bit % 8)
662     { fprintf(stderr, "%s: Error: CMOS checksum is not byte-aligned.\n",
663               prog_name);
664       exit(1);
665     }
666
667    index = e->bit / 8;
668    index2 = index + 1;  /* The CMOS checksum occupies 16 bits. */
669
670    if (verify_cmos_byte_index(index) || verify_cmos_byte_index(index2))
671     { fprintf(stderr, "%s: Error: CMOS checksum location out of range.\n",
672               prog_name);
673       exit(1);
674     }
675
676    if (((index >= cmos_checksum_start) && (index <= cmos_checksum_end)) ||
677        (((index2) >= cmos_checksum_start) && ((index2) <= cmos_checksum_end)))
678     { fprintf(stderr, "%s: Error: CMOS checksum overlaps checksummed area.\n",
679               prog_name);
680       exit(1);
681     }
682
683    cmos_checksum_index = index;
684  }
685
686 /****************************************************************************
687  * try_convert_checksum_layout
688  *
689  * Perform sanity checking on CMOS checksum layout information and attempt to
690  * convert information from bit positions to byte positions.  Return OK on
691  * success or an error code on failure.
692  ****************************************************************************/
693 static void try_convert_checksum_layout (cmos_checksum_layout_t *layout)
694  { switch (checksum_layout_to_bytes(layout))
695     { case OK:
696          return;
697
698       case LAYOUT_SUMMED_AREA_START_NOT_ALIGNED:
699          fprintf(stderr,
700                  "%s: CMOS checksummed area start is not byte-aligned.\n",
701                  prog_name);
702          break;
703
704       case LAYOUT_SUMMED_AREA_END_NOT_ALIGNED:
705          fprintf(stderr,
706                  "%s: CMOS checksummed area end is not byte-aligned.\n",
707                  prog_name);
708          break;
709
710       case LAYOUT_CHECKSUM_LOCATION_NOT_ALIGNED:
711          fprintf(stderr,
712                  "%s: CMOS checksum location is not byte-aligned.\n",
713                  prog_name);
714          break;
715
716       case LAYOUT_INVALID_SUMMED_AREA:
717          fprintf(stderr,
718                  "%s: CMOS checksummed area end must be greater than "
719                  "CMOS checksummed area start.\n",
720                  prog_name);
721          break;
722
723       case LAYOUT_CHECKSUM_OVERLAPS_SUMMED_AREA:
724          fprintf(stderr,
725                  "%s: CMOS checksum overlaps checksummed area.\n",
726                  prog_name);
727          break;
728
729       case LAYOUT_SUMMED_AREA_OUT_OF_RANGE:
730          fprintf(stderr,
731                  "%s: CMOS checksummed area out of range.\n",
732                  prog_name);
733          break;
734
735       case LAYOUT_CHECKSUM_LOCATION_OUT_OF_RANGE:
736          fprintf(stderr,
737                  "%s: CMOS checksum location out of range.\n",
738                  prog_name);
739          break;
740
741       default:
742          BUG();
743     }
744
745    exit(1);
746  }
747
748 /****************************************************************************
749  * try_add_cmos_table_enum
750  *
751  * Attempt to add a CMOS enum to our internal repository.  Exit with an error
752  * message on failure.
753  ****************************************************************************/
754 static void try_add_cmos_table_enum (cmos_enum_t *cmos_enum)
755  { switch (add_cmos_enum(cmos_enum))
756     { case OK:
757          return;
758
759       case LAYOUT_DUPLICATE_ENUM:
760          fprintf(stderr, "%s: Duplicate enum %s found in CMOS option "
761                  "table.\n", prog_name, cmos_enum->text);
762          break;
763
764       default:
765          BUG();
766     }
767
768    exit(1);
769  }
770
771 /****************************************************************************
772  * try_add_cmos_table_entry
773  *
774  * Attempt to add a CMOS entry to our internal repository.  Exit with an
775  * error message on failure.
776  ****************************************************************************/
777 static void try_add_cmos_table_entry (cmos_entry_t *cmos_entry)
778  { const cmos_entry_t *conflict;
779
780    switch (add_cmos_entry(cmos_entry, &conflict))
781     { case OK:
782          return;
783
784       case CMOS_AREA_OUT_OF_RANGE:
785          fprintf(stderr,
786                  "%s: Bad CMOS option layout in CMOS option table entry "
787                  "%s.\n", prog_name, cmos_entry->name);
788          break;
789
790       case CMOS_AREA_TOO_WIDE:
791          fprintf(stderr,
792                  "%s: Area too wide for CMOS option table entry %s.\n",
793                  prog_name, cmos_entry->name);
794          break;
795
796       case LAYOUT_ENTRY_OVERLAP:
797          fprintf(stderr,
798                  "%s: CMOS option table entries %s and %s have overlapping "
799                  "layouts.\n", prog_name, cmos_entry->name, conflict->name);
800          break;
801
802       case LAYOUT_ENTRY_BAD_LENGTH:
803          /* Silently ignore entries with zero length.  Although this should
804           * never happen in practice, we should handle the case in a
805           * reasonable manner just to be safe.
806           */
807          return;
808
809       default:
810          BUG();
811     }
812
813    exit(1);
814  }
815
816 /****************************************************************************
817  * find_lbrec
818  *
819  * Find the record in the coreboot table that matches 'tag'.  Return pointer
820  * to record on success or NULL if record not found.
821  ****************************************************************************/
822 static const struct lb_record * find_lbrec (uint32_t tag)
823  { const char *p;
824    uint32_t bytes_processed;
825    const struct lb_record *lbrec;
826
827    p = ((const char *) lbtable) + lbtable->header_bytes;
828
829    for (bytes_processed = 0;
830         bytes_processed < lbtable->table_bytes;
831         bytes_processed += lbrec->size)
832     { lbrec = (const struct lb_record *) &p[bytes_processed];
833
834       if (lbrec->tag == tag)
835          return lbrec;
836     }
837
838    return NULL;
839  }
840
841 /****************************************************************************
842  * lbrec_tag_to_str
843  *
844  * Return a pointer to the string representation of the given coreboot table
845  * tag.
846  ****************************************************************************/
847 static const char * lbrec_tag_to_str (uint32_t tag)
848  { switch (tag)
849     { case LB_TAG_UNUSED:
850          return "UNUSED";
851
852       case LB_TAG_MEMORY:
853          return "MEMORY";
854
855       case LB_TAG_HWRPB:
856          return "HWRPB";
857
858       case LB_TAG_MAINBOARD:
859          return "MAINBOARD";
860
861       case LB_TAG_VERSION:
862          return "VERSION";
863
864       case LB_TAG_EXTRA_VERSION:
865          return "EXTRA_VERSION";
866
867       case LB_TAG_BUILD:
868          return "BUILD";
869
870       case LB_TAG_COMPILE_TIME:
871          return "COMPILE_TIME";
872
873       case LB_TAG_COMPILE_BY:
874          return "COMPILE_BY";
875
876       case LB_TAG_COMPILE_HOST:
877          return "COMPILE_HOST";
878
879       case LB_TAG_COMPILE_DOMAIN:
880          return "COMPILE_DOMAIN";
881
882       case LB_TAG_COMPILER:
883          return "COMPILER";
884
885       case LB_TAG_LINKER:
886          return "LINKER";
887
888       case LB_TAG_ASSEMBLER:
889          return "ASSEMBLER";
890
891       case LB_TAG_SERIAL:
892          return "SERIAL";
893
894       case LB_TAG_CONSOLE:
895          return "CONSOLE";
896
897       case LB_TAG_FORWARD:
898          return "FORWARD";
899
900       case LB_TAG_CMOS_OPTION_TABLE:
901          return "CMOS_OPTION_TABLE";
902
903       case LB_TAG_OPTION_CHECKSUM:
904          return "OPTION_CHECKSUM";
905
906       default:
907          break;
908     }
909
910    return "UNKNOWN";
911  }
912
913 /****************************************************************************
914  * first_cmos_table_entry
915  *
916  * Return a pointer to the first entry in the CMOS table that represents a
917  * CMOS parameter.  Return NULL if CMOS table is empty.
918  ****************************************************************************/
919 static const struct cmos_entries * first_cmos_table_entry (void)
920  { return (const struct cmos_entries *) first_cmos_rec(LB_TAG_OPTION); }
921
922 /****************************************************************************
923  * next_cmos_table_entry
924  *
925  * Return a pointer to the next entry after 'last' in the CMOS table that
926  * represents a CMOS parameter.  Return NULL if there are no more parameters.
927  ****************************************************************************/
928 static const struct cmos_entries *
929       next_cmos_table_entry (const struct cmos_entries *last)
930  { return (const struct cmos_entries *)
931           next_cmos_rec((const struct lb_record *) last, LB_TAG_OPTION);
932  }
933
934 /****************************************************************************
935  * first_cmos_table_enum
936  *
937  * Return a pointer to the first entry in the CMOS table that represents a
938  * possible CMOS parameter value.  Return NULL if the table does not contain
939  * any such entries.
940  ****************************************************************************/
941 static const struct cmos_enums * first_cmos_table_enum (void)
942  { return (const struct cmos_enums *) first_cmos_rec(LB_TAG_OPTION_ENUM); }
943
944 /****************************************************************************
945  * next_cmos_table_enum
946  *
947  * Return a pointer to the next entry after 'last' in the CMOS table that
948  * represents a possible CMOS parameter value.  Return NULL if there are no
949  * more parameter values.
950  ****************************************************************************/
951 static const struct cmos_enums * next_cmos_table_enum
952       (const struct cmos_enums *last)
953  { return (const struct cmos_enums *)
954           next_cmos_rec((const struct lb_record *) last, LB_TAG_OPTION_ENUM);
955  }
956
957 /****************************************************************************
958  * first_cmos_rec
959  *
960  * Return a pointer to the first entry in the CMOS table whose type matches
961  * 'tag'.  Return NULL if CMOS table contains no such entry.
962  *
963  * Possible values for 'tag' are as follows:
964  *
965  *     LB_TAG_OPTION:      The entry represents a CMOS parameter.
966  *     LB_TAG_OPTION_ENUM: The entry represents a possible value for a CMOS
967  *                         parameter of type 'enum'.
968  *
969  * The CMOS table tells us where in the nonvolatile RAM to look for CMOS
970  * parameter values and specifies their types as 'enum', 'hex', or
971  * 'reserved'.
972  ****************************************************************************/
973 static const struct lb_record * first_cmos_rec (uint32_t tag)
974  { const char             *p;
975    uint32_t               bytes_processed, bytes_for_entries;
976    const struct lb_record *lbrec;
977
978    p = ((const char *) cmos_table) + cmos_table->header_length;
979    bytes_for_entries = cmos_table->size - cmos_table->header_length;
980
981    for (bytes_processed = 0;
982         bytes_processed < bytes_for_entries;
983         bytes_processed += lbrec->size)
984     { lbrec = (const struct lb_record *) &p[bytes_processed];
985
986       if (lbrec->tag == tag)
987          return lbrec;
988     }
989
990    return NULL;
991  }
992
993 /****************************************************************************
994  * next_cmos_rec
995  *
996  * Return a pointer to the next entry after 'last' in the CMOS table whose
997  * type matches 'tag'.  Return NULL if the table contains no more entries of
998  * this type.
999  ****************************************************************************/
1000 static const struct lb_record * next_cmos_rec (const struct lb_record *last,
1001                                                uint32_t tag)
1002  { const char *p;
1003    uint32_t bytes_processed, bytes_for_entries, last_offset;
1004    const struct lb_record *lbrec;
1005
1006    p = ((const char *) cmos_table) + cmos_table->header_length;
1007    bytes_for_entries = cmos_table->size - cmos_table->header_length;
1008    last_offset = ((const char *) last) - p;
1009
1010    for (bytes_processed = last_offset + last->size;
1011         bytes_processed < bytes_for_entries;
1012         bytes_processed += lbrec->size)
1013     { lbrec = (const struct lb_record *) &p[bytes_processed];
1014
1015       if (lbrec->tag == tag)
1016          return lbrec;
1017     }
1018
1019    return NULL;
1020  }
1021
1022 /****************************************************************************
1023  * memory_print_fn
1024  *
1025  * Display function for 'memory' item of coreboot table.
1026  ****************************************************************************/
1027 static void memory_print_fn (const struct lb_record *rec)
1028  { char start_str[19], end_str[19], size_str[19];
1029    const struct lb_memory *p;
1030    const char *mem_type;
1031    const struct lb_memory_range *ranges;
1032    uint64_t size, start, end;
1033    int i, entries;
1034
1035    p = (const struct lb_memory *) rec;
1036    entries = (p->size -  sizeof(*p)) / sizeof(p->map[0]);
1037    ranges = p->map;
1038
1039    if (entries == 0)
1040     { printf("No memory ranges were found.\n");
1041       return;
1042     }
1043
1044    for (i = 0; ; )
1045     { switch (ranges[i].type)
1046        { case LB_MEM_RAM:
1047             mem_type = "AVAILABLE";
1048             break;
1049
1050          case LB_MEM_RESERVED:
1051             mem_type = "RESERVED";
1052             break;
1053
1054          case LB_MEM_TABLE:
1055             mem_type = "CONFIG_TABLE";
1056             break;
1057
1058          default:
1059             mem_type = "UNKNOWN";
1060             break;
1061        }
1062
1063       size = unpack_lb64(ranges[i].size);
1064       start = unpack_lb64(ranges[i].start);
1065       end  = start + size - 1;
1066       uint64_to_hex_string(start_str, start);
1067       uint64_to_hex_string(end_str, end);
1068       uint64_to_hex_string(size_str, size);
1069       printf("%s memory:\n"
1070              "    from physical addresses %s to %s\n"
1071              "    size is %s bytes (%lld in decimal)\n",
1072              mem_type, start_str, end_str, size_str,
1073              (unsigned long long) size);
1074
1075       if (++i >= entries)
1076          break;
1077
1078       printf("\n");
1079     }
1080  }
1081
1082 /****************************************************************************
1083  * mainboard_print_fn
1084  *
1085  * Display function for 'mainboard' item of coreboot table.
1086  ****************************************************************************/
1087 static void mainboard_print_fn (const struct lb_record *rec)
1088  { const struct lb_mainboard *p;
1089
1090    p = (const struct lb_mainboard *) rec;
1091    printf("Vendor:      %s\n"
1092           "Part number: %s\n",
1093           &p->strings[p->vendor_idx],
1094           &p->strings[p->part_number_idx]);
1095  }
1096
1097 /****************************************************************************
1098  * cmos_opt_table_print_fn
1099  *
1100  * Display function for 'cmos_opt_table' item of coreboot table.
1101  ****************************************************************************/
1102 static void cmos_opt_table_print_fn (const struct lb_record *rec)
1103  {
1104    const struct cmos_option_table *p;
1105    const struct lb_record *cmos_item;
1106    uint32_t bytes_processed, bytes_for_entries;
1107    const char *q;
1108
1109    p = (const struct cmos_option_table *) rec;
1110    q = ((const char *) p) + p->header_length;
1111    bytes_for_entries = p->size - p->header_length;
1112
1113    printf("CMOS option table at physical address 0x%lx:\n"
1114           "    tag:           0x%x (decimal: %d)\n"
1115           "    size:          0x%x (decimal: %d)\n"
1116           "    header_length: 0x%x (decimal: %d)\n\n",
1117           vtophys(p), p->tag, p->tag, p->size, p->size, p->header_length,
1118           p->header_length);
1119
1120    if (p->header_length > p->size)
1121     { printf("Header length for CMOS option table is greater than the size "
1122              "of the entire table including header!!!\n");
1123       return;
1124     }
1125
1126    if (bytes_for_entries == 0)
1127     { printf("The CMOS option table is empty!!!\n");
1128       return;
1129     }
1130
1131    for (bytes_processed = 0; ; )
1132     { cmos_item = (const struct lb_record *) &q[bytes_processed];
1133
1134       switch (cmos_item->tag)
1135        { case LB_TAG_OPTION:
1136             print_option_record((const struct cmos_entries *) cmos_item);
1137             break;
1138
1139          case LB_TAG_OPTION_ENUM:
1140             print_enum_record((const struct cmos_enums *) cmos_item);
1141             break;
1142
1143          case LB_TAG_OPTION_DEFAULTS:
1144             print_defaults_record((const struct cmos_defaults *) cmos_item);
1145             break;
1146
1147          default:
1148             print_unknown_record(cmos_item);
1149             break;
1150        }
1151
1152       bytes_processed += cmos_item->size;
1153
1154       if (bytes_processed >= bytes_for_entries)
1155          break;
1156
1157       printf("\n");
1158     }
1159  }
1160
1161 /****************************************************************************
1162  * print_option_record
1163  *
1164  * Display "option" record from CMOS option table.
1165  ****************************************************************************/
1166 static void print_option_record (const struct cmos_entries *cmos_entry)
1167  { static const size_t S_BUFSIZE = 80;
1168    char s[S_BUFSIZE];
1169
1170    switch (cmos_entry->config)
1171     { case 'e':
1172          strcpy(s, "ENUM");
1173          break;
1174
1175       case 'h':
1176          strcpy(s, "HEX");
1177          break;
1178
1179       case 'r':
1180          strcpy(s, "RESERVED");
1181          break;
1182
1183       default:
1184          snprintf(s, S_BUFSIZE, "UNKNOWN: value is 0x%x (decimal: %d)",
1185                   cmos_entry->config, cmos_entry->config);
1186          break;
1187     }
1188
1189    printf("    OPTION record at physical address 0x%lx:\n"
1190           "        tag:       0x%x (decimal: %d)\n"
1191           "        size:      0x%x (decimal: %d)\n"
1192           "        bit:       0x%x (decimal: %d)\n"
1193           "        length:    0x%x (decimal: %d)\n"
1194           "        config:    %s\n"
1195           "        config_id: 0x%x (decimal: %d)\n"
1196           "        name:      %s\n",
1197           vtophys(cmos_entry), cmos_entry->tag, cmos_entry->tag,
1198           cmos_entry->size, cmos_entry->size, cmos_entry->bit,
1199           cmos_entry->bit, cmos_entry->length, cmos_entry->length, s,
1200           cmos_entry->config_id, cmos_entry->config_id, cmos_entry->name);
1201  }
1202
1203 /****************************************************************************
1204  * print_enum_record
1205  *
1206  * Display "enum" record from CMOS option table.
1207  ****************************************************************************/
1208 static void print_enum_record (const struct cmos_enums *cmos_enum)
1209  { printf("    ENUM record at physical address 0x%lx:\n"
1210           "        tag:       0x%x (decimal: %d)\n"
1211           "        size:      0x%x (decimal: %d)\n"
1212           "        config_id: 0x%x (decimal: %d)\n"
1213           "        value:     0x%x (decimal: %d)\n"
1214           "        text:      %s\n",
1215           vtophys(cmos_enum), cmos_enum->tag, cmos_enum->tag, cmos_enum->size,
1216           cmos_enum->size, cmos_enum->config_id, cmos_enum->config_id,
1217           cmos_enum->value, cmos_enum->value, cmos_enum->text);
1218  }
1219
1220 /****************************************************************************
1221  * print_defaults_record
1222  *
1223  * Display "defaults" record from CMOS option table.
1224  ****************************************************************************/
1225 static void print_defaults_record (const struct cmos_defaults *cmos_defaults)
1226  { printf("    DEFAULTS record at physical address 0x%lx:\n"
1227           "        tag:         0x%x (decimal: %d)\n"
1228           "        size:        0x%x (decimal: %d)\n"
1229           "        name_length: 0x%x (decimal: %d)\n"
1230           "        name:        %s\n"
1231           "        default_set:\n",
1232           vtophys(cmos_defaults), cmos_defaults->tag, cmos_defaults->tag,
1233           cmos_defaults->size, cmos_defaults->size,
1234           cmos_defaults->name_length, cmos_defaults->name_length,
1235           cmos_defaults->name);
1236    hexdump(cmos_defaults->default_set, CMOS_IMAGE_BUFFER_SIZE,
1237            vtophys(cmos_defaults->default_set), stdout, &format);
1238  }
1239
1240 /****************************************************************************
1241  * print_unknown_record
1242  *
1243  * Display record of unknown type from CMOS option table.
1244  ****************************************************************************/
1245 static void print_unknown_record (const struct lb_record *cmos_item)
1246  { const char *data;
1247
1248    printf("    UNKNOWN record at physical address 0x%lx:\n"
1249           "        tag:  0x%x (decimal: %d)\n"
1250           "        size: 0x%x (decimal: %d)\n"
1251           "        data:\n",
1252           vtophys(cmos_item), cmos_item->tag, cmos_item->tag,
1253           cmos_item->size, cmos_item->size);
1254    data = ((const char *) cmos_item) + sizeof(*cmos_item);
1255    hexdump(data, cmos_item->size - sizeof(*cmos_item), vtophys(data), stdout,
1256            &format);
1257  }
1258
1259 /****************************************************************************
1260  * option_checksum_print_fn
1261  *
1262  * Display function for 'option_checksum' item of coreboot table.
1263  ****************************************************************************/
1264 static void option_checksum_print_fn (const struct lb_record *rec)
1265  { struct cmos_checksum *p;
1266
1267    p = (struct cmos_checksum *) rec;
1268    printf("CMOS checksum from bit %d to bit %d\n"
1269           "at position %d is type %s.\n",
1270           p->range_start, p->range_end, p->location,
1271           (p->type == CHECKSUM_PCBIOS) ? "PC BIOS" : "NONE");
1272  }
1273
1274 /****************************************************************************
1275  * string_print_fn
1276  *
1277  * Display function for a generic item of coreboot table that simply
1278  * consists of a string.
1279  ****************************************************************************/
1280 static void string_print_fn (const struct lb_record *rec)
1281  { const struct lb_string *p;
1282
1283    p = (const struct lb_string *) rec;
1284    printf("%s\n", p->string);
1285  }
1286
1287 /****************************************************************************
1288  * uint64_to_hex_string
1289  *
1290  * Convert the 64-bit integer 'n' to its hexadecimal string representation,
1291  * storing the result in 's'.  's' must point to a buffer at least 19 bytes
1292  * long.  The result is displayed with as many leading zeros as needed to
1293  * make a 16-digit hex number including a 0x prefix (example: the number 1
1294  * will be displayed as "0x0000000000000001").
1295  ****************************************************************************/
1296 static void uint64_to_hex_string (char str[], uint64_t n)
1297  { int chars_printed;
1298
1299    str[0] = '0';
1300    str[1] = 'x';
1301
1302    /* Print the result right-justified with leading spaces in a
1303     * 16-character field. */
1304    chars_printed = sprintf(&str[2], "%016llx", (unsigned long long) n);
1305    assert(chars_printed == 16);
1306  }