blob: 77a6042ed4b387e35e5c3917a48c8ad9dffd1909 [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
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000024#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000025#include <sys/types.h>
26#include <sys/stat.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000027#include <string.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000028#include <stdlib.h>
Ollie Lho184a4042005-11-26 21:55:36 +000029#include <getopt.h>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000030#if HAVE_UTSNAME == 1
31#include <sys/utsname.h>
32#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000033#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000034#include "flashchips.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000035
Carl-Daniel Hailfingera80cfbc2009-07-22 20:13:00 +000036const char *flashrom_version = FLASHROM_VERSION;
Ronald G. Minnichceec4202003-07-25 04:37:41 +000037char *chip_to_probe = NULL;
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000038int verbose = 0;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000039
40#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +000041enum programmer programmer = PROGRAMMER_INTERNAL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000042#elif DUMMY_SUPPORT == 1
43enum programmer programmer = PROGRAMMER_DUMMY;
44#else
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000045/* If neither internal nor dummy are selected, we must pick a sensible default.
46 * Since there is no reason to prefer a particular external programmer, we fail
47 * if more than one of them is selected. If only one is selected, it is clear
48 * that the user wants that one to become the default.
49 */
Uwe Hermannddd5c9e2010-02-21 21:17:00 +000050#if NIC3COM_SUPPORT+GFXNVIDIA_SUPPORT+DRKAISER_SUPPORT+SATASII_SUPPORT+ATAHPT_SUPPORT+FT2232_SPI_SUPPORT+SERPROG_SUPPORT+BUSPIRATE_SPI_SUPPORT+DEDIPROG_SUPPORT > 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000051#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all external programmers except one.
52#endif
53enum programmer programmer =
54#if NIC3COM_SUPPORT == 1
55 PROGRAMMER_NIC3COM
56#endif
57#if GFXNVIDIA_SUPPORT == 1
58 PROGRAMMER_GFXNVIDIA
59#endif
60#if DRKAISER_SUPPORT == 1
61 PROGRAMMER_DRKAISER
62#endif
63#if SATASII_SUPPORT == 1
64 PROGRAMMER_SATASII
65#endif
Uwe Hermannddd5c9e2010-02-21 21:17:00 +000066#if ATAHPT_SUPPORT == 1
67 PROGRAMMER_ATAHPT
68#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000069#if FT2232_SPI_SUPPORT == 1
70 PROGRAMMER_FT2232SPI
71#endif
72#if SERPROG_SUPPORT == 1
73 PROGRAMMER_SERPROG
74#endif
75#if BUSPIRATE_SPI_SUPPORT == 1
76 PROGRAMMER_BUSPIRATESPI
77#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000078#if DEDIPROG_SUPPORT == 1
79 PROGRAMMER_DEDIPROG
80#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000081;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000082#endif
83
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000084char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000085
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000086/**
87 * flashrom defaults to Parallel/LPC/FWH flash devices. If a known host
88 * controller is found, the init routine sets the buses_supported bitfield to
89 * contain the supported buses for that controller.
90 */
91enum chipbustype buses_supported = CHIP_BUSTYPE_NONSPI;
92
93/**
94 * Programmers supporting multiple buses can have differing size limits on
95 * each bus. Store the limits for each bus in a common struct.
96 */
97struct decode_sizes max_rom_decode = {
98 .parallel = 0xffffffff,
99 .lpc = 0xffffffff,
100 .fwh = 0xffffffff,
101 .spi = 0xffffffff
102};
103
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000104const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000105#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000106 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000107 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000108 .init = internal_init,
109 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000110 .map_flash_region = physmap,
111 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000112 .chip_readb = internal_chip_readb,
113 .chip_readw = internal_chip_readw,
114 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000115 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000116 .chip_writeb = internal_chip_writeb,
117 .chip_writew = internal_chip_writew,
118 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000119 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000120 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000121 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000122#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000123
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000124#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000125 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000126 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000127 .init = dummy_init,
128 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000129 .map_flash_region = dummy_map,
130 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000131 .chip_readb = dummy_chip_readb,
132 .chip_readw = dummy_chip_readw,
133 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000134 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000135 .chip_writeb = dummy_chip_writeb,
136 .chip_writew = dummy_chip_writew,
137 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000138 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000139 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000140 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000141#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000142
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000143#if NIC3COM_SUPPORT == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000144 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000145 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000146 .init = nic3com_init,
147 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000148 .map_flash_region = fallback_map,
149 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000150 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000151 .chip_readw = fallback_chip_readw,
152 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000153 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000154 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000155 .chip_writew = fallback_chip_writew,
156 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000157 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000158 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000159 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000160#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000161
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000162#if GFXNVIDIA_SUPPORT == 1
163 {
164 .name = "gfxnvidia",
165 .init = gfxnvidia_init,
166 .shutdown = gfxnvidia_shutdown,
167 .map_flash_region = fallback_map,
168 .unmap_flash_region = fallback_unmap,
169 .chip_readb = gfxnvidia_chip_readb,
170 .chip_readw = fallback_chip_readw,
171 .chip_readl = fallback_chip_readl,
172 .chip_readn = fallback_chip_readn,
173 .chip_writeb = gfxnvidia_chip_writeb,
174 .chip_writew = fallback_chip_writew,
175 .chip_writel = fallback_chip_writel,
176 .chip_writen = fallback_chip_writen,
177 .delay = internal_delay,
178 },
179#endif
180
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000181#if DRKAISER_SUPPORT == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000182 {
TURBO Jb0912c02009-09-02 23:00:46 +0000183 .name = "drkaiser",
184 .init = drkaiser_init,
185 .shutdown = drkaiser_shutdown,
186 .map_flash_region = fallback_map,
187 .unmap_flash_region = fallback_unmap,
188 .chip_readb = drkaiser_chip_readb,
189 .chip_readw = fallback_chip_readw,
190 .chip_readl = fallback_chip_readl,
191 .chip_readn = fallback_chip_readn,
192 .chip_writeb = drkaiser_chip_writeb,
193 .chip_writew = fallback_chip_writew,
194 .chip_writel = fallback_chip_writel,
195 .chip_writen = fallback_chip_writen,
196 .delay = internal_delay,
197 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000198#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000199
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000200#if SATASII_SUPPORT == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000201 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000202 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000203 .init = satasii_init,
204 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000205 .map_flash_region = fallback_map,
206 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000207 .chip_readb = satasii_chip_readb,
208 .chip_readw = fallback_chip_readw,
209 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000210 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000211 .chip_writeb = satasii_chip_writeb,
212 .chip_writew = fallback_chip_writew,
213 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000214 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000215 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000216 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000217#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000218
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000219#if ATAHPT_SUPPORT == 1
220 {
221 .name = "atahpt",
222 .init = atahpt_init,
223 .shutdown = atahpt_shutdown,
224 .map_flash_region = fallback_map,
225 .unmap_flash_region = fallback_unmap,
226 .chip_readb = atahpt_chip_readb,
227 .chip_readw = fallback_chip_readw,
228 .chip_readl = fallback_chip_readl,
229 .chip_readn = fallback_chip_readn,
230 .chip_writeb = atahpt_chip_writeb,
231 .chip_writew = fallback_chip_writew,
232 .chip_writel = fallback_chip_writel,
233 .chip_writen = fallback_chip_writen,
234 .delay = internal_delay,
235 },
236#endif
237
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000238#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000239 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000240 .name = "it87spi",
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000241 .init = it87spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000242 .shutdown = noop_shutdown,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000243 .map_flash_region = fallback_map,
244 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000245 .chip_readb = noop_chip_readb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000246 .chip_readw = fallback_chip_readw,
247 .chip_readl = fallback_chip_readl,
248 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000249 .chip_writeb = noop_chip_writeb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000250 .chip_writew = fallback_chip_writew,
251 .chip_writel = fallback_chip_writel,
252 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000253 .delay = internal_delay,
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000254 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000255#endif
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000256
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000257#if FT2232_SPI_SUPPORT == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000258 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000259 .name = "ft2232spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000260 .init = ft2232_spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000261 .shutdown = noop_shutdown, /* Missing shutdown */
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000262 .map_flash_region = fallback_map,
263 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000264 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000265 .chip_readw = fallback_chip_readw,
266 .chip_readl = fallback_chip_readl,
267 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000268 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000269 .chip_writew = fallback_chip_writew,
270 .chip_writel = fallback_chip_writel,
271 .chip_writen = fallback_chip_writen,
272 .delay = internal_delay,
273 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000274#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000275
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000276#if SERPROG_SUPPORT == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000277 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000278 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000279 .init = serprog_init,
280 .shutdown = serprog_shutdown,
281 .map_flash_region = fallback_map,
282 .unmap_flash_region = fallback_unmap,
283 .chip_readb = serprog_chip_readb,
284 .chip_readw = fallback_chip_readw,
285 .chip_readl = fallback_chip_readl,
286 .chip_readn = serprog_chip_readn,
287 .chip_writeb = serprog_chip_writeb,
288 .chip_writew = fallback_chip_writew,
289 .chip_writel = fallback_chip_writel,
290 .chip_writen = fallback_chip_writen,
291 .delay = serprog_delay,
292 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000293#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000294
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000295#if BUSPIRATE_SPI_SUPPORT == 1
296 {
297 .name = "buspiratespi",
298 .init = buspirate_spi_init,
299 .shutdown = buspirate_spi_shutdown,
300 .map_flash_region = fallback_map,
301 .unmap_flash_region = fallback_unmap,
302 .chip_readb = noop_chip_readb,
303 .chip_readw = fallback_chip_readw,
304 .chip_readl = fallback_chip_readl,
305 .chip_readn = fallback_chip_readn,
306 .chip_writeb = noop_chip_writeb,
307 .chip_writew = fallback_chip_writew,
308 .chip_writel = fallback_chip_writel,
309 .chip_writen = fallback_chip_writen,
310 .delay = internal_delay,
311 },
312#endif
313
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000314#if DEDIPROG_SUPPORT == 1
315 {
316 .name = "dediprog",
317 .init = dediprog_init,
318 .shutdown = dediprog_shutdown,
319 .map_flash_region = fallback_map,
320 .unmap_flash_region = fallback_unmap,
321 .chip_readb = noop_chip_readb,
322 .chip_readw = fallback_chip_readw,
323 .chip_readl = fallback_chip_readl,
324 .chip_readn = fallback_chip_readn,
325 .chip_writeb = noop_chip_writeb,
326 .chip_writew = fallback_chip_writew,
327 .chip_writel = fallback_chip_writel,
328 .chip_writen = fallback_chip_writen,
329 .delay = internal_delay,
330 },
331#endif
332
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000333 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000334};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000335
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000336#define SHUTDOWN_MAXFN 4
337static int shutdown_fn_count = 0;
338struct shutdown_func_data {
339 void (*func) (void *data);
340 void *data;
341} shutdown_fn[SHUTDOWN_MAXFN];
342
343/* Register a function to be executed on programmer shutdown.
344 * The advantage over atexit() is that you can supply a void pointer which will
345 * be used as parameter to the registered function upon programmer shutdown.
346 * This pointer can point to arbitrary data used by said function, e.g. undo
347 * information for GPIO settings etc. If unneeded, set data=NULL.
348 * Please note that the first (void *data) belongs to the function signature of
349 * the function passed as first parameter.
350 */
351int register_shutdown(void (*function) (void *data), void *data)
352{
353 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
354 msg_perr("Tried to register more than %n shutdown functions.\n",
355 SHUTDOWN_MAXFN);
356 return 1;
357 }
358 shutdown_fn[shutdown_fn_count].func = function;
359 shutdown_fn[shutdown_fn_count].data = data;
360 shutdown_fn_count++;
361
362 return 0;
363}
364
Uwe Hermann09e04f72009-05-16 22:36:00 +0000365int programmer_init(void)
366{
367 return programmer_table[programmer].init();
368}
369
370int programmer_shutdown(void)
371{
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000372 int i;
373
374 for (i = shutdown_fn_count - 1; i >= 0; i--)
375 shutdown_fn[i].func(shutdown_fn[i].data);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000376 return programmer_table[programmer].shutdown();
377}
378
379void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
380 size_t len)
381{
382 return programmer_table[programmer].map_flash_region(descr,
383 phys_addr, len);
384}
385
386void programmer_unmap_flash_region(void *virt_addr, size_t len)
387{
388 programmer_table[programmer].unmap_flash_region(virt_addr, len);
389}
390
391void chip_writeb(uint8_t val, chipaddr addr)
392{
393 programmer_table[programmer].chip_writeb(val, addr);
394}
395
396void chip_writew(uint16_t val, chipaddr addr)
397{
398 programmer_table[programmer].chip_writew(val, addr);
399}
400
401void chip_writel(uint32_t val, chipaddr addr)
402{
403 programmer_table[programmer].chip_writel(val, addr);
404}
405
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000406void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
407{
408 programmer_table[programmer].chip_writen(buf, addr, len);
409}
410
Uwe Hermann09e04f72009-05-16 22:36:00 +0000411uint8_t chip_readb(const chipaddr addr)
412{
413 return programmer_table[programmer].chip_readb(addr);
414}
415
416uint16_t chip_readw(const chipaddr addr)
417{
418 return programmer_table[programmer].chip_readw(addr);
419}
420
421uint32_t chip_readl(const chipaddr addr)
422{
423 return programmer_table[programmer].chip_readl(addr);
424}
425
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000426void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
427{
428 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000429}
430
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000431void programmer_delay(int usecs)
432{
433 programmer_table[programmer].delay(usecs);
434}
435
Peter Stuge776d2022009-01-26 00:39:57 +0000436void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000437{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000438 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000439 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000440 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000441 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000442}
443
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000444int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000445{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000446 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000447
448 return 0;
449}
450
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000451unsigned long flashbase = 0;
452
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000453int min(int a, int b)
454{
455 return (a < b) ? a : b;
456}
457
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000458int max(int a, int b)
459{
460 return (a > b) ? a : b;
461}
462
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000463int bitcount(unsigned long a)
464{
465 int i = 0;
466 for (; a != 0; a >>= 1)
467 if (a & 1)
468 i++;
469 return i;
470}
471
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000472char *strcat_realloc(char *dest, const char *src)
473{
474 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
475 if (!dest)
476 return NULL;
477 strcat(dest, src);
478 return dest;
479}
480
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000481/* This is a somewhat hacked function similar in some ways to strtok().
482 * It will look for needle in haystack, return a copy of needle and remove
483 * everything from the first occurrence of needle to the next delimiter
484 * from haystack.
485 */
486char *extract_param(char **haystack, char *needle, char *delim)
487{
488 char *param_pos, *rest, *tmp;
489 char *dev = NULL;
490 int devlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000491 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000492
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000493 needlelen = strlen(needle);
494 if (!needlelen) {
495 msg_gerr("%s: empty needle! Please report a bug at "
496 "flashrom@flashrom.org\n", __func__);
497 return NULL;
498 }
499 /* No programmer parameters given. */
500 if (*haystack == NULL)
501 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000502 param_pos = strstr(*haystack, needle);
503 do {
504 if (!param_pos)
505 return NULL;
506 /* Beginning of the string? */
507 if (param_pos == *haystack)
508 break;
509 /* After a delimiter? */
510 if (strchr(delim, *(param_pos - 1)))
511 break;
512 /* Continue searching. */
513 param_pos++;
514 param_pos = strstr(param_pos, needle);
515 } while (1);
516
517 if (param_pos) {
518 param_pos += strlen(needle);
519 devlen = strcspn(param_pos, delim);
520 if (devlen) {
521 dev = malloc(devlen + 1);
522 if (!dev) {
523 fprintf(stderr, "Out of memory!\n");
524 exit(1);
525 }
526 strncpy(dev, param_pos, devlen);
527 dev[devlen] = '\0';
528 }
529 rest = param_pos + devlen;
530 rest += strspn(rest, delim);
531 param_pos -= strlen(needle);
532 memmove(param_pos, rest, strlen(rest) + 1);
533 tmp = realloc(*haystack, strlen(*haystack) + 1);
534 if (!tmp) {
535 fprintf(stderr, "Out of memory!\n");
536 exit(1);
537 }
538 *haystack = tmp;
539 }
540
541
542 return dev;
543}
544
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000545/* start is an offset to the base address of the flash chip */
546int check_erased_range(struct flashchip *flash, int start, int len)
547{
548 int ret;
549 uint8_t *cmpbuf = malloc(len);
550
551 if (!cmpbuf) {
552 fprintf(stderr, "Could not allocate memory!\n");
553 exit(1);
554 }
555 memset(cmpbuf, 0xff, len);
556 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
557 free(cmpbuf);
558 return ret;
559}
560
561/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000562 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
563 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000564 * @start offset to the base address of the flash chip
565 * @len length of the verified area
566 * @message string to print in the "FAILED" message
567 * @return 0 for success, -1 for failure
568 */
569int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
570{
571 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000572 int page_size = flash->page_size;
573 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000574 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000575
576 if (!len)
577 goto out_free;
578
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000579 if (!flash->read) {
580 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
581 return 1;
582 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000583 if (!readbuf) {
584 fprintf(stderr, "Could not allocate memory!\n");
585 exit(1);
586 }
587
588 if (start + len > flash->total_size * 1024) {
589 fprintf(stderr, "Error: %s called with start 0x%x + len 0x%x >"
590 " total_size 0x%x\n", __func__, start, len,
591 flash->total_size * 1024);
592 ret = -1;
593 goto out_free;
594 }
595 if (!message)
596 message = "VERIFY";
597
598 /* Warning: This loop has a very unusual condition and body.
599 * The loop needs to go through each page with at least one affected
600 * byte. The lowest page number is (start / page_size) since that
601 * division rounds down. The highest page number we want is the page
602 * where the last byte of the range lives. That last byte has the
603 * address (start + len - 1), thus the highest page number is
604 * (start + len - 1) / page_size. Since we want to include that last
605 * page as well, the loop condition uses <=.
606 */
607 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
608 /* Byte position of the first byte in the range in this page. */
609 starthere = max(start, i * page_size);
610 /* Length of bytes in the range in this page. */
611 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000612 flash->read(flash, readbuf, starthere, lenhere);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000613 for (j = 0; j < lenhere; j++) {
614 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000615 /* Only print the first failure. */
616 if (!failcount++)
617 fprintf(stderr, "%s FAILED at 0x%08x! "
618 "Expected=0x%02x, Read=0x%02x,",
619 message, starthere + j,
620 cmpbuf[starthere - start + j],
621 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000622 }
623 }
624 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000625 if (failcount) {
626 fprintf(stderr, " failed byte count from 0x%08x-0x%08x: 0x%x\n",
627 start, start + len - 1, failcount);
628 ret = -1;
629 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000630
631out_free:
632 free(readbuf);
633 return ret;
634}
635
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000636/**
637 * Check if the buffer @have can be programmed to the content of @want without
638 * erasing. This is only possible if all chunks of size @gran are either kept
639 * as-is or changed from an all-ones state to any other state.
640 * The following write granularities (enum @gran) are known:
641 * - 1 bit. Each bit can be cleared individually.
642 * - 1 byte. A byte can be written once. Further writes to an already written
643 * byte cause the contents to be either undefined or to stay unchanged.
644 * - 128 bytes. If less than 128 bytes are written, the rest will be
645 * erased. Each write to a 128-byte region will trigger an automatic erase
646 * before anything is written. Very uncommon behaviour and unsupported by
647 * this function.
648 * - 256 bytes. If less than 256 bytes are written, the contents of the
649 * unwritten bytes are undefined.
650 *
651 * @have buffer with current content
652 * @want buffer with desired content
653 * @len length of the verified area
654 * @gran write granularity (enum, not count)
655 * @return 0 if no erase is needed, 1 otherwise
656 */
657int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
658{
659 int result = 0;
660 int i, j, limit;
661
662 switch (gran) {
663 case write_gran_1bit:
664 for (i = 0; i < len; i++)
665 if ((have[i] & want[i]) != want[i]) {
666 result = 1;
667 break;
668 }
669 break;
670 case write_gran_1byte:
671 for (i = 0; i < len; i++)
672 if ((have[i] != want[i]) && (have[i] != 0xff)) {
673 result = 1;
674 break;
675 }
676 break;
677 case write_gran_256bytes:
678 for (j = 0; j < len / 256; j++) {
679 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000680 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000681 if (!memcmp(have + j * 256, want + j * 256, limit))
682 continue;
683 /* have needs to be in erased state. */
684 for (i = 0; i < limit; i++)
685 if (have[i] != 0xff) {
686 result = 1;
687 break;
688 }
689 if (result)
690 break;
691 }
692 break;
693 }
694 return result;
695}
696
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000697/* This function generates various test patterns useful for testing controller
698 * and chip communication as well as chip behaviour.
699 *
700 * If a byte can be written multiple times, each time keeping 0-bits at 0
701 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
702 * is essentially an AND operation. That's also the reason why this function
703 * provides the result of AND between various patterns.
704 *
705 * Below is a list of patterns (and their block length).
706 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
707 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
708 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
709 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
710 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
711 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
712 * Pattern 6 is 00 (1 Byte)
713 * Pattern 7 is ff (1 Byte)
714 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
715 * byte block.
716 *
717 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
718 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
719 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
720 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
721 * Pattern 12 is 00 (1 Byte)
722 * Pattern 13 is ff (1 Byte)
723 * Patterns 8-13 have no block number.
724 *
725 * Patterns 0-3 are created to detect and efficiently diagnose communication
726 * slips like missed bits or bytes and their repetitive nature gives good visual
727 * cues to the person inspecting the results. In addition, the following holds:
728 * AND Pattern 0/1 == Pattern 4
729 * AND Pattern 2/3 == Pattern 5
730 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
731 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
732 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
733 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
734 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
735 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
736 * Besides that, they provide for bit testing of the last two bytes of every
737 * 256 byte block which contains the block number for patterns 0-6.
738 * Patterns 10-11 are special purpose for detecting subblock aliasing with
739 * block sizes >256 bytes (some Dataflash chips etc.)
740 * AND Pattern 8/9 == Pattern 12
741 * AND Pattern 10/11 == Pattern 12
742 * Pattern 13 is the completely erased state.
743 * None of the patterns can detect aliasing at boundaries which are a multiple
744 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
745 */
746int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
747{
748 int i;
749
750 if (!buf) {
751 fprintf(stderr, "Invalid buffer!\n");
752 return 1;
753 }
754
755 switch (variant) {
756 case 0:
757 for (i = 0; i < size; i++)
758 buf[i] = (i & 0xf) << 4 | 0x5;
759 break;
760 case 1:
761 for (i = 0; i < size; i++)
762 buf[i] = (i & 0xf) << 4 | 0xa;
763 break;
764 case 2:
765 for (i = 0; i < size; i++)
766 buf[i] = 0x50 | (i & 0xf);
767 break;
768 case 3:
769 for (i = 0; i < size; i++)
770 buf[i] = 0xa0 | (i & 0xf);
771 break;
772 case 4:
773 for (i = 0; i < size; i++)
774 buf[i] = (i & 0xf) << 4;
775 break;
776 case 5:
777 for (i = 0; i < size; i++)
778 buf[i] = i & 0xf;
779 break;
780 case 6:
781 memset(buf, 0x00, size);
782 break;
783 case 7:
784 memset(buf, 0xff, size);
785 break;
786 case 8:
787 for (i = 0; i < size; i++)
788 buf[i] = i & 0xff;
789 break;
790 case 9:
791 for (i = 0; i < size; i++)
792 buf[i] = ~(i & 0xff);
793 break;
794 case 10:
795 for (i = 0; i < size % 2; i++) {
796 buf[i * 2] = (i >> 8) & 0xff;
797 buf[i * 2 + 1] = i & 0xff;
798 }
799 if (size & 0x1)
800 buf[i * 2] = (i >> 8) & 0xff;
801 break;
802 case 11:
803 for (i = 0; i < size % 2; i++) {
804 buf[i * 2] = ~((i >> 8) & 0xff);
805 buf[i * 2 + 1] = ~(i & 0xff);
806 }
807 if (size & 0x1)
808 buf[i * 2] = ~((i >> 8) & 0xff);
809 break;
810 case 12:
811 memset(buf, 0x00, size);
812 break;
813 case 13:
814 memset(buf, 0xff, size);
815 break;
816 }
817
818 if ((variant >= 0) && (variant <= 7)) {
819 /* Write block number in the last two bytes of each 256-byte
820 * block, big endian for easier reading of the hexdump.
821 * Note that this wraps around for chips larger than 2^24 bytes
822 * (16 MB).
823 */
824 for (i = 0; i < size / 256; i++) {
825 buf[i * 256 + 254] = (i >> 8) & 0xff;
826 buf[i * 256 + 255] = i & 0xff;
827 }
828 }
829
830 return 0;
831}
832
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000833int check_max_decode(enum chipbustype buses, uint32_t size)
834{
835 int limitexceeded = 0;
836 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
837 (max_rom_decode.parallel < size)) {
838 limitexceeded++;
839 printf_debug("Chip size %u kB is bigger than supported "
840 "size %u kB of chipset/board/programmer "
841 "for %s interface, "
842 "probe/read/erase/write may fail. ", size / 1024,
843 max_rom_decode.parallel / 1024, "Parallel");
844 }
845 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
846 limitexceeded++;
847 printf_debug("Chip size %u kB is bigger than supported "
848 "size %u kB of chipset/board/programmer "
849 "for %s interface, "
850 "probe/read/erase/write may fail. ", size / 1024,
851 max_rom_decode.lpc / 1024, "LPC");
852 }
853 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
854 limitexceeded++;
855 printf_debug("Chip size %u kB is bigger than supported "
856 "size %u kB of chipset/board/programmer "
857 "for %s interface, "
858 "probe/read/erase/write may fail. ", size / 1024,
859 max_rom_decode.fwh / 1024, "FWH");
860 }
861 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
862 limitexceeded++;
863 printf_debug("Chip size %u kB is bigger than supported "
864 "size %u kB of chipset/board/programmer "
865 "for %s interface, "
866 "probe/read/erase/write may fail. ", size / 1024,
867 max_rom_decode.spi / 1024, "SPI");
868 }
869 if (!limitexceeded)
870 return 0;
871 /* Sometimes chip and programmer have more than one bus in common,
872 * and the limit is not exceeded on all buses. Tell the user.
873 */
874 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000875 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000876 printf_debug("There is at least one common chip/programmer "
877 "interface which can support a chip of this size. "
878 "You can try --force at your own risk.\n");
879 return 1;
880}
881
Peter Stuge483b8f02008-09-03 23:10:05 +0000882struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000883{
Peter Stuge483b8f02008-09-03 23:10:05 +0000884 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000885 unsigned long base = 0;
886 uint32_t size;
887 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000888 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000889
Peter Stuge483b8f02008-09-03 23:10:05 +0000890 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000891 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000892 continue;
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000893 msg_gdbg("Probing for %s %s, %d KB: ",
Stefan Reinauerac378972008-03-17 22:59:40 +0000894 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000895 if (!flash->probe && !force) {
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000896 msg_gdbg("failed! flashrom has no probe function for "
897 "this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000898 continue;
899 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000900 buses_common = buses_supported & flash->bustype;
901 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000902 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000903 msg_gdbg("skipped.");
904 msg_gspew(" Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000905 free(tmp);
906 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000907 msg_gspew("and chip bus type %s are incompatible.",
908 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000909 free(tmp);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000910 msg_gdbg("\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000911 continue;
912 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000913
Ollie Lhocbbf1252004-03-17 22:22:08 +0000914 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000915 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000916
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000917 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000918 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000919
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000920 if (force)
921 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000922
Peter Stuge483b8f02008-09-03 23:10:05 +0000923 if (flash->probe(flash) != 1)
924 goto notfound;
925
Uwe Hermann394131e2008-10-18 21:14:13 +0000926 if (first_flash == flashchips
927 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000928 break;
929
Peter Stuge483b8f02008-09-03 23:10:05 +0000930notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000931 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000932 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000933
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000934 if (!flash || !flash->name)
935 return NULL;
936
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000937 printf("%s chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
938 force ? "Assuming" : "Found",
Uwe Hermann9899cad2009-06-28 21:47:57 +0000939 flash->vendor, flash->name, flash->total_size,
940 flashbuses_to_text(flash->bustype), base);
941
Sean Nelson6e0b9122010-02-19 00:52:10 +0000942 if (flash->printlock)
943 flash->printlock(flash);
944
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000945 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000946}
947
Stefan Reinauere3705282005-12-18 16:41:10 +0000948int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000949{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000950 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000951 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000952
Uwe Hermanna502dce2007-10-17 23:55:15 +0000953 printf("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000954
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000955 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000956
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000957 if (!ret)
958 printf("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000959
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000960 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000961}
962
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +0000963int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000964{
965 unsigned long numbytes;
966 FILE *image;
967 unsigned long size = flash->total_size * 1024;
968 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000969
970 if (!filename) {
971 printf("Error: No filename specified.\n");
972 return 1;
973 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +0000974 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000975 perror(filename);
976 exit(1);
977 }
978 printf("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000979 if (!flash->read) {
980 printf("FAILED!\n");
981 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
982 return 1;
983 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000984 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000985
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000986 numbytes = fwrite(buf, 1, size, image);
987 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +0000988 free(buf);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000989 printf("%s.\n", numbytes == size ? "done" : "FAILED");
990 if (numbytes != size)
991 return 1;
992 return 0;
993}
994
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000995/* This function shares a lot of its structure with erase_flash().
996 * Even if an error is found, the function will keep going and check the rest.
997 */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000998int selfcheck_eraseblocks(struct flashchip *flash)
999{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001000 int i, j, k;
1001 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001002
1003 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1004 unsigned int done = 0;
1005 struct block_eraser eraser = flash->block_erasers[k];
1006
1007 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1008 /* Blocks with zero size are bugs in flashchips.c. */
1009 if (eraser.eraseblocks[i].count &&
1010 !eraser.eraseblocks[i].size) {
1011 msg_gerr("ERROR: Flash chip %s erase function "
1012 "%i region %i has size 0. Please report"
1013 " a bug at flashrom@flashrom.org\n",
1014 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001015 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001016 }
1017 /* Blocks with zero count are bugs in flashchips.c. */
1018 if (!eraser.eraseblocks[i].count &&
1019 eraser.eraseblocks[i].size) {
1020 msg_gerr("ERROR: Flash chip %s erase function "
1021 "%i region %i has count 0. Please report"
1022 " a bug at flashrom@flashrom.org\n",
1023 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001024 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001025 }
1026 done += eraser.eraseblocks[i].count *
1027 eraser.eraseblocks[i].size;
1028 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001029 /* Empty eraseblock definition with erase function. */
1030 if (!done && eraser.block_erase)
1031 msg_pspew("Strange: Empty eraseblock definition with "
1032 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001033 if (!done)
1034 continue;
1035 if (done != flash->total_size * 1024) {
1036 msg_gerr("ERROR: Flash chip %s erase function %i "
1037 "region walking resulted in 0x%06x bytes total,"
1038 " expected 0x%06x bytes. Please report a bug at"
1039 " flashrom@flashrom.org\n", flash->name, k,
1040 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001041 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001042 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001043 if (!eraser.block_erase)
1044 continue;
1045 /* Check if there are identical erase functions for different
1046 * layouts. That would imply "magic" erase functions. The
1047 * easiest way to check this is with function pointers.
1048 */
Uwe Hermann43959702010-03-13 17:28:29 +00001049 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001050 if (eraser.block_erase ==
1051 flash->block_erasers[j].block_erase) {
1052 msg_gerr("ERROR: Flash chip %s erase function "
1053 "%i and %i are identical. Please report"
1054 " a bug at flashrom@flashrom.org\n",
1055 flash->name, k, j);
1056 ret = 1;
1057 }
Uwe Hermann43959702010-03-13 17:28:29 +00001058 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001059 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001060 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001061}
1062
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001063int erase_flash(struct flashchip *flash)
1064{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001065 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001066 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001067
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001068 printf("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001069 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001070 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001071 struct block_eraser eraser = flash->block_erasers[k];
1072
1073 printf_debug("Looking at blockwise erase function %i... ", k);
1074 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1075 printf_debug("not defined. "
1076 "Looking for another erase function.\n");
1077 continue;
1078 }
1079 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1080 printf_debug("eraseblock layout is known, but no "
1081 "matching block erase function found. "
1082 "Looking for another erase function.\n");
1083 continue;
1084 }
1085 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1086 printf_debug("block erase function found, but "
1087 "eraseblock layout is unknown. "
1088 "Looking for another erase function.\n");
1089 continue;
1090 }
1091 found = 1;
1092 printf_debug("trying... ");
1093 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1094 /* count==0 for all automatically initialized array
1095 * members so the loop below won't be executed for them.
1096 */
1097 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001098 start = done + eraser.eraseblocks[i].size * j;
1099 len = eraser.eraseblocks[i].size;
1100 printf_debug("0x%06x-0x%06x, ", start,
1101 start + len - 1);
1102 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001103 if (ret)
1104 break;
1105 }
1106 if (ret)
1107 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001108 done += eraser.eraseblocks[i].count *
1109 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001110 }
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001111 printf_debug("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001112 /* If everything is OK, don't try another erase function. */
1113 if (!ret)
1114 break;
1115 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001116 if (!found) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001117 fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
1118 return 1;
1119 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001120
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001121 if (ret) {
1122 fprintf(stderr, "FAILED!\n");
1123 } else {
1124 printf("SUCCESS.\n");
1125 }
1126 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001127}
1128
Uwe Hermannc67d0372009-10-01 18:40:02 +00001129void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001130{
1131 fprintf(stderr, "Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001132 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
1133 "mail flashrom@flashrom.org!\n--------------------"
1134 "-----------------------------------------------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001135 "DO NOT REBOOT OR POWEROFF!\n");
1136}
1137
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001138/* The way to go if you want a delimited list of programmers*/
1139void list_programmers(char *delim)
1140{
1141 enum programmer p;
1142 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1143 printf("%s", programmer_table[p].name);
1144 if (p < PROGRAMMER_INVALID - 1)
1145 printf("%s", delim);
1146 }
1147 printf("\n");
1148}
1149
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001150void print_sysinfo(void)
1151{
1152#if HAVE_UTSNAME == 1
1153 struct utsname osinfo;
1154 uname(&osinfo);
1155
1156 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1157 osinfo.machine);
1158#else
1159 msg_ginfo(" on unknown machine");
1160#endif
1161 msg_ginfo(", built with");
1162#if NEED_PCI == 1
1163#ifdef PCILIB_VERSION
1164 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1165#else
1166 msg_ginfo(" unknown PCI library,");
1167#endif
1168#endif
1169#ifdef __clang__
1170 msg_ginfo(" LLVM %i/clang %i", __llvm__, __clang__);
1171#elif defined(__GNUC__)
1172 msg_ginfo(" GCC");
1173#ifdef __VERSION__
1174 msg_ginfo(" %s", __VERSION__);
1175#else
1176 msg_ginfo(" unknown version");
1177#endif
1178#else
1179 msg_ginfo(" unknown compiler");
1180#endif
1181 msg_ginfo("\n");
1182}
1183
Bernhard Walle201bde32008-01-21 15:24:22 +00001184void print_version(void)
1185{
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001186 printf("flashrom v%s", flashrom_version);
1187 print_sysinfo();
Bernhard Walle201bde32008-01-21 15:24:22 +00001188}
1189
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001190int selfcheck(void)
1191{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001192 int ret = 0;
1193 struct flashchip *flash;
1194
1195 /* Safety check. Instead of aborting after the first error, check
1196 * if more errors exist.
1197 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001198 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
1199 fprintf(stderr, "Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001200 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001201 }
1202 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
1203 fprintf(stderr, "SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001204 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001205 }
1206#if BITBANG_SPI_SUPPORT == 1
1207 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
1208 fprintf(stderr, "Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001209 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001210 }
1211#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001212 for (flash = flashchips; flash && flash->name; flash++)
1213 if (selfcheck_eraseblocks(flash))
1214 ret = 1;
1215 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001216}
1217
1218void check_chip_supported(struct flashchip *flash)
1219{
1220 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
1221 printf("===\n");
1222 if (flash->tested & TEST_BAD_MASK) {
1223 printf("This flash part has status NOT WORKING for operations:");
1224 if (flash->tested & TEST_BAD_PROBE)
1225 printf(" PROBE");
1226 if (flash->tested & TEST_BAD_READ)
1227 printf(" READ");
1228 if (flash->tested & TEST_BAD_ERASE)
1229 printf(" ERASE");
1230 if (flash->tested & TEST_BAD_WRITE)
1231 printf(" WRITE");
1232 printf("\n");
1233 }
1234 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1235 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1236 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1237 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
1238 printf("This flash part has status UNTESTED for operations:");
1239 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
1240 printf(" PROBE");
1241 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
1242 printf(" READ");
1243 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
1244 printf(" ERASE");
1245 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
1246 printf(" WRITE");
1247 printf("\n");
1248 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001249 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001250 printf("Please email a report to flashrom@flashrom.org if any "
1251 "of the above operations\nwork correctly for you with "
1252 "this flash part. Please include the flashrom\noutput "
1253 "with the additional -V option for all operations you "
1254 "tested (-V, -rV,\n-wV, -EV), and mention which "
Uwe Hermann43959702010-03-13 17:28:29 +00001255 "mainboard or programmer you tested.\nThanks for your "
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001256 "help!\n===\n");
1257 }
1258}
1259
Ollie Lho761bf1b2004-03-20 16:46:10 +00001260int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001261{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001262 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001263}
1264
1265/* This function signature is horrible. We need to design a better interface,
1266 * but right now it allows us to split off the CLI code.
1267 */
1268int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1269{
1270 uint8_t *buf;
1271 unsigned long numbytes;
1272 FILE *image;
1273 int ret = 0;
1274 unsigned long size;
1275
1276 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001277 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001278
Ollie Lhoefa28582004-12-08 20:10:01 +00001279 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001280 if (flash->tested & TEST_BAD_ERASE) {
1281 fprintf(stderr, "Erase is not working on this chip. ");
1282 if (!force) {
1283 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001284 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001285 return 1;
1286 } else {
1287 fprintf(stderr, "Continuing anyway.\n");
1288 }
1289 }
Sean Nelson6e0b9122010-02-19 00:52:10 +00001290 if (flash->unlock)
1291 flash->unlock(flash);
1292
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001293 if (erase_flash(flash)) {
1294 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001295 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001296 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001297 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001298 } else if (read_it) {
Sean Nelson6e0b9122010-02-19 00:52:10 +00001299 if (flash->unlock)
1300 flash->unlock(flash);
1301
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001302 if (read_flash(flash, filename)) {
1303 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001304 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001305 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001306 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001307 struct stat image_stat;
1308
Sean Nelson6e0b9122010-02-19 00:52:10 +00001309 if (flash->unlock)
1310 flash->unlock(flash);
1311
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001312 if (flash->tested & TEST_BAD_ERASE) {
1313 fprintf(stderr, "Erase is not working on this chip "
1314 "and erase is needed for write. ");
1315 if (!force) {
1316 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001317 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001318 return 1;
1319 } else {
1320 fprintf(stderr, "Continuing anyway.\n");
1321 }
1322 }
1323 if (flash->tested & TEST_BAD_WRITE) {
1324 fprintf(stderr, "Write is not working on this chip. ");
1325 if (!force) {
1326 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001327 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001328 return 1;
1329 } else {
1330 fprintf(stderr, "Continuing anyway.\n");
1331 }
1332 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001333 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001334 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001335 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001336 exit(1);
1337 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001338 if (fstat(fileno(image), &image_stat) != 0) {
1339 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001340 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001341 exit(1);
1342 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001343 if (image_stat.st_size != flash->total_size * 1024) {
Uwe Hermann793bdcd2008-05-22 22:47:04 +00001344 fprintf(stderr, "Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001345 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001346 exit(1);
1347 }
1348
Peter Stuge1fec0f32009-01-12 21:00:35 +00001349 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001350#if INTERNAL_SUPPORT == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001351 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001352#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001353 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001354 if (numbytes != size) {
1355 fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001356 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001357 return 1;
1358 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001359 }
1360
Ollie Lho184a4042005-11-26 21:55:36 +00001361 // This should be moved into each flash part's code to do it
1362 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001363 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001364
Ollie Lho184a4042005-11-26 21:55:36 +00001365 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001366
Peter Stugef31104c2008-04-28 14:47:30 +00001367 if (write_it) {
Paul Foxd51410c2009-06-12 08:04:08 +00001368 printf("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001369 if (!flash->write) {
1370 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001371 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001372 return 1;
1373 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001374 ret = flash->write(flash, buf);
1375 if (ret) {
1376 fprintf(stderr, "FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001377 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001378 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001379 return 1;
1380 } else {
1381 printf("COMPLETE.\n");
1382 }
Peter Stugef31104c2008-04-28 14:47:30 +00001383 }
Ollie Lho184a4042005-11-26 21:55:36 +00001384
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001385 if (verify_it) {
1386 /* Work around chips which need some time to calm down. */
1387 if (write_it)
1388 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001389 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001390 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001391 * might have an emergency situation.
1392 */
1393 if (ret && write_it)
1394 emergency_help_message();
1395 }
Ollie Lho184a4042005-11-26 21:55:36 +00001396
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001397 programmer_shutdown();
1398
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001399 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001400}