blob: 8a9745493da04eaaf9c0ff5e54a4d4f81e89f053 [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 */
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000051#if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_FT2232_SPI+CONFIG_SERPROG+CONFIG_BUSPIRATE_SPI+CONFIG_DEDIPROG > 1
52#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
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000062#if CONFIG_GFXNVIDIA == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000063 PROGRAMMER_GFXNVIDIA
64#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000065#if CONFIG_DRKAISER == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000066 PROGRAMMER_DRKAISER
67#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000068#if CONFIG_SATASII == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000069 PROGRAMMER_SATASII
70#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000071#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +000072 PROGRAMMER_ATAHPT
73#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000074#if CONFIG_FT2232_SPI == 1
75 PROGRAMMER_FT2232_SPI
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000076#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000077#if CONFIG_SERPROG == 1
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000078 PROGRAMMER_SERPROG
79#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000080#if CONFIG_BUSPIRATE_SPI == 1
81 PROGRAMMER_BUSPIRATE_SPI
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000082#endif
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000083#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +000084 PROGRAMMER_DEDIPROG
85#endif
Carl-Daniel Hailfinger1a1415c2010-01-10 13:28:48 +000086;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000087#endif
88
Carl-Daniel Hailfingeref58a9c2009-08-12 13:32:56 +000089char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000090
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000091/**
92 * flashrom defaults to Parallel/LPC/FWH flash devices. If a known host
93 * controller is found, the init routine sets the buses_supported bitfield to
94 * contain the supported buses for that controller.
95 */
96enum chipbustype buses_supported = CHIP_BUSTYPE_NONSPI;
97
98/**
99 * Programmers supporting multiple buses can have differing size limits on
100 * each bus. Store the limits for each bus in a common struct.
101 */
102struct decode_sizes max_rom_decode = {
103 .parallel = 0xffffffff,
104 .lpc = 0xffffffff,
105 .fwh = 0xffffffff,
106 .spi = 0xffffffff
107};
108
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000109const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000110#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000111 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000112 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000113 .init = internal_init,
114 .shutdown = internal_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000115 .map_flash_region = physmap,
116 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000117 .chip_readb = internal_chip_readb,
118 .chip_readw = internal_chip_readw,
119 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000120 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000121 .chip_writeb = internal_chip_writeb,
122 .chip_writew = internal_chip_writew,
123 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000124 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000125 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000126 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000127#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000128
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000129#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000130 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000131 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000132 .init = dummy_init,
133 .shutdown = dummy_shutdown,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +0000134 .map_flash_region = dummy_map,
135 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000136 .chip_readb = dummy_chip_readb,
137 .chip_readw = dummy_chip_readw,
138 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000139 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +0000140 .chip_writeb = dummy_chip_writeb,
141 .chip_writew = dummy_chip_writew,
142 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000143 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000144 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000145 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000146#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000147
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000148#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000149 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000150 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000151 .init = nic3com_init,
152 .shutdown = nic3com_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000153 .map_flash_region = fallback_map,
154 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000155 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000156 .chip_readw = fallback_chip_readw,
157 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000158 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000159 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000160 .chip_writew = fallback_chip_writew,
161 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000162 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000163 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000164 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000165#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000166
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000167#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000168 {
169 .name = "nicrealtek",
170 .init = nicrealtek_init,
171 .shutdown = nicrealtek_shutdown,
172 .map_flash_region = fallback_map,
173 .unmap_flash_region = fallback_unmap,
174 .chip_readb = nicrealtek_chip_readb,
175 .chip_readw = fallback_chip_readw,
176 .chip_readl = fallback_chip_readl,
177 .chip_readn = fallback_chip_readn,
178 .chip_writeb = nicrealtek_chip_writeb,
179 .chip_writew = fallback_chip_writew,
180 .chip_writel = fallback_chip_writel,
181 .chip_writen = fallback_chip_writen,
182 .delay = internal_delay,
183 },
184 {
185 .name = "nicsmc1211",
186 .init = nicsmc1211_init,
187 .shutdown = nicrealtek_shutdown,
188 .map_flash_region = fallback_map,
189 .unmap_flash_region = fallback_unmap,
190 .chip_readb = nicrealtek_chip_readb,
191 .chip_readw = fallback_chip_readw,
192 .chip_readl = fallback_chip_readl,
193 .chip_readn = fallback_chip_readn,
194 .chip_writeb = nicrealtek_chip_writeb,
195 .chip_writew = fallback_chip_writew,
196 .chip_writel = fallback_chip_writel,
197 .chip_writen = fallback_chip_writen,
198 .delay = internal_delay,
199 },
200#endif
201
202
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000203#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000204 {
205 .name = "gfxnvidia",
206 .init = gfxnvidia_init,
207 .shutdown = gfxnvidia_shutdown,
208 .map_flash_region = fallback_map,
209 .unmap_flash_region = fallback_unmap,
210 .chip_readb = gfxnvidia_chip_readb,
211 .chip_readw = fallback_chip_readw,
212 .chip_readl = fallback_chip_readl,
213 .chip_readn = fallback_chip_readn,
214 .chip_writeb = gfxnvidia_chip_writeb,
215 .chip_writew = fallback_chip_writew,
216 .chip_writel = fallback_chip_writel,
217 .chip_writen = fallback_chip_writen,
218 .delay = internal_delay,
219 },
220#endif
221
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000222#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000223 {
TURBO Jb0912c02009-09-02 23:00:46 +0000224 .name = "drkaiser",
225 .init = drkaiser_init,
226 .shutdown = drkaiser_shutdown,
227 .map_flash_region = fallback_map,
228 .unmap_flash_region = fallback_unmap,
229 .chip_readb = drkaiser_chip_readb,
230 .chip_readw = fallback_chip_readw,
231 .chip_readl = fallback_chip_readl,
232 .chip_readn = fallback_chip_readn,
233 .chip_writeb = drkaiser_chip_writeb,
234 .chip_writew = fallback_chip_writew,
235 .chip_writel = fallback_chip_writel,
236 .chip_writen = fallback_chip_writen,
237 .delay = internal_delay,
238 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000239#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000240
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000241#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000242 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000243 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000244 .init = satasii_init,
245 .shutdown = satasii_shutdown,
Uwe Hermannc6915932009-05-17 23:12:17 +0000246 .map_flash_region = fallback_map,
247 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000248 .chip_readb = satasii_chip_readb,
249 .chip_readw = fallback_chip_readw,
250 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000251 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000252 .chip_writeb = satasii_chip_writeb,
253 .chip_writew = fallback_chip_writew,
254 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000255 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000256 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000257 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000258#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000259
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000260#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000261 {
262 .name = "atahpt",
263 .init = atahpt_init,
264 .shutdown = atahpt_shutdown,
265 .map_flash_region = fallback_map,
266 .unmap_flash_region = fallback_unmap,
267 .chip_readb = atahpt_chip_readb,
268 .chip_readw = fallback_chip_readw,
269 .chip_readl = fallback_chip_readl,
270 .chip_readn = fallback_chip_readn,
271 .chip_writeb = atahpt_chip_writeb,
272 .chip_writew = fallback_chip_writew,
273 .chip_writel = fallback_chip_writel,
274 .chip_writen = fallback_chip_writen,
275 .delay = internal_delay,
276 },
277#endif
278
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000279#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000280#if defined(__i386__) || defined(__x86_64__)
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000281 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000282 .name = "it87spi",
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000283 .init = it87spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000284 .shutdown = noop_shutdown,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000285 .map_flash_region = fallback_map,
286 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000287 .chip_readb = noop_chip_readb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000288 .chip_readw = fallback_chip_readw,
289 .chip_readl = fallback_chip_readl,
290 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000291 .chip_writeb = noop_chip_writeb,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000292 .chip_writew = fallback_chip_writew,
293 .chip_writel = fallback_chip_writel,
294 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000295 .delay = internal_delay,
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000296 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000297#endif
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +0000298#endif
Carl-Daniel Hailfingerb8afecd2009-05-31 18:00:57 +0000299
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000300#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000301 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000302 .name = "ft2232_spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000303 .init = ft2232_spi_init,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000304 .shutdown = noop_shutdown, /* Missing shutdown */
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000305 .map_flash_region = fallback_map,
306 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000307 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000308 .chip_readw = fallback_chip_readw,
309 .chip_readl = fallback_chip_readl,
310 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000311 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000312 .chip_writew = fallback_chip_writew,
313 .chip_writel = fallback_chip_writel,
314 .chip_writen = fallback_chip_writen,
315 .delay = internal_delay,
316 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000317#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000318
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000319#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000320 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000321 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000322 .init = serprog_init,
323 .shutdown = serprog_shutdown,
324 .map_flash_region = fallback_map,
325 .unmap_flash_region = fallback_unmap,
326 .chip_readb = serprog_chip_readb,
327 .chip_readw = fallback_chip_readw,
328 .chip_readl = fallback_chip_readl,
329 .chip_readn = serprog_chip_readn,
330 .chip_writeb = serprog_chip_writeb,
331 .chip_writew = fallback_chip_writew,
332 .chip_writel = fallback_chip_writel,
333 .chip_writen = fallback_chip_writen,
334 .delay = serprog_delay,
335 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000336#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000337
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000338#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000339 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000340 .name = "buspirate_spi",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000341 .init = buspirate_spi_init,
342 .shutdown = buspirate_spi_shutdown,
343 .map_flash_region = fallback_map,
344 .unmap_flash_region = fallback_unmap,
345 .chip_readb = noop_chip_readb,
346 .chip_readw = fallback_chip_readw,
347 .chip_readl = fallback_chip_readl,
348 .chip_readn = fallback_chip_readn,
349 .chip_writeb = noop_chip_writeb,
350 .chip_writew = fallback_chip_writew,
351 .chip_writel = fallback_chip_writel,
352 .chip_writen = fallback_chip_writen,
353 .delay = internal_delay,
354 },
355#endif
356
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000357#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000358 {
359 .name = "dediprog",
360 .init = dediprog_init,
361 .shutdown = dediprog_shutdown,
362 .map_flash_region = fallback_map,
363 .unmap_flash_region = fallback_unmap,
364 .chip_readb = noop_chip_readb,
365 .chip_readw = fallback_chip_readw,
366 .chip_readl = fallback_chip_readl,
367 .chip_readn = fallback_chip_readn,
368 .chip_writeb = noop_chip_writeb,
369 .chip_writew = fallback_chip_writew,
370 .chip_writel = fallback_chip_writel,
371 .chip_writen = fallback_chip_writen,
372 .delay = internal_delay,
373 },
374#endif
375
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000376 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000377};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000378
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000379#define SHUTDOWN_MAXFN 4
380static int shutdown_fn_count = 0;
381struct shutdown_func_data {
382 void (*func) (void *data);
383 void *data;
384} shutdown_fn[SHUTDOWN_MAXFN];
385
386/* Register a function to be executed on programmer shutdown.
387 * The advantage over atexit() is that you can supply a void pointer which will
388 * be used as parameter to the registered function upon programmer shutdown.
389 * This pointer can point to arbitrary data used by said function, e.g. undo
390 * information for GPIO settings etc. If unneeded, set data=NULL.
391 * Please note that the first (void *data) belongs to the function signature of
392 * the function passed as first parameter.
393 */
394int register_shutdown(void (*function) (void *data), void *data)
395{
396 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
397 msg_perr("Tried to register more than %n shutdown functions.\n",
398 SHUTDOWN_MAXFN);
399 return 1;
400 }
401 shutdown_fn[shutdown_fn_count].func = function;
402 shutdown_fn[shutdown_fn_count].data = data;
403 shutdown_fn_count++;
404
405 return 0;
406}
407
Uwe Hermann09e04f72009-05-16 22:36:00 +0000408int programmer_init(void)
409{
410 return programmer_table[programmer].init();
411}
412
413int programmer_shutdown(void)
414{
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000415 int i;
416
417 for (i = shutdown_fn_count - 1; i >= 0; i--)
418 shutdown_fn[i].func(shutdown_fn[i].data);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000419 return programmer_table[programmer].shutdown();
420}
421
422void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
423 size_t len)
424{
425 return programmer_table[programmer].map_flash_region(descr,
426 phys_addr, len);
427}
428
429void programmer_unmap_flash_region(void *virt_addr, size_t len)
430{
431 programmer_table[programmer].unmap_flash_region(virt_addr, len);
432}
433
434void chip_writeb(uint8_t val, chipaddr addr)
435{
436 programmer_table[programmer].chip_writeb(val, addr);
437}
438
439void chip_writew(uint16_t val, chipaddr addr)
440{
441 programmer_table[programmer].chip_writew(val, addr);
442}
443
444void chip_writel(uint32_t val, chipaddr addr)
445{
446 programmer_table[programmer].chip_writel(val, addr);
447}
448
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000449void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
450{
451 programmer_table[programmer].chip_writen(buf, addr, len);
452}
453
Uwe Hermann09e04f72009-05-16 22:36:00 +0000454uint8_t chip_readb(const chipaddr addr)
455{
456 return programmer_table[programmer].chip_readb(addr);
457}
458
459uint16_t chip_readw(const chipaddr addr)
460{
461 return programmer_table[programmer].chip_readw(addr);
462}
463
464uint32_t chip_readl(const chipaddr addr)
465{
466 return programmer_table[programmer].chip_readl(addr);
467}
468
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000469void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
470{
471 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000472}
473
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000474void programmer_delay(int usecs)
475{
476 programmer_table[programmer].delay(usecs);
477}
478
Peter Stuge776d2022009-01-26 00:39:57 +0000479void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000480{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000481 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000482 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000483 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000484 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000485}
486
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000487int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000488{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000489 chip_readn(buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000490
491 return 0;
492}
493
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +0000494unsigned long flashbase = 0;
495
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000496int min(int a, int b)
497{
498 return (a < b) ? a : b;
499}
500
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000501int max(int a, int b)
502{
503 return (a > b) ? a : b;
504}
505
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000506int bitcount(unsigned long a)
507{
508 int i = 0;
509 for (; a != 0; a >>= 1)
510 if (a & 1)
511 i++;
512 return i;
513}
514
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000515char *strcat_realloc(char *dest, const char *src)
516{
517 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
518 if (!dest)
519 return NULL;
520 strcat(dest, src);
521 return dest;
522}
523
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000524/* This is a somewhat hacked function similar in some ways to strtok().
525 * It will look for needle in haystack, return a copy of needle and remove
526 * everything from the first occurrence of needle to the next delimiter
527 * from haystack.
528 */
529char *extract_param(char **haystack, char *needle, char *delim)
530{
531 char *param_pos, *rest, *tmp;
532 char *dev = NULL;
533 int devlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000534 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000535
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000536 needlelen = strlen(needle);
537 if (!needlelen) {
538 msg_gerr("%s: empty needle! Please report a bug at "
539 "flashrom@flashrom.org\n", __func__);
540 return NULL;
541 }
542 /* No programmer parameters given. */
543 if (*haystack == NULL)
544 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000545 param_pos = strstr(*haystack, needle);
546 do {
547 if (!param_pos)
548 return NULL;
549 /* Beginning of the string? */
550 if (param_pos == *haystack)
551 break;
552 /* After a delimiter? */
553 if (strchr(delim, *(param_pos - 1)))
554 break;
555 /* Continue searching. */
556 param_pos++;
557 param_pos = strstr(param_pos, needle);
558 } while (1);
559
560 if (param_pos) {
561 param_pos += strlen(needle);
562 devlen = strcspn(param_pos, delim);
563 if (devlen) {
564 dev = malloc(devlen + 1);
565 if (!dev) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000566 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000567 exit(1);
568 }
569 strncpy(dev, param_pos, devlen);
570 dev[devlen] = '\0';
571 }
572 rest = param_pos + devlen;
573 rest += strspn(rest, delim);
574 param_pos -= strlen(needle);
575 memmove(param_pos, rest, strlen(rest) + 1);
576 tmp = realloc(*haystack, strlen(*haystack) + 1);
577 if (!tmp) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000578 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000579 exit(1);
580 }
581 *haystack = tmp;
582 }
583
584
585 return dev;
586}
587
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000588/* start is an offset to the base address of the flash chip */
589int check_erased_range(struct flashchip *flash, int start, int len)
590{
591 int ret;
592 uint8_t *cmpbuf = malloc(len);
593
594 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000595 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000596 exit(1);
597 }
598 memset(cmpbuf, 0xff, len);
599 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
600 free(cmpbuf);
601 return ret;
602}
603
604/**
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000605 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
606 flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000607 * @start offset to the base address of the flash chip
608 * @len length of the verified area
609 * @message string to print in the "FAILED" message
610 * @return 0 for success, -1 for failure
611 */
612int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
613{
614 int i, j, starthere, lenhere, ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000615 int page_size = flash->page_size;
616 uint8_t *readbuf = malloc(page_size);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000617 int failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000618
619 if (!len)
620 goto out_free;
621
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000622 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000623 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000624 return 1;
625 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000626 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000627 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000628 exit(1);
629 }
630
631 if (start + len > flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000632 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000633 " total_size 0x%x\n", __func__, start, len,
634 flash->total_size * 1024);
635 ret = -1;
636 goto out_free;
637 }
638 if (!message)
639 message = "VERIFY";
640
641 /* Warning: This loop has a very unusual condition and body.
642 * The loop needs to go through each page with at least one affected
643 * byte. The lowest page number is (start / page_size) since that
644 * division rounds down. The highest page number we want is the page
645 * where the last byte of the range lives. That last byte has the
646 * address (start + len - 1), thus the highest page number is
647 * (start + len - 1) / page_size. Since we want to include that last
648 * page as well, the loop condition uses <=.
649 */
650 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
651 /* Byte position of the first byte in the range in this page. */
652 starthere = max(start, i * page_size);
653 /* Length of bytes in the range in this page. */
654 lenhere = min(start + len, (i + 1) * page_size) - starthere;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000655 flash->read(flash, readbuf, starthere, lenhere);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000656 for (j = 0; j < lenhere; j++) {
657 if (cmpbuf[starthere - start + j] != readbuf[j]) {
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000658 /* Only print the first failure. */
659 if (!failcount++)
Sean Nelson316a29f2010-05-07 20:09:04 +0000660 msg_cerr("%s FAILED at 0x%08x! "
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000661 "Expected=0x%02x, Read=0x%02x,",
662 message, starthere + j,
663 cmpbuf[starthere - start + j],
664 readbuf[j]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000665 }
666 }
667 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000668 if (failcount) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000669 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000670 start, start + len - 1, failcount);
671 ret = -1;
672 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000673
674out_free:
675 free(readbuf);
676 return ret;
677}
678
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000679/**
680 * Check if the buffer @have can be programmed to the content of @want without
681 * erasing. This is only possible if all chunks of size @gran are either kept
682 * as-is or changed from an all-ones state to any other state.
683 * The following write granularities (enum @gran) are known:
684 * - 1 bit. Each bit can be cleared individually.
685 * - 1 byte. A byte can be written once. Further writes to an already written
686 * byte cause the contents to be either undefined or to stay unchanged.
687 * - 128 bytes. If less than 128 bytes are written, the rest will be
688 * erased. Each write to a 128-byte region will trigger an automatic erase
689 * before anything is written. Very uncommon behaviour and unsupported by
690 * this function.
691 * - 256 bytes. If less than 256 bytes are written, the contents of the
692 * unwritten bytes are undefined.
693 *
694 * @have buffer with current content
695 * @want buffer with desired content
696 * @len length of the verified area
697 * @gran write granularity (enum, not count)
698 * @return 0 if no erase is needed, 1 otherwise
699 */
700int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
701{
702 int result = 0;
703 int i, j, limit;
704
705 switch (gran) {
706 case write_gran_1bit:
707 for (i = 0; i < len; i++)
708 if ((have[i] & want[i]) != want[i]) {
709 result = 1;
710 break;
711 }
712 break;
713 case write_gran_1byte:
714 for (i = 0; i < len; i++)
715 if ((have[i] != want[i]) && (have[i] != 0xff)) {
716 result = 1;
717 break;
718 }
719 break;
720 case write_gran_256bytes:
721 for (j = 0; j < len / 256; j++) {
722 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000723 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000724 if (!memcmp(have + j * 256, want + j * 256, limit))
725 continue;
726 /* have needs to be in erased state. */
727 for (i = 0; i < limit; i++)
728 if (have[i] != 0xff) {
729 result = 1;
730 break;
731 }
732 if (result)
733 break;
734 }
735 break;
736 }
737 return result;
738}
739
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000740/* This function generates various test patterns useful for testing controller
741 * and chip communication as well as chip behaviour.
742 *
743 * If a byte can be written multiple times, each time keeping 0-bits at 0
744 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
745 * is essentially an AND operation. That's also the reason why this function
746 * provides the result of AND between various patterns.
747 *
748 * Below is a list of patterns (and their block length).
749 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
750 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
751 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
752 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
753 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
754 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
755 * Pattern 6 is 00 (1 Byte)
756 * Pattern 7 is ff (1 Byte)
757 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
758 * byte block.
759 *
760 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
761 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
762 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
763 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
764 * Pattern 12 is 00 (1 Byte)
765 * Pattern 13 is ff (1 Byte)
766 * Patterns 8-13 have no block number.
767 *
768 * Patterns 0-3 are created to detect and efficiently diagnose communication
769 * slips like missed bits or bytes and their repetitive nature gives good visual
770 * cues to the person inspecting the results. In addition, the following holds:
771 * AND Pattern 0/1 == Pattern 4
772 * AND Pattern 2/3 == Pattern 5
773 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
774 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
775 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
776 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
777 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
778 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
779 * Besides that, they provide for bit testing of the last two bytes of every
780 * 256 byte block which contains the block number for patterns 0-6.
781 * Patterns 10-11 are special purpose for detecting subblock aliasing with
782 * block sizes >256 bytes (some Dataflash chips etc.)
783 * AND Pattern 8/9 == Pattern 12
784 * AND Pattern 10/11 == Pattern 12
785 * Pattern 13 is the completely erased state.
786 * None of the patterns can detect aliasing at boundaries which are a multiple
787 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
788 */
789int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
790{
791 int i;
792
793 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000794 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000795 return 1;
796 }
797
798 switch (variant) {
799 case 0:
800 for (i = 0; i < size; i++)
801 buf[i] = (i & 0xf) << 4 | 0x5;
802 break;
803 case 1:
804 for (i = 0; i < size; i++)
805 buf[i] = (i & 0xf) << 4 | 0xa;
806 break;
807 case 2:
808 for (i = 0; i < size; i++)
809 buf[i] = 0x50 | (i & 0xf);
810 break;
811 case 3:
812 for (i = 0; i < size; i++)
813 buf[i] = 0xa0 | (i & 0xf);
814 break;
815 case 4:
816 for (i = 0; i < size; i++)
817 buf[i] = (i & 0xf) << 4;
818 break;
819 case 5:
820 for (i = 0; i < size; i++)
821 buf[i] = i & 0xf;
822 break;
823 case 6:
824 memset(buf, 0x00, size);
825 break;
826 case 7:
827 memset(buf, 0xff, size);
828 break;
829 case 8:
830 for (i = 0; i < size; i++)
831 buf[i] = i & 0xff;
832 break;
833 case 9:
834 for (i = 0; i < size; i++)
835 buf[i] = ~(i & 0xff);
836 break;
837 case 10:
838 for (i = 0; i < size % 2; i++) {
839 buf[i * 2] = (i >> 8) & 0xff;
840 buf[i * 2 + 1] = i & 0xff;
841 }
842 if (size & 0x1)
843 buf[i * 2] = (i >> 8) & 0xff;
844 break;
845 case 11:
846 for (i = 0; i < size % 2; i++) {
847 buf[i * 2] = ~((i >> 8) & 0xff);
848 buf[i * 2 + 1] = ~(i & 0xff);
849 }
850 if (size & 0x1)
851 buf[i * 2] = ~((i >> 8) & 0xff);
852 break;
853 case 12:
854 memset(buf, 0x00, size);
855 break;
856 case 13:
857 memset(buf, 0xff, size);
858 break;
859 }
860
861 if ((variant >= 0) && (variant <= 7)) {
862 /* Write block number in the last two bytes of each 256-byte
863 * block, big endian for easier reading of the hexdump.
864 * Note that this wraps around for chips larger than 2^24 bytes
865 * (16 MB).
866 */
867 for (i = 0; i < size / 256; i++) {
868 buf[i * 256 + 254] = (i >> 8) & 0xff;
869 buf[i * 256 + 255] = i & 0xff;
870 }
871 }
872
873 return 0;
874}
875
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000876int check_max_decode(enum chipbustype buses, uint32_t size)
877{
878 int limitexceeded = 0;
879 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
880 (max_rom_decode.parallel < size)) {
881 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000882 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000883 "size %u kB of chipset/board/programmer "
884 "for %s interface, "
885 "probe/read/erase/write may fail. ", size / 1024,
886 max_rom_decode.parallel / 1024, "Parallel");
887 }
888 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
889 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000890 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000891 "size %u kB of chipset/board/programmer "
892 "for %s interface, "
893 "probe/read/erase/write may fail. ", size / 1024,
894 max_rom_decode.lpc / 1024, "LPC");
895 }
896 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
897 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000898 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000899 "size %u kB of chipset/board/programmer "
900 "for %s interface, "
901 "probe/read/erase/write may fail. ", size / 1024,
902 max_rom_decode.fwh / 1024, "FWH");
903 }
904 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
905 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000906 msg_pdbg("Chip size %u kB is bigger than supported "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000907 "size %u kB of chipset/board/programmer "
908 "for %s interface, "
909 "probe/read/erase/write may fail. ", size / 1024,
910 max_rom_decode.spi / 1024, "SPI");
911 }
912 if (!limitexceeded)
913 return 0;
914 /* Sometimes chip and programmer have more than one bus in common,
915 * and the limit is not exceeded on all buses. Tell the user.
916 */
917 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000918 /* FIXME: This message is designed towards CLI users. */
Sean Nelson316a29f2010-05-07 20:09:04 +0000919 msg_pdbg("There is at least one common chip/programmer "
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000920 "interface which can support a chip of this size. "
921 "You can try --force at your own risk.\n");
922 return 1;
923}
924
Peter Stuge483b8f02008-09-03 23:10:05 +0000925struct flashchip *probe_flash(struct flashchip *first_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000926{
Peter Stuge483b8f02008-09-03 23:10:05 +0000927 struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000928 unsigned long base = 0;
929 uint32_t size;
930 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000931 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000932
Peter Stuge483b8f02008-09-03 23:10:05 +0000933 for (flash = first_flash; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000934 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000935 continue;
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000936 msg_gdbg("Probing for %s %s, %d KB: ",
Stefan Reinauerac378972008-03-17 22:59:40 +0000937 flash->vendor, flash->name, flash->total_size);
Peter Stuge7ffbc6f2008-06-18 02:08:40 +0000938 if (!flash->probe && !force) {
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000939 msg_gdbg("failed! flashrom has no probe function for "
940 "this flash chip.\n");
Peter Stugef31104c2008-04-28 14:47:30 +0000941 continue;
942 }
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000943 buses_common = buses_supported & flash->bustype;
944 if (!buses_common) {
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000945 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000946 msg_gdbg("skipped.");
947 msg_gspew(" Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000948 free(tmp);
949 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000950 msg_gspew("and chip bus type %s are incompatible.",
951 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000952 free(tmp);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000953 msg_gdbg("\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000954 continue;
955 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000956
Ollie Lhocbbf1252004-03-17 22:22:08 +0000957 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000958 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000959
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000960 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000961 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000962
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000963 if (force)
964 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000965
Peter Stuge483b8f02008-09-03 23:10:05 +0000966 if (flash->probe(flash) != 1)
967 goto notfound;
968
Uwe Hermann394131e2008-10-18 21:14:13 +0000969 if (first_flash == flashchips
970 || flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000971 break;
972
Peter Stuge483b8f02008-09-03 23:10:05 +0000973notfound:
Carl-Daniel Hailfinger5820f422009-05-16 21:22:56 +0000974 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000975 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000976
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000977 if (!flash || !flash->name)
978 return NULL;
979
Sean Nelson316a29f2010-05-07 20:09:04 +0000980 msg_cinfo("%s chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000981 force ? "Assuming" : "Found",
Uwe Hermann9899cad2009-06-28 21:47:57 +0000982 flash->vendor, flash->name, flash->total_size,
983 flashbuses_to_text(flash->bustype), base);
984
Sean Nelson6e0b9122010-02-19 00:52:10 +0000985 if (flash->printlock)
986 flash->printlock(flash);
987
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000988 return flash;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000989}
990
Stefan Reinauere3705282005-12-18 16:41:10 +0000991int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000992{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000993 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +0000994 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000995
Sean Nelson316a29f2010-05-07 20:09:04 +0000996 msg_cinfo("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +0000997
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000998 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +0000999
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001000 if (!ret)
Sean Nelson316a29f2010-05-07 20:09:04 +00001001 msg_cinfo("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001002
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001003 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001004}
1005
Carl-Daniel Hailfinger49eb4dd2009-06-19 11:23:57 +00001006int read_flash(struct flashchip *flash, char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001007{
1008 unsigned long numbytes;
1009 FILE *image;
1010 unsigned long size = flash->total_size * 1024;
1011 unsigned char *buf = calloc(size, sizeof(char));
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001012
1013 if (!filename) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001014 msg_gerr("Error: No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001015 return 1;
1016 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001017 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001018 perror(filename);
1019 exit(1);
1020 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001021 msg_cinfo("Reading flash... ");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001022 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001023 msg_cinfo("FAILED!\n");
1024 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001025 return 1;
1026 } else
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +00001027 flash->read(flash, buf, 0, size);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001028
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001029 numbytes = fwrite(buf, 1, size, image);
1030 fclose(image);
Stephan Guilloux5a8b2442009-06-01 21:37:00 +00001031 free(buf);
Sean Nelson316a29f2010-05-07 20:09:04 +00001032 msg_cinfo("%s.\n", numbytes == size ? "done" : "FAILED");
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001033 if (numbytes != size)
1034 return 1;
1035 return 0;
1036}
1037
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001038/* This function shares a lot of its structure with erase_flash().
1039 * Even if an error is found, the function will keep going and check the rest.
1040 */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001041int selfcheck_eraseblocks(struct flashchip *flash)
1042{
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001043 int i, j, k;
1044 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001045
1046 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1047 unsigned int done = 0;
1048 struct block_eraser eraser = flash->block_erasers[k];
1049
1050 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1051 /* Blocks with zero size are bugs in flashchips.c. */
1052 if (eraser.eraseblocks[i].count &&
1053 !eraser.eraseblocks[i].size) {
1054 msg_gerr("ERROR: Flash chip %s erase function "
1055 "%i region %i has size 0. Please report"
1056 " a bug at flashrom@flashrom.org\n",
1057 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001058 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001059 }
1060 /* Blocks with zero count are bugs in flashchips.c. */
1061 if (!eraser.eraseblocks[i].count &&
1062 eraser.eraseblocks[i].size) {
1063 msg_gerr("ERROR: Flash chip %s erase function "
1064 "%i region %i has count 0. Please report"
1065 " a bug at flashrom@flashrom.org\n",
1066 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001067 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001068 }
1069 done += eraser.eraseblocks[i].count *
1070 eraser.eraseblocks[i].size;
1071 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001072 /* Empty eraseblock definition with erase function. */
1073 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001074 msg_gspew("Strange: Empty eraseblock definition with "
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001075 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001076 if (!done)
1077 continue;
1078 if (done != flash->total_size * 1024) {
1079 msg_gerr("ERROR: Flash chip %s erase function %i "
1080 "region walking resulted in 0x%06x bytes total,"
1081 " expected 0x%06x bytes. Please report a bug at"
1082 " flashrom@flashrom.org\n", flash->name, k,
1083 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001084 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001085 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001086 if (!eraser.block_erase)
1087 continue;
1088 /* Check if there are identical erase functions for different
1089 * layouts. That would imply "magic" erase functions. The
1090 * easiest way to check this is with function pointers.
1091 */
Uwe Hermann43959702010-03-13 17:28:29 +00001092 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001093 if (eraser.block_erase ==
1094 flash->block_erasers[j].block_erase) {
1095 msg_gerr("ERROR: Flash chip %s erase function "
1096 "%i and %i are identical. Please report"
1097 " a bug at flashrom@flashrom.org\n",
1098 flash->name, k, j);
1099 ret = 1;
1100 }
Uwe Hermann43959702010-03-13 17:28:29 +00001101 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001102 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001103 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001104}
1105
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001106int erase_flash(struct flashchip *flash)
1107{
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001108 int i, j, k, ret = 0, found = 0;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001109 unsigned int start, len;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001110
Sean Nelson316a29f2010-05-07 20:09:04 +00001111 msg_cinfo("Erasing flash chip... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001112 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001113 unsigned int done = 0;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001114 struct block_eraser eraser = flash->block_erasers[k];
1115
Sean Nelson316a29f2010-05-07 20:09:04 +00001116 msg_cdbg("Looking at blockwise erase function %i... ", k);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001117 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001118 msg_cdbg("not defined. "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001119 "Looking for another erase function.\n");
1120 continue;
1121 }
1122 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001123 msg_cdbg("eraseblock layout is known, but no "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001124 "matching block erase function found. "
1125 "Looking for another erase function.\n");
1126 continue;
1127 }
1128 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001129 msg_cdbg("block erase function found, but "
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001130 "eraseblock layout is unknown. "
1131 "Looking for another erase function.\n");
1132 continue;
1133 }
1134 found = 1;
Sean Nelson316a29f2010-05-07 20:09:04 +00001135 msg_cdbg("trying... ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001136 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1137 /* count==0 for all automatically initialized array
1138 * members so the loop below won't be executed for them.
1139 */
1140 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001141 start = done + eraser.eraseblocks[i].size * j;
1142 len = eraser.eraseblocks[i].size;
Sean Nelson316a29f2010-05-07 20:09:04 +00001143 msg_cdbg("0x%06x-0x%06x, ", start,
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001144 start + len - 1);
1145 ret = eraser.block_erase(flash, start, len);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001146 if (ret)
1147 break;
1148 }
1149 if (ret)
1150 break;
Carl-Daniel Hailfinger9d489162009-12-14 04:04:18 +00001151 done += eraser.eraseblocks[i].count *
1152 eraser.eraseblocks[i].size;
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001153 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001154 msg_cdbg("\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001155 /* If everything is OK, don't try another erase function. */
1156 if (!ret)
1157 break;
1158 }
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001159 if (!found) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001160 msg_cerr("ERROR: flashrom has no erase function for this flash chip.\n");
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001161 return 1;
1162 }
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001163
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001164 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001165 msg_cerr("FAILED!\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001166 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001167 msg_cinfo("SUCCESS.\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001168 }
1169 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001170}
1171
Uwe Hermannc67d0372009-10-01 18:40:02 +00001172void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001173{
Sean Nelson316a29f2010-05-07 20:09:04 +00001174 msg_gerr("Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001175 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001176 "mail flashrom@flashrom.org!\n"
1177 "-------------------------------------------------------------"
1178 "------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001179 "DO NOT REBOOT OR POWEROFF!\n");
1180}
1181
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001182/* The way to go if you want a delimited list of programmers*/
1183void list_programmers(char *delim)
1184{
1185 enum programmer p;
1186 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001187 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001188 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001189 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001190 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001191 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001192}
1193
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001194void print_sysinfo(void)
1195{
1196#if HAVE_UTSNAME == 1
1197 struct utsname osinfo;
1198 uname(&osinfo);
1199
1200 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1201 osinfo.machine);
1202#else
1203 msg_ginfo(" on unknown machine");
1204#endif
1205 msg_ginfo(", built with");
1206#if NEED_PCI == 1
1207#ifdef PCILIB_VERSION
1208 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1209#else
1210 msg_ginfo(" unknown PCI library,");
1211#endif
1212#endif
1213#ifdef __clang__
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001214 msg_ginfo(" LLVM %i/clang %i, ", __llvm__, __clang__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001215#elif defined(__GNUC__)
1216 msg_ginfo(" GCC");
1217#ifdef __VERSION__
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001218 msg_ginfo(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001219#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001220 msg_ginfo(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001221#endif
1222#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001223 msg_ginfo(" unknown compiler,");
1224#endif
1225#if defined (__FLASHROM_LITTLE_ENDIAN__)
1226 msg_ginfo(" little endian");
1227#else
1228 msg_ginfo(" big endian");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001229#endif
1230 msg_ginfo("\n");
1231}
1232
Bernhard Walle201bde32008-01-21 15:24:22 +00001233void print_version(void)
1234{
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001235 msg_ginfo("flashrom v%s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001236 print_sysinfo();
Bernhard Walle201bde32008-01-21 15:24:22 +00001237}
1238
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001239void print_banner(void)
1240{
1241 msg_ginfo("flashrom is free software, get the source code at "
1242 "http://www.flashrom.org\n");
1243 msg_ginfo("\n");
1244}
1245
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001246int selfcheck(void)
1247{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001248 int ret = 0;
1249 struct flashchip *flash;
1250
1251 /* Safety check. Instead of aborting after the first error, check
1252 * if more errors exist.
1253 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001254 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001255 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001256 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001257 }
1258 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001259 msg_gerr("SPI programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001260 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001261 }
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001262#if CONFIG_BITBANG_SPI == 1
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001263 if (bitbang_spi_master_count - 1 != BITBANG_SPI_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001264 msg_gerr("Bitbanging SPI master table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001265 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001266 }
1267#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001268 for (flash = flashchips; flash && flash->name; flash++)
1269 if (selfcheck_eraseblocks(flash))
1270 ret = 1;
1271 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001272}
1273
1274void check_chip_supported(struct flashchip *flash)
1275{
1276 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001277 msg_cinfo("===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001278 if (flash->tested & TEST_BAD_MASK) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001279 msg_cinfo("This flash part has status NOT WORKING for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001280 if (flash->tested & TEST_BAD_PROBE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001281 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001282 if (flash->tested & TEST_BAD_READ)
Sean Nelson316a29f2010-05-07 20:09:04 +00001283 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001284 if (flash->tested & TEST_BAD_ERASE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001285 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001286 if (flash->tested & TEST_BAD_WRITE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001287 msg_cinfo(" WRITE");
1288 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001289 }
1290 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1291 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1292 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1293 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001294 msg_cinfo("This flash part has status UNTESTED for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001295 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001296 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001297 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
Sean Nelson316a29f2010-05-07 20:09:04 +00001298 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001299 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001300 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001301 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001302 msg_cinfo(" WRITE");
1303 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001304 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001305 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001306 msg_cinfo("The test status of this chip may have been updated "
1307 "in the latest development\n"
1308 "version of flashrom. If you are running the latest "
1309 "development version,\n"
1310 "please email a report to flashrom@flashrom.org if "
1311 "any of the above operations\n"
1312 "work correctly for you with this flash part. Please "
1313 "include the flashrom\n"
1314 "output with the additional -V option for all "
1315 "operations you tested (-V, -Vr,\n"
1316 "-Vw, -VE), and mention which mainboard or "
1317 "programmer you tested.\n"
1318 "Thanks for your help!\n"
1319 "===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001320 }
1321}
1322
Ollie Lho761bf1b2004-03-20 16:46:10 +00001323int main(int argc, char *argv[])
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001324{
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001325 return cli_classic(argc, argv);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001326}
1327
1328/* This function signature is horrible. We need to design a better interface,
1329 * but right now it allows us to split off the CLI code.
1330 */
1331int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1332{
1333 uint8_t *buf;
1334 unsigned long numbytes;
1335 FILE *image;
1336 int ret = 0;
1337 unsigned long size;
1338
1339 size = flash->total_size * 1024;
Ollie Lho184a4042005-11-26 21:55:36 +00001340 buf = (uint8_t *) calloc(size, sizeof(char));
Uwe Hermanna7e05482007-05-09 10:17:44 +00001341
Ollie Lhoefa28582004-12-08 20:10:01 +00001342 if (erase_it) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001343 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001344 msg_cerr("Erase is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001345 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001346 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001347 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001348 return 1;
1349 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001350 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001351 }
1352 }
Sean Nelson6e0b9122010-02-19 00:52:10 +00001353 if (flash->unlock)
1354 flash->unlock(flash);
1355
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001356 if (erase_flash(flash)) {
1357 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001358 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001359 return 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001360 }
Ollie Lhoefa28582004-12-08 20:10:01 +00001361 } else if (read_it) {
Sean Nelson6e0b9122010-02-19 00:52:10 +00001362 if (flash->unlock)
1363 flash->unlock(flash);
1364
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001365 if (read_flash(flash, filename)) {
1366 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001367 return 1;
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001368 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001369 } else {
Stefan Reinauer018aca82006-11-21 23:48:51 +00001370 struct stat image_stat;
1371
Sean Nelson6e0b9122010-02-19 00:52:10 +00001372 if (flash->unlock)
1373 flash->unlock(flash);
1374
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001375 if (flash->tested & TEST_BAD_ERASE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001376 msg_cerr("Erase is not working on this chip "
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001377 "and erase is needed for write. ");
1378 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001379 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001380 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001381 return 1;
1382 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001383 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001384 }
1385 }
1386 if (flash->tested & TEST_BAD_WRITE) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001387 msg_cerr("Write is not working on this chip. ");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001388 if (!force) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001389 msg_cerr("Aborting.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001390 programmer_shutdown();
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001391 return 1;
1392 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001393 msg_cerr("Continuing anyway.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001394 }
1395 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001396 if ((image = fopen(filename, "rb")) == NULL) {
Ollie Lhocbbf1252004-03-17 22:22:08 +00001397 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001398 programmer_shutdown();
Ollie Lhocbbf1252004-03-17 22:22:08 +00001399 exit(1);
1400 }
Stefan Reinauer018aca82006-11-21 23:48:51 +00001401 if (fstat(fileno(image), &image_stat) != 0) {
1402 perror(filename);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001403 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001404 exit(1);
1405 }
Uwe Hermanna7e05482007-05-09 10:17:44 +00001406 if (image_stat.st_size != flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001407 msg_gerr("Error: Image size doesn't match\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001408 programmer_shutdown();
Stefan Reinauer018aca82006-11-21 23:48:51 +00001409 exit(1);
1410 }
1411
Peter Stuge1fec0f32009-01-12 21:00:35 +00001412 numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001413#if CONFIG_INTERNAL == 1
Peter Stuge7ffbc6f2008-06-18 02:08:40 +00001414 show_id(buf, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001415#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001416 fclose(image);
Peter Stuge1fec0f32009-01-12 21:00:35 +00001417 if (numbytes != size) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001418 msg_gerr("Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001419 programmer_shutdown();
Peter Stuge1fec0f32009-01-12 21:00:35 +00001420 return 1;
1421 }
Ollie Lhocbbf1252004-03-17 22:22:08 +00001422 }
1423
Ollie Lho184a4042005-11-26 21:55:36 +00001424 // This should be moved into each flash part's code to do it
1425 // cleanly. This does the job.
Carl-Daniel Hailfingerf5fb51c2009-08-19 15:19:18 +00001426 handle_romentries(buf, flash);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001427
Ollie Lho184a4042005-11-26 21:55:36 +00001428 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001429
Peter Stugef31104c2008-04-28 14:47:30 +00001430 if (write_it) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001431 msg_cinfo("Writing flash chip... ");
Peter Stugef31104c2008-04-28 14:47:30 +00001432 if (!flash->write) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001433 msg_cerr("Error: flashrom has no write function for this flash chip.\n");
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001434 programmer_shutdown();
Peter Stugef31104c2008-04-28 14:47:30 +00001435 return 1;
1436 }
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001437 ret = flash->write(flash, buf);
1438 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001439 msg_cerr("FAILED!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001440 emergency_help_message();
Uwe Hermann2bc98f62009-09-30 18:29:55 +00001441 programmer_shutdown();
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001442 return 1;
1443 } else {
Sean Nelson316a29f2010-05-07 20:09:04 +00001444 msg_cinfo("COMPLETE.\n");
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001445 }
Peter Stugef31104c2008-04-28 14:47:30 +00001446 }
Ollie Lho184a4042005-11-26 21:55:36 +00001447
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001448 if (verify_it) {
1449 /* Work around chips which need some time to calm down. */
1450 if (write_it)
1451 programmer_delay(1000*1000);
Carl-Daniel Hailfinger0a3e5ae2009-07-24 12:18:54 +00001452 ret = verify_flash(flash, buf);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001453 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001454 * might have an emergency situation.
1455 */
1456 if (ret && write_it)
1457 emergency_help_message();
1458 }
Ollie Lho184a4042005-11-26 21:55:36 +00001459
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001460 programmer_shutdown();
1461
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001462 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001463}