flashrom.c 17.5 KB
Newer Older
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
1
/*
2
 * This file is part of the flashrom project.
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
3
 *
4 5
 * Copyright (C) 2000 Silicon Integrated System Corporation
 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Stefan Reinauer's avatar
Stefan Reinauer committed
6
 * Copyright (C) 2005-2008 coresystems GmbH 
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
7
 *
8 9 10 11
 * 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.
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
12
 *
13 14 15 16
 * 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.
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
17
 *
18 19 20
 * 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
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
21 22 23 24 25
 */

#include <errno.h>
#include <fcntl.h>
#include <sys/mman.h>
26 27
#include <sys/types.h>
#include <sys/stat.h>
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
28 29
#include <unistd.h>
#include <stdio.h>
Ronald G. Minnich's avatar
Ronald G. Minnich committed
30
#include <string.h>
Ronald G. Minnich's avatar
Ronald G. Minnich committed
31
#include <stdlib.h>
32
#include <getopt.h>
33 34 35 36 37 38 39 40
#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
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
41 42
#include "flash.h"

Ronald G. Minnich's avatar
Ronald G. Minnich committed
43
char *chip_to_probe = NULL;
44
struct pci_access *pacc;	/* For board and chipset_enable */
Stefan Reinauer's avatar
Stefan Reinauer committed
45
int exclude_start_page, exclude_end_page;
Peter Stuge's avatar
Peter Stuge committed
46
int verbose = 0;
Stefan Reinauer's avatar
Stefan Reinauer committed
47 48
int fd_mem;

49
struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
50
{
51 52
	struct pci_dev *temp;
	struct pci_filter filter;
53

54 55 56
	pci_filter_init(NULL, &filter);
	filter.vendor = vendor;
	filter.device = device;
57

58 59 60
	for (temp = pacc->devices; temp; temp = temp->next)
		if (pci_filter_match(&filter, temp))
			return temp;
61

62
	return NULL;
63 64
}

65 66
struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device,
			      uint16_t card_vendor, uint16_t card_device)
67
{
68 69 70 71 72 73 74 75 76
	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)) {
77 78 79 80
			if ((card_vendor ==
			     pci_read_word(temp, PCI_SUBSYSTEM_VENDOR_ID))
			    && (card_device ==
				pci_read_word(temp, PCI_SUBSYSTEM_ID)))
81 82
				return temp;
		}
83

84
	return NULL;
85 86
}

87 88 89 90 91 92
int map_flash_registers(struct flashchip *flash)
{
	volatile uint8_t *registers;
	size_t size = flash->total_size * 1024;

	registers = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
Uwe Hermann's avatar
Uwe Hermann committed
93
			 fd_mem, (off_t) (0xFFFFFFFF - 0x400000 - size + 1));
94 95 96 97 98 99 100 101 102 103

	if (registers == MAP_FAILED) {
		perror("Can't mmap registers using " MEM_DEV);
		exit(1);
	}
	flash->virtual_registers = registers;

	return 0;
}

