blob: c0b97c30e8a92b0918556a30a93b550e2b6488db [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
uwe4475e902009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
hailfinger23060112009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
rminnich8d3ff912003-10-25 17:01:29 +000019 */
20
hailfingera83a5fe2010-05-30 22:24:40 +000021#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000022#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000023#ifndef __LIBPAYLOAD__
24#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000026#endif
rminnich8d3ff912003-10-25 17:01:29 +000027#include <string.h>
28#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000029#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000030#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000031#if HAVE_UTSNAME == 1
32#include <sys/utsname.h>
33#endif
Vincent Palatin7ab23932014-10-01 12:09:16 -070034#include <unistd.h>
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -070035
36#include "action_descriptor.h"
rminnich8d3ff912003-10-25 17:01:29 +000037#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000038#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090039#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000040#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000041
krause2eb76212011-01-17 07:50:42 +000042const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000043char *chip_to_probe = NULL;
Edward O'Callaghan8d8d3972019-02-24 20:40:10 +110044enum flashrom_log_level verbose_screen = FLASHROM_MSG_ERROR;
45enum flashrom_log_level verbose_logfile = FLASHROM_MSG_DEBUG2;
hailfinger80422e22009-12-13 22:28:00 +000046
David Hendricks9ba79fb2015-04-03 12:06:16 -070047/* Set if any erase/write operation is to be done. This will be used to
48 * decide if final verification is needed. */
49static int content_has_changed = 0;
50
David Hendricks1ed1d352011-11-23 17:54:37 -080051/* error handling stuff */
52enum error_action access_denied_action = error_ignore;
53
54int ignore_error(int err) {
55 int rc = 0;
56
57 switch(err) {
58 case ACCESS_DENIED:
59 if (access_denied_action == error_ignore)
60 rc = 1;
61 break;
62 default:
63 break;
64 }
65
66 return rc;
67}
68
hailfinger969e2f32011-09-08 00:00:29 +000069static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000070
hailfingerddeb4ac2010-07-08 10:13:37 +000071static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000072
David Hendricksac1d25c2016-08-09 17:00:58 -070073/* Supported buses for the current programmer. */
74enum chipbustype buses_supported;
75
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
Patrick Georgi8ddfee92017-03-20 14:54:28 +0100308 {0}, /* 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
David Hendricksac1d25c2016-08-09 17:00:58 -0700377int programmer_init(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 };
David Hendricksac1d25c2016-08-09 17:00:58 -0700394 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000395 /* Default to top aligned flash at 4 GB. */
396 flashbase = 0;
397 /* Registering shutdown functions is now allowed. */
398 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000399 /* Default to allowing writes. Broken programmers set this to 0. */
400 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000401
402 programmer_param = param;
403 msg_pdbg("Initializing %s programmer\n",
404 programmer_table[programmer].name);
David Hendricksac1d25c2016-08-09 17:00:58 -0700405 ret = programmer_table[programmer].init();
hailfinger1ef766d2010-07-06 09:55:48 +0000406 return ret;
uweabe92a52009-05-16 22:36:00 +0000407}
408
David Hendricksbf36f092010-11-02 23:39:29 -0700409int chip_restore()
410{
411 int rc = 0;
412
413 while (chip_restore_fn_count > 0) {
414 int i = --chip_restore_fn_count;
415 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
416 chip_restore_fn[i].status);
417 }
418
419 return rc;
420}
421
David Hendricks93784b42016-08-09 17:00:38 -0700422int programmer_shutdown(void)
uweabe92a52009-05-16 22:36:00 +0000423{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000424 int ret = 0;
425
hailfinger1ff33dc2010-07-03 11:02:10 +0000426 /* Registering shutdown functions is no longer allowed. */
427 may_register_shutdown = 0;
428 while (shutdown_fn_count > 0) {
429 int i = --shutdown_fn_count;
David Hendricks93784b42016-08-09 17:00:38 -0700430 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000431 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000432 return ret;
uweabe92a52009-05-16 22:36:00 +0000433}
434
435void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
436 size_t len)
437{
438 return programmer_table[programmer].map_flash_region(descr,
439 phys_addr, len);
440}
441
442void programmer_unmap_flash_region(void *virt_addr, size_t len)
443{
444 programmer_table[programmer].unmap_flash_region(virt_addr, len);
445}
446
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700447void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000448{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100449 par_master->chip_writeb(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000450}
451
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700452void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000453{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100454 par_master->chip_writew(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000455}
456
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700457void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000458{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100459 par_master->chip_writel(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000460}
461
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700462void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000463{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100464 par_master->chip_writen(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000465}
466
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700467uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000468{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100469 return par_master->chip_readb(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000470}
471
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700472uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000473{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100474 return par_master->chip_readw(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000475}
476
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700477uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000478{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100479 return par_master->chip_readl(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000480}
481
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700482void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000483{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100484 par_master->chip_readn(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000485}
486
hailfingere5829f62009-06-05 17:48:08 +0000487void programmer_delay(int usecs)
488{
489 programmer_table[programmer].delay(usecs);
490}
491
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700492void map_flash_registers(struct flashctx *flash)
stepan15e64bc2007-05-24 08:48:10 +0000493{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100494 size_t size = flash->chip->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000495 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000496 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000497 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000498}
499
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700500int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000501{
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700502 chip_readn(flash, buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000503
hailfinger23060112009-05-08 12:49:03 +0000504 return 0;
505}
506
David Hendricksda18f692016-10-21 17:49:49 -0700507/* This is a somewhat hacked function similar in some ways to strtok(). It will
508 * look for needle with a subsequent '=' in haystack, return a copy of needle.
hailfinger6e5a52a2009-11-24 18:27:10 +0000509 */
stefanct52700282011-06-26 17:38:17 +0000510char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000511{
David Hendricksda18f692016-10-21 17:49:49 -0700512 char *param_pos, *opt_pos;
hailfinger1ef766d2010-07-06 09:55:48 +0000513 char *opt = NULL;
514 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000515 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000516
hailfingerf4aaccc2010-04-28 15:22:14 +0000517 needlelen = strlen(needle);
518 if (!needlelen) {
519 msg_gerr("%s: empty needle! Please report a bug at "
520 "flashrom@flashrom.org\n", __func__);
521 return NULL;
522 }
523 /* No programmer parameters given. */
524 if (*haystack == NULL)
525 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000526 param_pos = strstr(*haystack, needle);
527 do {
528 if (!param_pos)
529 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000530 /* Needle followed by '='? */
531 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600532
hailfinger1ef766d2010-07-06 09:55:48 +0000533 /* Beginning of the string? */
534 if (param_pos == *haystack)
535 break;
536 /* After a delimiter? */
537 if (strchr(delim, *(param_pos - 1)))
538 break;
539 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000540 /* Continue searching. */
541 param_pos++;
542 param_pos = strstr(param_pos, needle);
543 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000544
hailfinger6e5a52a2009-11-24 18:27:10 +0000545 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000546 /* Get the string after needle and '='. */
547 opt_pos = param_pos + needlelen + 1;
548 optlen = strcspn(opt_pos, delim);
549 /* Return an empty string if the parameter was empty. */
550 opt = malloc(optlen + 1);
551 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000552 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000553 exit(1);
554 }
hailfinger1ef766d2010-07-06 09:55:48 +0000555 strncpy(opt, opt_pos, optlen);
556 opt[optlen] = '\0';
hailfinger6e5a52a2009-11-24 18:27:10 +0000557 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000558
hailfinger1ef766d2010-07-06 09:55:48 +0000559 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000560}
561
stefanct52700282011-06-26 17:38:17 +0000562char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000563{
564 return extract_param(&programmer_param, param_name, ",");
565}
566
stefancte1c5acf2011-07-04 07:27:17 +0000567/* Returns the number of well-defined erasers for a chip. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700568static unsigned int count_usable_erasers(const struct flashctx *flash)
stefanct569dbb62011-07-01 00:19:12 +0000569{
570 unsigned int usable_erasefunctions = 0;
571 int k;
572 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
573 if (!check_block_eraser(flash, k, 0))
574 usable_erasefunctions++;
575 }
576 return usable_erasefunctions;
577}
578
hailfinger7af83692009-06-15 17:23:36 +0000579/* start is an offset to the base address of the flash chip */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700580int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000581{
582 int ret;
583 uint8_t *cmpbuf = malloc(len);
584
585 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000586 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000587 exit(1);
588 }
Simon Glass4c214132013-07-16 10:09:28 -0600589 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000590 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
591 free(cmpbuf);
592 return ret;
593}
594
Simon Glass4e305f42015-01-08 06:29:04 -0700595static int compare_chunk(uint8_t *readbuf, uint8_t *cmpbuf, unsigned int start,
596 unsigned int len, const char *message)
597{
598 int failcount = 0, i;
599
600 for (i = 0; i < len; i++) {
601 if (cmpbuf[i] != readbuf[i]) {
602 if (!failcount) {
603 msg_cerr("%s FAILED at 0x%08x! "
604 "Expected=0x%02x, Read=0x%02x,",
605 message, start + i,
606 cmpbuf[i], readbuf[i]);
607 }
608 failcount++;
609 }
610 }
611
612 return failcount;
613}
614
uwee15beb92010-08-08 17:01:18 +0000615/*
hailfinger7af3d192009-11-25 17:05:52 +0000616 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000617 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000618 * @start offset to the base address of the flash chip
619 * @len length of the verified area
620 * @message string to print in the "FAILED" message
621 * @return 0 for success, -1 for failure
622 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700623int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000624 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000625{
hailfinger8cb6ece2010-11-16 17:21:58 +0000626 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000627 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000628
629 if (!len)
630 goto out_free;
631
Patrick Georgif3fa2992017-02-02 16:24:44 +0100632 if (!flash->chip->read) {
snelsone42c3802010-05-07 20:09:04 +0000633 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000634 return 1;
635 }
hailfinger7af83692009-06-15 17:23:36 +0000636 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000637 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000638 exit(1);
639 }
640
Patrick Georgif3fa2992017-02-02 16:24:44 +0100641 if (start + len > flash->chip->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000642 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000643 " total_size 0x%x\n", __func__, start, len,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100644 flash->chip->total_size * 1024);
hailfinger7af83692009-06-15 17:23:36 +0000645 ret = -1;
646 goto out_free;
647 }
648 if (!message)
649 message = "VERIFY";
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -0700650 msg_gdbg("%#06x..%#06x ", start, start + len -1);
Simon Glass4e305f42015-01-08 06:29:04 -0700651 if (programmer_table[programmer].paranoid) {
652 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800653
Simon Glass4e305f42015-01-08 06:29:04 -0700654 /* limit chunksize in order to catch errors early */
655 for (i = 0, chunksize = 0; i < len; i += chunksize) {
656 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800657
Patrick Georgif3fa2992017-02-02 16:24:44 +0100658 chunksize = min(flash->chip->page_size, len - i);
659 tmp = flash->chip->read(flash, readbuf + i, start + i, chunksize);
Simon Glass4e305f42015-01-08 06:29:04 -0700660 if (tmp) {
661 ret = tmp;
662 if (ignore_error(tmp))
663 continue;
664 else
665 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800666 }
Simon Glass4e305f42015-01-08 06:29:04 -0700667
668 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
669 chunksize, message);
670 if (failcount)
671 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800672 }
Simon Glass4e305f42015-01-08 06:29:04 -0700673 } else {
674 int tmp;
675
676 /* read as much as we can to reduce transaction overhead */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100677 tmp = flash->chip->read(flash, readbuf, start, len);
Simon Glass4e305f42015-01-08 06:29:04 -0700678 if (tmp && !ignore_error(tmp)) {
679 ret = tmp;
680 goto out_free;
681 }
682
683 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000684 }
685
hailfinger5be6c0f2009-07-23 01:42:56 +0000686 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000687 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000688 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000689 ret = -1;
690 }
hailfinger7af83692009-06-15 17:23:36 +0000691
692out_free:
693 free(readbuf);
694 return ret;
695}
696
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100697/* Helper function for need_erase() that focuses on granularities of gran bytes. */
698static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
699 unsigned int gran)
700{
701 unsigned int i, j, limit;
702 for (j = 0; j < len / gran; j++) {
703 limit = min (gran, len - j * gran);
704 /* Are 'have' and 'want' identical? */
705 if (!memcmp(have + j * gran, want + j * gran, limit))
706 continue;
707 /* have needs to be in erased state. */
708 for (i = 0; i < limit; i++)
709 if (have[j * gran + i] != 0xff)
710 return 1;
711 }
712 return 0;
713}
714
uwee15beb92010-08-08 17:01:18 +0000715/*
hailfingerb247c7a2010-03-08 00:42:32 +0000716 * Check if the buffer @have can be programmed to the content of @want without
717 * erasing. This is only possible if all chunks of size @gran are either kept
718 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000719 *
hailfingerb247c7a2010-03-08 00:42:32 +0000720 * The following write granularities (enum @gran) are known:
721 * - 1 bit. Each bit can be cleared individually.
722 * - 1 byte. A byte can be written once. Further writes to an already written
723 * byte cause the contents to be either undefined or to stay unchanged.
724 * - 128 bytes. If less than 128 bytes are written, the rest will be
725 * erased. Each write to a 128-byte region will trigger an automatic erase
726 * before anything is written. Very uncommon behaviour and unsupported by
727 * this function.
728 * - 256 bytes. If less than 256 bytes are written, the contents of the
729 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000730 * Warning: This function assumes that @have and @want point to naturally
731 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000732 *
733 * @have buffer with current content
734 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000735 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000736 * @gran write granularity (enum, not count)
737 * @return 0 if no erase is needed, 1 otherwise
738 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700739static int need_erase(struct flashctx *flash, uint8_t *have, uint8_t *want,
Simon Glass4c214132013-07-16 10:09:28 -0600740 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000741{
hailfingerb91c08c2011-08-15 19:54:20 +0000742 int result = 0;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100743 unsigned int i;
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700744
hailfingerb247c7a2010-03-08 00:42:32 +0000745 switch (gran) {
746 case write_gran_1bit:
747 for (i = 0; i < len; i++)
748 if ((have[i] & want[i]) != want[i]) {
749 result = 1;
750 break;
751 }
752 break;
753 case write_gran_1byte:
754 for (i = 0; i < len; i++)
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100755 if ((have[i] != want[i]) && (have[i] != 0xff)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000756 result = 1;
757 break;
758 }
759 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100760 case write_gran_128bytes:
761 result = need_erase_gran_bytes(have, want, len, 128);
762 break;
hailfingerb247c7a2010-03-08 00:42:32 +0000763 case write_gran_256bytes:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100764 result = need_erase_gran_bytes(have, want, len, 256);
765 break;
766 case write_gran_264bytes:
767 result = need_erase_gran_bytes(have, want, len, 264);
768 break;
769 case write_gran_512bytes:
770 result = need_erase_gran_bytes(have, want, len, 512);
771 break;
772 case write_gran_528bytes:
773 result = need_erase_gran_bytes(have, want, len, 528);
774 break;
775 case write_gran_1024bytes:
776 result = need_erase_gran_bytes(have, want, len, 1024);
777 break;
778 case write_gran_1056bytes:
779 result = need_erase_gran_bytes(have, want, len, 1056);
780 break;
781 case write_gran_1byte_implicit_erase:
782 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
783 result = 0;
hailfingerb247c7a2010-03-08 00:42:32 +0000784 break;
hailfingerb437e282010-11-04 01:04:27 +0000785 default:
786 msg_cerr("%s: Unsupported granularity! Please report a bug at "
787 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000788 }
789 return result;
790}
791
hailfingerb437e282010-11-04 01:04:27 +0000792/**
793 * Check if the buffer @have needs to be programmed to get the content of @want.
794 * If yes, return 1 and fill in first_start with the start address of the
795 * write operation and first_len with the length of the first to-be-written
796 * chunk. If not, return 0 and leave first_start and first_len undefined.
797 *
798 * Warning: This function assumes that @have and @want point to naturally
799 * aligned regions.
800 *
801 * @have buffer with current content
802 * @want buffer with desired content
803 * @len length of the checked area
804 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000805 * @first_start offset of the first byte which needs to be written (passed in
806 * value is increased by the offset of the first needed write
807 * relative to have/want or unchanged if no write is needed)
808 * @return length of the first contiguous area which needs to be written
809 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000810 *
811 * FIXME: This function needs a parameter which tells it about coalescing
812 * in relation to the max write length of the programmer and the max write
813 * length of the chip.
814 */
stefanctc5eb8a92011-11-23 09:13:48 +0000815static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
816 unsigned int *first_start,
817 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000818{
stefanctc5eb8a92011-11-23 09:13:48 +0000819 int need_write = 0;
820 unsigned int rel_start = 0, first_len = 0;
821 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000822
hailfingerb437e282010-11-04 01:04:27 +0000823 switch (gran) {
824 case write_gran_1bit:
825 case write_gran_1byte:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100826 case write_gran_1byte_implicit_erase:
hailfinger90fcf9b2010-11-05 14:51:59 +0000827 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000828 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100829 case write_gran_128bytes:
830 stride = 128;
831 break;
hailfingerb437e282010-11-04 01:04:27 +0000832 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000833 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000834 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100835 case write_gran_264bytes:
836 stride = 264;
837 break;
838 case write_gran_512bytes:
839 stride = 512;
840 break;
841 case write_gran_528bytes:
842 stride = 528;
843 break;
844 case write_gran_1024bytes:
845 stride = 1024;
846 break;
847 case write_gran_1056bytes:
848 stride = 1056;
849 break;
hailfingerb437e282010-11-04 01:04:27 +0000850 default:
851 msg_cerr("%s: Unsupported granularity! Please report a bug at "
852 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000853 /* Claim that no write was needed. A write with unknown
854 * granularity is too dangerous to try.
855 */
856 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000857 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000858 for (i = 0; i < len / stride; i++) {
859 limit = min(stride, len - i * stride);
860 /* Are 'have' and 'want' identical? */
861 if (memcmp(have + i * stride, want + i * stride, limit)) {
862 if (!need_write) {
863 /* First location where have and want differ. */
864 need_write = 1;
865 rel_start = i * stride;
866 }
867 } else {
868 if (need_write) {
869 /* First location where have and want
870 * do not differ anymore.
871 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000872 break;
873 }
874 }
875 }
hailfingerffb7f382010-12-06 13:05:44 +0000876 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000877 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000878 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000879 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000880}
881
hailfinger0c515352009-11-23 12:55:31 +0000882/* This function generates various test patterns useful for testing controller
883 * and chip communication as well as chip behaviour.
884 *
885 * If a byte can be written multiple times, each time keeping 0-bits at 0
886 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
887 * is essentially an AND operation. That's also the reason why this function
888 * provides the result of AND between various patterns.
889 *
890 * Below is a list of patterns (and their block length).
891 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
892 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
893 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
894 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
895 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
896 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
897 * Pattern 6 is 00 (1 Byte)
898 * Pattern 7 is ff (1 Byte)
899 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
900 * byte block.
901 *
902 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
903 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
904 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
905 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
906 * Pattern 12 is 00 (1 Byte)
907 * Pattern 13 is ff (1 Byte)
908 * Patterns 8-13 have no block number.
909 *
910 * Patterns 0-3 are created to detect and efficiently diagnose communication
911 * slips like missed bits or bytes and their repetitive nature gives good visual
912 * cues to the person inspecting the results. In addition, the following holds:
913 * AND Pattern 0/1 == Pattern 4
914 * AND Pattern 2/3 == Pattern 5
915 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
916 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
917 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
918 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
919 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
920 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
921 * Besides that, they provide for bit testing of the last two bytes of every
922 * 256 byte block which contains the block number for patterns 0-6.
923 * Patterns 10-11 are special purpose for detecting subblock aliasing with
924 * block sizes >256 bytes (some Dataflash chips etc.)
925 * AND Pattern 8/9 == Pattern 12
926 * AND Pattern 10/11 == Pattern 12
927 * Pattern 13 is the completely erased state.
928 * None of the patterns can detect aliasing at boundaries which are a multiple
929 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
930 */
931int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
932{
933 int i;
934
935 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000936 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000937 return 1;
938 }
939
940 switch (variant) {
941 case 0:
942 for (i = 0; i < size; i++)
943 buf[i] = (i & 0xf) << 4 | 0x5;
944 break;
945 case 1:
946 for (i = 0; i < size; i++)
947 buf[i] = (i & 0xf) << 4 | 0xa;
948 break;
949 case 2:
950 for (i = 0; i < size; i++)
951 buf[i] = 0x50 | (i & 0xf);
952 break;
953 case 3:
954 for (i = 0; i < size; i++)
955 buf[i] = 0xa0 | (i & 0xf);
956 break;
957 case 4:
958 for (i = 0; i < size; i++)
959 buf[i] = (i & 0xf) << 4;
960 break;
961 case 5:
962 for (i = 0; i < size; i++)
963 buf[i] = i & 0xf;
964 break;
965 case 6:
966 memset(buf, 0x00, size);
967 break;
968 case 7:
969 memset(buf, 0xff, size);
970 break;
971 case 8:
972 for (i = 0; i < size; i++)
973 buf[i] = i & 0xff;
974 break;
975 case 9:
976 for (i = 0; i < size; i++)
977 buf[i] = ~(i & 0xff);
978 break;
979 case 10:
980 for (i = 0; i < size % 2; i++) {
981 buf[i * 2] = (i >> 8) & 0xff;
982 buf[i * 2 + 1] = i & 0xff;
983 }
984 if (size & 0x1)
985 buf[i * 2] = (i >> 8) & 0xff;
986 break;
987 case 11:
988 for (i = 0; i < size % 2; i++) {
989 buf[i * 2] = ~((i >> 8) & 0xff);
990 buf[i * 2 + 1] = ~(i & 0xff);
991 }
992 if (size & 0x1)
993 buf[i * 2] = ~((i >> 8) & 0xff);
994 break;
995 case 12:
996 memset(buf, 0x00, size);
997 break;
998 case 13:
999 memset(buf, 0xff, size);
1000 break;
1001 }
1002
1003 if ((variant >= 0) && (variant <= 7)) {
1004 /* Write block number in the last two bytes of each 256-byte
1005 * block, big endian for easier reading of the hexdump.
1006 * Note that this wraps around for chips larger than 2^24 bytes
1007 * (16 MB).
1008 */
1009 for (i = 0; i < size / 256; i++) {
1010 buf[i * 256 + 254] = (i >> 8) & 0xff;
1011 buf[i * 256 + 255] = i & 0xff;
1012 }
1013 }
1014
1015 return 0;
1016}
1017
hailfingeraec9c962009-10-31 01:53:09 +00001018int check_max_decode(enum chipbustype buses, uint32_t size)
1019{
1020 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001021
1022 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001023 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001024 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001025 "size %u kB of chipset/board/programmer "
1026 "for %s interface, "
1027 "probe/read/erase/write may fail. ", size / 1024,
1028 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001029 }
hailfingere1e41ea2011-07-27 07:13:06 +00001030 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001031 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001032 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001033 "size %u kB of chipset/board/programmer "
1034 "for %s interface, "
1035 "probe/read/erase/write may fail. ", size / 1024,
1036 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001037 }
hailfingere1e41ea2011-07-27 07:13:06 +00001038 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001039 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001040 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001041 "size %u kB of chipset/board/programmer "
1042 "for %s interface, "
1043 "probe/read/erase/write may fail. ", size / 1024,
1044 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001045 }
hailfingere1e41ea2011-07-27 07:13:06 +00001046 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001047 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001048 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001049 "size %u kB of chipset/board/programmer "
1050 "for %s interface, "
1051 "probe/read/erase/write may fail. ", size / 1024,
1052 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001053 }
1054 if (!limitexceeded)
1055 return 0;
1056 /* Sometimes chip and programmer have more than one bus in common,
1057 * and the limit is not exceeded on all buses. Tell the user.
1058 */
1059 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001060 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001061 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001062 "interface which can support a chip of this size. "
1063 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001064 return 1;
1065}
1066
David Hendricksac1d25c2016-08-09 17:00:58 -07001067int probe_flash(int startchip, struct flashctx *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001068{
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301069 const struct flashchip *flash, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001070 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001071 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001072 uint32_t size;
1073 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001074 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001075
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301076 /* Based on the host controller interface that a platform
1077 * needs to use (hwseq or swseq),
1078 * set the flashchips list here.
1079 */
1080 switch (ich_generation) {
1081 case CHIPSET_100_SERIES_SUNRISE_POINT:
Furquan Shaikh44088752016-07-11 22:48:08 -07001082 case CHIPSET_APL:
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301083 flash_list = flashchips_hwseq;
1084 break;
1085 default:
1086 flash_list = flashchips;
1087 break;
1088 }
1089
1090 for (flash = flash_list + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001091 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001092 continue;
David Hendricksac1d25c2016-08-09 17:00:58 -07001093 buses_common = buses_supported & flash->bustype;
1094 if (!buses_common) {
1095 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1096 flash->vendor, flash->name, flash->total_size);
1097 tmp = flashbuses_to_text(buses_supported);
1098 msg_gspew("Host bus type %s ", tmp);
1099 free(tmp);
1100 tmp = flashbuses_to_text(flash->bustype);
1101 msg_gspew("and chip bus type %s are incompatible.",
1102 tmp);
1103 free(tmp);
1104 msg_gspew("\n");
hailfinger18bd4cc2011-06-17 22:38:53 +00001105 continue;
David Hendricksac1d25c2016-08-09 17:00:58 -07001106 }
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001107 /* Only probe for SPI25 chips by default. */
1108 if (flash->bustype == BUS_SPI && !chip_to_probe && flash->spi_cmd_set != SPI25)
1109 continue;
hailfinger18bd4cc2011-06-17 22:38:53 +00001110 msg_gdbg("Probing for %s %s, %d kB: ",
1111 flash->vendor, flash->name, flash->total_size);
1112 if (!flash->probe && !force) {
1113 msg_gdbg("failed! flashrom has no probe function for "
1114 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001115 continue;
1116 }
stepan782fb172007-04-06 11:58:03 +00001117
ollie5672ac62004-03-17 22:22:08 +00001118 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001119 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001120
hailfinger48ed3e22011-05-04 00:39:50 +00001121 /* Start filling in the dynamic data. */
Patrick Georgif3fa2992017-02-02 16:24:44 +01001122 fill_flash->chip = calloc(1, sizeof(struct flashchip));
1123 if (!fill_flash->chip) {
1124 msg_gerr("Out of memory!\n");
1125 exit(1);
1126 }
1127 memcpy(fill_flash->chip, flash, sizeof(struct flashchip));
hailfinger48ed3e22011-05-04 00:39:50 +00001128
hailfinger72d3b982009-05-09 07:27:23 +00001129 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001130 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001131
stugec1e55fe2008-07-02 17:15:47 +00001132 if (force)
1133 break;
stepanc98b80b2006-03-16 16:57:41 +00001134
Patrick Georgif3fa2992017-02-02 16:24:44 +01001135 if (fill_flash->chip->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001136 goto notfound;
1137
hailfinger48ed3e22011-05-04 00:39:50 +00001138 /* If this is the first chip found, accept it.
1139 * If this is not the first chip found, accept it only if it is
1140 * a non-generic match.
1141 * We could either make chipcount global or provide it as
1142 * parameter, or we assume that startchip==0 means this call to
1143 * probe_flash() is the first one and thus no chip has been
1144 * found before.
1145 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01001146 if (startchip == 0 || fill_flash->chip->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001147 break;
1148
stuge56300c32008-09-03 23:10:05 +00001149notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001150 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
Patrick Georgif3fa2992017-02-02 16:24:44 +01001151 free(fill_flash->chip);
1152 fill_flash->chip = NULL;
rminnich8d3ff912003-10-25 17:01:29 +00001153 }
uwebe4477b2007-08-23 16:08:21 +00001154
stugec1e55fe2008-07-02 17:15:47 +00001155 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001156 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001157
hailfingere11396b2011-03-08 00:09:11 +00001158#if CONFIG_INTERNAL == 1
1159 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001160 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001161 else
1162#endif
stepan3e7aeae2011-01-19 06:21:54 +00001163 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001164
stefanct588b6d22011-06-26 20:45:35 +00001165 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001166 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
Patrick Georgif3fa2992017-02-02 16:24:44 +01001167 force ? "Assuming" : "Found", fill_flash->chip->vendor,
1168 fill_flash->chip->name, fill_flash->chip->total_size, tmp,
1169 location);
stefanct588b6d22011-06-26 20:45:35 +00001170 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001171
hailfinger0f4c3952010-12-02 21:59:42 +00001172 /* Flash registers will not be mapped if the chip was forced. Lock info
1173 * may be stored in registers, so avoid lock info printing.
1174 */
1175 if (!force)
Patrick Georgif3fa2992017-02-02 16:24:44 +01001176 if (fill_flash->chip->printlock)
1177 fill_flash->chip->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001178
hailfinger48ed3e22011-05-04 00:39:50 +00001179 /* Return position of matching chip. */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301180 return flash - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001181}
1182
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001183static int verify_flash(struct flashctx *flash,
1184 struct action_descriptor *descriptor,
1185 int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001186{
hailfingerb0f4d122009-06-24 08:20:45 +00001187 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +01001188 unsigned int total_size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001189 uint8_t *buf = descriptor->newcontents;
rminnich8d3ff912003-10-25 17:01:29 +00001190
snelsone42c3802010-05-07 20:09:04 +00001191 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001192
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001193 if (verify_it == VERIFY_PARTIAL) {
1194 struct processing_unit *pu = descriptor->processing_units;
1195
1196 /* Verify only areas which were written. */
1197 while (pu->num_blocks) {
1198 ret = verify_range(flash, buf + pu->offset, pu->offset,
1199 pu->block_size * pu->num_blocks,
1200 NULL);
1201 if (ret)
1202 break;
1203 pu++;
1204 }
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001205 } else {
1206 ret = verify_range(flash, buf, 0, total_size, NULL);
1207 }
uwef6641642007-05-09 10:17:44 +00001208
David Hendricks1ed1d352011-11-23 17:54:37 -08001209 if (ret == ACCESS_DENIED) {
1210 msg_gdbg("Could not fully verify due to access error, ");
1211 if (access_denied_action == error_ignore) {
1212 msg_gdbg("ignoring\n");
1213 ret = 0;
1214 } else {
1215 msg_gdbg("aborting\n");
1216 }
1217 }
1218
hailfingerb0f4d122009-06-24 08:20:45 +00001219 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001220 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001221
hailfingerb0f4d122009-06-24 08:20:45 +00001222 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001223}
1224
uwe8d342eb2011-07-28 08:13:25 +00001225int read_buf_from_file(unsigned char *buf, unsigned long size,
1226 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001227{
1228 unsigned long numbytes;
1229 FILE *image;
1230 struct stat image_stat;
1231
Vincent Palatin7ab23932014-10-01 12:09:16 -07001232 if (!strncmp(filename, "-", sizeof("-")))
1233 image = fdopen(STDIN_FILENO, "rb");
1234 else
1235 image = fopen(filename, "rb");
1236 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001237 perror(filename);
1238 return 1;
1239 }
1240 if (fstat(fileno(image), &image_stat) != 0) {
1241 perror(filename);
1242 fclose(image);
1243 return 1;
1244 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001245 if ((image_stat.st_size != size) &&
1246 (strncmp(filename, "-", sizeof("-")))) {
Mike Frysinger62c794d2017-05-29 12:02:45 -04001247 msg_gerr("Error: Image size doesn't match: stat %jd bytes, "
1248 "wanted %ld!\n", (intmax_t)image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001249 fclose(image);
1250 return 1;
1251 }
1252 numbytes = fread(buf, 1, size, image);
1253 if (fclose(image)) {
1254 perror(filename);
1255 return 1;
1256 }
1257 if (numbytes != size) {
1258 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1259 "wanted %ld!\n", numbytes, size);
1260 return 1;
1261 }
1262 return 0;
1263}
1264
uwe8d342eb2011-07-28 08:13:25 +00001265int write_buf_to_file(unsigned char *buf, unsigned long size,
1266 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001267{
1268 unsigned long numbytes;
1269 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001270
1271 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001272 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001273 return 1;
1274 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001275 if (!strncmp(filename, "-", sizeof("-")))
1276 image = fdopen(STDOUT_FILENO, "wb");
1277 else
1278 image = fopen(filename, "wb");
1279 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001280 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001281 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001282 }
hailfingerd219a232009-01-28 00:27:54 +00001283
hailfingerd219a232009-01-28 00:27:54 +00001284 numbytes = fwrite(buf, 1, size, image);
1285 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001286 if (numbytes != size) {
1287 msg_gerr("File %s could not be written completely.\n",
1288 filename);
hailfingerd219a232009-01-28 00:27:54 +00001289 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001290 }
hailfingerd219a232009-01-28 00:27:54 +00001291 return 0;
1292}
1293
David Hendrickse3451942013-03-21 17:23:29 -07001294/*
1295 * read_flash - wrapper for flash->read() with additional high-level policy
1296 *
1297 * @flash flash chip
1298 * @buf buffer to store data in
1299 * @start start address
1300 * @len number of bytes to read
1301 *
1302 * This wrapper simplifies most cases when the flash chip needs to be read
1303 * since policy decisions such as non-fatal error handling is centralized.
1304 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001305int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001306 unsigned int start, unsigned int len)
1307{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001308 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001309
Patrick Georgif3fa2992017-02-02 16:24:44 +01001310 if (!flash || !flash->chip->read)
David Hendrickse3451942013-03-21 17:23:29 -07001311 return -1;
1312
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001313 msg_cdbg("%#06x-%#06x:R ", start, start + len - 1);
1314
Patrick Georgif3fa2992017-02-02 16:24:44 +01001315 ret = flash->chip->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001316 if (ret) {
1317 if (ignore_error(ret)) {
1318 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1319 start, start + len - 1);
1320 ret = 0;
1321 } else {
1322 msg_gdbg("failed to read 0x%x-0x%x\n",
1323 start, start + len - 1);
1324 }
1325 }
1326
1327 return ret;
1328}
1329
David Hendricks7c8a1612013-04-26 19:14:44 -07001330/*
1331 * write_flash - wrapper for flash->write() with additional high-level policy
1332 *
1333 * @flash flash chip
1334 * @buf buffer to write to flash
1335 * @start start address in flash
1336 * @len number of bytes to write
1337 *
1338 * TODO: Look up regions that are write-protected and avoid attempt to write
1339 * to them at all.
1340 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001341int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001342 unsigned int start, unsigned int len)
1343{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001344 if (!flash || !flash->chip->write)
David Hendricks7c8a1612013-04-26 19:14:44 -07001345 return -1;
1346
Patrick Georgif3fa2992017-02-02 16:24:44 +01001347 return flash->chip->write(flash, buf, start, len);
David Hendricks7c8a1612013-04-26 19:14:44 -07001348}
1349
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001350int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001351{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001352 unsigned long size = flash->chip->total_size * 1024;
hailfinger42a850a2010-07-13 23:56:13 +00001353 unsigned char *buf = calloc(size, sizeof(char));
1354 int ret = 0;
1355
1356 msg_cinfo("Reading flash... ");
1357 if (!buf) {
1358 msg_gerr("Memory allocation failed!\n");
1359 msg_cinfo("FAILED.\n");
1360 return 1;
1361 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001362
1363 /* To support partial read, fill buffer to all 0xFF at beginning to make
1364 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001365 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001366
Patrick Georgif3fa2992017-02-02 16:24:44 +01001367 if (!flash->chip->read) {
hailfinger42a850a2010-07-13 23:56:13 +00001368 msg_cerr("No read function available for this flash chip.\n");
1369 ret = 1;
1370 goto out_free;
1371 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001372
1373 /* First try to handle partial read case, rather than read the whole
1374 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001375 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001376 if (ret < 0) {
1377 msg_cerr("Partial read operation failed!\n");
1378 ret = 1;
1379 goto out_free;
1380 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001381 int num_regions = get_num_include_args();
1382
1383 if (ret != num_regions) {
1384 msg_cerr("Requested %d regions, but only read %d\n",
1385 num_regions, ret);
1386 ret = 1;
1387 goto out_free;
1388 }
1389
1390 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001391 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001392 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001393 msg_cerr("Read operation failed!\n");
1394 ret = 1;
1395 goto out_free;
1396 }
hailfinger42a850a2010-07-13 23:56:13 +00001397 }
1398
David Hendricksdf29a832013-06-28 14:33:51 -07001399 if (filename)
1400 ret = write_buf_to_file(buf, size, filename);
1401
hailfinger42a850a2010-07-13 23:56:13 +00001402out_free:
1403 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001404 if (ret)
1405 msg_cerr("FAILED.");
1406 else
1407 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001408 return ret;
1409}
1410
hailfingerb437e282010-11-04 01:04:27 +00001411/* This function shares a lot of its structure with erase_and_write_flash() and
1412 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001413 * Even if an error is found, the function will keep going and check the rest.
1414 */
hailfinger48ed3e22011-05-04 00:39:50 +00001415static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001416{
hailfingerb91c08c2011-08-15 19:54:20 +00001417 int i, j, k;
1418 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001419
1420 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1421 unsigned int done = 0;
1422 struct block_eraser eraser = flash->block_erasers[k];
1423
1424 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1425 /* Blocks with zero size are bugs in flashchips.c. */
1426 if (eraser.eraseblocks[i].count &&
1427 !eraser.eraseblocks[i].size) {
1428 msg_gerr("ERROR: Flash chip %s erase function "
1429 "%i region %i has size 0. Please report"
1430 " a bug at flashrom@flashrom.org\n",
1431 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001432 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001433 }
1434 /* Blocks with zero count are bugs in flashchips.c. */
1435 if (!eraser.eraseblocks[i].count &&
1436 eraser.eraseblocks[i].size) {
1437 msg_gerr("ERROR: Flash chip %s erase function "
1438 "%i region %i has count 0. Please report"
1439 " a bug at flashrom@flashrom.org\n",
1440 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001441 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001442 }
1443 done += eraser.eraseblocks[i].count *
1444 eraser.eraseblocks[i].size;
1445 }
hailfinger9fed35d2010-01-19 06:42:46 +00001446 /* Empty eraseblock definition with erase function. */
1447 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001448 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001449 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001450 if (!done)
1451 continue;
1452 if (done != flash->total_size * 1024) {
1453 msg_gerr("ERROR: Flash chip %s erase function %i "
1454 "region walking resulted in 0x%06x bytes total,"
1455 " expected 0x%06x bytes. Please report a bug at"
1456 " flashrom@flashrom.org\n", flash->name, k,
1457 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001458 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001459 }
hailfinger9fed35d2010-01-19 06:42:46 +00001460 if (!eraser.block_erase)
1461 continue;
1462 /* Check if there are identical erase functions for different
1463 * layouts. That would imply "magic" erase functions. The
1464 * easiest way to check this is with function pointers.
1465 */
uwef6f94d42010-03-13 17:28:29 +00001466 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001467 if (eraser.block_erase ==
1468 flash->block_erasers[j].block_erase) {
1469 msg_gerr("ERROR: Flash chip %s erase function "
1470 "%i and %i are identical. Please report"
1471 " a bug at flashrom@flashrom.org\n",
1472 flash->name, k, j);
1473 ret = 1;
1474 }
uwef6f94d42010-03-13 17:28:29 +00001475 }
hailfinger45177872010-01-18 08:14:43 +00001476 }
hailfinger9fed35d2010-01-19 06:42:46 +00001477 return ret;
hailfinger45177872010-01-18 08:14:43 +00001478}
1479
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001480static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001481 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001482 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001483 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001484 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001485 unsigned int addr,
1486 unsigned int len))
1487{
stefanctc5eb8a92011-11-23 09:13:48 +00001488 unsigned int starthere = 0, lenhere = 0;
1489 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001490 int block_was_erased = 0;
hailfingerb437e282010-11-04 01:04:27 +00001491 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1492
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001493 /*
1494 * curcontents and newcontents are opaque to walk_eraseregions, and
1495 * need to be adjusted here to keep the impression of proper
1496 * abstraction
hailfingerb437e282010-11-04 01:04:27 +00001497 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001498
hailfinger90fcf9b2010-11-05 14:51:59 +00001499 curcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001500
hailfingerb437e282010-11-04 01:04:27 +00001501 newcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001502
hailfingerb437e282010-11-04 01:04:27 +00001503 msg_cdbg(":");
1504 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001505 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001506 content_has_changed |= 1;
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001507 msg_cdbg(" E");
hailfingerb437e282010-11-04 01:04:27 +00001508 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001509 if (ret) {
1510 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001511 msg_cdbg(" DENIED");
David Hendricks1ed1d352011-11-23 17:54:37 -08001512 else
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001513 msg_cerr(" ERASE_FAILED\n");
hailfingerb437e282010-11-04 01:04:27 +00001514 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001515 }
1516
David Hendricks0954ffc2015-11-13 15:15:44 -08001517 if (programmer_table[programmer].paranoid) {
1518 if (check_erased_range(flash, start, len)) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001519 msg_cerr(" ERASE_FAILED\n");
David Hendricks0954ffc2015-11-13 15:15:44 -08001520 return -1;
1521 }
hailfingerac8e3182011-06-26 17:04:16 +00001522 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001523
hailfinger90fcf9b2010-11-05 14:51:59 +00001524 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001525 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001526 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001527 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001528 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001529 /* get_next_write() sets starthere to a new value after the call. */
1530 while ((lenhere = get_next_write(curcontents + starthere,
1531 newcontents + starthere,
1532 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001533 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001534 if (!writecount++)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001535 msg_cdbg(" W");
hailfingerb437e282010-11-04 01:04:27 +00001536 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001537 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001538 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001539 if (ret) {
1540 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001541 msg_cdbg(" DENIED");
hailfingerb437e282010-11-04 01:04:27 +00001542 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001543 }
David Hendricks048b38c2016-03-28 18:47:06 -07001544
1545 /*
1546 * If the block needed to be erased and was erased successfully
1547 * then we can assume that we didn't run into any write-
1548 * protected areas. Otherwise, we need to verify each page to
1549 * ensure it was successfully written and abort if we encounter
1550 * any errors.
1551 */
1552 if (programmer_table[programmer].paranoid && !block_was_erased) {
1553 if (verify_range(flash, newcontents + starthere,
1554 start + starthere, lenhere, "WRITE"))
1555 return -1;
1556 }
1557
hailfingerb437e282010-11-04 01:04:27 +00001558 starthere += lenhere;
1559 skip = 0;
1560 }
1561 if (skip)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001562 msg_cdbg(" SKIP");
hailfingerb437e282010-11-04 01:04:27 +00001563 return ret;
1564}
1565
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001566/*
1567 * Function to process processing units accumulated in the action descriptor.
1568 *
1569 * @flash pointer to the flash context to operate on
1570 * @do_something helper function which can erase and program a section of the
1571 * flash chip. It receives the flash context, offset and length
1572 * of the area to erase/program, before and after contents (to
1573 * decide what exactly needs to be erased and or programmed)
1574 * and a pointer to the erase function which can operate on the
1575 * proper granularity.
1576 * @descriptor action descriptor including pointers to before and after
1577 * contents and an array of processing actions to take.
1578 *
1579 * Returns zero on success or an error code.
1580 */
1581static int walk_eraseregions(struct flashctx *flash,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001582 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001583 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001584 unsigned int len,
1585 uint8_t *param1,
1586 uint8_t *param2,
1587 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001588 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001589 unsigned int addr,
1590 unsigned int len)),
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001591 struct action_descriptor *descriptor)
hailfinger2b8c9382010-07-13 00:37:19 +00001592{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001593 struct processing_unit *pu;
1594 int rc = 0;
1595 static int print_comma;
uwe8d342eb2011-07-28 08:13:25 +00001596
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001597 for (pu = descriptor->processing_units; pu->num_blocks; pu++) {
1598 unsigned base = pu->offset;
1599 unsigned top = pu->offset + pu->block_size * pu->num_blocks;
David Hendricks605544b2015-08-15 16:32:58 -07001600
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001601 while (base < top) {
David Hendricks605544b2015-08-15 16:32:58 -07001602
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001603 if (print_comma)
hailfingerb437e282010-11-04 01:04:27 +00001604 msg_cdbg(", ");
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001605 else
1606 print_comma = 1;
1607
1608 msg_cdbg("0x%06x-0x%06zx", base, base + pu->block_size - 1);
1609
1610 rc = do_something(flash, base,
1611 pu->block_size,
1612 descriptor->oldcontents,
1613 descriptor->newcontents,
1614 flash->chip->block_erasers[pu->block_eraser_index].block_erase);
1615
David Hendricks1ed1d352011-11-23 17:54:37 -08001616 if (rc) {
1617 if (ignore_error(rc))
1618 rc = 0;
1619 else
1620 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001621 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001622 base += pu->block_size;
hailfinger2b8c9382010-07-13 00:37:19 +00001623 }
1624 }
hailfingerb437e282010-11-04 01:04:27 +00001625 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001626 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001627}
1628
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001629static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001630{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001631 struct block_eraser eraser = flash->chip->block_erasers[k];
hailfingercf848f12010-12-05 15:14:44 +00001632
1633 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1634 if (log)
1635 msg_cdbg("not defined. ");
1636 return 1;
1637 }
1638 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1639 if (log)
1640 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001641 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001642 return 1;
1643 }
1644 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1645 if (log)
1646 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001647 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001648 return 1;
1649 }
1650 return 0;
1651}
1652
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001653int erase_and_write_flash(struct flashctx *flash,
1654 struct action_descriptor *descriptor)
hailfingerd219a232009-01-28 00:27:54 +00001655{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001656 int ret = 1;
hailfingercf848f12010-12-05 15:14:44 +00001657
hailfingercf848f12010-12-05 15:14:44 +00001658 msg_cinfo("Erasing and writing flash chip... ");
hailfingerb437e282010-11-04 01:04:27 +00001659
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001660 ret = walk_eraseregions(flash, &erase_and_write_block_helper, descriptor);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001661
hailfinger7df21362009-09-05 02:30:58 +00001662 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001663 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001664 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001665 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001666 }
1667 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001668}
1669
hailfinger4c47e9d2010-10-19 22:06:20 +00001670void nonfatal_help_message(void)
1671{
1672 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1673 "This means we have to add special support for your board, "
1674 "programmer or flash chip.\n"
1675 "Please report this on IRC at irc.freenode.net (channel "
1676 "#flashrom) or\n"
1677 "mail flashrom@flashrom.org!\n"
1678 "-------------------------------------------------------------"
1679 "------------------\n"
1680 "You may now reboot or simply leave the machine running.\n");
1681}
1682
uweb34ec9f2009-10-01 18:40:02 +00001683void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001684{
snelsone42c3802010-05-07 20:09:04 +00001685 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001686 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001687 "mail flashrom@flashrom.org with FAILED: your board name in "
1688 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001689 "-------------------------------------------------------------"
1690 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001691 "DO NOT REBOOT OR POWEROFF!\n");
1692}
1693
uwe8d342eb2011-07-28 08:13:25 +00001694/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001695void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001696{
1697 enum programmer p;
1698 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001699 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001700 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001701 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001702 }
Simon Glass8dc82732013-07-16 10:13:51 -06001703 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001704}
1705
hailfingerf79d1712010-10-06 23:48:34 +00001706void list_programmers_linebreak(int startcol, int cols, int paren)
1707{
1708 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001709 int pnamelen;
1710 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001711 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001712 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001713
1714 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1715 pname = programmer_table[p].name;
1716 pnamelen = strlen(pname);
1717 if (remaining - pnamelen - 2 < 0) {
1718 if (firstline)
1719 firstline = 0;
1720 else
1721 printf("\n");
1722 for (i = 0; i < startcol; i++)
1723 printf(" ");
1724 remaining = cols - startcol;
1725 } else {
1726 printf(" ");
1727 remaining--;
1728 }
1729 if (paren && (p == 0)) {
1730 printf("(");
1731 remaining--;
1732 }
1733 printf("%s", pname);
1734 remaining -= pnamelen;
1735 if (p < PROGRAMMER_INVALID - 1) {
1736 printf(",");
1737 remaining--;
1738 } else {
1739 if (paren)
1740 printf(")");
1741 printf("\n");
1742 }
1743 }
1744}
1745
hailfinger3b471632010-03-27 16:36:40 +00001746void print_sysinfo(void)
1747{
David Hendricksc6c9f822010-11-03 15:07:01 -07001748 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001749#if HAVE_UTSNAME == 1
1750 struct utsname osinfo;
1751 uname(&osinfo);
1752
David Hendricksc6c9f822010-11-03 15:07:01 -07001753 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001754 osinfo.machine);
1755#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001756 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001757#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001758}
1759
1760void print_buildinfo(void)
1761{
1762 msg_gdbg("flashrom was built with");
hailfinger3b471632010-03-27 16:36:40 +00001763#if NEED_PCI == 1
1764#ifdef PCILIB_VERSION
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001765 msg_gdbg(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001766#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001767 msg_gdbg(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001768#endif
1769#endif
1770#ifdef __clang__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001771 msg_gdbg(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001772#ifdef __clang_version__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001773 msg_gdbg(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001774#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001775 msg_gdbg(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001776#endif
hailfinger3b471632010-03-27 16:36:40 +00001777#elif defined(__GNUC__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001778 msg_gdbg(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001779#ifdef __VERSION__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001780 msg_gdbg(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001781#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001782 msg_gdbg(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001783#endif
1784#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001785 msg_gdbg(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001786#endif
1787#if defined (__FLASHROM_LITTLE_ENDIAN__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001788 msg_gdbg(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001789#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001790 msg_gdbg(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001791#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001792 msg_gdbg("\n");
hailfinger3b471632010-03-27 16:36:40 +00001793}
1794
uwefdeca092008-01-21 15:24:22 +00001795void print_version(void)
1796{
David Hendricksc6c9f822010-11-03 15:07:01 -07001797 /* send to stderr for chromium os */
1798 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001799 print_sysinfo();
David Hendricks07268292016-09-14 16:05:58 -07001800 msg_gerr("\n");
uwefdeca092008-01-21 15:24:22 +00001801}
1802
hailfinger74819ad2010-05-15 15:04:37 +00001803void print_banner(void)
1804{
1805 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001806 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001807 msg_ginfo("\n");
1808}
1809
hailfingerc77acb52009-12-24 02:15:55 +00001810int selfcheck(void)
1811{
hailfinger45177872010-01-18 08:14:43 +00001812 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001813 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001814
1815 /* Safety check. Instead of aborting after the first error, check
1816 * if more errors exist.
1817 */
hailfingerc77acb52009-12-24 02:15:55 +00001818 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001819 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001820 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001821 }
stefanct8632c922011-07-26 14:33:46 +00001822 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001823 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001824 * For 'flashchips' we check the first element to be non-null. In the
1825 * other cases there exist use cases where the first element can be
1826 * null. */
Yunlian Jiang945c3392015-01-20 15:56:44 -08001827 if (flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001828 msg_gerr("Flashchips table miscompilation!\n");
1829 ret = 1;
1830 }
hailfinger45177872010-01-18 08:14:43 +00001831 for (flash = flashchips; flash && flash->name; flash++)
1832 if (selfcheck_eraseblocks(flash))
1833 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001834
hailfinger45177872010-01-18 08:14:43 +00001835 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001836}
1837
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001838void check_chip_supported(const struct flashctx *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001839{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001840 if (flash->chip->feature_bits & FEATURE_OTP) {
Stefan Tauner9816fc82016-08-12 15:47:49 -07001841 msg_cdbg("This chip may contain one-time programmable memory. "
1842 "flashrom cannot read\nand may never be able to write "
1843 "it, hence it may not be able to completely\n"
1844 "clone the contents of this chip (see man page for "
1845 "details).\n");
1846 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001847 if ((flash->chip->tested.probe != OK) ||
1848 (flash->chip->tested.read != OK) ||
1849 (flash->chip->tested.erase != OK) ||
1850 (flash->chip->tested.write != OK) ||
1851 (flash->chip->tested.uread != OK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001852 msg_cdbg("===\n");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001853 if ((flash->chip->tested.probe == BAD) ||
1854 (flash->chip->tested.read == BAD) ||
1855 (flash->chip->tested.erase == BAD) ||
1856 (flash->chip->tested.write == BAD) ||
1857 (flash->chip->tested.uread == BAD)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001858 msg_cdbg("This flash part has status NOT WORKING for operations:");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001859 if (flash->chip->tested.probe == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001860 msg_cdbg(" PROBE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001861 if (flash->chip->tested.read == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001862 msg_cdbg(" READ");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001863 if (flash->chip->tested.erase == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001864 msg_cdbg(" ERASE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001865 if (flash->chip->tested.write == BAD)
David Hendricksc801adb2010-12-09 16:58:56 -08001866 msg_cdbg(" WRITE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001867 if (flash->chip->tested.uread == BAD)
Duncan Laurie06ffd522015-10-26 12:56:08 -07001868 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001869 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001870 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001871 if ((flash->chip->tested.probe == NT) ||
1872 (flash->chip->tested.read == NT) ||
1873 (flash->chip->tested.erase == NT) ||
1874 (flash->chip->tested.write == NT) ||
1875 (flash->chip->tested.uread == NT)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001876 msg_cdbg("This flash part has status UNTESTED for operations:");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001877 if (flash->chip->tested.probe == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001878 msg_cdbg(" PROBE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001879 if (flash->chip->tested.read == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001880 msg_cdbg(" READ");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001881 if (flash->chip->tested.erase == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001882 msg_cdbg(" ERASE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001883 if (flash->chip->tested.write == NT)
David Hendricksc801adb2010-12-09 16:58:56 -08001884 msg_cdbg(" WRITE");
Patrick Georgiac3423f2017-02-03 20:58:06 +01001885 if (flash->chip->tested.uread == NT)
Duncan Laurie06ffd522015-10-26 12:56:08 -07001886 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001887 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001888 }
hailfinger92cd8e32010-01-07 03:24:05 +00001889 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001890 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001891 "in the latest development\n"
1892 "version of flashrom. If you are running the latest "
1893 "development version,\n"
1894 "please email a report to flashrom@flashrom.org if "
1895 "any of the above operations\n"
1896 "work correctly for you with this flash part. Please "
1897 "include the flashrom\n"
1898 "output with the additional -V option for all "
1899 "operations you tested (-V, -Vr,\n"
Paul Menzel425dded2016-08-08 16:42:48 -07001900 "-VE, -Vw), and mention which mainboard or "
hailfinger74819ad2010-05-15 15:04:37 +00001901 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001902 "Please mention your board in the subject line. "
1903 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001904 }
1905}
1906
hailfinger771fc182010-10-15 00:01:14 +00001907/* FIXME: This function signature needs to be improved once doit() has a better
1908 * function signature.
1909 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001910int 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 +00001911{
Patrick Georgiac3423f2017-02-03 20:58:06 +01001912 const struct flashchip *chip = flash->chip;
1913
hailfinger771fc182010-10-15 00:01:14 +00001914 if (!programmer_may_write && (write_it || erase_it)) {
1915 msg_perr("Write/erase is not working yet on your programmer in "
1916 "its current configuration.\n");
1917 /* --force is the wrong approach, but it's the best we can do
1918 * until the generic programmer parameter parser is merged.
1919 */
1920 if (!force)
1921 return 1;
1922 msg_cerr("Continuing anyway.\n");
1923 }
1924
1925 if (read_it || erase_it || write_it || verify_it) {
1926 /* Everything needs read. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001927 if (chip->tested.read == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001928 msg_cerr("Read is not working on this chip. ");
1929 if (!force)
1930 return 1;
1931 msg_cerr("Continuing anyway.\n");
1932 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001933 if (!chip->read) {
hailfinger771fc182010-10-15 00:01:14 +00001934 msg_cerr("flashrom has no read function for this "
1935 "flash chip.\n");
1936 return 1;
1937 }
1938 }
1939 if (erase_it || write_it) {
1940 /* Write needs erase. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001941 if (chip->tested.erase == NA) {
1942 msg_cerr("Erase is not possible on this chip.\n");
1943 return 1;
1944 }
1945 if (chip->tested.erase == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001946 msg_cerr("Erase is not working on this chip. ");
1947 if (!force)
1948 return 1;
1949 msg_cerr("Continuing anyway.\n");
1950 }
stefancte1c5acf2011-07-04 07:27:17 +00001951 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001952 msg_cerr("flashrom has no erase function for this "
1953 "flash chip.\n");
1954 return 1;
1955 }
hailfinger771fc182010-10-15 00:01:14 +00001956 }
1957 if (write_it) {
Patrick Georgiac3423f2017-02-03 20:58:06 +01001958 if (chip->tested.write == NA) {
1959 msg_cerr("Write is not possible on this chip.\n");
1960 return 1;
1961 }
1962 if (chip->tested.write == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001963 msg_cerr("Write is not working on this chip. ");
1964 if (!force)
1965 return 1;
1966 msg_cerr("Continuing anyway.\n");
1967 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001968 if (!chip->write) {
hailfinger771fc182010-10-15 00:01:14 +00001969 msg_cerr("flashrom has no write function for this "
1970 "flash chip.\n");
1971 return 1;
1972 }
1973 }
1974 return 0;
1975}
1976
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001977/*
1978 * Function to erase entire flash chip.
1979 *
1980 * @flashctx pointer to the flash context to use
1981 * @oldcontents pointer to the buffer including current chip contents, to
1982 * decide which areas do in fact need to be erased
1983 * @size the size of the flash chip, in bytes.
1984 *
1985 * Returns zero on success or an error code.
1986 */
1987static int erase_chip(struct flashctx *flash, void *oldcontents,
1988 void *newcontents, size_t size)
1989{
1990 /*
1991 * To make sure that the chip is fully erased, let's cheat and create
1992 * a descriptor where the new contents are all erased.
1993 */
1994 struct action_descriptor *fake_descriptor;
1995 int ret = 0;
1996
1997 fake_descriptor = prepare_action_descriptor(flash, oldcontents,
1998 newcontents, 1);
1999 /* FIXME: Do we really want the scary warning if erase failed? After
2000 * all, after erase the chip is either blank or partially blank or it
2001 * has the old contents. A blank chip won't boot, so if the user
2002 * wanted erase and reboots afterwards, the user knows very well that
2003 * booting won't work.
2004 */
2005 if (erase_and_write_flash(flash, fake_descriptor)) {
2006 emergency_help_message();
2007 ret = 1;
2008 }
2009
2010 free(fake_descriptor);
2011
2012 return ret;
2013}
2014
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002015static int read_dest_content(struct flashctx *flash, int verify_it,
2016 uint8_t *dest, unsigned long size)
2017{
2018 if (((verify_it == VERIFY_OFF) || (verify_it == VERIFY_PARTIAL))
2019 && get_num_include_args()) {
2020 /*
2021 * If no full verification is required and not
2022 * the entire chip is about to be programmed,
2023 * read only the areas which might change.
2024 */
2025 if (handle_partial_read(flash, dest, read_flash, 0) < 0)
2026 return 1;
2027 } else {
2028 if (read_flash(flash, dest, 0, size))
2029 return 1;
2030 }
2031 return 0;
2032}
2033
hailfingerc77acb52009-12-24 02:15:55 +00002034/* This function signature is horrible. We need to design a better interface,
2035 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00002036 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00002037 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002038int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09002039 int write_it, int erase_it, int verify_it, int extract_it,
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002040 const char *diff_file, int do_diff)
hailfingerc77acb52009-12-24 02:15:55 +00002041{
hailfinger4c47e9d2010-10-19 22:06:20 +00002042 uint8_t *oldcontents;
2043 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00002044 int ret = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +01002045 unsigned long size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002046 struct action_descriptor *descriptor = NULL;
hailfingerc77acb52009-12-24 02:15:55 +00002047
stefanct02116582011-05-18 01:30:56 +00002048 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00002049 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00002050 ret = 1;
2051 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00002052 }
2053
hailfinger771fc182010-10-15 00:01:14 +00002054 /* Given the existence of read locks, we want to unlock for read,
2055 * erase and write.
2056 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01002057 if (flash->chip->unlock)
2058 flash->chip->unlock(flash);
hailfinger771fc182010-10-15 00:01:14 +00002059
Ed Swierk28cf7992017-07-03 13:17:18 -07002060 /* Enable/disable 4-byte addressing mode if flash chip supports it */
2061 if ((flash->chip->feature_bits & FEATURE_4BA_SUPPORT) &&
2062 flash->chip->four_bytes_addr_funcs.set_4ba) {
2063 if (flash->chip->four_bytes_addr_funcs.set_4ba(flash)) {
2064 msg_cerr("Enabling/disabling 4-byte addressing mode failed!\n");
2065 return 1;
Boris Baykov6323c242016-06-11 18:29:03 +02002066 }
Boris Baykov1a2f5322016-06-11 18:29:00 +02002067 }
2068
Simon Glass9ad06c12013-07-03 22:08:17 +09002069 if (extract_it) {
2070 ret = extract_regions(flash);
2071 goto out_nofree;
2072 }
2073
David Hendricksd0ea9ed2011-03-04 17:31:57 -08002074 /* mark entries included using -i argument as "included" if they are
2075 found in the master rom_entries list */
2076 if (process_include_args() < 0) {
2077 ret = 1;
2078 goto out_nofree;
2079 }
2080
hailfinger771fc182010-10-15 00:01:14 +00002081 if (read_it) {
2082 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00002083 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00002084 }
hailfingerb437e282010-11-04 01:04:27 +00002085
stefanctd611e8f2011-07-12 22:35:21 +00002086 oldcontents = malloc(size);
2087 if (!oldcontents) {
2088 msg_gerr("Out of memory!\n");
2089 exit(1);
2090 }
Simon Glass4c214132013-07-16 10:09:28 -06002091 /* Assume worst case: All blocks are not erased. */
2092 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002093 newcontents = malloc(size);
2094 if (!newcontents) {
2095 msg_gerr("Out of memory!\n");
2096 exit(1);
2097 }
Simon Glass4c214132013-07-16 10:09:28 -06002098 /* Assume best case: All blocks are erased. */
2099 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002100 /* Side effect of the assumptions above: Default write action is erase
2101 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002102 * oldcontents being completely unerased means we have to erase
2103 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002104 */
2105
hailfingerd217d122010-10-08 18:52:29 +00002106 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002107 /*
2108 * Note: This must be done before any files specified by -i
2109 * arguments are processed merged into the newcontents since
2110 * -i files take priority. See http://crbug.com/263495.
2111 */
2112 if (filename) {
2113 if (read_buf_from_file(newcontents, size, filename)) {
2114 ret = 1;
2115 goto out;
2116 }
2117 } else {
2118 /* Content will be read from -i args, so they must
2119 * not overlap. */
2120 if (included_regions_overlap()) {
2121 msg_gerr("Error: Included regions must "
2122 "not overlap.\n");
2123 ret = 1;
2124 goto out;
2125 }
stepan1da96c02006-11-21 23:48:51 +00002126 }
2127
David Hendricksac82cac2012-06-19 10:29:37 -07002128#if 0
2129 /*
2130 * FIXME: show_id() causes failure if vendor:mainboard do not
2131 * match. This may happen if codenames are in flux.
2132 * See chrome-os-partner:10414.
2133 */
hailfinger90c7d542010-05-31 15:27:27 +00002134#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002135 if (programmer == PROGRAMMER_INTERNAL)
2136 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002137#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002138#endif
ollie5672ac62004-03-17 22:22:08 +00002139 }
2140
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002141 if (do_diff) {
2142 /*
2143 * Obtain a reference image so that we can check whether
2144 * regions need to be erased and to give better diagnostics in
2145 * case write fails. If --fast-verify is used then only the
2146 * regions which are included using -i will be read.
2147 */
2148 if (diff_file) {
2149 msg_cdbg("Reading old contents from file... ");
2150 if (read_buf_from_file(oldcontents, size, diff_file)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002151 ret = 1;
2152 msg_cdbg("FAILED.\n");
2153 goto out;
2154 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002155 } else {
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002156 msg_cdbg("Reading old contents from flash chip... ");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002157 ret = read_dest_content(flash, verify_it,
2158 oldcontents, size);
2159 if (ret) {
2160 msg_cdbg("FAILED.\n");
2161 goto out;
David Hendricks52ddff02013-07-23 15:05:14 -07002162 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002163 }
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002164 msg_cdbg("done.\n");
2165 } else if (!erase_it) {
2166 msg_pinfo("No diff performed, considering the chip erased.\n");
2167 memset(oldcontents, flash_erase_value(flash), size);
hailfinger4c47e9d2010-10-19 22:06:20 +00002168 }
David Hendricksac1d25c2016-08-09 17:00:58 -07002169
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002170
David Hendricksdf29a832013-06-28 14:33:51 -07002171 /*
2172 * Note: This must be done after reading the file specified for the
2173 * -w/-v argument, if any, so that files specified using -i end up
2174 * in the "newcontents" buffer before being written.
2175 * See http://crbug.com/263495.
2176 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002177 if (handle_romentries(flash, oldcontents, newcontents, erase_it)) {
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002178 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002179 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002180 goto out;
2181 }
uwef6641642007-05-09 10:17:44 +00002182
David Hendricksa7e114b2016-02-26 18:49:15 -08002183 if (erase_it) {
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002184 erase_chip(flash, oldcontents, newcontents, size);
2185 goto verify;
David Hendricksa7e114b2016-02-26 18:49:15 -08002186 }
2187
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002188 descriptor = prepare_action_descriptor(flash, oldcontents,
2189 newcontents, do_diff);
stuge8ce3a3c2008-04-28 14:47:30 +00002190 if (write_it) {
David Hendricksb64b39a2016-10-11 13:48:06 -07002191 // parse the new fmap and disable soft WP if necessary
David Hendricksac1d25c2016-08-09 17:00:58 -07002192 if ((ret = cros_ec_prepare(newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002193 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002194 goto out;
2195 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002196
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002197 if (erase_and_write_flash(flash, descriptor)) {
hailfingerb437e282010-11-04 01:04:27 +00002198 msg_cerr("Uh oh. Erase/write failed. Checking if "
2199 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002200 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002201 if (!memcmp(oldcontents, newcontents, size)) {
2202 msg_cinfo("Good. It seems nothing was "
2203 "changed.\n");
2204 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002205 ret = 1;
2206 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002207 }
2208 }
hailfingerd217d122010-10-08 18:52:29 +00002209 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002210 ret = 1;
2211 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002212 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002213
David Hendricksac1d25c2016-08-09 17:00:58 -07002214 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002215 if (ret < 0) {
2216 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002217 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002218 emergency_help_message();
2219 ret = 1;
2220 goto out;
2221 } else if (ret > 0) {
2222 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002223 msg_pdbg("CROS_EC needs 2nd pass.\n");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002224 ret = read_dest_content(flash, verify_it,
2225 oldcontents, size);
2226 if (ret) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002227 emergency_help_message();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002228 goto out;
2229 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002230
2231 /* Get a new descriptor. */
2232 free(descriptor);
2233 descriptor = prepare_action_descriptor(flash,
2234 oldcontents,
2235 newcontents,
2236 do_diff);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002237 // write 2nd pass
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002238 if (erase_and_write_flash(flash, descriptor)) {
David Hendricksb907de32014-08-11 16:47:09 -07002239 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002240 emergency_help_message();
2241 ret = 1;
2242 goto out;
2243 }
2244 ret = 0;
2245 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002246
David Hendricksac1d25c2016-08-09 17:00:58 -07002247 if (cros_ec_finish() < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002248 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002249 emergency_help_message();
2250 ret = 1;
2251 goto out;
2252 }
stuge8ce3a3c2008-04-28 14:47:30 +00002253 }
ollie6a600992005-11-26 21:55:36 +00002254
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002255 verify:
hailfinger0459e1c2009-08-19 13:55:34 +00002256 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002257 if ((write_it || erase_it) && !content_has_changed) {
2258 msg_gdbg("Nothing was erased or written, skipping "
2259 "verification\n");
2260 } else {
2261 /* Work around chips which need some time to calm down. */
2262 if (write_it && verify_it != VERIFY_PARTIAL)
2263 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002264
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002265 ret = verify_flash(flash, descriptor, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002266
David Hendricks9ba79fb2015-04-03 12:06:16 -07002267 /* If we tried to write, and verification now fails, we
2268 * might have an emergency situation.
2269 */
2270 if (ret && write_it)
2271 emergency_help_message();
2272 }
hailfinger0459e1c2009-08-19 13:55:34 +00002273 }
ollie6a600992005-11-26 21:55:36 +00002274
hailfinger90fcf9b2010-11-05 14:51:59 +00002275out:
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002276 if (descriptor)
2277 free(descriptor);
2278
hailfinger90fcf9b2010-11-05 14:51:59 +00002279 free(oldcontents);
2280 free(newcontents);
2281out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002282 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002283 /*
2284 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2285 * tree. This is because some operations, such as write protection,
2286 * requires programmer_shutdown() but does not call doit().
2287 */
2288// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002289 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002290}