2007-05-24 11:08:36 +02:00
|
|
|
/*
|
2007-08-29 19:52:32 +02:00
|
|
|
* This file is part of the flashrom project.
|
2007-05-24 11:08:36 +02: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-2007 coresystems GmbH
|
|
|
|
* (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
|
2007-08-29 19:52:32 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-05-24 11:08:36 +02:00
|
|
|
*/
|
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/mman.h>
|
2007-02-06 20:47:50 +01:00
|
|
|
#include "flash.h"
|
2007-02-06 20:53:51 +01:00
|
|
|
#include "linuxbios_tables.h"
|
2005-11-26 22:55:36 +01:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
char *lb_part = NULL, *lb_vendor = NULL;
|
2005-11-26 22:55:36 +01:00
|
|
|
|
|
|
|
static unsigned long compute_checksum(void *addr, unsigned long length)
|
|
|
|
{
|
|
|
|
uint8_t *ptr;
|
|
|
|
volatile union {
|
2007-05-09 12:17:44 +02:00
|
|
|
uint8_t byte[2];
|
2005-11-26 22:55:36 +01:00
|
|
|
uint16_t word;
|
|
|
|
} value;
|
2007-10-10 19:42:20 +02:00
|
|
|
unsigned long sum;
|
|
|
|
unsigned long i;
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
/* In the most straight forward way possible,
|
|
|
|
* compute an ip style checksum.
|
|
|
|
*/
|
|
|
|
sum = 0;
|
|
|
|
ptr = addr;
|
2007-05-09 12:17:44 +02:00
|
|
|
for (i = 0; i < length; i++) {
|
2005-11-26 22:55:36 +01:00
|
|
|
unsigned long value;
|
|
|
|
value = ptr[i];
|
2007-10-10 19:42:20 +02:00
|
|
|
if (i & 1) {
|
2005-11-26 22:55:36 +01:00
|
|
|
value <<= 8;
|
2007-10-10 19:42:20 +02:00
|
|
|
}
|
2005-11-26 22:55:36 +01:00
|
|
|
/* Add the new value */
|
|
|
|
sum += value;
|
|
|
|
/* Wrap around the carry */
|
2007-10-10 19:42:20 +02:00
|
|
|
if (sum > 0xFFFF) {
|
2005-11-26 22:55:36 +01:00
|
|
|
sum = (sum + (sum >> 16)) & 0xFFFF;
|
2007-10-10 19:42:20 +02:00
|
|
|
}
|
2005-11-26 22:55:36 +01:00
|
|
|
}
|
|
|
|
value.byte[0] = sum & 0xff;
|
|
|
|
value.byte[1] = (sum >> 8) & 0xff;
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
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)); \
|
2007-05-09 12:17:44 +02:00
|
|
|
rec = (struct lb_record *)(((char *)rec) + rec->size))
|
2005-11-26 22:55:36 +01:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
static int count_lb_records(struct lb_header *head)
|
2005-11-26 22:55:36 +01:00
|
|
|
{
|
|
|
|
struct lb_record *rec;
|
|
|
|
int count;
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
count = 0;
|
|
|
|
for_each_lbrec(head, rec) {
|
|
|
|
count++;
|
|
|
|
}
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
static struct lb_header *find_lb_table(void *base, unsigned long start,
|
|
|
|
unsigned long end)
|
2005-11-26 22:55:36 +01:00
|
|
|
{
|
|
|
|
unsigned long addr;
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
/* For now be stupid.... */
|
2007-05-09 12:17:44 +02:00
|
|
|
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));
|
2005-11-26 22:55:36 +01:00
|
|
|
if (memcmp(head->signature, "LBIO", 4) != 0)
|
|
|
|
continue;
|
2007-05-09 12:17:44 +02:00
|
|
|
printf_debug("Found canidate at: %08lx-%08lx\n",
|
|
|
|
addr, addr + head->table_bytes);
|
2005-11-26 22:55:36 +01:00
|
|
|
if (head->header_bytes != sizeof(*head)) {
|
|
|
|
fprintf(stderr, "Header bytes of %d are incorrect\n",
|
|
|
|
head->header_bytes);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (count_lb_records(head) != head->table_entries) {
|
2007-10-10 19:42:20 +02:00
|
|
|
fprintf(stderr, "bad record count: %d\n",
|
2005-11-26 22:55:36 +01:00
|
|
|
head->table_entries);
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
if (compute_checksum((uint8_t *) head, sizeof(*head)) != 0) {
|
2007-10-10 19:42:20 +02:00
|
|
|
fprintf(stderr, "bad header checksum\n");
|
2005-11-26 22:55:36 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (compute_checksum(recs, head->table_bytes)
|
2007-05-09 12:17:44 +02:00
|
|
|
!= head->table_checksum) {
|
2007-10-10 19:42:20 +02:00
|
|
|
fprintf(stderr, "bad table checksum: %04x\n",
|
2005-11-26 22:55:36 +01:00
|
|
|
head->table_checksum);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
fprintf(stdout, "Found LinuxBIOS table at: %08lx\n", addr);
|
|
|
|
return head;
|
|
|
|
|
|
|
|
};
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
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];
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
rec = (struct lb_mainboard *)ptr;
|
|
|
|
max_size = rec->size - sizeof(*rec);
|
2007-10-10 19:42:20 +02:00
|
|
|
printf("vendor id: %.*s part id: %.*s\n",
|
2007-05-09 12:17:44 +02: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);
|
2005-11-26 22:55:36 +01:00
|
|
|
snprintf(part, 255, "%.*s", max_size - rec->part_number_idx,
|
2007-05-09 12:17:44 +02:00
|
|
|
rec->strings + rec->part_number_idx);
|
2005-11-26 22:55:36 +01:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
if (lb_part) {
|
2007-10-10 19:42:20 +02:00
|
|
|
printf("overwritten by command line, vendor id: %s part id: %s\n", lb_vendor, lb_part);
|
2005-11-26 22:55:36 +01:00
|
|
|
} else {
|
2007-05-09 12:17:44 +02:00
|
|
|
lb_part = strdup(part);
|
|
|
|
lb_vendor = strdup(vendor);
|
2005-11-26 22:55:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct lb_record *next_record(struct lb_record *rec)
|
|
|
|
{
|
|
|
|
return (struct lb_record *)(((char *)rec) + rec->size);
|
|
|
|
}
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
static void search_lb_records(struct lb_record *rec, struct lb_record *last,
|
|
|
|
unsigned long addr)
|
2005-11-26 22:55:36 +01:00
|
|
|
{
|
|
|
|
struct lb_record *next;
|
|
|
|
int count;
|
|
|
|
count = 0;
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
for (next = next_record(rec); (rec < last) && (next <= last);
|
|
|
|
rec = next, addr += rec->size) {
|
2005-11-26 22:55:36 +01:00
|
|
|
next = next_record(rec);
|
|
|
|
count++;
|
2007-05-09 12:17:44 +02:00
|
|
|
if (rec->tag == LB_TAG_MAINBOARD) {
|
|
|
|
find_mainboard(rec, addr);
|
2005-11-26 22:55:36 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
int linuxbios_init(void)
|
2005-11-26 22:55:36 +01:00
|
|
|
{
|
|
|
|
uint8_t *low_1MB;
|
|
|
|
struct lb_header *lb_table;
|
|
|
|
struct lb_record *rec, *last;
|
2007-05-09 12:17:44 +02:00
|
|
|
|
|
|
|
low_1MB = mmap(0, 1024 * 1024, PROT_READ, MAP_SHARED, fd_mem,
|
|
|
|
0x00000000);
|
2007-04-06 13:58:03 +02:00
|
|
|
if (low_1MB == MAP_FAILED) {
|
2007-05-23 20:24:58 +02:00
|
|
|
perror("Can't mmap memory using " MEM_DEV);
|
2005-11-26 22:55:36 +01:00
|
|
|
exit(-2);
|
|
|
|
}
|
|
|
|
lb_table = 0;
|
|
|
|
if (!lb_table)
|
|
|
|
lb_table = find_lb_table(low_1MB, 0x00000, 0x1000);
|
|
|
|
if (!lb_table)
|
2007-05-09 12:17:44 +02:00
|
|
|
lb_table = find_lb_table(low_1MB, 0xf0000, 1024 * 1024);
|
2005-11-26 22:55:36 +01:00
|
|
|
if (lb_table) {
|
|
|
|
unsigned long addr;
|
|
|
|
addr = ((char *)lb_table) - ((char *)low_1MB);
|
2007-10-10 19:42:20 +02:00
|
|
|
printf_debug("lb_table found at address %p\n", lb_table);
|
2007-05-09 12:17:44 +02:00
|
|
|
rec = (struct lb_record *)(((char *)lb_table) + lb_table->header_bytes);
|
2005-11-26 22:55:36 +01:00
|
|
|
last = (struct lb_record *)(((char *)rec) + lb_table->table_bytes);
|
|
|
|
printf_debug("LinuxBIOS header(%d) checksum: %04x table(%d) checksum: %04x entries: %d\n",
|
2007-05-09 12:17:44 +02:00
|
|
|
lb_table->header_bytes, lb_table->header_checksum,
|
|
|
|
lb_table->table_bytes, lb_table->table_checksum,
|
|
|
|
lb_table->table_entries);
|
2005-11-26 22:55:36 +01:00
|
|
|
search_lb_records(rec, last, addr + lb_table->header_bytes);
|
2007-05-09 12:17:44 +02:00
|
|
|
} else {
|
2007-10-10 19:42:20 +02:00
|
|
|
printf("No LinuxBIOS table found.\n");
|
2005-11-26 22:55:36 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
return 0;
|
|
|
|
}
|