summaryrefslogtreecommitdiff
path: root/util/nvramtool/lbtable.c
blob: 2ce43c50f9f759699058480f6a37d1963c0ef074 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/*****************************************************************************\
 * lbtable.c
 *****************************************************************************
 *  Copyright (C) 2002-2005 The Regents of the University of California.
 *  Produced at the Lawrence Livermore National Laboratory.
 *  Written by Dave Peterson <dsp@llnl.gov> <dave_peterson@pobox.com>
 *  and Stefan Reinauer <stepan@openbios.org>.
 *  UCRL-CODE-2003-012
 *  All rights reserved.
 *
 *  This file is part of nvramtool, a utility for reading/writing coreboot
 *  parameters and displaying information from the coreboot table.
 *  For details, see http://coreboot.org/nvramtool.
 *
 *  Please also read the file DISCLAIMER which is included in this software
 *  distribution.
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License (as published by the
 *  Free Software Foundation) version 2, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the IMPLIED WARRANTY OF
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the terms and
 *  conditions of the GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
\*****************************************************************************/

#include <arpa/inet.h>
#include <string.h>
#include <sys/mman.h>
#include "common.h"
#include "coreboot_tables.h"
#include "ip_checksum.h"
#include "lbtable.h"
#include "layout.h"
#include "cmos_lowlevel.h"
#include "hexdump.h"
#include "cbfs.h"

typedef void (*lbtable_print_fn_t) (const struct lb_record * rec);

/* This structure represents an item in the coreboot table that may be
 * displayed using the -l option.
 */
typedef struct {
	uint32_t tag;
	const char *name;
	const char *description;
	const char *nofound_msg;
	lbtable_print_fn_t print_fn;
} lbtable_choice_t;

typedef struct {
	unsigned long start;	/* address of first byte of memory range */
	unsigned long end;	/* address of last byte of memory range */
} mem_range_t;

static const struct lb_header *lbtable_scan(unsigned long start,
					    unsigned long end,
					    int *bad_header_count,
					    int *bad_table_count);
static const struct lb_record *find_lbrec(uint32_t tag);
static const char *lbrec_tag_to_str(uint32_t tag);
static void memory_print_fn(const struct lb_record *rec);
static void mainboard_print_fn(const struct lb_record *rec);
static void cmos_opt_table_print_fn(const struct lb_record *rec);
static void print_option_record(const struct cmos_entries *cmos_entry);
static void print_enum_record(const struct cmos_enums *cmos_enum);
static void print_defaults_record(const struct cmos_defaults *cmos_defaults);
static void print_unknown_record(const struct lb_record *cmos_item);
static void option_checksum_print_fn(const struct lb_record *rec);
static void string_print_fn(const struct lb_record *rec);

static const char memory_desc[] =
    "    This shows information about system memory.\n";

static const char mainboard_desc[] =
    "    This shows information about your mainboard.\n";

static const char version_desc[] =
    "    This shows coreboot version information.\n";

static const char extra_version_desc[] =
    "    This shows extra coreboot version information.\n";

static const char build_desc[] = "    This shows coreboot build information.\n";

static const char compile_time_desc[] =
    "    This shows when coreboot was compiled.\n";

static const char compile_by_desc[] = "    This shows who compiled coreboot.\n";

static const char compile_host_desc[] =
    "    This shows the name of the machine that compiled coreboot.\n";

static const char compile_domain_desc[] =
    "    This shows the domain name of the machine that compiled coreboot.\n";

static const char compiler_desc[] =
    "    This shows the name of the compiler used to build coreboot.\n";

static const char linker_desc[] =
    "    This shows the name of the linker used to build coreboot.\n";

static const char assembler_desc[] =
    "    This shows the name of the assembler used to build coreboot.\n";

static const char cmos_opt_table_desc[] =
    "    This does a low-level dump of the CMOS option table.  The table "
    "contains\n"
    "    information about the layout of the values that coreboot stores in\n"
    "    nonvolatile RAM.\n";

static const char option_checksum_desc[] =
    "    This shows the location of the CMOS checksum and the area over which it "
    "is\n" "    calculated.\n";

static const char generic_nofound_msg[] =
    "%s: Item %s not found in coreboot table.\n";

