blob: c3e32a1f4513b26c4aa4cb30ec62471835571374 [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
Uwe Hermann09e04f72009-05-16 22:36:00 +0000311int programmer_init(void)
312{
313 return programmer_table[programmer].init();
314}
315
316int programmer_shutdown(void)
317{
318 return programmer_table[programmer].shutdown();
319}
320
321void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
322 size_t len)
323{
324 return programmer_table[programmer].map_flash_region(descr,
325 phys_addr, len);
326}
327
328void programmer_unmap_flash_region(void *virt_addr, size_t len)
329{
330 programmer_table[programmer].unmap_flash_region(virt_addr, len);
331}
332
333void chip_writeb(uint8_t val, chipaddr addr)
334{
335 programmer_table[programmer].chip_writeb(val, addr);
336}
337
338void chip_writew(uint16_t val, chipaddr addr)
339{
340 programmer_table[programmer].chip_writew(val, addr);
341}
342
343void chip_writel(uint32_t val, chipaddr addr)
344{
345 programmer_table[programmer].chip_writel(val, addr);
346}
347
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000348void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
349{
350 programmer_table[programmer].chip_writen(buf, addr, len);
351}
352
Uwe Hermann09e04f72009-05-16 22:36:00 +0000353uint8_t chip_readb(const chipaddr addr)
354{
355 return programmer_table[programmer].chip_readb(addr);
356}
357
358uint16_t chip_readw(const chipaddr addr)
359{
360 return programmer_table[programmer].chip_readw(addr);
361}
362
363uint32_t chip_readl(const chipaddr addr)
364{
365 return programmer_table[programmer].chip_readl(addr);
366}
367
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000368void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
369{
370 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000371}
372
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000373void programmer_delay(int usecs)
374{
375 programmer_table[programmer].delay(usecs);
376}
377
Peter Stuge776d2022009-01-26 00:39:57 +0000378void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000379{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000380 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000381 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000382 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000383 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000384}
385
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000386int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000387{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000388 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000389
390 return 0;
391}
392
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000393unsigned long flashbase = 0;
394
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000395int min(int a, int b)
396{
397 return (a < b) ? a : b;
398}
399
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000400int max(int a, int b)
401{
402 return (a > b) ? a : b;
403}
404
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000405int bitcount(unsigned long a)
406{
407 int i = 0;
408 for (; a != 0; a >>= 1)
409 if (a & 1)
410 i++;
411 return i;
412}
413
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000414char *strcat_realloc(char *dest, const char *src)
415{
416 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
417 if (!dest)
418 return NULL;
419 strcat(dest, src);
420 return dest;
421}
422
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000423/* This is a somewhat hacked function similar in some ways to strtok().
424 * It will look for needle in haystack, return a copy of needle and remove
425 * everything from the first occurrence of needle to the next delimiter
426 * from haystack.
427 */
428char *extract_param(char **haystack, char *needle, char *delim)
429{
430 char *param_pos, *rest, *tmp;
431 char *dev = NULL;
432 int devlen;
433
434 param_pos = strstr(*haystack, needle);
435 do {
436 if (!param_pos)
437 return NULL;
438 /* Beginning of the string? */
439 if (param_pos == *haystack)
440 break;
441 /* After a delimiter? */
442 if (strchr(delim, *(param_pos - 1)))
443 break;
444 /* Continue searching. */
445 param_pos++;
446 param_pos = strstr(param_pos, needle);
447 } while (1);
448
449 if (param_pos) {
450 param_pos += strlen(needle);
451 devlen = strcspn(param_pos, delim);
452 if (devlen) {
453 dev = malloc(devlen + 1);
454 if (!dev) {
455 fprintf(stderr, "Out of memory!\n");
456 exit(1);
457 }
458 strncpy(dev, param_pos, devlen);
459 dev[devlen] = '\0';
460 }
461 rest = param_pos + devlen;
462 rest += strspn(rest, delim);
463 param_pos -= strlen(needle);
464 memmove(param_pos, rest, strlen(rest) + 1);
465 tmp = realloc(*haystack, strlen(*haystack) + 1);
466 if (!tmp) {
467 fprintf(stderr, "Out of memory!\n");
468 exit(1);
469 }
470 *haystack = tmp;
471 }
472
473
474 return dev;
475}
476
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000477/* start is an offset to the base address of the flash chip */
478int check_erased_range(struct flashchip *flash, int start, int len)
479{
480 int ret;
481 uint8_t *cmpbuf = malloc(len);
482
483 if (!cmpbuf) {
484 fprintf(stderr, "Could not allocate memory!\n");
485 exit(1);
486 }
487 memset(cmpbuf, 0xff, len);
488 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
489 free(cmpbuf);
490 return ret;
491}
492
493/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000494 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
495 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000496 * @start offset to the base address of the flash chip
497 * @len length of the verified area
498 * @message string to print in the "FAILED" message
499 * @return 0 for success, -1 for failure
500 */
501int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
502{
503 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000504 int page_size = flash->page_size;
505 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000506 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000507
508 if (!len)
509 goto out_free;
510
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000511 if (!flash->read) {
512 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
513 return 1;
514 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000515 if (!readbuf) {
516 fprintf(stderr, "Could not allocate memory!\n");
517 exit(1);
518 }
519
520 if (start + len > flash->total_size * 1024) {
521 fprintf(stderr, "Error: %s called with start 0x%x + len 0x%x >"
522 " total_size 0x%x\n", __func__, start, len,
523 flash->total_size * 1024);
524 ret = -1;
525 goto out_free;
526 }
527 if (!message)
528 message = "VERIFY";
529
530 /* Warning: This loop has a very unusual condition and body.
531 * The loop needs to go through each page with at least one affected
532 * byte. The lowest page number is (start / page_size) since that
533 * division rounds down. The highest page number we want is the page
534 * where the last byte of the range lives. That last byte has the
535 * address (start + len - 1), thus the highest page number is
536 * (start + len - 1) / page_size. Since we want to include that last
537 * page as well, the loop condition uses <=.
538 */
539 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
540 /* Byte position of the first byte in the range in this page. */
541 starthere = max(start, i * page_size);
542 /* Length of bytes in the range in this page. */
543 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000544 flash->read(flash, readbuf, starthere, lenhere);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000545 for (j = 0; j < lenhere; j++) {
546 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000547 /* Only print the first failure. */
548 if (!failcount++)
549 fprintf(stderr, "%s FAILED at 0x%08x! "
550 "Expected=0x%02x, Read=0x%02x,",
551 message, starthere + j,
552 cmpbuf[starthere - start + j],
553 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000554 }
555 }
556 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000557 if (failcount) {
558 fprintf(stderr, " failed byte count from 0x%08x-0x%08x: 0x%x\n",
559 start, start + len - 1, failcount);
560 ret = -1;
561 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000562
563out_free:
564 free(readbuf);
565 return ret;
566}
567
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000568/* This function generates various test patterns useful for testing controller
569 * and chip communication as well as chip behaviour.
570 *
571 * If a byte can be written multiple times, each time keeping 0-bits at 0
572 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
573 * is essentially an AND operation. That's also the reason why this function
574 * provides the result of AND between various patterns.
575 *
576 * Below is a list of patterns (and their block length).
577 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
578 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
579 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
580 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
581 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
582 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
583 * Pattern 6 is 00 (1 Byte)
584 * Pattern 7 is ff (1 Byte)
585 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
586 * byte block.
587 *
588 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
589 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
590 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
591 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
592 * Pattern 12 is 00 (1 Byte)
593 * Pattern 13 is ff (1 Byte)
594 * Patterns 8-13 have no block number.
595 *
596 * Patterns 0-3 are created to detect and efficiently diagnose communication
597 * slips like missed bits or bytes and their repetitive nature gives good visual
598 * cues to the person inspecting the results. In addition, the following holds:
599 * AND Pattern 0/1 == Pattern 4
600 * AND Pattern 2/3 == Pattern 5
601 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
602 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
603 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
604 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
605 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
606 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
607 * Besides that, they provide for bit testing of the last two bytes of every
608 * 256 byte block which contains the block number for patterns 0-6.
609 * Patterns 10-11 are special purpose for detecting subblock aliasing with
610 * block sizes >256 bytes (some Dataflash chips etc.)
611 * AND Pattern 8/9 == Pattern 12
612 * AND Pattern 10/11 == Pattern 12
613 * Pattern 13 is the completely erased state.
614 * None of the patterns can detect aliasing at boundaries which are a multiple
615 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
616 */
617int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
618{
619 int i;
620
621 if (!buf) {
622 fprintf(stderr, "Invalid buffer!\n");
623 return 1;
624 }
625
626 switch (variant) {
627 case 0:
628 for (i = 0; i < size; i++)
629 buf[i] = (i & 0xf) << 4 | 0x5;
630 break;
631 case 1:
632 for (i = 0; i < size; i++)
633 buf[i] = (i & 0xf) << 4 | 0xa;
634 break;
635 case 2:
636 for (i = 0; i < size; i++)
637 buf[i] = 0x50 | (i & 0xf);
638 break;
639 case 3:
640 for (i = 0; i < size; i++)
641 buf[i] = 0xa0 | (i & 0xf);
642 break;
643 case 4:
644 for (i = 0; i < size; i++)
645 buf[i] = (i & 0xf) << 4;
646 break;
647 case 5:
648 for (i = 0; i < size; i++)
649 buf[i] = i & 0xf;
650 break;
651 case 6:
652 memset(buf, 0x00, size);
653 break;
654 case 7:
655 memset(buf, 0xff, size);
656 break;
657 case 8:
658 for (i = 0; i < size; i++)
659 buf[i] = i & 0xff;
660 break;
661 case 9:
662 for (i = 0; i < size; i++)
663 buf[i] = ~(i & 0xff);
664 break;
665 case 10:
666 for (i = 0; i < size % 2; i++) {
667 buf[i * 2] = (i >> 8) & 0xff;
668 buf[i * 2 + 1] = i & 0xff;
669 }
670 if (size & 0x1)
671 buf[i * 2] = (i >> 8) & 0xff;
672 break;
673 case 11:
674 for (i = 0; i < size % 2; i++) {
675 buf[i * 2] = ~((i >> 8) & 0xff);
676 buf[i * 2 + 1] = ~(i & 0xff);
677 }
678 if (size & 0x1)
679 buf[i * 2] = ~((i >> 8) & 0xff);
680 break;
681 case 12:
682 memset(buf, 0x00, size);
683 break;
684 case 13:
685 memset(buf, 0xff, size);
686 break;
687 }
688
689 if ((variant >= 0) && (variant <= 7)) {
690 /* Write block number in the last two bytes of each 256-byte
691 * block, big endian for easier reading of the hexdump.
692 * Note that this wraps around for chips larger than 2^24 bytes
693 * (16 MB).
694 */
695 for (i = 0; i < size / 256; i++) {
696 buf[i * 256 + 254] = (i >> 8) & 0xff;
697 buf[i * 256 + 255] = i & 0xff;
698 }
699 }
700
701 return 0;
702}
703
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000704int check_max_decode(enum chipbustype buses, uint32_t size)
705{
706 int limitexceeded = 0;
707 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
708 (max_rom_decode.parallel < size)) {
709 limitexceeded++;
710 printf_debug("Chip size %u kB is bigger than supported "
711 "size %u kB of chipset/board/programmer "
712 "for %s interface, "
713 "probe/read/erase/write may fail. ", size / 1024,
714 max_rom_decode.parallel / 1024, "Parallel");
715 }
716 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
717 limitexceeded++;
718 printf_debug("Chip size %u kB is bigger than supported "
719 "size %u kB of chipset/board/programmer "
720 "for %s interface, "
721 "probe/read/erase/write may fail. ", size / 1024,
722 max_rom_decode.lpc / 1024, "LPC");
723 }
724 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
725 limitexceeded++;
726 printf_debug("Chip size %u kB is bigger than supported "
727 "size %u kB of chipset/board/programmer "
728 "for %s interface, "
729 "probe/read/erase/write may fail. ", size / 1024,
730 max_rom_decode.fwh / 1024, "FWH");
731 }
732 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
733 limitexceeded++;
734 printf_debug("Chip size %u kB is bigger than supported "
735 "size %u kB of chipset/board/programmer "
736 "for %s interface, "
737 "probe/read/erase/write may fail. ", size / 1024,
738 max_rom_decode.spi / 1024, "SPI");
739 }
740 if (!limitexceeded)
741 return 0;
742 /* Sometimes chip and programmer have more than one bus in common,
743 * and the limit is not exceeded on all buses. Tell the user.
744 */
745 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000746 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000747 printf_debug("There is at least one common chip/programmer "
748 "interface which can support a chip of this size. "
749 "You can try --force at your own risk.\n");
750 return 1;
751}
752
Peter Stuge483b8f02008-09-03 23:10:05 +0000753struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000754{
Peter Stuge483b8f02008-09-03 23:10:05 +0000755 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000756 unsigned long base = 0;
757 uint32_t size;
758 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000759 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000760
Peter Stuge483b8f02008-09-03 23:10:05 +0000761 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000762 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000763 continue;
Stefan Reinauerac378972008-03-17 22:59:40 +0000764 printf_debug("Probing for %s %s, %d KB: ",
765 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000766 if (!flash->probe && !force) {
Peter Stugef31104c2008-04-28 14:47:30 +0000767 printf_debug("failed! flashrom has no probe function for this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000768 continue;
769 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000770 buses_common = buses_supported & flash->bustype;
771 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000772 tmp = flashbuses_to_text(buses_supported);
773 printf_debug("skipped. Host bus type %s ", tmp);
774 free(tmp);
775 tmp = flashbuses_to_text(flash->bustype);
776 printf_debug("and chip bus type %s are incompatible.\n", tmp);
777 free(tmp);
778 continue;
779 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000780
Ollie Lhocbbf1252004-03-17 22:22:08 +0000781 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000782 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000783
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000784 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000785 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000786
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000787 if (force)
788 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000789
Peter Stuge483b8f02008-09-03 23:10:05 +0000790 if (flash->probe(flash) != 1)
791 goto notfound;
792
Uwe Hermann394131e2008-10-18 21:14:13 +0000793 if (first_flash == flashchips
794 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000795 break;
796
Peter Stuge483b8f02008-09-03 23:10:05 +0000797notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000798 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000799 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000800
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000801 if (!flash || !flash->name)
802 return NULL;
803
Uwe Hermann9899cad2009-06-28 21:47:57 +0000804 printf("Found chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
805 flash->vendor, flash->name, flash->total_size,
806 flashbuses_to_text(flash->bustype), base);
807
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000808 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000809}
810
Stefan Reinauere3705282005-12-18 16:41:10 +0000811int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000812{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000813 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000814 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000815
Uwe Hermanna502dce2007-10-17 23:55:15 +0000816 printf("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000817
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000818 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000819
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000820 if (!ret)
821 printf("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000822
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000823 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000824}
825
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +0000826int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000827{
828 unsigned long numbytes;
829 FILE *image;
830 unsigned long size = flash->total_size * 1024;
831 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000832
833 if (!filename) {
834 printf("Error: No filename specified.\n");
835 return 1;
836 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000837 if ((image = fopen(filename, "w")) == NULL) {
838 perror(filename);
839 exit(1);
840 }
841 printf("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000842 if (!flash->read) {
843 printf("FAILED!\n");
844 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
845 return 1;
846 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000847 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000848
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000849 numbytes = fwrite(buf, 1, size, image);
850 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +0000851 free(buf);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000852 printf("%s.\n", numbytes == size ? "done" : "FAILED");
853 if (numbytes != size)
854 return 1;
855 return 0;
856}
857
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000858/* This function shares a lot of its structure with erase_flash().
859 * Even if an error is found, the function will keep going and check the rest.
860 */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000861int selfcheck_eraseblocks(struct flashchip *flash)
862{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000863 int i, j, k;
864 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000865
866 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
867 unsigned int done = 0;
868 struct block_eraser eraser = flash->block_erasers[k];
869
870 for (i = 0; i < NUM_ERASEREGIONS; i++) {
871 /* Blocks with zero size are bugs in flashchips.c. */
872 if (eraser.eraseblocks[i].count &&
873 !eraser.eraseblocks[i].size) {
874 msg_gerr("ERROR: Flash chip %s erase function "
875 "%i region %i has size 0. Please report"
876 " a bug at flashrom@flashrom.org\n",
877 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000878 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000879 }
880 /* Blocks with zero count are bugs in flashchips.c. */
881 if (!eraser.eraseblocks[i].count &&
882 eraser.eraseblocks[i].size) {
883 msg_gerr("ERROR: Flash chip %s erase function "
884 "%i region %i has count 0. Please report"
885 " a bug at flashrom@flashrom.org\n",
886 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000887 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000888 }
889 done += eraser.eraseblocks[i].count *
890 eraser.eraseblocks[i].size;
891 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000892 /* Empty eraseblock definition with erase function. */
893 if (!done && eraser.block_erase)
894 msg_pspew("Strange: Empty eraseblock definition with "
895 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000896 if (!done)
897 continue;
898 if (done != flash->total_size * 1024) {
899 msg_gerr("ERROR: Flash chip %s erase function %i "
900 "region walking resulted in 0x%06x bytes total,"
901 " expected 0x%06x bytes. Please report a bug at"
902 " flashrom@flashrom.org\n", flash->name, k,
903 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000904 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000905 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000906 if (!eraser.block_erase)
907 continue;
908 /* Check if there are identical erase functions for different
909 * layouts. That would imply "magic" erase functions. The
910 * easiest way to check this is with function pointers.
911 */
912 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++)
913 if (eraser.block_erase ==
914 flash->block_erasers[j].block_erase) {
915 msg_gerr("ERROR: Flash chip %s erase function "
916 "%i and %i are identical. Please report"
917 " a bug at flashrom@flashrom.org\n",
918 flash->name, k, j);
919 ret = 1;
920 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000921 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000922 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000923}
924
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000925int erase_flash(struct flashchip *flash)
926{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000927 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000928 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000929
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000930 printf("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000931 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000932 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000933 struct block_eraser eraser = flash->block_erasers[k];
934
935 printf_debug("Looking at blockwise erase function %i... ", k);
936 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
937 printf_debug("not defined. "
938 "Looking for another erase function.\n");
939 continue;
940 }
941 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
942 printf_debug("eraseblock layout is known, but no "
943 "matching block erase function found. "
944 "Looking for another erase function.\n");
945 continue;
946 }
947 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
948 printf_debug("block erase function found, but "
949 "eraseblock layout is unknown. "
950 "Looking for another erase function.\n");
951 continue;
952 }
953 found = 1;
954 printf_debug("trying... ");
955 for (i = 0; i < NUM_ERASEREGIONS; i++) {
956 /* count==0 for all automatically initialized array
957 * members so the loop below won't be executed for them.
958 */
959 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000960 start = done + eraser.eraseblocks[i].size * j;
961 len = eraser.eraseblocks[i].size;
962 printf_debug("0x%06x-0x%06x, ", start,
963 start + len - 1);
964 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000965 if (ret)
966 break;
967 }
968 if (ret)
969 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000970 done += eraser.eraseblocks[i].count *
971 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000972 }
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000973 printf_debug("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000974 /* If everything is OK, don't try another erase function. */
975 if (!ret)
976 break;
977 }
978 /* If no block erase function was found or block erase failed, retry. */
979 if ((!found || ret) && (flash->erase)) {
980 found = 1;
981 printf_debug("Trying whole-chip erase function... ");
982 ret = flash->erase(flash);
983 }
984 if (!found) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000985 fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
986 return 1;
987 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +0000988
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000989 if (ret) {
990 fprintf(stderr, "FAILED!\n");
991 } else {
992 printf("SUCCESS.\n");
993 }
994 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000995}
996
Uwe Hermannc67d0372009-10-01 18:40:02 +0000997void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000998{
999 fprintf(stderr, "Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001000 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
1001 "mail flashrom@flashrom.org!\n--------------------"
1002 "-----------------------------------------------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001003 "DO NOT REBOOT OR POWEROFF!\n");
1004}
1005
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001006/* The way to go if you want a delimited list of programmers*/
1007void list_programmers(char *delim)
1008{
1009 enum programmer p;
1010 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1011 printf("%s", programmer_table[p].name);
1012 if (p < PROGRAMMER_INVALID - 1)
1013 printf("%s", delim);
1014 }
1015 printf("\n");
1016}
1017
Bernhard Walle201bde32008-01-21 15:24:22 +00001018void print_version(void)
1019{
Carl-Daniel Hailfingera80cfbc2009-07-22 20:13:00 +00001020 printf("flashrom v%s\n", flashrom_version);
Bernhard Walle201bde32008-01-21 15:24:22 +00001021}
1022
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001023int selfcheck(void)
1024{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001025 int ret = 0;
1026 struct flashchip *flash;
1027
1028 /* Safety check. Instead of aborting after the first error, check
1029 * if more errors exist.
1030 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001031 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
1032 fprintf(stderr, "Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001033 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001034 }
1035 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
1036 fprintf(stderr, "SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001037 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001038 }
1039#if BITBANG_SPI_SUPPORT == 1
1040 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
1041 fprintf(stderr, "Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001042 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001043 }
1044#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001045 for (flash = flashchips; flash && flash->name; flash++)
1046 if (selfcheck_eraseblocks(flash))
1047 ret = 1;
1048 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001049}
1050
1051void check_chip_supported(struct flashchip *flash)
1052{
1053 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
1054 printf("===\n");
1055 if (flash->tested & TEST_BAD_MASK) {
1056 printf("This flash part has status NOT WORKING for operations:");
1057 if (flash->tested & TEST_BAD_PROBE)
1058 printf(" PROBE");
1059 if (flash->tested & TEST_BAD_READ)
1060 printf(" READ");
1061 if (flash->tested & TEST_BAD_ERASE)
1062 printf(" ERASE");
1063 if (flash->tested & TEST_BAD_WRITE)
1064 printf(" WRITE");
1065 printf("\n");
1066 }
1067 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1068 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1069 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1070 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
1071 printf("This flash part has status UNTESTED for operations:");
1072 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
1073 printf(" PROBE");
1074 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
1075 printf(" READ");
1076 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
1077 printf(" ERASE");
1078 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
1079 printf(" WRITE");
1080 printf("\n");
1081 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001082 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001083 printf("Please email a report to flashrom@flashrom.org if any "
1084 "of the above operations\nwork correctly for you with "
1085 "this flash part. Please include the flashrom\noutput "
1086 "with the additional -V option for all operations you "
1087 "tested (-V, -rV,\n-wV, -EV), and mention which "
1088 "mainboard or programmer you tested. Thanks for your "
1089 "help!\n===\n");
1090 }
1091}
1092
Ollie Lho761bf1b2004-03-20 16:46:10 +00001093int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001094{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001095 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001096}
1097
1098/* This function signature is horrible. We need to design a better interface,
1099 * but right now it allows us to split off the CLI code.
1100 */
1101int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1102{
1103 uint8_t *buf;
1104 unsigned long numbytes;
1105 FILE *image;
1106 int ret = 0;
1107 unsigned long size;
1108
1109 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001110 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001111
Ollie Lhoefa28582004-12-08 20:10:01 +00001112 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001113 if (flash->tested & TEST_BAD_ERASE) {
1114 fprintf(stderr, "Erase is not working on this chip. ");
1115 if (!force) {
1116 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001117 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001118 return 1;
1119 } else {
1120 fprintf(stderr, "Continuing anyway.\n");
1121 }
1122 }
1123 if (erase_flash(flash)) {
1124 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001125 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001126 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001127 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001128 } else if (read_it) {
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001129 if (read_flash(flash, filename)) {
1130 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001131 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001132 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001133 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001134 struct stat image_stat;
1135
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001136 if (flash->tested & TEST_BAD_ERASE) {
1137 fprintf(stderr, "Erase is not working on this chip "
1138 "and erase is needed for write. ");
1139 if (!force) {
1140 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001141 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001142 return 1;
1143 } else {
1144 fprintf(stderr, "Continuing anyway.\n");
1145 }
1146 }
1147 if (flash->tested & TEST_BAD_WRITE) {
1148 fprintf(stderr, "Write is not working on this chip. ");
1149 if (!force) {
1150 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001151 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001152 return 1;
1153 } else {
1154 fprintf(stderr, "Continuing anyway.\n");
1155 }
1156 }
Ollie Lho761bf1b2004-03-20 16:46:10 +00001157 if ((image = fopen(filename, "r")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001158 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001159 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001160 exit(1);
1161 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001162 if (fstat(fileno(image), &image_stat) != 0) {
1163 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001164 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001165 exit(1);
1166 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001167 if (image_stat.st_size != flash->total_size * 1024) {
Uwe Hermann793bdcd2008-05-22 22:47:04 +00001168 fprintf(stderr, "Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001169 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001170 exit(1);
1171 }
1172
Peter Stuge1fec0f32009-01-12 21:00:35 +00001173 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001174#if INTERNAL_SUPPORT == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001175 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001176#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001177 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001178 if (numbytes != size) {
1179 fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001180 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001181 return 1;
1182 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001183 }
1184
Ollie Lho184a4042005-11-26 21:55:36 +00001185 // This should be moved into each flash part's code to do it
1186 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001187 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001188
Ollie Lho184a4042005-11-26 21:55:36 +00001189 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001190
Peter Stugef31104c2008-04-28 14:47:30 +00001191 if (write_it) {
Paul Foxd51410c2009-06-12 08:04:08 +00001192 printf("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001193 if (!flash->write) {
1194 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001195 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001196 return 1;
1197 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001198 ret = flash->write(flash, buf);
1199 if (ret) {
1200 fprintf(stderr, "FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001201 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001202 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001203 return 1;
1204 } else {
1205 printf("COMPLETE.\n");
1206 }
Peter Stugef31104c2008-04-28 14:47:30 +00001207 }
Ollie Lho184a4042005-11-26 21:55:36 +00001208
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001209 if (verify_it) {
1210 /* Work around chips which need some time to calm down. */
1211 if (write_it)
1212 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001213 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001214 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001215 * might have an emergency situation.
1216 */
1217 if (ret && write_it)
1218 emergency_help_message();
1219 }
Ollie Lho184a4042005-11-26 21:55:36 +00001220
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001221 programmer_shutdown();
1222
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001223 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001224}