blob: c565a434514a60160779bc064050f4b0221e2c1f [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>
Stefan Reinauer018aca82006-11-21 23:48:51 +000025#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000026#ifndef __LIBPAYLOAD__
27#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000028#include <sys/stat.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000029#endif
Ronald G. Minnichceec4202003-07-25 04:37:41 +000030#include <string.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000031#include <stdlib.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000032#include <ctype.h>
Ollie Lho184a4042005-11-26 21:55:36 +000033#include <getopt.h>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000037#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000038#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000039#include "programmer.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000040
Mathias Krausea60faab2011-01-17 07:50:42 +000041const char flashrom_version[] = FLASHROM_VERSION;
Ronald G. Minnichceec4202003-07-25 04:37:41 +000042char *chip_to_probe = NULL;
Peter Stuge7ffbc6f2008-06-18 02:08:40 +000043int verbose = 0;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000044
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +000045static enum programmer programmer = PROGRAMMER_INVALID;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000046
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +000047static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000048
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000049/* Supported buses for the current programmer. */
50enum chipbustype buses_supported;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000051
Uwe Hermann48ec1b12010-08-08 17:01:18 +000052/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000053 * Programmers supporting multiple buses can have differing size limits on
54 * each bus. Store the limits for each bus in a common struct.
55 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000056struct decode_sizes max_rom_decode;
57
58/* If nonzero, used as the start address of bottom-aligned flash. */
59unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000060
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000061/* Is writing allowed with this programmer? */
62int programmer_may_write;
63
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000064const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000065#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000066 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000067 .name = "internal",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000068 .init = internal_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000069 .map_flash_region = physmap,
70 .unmap_flash_region = physunmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000071 .chip_readb = internal_chip_readb,
72 .chip_readw = internal_chip_readw,
73 .chip_readl = internal_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +000074 .chip_readn = internal_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000075 .chip_writeb = internal_chip_writeb,
76 .chip_writew = internal_chip_writew,
77 .chip_writel = internal_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +000078 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000079 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000080 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000081#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000082
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000083#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000084 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000085 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000086 .init = dummy_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000087 .map_flash_region = dummy_map,
88 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000089 .chip_readb = dummy_chip_readb,
90 .chip_readw = dummy_chip_readw,
91 .chip_readl = dummy_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +000092 .chip_readn = dummy_chip_readn,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000093 .chip_writeb = dummy_chip_writeb,
94 .chip_writew = dummy_chip_writew,
95 .chip_writel = dummy_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +000096 .chip_writen = dummy_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000097 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000098 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000099#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +0000100
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000101#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000102 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000103 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000104 .init = nic3com_init,
Uwe Hermannc6915932009-05-17 23:12:17 +0000105 .map_flash_region = fallback_map,
106 .unmap_flash_region = fallback_unmap,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000107 .chip_readb = nic3com_chip_readb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000108 .chip_readw = fallback_chip_readw,
109 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000110 .chip_readn = fallback_chip_readn,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000111 .chip_writeb = nic3com_chip_writeb,
Carl-Daniel Hailfinger9ee10772009-05-16 01:23:55 +0000112 .chip_writew = fallback_chip_writew,
113 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000114 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000115 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000116 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000117#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000118
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000119#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000120 {
Carl-Daniel Hailfinger40446ee2011-03-07 01:08:09 +0000121 /* This programmer works for Realtek RTL8139 and SMC 1211. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000122 .name = "nicrealtek",
123 //.name = "nicsmc1211",
124 .init = nicrealtek_init,
125 .map_flash_region = fallback_map,
126 .unmap_flash_region = fallback_unmap,
127 .chip_readb = nicrealtek_chip_readb,
128 .chip_readw = fallback_chip_readw,
129 .chip_readl = fallback_chip_readl,
130 .chip_readn = fallback_chip_readn,
131 .chip_writeb = nicrealtek_chip_writeb,
132 .chip_writew = fallback_chip_writew,
133 .chip_writel = fallback_chip_writel,
134 .chip_writen = fallback_chip_writen,
135 .delay = internal_delay,
Joerg Fischer5665ef32010-05-21 21:54:07 +0000136 },
Joerg Fischer5665ef32010-05-21 21:54:07 +0000137#endif
138
Andrew Morganc29c2e72010-06-07 22:37:54 +0000139#if CONFIG_NICNATSEMI == 1
140 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000141 .name = "nicnatsemi",
142 .init = nicnatsemi_init,
143 .map_flash_region = fallback_map,
144 .unmap_flash_region = fallback_unmap,
145 .chip_readb = nicnatsemi_chip_readb,
146 .chip_readw = fallback_chip_readw,
147 .chip_readl = fallback_chip_readl,
148 .chip_readn = fallback_chip_readn,
149 .chip_writeb = nicnatsemi_chip_writeb,
150 .chip_writew = fallback_chip_writew,
151 .chip_writel = fallback_chip_writel,
152 .chip_writen = fallback_chip_writen,
153 .delay = internal_delay,
Andrew Morganc29c2e72010-06-07 22:37:54 +0000154 },
155#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000156
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000157#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000158 {
159 .name = "gfxnvidia",
160 .init = gfxnvidia_init,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000161 .map_flash_region = fallback_map,
162 .unmap_flash_region = fallback_unmap,
163 .chip_readb = gfxnvidia_chip_readb,
164 .chip_readw = fallback_chip_readw,
165 .chip_readl = fallback_chip_readl,
166 .chip_readn = fallback_chip_readn,
167 .chip_writeb = gfxnvidia_chip_writeb,
168 .chip_writew = fallback_chip_writew,
169 .chip_writel = fallback_chip_writel,
170 .chip_writen = fallback_chip_writen,
171 .delay = internal_delay,
172 },
173#endif
174
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000175#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000176 {
TURBO Jb0912c02009-09-02 23:00:46 +0000177 .name = "drkaiser",
178 .init = drkaiser_init,
TURBO Jb0912c02009-09-02 23:00:46 +0000179 .map_flash_region = fallback_map,
180 .unmap_flash_region = fallback_unmap,
181 .chip_readb = drkaiser_chip_readb,
182 .chip_readw = fallback_chip_readw,
183 .chip_readl = fallback_chip_readl,
184 .chip_readn = fallback_chip_readn,
185 .chip_writeb = drkaiser_chip_writeb,
186 .chip_writew = fallback_chip_writew,
187 .chip_writel = fallback_chip_writel,
188 .chip_writen = fallback_chip_writen,
189 .delay = internal_delay,
190 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000191#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000192
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000193#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000194 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000195 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000196 .init = satasii_init,
Uwe Hermannc6915932009-05-17 23:12:17 +0000197 .map_flash_region = fallback_map,
198 .unmap_flash_region = fallback_unmap,
Rudolf Marek68720c72009-05-17 19:39:27 +0000199 .chip_readb = satasii_chip_readb,
200 .chip_readw = fallback_chip_readw,
201 .chip_readl = fallback_chip_readl,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000202 .chip_readn = fallback_chip_readn,
Rudolf Marek68720c72009-05-17 19:39:27 +0000203 .chip_writeb = satasii_chip_writeb,
204 .chip_writew = fallback_chip_writew,
205 .chip_writel = fallback_chip_writel,
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000206 .chip_writen = fallback_chip_writen,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000207 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000208 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000209#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000210
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000211#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000212 {
213 .name = "atahpt",
214 .init = atahpt_init,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000215 .map_flash_region = fallback_map,
216 .unmap_flash_region = fallback_unmap,
217 .chip_readb = atahpt_chip_readb,
218 .chip_readw = fallback_chip_readw,
219 .chip_readl = fallback_chip_readl,
220 .chip_readn = fallback_chip_readn,
221 .chip_writeb = atahpt_chip_writeb,
222 .chip_writew = fallback_chip_writew,
223 .chip_writel = fallback_chip_writel,
224 .chip_writen = fallback_chip_writen,
225 .delay = internal_delay,
226 },
227#endif
228
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000229#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000230 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000231 .name = "ft2232_spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000232 .init = ft2232_spi_init,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000233 .map_flash_region = fallback_map,
234 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000235 .chip_readb = noop_chip_readb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000236 .chip_readw = fallback_chip_readw,
237 .chip_readl = fallback_chip_readl,
238 .chip_readn = fallback_chip_readn,
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000239 .chip_writeb = noop_chip_writeb,
Paul Fox05dfbe62009-06-16 21:08:06 +0000240 .chip_writew = fallback_chip_writew,
241 .chip_writel = fallback_chip_writel,
242 .chip_writen = fallback_chip_writen,
243 .delay = internal_delay,
244 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000245#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000246
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000247#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000248 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000249 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000250 .init = serprog_init,
Urja Rannikko22915352009-06-23 11:33:43 +0000251 .map_flash_region = fallback_map,
252 .unmap_flash_region = fallback_unmap,
253 .chip_readb = serprog_chip_readb,
254 .chip_readw = fallback_chip_readw,
255 .chip_readl = fallback_chip_readl,
256 .chip_readn = serprog_chip_readn,
257 .chip_writeb = serprog_chip_writeb,
258 .chip_writew = fallback_chip_writew,
259 .chip_writel = fallback_chip_writel,
260 .chip_writen = fallback_chip_writen,
261 .delay = serprog_delay,
262 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000263#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000264
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000265#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000266 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000267 .name = "buspirate_spi",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000268 .init = buspirate_spi_init,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000269 .map_flash_region = fallback_map,
270 .unmap_flash_region = fallback_unmap,
271 .chip_readb = noop_chip_readb,
272 .chip_readw = fallback_chip_readw,
273 .chip_readl = fallback_chip_readl,
274 .chip_readn = fallback_chip_readn,
275 .chip_writeb = noop_chip_writeb,
276 .chip_writew = fallback_chip_writew,
277 .chip_writel = fallback_chip_writel,
278 .chip_writen = fallback_chip_writen,
279 .delay = internal_delay,
280 },
281#endif
282
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000283#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000284 {
285 .name = "dediprog",
286 .init = dediprog_init,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000287 .map_flash_region = fallback_map,
288 .unmap_flash_region = fallback_unmap,
289 .chip_readb = noop_chip_readb,
290 .chip_readw = fallback_chip_readw,
291 .chip_readl = fallback_chip_readl,
292 .chip_readn = fallback_chip_readn,
293 .chip_writeb = noop_chip_writeb,
294 .chip_writew = fallback_chip_writew,
295 .chip_writel = fallback_chip_writel,
296 .chip_writen = fallback_chip_writen,
297 .delay = internal_delay,
298 },
299#endif
300
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000301#if CONFIG_RAYER_SPI == 1
302 {
303 .name = "rayer_spi",
304 .init = rayer_spi_init,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000305 .map_flash_region = fallback_map,
306 .unmap_flash_region = fallback_unmap,
307 .chip_readb = noop_chip_readb,
308 .chip_readw = fallback_chip_readw,
309 .chip_readl = fallback_chip_readl,
310 .chip_readn = fallback_chip_readn,
311 .chip_writeb = noop_chip_writeb,
312 .chip_writew = fallback_chip_writew,
313 .chip_writel = fallback_chip_writel,
314 .chip_writen = fallback_chip_writen,
315 .delay = internal_delay,
316 },
317#endif
318
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000319#if CONFIG_NICINTEL == 1
320 {
321 .name = "nicintel",
322 .init = nicintel_init,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000323 .map_flash_region = fallback_map,
324 .unmap_flash_region = fallback_unmap,
325 .chip_readb = nicintel_chip_readb,
326 .chip_readw = fallback_chip_readw,
327 .chip_readl = fallback_chip_readl,
328 .chip_readn = fallback_chip_readn,
329 .chip_writeb = nicintel_chip_writeb,
330 .chip_writew = fallback_chip_writew,
331 .chip_writel = fallback_chip_writel,
332 .chip_writen = fallback_chip_writen,
333 .delay = internal_delay,
334 },
335#endif
336
Idwer Vollering004f4b72010-09-03 18:21:21 +0000337#if CONFIG_NICINTEL_SPI == 1
338 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000339 .name = "nicintel_spi",
340 .init = nicintel_spi_init,
341 .map_flash_region = fallback_map,
342 .unmap_flash_region = fallback_unmap,
343 .chip_readb = noop_chip_readb,
344 .chip_readw = fallback_chip_readw,
345 .chip_readl = fallback_chip_readl,
346 .chip_readn = fallback_chip_readn,
347 .chip_writeb = noop_chip_writeb,
348 .chip_writew = fallback_chip_writew,
349 .chip_writel = fallback_chip_writel,
350 .chip_writen = fallback_chip_writen,
351 .delay = internal_delay,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000352 },
353#endif
354
Mark Marshall90021f22010-12-03 14:48:11 +0000355#if CONFIG_OGP_SPI == 1
356 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000357 .name = "ogp_spi",
358 .init = ogp_spi_init,
359 .map_flash_region = fallback_map,
360 .unmap_flash_region = fallback_unmap,
361 .chip_readb = noop_chip_readb,
362 .chip_readw = fallback_chip_readw,
363 .chip_readl = fallback_chip_readl,
364 .chip_readn = fallback_chip_readn,
365 .chip_writeb = noop_chip_writeb,
366 .chip_writew = fallback_chip_writew,
367 .chip_writel = fallback_chip_writel,
368 .chip_writen = fallback_chip_writen,
369 .delay = internal_delay,
Mark Marshall90021f22010-12-03 14:48:11 +0000370 },
371#endif
372
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000373#if CONFIG_SATAMV == 1
374 {
375 .name = "satamv",
376 .init = satamv_init,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000377 .map_flash_region = fallback_map,
378 .unmap_flash_region = fallback_unmap,
379 .chip_readb = satamv_chip_readb,
380 .chip_readw = fallback_chip_readw,
381 .chip_readl = fallback_chip_readl,
382 .chip_readn = fallback_chip_readn,
383 .chip_writeb = satamv_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
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000391#if CONFIG_LINUX_SPI == 1
392 {
393 .name = "linux_spi",
394 .init = linux_spi_init,
395 .map_flash_region = fallback_map,
396 .unmap_flash_region = fallback_unmap,
397 .chip_readb = noop_chip_readb,
398 .chip_readw = fallback_chip_readw,
399 .chip_readl = fallback_chip_readl,
400 .chip_readn = fallback_chip_readn,
401 .chip_writeb = noop_chip_writeb,
402 .chip_writew = fallback_chip_writew,
403 .chip_writel = fallback_chip_writel,
404 .chip_writen = fallback_chip_writen,
405 .delay = internal_delay,
406 },
407#endif
408
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000409 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000410};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000411
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +0000412#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000413static int shutdown_fn_count = 0;
414struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +0000415 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000416 void *data;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000417} static shutdown_fn[SHUTDOWN_MAXFN];
418/* Initialize to 0 to make sure nobody registers a shutdown function before
419 * programmer init.
420 */
421static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000422
Stefan Tauner5368dca2011-07-01 00:19:12 +0000423static int check_block_eraser(const struct flashchip *flash, int k, int log);
424
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000425/* Register a function to be executed on programmer shutdown.
426 * The advantage over atexit() is that you can supply a void pointer which will
427 * be used as parameter to the registered function upon programmer shutdown.
428 * This pointer can point to arbitrary data used by said function, e.g. undo
429 * information for GPIO settings etc. If unneeded, set data=NULL.
430 * Please note that the first (void *data) belongs to the function signature of
431 * the function passed as first parameter.
432 */
David Hendricks8bb20212011-06-14 01:35:36 +0000433int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000434{
435 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000436 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000437 SHUTDOWN_MAXFN);
438 return 1;
439 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000440 if (!may_register_shutdown) {
441 msg_perr("Tried to register a shutdown function before "
442 "programmer init.\n");
443 return 1;
444 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000445 shutdown_fn[shutdown_fn_count].func = function;
446 shutdown_fn[shutdown_fn_count].data = data;
447 shutdown_fn_count++;
448
449 return 0;
450}
451
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000452int programmer_init(enum programmer prog, char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000453{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000454 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000455
456 if (prog >= PROGRAMMER_INVALID) {
457 msg_perr("Invalid programmer specified!\n");
458 return -1;
459 }
460 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000461 /* Initialize all programmer specific data. */
462 /* Default to unlimited decode sizes. */
463 max_rom_decode = (const struct decode_sizes) {
464 .parallel = 0xffffffff,
465 .lpc = 0xffffffff,
466 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000467 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000468 };
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000469 buses_supported = BUS_NONE;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000470 /* Default to top aligned flash at 4 GB. */
471 flashbase = 0;
472 /* Registering shutdown functions is now allowed. */
473 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000474 /* Default to allowing writes. Broken programmers set this to 0. */
475 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000476
477 programmer_param = param;
478 msg_pdbg("Initializing %s programmer\n",
479 programmer_table[programmer].name);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000480 ret = programmer_table[programmer].init();
481 if (programmer_param && strlen(programmer_param)) {
482 msg_perr("Unhandled programmer parameters: %s\n",
483 programmer_param);
484 /* Do not error out here, the init itself was successful. */
485 }
486 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000487}
488
489int programmer_shutdown(void)
490{
David Hendricks8bb20212011-06-14 01:35:36 +0000491 int ret = 0;
492
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000493 /* Registering shutdown functions is no longer allowed. */
494 may_register_shutdown = 0;
495 while (shutdown_fn_count > 0) {
496 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000497 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000498 }
David Hendricks8bb20212011-06-14 01:35:36 +0000499 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000500}
501
502void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
503 size_t len)
504{
505 return programmer_table[programmer].map_flash_region(descr,
506 phys_addr, len);
507}
508
509void programmer_unmap_flash_region(void *virt_addr, size_t len)
510{
511 programmer_table[programmer].unmap_flash_region(virt_addr, len);
512}
513
514void chip_writeb(uint8_t val, chipaddr addr)
515{
516 programmer_table[programmer].chip_writeb(val, addr);
517}
518
519void chip_writew(uint16_t val, chipaddr addr)
520{
521 programmer_table[programmer].chip_writew(val, addr);
522}
523
524void chip_writel(uint32_t val, chipaddr addr)
525{
526 programmer_table[programmer].chip_writel(val, addr);
527}
528
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000529void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
530{
531 programmer_table[programmer].chip_writen(buf, addr, len);
532}
533
Uwe Hermann09e04f72009-05-16 22:36:00 +0000534uint8_t chip_readb(const chipaddr addr)
535{
536 return programmer_table[programmer].chip_readb(addr);
537}
538
539uint16_t chip_readw(const chipaddr addr)
540{
541 return programmer_table[programmer].chip_readw(addr);
542}
543
544uint32_t chip_readl(const chipaddr addr)
545{
546 return programmer_table[programmer].chip_readl(addr);
547}
548
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000549void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
550{
551 programmer_table[programmer].chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000552}
553
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000554void programmer_delay(int usecs)
555{
556 programmer_table[programmer].delay(usecs);
557}
558
Peter Stuge776d2022009-01-26 00:39:57 +0000559void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000560{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000561 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000562 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000563 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000564 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000565}
566
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000567int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000568{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000569 chip_readn(buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000570
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000571 return 0;
572}
573
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000574int min(int a, int b)
575{
576 return (a < b) ? a : b;
577}
578
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000579int max(int a, int b)
580{
581 return (a > b) ? a : b;
582}
583
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000584int bitcount(unsigned long a)
585{
586 int i = 0;
587 for (; a != 0; a >>= 1)
588 if (a & 1)
589 i++;
590 return i;
591}
592
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000593void tolower_string(char *str)
594{
595 for (; *str != '\0'; str++)
596 *str = (char)tolower((unsigned char)*str);
597}
598
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000599char *strcat_realloc(char *dest, const char *src)
600{
601 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000602 if (!dest) {
603 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000604 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000605 }
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000606 strcat(dest, src);
607 return dest;
608}
609
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000610/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000611 * It will look for needle with a subsequent '=' in haystack, return a copy of
612 * needle and remove everything from the first occurrence of needle to the next
613 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000614 */
Stefan Tauner66652442011-06-26 17:38:17 +0000615char *extract_param(char **haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000616{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000617 char *param_pos, *opt_pos, *rest;
618 char *opt = NULL;
619 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000620 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000621
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000622 needlelen = strlen(needle);
623 if (!needlelen) {
624 msg_gerr("%s: empty needle! Please report a bug at "
625 "flashrom@flashrom.org\n", __func__);
626 return NULL;
627 }
628 /* No programmer parameters given. */
629 if (*haystack == NULL)
630 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000631 param_pos = strstr(*haystack, needle);
632 do {
633 if (!param_pos)
634 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000635 /* Needle followed by '='? */
636 if (param_pos[needlelen] == '=') {
637
638 /* Beginning of the string? */
639 if (param_pos == *haystack)
640 break;
641 /* After a delimiter? */
642 if (strchr(delim, *(param_pos - 1)))
643 break;
644 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000645 /* Continue searching. */
646 param_pos++;
647 param_pos = strstr(param_pos, needle);
648 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000649
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000650 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000651 /* Get the string after needle and '='. */
652 opt_pos = param_pos + needlelen + 1;
653 optlen = strcspn(opt_pos, delim);
654 /* Return an empty string if the parameter was empty. */
655 opt = malloc(optlen + 1);
656 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000657 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000658 exit(1);
659 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000660 strncpy(opt, opt_pos, optlen);
661 opt[optlen] = '\0';
662 rest = opt_pos + optlen;
663 /* Skip all delimiters after the current parameter. */
664 rest += strspn(rest, delim);
665 memmove(param_pos, rest, strlen(rest) + 1);
666 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000667 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000668
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000669 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000670}
671
Stefan Tauner66652442011-06-26 17:38:17 +0000672char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000673{
674 return extract_param(&programmer_param, param_name, ",");
675}
676
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000677/* Returns the number of well-defined erasers for a chip. */
678static unsigned int count_usable_erasers(const struct flashchip *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000679{
680 unsigned int usable_erasefunctions = 0;
681 int k;
682 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
683 if (!check_block_eraser(flash, k, 0))
684 usable_erasefunctions++;
685 }
686 return usable_erasefunctions;
687}
688
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000689/* start is an offset to the base address of the flash chip */
690int check_erased_range(struct flashchip *flash, int start, int len)
691{
692 int ret;
693 uint8_t *cmpbuf = malloc(len);
694
695 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000696 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000697 exit(1);
698 }
699 memset(cmpbuf, 0xff, len);
700 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
701 free(cmpbuf);
702 return ret;
703}
704
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000705/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000706 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000707 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000708 * @start offset to the base address of the flash chip
709 * @len length of the verified area
710 * @message string to print in the "FAILED" message
711 * @return 0 for success, -1 for failure
712 */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000713int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len,
714 const char *message)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000715{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000716 int i;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000717 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000718 int ret = 0, failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000719
720 if (!len)
721 goto out_free;
722
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000723 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000724 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000725 return 1;
726 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000727 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000728 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000729 exit(1);
730 }
731
732 if (start + len > flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000733 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000734 " total_size 0x%x\n", __func__, start, len,
735 flash->total_size * 1024);
736 ret = -1;
737 goto out_free;
738 }
739 if (!message)
740 message = "VERIFY";
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000741
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000742 ret = flash->read(flash, readbuf, start, len);
743 if (ret) {
744 msg_gerr("Verification impossible because read failed "
745 "at 0x%x (len 0x%x)\n", start, len);
746 return ret;
747 }
748
749 for (i = 0; i < len; i++) {
750 if (cmpbuf[i] != readbuf[i]) {
751 /* Only print the first failure. */
752 if (!failcount++)
753 msg_cerr("%s FAILED at 0x%08x! "
754 "Expected=0x%02x, Read=0x%02x,",
755 message, start + i, cmpbuf[i],
756 readbuf[i]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000757 }
758 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000759 if (failcount) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000760 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000761 start, start + len - 1, failcount);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000762 ret = -1;
763 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000764
765out_free:
766 free(readbuf);
767 return ret;
768}
769
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000770/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000771 * Check if the buffer @have can be programmed to the content of @want without
772 * erasing. This is only possible if all chunks of size @gran are either kept
773 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000774 *
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000775 * The following write granularities (enum @gran) are known:
776 * - 1 bit. Each bit can be cleared individually.
777 * - 1 byte. A byte can be written once. Further writes to an already written
778 * byte cause the contents to be either undefined or to stay unchanged.
779 * - 128 bytes. If less than 128 bytes are written, the rest will be
780 * erased. Each write to a 128-byte region will trigger an automatic erase
781 * before anything is written. Very uncommon behaviour and unsupported by
782 * this function.
783 * - 256 bytes. If less than 256 bytes are written, the contents of the
784 * unwritten bytes are undefined.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000785 * Warning: This function assumes that @have and @want point to naturally
786 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000787 *
788 * @have buffer with current content
789 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000790 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000791 * @gran write granularity (enum, not count)
792 * @return 0 if no erase is needed, 1 otherwise
793 */
794int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
795{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000796 int result = 0;
797 int i, j, limit;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000798
799 switch (gran) {
800 case write_gran_1bit:
801 for (i = 0; i < len; i++)
802 if ((have[i] & want[i]) != want[i]) {
803 result = 1;
804 break;
805 }
806 break;
807 case write_gran_1byte:
808 for (i = 0; i < len; i++)
809 if ((have[i] != want[i]) && (have[i] != 0xff)) {
810 result = 1;
811 break;
812 }
813 break;
814 case write_gran_256bytes:
815 for (j = 0; j < len / 256; j++) {
816 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000817 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000818 if (!memcmp(have + j * 256, want + j * 256, limit))
819 continue;
820 /* have needs to be in erased state. */
821 for (i = 0; i < limit; i++)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000822 if (have[j * 256 + i] != 0xff) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000823 result = 1;
824 break;
825 }
826 if (result)
827 break;
828 }
829 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000830 default:
831 msg_cerr("%s: Unsupported granularity! Please report a bug at "
832 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000833 }
834 return result;
835}
836
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000837/**
838 * Check if the buffer @have needs to be programmed to get the content of @want.
839 * If yes, return 1 and fill in first_start with the start address of the
840 * write operation and first_len with the length of the first to-be-written
841 * chunk. If not, return 0 and leave first_start and first_len undefined.
842 *
843 * Warning: This function assumes that @have and @want point to naturally
844 * aligned regions.
845 *
846 * @have buffer with current content
847 * @want buffer with desired content
848 * @len length of the checked area
849 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000850 * @first_start offset of the first byte which needs to be written (passed in
851 * value is increased by the offset of the first needed write
852 * relative to have/want or unchanged if no write is needed)
853 * @return length of the first contiguous area which needs to be written
854 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000855 *
856 * FIXME: This function needs a parameter which tells it about coalescing
857 * in relation to the max write length of the programmer and the max write
858 * length of the chip.
859 */
860static int get_next_write(uint8_t *have, uint8_t *want, int len,
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000861 int *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000862{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000863 int need_write = 0, rel_start = 0, first_len = 0;
864 int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000865
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000866 switch (gran) {
867 case write_gran_1bit:
868 case write_gran_1byte:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000869 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000870 break;
871 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000872 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000873 break;
874 default:
875 msg_cerr("%s: Unsupported granularity! Please report a bug at "
876 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000877 /* Claim that no write was needed. A write with unknown
878 * granularity is too dangerous to try.
879 */
880 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000881 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000882 for (i = 0; i < len / stride; i++) {
883 limit = min(stride, len - i * stride);
884 /* Are 'have' and 'want' identical? */
885 if (memcmp(have + i * stride, want + i * stride, limit)) {
886 if (!need_write) {
887 /* First location where have and want differ. */
888 need_write = 1;
889 rel_start = i * stride;
890 }
891 } else {
892 if (need_write) {
893 /* First location where have and want
894 * do not differ anymore.
895 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000896 break;
897 }
898 }
899 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000900 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000901 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000902 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000903 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000904}
905
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000906/* This function generates various test patterns useful for testing controller
907 * and chip communication as well as chip behaviour.
908 *
909 * If a byte can be written multiple times, each time keeping 0-bits at 0
910 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
911 * is essentially an AND operation. That's also the reason why this function
912 * provides the result of AND between various patterns.
913 *
914 * Below is a list of patterns (and their block length).
915 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
916 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
917 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
918 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
919 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
920 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
921 * Pattern 6 is 00 (1 Byte)
922 * Pattern 7 is ff (1 Byte)
923 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
924 * byte block.
925 *
926 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
927 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
928 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
929 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
930 * Pattern 12 is 00 (1 Byte)
931 * Pattern 13 is ff (1 Byte)
932 * Patterns 8-13 have no block number.
933 *
934 * Patterns 0-3 are created to detect and efficiently diagnose communication
935 * slips like missed bits or bytes and their repetitive nature gives good visual
936 * cues to the person inspecting the results. In addition, the following holds:
937 * AND Pattern 0/1 == Pattern 4
938 * AND Pattern 2/3 == Pattern 5
939 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
940 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
941 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
942 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
943 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
944 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
945 * Besides that, they provide for bit testing of the last two bytes of every
946 * 256 byte block which contains the block number for patterns 0-6.
947 * Patterns 10-11 are special purpose for detecting subblock aliasing with
948 * block sizes >256 bytes (some Dataflash chips etc.)
949 * AND Pattern 8/9 == Pattern 12
950 * AND Pattern 10/11 == Pattern 12
951 * Pattern 13 is the completely erased state.
952 * None of the patterns can detect aliasing at boundaries which are a multiple
953 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
954 */
955int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
956{
957 int i;
958
959 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000960 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000961 return 1;
962 }
963
964 switch (variant) {
965 case 0:
966 for (i = 0; i < size; i++)
967 buf[i] = (i & 0xf) << 4 | 0x5;
968 break;
969 case 1:
970 for (i = 0; i < size; i++)
971 buf[i] = (i & 0xf) << 4 | 0xa;
972 break;
973 case 2:
974 for (i = 0; i < size; i++)
975 buf[i] = 0x50 | (i & 0xf);
976 break;
977 case 3:
978 for (i = 0; i < size; i++)
979 buf[i] = 0xa0 | (i & 0xf);
980 break;
981 case 4:
982 for (i = 0; i < size; i++)
983 buf[i] = (i & 0xf) << 4;
984 break;
985 case 5:
986 for (i = 0; i < size; i++)
987 buf[i] = i & 0xf;
988 break;
989 case 6:
990 memset(buf, 0x00, size);
991 break;
992 case 7:
993 memset(buf, 0xff, size);
994 break;
995 case 8:
996 for (i = 0; i < size; i++)
997 buf[i] = i & 0xff;
998 break;
999 case 9:
1000 for (i = 0; i < size; i++)
1001 buf[i] = ~(i & 0xff);
1002 break;
1003 case 10:
1004 for (i = 0; i < size % 2; i++) {
1005 buf[i * 2] = (i >> 8) & 0xff;
1006 buf[i * 2 + 1] = i & 0xff;
1007 }
1008 if (size & 0x1)
1009 buf[i * 2] = (i >> 8) & 0xff;
1010 break;
1011 case 11:
1012 for (i = 0; i < size % 2; i++) {
1013 buf[i * 2] = ~((i >> 8) & 0xff);
1014 buf[i * 2 + 1] = ~(i & 0xff);
1015 }
1016 if (size & 0x1)
1017 buf[i * 2] = ~((i >> 8) & 0xff);
1018 break;
1019 case 12:
1020 memset(buf, 0x00, size);
1021 break;
1022 case 13:
1023 memset(buf, 0xff, size);
1024 break;
1025 }
1026
1027 if ((variant >= 0) && (variant <= 7)) {
1028 /* Write block number in the last two bytes of each 256-byte
1029 * block, big endian for easier reading of the hexdump.
1030 * Note that this wraps around for chips larger than 2^24 bytes
1031 * (16 MB).
1032 */
1033 for (i = 0; i < size / 256; i++) {
1034 buf[i * 256 + 254] = (i >> 8) & 0xff;
1035 buf[i * 256 + 255] = i & 0xff;
1036 }
1037 }
1038
1039 return 0;
1040}
1041
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001042int check_max_decode(enum chipbustype buses, uint32_t size)
1043{
1044 int limitexceeded = 0;
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001045
1046 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001047 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001048 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001049 "size %u kB of chipset/board/programmer "
1050 "for %s interface, "
1051 "probe/read/erase/write may fail. ", size / 1024,
1052 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001053 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001054 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001055 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001056 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001057 "size %u kB of chipset/board/programmer "
1058 "for %s interface, "
1059 "probe/read/erase/write may fail. ", size / 1024,
1060 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001061 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001062 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001063 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001064 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001065 "size %u kB of chipset/board/programmer "
1066 "for %s interface, "
1067 "probe/read/erase/write may fail. ", size / 1024,
1068 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001069 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001070 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001071 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001072 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001073 "size %u kB of chipset/board/programmer "
1074 "for %s interface, "
1075 "probe/read/erase/write may fail. ", size / 1024,
1076 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001077 }
1078 if (!limitexceeded)
1079 return 0;
1080 /* Sometimes chip and programmer have more than one bus in common,
1081 * and the limit is not exceeded on all buses. Tell the user.
1082 */
1083 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001084 /* FIXME: This message is designed towards CLI users. */
Sean Nelson316a29f2010-05-07 20:09:04 +00001085 msg_pdbg("There is at least one common chip/programmer "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001086 "interface which can support a chip of this size. "
1087 "You can try --force at your own risk.\n");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001088 return 1;
1089}
1090
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001091int probe_flash(int startchip, struct flashchip *fill_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001092{
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001093 const struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001094 unsigned long base = 0;
Stefan Reinauer051e2362011-01-19 06:21:54 +00001095 char location[64];
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001096 uint32_t size;
1097 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001098 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001099
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001100 for (flash = flashchips + startchip; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001101 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +00001102 continue;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001103 buses_common = buses_supported & flash->bustype;
1104 if (!buses_common) {
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +00001105 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1106 flash->vendor, flash->name, flash->total_size);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001107 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +00001108 msg_gspew("Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001109 free(tmp);
1110 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +00001111 msg_gspew("and chip bus type %s are incompatible.",
1112 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001113 free(tmp);
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +00001114 msg_gspew("\n");
1115 continue;
1116 }
1117 msg_gdbg("Probing for %s %s, %d kB: ",
1118 flash->vendor, flash->name, flash->total_size);
1119 if (!flash->probe && !force) {
1120 msg_gdbg("failed! flashrom has no probe function for "
1121 "this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001122 continue;
1123 }
Stefan Reinauer70385642007-04-06 11:58:03 +00001124
Ollie Lhocbbf1252004-03-17 22:22:08 +00001125 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001126 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +00001127
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001128 /* Start filling in the dynamic data. */
1129 *fill_flash = *flash;
1130
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +00001131 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001132 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001133
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001134 if (force)
1135 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001136
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001137 if (fill_flash->probe(fill_flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +00001138 goto notfound;
1139
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001140 /* If this is the first chip found, accept it.
1141 * If this is not the first chip found, accept it only if it is
1142 * a non-generic match.
1143 * We could either make chipcount global or provide it as
1144 * parameter, or we assume that startchip==0 means this call to
1145 * probe_flash() is the first one and thus no chip has been
1146 * found before.
1147 */
1148 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001149 break;
1150
Peter Stuge483b8f02008-09-03 23:10:05 +00001151notfound:
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001152 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001153 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001154
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001155 if (!flash || !flash->name)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001156 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001157
Carl-Daniel Hailfingerd9535582011-03-08 00:09:11 +00001158#if CONFIG_INTERNAL == 1
1159 if (programmer_table[programmer].map_flash_region == physmap)
Stefan Reinauer051e2362011-01-19 06:21:54 +00001160 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
Carl-Daniel Hailfingerd9535582011-03-08 00:09:11 +00001161 else
1162#endif
Stefan Reinauer051e2362011-01-19 06:21:54 +00001163 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
Stefan Reinauer051e2362011-01-19 06:21:54 +00001164
Stefan Tauner00155492011-06-26 20:45:35 +00001165 tmp = flashbuses_to_text(flash->bustype);
Stefan Tauner716e0982011-07-25 20:38:52 +00001166 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
Stefan Tauner00155492011-06-26 20:45:35 +00001167 force ? "Assuming" : "Found", fill_flash->vendor,
1168 fill_flash->name, fill_flash->total_size, tmp, location);
1169 free(tmp);
Uwe Hermann9899cad2009-06-28 21:47:57 +00001170
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +00001171 /* Flash registers will not be mapped if the chip was forced. Lock info
1172 * may be stored in registers, so avoid lock info printing.
1173 */
1174 if (!force)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001175 if (fill_flash->printlock)
1176 fill_flash->printlock(fill_flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +00001177
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001178 /* Return position of matching chip. */
1179 return flash - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001180}
1181
Stefan Reinauere3705282005-12-18 16:41:10 +00001182int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001183{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001184 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +00001185 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001186
Sean Nelson316a29f2010-05-07 20:09:04 +00001187 msg_cinfo("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +00001188
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001189 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001190
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001191 if (!ret)
Sean Nelson316a29f2010-05-07 20:09:04 +00001192 msg_cinfo("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001193
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001194 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001195}
1196
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001197int read_buf_from_file(unsigned char *buf, unsigned long size,
1198 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001199{
1200 unsigned long numbytes;
1201 FILE *image;
1202 struct stat image_stat;
1203
1204 if ((image = fopen(filename, "rb")) == NULL) {
1205 perror(filename);
1206 return 1;
1207 }
1208 if (fstat(fileno(image), &image_stat) != 0) {
1209 perror(filename);
1210 fclose(image);
1211 return 1;
1212 }
1213 if (image_stat.st_size != size) {
1214 msg_gerr("Error: Image size doesn't match\n");
1215 fclose(image);
1216 return 1;
1217 }
1218 numbytes = fread(buf, 1, size, image);
1219 if (fclose(image)) {
1220 perror(filename);
1221 return 1;
1222 }
1223 if (numbytes != size) {
1224 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1225 "wanted %ld!\n", numbytes, size);
1226 return 1;
1227 }
1228 return 0;
1229}
1230
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001231int write_buf_to_file(unsigned char *buf, unsigned long size,
1232 const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001233{
1234 unsigned long numbytes;
1235 FILE *image;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001236
1237 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001238 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001239 return 1;
1240 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001241 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001242 perror(filename);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001243 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001244 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001245
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001246 numbytes = fwrite(buf, 1, size, image);
1247 fclose(image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001248 if (numbytes != size) {
1249 msg_gerr("File %s could not be written completely.\n",
1250 filename);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001251 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001252 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001253 return 0;
1254}
1255
Stefan Tauner66652442011-06-26 17:38:17 +00001256int read_flash_to_file(struct flashchip *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001257{
1258 unsigned long size = flash->total_size * 1024;
1259 unsigned char *buf = calloc(size, sizeof(char));
1260 int ret = 0;
1261
1262 msg_cinfo("Reading flash... ");
1263 if (!buf) {
1264 msg_gerr("Memory allocation failed!\n");
1265 msg_cinfo("FAILED.\n");
1266 return 1;
1267 }
1268 if (!flash->read) {
1269 msg_cerr("No read function available for this flash chip.\n");
1270 ret = 1;
1271 goto out_free;
1272 }
1273 if (flash->read(flash, buf, 0, size)) {
1274 msg_cerr("Read operation failed!\n");
1275 ret = 1;
1276 goto out_free;
1277 }
1278
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001279 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001280out_free:
1281 free(buf);
1282 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1283 return ret;
1284}
1285
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001286/* This function shares a lot of its structure with erase_and_write_flash() and
1287 * walk_eraseregions().
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001288 * Even if an error is found, the function will keep going and check the rest.
1289 */
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001290static int selfcheck_eraseblocks(const struct flashchip *flash)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001291{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001292 int i, j, k;
1293 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001294
1295 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1296 unsigned int done = 0;
1297 struct block_eraser eraser = flash->block_erasers[k];
1298
1299 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1300 /* Blocks with zero size are bugs in flashchips.c. */
1301 if (eraser.eraseblocks[i].count &&
1302 !eraser.eraseblocks[i].size) {
1303 msg_gerr("ERROR: Flash chip %s erase function "
1304 "%i region %i has size 0. Please report"
1305 " a bug at flashrom@flashrom.org\n",
1306 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001307 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001308 }
1309 /* Blocks with zero count are bugs in flashchips.c. */
1310 if (!eraser.eraseblocks[i].count &&
1311 eraser.eraseblocks[i].size) {
1312 msg_gerr("ERROR: Flash chip %s erase function "
1313 "%i region %i has count 0. Please report"
1314 " a bug at flashrom@flashrom.org\n",
1315 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001316 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001317 }
1318 done += eraser.eraseblocks[i].count *
1319 eraser.eraseblocks[i].size;
1320 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001321 /* Empty eraseblock definition with erase function. */
1322 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001323 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001324 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001325 if (!done)
1326 continue;
1327 if (done != flash->total_size * 1024) {
1328 msg_gerr("ERROR: Flash chip %s erase function %i "
1329 "region walking resulted in 0x%06x bytes total,"
1330 " expected 0x%06x bytes. Please report a bug at"
1331 " flashrom@flashrom.org\n", flash->name, k,
1332 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001333 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001334 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001335 if (!eraser.block_erase)
1336 continue;
1337 /* Check if there are identical erase functions for different
1338 * layouts. That would imply "magic" erase functions. The
1339 * easiest way to check this is with function pointers.
1340 */
Uwe Hermann43959702010-03-13 17:28:29 +00001341 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001342 if (eraser.block_erase ==
1343 flash->block_erasers[j].block_erase) {
1344 msg_gerr("ERROR: Flash chip %s erase function "
1345 "%i and %i are identical. Please report"
1346 " a bug at flashrom@flashrom.org\n",
1347 flash->name, k, j);
1348 ret = 1;
1349 }
Uwe Hermann43959702010-03-13 17:28:29 +00001350 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001351 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001352 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001353}
1354
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001355static int erase_and_write_block_helper(struct flashchip *flash,
1356 unsigned int start, unsigned int len,
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001357 uint8_t *curcontents,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001358 uint8_t *newcontents,
1359 int (*erasefn) (struct flashchip *flash,
1360 unsigned int addr,
1361 unsigned int len))
1362{
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001363 int starthere = 0, lenhere = 0, ret = 0, skip = 1, writecount = 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001364 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1365
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001366 /* curcontents and newcontents are opaque to walk_eraseregions, and
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001367 * need to be adjusted here to keep the impression of proper abstraction
1368 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001369 curcontents += start;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001370 newcontents += start;
1371 msg_cdbg(":");
1372 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001373 if (need_erase(curcontents, newcontents, len, gran)) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001374 msg_cdbg("E");
1375 ret = erasefn(flash, start, len);
1376 if (ret)
1377 return ret;
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +00001378 if (check_erased_range(flash, start, len)) {
1379 msg_cerr("ERASE FAILED!\n");
1380 return -1;
1381 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001382 /* Erase was successful. Adjust curcontents. */
1383 memset(curcontents, 0xff, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001384 skip = 0;
1385 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001386 /* get_next_write() sets starthere to a new value after the call. */
1387 while ((lenhere = get_next_write(curcontents + starthere,
1388 newcontents + starthere,
1389 len - starthere, &starthere, gran))) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001390 if (!writecount++)
1391 msg_cdbg("W");
1392 /* Needs the partial write function signature. */
1393 ret = flash->write(flash, newcontents + starthere,
1394 start + starthere, lenhere);
1395 if (ret)
1396 return ret;
1397 starthere += lenhere;
1398 skip = 0;
1399 }
1400 if (skip)
1401 msg_cdbg("S");
1402 return ret;
1403}
1404
Carl-Daniel Hailfinger4d3e9ca2010-07-13 00:42:00 +00001405static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1406 int (*do_something) (struct flashchip *flash,
1407 unsigned int addr,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001408 unsigned int len,
1409 uint8_t *param1,
1410 uint8_t *param2,
1411 int (*erasefn) (
1412 struct flashchip *flash,
1413 unsigned int addr,
1414 unsigned int len)),
1415 void *param1, void *param2)
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001416{
1417 int i, j;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001418 unsigned int start = 0;
1419 unsigned int len;
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001420 struct block_eraser eraser = flash->block_erasers[erasefunction];
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001421
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001422 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1423 /* count==0 for all automatically initialized array
1424 * members so the loop below won't be executed for them.
1425 */
1426 len = eraser.eraseblocks[i].size;
1427 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001428 /* Print this for every block except the first one. */
1429 if (i || j)
1430 msg_cdbg(", ");
1431 msg_cdbg("0x%06x-0x%06x", start,
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001432 start + len - 1);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001433 if (do_something(flash, start, len, param1, param2,
1434 eraser.block_erase)) {
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001435 return 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001436 }
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001437 start += len;
1438 }
1439 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001440 msg_cdbg("\n");
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001441 return 0;
1442}
1443
Stefan Tauner5368dca2011-07-01 00:19:12 +00001444static int check_block_eraser(const struct flashchip *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001445{
1446 struct block_eraser eraser = flash->block_erasers[k];
1447
1448 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1449 if (log)
1450 msg_cdbg("not defined. ");
1451 return 1;
1452 }
1453 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1454 if (log)
1455 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001456 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001457 return 1;
1458 }
1459 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1460 if (log)
1461 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001462 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001463 return 1;
1464 }
1465 return 0;
1466}
1467
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001468int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1469 uint8_t *newcontents)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001470{
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001471 int k, ret = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001472 uint8_t *curcontents;
1473 unsigned long size = flash->total_size * 1024;
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001474 unsigned int usable_erasefunctions = count_usable_erasers(flash);
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001475
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001476 msg_cinfo("Erasing and writing flash chip... ");
Stefan Tauner269de352011-07-12 22:35:21 +00001477 curcontents = malloc(size);
1478 if (!curcontents) {
1479 msg_gerr("Out of memory!\n");
1480 exit(1);
1481 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001482 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1483 memcpy(curcontents, oldcontents, size);
1484
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001485 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001486 if (k != 0)
1487 msg_cdbg("Looking for another erase function.\n");
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001488 if (!usable_erasefunctions) {
1489 msg_cdbg("No usable erase functions left.\n");
1490 break;
1491 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001492 msg_cdbg("Trying erase function %i... ", k);
1493 if (check_block_eraser(flash, k, 1))
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001494 continue;
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001495 usable_erasefunctions--;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001496 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1497 curcontents, newcontents);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001498 /* If everything is OK, don't try another erase function. */
1499 if (!ret)
1500 break;
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001501 /* Write/erase failed, so try to find out what the current chip
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001502 * contents are. If no usable erase functions remain, we can
1503 * skip this: the next iteration will break immediately anyway.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001504 */
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001505 if (!usable_erasefunctions)
1506 continue;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001507 /* Reading the whole chip may take a while, inform the user even
1508 * in non-verbose mode.
1509 */
1510 msg_cinfo("Reading current flash chip contents... ");
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001511 if (flash->read(flash, curcontents, 0, size)) {
1512 /* Now we are truly screwed. Read failed as well. */
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001513 msg_cerr("Can't read anymore! Aborting.\n");
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001514 /* We have no idea about the flash chip contents, so
1515 * retrying with another erase function is pointless.
1516 */
1517 break;
1518 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001519 msg_cinfo("done. ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001520 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001521 /* Free the scratchpad. */
1522 free(curcontents);
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001523
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001524 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001525 msg_cerr("FAILED!\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001526 } else {
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001527 msg_cinfo("Erase/write done.\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001528 }
1529 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001530}
1531
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001532void nonfatal_help_message(void)
1533{
1534 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1535 "This means we have to add special support for your board, "
1536 "programmer or flash chip.\n"
1537 "Please report this on IRC at irc.freenode.net (channel "
1538 "#flashrom) or\n"
1539 "mail flashrom@flashrom.org!\n"
1540 "-------------------------------------------------------------"
1541 "------------------\n"
1542 "You may now reboot or simply leave the machine running.\n");
1543}
1544
Uwe Hermannc67d0372009-10-01 18:40:02 +00001545void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001546{
Sean Nelson316a29f2010-05-07 20:09:04 +00001547 msg_gerr("Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001548 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
Paul Menzelab6328f2010-10-08 11:03:02 +00001549 "mail flashrom@flashrom.org with FAILED: your board name in "
1550 "the subject line!\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001551 "-------------------------------------------------------------"
1552 "------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001553 "DO NOT REBOOT OR POWEROFF!\n");
1554}
1555
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001556/* The way to go if you want a delimited list of programmers */
Stefan Tauner66652442011-06-26 17:38:17 +00001557void list_programmers(const char *delim)
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001558{
1559 enum programmer p;
1560 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001561 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001562 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001563 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001564 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001565 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001566}
1567
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001568void list_programmers_linebreak(int startcol, int cols, int paren)
1569{
1570 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001571 int pnamelen;
1572 int remaining = 0, firstline = 1;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001573 enum programmer p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001574 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001575
1576 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1577 pname = programmer_table[p].name;
1578 pnamelen = strlen(pname);
1579 if (remaining - pnamelen - 2 < 0) {
1580 if (firstline)
1581 firstline = 0;
1582 else
1583 printf("\n");
1584 for (i = 0; i < startcol; i++)
1585 printf(" ");
1586 remaining = cols - startcol;
1587 } else {
1588 printf(" ");
1589 remaining--;
1590 }
1591 if (paren && (p == 0)) {
1592 printf("(");
1593 remaining--;
1594 }
1595 printf("%s", pname);
1596 remaining -= pnamelen;
1597 if (p < PROGRAMMER_INVALID - 1) {
1598 printf(",");
1599 remaining--;
1600 } else {
1601 if (paren)
1602 printf(")");
1603 printf("\n");
1604 }
1605 }
1606}
1607
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001608void print_sysinfo(void)
1609{
1610#if HAVE_UTSNAME == 1
1611 struct utsname osinfo;
1612 uname(&osinfo);
1613
1614 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1615 osinfo.machine);
1616#else
1617 msg_ginfo(" on unknown machine");
1618#endif
1619 msg_ginfo(", built with");
1620#if NEED_PCI == 1
1621#ifdef PCILIB_VERSION
1622 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1623#else
1624 msg_ginfo(" unknown PCI library,");
1625#endif
1626#endif
1627#ifdef __clang__
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001628 msg_ginfo(" LLVM Clang");
1629#ifdef __clang_version__
1630 msg_ginfo(" %s,", __clang_version__);
1631#else
1632 msg_ginfo(" unknown version (before r102686),");
1633#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001634#elif defined(__GNUC__)
1635 msg_ginfo(" GCC");
1636#ifdef __VERSION__
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001637 msg_ginfo(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001638#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001639 msg_ginfo(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001640#endif
1641#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001642 msg_ginfo(" unknown compiler,");
1643#endif
1644#if defined (__FLASHROM_LITTLE_ENDIAN__)
1645 msg_ginfo(" little endian");
1646#else
1647 msg_ginfo(" big endian");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001648#endif
1649 msg_ginfo("\n");
1650}
1651
Bernhard Walle201bde32008-01-21 15:24:22 +00001652void print_version(void)
1653{
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001654 msg_ginfo("flashrom v%s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001655 print_sysinfo();
Bernhard Walle201bde32008-01-21 15:24:22 +00001656}
1657
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001658void print_banner(void)
1659{
1660 msg_ginfo("flashrom is free software, get the source code at "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001661 "http://www.flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001662 msg_ginfo("\n");
1663}
1664
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001665int selfcheck(void)
1666{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001667 int ret = 0;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001668 const struct flashchip *flash;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001669
1670 /* Safety check. Instead of aborting after the first error, check
1671 * if more errors exist.
1672 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001673 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001674 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001675 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001676 }
Stefan Tauner93f70232011-07-26 14:33:46 +00001677 /* It would be favorable if we could also check for correct termination
Stefan Tauner716e0982011-07-25 20:38:52 +00001678 * of the following arrays, but we don't know their sizes in here...
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001679 * For 'flashchips' we check the first element to be non-null. In the
1680 * other cases there exist use cases where the first element can be
1681 * null. */
1682 if (flashchips == NULL || flashchips[0].vendor == NULL) {
1683 msg_gerr("Flashchips table miscompilation!\n");
1684 ret = 1;
1685 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001686 for (flash = flashchips; flash && flash->name; flash++)
1687 if (selfcheck_eraseblocks(flash))
1688 ret = 1;
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001689
1690#if CONFIG_INTERNAL == 1
1691 if (chipset_enables == NULL) {
1692 msg_gerr("Chipset enables table does not exist!\n");
1693 ret = 1;
1694 }
Carl-Daniel Hailfinger97d5b122011-08-31 16:19:50 +00001695 if (board_matches == NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001696 msg_gerr("Board enables table does not exist!\n");
1697 ret = 1;
1698 }
1699 if (boards_known == NULL) {
1700 msg_gerr("Known boards table does not exist!\n");
1701 ret = 1;
1702 }
1703 if (laptops_known == NULL) {
1704 msg_gerr("Known laptops table does not exist!\n");
1705 ret = 1;
1706 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001707#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001708 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001709}
1710
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001711void check_chip_supported(const struct flashchip *flash)
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001712{
1713 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001714 msg_cinfo("===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001715 if (flash->tested & TEST_BAD_MASK) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001716 msg_cinfo("This flash part has status NOT WORKING for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001717 if (flash->tested & TEST_BAD_PROBE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001718 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001719 if (flash->tested & TEST_BAD_READ)
Sean Nelson316a29f2010-05-07 20:09:04 +00001720 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001721 if (flash->tested & TEST_BAD_ERASE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001722 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001723 if (flash->tested & TEST_BAD_WRITE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001724 msg_cinfo(" WRITE");
1725 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001726 }
1727 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1728 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1729 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1730 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001731 msg_cinfo("This flash part has status UNTESTED for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001732 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001733 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001734 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
Sean Nelson316a29f2010-05-07 20:09:04 +00001735 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001736 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001737 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001738 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001739 msg_cinfo(" WRITE");
1740 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001741 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001742 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001743 msg_cinfo("The test status of this chip may have been updated "
1744 "in the latest development\n"
1745 "version of flashrom. If you are running the latest "
1746 "development version,\n"
1747 "please email a report to flashrom@flashrom.org if "
1748 "any of the above operations\n"
1749 "work correctly for you with this flash part. Please "
1750 "include the flashrom\n"
1751 "output with the additional -V option for all "
1752 "operations you tested (-V, -Vr,\n"
1753 "-Vw, -VE), and mention which mainboard or "
1754 "programmer you tested.\n"
Paul Menzelab6328f2010-10-08 11:03:02 +00001755 "Please mention your board in the subject line. "
1756 "Thanks for your help!\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001757 }
1758}
1759
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001760/* FIXME: This function signature needs to be improved once doit() has a better
1761 * function signature.
1762 */
David Hendricks77f931a2011-05-18 01:30:56 +00001763int chip_safety_check(struct flashchip *flash, int force, int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001764{
1765 if (!programmer_may_write && (write_it || erase_it)) {
1766 msg_perr("Write/erase is not working yet on your programmer in "
1767 "its current configuration.\n");
1768 /* --force is the wrong approach, but it's the best we can do
1769 * until the generic programmer parameter parser is merged.
1770 */
1771 if (!force)
1772 return 1;
1773 msg_cerr("Continuing anyway.\n");
1774 }
1775
1776 if (read_it || erase_it || write_it || verify_it) {
1777 /* Everything needs read. */
1778 if (flash->tested & TEST_BAD_READ) {
1779 msg_cerr("Read is not working on this chip. ");
1780 if (!force)
1781 return 1;
1782 msg_cerr("Continuing anyway.\n");
1783 }
1784 if (!flash->read) {
1785 msg_cerr("flashrom has no read function for this "
1786 "flash chip.\n");
1787 return 1;
1788 }
1789 }
1790 if (erase_it || write_it) {
1791 /* Write needs erase. */
1792 if (flash->tested & TEST_BAD_ERASE) {
1793 msg_cerr("Erase is not working on this chip. ");
1794 if (!force)
1795 return 1;
1796 msg_cerr("Continuing anyway.\n");
1797 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001798 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001799 msg_cerr("flashrom has no erase function for this "
1800 "flash chip.\n");
1801 return 1;
1802 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001803 }
1804 if (write_it) {
1805 if (flash->tested & TEST_BAD_WRITE) {
1806 msg_cerr("Write is not working on this chip. ");
1807 if (!force)
1808 return 1;
1809 msg_cerr("Continuing anyway.\n");
1810 }
1811 if (!flash->write) {
1812 msg_cerr("flashrom has no write function for this "
1813 "flash chip.\n");
1814 return 1;
1815 }
1816 }
1817 return 0;
1818}
1819
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001820/* This function signature is horrible. We need to design a better interface,
1821 * but right now it allows us to split off the CLI code.
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001822 * Besides that, the function itself is a textbook example of abysmal code flow.
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001823 */
Stefan Tauner66652442011-06-26 17:38:17 +00001824int doit(struct flashchip *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001825{
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001826 uint8_t *oldcontents;
1827 uint8_t *newcontents;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001828 int ret = 0;
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001829 unsigned long size = flash->total_size * 1024;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001830
David Hendricks77f931a2011-05-18 01:30:56 +00001831 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001832 msg_cerr("Aborting.\n");
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001833 ret = 1;
1834 goto out_nofree;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001835 }
1836
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001837 /* Given the existence of read locks, we want to unlock for read,
1838 * erase and write.
1839 */
1840 if (flash->unlock)
1841 flash->unlock(flash);
1842
1843 if (read_it) {
1844 ret = read_flash_to_file(flash, filename);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001845 goto out_nofree;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +00001846 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001847
Stefan Tauner269de352011-07-12 22:35:21 +00001848 oldcontents = malloc(size);
1849 if (!oldcontents) {
1850 msg_gerr("Out of memory!\n");
1851 exit(1);
1852 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001853 /* Assume worst case: All bits are 0. */
1854 memset(oldcontents, 0x00, size);
Stefan Tauner269de352011-07-12 22:35:21 +00001855 newcontents = malloc(size);
1856 if (!newcontents) {
1857 msg_gerr("Out of memory!\n");
1858 exit(1);
1859 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001860 /* Assume best case: All bits should be 1. */
1861 memset(newcontents, 0xff, size);
1862 /* Side effect of the assumptions above: Default write action is erase
1863 * because newcontents looks like a completely erased chip, and
1864 * oldcontents being completely 0x00 means we have to erase everything
1865 * before we can write.
1866 */
1867
Ollie Lhoefa28582004-12-08 20:10:01 +00001868 if (erase_it) {
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001869 /* FIXME: Do we really want the scary warning if erase failed?
1870 * After all, after erase the chip is either blank or partially
1871 * blank or it has the old contents. A blank chip won't boot,
1872 * so if the user wanted erase and reboots afterwards, the user
1873 * knows very well that booting won't work.
1874 */
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001875 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001876 emergency_help_message();
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001877 ret = 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001878 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001879 goto out;
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001880 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001881
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001882 if (write_it || verify_it) {
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001883 if (read_buf_from_file(newcontents, size, filename)) {
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001884 ret = 1;
1885 goto out;
Stefan Reinauer018aca82006-11-21 23:48:51 +00001886 }
1887
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001888#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001889 if (programmer == PROGRAMMER_INTERNAL)
1890 show_id(newcontents, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001891#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001892 }
1893
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001894 /* Read the whole chip to be able to check whether regions need to be
1895 * erased and to give better diagnostics in case write fails.
1896 * The alternative would be to read only the regions which are to be
1897 * preserved, but in that case we might perform unneeded erase which
1898 * takes time as well.
1899 */
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001900 msg_cinfo("Reading old flash chip contents... ");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001901 if (flash->read(flash, oldcontents, 0, size)) {
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001902 ret = 1;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001903 msg_cinfo("FAILED.\n");
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001904 goto out;
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001905 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001906 msg_cinfo("done.\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001907
Ollie Lho184a4042005-11-26 21:55:36 +00001908 // This should be moved into each flash part's code to do it
1909 // cleanly. This does the job.
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001910 handle_romentries(flash, oldcontents, newcontents);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001911
Ollie Lho184a4042005-11-26 21:55:36 +00001912 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001913
Peter Stugef31104c2008-04-28 14:47:30 +00001914 if (write_it) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001915 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1916 msg_cerr("Uh oh. Erase/write failed. Checking if "
1917 "anything changed.\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001918 if (!flash->read(flash, newcontents, 0, size)) {
1919 if (!memcmp(oldcontents, newcontents, size)) {
1920 msg_cinfo("Good. It seems nothing was "
1921 "changed.\n");
1922 nonfatal_help_message();
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001923 ret = 1;
1924 goto out;
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001925 }
1926 }
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001927 emergency_help_message();
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001928 ret = 1;
1929 goto out;
Peter Stugef31104c2008-04-28 14:47:30 +00001930 }
Peter Stugef31104c2008-04-28 14:47:30 +00001931 }
Ollie Lho184a4042005-11-26 21:55:36 +00001932
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001933 if (verify_it) {
1934 /* Work around chips which need some time to calm down. */
1935 if (write_it)
1936 programmer_delay(1000*1000);
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001937 ret = verify_flash(flash, newcontents);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001938 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001939 * might have an emergency situation.
1940 */
1941 if (ret && write_it)
1942 emergency_help_message();
1943 }
Ollie Lho184a4042005-11-26 21:55:36 +00001944
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001945out:
1946 free(oldcontents);
1947 free(newcontents);
1948out_nofree:
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001949 programmer_shutdown();
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001950 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001951}