blob: 0f51979ae5b398709d6c180c000cdf131ac6d8ae [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
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000024#include <stdio.h>
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000025#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000026#include <sys/types.h>
27#include <sys/stat.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000028#include <string.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000029#include <stdlib.h>
Ollie Lho184a4042005-11-26 21:55:36 +000030#include <getopt.h>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000031#if HAVE_UTSNAME == 1
32#include <sys/utsname.h>
33#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000034#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000035#include "flashchips.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000036
Carl-Daniel Hailfingera80cfbc2009-07-22 20:13:00 +000037const char *flashrom_version = FLASHROM_VERSION;
Ronald G. Minnichceec4202003-07-25 04:37:41 +000038char *chip_to_probe = NULL;
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000039int verbose = 0;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000040
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000041#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +000042enum programmer programmer = PROGRAMMER_INTERNAL;
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000043#elif CONFIG_DUMMY == 1
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000044enum programmer programmer = PROGRAMMER_DUMMY;
45#else
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000046/* If neither internal nor dummy are selected, we must pick a sensible default.
47 * Since there is no reason to prefer a particular external programmer, we fail
48 * if more than one of them is selected. If only one is selected, it is clear
49 * that the user wants that one to become the default.
50 */
Andrew Morganc29c2e72010-06-07 22:37:54 +000051#if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_FT2232_SPI+CONFIG_SERPROG+CONFIG_BUSPIRATE_SPI+CONFIG_DEDIPROG > 1
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000052#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all programmers except one.
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000053#endif
54enum programmer programmer =
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000055#if CONFIG_NIC3COM == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000056 PROGRAMMER_NIC3COM
57#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000058#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +000059 PROGRAMMER_NICREALTEK
60 PROGRAMMER_NICREALTEK2
61#endif
Andrew Morganc29c2e72010-06-07 22:37:54 +000062#if CONFIG_NICNATSEMI == 1
63 PROGRAMMER_NICNATSEMI
64#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000065#if CONFIG_GFXNVIDIA == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000066 PROGRAMMER_GFXNVIDIA
67#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000068#if CONFIG_DRKAISER == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000069 PROGRAMMER_DRKAISER
70#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000071#if CONFIG_SATASII == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000072 PROGRAMMER_SATASII
73#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000074#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +000075 PROGRAMMER_ATAHPT
76#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000077#if CONFIG_FT2232_SPI == 1
78 PROGRAMMER_FT2232_SPI
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000079#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000080#if CONFIG_SERPROG == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000081 PROGRAMMER_SERPROG
82#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000083#if CONFIG_BUSPIRATE_SPI == 1
84 PROGRAMMER_BUSPIRATE_SPI
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000085#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000086#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000087 PROGRAMMER_DEDIPROG
88#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000089;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000090#endif
91
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000092char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000093
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000094/**
95 * flashrom defaults to Parallel/LPC/FWH flash devices. If a known host
96 * controller is found, the init routine sets the buses_supported bitfield to
97 * contain the supported buses for that controller.
98 */
99enum chipbustype buses_supported = CHIP_BUSTYPE_NONSPI;
100
101/**
102 * Programmers supporting multiple buses can have differing size limits on
103 * each bus. Store the limits for each bus in a common struct.
104 */
105struct decode_sizes max_rom_decode = {
106 .parallel = 0xffffffff,
107 .lpc = 0xffffffff,
108 .fwh = 0xffffffff,
109 .spi = 0xffffffff
110};
111
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000112const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000113#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000114 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000115 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000116 .init = internal_init,
117 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000118 .map_flash_region = physmap,
119 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000120 .chip_readb = internal_chip_readb,
121 .chip_readw = internal_chip_readw,
122 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000123 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000124 .chip_writeb = internal_chip_writeb,
125 .chip_writew = internal_chip_writew,
126 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000127 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000128 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000129 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000130#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000131
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000132#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000133 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000134 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000135 .init = dummy_init,
136 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000137 .map_flash_region = dummy_map,
138 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000139 .chip_readb = dummy_chip_readb,
140 .chip_readw = dummy_chip_readw,
141 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000142 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000143 .chip_writeb = dummy_chip_writeb,
144 .chip_writew = dummy_chip_writew,
145 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000146 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000147 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000148 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000149#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000150
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000151#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000152 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000153 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000154 .init = nic3com_init,
155 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000156 .map_flash_region = fallback_map,
157 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000158 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000159 .chip_readw = fallback_chip_readw,
160 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000161 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000162 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000163 .chip_writew = fallback_chip_writew,
164 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000165 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000166 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000167 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000168#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000169
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000170#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000171 {
172 .name = "nicrealtek",
173 .init = nicrealtek_init,
174 .shutdown = nicrealtek_shutdown,
175 .map_flash_region = fallback_map,
176 .unmap_flash_region = fallback_unmap,
177 .chip_readb = nicrealtek_chip_readb,
178 .chip_readw = fallback_chip_readw,
179 .chip_readl = fallback_chip_readl,
180 .chip_readn = fallback_chip_readn,
181 .chip_writeb = nicrealtek_chip_writeb,
182 .chip_writew = fallback_chip_writew,
183 .chip_writel = fallback_chip_writel,
184 .chip_writen = fallback_chip_writen,
185 .delay = internal_delay,
186 },
187 {
188 .name = "nicsmc1211",
189 .init = nicsmc1211_init,
190 .shutdown = nicrealtek_shutdown,
191 .map_flash_region = fallback_map,
192 .unmap_flash_region = fallback_unmap,
193 .chip_readb = nicrealtek_chip_readb,
194 .chip_readw = fallback_chip_readw,
195 .chip_readl = fallback_chip_readl,
196 .chip_readn = fallback_chip_readn,
197 .chip_writeb = nicrealtek_chip_writeb,
198 .chip_writew = fallback_chip_writew,
199 .chip_writel = fallback_chip_writel,
200 .chip_writen = fallback_chip_writen,
201 .delay = internal_delay,
202 },
203#endif
204
Andrew Morganc29c2e72010-06-07 22:37:54 +0000205#if CONFIG_NICNATSEMI == 1
206 {
207 .name = "nicnatsemi",
208 .init = nicnatsemi_init,
209 .shutdown = nicnatsemi_shutdown,
210 .map_flash_region = fallback_map,
211 .unmap_flash_region = fallback_unmap,
212 .chip_readb = nicnatsemi_chip_readb,
213 .chip_readw = fallback_chip_readw,
214 .chip_readl = fallback_chip_readl,
215 .chip_readn = fallback_chip_readn,
216 .chip_writeb = nicnatsemi_chip_writeb,
217 .chip_writew = fallback_chip_writew,
218 .chip_writel = fallback_chip_writel,
219 .chip_writen = fallback_chip_writen,
220 .delay = internal_delay,
221 },
222#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000223
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000224#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000225 {
226 .name = "gfxnvidia",
227 .init = gfxnvidia_init,
228 .shutdown = gfxnvidia_shutdown,
229 .map_flash_region = fallback_map,
230 .unmap_flash_region = fallback_unmap,
231 .chip_readb = gfxnvidia_chip_readb,
232 .chip_readw = fallback_chip_readw,
233 .chip_readl = fallback_chip_readl,
234 .chip_readn = fallback_chip_readn,
235 .chip_writeb = gfxnvidia_chip_writeb,
236 .chip_writew = fallback_chip_writew,
237 .chip_writel = fallback_chip_writel,
238 .chip_writen = fallback_chip_writen,
239 .delay = internal_delay,
240 },
241#endif
242
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000243#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000244 {
TURBO Jb0912c02009-09-02 23:00:46 +0000245 .name = "drkaiser",
246 .init = drkaiser_init,
247 .shutdown = drkaiser_shutdown,
248 .map_flash_region = fallback_map,
249 .unmap_flash_region = fallback_unmap,
250 .chip_readb = drkaiser_chip_readb,
251 .chip_readw = fallback_chip_readw,
252 .chip_readl = fallback_chip_readl,
253 .chip_readn = fallback_chip_readn,
254 .chip_writeb = drkaiser_chip_writeb,
255 .chip_writew = fallback_chip_writew,
256 .chip_writel = fallback_chip_writel,
257 .chip_writen = fallback_chip_writen,
258 .delay = internal_delay,
259 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000260#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000261
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000262#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000263 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000264 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000265 .init = satasii_init,
266 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000267 .map_flash_region = fallback_map,
268 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000269 .chip_readb = satasii_chip_readb,
270 .chip_readw = fallback_chip_readw,
271 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000272 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000273 .chip_writeb = satasii_chip_writeb,
274 .chip_writew = fallback_chip_writew,
275 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000276 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000277 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000278 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000279#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000280
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000281#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000282 {
283 .name = "atahpt",
284 .init = atahpt_init,
285 .shutdown = atahpt_shutdown,
286 .map_flash_region = fallback_map,
287 .unmap_flash_region = fallback_unmap,
288 .chip_readb = atahpt_chip_readb,
289 .chip_readw = fallback_chip_readw,
290 .chip_readl = fallback_chip_readl,
291 .chip_readn = fallback_chip_readn,
292 .chip_writeb = atahpt_chip_writeb,
293 .chip_writew = fallback_chip_writew,
294 .chip_writel = fallback_chip_writel,
295 .chip_writen = fallback_chip_writen,
296 .delay = internal_delay,
297 },
298#endif
299
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000300#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000301#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000302 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000303 .name = "it87spi",
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000304 .init = it87spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000305 .shutdown = noop_shutdown,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000306 .map_flash_region = fallback_map,
307 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000308 .chip_readb = noop_chip_readb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000309 .chip_readw = fallback_chip_readw,
310 .chip_readl = fallback_chip_readl,
311 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000312 .chip_writeb = noop_chip_writeb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000313 .chip_writew = fallback_chip_writew,
314 .chip_writel = fallback_chip_writel,
315 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000316 .delay = internal_delay,
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000317 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000318#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000319#endif
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000320
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000321#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000322 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000323 .name = "ft2232_spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000324 .init = ft2232_spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000325 .shutdown = noop_shutdown, /* Missing shutdown */
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000326 .map_flash_region = fallback_map,
327 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000328 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000329 .chip_readw = fallback_chip_readw,
330 .chip_readl = fallback_chip_readl,
331 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000332 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000333 .chip_writew = fallback_chip_writew,
334 .chip_writel = fallback_chip_writel,
335 .chip_writen = fallback_chip_writen,
336 .delay = internal_delay,
337 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000338#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000339
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000340#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000341 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000342 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000343 .init = serprog_init,
344 .shutdown = serprog_shutdown,
345 .map_flash_region = fallback_map,
346 .unmap_flash_region = fallback_unmap,
347 .chip_readb = serprog_chip_readb,
348 .chip_readw = fallback_chip_readw,
349 .chip_readl = fallback_chip_readl,
350 .chip_readn = serprog_chip_readn,
351 .chip_writeb = serprog_chip_writeb,
352 .chip_writew = fallback_chip_writew,
353 .chip_writel = fallback_chip_writel,
354 .chip_writen = fallback_chip_writen,
355 .delay = serprog_delay,
356 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000357#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000358
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000359#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000360 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000361 .name = "buspirate_spi",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000362 .init = buspirate_spi_init,
363 .shutdown = buspirate_spi_shutdown,
364 .map_flash_region = fallback_map,
365 .unmap_flash_region = fallback_unmap,
366 .chip_readb = noop_chip_readb,
367 .chip_readw = fallback_chip_readw,
368 .chip_readl = fallback_chip_readl,
369 .chip_readn = fallback_chip_readn,
370 .chip_writeb = noop_chip_writeb,
371 .chip_writew = fallback_chip_writew,
372 .chip_writel = fallback_chip_writel,
373 .chip_writen = fallback_chip_writen,
374 .delay = internal_delay,
375 },
376#endif
377
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000378#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000379 {
380 .name = "dediprog",
381 .init = dediprog_init,
382 .shutdown = dediprog_shutdown,
383 .map_flash_region = fallback_map,
384 .unmap_flash_region = fallback_unmap,
385 .chip_readb = noop_chip_readb,
386 .chip_readw = fallback_chip_readw,
387 .chip_readl = fallback_chip_readl,
388 .chip_readn = fallback_chip_readn,
389 .chip_writeb = noop_chip_writeb,
390 .chip_writew = fallback_chip_writew,
391 .chip_writel = fallback_chip_writel,
392 .chip_writen = fallback_chip_writen,
393 .delay = internal_delay,
394 },
395#endif
396
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000397 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000398};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000399
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000400#define SHUTDOWN_MAXFN 4
401static int shutdown_fn_count = 0;
402struct shutdown_func_data {
403 void (*func) (void *data);
404 void *data;
405} shutdown_fn[SHUTDOWN_MAXFN];
406
407/* Register a function to be executed on programmer shutdown.
408 * The advantage over atexit() is that you can supply a void pointer which will
409 * be used as parameter to the registered function upon programmer shutdown.
410 * This pointer can point to arbitrary data used by said function, e.g. undo
411 * information for GPIO settings etc. If unneeded, set data=NULL.
412 * Please note that the first (void *data) belongs to the function signature of
413 * the function passed as first parameter.
414 */
415int register_shutdown(void (*function) (void *data), void *data)
416{
417 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000418 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000419 SHUTDOWN_MAXFN);
420 return 1;
421 }
422 shutdown_fn[shutdown_fn_count].func = function;
423 shutdown_fn[shutdown_fn_count].data = data;
424 shutdown_fn_count++;
425
426 return 0;
427}
428
Uwe Hermann09e04f72009-05-16 22:36:00 +0000429int programmer_init(void)
430{
431 return programmer_table[programmer].init();
432}
433
434int programmer_shutdown(void)
435{
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000436 int i;
437
438 for (i = shutdown_fn_count - 1; i >= 0; i--)
439 shutdown_fn[i].func(shutdown_fn[i].data);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000440 return programmer_table[programmer].shutdown();
441}
442
443void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
444 size_t len)
445{
446 return programmer_table[programmer].map_flash_region(descr,
447 phys_addr, len);
448}
449
450void programmer_unmap_flash_region(void *virt_addr, size_t len)
451{
452 programmer_table[programmer].unmap_flash_region(virt_addr, len);
453}
454
455void chip_writeb(uint8_t val, chipaddr addr)
456{
457 programmer_table[programmer].chip_writeb(val, addr);
458}
459
460void chip_writew(uint16_t val, chipaddr addr)
461{
462 programmer_table[programmer].chip_writew(val, addr);
463}
464
465void chip_writel(uint32_t val, chipaddr addr)
466{
467 programmer_table[programmer].chip_writel(val, addr);
468}
469
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000470void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
471{
472 programmer_table[programmer].chip_writen(buf, addr, len);
473}
474
Uwe Hermann09e04f72009-05-16 22:36:00 +0000475uint8_t chip_readb(const chipaddr addr)
476{
477 return programmer_table[programmer].chip_readb(addr);
478}
479
480uint16_t chip_readw(const chipaddr addr)
481{
482 return programmer_table[programmer].chip_readw(addr);
483}
484
485uint32_t chip_readl(const chipaddr addr)
486{
487 return programmer_table[programmer].chip_readl(addr);
488}
489
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000490void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
491{
492 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000493}
494
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000495void programmer_delay(int usecs)
496{
497 programmer_table[programmer].delay(usecs);
498}
499
Peter Stuge776d2022009-01-26 00:39:57 +0000500void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000501{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000502 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000503 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000504 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000505 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000506}
507
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000508int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000509{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000510 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000511
512 return 0;
513}
514
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000515unsigned long flashbase = 0;
516
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000517int min(int a, int b)
518{
519 return (a < b) ? a : b;
520}
521
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000522int max(int a, int b)
523{
524 return (a > b) ? a : b;
525}
526
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000527int bitcount(unsigned long a)
528{
529 int i = 0;
530 for (; a != 0; a >>= 1)
531 if (a & 1)
532 i++;
533 return i;
534}
535
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000536char *strcat_realloc(char *dest, const char *src)
537{
538 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
539 if (!dest)
540 return NULL;
541 strcat(dest, src);
542 return dest;
543}
544
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000545/* This is a somewhat hacked function similar in some ways to strtok().
546 * It will look for needle in haystack, return a copy of needle and remove
547 * everything from the first occurrence of needle to the next delimiter
548 * from haystack.
549 */
550char *extract_param(char **haystack, char *needle, char *delim)
551{
552 char *param_pos, *rest, *tmp;
553 char *dev = NULL;
554 int devlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000555 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000556
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000557 needlelen = strlen(needle);
558 if (!needlelen) {
559 msg_gerr("%s: empty needle! Please report a bug at "
560 "flashrom@flashrom.org\n", __func__);
561 return NULL;
562 }
563 /* No programmer parameters given. */
564 if (*haystack == NULL)
565 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000566 param_pos = strstr(*haystack, needle);
567 do {
568 if (!param_pos)
569 return NULL;
570 /* Beginning of the string? */
571 if (param_pos == *haystack)
572 break;
573 /* After a delimiter? */
574 if (strchr(delim, *(param_pos - 1)))
575 break;
576 /* Continue searching. */
577 param_pos++;
578 param_pos = strstr(param_pos, needle);
579 } while (1);
580
581 if (param_pos) {
582 param_pos += strlen(needle);
583 devlen = strcspn(param_pos, delim);
584 if (devlen) {
585 dev = malloc(devlen + 1);
586 if (!dev) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000587 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000588 exit(1);
589 }
590 strncpy(dev, param_pos, devlen);
591 dev[devlen] = '\0';
592 }
593 rest = param_pos + devlen;
594 rest += strspn(rest, delim);
595 param_pos -= strlen(needle);
596 memmove(param_pos, rest, strlen(rest) + 1);
597 tmp = realloc(*haystack, strlen(*haystack) + 1);
598 if (!tmp) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000599 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000600 exit(1);
601 }
602 *haystack = tmp;
603 }
604
605
606 return dev;
607}
608
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000609/* start is an offset to the base address of the flash chip */
610int check_erased_range(struct flashchip *flash, int start, int len)
611{
612 int ret;
613 uint8_t *cmpbuf = malloc(len);
614
615 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000616 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000617 exit(1);
618 }
619 memset(cmpbuf, 0xff, len);
620 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
621 free(cmpbuf);
622 return ret;
623}
624
625/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000626 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
627 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000628 * @start offset to the base address of the flash chip
629 * @len length of the verified area
630 * @message string to print in the "FAILED" message
631 * @return 0 for success, -1 for failure
632 */
633int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
634{
635 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000636 int page_size = flash->page_size;
637 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000638 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000639
640 if (!len)
641 goto out_free;
642
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000643 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000644 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000645 return 1;
646 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000647 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000648 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000649 exit(1);
650 }
651
652 if (start + len > flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000653 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000654 " total_size 0x%x\n", __func__, start, len,
655 flash->total_size * 1024);
656 ret = -1;
657 goto out_free;
658 }
659 if (!message)
660 message = "VERIFY";
661
662 /* Warning: This loop has a very unusual condition and body.
663 * The loop needs to go through each page with at least one affected
664 * byte. The lowest page number is (start / page_size) since that
665 * division rounds down. The highest page number we want is the page
666 * where the last byte of the range lives. That last byte has the
667 * address (start + len - 1), thus the highest page number is
668 * (start + len - 1) / page_size. Since we want to include that last
669 * page as well, the loop condition uses <=.
670 */
671 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
672 /* Byte position of the first byte in the range in this page. */
673 starthere = max(start, i * page_size);
674 /* Length of bytes in the range in this page. */
675 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000676 flash->read(flash, readbuf, starthere, lenhere);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000677 for (j = 0; j < lenhere; j++) {
678 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000679 /* Only print the first failure. */
680 if (!failcount++)
Sean Nelson316a29f2010-05-07 20:09:04 +0000681 msg_cerr("%s FAILED at 0x%08x! "
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000682 "Expected=0x%02x, Read=0x%02x,",
683 message, starthere + j,
684 cmpbuf[starthere - start + j],
685 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000686 }
687 }
688 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000689 if (failcount) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000690 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000691 start, start + len - 1, failcount);
692 ret = -1;
693 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000694
695out_free:
696 free(readbuf);
697 return ret;
698}
699
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000700/**
701 * Check if the buffer @have can be programmed to the content of @want without
702 * erasing. This is only possible if all chunks of size @gran are either kept
703 * as-is or changed from an all-ones state to any other state.
704 * The following write granularities (enum @gran) are known:
705 * - 1 bit. Each bit can be cleared individually.
706 * - 1 byte. A byte can be written once. Further writes to an already written
707 * byte cause the contents to be either undefined or to stay unchanged.
708 * - 128 bytes. If less than 128 bytes are written, the rest will be
709 * erased. Each write to a 128-byte region will trigger an automatic erase
710 * before anything is written. Very uncommon behaviour and unsupported by
711 * this function.
712 * - 256 bytes. If less than 256 bytes are written, the contents of the
713 * unwritten bytes are undefined.
714 *
715 * @have buffer with current content
716 * @want buffer with desired content
717 * @len length of the verified area
718 * @gran write granularity (enum, not count)
719 * @return 0 if no erase is needed, 1 otherwise
720 */
721int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
722{
723 int result = 0;
724 int i, j, limit;
725
726 switch (gran) {
727 case write_gran_1bit:
728 for (i = 0; i < len; i++)
729 if ((have[i] & want[i]) != want[i]) {
730 result = 1;
731 break;
732 }
733 break;
734 case write_gran_1byte:
735 for (i = 0; i < len; i++)
736 if ((have[i] != want[i]) && (have[i] != 0xff)) {
737 result = 1;
738 break;
739 }
740 break;
741 case write_gran_256bytes:
742 for (j = 0; j < len / 256; j++) {
743 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000744 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000745 if (!memcmp(have + j * 256, want + j * 256, limit))
746 continue;
747 /* have needs to be in erased state. */
748 for (i = 0; i < limit; i++)
749 if (have[i] != 0xff) {
750 result = 1;
751 break;
752 }
753 if (result)
754 break;
755 }
756 break;
757 }
758 return result;
759}
760
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000761/* This function generates various test patterns useful for testing controller
762 * and chip communication as well as chip behaviour.
763 *
764 * If a byte can be written multiple times, each time keeping 0-bits at 0
765 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
766 * is essentially an AND operation. That's also the reason why this function
767 * provides the result of AND between various patterns.
768 *
769 * Below is a list of patterns (and their block length).
770 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
771 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
772 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
773 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
774 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
775 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
776 * Pattern 6 is 00 (1 Byte)
777 * Pattern 7 is ff (1 Byte)
778 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
779 * byte block.
780 *
781 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
782 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
783 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
784 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
785 * Pattern 12 is 00 (1 Byte)
786 * Pattern 13 is ff (1 Byte)
787 * Patterns 8-13 have no block number.
788 *
789 * Patterns 0-3 are created to detect and efficiently diagnose communication
790 * slips like missed bits or bytes and their repetitive nature gives good visual
791 * cues to the person inspecting the results. In addition, the following holds:
792 * AND Pattern 0/1 == Pattern 4
793 * AND Pattern 2/3 == Pattern 5
794 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
795 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
796 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
797 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
798 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
799 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
800 * Besides that, they provide for bit testing of the last two bytes of every
801 * 256 byte block which contains the block number for patterns 0-6.
802 * Patterns 10-11 are special purpose for detecting subblock aliasing with
803 * block sizes >256 bytes (some Dataflash chips etc.)
804 * AND Pattern 8/9 == Pattern 12
805 * AND Pattern 10/11 == Pattern 12
806 * Pattern 13 is the completely erased state.
807 * None of the patterns can detect aliasing at boundaries which are a multiple
808 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
809 */
810int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
811{
812 int i;
813
814 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000815 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000816 return 1;
817 }
818
819 switch (variant) {
820 case 0:
821 for (i = 0; i < size; i++)
822 buf[i] = (i & 0xf) << 4 | 0x5;
823 break;
824 case 1:
825 for (i = 0; i < size; i++)
826 buf[i] = (i & 0xf) << 4 | 0xa;
827 break;
828 case 2:
829 for (i = 0; i < size; i++)
830 buf[i] = 0x50 | (i & 0xf);
831 break;
832 case 3:
833 for (i = 0; i < size; i++)
834 buf[i] = 0xa0 | (i & 0xf);
835 break;
836 case 4:
837 for (i = 0; i < size; i++)
838 buf[i] = (i & 0xf) << 4;
839 break;
840 case 5:
841 for (i = 0; i < size; i++)
842 buf[i] = i & 0xf;
843 break;
844 case 6:
845 memset(buf, 0x00, size);
846 break;
847 case 7:
848 memset(buf, 0xff, size);
849 break;
850 case 8:
851 for (i = 0; i < size; i++)
852 buf[i] = i & 0xff;
853 break;
854 case 9:
855 for (i = 0; i < size; i++)
856 buf[i] = ~(i & 0xff);
857 break;
858 case 10:
859 for (i = 0; i < size % 2; i++) {
860 buf[i * 2] = (i >> 8) & 0xff;
861 buf[i * 2 + 1] = i & 0xff;
862 }
863 if (size & 0x1)
864 buf[i * 2] = (i >> 8) & 0xff;
865 break;
866 case 11:
867 for (i = 0; i < size % 2; i++) {
868 buf[i * 2] = ~((i >> 8) & 0xff);
869 buf[i * 2 + 1] = ~(i & 0xff);
870 }
871 if (size & 0x1)
872 buf[i * 2] = ~((i >> 8) & 0xff);
873 break;
874 case 12:
875 memset(buf, 0x00, size);
876 break;
877 case 13:
878 memset(buf, 0xff, size);
879 break;
880 }
881
882 if ((variant >= 0) && (variant <= 7)) {
883 /* Write block number in the last two bytes of each 256-byte
884 * block, big endian for easier reading of the hexdump.
885 * Note that this wraps around for chips larger than 2^24 bytes
886 * (16 MB).
887 */
888 for (i = 0; i < size / 256; i++) {
889 buf[i * 256 + 254] = (i >> 8) & 0xff;
890 buf[i * 256 + 255] = i & 0xff;
891 }
892 }
893
894 return 0;
895}
896
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000897int check_max_decode(enum chipbustype buses, uint32_t size)
898{
899 int limitexceeded = 0;
900 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
901 (max_rom_decode.parallel < 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.parallel / 1024, "Parallel");
908 }
909 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
910 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000911 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000912 "size %u kB of chipset/board/programmer "
913 "for %s interface, "
914 "probe/read/erase/write may fail. ", size / 1024,
915 max_rom_decode.lpc / 1024, "LPC");
916 }
917 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
918 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000919 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000920 "size %u kB of chipset/board/programmer "
921 "for %s interface, "
922 "probe/read/erase/write may fail. ", size / 1024,
923 max_rom_decode.fwh / 1024, "FWH");
924 }
925 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
926 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000927 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000928 "size %u kB of chipset/board/programmer "
929 "for %s interface, "
930 "probe/read/erase/write may fail. ", size / 1024,
931 max_rom_decode.spi / 1024, "SPI");
932 }
933 if (!limitexceeded)
934 return 0;
935 /* Sometimes chip and programmer have more than one bus in common,
936 * and the limit is not exceeded on all buses. Tell the user.
937 */
938 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000939 /* FIXME: This message is designed towards CLI users. */
Sean Nelson316a29f2010-05-07 20:09:04 +0000940 msg_pdbg("There is at least one common chip/programmer "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000941 "interface which can support a chip of this size. "
942 "You can try --force at your own risk.\n");
943 return 1;
944}
945
Peter Stuge483b8f02008-09-03 23:10:05 +0000946struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000947{
Peter Stuge483b8f02008-09-03 23:10:05 +0000948 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000949 unsigned long base = 0;
950 uint32_t size;
951 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000952 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000953
Peter Stuge483b8f02008-09-03 23:10:05 +0000954 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000955 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000956 continue;
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000957 msg_gdbg("Probing for %s %s, %d KB: ",
Stefan Reinauerac378972008-03-17 22:59:40 +0000958 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000959 if (!flash->probe && !force) {
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000960 msg_gdbg("failed! flashrom has no probe function for "
961 "this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000962 continue;
963 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000964 buses_common = buses_supported & flash->bustype;
965 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000966 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000967 msg_gdbg("skipped.");
968 msg_gspew(" Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000969 free(tmp);
970 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000971 msg_gspew("and chip bus type %s are incompatible.",
972 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000973 free(tmp);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000974 msg_gdbg("\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000975 continue;
976 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000977
Ollie Lhocbbf1252004-03-17 22:22:08 +0000978 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000979 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000980
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000981 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000982 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000983
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000984 if (force)
985 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000986
Peter Stuge483b8f02008-09-03 23:10:05 +0000987 if (flash->probe(flash) != 1)
988 goto notfound;
989
Uwe Hermann394131e2008-10-18 21:14:13 +0000990 if (first_flash == flashchips
991 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000992 break;
993
Peter Stuge483b8f02008-09-03 23:10:05 +0000994notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000995 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000996 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000997
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000998 if (!flash || !flash->name)
999 return NULL;
1000
Sean Nelson316a29f2010-05-07 20:09:04 +00001001 msg_cinfo("%s chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +00001002 force ? "Assuming" : "Found",
Uwe Hermann9899cad2009-06-28 21:47:57 +00001003 flash->vendor, flash->name, flash->total_size,
1004 flashbuses_to_text(flash->bustype), base);
1005
Sean Nelson6e0b9122010-02-19 00:52:10 +00001006 if (flash->printlock)
1007 flash->printlock(flash);
1008
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001009 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001010}
1011
Stefan Reinauere3705282005-12-18 16:41:10 +00001012int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001013{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001014 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +00001015 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001016
Sean Nelson316a29f2010-05-07 20:09:04 +00001017 msg_cinfo("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +00001018
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001019 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001020
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001021 if (!ret)
Sean Nelson316a29f2010-05-07 20:09:04 +00001022 msg_cinfo("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001023
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001024 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001025}
1026
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +00001027int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001028{
1029 unsigned long numbytes;
1030 FILE *image;
1031 unsigned long size = flash->total_size * 1024;
1032 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001033
1034 if (!filename) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001035 msg_gerr("Error: No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001036 return 1;
1037 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001038 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001039 perror(filename);
1040 exit(1);
1041 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001042 msg_cinfo("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001043 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001044 msg_cinfo("FAILED!\n");
1045 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001046 return 1;
1047 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +00001048 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001049
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001050 numbytes = fwrite(buf, 1, size, image);
1051 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +00001052 free(buf);
Sean Nelson316a29f2010-05-07 20:09:04 +00001053 msg_cinfo("%s.\n", numbytes == size ? "done" : "FAILED");
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001054 if (numbytes != size)
1055 return 1;
1056 return 0;
1057}
1058
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001059/* This function shares a lot of its structure with erase_flash().
1060 * Even if an error is found, the function will keep going and check the rest.
1061 */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001062int selfcheck_eraseblocks(struct flashchip *flash)
1063{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001064 int i, j, k;
1065 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001066
1067 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1068 unsigned int done = 0;
1069 struct block_eraser eraser = flash->block_erasers[k];
1070
1071 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1072 /* Blocks with zero size are bugs in flashchips.c. */
1073 if (eraser.eraseblocks[i].count &&
1074 !eraser.eraseblocks[i].size) {
1075 msg_gerr("ERROR: Flash chip %s erase function "
1076 "%i region %i has size 0. Please report"
1077 " a bug at flashrom@flashrom.org\n",
1078 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001079 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001080 }
1081 /* Blocks with zero count are bugs in flashchips.c. */
1082 if (!eraser.eraseblocks[i].count &&
1083 eraser.eraseblocks[i].size) {
1084 msg_gerr("ERROR: Flash chip %s erase function "
1085 "%i region %i has count 0. Please report"
1086 " a bug at flashrom@flashrom.org\n",
1087 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001088 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001089 }
1090 done += eraser.eraseblocks[i].count *
1091 eraser.eraseblocks[i].size;
1092 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001093 /* Empty eraseblock definition with erase function. */
1094 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001095 msg_gspew("Strange: Empty eraseblock definition with "
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001096 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001097 if (!done)
1098 continue;
1099 if (done != flash->total_size * 1024) {
1100 msg_gerr("ERROR: Flash chip %s erase function %i "
1101 "region walking resulted in 0x%06x bytes total,"
1102 " expected 0x%06x bytes. Please report a bug at"
1103 " flashrom@flashrom.org\n", flash->name, k,
1104 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001105 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001106 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001107 if (!eraser.block_erase)
1108 continue;
1109 /* Check if there are identical erase functions for different
1110 * layouts. That would imply "magic" erase functions. The
1111 * easiest way to check this is with function pointers.
1112 */
Uwe Hermann43959702010-03-13 17:28:29 +00001113 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001114 if (eraser.block_erase ==
1115 flash->block_erasers[j].block_erase) {
1116 msg_gerr("ERROR: Flash chip %s erase function "
1117 "%i and %i are identical. Please report"
1118 " a bug at flashrom@flashrom.org\n",
1119 flash->name, k, j);
1120 ret = 1;
1121 }
Uwe Hermann43959702010-03-13 17:28:29 +00001122 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001123 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001124 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001125}
1126
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001127int erase_flash(struct flashchip *flash)
1128{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001129 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001130 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001131
Sean Nelson316a29f2010-05-07 20:09:04 +00001132 msg_cinfo("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001133 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001134 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001135 struct block_eraser eraser = flash->block_erasers[k];
1136
Sean Nelson316a29f2010-05-07 20:09:04 +00001137 msg_cdbg("Looking at blockwise erase function %i... ", k);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001138 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001139 msg_cdbg("not defined. "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001140 "Looking for another erase function.\n");
1141 continue;
1142 }
1143 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001144 msg_cdbg("eraseblock layout is known, but no "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001145 "matching block erase function found. "
1146 "Looking for another erase function.\n");
1147 continue;
1148 }
1149 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001150 msg_cdbg("block erase function found, but "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001151 "eraseblock layout is unknown. "
1152 "Looking for another erase function.\n");
1153 continue;
1154 }
1155 found = 1;
Sean Nelson316a29f2010-05-07 20:09:04 +00001156 msg_cdbg("trying... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001157 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1158 /* count==0 for all automatically initialized array
1159 * members so the loop below won't be executed for them.
1160 */
1161 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001162 start = done + eraser.eraseblocks[i].size * j;
1163 len = eraser.eraseblocks[i].size;
Sean Nelson316a29f2010-05-07 20:09:04 +00001164 msg_cdbg("0x%06x-0x%06x, ", start,
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001165 start + len - 1);
1166 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001167 if (ret)
1168 break;
1169 }
1170 if (ret)
1171 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001172 done += eraser.eraseblocks[i].count *
1173 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001174 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001175 msg_cdbg("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001176 /* If everything is OK, don't try another erase function. */
1177 if (!ret)
1178 break;
1179 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001180 if (!found) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001181 msg_cerr("ERROR: flashrom has no erase function for this flash chip.\n");
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001182 return 1;
1183 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001184
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001185 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001186 msg_cerr("FAILED!\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001187 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001188 msg_cinfo("SUCCESS.\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001189 }
1190 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001191}
1192
Uwe Hermannc67d0372009-10-01 18:40:02 +00001193void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001194{
Sean Nelson316a29f2010-05-07 20:09:04 +00001195 msg_gerr("Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001196 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001197 "mail flashrom@flashrom.org!\n"
1198 "-------------------------------------------------------------"
1199 "------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001200 "DO NOT REBOOT OR POWEROFF!\n");
1201}
1202
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001203/* The way to go if you want a delimited list of programmers*/
1204void list_programmers(char *delim)
1205{
1206 enum programmer p;
1207 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001208 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001209 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001210 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001211 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001212 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001213}
1214
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001215void print_sysinfo(void)
1216{
1217#if HAVE_UTSNAME == 1
1218 struct utsname osinfo;
1219 uname(&osinfo);
1220
1221 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1222 osinfo.machine);
1223#else
1224 msg_ginfo(" on unknown machine");
1225#endif
1226 msg_ginfo(", built with");
1227#if NEED_PCI == 1
1228#ifdef PCILIB_VERSION
1229 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1230#else
1231 msg_ginfo(" unknown PCI library,");
1232#endif
1233#endif
1234#ifdef __clang__
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001235 msg_ginfo(" LLVM %i/clang %i, ", __llvm__, __clang__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001236#elif defined(__GNUC__)
1237 msg_ginfo(" GCC");
1238#ifdef __VERSION__
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001239 msg_ginfo(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001240#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001241 msg_ginfo(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001242#endif
1243#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001244 msg_ginfo(" unknown compiler,");
1245#endif
1246#if defined (__FLASHROM_LITTLE_ENDIAN__)
1247 msg_ginfo(" little endian");
1248#else
1249 msg_ginfo(" big endian");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001250#endif
1251 msg_ginfo("\n");
1252}
1253
Bernhard Walle201bde32008-01-21 15:24:22 +00001254void print_version(void)
1255{
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001256 msg_ginfo("flashrom v%s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001257 print_sysinfo();
Bernhard Walle201bde32008-01-21 15:24:22 +00001258}
1259
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001260void print_banner(void)
1261{
1262 msg_ginfo("flashrom is free software, get the source code at "
1263 "http://www.flashrom.org\n");
1264 msg_ginfo("\n");
1265}
1266
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001267int selfcheck(void)
1268{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001269 int ret = 0;
1270 struct flashchip *flash;
1271
1272 /* Safety check. Instead of aborting after the first error, check
1273 * if more errors exist.
1274 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001275 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001276 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001277 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001278 }
1279 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001280 msg_gerr("SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001281 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001282 }
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001283#if CONFIG_BITBANG_SPI == 1
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001284 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001285 msg_gerr("Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001286 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001287 }
1288#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001289 for (flash = flashchips; flash && flash->name; flash++)
1290 if (selfcheck_eraseblocks(flash))
1291 ret = 1;
1292 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001293}
1294
1295void check_chip_supported(struct flashchip *flash)
1296{
1297 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001298 msg_cinfo("===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001299 if (flash->tested & TEST_BAD_MASK) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001300 msg_cinfo("This flash part has status NOT WORKING for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001301 if (flash->tested & TEST_BAD_PROBE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001302 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001303 if (flash->tested & TEST_BAD_READ)
Sean Nelson316a29f2010-05-07 20:09:04 +00001304 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001305 if (flash->tested & TEST_BAD_ERASE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001306 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001307 if (flash->tested & TEST_BAD_WRITE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001308 msg_cinfo(" WRITE");
1309 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001310 }
1311 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1312 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1313 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1314 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001315 msg_cinfo("This flash part has status UNTESTED for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001316 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001317 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001318 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
Sean Nelson316a29f2010-05-07 20:09:04 +00001319 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001320 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001321 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001322 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001323 msg_cinfo(" WRITE");
1324 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001325 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001326 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001327 msg_cinfo("The test status of this chip may have been updated "
1328 "in the latest development\n"
1329 "version of flashrom. If you are running the latest "
1330 "development version,\n"
1331 "please email a report to flashrom@flashrom.org if "
1332 "any of the above operations\n"
1333 "work correctly for you with this flash part. Please "
1334 "include the flashrom\n"
1335 "output with the additional -V option for all "
1336 "operations you tested (-V, -Vr,\n"
1337 "-Vw, -VE), and mention which mainboard or "
1338 "programmer you tested.\n"
1339 "Thanks for your help!\n"
1340 "===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001341 }
1342}
1343
Ollie Lho761bf1b2004-03-20 16:46:10 +00001344int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001345{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001346 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001347}
1348
1349/* This function signature is horrible. We need to design a better interface,
1350 * but right now it allows us to split off the CLI code.
1351 */
1352int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1353{
1354 uint8_t *buf;
1355 unsigned long numbytes;
1356 FILE *image;
1357 int ret = 0;
1358 unsigned long size;
1359
1360 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001361 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001362
Ollie Lhoefa28582004-12-08 20:10:01 +00001363 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001364 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001365 msg_cerr("Erase is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001366 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001367 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001368 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001369 return 1;
1370 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001371 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001372 }
1373 }
Sean Nelson6e0b9122010-02-19 00:52:10 +00001374 if (flash->unlock)
1375 flash->unlock(flash);
1376
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001377 if (erase_flash(flash)) {
1378 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001379 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001380 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001381 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001382 } else if (read_it) {
Sean Nelson6e0b9122010-02-19 00:52:10 +00001383 if (flash->unlock)
1384 flash->unlock(flash);
1385
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001386 if (read_flash(flash, filename)) {
1387 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001388 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001389 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001390 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001391 struct stat image_stat;
1392
Sean Nelson6e0b9122010-02-19 00:52:10 +00001393 if (flash->unlock)
1394 flash->unlock(flash);
1395
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001396 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001397 msg_cerr("Erase is not working on this chip "
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001398 "and erase is needed for write. ");
1399 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001400 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001401 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001402 return 1;
1403 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001404 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001405 }
1406 }
1407 if (flash->tested & TEST_BAD_WRITE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001408 msg_cerr("Write is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001409 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001410 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001411 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001412 return 1;
1413 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001414 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001415 }
1416 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001417 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001418 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001419 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001420 exit(1);
1421 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001422 if (fstat(fileno(image), &image_stat) != 0) {
1423 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001424 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001425 exit(1);
1426 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001427 if (image_stat.st_size != flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001428 msg_gerr("Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001429 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001430 exit(1);
1431 }
1432
Peter Stuge1fec0f32009-01-12 21:00:35 +00001433 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001434#if CONFIG_INTERNAL == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001435 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001436#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001437 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001438 if (numbytes != size) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001439 msg_gerr("Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001440 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001441 return 1;
1442 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001443 }
1444
Ollie Lho184a4042005-11-26 21:55:36 +00001445 // This should be moved into each flash part's code to do it
1446 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001447 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001448
Ollie Lho184a4042005-11-26 21:55:36 +00001449 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001450
Peter Stugef31104c2008-04-28 14:47:30 +00001451 if (write_it) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001452 msg_cinfo("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001453 if (!flash->write) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001454 msg_cerr("Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001455 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001456 return 1;
1457 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001458 ret = flash->write(flash, buf);
1459 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001460 msg_cerr("FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001461 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001462 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001463 return 1;
1464 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001465 msg_cinfo("COMPLETE.\n");
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001466 }
Peter Stugef31104c2008-04-28 14:47:30 +00001467 }
Ollie Lho184a4042005-11-26 21:55:36 +00001468
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001469 if (verify_it) {
1470 /* Work around chips which need some time to calm down. */
1471 if (write_it)
1472 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001473 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001474 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001475 * might have an emergency situation.
1476 */
1477 if (ret && write_it)
1478 emergency_help_message();
1479 }
Ollie Lho184a4042005-11-26 21:55:36 +00001480
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001481 programmer_shutdown();
1482
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001483 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001484}