blob: 1fd3bae791df4b464ed35e8a65792d1da6c3d1b9 [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 Hailfingerca8bfc62009-06-05 17:48:08 +000071 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000072 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000073#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000074
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000075#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000076 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000077 .name = "dummy",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000078 .init = dummy_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000079 .map_flash_region = dummy_map,
80 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000081 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000082 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000083#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000084
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000085#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +000086 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000087 .name = "nic3com",
Uwe Hermannb4dcb712009-05-13 11:36:06 +000088 .init = nic3com_init,
Uwe Hermannc6915932009-05-17 23:12:17 +000089 .map_flash_region = fallback_map,
90 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000091 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000092 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000093#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +000094
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000095#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +000096 {
Carl-Daniel Hailfinger40446ee2011-03-07 01:08:09 +000097 /* This programmer works for Realtek RTL8139 and SMC 1211. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +000098 .name = "nicrealtek",
99 //.name = "nicsmc1211",
100 .init = nicrealtek_init,
101 .map_flash_region = fallback_map,
102 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000103 .delay = internal_delay,
Joerg Fischer5665ef32010-05-21 21:54:07 +0000104 },
Joerg Fischer5665ef32010-05-21 21:54:07 +0000105#endif
106
Andrew Morganc29c2e72010-06-07 22:37:54 +0000107#if CONFIG_NICNATSEMI == 1
108 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000109 .name = "nicnatsemi",
110 .init = nicnatsemi_init,
111 .map_flash_region = fallback_map,
112 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000113 .delay = internal_delay,
Andrew Morganc29c2e72010-06-07 22:37:54 +0000114 },
115#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000116
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000117#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000118 {
119 .name = "gfxnvidia",
120 .init = gfxnvidia_init,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000121 .map_flash_region = fallback_map,
122 .unmap_flash_region = fallback_unmap,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000123 .delay = internal_delay,
124 },
125#endif
126
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000127#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000128 {
TURBO Jb0912c02009-09-02 23:00:46 +0000129 .name = "drkaiser",
130 .init = drkaiser_init,
TURBO Jb0912c02009-09-02 23:00:46 +0000131 .map_flash_region = fallback_map,
132 .unmap_flash_region = fallback_unmap,
TURBO Jb0912c02009-09-02 23:00:46 +0000133 .delay = internal_delay,
134 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000135#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000136
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000137#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000138 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000139 .name = "satasii",
Rudolf Marek68720c72009-05-17 19:39:27 +0000140 .init = satasii_init,
Uwe Hermannc6915932009-05-17 23:12:17 +0000141 .map_flash_region = fallback_map,
142 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000143 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000144 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000145#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000146
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000147#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000148 {
149 .name = "atahpt",
150 .init = atahpt_init,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000151 .map_flash_region = fallback_map,
152 .unmap_flash_region = fallback_unmap,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000153 .delay = internal_delay,
154 },
155#endif
156
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000157#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000158 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000159 .name = "ft2232_spi",
Paul Fox05dfbe62009-06-16 21:08:06 +0000160 .init = ft2232_spi_init,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000161 .map_flash_region = fallback_map,
162 .unmap_flash_region = fallback_unmap,
Paul Fox05dfbe62009-06-16 21:08:06 +0000163 .delay = internal_delay,
164 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000165#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000166
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000167#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000168 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000169 .name = "serprog",
Urja Rannikko22915352009-06-23 11:33:43 +0000170 .init = serprog_init,
Urja Rannikko22915352009-06-23 11:33:43 +0000171 .map_flash_region = fallback_map,
172 .unmap_flash_region = fallback_unmap,
Urja Rannikko22915352009-06-23 11:33:43 +0000173 .delay = serprog_delay,
174 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000175#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000176
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000177#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000178 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000179 .name = "buspirate_spi",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000180 .init = buspirate_spi_init,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000181 .map_flash_region = fallback_map,
182 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000183 .delay = internal_delay,
184 },
185#endif
186
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000187#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000188 {
189 .name = "dediprog",
190 .init = dediprog_init,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000191 .map_flash_region = fallback_map,
192 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000193 .delay = internal_delay,
194 },
195#endif
196
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000197#if CONFIG_RAYER_SPI == 1
198 {
199 .name = "rayer_spi",
200 .init = rayer_spi_init,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000201 .map_flash_region = fallback_map,
202 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000203 .delay = internal_delay,
204 },
205#endif
206
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000207#if CONFIG_NICINTEL == 1
208 {
209 .name = "nicintel",
210 .init = nicintel_init,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000211 .map_flash_region = fallback_map,
212 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000213 .delay = internal_delay,
214 },
215#endif
216
Idwer Vollering004f4b72010-09-03 18:21:21 +0000217#if CONFIG_NICINTEL_SPI == 1
218 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000219 .name = "nicintel_spi",
220 .init = nicintel_spi_init,
221 .map_flash_region = fallback_map,
222 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000223 .delay = internal_delay,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000224 },
225#endif
226
Mark Marshall90021f22010-12-03 14:48:11 +0000227#if CONFIG_OGP_SPI == 1
228 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000229 .name = "ogp_spi",
230 .init = ogp_spi_init,
231 .map_flash_region = fallback_map,
232 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000233 .delay = internal_delay,
Mark Marshall90021f22010-12-03 14:48:11 +0000234 },
235#endif
236
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000237#if CONFIG_SATAMV == 1
238 {
239 .name = "satamv",
240 .init = satamv_init,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000241 .map_flash_region = fallback_map,
242 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000243 .delay = internal_delay,
244 },
245#endif
246
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000247#if CONFIG_LINUX_SPI == 1
248 {
249 .name = "linux_spi",
250 .init = linux_spi_init,
251 .map_flash_region = fallback_map,
252 .unmap_flash_region = fallback_unmap,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000253 .delay = internal_delay,
254 },
255#endif
256
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000257 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000258};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000259
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +0000260#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000261static int shutdown_fn_count = 0;
262struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +0000263 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000264 void *data;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000265} static shutdown_fn[SHUTDOWN_MAXFN];
266/* Initialize to 0 to make sure nobody registers a shutdown function before
267 * programmer init.
268 */
269static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000270
Stefan Tauner5368dca2011-07-01 00:19:12 +0000271static int check_block_eraser(const struct flashchip *flash, int k, int log);
272
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000273/* Register a function to be executed on programmer shutdown.
274 * The advantage over atexit() is that you can supply a void pointer which will
275 * be used as parameter to the registered function upon programmer shutdown.
276 * This pointer can point to arbitrary data used by said function, e.g. undo
277 * information for GPIO settings etc. If unneeded, set data=NULL.
278 * Please note that the first (void *data) belongs to the function signature of
279 * the function passed as first parameter.
280 */
David Hendricks8bb20212011-06-14 01:35:36 +0000281int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000282{
283 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000284 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000285 SHUTDOWN_MAXFN);
286 return 1;
287 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000288 if (!may_register_shutdown) {
289 msg_perr("Tried to register a shutdown function before "
290 "programmer init.\n");
291 return 1;
292 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000293 shutdown_fn[shutdown_fn_count].func = function;
294 shutdown_fn[shutdown_fn_count].data = data;
295 shutdown_fn_count++;
296
297 return 0;
298}
299
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000300int programmer_init(enum programmer prog, char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000301{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000302 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000303
304 if (prog >= PROGRAMMER_INVALID) {
305 msg_perr("Invalid programmer specified!\n");
306 return -1;
307 }
308 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000309 /* Initialize all programmer specific data. */
310 /* Default to unlimited decode sizes. */
311 max_rom_decode = (const struct decode_sizes) {
312 .parallel = 0xffffffff,
313 .lpc = 0xffffffff,
314 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000315 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000316 };
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000317 buses_supported = BUS_NONE;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000318 /* Default to top aligned flash at 4 GB. */
319 flashbase = 0;
320 /* Registering shutdown functions is now allowed. */
321 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000322 /* Default to allowing writes. Broken programmers set this to 0. */
323 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000324
325 programmer_param = param;
326 msg_pdbg("Initializing %s programmer\n",
327 programmer_table[programmer].name);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000328 ret = programmer_table[programmer].init();
329 if (programmer_param && strlen(programmer_param)) {
330 msg_perr("Unhandled programmer parameters: %s\n",
331 programmer_param);
332 /* Do not error out here, the init itself was successful. */
333 }
334 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000335}
336
337int programmer_shutdown(void)
338{
David Hendricks8bb20212011-06-14 01:35:36 +0000339 int ret = 0;
340
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000341 /* Registering shutdown functions is no longer allowed. */
342 may_register_shutdown = 0;
343 while (shutdown_fn_count > 0) {
344 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000345 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000346 }
David Hendricks8bb20212011-06-14 01:35:36 +0000347 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000348}
349
350void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
351 size_t len)
352{
353 return programmer_table[programmer].map_flash_region(descr,
354 phys_addr, len);
355}
356
357void programmer_unmap_flash_region(void *virt_addr, size_t len)
358{
359 programmer_table[programmer].unmap_flash_region(virt_addr, len);
360}
361
362void chip_writeb(uint8_t val, chipaddr addr)
363{
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000364 par_programmer->chip_writeb(val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000365}
366
367void chip_writew(uint16_t val, chipaddr addr)
368{
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000369 par_programmer->chip_writew(val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000370}
371
372void chip_writel(uint32_t val, chipaddr addr)
373{
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000374 par_programmer->chip_writel(val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000375}
376
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000377void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
378{
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000379 par_programmer->chip_writen(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000380}
381
Uwe Hermann09e04f72009-05-16 22:36:00 +0000382uint8_t chip_readb(const chipaddr addr)
383{
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000384 return par_programmer->chip_readb(addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000385}
386
387uint16_t chip_readw(const chipaddr addr)
388{
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000389 return par_programmer->chip_readw(addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000390}
391
392uint32_t chip_readl(const chipaddr addr)
393{
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000394 return par_programmer->chip_readl(addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000395}
396
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000397void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
398{
Carl-Daniel Hailfingereaacd2d2011-11-09 23:40:00 +0000399 par_programmer->chip_readn(buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000400}
401
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000402void programmer_delay(int usecs)
403{
404 programmer_table[programmer].delay(usecs);
405}
406
Peter Stuge776d2022009-01-26 00:39:57 +0000407void map_flash_registers(struct flashchip *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000408{
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000409 size_t size = flash->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000410 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000411 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000412 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000413}
414
Stefan Tauner8c357452011-09-18 22:42:18 +0000415int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000416{
Carl-Daniel Hailfingercbf563c2009-06-16 08:55:44 +0000417 chip_readn(buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000418
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000419 return 0;
420}
421
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000422int min(int a, int b)
423{
424 return (a < b) ? a : b;
425}
426
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000427int max(int a, int b)
428{
429 return (a > b) ? a : b;
430}
431
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000432int bitcount(unsigned long a)
433{
434 int i = 0;
435 for (; a != 0; a >>= 1)
436 if (a & 1)
437 i++;
438 return i;
439}
440
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000441void tolower_string(char *str)
442{
443 for (; *str != '\0'; str++)
444 *str = (char)tolower((unsigned char)*str);
445}
446
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000447char *strcat_realloc(char *dest, const char *src)
448{
449 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000450 if (!dest) {
451 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000452 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000453 }
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000454 strcat(dest, src);
455 return dest;
456}
457
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000458/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000459 * It will look for needle with a subsequent '=' in haystack, return a copy of
460 * needle and remove everything from the first occurrence of needle to the next
461 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000462 */
Stefan Tauner66652442011-06-26 17:38:17 +0000463char *extract_param(char **haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000464{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000465 char *param_pos, *opt_pos, *rest;
466 char *opt = NULL;
467 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000468 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000469
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000470 needlelen = strlen(needle);
471 if (!needlelen) {
472 msg_gerr("%s: empty needle! Please report a bug at "
473 "flashrom@flashrom.org\n", __func__);
474 return NULL;
475 }
476 /* No programmer parameters given. */
477 if (*haystack == NULL)
478 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000479 param_pos = strstr(*haystack, needle);
480 do {
481 if (!param_pos)
482 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000483 /* Needle followed by '='? */
484 if (param_pos[needlelen] == '=') {
485
486 /* Beginning of the string? */
487 if (param_pos == *haystack)
488 break;
489 /* After a delimiter? */
490 if (strchr(delim, *(param_pos - 1)))
491 break;
492 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000493 /* Continue searching. */
494 param_pos++;
495 param_pos = strstr(param_pos, needle);
496 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000497
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000498 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000499 /* Get the string after needle and '='. */
500 opt_pos = param_pos + needlelen + 1;
501 optlen = strcspn(opt_pos, delim);
502 /* Return an empty string if the parameter was empty. */
503 opt = malloc(optlen + 1);
504 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000505 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000506 exit(1);
507 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000508 strncpy(opt, opt_pos, optlen);
509 opt[optlen] = '\0';
510 rest = opt_pos + optlen;
511 /* Skip all delimiters after the current parameter. */
512 rest += strspn(rest, delim);
513 memmove(param_pos, rest, strlen(rest) + 1);
514 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000515 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000516
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000517 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000518}
519
Stefan Tauner66652442011-06-26 17:38:17 +0000520char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000521{
522 return extract_param(&programmer_param, param_name, ",");
523}
524
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000525/* Returns the number of well-defined erasers for a chip. */
526static unsigned int count_usable_erasers(const struct flashchip *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000527{
528 unsigned int usable_erasefunctions = 0;
529 int k;
530 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
531 if (!check_block_eraser(flash, k, 0))
532 usable_erasefunctions++;
533 }
534 return usable_erasefunctions;
535}
536
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000537/* start is an offset to the base address of the flash chip */
538int check_erased_range(struct flashchip *flash, int start, int len)
539{
540 int ret;
541 uint8_t *cmpbuf = malloc(len);
542
543 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000544 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000545 exit(1);
546 }
547 memset(cmpbuf, 0xff, len);
548 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
549 free(cmpbuf);
550 return ret;
551}
552
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000553/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000554 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000555 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000556 * @start offset to the base address of the flash chip
557 * @len length of the verified area
558 * @message string to print in the "FAILED" message
559 * @return 0 for success, -1 for failure
560 */
Stefan Tauner8c357452011-09-18 22:42:18 +0000561int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000562 const char *message)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000563{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000564 int i;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000565 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000566 int ret = 0, failcount = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000567
568 if (!len)
569 goto out_free;
570
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000571 if (!flash->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000572 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000573 return 1;
574 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000575 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000576 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000577 exit(1);
578 }
579
580 if (start + len > flash->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000581 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000582 " total_size 0x%x\n", __func__, start, len,
583 flash->total_size * 1024);
584 ret = -1;
585 goto out_free;
586 }
587 if (!message)
588 message = "VERIFY";
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000589
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000590 ret = flash->read(flash, readbuf, start, len);
591 if (ret) {
592 msg_gerr("Verification impossible because read failed "
593 "at 0x%x (len 0x%x)\n", start, len);
594 return ret;
595 }
596
597 for (i = 0; i < len; i++) {
598 if (cmpbuf[i] != readbuf[i]) {
599 /* Only print the first failure. */
600 if (!failcount++)
601 msg_cerr("%s FAILED at 0x%08x! "
602 "Expected=0x%02x, Read=0x%02x,",
603 message, start + i, cmpbuf[i],
604 readbuf[i]);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000605 }
606 }
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000607 if (failcount) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000608 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000609 start, start + len - 1, failcount);
Carl-Daniel Hailfinger49b9cab2009-07-23 01:42:56 +0000610 ret = -1;
611 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000612
613out_free:
614 free(readbuf);
615 return ret;
616}
617
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000618/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000619 * Check if the buffer @have can be programmed to the content of @want without
620 * erasing. This is only possible if all chunks of size @gran are either kept
621 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000622 *
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000623 * The following write granularities (enum @gran) are known:
624 * - 1 bit. Each bit can be cleared individually.
625 * - 1 byte. A byte can be written once. Further writes to an already written
626 * byte cause the contents to be either undefined or to stay unchanged.
627 * - 128 bytes. If less than 128 bytes are written, the rest will be
628 * erased. Each write to a 128-byte region will trigger an automatic erase
629 * before anything is written. Very uncommon behaviour and unsupported by
630 * this function.
631 * - 256 bytes. If less than 256 bytes are written, the contents of the
632 * unwritten bytes are undefined.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000633 * Warning: This function assumes that @have and @want point to naturally
634 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000635 *
636 * @have buffer with current content
637 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000638 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000639 * @gran write granularity (enum, not count)
640 * @return 0 if no erase is needed, 1 otherwise
641 */
642int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
643{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000644 int result = 0;
645 int i, j, limit;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000646
647 switch (gran) {
648 case write_gran_1bit:
649 for (i = 0; i < len; i++)
650 if ((have[i] & want[i]) != want[i]) {
651 result = 1;
652 break;
653 }
654 break;
655 case write_gran_1byte:
656 for (i = 0; i < len; i++)
657 if ((have[i] != want[i]) && (have[i] != 0xff)) {
658 result = 1;
659 break;
660 }
661 break;
662 case write_gran_256bytes:
663 for (j = 0; j < len / 256; j++) {
664 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000665 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000666 if (!memcmp(have + j * 256, want + j * 256, limit))
667 continue;
668 /* have needs to be in erased state. */
669 for (i = 0; i < limit; i++)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000670 if (have[j * 256 + i] != 0xff) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000671 result = 1;
672 break;
673 }
674 if (result)
675 break;
676 }
677 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000678 default:
679 msg_cerr("%s: Unsupported granularity! Please report a bug at "
680 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000681 }
682 return result;
683}
684
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000685/**
686 * Check if the buffer @have needs to be programmed to get the content of @want.
687 * If yes, return 1 and fill in first_start with the start address of the
688 * write operation and first_len with the length of the first to-be-written
689 * chunk. If not, return 0 and leave first_start and first_len undefined.
690 *
691 * Warning: This function assumes that @have and @want point to naturally
692 * aligned regions.
693 *
694 * @have buffer with current content
695 * @want buffer with desired content
696 * @len length of the checked area
697 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000698 * @first_start offset of the first byte which needs to be written (passed in
699 * value is increased by the offset of the first needed write
700 * relative to have/want or unchanged if no write is needed)
701 * @return length of the first contiguous area which needs to be written
702 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000703 *
704 * FIXME: This function needs a parameter which tells it about coalescing
705 * in relation to the max write length of the programmer and the max write
706 * length of the chip.
707 */
708static int get_next_write(uint8_t *have, uint8_t *want, int len,
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000709 int *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000710{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000711 int need_write = 0, rel_start = 0, first_len = 0;
712 int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000713
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000714 switch (gran) {
715 case write_gran_1bit:
716 case write_gran_1byte:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000717 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000718 break;
719 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000720 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000721 break;
722 default:
723 msg_cerr("%s: Unsupported granularity! Please report a bug at "
724 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000725 /* Claim that no write was needed. A write with unknown
726 * granularity is too dangerous to try.
727 */
728 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000729 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000730 for (i = 0; i < len / stride; i++) {
731 limit = min(stride, len - i * stride);
732 /* Are 'have' and 'want' identical? */
733 if (memcmp(have + i * stride, want + i * stride, limit)) {
734 if (!need_write) {
735 /* First location where have and want differ. */
736 need_write = 1;
737 rel_start = i * stride;
738 }
739 } else {
740 if (need_write) {
741 /* First location where have and want
742 * do not differ anymore.
743 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000744 break;
745 }
746 }
747 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000748 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000749 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000750 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000751 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000752}
753
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000754/* This function generates various test patterns useful for testing controller
755 * and chip communication as well as chip behaviour.
756 *
757 * If a byte can be written multiple times, each time keeping 0-bits at 0
758 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
759 * is essentially an AND operation. That's also the reason why this function
760 * provides the result of AND between various patterns.
761 *
762 * Below is a list of patterns (and their block length).
763 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
764 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
765 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
766 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
767 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
768 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
769 * Pattern 6 is 00 (1 Byte)
770 * Pattern 7 is ff (1 Byte)
771 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
772 * byte block.
773 *
774 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
775 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
776 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
777 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
778 * Pattern 12 is 00 (1 Byte)
779 * Pattern 13 is ff (1 Byte)
780 * Patterns 8-13 have no block number.
781 *
782 * Patterns 0-3 are created to detect and efficiently diagnose communication
783 * slips like missed bits or bytes and their repetitive nature gives good visual
784 * cues to the person inspecting the results. In addition, the following holds:
785 * AND Pattern 0/1 == Pattern 4
786 * AND Pattern 2/3 == Pattern 5
787 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
788 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
789 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
790 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
791 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
792 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
793 * Besides that, they provide for bit testing of the last two bytes of every
794 * 256 byte block which contains the block number for patterns 0-6.
795 * Patterns 10-11 are special purpose for detecting subblock aliasing with
796 * block sizes >256 bytes (some Dataflash chips etc.)
797 * AND Pattern 8/9 == Pattern 12
798 * AND Pattern 10/11 == Pattern 12
799 * Pattern 13 is the completely erased state.
800 * None of the patterns can detect aliasing at boundaries which are a multiple
801 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
802 */
803int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
804{
805 int i;
806
807 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000808 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000809 return 1;
810 }
811
812 switch (variant) {
813 case 0:
814 for (i = 0; i < size; i++)
815 buf[i] = (i & 0xf) << 4 | 0x5;
816 break;
817 case 1:
818 for (i = 0; i < size; i++)
819 buf[i] = (i & 0xf) << 4 | 0xa;
820 break;
821 case 2:
822 for (i = 0; i < size; i++)
823 buf[i] = 0x50 | (i & 0xf);
824 break;
825 case 3:
826 for (i = 0; i < size; i++)
827 buf[i] = 0xa0 | (i & 0xf);
828 break;
829 case 4:
830 for (i = 0; i < size; i++)
831 buf[i] = (i & 0xf) << 4;
832 break;
833 case 5:
834 for (i = 0; i < size; i++)
835 buf[i] = i & 0xf;
836 break;
837 case 6:
838 memset(buf, 0x00, size);
839 break;
840 case 7:
841 memset(buf, 0xff, size);
842 break;
843 case 8:
844 for (i = 0; i < size; i++)
845 buf[i] = i & 0xff;
846 break;
847 case 9:
848 for (i = 0; i < size; i++)
849 buf[i] = ~(i & 0xff);
850 break;
851 case 10:
852 for (i = 0; i < size % 2; i++) {
853 buf[i * 2] = (i >> 8) & 0xff;
854 buf[i * 2 + 1] = i & 0xff;
855 }
856 if (size & 0x1)
857 buf[i * 2] = (i >> 8) & 0xff;
858 break;
859 case 11:
860 for (i = 0; i < size % 2; i++) {
861 buf[i * 2] = ~((i >> 8) & 0xff);
862 buf[i * 2 + 1] = ~(i & 0xff);
863 }
864 if (size & 0x1)
865 buf[i * 2] = ~((i >> 8) & 0xff);
866 break;
867 case 12:
868 memset(buf, 0x00, size);
869 break;
870 case 13:
871 memset(buf, 0xff, size);
872 break;
873 }
874
875 if ((variant >= 0) && (variant <= 7)) {
876 /* Write block number in the last two bytes of each 256-byte
877 * block, big endian for easier reading of the hexdump.
878 * Note that this wraps around for chips larger than 2^24 bytes
879 * (16 MB).
880 */
881 for (i = 0; i < size / 256; i++) {
882 buf[i * 256 + 254] = (i >> 8) & 0xff;
883 buf[i * 256 + 255] = i & 0xff;
884 }
885 }
886
887 return 0;
888}
889
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000890int check_max_decode(enum chipbustype buses, uint32_t size)
891{
892 int limitexceeded = 0;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000893
894 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000895 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000896 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000897 "size %u kB of chipset/board/programmer "
898 "for %s interface, "
899 "probe/read/erase/write may fail. ", size / 1024,
900 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000901 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000902 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000903 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000904 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000905 "size %u kB of chipset/board/programmer "
906 "for %s interface, "
907 "probe/read/erase/write may fail. ", size / 1024,
908 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000909 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000910 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000911 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000912 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000913 "size %u kB of chipset/board/programmer "
914 "for %s interface, "
915 "probe/read/erase/write may fail. ", size / 1024,
916 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000917 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000918 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000919 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000920 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000921 "size %u kB of chipset/board/programmer "
922 "for %s interface, "
923 "probe/read/erase/write may fail. ", size / 1024,
924 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000925 }
926 if (!limitexceeded)
927 return 0;
928 /* Sometimes chip and programmer have more than one bus in common,
929 * and the limit is not exceeded on all buses. Tell the user.
930 */
931 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000932 /* FIXME: This message is designed towards CLI users. */
Sean Nelson316a29f2010-05-07 20:09:04 +0000933 msg_pdbg("There is at least one common chip/programmer "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000934 "interface which can support a chip of this size. "
935 "You can try --force at your own risk.\n");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000936 return 1;
937}
938
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000939int probe_flash(int startchip, struct flashchip *fill_flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000940{
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000941 const struct flashchip *flash;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000942 unsigned long base = 0;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000943 char location[64];
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000944 uint32_t size;
945 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000946 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000947
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000948 for (flash = flashchips + startchip; flash && flash->name; flash++) {
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000949 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000950 continue;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000951 buses_common = buses_supported & flash->bustype;
952 if (!buses_common) {
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000953 msg_gspew("Probing for %s %s, %d kB: skipped. ",
954 flash->vendor, flash->name, flash->total_size);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000955 tmp = flashbuses_to_text(buses_supported);
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000956 msg_gspew("Host bus type %s ", tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000957 free(tmp);
958 tmp = flashbuses_to_text(flash->bustype);
Carl-Daniel Hailfinger12575e52010-03-22 23:43:51 +0000959 msg_gspew("and chip bus type %s are incompatible.",
960 tmp);
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000961 free(tmp);
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000962 msg_gspew("\n");
963 continue;
964 }
965 msg_gdbg("Probing for %s %s, %d kB: ",
966 flash->vendor, flash->name, flash->total_size);
967 if (!flash->probe && !force) {
968 msg_gdbg("failed! flashrom has no probe function for "
969 "this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000970 continue;
971 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000972
Ollie Lhocbbf1252004-03-17 22:22:08 +0000973 size = flash->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000974 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +0000975
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000976 /* Start filling in the dynamic data. */
977 *fill_flash = *flash;
978
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +0000979 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000980 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000981
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000982 if (force)
983 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000984
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000985 if (fill_flash->probe(fill_flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000986 goto notfound;
987
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000988 /* If this is the first chip found, accept it.
989 * If this is not the first chip found, accept it only if it is
990 * a non-generic match.
991 * We could either make chipcount global or provide it as
992 * parameter, or we assume that startchip==0 means this call to
993 * probe_flash() is the first one and thus no chip has been
994 * found before.
995 */
996 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000997 break;
998
Peter Stuge483b8f02008-09-03 23:10:05 +0000999notfound:
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001000 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001001 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001002
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001003 if (!flash || !flash->name)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001004 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001005
Carl-Daniel Hailfingerd9535582011-03-08 00:09:11 +00001006#if CONFIG_INTERNAL == 1
1007 if (programmer_table[programmer].map_flash_region == physmap)
Stefan Reinauer051e2362011-01-19 06:21:54 +00001008 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
Carl-Daniel Hailfingerd9535582011-03-08 00:09:11 +00001009 else
1010#endif
Stefan Reinauer051e2362011-01-19 06:21:54 +00001011 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
Stefan Reinauer051e2362011-01-19 06:21:54 +00001012
Stefan Tauner00155492011-06-26 20:45:35 +00001013 tmp = flashbuses_to_text(flash->bustype);
Stefan Tauner716e0982011-07-25 20:38:52 +00001014 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
Stefan Tauner00155492011-06-26 20:45:35 +00001015 force ? "Assuming" : "Found", fill_flash->vendor,
1016 fill_flash->name, fill_flash->total_size, tmp, location);
1017 free(tmp);
Uwe Hermann9899cad2009-06-28 21:47:57 +00001018
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +00001019 /* Flash registers will not be mapped if the chip was forced. Lock info
1020 * may be stored in registers, so avoid lock info printing.
1021 */
1022 if (!force)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001023 if (fill_flash->printlock)
1024 fill_flash->printlock(fill_flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +00001025
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001026 /* Return position of matching chip. */
1027 return flash - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001028}
1029
Stefan Reinauere3705282005-12-18 16:41:10 +00001030int verify_flash(struct flashchip *flash, uint8_t *buf)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001031{
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001032 int ret;
Ollie Lho761bf1b2004-03-20 16:46:10 +00001033 int total_size = flash->total_size * 1024;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001034
Sean Nelson316a29f2010-05-07 20:09:04 +00001035 msg_cinfo("Verifying flash... ");
Uwe Hermanna7e05482007-05-09 10:17:44 +00001036
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001037 ret = verify_range(flash, buf, 0, total_size, NULL);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001038
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001039 if (!ret)
Sean Nelson316a29f2010-05-07 20:09:04 +00001040 msg_cinfo("VERIFIED. \n");
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001041
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +00001042 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001043}
1044
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001045int read_buf_from_file(unsigned char *buf, unsigned long size,
1046 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001047{
1048 unsigned long numbytes;
1049 FILE *image;
1050 struct stat image_stat;
1051
1052 if ((image = fopen(filename, "rb")) == NULL) {
1053 perror(filename);
1054 return 1;
1055 }
1056 if (fstat(fileno(image), &image_stat) != 0) {
1057 perror(filename);
1058 fclose(image);
1059 return 1;
1060 }
1061 if (image_stat.st_size != size) {
1062 msg_gerr("Error: Image size doesn't match\n");
1063 fclose(image);
1064 return 1;
1065 }
1066 numbytes = fread(buf, 1, size, image);
1067 if (fclose(image)) {
1068 perror(filename);
1069 return 1;
1070 }
1071 if (numbytes != size) {
1072 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1073 "wanted %ld!\n", numbytes, size);
1074 return 1;
1075 }
1076 return 0;
1077}
1078
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001079int write_buf_to_file(unsigned char *buf, unsigned long size,
1080 const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001081{
1082 unsigned long numbytes;
1083 FILE *image;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001084
1085 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001086 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001087 return 1;
1088 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001089 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001090 perror(filename);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001091 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001092 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001093
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001094 numbytes = fwrite(buf, 1, size, image);
1095 fclose(image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001096 if (numbytes != size) {
1097 msg_gerr("File %s could not be written completely.\n",
1098 filename);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001099 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001100 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001101 return 0;
1102}
1103
Stefan Tauner66652442011-06-26 17:38:17 +00001104int read_flash_to_file(struct flashchip *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001105{
1106 unsigned long size = flash->total_size * 1024;
1107 unsigned char *buf = calloc(size, sizeof(char));
1108 int ret = 0;
1109
1110 msg_cinfo("Reading flash... ");
1111 if (!buf) {
1112 msg_gerr("Memory allocation failed!\n");
1113 msg_cinfo("FAILED.\n");
1114 return 1;
1115 }
1116 if (!flash->read) {
1117 msg_cerr("No read function available for this flash chip.\n");
1118 ret = 1;
1119 goto out_free;
1120 }
1121 if (flash->read(flash, buf, 0, size)) {
1122 msg_cerr("Read operation failed!\n");
1123 ret = 1;
1124 goto out_free;
1125 }
1126
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001127 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001128out_free:
1129 free(buf);
1130 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1131 return ret;
1132}
1133
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001134/* This function shares a lot of its structure with erase_and_write_flash() and
1135 * walk_eraseregions().
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001136 * Even if an error is found, the function will keep going and check the rest.
1137 */
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001138static int selfcheck_eraseblocks(const struct flashchip *flash)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001139{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001140 int i, j, k;
1141 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001142
1143 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1144 unsigned int done = 0;
1145 struct block_eraser eraser = flash->block_erasers[k];
1146
1147 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1148 /* Blocks with zero size are bugs in flashchips.c. */
1149 if (eraser.eraseblocks[i].count &&
1150 !eraser.eraseblocks[i].size) {
1151 msg_gerr("ERROR: Flash chip %s erase function "
1152 "%i region %i has size 0. Please report"
1153 " a bug at flashrom@flashrom.org\n",
1154 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001155 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001156 }
1157 /* Blocks with zero count are bugs in flashchips.c. */
1158 if (!eraser.eraseblocks[i].count &&
1159 eraser.eraseblocks[i].size) {
1160 msg_gerr("ERROR: Flash chip %s erase function "
1161 "%i region %i has count 0. Please report"
1162 " a bug at flashrom@flashrom.org\n",
1163 flash->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001164 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001165 }
1166 done += eraser.eraseblocks[i].count *
1167 eraser.eraseblocks[i].size;
1168 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001169 /* Empty eraseblock definition with erase function. */
1170 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001171 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001172 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001173 if (!done)
1174 continue;
1175 if (done != flash->total_size * 1024) {
1176 msg_gerr("ERROR: Flash chip %s erase function %i "
1177 "region walking resulted in 0x%06x bytes total,"
1178 " expected 0x%06x bytes. Please report a bug at"
1179 " flashrom@flashrom.org\n", flash->name, k,
1180 done, flash->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001181 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001182 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001183 if (!eraser.block_erase)
1184 continue;
1185 /* Check if there are identical erase functions for different
1186 * layouts. That would imply "magic" erase functions. The
1187 * easiest way to check this is with function pointers.
1188 */
Uwe Hermann43959702010-03-13 17:28:29 +00001189 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001190 if (eraser.block_erase ==
1191 flash->block_erasers[j].block_erase) {
1192 msg_gerr("ERROR: Flash chip %s erase function "
1193 "%i and %i are identical. Please report"
1194 " a bug at flashrom@flashrom.org\n",
1195 flash->name, k, j);
1196 ret = 1;
1197 }
Uwe Hermann43959702010-03-13 17:28:29 +00001198 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001199 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001200 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001201}
1202
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001203static int erase_and_write_block_helper(struct flashchip *flash,
1204 unsigned int start, unsigned int len,
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001205 uint8_t *curcontents,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001206 uint8_t *newcontents,
1207 int (*erasefn) (struct flashchip *flash,
1208 unsigned int addr,
1209 unsigned int len))
1210{
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001211 int starthere = 0, lenhere = 0, ret = 0, skip = 1, writecount = 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001212 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1213
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001214 /* curcontents and newcontents are opaque to walk_eraseregions, and
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001215 * need to be adjusted here to keep the impression of proper abstraction
1216 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001217 curcontents += start;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001218 newcontents += start;
1219 msg_cdbg(":");
1220 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001221 if (need_erase(curcontents, newcontents, len, gran)) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001222 msg_cdbg("E");
1223 ret = erasefn(flash, start, len);
1224 if (ret)
1225 return ret;
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +00001226 if (check_erased_range(flash, start, len)) {
1227 msg_cerr("ERASE FAILED!\n");
1228 return -1;
1229 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001230 /* Erase was successful. Adjust curcontents. */
1231 memset(curcontents, 0xff, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001232 skip = 0;
1233 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001234 /* get_next_write() sets starthere to a new value after the call. */
1235 while ((lenhere = get_next_write(curcontents + starthere,
1236 newcontents + starthere,
1237 len - starthere, &starthere, gran))) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001238 if (!writecount++)
1239 msg_cdbg("W");
1240 /* Needs the partial write function signature. */
1241 ret = flash->write(flash, newcontents + starthere,
1242 start + starthere, lenhere);
1243 if (ret)
1244 return ret;
1245 starthere += lenhere;
1246 skip = 0;
1247 }
1248 if (skip)
1249 msg_cdbg("S");
1250 return ret;
1251}
1252
Carl-Daniel Hailfinger4d3e9ca2010-07-13 00:42:00 +00001253static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1254 int (*do_something) (struct flashchip *flash,
1255 unsigned int addr,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001256 unsigned int len,
1257 uint8_t *param1,
1258 uint8_t *param2,
1259 int (*erasefn) (
1260 struct flashchip *flash,
1261 unsigned int addr,
1262 unsigned int len)),
1263 void *param1, void *param2)
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001264{
1265 int i, j;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001266 unsigned int start = 0;
1267 unsigned int len;
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001268 struct block_eraser eraser = flash->block_erasers[erasefunction];
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001269
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001270 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1271 /* count==0 for all automatically initialized array
1272 * members so the loop below won't be executed for them.
1273 */
1274 len = eraser.eraseblocks[i].size;
1275 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001276 /* Print this for every block except the first one. */
1277 if (i || j)
1278 msg_cdbg(", ");
1279 msg_cdbg("0x%06x-0x%06x", start,
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001280 start + len - 1);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001281 if (do_something(flash, start, len, param1, param2,
1282 eraser.block_erase)) {
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001283 return 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001284 }
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001285 start += len;
1286 }
1287 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001288 msg_cdbg("\n");
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001289 return 0;
1290}
1291
Stefan Tauner5368dca2011-07-01 00:19:12 +00001292static int check_block_eraser(const struct flashchip *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001293{
1294 struct block_eraser eraser = flash->block_erasers[k];
1295
1296 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1297 if (log)
1298 msg_cdbg("not defined. ");
1299 return 1;
1300 }
1301 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1302 if (log)
1303 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001304 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001305 return 1;
1306 }
1307 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1308 if (log)
1309 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001310 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001311 return 1;
1312 }
1313 return 0;
1314}
1315
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001316int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1317 uint8_t *newcontents)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001318{
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001319 int k, ret = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001320 uint8_t *curcontents;
1321 unsigned long size = flash->total_size * 1024;
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001322 unsigned int usable_erasefunctions = count_usable_erasers(flash);
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001323
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001324 msg_cinfo("Erasing and writing flash chip... ");
Stefan Tauner269de352011-07-12 22:35:21 +00001325 curcontents = malloc(size);
1326 if (!curcontents) {
1327 msg_gerr("Out of memory!\n");
1328 exit(1);
1329 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001330 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1331 memcpy(curcontents, oldcontents, size);
1332
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001333 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001334 if (k != 0)
1335 msg_cdbg("Looking for another erase function.\n");
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001336 if (!usable_erasefunctions) {
1337 msg_cdbg("No usable erase functions left.\n");
1338 break;
1339 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001340 msg_cdbg("Trying erase function %i... ", k);
1341 if (check_block_eraser(flash, k, 1))
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001342 continue;
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001343 usable_erasefunctions--;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001344 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1345 curcontents, newcontents);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001346 /* If everything is OK, don't try another erase function. */
1347 if (!ret)
1348 break;
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001349 /* Write/erase failed, so try to find out what the current chip
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001350 * contents are. If no usable erase functions remain, we can
1351 * skip this: the next iteration will break immediately anyway.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001352 */
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001353 if (!usable_erasefunctions)
1354 continue;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001355 /* Reading the whole chip may take a while, inform the user even
1356 * in non-verbose mode.
1357 */
1358 msg_cinfo("Reading current flash chip contents... ");
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001359 if (flash->read(flash, curcontents, 0, size)) {
1360 /* Now we are truly screwed. Read failed as well. */
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001361 msg_cerr("Can't read anymore! Aborting.\n");
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001362 /* We have no idea about the flash chip contents, so
1363 * retrying with another erase function is pointless.
1364 */
1365 break;
1366 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001367 msg_cinfo("done. ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001368 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001369 /* Free the scratchpad. */
1370 free(curcontents);
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001371
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001372 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001373 msg_cerr("FAILED!\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001374 } else {
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001375 msg_cinfo("Erase/write done.\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001376 }
1377 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001378}
1379
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001380void nonfatal_help_message(void)
1381{
1382 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1383 "This means we have to add special support for your board, "
1384 "programmer or flash chip.\n"
1385 "Please report this on IRC at irc.freenode.net (channel "
1386 "#flashrom) or\n"
1387 "mail flashrom@flashrom.org!\n"
1388 "-------------------------------------------------------------"
1389 "------------------\n"
1390 "You may now reboot or simply leave the machine running.\n");
1391}
1392
Uwe Hermannc67d0372009-10-01 18:40:02 +00001393void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001394{
Sean Nelson316a29f2010-05-07 20:09:04 +00001395 msg_gerr("Your flash chip is in an unknown state.\n"
Uwe Hermannc67d0372009-10-01 18:40:02 +00001396 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
Paul Menzelab6328f2010-10-08 11:03:02 +00001397 "mail flashrom@flashrom.org with FAILED: your board name in "
1398 "the subject line!\n"
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001399 "-------------------------------------------------------------"
1400 "------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001401 "DO NOT REBOOT OR POWEROFF!\n");
1402}
1403
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001404/* The way to go if you want a delimited list of programmers */
Stefan Tauner66652442011-06-26 17:38:17 +00001405void list_programmers(const char *delim)
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001406{
1407 enum programmer p;
1408 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001409 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001410 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001411 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001412 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001413 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001414}
1415
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001416void list_programmers_linebreak(int startcol, int cols, int paren)
1417{
1418 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001419 int pnamelen;
1420 int remaining = 0, firstline = 1;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001421 enum programmer p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001422 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001423
1424 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1425 pname = programmer_table[p].name;
1426 pnamelen = strlen(pname);
1427 if (remaining - pnamelen - 2 < 0) {
1428 if (firstline)
1429 firstline = 0;
1430 else
1431 printf("\n");
1432 for (i = 0; i < startcol; i++)
1433 printf(" ");
1434 remaining = cols - startcol;
1435 } else {
1436 printf(" ");
1437 remaining--;
1438 }
1439 if (paren && (p == 0)) {
1440 printf("(");
1441 remaining--;
1442 }
1443 printf("%s", pname);
1444 remaining -= pnamelen;
1445 if (p < PROGRAMMER_INVALID - 1) {
1446 printf(",");
1447 remaining--;
1448 } else {
1449 if (paren)
1450 printf(")");
1451 printf("\n");
1452 }
1453 }
1454}
1455
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001456void print_sysinfo(void)
1457{
1458#if HAVE_UTSNAME == 1
1459 struct utsname osinfo;
1460 uname(&osinfo);
1461
1462 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1463 osinfo.machine);
1464#else
1465 msg_ginfo(" on unknown machine");
1466#endif
1467 msg_ginfo(", built with");
1468#if NEED_PCI == 1
1469#ifdef PCILIB_VERSION
1470 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1471#else
1472 msg_ginfo(" unknown PCI library,");
1473#endif
1474#endif
1475#ifdef __clang__
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001476 msg_ginfo(" LLVM Clang");
1477#ifdef __clang_version__
1478 msg_ginfo(" %s,", __clang_version__);
1479#else
1480 msg_ginfo(" unknown version (before r102686),");
1481#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001482#elif defined(__GNUC__)
1483 msg_ginfo(" GCC");
1484#ifdef __VERSION__
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001485 msg_ginfo(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001486#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001487 msg_ginfo(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001488#endif
1489#else
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001490 msg_ginfo(" unknown compiler,");
1491#endif
1492#if defined (__FLASHROM_LITTLE_ENDIAN__)
1493 msg_ginfo(" little endian");
1494#else
1495 msg_ginfo(" big endian");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001496#endif
1497 msg_ginfo("\n");
1498}
1499
Bernhard Walle201bde32008-01-21 15:24:22 +00001500void print_version(void)
1501{
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001502 msg_ginfo("flashrom v%s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001503 print_sysinfo();
Bernhard Walle201bde32008-01-21 15:24:22 +00001504}
1505
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001506void print_banner(void)
1507{
1508 msg_ginfo("flashrom is free software, get the source code at "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001509 "http://www.flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001510 msg_ginfo("\n");
1511}
1512
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001513int selfcheck(void)
1514{
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001515 int ret = 0;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001516 const struct flashchip *flash;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001517
1518 /* Safety check. Instead of aborting after the first error, check
1519 * if more errors exist.
1520 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001521 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001522 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001523 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001524 }
Stefan Tauner93f70232011-07-26 14:33:46 +00001525 /* It would be favorable if we could also check for correct termination
Stefan Tauner716e0982011-07-25 20:38:52 +00001526 * of the following arrays, but we don't know their sizes in here...
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001527 * For 'flashchips' we check the first element to be non-null. In the
1528 * other cases there exist use cases where the first element can be
1529 * null. */
1530 if (flashchips == NULL || flashchips[0].vendor == NULL) {
1531 msg_gerr("Flashchips table miscompilation!\n");
1532 ret = 1;
1533 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001534 for (flash = flashchips; flash && flash->name; flash++)
1535 if (selfcheck_eraseblocks(flash))
1536 ret = 1;
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001537
1538#if CONFIG_INTERNAL == 1
1539 if (chipset_enables == NULL) {
1540 msg_gerr("Chipset enables table does not exist!\n");
1541 ret = 1;
1542 }
Carl-Daniel Hailfinger97d5b122011-08-31 16:19:50 +00001543 if (board_matches == NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001544 msg_gerr("Board enables table does not exist!\n");
1545 ret = 1;
1546 }
1547 if (boards_known == NULL) {
1548 msg_gerr("Known boards table does not exist!\n");
1549 ret = 1;
1550 }
1551 if (laptops_known == NULL) {
1552 msg_gerr("Known laptops table does not exist!\n");
1553 ret = 1;
1554 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001555#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001556 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001557}
1558
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001559void check_chip_supported(const struct flashchip *flash)
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001560{
1561 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001562 msg_cinfo("===\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001563 if (flash->tested & TEST_BAD_MASK) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001564 msg_cinfo("This flash part has status NOT WORKING for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001565 if (flash->tested & TEST_BAD_PROBE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001566 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001567 if (flash->tested & TEST_BAD_READ)
Sean Nelson316a29f2010-05-07 20:09:04 +00001568 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001569 if (flash->tested & TEST_BAD_ERASE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001570 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001571 if (flash->tested & TEST_BAD_WRITE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001572 msg_cinfo(" WRITE");
1573 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001574 }
1575 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1576 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1577 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1578 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001579 msg_cinfo("This flash part has status UNTESTED for operations:");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001580 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001581 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001582 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
Sean Nelson316a29f2010-05-07 20:09:04 +00001583 msg_cinfo(" READ");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001584 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001585 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001586 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001587 msg_cinfo(" WRITE");
1588 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001589 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001590 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001591 msg_cinfo("The test status of this chip may have been updated "
1592 "in the latest development\n"
1593 "version of flashrom. If you are running the latest "
1594 "development version,\n"
1595 "please email a report to flashrom@flashrom.org if "
1596 "any of the above operations\n"
1597 "work correctly for you with this flash part. Please "
1598 "include the flashrom\n"
1599 "output with the additional -V option for all "
1600 "operations you tested (-V, -Vr,\n"
1601 "-Vw, -VE), and mention which mainboard or "
1602 "programmer you tested.\n"
Paul Menzelab6328f2010-10-08 11:03:02 +00001603 "Please mention your board in the subject line. "
1604 "Thanks for your help!\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001605 }
1606}
1607
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001608/* FIXME: This function signature needs to be improved once doit() has a better
1609 * function signature.
1610 */
David Hendricks77f931a2011-05-18 01:30:56 +00001611int 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 +00001612{
1613 if (!programmer_may_write && (write_it || erase_it)) {
1614 msg_perr("Write/erase is not working yet on your programmer in "
1615 "its current configuration.\n");
1616 /* --force is the wrong approach, but it's the best we can do
1617 * until the generic programmer parameter parser is merged.
1618 */
1619 if (!force)
1620 return 1;
1621 msg_cerr("Continuing anyway.\n");
1622 }
1623
1624 if (read_it || erase_it || write_it || verify_it) {
1625 /* Everything needs read. */
1626 if (flash->tested & TEST_BAD_READ) {
1627 msg_cerr("Read is not working on this chip. ");
1628 if (!force)
1629 return 1;
1630 msg_cerr("Continuing anyway.\n");
1631 }
1632 if (!flash->read) {
1633 msg_cerr("flashrom has no read function for this "
1634 "flash chip.\n");
1635 return 1;
1636 }
1637 }
1638 if (erase_it || write_it) {
1639 /* Write needs erase. */
1640 if (flash->tested & TEST_BAD_ERASE) {
1641 msg_cerr("Erase is not working on this chip. ");
1642 if (!force)
1643 return 1;
1644 msg_cerr("Continuing anyway.\n");
1645 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001646 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001647 msg_cerr("flashrom has no erase function for this "
1648 "flash chip.\n");
1649 return 1;
1650 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001651 }
1652 if (write_it) {
1653 if (flash->tested & TEST_BAD_WRITE) {
1654 msg_cerr("Write is not working on this chip. ");
1655 if (!force)
1656 return 1;
1657 msg_cerr("Continuing anyway.\n");
1658 }
1659 if (!flash->write) {
1660 msg_cerr("flashrom has no write function for this "
1661 "flash chip.\n");
1662 return 1;
1663 }
1664 }
1665 return 0;
1666}
1667
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001668/* This function signature is horrible. We need to design a better interface,
1669 * but right now it allows us to split off the CLI code.
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001670 * Besides that, the function itself is a textbook example of abysmal code flow.
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001671 */
Stefan Tauner66652442011-06-26 17:38:17 +00001672int 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 +00001673{
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001674 uint8_t *oldcontents;
1675 uint8_t *newcontents;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001676 int ret = 0;
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001677 unsigned long size = flash->total_size * 1024;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001678
David Hendricks77f931a2011-05-18 01:30:56 +00001679 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001680 msg_cerr("Aborting.\n");
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001681 ret = 1;
1682 goto out_nofree;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001683 }
1684
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001685 /* Given the existence of read locks, we want to unlock for read,
1686 * erase and write.
1687 */
1688 if (flash->unlock)
1689 flash->unlock(flash);
1690
1691 if (read_it) {
1692 ret = read_flash_to_file(flash, filename);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001693 goto out_nofree;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +00001694 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001695
Stefan Tauner269de352011-07-12 22:35:21 +00001696 oldcontents = malloc(size);
1697 if (!oldcontents) {
1698 msg_gerr("Out of memory!\n");
1699 exit(1);
1700 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001701 /* Assume worst case: All bits are 0. */
1702 memset(oldcontents, 0x00, size);
Stefan Tauner269de352011-07-12 22:35:21 +00001703 newcontents = malloc(size);
1704 if (!newcontents) {
1705 msg_gerr("Out of memory!\n");
1706 exit(1);
1707 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001708 /* Assume best case: All bits should be 1. */
1709 memset(newcontents, 0xff, size);
1710 /* Side effect of the assumptions above: Default write action is erase
1711 * because newcontents looks like a completely erased chip, and
1712 * oldcontents being completely 0x00 means we have to erase everything
1713 * before we can write.
1714 */
1715
Ollie Lhoefa28582004-12-08 20:10:01 +00001716 if (erase_it) {
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001717 /* FIXME: Do we really want the scary warning if erase failed?
1718 * After all, after erase the chip is either blank or partially
1719 * blank or it has the old contents. A blank chip won't boot,
1720 * so if the user wanted erase and reboots afterwards, the user
1721 * knows very well that booting won't work.
1722 */
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001723 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001724 emergency_help_message();
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001725 ret = 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001726 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001727 goto out;
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001728 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001729
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001730 if (write_it || verify_it) {
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001731 if (read_buf_from_file(newcontents, size, filename)) {
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001732 ret = 1;
1733 goto out;
Stefan Reinauer018aca82006-11-21 23:48:51 +00001734 }
1735
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001736#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001737 if (programmer == PROGRAMMER_INTERNAL)
1738 show_id(newcontents, size, force);
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001739#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001740 }
1741
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001742 /* Read the whole chip to be able to check whether regions need to be
1743 * erased and to give better diagnostics in case write fails.
1744 * The alternative would be to read only the regions which are to be
1745 * preserved, but in that case we might perform unneeded erase which
1746 * takes time as well.
1747 */
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001748 msg_cinfo("Reading old flash chip contents... ");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001749 if (flash->read(flash, oldcontents, 0, size)) {
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001750 ret = 1;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001751 msg_cinfo("FAILED.\n");
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001752 goto out;
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001753 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001754 msg_cinfo("done.\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001755
Ollie Lho184a4042005-11-26 21:55:36 +00001756 // This should be moved into each flash part's code to do it
1757 // cleanly. This does the job.
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001758 handle_romentries(flash, oldcontents, newcontents);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001759
Ollie Lho184a4042005-11-26 21:55:36 +00001760 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001761
Peter Stugef31104c2008-04-28 14:47:30 +00001762 if (write_it) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001763 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1764 msg_cerr("Uh oh. Erase/write failed. Checking if "
1765 "anything changed.\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001766 if (!flash->read(flash, newcontents, 0, size)) {
1767 if (!memcmp(oldcontents, newcontents, size)) {
1768 msg_cinfo("Good. It seems nothing was "
1769 "changed.\n");
1770 nonfatal_help_message();
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001771 ret = 1;
1772 goto out;
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001773 }
1774 }
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001775 emergency_help_message();
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001776 ret = 1;
1777 goto out;
Peter Stugef31104c2008-04-28 14:47:30 +00001778 }
Peter Stugef31104c2008-04-28 14:47:30 +00001779 }
Ollie Lho184a4042005-11-26 21:55:36 +00001780
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001781 if (verify_it) {
1782 /* Work around chips which need some time to calm down. */
1783 if (write_it)
1784 programmer_delay(1000*1000);
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001785 ret = verify_flash(flash, newcontents);
Carl-Daniel Hailfingerf5292052009-11-17 09:57:34 +00001786 /* If we tried to write, and verification now fails, we
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001787 * might have an emergency situation.
1788 */
1789 if (ret && write_it)
1790 emergency_help_message();
1791 }
Ollie Lho184a4042005-11-26 21:55:36 +00001792
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001793out:
1794 free(oldcontents);
1795 free(newcontents);
1796out_nofree:
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001797 programmer_shutdown();
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001798 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001799}