First attempt to clean up SPI probing and create a common
[coreboot.git] / util / flashrom / flashrom.c
index 2aef991caf7d8f03fa80ae48a98294ff80b43a57..63aba1e75bbb3e0dd57e1bfedf7bd9aa26258edb 100644 (file)
@@ -1,28 +1,23 @@
 /*
- * flashrom.c: Flash programming utility
+ * This file is part of the flashrom project.
  *
- * Copyright 2000 Silicon Integrated System Corporation
- * Copyright 2004 Tyan Corp
- *     yhlu yhlu@tyan.com add exclude start and end option
- * Copyright 2005-2007 coresystems GmbH 
- *      Stefan Reinauer <stepan@coresystems.de> added rom layout
- *      support, and checking for suitable rom image, various fixes
- *      support for flashing the Technologic Systems 5300.
- * 
- *     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.
+ * Copyright (C) 2000 Silicon Integrated System Corporation
+ * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
+ * Copyright (C) 2005-2007 coresystems GmbH 
  *
- *     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.
+ * 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.
  *
- *     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.
+ * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
 
 #include <errno.h>
@@ -48,7 +43,7 @@
 char *chip_to_probe = NULL;
 struct pci_access *pacc;       /* For board and chipset_enable */
 int exclude_start_page, exclude_end_page;
-int force = 0, verbose = 0;
+int verbose = 0;
 int fd_mem;
 
 struct pci_dev *pci_dev_find(uint16_t vendor, uint16_t device)
@@ -79,8 +74,8 @@ struct pci_dev *pci_card_find(uint16_t vendor, uint16_t 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)))
+                       if ((card_vendor == pci_read_word(temp, PCI_SUBSYSTEM_VENDOR_ID)) &&
+                           (card_device == pci_read_word(temp, PCI_SUBSYSTEM_ID)))
                                return temp;
                }
 
@@ -104,7 +99,7 @@ int map_flash_registers(struct flashchip *flash)
        return 0;
 }
 
-struct flashchip *probe_flash(struct flashchip *flash)
+struct flashchip *probe_flash(struct flashchip *flash, int force)
 {
        volatile uint8_t *bios;
        unsigned long flash_baseaddr, size;
@@ -114,8 +109,13 @@ struct flashchip *probe_flash(struct flashchip *flash)
                        flash++;
                        continue;
                }
-               printf_debug("Probing for %s, %d KB\n",
-                            flash->name, flash->total_size);
+               printf_debug("Probing for %s %s, %d KB: ",
+                            flash->vendor, flash->name, flash->total_size);
+               if (!flash->probe && !force) {
+                       printf_debug("failed! flashrom has no probe function for this flash chip.\n");
+                       flash++;
+                       continue;
+               }
 
                size = flash->total_size * 1024;
 
@@ -134,9 +134,12 @@ struct flashchip *probe_flash(struct flashchip *flash)
                 */
 
                if (getpagesize() > size) {
+                       /*
+                        * 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.
+                        */
                        size = getpagesize();
-                       printf("WARNING: size: %d -> %ld (page size)\n",
-                              flash->total_size * 1024, (unsigned long)size);
                }
 
                bios = mmap(0, size, PROT_WRITE | PROT_READ, MAP_SHARED,
@@ -147,9 +150,10 @@ struct flashchip *probe_flash(struct flashchip *flash)
                }
                flash->virtual_memory = bios;
 
