2003-10-25 19:01:29 +02:00
|
|
|
/*
|
2007-08-29 19:52:32 +02:00
|
|
|
* This file is part of the flashrom project.
|
2003-10-25 19:01:29 +02:00
|
|
|
*
|
2007-09-09 22:21:05 +02:00
|
|
|
* Copyright (C) 2000 Silicon Integrated System Corporation
|
|
|
|
* Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
|
2008-08-02 17:13:58 +02:00
|
|
|
* Copyright (C) 2005-2008 coresystems GmbH
|
2003-10-25 19:01:29 +02:00
|
|
|
*
|
2007-08-29 19:52:32 +02: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; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2003-10-25 19:01:29 +02:00
|
|
|
*
|
2007-08-29 19:52:32 +02:00
|
|
|
* 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.
|
2003-10-25 19:01:29 +02:00
|
|
|
*
|
2007-08-29 19:52:32 +02:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-10-25 19:01:29 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2006-11-22 00:48:51 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2003-10-25 19:01:29 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2005-11-26 22:55:36 +01:00
|
|
|
#include <getopt.h>
|
2007-04-05 00:45:58 +02:00
|
|
|
#include <pci/pci.h>
|
|
|
|
/* for iopl */
|
|
|
|
#if defined (__sun) && (defined(__i386) || defined(__amd64))
|
|
|
|
#include <strings.h>
|
|
|
|
#include <sys/sysi86.h>
|
|
|
|
#include <sys/psw.h>
|
|
|
|
#include <asm/sunddi.h>
|
|
|
|
#endif
|
2003-10-25 19:01:29 +02:00
|
|
|
#include "flash.h"
|
|
|
|
|
|
|
|
char *chip_to_probe = NULL;
|
2007-05-09 12:17:44 +02:00
|
|
|
struct pci_access *pacc; /* For board and chipset_enable */
|
2005-12-18 17:41:10 +01:00
|
|
|
int exclude_start_page, exclude_end_page;
|
2008-06-18 04:08:40 +02:00
|
|
|
int verbose = 0;
|
2007-04-06 13:58:03 +02:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
|
2007-04-05 00:45:58 +02:00
|
|
|
{
|
2007-05-09 12:17:44 +02:00
|
|
|
struct pci_dev *temp;
|
|
|
|
struct pci_filter filter;
|
2007-04-05 00:45:58 +02:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
pci_filter_init(NULL, &filter);
|
|
|
|
filter.vendor = vendor;
|
|
|
|
filter.device = device;
|
2007-04-05 00:45:58 +02:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
for (temp = pacc->devices; temp; temp = temp->next)
|
|
|
|
if (pci_filter_match(&filter, temp))
|
|
|
|
return temp;
|
2007-04-05 00:45:58 +02:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
return NULL;
|
2007-04-05 00:45:58 +02:00
|
|
|
}
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device,
|
|
|
|
uint16_t card_vendor, uint16_t card_device)
|
2007-04-05 00:45:58 +02:00
|
|
|
{
|
2007-05-09 12:17:44 +02:00
|
|
|
struct pci_dev *temp;
|
|
|
|
struct pci_filter filter;
|
|
|
|
|
|
|
|
pci_filter_init(NULL, &filter);
|
|
|
|
filter.vendor = vendor;
|
|
|
|
filter.device = device;
|
|
|
|
|
|
|
|
for (temp = pacc->devices; temp; temp = temp->next)
|
|
|
|
if (pci_filter_match(&filter, temp)) {
|
2008-10-18 23:14:13 +02:00
|
|
|
if ((card_vendor ==
|
|
|
|
pci_read_word(temp, PCI_SUBSYSTEM_VENDOR_ID))
|
|
|
|
&& (card_device ==
|
|
|
|
pci_read_word(temp, PCI_SUBSYSTEM_ID)))
|
2007-05-09 12:17:44 +02:00
|
|
|
return temp;
|
|
|
|
}
|
2007-04-05 00:45:58 +02:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
return NULL;
|
2007-04-05 00:45:58 +02:00
|
|
|
}
|
|
|
|
|
2009-01-26 01:39:57 +01:00
|
|
|
void map_flash_registers(struct flashchip *flash)
|
2007-05-24 10:48:10 +02:00
|
|
|
{
|
|
|
|
size_t size = flash->total_size * 1024;
|
2009-01-26 02:10:48 +01:00
|
|
|
flash->virtual_registers = physmap("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
|
2007-05-24 10:48:10 +02:00
|
|
|
}
|
|
|
|
|
2008-09-04 01:10:05 +02:00
|
|
|
struct flashchip *probe_flash(struct flashchip *first_flash, int force)
|
2003-10-25 19:01:29 +02:00
|
|
|
{
|
2005-11-26 22:55:36 +01:00
|
|
|
volatile uint8_t *bios;
|
2008-09-04 01:10:05 +02:00
|
|
|
struct flashchip *flash;
|
2008-12-04 00:36:48 +01:00
|
|
|
unsigned long base = 0, size;
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2008-09-04 01:10:05 +02:00
|
|
|
for (flash = first_flash; flash && flash->name; flash++) {
|
2008-07-02 19:15:47 +02:00
|
|
|
if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
|
2004-03-17 23:22:08 +01:00
|
|
|
continue;
|
2008-03-17 23:59:40 +01:00
|
|
|
printf_debug("Probing for %s %s, %d KB: ",
|
|
|
|
flash->vendor, flash->name, flash->total_size);
|
2008-06-18 04:08:40 +02:00
|
|
|
if (!flash->probe && !force) {
|
2008-04-28 16:47:30 +02:00
|
|
|
printf_debug("failed! flashrom has no probe function for this flash chip.\n");
|
|
|
|
continue;
|
|
|
|
}
|
2007-04-06 13:58:03 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
size = flash->total_size * 1024;
|
2007-04-06 13:58:03 +02:00
|
|
|
|
|
|
|
/* If getpagesize() > size ->
|
2007-05-23 20:24:58 +02:00
|
|
|
* "Can't mmap memory using /dev/mem: Invalid argument"
|
2007-04-06 13:58:03 +02:00
|
|
|
* This should never happen as we don't support any flash chips
|
2007-05-23 20:24:58 +02:00
|
|
|
* smaller than 4k or 8k (yet).
|
2007-04-06 13:58:03 +02:00
|
|
|
*/
|
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
if (getpagesize() > size) {
|
2008-03-16 00:41:19 +01:00
|
|
|
/*
|
|
|
|
* if a flash size of 0 is mapped, we map a single page
|
|
|
|
* so we can probe in that area whether we know the
|
|
|
|
* vendor at least.
|
|
|
|
*/
|
2004-03-17 23:22:08 +01:00
|
|
|
size = getpagesize();
|
|
|
|
}
|
2008-08-12 13:58:00 +02:00
|
|
|
|
2009-02-09 21:26:14 +01:00
|
|
|
base = flashbase && flashchips == first_flash ? flashbase : (0xffffffff - size + 1);
|
2009-01-26 02:10:48 +01:00
|
|
|
flash->virtual_memory = bios = physmap("flash chip", base, size);
|
2004-03-17 23:22:08 +01:00
|
|
|
|
2008-07-02 19:15:47 +02:00
|
|
|
if (force)
|
|
|
|
break;
|
|
|
|
|
2008-09-04 01:10:05 +02:00
|
|
|
if (flash->probe(flash) != 1)
|
|
|
|
goto notfound;
|
|
|
|
|
2008-10-18 23:14:13 +02:00
|
|
|
if (first_flash == flashchips
|
|
|
|
|| flash->model_id != GENERIC_DEVICE_ID)
|
2008-07-02 19:15:47 +02:00
|
|
|
break;
|
2006-03-16 17:57:41 +01:00
|
|
|
|
2008-09-04 01:10:05 +02:00
|
|
|
notfound:
|
2009-01-26 02:10:48 +01:00
|
|
|
physunmap((void *)bios, size);
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2008-07-02 19:15:47 +02:00
|
|
|
if (!flash || !flash->name)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
printf("Found chip \"%s %s\" (%d KB) at physical address 0x%lx.\n",
|
2008-12-03 22:39:56 +01:00
|
|
|
flash->vendor, flash->name, flash->total_size, base);
|
|
|
|
flashbase = base;
|
2008-07-02 19:15:47 +02:00
|
|
|
return flash;
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
|
2005-12-18 17:41:10 +01:00
|
|
|
int verify_flash(struct flashchip *flash, uint8_t *buf)
|
2003-10-25 19:01:29 +02:00
|
|
|
{
|
2006-03-16 17:57:41 +01:00
|
|
|
int idx;
|
2004-03-20 17:46:10 +01:00
|
|
|
int total_size = flash->total_size * 1024;
|
2008-01-22 16:19:01 +01:00
|
|
|
uint8_t *buf2 = (uint8_t *) calloc(total_size, sizeof(char));
|
|
|
|
if (flash->read == NULL)
|
|
|
|
memcpy(buf2, (const char *)flash->virtual_memory, total_size);
|
|
|
|
else
|
|
|
|
flash->read(flash, buf2);
|
2004-03-17 23:22:08 +01:00
|
|
|
|
2007-10-18 01:55:15 +02:00
|
|
|
printf("Verifying flash... ");
|
2007-05-09 12:17:44 +02:00
|
|
|
|
|
|
|
if (verbose)
|
|
|
|
printf("address: 0x00000000\b\b\b\b\b\b\b\b\b\b");
|
|
|
|
|
2006-03-16 17:57:41 +01:00
|
|
|
for (idx = 0; idx < total_size; idx++) {
|
2007-05-09 12:17:44 +02:00
|
|
|
if (verbose && ((idx & 0xfff) == 0xfff))
|
2006-03-16 17:57:41 +01:00
|
|
|
printf("0x%08x", idx);
|
|
|
|
|
2008-01-22 16:19:01 +01:00
|
|
|
if (*(buf2 + idx) != *(buf + idx)) {
|
2009-01-13 15:32:27 +01:00
|
|
|
if (verbose)
|
|
|
|
printf("0x%08x FAILED!", idx);
|
|
|
|
else
|
|
|
|
printf("FAILED at 0x%08x!", idx);
|
|
|
|
printf(" Expected=0x%02x, Read=0x%02x\n",
|
2008-10-18 23:14:13 +02:00
|
|
|
*(buf + idx), *(buf2 + idx));
|
2006-03-16 17:57:41 +01:00
|
|
|
return 1;
|
2004-03-17 23:22:08 +01:00
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
|
|
|
|
if (verbose && ((idx & 0xfff) == 0xfff))
|
2004-03-17 23:22:08 +01:00
|
|
|
printf("\b\b\b\b\b\b\b\b\b\b");
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
if (verbose)
|
2006-03-16 17:57:41 +01:00
|
|
|
printf("\b\b\b\b\b\b\b\b\b\b ");
|
2007-05-09 12:17:44 +02:00
|
|
|
|
2007-10-18 01:55:15 +02:00
|
|
|
printf("VERIFIED. \n");
|
2007-08-23 18:08:21 +02:00
|
|
|
|
2006-03-16 17:57:41 +01:00
|
|
|
return 0;
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
|
2009-01-28 01:27:54 +01:00
|
|
|
int read_flash(struct flashchip *flash, char *filename, unsigned int exclude_start_position, unsigned int exclude_end_position)
|
|
|
|
{
|
|
|
|
unsigned long numbytes;
|
|
|
|
FILE *image;
|
|
|
|
unsigned long size = flash->total_size * 1024;
|
|
|
|
unsigned char *buf = calloc(size, sizeof(char));
|
|
|
|
if ((image = fopen(filename, "w")) == NULL) {
|
|
|
|
perror(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("Reading flash... ");
|
|
|
|
if (flash->read == NULL)
|
|
|
|
memcpy(buf, (const char *)flash->virtual_memory, size);
|
|
|
|
else
|
|
|
|
flash->read(flash, buf);
|
|
|
|
|
|
|
|
if (exclude_end_position - exclude_start_position > 0)
|
|
|
|
memset(buf + exclude_start_position, 0,
|
|
|
|
exclude_end_position - exclude_start_position);
|
|
|
|
|
|
|
|
numbytes = fwrite(buf, 1, size, image);
|
|
|
|
fclose(image);
|
|
|
|
printf("%s.\n", numbytes == size ? "done" : "FAILED");
|
|
|
|
if (numbytes != size)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int erase_flash(struct flashchip *flash)
|
|
|
|
{
|
|
|
|
uint32_t erasedbytes;
|
|
|
|
unsigned long size = flash->total_size * 1024;
|
|
|
|
unsigned char *buf = calloc(size, sizeof(char));
|
|
|
|
printf("Erasing flash chip... ");
|
|
|
|
if (NULL == flash->erase) {
|
|
|
|
printf("FAILED!\n");
|
|
|
|
fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
flash->erase(flash);
|
|
|
|
if (NULL == flash->read)
|
|
|
|
memcpy(buf, (const char *)flash->virtual_memory, size);
|
|
|
|
else
|
|
|
|
flash->read(flash, buf);
|
|
|
|
for (erasedbytes = 0; erasedbytes < size; erasedbytes++)
|
|
|
|
if (0xff != buf[erasedbytes]) {
|
|
|
|
printf("FAILED!\n");
|
|
|
|
fprintf(stderr, "ERROR at 0x%08x: Expected=0xff, Read=0x%02x\n",
|
|
|
|
erasedbytes, buf[erasedbytes]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
printf("SUCCESS.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-24 18:17:41 +02:00
|
|
|
#ifndef MAX
|
2008-12-06 02:37:09 +01:00
|
|
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
2009-04-24 18:17:41 +02:00
|
|
|
#endif
|
2008-12-06 02:37:09 +01:00
|
|
|
#define POS_PRINT(x) do { pos += strlen(x); printf(x); } while (0)
|
|
|
|
|
2008-03-12 12:54:51 +01:00
|
|
|
void print_supported_chips(void)
|
|
|
|
{
|
2008-12-06 02:37:09 +01:00
|
|
|
int okcol = 0, pos = 0;
|
|
|
|
struct flashchip *f;
|
2008-03-12 12:54:51 +01:00
|
|
|
|
2008-12-06 02:37:09 +01:00
|
|
|
for (f = flashchips; f->name != NULL; f++) {
|
|
|
|
if (GENERIC_DEVICE_ID == f->model_id)
|
|
|
|
continue;
|
|
|
|
okcol = MAX(okcol, strlen(f->vendor) + 1 + strlen(f->name));
|
|
|
|
}
|
|
|
|
okcol = (okcol + 7) & ~7;
|
2008-03-12 12:54:51 +01:00
|
|
|
|
2008-12-06 02:37:09 +01:00
|
|
|
POS_PRINT("Supported flash chips:");
|
|
|
|
while (pos < okcol) {
|
|
|
|
printf("\t");
|
|
|
|
pos += 8 - (pos % 8);
|
|
|
|
}
|
|
|
|
printf("Tested OK operations:\tKnown BAD operations:\n\n");
|
|
|
|
|
|
|
|
for (f = flashchips; f->name != NULL; f++) {
|
|
|
|
printf("%s %s", f->vendor, f->name);
|
|
|
|
pos = strlen(f->vendor) + 1 + strlen(f->name);
|
|
|
|
while (pos < okcol) {
|
|
|
|
printf("\t");
|
|
|
|
pos += 8 - (pos % 8);
|
|
|
|
}
|
|
|
|
if ((f->tested & TEST_OK_MASK)) {
|
|
|
|
if ((f->tested & TEST_OK_PROBE))
|
|
|
|
POS_PRINT("PROBE ");
|
|
|
|
if ((f->tested & TEST_OK_READ))
|
|
|
|
POS_PRINT("READ ");
|
|
|
|
if ((f->tested & TEST_OK_ERASE))
|
|
|
|
POS_PRINT("ERASE ");
|
|
|
|
if ((f->tested & TEST_OK_WRITE))
|
|
|
|
POS_PRINT("WRITE");
|
|
|
|
}
|
|
|
|
while (pos < okcol + 24) {
|
|
|
|
printf("\t");
|
|
|
|
pos += 8 - (pos % 8);
|
|
|
|
}
|
|
|
|
if ((f->tested & TEST_BAD_MASK)) {
|
|
|
|
if ((f->tested & TEST_BAD_PROBE))
|
|
|
|
printf("PROBE ");
|
|
|
|
if ((f->tested & TEST_BAD_READ))
|
|
|
|
printf("READ ");
|
|
|
|
if ((f->tested & TEST_BAD_ERASE))
|
|
|
|
printf("ERASE ");
|
|
|
|
if ((f->tested & TEST_BAD_WRITE))
|
|
|
|
printf("WRITE");
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
2008-03-12 12:54:51 +01:00
|
|
|
}
|
|
|
|
|
2003-10-25 19:01:29 +02:00
|
|
|
void usage(const char *name)
|
|
|
|
{
|
2008-10-18 23:14:13 +02:00
|
|
|
printf("usage: %s [-rwvEVfLhR] [-c chipname] [-s exclude_start]\n",
|
|
|
|
name);
|
2008-01-27 17:21:21 +01:00
|
|
|
printf(" [-e exclude_end] [-m [vendor:]part] [-l file.layout] [-i imagename] [file]\n");
|
2007-05-09 12:17:44 +02:00
|
|
|
printf
|
2008-01-27 17:21:21 +01:00
|
|
|
(" -r | --read: read flash and save into file\n"
|
|
|
|
" -w | --write: write file into flash\n"
|
|
|
|
" -v | --verify: verify flash against file\n"
|
|
|
|
" -E | --erase: erase flash device\n"
|
|
|
|
" -V | --verbose: more verbose output\n"
|
|
|
|
" -c | --chip <chipname>: probe only for specified flash chip\n"
|
|
|
|
" -s | --estart <addr>: exclude start position\n"
|
|
|
|
" -e | --eend <addr>: exclude end postion\n"
|
|
|
|
" -m | --mainboard <[vendor:]part>: override mainboard settings\n"
|
|
|
|
" -f | --force: force write without checking image\n"
|
|
|
|
" -l | --layout <file.layout>: read rom layout from file\n"
|
|
|
|
" -i | --image <name>: only flash image name from flash layout\n"
|
2008-03-12 12:54:51 +01:00
|
|
|
" -L | --list-supported: print supported devices\n"
|
|
|
|
" -h | --help: print this help text\n"
|
2008-01-27 17:21:21 +01:00
|
|
|
" -R | --version: print the version (release)\n"
|
2008-03-12 12:54:51 +01:00
|
|
|
"\n" " If no file is specified, then all that happens"
|
2007-05-09 12:17:44 +02:00
|
|
|
" is that flash info is dumped.\n\n");
|
2004-03-17 23:22:08 +01:00
|
|
|
exit(1);
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
|
2008-01-21 16:24:22 +01:00
|
|
|
void print_version(void)
|
|
|
|
{
|
|
|
|
printf("flashrom r%s\n", FLASHROM_VERSION);
|
|
|
|
}
|
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
int main(int argc, char *argv[])
|
2003-10-25 19:01:29 +02:00
|
|
|
{
|
2005-11-26 22:55:36 +01:00
|
|
|
uint8_t *buf;
|
2009-01-12 22:00:35 +01:00
|
|
|
unsigned long size, numbytes;
|
2004-03-20 17:46:10 +01:00
|
|
|
FILE *image;
|
2008-05-08 02:31:44 +02:00
|
|
|
/* Probe for up to three flash chips. */
|
|
|
|
struct flashchip *flash, *flashes[3];
|
2004-03-17 23:22:08 +01:00
|
|
|
int opt;
|
2005-11-26 22:55:36 +01:00
|
|
|
int option_index = 0;
|
2008-06-18 04:08:40 +02:00
|
|
|
int force = 0;
|
2007-05-09 12:17:44 +02:00
|
|
|
int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
|
2008-05-08 02:31:44 +02:00
|
|
|
int ret = 0, i;
|
2008-05-22 15:22:45 +02:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
int io_fd;
|
|
|
|
#endif
|
2005-11-26 22:55:36 +01:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
static struct option long_options[] = {
|
|
|
|
{"read", 0, 0, 'r'},
|
|
|
|
{"write", 0, 0, 'w'},
|
|
|
|
{"erase", 0, 0, 'E'},
|
|
|
|
{"verify", 0, 0, 'v'},
|
|
|
|
{"chip", 1, 0, 'c'},
|
|
|
|
{"estart", 1, 0, 's'},
|
|
|
|
{"eend", 1, 0, 'e'},
|
|
|
|
{"mainboard", 1, 0, 'm'},
|
|
|
|
{"verbose", 0, 0, 'V'},
|
|
|
|
{"force", 0, 0, 'f'},
|
|
|
|
{"layout", 1, 0, 'l'},
|
|
|
|
{"image", 1, 0, 'i'},
|
2008-03-12 12:54:51 +01:00
|
|
|
{"list-supported", 0, 0, 'L'},
|
2007-05-09 12:17:44 +02:00
|
|
|
{"help", 0, 0, 'h'},
|
2008-01-21 16:24:22 +01:00
|
|
|
{"version", 0, 0, 'R'},
|
2007-05-09 12:17:44 +02:00
|
|
|
{0, 0, 0, 0}
|
2005-11-26 22:55:36 +01:00
|
|
|
};
|
2004-03-17 23:22:08 +01:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
char *filename = NULL;
|
2004-10-20 07:07:16 +02:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
unsigned int exclude_start_position = 0, exclude_end_position = 0; // [x,y)
|
|
|
|
char *tempstr = NULL, *tempstr2 = NULL;
|
2004-10-20 07:07:16 +02:00
|
|
|
|
|
|
|
if (argc > 1) {
|
|
|
|
/* Yes, print them. */
|
|
|
|
int i;
|
2007-05-09 12:17:44 +02:00
|
|
|
printf_debug("The arguments are:\n");
|
2004-10-20 07:07:16 +02:00
|
|
|
for (i = 1; i < argc; ++i)
|
2007-05-09 12:17:44 +02:00
|
|
|
printf_debug("%s\n", argv[i]);
|
2004-10-20 07:07:16 +02:00
|
|
|
}
|
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
setbuf(stdout, NULL);
|
2008-03-12 12:54:51 +01:00
|
|
|
while ((opt = getopt_long(argc, argv, "rRwvVEfc:s:e:m:l:i:Lh",
|
2007-05-09 12:17:44 +02:00
|
|
|
long_options, &option_index)) != EOF) {
|
2004-03-17 23:22:08 +01:00
|
|
|
switch (opt) {
|
|
|
|
case 'r':
|
|
|
|
read_it = 1;
|
|
|
|
break;
|
|
|
|
case 'w':
|
|
|
|
write_it = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verify_it = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
chip_to_probe = strdup(optarg);
|
|
|
|
break;
|
2004-03-18 21:27:33 +01:00
|
|
|
case 'V':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
2004-12-08 21:10:01 +01:00
|
|
|
case 'E':
|
|
|
|
erase_it = 1;
|
|
|
|
break;
|
2004-10-20 07:07:16 +02:00
|
|
|
case 's':
|
|
|
|
tempstr = strdup(optarg);
|
2007-05-09 12:17:44 +02:00
|
|
|
sscanf(tempstr, "%x", &exclude_start_position);
|
2004-10-20 07:07:16 +02:00
|
|
|
break;
|
|
|
|
case 'e':
|
2004-12-07 04:15:51 +01:00
|
|
|
tempstr = strdup(optarg);
|
2007-05-09 12:17:44 +02:00
|
|
|
sscanf(tempstr, "%x", &exclude_end_position);
|
2004-12-07 04:15:51 +01:00
|
|
|
break;
|
2005-11-26 22:55:36 +01:00
|
|
|
case 'm':
|
|
|
|
tempstr = strdup(optarg);
|
|
|
|
strtok(tempstr, ":");
|
2007-05-09 12:17:44 +02:00
|
|
|
tempstr2 = strtok(NULL, ":");
|
2005-11-26 22:55:36 +01:00
|
|
|
if (tempstr2) {
|
2007-05-09 12:17:44 +02:00
|
|
|
lb_vendor = tempstr;
|
|
|
|
lb_part = tempstr2;
|
2005-11-26 22:55:36 +01:00
|
|
|
} else {
|
2008-01-27 17:21:21 +01:00
|
|
|
lb_vendor = NULL;
|
|
|
|
lb_part = tempstr;
|
2005-11-26 22:55:36 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'f':
|
2007-05-09 12:17:44 +02:00
|
|
|
force = 1;
|
2005-11-26 22:55:36 +01:00
|
|
|
break;
|
|
|
|
case 'l':
|
2007-05-09 12:17:44 +02:00
|
|
|
tempstr = strdup(optarg);
|
2007-04-14 18:32:59 +02:00
|
|
|
if (read_romlayout(tempstr))
|
|
|
|
exit(1);
|
2005-11-26 22:55:36 +01:00
|
|
|
break;
|
|
|
|
case 'i':
|
2007-05-09 12:17:44 +02:00
|
|
|
tempstr = strdup(optarg);
|
2005-11-26 22:55:36 +01:00
|
|
|
find_romentry(tempstr);
|
|
|
|
break;
|
2008-03-12 12:54:51 +01:00
|
|
|
case 'L':
|
|
|
|
print_supported_chips();
|
|
|
|
print_supported_chipsets();
|
|
|
|
print_supported_boards();
|
|
|
|
exit(0);
|
|
|
|
break;
|
2008-01-21 16:24:22 +01:00
|
|
|
case 'R':
|
|
|
|
print_version();
|
|
|
|
exit(0);
|
|
|
|
break;
|
2005-11-26 22:55:36 +01:00
|
|
|
case 'h':
|
2004-03-17 23:22:08 +01:00
|
|
|
default:
|
|
|
|
usage(argv[0]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-10-20 07:07:16 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
if (read_it && write_it) {
|
2008-05-23 00:47:04 +02:00
|
|
|
printf("Error: -r and -w are mutually exclusive.\n");
|
2004-03-17 23:22:08 +01:00
|
|
|
usage(argv[0]);
|
|
|
|
}
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
if (optind < argc)
|
|
|
|
filename = argv[optind++];
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
/* First get full io access */
|
2007-04-05 00:45:58 +02:00
|
|
|
#if defined (__sun) && (defined(__i386) || defined(__amd64))
|
2007-05-09 12:17:44 +02:00
|
|
|
if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
|
2008-05-22 15:22:45 +02:00
|
|
|
#elif defined(__FreeBSD__)
|
|
|
|
if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
|
2007-04-05 00:45:58 +02:00
|
|
|
#else
|
|
|
|
if (iopl(3) != 0) {
|
|
|
|
#endif
|
2008-10-18 23:14:13 +02:00
|
|
|
fprintf(stderr, "ERROR: Could not get IO privileges (%s).\nYou need to be root.\n", strerror(errno));
|
2007-04-05 00:45:58 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
/* Initialize PCI access for flash enables */
|
2007-04-05 00:45:58 +02:00
|
|
|
pacc = pci_alloc(); /* Get the pci_access structure */
|
|
|
|
/* Set all options you want -- here we stick with the defaults */
|
|
|
|
pci_init(pacc); /* Initialize the PCI library */
|
|
|
|
pci_scan_bus(pacc); /* We want to get the list of devices */
|
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
myusec_calibrate_delay();
|
2005-11-26 22:55:36 +01:00
|
|
|
|
|
|
|
/* We look at the lbtable first to see if we need a
|
|
|
|
* mainboard specific flash enable sequence.
|
|
|
|
*/
|
2008-01-18 16:33:10 +01:00
|
|
|
coreboot_init();
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
/* try to enable it. Failure IS an option, since not all motherboards
|
2005-11-26 22:55:36 +01:00
|
|
|
* really need this to be done, etc., etc.
|
2004-03-17 23:22:08 +01:00
|
|
|
*/
|
2007-05-09 12:17:44 +02:00
|
|
|
ret = chipset_flash_enable();
|
|
|
|
if (ret == -2) {
|
|
|
|
printf("WARNING: No chipset found. Flash detection "
|
|
|
|
"will most likely fail.\n");
|
|
|
|
}
|
2007-04-05 00:45:58 +02:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
board_flash_enable(lb_vendor, lb_part);
|
2004-03-20 17:46:10 +01:00
|
|
|
|
2008-05-08 02:31:44 +02:00
|
|
|
for (i = 0; i < ARRAY_SIZE(flashes); i++) {
|
2008-10-18 23:14:13 +02:00
|
|
|
flashes[i] =
|
|
|
|
probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0);
|
2008-05-08 02:31:44 +02:00
|
|
|
if (!flashes[i])
|
|
|
|
for (i++; i < ARRAY_SIZE(flashes); i++)
|
|
|
|
flashes[i] = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flashes[1]) {
|
|
|
|
printf("Multiple flash chips were detected:");
|
|
|
|
for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++)
|
|
|
|
printf(" %s", flashes[i]->name);
|
|
|
|
printf("\nPlease specify which chip to use with the -c <chipname> option.\n");
|
|
|
|
exit(1);
|
|
|
|
} else if (!flashes[0]) {
|
2005-11-26 22:55:36 +01:00
|
|
|
printf("No EEPROM/flash device found.\n");
|
2008-06-18 04:08:40 +02:00
|
|
|
if (!force || !chip_to_probe) {
|
|
|
|
printf("If you know which flash chip you have, and if this version of flashrom\n");
|
|
|
|
printf("supports a similar flash chip, you can try to force read your chip. Run:\n");
|
|
|
|
printf("flashrom -f -r -c similar_supported_flash_chip filename\n");
|
|
|
|
printf("\n");
|
|
|
|
printf("Note: flashrom can never write when the flash chip isn't found automatically.\n");
|
|
|
|
}
|
|
|
|
if (force && read_it && chip_to_probe) {
|
|
|
|
printf("Force read (-f -r -c) requested, forcing chip probe success:\n");
|
|
|
|
flashes[0] = probe_flash(flashchips, 1);
|
|
|
|
if (!flashes[0]) {
|
|
|
|
printf("flashrom does not support a flash chip named '%s'.\n", chip_to_probe);
|
|
|
|
printf("Run flashrom -L to view the hardware supported in this flashrom version.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-10-10 22:43:17 +02:00
|
|
|
if (!filename) {
|
|
|
|
printf("Error: No filename specified.\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-06-18 04:08:40 +02:00
|
|
|
size = flashes[0]->total_size * 1024;
|
|
|
|
buf = (uint8_t *) calloc(size, sizeof(char));
|
|
|
|
|
|
|
|
if ((image = fopen(filename, "w")) == NULL) {
|
|
|
|
perror(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-11-28 22:36:51 +01:00
|
|
|
printf("Force reading flash... ");
|
2008-06-18 04:08:40 +02:00
|
|
|
if (!flashes[0]->read)
|
|
|
|
memcpy(buf, (const char *)flashes[0]->virtual_memory, size);
|
|
|
|
else
|
|
|
|
flashes[0]->read(flashes[0], buf);
|
|
|
|
|
|
|
|
if (exclude_end_position - exclude_start_position > 0)
|
|
|
|
memset(buf + exclude_start_position, 0,
|
2008-10-18 23:14:13 +02:00
|
|
|
exclude_end_position -
|
|
|
|
exclude_start_position);
|
2008-06-18 04:08:40 +02:00
|
|
|
|
2009-01-12 22:00:35 +01:00
|
|
|
numbytes = fwrite(buf, 1, size, image);
|
2008-06-18 04:08:40 +02:00
|
|
|
fclose(image);
|
2009-01-12 22:00:35 +01:00
|
|
|
printf("%s.\n", numbytes == size ? "done" : "FAILED");
|
2008-06-18 04:08:40 +02:00
|
|
|
free(buf);
|
2009-01-12 22:00:35 +01:00
|
|
|
return numbytes != size;
|
2008-06-18 04:08:40 +02:00
|
|
|
}
|
2007-10-04 08:26:41 +02:00
|
|
|
// FIXME: flash writes stay enabled!
|
2004-03-17 23:22:08 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2008-05-08 02:31:44 +02:00
|
|
|
flash = flashes[0];
|
|
|
|
|
2008-05-03 06:34:37 +02:00
|
|
|
if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
|
|
|
|
printf("===\n");
|
|
|
|
if (flash->tested & TEST_BAD_MASK) {
|
|
|
|
printf("This flash part has status NOT WORKING for operations:");
|
|
|
|
if (flash->tested & TEST_BAD_PROBE)
|
|
|
|
printf(" PROBE");
|
|
|
|
if (flash->tested & TEST_BAD_READ)
|
|
|
|
printf(" READ");
|
|
|
|
if (flash->tested & TEST_BAD_ERASE)
|
|
|
|
printf(" ERASE");
|
|
|
|
if (flash->tested & TEST_BAD_WRITE)
|
|
|
|
printf(" WRITE");
|
|
|
|
printf("\n");
|
2008-11-29 00:45:27 +01:00
|
|
|
}
|
|
|
|
if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
|
|
|
|
(!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
|
|
|
|
(!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
|
|
|
|
(!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
|
2008-05-03 06:34:37 +02:00
|
|
|
printf("This flash part has status UNTESTED for operations:");
|
2008-11-29 00:45:27 +01:00
|
|
|
if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
|
2008-05-03 06:34:37 +02:00
|
|
|
printf(" PROBE");
|
2008-11-29 00:45:27 +01:00
|
|
|
if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
|
2008-05-03 06:34:37 +02:00
|
|
|
printf(" READ");
|
2008-11-29 00:45:27 +01:00
|
|
|
if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
|
2008-05-03 06:34:37 +02:00
|
|
|
printf(" ERASE");
|
2008-11-29 00:45:27 +01:00
|
|
|
if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
|
2008-05-03 06:34:37 +02:00
|
|
|
printf(" WRITE");
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
printf("Please email a report to flashrom@coreboot.org if any of the above operations\n");
|
|
|
|
printf("work correctly for you with this flash part. Please include the full output\n");
|
|
|
|
printf("from the program, including chipset found. Thank you for your help!\n");
|
|
|
|
printf("===\n");
|
|
|
|
}
|
2005-11-26 22:55:36 +01:00
|
|
|
|
2007-10-04 08:26:41 +02:00
|
|
|
if (!(read_it | write_it | verify_it | erase_it)) {
|
|
|
|
printf("No operations were specified.\n");
|
|
|
|
// FIXME: flash writes stay enabled!
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2004-12-08 21:10:01 +01:00
|
|
|
if (!filename && !erase_it) {
|
2007-10-04 08:26:41 +02:00
|
|
|
printf("Error: No filename specified.\n");
|
|
|
|
// FIXME: flash writes stay enabled!
|
|
|
|
exit(1);
|
2004-03-17 23:22:08 +01:00
|
|
|
}
|
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
size = flash->total_size * 1024;
|
|
|
|
buf = (uint8_t *) calloc(size, sizeof(char));
|
2007-05-09 12:17:44 +02:00
|
|
|
|
2004-12-08 21:10:01 +01:00
|
|
|
if (erase_it) {
|
2009-01-28 01:27:54 +01:00
|
|
|
if (erase_flash(flash))
|
2008-04-28 16:47:30 +02:00
|
|
|
return 1;
|
2004-12-08 21:10:01 +01:00
|
|
|
} else if (read_it) {
|
2009-01-28 01:27:54 +01:00
|
|
|
if (read_flash(flash, filename, exclude_start_position, exclude_end_position))
|
2009-01-12 22:00:35 +01:00
|
|
|
return 1;
|
2004-03-17 23:22:08 +01:00
|
|
|
} else {
|
2006-11-22 00:48:51 +01:00
|
|
|
struct stat image_stat;
|
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
if ((image = fopen(filename, "r")) == NULL) {
|
2004-03-17 23:22:08 +01:00
|
|
|
perror(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2006-11-22 00:48:51 +01:00
|
|
|
if (fstat(fileno(image), &image_stat) != 0) {
|
|
|
|
perror(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
if (image_stat.st_size != flash->total_size * 1024) {
|
2008-05-23 00:47:04 +02:00
|
|
|
fprintf(stderr, "Error: Image size doesn't match\n");
|
2006-11-22 00:48:51 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2009-01-12 22:00:35 +01:00
|
|
|
numbytes = fread(buf, 1, size, image);
|
2008-06-18 04:08:40 +02:00
|
|
|
show_id(buf, size, force);
|
2004-03-17 23:22:08 +01:00
|
|
|
fclose(image);
|
2009-01-12 22:00:35 +01:00
|
|
|
if (numbytes != size) {
|
|
|
|
fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
|
|
|
|
return 1;
|
|
|
|
}
|
2004-03-17 23:22:08 +01:00
|
|
|
}
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
/* exclude range stuff. Nice idea, but at the moment it is only
|
|
|
|
* supported in hardware by the pm49fl004 chips.
|
|
|
|
* Instead of implementing this for all chips I suggest advancing
|
|
|
|
* it to the rom layout feature below and drop exclude range
|
|
|
|
* completely once all flash chips can do rom layouts. stepan
|
|
|
|
*/
|
2007-05-09 12:17:44 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
// ////////////////////////////////////////////////////////////
|
2004-12-07 04:15:51 +01:00
|
|
|
if (exclude_end_position - exclude_start_position > 0)
|
2007-05-09 12:17:44 +02:00
|
|
|
memcpy(buf + exclude_start_position,
|
2007-05-24 21:17:29 +02:00
|
|
|
(const char *)flash->virtual_memory +
|
|
|
|
exclude_start_position,
|
2007-05-09 12:17:44 +02:00
|
|
|
exclude_end_position - exclude_start_position);
|
2004-12-07 04:15:51 +01:00
|
|
|
|
2007-05-09 12:17:44 +02:00
|
|
|
exclude_start_page = exclude_start_position / flash->page_size;
|
|
|
|
if ((exclude_start_position % flash->page_size) != 0) {
|
2004-12-07 04:15:51 +01:00
|
|
|
exclude_start_page++;
|
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
exclude_end_page = exclude_end_position / flash->page_size;
|
2005-11-26 22:55:36 +01:00
|
|
|
// ////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// This should be moved into each flash part's code to do it
|
|
|
|
// cleanly. This does the job.
|
2007-05-23 19:20:56 +02:00
|
|
|
handle_romentries(buf, (uint8_t *) flash->virtual_memory);
|
2007-05-09 12:17:44 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
// ////////////////////////////////////////////////////////////
|
2007-05-09 12:17:44 +02:00
|
|
|
|
2008-04-28 16:47:30 +02:00
|
|
|
if (write_it) {
|
|
|
|
if (!flash->write) {
|
|
|
|
fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2006-01-04 17:42:57 +01:00
|
|
|
ret |= flash->write(flash, buf);
|
2008-04-28 16:47:30 +02:00
|
|
|
}
|
2005-11-26 22:55:36 +01:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
if (verify_it)
|
2006-01-04 17:42:57 +01:00
|
|
|
ret |= verify_flash(flash, buf);
|
2005-11-26 22:55:36 +01:00
|
|
|
|
2008-05-22 15:22:45 +02:00
|
|
|
#ifdef __FreeBSD__
|
|
|
|
close(io_fd);
|
|
|
|
#endif
|
2006-01-04 17:42:57 +01:00
|
|
|
return ret;
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|