blob: 701320502a07507911426401e3f3bfdd5bb0227a [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;
Souvik Ghosh3c963a42016-07-19 18:48:15 -070045int verbose_screen = MSG_ERROR;
46int verbose_logfile = MSG_DEBUG2;
hailfinger80422e22009-12-13 22:28:00 +000047
David Hendricks54777392015-01-11 18:55:14 -080048unsigned int required_erase_size = 0; /* see comment in flash.h */
49
David Hendricks9ba79fb2015-04-03 12:06:16 -070050/* Set if any erase/write operation is to be done. This will be used to
51 * decide if final verification is needed. */
52static int content_has_changed = 0;
53
David Hendricks1ed1d352011-11-23 17:54:37 -080054/* error handling stuff */
55enum error_action access_denied_action = error_ignore;
56
57int ignore_error(int err) {
58 int rc = 0;
59
60 switch(err) {
61 case ACCESS_DENIED:
62 if (access_denied_action == error_ignore)
63 rc = 1;
64 break;
65 default:
66 break;
67 }
68
69 return rc;
70}
71
hailfinger969e2f32011-09-08 00:00:29 +000072static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000073
hailfingerddeb4ac2010-07-08 10:13:37 +000074static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000075
David Hendricksac1d25c2016-08-09 17:00:58 -070076/* Supported buses for the current programmer. */
77enum chipbustype buses_supported;
78
uwee15beb92010-08-08 17:01:18 +000079/*
hailfinger80422e22009-12-13 22:28:00 +000080 * Programmers supporting multiple buses can have differing size limits on
81 * each bus. Store the limits for each bus in a common struct.
82 */
hailfinger1ff33dc2010-07-03 11:02:10 +000083struct decode_sizes max_rom_decode;
84
85/* If nonzero, used as the start address of bottom-aligned flash. */
86unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000087
hailfinger5828baf2010-07-03 12:14:25 +000088/* Is writing allowed with this programmer? */
89int programmer_may_write;
90
hailfingerabe249e2009-05-08 17:43:22 +000091const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000092#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000093 {
hailfinger3548a9a2009-08-12 14:34:35 +000094 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000095 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000096 .map_flash_region = physmap,
97 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000098 .delay = internal_delay,
David Hendricks55cdd9c2015-11-25 14:37:26 -080099
100 /*
101 * "Internal" implies in-system programming on a live system, so
102 * handle with paranoia to catch errors early. If something goes
103 * wrong then hopefully the system will still be recoverable.
104 */
105 .paranoid = 1,
hailfingerabe249e2009-05-08 17:43:22 +0000106 },
hailfinger80422e22009-12-13 22:28:00 +0000107#endif
stepan927d4e22007-04-04 22:45:58 +0000108
hailfinger90c7d542010-05-31 15:27:27 +0000109#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000110 {
hailfinger3548a9a2009-08-12 14:34:35 +0000111 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000112 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000113 .map_flash_region = dummy_map,
114 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000115 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000116 },
hailfinger571a6b32009-09-16 10:09:21 +0000117#endif
hailfingera9df33c2009-05-09 00:54:55 +0000118
hailfinger90c7d542010-05-31 15:27:27 +0000119#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000120 {
hailfinger3548a9a2009-08-12 14:34:35 +0000121 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000122 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000123 .map_flash_region = fallback_map,
124 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000125 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000126 },
hailfinger571a6b32009-09-16 10:09:21 +0000127#endif
uwe0f5a3a22009-05-13 11:36:06 +0000128
hailfinger90c7d542010-05-31 15:27:27 +0000129#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000130 {
hailfinger0d703d42011-03-07 01:08:09 +0000131 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000132 .name = "nicrealtek",
133 //.name = "nicsmc1211",
134 .init = nicrealtek_init,
135 .map_flash_region = fallback_map,
136 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000137 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000138 },
hailfinger5aa36982010-05-21 21:54:07 +0000139#endif
140
hailfingerf0a368f2010-06-07 22:37:54 +0000141#if CONFIG_NICNATSEMI == 1
142 {
uwe8d342eb2011-07-28 08:13:25 +0000143 .name = "nicnatsemi",
144 .init = nicnatsemi_init,
145 .map_flash_region = fallback_map,
146 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000147 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000148 },
149#endif
hailfinger5aa36982010-05-21 21:54:07 +0000150
hailfinger90c7d542010-05-31 15:27:27 +0000151#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000152 {
153 .name = "gfxnvidia",
154 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000155 .map_flash_region = fallback_map,
156 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000157 .delay = internal_delay,
158 },
159#endif
160
hailfinger90c7d542010-05-31 15:27:27 +0000161#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000162 {
uwee2f95ef2009-09-02 23:00:46 +0000163 .name = "drkaiser",
164 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000165 .map_flash_region = fallback_map,
166 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000167 .delay = internal_delay,
168 },
hailfinger571a6b32009-09-16 10:09:21 +0000169#endif
uwee2f95ef2009-09-02 23:00:46 +0000170
hailfinger90c7d542010-05-31 15:27:27 +0000171#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000172 {
hailfinger3548a9a2009-08-12 14:34:35 +0000173 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000174 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000175 .map_flash_region = fallback_map,
176 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000177 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000178 },
hailfinger571a6b32009-09-16 10:09:21 +0000179#endif
ruikda922a12009-05-17 19:39:27 +0000180
hailfinger90c7d542010-05-31 15:27:27 +0000181#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000182 {
183 .name = "atahpt",
184 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000185 .map_flash_region = fallback_map,
186 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000187 .delay = internal_delay,
188 },
189#endif
190
hailfinger90c7d542010-05-31 15:27:27 +0000191#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000192 {
hailfinger90c7d542010-05-31 15:27:27 +0000193 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000194 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000195 .map_flash_region = fallback_map,
196 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000197 .delay = internal_delay,
198 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000199#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000200
hailfinger90c7d542010-05-31 15:27:27 +0000201#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000202 {
hailfinger3548a9a2009-08-12 14:34:35 +0000203 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000204 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000205 .map_flash_region = fallback_map,
206 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000207 .delay = serprog_delay,
208 },
hailfinger74d88a72009-08-12 16:17:41 +0000209#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000210
hailfinger90c7d542010-05-31 15:27:27 +0000211#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000212 {
hailfinger90c7d542010-05-31 15:27:27 +0000213 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000214 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000215 .map_flash_region = fallback_map,
216 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000217 .delay = internal_delay,
218 },
219#endif
220
Anton Staafb2647882014-09-17 15:13:43 -0700221#if CONFIG_RAIDEN_DEBUG_SPI == 1
222 {
223 .name = "raiden_debug_spi",
224 .init = raiden_debug_spi_init,
225 .map_flash_region = fallback_map,
226 .unmap_flash_region = fallback_unmap,
227 .delay = internal_delay,
228 },
229#endif
230
hailfinger90c7d542010-05-31 15:27:27 +0000231#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000232 {
233 .name = "dediprog",
234 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000235 .map_flash_region = fallback_map,
236 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000237 .delay = internal_delay,
238 },
239#endif
240
hailfinger52c4fa02010-07-21 10:26:01 +0000241#if CONFIG_RAYER_SPI == 1
242 {
243 .name = "rayer_spi",
244 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000245 .map_flash_region = fallback_map,
246 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000247 .delay = internal_delay,
248 },
249#endif
250
hailfinger7949b652011-05-08 00:24:18 +0000251#if CONFIG_NICINTEL == 1
252 {
253 .name = "nicintel",
254 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000255 .map_flash_region = fallback_map,
256 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000257 .delay = internal_delay,
258 },
259#endif
260
uwe6764e922010-09-03 18:21:21 +0000261#if CONFIG_NICINTEL_SPI == 1
262 {
uwe8d342eb2011-07-28 08:13:25 +0000263 .name = "nicintel_spi",
264 .init = nicintel_spi_init,
265 .map_flash_region = fallback_map,
266 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000267 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000268 },
269#endif
270
hailfingerfb1f31f2010-12-03 14:48:11 +0000271#if CONFIG_OGP_SPI == 1
272 {
uwe8d342eb2011-07-28 08:13:25 +0000273 .name = "ogp_spi",
274 .init = ogp_spi_init,
275 .map_flash_region = fallback_map,
276 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000277 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000278 },
279#endif
280
hailfinger935365d2011-02-04 21:37:59 +0000281#if CONFIG_SATAMV == 1
282 {
283 .name = "satamv",
284 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000285 .map_flash_region = fallback_map,
286 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000287 .delay = internal_delay,
288 },
289#endif
290
David Hendrickscebee892015-05-23 20:30:30 -0700291#if CONFIG_LINUX_MTD == 1
292 {
293 .name = "linux_mtd",
294 .init = linux_mtd_init,
295 .map_flash_region = fallback_map,
296 .unmap_flash_region = fallback_unmap,
297 .delay = internal_delay,
298 },
299#endif
300
uwe7df6dda2011-09-03 18:37:52 +0000301#if CONFIG_LINUX_SPI == 1
302 {
303 .name = "linux_spi",
304 .init = linux_spi_init,
305 .map_flash_region = fallback_map,
306 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000307 .delay = internal_delay,
308 },
309#endif
310
hailfinger3548a9a2009-08-12 14:34:35 +0000311 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000312};
stepan927d4e22007-04-04 22:45:58 +0000313
David Hendricksbf36f092010-11-02 23:39:29 -0700314#define CHIP_RESTORE_MAXFN 4
315static int chip_restore_fn_count = 0;
316struct chip_restore_func_data {
317 CHIP_RESTORE_CALLBACK;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700318 struct flashctx *flash;
David Hendricksbf36f092010-11-02 23:39:29 -0700319 uint8_t status;
320} static chip_restore_fn[CHIP_RESTORE_MAXFN];
321
David Hendricks668f29d2011-01-27 18:51:45 -0800322
hailfingerf31cbdc2010-11-10 15:25:18 +0000323#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000324static int shutdown_fn_count = 0;
325struct shutdown_func_data {
David Hendricks93784b42016-08-09 17:00:38 -0700326 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000327 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000328} static shutdown_fn[SHUTDOWN_MAXFN];
329/* Initialize to 0 to make sure nobody registers a shutdown function before
330 * programmer init.
331 */
332static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000333
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700334static int check_block_eraser(const struct flashctx *flash, int k, int log);
stefanct569dbb62011-07-01 00:19:12 +0000335
hailfingerdc6f7972010-02-14 01:20:28 +0000336/* Register a function to be executed on programmer shutdown.
337 * The advantage over atexit() is that you can supply a void pointer which will
338 * be used as parameter to the registered function upon programmer shutdown.
339 * This pointer can point to arbitrary data used by said function, e.g. undo
340 * information for GPIO settings etc. If unneeded, set data=NULL.
341 * Please note that the first (void *data) belongs to the function signature of
342 * the function passed as first parameter.
343 */
David Hendricks93784b42016-08-09 17:00:38 -0700344int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000345{
346 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000347 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000348 SHUTDOWN_MAXFN);
349 return 1;
350 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000351 if (!may_register_shutdown) {
352 msg_perr("Tried to register a shutdown function before "
353 "programmer init.\n");
354 return 1;
355 }
hailfingerdc6f7972010-02-14 01:20:28 +0000356 shutdown_fn[shutdown_fn_count].func = function;
357 shutdown_fn[shutdown_fn_count].data = data;
358 shutdown_fn_count++;
359
360 return 0;
361}
362
David Hendricksbf36f092010-11-02 23:39:29 -0700363//int register_chip_restore(int (*function) (void *data), void *data)
364int register_chip_restore(CHIP_RESTORE_CALLBACK,
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700365 struct flashctx *flash, uint8_t status)
David Hendricksbf36f092010-11-02 23:39:29 -0700366{
367 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
368 msg_perr("Tried to register more than %i chip restore"
369 " functions.\n", CHIP_RESTORE_MAXFN);
370 return 1;
371 }
372 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
373 chip_restore_fn[chip_restore_fn_count].flash = flash;
374 chip_restore_fn[chip_restore_fn_count].status = status;
375 chip_restore_fn_count++;
376
377 return 0;
378}
379
David Hendricksac1d25c2016-08-09 17:00:58 -0700380int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000381{
hailfinger1ef766d2010-07-06 09:55:48 +0000382 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000383
384 if (prog >= PROGRAMMER_INVALID) {
385 msg_perr("Invalid programmer specified!\n");
386 return -1;
387 }
388 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000389 /* Initialize all programmer specific data. */
390 /* Default to unlimited decode sizes. */
391 max_rom_decode = (const struct decode_sizes) {
392 .parallel = 0xffffffff,
393 .lpc = 0xffffffff,
394 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000395 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000396 };
David Hendricksac1d25c2016-08-09 17:00:58 -0700397 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000398 /* Default to top aligned flash at 4 GB. */
399 flashbase = 0;
400 /* Registering shutdown functions is now allowed. */
401 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000402 /* Default to allowing writes. Broken programmers set this to 0. */
403 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000404
405 programmer_param = param;
406 msg_pdbg("Initializing %s programmer\n",
407 programmer_table[programmer].name);
David Hendricksac1d25c2016-08-09 17:00:58 -0700408 ret = programmer_table[programmer].init();
hailfinger1ef766d2010-07-06 09:55:48 +0000409 if (programmer_param && strlen(programmer_param)) {
410 msg_perr("Unhandled programmer parameters: %s\n",
411 programmer_param);
412 /* Do not error out here, the init itself was successful. */
413 }
414 return ret;
uweabe92a52009-05-16 22:36:00 +0000415}
416
David Hendricksbf36f092010-11-02 23:39:29 -0700417int chip_restore()
418{
419 int rc = 0;
420
421 while (chip_restore_fn_count > 0) {
422 int i = --chip_restore_fn_count;
423 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
424 chip_restore_fn[i].status);
425 }
426
427 return rc;
428}
429
David Hendricks93784b42016-08-09 17:00:38 -0700430int programmer_shutdown(void)
uweabe92a52009-05-16 22:36:00 +0000431{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000432 int ret = 0;
433
hailfinger1ff33dc2010-07-03 11:02:10 +0000434 /* Registering shutdown functions is no longer allowed. */
435 may_register_shutdown = 0;
436 while (shutdown_fn_count > 0) {
437 int i = --shutdown_fn_count;
David Hendricks93784b42016-08-09 17:00:38 -0700438 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000439 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000440 return ret;
uweabe92a52009-05-16 22:36:00 +0000441}
442
443void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
444 size_t len)
445{
446 return programmer_table[programmer].map_flash_region(descr,
447 phys_addr, len);
448}
449
450void programmer_unmap_flash_region(void *virt_addr, size_t len)
451{
452 programmer_table[programmer].unmap_flash_region(virt_addr, len);
453}
454
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700455void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000456{
David Hendricksac1d25c2016-08-09 17:00:58 -0700457 par_programmer->chip_writeb(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000458}
459
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700460void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000461{
David Hendricksac1d25c2016-08-09 17:00:58 -0700462 par_programmer->chip_writew(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000463}
464
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700465void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000466{
David Hendricksac1d25c2016-08-09 17:00:58 -0700467 par_programmer->chip_writel(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000468}
469
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700470void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000471{
David Hendricksac1d25c2016-08-09 17:00:58 -0700472 par_programmer->chip_writen(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000473}
474
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700475uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000476{
David Hendricksac1d25c2016-08-09 17:00:58 -0700477 return par_programmer->chip_readb(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000478}
479
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700480uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000481{
David Hendricksac1d25c2016-08-09 17:00:58 -0700482 return par_programmer->chip_readw(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000483}
484
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700485uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000486{
David Hendricksac1d25c2016-08-09 17:00:58 -0700487 return par_programmer->chip_readl(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000488}
489
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700490void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000491{
David Hendricksac1d25c2016-08-09 17:00:58 -0700492 par_programmer->chip_readn(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000493}
494
hailfingere5829f62009-06-05 17:48:08 +0000495void programmer_delay(int usecs)
496{
497 programmer_table[programmer].delay(usecs);
498}
499
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700500void map_flash_registers(struct flashctx *flash)
stepan15e64bc2007-05-24 08:48:10 +0000501{
stepan15e64bc2007-05-24 08:48:10 +0000502 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000503 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000504 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000505 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000506}
507
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700508int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000509{
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700510 chip_readn(flash, buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000511
hailfinger23060112009-05-08 12:49:03 +0000512 return 0;
513}
514
hailfinger7b414742009-06-13 12:04:03 +0000515int min(int a, int b)
516{
517 return (a < b) ? a : b;
518}
519
hailfinger7af83692009-06-15 17:23:36 +0000520int max(int a, int b)
521{
522 return (a > b) ? a : b;
523}
524
hailfingeraec9c962009-10-31 01:53:09 +0000525int bitcount(unsigned long a)
526{
527 int i = 0;
528 for (; a != 0; a >>= 1)
529 if (a & 1)
530 i++;
531 return i;
532}
533
hailfingerf76cc322010-11-09 22:00:31 +0000534void tolower_string(char *str)
535{
536 for (; *str != '\0'; str++)
537 *str = (char)tolower((unsigned char)*str);
538}
539
hailfingera916b422009-06-01 02:08:58 +0000540char *strcat_realloc(char *dest, const char *src)
541{
542 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000543 if (!dest) {
544 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000545 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000546 }
hailfingera916b422009-06-01 02:08:58 +0000547 strcat(dest, src);
548 return dest;
549}
550
hailfinger6e5a52a2009-11-24 18:27:10 +0000551/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000552 * It will look for needle with a subsequent '=' in haystack, return a copy of
553 * needle and remove everything from the first occurrence of needle to the next
554 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000555 */
stefanct52700282011-06-26 17:38:17 +0000556char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000557{
hailfinger1ef766d2010-07-06 09:55:48 +0000558 char *param_pos, *opt_pos, *rest;
559 char *opt = NULL;
560 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000561 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000562
hailfingerf4aaccc2010-04-28 15:22:14 +0000563 needlelen = strlen(needle);
564 if (!needlelen) {
565 msg_gerr("%s: empty needle! Please report a bug at "
566 "flashrom@flashrom.org\n", __func__);
567 return NULL;
568 }
569 /* No programmer parameters given. */
570 if (*haystack == NULL)
571 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000572 param_pos = strstr(*haystack, needle);
573 do {
574 if (!param_pos)
575 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000576 /* Needle followed by '='? */
577 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600578
hailfinger1ef766d2010-07-06 09:55:48 +0000579 /* Beginning of the string? */
580 if (param_pos == *haystack)
581 break;
582 /* After a delimiter? */
583 if (strchr(delim, *(param_pos - 1)))
584 break;
585 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000586 /* Continue searching. */
587 param_pos++;
588 param_pos = strstr(param_pos, needle);
589 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000590
hailfinger6e5a52a2009-11-24 18:27:10 +0000591 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000592 /* Get the string after needle and '='. */
593 opt_pos = param_pos + needlelen + 1;
594 optlen = strcspn(opt_pos, delim);
595 /* Return an empty string if the parameter was empty. */
596 opt = malloc(optlen + 1);
597 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000598 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000599 exit(1);
600 }
hailfinger1ef766d2010-07-06 09:55:48 +0000601 strncpy(opt, opt_pos, optlen);
602 opt[optlen] = '\0';
603 rest = opt_pos + optlen;
604 /* Skip all delimiters after the current parameter. */
605 rest += strspn(rest, delim);
606 memmove(param_pos, rest, strlen(rest) + 1);
607 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000608 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000609
hailfinger1ef766d2010-07-06 09:55:48 +0000610 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000611}
612
stefanct52700282011-06-26 17:38:17 +0000613char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000614{
615 return extract_param(&programmer_param, param_name, ",");
616}
617
stefancte1c5acf2011-07-04 07:27:17 +0000618/* Returns the number of well-defined erasers for a chip. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700619static unsigned int count_usable_erasers(const struct flashctx *flash)
stefanct569dbb62011-07-01 00:19:12 +0000620{
621 unsigned int usable_erasefunctions = 0;
622 int k;
623 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
624 if (!check_block_eraser(flash, k, 0))
625 usable_erasefunctions++;
626 }
627 return usable_erasefunctions;
628}
629
hailfinger7af83692009-06-15 17:23:36 +0000630/* start is an offset to the base address of the flash chip */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700631int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000632{
633 int ret;
634 uint8_t *cmpbuf = malloc(len);
635
636 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000637 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000638 exit(1);
639 }
Simon Glass4c214132013-07-16 10:09:28 -0600640 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000641 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
642 free(cmpbuf);
643 return ret;
644}
645
Simon Glass4e305f42015-01-08 06:29:04 -0700646static int compare_chunk(uint8_t *readbuf, uint8_t *cmpbuf, unsigned int start,
647 unsigned int len, const char *message)
648{
649 int failcount = 0, i;
650
651 for (i = 0; i < len; i++) {
652 if (cmpbuf[i] != readbuf[i]) {
653 if (!failcount) {
654 msg_cerr("%s FAILED at 0x%08x! "
655 "Expected=0x%02x, Read=0x%02x,",
656 message, start + i,
657 cmpbuf[i], readbuf[i]);
658 }
659 failcount++;
660 }
661 }
662
663 return failcount;
664}
665
uwee15beb92010-08-08 17:01:18 +0000666/*
hailfinger7af3d192009-11-25 17:05:52 +0000667 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000668 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000669 * @start offset to the base address of the flash chip
670 * @len length of the verified area
671 * @message string to print in the "FAILED" message
672 * @return 0 for success, -1 for failure
673 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700674int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000675 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000676{
hailfinger8cb6ece2010-11-16 17:21:58 +0000677 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000678 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000679
680 if (!len)
681 goto out_free;
682
hailfingerb0f4d122009-06-24 08:20:45 +0000683 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000684 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000685 return 1;
686 }
hailfinger7af83692009-06-15 17:23:36 +0000687 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000688 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000689 exit(1);
690 }
691
692 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000693 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000694 " total_size 0x%x\n", __func__, start, len,
695 flash->total_size * 1024);
696 ret = -1;
697 goto out_free;
698 }
699 if (!message)
700 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000701
Simon Glass4e305f42015-01-08 06:29:04 -0700702 if (programmer_table[programmer].paranoid) {
703 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800704
Simon Glass4e305f42015-01-08 06:29:04 -0700705 /* limit chunksize in order to catch errors early */
706 for (i = 0, chunksize = 0; i < len; i += chunksize) {
707 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800708
Simon Glass4e305f42015-01-08 06:29:04 -0700709 chunksize = min(flash->page_size, len - i);
710 tmp = flash->read(flash, readbuf + i, start + i, chunksize);
711 if (tmp) {
712 ret = tmp;
713 if (ignore_error(tmp))
714 continue;
715 else
716 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800717 }
Simon Glass4e305f42015-01-08 06:29:04 -0700718
719 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
720 chunksize, message);
721 if (failcount)
722 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800723 }
Simon Glass4e305f42015-01-08 06:29:04 -0700724 } else {
725 int tmp;
726
727 /* read as much as we can to reduce transaction overhead */
728 tmp = flash->read(flash, readbuf, start, len);
729 if (tmp && !ignore_error(tmp)) {
730 ret = tmp;
731 goto out_free;
732 }
733
734 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000735 }
736
hailfinger5be6c0f2009-07-23 01:42:56 +0000737 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000738 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000739 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000740 ret = -1;
741 }
hailfinger7af83692009-06-15 17:23:36 +0000742
743out_free:
744 free(readbuf);
745 return ret;
746}
747
uwee15beb92010-08-08 17:01:18 +0000748/*
hailfingerb247c7a2010-03-08 00:42:32 +0000749 * Check if the buffer @have can be programmed to the content of @want without
750 * erasing. This is only possible if all chunks of size @gran are either kept
751 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000752 *
hailfingerb247c7a2010-03-08 00:42:32 +0000753 * The following write granularities (enum @gran) are known:
754 * - 1 bit. Each bit can be cleared individually.
755 * - 1 byte. A byte can be written once. Further writes to an already written
756 * byte cause the contents to be either undefined or to stay unchanged.
757 * - 128 bytes. If less than 128 bytes are written, the rest will be
758 * erased. Each write to a 128-byte region will trigger an automatic erase
759 * before anything is written. Very uncommon behaviour and unsupported by
760 * this function.
761 * - 256 bytes. If less than 256 bytes are written, the contents of the
762 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000763 * Warning: This function assumes that @have and @want point to naturally
764 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000765 *
766 * @have buffer with current content
767 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000768 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000769 * @gran write granularity (enum, not count)
770 * @return 0 if no erase is needed, 1 otherwise
771 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700772static int need_erase(struct flashctx *flash, uint8_t *have, uint8_t *want,
Simon Glass4c214132013-07-16 10:09:28 -0600773 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000774{
hailfingerb91c08c2011-08-15 19:54:20 +0000775 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000776 unsigned int i, j, limit;
Simon Glass4c214132013-07-16 10:09:28 -0600777 int erase_value = flash_erase_value(flash);
hailfingerb247c7a2010-03-08 00:42:32 +0000778
779 switch (gran) {
780 case write_gran_1bit:
781 for (i = 0; i < len; i++)
782 if ((have[i] & want[i]) != want[i]) {
783 result = 1;
784 break;
785 }
786 break;
787 case write_gran_1byte:
788 for (i = 0; i < len; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600789 if ((have[i] != want[i]) && (have[i] != erase_value)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000790 result = 1;
791 break;
792 }
793 break;
794 case write_gran_256bytes:
795 for (j = 0; j < len / 256; j++) {
796 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000797 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000798 if (!memcmp(have + j * 256, want + j * 256, limit))
799 continue;
800 /* have needs to be in erased state. */
801 for (i = 0; i < limit; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600802 if (have[j * 256 + i] != erase_value) {
hailfingerb247c7a2010-03-08 00:42:32 +0000803 result = 1;
804 break;
805 }
806 if (result)
807 break;
808 }
809 break;
hailfingerb437e282010-11-04 01:04:27 +0000810 default:
811 msg_cerr("%s: Unsupported granularity! Please report a bug at "
812 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000813 }
814 return result;
815}
816
hailfingerb437e282010-11-04 01:04:27 +0000817/**
818 * Check if the buffer @have needs to be programmed to get the content of @want.
819 * If yes, return 1 and fill in first_start with the start address of the
820 * write operation and first_len with the length of the first to-be-written
821 * chunk. If not, return 0 and leave first_start and first_len undefined.
822 *
823 * Warning: This function assumes that @have and @want point to naturally
824 * aligned regions.
825 *
826 * @have buffer with current content
827 * @want buffer with desired content
828 * @len length of the checked area
829 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000830 * @first_start offset of the first byte which needs to be written (passed in
831 * value is increased by the offset of the first needed write
832 * relative to have/want or unchanged if no write is needed)
833 * @return length of the first contiguous area which needs to be written
834 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000835 *
836 * FIXME: This function needs a parameter which tells it about coalescing
837 * in relation to the max write length of the programmer and the max write
838 * length of the chip.
839 */
stefanctc5eb8a92011-11-23 09:13:48 +0000840static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
841 unsigned int *first_start,
842 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000843{
stefanctc5eb8a92011-11-23 09:13:48 +0000844 int need_write = 0;
845 unsigned int rel_start = 0, first_len = 0;
846 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000847
hailfingerb437e282010-11-04 01:04:27 +0000848 switch (gran) {
849 case write_gran_1bit:
850 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000851 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000852 break;
853 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000854 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000855 break;
856 default:
857 msg_cerr("%s: Unsupported granularity! Please report a bug at "
858 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000859 /* Claim that no write was needed. A write with unknown
860 * granularity is too dangerous to try.
861 */
862 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000863 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000864 for (i = 0; i < len / stride; i++) {
865 limit = min(stride, len - i * stride);
866 /* Are 'have' and 'want' identical? */
867 if (memcmp(have + i * stride, want + i * stride, limit)) {
868 if (!need_write) {
869 /* First location where have and want differ. */
870 need_write = 1;
871 rel_start = i * stride;
872 }
873 } else {
874 if (need_write) {
875 /* First location where have and want
876 * do not differ anymore.
877 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000878 break;
879 }
880 }
881 }
hailfingerffb7f382010-12-06 13:05:44 +0000882 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000883 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000884 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000885 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000886}
887
hailfinger0c515352009-11-23 12:55:31 +0000888/* This function generates various test patterns useful for testing controller
889 * and chip communication as well as chip behaviour.
890 *
891 * If a byte can be written multiple times, each time keeping 0-bits at 0
892 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
893 * is essentially an AND operation. That's also the reason why this function
894 * provides the result of AND between various patterns.
895 *
896 * Below is a list of patterns (and their block length).
897 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
898 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
899 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
900 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
901 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
902 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
903 * Pattern 6 is 00 (1 Byte)
904 * Pattern 7 is ff (1 Byte)
905 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
906 * byte block.
907 *
908 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
909 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
910 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
911 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
912 * Pattern 12 is 00 (1 Byte)
913 * Pattern 13 is ff (1 Byte)
914 * Patterns 8-13 have no block number.
915 *
916 * Patterns 0-3 are created to detect and efficiently diagnose communication
917 * slips like missed bits or bytes and their repetitive nature gives good visual
918 * cues to the person inspecting the results. In addition, the following holds:
919 * AND Pattern 0/1 == Pattern 4
920 * AND Pattern 2/3 == Pattern 5
921 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
922 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
923 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
924 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
925 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
926 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
927 * Besides that, they provide for bit testing of the last two bytes of every
928 * 256 byte block which contains the block number for patterns 0-6.
929 * Patterns 10-11 are special purpose for detecting subblock aliasing with
930 * block sizes >256 bytes (some Dataflash chips etc.)
931 * AND Pattern 8/9 == Pattern 12
932 * AND Pattern 10/11 == Pattern 12
933 * Pattern 13 is the completely erased state.
934 * None of the patterns can detect aliasing at boundaries which are a multiple
935 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
936 */
937int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
938{
939 int i;
940
941 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000942 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000943 return 1;
944 }
945
946 switch (variant) {
947 case 0:
948 for (i = 0; i < size; i++)
949 buf[i] = (i & 0xf) << 4 | 0x5;
950 break;
951 case 1:
952 for (i = 0; i < size; i++)
953 buf[i] = (i & 0xf) << 4 | 0xa;
954 break;
955 case 2:
956 for (i = 0; i < size; i++)
957 buf[i] = 0x50 | (i & 0xf);
958 break;
959 case 3:
960 for (i = 0; i < size; i++)
961 buf[i] = 0xa0 | (i & 0xf);
962 break;
963 case 4:
964 for (i = 0; i < size; i++)
965 buf[i] = (i & 0xf) << 4;
966 break;
967 case 5:
968 for (i = 0; i < size; i++)
969 buf[i] = i & 0xf;
970 break;
971 case 6:
972 memset(buf, 0x00, size);
973 break;
974 case 7:
975 memset(buf, 0xff, size);
976 break;
977 case 8:
978 for (i = 0; i < size; i++)
979 buf[i] = i & 0xff;
980 break;
981 case 9:
982 for (i = 0; i < size; i++)
983 buf[i] = ~(i & 0xff);
984 break;
985 case 10:
986 for (i = 0; i < size % 2; i++) {
987 buf[i * 2] = (i >> 8) & 0xff;
988 buf[i * 2 + 1] = i & 0xff;
989 }
990 if (size & 0x1)
991 buf[i * 2] = (i >> 8) & 0xff;
992 break;
993 case 11:
994 for (i = 0; i < size % 2; i++) {
995 buf[i * 2] = ~((i >> 8) & 0xff);
996 buf[i * 2 + 1] = ~(i & 0xff);
997 }
998 if (size & 0x1)
999 buf[i * 2] = ~((i >> 8) & 0xff);
1000 break;
1001 case 12:
1002 memset(buf, 0x00, size);
1003 break;
1004 case 13:
1005 memset(buf, 0xff, size);
1006 break;
1007 }
1008
1009 if ((variant >= 0) && (variant <= 7)) {
1010 /* Write block number in the last two bytes of each 256-byte
1011 * block, big endian for easier reading of the hexdump.
1012 * Note that this wraps around for chips larger than 2^24 bytes
1013 * (16 MB).
1014 */
1015 for (i = 0; i < size / 256; i++) {
1016 buf[i * 256 + 254] = (i >> 8) & 0xff;
1017 buf[i * 256 + 255] = i & 0xff;
1018 }
1019 }
1020
1021 return 0;
1022}
1023
hailfingeraec9c962009-10-31 01:53:09 +00001024int check_max_decode(enum chipbustype buses, uint32_t size)
1025{
1026 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001027
1028 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001029 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001030 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001031 "size %u kB of chipset/board/programmer "
1032 "for %s interface, "
1033 "probe/read/erase/write may fail. ", size / 1024,
1034 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001035 }
hailfingere1e41ea2011-07-27 07:13:06 +00001036 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001037 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001038 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001039 "size %u kB of chipset/board/programmer "
1040 "for %s interface, "
1041 "probe/read/erase/write may fail. ", size / 1024,
1042 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001043 }
hailfingere1e41ea2011-07-27 07:13:06 +00001044 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001045 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001046 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001047 "size %u kB of chipset/board/programmer "
1048 "for %s interface, "
1049 "probe/read/erase/write may fail. ", size / 1024,
1050 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001051 }
hailfingere1e41ea2011-07-27 07:13:06 +00001052 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001053 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001054 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001055 "size %u kB of chipset/board/programmer "
1056 "for %s interface, "
1057 "probe/read/erase/write may fail. ", size / 1024,
1058 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001059 }
1060 if (!limitexceeded)
1061 return 0;
1062 /* Sometimes chip and programmer have more than one bus in common,
1063 * and the limit is not exceeded on all buses. Tell the user.
1064 */
1065 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001066 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001067 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001068 "interface which can support a chip of this size. "
1069 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001070 return 1;
1071}
1072
David Hendricksac1d25c2016-08-09 17:00:58 -07001073int probe_flash(int startchip, struct flashctx *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001074{
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301075 const struct flashchip *flash, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001076 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001077 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001078 uint32_t size;
1079 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001080 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001081
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301082 /* Based on the host controller interface that a platform
1083 * needs to use (hwseq or swseq),
1084 * set the flashchips list here.
1085 */
1086 switch (ich_generation) {
1087 case CHIPSET_100_SERIES_SUNRISE_POINT:
Furquan Shaikh44088752016-07-11 22:48:08 -07001088 case CHIPSET_APL:
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301089 flash_list = flashchips_hwseq;
1090 break;
1091 default:
1092 flash_list = flashchips;
1093 break;
1094 }
1095
1096 for (flash = flash_list + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001097 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001098 continue;
David Hendricksac1d25c2016-08-09 17:00:58 -07001099 buses_common = buses_supported & flash->bustype;
1100 if (!buses_common) {
1101 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1102 flash->vendor, flash->name, flash->total_size);
1103 tmp = flashbuses_to_text(buses_supported);
1104 msg_gspew("Host bus type %s ", tmp);
1105 free(tmp);
1106 tmp = flashbuses_to_text(flash->bustype);
1107 msg_gspew("and chip bus type %s are incompatible.",
1108 tmp);
1109 free(tmp);
1110 msg_gspew("\n");
hailfinger18bd4cc2011-06-17 22:38:53 +00001111 continue;
David Hendricksac1d25c2016-08-09 17:00:58 -07001112 }
hailfinger18bd4cc2011-06-17 22:38:53 +00001113 msg_gdbg("Probing for %s %s, %d kB: ",
1114 flash->vendor, flash->name, flash->total_size);
1115 if (!flash->probe && !force) {
1116 msg_gdbg("failed! flashrom has no probe function for "
1117 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001118 continue;
1119 }
stepan782fb172007-04-06 11:58:03 +00001120
ollie5672ac62004-03-17 22:22:08 +00001121 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001122 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001123
hailfinger48ed3e22011-05-04 00:39:50 +00001124 /* Start filling in the dynamic data. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001125 memcpy(fill_flash, flash, sizeof(struct flashchip));
hailfinger48ed3e22011-05-04 00:39:50 +00001126
hailfinger72d3b982009-05-09 07:27:23 +00001127 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001128 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001129
stugec1e55fe2008-07-02 17:15:47 +00001130 if (force)
1131 break;
stepanc98b80b2006-03-16 16:57:41 +00001132
hailfinger48ed3e22011-05-04 00:39:50 +00001133 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001134 goto notfound;
1135
hailfinger48ed3e22011-05-04 00:39:50 +00001136 /* If this is the first chip found, accept it.
1137 * If this is not the first chip found, accept it only if it is
1138 * a non-generic match.
1139 * We could either make chipcount global or provide it as
1140 * parameter, or we assume that startchip==0 means this call to
1141 * probe_flash() is the first one and thus no chip has been
1142 * found before.
1143 */
1144 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001145 break;
1146
stuge56300c32008-09-03 23:10:05 +00001147notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001148 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001149 }
uwebe4477b2007-08-23 16:08:21 +00001150
stugec1e55fe2008-07-02 17:15:47 +00001151 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001152 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001153
hailfingere11396b2011-03-08 00:09:11 +00001154#if CONFIG_INTERNAL == 1
1155 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001156 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001157 else
1158#endif
stepan3e7aeae2011-01-19 06:21:54 +00001159 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001160
stefanct588b6d22011-06-26 20:45:35 +00001161 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001162 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1163 force ? "Assuming" : "Found", fill_flash->vendor,
1164 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001165 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001166
hailfinger0f4c3952010-12-02 21:59:42 +00001167 /* Flash registers will not be mapped if the chip was forced. Lock info
1168 * may be stored in registers, so avoid lock info printing.
1169 */
1170 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001171 if (fill_flash->printlock)
1172 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001173
hailfinger48ed3e22011-05-04 00:39:50 +00001174 /* Return position of matching chip. */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301175 return flash - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001176}
1177
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001178int verify_flash(struct flashctx *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001179{
hailfingerb0f4d122009-06-24 08:20:45 +00001180 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001181 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001182
snelsone42c3802010-05-07 20:09:04 +00001183 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001184
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001185 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1186 ret = handle_partial_verify(flash, buf, verify_range);
1187 } else {
1188 ret = verify_range(flash, buf, 0, total_size, NULL);
1189 }
uwef6641642007-05-09 10:17:44 +00001190
David Hendricks1ed1d352011-11-23 17:54:37 -08001191 if (ret == ACCESS_DENIED) {
1192 msg_gdbg("Could not fully verify due to access error, ");
1193 if (access_denied_action == error_ignore) {
1194 msg_gdbg("ignoring\n");
1195 ret = 0;
1196 } else {
1197 msg_gdbg("aborting\n");
1198 }
1199 }
1200
hailfingerb0f4d122009-06-24 08:20:45 +00001201 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001202 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001203
hailfingerb0f4d122009-06-24 08:20:45 +00001204 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001205}
1206
uwe8d342eb2011-07-28 08:13:25 +00001207int read_buf_from_file(unsigned char *buf, unsigned long size,
1208 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001209{
1210 unsigned long numbytes;
1211 FILE *image;
1212 struct stat image_stat;
1213
Vincent Palatin7ab23932014-10-01 12:09:16 -07001214 if (!strncmp(filename, "-", sizeof("-")))
1215 image = fdopen(STDIN_FILENO, "rb");
1216 else
1217 image = fopen(filename, "rb");
1218 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001219 perror(filename);
1220 return 1;
1221 }
1222 if (fstat(fileno(image), &image_stat) != 0) {
1223 perror(filename);
1224 fclose(image);
1225 return 1;
1226 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001227 if ((image_stat.st_size != size) &&
1228 (strncmp(filename, "-", sizeof("-")))) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001229 msg_gerr("Error: Image size doesn't match: stat %ld bytes, "
1230 "wanted %ld!\n", image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001231 fclose(image);
1232 return 1;
1233 }
1234 numbytes = fread(buf, 1, size, image);
1235 if (fclose(image)) {
1236 perror(filename);
1237 return 1;
1238 }
1239 if (numbytes != size) {
1240 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1241 "wanted %ld!\n", numbytes, size);
1242 return 1;
1243 }
1244 return 0;
1245}
1246
uwe8d342eb2011-07-28 08:13:25 +00001247int write_buf_to_file(unsigned char *buf, unsigned long size,
1248 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001249{
1250 unsigned long numbytes;
1251 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001252
1253 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001254 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001255 return 1;
1256 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001257 if (!strncmp(filename, "-", sizeof("-")))
1258 image = fdopen(STDOUT_FILENO, "wb");
1259 else
1260 image = fopen(filename, "wb");
1261 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001262 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001263 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001264 }
hailfingerd219a232009-01-28 00:27:54 +00001265
hailfingerd219a232009-01-28 00:27:54 +00001266 numbytes = fwrite(buf, 1, size, image);
1267 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001268 if (numbytes != size) {
1269 msg_gerr("File %s could not be written completely.\n",
1270 filename);
hailfingerd219a232009-01-28 00:27:54 +00001271 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001272 }
hailfingerd219a232009-01-28 00:27:54 +00001273 return 0;
1274}
1275
David Hendrickse3451942013-03-21 17:23:29 -07001276/*
1277 * read_flash - wrapper for flash->read() with additional high-level policy
1278 *
1279 * @flash flash chip
1280 * @buf buffer to store data in
1281 * @start start address
1282 * @len number of bytes to read
1283 *
1284 * This wrapper simplifies most cases when the flash chip needs to be read
1285 * since policy decisions such as non-fatal error handling is centralized.
1286 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001287int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001288 unsigned int start, unsigned int len)
1289{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001290 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001291
1292 if (!flash || !flash->read)
1293 return -1;
1294
David Hendricks4e76fdc2013-05-13 16:05:36 -07001295 ret = flash->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001296 if (ret) {
1297 if (ignore_error(ret)) {
1298 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1299 start, start + len - 1);
1300 ret = 0;
1301 } else {
1302 msg_gdbg("failed to read 0x%x-0x%x\n",
1303 start, start + len - 1);
1304 }
1305 }
1306
1307 return ret;
1308}
1309
David Hendricks7c8a1612013-04-26 19:14:44 -07001310/*
1311 * write_flash - wrapper for flash->write() with additional high-level policy
1312 *
1313 * @flash flash chip
1314 * @buf buffer to write to flash
1315 * @start start address in flash
1316 * @len number of bytes to write
1317 *
1318 * TODO: Look up regions that are write-protected and avoid attempt to write
1319 * to them at all.
1320 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001321int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001322 unsigned int start, unsigned int len)
1323{
1324 if (!flash || !flash->write)
1325 return -1;
1326
1327 return flash->write(flash, buf, start, len);
1328}
1329
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001330int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001331{
1332 unsigned long size = flash->total_size * 1024;
1333 unsigned char *buf = calloc(size, sizeof(char));
1334 int ret = 0;
1335
1336 msg_cinfo("Reading flash... ");
1337 if (!buf) {
1338 msg_gerr("Memory allocation failed!\n");
1339 msg_cinfo("FAILED.\n");
1340 return 1;
1341 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001342
1343 /* To support partial read, fill buffer to all 0xFF at beginning to make
1344 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001345 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001346
hailfinger42a850a2010-07-13 23:56:13 +00001347 if (!flash->read) {
1348 msg_cerr("No read function available for this flash chip.\n");
1349 ret = 1;
1350 goto out_free;
1351 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001352
1353 /* First try to handle partial read case, rather than read the whole
1354 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001355 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001356 if (ret < 0) {
1357 msg_cerr("Partial read operation failed!\n");
1358 ret = 1;
1359 goto out_free;
1360 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001361 int num_regions = get_num_include_args();
1362
1363 if (ret != num_regions) {
1364 msg_cerr("Requested %d regions, but only read %d\n",
1365 num_regions, ret);
1366 ret = 1;
1367 goto out_free;
1368 }
1369
1370 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001371 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001372 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001373 msg_cerr("Read operation failed!\n");
1374 ret = 1;
1375 goto out_free;
1376 }
hailfinger42a850a2010-07-13 23:56:13 +00001377 }
1378
David Hendricksdf29a832013-06-28 14:33:51 -07001379 if (filename)
1380 ret = write_buf_to_file(buf, size, filename);
1381
hailfinger42a850a2010-07-13 23:56:13 +00001382out_free:
1383 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001384 if (ret)
1385 msg_cerr("FAILED.");
1386 else
1387 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001388 return ret;
1389}
1390
hailfingerb437e282010-11-04 01:04:27 +00001391/* This function shares a lot of its structure with erase_and_write_flash() and
1392 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001393 * Even if an error is found, the function will keep going and check the rest.
1394 */
hailfinger48ed3e22011-05-04 00:39:50 +00001395static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001396{
hailfingerb91c08c2011-08-15 19:54:20 +00001397 int i, j, k;
1398 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001399
1400 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1401 unsigned int done = 0;
1402 struct block_eraser eraser = flash->block_erasers[k];
1403
1404 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1405 /* Blocks with zero size are bugs in flashchips.c. */
1406 if (eraser.eraseblocks[i].count &&
1407 !eraser.eraseblocks[i].size) {
1408 msg_gerr("ERROR: Flash chip %s erase function "
1409 "%i region %i has size 0. Please report"
1410 " a bug at flashrom@flashrom.org\n",
1411 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001412 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001413 }
1414 /* Blocks with zero count are bugs in flashchips.c. */
1415 if (!eraser.eraseblocks[i].count &&
1416 eraser.eraseblocks[i].size) {
1417 msg_gerr("ERROR: Flash chip %s erase function "
1418 "%i region %i has count 0. Please report"
1419 " a bug at flashrom@flashrom.org\n",
1420 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001421 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001422 }
1423 done += eraser.eraseblocks[i].count *
1424 eraser.eraseblocks[i].size;
1425 }
hailfinger9fed35d2010-01-19 06:42:46 +00001426 /* Empty eraseblock definition with erase function. */
1427 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001428 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001429 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001430 if (!done)
1431 continue;
1432 if (done != flash->total_size * 1024) {
1433 msg_gerr("ERROR: Flash chip %s erase function %i "
1434 "region walking resulted in 0x%06x bytes total,"
1435 " expected 0x%06x bytes. Please report a bug at"
1436 " flashrom@flashrom.org\n", flash->name, k,
1437 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001438 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001439 }
hailfinger9fed35d2010-01-19 06:42:46 +00001440 if (!eraser.block_erase)
1441 continue;
1442 /* Check if there are identical erase functions for different
1443 * layouts. That would imply "magic" erase functions. The
1444 * easiest way to check this is with function pointers.
1445 */
uwef6f94d42010-03-13 17:28:29 +00001446 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001447 if (eraser.block_erase ==
1448 flash->block_erasers[j].block_erase) {
1449 msg_gerr("ERROR: Flash chip %s erase function "
1450 "%i and %i are identical. Please report"
1451 " a bug at flashrom@flashrom.org\n",
1452 flash->name, k, j);
1453 ret = 1;
1454 }
uwef6f94d42010-03-13 17:28:29 +00001455 }
hailfinger45177872010-01-18 08:14:43 +00001456 }
hailfinger9fed35d2010-01-19 06:42:46 +00001457 return ret;
hailfinger45177872010-01-18 08:14:43 +00001458}
1459
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001460static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001461 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001462 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001463 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001464 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001465 unsigned int addr,
1466 unsigned int len))
1467{
stefanctc5eb8a92011-11-23 09:13:48 +00001468 unsigned int starthere = 0, lenhere = 0;
1469 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001470 int block_was_erased = 0;
hailfingerb437e282010-11-04 01:04:27 +00001471 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1472
hailfinger90fcf9b2010-11-05 14:51:59 +00001473 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001474 * need to be adjusted here to keep the impression of proper abstraction
1475 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001476 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001477 newcontents += start;
1478 msg_cdbg(":");
1479 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001480 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001481 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001482 msg_cdbg("E");
1483 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001484 if (ret) {
1485 if (ret == ACCESS_DENIED)
1486 msg_cdbg("D");
1487 else
1488 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001489 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001490 }
1491
David Hendricks0954ffc2015-11-13 15:15:44 -08001492 if (programmer_table[programmer].paranoid) {
1493 if (check_erased_range(flash, start, len)) {
1494 msg_cerr("ERASE FAILED!\n");
1495 return -1;
1496 }
hailfingerac8e3182011-06-26 17:04:16 +00001497 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001498
hailfinger90fcf9b2010-11-05 14:51:59 +00001499 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001500 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001501 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001502 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001503 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001504 /* get_next_write() sets starthere to a new value after the call. */
1505 while ((lenhere = get_next_write(curcontents + starthere,
1506 newcontents + starthere,
1507 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001508 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001509 if (!writecount++)
1510 msg_cdbg("W");
1511 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001512 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001513 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001514 if (ret) {
1515 if (ret == ACCESS_DENIED)
1516 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001517 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001518 }
David Hendricks048b38c2016-03-28 18:47:06 -07001519
1520 /*
1521 * If the block needed to be erased and was erased successfully
1522 * then we can assume that we didn't run into any write-
1523 * protected areas. Otherwise, we need to verify each page to
1524 * ensure it was successfully written and abort if we encounter
1525 * any errors.
1526 */
1527 if (programmer_table[programmer].paranoid && !block_was_erased) {
1528 if (verify_range(flash, newcontents + starthere,
1529 start + starthere, lenhere, "WRITE"))
1530 return -1;
1531 }
1532
hailfingerb437e282010-11-04 01:04:27 +00001533 starthere += lenhere;
1534 skip = 0;
1535 }
1536 if (skip)
1537 msg_cdbg("S");
1538 return ret;
1539}
1540
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001541static int walk_eraseregions(struct flashctx *flash, int erasefunction,
1542 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001543 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001544 unsigned int len,
1545 uint8_t *param1,
1546 uint8_t *param2,
1547 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001548 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001549 unsigned int addr,
1550 unsigned int len)),
1551 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001552{
David Hendricks605544b2015-08-15 16:32:58 -07001553 int i, j, rc = -1;
hailfingerb91c08c2011-08-15 19:54:20 +00001554 unsigned int start = 0;
1555 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001556 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001557
hailfinger2b8c9382010-07-13 00:37:19 +00001558 for (i = 0; i < NUM_ERASEREGIONS; i++) {
David Hendricks605544b2015-08-15 16:32:58 -07001559 /* count==0 and size==0 for all automatically initialized array
hailfinger2b8c9382010-07-13 00:37:19 +00001560 * members so the loop below won't be executed for them.
1561 */
1562 len = eraser.eraseblocks[i].size;
David Hendricks605544b2015-08-15 16:32:58 -07001563 if (!len)
1564 continue;
1565
1566 if (required_erase_size && (len != required_erase_size)) {
1567 msg_cdbg("%u does not meet erase alignment", len);
1568 rc = -1;
1569 break;
1570 }
1571
hailfinger2b8c9382010-07-13 00:37:19 +00001572 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001573 /* Print this for every block except the first one. */
1574 if (i || j)
1575 msg_cdbg(", ");
1576 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001577 start + len - 1);
David Hendricks1ed1d352011-11-23 17:54:37 -08001578 rc = do_something(flash, start, len, param1, param2,
1579 eraser.block_erase);
1580 if (rc) {
1581 if (ignore_error(rc))
1582 rc = 0;
1583 else
1584 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001585 }
hailfinger2b8c9382010-07-13 00:37:19 +00001586 start += len;
1587 }
1588 }
hailfingerb437e282010-11-04 01:04:27 +00001589 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001590 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001591}
1592
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001593static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001594{
1595 struct block_eraser eraser = flash->block_erasers[k];
1596
1597 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1598 if (log)
1599 msg_cdbg("not defined. ");
1600 return 1;
1601 }
1602 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1603 if (log)
1604 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001605 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001606 return 1;
1607 }
1608 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1609 if (log)
1610 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001611 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001612 return 1;
1613 }
1614 return 0;
1615}
1616
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001617int erase_and_write_flash(struct flashctx *flash, uint8_t *oldcontents,
uwe8d342eb2011-07-28 08:13:25 +00001618 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001619{
hailfinger8f524a82011-07-21 21:21:04 +00001620 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001621 uint8_t *curcontents;
1622 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001623 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001624
hailfingercf848f12010-12-05 15:14:44 +00001625 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001626 curcontents = malloc(size);
1627 if (!curcontents) {
1628 msg_gerr("Out of memory!\n");
1629 exit(1);
1630 }
hailfingerb437e282010-11-04 01:04:27 +00001631 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1632 memcpy(curcontents, oldcontents, size);
1633
hailfinger7df21362009-09-05 02:30:58 +00001634 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001635 if (k != 0)
1636 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001637 if (!usable_erasefunctions) {
1638 msg_cdbg("No usable erase functions left.\n");
1639 break;
1640 }
stefanctf0bc4712011-07-26 14:28:35 +00001641 msg_cdbg("Trying erase function %i... ", k);
1642 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001643 continue;
hailfingercf848f12010-12-05 15:14:44 +00001644 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001645 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1646 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001647 /* If everything is OK, don't try another erase function. */
1648 if (!ret)
1649 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001650 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001651 * contents are. If no usable erase functions remain, we can
1652 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001653 */
hailfingercf848f12010-12-05 15:14:44 +00001654 if (!usable_erasefunctions)
1655 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001656 /* Reading the whole chip may take a while, inform the user even
1657 * in non-verbose mode.
1658 */
1659 msg_cinfo("Reading current flash chip contents... ");
David Hendrickse3451942013-03-21 17:23:29 -07001660 if (read_flash(flash, curcontents, 0, size)) {
hailfinger6237f5e2010-12-02 02:41:55 +00001661 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001662 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001663 /* We have no idea about the flash chip contents, so
1664 * retrying with another erase function is pointless.
1665 */
1666 break;
1667 }
David Hendricks89a45e52011-11-22 16:56:22 -08001668 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001669 }
hailfingerb437e282010-11-04 01:04:27 +00001670 /* Free the scratchpad. */
1671 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001672
hailfinger7df21362009-09-05 02:30:58 +00001673 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001674 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001675 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001676 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001677 }
1678 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001679}
1680
hailfinger4c47e9d2010-10-19 22:06:20 +00001681void nonfatal_help_message(void)
1682{
1683 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1684 "This means we have to add special support for your board, "
1685 "programmer or flash chip.\n"
1686 "Please report this on IRC at irc.freenode.net (channel "
1687 "#flashrom) or\n"
1688 "mail flashrom@flashrom.org!\n"
1689 "-------------------------------------------------------------"
1690 "------------------\n"
1691 "You may now reboot or simply leave the machine running.\n");
1692}
1693
uweb34ec9f2009-10-01 18:40:02 +00001694void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001695{
snelsone42c3802010-05-07 20:09:04 +00001696 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001697 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001698 "mail flashrom@flashrom.org with FAILED: your board name in "
1699 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001700 "-------------------------------------------------------------"
1701 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001702 "DO NOT REBOOT OR POWEROFF!\n");
1703}
1704
uwe8d342eb2011-07-28 08:13:25 +00001705/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001706void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001707{
1708 enum programmer p;
1709 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001710 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001711 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001712 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001713 }
Simon Glass8dc82732013-07-16 10:13:51 -06001714 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001715}
1716
hailfingerf79d1712010-10-06 23:48:34 +00001717void list_programmers_linebreak(int startcol, int cols, int paren)
1718{
1719 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001720 int pnamelen;
1721 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001722 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001723 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001724
1725 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1726 pname = programmer_table[p].name;
1727 pnamelen = strlen(pname);
1728 if (remaining - pnamelen - 2 < 0) {
1729 if (firstline)
1730 firstline = 0;
1731 else
1732 printf("\n");
1733 for (i = 0; i < startcol; i++)
1734 printf(" ");
1735 remaining = cols - startcol;
1736 } else {
1737 printf(" ");
1738 remaining--;
1739 }
1740 if (paren && (p == 0)) {
1741 printf("(");
1742 remaining--;
1743 }
1744 printf("%s", pname);
1745 remaining -= pnamelen;
1746 if (p < PROGRAMMER_INVALID - 1) {
1747 printf(",");
1748 remaining--;
1749 } else {
1750 if (paren)
1751 printf(")");
1752 printf("\n");
1753 }
1754 }
1755}
1756
hailfinger3b471632010-03-27 16:36:40 +00001757void print_sysinfo(void)
1758{
David Hendricksc6c9f822010-11-03 15:07:01 -07001759 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001760#if HAVE_UTSNAME == 1
1761 struct utsname osinfo;
1762 uname(&osinfo);
1763
David Hendricksc6c9f822010-11-03 15:07:01 -07001764 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001765 osinfo.machine);
1766#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001767 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001768#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001769}
1770
1771void print_buildinfo(void)
1772{
1773 msg_gdbg("flashrom was built with");
hailfinger3b471632010-03-27 16:36:40 +00001774#if NEED_PCI == 1
1775#ifdef PCILIB_VERSION
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001776 msg_gdbg(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001777#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001778 msg_gdbg(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001779#endif
1780#endif
1781#ifdef __clang__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001782 msg_gdbg(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001783#ifdef __clang_version__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001784 msg_gdbg(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001785#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001786 msg_gdbg(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001787#endif
hailfinger3b471632010-03-27 16:36:40 +00001788#elif defined(__GNUC__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001789 msg_gdbg(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001790#ifdef __VERSION__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001791 msg_gdbg(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001792#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001793 msg_gdbg(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001794#endif
1795#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001796 msg_gdbg(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001797#endif
1798#if defined (__FLASHROM_LITTLE_ENDIAN__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001799 msg_gdbg(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001800#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001801 msg_gdbg(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001802#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001803 msg_gdbg("\n");
hailfinger3b471632010-03-27 16:36:40 +00001804}
1805
uwefdeca092008-01-21 15:24:22 +00001806void print_version(void)
1807{
David Hendricksc6c9f822010-11-03 15:07:01 -07001808 /* send to stderr for chromium os */
1809 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001810 print_sysinfo();
David Hendricks07268292016-09-14 16:05:58 -07001811 msg_gerr("\n");
uwefdeca092008-01-21 15:24:22 +00001812}
1813
hailfinger74819ad2010-05-15 15:04:37 +00001814void print_banner(void)
1815{
1816 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001817 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001818 msg_ginfo("\n");
1819}
1820
hailfingerc77acb52009-12-24 02:15:55 +00001821int selfcheck(void)
1822{
hailfinger45177872010-01-18 08:14:43 +00001823 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001824 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001825
1826 /* Safety check. Instead of aborting after the first error, check
1827 * if more errors exist.
1828 */
hailfingerc77acb52009-12-24 02:15:55 +00001829 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001830 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001831 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001832 }
stefanct8632c922011-07-26 14:33:46 +00001833 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001834 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001835 * For 'flashchips' we check the first element to be non-null. In the
1836 * other cases there exist use cases where the first element can be
1837 * null. */
Yunlian Jiang945c3392015-01-20 15:56:44 -08001838 if (flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001839 msg_gerr("Flashchips table miscompilation!\n");
1840 ret = 1;
1841 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001842 /* Check that virtual_memory in struct flashctx is placed directly
1843 * after the members copied from struct flashchip.
1844 */
1845 if (sizeof(struct flashchip) != offsetof(struct flashctx, virtual_memory)) {
1846 msg_gerr("struct flashctx broken!\n");
1847 ret = 1;
1848 }
hailfinger45177872010-01-18 08:14:43 +00001849 for (flash = flashchips; flash && flash->name; flash++)
1850 if (selfcheck_eraseblocks(flash))
1851 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001852
hailfinger45177872010-01-18 08:14:43 +00001853 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001854}
1855
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001856void check_chip_supported(const struct flashctx *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001857{
Stefan Tauner9816fc82016-08-12 15:47:49 -07001858 if (flash->feature_bits & FEATURE_OTP) {
1859 msg_cdbg("This chip may contain one-time programmable memory. "
1860 "flashrom cannot read\nand may never be able to write "
1861 "it, hence it may not be able to completely\n"
1862 "clone the contents of this chip (see man page for "
1863 "details).\n");
1864 }
hailfingerc77acb52009-12-24 02:15:55 +00001865 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001866 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001867 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001868 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001869 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001870 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001871 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001872 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001873 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001874 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001875 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001876 msg_cdbg(" WRITE");
Duncan Laurie06ffd522015-10-26 12:56:08 -07001877 if (flash->tested & TEST_BAD_UREAD)
1878 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001879 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001880 }
1881 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1882 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1883 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1884 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001885 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001886 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001887 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001888 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001889 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001890 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001891 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001892 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001893 msg_cdbg(" WRITE");
Duncan Laurie06ffd522015-10-26 12:56:08 -07001894 if (!(flash->tested & TEST_BAD_UREAD) && !(flash->tested & TEST_OK_UREAD))
1895 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001896 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001897 }
hailfinger92cd8e32010-01-07 03:24:05 +00001898 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001899 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001900 "in the latest development\n"
1901 "version of flashrom. If you are running the latest "
1902 "development version,\n"
1903 "please email a report to flashrom@flashrom.org if "
1904 "any of the above operations\n"
1905 "work correctly for you with this flash part. Please "
1906 "include the flashrom\n"
1907 "output with the additional -V option for all "
1908 "operations you tested (-V, -Vr,\n"
Paul Menzel425dded2016-08-08 16:42:48 -07001909 "-VE, -Vw), and mention which mainboard or "
hailfinger74819ad2010-05-15 15:04:37 +00001910 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001911 "Please mention your board in the subject line. "
1912 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001913 }
1914}
1915
hailfinger771fc182010-10-15 00:01:14 +00001916/* FIXME: This function signature needs to be improved once doit() has a better
1917 * function signature.
1918 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001919int chip_safety_check(struct flashctx *flash, int force, int read_it, int write_it, int erase_it, int verify_it)
hailfinger771fc182010-10-15 00:01:14 +00001920{
1921 if (!programmer_may_write && (write_it || erase_it)) {
1922 msg_perr("Write/erase is not working yet on your programmer in "
1923 "its current configuration.\n");
1924 /* --force is the wrong approach, but it's the best we can do
1925 * until the generic programmer parameter parser is merged.
1926 */
1927 if (!force)
1928 return 1;
1929 msg_cerr("Continuing anyway.\n");
1930 }
1931
1932 if (read_it || erase_it || write_it || verify_it) {
1933 /* Everything needs read. */
1934 if (flash->tested & TEST_BAD_READ) {
1935 msg_cerr("Read is not working on this chip. ");
1936 if (!force)
1937 return 1;
1938 msg_cerr("Continuing anyway.\n");
1939 }
1940 if (!flash->read) {
1941 msg_cerr("flashrom has no read function for this "
1942 "flash chip.\n");
1943 return 1;
1944 }
1945 }
1946 if (erase_it || write_it) {
1947 /* Write needs erase. */
1948 if (flash->tested & TEST_BAD_ERASE) {
1949 msg_cerr("Erase is not working on this chip. ");
1950 if (!force)
1951 return 1;
1952 msg_cerr("Continuing anyway.\n");
1953 }
stefancte1c5acf2011-07-04 07:27:17 +00001954 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001955 msg_cerr("flashrom has no erase function for this "
1956 "flash chip.\n");
1957 return 1;
1958 }
hailfinger771fc182010-10-15 00:01:14 +00001959 }
1960 if (write_it) {
1961 if (flash->tested & TEST_BAD_WRITE) {
1962 msg_cerr("Write is not working on this chip. ");
1963 if (!force)
1964 return 1;
1965 msg_cerr("Continuing anyway.\n");
1966 }
1967 if (!flash->write) {
1968 msg_cerr("flashrom has no write function for this "
1969 "flash chip.\n");
1970 return 1;
1971 }
1972 }
1973 return 0;
1974}
1975
hailfingerc77acb52009-12-24 02:15:55 +00001976/* This function signature is horrible. We need to design a better interface,
1977 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001978 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001979 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001980int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09001981 int write_it, int erase_it, int verify_it, int extract_it,
1982 const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001983{
hailfinger4c47e9d2010-10-19 22:06:20 +00001984 uint8_t *oldcontents;
1985 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001986 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001987 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001988
stefanct02116582011-05-18 01:30:56 +00001989 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001990 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001991 ret = 1;
1992 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001993 }
1994
hailfinger771fc182010-10-15 00:01:14 +00001995 /* Given the existence of read locks, we want to unlock for read,
1996 * erase and write.
1997 */
1998 if (flash->unlock)
1999 flash->unlock(flash);
2000
David Hendricks6d62d752011-03-07 21:20:22 -08002001 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08002002 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08002003 ret = 1;
2004 goto out_nofree;
2005 }
2006
Simon Glass9ad06c12013-07-03 22:08:17 +09002007 if (extract_it) {
2008 ret = extract_regions(flash);
2009 goto out_nofree;
2010 }
2011
David Hendricksd0ea9ed2011-03-04 17:31:57 -08002012 /* mark entries included using -i argument as "included" if they are
2013 found in the master rom_entries list */
2014 if (process_include_args() < 0) {
2015 ret = 1;
2016 goto out_nofree;
2017 }
2018
hailfinger771fc182010-10-15 00:01:14 +00002019 if (read_it) {
2020 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00002021 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00002022 }
hailfingerb437e282010-11-04 01:04:27 +00002023
stefanctd611e8f2011-07-12 22:35:21 +00002024 oldcontents = malloc(size);
2025 if (!oldcontents) {
2026 msg_gerr("Out of memory!\n");
2027 exit(1);
2028 }
Simon Glass4c214132013-07-16 10:09:28 -06002029 /* Assume worst case: All blocks are not erased. */
2030 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002031 newcontents = malloc(size);
2032 if (!newcontents) {
2033 msg_gerr("Out of memory!\n");
2034 exit(1);
2035 }
Simon Glass4c214132013-07-16 10:09:28 -06002036 /* Assume best case: All blocks are erased. */
2037 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002038 /* Side effect of the assumptions above: Default write action is erase
2039 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002040 * oldcontents being completely unerased means we have to erase
2041 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002042 */
2043
hailfingerd217d122010-10-08 18:52:29 +00002044 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002045 /*
2046 * Note: This must be done before any files specified by -i
2047 * arguments are processed merged into the newcontents since
2048 * -i files take priority. See http://crbug.com/263495.
2049 */
2050 if (filename) {
2051 if (read_buf_from_file(newcontents, size, filename)) {
2052 ret = 1;
2053 goto out;
2054 }
2055 } else {
2056 /* Content will be read from -i args, so they must
2057 * not overlap. */
2058 if (included_regions_overlap()) {
2059 msg_gerr("Error: Included regions must "
2060 "not overlap.\n");
2061 ret = 1;
2062 goto out;
2063 }
stepan1da96c02006-11-21 23:48:51 +00002064 }
2065
David Hendricksac82cac2012-06-19 10:29:37 -07002066#if 0
2067 /*
2068 * FIXME: show_id() causes failure if vendor:mainboard do not
2069 * match. This may happen if codenames are in flux.
2070 * See chrome-os-partner:10414.
2071 */
hailfinger90c7d542010-05-31 15:27:27 +00002072#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002073 if (programmer == PROGRAMMER_INTERNAL)
2074 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002075#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002076#endif
ollie5672ac62004-03-17 22:22:08 +00002077 }
2078
David Hendricksc44d7a02011-10-17 11:28:43 -07002079 /* Obtain a reference image so that we can check whether regions need
2080 * to be erased and to give better diagnostics in case write fails.
David Hendricks52ddff02013-07-23 15:05:14 -07002081 * If --fast-verify is used then only the regions which are included
2082 * using -i will be read.
hailfinger4c47e9d2010-10-19 22:06:20 +00002083 */
David Hendricksc44d7a02011-10-17 11:28:43 -07002084 if (diff_file) {
2085 msg_cdbg("Reading old contents from file... ");
2086 if (read_buf_from_file(oldcontents, size, diff_file)) {
2087 ret = 1;
2088 msg_cdbg("FAILED.\n");
2089 goto out;
2090 }
2091 } else {
2092 msg_cdbg("Reading old contents from flash chip... ");
David Hendricksd4e712c2013-08-02 17:06:16 -07002093 if (verify_it == VERIFY_PARTIAL) {
2094 if (handle_partial_read(flash, oldcontents,
2095 read_flash, 0) < 0) {
David Hendricks52ddff02013-07-23 15:05:14 -07002096 ret = 1;
2097 msg_cdbg("FAILED.\n");
2098 goto out;
2099 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002100 } else {
2101 if (read_flash(flash, oldcontents, 0, size)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002102 ret = 1;
2103 msg_cdbg("FAILED.\n");
2104 goto out;
2105 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002106 }
hailfinger4c47e9d2010-10-19 22:06:20 +00002107 }
David Hendricks89a45e52011-11-22 16:56:22 -08002108 msg_cdbg("done.\n");
David Hendricksac1d25c2016-08-09 17:00:58 -07002109
David Hendricksdf29a832013-06-28 14:33:51 -07002110 /*
2111 * Note: This must be done after reading the file specified for the
2112 * -w/-v argument, if any, so that files specified using -i end up
2113 * in the "newcontents" buffer before being written.
2114 * See http://crbug.com/263495.
2115 */
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002116 if (handle_romentries(flash, oldcontents, newcontents)) {
2117 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002118 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002119 goto out;
2120 }
uwef6641642007-05-09 10:17:44 +00002121
David Hendricksa7e114b2016-02-26 18:49:15 -08002122 if (erase_it) {
2123 /* FIXME: Do we really want the scary warning if erase failed?
2124 * After all, after erase the chip is either blank or partially
2125 * blank or it has the old contents. A blank chip won't boot,
2126 * so if the user wanted erase and reboots afterwards, the user
2127 * knows very well that booting won't work.
2128 */
2129 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2130 emergency_help_message();
2131 ret = 1;
2132 }
2133 goto out;
2134 }
2135
stuge8ce3a3c2008-04-28 14:47:30 +00002136 if (write_it) {
David Hendricksb64b39a2016-10-11 13:48:06 -07002137 // parse the new fmap and disable soft WP if necessary
David Hendricksac1d25c2016-08-09 17:00:58 -07002138 if ((ret = cros_ec_prepare(newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002139 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002140 goto out;
2141 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002142
hailfingerb437e282010-11-04 01:04:27 +00002143 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2144 msg_cerr("Uh oh. Erase/write failed. Checking if "
2145 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002146 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002147 if (!memcmp(oldcontents, newcontents, size)) {
2148 msg_cinfo("Good. It seems nothing was "
2149 "changed.\n");
2150 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002151 ret = 1;
2152 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002153 }
2154 }
hailfingerd217d122010-10-08 18:52:29 +00002155 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002156 ret = 1;
2157 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002158 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002159
David Hendricksac1d25c2016-08-09 17:00:58 -07002160 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002161 if (ret < 0) {
2162 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002163 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002164 emergency_help_message();
2165 ret = 1;
2166 goto out;
2167 } else if (ret > 0) {
2168 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002169 msg_pdbg("CROS_EC needs 2nd pass.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002170 if (read_flash(flash, oldcontents, 0, size)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002171 msg_cerr("Uh oh. Cannot get latest content.\n");
2172 emergency_help_message();
2173 ret = 1;
2174 goto out;
2175 }
2176 // write 2nd pass
2177 if (erase_and_write_flash(flash, oldcontents,
2178 newcontents)) {
David Hendricksb907de32014-08-11 16:47:09 -07002179 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002180 emergency_help_message();
2181 ret = 1;
2182 goto out;
2183 }
2184 ret = 0;
2185 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002186
David Hendricksac1d25c2016-08-09 17:00:58 -07002187 if (cros_ec_finish() < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002188 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002189 emergency_help_message();
2190 ret = 1;
2191 goto out;
2192 }
stuge8ce3a3c2008-04-28 14:47:30 +00002193 }
ollie6a600992005-11-26 21:55:36 +00002194
hailfinger0459e1c2009-08-19 13:55:34 +00002195 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002196 if ((write_it || erase_it) && !content_has_changed) {
2197 msg_gdbg("Nothing was erased or written, skipping "
2198 "verification\n");
2199 } else {
2200 /* Work around chips which need some time to calm down. */
2201 if (write_it && verify_it != VERIFY_PARTIAL)
2202 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002203
David Hendricks9ba79fb2015-04-03 12:06:16 -07002204 ret = verify_flash(flash, newcontents, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002205
David Hendricks9ba79fb2015-04-03 12:06:16 -07002206 /* If we tried to write, and verification now fails, we
2207 * might have an emergency situation.
2208 */
2209 if (ret && write_it)
2210 emergency_help_message();
2211 }
hailfinger0459e1c2009-08-19 13:55:34 +00002212 }
ollie6a600992005-11-26 21:55:36 +00002213
hailfinger90fcf9b2010-11-05 14:51:59 +00002214out:
2215 free(oldcontents);
2216 free(newcontents);
2217out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002218 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002219 /*
2220 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2221 * tree. This is because some operations, such as write protection,
2222 * requires programmer_shutdown() but does not call doit().
2223 */
2224// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002225 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002226}