104
struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
105
{
106
	volatile uint8_t *bios;
107
	struct flashchip *flash;
108
	unsigned long base = 0, size;
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
109

110
	for (flash = first_flash; flash && flash->name; flash++) {
111
		if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
112
			continue;
113 114
		printf_debug("Probing for %s %s, %d KB: ",
			     flash->vendor, flash->name, flash->total_size);
Peter Stuge's avatar
Peter Stuge committed
115
		if (!flash->probe && !force) {
116 117 118
			printf_debug("failed! flashrom has no probe function for this flash chip.\n");
			continue;
		}
Stefan Reinauer's avatar
Stefan Reinauer committed
119

120
		size = flash->total_size * 1024;
Stefan Reinauer's avatar
Stefan Reinauer committed
121 122

		/* If getpagesize() > size -> 
Stefan Reinauer's avatar
Stefan Reinauer committed
123
		 * "Can't mmap memory using /dev/mem: Invalid argument"
Stefan Reinauer's avatar
Stefan Reinauer committed
124
		 * This should never happen as we don't support any flash chips
Stefan Reinauer's avatar
Stefan Reinauer committed
125
		 * smaller than 4k or 8k (yet).
Stefan Reinauer's avatar
Stefan Reinauer committed
126 127
		 */

128
		if (getpagesize() > size) {
129 130 131 132 133
			/*
			 * 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.
			 */
134 135
			size = getpagesize();
		}
136

Peter Stuge's avatar
Peter Stuge committed
137
		base = flashbase ? flashbase : (0xffffffff - size + 1);
138
		bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
Peter Stuge's avatar
Peter Stuge committed
139
			    fd_mem, (off_t) base);
140
		if (bios == MAP_FAILED) {
Stefan Reinauer's avatar
Stefan Reinauer committed
141
			perror("Can't mmap memory using " MEM_DEV);
142 143
			exit(1);
		}
144
		flash->virtual_memory = bios;
145

146 147 148
		if (force)
			break;

149 150 151
		if (flash->probe(flash) != 1)
			goto notfound;

152 153
		if (first_flash == flashchips
		    || flash->model_id != GENERIC_DEVICE_ID)
154
			break;
Stefan Reinauer's avatar
Stefan Reinauer committed
155

156
notfound:
157
		munmap((void *)bios, size);
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
158
	}
Uwe Hermann's avatar
Uwe Hermann committed
159

160 161 162 163
	if (!flash || !flash->name)
		return NULL;

	printf("Found chip \"%s %s\" (%d KB) at physical address 0x%lx.\n",
Peter Stuge's avatar
Peter Stuge committed
164 165
	       flash->vendor, flash->name, flash->total_size, base);
	flashbase = base;
166
	return flash;
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
167 168
}

Stefan Reinauer's avatar
Stefan Reinauer committed
169
int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
170
{
Stefan Reinauer's avatar
Stefan Reinauer committed
171
	int idx;
172
	int total_size = flash->total_size * 1024;
173 174 175 176 177
	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);
178

179
	printf("Verifying flash... ");
180 181 182 183

	if (verbose)
		printf("address: 0x00000000\b\b\b\b\b\b\b\b\b\b");

Stefan Reinauer's avatar
Stefan Reinauer committed
184
	for (idx = 0; idx < total_size; idx++) {
185
		if (verbose && ((idx & 0xfff) == 0xfff))
Stefan Reinauer's avatar
Stefan Reinauer committed
186 187
			printf("0x%08x", idx);

188
		if (*(buf2 + idx) != *(buf + idx)) {
Stefan Reinauer's avatar
Stefan Reinauer committed
189 190 191
			if (verbose) {
				printf("0x%08x ", idx);
			}
192
			printf("FAILED!  Expected=0x%02x, Read=0x%02x\n",
193
			       *(buf + idx), *(buf2 + idx));
Stefan Reinauer's avatar
Stefan Reinauer committed
194
			return 1;
195
		}
196 197

		if (verbose && ((idx & 0xfff) == 0xfff))
198
			printf("\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
199
	}
200
	if (verbose)
Stefan Reinauer's avatar
Stefan Reinauer committed
201
		printf("\b\b\b\b\b\b\b\b\b\b ");
202

203
	printf("VERIFIED.          \n");
Uwe Hermann's avatar
Uwe Hermann committed
204

Stefan Reinauer's avatar
Stefan Reinauer committed
205
	return 0;
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
206 207
}

208 209 210 211 212 213 214
void print_supported_chips(void)
{
	int i;

	printf("Supported ROM chips:\n\n");

	for (i = 0; flashchips[i].name != NULL; i++)
215
		printf("%s %s\n", flashchips[i].vendor, flashchips[i].name);
216 217
}

