cbtable.c 9.41 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
 * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
9
 * Copyright (C) 2010 Carl-Daniel Hailfinger
Stefan Reinauer's avatar
Stefan Reinauer committed
10 11 12 13 14 15 16 17 18 19 20 21
 *
 * 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
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
Stefan Reinauer's avatar
Stefan Reinauer committed
23 24
 */

25 26
#include <unistd.h>
#include <stdio.h>
27
#include <ctype.h>
Carl-Daniel Hailfinger's avatar
Carl-Daniel Hailfinger committed
28
#include <strings.h>
29
#include <string.h>
30
#include "flash.h"
31
#include "programmer.h"
32
#include "coreboot_tables.h"
33

34
static char *cb_vendor = NULL, *cb_model = NULL;
35

36 37 38 39 40 41
/* Tries to find coreboot IDs in the supplied image and compares them to the current IDs.
 * Returns...
 * 	-1	if IDs in the image do not match the IDs embedded in the current firmware,
 * 	 0	if the IDs could not be found in the image or if they match correctly.
 */
int cb_check_image(uint8_t *image, int size)
42
{
43 44
	const char *image_vendor = NULL;
	const char *image_model = NULL;
45 46 47 48
	unsigned int *walk;
	unsigned int mb_part_offset, mb_vendor_offset;
	char *mb_part, *mb_vendor;

49
	walk = (unsigned int *)(image + size - 0x10);
50 51 52
	walk--;

	if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
53 54 55 56 57
		/* Some NVIDIA chipsets store chipset soft straps (IIRC Hypertransport init info etc.) in
		 * flash at exactly the location where coreboot image size, coreboot vendor name pointer and
		 * coreboot board name pointer are usually stored. In this case coreboot uses an alternate
		 * location for the coreboot image data. */
		walk = (unsigned int *)(image + size - 0x80);
58 59 60 61 62 63 64 65 66 67 68 69 70
		walk--;
	}

	/*
	 * Check if coreboot last image size is 0 or not a multiple of 1k or
	 * bigger than the chip or if the pointers to vendor ID or mainboard ID
	 * are outside the image of if the start of ID strings are nonsensical
	 * (nonprintable and not \0).
	 */
	mb_part_offset = *(walk - 1);
	mb_vendor_offset = *(walk - 2);
	if ((*walk) == 0 || ((*walk) & 0x3ff) != 0 || (*walk) > size ||
	    mb_part_offset > size || mb_vendor_offset > size) {
71
		msg_pdbg("Flash image seems to be a legacy BIOS. Disabling coreboot-related checks.\n");
72 73 74
		return 0;
	}

75 76
	mb_part = (char *)(image + size - mb_part_offset);
	mb_vendor = (char *)(image + size - mb_vendor_offset);
77 78
	if (!isprint((unsigned char)*mb_part) ||
	    !isprint((unsigned char)*mb_vendor)) {
79 80
		msg_pdbg("Flash image seems to have garbage in the ID location. "
			 "Disabling coreboot-related checks.\n");
81 82 83 84 85
		return 0;
	}

	msg_pdbg("coreboot last image size (not ROM size) is %d bytes.\n", *walk);

86 87 88 89
	image_vendor = strdup(mb_vendor);
	image_model = strdup(mb_part);
	msg_pdbg("Manufacturer: %s\n", image_vendor);
	msg_pdbg("Mainboard ID: %s\n", image_model);
90 91

	/* If these are not set, the coreboot table was not found. */
92
	if (!cb_vendor || !cb_model)
93 94 95
		return 0;

	/* These comparisons are case insensitive to make things a little less user^Werror prone. */
96 97
	if (!strcasecmp(image_vendor, cb_vendor) && !strcasecmp(image_model, cb_model)) {
		msg_pdbg2("This coreboot image matches this mainboard.\n");
98
	} else {
Stefan Tauner's avatar
Stefan Tauner committed
99 100 101
		msg_perr("This coreboot image (%s:%s) does not appear to\n"
			 "be correct for the detected mainboard (%s:%s).\n",
			 image_vendor, image_model, cb_vendor, cb_model);
102
		return -1;
103 104 105 106 107
	}

	return 0;
}

108 109 110 111
static unsigned long compute_checksum(void *addr, unsigned long length)
{
	uint8_t *ptr;
	volatile union {
112
		uint8_t byte[2];
113
		uint16_t word;
114
	} chksum;
Uwe Hermann's avatar
Uwe Hermann committed
115 116
	unsigned long sum;
	unsigned long i;
Uwe Hermann's avatar
Uwe Hermann committed
117

118 119 120 121 122
	/* In the most straight forward way possible,
	 * compute an ip style checksum.
	 */
	sum = 0;
	ptr = addr;
123
	for (i = 0; i < length; i++) {
124 125
		unsigned long value;
		value = ptr[i];
Uwe Hermann's avatar
Uwe Hermann committed
126
		if (i & 1) {
127
			value <<= 8;
Uwe Hermann's avatar
Uwe Hermann committed
128
		}
129 130 131
		/* Add the new value */
		sum += value;
		/* Wrap around the carry */
Uwe Hermann's avatar
Uwe Hermann committed
132
		if (sum > 0xFFFF) {
133
			sum = (sum + (sum >> 16)) & 0xFFFF;
Uwe Hermann's avatar
Uwe Hermann committed
134
		}
135
	}
136 137
	chksum.byte[0] = sum & 0xff;
	chksum.byte[1] = (sum >> 8) & 0xff;
Uwe Hermann's avatar
Uwe Hermann committed
138

139
	return (~chksum.word) & 0xFFFF;
140 141 142 143 144 145 146
}