static const char nofound_msg_cmos_opt_table[] =
    "%s: Item %s not found in coreboot table.  Apparently, the "
    "coreboot installed on this system was built without specifying "
    "CONFIG_HAVE_OPTION_TABLE.\n";

static const char nofound_msg_option_checksum[] =
    "%s: Item %s not found in coreboot table. Apparently, you are "
    "using coreboot v1.\n";

int fd;

/* This is the number of items from the coreboot table that may be displayed
 * using the -l option.
 */
#define NUM_LBTABLE_CHOICES 14

/* These represent the various items from the coreboot table that may be
 * displayed using the -l option.
 */
static const lbtable_choice_t lbtable_choices[NUM_LBTABLE_CHOICES] =
    { {LB_TAG_MEMORY, "memory",
       memory_desc, generic_nofound_msg,
       memory_print_fn},
{LB_TAG_MAINBOARD, "mainboard",
 mainboard_desc, generic_nofound_msg,
 mainboard_print_fn},
{LB_TAG_VERSION, "version",
 version_desc, generic_nofound_msg,
 string_print_fn},
{LB_TAG_EXTRA_VERSION, "extra_version",
 extra_version_desc, generic_nofound_msg,
 string_print_fn},
{LB_TAG_BUILD, "build",
 build_desc, generic_nofound_msg,
 string_print_fn},
{LB_TAG_COMPILE_TIME, "compile_time",
 compile_time_desc, generic_nofound_msg,
 string_print_fn},
{LB_TAG_COMPILE_BY, "compile_by",
 compile_by_desc, generic_nofound_msg,
 string_print_fn},
{LB_TAG_COMPILE_HOST, "compile_host",
 compile_host_desc, generic_nofound_msg,
 string_print_fn},
{LB_TAG_COMPILE_DOMAIN, "compile_domain",
 compile_domain_desc, generic_nofound_msg,
 string_print_fn},
{LB_TAG_COMPILER, "compiler",
 compiler_desc, generic_nofound_msg,
 string_print_fn},
{LB_TAG_LINKER, "linker",
 linker_desc, generic_nofound_msg,
 string_print_fn},
{LB_TAG_ASSEMBLER, "assembler",
 assembler_desc, generic_nofound_msg,
 string_print_fn},
{LB_TAG_CMOS_OPTION_TABLE, "cmos_opt_table",
 cmos_opt_table_desc, nofound_msg_cmos_opt_table,
 cmos_opt_table_print_fn},
{LB_TAG_OPTION_CHECKSUM, "option_checksum",
 option_checksum_desc, nofound_msg_option_checksum,
 option_checksum_print_fn}
};

/* The coreboot table resides in low physical memory, which we access using
 * /dev/mem.  These are ranges of physical memory that should be scanned for a
 * coreboot table.
 */

#define NUM_MEM_RANGES 2

static const mem_range_t mem_ranges[NUM_MEM_RANGES] =
    { {0x00000000, 0x00000fff},
{0x000f0000, 0x000fffff}
};

/* This is the number of bytes of physical memory to map, starting at physical
 * address 0.  This value must be large enough to contain all memory ranges
 * specified in mem_ranges above plus the maximum possible size of the
 * coreboot table (since the start of the table could potentially occur at
 * the end of the last memory range).
 */
static const size_t BYTES_TO_MAP = (1024 * 1024);

/* Pointer to low physical memory that we access by calling mmap() on
 * /dev/mem.
 */
static const void *low_phys_mem;
static unsigned long low_phys_base = 0;

/* Pointer to coreboot table. */
static const struct lb_header *lbtable = NULL;

/* The CMOS option table is located within the coreboot table.  It tells us
 * where the CMOS parameters are located in the nonvolatile RAM.
 */
static const struct cmos_option_table *cmos_table = NULL;

static const hexdump_format_t format =
    { 12, 4, "            ", " | ", " ", " | ", '.' };

/****************************************************************************
 * vtophys
 *
 * Convert a virtual address to a physical address.  'vaddr' is a virtual
 * address in the address space of the current process.  It points to
 * somewhere in the chunk of memory that we mapped by calling mmap() on
 * /dev/mem.  This macro converts 'vaddr' to a physical address.
 ****************************************************************************/
#define vtophys(vaddr) (((unsigned long) vaddr) -       \
                        ((unsigned long) low_phys_mem) + low_phys_base)

