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