flashrom.c 11.7 KB
Newer Older
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
1
/*
Uwe Hermann's avatar
Uwe Hermann committed
2
 * flashrom.c: Flash programming utility
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
3 4
 *
 * Copyright 2000 Silicon Integrated System Corporation
5 6
 * Copyright 2004 Tyan Corp
 *	yhlu yhlu@tyan.com add exclude start and end option
Stefan Reinauer's avatar
Stefan Reinauer committed
7
 * Copyright 2005-2007 coresystems GmbH 
Stefan Reinauer's avatar
Stefan Reinauer committed
8 9 10
 *      Stefan Reinauer <stepan@coresystems.de> added rom layout
 *      support, and checking for suitable rom image, various fixes
 *      support for flashing the Technologic Systems 5300.
11
 * 
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *	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>
31 32
#include <sys/types.h>
#include <sys/stat.h>
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
33 34
#include <unistd.h>
#include <stdio.h>
Ronald G. Minnich's avatar
Ronald G. Minnich committed
35
#include <string.h>
Ronald G. Minnich's avatar
Ronald G. Minnich committed
36
#include <stdlib.h>
37
#include <getopt.h>
38 39 40 41 42 43 44 45 46
#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
47 48

#include "flash.h"
49 50 51
#include "lbtable.h"
#include "layout.h"
#include "debug.h"
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
52

Ronald G. Minnich's avatar
Ronald G. Minnich committed
53
char *chip_to_probe = NULL;
54
struct pci_access *pacc;	/* For board and chipset_enable */
Stefan Reinauer's avatar
Stefan Reinauer committed
55
int exclude_start_page, exclude_end_page;
56
int force = 0, verbose = 0;
Stefan Reinauer's avatar
Stefan Reinauer committed
57

Stefan Reinauer's avatar
Stefan Reinauer committed
58 59
int fd_mem;

60 61 62
/*
 *
 */
63
struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
64
{
65 66
	struct pci_dev *temp;
	struct pci_filter filter;
67

68 69 70
	pci_filter_init(NULL, &filter);
	filter.vendor = vendor;
	filter.device = device;
71

72 73 74
	for (temp = pacc->devices; temp; temp = temp->next)
		if (pci_filter_match(&filter, temp))
			return temp;
75

76
	return NULL;
77 78 79 80 81
}

/*
 *
 */
82 83
struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device,
			      uint16_t card_vendor, uint16_t card_device)
84
{
85 86 87 88 89 90 91 92 93 94 95 96 97
	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;
		}
98

99
	return NULL;
100 101
}

102
struct flashchip *probe_flash(struct flashchip *flash)
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
103
{
104
	volatile uint8_t *bios;
Stefan Reinauer's avatar
Stefan Reinauer committed
105
	unsigned long flash_baseaddr, size;
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
106

107
	while (flash->name != NULL) {
108
		if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
109 110 111
			flash++;
			continue;
		}
112 113
		printf_debug("Probing for %s, %d KB\n",
			     flash->name, flash->total_size);
Stefan Reinauer's avatar
Stefan Reinauer committed
114

115
		size = flash->total_size * 1024;
Stefan Reinauer's avatar
Stefan Reinauer committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129

#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.
		 */

130 131
		if (getpagesize() > size) {
			size = getpagesize();
Stefan Reinauer's avatar
Stefan Reinauer committed
132
			printf("WARNING: size: %d -> %ld (page size)\n",
133
			       flash->total_size * 1024, (unsigned long)size);
134
		}
135

136
		bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
137
			    fd_mem, (off_t) flash_baseaddr);
138
		if (bios == MAP_FAILED) {
139
			perror("Error: Can't mmap " MEM_DEV ".");
140 141
			exit(1);
		}
142
		flash->virtual_memory = bios;
143 144

		if (flash->probe(flash) == 1) {
145
			printf("%s found at physical address: 0x%lx\n",
Stefan Reinauer's avatar
Stefan Reinauer committed
146
			       flash->name, flash_baseaddr);
147 148
			return flash;
		}
149
		munmap((void *)bios, size);
Stefan Reinauer's avatar
Stefan Reinauer committed
150

151
		flash++;
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
152
	}
153
	return NULL;
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
154 155
}