/****************************************************************************
 * phystov
 *
 * Convert a physical address to a virtual address.  'paddr' is a physical
 * address.  This macro converts 'paddr' to a virtual address in the address
 * space of the current process.  The virtual to physical mapping was set up
 * by calling mmap() on /dev/mem.
 ****************************************************************************/
#define phystov(paddr) (((unsigned long) low_phys_mem) + \
                        ((unsigned long) paddr) - low_phys_base)

/****************************************************************************
 * get_lbtable
 *
 * Find the coreboot table and set global variable lbtable to point to it.
 ****************************************************************************/
void get_lbtable(void)
{
	int i, bad_header_count, bad_table_count, bad_headers, bad_tables;

	if (lbtable != NULL)
		return;

	/* The coreboot table is located in low physical memory, which may be
	 * conveniently accessed by calling mmap() on /dev/mem.
	 */

	if ((fd = open("/dev/mem", O_RDONLY, 0)) < 0) {
		fprintf(stderr, "%s: Can not open /dev/mem for reading: %s\n",
			prog_name, strerror(errno));
		exit(1);
	}

	if ((low_phys_mem =
	     mmap(NULL, BYTES_TO_MAP, PROT_READ, MAP_SHARED, fd, 0))
	    == MAP_FAILED) {
		fprintf(stderr, "%s: Failed to mmap /dev/mem: %s\n", prog_name,
			strerror(errno));
		exit(1);
	}

	bad_header_count = 0;
	bad_table_count = 0;

	for (i = 0; i < NUM_MEM_RANGES; i++) {
		lbtable = lbtable_scan(phystov(mem_ranges[i].start),
				       phystov(mem_ranges[i].end),
				       &bad_headers, &bad_tables);

		if (lbtable != NULL)
			return;	/* success: we found it! */

		bad_header_count += bad_headers;
		bad_table_count += bad_tables;
	}

	fprintf(stderr,
		"%s: coreboot table not found.  coreboot does not appear to\n"
		"        be installed on this system.  Scanning for the table "
		"produced the\n"
		"        following results:\n\n"
		"            %d valid signatures were found with bad header "
		"checksums.\n"
		"            %d valid headers were found with bad table "
		"checksums.\n", prog_name, bad_header_count, bad_table_count);
	exit(1);
}

/****************************************************************************
 * get_layout_from_cmos_table
 *
 * Find the CMOS table which is stored within the coreboot table and set the
 * global variable cmos_table to point to it.
 ****************************************************************************/
void get_layout_from_cmos_table(void)
{

	get_lbtable();
	cmos_table = (const struct cmos_option_table *)
	    find_lbrec(LB_TAG_CMOS_OPTION_TABLE);
	process_layout();
}

/****************************************************************************
 * dump_lbtable
 *
 * Do a low-level dump of the coreboot table.
 ****************************************************************************/
void dump_lbtable(void)
{
	const char *p, *data;
	uint32_t bytes_processed;
	const struct lb_record *lbrec;

	p = ((const char *)lbtable) + lbtable->header_bytes;
	printf("Coreboot table at physical address 0x%lx:\n"
	       "    signature:       0x%x (ASCII: %c%c%c%c)\n"
	       "    header_bytes:    0x%x (decimal: %d)\n"
	       "    header_checksum: 0x%x (decimal: %d)\n"
	       "    table_bytes:     0x%x (decimal: %d)\n"
	       "    table_checksum:  0x%x (decimal: %d)\n"
	       "    table_entries:   0x%x (decimal: %d)\n\n",
	       vtophys(lbtable), lbtable->signature32,
	       lbtable->signature[0], lbtable->signature[1],
	       lbtable->signature[2], lbtable->signature[3],
	       lbtable->header_bytes, lbtable->header_bytes,
	       lbtable->header_checksum, lbtable->header_checksum,
	       lbtable->table_bytes, lbtable->table_bytes,
	       lbtable->table_checksum, lbtable->table_checksum,
	       lbtable->table_entries, lbtable->table_entries);

	if ((lbtable->table_bytes == 0) != (lbtable->table_entries == 0)) {
		printf
		    ("Inconsistent values for table_bytes and table_entries!!!\n"
		     "They should be either both 0 or both nonzero.\n");
		return;
	}

	if (lbtable->table_bytes == 0) {
		printf("The coreboot table is empty!!!\n");
		return;
	}

	for (bytes_processed = 0;;) {
		lbrec = (const struct lb_record *)&p[bytes_processed];
		printf("    %s record at physical address 0x%lx:\n"
		       "        tag:  0x%x (decimal: %d)\n"
		       "        size: 0x%x (decimal: %d)\n"
		       "        data:\n",
		       lbrec_tag_to_str(lbrec->tag), vtophys(lbrec), lbrec->tag,
		       lbrec->tag, lbrec->size, lbrec->size);

		data = ((const char *)lbrec) + sizeof(*lbrec);
		hexdump(data, lbrec->size - sizeof(*lbrec), vtophys(data),
			stdout, &format);

		bytes_processed += lbrec->size;

		if (bytes_processed >= lbtable->table_bytes)
			break;

		printf("\n");
	}
}

