2003-10-25 19:01:29 +02:00
|
|
|
/*
|
2007-04-12 01:31:45 +02:00
|
|
|
* flashrom.c: Flash programming utility
|
2003-10-25 19:01:29 +02:00
|
|
|
*
|
|
|
|
* Copyright 2000 Silicon Integrated System Corporation
|
2004-10-20 07:07:16 +02:00
|
|
|
* Copyright 2004 Tyan Corp
|
|
|
|
* yhlu yhlu@tyan.com add exclude start and end option
|
2007-04-06 13:58:03 +02:00
|
|
|
* Copyright 2005-2007 coresystems GmbH
|
2006-03-16 17:57:41 +01:00
|
|
|
* Stefan Reinauer <stepan@coresystems.de> added rom layout
|
|
|
|
* support, and checking for suitable rom image, various fixes
|
|
|
|
* support for flashing the Technologic Systems 5300.
|
2005-11-26 22:55:36 +01:00
|
|
|
*
|
2003-10-25 19:01:29 +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.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.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"
|
2005-11-26 22:55:36 +01:00
|
|
|
#include "lbtable.h"
|
|
|
|
#include "layout.h"
|
|
|
|
#include "debug.h"
|
2003-10-25 19:01:29 +02:00
|
|
|
|
|
|
|
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;
|
2007-05-09 12:17:44 +02:00
|
|
|
int force = 0, verbose = 0;
|
2005-12-18 17:41:10 +01:00
|
|
|
|
2007-04-06 13:58:03 +02:00
|
|
|
int fd_mem;
|
|
|
|
|
2007-04-05 00:45:58 +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)) {
|
|
|
|
if ((card_vendor == pci_read_word(temp, 0x2C)) &&
|
|
|
|
(card_device == pci_read_word(temp, 0x2E)))
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
struct flashchip *probe_flash(struct flashchip *flash)
|
2003-10-25 19:01:29 +02:00
|
|
|
{
|
2005-11-26 22:55:36 +01:00
|
|
|
volatile uint8_t *bios;
|
2007-04-06 13:58:03 +02:00
|
|
|
unsigned long flash_baseaddr, size;
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
while (flash->name != NULL) {
|
2004-03-27 01:18:15 +01:00
|
|
|
if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
|
2004-03-17 23:22:08 +01:00
|
|
|
flash++;
|
|
|
|
continue;
|
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
printf_debug("Probing for %s, %d KB\n",
|
|
|
|
flash->name, flash->total_size);
|
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
|
|
|
|
|
|
|
#ifdef TS5300
|
|
|
|
// FIXME: Wrong place for this decision
|
|
|
|
flash_baseaddr = 0x9400000;
|
|
|
|
#else
|
|
|
|
flash_baseaddr = (0xffffffff - size + 1);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* If getpagesize() > size ->
|
|
|
|
* `Error MMAP /dev/mem: Invalid argument'
|
|
|
|
* This should never happen as we don't support any flash chips
|
|
|
|
* smaller than 4k or 8k yet.
|
|
|
|
*/
|
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
if (getpagesize() > size) {
|
|
|
|
size = getpagesize();
|
2007-04-06 13:58:03 +02:00
|
|
|
printf("WARNING: size: %d -> %ld (page size)\n",
|
2007-05-09 12:17:44 +02:00
|
|
|
flash->total_size * 1024, (unsigned long)size);
|
2004-03-17 23:22:08 +01:00
|
|
|
}
|
2007-04-05 00:45:58 +02:00
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
|
2007-05-09 12:17:44 +02:00
|
|
|
fd_mem, (off_t) flash_baseaddr);
|
2004-03-17 23:22:08 +01:00
|
|
|
if (bios == MAP_FAILED) {
|
2007-04-05 00:45:58 +02:00
|
|
|
perror("Error: Can't mmap " MEM_DEV ".");
|
2004-03-17 23:22:08 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
flash->virt_addr = bios;
|
|
|
|
|
|
|
|
if (flash->probe(flash) == 1) {
|
2004-03-20 17:46:10 +01:00
|
|
|
printf("%s found at physical address: 0x%lx\n",
|
2007-04-06 13:58:03 +02:00
|
|
|
flash->name, flash_baseaddr);
|
2004-03-17 23:22:08 +01:00
|
|
|
return flash;
|
|
|
|
}
|
2007-05-09 12:17:44 +02:00
|
|
|
munmap((void *)bios, size);
|
2006-03-16 17:57:41 +01:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
flash++;
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
2004-03-17 23:22:08 +01:00
|
|
|
return NULL;
|
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;
|
2005-11-26 22:55:36 +01:00
|
|
|
volatile uint8_t *bios = flash->virt_addr;
|
2004-03-17 23:22:08 +01:00
|
|
|
|
2006-03-16 17:57:41 +01: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);
|
|
|
|
|
|
|
|
if (*(bios + idx) != *(buf + idx)) {
|
|
|
|
if (verbose) {
|
|
|
|
printf("0x%08x ", idx);
|
|
|
|
}
|
|
|
|
printf("- FAILED\n");
|
|
|
|
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
|
|
|
|
2006-03-16 17:57:41 +01:00
|
|
|
printf("- VERIFIED \n");
|
|
|
|
return 0;
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void usage(const char *name)
|
|
|
|
{
|
2006-08-03 12:49:09 +02:00
|
|
|
printf("usage: %s [-rwvEVfh] [-c chipname] [-s exclude_start]\n", name);
|
|
|
|
printf(" [-e exclude_end] [-m vendor:part] [-l file.layout] [-i imagename] [file]\n");
|
2007-05-09 12:17:44 +02:00
|
|
|
printf
|
|
|
|
(" -r | --read: read flash and save into file\n"
|
|
|
|
" -w | --write: write file into flash (default when\n"
|
|
|
|
" file is specified)\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"
|
|
|
|
"\n" " If no file is specified, then all that happens\n"
|
|
|
|
" 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
|
|
|
}
|
|
|
|
|
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;
|
2004-03-17 23:22:08 +01:00
|
|
|
unsigned long size;
|
2004-03-20 17:46:10 +01:00
|
|
|
FILE *image;
|
|
|
|
struct flashchip *flash;
|
2004-03-17 23:22:08 +01:00
|
|
|
int opt;
|
2005-11-26 22:55:36 +01:00
|
|
|
int option_index = 0;
|
2007-05-09 12:17:44 +02:00
|
|
|
int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
|
2006-01-04 17:42:57 +01:00
|
|
|
int ret = 0;
|
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'},
|
|
|
|
{"help", 0, 0, 'h'},
|
|
|
|
{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);
|
2007-05-09 12:17:44 +02:00
|
|
|
while ((opt = getopt_long(argc, argv, "rwvVEfc:s:e:m:l:i:h",
|
|
|
|
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 {
|
|
|
|
printf("warning: ignored wrong format of"
|
2007-05-09 12:17:44 +02:00
|
|
|
" mainboard: %s\n", 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;
|
|
|
|
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) {
|
|
|
|
printf("-r and -w are mutually exclusive\n");
|
|
|
|
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) {
|
2007-04-05 00:45:58 +02:00
|
|
|
#else
|
|
|
|
if (iopl(3) != 0) {
|
|
|
|
#endif
|
2007-05-09 12:17:44 +02:00
|
|
|
fprintf(stderr, "ERROR: iopl failed: \"%s\"\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 */
|
|
|
|
|
2007-04-06 13:58:03 +02:00
|
|
|
/* Open the memory device. A lot of functions need it */
|
|
|
|
if ((fd_mem = open(MEM_DEV, O_RDWR)) < 0) {
|
2007-05-09 12:17:44 +02:00
|
|
|
perror("Error: Can not access memory using " MEM_DEV
|
|
|
|
". You need to be root.");
|
2007-04-06 13:58:03 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
linuxbios_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
|
|
|
|
|
|
|
if ((flash = probe_flash(flashchips)) == NULL) {
|
2005-11-26 22:55:36 +01:00
|
|
|
printf("No EEPROM/flash device found.\n");
|
2004-03-17 23:22:08 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-10-25 19:01:29 +02:00
|
|
|
|
2006-10-07 02:23:51 +02:00
|
|
|
printf("Flash part is %s (%d KB)\n", flash->name, flash->total_size);
|
2005-11-26 22:55:36 +01:00
|
|
|
|
2004-12-08 21:10:01 +01:00
|
|
|
if (!filename && !erase_it) {
|
2005-11-26 22:55:36 +01:00
|
|
|
// FIXME: Do we really want this feature implicitly?
|
|
|
|
printf("OK, only ENABLING flash write, but NOT FLASHING.\n");
|
2004-03-17 23:22:08 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
printf("Erasing flash chip\n");
|
|
|
|
flash->erase(flash);
|
2007-05-09 12:17:44 +02:00
|
|
|
exit(0);
|
2004-12-08 21:10:01 +01:00
|
|
|
} else if (read_it) {
|
2004-03-20 17:46:10 +01:00
|
|
|
if ((image = fopen(filename, "w")) == NULL) {
|
2004-03-17 23:22:08 +01:00
|
|
|
perror(filename);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("Reading Flash...");
|
2004-03-19 23:10:07 +01:00
|
|
|
if (flash->read == NULL)
|
2007-05-09 12:17:44 +02:00
|
|
|
memcpy(buf, (const char *)flash->virt_addr, size);
|
2004-03-17 23:22:08 +01:00
|
|
|
else
|
2004-03-20 17:46:10 +01:00
|
|
|
flash->read(flash, buf);
|
2004-10-20 07:07:16 +02: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
|
|
|
memset(buf + exclude_start_position, 0,
|
|
|
|
exclude_end_position - exclude_start_position);
|
2004-10-20 07:07:16 +02:00
|
|
|
|
2004-03-17 23:22:08 +01:00
|
|
|
fwrite(buf, sizeof(char), size, image);
|
|
|
|
fclose(image);
|
|
|
|
printf("done\n");
|
|
|
|
} 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) {
|
2007-03-22 15:51:45 +01:00
|
|
|
fprintf(stderr, "Error: Image size doesnt match\n");
|
2006-11-22 00:48:51 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2004-03-20 17:46:10 +01:00
|
|
|
fread(buf, sizeof(char), size, image);
|
2005-11-26 22:55:36 +01:00
|
|
|
show_id(buf, size);
|
2004-03-17 23:22:08 +01:00
|
|
|
fclose(image);
|
|
|
|
}
|
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,
|
|
|
|
(const char *)flash->virt_addr + exclude_start_position,
|
|
|
|
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-09 12:17:44 +02:00
|
|
|
handle_romentries(buf, (uint8_t *) flash->virt_addr);
|
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
// ////////////////////////////////////////////////////////////
|
2007-05-09 12:17:44 +02:00
|
|
|
|
2005-11-26 22:55:36 +01:00
|
|
|
if (write_it)
|
2006-01-04 17:42:57 +01:00
|
|
|
ret |= flash->write(flash, buf);
|
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
|
|
|
|
2006-01-04 17:42:57 +01:00
|
|
|
return ret;
|
2003-10-25 19:01:29 +02:00
|
|
|
}
|