cbtable.c 6.37 KB
Newer Older
Stefan Reinauer's avatar
Stefan Reinauer committed
1
/*
2
 * This file is part of the flashrom project.
Stefan Reinauer's avatar
Stefan Reinauer committed
3 4 5 6
 *
 * Copyright (C) 2002 Steven James <pyro@linuxlabs.com>
 * Copyright (C) 2002 Linux Networx
 * (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
7
 * Copyright (C) 2006-2009 coresystems GmbH
Stefan Reinauer's avatar
Stefan Reinauer committed
8 9 10 11 12 13 14 15 16 17 18 19 20
 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
Stefan Reinauer's avatar
Stefan Reinauer committed
22 23
 */

24 25 26 27 28 29
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
#include <errno.h>
#include <sys/mman.h>
30
#include "flash.h"
31
#include "coreboot_tables.h"
32

33
char *lb_part = NULL, *lb_vendor = NULL;
34 35 36 37 38

static unsigned long compute_checksum(void *addr, unsigned long length)
{
	uint8_t *ptr;
	volatile union {
39
		uint8_t byte[2];
40 41
		uint16_t word;
	} value;
Uwe Hermann's avatar
Uwe Hermann committed
42 43
	unsigned long sum;
	unsigned long i;
Uwe Hermann's avatar
Uwe Hermann committed
44

45 46 47 48 49
	/* In the most straight forward way possible,
	 * compute an ip style checksum.
	 */
	sum = 0;
	ptr = addr;
50
	for (i = 0; i < length; i++) {
51 52
		unsigned long value;
		value = ptr[i];
Uwe Hermann's avatar
Uwe Hermann committed
53
		if (i & 1) {
54
			value <<= 8;
Uwe Hermann's avatar
Uwe Hermann committed
55
		}
56 57 58
		/* Add the new value */
		sum += value;
		/* Wrap around the carry */
Uwe Hermann's avatar
Uwe Hermann committed
59
		if (sum > 0xFFFF) {
60
			sum = (sum + (sum >> 16)) & 0xFFFF;
Uwe Hermann's avatar
Uwe Hermann committed
61
		}
62 63 64
	}
	value.byte[0] = sum & 0xff;
	value.byte[1] = (sum >> 8) & 0xff;
Uwe Hermann's avatar
Uwe Hermann committed
65

66 67 68 69 70 71 72 73
	return (~value.word) & 0xFFFF;
}

#define for_each_lbrec(head, rec) \
	for(rec = (struct lb_record *)(((char *)head) + sizeof(*head)); \
		(((char *)rec) < (((char *)head) + sizeof(*head) + head->table_bytes))  && \
		(rec->size >= 1) && \
		((((char *)rec) + rec->size) <= (((char *)head) + sizeof(*head) + head->table_bytes)); \
74
		rec = (struct lb_record *)(((char *)rec) + rec->size))
75

76
static int count_lb_records(struct lb_header *head)
77 78 79
{
	struct lb_record *rec;
	int count;
Uwe Hermann's avatar
Uwe Hermann committed
80

81 82 83 84
	count = 0;
	for_each_lbrec(head, rec) {
		count++;
	}
Uwe Hermann's avatar
Uwe Hermann committed
85

86 87 88
	return count;
}

89 90
static struct lb_header *find_lb_table(void *base, unsigned long start,
				       unsigned long end)
91 92
{
	unsigned long addr;
Uwe Hermann's avatar
Uwe Hermann committed
93

94
	/* For now be stupid.... */
95 96 97 98 99
	for (addr = start; addr < end; addr += 16) {
		struct lb_header *head =
		    (struct lb_header *)(((char *)base) + addr);
		struct lb_record *recs =
		    (struct lb_record *)(((char *)base) + addr + sizeof(*head));
100 101
		if (memcmp(head->signature, "LBIO", 4) != 0)
			continue;
102
		printf_debug("Found candidate at: %08lx-%08lx\n",
103
			     addr, addr + head->table_bytes);
104
		if (head->header_bytes != sizeof(*head)) {
105
			fprintf(stderr, "Header bytes of %d are incorrect.\n",
106 107 108 109
				head->header_bytes);
			continue;
		}
		if (count_lb_records(head) != head->table_entries) {
110
			fprintf(stderr, "Bad record count: %d.\n",
111 112 113
				head->table_entries);
			continue;
		}
114
		if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
115
			fprintf(stderr, "Bad header checksum.\n");
116 117 118
			continue;
		}
		if (compute_checksum(recs, head->table_bytes)
119
		    != head->table_checksum) {
120
			fprintf(stderr, "Bad table checksum: %04x.\n",
121 122 123
				head->table_checksum);
			continue;
		}
124
		printf_debug("Found coreboot table at 0x%08lx.\n", addr);
125 126 127
		return head;

	};