#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)); \
147
		rec = (struct lb_record *)(((char *)rec) + rec->size))
148

149
static int count_lb_records(struct lb_header *head)
150 151 152
{
	struct lb_record *rec;
	int count;
Uwe Hermann's avatar
Uwe Hermann committed
153

154 155 156 157
	count = 0;
	for_each_lbrec(head, rec) {
		count++;
	}
Uwe Hermann's avatar
Uwe Hermann committed
158

159 160 161
	return count;
}

162 163
static struct lb_header *find_lb_table(void *base, unsigned long start,
				       unsigned long end)
164 165
{
	unsigned long addr;
Uwe Hermann's avatar
Uwe Hermann committed
166

167
	/* For now be stupid.... */
168 169 170 171 172
	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));
173 174
		if (memcmp(head->signature, "LBIO", 4) != 0)
			continue;
175
		msg_pdbg("Found candidate at: %08lx-%08lx\n",
176
			     addr, addr + head->table_bytes);
177
		if (head->header_bytes != sizeof(*head)) {
178
			msg_perr("Header bytes of %d are incorrect.\n",
179 180 181 182
				head->header_bytes);
			continue;
		}
		if (count_lb_records(head) != head->table_entries) {
183
			msg_perr("Bad record count: %d.\n",
184 185 186
				head->table_entries);
			continue;
		}
187
		if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
188
			msg_perr("Bad header checksum.\n");
189 190 191
			continue;
		}
		if (compute_checksum(recs, head->table_bytes)
192
		    != head->table_checksum) {
193
			msg_perr("Bad table checksum: %04x.\n",
194 195 196
				head->table_checksum);
			continue;
		}
197
		msg_pdbg("Found coreboot table at 0x%08lx.\n", addr);
198 199 200
		return head;

	};
Uwe Hermann's avatar
Uwe Hermann committed
201

202
	return NULL;
203 204 205 206 207 208 209
}

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
210

211 212
	rec = (struct lb_mainboard *)ptr;
	max_size = rec->size - sizeof(*rec);
213
	msg_pdbg("Vendor ID: %.*s, part ID: %.*s\n",
214 215 216 217 218 219 220 221 222
	         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);
	snprintf(part, 255, "%.*s", max_size - rec->part_number_idx, rec->strings + rec->part_number_idx);

	cb_vendor = strdup(vendor);
	cb_model = strdup(part);
223 224 225 226 227 228 229
}

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

230
static void search_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr)
231 232 233 234 235
{
	struct lb_record *next;
	int count;
	count = 0;

236 237
	for (next = next_record(rec); (rec < last) && (next <= last);
	     rec = next, addr += rec->size) {
238 239
		next = next_record(rec);
		count++;
240 241
		if (rec->tag == LB_TAG_MAINBOARD) {
			find_mainboard(rec, addr);
242 243 244 245 246
			break;
		}
	}
}

247
#define BYTES_TO_MAP (1024*1024)
248 249
/* returns 0 if the table was parsed successfully and cb_vendor/cb_model have been set. */
int cb_parse_table(const char **vendor, const char **model)
250
{
251
	uint8_t *table_area;
Stefan Reinauer's avatar
Stefan Reinauer committed
252
	unsigned long addr, start;
253 254
	struct lb_header *lb_table;
	struct lb_record *rec, *last;
255

256
#if defined(__MACH__) && defined(__APPLE__)
257
	/* This is a hack. DirectHW fails to map physical address 0x00000000.
Stefan Reinauer's avatar
Stefan Reinauer committed
258 259 260 261 262 263
	 * Why?
	 */
	start = 0x400;
#else
	start = 0x0;
#endif
264
	table_area = physmap_ro_unaligned("low megabyte", start, BYTES_TO_MAP - start);
265
	if (ERROR_PTR == table_area) {
266 267 268
		msg_perr("Failed getting access to coreboot low tables.\n");
		return -1;
	}
Stefan Reinauer's avatar
Stefan Reinauer committed
269

270
	lb_table = find_lb_table(table_area, 0x00000, 0x1000);
271
	if (!lb_table)
272
		lb_table = find_lb_table(table_area, 0xf0000 - start, BYTES_TO_MAP - start);
273 274 275 276 277
	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;
278
			start &= ~(getpagesize() - 1);
279 280 281
			physunmap_unaligned(table_area, BYTES_TO_MAP);
			// FIXME: table_area is never unmapped below, nor is it unmapped above in the no-forward case
			table_area = physmap_ro_unaligned("high tables", start, BYTES_TO_MAP);
282
			if (ERROR_PTR == table_area) {
283
				msg_perr("Failed getting access to coreboot high tables.\n");
284 285
				return -1;
			}
286 287 288 289
			lb_table = find_lb_table(table_area, 0x00000, 0x1000);
		}
	}

290
	if (!lb_table) {
291
		msg_pdbg("No coreboot table found.\n");
292 293
		return -1;
	}
Uwe Hermann's avatar
Uwe Hermann committed
294

295
	addr = ((char *)lb_table) - ((char *)table_area) + start;
296
	msg_pinfo("coreboot table found at 0x%lx.\n", 
297
		(unsigned long)lb_table - (unsigned long)table_area + start);
298 299
	rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
	last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
300
	msg_pdbg("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
301 302 303 304
	     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);
305 306
	*vendor = cb_vendor;
	*model = cb_model;
307 308
	return 0;
}