blob: b8de3c10789e92aadc18add728cdbc0e039683e3 [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 */
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000047#if NIC3COM_SUPPORT+GFXNVIDIA_SUPPORT+DRKAISER_SUPPORT+SATASII_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
63#if FT2232_SPI_SUPPORT == 1
64 PROGRAMMER_FT2232SPI
65#endif
66#if SERPROG_SUPPORT == 1
67 PROGRAMMER_SERPROG
68#endif
69#if BUSPIRATE_SPI_SUPPORT == 1
70 PROGRAMMER_BUSPIRATESPI
71#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000072#if DEDIPROG_SUPPORT == 1
73 PROGRAMMER_DEDIPROG
74#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000075;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000076#endif
77
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000078char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000079
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000080/**
81 * flashrom defaults to Parallel/LPC/FWH flash devices. If a known host
82 * controller is found, the init routine sets the buses_supported bitfield to
83 * contain the supported buses for that controller.
84 */
85enum chipbustype buses_supported = CHIP_BUSTYPE_NONSPI;
86
87/**
88 * Programmers supporting multiple buses can have differing size limits on
89 * each bus. Store the limits for each bus in a common struct.
90 */
91struct decode_sizes max_rom_decode = {
92 .parallel = 0xffffffff,
93 .lpc = 0xffffffff,
94 .fwh = 0xffffffff,
95 .spi = 0xffffffff
96};
97
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000098const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000099#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000100 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000101 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000102 .init = internal_init,
103 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000104 .map_flash_region = physmap,
105 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000106 .chip_readb = internal_chip_readb,
107 .chip_readw = internal_chip_readw,
108 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000109 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000110 .chip_writeb = internal_chip_writeb,
111 .chip_writew = internal_chip_writew,
112 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000113 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000114 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000115 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000116#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000117
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000118#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000119 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000120 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000121 .init = dummy_init,
122 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000123 .map_flash_region = dummy_map,
124 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000125 .chip_readb = dummy_chip_readb,
126 .chip_readw = dummy_chip_readw,
127 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000128 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000129 .chip_writeb = dummy_chip_writeb,
130 .chip_writew = dummy_chip_writew,
131 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000132 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000133 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000134 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000135#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000136
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000137#if NIC3COM_SUPPORT == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000138 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000139 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000140 .init = nic3com_init,
141 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000142 .map_flash_region = fallback_map,
143 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000144 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000145 .chip_readw = fallback_chip_readw,
146 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000147 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000148 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000149 .chip_writew = fallback_chip_writew,
150 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000151 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000152 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000153 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000154#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000155
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000156#if GFXNVIDIA_SUPPORT == 1
157 {
158 .name = "gfxnvidia",
159 .init = gfxnvidia_init,
160 .shutdown = gfxnvidia_shutdown,
161 .map_flash_region = fallback_map,
162 .unmap_flash_region = fallback_unmap,
163 .chip_readb = gfxnvidia_chip_readb,
164 .chip_readw = fallback_chip_readw,
165 .chip_readl = fallback_chip_readl,
166 .chip_readn = fallback_chip_readn,
167 .chip_writeb = gfxnvidia_chip_writeb,
168 .chip_writew = fallback_chip_writew,
169 .chip_writel = fallback_chip_writel,
170 .chip_writen = fallback_chip_writen,
171 .delay = internal_delay,
172 },
173#endif
174
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000175#if DRKAISER_SUPPORT == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000176 {
TURBO Jb0912c02009-09-02 23:00:46 +0000177 .name = "drkaiser",
178 .init = drkaiser_init,
179 .shutdown = drkaiser_shutdown,
180 .map_flash_region = fallback_map,
181 .unmap_flash_region = fallback_unmap,
182 .chip_readb = drkaiser_chip_readb,
183 .chip_readw = fallback_chip_readw,
184 .chip_readl = fallback_chip_readl,
185 .chip_readn = fallback_chip_readn,
186 .chip_writeb = drkaiser_chip_writeb,
187 .chip_writew = fallback_chip_writew,
188 .chip_writel = fallback_chip_writel,
189 .chip_writen = fallback_chip_writen,
190 .delay = internal_delay,
191 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000192#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000193
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000194#if SATASII_SUPPORT == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000195 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000196 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000197 .init = satasii_init,
198 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000199 .map_flash_region = fallback_map,
200 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000201 .chip_readb = satasii_chip_readb,
202 .chip_readw = fallback_chip_readw,
203 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000204 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000205 .chip_writeb = satasii_chip_writeb,
206 .chip_writew = fallback_chip_writew,
207 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000208 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000209 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000210 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000211#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000212
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000213#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000214 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000215 .name = "it87spi",
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000216 .init = it87spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000217 .shutdown = noop_shutdown,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000218 .map_flash_region = fallback_map,
219 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000220 .chip_readb = noop_chip_readb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000221 .chip_readw = fallback_chip_readw,
222 .chip_readl = fallback_chip_readl,
223 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000224 .chip_writeb = noop_chip_writeb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000225 .chip_writew = fallback_chip_writew,
226 .chip_writel = fallback_chip_writel,
227 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000228 .delay = internal_delay,
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000229 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000230#endif
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000231
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000232#if FT2232_SPI_SUPPORT == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000233 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000234 .name = "ft2232spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000235 .init = ft2232_spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000236 .shutdown = noop_shutdown, /* Missing shutdown */
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000237 .map_flash_region = fallback_map,
238 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000239 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000240 .chip_readw = fallback_chip_readw,
241 .chip_readl = fallback_chip_readl,
242 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000243 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000244 .chip_writew = fallback_chip_writew,
245 .chip_writel = fallback_chip_writel,
246 .chip_writen = fallback_chip_writen,
247 .delay = internal_delay,
248 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000249#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000250
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000251#if SERPROG_SUPPORT == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000252 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000253 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000254 .init = serprog_init,
255 .shutdown = serprog_shutdown,
256 .map_flash_region = fallback_map,
257 .unmap_flash_region = fallback_unmap,
258 .chip_readb = serprog_chip_readb,
259 .chip_readw = fallback_chip_readw,
260 .chip_readl = fallback_chip_readl,
261 .chip_readn = serprog_chip_readn,
262 .chip_writeb = serprog_chip_writeb,
263 .chip_writew = fallback_chip_writew,
264 .chip_writel = fallback_chip_writel,
265 .chip_writen = fallback_chip_writen,
266 .delay = serprog_delay,
267 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000268#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000269
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000270#if BUSPIRATE_SPI_SUPPORT == 1
271 {
272 .name = "buspiratespi",
273 .init = buspirate_spi_init,
274 .shutdown = buspirate_spi_shutdown,
275 .map_flash_region = fallback_map,
276 .unmap_flash_region = fallback_unmap,
277 .chip_readb = noop_chip_readb,
278 .chip_readw = fallback_chip_readw,
279 .chip_readl = fallback_chip_readl,
280 .chip_readn = fallback_chip_readn,
281 .chip_writeb = noop_chip_writeb,
282 .chip_writew = fallback_chip_writew,
283 .chip_writel = fallback_chip_writel,
284 .chip_writen = fallback_chip_writen,
285 .delay = internal_delay,
286 },
287#endif
288
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000289#if DEDIPROG_SUPPORT == 1
290 {
291 .name = "dediprog",
292 .init = dediprog_init,
293 .shutdown = dediprog_shutdown,
294 .map_flash_region = fallback_map,
295 .unmap_flash_region = fallback_unmap,
296 .chip_readb = noop_chip_readb,
297 .chip_readw = fallback_chip_readw,
298 .chip_readl = fallback_chip_readl,
299 .chip_readn = fallback_chip_readn,
300 .chip_writeb = noop_chip_writeb,
301 .chip_writew = fallback_chip_writew,
302 .chip_writel = fallback_chip_writel,
303 .chip_writen = fallback_chip_writen,
304 .delay = internal_delay,
305 },
306#endif
307
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000308 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000309};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000310
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000311#define SHUTDOWN_MAXFN 4
312static int shutdown_fn_count = 0;
313struct shutdown_func_data {
314 void (*func) (void *data);
315 void *data;
316} shutdown_fn[SHUTDOWN_MAXFN];
317
318/* Register a function to be executed on programmer shutdown.
319 * The advantage over atexit() is that you can supply a void pointer which will
320 * be used as parameter to the registered function upon programmer shutdown.
321 * This pointer can point to arbitrary data used by said function, e.g. undo
322 * information for GPIO settings etc. If unneeded, set data=NULL.
323 * Please note that the first (void *data) belongs to the function signature of
324 * the function passed as first parameter.
325 */
326int register_shutdown(void (*function) (void *data), void *data)
327{
328 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
329 msg_perr("Tried to register more than %n shutdown functions.\n",
330 SHUTDOWN_MAXFN);
331 return 1;
332 }
333 shutdown_fn[shutdown_fn_count].func = function;
334 shutdown_fn[shutdown_fn_count].data = data;
335 shutdown_fn_count++;
336
337 return 0;
338}
339
Uwe Hermann09e04f72009-05-16 22:36:00 +0000340int programmer_init(void)
341{
342 return programmer_table[programmer].init();
343}
344
345int programmer_shutdown(void)
346{
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000347 int i;
348
349 for (i = shutdown_fn_count - 1; i >= 0; i--)
350 shutdown_fn[i].func(shutdown_fn[i].data);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000351 return programmer_table[programmer].shutdown();
352}
353
354void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
355 size_t len)
356{
357 return programmer_table[programmer].map_flash_region(descr,
358 phys_addr, len);
359}
360
361void programmer_unmap_flash_region(void *virt_addr, size_t len)
362{
363 programmer_table[programmer].unmap_flash_region(virt_addr, len);
364}
365
366void chip_writeb(uint8_t val, chipaddr addr)
367{
368 programmer_table[programmer].chip_writeb(val, addr);
369}
370
371void chip_writew(uint16_t val, chipaddr addr)
372{
373 programmer_table[programmer].chip_writew(val, addr);
374}
375
376void chip_writel(uint32_t val, chipaddr addr)
377{
378 programmer_table[programmer].chip_writel(val, addr);
379}
380
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000381void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
382{
383 programmer_table[programmer].chip_writen(buf, addr, len);
384}
385
Uwe Hermann09e04f72009-05-16 22:36:00 +0000386uint8_t chip_readb(const chipaddr addr)
387{
388 return programmer_table[programmer].chip_readb(addr);
389}
390
391uint16_t chip_readw(const chipaddr addr)
392{
393 return programmer_table[programmer].chip_readw(addr);
394}
395
396uint32_t chip_readl(const chipaddr addr)
397{
398 return programmer_table[programmer].chip_readl(addr);
399}
400
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000401void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
402{
403 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000404}
405
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000406void programmer_delay(int usecs)
407{
408 programmer_table[programmer].delay(usecs);
409}
410
Peter Stuge776d2022009-01-26 00:39:57 +0000411void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000412{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000413 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000414 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000415 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000416 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000417}
418
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000419int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000420{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000421 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000422
423 return 0;
424}
425
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000426unsigned long flashbase = 0;
427
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000428int min(int a, int b)
429{
430 return (a < b) ? a : b;
431}
432
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000433int max(int a, int b)
434{
435 return (a > b) ? a : b;
436}
437
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000438int bitcount(unsigned long a)
439{
440 int i = 0;
441 for (; a != 0; a >>= 1)
442 if (a & 1)
443 i++;
444 return i;
445}
446
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000447char *strcat_realloc(char *dest, const char *src)
448{
449 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
450 if (!dest)
451 return NULL;
452 strcat(dest, src);
453 return dest;
454}
455
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000456/* This is a somewhat hacked function similar in some ways to strtok().
457 * It will look for needle in haystack, return a copy of needle and remove
458 * everything from the first occurrence of needle to the next delimiter
459 * from haystack.
460 */
461char *extract_param(char **haystack, char *needle, char *delim)
462{
463 char *param_pos, *rest, *tmp;
464 char *dev = NULL;
465 int devlen;
466
467 param_pos = strstr(*haystack, needle);
468 do {
469 if (!param_pos)
470 return NULL;
471 /* Beginning of the string? */
472 if (param_pos == *haystack)
473 break;
474 /* After a delimiter? */
475 if (strchr(delim, *(param_pos - 1)))
476 break;
477 /* Continue searching. */
478 param_pos++;
479 param_pos = strstr(param_pos, needle);
480 } while (1);
481
482 if (param_pos) {
483 param_pos += strlen(needle);
484 devlen = strcspn(param_pos, delim);
485 if (devlen) {
486 dev = malloc(devlen + 1);
487 if (!dev) {
488 fprintf(stderr, "Out of memory!\n");
489 exit(1);
490 }
491 strncpy(dev, param_pos, devlen);
492 dev[devlen] = '\0';
493 }
494 rest = param_pos + devlen;
495 rest += strspn(rest, delim);
496 param_pos -= strlen(needle);
497 memmove(param_pos, rest, strlen(rest) + 1);
498 tmp = realloc(*haystack, strlen(*haystack) + 1);
499 if (!tmp) {
500 fprintf(stderr, "Out of memory!\n");
501 exit(1);
502 }
503 *haystack = tmp;
504 }
505
506
507 return dev;
508}
509
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000510/* start is an offset to the base address of the flash chip */
511int check_erased_range(struct flashchip *flash, int start, int len)
512{
513 int ret;
514 uint8_t *cmpbuf = malloc(len);
515
516 if (!cmpbuf) {
517 fprintf(stderr, "Could not allocate memory!\n");
518 exit(1);
519 }
520 memset(cmpbuf, 0xff, len);
521 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
522 free(cmpbuf);
523 return ret;
524}
525
526/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000527 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
528 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000529 * @start offset to the base address of the flash chip
530 * @len length of the verified area
531 * @message string to print in the "FAILED" message
532 * @return 0 for success, -1 for failure
533 */
534int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
535{
536 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000537 int page_size = flash->page_size;
538 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000539 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000540
541 if (!len)
542 goto out_free;
543
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000544 if (!flash->read) {
545 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
546 return 1;
547 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000548 if (!readbuf) {
549 fprintf(stderr, "Could not allocate memory!\n");
550 exit(1);
551 }
552
553 if (start + len > flash->total_size * 1024) {
554 fprintf(stderr, "Error: %s called with start 0x%x + len 0x%x >"
555 " total_size 0x%x\n", __func__, start, len,
556 flash->total_size * 1024);
557 ret = -1;
558 goto out_free;
559 }
560 if (!message)
561 message = "VERIFY";
562
563 /* Warning: This loop has a very unusual condition and body.
564 * The loop needs to go through each page with at least one affected
565 * byte. The lowest page number is (start / page_size) since that
566 * division rounds down. The highest page number we want is the page
567 * where the last byte of the range lives. That last byte has the
568 * address (start + len - 1), thus the highest page number is
569 * (start + len - 1) / page_size. Since we want to include that last
570 * page as well, the loop condition uses <=.
571 */
572 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
573 /* Byte position of the first byte in the range in this page. */
574 starthere = max(start, i * page_size);
575 /* Length of bytes in the range in this page. */
576 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000577 flash->read(flash, readbuf, starthere, lenhere);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000578 for (j = 0; j < lenhere; j++) {
579 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000580 /* Only print the first failure. */
581 if (!failcount++)
582 fprintf(stderr, "%s FAILED at 0x%08x! "
583 "Expected=0x%02x, Read=0x%02x,",
584 message, starthere + j,
585 cmpbuf[starthere - start + j],
586 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000587 }
588 }
589 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000590 if (failcount) {
591 fprintf(stderr, " failed byte count from 0x%08x-0x%08x: 0x%x\n",
592 start, start + len - 1, failcount);
593 ret = -1;
594 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000595
596out_free:
597 free(readbuf);
598 return ret;
599}
600
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000601/* This function generates various test patterns useful for testing controller
602 * and chip communication as well as chip behaviour.
603 *
604 * If a byte can be written multiple times, each time keeping 0-bits at 0
605 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
606 * is essentially an AND operation. That's also the reason why this function
607 * provides the result of AND between various patterns.
608 *
609 * Below is a list of patterns (and their block length).
610 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
611 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
612 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
613 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
614 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
615 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
616 * Pattern 6 is 00 (1 Byte)
617 * Pattern 7 is ff (1 Byte)
618 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
619 * byte block.
620 *
621 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
622 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
623 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
624 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
625 * Pattern 12 is 00 (1 Byte)
626 * Pattern 13 is ff (1 Byte)
627 * Patterns 8-13 have no block number.
628 *
629 * Patterns 0-3 are created to detect and efficiently diagnose communication
630 * slips like missed bits or bytes and their repetitive nature gives good visual
631 * cues to the person inspecting the results. In addition, the following holds:
632 * AND Pattern 0/1 == Pattern 4
633 * AND Pattern 2/3 == Pattern 5
634 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
635 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
636 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
637 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
638 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
639 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
640 * Besides that, they provide for bit testing of the last two bytes of every
641 * 256 byte block which contains the block number for patterns 0-6.
642 * Patterns 10-11 are special purpose for detecting subblock aliasing with
643 * block sizes >256 bytes (some Dataflash chips etc.)
644 * AND Pattern 8/9 == Pattern 12
645 * AND Pattern 10/11 == Pattern 12
646 * Pattern 13 is the completely erased state.
647 * None of the patterns can detect aliasing at boundaries which are a multiple
648 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
649 */
650int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
651{
652 int i;
653
654 if (!buf) {
655 fprintf(stderr, "Invalid buffer!\n");
656 return 1;
657 }
658
659 switch (variant) {
660 case 0:
661 for (i = 0; i < size; i++)
662 buf[i] = (i & 0xf) << 4 | 0x5;
663 break;
664 case 1:
665 for (i = 0; i < size; i++)
666 buf[i] = (i & 0xf) << 4 | 0xa;
667 break;
668 case 2:
669 for (i = 0; i < size; i++)
670 buf[i] = 0x50 | (i & 0xf);
671 break;
672 case 3:
673 for (i = 0; i < size; i++)
674 buf[i] = 0xa0 | (i & 0xf);
675 break;
676 case 4:
677 for (i = 0; i < size; i++)
678 buf[i] = (i & 0xf) << 4;
679 break;
680 case 5:
681 for (i = 0; i < size; i++)
682 buf[i] = i & 0xf;
683 break;
684 case 6:
685 memset(buf, 0x00, size);
686 break;
687 case 7:
688 memset(buf, 0xff, size);
689 break;
690 case 8:
691 for (i = 0; i < size; i++)
692 buf[i] = i & 0xff;
693 break;
694 case 9:
695 for (i = 0; i < size; i++)
696 buf[i] = ~(i & 0xff);
697 break;
698 case 10:
699 for (i = 0; i < size % 2; i++) {
700 buf[i * 2] = (i >> 8) & 0xff;
701 buf[i * 2 + 1] = i & 0xff;
702 }
703 if (size & 0x1)
704 buf[i * 2] = (i >> 8) & 0xff;
705 break;
706 case 11:
707 for (i = 0; i < size % 2; i++) {
708 buf[i * 2] = ~((i >> 8) & 0xff);
709 buf[i * 2 + 1] = ~(i & 0xff);
710 }
711 if (size & 0x1)
712 buf[i * 2] = ~((i >> 8) & 0xff);
713 break;
714 case 12:
715 memset(buf, 0x00, size);
716 break;
717 case 13:
718 memset(buf, 0xff, size);
719 break;
720 }
721
722 if ((variant >= 0) && (variant <= 7)) {
723 /* Write block number in the last two bytes of each 256-byte
724 * block, big endian for easier reading of the hexdump.
725 * Note that this wraps around for chips larger than 2^24 bytes
726 * (16 MB).
727 */
728 for (i = 0; i < size / 256; i++) {
729 buf[i * 256 + 254] = (i >> 8) & 0xff;
730 buf[i * 256 + 255] = i & 0xff;
731 }
732 }
733
734 return 0;
735}
736
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000737int check_max_decode(enum chipbustype buses, uint32_t size)
738{
739 int limitexceeded = 0;
740 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
741 (max_rom_decode.parallel < size)) {
742 limitexceeded++;
743 printf_debug("Chip size %u kB is bigger than supported "
744 "size %u kB of chipset/board/programmer "
745 "for %s interface, "
746 "probe/read/erase/write may fail. ", size / 1024,
747 max_rom_decode.parallel / 1024, "Parallel");
748 }
749 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
750 limitexceeded++;
751 printf_debug("Chip size %u kB is bigger than supported "
752 "size %u kB of chipset/board/programmer "
753 "for %s interface, "
754 "probe/read/erase/write may fail. ", size / 1024,
755 max_rom_decode.lpc / 1024, "LPC");
756 }
757 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
758 limitexceeded++;
759 printf_debug("Chip size %u kB is bigger than supported "
760 "size %u kB of chipset/board/programmer "
761 "for %s interface, "
762 "probe/read/erase/write may fail. ", size / 1024,
763 max_rom_decode.fwh / 1024, "FWH");
764 }
765 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
766 limitexceeded++;
767 printf_debug("Chip size %u kB is bigger than supported "
768 "size %u kB of chipset/board/programmer "
769 "for %s interface, "
770 "probe/read/erase/write may fail. ", size / 1024,
771 max_rom_decode.spi / 1024, "SPI");
772 }
773 if (!limitexceeded)
774 return 0;
775 /* Sometimes chip and programmer have more than one bus in common,
776 * and the limit is not exceeded on all buses. Tell the user.
777 */
778 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000779 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000780 printf_debug("There is at least one common chip/programmer "
781 "interface which can support a chip of this size. "
782 "You can try --force at your own risk.\n");
783 return 1;
784}
785
Peter Stuge483b8f02008-09-03 23:10:05 +0000786struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000787{
Peter Stuge483b8f02008-09-03 23:10:05 +0000788 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000789 unsigned long base = 0;
790 uint32_t size;
791 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000792 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000793
Peter Stuge483b8f02008-09-03 23:10:05 +0000794 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000795 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000796 continue;
Stefan Reinauerac378972008-03-17 22:59:40 +0000797 printf_debug("Probing for %s %s, %d KB: ",
798 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000799 if (!flash->probe && !force) {
Peter Stugef31104c2008-04-28 14:47:30 +0000800 printf_debug("failed! flashrom has no probe function for this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000801 continue;
802 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000803 buses_common = buses_supported & flash->bustype;
804 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000805 tmp = flashbuses_to_text(buses_supported);
806 printf_debug("skipped. Host bus type %s ", tmp);
807 free(tmp);
808 tmp = flashbuses_to_text(flash->bustype);
809 printf_debug("and chip bus type %s are incompatible.\n", tmp);
810 free(tmp);
811 continue;
812 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000813
Ollie Lhocbbf1252004-03-17 22:22:08 +0000814 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000815 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000816
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000817 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000818 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000819
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000820 if (force)
821 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000822
Peter Stuge483b8f02008-09-03 23:10:05 +0000823 if (flash->probe(flash) != 1)
824 goto notfound;
825
Uwe Hermann394131e2008-10-18 21:14:13 +0000826 if (first_flash == flashchips
827 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000828 break;
829
Peter Stuge483b8f02008-09-03 23:10:05 +0000830notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000831 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000832 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000833
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000834 if (!flash || !flash->name)
835 return NULL;
836
Uwe Hermann9899cad2009-06-28 21:47:57 +0000837 printf("Found chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
838 flash->vendor, flash->name, flash->total_size,
839 flashbuses_to_text(flash->bustype), base);
840
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000841 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000842}
843
Stefan Reinauere3705282005-12-18 16:41:10 +0000844int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000845{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000846 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000847 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000848
Uwe Hermanna502dce2007-10-17 23:55:15 +0000849 printf("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000850
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000851 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000852
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000853 if (!ret)
854 printf("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000855
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000856 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000857}
858
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +0000859int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000860{
861 unsigned long numbytes;
862 FILE *image;
863 unsigned long size = flash->total_size * 1024;
864 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000865
866 if (!filename) {
867 printf("Error: No filename specified.\n");
868 return 1;
869 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +0000870 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000871 perror(filename);
872 exit(1);
873 }
874 printf("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000875 if (!flash->read) {
876 printf("FAILED!\n");
877 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
878 return 1;
879 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000880 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000881
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000882 numbytes = fwrite(buf, 1, size, image);
883 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +0000884 free(buf);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000885 printf("%s.\n", numbytes == size ? "done" : "FAILED");
886 if (numbytes != size)
887 return 1;
888 return 0;
889}
890
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000891/* This function shares a lot of its structure with erase_flash().
892 * Even if an error is found, the function will keep going and check the rest.
893 */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000894int selfcheck_eraseblocks(struct flashchip *flash)
895{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000896 int i, j, k;
897 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000898
899 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
900 unsigned int done = 0;
901 struct block_eraser eraser = flash->block_erasers[k];
902
903 for (i = 0; i < NUM_ERASEREGIONS; i++) {
904 /* Blocks with zero size are bugs in flashchips.c. */
905 if (eraser.eraseblocks[i].count &&
906 !eraser.eraseblocks[i].size) {
907 msg_gerr("ERROR: Flash chip %s erase function "
908 "%i region %i has size 0. Please report"
909 " a bug at flashrom@flashrom.org\n",
910 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000911 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000912 }
913 /* Blocks with zero count are bugs in flashchips.c. */
914 if (!eraser.eraseblocks[i].count &&
915 eraser.eraseblocks[i].size) {
916 msg_gerr("ERROR: Flash chip %s erase function "
917 "%i region %i has count 0. Please report"
918 " a bug at flashrom@flashrom.org\n",
919 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000920 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000921 }
922 done += eraser.eraseblocks[i].count *
923 eraser.eraseblocks[i].size;
924 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000925 /* Empty eraseblock definition with erase function. */
926 if (!done && eraser.block_erase)
927 msg_pspew("Strange: Empty eraseblock definition with "
928 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000929 if (!done)
930 continue;
931 if (done != flash->total_size * 1024) {
932 msg_gerr("ERROR: Flash chip %s erase function %i "
933 "region walking resulted in 0x%06x bytes total,"
934 " expected 0x%06x bytes. Please report a bug at"
935 " flashrom@flashrom.org\n", flash->name, k,
936 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000937 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000938 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000939 if (!eraser.block_erase)
940 continue;
941 /* Check if there are identical erase functions for different
942 * layouts. That would imply "magic" erase functions. The
943 * easiest way to check this is with function pointers.
944 */
945 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++)
946 if (eraser.block_erase ==
947 flash->block_erasers[j].block_erase) {
948 msg_gerr("ERROR: Flash chip %s erase function "
949 "%i and %i are identical. Please report"
950 " a bug at flashrom@flashrom.org\n",
951 flash->name, k, j);
952 ret = 1;
953 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000954 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000955 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000956}
957
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000958int erase_flash(struct flashchip *flash)
959{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000960 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000961 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000962
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000963 printf("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000964 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000965 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000966 struct block_eraser eraser = flash->block_erasers[k];
967
968 printf_debug("Looking at blockwise erase function %i... ", k);
969 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
970 printf_debug("not defined. "
971 "Looking for another erase function.\n");
972 continue;
973 }
974 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
975 printf_debug("eraseblock layout is known, but no "
976 "matching block erase function found. "
977 "Looking for another erase function.\n");
978 continue;
979 }
980 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
981 printf_debug("block erase function found, but "
982 "eraseblock layout is unknown. "
983 "Looking for another erase function.\n");
984 continue;
985 }
986 found = 1;
987 printf_debug("trying... ");
988 for (i = 0; i < NUM_ERASEREGIONS; i++) {
989 /* count==0 for all automatically initialized array
990 * members so the loop below won't be executed for them.
991 */
992 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000993 start = done + eraser.eraseblocks[i].size * j;
994 len = eraser.eraseblocks[i].size;
995 printf_debug("0x%06x-0x%06x, ", start,
996 start + len - 1);
997 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000998 if (ret)
999 break;
1000 }
1001 if (ret)
1002 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001003 done += eraser.eraseblocks[i].count *
1004 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001005 }
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001006 printf_debug("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001007 /* If everything is OK, don't try another erase function. */
1008 if (!ret)
1009 break;
1010 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001011 if (!found) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001012 fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
1013 return 1;
1014 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001015
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001016 if (ret) {
1017 fprintf(stderr, "FAILED!\n");
1018 } else {
1019 printf("SUCCESS.\n");
1020 }
1021 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001022}
1023
Uwe Hermannc67d0372009-10-01 18:40:02 +00001024void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001025{
1026 fprintf(stderr, "Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001027 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
1028 "mail flashrom@flashrom.org!\n--------------------"
1029 "-----------------------------------------------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001030 "DO NOT REBOOT OR POWEROFF!\n");
1031}
1032
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001033/* The way to go if you want a delimited list of programmers*/
1034void list_programmers(char *delim)
1035{
1036 enum programmer p;
1037 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1038 printf("%s", programmer_table[p].name);
1039 if (p < PROGRAMMER_INVALID - 1)
1040 printf("%s", delim);
1041 }
1042 printf("\n");
1043}
1044
Bernhard Walle201bde32008-01-21 15:24:22 +00001045void print_version(void)
1046{
Carl-Daniel Hailfingera80cfbc2009-07-22 20:13:00 +00001047 printf("flashrom v%s\n", flashrom_version);
Bernhard Walle201bde32008-01-21 15:24:22 +00001048}
1049
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001050int selfcheck(void)
1051{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001052 int ret = 0;
1053 struct flashchip *flash;
1054
1055 /* Safety check. Instead of aborting after the first error, check
1056 * if more errors exist.
1057 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001058 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
1059 fprintf(stderr, "Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001060 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001061 }
1062 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
1063 fprintf(stderr, "SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001064 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001065 }
1066#if BITBANG_SPI_SUPPORT == 1
1067 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
1068 fprintf(stderr, "Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001069 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001070 }
1071#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001072 for (flash = flashchips; flash && flash->name; flash++)
1073 if (selfcheck_eraseblocks(flash))
1074 ret = 1;
1075 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001076}
1077
1078void check_chip_supported(struct flashchip *flash)
1079{
1080 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
1081 printf("===\n");
1082 if (flash->tested & TEST_BAD_MASK) {
1083 printf("This flash part has status NOT WORKING for operations:");
1084 if (flash->tested & TEST_BAD_PROBE)
1085 printf(" PROBE");
1086 if (flash->tested & TEST_BAD_READ)
1087 printf(" READ");
1088 if (flash->tested & TEST_BAD_ERASE)
1089 printf(" ERASE");
1090 if (flash->tested & TEST_BAD_WRITE)
1091 printf(" WRITE");
1092 printf("\n");
1093 }
1094 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1095 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1096 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1097 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
1098 printf("This flash part has status UNTESTED for operations:");
1099 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
1100 printf(" PROBE");
1101 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
1102 printf(" READ");
1103 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
1104 printf(" ERASE");
1105 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
1106 printf(" WRITE");
1107 printf("\n");
1108 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001109 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001110 printf("Please email a report to flashrom@flashrom.org if any "
1111 "of the above operations\nwork correctly for you with "
1112 "this flash part. Please include the flashrom\noutput "
1113 "with the additional -V option for all operations you "
1114 "tested (-V, -rV,\n-wV, -EV), and mention which "
1115 "mainboard or programmer you tested. Thanks for your "
1116 "help!\n===\n");
1117 }
1118}
1119
Ollie Lho761bf1b2004-03-20 16:46:10 +00001120int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001121{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001122 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001123}
1124
1125/* This function signature is horrible. We need to design a better interface,
1126 * but right now it allows us to split off the CLI code.
1127 */
1128int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1129{
1130 uint8_t *buf;
1131 unsigned long numbytes;
1132 FILE *image;
1133 int ret = 0;
1134 unsigned long size;
1135
1136 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001137 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001138
Ollie Lhoefa28582004-12-08 20:10:01 +00001139 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001140 if (flash->tested & TEST_BAD_ERASE) {
1141 fprintf(stderr, "Erase is not working on this chip. ");
1142 if (!force) {
1143 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001144 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001145 return 1;
1146 } else {
1147 fprintf(stderr, "Continuing anyway.\n");
1148 }
1149 }
1150 if (erase_flash(flash)) {
1151 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001152 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001153 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001154 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001155 } else if (read_it) {
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001156 if (read_flash(flash, filename)) {
1157 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001158 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001159 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001160 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001161 struct stat image_stat;
1162
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001163 if (flash->tested & TEST_BAD_ERASE) {
1164 fprintf(stderr, "Erase is not working on this chip "
1165 "and erase is needed for write. ");
1166 if (!force) {
1167 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001168 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001169 return 1;
1170 } else {
1171 fprintf(stderr, "Continuing anyway.\n");
1172 }
1173 }
1174 if (flash->tested & TEST_BAD_WRITE) {
1175 fprintf(stderr, "Write is not working on this chip. ");
1176 if (!force) {
1177 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001178 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001179 return 1;
1180 } else {
1181 fprintf(stderr, "Continuing anyway.\n");
1182 }
1183 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001184 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001185 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001186 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001187 exit(1);
1188 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001189 if (fstat(fileno(image), &image_stat) != 0) {
1190 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001191 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001192 exit(1);
1193 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001194 if (image_stat.st_size != flash->total_size * 1024) {
Uwe Hermann793bdcd2008-05-22 22:47:04 +00001195 fprintf(stderr, "Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001196 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001197 exit(1);
1198 }
1199
Peter Stuge1fec0f32009-01-12 21:00:35 +00001200 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001201#if INTERNAL_SUPPORT == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001202 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001203#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001204 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001205 if (numbytes != size) {
1206 fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001207 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001208 return 1;
1209 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001210 }
1211
Ollie Lho184a4042005-11-26 21:55:36 +00001212 // This should be moved into each flash part's code to do it
1213 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001214 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001215
Ollie Lho184a4042005-11-26 21:55:36 +00001216 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001217
Peter Stugef31104c2008-04-28 14:47:30 +00001218 if (write_it) {
Paul Foxd51410c2009-06-12 08:04:08 +00001219 printf("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001220 if (!flash->write) {
1221 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001222 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001223 return 1;
1224 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001225 ret = flash->write(flash, buf);
1226 if (ret) {
1227 fprintf(stderr, "FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001228 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001229 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001230 return 1;
1231 } else {
1232 printf("COMPLETE.\n");
1233 }
Peter Stugef31104c2008-04-28 14:47:30 +00001234 }
Ollie Lho184a4042005-11-26 21:55:36 +00001235
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001236 if (verify_it) {
1237 /* Work around chips which need some time to calm down. */
1238 if (write_it)
1239 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001240 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001241 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001242 * might have an emergency situation.
1243 */
1244 if (ret && write_it)
1245 emergency_help_message();
1246 }
Ollie Lho184a4042005-11-26 21:55:36 +00001247
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001248 programmer_shutdown();
1249
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001250 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001251}