blob: 1b26ddae1764a8ed1d4bb29b45d83512ce670f25 [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>
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -070038
39#include "action_descriptor.h"
rminnich8d3ff912003-10-25 17:01:29 +000040#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000041#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090042#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000043#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000044
krause2eb76212011-01-17 07:50:42 +000045const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000046char *chip_to_probe = NULL;
Souvik Ghosh3c963a42016-07-19 18:48:15 -070047int verbose_screen = MSG_ERROR;
48int verbose_logfile = MSG_DEBUG2;
hailfinger80422e22009-12-13 22:28:00 +000049
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
Patrick Georgi8ddfee92017-03-20 14:54:28 +0100311 {0}, /* 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 return ret;
uweabe92a52009-05-16 22:36:00 +0000410}
411
David Hendricksbf36f092010-11-02 23:39:29 -0700412int chip_restore()
413{
414 int rc = 0;
415
416 while (chip_restore_fn_count > 0) {
417 int i = --chip_restore_fn_count;
418 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
419 chip_restore_fn[i].status);
420 }
421
422 return rc;
423}
424
David Hendricks93784b42016-08-09 17:00:38 -0700425int programmer_shutdown(void)
uweabe92a52009-05-16 22:36:00 +0000426{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000427 int ret = 0;
428
hailfinger1ff33dc2010-07-03 11:02:10 +0000429 /* Registering shutdown functions is no longer allowed. */
430 may_register_shutdown = 0;
431 while (shutdown_fn_count > 0) {
432 int i = --shutdown_fn_count;
David Hendricks93784b42016-08-09 17:00:38 -0700433 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000434 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000435 return ret;
uweabe92a52009-05-16 22:36:00 +0000436}
437
438void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
439 size_t len)
440{
441 return programmer_table[programmer].map_flash_region(descr,
442 phys_addr, len);
443}
444
445void programmer_unmap_flash_region(void *virt_addr, size_t len)
446{
447 programmer_table[programmer].unmap_flash_region(virt_addr, len);
448}
449
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700450void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000451{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100452 par_master->chip_writeb(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000453}
454
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700455void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000456{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100457 par_master->chip_writew(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000458}
459
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700460void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000461{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100462 par_master->chip_writel(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000463}
464
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700465void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000466{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100467 par_master->chip_writen(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000468}
469
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700470uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000471{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100472 return par_master->chip_readb(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000473}
474
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700475uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000476{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100477 return par_master->chip_readw(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000478}
479
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700480uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000481{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100482 return par_master->chip_readl(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000483}
484
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700485void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000486{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100487 par_master->chip_readn(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000488}
489
hailfingere5829f62009-06-05 17:48:08 +0000490void programmer_delay(int usecs)
491{
492 programmer_table[programmer].delay(usecs);
493}
494
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700495void map_flash_registers(struct flashctx *flash)
stepan15e64bc2007-05-24 08:48:10 +0000496{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100497 size_t size = flash->chip->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000498 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000499 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000500 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000501}
502
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700503int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000504{
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700505 chip_readn(flash, buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000506
hailfinger23060112009-05-08 12:49:03 +0000507 return 0;
508}
509
hailfinger7b414742009-06-13 12:04:03 +0000510int min(int a, int b)
511{
512 return (a < b) ? a : b;
513}
514
hailfinger7af83692009-06-15 17:23:36 +0000515int max(int a, int b)
516{
517 return (a > b) ? a : b;
518}
519
hailfingeraec9c962009-10-31 01:53:09 +0000520int bitcount(unsigned long a)
521{
522 int i = 0;
523 for (; a != 0; a >>= 1)
524 if (a & 1)
525 i++;
526 return i;
527}
528
hailfingerf76cc322010-11-09 22:00:31 +0000529void tolower_string(char *str)
530{
531 for (; *str != '\0'; str++)
532 *str = (char)tolower((unsigned char)*str);
533}
534
hailfingera916b422009-06-01 02:08:58 +0000535char *strcat_realloc(char *dest, const char *src)
536{
537 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000538 if (!dest) {
539 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000540 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000541 }
hailfingera916b422009-06-01 02:08:58 +0000542 strcat(dest, src);
543 return dest;
544}
545
David Hendricksda18f692016-10-21 17:49:49 -0700546/* This is a somewhat hacked function similar in some ways to strtok(). It will
547 * look for needle with a subsequent '=' in haystack, return a copy of needle.
hailfinger6e5a52a2009-11-24 18:27:10 +0000548 */
stefanct52700282011-06-26 17:38:17 +0000549char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000550{
David Hendricksda18f692016-10-21 17:49:49 -0700551 char *param_pos, *opt_pos;
hailfinger1ef766d2010-07-06 09:55:48 +0000552 char *opt = NULL;
553 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000554 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000555
hailfingerf4aaccc2010-04-28 15:22:14 +0000556 needlelen = strlen(needle);
557 if (!needlelen) {
558 msg_gerr("%s: empty needle! Please report a bug at "
559 "flashrom@flashrom.org\n", __func__);
560 return NULL;
561 }
562 /* No programmer parameters given. */
563 if (*haystack == NULL)
564 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000565 param_pos = strstr(*haystack, needle);
566 do {
567 if (!param_pos)
568 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000569 /* Needle followed by '='? */
570 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600571
hailfinger1ef766d2010-07-06 09:55:48 +0000572 /* Beginning of the string? */
573 if (param_pos == *haystack)
574 break;
575 /* After a delimiter? */
576 if (strchr(delim, *(param_pos - 1)))
577 break;
578 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000579 /* Continue searching. */
580 param_pos++;
581 param_pos = strstr(param_pos, needle);
582 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000583
hailfinger6e5a52a2009-11-24 18:27:10 +0000584 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000585 /* Get the string after needle and '='. */
586 opt_pos = param_pos + needlelen + 1;
587 optlen = strcspn(opt_pos, delim);
588 /* Return an empty string if the parameter was empty. */
589 opt = malloc(optlen + 1);
590 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000591 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000592 exit(1);
593 }
hailfinger1ef766d2010-07-06 09:55:48 +0000594 strncpy(opt, opt_pos, optlen);
595 opt[optlen] = '\0';
hailfinger6e5a52a2009-11-24 18:27:10 +0000596 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000597
hailfinger1ef766d2010-07-06 09:55:48 +0000598 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000599}
600
stefanct52700282011-06-26 17:38:17 +0000601char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000602{
603 return extract_param(&programmer_param, param_name, ",");
604}
605
stefancte1c5acf2011-07-04 07:27:17 +0000606/* Returns the number of well-defined erasers for a chip. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700607static unsigned int count_usable_erasers(const struct flashctx *flash)
stefanct569dbb62011-07-01 00:19:12 +0000608{
609 unsigned int usable_erasefunctions = 0;
610 int k;
611 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
612 if (!check_block_eraser(flash, k, 0))
613 usable_erasefunctions++;
614 }
615 return usable_erasefunctions;
616}
617
hailfinger7af83692009-06-15 17:23:36 +0000618/* start is an offset to the base address of the flash chip */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700619int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000620{
621 int ret;
622 uint8_t *cmpbuf = malloc(len);
623
624 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000625 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000626 exit(1);
627 }
Simon Glass4c214132013-07-16 10:09:28 -0600628 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000629 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
630 free(cmpbuf);
631 return ret;
632}
633
Simon Glass4e305f42015-01-08 06:29:04 -0700634static int compare_chunk(uint8_t *readbuf, uint8_t *cmpbuf, unsigned int start,
635 unsigned int len, const char *message)
636{
637 int failcount = 0, i;
638
639 for (i = 0; i < len; i++) {
640 if (cmpbuf[i] != readbuf[i]) {
641 if (!failcount) {
642 msg_cerr("%s FAILED at 0x%08x! "
643 "Expected=0x%02x, Read=0x%02x,",
644 message, start + i,
645 cmpbuf[i], readbuf[i]);
646 }
647 failcount++;
648 }
649 }
650
651 return failcount;
652}
653
uwee15beb92010-08-08 17:01:18 +0000654/*
hailfinger7af3d192009-11-25 17:05:52 +0000655 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000656 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000657 * @start offset to the base address of the flash chip
658 * @len length of the verified area
659 * @message string to print in the "FAILED" message
660 * @return 0 for success, -1 for failure
661 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700662int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000663 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000664{
hailfinger8cb6ece2010-11-16 17:21:58 +0000665 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000666 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000667
668 if (!len)
669 goto out_free;
670
Patrick Georgif3fa2992017-02-02 16:24:44 +0100671 if (!flash->chip->read) {
snelsone42c3802010-05-07 20:09:04 +0000672 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000673 return 1;
674 }
hailfinger7af83692009-06-15 17:23:36 +0000675 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000676 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000677 exit(1);
678 }
679
Patrick Georgif3fa2992017-02-02 16:24:44 +0100680 if (start + len > flash->chip->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000681 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000682 " total_size 0x%x\n", __func__, start, len,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100683 flash->chip->total_size * 1024);
hailfinger7af83692009-06-15 17:23:36 +0000684 ret = -1;
685 goto out_free;
686 }
687 if (!message)
688 message = "VERIFY";
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -0700689 msg_gdbg("%#06x..%#06x ", start, start + len -1);
Simon Glass4e305f42015-01-08 06:29:04 -0700690 if (programmer_table[programmer].paranoid) {
691 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800692
Simon Glass4e305f42015-01-08 06:29:04 -0700693 /* limit chunksize in order to catch errors early */
694 for (i = 0, chunksize = 0; i < len; i += chunksize) {
695 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800696
Patrick Georgif3fa2992017-02-02 16:24:44 +0100697 chunksize = min(flash->chip->page_size, len - i);
698 tmp = flash->chip->read(flash, readbuf + i, start + i, chunksize);
Simon Glass4e305f42015-01-08 06:29:04 -0700699 if (tmp) {
700 ret = tmp;
701 if (ignore_error(tmp))
702 continue;
703 else
704 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800705 }
Simon Glass4e305f42015-01-08 06:29:04 -0700706
707 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
708 chunksize, message);
709 if (failcount)
710 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800711 }
Simon Glass4e305f42015-01-08 06:29:04 -0700712 } else {
713 int tmp;
714
715 /* read as much as we can to reduce transaction overhead */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100716 tmp = flash->chip->read(flash, readbuf, start, len);
Simon Glass4e305f42015-01-08 06:29:04 -0700717 if (tmp && !ignore_error(tmp)) {
718 ret = tmp;
719 goto out_free;
720 }
721
722 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000723 }
724
hailfinger5be6c0f2009-07-23 01:42:56 +0000725 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000726 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000727 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000728 ret = -1;
729 }
hailfinger7af83692009-06-15 17:23:36 +0000730
731out_free:
732 free(readbuf);
733 return ret;
734}
735
uwee15beb92010-08-08 17:01:18 +0000736/*
hailfingerb247c7a2010-03-08 00:42:32 +0000737 * Check if the buffer @have can be programmed to the content of @want without
738 * erasing. This is only possible if all chunks of size @gran are either kept
739 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000740 *
hailfingerb247c7a2010-03-08 00:42:32 +0000741 * The following write granularities (enum @gran) are known:
742 * - 1 bit. Each bit can be cleared individually.
743 * - 1 byte. A byte can be written once. Further writes to an already written
744 * byte cause the contents to be either undefined or to stay unchanged.
745 * - 128 bytes. If less than 128 bytes are written, the rest will be
746 * erased. Each write to a 128-byte region will trigger an automatic erase
747 * before anything is written. Very uncommon behaviour and unsupported by
748 * this function.
749 * - 256 bytes. If less than 256 bytes are written, the contents of the
750 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000751 * Warning: This function assumes that @have and @want point to naturally
752 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000753 *
754 * @have buffer with current content
755 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000756 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000757 * @gran write granularity (enum, not count)
758 * @return 0 if no erase is needed, 1 otherwise
759 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700760static int need_erase(struct flashctx *flash, uint8_t *have, uint8_t *want,
Simon Glass4c214132013-07-16 10:09:28 -0600761 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000762{
hailfingerb91c08c2011-08-15 19:54:20 +0000763 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000764 unsigned int i, j, limit;
Simon Glass4c214132013-07-16 10:09:28 -0600765 int erase_value = flash_erase_value(flash);
hailfingerb247c7a2010-03-08 00:42:32 +0000766
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700767 if (flash->chip->feature_bits & FEATURE_NO_ERASE)
768 return 0;
769
hailfingerb247c7a2010-03-08 00:42:32 +0000770 switch (gran) {
771 case write_gran_1bit:
772 for (i = 0; i < len; i++)
773 if ((have[i] & want[i]) != want[i]) {
774 result = 1;
775 break;
776 }
777 break;
778 case write_gran_1byte:
779 for (i = 0; i < len; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600780 if ((have[i] != want[i]) && (have[i] != erase_value)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000781 result = 1;
782 break;
783 }
784 break;
785 case write_gran_256bytes:
786 for (j = 0; j < len / 256; j++) {
787 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000788 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000789 if (!memcmp(have + j * 256, want + j * 256, limit))
790 continue;
791 /* have needs to be in erased state. */
792 for (i = 0; i < limit; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600793 if (have[j * 256 + i] != erase_value) {
hailfingerb247c7a2010-03-08 00:42:32 +0000794 result = 1;
795 break;
796 }
797 if (result)
798 break;
799 }
800 break;
hailfingerb437e282010-11-04 01:04:27 +0000801 default:
802 msg_cerr("%s: Unsupported granularity! Please report a bug at "
803 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000804 }
805 return result;
806}
807
hailfingerb437e282010-11-04 01:04:27 +0000808/**
809 * Check if the buffer @have needs to be programmed to get the content of @want.
810 * If yes, return 1 and fill in first_start with the start address of the
811 * write operation and first_len with the length of the first to-be-written
812 * chunk. If not, return 0 and leave first_start and first_len undefined.
813 *
814 * Warning: This function assumes that @have and @want point to naturally
815 * aligned regions.
816 *
817 * @have buffer with current content
818 * @want buffer with desired content
819 * @len length of the checked area
820 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000821 * @first_start offset of the first byte which needs to be written (passed in
822 * value is increased by the offset of the first needed write
823 * relative to have/want or unchanged if no write is needed)
824 * @return length of the first contiguous area which needs to be written
825 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000826 *
827 * FIXME: This function needs a parameter which tells it about coalescing
828 * in relation to the max write length of the programmer and the max write
829 * length of the chip.
830 */
stefanctc5eb8a92011-11-23 09:13:48 +0000831static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
832 unsigned int *first_start,
833 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000834{
stefanctc5eb8a92011-11-23 09:13:48 +0000835 int need_write = 0;
836 unsigned int rel_start = 0, first_len = 0;
837 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000838
hailfingerb437e282010-11-04 01:04:27 +0000839 switch (gran) {
840 case write_gran_1bit:
841 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000842 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000843 break;
844 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000845 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000846 break;
847 default:
848 msg_cerr("%s: Unsupported granularity! Please report a bug at "
849 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000850 /* Claim that no write was needed. A write with unknown
851 * granularity is too dangerous to try.
852 */
853 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000854 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000855 for (i = 0; i < len / stride; i++) {
856 limit = min(stride, len - i * stride);
857 /* Are 'have' and 'want' identical? */
858 if (memcmp(have + i * stride, want + i * stride, limit)) {
859 if (!need_write) {
860 /* First location where have and want differ. */
861 need_write = 1;
862 rel_start = i * stride;
863 }
864 } else {
865 if (need_write) {
866 /* First location where have and want
867 * do not differ anymore.
868 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000869 break;
870 }
871 }
872 }
hailfingerffb7f382010-12-06 13:05:44 +0000873 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000874 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000875 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000876 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000877}
878
hailfinger0c515352009-11-23 12:55:31 +0000879/* This function generates various test patterns useful for testing controller
880 * and chip communication as well as chip behaviour.
881 *
882 * If a byte can be written multiple times, each time keeping 0-bits at 0
883 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
884 * is essentially an AND operation. That's also the reason why this function
885 * provides the result of AND between various patterns.
886 *
887 * Below is a list of patterns (and their block length).
888 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
889 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
890 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
891 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
892 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
893 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
894 * Pattern 6 is 00 (1 Byte)
895 * Pattern 7 is ff (1 Byte)
896 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
897 * byte block.
898 *
899 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
900 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
901 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
902 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
903 * Pattern 12 is 00 (1 Byte)
904 * Pattern 13 is ff (1 Byte)
905 * Patterns 8-13 have no block number.
906 *
907 * Patterns 0-3 are created to detect and efficiently diagnose communication
908 * slips like missed bits or bytes and their repetitive nature gives good visual
909 * cues to the person inspecting the results. In addition, the following holds:
910 * AND Pattern 0/1 == Pattern 4
911 * AND Pattern 2/3 == Pattern 5
912 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
913 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
914 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
915 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
916 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
917 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
918 * Besides that, they provide for bit testing of the last two bytes of every
919 * 256 byte block which contains the block number for patterns 0-6.
920 * Patterns 10-11 are special purpose for detecting subblock aliasing with
921 * block sizes >256 bytes (some Dataflash chips etc.)
922 * AND Pattern 8/9 == Pattern 12
923 * AND Pattern 10/11 == Pattern 12
924 * Pattern 13 is the completely erased state.
925 * None of the patterns can detect aliasing at boundaries which are a multiple
926 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
927 */
928int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
929{
930 int i;
931
932 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000933 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000934 return 1;
935 }
936
937 switch (variant) {
938 case 0:
939 for (i = 0; i < size; i++)
940 buf[i] = (i & 0xf) << 4 | 0x5;
941 break;
942 case 1:
943 for (i = 0; i < size; i++)
944 buf[i] = (i & 0xf) << 4 | 0xa;
945 break;
946 case 2:
947 for (i = 0; i < size; i++)
948 buf[i] = 0x50 | (i & 0xf);
949 break;
950 case 3:
951 for (i = 0; i < size; i++)
952 buf[i] = 0xa0 | (i & 0xf);
953 break;
954 case 4:
955 for (i = 0; i < size; i++)
956 buf[i] = (i & 0xf) << 4;
957 break;
958 case 5:
959 for (i = 0; i < size; i++)
960 buf[i] = i & 0xf;
961 break;
962 case 6:
963 memset(buf, 0x00, size);
964 break;
965 case 7:
966 memset(buf, 0xff, size);
967 break;
968 case 8:
969 for (i = 0; i < size; i++)
970 buf[i] = i & 0xff;
971 break;
972 case 9:
973 for (i = 0; i < size; i++)
974 buf[i] = ~(i & 0xff);
975 break;
976 case 10:
977 for (i = 0; i < size % 2; i++) {
978 buf[i * 2] = (i >> 8) & 0xff;
979 buf[i * 2 + 1] = i & 0xff;
980 }
981 if (size & 0x1)
982 buf[i * 2] = (i >> 8) & 0xff;
983 break;
984 case 11:
985 for (i = 0; i < size % 2; i++) {
986 buf[i * 2] = ~((i >> 8) & 0xff);
987 buf[i * 2 + 1] = ~(i & 0xff);
988 }
989 if (size & 0x1)
990 buf[i * 2] = ~((i >> 8) & 0xff);
991 break;
992 case 12:
993 memset(buf, 0x00, size);
994 break;
995 case 13:
996 memset(buf, 0xff, size);
997 break;
998 }
999
1000 if ((variant >= 0) && (variant <= 7)) {
1001 /* Write block number in the last two bytes of each 256-byte
1002 * block, big endian for easier reading of the hexdump.
1003 * Note that this wraps around for chips larger than 2^24 bytes
1004 * (16 MB).
1005 */
1006 for (i = 0; i < size / 256; i++) {
1007 buf[i * 256 + 254] = (i >> 8) & 0xff;
1008 buf[i * 256 + 255] = i & 0xff;
1009 }
1010 }
1011
1012 return 0;
1013}
1014
hailfingeraec9c962009-10-31 01:53:09 +00001015int check_max_decode(enum chipbustype buses, uint32_t size)
1016{
1017 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001018
1019 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001020 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001021 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001022 "size %u kB of chipset/board/programmer "
1023 "for %s interface, "
1024 "probe/read/erase/write may fail. ", size / 1024,
1025 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001026 }
hailfingere1e41ea2011-07-27 07:13:06 +00001027 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001028 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001029 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001030 "size %u kB of chipset/board/programmer "
1031 "for %s interface, "
1032 "probe/read/erase/write may fail. ", size / 1024,
1033 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001034 }
hailfingere1e41ea2011-07-27 07:13:06 +00001035 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001036 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001037 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001038 "size %u kB of chipset/board/programmer "
1039 "for %s interface, "
1040 "probe/read/erase/write may fail. ", size / 1024,
1041 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001042 }
hailfingere1e41ea2011-07-27 07:13:06 +00001043 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001044 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001045 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001046 "size %u kB of chipset/board/programmer "
1047 "for %s interface, "
1048 "probe/read/erase/write may fail. ", size / 1024,
1049 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001050 }
1051 if (!limitexceeded)
1052 return 0;
1053 /* Sometimes chip and programmer have more than one bus in common,
1054 * and the limit is not exceeded on all buses. Tell the user.
1055 */
1056 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001057 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001058 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001059 "interface which can support a chip of this size. "
1060 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001061 return 1;
1062}
1063
David Hendricksac1d25c2016-08-09 17:00:58 -07001064int probe_flash(int startchip, struct flashctx *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001065{
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301066 const struct flashchip *flash, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001067 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001068 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001069 uint32_t size;
1070 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001071 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001072
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301073 /* Based on the host controller interface that a platform
1074 * needs to use (hwseq or swseq),
1075 * set the flashchips list here.
1076 */
1077 switch (ich_generation) {
1078 case CHIPSET_100_SERIES_SUNRISE_POINT:
Furquan Shaikh44088752016-07-11 22:48:08 -07001079 case CHIPSET_APL:
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301080 flash_list = flashchips_hwseq;
1081 break;
1082 default:
1083 flash_list = flashchips;
1084 break;
1085 }
1086
1087 for (flash = flash_list + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001088 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001089 continue;
David Hendricksac1d25c2016-08-09 17:00:58 -07001090 buses_common = buses_supported & flash->bustype;
1091 if (!buses_common) {
1092 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1093 flash->vendor, flash->name, flash->total_size);
1094 tmp = flashbuses_to_text(buses_supported);
1095 msg_gspew("Host bus type %s ", tmp);
1096 free(tmp);
1097 tmp = flashbuses_to_text(flash->bustype);
1098 msg_gspew("and chip bus type %s are incompatible.",
1099 tmp);
1100 free(tmp);
1101 msg_gspew("\n");
hailfinger18bd4cc2011-06-17 22:38:53 +00001102 continue;
David Hendricksac1d25c2016-08-09 17:00:58 -07001103 }
hailfinger18bd4cc2011-06-17 22:38:53 +00001104 msg_gdbg("Probing for %s %s, %d kB: ",
1105 flash->vendor, flash->name, flash->total_size);
1106 if (!flash->probe && !force) {
1107 msg_gdbg("failed! flashrom has no probe function for "
1108 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001109 continue;
1110 }
stepan782fb172007-04-06 11:58:03 +00001111
ollie5672ac62004-03-17 22:22:08 +00001112 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001113 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001114
hailfinger48ed3e22011-05-04 00:39:50 +00001115 /* Start filling in the dynamic data. */
Patrick Georgif3fa2992017-02-02 16:24:44 +01001116 fill_flash->chip = calloc(1, sizeof(struct flashchip));
1117 if (!fill_flash->chip) {
1118 msg_gerr("Out of memory!\n");
1119 exit(1);
1120 }
1121 memcpy(fill_flash->chip, flash, sizeof(struct flashchip));
hailfinger48ed3e22011-05-04 00:39:50 +00001122
hailfinger72d3b982009-05-09 07:27:23 +00001123 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001124 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001125
stugec1e55fe2008-07-02 17:15:47 +00001126 if (force)
1127 break;
stepanc98b80b2006-03-16 16:57:41 +00001128
Patrick Georgif3fa2992017-02-02 16:24:44 +01001129 if (fill_flash->chip->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001130 goto notfound;
1131
hailfinger48ed3e22011-05-04 00:39:50 +00001132 /* If this is the first chip found, accept it.
1133 * If this is not the first chip found, accept it only if it is
1134 * a non-generic match.
1135 * We could either make chipcount global or provide it as
1136 * parameter, or we assume that startchip==0 means this call to
1137 * probe_flash() is the first one and thus no chip has been
1138 * found before.
1139 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01001140 if (startchip == 0 || fill_flash->chip->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001141 break;
1142
stuge56300c32008-09-03 23:10:05 +00001143notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001144 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
Patrick Georgif3fa2992017-02-02 16:24:44 +01001145 free(fill_flash->chip);
1146 fill_flash->chip = NULL;
rminnich8d3ff912003-10-25 17:01:29 +00001147 }
uwebe4477b2007-08-23 16:08:21 +00001148
stugec1e55fe2008-07-02 17:15:47 +00001149 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001150 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001151
hailfingere11396b2011-03-08 00:09:11 +00001152#if CONFIG_INTERNAL == 1
1153 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001154 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001155 else
1156#endif
stepan3e7aeae2011-01-19 06:21:54 +00001157 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001158
stefanct588b6d22011-06-26 20:45:35 +00001159 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001160 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001161 force ? "Assuming" : "Found", fill_flash->chip->vendor,
1162 fill_flash->chip->name, fill_flash->chip->total_size, tmp,
1163 location);
stefanct588b6d22011-06-26 20:45:35 +00001164 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001165
hailfinger0f4c3952010-12-02 21:59:42 +00001166 /* Flash registers will not be mapped if the chip was forced. Lock info
1167 * may be stored in registers, so avoid lock info printing.
1168 */
1169 if (!force)
Patrick Georgif3fa2992017-02-02 16:24:44 +01001170 if (fill_flash->chip->printlock)
1171 fill_flash->chip->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001172
hailfinger48ed3e22011-05-04 00:39:50 +00001173 /* Return position of matching chip. */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301174 return flash - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001175}
1176
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001177static int verify_flash(struct flashctx *flash,
1178 struct action_descriptor *descriptor,
1179 int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001180{
hailfingerb0f4d122009-06-24 08:20:45 +00001181 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +01001182 unsigned int total_size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001183 uint8_t *buf = descriptor->newcontents;
rminnich8d3ff912003-10-25 17:01:29 +00001184
snelsone42c3802010-05-07 20:09:04 +00001185 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001186
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001187 if (verify_it == VERIFY_PARTIAL) {
1188 struct processing_unit *pu = descriptor->processing_units;
1189
1190 /* Verify only areas which were written. */
1191 while (pu->num_blocks) {
1192 ret = verify_range(flash, buf + pu->offset, pu->offset,
1193 pu->block_size * pu->num_blocks,
1194 NULL);
1195 if (ret)
1196 break;
1197 pu++;
1198 }
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001199 } else {
1200 ret = verify_range(flash, buf, 0, total_size, NULL);
1201 }
uwef6641642007-05-09 10:17:44 +00001202
David Hendricks1ed1d352011-11-23 17:54:37 -08001203 if (ret == ACCESS_DENIED) {
1204 msg_gdbg("Could not fully verify due to access error, ");
1205 if (access_denied_action == error_ignore) {
1206 msg_gdbg("ignoring\n");
1207 ret = 0;
1208 } else {
1209 msg_gdbg("aborting\n");
1210 }
1211 }
1212
hailfingerb0f4d122009-06-24 08:20:45 +00001213 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001214 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001215
hailfingerb0f4d122009-06-24 08:20:45 +00001216 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001217}
1218
uwe8d342eb2011-07-28 08:13:25 +00001219int read_buf_from_file(unsigned char *buf, unsigned long size,
1220 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001221{
1222 unsigned long numbytes;
1223 FILE *image;
1224 struct stat image_stat;
1225
Vincent Palatin7ab23932014-10-01 12:09:16 -07001226 if (!strncmp(filename, "-", sizeof("-")))
1227 image = fdopen(STDIN_FILENO, "rb");
1228 else
1229 image = fopen(filename, "rb");
1230 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001231 perror(filename);
1232 return 1;
1233 }
1234 if (fstat(fileno(image), &image_stat) != 0) {
1235 perror(filename);
1236 fclose(image);
1237 return 1;
1238 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001239 if ((image_stat.st_size != size) &&
1240 (strncmp(filename, "-", sizeof("-")))) {
Mike Frysinger62c794d2017-05-29 12:02:45 -04001241 msg_gerr("Error: Image size doesn't match: stat %jd bytes, "
1242 "wanted %ld!\n", (intmax_t)image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001243 fclose(image);
1244 return 1;
1245 }
1246 numbytes = fread(buf, 1, size, image);
1247 if (fclose(image)) {
1248 perror(filename);
1249 return 1;
1250 }
1251 if (numbytes != size) {
1252 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1253 "wanted %ld!\n", numbytes, size);
1254 return 1;
1255 }
1256 return 0;
1257}
1258
uwe8d342eb2011-07-28 08:13:25 +00001259int write_buf_to_file(unsigned char *buf, unsigned long size,
1260 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001261{
1262 unsigned long numbytes;
1263 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001264
1265 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001266 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001267 return 1;
1268 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001269 if (!strncmp(filename, "-", sizeof("-")))
1270 image = fdopen(STDOUT_FILENO, "wb");
1271 else
1272 image = fopen(filename, "wb");
1273 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001274 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001275 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001276 }
hailfingerd219a232009-01-28 00:27:54 +00001277
hailfingerd219a232009-01-28 00:27:54 +00001278 numbytes = fwrite(buf, 1, size, image);
1279 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001280 if (numbytes != size) {
1281 msg_gerr("File %s could not be written completely.\n",
1282 filename);
hailfingerd219a232009-01-28 00:27:54 +00001283 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001284 }
hailfingerd219a232009-01-28 00:27:54 +00001285 return 0;
1286}
1287
David Hendrickse3451942013-03-21 17:23:29 -07001288/*
1289 * read_flash - wrapper for flash->read() with additional high-level policy
1290 *
1291 * @flash flash chip
1292 * @buf buffer to store data in
1293 * @start start address
1294 * @len number of bytes to read
1295 *
1296 * This wrapper simplifies most cases when the flash chip needs to be read
1297 * since policy decisions such as non-fatal error handling is centralized.
1298 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001299int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001300 unsigned int start, unsigned int len)
1301{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001302 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001303
Patrick Georgif3fa2992017-02-02 16:24:44 +01001304 if (!flash || !flash->chip->read)
David Hendrickse3451942013-03-21 17:23:29 -07001305 return -1;
1306
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001307 msg_cdbg("%#06x-%#06x:R ", start, start + len - 1);
1308
Patrick Georgif3fa2992017-02-02 16:24:44 +01001309 ret = flash->chip->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001310 if (ret) {
1311 if (ignore_error(ret)) {
1312 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1313 start, start + len - 1);
1314 ret = 0;
1315 } else {
1316 msg_gdbg("failed to read 0x%x-0x%x\n",
1317 start, start + len - 1);
1318 }
1319 }
1320
1321 return ret;
1322}
1323
David Hendricks7c8a1612013-04-26 19:14:44 -07001324/*
1325 * write_flash - wrapper for flash->write() with additional high-level policy
1326 *
1327 * @flash flash chip
1328 * @buf buffer to write to flash
1329 * @start start address in flash
1330 * @len number of bytes to write
1331 *
1332 * TODO: Look up regions that are write-protected and avoid attempt to write
1333 * to them at all.
1334 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001335int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001336 unsigned int start, unsigned int len)
1337{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001338 if (!flash || !flash->chip->write)
David Hendricks7c8a1612013-04-26 19:14:44 -07001339 return -1;
1340
Patrick Georgif3fa2992017-02-02 16:24:44 +01001341 return flash->chip->write(flash, buf, start, len);
David Hendricks7c8a1612013-04-26 19:14:44 -07001342}
1343
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001344int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001345{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001346 unsigned long size = flash->chip->total_size * 1024;
hailfinger42a850a2010-07-13 23:56:13 +00001347 unsigned char *buf = calloc(size, sizeof(char));
1348 int ret = 0;
1349
1350 msg_cinfo("Reading flash... ");
1351 if (!buf) {
1352 msg_gerr("Memory allocation failed!\n");
1353 msg_cinfo("FAILED.\n");
1354 return 1;
1355 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001356
1357 /* To support partial read, fill buffer to all 0xFF at beginning to make
1358 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001359 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001360
Patrick Georgif3fa2992017-02-02 16:24:44 +01001361 if (!flash->chip->read) {
hailfinger42a850a2010-07-13 23:56:13 +00001362 msg_cerr("No read function available for this flash chip.\n");
1363 ret = 1;
1364 goto out_free;
1365 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001366
1367 /* First try to handle partial read case, rather than read the whole
1368 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001369 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001370 if (ret < 0) {
1371 msg_cerr("Partial read operation failed!\n");
1372 ret = 1;
1373 goto out_free;
1374 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001375 int num_regions = get_num_include_args();
1376
1377 if (ret != num_regions) {
1378 msg_cerr("Requested %d regions, but only read %d\n",
1379 num_regions, ret);
1380 ret = 1;
1381 goto out_free;
1382 }
1383
1384 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001385 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001386 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001387 msg_cerr("Read operation failed!\n");
1388 ret = 1;
1389 goto out_free;
1390 }
hailfinger42a850a2010-07-13 23:56:13 +00001391 }
1392
David Hendricksdf29a832013-06-28 14:33:51 -07001393 if (filename)
1394 ret = write_buf_to_file(buf, size, filename);
1395
hailfinger42a850a2010-07-13 23:56:13 +00001396out_free:
1397 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001398 if (ret)
1399 msg_cerr("FAILED.");
1400 else
1401 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001402 return ret;
1403}
1404
hailfingerb437e282010-11-04 01:04:27 +00001405/* This function shares a lot of its structure with erase_and_write_flash() and
1406 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001407 * Even if an error is found, the function will keep going and check the rest.
1408 */
hailfinger48ed3e22011-05-04 00:39:50 +00001409static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001410{
hailfingerb91c08c2011-08-15 19:54:20 +00001411 int i, j, k;
1412 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001413
1414 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1415 unsigned int done = 0;
1416 struct block_eraser eraser = flash->block_erasers[k];
1417
1418 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1419 /* Blocks with zero size are bugs in flashchips.c. */
1420 if (eraser.eraseblocks[i].count &&
1421 !eraser.eraseblocks[i].size) {
1422 msg_gerr("ERROR: Flash chip %s erase function "
1423 "%i region %i has size 0. Please report"
1424 " a bug at flashrom@flashrom.org\n",
1425 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001426 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001427 }
1428 /* Blocks with zero count are bugs in flashchips.c. */
1429 if (!eraser.eraseblocks[i].count &&
1430 eraser.eraseblocks[i].size) {
1431 msg_gerr("ERROR: Flash chip %s erase function "
1432 "%i region %i has count 0. Please report"
1433 " a bug at flashrom@flashrom.org\n",
1434 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001435 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001436 }
1437 done += eraser.eraseblocks[i].count *
1438 eraser.eraseblocks[i].size;
1439 }
hailfinger9fed35d2010-01-19 06:42:46 +00001440 /* Empty eraseblock definition with erase function. */
1441 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001442 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001443 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001444 if (!done)
1445 continue;
1446 if (done != flash->total_size * 1024) {
1447 msg_gerr("ERROR: Flash chip %s erase function %i "
1448 "region walking resulted in 0x%06x bytes total,"
1449 " expected 0x%06x bytes. Please report a bug at"
1450 " flashrom@flashrom.org\n", flash->name, k,
1451 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001452 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001453 }
hailfinger9fed35d2010-01-19 06:42:46 +00001454 if (!eraser.block_erase)
1455 continue;
1456 /* Check if there are identical erase functions for different
1457 * layouts. That would imply "magic" erase functions. The
1458 * easiest way to check this is with function pointers.
1459 */
uwef6f94d42010-03-13 17:28:29 +00001460 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001461 if (eraser.block_erase ==
1462 flash->block_erasers[j].block_erase) {
1463 msg_gerr("ERROR: Flash chip %s erase function "
1464 "%i and %i are identical. Please report"
1465 " a bug at flashrom@flashrom.org\n",
1466 flash->name, k, j);
1467 ret = 1;
1468 }
uwef6f94d42010-03-13 17:28:29 +00001469 }
hailfinger45177872010-01-18 08:14:43 +00001470 }
hailfinger9fed35d2010-01-19 06:42:46 +00001471 return ret;
hailfinger45177872010-01-18 08:14:43 +00001472}
1473
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001474static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001475 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001476 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001477 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001478 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001479 unsigned int addr,
1480 unsigned int len))
1481{
stefanctc5eb8a92011-11-23 09:13:48 +00001482 unsigned int starthere = 0, lenhere = 0;
1483 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001484 int block_was_erased = 0;
hailfingerb437e282010-11-04 01:04:27 +00001485 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1486
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001487 /*
1488 * curcontents and newcontents are opaque to walk_eraseregions, and
1489 * need to be adjusted here to keep the impression of proper
1490 * abstraction
hailfingerb437e282010-11-04 01:04:27 +00001491 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001492
hailfinger90fcf9b2010-11-05 14:51:59 +00001493 curcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001494
hailfingerb437e282010-11-04 01:04:27 +00001495 newcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001496
hailfingerb437e282010-11-04 01:04:27 +00001497 msg_cdbg(":");
1498 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001499 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001500 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001501 msg_cdbg("E");
1502 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001503 if (ret) {
1504 if (ret == ACCESS_DENIED)
1505 msg_cdbg("D");
1506 else
1507 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001508 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001509 }
1510
David Hendricks0954ffc2015-11-13 15:15:44 -08001511 if (programmer_table[programmer].paranoid) {
1512 if (check_erased_range(flash, start, len)) {
1513 msg_cerr("ERASE FAILED!\n");
1514 return -1;
1515 }
hailfingerac8e3182011-06-26 17:04:16 +00001516 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001517
hailfinger90fcf9b2010-11-05 14:51:59 +00001518 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001519 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001520 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001521 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001522 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001523 /* get_next_write() sets starthere to a new value after the call. */
1524 while ((lenhere = get_next_write(curcontents + starthere,
1525 newcontents + starthere,
1526 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001527 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001528 if (!writecount++)
1529 msg_cdbg("W");
1530 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001531 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001532 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001533 if (ret) {
1534 if (ret == ACCESS_DENIED)
1535 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001536 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001537 }
David Hendricks048b38c2016-03-28 18:47:06 -07001538
1539 /*
1540 * If the block needed to be erased and was erased successfully
1541 * then we can assume that we didn't run into any write-
1542 * protected areas. Otherwise, we need to verify each page to
1543 * ensure it was successfully written and abort if we encounter
1544 * any errors.
1545 */
1546 if (programmer_table[programmer].paranoid && !block_was_erased) {
1547 if (verify_range(flash, newcontents + starthere,
1548 start + starthere, lenhere, "WRITE"))
1549 return -1;
1550 }
1551
hailfingerb437e282010-11-04 01:04:27 +00001552 starthere += lenhere;
1553 skip = 0;
1554 }
1555 if (skip)
1556 msg_cdbg("S");
1557 return ret;
1558}
1559
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001560/*
1561 * Function to process processing units accumulated in the action descriptor.
1562 *
1563 * @flash pointer to the flash context to operate on
1564 * @do_something helper function which can erase and program a section of the
1565 * flash chip. It receives the flash context, offset and length
1566 * of the area to erase/program, before and after contents (to
1567 * decide what exactly needs to be erased and or programmed)
1568 * and a pointer to the erase function which can operate on the
1569 * proper granularity.
1570 * @descriptor action descriptor including pointers to before and after
1571 * contents and an array of processing actions to take.
1572 *
1573 * Returns zero on success or an error code.
1574 */
1575static int walk_eraseregions(struct flashctx *flash,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001576 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001577 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001578 unsigned int len,
1579 uint8_t *param1,
1580 uint8_t *param2,
1581 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001582 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001583 unsigned int addr,
1584 unsigned int len)),
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001585 struct action_descriptor *descriptor)
hailfinger2b8c9382010-07-13 00:37:19 +00001586{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001587 struct processing_unit *pu;
1588 int rc = 0;
1589 static int print_comma;
uwe8d342eb2011-07-28 08:13:25 +00001590
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001591 for (pu = descriptor->processing_units; pu->num_blocks; pu++) {
1592 unsigned base = pu->offset;
1593 unsigned top = pu->offset + pu->block_size * pu->num_blocks;
David Hendricks605544b2015-08-15 16:32:58 -07001594
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001595 while (base < top) {
David Hendricks605544b2015-08-15 16:32:58 -07001596
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001597 if (print_comma)
hailfingerb437e282010-11-04 01:04:27 +00001598 msg_cdbg(", ");
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001599 else
1600 print_comma = 1;
1601
1602 msg_cdbg("0x%06x-0x%06zx", base, base + pu->block_size - 1);
1603
1604 rc = do_something(flash, base,
1605 pu->block_size,
1606 descriptor->oldcontents,
1607 descriptor->newcontents,
1608 flash->chip->block_erasers[pu->block_eraser_index].block_erase);
1609
David Hendricks1ed1d352011-11-23 17:54:37 -08001610 if (rc) {
1611 if (ignore_error(rc))
1612 rc = 0;
1613 else
1614 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001615 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001616 base += pu->block_size;
hailfinger2b8c9382010-07-13 00:37:19 +00001617 }
1618 }
hailfingerb437e282010-11-04 01:04:27 +00001619 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001620 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001621}
1622
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001623static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001624{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001625 struct block_eraser eraser = flash->chip->block_erasers[k];
hailfingercf848f12010-12-05 15:14:44 +00001626
1627 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1628 if (log)
1629 msg_cdbg("not defined. ");
1630 return 1;
1631 }
1632 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1633 if (log)
1634 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001635 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001636 return 1;
1637 }
1638 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1639 if (log)
1640 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001641 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001642 return 1;
1643 }
1644 return 0;
1645}
1646
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001647int erase_and_write_flash(struct flashctx *flash,
1648 struct action_descriptor *descriptor)
hailfingerd219a232009-01-28 00:27:54 +00001649{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001650 int ret = 1;
hailfingercf848f12010-12-05 15:14:44 +00001651
hailfingercf848f12010-12-05 15:14:44 +00001652 msg_cinfo("Erasing and writing flash chip... ");
hailfingerb437e282010-11-04 01:04:27 +00001653
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001654 ret = walk_eraseregions(flash, &erase_and_write_block_helper, descriptor);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001655
hailfinger7df21362009-09-05 02:30:58 +00001656 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001657 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001658 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001659 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001660 }
1661 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001662}
1663
hailfinger4c47e9d2010-10-19 22:06:20 +00001664void nonfatal_help_message(void)
1665{
1666 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1667 "This means we have to add special support for your board, "
1668 "programmer or flash chip.\n"
1669 "Please report this on IRC at irc.freenode.net (channel "
1670 "#flashrom) or\n"
1671 "mail flashrom@flashrom.org!\n"
1672 "-------------------------------------------------------------"
1673 "------------------\n"
1674 "You may now reboot or simply leave the machine running.\n");
1675}
1676
uweb34ec9f2009-10-01 18:40:02 +00001677void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001678{
snelsone42c3802010-05-07 20:09:04 +00001679 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001680 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001681 "mail flashrom@flashrom.org with FAILED: your board name in "
1682 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001683 "-------------------------------------------------------------"
1684 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001685 "DO NOT REBOOT OR POWEROFF!\n");
1686}
1687
uwe8d342eb2011-07-28 08:13:25 +00001688/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001689void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001690{
1691 enum programmer p;
1692 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001693 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001694 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001695 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001696 }
Simon Glass8dc82732013-07-16 10:13:51 -06001697 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001698}
1699
hailfingerf79d1712010-10-06 23:48:34 +00001700void list_programmers_linebreak(int startcol, int cols, int paren)
1701{
1702 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001703 int pnamelen;
1704 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001705 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001706 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001707
1708 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1709 pname = programmer_table[p].name;
1710 pnamelen = strlen(pname);
1711 if (remaining - pnamelen - 2 < 0) {
1712 if (firstline)
1713 firstline = 0;
1714 else
1715 printf("\n");
1716 for (i = 0; i < startcol; i++)
1717 printf(" ");
1718 remaining = cols - startcol;
1719 } else {
1720 printf(" ");
1721 remaining--;
1722 }
1723 if (paren && (p == 0)) {
1724 printf("(");
1725 remaining--;
1726 }
1727 printf("%s", pname);
1728 remaining -= pnamelen;
1729 if (p < PROGRAMMER_INVALID - 1) {
1730 printf(",");
1731 remaining--;
1732 } else {
1733 if (paren)
1734 printf(")");
1735 printf("\n");
1736 }
1737 }
1738}
1739
hailfinger3b471632010-03-27 16:36:40 +00001740void print_sysinfo(void)
1741{
David Hendricksc6c9f822010-11-03 15:07:01 -07001742 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001743#if HAVE_UTSNAME == 1
1744 struct utsname osinfo;
1745 uname(&osinfo);
1746
David Hendricksc6c9f822010-11-03 15:07:01 -07001747 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001748 osinfo.machine);
1749#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001750 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001751#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001752}
1753
1754void print_buildinfo(void)
1755{
1756 msg_gdbg("flashrom was built with");
hailfinger3b471632010-03-27 16:36:40 +00001757#if NEED_PCI == 1
1758#ifdef PCILIB_VERSION
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001759 msg_gdbg(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001760#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001761 msg_gdbg(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001762#endif
1763#endif
1764#ifdef __clang__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001765 msg_gdbg(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001766#ifdef __clang_version__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001767 msg_gdbg(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001768#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001769 msg_gdbg(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001770#endif
hailfinger3b471632010-03-27 16:36:40 +00001771#elif defined(__GNUC__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001772 msg_gdbg(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001773#ifdef __VERSION__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001774 msg_gdbg(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001775#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001776 msg_gdbg(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001777#endif
1778#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001779 msg_gdbg(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001780#endif
1781#if defined (__FLASHROM_LITTLE_ENDIAN__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001782 msg_gdbg(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001783#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001784 msg_gdbg(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001785#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001786 msg_gdbg("\n");
hailfinger3b471632010-03-27 16:36:40 +00001787}
1788
uwefdeca092008-01-21 15:24:22 +00001789void print_version(void)
1790{
David Hendricksc6c9f822010-11-03 15:07:01 -07001791 /* send to stderr for chromium os */
1792 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001793 print_sysinfo();
David Hendricks07268292016-09-14 16:05:58 -07001794 msg_gerr("\n");
uwefdeca092008-01-21 15:24:22 +00001795}
1796
hailfinger74819ad2010-05-15 15:04:37 +00001797void print_banner(void)
1798{
1799 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001800 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001801 msg_ginfo("\n");
1802}
1803
hailfingerc77acb52009-12-24 02:15:55 +00001804int selfcheck(void)
1805{
hailfinger45177872010-01-18 08:14:43 +00001806 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001807 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001808
1809 /* Safety check. Instead of aborting after the first error, check
1810 * if more errors exist.
1811 */
hailfingerc77acb52009-12-24 02:15:55 +00001812 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001813 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001814 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001815 }
stefanct8632c922011-07-26 14:33:46 +00001816 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001817 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001818 * For 'flashchips' we check the first element to be non-null. In the
1819 * other cases there exist use cases where the first element can be
1820 * null. */
Yunlian Jiang945c3392015-01-20 15:56:44 -08001821 if (flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001822 msg_gerr("Flashchips table miscompilation!\n");
1823 ret = 1;
1824 }
hailfinger45177872010-01-18 08:14:43 +00001825 for (flash = flashchips; flash && flash->name; flash++)
1826 if (selfcheck_eraseblocks(flash))
1827 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001828
hailfinger45177872010-01-18 08:14:43 +00001829 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001830}
1831
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001832void check_chip_supported(const struct flashctx *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001833{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001834 if (flash->chip->feature_bits & FEATURE_OTP) {
Stefan Tauner9816fc82016-08-12 15:47:49 -07001835 msg_cdbg("This chip may contain one-time programmable memory. "
1836 "flashrom cannot read\nand may never be able to write "
1837 "it, hence it may not be able to completely\n"
1838 "clone the contents of this chip (see man page for "
1839 "details).\n");
1840 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001841 if ((flash->chip->tested.probe != OK) ||
1842 (flash->chip->tested.read != OK) ||
1843 (flash->chip->tested.erase != OK) ||
1844 (flash->chip->tested.write != OK) ||
1845 (flash->chip->tested.uread != OK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001846 msg_cdbg("===\n");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001847 if ((flash->chip->tested.probe == BAD) ||
1848 (flash->chip->tested.read == BAD) ||
1849 (flash->chip->tested.erase == BAD) ||
1850 (flash->chip->tested.write == BAD) ||
1851 (flash->chip->tested.uread == BAD)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001852 msg_cdbg("This flash part has status NOT WORKING for operations:");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001853 if (flash->chip->tested.probe == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001854 msg_cdbg(" PROBE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001855 if (flash->chip->tested.read == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001856 msg_cdbg(" READ");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001857 if (flash->chip->tested.erase == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001858 msg_cdbg(" ERASE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001859 if (flash->chip->tested.write == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001860 msg_cdbg(" WRITE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001861 if (flash->chip->tested.uread == BAD)
Duncan Laurie06ffd522015-10-26 12:56:08 -07001862 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001863 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001864 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001865 if ((flash->chip->tested.probe == NT) ||
1866 (flash->chip->tested.read == NT) ||
1867 (flash->chip->tested.erase == NT) ||
1868 (flash->chip->tested.write == NT) ||
1869 (flash->chip->tested.uread == NT)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001870 msg_cdbg("This flash part has status UNTESTED for operations:");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001871 if (flash->chip->tested.probe == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001872 msg_cdbg(" PROBE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001873 if (flash->chip->tested.read == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001874 msg_cdbg(" READ");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001875 if (flash->chip->tested.erase == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001876 msg_cdbg(" ERASE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001877 if (flash->chip->tested.write == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001878 msg_cdbg(" WRITE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001879 if (flash->chip->tested.uread == NT)
Duncan Laurie06ffd522015-10-26 12:56:08 -07001880 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001881 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001882 }
hailfinger92cd8e32010-01-07 03:24:05 +00001883 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001884 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001885 "in the latest development\n"
1886 "version of flashrom. If you are running the latest "
1887 "development version,\n"
1888 "please email a report to flashrom@flashrom.org if "
1889 "any of the above operations\n"
1890 "work correctly for you with this flash part. Please "
1891 "include the flashrom\n"
1892 "output with the additional -V option for all "
1893 "operations you tested (-V, -Vr,\n"
Paul Menzel425dded2016-08-08 16:42:48 -07001894 "-VE, -Vw), and mention which mainboard or "
hailfinger74819ad2010-05-15 15:04:37 +00001895 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001896 "Please mention your board in the subject line. "
1897 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001898 }
1899}
1900
hailfinger771fc182010-10-15 00:01:14 +00001901/* FIXME: This function signature needs to be improved once doit() has a better
1902 * function signature.
1903 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001904int 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 +00001905{
Patrick Georgiac3423f2017-02-03 20:58:06 +01001906 const struct flashchip *chip = flash->chip;
1907
hailfinger771fc182010-10-15 00:01:14 +00001908 if (!programmer_may_write && (write_it || erase_it)) {
1909 msg_perr("Write/erase is not working yet on your programmer in "
1910 "its current configuration.\n");
1911 /* --force is the wrong approach, but it's the best we can do
1912 * until the generic programmer parameter parser is merged.
1913 */
1914 if (!force)
1915 return 1;
1916 msg_cerr("Continuing anyway.\n");
1917 }
1918
1919 if (read_it || erase_it || write_it || verify_it) {
1920 /* Everything needs read. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001921 if (chip->tested.read == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001922 msg_cerr("Read is not working on this chip. ");
1923 if (!force)
1924 return 1;
1925 msg_cerr("Continuing anyway.\n");
1926 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001927 if (!chip->read) {
hailfinger771fc182010-10-15 00:01:14 +00001928 msg_cerr("flashrom has no read function for this "
1929 "flash chip.\n");
1930 return 1;
1931 }
1932 }
1933 if (erase_it || write_it) {
1934 /* Write needs erase. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001935 if (chip->tested.erase == NA) {
1936 msg_cerr("Erase is not possible on this chip.\n");
1937 return 1;
1938 }
1939 if (chip->tested.erase == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001940 msg_cerr("Erase is not working on this chip. ");
1941 if (!force)
1942 return 1;
1943 msg_cerr("Continuing anyway.\n");
1944 }
stefancte1c5acf2011-07-04 07:27:17 +00001945 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001946 msg_cerr("flashrom has no erase function for this "
1947 "flash chip.\n");
1948 return 1;
1949 }
hailfinger771fc182010-10-15 00:01:14 +00001950 }
1951 if (write_it) {
Patrick Georgiac3423f2017-02-03 20:58:06 +01001952 if (chip->tested.write == NA) {
1953 msg_cerr("Write is not possible on this chip.\n");
1954 return 1;
1955 }
1956 if (chip->tested.write == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001957 msg_cerr("Write is not working on this chip. ");
1958 if (!force)
1959 return 1;
1960 msg_cerr("Continuing anyway.\n");
1961 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001962 if (!chip->write) {
hailfinger771fc182010-10-15 00:01:14 +00001963 msg_cerr("flashrom has no write function for this "
1964 "flash chip.\n");
1965 return 1;
1966 }
1967 }
1968 return 0;
1969}
1970
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001971/*
1972 * Function to erase entire flash chip.
1973 *
1974 * @flashctx pointer to the flash context to use
1975 * @oldcontents pointer to the buffer including current chip contents, to
1976 * decide which areas do in fact need to be erased
1977 * @size the size of the flash chip, in bytes.
1978 *
1979 * Returns zero on success or an error code.
1980 */
1981static int erase_chip(struct flashctx *flash, void *oldcontents,
1982 void *newcontents, size_t size)
1983{
1984 /*
1985 * To make sure that the chip is fully erased, let's cheat and create
1986 * a descriptor where the new contents are all erased.
1987 */
1988 struct action_descriptor *fake_descriptor;
1989 int ret = 0;
1990
1991 fake_descriptor = prepare_action_descriptor(flash, oldcontents,
1992 newcontents, 1);
1993 /* FIXME: Do we really want the scary warning if erase failed? After
1994 * all, after erase the chip is either blank or partially blank or it
1995 * has the old contents. A blank chip won't boot, so if the user
1996 * wanted erase and reboots afterwards, the user knows very well that
1997 * booting won't work.
1998 */
1999 if (erase_and_write_flash(flash, fake_descriptor)) {
2000 emergency_help_message();
2001 ret = 1;
2002 }
2003
2004 free(fake_descriptor);
2005
2006 return ret;
2007}
2008
hailfingerc77acb52009-12-24 02:15:55 +00002009/* This function signature is horrible. We need to design a better interface,
2010 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00002011 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00002012 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002013int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09002014 int write_it, int erase_it, int verify_it, int extract_it,
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002015 const char *diff_file, int do_diff)
hailfingerc77acb52009-12-24 02:15:55 +00002016{
hailfinger4c47e9d2010-10-19 22:06:20 +00002017 uint8_t *oldcontents;
2018 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00002019 int ret = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +01002020 unsigned long size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002021 struct action_descriptor *descriptor = NULL;
hailfingerc77acb52009-12-24 02:15:55 +00002022
stefanct02116582011-05-18 01:30:56 +00002023 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00002024 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00002025 ret = 1;
2026 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00002027 }
2028
hailfinger771fc182010-10-15 00:01:14 +00002029 /* Given the existence of read locks, we want to unlock for read,
2030 * erase and write.
2031 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01002032 if (flash->chip->unlock)
2033 flash->chip->unlock(flash);
hailfinger771fc182010-10-15 00:01:14 +00002034
Ed Swierk28cf7992017-07-03 13:17:18 -07002035 /* Enable/disable 4-byte addressing mode if flash chip supports it */
2036 if ((flash->chip->feature_bits & FEATURE_4BA_SUPPORT) &&
2037 flash->chip->four_bytes_addr_funcs.set_4ba) {
2038 if (flash->chip->four_bytes_addr_funcs.set_4ba(flash)) {
2039 msg_cerr("Enabling/disabling 4-byte addressing mode failed!\n");
2040 return 1;
Boris Baykov6323c242016-06-11 18:29:03 +02002041 }
Boris Baykov1a2f5322016-06-11 18:29:00 +02002042 }
2043
Simon Glass9ad06c12013-07-03 22:08:17 +09002044 if (extract_it) {
2045 ret = extract_regions(flash);
2046 goto out_nofree;
2047 }
2048
David Hendricksd0ea9ed2011-03-04 17:31:57 -08002049 /* mark entries included using -i argument as "included" if they are
2050 found in the master rom_entries list */
2051 if (process_include_args() < 0) {
2052 ret = 1;
2053 goto out_nofree;
2054 }
2055
hailfinger771fc182010-10-15 00:01:14 +00002056 if (read_it) {
2057 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00002058 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00002059 }
hailfingerb437e282010-11-04 01:04:27 +00002060
stefanctd611e8f2011-07-12 22:35:21 +00002061 oldcontents = malloc(size);
2062 if (!oldcontents) {
2063 msg_gerr("Out of memory!\n");
2064 exit(1);
2065 }
Simon Glass4c214132013-07-16 10:09:28 -06002066 /* Assume worst case: All blocks are not erased. */
2067 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002068 newcontents = malloc(size);
2069 if (!newcontents) {
2070 msg_gerr("Out of memory!\n");
2071 exit(1);
2072 }
Simon Glass4c214132013-07-16 10:09:28 -06002073 /* Assume best case: All blocks are erased. */
2074 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002075 /* Side effect of the assumptions above: Default write action is erase
2076 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002077 * oldcontents being completely unerased means we have to erase
2078 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002079 */
2080
hailfingerd217d122010-10-08 18:52:29 +00002081 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002082 /*
2083 * Note: This must be done before any files specified by -i
2084 * arguments are processed merged into the newcontents since
2085 * -i files take priority. See http://crbug.com/263495.
2086 */
2087 if (filename) {
2088 if (read_buf_from_file(newcontents, size, filename)) {
2089 ret = 1;
2090 goto out;
2091 }
2092 } else {
2093 /* Content will be read from -i args, so they must
2094 * not overlap. */
2095 if (included_regions_overlap()) {
2096 msg_gerr("Error: Included regions must "
2097 "not overlap.\n");
2098 ret = 1;
2099 goto out;
2100 }
stepan1da96c02006-11-21 23:48:51 +00002101 }
2102
David Hendricksac82cac2012-06-19 10:29:37 -07002103#if 0
2104 /*
2105 * FIXME: show_id() causes failure if vendor:mainboard do not
2106 * match. This may happen if codenames are in flux.
2107 * See chrome-os-partner:10414.
2108 */
hailfinger90c7d542010-05-31 15:27:27 +00002109#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002110 if (programmer == PROGRAMMER_INTERNAL)
2111 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002112#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002113#endif
ollie5672ac62004-03-17 22:22:08 +00002114 }
2115
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002116 if (do_diff) {
2117 /*
2118 * Obtain a reference image so that we can check whether
2119 * regions need to be erased and to give better diagnostics in
2120 * case write fails. If --fast-verify is used then only the
2121 * regions which are included using -i will be read.
2122 */
2123 if (diff_file) {
2124 msg_cdbg("Reading old contents from file... ");
2125 if (read_buf_from_file(oldcontents, size, diff_file)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002126 ret = 1;
2127 msg_cdbg("FAILED.\n");
2128 goto out;
2129 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002130 } else {
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002131 msg_cdbg("Reading old contents from flash chip... ");
2132 if (((verify_it == VERIFY_OFF) ||
2133 (verify_it == VERIFY_PARTIAL)) &&
2134 get_num_include_args()) {
2135 /*
2136 * If no full verification is required and not
2137 * the entire chip is about to be programmed,
2138 * read only the areas which might change.
2139 */
2140 if (handle_partial_read(flash, oldcontents,
2141 read_flash, 0) < 0) {
2142 ret = 1;
2143 msg_cdbg("FAILED.\n");
2144 goto out;
2145 }
2146 } else {
2147 if (read_flash(flash, oldcontents, 0, size)) {
2148 ret = 1;
2149 msg_cdbg("FAILED.\n");
2150 goto out;
2151 }
David Hendricks52ddff02013-07-23 15:05:14 -07002152 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002153 }
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002154 msg_cdbg("done.\n");
2155 } else if (!erase_it) {
2156 msg_pinfo("No diff performed, considering the chip erased.\n");
2157 memset(oldcontents, flash_erase_value(flash), size);
hailfinger4c47e9d2010-10-19 22:06:20 +00002158 }
David Hendricksac1d25c2016-08-09 17:00:58 -07002159
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002160
David Hendricksdf29a832013-06-28 14:33:51 -07002161 /*
2162 * Note: This must be done after reading the file specified for the
2163 * -w/-v argument, if any, so that files specified using -i end up
2164 * in the "newcontents" buffer before being written.
2165 * See http://crbug.com/263495.
2166 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002167 if (handle_romentries(flash, oldcontents, newcontents, erase_it)) {
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002168 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002169 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002170 goto out;
2171 }
uwef6641642007-05-09 10:17:44 +00002172
David Hendricksa7e114b2016-02-26 18:49:15 -08002173 if (erase_it) {
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002174 erase_chip(flash, oldcontents, newcontents, size);
2175 goto verify;
David Hendricksa7e114b2016-02-26 18:49:15 -08002176 }
2177
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002178 descriptor = prepare_action_descriptor(flash, oldcontents,
2179 newcontents, do_diff);
stuge8ce3a3c2008-04-28 14:47:30 +00002180 if (write_it) {
David Hendricksb64b39a2016-10-11 13:48:06 -07002181 // parse the new fmap and disable soft WP if necessary
David Hendricksac1d25c2016-08-09 17:00:58 -07002182 if ((ret = cros_ec_prepare(newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002183 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002184 goto out;
2185 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002186
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002187 if (erase_and_write_flash(flash, descriptor)) {
hailfingerb437e282010-11-04 01:04:27 +00002188 msg_cerr("Uh oh. Erase/write failed. Checking if "
2189 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002190 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002191 if (!memcmp(oldcontents, newcontents, size)) {
2192 msg_cinfo("Good. It seems nothing was "
2193 "changed.\n");
2194 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002195 ret = 1;
2196 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002197 }
2198 }
hailfingerd217d122010-10-08 18:52:29 +00002199 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002200 ret = 1;
2201 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002202 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002203
David Hendricksac1d25c2016-08-09 17:00:58 -07002204 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002205 if (ret < 0) {
2206 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002207 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002208 emergency_help_message();
2209 ret = 1;
2210 goto out;
2211 } else if (ret > 0) {
2212 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002213 msg_pdbg("CROS_EC needs 2nd pass.\n");
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002214
David Hendrickse3451942013-03-21 17:23:29 -07002215 if (read_flash(flash, oldcontents, 0, size)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002216 msg_cerr("Uh oh. Cannot get latest content.\n");
2217 emergency_help_message();
2218 ret = 1;
2219 goto out;
2220 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002221
2222 /* Get a new descriptor. */
2223 free(descriptor);
2224 descriptor = prepare_action_descriptor(flash,
2225 oldcontents,
2226 newcontents,
2227 do_diff);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002228 // write 2nd pass
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002229 if (erase_and_write_flash(flash, descriptor)) {
David Hendricksb907de32014-08-11 16:47:09 -07002230 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002231 emergency_help_message();
2232 ret = 1;
2233 goto out;
2234 }
2235 ret = 0;
2236 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002237
David Hendricksac1d25c2016-08-09 17:00:58 -07002238 if (cros_ec_finish() < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002239 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002240 emergency_help_message();
2241 ret = 1;
2242 goto out;
2243 }
stuge8ce3a3c2008-04-28 14:47:30 +00002244 }
ollie6a600992005-11-26 21:55:36 +00002245
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002246 verify:
hailfinger0459e1c2009-08-19 13:55:34 +00002247 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002248 if ((write_it || erase_it) && !content_has_changed) {
2249 msg_gdbg("Nothing was erased or written, skipping "
2250 "verification\n");
2251 } else {
2252 /* Work around chips which need some time to calm down. */
2253 if (write_it && verify_it != VERIFY_PARTIAL)
2254 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002255
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002256 ret = verify_flash(flash, descriptor, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002257
David Hendricks9ba79fb2015-04-03 12:06:16 -07002258 /* If we tried to write, and verification now fails, we
2259 * might have an emergency situation.
2260 */
2261 if (ret && write_it)
2262 emergency_help_message();
2263 }
hailfinger0459e1c2009-08-19 13:55:34 +00002264 }
ollie6a600992005-11-26 21:55:36 +00002265
hailfinger90fcf9b2010-11-05 14:51:59 +00002266out:
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002267 if (descriptor)
2268 free(descriptor);
2269
hailfinger90fcf9b2010-11-05 14:51:59 +00002270 free(oldcontents);
2271 free(newcontents);
2272out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002273 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002274 /*
2275 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2276 * tree. This is because some operations, such as write protection,
2277 * requires programmer_shutdown() but does not call doit().
2278 */
2279// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002280 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002281}