/****************************************************************************
 * list_lbtable_choices
 *
 * List names and informational blurbs for items from the coreboot table
 * that may be displayed using the -l option.
 ****************************************************************************/
void list_lbtable_choices(void)
{
	int i;

	for (i = 0;;) {
		printf("%s:\n%s",
		       lbtable_choices[i].name, lbtable_choices[i].description);

		if (++i >= NUM_LBTABLE_CHOICES)
			break;

		printf("\n");
	}
}

/****************************************************************************
 * list_lbtable_item
 *
 * Show the coreboot table item specified by 'item'.
 ****************************************************************************/
void list_lbtable_item(const char item[])
{
	int i;
	const struct lb_record *rec;

	for (i = 0; i < NUM_LBTABLE_CHOICES; i++) {
		if (strcmp(item, lbtable_choices[i].name) == 0)
			break;
	}

	if (i == NUM_LBTABLE_CHOICES) {
		fprintf(stderr, "%s: Invalid coreboot table item %s.\n",
			prog_name, item);
		exit(1);
	}

	if ((rec = find_lbrec(lbtable_choices[i].tag)) == NULL) {
		fprintf(stderr, lbtable_choices[i].nofound_msg, prog_name,
			lbtable_choices[i].name);
		exit(1);
	}

	lbtable_choices[i].print_fn(rec);
}

/****************************************************************************
 * lbtable_scan
 *
 * Scan the chunk of memory specified by 'start' and 'end' for a coreboot
 * table.  The first 4 bytes of the table are marked by the signature
 * { 'L', 'B', 'I', 'O' }.  'start' and 'end' indicate the addresses of the
 * first and last bytes of the chunk of memory to be scanned.  For instance,
 * values of 0x10000000 and 0x1000ffff for 'start' and 'end' specify a 64k
 * chunk of memory starting at address 0x10000000.  'start' and 'end' are
 * virtual addresses in the address space of the current process.  They
 * represent a chunk of memory obtained by calling mmap() on /dev/mem.
 *
 * If a coreboot table is found, return a pointer to it.  Otherwise return
 * NULL.  On return, *bad_header_count and *bad_table_count are set as
 * follows:
 *
 *     *bad_header_count:
 *         Indicates the number of times in which a valid signature was found
 *         but the header checksum was invalid.
 *
 *     *bad_table_count:
 *         Indicates the number of times in which a header with a valid
 *         checksum was found but the table checksum was invalid.
 ****************************************************************************/
