10 #include "../../src/include/boot/linuxbios_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 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",
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 LinuxBIOS 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 value)
111 if (value > 1024ULL*1024*1024*1024*1024*1024) {
112 value /= 1024ULL*1024*1024*1024*1024*1024;
113 fprintf(stream, "%lldEB", value);
115 else if (value > 1024ULL*1024*1024*1024*1024) {
116 value /= 1024ULL*1024*1024*1024*1024;
117 fprintf(stream, "%lldPB", value);
119 else if (value > 1024ULL*1024*1024*1024) {
120 value /= 1024ULL*1024*1024*1024;
121 fprintf(stream, "%lldTB", value);
123 else if (value > 1024ULL*1024*1024) {
124 value /= 1024ULL*1024*1024;
125 fprintf(stream, "%lldGB", value);
127 else if (value > 1024ULL*1024) {
128 value /= 1024ULL*1024;
129 fprintf(stream, "%lldMB", value);
131 else if (value > 1024ULL) {
133 fprintf(stream, "%lldKB", value);
136 fprintf(stream, "%lldB", value);
140 void print_memory(struct lb_record *ptr, unsigned long addr)
142 struct lb_memory *rec = (void *)ptr;
145 entries = (rec->size - sizeof(*rec))/sizeof(rec->map[0]);
146 for(i = 0; i < entries; i++) {
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;
157 case 2: mem_type = "reserved"; break;
159 printf("0x%08llx - 0x%08llx %s (",
160 start, end, mem_type);
161 pretty_print_number(stdout, start);
163 pretty_print_number(stdout, end);
168 void print_mainboard(struct lb_record *ptr, unsigned long addr)
170 struct lb_mainboard *rec;
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);
179 void print_string(struct lb_record *ptr, unsigned long addr)
181 struct lb_string *rec;
183 rec = (struct lb_string *)ptr;
184 max_size = rec->size - sizeof(*rec);
185 printf("%.*s\n", max_size, rec->string);
188 void print_option_table(struct lb_record *ptr, unsigned long addr)
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);
200 unsigned char *data = (unsigned char *)ptr;
202 for(i = 0; i < hdr->size; i++) {
204 fprintf(stderr, "\n\t");
206 fprintf(stderr, "0x%02x,", data[i]);
213 void print_option(struct lb_record *ptr, unsigned long addr)
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);
222 void print_option_enumeration(struct lb_record *ptr, unsigned long addr)
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,
231 void print_option_checksum(struct lb_record *ptr, unsigned long addr)
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",
237 rec->range_start, rec->range_end, rec->location, rec->type);
243 void (*print)(struct lb_record *rec, unsigned long addr);
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 },
267 static struct lb_record *next_record(struct lb_record *rec)
269 return (struct lb_record *)(((char *)rec) + rec->size);
272 void print_lb_records(struct lb_record *rec, struct lb_record *last,
275 struct lb_record *next;
280 for(next = next_record(rec); (rec < last) && (next <= last);
281 rec = next, addr += rec->size) {
282 next = next_record(rec);
284 for(i = 0; lb_types[i].print != 0; i++) {
285 if (lb_types[i].type == rec->tag) {
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);
297 void print_lb_table(struct lb_header *head, unsigned long addr)
299 struct lb_record *rec, *last;
301 rec = (struct lb_record *)(((char *)head) + head->header_bytes);
302 last = (struct lb_record *)(((char *)rec) + head->table_bytes);
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);
310 int main(int argc, char **argv)
312 unsigned char *low_1MB;
313 struct lb_header *lb_table;
315 fd = open("/dev/mem", O_RDONLY);
317 fprintf(stderr, "Can not open /dev/mem\n");
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));
328 lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
330 lb_table = find_lb_table(low_1MB, 0xf0000, 1024*1024);
333 addr = ((char *)lb_table) - ((char *)low_1MB);
334 print_lb_table(lb_table, addr);
337 printf("lb_table not found\n");