-               if (flash->probe(flash) == 1) {
-                       printf("%s found at physical address: 0x%lx\n",
-                              flash->name, flash_baseaddr);
+               if (force || flash->probe(flash) == 1) {
+                       printf("Found chip \"%s %s\" (%d KB) at physical address 0x%lx.\n",
+                              flash->vendor, flash->name, flash->total_size,
+                              flash_baseaddr);
                        return flash;
                }
                munmap((void *)bios, size);
@@ -164,9 +168,13 @@ int verify_flash(struct flashchip *flash, uint8_t *buf)
 {
        int idx;
        int total_size = flash->total_size * 1024;
-       volatile uint8_t *bios = flash->virtual_memory;
+       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);
 
-       printf("Verifying flash ");
+       printf("Verifying flash... ");
 
        if (verbose)
                printf("address: 0x00000000\b\b\b\b\b\b\b\b\b\b");
@@ -175,11 +183,12 @@ int verify_flash(struct flashchip *flash, uint8_t *buf)
                if (verbose && ((idx & 0xfff) == 0xfff))
                        printf("0x%08x", idx);
 
-               if (*(bios + idx) != *(buf + idx)) {
+               if (*(buf2 + idx) != *(buf + idx)) {
                        if (verbose) {
                                printf("0x%08x ", idx);
                        }
-                       printf("- FAILED\n");
+                       printf("FAILED!  Expected=0x%02x, Read=0x%02x\n",
+                               *(buf + idx), *(buf2 + idx));
                        return 1;
                }
 
@@ -189,44 +198,66 @@ int verify_flash(struct flashchip *flash, uint8_t *buf)
        if (verbose)
                printf("\b\b\b\b\b\b\b\b\b\b ");
 
-       printf("- VERIFIED         \n");
+       printf("VERIFIED.          \n");
 
        return 0;
 }
 
+void print_supported_chips(void)
+{
+       int i;
+
+       printf("Supported ROM chips:\n\n");
+
+       for (i = 0; flashchips[i].name != NULL; i++)
+               printf("%s %s\n", flashchips[i].vendor, flashchips[i].name);
+}
+
 void usage(const char *name)
 {
-       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");
+       printf("usage: %s [-rwvEVfLhR] [-c chipname] [-s exclude_start]\n", name);
+       printf("       [-e exclude_end] [-m [vendor:]part] [-l file.layout] [-i imagename] [file]\n");
        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"
+           ("   -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"
+            "   -L | --list-supported:            print supported devices\n"
+            "   -h | --help:                      print this help text\n"
+            "   -R | --version:                   print the version (release)\n"
+            "\n" " If no file is specified, then all that happens"
             " is that flash info is dumped.\n\n");
        exit(1);
 }
 
+void print_version(void)
+{
+       printf("flashrom r%s\n", FLASHROM_VERSION);
+}
+
 int main(int argc, char *argv[])
 {
        uint8_t *buf;
        unsigned long size;
        FILE *image;
-       struct flashchip *flash;
+       /* Probe for up to three flash chips. */
+       struct flashchip *flash, *flashes[3];
        int opt;
        int option_index = 0;
+       int force = 0;
        int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0;
-       int ret = 0;
+       int ret = 0, i;
+#ifdef __FreeBSD__
+       int io_fd;
+#endif
 
        static struct option long_options[] = {
                {"read", 0, 0, 'r'},
@@ -241,7 +272,9 @@ int main(int argc, char *argv[])
                {"force", 0, 0, 'f'},
                {"layout", 1, 0, 'l'},
                {"image", 1, 0, 'i'},
+               {"list-supported", 0, 0, 'L'},
                {"help", 0, 0, 'h'},
+               {"version", 0, 0, 'R'},
                {0, 0, 0, 0}
        };
 
@@ -259,7 +292,7 @@ int main(int argc, char *argv[])
        }
 
        setbuf(stdout, NULL);
-       while ((opt = getopt_long(argc, argv, "rwvVEfc:s:e:m:l:i:h",
+       while ((opt = getopt_long(argc, argv, "rRwvVEfc:s:e:m:l:i:Lh",
                                  long_options, &option_index)) != EOF) {
                switch (opt) {
                case 'r':
@@ -296,8 +329,8 @@ int main(int argc, char *argv[])
                                lb_vendor = tempstr;
                                lb_part = tempstr2;
                        } else {
-                               printf("warning: ignored wrong format of"
-                                      " mainboard: %s\n", tempstr);
+                               lb_vendor = NULL;
+                               lb_part = tempstr;
                        }
                        break;
                case 'f':
@@ -312,6 +345,16 @@ int main(int argc, char *argv[])
                        tempstr = strdup(optarg);
                        find_romentry(tempstr);
                        break;
+               case 'L':
+                       print_supported_chips();
+                       print_supported_chipsets();
+                       print_supported_boards();
+                       exit(0);
+                       break;
+               case 'R':
+                       print_version();
+                       exit(0);
+                       break;
                case 'h':
                default:
                        usage(argv[0]);
@@ -320,7 +363,7 @@ int main(int argc, char *argv[])
        }
 
        if (read_it && write_it) {
-               printf("-r and -w are mutually exclusive\n");
+               printf("Error: -r and -w are mutually exclusive.\n");
                usage(argv[0]);
        }
 
@@ -330,10 +373,12 @@ int main(int argc, char *argv[])
        /* First get full io access */
 #if defined (__sun) && (defined(__i386) || defined(__amd64))
        if (sysi86(SI86V86, V86SC_IOPL, PS_IOPL) != 0) {
+#elif defined(__FreeBSD__)
+       if ((io_fd = open("/dev/io", O_RDWR)) < 0) {
 #else
        if (iopl(3) != 0) {
 #endif
-               fprintf(stderr, "ERROR: iopl failed: \"%s\"\n",
+               fprintf(stderr, "ERROR: Could not get IO privileges (%s).\nYou need to be root.\n",
                        strerror(errno));
                exit(1);
        }
@@ -344,8 +389,8 @@ int main(int argc, char *argv[])
        pci_init(pacc);         /* Initialize the PCI library */
        pci_scan_bus(pacc);     /* We want to get the list of devices */
 
-       /* Open the memory device. A lot of functions need it */
-       if ((fd_mem = open(MEM_DEV, O_RDWR)) < 0) {
+       /* Open the memory device UNCACHED. That's important for MMIO. */
+       if ((fd_mem = open(MEM_DEV, O_RDWR|O_SYNC)) < 0) {
                perror("Error: Can not access memory using " MEM_DEV
                       ". You need to be root.");
                exit(1);
@@ -356,7 +401,7 @@ int main(int argc, char *argv[])
        /* We look at the lbtable first to see if we need a
         * mainboard specific flash enable sequence.
         */
-       linuxbios_init();
+       coreboot_init();
 
        /* try to enable it. Failure IS an option, since not all motherboards
         * really need this to be done, etc., etc.
@@ -369,24 +414,117 @@ int main(int argc, char *argv[])
 
        board_flash_enable(lb_vendor, lb_part);
 
-       if ((flash = probe_flash(flashchips)) == NULL) {
+       for (i = 0; i < ARRAY_SIZE(flashes); i++) {
+               flashes[i] = probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0);
+               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]) {
                printf("No EEPROM/flash device found.\n");
+               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);
+                       }
+                       size = flashes[0]->total_size * 1024;
+                       buf = (uint8_t *) calloc(size, sizeof(char));
+
+                       if ((image = fopen(filename, "w")) == NULL) {
+                               perror(filename);
+                               exit(1);
+                       }
+                       printf("Force reading flash...");
+                       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,
+                                      exclude_end_position - exclude_start_position);
+
+                       fwrite(buf, sizeof(char), size, image);
+                       fclose(image);
+                       printf("done\n");
+                       free(buf);
+                       exit(0);
+               }
+               // FIXME: flash writes stay enabled!
                exit(1);
        }
 
-       printf("Flash part is %s (%d KB)\n", flash->name, flash->total_size);
+       flash = flashes[0];
+
+       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");
+               } else {
+                       printf("This flash part has status UNTESTED for operations:");
+                       if (!(flash->tested & TEST_OK_PROBE))
+                               printf(" PROBE");
+                       if (!(flash->tested & TEST_OK_READ))
+                               printf(" READ");
+                       if (!(flash->tested & TEST_OK_ERASE))
+                               printf(" ERASE");
+                       if (!(flash->tested & TEST_OK_WRITE))
+                               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");
+       }
+
+       if (!(read_it | write_it | verify_it | erase_it)) {
+               printf("No operations were specified.\n");
+               // FIXME: flash writes stay enabled!
+               exit(1);
+       }
 
        if (!filename && !erase_it) {
-               // FIXME: Do we really want this feature implicitly?
-               printf("OK, only ENABLING flash write, but NOT FLASHING.\n");
-               return 0;
+               printf("Error: No filename specified.\n");
+               // FIXME: flash writes stay enabled!
+               exit(1);
        }
 
        size = flash->total_size * 1024;
        buf = (uint8_t *) calloc(size, sizeof(char));
 
        if (erase_it) {
-               printf("Erasing flash chip\n");
+               printf("Erasing flash chip.\n");
+               if (!flash->erase) {
+                       fprintf(stderr, "Error: flashrom has no erase function for this flash chip.\n");
+                       return 1;
+               }
                flash->erase(flash);
                exit(0);
        } else if (read_it) {
@@ -419,12 +557,12 @@ int main(int argc, char *argv[])
                        exit(1);
                }
                if (image_stat.st_size != flash->total_size * 1024) {
-                       fprintf(stderr, "Error: Image size doesnt match\n");
+                       fprintf(stderr, "Error: Image size doesn't match\n");
                        exit(1);
                }
 
                fread(buf, sizeof(char), size, image);
-               show_id(buf, size);
+               show_id(buf, size, force);
                fclose(image);
        }
 
@@ -455,11 +593,19 @@ int main(int argc, char *argv[])
 
        // ////////////////////////////////////////////////////////////
 
-       if (write_it)
+       if (write_it) {
+               if (!flash->write) {
+                       fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
+                       return 1;
+               }
                ret |= flash->write(flash, buf);
+       }
 
        if (verify_it)
                ret |= verify_flash(flash, buf);
 
+#ifdef __FreeBSD__
+       close(io_fd);
+#endif
        return ret;
 }