blob: 6a9108919963c2fe43d473664177676ccc0bd603 [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);
667 /* Are have and want identical? */
668 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;
Stefan Reinauerac378972008-03-17 22:59:40 +0000880 printf_debug("Probing for %s %s, %d KB: ",
881 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000882 if (!flash->probe && !force) {
Peter Stugef31104c2008-04-28 14:47:30 +0000883 printf_debug("failed! flashrom has no probe function for this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000884 continue;
885 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000886 buses_common = buses_supported & flash->bustype;
887 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000888 tmp = flashbuses_to_text(buses_supported);
889 printf_debug("skipped. Host bus type %s ", tmp);
890 free(tmp);
891 tmp = flashbuses_to_text(flash->bustype);
892 printf_debug("and chip bus type %s are incompatible.\n", tmp);
893 free(tmp);
894 continue;
895 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000896
Ollie Lhocbbf1252004-03-17 22:22:08 +0000897 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000898 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000899
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000900 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000901 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000902
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000903 if (force)
904 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000905
Peter Stuge483b8f02008-09-03 23:10:05 +0000906 if (flash->probe(flash) != 1)
907 goto notfound;
908
Uwe Hermann394131e2008-10-18 21:14:13 +0000909 if (first_flash == flashchips
910 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000911 break;
912
Peter Stuge483b8f02008-09-03 23:10:05 +0000913notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000914 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000915 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000916
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000917 if (!flash || !flash->name)
918 return NULL;
919
Uwe Hermann9899cad2009-06-28 21:47:57 +0000920 printf("Found chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
921 flash->vendor, flash->name, flash->total_size,
922 flashbuses_to_text(flash->bustype), base);
923
Sean Nelson6e0b9122010-02-19 00:52:10 +0000924 if (flash->printlock)
925 flash->printlock(flash);
926
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000927 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000928}
929
Stefan Reinauere3705282005-12-18 16:41:10 +0000930int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000931{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000932 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000933 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000934
Uwe Hermanna502dce2007-10-17 23:55:15 +0000935 printf("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000936
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000937 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000938
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000939 if (!ret)
940 printf("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000941
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000942 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000943}
944
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +0000945int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000946{
947 unsigned long numbytes;
948 FILE *image;
949 unsigned long size = flash->total_size * 1024;
950 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000951
952 if (!filename) {
953 printf("Error: No filename specified.\n");
954 return 1;
955 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +0000956 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000957 perror(filename);
958 exit(1);
959 }
960 printf("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000961 if (!flash->read) {
962 printf("FAILED!\n");
963 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
964 return 1;
965 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000966 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000967
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000968 numbytes = fwrite(buf, 1, size, image);
969 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +0000970 free(buf);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000971 printf("%s.\n", numbytes == size ? "done" : "FAILED");
972 if (numbytes != size)
973 return 1;
974 return 0;
975}
976
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000977/* This function shares a lot of its structure with erase_flash().
978 * Even if an error is found, the function will keep going and check the rest.
979 */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000980int selfcheck_eraseblocks(struct flashchip *flash)
981{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000982 int i, j, k;
983 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000984
985 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
986 unsigned int done = 0;
987 struct block_eraser eraser = flash->block_erasers[k];
988
989 for (i = 0; i < NUM_ERASEREGIONS; i++) {
990 /* Blocks with zero size are bugs in flashchips.c. */
991 if (eraser.eraseblocks[i].count &&
992 !eraser.eraseblocks[i].size) {
993 msg_gerr("ERROR: Flash chip %s erase function "
994 "%i region %i has size 0. Please report"
995 " a bug at flashrom@flashrom.org\n",
996 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000997 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000998 }
999 /* Blocks with zero count are bugs in flashchips.c. */
1000 if (!eraser.eraseblocks[i].count &&
1001 eraser.eraseblocks[i].size) {
1002 msg_gerr("ERROR: Flash chip %s erase function "
1003 "%i region %i has count 0. Please report"
1004 " a bug at flashrom@flashrom.org\n",
1005 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001006 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001007 }
1008 done += eraser.eraseblocks[i].count *
1009 eraser.eraseblocks[i].size;
1010 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001011 /* Empty eraseblock definition with erase function. */
1012 if (!done && eraser.block_erase)
1013 msg_pspew("Strange: Empty eraseblock definition with "
1014 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001015 if (!done)
1016 continue;
1017 if (done != flash->total_size * 1024) {
1018 msg_gerr("ERROR: Flash chip %s erase function %i "
1019 "region walking resulted in 0x%06x bytes total,"
1020 " expected 0x%06x bytes. Please report a bug at"
1021 " flashrom@flashrom.org\n", flash->name, k,
1022 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001023 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001024 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001025 if (!eraser.block_erase)
1026 continue;
1027 /* Check if there are identical erase functions for different
1028 * layouts. That would imply "magic" erase functions. The
1029 * easiest way to check this is with function pointers.
1030 */
1031 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++)
1032 if (eraser.block_erase ==
1033 flash->block_erasers[j].block_erase) {
1034 msg_gerr("ERROR: Flash chip %s erase function "
1035 "%i and %i are identical. Please report"
1036 " a bug at flashrom@flashrom.org\n",
1037 flash->name, k, j);
1038 ret = 1;
1039 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001040 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001041 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001042}
1043
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001044int erase_flash(struct flashchip *flash)
1045{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001046 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001047 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001048
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001049 printf("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001050 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001051 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001052 struct block_eraser eraser = flash->block_erasers[k];
1053
1054 printf_debug("Looking at blockwise erase function %i... ", k);
1055 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1056 printf_debug("not defined. "
1057 "Looking for another erase function.\n");
1058 continue;
1059 }
1060 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1061 printf_debug("eraseblock layout is known, but no "
1062 "matching block erase function found. "
1063 "Looking for another erase function.\n");
1064 continue;
1065 }
1066 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1067 printf_debug("block erase function found, but "
1068 "eraseblock layout is unknown. "
1069 "Looking for another erase function.\n");
1070 continue;
1071 }
1072 found = 1;
1073 printf_debug("trying... ");
1074 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1075 /* count==0 for all automatically initialized array
1076 * members so the loop below won't be executed for them.
1077 */
1078 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001079 start = done + eraser.eraseblocks[i].size * j;
1080 len = eraser.eraseblocks[i].size;
1081 printf_debug("0x%06x-0x%06x, ", start,
1082 start + len - 1);
1083 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001084 if (ret)
1085 break;
1086 }
1087 if (ret)
1088 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001089 done += eraser.eraseblocks[i].count *
1090 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001091 }
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001092 printf_debug("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001093 /* If everything is OK, don't try another erase function. */
1094 if (!ret)
1095 break;
1096 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001097 if (!found) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001098 fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
1099 return 1;
1100 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001101
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001102 if (ret) {
1103 fprintf(stderr, "FAILED!\n");
1104 } else {
1105 printf("SUCCESS.\n");
1106 }
1107 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001108}
1109
Uwe Hermannc67d0372009-10-01 18:40:02 +00001110void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001111{
1112 fprintf(stderr, "Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001113 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
1114 "mail flashrom@flashrom.org!\n--------------------"
1115 "-----------------------------------------------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001116 "DO NOT REBOOT OR POWEROFF!\n");
1117}
1118
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001119/* The way to go if you want a delimited list of programmers*/
1120void list_programmers(char *delim)
1121{
1122 enum programmer p;
1123 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1124 printf("%s", programmer_table[p].name);
1125 if (p < PROGRAMMER_INVALID - 1)
1126 printf("%s", delim);
1127 }
1128 printf("\n");
1129}
1130
Bernhard Walle201bde32008-01-21 15:24:22 +00001131void print_version(void)
1132{
Carl-Daniel Hailfingera80cfbc2009-07-22 20:13:00 +00001133 printf("flashrom v%s\n", flashrom_version);
Bernhard Walle201bde32008-01-21 15:24:22 +00001134}
1135
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001136int selfcheck(void)
1137{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001138 int ret = 0;
1139 struct flashchip *flash;
1140
1141 /* Safety check. Instead of aborting after the first error, check
1142 * if more errors exist.
1143 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001144 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
1145 fprintf(stderr, "Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001146 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001147 }
1148 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
1149 fprintf(stderr, "SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001150 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001151 }
1152#if BITBANG_SPI_SUPPORT == 1
1153 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
1154 fprintf(stderr, "Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001155 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001156 }
1157#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001158 for (flash = flashchips; flash && flash->name; flash++)
1159 if (selfcheck_eraseblocks(flash))
1160 ret = 1;
1161 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001162}
1163
1164void check_chip_supported(struct flashchip *flash)
1165{
1166 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
1167 printf("===\n");
1168 if (flash->tested & TEST_BAD_MASK) {
1169 printf("This flash part has status NOT WORKING for operations:");
1170 if (flash->tested & TEST_BAD_PROBE)
1171 printf(" PROBE");
1172 if (flash->tested & TEST_BAD_READ)
1173 printf(" READ");
1174 if (flash->tested & TEST_BAD_ERASE)
1175 printf(" ERASE");
1176 if (flash->tested & TEST_BAD_WRITE)
1177 printf(" WRITE");
1178 printf("\n");
1179 }
1180 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1181 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1182 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1183 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
1184 printf("This flash part has status UNTESTED for operations:");
1185 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
1186 printf(" PROBE");
1187 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
1188 printf(" READ");
1189 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
1190 printf(" ERASE");
1191 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
1192 printf(" WRITE");
1193 printf("\n");
1194 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001195 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001196 printf("Please email a report to flashrom@flashrom.org if any "
1197 "of the above operations\nwork correctly for you with "
1198 "this flash part. Please include the flashrom\noutput "
1199 "with the additional -V option for all operations you "
1200 "tested (-V, -rV,\n-wV, -EV), and mention which "
1201 "mainboard or programmer you tested. Thanks for your "
1202 "help!\n===\n");
1203 }
1204}
1205
Ollie Lho761bf1b2004-03-20 16:46:10 +00001206int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001207{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001208 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001209}
1210
1211/* This function signature is horrible. We need to design a better interface,
1212 * but right now it allows us to split off the CLI code.
1213 */
1214int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1215{
1216 uint8_t *buf;
1217 unsigned long numbytes;
1218 FILE *image;
1219 int ret = 0;
1220 unsigned long size;
1221
1222 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001223 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001224
Ollie Lhoefa28582004-12-08 20:10:01 +00001225 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001226 if (flash->tested & TEST_BAD_ERASE) {
1227 fprintf(stderr, "Erase is not working on this chip. ");
1228 if (!force) {
1229 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001230 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001231 return 1;
1232 } else {
1233 fprintf(stderr, "Continuing anyway.\n");
1234 }
1235 }
Sean Nelson6e0b9122010-02-19 00:52:10 +00001236 if (flash->unlock)
1237 flash->unlock(flash);
1238
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001239 if (erase_flash(flash)) {
1240 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001241 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001242 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001243 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001244 } else if (read_it) {
Sean Nelson6e0b9122010-02-19 00:52:10 +00001245 if (flash->unlock)
1246 flash->unlock(flash);
1247
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001248 if (read_flash(flash, filename)) {
1249 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001250 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001251 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001252 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001253 struct stat image_stat;
1254
Sean Nelson6e0b9122010-02-19 00:52:10 +00001255 if (flash->unlock)
1256 flash->unlock(flash);
1257
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001258 if (flash->tested & TEST_BAD_ERASE) {
1259 fprintf(stderr, "Erase is not working on this chip "
1260 "and erase is needed for write. ");
1261 if (!force) {
1262 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001263 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001264 return 1;
1265 } else {
1266 fprintf(stderr, "Continuing anyway.\n");
1267 }
1268 }
1269 if (flash->tested & TEST_BAD_WRITE) {
1270 fprintf(stderr, "Write 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 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001279 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001280 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001281 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001282 exit(1);
1283 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001284 if (fstat(fileno(image), &image_stat) != 0) {
1285 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001286 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001287 exit(1);
1288 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001289 if (image_stat.st_size != flash->total_size * 1024) {
Uwe Hermann793bdcd2008-05-22 22:47:04 +00001290 fprintf(stderr, "Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001291 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001292 exit(1);
1293 }
1294
Peter Stuge1fec0f32009-01-12 21:00:35 +00001295 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001296#if INTERNAL_SUPPORT == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001297 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001298#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001299 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001300 if (numbytes != size) {
1301 fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001302 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001303 return 1;
1304 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001305 }
1306
Ollie Lho184a4042005-11-26 21:55:36 +00001307 // This should be moved into each flash part's code to do it
1308 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001309 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001310
Ollie Lho184a4042005-11-26 21:55:36 +00001311 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001312
Peter Stugef31104c2008-04-28 14:47:30 +00001313 if (write_it) {
Paul Foxd51410c2009-06-12 08:04:08 +00001314 printf("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001315 if (!flash->write) {
1316 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001317 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001318 return 1;
1319 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001320 ret = flash->write(flash, buf);
1321 if (ret) {
1322 fprintf(stderr, "FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001323 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001324 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001325 return 1;
1326 } else {
1327 printf("COMPLETE.\n");
1328 }
Peter Stugef31104c2008-04-28 14:47:30 +00001329 }
Ollie Lho184a4042005-11-26 21:55:36 +00001330
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001331 if (verify_it) {
1332 /* Work around chips which need some time to calm down. */
1333 if (write_it)
1334 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001335 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001336 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001337 * might have an emergency situation.
1338 */
1339 if (ret && write_it)
1340 emergency_help_message();
1341 }
Ollie Lho184a4042005-11-26 21:55:36 +00001342
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001343 programmer_shutdown();
1344
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001345 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001346}