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 | |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame^] | 836 | /* This function shares a lot of its structure with erase_flash(). */ |
| 837 | int selfcheck_eraseblocks(struct flashchip *flash) |
| 838 | { |
| 839 | int i, k; |
| 840 | |
| 841 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 842 | unsigned int done = 0; |
| 843 | struct block_eraser eraser = flash->block_erasers[k]; |
| 844 | |
| 845 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 846 | /* Blocks with zero size are bugs in flashchips.c. */ |
| 847 | if (eraser.eraseblocks[i].count && |
| 848 | !eraser.eraseblocks[i].size) { |
| 849 | msg_gerr("ERROR: Flash chip %s erase function " |
| 850 | "%i region %i has size 0. Please report" |
| 851 | " a bug at flashrom@flashrom.org\n", |
| 852 | flash->name, k, i); |
| 853 | return 1; |
| 854 | break; |
| 855 | } |
| 856 | /* Blocks with zero count are bugs in flashchips.c. */ |
| 857 | if (!eraser.eraseblocks[i].count && |
| 858 | eraser.eraseblocks[i].size) { |
| 859 | msg_gerr("ERROR: Flash chip %s erase function " |
| 860 | "%i region %i has count 0. Please report" |
| 861 | " a bug at flashrom@flashrom.org\n", |
| 862 | flash->name, k, i); |
| 863 | return 1; |
| 864 | break; |
| 865 | } |
| 866 | done += eraser.eraseblocks[i].count * |
| 867 | eraser.eraseblocks[i].size; |
| 868 | } |
| 869 | /* This erase function is completely empty. */ |
| 870 | if (!done) |
| 871 | continue; |
| 872 | if (done != flash->total_size * 1024) { |
| 873 | msg_gerr("ERROR: Flash chip %s erase function %i " |
| 874 | "region walking resulted in 0x%06x bytes total," |
| 875 | " expected 0x%06x bytes. Please report a bug at" |
| 876 | " flashrom@flashrom.org\n", flash->name, k, |
| 877 | done, flash->total_size * 1024); |
| 878 | return 1; |
| 879 | } |
| 880 | } |
| 881 | return 0; |
| 882 | } |
| 883 | |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 884 | int erase_flash(struct flashchip *flash) |
| 885 | { |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 886 | int i, j, k, ret = 0, found = 0; |
Carl-Daniel Hailfinger | 9d48916 | 2009-12-14 04:04:18 +0000 | [diff] [blame] | 887 | unsigned int start, len; |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 888 | |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 889 | printf("Erasing flash chip... "); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 890 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
Carl-Daniel Hailfinger | 9d48916 | 2009-12-14 04:04:18 +0000 | [diff] [blame] | 891 | unsigned int done = 0; |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 892 | struct block_eraser eraser = flash->block_erasers[k]; |
| 893 | |
| 894 | printf_debug("Looking at blockwise erase function %i... ", k); |
| 895 | if (!eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 896 | printf_debug("not defined. " |
| 897 | "Looking for another erase function.\n"); |
| 898 | continue; |
| 899 | } |
| 900 | if (!eraser.block_erase && eraser.eraseblocks[0].count) { |
| 901 | printf_debug("eraseblock layout is known, but no " |
| 902 | "matching block erase function found. " |
| 903 | "Looking for another erase function.\n"); |
| 904 | continue; |
| 905 | } |
| 906 | if (eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 907 | printf_debug("block erase function found, but " |
| 908 | "eraseblock layout is unknown. " |
| 909 | "Looking for another erase function.\n"); |
| 910 | continue; |
| 911 | } |
| 912 | found = 1; |
| 913 | printf_debug("trying... "); |
| 914 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 915 | /* count==0 for all automatically initialized array |
| 916 | * members so the loop below won't be executed for them. |
| 917 | */ |
| 918 | for (j = 0; j < eraser.eraseblocks[i].count; j++) { |
Carl-Daniel Hailfinger | 9d48916 | 2009-12-14 04:04:18 +0000 | [diff] [blame] | 919 | start = done + eraser.eraseblocks[i].size * j; |
| 920 | len = eraser.eraseblocks[i].size; |
| 921 | printf_debug("0x%06x-0x%06x, ", start, |
| 922 | start + len - 1); |
| 923 | ret = eraser.block_erase(flash, start, len); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 924 | if (ret) |
| 925 | break; |
| 926 | } |
| 927 | if (ret) |
| 928 | break; |
Carl-Daniel Hailfinger | 9d48916 | 2009-12-14 04:04:18 +0000 | [diff] [blame] | 929 | done += eraser.eraseblocks[i].count * |
| 930 | eraser.eraseblocks[i].size; |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 931 | } |
Carl-Daniel Hailfinger | 9d48916 | 2009-12-14 04:04:18 +0000 | [diff] [blame] | 932 | printf_debug("\n"); |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 933 | /* If everything is OK, don't try another erase function. */ |
| 934 | if (!ret) |
| 935 | break; |
| 936 | } |
| 937 | /* If no block erase function was found or block erase failed, retry. */ |
| 938 | if ((!found || ret) && (flash->erase)) { |
| 939 | found = 1; |
| 940 | printf_debug("Trying whole-chip erase function... "); |
| 941 | ret = flash->erase(flash); |
| 942 | } |
| 943 | if (!found) { |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 944 | fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n"); |
| 945 | return 1; |
| 946 | } |
Carl-Daniel Hailfinger | f160a12 | 2009-05-08 17:15:15 +0000 | [diff] [blame] | 947 | |
Carl-Daniel Hailfinger | f38431a | 2009-09-05 02:30:58 +0000 | [diff] [blame] | 948 | if (ret) { |
| 949 | fprintf(stderr, "FAILED!\n"); |
| 950 | } else { |
| 951 | printf("SUCCESS.\n"); |
| 952 | } |
| 953 | return ret; |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 956 | void emergency_help_message(void) |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 957 | { |
| 958 | fprintf(stderr, "Your flash chip is in an unknown state.\n" |
Uwe Hermann | c67d037 | 2009-10-01 18:40:02 +0000 | [diff] [blame] | 959 | "Get help on IRC at irc.freenode.net (channel #flashrom) or\n" |
| 960 | "mail flashrom@flashrom.org!\n--------------------" |
| 961 | "-----------------------------------------------------------\n" |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 962 | "DO NOT REBOOT OR POWEROFF!\n"); |
| 963 | } |
| 964 | |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 965 | /* The way to go if you want a delimited list of programmers*/ |
| 966 | void list_programmers(char *delim) |
| 967 | { |
| 968 | enum programmer p; |
| 969 | for (p = 0; p < PROGRAMMER_INVALID; p++) { |
| 970 | printf("%s", programmer_table[p].name); |
| 971 | if (p < PROGRAMMER_INVALID - 1) |
| 972 | printf("%s", delim); |
| 973 | } |
| 974 | printf("\n"); |
| 975 | } |
| 976 | |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 977 | void print_version(void) |
| 978 | { |
Carl-Daniel Hailfinger | a80cfbc | 2009-07-22 20:13:00 +0000 | [diff] [blame] | 979 | printf("flashrom v%s\n", flashrom_version); |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 980 | } |
| 981 | |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 982 | int selfcheck(void) |
| 983 | { |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame^] | 984 | int ret = 0; |
| 985 | struct flashchip *flash; |
| 986 | |
| 987 | /* Safety check. Instead of aborting after the first error, check |
| 988 | * if more errors exist. |
| 989 | */ |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 990 | if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) { |
| 991 | fprintf(stderr, "Programmer table miscompilation!\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame^] | 992 | ret = 1; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 993 | } |
| 994 | if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) { |
| 995 | fprintf(stderr, "SPI programmer table miscompilation!\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame^] | 996 | ret = 1; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 997 | } |
| 998 | #if BITBANG_SPI_SUPPORT == 1 |
| 999 | if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) { |
| 1000 | fprintf(stderr, "Bitbanging SPI master table miscompilation!\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame^] | 1001 | ret = 1; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1002 | } |
| 1003 | #endif |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame^] | 1004 | for (flash = flashchips; flash && flash->name; flash++) |
| 1005 | if (selfcheck_eraseblocks(flash)) |
| 1006 | ret = 1; |
| 1007 | return ret; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | void check_chip_supported(struct flashchip *flash) |
| 1011 | { |
| 1012 | if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) { |
| 1013 | printf("===\n"); |
| 1014 | if (flash->tested & TEST_BAD_MASK) { |
| 1015 | printf("This flash part has status NOT WORKING for operations:"); |
| 1016 | if (flash->tested & TEST_BAD_PROBE) |
| 1017 | printf(" PROBE"); |
| 1018 | if (flash->tested & TEST_BAD_READ) |
| 1019 | printf(" READ"); |
| 1020 | if (flash->tested & TEST_BAD_ERASE) |
| 1021 | printf(" ERASE"); |
| 1022 | if (flash->tested & TEST_BAD_WRITE) |
| 1023 | printf(" WRITE"); |
| 1024 | printf("\n"); |
| 1025 | } |
| 1026 | if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) || |
| 1027 | (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) || |
| 1028 | (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) || |
| 1029 | (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) { |
| 1030 | printf("This flash part has status UNTESTED for operations:"); |
| 1031 | if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) |
| 1032 | printf(" PROBE"); |
| 1033 | if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) |
| 1034 | printf(" READ"); |
| 1035 | if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) |
| 1036 | printf(" ERASE"); |
| 1037 | if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE)) |
| 1038 | printf(" WRITE"); |
| 1039 | printf("\n"); |
| 1040 | } |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1041 | /* FIXME: This message is designed towards CLI users. */ |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1042 | printf("Please email a report to flashrom@flashrom.org if any " |
| 1043 | "of the above operations\nwork correctly for you with " |
| 1044 | "this flash part. Please include the flashrom\noutput " |
| 1045 | "with the additional -V option for all operations you " |
| 1046 | "tested (-V, -rV,\n-wV, -EV), and mention which " |
| 1047 | "mainboard or programmer you tested. Thanks for your " |
| 1048 | "help!\n===\n"); |
| 1049 | } |
| 1050 | } |
| 1051 | |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 1052 | int main(int argc, char *argv[]) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1053 | { |
Carl-Daniel Hailfinger | a84835a | 2010-01-07 03:24:05 +0000 | [diff] [blame] | 1054 | return cli_classic(argc, argv); |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
| 1057 | /* This function signature is horrible. We need to design a better interface, |
| 1058 | * but right now it allows us to split off the CLI code. |
| 1059 | */ |
| 1060 | int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it) |
| 1061 | { |
| 1062 | uint8_t *buf; |
| 1063 | unsigned long numbytes; |
| 1064 | FILE *image; |
| 1065 | int ret = 0; |
| 1066 | unsigned long size; |
| 1067 | |
| 1068 | size = flash->total_size * 1024; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1069 | buf = (uint8_t *) calloc(size, sizeof(char)); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1070 | |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 1071 | if (erase_it) { |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1072 | if (flash->tested & TEST_BAD_ERASE) { |
| 1073 | fprintf(stderr, "Erase is not working on this chip. "); |
| 1074 | if (!force) { |
| 1075 | fprintf(stderr, "Aborting.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1076 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1077 | return 1; |
| 1078 | } else { |
| 1079 | fprintf(stderr, "Continuing anyway.\n"); |
| 1080 | } |
| 1081 | } |
| 1082 | if (erase_flash(flash)) { |
| 1083 | emergency_help_message(); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1084 | programmer_shutdown(); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1085 | return 1; |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1086 | } |
Ollie Lho | efa2858 | 2004-12-08 20:10:01 +0000 | [diff] [blame] | 1087 | } else if (read_it) { |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1088 | if (read_flash(flash, filename)) { |
| 1089 | programmer_shutdown(); |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1090 | return 1; |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1091 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1092 | } else { |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1093 | struct stat image_stat; |
| 1094 | |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1095 | if (flash->tested & TEST_BAD_ERASE) { |
| 1096 | fprintf(stderr, "Erase is not working on this chip " |
| 1097 | "and erase is needed for write. "); |
| 1098 | if (!force) { |
| 1099 | fprintf(stderr, "Aborting.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1100 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1101 | return 1; |
| 1102 | } else { |
| 1103 | fprintf(stderr, "Continuing anyway.\n"); |
| 1104 | } |
| 1105 | } |
| 1106 | if (flash->tested & TEST_BAD_WRITE) { |
| 1107 | fprintf(stderr, "Write is not working on this chip. "); |
| 1108 | if (!force) { |
| 1109 | fprintf(stderr, "Aborting.\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1110 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1111 | return 1; |
| 1112 | } else { |
| 1113 | fprintf(stderr, "Continuing anyway.\n"); |
| 1114 | } |
| 1115 | } |
Ollie Lho | 761bf1b | 2004-03-20 16:46:10 +0000 | [diff] [blame] | 1116 | if ((image = fopen(filename, "r")) == NULL) { |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1117 | perror(filename); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1118 | programmer_shutdown(); |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1119 | exit(1); |
| 1120 | } |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1121 | if (fstat(fileno(image), &image_stat) != 0) { |
| 1122 | perror(filename); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1123 | programmer_shutdown(); |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1124 | exit(1); |
| 1125 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1126 | if (image_stat.st_size != flash->total_size * 1024) { |
Uwe Hermann | 793bdcd | 2008-05-22 22:47:04 +0000 | [diff] [blame] | 1127 | fprintf(stderr, "Error: Image size doesn't match\n"); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1128 | programmer_shutdown(); |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 1129 | exit(1); |
| 1130 | } |
| 1131 | |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1132 | numbytes = fread(buf, 1, size, image); |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 1133 | #if INTERNAL_SUPPORT == 1 |
Peter Stuge | 7ffbc6f | 2008-06-18 02:08:40 +0000 | [diff] [blame] | 1134 | show_id(buf, size, force); |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 1135 | #endif |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1136 | fclose(image); |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1137 | if (numbytes != size) { |
| 1138 | 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] | 1139 | programmer_shutdown(); |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 1140 | return 1; |
| 1141 | } |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1144 | // This should be moved into each flash part's code to do it |
| 1145 | // cleanly. This does the job. |
Carl-Daniel Hailfinger | f5fb51c | 2009-08-19 15:19:18 +0000 | [diff] [blame] | 1146 | handle_romentries(buf, flash); |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1147 | |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1148 | // //////////////////////////////////////////////////////////// |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 1149 | |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1150 | if (write_it) { |
Paul Fox | d51410c | 2009-06-12 08:04:08 +0000 | [diff] [blame] | 1151 | printf("Writing flash chip... "); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1152 | if (!flash->write) { |
| 1153 | 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] | 1154 | programmer_shutdown(); |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1155 | return 1; |
| 1156 | } |
Carl-Daniel Hailfinger | 0a3e5ae | 2009-07-24 12:18:54 +0000 | [diff] [blame] | 1157 | ret = flash->write(flash, buf); |
| 1158 | if (ret) { |
| 1159 | fprintf(stderr, "FAILED!\n"); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1160 | emergency_help_message(); |
Uwe Hermann | 2bc98f6 | 2009-09-30 18:29:55 +0000 | [diff] [blame] | 1161 | programmer_shutdown(); |
Carl-Daniel Hailfinger | 0a3e5ae | 2009-07-24 12:18:54 +0000 | [diff] [blame] | 1162 | return 1; |
| 1163 | } else { |
| 1164 | printf("COMPLETE.\n"); |
| 1165 | } |
Peter Stuge | f31104c | 2008-04-28 14:47:30 +0000 | [diff] [blame] | 1166 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1167 | |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1168 | if (verify_it) { |
| 1169 | /* Work around chips which need some time to calm down. */ |
| 1170 | if (write_it) |
| 1171 | programmer_delay(1000*1000); |
Carl-Daniel Hailfinger | 0a3e5ae | 2009-07-24 12:18:54 +0000 | [diff] [blame] | 1172 | ret = verify_flash(flash, buf); |
Carl-Daniel Hailfinger | f529205 | 2009-11-17 09:57:34 +0000 | [diff] [blame] | 1173 | /* If we tried to write, and verification now fails, we |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1174 | * might have an emergency situation. |
| 1175 | */ |
| 1176 | if (ret && write_it) |
| 1177 | emergency_help_message(); |
| 1178 | } |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 1179 | |
Carl-Daniel Hailfinger | 702218d | 2009-05-08 17:43:22 +0000 | [diff] [blame] | 1180 | programmer_shutdown(); |
| 1181 | |
Stefan Reinauer | 143da0b | 2006-01-04 16:42:57 +0000 | [diff] [blame] | 1182 | return ret; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1183 | } |