10 #include "../../src/include/boot/coreboot_tables.h"
12 void print_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr);
14 unsigned long compute_checksum(void *addr, unsigned long length)
23 /* In the most straight forward way possible,
24 * compute an ip style checksum.
28 for(i = 0; i < length; i++) {
34 /* Add the new value */
36 /* Wrap around the carry */
38 sum = (sum + (sum >> 16)) & 0xFFFF;
41 value.byte[0] = sum & 0xff;
42 value.byte[1] = (sum >> 8) & 0xff;
43 return (~value.word) & 0xFFFF;
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)) && \
50 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
51 rec = (struct lb_record *)(((char *)rec) + rec->size))
54 static int count_lb_records(struct lb_header *head)
56 struct lb_record *rec;
59 for_each_lbrec(head, rec) {
66 struct lb_header *find_lb_table(void *base, unsigned long start, unsigned long end)
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)
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",
82 if (count_lb_records(head) != head->table_entries) {
83 fprintf(stderr, "bad record count: %d\n",
87 if (compute_checksum((unsigned char *)head, sizeof(*head)) != 0) {
88 fprintf(stderr, "bad header checksum\n");
91 if (compute_checksum(recs, head->table_bytes)
92 != head->table_checksum) {
93 fprintf(stderr, "bad table checksum: %04x\n",
94 head->table_checksum);
97 fprintf(stdout, "Found coreboot table at: %08lx\n", addr);
104 void nop_print(struct lb_record *rec, unsigned long addr)
109 void pretty_print_number(FILE *stream, uint64_t num)
111 unsigned long long value = (unsigned long long) num;
113 if (value > 1024ULL*1024*1024*1024*1024*1024) {
114 value /= 1024ULL*1024*1024*1024*1024*1024;
115 fprintf(stream, "%lldEB", value);
117 else if (value > 1024ULL*1024*1024*1024*1024) {
118 value /= 1024ULL*1024*1024*1024*1024;
119 fprintf(stream, "%lldPB", value);
121 else if (value > 1024ULL*1024*1024*1024) {
122 value /= 1024ULL*1024*1024*1024;
123 fprintf(stream, "%lldTB", value);
125 else if (value > 1024ULL*1024*1024) {
126 value /= 1024ULL*1024*1024;
127 fprintf(stream, "%lldGB", value);
129 else if (value > 1024ULL*1024) {
130 value /= 1024ULL*1024;
131 fprintf(stream, "%lldMB", value);
133 else if (value > 1024ULL) {
135 fprintf(stream, "%lldKB", value);
138 fprintf(stream, "%lldB", value);
142 void print_memory(struct lb_record *ptr, unsigned long addr)
144 struct lb_memory *rec = (void *)ptr;
147 entries = (rec->size - sizeof(*rec))/sizeof(rec->map[0]);
148 for(i = 0; i < entries; i++) {
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;
159 case 2: mem_type = "reserved"; break;
161 printf("0x%08llx - 0x%08llx %s (",
162 (unsigned long long)start, (unsigned long long)end, mem_type);
163 pretty_print_number(stdout, start);
165 pretty_print_number(stdout, end);
170 void print_mainboard(struct lb_record *ptr, unsigned long addr)
172 struct lb_mainboard *rec;
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);
181 void print_string(struct lb_record *ptr, unsigned long addr)
183 struct lb_string *rec;
185 rec = (struct lb_string *)ptr;
186 max_size = rec->size - sizeof(*rec);
187 printf("%.*s\n", max_size, rec->string);
190 void print_option_table(struct lb_record *ptr, unsigned long addr)
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);
202 unsigned char *data = (unsigned char *)ptr;
204 for(i = 0; i < hdr->size; i++) {
206 fprintf(stderr, "\n\t");
208 fprintf(stderr, "0x%02x,", data[i]);
215 void print_option(struct lb_record *ptr, unsigned long addr)
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);
224 void print_option_enumeration(struct lb_record *ptr, unsigned long addr)
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,
233 void print_option_checksum(struct lb_record *ptr, unsigned long addr)
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",
239 rec->range_start, rec->range_end, rec->location, rec->type);
245 void (*print)(struct lb_record *rec, unsigned long addr);
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 },
269 static struct lb_record *next_record(struct lb_record *rec)
271 return (struct lb_record *)(((char *)rec) + rec->size);
274 void print_lb_records(struct lb_record *rec, struct lb_record *last,
277 struct lb_record *next;
282 for(next = next_record(rec); (rec < last) && (next <= last);
283 rec = next, addr += rec->size) {
284 next = next_record(rec);
286 for(i = 0; lb_types[i].print != 0; i++) {
287 if (lb_types[i].type == rec->tag) {
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);
299 void print_lb_table(struct lb_header *head, unsigned long addr)
301 struct lb_record *rec, *last;
303 rec = (struct lb_record *)(((char *)head) + head->header_bytes);
304 last = (struct lb_record *)(((char *)rec) + head->table_bytes);
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);
312 int main(int argc, char **argv)
314 unsigned char *low_1MB;
315 struct lb_header *lb_table;
317 fd = open("/dev/mem", O_RDONLY);
319 fprintf(stderr, "Can not open /dev/mem\n");
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));
330 lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
332 lb_table = find_lb_table(low_1MB, 0xf0000, 1024*1024);
335 addr = ((char *)lb_table) - ((char *)low_1MB);
336 print_lb_table(lb_table, addr);
339 printf("lb_table not found\n");