blob: 06845c7ca242ea67507598a9778168343c216842 [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
uwe4475e902009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
hailfinger23060112009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-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.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-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.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
uweb25f1ea2007-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
rminnich8d3ff912003-10-25 17:01:29 +000022 */
23
hailfingera83a5fe2010-05-30 22:24:40 +000024#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000026#ifndef __LIBPAYLOAD__
27#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000028#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000029#endif
rminnich8d3ff912003-10-25 17:01:29 +000030#include <string.h>
31#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000032#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000033#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
Vincent Palatin7ab23932014-10-01 12:09:16 -070037#include <unistd.h>
rminnich8d3ff912003-10-25 17:01:29 +000038#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000039#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090040#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000041#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000042
krause2eb76212011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000044char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000045int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000046
David Hendricks1ed1d352011-11-23 17:54:37 -080047/* error handling stuff */
48enum error_action access_denied_action = error_ignore;
49
50int ignore_error(int err) {
51 int rc = 0;
52
53 switch(err) {
54 case ACCESS_DENIED:
55 if (access_denied_action == error_ignore)
56 rc = 1;
57 break;
58 default:
59 break;
60 }
61
62 return rc;
63}
64
hailfinger969e2f32011-09-08 00:00:29 +000065static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000066
hailfingerddeb4ac2010-07-08 10:13:37 +000067static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000068
hailfinger1ff33dc2010-07-03 11:02:10 +000069/* Supported buses for the current programmer. */
70enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +000071
uwee15beb92010-08-08 17:01:18 +000072/*
hailfinger80422e22009-12-13 22:28:00 +000073 * Programmers supporting multiple buses can have differing size limits on
74 * each bus. Store the limits for each bus in a common struct.
75 */
hailfinger1ff33dc2010-07-03 11:02:10 +000076struct decode_sizes max_rom_decode;
77
78/* If nonzero, used as the start address of bottom-aligned flash. */
79unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000080
hailfinger5828baf2010-07-03 12:14:25 +000081/* Is writing allowed with this programmer? */
82int programmer_may_write;
83
hailfingerabe249e2009-05-08 17:43:22 +000084const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000085#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000086 {
hailfinger3548a9a2009-08-12 14:34:35 +000087 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000088 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000089 .map_flash_region = physmap,
90 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000091 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +000092 },
hailfinger80422e22009-12-13 22:28:00 +000093#endif
stepan927d4e22007-04-04 22:45:58 +000094
hailfinger90c7d542010-05-31 15:27:27 +000095#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +000096 {
hailfinger3548a9a2009-08-12 14:34:35 +000097 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +000098 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +000099 .map_flash_region = dummy_map,
100 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000101 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000102 },
hailfinger571a6b32009-09-16 10:09:21 +0000103#endif
hailfingera9df33c2009-05-09 00:54:55 +0000104
hailfinger90c7d542010-05-31 15:27:27 +0000105#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000106 {
hailfinger3548a9a2009-08-12 14:34:35 +0000107 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000108 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000109 .map_flash_region = fallback_map,
110 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000111 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000112 },
hailfinger571a6b32009-09-16 10:09:21 +0000113#endif
uwe0f5a3a22009-05-13 11:36:06 +0000114
hailfinger90c7d542010-05-31 15:27:27 +0000115#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000116 {
hailfinger0d703d42011-03-07 01:08:09 +0000117 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000118 .name = "nicrealtek",
119 //.name = "nicsmc1211",
120 .init = nicrealtek_init,
121 .map_flash_region = fallback_map,
122 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000123 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000124 },
hailfinger5aa36982010-05-21 21:54:07 +0000125#endif
126
hailfingerf0a368f2010-06-07 22:37:54 +0000127#if CONFIG_NICNATSEMI == 1
128 {
uwe8d342eb2011-07-28 08:13:25 +0000129 .name = "nicnatsemi",
130 .init = nicnatsemi_init,
131 .map_flash_region = fallback_map,
132 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000133 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000134 },
135#endif
hailfinger5aa36982010-05-21 21:54:07 +0000136
hailfinger90c7d542010-05-31 15:27:27 +0000137#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000138 {
139 .name = "gfxnvidia",
140 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000141 .map_flash_region = fallback_map,
142 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000143 .delay = internal_delay,
144 },
145#endif
146
hailfinger90c7d542010-05-31 15:27:27 +0000147#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000148 {
uwee2f95ef2009-09-02 23:00:46 +0000149 .name = "drkaiser",
150 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000151 .map_flash_region = fallback_map,
152 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000153 .delay = internal_delay,
154 },
hailfinger571a6b32009-09-16 10:09:21 +0000155#endif
uwee2f95ef2009-09-02 23:00:46 +0000156
hailfinger90c7d542010-05-31 15:27:27 +0000157#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000158 {
hailfinger3548a9a2009-08-12 14:34:35 +0000159 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000160 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000161 .map_flash_region = fallback_map,
162 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000163 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000164 },
hailfinger571a6b32009-09-16 10:09:21 +0000165#endif
ruikda922a12009-05-17 19:39:27 +0000166
hailfinger90c7d542010-05-31 15:27:27 +0000167#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000168 {
169 .name = "atahpt",
170 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000171 .map_flash_region = fallback_map,
172 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000173 .delay = internal_delay,
174 },
175#endif
176
hailfinger90c7d542010-05-31 15:27:27 +0000177#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000178 {
hailfinger90c7d542010-05-31 15:27:27 +0000179 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000180 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000181 .map_flash_region = fallback_map,
182 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000183 .delay = internal_delay,
184 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000185#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000186
hailfinger90c7d542010-05-31 15:27:27 +0000187#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000188 {
hailfinger3548a9a2009-08-12 14:34:35 +0000189 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000190 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000191 .map_flash_region = fallback_map,
192 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000193 .delay = serprog_delay,
194 },
hailfinger74d88a72009-08-12 16:17:41 +0000195#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000196
hailfinger90c7d542010-05-31 15:27:27 +0000197#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000198 {
hailfinger90c7d542010-05-31 15:27:27 +0000199 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000200 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000201 .map_flash_region = fallback_map,
202 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000203 .delay = internal_delay,
204 },
205#endif
206
Anton Staafb2647882014-09-17 15:13:43 -0700207#if CONFIG_RAIDEN_DEBUG_SPI == 1
208 {
209 .name = "raiden_debug_spi",
210 .init = raiden_debug_spi_init,
211 .map_flash_region = fallback_map,
212 .unmap_flash_region = fallback_unmap,
213 .delay = internal_delay,
214 },
215#endif
216
hailfinger90c7d542010-05-31 15:27:27 +0000217#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000218 {
219 .name = "dediprog",
220 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000221 .map_flash_region = fallback_map,
222 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000223 .delay = internal_delay,
224 },
225#endif
226
hailfinger52c4fa02010-07-21 10:26:01 +0000227#if CONFIG_RAYER_SPI == 1
228 {
229 .name = "rayer_spi",
230 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000231 .map_flash_region = fallback_map,
232 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000233 .delay = internal_delay,
234 },
235#endif
236
hailfinger7949b652011-05-08 00:24:18 +0000237#if CONFIG_NICINTEL == 1
238 {
239 .name = "nicintel",
240 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000241 .map_flash_region = fallback_map,
242 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000243 .delay = internal_delay,
244 },
245#endif
246
uwe6764e922010-09-03 18:21:21 +0000247#if CONFIG_NICINTEL_SPI == 1
248 {
uwe8d342eb2011-07-28 08:13:25 +0000249 .name = "nicintel_spi",
250 .init = nicintel_spi_init,
251 .map_flash_region = fallback_map,
252 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000253 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000254 },
255#endif
256
hailfingerfb1f31f2010-12-03 14:48:11 +0000257#if CONFIG_OGP_SPI == 1
258 {
uwe8d342eb2011-07-28 08:13:25 +0000259 .name = "ogp_spi",
260 .init = ogp_spi_init,
261 .map_flash_region = fallback_map,
262 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000263 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000264 },
265#endif
266
hailfinger935365d2011-02-04 21:37:59 +0000267#if CONFIG_SATAMV == 1
268 {
269 .name = "satamv",
270 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000271 .map_flash_region = fallback_map,
272 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000273 .delay = internal_delay,
274 },
275#endif
276
uwe7df6dda2011-09-03 18:37:52 +0000277#if CONFIG_LINUX_SPI == 1
278 {
279 .name = "linux_spi",
280 .init = linux_spi_init,
281 .map_flash_region = fallback_map,
282 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000283 .delay = internal_delay,
284 },
285#endif
286
hailfinger3548a9a2009-08-12 14:34:35 +0000287 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000288};
stepan927d4e22007-04-04 22:45:58 +0000289
David Hendricksbf36f092010-11-02 23:39:29 -0700290#define CHIP_RESTORE_MAXFN 4
291static int chip_restore_fn_count = 0;
292struct chip_restore_func_data {
293 CHIP_RESTORE_CALLBACK;
294 struct flashchip *flash;
295 uint8_t status;
296} static chip_restore_fn[CHIP_RESTORE_MAXFN];
297
David Hendricks668f29d2011-01-27 18:51:45 -0800298
hailfingerf31cbdc2010-11-10 15:25:18 +0000299#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000300static int shutdown_fn_count = 0;
301struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000302 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000303 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000304} static shutdown_fn[SHUTDOWN_MAXFN];
305/* Initialize to 0 to make sure nobody registers a shutdown function before
306 * programmer init.
307 */
308static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000309
stefanct569dbb62011-07-01 00:19:12 +0000310static int check_block_eraser(const struct flashchip *flash, int k, int log);
311
hailfingerdc6f7972010-02-14 01:20:28 +0000312/* Register a function to be executed on programmer shutdown.
313 * The advantage over atexit() is that you can supply a void pointer which will
314 * be used as parameter to the registered function upon programmer shutdown.
315 * This pointer can point to arbitrary data used by said function, e.g. undo
316 * information for GPIO settings etc. If unneeded, set data=NULL.
317 * Please note that the first (void *data) belongs to the function signature of
318 * the function passed as first parameter.
319 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000320int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000321{
322 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000323 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000324 SHUTDOWN_MAXFN);
325 return 1;
326 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000327 if (!may_register_shutdown) {
328 msg_perr("Tried to register a shutdown function before "
329 "programmer init.\n");
330 return 1;
331 }
hailfingerdc6f7972010-02-14 01:20:28 +0000332 shutdown_fn[shutdown_fn_count].func = function;
333 shutdown_fn[shutdown_fn_count].data = data;
334 shutdown_fn_count++;
335
336 return 0;
337}
338
David Hendricksbf36f092010-11-02 23:39:29 -0700339//int register_chip_restore(int (*function) (void *data), void *data)
340int register_chip_restore(CHIP_RESTORE_CALLBACK,
341 struct flashchip *flash, uint8_t status)
342{
343 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
344 msg_perr("Tried to register more than %i chip restore"
345 " functions.\n", CHIP_RESTORE_MAXFN);
346 return 1;
347 }
348 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
349 chip_restore_fn[chip_restore_fn_count].flash = flash;
350 chip_restore_fn[chip_restore_fn_count].status = status;
351 chip_restore_fn_count++;
352
353 return 0;
354}
355
hailfinger969e2f32011-09-08 00:00:29 +0000356int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000357{
hailfinger1ef766d2010-07-06 09:55:48 +0000358 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000359
360 if (prog >= PROGRAMMER_INVALID) {
361 msg_perr("Invalid programmer specified!\n");
362 return -1;
363 }
364 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000365 /* Initialize all programmer specific data. */
366 /* Default to unlimited decode sizes. */
367 max_rom_decode = (const struct decode_sizes) {
368 .parallel = 0xffffffff,
369 .lpc = 0xffffffff,
370 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000371 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000372 };
hailfingere1e41ea2011-07-27 07:13:06 +0000373 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000374 /* Default to top aligned flash at 4 GB. */
375 flashbase = 0;
376 /* Registering shutdown functions is now allowed. */
377 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000378 /* Default to allowing writes. Broken programmers set this to 0. */
379 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000380
381 programmer_param = param;
382 msg_pdbg("Initializing %s programmer\n",
383 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000384 ret = programmer_table[programmer].init();
385 if (programmer_param && strlen(programmer_param)) {
386 msg_perr("Unhandled programmer parameters: %s\n",
387 programmer_param);
388 /* Do not error out here, the init itself was successful. */
389 }
390 return ret;
uweabe92a52009-05-16 22:36:00 +0000391}
392
David Hendricksbf36f092010-11-02 23:39:29 -0700393int chip_restore()
394{
395 int rc = 0;
396
397 while (chip_restore_fn_count > 0) {
398 int i = --chip_restore_fn_count;
399 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
400 chip_restore_fn[i].status);
401 }
402
403 return rc;
404}
405
uweabe92a52009-05-16 22:36:00 +0000406int programmer_shutdown(void)
407{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000408 int ret = 0;
409
hailfinger1ff33dc2010-07-03 11:02:10 +0000410 /* Registering shutdown functions is no longer allowed. */
411 may_register_shutdown = 0;
412 while (shutdown_fn_count > 0) {
413 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000414 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000415 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000416 return ret;
uweabe92a52009-05-16 22:36:00 +0000417}
418
419void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
420 size_t len)
421{
422 return programmer_table[programmer].map_flash_region(descr,
423 phys_addr, len);
424}
425
426void programmer_unmap_flash_region(void *virt_addr, size_t len)
427{
428 programmer_table[programmer].unmap_flash_region(virt_addr, len);
429}
430
431void chip_writeb(uint8_t val, chipaddr addr)
432{
hailfinger76bb7e92011-11-09 23:40:00 +0000433 par_programmer->chip_writeb(val, addr);
uweabe92a52009-05-16 22:36:00 +0000434}
435
436void chip_writew(uint16_t val, chipaddr addr)
437{
hailfinger76bb7e92011-11-09 23:40:00 +0000438 par_programmer->chip_writew(val, addr);
uweabe92a52009-05-16 22:36:00 +0000439}
440
441void chip_writel(uint32_t val, chipaddr addr)
442{
hailfinger76bb7e92011-11-09 23:40:00 +0000443 par_programmer->chip_writel(val, addr);
uweabe92a52009-05-16 22:36:00 +0000444}
445
hailfinger9d987ef2009-06-05 18:32:07 +0000446void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
447{
hailfinger76bb7e92011-11-09 23:40:00 +0000448 par_programmer->chip_writen(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000449}
450
uweabe92a52009-05-16 22:36:00 +0000451uint8_t chip_readb(const chipaddr addr)
452{
hailfinger76bb7e92011-11-09 23:40:00 +0000453 return par_programmer->chip_readb(addr);
uweabe92a52009-05-16 22:36:00 +0000454}
455
456uint16_t chip_readw(const chipaddr addr)
457{
hailfinger76bb7e92011-11-09 23:40:00 +0000458 return par_programmer->chip_readw(addr);
uweabe92a52009-05-16 22:36:00 +0000459}
460
461uint32_t chip_readl(const chipaddr addr)
462{
hailfinger76bb7e92011-11-09 23:40:00 +0000463 return par_programmer->chip_readl(addr);
uweabe92a52009-05-16 22:36:00 +0000464}
465
hailfinger9d987ef2009-06-05 18:32:07 +0000466void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
467{
hailfinger76bb7e92011-11-09 23:40:00 +0000468 par_programmer->chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000469}
470
hailfingere5829f62009-06-05 17:48:08 +0000471void programmer_delay(int usecs)
472{
473 programmer_table[programmer].delay(usecs);
474}
475
stuge5ff0e6c2009-01-26 00:39:57 +0000476void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000477{
stepan15e64bc2007-05-24 08:48:10 +0000478 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000479 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000480 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000481 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000482}
483
stefanctc5eb8a92011-11-23 09:13:48 +0000484int read_memmapped(struct flashchip *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000485{
hailfinger0f08b7a2009-06-16 08:55:44 +0000486 chip_readn(buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000487
hailfinger23060112009-05-08 12:49:03 +0000488 return 0;
489}
490
hailfinger7b414742009-06-13 12:04:03 +0000491int min(int a, int b)
492{
493 return (a < b) ? a : b;
494}
495
hailfinger7af83692009-06-15 17:23:36 +0000496int max(int a, int b)
497{
498 return (a > b) ? a : b;
499}
500
hailfingeraec9c962009-10-31 01:53:09 +0000501int bitcount(unsigned long a)
502{
503 int i = 0;
504 for (; a != 0; a >>= 1)
505 if (a & 1)
506 i++;
507 return i;
508}
509
hailfingerf76cc322010-11-09 22:00:31 +0000510void tolower_string(char *str)
511{
512 for (; *str != '\0'; str++)
513 *str = (char)tolower((unsigned char)*str);
514}
515
hailfingera916b422009-06-01 02:08:58 +0000516char *strcat_realloc(char *dest, const char *src)
517{
518 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000519 if (!dest) {
520 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000521 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000522 }
hailfingera916b422009-06-01 02:08:58 +0000523 strcat(dest, src);
524 return dest;
525}
526
hailfinger6e5a52a2009-11-24 18:27:10 +0000527/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000528 * It will look for needle with a subsequent '=' in haystack, return a copy of
529 * needle and remove everything from the first occurrence of needle to the next
530 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000531 */
stefanct52700282011-06-26 17:38:17 +0000532char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000533{
hailfinger1ef766d2010-07-06 09:55:48 +0000534 char *param_pos, *opt_pos, *rest;
535 char *opt = NULL;
536 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000537 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000538
hailfingerf4aaccc2010-04-28 15:22:14 +0000539 needlelen = strlen(needle);
540 if (!needlelen) {
541 msg_gerr("%s: empty needle! Please report a bug at "
542 "flashrom@flashrom.org\n", __func__);
543 return NULL;
544 }
545 /* No programmer parameters given. */
546 if (*haystack == NULL)
547 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000548 param_pos = strstr(*haystack, needle);
549 do {
550 if (!param_pos)
551 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000552 /* Needle followed by '='? */
553 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600554
hailfinger1ef766d2010-07-06 09:55:48 +0000555 /* Beginning of the string? */
556 if (param_pos == *haystack)
557 break;
558 /* After a delimiter? */
559 if (strchr(delim, *(param_pos - 1)))
560 break;
561 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000562 /* Continue searching. */
563 param_pos++;
564 param_pos = strstr(param_pos, needle);
565 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000566
hailfinger6e5a52a2009-11-24 18:27:10 +0000567 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000568 /* Get the string after needle and '='. */
569 opt_pos = param_pos + needlelen + 1;
570 optlen = strcspn(opt_pos, delim);
571 /* Return an empty string if the parameter was empty. */
572 opt = malloc(optlen + 1);
573 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000574 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000575 exit(1);
576 }
hailfinger1ef766d2010-07-06 09:55:48 +0000577 strncpy(opt, opt_pos, optlen);
578 opt[optlen] = '\0';
579 rest = opt_pos + optlen;
580 /* Skip all delimiters after the current parameter. */
581 rest += strspn(rest, delim);
582 memmove(param_pos, rest, strlen(rest) + 1);
583 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000584 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000585
hailfinger1ef766d2010-07-06 09:55:48 +0000586 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000587}
588
stefanct52700282011-06-26 17:38:17 +0000589char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000590{
591 return extract_param(&programmer_param, param_name, ",");
592}
593
stefancte1c5acf2011-07-04 07:27:17 +0000594/* Returns the number of well-defined erasers for a chip. */
595static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000596{
597 unsigned int usable_erasefunctions = 0;
598 int k;
599 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
600 if (!check_block_eraser(flash, k, 0))
601 usable_erasefunctions++;
602 }
603 return usable_erasefunctions;
604}
605
hailfinger7af83692009-06-15 17:23:36 +0000606/* start is an offset to the base address of the flash chip */
stefanctc5eb8a92011-11-23 09:13:48 +0000607int check_erased_range(struct flashchip *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000608{
609 int ret;
610 uint8_t *cmpbuf = malloc(len);
611
612 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000613 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000614 exit(1);
615 }
Simon Glass4c214132013-07-16 10:09:28 -0600616 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000617 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
618 free(cmpbuf);
619 return ret;
620}
621
uwee15beb92010-08-08 17:01:18 +0000622/*
hailfinger7af3d192009-11-25 17:05:52 +0000623 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000624 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000625 * @start offset to the base address of the flash chip
626 * @len length of the verified area
627 * @message string to print in the "FAILED" message
628 * @return 0 for success, -1 for failure
629 */
stefanctc5eb8a92011-11-23 09:13:48 +0000630int verify_range(struct flashchip *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000631 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000632{
stefanctc5eb8a92011-11-23 09:13:48 +0000633 unsigned int i;
hailfinger8cb6ece2010-11-16 17:21:58 +0000634 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000635 int ret = 0, failcount = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -0800636 unsigned int chunksize;
hailfinger7af83692009-06-15 17:23:36 +0000637
638 if (!len)
639 goto out_free;
640
hailfingerb0f4d122009-06-24 08:20:45 +0000641 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000642 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000643 return 1;
644 }
hailfinger7af83692009-06-15 17:23:36 +0000645 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000646 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000647 exit(1);
648 }
649
650 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000651 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000652 " total_size 0x%x\n", __func__, start, len,
653 flash->total_size * 1024);
654 ret = -1;
655 goto out_free;
656 }
657 if (!message)
658 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000659
David Hendricks758e2fb2014-08-19 20:34:43 -0700660 for (i = 0, chunksize = 0; i < len; i += chunksize) {
David Hendricks1ed1d352011-11-23 17:54:37 -0800661 int tmp, j;
662
David Hendricks758e2fb2014-08-19 20:34:43 -0700663 chunksize = min(flash->page_size, len - i);
David Hendricks1ed1d352011-11-23 17:54:37 -0800664 tmp = flash->read(flash, readbuf + i, start + i, chunksize);
665
David Hendrickse3451942013-03-21 17:23:29 -0700666 /* Since this function explicitly compares the bytes, we need
667 to handle errors manually */
David Hendricks1ed1d352011-11-23 17:54:37 -0800668 if (tmp) {
669 ret = tmp;
David Hendricks758e2fb2014-08-19 20:34:43 -0700670 if (ignore_error(tmp))
David Hendricks1ed1d352011-11-23 17:54:37 -0800671 continue;
David Hendricks758e2fb2014-08-19 20:34:43 -0700672 else
David Hendricks1ed1d352011-11-23 17:54:37 -0800673 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800674 }
675
676 for (j = 0; j < chunksize; j++) {
677 if (cmpbuf[i + j] != readbuf[i + j]) {
678 /* Only print the first failure. */
679 if (!failcount++)
680 msg_cerr("%s FAILED at 0x%08x! "
681 "Expected=0x%02x, Read=0x%02x,",
682 message, start + i + j, cmpbuf[i + j],
683 readbuf[j]);
684 }
685 }
hailfinger8cb6ece2010-11-16 17:21:58 +0000686 }
687
hailfinger5be6c0f2009-07-23 01:42:56 +0000688 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000689 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000690 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000691 ret = -1;
692 }
hailfinger7af83692009-06-15 17:23:36 +0000693
694out_free:
695 free(readbuf);
696 return ret;
697}
698
uwee15beb92010-08-08 17:01:18 +0000699/*
hailfingerb247c7a2010-03-08 00:42:32 +0000700 * Check if the buffer @have can be programmed to the content of @want without
701 * erasing. This is only possible if all chunks of size @gran are either kept
702 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000703 *
hailfingerb247c7a2010-03-08 00:42:32 +0000704 * The following write granularities (enum @gran) are known:
705 * - 1 bit. Each bit can be cleared individually.
706 * - 1 byte. A byte can be written once. Further writes to an already written
707 * byte cause the contents to be either undefined or to stay unchanged.
708 * - 128 bytes. If less than 128 bytes are written, the rest will be
709 * erased. Each write to a 128-byte region will trigger an automatic erase
710 * before anything is written. Very uncommon behaviour and unsupported by
711 * this function.
712 * - 256 bytes. If less than 256 bytes are written, the contents of the
713 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000714 * Warning: This function assumes that @have and @want point to naturally
715 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000716 *
717 * @have buffer with current content
718 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000719 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000720 * @gran write granularity (enum, not count)
721 * @return 0 if no erase is needed, 1 otherwise
722 */
Simon Glass4c214132013-07-16 10:09:28 -0600723static int need_erase(struct flashchip *flash, uint8_t *have, uint8_t *want,
724 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000725{
hailfingerb91c08c2011-08-15 19:54:20 +0000726 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000727 unsigned int i, j, limit;
Simon Glass4c214132013-07-16 10:09:28 -0600728 int erase_value = flash_erase_value(flash);
hailfingerb247c7a2010-03-08 00:42:32 +0000729
730 switch (gran) {
731 case write_gran_1bit:
732 for (i = 0; i < len; i++)
733 if ((have[i] & want[i]) != want[i]) {
734 result = 1;
735 break;
736 }
737 break;
738 case write_gran_1byte:
739 for (i = 0; i < len; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600740 if ((have[i] != want[i]) && (have[i] != erase_value)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000741 result = 1;
742 break;
743 }
744 break;
745 case write_gran_256bytes:
746 for (j = 0; j < len / 256; j++) {
747 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000748 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000749 if (!memcmp(have + j * 256, want + j * 256, limit))
750 continue;
751 /* have needs to be in erased state. */
752 for (i = 0; i < limit; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600753 if (have[j * 256 + i] != erase_value) {
hailfingerb247c7a2010-03-08 00:42:32 +0000754 result = 1;
755 break;
756 }
757 if (result)
758 break;
759 }
760 break;
hailfingerb437e282010-11-04 01:04:27 +0000761 default:
762 msg_cerr("%s: Unsupported granularity! Please report a bug at "
763 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000764 }
765 return result;
766}
767
hailfingerb437e282010-11-04 01:04:27 +0000768/**
769 * Check if the buffer @have needs to be programmed to get the content of @want.
770 * If yes, return 1 and fill in first_start with the start address of the
771 * write operation and first_len with the length of the first to-be-written
772 * chunk. If not, return 0 and leave first_start and first_len undefined.
773 *
774 * Warning: This function assumes that @have and @want point to naturally
775 * aligned regions.
776 *
777 * @have buffer with current content
778 * @want buffer with desired content
779 * @len length of the checked area
780 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000781 * @first_start offset of the first byte which needs to be written (passed in
782 * value is increased by the offset of the first needed write
783 * relative to have/want or unchanged if no write is needed)
784 * @return length of the first contiguous area which needs to be written
785 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000786 *
787 * FIXME: This function needs a parameter which tells it about coalescing
788 * in relation to the max write length of the programmer and the max write
789 * length of the chip.
790 */
stefanctc5eb8a92011-11-23 09:13:48 +0000791static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
792 unsigned int *first_start,
793 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000794{
stefanctc5eb8a92011-11-23 09:13:48 +0000795 int need_write = 0;
796 unsigned int rel_start = 0, first_len = 0;
797 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000798
hailfingerb437e282010-11-04 01:04:27 +0000799 switch (gran) {
800 case write_gran_1bit:
801 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000802 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000803 break;
804 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000805 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000806 break;
807 default:
808 msg_cerr("%s: Unsupported granularity! Please report a bug at "
809 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000810 /* Claim that no write was needed. A write with unknown
811 * granularity is too dangerous to try.
812 */
813 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000814 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000815 for (i = 0; i < len / stride; i++) {
816 limit = min(stride, len - i * stride);
817 /* Are 'have' and 'want' identical? */
818 if (memcmp(have + i * stride, want + i * stride, limit)) {
819 if (!need_write) {
820 /* First location where have and want differ. */
821 need_write = 1;
822 rel_start = i * stride;
823 }
824 } else {
825 if (need_write) {
826 /* First location where have and want
827 * do not differ anymore.
828 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000829 break;
830 }
831 }
832 }
hailfingerffb7f382010-12-06 13:05:44 +0000833 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000834 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000835 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000836 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000837}
838
hailfinger0c515352009-11-23 12:55:31 +0000839/* This function generates various test patterns useful for testing controller
840 * and chip communication as well as chip behaviour.
841 *
842 * If a byte can be written multiple times, each time keeping 0-bits at 0
843 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
844 * is essentially an AND operation. That's also the reason why this function
845 * provides the result of AND between various patterns.
846 *
847 * Below is a list of patterns (and their block length).
848 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
849 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
850 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
851 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
852 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
853 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
854 * Pattern 6 is 00 (1 Byte)
855 * Pattern 7 is ff (1 Byte)
856 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
857 * byte block.
858 *
859 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
860 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
861 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
862 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
863 * Pattern 12 is 00 (1 Byte)
864 * Pattern 13 is ff (1 Byte)
865 * Patterns 8-13 have no block number.
866 *
867 * Patterns 0-3 are created to detect and efficiently diagnose communication
868 * slips like missed bits or bytes and their repetitive nature gives good visual
869 * cues to the person inspecting the results. In addition, the following holds:
870 * AND Pattern 0/1 == Pattern 4
871 * AND Pattern 2/3 == Pattern 5
872 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
873 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
874 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
875 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
876 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
877 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
878 * Besides that, they provide for bit testing of the last two bytes of every
879 * 256 byte block which contains the block number for patterns 0-6.
880 * Patterns 10-11 are special purpose for detecting subblock aliasing with
881 * block sizes >256 bytes (some Dataflash chips etc.)
882 * AND Pattern 8/9 == Pattern 12
883 * AND Pattern 10/11 == Pattern 12
884 * Pattern 13 is the completely erased state.
885 * None of the patterns can detect aliasing at boundaries which are a multiple
886 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
887 */
888int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
889{
890 int i;
891
892 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000893 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000894 return 1;
895 }
896
897 switch (variant) {
898 case 0:
899 for (i = 0; i < size; i++)
900 buf[i] = (i & 0xf) << 4 | 0x5;
901 break;
902 case 1:
903 for (i = 0; i < size; i++)
904 buf[i] = (i & 0xf) << 4 | 0xa;
905 break;
906 case 2:
907 for (i = 0; i < size; i++)
908 buf[i] = 0x50 | (i & 0xf);
909 break;
910 case 3:
911 for (i = 0; i < size; i++)
912 buf[i] = 0xa0 | (i & 0xf);
913 break;
914 case 4:
915 for (i = 0; i < size; i++)
916 buf[i] = (i & 0xf) << 4;
917 break;
918 case 5:
919 for (i = 0; i < size; i++)
920 buf[i] = i & 0xf;
921 break;
922 case 6:
923 memset(buf, 0x00, size);
924 break;
925 case 7:
926 memset(buf, 0xff, size);
927 break;
928 case 8:
929 for (i = 0; i < size; i++)
930 buf[i] = i & 0xff;
931 break;
932 case 9:
933 for (i = 0; i < size; i++)
934 buf[i] = ~(i & 0xff);
935 break;
936 case 10:
937 for (i = 0; i < size % 2; i++) {
938 buf[i * 2] = (i >> 8) & 0xff;
939 buf[i * 2 + 1] = i & 0xff;
940 }
941 if (size & 0x1)
942 buf[i * 2] = (i >> 8) & 0xff;
943 break;
944 case 11:
945 for (i = 0; i < size % 2; i++) {
946 buf[i * 2] = ~((i >> 8) & 0xff);
947 buf[i * 2 + 1] = ~(i & 0xff);
948 }
949 if (size & 0x1)
950 buf[i * 2] = ~((i >> 8) & 0xff);
951 break;
952 case 12:
953 memset(buf, 0x00, size);
954 break;
955 case 13:
956 memset(buf, 0xff, size);
957 break;
958 }
959
960 if ((variant >= 0) && (variant <= 7)) {
961 /* Write block number in the last two bytes of each 256-byte
962 * block, big endian for easier reading of the hexdump.
963 * Note that this wraps around for chips larger than 2^24 bytes
964 * (16 MB).
965 */
966 for (i = 0; i < size / 256; i++) {
967 buf[i * 256 + 254] = (i >> 8) & 0xff;
968 buf[i * 256 + 255] = i & 0xff;
969 }
970 }
971
972 return 0;
973}
974
hailfingeraec9c962009-10-31 01:53:09 +0000975int check_max_decode(enum chipbustype buses, uint32_t size)
976{
977 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +0000978
979 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000980 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000981 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000982 "size %u kB of chipset/board/programmer "
983 "for %s interface, "
984 "probe/read/erase/write may fail. ", size / 1024,
985 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +0000986 }
hailfingere1e41ea2011-07-27 07:13:06 +0000987 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000988 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000989 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000990 "size %u kB of chipset/board/programmer "
991 "for %s interface, "
992 "probe/read/erase/write may fail. ", size / 1024,
993 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +0000994 }
hailfingere1e41ea2011-07-27 07:13:06 +0000995 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000996 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000997 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000998 "size %u kB of chipset/board/programmer "
999 "for %s interface, "
1000 "probe/read/erase/write may fail. ", size / 1024,
1001 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001002 }
hailfingere1e41ea2011-07-27 07:13:06 +00001003 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001004 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001005 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001006 "size %u kB of chipset/board/programmer "
1007 "for %s interface, "
1008 "probe/read/erase/write may fail. ", size / 1024,
1009 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001010 }
1011 if (!limitexceeded)
1012 return 0;
1013 /* Sometimes chip and programmer have more than one bus in common,
1014 * and the limit is not exceeded on all buses. Tell the user.
1015 */
1016 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001017 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001018 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001019 "interface which can support a chip of this size. "
1020 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001021 return 1;
1022}
1023
hailfinger48ed3e22011-05-04 00:39:50 +00001024int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001025{
hailfinger48ed3e22011-05-04 00:39:50 +00001026 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001027 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001028 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001029 uint32_t size;
1030 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001031 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001032
hailfinger48ed3e22011-05-04 00:39:50 +00001033 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001034 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001035 continue;
hailfingeraec9c962009-10-31 01:53:09 +00001036 buses_common = buses_supported & flash->bustype;
1037 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001038 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1039 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +00001040 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +00001041 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001042 free(tmp);
1043 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001044 msg_gspew("and chip bus type %s are incompatible.",
1045 tmp);
hailfingera916b422009-06-01 02:08:58 +00001046 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001047 msg_gspew("\n");
1048 continue;
1049 }
1050 msg_gdbg("Probing for %s %s, %d kB: ",
1051 flash->vendor, flash->name, flash->total_size);
1052 if (!flash->probe && !force) {
1053 msg_gdbg("failed! flashrom has no probe function for "
1054 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001055 continue;
1056 }
stepan782fb172007-04-06 11:58:03 +00001057
ollie5672ac62004-03-17 22:22:08 +00001058 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001059 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001060
hailfinger48ed3e22011-05-04 00:39:50 +00001061 /* Start filling in the dynamic data. */
1062 *fill_flash = *flash;
1063
hailfinger72d3b982009-05-09 07:27:23 +00001064 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001065 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001066
stugec1e55fe2008-07-02 17:15:47 +00001067 if (force)
1068 break;
stepanc98b80b2006-03-16 16:57:41 +00001069
hailfinger48ed3e22011-05-04 00:39:50 +00001070 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001071 goto notfound;
1072
hailfinger48ed3e22011-05-04 00:39:50 +00001073 /* If this is the first chip found, accept it.
1074 * If this is not the first chip found, accept it only if it is
1075 * a non-generic match.
1076 * We could either make chipcount global or provide it as
1077 * parameter, or we assume that startchip==0 means this call to
1078 * probe_flash() is the first one and thus no chip has been
1079 * found before.
1080 */
1081 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001082 break;
1083
stuge56300c32008-09-03 23:10:05 +00001084notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001085 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001086 }
uwebe4477b2007-08-23 16:08:21 +00001087
stugec1e55fe2008-07-02 17:15:47 +00001088 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001089 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001090
hailfingere11396b2011-03-08 00:09:11 +00001091#if CONFIG_INTERNAL == 1
1092 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001093 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001094 else
1095#endif
stepan3e7aeae2011-01-19 06:21:54 +00001096 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001097
stefanct588b6d22011-06-26 20:45:35 +00001098 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001099 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1100 force ? "Assuming" : "Found", fill_flash->vendor,
1101 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001102 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001103
hailfinger0f4c3952010-12-02 21:59:42 +00001104 /* Flash registers will not be mapped if the chip was forced. Lock info
1105 * may be stored in registers, so avoid lock info printing.
1106 */
1107 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001108 if (fill_flash->printlock)
1109 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001110
hailfinger48ed3e22011-05-04 00:39:50 +00001111 /* Return position of matching chip. */
1112 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001113}
1114
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001115int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001116{
hailfingerb0f4d122009-06-24 08:20:45 +00001117 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001118 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001119
snelsone42c3802010-05-07 20:09:04 +00001120 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001121
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001122 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1123 ret = handle_partial_verify(flash, buf, verify_range);
1124 } else {
1125 ret = verify_range(flash, buf, 0, total_size, NULL);
1126 }
uwef6641642007-05-09 10:17:44 +00001127
David Hendricks1ed1d352011-11-23 17:54:37 -08001128 if (ret == ACCESS_DENIED) {
1129 msg_gdbg("Could not fully verify due to access error, ");
1130 if (access_denied_action == error_ignore) {
1131 msg_gdbg("ignoring\n");
1132 ret = 0;
1133 } else {
1134 msg_gdbg("aborting\n");
1135 }
1136 }
1137
hailfingerb0f4d122009-06-24 08:20:45 +00001138 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001139 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001140
hailfingerb0f4d122009-06-24 08:20:45 +00001141 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001142}
1143
uwe8d342eb2011-07-28 08:13:25 +00001144int read_buf_from_file(unsigned char *buf, unsigned long size,
1145 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001146{
1147 unsigned long numbytes;
1148 FILE *image;
1149 struct stat image_stat;
1150
Vincent Palatin7ab23932014-10-01 12:09:16 -07001151 if (!strncmp(filename, "-", sizeof("-")))
1152 image = fdopen(STDIN_FILENO, "rb");
1153 else
1154 image = fopen(filename, "rb");
1155 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001156 perror(filename);
1157 return 1;
1158 }
1159 if (fstat(fileno(image), &image_stat) != 0) {
1160 perror(filename);
1161 fclose(image);
1162 return 1;
1163 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001164 if ((image_stat.st_size != size) &&
1165 (strncmp(filename, "-", sizeof("-")))) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001166 msg_gerr("Error: Image size doesn't match: stat %ld bytes, "
1167 "wanted %ld!\n", image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001168 fclose(image);
1169 return 1;
1170 }
1171 numbytes = fread(buf, 1, size, image);
1172 if (fclose(image)) {
1173 perror(filename);
1174 return 1;
1175 }
1176 if (numbytes != size) {
1177 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1178 "wanted %ld!\n", numbytes, size);
1179 return 1;
1180 }
1181 return 0;
1182}
1183
uwe8d342eb2011-07-28 08:13:25 +00001184int write_buf_to_file(unsigned char *buf, unsigned long size,
1185 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001186{
1187 unsigned long numbytes;
1188 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001189
1190 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001191 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001192 return 1;
1193 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001194 if (!strncmp(filename, "-", sizeof("-")))
1195 image = fdopen(STDOUT_FILENO, "wb");
1196 else
1197 image = fopen(filename, "wb");
1198 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001199 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001200 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001201 }
hailfingerd219a232009-01-28 00:27:54 +00001202
hailfingerd219a232009-01-28 00:27:54 +00001203 numbytes = fwrite(buf, 1, size, image);
1204 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001205 if (numbytes != size) {
1206 msg_gerr("File %s could not be written completely.\n",
1207 filename);
hailfingerd219a232009-01-28 00:27:54 +00001208 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001209 }
hailfingerd219a232009-01-28 00:27:54 +00001210 return 0;
1211}
1212
David Hendrickse3451942013-03-21 17:23:29 -07001213/*
1214 * read_flash - wrapper for flash->read() with additional high-level policy
1215 *
1216 * @flash flash chip
1217 * @buf buffer to store data in
1218 * @start start address
1219 * @len number of bytes to read
1220 *
1221 * This wrapper simplifies most cases when the flash chip needs to be read
1222 * since policy decisions such as non-fatal error handling is centralized.
1223 */
1224int read_flash(struct flashchip *flash, uint8_t *buf,
1225 unsigned int start, unsigned int len)
1226{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001227 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001228
1229 if (!flash || !flash->read)
1230 return -1;
1231
David Hendricks4e76fdc2013-05-13 16:05:36 -07001232 ret = flash->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001233 if (ret) {
1234 if (ignore_error(ret)) {
1235 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1236 start, start + len - 1);
1237 ret = 0;
1238 } else {
1239 msg_gdbg("failed to read 0x%x-0x%x\n",
1240 start, start + len - 1);
1241 }
1242 }
1243
1244 return ret;
1245}
1246
David Hendricks7c8a1612013-04-26 19:14:44 -07001247/*
1248 * write_flash - wrapper for flash->write() with additional high-level policy
1249 *
1250 * @flash flash chip
1251 * @buf buffer to write to flash
1252 * @start start address in flash
1253 * @len number of bytes to write
1254 *
1255 * TODO: Look up regions that are write-protected and avoid attempt to write
1256 * to them at all.
1257 */
1258int write_flash(struct flashchip *flash, uint8_t *buf,
1259 unsigned int start, unsigned int len)
1260{
1261 if (!flash || !flash->write)
1262 return -1;
1263
1264 return flash->write(flash, buf, start, len);
1265}
1266
stefanct52700282011-06-26 17:38:17 +00001267int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001268{
1269 unsigned long size = flash->total_size * 1024;
1270 unsigned char *buf = calloc(size, sizeof(char));
1271 int ret = 0;
1272
1273 msg_cinfo("Reading flash... ");
1274 if (!buf) {
1275 msg_gerr("Memory allocation failed!\n");
1276 msg_cinfo("FAILED.\n");
1277 return 1;
1278 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001279
1280 /* To support partial read, fill buffer to all 0xFF at beginning to make
1281 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001282 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001283
hailfinger42a850a2010-07-13 23:56:13 +00001284 if (!flash->read) {
1285 msg_cerr("No read function available for this flash chip.\n");
1286 ret = 1;
1287 goto out_free;
1288 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001289
1290 /* First try to handle partial read case, rather than read the whole
1291 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001292 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001293 if (ret < 0) {
1294 msg_cerr("Partial read operation failed!\n");
1295 ret = 1;
1296 goto out_free;
1297 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001298 int num_regions = get_num_include_args();
1299
1300 if (ret != num_regions) {
1301 msg_cerr("Requested %d regions, but only read %d\n",
1302 num_regions, ret);
1303 ret = 1;
1304 goto out_free;
1305 }
1306
1307 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001308 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001309 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001310 msg_cerr("Read operation failed!\n");
1311 ret = 1;
1312 goto out_free;
1313 }
hailfinger42a850a2010-07-13 23:56:13 +00001314 }
1315
David Hendricksdf29a832013-06-28 14:33:51 -07001316 if (filename)
1317 ret = write_buf_to_file(buf, size, filename);
1318
hailfinger42a850a2010-07-13 23:56:13 +00001319out_free:
1320 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001321 if (ret)
1322 msg_cerr("FAILED.");
1323 else
1324 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001325 return ret;
1326}
1327
hailfingerb437e282010-11-04 01:04:27 +00001328/* This function shares a lot of its structure with erase_and_write_flash() and
1329 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001330 * Even if an error is found, the function will keep going and check the rest.
1331 */
hailfinger48ed3e22011-05-04 00:39:50 +00001332static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001333{
hailfingerb91c08c2011-08-15 19:54:20 +00001334 int i, j, k;
1335 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001336
1337 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1338 unsigned int done = 0;
1339 struct block_eraser eraser = flash->block_erasers[k];
1340
1341 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1342 /* Blocks with zero size are bugs in flashchips.c. */
1343 if (eraser.eraseblocks[i].count &&
1344 !eraser.eraseblocks[i].size) {
1345 msg_gerr("ERROR: Flash chip %s erase function "
1346 "%i region %i has size 0. Please report"
1347 " a bug at flashrom@flashrom.org\n",
1348 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001349 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001350 }
1351 /* Blocks with zero count are bugs in flashchips.c. */
1352 if (!eraser.eraseblocks[i].count &&
1353 eraser.eraseblocks[i].size) {
1354 msg_gerr("ERROR: Flash chip %s erase function "
1355 "%i region %i has count 0. Please report"
1356 " a bug at flashrom@flashrom.org\n",
1357 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001358 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001359 }
1360 done += eraser.eraseblocks[i].count *
1361 eraser.eraseblocks[i].size;
1362 }
hailfinger9fed35d2010-01-19 06:42:46 +00001363 /* Empty eraseblock definition with erase function. */
1364 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001365 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001366 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001367 if (!done)
1368 continue;
1369 if (done != flash->total_size * 1024) {
1370 msg_gerr("ERROR: Flash chip %s erase function %i "
1371 "region walking resulted in 0x%06x bytes total,"
1372 " expected 0x%06x bytes. Please report a bug at"
1373 " flashrom@flashrom.org\n", flash->name, k,
1374 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001375 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001376 }
hailfinger9fed35d2010-01-19 06:42:46 +00001377 if (!eraser.block_erase)
1378 continue;
1379 /* Check if there are identical erase functions for different
1380 * layouts. That would imply "magic" erase functions. The
1381 * easiest way to check this is with function pointers.
1382 */
uwef6f94d42010-03-13 17:28:29 +00001383 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001384 if (eraser.block_erase ==
1385 flash->block_erasers[j].block_erase) {
1386 msg_gerr("ERROR: Flash chip %s erase function "
1387 "%i and %i are identical. Please report"
1388 " a bug at flashrom@flashrom.org\n",
1389 flash->name, k, j);
1390 ret = 1;
1391 }
uwef6f94d42010-03-13 17:28:29 +00001392 }
hailfinger45177872010-01-18 08:14:43 +00001393 }
hailfinger9fed35d2010-01-19 06:42:46 +00001394 return ret;
hailfinger45177872010-01-18 08:14:43 +00001395}
1396
hailfingerb437e282010-11-04 01:04:27 +00001397static int erase_and_write_block_helper(struct flashchip *flash,
1398 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001399 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001400 uint8_t *newcontents,
1401 int (*erasefn) (struct flashchip *flash,
1402 unsigned int addr,
1403 unsigned int len))
1404{
stefanctc5eb8a92011-11-23 09:13:48 +00001405 unsigned int starthere = 0, lenhere = 0;
1406 int ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001407 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1408
hailfinger90fcf9b2010-11-05 14:51:59 +00001409 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001410 * need to be adjusted here to keep the impression of proper abstraction
1411 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001412 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001413 newcontents += start;
1414 msg_cdbg(":");
1415 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001416 if (need_erase(flash, curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001417 msg_cdbg("E");
1418 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001419 if (ret) {
1420 if (ret == ACCESS_DENIED)
1421 msg_cdbg("D");
1422 else
1423 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001424 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001425 }
1426
hailfingerac8e3182011-06-26 17:04:16 +00001427 if (check_erased_range(flash, start, len)) {
1428 msg_cerr("ERASE FAILED!\n");
1429 return -1;
1430 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001431 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001432 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001433 skip = 0;
1434 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001435 /* get_next_write() sets starthere to a new value after the call. */
1436 while ((lenhere = get_next_write(curcontents + starthere,
1437 newcontents + starthere,
1438 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001439 if (!writecount++)
1440 msg_cdbg("W");
1441 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001442 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001443 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001444 if (ret) {
1445 if (ret == ACCESS_DENIED)
1446 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001447 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001448 }
hailfingerb437e282010-11-04 01:04:27 +00001449 starthere += lenhere;
1450 skip = 0;
1451 }
1452 if (skip)
1453 msg_cdbg("S");
1454 return ret;
1455}
1456
hailfinger83541b32010-07-13 00:42:00 +00001457static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1458 int (*do_something) (struct flashchip *flash,
1459 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001460 unsigned int len,
1461 uint8_t *param1,
1462 uint8_t *param2,
1463 int (*erasefn) (
1464 struct flashchip *flash,
1465 unsigned int addr,
1466 unsigned int len)),
1467 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001468{
David Hendricks1ed1d352011-11-23 17:54:37 -08001469 int i, j, rc = 0;
hailfingerb91c08c2011-08-15 19:54:20 +00001470 unsigned int start = 0;
1471 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001472 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001473
hailfinger2b8c9382010-07-13 00:37:19 +00001474 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1475 /* count==0 for all automatically initialized array
1476 * members so the loop below won't be executed for them.
1477 */
1478 len = eraser.eraseblocks[i].size;
1479 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001480 /* Print this for every block except the first one. */
1481 if (i || j)
1482 msg_cdbg(", ");
1483 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001484 start + len - 1);
David Hendricks1ed1d352011-11-23 17:54:37 -08001485 rc = do_something(flash, start, len, param1, param2,
1486 eraser.block_erase);
1487 if (rc) {
1488 if (ignore_error(rc))
1489 rc = 0;
1490 else
1491 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001492 }
hailfinger2b8c9382010-07-13 00:37:19 +00001493 start += len;
1494 }
1495 }
hailfingerb437e282010-11-04 01:04:27 +00001496 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001497 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001498}
1499
stefanct569dbb62011-07-01 00:19:12 +00001500static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001501{
1502 struct block_eraser eraser = flash->block_erasers[k];
1503
1504 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1505 if (log)
1506 msg_cdbg("not defined. ");
1507 return 1;
1508 }
1509 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1510 if (log)
1511 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001512 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001513 return 1;
1514 }
1515 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1516 if (log)
1517 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001518 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001519 return 1;
1520 }
1521 return 0;
1522}
1523
uwe8d342eb2011-07-28 08:13:25 +00001524int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1525 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001526{
hailfinger8f524a82011-07-21 21:21:04 +00001527 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001528 uint8_t *curcontents;
1529 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001530 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001531
hailfingercf848f12010-12-05 15:14:44 +00001532 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001533 curcontents = malloc(size);
1534 if (!curcontents) {
1535 msg_gerr("Out of memory!\n");
1536 exit(1);
1537 }
hailfingerb437e282010-11-04 01:04:27 +00001538 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1539 memcpy(curcontents, oldcontents, size);
1540
hailfinger7df21362009-09-05 02:30:58 +00001541 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001542 if (k != 0)
1543 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001544 if (!usable_erasefunctions) {
1545 msg_cdbg("No usable erase functions left.\n");
1546 break;
1547 }
stefanctf0bc4712011-07-26 14:28:35 +00001548 msg_cdbg("Trying erase function %i... ", k);
1549 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001550 continue;
hailfingercf848f12010-12-05 15:14:44 +00001551 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001552 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1553 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001554 /* If everything is OK, don't try another erase function. */
1555 if (!ret)
1556 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001557 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001558 * contents are. If no usable erase functions remain, we can
1559 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001560 */
hailfingercf848f12010-12-05 15:14:44 +00001561 if (!usable_erasefunctions)
1562 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001563 /* Reading the whole chip may take a while, inform the user even
1564 * in non-verbose mode.
1565 */
1566 msg_cinfo("Reading current flash chip contents... ");
David Hendrickse3451942013-03-21 17:23:29 -07001567 if (read_flash(flash, curcontents, 0, size)) {
hailfinger6237f5e2010-12-02 02:41:55 +00001568 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001569 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001570 /* We have no idea about the flash chip contents, so
1571 * retrying with another erase function is pointless.
1572 */
1573 break;
1574 }
David Hendricks89a45e52011-11-22 16:56:22 -08001575 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001576 }
hailfingerb437e282010-11-04 01:04:27 +00001577 /* Free the scratchpad. */
1578 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001579
hailfinger7df21362009-09-05 02:30:58 +00001580 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001581 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001582 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001583 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001584 }
1585 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001586}
1587
hailfinger4c47e9d2010-10-19 22:06:20 +00001588void nonfatal_help_message(void)
1589{
1590 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1591 "This means we have to add special support for your board, "
1592 "programmer or flash chip.\n"
1593 "Please report this on IRC at irc.freenode.net (channel "
1594 "#flashrom) or\n"
1595 "mail flashrom@flashrom.org!\n"
1596 "-------------------------------------------------------------"
1597 "------------------\n"
1598 "You may now reboot or simply leave the machine running.\n");
1599}
1600
uweb34ec9f2009-10-01 18:40:02 +00001601void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001602{
snelsone42c3802010-05-07 20:09:04 +00001603 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001604 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001605 "mail flashrom@flashrom.org with FAILED: your board name in "
1606 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001607 "-------------------------------------------------------------"
1608 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001609 "DO NOT REBOOT OR POWEROFF!\n");
1610}
1611
uwe8d342eb2011-07-28 08:13:25 +00001612/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001613void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001614{
1615 enum programmer p;
1616 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001617 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001618 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001619 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001620 }
Simon Glass8dc82732013-07-16 10:13:51 -06001621 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001622}
1623
hailfingerf79d1712010-10-06 23:48:34 +00001624void list_programmers_linebreak(int startcol, int cols, int paren)
1625{
1626 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001627 int pnamelen;
1628 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001629 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001630 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001631
1632 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1633 pname = programmer_table[p].name;
1634 pnamelen = strlen(pname);
1635 if (remaining - pnamelen - 2 < 0) {
1636 if (firstline)
1637 firstline = 0;
1638 else
1639 printf("\n");
1640 for (i = 0; i < startcol; i++)
1641 printf(" ");
1642 remaining = cols - startcol;
1643 } else {
1644 printf(" ");
1645 remaining--;
1646 }
1647 if (paren && (p == 0)) {
1648 printf("(");
1649 remaining--;
1650 }
1651 printf("%s", pname);
1652 remaining -= pnamelen;
1653 if (p < PROGRAMMER_INVALID - 1) {
1654 printf(",");
1655 remaining--;
1656 } else {
1657 if (paren)
1658 printf(")");
1659 printf("\n");
1660 }
1661 }
1662}
1663
hailfinger3b471632010-03-27 16:36:40 +00001664void print_sysinfo(void)
1665{
David Hendricksc6c9f822010-11-03 15:07:01 -07001666 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001667#if HAVE_UTSNAME == 1
1668 struct utsname osinfo;
1669 uname(&osinfo);
1670
David Hendricksc6c9f822010-11-03 15:07:01 -07001671 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001672 osinfo.machine);
1673#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001674 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001675#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001676 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001677#if NEED_PCI == 1
1678#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001679 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001680#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001681 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001682#endif
1683#endif
1684#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001685 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001686#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001687 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001688#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001689 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001690#endif
hailfinger3b471632010-03-27 16:36:40 +00001691#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001692 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001693#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001694 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001695#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001696 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001697#endif
1698#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001699 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001700#endif
1701#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001702 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001703#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001704 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001705#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001706 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001707}
1708
uwefdeca092008-01-21 15:24:22 +00001709void print_version(void)
1710{
David Hendricksc6c9f822010-11-03 15:07:01 -07001711 /* send to stderr for chromium os */
1712 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001713 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001714}
1715
hailfinger74819ad2010-05-15 15:04:37 +00001716void print_banner(void)
1717{
1718 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001719 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001720 msg_ginfo("\n");
1721}
1722
hailfingerc77acb52009-12-24 02:15:55 +00001723int selfcheck(void)
1724{
hailfinger45177872010-01-18 08:14:43 +00001725 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001726 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001727
1728 /* Safety check. Instead of aborting after the first error, check
1729 * if more errors exist.
1730 */
hailfingerc77acb52009-12-24 02:15:55 +00001731 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001732 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001733 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001734 }
stefanct8632c922011-07-26 14:33:46 +00001735 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001736 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001737 * For 'flashchips' we check the first element to be non-null. In the
1738 * other cases there exist use cases where the first element can be
1739 * null. */
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001740 if (!flashchips || flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001741 msg_gerr("Flashchips table miscompilation!\n");
1742 ret = 1;
1743 }
hailfinger45177872010-01-18 08:14:43 +00001744 for (flash = flashchips; flash && flash->name; flash++)
1745 if (selfcheck_eraseblocks(flash))
1746 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001747
1748#if CONFIG_INTERNAL == 1
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001749 if (!chipset_enables) {
stefanct6d836ba2011-05-26 01:35:19 +00001750 msg_gerr("Chipset enables table does not exist!\n");
1751 ret = 1;
1752 }
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001753 if (!board_matches) {
stefanct6d836ba2011-05-26 01:35:19 +00001754 msg_gerr("Board enables table does not exist!\n");
1755 ret = 1;
1756 }
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001757 if (!boards_known) {
stefanct6d836ba2011-05-26 01:35:19 +00001758 msg_gerr("Known boards table does not exist!\n");
1759 ret = 1;
1760 }
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001761 if (!laptops_known) {
stefanct6d836ba2011-05-26 01:35:19 +00001762 msg_gerr("Known laptops table does not exist!\n");
1763 ret = 1;
1764 }
uwe8d342eb2011-07-28 08:13:25 +00001765#endif
hailfinger45177872010-01-18 08:14:43 +00001766 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001767}
1768
hailfinger48ed3e22011-05-04 00:39:50 +00001769void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001770{
1771 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001772 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001773 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001774 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001775 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001776 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001777 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001778 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001779 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001780 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001781 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001782 msg_cdbg(" WRITE");
1783 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001784 }
1785 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1786 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1787 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1788 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001789 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001790 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001791 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001792 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001793 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001794 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001795 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001796 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001797 msg_cdbg(" WRITE");
1798 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001799 }
hailfinger92cd8e32010-01-07 03:24:05 +00001800 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001801 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001802 "in the latest development\n"
1803 "version of flashrom. If you are running the latest "
1804 "development version,\n"
1805 "please email a report to flashrom@flashrom.org if "
1806 "any of the above operations\n"
1807 "work correctly for you with this flash part. Please "
1808 "include the flashrom\n"
1809 "output with the additional -V option for all "
1810 "operations you tested (-V, -Vr,\n"
1811 "-Vw, -VE), and mention which mainboard or "
1812 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001813 "Please mention your board in the subject line. "
1814 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001815 }
1816}
1817
hailfinger771fc182010-10-15 00:01:14 +00001818/* FIXME: This function signature needs to be improved once doit() has a better
1819 * function signature.
1820 */
stefanct02116582011-05-18 01:30:56 +00001821int chip_safety_check(struct flashchip *flash, int force, int read_it, int write_it, int erase_it, int verify_it)
hailfinger771fc182010-10-15 00:01:14 +00001822{
1823 if (!programmer_may_write && (write_it || erase_it)) {
1824 msg_perr("Write/erase is not working yet on your programmer in "
1825 "its current configuration.\n");
1826 /* --force is the wrong approach, but it's the best we can do
1827 * until the generic programmer parameter parser is merged.
1828 */
1829 if (!force)
1830 return 1;
1831 msg_cerr("Continuing anyway.\n");
1832 }
1833
1834 if (read_it || erase_it || write_it || verify_it) {
1835 /* Everything needs read. */
1836 if (flash->tested & TEST_BAD_READ) {
1837 msg_cerr("Read is not working on this chip. ");
1838 if (!force)
1839 return 1;
1840 msg_cerr("Continuing anyway.\n");
1841 }
1842 if (!flash->read) {
1843 msg_cerr("flashrom has no read function for this "
1844 "flash chip.\n");
1845 return 1;
1846 }
1847 }
1848 if (erase_it || write_it) {
1849 /* Write needs erase. */
1850 if (flash->tested & TEST_BAD_ERASE) {
1851 msg_cerr("Erase is not working on this chip. ");
1852 if (!force)
1853 return 1;
1854 msg_cerr("Continuing anyway.\n");
1855 }
stefancte1c5acf2011-07-04 07:27:17 +00001856 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001857 msg_cerr("flashrom has no erase function for this "
1858 "flash chip.\n");
1859 return 1;
1860 }
hailfinger771fc182010-10-15 00:01:14 +00001861 }
1862 if (write_it) {
1863 if (flash->tested & TEST_BAD_WRITE) {
1864 msg_cerr("Write is not working on this chip. ");
1865 if (!force)
1866 return 1;
1867 msg_cerr("Continuing anyway.\n");
1868 }
1869 if (!flash->write) {
1870 msg_cerr("flashrom has no write function for this "
1871 "flash chip.\n");
1872 return 1;
1873 }
1874 }
1875 return 0;
1876}
1877
hailfingerc77acb52009-12-24 02:15:55 +00001878/* This function signature is horrible. We need to design a better interface,
1879 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001880 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001881 */
Simon Glass9ad06c12013-07-03 22:08:17 +09001882int doit(struct flashchip *flash, int force, const char *filename, int read_it,
1883 int write_it, int erase_it, int verify_it, int extract_it,
1884 const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001885{
hailfinger4c47e9d2010-10-19 22:06:20 +00001886 uint8_t *oldcontents;
1887 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001888 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001889 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001890
stefanct02116582011-05-18 01:30:56 +00001891 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001892 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001893 ret = 1;
1894 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001895 }
1896
hailfinger771fc182010-10-15 00:01:14 +00001897 /* Given the existence of read locks, we want to unlock for read,
1898 * erase and write.
1899 */
1900 if (flash->unlock)
1901 flash->unlock(flash);
1902
David Hendricks6d62d752011-03-07 21:20:22 -08001903 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001904 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001905 ret = 1;
1906 goto out_nofree;
1907 }
1908
Simon Glass9ad06c12013-07-03 22:08:17 +09001909 if (extract_it) {
1910 ret = extract_regions(flash);
1911 goto out_nofree;
1912 }
1913
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001914 /* mark entries included using -i argument as "included" if they are
1915 found in the master rom_entries list */
1916 if (process_include_args() < 0) {
1917 ret = 1;
1918 goto out_nofree;
1919 }
1920
hailfinger771fc182010-10-15 00:01:14 +00001921 if (read_it) {
1922 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001923 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001924 }
hailfingerb437e282010-11-04 01:04:27 +00001925
stefanctd611e8f2011-07-12 22:35:21 +00001926 oldcontents = malloc(size);
1927 if (!oldcontents) {
1928 msg_gerr("Out of memory!\n");
1929 exit(1);
1930 }
Simon Glass4c214132013-07-16 10:09:28 -06001931 /* Assume worst case: All blocks are not erased. */
1932 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00001933 newcontents = malloc(size);
1934 if (!newcontents) {
1935 msg_gerr("Out of memory!\n");
1936 exit(1);
1937 }
Simon Glass4c214132013-07-16 10:09:28 -06001938 /* Assume best case: All blocks are erased. */
1939 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00001940 /* Side effect of the assumptions above: Default write action is erase
1941 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06001942 * oldcontents being completely unerased means we have to erase
1943 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00001944 */
1945
ollie0eb62d62004-12-08 20:10:01 +00001946 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001947 /* FIXME: Do we really want the scary warning if erase failed?
1948 * After all, after erase the chip is either blank or partially
1949 * blank or it has the old contents. A blank chip won't boot,
1950 * so if the user wanted erase and reboots afterwards, the user
1951 * knows very well that booting won't work.
1952 */
hailfingerb437e282010-11-04 01:04:27 +00001953 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001954 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001955 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001956 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001957 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001958 }
hailfinger771fc182010-10-15 00:01:14 +00001959
hailfingerd217d122010-10-08 18:52:29 +00001960 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07001961 /*
1962 * Note: This must be done before any files specified by -i
1963 * arguments are processed merged into the newcontents since
1964 * -i files take priority. See http://crbug.com/263495.
1965 */
1966 if (filename) {
1967 if (read_buf_from_file(newcontents, size, filename)) {
1968 ret = 1;
1969 goto out;
1970 }
1971 } else {
1972 /* Content will be read from -i args, so they must
1973 * not overlap. */
1974 if (included_regions_overlap()) {
1975 msg_gerr("Error: Included regions must "
1976 "not overlap.\n");
1977 ret = 1;
1978 goto out;
1979 }
stepan1da96c02006-11-21 23:48:51 +00001980 }
1981
David Hendricksac82cac2012-06-19 10:29:37 -07001982#if 0
1983 /*
1984 * FIXME: show_id() causes failure if vendor:mainboard do not
1985 * match. This may happen if codenames are in flux.
1986 * See chrome-os-partner:10414.
1987 */
hailfinger90c7d542010-05-31 15:27:27 +00001988#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001989 if (programmer == PROGRAMMER_INTERNAL)
1990 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001991#endif
David Hendricksac82cac2012-06-19 10:29:37 -07001992#endif
ollie5672ac62004-03-17 22:22:08 +00001993 }
1994
David Hendricksc44d7a02011-10-17 11:28:43 -07001995 /* Obtain a reference image so that we can check whether regions need
1996 * to be erased and to give better diagnostics in case write fails.
David Hendricks52ddff02013-07-23 15:05:14 -07001997 * If --fast-verify is used then only the regions which are included
1998 * using -i will be read.
hailfinger4c47e9d2010-10-19 22:06:20 +00001999 */
David Hendricksc44d7a02011-10-17 11:28:43 -07002000 if (diff_file) {
2001 msg_cdbg("Reading old contents from file... ");
2002 if (read_buf_from_file(oldcontents, size, diff_file)) {
2003 ret = 1;
2004 msg_cdbg("FAILED.\n");
2005 goto out;
2006 }
2007 } else {
2008 msg_cdbg("Reading old contents from flash chip... ");
David Hendricksd4e712c2013-08-02 17:06:16 -07002009 if (verify_it == VERIFY_PARTIAL) {
2010 if (handle_partial_read(flash, oldcontents,
2011 read_flash, 0) < 0) {
David Hendricks52ddff02013-07-23 15:05:14 -07002012 ret = 1;
2013 msg_cdbg("FAILED.\n");
2014 goto out;
2015 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002016 } else {
2017 if (read_flash(flash, oldcontents, 0, size)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002018 ret = 1;
2019 msg_cdbg("FAILED.\n");
2020 goto out;
2021 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002022 }
hailfinger4c47e9d2010-10-19 22:06:20 +00002023 }
David Hendricks89a45e52011-11-22 16:56:22 -08002024 msg_cdbg("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00002025
David Hendricksdf29a832013-06-28 14:33:51 -07002026 /*
2027 * Note: This must be done after reading the file specified for the
2028 * -w/-v argument, if any, so that files specified using -i end up
2029 * in the "newcontents" buffer before being written.
2030 * See http://crbug.com/263495.
2031 */
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002032 if (handle_romentries(flash, oldcontents, newcontents)) {
2033 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002034 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002035 goto out;
2036 }
uwef6641642007-05-09 10:17:44 +00002037
stuge8ce3a3c2008-04-28 14:47:30 +00002038 if (write_it) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002039 // parse the new fmap
David Hendricksb907de32014-08-11 16:47:09 -07002040 if ((ret = cros_ec_prepare(newcontents, size))) {
2041 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002042 goto out;
2043 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002044
hailfingerb437e282010-11-04 01:04:27 +00002045 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2046 msg_cerr("Uh oh. Erase/write failed. Checking if "
2047 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002048 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002049 if (!memcmp(oldcontents, newcontents, size)) {
2050 msg_cinfo("Good. It seems nothing was "
2051 "changed.\n");
2052 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002053 ret = 1;
2054 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002055 }
2056 }
hailfingerd217d122010-10-08 18:52:29 +00002057 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002058 ret = 1;
2059 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002060 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002061
David Hendricksb907de32014-08-11 16:47:09 -07002062 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002063 if (ret < 0) {
2064 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002065 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002066 emergency_help_message();
2067 ret = 1;
2068 goto out;
2069 } else if (ret > 0) {
2070 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002071 msg_pdbg("CROS_EC needs 2nd pass.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002072 if (read_flash(flash, oldcontents, 0, size)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002073 msg_cerr("Uh oh. Cannot get latest content.\n");
2074 emergency_help_message();
2075 ret = 1;
2076 goto out;
2077 }
2078 // write 2nd pass
2079 if (erase_and_write_flash(flash, oldcontents,
2080 newcontents)) {
David Hendricksb907de32014-08-11 16:47:09 -07002081 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002082 emergency_help_message();
2083 ret = 1;
2084 goto out;
2085 }
2086 ret = 0;
2087 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002088
David Hendricksb907de32014-08-11 16:47:09 -07002089 if (cros_ec_finish() < 0) {
2090 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002091 emergency_help_message();
2092 ret = 1;
2093 goto out;
2094 }
stuge8ce3a3c2008-04-28 14:47:30 +00002095 }
ollie6a600992005-11-26 21:55:36 +00002096
hailfinger0459e1c2009-08-19 13:55:34 +00002097 if (verify_it) {
2098 /* Work around chips which need some time to calm down. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002099 if (write_it && verify_it != VERIFY_PARTIAL)
hailfinger0459e1c2009-08-19 13:55:34 +00002100 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002101
2102 ret = verify_flash(flash, newcontents, verify_it);
2103
hailfingera50d60e2009-11-17 09:57:34 +00002104 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00002105 * might have an emergency situation.
2106 */
2107 if (ret && write_it)
2108 emergency_help_message();
2109 }
ollie6a600992005-11-26 21:55:36 +00002110
hailfinger90fcf9b2010-11-05 14:51:59 +00002111out:
2112 free(oldcontents);
2113 free(newcontents);
2114out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002115 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002116 /*
2117 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2118 * tree. This is because some operations, such as write protection,
2119 * requires programmer_shutdown() but does not call doit().
2120 */
2121// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002122 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002123}