blob: db44c2f71646fabd4fa53369218157ea677da0b2 [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
836int erase_flash(struct flashchip *flash)
837{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000838 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000839 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000840
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000841 printf("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000842 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000843 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000844 struct block_eraser eraser = flash->block_erasers[k];
845
846 printf_debug("Looking at blockwise erase function %i... ", k);
847 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
848 printf_debug("not defined. "
849 "Looking for another erase function.\n");
850 continue;
851 }
852 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
853 printf_debug("eraseblock layout is known, but no "
854 "matching block erase function found. "
855 "Looking for another erase function.\n");
856 continue;
857 }
858 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
859 printf_debug("block erase function found, but "
860 "eraseblock layout is unknown. "
861 "Looking for another erase function.\n");
862 continue;
863 }
864 found = 1;
865 printf_debug("trying... ");
866 for (i = 0; i < NUM_ERASEREGIONS; i++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000867 /* Blocks with zero size are bugs in flashchips.c.
868 * FIXME: This check should be performed on startup.
869 */
870 if (eraser.eraseblocks[i].count &&
871 !eraser.eraseblocks[i].size) {
872 fprintf(stderr, "ERROR: Erase region with size "
873 "0 for this chip. Please report a bug "
874 "at flashrom@flashrom.org\n");
875 ret = 1;
876 break;
877 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000878 /* count==0 for all automatically initialized array
879 * members so the loop below won't be executed for them.
880 */
881 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000882 start = done + eraser.eraseblocks[i].size * j;
883 len = eraser.eraseblocks[i].size;
884 printf_debug("0x%06x-0x%06x, ", start,
885 start + len - 1);
886 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000887 if (ret)
888 break;
889 }
890 if (ret)
891 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000892 done += eraser.eraseblocks[i].count *
893 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000894 }
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +0000895 printf_debug("\n");
896 if (done != flash->total_size * 1024)
897 fprintf(stderr, "ERROR: Erase region walking erased "
898 "0x%06x bytes total, expected 0x%06x bytes.",
899 done, flash->total_size * 1024);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000900 /* If everything is OK, don't try another erase function. */
901 if (!ret)
902 break;
903 }
904 /* If no block erase function was found or block erase failed, retry. */
905 if ((!found || ret) && (flash->erase)) {
906 found = 1;
907 printf_debug("Trying whole-chip erase function... ");
908 ret = flash->erase(flash);
909 }
910 if (!found) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000911 fprintf(stderr, "ERROR: flashrom has no erase function for this flash chip.\n");
912 return 1;
913 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +0000914
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +0000915 if (ret) {
916 fprintf(stderr, "FAILED!\n");
917 } else {
918 printf("SUCCESS.\n");
919 }
920 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000921}
922
Uwe Hermannc67d0372009-10-01 18:40:02 +0000923void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000924{
925 fprintf(stderr, "Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +0000926 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
927 "mail flashrom@flashrom.org!\n--------------------"
928 "-----------------------------------------------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000929 "DO NOT REBOOT OR POWEROFF!\n");
930}
931
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +0000932/* The way to go if you want a delimited list of programmers*/
933void list_programmers(char *delim)
934{
935 enum programmer p;
936 for (p = 0; p < PROGRAMMER_INVALID; p++) {
937 printf("%s", programmer_table[p].name);
938 if (p < PROGRAMMER_INVALID - 1)
939 printf("%s", delim);
940 }
941 printf("\n");
942}
943
Bernhard Walle201bde32008-01-21 15:24:22 +0000944void print_version(void)
945{
Carl-Daniel Hailfingera80cfbc2009-07-22 20:13:00 +0000946 printf("flashrom v%s\n", flashrom_version);
Bernhard Walle201bde32008-01-21 15:24:22 +0000947}
948
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +0000949int selfcheck(void)
950{
951 /* Safety check. */
952 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
953 fprintf(stderr, "Programmer table miscompilation!\n");
954 return 1;
955 }
956 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
957 fprintf(stderr, "SPI programmer table miscompilation!\n");
958 return 1;
959 }
960#if BITBANG_SPI_SUPPORT == 1
961 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
962 fprintf(stderr, "Bitbanging SPI master table miscompilation!\n");
963 return 1;
964 }
965#endif
966 return 0;
967}
968
969void check_chip_supported(struct flashchip *flash)
970{
971 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
972 printf("===\n");
973 if (flash->tested & TEST_BAD_MASK) {
974 printf("This flash part has status NOT WORKING for operations:");
975 if (flash->tested & TEST_BAD_PROBE)
976 printf(" PROBE");
977 if (flash->tested & TEST_BAD_READ)
978 printf(" READ");
979 if (flash->tested & TEST_BAD_ERASE)
980 printf(" ERASE");
981 if (flash->tested & TEST_BAD_WRITE)
982 printf(" WRITE");
983 printf("\n");
984 }
985 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
986 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
987 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
988 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
989 printf("This flash part has status UNTESTED for operations:");
990 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
991 printf(" PROBE");
992 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
993 printf(" READ");
994 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
995 printf(" ERASE");
996 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
997 printf(" WRITE");
998 printf("\n");
999 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001000 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001001 printf("Please email a report to flashrom@flashrom.org if any "
1002 "of the above operations\nwork correctly for you with "
1003 "this flash part. Please include the flashrom\noutput "
1004 "with the additional -V option for all operations you "
1005 "tested (-V, -rV,\n-wV, -EV), and mention which "
1006 "mainboard or programmer you tested. Thanks for your "
1007 "help!\n===\n");
1008 }
1009}
1010
Ollie Lho761bf1b2004-03-20 16:46:10 +00001011int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001012{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001013 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001014}
1015
1016/* This function signature is horrible. We need to design a better interface,
1017 * but right now it allows us to split off the CLI code.
1018 */
1019int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1020{
1021 uint8_t *buf;
1022 unsigned long numbytes;
1023 FILE *image;
1024 int ret = 0;
1025 unsigned long size;
1026
1027 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001028 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001029
Ollie Lhoefa28582004-12-08 20:10:01 +00001030 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001031 if (flash->tested & TEST_BAD_ERASE) {
1032 fprintf(stderr, "Erase is not working on this chip. ");
1033 if (!force) {
1034 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001035 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001036 return 1;
1037 } else {
1038 fprintf(stderr, "Continuing anyway.\n");
1039 }
1040 }
1041 if (erase_flash(flash)) {
1042 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001043 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001044 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001045 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001046 } else if (read_it) {
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001047 if (read_flash(flash, filename)) {
1048 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001049 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001050 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001051 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001052 struct stat image_stat;
1053
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001054 if (flash->tested & TEST_BAD_ERASE) {
1055 fprintf(stderr, "Erase is not working on this chip "
1056 "and erase is needed for write. ");
1057 if (!force) {
1058 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001059 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001060 return 1;
1061 } else {
1062 fprintf(stderr, "Continuing anyway.\n");
1063 }
1064 }
1065 if (flash->tested & TEST_BAD_WRITE) {
1066 fprintf(stderr, "Write is not working on this chip. ");
1067 if (!force) {
1068 fprintf(stderr, "Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001069 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001070 return 1;
1071 } else {
1072 fprintf(stderr, "Continuing anyway.\n");
1073 }
1074 }
Ollie Lho761bf1b2004-03-20 16:46:10 +00001075 if ((image = fopen(filename, "r")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001076 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001077 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001078 exit(1);
1079 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001080 if (fstat(fileno(image), &image_stat) != 0) {
1081 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001082 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001083 exit(1);
1084 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001085 if (image_stat.st_size != flash->total_size * 1024) {
Uwe Hermann793bdcd2008-05-22 22:47:04 +00001086 fprintf(stderr, "Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001087 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001088 exit(1);
1089 }
1090
Peter Stuge1fec0f32009-01-12 21:00:35 +00001091 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001092#if INTERNAL_SUPPORT == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001093 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001094#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001095 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001096 if (numbytes != size) {
1097 fprintf(stderr, "Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001098 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001099 return 1;
1100 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001101 }
1102
Ollie Lho184a4042005-11-26 21:55:36 +00001103 // This should be moved into each flash part's code to do it
1104 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001105 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001106
Ollie Lho184a4042005-11-26 21:55:36 +00001107 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001108
Peter Stugef31104c2008-04-28 14:47:30 +00001109 if (write_it) {
Paul Foxd51410c2009-06-12 08:04:08 +00001110 printf("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001111 if (!flash->write) {
1112 fprintf(stderr, "Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001113 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001114 return 1;
1115 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001116 ret = flash->write(flash, buf);
1117 if (ret) {
1118 fprintf(stderr, "FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001119 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001120 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001121 return 1;
1122 } else {
1123 printf("COMPLETE.\n");
1124 }
Peter Stugef31104c2008-04-28 14:47:30 +00001125 }
Ollie Lho184a4042005-11-26 21:55:36 +00001126
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001127 if (verify_it) {
1128 /* Work around chips which need some time to calm down. */
1129 if (write_it)
1130 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001131 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001132 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001133 * might have an emergency situation.
1134 */
1135 if (ret && write_it)
1136 emergency_help_message();
1137 }
Ollie Lho184a4042005-11-26 21:55:36 +00001138
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001139 programmer_shutdown();
1140
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001141 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001142}