static const struct lb_header *lbtable_scan(unsigned long start,
					    unsigned long end,
					    int *bad_header_count,
					    int *bad_table_count)
{
	static const char signature[4] = { 'L', 'B', 'I', 'O' };
	const struct lb_header *table;
	const struct lb_forward *forward;
	const uint32_t *p;
	uint32_t sig;

	assert(end >= start);
	memcpy(&sig, signature, sizeof(sig));
	table = NULL;
	*bad_header_count = 0;
	*bad_table_count = 0;

	/* Look for signature.  Table is aligned on 16-byte boundary.  Therefore
	 * only check every fourth 32-bit memory word.  As the loop is coded below,
	 * this function will behave in a reasonable manner for ALL possible values
	 * for 'start' and 'end': even weird boundary cases like 0x00000000 and
	 * 0xffffffff on a 32-bit architecture.
	 */
	for (p = (const uint32_t *)start;
	     (((unsigned long)p) <= end) &&
	     ((end - (unsigned long)p) >= (sizeof(uint32_t) - 1)); p += 4) {
		if (*p != sig)
			continue;

		/* We found a valid signature. */
		table = (const struct lb_header *)p;

		/* validate header checksum */
		if (compute_ip_checksum((void *)table, sizeof(*table))) {
			(*bad_header_count)++;
			continue;
		}

		/* validate table checksum */
		if (table->table_checksum !=
		    compute_ip_checksum(((char *)table) + sizeof(*table),
					table->table_bytes)) {
			(*bad_table_count)++;
			continue;
		}

		/* checksums are ok: we found it! */
		/* But it may just be a forwarding table, so look if there's a forwarder */
		lbtable = table;
		forward = (struct lb_forward *)find_lbrec(LB_TAG_FORWARD);
		lbtable = NULL;

		if (forward) {
			uint64_t new_phys = forward->forward;

			new_phys &= ~(getpagesize() - 1);

			munmap((void *)low_phys_mem, BYTES_TO_MAP);
			if ((low_phys_mem =
			     mmap(NULL, BYTES_TO_MAP, PROT_READ, MAP_SHARED, fd,
				  (off_t) new_phys)) == MAP_FAILED) {
				fprintf(stderr,
					"%s: Failed to mmap /dev/mem: %s\n",
					prog_name, strerror(errno));
				exit(1);
			}
			low_phys_base = new_phys;
			table =
			    lbtable_scan(phystov(low_phys_base),
					 phystov(low_phys_base + BYTES_TO_MAP),
					 bad_header_count, bad_table_count);
		}
		return table;
	}

	return NULL;
}

/****************************************************************************
 * find_lbrec
 *
 * Find the record in the coreboot table that matches 'tag'.  Return pointer
 * to record on success or NULL if record not found.
 ****************************************************************************/
static const struct lb_record *find_lbrec(uint32_t tag)
{
	const char *p;
	uint32_t bytes_processed;
	const struct lb_record *lbrec;

	p = ((const char *)lbtable) + lbtable->header_bytes;

	for (bytes_processed = 0;
	     bytes_processed < lbtable->table_bytes;
	     bytes_processed += lbrec->size) {
		lbrec = (const struct lb_record *)&p[bytes_processed];

		if (lbrec->tag == tag)
			return lbrec;
	}

	return NULL;
}

/****************************************************************************
 * lbrec_tag_to_str
 *
 * Return a pointer to the string representation of the given coreboot table
 * tag.
 ****************************************************************************/
static const char *lbrec_tag_to_str(uint32_t tag)
{
	switch (tag) {
	case LB_TAG_UNUSED:
		return "UNUSED";

	case LB_TAG_MEMORY:
		return "MEMORY";

	case LB_TAG_HWRPB:
		return "HWRPB";

	case LB_TAG_MAINBOARD:
		return "MAINBOARD";

	case LB_TAG_VERSION:
		return "VERSION";

	case LB_TAG_EXTRA_VERSION:
		return "EXTRA_VERSION";

	case LB_TAG_BUILD:
		return "BUILD";

	case LB_TAG_COMPILE_TIME:
		return "COMPILE_TIME";

	case LB_TAG_COMPILE_BY:
		return "COMPILE_BY";

	case LB_TAG_COMPILE_HOST:
		return "COMPILE_HOST";

	case LB_TAG_COMPILE_DOMAIN:
		return "COMPILE_DOMAIN";

	case LB_TAG_COMPILER:
		return "COMPILER";

	case LB_TAG_LINKER:
		return "LINKER";

	case LB_TAG_ASSEMBLER:
		return "ASSEMBLER";

	case LB_TAG_SERIAL:
		return "SERIAL";

	case LB_TAG_CONSOLE:
		return "CONSOLE";

	case LB_TAG_FORWARD:
		return "FORWARD";

	case LB_TAG_CMOS_OPTION_TABLE:
		return "CMOS_OPTION_TABLE";

	case LB_TAG_OPTION_CHECKSUM:
		return "OPTION_CHECKSUM";

	default:
		break;
	}

	return "UNKNOWN";
}

/****************************************************************************
 * memory_print_fn
 *
 * Display function for 'memory' item of coreboot table.
 ****************************************************************************/
