blob: c5e4f281b7edfbca542067e1c79468fd5588bf40 [file] [log] [blame]
Stefan Reinauer8af79982005-11-12 20:58:26 +00001#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>
Stefan Reinauerca374d42008-01-18 16:16:45 +000010#include "../../src/include/boot/coreboot_tables.h"
Stefan Reinauer8af79982005-11-12 20:58:26 +000011
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +010012uint64_t print_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr);
Stefan Reinauer8af79982005-11-12 20:58:26 +000013
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)); \
Stefan Reinauer14e22772010-04-27 06:56:47 +000051 rec = (struct lb_record *)(((char *)rec) + rec->size))
52
Stefan Reinauer8af79982005-11-12 20:58:26 +000053
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;
Stefan Reinauer14e22772010-04-27 06:56:47 +000075 fprintf(stdout, "Found candidate at: %08lx-%08lx\n",
Stefan Reinauer8af79982005-11-12 20:58:26 +000076 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 }
Stefan Reinauerf8ee1802008-01-18 15:08:58 +000097 fprintf(stdout, "Found coreboot table at: %08lx\n", addr);
Stefan Reinauer8af79982005-11-12 20:58:26 +000098 return head;
99
100 };
101 return 0;
102}
103
104void nop_print(struct lb_record *rec, unsigned long addr)
105{
106 return;
107}
108
Stefan Reinauerc1412822007-05-20 17:28:55 +0000109void pretty_print_number(FILE *stream, uint64_t num)
Stefan Reinauer8af79982005-11-12 20:58:26 +0000110{
Stefan Reinauerc1412822007-05-20 17:28:55 +0000111 unsigned long long value = (unsigned long long) num;
112
Stefan Reinauer8af79982005-11-12 20:58:26 +0000113 if (value > 1024ULL*1024*1024*1024*1024*1024) {
114 value /= 1024ULL*1024*1024*1024*1024*1024;
Ben Hewson917bf6b2007-05-20 17:10:17 +0000115 fprintf(stream, "%lldEB", value);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000116 }
117 else if (value > 1024ULL*1024*1024*1024*1024) {
118 value /= 1024ULL*1024*1024*1024*1024;
Ben Hewson917bf6b2007-05-20 17:10:17 +0000119 fprintf(stream, "%lldPB", value);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000120 }
121 else if (value > 1024ULL*1024*1024*1024) {
122 value /= 1024ULL*1024*1024*1024;
Ben Hewson917bf6b2007-05-20 17:10:17 +0000123 fprintf(stream, "%lldTB", value);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000124 }
125 else if (value > 1024ULL*1024*1024) {
126 value /= 1024ULL*1024*1024;
Ben Hewson917bf6b2007-05-20 17:10:17 +0000127 fprintf(stream, "%lldGB", value);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000128 }
129 else if (value > 1024ULL*1024) {
130 value /= 1024ULL*1024;
Ben Hewson917bf6b2007-05-20 17:10:17 +0000131 fprintf(stream, "%lldMB", value);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000132 }
133 else if (value > 1024ULL) {
134 value /= 1024ULL;
Ben Hewson917bf6b2007-05-20 17:10:17 +0000135 fprintf(stream, "%lldKB", value);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000136 }
137 else {
Ben Hewson917bf6b2007-05-20 17:10:17 +0000138 fprintf(stream, "%lldB", value);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000139 }
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 }
Ben Hewson917bf6b2007-05-20 17:10:17 +0000161 printf("0x%08llx - 0x%08llx %s (",
Stefan Reinauerc1412822007-05-20 17:28:55 +0000162 (unsigned long long)start, (unsigned long long)end, mem_type);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000163 pretty_print_number(stdout, start);
164 printf(" - ");
Stefan Reinauer14e22772010-04-27 06:56:47 +0000165 pretty_print_number(stdout, end);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000166 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",
Stefan Reinauer14e22772010-04-27 06:56:47 +0000177 max_size - rec->vendor_idx, rec->strings + rec->vendor_idx,
Stefan Reinauer8af79982005-11-12 20:58:26 +0000178 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",
Stefan Reinauer14e22772010-04-27 06:56:47 +0000238 rec->tag, rec->size,
Stefan Reinauer8af79982005-11-12 20:58:26 +0000239 rec->range_start, rec->range_end, rec->location, rec->type);
240}
241
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +0100242void print_forward(struct lb_record *ptr, unsigned long addr)
243{
244 struct lb_forward *rec;
245 rec = (struct lb_forward *)ptr;
246 printf("forward %d, rec len %d, forward: 0x%llx\n",
247 rec->tag, rec->size, (unsigned long long) rec->forward);
248}
249
Stefan Reinauer8af79982005-11-12 20:58:26 +0000250struct {
251 uint32_t type;
252 char *type_name;
253 void (*print)(struct lb_record *rec, unsigned long addr);
254} lb_types[] = {
255 { LB_TAG_UNUSED, "Unused", nop_print },
256 { LB_TAG_MEMORY, "Memory", print_memory },
257 { LB_TAG_HWRPB, "HWRPB", nop_print },
258 { LB_TAG_MAINBOARD, "Mainboard", print_mainboard },
259 { LB_TAG_VERSION, "Version", print_string },
260 { LB_TAG_EXTRA_VERSION, "Extra Version", print_string },
261 { LB_TAG_BUILD, "Build", print_string },
262 { LB_TAG_COMPILE_TIME, "Compile Time", print_string },
263 { LB_TAG_COMPILE_BY, "Compile By", print_string },
264 { LB_TAG_COMPILE_HOST, "Compile Host", print_string },
265 { LB_TAG_COMPILE_DOMAIN, "Compile Domain", print_string },
266 { LB_TAG_COMPILER, "Compiler", print_string },
267 { LB_TAG_LINKER, "Linker", print_string },
268 { LB_TAG_ASSEMBLER, "Assembler", print_string },
269 { LB_TAG_CMOS_OPTION_TABLE, "CMOS option table", print_option_table },
270 { LB_TAG_OPTION, "Option", print_option },
271 { LB_TAG_OPTION_ENUM, "Option Enumeration", print_option_enumeration },
272 { LB_TAG_OPTION_DEFAULTS, "Option Defaults", nop_print },
273 { LB_TAG_OPTION_CHECKSUM, "Option Checksum", print_option_checksum },
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +0100274 { LB_TAG_FORWARD, "Forward link", print_forward },
Stefan Reinauer8af79982005-11-12 20:58:26 +0000275 { -1, "Unknown", 0 }
276};
277
278static struct lb_record *next_record(struct lb_record *rec)
279{
280 return (struct lb_record *)(((char *)rec) + rec->size);
281}
282
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +0100283uint64_t print_lb_records(struct lb_record *rec, struct lb_record *last,
Stefan Reinauer8af79982005-11-12 20:58:26 +0000284 unsigned long addr)
285{
286 struct lb_record *next;
287 int i;
288 int count;
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +0100289 uint64_t forward = 0;
Stefan Reinauer8af79982005-11-12 20:58:26 +0000290 count = 0;
291
Stefan Reinauer14e22772010-04-27 06:56:47 +0000292 for(next = next_record(rec); (rec < last) && (next <= last);
293 rec = next, addr += rec->size) {
Stefan Reinauer8af79982005-11-12 20:58:26 +0000294 next = next_record(rec);
295 count++;
296 for(i = 0; lb_types[i].print != 0; i++) {
297 if (lb_types[i].type == rec->tag) {
298 break;
299 }
300 }
301 printf("lb_record #%d type %d @ 0x%08lx %s\n",
302 count, rec->tag, addr, lb_types[i].type_name);
303 if (lb_types[i].print) {
304 lb_types[i].print(rec, addr);
305 }
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +0100306 if (rec->tag == LB_TAG_FORWARD)
307 forward = ((struct lb_forward *) rec)->forward;
Stefan Reinauer8af79982005-11-12 20:58:26 +0000308 }
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +0100309 return forward;
Stefan Reinauer8af79982005-11-12 20:58:26 +0000310}
311
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +0100312static void *do_map(int fd, uint64_t addr, uint32_t len, void **mpaddr_out, size_t *mplen)
313{
314 uint64_t mpaddr, mpaddrend;
315 uint8_t *ret;
316 mpaddrend = (addr + len + 4095) & ~4095ULL;
317 mpaddr = addr & ~4095ULL;
318 ret = mmap(0, mpaddrend - mpaddr, PROT_READ, MAP_SHARED, fd, mpaddr);
319 if (ret == ((void *) -1)) {
320 fprintf(stderr, "Can not mmap /dev/mem at %08lx-%08lx errno(%d):%s\n",
321 mpaddr, mpaddrend, errno, strerror(errno));
322 exit(-2);
323 }
324 *mpaddr_out = ret;
325 *mplen = mpaddrend - mpaddr;
326 return ret + addr - mpaddr;
327}
328
329uint64_t print_lb_table(int fd, uint64_t addr)
Stefan Reinauer8af79982005-11-12 20:58:26 +0000330{
331 struct lb_record *rec, *last;
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +0100332 struct lb_header *head;
333 void *mp;
334 size_t mplen;
335 uint32_t bytes;
336 uint64_t forward;
337
338 head = do_map(fd, addr, sizeof (*head), &mp, &mplen);
339 bytes = head->table_bytes;
340 munmap (mp, mplen);
341
342 head = do_map(fd, addr, bytes, &mp, &mplen);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000343
344 rec = (struct lb_record *)(((char *)head) + head->header_bytes);
345 last = (struct lb_record *)(((char *)rec) + head->table_bytes);
346
Stefan Reinauerf8ee1802008-01-18 15:08:58 +0000347 printf("Coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
Stefan Reinauer8af79982005-11-12 20:58:26 +0000348 head->header_bytes, head->header_checksum,
349 head->table_bytes, head->table_checksum, head->table_entries);
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +0100350 forward = print_lb_records(rec, last, addr + head->header_bytes);
351 munmap (mp, mplen);
352 return forward;
Stefan Reinauer8af79982005-11-12 20:58:26 +0000353}
354
Stefan Reinauer14e22772010-04-27 06:56:47 +0000355int main(int argc, char **argv)
Stefan Reinauer8af79982005-11-12 20:58:26 +0000356{
357 unsigned char *low_1MB;
358 struct lb_header *lb_table;
359 int fd;
360 fd = open("/dev/mem", O_RDONLY);
361 if (fd < 0) {
362 fprintf(stderr, "Can not open /dev/mem\n");
363 exit(-1);
364 }
365 low_1MB = mmap(0, 1024*1024, PROT_READ, MAP_SHARED, fd, 0x00000000);
366 if (low_1MB == ((void *) -1)) {
367 fprintf(stderr, "Can not mmap /dev/mem at %08lx errno(%d):%s\n",
368 0x00000000UL, errno, strerror(errno));
369 exit(-2);
370 }
371 lb_table = 0;
372 if (!lb_table)
373 lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
374 if (!lb_table)
375 lb_table = find_lb_table(low_1MB, 0xf0000, 1024*1024);
376 if (lb_table) {
377 unsigned long addr;
378 addr = ((char *)lb_table) - ((char *)low_1MB);
Vladimir Serbinenko7d48f042014-01-27 00:16:51 +0100379
380 while (addr)
381 addr = print_lb_table(fd, addr);
Stefan Reinauer8af79982005-11-12 20:58:26 +0000382 }
383 else {
384 printf("lb_table not found\n");
385 }
386 return 0;
387}