Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1 | /* |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 3 | * |
Uwe Hermann | d22a1d4 | 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> |
Uwe Hermann | c7e8a0c | 2009-05-19 14:14:21 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005-2008 coresystems GmbH |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008,2009 Carl-Daniel Hailfinger |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 8 | * |
Uwe Hermann | d110764 | 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. |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 13 | * |
Uwe Hermann | d110764 | 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. |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 18 | * |
Uwe Hermann | d110764 | 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 |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 27 | #include <string.h> |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 28 | #include <stdlib.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 29 | #include <getopt.h> |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 30 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 31 | #include "flashchips.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 32 | |
Carl-Daniel Hailfinger | a80cfbc | 2009-07-22 20:13:00 +0000 | [diff] [blame] | 33 | const char *flashrom_version = FLASHROM_VERSION; |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 34 | char *chip_to_probe = NULL; |
Peter Stuge | 7ffbc6f | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 35 | int verbose = 0; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 36 | |
| 37 | #if INTERNAL_SUPPORT == 1 |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 38 | enum programmer programmer = PROGRAMMER_INTERNAL; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 39 | #elif DUMMY_SUPPORT == 1 |
| 40 | enum programmer programmer = PROGRAMMER_DUMMY; |
| 41 | #else |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 42 | /* If neither internal nor dummy are selected, we must pick a sensible default. |
| 43 | * Since there is no reason to prefer a particular external programmer, we fail |
| 44 | * if more than one of them is selected. If only one is selected, it is clear |
| 45 | * that the user wants that one to become the default. |
| 46 | */ |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 47 | #if NIC3COM_SUPPORT+GFXNVIDIA_SUPPORT+DRKAISER_SUPPORT+SATASII_SUPPORT+ATAHPT_SUPPORT+FT2232_SPI_SUPPORT+SERPROG_SUPPORT+BUSPIRATE_SPI_SUPPORT+DEDIPROG_SUPPORT > 1 |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 48 | #error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all external programmers except one. |
| 49 | #endif |
| 50 | enum programmer programmer = |
| 51 | #if NIC3COM_SUPPORT == 1 |
| 52 | PROGRAMMER_NIC3COM |
| 53 | #endif |
| 54 | #if GFXNVIDIA_SUPPORT == 1 |
| 55 | PROGRAMMER_GFXNVIDIA |
| 56 | #endif |
| 57 | #if DRKAISER_SUPPORT == 1 |
| 58 | PROGRAMMER_DRKAISER |
| 59 | #endif |
| 60 | #if SATASII_SUPPORT == 1 |
| 61 | PROGRAMMER_SATASII |
| 62 | #endif |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 63 | #if ATAHPT_SUPPORT == 1 |
| 64 | PROGRAMMER_ATAHPT |
| 65 | #endif |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 66 | #if FT2232_SPI_SUPPORT == 1 |
| 67 | PROGRAMMER_FT2232SPI |
| 68 | #endif |
| 69 | #if SERPROG_SUPPORT == 1 |
| 70 | PROGRAMMER_SERPROG |
| 71 | #endif |
| 72 | #if BUSPIRATE_SPI_SUPPORT == 1 |
| 73 | PROGRAMMER_BUSPIRATESPI |
| 74 | #endif |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 75 | #if DEDIPROG_SUPPORT == 1 |
| 76 | PROGRAMMER_DEDIPROG |
| 77 | #endif |
Carl-Daniel Hailfinger | 1a1415c | 2010-01-10 13:28:48 +0000 | [diff] [blame] | 78 | ; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 79 | #endif |
| 80 | |
Carl-Daniel Hailfinger | ef58a9c | 2009-08-12 13:32:56 +0000 | [diff] [blame] | 81 | char *programmer_param = NULL; |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 82 | |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 83 | /** |
| 84 | * flashrom defaults to Parallel/LPC/FWH flash devices. If a known host |
| 85 | * controller is found, the init routine sets the buses_supported bitfield to |
| 86 | * contain the supported buses for that controller. |
| 87 | */ |
| 88 | enum chipbustype buses_supported = CHIP_BUSTYPE_NONSPI; |
| 89 | |
| 90 | /** |
| 91 | * Programmers supporting multiple buses can have differing size limits on |
| 92 | * each bus. Store the limits for each bus in a common struct. |
| 93 | */ |
| 94 | struct decode_sizes max_rom_decode = { |
| 95 | .parallel = 0xffffffff, |
| 96 | .lpc = 0xffffffff, |
| 97 | .fwh = 0xffffffff, |
| 98 | .spi = 0xffffffff |
| 99 | }; |
| 100 | |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 101 | const struct programmer_entry programmer_table[] = { |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 102 | #if INTERNAL_SUPPORT == 1 |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 103 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 104 | .name = "internal", |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 105 | .init = internal_init, |
| 106 | .shutdown = internal_shutdown, |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 107 | .map_flash_region = physmap, |
| 108 | .unmap_flash_region = physunmap, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 109 | .chip_readb = internal_chip_readb, |
| 110 | .chip_readw = internal_chip_readw, |
| 111 | .chip_readl = internal_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 112 | .chip_readn = internal_chip_readn, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 113 | .chip_writeb = internal_chip_writeb, |
| 114 | .chip_writew = internal_chip_writew, |
| 115 | .chip_writel = internal_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 116 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 117 | .delay = internal_delay, |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 118 | }, |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 119 | #endif |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 120 | |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 121 | #if DUMMY_SUPPORT == 1 |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 122 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 123 | .name = "dummy", |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 124 | .init = dummy_init, |
| 125 | .shutdown = dummy_shutdown, |
Carl-Daniel Hailfinger | 1455b2b | 2009-05-11 14:13:25 +0000 | [diff] [blame] | 126 | .map_flash_region = dummy_map, |
| 127 | .unmap_flash_region = dummy_unmap, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 128 | .chip_readb = dummy_chip_readb, |
| 129 | .chip_readw = dummy_chip_readw, |
| 130 | .chip_readl = dummy_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 131 | .chip_readn = dummy_chip_readn, |
Carl-Daniel Hailfinger | 1e334e6 | 2009-05-11 15:46:43 +0000 | [diff] [blame] | 132 | .chip_writeb = dummy_chip_writeb, |
| 133 | .chip_writew = dummy_chip_writew, |
| 134 | .chip_writel = dummy_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 135 | .chip_writen = dummy_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 136 | .delay = internal_delay, |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 137 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 138 | #endif |
Carl-Daniel Hailfinger | c312920 | 2009-05-09 00:54:55 +0000 | [diff] [blame] | 139 | |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 140 | #if NIC3COM_SUPPORT == 1 |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 141 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 142 | .name = "nic3com", |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 143 | .init = nic3com_init, |
| 144 | .shutdown = nic3com_shutdown, |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 145 | .map_flash_region = fallback_map, |
| 146 | .unmap_flash_region = fallback_unmap, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 147 | .chip_readb = nic3com_chip_readb, |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 148 | .chip_readw = fallback_chip_readw, |
| 149 | .chip_readl = fallback_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 150 | .chip_readn = fallback_chip_readn, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 151 | .chip_writeb = nic3com_chip_writeb, |
Carl-Daniel Hailfinger | 9ee1077 | 2009-05-16 01:23:55 +0000 | [diff] [blame] | 152 | .chip_writew = fallback_chip_writew, |
| 153 | .chip_writel = fallback_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 154 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 155 | .delay = internal_delay, |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 156 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 157 | #endif |
Uwe Hermann | b4dcb71 | 2009-05-13 11:36:06 +0000 | [diff] [blame] | 158 | |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 159 | #if GFXNVIDIA_SUPPORT == 1 |
| 160 | { |
| 161 | .name = "gfxnvidia", |
| 162 | .init = gfxnvidia_init, |
| 163 | .shutdown = gfxnvidia_shutdown, |
| 164 | .map_flash_region = fallback_map, |
| 165 | .unmap_flash_region = fallback_unmap, |
| 166 | .chip_readb = gfxnvidia_chip_readb, |
| 167 | .chip_readw = fallback_chip_readw, |
| 168 | .chip_readl = fallback_chip_readl, |
| 169 | .chip_readn = fallback_chip_readn, |
| 170 | .chip_writeb = gfxnvidia_chip_writeb, |
| 171 | .chip_writew = fallback_chip_writew, |
| 172 | .chip_writel = fallback_chip_writel, |
| 173 | .chip_writen = fallback_chip_writen, |
| 174 | .delay = internal_delay, |
| 175 | }, |
| 176 | #endif |
| 177 | |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 178 | #if DRKAISER_SUPPORT == 1 |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 179 | { |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 180 | .name = "drkaiser", |
| 181 | .init = drkaiser_init, |
| 182 | .shutdown = drkaiser_shutdown, |
| 183 | .map_flash_region = fallback_map, |
| 184 | .unmap_flash_region = fallback_unmap, |
| 185 | .chip_readb = drkaiser_chip_readb, |
| 186 | .chip_readw = fallback_chip_readw, |
| 187 | .chip_readl = fallback_chip_readl, |
| 188 | .chip_readn = fallback_chip_readn, |
| 189 | .chip_writeb = drkaiser_chip_writeb, |
| 190 | .chip_writew = fallback_chip_writew, |
| 191 | .chip_writel = fallback_chip_writel, |
| 192 | .chip_writen = fallback_chip_writen, |
| 193 | .delay = internal_delay, |
| 194 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 195 | #endif |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 196 | |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 197 | #if SATASII_SUPPORT == 1 |
TURBO J | b0912c0 | 2009-09-02 23:00:46 +0000 | [diff] [blame] | 198 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 199 | .name = "satasii", |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 200 | .init = satasii_init, |
| 201 | .shutdown = satasii_shutdown, |
Uwe Hermann | c691593 | 2009-05-17 23:12:17 +0000 | [diff] [blame] | 202 | .map_flash_region = fallback_map, |
| 203 | .unmap_flash_region = fallback_unmap, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 204 | .chip_readb = satasii_chip_readb, |
| 205 | .chip_readw = fallback_chip_readw, |
| 206 | .chip_readl = fallback_chip_readl, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 207 | .chip_readn = fallback_chip_readn, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 208 | .chip_writeb = satasii_chip_writeb, |
| 209 | .chip_writew = fallback_chip_writew, |
| 210 | .chip_writel = fallback_chip_writel, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 211 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 212 | .delay = internal_delay, |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 213 | }, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 214 | #endif |
Rudolf Marek | 68720c7 | 2009-05-17 19:39:27 +0000 | [diff] [blame] | 215 | |
Uwe Hermann | ddd5c9e | 2010-02-21 21:17:00 +0000 | [diff] [blame] | 216 | #if ATAHPT_SUPPORT == 1 |
| 217 | { |
| 218 | .name = "atahpt", |
| 219 | .init = atahpt_init, |
| 220 | .shutdown = atahpt_shutdown, |
| 221 | .map_flash_region = fallback_map, |
| 222 | .unmap_flash_region = fallback_unmap, |
| 223 | .chip_readb = atahpt_chip_readb, |
| 224 | .chip_readw = fallback_chip_readw, |
| 225 | .chip_readl = fallback_chip_readl, |
| 226 | .chip_readn = fallback_chip_readn, |
| 227 | .chip_writeb = atahpt_chip_writeb, |
| 228 | .chip_writew = fallback_chip_writew, |
| 229 | .chip_writel = fallback_chip_writel, |
| 230 | .chip_writen = fallback_chip_writen, |
| 231 | .delay = internal_delay, |
| 232 | }, |
| 233 | #endif |
| 234 | |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 235 | #if INTERNAL_SUPPORT == 1 |
Carl-Daniel Hailfinger | b8afecd | 2009-05-31 18:00:57 +0000 | [diff] [blame] | 236 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 237 | .name = "it87spi", |
Carl-Daniel Hailfinger | b8afecd | 2009-05-31 18:00:57 +0000 | [diff] [blame] | 238 | .init = it87spi_init, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 239 | .shutdown = noop_shutdown, |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 240 | .map_flash_region = fallback_map, |
| 241 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 242 | .chip_readb = noop_chip_readb, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 243 | .chip_readw = fallback_chip_readw, |
| 244 | .chip_readl = fallback_chip_readl, |
| 245 | .chip_readn = fallback_chip_readn, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 246 | .chip_writeb = noop_chip_writeb, |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 247 | .chip_writew = fallback_chip_writew, |
| 248 | .chip_writel = fallback_chip_writel, |
| 249 | .chip_writen = fallback_chip_writen, |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 250 | .delay = internal_delay, |
Carl-Daniel Hailfinger | b8afecd | 2009-05-31 18:00:57 +0000 | [diff] [blame] | 251 | }, |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 252 | #endif |
Carl-Daniel Hailfinger | b8afecd | 2009-05-31 18:00:57 +0000 | [diff] [blame] | 253 | |
Carl-Daniel Hailfinger | 3426ef6 | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 254 | #if FT2232_SPI_SUPPORT == 1 |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 255 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 256 | .name = "ft2232spi", |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 257 | .init = ft2232_spi_init, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 258 | .shutdown = noop_shutdown, /* Missing shutdown */ |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 259 | .map_flash_region = fallback_map, |
| 260 | .unmap_flash_region = fallback_unmap, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 261 | .chip_readb = noop_chip_readb, |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 262 | .chip_readw = fallback_chip_readw, |
| 263 | .chip_readl = fallback_chip_readl, |
| 264 | .chip_readn = fallback_chip_readn, |
Carl-Daniel Hailfinger | 4740c6f | 2009-09-16 10:09:21 +0000 | [diff] [blame] | 265 | .chip_writeb = noop_chip_writeb, |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 266 | .chip_writew = fallback_chip_writew, |
| 267 | .chip_writel = fallback_chip_writel, |
| 268 | .chip_writen = fallback_chip_writen, |
| 269 | .delay = internal_delay, |
| 270 | }, |
Carl-Daniel Hailfinger | 3426ef6 | 2009-08-19 13:27:58 +0000 | [diff] [blame] | 271 | #endif |
Carl-Daniel Hailfinger | 415e513 | 2009-08-12 11:39:29 +0000 | [diff] [blame] | 272 | |
Carl-Daniel Hailfinger | 6be7411 | 2009-08-12 16:17:41 +0000 | [diff] [blame] | 273 | #if SERPROG_SUPPORT == 1 |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 274 | { |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 275 | .name = "serprog", |
Urja Rannikko | 2291535 | 2009-06-23 11:33:43 +0000 | [diff] [blame] | 276 | .init = serprog_init, |
| 277 | .shutdown = serprog_shutdown, |
| 278 | .map_flash_region = fallback_map, |
| 279 | .unmap_flash_region = fallback_unmap, |
| 280 | .chip_readb = serprog_chip_readb, |
| 281 | .chip_readw = fallback_chip_readw, |
| 282 | .chip_readl = fallback_chip_readl, |
| 283 | .chip_readn = serprog_chip_readn, |
| 284 | .chip_writeb = serprog_chip_writeb, |
| 285 | .chip_writew = fallback_chip_writew, |
| 286 | .chip_writel = fallback_chip_writel, |
| 287 | .chip_writen = fallback_chip_writen, |
| 288 | .delay = serprog_delay, |
| 289 | }, |
Carl-Daniel Hailfinger | 6be7411 | 2009-08-12 16:17:41 +0000 | [diff] [blame] | 290 | #endif |
Paul Fox | 05dfbe6 | 2009-06-16 21:08:06 +0000 | [diff] [blame] | 291 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 292 | #if BUSPIRATE_SPI_SUPPORT == 1 |
| 293 | { |
| 294 | .name = "buspiratespi", |
| 295 | .init = buspirate_spi_init, |
| 296 | .shutdown = buspirate_spi_shutdown, |
| 297 | .map_flash_region = fallback_map, |
| 298 | .unmap_flash_region = fallback_unmap, |
| 299 | .chip_readb = noop_chip_readb, |
| 300 | .chip_readw = fallback_chip_readw, |
| 301 | .chip_readl = fallback_chip_readl, |
| 302 | .chip_readn = fallback_chip_readn, |
| 303 | .chip_writeb = noop_chip_writeb, |
| 304 | .chip_writew = fallback_chip_writew, |
| 305 | .chip_writel = fallback_chip_writel, |
| 306 | .chip_writen = fallback_chip_writen, |
| 307 | .delay = internal_delay, |
| 308 | }, |
| 309 | #endif |
| 310 | |
Carl-Daniel Hailfinger | d38fac8 | 2010-01-19 11:15:48 +0000 | [diff] [blame] | 311 | #if DEDIPROG_SUPPORT == 1 |
| 312 | { |
| 313 | .name = "dediprog", |
| 314 | .init = dediprog_init, |
| 315 | .shutdown = dediprog_shutdown, |
| 316 | .map_flash_region = fallback_map, |
| 317 | .unmap_flash_region = fallback_unmap, |
| 318 | .chip_readb = noop_chip_readb, |
| 319 | .chip_readw = fallback_chip_readw, |
| 320 | .chip_readl = fallback_chip_readl, |
| 321 | .chip_readn = fallback_chip_readn, |
| 322 | .chip_writeb = noop_chip_writeb, |
| 323 | .chip_writew = fallback_chip_writew, |
| 324 | .chip_writel = fallback_chip_writel, |
| 325 | .chip_writen = fallback_chip_writen, |
| 326 | .delay = internal_delay, |
| 327 | }, |
| 328 | #endif |
| 329 | |
Carl-Daniel Hailfinger | 37fc469 | 2009-08-12 14:34:35 +0000 | [diff] [blame] | 330 | {}, /* This entry corresponds to PROGRAMMER_INVALID. */ |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 331 | }; |
Luc Verhaegen | 8e3a600 | 2007-04-04 22:45:58 +0000 | [diff] [blame] | 332 | |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 333 | #define SHUTDOWN_MAXFN 4 |
| 334 | static int shutdown_fn_count = 0; |
| 335 | struct shutdown_func_data { |
| 336 | void (*func) (void *data); |
| 337 | void *data; |
| 338 | } shutdown_fn[SHUTDOWN_MAXFN]; |
| 339 | |
| 340 | /* Register a function to be executed on programmer shutdown. |
| 341 | * The advantage over atexit() is that you can supply a void pointer which will |
| 342 | * be used as parameter to the registered function upon programmer shutdown. |
| 343 | * This pointer can point to arbitrary data used by said function, e.g. undo |
| 344 | * information for GPIO settings etc. If unneeded, set data=NULL. |
| 345 | * Please note that the first (void *data) belongs to the function signature of |
| 346 | * the function passed as first parameter. |
| 347 | */ |
| 348 | int register_shutdown(void (*function) (void *data), void *data) |
| 349 | { |
| 350 | if (shutdown_fn_count >= SHUTDOWN_MAXFN) { |
| 351 | msg_perr("Tried to register more than %n shutdown functions.\n", |
| 352 | SHUTDOWN_MAXFN); |
| 353 | return 1; |
| 354 | } |
| 355 | shutdown_fn[shutdown_fn_count].func = function; |
| 356 | shutdown_fn[shutdown_fn_count].data = data; |
| 357 | shutdown_fn_count++; |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 362 | int programmer_init(void) |
| 363 | { |
| 364 | return programmer_table[programmer].init(); |
| 365 | } |
| 366 | |
| 367 | int programmer_shutdown(void) |
| 368 | { |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 369 | int i; |
| 370 | |
| 371 | for (i = shutdown_fn_count - 1; i >= 0; i--) |
| 372 | shutdown_fn[i].func(shutdown_fn[i].data); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 373 | return programmer_table[programmer].shutdown(); |
| 374 | } |
| 375 | |
| 376 | void *programmer_map_flash_region(const char *descr, unsigned long phys_addr, |
| 377 | size_t len) |
| 378 | { |
| 379 | return programmer_table[programmer].map_flash_region(descr, |
| 380 | phys_addr, len); |
| 381 | } |
| 382 | |
| 383 | void programmer_unmap_flash_region(void *virt_addr, size_t len) |
| 384 | { |
| 385 | programmer_table[programmer].unmap_flash_region(virt_addr, len); |
| 386 | } |
| 387 | |
| 388 | void chip_writeb(uint8_t val, chipaddr addr) |
| 389 | { |
| 390 | programmer_table[programmer].chip_writeb(val, addr); |
| 391 | } |
| 392 | |
| 393 | void chip_writew(uint16_t val, chipaddr addr) |
| 394 | { |
| 395 | programmer_table[programmer].chip_writew(val, addr); |
| 396 | } |
| 397 | |
| 398 | void chip_writel(uint32_t val, chipaddr addr) |
| 399 | { |
| 400 | programmer_table[programmer].chip_writel(val, addr); |
| 401 | } |
| 402 | |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 403 | void chip_writen(uint8_t *buf, chipaddr addr, size_t len) |
| 404 | { |
| 405 | programmer_table[programmer].chip_writen(buf, addr, len); |
| 406 | } |
| 407 | |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 408 | uint8_t chip_readb(const chipaddr addr) |
| 409 | { |
| 410 | return programmer_table[programmer].chip_readb(addr); |
| 411 | } |
| 412 | |
| 413 | uint16_t chip_readw(const chipaddr addr) |
| 414 | { |
| 415 | return programmer_table[programmer].chip_readw(addr); |
| 416 | } |
| 417 | |
| 418 | uint32_t chip_readl(const chipaddr addr) |
| 419 | { |
| 420 | return programmer_table[programmer].chip_readl(addr); |
| 421 | } |
| 422 | |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 423 | void chip_readn(uint8_t *buf, chipaddr addr, size_t len) |
| 424 | { |
| 425 | programmer_table[programmer].chip_readn(buf, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 428 | void programmer_delay(int usecs) |
| 429 | { |
| 430 | programmer_table[programmer].delay(usecs); |
| 431 | } |
| 432 | |
Peter Stuge | 776d202 | 2009-01-26 00:39:57 +0000 | [diff] [blame] | 433 | void map_flash_registers(struct flashchip *flash) |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 434 | { |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 435 | size_t size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | d0fc946 | 2009-05-11 14:01:17 +0000 | [diff] [blame] | 436 | /* Flash registers live 4 MByte below the flash. */ |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 437 | /* FIXME: This is incorrect for nonstandard flashbase. */ |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 438 | flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size); |
Stefan Reinauer | ff4f197 | 2007-05-24 08:48:10 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 441 | int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len) |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 442 | { |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 443 | chip_readn(buf, flash->virtual_memory + start, len); |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 448 | unsigned long flashbase = 0; |
| 449 | |
Carl-Daniel Hailfinger | 38a059d | 2009-06-13 12:04:03 +0000 | [diff] [blame] | 450 | int min(int a, int b) |
| 451 | { |
| 452 | return (a < b) ? a : b; |
| 453 | } |
| 454 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 455 | int max(int a, int b) |
| 456 | { |
| 457 | return (a > b) ? a : b; |
| 458 | } |
| 459 | |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 460 | int bitcount(unsigned long a) |
| 461 | { |
| 462 | int i = 0; |
| 463 | for (; a != 0; a >>= 1) |
| 464 | if (a & 1) |
| 465 | i++; |
| 466 | return i; |
| 467 | } |
| 468 | |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 469 | char *strcat_realloc(char *dest, const char *src) |
| 470 | { |
| 471 | dest = realloc(dest, strlen(dest) + strlen(src) + 1); |
| 472 | if (!dest) |
| 473 | return NULL; |
| 474 | strcat(dest, src); |
| 475 | return dest; |
| 476 | } |
| 477 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 478 | /* This is a somewhat hacked function similar in some ways to strtok(). |
| 479 | * It will look for needle in haystack, return a copy of needle and remove |
| 480 | * everything from the first occurrence of needle to the next delimiter |
| 481 | * from haystack. |
| 482 | */ |
| 483 | char *extract_param(char **haystack, char *needle, char *delim) |
| 484 | { |
| 485 | char *param_pos, *rest, *tmp; |
| 486 | char *dev = NULL; |
| 487 | int devlen; |
| 488 | |
| 489 | param_pos = strstr(*haystack, needle); |
| 490 | do { |
| 491 | if (!param_pos) |
| 492 | return NULL; |
| 493 | /* Beginning of the string? */ |
| 494 | if (param_pos == *haystack) |
| 495 | break; |
| 496 | /* After a delimiter? */ |
| 497 | if (strchr(delim, *(param_pos - 1))) |
| 498 | break; |
| 499 | /* Continue searching. */ |
| 500 | param_pos++; |
| 501 | param_pos = strstr(param_pos, needle); |
| 502 | } while (1); |
| 503 | |
| 504 | if (param_pos) { |
| 505 | param_pos += strlen(needle); |
| 506 | devlen = strcspn(param_pos, delim); |
| 507 | if (devlen) { |
| 508 | dev = malloc(devlen + 1); |
| 509 | if (!dev) { |
| 510 | fprintf(stderr, "Out of memory!\n"); |
| 511 | exit(1); |
| 512 | } |
| 513 | strncpy(dev, param_pos, devlen); |
| 514 | dev[devlen] = '\0'; |
| 515 | } |
| 516 | rest = param_pos + devlen; |
| 517 | rest += strspn(rest, delim); |
| 518 | param_pos -= strlen(needle); |
| 519 | memmove(param_pos, rest, strlen(rest) + 1); |
| 520 | tmp = realloc(*haystack, strlen(*haystack) + 1); |
| 521 | if (!tmp) { |
| 522 | fprintf(stderr, "Out of memory!\n"); |
| 523 | exit(1); |
| 524 | } |
| 525 | *haystack = tmp; |
| 526 | } |
| 527 | |
| 528 | |
| 529 | return dev; |
| 530 | } |
| 531 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 532 | /* start is an offset to the base address of the flash chip */ |
| 533 | int check_erased_range(struct flashchip *flash, int start, int len) |
| 534 | { |
| 535 | int ret; |
| 536 | uint8_t *cmpbuf = malloc(len); |
| 537 | |
| 538 | if (!cmpbuf) { |
| 539 | fprintf(stderr, "Could not allocate memory!\n"); |
| 540 | exit(1); |
| 541 | } |
| 542 | memset(cmpbuf, 0xff, len); |
| 543 | ret = verify_range(flash, cmpbuf, start, len, "ERASE"); |
| 544 | free(cmpbuf); |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | /** |
Carl-Daniel Hailfinger | d0250a3 | 2009-11-25 17:05:52 +0000 | [diff] [blame] | 549 | * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the |
| 550 | flash content at location start |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 551 | * @start offset to the base address of the flash chip |
| 552 | * @len length of the verified area |
| 553 | * @message string to print in the "FAILED" message |
| 554 | * @return 0 for success, -1 for failure |
| 555 | */ |
| 556 | int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message) |
| 557 | { |
| 558 | int i, j, starthere, lenhere, ret = 0; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 559 | int page_size = flash->page_size; |
| 560 | uint8_t *readbuf = malloc(page_size); |
Carl-Daniel Hailfinger | 49b9cab | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 561 | int failcount = 0; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 562 | |
| 563 | if (!len) |
| 564 | goto out_free; |
| 565 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 566 | if (!flash->read) { |
| 567 | fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n"); |
| 568 | return 1; |
| 569 | } |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 570 | if (!readbuf) { |
| 571 | fprintf(stderr, "Could not allocate memory!\n"); |
| 572 | exit(1); |
| 573 | } |
| 574 | |
| 575 | if (start + len > flash->total_size * 1024) { |
| 576 | fprintf(stderr, "Error: %s called with start 0x%x + len 0x%x >" |
| 577 | " total_size 0x%x\n", __func__, start, len, |
| 578 | flash->total_size * 1024); |
| 579 | ret = -1; |
| 580 | goto out_free; |
| 581 | } |
| 582 | if (!message) |
| 583 | message = "VERIFY"; |
| 584 | |
| 585 | /* Warning: This loop has a very unusual condition and body. |
| 586 | * The loop needs to go through each page with at least one affected |
| 587 | * byte. The lowest page number is (start / page_size) since that |
| 588 | * division rounds down. The highest page number we want is the page |
| 589 | * where the last byte of the range lives. That last byte has the |
| 590 | * address (start + len - 1), thus the highest page number is |
| 591 | * (start + len - 1) / page_size. Since we want to include that last |
| 592 | * page as well, the loop condition uses <=. |
| 593 | */ |
| 594 | for (i = start / page_size; i <= (start + len - 1) / page_size; i++) { |
| 595 | /* Byte position of the first byte in the range in this page. */ |
| 596 | starthere = max(start, i * page_size); |
| 597 | /* Length of bytes in the range in this page. */ |
| 598 | lenhere = min(start + len, (i + 1) * page_size) - starthere; |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 599 | flash->read(flash, readbuf, starthere, lenhere); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 600 | for (j = 0; j < lenhere; j++) { |
| 601 | if (cmpbuf[starthere - start + j] != readbuf[j]) { |
Carl-Daniel Hailfinger | 49b9cab | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 602 | /* Only print the first failure. */ |
| 603 | if (!failcount++) |
| 604 | fprintf(stderr, "%s FAILED at 0x%08x! " |
| 605 | "Expected=0x%02x, Read=0x%02x,", |
| 606 | message, starthere + j, |
| 607 | cmpbuf[starthere - start + j], |
| 608 | readbuf[j]); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | } |
Carl-Daniel Hailfinger | 49b9cab | 2009-07-23 01:42:56 +0000 | [diff] [blame] | 612 | if (failcount) { |
| 613 | fprintf(stderr, " failed byte count from 0x%08x-0x%08x: 0x%x\n", |
| 614 | start, start + len - 1, failcount); |
| 615 | ret = -1; |
| 616 | } |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 617 | |
| 618 | out_free: |
| 619 | free(readbuf); |
| 620 | return ret; |
| 621 | } |
| 622 | |
Carl-Daniel Hailfinger | eaac68b | 2009-11-23 12:55:31 +0000 | [diff] [blame] | 623 | /* This function generates various test patterns useful for testing controller |
| 624 | * and chip communication as well as chip behaviour. |
| 625 | * |
| 626 | * If a byte can be written multiple times, each time keeping 0-bits at 0 |
| 627 | * and changing 1-bits to 0 if the new value for that bit is 0, the effect |
| 628 | * is essentially an AND operation. That's also the reason why this function |
| 629 | * provides the result of AND between various patterns. |
| 630 | * |
| 631 | * Below is a list of patterns (and their block length). |
| 632 | * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes) |
| 633 | * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes) |
| 634 | * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes) |
| 635 | * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes) |
| 636 | * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes) |
| 637 | * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes) |
| 638 | * Pattern 6 is 00 (1 Byte) |
| 639 | * Pattern 7 is ff (1 Byte) |
| 640 | * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256 |
| 641 | * byte block. |
| 642 | * |
| 643 | * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B) |
| 644 | * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B) |
| 645 | * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter) |
| 646 | * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards) |
| 647 | * Pattern 12 is 00 (1 Byte) |
| 648 | * Pattern 13 is ff (1 Byte) |
| 649 | * Patterns 8-13 have no block number. |
| 650 | * |
| 651 | * Patterns 0-3 are created to detect and efficiently diagnose communication |
| 652 | * slips like missed bits or bytes and their repetitive nature gives good visual |
| 653 | * cues to the person inspecting the results. In addition, the following holds: |
| 654 | * AND Pattern 0/1 == Pattern 4 |
| 655 | * AND Pattern 2/3 == Pattern 5 |
| 656 | * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6 |
| 657 | * A weakness of pattern 0-5 is the inability to detect swaps/copies between |
| 658 | * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc. |
| 659 | * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes. |
| 660 | * 0x5 and 0xa were picked because they are 0101 and 1010 binary. |
| 661 | * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes. |
| 662 | * Besides that, they provide for bit testing of the last two bytes of every |
| 663 | * 256 byte block which contains the block number for patterns 0-6. |
| 664 | * Patterns 10-11 are special purpose for detecting subblock aliasing with |
| 665 | * block sizes >256 bytes (some Dataflash chips etc.) |
| 666 | * AND Pattern 8/9 == Pattern 12 |
| 667 | * AND Pattern 10/11 == Pattern 12 |
| 668 | * Pattern 13 is the completely erased state. |
| 669 | * None of the patterns can detect aliasing at boundaries which are a multiple |
| 670 | * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI). |
| 671 | */ |
| 672 | int generate_testpattern(uint8_t *buf, uint32_t size, int variant) |
| 673 | { |
| 674 | int i; |
| 675 | |
| 676 | if (!buf) { |
| 677 | fprintf(stderr, "Invalid buffer!\n"); |
| 678 | return 1; |
| 679 | } |
| 680 | |
| 681 | switch (variant) { |
| 682 | case 0: |
| 683 | for (i = 0; i < size; i++) |
| 684 | buf[i] = (i & 0xf) << 4 | 0x5; |
| 685 | break; |
| 686 | case 1: |
| 687 | for (i = 0; i < size; i++) |
| 688 | buf[i] = (i & 0xf) << 4 | 0xa; |
| 689 | break; |
| 690 | case 2: |
| 691 | for (i = 0; i < size; i++) |
| 692 | buf[i] = 0x50 | (i & 0xf); |
| 693 | break; |
| 694 | case 3: |
| 695 | for (i = 0; i < size; i++) |
| 696 | buf[i] = 0xa0 | (i & 0xf); |
| 697 | break; |
| 698 | case 4: |
| 699 | for (i = 0; i < size; i++) |
| 700 | buf[i] = (i & 0xf) << 4; |
| 701 | break; |
| 702 | case 5: |
| 703 | for (i = 0; i < size; i++) |
| 704 | buf[i] = i & 0xf; |
| 705 | break; |
| 706 | case 6: |
| 707 | memset(buf, 0x00, size); |
| 708 | break; |
| 709 | case 7: |
| 710 | memset(buf, 0xff, size); |
| 711 | break; |
| 712 | case 8: |
| 713 | for (i = 0; i < size; i++) |
| 714 | buf[i] = i & 0xff; |
| 715 | break; |
| 716 | case 9: |
| 717 | for (i = 0; i < size; i++) |
| 718 | buf[i] = ~(i & 0xff); |
| 719 | break; |
| 720 | case 10: |
| 721 | for (i = 0; i < size % 2; i++) { |
| 722 | buf[i * 2] = (i >> 8) & 0xff; |
| 723 | buf[i * 2 + 1] = i & 0xff; |
| 724 | } |
| 725 | if (size & 0x1) |
| 726 | buf[i * 2] = (i >> 8) & 0xff; |
| 727 | break; |
| 728 | case 11: |
| 729 | for (i = 0; i < size % 2; i++) { |
| 730 | buf[i * 2] = ~((i >> 8) & 0xff); |
| 731 | buf[i * 2 + 1] = ~(i & 0xff); |
| 732 | } |
| 733 | if (size & 0x1) |
| 734 | buf[i * 2] = ~((i >> 8) & 0xff); |
| 735 | break; |
| 736 | case 12: |
| 737 | memset(buf, 0x00, size); |
| 738 | break; |
| 739 | case 13: |
| 740 | memset(buf, 0xff, size); |
| 741 | break; |
| 742 | } |
| 743 | |
| 744 | if ((variant >= 0) && (variant <= 7)) { |
| 745 | /* Write block number in the last two bytes of each 256-byte |
| 746 | * block, big endian for easier reading of the hexdump. |
| 747 | * Note that this wraps around for chips larger than 2^24 bytes |
| 748 | * (16 MB). |
| 749 | */ |
| 750 | for (i = 0; i < size / 256; i++) { |
| 751 | buf[i * 256 + 254] = (i >> 8) & 0xff; |
| 752 | buf[i * 256 + 255] = i & 0xff; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 759 | int check_max_decode(enum chipbustype buses, uint32_t size) |
| 760 | { |
| 761 | int limitexceeded = 0; |
| 762 | if ((buses & CHIP_BUSTYPE_PARALLEL) && |
| 763 | (max_rom_decode.parallel < size)) { |
| 764 | limitexceeded++; |
| 765 | printf_debug("Chip size %u kB is bigger than supported " |
| 766 | "size %u kB of chipset/board/programmer " |
| 767 | "for %s interface, " |
| 768 | "probe/read/erase/write may fail. ", size / 1024, |
| 769 | max_rom_decode.parallel / 1024, "Parallel"); |
| 770 | } |
| 771 | if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) { |
| 772 | limitexceeded++; |
| 773 | printf_debug("Chip size %u kB is bigger than supported " |
| 774 | "size %u kB of chipset/board/programmer " |
| 775 | "for %s interface, " |
| 776 | "probe/read/erase/write may fail. ", size / 1024, |
| 777 | max_rom_decode.lpc / 1024, "LPC"); |
| 778 | } |
| 779 | if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) { |
| 780 | limitexceeded++; |
| 781 | printf_debug("Chip size %u kB is bigger than supported " |
| 782 | "size %u kB of chipset/board/programmer " |
| 783 | "for %s interface, " |
| 784 | "probe/read/erase/write may fail. ", size / 1024, |
| 785 | max_rom_decode.fwh / 1024, "FWH"); |
| 786 | } |
| 787 | if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) { |
| 788 | limitexceeded++; |
| 789 | printf_debug("Chip size %u kB is bigger than supported " |
| 790 | "size %u kB of chipset/board/programmer " |
| 791 | "for %s interface, " |
| 792 | "probe/read/erase/write may fail. ", size / 1024, |
| 793 | max_rom_decode.spi / 1024, "SPI"); |
| 794 | } |
| 795 | if (!limitexceeded) |
| 796 | return 0; |
| 797 | /* Sometimes chip and programmer have more than one bus in common, |
| 798 | * and the limit is not exceeded on all buses. Tell the user. |
| 799 | */ |
| 800 | if (bitcount(buses) > limitexceeded) |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 801 | /* FIXME: This message is designed towards CLI users. */ |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 802 | printf_debug("There is at least one common chip/programmer " |
| 803 | "interface which can support a chip of this size. " |
| 804 | "You can try --force at your own risk.\n"); |
| 805 | return 1; |
| 806 | } |
| 807 | |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 808 | struct flashchip *probe_flash(struct flashchip *first_flash, int force) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 809 | { |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 810 | struct flashchip *flash; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 811 | unsigned long base = 0; |
| 812 | uint32_t size; |
| 813 | enum chipbustype buses_common; |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 814 | char *tmp; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 815 | |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 816 | for (flash = first_flash; flash && flash->name; flash++) { |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 817 | if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 818 | continue; |
Stefan Reinauer | ac37897 | 2008-03-17 22:59:40 +0000 | [diff] [blame] | 819 | printf_debug("Probing for %s %s, %d KB: ", |
| 820 | flash->vendor, flash->name, flash->total_size); |
Peter Stuge | 7ffbc6f | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 821 | if (!flash->probe && !force) { |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 822 | printf_debug("failed! flashrom has no probe function for this flash chip.\n"); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 823 | continue; |
| 824 | } |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 825 | buses_common = buses_supported & flash->bustype; |
| 826 | if (!buses_common) { |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 827 | tmp = flashbuses_to_text(buses_supported); |
| 828 | printf_debug("skipped. Host bus type %s ", tmp); |
| 829 | free(tmp); |
| 830 | tmp = flashbuses_to_text(flash->bustype); |
| 831 | printf_debug("and chip bus type %s are incompatible.\n", tmp); |
| 832 | free(tmp); |
| 833 | continue; |
| 834 | } |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 835 | |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 836 | size = flash->total_size * 1024; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 837 | check_max_decode(buses_common, size); |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 838 | |
Carl-Daniel Hailfinger | 97d6b09 | 2009-05-09 07:27:23 +0000 | [diff] [blame] | 839 | base = flashbase ? flashbase : (0xffffffff - size + 1); |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 840 | flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 841 | |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 842 | if (force) |
| 843 | break; |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 844 | |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 845 | if (flash->probe(flash) != 1) |
| 846 | goto notfound; |
| 847 | |
Uwe Hermann | 394131e | 2008-10-18 21:14:13 +0000 | [diff] [blame] | 848 | if (first_flash == flashchips |
| 849 | || flash->model_id != GENERIC_DEVICE_ID) |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 850 | break; |
| 851 | |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 852 | notfound: |
Carl-Daniel Hailfinger | 5820f42 | 2009-05-16 21:22:56 +0000 | [diff] [blame] | 853 | programmer_unmap_flash_region((void *)flash->virtual_memory, size); |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 854 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 855 | |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 856 | if (!flash || !flash->name) |
| 857 | return NULL; |
| 858 | |
Uwe Hermann | 9899cad | 2009-06-28 21:47:57 +0000 | [diff] [blame] | 859 | printf("Found chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n", |
| 860 | flash->vendor, flash->name, flash->total_size, |
| 861 | flashbuses_to_text(flash->bustype), base); |
| 862 | |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 863 | if (flash->printlock) |
| 864 | flash->printlock(flash); |
| 865 | |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 866 | return flash; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Stefan Reinauer | e370528 | 2005-12-18 16:41:10 +0000 | [diff] [blame] | 869 | int verify_flash(struct flashchip *flash, uint8_t *buf) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 870 | { |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 871 | int ret; |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 872 | int total_size = flash->total_size * 1024; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 873 | |
Uwe Hermann | a502dce | 2007-10-17 23:55:15 +0000 | [diff] [blame] | 874 | printf("Verifying flash... "); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 875 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 876 | ret = verify_range(flash, buf, 0, total_size, NULL); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 877 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 878 | if (!ret) |
| 879 | printf("VERIFIED. \n"); |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 880 | |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 881 | return ret; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Carl-Daniel Hailfinger | 49eb4dd | 2009-06-19 11:23:57 +0000 | [diff] [blame] | 884 | int read_flash(struct flashchip *flash, char *filename) |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 885 | { |
| 886 | unsigned long numbytes; |
| 887 | FILE *image; |
| 888 | unsigned long size = flash->total_size * 1024; |
| 889 | unsigned char *buf = calloc(size, sizeof(char)); |
Stephan Guilloux | 21dd55b | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 890 | |
| 891 | if (!filename) { |
| 892 | printf("Error: No filename specified.\n"); |
| 893 | return 1; |
| 894 | } |
Patrick Georgi | 0bf842d | 2010-01-25 22:55:33 +0000 | [diff] [blame] | 895 | if ((image = fopen(filename, "wb")) == NULL) { |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 896 | perror(filename); |
| 897 | exit(1); |
| 898 | } |
| 899 | printf("Reading flash... "); |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 900 | if (!flash->read) { |
| 901 | printf("FAILED!\n"); |
| 902 | fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n"); |
| 903 | return 1; |
| 904 | } else |
Carl-Daniel Hailfinger | cbf563c | 2009-06-16 08:55:44 +0000 | [diff] [blame] | 905 | flash->read(flash, buf, 0, size); |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 906 | |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 907 | numbytes = fwrite(buf, 1, size, image); |
| 908 | fclose(image); |
Stephan Guilloux | 5a8b244 | 2009-06-01 21:37:00 +0000 | [diff] [blame] | 909 | free(buf); |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 910 | printf("%s.\n", numbytes == size ? "done" : "FAILED"); |
| 911 | if (numbytes != size) |
| 912 | return 1; |
| 913 | return 0; |
| 914 | } |
| 915 | |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 916 | /* This function shares a lot of its structure with erase_flash(). |
| 917 | * Even if an error is found, the function will keep going and check the rest. |
| 918 | */ |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 919 | int selfcheck_eraseblocks(struct flashchip *flash) |
| 920 | { |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 921 | int i, j, k; |
| 922 | int ret = 0; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 923 | |
| 924 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 925 | unsigned int done = 0; |
| 926 | struct block_eraser eraser = flash->block_erasers[k]; |
| 927 | |
| 928 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 929 | /* Blocks with zero size are bugs in flashchips.c. */ |
| 930 | if (eraser.eraseblocks[i].count && |
| 931 | !eraser.eraseblocks[i].size) { |
| 932 | msg_gerr("ERROR: Flash chip %s erase function " |
| 933 | "%i region %i has size 0. Please report" |
| 934 | " a bug at flashrom@flashrom.org\n", |
| 935 | flash->name, k, i); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 936 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 937 | } |
| 938 | /* Blocks with zero count are bugs in flashchips.c. */ |
| 939 | if (!eraser.eraseblocks[i].count && |
| 940 | eraser.eraseblocks[i].size) { |
| 941 | msg_gerr("ERROR: Flash chip %s erase function " |
| 942 | "%i region %i has count 0. Please report" |
| 943 | " a bug at flashrom@flashrom.org\n", |
| 944 | flash->name, k, i); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 945 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 946 | } |
| 947 | done += eraser.eraseblocks[i].count * |
| 948 | eraser.eraseblocks[i].size; |
| 949 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 950 | /* Empty eraseblock definition with erase function. */ |
| 951 | if (!done && eraser.block_erase) |
| 952 | msg_pspew("Strange: Empty eraseblock definition with " |
| 953 | "non-empty erase function. Not an error.\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 954 | if (!done) |
| 955 | continue; |
| 956 | if (done != flash->total_size * 1024) { |
| 957 | msg_gerr("ERROR: Flash chip %s erase function %i " |
| 958 | "region walking resulted in 0x%06x bytes total," |
| 959 | " expected 0x%06x bytes. Please report a bug at" |
| 960 | " flashrom@flashrom.org\n", flash->name, k, |
| 961 | done, flash->total_size * 1024); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 962 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 963 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 964 | if (!eraser.block_erase) |
| 965 | continue; |
| 966 | /* Check if there are identical erase functions for different |
| 967 | * layouts. That would imply "magic" erase functions. The |
| 968 | * easiest way to check this is with function pointers. |
| 969 | */ |
| 970 | for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) |
| 971 | if (eraser.block_erase == |
| 972 | flash->block_erasers[j].block_erase) { |
| 973 | msg_gerr("ERROR: Flash chip %s erase function " |
| 974 | "%i and %i are identical. Please report" |
| 975 | " a bug at flashrom@flashrom.org\n", |
| 976 | flash->name, k, j); |
| 977 | ret = 1; |
| 978 | } |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 979 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 980 | return ret; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 983 | int erase_flash(struct flashchip *flash) |
| 984 | { |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 985 | int i, j, k, ret = 0, found = 0; |
Carl-Daniel Hailfinger | 9d48916 | 2009-12-14 04:04:18 +0000 | [diff] [blame] | 986 | unsigned int start, len; |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 987 | |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 988 | printf("Erasing flash chip... "); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 989 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
Carl-Daniel Hailfinger | 9d48916 | 2009-12-14 04:04:18 +0000 | [diff] [blame] | 990 | unsigned int done = 0; |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 991 | struct block_eraser eraser = flash->block_erasers[k]; |
| 992 | |
| 993 | printf_debug("Looking at blockwise erase function %i... ", k); |
| 994 | if (!eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 995 | printf_debug("not defined. " |
| 996 | "Looking for another erase function.\n"); |
| 997 | continue; |
| 998 | } |
| 999 | if (!eraser.block_erase && eraser.eraseblocks[0].count) { |
| 1000 | printf_debug("eraseblock layout is known, but no " |
| 1001 | "matching block erase function found. " |
| 1002 | "Looking for another erase function.\n"); |
| 1003 | continue; |
| 1004 | } |
| 1005 | if (eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 1006 | printf_debug("block erase function found, but " |
| 1007 | "eraseblock layout is unknown. " |
| 1008 | "Looking for another erase function.\n"); |
| 1009 | continue; |
| 1010 | } |
| 1011 | found = 1; |
| 1012 | printf_debug("trying... "); |
| 1013 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 1014 | /* count==0 for all automatically initialized array |
| 1015 | * members so the loop below won't be executed for them. |
| 1016 | */ |
| 1017 | for (j = 0; j < eraser.eraseblocks[i].count; j++) { |
Carl-Daniel Hailfinger | 9d48916 | 2009-12-14 04:04:18 +0000 | [diff] [blame] | 1018 | start = done + eraser.eraseblocks[i].size * j; |
| 1019 | len = eraser.eraseblocks[i].size; |
| 1020 | printf_debug("0x%06x-0x%06x, ", start, |
| 1021 | start + len - 1); |
| 1022 | ret = eraser.block_erase(flash, start, len); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1023 | if (ret) |
| 1024 | break; |
| 1025 | } |
| 1026 | if (ret) |
| 1027 | break; |
Carl-Daniel Hailfinger | 9d48916 | 2009-12-14 04:04:18 +0000 | [diff] [blame] | 1028 | done += eraser.eraseblocks[i].count * |
| 1029 | eraser.eraseblocks[i].size; |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1030 | } |
Carl-Daniel Hailfinger | 9d48916 | 2009-12-14 04:04:18 +0000 | [diff] [blame] | 1031 | printf_debug("\n"); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1032 | /* If everything is OK, don't try another erase function. */ |
| 1033 | if (!ret) |
| 1034 | break; |
| 1035 | } |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1036 | if (!found) { |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1037 | fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n"); |
| 1038 | return 1; |
| 1039 | } |
Carl-Daniel Hailfinger | f160a12 | 2009-05-08 17:15:15 +0000 | [diff] [blame] | 1040 | |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 1041 | if (ret) { |
| 1042 | fprintf(stderr, "FAILED!\n"); |
| 1043 | } else { |
| 1044 | printf("SUCCESS.\n"); |
| 1045 | } |
| 1046 | return ret; |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 1049 | void emergency_help_message(void) |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1050 | { |
| 1051 | fprintf(stderr, "Your flash chip is in an unknown state.\n" |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 1052 | "Get help on IRC at irc.freenode.net (channel #flashrom) or\n" |
| 1053 | "mail flashrom@flashrom.org!\n--------------------" |
| 1054 | "-----------------------------------------------------------\n" |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1055 | "DO NOT REBOOT OR POWEROFF!\n"); |
| 1056 | } |
| 1057 | |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1058 | /* The way to go if you want a delimited list of programmers*/ |
| 1059 | void list_programmers(char *delim) |
| 1060 | { |
| 1061 | enum programmer p; |
| 1062 | for (p = 0; p < PROGRAMMER_INVALID; p++) { |
| 1063 | printf("%s", programmer_table[p].name); |
| 1064 | if (p < PROGRAMMER_INVALID - 1) |
| 1065 | printf("%s", delim); |
| 1066 | } |
| 1067 | printf("\n"); |
| 1068 | } |
| 1069 | |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 1070 | void print_version(void) |
| 1071 | { |
Carl-Daniel Hailfinger | a80cfbc | 2009-07-22 20:13:00 +0000 | [diff] [blame] | 1072 | printf("flashrom v%s\n", flashrom_version); |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1075 | int selfcheck(void) |
| 1076 | { |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1077 | int ret = 0; |
| 1078 | struct flashchip *flash; |
| 1079 | |
| 1080 | /* Safety check. Instead of aborting after the first error, check |
| 1081 | * if more errors exist. |
| 1082 | */ |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1083 | if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) { |
| 1084 | fprintf(stderr, "Programmer table miscompilation!\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1085 | ret = 1; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1086 | } |
| 1087 | if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) { |
| 1088 | fprintf(stderr, "SPI programmer table miscompilation!\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1089 | ret = 1; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1090 | } |
| 1091 | #if BITBANG_SPI_SUPPORT == 1 |
| 1092 | if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) { |
| 1093 | fprintf(stderr, "Bitbanging SPI master table miscompilation!\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1094 | ret = 1; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1095 | } |
| 1096 | #endif |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1097 | for (flash = flashchips; flash && flash->name; flash++) |
| 1098 | if (selfcheck_eraseblocks(flash)) |
| 1099 | ret = 1; |
| 1100 | return ret; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | void check_chip_supported(struct flashchip *flash) |
| 1104 | { |
| 1105 | if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) { |
| 1106 | printf("===\n"); |
| 1107 | if (flash->tested & TEST_BAD_MASK) { |
| 1108 | printf("This flash part has status NOT WORKING for operations:"); |
| 1109 | if (flash->tested & TEST_BAD_PROBE) |
| 1110 | printf(" PROBE"); |
| 1111 | if (flash->tested & TEST_BAD_READ) |
| 1112 | printf(" READ"); |
| 1113 | if (flash->tested & TEST_BAD_ERASE) |
| 1114 | printf(" ERASE"); |
| 1115 | if (flash->tested & TEST_BAD_WRITE) |
| 1116 | printf(" WRITE"); |
| 1117 | printf("\n"); |
| 1118 | } |
| 1119 | if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) || |
| 1120 | (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) || |
| 1121 | (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) || |
| 1122 | (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) { |
| 1123 | printf("This flash part has status UNTESTED for operations:"); |
| 1124 | if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) |
| 1125 | printf(" PROBE"); |
| 1126 | if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) |
| 1127 | printf(" READ"); |
| 1128 | if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) |
| 1129 | printf(" ERASE"); |
| 1130 | if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE)) |
| 1131 | printf(" WRITE"); |
| 1132 | printf("\n"); |
| 1133 | } |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1134 | /* FIXME: This message is designed towards CLI users. */ |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1135 | printf("Please email a report to flashrom@flashrom.org if any " |
| 1136 | "of the above operations\nwork correctly for you with " |
| 1137 | "this flash part. Please include the flashrom\noutput " |
| 1138 | "with the additional -V option for all operations you " |
| 1139 | "tested (-V, -rV,\n-wV, -EV), and mention which " |
| 1140 | "mainboard or programmer you tested. Thanks for your " |
| 1141 | "help!\n===\n"); |
| 1142 | } |
| 1143 | } |
| 1144 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 1145 | int main(int argc, char *argv[]) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1146 | { |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1147 | return cli_classic(argc, argv); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | /* This function signature is horrible. We need to design a better interface, |
| 1151 | * but right now it allows us to split off the CLI code. |
| 1152 | */ |
| 1153 | int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it) |
| 1154 | { |
| 1155 | uint8_t *buf; |
| 1156 | unsigned long numbytes; |
| 1157 | FILE *image; |
| 1158 | int ret = 0; |
| 1159 | unsigned long size; |
| 1160 | |
| 1161 | size = flash->total_size * 1024; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1162 | buf = (uint8_t *) calloc(size, sizeof(char)); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1163 | |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 1164 | if (erase_it) { |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1165 | if (flash->tested & TEST_BAD_ERASE) { |
| 1166 | fprintf(stderr, "Erase is not working on this chip. "); |
| 1167 | if (!force) { |
| 1168 | fprintf(stderr, "Aborting.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1169 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1170 | return 1; |
| 1171 | } else { |
| 1172 | fprintf(stderr, "Continuing anyway.\n"); |
| 1173 | } |
| 1174 | } |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 1175 | if (flash->unlock) |
| 1176 | flash->unlock(flash); |
| 1177 | |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1178 | if (erase_flash(flash)) { |
| 1179 | emergency_help_message(); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1180 | programmer_shutdown(); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1181 | return 1; |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1182 | } |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 1183 | } else if (read_it) { |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 1184 | if (flash->unlock) |
| 1185 | flash->unlock(flash); |
| 1186 | |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1187 | if (read_flash(flash, filename)) { |
| 1188 | programmer_shutdown(); |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1189 | return 1; |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1190 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1191 | } else { |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1192 | struct stat image_stat; |
| 1193 | |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 1194 | if (flash->unlock) |
| 1195 | flash->unlock(flash); |
| 1196 | |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1197 | if (flash->tested & TEST_BAD_ERASE) { |
| 1198 | fprintf(stderr, "Erase is not working on this chip " |
| 1199 | "and erase is needed for write. "); |
| 1200 | if (!force) { |
| 1201 | fprintf(stderr, "Aborting.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1202 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1203 | return 1; |
| 1204 | } else { |
| 1205 | fprintf(stderr, "Continuing anyway.\n"); |
| 1206 | } |
| 1207 | } |
| 1208 | if (flash->tested & TEST_BAD_WRITE) { |
| 1209 | fprintf(stderr, "Write is not working on this chip. "); |
| 1210 | if (!force) { |
| 1211 | fprintf(stderr, "Aborting.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1212 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1213 | return 1; |
| 1214 | } else { |
| 1215 | fprintf(stderr, "Continuing anyway.\n"); |
| 1216 | } |
| 1217 | } |
Patrick Georgi | 0bf842d | 2010-01-25 22:55:33 +0000 | [diff] [blame] | 1218 | if ((image = fopen(filename, "rb")) == NULL) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1219 | perror(filename); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1220 | programmer_shutdown(); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1221 | exit(1); |
| 1222 | } |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1223 | if (fstat(fileno(image), &image_stat) != 0) { |
| 1224 | perror(filename); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1225 | programmer_shutdown(); |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1226 | exit(1); |
| 1227 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1228 | if (image_stat.st_size != flash->total_size * 1024) { |
Uwe Hermann | 793bdcd | 2008-05-22 22:47:04 +0000 | [diff] [blame] | 1229 | fprintf(stderr, "Error: Image size doesn't match\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1230 | programmer_shutdown(); |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1231 | exit(1); |
| 1232 | } |
| 1233 | |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1234 | numbytes = fread(buf, 1, size, image); |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 1235 | #if INTERNAL_SUPPORT == 1 |
Peter Stuge | 7ffbc6f | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 1236 | show_id(buf, size, force); |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 1237 | #endif |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1238 | fclose(image); |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1239 | if (numbytes != size) { |
| 1240 | fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1241 | programmer_shutdown(); |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1242 | return 1; |
| 1243 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1246 | // This should be moved into each flash part's code to do it |
| 1247 | // cleanly. This does the job. |
Carl-Daniel Hailfinger | f5fb51c | 2009-08-19 15:19:18 +0000 | [diff] [blame] | 1248 | handle_romentries(buf, flash); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1249 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1250 | // //////////////////////////////////////////////////////////// |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1251 | |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1252 | if (write_it) { |
Paul Fox | d51410c | 2009-06-12 08:04:08 +0000 | [diff] [blame] | 1253 | printf("Writing flash chip... "); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1254 | if (!flash->write) { |
| 1255 | fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1256 | programmer_shutdown(); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1257 | return 1; |
| 1258 | } |
Carl-Daniel Hailfinger | 0a3e5ae | 2009-07-24 12:18:54 +0000 | [diff] [blame] | 1259 | ret = flash->write(flash, buf); |
| 1260 | if (ret) { |
| 1261 | fprintf(stderr, "FAILED!\n"); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1262 | emergency_help_message(); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1263 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 0a3e5ae | 2009-07-24 12:18:54 +0000 | [diff] [blame] | 1264 | return 1; |
| 1265 | } else { |
| 1266 | printf("COMPLETE.\n"); |
| 1267 | } |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1268 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1269 | |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1270 | if (verify_it) { |
| 1271 | /* Work around chips which need some time to calm down. */ |
| 1272 | if (write_it) |
| 1273 | programmer_delay(1000*1000); |
Carl-Daniel Hailfinger | 0a3e5ae | 2009-07-24 12:18:54 +0000 | [diff] [blame] | 1274 | ret = verify_flash(flash, buf); |
Carl-Daniel Hailfinger | f529205 | 2009-11-17 09:57:34 +0000 | [diff] [blame] | 1275 | /* If we tried to write, and verification now fails, we |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1276 | * might have an emergency situation. |
| 1277 | */ |
| 1278 | if (ret && write_it) |
| 1279 | emergency_help_message(); |
| 1280 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1281 | |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 1282 | programmer_shutdown(); |
| 1283 | |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 1284 | return ret; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1285 | } |