blob: f5c0721f9d6058b10ff407f755ebc3d4d1f210ce [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>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000030#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000031#include "flashchips.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000032
Carl-Daniel Hailfingera80cfbc2009-07-22 20:13:00 +000033const char *flashrom_version = FLASHROM_VERSION;
Ronald G. Minnichceec4202003-07-25 04:37:41 +000034char *chip_to_probe = NULL;
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000035int verbose = 0;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000036
37#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +000038enum programmer programmer = PROGRAMMER_INTERNAL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000039#elif DUMMY_SUPPORT == 1
40enum programmer programmer = PROGRAMMER_DUMMY;
41#else
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000042/* If neither internal nor dummy are selected, we must pick a sensible default.
43 * Since there is no reason to prefer a particular external programmer, we fail
44 * if more than one of them is selected. If only one is selected, it is clear
45 * that the user wants that one to become the default.
46 */
Uwe Hermannddd5c9e2010-02-21 21:17:00 +000047#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 +000048#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all external programmers except one.
49#endif
50enum programmer programmer =
51#if NIC3COM_SUPPORT == 1
52 PROGRAMMER_NIC3COM
53#endif
54#if GFXNVIDIA_SUPPORT == 1
55 PROGRAMMER_GFXNVIDIA
56#endif
57#if DRKAISER_SUPPORT == 1
58 PROGRAMMER_DRKAISER
59#endif
60#if SATASII_SUPPORT == 1
61 PROGRAMMER_SATASII
62#endif
Uwe Hermannddd5c9e2010-02-21 21:17:00 +000063#if ATAHPT_SUPPORT == 1
64 PROGRAMMER_ATAHPT
65#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000066#if FT2232_SPI_SUPPORT == 1
67 PROGRAMMER_FT2232SPI
68#endif
69#if SERPROG_SUPPORT == 1
70 PROGRAMMER_SERPROG
71#endif
72#if BUSPIRATE_SPI_SUPPORT == 1
73 PROGRAMMER_BUSPIRATESPI
74#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000075#if DEDIPROG_SUPPORT == 1
76 PROGRAMMER_DEDIPROG
77#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000078;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000079#endif
80
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000081char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000082
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000083/**
84 * flashrom defaults to Parallel/LPC/FWH flash devices. If a known host
85 * controller is found, the init routine sets the buses_supported bitfield to
86 * contain the supported buses for that controller.
87 */
88enum chipbustype buses_supported = CHIP_BUSTYPE_NONSPI;
89
90/**
91 * Programmers supporting multiple buses can have differing size limits on
92 * each bus. Store the limits for each bus in a common struct.
93 */
94struct decode_sizes max_rom_decode = {
95 .parallel = 0xffffffff,
96 .lpc = 0xffffffff,
97 .fwh = 0xffffffff,
98 .spi = 0xffffffff
99};
100
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000101const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000102#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000103 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000104 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000105 .init = internal_init,
106 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000107 .map_flash_region = physmap,
108 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000109 .chip_readb = internal_chip_readb,
110 .chip_readw = internal_chip_readw,
111 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000112 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000113 .chip_writeb = internal_chip_writeb,
114 .chip_writew = internal_chip_writew,
115 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000116 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000117 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000118 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000119#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000120
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000121#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000122 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000123 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000124 .init = dummy_init,
125 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000126 .map_flash_region = dummy_map,
127 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000128 .chip_readb = dummy_chip_readb,
129 .chip_readw = dummy_chip_readw,
130 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000131 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000132 .chip_writeb = dummy_chip_writeb,
133 .chip_writew = dummy_chip_writew,
134 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000135 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000136 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000137 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000138#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000139
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000140#if NIC3COM_SUPPORT == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000141 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000142 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000143 .init = nic3com_init,
144 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000145 .map_flash_region = fallback_map,
146 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000147 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000148 .chip_readw = fallback_chip_readw,
149 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000150 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000151 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000152 .chip_writew = fallback_chip_writew,
153 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000154 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000155 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000156 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000157#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000158
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000159#if GFXNVIDIA_SUPPORT == 1
160 {
161 .name = "gfxnvidia",
162 .init = gfxnvidia_init,
163 .shutdown = gfxnvidia_shutdown,
164 .map_flash_region = fallback_map,
165 .unmap_flash_region = fallback_unmap,
166 .chip_readb = gfxnvidia_chip_readb,
167 .chip_readw = fallback_chip_readw,
168 .chip_readl = fallback_chip_readl,
169 .chip_readn = fallback_chip_readn,
170 .chip_writeb = gfxnvidia_chip_writeb,
171 .chip_writew = fallback_chip_writew,
172 .chip_writel = fallback_chip_writel,
173 .chip_writen = fallback_chip_writen,
174 .delay = internal_delay,
175 },
176#endif
177
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000178#if DRKAISER_SUPPORT == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000179 {
TURBO Jb0912c02009-09-02 23:00:46 +0000180 .name = "drkaiser",
181 .init = drkaiser_init,
182 .shutdown = drkaiser_shutdown,
183 .map_flash_region = fallback_map,
184 .unmap_flash_region = fallback_unmap,
185 .chip_readb = drkaiser_chip_readb,
186 .chip_readw = fallback_chip_readw,
187 .chip_readl = fallback_chip_readl,
188 .chip_readn = fallback_chip_readn,
189 .chip_writeb = drkaiser_chip_writeb,
190 .chip_writew = fallback_chip_writew,
191 .chip_writel = fallback_chip_writel,
192 .chip_writen = fallback_chip_writen,
193 .delay = internal_delay,
194 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000195#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000196
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000197#if SATASII_SUPPORT == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000198 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000199 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000200 .init = satasii_init,
201 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000202 .map_flash_region = fallback_map,
203 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000204 .chip_readb = satasii_chip_readb,
205 .chip_readw = fallback_chip_readw,
206 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000207 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000208 .chip_writeb = satasii_chip_writeb,
209 .chip_writew = fallback_chip_writew,
210 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000211 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000212 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000213 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000214#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000215
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000216#if ATAHPT_SUPPORT == 1
217 {
218 .name = "atahpt",
219 .init = atahpt_init,
220 .shutdown = atahpt_shutdown,
221 .map_flash_region = fallback_map,
222 .unmap_flash_region = fallback_unmap,
223 .chip_readb = atahpt_chip_readb,
224 .chip_readw = fallback_chip_readw,
225 .chip_readl = fallback_chip_readl,
226 .chip_readn = fallback_chip_readn,
227 .chip_writeb = atahpt_chip_writeb,
228 .chip_writew = fallback_chip_writew,
229 .chip_writel = fallback_chip_writel,
230 .chip_writen = fallback_chip_writen,
231 .delay = internal_delay,
232 },
233#endif
234
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000235#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000236 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000237 .name = "it87spi",
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000238 .init = it87spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000239 .shutdown = noop_shutdown,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000240 .map_flash_region = fallback_map,
241 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000242 .chip_readb = noop_chip_readb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000243 .chip_readw = fallback_chip_readw,
244 .chip_readl = fallback_chip_readl,
245 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000246 .chip_writeb = noop_chip_writeb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000247 .chip_writew = fallback_chip_writew,
248 .chip_writel = fallback_chip_writel,
249 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000250 .delay = internal_delay,
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000251 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000252#endif
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000253
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000254#if FT2232_SPI_SUPPORT == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000255 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000256 .name = "ft2232spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000257 .init = ft2232_spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000258 .shutdown = noop_shutdown, /* Missing shutdown */
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000259 .map_flash_region = fallback_map,
260 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000261 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000262 .chip_readw = fallback_chip_readw,
263 .chip_readl = fallback_chip_readl,
264 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000265 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000266 .chip_writew = fallback_chip_writew,
267 .chip_writel = fallback_chip_writel,
268 .chip_writen = fallback_chip_writen,
269 .delay = internal_delay,
270 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000271#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000272
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000273#if SERPROG_SUPPORT == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000274 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000275 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000276 .init = serprog_init,
277 .shutdown = serprog_shutdown,
278 .map_flash_region = fallback_map,
279 .unmap_flash_region = fallback_unmap,
280 .chip_readb = serprog_chip_readb,
281 .chip_readw = fallback_chip_readw,
282 .chip_readl = fallback_chip_readl,
283 .chip_readn = serprog_chip_readn,
284 .chip_writeb = serprog_chip_writeb,
285 .chip_writew = fallback_chip_writew,
286 .chip_writel = fallback_chip_writel,
287 .chip_writen = fallback_chip_writen,
288 .delay = serprog_delay,
289 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000290#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000291
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000292#if BUSPIRATE_SPI_SUPPORT == 1
293 {
294 .name = "buspiratespi",
295 .init = buspirate_spi_init,
296 .shutdown = buspirate_spi_shutdown,
297 .map_flash_region = fallback_map,
298 .unmap_flash_region = fallback_unmap,
299 .chip_readb = noop_chip_readb,
300 .chip_readw = fallback_chip_readw,
301 .chip_readl = fallback_chip_readl,
302 .chip_readn = fallback_chip_readn,
303 .chip_writeb = noop_chip_writeb,
304 .chip_writew = fallback_chip_writew,
305 .chip_writel = fallback_chip_writel,
306 .chip_writen = fallback_chip_writen,
307 .delay = internal_delay,
308 },
309#endif
310
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000311#if DEDIPROG_SUPPORT == 1
312 {
313 .name = "dediprog",
314 .init = dediprog_init,
315 .shutdown = dediprog_shutdown,
316 .map_flash_region = fallback_map,
317 .unmap_flash_region = fallback_unmap,
318 .chip_readb = noop_chip_readb,
319 .chip_readw = fallback_chip_readw,
320 .chip_readl = fallback_chip_readl,
321 .chip_readn = fallback_chip_readn,
322 .chip_writeb = noop_chip_writeb,
323 .chip_writew = fallback_chip_writew,
324 .chip_writel = fallback_chip_writel,
325 .chip_writen = fallback_chip_writen,
326 .delay = internal_delay,
327 },
328#endif
329
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000330 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000331};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000332
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000333#define SHUTDOWN_MAXFN 4
334static int shutdown_fn_count = 0;
335struct shutdown_func_data {
336 void (*func) (void *data);
337 void *data;
338} shutdown_fn[SHUTDOWN_MAXFN];
339
340/* Register a function to be executed on programmer shutdown.
341 * The advantage over atexit() is that you can supply a void pointer which will
342 * be used as parameter to the registered function upon programmer shutdown.
343 * This pointer can point to arbitrary data used by said function, e.g. undo
344 * information for GPIO settings etc. If unneeded, set data=NULL.
345 * Please note that the first (void *data) belongs to the function signature of
346 * the function passed as first parameter.
347 */
348int register_shutdown(void (*function) (void *data), void *data)
349{
350 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
351 msg_perr("Tried to register more than %n shutdown functions.\n",
352 SHUTDOWN_MAXFN);
353 return 1;
354 }
355 shutdown_fn[shutdown_fn_count].func = function;
356 shutdown_fn[shutdown_fn_count].data = data;
357 shutdown_fn_count++;
358
359 return 0;
360}
361
Uwe Hermann09e04f72009-05-16 22:36:00 +0000362int programmer_init(void)
363{
364 return programmer_table[programmer].init();
365}
366
367int programmer_shutdown(void)
368{
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000369 int i;
370
371 for (i = shutdown_fn_count - 1; i >= 0; i--)
372 shutdown_fn[i].func(shutdown_fn[i].data);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000373 return programmer_table[programmer].shutdown();
374}
375
376void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
377 size_t len)
378{
379 return programmer_table[programmer].map_flash_region(descr,
380 phys_addr, len);
381}
382
383void programmer_unmap_flash_region(void *virt_addr, size_t len)
384{
385 programmer_table[programmer].unmap_flash_region(virt_addr, len);
386}
387
388void chip_writeb(uint8_t val, chipaddr addr)
389{
390 programmer_table[programmer].chip_writeb(val, addr);
391}
392
393void chip_writew(uint16_t val, chipaddr addr)
394{
395 programmer_table[programmer].chip_writew(val, addr);
396}
397
398void chip_writel(uint32_t val, chipaddr addr)
399{
400 programmer_table[programmer].chip_writel(val, addr);
401}
402
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000403void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
404{
405 programmer_table[programmer].chip_writen(buf, addr, len);
406}
407
Uwe Hermann09e04f72009-05-16 22:36:00 +0000408uint8_t chip_readb(const chipaddr addr)
409{
410 return programmer_table[programmer].chip_readb(addr);
411}
412
413uint16_t chip_readw(const chipaddr addr)
414{
415 return programmer_table[programmer].chip_readw(addr);
416}
417
418uint32_t chip_readl(const chipaddr addr)
419{
420 return programmer_table[programmer].chip_readl(addr);
421}
422
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000423void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
424{
425 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000426}
427
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000428void programmer_delay(int usecs)
429{
430 programmer_table[programmer].delay(usecs);
431}
432
Peter Stuge776d2022009-01-26 00:39:57 +0000433void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000434{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000435 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000436 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000437 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000438 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000439}
440
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000441int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000442{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000443 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000444
445 return 0;
446}
447
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000448unsigned long flashbase = 0;
449
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000450int min(int a, int b)
451{
452 return (a < b) ? a : b;
453}
454
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000455int max(int a, int b)
456{
457 return (a > b) ? a : b;
458}
459
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000460int bitcount(unsigned long a)
461{
462 int i = 0;
463 for (; a != 0; a >>= 1)
464 if (a & 1)
465 i++;
466 return i;
467}
468
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000469char *strcat_realloc(char *dest, const char *src)
470{
471 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
472 if (!dest)
473 return NULL;
474 strcat(dest, src);
475 return dest;
476}
477
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000478/* This is a somewhat hacked function similar in some ways to strtok().
479 * It will look for needle in haystack, return a copy of needle and remove
480 * everything from the first occurrence of needle to the next delimiter
481 * from haystack.
482 */
483char *extract_param(char **haystack, char *needle, char *delim)
484{
485 char *param_pos, *rest, *tmp;
486 char *dev = NULL;
487 int devlen;
488
489 param_pos = strstr(*haystack, needle);
490 do {
491 if (!param_pos)
492 return NULL;
493 /* Beginning of the string? */
494 if (param_pos == *haystack)
495 break;
496 /* After a delimiter? */
497 if (strchr(delim, *(param_pos - 1)))
498 break;
499 /* Continue searching. */
500 param_pos++;
501 param_pos = strstr(param_pos, needle);
502 } while (1);
503
504 if (param_pos) {
505 param_pos += strlen(needle);
506 devlen = strcspn(param_pos, delim);
507 if (devlen) {
508 dev = malloc(devlen + 1);
509 if (!dev) {
510 fprintf(stderr, "Out of memory!\n");
511 exit(1);
512 }
513 strncpy(dev, param_pos, devlen);
514 dev[devlen] = '\0';
515 }
516 rest = param_pos + devlen;
517 rest += strspn(rest, delim);
518 param_pos -= strlen(needle);
519 memmove(param_pos, rest, strlen(rest) + 1);
520 tmp = realloc(*haystack, strlen(*haystack) + 1);
521 if (!tmp) {
522 fprintf(stderr, "Out of memory!\n");
523 exit(1);
524 }
525 *haystack = tmp;
526 }
527
528
529 return dev;
530}
531
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000532/* start is an offset to the base address of the flash chip */
533int check_erased_range(struct flashchip *flash, int start, int len)
534{
535 int ret;
536 uint8_t *cmpbuf = malloc(len);
537
538 if (!cmpbuf) {
539 fprintf(stderr, "Could not allocate memory!\n");
540 exit(1);
541 }
542 memset(cmpbuf, 0xff, len);
543 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
544 free(cmpbuf);
545 return ret;
546}
547
548/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000549 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
550 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000551 * @start offset to the base address of the flash chip
552 * @len length of the verified area
553 * @message string to print in the "FAILED" message
554 * @return 0 for success, -1 for failure
555 */
556int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
557{
558 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000559 int page_size = flash->page_size;
560 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000561 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000562
563 if (!len)
564 goto out_free;
565
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000566 if (!flash->read) {
567 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
568 return 1;
569 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000570 if (!readbuf) {
571 fprintf(stderr, "Could not allocate memory!\n");
572 exit(1);
573 }
574
575 if (start + len > flash->total_size * 1024) {
576 fprintf(stderr, "Error: %s called with start 0x%x + len 0x%x >"
577 " total_size 0x%x\n", __func__, start, len,
578 flash->total_size * 1024);
579 ret = -1;
580 goto out_free;
581 }
582 if (!message)
583 message = "VERIFY";
584
585 /* Warning: This loop has a very unusual condition and body.
586 * The loop needs to go through each page with at least one affected
587 * byte. The lowest page number is (start / page_size) since that
588 * division rounds down. The highest page number we want is the page
589 * where the last byte of the range lives. That last byte has the
590 * address (start + len - 1), thus the highest page number is
591 * (start + len - 1) / page_size. Since we want to include that last
592 * page as well, the loop condition uses <=.
593 */
594 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
595 /* Byte position of the first byte in the range in this page. */
596 starthere = max(start, i * page_size);
597 /* Length of bytes in the range in this page. */
598 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000599 flash->read(flash, readbuf, starthere, lenhere);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000600 for (j = 0; j < lenhere; j++) {
601 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000602 /* Only print the first failure. */
603 if (!failcount++)
604 fprintf(stderr, "%s FAILED at 0x%08x! "
605 "Expected=0x%02x, Read=0x%02x,",
606 message, starthere + j,
607 cmpbuf[starthere - start + j],
608 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000609 }
610 }
611 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000612 if (failcount) {
613 fprintf(stderr, " failed byte count from 0x%08x-0x%08x: 0x%x\n",
614 start, start + len - 1, failcount);
615 ret = -1;
616 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000617
618out_free:
619 free(readbuf);
620 return ret;
621}
622
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000623/**
624 * Check if the buffer @have can be programmed to the content of @want without
625 * erasing. This is only possible if all chunks of size @gran are either kept
626 * as-is or changed from an all-ones state to any other state.
627 * The following write granularities (enum @gran) are known:
628 * - 1 bit. Each bit can be cleared individually.
629 * - 1 byte. A byte can be written once. Further writes to an already written
630 * byte cause the contents to be either undefined or to stay unchanged.
631 * - 128 bytes. If less than 128 bytes are written, the rest will be
632 * erased. Each write to a 128-byte region will trigger an automatic erase
633 * before anything is written. Very uncommon behaviour and unsupported by
634 * this function.
635 * - 256 bytes. If less than 256 bytes are written, the contents of the
636 * unwritten bytes are undefined.
637 *
638 * @have buffer with current content
639 * @want buffer with desired content
640 * @len length of the verified area
641 * @gran write granularity (enum, not count)
642 * @return 0 if no erase is needed, 1 otherwise
643 */
644int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
645{
646 int result = 0;
647 int i, j, limit;
648
649 switch (gran) {
650 case write_gran_1bit:
651 for (i = 0; i < len; i++)
652 if ((have[i] & want[i]) != want[i]) {
653 result = 1;
654 break;
655 }
656 break;
657 case write_gran_1byte:
658 for (i = 0; i < len; i++)
659 if ((have[i] != want[i]) && (have[i] != 0xff)) {
660 result = 1;
661 break;
662 }
663 break;
664 case write_gran_256bytes:
665 for (j = 0; j < len / 256; j++) {
666 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000667 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000668 if (!memcmp(have + j * 256, want + j * 256, limit))
669 continue;
670 /* have needs to be in erased state. */
671 for (i = 0; i < limit; i++)
672 if (have[i] != 0xff) {
673 result = 1;
674 break;
675 }
676 if (result)
677 break;
678 }
679 break;
680 }
681 return result;
682}
683
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000684/* This function generates various test patterns useful for testing controller
685 * and chip communication as well as chip behaviour.
686 *
687 * If a byte can be written multiple times, each time keeping 0-bits at 0
688 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
689 * is essentially an AND operation. That's also the reason why this function
690 * provides the result of AND between various patterns.
691 *
692 * Below is a list of patterns (and their block length).
693 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
694 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
695 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
696 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
697 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
698 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
699 * Pattern 6 is 00 (1 Byte)
700 * Pattern 7 is ff (1 Byte)
701 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
702 * byte block.
703 *
704 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
705 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
706 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
707 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
708 * Pattern 12 is 00 (1 Byte)
709 * Pattern 13 is ff (1 Byte)
710 * Patterns 8-13 have no block number.
711 *
712 * Patterns 0-3 are created to detect and efficiently diagnose communication
713 * slips like missed bits or bytes and their repetitive nature gives good visual
714 * cues to the person inspecting the results. In addition, the following holds:
715 * AND Pattern 0/1 == Pattern 4
716 * AND Pattern 2/3 == Pattern 5
717 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
718 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
719 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
720 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
721 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
722 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
723 * Besides that, they provide for bit testing of the last two bytes of every
724 * 256 byte block which contains the block number for patterns 0-6.
725 * Patterns 10-11 are special purpose for detecting subblock aliasing with
726 * block sizes >256 bytes (some Dataflash chips etc.)
727 * AND Pattern 8/9 == Pattern 12
728 * AND Pattern 10/11 == Pattern 12
729 * Pattern 13 is the completely erased state.
730 * None of the patterns can detect aliasing at boundaries which are a multiple
731 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
732 */
733int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
734{
735 int i;
736
737 if (!buf) {
738 fprintf(stderr, "Invalid buffer!\n");
739 return 1;
740 }
741
742 switch (variant) {
743 case 0:
744 for (i = 0; i < size; i++)
745 buf[i] = (i & 0xf) << 4 | 0x5;
746 break;
747 case 1:
748 for (i = 0; i < size; i++)
749 buf[i] = (i & 0xf) << 4 | 0xa;
750 break;
751 case 2:
752 for (i = 0; i < size; i++)
753 buf[i] = 0x50 | (i & 0xf);
754 break;
755 case 3:
756 for (i = 0; i < size; i++)
757 buf[i] = 0xa0 | (i & 0xf);
758 break;
759 case 4:
760 for (i = 0; i < size; i++)
761 buf[i] = (i & 0xf) << 4;
762 break;
763 case 5:
764 for (i = 0; i < size; i++)
765 buf[i] = i & 0xf;
766 break;
767 case 6:
768 memset(buf, 0x00, size);
769 break;
770 case 7:
771 memset(buf, 0xff, size);
772 break;
773 case 8:
774 for (i = 0; i < size; i++)
775 buf[i] = i & 0xff;
776 break;
777 case 9:
778 for (i = 0; i < size; i++)
779 buf[i] = ~(i & 0xff);
780 break;
781 case 10:
782 for (i = 0; i < size % 2; i++) {
783 buf[i * 2] = (i >> 8) & 0xff;
784 buf[i * 2 + 1] = i & 0xff;
785 }
786 if (size & 0x1)
787 buf[i * 2] = (i >> 8) & 0xff;
788 break;
789 case 11:
790 for (i = 0; i < size % 2; i++) {
791 buf[i * 2] = ~((i >> 8) & 0xff);
792 buf[i * 2 + 1] = ~(i & 0xff);
793 }
794 if (size & 0x1)
795 buf[i * 2] = ~((i >> 8) & 0xff);
796 break;
797 case 12:
798 memset(buf, 0x00, size);
799 break;
800 case 13:
801 memset(buf, 0xff, size);
802 break;
803 }
804
805 if ((variant >= 0) && (variant <= 7)) {
806 /* Write block number in the last two bytes of each 256-byte
807 * block, big endian for easier reading of the hexdump.
808 * Note that this wraps around for chips larger than 2^24 bytes
809 * (16 MB).
810 */
811 for (i = 0; i < size / 256; i++) {
812 buf[i * 256 + 254] = (i >> 8) & 0xff;
813 buf[i * 256 + 255] = i & 0xff;
814 }
815 }
816
817 return 0;
818}
819
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000820int check_max_decode(enum chipbustype buses, uint32_t size)
821{
822 int limitexceeded = 0;
823 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
824 (max_rom_decode.parallel < size)) {
825 limitexceeded++;
826 printf_debug("Chip size %u kB is bigger than supported "
827 "size %u kB of chipset/board/programmer "
828 "for %s interface, "
829 "probe/read/erase/write may fail. ", size / 1024,
830 max_rom_decode.parallel / 1024, "Parallel");
831 }
832 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
833 limitexceeded++;
834 printf_debug("Chip size %u kB is bigger than supported "
835 "size %u kB of chipset/board/programmer "
836 "for %s interface, "
837 "probe/read/erase/write may fail. ", size / 1024,
838 max_rom_decode.lpc / 1024, "LPC");
839 }
840 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
841 limitexceeded++;
842 printf_debug("Chip size %u kB is bigger than supported "
843 "size %u kB of chipset/board/programmer "
844 "for %s interface, "
845 "probe/read/erase/write may fail. ", size / 1024,
846 max_rom_decode.fwh / 1024, "FWH");
847 }
848 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
849 limitexceeded++;
850 printf_debug("Chip size %u kB is bigger than supported "
851 "size %u kB of chipset/board/programmer "
852 "for %s interface, "
853 "probe/read/erase/write may fail. ", size / 1024,
854 max_rom_decode.spi / 1024, "SPI");
855 }
856 if (!limitexceeded)
857 return 0;
858 /* Sometimes chip and programmer have more than one bus in common,
859 * and the limit is not exceeded on all buses. Tell the user.
860 */
861 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000862 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000863 printf_debug("There is at least one common chip/programmer "
864 "interface which can support a chip of this size. "
865 "You can try --force at your own risk.\n");
866 return 1;
867}
868
Peter Stuge483b8f02008-09-03 23:10:05 +0000869struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000870{
Peter Stuge483b8f02008-09-03 23:10:05 +0000871 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000872 unsigned long base = 0;
873 uint32_t size;
874 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000875 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000876
Peter Stuge483b8f02008-09-03 23:10:05 +0000877 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000878 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000879 continue;
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000880 msg_gdbg("Probing for %s %s, %d KB: ",
Stefan Reinauerac378972008-03-17 22:59:40 +0000881 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000882 if (!flash->probe && !force) {
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000883 msg_gdbg("failed! flashrom has no probe function for "
884 "this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000885 continue;
886 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000887 buses_common = buses_supported & flash->bustype;
888 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000889 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000890 msg_gdbg("skipped.");
891 msg_gspew(" Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000892 free(tmp);
893 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000894 msg_gspew("and chip bus type %s are incompatible.",
895 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000896 free(tmp);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000897 msg_gdbg("\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000898 continue;
899 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000900
Ollie Lhocbbf1252004-03-17 22:22:08 +0000901 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000902 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000903
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000904 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000905 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000906
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000907 if (force)
908 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000909
Peter Stuge483b8f02008-09-03 23:10:05 +0000910 if (flash->probe(flash) != 1)
911 goto notfound;
912
Uwe Hermann394131e2008-10-18 21:14:13 +0000913 if (first_flash == flashchips
914 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000915 break;
916
Peter Stuge483b8f02008-09-03 23:10:05 +0000917notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000918 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000919 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000920
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000921 if (!flash || !flash->name)
922 return NULL;
923
Uwe Hermann9899cad2009-06-28 21:47:57 +0000924 printf("Found chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
925 flash->vendor, flash->name, flash->total_size,
926 flashbuses_to_text(flash->bustype), base);
927
Sean Nelson6e0b9122010-02-19 00:52:10 +0000928 if (flash->printlock)
929 flash->printlock(flash);
930
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000931 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000932}
933
Stefan Reinauere3705282005-12-18 16:41:10 +0000934int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000935{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000936 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000937 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000938
Uwe Hermanna502dce2007-10-17 23:55:15 +0000939 printf("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000940
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000941 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000942
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000943 if (!ret)
944 printf("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000945
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000946 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000947}
948
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +0000949int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000950{
951 unsigned long numbytes;
952 FILE *image;
953 unsigned long size = flash->total_size * 1024;
954 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000955
956 if (!filename) {
957 printf("Error: No filename specified.\n");
958 return 1;
959 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +0000960 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000961 perror(filename);
962 exit(1);
963 }
964 printf("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000965 if (!flash->read) {
966 printf("FAILED!\n");
967 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
968 return 1;
969 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000970 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000971
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000972 numbytes = fwrite(buf, 1, size, image);
973 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +0000974 free(buf);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000975 printf("%s.\n", numbytes == size ? "done" : "FAILED");
976 if (numbytes != size)
977 return 1;
978 return 0;
979}
980
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000981/* This function shares a lot of its structure with erase_flash().
982 * Even if an error is found, the function will keep going and check the rest.
983 */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000984int selfcheck_eraseblocks(struct flashchip *flash)
985{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000986 int i, j, k;
987 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000988
989 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
990 unsigned int done = 0;
991 struct block_eraser eraser = flash->block_erasers[k];
992
993 for (i = 0; i < NUM_ERASEREGIONS; i++) {
994 /* Blocks with zero size are bugs in flashchips.c. */
995 if (eraser.eraseblocks[i].count &&
996 !eraser.eraseblocks[i].size) {
997 msg_gerr("ERROR: Flash chip %s erase function "
998 "%i region %i has size 0. Please report"
999 " a bug at flashrom@flashrom.org\n",
1000 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001001 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001002 }
1003 /* Blocks with zero count are bugs in flashchips.c. */
1004 if (!eraser.eraseblocks[i].count &&
1005 eraser.eraseblocks[i].size) {
1006 msg_gerr("ERROR: Flash chip %s erase function "
1007 "%i region %i has count 0. Please report"
1008 " a bug at flashrom@flashrom.org\n",
1009 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001010 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001011 }
1012 done += eraser.eraseblocks[i].count *
1013 eraser.eraseblocks[i].size;
1014 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001015 /* Empty eraseblock definition with erase function. */
1016 if (!done && eraser.block_erase)
1017 msg_pspew("Strange: Empty eraseblock definition with "
1018 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001019 if (!done)
1020 continue;
1021 if (done != flash->total_size * 1024) {
1022 msg_gerr("ERROR: Flash chip %s erase function %i "
1023 "region walking resulted in 0x%06x bytes total,"
1024 " expected 0x%06x bytes. Please report a bug at"
1025 " flashrom@flashrom.org\n", flash->name, k,
1026 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001027 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001028 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001029 if (!eraser.block_erase)
1030 continue;
1031 /* Check if there are identical erase functions for different
1032 * layouts. That would imply "magic" erase functions. The
1033 * easiest way to check this is with function pointers.
1034 */
Uwe Hermann43959702010-03-13 17:28:29 +00001035 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001036 if (eraser.block_erase ==
1037 flash->block_erasers[j].block_erase) {
1038 msg_gerr("ERROR: Flash chip %s erase function "
1039 "%i and %i are identical. Please report"
1040 " a bug at flashrom@flashrom.org\n",
1041 flash->name, k, j);
1042 ret = 1;
1043 }
Uwe Hermann43959702010-03-13 17:28:29 +00001044 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001045 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001046 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001047}
1048
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001049int erase_flash(struct flashchip *flash)
1050{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001051 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001052 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001053
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001054 printf("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001055 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001056 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001057 struct block_eraser eraser = flash->block_erasers[k];
1058
1059 printf_debug("Looking at blockwise erase function %i... ", k);
1060 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1061 printf_debug("not defined. "
1062 "Looking for another erase function.\n");
1063 continue;
1064 }
1065 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1066 printf_debug("eraseblock layout is known, but no "
1067 "matching block erase function found. "
1068 "Looking for another erase function.\n");
1069 continue;
1070 }
1071 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1072 printf_debug("block erase function found, but "
1073 "eraseblock layout is unknown. "
1074 "Looking for another erase function.\n");
1075 continue;
1076 }
1077 found = 1;
1078 printf_debug("trying... ");
1079 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1080 /* count==0 for all automatically initialized array
1081 * members so the loop below won't be executed for them.
1082 */
1083 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001084 start = done + eraser.eraseblocks[i].size * j;
1085 len = eraser.eraseblocks[i].size;
1086 printf_debug("0x%06x-0x%06x, ", start,
1087 start + len - 1);
1088 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001089 if (ret)
1090 break;
1091 }
1092 if (ret)
1093 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001094 done += eraser.eraseblocks[i].count *
1095 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001096 }
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001097 printf_debug("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001098 /* If everything is OK, don't try another erase function. */
1099 if (!ret)
1100 break;
1101 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001102 if (!found) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001103 fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
1104 return 1;
1105 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001106
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001107 if (ret) {
1108 fprintf(stderr, "FAILED!\n");
1109 } else {
1110 printf("SUCCESS.\n");
1111 }
1112 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001113}
1114
Uwe Hermannc67d0372009-10-01 18:40:02 +00001115void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001116{
1117 fprintf(stderr, "Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001118 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
1119 "mail flashrom@flashrom.org!\n--------------------"
1120 "-----------------------------------------------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001121 "DO NOT REBOOT OR POWEROFF!\n");
1122}
1123
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001124/* The way to go if you want a delimited list of programmers*/
1125void list_programmers(char *delim)
1126{
1127 enum programmer p;
1128 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1129 printf("%s", programmer_table[p].name);
1130 if (p < PROGRAMMER_INVALID - 1)
1131 printf("%s", delim);
1132 }
1133 printf("\n");
1134}
1135
Bernhard Walle201bde32008-01-21 15:24:22 +00001136void print_version(void)
1137{
Carl-Daniel Hailfingera80cfbc2009-07-22 20:13:00 +00001138 printf("flashrom v%s\n", flashrom_version);
Bernhard Walle201bde32008-01-21 15:24:22 +00001139}
1140
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001141int selfcheck(void)
1142{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001143 int ret = 0;
1144 struct flashchip *flash;
1145
1146 /* Safety check. Instead of aborting after the first error, check
1147 * if more errors exist.
1148 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001149 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
1150 fprintf(stderr, "Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001151 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001152 }
1153 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
1154 fprintf(stderr, "SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001155 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001156 }
1157#if BITBANG_SPI_SUPPORT == 1
1158 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
1159 fprintf(stderr, "Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001160 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001161 }
1162#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001163 for (flash = flashchips; flash && flash->name; flash++)
1164 if (selfcheck_eraseblocks(flash))
1165 ret = 1;
1166 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001167}
1168
1169void check_chip_supported(struct flashchip *flash)
1170{
1171 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
1172 printf("===\n");
1173 if (flash->tested & TEST_BAD_MASK) {
1174 printf("This flash part has status NOT WORKING for operations:");
1175 if (flash->tested & TEST_BAD_PROBE)
1176 printf(" PROBE");
1177 if (flash->tested & TEST_BAD_READ)
1178 printf(" READ");
1179 if (flash->tested & TEST_BAD_ERASE)
1180 printf(" ERASE");
1181 if (flash->tested & TEST_BAD_WRITE)
1182 printf(" WRITE");
1183 printf("\n");
1184 }
1185 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1186 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1187 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1188 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
1189 printf("This flash part has status UNTESTED for operations:");
1190 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
1191 printf(" PROBE");
1192 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
1193 printf(" READ");
1194 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
1195 printf(" ERASE");
1196 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
1197 printf(" WRITE");
1198 printf("\n");
1199 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001200 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001201 printf("Please email a report to flashrom@flashrom.org if any "
1202 "of the above operations\nwork correctly for you with "
1203 "this flash part. Please include the flashrom\noutput "
1204 "with the additional -V option for all operations you "
1205 "tested (-V, -rV,\n-wV, -EV), and mention which "
Uwe Hermann43959702010-03-13 17:28:29 +00001206 "mainboard or programmer you tested.\nThanks for your "
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001207 "help!\n===\n");
1208 }
1209}
1210
Ollie Lho761bf1b2004-03-20 16:46:10 +00001211int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001212{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001213 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001214}
1215
1216/* This function signature is horrible. We need to design a better interface,
1217 * but right now it allows us to split off the CLI code.
1218 */
1219int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1220{
1221 uint8_t *buf;
1222 unsigned long numbytes;
1223 FILE *image;
1224 int ret = 0;
1225 unsigned long size;
1226
1227 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001228 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001229
Ollie Lhoefa28582004-12-08 20:10:01 +00001230 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001231 if (flash->tested & TEST_BAD_ERASE) {
1232 fprintf(stderr, "Erase is not working on this chip. ");
1233 if (!force) {
1234 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001235 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001236 return 1;
1237 } else {
1238 fprintf(stderr, "Continuing anyway.\n");
1239 }
1240 }
Sean Nelson6e0b9122010-02-19 00:52:10 +00001241 if (flash->unlock)
1242 flash->unlock(flash);
1243
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001244 if (erase_flash(flash)) {
1245 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001246 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001247 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001248 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001249 } else if (read_it) {
Sean Nelson6e0b9122010-02-19 00:52:10 +00001250 if (flash->unlock)
1251 flash->unlock(flash);
1252
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001253 if (read_flash(flash, filename)) {
1254 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001255 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001256 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001257 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001258 struct stat image_stat;
1259
Sean Nelson6e0b9122010-02-19 00:52:10 +00001260 if (flash->unlock)
1261 flash->unlock(flash);
1262
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001263 if (flash->tested & TEST_BAD_ERASE) {
1264 fprintf(stderr, "Erase is not working on this chip "
1265 "and erase is needed for write. ");
1266 if (!force) {
1267 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001268 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001269 return 1;
1270 } else {
1271 fprintf(stderr, "Continuing anyway.\n");
1272 }
1273 }
1274 if (flash->tested & TEST_BAD_WRITE) {
1275 fprintf(stderr, "Write is not working on this chip. ");
1276 if (!force) {
1277 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001278 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001279 return 1;
1280 } else {
1281 fprintf(stderr, "Continuing anyway.\n");
1282 }
1283 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001284 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001285 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001286 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001287 exit(1);
1288 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001289 if (fstat(fileno(image), &image_stat) != 0) {
1290 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001291 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001292 exit(1);
1293 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001294 if (image_stat.st_size != flash->total_size * 1024) {
Uwe Hermann793bdcd2008-05-22 22:47:04 +00001295 fprintf(stderr, "Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001296 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001297 exit(1);
1298 }
1299
Peter Stuge1fec0f32009-01-12 21:00:35 +00001300 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001301#if INTERNAL_SUPPORT == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001302 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001303#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001304 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001305 if (numbytes != size) {
1306 fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001307 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001308 return 1;
1309 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001310 }
1311
Ollie Lho184a4042005-11-26 21:55:36 +00001312 // This should be moved into each flash part's code to do it
1313 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001314 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001315
Ollie Lho184a4042005-11-26 21:55:36 +00001316 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001317
Peter Stugef31104c2008-04-28 14:47:30 +00001318 if (write_it) {
Paul Foxd51410c2009-06-12 08:04:08 +00001319 printf("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001320 if (!flash->write) {
1321 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001322 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001323 return 1;
1324 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001325 ret = flash->write(flash, buf);
1326 if (ret) {
1327 fprintf(stderr, "FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001328 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001329 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001330 return 1;
1331 } else {
1332 printf("COMPLETE.\n");
1333 }
Peter Stugef31104c2008-04-28 14:47:30 +00001334 }
Ollie Lho184a4042005-11-26 21:55:36 +00001335
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001336 if (verify_it) {
1337 /* Work around chips which need some time to calm down. */
1338 if (write_it)
1339 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001340 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001341 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001342 * might have an emergency situation.
1343 */
1344 if (ret && write_it)
1345 emergency_help_message();
1346 }
Ollie Lho184a4042005-11-26 21:55:36 +00001347
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001348 programmer_shutdown();
1349
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001350 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001351}