Stefan Reinauer's avatar
Stefan Reinauer committed
156
int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
157
{
Stefan Reinauer's avatar
Stefan Reinauer committed
158
	int idx;
159
	int total_size = flash->total_size * 1024;
160
	volatile uint8_t *bios = flash->virtual_memory;
161

Stefan Reinauer's avatar
Stefan Reinauer committed
162
	printf("Verifying flash ");
163 164 165 166

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

Stefan Reinauer's avatar
Stefan Reinauer committed
167
	for (idx = 0; idx < total_size; idx++) {
168
		if (verbose && ((idx & 0xfff) == 0xfff))
Stefan Reinauer's avatar
Stefan Reinauer committed
169 170 171 172 173 174 175 176
			printf("0x%08x", idx);

		if (*(bios + idx) != *(buf + idx)) {
			if (verbose) {
				printf("0x%08x ", idx);
			}
			printf("- FAILED\n");
			return 1;
177
		}
178 179

		if (verbose && ((idx & 0xfff) == 0xfff))
180
			printf("\b\b\b\b\b\b\b\b\b\b");
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
181
	}
182
	if (verbose)
Stefan Reinauer's avatar
Stefan Reinauer committed
183
		printf("\b\b\b\b\b\b\b\b\b\b ");
184

Stefan Reinauer's avatar
Stefan Reinauer committed
185 186
	printf("- VERIFIED         \n");
	return 0;
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
187 188
}

Ronald G. Minnich's avatar
Ronald G. Minnich committed
189 190
void usage(const char *name)
{
191 192
	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");
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
	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");
209
	exit(1);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
210 211
}

212
int main(int argc, char *argv[])
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
213
{
214
	uint8_t *buf;
215
	unsigned long size;
216 217
	FILE *image;
	struct flashchip *flash;
218
	int opt;
219
	int option_index = 0;
220
	int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
Stefan Reinauer's avatar
Stefan Reinauer committed
221
	int ret = 0;
222

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	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}
238
	};
239

240
	char *filename = NULL;
241

242 243
	unsigned int exclude_start_position = 0, exclude_end_position = 0;	// [x,y)
	char *tempstr = NULL, *tempstr2 = NULL;
244 245 246 247

	if (argc > 1) {
		/* Yes, print them. */
		int i;
248
		printf_debug("The arguments are:\n");
249
		for (i = 1; i < argc; ++i)
250
			printf_debug("%s\n", argv[i]);
251 252
	}

253
	setbuf(stdout, NULL);
254 255
	while ((opt = getopt_long(argc, argv, "rwvVEfc:s:e:m:l:i:h",
				  long_options, &option_index)) != EOF) {
256 257 258 259 260 261 262 263 264 265 266 267 268
		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
269 270 271
		case 'V':
			verbose = 1;
			break;
272 273 274
		case 'E':
			erase_it = 1;
			break;
275 276
		case 's':
			tempstr = strdup(optarg);
277
			sscanf(tempstr, "%x", &exclude_start_position);
278 279
			break;
		case 'e':
280
			tempstr = strdup(optarg);
281
			sscanf(tempstr, "%x", &exclude_end_position);
282
			break;
283 284 285
		case 'm':
			tempstr = strdup(optarg);
			strtok(tempstr, ":");
286
			tempstr2 = strtok(NULL, ":");
287
			if (tempstr2) {
288 289
				lb_vendor = tempstr;
				lb_part = tempstr2;
290 291
			} else {
				printf("warning: ignored wrong format of"
292
				       " mainboard: %s\n", tempstr);
293 294 295
			}
			break;
		case 'f':
296
			force = 1;
297 298
			break;
		case 'l':
299
			tempstr = strdup(optarg);
300 301
			if (read_romlayout(tempstr))
				exit(1);
302 303
			break;
		case 'i':
304
			tempstr = strdup(optarg);
305 306 307
			find_romentry(tempstr);
			break;
		case 'h':
308 309 310 311 312
		default:
			usage(argv[0]);
			break;
		}
	}
313

314 315 316 317
	if (read_it && write_it) {
		printf("-r and -w are mutually exclusive\n");
		usage(argv[0]);
	}
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
318

319 320
	if (optind < argc)
		filename = argv[optind++];
Ronald G. Minnich's avatar
Ronald G. Minnich committed
321

322
	/* First get full io access */
323
#if defined (__sun) && (defined(__i386) || defined(__amd64))
324
	if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
