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