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