Uwe Hermann's avatar
Uwe Hermann committed
128

129 130 131 132 133 134 135 136
	return 0;
}

static void find_mainboard(struct lb_record *ptr, unsigned long addr)
{
	struct lb_mainboard *rec;
	int max_size;
	char vendor[256], part[256];
Uwe Hermann's avatar
Uwe Hermann committed
137

138 139
	rec = (struct lb_mainboard *)ptr;
	max_size = rec->size - sizeof(*rec);
140
	printf("Vendor ID: %.*s, part ID: %.*s\n",
141 142 143 144 145 146
	       max_size - rec->vendor_idx,
	       rec->strings + rec->vendor_idx,
	       max_size - rec->part_number_idx,
	       rec->strings + rec->part_number_idx);
	snprintf(vendor, 255, "%.*s", max_size - rec->vendor_idx,
		 rec->strings + rec->vendor_idx);
147
	snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
148
		 rec->strings + rec->part_number_idx);
149

150
	if (lb_part) {
151
		printf("Overwritten by command line, vendor ID: %s, part ID: %s.\n", lb_vendor, lb_part);
152
	} else {
153 154
		lb_part = strdup(part);
		lb_vendor = strdup(vendor);
155 156 157 158 159 160 161 162
	}
}

static struct lb_record *next_record(struct lb_record *rec)
{
	return (struct lb_record *)(((char *)rec) + rec->size);
}

163 164
static void search_lb_records(struct lb_record *rec, struct lb_record *last,
			      unsigned long addr)
165 166 167 168 169
{
	struct lb_record *next;
	int count;
	count = 0;

170 171
	for (next = next_record(rec); (rec < last) && (next <= last);
	     rec = next, addr += rec->size) {
172 173
		next = next_record(rec);
		count++;
174 175
		if (rec->tag == LB_TAG_MAINBOARD) {
			find_mainboard(rec, addr);
176 177 178 179 180
			break;
		}
	}
}

181
#define BYTES_TO_MAP (1024*1024)
Stefan Reinauer's avatar
Stefan Reinauer committed
182
int coreboot_init(void)
183
{
184
	uint8_t *table_area;
Stefan Reinauer's avatar
Stefan Reinauer committed
185
	unsigned long addr, start;
186 187
	struct lb_header *lb_table;
	struct lb_record *rec, *last;
188

Stefan Reinauer's avatar
Stefan Reinauer committed
189 190 191 192 193 194 195 196
#ifdef __DARWIN__
	/* This is a hack. DirectIO fails to map physical address 0x00000000.
	 * Why?
	 */
	start = 0x400;
#else
	start = 0x0;
#endif
197
	table_area = physmap("low megabyte", start, BYTES_TO_MAP);
Stefan Reinauer's avatar
Stefan Reinauer committed
198

199
	lb_table = find_lb_table(table_area, 0x00000, 0x1000);
200
	if (!lb_table)
201 202 203 204 205 206
		lb_table = find_lb_table(table_area, 0xf0000, BYTES_TO_MAP);
	if (lb_table) {
		struct lb_forward *forward = (struct lb_forward *)
			(((char *)lb_table) + lb_table->header_bytes);
		if (forward->tag == LB_TAG_FORWARD) {
			start = forward->forward;
207
			start &= ~(getpagesize() - 1);
208 209 210 211 212 213
			physunmap(table_area, BYTES_TO_MAP);
			table_area = physmap("high tables", start, BYTES_TO_MAP);
			lb_table = find_lb_table(table_area, 0x00000, 0x1000);
		}
	}

214
	if (!lb_table) {
Stefan Reinauer's avatar
Stefan Reinauer committed
215
		printf("No coreboot table found.\n");
216 217
		return -1;
	}
Uwe Hermann's avatar
Uwe Hermann committed
218

219 220 221
	addr = ((char *)lb_table) - ((char *)table_area) + start;
	fprintf(stdout, "coreboot table found at 0x%lx.\n", 
		(unsigned long)lb_table - (unsigned long)table_area + start);
222 223
	rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
	last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
Peter Stuge's avatar
Peter Stuge committed
224
	printf_debug("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
225 226 227 228 229
	     lb_table->header_bytes, lb_table->header_checksum,
	     lb_table->table_bytes, lb_table->table_checksum,
	     lb_table->table_entries);
	search_lb_records(rec, last, addr + lb_table->header_bytes);

230 231
	return 0;
}