Ronald G. Minnich's avatar
Ronald G. Minnich committed
218 219
void usage(const char *name)
{
220 221
	printf("usage: %s [-rwvEVfLhR] [-c chipname] [-s exclude_start]\n",
	       name);
222
	printf("       [-e exclude_end] [-m [vendor:]part] [-l file.layout] [-i imagename] [file]\n");
223
	printf
224 225 226 227 228 229 230 231 232 233 234 235
	    ("   -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"
236 237
	     "   -L | --list-supported:            print supported devices\n"
	     "   -h | --help:                      print this help text\n"
238
	     "   -R | --version:                   print the version (release)\n"
239
	     "\n" " If no file is specified, then all that happens"
240
	     " is that flash info is dumped.\n\n");
241
	exit(1);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
242 243
}

244 245 246 247 248
void print_version(void)
{
	printf("flashrom r%s\n", FLASHROM_VERSION);
}

249
int main(int argc, char *argv[])
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
250
{
251
	uint8_t *buf;
252
	unsigned long size;
253
	size_t erasedbytes;
254
	FILE *image;
Claus Gindhart's avatar
Claus Gindhart committed
255 256
	/* Probe for up to three flash chips. */
	struct flashchip *flash, *flashes[3];
257
	int opt;
258
	int option_index = 0;
Peter Stuge's avatar
Peter Stuge committed
259
	int force = 0;
260
	int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
Claus Gindhart's avatar
Claus Gindhart committed
261
	int ret = 0, i;
262 263 264
#ifdef __FreeBSD__
	int io_fd;
#endif
265

266 267 268 269 270 271 272 273 274 275 276 277 278
	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'},
279
		{"list-supported", 0, 0, 'L'},
280
		{"help", 0, 0, 'h'},
281
		{"version", 0, 0, 'R'},
282
		{0, 0, 0, 0}
283
	};
284

285
	char *filename = NULL;
286

287 288
	unsigned int exclude_start_position = 0, exclude_end_position = 0;	// [x,y)
	char *tempstr = NULL, *tempstr2 = NULL;
289 290 291 292

	if (argc > 1) {
		/* Yes, print them. */
		int i;
293
		printf_debug("The arguments are:\n");
294
		for (i = 1; i < argc; ++i)
295
			printf_debug("%s\n", argv[i]);
296 297
	}

298
	setbuf(stdout, NULL);
299
	while ((opt = getopt_long(argc, argv, "rRwvVEfc:s:e:m:l:i:Lh",
300
				  long_options, &option_index)) != EOF) {
301 302 303 304 305 306 307 308 309 310 311 312 313
		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;
Ollie Lho's avatar
Ollie Lho committed
314 315 316
		case 'V':
			verbose = 1;
			break;
317 318 319
		case 'E':
			erase_it = 1;
			break;
320 321
		case 's':
			tempstr = strdup(optarg);
322
			sscanf(tempstr, "%x", &exclude_start_position);
323 324
			break;
		case 'e':
325
			tempstr = strdup(optarg);
326
			sscanf(tempstr, "%x", &exclude_end_position);
327
			break;
328 329 330
		case 'm':
			tempstr = strdup(optarg);
			strtok(tempstr, ":");
331
			tempstr2 = strtok(NULL, ":");
332
			if (tempstr2) {
333 334
				lb_vendor = tempstr;
				lb_part = tempstr2;
335
			} else {
336 337
				lb_vendor = NULL;
				lb_part = tempstr;
338 339 340
			}
			break;
		case 'f':
341
			force = 1;
342 343
			break;
		case 'l':
344
			tempstr = strdup(optarg);
345 346
			if (read_romlayout(tempstr))
				exit(1);
347 348
			break;
		case 'i':
349
			tempstr = strdup(optarg);
350 351
			find_romentry(tempstr);
			break;
352 353 354 355 356 357
		case 'L':
			print_supported_chips();
			print_supported_chipsets();
			print_supported_boards();
			exit(0);
			break;
358 359 360 361
		case 'R':
			print_version();
			exit(0);
			break;
362
		case 'h':
363 364 365 366 367
		default:
			usage(argv[0]);
			break;
		}
	}
