blob: f72202901897b088442f6163d543bda53f5933ff [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"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000036#include "programmer.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000037
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000038const char * const flashrom_version = FLASHROM_VERSION;
Ronald G. Minnichceec4202003-07-25 04:37:41 +000039char *chip_to_probe = NULL;
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000040int verbose = 0;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000041
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000042#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +000043enum programmer programmer = PROGRAMMER_INTERNAL;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000044#elif CONFIG_DUMMY == 1
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000045enum programmer programmer = PROGRAMMER_DUMMY;
46#else
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000047/* If neither internal nor dummy are selected, we must pick a sensible default.
48 * Since there is no reason to prefer a particular external programmer, we fail
49 * if more than one of them is selected. If only one is selected, it is clear
50 * that the user wants that one to become the default.
51 */
Idwer Vollering004f4b72010-09-03 18:21:21 +000052#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+CONFIG_RAYER_SPI+CONFIG_NICINTEL_SPI > 1
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000053#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 +000054#endif
55enum programmer programmer =
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000056#if CONFIG_NIC3COM == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000057 PROGRAMMER_NIC3COM
58#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000059#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +000060 PROGRAMMER_NICREALTEK
61 PROGRAMMER_NICREALTEK2
62#endif
Andrew Morganc29c2e72010-06-07 22:37:54 +000063#if CONFIG_NICNATSEMI == 1
64 PROGRAMMER_NICNATSEMI
65#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000066#if CONFIG_GFXNVIDIA == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000067 PROGRAMMER_GFXNVIDIA
68#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000069#if CONFIG_DRKAISER == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000070 PROGRAMMER_DRKAISER
71#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000072#if CONFIG_SATASII == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000073 PROGRAMMER_SATASII
74#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000075#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +000076 PROGRAMMER_ATAHPT
77#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000078#if CONFIG_FT2232_SPI == 1
79 PROGRAMMER_FT2232_SPI
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000080#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000081#if CONFIG_SERPROG == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000082 PROGRAMMER_SERPROG
83#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000084#if CONFIG_BUSPIRATE_SPI == 1
85 PROGRAMMER_BUSPIRATE_SPI
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000086#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000087#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000088 PROGRAMMER_DEDIPROG
89#endif
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +000090#if CONFIG_RAYER_SPI == 1
91 PROGRAMMER_RAYER_SPI
92#endif
Idwer Vollering004f4b72010-09-03 18:21:21 +000093#if CONFIG_NICINTEL_SPI == 1
94 PROGRAMMER_NICINTEL_SPI
95#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000096;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000097#endif
98
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +000099static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +0000100
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000101/* Supported buses for the current programmer. */
102enum chipbustype buses_supported;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000103
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000104/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000105 * Programmers supporting multiple buses can have differing size limits on
106 * each bus. Store the limits for each bus in a common struct.
107 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000108struct decode_sizes max_rom_decode;
109
110/* If nonzero, used as the start address of bottom-aligned flash. */
111unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000112
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000113/* Is writing allowed with this programmer? */
114int programmer_may_write;
115
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000116const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000117#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000118 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000119 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000120 .init = internal_init,
121 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000122 .map_flash_region = physmap,
123 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000124 .chip_readb = internal_chip_readb,
125 .chip_readw = internal_chip_readw,
126 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000127 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000128 .chip_writeb = internal_chip_writeb,
129 .chip_writew = internal_chip_writew,
130 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000131 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000132 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000133 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000134#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000135
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000136#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000137 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000138 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000139 .init = dummy_init,
140 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000141 .map_flash_region = dummy_map,
142 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000143 .chip_readb = dummy_chip_readb,
144 .chip_readw = dummy_chip_readw,
145 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000146 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000147 .chip_writeb = dummy_chip_writeb,
148 .chip_writew = dummy_chip_writew,
149 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000150 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000151 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000152 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000153#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000154
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000155#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000156 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000157 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000158 .init = nic3com_init,
159 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000160 .map_flash_region = fallback_map,
161 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000162 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000163 .chip_readw = fallback_chip_readw,
164 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000165 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000166 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000167 .chip_writew = fallback_chip_writew,
168 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000169 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000170 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000171 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000172#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000173
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000174#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000175 {
176 .name = "nicrealtek",
177 .init = nicrealtek_init,
178 .shutdown = nicrealtek_shutdown,
179 .map_flash_region = fallback_map,
180 .unmap_flash_region = fallback_unmap,
181 .chip_readb = nicrealtek_chip_readb,
182 .chip_readw = fallback_chip_readw,
183 .chip_readl = fallback_chip_readl,
184 .chip_readn = fallback_chip_readn,
185 .chip_writeb = nicrealtek_chip_writeb,
186 .chip_writew = fallback_chip_writew,
187 .chip_writel = fallback_chip_writel,
188 .chip_writen = fallback_chip_writen,
189 .delay = internal_delay,
190 },
191 {
192 .name = "nicsmc1211",
193 .init = nicsmc1211_init,
194 .shutdown = nicrealtek_shutdown,
195 .map_flash_region = fallback_map,
196 .unmap_flash_region = fallback_unmap,
197 .chip_readb = nicrealtek_chip_readb,
198 .chip_readw = fallback_chip_readw,
199 .chip_readl = fallback_chip_readl,
200 .chip_readn = fallback_chip_readn,
201 .chip_writeb = nicrealtek_chip_writeb,
202 .chip_writew = fallback_chip_writew,
203 .chip_writel = fallback_chip_writel,
204 .chip_writen = fallback_chip_writen,
205 .delay = internal_delay,
206 },
207#endif
208
Andrew Morganc29c2e72010-06-07 22:37:54 +0000209#if CONFIG_NICNATSEMI == 1
210 {
211 .name = "nicnatsemi",
212 .init = nicnatsemi_init,
213 .shutdown = nicnatsemi_shutdown,
214 .map_flash_region = fallback_map,
215 .unmap_flash_region = fallback_unmap,
216 .chip_readb = nicnatsemi_chip_readb,
217 .chip_readw = fallback_chip_readw,
218 .chip_readl = fallback_chip_readl,
219 .chip_readn = fallback_chip_readn,
220 .chip_writeb = nicnatsemi_chip_writeb,
221 .chip_writew = fallback_chip_writew,
222 .chip_writel = fallback_chip_writel,
223 .chip_writen = fallback_chip_writen,
224 .delay = internal_delay,
225 },
226#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000227
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000228#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000229 {
230 .name = "gfxnvidia",
231 .init = gfxnvidia_init,
232 .shutdown = gfxnvidia_shutdown,
233 .map_flash_region = fallback_map,
234 .unmap_flash_region = fallback_unmap,
235 .chip_readb = gfxnvidia_chip_readb,
236 .chip_readw = fallback_chip_readw,
237 .chip_readl = fallback_chip_readl,
238 .chip_readn = fallback_chip_readn,
239 .chip_writeb = gfxnvidia_chip_writeb,
240 .chip_writew = fallback_chip_writew,
241 .chip_writel = fallback_chip_writel,
242 .chip_writen = fallback_chip_writen,
243 .delay = internal_delay,
244 },
245#endif
246
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000247#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000248 {
TURBO Jb0912c02009-09-02 23:00:46 +0000249 .name = "drkaiser",
250 .init = drkaiser_init,
251 .shutdown = drkaiser_shutdown,
252 .map_flash_region = fallback_map,
253 .unmap_flash_region = fallback_unmap,
254 .chip_readb = drkaiser_chip_readb,
255 .chip_readw = fallback_chip_readw,
256 .chip_readl = fallback_chip_readl,
257 .chip_readn = fallback_chip_readn,
258 .chip_writeb = drkaiser_chip_writeb,
259 .chip_writew = fallback_chip_writew,
260 .chip_writel = fallback_chip_writel,
261 .chip_writen = fallback_chip_writen,
262 .delay = internal_delay,
263 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000264#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000265
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000266#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000267 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000268 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000269 .init = satasii_init,
270 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000271 .map_flash_region = fallback_map,
272 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000273 .chip_readb = satasii_chip_readb,
274 .chip_readw = fallback_chip_readw,
275 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000276 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000277 .chip_writeb = satasii_chip_writeb,
278 .chip_writew = fallback_chip_writew,
279 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000280 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000281 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000282 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000283#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000284
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000285#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000286 {
287 .name = "atahpt",
288 .init = atahpt_init,
289 .shutdown = atahpt_shutdown,
290 .map_flash_region = fallback_map,
291 .unmap_flash_region = fallback_unmap,
292 .chip_readb = atahpt_chip_readb,
293 .chip_readw = fallback_chip_readw,
294 .chip_readl = fallback_chip_readl,
295 .chip_readn = fallback_chip_readn,
296 .chip_writeb = atahpt_chip_writeb,
297 .chip_writew = fallback_chip_writew,
298 .chip_writel = fallback_chip_writel,
299 .chip_writen = fallback_chip_writen,
300 .delay = internal_delay,
301 },
302#endif
303
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000304#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000305#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000306 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000307 .name = "it87spi",
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000308 .init = it87spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000309 .shutdown = noop_shutdown,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000310 .map_flash_region = fallback_map,
311 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000312 .chip_readb = noop_chip_readb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000313 .chip_readw = fallback_chip_readw,
314 .chip_readl = fallback_chip_readl,
315 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000316 .chip_writeb = noop_chip_writeb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000317 .chip_writew = fallback_chip_writew,
318 .chip_writel = fallback_chip_writel,
319 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000320 .delay = internal_delay,
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000321 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000322#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000323#endif
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000324
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000325#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000326 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000327 .name = "ft2232_spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000328 .init = ft2232_spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000329 .shutdown = noop_shutdown, /* Missing shutdown */
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000330 .map_flash_region = fallback_map,
331 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000332 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000333 .chip_readw = fallback_chip_readw,
334 .chip_readl = fallback_chip_readl,
335 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000336 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000337 .chip_writew = fallback_chip_writew,
338 .chip_writel = fallback_chip_writel,
339 .chip_writen = fallback_chip_writen,
340 .delay = internal_delay,
341 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000342#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000343
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000344#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000345 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000346 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000347 .init = serprog_init,
348 .shutdown = serprog_shutdown,
349 .map_flash_region = fallback_map,
350 .unmap_flash_region = fallback_unmap,
351 .chip_readb = serprog_chip_readb,
352 .chip_readw = fallback_chip_readw,
353 .chip_readl = fallback_chip_readl,
354 .chip_readn = serprog_chip_readn,
355 .chip_writeb = serprog_chip_writeb,
356 .chip_writew = fallback_chip_writew,
357 .chip_writel = fallback_chip_writel,
358 .chip_writen = fallback_chip_writen,
359 .delay = serprog_delay,
360 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000361#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000362
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000363#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000364 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000365 .name = "buspirate_spi",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000366 .init = buspirate_spi_init,
367 .shutdown = buspirate_spi_shutdown,
368 .map_flash_region = fallback_map,
369 .unmap_flash_region = fallback_unmap,
370 .chip_readb = noop_chip_readb,
371 .chip_readw = fallback_chip_readw,
372 .chip_readl = fallback_chip_readl,
373 .chip_readn = fallback_chip_readn,
374 .chip_writeb = noop_chip_writeb,
375 .chip_writew = fallback_chip_writew,
376 .chip_writel = fallback_chip_writel,
377 .chip_writen = fallback_chip_writen,
378 .delay = internal_delay,
379 },
380#endif
381
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000382#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000383 {
384 .name = "dediprog",
385 .init = dediprog_init,
386 .shutdown = dediprog_shutdown,
387 .map_flash_region = fallback_map,
388 .unmap_flash_region = fallback_unmap,
389 .chip_readb = noop_chip_readb,
390 .chip_readw = fallback_chip_readw,
391 .chip_readl = fallback_chip_readl,
392 .chip_readn = fallback_chip_readn,
393 .chip_writeb = noop_chip_writeb,
394 .chip_writew = fallback_chip_writew,
395 .chip_writel = fallback_chip_writel,
396 .chip_writen = fallback_chip_writen,
397 .delay = internal_delay,
398 },
399#endif
400
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000401#if CONFIG_RAYER_SPI == 1
402 {
403 .name = "rayer_spi",
404 .init = rayer_spi_init,
405 .shutdown = noop_shutdown,
406 .map_flash_region = fallback_map,
407 .unmap_flash_region = fallback_unmap,
408 .chip_readb = noop_chip_readb,
409 .chip_readw = fallback_chip_readw,
410 .chip_readl = fallback_chip_readl,
411 .chip_readn = fallback_chip_readn,
412 .chip_writeb = noop_chip_writeb,
413 .chip_writew = fallback_chip_writew,
414 .chip_writel = fallback_chip_writel,
415 .chip_writen = fallback_chip_writen,
416 .delay = internal_delay,
417 },
418#endif
419
Idwer Vollering004f4b72010-09-03 18:21:21 +0000420#if CONFIG_NICINTEL_SPI == 1
421 {
422 .name = "nicintel_spi",
423 .init = nicintel_spi_init,
424 .shutdown = nicintel_spi_shutdown,
425 .map_flash_region = fallback_map,
426 .unmap_flash_region = fallback_unmap,
427 .chip_readb = noop_chip_readb,
428 .chip_readw = fallback_chip_readw,
429 .chip_readl = fallback_chip_readl,
430 .chip_readn = fallback_chip_readn,
431 .chip_writeb = noop_chip_writeb,
432 .chip_writew = fallback_chip_writew,
433 .chip_writel = fallback_chip_writel,
434 .chip_writen = fallback_chip_writen,
435 .delay = internal_delay,
436 },
437#endif
438
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000439 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000440};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000441
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000442#define SHUTDOWN_MAXFN 4
443static int shutdown_fn_count = 0;
444struct shutdown_func_data {
445 void (*func) (void *data);
446 void *data;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000447} static shutdown_fn[SHUTDOWN_MAXFN];
448/* Initialize to 0 to make sure nobody registers a shutdown function before
449 * programmer init.
450 */
451static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000452
453/* Register a function to be executed on programmer shutdown.
454 * The advantage over atexit() is that you can supply a void pointer which will
455 * be used as parameter to the registered function upon programmer shutdown.
456 * This pointer can point to arbitrary data used by said function, e.g. undo
457 * information for GPIO settings etc. If unneeded, set data=NULL.
458 * Please note that the first (void *data) belongs to the function signature of
459 * the function passed as first parameter.
460 */
461int register_shutdown(void (*function) (void *data), void *data)
462{
463 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000464 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000465 SHUTDOWN_MAXFN);
466 return 1;
467 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000468 if (!may_register_shutdown) {
469 msg_perr("Tried to register a shutdown function before "
470 "programmer init.\n");
471 return 1;
472 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000473 shutdown_fn[shutdown_fn_count].func = function;
474 shutdown_fn[shutdown_fn_count].data = data;
475 shutdown_fn_count++;
476
477 return 0;
478}
479
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000480int programmer_init(char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000481{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000482 int ret;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000483 /* Initialize all programmer specific data. */
484 /* Default to unlimited decode sizes. */
485 max_rom_decode = (const struct decode_sizes) {
486 .parallel = 0xffffffff,
487 .lpc = 0xffffffff,
488 .fwh = 0xffffffff,
489 .spi = 0xffffffff
490 };
491 /* Default to Parallel/LPC/FWH flash devices. If a known host controller
492 * is found, the init routine sets the buses_supported bitfield.
493 */
494 buses_supported = CHIP_BUSTYPE_NONSPI;
495 /* Default to top aligned flash at 4 GB. */
496 flashbase = 0;
497 /* Registering shutdown functions is now allowed. */
498 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000499 /* Default to allowing writes. Broken programmers set this to 0. */
500 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000501
502 programmer_param = param;
503 msg_pdbg("Initializing %s programmer\n",
504 programmer_table[programmer].name);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000505 ret = programmer_table[programmer].init();
506 if (programmer_param && strlen(programmer_param)) {
507 msg_perr("Unhandled programmer parameters: %s\n",
508 programmer_param);
509 /* Do not error out here, the init itself was successful. */
510 }
511 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000512}
513
514int programmer_shutdown(void)
515{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000516 /* Registering shutdown functions is no longer allowed. */
517 may_register_shutdown = 0;
518 while (shutdown_fn_count > 0) {
519 int i = --shutdown_fn_count;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000520 shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000521 }
Uwe Hermann09e04f72009-05-16 22:36:00 +0000522 return programmer_table[programmer].shutdown();
523}
524
525void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
526 size_t len)
527{
528 return programmer_table[programmer].map_flash_region(descr,
529 phys_addr, len);
530}
531
532void programmer_unmap_flash_region(void *virt_addr, size_t len)
533{
534 programmer_table[programmer].unmap_flash_region(virt_addr, len);
535}
536
537void chip_writeb(uint8_t val, chipaddr addr)
538{
539 programmer_table[programmer].chip_writeb(val, addr);
540}
541
542void chip_writew(uint16_t val, chipaddr addr)
543{
544 programmer_table[programmer].chip_writew(val, addr);
545}
546
547void chip_writel(uint32_t val, chipaddr addr)
548{
549 programmer_table[programmer].chip_writel(val, addr);
550}
551
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000552void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
553{
554 programmer_table[programmer].chip_writen(buf, addr, len);
555}
556
Uwe Hermann09e04f72009-05-16 22:36:00 +0000557uint8_t chip_readb(const chipaddr addr)
558{
559 return programmer_table[programmer].chip_readb(addr);
560}
561
562uint16_t chip_readw(const chipaddr addr)
563{
564 return programmer_table[programmer].chip_readw(addr);
565}
566
567uint32_t chip_readl(const chipaddr addr)
568{
569 return programmer_table[programmer].chip_readl(addr);
570}
571
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000572void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
573{
574 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000575}
576
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000577void programmer_delay(int usecs)
578{
579 programmer_table[programmer].delay(usecs);
580}
581
Peter Stuge776d2022009-01-26 00:39:57 +0000582void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000583{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000584 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000585 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000586 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000587 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000588}
589
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000590int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000591{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000592 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000593
594 return 0;
595}
596
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000597int min(int a, int b)
598{
599 return (a < b) ? a : b;
600}
601
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000602int max(int a, int b)
603{
604 return (a > b) ? a : b;
605}
606
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000607int bitcount(unsigned long a)
608{
609 int i = 0;
610 for (; a != 0; a >>= 1)
611 if (a & 1)
612 i++;
613 return i;
614}
615
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000616char *strcat_realloc(char *dest, const char *src)
617{
618 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000619 if (!dest) {
620 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000621 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000622 }
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000623 strcat(dest, src);
624 return dest;
625}
626
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000627/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000628 * It will look for needle with a subsequent '=' in haystack, return a copy of
629 * needle and remove everything from the first occurrence of needle to the next
630 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000631 */
632char *extract_param(char **haystack, char *needle, char *delim)
633{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000634 char *param_pos, *opt_pos, *rest;
635 char *opt = NULL;
636 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000637 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000638
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000639 needlelen = strlen(needle);
640 if (!needlelen) {
641 msg_gerr("%s: empty needle! Please report a bug at "
642 "flashrom@flashrom.org\n", __func__);
643 return NULL;
644 }
645 /* No programmer parameters given. */
646 if (*haystack == NULL)
647 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000648 param_pos = strstr(*haystack, needle);
649 do {
650 if (!param_pos)
651 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000652 /* Needle followed by '='? */
653 if (param_pos[needlelen] == '=') {
654
655 /* Beginning of the string? */
656 if (param_pos == *haystack)
657 break;
658 /* After a delimiter? */
659 if (strchr(delim, *(param_pos - 1)))
660 break;
661 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000662 /* Continue searching. */
663 param_pos++;
664 param_pos = strstr(param_pos, needle);
665 } while (1);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000666
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000667 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000668 /* Get the string after needle and '='. */
669 opt_pos = param_pos + needlelen + 1;
670 optlen = strcspn(opt_pos, delim);
671 /* Return an empty string if the parameter was empty. */
672 opt = malloc(optlen + 1);
673 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000674 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000675 exit(1);
676 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000677 strncpy(opt, opt_pos, optlen);
678 opt[optlen] = '\0';
679 rest = opt_pos + optlen;
680 /* Skip all delimiters after the current parameter. */
681 rest += strspn(rest, delim);
682 memmove(param_pos, rest, strlen(rest) + 1);
683 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000684 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000685
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000686 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000687}
688
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000689char *extract_programmer_param(char *param_name)
690{
691 return extract_param(&programmer_param, param_name, ",");
692}
693
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000694/* start is an offset to the base address of the flash chip */
695int check_erased_range(struct flashchip *flash, int start, int len)
696{
697 int ret;
698 uint8_t *cmpbuf = malloc(len);
699
700 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000701 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000702 exit(1);
703 }
704 memset(cmpbuf, 0xff, len);
705 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
706 free(cmpbuf);
707 return ret;
708}
709
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000710/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000711 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
712 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000713 * @start offset to the base address of the flash chip
714 * @len length of the verified area
715 * @message string to print in the "FAILED" message
716 * @return 0 for success, -1 for failure
717 */
718int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
719{
720 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000721 int page_size = flash->page_size;
722 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000723 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000724
725 if (!len)
726 goto out_free;
727
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000728 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000729 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000730 return 1;
731 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000732 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000733 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000734 exit(1);
735 }
736
737 if (start + len > flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000738 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000739 " total_size 0x%x\n", __func__, start, len,
740 flash->total_size * 1024);
741 ret = -1;
742 goto out_free;
743 }
744 if (!message)
745 message = "VERIFY";
746
747 /* Warning: This loop has a very unusual condition and body.
748 * The loop needs to go through each page with at least one affected
749 * byte. The lowest page number is (start / page_size) since that
750 * division rounds down. The highest page number we want is the page
751 * where the last byte of the range lives. That last byte has the
752 * address (start + len - 1), thus the highest page number is
753 * (start + len - 1) / page_size. Since we want to include that last
754 * page as well, the loop condition uses <=.
755 */
756 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
757 /* Byte position of the first byte in the range in this page. */
758 starthere = max(start, i * page_size);
759 /* Length of bytes in the range in this page. */
760 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000761 ret = flash->read(flash, readbuf, starthere, lenhere);
762 if (ret) {
763 msg_gerr("Verification impossible because read failed "
764 "at 0x%x (len 0x%x)\n", starthere, lenhere);
765 break;
766 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000767 for (j = 0; j < lenhere; j++) {
768 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000769 /* Only print the first failure. */
770 if (!failcount++)
Sean Nelson316a29f2010-05-07 20:09:04 +0000771 msg_cerr("%s FAILED at 0x%08x! "
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000772 "Expected=0x%02x, Read=0x%02x,",
773 message, starthere + j,
774 cmpbuf[starthere - start + j],
775 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000776 }
777 }
778 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000779 if (failcount) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000780 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000781 start, start + len - 1, failcount);
782 ret = -1;
783 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000784
785out_free:
786 free(readbuf);
787 return ret;
788}
789
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000790/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000791 * Check if the buffer @have can be programmed to the content of @want without
792 * erasing. This is only possible if all chunks of size @gran are either kept
793 * as-is or changed from an all-ones state to any other state.
794 * The following write granularities (enum @gran) are known:
795 * - 1 bit. Each bit can be cleared individually.
796 * - 1 byte. A byte can be written once. Further writes to an already written
797 * byte cause the contents to be either undefined or to stay unchanged.
798 * - 128 bytes. If less than 128 bytes are written, the rest will be
799 * erased. Each write to a 128-byte region will trigger an automatic erase
800 * before anything is written. Very uncommon behaviour and unsupported by
801 * this function.
802 * - 256 bytes. If less than 256 bytes are written, the contents of the
803 * unwritten bytes are undefined.
804 *
805 * @have buffer with current content
806 * @want buffer with desired content
807 * @len length of the verified area
808 * @gran write granularity (enum, not count)
809 * @return 0 if no erase is needed, 1 otherwise
810 */
811int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
812{
813 int result = 0;
814 int i, j, limit;
815
816 switch (gran) {
817 case write_gran_1bit:
818 for (i = 0; i < len; i++)
819 if ((have[i] & want[i]) != want[i]) {
820 result = 1;
821 break;
822 }
823 break;
824 case write_gran_1byte:
825 for (i = 0; i < len; i++)
826 if ((have[i] != want[i]) && (have[i] != 0xff)) {
827 result = 1;
828 break;
829 }
830 break;
831 case write_gran_256bytes:
832 for (j = 0; j < len / 256; j++) {
833 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000834 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000835 if (!memcmp(have + j * 256, want + j * 256, limit))
836 continue;
837 /* have needs to be in erased state. */
838 for (i = 0; i < limit; i++)
839 if (have[i] != 0xff) {
840 result = 1;
841 break;
842 }
843 if (result)
844 break;
845 }
846 break;
847 }
848 return result;
849}
850
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000851/* This function generates various test patterns useful for testing controller
852 * and chip communication as well as chip behaviour.
853 *
854 * If a byte can be written multiple times, each time keeping 0-bits at 0
855 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
856 * is essentially an AND operation. That's also the reason why this function
857 * provides the result of AND between various patterns.
858 *
859 * Below is a list of patterns (and their block length).
860 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
861 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
862 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
863 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
864 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
865 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
866 * Pattern 6 is 00 (1 Byte)
867 * Pattern 7 is ff (1 Byte)
868 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
869 * byte block.
870 *
871 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
872 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
873 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
874 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
875 * Pattern 12 is 00 (1 Byte)
876 * Pattern 13 is ff (1 Byte)
877 * Patterns 8-13 have no block number.
878 *
879 * Patterns 0-3 are created to detect and efficiently diagnose communication
880 * slips like missed bits or bytes and their repetitive nature gives good visual
881 * cues to the person inspecting the results. In addition, the following holds:
882 * AND Pattern 0/1 == Pattern 4
883 * AND Pattern 2/3 == Pattern 5
884 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
885 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
886 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
887 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
888 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
889 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
890 * Besides that, they provide for bit testing of the last two bytes of every
891 * 256 byte block which contains the block number for patterns 0-6.
892 * Patterns 10-11 are special purpose for detecting subblock aliasing with
893 * block sizes >256 bytes (some Dataflash chips etc.)
894 * AND Pattern 8/9 == Pattern 12
895 * AND Pattern 10/11 == Pattern 12
896 * Pattern 13 is the completely erased state.
897 * None of the patterns can detect aliasing at boundaries which are a multiple
898 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
899 */
900int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
901{
902 int i;
903
904 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000905 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000906 return 1;
907 }
908
909 switch (variant) {
910 case 0:
911 for (i = 0; i < size; i++)
912 buf[i] = (i & 0xf) << 4 | 0x5;
913 break;
914 case 1:
915 for (i = 0; i < size; i++)
916 buf[i] = (i & 0xf) << 4 | 0xa;
917 break;
918 case 2:
919 for (i = 0; i < size; i++)
920 buf[i] = 0x50 | (i & 0xf);
921 break;
922 case 3:
923 for (i = 0; i < size; i++)
924 buf[i] = 0xa0 | (i & 0xf);
925 break;
926 case 4:
927 for (i = 0; i < size; i++)
928 buf[i] = (i & 0xf) << 4;
929 break;
930 case 5:
931 for (i = 0; i < size; i++)
932 buf[i] = i & 0xf;
933 break;
934 case 6:
935 memset(buf, 0x00, size);
936 break;
937 case 7:
938 memset(buf, 0xff, size);
939 break;
940 case 8:
941 for (i = 0; i < size; i++)
942 buf[i] = i & 0xff;
943 break;
944 case 9:
945 for (i = 0; i < size; i++)
946 buf[i] = ~(i & 0xff);
947 break;
948 case 10:
949 for (i = 0; i < size % 2; i++) {
950 buf[i * 2] = (i >> 8) & 0xff;
951 buf[i * 2 + 1] = i & 0xff;
952 }
953 if (size & 0x1)
954 buf[i * 2] = (i >> 8) & 0xff;
955 break;
956 case 11:
957 for (i = 0; i < size % 2; i++) {
958 buf[i * 2] = ~((i >> 8) & 0xff);
959 buf[i * 2 + 1] = ~(i & 0xff);
960 }
961 if (size & 0x1)
962 buf[i * 2] = ~((i >> 8) & 0xff);
963 break;
964 case 12:
965 memset(buf, 0x00, size);
966 break;
967 case 13:
968 memset(buf, 0xff, size);
969 break;
970 }
971
972 if ((variant >= 0) && (variant <= 7)) {
973 /* Write block number in the last two bytes of each 256-byte
974 * block, big endian for easier reading of the hexdump.
975 * Note that this wraps around for chips larger than 2^24 bytes
976 * (16 MB).
977 */
978 for (i = 0; i < size / 256; i++) {
979 buf[i * 256 + 254] = (i >> 8) & 0xff;
980 buf[i * 256 + 255] = i & 0xff;
981 }
982 }
983
984 return 0;
985}
986
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000987int check_max_decode(enum chipbustype buses, uint32_t size)
988{
989 int limitexceeded = 0;
990 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
991 (max_rom_decode.parallel < size)) {
992 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000993 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000994 "size %u kB of chipset/board/programmer "
995 "for %s interface, "
996 "probe/read/erase/write may fail. ", size / 1024,
997 max_rom_decode.parallel / 1024, "Parallel");
998 }
999 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
1000 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001001 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001002 "size %u kB of chipset/board/programmer "
1003 "for %s interface, "
1004 "probe/read/erase/write may fail. ", size / 1024,
1005 max_rom_decode.lpc / 1024, "LPC");
1006 }
1007 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
1008 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001009 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001010 "size %u kB of chipset/board/programmer "
1011 "for %s interface, "
1012 "probe/read/erase/write may fail. ", size / 1024,
1013 max_rom_decode.fwh / 1024, "FWH");
1014 }
1015 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
1016 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001017 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001018 "size %u kB of chipset/board/programmer "
1019 "for %s interface, "
1020 "probe/read/erase/write may fail. ", size / 1024,
1021 max_rom_decode.spi / 1024, "SPI");
1022 }
1023 if (!limitexceeded)
1024 return 0;
1025 /* Sometimes chip and programmer have more than one bus in common,
1026 * and the limit is not exceeded on all buses. Tell the user.
1027 */
1028 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001029 /* FIXME: This message is designed towards CLI users. */
Sean Nelson316a29f2010-05-07 20:09:04 +00001030 msg_pdbg("There is at least one common chip/programmer "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001031 "interface which can support a chip of this size. "
1032 "You can try --force at your own risk.\n");
1033 return 1;
1034}
1035
Peter Stuge483b8f02008-09-03 23:10:05 +00001036struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001037{
Peter Stuge483b8f02008-09-03 23:10:05 +00001038 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001039 unsigned long base = 0;
1040 uint32_t size;
1041 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001042 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001043
Peter Stuge483b8f02008-09-03 23:10:05 +00001044 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001045 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +00001046 continue;
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001047 msg_gdbg("Probing for %s %s, %d KB: ",
Stefan Reinauerac378972008-03-17 22:59:40 +00001048 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001049 if (!flash->probe && !force) {
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001050 msg_gdbg("failed! flashrom has no probe function for "
1051 "this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +00001052 continue;
1053 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001054 buses_common = buses_supported & flash->bustype;
1055 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001056 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001057 msg_gdbg("skipped.");
1058 msg_gspew(" Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001059 free(tmp);
1060 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001061 msg_gspew("and chip bus type %s are incompatible.",
1062 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001063 free(tmp);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001064 msg_gdbg("\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001065 continue;
1066 }
Stefan Reinauer70385642007-04-06 11:58:03 +00001067
Ollie Lhocbbf1252004-03-17 22:22:08 +00001068 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001069 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +00001070
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +00001071 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +00001072 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001073
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001074 if (force)
1075 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001076
Peter Stuge483b8f02008-09-03 23:10:05 +00001077 if (flash->probe(flash) != 1)
1078 goto notfound;
1079
Uwe Hermann394131e2008-10-18 21:14:13 +00001080 if (first_flash == flashchips
1081 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001082 break;
1083
Peter Stuge483b8f02008-09-03 23:10:05 +00001084notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +00001085 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001086 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001087
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001088 if (!flash || !flash->name)
1089 return NULL;
1090
Sean Nelson316a29f2010-05-07 20:09:04 +00001091 msg_cinfo("%s chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +00001092 force ? "Assuming" : "Found",
Uwe Hermann9899cad2009-06-28 21:47:57 +00001093 flash->vendor, flash->name, flash->total_size,
1094 flashbuses_to_text(flash->bustype), base);
1095
Sean Nelson6e0b9122010-02-19 00:52:10 +00001096 if (flash->printlock)
1097 flash->printlock(flash);
1098
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001099 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001100}
1101
Stefan Reinauere3705282005-12-18 16:41:10 +00001102int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001103{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001104 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +00001105 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001106
Sean Nelson316a29f2010-05-07 20:09:04 +00001107 msg_cinfo("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +00001108
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001109 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001110
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001111 if (!ret)
Sean Nelson316a29f2010-05-07 20:09:04 +00001112 msg_cinfo("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001113
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001114 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001115}
1116
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001117int write_buf_to_file(unsigned char *buf, unsigned long size, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001118{
1119 unsigned long numbytes;
1120 FILE *image;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001121
1122 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001123 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001124 return 1;
1125 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001126 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001127 perror(filename);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001128 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001129 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001130
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001131 numbytes = fwrite(buf, 1, size, image);
1132 fclose(image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001133 if (numbytes != size) {
1134 msg_gerr("File %s could not be written completely.\n",
1135 filename);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001136 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001137 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001138 return 0;
1139}
1140
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001141int read_flash_to_file(struct flashchip *flash, char *filename)
1142{
1143 unsigned long size = flash->total_size * 1024;
1144 unsigned char *buf = calloc(size, sizeof(char));
1145 int ret = 0;
1146
1147 msg_cinfo("Reading flash... ");
1148 if (!buf) {
1149 msg_gerr("Memory allocation failed!\n");
1150 msg_cinfo("FAILED.\n");
1151 return 1;
1152 }
1153 if (!flash->read) {
1154 msg_cerr("No read function available for this flash chip.\n");
1155 ret = 1;
1156 goto out_free;
1157 }
1158 if (flash->read(flash, buf, 0, size)) {
1159 msg_cerr("Read operation failed!\n");
1160 ret = 1;
1161 goto out_free;
1162 }
1163
1164 ret = write_buf_to_file(buf, flash->total_size * 1024, filename);
1165out_free:
1166 free(buf);
1167 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1168 return ret;
1169}
1170
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001171/* This function shares a lot of its structure with erase_flash().
1172 * Even if an error is found, the function will keep going and check the rest.
1173 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001174static int selfcheck_eraseblocks(struct flashchip *flash)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001175{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001176 int i, j, k;
1177 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001178
1179 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1180 unsigned int done = 0;
1181 struct block_eraser eraser = flash->block_erasers[k];
1182
1183 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1184 /* Blocks with zero size are bugs in flashchips.c. */
1185 if (eraser.eraseblocks[i].count &&
1186 !eraser.eraseblocks[i].size) {
1187 msg_gerr("ERROR: Flash chip %s erase function "
1188 "%i region %i has size 0. Please report"
1189 " a bug at flashrom@flashrom.org\n",
1190 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001191 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001192 }
1193 /* Blocks with zero count are bugs in flashchips.c. */
1194 if (!eraser.eraseblocks[i].count &&
1195 eraser.eraseblocks[i].size) {
1196 msg_gerr("ERROR: Flash chip %s erase function "
1197 "%i region %i has count 0. Please report"
1198 " a bug at flashrom@flashrom.org\n",
1199 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001200 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001201 }
1202 done += eraser.eraseblocks[i].count *
1203 eraser.eraseblocks[i].size;
1204 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001205 /* Empty eraseblock definition with erase function. */
1206 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001207 msg_gspew("Strange: Empty eraseblock definition with "
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001208 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001209 if (!done)
1210 continue;
1211 if (done != flash->total_size * 1024) {
1212 msg_gerr("ERROR: Flash chip %s erase function %i "
1213 "region walking resulted in 0x%06x bytes total,"
1214 " expected 0x%06x bytes. Please report a bug at"
1215 " flashrom@flashrom.org\n", flash->name, k,
1216 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001217 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001218 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001219 if (!eraser.block_erase)
1220 continue;
1221 /* Check if there are identical erase functions for different
1222 * layouts. That would imply "magic" erase functions. The
1223 * easiest way to check this is with function pointers.
1224 */
Uwe Hermann43959702010-03-13 17:28:29 +00001225 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001226 if (eraser.block_erase ==
1227 flash->block_erasers[j].block_erase) {
1228 msg_gerr("ERROR: Flash chip %s erase function "
1229 "%i and %i are identical. Please report"
1230 " a bug at flashrom@flashrom.org\n",
1231 flash->name, k, j);
1232 ret = 1;
1233 }
Uwe Hermann43959702010-03-13 17:28:29 +00001234 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001235 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001236 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001237}
1238
Carl-Daniel Hailfinger4d3e9ca2010-07-13 00:42:00 +00001239static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1240 int (*do_something) (struct flashchip *flash,
1241 unsigned int addr,
1242 unsigned int len))
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001243{
1244 int i, j;
1245 unsigned int start = 0;
1246 unsigned int len;
1247 struct block_eraser eraser = flash->block_erasers[erasefunction];
1248 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1249 /* count==0 for all automatically initialized array
1250 * members so the loop below won't be executed for them.
1251 */
1252 len = eraser.eraseblocks[i].size;
1253 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
1254 msg_cdbg("0x%06x-0x%06x, ", start,
1255 start + len - 1);
1256 if (do_something(flash, start, len))
1257 return 1;
1258 start += len;
1259 }
1260 }
1261 return 0;
1262}
1263
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001264int erase_flash(struct flashchip *flash)
1265{
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001266 int k, ret = 0, found = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001267
Sean Nelson316a29f2010-05-07 20:09:04 +00001268 msg_cinfo("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001269 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001270 struct block_eraser eraser = flash->block_erasers[k];
1271
Sean Nelson316a29f2010-05-07 20:09:04 +00001272 msg_cdbg("Looking at blockwise erase function %i... ", k);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001273 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001274 msg_cdbg("not defined. "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001275 "Looking for another erase function.\n");
1276 continue;
1277 }
1278 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001279 msg_cdbg("eraseblock layout is known, but no "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001280 "matching block erase function found. "
1281 "Looking for another erase function.\n");
1282 continue;
1283 }
1284 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001285 msg_cdbg("block erase function found, but "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001286 "eraseblock layout is unknown. "
1287 "Looking for another erase function.\n");
1288 continue;
1289 }
1290 found = 1;
Sean Nelson316a29f2010-05-07 20:09:04 +00001291 msg_cdbg("trying... ");
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001292 ret = walk_eraseregions(flash, k, eraser.block_erase);
Sean Nelson316a29f2010-05-07 20:09:04 +00001293 msg_cdbg("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001294 /* If everything is OK, don't try another erase function. */
1295 if (!ret)
1296 break;
1297 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001298 if (!found) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001299 msg_cerr("ERROR: flashrom has no erase function for this flash chip.\n");
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001300 return 1;
1301 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001302
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001303 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001304 msg_cerr("FAILED!\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001305 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001306 msg_cinfo("SUCCESS.\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001307 }
1308 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001309}
1310
Uwe Hermannc67d0372009-10-01 18:40:02 +00001311void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001312{
Sean Nelson316a29f2010-05-07 20:09:04 +00001313 msg_gerr("Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001314 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001315 "mail flashrom@flashrom.org!\n"
1316 "-------------------------------------------------------------"
1317 "------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001318 "DO NOT REBOOT OR POWEROFF!\n");
1319}
1320
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001321/* The way to go if you want a delimited list of programmers*/
1322void list_programmers(char *delim)
1323{
1324 enum programmer p;
1325 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001326 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001327 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001328 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001329 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001330 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001331}
1332
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001333void print_sysinfo(void)
1334{
1335#if HAVE_UTSNAME == 1
1336 struct utsname osinfo;
1337 uname(&osinfo);
1338
1339 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1340 osinfo.machine);
1341#else
1342 msg_ginfo(" on unknown machine");
1343#endif
1344 msg_ginfo(", built with");
1345#if NEED_PCI == 1
1346#ifdef PCILIB_VERSION
1347 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1348#else
1349 msg_ginfo(" unknown PCI library,");
1350#endif
1351#endif
1352#ifdef __clang__
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001353 msg_ginfo(" LLVM Clang");
1354#ifdef __clang_version__
1355 msg_ginfo(" %s,", __clang_version__);
1356#else
1357 msg_ginfo(" unknown version (before r102686),");
1358#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001359#elif defined(__GNUC__)
1360 msg_ginfo(" GCC");
1361#ifdef __VERSION__
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001362 msg_ginfo(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001363#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001364 msg_ginfo(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001365#endif
1366#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001367 msg_ginfo(" unknown compiler,");
1368#endif
1369#if defined (__FLASHROM_LITTLE_ENDIAN__)
1370 msg_ginfo(" little endian");
1371#else
1372 msg_ginfo(" big endian");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001373#endif
1374 msg_ginfo("\n");
1375}
1376
Bernhard Walle201bde32008-01-21 15:24:22 +00001377void print_version(void)
1378{
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001379 msg_ginfo("flashrom v%s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001380 print_sysinfo();
Bernhard Walle201bde32008-01-21 15:24:22 +00001381}
1382
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001383void print_banner(void)
1384{
1385 msg_ginfo("flashrom is free software, get the source code at "
1386 "http://www.flashrom.org\n");
1387 msg_ginfo("\n");
1388}
1389
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001390int selfcheck(void)
1391{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001392 int ret = 0;
1393 struct flashchip *flash;
1394
1395 /* Safety check. Instead of aborting after the first error, check
1396 * if more errors exist.
1397 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001398 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001399 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001400 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001401 }
1402 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001403 msg_gerr("SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001404 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001405 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001406 for (flash = flashchips; flash && flash->name; flash++)
1407 if (selfcheck_eraseblocks(flash))
1408 ret = 1;
1409 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001410}
1411
1412void check_chip_supported(struct flashchip *flash)
1413{
1414 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001415 msg_cinfo("===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001416 if (flash->tested & TEST_BAD_MASK) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001417 msg_cinfo("This flash part has status NOT WORKING for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001418 if (flash->tested & TEST_BAD_PROBE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001419 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001420 if (flash->tested & TEST_BAD_READ)
Sean Nelson316a29f2010-05-07 20:09:04 +00001421 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001422 if (flash->tested & TEST_BAD_ERASE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001423 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001424 if (flash->tested & TEST_BAD_WRITE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001425 msg_cinfo(" WRITE");
1426 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001427 }
1428 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1429 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1430 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1431 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001432 msg_cinfo("This flash part has status UNTESTED for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001433 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001434 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001435 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
Sean Nelson316a29f2010-05-07 20:09:04 +00001436 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001437 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001438 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001439 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001440 msg_cinfo(" WRITE");
1441 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001442 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001443 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001444 msg_cinfo("The test status of this chip may have been updated "
1445 "in the latest development\n"
1446 "version of flashrom. If you are running the latest "
1447 "development version,\n"
1448 "please email a report to flashrom@flashrom.org if "
1449 "any of the above operations\n"
1450 "work correctly for you with this flash part. Please "
1451 "include the flashrom\n"
1452 "output with the additional -V option for all "
1453 "operations you tested (-V, -Vr,\n"
1454 "-Vw, -VE), and mention which mainboard or "
1455 "programmer you tested.\n"
1456 "Thanks for your help!\n"
1457 "===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001458 }
1459}
1460
Ollie Lho761bf1b2004-03-20 16:46:10 +00001461int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001462{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001463 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001464}
1465
1466/* This function signature is horrible. We need to design a better interface,
1467 * but right now it allows us to split off the CLI code.
1468 */
1469int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1470{
1471 uint8_t *buf;
1472 unsigned long numbytes;
1473 FILE *image;
1474 int ret = 0;
1475 unsigned long size;
1476
1477 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001478 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001479
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +00001480 if (!programmer_may_write && (write_it || erase_it)) {
1481 msg_perr("Write/erase is not working yet on your programmer in "
1482 "its current configuration.\n");
1483 /* --force is the wrong approach, but it's the best we can do
1484 * until the generic programmer parameter parser is merged.
1485 */
1486 if (!force) {
1487 msg_perr("Aborting.\n");
1488 programmer_shutdown();
1489 return 1;
1490 } else {
1491 msg_cerr("Continuing anyway.\n");
1492 }
1493 }
1494
Ollie Lhoefa28582004-12-08 20:10:01 +00001495 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001496 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001497 msg_cerr("Erase is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001498 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001499 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001500 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001501 return 1;
1502 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001503 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001504 }
1505 }
Sean Nelson6e0b9122010-02-19 00:52:10 +00001506 if (flash->unlock)
1507 flash->unlock(flash);
1508
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001509 if (erase_flash(flash)) {
1510 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001511 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001512 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001513 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001514 } else if (read_it) {
Sean Nelson6e0b9122010-02-19 00:52:10 +00001515 if (flash->unlock)
1516 flash->unlock(flash);
1517
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001518 if (read_flash_to_file(flash, filename)) {
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001519 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001520 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001521 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001522 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001523 struct stat image_stat;
1524
Sean Nelson6e0b9122010-02-19 00:52:10 +00001525 if (flash->unlock)
1526 flash->unlock(flash);
1527
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001528 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001529 msg_cerr("Erase is not working on this chip "
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001530 "and erase is needed for write. ");
1531 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001532 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001533 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001534 return 1;
1535 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001536 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001537 }
1538 }
1539 if (flash->tested & TEST_BAD_WRITE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001540 msg_cerr("Write is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001541 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001542 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001543 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001544 return 1;
1545 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001546 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001547 }
1548 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001549 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001550 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001551 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001552 exit(1);
1553 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001554 if (fstat(fileno(image), &image_stat) != 0) {
1555 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001556 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001557 exit(1);
1558 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001559 if (image_stat.st_size != flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001560 msg_gerr("Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001561 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001562 exit(1);
1563 }
1564
Peter Stuge1fec0f32009-01-12 21:00:35 +00001565 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001566#if CONFIG_INTERNAL == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001567 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001568#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001569 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001570 if (numbytes != size) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001571 msg_gerr("Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001572 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001573 return 1;
1574 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001575 }
1576
Ollie Lho184a4042005-11-26 21:55:36 +00001577 // This should be moved into each flash part's code to do it
1578 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001579 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001580
Ollie Lho184a4042005-11-26 21:55:36 +00001581 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001582
Peter Stugef31104c2008-04-28 14:47:30 +00001583 if (write_it) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001584 msg_cinfo("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001585 if (!flash->write) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001586 msg_cerr("Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001587 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001588 return 1;
1589 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001590 ret = flash->write(flash, buf);
1591 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001592 msg_cerr("FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001593 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001594 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001595 return 1;
1596 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001597 msg_cinfo("COMPLETE.\n");
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001598 }
Peter Stugef31104c2008-04-28 14:47:30 +00001599 }
Ollie Lho184a4042005-11-26 21:55:36 +00001600
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001601 if (verify_it) {
1602 /* Work around chips which need some time to calm down. */
1603 if (write_it)
1604 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001605 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001606 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001607 * might have an emergency situation.
1608 */
1609 if (ret && write_it)
1610 emergency_help_message();
1611 }
Ollie Lho184a4042005-11-26 21:55:36 +00001612
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001613 programmer_shutdown();
1614
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001615 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001616}