325 326 327
#else
	if (iopl(3) != 0) {
#endif
328 329
		fprintf(stderr, "ERROR: iopl failed: \"%s\"\n",
			strerror(errno));
330 331 332
		exit(1);
	}

333
	/* Initialize PCI access for flash enables */
334 335 336 337 338
	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 */

Stefan Reinauer's avatar
Stefan Reinauer committed
339 340
	/* Open the memory device. A lot of functions need it */
	if ((fd_mem = open(MEM_DEV, O_RDWR)) < 0) {
341 342
		perror("Error: Can not access memory using " MEM_DEV
		       ". You need to be root.");
Stefan Reinauer's avatar
Stefan Reinauer committed
343 344 345
		exit(1);
	}

346
	myusec_calibrate_delay();
347 348 349 350 351

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

353
	/* try to enable it. Failure IS an option, since not all motherboards
354
	 * really need this to be done, etc., etc.
355
	 */
356 357 358 359 360
	ret = chipset_flash_enable();
	if (ret == -2) {
		printf("WARNING: No chipset found. Flash detection "
		       "will most likely fail.\n");
	}
361

362
	board_flash_enable(lb_vendor, lb_part);
363 364

	if ((flash = probe_flash(flashchips)) == NULL) {
365
		printf("No EEPROM/flash device found.\n");
366 367
		exit(1);
	}
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
368

369
	printf("Flash part is %s (%d KB)\n", flash->name, flash->total_size);
370

371
	if (!filename && !erase_it) {
372 373
		// FIXME: Do we really want this feature implicitly?
		printf("OK, only ENABLING flash write, but NOT FLASHING.\n");
374 375 376
		return 0;
	}

377 378
	size = flash->total_size * 1024;
	buf = (uint8_t *) calloc(size, sizeof(char));
379

380 381 382
	if (erase_it) {
		printf("Erasing flash chip\n");
		flash->erase(flash);
383
		exit(0);
384
	} else if (read_it) {
385
		if ((image = fopen(filename, "w")) == NULL) {
386 387 388 389
			perror(filename);
			exit(1);
		}
		printf("Reading Flash...");
Ollie Lho's avatar
Ollie Lho committed
390
		if (flash->read == NULL)
391
			memcpy(buf, (const char *)flash->virtual_memory, size);
392
		else
393
			flash->read(flash, buf);
394

395
		if (exclude_end_position - exclude_start_position > 0)
396 397
			memset(buf + exclude_start_position, 0,
			       exclude_end_position - exclude_start_position);
398

399 400 401 402
		fwrite(buf, sizeof(char), size, image);
		fclose(image);
		printf("done\n");
	} else {
403 404
		struct stat image_stat;

405
		if ((image = fopen(filename, "r")) == NULL) {
406 407 408
			perror(filename);
			exit(1);
		}
409 410 411 412
		if (fstat(fileno(image), &image_stat) != 0) {
			perror(filename);
			exit(1);
		}
413
		if (image_stat.st_size != flash->total_size * 1024) {
414
			fprintf(stderr, "Error: Image size doesnt match\n");
415 416 417
			exit(1);
		}

418
		fread(buf, sizeof(char), size, image);
419
		show_id(buf, size);
420 421
		fclose(image);
	}
Ronald G. Minnich's avatar
Ronald G. Minnich committed
422

423 424 425 426 427 428
	/* 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
	 */
429

430
	// ////////////////////////////////////////////////////////////
431
	if (exclude_end_position - exclude_start_position > 0)
432
		memcpy(buf + exclude_start_position,
433
		       (const char *)flash->virtual_memory + exclude_start_position,
434
		       exclude_end_position - exclude_start_position);
435

436 437
	exclude_start_page = exclude_start_position / flash->page_size;
	if ((exclude_start_position % flash->page_size) != 0) {
438 439
		exclude_start_page++;
	}
440
	exclude_end_page = exclude_end_position / flash->page_size;
441 442 443 444
	// ////////////////////////////////////////////////////////////

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

447
	// ////////////////////////////////////////////////////////////
448

449
	if (write_it)
Stefan Reinauer's avatar
Stefan Reinauer committed
450
		ret |= flash->write(flash, buf);
451

452
	if (verify_it)
Stefan Reinauer's avatar
Stefan Reinauer committed
453
		ret |= verify_flash(flash, buf);
454

Stefan Reinauer's avatar
Stefan Reinauer committed
455
	return ret;
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
456
}