blob: 056821203f7880634a1e6c2d85c0603c5fb9787e [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
uwe4475e902009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
hailfinger23060112009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
uweb25f1ea2007-08-29 17:52:32 +000019 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
rminnich8d3ff912003-10-25 17:01:29 +000022 */
23
hailfingera83a5fe2010-05-30 22:24:40 +000024#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000026#ifndef __LIBPAYLOAD__
27#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000028#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000029#endif
rminnich8d3ff912003-10-25 17:01:29 +000030#include <string.h>
31#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000032#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000033#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
Vincent Palatin7ab23932014-10-01 12:09:16 -070037#include <unistd.h>
rminnich8d3ff912003-10-25 17:01:29 +000038#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000039#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090040#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000041#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000042
krause2eb76212011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000044char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000045int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000046
David Hendricks54777392015-01-11 18:55:14 -080047unsigned int required_erase_size = 0; /* see comment in flash.h */
48
David Hendricks9ba79fb2015-04-03 12:06:16 -070049/* Set if any erase/write operation is to be done. This will be used to
50 * decide if final verification is needed. */
51static int content_has_changed = 0;
52
David Hendricks1ed1d352011-11-23 17:54:37 -080053/* error handling stuff */
54enum error_action access_denied_action = error_ignore;
55
56int ignore_error(int err) {
57 int rc = 0;
58
59 switch(err) {
60 case ACCESS_DENIED:
61 if (access_denied_action == error_ignore)
62 rc = 1;
63 break;
64 default:
65 break;
66 }
67
68 return rc;
69}
70
hailfinger969e2f32011-09-08 00:00:29 +000071static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000072
hailfingerddeb4ac2010-07-08 10:13:37 +000073static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000074
uwee15beb92010-08-08 17:01:18 +000075/*
hailfinger80422e22009-12-13 22:28:00 +000076 * Programmers supporting multiple buses can have differing size limits on
77 * each bus. Store the limits for each bus in a common struct.
78 */
hailfinger1ff33dc2010-07-03 11:02:10 +000079struct decode_sizes max_rom_decode;
80
81/* If nonzero, used as the start address of bottom-aligned flash. */
82unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000083
hailfinger5828baf2010-07-03 12:14:25 +000084/* Is writing allowed with this programmer? */
85int programmer_may_write;
86
hailfingerabe249e2009-05-08 17:43:22 +000087const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000088#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000089 {
hailfinger3548a9a2009-08-12 14:34:35 +000090 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000091 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000092 .map_flash_region = physmap,
93 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000094 .delay = internal_delay,
David Hendricks55cdd9c2015-11-25 14:37:26 -080095
96 /*
97 * "Internal" implies in-system programming on a live system, so
98 * handle with paranoia to catch errors early. If something goes
99 * wrong then hopefully the system will still be recoverable.
100 */
101 .paranoid = 1,
hailfingerabe249e2009-05-08 17:43:22 +0000102 },
hailfinger80422e22009-12-13 22:28:00 +0000103#endif
stepan927d4e22007-04-04 22:45:58 +0000104
hailfinger90c7d542010-05-31 15:27:27 +0000105#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000106 {
hailfinger3548a9a2009-08-12 14:34:35 +0000107 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000108 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000109 .map_flash_region = dummy_map,
110 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000111 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000112 },
hailfinger571a6b32009-09-16 10:09:21 +0000113#endif
hailfingera9df33c2009-05-09 00:54:55 +0000114
hailfinger90c7d542010-05-31 15:27:27 +0000115#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000116 {
hailfinger3548a9a2009-08-12 14:34:35 +0000117 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000118 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000119 .map_flash_region = fallback_map,
120 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000121 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000122 },
hailfinger571a6b32009-09-16 10:09:21 +0000123#endif
uwe0f5a3a22009-05-13 11:36:06 +0000124
hailfinger90c7d542010-05-31 15:27:27 +0000125#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000126 {
hailfinger0d703d42011-03-07 01:08:09 +0000127 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000128 .name = "nicrealtek",
129 //.name = "nicsmc1211",
130 .init = nicrealtek_init,
131 .map_flash_region = fallback_map,
132 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000133 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000134 },
hailfinger5aa36982010-05-21 21:54:07 +0000135#endif
136
hailfingerf0a368f2010-06-07 22:37:54 +0000137#if CONFIG_NICNATSEMI == 1
138 {
uwe8d342eb2011-07-28 08:13:25 +0000139 .name = "nicnatsemi",
140 .init = nicnatsemi_init,
141 .map_flash_region = fallback_map,
142 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000143 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000144 },
145#endif
hailfinger5aa36982010-05-21 21:54:07 +0000146
hailfinger90c7d542010-05-31 15:27:27 +0000147#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000148 {
149 .name = "gfxnvidia",
150 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000151 .map_flash_region = fallback_map,
152 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000153 .delay = internal_delay,
154 },
155#endif
156
hailfinger90c7d542010-05-31 15:27:27 +0000157#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000158 {
uwee2f95ef2009-09-02 23:00:46 +0000159 .name = "drkaiser",
160 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000161 .map_flash_region = fallback_map,
162 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000163 .delay = internal_delay,
164 },
hailfinger571a6b32009-09-16 10:09:21 +0000165#endif
uwee2f95ef2009-09-02 23:00:46 +0000166
hailfinger90c7d542010-05-31 15:27:27 +0000167#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000168 {
hailfinger3548a9a2009-08-12 14:34:35 +0000169 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000170 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000171 .map_flash_region = fallback_map,
172 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000173 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000174 },
hailfinger571a6b32009-09-16 10:09:21 +0000175#endif
ruikda922a12009-05-17 19:39:27 +0000176
hailfinger90c7d542010-05-31 15:27:27 +0000177#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000178 {
179 .name = "atahpt",
180 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000181 .map_flash_region = fallback_map,
182 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000183 .delay = internal_delay,
184 },
185#endif
186
hailfinger90c7d542010-05-31 15:27:27 +0000187#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000188 {
hailfinger90c7d542010-05-31 15:27:27 +0000189 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000190 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000191 .map_flash_region = fallback_map,
192 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000193 .delay = internal_delay,
194 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000195#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000196
hailfinger90c7d542010-05-31 15:27:27 +0000197#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000198 {
hailfinger3548a9a2009-08-12 14:34:35 +0000199 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000200 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000201 .map_flash_region = fallback_map,
202 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000203 .delay = serprog_delay,
204 },
hailfinger74d88a72009-08-12 16:17:41 +0000205#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000206
hailfinger90c7d542010-05-31 15:27:27 +0000207#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000208 {
hailfinger90c7d542010-05-31 15:27:27 +0000209 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000210 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000211 .map_flash_region = fallback_map,
212 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000213 .delay = internal_delay,
214 },
215#endif
216
Anton Staafb2647882014-09-17 15:13:43 -0700217#if CONFIG_RAIDEN_DEBUG_SPI == 1
218 {
219 .name = "raiden_debug_spi",
220 .init = raiden_debug_spi_init,
221 .map_flash_region = fallback_map,
222 .unmap_flash_region = fallback_unmap,
223 .delay = internal_delay,
224 },
225#endif
226
hailfinger90c7d542010-05-31 15:27:27 +0000227#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000228 {
229 .name = "dediprog",
230 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000231 .map_flash_region = fallback_map,
232 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000233 .delay = internal_delay,
234 },
235#endif
236
hailfinger52c4fa02010-07-21 10:26:01 +0000237#if CONFIG_RAYER_SPI == 1
238 {
239 .name = "rayer_spi",
240 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000241 .map_flash_region = fallback_map,
242 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000243 .delay = internal_delay,
244 },
245#endif
246
hailfinger7949b652011-05-08 00:24:18 +0000247#if CONFIG_NICINTEL == 1
248 {
249 .name = "nicintel",
250 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000251 .map_flash_region = fallback_map,
252 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000253 .delay = internal_delay,
254 },
255#endif
256
uwe6764e922010-09-03 18:21:21 +0000257#if CONFIG_NICINTEL_SPI == 1
258 {
uwe8d342eb2011-07-28 08:13:25 +0000259 .name = "nicintel_spi",
260 .init = nicintel_spi_init,
261 .map_flash_region = fallback_map,
262 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000263 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000264 },
265#endif
266
hailfingerfb1f31f2010-12-03 14:48:11 +0000267#if CONFIG_OGP_SPI == 1
268 {
uwe8d342eb2011-07-28 08:13:25 +0000269 .name = "ogp_spi",
270 .init = ogp_spi_init,
271 .map_flash_region = fallback_map,
272 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000273 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000274 },
275#endif
276
hailfinger935365d2011-02-04 21:37:59 +0000277#if CONFIG_SATAMV == 1
278 {
279 .name = "satamv",
280 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000281 .map_flash_region = fallback_map,
282 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000283 .delay = internal_delay,
284 },
285#endif
286
David Hendrickscebee892015-05-23 20:30:30 -0700287#if CONFIG_LINUX_MTD == 1
288 {
289 .name = "linux_mtd",
290 .init = linux_mtd_init,
291 .map_flash_region = fallback_map,
292 .unmap_flash_region = fallback_unmap,
293 .delay = internal_delay,
294 },
295#endif
296
uwe7df6dda2011-09-03 18:37:52 +0000297#if CONFIG_LINUX_SPI == 1
298 {
299 .name = "linux_spi",
300 .init = linux_spi_init,
301 .map_flash_region = fallback_map,
302 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000303 .delay = internal_delay,
304 },
305#endif
306
hailfinger3548a9a2009-08-12 14:34:35 +0000307 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000308};
stepan927d4e22007-04-04 22:45:58 +0000309
David Hendricksbf36f092010-11-02 23:39:29 -0700310#define CHIP_RESTORE_MAXFN 4
311static int chip_restore_fn_count = 0;
312struct chip_restore_func_data {
313 CHIP_RESTORE_CALLBACK;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700314 struct flashctx *flash;
David Hendricksbf36f092010-11-02 23:39:29 -0700315 uint8_t status;
316} static chip_restore_fn[CHIP_RESTORE_MAXFN];
317
David Hendricks668f29d2011-01-27 18:51:45 -0800318
hailfingerf31cbdc2010-11-10 15:25:18 +0000319#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000320static int shutdown_fn_count = 0;
321struct shutdown_func_data {
Souvik Ghosh052210a2016-07-11 12:29:05 -0700322 int (*func) (struct flashctx *flash, void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000323 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000324} static shutdown_fn[SHUTDOWN_MAXFN];
325/* Initialize to 0 to make sure nobody registers a shutdown function before
326 * programmer init.
327 */
328static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000329
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700330static int check_block_eraser(const struct flashctx *flash, int k, int log);
stefanct569dbb62011-07-01 00:19:12 +0000331
hailfingerdc6f7972010-02-14 01:20:28 +0000332/* Register a function to be executed on programmer shutdown.
333 * The advantage over atexit() is that you can supply a void pointer which will
334 * be used as parameter to the registered function upon programmer shutdown.
335 * This pointer can point to arbitrary data used by said function, e.g. undo
336 * information for GPIO settings etc. If unneeded, set data=NULL.
337 * Please note that the first (void *data) belongs to the function signature of
338 * the function passed as first parameter.
339 */
Souvik Ghosh052210a2016-07-11 12:29:05 -0700340int register_shutdown(int (*function) (struct flashctx *flash, void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000341{
342 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000343 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000344 SHUTDOWN_MAXFN);
345 return 1;
346 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000347 if (!may_register_shutdown) {
348 msg_perr("Tried to register a shutdown function before "
349 "programmer init.\n");
350 return 1;
351 }
hailfingerdc6f7972010-02-14 01:20:28 +0000352 shutdown_fn[shutdown_fn_count].func = function;
353 shutdown_fn[shutdown_fn_count].data = data;
354 shutdown_fn_count++;
355
356 return 0;
357}
358
David Hendricksbf36f092010-11-02 23:39:29 -0700359//int register_chip_restore(int (*function) (void *data), void *data)
360int register_chip_restore(CHIP_RESTORE_CALLBACK,
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700361 struct flashctx *flash, uint8_t status)
David Hendricksbf36f092010-11-02 23:39:29 -0700362{
363 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
364 msg_perr("Tried to register more than %i chip restore"
365 " functions.\n", CHIP_RESTORE_MAXFN);
366 return 1;
367 }
368 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
369 chip_restore_fn[chip_restore_fn_count].flash = flash;
370 chip_restore_fn[chip_restore_fn_count].status = status;
371 chip_restore_fn_count++;
372
373 return 0;
374}
375
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700376int programmer_init(struct flashctx *flash, enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000377{
hailfinger1ef766d2010-07-06 09:55:48 +0000378 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000379
380 if (prog >= PROGRAMMER_INVALID) {
381 msg_perr("Invalid programmer specified!\n");
382 return -1;
383 }
384 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000385 /* Initialize all programmer specific data. */
386 /* Default to unlimited decode sizes. */
387 max_rom_decode = (const struct decode_sizes) {
388 .parallel = 0xffffffff,
389 .lpc = 0xffffffff,
390 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000391 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000392 };
hailfinger1ff33dc2010-07-03 11:02:10 +0000393 /* Default to top aligned flash at 4 GB. */
394 flashbase = 0;
395 /* Registering shutdown functions is now allowed. */
396 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000397 /* Default to allowing writes. Broken programmers set this to 0. */
398 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000399
400 programmer_param = param;
401 msg_pdbg("Initializing %s programmer\n",
402 programmer_table[programmer].name);
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700403 ret = programmer_table[programmer].init(flash);
hailfinger1ef766d2010-07-06 09:55:48 +0000404 if (programmer_param && strlen(programmer_param)) {
405 msg_perr("Unhandled programmer parameters: %s\n",
406 programmer_param);
407 /* Do not error out here, the init itself was successful. */
408 }
409 return ret;
uweabe92a52009-05-16 22:36:00 +0000410}
411
David Hendricksbf36f092010-11-02 23:39:29 -0700412int chip_restore()
413{
414 int rc = 0;
415
416 while (chip_restore_fn_count > 0) {
417 int i = --chip_restore_fn_count;
418 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
419 chip_restore_fn[i].status);
420 }
421
422 return rc;
423}
424
Souvik Ghosh052210a2016-07-11 12:29:05 -0700425int programmer_shutdown(struct flashctx *flash)
uweabe92a52009-05-16 22:36:00 +0000426{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000427 int ret = 0;
428
hailfinger1ff33dc2010-07-03 11:02:10 +0000429 /* Registering shutdown functions is no longer allowed. */
430 may_register_shutdown = 0;
431 while (shutdown_fn_count > 0) {
432 int i = --shutdown_fn_count;
Souvik Ghosh052210a2016-07-11 12:29:05 -0700433 ret |= shutdown_fn[i].func(flash, shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000434 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000435 return ret;
uweabe92a52009-05-16 22:36:00 +0000436}
437
438void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
439 size_t len)
440{
441 return programmer_table[programmer].map_flash_region(descr,
442 phys_addr, len);
443}
444
445void programmer_unmap_flash_region(void *virt_addr, size_t len)
446{
447 programmer_table[programmer].unmap_flash_region(virt_addr, len);
448}
449
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700450void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000451{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700452 flash->pgm->par.chip_writeb(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000453}
454
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700455void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000456{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700457 flash->pgm->par.chip_writew(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000458}
459
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700460void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000461{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700462 flash->pgm->par.chip_writel(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000463}
464
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700465void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000466{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700467 flash->pgm->par.chip_writen(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000468}
469
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700470uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000471{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700472 return flash->pgm->par.chip_readb(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000473}
474
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700475uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000476{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700477 return flash->pgm->par.chip_readw(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000478}
479
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700480uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000481{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700482 return flash->pgm->par.chip_readl(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000483}
484
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700485void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000486{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700487 flash->pgm->par.chip_readn(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000488}
489
hailfingere5829f62009-06-05 17:48:08 +0000490void programmer_delay(int usecs)
491{
492 programmer_table[programmer].delay(usecs);
493}
494
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700495void map_flash_registers(struct flashctx *flash)
stepan15e64bc2007-05-24 08:48:10 +0000496{
stepan15e64bc2007-05-24 08:48:10 +0000497 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000498 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000499 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000500 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000501}
502
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700503int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000504{
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700505 chip_readn(flash, buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000506
hailfinger23060112009-05-08 12:49:03 +0000507 return 0;
508}
509
hailfinger7b414742009-06-13 12:04:03 +0000510int min(int a, int b)
511{
512 return (a < b) ? a : b;
513}
514
hailfinger7af83692009-06-15 17:23:36 +0000515int max(int a, int b)
516{
517 return (a > b) ? a : b;
518}
519
hailfingeraec9c962009-10-31 01:53:09 +0000520int bitcount(unsigned long a)
521{
522 int i = 0;
523 for (; a != 0; a >>= 1)
524 if (a & 1)
525 i++;
526 return i;
527}
528
hailfingerf76cc322010-11-09 22:00:31 +0000529void tolower_string(char *str)
530{
531 for (; *str != '\0'; str++)
532 *str = (char)tolower((unsigned char)*str);
533}
534
hailfingera916b422009-06-01 02:08:58 +0000535char *strcat_realloc(char *dest, const char *src)
536{
537 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000538 if (!dest) {
539 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000540 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000541 }
hailfingera916b422009-06-01 02:08:58 +0000542 strcat(dest, src);
543 return dest;
544}
545
hailfinger6e5a52a2009-11-24 18:27:10 +0000546/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000547 * It will look for needle with a subsequent '=' in haystack, return a copy of
548 * needle and remove everything from the first occurrence of needle to the next
549 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000550 */
stefanct52700282011-06-26 17:38:17 +0000551char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000552{
hailfinger1ef766d2010-07-06 09:55:48 +0000553 char *param_pos, *opt_pos, *rest;
554 char *opt = NULL;
555 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000556 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000557
hailfingerf4aaccc2010-04-28 15:22:14 +0000558 needlelen = strlen(needle);
559 if (!needlelen) {
560 msg_gerr("%s: empty needle! Please report a bug at "
561 "flashrom@flashrom.org\n", __func__);
562 return NULL;
563 }
564 /* No programmer parameters given. */
565 if (*haystack == NULL)
566 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000567 param_pos = strstr(*haystack, needle);
568 do {
569 if (!param_pos)
570 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000571 /* Needle followed by '='? */
572 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600573
hailfinger1ef766d2010-07-06 09:55:48 +0000574 /* Beginning of the string? */
575 if (param_pos == *haystack)
576 break;
577 /* After a delimiter? */
578 if (strchr(delim, *(param_pos - 1)))
579 break;
580 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000581 /* Continue searching. */
582 param_pos++;
583 param_pos = strstr(param_pos, needle);
584 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000585
hailfinger6e5a52a2009-11-24 18:27:10 +0000586 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000587 /* Get the string after needle and '='. */
588 opt_pos = param_pos + needlelen + 1;
589 optlen = strcspn(opt_pos, delim);
590 /* Return an empty string if the parameter was empty. */
591 opt = malloc(optlen + 1);
592 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000593 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000594 exit(1);
595 }
hailfinger1ef766d2010-07-06 09:55:48 +0000596 strncpy(opt, opt_pos, optlen);
597 opt[optlen] = '\0';
598 rest = opt_pos + optlen;
599 /* Skip all delimiters after the current parameter. */
600 rest += strspn(rest, delim);
601 memmove(param_pos, rest, strlen(rest) + 1);
602 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000603 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000604
hailfinger1ef766d2010-07-06 09:55:48 +0000605 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000606}
607
stefanct52700282011-06-26 17:38:17 +0000608char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000609{
610 return extract_param(&programmer_param, param_name, ",");
611}
612
stefancte1c5acf2011-07-04 07:27:17 +0000613/* Returns the number of well-defined erasers for a chip. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700614static unsigned int count_usable_erasers(const struct flashctx *flash)
stefanct569dbb62011-07-01 00:19:12 +0000615{
616 unsigned int usable_erasefunctions = 0;
617 int k;
618 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
619 if (!check_block_eraser(flash, k, 0))
620 usable_erasefunctions++;
621 }
622 return usable_erasefunctions;
623}
624
hailfinger7af83692009-06-15 17:23:36 +0000625/* start is an offset to the base address of the flash chip */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700626int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000627{
628 int ret;
629 uint8_t *cmpbuf = malloc(len);
630
631 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000632 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000633 exit(1);
634 }
Simon Glass4c214132013-07-16 10:09:28 -0600635 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000636 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
637 free(cmpbuf);
638 return ret;
639}
640
Simon Glass4e305f42015-01-08 06:29:04 -0700641static int compare_chunk(uint8_t *readbuf, uint8_t *cmpbuf, unsigned int start,
642 unsigned int len, const char *message)
643{
644 int failcount = 0, i;
645
646 for (i = 0; i < len; i++) {
647 if (cmpbuf[i] != readbuf[i]) {
648 if (!failcount) {
649 msg_cerr("%s FAILED at 0x%08x! "
650 "Expected=0x%02x, Read=0x%02x,",
651 message, start + i,
652 cmpbuf[i], readbuf[i]);
653 }
654 failcount++;
655 }
656 }
657
658 return failcount;
659}
660
uwee15beb92010-08-08 17:01:18 +0000661/*
hailfinger7af3d192009-11-25 17:05:52 +0000662 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000663 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000664 * @start offset to the base address of the flash chip
665 * @len length of the verified area
666 * @message string to print in the "FAILED" message
667 * @return 0 for success, -1 for failure
668 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700669int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000670 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000671{
hailfinger8cb6ece2010-11-16 17:21:58 +0000672 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000673 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000674
675 if (!len)
676 goto out_free;
677
hailfingerb0f4d122009-06-24 08:20:45 +0000678 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000679 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000680 return 1;
681 }
hailfinger7af83692009-06-15 17:23:36 +0000682 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000683 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000684 exit(1);
685 }
686
687 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000688 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000689 " total_size 0x%x\n", __func__, start, len,
690 flash->total_size * 1024);
691 ret = -1;
692 goto out_free;
693 }
694 if (!message)
695 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000696
Simon Glass4e305f42015-01-08 06:29:04 -0700697 if (programmer_table[programmer].paranoid) {
698 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800699
Simon Glass4e305f42015-01-08 06:29:04 -0700700 /* limit chunksize in order to catch errors early */
701 for (i = 0, chunksize = 0; i < len; i += chunksize) {
702 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800703
Simon Glass4e305f42015-01-08 06:29:04 -0700704 chunksize = min(flash->page_size, len - i);
705 tmp = flash->read(flash, readbuf + i, start + i, chunksize);
706 if (tmp) {
707 ret = tmp;
708 if (ignore_error(tmp))
709 continue;
710 else
711 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800712 }
Simon Glass4e305f42015-01-08 06:29:04 -0700713
714 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
715 chunksize, message);
716 if (failcount)
717 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800718 }
Simon Glass4e305f42015-01-08 06:29:04 -0700719 } else {
720 int tmp;
721
722 /* read as much as we can to reduce transaction overhead */
723 tmp = flash->read(flash, readbuf, start, len);
724 if (tmp && !ignore_error(tmp)) {
725 ret = tmp;
726 goto out_free;
727 }
728
729 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000730 }
731
hailfinger5be6c0f2009-07-23 01:42:56 +0000732 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000733 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000734 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000735 ret = -1;
736 }
hailfinger7af83692009-06-15 17:23:36 +0000737
738out_free:
739 free(readbuf);
740 return ret;
741}
742
uwee15beb92010-08-08 17:01:18 +0000743/*
hailfingerb247c7a2010-03-08 00:42:32 +0000744 * Check if the buffer @have can be programmed to the content of @want without
745 * erasing. This is only possible if all chunks of size @gran are either kept
746 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000747 *
hailfingerb247c7a2010-03-08 00:42:32 +0000748 * The following write granularities (enum @gran) are known:
749 * - 1 bit. Each bit can be cleared individually.
750 * - 1 byte. A byte can be written once. Further writes to an already written
751 * byte cause the contents to be either undefined or to stay unchanged.
752 * - 128 bytes. If less than 128 bytes are written, the rest will be
753 * erased. Each write to a 128-byte region will trigger an automatic erase
754 * before anything is written. Very uncommon behaviour and unsupported by
755 * this function.
756 * - 256 bytes. If less than 256 bytes are written, the contents of the
757 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000758 * Warning: This function assumes that @have and @want point to naturally
759 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000760 *
761 * @have buffer with current content
762 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000763 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000764 * @gran write granularity (enum, not count)
765 * @return 0 if no erase is needed, 1 otherwise
766 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700767static int need_erase(struct flashctx *flash, uint8_t *have, uint8_t *want,
Simon Glass4c214132013-07-16 10:09:28 -0600768 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000769{
hailfingerb91c08c2011-08-15 19:54:20 +0000770 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000771 unsigned int i, j, limit;
Simon Glass4c214132013-07-16 10:09:28 -0600772 int erase_value = flash_erase_value(flash);
hailfingerb247c7a2010-03-08 00:42:32 +0000773
774 switch (gran) {
775 case write_gran_1bit:
776 for (i = 0; i < len; i++)
777 if ((have[i] & want[i]) != want[i]) {
778 result = 1;
779 break;
780 }
781 break;
782 case write_gran_1byte:
783 for (i = 0; i < len; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600784 if ((have[i] != want[i]) && (have[i] != erase_value)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000785 result = 1;
786 break;
787 }
788 break;
789 case write_gran_256bytes:
790 for (j = 0; j < len / 256; j++) {
791 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000792 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000793 if (!memcmp(have + j * 256, want + j * 256, limit))
794 continue;
795 /* have needs to be in erased state. */
796 for (i = 0; i < limit; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600797 if (have[j * 256 + i] != erase_value) {
hailfingerb247c7a2010-03-08 00:42:32 +0000798 result = 1;
799 break;
800 }
801 if (result)
802 break;
803 }
804 break;
hailfingerb437e282010-11-04 01:04:27 +0000805 default:
806 msg_cerr("%s: Unsupported granularity! Please report a bug at "
807 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000808 }
809 return result;
810}
811
hailfingerb437e282010-11-04 01:04:27 +0000812/**
813 * Check if the buffer @have needs to be programmed to get the content of @want.
814 * If yes, return 1 and fill in first_start with the start address of the
815 * write operation and first_len with the length of the first to-be-written
816 * chunk. If not, return 0 and leave first_start and first_len undefined.
817 *
818 * Warning: This function assumes that @have and @want point to naturally
819 * aligned regions.
820 *
821 * @have buffer with current content
822 * @want buffer with desired content
823 * @len length of the checked area
824 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000825 * @first_start offset of the first byte which needs to be written (passed in
826 * value is increased by the offset of the first needed write
827 * relative to have/want or unchanged if no write is needed)
828 * @return length of the first contiguous area which needs to be written
829 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000830 *
831 * FIXME: This function needs a parameter which tells it about coalescing
832 * in relation to the max write length of the programmer and the max write
833 * length of the chip.
834 */
stefanctc5eb8a92011-11-23 09:13:48 +0000835static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
836 unsigned int *first_start,
837 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000838{
stefanctc5eb8a92011-11-23 09:13:48 +0000839 int need_write = 0;
840 unsigned int rel_start = 0, first_len = 0;
841 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000842
hailfingerb437e282010-11-04 01:04:27 +0000843 switch (gran) {
844 case write_gran_1bit:
845 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000846 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000847 break;
848 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000849 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000850 break;
851 default:
852 msg_cerr("%s: Unsupported granularity! Please report a bug at "
853 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000854 /* Claim that no write was needed. A write with unknown
855 * granularity is too dangerous to try.
856 */
857 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000858 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000859 for (i = 0; i < len / stride; i++) {
860 limit = min(stride, len - i * stride);
861 /* Are 'have' and 'want' identical? */
862 if (memcmp(have + i * stride, want + i * stride, limit)) {
863 if (!need_write) {
864 /* First location where have and want differ. */
865 need_write = 1;
866 rel_start = i * stride;
867 }
868 } else {
869 if (need_write) {
870 /* First location where have and want
871 * do not differ anymore.
872 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000873 break;
874 }
875 }
876 }
hailfingerffb7f382010-12-06 13:05:44 +0000877 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000878 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000879 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000880 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000881}
882
hailfinger0c515352009-11-23 12:55:31 +0000883/* This function generates various test patterns useful for testing controller
884 * and chip communication as well as chip behaviour.
885 *
886 * If a byte can be written multiple times, each time keeping 0-bits at 0
887 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
888 * is essentially an AND operation. That's also the reason why this function
889 * provides the result of AND between various patterns.
890 *
891 * Below is a list of patterns (and their block length).
892 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
893 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
894 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
895 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
896 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
897 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
898 * Pattern 6 is 00 (1 Byte)
899 * Pattern 7 is ff (1 Byte)
900 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
901 * byte block.
902 *
903 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
904 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
905 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
906 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
907 * Pattern 12 is 00 (1 Byte)
908 * Pattern 13 is ff (1 Byte)
909 * Patterns 8-13 have no block number.
910 *
911 * Patterns 0-3 are created to detect and efficiently diagnose communication
912 * slips like missed bits or bytes and their repetitive nature gives good visual
913 * cues to the person inspecting the results. In addition, the following holds:
914 * AND Pattern 0/1 == Pattern 4
915 * AND Pattern 2/3 == Pattern 5
916 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
917 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
918 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
919 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
920 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
921 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
922 * Besides that, they provide for bit testing of the last two bytes of every
923 * 256 byte block which contains the block number for patterns 0-6.
924 * Patterns 10-11 are special purpose for detecting subblock aliasing with
925 * block sizes >256 bytes (some Dataflash chips etc.)
926 * AND Pattern 8/9 == Pattern 12
927 * AND Pattern 10/11 == Pattern 12
928 * Pattern 13 is the completely erased state.
929 * None of the patterns can detect aliasing at boundaries which are a multiple
930 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
931 */
932int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
933{
934 int i;
935
936 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000937 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000938 return 1;
939 }
940
941 switch (variant) {
942 case 0:
943 for (i = 0; i < size; i++)
944 buf[i] = (i & 0xf) << 4 | 0x5;
945 break;
946 case 1:
947 for (i = 0; i < size; i++)
948 buf[i] = (i & 0xf) << 4 | 0xa;
949 break;
950 case 2:
951 for (i = 0; i < size; i++)
952 buf[i] = 0x50 | (i & 0xf);
953 break;
954 case 3:
955 for (i = 0; i < size; i++)
956 buf[i] = 0xa0 | (i & 0xf);
957 break;
958 case 4:
959 for (i = 0; i < size; i++)
960 buf[i] = (i & 0xf) << 4;
961 break;
962 case 5:
963 for (i = 0; i < size; i++)
964 buf[i] = i & 0xf;
965 break;
966 case 6:
967 memset(buf, 0x00, size);
968 break;
969 case 7:
970 memset(buf, 0xff, size);
971 break;
972 case 8:
973 for (i = 0; i < size; i++)
974 buf[i] = i & 0xff;
975 break;
976 case 9:
977 for (i = 0; i < size; i++)
978 buf[i] = ~(i & 0xff);
979 break;
980 case 10:
981 for (i = 0; i < size % 2; i++) {
982 buf[i * 2] = (i >> 8) & 0xff;
983 buf[i * 2 + 1] = i & 0xff;
984 }
985 if (size & 0x1)
986 buf[i * 2] = (i >> 8) & 0xff;
987 break;
988 case 11:
989 for (i = 0; i < size % 2; i++) {
990 buf[i * 2] = ~((i >> 8) & 0xff);
991 buf[i * 2 + 1] = ~(i & 0xff);
992 }
993 if (size & 0x1)
994 buf[i * 2] = ~((i >> 8) & 0xff);
995 break;
996 case 12:
997 memset(buf, 0x00, size);
998 break;
999 case 13:
1000 memset(buf, 0xff, size);
1001 break;
1002 }
1003
1004 if ((variant >= 0) && (variant <= 7)) {
1005 /* Write block number in the last two bytes of each 256-byte
1006 * block, big endian for easier reading of the hexdump.
1007 * Note that this wraps around for chips larger than 2^24 bytes
1008 * (16 MB).
1009 */
1010 for (i = 0; i < size / 256; i++) {
1011 buf[i * 256 + 254] = (i >> 8) & 0xff;
1012 buf[i * 256 + 255] = i & 0xff;
1013 }
1014 }
1015
1016 return 0;
1017}
1018
hailfingeraec9c962009-10-31 01:53:09 +00001019int check_max_decode(enum chipbustype buses, uint32_t size)
1020{
1021 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001022
1023 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001024 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001025 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001026 "size %u kB of chipset/board/programmer "
1027 "for %s interface, "
1028 "probe/read/erase/write may fail. ", size / 1024,
1029 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001030 }
hailfingere1e41ea2011-07-27 07:13:06 +00001031 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001032 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001033 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001034 "size %u kB of chipset/board/programmer "
1035 "for %s interface, "
1036 "probe/read/erase/write may fail. ", size / 1024,
1037 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001038 }
hailfingere1e41ea2011-07-27 07:13:06 +00001039 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001040 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001041 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001042 "size %u kB of chipset/board/programmer "
1043 "for %s interface, "
1044 "probe/read/erase/write may fail. ", size / 1024,
1045 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001046 }
hailfingere1e41ea2011-07-27 07:13:06 +00001047 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001048 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001049 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001050 "size %u kB of chipset/board/programmer "
1051 "for %s interface, "
1052 "probe/read/erase/write may fail. ", size / 1024,
1053 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001054 }
1055 if (!limitexceeded)
1056 return 0;
1057 /* Sometimes chip and programmer have more than one bus in common,
1058 * and the limit is not exceeded on all buses. Tell the user.
1059 */
1060 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001061 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001062 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001063 "interface which can support a chip of this size. "
1064 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001065 return 1;
1066}
1067
Souvik Ghosh63b92f92016-06-29 18:45:52 -07001068int probe_flash(struct registered_programmer *pgm, int startchip,
1069 struct flashctx *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001070{
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301071 const struct flashchip *flash, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001072 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001073 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001074 uint32_t size;
1075 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001076 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001077
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301078 /* Based on the host controller interface that a platform
1079 * needs to use (hwseq or swseq),
1080 * set the flashchips list here.
1081 */
1082 switch (ich_generation) {
1083 case CHIPSET_100_SERIES_SUNRISE_POINT:
Furquan Shaikh44088752016-07-11 22:48:08 -07001084 case CHIPSET_APL:
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301085 flash_list = flashchips_hwseq;
1086 break;
1087 default:
1088 flash_list = flashchips;
1089 break;
1090 }
1091
1092 for (flash = flash_list + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001093 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001094 continue;
Souvik Ghosh63b92f92016-06-29 18:45:52 -07001095 buses_common = pgm->buses_supported & flash->bustype;
1096 if (!buses_common)
hailfinger18bd4cc2011-06-17 22:38:53 +00001097 continue;
hailfinger18bd4cc2011-06-17 22:38:53 +00001098 msg_gdbg("Probing for %s %s, %d kB: ",
1099 flash->vendor, flash->name, flash->total_size);
1100 if (!flash->probe && !force) {
1101 msg_gdbg("failed! flashrom has no probe function for "
1102 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001103 continue;
1104 }
stepan782fb172007-04-06 11:58:03 +00001105
ollie5672ac62004-03-17 22:22:08 +00001106 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001107 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001108
hailfinger48ed3e22011-05-04 00:39:50 +00001109 /* Start filling in the dynamic data. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001110 memcpy(fill_flash, flash, sizeof(struct flashchip));
Souvik Ghosh63b92f92016-06-29 18:45:52 -07001111 fill_flash->pgm = pgm;
hailfinger48ed3e22011-05-04 00:39:50 +00001112
hailfinger72d3b982009-05-09 07:27:23 +00001113 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001114 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001115
stugec1e55fe2008-07-02 17:15:47 +00001116 if (force)
1117 break;
stepanc98b80b2006-03-16 16:57:41 +00001118
hailfinger48ed3e22011-05-04 00:39:50 +00001119 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001120 goto notfound;
1121
hailfinger48ed3e22011-05-04 00:39:50 +00001122 /* If this is the first chip found, accept it.
1123 * If this is not the first chip found, accept it only if it is
1124 * a non-generic match.
1125 * We could either make chipcount global or provide it as
1126 * parameter, or we assume that startchip==0 means this call to
1127 * probe_flash() is the first one and thus no chip has been
1128 * found before.
1129 */
1130 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001131 break;
1132
stuge56300c32008-09-03 23:10:05 +00001133notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001134 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001135 }
uwebe4477b2007-08-23 16:08:21 +00001136
stugec1e55fe2008-07-02 17:15:47 +00001137 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001138 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001139
hailfingere11396b2011-03-08 00:09:11 +00001140#if CONFIG_INTERNAL == 1
1141 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001142 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001143 else
1144#endif
stepan3e7aeae2011-01-19 06:21:54 +00001145 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001146
stefanct588b6d22011-06-26 20:45:35 +00001147 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001148 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1149 force ? "Assuming" : "Found", fill_flash->vendor,
1150 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001151 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001152
hailfinger0f4c3952010-12-02 21:59:42 +00001153 /* Flash registers will not be mapped if the chip was forced. Lock info
1154 * may be stored in registers, so avoid lock info printing.
1155 */
1156 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001157 if (fill_flash->printlock)
1158 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001159
hailfinger48ed3e22011-05-04 00:39:50 +00001160 /* Return position of matching chip. */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301161 return flash - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001162}
1163
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001164int verify_flash(struct flashctx *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001165{
hailfingerb0f4d122009-06-24 08:20:45 +00001166 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001167 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001168
snelsone42c3802010-05-07 20:09:04 +00001169 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001170
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001171 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1172 ret = handle_partial_verify(flash, buf, verify_range);
1173 } else {
1174 ret = verify_range(flash, buf, 0, total_size, NULL);
1175 }
uwef6641642007-05-09 10:17:44 +00001176
David Hendricks1ed1d352011-11-23 17:54:37 -08001177 if (ret == ACCESS_DENIED) {
1178 msg_gdbg("Could not fully verify due to access error, ");
1179 if (access_denied_action == error_ignore) {
1180 msg_gdbg("ignoring\n");
1181 ret = 0;
1182 } else {
1183 msg_gdbg("aborting\n");
1184 }
1185 }
1186
hailfingerb0f4d122009-06-24 08:20:45 +00001187 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001188 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001189
hailfingerb0f4d122009-06-24 08:20:45 +00001190 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001191}
1192
uwe8d342eb2011-07-28 08:13:25 +00001193int read_buf_from_file(unsigned char *buf, unsigned long size,
1194 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001195{
1196 unsigned long numbytes;
1197 FILE *image;
1198 struct stat image_stat;
1199
Vincent Palatin7ab23932014-10-01 12:09:16 -07001200 if (!strncmp(filename, "-", sizeof("-")))
1201 image = fdopen(STDIN_FILENO, "rb");
1202 else
1203 image = fopen(filename, "rb");
1204 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001205 perror(filename);
1206 return 1;
1207 }
1208 if (fstat(fileno(image), &image_stat) != 0) {
1209 perror(filename);
1210 fclose(image);
1211 return 1;
1212 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001213 if ((image_stat.st_size != size) &&
1214 (strncmp(filename, "-", sizeof("-")))) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001215 msg_gerr("Error: Image size doesn't match: stat %ld bytes, "
1216 "wanted %ld!\n", image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001217 fclose(image);
1218 return 1;
1219 }
1220 numbytes = fread(buf, 1, size, image);
1221 if (fclose(image)) {
1222 perror(filename);
1223 return 1;
1224 }
1225 if (numbytes != size) {
1226 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1227 "wanted %ld!\n", numbytes, size);
1228 return 1;
1229 }
1230 return 0;
1231}
1232
uwe8d342eb2011-07-28 08:13:25 +00001233int write_buf_to_file(unsigned char *buf, unsigned long size,
1234 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001235{
1236 unsigned long numbytes;
1237 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001238
1239 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001240 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001241 return 1;
1242 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001243 if (!strncmp(filename, "-", sizeof("-")))
1244 image = fdopen(STDOUT_FILENO, "wb");
1245 else
1246 image = fopen(filename, "wb");
1247 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001248 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001249 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001250 }
hailfingerd219a232009-01-28 00:27:54 +00001251
hailfingerd219a232009-01-28 00:27:54 +00001252 numbytes = fwrite(buf, 1, size, image);
1253 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001254 if (numbytes != size) {
1255 msg_gerr("File %s could not be written completely.\n",
1256 filename);
hailfingerd219a232009-01-28 00:27:54 +00001257 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001258 }
hailfingerd219a232009-01-28 00:27:54 +00001259 return 0;
1260}
1261
David Hendrickse3451942013-03-21 17:23:29 -07001262/*
1263 * read_flash - wrapper for flash->read() with additional high-level policy
1264 *
1265 * @flash flash chip
1266 * @buf buffer to store data in
1267 * @start start address
1268 * @len number of bytes to read
1269 *
1270 * This wrapper simplifies most cases when the flash chip needs to be read
1271 * since policy decisions such as non-fatal error handling is centralized.
1272 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001273int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001274 unsigned int start, unsigned int len)
1275{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001276 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001277
1278 if (!flash || !flash->read)
1279 return -1;
1280
David Hendricks4e76fdc2013-05-13 16:05:36 -07001281 ret = flash->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001282 if (ret) {
1283 if (ignore_error(ret)) {
1284 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1285 start, start + len - 1);
1286 ret = 0;
1287 } else {
1288 msg_gdbg("failed to read 0x%x-0x%x\n",
1289 start, start + len - 1);
1290 }
1291 }
1292
1293 return ret;
1294}
1295
David Hendricks7c8a1612013-04-26 19:14:44 -07001296/*
1297 * write_flash - wrapper for flash->write() with additional high-level policy
1298 *
1299 * @flash flash chip
1300 * @buf buffer to write to flash
1301 * @start start address in flash
1302 * @len number of bytes to write
1303 *
1304 * TODO: Look up regions that are write-protected and avoid attempt to write
1305 * to them at all.
1306 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001307int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001308 unsigned int start, unsigned int len)
1309{
1310 if (!flash || !flash->write)
1311 return -1;
1312
1313 return flash->write(flash, buf, start, len);
1314}
1315
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001316int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001317{
1318 unsigned long size = flash->total_size * 1024;
1319 unsigned char *buf = calloc(size, sizeof(char));
1320 int ret = 0;
1321
1322 msg_cinfo("Reading flash... ");
1323 if (!buf) {
1324 msg_gerr("Memory allocation failed!\n");
1325 msg_cinfo("FAILED.\n");
1326 return 1;
1327 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001328
1329 /* To support partial read, fill buffer to all 0xFF at beginning to make
1330 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001331 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001332
hailfinger42a850a2010-07-13 23:56:13 +00001333 if (!flash->read) {
1334 msg_cerr("No read function available for this flash chip.\n");
1335 ret = 1;
1336 goto out_free;
1337 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001338
1339 /* First try to handle partial read case, rather than read the whole
1340 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001341 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001342 if (ret < 0) {
1343 msg_cerr("Partial read operation failed!\n");
1344 ret = 1;
1345 goto out_free;
1346 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001347 int num_regions = get_num_include_args();
1348
1349 if (ret != num_regions) {
1350 msg_cerr("Requested %d regions, but only read %d\n",
1351 num_regions, ret);
1352 ret = 1;
1353 goto out_free;
1354 }
1355
1356 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001357 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001358 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001359 msg_cerr("Read operation failed!\n");
1360 ret = 1;
1361 goto out_free;
1362 }
hailfinger42a850a2010-07-13 23:56:13 +00001363 }
1364
David Hendricksdf29a832013-06-28 14:33:51 -07001365 if (filename)
1366 ret = write_buf_to_file(buf, size, filename);
1367
hailfinger42a850a2010-07-13 23:56:13 +00001368out_free:
1369 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001370 if (ret)
1371 msg_cerr("FAILED.");
1372 else
1373 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001374 return ret;
1375}
1376
hailfingerb437e282010-11-04 01:04:27 +00001377/* This function shares a lot of its structure with erase_and_write_flash() and
1378 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001379 * Even if an error is found, the function will keep going and check the rest.
1380 */
hailfinger48ed3e22011-05-04 00:39:50 +00001381static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001382{
hailfingerb91c08c2011-08-15 19:54:20 +00001383 int i, j, k;
1384 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001385
1386 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1387 unsigned int done = 0;
1388 struct block_eraser eraser = flash->block_erasers[k];
1389
1390 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1391 /* Blocks with zero size are bugs in flashchips.c. */
1392 if (eraser.eraseblocks[i].count &&
1393 !eraser.eraseblocks[i].size) {
1394 msg_gerr("ERROR: Flash chip %s erase function "
1395 "%i region %i has size 0. Please report"
1396 " a bug at flashrom@flashrom.org\n",
1397 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001398 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001399 }
1400 /* Blocks with zero count are bugs in flashchips.c. */
1401 if (!eraser.eraseblocks[i].count &&
1402 eraser.eraseblocks[i].size) {
1403 msg_gerr("ERROR: Flash chip %s erase function "
1404 "%i region %i has count 0. Please report"
1405 " a bug at flashrom@flashrom.org\n",
1406 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001407 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001408 }
1409 done += eraser.eraseblocks[i].count *
1410 eraser.eraseblocks[i].size;
1411 }
hailfinger9fed35d2010-01-19 06:42:46 +00001412 /* Empty eraseblock definition with erase function. */
1413 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001414 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001415 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001416 if (!done)
1417 continue;
1418 if (done != flash->total_size * 1024) {
1419 msg_gerr("ERROR: Flash chip %s erase function %i "
1420 "region walking resulted in 0x%06x bytes total,"
1421 " expected 0x%06x bytes. Please report a bug at"
1422 " flashrom@flashrom.org\n", flash->name, k,
1423 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001424 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001425 }
hailfinger9fed35d2010-01-19 06:42:46 +00001426 if (!eraser.block_erase)
1427 continue;
1428 /* Check if there are identical erase functions for different
1429 * layouts. That would imply "magic" erase functions. The
1430 * easiest way to check this is with function pointers.
1431 */
uwef6f94d42010-03-13 17:28:29 +00001432 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001433 if (eraser.block_erase ==
1434 flash->block_erasers[j].block_erase) {
1435 msg_gerr("ERROR: Flash chip %s erase function "
1436 "%i and %i are identical. Please report"
1437 " a bug at flashrom@flashrom.org\n",
1438 flash->name, k, j);
1439 ret = 1;
1440 }
uwef6f94d42010-03-13 17:28:29 +00001441 }
hailfinger45177872010-01-18 08:14:43 +00001442 }
hailfinger9fed35d2010-01-19 06:42:46 +00001443 return ret;
hailfinger45177872010-01-18 08:14:43 +00001444}
1445
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001446static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001447 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001448 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001449 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001450 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001451 unsigned int addr,
1452 unsigned int len))
1453{
stefanctc5eb8a92011-11-23 09:13:48 +00001454 unsigned int starthere = 0, lenhere = 0;
1455 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001456 int block_was_erased = 0;
hailfingerb437e282010-11-04 01:04:27 +00001457 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1458
hailfinger90fcf9b2010-11-05 14:51:59 +00001459 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001460 * need to be adjusted here to keep the impression of proper abstraction
1461 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001462 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001463 newcontents += start;
1464 msg_cdbg(":");
1465 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001466 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001467 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001468 msg_cdbg("E");
1469 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001470 if (ret) {
1471 if (ret == ACCESS_DENIED)
1472 msg_cdbg("D");
1473 else
1474 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001475 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001476 }
1477
David Hendricks0954ffc2015-11-13 15:15:44 -08001478 if (programmer_table[programmer].paranoid) {
1479 if (check_erased_range(flash, start, len)) {
1480 msg_cerr("ERASE FAILED!\n");
1481 return -1;
1482 }
hailfingerac8e3182011-06-26 17:04:16 +00001483 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001484
hailfinger90fcf9b2010-11-05 14:51:59 +00001485 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001486 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001487 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001488 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001489 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001490 /* get_next_write() sets starthere to a new value after the call. */
1491 while ((lenhere = get_next_write(curcontents + starthere,
1492 newcontents + starthere,
1493 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001494 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001495 if (!writecount++)
1496 msg_cdbg("W");
1497 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001498 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001499 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001500 if (ret) {
1501 if (ret == ACCESS_DENIED)
1502 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001503 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001504 }
David Hendricks048b38c2016-03-28 18:47:06 -07001505
1506 /*
1507 * If the block needed to be erased and was erased successfully
1508 * then we can assume that we didn't run into any write-
1509 * protected areas. Otherwise, we need to verify each page to
1510 * ensure it was successfully written and abort if we encounter
1511 * any errors.
1512 */
1513 if (programmer_table[programmer].paranoid && !block_was_erased) {
1514 if (verify_range(flash, newcontents + starthere,
1515 start + starthere, lenhere, "WRITE"))
1516 return -1;
1517 }
1518
hailfingerb437e282010-11-04 01:04:27 +00001519 starthere += lenhere;
1520 skip = 0;
1521 }
1522 if (skip)
1523 msg_cdbg("S");
1524 return ret;
1525}
1526
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001527static int walk_eraseregions(struct flashctx *flash, int erasefunction,
1528 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001529 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001530 unsigned int len,
1531 uint8_t *param1,
1532 uint8_t *param2,
1533 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001534 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001535 unsigned int addr,
1536 unsigned int len)),
1537 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001538{
David Hendricks605544b2015-08-15 16:32:58 -07001539 int i, j, rc = -1;
hailfingerb91c08c2011-08-15 19:54:20 +00001540 unsigned int start = 0;
1541 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001542 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001543
hailfinger2b8c9382010-07-13 00:37:19 +00001544 for (i = 0; i < NUM_ERASEREGIONS; i++) {
David Hendricks605544b2015-08-15 16:32:58 -07001545 /* count==0 and size==0 for all automatically initialized array
hailfinger2b8c9382010-07-13 00:37:19 +00001546 * members so the loop below won't be executed for them.
1547 */
1548 len = eraser.eraseblocks[i].size;
David Hendricks605544b2015-08-15 16:32:58 -07001549 if (!len)
1550 continue;
1551
1552 if (required_erase_size && (len != required_erase_size)) {
1553 msg_cdbg("%u does not meet erase alignment", len);
1554 rc = -1;
1555 break;
1556 }
1557
hailfinger2b8c9382010-07-13 00:37:19 +00001558 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001559 /* Print this for every block except the first one. */
1560 if (i || j)
1561 msg_cdbg(", ");
1562 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001563 start + len - 1);
David Hendricks1ed1d352011-11-23 17:54:37 -08001564 rc = do_something(flash, start, len, param1, param2,
1565 eraser.block_erase);
1566 if (rc) {
1567 if (ignore_error(rc))
1568 rc = 0;
1569 else
1570 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001571 }
hailfinger2b8c9382010-07-13 00:37:19 +00001572 start += len;
1573 }
1574 }
hailfingerb437e282010-11-04 01:04:27 +00001575 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001576 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001577}
1578
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001579static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001580{
1581 struct block_eraser eraser = flash->block_erasers[k];
1582
1583 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1584 if (log)
1585 msg_cdbg("not defined. ");
1586 return 1;
1587 }
1588 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1589 if (log)
1590 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001591 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001592 return 1;
1593 }
1594 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1595 if (log)
1596 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001597 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001598 return 1;
1599 }
1600 return 0;
1601}
1602
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001603int erase_and_write_flash(struct flashctx *flash, uint8_t *oldcontents,
uwe8d342eb2011-07-28 08:13:25 +00001604 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001605{
hailfinger8f524a82011-07-21 21:21:04 +00001606 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001607 uint8_t *curcontents;
1608 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001609 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001610
hailfingercf848f12010-12-05 15:14:44 +00001611 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001612 curcontents = malloc(size);
1613 if (!curcontents) {
1614 msg_gerr("Out of memory!\n");
1615 exit(1);
1616 }
hailfingerb437e282010-11-04 01:04:27 +00001617 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1618 memcpy(curcontents, oldcontents, size);
1619
hailfinger7df21362009-09-05 02:30:58 +00001620 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001621 if (k != 0)
1622 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001623 if (!usable_erasefunctions) {
1624 msg_cdbg("No usable erase functions left.\n");
1625 break;
1626 }
stefanctf0bc4712011-07-26 14:28:35 +00001627 msg_cdbg("Trying erase function %i... ", k);
1628 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001629 continue;
hailfingercf848f12010-12-05 15:14:44 +00001630 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001631 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1632 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001633 /* If everything is OK, don't try another erase function. */
1634 if (!ret)
1635 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001636 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001637 * contents are. If no usable erase functions remain, we can
1638 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001639 */
hailfingercf848f12010-12-05 15:14:44 +00001640 if (!usable_erasefunctions)
1641 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001642 /* Reading the whole chip may take a while, inform the user even
1643 * in non-verbose mode.
1644 */
1645 msg_cinfo("Reading current flash chip contents... ");
David Hendrickse3451942013-03-21 17:23:29 -07001646 if (read_flash(flash, curcontents, 0, size)) {
hailfinger6237f5e2010-12-02 02:41:55 +00001647 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001648 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001649 /* We have no idea about the flash chip contents, so
1650 * retrying with another erase function is pointless.
1651 */
1652 break;
1653 }
David Hendricks89a45e52011-11-22 16:56:22 -08001654 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001655 }
hailfingerb437e282010-11-04 01:04:27 +00001656 /* Free the scratchpad. */
1657 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001658
hailfinger7df21362009-09-05 02:30:58 +00001659 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001660 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001661 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001662 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001663 }
1664 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001665}
1666
hailfinger4c47e9d2010-10-19 22:06:20 +00001667void nonfatal_help_message(void)
1668{
1669 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1670 "This means we have to add special support for your board, "
1671 "programmer or flash chip.\n"
1672 "Please report this on IRC at irc.freenode.net (channel "
1673 "#flashrom) or\n"
1674 "mail flashrom@flashrom.org!\n"
1675 "-------------------------------------------------------------"
1676 "------------------\n"
1677 "You may now reboot or simply leave the machine running.\n");
1678}
1679
uweb34ec9f2009-10-01 18:40:02 +00001680void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001681{
snelsone42c3802010-05-07 20:09:04 +00001682 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001683 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001684 "mail flashrom@flashrom.org with FAILED: your board name in "
1685 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001686 "-------------------------------------------------------------"
1687 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001688 "DO NOT REBOOT OR POWEROFF!\n");
1689}
1690
uwe8d342eb2011-07-28 08:13:25 +00001691/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001692void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001693{
1694 enum programmer p;
1695 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001696 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001697 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001698 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001699 }
Simon Glass8dc82732013-07-16 10:13:51 -06001700 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001701}
1702
hailfingerf79d1712010-10-06 23:48:34 +00001703void list_programmers_linebreak(int startcol, int cols, int paren)
1704{
1705 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001706 int pnamelen;
1707 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001708 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001709 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001710
1711 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1712 pname = programmer_table[p].name;
1713 pnamelen = strlen(pname);
1714 if (remaining - pnamelen - 2 < 0) {
1715 if (firstline)
1716 firstline = 0;
1717 else
1718 printf("\n");
1719 for (i = 0; i < startcol; i++)
1720 printf(" ");
1721 remaining = cols - startcol;
1722 } else {
1723 printf(" ");
1724 remaining--;
1725 }
1726 if (paren && (p == 0)) {
1727 printf("(");
1728 remaining--;
1729 }
1730 printf("%s", pname);
1731 remaining -= pnamelen;
1732 if (p < PROGRAMMER_INVALID - 1) {
1733 printf(",");
1734 remaining--;
1735 } else {
1736 if (paren)
1737 printf(")");
1738 printf("\n");
1739 }
1740 }
1741}
1742
hailfinger3b471632010-03-27 16:36:40 +00001743void print_sysinfo(void)
1744{
David Hendricksc6c9f822010-11-03 15:07:01 -07001745 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001746#if HAVE_UTSNAME == 1
1747 struct utsname osinfo;
1748 uname(&osinfo);
1749
David Hendricksc6c9f822010-11-03 15:07:01 -07001750 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001751 osinfo.machine);
1752#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001753 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001754#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001755 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001756#if NEED_PCI == 1
1757#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001758 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001759#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001760 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001761#endif
1762#endif
1763#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001764 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001765#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001766 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001767#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001768 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001769#endif
hailfinger3b471632010-03-27 16:36:40 +00001770#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001771 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001772#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001773 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001774#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001775 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001776#endif
1777#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001778 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001779#endif
1780#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001781 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001782#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001783 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001784#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001785 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001786}
1787
uwefdeca092008-01-21 15:24:22 +00001788void print_version(void)
1789{
David Hendricksc6c9f822010-11-03 15:07:01 -07001790 /* send to stderr for chromium os */
1791 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001792 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001793}
1794
hailfinger74819ad2010-05-15 15:04:37 +00001795void print_banner(void)
1796{
1797 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001798 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001799 msg_ginfo("\n");
1800}
1801
hailfingerc77acb52009-12-24 02:15:55 +00001802int selfcheck(void)
1803{
hailfinger45177872010-01-18 08:14:43 +00001804 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001805 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001806
1807 /* Safety check. Instead of aborting after the first error, check
1808 * if more errors exist.
1809 */
hailfingerc77acb52009-12-24 02:15:55 +00001810 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001811 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001812 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001813 }
stefanct8632c922011-07-26 14:33:46 +00001814 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001815 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001816 * For 'flashchips' we check the first element to be non-null. In the
1817 * other cases there exist use cases where the first element can be
1818 * null. */
Yunlian Jiang945c3392015-01-20 15:56:44 -08001819 if (flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001820 msg_gerr("Flashchips table miscompilation!\n");
1821 ret = 1;
1822 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001823 /* Check that virtual_memory in struct flashctx is placed directly
1824 * after the members copied from struct flashchip.
1825 */
1826 if (sizeof(struct flashchip) != offsetof(struct flashctx, virtual_memory)) {
1827 msg_gerr("struct flashctx broken!\n");
1828 ret = 1;
1829 }
hailfinger45177872010-01-18 08:14:43 +00001830 for (flash = flashchips; flash && flash->name; flash++)
1831 if (selfcheck_eraseblocks(flash))
1832 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001833
hailfinger45177872010-01-18 08:14:43 +00001834 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001835}
1836
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001837void check_chip_supported(const struct flashctx *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001838{
1839 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001840 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001841 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001842 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001843 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001844 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001845 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001846 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001847 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001848 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001849 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001850 msg_cdbg(" WRITE");
Duncan Laurie06ffd522015-10-26 12:56:08 -07001851 if (flash->tested & TEST_BAD_UREAD)
1852 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001853 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001854 }
1855 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1856 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1857 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1858 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001859 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001860 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001861 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001862 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001863 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001864 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001865 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001866 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001867 msg_cdbg(" WRITE");
Duncan Laurie06ffd522015-10-26 12:56:08 -07001868 if (!(flash->tested & TEST_BAD_UREAD) && !(flash->tested & TEST_OK_UREAD))
1869 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001870 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001871 }
hailfinger92cd8e32010-01-07 03:24:05 +00001872 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001873 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001874 "in the latest development\n"
1875 "version of flashrom. If you are running the latest "
1876 "development version,\n"
1877 "please email a report to flashrom@flashrom.org if "
1878 "any of the above operations\n"
1879 "work correctly for you with this flash part. Please "
1880 "include the flashrom\n"
1881 "output with the additional -V option for all "
1882 "operations you tested (-V, -Vr,\n"
1883 "-Vw, -VE), and mention which mainboard or "
1884 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001885 "Please mention your board in the subject line. "
1886 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001887 }
1888}
1889
hailfinger771fc182010-10-15 00:01:14 +00001890/* FIXME: This function signature needs to be improved once doit() has a better
1891 * function signature.
1892 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001893int 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 +00001894{
1895 if (!programmer_may_write && (write_it || erase_it)) {
1896 msg_perr("Write/erase is not working yet on your programmer in "
1897 "its current configuration.\n");
1898 /* --force is the wrong approach, but it's the best we can do
1899 * until the generic programmer parameter parser is merged.
1900 */
1901 if (!force)
1902 return 1;
1903 msg_cerr("Continuing anyway.\n");
1904 }
1905
1906 if (read_it || erase_it || write_it || verify_it) {
1907 /* Everything needs read. */
1908 if (flash->tested & TEST_BAD_READ) {
1909 msg_cerr("Read is not working on this chip. ");
1910 if (!force)
1911 return 1;
1912 msg_cerr("Continuing anyway.\n");
1913 }
1914 if (!flash->read) {
1915 msg_cerr("flashrom has no read function for this "
1916 "flash chip.\n");
1917 return 1;
1918 }
1919 }
1920 if (erase_it || write_it) {
1921 /* Write needs erase. */
1922 if (flash->tested & TEST_BAD_ERASE) {
1923 msg_cerr("Erase is not working on this chip. ");
1924 if (!force)
1925 return 1;
1926 msg_cerr("Continuing anyway.\n");
1927 }
stefancte1c5acf2011-07-04 07:27:17 +00001928 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001929 msg_cerr("flashrom has no erase function for this "
1930 "flash chip.\n");
1931 return 1;
1932 }
hailfinger771fc182010-10-15 00:01:14 +00001933 }
1934 if (write_it) {
1935 if (flash->tested & TEST_BAD_WRITE) {
1936 msg_cerr("Write is not working on this chip. ");
1937 if (!force)
1938 return 1;
1939 msg_cerr("Continuing anyway.\n");
1940 }
1941 if (!flash->write) {
1942 msg_cerr("flashrom has no write function for this "
1943 "flash chip.\n");
1944 return 1;
1945 }
1946 }
1947 return 0;
1948}
1949
hailfingerc77acb52009-12-24 02:15:55 +00001950/* This function signature is horrible. We need to design a better interface,
1951 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001952 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001953 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001954int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09001955 int write_it, int erase_it, int verify_it, int extract_it,
1956 const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001957{
hailfinger4c47e9d2010-10-19 22:06:20 +00001958 uint8_t *oldcontents;
1959 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001960 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001961 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001962
stefanct02116582011-05-18 01:30:56 +00001963 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001964 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001965 ret = 1;
1966 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001967 }
1968
hailfinger771fc182010-10-15 00:01:14 +00001969 /* Given the existence of read locks, we want to unlock for read,
1970 * erase and write.
1971 */
1972 if (flash->unlock)
1973 flash->unlock(flash);
1974
David Hendricks6d62d752011-03-07 21:20:22 -08001975 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001976 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001977 ret = 1;
1978 goto out_nofree;
1979 }
1980
Simon Glass9ad06c12013-07-03 22:08:17 +09001981 if (extract_it) {
1982 ret = extract_regions(flash);
1983 goto out_nofree;
1984 }
1985
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001986 /* mark entries included using -i argument as "included" if they are
1987 found in the master rom_entries list */
1988 if (process_include_args() < 0) {
1989 ret = 1;
1990 goto out_nofree;
1991 }
1992
hailfinger771fc182010-10-15 00:01:14 +00001993 if (read_it) {
1994 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001995 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001996 }
hailfingerb437e282010-11-04 01:04:27 +00001997
stefanctd611e8f2011-07-12 22:35:21 +00001998 oldcontents = malloc(size);
1999 if (!oldcontents) {
2000 msg_gerr("Out of memory!\n");
2001 exit(1);
2002 }
Simon Glass4c214132013-07-16 10:09:28 -06002003 /* Assume worst case: All blocks are not erased. */
2004 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002005 newcontents = malloc(size);
2006 if (!newcontents) {
2007 msg_gerr("Out of memory!\n");
2008 exit(1);
2009 }
Simon Glass4c214132013-07-16 10:09:28 -06002010 /* Assume best case: All blocks are erased. */
2011 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002012 /* Side effect of the assumptions above: Default write action is erase
2013 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002014 * oldcontents being completely unerased means we have to erase
2015 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002016 */
2017
hailfingerd217d122010-10-08 18:52:29 +00002018 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002019 /*
2020 * Note: This must be done before any files specified by -i
2021 * arguments are processed merged into the newcontents since
2022 * -i files take priority. See http://crbug.com/263495.
2023 */
2024 if (filename) {
2025 if (read_buf_from_file(newcontents, size, filename)) {
2026 ret = 1;
2027 goto out;
2028 }
2029 } else {
2030 /* Content will be read from -i args, so they must
2031 * not overlap. */
2032 if (included_regions_overlap()) {
2033 msg_gerr("Error: Included regions must "
2034 "not overlap.\n");
2035 ret = 1;
2036 goto out;
2037 }
stepan1da96c02006-11-21 23:48:51 +00002038 }
2039
David Hendricksac82cac2012-06-19 10:29:37 -07002040#if 0
2041 /*
2042 * FIXME: show_id() causes failure if vendor:mainboard do not
2043 * match. This may happen if codenames are in flux.
2044 * See chrome-os-partner:10414.
2045 */
hailfinger90c7d542010-05-31 15:27:27 +00002046#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002047 if (programmer == PROGRAMMER_INTERNAL)
2048 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002049#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002050#endif
ollie5672ac62004-03-17 22:22:08 +00002051 }
2052
David Hendricksc44d7a02011-10-17 11:28:43 -07002053 /* Obtain a reference image so that we can check whether regions need
2054 * to be erased and to give better diagnostics in case write fails.
David Hendricks52ddff02013-07-23 15:05:14 -07002055 * If --fast-verify is used then only the regions which are included
2056 * using -i will be read.
hailfinger4c47e9d2010-10-19 22:06:20 +00002057 */
David Hendricksc44d7a02011-10-17 11:28:43 -07002058 if (diff_file) {
2059 msg_cdbg("Reading old contents from file... ");
2060 if (read_buf_from_file(oldcontents, size, diff_file)) {
2061 ret = 1;
2062 msg_cdbg("FAILED.\n");
2063 goto out;
2064 }
2065 } else {
2066 msg_cdbg("Reading old contents from flash chip... ");
David Hendricksd4e712c2013-08-02 17:06:16 -07002067 if (verify_it == VERIFY_PARTIAL) {
2068 if (handle_partial_read(flash, oldcontents,
2069 read_flash, 0) < 0) {
David Hendricks52ddff02013-07-23 15:05:14 -07002070 ret = 1;
2071 msg_cdbg("FAILED.\n");
2072 goto out;
2073 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002074 } else {
2075 if (read_flash(flash, oldcontents, 0, size)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002076 ret = 1;
2077 msg_cdbg("FAILED.\n");
2078 goto out;
2079 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002080 }
hailfinger4c47e9d2010-10-19 22:06:20 +00002081 }
David Hendricks89a45e52011-11-22 16:56:22 -08002082 msg_cdbg("done.\n");
David Hendricksdf29a832013-06-28 14:33:51 -07002083 /*
2084 * Note: This must be done after reading the file specified for the
2085 * -w/-v argument, if any, so that files specified using -i end up
2086 * in the "newcontents" buffer before being written.
2087 * See http://crbug.com/263495.
2088 */
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002089 if (handle_romentries(flash, oldcontents, newcontents)) {
2090 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002091 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002092 goto out;
2093 }
uwef6641642007-05-09 10:17:44 +00002094
David Hendricksa7e114b2016-02-26 18:49:15 -08002095 if (erase_it) {
2096 /* FIXME: Do we really want the scary warning if erase failed?
2097 * After all, after erase the chip is either blank or partially
2098 * blank or it has the old contents. A blank chip won't boot,
2099 * so if the user wanted erase and reboots afterwards, the user
2100 * knows very well that booting won't work.
2101 */
2102 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2103 emergency_help_message();
2104 ret = 1;
2105 }
2106 goto out;
2107 }
2108
stuge8ce3a3c2008-04-28 14:47:30 +00002109 if (write_it) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002110 // parse the new fmap
Souvik Ghosh63b92f92016-06-29 18:45:52 -07002111 if ((ret = cros_ec_prepare(flash, newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002112 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002113 goto out;
2114 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002115
hailfingerb437e282010-11-04 01:04:27 +00002116 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2117 msg_cerr("Uh oh. Erase/write failed. Checking if "
2118 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002119 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002120 if (!memcmp(oldcontents, newcontents, size)) {
2121 msg_cinfo("Good. It seems nothing was "
2122 "changed.\n");
2123 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002124 ret = 1;
2125 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002126 }
2127 }
hailfingerd217d122010-10-08 18:52:29 +00002128 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002129 ret = 1;
2130 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002131 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002132
Souvik Ghosh63b92f92016-06-29 18:45:52 -07002133 ret = cros_ec_need_2nd_pass(flash);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002134 if (ret < 0) {
2135 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002136 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002137 emergency_help_message();
2138 ret = 1;
2139 goto out;
2140 } else if (ret > 0) {
2141 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002142 msg_pdbg("CROS_EC needs 2nd pass.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002143 if (read_flash(flash, oldcontents, 0, size)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002144 msg_cerr("Uh oh. Cannot get latest content.\n");
2145 emergency_help_message();
2146 ret = 1;
2147 goto out;
2148 }
2149 // write 2nd pass
2150 if (erase_and_write_flash(flash, oldcontents,
2151 newcontents)) {
David Hendricksb907de32014-08-11 16:47:09 -07002152 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002153 emergency_help_message();
2154 ret = 1;
2155 goto out;
2156 }
2157 ret = 0;
2158 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002159
Souvik Ghosh63b92f92016-06-29 18:45:52 -07002160 if (cros_ec_finish(flash) < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002161 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002162 emergency_help_message();
2163 ret = 1;
2164 goto out;
2165 }
stuge8ce3a3c2008-04-28 14:47:30 +00002166 }
ollie6a600992005-11-26 21:55:36 +00002167
hailfinger0459e1c2009-08-19 13:55:34 +00002168 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002169 if ((write_it || erase_it) && !content_has_changed) {
2170 msg_gdbg("Nothing was erased or written, skipping "
2171 "verification\n");
2172 } else {
2173 /* Work around chips which need some time to calm down. */
2174 if (write_it && verify_it != VERIFY_PARTIAL)
2175 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002176
David Hendricks9ba79fb2015-04-03 12:06:16 -07002177 ret = verify_flash(flash, newcontents, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002178
David Hendricks9ba79fb2015-04-03 12:06:16 -07002179 /* If we tried to write, and verification now fails, we
2180 * might have an emergency situation.
2181 */
2182 if (ret && write_it)
2183 emergency_help_message();
2184 }
hailfinger0459e1c2009-08-19 13:55:34 +00002185 }
ollie6a600992005-11-26 21:55:36 +00002186
hailfinger90fcf9b2010-11-05 14:51:59 +00002187out:
2188 free(oldcontents);
2189 free(newcontents);
2190out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002191 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002192 /*
2193 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2194 * tree. This is because some operations, such as write protection,
2195 * requires programmer_shutdown() but does not call doit().
2196 */
2197// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002198 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002199}