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