coreboot/util/lbtdump/lbtdump.c
<<
>>
Prefs
   1#include <stdio.h>
   2#include <stdint.h>
   3#include <stdlib.h>
   4#include <fcntl.h>
   5#include <unistd.h>
   6#include <sys/types.h>
   7#include <string.h>
   8#include <errno.h>
   9#include <sys/mman.h>
  10#include "../../src/include/boot/coreboot_tables.h"
  11
  12void print_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr);
  13
  14unsigned long compute_checksum(void *addr, unsigned long length)
  15{
  16        uint8_t *ptr;
  17        volatile union {
  18                uint8_t  byte[2];
  19                uint16_t word;
  20        } value;
  21        unsigned long sum;
  22        unsigned long i;
  23        /* In the most straight forward way possible,
  24         * compute an ip style checksum.
  25         */
  26        sum = 0;
  27        ptr = addr;
  28        for(i = 0; i < length; i++) {
  29                unsigned long value;
  30                value = ptr[i];
  31                if (i & 1) {
  32                        value <<= 8;
  33                }
  34                /* Add the new value */
  35                sum += value;
  36                /* Wrap around the carry */
  37                if (sum > 0xFFFF) {
  38                        sum = (sum + (sum >> 16)) & 0xFFFF;
  39                }
  40        }
  41        value.byte[0] = sum & 0xff;
  42        value.byte[1] = (sum >> 8) & 0xff;
  43        return (~value.word) & 0xFFFF;
  44}
  45
  46#define for_each_lbrec(head, rec) \
  47        for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
  48                (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes))  && \
  49                (rec->size >= 1) && \
  50                ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
  51                rec = (struct lb_record *)(((char *)rec) + rec->size))
  52
  53
  54static int  count_lb_records(struct lb_header *head)
  55{
  56        struct lb_record *rec;
  57        int count;
  58        count = 0;
  59        for_each_lbrec(head, rec) {
  60                count++;
  61        }
  62        return count;
  63}
  64
  65
  66struct lb_header *find_lb_table(void *base, unsigned long start, unsigned long end)
  67{
  68        unsigned long addr;
  69        /* For now be stupid.... */
  70        for(addr = start; addr < end; addr += 16) {
  71                struct lb_header *head = (struct lb_header *)(((char*)base) + addr);
  72                struct lb_record *recs = (struct lb_record *)(((char*)base) + addr + sizeof(*head));
  73                if (memcmp(head->signature, "LBIO", 4) != 0)
  74                        continue;
  75                fprintf(stdout, "Found candidate at: %08lx-%08lx\n",
  76                        addr, addr + head->table_bytes);
  77                if (head->header_bytes != sizeof(*head)) {
  78                        fprintf(stderr, "Header bytes of %d are incorrect\n",
  79                                head->header_bytes);
  80                        continue;
  81                }
  82                if (count_lb_records(head) != head->table_entries) {
  83                        fprintf(stderr, "bad record count: %d\n",
  84                                head->table_entries);
  85                        continue;
  86                }
  87                if (compute_checksum((unsigned char *)head, sizeof(*head)) != 0) {
  88                        fprintf(stderr, "bad header checksum\n");
  89                        continue;
  90                }
  91                if (compute_checksum(recs, head->table_bytes)
  92                        != head->table_checksum) {
  93                        fprintf(stderr, "bad table checksum: %04x\n",
  94                                head->table_checksum);
  95                        continue;
  96                }
  97                fprintf(stdout, "Found coreboot table at: %08lx\n", addr);
  98                return head;
  99
 100        };
 101        return 0;
 102}
 103
 104void nop_print(struct lb_record *rec, unsigned long addr)
 105{
 106        return;
 107}
 108
 109void pretty_print_number(FILE *stream, uint64_t num)
 110{
 111        unsigned long long value = (unsigned long long) num;
 112
 113        if (value >      1024ULL*1024*1024*1024*1024*1024) {
 114                value /= 1024ULL*1024*1024*1024*1024*1024;
 115                fprintf(stream, "%lldEB", value);
 116        }
 117        else if (value > 1024ULL*1024*1024*1024*1024) {
 118                value /= 1024ULL*1024*1024*1024*1024;
 119                fprintf(stream, "%lldPB", value);
 120        }
 121        else if (value > 1024ULL*1024*1024*1024) {
 122                value /= 1024ULL*1024*1024*1024;
 123                fprintf(stream, "%lldTB", value);
 124        }
 125        else if (value > 1024ULL*1024*1024) {
 126                value /= 1024ULL*1024*1024;
 127                fprintf(stream, "%lldGB", value);
 128        }
 129        else if (value > 1024ULL*1024) {
 130                value /= 1024ULL*1024;
 131                fprintf(stream, "%lldMB", value);
 132        }
 133        else if (value > 1024ULL) {
 134                value /= 1024ULL;
 135                fprintf(stream, "%lldKB", value);
 136        }
 137        else {
 138                fprintf(stream, "%lldB", value);
 139        }
 140}
 141
 142void print_memory(struct lb_record *ptr, unsigned long addr)
 143{
 144        struct lb_memory *rec = (void *)ptr;
 145        int entries;
 146        int i;
 147        entries = (rec->size -  sizeof(*rec))/sizeof(rec->map[0]);
 148        for(i = 0; i < entries; i++) {
 149                char *mem_type;
 150                uint64_t start;
 151                uint64_t end;
 152                start = unpack_lb64(rec->map[i].start);
 153                end = start + unpack_lb64(rec->map[i].size);
 154                switch(rec->map[i].type) {
 155                case 1: mem_type = "ram"; break;
 156                case 3: mem_type = "acpi"; break;
 157                case 4: mem_type = "nvs"; break;
 158                default:
 159                case 2: mem_type = "reserved"; break;
 160                }
 161                printf("0x%08llx - 0x%08llx %s (",
 162                        (unsigned long long)start, (unsigned long long)end, mem_type);
 163                pretty_print_number(stdout, start);
 164                printf(" - ");
 165                pretty_print_number(stdout, end);
 166                printf(")\n");
 167        }
 168}
 169
 170void print_mainboard(struct lb_record *ptr, unsigned long addr)
 171{
 172        struct lb_mainboard *rec;
 173        int max_size;
 174        rec = (struct lb_mainboard *)ptr;
 175        max_size = rec->size - sizeof(*rec);
 176        printf("vendor: %.*s part number: %.*s\n",
 177                max_size - rec->vendor_idx,      rec->strings + rec->vendor_idx,
 178                max_size - rec->part_number_idx, rec->strings + rec->part_number_idx);
 179}
 180
 181void print_string(struct lb_record *ptr, unsigned long addr)
 182{
 183        struct lb_string *rec;
 184        int max_size;
 185        rec = (struct lb_string *)ptr;
 186        max_size = rec->size - sizeof(*rec);
 187        printf("%.*s\n", max_size, rec->string);
 188}
 189
 190void print_option_table(struct lb_record *ptr, unsigned long addr)
 191{
 192        struct lb_record *rec, *last;
 193        struct cmos_option_table *hdr;
 194        hdr = (struct cmos_option_table *)ptr;
 195        rec  = (struct lb_record *)(((char *)hdr) + hdr->header_length);
 196        last = (struct lb_record *)(((char *)hdr) + hdr->size);
 197        printf("cmos option header record = type %d, size %d, header length %d\n",
 198                hdr->tag, hdr->size, hdr->header_length);
 199        print_lb_records(rec, last, addr + hdr->header_length);
 200#if 0
 201        {
 202                unsigned char *data = (unsigned char *)ptr;
 203                int i;
 204                for(i = 0; i < hdr->size; i++) {
 205                        if ((i %10) == 0 ) {
 206                                fprintf(stderr, "\n\t");
 207                        }
 208                        fprintf(stderr, "0x%02x,", data[i]);
 209                }
 210        }
 211#endif
 212
 213}
 214
 215void print_option(struct lb_record *ptr, unsigned long addr)
 216{
 217        struct cmos_entries *rec;
 218        rec= (struct cmos_entries *)ptr;
 219        printf("entry %d, rec len %d, start %d, length %d, conf %d, id %d, %s\n",
 220                rec->tag, rec->size, rec->bit, rec->length,
 221                rec->config, rec->config_id, rec->name);
 222}
 223
 224void print_option_enumeration(struct lb_record *ptr, unsigned long addr)
 225{
 226        struct cmos_enums *rec;
 227        rec = (struct cmos_enums *)ptr;
 228        printf("enumeration %d, rec len %d, id %d, value %d, %s\n",
 229                rec->tag, rec->size, rec->config_id, rec->value,
 230                rec->text);
 231}
 232
 233void print_option_checksum(struct lb_record *ptr, unsigned long addr)
 234{
 235        struct cmos_checksum *rec;
 236        rec = (struct cmos_checksum *)ptr;
 237        printf("checksum %d, rec len %d, range %d-%d location %d type %d\n",
 238                rec->tag, rec->size,
 239                rec->range_start, rec->range_end, rec->location, rec->type);
 240}
 241
 242struct {
 243        uint32_t type;
 244        char *type_name;
 245        void (*print)(struct lb_record *rec, unsigned long addr);
 246} lb_types[] = {
 247        { LB_TAG_UNUSED,            "Unused",             nop_print },
 248        { LB_TAG_MEMORY,            "Memory",             print_memory },
 249        { LB_TAG_HWRPB,             "HWRPB",              nop_print },
 250        { LB_TAG_MAINBOARD,         "Mainboard",          print_mainboard },
 251        { LB_TAG_VERSION,           "Version",            print_string },
 252        { LB_TAG_EXTRA_VERSION,     "Extra Version",      print_string },
 253        { LB_TAG_BUILD,             "Build",              print_string },
 254        { LB_TAG_COMPILE_TIME,      "Compile Time",       print_string },
 255        { LB_TAG_COMPILE_BY,        "Compile By",         print_string },
 256        { LB_TAG_COMPILE_HOST,      "Compile Host",       print_string },
 257        { LB_TAG_COMPILE_DOMAIN,    "Compile Domain",     print_string },
 258        { LB_TAG_COMPILER,          "Compiler",           print_string },
 259        { LB_TAG_LINKER,            "Linker",             print_string },
 260        { LB_TAG_ASSEMBLER,         "Assembler",          print_string },
 261        { LB_TAG_CMOS_OPTION_TABLE, "CMOS option table",  print_option_table },
 262        { LB_TAG_OPTION,            "Option",             print_option },
 263        { LB_TAG_OPTION_ENUM,       "Option Enumeration", print_option_enumeration },
 264        { LB_TAG_OPTION_DEFAULTS,   "Option Defaults",    nop_print },
 265        { LB_TAG_OPTION_CHECKSUM,   "Option Checksum",    print_option_checksum },
 266        { -1, "Unknown", 0 }
 267};
 268
 269static struct lb_record *next_record(struct lb_record *rec)
 270{
 271        return (struct lb_record *)(((char *)rec) + rec->size);
 272}
 273
 274void print_lb_records(struct lb_record *rec, struct lb_record *last,
 275        unsigned long addr)
 276{
 277        struct lb_record *next;
 278        int i;
 279        int count;
 280        count = 0;
 281
 282        for(next = next_record(rec); (rec < last) && (next <= last);
 283                rec = next, addr += rec->size) {
 284                next = next_record(rec);
 285                count++;
 286                for(i = 0; lb_types[i].print != 0; i++) {
 287                        if (lb_types[i].type == rec->tag) {
 288                                break;
 289                        }
 290                }
 291                printf("lb_record #%d type %d @ 0x%08lx %s\n",
 292                        count, rec->tag, addr, lb_types[i].type_name);
 293                if (lb_types[i].print) {
 294                        lb_types[i].print(rec, addr);
 295                }
 296        }
 297}
 298
 299void print_lb_table(struct lb_header *head, unsigned long addr)
 300{
 301        struct lb_record *rec, *last;
 302
 303        rec  = (struct lb_record *)(((char *)head) + head->header_bytes);
 304        last = (struct lb_record *)(((char *)rec) + head->table_bytes);
 305
 306        printf("Coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
 307                head->header_bytes, head->header_checksum,
 308                head->table_bytes, head->table_checksum, head->table_entries);
 309        print_lb_records(rec, last, addr + head->header_bytes);
 310}
 311
 312int main(int argc, char **argv)
 313{
 314        unsigned char *low_1MB;
 315        struct lb_header *lb_table;
 316        int fd;
 317        fd = open("/dev/mem", O_RDONLY);
 318        if (fd < 0) {
 319                fprintf(stderr, "Can not open /dev/mem\n");
 320                exit(-1);
 321        }
 322        low_1MB = mmap(0, 1024*1024, PROT_READ, MAP_SHARED, fd, 0x00000000);
 323        if (low_1MB == ((void *) -1)) {
 324                fprintf(stderr, "Can not mmap /dev/mem at %08lx errno(%d):%s\n",
 325                        0x00000000UL, errno, strerror(errno));
 326                exit(-2);
 327        }
 328        lb_table = 0;
 329        if (!lb_table)
 330                lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
 331        if (!lb_table)
 332                lb_table = find_lb_table(low_1MB, 0xf0000, 1024*1024);
 333        if (lb_table) {
 334                unsigned long addr;
 335                addr = ((char *)lb_table) - ((char *)low_1MB);
 336                print_lb_table(lb_table, addr);
 337        }
 338        else {
 339                printf("lb_table not found\n");
 340        }
 341        return 0;
 342}
 343
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.