00001 #include <stdio.h>
00002 #include <stdint.h>
00003 #include <stdlib.h>
00004 #include <fcntl.h>
00005 #include <unistd.h>
00006 #include <sys/types.h>
00007 #include <string.h>
00008 #include <errno.h>
00009 #include <sys/mman.h>
00010 #include "../../src/include/boot/coreboot_tables.h"
00011
00012 void print_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr);
00013
00014 unsigned long compute_checksum(void *addr, unsigned long length)
00015 {
00016 uint8_t *ptr;
00017 volatile union {
00018 uint8_t byte[2];
00019 uint16_t word;
00020 } value;
00021 unsigned long sum;
00022 unsigned long i;
00023
00024
00025
00026 sum = 0;
00027 ptr = addr;
00028 for(i = 0; i < length; i++) {
00029 unsigned long value;
00030 value = ptr[i];
00031 if (i & 1) {
00032 value <<= 8;
00033 }
00034
00035 sum += value;
00036
00037 if (sum > 0xFFFF) {
00038 sum = (sum + (sum >> 16)) & 0xFFFF;
00039 }
00040 }
00041 value.byte[0] = sum & 0xff;
00042 value.byte[1] = (sum >> 8) & 0xff;
00043 return (~value.word) & 0xFFFF;
00044 }
00045
00046 #define for_each_lbrec(head, rec) \
00047 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
00048 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
00049 (rec->size >= 1) && \
00050 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
00051 rec = (struct lb_record *)(((char *)rec) + rec->size))
00052
00053
00054 static int count_lb_records(struct lb_header *head)
00055 {
00056 struct lb_record *rec;
00057 int count;
00058 count = 0;
00059 for_each_lbrec(head, rec) {
00060 count++;
00061 }
00062 return count;
00063 }
00064
00065
00066 struct lb_header *find_lb_table(void *base, unsigned long start, unsigned long end)
00067 {
00068 unsigned long addr;
00069
00070 for(addr = start; addr < end; addr += 16) {
00071 struct lb_header *head = (struct lb_header *)(((char*)base) + addr);
00072 struct lb_record *recs = (struct lb_record *)(((char*)base) + addr + sizeof(*head));
00073 if (memcmp(head->signature, "LBIO", 4) != 0)
00074 continue;
00075 fprintf(stdout, "Found candidate at: %08lx-%08lx\n",
00076 addr, addr + head->table_bytes);
00077 if (head->header_bytes != sizeof(*head)) {
00078 fprintf(stderr, "Header bytes of %d are incorrect\n",
00079 head->header_bytes);
00080 continue;
00081 }
00082 if (count_lb_records(head) != head->table_entries) {
00083 fprintf(stderr, "bad record count: %d\n",
00084 head->table_entries);
00085 continue;
00086 }
00087 if (compute_checksum((unsigned char *)head, sizeof(*head)) != 0) {
00088 fprintf(stderr, "bad header checksum\n");
00089 continue;
00090 }
00091 if (compute_checksum(recs, head->table_bytes)
00092 != head->table_checksum) {
00093 fprintf(stderr, "bad table checksum: %04x\n",
00094 head->table_checksum);
00095 continue;
00096 }
00097 fprintf(stdout, "Found coreboot table at: %08lx\n", addr);
00098 return head;
00099
00100 };
00101 return 0;
00102 }
00103
00104 void nop_print(struct lb_record *rec, unsigned long addr)
00105 {
00106 return;
00107 }
00108
00109 void pretty_print_number(FILE *stream, uint64_t num)
00110 {
00111 unsigned long long value = (unsigned long long) num;
00112
00113 if (value > 1024ULL*1024*1024*1024*1024*1024) {
00114 value /= 1024ULL*1024*1024*1024*1024*1024;
00115 fprintf(stream, "%lldEB", value);
00116 }
00117 else if (value > 1024ULL*1024*1024*1024*1024) {
00118 value /= 1024ULL*1024*1024*1024*1024;
00119 fprintf(stream, "%lldPB", value);
00120 }
00121 else if (value > 1024ULL*1024*1024*1024) {
00122 value /= 1024ULL*1024*1024*1024;
00123 fprintf(stream, "%lldTB", value);
00124 }
00125 else if (value > 1024ULL*1024*1024) {
00126 value /= 1024ULL*1024*1024;
00127 fprintf(stream, "%lldGB", value);
00128 }
00129 else if (value > 1024ULL*1024) {
00130 value /= 1024ULL*1024;
00131 fprintf(stream, "%lldMB", value);
00132 }
00133 else if (value > 1024ULL) {
00134 value /= 1024ULL;
00135 fprintf(stream, "%lldKB", value);
00136 }
00137 else {
00138 fprintf(stream, "%lldB", value);
00139 }
00140 }
00141
00142 void print_memory(struct lb_record *ptr, unsigned long addr)
00143 {
00144 struct lb_memory *rec = (void *)ptr;
00145 int entries;
00146 int i;
00147 entries = (rec->size - sizeof(*rec))/sizeof(rec->map[0]);
00148 for(i = 0; i < entries; i++) {
00149 char *mem_type;
00150 uint64_t start;
00151 uint64_t end;
00152 start = unpack_lb64(rec->map[i].start);
00153 end = start + unpack_lb64(rec->map[i].size);
00154 switch(rec->map[i].type) {
00155 case 1: mem_type = "ram"; break;
00156 case 3: mem_type = "acpi"; break;
00157 case 4: mem_type = "nvs"; break;
00158 default:
00159 case 2: mem_type = "reserved"; break;
00160 }
00161 printf("0x%08llx - 0x%08llx %s (",
00162 (unsigned long long)start, (unsigned long long)end, mem_type);
00163 pretty_print_number(stdout, start);
00164 printf(" - ");
00165 pretty_print_number(stdout, end);
00166 printf(")\n");
00167 }
00168 }
00169
00170 void print_mainboard(struct lb_record *ptr, unsigned long addr)
00171 {
00172 struct lb_mainboard *rec;
00173 int max_size;
00174 rec = (struct lb_mainboard *)ptr;
00175 max_size = rec->size - sizeof(*rec);
00176 printf("vendor: %.*s part number: %.*s\n",
00177 max_size - rec->vendor_idx, rec->strings + rec->vendor_idx,
00178 max_size - rec->part_number_idx, rec->strings + rec->part_number_idx);
00179 }
00180
00181 void print_string(struct lb_record *ptr, unsigned long addr)
00182 {
00183 struct lb_string *rec;
00184 int max_size;
00185 rec = (struct lb_string *)ptr;
00186 max_size = rec->size - sizeof(*rec);
00187 printf("%.*s\n", max_size, rec->string);
00188 }
00189
00190 void print_option_table(struct lb_record *ptr, unsigned long addr)
00191 {
00192 struct lb_record *rec, *last;
00193 struct cmos_option_table *hdr;
00194 hdr = (struct cmos_option_table *)ptr;
00195 rec = (struct lb_record *)(((char *)hdr) + hdr->header_length);
00196 last = (struct lb_record *)(((char *)hdr) + hdr->size);
00197 printf("cmos option header record = type %d, size %d, header length %d\n",
00198 hdr->tag, hdr->size, hdr->header_length);
00199 print_lb_records(rec, last, addr + hdr->header_length);
00200 #if 0
00201 {
00202 unsigned char *data = (unsigned char *)ptr;
00203 int i;
00204 for(i = 0; i < hdr->size; i++) {
00205 if ((i %10) == 0 ) {
00206 fprintf(stderr, "\n\t");
00207 }
00208 fprintf(stderr, "0x%02x,", data[i]);
00209 }
00210 }
00211 #endif
00212
00213 }
00214
00215 void print_option(struct lb_record *ptr, unsigned long addr)
00216 {
00217 struct cmos_entries *rec;
00218 rec= (struct cmos_entries *)ptr;
00219 printf("entry %d, rec len %d, start %d, length %d, conf %d, id %d, %s\n",
00220 rec->tag, rec->size, rec->bit, rec->length,
00221 rec->config, rec->config_id, rec->name);
00222 }
00223
00224 void print_option_enumeration(struct lb_record *ptr, unsigned long addr)
00225 {
00226 struct cmos_enums *rec;
00227 rec = (struct cmos_enums *)ptr;
00228 printf("enumeration %d, rec len %d, id %d, value %d, %s\n",
00229 rec->tag, rec->size, rec->config_id, rec->value,
00230 rec->text);
00231 }
00232
00233 void print_option_checksum(struct lb_record *ptr, unsigned long addr)
00234 {
00235 struct cmos_checksum *rec;
00236 rec = (struct cmos_checksum *)ptr;
00237 printf("checksum %d, rec len %d, range %d-%d location %d type %d\n",
00238 rec->tag, rec->size,
00239 rec->range_start, rec->range_end, rec->location, rec->type);
00240 }
00241
00242 struct {
00243 uint32_t type;
00244 char *type_name;
00245 void (*print)(struct lb_record *rec, unsigned long addr);
00246 } lb_types[] = {
00247 { LB_TAG_UNUSED, "Unused", nop_print },
00248 { LB_TAG_MEMORY, "Memory", print_memory },
00249 { LB_TAG_HWRPB, "HWRPB", nop_print },
00250 { LB_TAG_MAINBOARD, "Mainboard", print_mainboard },
00251 { LB_TAG_VERSION, "Version", print_string },
00252 { LB_TAG_EXTRA_VERSION, "Extra Version", print_string },
00253 { LB_TAG_BUILD, "Build", print_string },
00254 { LB_TAG_COMPILE_TIME, "Compile Time", print_string },
00255 { LB_TAG_COMPILE_BY, "Compile By", print_string },
00256 { LB_TAG_COMPILE_HOST, "Compile Host", print_string },
00257 { LB_TAG_COMPILE_DOMAIN, "Compile Domain", print_string },
00258 { LB_TAG_COMPILER, "Compiler", print_string },
00259 { LB_TAG_LINKER, "Linker", print_string },
00260 { LB_TAG_ASSEMBLER, "Assembler", print_string },
00261 { LB_TAG_CMOS_OPTION_TABLE, "CMOS option table", print_option_table },
00262 { LB_TAG_OPTION, "Option", print_option },
00263 { LB_TAG_OPTION_ENUM, "Option Enumeration", print_option_enumeration },
00264 { LB_TAG_OPTION_DEFAULTS, "Option Defaults", nop_print },
00265 { LB_TAG_OPTION_CHECKSUM, "Option Checksum", print_option_checksum },
00266 { -1, "Unknown", 0 }
00267 };
00268
00269 static struct lb_record *next_record(struct lb_record *rec)
00270 {
00271 return (struct lb_record *)(((char *)rec) + rec->size);
00272 }
00273
00274 void print_lb_records(struct lb_record *rec, struct lb_record *last,
00275 unsigned long addr)
00276 {
00277 struct lb_record *next;
00278 int i;
00279 int count;
00280 count = 0;
00281
00282 for(next = next_record(rec); (rec < last) && (next <= last);
00283 rec = next, addr += rec->size) {
00284 next = next_record(rec);
00285 count++;
00286 for(i = 0; lb_types[i].print != 0; i++) {
00287 if (lb_types[i].type == rec->tag) {
00288 break;
00289 }
00290 }
00291 printf("lb_record #%d type %d @ 0x%08lx %s\n",
00292 count, rec->tag, addr, lb_types[i].type_name);
00293 if (lb_types[i].print) {
00294 lb_types[i].print(rec, addr);
00295 }
00296 }
00297 }
00298
00299 void print_lb_table(struct lb_header *head, unsigned long addr)
00300 {
00301 struct lb_record *rec, *last;
00302
00303 rec = (struct lb_record *)(((char *)head) + head->header_bytes);
00304 last = (struct lb_record *)(((char *)rec) + head->table_bytes);
00305
00306 printf("Coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
00307 head->header_bytes, head->header_checksum,
00308 head->table_bytes, head->table_checksum, head->table_entries);
00309 print_lb_records(rec, last, addr + head->header_bytes);
00310 }
00311
00312 int main(int argc, char **argv)
00313 {
00314 unsigned char *low_1MB;
00315 struct lb_header *lb_table;
00316 int fd;
00317 fd = open("/dev/mem", O_RDONLY);
00318 if (fd < 0) {
00319 fprintf(stderr, "Can not open /dev/mem\n");
00320 exit(-1);
00321 }
00322 low_1MB = mmap(0, 1024*1024, PROT_READ, MAP_SHARED, fd, 0x00000000);
00323 if (low_1MB == ((void *) -1)) {
00324 fprintf(stderr, "Can not mmap /dev/mem at %08lx errno(%d):%s\n",
00325 0x00000000UL, errno, strerror(errno));
00326 exit(-2);
00327 }
00328 lb_table = 0;
00329 if (!lb_table)
00330 lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
00331 if (!lb_table)
00332 lb_table = find_lb_table(low_1MB, 0xf0000, 1024*1024);
00333 if (lb_table) {
00334 unsigned long addr;
00335 addr = ((char *)lb_table) - ((char *)low_1MB);
00336 print_lb_table(lb_table, addr);
00337 }
00338 else {
00339 printf("lb_table not found\n");
00340 }
00341 return 0;
00342 }