368

369
	if (read_it && write_it) {
Uwe Hermann's avatar
Uwe Hermann committed
370
		printf("Error: -r and -w are mutually exclusive.\n");
371 372
		usage(argv[0]);
	}
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
373

374 375
	if (optind < argc)
		filename = argv[optind++];
Ronald G. Minnich's avatar
Ronald G. Minnich committed
376

377
	/* First get full io access */
378
#if defined (__sun) && (defined(__i386) || defined(__amd64))
379
	if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
380 381
#elif defined(__FreeBSD__)
	if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
382 383 384
#else
	if (iopl(3) != 0) {
#endif
385
		fprintf(stderr, "ERROR: Could not get IO privileges (%s).\nYou need to be root.\n", strerror(errno));
386 387 388
		exit(1);
	}

389
	/* Initialize PCI access for flash enables */
390 391 392 393 394
	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 */

395
	/* Open the memory device UNCACHED. That's important for MMIO. */
396
	if ((fd_mem = open(MEM_DEV, O_RDWR | O_SYNC)) < 0) {
397 398
		perror("Error: Can not access memory using " MEM_DEV
		       ". You need to be root.");
Stefan Reinauer's avatar
Stefan Reinauer committed
399 400 401
		exit(1);
	}

402
	myusec_calibrate_delay();
403 404 405 406

	/* We look at the lbtable first to see if we need a
	 * mainboard specific flash enable sequence.
	 */
Stefan Reinauer's avatar
Stefan Reinauer committed
407
	coreboot_init();
Ronald G. Minnich's avatar
Ronald G. Minnich committed
408

409
	/* try to enable it. Failure IS an option, since not all motherboards
410
	 * really need this to be done, etc., etc.
411
	 */
412 413 414 415 416
	ret = chipset_flash_enable();
	if (ret == -2) {
		printf("WARNING: No chipset found. Flash detection "
		       "will most likely fail.\n");
	}
417

418
	board_flash_enable(lb_vendor, lb_part);
419

Claus Gindhart's avatar
Claus Gindhart committed
420
	for (i = 0; i < ARRAY_SIZE(flashes); i++) {
421 422
		flashes[i] =
		    probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0);
Claus Gindhart's avatar
Claus Gindhart committed
423 424 425 426 427 428 429 430 431 432 433 434
		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]) {
435
		printf("No EEPROM/flash device found.\n");
Peter Stuge's avatar
Peter Stuge committed
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
		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);
			}
451 452 453 454
			if (!filename) {
				printf("Error: No filename specified.\n");
				exit(1);
			}
Peter Stuge's avatar
Peter Stuge committed
455 456 457 458 459 460 461
			size = flashes[0]->total_size * 1024;
			buf = (uint8_t *) calloc(size, sizeof(char));

			if ((image = fopen(filename, "w")) == NULL) {
				perror(filename);
				exit(1);
			}
462
			printf("Force reading flash... ");
Peter Stuge's avatar
Peter Stuge committed
463 464 465 466 467 468 469
			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,
470 471
				       exclude_end_position -
				       exclude_start_position);
Peter Stuge's avatar
Peter Stuge committed
472 473 474

			fwrite(buf, sizeof(char), size, image);
			fclose(image);
475
			printf("done.\n");
Peter Stuge's avatar
Peter Stuge committed
476 477 478
			free(buf);
			exit(0);
		}
479
		// FIXME: flash writes stay enabled!
480 481
		exit(1);
	}
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
482

Claus Gindhart's avatar
Claus Gindhart committed
483 484
	flash = flashes[0];

485 486 487 488 489 490 491 492 493 494 495 496 497
	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");
498 499 500 501 502
		}
		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))) {
503
			printf("This flash part has status UNTESTED for operations:");
504
			if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
505
				printf(" PROBE");
506
			if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
507
				printf(" READ");
508
			if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
509
				printf(" ERASE");
510
			if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
511 512 513 514 515 516 517 518
				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");
	}
