2 * This file is part of the flashrom project.
4 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
6 * Copyright (C) 2005-2007 coresystems GmbH
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 #include <sys/types.h>
35 #if defined (__sun) && (defined(__i386) || defined(__amd64))
37 #include <sys/sysi86.h>
39 #include <asm/sunddi.h>
43 char *chip_to_probe = NULL;
44 struct pci_access *pacc; /* For board and chipset_enable */
45 int exclude_start_page, exclude_end_page;
46 int force = 0, verbose = 0;
49 struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
52 struct pci_filter filter;
54 pci_filter_init(NULL, &filter);
55 filter.vendor = vendor;
56 filter.device = device;
58 for (temp = pacc->devices; temp; temp = temp->next)
59 if (pci_filter_match(&filter, temp))
65 struct pci_dev *pci_card_find(uint16_t vendor, uint16_t device,
66 uint16_t card_vendor, uint16_t card_device)
69 struct pci_filter filter;
71 pci_filter_init(NULL, &filter);
72 filter.vendor = vendor;
73 filter.device = device;
75 for (temp = pacc->devices; temp; temp = temp->next)
76 if (pci_filter_match(&filter, temp)) {
77 if ((card_vendor == pci_read_word(temp, 0x2C)) &&
78 (card_device == pci_read_word(temp, 0x2E)))
85 int map_flash_registers(struct flashchip *flash)
87 volatile uint8_t *registers;
88 size_t size = flash->total_size * 1024;
90 registers = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
91 fd_mem, (off_t) (0xFFFFFFFF - 0x400000 - size + 1));
93 if (registers == MAP_FAILED) {
94 perror("Can't mmap registers using " MEM_DEV);
97 flash->virtual_registers = registers;
102 struct flashchip *probe_flash(struct flashchip *flash)
104 volatile uint8_t *bios;
105 unsigned long flash_baseaddr, size;
107 while (flash->name != NULL) {
108 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) {
112 printf_debug("Probing for %s %s, %d KB: ",
113 flash->vendor, flash->name, flash->total_size);
115 printf_debug("failed! flashrom has no probe function for this flash chip.\n");
120 size = flash->total_size * 1024;
123 // FIXME: Wrong place for this decision
124 // FIXME: This should be autodetected. It is trivial.
125 flash_baseaddr = 0x9400000;
127 flash_baseaddr = (0xffffffff - size + 1);
130 /* If getpagesize() > size ->
131 * "Can't mmap memory using /dev/mem: Invalid argument"
132 * This should never happen as we don't support any flash chips
133 * smaller than 4k or 8k (yet).
136 if (getpagesize() > size) {
138 * if a flash size of 0 is mapped, we map a single page
139 * so we can probe in that area whether we know the
142 size = getpagesize();
145 bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
146 fd_mem, (off_t) flash_baseaddr);
147 if (bios == MAP_FAILED) {
148 perror("Can't mmap memory using " MEM_DEV);
151 flash->virtual_memory = bios;
153 if (flash->probe(flash) == 1) {
154 printf("%s found at physical address 0x%lx.\n",
155 flash->name, flash_baseaddr);
158 munmap((void *)bios, size);
166 int verify_flash(struct flashchip *flash, uint8_t *buf)
169 int total_size = flash->total_size * 1024;
170 uint8_t *buf2 = (uint8_t *) calloc(total_size, sizeof(char));
171 if (flash->read == NULL)
172 memcpy(buf2, (const char *)flash->virtual_memory, total_size);
174 flash->read(flash, buf2);
176 printf("Verifying flash... ");
179 printf("address: 0x00000000\b\b\b\b\b\b\b\b\b\b");
181 for (idx = 0; idx < total_size; idx++) {
182 if (verbose && ((idx & 0xfff) == 0xfff))
183 printf("0x%08x", idx);
185 if (*(buf2 + idx) != *(buf + idx)) {
187 printf("0x%08x ", idx);
193 if (verbose && ((idx & 0xfff) == 0xfff))
194 printf("\b\b\b\b\b\b\b\b\b\b");
197 printf("\b\b\b\b\b\b\b\b\b\b ");
199 printf("VERIFIED. \n");
204 void print_supported_chips(void)
208 printf("Supported ROM chips:\n\n");
210 for (i = 0; flashchips[i].name != NULL; i++)
211 printf("%s %s\n", flashchips[i].vendor, flashchips[i].name);
214 void usage(const char *name)
216 printf("usage: %s [-rwvEVfLhR] [-c chipname] [-s exclude_start]\n", name);
217 printf(" [-e exclude_end] [-m [vendor:]part] [-l file.layout] [-i imagename] [file]\n");
219 (" -r | --read: read flash and save into file\n"
220 " -w | --write: write file into flash\n"
221 " -v | --verify: verify flash against file\n"
222 " -E | --erase: erase flash device\n"
223 " -V | --verbose: more verbose output\n"
224 " -c | --chip <chipname>: probe only for specified flash chip\n"
225 " -s | --estart <addr>: exclude start position\n"
226 " -e | --eend <addr>: exclude end postion\n"
227 " -m | --mainboard <[vendor:]part>: override mainboard settings\n"
228 " -f | --force: force write without checking image\n"
229 " -l | --layout <file.layout>: read rom layout from file\n"
230 " -i | --image <name>: only flash image name from flash layout\n"
231 " -L | --list-supported: print supported devices\n"
232 " -h | --help: print this help text\n"
233 " -R | --version: print the version (release)\n"
234 "\n" " If no file is specified, then all that happens"
235 " is that flash info is dumped.\n\n");
239 void print_version(void)
241 printf("flashrom r%s\n", FLASHROM_VERSION);
244 int main(int argc, char *argv[])
249 /* Probe for up to three flash chips. */
250 struct flashchip *flash, *flashes[3];
252 int option_index = 0;
253 int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
256 static struct option long_options[] = {
258 {"write", 0, 0, 'w'},
259 {"erase", 0, 0, 'E'},
260 {"verify", 0, 0, 'v'},
262 {"estart", 1, 0, 's'},
264 {"mainboard", 1, 0, 'm'},
265 {"verbose", 0, 0, 'V'},
266 {"force", 0, 0, 'f'},
267 {"layout", 1, 0, 'l'},
268 {"image", 1, 0, 'i'},
269 {"list-supported", 0, 0, 'L'},
271 {"version", 0, 0, 'R'},
275 char *filename = NULL;
277 unsigned int exclude_start_position = 0, exclude_end_position = 0; // [x,y)
278 char *tempstr = NULL, *tempstr2 = NULL;
281 /* Yes, print them. */
283 printf_debug("The arguments are:\n");
284 for (i = 1; i < argc; ++i)
285 printf_debug("%s\n", argv[i]);
288 setbuf(stdout, NULL);
289 while ((opt = getopt_long(argc, argv, "rRwvVEfc:s:e:m:l:i:Lh",
290 long_options, &option_index)) != EOF) {
302 chip_to_probe = strdup(optarg);
311 tempstr = strdup(optarg);
312 sscanf(tempstr, "%x", &exclude_start_position);
315 tempstr = strdup(optarg);
316 sscanf(tempstr, "%x", &exclude_end_position);
319 tempstr = strdup(optarg);
320 strtok(tempstr, ":");
321 tempstr2 = strtok(NULL, ":");
334 tempstr = strdup(optarg);
335 if (read_romlayout(tempstr))
339 tempstr = strdup(optarg);
340 find_romentry(tempstr);
343 print_supported_chips();
344 print_supported_chipsets();
345 print_supported_boards();
359 if (read_it && write_it) {
360 printf("-r and -w are mutually exclusive\n");
365 filename = argv[optind++];
367 /* First get full io access */
368 #if defined (__sun) && (defined(__i386) || defined(__amd64))
369 if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
373 fprintf(stderr, "ERROR: iopl failed: \"%s\"\n",
378 /* Initialize PCI access for flash enables */
379 pacc = pci_alloc(); /* Get the pci_access structure */
380 /* Set all options you want -- here we stick with the defaults */
381 pci_init(pacc); /* Initialize the PCI library */
382 pci_scan_bus(pacc); /* We want to get the list of devices */
384 /* Open the memory device UNCACHED. That's important for MMIO. */
385 if ((fd_mem = open(MEM_DEV, O_RDWR|O_SYNC)) < 0) {
386 perror("Error: Can not access memory using " MEM_DEV
387 ". You need to be root.");
391 myusec_calibrate_delay();
393 /* We look at the lbtable first to see if we need a
394 * mainboard specific flash enable sequence.
398 /* try to enable it. Failure IS an option, since not all motherboards
399 * really need this to be done, etc., etc.
401 ret = chipset_flash_enable();
403 printf("WARNING: No chipset found. Flash detection "
404 "will most likely fail.\n");
407 board_flash_enable(lb_vendor, lb_part);
409 for (i = 0; i < ARRAY_SIZE(flashes); i++) {
410 flashes[i] = probe_flash(i ? flashes[i - 1] + 1 : flashchips);
412 for (i++; i < ARRAY_SIZE(flashes); i++)
417 printf("Multiple flash chips were detected:");
418 for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++)
419 printf(" %s", flashes[i]->name);
420 printf("\nPlease specify which chip to use with the -c <chipname> option.\n");
422 } else if (!flashes[0]) {
423 printf("No EEPROM/flash device found.\n");
424 // FIXME: flash writes stay enabled!
430 printf("Flash part is %s (%d KB).\n", flash->name, flash->total_size);
431 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
433 if (flash->tested & TEST_BAD_MASK) {
434 printf("This flash part has status NOT WORKING for operations:");
435 if (flash->tested & TEST_BAD_PROBE)
437 if (flash->tested & TEST_BAD_READ)
439 if (flash->tested & TEST_BAD_ERASE)
441 if (flash->tested & TEST_BAD_WRITE)
445 printf("This flash part has status UNTESTED for operations:");
446 if (!(flash->tested & TEST_OK_PROBE))
448 if (!(flash->tested & TEST_OK_READ))
450 if (!(flash->tested & TEST_OK_ERASE))
452 if (!(flash->tested & TEST_OK_WRITE))
456 printf("Please email a report to flashrom@coreboot.org if any of the above operations\n");
457 printf("work correctly for you with this flash part. Please include the full output\n");
458 printf("from the program, including chipset found. Thank you for your help!\n");
462 if (!(read_it | write_it | verify_it | erase_it)) {
463 printf("No operations were specified.\n");
464 // FIXME: flash writes stay enabled!
468 if (!filename && !erase_it) {
469 printf("Error: No filename specified.\n");
470 // FIXME: flash writes stay enabled!
474 size = flash->total_size * 1024;
475 buf = (uint8_t *) calloc(size, sizeof(char));
478 printf("Erasing flash chip\n");
480 fprintf(stderr, "Error: flashrom has no erase function for this flash chip.\n");
485 } else if (read_it) {
486 if ((image = fopen(filename, "w")) == NULL) {
490 printf("Reading Flash...");
491 if (flash->read == NULL)
492 memcpy(buf, (const char *)flash->virtual_memory, size);
494 flash->read(flash, buf);
496 if (exclude_end_position - exclude_start_position > 0)
497 memset(buf + exclude_start_position, 0,
498 exclude_end_position - exclude_start_position);
500 fwrite(buf, sizeof(char), size, image);
504 struct stat image_stat;
506 if ((image = fopen(filename, "r")) == NULL) {
510 if (fstat(fileno(image), &image_stat) != 0) {
514 if (image_stat.st_size != flash->total_size * 1024) {
515 fprintf(stderr, "Error: Image size doesnt match\n");
519 fread(buf, sizeof(char), size, image);
524 /* exclude range stuff. Nice idea, but at the moment it is only
525 * supported in hardware by the pm49fl004 chips.
526 * Instead of implementing this for all chips I suggest advancing
527 * it to the rom layout feature below and drop exclude range
528 * completely once all flash chips can do rom layouts. stepan
531 // ////////////////////////////////////////////////////////////
532 if (exclude_end_position - exclude_start_position > 0)
533 memcpy(buf + exclude_start_position,
534 (const char *)flash->virtual_memory +
535 exclude_start_position,
536 exclude_end_position - exclude_start_position);
538 exclude_start_page = exclude_start_position / flash->page_size;
539 if ((exclude_start_position % flash->page_size) != 0) {
540 exclude_start_page++;
542 exclude_end_page = exclude_end_position / flash->page_size;
543 // ////////////////////////////////////////////////////////////
545 // This should be moved into each flash part's code to do it
546 // cleanly. This does the job.
547 handle_romentries(buf, (uint8_t *) flash->virtual_memory);
549 // ////////////////////////////////////////////////////////////
553 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
556 ret |= flash->write(flash, buf);
560 ret |= verify_flash(flash, buf);