blob: d65d1a0c4d6b79ce4ad93575a750b5cbd539779c [file] [log] [blame]
Ollie Lho184a4042005-11-26 21:55:36 +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>
Adam Kaufman064b1f22007-02-06 19:47:50 +000010#include "flash.h"
Uwe Hermannce1041c2007-02-06 19:53:51 +000011#include "linuxbios_tables.h"
Ollie Lho184a4042005-11-26 21:55:36 +000012#include "debug.h"
13
Uwe Hermanna7e05482007-05-09 10:17:44 +000014char *lb_part = NULL, *lb_vendor = NULL;
Ollie Lho184a4042005-11-26 21:55:36 +000015
16static unsigned long compute_checksum(void *addr, unsigned long length)
17{
18 uint8_t *ptr;
19 volatile union {
Uwe Hermanna7e05482007-05-09 10:17:44 +000020 uint8_t byte[2];
Ollie Lho184a4042005-11-26 21:55:36 +000021 uint16_t word;
22 } value;
23 unsigned long sum;
24 unsigned long i;
25 /* In the most straight forward way possible,
26 * compute an ip style checksum.
27 */
28 sum = 0;
29 ptr = addr;
Uwe Hermanna7e05482007-05-09 10:17:44 +000030 for (i = 0; i < length; i++) {
Ollie Lho184a4042005-11-26 21:55:36 +000031 unsigned long value;
32 value = ptr[i];
33 if (i & 1) {
34 value <<= 8;
35 }
36 /* Add the new value */
37 sum += value;
38 /* Wrap around the carry */
39 if (sum > 0xFFFF) {
40 sum = (sum + (sum >> 16)) & 0xFFFF;
41 }
42 }
43 value.byte[0] = sum & 0xff;
44 value.byte[1] = (sum >> 8) & 0xff;
45 return (~value.word) & 0xFFFF;
46}
47
48#define for_each_lbrec(head, rec) \
49 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
50 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
51 (rec->size >= 1) && \
52 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
Uwe Hermanna7e05482007-05-09 10:17:44 +000053 rec = (struct lb_record *)(((char *)rec) + rec->size))
Ollie Lho184a4042005-11-26 21:55:36 +000054
Uwe Hermanna7e05482007-05-09 10:17:44 +000055static int count_lb_records(struct lb_header *head)
Ollie Lho184a4042005-11-26 21:55:36 +000056{
57 struct lb_record *rec;
58 int count;
59 count = 0;
60 for_each_lbrec(head, rec) {
61 count++;
62 }
63 return count;
64}
65
Uwe Hermanna7e05482007-05-09 10:17:44 +000066static struct lb_header *find_lb_table(void *base, unsigned long start,
67 unsigned long end)
Ollie Lho184a4042005-11-26 21:55:36 +000068{
69 unsigned long addr;
70 /* For now be stupid.... */
Uwe Hermanna7e05482007-05-09 10:17:44 +000071 for (addr = start; addr < end; addr += 16) {
72 struct lb_header *head =
73 (struct lb_header *)(((char *)base) + addr);
74 struct lb_record *recs =
75 (struct lb_record *)(((char *)base) + addr + sizeof(*head));
Ollie Lho184a4042005-11-26 21:55:36 +000076 if (memcmp(head->signature, "LBIO", 4) != 0)
77 continue;
Uwe Hermanna7e05482007-05-09 10:17:44 +000078 printf_debug("Found canidate at: %08lx-%08lx\n",
79 addr, addr + head->table_bytes);
Ollie Lho184a4042005-11-26 21:55:36 +000080 if (head->header_bytes != sizeof(*head)) {
81 fprintf(stderr, "Header bytes of %d are incorrect\n",
82 head->header_bytes);
83 continue;
84 }
85 if (count_lb_records(head) != head->table_entries) {
86 fprintf(stderr, "bad record count: %d\n",
87 head->table_entries);
88 continue;
89 }
Uwe Hermanna7e05482007-05-09 10:17:44 +000090 if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
Ollie Lho184a4042005-11-26 21:55:36 +000091 fprintf(stderr, "bad header checksum\n");
92 continue;
93 }
94 if (compute_checksum(recs, head->table_bytes)
Uwe Hermanna7e05482007-05-09 10:17:44 +000095 != head->table_checksum) {
Ollie Lho184a4042005-11-26 21:55:36 +000096 fprintf(stderr, "bad table checksum: %04x\n",
97 head->table_checksum);
98 continue;
99 }
100 fprintf(stdout, "Found LinuxBIOS table at: %08lx\n", addr);
101 return head;
102
103 };
104 return 0;
105}
106
107static void find_mainboard(struct lb_record *ptr, unsigned long addr)
108{
109 struct lb_mainboard *rec;
110 int max_size;
111 char vendor[256], part[256];
112 rec = (struct lb_mainboard *)ptr;
113 max_size = rec->size - sizeof(*rec);
114 printf("vendor id: %.*s part id: %.*s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000115 max_size - rec->vendor_idx,
116 rec->strings + rec->vendor_idx,
117 max_size - rec->part_number_idx,
118 rec->strings + rec->part_number_idx);
119 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx,
120 rec->strings + rec->vendor_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000121 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000122 rec->strings + rec->part_number_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000123
Uwe Hermanna7e05482007-05-09 10:17:44 +0000124 if (lb_part) {
125 printf("overwritten by command line, vendor id: %s part id: %s\n", lb_vendor, lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000126 } else {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000127 lb_part = strdup(part);
128 lb_vendor = strdup(vendor);
Ollie Lho184a4042005-11-26 21:55:36 +0000129 }
130}
131
132static struct lb_record *next_record(struct lb_record *rec)
133{
134 return (struct lb_record *)(((char *)rec) + rec->size);
135}
136
Uwe Hermanna7e05482007-05-09 10:17:44 +0000137static void search_lb_records(struct lb_record *rec, struct lb_record *last,
138 unsigned long addr)
Ollie Lho184a4042005-11-26 21:55:36 +0000139{
140 struct lb_record *next;
141 int count;
142 count = 0;
143
Uwe Hermanna7e05482007-05-09 10:17:44 +0000144 for (next = next_record(rec); (rec < last) && (next <= last);
145 rec = next, addr += rec->size) {
Ollie Lho184a4042005-11-26 21:55:36 +0000146 next = next_record(rec);
147 count++;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000148 if (rec->tag == LB_TAG_MAINBOARD) {
149 find_mainboard(rec, addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000150 break;
151 }
152 }
153}
154
Uwe Hermanna7e05482007-05-09 10:17:44 +0000155int linuxbios_init(void)
Ollie Lho184a4042005-11-26 21:55:36 +0000156{
157 uint8_t *low_1MB;
158 struct lb_header *lb_table;
159 struct lb_record *rec, *last;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000160
161 low_1MB = mmap(0, 1024 * 1024, PROT_READ, MAP_SHARED, fd_mem,
162 0x00000000);
Stefan Reinauer70385642007-04-06 11:58:03 +0000163 if (low_1MB == MAP_FAILED) {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000164 fprintf(stderr,
165 "Can not mmap " MEM_DEV " at %08lx errno(%d):%s\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000166 0x00000000UL, errno, strerror(errno));
167 exit(-2);
168 }
169 lb_table = 0;
170 if (!lb_table)
171 lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
172 if (!lb_table)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000173 lb_table = find_lb_table(low_1MB, 0xf0000, 1024 * 1024);
Ollie Lho184a4042005-11-26 21:55:36 +0000174 if (lb_table) {
175 unsigned long addr;
176 addr = ((char *)lb_table) - ((char *)low_1MB);
177 printf_debug("lb_table found at address %p\n", lb_table);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000178 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
Ollie Lho184a4042005-11-26 21:55:36 +0000179 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
180 printf_debug("LinuxBIOS header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000181 lb_table->header_bytes, lb_table->header_checksum,
182 lb_table->table_bytes, lb_table->table_checksum,
183 lb_table->table_entries);
Ollie Lho184a4042005-11-26 21:55:36 +0000184 search_lb_records(rec, last, addr + lb_table->header_bytes);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000185 } else {
Ollie Lho184a4042005-11-26 21:55:36 +0000186 printf("No LinuxBIOS table found.\n");
187 return -1;
188 }
189 return 0;
190}