blob: 9f5ab576198a94c321b07bb3b9dd4ac1ffae4ce9 [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 Hailfingerad3cc552010-07-03 11:02:10 +000037const char * const 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 Hailfingerad3cc552010-07-03 11:02:10 +000094/* Supported buses for the current programmer. */
95enum chipbustype buses_supported;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000096
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 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000101struct decode_sizes max_rom_decode;
102
103/* If nonzero, used as the start address of bottom-aligned flash. */
104unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000105
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000106const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000107#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000108 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000109 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000110 .init = internal_init,
111 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000112 .map_flash_region = physmap,
113 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000114 .chip_readb = internal_chip_readb,
115 .chip_readw = internal_chip_readw,
116 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000117 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000118 .chip_writeb = internal_chip_writeb,
119 .chip_writew = internal_chip_writew,
120 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000121 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000122 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000123 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000124#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000125
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000126#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000127 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000128 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000129 .init = dummy_init,
130 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000131 .map_flash_region = dummy_map,
132 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000133 .chip_readb = dummy_chip_readb,
134 .chip_readw = dummy_chip_readw,
135 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000136 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000137 .chip_writeb = dummy_chip_writeb,
138 .chip_writew = dummy_chip_writew,
139 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000140 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000141 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000142 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000143#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000144
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000145#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000146 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000147 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000148 .init = nic3com_init,
149 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000150 .map_flash_region = fallback_map,
151 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000152 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000153 .chip_readw = fallback_chip_readw,
154 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000155 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000156 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000157 .chip_writew = fallback_chip_writew,
158 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000159 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000160 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000161 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000162#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000163
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000164#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000165 {
166 .name = "nicrealtek",
167 .init = nicrealtek_init,
168 .shutdown = nicrealtek_shutdown,
169 .map_flash_region = fallback_map,
170 .unmap_flash_region = fallback_unmap,
171 .chip_readb = nicrealtek_chip_readb,
172 .chip_readw = fallback_chip_readw,
173 .chip_readl = fallback_chip_readl,
174 .chip_readn = fallback_chip_readn,
175 .chip_writeb = nicrealtek_chip_writeb,
176 .chip_writew = fallback_chip_writew,
177 .chip_writel = fallback_chip_writel,
178 .chip_writen = fallback_chip_writen,
179 .delay = internal_delay,
180 },
181 {
182 .name = "nicsmc1211",
183 .init = nicsmc1211_init,
184 .shutdown = nicrealtek_shutdown,
185 .map_flash_region = fallback_map,
186 .unmap_flash_region = fallback_unmap,
187 .chip_readb = nicrealtek_chip_readb,
188 .chip_readw = fallback_chip_readw,
189 .chip_readl = fallback_chip_readl,
190 .chip_readn = fallback_chip_readn,
191 .chip_writeb = nicrealtek_chip_writeb,
192 .chip_writew = fallback_chip_writew,
193 .chip_writel = fallback_chip_writel,
194 .chip_writen = fallback_chip_writen,
195 .delay = internal_delay,
196 },
197#endif
198
Andrew Morganc29c2e72010-06-07 22:37:54 +0000199#if CONFIG_NICNATSEMI == 1
200 {
201 .name = "nicnatsemi",
202 .init = nicnatsemi_init,
203 .shutdown = nicnatsemi_shutdown,
204 .map_flash_region = fallback_map,
205 .unmap_flash_region = fallback_unmap,
206 .chip_readb = nicnatsemi_chip_readb,
207 .chip_readw = fallback_chip_readw,
208 .chip_readl = fallback_chip_readl,
209 .chip_readn = fallback_chip_readn,
210 .chip_writeb = nicnatsemi_chip_writeb,
211 .chip_writew = fallback_chip_writew,
212 .chip_writel = fallback_chip_writel,
213 .chip_writen = fallback_chip_writen,
214 .delay = internal_delay,
215 },
216#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000217
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000218#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000219 {
220 .name = "gfxnvidia",
221 .init = gfxnvidia_init,
222 .shutdown = gfxnvidia_shutdown,
223 .map_flash_region = fallback_map,
224 .unmap_flash_region = fallback_unmap,
225 .chip_readb = gfxnvidia_chip_readb,
226 .chip_readw = fallback_chip_readw,
227 .chip_readl = fallback_chip_readl,
228 .chip_readn = fallback_chip_readn,
229 .chip_writeb = gfxnvidia_chip_writeb,
230 .chip_writew = fallback_chip_writew,
231 .chip_writel = fallback_chip_writel,
232 .chip_writen = fallback_chip_writen,
233 .delay = internal_delay,
234 },
235#endif
236
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000237#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000238 {
TURBO Jb0912c02009-09-02 23:00:46 +0000239 .name = "drkaiser",
240 .init = drkaiser_init,
241 .shutdown = drkaiser_shutdown,
242 .map_flash_region = fallback_map,
243 .unmap_flash_region = fallback_unmap,
244 .chip_readb = drkaiser_chip_readb,
245 .chip_readw = fallback_chip_readw,
246 .chip_readl = fallback_chip_readl,
247 .chip_readn = fallback_chip_readn,
248 .chip_writeb = drkaiser_chip_writeb,
249 .chip_writew = fallback_chip_writew,
250 .chip_writel = fallback_chip_writel,
251 .chip_writen = fallback_chip_writen,
252 .delay = internal_delay,
253 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000254#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000255
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000256#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000257 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000258 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000259 .init = satasii_init,
260 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000261 .map_flash_region = fallback_map,
262 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000263 .chip_readb = satasii_chip_readb,
264 .chip_readw = fallback_chip_readw,
265 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000266 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000267 .chip_writeb = satasii_chip_writeb,
268 .chip_writew = fallback_chip_writew,
269 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000270 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000271 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000272 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000273#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000274
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000275#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000276 {
277 .name = "atahpt",
278 .init = atahpt_init,
279 .shutdown = atahpt_shutdown,
280 .map_flash_region = fallback_map,
281 .unmap_flash_region = fallback_unmap,
282 .chip_readb = atahpt_chip_readb,
283 .chip_readw = fallback_chip_readw,
284 .chip_readl = fallback_chip_readl,
285 .chip_readn = fallback_chip_readn,
286 .chip_writeb = atahpt_chip_writeb,
287 .chip_writew = fallback_chip_writew,
288 .chip_writel = fallback_chip_writel,
289 .chip_writen = fallback_chip_writen,
290 .delay = internal_delay,
291 },
292#endif
293
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000294#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000295#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000296 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000297 .name = "it87spi",
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000298 .init = it87spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000299 .shutdown = noop_shutdown,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000300 .map_flash_region = fallback_map,
301 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000302 .chip_readb = noop_chip_readb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000303 .chip_readw = fallback_chip_readw,
304 .chip_readl = fallback_chip_readl,
305 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000306 .chip_writeb = noop_chip_writeb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000307 .chip_writew = fallback_chip_writew,
308 .chip_writel = fallback_chip_writel,
309 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000310 .delay = internal_delay,
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000311 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000312#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000313#endif
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000314
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000315#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000316 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000317 .name = "ft2232_spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000318 .init = ft2232_spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000319 .shutdown = noop_shutdown, /* Missing shutdown */
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000320 .map_flash_region = fallback_map,
321 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000322 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000323 .chip_readw = fallback_chip_readw,
324 .chip_readl = fallback_chip_readl,
325 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000326 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000327 .chip_writew = fallback_chip_writew,
328 .chip_writel = fallback_chip_writel,
329 .chip_writen = fallback_chip_writen,
330 .delay = internal_delay,
331 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000332#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000333
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000334#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000335 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000336 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000337 .init = serprog_init,
338 .shutdown = serprog_shutdown,
339 .map_flash_region = fallback_map,
340 .unmap_flash_region = fallback_unmap,
341 .chip_readb = serprog_chip_readb,
342 .chip_readw = fallback_chip_readw,
343 .chip_readl = fallback_chip_readl,
344 .chip_readn = serprog_chip_readn,
345 .chip_writeb = serprog_chip_writeb,
346 .chip_writew = fallback_chip_writew,
347 .chip_writel = fallback_chip_writel,
348 .chip_writen = fallback_chip_writen,
349 .delay = serprog_delay,
350 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000351#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000352
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000353#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000354 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000355 .name = "buspirate_spi",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000356 .init = buspirate_spi_init,
357 .shutdown = buspirate_spi_shutdown,
358 .map_flash_region = fallback_map,
359 .unmap_flash_region = fallback_unmap,
360 .chip_readb = noop_chip_readb,
361 .chip_readw = fallback_chip_readw,
362 .chip_readl = fallback_chip_readl,
363 .chip_readn = fallback_chip_readn,
364 .chip_writeb = noop_chip_writeb,
365 .chip_writew = fallback_chip_writew,
366 .chip_writel = fallback_chip_writel,
367 .chip_writen = fallback_chip_writen,
368 .delay = internal_delay,
369 },
370#endif
371
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000372#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000373 {
374 .name = "dediprog",
375 .init = dediprog_init,
376 .shutdown = dediprog_shutdown,
377 .map_flash_region = fallback_map,
378 .unmap_flash_region = fallback_unmap,
379 .chip_readb = noop_chip_readb,
380 .chip_readw = fallback_chip_readw,
381 .chip_readl = fallback_chip_readl,
382 .chip_readn = fallback_chip_readn,
383 .chip_writeb = noop_chip_writeb,
384 .chip_writew = fallback_chip_writew,
385 .chip_writel = fallback_chip_writel,
386 .chip_writen = fallback_chip_writen,
387 .delay = internal_delay,
388 },
389#endif
390
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000391 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000392};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000393
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000394#define SHUTDOWN_MAXFN 4
395static int shutdown_fn_count = 0;
396struct shutdown_func_data {
397 void (*func) (void *data);
398 void *data;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000399} static shutdown_fn[SHUTDOWN_MAXFN];
400/* Initialize to 0 to make sure nobody registers a shutdown function before
401 * programmer init.
402 */
403static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000404
405/* Register a function to be executed on programmer shutdown.
406 * The advantage over atexit() is that you can supply a void pointer which will
407 * be used as parameter to the registered function upon programmer shutdown.
408 * This pointer can point to arbitrary data used by said function, e.g. undo
409 * information for GPIO settings etc. If unneeded, set data=NULL.
410 * Please note that the first (void *data) belongs to the function signature of
411 * the function passed as first parameter.
412 */
413int register_shutdown(void (*function) (void *data), void *data)
414{
415 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000416 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000417 SHUTDOWN_MAXFN);
418 return 1;
419 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000420 if (!may_register_shutdown) {
421 msg_perr("Tried to register a shutdown function before "
422 "programmer init.\n");
423 return 1;
424 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000425 shutdown_fn[shutdown_fn_count].func = function;
426 shutdown_fn[shutdown_fn_count].data = data;
427 shutdown_fn_count++;
428
429 return 0;
430}
431
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000432int programmer_init(char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000433{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000434 /* Initialize all programmer specific data. */
435 /* Default to unlimited decode sizes. */
436 max_rom_decode = (const struct decode_sizes) {
437 .parallel = 0xffffffff,
438 .lpc = 0xffffffff,
439 .fwh = 0xffffffff,
440 .spi = 0xffffffff
441 };
442 /* Default to Parallel/LPC/FWH flash devices. If a known host controller
443 * is found, the init routine sets the buses_supported bitfield.
444 */
445 buses_supported = CHIP_BUSTYPE_NONSPI;
446 /* Default to top aligned flash at 4 GB. */
447 flashbase = 0;
448 /* Registering shutdown functions is now allowed. */
449 may_register_shutdown = 1;
450
451 programmer_param = param;
452 msg_pdbg("Initializing %s programmer\n",
453 programmer_table[programmer].name);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000454 return programmer_table[programmer].init();
455}
456
457int programmer_shutdown(void)
458{
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000459 /* Registering shutdown functions is no longer allowed. */
460 may_register_shutdown = 0;
461 while (shutdown_fn_count > 0) {
462 int i = --shutdown_fn_count;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000463 shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000464 }
Uwe Hermann09e04f72009-05-16 22:36:00 +0000465 return programmer_table[programmer].shutdown();
466}
467
468void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
469 size_t len)
470{
471 return programmer_table[programmer].map_flash_region(descr,
472 phys_addr, len);
473}
474
475void programmer_unmap_flash_region(void *virt_addr, size_t len)
476{
477 programmer_table[programmer].unmap_flash_region(virt_addr, len);
478}
479
480void chip_writeb(uint8_t val, chipaddr addr)
481{
482 programmer_table[programmer].chip_writeb(val, addr);
483}
484
485void chip_writew(uint16_t val, chipaddr addr)
486{
487 programmer_table[programmer].chip_writew(val, addr);
488}
489
490void chip_writel(uint32_t val, chipaddr addr)
491{
492 programmer_table[programmer].chip_writel(val, addr);
493}
494
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000495void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
496{
497 programmer_table[programmer].chip_writen(buf, addr, len);
498}
499
Uwe Hermann09e04f72009-05-16 22:36:00 +0000500uint8_t chip_readb(const chipaddr addr)
501{
502 return programmer_table[programmer].chip_readb(addr);
503}
504
505uint16_t chip_readw(const chipaddr addr)
506{
507 return programmer_table[programmer].chip_readw(addr);
508}
509
510uint32_t chip_readl(const chipaddr addr)
511{
512 return programmer_table[programmer].chip_readl(addr);
513}
514
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000515void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
516{
517 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000518}
519
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000520void programmer_delay(int usecs)
521{
522 programmer_table[programmer].delay(usecs);
523}
524
Peter Stuge776d2022009-01-26 00:39:57 +0000525void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000526{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000527 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000528 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000529 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000530 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000531}
532
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000533int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000534{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000535 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000536
537 return 0;
538}
539
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000540int min(int a, int b)
541{
542 return (a < b) ? a : b;
543}
544
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000545int max(int a, int b)
546{
547 return (a > b) ? a : b;
548}
549
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000550int bitcount(unsigned long a)
551{
552 int i = 0;
553 for (; a != 0; a >>= 1)
554 if (a & 1)
555 i++;
556 return i;
557}
558
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000559char *strcat_realloc(char *dest, const char *src)
560{
561 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
562 if (!dest)
563 return NULL;
564 strcat(dest, src);
565 return dest;
566}
567
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000568/* This is a somewhat hacked function similar in some ways to strtok().
569 * It will look for needle in haystack, return a copy of needle and remove
570 * everything from the first occurrence of needle to the next delimiter
571 * from haystack.
572 */
573char *extract_param(char **haystack, char *needle, char *delim)
574{
575 char *param_pos, *rest, *tmp;
576 char *dev = NULL;
577 int devlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000578 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000579
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000580 needlelen = strlen(needle);
581 if (!needlelen) {
582 msg_gerr("%s: empty needle! Please report a bug at "
583 "flashrom@flashrom.org\n", __func__);
584 return NULL;
585 }
586 /* No programmer parameters given. */
587 if (*haystack == NULL)
588 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000589 param_pos = strstr(*haystack, needle);
590 do {
591 if (!param_pos)
592 return NULL;
593 /* Beginning of the string? */
594 if (param_pos == *haystack)
595 break;
596 /* After a delimiter? */
597 if (strchr(delim, *(param_pos - 1)))
598 break;
599 /* Continue searching. */
600 param_pos++;
601 param_pos = strstr(param_pos, needle);
602 } while (1);
603
604 if (param_pos) {
605 param_pos += strlen(needle);
606 devlen = strcspn(param_pos, delim);
607 if (devlen) {
608 dev = malloc(devlen + 1);
609 if (!dev) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000610 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000611 exit(1);
612 }
613 strncpy(dev, param_pos, devlen);
614 dev[devlen] = '\0';
615 }
616 rest = param_pos + devlen;
617 rest += strspn(rest, delim);
618 param_pos -= strlen(needle);
619 memmove(param_pos, rest, strlen(rest) + 1);
620 tmp = realloc(*haystack, strlen(*haystack) + 1);
621 if (!tmp) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000622 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000623 exit(1);
624 }
625 *haystack = tmp;
626 }
627
628
629 return dev;
630}
631
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000632/* start is an offset to the base address of the flash chip */
633int check_erased_range(struct flashchip *flash, int start, int len)
634{
635 int ret;
636 uint8_t *cmpbuf = malloc(len);
637
638 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000639 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000640 exit(1);
641 }
642 memset(cmpbuf, 0xff, len);
643 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
644 free(cmpbuf);
645 return ret;
646}
647
648/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000649 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
650 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000651 * @start offset to the base address of the flash chip
652 * @len length of the verified area
653 * @message string to print in the "FAILED" message
654 * @return 0 for success, -1 for failure
655 */
656int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
657{
658 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000659 int page_size = flash->page_size;
660 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000661 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000662
663 if (!len)
664 goto out_free;
665
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000666 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000667 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000668 return 1;
669 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000670 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000671 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000672 exit(1);
673 }
674
675 if (start + len > flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000676 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000677 " total_size 0x%x\n", __func__, start, len,
678 flash->total_size * 1024);
679 ret = -1;
680 goto out_free;
681 }
682 if (!message)
683 message = "VERIFY";
684
685 /* Warning: This loop has a very unusual condition and body.
686 * The loop needs to go through each page with at least one affected
687 * byte. The lowest page number is (start / page_size) since that
688 * division rounds down. The highest page number we want is the page
689 * where the last byte of the range lives. That last byte has the
690 * address (start + len - 1), thus the highest page number is
691 * (start + len - 1) / page_size. Since we want to include that last
692 * page as well, the loop condition uses <=.
693 */
694 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
695 /* Byte position of the first byte in the range in this page. */
696 starthere = max(start, i * page_size);
697 /* Length of bytes in the range in this page. */
698 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000699 flash->read(flash, readbuf, starthere, lenhere);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000700 for (j = 0; j < lenhere; j++) {
701 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000702 /* Only print the first failure. */
703 if (!failcount++)
Sean Nelson316a29f2010-05-07 20:09:04 +0000704 msg_cerr("%s FAILED at 0x%08x! "
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000705 "Expected=0x%02x, Read=0x%02x,",
706 message, starthere + j,
707 cmpbuf[starthere - start + j],
708 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000709 }
710 }
711 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000712 if (failcount) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000713 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000714 start, start + len - 1, failcount);
715 ret = -1;
716 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000717
718out_free:
719 free(readbuf);
720 return ret;
721}
722
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000723/**
724 * Check if the buffer @have can be programmed to the content of @want without
725 * erasing. This is only possible if all chunks of size @gran are either kept
726 * as-is or changed from an all-ones state to any other state.
727 * The following write granularities (enum @gran) are known:
728 * - 1 bit. Each bit can be cleared individually.
729 * - 1 byte. A byte can be written once. Further writes to an already written
730 * byte cause the contents to be either undefined or to stay unchanged.
731 * - 128 bytes. If less than 128 bytes are written, the rest will be
732 * erased. Each write to a 128-byte region will trigger an automatic erase
733 * before anything is written. Very uncommon behaviour and unsupported by
734 * this function.
735 * - 256 bytes. If less than 256 bytes are written, the contents of the
736 * unwritten bytes are undefined.
737 *
738 * @have buffer with current content
739 * @want buffer with desired content
740 * @len length of the verified area
741 * @gran write granularity (enum, not count)
742 * @return 0 if no erase is needed, 1 otherwise
743 */
744int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
745{
746 int result = 0;
747 int i, j, limit;
748
749 switch (gran) {
750 case write_gran_1bit:
751 for (i = 0; i < len; i++)
752 if ((have[i] & want[i]) != want[i]) {
753 result = 1;
754 break;
755 }
756 break;
757 case write_gran_1byte:
758 for (i = 0; i < len; i++)
759 if ((have[i] != want[i]) && (have[i] != 0xff)) {
760 result = 1;
761 break;
762 }
763 break;
764 case write_gran_256bytes:
765 for (j = 0; j < len / 256; j++) {
766 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000767 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000768 if (!memcmp(have + j * 256, want + j * 256, limit))
769 continue;
770 /* have needs to be in erased state. */
771 for (i = 0; i < limit; i++)
772 if (have[i] != 0xff) {
773 result = 1;
774 break;
775 }
776 if (result)
777 break;
778 }
779 break;
780 }
781 return result;
782}
783
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000784/* This function generates various test patterns useful for testing controller
785 * and chip communication as well as chip behaviour.
786 *
787 * If a byte can be written multiple times, each time keeping 0-bits at 0
788 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
789 * is essentially an AND operation. That's also the reason why this function
790 * provides the result of AND between various patterns.
791 *
792 * Below is a list of patterns (and their block length).
793 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
794 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
795 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
796 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
797 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
798 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
799 * Pattern 6 is 00 (1 Byte)
800 * Pattern 7 is ff (1 Byte)
801 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
802 * byte block.
803 *
804 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
805 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
806 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
807 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
808 * Pattern 12 is 00 (1 Byte)
809 * Pattern 13 is ff (1 Byte)
810 * Patterns 8-13 have no block number.
811 *
812 * Patterns 0-3 are created to detect and efficiently diagnose communication
813 * slips like missed bits or bytes and their repetitive nature gives good visual
814 * cues to the person inspecting the results. In addition, the following holds:
815 * AND Pattern 0/1 == Pattern 4
816 * AND Pattern 2/3 == Pattern 5
817 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
818 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
819 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
820 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
821 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
822 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
823 * Besides that, they provide for bit testing of the last two bytes of every
824 * 256 byte block which contains the block number for patterns 0-6.
825 * Patterns 10-11 are special purpose for detecting subblock aliasing with
826 * block sizes >256 bytes (some Dataflash chips etc.)
827 * AND Pattern 8/9 == Pattern 12
828 * AND Pattern 10/11 == Pattern 12
829 * Pattern 13 is the completely erased state.
830 * None of the patterns can detect aliasing at boundaries which are a multiple
831 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
832 */
833int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
834{
835 int i;
836
837 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000838 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000839 return 1;
840 }
841
842 switch (variant) {
843 case 0:
844 for (i = 0; i < size; i++)
845 buf[i] = (i & 0xf) << 4 | 0x5;
846 break;
847 case 1:
848 for (i = 0; i < size; i++)
849 buf[i] = (i & 0xf) << 4 | 0xa;
850 break;
851 case 2:
852 for (i = 0; i < size; i++)
853 buf[i] = 0x50 | (i & 0xf);
854 break;
855 case 3:
856 for (i = 0; i < size; i++)
857 buf[i] = 0xa0 | (i & 0xf);
858 break;
859 case 4:
860 for (i = 0; i < size; i++)
861 buf[i] = (i & 0xf) << 4;
862 break;
863 case 5:
864 for (i = 0; i < size; i++)
865 buf[i] = i & 0xf;
866 break;
867 case 6:
868 memset(buf, 0x00, size);
869 break;
870 case 7:
871 memset(buf, 0xff, size);
872 break;
873 case 8:
874 for (i = 0; i < size; i++)
875 buf[i] = i & 0xff;
876 break;
877 case 9:
878 for (i = 0; i < size; i++)
879 buf[i] = ~(i & 0xff);
880 break;
881 case 10:
882 for (i = 0; i < size % 2; i++) {
883 buf[i * 2] = (i >> 8) & 0xff;
884 buf[i * 2 + 1] = i & 0xff;
885 }
886 if (size & 0x1)
887 buf[i * 2] = (i >> 8) & 0xff;
888 break;
889 case 11:
890 for (i = 0; i < size % 2; i++) {
891 buf[i * 2] = ~((i >> 8) & 0xff);
892 buf[i * 2 + 1] = ~(i & 0xff);
893 }
894 if (size & 0x1)
895 buf[i * 2] = ~((i >> 8) & 0xff);
896 break;
897 case 12:
898 memset(buf, 0x00, size);
899 break;
900 case 13:
901 memset(buf, 0xff, size);
902 break;
903 }
904
905 if ((variant >= 0) && (variant <= 7)) {
906 /* Write block number in the last two bytes of each 256-byte
907 * block, big endian for easier reading of the hexdump.
908 * Note that this wraps around for chips larger than 2^24 bytes
909 * (16 MB).
910 */
911 for (i = 0; i < size / 256; i++) {
912 buf[i * 256 + 254] = (i >> 8) & 0xff;
913 buf[i * 256 + 255] = i & 0xff;
914 }
915 }
916
917 return 0;
918}
919
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000920int check_max_decode(enum chipbustype buses, uint32_t size)
921{
922 int limitexceeded = 0;
923 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
924 (max_rom_decode.parallel < size)) {
925 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000926 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000927 "size %u kB of chipset/board/programmer "
928 "for %s interface, "
929 "probe/read/erase/write may fail. ", size / 1024,
930 max_rom_decode.parallel / 1024, "Parallel");
931 }
932 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
933 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000934 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000935 "size %u kB of chipset/board/programmer "
936 "for %s interface, "
937 "probe/read/erase/write may fail. ", size / 1024,
938 max_rom_decode.lpc / 1024, "LPC");
939 }
940 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
941 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000942 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000943 "size %u kB of chipset/board/programmer "
944 "for %s interface, "
945 "probe/read/erase/write may fail. ", size / 1024,
946 max_rom_decode.fwh / 1024, "FWH");
947 }
948 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
949 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000950 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000951 "size %u kB of chipset/board/programmer "
952 "for %s interface, "
953 "probe/read/erase/write may fail. ", size / 1024,
954 max_rom_decode.spi / 1024, "SPI");
955 }
956 if (!limitexceeded)
957 return 0;
958 /* Sometimes chip and programmer have more than one bus in common,
959 * and the limit is not exceeded on all buses. Tell the user.
960 */
961 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000962 /* FIXME: This message is designed towards CLI users. */
Sean Nelson316a29f2010-05-07 20:09:04 +0000963 msg_pdbg("There is at least one common chip/programmer "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000964 "interface which can support a chip of this size. "
965 "You can try --force at your own risk.\n");
966 return 1;
967}
968
Peter Stuge483b8f02008-09-03 23:10:05 +0000969struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000970{
Peter Stuge483b8f02008-09-03 23:10:05 +0000971 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000972 unsigned long base = 0;
973 uint32_t size;
974 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000975 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000976
Peter Stuge483b8f02008-09-03 23:10:05 +0000977 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000978 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000979 continue;
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000980 msg_gdbg("Probing for %s %s, %d KB: ",
Stefan Reinauerac378972008-03-17 22:59:40 +0000981 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000982 if (!flash->probe && !force) {
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000983 msg_gdbg("failed! flashrom has no probe function for "
984 "this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000985 continue;
986 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000987 buses_common = buses_supported & flash->bustype;
988 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000989 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000990 msg_gdbg("skipped.");
991 msg_gspew(" Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000992 free(tmp);
993 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000994 msg_gspew("and chip bus type %s are incompatible.",
995 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000996 free(tmp);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000997 msg_gdbg("\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000998 continue;
999 }
Stefan Reinauer70385642007-04-06 11:58:03 +00001000
Ollie Lhocbbf1252004-03-17 22:22:08 +00001001 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001002 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +00001003
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +00001004 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +00001005 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001006
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001007 if (force)
1008 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001009
Peter Stuge483b8f02008-09-03 23:10:05 +00001010 if (flash->probe(flash) != 1)
1011 goto notfound;
1012
Uwe Hermann394131e2008-10-18 21:14:13 +00001013 if (first_flash == flashchips
1014 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001015 break;
1016
Peter Stuge483b8f02008-09-03 23:10:05 +00001017notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +00001018 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001019 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001020
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001021 if (!flash || !flash->name)
1022 return NULL;
1023
Sean Nelson316a29f2010-05-07 20:09:04 +00001024 msg_cinfo("%s chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +00001025 force ? "Assuming" : "Found",
Uwe Hermann9899cad2009-06-28 21:47:57 +00001026 flash->vendor, flash->name, flash->total_size,
1027 flashbuses_to_text(flash->bustype), base);
1028
Sean Nelson6e0b9122010-02-19 00:52:10 +00001029 if (flash->printlock)
1030 flash->printlock(flash);
1031
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001032 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001033}
1034
Stefan Reinauere3705282005-12-18 16:41:10 +00001035int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001036{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001037 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +00001038 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001039
Sean Nelson316a29f2010-05-07 20:09:04 +00001040 msg_cinfo("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +00001041
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001042 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001043
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001044 if (!ret)
Sean Nelson316a29f2010-05-07 20:09:04 +00001045 msg_cinfo("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001046
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001047 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001048}
1049
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +00001050int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001051{
1052 unsigned long numbytes;
1053 FILE *image;
1054 unsigned long size = flash->total_size * 1024;
1055 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001056
1057 if (!filename) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001058 msg_gerr("Error: No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001059 return 1;
1060 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001061 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001062 perror(filename);
1063 exit(1);
1064 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001065 msg_cinfo("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001066 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001067 msg_cinfo("FAILED!\n");
1068 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001069 return 1;
1070 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +00001071 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001072
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001073 numbytes = fwrite(buf, 1, size, image);
1074 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +00001075 free(buf);
Sean Nelson316a29f2010-05-07 20:09:04 +00001076 msg_cinfo("%s.\n", numbytes == size ? "done" : "FAILED");
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001077 if (numbytes != size)
1078 return 1;
1079 return 0;
1080}
1081
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001082/* This function shares a lot of its structure with erase_flash().
1083 * Even if an error is found, the function will keep going and check the rest.
1084 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +00001085static int selfcheck_eraseblocks(struct flashchip *flash)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001086{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001087 int i, j, k;
1088 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001089
1090 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1091 unsigned int done = 0;
1092 struct block_eraser eraser = flash->block_erasers[k];
1093
1094 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1095 /* Blocks with zero size are bugs in flashchips.c. */
1096 if (eraser.eraseblocks[i].count &&
1097 !eraser.eraseblocks[i].size) {
1098 msg_gerr("ERROR: Flash chip %s erase function "
1099 "%i region %i has size 0. Please report"
1100 " a bug at flashrom@flashrom.org\n",
1101 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001102 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001103 }
1104 /* Blocks with zero count are bugs in flashchips.c. */
1105 if (!eraser.eraseblocks[i].count &&
1106 eraser.eraseblocks[i].size) {
1107 msg_gerr("ERROR: Flash chip %s erase function "
1108 "%i region %i has count 0. Please report"
1109 " a bug at flashrom@flashrom.org\n",
1110 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001111 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001112 }
1113 done += eraser.eraseblocks[i].count *
1114 eraser.eraseblocks[i].size;
1115 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001116 /* Empty eraseblock definition with erase function. */
1117 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001118 msg_gspew("Strange: Empty eraseblock definition with "
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001119 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001120 if (!done)
1121 continue;
1122 if (done != flash->total_size * 1024) {
1123 msg_gerr("ERROR: Flash chip %s erase function %i "
1124 "region walking resulted in 0x%06x bytes total,"
1125 " expected 0x%06x bytes. Please report a bug at"
1126 " flashrom@flashrom.org\n", flash->name, k,
1127 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001128 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001129 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001130 if (!eraser.block_erase)
1131 continue;
1132 /* Check if there are identical erase functions for different
1133 * layouts. That would imply "magic" erase functions. The
1134 * easiest way to check this is with function pointers.
1135 */
Uwe Hermann43959702010-03-13 17:28:29 +00001136 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001137 if (eraser.block_erase ==
1138 flash->block_erasers[j].block_erase) {
1139 msg_gerr("ERROR: Flash chip %s erase function "
1140 "%i and %i are identical. Please report"
1141 " a bug at flashrom@flashrom.org\n",
1142 flash->name, k, j);
1143 ret = 1;
1144 }
Uwe Hermann43959702010-03-13 17:28:29 +00001145 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001146 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001147 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001148}
1149
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001150int erase_flash(struct flashchip *flash)
1151{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001152 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001153 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001154
Sean Nelson316a29f2010-05-07 20:09:04 +00001155 msg_cinfo("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001156 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001157 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001158 struct block_eraser eraser = flash->block_erasers[k];
1159
Sean Nelson316a29f2010-05-07 20:09:04 +00001160 msg_cdbg("Looking at blockwise erase function %i... ", k);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001161 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001162 msg_cdbg("not defined. "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001163 "Looking for another erase function.\n");
1164 continue;
1165 }
1166 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001167 msg_cdbg("eraseblock layout is known, but no "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001168 "matching block erase function found. "
1169 "Looking for another erase function.\n");
1170 continue;
1171 }
1172 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001173 msg_cdbg("block erase function found, but "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001174 "eraseblock layout is unknown. "
1175 "Looking for another erase function.\n");
1176 continue;
1177 }
1178 found = 1;
Sean Nelson316a29f2010-05-07 20:09:04 +00001179 msg_cdbg("trying... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001180 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1181 /* count==0 for all automatically initialized array
1182 * members so the loop below won't be executed for them.
1183 */
1184 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001185 start = done + eraser.eraseblocks[i].size * j;
1186 len = eraser.eraseblocks[i].size;
Sean Nelson316a29f2010-05-07 20:09:04 +00001187 msg_cdbg("0x%06x-0x%06x, ", start,
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001188 start + len - 1);
1189 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001190 if (ret)
1191 break;
1192 }
1193 if (ret)
1194 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001195 done += eraser.eraseblocks[i].count *
1196 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001197 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001198 msg_cdbg("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001199 /* If everything is OK, don't try another erase function. */
1200 if (!ret)
1201 break;
1202 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001203 if (!found) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001204 msg_cerr("ERROR: flashrom has no erase function for this flash chip.\n");
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001205 return 1;
1206 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001207
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001208 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001209 msg_cerr("FAILED!\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001210 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001211 msg_cinfo("SUCCESS.\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001212 }
1213 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001214}
1215
Uwe Hermannc67d0372009-10-01 18:40:02 +00001216void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001217{
Sean Nelson316a29f2010-05-07 20:09:04 +00001218 msg_gerr("Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001219 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001220 "mail flashrom@flashrom.org!\n"
1221 "-------------------------------------------------------------"
1222 "------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001223 "DO NOT REBOOT OR POWEROFF!\n");
1224}
1225
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001226/* The way to go if you want a delimited list of programmers*/
1227void list_programmers(char *delim)
1228{
1229 enum programmer p;
1230 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001231 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001232 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001233 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001234 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001235 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001236}
1237
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001238void print_sysinfo(void)
1239{
1240#if HAVE_UTSNAME == 1
1241 struct utsname osinfo;
1242 uname(&osinfo);
1243
1244 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1245 osinfo.machine);
1246#else
1247 msg_ginfo(" on unknown machine");
1248#endif
1249 msg_ginfo(", built with");
1250#if NEED_PCI == 1
1251#ifdef PCILIB_VERSION
1252 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1253#else
1254 msg_ginfo(" unknown PCI library,");
1255#endif
1256#endif
1257#ifdef __clang__
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001258 msg_ginfo(" LLVM %i/clang %i, ", __llvm__, __clang__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001259#elif defined(__GNUC__)
1260 msg_ginfo(" GCC");
1261#ifdef __VERSION__
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001262 msg_ginfo(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001263#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001264 msg_ginfo(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001265#endif
1266#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001267 msg_ginfo(" unknown compiler,");
1268#endif
1269#if defined (__FLASHROM_LITTLE_ENDIAN__)
1270 msg_ginfo(" little endian");
1271#else
1272 msg_ginfo(" big endian");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001273#endif
1274 msg_ginfo("\n");
1275}
1276
Bernhard Walle201bde32008-01-21 15:24:22 +00001277void print_version(void)
1278{
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001279 msg_ginfo("flashrom v%s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001280 print_sysinfo();
Bernhard Walle201bde32008-01-21 15:24:22 +00001281}
1282
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001283void print_banner(void)
1284{
1285 msg_ginfo("flashrom is free software, get the source code at "
1286 "http://www.flashrom.org\n");
1287 msg_ginfo("\n");
1288}
1289
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001290int selfcheck(void)
1291{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001292 int ret = 0;
1293 struct flashchip *flash;
1294
1295 /* Safety check. Instead of aborting after the first error, check
1296 * if more errors exist.
1297 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001298 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001299 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001300 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001301 }
1302 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001303 msg_gerr("SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001304 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001305 }
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001306#if CONFIG_BITBANG_SPI == 1
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001307 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001308 msg_gerr("Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001309 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001310 }
1311#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001312 for (flash = flashchips; flash && flash->name; flash++)
1313 if (selfcheck_eraseblocks(flash))
1314 ret = 1;
1315 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001316}
1317
1318void check_chip_supported(struct flashchip *flash)
1319{
1320 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001321 msg_cinfo("===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001322 if (flash->tested & TEST_BAD_MASK) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001323 msg_cinfo("This flash part has status NOT WORKING for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001324 if (flash->tested & TEST_BAD_PROBE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001325 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001326 if (flash->tested & TEST_BAD_READ)
Sean Nelson316a29f2010-05-07 20:09:04 +00001327 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001328 if (flash->tested & TEST_BAD_ERASE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001329 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001330 if (flash->tested & TEST_BAD_WRITE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001331 msg_cinfo(" WRITE");
1332 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001333 }
1334 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1335 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1336 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1337 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001338 msg_cinfo("This flash part has status UNTESTED for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001339 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001340 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001341 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
Sean Nelson316a29f2010-05-07 20:09:04 +00001342 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001343 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001344 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001345 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001346 msg_cinfo(" WRITE");
1347 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001348 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001349 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001350 msg_cinfo("The test status of this chip may have been updated "
1351 "in the latest development\n"
1352 "version of flashrom. If you are running the latest "
1353 "development version,\n"
1354 "please email a report to flashrom@flashrom.org if "
1355 "any of the above operations\n"
1356 "work correctly for you with this flash part. Please "
1357 "include the flashrom\n"
1358 "output with the additional -V option for all "
1359 "operations you tested (-V, -Vr,\n"
1360 "-Vw, -VE), and mention which mainboard or "
1361 "programmer you tested.\n"
1362 "Thanks for your help!\n"
1363 "===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001364 }
1365}
1366
Ollie Lho761bf1b2004-03-20 16:46:10 +00001367int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001368{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001369 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001370}
1371
1372/* This function signature is horrible. We need to design a better interface,
1373 * but right now it allows us to split off the CLI code.
1374 */
1375int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1376{
1377 uint8_t *buf;
1378 unsigned long numbytes;
1379 FILE *image;
1380 int ret = 0;
1381 unsigned long size;
1382
1383 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001384 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001385
Ollie Lhoefa28582004-12-08 20:10:01 +00001386 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001387 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001388 msg_cerr("Erase is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001389 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001390 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001391 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001392 return 1;
1393 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001394 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001395 }
1396 }
Sean Nelson6e0b9122010-02-19 00:52:10 +00001397 if (flash->unlock)
1398 flash->unlock(flash);
1399
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001400 if (erase_flash(flash)) {
1401 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001402 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001403 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001404 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001405 } else if (read_it) {
Sean Nelson6e0b9122010-02-19 00:52:10 +00001406 if (flash->unlock)
1407 flash->unlock(flash);
1408
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001409 if (read_flash(flash, filename)) {
1410 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001411 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001412 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001413 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001414 struct stat image_stat;
1415
Sean Nelson6e0b9122010-02-19 00:52:10 +00001416 if (flash->unlock)
1417 flash->unlock(flash);
1418
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001419 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001420 msg_cerr("Erase is not working on this chip "
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001421 "and erase is needed for write. ");
1422 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001423 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001424 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001425 return 1;
1426 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001427 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001428 }
1429 }
1430 if (flash->tested & TEST_BAD_WRITE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001431 msg_cerr("Write is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001432 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001433 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001434 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001435 return 1;
1436 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001437 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001438 }
1439 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001440 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001441 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001442 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001443 exit(1);
1444 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001445 if (fstat(fileno(image), &image_stat) != 0) {
1446 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001447 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001448 exit(1);
1449 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001450 if (image_stat.st_size != flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001451 msg_gerr("Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001452 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001453 exit(1);
1454 }
1455
Peter Stuge1fec0f32009-01-12 21:00:35 +00001456 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001457#if CONFIG_INTERNAL == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001458 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001459#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001460 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001461 if (numbytes != size) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001462 msg_gerr("Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001463 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001464 return 1;
1465 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001466 }
1467
Ollie Lho184a4042005-11-26 21:55:36 +00001468 // This should be moved into each flash part's code to do it
1469 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001470 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001471
Ollie Lho184a4042005-11-26 21:55:36 +00001472 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001473
Peter Stugef31104c2008-04-28 14:47:30 +00001474 if (write_it) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001475 msg_cinfo("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001476 if (!flash->write) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001477 msg_cerr("Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001478 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001479 return 1;
1480 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001481 ret = flash->write(flash, buf);
1482 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001483 msg_cerr("FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001484 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001485 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001486 return 1;
1487 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001488 msg_cinfo("COMPLETE.\n");
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001489 }
Peter Stugef31104c2008-04-28 14:47:30 +00001490 }
Ollie Lho184a4042005-11-26 21:55:36 +00001491
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001492 if (verify_it) {
1493 /* Work around chips which need some time to calm down. */
1494 if (write_it)
1495 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001496 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001497 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001498 * might have an emergency situation.
1499 */
1500 if (ret && write_it)
1501 emergency_help_message();
1502 }
Ollie Lho184a4042005-11-26 21:55:36 +00001503
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001504 programmer_shutdown();
1505
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001506 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001507}