blob: 0f496c4d864baa40ba4d01a572d177b5bd5c7c23 [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 *
rminnich8d3ff912003-10-25 17:01:29 +000019 */
20
hailfingera83a5fe2010-05-30 22:24:40 +000021#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000022#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000023#ifndef __LIBPAYLOAD__
24#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000026#endif
rminnich8d3ff912003-10-25 17:01:29 +000027#include <string.h>
28#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000029#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000030#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000031#if HAVE_UTSNAME == 1
32#include <sys/utsname.h>
33#endif
Vincent Palatin7ab23932014-10-01 12:09:16 -070034#include <unistd.h>
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -070035
36#include "action_descriptor.h"
rminnich8d3ff912003-10-25 17:01:29 +000037#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000038#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090039#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000040#include "programmer.h"
Duncan Laurie25a4ca22019-04-25 12:08:52 -070041#include "spi.h"
rminnich8d3ff912003-10-25 17:01:29 +000042
krause2eb76212011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000044char *chip_to_probe = NULL;
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110045enum flashrom_log_level verbose_screen = FLASHROM_MSG_ERROR;
46enum flashrom_log_level verbose_logfile = FLASHROM_MSG_DEBUG2;
hailfinger80422e22009-12-13 22:28:00 +000047
David Hendricks9ba79fb2015-04-03 12:06:16 -070048/* Set if any erase/write operation is to be done. This will be used to
49 * decide if final verification is needed. */
50static int content_has_changed = 0;
51
David Hendricks1ed1d352011-11-23 17:54:37 -080052/* error handling stuff */
53enum error_action access_denied_action = error_ignore;
54
55int ignore_error(int err) {
56 int rc = 0;
57
58 switch(err) {
59 case ACCESS_DENIED:
60 if (access_denied_action == error_ignore)
61 rc = 1;
62 break;
63 default:
64 break;
65 }
66
67 return rc;
68}
69
hailfinger969e2f32011-09-08 00:00:29 +000070static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000071
hailfingerddeb4ac2010-07-08 10:13:37 +000072static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000073
David Hendricksac1d25c2016-08-09 17:00:58 -070074/* Supported buses for the current programmer. */
75enum chipbustype buses_supported;
76
uwee15beb92010-08-08 17:01:18 +000077/*
hailfinger80422e22009-12-13 22:28:00 +000078 * Programmers supporting multiple buses can have differing size limits on
79 * each bus. Store the limits for each bus in a common struct.
80 */
hailfinger1ff33dc2010-07-03 11:02:10 +000081struct decode_sizes max_rom_decode;
82
83/* If nonzero, used as the start address of bottom-aligned flash. */
84unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000085
hailfinger5828baf2010-07-03 12:14:25 +000086/* Is writing allowed with this programmer? */
87int programmer_may_write;
88
hailfingerabe249e2009-05-08 17:43:22 +000089const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000090#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000091 {
hailfinger3548a9a2009-08-12 14:34:35 +000092 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000093 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000094 .map_flash_region = physmap,
95 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000096 .delay = internal_delay,
David Hendricks55cdd9c2015-11-25 14:37:26 -080097
98 /*
99 * "Internal" implies in-system programming on a live system, so
100 * handle with paranoia to catch errors early. If something goes
101 * wrong then hopefully the system will still be recoverable.
102 */
103 .paranoid = 1,
hailfingerabe249e2009-05-08 17:43:22 +0000104 },
hailfinger80422e22009-12-13 22:28:00 +0000105#endif
stepan927d4e22007-04-04 22:45:58 +0000106
hailfinger90c7d542010-05-31 15:27:27 +0000107#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000108 {
hailfinger3548a9a2009-08-12 14:34:35 +0000109 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000110 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000111 .map_flash_region = dummy_map,
112 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000113 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000114 },
hailfinger571a6b32009-09-16 10:09:21 +0000115#endif
hailfingera9df33c2009-05-09 00:54:55 +0000116
hailfinger90c7d542010-05-31 15:27:27 +0000117#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000118 {
hailfinger3548a9a2009-08-12 14:34:35 +0000119 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000120 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000121 .map_flash_region = fallback_map,
122 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000123 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000124 },
hailfinger571a6b32009-09-16 10:09:21 +0000125#endif
uwe0f5a3a22009-05-13 11:36:06 +0000126
hailfinger90c7d542010-05-31 15:27:27 +0000127#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000128 {
hailfinger0d703d42011-03-07 01:08:09 +0000129 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000130 .name = "nicrealtek",
131 //.name = "nicsmc1211",
132 .init = nicrealtek_init,
133 .map_flash_region = fallback_map,
134 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000135 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000136 },
hailfinger5aa36982010-05-21 21:54:07 +0000137#endif
138
hailfingerf0a368f2010-06-07 22:37:54 +0000139#if CONFIG_NICNATSEMI == 1
140 {
uwe8d342eb2011-07-28 08:13:25 +0000141 .name = "nicnatsemi",
142 .init = nicnatsemi_init,
143 .map_flash_region = fallback_map,
144 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000145 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000146 },
147#endif
hailfinger5aa36982010-05-21 21:54:07 +0000148
hailfinger90c7d542010-05-31 15:27:27 +0000149#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000150 {
151 .name = "gfxnvidia",
152 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000153 .map_flash_region = fallback_map,
154 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000155 .delay = internal_delay,
156 },
157#endif
158
hailfinger90c7d542010-05-31 15:27:27 +0000159#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000160 {
uwee2f95ef2009-09-02 23:00:46 +0000161 .name = "drkaiser",
162 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000163 .map_flash_region = fallback_map,
164 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000165 .delay = internal_delay,
166 },
hailfinger571a6b32009-09-16 10:09:21 +0000167#endif
uwee2f95ef2009-09-02 23:00:46 +0000168
hailfinger90c7d542010-05-31 15:27:27 +0000169#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000170 {
hailfinger3548a9a2009-08-12 14:34:35 +0000171 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000172 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000173 .map_flash_region = fallback_map,
174 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000175 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000176 },
hailfinger571a6b32009-09-16 10:09:21 +0000177#endif
ruikda922a12009-05-17 19:39:27 +0000178
hailfinger90c7d542010-05-31 15:27:27 +0000179#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000180 {
181 .name = "atahpt",
182 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000183 .map_flash_region = fallback_map,
184 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000185 .delay = internal_delay,
186 },
187#endif
188
hailfinger90c7d542010-05-31 15:27:27 +0000189#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000190 {
hailfinger90c7d542010-05-31 15:27:27 +0000191 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000192 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000193 .map_flash_region = fallback_map,
194 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000195 .delay = internal_delay,
196 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000197#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000198
hailfinger90c7d542010-05-31 15:27:27 +0000199#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000200 {
hailfinger3548a9a2009-08-12 14:34:35 +0000201 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000202 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000203 .map_flash_region = fallback_map,
204 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000205 .delay = serprog_delay,
206 },
hailfinger74d88a72009-08-12 16:17:41 +0000207#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000208
hailfinger90c7d542010-05-31 15:27:27 +0000209#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000210 {
hailfinger90c7d542010-05-31 15:27:27 +0000211 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000212 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000213 .map_flash_region = fallback_map,
214 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000215 .delay = internal_delay,
216 },
217#endif
218
Anton Staafb2647882014-09-17 15:13:43 -0700219#if CONFIG_RAIDEN_DEBUG_SPI == 1
220 {
221 .name = "raiden_debug_spi",
222 .init = raiden_debug_spi_init,
223 .map_flash_region = fallback_map,
224 .unmap_flash_region = fallback_unmap,
225 .delay = internal_delay,
226 },
227#endif
228
hailfinger90c7d542010-05-31 15:27:27 +0000229#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000230 {
231 .name = "dediprog",
232 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000233 .map_flash_region = fallback_map,
234 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000235 .delay = internal_delay,
236 },
237#endif
238
hailfinger52c4fa02010-07-21 10:26:01 +0000239#if CONFIG_RAYER_SPI == 1
240 {
241 .name = "rayer_spi",
242 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000243 .map_flash_region = fallback_map,
244 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000245 .delay = internal_delay,
246 },
247#endif
248
hailfinger7949b652011-05-08 00:24:18 +0000249#if CONFIG_NICINTEL == 1
250 {
251 .name = "nicintel",
252 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000253 .map_flash_region = fallback_map,
254 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000255 .delay = internal_delay,
256 },
257#endif
258
uwe6764e922010-09-03 18:21:21 +0000259#if CONFIG_NICINTEL_SPI == 1
260 {
uwe8d342eb2011-07-28 08:13:25 +0000261 .name = "nicintel_spi",
262 .init = nicintel_spi_init,
263 .map_flash_region = fallback_map,
264 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000265 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000266 },
267#endif
268
hailfingerfb1f31f2010-12-03 14:48:11 +0000269#if CONFIG_OGP_SPI == 1
270 {
uwe8d342eb2011-07-28 08:13:25 +0000271 .name = "ogp_spi",
272 .init = ogp_spi_init,
273 .map_flash_region = fallback_map,
274 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000275 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000276 },
277#endif
278
hailfinger935365d2011-02-04 21:37:59 +0000279#if CONFIG_SATAMV == 1
280 {
281 .name = "satamv",
282 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000283 .map_flash_region = fallback_map,
284 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000285 .delay = internal_delay,
286 },
287#endif
288
David Hendrickscebee892015-05-23 20:30:30 -0700289#if CONFIG_LINUX_MTD == 1
290 {
291 .name = "linux_mtd",
292 .init = linux_mtd_init,
293 .map_flash_region = fallback_map,
294 .unmap_flash_region = fallback_unmap,
295 .delay = internal_delay,
296 },
297#endif
298
uwe7df6dda2011-09-03 18:37:52 +0000299#if CONFIG_LINUX_SPI == 1
300 {
301 .name = "linux_spi",
302 .init = linux_spi_init,
303 .map_flash_region = fallback_map,
304 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000305 .delay = internal_delay,
306 },
307#endif
308
Patrick Georgi8ddfee92017-03-20 14:54:28 +0100309 {0}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000310};
stepan927d4e22007-04-04 22:45:58 +0000311
David Hendricksbf36f092010-11-02 23:39:29 -0700312#define CHIP_RESTORE_MAXFN 4
313static int chip_restore_fn_count = 0;
314struct chip_restore_func_data {
315 CHIP_RESTORE_CALLBACK;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700316 struct flashctx *flash;
David Hendricksbf36f092010-11-02 23:39:29 -0700317 uint8_t status;
318} static chip_restore_fn[CHIP_RESTORE_MAXFN];
319
David Hendricks668f29d2011-01-27 18:51:45 -0800320
hailfingerf31cbdc2010-11-10 15:25:18 +0000321#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000322static int shutdown_fn_count = 0;
323struct shutdown_func_data {
David Hendricks93784b42016-08-09 17:00:38 -0700324 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000325 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000326} static shutdown_fn[SHUTDOWN_MAXFN];
327/* Initialize to 0 to make sure nobody registers a shutdown function before
328 * programmer init.
329 */
330static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000331
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700332static int check_block_eraser(const struct flashctx *flash, int k, int log);
stefanct569dbb62011-07-01 00:19:12 +0000333
hailfingerdc6f7972010-02-14 01:20:28 +0000334/* Register a function to be executed on programmer shutdown.
335 * The advantage over atexit() is that you can supply a void pointer which will
336 * be used as parameter to the registered function upon programmer shutdown.
337 * This pointer can point to arbitrary data used by said function, e.g. undo
338 * information for GPIO settings etc. If unneeded, set data=NULL.
339 * Please note that the first (void *data) belongs to the function signature of
340 * the function passed as first parameter.
341 */
David Hendricks93784b42016-08-09 17:00:38 -0700342int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000343{
344 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000345 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000346 SHUTDOWN_MAXFN);
347 return 1;
348 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000349 if (!may_register_shutdown) {
350 msg_perr("Tried to register a shutdown function before "
351 "programmer init.\n");
352 return 1;
353 }
hailfingerdc6f7972010-02-14 01:20:28 +0000354 shutdown_fn[shutdown_fn_count].func = function;
355 shutdown_fn[shutdown_fn_count].data = data;
356 shutdown_fn_count++;
357
358 return 0;
359}
360
David Hendricksbf36f092010-11-02 23:39:29 -0700361//int register_chip_restore(int (*function) (void *data), void *data)
362int register_chip_restore(CHIP_RESTORE_CALLBACK,
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700363 struct flashctx *flash, uint8_t status)
David Hendricksbf36f092010-11-02 23:39:29 -0700364{
365 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
366 msg_perr("Tried to register more than %i chip restore"
367 " functions.\n", CHIP_RESTORE_MAXFN);
368 return 1;
369 }
370 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
371 chip_restore_fn[chip_restore_fn_count].flash = flash;
372 chip_restore_fn[chip_restore_fn_count].status = status;
373 chip_restore_fn_count++;
374
375 return 0;
376}
377
David Hendricksac1d25c2016-08-09 17:00:58 -0700378int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000379{
hailfinger1ef766d2010-07-06 09:55:48 +0000380 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000381
382 if (prog >= PROGRAMMER_INVALID) {
383 msg_perr("Invalid programmer specified!\n");
384 return -1;
385 }
386 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000387 /* Initialize all programmer specific data. */
388 /* Default to unlimited decode sizes. */
389 max_rom_decode = (const struct decode_sizes) {
390 .parallel = 0xffffffff,
391 .lpc = 0xffffffff,
392 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000393 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000394 };
David Hendricksac1d25c2016-08-09 17:00:58 -0700395 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000396 /* Default to top aligned flash at 4 GB. */
397 flashbase = 0;
398 /* Registering shutdown functions is now allowed. */
399 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000400 /* Default to allowing writes. Broken programmers set this to 0. */
401 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000402
403 programmer_param = param;
404 msg_pdbg("Initializing %s programmer\n",
405 programmer_table[programmer].name);
David Hendricksac1d25c2016-08-09 17:00:58 -0700406 ret = programmer_table[programmer].init();
hailfinger1ef766d2010-07-06 09:55:48 +0000407 return ret;
uweabe92a52009-05-16 22:36:00 +0000408}
409
David Hendricksbf36f092010-11-02 23:39:29 -0700410int chip_restore()
411{
412 int rc = 0;
413
414 while (chip_restore_fn_count > 0) {
415 int i = --chip_restore_fn_count;
416 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
417 chip_restore_fn[i].status);
418 }
419
420 return rc;
421}
422
David Hendricks93784b42016-08-09 17:00:38 -0700423int programmer_shutdown(void)
uweabe92a52009-05-16 22:36:00 +0000424{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000425 int ret = 0;
426
hailfinger1ff33dc2010-07-03 11:02:10 +0000427 /* Registering shutdown functions is no longer allowed. */
428 may_register_shutdown = 0;
429 while (shutdown_fn_count > 0) {
430 int i = --shutdown_fn_count;
David Hendricks93784b42016-08-09 17:00:38 -0700431 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000432 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000433 return ret;
uweabe92a52009-05-16 22:36:00 +0000434}
435
436void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
437 size_t len)
438{
439 return programmer_table[programmer].map_flash_region(descr,
440 phys_addr, len);
441}
442
443void programmer_unmap_flash_region(void *virt_addr, size_t len)
444{
445 programmer_table[programmer].unmap_flash_region(virt_addr, len);
446}
447
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700448void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000449{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100450 par_master->chip_writeb(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000451}
452
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700453void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000454{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100455 par_master->chip_writew(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000456}
457
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700458void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000459{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100460 par_master->chip_writel(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000461}
462
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700463void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000464{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100465 par_master->chip_writen(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000466}
467
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700468uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000469{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100470 return par_master->chip_readb(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000471}
472
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700473uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000474{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100475 return par_master->chip_readw(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000476}
477
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700478uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000479{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100480 return par_master->chip_readl(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000481}
482
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700483void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000484{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100485 par_master->chip_readn(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000486}
487
hailfingere5829f62009-06-05 17:48:08 +0000488void programmer_delay(int usecs)
489{
490 programmer_table[programmer].delay(usecs);
491}
492
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700493void map_flash_registers(struct flashctx *flash)
stepan15e64bc2007-05-24 08:48:10 +0000494{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100495 size_t size = flash->chip->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000496 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000497 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000498 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000499}
500
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700501int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000502{
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700503 chip_readn(flash, buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000504
hailfinger23060112009-05-08 12:49:03 +0000505 return 0;
506}
507
David Hendricksda18f692016-10-21 17:49:49 -0700508/* This is a somewhat hacked function similar in some ways to strtok(). It will
509 * look for needle with a subsequent '=' in haystack, return a copy of needle.
hailfinger6e5a52a2009-11-24 18:27:10 +0000510 */
stefanct52700282011-06-26 17:38:17 +0000511char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000512{
David Hendricksda18f692016-10-21 17:49:49 -0700513 char *param_pos, *opt_pos;
hailfinger1ef766d2010-07-06 09:55:48 +0000514 char *opt = NULL;
515 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000516 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000517
hailfingerf4aaccc2010-04-28 15:22:14 +0000518 needlelen = strlen(needle);
519 if (!needlelen) {
520 msg_gerr("%s: empty needle! Please report a bug at "
521 "flashrom@flashrom.org\n", __func__);
522 return NULL;
523 }
524 /* No programmer parameters given. */
525 if (*haystack == NULL)
526 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000527 param_pos = strstr(*haystack, needle);
528 do {
529 if (!param_pos)
530 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000531 /* Needle followed by '='? */
532 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600533
hailfinger1ef766d2010-07-06 09:55:48 +0000534 /* Beginning of the string? */
535 if (param_pos == *haystack)
536 break;
537 /* After a delimiter? */
538 if (strchr(delim, *(param_pos - 1)))
539 break;
540 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000541 /* Continue searching. */
542 param_pos++;
543 param_pos = strstr(param_pos, needle);
544 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000545
hailfinger6e5a52a2009-11-24 18:27:10 +0000546 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000547 /* Get the string after needle and '='. */
548 opt_pos = param_pos + needlelen + 1;
549 optlen = strcspn(opt_pos, delim);
550 /* Return an empty string if the parameter was empty. */
551 opt = malloc(optlen + 1);
552 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000553 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000554 exit(1);
555 }
hailfinger1ef766d2010-07-06 09:55:48 +0000556 strncpy(opt, opt_pos, optlen);
557 opt[optlen] = '\0';
hailfinger6e5a52a2009-11-24 18:27:10 +0000558 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000559
hailfinger1ef766d2010-07-06 09:55:48 +0000560 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000561}
562
stefanct52700282011-06-26 17:38:17 +0000563char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000564{
565 return extract_param(&programmer_param, param_name, ",");
566}
567
stefancte1c5acf2011-07-04 07:27:17 +0000568/* Returns the number of well-defined erasers for a chip. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700569static unsigned int count_usable_erasers(const struct flashctx *flash)
stefanct569dbb62011-07-01 00:19:12 +0000570{
571 unsigned int usable_erasefunctions = 0;
572 int k;
573 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
574 if (!check_block_eraser(flash, k, 0))
575 usable_erasefunctions++;
576 }
577 return usable_erasefunctions;
578}
579
hailfinger7af83692009-06-15 17:23:36 +0000580/* start is an offset to the base address of the flash chip */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700581int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000582{
583 int ret;
584 uint8_t *cmpbuf = malloc(len);
585
586 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000587 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000588 exit(1);
589 }
Simon Glass4c214132013-07-16 10:09:28 -0600590 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000591 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
592 free(cmpbuf);
593 return ret;
594}
595
Simon Glass4e305f42015-01-08 06:29:04 -0700596static int compare_chunk(uint8_t *readbuf, uint8_t *cmpbuf, unsigned int start,
597 unsigned int len, const char *message)
598{
599 int failcount = 0, i;
600
601 for (i = 0; i < len; i++) {
602 if (cmpbuf[i] != readbuf[i]) {
603 if (!failcount) {
604 msg_cerr("%s FAILED at 0x%08x! "
605 "Expected=0x%02x, Read=0x%02x,",
606 message, start + i,
607 cmpbuf[i], readbuf[i]);
608 }
609 failcount++;
610 }
611 }
612
613 return failcount;
614}
615
uwee15beb92010-08-08 17:01:18 +0000616/*
hailfinger7af3d192009-11-25 17:05:52 +0000617 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000618 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000619 * @start offset to the base address of the flash chip
620 * @len length of the verified area
621 * @message string to print in the "FAILED" message
622 * @return 0 for success, -1 for failure
623 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700624int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000625 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000626{
hailfinger8cb6ece2010-11-16 17:21:58 +0000627 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000628 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000629
630 if (!len)
631 goto out_free;
632
Patrick Georgif3fa2992017-02-02 16:24:44 +0100633 if (!flash->chip->read) {
snelsone42c3802010-05-07 20:09:04 +0000634 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000635 return 1;
636 }
hailfinger7af83692009-06-15 17:23:36 +0000637 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000638 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000639 exit(1);
640 }
641
Patrick Georgif3fa2992017-02-02 16:24:44 +0100642 if (start + len > flash->chip->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000643 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000644 " total_size 0x%x\n", __func__, start, len,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100645 flash->chip->total_size * 1024);
hailfinger7af83692009-06-15 17:23:36 +0000646 ret = -1;
647 goto out_free;
648 }
649 if (!message)
650 message = "VERIFY";
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -0700651 msg_gdbg("%#06x..%#06x ", start, start + len -1);
Simon Glass4e305f42015-01-08 06:29:04 -0700652 if (programmer_table[programmer].paranoid) {
653 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800654
Simon Glass4e305f42015-01-08 06:29:04 -0700655 /* limit chunksize in order to catch errors early */
656 for (i = 0, chunksize = 0; i < len; i += chunksize) {
657 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800658
Patrick Georgif3fa2992017-02-02 16:24:44 +0100659 chunksize = min(flash->chip->page_size, len - i);
660 tmp = flash->chip->read(flash, readbuf + i, start + i, chunksize);
Simon Glass4e305f42015-01-08 06:29:04 -0700661 if (tmp) {
662 ret = tmp;
663 if (ignore_error(tmp))
664 continue;
665 else
666 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800667 }
Simon Glass4e305f42015-01-08 06:29:04 -0700668
Duncan Laurie25a4ca22019-04-25 12:08:52 -0700669 /*
670 * Check write access permission and do not compare chunks
671 * where flashrom does not have write access to the region.
672 */
673 if (flash->chip->check_access) {
674 tmp = flash->chip->check_access(flash, start + i, chunksize, 0);
675 if (tmp && ignore_error(tmp))
676 continue;
677 }
678
Simon Glass4e305f42015-01-08 06:29:04 -0700679 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
680 chunksize, message);
681 if (failcount)
682 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800683 }
Simon Glass4e305f42015-01-08 06:29:04 -0700684 } else {
685 int tmp;
686
687 /* read as much as we can to reduce transaction overhead */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100688 tmp = flash->chip->read(flash, readbuf, start, len);
Simon Glass4e305f42015-01-08 06:29:04 -0700689 if (tmp && !ignore_error(tmp)) {
690 ret = tmp;
691 goto out_free;
692 }
693
694 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000695 }
696
hailfinger5be6c0f2009-07-23 01:42:56 +0000697 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000698 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000699 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000700 ret = -1;
701 }
hailfinger7af83692009-06-15 17:23:36 +0000702
703out_free:
704 free(readbuf);
705 return ret;
706}
707
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100708/* Helper function for need_erase() that focuses on granularities of gran bytes. */
709static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
710 unsigned int gran)
711{
712 unsigned int i, j, limit;
713 for (j = 0; j < len / gran; j++) {
714 limit = min (gran, len - j * gran);
715 /* Are 'have' and 'want' identical? */
716 if (!memcmp(have + j * gran, want + j * gran, limit))
717 continue;
718 /* have needs to be in erased state. */
719 for (i = 0; i < limit; i++)
720 if (have[j * gran + i] != 0xff)
721 return 1;
722 }
723 return 0;
724}
725
uwee15beb92010-08-08 17:01:18 +0000726/*
hailfingerb247c7a2010-03-08 00:42:32 +0000727 * Check if the buffer @have can be programmed to the content of @want without
728 * erasing. This is only possible if all chunks of size @gran are either kept
729 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000730 *
hailfingerb247c7a2010-03-08 00:42:32 +0000731 * The following write granularities (enum @gran) are known:
732 * - 1 bit. Each bit can be cleared individually.
733 * - 1 byte. A byte can be written once. Further writes to an already written
734 * byte cause the contents to be either undefined or to stay unchanged.
735 * - 128 bytes. If less than 128 bytes are written, the rest will be
736 * erased. Each write to a 128-byte region will trigger an automatic erase
737 * before anything is written. Very uncommon behaviour and unsupported by
738 * this function.
739 * - 256 bytes. If less than 256 bytes are written, the contents of the
740 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000741 * Warning: This function assumes that @have and @want point to naturally
742 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000743 *
744 * @have buffer with current content
745 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000746 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000747 * @gran write granularity (enum, not count)
748 * @return 0 if no erase is needed, 1 otherwise
749 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700750static int need_erase(struct flashctx *flash, uint8_t *have, uint8_t *want,
Simon Glass4c214132013-07-16 10:09:28 -0600751 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000752{
hailfingerb91c08c2011-08-15 19:54:20 +0000753 int result = 0;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100754 unsigned int i;
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700755
hailfingerb247c7a2010-03-08 00:42:32 +0000756 switch (gran) {
757 case write_gran_1bit:
758 for (i = 0; i < len; i++)
759 if ((have[i] & want[i]) != want[i]) {
760 result = 1;
761 break;
762 }
763 break;
764 case write_gran_1byte:
765 for (i = 0; i < len; i++)
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100766 if ((have[i] != want[i]) && (have[i] != 0xff)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000767 result = 1;
768 break;
769 }
770 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100771 case write_gran_128bytes:
772 result = need_erase_gran_bytes(have, want, len, 128);
773 break;
hailfingerb247c7a2010-03-08 00:42:32 +0000774 case write_gran_256bytes:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100775 result = need_erase_gran_bytes(have, want, len, 256);
776 break;
777 case write_gran_264bytes:
778 result = need_erase_gran_bytes(have, want, len, 264);
779 break;
780 case write_gran_512bytes:
781 result = need_erase_gran_bytes(have, want, len, 512);
782 break;
783 case write_gran_528bytes:
784 result = need_erase_gran_bytes(have, want, len, 528);
785 break;
786 case write_gran_1024bytes:
787 result = need_erase_gran_bytes(have, want, len, 1024);
788 break;
789 case write_gran_1056bytes:
790 result = need_erase_gran_bytes(have, want, len, 1056);
791 break;
792 case write_gran_1byte_implicit_erase:
793 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
794 result = 0;
hailfingerb247c7a2010-03-08 00:42:32 +0000795 break;
hailfingerb437e282010-11-04 01:04:27 +0000796 default:
797 msg_cerr("%s: Unsupported granularity! Please report a bug at "
798 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000799 }
800 return result;
801}
802
hailfingerb437e282010-11-04 01:04:27 +0000803/**
804 * Check if the buffer @have needs to be programmed to get the content of @want.
805 * If yes, return 1 and fill in first_start with the start address of the
806 * write operation and first_len with the length of the first to-be-written
807 * chunk. If not, return 0 and leave first_start and first_len undefined.
808 *
809 * Warning: This function assumes that @have and @want point to naturally
810 * aligned regions.
811 *
812 * @have buffer with current content
813 * @want buffer with desired content
814 * @len length of the checked area
815 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000816 * @first_start offset of the first byte which needs to be written (passed in
817 * value is increased by the offset of the first needed write
818 * relative to have/want or unchanged if no write is needed)
819 * @return length of the first contiguous area which needs to be written
820 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000821 *
822 * FIXME: This function needs a parameter which tells it about coalescing
823 * in relation to the max write length of the programmer and the max write
824 * length of the chip.
825 */
stefanctc5eb8a92011-11-23 09:13:48 +0000826static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
827 unsigned int *first_start,
828 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000829{
stefanctc5eb8a92011-11-23 09:13:48 +0000830 int need_write = 0;
831 unsigned int rel_start = 0, first_len = 0;
832 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000833
hailfingerb437e282010-11-04 01:04:27 +0000834 switch (gran) {
835 case write_gran_1bit:
836 case write_gran_1byte:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100837 case write_gran_1byte_implicit_erase:
hailfinger90fcf9b2010-11-05 14:51:59 +0000838 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000839 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100840 case write_gran_128bytes:
841 stride = 128;
842 break;
hailfingerb437e282010-11-04 01:04:27 +0000843 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000844 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000845 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100846 case write_gran_264bytes:
847 stride = 264;
848 break;
849 case write_gran_512bytes:
850 stride = 512;
851 break;
852 case write_gran_528bytes:
853 stride = 528;
854 break;
855 case write_gran_1024bytes:
856 stride = 1024;
857 break;
858 case write_gran_1056bytes:
859 stride = 1056;
860 break;
hailfingerb437e282010-11-04 01:04:27 +0000861 default:
862 msg_cerr("%s: Unsupported granularity! Please report a bug at "
863 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000864 /* Claim that no write was needed. A write with unknown
865 * granularity is too dangerous to try.
866 */
867 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000868 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000869 for (i = 0; i < len / stride; i++) {
870 limit = min(stride, len - i * stride);
871 /* Are 'have' and 'want' identical? */
872 if (memcmp(have + i * stride, want + i * stride, limit)) {
873 if (!need_write) {
874 /* First location where have and want differ. */
875 need_write = 1;
876 rel_start = i * stride;
877 }
878 } else {
879 if (need_write) {
880 /* First location where have and want
881 * do not differ anymore.
882 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000883 break;
884 }
885 }
886 }
hailfingerffb7f382010-12-06 13:05:44 +0000887 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000888 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000889 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000890 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000891}
892
hailfinger0c515352009-11-23 12:55:31 +0000893/* This function generates various test patterns useful for testing controller
894 * and chip communication as well as chip behaviour.
895 *
896 * If a byte can be written multiple times, each time keeping 0-bits at 0
897 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
898 * is essentially an AND operation. That's also the reason why this function
899 * provides the result of AND between various patterns.
900 *
901 * Below is a list of patterns (and their block length).
902 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
903 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
904 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
905 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
906 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
907 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
908 * Pattern 6 is 00 (1 Byte)
909 * Pattern 7 is ff (1 Byte)
910 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
911 * byte block.
912 *
913 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
914 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
915 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
916 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
917 * Pattern 12 is 00 (1 Byte)
918 * Pattern 13 is ff (1 Byte)
919 * Patterns 8-13 have no block number.
920 *
921 * Patterns 0-3 are created to detect and efficiently diagnose communication
922 * slips like missed bits or bytes and their repetitive nature gives good visual
923 * cues to the person inspecting the results. In addition, the following holds:
924 * AND Pattern 0/1 == Pattern 4
925 * AND Pattern 2/3 == Pattern 5
926 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
927 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
928 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
929 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
930 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
931 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
932 * Besides that, they provide for bit testing of the last two bytes of every
933 * 256 byte block which contains the block number for patterns 0-6.
934 * Patterns 10-11 are special purpose for detecting subblock aliasing with
935 * block sizes >256 bytes (some Dataflash chips etc.)
936 * AND Pattern 8/9 == Pattern 12
937 * AND Pattern 10/11 == Pattern 12
938 * Pattern 13 is the completely erased state.
939 * None of the patterns can detect aliasing at boundaries which are a multiple
940 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
941 */
942int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
943{
944 int i;
945
946 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000947 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000948 return 1;
949 }
950
951 switch (variant) {
952 case 0:
953 for (i = 0; i < size; i++)
954 buf[i] = (i & 0xf) << 4 | 0x5;
955 break;
956 case 1:
957 for (i = 0; i < size; i++)
958 buf[i] = (i & 0xf) << 4 | 0xa;
959 break;
960 case 2:
961 for (i = 0; i < size; i++)
962 buf[i] = 0x50 | (i & 0xf);
963 break;
964 case 3:
965 for (i = 0; i < size; i++)
966 buf[i] = 0xa0 | (i & 0xf);
967 break;
968 case 4:
969 for (i = 0; i < size; i++)
970 buf[i] = (i & 0xf) << 4;
971 break;
972 case 5:
973 for (i = 0; i < size; i++)
974 buf[i] = i & 0xf;
975 break;
976 case 6:
977 memset(buf, 0x00, size);
978 break;
979 case 7:
980 memset(buf, 0xff, size);
981 break;
982 case 8:
983 for (i = 0; i < size; i++)
984 buf[i] = i & 0xff;
985 break;
986 case 9:
987 for (i = 0; i < size; i++)
988 buf[i] = ~(i & 0xff);
989 break;
990 case 10:
991 for (i = 0; i < size % 2; i++) {
992 buf[i * 2] = (i >> 8) & 0xff;
993 buf[i * 2 + 1] = i & 0xff;
994 }
995 if (size & 0x1)
996 buf[i * 2] = (i >> 8) & 0xff;
997 break;
998 case 11:
999 for (i = 0; i < size % 2; i++) {
1000 buf[i * 2] = ~((i >> 8) & 0xff);
1001 buf[i * 2 + 1] = ~(i & 0xff);
1002 }
1003 if (size & 0x1)
1004 buf[i * 2] = ~((i >> 8) & 0xff);
1005 break;
1006 case 12:
1007 memset(buf, 0x00, size);
1008 break;
1009 case 13:
1010 memset(buf, 0xff, size);
1011 break;
1012 }
1013
1014 if ((variant >= 0) && (variant <= 7)) {
1015 /* Write block number in the last two bytes of each 256-byte
1016 * block, big endian for easier reading of the hexdump.
1017 * Note that this wraps around for chips larger than 2^24 bytes
1018 * (16 MB).
1019 */
1020 for (i = 0; i < size / 256; i++) {
1021 buf[i * 256 + 254] = (i >> 8) & 0xff;
1022 buf[i * 256 + 255] = i & 0xff;
1023 }
1024 }
1025
1026 return 0;
1027}
1028
hailfingeraec9c962009-10-31 01:53:09 +00001029int check_max_decode(enum chipbustype buses, uint32_t size)
1030{
1031 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001032
1033 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001034 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001035 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001036 "size %u kB of chipset/board/programmer "
1037 "for %s interface, "
1038 "probe/read/erase/write may fail. ", size / 1024,
1039 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001040 }
hailfingere1e41ea2011-07-27 07:13:06 +00001041 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001042 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001043 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001044 "size %u kB of chipset/board/programmer "
1045 "for %s interface, "
1046 "probe/read/erase/write may fail. ", size / 1024,
1047 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001048 }
hailfingere1e41ea2011-07-27 07:13:06 +00001049 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001050 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001051 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001052 "size %u kB of chipset/board/programmer "
1053 "for %s interface, "
1054 "probe/read/erase/write may fail. ", size / 1024,
1055 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001056 }
hailfingere1e41ea2011-07-27 07:13:06 +00001057 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001058 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001059 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001060 "size %u kB of chipset/board/programmer "
1061 "for %s interface, "
1062 "probe/read/erase/write may fail. ", size / 1024,
1063 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001064 }
1065 if (!limitexceeded)
1066 return 0;
1067 /* Sometimes chip and programmer have more than one bus in common,
1068 * and the limit is not exceeded on all buses. Tell the user.
1069 */
1070 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001071 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001072 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001073 "interface which can support a chip of this size. "
1074 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001075 return 1;
1076}
1077
David Hendricksac1d25c2016-08-09 17:00:58 -07001078int probe_flash(int startchip, struct flashctx *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001079{
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301080 const struct flashchip *flash, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001081 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001082 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001083 uint32_t size;
1084 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001085 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001086
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301087 /* Based on the host controller interface that a platform
1088 * needs to use (hwseq or swseq),
1089 * set the flashchips list here.
1090 */
1091 switch (ich_generation) {
1092 case CHIPSET_100_SERIES_SUNRISE_POINT:
Furquan Shaikh44088752016-07-11 22:48:08 -07001093 case CHIPSET_APL:
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301094 flash_list = flashchips_hwseq;
1095 break;
1096 default:
1097 flash_list = flashchips;
1098 break;
1099 }
1100
1101 for (flash = flash_list + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001102 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001103 continue;
David Hendricksac1d25c2016-08-09 17:00:58 -07001104 buses_common = buses_supported & flash->bustype;
1105 if (!buses_common) {
1106 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1107 flash->vendor, flash->name, flash->total_size);
1108 tmp = flashbuses_to_text(buses_supported);
1109 msg_gspew("Host bus type %s ", tmp);
1110 free(tmp);
1111 tmp = flashbuses_to_text(flash->bustype);
1112 msg_gspew("and chip bus type %s are incompatible.",
1113 tmp);
1114 free(tmp);
1115 msg_gspew("\n");
hailfinger18bd4cc2011-06-17 22:38:53 +00001116 continue;
David Hendricksac1d25c2016-08-09 17:00:58 -07001117 }
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001118 /* Only probe for SPI25 chips by default. */
1119 if (flash->bustype == BUS_SPI && !chip_to_probe && flash->spi_cmd_set != SPI25)
1120 continue;
hailfinger18bd4cc2011-06-17 22:38:53 +00001121 msg_gdbg("Probing for %s %s, %d kB: ",
1122 flash->vendor, flash->name, flash->total_size);
1123 if (!flash->probe && !force) {
1124 msg_gdbg("failed! flashrom has no probe function for "
1125 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001126 continue;
1127 }
stepan782fb172007-04-06 11:58:03 +00001128
ollie5672ac62004-03-17 22:22:08 +00001129 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001130 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001131
hailfinger48ed3e22011-05-04 00:39:50 +00001132 /* Start filling in the dynamic data. */
Patrick Georgif3fa2992017-02-02 16:24:44 +01001133 fill_flash->chip = calloc(1, sizeof(struct flashchip));
1134 if (!fill_flash->chip) {
1135 msg_gerr("Out of memory!\n");
1136 exit(1);
1137 }
1138 memcpy(fill_flash->chip, flash, sizeof(struct flashchip));
hailfinger48ed3e22011-05-04 00:39:50 +00001139
hailfinger72d3b982009-05-09 07:27:23 +00001140 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001141 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001142
stugec1e55fe2008-07-02 17:15:47 +00001143 if (force)
1144 break;
stepanc98b80b2006-03-16 16:57:41 +00001145
Patrick Georgif3fa2992017-02-02 16:24:44 +01001146 if (fill_flash->chip->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001147 goto notfound;
1148
hailfinger48ed3e22011-05-04 00:39:50 +00001149 /* If this is the first chip found, accept it.
1150 * If this is not the first chip found, accept it only if it is
1151 * a non-generic match.
1152 * We could either make chipcount global or provide it as
1153 * parameter, or we assume that startchip==0 means this call to
1154 * probe_flash() is the first one and thus no chip has been
1155 * found before.
1156 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01001157 if (startchip == 0 || fill_flash->chip->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001158 break;
1159
stuge56300c32008-09-03 23:10:05 +00001160notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001161 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
Patrick Georgif3fa2992017-02-02 16:24:44 +01001162 free(fill_flash->chip);
1163 fill_flash->chip = NULL;
rminnich8d3ff912003-10-25 17:01:29 +00001164 }
uwebe4477b2007-08-23 16:08:21 +00001165
stugec1e55fe2008-07-02 17:15:47 +00001166 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001167 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001168
hailfingere11396b2011-03-08 00:09:11 +00001169#if CONFIG_INTERNAL == 1
1170 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001171 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001172 else
1173#endif
stepan3e7aeae2011-01-19 06:21:54 +00001174 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001175
stefanct588b6d22011-06-26 20:45:35 +00001176 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001177 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001178 force ? "Assuming" : "Found", fill_flash->chip->vendor,
1179 fill_flash->chip->name, fill_flash->chip->total_size, tmp,
1180 location);
stefanct588b6d22011-06-26 20:45:35 +00001181 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001182
hailfinger0f4c3952010-12-02 21:59:42 +00001183 /* Flash registers will not be mapped if the chip was forced. Lock info
1184 * may be stored in registers, so avoid lock info printing.
1185 */
1186 if (!force)
Patrick Georgif3fa2992017-02-02 16:24:44 +01001187 if (fill_flash->chip->printlock)
1188 fill_flash->chip->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001189
hailfinger48ed3e22011-05-04 00:39:50 +00001190 /* Return position of matching chip. */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301191 return flash - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001192}
1193
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001194static int verify_flash(struct flashctx *flash,
1195 struct action_descriptor *descriptor,
1196 int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001197{
hailfingerb0f4d122009-06-24 08:20:45 +00001198 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +01001199 unsigned int total_size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001200 uint8_t *buf = descriptor->newcontents;
rminnich8d3ff912003-10-25 17:01:29 +00001201
snelsone42c3802010-05-07 20:09:04 +00001202 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001203
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001204 if (verify_it == VERIFY_PARTIAL) {
1205 struct processing_unit *pu = descriptor->processing_units;
1206
1207 /* Verify only areas which were written. */
1208 while (pu->num_blocks) {
1209 ret = verify_range(flash, buf + pu->offset, pu->offset,
1210 pu->block_size * pu->num_blocks,
1211 NULL);
1212 if (ret)
1213 break;
1214 pu++;
1215 }
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001216 } else {
1217 ret = verify_range(flash, buf, 0, total_size, NULL);
1218 }
uwef6641642007-05-09 10:17:44 +00001219
David Hendricks1ed1d352011-11-23 17:54:37 -08001220 if (ret == ACCESS_DENIED) {
1221 msg_gdbg("Could not fully verify due to access error, ");
1222 if (access_denied_action == error_ignore) {
1223 msg_gdbg("ignoring\n");
1224 ret = 0;
1225 } else {
1226 msg_gdbg("aborting\n");
1227 }
1228 }
1229
hailfingerb0f4d122009-06-24 08:20:45 +00001230 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001231 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001232
hailfingerb0f4d122009-06-24 08:20:45 +00001233 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001234}
1235
uwe8d342eb2011-07-28 08:13:25 +00001236int read_buf_from_file(unsigned char *buf, unsigned long size,
1237 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001238{
1239 unsigned long numbytes;
1240 FILE *image;
1241 struct stat image_stat;
1242
Vincent Palatin7ab23932014-10-01 12:09:16 -07001243 if (!strncmp(filename, "-", sizeof("-")))
1244 image = fdopen(STDIN_FILENO, "rb");
1245 else
1246 image = fopen(filename, "rb");
1247 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001248 perror(filename);
1249 return 1;
1250 }
1251 if (fstat(fileno(image), &image_stat) != 0) {
1252 perror(filename);
1253 fclose(image);
1254 return 1;
1255 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001256 if ((image_stat.st_size != size) &&
1257 (strncmp(filename, "-", sizeof("-")))) {
Mike Frysinger62c794d2017-05-29 12:02:45 -04001258 msg_gerr("Error: Image size doesn't match: stat %jd bytes, "
1259 "wanted %ld!\n", (intmax_t)image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001260 fclose(image);
1261 return 1;
1262 }
1263 numbytes = fread(buf, 1, size, image);
1264 if (fclose(image)) {
1265 perror(filename);
1266 return 1;
1267 }
1268 if (numbytes != size) {
1269 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1270 "wanted %ld!\n", numbytes, size);
1271 return 1;
1272 }
1273 return 0;
1274}
1275
uwe8d342eb2011-07-28 08:13:25 +00001276int write_buf_to_file(unsigned char *buf, unsigned long size,
1277 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001278{
1279 unsigned long numbytes;
1280 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001281
1282 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001283 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001284 return 1;
1285 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001286 if (!strncmp(filename, "-", sizeof("-")))
1287 image = fdopen(STDOUT_FILENO, "wb");
1288 else
1289 image = fopen(filename, "wb");
1290 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001291 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001292 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001293 }
hailfingerd219a232009-01-28 00:27:54 +00001294
hailfingerd219a232009-01-28 00:27:54 +00001295 numbytes = fwrite(buf, 1, size, image);
1296 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001297 if (numbytes != size) {
1298 msg_gerr("File %s could not be written completely.\n",
1299 filename);
hailfingerd219a232009-01-28 00:27:54 +00001300 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001301 }
hailfingerd219a232009-01-28 00:27:54 +00001302 return 0;
1303}
1304
David Hendrickse3451942013-03-21 17:23:29 -07001305/*
1306 * read_flash - wrapper for flash->read() with additional high-level policy
1307 *
1308 * @flash flash chip
1309 * @buf buffer to store data in
1310 * @start start address
1311 * @len number of bytes to read
1312 *
1313 * This wrapper simplifies most cases when the flash chip needs to be read
1314 * since policy decisions such as non-fatal error handling is centralized.
1315 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001316int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001317 unsigned int start, unsigned int len)
1318{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001319 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001320
Patrick Georgif3fa2992017-02-02 16:24:44 +01001321 if (!flash || !flash->chip->read)
David Hendrickse3451942013-03-21 17:23:29 -07001322 return -1;
1323
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001324 msg_cdbg("%#06x-%#06x:R ", start, start + len - 1);
1325
Patrick Georgif3fa2992017-02-02 16:24:44 +01001326 ret = flash->chip->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001327 if (ret) {
1328 if (ignore_error(ret)) {
1329 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1330 start, start + len - 1);
1331 ret = 0;
1332 } else {
1333 msg_gdbg("failed to read 0x%x-0x%x\n",
1334 start, start + len - 1);
1335 }
1336 }
1337
1338 return ret;
1339}
1340
David Hendricks7c8a1612013-04-26 19:14:44 -07001341/*
1342 * write_flash - wrapper for flash->write() with additional high-level policy
1343 *
1344 * @flash flash chip
1345 * @buf buffer to write to flash
1346 * @start start address in flash
1347 * @len number of bytes to write
1348 *
1349 * TODO: Look up regions that are write-protected and avoid attempt to write
1350 * to them at all.
1351 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001352int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001353 unsigned int start, unsigned int len)
1354{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001355 if (!flash || !flash->chip->write)
David Hendricks7c8a1612013-04-26 19:14:44 -07001356 return -1;
1357
Patrick Georgif3fa2992017-02-02 16:24:44 +01001358 return flash->chip->write(flash, buf, start, len);
David Hendricks7c8a1612013-04-26 19:14:44 -07001359}
1360
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001361int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001362{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001363 unsigned long size = flash->chip->total_size * 1024;
hailfinger42a850a2010-07-13 23:56:13 +00001364 unsigned char *buf = calloc(size, sizeof(char));
1365 int ret = 0;
1366
1367 msg_cinfo("Reading flash... ");
1368 if (!buf) {
1369 msg_gerr("Memory allocation failed!\n");
1370 msg_cinfo("FAILED.\n");
1371 return 1;
1372 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001373
1374 /* To support partial read, fill buffer to all 0xFF at beginning to make
1375 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001376 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001377
Patrick Georgif3fa2992017-02-02 16:24:44 +01001378 if (!flash->chip->read) {
hailfinger42a850a2010-07-13 23:56:13 +00001379 msg_cerr("No read function available for this flash chip.\n");
1380 ret = 1;
1381 goto out_free;
1382 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001383
1384 /* First try to handle partial read case, rather than read the whole
1385 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001386 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001387 if (ret < 0) {
1388 msg_cerr("Partial read operation failed!\n");
1389 ret = 1;
1390 goto out_free;
1391 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001392 int num_regions = get_num_include_args();
1393
1394 if (ret != num_regions) {
1395 msg_cerr("Requested %d regions, but only read %d\n",
1396 num_regions, ret);
1397 ret = 1;
1398 goto out_free;
1399 }
1400
1401 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001402 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001403 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001404 msg_cerr("Read operation failed!\n");
1405 ret = 1;
1406 goto out_free;
1407 }
hailfinger42a850a2010-07-13 23:56:13 +00001408 }
1409
David Hendricksdf29a832013-06-28 14:33:51 -07001410 if (filename)
1411 ret = write_buf_to_file(buf, size, filename);
1412
hailfinger42a850a2010-07-13 23:56:13 +00001413out_free:
1414 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001415 if (ret)
1416 msg_cerr("FAILED.");
1417 else
1418 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001419 return ret;
1420}
1421
hailfingerb437e282010-11-04 01:04:27 +00001422/* This function shares a lot of its structure with erase_and_write_flash() and
1423 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001424 * Even if an error is found, the function will keep going and check the rest.
1425 */
hailfinger48ed3e22011-05-04 00:39:50 +00001426static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001427{
hailfingerb91c08c2011-08-15 19:54:20 +00001428 int i, j, k;
1429 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001430
1431 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1432 unsigned int done = 0;
1433 struct block_eraser eraser = flash->block_erasers[k];
1434
1435 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1436 /* Blocks with zero size are bugs in flashchips.c. */
1437 if (eraser.eraseblocks[i].count &&
1438 !eraser.eraseblocks[i].size) {
1439 msg_gerr("ERROR: Flash chip %s erase function "
1440 "%i region %i has size 0. Please report"
1441 " a bug at flashrom@flashrom.org\n",
1442 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001443 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001444 }
1445 /* Blocks with zero count are bugs in flashchips.c. */
1446 if (!eraser.eraseblocks[i].count &&
1447 eraser.eraseblocks[i].size) {
1448 msg_gerr("ERROR: Flash chip %s erase function "
1449 "%i region %i has count 0. Please report"
1450 " a bug at flashrom@flashrom.org\n",
1451 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001452 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001453 }
1454 done += eraser.eraseblocks[i].count *
1455 eraser.eraseblocks[i].size;
1456 }
hailfinger9fed35d2010-01-19 06:42:46 +00001457 /* Empty eraseblock definition with erase function. */
1458 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001459 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001460 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001461 if (!done)
1462 continue;
1463 if (done != flash->total_size * 1024) {
1464 msg_gerr("ERROR: Flash chip %s erase function %i "
1465 "region walking resulted in 0x%06x bytes total,"
1466 " expected 0x%06x bytes. Please report a bug at"
1467 " flashrom@flashrom.org\n", flash->name, k,
1468 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001469 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001470 }
hailfinger9fed35d2010-01-19 06:42:46 +00001471 if (!eraser.block_erase)
1472 continue;
1473 /* Check if there are identical erase functions for different
1474 * layouts. That would imply "magic" erase functions. The
1475 * easiest way to check this is with function pointers.
1476 */
uwef6f94d42010-03-13 17:28:29 +00001477 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001478 if (eraser.block_erase ==
1479 flash->block_erasers[j].block_erase) {
1480 msg_gerr("ERROR: Flash chip %s erase function "
1481 "%i and %i are identical. Please report"
1482 " a bug at flashrom@flashrom.org\n",
1483 flash->name, k, j);
1484 ret = 1;
1485 }
uwef6f94d42010-03-13 17:28:29 +00001486 }
hailfinger45177872010-01-18 08:14:43 +00001487 }
hailfinger9fed35d2010-01-19 06:42:46 +00001488 return ret;
hailfinger45177872010-01-18 08:14:43 +00001489}
1490
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001491static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001492 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001493 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001494 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001495 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001496 unsigned int addr,
1497 unsigned int len))
1498{
stefanctc5eb8a92011-11-23 09:13:48 +00001499 unsigned int starthere = 0, lenhere = 0;
1500 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001501 int block_was_erased = 0;
hailfingerb437e282010-11-04 01:04:27 +00001502 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1503
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001504 /*
1505 * curcontents and newcontents are opaque to walk_eraseregions, and
1506 * need to be adjusted here to keep the impression of proper
1507 * abstraction
hailfingerb437e282010-11-04 01:04:27 +00001508 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001509
hailfinger90fcf9b2010-11-05 14:51:59 +00001510 curcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001511
hailfingerb437e282010-11-04 01:04:27 +00001512 newcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001513
hailfingerb437e282010-11-04 01:04:27 +00001514 msg_cdbg(":");
1515 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001516 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001517 content_has_changed |= 1;
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001518 msg_cdbg(" E");
hailfingerb437e282010-11-04 01:04:27 +00001519 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001520 if (ret) {
1521 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001522 msg_cdbg(" DENIED");
David Hendricks1ed1d352011-11-23 17:54:37 -08001523 else
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001524 msg_cerr(" ERASE_FAILED\n");
hailfingerb437e282010-11-04 01:04:27 +00001525 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001526 }
1527
David Hendricks0954ffc2015-11-13 15:15:44 -08001528 if (programmer_table[programmer].paranoid) {
1529 if (check_erased_range(flash, start, len)) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001530 msg_cerr(" ERASE_FAILED\n");
David Hendricks0954ffc2015-11-13 15:15:44 -08001531 return -1;
1532 }
hailfingerac8e3182011-06-26 17:04:16 +00001533 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001534
hailfinger90fcf9b2010-11-05 14:51:59 +00001535 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001536 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001537 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001538 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001539 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001540 /* get_next_write() sets starthere to a new value after the call. */
1541 while ((lenhere = get_next_write(curcontents + starthere,
1542 newcontents + starthere,
1543 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001544 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001545 if (!writecount++)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001546 msg_cdbg(" W");
hailfingerb437e282010-11-04 01:04:27 +00001547 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001548 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001549 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001550 if (ret) {
1551 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001552 msg_cdbg(" DENIED");
hailfingerb437e282010-11-04 01:04:27 +00001553 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001554 }
David Hendricks048b38c2016-03-28 18:47:06 -07001555
1556 /*
1557 * If the block needed to be erased and was erased successfully
1558 * then we can assume that we didn't run into any write-
1559 * protected areas. Otherwise, we need to verify each page to
1560 * ensure it was successfully written and abort if we encounter
1561 * any errors.
1562 */
1563 if (programmer_table[programmer].paranoid && !block_was_erased) {
1564 if (verify_range(flash, newcontents + starthere,
1565 start + starthere, lenhere, "WRITE"))
1566 return -1;
1567 }
1568
hailfingerb437e282010-11-04 01:04:27 +00001569 starthere += lenhere;
1570 skip = 0;
1571 }
1572 if (skip)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001573 msg_cdbg(" SKIP");
hailfingerb437e282010-11-04 01:04:27 +00001574 return ret;
1575}
1576
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001577/*
1578 * Function to process processing units accumulated in the action descriptor.
1579 *
1580 * @flash pointer to the flash context to operate on
1581 * @do_something helper function which can erase and program a section of the
1582 * flash chip. It receives the flash context, offset and length
1583 * of the area to erase/program, before and after contents (to
1584 * decide what exactly needs to be erased and or programmed)
1585 * and a pointer to the erase function which can operate on the
1586 * proper granularity.
1587 * @descriptor action descriptor including pointers to before and after
1588 * contents and an array of processing actions to take.
1589 *
1590 * Returns zero on success or an error code.
1591 */
1592static int walk_eraseregions(struct flashctx *flash,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001593 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001594 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001595 unsigned int len,
1596 uint8_t *param1,
1597 uint8_t *param2,
1598 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001599 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001600 unsigned int addr,
1601 unsigned int len)),
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001602 struct action_descriptor *descriptor)
hailfinger2b8c9382010-07-13 00:37:19 +00001603{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001604 struct processing_unit *pu;
1605 int rc = 0;
1606 static int print_comma;
uwe8d342eb2011-07-28 08:13:25 +00001607
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001608 for (pu = descriptor->processing_units; pu->num_blocks; pu++) {
1609 unsigned base = pu->offset;
1610 unsigned top = pu->offset + pu->block_size * pu->num_blocks;
David Hendricks605544b2015-08-15 16:32:58 -07001611
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001612 while (base < top) {
David Hendricks605544b2015-08-15 16:32:58 -07001613
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001614 if (print_comma)
hailfingerb437e282010-11-04 01:04:27 +00001615 msg_cdbg(", ");
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001616 else
1617 print_comma = 1;
1618
1619 msg_cdbg("0x%06x-0x%06zx", base, base + pu->block_size - 1);
1620
1621 rc = do_something(flash, base,
1622 pu->block_size,
1623 descriptor->oldcontents,
1624 descriptor->newcontents,
1625 flash->chip->block_erasers[pu->block_eraser_index].block_erase);
1626
David Hendricks1ed1d352011-11-23 17:54:37 -08001627 if (rc) {
1628 if (ignore_error(rc))
1629 rc = 0;
1630 else
1631 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001632 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001633 base += pu->block_size;
hailfinger2b8c9382010-07-13 00:37:19 +00001634 }
1635 }
hailfingerb437e282010-11-04 01:04:27 +00001636 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001637 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001638}
1639
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001640static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001641{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001642 struct block_eraser eraser = flash->chip->block_erasers[k];
hailfingercf848f12010-12-05 15:14:44 +00001643
1644 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1645 if (log)
1646 msg_cdbg("not defined. ");
1647 return 1;
1648 }
1649 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1650 if (log)
1651 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001652 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001653 return 1;
1654 }
1655 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1656 if (log)
1657 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001658 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001659 return 1;
1660 }
1661 return 0;
1662}
1663
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001664int erase_and_write_flash(struct flashctx *flash,
1665 struct action_descriptor *descriptor)
hailfingerd219a232009-01-28 00:27:54 +00001666{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001667 int ret = 1;
hailfingercf848f12010-12-05 15:14:44 +00001668
hailfingercf848f12010-12-05 15:14:44 +00001669 msg_cinfo("Erasing and writing flash chip... ");
hailfingerb437e282010-11-04 01:04:27 +00001670
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001671 ret = walk_eraseregions(flash, &erase_and_write_block_helper, descriptor);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001672
hailfinger7df21362009-09-05 02:30:58 +00001673 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001674 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001675 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001676 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001677 }
1678 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001679}
1680
hailfinger4c47e9d2010-10-19 22:06:20 +00001681void nonfatal_help_message(void)
1682{
1683 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1684 "This means we have to add special support for your board, "
1685 "programmer or flash chip.\n"
1686 "Please report this on IRC at irc.freenode.net (channel "
1687 "#flashrom) or\n"
1688 "mail flashrom@flashrom.org!\n"
1689 "-------------------------------------------------------------"
1690 "------------------\n"
1691 "You may now reboot or simply leave the machine running.\n");
1692}
1693
uweb34ec9f2009-10-01 18:40:02 +00001694void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001695{
snelsone42c3802010-05-07 20:09:04 +00001696 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001697 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001698 "mail flashrom@flashrom.org with FAILED: your board name in "
1699 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001700 "-------------------------------------------------------------"
1701 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001702 "DO NOT REBOOT OR POWEROFF!\n");
1703}
1704
uwe8d342eb2011-07-28 08:13:25 +00001705/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001706void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001707{
1708 enum programmer p;
1709 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001710 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001711 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001712 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001713 }
Simon Glass8dc82732013-07-16 10:13:51 -06001714 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001715}
1716
hailfingerf79d1712010-10-06 23:48:34 +00001717void list_programmers_linebreak(int startcol, int cols, int paren)
1718{
1719 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001720 int pnamelen;
1721 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001722 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001723 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001724
1725 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1726 pname = programmer_table[p].name;
1727 pnamelen = strlen(pname);
1728 if (remaining - pnamelen - 2 < 0) {
1729 if (firstline)
1730 firstline = 0;
1731 else
1732 printf("\n");
1733 for (i = 0; i < startcol; i++)
1734 printf(" ");
1735 remaining = cols - startcol;
1736 } else {
1737 printf(" ");
1738 remaining--;
1739 }
1740 if (paren && (p == 0)) {
1741 printf("(");
1742 remaining--;
1743 }
1744 printf("%s", pname);
1745 remaining -= pnamelen;
1746 if (p < PROGRAMMER_INVALID - 1) {
1747 printf(",");
1748 remaining--;
1749 } else {
1750 if (paren)
1751 printf(")");
1752 printf("\n");
1753 }
1754 }
1755}
1756
hailfinger3b471632010-03-27 16:36:40 +00001757void print_sysinfo(void)
1758{
David Hendricksc6c9f822010-11-03 15:07:01 -07001759 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001760#if HAVE_UTSNAME == 1
1761 struct utsname osinfo;
1762 uname(&osinfo);
1763
David Hendricksc6c9f822010-11-03 15:07:01 -07001764 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001765 osinfo.machine);
1766#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001767 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001768#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001769}
1770
1771void print_buildinfo(void)
1772{
1773 msg_gdbg("flashrom was built with");
hailfinger3b471632010-03-27 16:36:40 +00001774#if NEED_PCI == 1
1775#ifdef PCILIB_VERSION
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001776 msg_gdbg(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001777#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001778 msg_gdbg(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001779#endif
1780#endif
1781#ifdef __clang__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001782 msg_gdbg(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001783#ifdef __clang_version__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001784 msg_gdbg(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001785#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001786 msg_gdbg(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001787#endif
hailfinger3b471632010-03-27 16:36:40 +00001788#elif defined(__GNUC__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001789 msg_gdbg(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001790#ifdef __VERSION__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001791 msg_gdbg(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001792#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001793 msg_gdbg(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001794#endif
1795#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001796 msg_gdbg(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001797#endif
1798#if defined (__FLASHROM_LITTLE_ENDIAN__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001799 msg_gdbg(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001800#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001801 msg_gdbg(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001802#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001803 msg_gdbg("\n");
hailfinger3b471632010-03-27 16:36:40 +00001804}
1805
uwefdeca092008-01-21 15:24:22 +00001806void print_version(void)
1807{
David Hendricksc6c9f822010-11-03 15:07:01 -07001808 /* send to stderr for chromium os */
1809 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001810 print_sysinfo();
David Hendricks07268292016-09-14 16:05:58 -07001811 msg_gerr("\n");
uwefdeca092008-01-21 15:24:22 +00001812}
1813
hailfinger74819ad2010-05-15 15:04:37 +00001814void print_banner(void)
1815{
1816 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001817 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001818 msg_ginfo("\n");
1819}
1820
hailfingerc77acb52009-12-24 02:15:55 +00001821int selfcheck(void)
1822{
hailfinger45177872010-01-18 08:14:43 +00001823 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001824 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001825
1826 /* Safety check. Instead of aborting after the first error, check
1827 * if more errors exist.
1828 */
hailfingerc77acb52009-12-24 02:15:55 +00001829 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001830 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001831 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001832 }
stefanct8632c922011-07-26 14:33:46 +00001833 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001834 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001835 * For 'flashchips' we check the first element to be non-null. In the
1836 * other cases there exist use cases where the first element can be
1837 * null. */
Yunlian Jiang945c3392015-01-20 15:56:44 -08001838 if (flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001839 msg_gerr("Flashchips table miscompilation!\n");
1840 ret = 1;
1841 }
hailfinger45177872010-01-18 08:14:43 +00001842 for (flash = flashchips; flash && flash->name; flash++)
1843 if (selfcheck_eraseblocks(flash))
1844 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001845
hailfinger45177872010-01-18 08:14:43 +00001846 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001847}
1848
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001849void check_chip_supported(const struct flashctx *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001850{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001851 if (flash->chip->feature_bits & FEATURE_OTP) {
Stefan Tauner9816fc82016-08-12 15:47:49 -07001852 msg_cdbg("This chip may contain one-time programmable memory. "
1853 "flashrom cannot read\nand may never be able to write "
1854 "it, hence it may not be able to completely\n"
1855 "clone the contents of this chip (see man page for "
1856 "details).\n");
1857 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001858 if ((flash->chip->tested.probe != OK) ||
1859 (flash->chip->tested.read != OK) ||
1860 (flash->chip->tested.erase != OK) ||
1861 (flash->chip->tested.write != OK) ||
1862 (flash->chip->tested.uread != OK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001863 msg_cdbg("===\n");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001864 if ((flash->chip->tested.probe == BAD) ||
1865 (flash->chip->tested.read == BAD) ||
1866 (flash->chip->tested.erase == BAD) ||
1867 (flash->chip->tested.write == BAD) ||
1868 (flash->chip->tested.uread == BAD)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001869 msg_cdbg("This flash part has status NOT WORKING for operations:");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001870 if (flash->chip->tested.probe == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001871 msg_cdbg(" PROBE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001872 if (flash->chip->tested.read == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001873 msg_cdbg(" READ");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001874 if (flash->chip->tested.erase == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001875 msg_cdbg(" ERASE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001876 if (flash->chip->tested.write == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001877 msg_cdbg(" WRITE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001878 if (flash->chip->tested.uread == BAD)
Duncan Laurie06ffd522015-10-26 12:56:08 -07001879 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001880 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001881 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001882 if ((flash->chip->tested.probe == NT) ||
1883 (flash->chip->tested.read == NT) ||
1884 (flash->chip->tested.erase == NT) ||
1885 (flash->chip->tested.write == NT) ||
1886 (flash->chip->tested.uread == NT)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001887 msg_cdbg("This flash part has status UNTESTED for operations:");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001888 if (flash->chip->tested.probe == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001889 msg_cdbg(" PROBE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001890 if (flash->chip->tested.read == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001891 msg_cdbg(" READ");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001892 if (flash->chip->tested.erase == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001893 msg_cdbg(" ERASE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001894 if (flash->chip->tested.write == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001895 msg_cdbg(" WRITE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001896 if (flash->chip->tested.uread == NT)
Duncan Laurie06ffd522015-10-26 12:56:08 -07001897 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001898 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001899 }
hailfinger92cd8e32010-01-07 03:24:05 +00001900 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001901 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001902 "in the latest development\n"
1903 "version of flashrom. If you are running the latest "
1904 "development version,\n"
1905 "please email a report to flashrom@flashrom.org if "
1906 "any of the above operations\n"
1907 "work correctly for you with this flash part. Please "
1908 "include the flashrom\n"
1909 "output with the additional -V option for all "
1910 "operations you tested (-V, -Vr,\n"
Paul Menzel425dded2016-08-08 16:42:48 -07001911 "-VE, -Vw), and mention which mainboard or "
hailfinger74819ad2010-05-15 15:04:37 +00001912 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001913 "Please mention your board in the subject line. "
1914 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001915 }
1916}
1917
hailfinger771fc182010-10-15 00:01:14 +00001918/* FIXME: This function signature needs to be improved once doit() has a better
1919 * function signature.
1920 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001921int 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 +00001922{
Patrick Georgiac3423f2017-02-03 20:58:06 +01001923 const struct flashchip *chip = flash->chip;
1924
hailfinger771fc182010-10-15 00:01:14 +00001925 if (!programmer_may_write && (write_it || erase_it)) {
1926 msg_perr("Write/erase is not working yet on your programmer in "
1927 "its current configuration.\n");
1928 /* --force is the wrong approach, but it's the best we can do
1929 * until the generic programmer parameter parser is merged.
1930 */
1931 if (!force)
1932 return 1;
1933 msg_cerr("Continuing anyway.\n");
1934 }
1935
1936 if (read_it || erase_it || write_it || verify_it) {
1937 /* Everything needs read. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001938 if (chip->tested.read == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001939 msg_cerr("Read is not working on this chip. ");
1940 if (!force)
1941 return 1;
1942 msg_cerr("Continuing anyway.\n");
1943 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001944 if (!chip->read) {
hailfinger771fc182010-10-15 00:01:14 +00001945 msg_cerr("flashrom has no read function for this "
1946 "flash chip.\n");
1947 return 1;
1948 }
1949 }
1950 if (erase_it || write_it) {
1951 /* Write needs erase. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001952 if (chip->tested.erase == NA) {
1953 msg_cerr("Erase is not possible on this chip.\n");
1954 return 1;
1955 }
1956 if (chip->tested.erase == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001957 msg_cerr("Erase is not working on this chip. ");
1958 if (!force)
1959 return 1;
1960 msg_cerr("Continuing anyway.\n");
1961 }
stefancte1c5acf2011-07-04 07:27:17 +00001962 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001963 msg_cerr("flashrom has no erase function for this "
1964 "flash chip.\n");
1965 return 1;
1966 }
hailfinger771fc182010-10-15 00:01:14 +00001967 }
1968 if (write_it) {
Patrick Georgiac3423f2017-02-03 20:58:06 +01001969 if (chip->tested.write == NA) {
1970 msg_cerr("Write is not possible on this chip.\n");
1971 return 1;
1972 }
1973 if (chip->tested.write == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001974 msg_cerr("Write is not working on this chip. ");
1975 if (!force)
1976 return 1;
1977 msg_cerr("Continuing anyway.\n");
1978 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001979 if (!chip->write) {
hailfinger771fc182010-10-15 00:01:14 +00001980 msg_cerr("flashrom has no write function for this "
1981 "flash chip.\n");
1982 return 1;
1983 }
1984 }
1985 return 0;
1986}
1987
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001988/*
1989 * Function to erase entire flash chip.
1990 *
1991 * @flashctx pointer to the flash context to use
1992 * @oldcontents pointer to the buffer including current chip contents, to
1993 * decide which areas do in fact need to be erased
1994 * @size the size of the flash chip, in bytes.
1995 *
1996 * Returns zero on success or an error code.
1997 */
1998static int erase_chip(struct flashctx *flash, void *oldcontents,
1999 void *newcontents, size_t size)
2000{
2001 /*
2002 * To make sure that the chip is fully erased, let's cheat and create
2003 * a descriptor where the new contents are all erased.
2004 */
2005 struct action_descriptor *fake_descriptor;
2006 int ret = 0;
2007
2008 fake_descriptor = prepare_action_descriptor(flash, oldcontents,
2009 newcontents, 1);
2010 /* FIXME: Do we really want the scary warning if erase failed? After
2011 * all, after erase the chip is either blank or partially blank or it
2012 * has the old contents. A blank chip won't boot, so if the user
2013 * wanted erase and reboots afterwards, the user knows very well that
2014 * booting won't work.
2015 */
2016 if (erase_and_write_flash(flash, fake_descriptor)) {
2017 emergency_help_message();
2018 ret = 1;
2019 }
2020
2021 free(fake_descriptor);
2022
2023 return ret;
2024}
2025
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002026static int read_dest_content(struct flashctx *flash, int verify_it,
2027 uint8_t *dest, unsigned long size)
2028{
2029 if (((verify_it == VERIFY_OFF) || (verify_it == VERIFY_PARTIAL))
2030 && get_num_include_args()) {
2031 /*
2032 * If no full verification is required and not
2033 * the entire chip is about to be programmed,
2034 * read only the areas which might change.
2035 */
2036 if (handle_partial_read(flash, dest, read_flash, 0) < 0)
2037 return 1;
2038 } else {
2039 if (read_flash(flash, dest, 0, size))
2040 return 1;
2041 }
2042 return 0;
2043}
2044
hailfingerc77acb52009-12-24 02:15:55 +00002045/* This function signature is horrible. We need to design a better interface,
2046 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00002047 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00002048 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002049int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09002050 int write_it, int erase_it, int verify_it, int extract_it,
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002051 const char *diff_file, int do_diff)
hailfingerc77acb52009-12-24 02:15:55 +00002052{
hailfinger4c47e9d2010-10-19 22:06:20 +00002053 uint8_t *oldcontents;
2054 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00002055 int ret = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +01002056 unsigned long size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002057 struct action_descriptor *descriptor = NULL;
hailfingerc77acb52009-12-24 02:15:55 +00002058
stefanct02116582011-05-18 01:30:56 +00002059 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00002060 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00002061 ret = 1;
2062 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00002063 }
2064
hailfinger771fc182010-10-15 00:01:14 +00002065 /* Given the existence of read locks, we want to unlock for read,
2066 * erase and write.
2067 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01002068 if (flash->chip->unlock)
2069 flash->chip->unlock(flash);
hailfinger771fc182010-10-15 00:01:14 +00002070
Ed Swierk28cf7992017-07-03 13:17:18 -07002071 /* Enable/disable 4-byte addressing mode if flash chip supports it */
2072 if ((flash->chip->feature_bits & FEATURE_4BA_SUPPORT) &&
2073 flash->chip->four_bytes_addr_funcs.set_4ba) {
2074 if (flash->chip->four_bytes_addr_funcs.set_4ba(flash)) {
2075 msg_cerr("Enabling/disabling 4-byte addressing mode failed!\n");
2076 return 1;
Boris Baykov6323c242016-06-11 18:29:03 +02002077 }
Boris Baykov1a2f5322016-06-11 18:29:00 +02002078 }
2079
Simon Glass9ad06c12013-07-03 22:08:17 +09002080 if (extract_it) {
2081 ret = extract_regions(flash);
2082 goto out_nofree;
2083 }
2084
David Hendricksd0ea9ed2011-03-04 17:31:57 -08002085 /* mark entries included using -i argument as "included" if they are
2086 found in the master rom_entries list */
2087 if (process_include_args() < 0) {
2088 ret = 1;
2089 goto out_nofree;
2090 }
2091
hailfinger771fc182010-10-15 00:01:14 +00002092 if (read_it) {
2093 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00002094 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00002095 }
hailfingerb437e282010-11-04 01:04:27 +00002096
stefanctd611e8f2011-07-12 22:35:21 +00002097 oldcontents = malloc(size);
2098 if (!oldcontents) {
2099 msg_gerr("Out of memory!\n");
2100 exit(1);
2101 }
Simon Glass4c214132013-07-16 10:09:28 -06002102 /* Assume worst case: All blocks are not erased. */
2103 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002104 newcontents = malloc(size);
2105 if (!newcontents) {
2106 msg_gerr("Out of memory!\n");
2107 exit(1);
2108 }
Simon Glass4c214132013-07-16 10:09:28 -06002109 /* Assume best case: All blocks are erased. */
2110 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002111 /* Side effect of the assumptions above: Default write action is erase
2112 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002113 * oldcontents being completely unerased means we have to erase
2114 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002115 */
2116
hailfingerd217d122010-10-08 18:52:29 +00002117 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002118 /*
2119 * Note: This must be done before any files specified by -i
2120 * arguments are processed merged into the newcontents since
2121 * -i files take priority. See http://crbug.com/263495.
2122 */
2123 if (filename) {
2124 if (read_buf_from_file(newcontents, size, filename)) {
2125 ret = 1;
2126 goto out;
2127 }
2128 } else {
2129 /* Content will be read from -i args, so they must
2130 * not overlap. */
2131 if (included_regions_overlap()) {
2132 msg_gerr("Error: Included regions must "
2133 "not overlap.\n");
2134 ret = 1;
2135 goto out;
2136 }
stepan1da96c02006-11-21 23:48:51 +00002137 }
2138
David Hendricksac82cac2012-06-19 10:29:37 -07002139#if 0
2140 /*
2141 * FIXME: show_id() causes failure if vendor:mainboard do not
2142 * match. This may happen if codenames are in flux.
2143 * See chrome-os-partner:10414.
2144 */
hailfinger90c7d542010-05-31 15:27:27 +00002145#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002146 if (programmer == PROGRAMMER_INTERNAL)
2147 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002148#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002149#endif
ollie5672ac62004-03-17 22:22:08 +00002150 }
2151
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002152 if (do_diff) {
2153 /*
2154 * Obtain a reference image so that we can check whether
2155 * regions need to be erased and to give better diagnostics in
2156 * case write fails. If --fast-verify is used then only the
2157 * regions which are included using -i will be read.
2158 */
2159 if (diff_file) {
2160 msg_cdbg("Reading old contents from file... ");
2161 if (read_buf_from_file(oldcontents, size, diff_file)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002162 ret = 1;
2163 msg_cdbg("FAILED.\n");
2164 goto out;
2165 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002166 } else {
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002167 msg_cdbg("Reading old contents from flash chip... ");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002168 ret = read_dest_content(flash, verify_it,
2169 oldcontents, size);
2170 if (ret) {
2171 msg_cdbg("FAILED.\n");
2172 goto out;
David Hendricks52ddff02013-07-23 15:05:14 -07002173 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002174 }
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002175 msg_cdbg("done.\n");
2176 } else if (!erase_it) {
2177 msg_pinfo("No diff performed, considering the chip erased.\n");
2178 memset(oldcontents, flash_erase_value(flash), size);
hailfinger4c47e9d2010-10-19 22:06:20 +00002179 }
David Hendricksac1d25c2016-08-09 17:00:58 -07002180
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002181
David Hendricksdf29a832013-06-28 14:33:51 -07002182 /*
2183 * Note: This must be done after reading the file specified for the
2184 * -w/-v argument, if any, so that files specified using -i end up
2185 * in the "newcontents" buffer before being written.
2186 * See http://crbug.com/263495.
2187 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002188 if (handle_romentries(flash, oldcontents, newcontents, erase_it)) {
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002189 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002190 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002191 goto out;
2192 }
uwef6641642007-05-09 10:17:44 +00002193
David Hendricksa7e114b2016-02-26 18:49:15 -08002194 if (erase_it) {
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002195 erase_chip(flash, oldcontents, newcontents, size);
2196 goto verify;
David Hendricksa7e114b2016-02-26 18:49:15 -08002197 }
2198
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002199 descriptor = prepare_action_descriptor(flash, oldcontents,
2200 newcontents, do_diff);
stuge8ce3a3c2008-04-28 14:47:30 +00002201 if (write_it) {
David Hendricksb64b39a2016-10-11 13:48:06 -07002202 // parse the new fmap and disable soft WP if necessary
David Hendricksac1d25c2016-08-09 17:00:58 -07002203 if ((ret = cros_ec_prepare(newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002204 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002205 goto out;
2206 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002207
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002208 if (erase_and_write_flash(flash, descriptor)) {
hailfingerb437e282010-11-04 01:04:27 +00002209 msg_cerr("Uh oh. Erase/write failed. Checking if "
2210 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002211 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002212 if (!memcmp(oldcontents, newcontents, size)) {
2213 msg_cinfo("Good. It seems nothing was "
2214 "changed.\n");
2215 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002216 ret = 1;
2217 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002218 }
2219 }
hailfingerd217d122010-10-08 18:52:29 +00002220 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002221 ret = 1;
2222 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002223 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002224
David Hendricksac1d25c2016-08-09 17:00:58 -07002225 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002226 if (ret < 0) {
2227 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002228 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002229 emergency_help_message();
2230 ret = 1;
2231 goto out;
2232 } else if (ret > 0) {
2233 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002234 msg_pdbg("CROS_EC needs 2nd pass.\n");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002235 ret = read_dest_content(flash, verify_it,
2236 oldcontents, size);
2237 if (ret) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002238 emergency_help_message();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002239 goto out;
2240 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002241
2242 /* Get a new descriptor. */
2243 free(descriptor);
2244 descriptor = prepare_action_descriptor(flash,
2245 oldcontents,
2246 newcontents,
2247 do_diff);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002248 // write 2nd pass
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002249 if (erase_and_write_flash(flash, descriptor)) {
David Hendricksb907de32014-08-11 16:47:09 -07002250 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002251 emergency_help_message();
2252 ret = 1;
2253 goto out;
2254 }
2255 ret = 0;
2256 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002257
David Hendricksac1d25c2016-08-09 17:00:58 -07002258 if (cros_ec_finish() < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002259 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002260 emergency_help_message();
2261 ret = 1;
2262 goto out;
2263 }
stuge8ce3a3c2008-04-28 14:47:30 +00002264 }
ollie6a600992005-11-26 21:55:36 +00002265
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002266 verify:
hailfinger0459e1c2009-08-19 13:55:34 +00002267 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002268 if ((write_it || erase_it) && !content_has_changed) {
2269 msg_gdbg("Nothing was erased or written, skipping "
2270 "verification\n");
2271 } else {
2272 /* Work around chips which need some time to calm down. */
2273 if (write_it && verify_it != VERIFY_PARTIAL)
2274 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002275
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002276 ret = verify_flash(flash, descriptor, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002277
David Hendricks9ba79fb2015-04-03 12:06:16 -07002278 /* If we tried to write, and verification now fails, we
2279 * might have an emergency situation.
2280 */
2281 if (ret && write_it)
2282 emergency_help_message();
2283 }
hailfinger0459e1c2009-08-19 13:55:34 +00002284 }
ollie6a600992005-11-26 21:55:36 +00002285
hailfinger90fcf9b2010-11-05 14:51:59 +00002286out:
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002287 if (descriptor)
2288 free(descriptor);
2289
hailfinger90fcf9b2010-11-05 14:51:59 +00002290 free(oldcontents);
2291 free(newcontents);
2292out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002293 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002294 /*
2295 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2296 * tree. This is because some operations, such as write protection,
2297 * requires programmer_shutdown() but does not call doit().
2298 */
2299// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002300 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002301}