blob: 2adf5c9c360ca4316fcaa34bba30bb168d6b9f0a [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
14char *lb_part=NULL, *lb_vendor=NULL;
15
16static unsigned long compute_checksum(void *addr, unsigned long length)
17{
18 uint8_t *ptr;
19 volatile union {
20 uint8_t byte[2];
21 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;
30 for(i = 0; i < length; i++) {
31 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)); \
53 rec = (struct lb_record *)(((char *)rec) + rec->size))
54
55
56static int count_lb_records(struct lb_header *head)
57{
58 struct lb_record *rec;
59 int count;
60 count = 0;
61 for_each_lbrec(head, rec) {
62 count++;
63 }
64 return count;
65}
66
67
68static struct lb_header *find_lb_table(void *base, unsigned long start, unsigned long end)
69{
70 unsigned long addr;
71 /* For now be stupid.... */
72 for(addr = start; addr < end; addr += 16) {
73 struct lb_header *head = (struct lb_header *)(((char*)base) + addr);
74 struct lb_record *recs = (struct lb_record *)(((char*)base) + addr + sizeof(*head));
75 if (memcmp(head->signature, "LBIO", 4) != 0)
76 continue;
77 printf_debug( "Found canidate at: %08lx-%08lx\n",
78 addr, addr + head->table_bytes);
79 if (head->header_bytes != sizeof(*head)) {
80 fprintf(stderr, "Header bytes of %d are incorrect\n",
81 head->header_bytes);
82 continue;
83 }
84 if (count_lb_records(head) != head->table_entries) {
85 fprintf(stderr, "bad record count: %d\n",
86 head->table_entries);
87 continue;
88 }
89 if (compute_checksum((uint8_t *)head, sizeof(*head)) != 0) {
90 fprintf(stderr, "bad header checksum\n");
91 continue;
92 }
93 if (compute_checksum(recs, head->table_bytes)
94 != head->table_checksum) {
95 fprintf(stderr, "bad table checksum: %04x\n",
96 head->table_checksum);
97 continue;
98 }
99 fprintf(stdout, "Found LinuxBIOS table at: %08lx\n", addr);
100 return head;
101
102 };
103 return 0;
104}
105
106static void find_mainboard(struct lb_record *ptr, unsigned long addr)
107{
108 struct lb_mainboard *rec;
109 int max_size;
110 char vendor[256], part[256];
111 rec = (struct lb_mainboard *)ptr;
112 max_size = rec->size - sizeof(*rec);
113 printf("vendor id: %.*s part id: %.*s\n",
114 max_size - rec->vendor_idx,
115 rec->strings + rec->vendor_idx,
116 max_size - rec->part_number_idx,
117 rec->strings + rec->part_number_idx);
118 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx,
119 rec->strings + rec->vendor_idx);
120 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
121 rec->strings + rec->part_number_idx);
122
123 if(lb_part) {
124 printf("overwritten by command line, vendor id: %s part id: %s\n",
125 lb_vendor, lb_part);
126 } else {
127 lb_part=strdup(part);
128 lb_vendor=strdup(vendor);
129 }
130}
131
132static struct lb_record *next_record(struct lb_record *rec)
133{
134 return (struct lb_record *)(((char *)rec) + rec->size);
135}
136
137static void search_lb_records(struct lb_record *rec, struct lb_record *last,
138 unsigned long addr)
139{
140 struct lb_record *next;
141 int count;
142 count = 0;
143
144 for(next = next_record(rec); (rec < last) && (next <= last);
145 rec = next, addr += rec->size) {
146 next = next_record(rec);
147 count++;
148 if(rec->tag == LB_TAG_MAINBOARD) {
149 find_mainboard(rec,addr);
150 break;
151 }
152 }
153}
154
155int linuxbios_init(void)
156{
157 uint8_t *low_1MB;
158 struct lb_header *lb_table;
159 struct lb_record *rec, *last;
160
161 int fd;
Adam Kaufman064b1f22007-02-06 19:47:50 +0000162 fd = open(MEM_DEV, O_RDONLY);
Ollie Lho184a4042005-11-26 21:55:36 +0000163 if (fd < 0) {
Adam Kaufman064b1f22007-02-06 19:47:50 +0000164 fprintf(stderr, "Can not access memory using " MEM_DEV "\n");
Ollie Lho184a4042005-11-26 21:55:36 +0000165 exit(-1);
166 }
167 low_1MB = mmap(0, 1024*1024, PROT_READ, MAP_SHARED, fd, 0x00000000);
168 if (low_1MB == ((void *) -1)) {
Adam Kaufman064b1f22007-02-06 19:47:50 +0000169 fprintf(stderr, "Can not mmap " MEM_DEV " at %08lx errno(%d):%s\n",
Ollie Lho184a4042005-11-26 21:55:36 +0000170 0x00000000UL, errno, strerror(errno));
171 exit(-2);
172 }
173 lb_table = 0;
174 if (!lb_table)
175 lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
176 if (!lb_table)
177 lb_table = find_lb_table(low_1MB, 0xf0000, 1024*1024);
178 if (lb_table) {
179 unsigned long addr;
180 addr = ((char *)lb_table) - ((char *)low_1MB);
181 printf_debug("lb_table found at address %p\n", lb_table);
182 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
183 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
184 printf_debug("LinuxBIOS header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
185 lb_table->header_bytes, lb_table->header_checksum,
186 lb_table->table_bytes, lb_table->table_checksum, lb_table->table_entries);
187 search_lb_records(rec, last, addr + lb_table->header_bytes);
188 }
189 else {
190 printf("No LinuxBIOS table found.\n");
191 return -1;
192 }
193 return 0;
194}