flashrom.c 11.9 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 6
 * Copyright (C) 2000 Silicon Integrated System Corporation
 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
 * Copyright (C) 2005-2007 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;
46
int force = 0, 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 77 78 79 80
	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;
		}
81

82
	return NULL;
83 84
}

85 86 87 88 89 90
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
91
			 fd_mem, (off_t) (0xFFFFFFFF - 0x400000 - size + 1));
92 93 94 95 96 97 98 99 100 101

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

	return 0;
}

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

#ifdef TS5300
		// FIXME: Wrong place for this decision
Stefan Reinauer's avatar
Stefan Reinauer committed
119
		// FIXME: This should be autodetected. It is trivial.
Stefan Reinauer's avatar
Stefan Reinauer committed
120 121 122 123 124 125
		flash_baseaddr = 0x9400000;
#else
		flash_baseaddr = (0xffffffff - size + 1);
#endif

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

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

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

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

152
		flash++;
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
153
	}
Uwe Hermann's avatar
Uwe Hermann committed
154

155
	return NULL;
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
156 157
}

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

164
	printf("Verifying flash... ");
165 166 167 168

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

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

		if (*(bios + idx) != *(buf + idx)) {
			if (verbose) {
				printf("0x%08x ", idx);
			}
177
			printf("FAILED!\n");
Stefan Reinauer's avatar
Stefan Reinauer committed
178
			return 1;
179
		}
180 181

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

187
	printf("VERIFIED.          \n");
Uwe Hermann's avatar
Uwe Hermann committed
188

Stefan Reinauer's avatar
Stefan Reinauer committed
189
	return 0;
Ronald G. Minnich's avatar
Fixes  
Ronald G. Minnich committed
190 191
}

Ronald G. Minnich's avatar
Ronald G. Minnich committed
192 193
void usage(const char *name)
{
194 195
	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");
196 197
	printf
	    ("   -r | --read:                    read flash and save into file\n"
198
	     "   -w | --write:                   write file into flash\n"
199 200 201 202 203 204 205 206 207 208
	     "   -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"
209
            "\n" " If no file is specified, then all that happens"
210
	     " is that flash info is dumped.\n\n");
211
	exit(1);
Ronald G. Minnich's avatar
Ronald G. Minnich committed
212 213
}

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

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

242
	char *filename = NULL;
243

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

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

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

316 317 318 319
	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
320

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

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

335
	/* Initialize PCI access for flash enables */
336 337 338 339 340
	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
341 342
	/* Open the memory device. A lot of functions need it */
	if ((fd_mem = open(MEM_DEV, O_RDWR)) < 0) {
343 344
		perror("Error: Can not access memory using " MEM_DEV
		       ". You need to be root.");
Stefan Reinauer's avatar
Stefan Reinauer committed
345 346 347
		exit(1);
	}

348
	myusec_calibrate_delay();
349 350 351 352 353

	/* 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
354

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

364
	board_flash_enable(lb_vendor, lb_part);
365 366

	if ((flash = probe_flash(flashchips)) == NULL) {
367
		printf("No EEPROM/flash device found.\n");
368
		// FIXME: flash writes stay enabled!
369 370
		exit(1);
	}
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
371

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

374 375 376 377 378 379
	if (!(read_it | write_it | verify_it | erase_it)) {
		printf("No operations were specified.\n");
		// FIXME: flash writes stay enabled!
		exit(1);
	}

380
	if (!filename && !erase_it) {
381 382 383
		printf("Error: No filename specified.\n");
		// FIXME: flash writes stay enabled!
		exit(1);
384 385
	}

386 387
	size = flash->total_size * 1024;
	buf = (uint8_t *) calloc(size, sizeof(char));
388

389 390 391
	if (erase_it) {
		printf("Erasing flash chip\n");
		flash->erase(flash);
392
		exit(0);
393
	} else if (read_it) {
394
		if ((image = fopen(filename, "w")) == NULL) {
395 396 397 398
			perror(filename);
			exit(1);
		}
		printf("Reading Flash...");
Ollie Lho's avatar
Ollie Lho committed
399
		if (flash->read == NULL)
400
			memcpy(buf, (const char *)flash->virtual_memory, size);
401
		else
402
			flash->read(flash, buf);
403

404
		if (exclude_end_position - exclude_start_position > 0)
405 406
			memset(buf + exclude_start_position, 0,
			       exclude_end_position - exclude_start_position);
407

408 409 410 411
		fwrite(buf, sizeof(char), size, image);
		fclose(image);
		printf("done\n");
	} else {
412 413
		struct stat image_stat;

414
		if ((image = fopen(filename, "r")) == NULL) {
415 416 417
			perror(filename);
			exit(1);
		}
418 419 420 421
		if (fstat(fileno(image), &image_stat) != 0) {
			perror(filename);
			exit(1);
		}
422
		if (image_stat.st_size != flash->total_size * 1024) {
423
			fprintf(stderr, "Error: Image size doesnt match\n");
424 425 426
			exit(1);
		}

427
		fread(buf, sizeof(char), size, image);
428
		show_id(buf, size);
429 430
		fclose(image);
	}
Ronald G. Minnich's avatar
Ronald G. Minnich committed
431

432 433 434 435 436 437
	/* 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
	 */
438

439
	// ////////////////////////////////////////////////////////////
440
	if (exclude_end_position - exclude_start_position > 0)
441
		memcpy(buf + exclude_start_position,
Uwe Hermann's avatar
Uwe Hermann committed
442 443
		       (const char *)flash->virtual_memory +
		       exclude_start_position,
444
		       exclude_end_position - exclude_start_position);
445

446 447
	exclude_start_page = exclude_start_position / flash->page_size;
	if ((exclude_start_position % flash->page_size) != 0) {
448 449
		exclude_start_page++;
	}
450
	exclude_end_page = exclude_end_position / flash->page_size;
451 452 453 454
	// ////////////////////////////////////////////////////////////

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

457
	// ////////////////////////////////////////////////////////////
458

459
	if (write_it)
Stefan Reinauer's avatar
Stefan Reinauer committed
460
		ret |= flash->write(flash, buf);
461

462
	if (verify_it)
Stefan Reinauer's avatar
Stefan Reinauer committed
463
		ret |= verify_flash(flash, buf);
464

Stefan Reinauer's avatar
Stefan Reinauer committed
465
	return ret;
Ronald G. Minnich's avatar
Dammit  
Ronald G. Minnich committed
466
}