blob: 2450dee4590158b307bf33211d8a8912a06b46f2 [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;
491
492 param_pos = strstr(*haystack, needle);
493 do {
494 if (!param_pos)
495 return NULL;
496 /* Beginning of the string? */
497 if (param_pos == *haystack)
498 break;
499 /* After a delimiter? */
500 if (strchr(delim, *(param_pos - 1)))
501 break;
502 /* Continue searching. */
503 param_pos++;
504 param_pos = strstr(param_pos, needle);
505 } while (1);
506
507 if (param_pos) {
508 param_pos += strlen(needle);
509 devlen = strcspn(param_pos, delim);
510 if (devlen) {
511 dev = malloc(devlen + 1);
512 if (!dev) {
513 fprintf(stderr, "Out of memory!\n");
514 exit(1);
515 }
516 strncpy(dev, param_pos, devlen);
517 dev[devlen] = '\0';
518 }
519 rest = param_pos + devlen;
520 rest += strspn(rest, delim);
521 param_pos -= strlen(needle);
522 memmove(param_pos, rest, strlen(rest) + 1);
523 tmp = realloc(*haystack, strlen(*haystack) + 1);
524 if (!tmp) {
525 fprintf(stderr, "Out of memory!\n");
526 exit(1);
527 }
528 *haystack = tmp;
529 }
530
531
532 return dev;
533}
534
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000535/* start is an offset to the base address of the flash chip */
536int check_erased_range(struct flashchip *flash, int start, int len)
537{
538 int ret;
539 uint8_t *cmpbuf = malloc(len);
540
541 if (!cmpbuf) {
542 fprintf(stderr, "Could not allocate memory!\n");
543 exit(1);
544 }
545 memset(cmpbuf, 0xff, len);
546 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
547 free(cmpbuf);
548 return ret;
549}
550
551/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000552 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
553 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000554 * @start offset to the base address of the flash chip
555 * @len length of the verified area
556 * @message string to print in the "FAILED" message
557 * @return 0 for success, -1 for failure
558 */
559int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
560{
561 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000562 int page_size = flash->page_size;
563 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000564 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000565
566 if (!len)
567 goto out_free;
568
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000569 if (!flash->read) {
570 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
571 return 1;
572 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000573 if (!readbuf) {
574 fprintf(stderr, "Could not allocate memory!\n");
575 exit(1);
576 }
577
578 if (start + len > flash->total_size * 1024) {
579 fprintf(stderr, "Error: %s called with start 0x%x + len 0x%x >"
580 " total_size 0x%x\n", __func__, start, len,
581 flash->total_size * 1024);
582 ret = -1;
583 goto out_free;
584 }
585 if (!message)
586 message = "VERIFY";
587
588 /* Warning: This loop has a very unusual condition and body.
589 * The loop needs to go through each page with at least one affected
590 * byte. The lowest page number is (start / page_size) since that
591 * division rounds down. The highest page number we want is the page
592 * where the last byte of the range lives. That last byte has the
593 * address (start + len - 1), thus the highest page number is
594 * (start + len - 1) / page_size. Since we want to include that last
595 * page as well, the loop condition uses <=.
596 */
597 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
598 /* Byte position of the first byte in the range in this page. */
599 starthere = max(start, i * page_size);
600 /* Length of bytes in the range in this page. */
601 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000602 flash->read(flash, readbuf, starthere, lenhere);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000603 for (j = 0; j < lenhere; j++) {
604 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000605 /* Only print the first failure. */
606 if (!failcount++)
607 fprintf(stderr, "%s FAILED at 0x%08x! "
608 "Expected=0x%02x, Read=0x%02x,",
609 message, starthere + j,
610 cmpbuf[starthere - start + j],
611 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000612 }
613 }
614 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000615 if (failcount) {
616 fprintf(stderr, " failed byte count from 0x%08x-0x%08x: 0x%x\n",
617 start, start + len - 1, failcount);
618 ret = -1;
619 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000620
621out_free:
622 free(readbuf);
623 return ret;
624}
625
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000626/**
627 * Check if the buffer @have can be programmed to the content of @want without
628 * erasing. This is only possible if all chunks of size @gran are either kept
629 * as-is or changed from an all-ones state to any other state.
630 * The following write granularities (enum @gran) are known:
631 * - 1 bit. Each bit can be cleared individually.
632 * - 1 byte. A byte can be written once. Further writes to an already written
633 * byte cause the contents to be either undefined or to stay unchanged.
634 * - 128 bytes. If less than 128 bytes are written, the rest will be
635 * erased. Each write to a 128-byte region will trigger an automatic erase
636 * before anything is written. Very uncommon behaviour and unsupported by
637 * this function.
638 * - 256 bytes. If less than 256 bytes are written, the contents of the
639 * unwritten bytes are undefined.
640 *
641 * @have buffer with current content
642 * @want buffer with desired content
643 * @len length of the verified area
644 * @gran write granularity (enum, not count)
645 * @return 0 if no erase is needed, 1 otherwise
646 */
647int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
648{
649 int result = 0;
650 int i, j, limit;
651
652 switch (gran) {
653 case write_gran_1bit:
654 for (i = 0; i < len; i++)
655 if ((have[i] & want[i]) != want[i]) {
656 result = 1;
657 break;
658 }
659 break;
660 case write_gran_1byte:
661 for (i = 0; i < len; i++)
662 if ((have[i] != want[i]) && (have[i] != 0xff)) {
663 result = 1;
664 break;
665 }
666 break;
667 case write_gran_256bytes:
668 for (j = 0; j < len / 256; j++) {
669 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000670 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000671 if (!memcmp(have + j * 256, want + j * 256, limit))
672 continue;
673 /* have needs to be in erased state. */
674 for (i = 0; i < limit; i++)
675 if (have[i] != 0xff) {
676 result = 1;
677 break;
678 }
679 if (result)
680 break;
681 }
682 break;
683 }
684 return result;
685}
686
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000687/* This function generates various test patterns useful for testing controller
688 * and chip communication as well as chip behaviour.
689 *
690 * If a byte can be written multiple times, each time keeping 0-bits at 0
691 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
692 * is essentially an AND operation. That's also the reason why this function
693 * provides the result of AND between various patterns.
694 *
695 * Below is a list of patterns (and their block length).
696 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
697 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
698 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
699 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
700 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
701 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
702 * Pattern 6 is 00 (1 Byte)
703 * Pattern 7 is ff (1 Byte)
704 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
705 * byte block.
706 *
707 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
708 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
709 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
710 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
711 * Pattern 12 is 00 (1 Byte)
712 * Pattern 13 is ff (1 Byte)
713 * Patterns 8-13 have no block number.
714 *
715 * Patterns 0-3 are created to detect and efficiently diagnose communication
716 * slips like missed bits or bytes and their repetitive nature gives good visual
717 * cues to the person inspecting the results. In addition, the following holds:
718 * AND Pattern 0/1 == Pattern 4
719 * AND Pattern 2/3 == Pattern 5
720 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
721 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
722 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
723 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
724 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
725 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
726 * Besides that, they provide for bit testing of the last two bytes of every
727 * 256 byte block which contains the block number for patterns 0-6.
728 * Patterns 10-11 are special purpose for detecting subblock aliasing with
729 * block sizes >256 bytes (some Dataflash chips etc.)
730 * AND Pattern 8/9 == Pattern 12
731 * AND Pattern 10/11 == Pattern 12
732 * Pattern 13 is the completely erased state.
733 * None of the patterns can detect aliasing at boundaries which are a multiple
734 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
735 */
736int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
737{
738 int i;
739
740 if (!buf) {
741 fprintf(stderr, "Invalid buffer!\n");
742 return 1;
743 }
744
745 switch (variant) {
746 case 0:
747 for (i = 0; i < size; i++)
748 buf[i] = (i & 0xf) << 4 | 0x5;
749 break;
750 case 1:
751 for (i = 0; i < size; i++)
752 buf[i] = (i & 0xf) << 4 | 0xa;
753 break;
754 case 2:
755 for (i = 0; i < size; i++)
756 buf[i] = 0x50 | (i & 0xf);
757 break;
758 case 3:
759 for (i = 0; i < size; i++)
760 buf[i] = 0xa0 | (i & 0xf);
761 break;
762 case 4:
763 for (i = 0; i < size; i++)
764 buf[i] = (i & 0xf) << 4;
765 break;
766 case 5:
767 for (i = 0; i < size; i++)
768 buf[i] = i & 0xf;
769 break;
770 case 6:
771 memset(buf, 0x00, size);
772 break;
773 case 7:
774 memset(buf, 0xff, size);
775 break;
776 case 8:
777 for (i = 0; i < size; i++)
778 buf[i] = i & 0xff;
779 break;
780 case 9:
781 for (i = 0; i < size; i++)
782 buf[i] = ~(i & 0xff);
783 break;
784 case 10:
785 for (i = 0; i < size % 2; i++) {
786 buf[i * 2] = (i >> 8) & 0xff;
787 buf[i * 2 + 1] = i & 0xff;
788 }
789 if (size & 0x1)
790 buf[i * 2] = (i >> 8) & 0xff;
791 break;
792 case 11:
793 for (i = 0; i < size % 2; i++) {
794 buf[i * 2] = ~((i >> 8) & 0xff);
795 buf[i * 2 + 1] = ~(i & 0xff);
796 }
797 if (size & 0x1)
798 buf[i * 2] = ~((i >> 8) & 0xff);
799 break;
800 case 12:
801 memset(buf, 0x00, size);
802 break;
803 case 13:
804 memset(buf, 0xff, size);
805 break;
806 }
807
808 if ((variant >= 0) && (variant <= 7)) {
809 /* Write block number in the last two bytes of each 256-byte
810 * block, big endian for easier reading of the hexdump.
811 * Note that this wraps around for chips larger than 2^24 bytes
812 * (16 MB).
813 */
814 for (i = 0; i < size / 256; i++) {
815 buf[i * 256 + 254] = (i >> 8) & 0xff;
816 buf[i * 256 + 255] = i & 0xff;
817 }
818 }
819
820 return 0;
821}
822
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000823int check_max_decode(enum chipbustype buses, uint32_t size)
824{
825 int limitexceeded = 0;
826 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
827 (max_rom_decode.parallel < size)) {
828 limitexceeded++;
829 printf_debug("Chip size %u kB is bigger than supported "
830 "size %u kB of chipset/board/programmer "
831 "for %s interface, "
832 "probe/read/erase/write may fail. ", size / 1024,
833 max_rom_decode.parallel / 1024, "Parallel");
834 }
835 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
836 limitexceeded++;
837 printf_debug("Chip size %u kB is bigger than supported "
838 "size %u kB of chipset/board/programmer "
839 "for %s interface, "
840 "probe/read/erase/write may fail. ", size / 1024,
841 max_rom_decode.lpc / 1024, "LPC");
842 }
843 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
844 limitexceeded++;
845 printf_debug("Chip size %u kB is bigger than supported "
846 "size %u kB of chipset/board/programmer "
847 "for %s interface, "
848 "probe/read/erase/write may fail. ", size / 1024,
849 max_rom_decode.fwh / 1024, "FWH");
850 }
851 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
852 limitexceeded++;
853 printf_debug("Chip size %u kB is bigger than supported "
854 "size %u kB of chipset/board/programmer "
855 "for %s interface, "
856 "probe/read/erase/write may fail. ", size / 1024,
857 max_rom_decode.spi / 1024, "SPI");
858 }
859 if (!limitexceeded)
860 return 0;
861 /* Sometimes chip and programmer have more than one bus in common,
862 * and the limit is not exceeded on all buses. Tell the user.
863 */
864 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000865 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000866 printf_debug("There is at least one common chip/programmer "
867 "interface which can support a chip of this size. "
868 "You can try --force at your own risk.\n");
869 return 1;
870}
871
Peter Stuge483b8f02008-09-03 23:10:05 +0000872struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000873{
Peter Stuge483b8f02008-09-03 23:10:05 +0000874 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000875 unsigned long base = 0;
876 uint32_t size;
877 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000878 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000879
Peter Stuge483b8f02008-09-03 23:10:05 +0000880 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000881 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000882 continue;
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000883 msg_gdbg("Probing for %s %s, %d KB: ",
Stefan Reinauerac378972008-03-17 22:59:40 +0000884 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000885 if (!flash->probe && !force) {
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000886 msg_gdbg("failed! flashrom has no probe function for "
887 "this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000888 continue;
889 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000890 buses_common = buses_supported & flash->bustype;
891 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000892 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000893 msg_gdbg("skipped.");
894 msg_gspew(" Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000895 free(tmp);
896 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000897 msg_gspew("and chip bus type %s are incompatible.",
898 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000899 free(tmp);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000900 msg_gdbg("\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000901 continue;
902 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000903
Ollie Lhocbbf1252004-03-17 22:22:08 +0000904 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000905 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000906
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000907 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000908 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000909
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000910 if (force)
911 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000912
Peter Stuge483b8f02008-09-03 23:10:05 +0000913 if (flash->probe(flash) != 1)
914 goto notfound;
915
Uwe Hermann394131e2008-10-18 21:14:13 +0000916 if (first_flash == flashchips
917 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000918 break;
919
Peter Stuge483b8f02008-09-03 23:10:05 +0000920notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000921 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000922 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000923
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000924 if (!flash || !flash->name)
925 return NULL;
926
Uwe Hermann9899cad2009-06-28 21:47:57 +0000927 printf("Found chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
928 flash->vendor, flash->name, flash->total_size,
929 flashbuses_to_text(flash->bustype), base);
930
Sean Nelson6e0b9122010-02-19 00:52:10 +0000931 if (flash->printlock)
932 flash->printlock(flash);
933
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000934 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000935}
936
Stefan Reinauere3705282005-12-18 16:41:10 +0000937int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000938{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000939 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000940 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000941
Uwe Hermanna502dce2007-10-17 23:55:15 +0000942 printf("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000943
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000944 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000945
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000946 if (!ret)
947 printf("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000948
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000949 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000950}
951
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +0000952int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000953{
954 unsigned long numbytes;
955 FILE *image;
956 unsigned long size = flash->total_size * 1024;
957 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000958
959 if (!filename) {
960 printf("Error: No filename specified.\n");
961 return 1;
962 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +0000963 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000964 perror(filename);
965 exit(1);
966 }
967 printf("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000968 if (!flash->read) {
969 printf("FAILED!\n");
970 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
971 return 1;
972 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000973 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000974
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000975 numbytes = fwrite(buf, 1, size, image);
976 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +0000977 free(buf);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000978 printf("%s.\n", numbytes == size ? "done" : "FAILED");
979 if (numbytes != size)
980 return 1;
981 return 0;
982}
983
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000984/* This function shares a lot of its structure with erase_flash().
985 * Even if an error is found, the function will keep going and check the rest.
986 */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000987int selfcheck_eraseblocks(struct flashchip *flash)
988{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000989 int i, j, k;
990 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000991
992 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
993 unsigned int done = 0;
994 struct block_eraser eraser = flash->block_erasers[k];
995
996 for (i = 0; i < NUM_ERASEREGIONS; i++) {
997 /* Blocks with zero size are bugs in flashchips.c. */
998 if (eraser.eraseblocks[i].count &&
999 !eraser.eraseblocks[i].size) {
1000 msg_gerr("ERROR: Flash chip %s erase function "
1001 "%i region %i has size 0. Please report"
1002 " a bug at flashrom@flashrom.org\n",
1003 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001004 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001005 }
1006 /* Blocks with zero count are bugs in flashchips.c. */
1007 if (!eraser.eraseblocks[i].count &&
1008 eraser.eraseblocks[i].size) {
1009 msg_gerr("ERROR: Flash chip %s erase function "
1010 "%i region %i has count 0. Please report"
1011 " a bug at flashrom@flashrom.org\n",
1012 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001013 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001014 }
1015 done += eraser.eraseblocks[i].count *
1016 eraser.eraseblocks[i].size;
1017 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001018 /* Empty eraseblock definition with erase function. */
1019 if (!done && eraser.block_erase)
1020 msg_pspew("Strange: Empty eraseblock definition with "
1021 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001022 if (!done)
1023 continue;
1024 if (done != flash->total_size * 1024) {
1025 msg_gerr("ERROR: Flash chip %s erase function %i "
1026 "region walking resulted in 0x%06x bytes total,"
1027 " expected 0x%06x bytes. Please report a bug at"
1028 " flashrom@flashrom.org\n", flash->name, k,
1029 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001030 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001031 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001032 if (!eraser.block_erase)
1033 continue;
1034 /* Check if there are identical erase functions for different
1035 * layouts. That would imply "magic" erase functions. The
1036 * easiest way to check this is with function pointers.
1037 */
Uwe Hermann43959702010-03-13 17:28:29 +00001038 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001039 if (eraser.block_erase ==
1040 flash->block_erasers[j].block_erase) {
1041 msg_gerr("ERROR: Flash chip %s erase function "
1042 "%i and %i are identical. Please report"
1043 " a bug at flashrom@flashrom.org\n",
1044 flash->name, k, j);
1045 ret = 1;
1046 }
Uwe Hermann43959702010-03-13 17:28:29 +00001047 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001048 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001049 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001050}
1051
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001052int erase_flash(struct flashchip *flash)
1053{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001054 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001055 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001056
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001057 printf("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001058 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001059 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001060 struct block_eraser eraser = flash->block_erasers[k];
1061
1062 printf_debug("Looking at blockwise erase function %i... ", k);
1063 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1064 printf_debug("not defined. "
1065 "Looking for another erase function.\n");
1066 continue;
1067 }
1068 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1069 printf_debug("eraseblock layout is known, but no "
1070 "matching block erase function found. "
1071 "Looking for another erase function.\n");
1072 continue;
1073 }
1074 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1075 printf_debug("block erase function found, but "
1076 "eraseblock layout is unknown. "
1077 "Looking for another erase function.\n");
1078 continue;
1079 }
1080 found = 1;
1081 printf_debug("trying... ");
1082 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1083 /* count==0 for all automatically initialized array
1084 * members so the loop below won't be executed for them.
1085 */
1086 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001087 start = done + eraser.eraseblocks[i].size * j;
1088 len = eraser.eraseblocks[i].size;
1089 printf_debug("0x%06x-0x%06x, ", start,
1090 start + len - 1);
1091 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001092 if (ret)
1093 break;
1094 }
1095 if (ret)
1096 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001097 done += eraser.eraseblocks[i].count *
1098 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001099 }
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001100 printf_debug("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001101 /* If everything is OK, don't try another erase function. */
1102 if (!ret)
1103 break;
1104 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001105 if (!found) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001106 fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
1107 return 1;
1108 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001109
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001110 if (ret) {
1111 fprintf(stderr, "FAILED!\n");
1112 } else {
1113 printf("SUCCESS.\n");
1114 }
1115 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001116}
1117
Uwe Hermannc67d0372009-10-01 18:40:02 +00001118void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001119{
1120 fprintf(stderr, "Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001121 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
1122 "mail flashrom@flashrom.org!\n--------------------"
1123 "-----------------------------------------------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001124 "DO NOT REBOOT OR POWEROFF!\n");
1125}
1126
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001127/* The way to go if you want a delimited list of programmers*/
1128void list_programmers(char *delim)
1129{
1130 enum programmer p;
1131 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1132 printf("%s", programmer_table[p].name);
1133 if (p < PROGRAMMER_INVALID - 1)
1134 printf("%s", delim);
1135 }
1136 printf("\n");
1137}
1138
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001139void print_sysinfo(void)
1140{
1141#if HAVE_UTSNAME == 1
1142 struct utsname osinfo;
1143 uname(&osinfo);
1144
1145 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1146 osinfo.machine);
1147#else
1148 msg_ginfo(" on unknown machine");
1149#endif
1150 msg_ginfo(", built with");
1151#if NEED_PCI == 1
1152#ifdef PCILIB_VERSION
1153 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1154#else
1155 msg_ginfo(" unknown PCI library,");
1156#endif
1157#endif
1158#ifdef __clang__
1159 msg_ginfo(" LLVM %i/clang %i", __llvm__, __clang__);
1160#elif defined(__GNUC__)
1161 msg_ginfo(" GCC");
1162#ifdef __VERSION__
1163 msg_ginfo(" %s", __VERSION__);
1164#else
1165 msg_ginfo(" unknown version");
1166#endif
1167#else
1168 msg_ginfo(" unknown compiler");
1169#endif
1170 msg_ginfo("\n");
1171}
1172
Bernhard Walle201bde32008-01-21 15:24:22 +00001173void print_version(void)
1174{
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001175 printf("flashrom v%s", flashrom_version);
1176 print_sysinfo();
Bernhard Walle201bde32008-01-21 15:24:22 +00001177}
1178
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001179int selfcheck(void)
1180{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001181 int ret = 0;
1182 struct flashchip *flash;
1183
1184 /* Safety check. Instead of aborting after the first error, check
1185 * if more errors exist.
1186 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001187 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
1188 fprintf(stderr, "Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001189 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001190 }
1191 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
1192 fprintf(stderr, "SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001193 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001194 }
1195#if BITBANG_SPI_SUPPORT == 1
1196 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
1197 fprintf(stderr, "Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001198 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001199 }
1200#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001201 for (flash = flashchips; flash && flash->name; flash++)
1202 if (selfcheck_eraseblocks(flash))
1203 ret = 1;
1204 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001205}
1206
1207void check_chip_supported(struct flashchip *flash)
1208{
1209 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
1210 printf("===\n");
1211 if (flash->tested & TEST_BAD_MASK) {
1212 printf("This flash part has status NOT WORKING for operations:");
1213 if (flash->tested & TEST_BAD_PROBE)
1214 printf(" PROBE");
1215 if (flash->tested & TEST_BAD_READ)
1216 printf(" READ");
1217 if (flash->tested & TEST_BAD_ERASE)
1218 printf(" ERASE");
1219 if (flash->tested & TEST_BAD_WRITE)
1220 printf(" WRITE");
1221 printf("\n");
1222 }
1223 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1224 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1225 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1226 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
1227 printf("This flash part has status UNTESTED for operations:");
1228 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
1229 printf(" PROBE");
1230 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
1231 printf(" READ");
1232 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
1233 printf(" ERASE");
1234 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
1235 printf(" WRITE");
1236 printf("\n");
1237 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001238 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001239 printf("Please email a report to flashrom@flashrom.org if any "
1240 "of the above operations\nwork correctly for you with "
1241 "this flash part. Please include the flashrom\noutput "
1242 "with the additional -V option for all operations you "
1243 "tested (-V, -rV,\n-wV, -EV), and mention which "
Uwe Hermann43959702010-03-13 17:28:29 +00001244 "mainboard or programmer you tested.\nThanks for your "
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001245 "help!\n===\n");
1246 }
1247}
1248
Ollie Lho761bf1b2004-03-20 16:46:10 +00001249int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001250{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001251 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001252}
1253
1254/* This function signature is horrible. We need to design a better interface,
1255 * but right now it allows us to split off the CLI code.
1256 */
1257int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1258{
1259 uint8_t *buf;
1260 unsigned long numbytes;
1261 FILE *image;
1262 int ret = 0;
1263 unsigned long size;
1264
1265 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001266 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001267
Ollie Lhoefa28582004-12-08 20:10:01 +00001268 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001269 if (flash->tested & TEST_BAD_ERASE) {
1270 fprintf(stderr, "Erase is not working on this chip. ");
1271 if (!force) {
1272 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001273 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001274 return 1;
1275 } else {
1276 fprintf(stderr, "Continuing anyway.\n");
1277 }
1278 }
Sean Nelson6e0b9122010-02-19 00:52:10 +00001279 if (flash->unlock)
1280 flash->unlock(flash);
1281
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001282 if (erase_flash(flash)) {
1283 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001284 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001285 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001286 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001287 } else if (read_it) {
Sean Nelson6e0b9122010-02-19 00:52:10 +00001288 if (flash->unlock)
1289 flash->unlock(flash);
1290
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001291 if (read_flash(flash, filename)) {
1292 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001293 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001294 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001295 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001296 struct stat image_stat;
1297
Sean Nelson6e0b9122010-02-19 00:52:10 +00001298 if (flash->unlock)
1299 flash->unlock(flash);
1300
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001301 if (flash->tested & TEST_BAD_ERASE) {
1302 fprintf(stderr, "Erase is not working on this chip "
1303 "and erase is needed for write. ");
1304 if (!force) {
1305 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001306 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001307 return 1;
1308 } else {
1309 fprintf(stderr, "Continuing anyway.\n");
1310 }
1311 }
1312 if (flash->tested & TEST_BAD_WRITE) {
1313 fprintf(stderr, "Write is not working on this chip. ");
1314 if (!force) {
1315 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001316 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001317 return 1;
1318 } else {
1319 fprintf(stderr, "Continuing anyway.\n");
1320 }
1321 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001322 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001323 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001324 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001325 exit(1);
1326 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001327 if (fstat(fileno(image), &image_stat) != 0) {
1328 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001329 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001330 exit(1);
1331 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001332 if (image_stat.st_size != flash->total_size * 1024) {
Uwe Hermann793bdcd2008-05-22 22:47:04 +00001333 fprintf(stderr, "Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001334 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001335 exit(1);
1336 }
1337
Peter Stuge1fec0f32009-01-12 21:00:35 +00001338 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001339#if INTERNAL_SUPPORT == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001340 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001341#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001342 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001343 if (numbytes != size) {
1344 fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001345 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001346 return 1;
1347 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001348 }
1349
Ollie Lho184a4042005-11-26 21:55:36 +00001350 // This should be moved into each flash part's code to do it
1351 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001352 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001353
Ollie Lho184a4042005-11-26 21:55:36 +00001354 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001355
Peter Stugef31104c2008-04-28 14:47:30 +00001356 if (write_it) {
Paul Foxd51410c2009-06-12 08:04:08 +00001357 printf("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001358 if (!flash->write) {
1359 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001360 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001361 return 1;
1362 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001363 ret = flash->write(flash, buf);
1364 if (ret) {
1365 fprintf(stderr, "FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001366 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001367 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001368 return 1;
1369 } else {
1370 printf("COMPLETE.\n");
1371 }
Peter Stugef31104c2008-04-28 14:47:30 +00001372 }
Ollie Lho184a4042005-11-26 21:55:36 +00001373
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001374 if (verify_it) {
1375 /* Work around chips which need some time to calm down. */
1376 if (write_it)
1377 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001378 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001379 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001380 * might have an emergency situation.
1381 */
1382 if (ret && write_it)
1383 emergency_help_message();
1384 }
Ollie Lho184a4042005-11-26 21:55:36 +00001385
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001386 programmer_shutdown();
1387
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001388 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001389}