Here is a small fix to prevent a segmentation fault in lbtdump.
[coreboot.git] / util / lbtdump / lbtdump.c
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/linuxbios_tables.h"
11
12 void print_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr);
13
14 unsigned 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
54 static 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
66 struct 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 canidate 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 LinuxBIOS table at: %08lx\n", addr);
98                 return head;
99
100         };
101         return 0;
102 }
103
104 void nop_print(struct lb_record *rec, unsigned long addr)
105 {
106         return;
107 }
108
109 void pretty_print_number(FILE *stream, uint64_t value)
110 {
111         if (value >      1024ULL*1024*1024*1024*1024*1024) {
112                 value /= 1024ULL*1024*1024*1024*1024*1024;
113                 fprintf(stream, "%lldEB", value);
114         }
115         else if (value > 1024ULL*1024*1024*1024*1024) {
116                 value /= 1024ULL*1024*1024*1024*1024;
117                 fprintf(stream, "%lldPB", value);
118         }
119         else if (value > 1024ULL*1024*1024*1024) {
120                 value /= 1024ULL*1024*1024*1024;
121                 fprintf(stream, "%lldTB", value);
122         }
123         else if (value > 1024ULL*1024*1024) {
124                 value /= 1024ULL*1024*1024;
125                 fprintf(stream, "%lldGB", value);
126         }
127         else if (value > 1024ULL*1024) {
128                 value /= 1024ULL*1024;
129                 fprintf(stream, "%lldMB", value);
130         }
131         else if (value > 1024ULL) {
132                 value /= 1024ULL;
133                 fprintf(stream, "%lldKB", value);
134         }
135         else {
136                 fprintf(stream, "%lldB", value);
137         }
138 }
139
140 void print_memory(struct lb_record *ptr, unsigned long addr)
141 {
142         struct lb_memory *rec = (void *)ptr;
143         int entries;
144         int i;
145         entries = (rec->size -  sizeof(*rec))/sizeof(rec->map[0]);
146         for(i = 0; i < entries; i++) {
147                 char *mem_type;
148                 uint64_t start;
149                 uint64_t end;
150                 start = unpack_lb64(rec->map[i].start);
151                 end = start + unpack_lb64(rec->map[i].size);
152                 switch(rec->map[i].type) {
153                 case 1: mem_type = "ram"; break;
154                 case 3: mem_type = "acpi"; break;
155                 case 4: mem_type = "nvs"; break;
156                 default:
157                 case 2: mem_type = "reserved"; break;
158                 }
159                 printf("0x%08llx - 0x%08llx %s (",
160                         start, end, mem_type);
161                 pretty_print_number(stdout, start);
162                 printf(" - ");
163                 pretty_print_number(stdout, end);               
164                 printf(")\n");
165         }
166 }
167
168 void print_mainboard(struct lb_record *ptr, unsigned long addr)
169 {
170         struct lb_mainboard *rec;
171         int max_size;
172         rec = (struct lb_mainboard *)ptr;
173         max_size = rec->size - sizeof(*rec);
174         printf("vendor: %.*s part number: %.*s\n",
175                 max_size - rec->vendor_idx,      rec->strings + rec->vendor_idx, 
176                 max_size - rec->part_number_idx, rec->strings + rec->part_number_idx);
177 }
178
179 void print_string(struct lb_record *ptr, unsigned long addr)
180 {
181         struct lb_string *rec;
182         int max_size;
183         rec = (struct lb_string *)ptr;
184         max_size = rec->size - sizeof(*rec);
185         printf("%.*s\n", max_size, rec->string);
186 }
187
188 void print_option_table(struct lb_record *ptr, unsigned long addr)
189 {
190         struct lb_record *rec, *last;
191         struct cmos_option_table *hdr;
192         hdr = (struct cmos_option_table *)ptr;
193         rec  = (struct lb_record *)(((char *)hdr) + hdr->header_length);
194         last = (struct lb_record *)(((char *)hdr) + hdr->size);
195         printf("cmos option header record = type %d, size %d, header length %d\n",
196                 hdr->tag, hdr->size, hdr->header_length);
197         print_lb_records(rec, last, addr + hdr->header_length);
198 #if 0
199         {
200                 unsigned char *data = (unsigned char *)ptr;
201                 int i;
202                 for(i = 0; i < hdr->size; i++) {
203                         if ((i %10) == 0 ) {
204                                 fprintf(stderr, "\n\t");
205                         }
206                         fprintf(stderr, "0x%02x,", data[i]);
207                 }
208         }
209 #endif
210
211 }
212
213 void print_option(struct lb_record *ptr, unsigned long addr)
214 {
215         struct cmos_entries *rec;
216         rec= (struct cmos_entries *)ptr;
217         printf("entry %d, rec len %d, start %d, length %d, conf %d, id %d, %s\n",
218                 rec->tag, rec->size, rec->bit, rec->length,
219                 rec->config, rec->config_id, rec->name);
220 }
221
222 void print_option_enumeration(struct lb_record *ptr, unsigned long addr)
223 {
224         struct cmos_enums *rec;
225         rec = (struct cmos_enums *)ptr;
226         printf("enumeration %d, rec len %d, id %d, value %d, %s\n",
227                 rec->tag, rec->size, rec->config_id, rec->value,
228                 rec->text);
229 }
230
231 void print_option_checksum(struct lb_record *ptr, unsigned long addr)
232 {
233         struct cmos_checksum *rec;
234         rec = (struct cmos_checksum *)ptr;
235         printf("checksum %d, rec len %d, range %d-%d location %d type %d\n",
236                 rec->tag, rec->size, 
237                 rec->range_start, rec->range_end, rec->location, rec->type);
238 }
239
240 struct {
241         uint32_t type;
242         char *type_name;
243         void (*print)(struct lb_record *rec, unsigned long addr);
244 } lb_types[] = {
245         { LB_TAG_UNUSED,            "Unused",             nop_print },
246         { LB_TAG_MEMORY,            "Memory",             print_memory },
247         { LB_TAG_HWRPB,             "HWRPB",              nop_print },
248         { LB_TAG_MAINBOARD,         "Mainboard",          print_mainboard },
249         { LB_TAG_VERSION,           "Version",            print_string },
250         { LB_TAG_EXTRA_VERSION,     "Extra Version",      print_string },
251         { LB_TAG_BUILD,             "Build",              print_string },
252         { LB_TAG_COMPILE_TIME,      "Compile Time",       print_string },
253         { LB_TAG_COMPILE_BY,        "Compile By",         print_string },
254         { LB_TAG_COMPILE_HOST,      "Compile Host",       print_string },
255         { LB_TAG_COMPILE_DOMAIN,    "Compile Domain",     print_string },
256         { LB_TAG_COMPILER,          "Compiler",           print_string },
257         { LB_TAG_LINKER,            "Linker",             print_string },
258         { LB_TAG_ASSEMBLER,         "Assembler",          print_string },
259         { LB_TAG_CMOS_OPTION_TABLE, "CMOS option table",  print_option_table },
260         { LB_TAG_OPTION,            "Option",             print_option },
261         { LB_TAG_OPTION_ENUM,       "Option Enumeration", print_option_enumeration },
262         { LB_TAG_OPTION_DEFAULTS,   "Option Defaults",    nop_print },
263         { LB_TAG_OPTION_CHECKSUM,   "Option Checksum",    print_option_checksum },
264         { -1, "Unknown", 0 }
265 };
266
267 static struct lb_record *next_record(struct lb_record *rec)
268 {
269         return (struct lb_record *)(((char *)rec) + rec->size);
270 }
271
272 void print_lb_records(struct lb_record *rec, struct lb_record *last, 
273         unsigned long addr)
274 {
275         struct lb_record *next;
276         int i;
277         int count;
278         count = 0;
279
280         for(next = next_record(rec); (rec < last) && (next <= last); 
281                 rec = next, addr += rec->size) { 
282                 next = next_record(rec);
283                 count++;
284                 for(i = 0; lb_types[i].print != 0; i++) {
285                         if (lb_types[i].type == rec->tag) {
286                                 break;
287                         }
288                 }
289                 printf("lb_record #%d type %d @ 0x%08lx %s\n",
290                         count, rec->tag, addr, lb_types[i].type_name);
291                 if (lb_types[i].print) {
292                         lb_types[i].print(rec, addr);
293                 }
294         }
295 }
296
297 void print_lb_table(struct lb_header *head, unsigned long addr)
298 {
299         struct lb_record *rec, *last;
300
301         rec  = (struct lb_record *)(((char *)head) + head->header_bytes);
302         last = (struct lb_record *)(((char *)rec) + head->table_bytes);
303
304         printf("LinuxBIOS header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
305                 head->header_bytes, head->header_checksum,
306                 head->table_bytes, head->table_checksum, head->table_entries);
307         print_lb_records(rec, last, addr + head->header_bytes);
308 }
309
310 int main(int argc, char **argv) 
311 {
312         unsigned char *low_1MB;
313         struct lb_header *lb_table;
314         int fd;
315         fd = open("/dev/mem", O_RDONLY);
316         if (fd < 0) {
317                 fprintf(stderr, "Can not open /dev/mem\n");
318                 exit(-1);
319         }
320         low_1MB = mmap(0, 1024*1024, PROT_READ, MAP_SHARED, fd, 0x00000000);
321         if (low_1MB == ((void *) -1)) {
322                 fprintf(stderr, "Can not mmap /dev/mem at %08lx errno(%d):%s\n",
323                         0x00000000UL, errno, strerror(errno));
324                 exit(-2);
325         }
326         lb_table = 0;
327         if (!lb_table)
328                 lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
329         if (!lb_table)
330                 lb_table = find_lb_table(low_1MB, 0xf0000, 1024*1024);
331         if (lb_table) {
332                 unsigned long addr;
333                 addr = ((char *)lb_table) - ((char *)low_1MB);
334                 print_lb_table(lb_table, addr);
335         }
336         else {
337                 printf("lb_table not found\n");
338         }
339         return 0;
340 }