blob: f98267273eb4547227f51f4ed6b6aa29f34a07d5 [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>
10#include "../../src/include/boot/linuxbios_tables.h"
11#include "debug.h"
12
13char *lb_part=NULL, *lb_vendor=NULL;
14
15static unsigned long compute_checksum(void *addr, unsigned long length)
16{
17 uint8_t *ptr;
18 volatile union {
19 uint8_t byte[2];
20 uint16_t word;
21 } value;
22 unsigned long sum;
23 unsigned long i;
24 /* In the most straight forward way possible,
25 * compute an ip style checksum.
26 */
27 sum = 0;
28 ptr = addr;
29 for(i = 0; i < length; i++) {
30 unsigned long value;
31 value = ptr[i];
32 if (i & 1) {
33 value <<= 8;
34 }
35 /* Add the new value */
36 sum += value;
37 /* Wrap around the carry */
38 if (sum > 0xFFFF) {
39 sum = (sum + (sum >> 16)) & 0xFFFF;
40 }
41 }
42 value.byte[0] = sum & 0xff;
43 value.byte[1] = (sum >> 8) & 0xff;
44 return (~value.word) & 0xFFFF;
45}
46
47#define for_each_lbrec(head, rec) \
48 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
49 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
50 (rec->size >= 1) && \
51 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
52 rec = (struct lb_record *)(((char *)rec) + rec->size))
53
54
55static int count_lb_records(struct lb_header *head)
56{
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
66
67static struct lb_header *find_lb_table(void *base, unsigned long start, unsigned long end)
68{
69 unsigned long addr;
70 /* For now be stupid.... */
71 for(addr = start; addr < end; addr += 16) {
72 struct lb_header *head = (struct lb_header *)(((char*)base) + addr);
73 struct lb_record *recs = (struct lb_record *)(((char*)base) + addr + sizeof(*head));
74 if (memcmp(head->signature, "LBIO", 4) != 0)
75 continue;
76 printf_debug( "Found canidate at: %08lx-%08lx\n",
77 addr, addr + head->table_bytes);
78 if (head->header_bytes != sizeof(*head)) {
79 fprintf(stderr, "Header bytes of %d are incorrect\n",
80 head->header_bytes);
81 continue;
82 }
83 if (count_lb_records(head) != head->table_entries) {
84 fprintf(stderr, "bad record count: %d\n",
85 head->table_entries);
86 continue;
87 }
88 if (compute_checksum((uint8_t *)head, sizeof(*head)) != 0) {
89 fprintf(stderr, "bad header checksum\n");
90 continue;
91 }
92 if (compute_checksum(recs, head->table_bytes)
93 != head->table_checksum) {
94 fprintf(stderr, "bad table checksum: %04x\n",
95 head->table_checksum);
96 continue;
97 }
98 fprintf(stdout, "Found LinuxBIOS table at: %08lx\n", addr);
99 return head;
100
101 };
102 return 0;
103}
104
105static void find_mainboard(struct lb_record *ptr, unsigned long addr)
106{
107 struct lb_mainboard *rec;
108 int max_size;
109 char vendor[256], part[256];
110 rec = (struct lb_mainboard *)ptr;
111 max_size = rec->size - sizeof(*rec);
112 printf("vendor id: %.*s part id: %.*s\n",
113 max_size - rec->vendor_idx,
114 rec->strings + rec->vendor_idx,
115 max_size - rec->part_number_idx,
116 rec->strings + rec->part_number_idx);
117 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx,
118 rec->strings + rec->vendor_idx);
119 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
120 rec->strings + rec->part_number_idx);
121
122 if(lb_part) {
123 printf("overwritten by command line, vendor id: %s part id: %s\n",
124 lb_vendor, lb_part);
125 } else {
126 lb_part=strdup(part);
127 lb_vendor=strdup(vendor);
128 }
129}
130
131static struct lb_record *next_record(struct lb_record *rec)
132{
133 return (struct lb_record *)(((char *)rec) + rec->size);
134}
135
136static void search_lb_records(struct lb_record *rec, struct lb_record *last,
137 unsigned long addr)
138{
139 struct lb_record *next;
140 int count;
141 count = 0;
142
143 for(next = next_record(rec); (rec < last) && (next <= last);
144 rec = next, addr += rec->size) {
145 next = next_record(rec);
146 count++;
147 if(rec->tag == LB_TAG_MAINBOARD) {
148 find_mainboard(rec,addr);
149 break;
150 }
151 }
152}
153
154int linuxbios_init(void)
155{
156 uint8_t *low_1MB;
157 struct lb_header *lb_table;
158 struct lb_record *rec, *last;
159
160 int fd;
161 fd = open("/dev/mem", O_RDONLY);
162 if (fd < 0) {
163 fprintf(stderr, "Can not open /dev/mem\n");
164 exit(-1);
165 }
166 low_1MB = mmap(0, 1024*1024, PROT_READ, MAP_SHARED, fd, 0x00000000);
167 if (low_1MB == ((void *) -1)) {
168 fprintf(stderr, "Can not mmap /dev/mem at %08lx errno(%d):%s\n",
169 0x00000000UL, errno, strerror(errno));
170 exit(-2);
171 }
172 lb_table = 0;
173 if (!lb_table)
174 lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
175 if (!lb_table)
176 lb_table = find_lb_table(low_1MB, 0xf0000, 1024*1024);
177 if (lb_table) {
178 unsigned long addr;
179 addr = ((char *)lb_table) - ((char *)low_1MB);
180 printf_debug("lb_table found at address %p\n", lb_table);
181 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
182 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
183 printf_debug("LinuxBIOS header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
184 lb_table->header_bytes, lb_table->header_checksum,
185 lb_table->table_bytes, lb_table->table_checksum, lb_table->table_entries);
186 search_lb_records(rec, last, addr + lb_table->header_bytes);
187 }
188 else {
189 printf("No LinuxBIOS table found.\n");
190 return -1;
191 }
192 return 0;
193}