static void memory_print_fn(const struct lb_record *rec)
{
	const struct lb_memory *p;
	const char *mem_type;
	const struct lb_memory_range *ranges;
	uint64_t size, start, end;
	int i, entries;

	p = (const struct lb_memory *)rec;
	entries = (p->size - sizeof(*p)) / sizeof(p->map[0]);
	ranges = p->map;

	if (entries == 0) {
		printf("No memory ranges were found.\n");
		return;
	}

	for (i = 0;;) {
		switch (ranges[i].type) {
		case LB_MEM_RAM:
			mem_type = "AVAILABLE";
			break;

		case LB_MEM_RESERVED:
			mem_type = "RESERVED";
			break;

		case LB_MEM_TABLE:
			mem_type = "CONFIG_TABLE";
			break;

		default:
			mem_type = "UNKNOWN";
			break;
		}

		size = unpack_lb64(ranges[i].size);
		start = unpack_lb64(ranges[i].start);
		end = start + size - 1;
		printf("%s memory:\n"
		       "    from physical addresses 0x%016llx to 0x%016llx\n"
		       "    size is 0x%016llx bytes (%lld in decimal)\n",
		       mem_type, start, end, size,
		       (unsigned long long)size);

		if (++i >= entries)
			break;

		printf("\n");
	}
}

/****************************************************************************
 * mainboard_print_fn
 *
 * Display function for 'mainboard' item of coreboot table.
 ****************************************************************************/
static void mainboard_print_fn(const struct lb_record *rec)
{
	const struct lb_mainboard *p;

	p = (const struct lb_mainboard *)rec;
	printf("Vendor:      %s\n"
	       "Part number: %s\n",
	       &p->strings[p->vendor_idx], &p->strings[p->part_number_idx]);
}

/****************************************************************************
 * cmos_opt_table_print_fn
 *
 * Display function for 'cmos_opt_table' item of coreboot table.
 ****************************************************************************/
static void cmos_opt_table_print_fn(const struct lb_record *rec)
{
	const struct cmos_option_table *p;
	const struct lb_record *cmos_item;
	uint32_t bytes_processed, bytes_for_entries;
	const char *q;

	p = (const struct cmos_option_table *)rec;
	q = ((const char *)p) + p->header_length;
	bytes_for_entries = p->size - p->header_length;

	printf("CMOS option table at physical address 0x%lx:\n"
	       "    tag:           0x%x (decimal: %d)\n"
	       "    size:          0x%x (decimal: %d)\n"
	       "    header_length: 0x%x (decimal: %d)\n\n",
	       vtophys(p), p->tag, p->tag, p->size, p->size, p->header_length,
	       p->header_length);

	if (p->header_length > p->size) {
		printf
		    ("Header length for CMOS option table is greater than the size "
		     "of the entire table including header!!!\n");
		return;
	}

	if (bytes_for_entries == 0) {
		printf("The CMOS option table is empty!!!\n");
		return;
	}

	for (bytes_processed = 0;;) {
		cmos_item = (const struct lb_record *)&q[bytes_processed];

		switch (cmos_item->tag) {
		case LB_TAG_OPTION:
			print_option_record((const struct cmos_entries *)
					    cmos_item);
			break;

		case LB_TAG_OPTION_ENUM:
			print_enum_record((const struct cmos_enums *)cmos_item);
			break;

		case LB_TAG_OPTION_DEFAULTS:
			print_defaults_record((const struct cmos_defaults *)
					      cmos_item);
			break;

		default:
			print_unknown_record(cmos_item);
			break;
		}

		bytes_processed += cmos_item->size;

		if (bytes_processed >= bytes_for_entries)
			break;

		printf("\n");
	}
}

/****************************************************************************
 * print_option_record
 *
 * Display "option" record from CMOS option table.
 ****************************************************************************/
