blob: 2118beccda997fa9868f1ede9b99190087864a07 [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00008 *
Uwe Hermannd1107642007-08-29 17:52:32 +00009 * 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. Minnichf4cf2ba2002-01-29 18:26:26 +000013 *
Uwe Hermannd1107642007-08-29 17:52:32 +000014 * 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. Minnichf4cf2ba2002-01-29 18:26:26 +000018 *
Uwe Hermannd1107642007-08-29 17:52:32 +000019 * 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. Minnichf4cf2ba2002-01-29 18:26:26 +000022 */
23
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000024#include <stdio.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000025#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000026#include <sys/types.h>
27#include <sys/stat.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000028#include <string.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000029#include <stdlib.h>
Ollie Lho184a4042005-11-26 21:55:36 +000030#include <getopt.h>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000031#if HAVE_UTSNAME == 1
32#include <sys/utsname.h>
33#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000034#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000035#include "flashchips.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000036
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000037const char * const flashrom_version = FLASHROM_VERSION;
Ronald G. Minnichceec4202003-07-25 04:37:41 +000038char *chip_to_probe = NULL;
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000039int verbose = 0;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000040
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000041#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +000042enum programmer programmer = PROGRAMMER_INTERNAL;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000043#elif CONFIG_DUMMY == 1
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000044enum programmer programmer = PROGRAMMER_DUMMY;
45#else
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000046/* If neither internal nor dummy are selected, we must pick a sensible default.
47 * Since there is no reason to prefer a particular external programmer, we fail
48 * if more than one of them is selected. If only one is selected, it is clear
49 * that the user wants that one to become the default.
50 */
Andrew Morganc29c2e72010-06-07 22:37:54 +000051#if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_FT2232_SPI+CONFIG_SERPROG+CONFIG_BUSPIRATE_SPI+CONFIG_DEDIPROG > 1
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000052#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all programmers except one.
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000053#endif
54enum programmer programmer =
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000055#if CONFIG_NIC3COM == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000056 PROGRAMMER_NIC3COM
57#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000058#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +000059 PROGRAMMER_NICREALTEK
60 PROGRAMMER_NICREALTEK2
61#endif
Andrew Morganc29c2e72010-06-07 22:37:54 +000062#if CONFIG_NICNATSEMI == 1
63 PROGRAMMER_NICNATSEMI
64#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000065#if CONFIG_GFXNVIDIA == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000066 PROGRAMMER_GFXNVIDIA
67#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000068#if CONFIG_DRKAISER == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000069 PROGRAMMER_DRKAISER
70#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000071#if CONFIG_SATASII == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000072 PROGRAMMER_SATASII
73#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000074#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +000075 PROGRAMMER_ATAHPT
76#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000077#if CONFIG_FT2232_SPI == 1
78 PROGRAMMER_FT2232_SPI
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000079#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000080#if CONFIG_SERPROG == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000081 PROGRAMMER_SERPROG
82#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000083#if CONFIG_BUSPIRATE_SPI == 1
84 PROGRAMMER_BUSPIRATE_SPI
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000085#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000086#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000087 PROGRAMMER_DEDIPROG
88#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000089;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000090#endif
91
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +000092static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000093
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000094/* Supported buses for the current programmer. */
95enum chipbustype buses_supported;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000096
97/**
98 * Programmers supporting multiple buses can have differing size limits on
99 * each bus. Store the limits for each bus in a common struct.
100 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000101struct decode_sizes max_rom_decode;
102
103/* If nonzero, used as the start address of bottom-aligned flash. */
104unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000105
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000106/* Is writing allowed with this programmer? */
107int programmer_may_write;
108
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000109const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000110#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000111 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000112 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000113 .init = internal_init,
114 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000115 .map_flash_region = physmap,
116 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000117 .chip_readb = internal_chip_readb,
118 .chip_readw = internal_chip_readw,
119 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000120 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000121 .chip_writeb = internal_chip_writeb,
122 .chip_writew = internal_chip_writew,
123 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000124 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000125 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000126 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000127#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000128
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000129#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000130 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000131 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000132 .init = dummy_init,
133 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000134 .map_flash_region = dummy_map,
135 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000136 .chip_readb = dummy_chip_readb,
137 .chip_readw = dummy_chip_readw,
138 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000139 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000140 .chip_writeb = dummy_chip_writeb,
141 .chip_writew = dummy_chip_writew,
142 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000143 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000144 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000145 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000146#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000147
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000148#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000149 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000150 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000151 .init = nic3com_init,
152 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000153 .map_flash_region = fallback_map,
154 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000155 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000156 .chip_readw = fallback_chip_readw,
157 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000158 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000159 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000160 .chip_writew = fallback_chip_writew,
161 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000162 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000163 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000164 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000165#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000166
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000167#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000168 {
169 .name = "nicrealtek",
170 .init = nicrealtek_init,
171 .shutdown = nicrealtek_shutdown,
172 .map_flash_region = fallback_map,
173 .unmap_flash_region = fallback_unmap,
174 .chip_readb = nicrealtek_chip_readb,
175 .chip_readw = fallback_chip_readw,
176 .chip_readl = fallback_chip_readl,
177 .chip_readn = fallback_chip_readn,
178 .chip_writeb = nicrealtek_chip_writeb,
179 .chip_writew = fallback_chip_writew,
180 .chip_writel = fallback_chip_writel,
181 .chip_writen = fallback_chip_writen,
182 .delay = internal_delay,
183 },
184 {
185 .name = "nicsmc1211",
186 .init = nicsmc1211_init,
187 .shutdown = nicrealtek_shutdown,
188 .map_flash_region = fallback_map,
189 .unmap_flash_region = fallback_unmap,
190 .chip_readb = nicrealtek_chip_readb,
191 .chip_readw = fallback_chip_readw,
192 .chip_readl = fallback_chip_readl,
193 .chip_readn = fallback_chip_readn,
194 .chip_writeb = nicrealtek_chip_writeb,
195 .chip_writew = fallback_chip_writew,
196 .chip_writel = fallback_chip_writel,
197 .chip_writen = fallback_chip_writen,
198 .delay = internal_delay,
199 },
200#endif
201
Andrew Morganc29c2e72010-06-07 22:37:54 +0000202#if CONFIG_NICNATSEMI == 1
203 {
204 .name = "nicnatsemi",
205 .init = nicnatsemi_init,
206 .shutdown = nicnatsemi_shutdown,
207 .map_flash_region = fallback_map,
208 .unmap_flash_region = fallback_unmap,
209 .chip_readb = nicnatsemi_chip_readb,
210 .chip_readw = fallback_chip_readw,
211 .chip_readl = fallback_chip_readl,
212 .chip_readn = fallback_chip_readn,
213 .chip_writeb = nicnatsemi_chip_writeb,
214 .chip_writew = fallback_chip_writew,
215 .chip_writel = fallback_chip_writel,
216 .chip_writen = fallback_chip_writen,
217 .delay = internal_delay,
218 },
219#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000220
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000221#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000222 {
223 .name = "gfxnvidia",
224 .init = gfxnvidia_init,
225 .shutdown = gfxnvidia_shutdown,
226 .map_flash_region = fallback_map,
227 .unmap_flash_region = fallback_unmap,
228 .chip_readb = gfxnvidia_chip_readb,
229 .chip_readw = fallback_chip_readw,
230 .chip_readl = fallback_chip_readl,
231 .chip_readn = fallback_chip_readn,
232 .chip_writeb = gfxnvidia_chip_writeb,
233 .chip_writew = fallback_chip_writew,
234 .chip_writel = fallback_chip_writel,
235 .chip_writen = fallback_chip_writen,
236 .delay = internal_delay,
237 },
238#endif
239
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000240#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000241 {
TURBO Jb0912c02009-09-02 23:00:46 +0000242 .name = "drkaiser",
243 .init = drkaiser_init,
244 .shutdown = drkaiser_shutdown,
245 .map_flash_region = fallback_map,
246 .unmap_flash_region = fallback_unmap,
247 .chip_readb = drkaiser_chip_readb,
248 .chip_readw = fallback_chip_readw,
249 .chip_readl = fallback_chip_readl,
250 .chip_readn = fallback_chip_readn,
251 .chip_writeb = drkaiser_chip_writeb,
252 .chip_writew = fallback_chip_writew,
253 .chip_writel = fallback_chip_writel,
254 .chip_writen = fallback_chip_writen,
255 .delay = internal_delay,
256 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000257#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000258
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000259#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000260 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000261 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000262 .init = satasii_init,
263 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000264 .map_flash_region = fallback_map,
265 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000266 .chip_readb = satasii_chip_readb,
267 .chip_readw = fallback_chip_readw,
268 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000269 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000270 .chip_writeb = satasii_chip_writeb,
271 .chip_writew = fallback_chip_writew,
272 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000273 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000274 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000275 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000276#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000277
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000278#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000279 {
280 .name = "atahpt",
281 .init = atahpt_init,
282 .shutdown = atahpt_shutdown,
283 .map_flash_region = fallback_map,
284 .unmap_flash_region = fallback_unmap,
285 .chip_readb = atahpt_chip_readb,
286 .chip_readw = fallback_chip_readw,
287 .chip_readl = fallback_chip_readl,
288 .chip_readn = fallback_chip_readn,
289 .chip_writeb = atahpt_chip_writeb,
290 .chip_writew = fallback_chip_writew,
291 .chip_writel = fallback_chip_writel,
292 .chip_writen = fallback_chip_writen,
293 .delay = internal_delay,
294 },
295#endif
296
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000297#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000298#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000299 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000300 .name = "it87spi",
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000301 .init = it87spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000302 .shutdown = noop_shutdown,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000303 .map_flash_region = fallback_map,
304 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000305 .chip_readb = noop_chip_readb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000306 .chip_readw = fallback_chip_readw,
307 .chip_readl = fallback_chip_readl,
308 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000309 .chip_writeb = noop_chip_writeb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000310 .chip_writew = fallback_chip_writew,
311 .chip_writel = fallback_chip_writel,
312 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000313 .delay = internal_delay,
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000314 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000315#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000316#endif
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000317
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000318#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000319 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000320 .name = "ft2232_spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000321 .init = ft2232_spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000322 .shutdown = noop_shutdown, /* Missing shutdown */
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000323 .map_flash_region = fallback_map,
324 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000325 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000326 .chip_readw = fallback_chip_readw,
327 .chip_readl = fallback_chip_readl,
328 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000329 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000330 .chip_writew = fallback_chip_writew,
331 .chip_writel = fallback_chip_writel,
332 .chip_writen = fallback_chip_writen,
333 .delay = internal_delay,
334 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000335#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000336
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000337#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000338 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000339 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000340 .init = serprog_init,
341 .shutdown = serprog_shutdown,
342 .map_flash_region = fallback_map,
343 .unmap_flash_region = fallback_unmap,
344 .chip_readb = serprog_chip_readb,
345 .chip_readw = fallback_chip_readw,
346 .chip_readl = fallback_chip_readl,
347 .chip_readn = serprog_chip_readn,
348 .chip_writeb = serprog_chip_writeb,
349 .chip_writew = fallback_chip_writew,
350 .chip_writel = fallback_chip_writel,
351 .chip_writen = fallback_chip_writen,
352 .delay = serprog_delay,
353 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000354#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000355
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000356#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000357 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000358 .name = "buspirate_spi",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000359 .init = buspirate_spi_init,
360 .shutdown = buspirate_spi_shutdown,
361 .map_flash_region = fallback_map,
362 .unmap_flash_region = fallback_unmap,
363 .chip_readb = noop_chip_readb,
364 .chip_readw = fallback_chip_readw,
365 .chip_readl = fallback_chip_readl,
366 .chip_readn = fallback_chip_readn,
367 .chip_writeb = noop_chip_writeb,
368 .chip_writew = fallback_chip_writew,
369 .chip_writel = fallback_chip_writel,
370 .chip_writen = fallback_chip_writen,
371 .delay = internal_delay,
372 },
373#endif
374
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000375#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000376 {
377 .name = "dediprog",
378 .init = dediprog_init,
379 .shutdown = dediprog_shutdown,
380 .map_flash_region = fallback_map,
381 .unmap_flash_region = fallback_unmap,
382 .chip_readb = noop_chip_readb,
383 .chip_readw = fallback_chip_readw,
384 .chip_readl = fallback_chip_readl,
385 .chip_readn = fallback_chip_readn,
386 .chip_writeb = noop_chip_writeb,
387 .chip_writew = fallback_chip_writew,
388 .chip_writel = fallback_chip_writel,
389 .chip_writen = fallback_chip_writen,
390 .delay = internal_delay,
391 },
392#endif
393
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000394 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000395};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000396
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000397#define SHUTDOWN_MAXFN 4
398static int shutdown_fn_count = 0;
399struct shutdown_func_data {
400 void (*func) (void *data);
401 void *data;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000402} static shutdown_fn[SHUTDOWN_MAXFN];
403/* Initialize to 0 to make sure nobody registers a shutdown function before
404 * programmer init.
405 */
406static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000407
408/* Register a function to be executed on programmer shutdown.
409 * The advantage over atexit() is that you can supply a void pointer which will
410 * be used as parameter to the registered function upon programmer shutdown.
411 * This pointer can point to arbitrary data used by said function, e.g. undo
412 * information for GPIO settings etc. If unneeded, set data=NULL.
413 * Please note that the first (void *data) belongs to the function signature of
414 * the function passed as first parameter.
415 */
416int register_shutdown(void (*function) (void *data), void *data)
417{
418 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000419 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000420 SHUTDOWN_MAXFN);
421 return 1;
422 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000423 if (!may_register_shutdown) {
424 msg_perr("Tried to register a shutdown function before "
425 "programmer init.\n");
426 return 1;
427 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000428 shutdown_fn[shutdown_fn_count].func = function;
429 shutdown_fn[shutdown_fn_count].data = data;
430 shutdown_fn_count++;
431
432 return 0;
433}
434
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000435int programmer_init(char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000436{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000437 int ret;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000438 /* Initialize all programmer specific data. */
439 /* Default to unlimited decode sizes. */
440 max_rom_decode = (const struct decode_sizes) {
441 .parallel = 0xffffffff,
442 .lpc = 0xffffffff,
443 .fwh = 0xffffffff,
444 .spi = 0xffffffff
445 };
446 /* Default to Parallel/LPC/FWH flash devices. If a known host controller
447 * is found, the init routine sets the buses_supported bitfield.
448 */
449 buses_supported = CHIP_BUSTYPE_NONSPI;
450 /* Default to top aligned flash at 4 GB. */
451 flashbase = 0;
452 /* Registering shutdown functions is now allowed. */
453 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000454 /* Default to allowing writes. Broken programmers set this to 0. */
455 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000456
457 programmer_param = param;
458 msg_pdbg("Initializing %s programmer\n",
459 programmer_table[programmer].name);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000460 ret = programmer_table[programmer].init();
461 if (programmer_param && strlen(programmer_param)) {
462 msg_perr("Unhandled programmer parameters: %s\n",
463 programmer_param);
464 /* Do not error out here, the init itself was successful. */
465 }
466 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000467}
468
469int programmer_shutdown(void)
470{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000471 /* Registering shutdown functions is no longer allowed. */
472 may_register_shutdown = 0;
473 while (shutdown_fn_count > 0) {
474 int i = --shutdown_fn_count;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000475 shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000476 }
Uwe Hermann09e04f72009-05-16 22:36:00 +0000477 return programmer_table[programmer].shutdown();
478}
479
480void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
481 size_t len)
482{
483 return programmer_table[programmer].map_flash_region(descr,
484 phys_addr, len);
485}
486
487void programmer_unmap_flash_region(void *virt_addr, size_t len)
488{
489 programmer_table[programmer].unmap_flash_region(virt_addr, len);
490}
491
492void chip_writeb(uint8_t val, chipaddr addr)
493{
494 programmer_table[programmer].chip_writeb(val, addr);
495}
496
497void chip_writew(uint16_t val, chipaddr addr)
498{
499 programmer_table[programmer].chip_writew(val, addr);
500}
501
502void chip_writel(uint32_t val, chipaddr addr)
503{
504 programmer_table[programmer].chip_writel(val, addr);
505}
506
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000507void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
508{
509 programmer_table[programmer].chip_writen(buf, addr, len);
510}
511
Uwe Hermann09e04f72009-05-16 22:36:00 +0000512uint8_t chip_readb(const chipaddr addr)
513{
514 return programmer_table[programmer].chip_readb(addr);
515}
516
517uint16_t chip_readw(const chipaddr addr)
518{
519 return programmer_table[programmer].chip_readw(addr);
520}
521
522uint32_t chip_readl(const chipaddr addr)
523{
524 return programmer_table[programmer].chip_readl(addr);
525}
526
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000527void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
528{
529 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000530}
531
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000532void programmer_delay(int usecs)
533{
534 programmer_table[programmer].delay(usecs);
535}
536
Peter Stuge776d2022009-01-26 00:39:57 +0000537void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000538{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000539 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000540 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000541 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000542 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000543}
544
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000545int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000546{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000547 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000548
549 return 0;
550}
551
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000552int min(int a, int b)
553{
554 return (a < b) ? a : b;
555}
556
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000557int max(int a, int b)
558{
559 return (a > b) ? a : b;
560}
561
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000562int bitcount(unsigned long a)
563{
564 int i = 0;
565 for (; a != 0; a >>= 1)
566 if (a & 1)
567 i++;
568 return i;
569}
570
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000571char *strcat_realloc(char *dest, const char *src)
572{
573 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000574 if (!dest) {
575 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000576 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000577 }
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000578 strcat(dest, src);
579 return dest;
580}
581
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000582/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000583 * It will look for needle with a subsequent '=' in haystack, return a copy of
584 * needle and remove everything from the first occurrence of needle to the next
585 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000586 */
587char *extract_param(char **haystack, char *needle, char *delim)
588{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000589 char *param_pos, *opt_pos, *rest;
590 char *opt = NULL;
591 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000592 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000593
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000594 needlelen = strlen(needle);
595 if (!needlelen) {
596 msg_gerr("%s: empty needle! Please report a bug at "
597 "flashrom@flashrom.org\n", __func__);
598 return NULL;
599 }
600 /* No programmer parameters given. */
601 if (*haystack == NULL)
602 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000603 param_pos = strstr(*haystack, needle);
604 do {
605 if (!param_pos)
606 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000607 /* Needle followed by '='? */
608 if (param_pos[needlelen] == '=') {
609
610 /* Beginning of the string? */
611 if (param_pos == *haystack)
612 break;
613 /* After a delimiter? */
614 if (strchr(delim, *(param_pos - 1)))
615 break;
616 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000617 /* Continue searching. */
618 param_pos++;
619 param_pos = strstr(param_pos, needle);
620 } while (1);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000621
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000622 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000623 /* Get the string after needle and '='. */
624 opt_pos = param_pos + needlelen + 1;
625 optlen = strcspn(opt_pos, delim);
626 /* Return an empty string if the parameter was empty. */
627 opt = malloc(optlen + 1);
628 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000629 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000630 exit(1);
631 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000632 strncpy(opt, opt_pos, optlen);
633 opt[optlen] = '\0';
634 rest = opt_pos + optlen;
635 /* Skip all delimiters after the current parameter. */
636 rest += strspn(rest, delim);
637 memmove(param_pos, rest, strlen(rest) + 1);
638 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000639 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000640
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000641 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000642}
643
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000644char *extract_programmer_param(char *param_name)
645{
646 return extract_param(&programmer_param, param_name, ",");
647}
648
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000649/* start is an offset to the base address of the flash chip */
650int check_erased_range(struct flashchip *flash, int start, int len)
651{
652 int ret;
653 uint8_t *cmpbuf = malloc(len);
654
655 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000656 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000657 exit(1);
658 }
659 memset(cmpbuf, 0xff, len);
660 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
661 free(cmpbuf);
662 return ret;
663}
664
665/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000666 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
667 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000668 * @start offset to the base address of the flash chip
669 * @len length of the verified area
670 * @message string to print in the "FAILED" message
671 * @return 0 for success, -1 for failure
672 */
673int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
674{
675 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000676 int page_size = flash->page_size;
677 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000678 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000679
680 if (!len)
681 goto out_free;
682
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000683 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000684 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000685 return 1;
686 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000687 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000688 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000689 exit(1);
690 }
691
692 if (start + len > flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000693 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000694 " total_size 0x%x\n", __func__, start, len,
695 flash->total_size * 1024);
696 ret = -1;
697 goto out_free;
698 }
699 if (!message)
700 message = "VERIFY";
701
702 /* Warning: This loop has a very unusual condition and body.
703 * The loop needs to go through each page with at least one affected
704 * byte. The lowest page number is (start / page_size) since that
705 * division rounds down. The highest page number we want is the page
706 * where the last byte of the range lives. That last byte has the
707 * address (start + len - 1), thus the highest page number is
708 * (start + len - 1) / page_size. Since we want to include that last
709 * page as well, the loop condition uses <=.
710 */
711 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
712 /* Byte position of the first byte in the range in this page. */
713 starthere = max(start, i * page_size);
714 /* Length of bytes in the range in this page. */
715 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000716 ret = flash->read(flash, readbuf, starthere, lenhere);
717 if (ret) {
718 msg_gerr("Verification impossible because read failed "
719 "at 0x%x (len 0x%x)\n", starthere, lenhere);
720 break;
721 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000722 for (j = 0; j < lenhere; j++) {
723 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000724 /* Only print the first failure. */
725 if (!failcount++)
Sean Nelson316a29f2010-05-07 20:09:04 +0000726 msg_cerr("%s FAILED at 0x%08x! "
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000727 "Expected=0x%02x, Read=0x%02x,",
728 message, starthere + j,
729 cmpbuf[starthere - start + j],
730 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000731 }
732 }
733 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000734 if (failcount) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000735 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000736 start, start + len - 1, failcount);
737 ret = -1;
738 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000739
740out_free:
741 free(readbuf);
742 return ret;
743}
744
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000745/**
746 * Check if the buffer @have can be programmed to the content of @want without
747 * erasing. This is only possible if all chunks of size @gran are either kept
748 * as-is or changed from an all-ones state to any other state.
749 * The following write granularities (enum @gran) are known:
750 * - 1 bit. Each bit can be cleared individually.
751 * - 1 byte. A byte can be written once. Further writes to an already written
752 * byte cause the contents to be either undefined or to stay unchanged.
753 * - 128 bytes. If less than 128 bytes are written, the rest will be
754 * erased. Each write to a 128-byte region will trigger an automatic erase
755 * before anything is written. Very uncommon behaviour and unsupported by
756 * this function.
757 * - 256 bytes. If less than 256 bytes are written, the contents of the
758 * unwritten bytes are undefined.
759 *
760 * @have buffer with current content
761 * @want buffer with desired content
762 * @len length of the verified area
763 * @gran write granularity (enum, not count)
764 * @return 0 if no erase is needed, 1 otherwise
765 */
766int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
767{
768 int result = 0;
769 int i, j, limit;
770
771 switch (gran) {
772 case write_gran_1bit:
773 for (i = 0; i < len; i++)
774 if ((have[i] & want[i]) != want[i]) {
775 result = 1;
776 break;
777 }
778 break;
779 case write_gran_1byte:
780 for (i = 0; i < len; i++)
781 if ((have[i] != want[i]) && (have[i] != 0xff)) {
782 result = 1;
783 break;
784 }
785 break;
786 case write_gran_256bytes:
787 for (j = 0; j < len / 256; j++) {
788 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000789 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000790 if (!memcmp(have + j * 256, want + j * 256, limit))
791 continue;
792 /* have needs to be in erased state. */
793 for (i = 0; i < limit; i++)
794 if (have[i] != 0xff) {
795 result = 1;
796 break;
797 }
798 if (result)
799 break;
800 }
801 break;
802 }
803 return result;
804}
805
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000806/* This function generates various test patterns useful for testing controller
807 * and chip communication as well as chip behaviour.
808 *
809 * If a byte can be written multiple times, each time keeping 0-bits at 0
810 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
811 * is essentially an AND operation. That's also the reason why this function
812 * provides the result of AND between various patterns.
813 *
814 * Below is a list of patterns (and their block length).
815 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
816 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
817 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
818 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
819 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
820 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
821 * Pattern 6 is 00 (1 Byte)
822 * Pattern 7 is ff (1 Byte)
823 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
824 * byte block.
825 *
826 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
827 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
828 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
829 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
830 * Pattern 12 is 00 (1 Byte)
831 * Pattern 13 is ff (1 Byte)
832 * Patterns 8-13 have no block number.
833 *
834 * Patterns 0-3 are created to detect and efficiently diagnose communication
835 * slips like missed bits or bytes and their repetitive nature gives good visual
836 * cues to the person inspecting the results. In addition, the following holds:
837 * AND Pattern 0/1 == Pattern 4
838 * AND Pattern 2/3 == Pattern 5
839 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
840 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
841 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
842 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
843 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
844 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
845 * Besides that, they provide for bit testing of the last two bytes of every
846 * 256 byte block which contains the block number for patterns 0-6.
847 * Patterns 10-11 are special purpose for detecting subblock aliasing with
848 * block sizes >256 bytes (some Dataflash chips etc.)
849 * AND Pattern 8/9 == Pattern 12
850 * AND Pattern 10/11 == Pattern 12
851 * Pattern 13 is the completely erased state.
852 * None of the patterns can detect aliasing at boundaries which are a multiple
853 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
854 */
855int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
856{
857 int i;
858
859 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000860 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000861 return 1;
862 }
863
864 switch (variant) {
865 case 0:
866 for (i = 0; i < size; i++)
867 buf[i] = (i & 0xf) << 4 | 0x5;
868 break;
869 case 1:
870 for (i = 0; i < size; i++)
871 buf[i] = (i & 0xf) << 4 | 0xa;
872 break;
873 case 2:
874 for (i = 0; i < size; i++)
875 buf[i] = 0x50 | (i & 0xf);
876 break;
877 case 3:
878 for (i = 0; i < size; i++)
879 buf[i] = 0xa0 | (i & 0xf);
880 break;
881 case 4:
882 for (i = 0; i < size; i++)
883 buf[i] = (i & 0xf) << 4;
884 break;
885 case 5:
886 for (i = 0; i < size; i++)
887 buf[i] = i & 0xf;
888 break;
889 case 6:
890 memset(buf, 0x00, size);
891 break;
892 case 7:
893 memset(buf, 0xff, size);
894 break;
895 case 8:
896 for (i = 0; i < size; i++)
897 buf[i] = i & 0xff;
898 break;
899 case 9:
900 for (i = 0; i < size; i++)
901 buf[i] = ~(i & 0xff);
902 break;
903 case 10:
904 for (i = 0; i < size % 2; i++) {
905 buf[i * 2] = (i >> 8) & 0xff;
906 buf[i * 2 + 1] = i & 0xff;
907 }
908 if (size & 0x1)
909 buf[i * 2] = (i >> 8) & 0xff;
910 break;
911 case 11:
912 for (i = 0; i < size % 2; i++) {
913 buf[i * 2] = ~((i >> 8) & 0xff);
914 buf[i * 2 + 1] = ~(i & 0xff);
915 }
916 if (size & 0x1)
917 buf[i * 2] = ~((i >> 8) & 0xff);
918 break;
919 case 12:
920 memset(buf, 0x00, size);
921 break;
922 case 13:
923 memset(buf, 0xff, size);
924 break;
925 }
926
927 if ((variant >= 0) && (variant <= 7)) {
928 /* Write block number in the last two bytes of each 256-byte
929 * block, big endian for easier reading of the hexdump.
930 * Note that this wraps around for chips larger than 2^24 bytes
931 * (16 MB).
932 */
933 for (i = 0; i < size / 256; i++) {
934 buf[i * 256 + 254] = (i >> 8) & 0xff;
935 buf[i * 256 + 255] = i & 0xff;
936 }
937 }
938
939 return 0;
940}
941
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000942int check_max_decode(enum chipbustype buses, uint32_t size)
943{
944 int limitexceeded = 0;
945 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
946 (max_rom_decode.parallel < size)) {
947 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000948 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000949 "size %u kB of chipset/board/programmer "
950 "for %s interface, "
951 "probe/read/erase/write may fail. ", size / 1024,
952 max_rom_decode.parallel / 1024, "Parallel");
953 }
954 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
955 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000956 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000957 "size %u kB of chipset/board/programmer "
958 "for %s interface, "
959 "probe/read/erase/write may fail. ", size / 1024,
960 max_rom_decode.lpc / 1024, "LPC");
961 }
962 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
963 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000964 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000965 "size %u kB of chipset/board/programmer "
966 "for %s interface, "
967 "probe/read/erase/write may fail. ", size / 1024,
968 max_rom_decode.fwh / 1024, "FWH");
969 }
970 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
971 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000972 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000973 "size %u kB of chipset/board/programmer "
974 "for %s interface, "
975 "probe/read/erase/write may fail. ", size / 1024,
976 max_rom_decode.spi / 1024, "SPI");
977 }
978 if (!limitexceeded)
979 return 0;
980 /* Sometimes chip and programmer have more than one bus in common,
981 * and the limit is not exceeded on all buses. Tell the user.
982 */
983 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000984 /* FIXME: This message is designed towards CLI users. */
Sean Nelson316a29f2010-05-07 20:09:04 +0000985 msg_pdbg("There is at least one common chip/programmer "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000986 "interface which can support a chip of this size. "
987 "You can try --force at your own risk.\n");
988 return 1;
989}
990
Peter Stuge483b8f02008-09-03 23:10:05 +0000991struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000992{
Peter Stuge483b8f02008-09-03 23:10:05 +0000993 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000994 unsigned long base = 0;
995 uint32_t size;
996 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000997 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000998
Peter Stuge483b8f02008-09-03 23:10:05 +0000999 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001000 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +00001001 continue;
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001002 msg_gdbg("Probing for %s %s, %d KB: ",
Stefan Reinauerac378972008-03-17 22:59:40 +00001003 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001004 if (!flash->probe && !force) {
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001005 msg_gdbg("failed! flashrom has no probe function for "
1006 "this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +00001007 continue;
1008 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001009 buses_common = buses_supported & flash->bustype;
1010 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001011 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001012 msg_gdbg("skipped.");
1013 msg_gspew(" Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001014 free(tmp);
1015 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001016 msg_gspew("and chip bus type %s are incompatible.",
1017 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001018 free(tmp);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001019 msg_gdbg("\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001020 continue;
1021 }
Stefan Reinauer70385642007-04-06 11:58:03 +00001022
Ollie Lhocbbf1252004-03-17 22:22:08 +00001023 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001024 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +00001025
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +00001026 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +00001027 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001028
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001029 if (force)
1030 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001031
Peter Stuge483b8f02008-09-03 23:10:05 +00001032 if (flash->probe(flash) != 1)
1033 goto notfound;
1034
Uwe Hermann394131e2008-10-18 21:14:13 +00001035 if (first_flash == flashchips
1036 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001037 break;
1038
Peter Stuge483b8f02008-09-03 23:10:05 +00001039notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +00001040 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001041 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001042
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001043 if (!flash || !flash->name)
1044 return NULL;
1045
Sean Nelson316a29f2010-05-07 20:09:04 +00001046 msg_cinfo("%s chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +00001047 force ? "Assuming" : "Found",
Uwe Hermann9899cad2009-06-28 21:47:57 +00001048 flash->vendor, flash->name, flash->total_size,
1049 flashbuses_to_text(flash->bustype), base);
1050
Sean Nelson6e0b9122010-02-19 00:52:10 +00001051 if (flash->printlock)
1052 flash->printlock(flash);
1053
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001054 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001055}
1056
Stefan Reinauere3705282005-12-18 16:41:10 +00001057int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001058{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001059 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +00001060 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001061
Sean Nelson316a29f2010-05-07 20:09:04 +00001062 msg_cinfo("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +00001063
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001064 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001065
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001066 if (!ret)
Sean Nelson316a29f2010-05-07 20:09:04 +00001067 msg_cinfo("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001068
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001069 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001070}
1071
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001072int write_buf_to_file(unsigned char *buf, unsigned long size, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001073{
1074 unsigned long numbytes;
1075 FILE *image;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001076
1077 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001078 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001079 return 1;
1080 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001081 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001082 perror(filename);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001083 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001084 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001085
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001086 numbytes = fwrite(buf, 1, size, image);
1087 fclose(image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001088 if (numbytes != size) {
1089 msg_gerr("File %s could not be written completely.\n",
1090 filename);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001091 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001092 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001093 return 0;
1094}
1095
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001096int read_flash_to_file(struct flashchip *flash, char *filename)
1097{
1098 unsigned long size = flash->total_size * 1024;
1099 unsigned char *buf = calloc(size, sizeof(char));
1100 int ret = 0;
1101
1102 msg_cinfo("Reading flash... ");
1103 if (!buf) {
1104 msg_gerr("Memory allocation failed!\n");
1105 msg_cinfo("FAILED.\n");
1106 return 1;
1107 }
1108 if (!flash->read) {
1109 msg_cerr("No read function available for this flash chip.\n");
1110 ret = 1;
1111 goto out_free;
1112 }
1113 if (flash->read(flash, buf, 0, size)) {
1114 msg_cerr("Read operation failed!\n");
1115 ret = 1;
1116 goto out_free;
1117 }
1118
1119 ret = write_buf_to_file(buf, flash->total_size * 1024, filename);
1120out_free:
1121 free(buf);
1122 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1123 return ret;
1124}
1125
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001126/* This function shares a lot of its structure with erase_flash().
1127 * Even if an error is found, the function will keep going and check the rest.
1128 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001129static int selfcheck_eraseblocks(struct flashchip *flash)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001130{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001131 int i, j, k;
1132 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001133
1134 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1135 unsigned int done = 0;
1136 struct block_eraser eraser = flash->block_erasers[k];
1137
1138 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1139 /* Blocks with zero size are bugs in flashchips.c. */
1140 if (eraser.eraseblocks[i].count &&
1141 !eraser.eraseblocks[i].size) {
1142 msg_gerr("ERROR: Flash chip %s erase function "
1143 "%i region %i has size 0. Please report"
1144 " a bug at flashrom@flashrom.org\n",
1145 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001146 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001147 }
1148 /* Blocks with zero count are bugs in flashchips.c. */
1149 if (!eraser.eraseblocks[i].count &&
1150 eraser.eraseblocks[i].size) {
1151 msg_gerr("ERROR: Flash chip %s erase function "
1152 "%i region %i has count 0. Please report"
1153 " a bug at flashrom@flashrom.org\n",
1154 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001155 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001156 }
1157 done += eraser.eraseblocks[i].count *
1158 eraser.eraseblocks[i].size;
1159 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001160 /* Empty eraseblock definition with erase function. */
1161 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001162 msg_gspew("Strange: Empty eraseblock definition with "
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001163 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001164 if (!done)
1165 continue;
1166 if (done != flash->total_size * 1024) {
1167 msg_gerr("ERROR: Flash chip %s erase function %i "
1168 "region walking resulted in 0x%06x bytes total,"
1169 " expected 0x%06x bytes. Please report a bug at"
1170 " flashrom@flashrom.org\n", flash->name, k,
1171 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001172 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001173 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001174 if (!eraser.block_erase)
1175 continue;
1176 /* Check if there are identical erase functions for different
1177 * layouts. That would imply "magic" erase functions. The
1178 * easiest way to check this is with function pointers.
1179 */
Uwe Hermann43959702010-03-13 17:28:29 +00001180 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001181 if (eraser.block_erase ==
1182 flash->block_erasers[j].block_erase) {
1183 msg_gerr("ERROR: Flash chip %s erase function "
1184 "%i and %i are identical. Please report"
1185 " a bug at flashrom@flashrom.org\n",
1186 flash->name, k, j);
1187 ret = 1;
1188 }
Uwe Hermann43959702010-03-13 17:28:29 +00001189 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001190 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001191 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001192}
1193
Carl-Daniel Hailfinger4d3e9ca2010-07-13 00:42:00 +00001194static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1195 int (*do_something) (struct flashchip *flash,
1196 unsigned int addr,
1197 unsigned int len))
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001198{
1199 int i, j;
1200 unsigned int start = 0;
1201 unsigned int len;
1202 struct block_eraser eraser = flash->block_erasers[erasefunction];
1203 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1204 /* count==0 for all automatically initialized array
1205 * members so the loop below won't be executed for them.
1206 */
1207 len = eraser.eraseblocks[i].size;
1208 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
1209 msg_cdbg("0x%06x-0x%06x, ", start,
1210 start + len - 1);
1211 if (do_something(flash, start, len))
1212 return 1;
1213 start += len;
1214 }
1215 }
1216 return 0;
1217}
1218
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001219int erase_flash(struct flashchip *flash)
1220{
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001221 int k, ret = 0, found = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001222
Sean Nelson316a29f2010-05-07 20:09:04 +00001223 msg_cinfo("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001224 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001225 struct block_eraser eraser = flash->block_erasers[k];
1226
Sean Nelson316a29f2010-05-07 20:09:04 +00001227 msg_cdbg("Looking at blockwise erase function %i... ", k);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001228 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001229 msg_cdbg("not defined. "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001230 "Looking for another erase function.\n");
1231 continue;
1232 }
1233 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001234 msg_cdbg("eraseblock layout is known, but no "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001235 "matching block erase function found. "
1236 "Looking for another erase function.\n");
1237 continue;
1238 }
1239 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001240 msg_cdbg("block erase function found, but "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001241 "eraseblock layout is unknown. "
1242 "Looking for another erase function.\n");
1243 continue;
1244 }
1245 found = 1;
Sean Nelson316a29f2010-05-07 20:09:04 +00001246 msg_cdbg("trying... ");
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001247 ret = walk_eraseregions(flash, k, eraser.block_erase);
Sean Nelson316a29f2010-05-07 20:09:04 +00001248 msg_cdbg("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001249 /* If everything is OK, don't try another erase function. */
1250 if (!ret)
1251 break;
1252 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001253 if (!found) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001254 msg_cerr("ERROR: flashrom has no erase function for this flash chip.\n");
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001255 return 1;
1256 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001257
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001258 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001259 msg_cerr("FAILED!\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001260 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001261 msg_cinfo("SUCCESS.\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001262 }
1263 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001264}
1265
Uwe Hermannc67d0372009-10-01 18:40:02 +00001266void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001267{
Sean Nelson316a29f2010-05-07 20:09:04 +00001268 msg_gerr("Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001269 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001270 "mail flashrom@flashrom.org!\n"
1271 "-------------------------------------------------------------"
1272 "------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001273 "DO NOT REBOOT OR POWEROFF!\n");
1274}
1275
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001276/* The way to go if you want a delimited list of programmers*/
1277void list_programmers(char *delim)
1278{
1279 enum programmer p;
1280 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001281 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001282 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001283 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001284 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001285 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001286}
1287
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001288void print_sysinfo(void)
1289{
1290#if HAVE_UTSNAME == 1
1291 struct utsname osinfo;
1292 uname(&osinfo);
1293
1294 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1295 osinfo.machine);
1296#else
1297 msg_ginfo(" on unknown machine");
1298#endif
1299 msg_ginfo(", built with");
1300#if NEED_PCI == 1
1301#ifdef PCILIB_VERSION
1302 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1303#else
1304 msg_ginfo(" unknown PCI library,");
1305#endif
1306#endif
1307#ifdef __clang__
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001308 msg_ginfo(" LLVM Clang");
1309#ifdef __clang_version__
1310 msg_ginfo(" %s,", __clang_version__);
1311#else
1312 msg_ginfo(" unknown version (before r102686),");
1313#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001314#elif defined(__GNUC__)
1315 msg_ginfo(" GCC");
1316#ifdef __VERSION__
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001317 msg_ginfo(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001318#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001319 msg_ginfo(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001320#endif
1321#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001322 msg_ginfo(" unknown compiler,");
1323#endif
1324#if defined (__FLASHROM_LITTLE_ENDIAN__)
1325 msg_ginfo(" little endian");
1326#else
1327 msg_ginfo(" big endian");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001328#endif
1329 msg_ginfo("\n");
1330}
1331
Bernhard Walle201bde32008-01-21 15:24:22 +00001332void print_version(void)
1333{
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001334 msg_ginfo("flashrom v%s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001335 print_sysinfo();
Bernhard Walle201bde32008-01-21 15:24:22 +00001336}
1337
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001338void print_banner(void)
1339{
1340 msg_ginfo("flashrom is free software, get the source code at "
1341 "http://www.flashrom.org\n");
1342 msg_ginfo("\n");
1343}
1344
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001345int selfcheck(void)
1346{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001347 int ret = 0;
1348 struct flashchip *flash;
1349
1350 /* Safety check. Instead of aborting after the first error, check
1351 * if more errors exist.
1352 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001353 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001354 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001355 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001356 }
1357 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001358 msg_gerr("SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001359 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001360 }
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001361#if CONFIG_BITBANG_SPI == 1
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001362 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001363 msg_gerr("Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001364 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001365 }
1366#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001367 for (flash = flashchips; flash && flash->name; flash++)
1368 if (selfcheck_eraseblocks(flash))
1369 ret = 1;
1370 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001371}
1372
1373void check_chip_supported(struct flashchip *flash)
1374{
1375 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001376 msg_cinfo("===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001377 if (flash->tested & TEST_BAD_MASK) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001378 msg_cinfo("This flash part has status NOT WORKING for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001379 if (flash->tested & TEST_BAD_PROBE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001380 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001381 if (flash->tested & TEST_BAD_READ)
Sean Nelson316a29f2010-05-07 20:09:04 +00001382 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001383 if (flash->tested & TEST_BAD_ERASE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001384 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001385 if (flash->tested & TEST_BAD_WRITE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001386 msg_cinfo(" WRITE");
1387 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001388 }
1389 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1390 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1391 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1392 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001393 msg_cinfo("This flash part has status UNTESTED for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001394 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001395 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001396 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
Sean Nelson316a29f2010-05-07 20:09:04 +00001397 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001398 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001399 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001400 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001401 msg_cinfo(" WRITE");
1402 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001403 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001404 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001405 msg_cinfo("The test status of this chip may have been updated "
1406 "in the latest development\n"
1407 "version of flashrom. If you are running the latest "
1408 "development version,\n"
1409 "please email a report to flashrom@flashrom.org if "
1410 "any of the above operations\n"
1411 "work correctly for you with this flash part. Please "
1412 "include the flashrom\n"
1413 "output with the additional -V option for all "
1414 "operations you tested (-V, -Vr,\n"
1415 "-Vw, -VE), and mention which mainboard or "
1416 "programmer you tested.\n"
1417 "Thanks for your help!\n"
1418 "===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001419 }
1420}
1421
Ollie Lho761bf1b2004-03-20 16:46:10 +00001422int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001423{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001424 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001425}
1426
1427/* This function signature is horrible. We need to design a better interface,
1428 * but right now it allows us to split off the CLI code.
1429 */
1430int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1431{
1432 uint8_t *buf;
1433 unsigned long numbytes;
1434 FILE *image;
1435 int ret = 0;
1436 unsigned long size;
1437
1438 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001439 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001440
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +00001441 if (!programmer_may_write && (write_it || erase_it)) {
1442 msg_perr("Write/erase is not working yet on your programmer in "
1443 "its current configuration.\n");
1444 /* --force is the wrong approach, but it's the best we can do
1445 * until the generic programmer parameter parser is merged.
1446 */
1447 if (!force) {
1448 msg_perr("Aborting.\n");
1449 programmer_shutdown();
1450 return 1;
1451 } else {
1452 msg_cerr("Continuing anyway.\n");
1453 }
1454 }
1455
Ollie Lhoefa28582004-12-08 20:10:01 +00001456 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001457 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001458 msg_cerr("Erase is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001459 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001460 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001461 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001462 return 1;
1463 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001464 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001465 }
1466 }
Sean Nelson6e0b9122010-02-19 00:52:10 +00001467 if (flash->unlock)
1468 flash->unlock(flash);
1469
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001470 if (erase_flash(flash)) {
1471 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001472 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001473 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001474 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001475 } else if (read_it) {
Sean Nelson6e0b9122010-02-19 00:52:10 +00001476 if (flash->unlock)
1477 flash->unlock(flash);
1478
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001479 if (read_flash_to_file(flash, filename)) {
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001480 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001481 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001482 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001483 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001484 struct stat image_stat;
1485
Sean Nelson6e0b9122010-02-19 00:52:10 +00001486 if (flash->unlock)
1487 flash->unlock(flash);
1488
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001489 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001490 msg_cerr("Erase is not working on this chip "
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001491 "and erase is needed for write. ");
1492 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001493 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001494 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001495 return 1;
1496 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001497 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001498 }
1499 }
1500 if (flash->tested & TEST_BAD_WRITE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001501 msg_cerr("Write is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001502 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001503 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001504 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001505 return 1;
1506 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001507 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001508 }
1509 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001510 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001511 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001512 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001513 exit(1);
1514 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001515 if (fstat(fileno(image), &image_stat) != 0) {
1516 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001517 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001518 exit(1);
1519 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001520 if (image_stat.st_size != flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001521 msg_gerr("Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001522 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001523 exit(1);
1524 }
1525
Peter Stuge1fec0f32009-01-12 21:00:35 +00001526 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001527#if CONFIG_INTERNAL == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001528 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001529#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001530 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001531 if (numbytes != size) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001532 msg_gerr("Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001533 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001534 return 1;
1535 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001536 }
1537
Ollie Lho184a4042005-11-26 21:55:36 +00001538 // This should be moved into each flash part's code to do it
1539 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001540 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001541
Ollie Lho184a4042005-11-26 21:55:36 +00001542 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001543
Peter Stugef31104c2008-04-28 14:47:30 +00001544 if (write_it) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001545 msg_cinfo("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001546 if (!flash->write) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001547 msg_cerr("Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001548 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001549 return 1;
1550 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001551 ret = flash->write(flash, buf);
1552 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001553 msg_cerr("FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001554 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001555 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001556 return 1;
1557 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001558 msg_cinfo("COMPLETE.\n");
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001559 }
Peter Stugef31104c2008-04-28 14:47:30 +00001560 }
Ollie Lho184a4042005-11-26 21:55:36 +00001561
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001562 if (verify_it) {
1563 /* Work around chips which need some time to calm down. */
1564 if (write_it)
1565 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001566 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001567 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001568 * might have an emergency situation.
1569 */
1570 if (ret && write_it)
1571 emergency_help_message();
1572 }
Ollie Lho184a4042005-11-26 21:55:36 +00001573
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001574 programmer_shutdown();
1575
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001576 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001577}