blob: c456bbfb4a09d8acc90173f1ba453cbb67e98dfc [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
uwe4475e902009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
hailfinger23060112009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
uweb25f1ea2007-08-29 17:52:32 +000019 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
rminnich8d3ff912003-10-25 17:01:29 +000022 */
23
hailfingera83a5fe2010-05-30 22:24:40 +000024#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000026#ifndef __LIBPAYLOAD__
27#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000028#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000029#endif
rminnich8d3ff912003-10-25 17:01:29 +000030#include <string.h>
31#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000032#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000033#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
Vincent Palatin7ab23932014-10-01 12:09:16 -070037#include <unistd.h>
rminnich8d3ff912003-10-25 17:01:29 +000038#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000039#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090040#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000041#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000042
krause2eb76212011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000044char *chip_to_probe = NULL;
Souvik Ghosh3c963a42016-07-19 18:48:15 -070045int verbose_screen = MSG_ERROR;
46int verbose_logfile = MSG_DEBUG2;
hailfinger80422e22009-12-13 22:28:00 +000047
David Hendricks54777392015-01-11 18:55:14 -080048unsigned int required_erase_size = 0; /* see comment in flash.h */
49
David Hendricks9ba79fb2015-04-03 12:06:16 -070050/* Set if any erase/write operation is to be done. This will be used to
51 * decide if final verification is needed. */
52static int content_has_changed = 0;
53
David Hendricks1ed1d352011-11-23 17:54:37 -080054/* error handling stuff */
55enum error_action access_denied_action = error_ignore;
56
57int ignore_error(int err) {
58 int rc = 0;
59
60 switch(err) {
61 case ACCESS_DENIED:
62 if (access_denied_action == error_ignore)
63 rc = 1;
64 break;
65 default:
66 break;
67 }
68
69 return rc;
70}
71
hailfinger969e2f32011-09-08 00:00:29 +000072static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000073
hailfingerddeb4ac2010-07-08 10:13:37 +000074static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000075
uwee15beb92010-08-08 17:01:18 +000076/*
hailfinger80422e22009-12-13 22:28:00 +000077 * Programmers supporting multiple buses can have differing size limits on
78 * each bus. Store the limits for each bus in a common struct.
79 */
hailfinger1ff33dc2010-07-03 11:02:10 +000080struct decode_sizes max_rom_decode;
81
82/* If nonzero, used as the start address of bottom-aligned flash. */
83unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000084
hailfinger5828baf2010-07-03 12:14:25 +000085/* Is writing allowed with this programmer? */
86int programmer_may_write;
87
hailfingerabe249e2009-05-08 17:43:22 +000088const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000089#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000090 {
hailfinger3548a9a2009-08-12 14:34:35 +000091 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000092 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000093 .map_flash_region = physmap,
94 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000095 .delay = internal_delay,
David Hendricks55cdd9c2015-11-25 14:37:26 -080096
97 /*
98 * "Internal" implies in-system programming on a live system, so
99 * handle with paranoia to catch errors early. If something goes
100 * wrong then hopefully the system will still be recoverable.
101 */
102 .paranoid = 1,
hailfingerabe249e2009-05-08 17:43:22 +0000103 },
hailfinger80422e22009-12-13 22:28:00 +0000104#endif
stepan927d4e22007-04-04 22:45:58 +0000105
hailfinger90c7d542010-05-31 15:27:27 +0000106#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000107 {
hailfinger3548a9a2009-08-12 14:34:35 +0000108 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000109 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000110 .map_flash_region = dummy_map,
111 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000112 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000113 },
hailfinger571a6b32009-09-16 10:09:21 +0000114#endif
hailfingera9df33c2009-05-09 00:54:55 +0000115
hailfinger90c7d542010-05-31 15:27:27 +0000116#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000117 {
hailfinger3548a9a2009-08-12 14:34:35 +0000118 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000119 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000120 .map_flash_region = fallback_map,
121 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000122 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000123 },
hailfinger571a6b32009-09-16 10:09:21 +0000124#endif
uwe0f5a3a22009-05-13 11:36:06 +0000125
hailfinger90c7d542010-05-31 15:27:27 +0000126#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000127 {
hailfinger0d703d42011-03-07 01:08:09 +0000128 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000129 .name = "nicrealtek",
130 //.name = "nicsmc1211",
131 .init = nicrealtek_init,
132 .map_flash_region = fallback_map,
133 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000134 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000135 },
hailfinger5aa36982010-05-21 21:54:07 +0000136#endif
137
hailfingerf0a368f2010-06-07 22:37:54 +0000138#if CONFIG_NICNATSEMI == 1
139 {
uwe8d342eb2011-07-28 08:13:25 +0000140 .name = "nicnatsemi",
141 .init = nicnatsemi_init,
142 .map_flash_region = fallback_map,
143 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000144 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000145 },
146#endif
hailfinger5aa36982010-05-21 21:54:07 +0000147
hailfinger90c7d542010-05-31 15:27:27 +0000148#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000149 {
150 .name = "gfxnvidia",
151 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000152 .map_flash_region = fallback_map,
153 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000154 .delay = internal_delay,
155 },
156#endif
157
hailfinger90c7d542010-05-31 15:27:27 +0000158#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000159 {
uwee2f95ef2009-09-02 23:00:46 +0000160 .name = "drkaiser",
161 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000162 .map_flash_region = fallback_map,
163 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000164 .delay = internal_delay,
165 },
hailfinger571a6b32009-09-16 10:09:21 +0000166#endif
uwee2f95ef2009-09-02 23:00:46 +0000167
hailfinger90c7d542010-05-31 15:27:27 +0000168#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000169 {
hailfinger3548a9a2009-08-12 14:34:35 +0000170 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000171 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000172 .map_flash_region = fallback_map,
173 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000174 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000175 },
hailfinger571a6b32009-09-16 10:09:21 +0000176#endif
ruikda922a12009-05-17 19:39:27 +0000177
hailfinger90c7d542010-05-31 15:27:27 +0000178#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000179 {
180 .name = "atahpt",
181 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000182 .map_flash_region = fallback_map,
183 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000184 .delay = internal_delay,
185 },
186#endif
187
hailfinger90c7d542010-05-31 15:27:27 +0000188#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000189 {
hailfinger90c7d542010-05-31 15:27:27 +0000190 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000191 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000192 .map_flash_region = fallback_map,
193 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000194 .delay = internal_delay,
195 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000196#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000197
hailfinger90c7d542010-05-31 15:27:27 +0000198#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000199 {
hailfinger3548a9a2009-08-12 14:34:35 +0000200 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000201 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000202 .map_flash_region = fallback_map,
203 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000204 .delay = serprog_delay,
205 },
hailfinger74d88a72009-08-12 16:17:41 +0000206#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000207
hailfinger90c7d542010-05-31 15:27:27 +0000208#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000209 {
hailfinger90c7d542010-05-31 15:27:27 +0000210 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000211 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000212 .map_flash_region = fallback_map,
213 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000214 .delay = internal_delay,
215 },
216#endif
217
Anton Staafb2647882014-09-17 15:13:43 -0700218#if CONFIG_RAIDEN_DEBUG_SPI == 1
219 {
220 .name = "raiden_debug_spi",
221 .init = raiden_debug_spi_init,
222 .map_flash_region = fallback_map,
223 .unmap_flash_region = fallback_unmap,
224 .delay = internal_delay,
225 },
226#endif
227
hailfinger90c7d542010-05-31 15:27:27 +0000228#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000229 {
230 .name = "dediprog",
231 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000232 .map_flash_region = fallback_map,
233 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000234 .delay = internal_delay,
235 },
236#endif
237
hailfinger52c4fa02010-07-21 10:26:01 +0000238#if CONFIG_RAYER_SPI == 1
239 {
240 .name = "rayer_spi",
241 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000242 .map_flash_region = fallback_map,
243 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000244 .delay = internal_delay,
245 },
246#endif
247
hailfinger7949b652011-05-08 00:24:18 +0000248#if CONFIG_NICINTEL == 1
249 {
250 .name = "nicintel",
251 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000252 .map_flash_region = fallback_map,
253 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000254 .delay = internal_delay,
255 },
256#endif
257
uwe6764e922010-09-03 18:21:21 +0000258#if CONFIG_NICINTEL_SPI == 1
259 {
uwe8d342eb2011-07-28 08:13:25 +0000260 .name = "nicintel_spi",
261 .init = nicintel_spi_init,
262 .map_flash_region = fallback_map,
263 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000264 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000265 },
266#endif
267
hailfingerfb1f31f2010-12-03 14:48:11 +0000268#if CONFIG_OGP_SPI == 1
269 {
uwe8d342eb2011-07-28 08:13:25 +0000270 .name = "ogp_spi",
271 .init = ogp_spi_init,
272 .map_flash_region = fallback_map,
273 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000274 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000275 },
276#endif
277
hailfinger935365d2011-02-04 21:37:59 +0000278#if CONFIG_SATAMV == 1
279 {
280 .name = "satamv",
281 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000282 .map_flash_region = fallback_map,
283 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000284 .delay = internal_delay,
285 },
286#endif
287
David Hendrickscebee892015-05-23 20:30:30 -0700288#if CONFIG_LINUX_MTD == 1
289 {
290 .name = "linux_mtd",
291 .init = linux_mtd_init,
292 .map_flash_region = fallback_map,
293 .unmap_flash_region = fallback_unmap,
294 .delay = internal_delay,
295 },
296#endif
297
uwe7df6dda2011-09-03 18:37:52 +0000298#if CONFIG_LINUX_SPI == 1
299 {
300 .name = "linux_spi",
301 .init = linux_spi_init,
302 .map_flash_region = fallback_map,
303 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000304 .delay = internal_delay,
305 },
306#endif
307
hailfinger3548a9a2009-08-12 14:34:35 +0000308 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000309};
stepan927d4e22007-04-04 22:45:58 +0000310
David Hendricksbf36f092010-11-02 23:39:29 -0700311#define CHIP_RESTORE_MAXFN 4
312static int chip_restore_fn_count = 0;
313struct chip_restore_func_data {
314 CHIP_RESTORE_CALLBACK;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700315 struct flashctx *flash;
David Hendricksbf36f092010-11-02 23:39:29 -0700316 uint8_t status;
317} static chip_restore_fn[CHIP_RESTORE_MAXFN];
318
David Hendricks668f29d2011-01-27 18:51:45 -0800319
hailfingerf31cbdc2010-11-10 15:25:18 +0000320#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000321static int shutdown_fn_count = 0;
322struct shutdown_func_data {
David Hendricks93784b42016-08-09 17:00:38 -0700323 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000324 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000325} static shutdown_fn[SHUTDOWN_MAXFN];
326/* Initialize to 0 to make sure nobody registers a shutdown function before
327 * programmer init.
328 */
329static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000330
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700331static int check_block_eraser(const struct flashctx *flash, int k, int log);
stefanct569dbb62011-07-01 00:19:12 +0000332
hailfingerdc6f7972010-02-14 01:20:28 +0000333/* Register a function to be executed on programmer shutdown.
334 * The advantage over atexit() is that you can supply a void pointer which will
335 * be used as parameter to the registered function upon programmer shutdown.
336 * This pointer can point to arbitrary data used by said function, e.g. undo
337 * information for GPIO settings etc. If unneeded, set data=NULL.
338 * Please note that the first (void *data) belongs to the function signature of
339 * the function passed as first parameter.
340 */
David Hendricks93784b42016-08-09 17:00:38 -0700341int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000342{
343 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000344 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000345 SHUTDOWN_MAXFN);
346 return 1;
347 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000348 if (!may_register_shutdown) {
349 msg_perr("Tried to register a shutdown function before "
350 "programmer init.\n");
351 return 1;
352 }
hailfingerdc6f7972010-02-14 01:20:28 +0000353 shutdown_fn[shutdown_fn_count].func = function;
354 shutdown_fn[shutdown_fn_count].data = data;
355 shutdown_fn_count++;
356
357 return 0;
358}
359
David Hendricksbf36f092010-11-02 23:39:29 -0700360//int register_chip_restore(int (*function) (void *data), void *data)
361int register_chip_restore(CHIP_RESTORE_CALLBACK,
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700362 struct flashctx *flash, uint8_t status)
David Hendricksbf36f092010-11-02 23:39:29 -0700363{
364 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
365 msg_perr("Tried to register more than %i chip restore"
366 " functions.\n", CHIP_RESTORE_MAXFN);
367 return 1;
368 }
369 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
370 chip_restore_fn[chip_restore_fn_count].flash = flash;
371 chip_restore_fn[chip_restore_fn_count].status = status;
372 chip_restore_fn_count++;
373
374 return 0;
375}
376
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700377int programmer_init(struct flashctx *flash, enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000378{
hailfinger1ef766d2010-07-06 09:55:48 +0000379 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000380
381 if (prog >= PROGRAMMER_INVALID) {
382 msg_perr("Invalid programmer specified!\n");
383 return -1;
384 }
385 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000386 /* Initialize all programmer specific data. */
387 /* Default to unlimited decode sizes. */
388 max_rom_decode = (const struct decode_sizes) {
389 .parallel = 0xffffffff,
390 .lpc = 0xffffffff,
391 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000392 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000393 };
hailfinger1ff33dc2010-07-03 11:02:10 +0000394 /* Default to top aligned flash at 4 GB. */
395 flashbase = 0;
396 /* Registering shutdown functions is now allowed. */
397 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000398 /* Default to allowing writes. Broken programmers set this to 0. */
399 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000400
401 programmer_param = param;
402 msg_pdbg("Initializing %s programmer\n",
403 programmer_table[programmer].name);
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700404 ret = programmer_table[programmer].init(flash);
hailfinger1ef766d2010-07-06 09:55:48 +0000405 if (programmer_param && strlen(programmer_param)) {
406 msg_perr("Unhandled programmer parameters: %s\n",
407 programmer_param);
408 /* Do not error out here, the init itself was successful. */
409 }
410 return ret;
uweabe92a52009-05-16 22:36:00 +0000411}
412
David Hendricksbf36f092010-11-02 23:39:29 -0700413int chip_restore()
414{
415 int rc = 0;
416
417 while (chip_restore_fn_count > 0) {
418 int i = --chip_restore_fn_count;
419 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
420 chip_restore_fn[i].status);
421 }
422
423 return rc;
424}
425
David Hendricks93784b42016-08-09 17:00:38 -0700426int programmer_shutdown(void)
uweabe92a52009-05-16 22:36:00 +0000427{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000428 int ret = 0;
429
hailfinger1ff33dc2010-07-03 11:02:10 +0000430 /* Registering shutdown functions is no longer allowed. */
431 may_register_shutdown = 0;
432 while (shutdown_fn_count > 0) {
433 int i = --shutdown_fn_count;
David Hendricks93784b42016-08-09 17:00:38 -0700434 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000435 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000436 return ret;
uweabe92a52009-05-16 22:36:00 +0000437}
438
439void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
440 size_t len)
441{
442 return programmer_table[programmer].map_flash_region(descr,
443 phys_addr, len);
444}
445
446void programmer_unmap_flash_region(void *virt_addr, size_t len)
447{
448 programmer_table[programmer].unmap_flash_region(virt_addr, len);
449}
450
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700451void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000452{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700453 flash->pgm->par.chip_writeb(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000454}
455
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700456void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000457{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700458 flash->pgm->par.chip_writew(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000459}
460
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700461void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000462{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700463 flash->pgm->par.chip_writel(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000464}
465
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700466void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000467{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700468 flash->pgm->par.chip_writen(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000469}
470
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700471uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000472{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700473 return flash->pgm->par.chip_readb(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000474}
475
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700476uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000477{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700478 return flash->pgm->par.chip_readw(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000479}
480
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700481uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000482{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700483 return flash->pgm->par.chip_readl(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000484}
485
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700486void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000487{
Souvik Ghosh63b92f92016-06-29 18:45:52 -0700488 flash->pgm->par.chip_readn(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000489}
490
hailfingere5829f62009-06-05 17:48:08 +0000491void programmer_delay(int usecs)
492{
493 programmer_table[programmer].delay(usecs);
494}
495
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700496void map_flash_registers(struct flashctx *flash)
stepan15e64bc2007-05-24 08:48:10 +0000497{
stepan15e64bc2007-05-24 08:48:10 +0000498 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000499 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000500 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000501 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000502}
503
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700504int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000505{
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700506 chip_readn(flash, buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000507
hailfinger23060112009-05-08 12:49:03 +0000508 return 0;
509}
510
hailfinger7b414742009-06-13 12:04:03 +0000511int min(int a, int b)
512{
513 return (a < b) ? a : b;
514}
515
hailfinger7af83692009-06-15 17:23:36 +0000516int max(int a, int b)
517{
518 return (a > b) ? a : b;
519}
520
hailfingeraec9c962009-10-31 01:53:09 +0000521int bitcount(unsigned long a)
522{
523 int i = 0;
524 for (; a != 0; a >>= 1)
525 if (a & 1)
526 i++;
527 return i;
528}
529
hailfingerf76cc322010-11-09 22:00:31 +0000530void tolower_string(char *str)
531{
532 for (; *str != '\0'; str++)
533 *str = (char)tolower((unsigned char)*str);
534}
535
hailfingera916b422009-06-01 02:08:58 +0000536char *strcat_realloc(char *dest, const char *src)
537{
538 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000539 if (!dest) {
540 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000541 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000542 }
hailfingera916b422009-06-01 02:08:58 +0000543 strcat(dest, src);
544 return dest;
545}
546
hailfinger6e5a52a2009-11-24 18:27:10 +0000547/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000548 * It will look for needle with a subsequent '=' in haystack, return a copy of
549 * needle and remove everything from the first occurrence of needle to the next
550 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000551 */
stefanct52700282011-06-26 17:38:17 +0000552char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000553{
hailfinger1ef766d2010-07-06 09:55:48 +0000554 char *param_pos, *opt_pos, *rest;
555 char *opt = NULL;
556 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000557 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000558
hailfingerf4aaccc2010-04-28 15:22:14 +0000559 needlelen = strlen(needle);
560 if (!needlelen) {
561 msg_gerr("%s: empty needle! Please report a bug at "
562 "flashrom@flashrom.org\n", __func__);
563 return NULL;
564 }
565 /* No programmer parameters given. */
566 if (*haystack == NULL)
567 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000568 param_pos = strstr(*haystack, needle);
569 do {
570 if (!param_pos)
571 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000572 /* Needle followed by '='? */
573 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600574
hailfinger1ef766d2010-07-06 09:55:48 +0000575 /* Beginning of the string? */
576 if (param_pos == *haystack)
577 break;
578 /* After a delimiter? */
579 if (strchr(delim, *(param_pos - 1)))
580 break;
581 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000582 /* Continue searching. */
583 param_pos++;
584 param_pos = strstr(param_pos, needle);
585 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000586
hailfinger6e5a52a2009-11-24 18:27:10 +0000587 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000588 /* Get the string after needle and '='. */
589 opt_pos = param_pos + needlelen + 1;
590 optlen = strcspn(opt_pos, delim);
591 /* Return an empty string if the parameter was empty. */
592 opt = malloc(optlen + 1);
593 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000594 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000595 exit(1);
596 }
hailfinger1ef766d2010-07-06 09:55:48 +0000597 strncpy(opt, opt_pos, optlen);
598 opt[optlen] = '\0';
599 rest = opt_pos + optlen;
600 /* Skip all delimiters after the current parameter. */
601 rest += strspn(rest, delim);
602 memmove(param_pos, rest, strlen(rest) + 1);
603 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000604 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000605
hailfinger1ef766d2010-07-06 09:55:48 +0000606 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000607}
608
stefanct52700282011-06-26 17:38:17 +0000609char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000610{
611 return extract_param(&programmer_param, param_name, ",");
612}
613
stefancte1c5acf2011-07-04 07:27:17 +0000614/* Returns the number of well-defined erasers for a chip. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700615static unsigned int count_usable_erasers(const struct flashctx *flash)
stefanct569dbb62011-07-01 00:19:12 +0000616{
617 unsigned int usable_erasefunctions = 0;
618 int k;
619 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
620 if (!check_block_eraser(flash, k, 0))
621 usable_erasefunctions++;
622 }
623 return usable_erasefunctions;
624}
625
hailfinger7af83692009-06-15 17:23:36 +0000626/* start is an offset to the base address of the flash chip */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700627int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000628{
629 int ret;
630 uint8_t *cmpbuf = malloc(len);
631
632 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000633 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000634 exit(1);
635 }
Simon Glass4c214132013-07-16 10:09:28 -0600636 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000637 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
638 free(cmpbuf);
639 return ret;
640}
641
Simon Glass4e305f42015-01-08 06:29:04 -0700642static int compare_chunk(uint8_t *readbuf, uint8_t *cmpbuf, unsigned int start,
643 unsigned int len, const char *message)
644{
645 int failcount = 0, i;
646
647 for (i = 0; i < len; i++) {
648 if (cmpbuf[i] != readbuf[i]) {
649 if (!failcount) {
650 msg_cerr("%s FAILED at 0x%08x! "
651 "Expected=0x%02x, Read=0x%02x,",
652 message, start + i,
653 cmpbuf[i], readbuf[i]);
654 }
655 failcount++;
656 }
657 }
658
659 return failcount;
660}
661
uwee15beb92010-08-08 17:01:18 +0000662/*
hailfinger7af3d192009-11-25 17:05:52 +0000663 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000664 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000665 * @start offset to the base address of the flash chip
666 * @len length of the verified area
667 * @message string to print in the "FAILED" message
668 * @return 0 for success, -1 for failure
669 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700670int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000671 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000672{
hailfinger8cb6ece2010-11-16 17:21:58 +0000673 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000674 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000675
676 if (!len)
677 goto out_free;
678
hailfingerb0f4d122009-06-24 08:20:45 +0000679 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000680 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000681 return 1;
682 }
hailfinger7af83692009-06-15 17:23:36 +0000683 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000684 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000685 exit(1);
686 }
687
688 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000689 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000690 " total_size 0x%x\n", __func__, start, len,
691 flash->total_size * 1024);
692 ret = -1;
693 goto out_free;
694 }
695 if (!message)
696 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000697
Simon Glass4e305f42015-01-08 06:29:04 -0700698 if (programmer_table[programmer].paranoid) {
699 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800700
Simon Glass4e305f42015-01-08 06:29:04 -0700701 /* limit chunksize in order to catch errors early */
702 for (i = 0, chunksize = 0; i < len; i += chunksize) {
703 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800704
Simon Glass4e305f42015-01-08 06:29:04 -0700705 chunksize = min(flash->page_size, len - i);
706 tmp = flash->read(flash, readbuf + i, start + i, chunksize);
707 if (tmp) {
708 ret = tmp;
709 if (ignore_error(tmp))
710 continue;
711 else
712 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800713 }
Simon Glass4e305f42015-01-08 06:29:04 -0700714
715 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
716 chunksize, message);
717 if (failcount)
718 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800719 }
Simon Glass4e305f42015-01-08 06:29:04 -0700720 } else {
721 int tmp;
722
723 /* read as much as we can to reduce transaction overhead */
724 tmp = flash->read(flash, readbuf, start, len);
725 if (tmp && !ignore_error(tmp)) {
726 ret = tmp;
727 goto out_free;
728 }
729
730 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000731 }
732
hailfinger5be6c0f2009-07-23 01:42:56 +0000733 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000734 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000735 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000736 ret = -1;
737 }
hailfinger7af83692009-06-15 17:23:36 +0000738
739out_free:
740 free(readbuf);
741 return ret;
742}
743
uwee15beb92010-08-08 17:01:18 +0000744/*
hailfingerb247c7a2010-03-08 00:42:32 +0000745 * Check if the buffer @have can be programmed to the content of @want without
746 * erasing. This is only possible if all chunks of size @gran are either kept
747 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000748 *
hailfingerb247c7a2010-03-08 00:42:32 +0000749 * The following write granularities (enum @gran) are known:
750 * - 1 bit. Each bit can be cleared individually.
751 * - 1 byte. A byte can be written once. Further writes to an already written
752 * byte cause the contents to be either undefined or to stay unchanged.
753 * - 128 bytes. If less than 128 bytes are written, the rest will be
754 * erased. Each write to a 128-byte region will trigger an automatic erase
755 * before anything is written. Very uncommon behaviour and unsupported by
756 * this function.
757 * - 256 bytes. If less than 256 bytes are written, the contents of the
758 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000759 * Warning: This function assumes that @have and @want point to naturally
760 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000761 *
762 * @have buffer with current content
763 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000764 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000765 * @gran write granularity (enum, not count)
766 * @return 0 if no erase is needed, 1 otherwise
767 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700768static int need_erase(struct flashctx *flash, uint8_t *have, uint8_t *want,
Simon Glass4c214132013-07-16 10:09:28 -0600769 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000770{
hailfingerb91c08c2011-08-15 19:54:20 +0000771 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000772 unsigned int i, j, limit;
Simon Glass4c214132013-07-16 10:09:28 -0600773 int erase_value = flash_erase_value(flash);
hailfingerb247c7a2010-03-08 00:42:32 +0000774
775 switch (gran) {
776 case write_gran_1bit:
777 for (i = 0; i < len; i++)
778 if ((have[i] & want[i]) != want[i]) {
779 result = 1;
780 break;
781 }
782 break;
783 case write_gran_1byte:
784 for (i = 0; i < len; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600785 if ((have[i] != want[i]) && (have[i] != erase_value)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000786 result = 1;
787 break;
788 }
789 break;
790 case write_gran_256bytes:
791 for (j = 0; j < len / 256; j++) {
792 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000793 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000794 if (!memcmp(have + j * 256, want + j * 256, limit))
795 continue;
796 /* have needs to be in erased state. */
797 for (i = 0; i < limit; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600798 if (have[j * 256 + i] != erase_value) {
hailfingerb247c7a2010-03-08 00:42:32 +0000799 result = 1;
800 break;
801 }
802 if (result)
803 break;
804 }
805 break;
hailfingerb437e282010-11-04 01:04:27 +0000806 default:
807 msg_cerr("%s: Unsupported granularity! Please report a bug at "
808 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000809 }
810 return result;
811}
812
hailfingerb437e282010-11-04 01:04:27 +0000813/**
814 * Check if the buffer @have needs to be programmed to get the content of @want.
815 * If yes, return 1 and fill in first_start with the start address of the
816 * write operation and first_len with the length of the first to-be-written
817 * chunk. If not, return 0 and leave first_start and first_len undefined.
818 *
819 * Warning: This function assumes that @have and @want point to naturally
820 * aligned regions.
821 *
822 * @have buffer with current content
823 * @want buffer with desired content
824 * @len length of the checked area
825 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000826 * @first_start offset of the first byte which needs to be written (passed in
827 * value is increased by the offset of the first needed write
828 * relative to have/want or unchanged if no write is needed)
829 * @return length of the first contiguous area which needs to be written
830 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000831 *
832 * FIXME: This function needs a parameter which tells it about coalescing
833 * in relation to the max write length of the programmer and the max write
834 * length of the chip.
835 */
stefanctc5eb8a92011-11-23 09:13:48 +0000836static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
837 unsigned int *first_start,
838 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000839{
stefanctc5eb8a92011-11-23 09:13:48 +0000840 int need_write = 0;
841 unsigned int rel_start = 0, first_len = 0;
842 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000843
hailfingerb437e282010-11-04 01:04:27 +0000844 switch (gran) {
845 case write_gran_1bit:
846 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000847 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000848 break;
849 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000850 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000851 break;
852 default:
853 msg_cerr("%s: Unsupported granularity! Please report a bug at "
854 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000855 /* Claim that no write was needed. A write with unknown
856 * granularity is too dangerous to try.
857 */
858 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000859 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000860 for (i = 0; i < len / stride; i++) {
861 limit = min(stride, len - i * stride);
862 /* Are 'have' and 'want' identical? */
863 if (memcmp(have + i * stride, want + i * stride, limit)) {
864 if (!need_write) {
865 /* First location where have and want differ. */
866 need_write = 1;
867 rel_start = i * stride;
868 }
869 } else {
870 if (need_write) {
871 /* First location where have and want
872 * do not differ anymore.
873 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000874 break;
875 }
876 }
877 }
hailfingerffb7f382010-12-06 13:05:44 +0000878 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000879 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000880 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000881 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000882}
883
hailfinger0c515352009-11-23 12:55:31 +0000884/* This function generates various test patterns useful for testing controller
885 * and chip communication as well as chip behaviour.
886 *
887 * If a byte can be written multiple times, each time keeping 0-bits at 0
888 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
889 * is essentially an AND operation. That's also the reason why this function
890 * provides the result of AND between various patterns.
891 *
892 * Below is a list of patterns (and their block length).
893 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
894 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
895 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
896 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
897 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
898 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
899 * Pattern 6 is 00 (1 Byte)
900 * Pattern 7 is ff (1 Byte)
901 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
902 * byte block.
903 *
904 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
905 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
906 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
907 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
908 * Pattern 12 is 00 (1 Byte)
909 * Pattern 13 is ff (1 Byte)
910 * Patterns 8-13 have no block number.
911 *
912 * Patterns 0-3 are created to detect and efficiently diagnose communication
913 * slips like missed bits or bytes and their repetitive nature gives good visual
914 * cues to the person inspecting the results. In addition, the following holds:
915 * AND Pattern 0/1 == Pattern 4
916 * AND Pattern 2/3 == Pattern 5
917 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
918 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
919 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
920 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
921 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
922 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
923 * Besides that, they provide for bit testing of the last two bytes of every
924 * 256 byte block which contains the block number for patterns 0-6.
925 * Patterns 10-11 are special purpose for detecting subblock aliasing with
926 * block sizes >256 bytes (some Dataflash chips etc.)
927 * AND Pattern 8/9 == Pattern 12
928 * AND Pattern 10/11 == Pattern 12
929 * Pattern 13 is the completely erased state.
930 * None of the patterns can detect aliasing at boundaries which are a multiple
931 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
932 */
933int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
934{
935 int i;
936
937 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000938 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000939 return 1;
940 }
941
942 switch (variant) {
943 case 0:
944 for (i = 0; i < size; i++)
945 buf[i] = (i & 0xf) << 4 | 0x5;
946 break;
947 case 1:
948 for (i = 0; i < size; i++)
949 buf[i] = (i & 0xf) << 4 | 0xa;
950 break;
951 case 2:
952 for (i = 0; i < size; i++)
953 buf[i] = 0x50 | (i & 0xf);
954 break;
955 case 3:
956 for (i = 0; i < size; i++)
957 buf[i] = 0xa0 | (i & 0xf);
958 break;
959 case 4:
960 for (i = 0; i < size; i++)
961 buf[i] = (i & 0xf) << 4;
962 break;
963 case 5:
964 for (i = 0; i < size; i++)
965 buf[i] = i & 0xf;
966 break;
967 case 6:
968 memset(buf, 0x00, size);
969 break;
970 case 7:
971 memset(buf, 0xff, size);
972 break;
973 case 8:
974 for (i = 0; i < size; i++)
975 buf[i] = i & 0xff;
976 break;
977 case 9:
978 for (i = 0; i < size; i++)
979 buf[i] = ~(i & 0xff);
980 break;
981 case 10:
982 for (i = 0; i < size % 2; i++) {
983 buf[i * 2] = (i >> 8) & 0xff;
984 buf[i * 2 + 1] = i & 0xff;
985 }
986 if (size & 0x1)
987 buf[i * 2] = (i >> 8) & 0xff;
988 break;
989 case 11:
990 for (i = 0; i < size % 2; i++) {
991 buf[i * 2] = ~((i >> 8) & 0xff);
992 buf[i * 2 + 1] = ~(i & 0xff);
993 }
994 if (size & 0x1)
995 buf[i * 2] = ~((i >> 8) & 0xff);
996 break;
997 case 12:
998 memset(buf, 0x00, size);
999 break;
1000 case 13:
1001 memset(buf, 0xff, size);
1002 break;
1003 }
1004
1005 if ((variant >= 0) && (variant <= 7)) {
1006 /* Write block number in the last two bytes of each 256-byte
1007 * block, big endian for easier reading of the hexdump.
1008 * Note that this wraps around for chips larger than 2^24 bytes
1009 * (16 MB).
1010 */
1011 for (i = 0; i < size / 256; i++) {
1012 buf[i * 256 + 254] = (i >> 8) & 0xff;
1013 buf[i * 256 + 255] = i & 0xff;
1014 }
1015 }
1016
1017 return 0;
1018}
1019
hailfingeraec9c962009-10-31 01:53:09 +00001020int check_max_decode(enum chipbustype buses, uint32_t size)
1021{
1022 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001023
1024 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001025 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001026 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001027 "size %u kB of chipset/board/programmer "
1028 "for %s interface, "
1029 "probe/read/erase/write may fail. ", size / 1024,
1030 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001031 }
hailfingere1e41ea2011-07-27 07:13:06 +00001032 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001033 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001034 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001035 "size %u kB of chipset/board/programmer "
1036 "for %s interface, "
1037 "probe/read/erase/write may fail. ", size / 1024,
1038 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001039 }
hailfingere1e41ea2011-07-27 07:13:06 +00001040 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001041 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001042 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001043 "size %u kB of chipset/board/programmer "
1044 "for %s interface, "
1045 "probe/read/erase/write may fail. ", size / 1024,
1046 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001047 }
hailfingere1e41ea2011-07-27 07:13:06 +00001048 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001049 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001050 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001051 "size %u kB of chipset/board/programmer "
1052 "for %s interface, "
1053 "probe/read/erase/write may fail. ", size / 1024,
1054 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001055 }
1056 if (!limitexceeded)
1057 return 0;
1058 /* Sometimes chip and programmer have more than one bus in common,
1059 * and the limit is not exceeded on all buses. Tell the user.
1060 */
1061 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001062 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001063 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001064 "interface which can support a chip of this size. "
1065 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001066 return 1;
1067}
1068
Souvik Ghosh63b92f92016-06-29 18:45:52 -07001069int probe_flash(struct registered_programmer *pgm, int startchip,
1070 struct flashctx *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001071{
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301072 const struct flashchip *flash, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001073 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001074 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001075 uint32_t size;
1076 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001077 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001078
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301079 /* Based on the host controller interface that a platform
1080 * needs to use (hwseq or swseq),
1081 * set the flashchips list here.
1082 */
1083 switch (ich_generation) {
1084 case CHIPSET_100_SERIES_SUNRISE_POINT:
Furquan Shaikh44088752016-07-11 22:48:08 -07001085 case CHIPSET_APL:
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301086 flash_list = flashchips_hwseq;
1087 break;
1088 default:
1089 flash_list = flashchips;
1090 break;
1091 }
1092
1093 for (flash = flash_list + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001094 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001095 continue;
Souvik Ghosh63b92f92016-06-29 18:45:52 -07001096 buses_common = pgm->buses_supported & flash->bustype;
1097 if (!buses_common)
hailfinger18bd4cc2011-06-17 22:38:53 +00001098 continue;
hailfinger18bd4cc2011-06-17 22:38:53 +00001099 msg_gdbg("Probing for %s %s, %d kB: ",
1100 flash->vendor, flash->name, flash->total_size);
1101 if (!flash->probe && !force) {
1102 msg_gdbg("failed! flashrom has no probe function for "
1103 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001104 continue;
1105 }
stepan782fb172007-04-06 11:58:03 +00001106
ollie5672ac62004-03-17 22:22:08 +00001107 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001108 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001109
hailfinger48ed3e22011-05-04 00:39:50 +00001110 /* Start filling in the dynamic data. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001111 memcpy(fill_flash, flash, sizeof(struct flashchip));
Souvik Ghosh63b92f92016-06-29 18:45:52 -07001112 fill_flash->pgm = pgm;
hailfinger48ed3e22011-05-04 00:39:50 +00001113
hailfinger72d3b982009-05-09 07:27:23 +00001114 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001115 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001116
stugec1e55fe2008-07-02 17:15:47 +00001117 if (force)
1118 break;
stepanc98b80b2006-03-16 16:57:41 +00001119
hailfinger48ed3e22011-05-04 00:39:50 +00001120 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001121 goto notfound;
1122
hailfinger48ed3e22011-05-04 00:39:50 +00001123 /* If this is the first chip found, accept it.
1124 * If this is not the first chip found, accept it only if it is
1125 * a non-generic match.
1126 * We could either make chipcount global or provide it as
1127 * parameter, or we assume that startchip==0 means this call to
1128 * probe_flash() is the first one and thus no chip has been
1129 * found before.
1130 */
1131 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001132 break;
1133
stuge56300c32008-09-03 23:10:05 +00001134notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001135 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001136 }
uwebe4477b2007-08-23 16:08:21 +00001137
stugec1e55fe2008-07-02 17:15:47 +00001138 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001139 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001140
hailfingere11396b2011-03-08 00:09:11 +00001141#if CONFIG_INTERNAL == 1
1142 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001143 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001144 else
1145#endif
stepan3e7aeae2011-01-19 06:21:54 +00001146 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001147
stefanct588b6d22011-06-26 20:45:35 +00001148 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001149 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1150 force ? "Assuming" : "Found", fill_flash->vendor,
1151 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001152 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001153
hailfinger0f4c3952010-12-02 21:59:42 +00001154 /* Flash registers will not be mapped if the chip was forced. Lock info
1155 * may be stored in registers, so avoid lock info printing.
1156 */
1157 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001158 if (fill_flash->printlock)
1159 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001160
hailfinger48ed3e22011-05-04 00:39:50 +00001161 /* Return position of matching chip. */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301162 return flash - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001163}
1164
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001165int verify_flash(struct flashctx *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001166{
hailfingerb0f4d122009-06-24 08:20:45 +00001167 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001168 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001169
snelsone42c3802010-05-07 20:09:04 +00001170 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001171
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001172 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1173 ret = handle_partial_verify(flash, buf, verify_range);
1174 } else {
1175 ret = verify_range(flash, buf, 0, total_size, NULL);
1176 }
uwef6641642007-05-09 10:17:44 +00001177
David Hendricks1ed1d352011-11-23 17:54:37 -08001178 if (ret == ACCESS_DENIED) {
1179 msg_gdbg("Could not fully verify due to access error, ");
1180 if (access_denied_action == error_ignore) {
1181 msg_gdbg("ignoring\n");
1182 ret = 0;
1183 } else {
1184 msg_gdbg("aborting\n");
1185 }
1186 }
1187
hailfingerb0f4d122009-06-24 08:20:45 +00001188 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001189 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001190
hailfingerb0f4d122009-06-24 08:20:45 +00001191 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001192}
1193
uwe8d342eb2011-07-28 08:13:25 +00001194int read_buf_from_file(unsigned char *buf, unsigned long size,
1195 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001196{
1197 unsigned long numbytes;
1198 FILE *image;
1199 struct stat image_stat;
1200
Vincent Palatin7ab23932014-10-01 12:09:16 -07001201 if (!strncmp(filename, "-", sizeof("-")))
1202 image = fdopen(STDIN_FILENO, "rb");
1203 else
1204 image = fopen(filename, "rb");
1205 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001206 perror(filename);
1207 return 1;
1208 }
1209 if (fstat(fileno(image), &image_stat) != 0) {
1210 perror(filename);
1211 fclose(image);
1212 return 1;
1213 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001214 if ((image_stat.st_size != size) &&
1215 (strncmp(filename, "-", sizeof("-")))) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001216 msg_gerr("Error: Image size doesn't match: stat %ld bytes, "
1217 "wanted %ld!\n", image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001218 fclose(image);
1219 return 1;
1220 }
1221 numbytes = fread(buf, 1, size, image);
1222 if (fclose(image)) {
1223 perror(filename);
1224 return 1;
1225 }
1226 if (numbytes != size) {
1227 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1228 "wanted %ld!\n", numbytes, size);
1229 return 1;
1230 }
1231 return 0;
1232}
1233
uwe8d342eb2011-07-28 08:13:25 +00001234int write_buf_to_file(unsigned char *buf, unsigned long size,
1235 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001236{
1237 unsigned long numbytes;
1238 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001239
1240 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001241 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001242 return 1;
1243 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001244 if (!strncmp(filename, "-", sizeof("-")))
1245 image = fdopen(STDOUT_FILENO, "wb");
1246 else
1247 image = fopen(filename, "wb");
1248 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001249 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001250 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001251 }
hailfingerd219a232009-01-28 00:27:54 +00001252
hailfingerd219a232009-01-28 00:27:54 +00001253 numbytes = fwrite(buf, 1, size, image);
1254 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001255 if (numbytes != size) {
1256 msg_gerr("File %s could not be written completely.\n",
1257 filename);
hailfingerd219a232009-01-28 00:27:54 +00001258 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001259 }
hailfingerd219a232009-01-28 00:27:54 +00001260 return 0;
1261}
1262
David Hendrickse3451942013-03-21 17:23:29 -07001263/*
1264 * read_flash - wrapper for flash->read() with additional high-level policy
1265 *
1266 * @flash flash chip
1267 * @buf buffer to store data in
1268 * @start start address
1269 * @len number of bytes to read
1270 *
1271 * This wrapper simplifies most cases when the flash chip needs to be read
1272 * since policy decisions such as non-fatal error handling is centralized.
1273 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001274int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001275 unsigned int start, unsigned int len)
1276{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001277 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001278
1279 if (!flash || !flash->read)
1280 return -1;
1281
David Hendricks4e76fdc2013-05-13 16:05:36 -07001282 ret = flash->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001283 if (ret) {
1284 if (ignore_error(ret)) {
1285 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1286 start, start + len - 1);
1287 ret = 0;
1288 } else {
1289 msg_gdbg("failed to read 0x%x-0x%x\n",
1290 start, start + len - 1);
1291 }
1292 }
1293
1294 return ret;
1295}
1296
David Hendricks7c8a1612013-04-26 19:14:44 -07001297/*
1298 * write_flash - wrapper for flash->write() with additional high-level policy
1299 *
1300 * @flash flash chip
1301 * @buf buffer to write to flash
1302 * @start start address in flash
1303 * @len number of bytes to write
1304 *
1305 * TODO: Look up regions that are write-protected and avoid attempt to write
1306 * to them at all.
1307 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001308int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001309 unsigned int start, unsigned int len)
1310{
1311 if (!flash || !flash->write)
1312 return -1;
1313
1314 return flash->write(flash, buf, start, len);
1315}
1316
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001317int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001318{
1319 unsigned long size = flash->total_size * 1024;
1320 unsigned char *buf = calloc(size, sizeof(char));
1321 int ret = 0;
1322
1323 msg_cinfo("Reading flash... ");
1324 if (!buf) {
1325 msg_gerr("Memory allocation failed!\n");
1326 msg_cinfo("FAILED.\n");
1327 return 1;
1328 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001329
1330 /* To support partial read, fill buffer to all 0xFF at beginning to make
1331 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001332 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001333
hailfinger42a850a2010-07-13 23:56:13 +00001334 if (!flash->read) {
1335 msg_cerr("No read function available for this flash chip.\n");
1336 ret = 1;
1337 goto out_free;
1338 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001339
1340 /* First try to handle partial read case, rather than read the whole
1341 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001342 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001343 if (ret < 0) {
1344 msg_cerr("Partial read operation failed!\n");
1345 ret = 1;
1346 goto out_free;
1347 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001348 int num_regions = get_num_include_args();
1349
1350 if (ret != num_regions) {
1351 msg_cerr("Requested %d regions, but only read %d\n",
1352 num_regions, ret);
1353 ret = 1;
1354 goto out_free;
1355 }
1356
1357 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001358 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001359 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001360 msg_cerr("Read operation failed!\n");
1361 ret = 1;
1362 goto out_free;
1363 }
hailfinger42a850a2010-07-13 23:56:13 +00001364 }
1365
David Hendricksdf29a832013-06-28 14:33:51 -07001366 if (filename)
1367 ret = write_buf_to_file(buf, size, filename);
1368
hailfinger42a850a2010-07-13 23:56:13 +00001369out_free:
1370 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001371 if (ret)
1372 msg_cerr("FAILED.");
1373 else
1374 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001375 return ret;
1376}
1377
hailfingerb437e282010-11-04 01:04:27 +00001378/* This function shares a lot of its structure with erase_and_write_flash() and
1379 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001380 * Even if an error is found, the function will keep going and check the rest.
1381 */
hailfinger48ed3e22011-05-04 00:39:50 +00001382static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001383{
hailfingerb91c08c2011-08-15 19:54:20 +00001384 int i, j, k;
1385 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001386
1387 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1388 unsigned int done = 0;
1389 struct block_eraser eraser = flash->block_erasers[k];
1390
1391 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1392 /* Blocks with zero size are bugs in flashchips.c. */
1393 if (eraser.eraseblocks[i].count &&
1394 !eraser.eraseblocks[i].size) {
1395 msg_gerr("ERROR: Flash chip %s erase function "
1396 "%i region %i has size 0. Please report"
1397 " a bug at flashrom@flashrom.org\n",
1398 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001399 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001400 }
1401 /* Blocks with zero count are bugs in flashchips.c. */
1402 if (!eraser.eraseblocks[i].count &&
1403 eraser.eraseblocks[i].size) {
1404 msg_gerr("ERROR: Flash chip %s erase function "
1405 "%i region %i has count 0. Please report"
1406 " a bug at flashrom@flashrom.org\n",
1407 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001408 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001409 }
1410 done += eraser.eraseblocks[i].count *
1411 eraser.eraseblocks[i].size;
1412 }
hailfinger9fed35d2010-01-19 06:42:46 +00001413 /* Empty eraseblock definition with erase function. */
1414 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001415 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001416 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001417 if (!done)
1418 continue;
1419 if (done != flash->total_size * 1024) {
1420 msg_gerr("ERROR: Flash chip %s erase function %i "
1421 "region walking resulted in 0x%06x bytes total,"
1422 " expected 0x%06x bytes. Please report a bug at"
1423 " flashrom@flashrom.org\n", flash->name, k,
1424 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001425 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001426 }
hailfinger9fed35d2010-01-19 06:42:46 +00001427 if (!eraser.block_erase)
1428 continue;
1429 /* Check if there are identical erase functions for different
1430 * layouts. That would imply "magic" erase functions. The
1431 * easiest way to check this is with function pointers.
1432 */
uwef6f94d42010-03-13 17:28:29 +00001433 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001434 if (eraser.block_erase ==
1435 flash->block_erasers[j].block_erase) {
1436 msg_gerr("ERROR: Flash chip %s erase function "
1437 "%i and %i are identical. Please report"
1438 " a bug at flashrom@flashrom.org\n",
1439 flash->name, k, j);
1440 ret = 1;
1441 }
uwef6f94d42010-03-13 17:28:29 +00001442 }
hailfinger45177872010-01-18 08:14:43 +00001443 }
hailfinger9fed35d2010-01-19 06:42:46 +00001444 return ret;
hailfinger45177872010-01-18 08:14:43 +00001445}
1446
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001447static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001448 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001449 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001450 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001451 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001452 unsigned int addr,
1453 unsigned int len))
1454{
stefanctc5eb8a92011-11-23 09:13:48 +00001455 unsigned int starthere = 0, lenhere = 0;
1456 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001457 int block_was_erased = 0;
hailfingerb437e282010-11-04 01:04:27 +00001458 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1459
hailfinger90fcf9b2010-11-05 14:51:59 +00001460 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001461 * need to be adjusted here to keep the impression of proper abstraction
1462 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001463 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001464 newcontents += start;
1465 msg_cdbg(":");
1466 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001467 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001468 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001469 msg_cdbg("E");
1470 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001471 if (ret) {
1472 if (ret == ACCESS_DENIED)
1473 msg_cdbg("D");
1474 else
1475 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001476 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001477 }
1478
David Hendricks0954ffc2015-11-13 15:15:44 -08001479 if (programmer_table[programmer].paranoid) {
1480 if (check_erased_range(flash, start, len)) {
1481 msg_cerr("ERASE FAILED!\n");
1482 return -1;
1483 }
hailfingerac8e3182011-06-26 17:04:16 +00001484 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001485
hailfinger90fcf9b2010-11-05 14:51:59 +00001486 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001487 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001488 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001489 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001490 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001491 /* get_next_write() sets starthere to a new value after the call. */
1492 while ((lenhere = get_next_write(curcontents + starthere,
1493 newcontents + starthere,
1494 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001495 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001496 if (!writecount++)
1497 msg_cdbg("W");
1498 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001499 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001500 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001501 if (ret) {
1502 if (ret == ACCESS_DENIED)
1503 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001504 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001505 }
David Hendricks048b38c2016-03-28 18:47:06 -07001506
1507 /*
1508 * If the block needed to be erased and was erased successfully
1509 * then we can assume that we didn't run into any write-
1510 * protected areas. Otherwise, we need to verify each page to
1511 * ensure it was successfully written and abort if we encounter
1512 * any errors.
1513 */
1514 if (programmer_table[programmer].paranoid && !block_was_erased) {
1515 if (verify_range(flash, newcontents + starthere,
1516 start + starthere, lenhere, "WRITE"))
1517 return -1;
1518 }
1519
hailfingerb437e282010-11-04 01:04:27 +00001520 starthere += lenhere;
1521 skip = 0;
1522 }
1523 if (skip)
1524 msg_cdbg("S");
1525 return ret;
1526}
1527
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001528static int walk_eraseregions(struct flashctx *flash, int erasefunction,
1529 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001530 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001531 unsigned int len,
1532 uint8_t *param1,
1533 uint8_t *param2,
1534 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001535 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001536 unsigned int addr,
1537 unsigned int len)),
1538 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001539{
David Hendricks605544b2015-08-15 16:32:58 -07001540 int i, j, rc = -1;
hailfingerb91c08c2011-08-15 19:54:20 +00001541 unsigned int start = 0;
1542 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001543 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001544
hailfinger2b8c9382010-07-13 00:37:19 +00001545 for (i = 0; i < NUM_ERASEREGIONS; i++) {
David Hendricks605544b2015-08-15 16:32:58 -07001546 /* count==0 and size==0 for all automatically initialized array
hailfinger2b8c9382010-07-13 00:37:19 +00001547 * members so the loop below won't be executed for them.
1548 */
1549 len = eraser.eraseblocks[i].size;
David Hendricks605544b2015-08-15 16:32:58 -07001550 if (!len)
1551 continue;
1552
1553 if (required_erase_size && (len != required_erase_size)) {
1554 msg_cdbg("%u does not meet erase alignment", len);
1555 rc = -1;
1556 break;
1557 }
1558
hailfinger2b8c9382010-07-13 00:37:19 +00001559 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001560 /* Print this for every block except the first one. */
1561 if (i || j)
1562 msg_cdbg(", ");
1563 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001564 start + len - 1);
David Hendricks1ed1d352011-11-23 17:54:37 -08001565 rc = do_something(flash, start, len, param1, param2,
1566 eraser.block_erase);
1567 if (rc) {
1568 if (ignore_error(rc))
1569 rc = 0;
1570 else
1571 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001572 }
hailfinger2b8c9382010-07-13 00:37:19 +00001573 start += len;
1574 }
1575 }
hailfingerb437e282010-11-04 01:04:27 +00001576 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001577 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001578}
1579
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001580static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001581{
1582 struct block_eraser eraser = flash->block_erasers[k];
1583
1584 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1585 if (log)
1586 msg_cdbg("not defined. ");
1587 return 1;
1588 }
1589 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1590 if (log)
1591 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001592 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001593 return 1;
1594 }
1595 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1596 if (log)
1597 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001598 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001599 return 1;
1600 }
1601 return 0;
1602}
1603
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001604int erase_and_write_flash(struct flashctx *flash, uint8_t *oldcontents,
uwe8d342eb2011-07-28 08:13:25 +00001605 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001606{
hailfinger8f524a82011-07-21 21:21:04 +00001607 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001608 uint8_t *curcontents;
1609 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001610 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001611
hailfingercf848f12010-12-05 15:14:44 +00001612 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001613 curcontents = malloc(size);
1614 if (!curcontents) {
1615 msg_gerr("Out of memory!\n");
1616 exit(1);
1617 }
hailfingerb437e282010-11-04 01:04:27 +00001618 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1619 memcpy(curcontents, oldcontents, size);
1620
hailfinger7df21362009-09-05 02:30:58 +00001621 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001622 if (k != 0)
1623 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001624 if (!usable_erasefunctions) {
1625 msg_cdbg("No usable erase functions left.\n");
1626 break;
1627 }
stefanctf0bc4712011-07-26 14:28:35 +00001628 msg_cdbg("Trying erase function %i... ", k);
1629 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001630 continue;
hailfingercf848f12010-12-05 15:14:44 +00001631 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001632 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1633 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001634 /* If everything is OK, don't try another erase function. */
1635 if (!ret)
1636 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001637 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001638 * contents are. If no usable erase functions remain, we can
1639 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001640 */
hailfingercf848f12010-12-05 15:14:44 +00001641 if (!usable_erasefunctions)
1642 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001643 /* Reading the whole chip may take a while, inform the user even
1644 * in non-verbose mode.
1645 */
1646 msg_cinfo("Reading current flash chip contents... ");
David Hendrickse3451942013-03-21 17:23:29 -07001647 if (read_flash(flash, curcontents, 0, size)) {
hailfinger6237f5e2010-12-02 02:41:55 +00001648 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001649 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001650 /* We have no idea about the flash chip contents, so
1651 * retrying with another erase function is pointless.
1652 */
1653 break;
1654 }
David Hendricks89a45e52011-11-22 16:56:22 -08001655 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001656 }
hailfingerb437e282010-11-04 01:04:27 +00001657 /* Free the scratchpad. */
1658 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001659
hailfinger7df21362009-09-05 02:30:58 +00001660 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001661 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001662 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001663 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001664 }
1665 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001666}
1667
hailfinger4c47e9d2010-10-19 22:06:20 +00001668void nonfatal_help_message(void)
1669{
1670 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1671 "This means we have to add special support for your board, "
1672 "programmer or flash chip.\n"
1673 "Please report this on IRC at irc.freenode.net (channel "
1674 "#flashrom) or\n"
1675 "mail flashrom@flashrom.org!\n"
1676 "-------------------------------------------------------------"
1677 "------------------\n"
1678 "You may now reboot or simply leave the machine running.\n");
1679}
1680
uweb34ec9f2009-10-01 18:40:02 +00001681void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001682{
snelsone42c3802010-05-07 20:09:04 +00001683 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001684 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001685 "mail flashrom@flashrom.org with FAILED: your board name in "
1686 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001687 "-------------------------------------------------------------"
1688 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001689 "DO NOT REBOOT OR POWEROFF!\n");
1690}
1691
uwe8d342eb2011-07-28 08:13:25 +00001692/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001693void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001694{
1695 enum programmer p;
1696 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001697 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001698 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001699 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001700 }
Simon Glass8dc82732013-07-16 10:13:51 -06001701 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001702}
1703
hailfingerf79d1712010-10-06 23:48:34 +00001704void list_programmers_linebreak(int startcol, int cols, int paren)
1705{
1706 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001707 int pnamelen;
1708 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001709 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001710 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001711
1712 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1713 pname = programmer_table[p].name;
1714 pnamelen = strlen(pname);
1715 if (remaining - pnamelen - 2 < 0) {
1716 if (firstline)
1717 firstline = 0;
1718 else
1719 printf("\n");
1720 for (i = 0; i < startcol; i++)
1721 printf(" ");
1722 remaining = cols - startcol;
1723 } else {
1724 printf(" ");
1725 remaining--;
1726 }
1727 if (paren && (p == 0)) {
1728 printf("(");
1729 remaining--;
1730 }
1731 printf("%s", pname);
1732 remaining -= pnamelen;
1733 if (p < PROGRAMMER_INVALID - 1) {
1734 printf(",");
1735 remaining--;
1736 } else {
1737 if (paren)
1738 printf(")");
1739 printf("\n");
1740 }
1741 }
1742}
1743
hailfinger3b471632010-03-27 16:36:40 +00001744void print_sysinfo(void)
1745{
David Hendricksc6c9f822010-11-03 15:07:01 -07001746 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001747#if HAVE_UTSNAME == 1
1748 struct utsname osinfo;
1749 uname(&osinfo);
1750
David Hendricksc6c9f822010-11-03 15:07:01 -07001751 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001752 osinfo.machine);
1753#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001754 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001755#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001756}
1757
1758void print_buildinfo(void)
1759{
1760 msg_gdbg("flashrom was built with");
hailfinger3b471632010-03-27 16:36:40 +00001761#if NEED_PCI == 1
1762#ifdef PCILIB_VERSION
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001763 msg_gdbg(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001764#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001765 msg_gdbg(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001766#endif
1767#endif
1768#ifdef __clang__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001769 msg_gdbg(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001770#ifdef __clang_version__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001771 msg_gdbg(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001772#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001773 msg_gdbg(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001774#endif
hailfinger3b471632010-03-27 16:36:40 +00001775#elif defined(__GNUC__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001776 msg_gdbg(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001777#ifdef __VERSION__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001778 msg_gdbg(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001779#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001780 msg_gdbg(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001781#endif
1782#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001783 msg_gdbg(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001784#endif
1785#if defined (__FLASHROM_LITTLE_ENDIAN__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001786 msg_gdbg(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001787#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001788 msg_gdbg(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001789#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001790 msg_gdbg("\n");
hailfinger3b471632010-03-27 16:36:40 +00001791}
1792
uwefdeca092008-01-21 15:24:22 +00001793void print_version(void)
1794{
David Hendricksc6c9f822010-11-03 15:07:01 -07001795 /* send to stderr for chromium os */
1796 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001797 print_sysinfo();
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001798 msg_ginfo("\n");
uwefdeca092008-01-21 15:24:22 +00001799}
1800
hailfinger74819ad2010-05-15 15:04:37 +00001801void print_banner(void)
1802{
1803 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001804 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001805 msg_ginfo("\n");
1806}
1807
hailfingerc77acb52009-12-24 02:15:55 +00001808int selfcheck(void)
1809{
hailfinger45177872010-01-18 08:14:43 +00001810 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001811 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001812
1813 /* Safety check. Instead of aborting after the first error, check
1814 * if more errors exist.
1815 */
hailfingerc77acb52009-12-24 02:15:55 +00001816 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001817 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001818 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001819 }
stefanct8632c922011-07-26 14:33:46 +00001820 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001821 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001822 * For 'flashchips' we check the first element to be non-null. In the
1823 * other cases there exist use cases where the first element can be
1824 * null. */
Yunlian Jiang945c3392015-01-20 15:56:44 -08001825 if (flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001826 msg_gerr("Flashchips table miscompilation!\n");
1827 ret = 1;
1828 }
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001829 /* Check that virtual_memory in struct flashctx is placed directly
1830 * after the members copied from struct flashchip.
1831 */
1832 if (sizeof(struct flashchip) != offsetof(struct flashctx, virtual_memory)) {
1833 msg_gerr("struct flashctx broken!\n");
1834 ret = 1;
1835 }
hailfinger45177872010-01-18 08:14:43 +00001836 for (flash = flashchips; flash && flash->name; flash++)
1837 if (selfcheck_eraseblocks(flash))
1838 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001839
hailfinger45177872010-01-18 08:14:43 +00001840 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001841}
1842
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001843void check_chip_supported(const struct flashctx *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001844{
1845 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001846 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001847 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001848 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001849 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001850 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001851 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001852 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001853 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001854 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001855 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001856 msg_cdbg(" WRITE");
Duncan Laurie06ffd522015-10-26 12:56:08 -07001857 if (flash->tested & TEST_BAD_UREAD)
1858 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001859 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001860 }
1861 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1862 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1863 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1864 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001865 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001866 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001867 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001868 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001869 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001870 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001871 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001872 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001873 msg_cdbg(" WRITE");
Duncan Laurie06ffd522015-10-26 12:56:08 -07001874 if (!(flash->tested & TEST_BAD_UREAD) && !(flash->tested & TEST_OK_UREAD))
1875 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001876 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001877 }
hailfinger92cd8e32010-01-07 03:24:05 +00001878 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001879 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001880 "in the latest development\n"
1881 "version of flashrom. If you are running the latest "
1882 "development version,\n"
1883 "please email a report to flashrom@flashrom.org if "
1884 "any of the above operations\n"
1885 "work correctly for you with this flash part. Please "
1886 "include the flashrom\n"
1887 "output with the additional -V option for all "
1888 "operations you tested (-V, -Vr,\n"
1889 "-Vw, -VE), and mention which mainboard or "
1890 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001891 "Please mention your board in the subject line. "
1892 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001893 }
1894}
1895
hailfinger771fc182010-10-15 00:01:14 +00001896/* FIXME: This function signature needs to be improved once doit() has a better
1897 * function signature.
1898 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001899int 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 +00001900{
1901 if (!programmer_may_write && (write_it || erase_it)) {
1902 msg_perr("Write/erase is not working yet on your programmer in "
1903 "its current configuration.\n");
1904 /* --force is the wrong approach, but it's the best we can do
1905 * until the generic programmer parameter parser is merged.
1906 */
1907 if (!force)
1908 return 1;
1909 msg_cerr("Continuing anyway.\n");
1910 }
1911
1912 if (read_it || erase_it || write_it || verify_it) {
1913 /* Everything needs read. */
1914 if (flash->tested & TEST_BAD_READ) {
1915 msg_cerr("Read is not working on this chip. ");
1916 if (!force)
1917 return 1;
1918 msg_cerr("Continuing anyway.\n");
1919 }
1920 if (!flash->read) {
1921 msg_cerr("flashrom has no read function for this "
1922 "flash chip.\n");
1923 return 1;
1924 }
1925 }
1926 if (erase_it || write_it) {
1927 /* Write needs erase. */
1928 if (flash->tested & TEST_BAD_ERASE) {
1929 msg_cerr("Erase is not working on this chip. ");
1930 if (!force)
1931 return 1;
1932 msg_cerr("Continuing anyway.\n");
1933 }
stefancte1c5acf2011-07-04 07:27:17 +00001934 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001935 msg_cerr("flashrom has no erase function for this "
1936 "flash chip.\n");
1937 return 1;
1938 }
hailfinger771fc182010-10-15 00:01:14 +00001939 }
1940 if (write_it) {
1941 if (flash->tested & TEST_BAD_WRITE) {
1942 msg_cerr("Write is not working on this chip. ");
1943 if (!force)
1944 return 1;
1945 msg_cerr("Continuing anyway.\n");
1946 }
1947 if (!flash->write) {
1948 msg_cerr("flashrom has no write function for this "
1949 "flash chip.\n");
1950 return 1;
1951 }
1952 }
1953 return 0;
1954}
1955
hailfingerc77acb52009-12-24 02:15:55 +00001956/* This function signature is horrible. We need to design a better interface,
1957 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001958 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001959 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001960int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09001961 int write_it, int erase_it, int verify_it, int extract_it,
1962 const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001963{
hailfinger4c47e9d2010-10-19 22:06:20 +00001964 uint8_t *oldcontents;
1965 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001966 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001967 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001968
stefanct02116582011-05-18 01:30:56 +00001969 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001970 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001971 ret = 1;
1972 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001973 }
1974
hailfinger771fc182010-10-15 00:01:14 +00001975 /* Given the existence of read locks, we want to unlock for read,
1976 * erase and write.
1977 */
1978 if (flash->unlock)
1979 flash->unlock(flash);
1980
David Hendricks6d62d752011-03-07 21:20:22 -08001981 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001982 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001983 ret = 1;
1984 goto out_nofree;
1985 }
1986
Simon Glass9ad06c12013-07-03 22:08:17 +09001987 if (extract_it) {
1988 ret = extract_regions(flash);
1989 goto out_nofree;
1990 }
1991
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001992 /* mark entries included using -i argument as "included" if they are
1993 found in the master rom_entries list */
1994 if (process_include_args() < 0) {
1995 ret = 1;
1996 goto out_nofree;
1997 }
1998
hailfinger771fc182010-10-15 00:01:14 +00001999 if (read_it) {
2000 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00002001 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00002002 }
hailfingerb437e282010-11-04 01:04:27 +00002003
stefanctd611e8f2011-07-12 22:35:21 +00002004 oldcontents = malloc(size);
2005 if (!oldcontents) {
2006 msg_gerr("Out of memory!\n");
2007 exit(1);
2008 }
Simon Glass4c214132013-07-16 10:09:28 -06002009 /* Assume worst case: All blocks are not erased. */
2010 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002011 newcontents = malloc(size);
2012 if (!newcontents) {
2013 msg_gerr("Out of memory!\n");
2014 exit(1);
2015 }
Simon Glass4c214132013-07-16 10:09:28 -06002016 /* Assume best case: All blocks are erased. */
2017 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002018 /* Side effect of the assumptions above: Default write action is erase
2019 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002020 * oldcontents being completely unerased means we have to erase
2021 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002022 */
2023
hailfingerd217d122010-10-08 18:52:29 +00002024 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002025 /*
2026 * Note: This must be done before any files specified by -i
2027 * arguments are processed merged into the newcontents since
2028 * -i files take priority. See http://crbug.com/263495.
2029 */
2030 if (filename) {
2031 if (read_buf_from_file(newcontents, size, filename)) {
2032 ret = 1;
2033 goto out;
2034 }
2035 } else {
2036 /* Content will be read from -i args, so they must
2037 * not overlap. */
2038 if (included_regions_overlap()) {
2039 msg_gerr("Error: Included regions must "
2040 "not overlap.\n");
2041 ret = 1;
2042 goto out;
2043 }
stepan1da96c02006-11-21 23:48:51 +00002044 }
2045
David Hendricksac82cac2012-06-19 10:29:37 -07002046#if 0
2047 /*
2048 * FIXME: show_id() causes failure if vendor:mainboard do not
2049 * match. This may happen if codenames are in flux.
2050 * See chrome-os-partner:10414.
2051 */
hailfinger90c7d542010-05-31 15:27:27 +00002052#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002053 if (programmer == PROGRAMMER_INTERNAL)
2054 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002055#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002056#endif
ollie5672ac62004-03-17 22:22:08 +00002057 }
2058
David Hendricksc44d7a02011-10-17 11:28:43 -07002059 /* Obtain a reference image so that we can check whether regions need
2060 * to be erased and to give better diagnostics in case write fails.
David Hendricks52ddff02013-07-23 15:05:14 -07002061 * If --fast-verify is used then only the regions which are included
2062 * using -i will be read.
hailfinger4c47e9d2010-10-19 22:06:20 +00002063 */
David Hendricksc44d7a02011-10-17 11:28:43 -07002064 if (diff_file) {
2065 msg_cdbg("Reading old contents from file... ");
2066 if (read_buf_from_file(oldcontents, size, diff_file)) {
2067 ret = 1;
2068 msg_cdbg("FAILED.\n");
2069 goto out;
2070 }
2071 } else {
2072 msg_cdbg("Reading old contents from flash chip... ");
David Hendricksd4e712c2013-08-02 17:06:16 -07002073 if (verify_it == VERIFY_PARTIAL) {
2074 if (handle_partial_read(flash, oldcontents,
2075 read_flash, 0) < 0) {
David Hendricks52ddff02013-07-23 15:05:14 -07002076 ret = 1;
2077 msg_cdbg("FAILED.\n");
2078 goto out;
2079 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002080 } else {
2081 if (read_flash(flash, oldcontents, 0, size)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002082 ret = 1;
2083 msg_cdbg("FAILED.\n");
2084 goto out;
2085 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002086 }
hailfinger4c47e9d2010-10-19 22:06:20 +00002087 }
David Hendricks89a45e52011-11-22 16:56:22 -08002088 msg_cdbg("done.\n");
David Hendricksdf29a832013-06-28 14:33:51 -07002089 /*
2090 * Note: This must be done after reading the file specified for the
2091 * -w/-v argument, if any, so that files specified using -i end up
2092 * in the "newcontents" buffer before being written.
2093 * See http://crbug.com/263495.
2094 */
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002095 if (handle_romentries(flash, oldcontents, newcontents)) {
2096 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002097 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002098 goto out;
2099 }
uwef6641642007-05-09 10:17:44 +00002100
David Hendricksa7e114b2016-02-26 18:49:15 -08002101 if (erase_it) {
2102 /* FIXME: Do we really want the scary warning if erase failed?
2103 * After all, after erase the chip is either blank or partially
2104 * blank or it has the old contents. A blank chip won't boot,
2105 * so if the user wanted erase and reboots afterwards, the user
2106 * knows very well that booting won't work.
2107 */
2108 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2109 emergency_help_message();
2110 ret = 1;
2111 }
2112 goto out;
2113 }
2114
stuge8ce3a3c2008-04-28 14:47:30 +00002115 if (write_it) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002116 // parse the new fmap
Souvik Ghosh63b92f92016-06-29 18:45:52 -07002117 if ((ret = cros_ec_prepare(flash, newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002118 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002119 goto out;
2120 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002121
hailfingerb437e282010-11-04 01:04:27 +00002122 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2123 msg_cerr("Uh oh. Erase/write failed. Checking if "
2124 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002125 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002126 if (!memcmp(oldcontents, newcontents, size)) {
2127 msg_cinfo("Good. It seems nothing was "
2128 "changed.\n");
2129 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002130 ret = 1;
2131 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002132 }
2133 }
hailfingerd217d122010-10-08 18:52:29 +00002134 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002135 ret = 1;
2136 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002137 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002138
Souvik Ghosh63b92f92016-06-29 18:45:52 -07002139 ret = cros_ec_need_2nd_pass(flash);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002140 if (ret < 0) {
2141 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002142 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002143 emergency_help_message();
2144 ret = 1;
2145 goto out;
2146 } else if (ret > 0) {
2147 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002148 msg_pdbg("CROS_EC needs 2nd pass.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002149 if (read_flash(flash, oldcontents, 0, size)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002150 msg_cerr("Uh oh. Cannot get latest content.\n");
2151 emergency_help_message();
2152 ret = 1;
2153 goto out;
2154 }
2155 // write 2nd pass
2156 if (erase_and_write_flash(flash, oldcontents,
2157 newcontents)) {
David Hendricksb907de32014-08-11 16:47:09 -07002158 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002159 emergency_help_message();
2160 ret = 1;
2161 goto out;
2162 }
2163 ret = 0;
2164 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002165
Souvik Ghosh63b92f92016-06-29 18:45:52 -07002166 if (cros_ec_finish(flash) < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002167 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002168 emergency_help_message();
2169 ret = 1;
2170 goto out;
2171 }
stuge8ce3a3c2008-04-28 14:47:30 +00002172 }
ollie6a600992005-11-26 21:55:36 +00002173
hailfinger0459e1c2009-08-19 13:55:34 +00002174 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002175 if ((write_it || erase_it) && !content_has_changed) {
2176 msg_gdbg("Nothing was erased or written, skipping "
2177 "verification\n");
2178 } else {
2179 /* Work around chips which need some time to calm down. */
2180 if (write_it && verify_it != VERIFY_PARTIAL)
2181 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002182
David Hendricks9ba79fb2015-04-03 12:06:16 -07002183 ret = verify_flash(flash, newcontents, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002184
David Hendricks9ba79fb2015-04-03 12:06:16 -07002185 /* If we tried to write, and verification now fails, we
2186 * might have an emergency situation.
2187 */
2188 if (ret && write_it)
2189 emergency_help_message();
2190 }
hailfinger0459e1c2009-08-19 13:55:34 +00002191 }
ollie6a600992005-11-26 21:55:36 +00002192
hailfinger90fcf9b2010-11-05 14:51:59 +00002193out:
2194 free(oldcontents);
2195 free(newcontents);
2196out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002197 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002198 /*
2199 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2200 * tree. This is because some operations, such as write protection,
2201 * requires programmer_shutdown() but does not call doit().
2202 */
2203// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002204 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002205}