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 struct flashchip *flash;
251 int option_index = 0;
252 int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
255 static struct option long_options[] = {
257 {"write", 0, 0, 'w'},
258 {"erase", 0, 0, 'E'},
259 {"verify", 0, 0, 'v'},
261 {"estart", 1, 0, 's'},
263 {"mainboard", 1, 0, 'm'},
264 {"verbose", 0, 0, 'V'},
265 {"force", 0, 0, 'f'},
266 {"layout", 1, 0, 'l'},
267 {"image", 1, 0, 'i'},
268 {"list-supported", 0, 0, 'L'},
270 {"version", 0, 0, 'R'},
274 char *filename = NULL;
276 unsigned int exclude_start_position = 0, exclude_end_position = 0; // [x,y)
277 char *tempstr = NULL, *tempstr2 = NULL;
280 /* Yes, print them. */
282 printf_debug("The arguments are:\n");
283 for (i = 1; i < argc; ++i)
284 printf_debug("%s\n", argv[i]);
287 setbuf(stdout, NULL);
288 while ((opt = getopt_long(argc, argv, "rRwvVEfc:s:e:m:l:i:Lh",
289 long_options, &option_index)) != EOF) {
301 chip_to_probe = strdup(optarg);
310 tempstr = strdup(optarg);
311 sscanf(tempstr, "%x", &exclude_start_position);
314 tempstr = strdup(optarg);
315 sscanf(tempstr, "%x", &exclude_end_position);
318 tempstr = strdup(optarg);
319 strtok(tempstr, ":");
320 tempstr2 = strtok(NULL, ":");
333 tempstr = strdup(optarg);
334 if (read_romlayout(tempstr))
338 tempstr = strdup(optarg);
339 find_romentry(tempstr);
342 print_supported_chips();
343 print_supported_chipsets();
344 print_supported_boards();
358 if (read_it && write_it) {
359 printf("-r and -w are mutually exclusive\n");
364 filename = argv[optind++];
366 /* First get full io access */
367 #if defined (__sun) && (defined(__i386) || defined(__amd64))
368 if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
372 fprintf(stderr, "ERROR: iopl failed: \"%s\"\n",
377 /* Initialize PCI access for flash enables */
378 pacc = pci_alloc(); /* Get the pci_access structure */
379 /* Set all options you want -- here we stick with the defaults */
380 pci_init(pacc); /* Initialize the PCI library */
381 pci_scan_bus(pacc); /* We want to get the list of devices */
383 /* Open the memory device. A lot of functions need it */
384 if ((fd_mem = open(MEM_DEV, O_RDWR)) < 0) {
385 perror("Error: Can not access memory using " MEM_DEV
386 ". You need to be root.");
390 myusec_calibrate_delay();
392 /* We look at the lbtable first to see if we need a
393 * mainboard specific flash enable sequence.
397 /* try to enable it. Failure IS an option, since not all motherboards
398 * really need this to be done, etc., etc.
400 ret = chipset_flash_enable();
402 printf("WARNING: No chipset found. Flash detection "
403 "will most likely fail.\n");
406 board_flash_enable(lb_vendor, lb_part);
408 if ((flash = probe_flash(flashchips)) == NULL) {
409 printf("No EEPROM/flash device found.\n");
410 // FIXME: flash writes stay enabled!
414 printf("Flash part is %s (%d KB).\n", flash->name, flash->total_size);
415 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
417 if (flash->tested & TEST_BAD_MASK) {
418 printf("This flash part has status NOT WORKING for operations:");
419 if (flash->tested & TEST_BAD_PROBE)
421 if (flash->tested & TEST_BAD_READ)
423 if (flash->tested & TEST_BAD_ERASE)
425 if (flash->tested & TEST_BAD_WRITE)
429 printf("This flash part has status UNTESTED for operations:");
430 if (!(flash->tested & TEST_OK_PROBE))
432 if (!(flash->tested & TEST_OK_READ))
434 if (!(flash->tested & TEST_OK_ERASE))
436 if (!(flash->tested & TEST_OK_WRITE))
440 printf("Please email a report to flashrom@coreboot.org if any of the above operations\n");
441 printf("work correctly for you with this flash part. Please include the full output\n");
442 printf("from the program, including chipset found. Thank you for your help!\n");
446 if (!(read_it | write_it | verify_it | erase_it)) {
447 printf("No operations were specified.\n");
448 // FIXME: flash writes stay enabled!
452 if (!filename && !erase_it) {
453 printf("Error: No filename specified.\n");
454 // FIXME: flash writes stay enabled!
458 size = flash->total_size * 1024;
459 buf = (uint8_t *) calloc(size, sizeof(char));
462 printf("Erasing flash chip\n");
464 fprintf(stderr, "Error: flashrom has no erase function for this flash chip.\n");
469 } else if (read_it) {
470 if ((image = fopen(filename, "w")) == NULL) {
474 printf("Reading Flash...");
475 if (flash->read == NULL)
476 memcpy(buf, (const char *)flash->virtual_memory, size);
478 flash->read(flash, buf);
480 if (exclude_end_position - exclude_start_position > 0)
481 memset(buf + exclude_start_position, 0,
482 exclude_end_position - exclude_start_position);
484 fwrite(buf, sizeof(char), size, image);
488 struct stat image_stat;
490 if ((image = fopen(filename, "r")) == NULL) {
494 if (fstat(fileno(image), &image_stat) != 0) {
498 if (image_stat.st_size != flash->total_size * 1024) {
499 fprintf(stderr, "Error: Image size doesnt match\n");
503 fread(buf, sizeof(char), size, image);
508 /* exclude range stuff. Nice idea, but at the moment it is only
509 * supported in hardware by the pm49fl004 chips.
510 * Instead of implementing this for all chips I suggest advancing
511 * it to the rom layout feature below and drop exclude range
512 * completely once all flash chips can do rom layouts. stepan
515 // ////////////////////////////////////////////////////////////
516 if (exclude_end_position - exclude_start_position > 0)
517 memcpy(buf + exclude_start_position,
518 (const char *)flash->virtual_memory +
519 exclude_start_position,
520 exclude_end_position - exclude_start_position);
522 exclude_start_page = exclude_start_position / flash->page_size;
523 if ((exclude_start_position % flash->page_size) != 0) {
524 exclude_start_page++;
526 exclude_end_page = exclude_end_position / flash->page_size;
527 // ////////////////////////////////////////////////////////////
529 // This should be moved into each flash part's code to do it
530 // cleanly. This does the job.
531 handle_romentries(buf, (uint8_t *) flash->virtual_memory);
533 // ////////////////////////////////////////////////////////////
537 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
540 ret |= flash->write(flash, buf);
544 ret |= verify_flash(flash, buf);