519

520 521 522 523 524 525
	if (!(read_it | write_it | verify_it | erase_it)) {
		printf("No operations were specified.\n");
		// FIXME: flash writes stay enabled!
		exit(1);
	}

526
	if (!filename && !erase_it) {
527 528 529
		printf("Error: No filename specified.\n");
		// FIXME: flash writes stay enabled!
		exit(1);
530 531
	}

532 533
	size = flash->total_size * 1024;
	buf = (uint8_t *) calloc(size, sizeof(char));
534

535
	if (erase_it) {
536
		printf("Erasing flash chip... ");
537 538 539
		if (NULL == flash->erase) {
			printf("FAILED!\n");
			fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
540 541
			return 1;
		}
542
		flash->erase(flash);
543 544 545 546 547 548 549 550 551 552 553 554 555
		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;
556
	} else if (read_it) {
557
		if ((image = fopen(filename, "w")) == NULL) {
558 559 560
			perror(filename);
			exit(1);
		}
561
		printf("Reading flash... ");
Ollie Lho's avatar
Ollie Lho committed
562
		if (flash->read == NULL)
563
			memcpy(buf, (const char *)flash->virtual_memory, size);
564
		else
565
			flash->read(flash, buf);
566

567
		if (exclude_end_position - exclude_start_position > 0)
568 569
			memset(buf + exclude_start_position, 0,
			       exclude_end_position - exclude_start_position);
570

571 572
		fwrite(buf, sizeof(char), size, image);
		fclose(image);
573
		printf("done.\n");
574
	} else {
575 576
		struct stat image_stat;

577
		if ((image = fopen(filename, "r")) == NULL) {
578 579 580
			perror(filename);
			exit(1);
		}
581 582 583 584
		if (fstat(fileno(image), &image_stat) != 0) {
			perror(filename);
			exit(1);
		}
585
		if (image_stat.st_size != flash->total_size * 1024) {
Uwe Hermann's avatar
Uwe Hermann committed
586
			fprintf(stderr, "Error: Image size doesn't match\n");
587 588 589
			exit(1);
		}

590
		fread(buf, sizeof(char), size, image);
Peter Stuge's avatar
Peter Stuge committed
591
		show_id(buf, size, force);
592 593
		fclose(image);
	}
Ronald G. Minnich's avatar
Ronald G. Minnich committed
594

595 596 597 598 599 600
	/* 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
	 */
601

602
	// ////////////////////////////////////////////////////////////
603
	if (exclude_end_position - exclude_start_position > 0)
604
		memcpy(buf + exclude_start_position,
Uwe Hermann's avatar
Uwe Hermann committed
605 606
		       (const char *)flash->virtual_memory +
		       exclude_start_position,
607
		       exclude_end_position - exclude_start_position);
608

609 610
	exclude_start_page = exclude_start_position / flash->page_size;
	if ((exclude_start_position % flash->page_size) != 0) {
611 612
		exclude_start_page++;
	}
613
	exclude_end_page = exclude_end_position / flash->page_size;
614 615 616 617
	// ////////////////////////////////////////////////////////////

	// This should be moved into each flash part's code to do it 
	// cleanly. This does the job.
618
	handle_romentries(buf, (uint8_t *) flash->virtual_memory);
619

620
	// ////////////////////////////////////////////////////////////
621

622 623 624 625 626
	if (write_it) {
		if (!flash->write) {
			fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
			return 1;
		}
Stefan Reinauer's avatar
Stefan Reinauer committed
627
		ret |= flash->write(flash, buf);
628
	}
629

630
	if (verify_it)
Stefan Reinauer's avatar
Stefan Reinauer committed
631
		ret |= verify_flash(flash, buf);
632

633 634 635
#ifdef __FreeBSD__
	close(io_fd);
#endif
Stefan Reinauer's avatar
Stefan Reinauer committed
636
	return ret;
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
637
}