blob: 99b8da5e0b07c67c9f242401f11f1c7165ac1e40 [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>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000030#if HAVE_UTSNAME == 1
31#include <sys/utsname.h>
32#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000033#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000034#include "flashchips.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000035
Carl-Daniel Hailfingera80cfbc2009-07-22 20:13:00 +000036const char *flashrom_version = FLASHROM_VERSION;
Ronald G. Minnichceec4202003-07-25 04:37:41 +000037char *chip_to_probe = NULL;
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000038int verbose = 0;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000039
40#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +000041enum programmer programmer = PROGRAMMER_INTERNAL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000042#elif DUMMY_SUPPORT == 1
43enum programmer programmer = PROGRAMMER_DUMMY;
44#else
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000045/* If neither internal nor dummy are selected, we must pick a sensible default.
46 * Since there is no reason to prefer a particular external programmer, we fail
47 * if more than one of them is selected. If only one is selected, it is clear
48 * that the user wants that one to become the default.
49 */
Joerg Fischer5665ef32010-05-21 21:54:07 +000050#if NIC3COM_SUPPORT+GFXNVIDIA_SUPPORT+DRKAISER_SUPPORT+SATASII_SUPPORT+ATAHPT_SUPPORT+FT2232_SPI_SUPPORT+SERPROG_SUPPORT+BUSPIRATE_SPI_SUPPORT+DEDIPROG_SUPPORT+NICREALTEK_SUPPORT > 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000051#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all external programmers except one.
52#endif
53enum programmer programmer =
54#if NIC3COM_SUPPORT == 1
55 PROGRAMMER_NIC3COM
56#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +000057#if NICREALTEK_SUPPORT == 1
58 PROGRAMMER_NICREALTEK
59 PROGRAMMER_NICREALTEK2
60#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000061#if GFXNVIDIA_SUPPORT == 1
62 PROGRAMMER_GFXNVIDIA
63#endif
64#if DRKAISER_SUPPORT == 1
65 PROGRAMMER_DRKAISER
66#endif
67#if SATASII_SUPPORT == 1
68 PROGRAMMER_SATASII
69#endif
Uwe Hermannddd5c9e2010-02-21 21:17:00 +000070#if ATAHPT_SUPPORT == 1
71 PROGRAMMER_ATAHPT
72#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000073#if FT2232_SPI_SUPPORT == 1
74 PROGRAMMER_FT2232SPI
75#endif
76#if SERPROG_SUPPORT == 1
77 PROGRAMMER_SERPROG
78#endif
79#if BUSPIRATE_SPI_SUPPORT == 1
80 PROGRAMMER_BUSPIRATESPI
81#endif
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000082#if DEDIPROG_SUPPORT == 1
83 PROGRAMMER_DEDIPROG
84#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000085;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000086#endif
87
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000088char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000089
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000090/**
91 * flashrom defaults to Parallel/LPC/FWH flash devices. If a known host
92 * controller is found, the init routine sets the buses_supported bitfield to
93 * contain the supported buses for that controller.
94 */
95enum chipbustype buses_supported = CHIP_BUSTYPE_NONSPI;
96
97/**
98 * Programmers supporting multiple buses can have differing size limits on
99 * each bus. Store the limits for each bus in a common struct.
100 */
101struct decode_sizes max_rom_decode = {
102 .parallel = 0xffffffff,
103 .lpc = 0xffffffff,
104 .fwh = 0xffffffff,
105 .spi = 0xffffffff
106};
107
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000108const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000109#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000110 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000111 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000112 .init = internal_init,
113 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000114 .map_flash_region = physmap,
115 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000116 .chip_readb = internal_chip_readb,
117 .chip_readw = internal_chip_readw,
118 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000119 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000120 .chip_writeb = internal_chip_writeb,
121 .chip_writew = internal_chip_writew,
122 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000123 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000124 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000125 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000126#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000127
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000128#if DUMMY_SUPPORT == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000129 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000130 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000131 .init = dummy_init,
132 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000133 .map_flash_region = dummy_map,
134 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000135 .chip_readb = dummy_chip_readb,
136 .chip_readw = dummy_chip_readw,
137 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000138 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000139 .chip_writeb = dummy_chip_writeb,
140 .chip_writew = dummy_chip_writew,
141 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000142 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000143 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000144 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000145#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000146
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000147#if NIC3COM_SUPPORT == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000148 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000149 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000150 .init = nic3com_init,
151 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000152 .map_flash_region = fallback_map,
153 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000154 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000155 .chip_readw = fallback_chip_readw,
156 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000157 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000158 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000159 .chip_writew = fallback_chip_writew,
160 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000161 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000162 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000163 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000164#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000165
Joerg Fischer5665ef32010-05-21 21:54:07 +0000166#if NICREALTEK_SUPPORT == 1
167 {
168 .name = "nicrealtek",
169 .init = nicrealtek_init,
170 .shutdown = nicrealtek_shutdown,
171 .map_flash_region = fallback_map,
172 .unmap_flash_region = fallback_unmap,
173 .chip_readb = nicrealtek_chip_readb,
174 .chip_readw = fallback_chip_readw,
175 .chip_readl = fallback_chip_readl,
176 .chip_readn = fallback_chip_readn,
177 .chip_writeb = nicrealtek_chip_writeb,
178 .chip_writew = fallback_chip_writew,
179 .chip_writel = fallback_chip_writel,
180 .chip_writen = fallback_chip_writen,
181 .delay = internal_delay,
182 },
183 {
184 .name = "nicsmc1211",
185 .init = nicsmc1211_init,
186 .shutdown = nicrealtek_shutdown,
187 .map_flash_region = fallback_map,
188 .unmap_flash_region = fallback_unmap,
189 .chip_readb = nicrealtek_chip_readb,
190 .chip_readw = fallback_chip_readw,
191 .chip_readl = fallback_chip_readl,
192 .chip_readn = fallback_chip_readn,
193 .chip_writeb = nicrealtek_chip_writeb,
194 .chip_writew = fallback_chip_writew,
195 .chip_writel = fallback_chip_writel,
196 .chip_writen = fallback_chip_writen,
197 .delay = internal_delay,
198 },
199#endif
200
201
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000202#if GFXNVIDIA_SUPPORT == 1
203 {
204 .name = "gfxnvidia",
205 .init = gfxnvidia_init,
206 .shutdown = gfxnvidia_shutdown,
207 .map_flash_region = fallback_map,
208 .unmap_flash_region = fallback_unmap,
209 .chip_readb = gfxnvidia_chip_readb,
210 .chip_readw = fallback_chip_readw,
211 .chip_readl = fallback_chip_readl,
212 .chip_readn = fallback_chip_readn,
213 .chip_writeb = gfxnvidia_chip_writeb,
214 .chip_writew = fallback_chip_writew,
215 .chip_writel = fallback_chip_writel,
216 .chip_writen = fallback_chip_writen,
217 .delay = internal_delay,
218 },
219#endif
220
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000221#if DRKAISER_SUPPORT == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000222 {
TURBO Jb0912c02009-09-02 23:00:46 +0000223 .name = "drkaiser",
224 .init = drkaiser_init,
225 .shutdown = drkaiser_shutdown,
226 .map_flash_region = fallback_map,
227 .unmap_flash_region = fallback_unmap,
228 .chip_readb = drkaiser_chip_readb,
229 .chip_readw = fallback_chip_readw,
230 .chip_readl = fallback_chip_readl,
231 .chip_readn = fallback_chip_readn,
232 .chip_writeb = drkaiser_chip_writeb,
233 .chip_writew = fallback_chip_writew,
234 .chip_writel = fallback_chip_writel,
235 .chip_writen = fallback_chip_writen,
236 .delay = internal_delay,
237 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000238#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000239
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000240#if SATASII_SUPPORT == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000241 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000242 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000243 .init = satasii_init,
244 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000245 .map_flash_region = fallback_map,
246 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000247 .chip_readb = satasii_chip_readb,
248 .chip_readw = fallback_chip_readw,
249 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000250 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000251 .chip_writeb = satasii_chip_writeb,
252 .chip_writew = fallback_chip_writew,
253 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000254 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000255 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000256 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000257#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000258
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000259#if ATAHPT_SUPPORT == 1
260 {
261 .name = "atahpt",
262 .init = atahpt_init,
263 .shutdown = atahpt_shutdown,
264 .map_flash_region = fallback_map,
265 .unmap_flash_region = fallback_unmap,
266 .chip_readb = atahpt_chip_readb,
267 .chip_readw = fallback_chip_readw,
268 .chip_readl = fallback_chip_readl,
269 .chip_readn = fallback_chip_readn,
270 .chip_writeb = atahpt_chip_writeb,
271 .chip_writew = fallback_chip_writew,
272 .chip_writel = fallback_chip_writel,
273 .chip_writen = fallback_chip_writen,
274 .delay = internal_delay,
275 },
276#endif
277
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000278#if INTERNAL_SUPPORT == 1
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000279 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000280 .name = "it87spi",
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000281 .init = it87spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000282 .shutdown = noop_shutdown,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000283 .map_flash_region = fallback_map,
284 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000285 .chip_readb = noop_chip_readb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000286 .chip_readw = fallback_chip_readw,
287 .chip_readl = fallback_chip_readl,
288 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000289 .chip_writeb = noop_chip_writeb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000290 .chip_writew = fallback_chip_writew,
291 .chip_writel = fallback_chip_writel,
292 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000293 .delay = internal_delay,
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000294 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000295#endif
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000296
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000297#if FT2232_SPI_SUPPORT == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000298 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000299 .name = "ft2232spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000300 .init = ft2232_spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000301 .shutdown = noop_shutdown, /* Missing shutdown */
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000302 .map_flash_region = fallback_map,
303 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000304 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000305 .chip_readw = fallback_chip_readw,
306 .chip_readl = fallback_chip_readl,
307 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000308 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000309 .chip_writew = fallback_chip_writew,
310 .chip_writel = fallback_chip_writel,
311 .chip_writen = fallback_chip_writen,
312 .delay = internal_delay,
313 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000314#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000315
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000316#if SERPROG_SUPPORT == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000317 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000318 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000319 .init = serprog_init,
320 .shutdown = serprog_shutdown,
321 .map_flash_region = fallback_map,
322 .unmap_flash_region = fallback_unmap,
323 .chip_readb = serprog_chip_readb,
324 .chip_readw = fallback_chip_readw,
325 .chip_readl = fallback_chip_readl,
326 .chip_readn = serprog_chip_readn,
327 .chip_writeb = serprog_chip_writeb,
328 .chip_writew = fallback_chip_writew,
329 .chip_writel = fallback_chip_writel,
330 .chip_writen = fallback_chip_writen,
331 .delay = serprog_delay,
332 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000333#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000334
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000335#if BUSPIRATE_SPI_SUPPORT == 1
336 {
337 .name = "buspiratespi",
338 .init = buspirate_spi_init,
339 .shutdown = buspirate_spi_shutdown,
340 .map_flash_region = fallback_map,
341 .unmap_flash_region = fallback_unmap,
342 .chip_readb = noop_chip_readb,
343 .chip_readw = fallback_chip_readw,
344 .chip_readl = fallback_chip_readl,
345 .chip_readn = fallback_chip_readn,
346 .chip_writeb = noop_chip_writeb,
347 .chip_writew = fallback_chip_writew,
348 .chip_writel = fallback_chip_writel,
349 .chip_writen = fallback_chip_writen,
350 .delay = internal_delay,
351 },
352#endif
353
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000354#if DEDIPROG_SUPPORT == 1
355 {
356 .name = "dediprog",
357 .init = dediprog_init,
358 .shutdown = dediprog_shutdown,
359 .map_flash_region = fallback_map,
360 .unmap_flash_region = fallback_unmap,
361 .chip_readb = noop_chip_readb,
362 .chip_readw = fallback_chip_readw,
363 .chip_readl = fallback_chip_readl,
364 .chip_readn = fallback_chip_readn,
365 .chip_writeb = noop_chip_writeb,
366 .chip_writew = fallback_chip_writew,
367 .chip_writel = fallback_chip_writel,
368 .chip_writen = fallback_chip_writen,
369 .delay = internal_delay,
370 },
371#endif
372
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000373 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000374};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000375
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000376#define SHUTDOWN_MAXFN 4
377static int shutdown_fn_count = 0;
378struct shutdown_func_data {
379 void (*func) (void *data);
380 void *data;
381} shutdown_fn[SHUTDOWN_MAXFN];
382
383/* Register a function to be executed on programmer shutdown.
384 * The advantage over atexit() is that you can supply a void pointer which will
385 * be used as parameter to the registered function upon programmer shutdown.
386 * This pointer can point to arbitrary data used by said function, e.g. undo
387 * information for GPIO settings etc. If unneeded, set data=NULL.
388 * Please note that the first (void *data) belongs to the function signature of
389 * the function passed as first parameter.
390 */
391int register_shutdown(void (*function) (void *data), void *data)
392{
393 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
394 msg_perr("Tried to register more than %n shutdown functions.\n",
395 SHUTDOWN_MAXFN);
396 return 1;
397 }
398 shutdown_fn[shutdown_fn_count].func = function;
399 shutdown_fn[shutdown_fn_count].data = data;
400 shutdown_fn_count++;
401
402 return 0;
403}
404
Uwe Hermann09e04f72009-05-16 22:36:00 +0000405int programmer_init(void)
406{
407 return programmer_table[programmer].init();
408}
409
410int programmer_shutdown(void)
411{
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000412 int i;
413
414 for (i = shutdown_fn_count - 1; i >= 0; i--)
415 shutdown_fn[i].func(shutdown_fn[i].data);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000416 return programmer_table[programmer].shutdown();
417}
418
419void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
420 size_t len)
421{
422 return programmer_table[programmer].map_flash_region(descr,
423 phys_addr, len);
424}
425
426void programmer_unmap_flash_region(void *virt_addr, size_t len)
427{
428 programmer_table[programmer].unmap_flash_region(virt_addr, len);
429}
430
431void chip_writeb(uint8_t val, chipaddr addr)
432{
433 programmer_table[programmer].chip_writeb(val, addr);
434}
435
436void chip_writew(uint16_t val, chipaddr addr)
437{
438 programmer_table[programmer].chip_writew(val, addr);
439}
440
441void chip_writel(uint32_t val, chipaddr addr)
442{
443 programmer_table[programmer].chip_writel(val, addr);
444}
445
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000446void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
447{
448 programmer_table[programmer].chip_writen(buf, addr, len);
449}
450
Uwe Hermann09e04f72009-05-16 22:36:00 +0000451uint8_t chip_readb(const chipaddr addr)
452{
453 return programmer_table[programmer].chip_readb(addr);
454}
455
456uint16_t chip_readw(const chipaddr addr)
457{
458 return programmer_table[programmer].chip_readw(addr);
459}
460
461uint32_t chip_readl(const chipaddr addr)
462{
463 return programmer_table[programmer].chip_readl(addr);
464}
465
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000466void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
467{
468 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000469}
470
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000471void programmer_delay(int usecs)
472{
473 programmer_table[programmer].delay(usecs);
474}
475
Peter Stuge776d2022009-01-26 00:39:57 +0000476void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000477{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000478 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000479 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000480 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000481 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000482}
483
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000484int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000485{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000486 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000487
488 return 0;
489}
490
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000491unsigned long flashbase = 0;
492
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000493int min(int a, int b)
494{
495 return (a < b) ? a : b;
496}
497
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000498int max(int a, int b)
499{
500 return (a > b) ? a : b;
501}
502
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000503int bitcount(unsigned long a)
504{
505 int i = 0;
506 for (; a != 0; a >>= 1)
507 if (a & 1)
508 i++;
509 return i;
510}
511
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000512char *strcat_realloc(char *dest, const char *src)
513{
514 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
515 if (!dest)
516 return NULL;
517 strcat(dest, src);
518 return dest;
519}
520
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000521/* This is a somewhat hacked function similar in some ways to strtok().
522 * It will look for needle in haystack, return a copy of needle and remove
523 * everything from the first occurrence of needle to the next delimiter
524 * from haystack.
525 */
526char *extract_param(char **haystack, char *needle, char *delim)
527{
528 char *param_pos, *rest, *tmp;
529 char *dev = NULL;
530 int devlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000531 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000532
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000533 needlelen = strlen(needle);
534 if (!needlelen) {
535 msg_gerr("%s: empty needle! Please report a bug at "
536 "flashrom@flashrom.org\n", __func__);
537 return NULL;
538 }
539 /* No programmer parameters given. */
540 if (*haystack == NULL)
541 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000542 param_pos = strstr(*haystack, needle);
543 do {
544 if (!param_pos)
545 return NULL;
546 /* Beginning of the string? */
547 if (param_pos == *haystack)
548 break;
549 /* After a delimiter? */
550 if (strchr(delim, *(param_pos - 1)))
551 break;
552 /* Continue searching. */
553 param_pos++;
554 param_pos = strstr(param_pos, needle);
555 } while (1);
556
557 if (param_pos) {
558 param_pos += strlen(needle);
559 devlen = strcspn(param_pos, delim);
560 if (devlen) {
561 dev = malloc(devlen + 1);
562 if (!dev) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000563 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000564 exit(1);
565 }
566 strncpy(dev, param_pos, devlen);
567 dev[devlen] = '\0';
568 }
569 rest = param_pos + devlen;
570 rest += strspn(rest, delim);
571 param_pos -= strlen(needle);
572 memmove(param_pos, rest, strlen(rest) + 1);
573 tmp = realloc(*haystack, strlen(*haystack) + 1);
574 if (!tmp) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000575 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000576 exit(1);
577 }
578 *haystack = tmp;
579 }
580
581
582 return dev;
583}
584
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000585/* start is an offset to the base address of the flash chip */
586int check_erased_range(struct flashchip *flash, int start, int len)
587{
588 int ret;
589 uint8_t *cmpbuf = malloc(len);
590
591 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000592 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000593 exit(1);
594 }
595 memset(cmpbuf, 0xff, len);
596 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
597 free(cmpbuf);
598 return ret;
599}
600
601/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000602 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
603 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000604 * @start offset to the base address of the flash chip
605 * @len length of the verified area
606 * @message string to print in the "FAILED" message
607 * @return 0 for success, -1 for failure
608 */
609int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
610{
611 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000612 int page_size = flash->page_size;
613 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000614 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000615
616 if (!len)
617 goto out_free;
618
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000619 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000620 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000621 return 1;
622 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000623 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000624 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000625 exit(1);
626 }
627
628 if (start + len > flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000629 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000630 " total_size 0x%x\n", __func__, start, len,
631 flash->total_size * 1024);
632 ret = -1;
633 goto out_free;
634 }
635 if (!message)
636 message = "VERIFY";
637
638 /* Warning: This loop has a very unusual condition and body.
639 * The loop needs to go through each page with at least one affected
640 * byte. The lowest page number is (start / page_size) since that
641 * division rounds down. The highest page number we want is the page
642 * where the last byte of the range lives. That last byte has the
643 * address (start + len - 1), thus the highest page number is
644 * (start + len - 1) / page_size. Since we want to include that last
645 * page as well, the loop condition uses <=.
646 */
647 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
648 /* Byte position of the first byte in the range in this page. */
649 starthere = max(start, i * page_size);
650 /* Length of bytes in the range in this page. */
651 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000652 flash->read(flash, readbuf, starthere, lenhere);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000653 for (j = 0; j < lenhere; j++) {
654 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000655 /* Only print the first failure. */
656 if (!failcount++)
Sean Nelson316a29f2010-05-07 20:09:04 +0000657 msg_cerr("%s FAILED at 0x%08x! "
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000658 "Expected=0x%02x, Read=0x%02x,",
659 message, starthere + j,
660 cmpbuf[starthere - start + j],
661 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000662 }
663 }
664 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000665 if (failcount) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000666 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000667 start, start + len - 1, failcount);
668 ret = -1;
669 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000670
671out_free:
672 free(readbuf);
673 return ret;
674}
675
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000676/**
677 * Check if the buffer @have can be programmed to the content of @want without
678 * erasing. This is only possible if all chunks of size @gran are either kept
679 * as-is or changed from an all-ones state to any other state.
680 * The following write granularities (enum @gran) are known:
681 * - 1 bit. Each bit can be cleared individually.
682 * - 1 byte. A byte can be written once. Further writes to an already written
683 * byte cause the contents to be either undefined or to stay unchanged.
684 * - 128 bytes. If less than 128 bytes are written, the rest will be
685 * erased. Each write to a 128-byte region will trigger an automatic erase
686 * before anything is written. Very uncommon behaviour and unsupported by
687 * this function.
688 * - 256 bytes. If less than 256 bytes are written, the contents of the
689 * unwritten bytes are undefined.
690 *
691 * @have buffer with current content
692 * @want buffer with desired content
693 * @len length of the verified area
694 * @gran write granularity (enum, not count)
695 * @return 0 if no erase is needed, 1 otherwise
696 */
697int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
698{
699 int result = 0;
700 int i, j, limit;
701
702 switch (gran) {
703 case write_gran_1bit:
704 for (i = 0; i < len; i++)
705 if ((have[i] & want[i]) != want[i]) {
706 result = 1;
707 break;
708 }
709 break;
710 case write_gran_1byte:
711 for (i = 0; i < len; i++)
712 if ((have[i] != want[i]) && (have[i] != 0xff)) {
713 result = 1;
714 break;
715 }
716 break;
717 case write_gran_256bytes:
718 for (j = 0; j < len / 256; j++) {
719 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000720 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000721 if (!memcmp(have + j * 256, want + j * 256, limit))
722 continue;
723 /* have needs to be in erased state. */
724 for (i = 0; i < limit; i++)
725 if (have[i] != 0xff) {
726 result = 1;
727 break;
728 }
729 if (result)
730 break;
731 }
732 break;
733 }
734 return result;
735}
736
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000737/* This function generates various test patterns useful for testing controller
738 * and chip communication as well as chip behaviour.
739 *
740 * If a byte can be written multiple times, each time keeping 0-bits at 0
741 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
742 * is essentially an AND operation. That's also the reason why this function
743 * provides the result of AND between various patterns.
744 *
745 * Below is a list of patterns (and their block length).
746 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
747 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
748 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
749 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
750 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
751 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
752 * Pattern 6 is 00 (1 Byte)
753 * Pattern 7 is ff (1 Byte)
754 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
755 * byte block.
756 *
757 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
758 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
759 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
760 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
761 * Pattern 12 is 00 (1 Byte)
762 * Pattern 13 is ff (1 Byte)
763 * Patterns 8-13 have no block number.
764 *
765 * Patterns 0-3 are created to detect and efficiently diagnose communication
766 * slips like missed bits or bytes and their repetitive nature gives good visual
767 * cues to the person inspecting the results. In addition, the following holds:
768 * AND Pattern 0/1 == Pattern 4
769 * AND Pattern 2/3 == Pattern 5
770 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
771 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
772 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
773 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
774 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
775 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
776 * Besides that, they provide for bit testing of the last two bytes of every
777 * 256 byte block which contains the block number for patterns 0-6.
778 * Patterns 10-11 are special purpose for detecting subblock aliasing with
779 * block sizes >256 bytes (some Dataflash chips etc.)
780 * AND Pattern 8/9 == Pattern 12
781 * AND Pattern 10/11 == Pattern 12
782 * Pattern 13 is the completely erased state.
783 * None of the patterns can detect aliasing at boundaries which are a multiple
784 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
785 */
786int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
787{
788 int i;
789
790 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000791 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000792 return 1;
793 }
794
795 switch (variant) {
796 case 0:
797 for (i = 0; i < size; i++)
798 buf[i] = (i & 0xf) << 4 | 0x5;
799 break;
800 case 1:
801 for (i = 0; i < size; i++)
802 buf[i] = (i & 0xf) << 4 | 0xa;
803 break;
804 case 2:
805 for (i = 0; i < size; i++)
806 buf[i] = 0x50 | (i & 0xf);
807 break;
808 case 3:
809 for (i = 0; i < size; i++)
810 buf[i] = 0xa0 | (i & 0xf);
811 break;
812 case 4:
813 for (i = 0; i < size; i++)
814 buf[i] = (i & 0xf) << 4;
815 break;
816 case 5:
817 for (i = 0; i < size; i++)
818 buf[i] = i & 0xf;
819 break;
820 case 6:
821 memset(buf, 0x00, size);
822 break;
823 case 7:
824 memset(buf, 0xff, size);
825 break;
826 case 8:
827 for (i = 0; i < size; i++)
828 buf[i] = i & 0xff;
829 break;
830 case 9:
831 for (i = 0; i < size; i++)
832 buf[i] = ~(i & 0xff);
833 break;
834 case 10:
835 for (i = 0; i < size % 2; i++) {
836 buf[i * 2] = (i >> 8) & 0xff;
837 buf[i * 2 + 1] = i & 0xff;
838 }
839 if (size & 0x1)
840 buf[i * 2] = (i >> 8) & 0xff;
841 break;
842 case 11:
843 for (i = 0; i < size % 2; i++) {
844 buf[i * 2] = ~((i >> 8) & 0xff);
845 buf[i * 2 + 1] = ~(i & 0xff);
846 }
847 if (size & 0x1)
848 buf[i * 2] = ~((i >> 8) & 0xff);
849 break;
850 case 12:
851 memset(buf, 0x00, size);
852 break;
853 case 13:
854 memset(buf, 0xff, size);
855 break;
856 }
857
858 if ((variant >= 0) && (variant <= 7)) {
859 /* Write block number in the last two bytes of each 256-byte
860 * block, big endian for easier reading of the hexdump.
861 * Note that this wraps around for chips larger than 2^24 bytes
862 * (16 MB).
863 */
864 for (i = 0; i < size / 256; i++) {
865 buf[i * 256 + 254] = (i >> 8) & 0xff;
866 buf[i * 256 + 255] = i & 0xff;
867 }
868 }
869
870 return 0;
871}
872
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000873int check_max_decode(enum chipbustype buses, uint32_t size)
874{
875 int limitexceeded = 0;
876 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
877 (max_rom_decode.parallel < size)) {
878 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000879 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000880 "size %u kB of chipset/board/programmer "
881 "for %s interface, "
882 "probe/read/erase/write may fail. ", size / 1024,
883 max_rom_decode.parallel / 1024, "Parallel");
884 }
885 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
886 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000887 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000888 "size %u kB of chipset/board/programmer "
889 "for %s interface, "
890 "probe/read/erase/write may fail. ", size / 1024,
891 max_rom_decode.lpc / 1024, "LPC");
892 }
893 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
894 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000895 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000896 "size %u kB of chipset/board/programmer "
897 "for %s interface, "
898 "probe/read/erase/write may fail. ", size / 1024,
899 max_rom_decode.fwh / 1024, "FWH");
900 }
901 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
902 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000903 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000904 "size %u kB of chipset/board/programmer "
905 "for %s interface, "
906 "probe/read/erase/write may fail. ", size / 1024,
907 max_rom_decode.spi / 1024, "SPI");
908 }
909 if (!limitexceeded)
910 return 0;
911 /* Sometimes chip and programmer have more than one bus in common,
912 * and the limit is not exceeded on all buses. Tell the user.
913 */
914 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000915 /* FIXME: This message is designed towards CLI users. */
Sean Nelson316a29f2010-05-07 20:09:04 +0000916 msg_pdbg("There is at least one common chip/programmer "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000917 "interface which can support a chip of this size. "
918 "You can try --force at your own risk.\n");
919 return 1;
920}
921
Peter Stuge483b8f02008-09-03 23:10:05 +0000922struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000923{
Peter Stuge483b8f02008-09-03 23:10:05 +0000924 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000925 unsigned long base = 0;
926 uint32_t size;
927 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000928 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000929
Peter Stuge483b8f02008-09-03 23:10:05 +0000930 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000931 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000932 continue;
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000933 msg_gdbg("Probing for %s %s, %d KB: ",
Stefan Reinauerac378972008-03-17 22:59:40 +0000934 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000935 if (!flash->probe && !force) {
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000936 msg_gdbg("failed! flashrom has no probe function for "
937 "this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000938 continue;
939 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000940 buses_common = buses_supported & flash->bustype;
941 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000942 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000943 msg_gdbg("skipped.");
944 msg_gspew(" Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000945 free(tmp);
946 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000947 msg_gspew("and chip bus type %s are incompatible.",
948 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000949 free(tmp);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000950 msg_gdbg("\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000951 continue;
952 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000953
Ollie Lhocbbf1252004-03-17 22:22:08 +0000954 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000955 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000956
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000957 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000958 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000959
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000960 if (force)
961 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000962
Peter Stuge483b8f02008-09-03 23:10:05 +0000963 if (flash->probe(flash) != 1)
964 goto notfound;
965
Uwe Hermann394131e2008-10-18 21:14:13 +0000966 if (first_flash == flashchips
967 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000968 break;
969
Peter Stuge483b8f02008-09-03 23:10:05 +0000970notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000971 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000972 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000973
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000974 if (!flash || !flash->name)
975 return NULL;
976
Sean Nelson316a29f2010-05-07 20:09:04 +0000977 msg_cinfo("%s chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000978 force ? "Assuming" : "Found",
Uwe Hermann9899cad2009-06-28 21:47:57 +0000979 flash->vendor, flash->name, flash->total_size,
980 flashbuses_to_text(flash->bustype), base);
981
Sean Nelson6e0b9122010-02-19 00:52:10 +0000982 if (flash->printlock)
983 flash->printlock(flash);
984
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000985 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000986}
987
Stefan Reinauere3705282005-12-18 16:41:10 +0000988int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000989{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000990 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000991 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000992
Sean Nelson316a29f2010-05-07 20:09:04 +0000993 msg_cinfo("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000994
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000995 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000996
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000997 if (!ret)
Sean Nelson316a29f2010-05-07 20:09:04 +0000998 msg_cinfo("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000999
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001000 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001001}
1002
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +00001003int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001004{
1005 unsigned long numbytes;
1006 FILE *image;
1007 unsigned long size = flash->total_size * 1024;
1008 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001009
1010 if (!filename) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001011 msg_gerr("Error: No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001012 return 1;
1013 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001014 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001015 perror(filename);
1016 exit(1);
1017 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001018 msg_cinfo("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001019 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001020 msg_cinfo("FAILED!\n");
1021 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001022 return 1;
1023 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +00001024 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001025
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001026 numbytes = fwrite(buf, 1, size, image);
1027 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +00001028 free(buf);
Sean Nelson316a29f2010-05-07 20:09:04 +00001029 msg_cinfo("%s.\n", numbytes == size ? "done" : "FAILED");
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001030 if (numbytes != size)
1031 return 1;
1032 return 0;
1033}
1034
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001035/* This function shares a lot of its structure with erase_flash().
1036 * Even if an error is found, the function will keep going and check the rest.
1037 */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001038int selfcheck_eraseblocks(struct flashchip *flash)
1039{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001040 int i, j, k;
1041 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001042
1043 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1044 unsigned int done = 0;
1045 struct block_eraser eraser = flash->block_erasers[k];
1046
1047 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1048 /* Blocks with zero size are bugs in flashchips.c. */
1049 if (eraser.eraseblocks[i].count &&
1050 !eraser.eraseblocks[i].size) {
1051 msg_gerr("ERROR: Flash chip %s erase function "
1052 "%i region %i has size 0. Please report"
1053 " a bug at flashrom@flashrom.org\n",
1054 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001055 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001056 }
1057 /* Blocks with zero count are bugs in flashchips.c. */
1058 if (!eraser.eraseblocks[i].count &&
1059 eraser.eraseblocks[i].size) {
1060 msg_gerr("ERROR: Flash chip %s erase function "
1061 "%i region %i has count 0. Please report"
1062 " a bug at flashrom@flashrom.org\n",
1063 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001064 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001065 }
1066 done += eraser.eraseblocks[i].count *
1067 eraser.eraseblocks[i].size;
1068 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001069 /* Empty eraseblock definition with erase function. */
1070 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001071 msg_gspew("Strange: Empty eraseblock definition with "
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001072 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001073 if (!done)
1074 continue;
1075 if (done != flash->total_size * 1024) {
1076 msg_gerr("ERROR: Flash chip %s erase function %i "
1077 "region walking resulted in 0x%06x bytes total,"
1078 " expected 0x%06x bytes. Please report a bug at"
1079 " flashrom@flashrom.org\n", flash->name, k,
1080 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001081 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001082 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001083 if (!eraser.block_erase)
1084 continue;
1085 /* Check if there are identical erase functions for different
1086 * layouts. That would imply "magic" erase functions. The
1087 * easiest way to check this is with function pointers.
1088 */
Uwe Hermann43959702010-03-13 17:28:29 +00001089 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001090 if (eraser.block_erase ==
1091 flash->block_erasers[j].block_erase) {
1092 msg_gerr("ERROR: Flash chip %s erase function "
1093 "%i and %i are identical. Please report"
1094 " a bug at flashrom@flashrom.org\n",
1095 flash->name, k, j);
1096 ret = 1;
1097 }
Uwe Hermann43959702010-03-13 17:28:29 +00001098 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001099 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001100 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001101}
1102
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001103int erase_flash(struct flashchip *flash)
1104{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001105 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001106 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001107
Sean Nelson316a29f2010-05-07 20:09:04 +00001108 msg_cinfo("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001109 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001110 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001111 struct block_eraser eraser = flash->block_erasers[k];
1112
Sean Nelson316a29f2010-05-07 20:09:04 +00001113 msg_cdbg("Looking at blockwise erase function %i... ", k);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001114 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001115 msg_cdbg("not defined. "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001116 "Looking for another erase function.\n");
1117 continue;
1118 }
1119 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001120 msg_cdbg("eraseblock layout is known, but no "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001121 "matching block erase function found. "
1122 "Looking for another erase function.\n");
1123 continue;
1124 }
1125 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001126 msg_cdbg("block erase function found, but "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001127 "eraseblock layout is unknown. "
1128 "Looking for another erase function.\n");
1129 continue;
1130 }
1131 found = 1;
Sean Nelson316a29f2010-05-07 20:09:04 +00001132 msg_cdbg("trying... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001133 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1134 /* count==0 for all automatically initialized array
1135 * members so the loop below won't be executed for them.
1136 */
1137 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001138 start = done + eraser.eraseblocks[i].size * j;
1139 len = eraser.eraseblocks[i].size;
Sean Nelson316a29f2010-05-07 20:09:04 +00001140 msg_cdbg("0x%06x-0x%06x, ", start,
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001141 start + len - 1);
1142 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001143 if (ret)
1144 break;
1145 }
1146 if (ret)
1147 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001148 done += eraser.eraseblocks[i].count *
1149 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001150 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001151 msg_cdbg("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001152 /* If everything is OK, don't try another erase function. */
1153 if (!ret)
1154 break;
1155 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001156 if (!found) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001157 msg_cerr("ERROR: flashrom has no erase function for this flash chip.\n");
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001158 return 1;
1159 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001160
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001161 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001162 msg_cerr("FAILED!\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001163 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001164 msg_cinfo("SUCCESS.\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001165 }
1166 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001167}
1168
Uwe Hermannc67d0372009-10-01 18:40:02 +00001169void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001170{
Sean Nelson316a29f2010-05-07 20:09:04 +00001171 msg_gerr("Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001172 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001173 "mail flashrom@flashrom.org!\n"
1174 "-------------------------------------------------------------"
1175 "------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001176 "DO NOT REBOOT OR POWEROFF!\n");
1177}
1178
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001179/* The way to go if you want a delimited list of programmers*/
1180void list_programmers(char *delim)
1181{
1182 enum programmer p;
1183 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001184 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001185 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001186 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001187 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001188 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001189}
1190
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001191void print_sysinfo(void)
1192{
1193#if HAVE_UTSNAME == 1
1194 struct utsname osinfo;
1195 uname(&osinfo);
1196
1197 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1198 osinfo.machine);
1199#else
1200 msg_ginfo(" on unknown machine");
1201#endif
1202 msg_ginfo(", built with");
1203#if NEED_PCI == 1
1204#ifdef PCILIB_VERSION
1205 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1206#else
1207 msg_ginfo(" unknown PCI library,");
1208#endif
1209#endif
1210#ifdef __clang__
1211 msg_ginfo(" LLVM %i/clang %i", __llvm__, __clang__);
1212#elif defined(__GNUC__)
1213 msg_ginfo(" GCC");
1214#ifdef __VERSION__
1215 msg_ginfo(" %s", __VERSION__);
1216#else
1217 msg_ginfo(" unknown version");
1218#endif
1219#else
1220 msg_ginfo(" unknown compiler");
1221#endif
1222 msg_ginfo("\n");
1223}
1224
Bernhard Walle201bde32008-01-21 15:24:22 +00001225void print_version(void)
1226{
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001227 msg_ginfo("flashrom v%s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001228 print_sysinfo();
Bernhard Walle201bde32008-01-21 15:24:22 +00001229}
1230
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001231void print_banner(void)
1232{
1233 msg_ginfo("flashrom is free software, get the source code at "
1234 "http://www.flashrom.org\n");
1235 msg_ginfo("\n");
1236}
1237
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001238int selfcheck(void)
1239{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001240 int ret = 0;
1241 struct flashchip *flash;
1242
1243 /* Safety check. Instead of aborting after the first error, check
1244 * if more errors exist.
1245 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001246 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001247 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001248 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001249 }
1250 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001251 msg_gerr("SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001252 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001253 }
1254#if BITBANG_SPI_SUPPORT == 1
1255 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001256 msg_gerr("Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001257 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001258 }
1259#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001260 for (flash = flashchips; flash && flash->name; flash++)
1261 if (selfcheck_eraseblocks(flash))
1262 ret = 1;
1263 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001264}
1265
1266void check_chip_supported(struct flashchip *flash)
1267{
1268 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001269 msg_cinfo("===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001270 if (flash->tested & TEST_BAD_MASK) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001271 msg_cinfo("This flash part has status NOT WORKING for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001272 if (flash->tested & TEST_BAD_PROBE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001273 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001274 if (flash->tested & TEST_BAD_READ)
Sean Nelson316a29f2010-05-07 20:09:04 +00001275 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001276 if (flash->tested & TEST_BAD_ERASE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001277 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001278 if (flash->tested & TEST_BAD_WRITE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001279 msg_cinfo(" WRITE");
1280 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001281 }
1282 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1283 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1284 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1285 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001286 msg_cinfo("This flash part has status UNTESTED for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001287 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001288 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001289 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
Sean Nelson316a29f2010-05-07 20:09:04 +00001290 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001291 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001292 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001293 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001294 msg_cinfo(" WRITE");
1295 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001296 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001297 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001298 msg_cinfo("The test status of this chip may have been updated "
1299 "in the latest development\n"
1300 "version of flashrom. If you are running the latest "
1301 "development version,\n"
1302 "please email a report to flashrom@flashrom.org if "
1303 "any of the above operations\n"
1304 "work correctly for you with this flash part. Please "
1305 "include the flashrom\n"
1306 "output with the additional -V option for all "
1307 "operations you tested (-V, -Vr,\n"
1308 "-Vw, -VE), and mention which mainboard or "
1309 "programmer you tested.\n"
1310 "Thanks for your help!\n"
1311 "===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001312 }
1313}
1314
Ollie Lho761bf1b2004-03-20 16:46:10 +00001315int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001316{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001317 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001318}
1319
1320/* This function signature is horrible. We need to design a better interface,
1321 * but right now it allows us to split off the CLI code.
1322 */
1323int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1324{
1325 uint8_t *buf;
1326 unsigned long numbytes;
1327 FILE *image;
1328 int ret = 0;
1329 unsigned long size;
1330
1331 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001332 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001333
Ollie Lhoefa28582004-12-08 20:10:01 +00001334 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001335 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001336 msg_cerr("Erase is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001337 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001338 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001339 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001340 return 1;
1341 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001342 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001343 }
1344 }
Sean Nelson6e0b9122010-02-19 00:52:10 +00001345 if (flash->unlock)
1346 flash->unlock(flash);
1347
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001348 if (erase_flash(flash)) {
1349 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001350 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001351 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001352 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001353 } else if (read_it) {
Sean Nelson6e0b9122010-02-19 00:52:10 +00001354 if (flash->unlock)
1355 flash->unlock(flash);
1356
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001357 if (read_flash(flash, filename)) {
1358 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001359 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001360 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001361 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001362 struct stat image_stat;
1363
Sean Nelson6e0b9122010-02-19 00:52:10 +00001364 if (flash->unlock)
1365 flash->unlock(flash);
1366
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001367 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001368 msg_cerr("Erase is not working on this chip "
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001369 "and erase is needed for write. ");
1370 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001371 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001372 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001373 return 1;
1374 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001375 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001376 }
1377 }
1378 if (flash->tested & TEST_BAD_WRITE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001379 msg_cerr("Write is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001380 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001381 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001382 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001383 return 1;
1384 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001385 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001386 }
1387 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001388 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001389 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001390 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001391 exit(1);
1392 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001393 if (fstat(fileno(image), &image_stat) != 0) {
1394 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001395 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001396 exit(1);
1397 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001398 if (image_stat.st_size != flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001399 msg_gerr("Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001400 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001401 exit(1);
1402 }
1403
Peter Stuge1fec0f32009-01-12 21:00:35 +00001404 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001405#if INTERNAL_SUPPORT == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001406 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001407#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001408 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001409 if (numbytes != size) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001410 msg_gerr("Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001411 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001412 return 1;
1413 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001414 }
1415
Ollie Lho184a4042005-11-26 21:55:36 +00001416 // This should be moved into each flash part's code to do it
1417 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001418 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001419
Ollie Lho184a4042005-11-26 21:55:36 +00001420 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001421
Peter Stugef31104c2008-04-28 14:47:30 +00001422 if (write_it) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001423 msg_cinfo("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001424 if (!flash->write) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001425 msg_cerr("Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001426 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001427 return 1;
1428 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001429 ret = flash->write(flash, buf);
1430 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001431 msg_cerr("FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001432 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001433 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001434 return 1;
1435 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001436 msg_cinfo("COMPLETE.\n");
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001437 }
Peter Stugef31104c2008-04-28 14:47:30 +00001438 }
Ollie Lho184a4042005-11-26 21:55:36 +00001439
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001440 if (verify_it) {
1441 /* Work around chips which need some time to calm down. */
1442 if (write_it)
1443 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001444 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001445 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001446 * might have an emergency situation.
1447 */
1448 if (ret && write_it)
1449 emergency_help_message();
1450 }
Ollie Lho184a4042005-11-26 21:55:36 +00001451
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001452 programmer_shutdown();
1453
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001454 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001455}