static void print_option_record(const struct cmos_entries *cmos_entry)
{
	static const size_t S_BUFSIZE = 80;
	char s[S_BUFSIZE];

	switch (cmos_entry->config) {
	case 'e':
		strcpy(s, "ENUM");
		break;

	case 'h':
		strcpy(s, "HEX");
		break;

	case 'r':
		strcpy(s, "RESERVED");
		break;

	default:
		snprintf(s, S_BUFSIZE, "UNKNOWN: value is 0x%x (decimal: %d)",
			 cmos_entry->config, cmos_entry->config);
		break;
	}

	printf("    OPTION record at physical address 0x%lx:\n"
	       "        tag:       0x%x (decimal: %d)\n"
	       "        size:      0x%x (decimal: %d)\n"
	       "        bit:       0x%x (decimal: %d)\n"
	       "        length:    0x%x (decimal: %d)\n"
	       "        config:    %s\n"
	       "        config_id: 0x%x (decimal: %d)\n"
	       "        name:      %s\n",
	       vtophys(cmos_entry), cmos_entry->tag, cmos_entry->tag,
	       cmos_entry->size, cmos_entry->size, cmos_entry->bit,
	       cmos_entry->bit, cmos_entry->length, cmos_entry->length, s,
	       cmos_entry->config_id, cmos_entry->config_id, cmos_entry->name);
}

/****************************************************************************
 * print_enum_record
 *
 * Display "enum" record from CMOS option table.
 ****************************************************************************/
static void print_enum_record(const struct cmos_enums *cmos_enum)
{
	printf("    ENUM record at physical address 0x%lx:\n"
	       "        tag:       0x%x (decimal: %d)\n"
	       "        size:      0x%x (decimal: %d)\n"
	       "        config_id: 0x%x (decimal: %d)\n"
	       "        value:     0x%x (decimal: %d)\n"
	       "        text:      %s\n",
	       vtophys(cmos_enum), cmos_enum->tag, cmos_enum->tag,
	       cmos_enum->size, cmos_enum->size, cmos_enum->config_id,
	       cmos_enum->config_id, cmos_enum->value, cmos_enum->value,
	       cmos_enum->text);
}

/****************************************************************************
 * print_defaults_record
 *
 * Display "defaults" record from CMOS option table.
 ****************************************************************************/
static void print_defaults_record(const struct cmos_defaults *cmos_defaults)
{
	printf("    DEFAULTS record at physical address 0x%lx:\n"
	       "        tag:         0x%x (decimal: %d)\n"
	       "        size:        0x%x (decimal: %d)\n"
	       "        name_length: 0x%x (decimal: %d)\n"
	       "        name:        %s\n"
	       "        default_set:\n",
	       vtophys(cmos_defaults), cmos_defaults->tag, cmos_defaults->tag,
	       cmos_defaults->size, cmos_defaults->size,
	       cmos_defaults->name_length, cmos_defaults->name_length,
	       cmos_defaults->name);
	hexdump(cmos_defaults->default_set, CMOS_IMAGE_BUFFER_SIZE,
		vtophys(cmos_defaults->default_set), stdout, &format);
}

/****************************************************************************
 * print_unknown_record
 *
 * Display record of unknown type from CMOS option table.
 ****************************************************************************/
static void print_unknown_record(const struct lb_record *cmos_item)
{
	const char *data;

	printf("    UNKNOWN record at physical address 0x%lx:\n"
	       "        tag:  0x%x (decimal: %d)\n"
	       "        size: 0x%x (decimal: %d)\n"
	       "        data:\n",
	       vtophys(cmos_item), cmos_item->tag, cmos_item->tag,
	       cmos_item->size, cmos_item->size);
	data = ((const char *)cmos_item) + sizeof(*cmos_item);
	hexdump(data, cmos_item->size - sizeof(*cmos_item), vtophys(data),
		stdout, &format);
}

/****************************************************************************
 * option_checksum_print_fn
 *
 * Display function for 'option_checksum' item of coreboot table.
 ****************************************************************************/
static void option_checksum_print_fn(const struct lb_record *rec)
{
	struct cmos_checksum *p;

	p = (struct cmos_checksum *)rec;
	printf("CMOS checksum from bit %d to bit %d\n"
	       "at position %d is type %s.\n",
	       p->range_start, p->range_end, p->location,
	       (p->type == CHECKSUM_PCBIOS) ? "PC BIOS" : "NONE");
}

/****************************************************************************
 * string_print_fn
 *
 * Display function for a generic item of coreboot table that simply
 * consists of a string.
 ****************************************************************************/
static void string_print_fn(const struct lb_record *rec)
{
	const struct lb_string *p;

	p = (const struct lb_string *)rec;
	printf("%s\n", p->string);
}