rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 1 | /* |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 3 | * |
uwe | 555dd97 | 2007-09-09 20:21:05 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000 Silicon Integrated System Corporation |
| 5 | * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com> |
stepan | 838a3f1 | 2008-08-02 15:13:58 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005-2008 coresystems GmbH |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008,2009 Carl-Daniel Hailfinger |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 8 | * |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 13 | * |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 18 | * |
uwe | b25f1ea | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 27 | #include <unistd.h> |
| 28 | #include <stdio.h> |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 29 | #include <string.h> |
| 30 | #include <stdlib.h> |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 31 | #include <getopt.h> |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 32 | #include "flash.h" |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 33 | |
| 34 | char *chip_to_probe = NULL; |
stepan | 193c9c2 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 35 | int exclude_start_page, exclude_end_page; |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 36 | int verbose = 0; |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 37 | int programmer = PROGRAMMER_INTERNAL; |
uwe | 6934c4a | 2009-05-14 18:57:26 +0000 | [diff] [blame] | 38 | char *nic_pcidev = NULL; |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 39 | |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 40 | const struct programmer_entry programmer_table[] = { |
| 41 | { |
hailfinger | 6c69ab0 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 42 | .init = internal_init, |
| 43 | .shutdown = internal_shutdown, |
hailfinger | 11ae3c4 | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 44 | .map_flash_region = physmap, |
| 45 | .unmap_flash_region = physunmap, |
hailfinger | 6c69ab0 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 46 | .chip_readb = internal_chip_readb, |
| 47 | .chip_readw = internal_chip_readw, |
| 48 | .chip_readl = internal_chip_readl, |
| 49 | .chip_writeb = internal_chip_writeb, |
| 50 | .chip_writew = internal_chip_writew, |
| 51 | .chip_writel = internal_chip_writel, |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 52 | }, |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 53 | |
hailfinger | a9df33c | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 54 | { |
hailfinger | 6c69ab0 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 55 | .init = dummy_init, |
| 56 | .shutdown = dummy_shutdown, |
hailfinger | 11ae3c4 | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 57 | .map_flash_region = dummy_map, |
| 58 | .unmap_flash_region = dummy_unmap, |
hailfinger | 6c69ab0 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 59 | .chip_readb = dummy_chip_readb, |
| 60 | .chip_readw = dummy_chip_readw, |
| 61 | .chip_readl = dummy_chip_readl, |
| 62 | .chip_writeb = dummy_chip_writeb, |
| 63 | .chip_writew = dummy_chip_writew, |
| 64 | .chip_writel = dummy_chip_writel, |
hailfinger | a9df33c | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 65 | }, |
| 66 | |
uwe | 0f5a3a2 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 67 | { |
| 68 | .init = nic3com_init, |
| 69 | .shutdown = nic3com_shutdown, |
| 70 | .map_flash_region = nic3com_map, |
| 71 | .unmap_flash_region = nic3com_unmap, |
| 72 | .chip_readb = nic3com_chip_readb, |
| 73 | .chip_readw = nic3com_chip_readw, |
| 74 | .chip_readl = nic3com_chip_readl, |
| 75 | .chip_writeb = nic3com_chip_writeb, |
| 76 | .chip_writew = nic3com_chip_writew, |
| 77 | .chip_writel = nic3com_chip_writel, |
| 78 | }, |
| 79 | |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 80 | {}, |
| 81 | }; |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 82 | |
stuge | 5ff0e6c | 2009-01-26 00:39:57 +0000 | [diff] [blame] | 83 | void map_flash_registers(struct flashchip *flash) |
stepan | 15e64bc | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 84 | { |
stepan | 15e64bc | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 85 | size_t size = flash->total_size * 1024; |
hailfinger | 8577ad1 | 2009-05-11 14:01:17 +0000 | [diff] [blame] | 86 | /* Flash registers live 4 MByte below the flash. */ |
hailfinger | 11ae3c4 | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 87 | flash->virtual_registers = programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size); |
stepan | 15e64bc | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 88 | } |
| 89 | |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 90 | int read_memmapped(struct flashchip *flash, uint8_t *buf) |
| 91 | { |
| 92 | int i; |
| 93 | |
| 94 | /* We could do a memcpy as optimization if the flash is onboard */ |
| 95 | //memcpy(buf, (const char *)flash->virtual_memory, flash->total_size * 1024); |
| 96 | for (i = 0; i < flash->total_size * 1024; i++) |
| 97 | buf[i] = chip_readb(flash->virtual_memory + i); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
stuge | 56300c3 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 102 | struct flashchip *probe_flash(struct flashchip *first_flash, int force) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 103 | { |
stuge | 56300c3 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 104 | struct flashchip *flash; |
stuge | 0b8a7fb | 2008-12-03 23:36:48 +0000 | [diff] [blame] | 105 | unsigned long base = 0, size; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 106 | |
stuge | 56300c3 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 107 | for (flash = first_flash; flash && flash->name; flash++) { |
stuge | c1e55fe | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 108 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 109 | continue; |
stepan | b8361b9 | 2008-03-17 22:59:40 +0000 | [diff] [blame] | 110 | printf_debug("Probing for %s %s, %d KB: ", |
| 111 | flash->vendor, flash->name, flash->total_size); |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 112 | if (!flash->probe && !force) { |
stuge | 8ce3a3c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 113 | printf_debug("failed! flashrom has no probe function for this flash chip.\n"); |
stuge | 8ce3a3c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 114 | continue; |
| 115 | } |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 116 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 117 | size = flash->total_size * 1024; |
stepan | 782fb17 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 118 | |
hailfinger | 72d3b98 | 2009-05-09 07:27:23 +0000 | [diff] [blame] | 119 | base = flashbase ? flashbase : (0xffffffff - size + 1); |
hailfinger | 11ae3c4 | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 120 | flash->virtual_memory = programmer_map_flash_region("flash chip", base, size); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 121 | |
stuge | c1e55fe | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 122 | if (force) |
| 123 | break; |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 124 | |
stuge | 56300c3 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 125 | if (flash->probe(flash) != 1) |
| 126 | goto notfound; |
| 127 | |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 128 | if (first_flash == flashchips |
| 129 | || flash->model_id != GENERIC_DEVICE_ID) |
stuge | c1e55fe | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 130 | break; |
| 131 | |
stuge | 56300c3 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 132 | notfound: |
hailfinger | 11ae3c4 | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 133 | /* The intermediate cast to unsigned long works around a gcc warning bug. */ |
| 134 | programmer_unmap_flash_region((void *)(unsigned long)flash->virtual_memory, size); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 135 | } |
uwe | be4477b | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 136 | |
stuge | c1e55fe | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 137 | if (!flash || !flash->name) |
| 138 | return NULL; |
| 139 | |
| 140 | printf("Found chip \"%s %s\" (%d KB) at physical address 0x%lx.\n", |
stuge | ceb2afc | 2008-12-03 21:39:56 +0000 | [diff] [blame] | 141 | flash->vendor, flash->name, flash->total_size, base); |
stuge | c1e55fe | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 142 | return flash; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 143 | } |
| 144 | |
stepan | 193c9c2 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 145 | int verify_flash(struct flashchip *flash, uint8_t *buf) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 146 | { |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 147 | int idx; |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 148 | int total_size = flash->total_size * 1024; |
hailfinger | 94586ba | 2008-01-22 15:19:01 +0000 | [diff] [blame] | 149 | uint8_t *buf2 = (uint8_t *) calloc(total_size, sizeof(char)); |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 150 | if (!flash->read) { |
| 151 | printf("FAILED!\n"); |
| 152 | fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n"); |
| 153 | return 1; |
| 154 | } else |
hailfinger | 94586ba | 2008-01-22 15:19:01 +0000 | [diff] [blame] | 155 | flash->read(flash, buf2); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 156 | |
uwe | fd2d0fe | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 157 | printf("Verifying flash... "); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 158 | |
| 159 | if (verbose) |
| 160 | printf("address: 0x00000000\b\b\b\b\b\b\b\b\b\b"); |
| 161 | |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 162 | for (idx = 0; idx < total_size; idx++) { |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 163 | if (verbose && ((idx & 0xfff) == 0xfff)) |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 164 | printf("0x%08x", idx); |
| 165 | |
hailfinger | 94586ba | 2008-01-22 15:19:01 +0000 | [diff] [blame] | 166 | if (*(buf2 + idx) != *(buf + idx)) { |
stuge | 4eb58f1 | 2009-01-13 14:32:27 +0000 | [diff] [blame] | 167 | if (verbose) |
| 168 | printf("0x%08x FAILED!", idx); |
| 169 | else |
| 170 | printf("FAILED at 0x%08x!", idx); |
| 171 | printf(" Expected=0x%02x, Read=0x%02x\n", |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 172 | *(buf + idx), *(buf2 + idx)); |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 173 | return 1; |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 174 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 175 | |
| 176 | if (verbose && ((idx & 0xfff) == 0xfff)) |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 177 | printf("\b\b\b\b\b\b\b\b\b\b"); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 178 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 179 | if (verbose) |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 180 | printf("\b\b\b\b\b\b\b\b\b\b "); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 181 | |
uwe | fd2d0fe | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 182 | printf("VERIFIED. \n"); |
uwe | be4477b | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 183 | |
stepan | c98b80b | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 184 | return 0; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 185 | } |
| 186 | |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 187 | int read_flash(struct flashchip *flash, char *filename, unsigned int exclude_start_position, unsigned int exclude_end_position) |
| 188 | { |
| 189 | unsigned long numbytes; |
| 190 | FILE *image; |
| 191 | unsigned long size = flash->total_size * 1024; |
| 192 | unsigned char *buf = calloc(size, sizeof(char)); |
| 193 | if ((image = fopen(filename, "w")) == NULL) { |
| 194 | perror(filename); |
| 195 | exit(1); |
| 196 | } |
| 197 | printf("Reading flash... "); |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 198 | if (!flash->read) { |
| 199 | printf("FAILED!\n"); |
| 200 | fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n"); |
| 201 | return 1; |
| 202 | } else |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 203 | flash->read(flash, buf); |
| 204 | |
| 205 | if (exclude_end_position - exclude_start_position > 0) |
| 206 | memset(buf + exclude_start_position, 0, |
| 207 | exclude_end_position - exclude_start_position); |
| 208 | |
| 209 | numbytes = fwrite(buf, 1, size, image); |
| 210 | fclose(image); |
| 211 | printf("%s.\n", numbytes == size ? "done" : "FAILED"); |
| 212 | if (numbytes != size) |
| 213 | return 1; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int erase_flash(struct flashchip *flash) |
| 218 | { |
| 219 | uint32_t erasedbytes; |
| 220 | unsigned long size = flash->total_size * 1024; |
| 221 | unsigned char *buf = calloc(size, sizeof(char)); |
| 222 | printf("Erasing flash chip... "); |
| 223 | if (NULL == flash->erase) { |
| 224 | printf("FAILED!\n"); |
| 225 | fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n"); |
| 226 | return 1; |
| 227 | } |
hailfinger | 1e9ee0f | 2009-05-08 17:15:15 +0000 | [diff] [blame] | 228 | flash->erase(flash); |
| 229 | |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 230 | if (!flash->read) { |
| 231 | printf("FAILED!\n"); |
| 232 | fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n"); |
| 233 | return 1; |
| 234 | } else |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 235 | flash->read(flash, buf); |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 236 | |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 237 | for (erasedbytes = 0; erasedbytes < size; erasedbytes++) |
| 238 | if (0xff != buf[erasedbytes]) { |
| 239 | printf("FAILED!\n"); |
| 240 | fprintf(stderr, "ERROR at 0x%08x: Expected=0xff, Read=0x%02x\n", |
| 241 | erasedbytes, buf[erasedbytes]); |
| 242 | return 1; |
| 243 | } |
| 244 | printf("SUCCESS.\n"); |
| 245 | return 0; |
| 246 | } |
| 247 | |
uwe | e06bcf8 | 2009-04-24 16:17:41 +0000 | [diff] [blame] | 248 | #ifndef MAX |
stuge | cae7e53 | 2008-12-06 01:37:09 +0000 | [diff] [blame] | 249 | #define MAX(a, b) ((a) > (b) ? (a) : (b)) |
uwe | e06bcf8 | 2009-04-24 16:17:41 +0000 | [diff] [blame] | 250 | #endif |
stuge | cae7e53 | 2008-12-06 01:37:09 +0000 | [diff] [blame] | 251 | #define POS_PRINT(x) do { pos += strlen(x); printf(x); } while (0) |
| 252 | |
uwe | 16f9909 | 2008-03-12 11:54:51 +0000 | [diff] [blame] | 253 | void print_supported_chips(void) |
| 254 | { |
stuge | cae7e53 | 2008-12-06 01:37:09 +0000 | [diff] [blame] | 255 | int okcol = 0, pos = 0; |
| 256 | struct flashchip *f; |
uwe | 16f9909 | 2008-03-12 11:54:51 +0000 | [diff] [blame] | 257 | |
stuge | cae7e53 | 2008-12-06 01:37:09 +0000 | [diff] [blame] | 258 | for (f = flashchips; f->name != NULL; f++) { |
| 259 | if (GENERIC_DEVICE_ID == f->model_id) |
| 260 | continue; |
| 261 | okcol = MAX(okcol, strlen(f->vendor) + 1 + strlen(f->name)); |
| 262 | } |
| 263 | okcol = (okcol + 7) & ~7; |
uwe | 16f9909 | 2008-03-12 11:54:51 +0000 | [diff] [blame] | 264 | |
stuge | cae7e53 | 2008-12-06 01:37:09 +0000 | [diff] [blame] | 265 | POS_PRINT("Supported flash chips:"); |
| 266 | while (pos < okcol) { |
| 267 | printf("\t"); |
| 268 | pos += 8 - (pos % 8); |
| 269 | } |
| 270 | printf("Tested OK operations:\tKnown BAD operations:\n\n"); |
| 271 | |
| 272 | for (f = flashchips; f->name != NULL; f++) { |
| 273 | printf("%s %s", f->vendor, f->name); |
| 274 | pos = strlen(f->vendor) + 1 + strlen(f->name); |
| 275 | while (pos < okcol) { |
| 276 | printf("\t"); |
| 277 | pos += 8 - (pos % 8); |
| 278 | } |
| 279 | if ((f->tested & TEST_OK_MASK)) { |
| 280 | if ((f->tested & TEST_OK_PROBE)) |
| 281 | POS_PRINT("PROBE "); |
| 282 | if ((f->tested & TEST_OK_READ)) |
| 283 | POS_PRINT("READ "); |
| 284 | if ((f->tested & TEST_OK_ERASE)) |
| 285 | POS_PRINT("ERASE "); |
| 286 | if ((f->tested & TEST_OK_WRITE)) |
| 287 | POS_PRINT("WRITE"); |
| 288 | } |
| 289 | while (pos < okcol + 24) { |
| 290 | printf("\t"); |
| 291 | pos += 8 - (pos % 8); |
| 292 | } |
| 293 | if ((f->tested & TEST_BAD_MASK)) { |
| 294 | if ((f->tested & TEST_BAD_PROBE)) |
| 295 | printf("PROBE "); |
| 296 | if ((f->tested & TEST_BAD_READ)) |
| 297 | printf("READ "); |
| 298 | if ((f->tested & TEST_BAD_ERASE)) |
| 299 | printf("ERASE "); |
| 300 | if ((f->tested & TEST_BAD_WRITE)) |
| 301 | printf("WRITE"); |
| 302 | } |
| 303 | printf("\n"); |
| 304 | } |
uwe | 16f9909 | 2008-03-12 11:54:51 +0000 | [diff] [blame] | 305 | } |
| 306 | |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 307 | void usage(const char *name) |
| 308 | { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 309 | printf("usage: %s [-rwvEVfLhR] [-c chipname] [-s exclude_start]\n", |
| 310 | name); |
stuge | b9b411f | 2008-01-27 16:21:21 +0000 | [diff] [blame] | 311 | printf(" [-e exclude_end] [-m [vendor:]part] [-l file.layout] [-i imagename] [file]\n"); |
hailfinger | ca7f01d | 2009-05-01 10:53:49 +0000 | [diff] [blame] | 312 | printf("Please note that the command line interface for flashrom will " |
| 313 | "change before flashrom 1.0. Do not use flashrom in scripts or " |
| 314 | "other automated tools without checking that your flashrom " |
| 315 | "version won't interpret them in a totally different way.\n\n"); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 316 | printf |
stuge | b9b411f | 2008-01-27 16:21:21 +0000 | [diff] [blame] | 317 | (" -r | --read: read flash and save into file\n" |
| 318 | " -w | --write: write file into flash\n" |
| 319 | " -v | --verify: verify flash against file\n" |
| 320 | " -E | --erase: erase flash device\n" |
| 321 | " -V | --verbose: more verbose output\n" |
| 322 | " -c | --chip <chipname>: probe only for specified flash chip\n" |
| 323 | " -s | --estart <addr>: exclude start position\n" |
| 324 | " -e | --eend <addr>: exclude end postion\n" |
| 325 | " -m | --mainboard <[vendor:]part>: override mainboard settings\n" |
| 326 | " -f | --force: force write without checking image\n" |
| 327 | " -l | --layout <file.layout>: read rom layout from file\n" |
| 328 | " -i | --image <name>: only flash image name from flash layout\n" |
uwe | 16f9909 | 2008-03-12 11:54:51 +0000 | [diff] [blame] | 329 | " -L | --list-supported: print supported devices\n" |
hailfinger | 46bed04 | 2009-05-09 00:27:07 +0000 | [diff] [blame] | 330 | " -p | --programmer <name>: specify the programmer device\n" |
uwe | 16f9909 | 2008-03-12 11:54:51 +0000 | [diff] [blame] | 331 | " -h | --help: print this help text\n" |
stuge | b9b411f | 2008-01-27 16:21:21 +0000 | [diff] [blame] | 332 | " -R | --version: print the version (release)\n" |
uwe | 16f9909 | 2008-03-12 11:54:51 +0000 | [diff] [blame] | 333 | "\n" " If no file is specified, then all that happens" |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 334 | " is that flash info is dumped.\n\n"); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 335 | exit(1); |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 336 | } |
| 337 | |
uwe | fdeca09 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 338 | void print_version(void) |
| 339 | { |
hailfinger | a5cfc5f | 2009-05-04 12:18:10 +0000 | [diff] [blame] | 340 | printf("flashrom v%s\n", FLASHROM_VERSION); |
uwe | fdeca09 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 341 | } |
| 342 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 343 | int main(int argc, char *argv[]) |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 344 | { |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 345 | uint8_t *buf; |
stuge | bbcc2f1 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 346 | unsigned long size, numbytes; |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 347 | FILE *image; |
stuge | c7354dc | 2008-05-08 00:31:44 +0000 | [diff] [blame] | 348 | /* Probe for up to three flash chips. */ |
| 349 | struct flashchip *flash, *flashes[3]; |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 350 | int opt; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 351 | int option_index = 0; |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 352 | int force = 0; |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 353 | int read_it = 0, write_it = 0, erase_it = 0, verify_it = 0; |
stuge | c7354dc | 2008-05-08 00:31:44 +0000 | [diff] [blame] | 354 | int ret = 0, i; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 355 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 356 | static struct option long_options[] = { |
| 357 | {"read", 0, 0, 'r'}, |
| 358 | {"write", 0, 0, 'w'}, |
| 359 | {"erase", 0, 0, 'E'}, |
| 360 | {"verify", 0, 0, 'v'}, |
| 361 | {"chip", 1, 0, 'c'}, |
| 362 | {"estart", 1, 0, 's'}, |
| 363 | {"eend", 1, 0, 'e'}, |
| 364 | {"mainboard", 1, 0, 'm'}, |
| 365 | {"verbose", 0, 0, 'V'}, |
| 366 | {"force", 0, 0, 'f'}, |
| 367 | {"layout", 1, 0, 'l'}, |
| 368 | {"image", 1, 0, 'i'}, |
uwe | 16f9909 | 2008-03-12 11:54:51 +0000 | [diff] [blame] | 369 | {"list-supported", 0, 0, 'L'}, |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 370 | {"programmer", 1, 0, 'p'}, |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 371 | {"help", 0, 0, 'h'}, |
uwe | fdeca09 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 372 | {"version", 0, 0, 'R'}, |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 373 | {0, 0, 0, 0} |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 374 | }; |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 375 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 376 | char *filename = NULL; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 377 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 378 | unsigned int exclude_start_position = 0, exclude_end_position = 0; // [x,y) |
| 379 | char *tempstr = NULL, *tempstr2 = NULL; |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 380 | |
hailfinger | f17c52f | 2009-05-07 00:59:53 +0000 | [diff] [blame] | 381 | print_version(); |
| 382 | |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 383 | if (argc > 1) { |
| 384 | /* Yes, print them. */ |
| 385 | int i; |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 386 | printf_debug("The arguments are:\n"); |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 387 | for (i = 1; i < argc; ++i) |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 388 | printf_debug("%s\n", argv[i]); |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 389 | } |
| 390 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 391 | setbuf(stdout, NULL); |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 392 | while ((opt = getopt_long(argc, argv, "rRwvVEfc:s:e:m:l:i:p:Lh", |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 393 | long_options, &option_index)) != EOF) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 394 | switch (opt) { |
| 395 | case 'r': |
| 396 | read_it = 1; |
| 397 | break; |
| 398 | case 'w': |
| 399 | write_it = 1; |
| 400 | break; |
| 401 | case 'v': |
| 402 | verify_it = 1; |
| 403 | break; |
| 404 | case 'c': |
| 405 | chip_to_probe = strdup(optarg); |
| 406 | break; |
ollie | 077017b | 2004-03-18 20:27:33 +0000 | [diff] [blame] | 407 | case 'V': |
| 408 | verbose = 1; |
| 409 | break; |
ollie | 0eb62d6 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 410 | case 'E': |
| 411 | erase_it = 1; |
| 412 | break; |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 413 | case 's': |
| 414 | tempstr = strdup(optarg); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 415 | sscanf(tempstr, "%x", &exclude_start_position); |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 416 | break; |
| 417 | case 'e': |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 418 | tempstr = strdup(optarg); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 419 | sscanf(tempstr, "%x", &exclude_end_position); |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 420 | break; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 421 | case 'm': |
| 422 | tempstr = strdup(optarg); |
| 423 | strtok(tempstr, ":"); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 424 | tempstr2 = strtok(NULL, ":"); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 425 | if (tempstr2) { |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 426 | lb_vendor = tempstr; |
| 427 | lb_part = tempstr2; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 428 | } else { |
stuge | b9b411f | 2008-01-27 16:21:21 +0000 | [diff] [blame] | 429 | lb_vendor = NULL; |
| 430 | lb_part = tempstr; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 431 | } |
| 432 | break; |
| 433 | case 'f': |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 434 | force = 1; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 435 | break; |
| 436 | case 'l': |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 437 | tempstr = strdup(optarg); |
stepan | 14566ee | 2007-04-14 16:32:59 +0000 | [diff] [blame] | 438 | if (read_romlayout(tempstr)) |
| 439 | exit(1); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 440 | break; |
| 441 | case 'i': |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 442 | tempstr = strdup(optarg); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 443 | find_romentry(tempstr); |
| 444 | break; |
uwe | 16f9909 | 2008-03-12 11:54:51 +0000 | [diff] [blame] | 445 | case 'L': |
| 446 | print_supported_chips(); |
| 447 | print_supported_chipsets(); |
| 448 | print_supported_boards(); |
| 449 | exit(0); |
| 450 | break; |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 451 | case 'p': |
| 452 | if (strncmp(optarg, "internal", 8) == 0) { |
| 453 | programmer = PROGRAMMER_INTERNAL; |
hailfinger | a9df33c | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 454 | } else if (strncmp(optarg, "dummy", 5) == 0) { |
| 455 | programmer = PROGRAMMER_DUMMY; |
uwe | 0f5a3a2 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 456 | } else if (strncmp(optarg, "nic3com", 7) == 0) { |
| 457 | programmer = PROGRAMMER_NIC3COM; |
uwe | 6934c4a | 2009-05-14 18:57:26 +0000 | [diff] [blame] | 458 | if (optarg[7] == '=') |
| 459 | nic_pcidev = strdup(optarg + 8); |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 460 | } else { |
| 461 | printf("Error: Unknown programmer.\n"); |
| 462 | exit(1); |
| 463 | } |
| 464 | break; |
uwe | fdeca09 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 465 | case 'R': |
hailfinger | f17c52f | 2009-05-07 00:59:53 +0000 | [diff] [blame] | 466 | /* print_version() is always called during startup. */ |
uwe | fdeca09 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 467 | exit(0); |
| 468 | break; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 469 | case 'h': |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 470 | default: |
| 471 | usage(argv[0]); |
| 472 | break; |
| 473 | } |
| 474 | } |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 475 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 476 | if (read_it && write_it) { |
uwe | 017911e | 2008-05-22 22:47:04 +0000 | [diff] [blame] | 477 | printf("Error: -r and -w are mutually exclusive.\n"); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 478 | usage(argv[0]); |
| 479 | } |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 480 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 481 | if (optind < argc) |
| 482 | filename = argv[optind++]; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 483 | |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 484 | ret = programmer_init(); |
stepan | 927d4e2 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 485 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 486 | myusec_calibrate_delay(); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 487 | |
stuge | c7354dc | 2008-05-08 00:31:44 +0000 | [diff] [blame] | 488 | for (i = 0; i < ARRAY_SIZE(flashes); i++) { |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 489 | flashes[i] = |
| 490 | probe_flash(i ? flashes[i - 1] + 1 : flashchips, 0); |
stuge | c7354dc | 2008-05-08 00:31:44 +0000 | [diff] [blame] | 491 | if (!flashes[i]) |
| 492 | for (i++; i < ARRAY_SIZE(flashes); i++) |
| 493 | flashes[i] = NULL; |
| 494 | } |
| 495 | |
| 496 | if (flashes[1]) { |
| 497 | printf("Multiple flash chips were detected:"); |
| 498 | for (i = 0; i < ARRAY_SIZE(flashes) && flashes[i]; i++) |
| 499 | printf(" %s", flashes[i]->name); |
| 500 | printf("\nPlease specify which chip to use with the -c <chipname> option.\n"); |
| 501 | exit(1); |
| 502 | } else if (!flashes[0]) { |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 503 | printf("No EEPROM/flash device found.\n"); |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 504 | if (!force || !chip_to_probe) { |
| 505 | printf("If you know which flash chip you have, and if this version of flashrom\n"); |
| 506 | printf("supports a similar flash chip, you can try to force read your chip. Run:\n"); |
| 507 | printf("flashrom -f -r -c similar_supported_flash_chip filename\n"); |
| 508 | printf("\n"); |
| 509 | printf("Note: flashrom can never write when the flash chip isn't found automatically.\n"); |
| 510 | } |
| 511 | if (force && read_it && chip_to_probe) { |
| 512 | printf("Force read (-f -r -c) requested, forcing chip probe success:\n"); |
| 513 | flashes[0] = probe_flash(flashchips, 1); |
| 514 | if (!flashes[0]) { |
| 515 | printf("flashrom does not support a flash chip named '%s'.\n", chip_to_probe); |
| 516 | printf("Run flashrom -L to view the hardware supported in this flashrom version.\n"); |
| 517 | exit(1); |
| 518 | } |
stuge | d88bbe5 | 2008-10-10 20:43:17 +0000 | [diff] [blame] | 519 | if (!filename) { |
| 520 | printf("Error: No filename specified.\n"); |
| 521 | exit(1); |
| 522 | } |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 523 | size = flashes[0]->total_size * 1024; |
| 524 | buf = (uint8_t *) calloc(size, sizeof(char)); |
| 525 | |
| 526 | if ((image = fopen(filename, "w")) == NULL) { |
| 527 | perror(filename); |
| 528 | exit(1); |
| 529 | } |
uwe | 17efbed | 2008-11-28 21:36:51 +0000 | [diff] [blame] | 530 | printf("Force reading flash... "); |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 531 | if (!flashes[0]->read) { |
| 532 | printf("FAILED!\n"); |
| 533 | fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n"); |
| 534 | return 1; |
| 535 | } else |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 536 | flashes[0]->read(flashes[0], buf); |
| 537 | |
| 538 | if (exclude_end_position - exclude_start_position > 0) |
| 539 | memset(buf + exclude_start_position, 0, |
uwe | fa98ca1 | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 540 | exclude_end_position - |
| 541 | exclude_start_position); |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 542 | |
stuge | bbcc2f1 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 543 | numbytes = fwrite(buf, 1, size, image); |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 544 | fclose(image); |
stuge | bbcc2f1 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 545 | printf("%s.\n", numbytes == size ? "done" : "FAILED"); |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 546 | free(buf); |
stuge | bbcc2f1 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 547 | return numbytes != size; |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 548 | } |
stepan | 9cbfb25 | 2007-10-04 06:26:41 +0000 | [diff] [blame] | 549 | // FIXME: flash writes stay enabled! |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 550 | exit(1); |
| 551 | } |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 552 | |
stuge | c7354dc | 2008-05-08 00:31:44 +0000 | [diff] [blame] | 553 | flash = flashes[0]; |
| 554 | |
stuge | 9cd64bd | 2008-05-03 04:34:37 +0000 | [diff] [blame] | 555 | if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) { |
| 556 | printf("===\n"); |
| 557 | if (flash->tested & TEST_BAD_MASK) { |
| 558 | printf("This flash part has status NOT WORKING for operations:"); |
| 559 | if (flash->tested & TEST_BAD_PROBE) |
| 560 | printf(" PROBE"); |
| 561 | if (flash->tested & TEST_BAD_READ) |
| 562 | printf(" READ"); |
| 563 | if (flash->tested & TEST_BAD_ERASE) |
| 564 | printf(" ERASE"); |
| 565 | if (flash->tested & TEST_BAD_WRITE) |
| 566 | printf(" WRITE"); |
| 567 | printf("\n"); |
hailfinger | 6aaf46a | 2008-11-28 23:45:27 +0000 | [diff] [blame] | 568 | } |
| 569 | if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) || |
| 570 | (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) || |
| 571 | (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) || |
| 572 | (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) { |
stuge | 9cd64bd | 2008-05-03 04:34:37 +0000 | [diff] [blame] | 573 | printf("This flash part has status UNTESTED for operations:"); |
hailfinger | 6aaf46a | 2008-11-28 23:45:27 +0000 | [diff] [blame] | 574 | if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) |
stuge | 9cd64bd | 2008-05-03 04:34:37 +0000 | [diff] [blame] | 575 | printf(" PROBE"); |
hailfinger | 6aaf46a | 2008-11-28 23:45:27 +0000 | [diff] [blame] | 576 | if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) |
stuge | 9cd64bd | 2008-05-03 04:34:37 +0000 | [diff] [blame] | 577 | printf(" READ"); |
hailfinger | 6aaf46a | 2008-11-28 23:45:27 +0000 | [diff] [blame] | 578 | if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) |
stuge | 9cd64bd | 2008-05-03 04:34:37 +0000 | [diff] [blame] | 579 | printf(" ERASE"); |
hailfinger | 6aaf46a | 2008-11-28 23:45:27 +0000 | [diff] [blame] | 580 | if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE)) |
stuge | 9cd64bd | 2008-05-03 04:34:37 +0000 | [diff] [blame] | 581 | printf(" WRITE"); |
| 582 | printf("\n"); |
| 583 | } |
| 584 | printf("Please email a report to flashrom@coreboot.org if any of the above operations\n"); |
| 585 | printf("work correctly for you with this flash part. Please include the full output\n"); |
| 586 | printf("from the program, including chipset found. Thank you for your help!\n"); |
| 587 | printf("===\n"); |
| 588 | } |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 589 | |
stepan | 9cbfb25 | 2007-10-04 06:26:41 +0000 | [diff] [blame] | 590 | if (!(read_it | write_it | verify_it | erase_it)) { |
| 591 | printf("No operations were specified.\n"); |
| 592 | // FIXME: flash writes stay enabled! |
| 593 | exit(1); |
| 594 | } |
| 595 | |
ollie | 0eb62d6 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 596 | if (!filename && !erase_it) { |
stepan | 9cbfb25 | 2007-10-04 06:26:41 +0000 | [diff] [blame] | 597 | printf("Error: No filename specified.\n"); |
| 598 | // FIXME: flash writes stay enabled! |
| 599 | exit(1); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 600 | } |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 601 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 602 | size = flash->total_size * 1024; |
| 603 | buf = (uint8_t *) calloc(size, sizeof(char)); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 604 | |
ollie | 0eb62d6 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 605 | if (erase_it) { |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 606 | if (erase_flash(flash)) |
stuge | 8ce3a3c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 607 | return 1; |
ollie | 0eb62d6 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 608 | } else if (read_it) { |
hailfinger | d219a23 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 609 | if (read_flash(flash, filename, exclude_start_position, exclude_end_position)) |
stuge | bbcc2f1 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 610 | return 1; |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 611 | } else { |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 612 | struct stat image_stat; |
| 613 | |
ollie | 5b62157 | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 614 | if ((image = fopen(filename, "r")) == NULL) { |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 615 | perror(filename); |
| 616 | exit(1); |
| 617 | } |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 618 | if (fstat(fileno(image), &image_stat) != 0) { |
| 619 | perror(filename); |
| 620 | exit(1); |
| 621 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 622 | if (image_stat.st_size != flash->total_size * 1024) { |
uwe | 017911e | 2008-05-22 22:47:04 +0000 | [diff] [blame] | 623 | fprintf(stderr, "Error: Image size doesn't match\n"); |
stepan | 1da96c0 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 624 | exit(1); |
| 625 | } |
| 626 | |
stuge | bbcc2f1 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 627 | numbytes = fread(buf, 1, size, image); |
stuge | 98c09aa | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 628 | show_id(buf, size, force); |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 629 | fclose(image); |
stuge | bbcc2f1 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 630 | if (numbytes != size) { |
| 631 | fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size); |
| 632 | return 1; |
| 633 | } |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 634 | } |
| 635 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 636 | /* exclude range stuff. Nice idea, but at the moment it is only |
| 637 | * supported in hardware by the pm49fl004 chips. |
| 638 | * Instead of implementing this for all chips I suggest advancing |
| 639 | * it to the rom layout feature below and drop exclude range |
| 640 | * completely once all flash chips can do rom layouts. stepan |
| 641 | */ |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 642 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 643 | // //////////////////////////////////////////////////////////// |
hailfinger | 2306011 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 644 | /* FIXME: This memcpy will not work for SPI nor external flashers. |
| 645 | * Convert to chip_readb. |
| 646 | */ |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 647 | if (exclude_end_position - exclude_start_position > 0) |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 648 | memcpy(buf + exclude_start_position, |
uwe | cdf59ac | 2007-05-24 19:17:29 +0000 | [diff] [blame] | 649 | (const char *)flash->virtual_memory + |
| 650 | exclude_start_position, |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 651 | exclude_end_position - exclude_start_position); |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 652 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 653 | exclude_start_page = exclude_start_position / flash->page_size; |
| 654 | if ((exclude_start_position % flash->page_size) != 0) { |
ollie | a430280 | 2004-12-07 03:15:51 +0000 | [diff] [blame] | 655 | exclude_start_page++; |
| 656 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 657 | exclude_end_page = exclude_end_position / flash->page_size; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 658 | // //////////////////////////////////////////////////////////// |
yhlu | 5d4383a | 2004-10-20 05:07:16 +0000 | [diff] [blame] | 659 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 660 | // This should be moved into each flash part's code to do it |
| 661 | // cleanly. This does the job. |
stepan | 7cd945e | 2007-05-23 17:20:56 +0000 | [diff] [blame] | 662 | handle_romentries(buf, (uint8_t *) flash->virtual_memory); |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 663 | |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 664 | // //////////////////////////////////////////////////////////// |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 665 | |
stuge | 8ce3a3c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 666 | if (write_it) { |
| 667 | if (!flash->write) { |
| 668 | fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n"); |
| 669 | return 1; |
| 670 | } |
stepan | 83eca25 | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 671 | ret |= flash->write(flash, buf); |
stuge | 8ce3a3c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 672 | } |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 673 | |
ollie | 5672ac6 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 674 | if (verify_it) |
stepan | 83eca25 | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 675 | ret |= verify_flash(flash, buf); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 676 | |
hailfinger | abe249e | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 677 | programmer_shutdown(); |
| 678 | |
stepan | 83eca25 | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 679 | return ret; |
rminnich | 8d3ff91 | 2003-10-25 17:01:29 +0000 | [diff] [blame] | 680 | } |