Files

301 lines
9.1 KiB
C
Raw Permalink Normal View History

2007-05-24 09:08:36 +00:00
/*
* This file is part of the flashrom project.
2007-05-24 09:08:36 +00:00
*
* Copyright (C) 2002 Steven James <pyro@linuxlabs.com>
* Copyright (C) 2002 Linux Networx
* (Written by Eric Biederman <ebiederman@lnxi.com> for Linux Networx)
* Copyright (C) 2006-2009 coresystems GmbH
2007-05-24 09:08:36 +00:00
* (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH)
* Copyright (C) 2010 Carl-Daniel Hailfinger
2007-05-24 09:08:36 +00:00
*
* 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.
*/
#include <unistd.h>
#include <stdio.h>
2012-08-11 16:07:08 +00:00
#include <ctype.h>
2012-08-27 00:44:42 +00:00
#include <strings.h>
#include <string.h>
#include "flash.h"
2010-07-27 22:41:39 +00:00
#include "programmer.h"
#include "coreboot_tables.h"
2012-08-20 00:24:22 +00:00
static char *cb_vendor = NULL, *cb_model = NULL;
2012-08-20 00:24:22 +00:00
/* Tries to find coreboot IDs in the supplied image and compares them to the current IDs.
* Returns...
2018-05-28 16:52:21 +02:00
* -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(const uint8_t *image, int size)
2012-08-11 16:07:08 +00:00
{
const unsigned int *walk;
2012-08-11 16:07:08 +00:00
unsigned int mb_part_offset, mb_vendor_offset;
const char *mb_part, *mb_vendor;
2012-08-11 16:07:08 +00:00
walk = (const unsigned int *)(image + size - 0x10);
2012-08-11 16:07:08 +00:00
walk--;
if ((*walk) == 0 || ((*walk) & 0x3ff) != 0) {
2012-08-20 00:24:22 +00:00
/* 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 = (const unsigned int *)(image + size - 0x80);
2012-08-11 16:07:08 +00:00
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) {
2012-08-20 00:24:22 +00:00
msg_pdbg("Flash image seems to be a legacy BIOS. Disabling coreboot-related checks.\n");
2012-08-11 16:07:08 +00:00
return 0;
}
mb_part = (const char *)(image + size - mb_part_offset);
mb_vendor = (const char *)(image + size - mb_vendor_offset);
2012-08-11 16:07:08 +00:00
if (!isprint((unsigned char)*mb_part) ||
!isprint((unsigned char)*mb_vendor)) {
2012-08-20 00:24:22 +00:00
msg_pdbg("Flash image seems to have garbage in the ID location. "
"Disabling coreboot-related checks.\n");
2012-08-11 16:07:08 +00:00
return 0;
}
msg_pdbg("coreboot last image size (not ROM size) is %d bytes.\n", *walk);
msg_pdbg("Manufacturer: %s\n", mb_vendor);
msg_pdbg("Mainboard ID: %s\n", mb_part);
2012-08-11 16:07:08 +00:00
/* If these are not set, the coreboot table was not found. */
2012-08-20 00:24:22 +00:00
if (!cb_vendor || !cb_model)
2012-08-11 16:07:08 +00:00
return 0;
/* These comparisons are case insensitive to make things a little less user^Werror prone. */
if (!strcasecmp(mb_vendor, cb_vendor) && !strcasecmp(mb_part, cb_model)) {
2012-08-20 00:24:22 +00:00
msg_pdbg2("This coreboot image matches this mainboard.\n");
2012-08-11 16:07:08 +00:00
} else {
2013-01-04 22:54:07 +00:00
msg_perr("This coreboot image (%s:%s) does not appear to\n"
"be correct for the detected mainboard (%s:%s).\n",
mb_vendor, mb_part, cb_vendor, cb_model);
2012-08-20 00:24:22 +00:00
return -1;
2012-08-11 16:07:08 +00:00
}
return 0;
}
static unsigned long compute_checksum(void *addr, unsigned long length)
{
uint8_t *ptr;
volatile union {
uint8_t byte[2];
uint16_t word;
2010-01-12 15:36:24 +00:00
} chksum;
2007-10-10 17:42:20 +00:00
unsigned long sum;
unsigned long i;
2007-08-23 16:08:21 +00:00
/* In the most straight forward way possible,
* compute an ip style checksum.
*/
sum = 0;
ptr = addr;
for (i = 0; i < length; i++) {
unsigned long value;
value = ptr[i];
2007-10-10 17:42:20 +00:00
if (i & 1) {
value <<= 8;
2007-10-10 17:42:20 +00:00
}
/* Add the new value */
sum += value;
/* Wrap around the carry */
2007-10-10 17:42:20 +00:00
if (sum > 0xFFFF) {
sum = (sum + (sum >> 16)) & 0xFFFF;
2007-10-10 17:42:20 +00:00
}
}
2010-01-12 15:36:24 +00:00
chksum.byte[0] = sum & 0xff;
chksum.byte[1] = (sum >> 8) & 0xff;
2007-08-23 16:08:21 +00:00
2010-01-12 15:36:24 +00:00
return (~chksum.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)); \
rec = (struct lb_record *)(((char *)rec) + rec->size))
static int count_lb_records(struct lb_header *head)
{
struct lb_record *rec;
int count;
2007-08-23 16:08:21 +00:00
count = 0;
for_each_lbrec(head, rec) {
count++;
}
2007-08-23 16:08:21 +00:00
return count;
}
static struct lb_header *find_lb_table(void *base, unsigned long start,
unsigned long end)
{
unsigned long addr;
2007-08-23 16:08:21 +00:00
/* For now be stupid.... */
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));
if (memcmp(head->signature, "LBIO", 4) != 0)
continue;
msg_pdbg("Found candidate at: %08lx-%08lx\n",
addr, addr + head->table_bytes);
if (head->header_bytes != sizeof(*head)) {
msg_perr("Header bytes of %d are incorrect.\n",
head->header_bytes);
continue;
}
if (count_lb_records(head) != head->table_entries) {
msg_perr("Bad record count: %d.\n",
head->table_entries);
continue;
}
if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
msg_perr("Bad header checksum.\n");
continue;
}
if (compute_checksum(recs, head->table_bytes)
!= head->table_checksum) {
msg_perr("Bad table checksum: %04x.\n",
head->table_checksum);
continue;
}
msg_pdbg("Found coreboot table at 0x%08lx.\n", addr);
return head;
};
2007-08-23 16:08:21 +00:00
return NULL;
}
static void find_mainboard(struct lb_record *ptr, unsigned long addr)
{
struct lb_mainboard *rec;
int max_size;
char vendor[256], part[256];
2007-08-23 16:08:21 +00:00
rec = (struct lb_mainboard *)ptr;
max_size = rec->size - sizeof(*rec);
msg_pdbg("Vendor ID: %.*s, part ID: %.*s\n",
2012-08-20 00:24:22 +00:00
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);
2012-08-20 00:24:22 +00:00
cb_vendor = strdup(vendor);
cb_model = strdup(part);
}
static struct lb_record *next_record(struct lb_record *rec)
{
return (struct lb_record *)(((char *)rec) + rec->size);
}
2012-08-20 00:24:22 +00:00
static void search_lb_records(struct lb_record *rec, struct lb_record *last, unsigned long addr)
{
struct lb_record *next;
int count;
count = 0;
for (next = next_record(rec); (rec < last) && (next <= last);
rec = next, addr += rec->size) {
next = next_record(rec);
count++;
if (rec->tag == LB_TAG_MAINBOARD) {
find_mainboard(rec, addr);
break;
}
}
}
#define BYTES_TO_MAP (1024*1024)
2012-08-20 00:24:22 +00:00
/* 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)
{
uint8_t *table_area;
2009-01-26 01:23:31 +00:00
unsigned long addr, start;
struct lb_header *lb_table;
struct lb_record *rec, *last;
2012-08-09 23:34:41 +00:00
#if defined(__MACH__) && defined(__APPLE__)
2010-10-06 23:16:10 +00:00
/* This is a hack. DirectHW fails to map physical address 0x00000000.
2009-01-26 01:23:31 +00:00
* Why?
*/
start = 0x400;
#else
start = 0x0;
#endif
table_area = physmap_ro_unaligned("low megabyte", start, BYTES_TO_MAP - start);
if (ERROR_PTR == table_area) {
msg_perr("Failed getting access to coreboot low tables.\n");
return -1;
}
2009-01-26 01:23:31 +00:00
lb_table = find_lb_table(table_area, 0x00000, 0x1000);
if (!lb_table)
lb_table = find_lb_table(table_area, 0xf0000 - start, BYTES_TO_MAP - start);
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;
2009-04-15 10:52:49 +00:00
start &= ~(getpagesize() - 1);
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);
if (ERROR_PTR == table_area) {
2012-08-20 00:24:22 +00:00
msg_perr("Failed getting access to coreboot high tables.\n");
return -1;
}
lb_table = find_lb_table(table_area, 0x00000, 0x1000);
}
}
if (!lb_table) {
msg_pdbg("No coreboot table found.\n");
return -1;
}
2007-08-23 16:08:21 +00:00
addr = ((char *)lb_table) - ((char *)table_area) + start;
2018-03-27 12:15:09 +02:00
msg_pinfo("coreboot table found at 0x%lx.\n",
(unsigned long)lb_table - (unsigned long)table_area + start);
rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
msg_pdbg("coreboot header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
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);
2012-08-20 00:24:22 +00:00
*vendor = cb_vendor;
*model = cb_model;
return 0;
}