blob: 5a3c44842c312d4da7b06b34cf81c1dbaefa1aff [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 */
47#if NIC3COM_SUPPORT+GFXNVIDIA_SUPPORT+DRKAISER_SUPPORT+SATASII_SUPPORT+FT2232_SPI_SUPPORT+SERPROG_SUPPORT+BUSPIRATE_SPI_SUPPORT > 1
48#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
72;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000073#endif
74
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000075char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000076
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000077/**
78 * flashrom defaults to Parallel/LPC/FWH flash devices. If a known host
79 * controller is found, the init routine sets the buses_supported bitfield to
80 * contain the supported buses for that controller.
81 */
82enum chipbustype buses_supported = CHIP_BUSTYPE_NONSPI;
83
84/**
85 * Programmers supporting multiple buses can have differing size limits on
86 * each bus. Store the limits for each bus in a common struct.
87 */
88struct decode_sizes max_rom_decode = {
89 .parallel = 0xffffffff,
90 .lpc = 0xffffffff,
91 .fwh = 0xffffffff,
92 .spi = 0xffffffff
93};
94
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000095const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000096#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000097 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000098 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000099 .init = internal_init,
100 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000101 .map_flash_region = physmap,
102 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000103 .chip_readb = internal_chip_readb,
104 .chip_readw = internal_chip_readw,
105 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000106 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000107 .chip_writeb = internal_chip_writeb,
108 .chip_writew = internal_chip_writew,
109 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000110 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000111 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000112 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000113#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000114
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000115#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000116 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000117 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000118 .init = dummy_init,
119 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000120 .map_flash_region = dummy_map,
121 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000122 .chip_readb = dummy_chip_readb,
123 .chip_readw = dummy_chip_readw,
124 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000125 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000126 .chip_writeb = dummy_chip_writeb,
127 .chip_writew = dummy_chip_writew,
128 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000129 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000130 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000131 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000132#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000133
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000134#if NIC3COM_SUPPORT == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000135 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000136 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000137 .init = nic3com_init,
138 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000139 .map_flash_region = fallback_map,
140 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000141 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000142 .chip_readw = fallback_chip_readw,
143 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000144 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000145 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000146 .chip_writew = fallback_chip_writew,
147 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000148 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000149 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000150 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000151#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000152
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000153#if GFXNVIDIA_SUPPORT == 1
154 {
155 .name = "gfxnvidia",
156 .init = gfxnvidia_init,
157 .shutdown = gfxnvidia_shutdown,
158 .map_flash_region = fallback_map,
159 .unmap_flash_region = fallback_unmap,
160 .chip_readb = gfxnvidia_chip_readb,
161 .chip_readw = fallback_chip_readw,
162 .chip_readl = fallback_chip_readl,
163 .chip_readn = fallback_chip_readn,
164 .chip_writeb = gfxnvidia_chip_writeb,
165 .chip_writew = fallback_chip_writew,
166 .chip_writel = fallback_chip_writel,
167 .chip_writen = fallback_chip_writen,
168 .delay = internal_delay,
169 },
170#endif
171
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000172#if DRKAISER_SUPPORT == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000173 {
TURBO Jb0912c02009-09-02 23:00:46 +0000174 .name = "drkaiser",
175 .init = drkaiser_init,
176 .shutdown = drkaiser_shutdown,
177 .map_flash_region = fallback_map,
178 .unmap_flash_region = fallback_unmap,
179 .chip_readb = drkaiser_chip_readb,
180 .chip_readw = fallback_chip_readw,
181 .chip_readl = fallback_chip_readl,
182 .chip_readn = fallback_chip_readn,
183 .chip_writeb = drkaiser_chip_writeb,
184 .chip_writew = fallback_chip_writew,
185 .chip_writel = fallback_chip_writel,
186 .chip_writen = fallback_chip_writen,
187 .delay = internal_delay,
188 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000189#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000190
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000191#if SATASII_SUPPORT == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000192 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000193 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000194 .init = satasii_init,
195 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000196 .map_flash_region = fallback_map,
197 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000198 .chip_readb = satasii_chip_readb,
199 .chip_readw = fallback_chip_readw,
200 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000201 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000202 .chip_writeb = satasii_chip_writeb,
203 .chip_writew = fallback_chip_writew,
204 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000205 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000206 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000207 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000208#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000209
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000210#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000211 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000212 .name = "it87spi",
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000213 .init = it87spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000214 .shutdown = noop_shutdown,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000215 .map_flash_region = fallback_map,
216 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000217 .chip_readb = noop_chip_readb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000218 .chip_readw = fallback_chip_readw,
219 .chip_readl = fallback_chip_readl,
220 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000221 .chip_writeb = noop_chip_writeb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000222 .chip_writew = fallback_chip_writew,
223 .chip_writel = fallback_chip_writel,
224 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000225 .delay = internal_delay,
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000226 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000227#endif
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000228
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000229#if FT2232_SPI_SUPPORT == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000230 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000231 .name = "ft2232spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000232 .init = ft2232_spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000233 .shutdown = noop_shutdown, /* Missing shutdown */
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000234 .map_flash_region = fallback_map,
235 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000236 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000237 .chip_readw = fallback_chip_readw,
238 .chip_readl = fallback_chip_readl,
239 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000240 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000241 .chip_writew = fallback_chip_writew,
242 .chip_writel = fallback_chip_writel,
243 .chip_writen = fallback_chip_writen,
244 .delay = internal_delay,
245 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000246#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000247
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000248#if SERPROG_SUPPORT == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000249 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000250 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000251 .init = serprog_init,
252 .shutdown = serprog_shutdown,
253 .map_flash_region = fallback_map,
254 .unmap_flash_region = fallback_unmap,
255 .chip_readb = serprog_chip_readb,
256 .chip_readw = fallback_chip_readw,
257 .chip_readl = fallback_chip_readl,
258 .chip_readn = serprog_chip_readn,
259 .chip_writeb = serprog_chip_writeb,
260 .chip_writew = fallback_chip_writew,
261 .chip_writel = fallback_chip_writel,
262 .chip_writen = fallback_chip_writen,
263 .delay = serprog_delay,
264 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000265#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000266
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000267#if BUSPIRATE_SPI_SUPPORT == 1
268 {
269 .name = "buspiratespi",
270 .init = buspirate_spi_init,
271 .shutdown = buspirate_spi_shutdown,
272 .map_flash_region = fallback_map,
273 .unmap_flash_region = fallback_unmap,
274 .chip_readb = noop_chip_readb,
275 .chip_readw = fallback_chip_readw,
276 .chip_readl = fallback_chip_readl,
277 .chip_readn = fallback_chip_readn,
278 .chip_writeb = noop_chip_writeb,
279 .chip_writew = fallback_chip_writew,
280 .chip_writel = fallback_chip_writel,
281 .chip_writen = fallback_chip_writen,
282 .delay = internal_delay,
283 },
284#endif
285
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000286 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000287};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000288
Uwe Hermann09e04f72009-05-16 22:36:00 +0000289int programmer_init(void)
290{
291 return programmer_table[programmer].init();
292}
293
294int programmer_shutdown(void)
295{
296 return programmer_table[programmer].shutdown();
297}
298
299void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
300 size_t len)
301{
302 return programmer_table[programmer].map_flash_region(descr,
303 phys_addr, len);
304}
305
306void programmer_unmap_flash_region(void *virt_addr, size_t len)
307{
308 programmer_table[programmer].unmap_flash_region(virt_addr, len);
309}
310
311void chip_writeb(uint8_t val, chipaddr addr)
312{
313 programmer_table[programmer].chip_writeb(val, addr);
314}
315
316void chip_writew(uint16_t val, chipaddr addr)
317{
318 programmer_table[programmer].chip_writew(val, addr);
319}
320
321void chip_writel(uint32_t val, chipaddr addr)
322{
323 programmer_table[programmer].chip_writel(val, addr);
324}
325
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000326void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
327{
328 programmer_table[programmer].chip_writen(buf, addr, len);
329}
330
Uwe Hermann09e04f72009-05-16 22:36:00 +0000331uint8_t chip_readb(const chipaddr addr)
332{
333 return programmer_table[programmer].chip_readb(addr);
334}
335
336uint16_t chip_readw(const chipaddr addr)
337{
338 return programmer_table[programmer].chip_readw(addr);
339}
340
341uint32_t chip_readl(const chipaddr addr)
342{
343 return programmer_table[programmer].chip_readl(addr);
344}
345
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000346void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
347{
348 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000349}
350
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000351void programmer_delay(int usecs)
352{
353 programmer_table[programmer].delay(usecs);
354}
355
Peter Stuge776d2022009-01-26 00:39:57 +0000356void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000357{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000358 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000359 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000360 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000361 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000362}
363
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000364int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000365{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000366 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000367
368 return 0;
369}
370
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000371unsigned long flashbase = 0;
372
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000373int min(int a, int b)
374{
375 return (a < b) ? a : b;
376}
377
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000378int max(int a, int b)
379{
380 return (a > b) ? a : b;
381}
382
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000383int bitcount(unsigned long a)
384{
385 int i = 0;
386 for (; a != 0; a >>= 1)
387 if (a & 1)
388 i++;
389 return i;
390}
391
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000392char *strcat_realloc(char *dest, const char *src)
393{
394 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
395 if (!dest)
396 return NULL;
397 strcat(dest, src);
398 return dest;
399}
400
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000401/* This is a somewhat hacked function similar in some ways to strtok().
402 * It will look for needle in haystack, return a copy of needle and remove
403 * everything from the first occurrence of needle to the next delimiter
404 * from haystack.
405 */
406char *extract_param(char **haystack, char *needle, char *delim)
407{
408 char *param_pos, *rest, *tmp;
409 char *dev = NULL;
410 int devlen;
411
412 param_pos = strstr(*haystack, needle);
413 do {
414 if (!param_pos)
415 return NULL;
416 /* Beginning of the string? */
417 if (param_pos == *haystack)
418 break;
419 /* After a delimiter? */
420 if (strchr(delim, *(param_pos - 1)))
421 break;
422 /* Continue searching. */
423 param_pos++;
424 param_pos = strstr(param_pos, needle);
425 } while (1);
426
427 if (param_pos) {
428 param_pos += strlen(needle);
429 devlen = strcspn(param_pos, delim);
430 if (devlen) {
431 dev = malloc(devlen + 1);
432 if (!dev) {
433 fprintf(stderr, "Out of memory!\n");
434 exit(1);
435 }
436 strncpy(dev, param_pos, devlen);
437 dev[devlen] = '\0';
438 }
439 rest = param_pos + devlen;
440 rest += strspn(rest, delim);
441 param_pos -= strlen(needle);
442 memmove(param_pos, rest, strlen(rest) + 1);
443 tmp = realloc(*haystack, strlen(*haystack) + 1);
444 if (!tmp) {
445 fprintf(stderr, "Out of memory!\n");
446 exit(1);
447 }
448 *haystack = tmp;
449 }
450
451
452 return dev;
453}
454
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000455/* start is an offset to the base address of the flash chip */
456int check_erased_range(struct flashchip *flash, int start, int len)
457{
458 int ret;
459 uint8_t *cmpbuf = malloc(len);
460
461 if (!cmpbuf) {
462 fprintf(stderr, "Could not allocate memory!\n");
463 exit(1);
464 }
465 memset(cmpbuf, 0xff, len);
466 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
467 free(cmpbuf);
468 return ret;
469}
470
471/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000472 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
473 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000474 * @start offset to the base address of the flash chip
475 * @len length of the verified area
476 * @message string to print in the "FAILED" message
477 * @return 0 for success, -1 for failure
478 */
479int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
480{
481 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000482 int page_size = flash->page_size;
483 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000484 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000485
486 if (!len)
487 goto out_free;
488
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000489 if (!flash->read) {
490 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
491 return 1;
492 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000493 if (!readbuf) {
494 fprintf(stderr, "Could not allocate memory!\n");
495 exit(1);
496 }
497
498 if (start + len > flash->total_size * 1024) {
499 fprintf(stderr, "Error: %s called with start 0x%x + len 0x%x >"
500 " total_size 0x%x\n", __func__, start, len,
501 flash->total_size * 1024);
502 ret = -1;
503 goto out_free;
504 }
505 if (!message)
506 message = "VERIFY";
507
508 /* Warning: This loop has a very unusual condition and body.
509 * The loop needs to go through each page with at least one affected
510 * byte. The lowest page number is (start / page_size) since that
511 * division rounds down. The highest page number we want is the page
512 * where the last byte of the range lives. That last byte has the
513 * address (start + len - 1), thus the highest page number is
514 * (start + len - 1) / page_size. Since we want to include that last
515 * page as well, the loop condition uses <=.
516 */
517 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
518 /* Byte position of the first byte in the range in this page. */
519 starthere = max(start, i * page_size);
520 /* Length of bytes in the range in this page. */
521 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000522 flash->read(flash, readbuf, starthere, lenhere);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000523 for (j = 0; j < lenhere; j++) {
524 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000525 /* Only print the first failure. */
526 if (!failcount++)
527 fprintf(stderr, "%s FAILED at 0x%08x! "
528 "Expected=0x%02x, Read=0x%02x,",
529 message, starthere + j,
530 cmpbuf[starthere - start + j],
531 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000532 }
533 }
534 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000535 if (failcount) {
536 fprintf(stderr, " failed byte count from 0x%08x-0x%08x: 0x%x\n",
537 start, start + len - 1, failcount);
538 ret = -1;
539 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000540
541out_free:
542 free(readbuf);
543 return ret;
544}
545
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000546/* This function generates various test patterns useful for testing controller
547 * and chip communication as well as chip behaviour.
548 *
549 * If a byte can be written multiple times, each time keeping 0-bits at 0
550 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
551 * is essentially an AND operation. That's also the reason why this function
552 * provides the result of AND between various patterns.
553 *
554 * Below is a list of patterns (and their block length).
555 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
556 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
557 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
558 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
559 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
560 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
561 * Pattern 6 is 00 (1 Byte)
562 * Pattern 7 is ff (1 Byte)
563 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
564 * byte block.
565 *
566 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
567 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
568 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
569 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
570 * Pattern 12 is 00 (1 Byte)
571 * Pattern 13 is ff (1 Byte)
572 * Patterns 8-13 have no block number.
573 *
574 * Patterns 0-3 are created to detect and efficiently diagnose communication
575 * slips like missed bits or bytes and their repetitive nature gives good visual
576 * cues to the person inspecting the results. In addition, the following holds:
577 * AND Pattern 0/1 == Pattern 4
578 * AND Pattern 2/3 == Pattern 5
579 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
580 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
581 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
582 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
583 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
584 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
585 * Besides that, they provide for bit testing of the last two bytes of every
586 * 256 byte block which contains the block number for patterns 0-6.
587 * Patterns 10-11 are special purpose for detecting subblock aliasing with
588 * block sizes >256 bytes (some Dataflash chips etc.)
589 * AND Pattern 8/9 == Pattern 12
590 * AND Pattern 10/11 == Pattern 12
591 * Pattern 13 is the completely erased state.
592 * None of the patterns can detect aliasing at boundaries which are a multiple
593 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
594 */
595int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
596{
597 int i;
598
599 if (!buf) {
600 fprintf(stderr, "Invalid buffer!\n");
601 return 1;
602 }
603
604 switch (variant) {
605 case 0:
606 for (i = 0; i < size; i++)
607 buf[i] = (i & 0xf) << 4 | 0x5;
608 break;
609 case 1:
610 for (i = 0; i < size; i++)
611 buf[i] = (i & 0xf) << 4 | 0xa;
612 break;
613 case 2:
614 for (i = 0; i < size; i++)
615 buf[i] = 0x50 | (i & 0xf);
616 break;
617 case 3:
618 for (i = 0; i < size; i++)
619 buf[i] = 0xa0 | (i & 0xf);
620 break;
621 case 4:
622 for (i = 0; i < size; i++)
623 buf[i] = (i & 0xf) << 4;
624 break;
625 case 5:
626 for (i = 0; i < size; i++)
627 buf[i] = i & 0xf;
628 break;
629 case 6:
630 memset(buf, 0x00, size);
631 break;
632 case 7:
633 memset(buf, 0xff, size);
634 break;
635 case 8:
636 for (i = 0; i < size; i++)
637 buf[i] = i & 0xff;
638 break;
639 case 9:
640 for (i = 0; i < size; i++)
641 buf[i] = ~(i & 0xff);
642 break;
643 case 10:
644 for (i = 0; i < size % 2; i++) {
645 buf[i * 2] = (i >> 8) & 0xff;
646 buf[i * 2 + 1] = i & 0xff;
647 }
648 if (size & 0x1)
649 buf[i * 2] = (i >> 8) & 0xff;
650 break;
651 case 11:
652 for (i = 0; i < size % 2; i++) {
653 buf[i * 2] = ~((i >> 8) & 0xff);
654 buf[i * 2 + 1] = ~(i & 0xff);
655 }
656 if (size & 0x1)
657 buf[i * 2] = ~((i >> 8) & 0xff);
658 break;
659 case 12:
660 memset(buf, 0x00, size);
661 break;
662 case 13:
663 memset(buf, 0xff, size);
664 break;
665 }
666
667 if ((variant >= 0) && (variant <= 7)) {
668 /* Write block number in the last two bytes of each 256-byte
669 * block, big endian for easier reading of the hexdump.
670 * Note that this wraps around for chips larger than 2^24 bytes
671 * (16 MB).
672 */
673 for (i = 0; i < size / 256; i++) {
674 buf[i * 256 + 254] = (i >> 8) & 0xff;
675 buf[i * 256 + 255] = i & 0xff;
676 }
677 }
678
679 return 0;
680}
681
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000682int check_max_decode(enum chipbustype buses, uint32_t size)
683{
684 int limitexceeded = 0;
685 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
686 (max_rom_decode.parallel < size)) {
687 limitexceeded++;
688 printf_debug("Chip size %u kB is bigger than supported "
689 "size %u kB of chipset/board/programmer "
690 "for %s interface, "
691 "probe/read/erase/write may fail. ", size / 1024,
692 max_rom_decode.parallel / 1024, "Parallel");
693 }
694 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
695 limitexceeded++;
696 printf_debug("Chip size %u kB is bigger than supported "
697 "size %u kB of chipset/board/programmer "
698 "for %s interface, "
699 "probe/read/erase/write may fail. ", size / 1024,
700 max_rom_decode.lpc / 1024, "LPC");
701 }
702 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
703 limitexceeded++;
704 printf_debug("Chip size %u kB is bigger than supported "
705 "size %u kB of chipset/board/programmer "
706 "for %s interface, "
707 "probe/read/erase/write may fail. ", size / 1024,
708 max_rom_decode.fwh / 1024, "FWH");
709 }
710 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
711 limitexceeded++;
712 printf_debug("Chip size %u kB is bigger than supported "
713 "size %u kB of chipset/board/programmer "
714 "for %s interface, "
715 "probe/read/erase/write may fail. ", size / 1024,
716 max_rom_decode.spi / 1024, "SPI");
717 }
718 if (!limitexceeded)
719 return 0;
720 /* Sometimes chip and programmer have more than one bus in common,
721 * and the limit is not exceeded on all buses. Tell the user.
722 */
723 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000724 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000725 printf_debug("There is at least one common chip/programmer "
726 "interface which can support a chip of this size. "
727 "You can try --force at your own risk.\n");
728 return 1;
729}
730
Peter Stuge483b8f02008-09-03 23:10:05 +0000731struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000732{
Peter Stuge483b8f02008-09-03 23:10:05 +0000733 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000734 unsigned long base = 0;
735 uint32_t size;
736 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000737 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000738
Peter Stuge483b8f02008-09-03 23:10:05 +0000739 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000740 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000741 continue;
Stefan Reinauerac378972008-03-17 22:59:40 +0000742 printf_debug("Probing for %s %s, %d KB: ",
743 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000744 if (!flash->probe && !force) {
Peter Stugef31104c2008-04-28 14:47:30 +0000745 printf_debug("failed! flashrom has no probe function for this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000746 continue;
747 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000748 buses_common = buses_supported & flash->bustype;
749 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000750 tmp = flashbuses_to_text(buses_supported);
751 printf_debug("skipped. Host bus type %s ", tmp);
752 free(tmp);
753 tmp = flashbuses_to_text(flash->bustype);
754 printf_debug("and chip bus type %s are incompatible.\n", tmp);
755 free(tmp);
756 continue;
757 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000758
Ollie Lhocbbf1252004-03-17 22:22:08 +0000759 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000760 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000761
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000762 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000763 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000764
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000765 if (force)
766 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000767
Peter Stuge483b8f02008-09-03 23:10:05 +0000768 if (flash->probe(flash) != 1)
769 goto notfound;
770
Uwe Hermann394131e2008-10-18 21:14:13 +0000771 if (first_flash == flashchips
772 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000773 break;
774
Peter Stuge483b8f02008-09-03 23:10:05 +0000775notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000776 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000777 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000778
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000779 if (!flash || !flash->name)
780 return NULL;
781
Uwe Hermann9899cad2009-06-28 21:47:57 +0000782 printf("Found chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
783 flash->vendor, flash->name, flash->total_size,
784 flashbuses_to_text(flash->bustype), base);
785
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000786 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000787}
788
Stefan Reinauere3705282005-12-18 16:41:10 +0000789int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000790{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000791 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000792 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000793
Uwe Hermanna502dce2007-10-17 23:55:15 +0000794 printf("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000795
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000796 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000797
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000798 if (!ret)
799 printf("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000800
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000801 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000802}
803
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +0000804int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000805{
806 unsigned long numbytes;
807 FILE *image;
808 unsigned long size = flash->total_size * 1024;
809 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000810
811 if (!filename) {
812 printf("Error: No filename specified.\n");
813 return 1;
814 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000815 if ((image = fopen(filename, "w")) == NULL) {
816 perror(filename);
817 exit(1);
818 }
819 printf("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000820 if (!flash->read) {
821 printf("FAILED!\n");
822 fprintf(stderr, "ERROR: flashrom has no read function for this flash chip.\n");
823 return 1;
824 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000825 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000826
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000827 numbytes = fwrite(buf, 1, size, image);
828 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +0000829 free(buf);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000830 printf("%s.\n", numbytes == size ? "done" : "FAILED");
831 if (numbytes != size)
832 return 1;
833 return 0;
834}
835
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000836/* This function shares a lot of its structure with erase_flash(). */
837int selfcheck_eraseblocks(struct flashchip *flash)
838{
839 int i, k;
840
841 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
842 unsigned int done = 0;
843 struct block_eraser eraser = flash->block_erasers[k];
844
845 for (i = 0; i < NUM_ERASEREGIONS; i++) {
846 /* Blocks with zero size are bugs in flashchips.c. */
847 if (eraser.eraseblocks[i].count &&
848 !eraser.eraseblocks[i].size) {
849 msg_gerr("ERROR: Flash chip %s erase function "
850 "%i region %i has size 0. Please report"
851 " a bug at flashrom@flashrom.org\n",
852 flash->name, k, i);
853 return 1;
854 break;
855 }
856 /* Blocks with zero count are bugs in flashchips.c. */
857 if (!eraser.eraseblocks[i].count &&
858 eraser.eraseblocks[i].size) {
859 msg_gerr("ERROR: Flash chip %s erase function "
860 "%i region %i has count 0. Please report"
861 " a bug at flashrom@flashrom.org\n",
862 flash->name, k, i);
863 return 1;
864 break;
865 }
866 done += eraser.eraseblocks[i].count *
867 eraser.eraseblocks[i].size;
868 }
869 /* This erase function is completely empty. */
870 if (!done)
871 continue;
872 if (done != flash->total_size * 1024) {
873 msg_gerr("ERROR: Flash chip %s erase function %i "
874 "region walking resulted in 0x%06x bytes total,"
875 " expected 0x%06x bytes. Please report a bug at"
876 " flashrom@flashrom.org\n", flash->name, k,
877 done, flash->total_size * 1024);
878 return 1;
879 }
880 }
881 return 0;
882}
883
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000884int erase_flash(struct flashchip *flash)
885{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000886 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000887 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000888
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000889 printf("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000890 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000891 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000892 struct block_eraser eraser = flash->block_erasers[k];
893
894 printf_debug("Looking at blockwise erase function %i... ", k);
895 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
896 printf_debug("not defined. "
897 "Looking for another erase function.\n");
898 continue;
899 }
900 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
901 printf_debug("eraseblock layout is known, but no "
902 "matching block erase function found. "
903 "Looking for another erase function.\n");
904 continue;
905 }
906 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
907 printf_debug("block erase function found, but "
908 "eraseblock layout is unknown. "
909 "Looking for another erase function.\n");
910 continue;
911 }
912 found = 1;
913 printf_debug("trying... ");
914 for (i = 0; i < NUM_ERASEREGIONS; i++) {
915 /* count==0 for all automatically initialized array
916 * members so the loop below won't be executed for them.
917 */
918 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000919 start = done + eraser.eraseblocks[i].size * j;
920 len = eraser.eraseblocks[i].size;
921 printf_debug("0x%06x-0x%06x, ", start,
922 start + len - 1);
923 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000924 if (ret)
925 break;
926 }
927 if (ret)
928 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000929 done += eraser.eraseblocks[i].count *
930 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000931 }
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000932 printf_debug("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000933 /* If everything is OK, don't try another erase function. */
934 if (!ret)
935 break;
936 }
937 /* If no block erase function was found or block erase failed, retry. */
938 if ((!found || ret) && (flash->erase)) {
939 found = 1;
940 printf_debug("Trying whole-chip erase function... ");
941 ret = flash->erase(flash);
942 }
943 if (!found) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000944 fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
945 return 1;
946 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +0000947
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000948 if (ret) {
949 fprintf(stderr, "FAILED!\n");
950 } else {
951 printf("SUCCESS.\n");
952 }
953 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000954}
955
Uwe Hermannc67d0372009-10-01 18:40:02 +0000956void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000957{
958 fprintf(stderr, "Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +0000959 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
960 "mail flashrom@flashrom.org!\n--------------------"
961 "-----------------------------------------------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000962 "DO NOT REBOOT OR POWEROFF!\n");
963}
964
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +0000965/* The way to go if you want a delimited list of programmers*/
966void list_programmers(char *delim)
967{
968 enum programmer p;
969 for (p = 0; p < PROGRAMMER_INVALID; p++) {
970 printf("%s", programmer_table[p].name);
971 if (p < PROGRAMMER_INVALID - 1)
972 printf("%s", delim);
973 }
974 printf("\n");
975}
976
Bernhard Walle201bde32008-01-21 15:24:22 +0000977void print_version(void)
978{
Carl-Daniel Hailfingera80cfbc2009-07-22 20:13:00 +0000979 printf("flashrom v%s\n", flashrom_version);
Bernhard Walle201bde32008-01-21 15:24:22 +0000980}
981
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +0000982int selfcheck(void)
983{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000984 int ret = 0;
985 struct flashchip *flash;
986
987 /* Safety check. Instead of aborting after the first error, check
988 * if more errors exist.
989 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +0000990 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
991 fprintf(stderr, "Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000992 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +0000993 }
994 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
995 fprintf(stderr, "SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000996 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +0000997 }
998#if BITBANG_SPI_SUPPORT == 1
999 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
1000 fprintf(stderr, "Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001001 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001002 }
1003#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001004 for (flash = flashchips; flash && flash->name; flash++)
1005 if (selfcheck_eraseblocks(flash))
1006 ret = 1;
1007 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001008}
1009
1010void check_chip_supported(struct flashchip *flash)
1011{
1012 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
1013 printf("===\n");
1014 if (flash->tested & TEST_BAD_MASK) {
1015 printf("This flash part has status NOT WORKING for operations:");
1016 if (flash->tested & TEST_BAD_PROBE)
1017 printf(" PROBE");
1018 if (flash->tested & TEST_BAD_READ)
1019 printf(" READ");
1020 if (flash->tested & TEST_BAD_ERASE)
1021 printf(" ERASE");
1022 if (flash->tested & TEST_BAD_WRITE)
1023 printf(" WRITE");
1024 printf("\n");
1025 }
1026 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1027 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1028 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1029 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
1030 printf("This flash part has status UNTESTED for operations:");
1031 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
1032 printf(" PROBE");
1033 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
1034 printf(" READ");
1035 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
1036 printf(" ERASE");
1037 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
1038 printf(" WRITE");
1039 printf("\n");
1040 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001041 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001042 printf("Please email a report to flashrom@flashrom.org if any "
1043 "of the above operations\nwork correctly for you with "
1044 "this flash part. Please include the flashrom\noutput "
1045 "with the additional -V option for all operations you "
1046 "tested (-V, -rV,\n-wV, -EV), and mention which "
1047 "mainboard or programmer you tested. Thanks for your "
1048 "help!\n===\n");
1049 }
1050}
1051
Ollie Lho761bf1b2004-03-20 16:46:10 +00001052int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001053{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001054 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001055}
1056
1057/* This function signature is horrible. We need to design a better interface,
1058 * but right now it allows us to split off the CLI code.
1059 */
1060int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1061{
1062 uint8_t *buf;
1063 unsigned long numbytes;
1064 FILE *image;
1065 int ret = 0;
1066 unsigned long size;
1067
1068 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001069 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001070
Ollie Lhoefa28582004-12-08 20:10:01 +00001071 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001072 if (flash->tested & TEST_BAD_ERASE) {
1073 fprintf(stderr, "Erase is not working on this chip. ");
1074 if (!force) {
1075 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001076 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001077 return 1;
1078 } else {
1079 fprintf(stderr, "Continuing anyway.\n");
1080 }
1081 }
1082 if (erase_flash(flash)) {
1083 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001084 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001085 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001086 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001087 } else if (read_it) {
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001088 if (read_flash(flash, filename)) {
1089 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001090 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001091 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001092 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001093 struct stat image_stat;
1094
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001095 if (flash->tested & TEST_BAD_ERASE) {
1096 fprintf(stderr, "Erase is not working on this chip "
1097 "and erase is needed for write. ");
1098 if (!force) {
1099 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001100 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001101 return 1;
1102 } else {
1103 fprintf(stderr, "Continuing anyway.\n");
1104 }
1105 }
1106 if (flash->tested & TEST_BAD_WRITE) {
1107 fprintf(stderr, "Write is not working on this chip. ");
1108 if (!force) {
1109 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001110 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001111 return 1;
1112 } else {
1113 fprintf(stderr, "Continuing anyway.\n");
1114 }
1115 }
Ollie Lho761bf1b2004-03-20 16:46:10 +00001116 if ((image = fopen(filename, "r")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001117 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001118 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001119 exit(1);
1120 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001121 if (fstat(fileno(image), &image_stat) != 0) {
1122 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001123 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001124 exit(1);
1125 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001126 if (image_stat.st_size != flash->total_size * 1024) {
Uwe Hermann793bdcd2008-05-22 22:47:04 +00001127 fprintf(stderr, "Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001128 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001129 exit(1);
1130 }
1131
Peter Stuge1fec0f32009-01-12 21:00:35 +00001132 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001133#if INTERNAL_SUPPORT == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001134 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001135#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001136 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001137 if (numbytes != size) {
1138 fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001139 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001140 return 1;
1141 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001142 }
1143
Ollie Lho184a4042005-11-26 21:55:36 +00001144 // This should be moved into each flash part's code to do it
1145 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001146 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001147
Ollie Lho184a4042005-11-26 21:55:36 +00001148 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001149
Peter Stugef31104c2008-04-28 14:47:30 +00001150 if (write_it) {
Paul Foxd51410c2009-06-12 08:04:08 +00001151 printf("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001152 if (!flash->write) {
1153 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001154 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001155 return 1;
1156 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001157 ret = flash->write(flash, buf);
1158 if (ret) {
1159 fprintf(stderr, "FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001160 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001161 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001162 return 1;
1163 } else {
1164 printf("COMPLETE.\n");
1165 }
Peter Stugef31104c2008-04-28 14:47:30 +00001166 }
Ollie Lho184a4042005-11-26 21:55:36 +00001167
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001168 if (verify_it) {
1169 /* Work around chips which need some time to calm down. */
1170 if (write_it)
1171 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001172 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001173 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001174 * might have an emergency situation.
1175 */
1176 if (ret && write_it)
1177 emergency_help_message();
1178 }
Ollie Lho184a4042005-11-26 21:55:36 +00001179
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001180 programmer_shutdown();
1181
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001182 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001183}