blob: dd005e249cef95c1b69de62c193d6f5b24e36ea9 [file] [log] [blame]
Stefan Reinauer5380d512007-05-24 09:08:36 +00001/*
2 * lbtable.c: LinuxBIOS table handling
3 *
4 * Copyright (C) 2002 Steven James <pyro@linuxlabs.com>
5 * Copyright (C) 2002 Linux Networx
6 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
7 * Copyright (C) 2006-2007 coresystems GmbH
8 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
22 */
23
Ollie Lho184a4042005-11-26 21:55:36 +000024#include <stdio.h>
25#include <stdint.h>
26#include <stdlib.h>
27#include <fcntl.h>
28#include <unistd.h>
29#include <sys/types.h>
30#include <string.h>
31#include <errno.h>
32#include <sys/mman.h>
Adam Kaufman064b1f22007-02-06 19:47:50 +000033#include "flash.h"
Uwe Hermannce1041c2007-02-06 19:53:51 +000034#include "linuxbios_tables.h"
Ollie Lho184a4042005-11-26 21:55:36 +000035#include "debug.h"
36
Uwe Hermanna7e05482007-05-09 10:17:44 +000037char *lb_part = NULL, *lb_vendor = NULL;
Ollie Lho184a4042005-11-26 21:55:36 +000038
39static unsigned long compute_checksum(void *addr, unsigned long length)
40{
41 uint8_t *ptr;
42 volatile union {
Uwe Hermanna7e05482007-05-09 10:17:44 +000043 uint8_t byte[2];
Ollie Lho184a4042005-11-26 21:55:36 +000044 uint16_t word;
45 } value;
46 unsigned long sum;
47 unsigned long i;
48 /* In the most straight forward way possible,
49 * compute an ip style checksum.
50 */
51 sum = 0;
52 ptr = addr;
Uwe Hermanna7e05482007-05-09 10:17:44 +000053 for (i = 0; i < length; i++) {
Ollie Lho184a4042005-11-26 21:55:36 +000054 unsigned long value;
55 value = ptr[i];
56 if (i & 1) {
57 value <<= 8;
58 }
59 /* Add the new value */
60 sum += value;
61 /* Wrap around the carry */
62 if (sum > 0xFFFF) {
63 sum = (sum + (sum >> 16)) & 0xFFFF;
64 }
65 }
66 value.byte[0] = sum & 0xff;
67 value.byte[1] = (sum >> 8) & 0xff;
68 return (~value.word) & 0xFFFF;
69}
70
71#define for_each_lbrec(head, rec) \
72 for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
73 (((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes)) && \
74 (rec->size >= 1) && \
75 ((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
Uwe Hermanna7e05482007-05-09 10:17:44 +000076 rec = (struct lb_record *)(((char *)rec) + rec->size))
Ollie Lho184a4042005-11-26 21:55:36 +000077
Uwe Hermanna7e05482007-05-09 10:17:44 +000078static int count_lb_records(struct lb_header *head)
Ollie Lho184a4042005-11-26 21:55:36 +000079{
80 struct lb_record *rec;
81 int count;
82 count = 0;
83 for_each_lbrec(head, rec) {
84 count++;
85 }
86 return count;
87}
88
Uwe Hermanna7e05482007-05-09 10:17:44 +000089static struct lb_header *find_lb_table(void *base, unsigned long start,
90 unsigned long end)
Ollie Lho184a4042005-11-26 21:55:36 +000091{
92 unsigned long addr;
93 /* For now be stupid.... */
Uwe Hermanna7e05482007-05-09 10:17:44 +000094 for (addr = start; addr < end; addr += 16) {
95 struct lb_header *head =
96 (struct lb_header *)(((char *)base) + addr);
97 struct lb_record *recs =
98 (struct lb_record *)(((char *)base) + addr + sizeof(*head));
Ollie Lho184a4042005-11-26 21:55:36 +000099 if (memcmp(head->signature, "LBIO", 4) != 0)
100 continue;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000101 printf_debug("Found canidate at: %08lx-%08lx\n",
102 addr, addr + head->table_bytes);
Ollie Lho184a4042005-11-26 21:55:36 +0000103 if (head->header_bytes != sizeof(*head)) {
104 fprintf(stderr, "Header bytes of %d are incorrect\n",
105 head->header_bytes);
106 continue;
107 }
108 if (count_lb_records(head) != head->table_entries) {
109 fprintf(stderr, "bad record count: %d\n",
110 head->table_entries);
111 continue;
112 }
Uwe Hermanna7e05482007-05-09 10:17:44 +0000113 if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
Ollie Lho184a4042005-11-26 21:55:36 +0000114 fprintf(stderr, "bad header checksum\n");
115 continue;
116 }
117 if (compute_checksum(recs, head->table_bytes)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000118 != head->table_checksum) {
Ollie Lho184a4042005-11-26 21:55:36 +0000119 fprintf(stderr, "bad table checksum: %04x\n",
120 head->table_checksum);
121 continue;
122 }
123 fprintf(stdout, "Found LinuxBIOS table at: %08lx\n", addr);
124 return head;
125
126 };
127 return 0;
128}
129
130static void find_mainboard(struct lb_record *ptr, unsigned long addr)
131{
132 struct lb_mainboard *rec;
133 int max_size;
134 char vendor[256], part[256];
135 rec = (struct lb_mainboard *)ptr;
136 max_size = rec->size - sizeof(*rec);
137 printf("vendor id: %.*s part id: %.*s\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000138 max_size - rec->vendor_idx,
139 rec->strings + rec->vendor_idx,
140 max_size - rec->part_number_idx,
141 rec->strings + rec->part_number_idx);
142 snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx,
143 rec->strings + rec->vendor_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000144 snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
Uwe Hermanna7e05482007-05-09 10:17:44 +0000145 rec->strings + rec->part_number_idx);
Ollie Lho184a4042005-11-26 21:55:36 +0000146
Uwe Hermanna7e05482007-05-09 10:17:44 +0000147 if (lb_part) {
148 printf("overwritten by command line, vendor id: %s part id: %s\n", lb_vendor, lb_part);
Ollie Lho184a4042005-11-26 21:55:36 +0000149 } else {
Uwe Hermanna7e05482007-05-09 10:17:44 +0000150 lb_part = strdup(part);
151 lb_vendor = strdup(vendor);
Ollie Lho184a4042005-11-26 21:55:36 +0000152 }
153}
154
155static struct lb_record *next_record(struct lb_record *rec)
156{
157 return (struct lb_record *)(((char *)rec) + rec->size);
158}
159
Uwe Hermanna7e05482007-05-09 10:17:44 +0000160static void search_lb_records(struct lb_record *rec, struct lb_record *last,
161 unsigned long addr)
Ollie Lho184a4042005-11-26 21:55:36 +0000162{
163 struct lb_record *next;
164 int count;
165 count = 0;
166
Uwe Hermanna7e05482007-05-09 10:17:44 +0000167 for (next = next_record(rec); (rec < last) && (next <= last);
168 rec = next, addr += rec->size) {
Ollie Lho184a4042005-11-26 21:55:36 +0000169 next = next_record(rec);
170 count++;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000171 if (rec->tag == LB_TAG_MAINBOARD) {
172 find_mainboard(rec, addr);
Ollie Lho184a4042005-11-26 21:55:36 +0000173 break;
174 }
175 }
176}
177
Uwe Hermanna7e05482007-05-09 10:17:44 +0000178int linuxbios_init(void)
Ollie Lho184a4042005-11-26 21:55:36 +0000179{
180 uint8_t *low_1MB;
181 struct lb_header *lb_table;
182 struct lb_record *rec, *last;
Uwe Hermanna7e05482007-05-09 10:17:44 +0000183
184 low_1MB = mmap(0, 1024 * 1024, PROT_READ, MAP_SHARED, fd_mem,
185 0x00000000);
Stefan Reinauer70385642007-04-06 11:58:03 +0000186 if (low_1MB == MAP_FAILED) {
Stefan Reinauer7c1402f2007-05-23 18:24:58 +0000187 perror("Can't mmap memory using " MEM_DEV);
Ollie Lho184a4042005-11-26 21:55:36 +0000188 exit(-2);
189 }
190 lb_table = 0;
191 if (!lb_table)
192 lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
193 if (!lb_table)
Uwe Hermanna7e05482007-05-09 10:17:44 +0000194 lb_table = find_lb_table(low_1MB, 0xf0000, 1024 * 1024);
Ollie Lho184a4042005-11-26 21:55:36 +0000195 if (lb_table) {
196 unsigned long addr;
197 addr = ((char *)lb_table) - ((char *)low_1MB);
198 printf_debug("lb_table found at address %p\n", lb_table);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000199 rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
Ollie Lho184a4042005-11-26 21:55:36 +0000200 last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
201 printf_debug("LinuxBIOS header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
Uwe Hermanna7e05482007-05-09 10:17:44 +0000202 lb_table->header_bytes, lb_table->header_checksum,
203 lb_table->table_bytes, lb_table->table_checksum,
204 lb_table->table_entries);
Ollie Lho184a4042005-11-26 21:55:36 +0000205 search_lb_records(rec, last, addr + lb_table->header_bytes);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000206 } else {
Ollie Lho184a4042005-11-26 21:55:36 +0000207 printf("No LinuxBIOS table found.\n");
208 return -1;
209 }
210 return 0;
211}