blob: 2ab9d63099ee430b4d66f959e5c6c7d8d2cd9116 [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
uwe4475e902009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
hailfinger23060112009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
rminnich8d3ff912003-10-25 17:01:29 +000019 */
20
hailfingera83a5fe2010-05-30 22:24:40 +000021#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000022#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000023#ifndef __LIBPAYLOAD__
24#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000026#endif
rminnich8d3ff912003-10-25 17:01:29 +000027#include <string.h>
28#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000029#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000030#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000031#if HAVE_UTSNAME == 1
32#include <sys/utsname.h>
33#endif
Vincent Palatin7ab23932014-10-01 12:09:16 -070034#include <unistd.h>
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -070035
36#include "action_descriptor.h"
rminnich8d3ff912003-10-25 17:01:29 +000037#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000038#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090039#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000040#include "programmer.h"
Duncan Laurie25a4ca22019-04-25 12:08:52 -070041#include "spi.h"
rminnich8d3ff912003-10-25 17:01:29 +000042
krause2eb76212011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000044char *chip_to_probe = NULL;
hailfinger80422e22009-12-13 22:28:00 +000045
David Hendricks9ba79fb2015-04-03 12:06:16 -070046/* Set if any erase/write operation is to be done. This will be used to
47 * decide if final verification is needed. */
48static int content_has_changed = 0;
49
David Hendricks1ed1d352011-11-23 17:54:37 -080050/* error handling stuff */
51enum error_action access_denied_action = error_ignore;
52
53int ignore_error(int err) {
54 int rc = 0;
55
56 switch(err) {
57 case ACCESS_DENIED:
58 if (access_denied_action == error_ignore)
59 rc = 1;
60 break;
61 default:
62 break;
63 }
64
65 return rc;
66}
67
hailfinger969e2f32011-09-08 00:00:29 +000068static enum programmer programmer = PROGRAMMER_INVALID;
hailfingerddeb4ac2010-07-08 10:13:37 +000069static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000070
David Hendricksac1d25c2016-08-09 17:00:58 -070071/* Supported buses for the current programmer. */
72enum chipbustype buses_supported;
73
uwee15beb92010-08-08 17:01:18 +000074/*
hailfinger80422e22009-12-13 22:28:00 +000075 * Programmers supporting multiple buses can have differing size limits on
76 * each bus. Store the limits for each bus in a common struct.
77 */
hailfinger1ff33dc2010-07-03 11:02:10 +000078struct decode_sizes max_rom_decode;
79
80/* If nonzero, used as the start address of bottom-aligned flash. */
81unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000082
hailfinger5828baf2010-07-03 12:14:25 +000083/* Is writing allowed with this programmer? */
84int programmer_may_write;
85
hailfingerabe249e2009-05-08 17:43:22 +000086const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000087#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000088 {
hailfinger3548a9a2009-08-12 14:34:35 +000089 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000090 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000091 .map_flash_region = physmap,
92 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000093 .delay = internal_delay,
David Hendricks55cdd9c2015-11-25 14:37:26 -080094
95 /*
96 * "Internal" implies in-system programming on a live system, so
97 * handle with paranoia to catch errors early. If something goes
98 * wrong then hopefully the system will still be recoverable.
99 */
100 .paranoid = 1,
hailfingerabe249e2009-05-08 17:43:22 +0000101 },
hailfinger80422e22009-12-13 22:28:00 +0000102#endif
stepan927d4e22007-04-04 22:45:58 +0000103
hailfinger90c7d542010-05-31 15:27:27 +0000104#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000105 {
hailfinger3548a9a2009-08-12 14:34:35 +0000106 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000107 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000108 .map_flash_region = dummy_map,
109 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000110 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000111 },
hailfinger571a6b32009-09-16 10:09:21 +0000112#endif
hailfingera9df33c2009-05-09 00:54:55 +0000113
hailfinger90c7d542010-05-31 15:27:27 +0000114#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000115 {
hailfinger3548a9a2009-08-12 14:34:35 +0000116 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000117 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000118 .map_flash_region = fallback_map,
119 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000120 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000121 },
hailfinger571a6b32009-09-16 10:09:21 +0000122#endif
uwe0f5a3a22009-05-13 11:36:06 +0000123
hailfinger90c7d542010-05-31 15:27:27 +0000124#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000125 {
hailfinger0d703d42011-03-07 01:08:09 +0000126 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000127 .name = "nicrealtek",
128 //.name = "nicsmc1211",
129 .init = nicrealtek_init,
130 .map_flash_region = fallback_map,
131 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000132 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000133 },
hailfinger5aa36982010-05-21 21:54:07 +0000134#endif
135
hailfingerf0a368f2010-06-07 22:37:54 +0000136#if CONFIG_NICNATSEMI == 1
137 {
uwe8d342eb2011-07-28 08:13:25 +0000138 .name = "nicnatsemi",
139 .init = nicnatsemi_init,
140 .map_flash_region = fallback_map,
141 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000142 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000143 },
144#endif
hailfinger5aa36982010-05-21 21:54:07 +0000145
hailfinger90c7d542010-05-31 15:27:27 +0000146#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000147 {
148 .name = "gfxnvidia",
149 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000150 .map_flash_region = fallback_map,
151 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000152 .delay = internal_delay,
153 },
154#endif
155
hailfinger90c7d542010-05-31 15:27:27 +0000156#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000157 {
uwee2f95ef2009-09-02 23:00:46 +0000158 .name = "drkaiser",
159 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000160 .map_flash_region = fallback_map,
161 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000162 .delay = internal_delay,
163 },
hailfinger571a6b32009-09-16 10:09:21 +0000164#endif
uwee2f95ef2009-09-02 23:00:46 +0000165
hailfinger90c7d542010-05-31 15:27:27 +0000166#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000167 {
hailfinger3548a9a2009-08-12 14:34:35 +0000168 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000169 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000170 .map_flash_region = fallback_map,
171 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000172 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000173 },
hailfinger571a6b32009-09-16 10:09:21 +0000174#endif
ruikda922a12009-05-17 19:39:27 +0000175
hailfinger90c7d542010-05-31 15:27:27 +0000176#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000177 {
178 .name = "atahpt",
179 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000180 .map_flash_region = fallback_map,
181 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000182 .delay = internal_delay,
183 },
184#endif
185
hailfinger90c7d542010-05-31 15:27:27 +0000186#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000187 {
hailfinger90c7d542010-05-31 15:27:27 +0000188 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000189 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000190 .map_flash_region = fallback_map,
191 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000192 .delay = internal_delay,
193 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000194#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000195
hailfinger90c7d542010-05-31 15:27:27 +0000196#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000197 {
hailfinger3548a9a2009-08-12 14:34:35 +0000198 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000199 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000200 .map_flash_region = fallback_map,
201 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000202 .delay = serprog_delay,
203 },
hailfinger74d88a72009-08-12 16:17:41 +0000204#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000205
hailfinger90c7d542010-05-31 15:27:27 +0000206#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000207 {
hailfinger90c7d542010-05-31 15:27:27 +0000208 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000209 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000210 .map_flash_region = fallback_map,
211 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000212 .delay = internal_delay,
213 },
214#endif
215
Anton Staafb2647882014-09-17 15:13:43 -0700216#if CONFIG_RAIDEN_DEBUG_SPI == 1
217 {
218 .name = "raiden_debug_spi",
219 .init = raiden_debug_spi_init,
220 .map_flash_region = fallback_map,
221 .unmap_flash_region = fallback_unmap,
222 .delay = internal_delay,
223 },
224#endif
225
hailfinger90c7d542010-05-31 15:27:27 +0000226#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000227 {
228 .name = "dediprog",
229 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000230 .map_flash_region = fallback_map,
231 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000232 .delay = internal_delay,
233 },
234#endif
235
hailfinger52c4fa02010-07-21 10:26:01 +0000236#if CONFIG_RAYER_SPI == 1
237 {
238 .name = "rayer_spi",
239 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000240 .map_flash_region = fallback_map,
241 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000242 .delay = internal_delay,
243 },
244#endif
245
hailfinger7949b652011-05-08 00:24:18 +0000246#if CONFIG_NICINTEL == 1
247 {
248 .name = "nicintel",
249 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000250 .map_flash_region = fallback_map,
251 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000252 .delay = internal_delay,
253 },
254#endif
255
uwe6764e922010-09-03 18:21:21 +0000256#if CONFIG_NICINTEL_SPI == 1
257 {
uwe8d342eb2011-07-28 08:13:25 +0000258 .name = "nicintel_spi",
259 .init = nicintel_spi_init,
260 .map_flash_region = fallback_map,
261 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000262 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000263 },
264#endif
265
hailfingerfb1f31f2010-12-03 14:48:11 +0000266#if CONFIG_OGP_SPI == 1
267 {
uwe8d342eb2011-07-28 08:13:25 +0000268 .name = "ogp_spi",
269 .init = ogp_spi_init,
270 .map_flash_region = fallback_map,
271 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000272 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000273 },
274#endif
275
hailfinger935365d2011-02-04 21:37:59 +0000276#if CONFIG_SATAMV == 1
277 {
278 .name = "satamv",
279 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000280 .map_flash_region = fallback_map,
281 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000282 .delay = internal_delay,
283 },
284#endif
285
David Hendrickscebee892015-05-23 20:30:30 -0700286#if CONFIG_LINUX_MTD == 1
287 {
288 .name = "linux_mtd",
289 .init = linux_mtd_init,
290 .map_flash_region = fallback_map,
291 .unmap_flash_region = fallback_unmap,
292 .delay = internal_delay,
293 },
294#endif
295
uwe7df6dda2011-09-03 18:37:52 +0000296#if CONFIG_LINUX_SPI == 1
297 {
298 .name = "linux_spi",
299 .init = linux_spi_init,
300 .map_flash_region = fallback_map,
301 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000302 .delay = internal_delay,
303 },
304#endif
305
Patrick Georgi8ddfee92017-03-20 14:54:28 +0100306 {0}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000307};
stepan927d4e22007-04-04 22:45:58 +0000308
David Hendricksbf36f092010-11-02 23:39:29 -0700309#define CHIP_RESTORE_MAXFN 4
310static int chip_restore_fn_count = 0;
311struct chip_restore_func_data {
312 CHIP_RESTORE_CALLBACK;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700313 struct flashctx *flash;
David Hendricksbf36f092010-11-02 23:39:29 -0700314 uint8_t status;
315} static chip_restore_fn[CHIP_RESTORE_MAXFN];
316
David Hendricks668f29d2011-01-27 18:51:45 -0800317
hailfingerf31cbdc2010-11-10 15:25:18 +0000318#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000319static int shutdown_fn_count = 0;
320struct shutdown_func_data {
David Hendricks93784b42016-08-09 17:00:38 -0700321 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000322 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000323} static shutdown_fn[SHUTDOWN_MAXFN];
324/* Initialize to 0 to make sure nobody registers a shutdown function before
325 * programmer init.
326 */
327static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000328
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700329static int check_block_eraser(const struct flashctx *flash, int k, int log);
stefanct569dbb62011-07-01 00:19:12 +0000330
hailfingerdc6f7972010-02-14 01:20:28 +0000331/* Register a function to be executed on programmer shutdown.
332 * The advantage over atexit() is that you can supply a void pointer which will
333 * be used as parameter to the registered function upon programmer shutdown.
334 * This pointer can point to arbitrary data used by said function, e.g. undo
335 * information for GPIO settings etc. If unneeded, set data=NULL.
336 * Please note that the first (void *data) belongs to the function signature of
337 * the function passed as first parameter.
338 */
David Hendricks93784b42016-08-09 17:00:38 -0700339int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000340{
341 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000342 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000343 SHUTDOWN_MAXFN);
344 return 1;
345 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000346 if (!may_register_shutdown) {
347 msg_perr("Tried to register a shutdown function before "
348 "programmer init.\n");
349 return 1;
350 }
hailfingerdc6f7972010-02-14 01:20:28 +0000351 shutdown_fn[shutdown_fn_count].func = function;
352 shutdown_fn[shutdown_fn_count].data = data;
353 shutdown_fn_count++;
354
355 return 0;
356}
357
David Hendricksbf36f092010-11-02 23:39:29 -0700358//int register_chip_restore(int (*function) (void *data), void *data)
359int register_chip_restore(CHIP_RESTORE_CALLBACK,
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700360 struct flashctx *flash, uint8_t status)
David Hendricksbf36f092010-11-02 23:39:29 -0700361{
362 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
363 msg_perr("Tried to register more than %i chip restore"
364 " functions.\n", CHIP_RESTORE_MAXFN);
365 return 1;
366 }
367 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
368 chip_restore_fn[chip_restore_fn_count].flash = flash;
369 chip_restore_fn[chip_restore_fn_count].status = status;
370 chip_restore_fn_count++;
371
372 return 0;
373}
374
David Hendricksac1d25c2016-08-09 17:00:58 -0700375int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000376{
hailfinger1ef766d2010-07-06 09:55:48 +0000377 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000378
379 if (prog >= PROGRAMMER_INVALID) {
380 msg_perr("Invalid programmer specified!\n");
381 return -1;
382 }
383 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000384 /* Initialize all programmer specific data. */
385 /* Default to unlimited decode sizes. */
386 max_rom_decode = (const struct decode_sizes) {
387 .parallel = 0xffffffff,
388 .lpc = 0xffffffff,
389 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000390 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000391 };
David Hendricksac1d25c2016-08-09 17:00:58 -0700392 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000393 /* Default to top aligned flash at 4 GB. */
394 flashbase = 0;
395 /* Registering shutdown functions is now allowed. */
396 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000397 /* Default to allowing writes. Broken programmers set this to 0. */
398 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000399
400 programmer_param = param;
401 msg_pdbg("Initializing %s programmer\n",
402 programmer_table[programmer].name);
David Hendricksac1d25c2016-08-09 17:00:58 -0700403 ret = programmer_table[programmer].init();
hailfinger1ef766d2010-07-06 09:55:48 +0000404 return ret;
uweabe92a52009-05-16 22:36:00 +0000405}
406
David Hendricksbf36f092010-11-02 23:39:29 -0700407int chip_restore()
408{
409 int rc = 0;
410
411 while (chip_restore_fn_count > 0) {
412 int i = --chip_restore_fn_count;
413 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
414 chip_restore_fn[i].status);
415 }
416
417 return rc;
418}
419
David Hendricks93784b42016-08-09 17:00:38 -0700420int programmer_shutdown(void)
uweabe92a52009-05-16 22:36:00 +0000421{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000422 int ret = 0;
423
hailfinger1ff33dc2010-07-03 11:02:10 +0000424 /* Registering shutdown functions is no longer allowed. */
425 may_register_shutdown = 0;
426 while (shutdown_fn_count > 0) {
427 int i = --shutdown_fn_count;
David Hendricks93784b42016-08-09 17:00:38 -0700428 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000429 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000430 return ret;
uweabe92a52009-05-16 22:36:00 +0000431}
432
433void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
434 size_t len)
435{
436 return programmer_table[programmer].map_flash_region(descr,
437 phys_addr, len);
438}
439
440void programmer_unmap_flash_region(void *virt_addr, size_t len)
441{
442 programmer_table[programmer].unmap_flash_region(virt_addr, len);
443}
444
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700445void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000446{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100447 par_master->chip_writeb(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000448}
449
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700450void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000451{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100452 par_master->chip_writew(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000453}
454
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700455void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000456{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100457 par_master->chip_writel(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000458}
459
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700460void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000461{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100462 par_master->chip_writen(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000463}
464
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700465uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000466{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100467 return par_master->chip_readb(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000468}
469
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700470uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000471{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100472 return par_master->chip_readw(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000473}
474
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700475uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000476{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100477 return par_master->chip_readl(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000478}
479
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700480void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000481{
Patrick Georgi0a9533a2017-02-03 19:28:38 +0100482 par_master->chip_readn(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000483}
484
hailfingere5829f62009-06-05 17:48:08 +0000485void programmer_delay(int usecs)
486{
487 programmer_table[programmer].delay(usecs);
488}
489
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700490void map_flash_registers(struct flashctx *flash)
stepan15e64bc2007-05-24 08:48:10 +0000491{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100492 size_t size = flash->chip->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000493 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000494 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000495 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000496}
497
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700498int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000499{
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700500 chip_readn(flash, buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000501
hailfinger23060112009-05-08 12:49:03 +0000502 return 0;
503}
504
David Hendricksda18f692016-10-21 17:49:49 -0700505/* This is a somewhat hacked function similar in some ways to strtok(). It will
506 * look for needle with a subsequent '=' in haystack, return a copy of needle.
hailfinger6e5a52a2009-11-24 18:27:10 +0000507 */
stefanct52700282011-06-26 17:38:17 +0000508char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000509{
David Hendricksda18f692016-10-21 17:49:49 -0700510 char *param_pos, *opt_pos;
hailfinger1ef766d2010-07-06 09:55:48 +0000511 char *opt = NULL;
512 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000513 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000514
hailfingerf4aaccc2010-04-28 15:22:14 +0000515 needlelen = strlen(needle);
516 if (!needlelen) {
517 msg_gerr("%s: empty needle! Please report a bug at "
518 "flashrom@flashrom.org\n", __func__);
519 return NULL;
520 }
521 /* No programmer parameters given. */
522 if (*haystack == NULL)
523 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000524 param_pos = strstr(*haystack, needle);
525 do {
526 if (!param_pos)
527 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000528 /* Needle followed by '='? */
529 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600530
hailfinger1ef766d2010-07-06 09:55:48 +0000531 /* Beginning of the string? */
532 if (param_pos == *haystack)
533 break;
534 /* After a delimiter? */
535 if (strchr(delim, *(param_pos - 1)))
536 break;
537 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000538 /* Continue searching. */
539 param_pos++;
540 param_pos = strstr(param_pos, needle);
541 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000542
hailfinger6e5a52a2009-11-24 18:27:10 +0000543 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000544 /* Get the string after needle and '='. */
545 opt_pos = param_pos + needlelen + 1;
546 optlen = strcspn(opt_pos, delim);
547 /* Return an empty string if the parameter was empty. */
548 opt = malloc(optlen + 1);
549 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000550 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000551 exit(1);
552 }
hailfinger1ef766d2010-07-06 09:55:48 +0000553 strncpy(opt, opt_pos, optlen);
554 opt[optlen] = '\0';
hailfinger6e5a52a2009-11-24 18:27:10 +0000555 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000556
hailfinger1ef766d2010-07-06 09:55:48 +0000557 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000558}
559
stefanct52700282011-06-26 17:38:17 +0000560char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000561{
562 return extract_param(&programmer_param, param_name, ",");
563}
564
stefancte1c5acf2011-07-04 07:27:17 +0000565/* Returns the number of well-defined erasers for a chip. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700566static unsigned int count_usable_erasers(const struct flashctx *flash)
stefanct569dbb62011-07-01 00:19:12 +0000567{
568 unsigned int usable_erasefunctions = 0;
569 int k;
570 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
571 if (!check_block_eraser(flash, k, 0))
572 usable_erasefunctions++;
573 }
574 return usable_erasefunctions;
575}
576
hailfinger7af83692009-06-15 17:23:36 +0000577/* start is an offset to the base address of the flash chip */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700578int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000579{
580 int ret;
581 uint8_t *cmpbuf = malloc(len);
582
583 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000584 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000585 exit(1);
586 }
Simon Glass4c214132013-07-16 10:09:28 -0600587 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000588 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
589 free(cmpbuf);
590 return ret;
591}
592
Simon Glass4e305f42015-01-08 06:29:04 -0700593static int compare_chunk(uint8_t *readbuf, uint8_t *cmpbuf, unsigned int start,
594 unsigned int len, const char *message)
595{
596 int failcount = 0, i;
597
598 for (i = 0; i < len; i++) {
599 if (cmpbuf[i] != readbuf[i]) {
600 if (!failcount) {
601 msg_cerr("%s FAILED at 0x%08x! "
602 "Expected=0x%02x, Read=0x%02x,",
603 message, start + i,
604 cmpbuf[i], readbuf[i]);
605 }
606 failcount++;
607 }
608 }
609
610 return failcount;
611}
612
uwee15beb92010-08-08 17:01:18 +0000613/*
hailfinger7af3d192009-11-25 17:05:52 +0000614 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000615 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000616 * @start offset to the base address of the flash chip
617 * @len length of the verified area
618 * @message string to print in the "FAILED" message
619 * @return 0 for success, -1 for failure
620 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700621int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000622 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000623{
hailfinger8cb6ece2010-11-16 17:21:58 +0000624 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000625 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000626
627 if (!len)
628 goto out_free;
629
Patrick Georgif3fa2992017-02-02 16:24:44 +0100630 if (!flash->chip->read) {
snelsone42c3802010-05-07 20:09:04 +0000631 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000632 return 1;
633 }
hailfinger7af83692009-06-15 17:23:36 +0000634 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000635 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000636 exit(1);
637 }
638
Patrick Georgif3fa2992017-02-02 16:24:44 +0100639 if (start + len > flash->chip->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000640 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000641 " total_size 0x%x\n", __func__, start, len,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100642 flash->chip->total_size * 1024);
hailfinger7af83692009-06-15 17:23:36 +0000643 ret = -1;
644 goto out_free;
645 }
646 if (!message)
647 message = "VERIFY";
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -0700648 msg_gdbg("%#06x..%#06x ", start, start + len -1);
Simon Glass4e305f42015-01-08 06:29:04 -0700649 if (programmer_table[programmer].paranoid) {
650 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800651
Simon Glass4e305f42015-01-08 06:29:04 -0700652 /* limit chunksize in order to catch errors early */
653 for (i = 0, chunksize = 0; i < len; i += chunksize) {
654 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800655
Patrick Georgif3fa2992017-02-02 16:24:44 +0100656 chunksize = min(flash->chip->page_size, len - i);
657 tmp = flash->chip->read(flash, readbuf + i, start + i, chunksize);
Simon Glass4e305f42015-01-08 06:29:04 -0700658 if (tmp) {
659 ret = tmp;
660 if (ignore_error(tmp))
661 continue;
662 else
663 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800664 }
Simon Glass4e305f42015-01-08 06:29:04 -0700665
Duncan Laurie25a4ca22019-04-25 12:08:52 -0700666 /*
667 * Check write access permission and do not compare chunks
668 * where flashrom does not have write access to the region.
669 */
670 if (flash->chip->check_access) {
671 tmp = flash->chip->check_access(flash, start + i, chunksize, 0);
672 if (tmp && ignore_error(tmp))
673 continue;
674 }
675
Simon Glass4e305f42015-01-08 06:29:04 -0700676 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
677 chunksize, message);
678 if (failcount)
679 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800680 }
Simon Glass4e305f42015-01-08 06:29:04 -0700681 } else {
682 int tmp;
683
684 /* read as much as we can to reduce transaction overhead */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100685 tmp = flash->chip->read(flash, readbuf, start, len);
Simon Glass4e305f42015-01-08 06:29:04 -0700686 if (tmp && !ignore_error(tmp)) {
687 ret = tmp;
688 goto out_free;
689 }
690
691 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000692 }
693
hailfinger5be6c0f2009-07-23 01:42:56 +0000694 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000695 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000696 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000697 ret = -1;
698 }
hailfinger7af83692009-06-15 17:23:36 +0000699
700out_free:
701 free(readbuf);
702 return ret;
703}
704
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100705/* Helper function for need_erase() that focuses on granularities of gran bytes. */
706static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
707 unsigned int gran)
708{
709 unsigned int i, j, limit;
710 for (j = 0; j < len / gran; j++) {
711 limit = min (gran, len - j * gran);
712 /* Are 'have' and 'want' identical? */
713 if (!memcmp(have + j * gran, want + j * gran, limit))
714 continue;
715 /* have needs to be in erased state. */
716 for (i = 0; i < limit; i++)
717 if (have[j * gran + i] != 0xff)
718 return 1;
719 }
720 return 0;
721}
722
uwee15beb92010-08-08 17:01:18 +0000723/*
hailfingerb247c7a2010-03-08 00:42:32 +0000724 * Check if the buffer @have can be programmed to the content of @want without
725 * erasing. This is only possible if all chunks of size @gran are either kept
726 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000727 *
hailfingerb247c7a2010-03-08 00:42:32 +0000728 * The following write granularities (enum @gran) are known:
729 * - 1 bit. Each bit can be cleared individually.
730 * - 1 byte. A byte can be written once. Further writes to an already written
731 * byte cause the contents to be either undefined or to stay unchanged.
732 * - 128 bytes. If less than 128 bytes are written, the rest will be
733 * erased. Each write to a 128-byte region will trigger an automatic erase
734 * before anything is written. Very uncommon behaviour and unsupported by
735 * this function.
736 * - 256 bytes. If less than 256 bytes are written, the contents of the
737 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000738 * Warning: This function assumes that @have and @want point to naturally
739 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000740 *
741 * @have buffer with current content
742 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000743 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000744 * @gran write granularity (enum, not count)
745 * @return 0 if no erase is needed, 1 otherwise
746 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700747static int need_erase(struct flashctx *flash, uint8_t *have, uint8_t *want,
Simon Glass4c214132013-07-16 10:09:28 -0600748 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000749{
hailfingerb91c08c2011-08-15 19:54:20 +0000750 int result = 0;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100751 unsigned int i;
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700752
hailfingerb247c7a2010-03-08 00:42:32 +0000753 switch (gran) {
754 case write_gran_1bit:
755 for (i = 0; i < len; i++)
756 if ((have[i] & want[i]) != want[i]) {
757 result = 1;
758 break;
759 }
760 break;
761 case write_gran_1byte:
762 for (i = 0; i < len; i++)
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100763 if ((have[i] != want[i]) && (have[i] != 0xff)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000764 result = 1;
765 break;
766 }
767 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100768 case write_gran_128bytes:
769 result = need_erase_gran_bytes(have, want, len, 128);
770 break;
hailfingerb247c7a2010-03-08 00:42:32 +0000771 case write_gran_256bytes:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100772 result = need_erase_gran_bytes(have, want, len, 256);
773 break;
774 case write_gran_264bytes:
775 result = need_erase_gran_bytes(have, want, len, 264);
776 break;
777 case write_gran_512bytes:
778 result = need_erase_gran_bytes(have, want, len, 512);
779 break;
780 case write_gran_528bytes:
781 result = need_erase_gran_bytes(have, want, len, 528);
782 break;
783 case write_gran_1024bytes:
784 result = need_erase_gran_bytes(have, want, len, 1024);
785 break;
786 case write_gran_1056bytes:
787 result = need_erase_gran_bytes(have, want, len, 1056);
788 break;
789 case write_gran_1byte_implicit_erase:
790 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
791 result = 0;
hailfingerb247c7a2010-03-08 00:42:32 +0000792 break;
hailfingerb437e282010-11-04 01:04:27 +0000793 default:
794 msg_cerr("%s: Unsupported granularity! Please report a bug at "
795 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000796 }
797 return result;
798}
799
hailfingerb437e282010-11-04 01:04:27 +0000800/**
801 * Check if the buffer @have needs to be programmed to get the content of @want.
802 * If yes, return 1 and fill in first_start with the start address of the
803 * write operation and first_len with the length of the first to-be-written
804 * chunk. If not, return 0 and leave first_start and first_len undefined.
805 *
806 * Warning: This function assumes that @have and @want point to naturally
807 * aligned regions.
808 *
809 * @have buffer with current content
810 * @want buffer with desired content
811 * @len length of the checked area
812 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000813 * @first_start offset of the first byte which needs to be written (passed in
814 * value is increased by the offset of the first needed write
815 * relative to have/want or unchanged if no write is needed)
816 * @return length of the first contiguous area which needs to be written
817 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000818 *
819 * FIXME: This function needs a parameter which tells it about coalescing
820 * in relation to the max write length of the programmer and the max write
821 * length of the chip.
822 */
stefanctc5eb8a92011-11-23 09:13:48 +0000823static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
824 unsigned int *first_start,
825 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000826{
stefanctc5eb8a92011-11-23 09:13:48 +0000827 int need_write = 0;
828 unsigned int rel_start = 0, first_len = 0;
829 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000830
hailfingerb437e282010-11-04 01:04:27 +0000831 switch (gran) {
832 case write_gran_1bit:
833 case write_gran_1byte:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100834 case write_gran_1byte_implicit_erase:
hailfinger90fcf9b2010-11-05 14:51:59 +0000835 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000836 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100837 case write_gran_128bytes:
838 stride = 128;
839 break;
hailfingerb437e282010-11-04 01:04:27 +0000840 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000841 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000842 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100843 case write_gran_264bytes:
844 stride = 264;
845 break;
846 case write_gran_512bytes:
847 stride = 512;
848 break;
849 case write_gran_528bytes:
850 stride = 528;
851 break;
852 case write_gran_1024bytes:
853 stride = 1024;
854 break;
855 case write_gran_1056bytes:
856 stride = 1056;
857 break;
hailfingerb437e282010-11-04 01:04:27 +0000858 default:
859 msg_cerr("%s: Unsupported granularity! Please report a bug at "
860 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000861 /* Claim that no write was needed. A write with unknown
862 * granularity is too dangerous to try.
863 */
864 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000865 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000866 for (i = 0; i < len / stride; i++) {
867 limit = min(stride, len - i * stride);
868 /* Are 'have' and 'want' identical? */
869 if (memcmp(have + i * stride, want + i * stride, limit)) {
870 if (!need_write) {
871 /* First location where have and want differ. */
872 need_write = 1;
873 rel_start = i * stride;
874 }
875 } else {
876 if (need_write) {
877 /* First location where have and want
878 * do not differ anymore.
879 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000880 break;
881 }
882 }
883 }
hailfingerffb7f382010-12-06 13:05:44 +0000884 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000885 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000886 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000887 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000888}
889
hailfinger0c515352009-11-23 12:55:31 +0000890/* This function generates various test patterns useful for testing controller
891 * and chip communication as well as chip behaviour.
892 *
893 * If a byte can be written multiple times, each time keeping 0-bits at 0
894 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
895 * is essentially an AND operation. That's also the reason why this function
896 * provides the result of AND between various patterns.
897 *
898 * Below is a list of patterns (and their block length).
899 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
900 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
901 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
902 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
903 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
904 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
905 * Pattern 6 is 00 (1 Byte)
906 * Pattern 7 is ff (1 Byte)
907 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
908 * byte block.
909 *
910 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
911 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
912 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
913 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
914 * Pattern 12 is 00 (1 Byte)
915 * Pattern 13 is ff (1 Byte)
916 * Patterns 8-13 have no block number.
917 *
918 * Patterns 0-3 are created to detect and efficiently diagnose communication
919 * slips like missed bits or bytes and their repetitive nature gives good visual
920 * cues to the person inspecting the results. In addition, the following holds:
921 * AND Pattern 0/1 == Pattern 4
922 * AND Pattern 2/3 == Pattern 5
923 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
924 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
925 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
926 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
927 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
928 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
929 * Besides that, they provide for bit testing of the last two bytes of every
930 * 256 byte block which contains the block number for patterns 0-6.
931 * Patterns 10-11 are special purpose for detecting subblock aliasing with
932 * block sizes >256 bytes (some Dataflash chips etc.)
933 * AND Pattern 8/9 == Pattern 12
934 * AND Pattern 10/11 == Pattern 12
935 * Pattern 13 is the completely erased state.
936 * None of the patterns can detect aliasing at boundaries which are a multiple
937 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
938 */
939int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
940{
941 int i;
942
943 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000944 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000945 return 1;
946 }
947
948 switch (variant) {
949 case 0:
950 for (i = 0; i < size; i++)
951 buf[i] = (i & 0xf) << 4 | 0x5;
952 break;
953 case 1:
954 for (i = 0; i < size; i++)
955 buf[i] = (i & 0xf) << 4 | 0xa;
956 break;
957 case 2:
958 for (i = 0; i < size; i++)
959 buf[i] = 0x50 | (i & 0xf);
960 break;
961 case 3:
962 for (i = 0; i < size; i++)
963 buf[i] = 0xa0 | (i & 0xf);
964 break;
965 case 4:
966 for (i = 0; i < size; i++)
967 buf[i] = (i & 0xf) << 4;
968 break;
969 case 5:
970 for (i = 0; i < size; i++)
971 buf[i] = i & 0xf;
972 break;
973 case 6:
974 memset(buf, 0x00, size);
975 break;
976 case 7:
977 memset(buf, 0xff, size);
978 break;
979 case 8:
980 for (i = 0; i < size; i++)
981 buf[i] = i & 0xff;
982 break;
983 case 9:
984 for (i = 0; i < size; i++)
985 buf[i] = ~(i & 0xff);
986 break;
987 case 10:
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 11:
996 for (i = 0; i < size % 2; i++) {
997 buf[i * 2] = ~((i >> 8) & 0xff);
998 buf[i * 2 + 1] = ~(i & 0xff);
999 }
1000 if (size & 0x1)
1001 buf[i * 2] = ~((i >> 8) & 0xff);
1002 break;
1003 case 12:
1004 memset(buf, 0x00, size);
1005 break;
1006 case 13:
1007 memset(buf, 0xff, size);
1008 break;
1009 }
1010
1011 if ((variant >= 0) && (variant <= 7)) {
1012 /* Write block number in the last two bytes of each 256-byte
1013 * block, big endian for easier reading of the hexdump.
1014 * Note that this wraps around for chips larger than 2^24 bytes
1015 * (16 MB).
1016 */
1017 for (i = 0; i < size / 256; i++) {
1018 buf[i * 256 + 254] = (i >> 8) & 0xff;
1019 buf[i * 256 + 255] = i & 0xff;
1020 }
1021 }
1022
1023 return 0;
1024}
1025
hailfingeraec9c962009-10-31 01:53:09 +00001026int check_max_decode(enum chipbustype buses, uint32_t size)
1027{
1028 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001029
1030 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < 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.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001037 }
hailfingere1e41ea2011-07-27 07:13:06 +00001038 if ((buses & BUS_LPC) && (max_rom_decode.lpc < 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.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001045 }
hailfingere1e41ea2011-07-27 07:13:06 +00001046 if ((buses & BUS_FWH) && (max_rom_decode.fwh < 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.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001053 }
hailfingere1e41ea2011-07-27 07:13:06 +00001054 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001055 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001056 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001057 "size %u kB of chipset/board/programmer "
1058 "for %s interface, "
1059 "probe/read/erase/write may fail. ", size / 1024,
1060 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001061 }
1062 if (!limitexceeded)
1063 return 0;
1064 /* Sometimes chip and programmer have more than one bus in common,
1065 * and the limit is not exceeded on all buses. Tell the user.
1066 */
1067 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001068 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001069 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001070 "interface which can support a chip of this size. "
1071 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001072 return 1;
1073}
1074
Edward O'Callaghan8488f122019-06-17 12:38:15 +10001075/*
1076 * Return a string corresponding to the bustype parameter.
1077 * Memory is obtained with malloc() and must be freed with free() by the caller.
1078 */
1079char *flashbuses_to_text(enum chipbustype bustype)
1080{
1081 char *ret = calloc(1, 1);
1082 /*
1083 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
1084 * will cease to exist and should be eliminated here as well.
1085 */
1086 if (bustype == BUS_NONSPI) {
1087 ret = strcat_realloc(ret, "Non-SPI, ");
1088 } else {
1089 if (bustype & BUS_PARALLEL)
1090 ret = strcat_realloc(ret, "Parallel, ");
1091 if (bustype & BUS_LPC)
1092 ret = strcat_realloc(ret, "LPC, ");
1093 if (bustype & BUS_FWH)
1094 ret = strcat_realloc(ret, "FWH, ");
1095 if (bustype & BUS_SPI)
1096 ret = strcat_realloc(ret, "SPI, ");
1097 if (bustype & BUS_PROG)
1098 ret = strcat_realloc(ret, "Programmer-specific, ");
1099 if (bustype == BUS_NONE)
1100 ret = strcat_realloc(ret, "None, ");
1101 }
1102 /* Kill last comma. */
1103 ret[strlen(ret) - 2] = '\0';
1104 ret = realloc(ret, strlen(ret) + 1);
1105 return ret;
1106}
1107
Edward O'Callaghan20596a82019-06-13 14:47:03 +10001108int probe_flash(struct registered_master *mst, int startchip,
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001109 struct flashctx *flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001110{
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001111 const struct flashchip *chip, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001112 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001113 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001114 uint32_t size;
1115 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001116 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001117
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301118 /* Based on the host controller interface that a platform
1119 * needs to use (hwseq or swseq),
1120 * set the flashchips list here.
1121 */
1122 switch (ich_generation) {
1123 case CHIPSET_100_SERIES_SUNRISE_POINT:
Furquan Shaikh44088752016-07-11 22:48:08 -07001124 case CHIPSET_APL:
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301125 flash_list = flashchips_hwseq;
1126 break;
1127 default:
1128 flash_list = flashchips;
1129 break;
1130 }
1131
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001132 for (chip = flash_list + startchip; chip && chip->name; chip++) {
1133 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001134 continue;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001135 buses_common = buses_supported & chip->bustype;
David Hendricksac1d25c2016-08-09 17:00:58 -07001136 if (!buses_common) {
1137 msg_gspew("Probing for %s %s, %d kB: skipped. ",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001138 chip->vendor, chip->name, chip->total_size);
David Hendricksac1d25c2016-08-09 17:00:58 -07001139 tmp = flashbuses_to_text(buses_supported);
1140 msg_gspew("Host bus type %s ", tmp);
1141 free(tmp);
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001142 tmp = flashbuses_to_text(chip->bustype);
David Hendricksac1d25c2016-08-09 17:00:58 -07001143 msg_gspew("and chip bus type %s are incompatible.",
1144 tmp);
1145 free(tmp);
1146 msg_gspew("\n");
hailfinger18bd4cc2011-06-17 22:38:53 +00001147 continue;
David Hendricksac1d25c2016-08-09 17:00:58 -07001148 }
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001149 /* Only probe for SPI25 chips by default. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001150 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001151 continue;
hailfinger18bd4cc2011-06-17 22:38:53 +00001152 msg_gdbg("Probing for %s %s, %d kB: ",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001153 chip->vendor, chip->name, chip->total_size);
1154 if (!chip->probe && !force) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001155 msg_gdbg("failed! flashrom has no probe function for "
1156 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001157 continue;
1158 }
stepan782fb172007-04-06 11:58:03 +00001159
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001160 size = chip->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001161 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001162
hailfinger48ed3e22011-05-04 00:39:50 +00001163 /* Start filling in the dynamic data. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001164 flash->chip = calloc(1, sizeof(struct flashchip));
1165 if (!flash->chip) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001166 msg_gerr("Out of memory!\n");
1167 exit(1);
1168 }
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001169 memcpy(flash->chip, chip, sizeof(struct flashchip));
1170 flash->mst = mst;
hailfinger48ed3e22011-05-04 00:39:50 +00001171
hailfinger72d3b982009-05-09 07:27:23 +00001172 base = flashbase ? flashbase : (0xffffffff - size + 1);
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001173 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001174
stugec1e55fe2008-07-02 17:15:47 +00001175 if (force)
1176 break;
stepanc98b80b2006-03-16 16:57:41 +00001177
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001178 if (flash->chip->probe(flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001179 goto notfound;
1180
hailfinger48ed3e22011-05-04 00:39:50 +00001181 /* If this is the first chip found, accept it.
1182 * If this is not the first chip found, accept it only if it is
1183 * a non-generic match.
1184 * We could either make chipcount global or provide it as
1185 * parameter, or we assume that startchip==0 means this call to
1186 * probe_flash() is the first one and thus no chip has been
1187 * found before.
1188 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001189 if (startchip == 0 || flash->chip->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001190 break;
1191
stuge56300c32008-09-03 23:10:05 +00001192notfound:
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001193 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
1194 free(flash->chip);
1195 flash->chip = NULL;
rminnich8d3ff912003-10-25 17:01:29 +00001196 }
uwebe4477b2007-08-23 16:08:21 +00001197
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001198 if (!chip || !chip->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001199 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001200
hailfingere11396b2011-03-08 00:09:11 +00001201#if CONFIG_INTERNAL == 1
1202 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001203 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001204 else
1205#endif
stepan3e7aeae2011-01-19 06:21:54 +00001206 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001207
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001208 tmp = flashbuses_to_text(chip->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001209 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001210 force ? "Assuming" : "Found", flash->chip->vendor,
1211 flash->chip->name, flash->chip->total_size, tmp,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001212 location);
stefanct588b6d22011-06-26 20:45:35 +00001213 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001214
hailfinger0f4c3952010-12-02 21:59:42 +00001215 /* Flash registers will not be mapped if the chip was forced. Lock info
1216 * may be stored in registers, so avoid lock info printing.
1217 */
1218 if (!force)
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001219 if (flash->chip->printlock)
1220 flash->chip->printlock(flash);
snelson1ee293c2010-02-19 00:52:10 +00001221
hailfinger48ed3e22011-05-04 00:39:50 +00001222 /* Return position of matching chip. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001223 return chip - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001224}
1225
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001226static int verify_flash(struct flashctx *flash,
1227 struct action_descriptor *descriptor,
1228 int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001229{
hailfingerb0f4d122009-06-24 08:20:45 +00001230 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +01001231 unsigned int total_size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001232 uint8_t *buf = descriptor->newcontents;
rminnich8d3ff912003-10-25 17:01:29 +00001233
snelsone42c3802010-05-07 20:09:04 +00001234 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001235
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001236 if (verify_it == VERIFY_PARTIAL) {
1237 struct processing_unit *pu = descriptor->processing_units;
1238
1239 /* Verify only areas which were written. */
1240 while (pu->num_blocks) {
1241 ret = verify_range(flash, buf + pu->offset, pu->offset,
1242 pu->block_size * pu->num_blocks,
1243 NULL);
1244 if (ret)
1245 break;
1246 pu++;
1247 }
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001248 } else {
1249 ret = verify_range(flash, buf, 0, total_size, NULL);
1250 }
uwef6641642007-05-09 10:17:44 +00001251
David Hendricks1ed1d352011-11-23 17:54:37 -08001252 if (ret == ACCESS_DENIED) {
1253 msg_gdbg("Could not fully verify due to access error, ");
1254 if (access_denied_action == error_ignore) {
1255 msg_gdbg("ignoring\n");
1256 ret = 0;
1257 } else {
1258 msg_gdbg("aborting\n");
1259 }
1260 }
1261
hailfingerb0f4d122009-06-24 08:20:45 +00001262 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001263 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001264
hailfingerb0f4d122009-06-24 08:20:45 +00001265 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001266}
1267
uwe8d342eb2011-07-28 08:13:25 +00001268int read_buf_from_file(unsigned char *buf, unsigned long size,
1269 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001270{
1271 unsigned long numbytes;
1272 FILE *image;
1273 struct stat image_stat;
1274
Vincent Palatin7ab23932014-10-01 12:09:16 -07001275 if (!strncmp(filename, "-", sizeof("-")))
1276 image = fdopen(STDIN_FILENO, "rb");
1277 else
1278 image = fopen(filename, "rb");
1279 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001280 perror(filename);
1281 return 1;
1282 }
1283 if (fstat(fileno(image), &image_stat) != 0) {
1284 perror(filename);
1285 fclose(image);
1286 return 1;
1287 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001288 if ((image_stat.st_size != size) &&
1289 (strncmp(filename, "-", sizeof("-")))) {
Mike Frysinger62c794d2017-05-29 12:02:45 -04001290 msg_gerr("Error: Image size doesn't match: stat %jd bytes, "
1291 "wanted %ld!\n", (intmax_t)image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001292 fclose(image);
1293 return 1;
1294 }
1295 numbytes = fread(buf, 1, size, image);
1296 if (fclose(image)) {
1297 perror(filename);
1298 return 1;
1299 }
1300 if (numbytes != size) {
1301 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1302 "wanted %ld!\n", numbytes, size);
1303 return 1;
1304 }
1305 return 0;
1306}
1307
uwe8d342eb2011-07-28 08:13:25 +00001308int write_buf_to_file(unsigned char *buf, unsigned long size,
1309 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001310{
1311 unsigned long numbytes;
1312 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001313
1314 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001315 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001316 return 1;
1317 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001318 if (!strncmp(filename, "-", sizeof("-")))
1319 image = fdopen(STDOUT_FILENO, "wb");
1320 else
1321 image = fopen(filename, "wb");
1322 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001323 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001324 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001325 }
hailfingerd219a232009-01-28 00:27:54 +00001326
hailfingerd219a232009-01-28 00:27:54 +00001327 numbytes = fwrite(buf, 1, size, image);
1328 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001329 if (numbytes != size) {
1330 msg_gerr("File %s could not be written completely.\n",
1331 filename);
hailfingerd219a232009-01-28 00:27:54 +00001332 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001333 }
hailfingerd219a232009-01-28 00:27:54 +00001334 return 0;
1335}
1336
David Hendrickse3451942013-03-21 17:23:29 -07001337/*
1338 * read_flash - wrapper for flash->read() with additional high-level policy
1339 *
1340 * @flash flash chip
1341 * @buf buffer to store data in
1342 * @start start address
1343 * @len number of bytes to read
1344 *
1345 * This wrapper simplifies most cases when the flash chip needs to be read
1346 * since policy decisions such as non-fatal error handling is centralized.
1347 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001348int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001349 unsigned int start, unsigned int len)
1350{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001351 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001352
Patrick Georgif3fa2992017-02-02 16:24:44 +01001353 if (!flash || !flash->chip->read)
David Hendrickse3451942013-03-21 17:23:29 -07001354 return -1;
1355
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001356 msg_cdbg("%#06x-%#06x:R ", start, start + len - 1);
1357
Patrick Georgif3fa2992017-02-02 16:24:44 +01001358 ret = flash->chip->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001359 if (ret) {
1360 if (ignore_error(ret)) {
1361 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1362 start, start + len - 1);
1363 ret = 0;
1364 } else {
1365 msg_gdbg("failed to read 0x%x-0x%x\n",
1366 start, start + len - 1);
1367 }
1368 }
1369
1370 return ret;
1371}
1372
David Hendricks7c8a1612013-04-26 19:14:44 -07001373/*
1374 * write_flash - wrapper for flash->write() with additional high-level policy
1375 *
1376 * @flash flash chip
1377 * @buf buffer to write to flash
1378 * @start start address in flash
1379 * @len number of bytes to write
1380 *
1381 * TODO: Look up regions that are write-protected and avoid attempt to write
1382 * to them at all.
1383 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001384int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001385 unsigned int start, unsigned int len)
1386{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001387 if (!flash || !flash->chip->write)
David Hendricks7c8a1612013-04-26 19:14:44 -07001388 return -1;
1389
Patrick Georgif3fa2992017-02-02 16:24:44 +01001390 return flash->chip->write(flash, buf, start, len);
David Hendricks7c8a1612013-04-26 19:14:44 -07001391}
1392
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001393int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001394{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001395 unsigned long size = flash->chip->total_size * 1024;
hailfinger42a850a2010-07-13 23:56:13 +00001396 unsigned char *buf = calloc(size, sizeof(char));
1397 int ret = 0;
1398
1399 msg_cinfo("Reading flash... ");
1400 if (!buf) {
1401 msg_gerr("Memory allocation failed!\n");
1402 msg_cinfo("FAILED.\n");
1403 return 1;
1404 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001405
1406 /* To support partial read, fill buffer to all 0xFF at beginning to make
1407 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001408 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001409
Patrick Georgif3fa2992017-02-02 16:24:44 +01001410 if (!flash->chip->read) {
hailfinger42a850a2010-07-13 23:56:13 +00001411 msg_cerr("No read function available for this flash chip.\n");
1412 ret = 1;
1413 goto out_free;
1414 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001415
1416 /* First try to handle partial read case, rather than read the whole
1417 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001418 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001419 if (ret < 0) {
1420 msg_cerr("Partial read operation failed!\n");
1421 ret = 1;
1422 goto out_free;
1423 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001424 int num_regions = get_num_include_args();
1425
1426 if (ret != num_regions) {
1427 msg_cerr("Requested %d regions, but only read %d\n",
1428 num_regions, ret);
1429 ret = 1;
1430 goto out_free;
1431 }
1432
1433 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001434 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001435 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001436 msg_cerr("Read operation failed!\n");
1437 ret = 1;
1438 goto out_free;
1439 }
hailfinger42a850a2010-07-13 23:56:13 +00001440 }
1441
David Hendricksdf29a832013-06-28 14:33:51 -07001442 if (filename)
1443 ret = write_buf_to_file(buf, size, filename);
1444
hailfinger42a850a2010-07-13 23:56:13 +00001445out_free:
1446 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001447 if (ret)
1448 msg_cerr("FAILED.");
1449 else
1450 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001451 return ret;
1452}
1453
hailfingerb437e282010-11-04 01:04:27 +00001454/* This function shares a lot of its structure with erase_and_write_flash() and
1455 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001456 * Even if an error is found, the function will keep going and check the rest.
1457 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001458static int selfcheck_eraseblocks(const struct flashchip *chip)
hailfinger45177872010-01-18 08:14:43 +00001459{
hailfingerb91c08c2011-08-15 19:54:20 +00001460 int i, j, k;
1461 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001462
1463 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1464 unsigned int done = 0;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001465 struct block_eraser eraser = chip->block_erasers[k];
hailfinger45177872010-01-18 08:14:43 +00001466
1467 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1468 /* Blocks with zero size are bugs in flashchips.c. */
1469 if (eraser.eraseblocks[i].count &&
1470 !eraser.eraseblocks[i].size) {
1471 msg_gerr("ERROR: Flash chip %s erase function "
1472 "%i region %i has size 0. Please report"
1473 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001474 chip->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001475 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001476 }
1477 /* Blocks with zero count are bugs in flashchips.c. */
1478 if (!eraser.eraseblocks[i].count &&
1479 eraser.eraseblocks[i].size) {
1480 msg_gerr("ERROR: Flash chip %s erase function "
1481 "%i region %i has count 0. Please report"
1482 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001483 chip->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001484 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001485 }
1486 done += eraser.eraseblocks[i].count *
1487 eraser.eraseblocks[i].size;
1488 }
hailfinger9fed35d2010-01-19 06:42:46 +00001489 /* Empty eraseblock definition with erase function. */
1490 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001491 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001492 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001493 if (!done)
1494 continue;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001495 if (done != chip->total_size * 1024) {
hailfinger45177872010-01-18 08:14:43 +00001496 msg_gerr("ERROR: Flash chip %s erase function %i "
1497 "region walking resulted in 0x%06x bytes total,"
1498 " expected 0x%06x bytes. Please report a bug at"
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001499 " flashrom@flashrom.org\n", chip->name, k,
1500 done, chip->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001501 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001502 }
hailfinger9fed35d2010-01-19 06:42:46 +00001503 if (!eraser.block_erase)
1504 continue;
1505 /* Check if there are identical erase functions for different
1506 * layouts. That would imply "magic" erase functions. The
1507 * easiest way to check this is with function pointers.
1508 */
uwef6f94d42010-03-13 17:28:29 +00001509 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001510 if (eraser.block_erase ==
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001511 chip->block_erasers[j].block_erase) {
hailfinger9fed35d2010-01-19 06:42:46 +00001512 msg_gerr("ERROR: Flash chip %s erase function "
1513 "%i and %i are identical. Please report"
1514 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001515 chip->name, k, j);
hailfinger9fed35d2010-01-19 06:42:46 +00001516 ret = 1;
1517 }
uwef6f94d42010-03-13 17:28:29 +00001518 }
hailfinger45177872010-01-18 08:14:43 +00001519 }
hailfinger9fed35d2010-01-19 06:42:46 +00001520 return ret;
hailfinger45177872010-01-18 08:14:43 +00001521}
1522
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001523static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001524 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001525 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001526 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001527 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001528 unsigned int addr,
1529 unsigned int len))
1530{
stefanctc5eb8a92011-11-23 09:13:48 +00001531 unsigned int starthere = 0, lenhere = 0;
1532 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001533 int block_was_erased = 0;
Edward O'Callaghan10e63d92019-06-17 14:12:52 +10001534 enum write_granularity gran = flash->chip->gran;
hailfingerb437e282010-11-04 01:04:27 +00001535
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001536 /*
1537 * curcontents and newcontents are opaque to walk_eraseregions, and
1538 * need to be adjusted here to keep the impression of proper
1539 * abstraction
hailfingerb437e282010-11-04 01:04:27 +00001540 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001541
hailfinger90fcf9b2010-11-05 14:51:59 +00001542 curcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001543
hailfingerb437e282010-11-04 01:04:27 +00001544 newcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001545
hailfingerb437e282010-11-04 01:04:27 +00001546 msg_cdbg(":");
Simon Glass4c214132013-07-16 10:09:28 -06001547 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001548 content_has_changed |= 1;
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001549 msg_cdbg(" E");
hailfingerb437e282010-11-04 01:04:27 +00001550 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001551 if (ret) {
1552 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001553 msg_cdbg(" DENIED");
David Hendricks1ed1d352011-11-23 17:54:37 -08001554 else
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001555 msg_cerr(" ERASE_FAILED\n");
hailfingerb437e282010-11-04 01:04:27 +00001556 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001557 }
1558
David Hendricks0954ffc2015-11-13 15:15:44 -08001559 if (programmer_table[programmer].paranoid) {
1560 if (check_erased_range(flash, start, len)) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001561 msg_cerr(" ERASE_FAILED\n");
David Hendricks0954ffc2015-11-13 15:15:44 -08001562 return -1;
1563 }
hailfingerac8e3182011-06-26 17:04:16 +00001564 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001565
hailfinger90fcf9b2010-11-05 14:51:59 +00001566 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001567 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001568 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001569 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001570 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001571 /* get_next_write() sets starthere to a new value after the call. */
1572 while ((lenhere = get_next_write(curcontents + starthere,
1573 newcontents + starthere,
1574 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001575 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001576 if (!writecount++)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001577 msg_cdbg(" W");
hailfingerb437e282010-11-04 01:04:27 +00001578 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001579 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001580 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001581 if (ret) {
1582 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001583 msg_cdbg(" DENIED");
hailfingerb437e282010-11-04 01:04:27 +00001584 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001585 }
David Hendricks048b38c2016-03-28 18:47:06 -07001586
1587 /*
1588 * If the block needed to be erased and was erased successfully
1589 * then we can assume that we didn't run into any write-
1590 * protected areas. Otherwise, we need to verify each page to
1591 * ensure it was successfully written and abort if we encounter
1592 * any errors.
1593 */
1594 if (programmer_table[programmer].paranoid && !block_was_erased) {
1595 if (verify_range(flash, newcontents + starthere,
1596 start + starthere, lenhere, "WRITE"))
1597 return -1;
1598 }
1599
hailfingerb437e282010-11-04 01:04:27 +00001600 starthere += lenhere;
1601 skip = 0;
1602 }
1603 if (skip)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001604 msg_cdbg(" SKIP");
hailfingerb437e282010-11-04 01:04:27 +00001605 return ret;
1606}
1607
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001608/*
1609 * Function to process processing units accumulated in the action descriptor.
1610 *
1611 * @flash pointer to the flash context to operate on
1612 * @do_something helper function which can erase and program a section of the
1613 * flash chip. It receives the flash context, offset and length
1614 * of the area to erase/program, before and after contents (to
1615 * decide what exactly needs to be erased and or programmed)
1616 * and a pointer to the erase function which can operate on the
1617 * proper granularity.
1618 * @descriptor action descriptor including pointers to before and after
1619 * contents and an array of processing actions to take.
1620 *
1621 * Returns zero on success or an error code.
1622 */
1623static int walk_eraseregions(struct flashctx *flash,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001624 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001625 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001626 unsigned int len,
1627 uint8_t *param1,
1628 uint8_t *param2,
1629 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001630 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001631 unsigned int addr,
1632 unsigned int len)),
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001633 struct action_descriptor *descriptor)
hailfinger2b8c9382010-07-13 00:37:19 +00001634{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001635 struct processing_unit *pu;
1636 int rc = 0;
1637 static int print_comma;
uwe8d342eb2011-07-28 08:13:25 +00001638
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001639 for (pu = descriptor->processing_units; pu->num_blocks; pu++) {
1640 unsigned base = pu->offset;
1641 unsigned top = pu->offset + pu->block_size * pu->num_blocks;
David Hendricks605544b2015-08-15 16:32:58 -07001642
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001643 while (base < top) {
David Hendricks605544b2015-08-15 16:32:58 -07001644
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001645 if (print_comma)
hailfingerb437e282010-11-04 01:04:27 +00001646 msg_cdbg(", ");
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001647 else
1648 print_comma = 1;
1649
1650 msg_cdbg("0x%06x-0x%06zx", base, base + pu->block_size - 1);
1651
1652 rc = do_something(flash, base,
1653 pu->block_size,
1654 descriptor->oldcontents,
1655 descriptor->newcontents,
1656 flash->chip->block_erasers[pu->block_eraser_index].block_erase);
1657
David Hendricks1ed1d352011-11-23 17:54:37 -08001658 if (rc) {
1659 if (ignore_error(rc))
1660 rc = 0;
1661 else
1662 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001663 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001664 base += pu->block_size;
hailfinger2b8c9382010-07-13 00:37:19 +00001665 }
1666 }
hailfingerb437e282010-11-04 01:04:27 +00001667 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001668 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001669}
1670
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001671static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001672{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001673 struct block_eraser eraser = flash->chip->block_erasers[k];
hailfingercf848f12010-12-05 15:14:44 +00001674
1675 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1676 if (log)
1677 msg_cdbg("not defined. ");
1678 return 1;
1679 }
1680 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1681 if (log)
1682 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001683 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001684 return 1;
1685 }
1686 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1687 if (log)
1688 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001689 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001690 return 1;
1691 }
1692 return 0;
1693}
1694
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001695int erase_and_write_flash(struct flashctx *flash,
1696 struct action_descriptor *descriptor)
hailfingerd219a232009-01-28 00:27:54 +00001697{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001698 int ret = 1;
hailfingercf848f12010-12-05 15:14:44 +00001699
hailfingercf848f12010-12-05 15:14:44 +00001700 msg_cinfo("Erasing and writing flash chip... ");
hailfingerb437e282010-11-04 01:04:27 +00001701
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001702 ret = walk_eraseregions(flash, &erase_and_write_block_helper, descriptor);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001703
hailfinger7df21362009-09-05 02:30:58 +00001704 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001705 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001706 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001707 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001708 }
1709 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001710}
1711
hailfinger4c47e9d2010-10-19 22:06:20 +00001712void nonfatal_help_message(void)
1713{
1714 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1715 "This means we have to add special support for your board, "
1716 "programmer or flash chip.\n"
1717 "Please report this on IRC at irc.freenode.net (channel "
1718 "#flashrom) or\n"
1719 "mail flashrom@flashrom.org!\n"
1720 "-------------------------------------------------------------"
1721 "------------------\n"
1722 "You may now reboot or simply leave the machine running.\n");
1723}
1724
uweb34ec9f2009-10-01 18:40:02 +00001725void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001726{
snelsone42c3802010-05-07 20:09:04 +00001727 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001728 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001729 "mail flashrom@flashrom.org with FAILED: your board name in "
1730 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001731 "-------------------------------------------------------------"
1732 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001733 "DO NOT REBOOT OR POWEROFF!\n");
1734}
1735
uwe8d342eb2011-07-28 08:13:25 +00001736/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001737void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001738{
1739 enum programmer p;
1740 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001741 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001742 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001743 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001744 }
Simon Glass8dc82732013-07-16 10:13:51 -06001745 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001746}
1747
hailfingerf79d1712010-10-06 23:48:34 +00001748void list_programmers_linebreak(int startcol, int cols, int paren)
1749{
1750 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001751 int pnamelen;
1752 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001753 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001754 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001755
1756 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1757 pname = programmer_table[p].name;
1758 pnamelen = strlen(pname);
1759 if (remaining - pnamelen - 2 < 0) {
1760 if (firstline)
1761 firstline = 0;
1762 else
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001763 msg_ginfo("\n");
hailfingerf79d1712010-10-06 23:48:34 +00001764 for (i = 0; i < startcol; i++)
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001765 msg_ginfo(" ");
hailfingerf79d1712010-10-06 23:48:34 +00001766 remaining = cols - startcol;
1767 } else {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001768 msg_ginfo(" ");
hailfingerf79d1712010-10-06 23:48:34 +00001769 remaining--;
1770 }
1771 if (paren && (p == 0)) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001772 msg_ginfo("(");
hailfingerf79d1712010-10-06 23:48:34 +00001773 remaining--;
1774 }
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001775 msg_ginfo("%s", pname);
hailfingerf79d1712010-10-06 23:48:34 +00001776 remaining -= pnamelen;
1777 if (p < PROGRAMMER_INVALID - 1) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001778 msg_ginfo(",");
hailfingerf79d1712010-10-06 23:48:34 +00001779 remaining--;
1780 } else {
1781 if (paren)
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001782 msg_ginfo(")");
1783 msg_ginfo("\n");
hailfingerf79d1712010-10-06 23:48:34 +00001784 }
1785 }
1786}
1787
hailfinger3b471632010-03-27 16:36:40 +00001788void print_sysinfo(void)
1789{
David Hendricksc6c9f822010-11-03 15:07:01 -07001790 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001791#if HAVE_UTSNAME == 1
1792 struct utsname osinfo;
1793 uname(&osinfo);
1794
David Hendricksc6c9f822010-11-03 15:07:01 -07001795 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001796 osinfo.machine);
1797#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001798 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001799#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001800}
1801
1802void print_buildinfo(void)
1803{
1804 msg_gdbg("flashrom was built with");
hailfinger3b471632010-03-27 16:36:40 +00001805#if NEED_PCI == 1
1806#ifdef PCILIB_VERSION
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001807 msg_gdbg(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001808#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001809 msg_gdbg(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001810#endif
1811#endif
1812#ifdef __clang__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001813 msg_gdbg(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001814#ifdef __clang_version__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001815 msg_gdbg(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001816#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001817 msg_gdbg(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001818#endif
hailfinger3b471632010-03-27 16:36:40 +00001819#elif defined(__GNUC__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001820 msg_gdbg(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001821#ifdef __VERSION__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001822 msg_gdbg(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001823#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001824 msg_gdbg(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001825#endif
1826#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001827 msg_gdbg(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001828#endif
1829#if defined (__FLASHROM_LITTLE_ENDIAN__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001830 msg_gdbg(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001831#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001832 msg_gdbg(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001833#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001834 msg_gdbg("\n");
hailfinger3b471632010-03-27 16:36:40 +00001835}
1836
uwefdeca092008-01-21 15:24:22 +00001837void print_version(void)
1838{
David Hendricksc6c9f822010-11-03 15:07:01 -07001839 /* send to stderr for chromium os */
1840 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001841 print_sysinfo();
David Hendricks07268292016-09-14 16:05:58 -07001842 msg_gerr("\n");
uwefdeca092008-01-21 15:24:22 +00001843}
1844
hailfinger74819ad2010-05-15 15:04:37 +00001845void print_banner(void)
1846{
1847 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001848 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001849 msg_ginfo("\n");
1850}
1851
hailfingerc77acb52009-12-24 02:15:55 +00001852int selfcheck(void)
1853{
hailfinger45177872010-01-18 08:14:43 +00001854 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001855 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001856
1857 /* Safety check. Instead of aborting after the first error, check
1858 * if more errors exist.
1859 */
hailfingerc77acb52009-12-24 02:15:55 +00001860 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001861 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001862 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001863 }
stefanct8632c922011-07-26 14:33:46 +00001864 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001865 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001866 * For 'flashchips' we check the first element to be non-null. In the
1867 * other cases there exist use cases where the first element can be
1868 * null. */
Yunlian Jiang945c3392015-01-20 15:56:44 -08001869 if (flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001870 msg_gerr("Flashchips table miscompilation!\n");
1871 ret = 1;
1872 }
hailfinger45177872010-01-18 08:14:43 +00001873 for (flash = flashchips; flash && flash->name; flash++)
1874 if (selfcheck_eraseblocks(flash))
1875 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001876
hailfinger45177872010-01-18 08:14:43 +00001877 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001878}
1879
hailfinger771fc182010-10-15 00:01:14 +00001880/* FIXME: This function signature needs to be improved once doit() has a better
1881 * function signature.
1882 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001883int chip_safety_check(const struct flashctx *flash, int force, int read_it, int write_it, int erase_it, int verify_it)
hailfinger771fc182010-10-15 00:01:14 +00001884{
Patrick Georgiac3423f2017-02-03 20:58:06 +01001885 const struct flashchip *chip = flash->chip;
1886
hailfinger771fc182010-10-15 00:01:14 +00001887 if (!programmer_may_write && (write_it || erase_it)) {
1888 msg_perr("Write/erase is not working yet on your programmer in "
1889 "its current configuration.\n");
1890 /* --force is the wrong approach, but it's the best we can do
1891 * until the generic programmer parameter parser is merged.
1892 */
1893 if (!force)
1894 return 1;
1895 msg_cerr("Continuing anyway.\n");
1896 }
1897
1898 if (read_it || erase_it || write_it || verify_it) {
1899 /* Everything needs read. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001900 if (chip->tested.read == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001901 msg_cerr("Read is not working on this chip. ");
1902 if (!force)
1903 return 1;
1904 msg_cerr("Continuing anyway.\n");
1905 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001906 if (!chip->read) {
hailfinger771fc182010-10-15 00:01:14 +00001907 msg_cerr("flashrom has no read function for this "
1908 "flash chip.\n");
1909 return 1;
1910 }
1911 }
1912 if (erase_it || write_it) {
1913 /* Write needs erase. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001914 if (chip->tested.erase == NA) {
1915 msg_cerr("Erase is not possible on this chip.\n");
1916 return 1;
1917 }
1918 if (chip->tested.erase == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001919 msg_cerr("Erase is not working on this chip. ");
1920 if (!force)
1921 return 1;
1922 msg_cerr("Continuing anyway.\n");
1923 }
stefancte1c5acf2011-07-04 07:27:17 +00001924 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001925 msg_cerr("flashrom has no erase function for this "
1926 "flash chip.\n");
1927 return 1;
1928 }
hailfinger771fc182010-10-15 00:01:14 +00001929 }
1930 if (write_it) {
Patrick Georgiac3423f2017-02-03 20:58:06 +01001931 if (chip->tested.write == NA) {
1932 msg_cerr("Write is not possible on this chip.\n");
1933 return 1;
1934 }
1935 if (chip->tested.write == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001936 msg_cerr("Write is not working on this chip. ");
1937 if (!force)
1938 return 1;
1939 msg_cerr("Continuing anyway.\n");
1940 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001941 if (!chip->write) {
hailfinger771fc182010-10-15 00:01:14 +00001942 msg_cerr("flashrom has no write function for this "
1943 "flash chip.\n");
1944 return 1;
1945 }
1946 }
1947 return 0;
1948}
1949
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001950/*
1951 * Function to erase entire flash chip.
1952 *
1953 * @flashctx pointer to the flash context to use
1954 * @oldcontents pointer to the buffer including current chip contents, to
1955 * decide which areas do in fact need to be erased
1956 * @size the size of the flash chip, in bytes.
1957 *
1958 * Returns zero on success or an error code.
1959 */
1960static int erase_chip(struct flashctx *flash, void *oldcontents,
1961 void *newcontents, size_t size)
1962{
1963 /*
1964 * To make sure that the chip is fully erased, let's cheat and create
1965 * a descriptor where the new contents are all erased.
1966 */
1967 struct action_descriptor *fake_descriptor;
1968 int ret = 0;
1969
1970 fake_descriptor = prepare_action_descriptor(flash, oldcontents,
1971 newcontents, 1);
1972 /* FIXME: Do we really want the scary warning if erase failed? After
1973 * all, after erase the chip is either blank or partially blank or it
1974 * has the old contents. A blank chip won't boot, so if the user
1975 * wanted erase and reboots afterwards, the user knows very well that
1976 * booting won't work.
1977 */
1978 if (erase_and_write_flash(flash, fake_descriptor)) {
1979 emergency_help_message();
1980 ret = 1;
1981 }
1982
1983 free(fake_descriptor);
1984
1985 return ret;
1986}
1987
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07001988static int read_dest_content(struct flashctx *flash, int verify_it,
1989 uint8_t *dest, unsigned long size)
1990{
1991 if (((verify_it == VERIFY_OFF) || (verify_it == VERIFY_PARTIAL))
1992 && get_num_include_args()) {
1993 /*
1994 * If no full verification is required and not
1995 * the entire chip is about to be programmed,
1996 * read only the areas which might change.
1997 */
1998 if (handle_partial_read(flash, dest, read_flash, 0) < 0)
1999 return 1;
2000 } else {
2001 if (read_flash(flash, dest, 0, size))
2002 return 1;
2003 }
2004 return 0;
2005}
2006
hailfingerc77acb52009-12-24 02:15:55 +00002007/* This function signature is horrible. We need to design a better interface,
2008 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00002009 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00002010 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002011int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09002012 int write_it, int erase_it, int verify_it, int extract_it,
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002013 const char *diff_file, int do_diff)
hailfingerc77acb52009-12-24 02:15:55 +00002014{
hailfinger4c47e9d2010-10-19 22:06:20 +00002015 uint8_t *oldcontents;
2016 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00002017 int ret = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +01002018 unsigned long size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002019 struct action_descriptor *descriptor = NULL;
hailfingerc77acb52009-12-24 02:15:55 +00002020
stefanct02116582011-05-18 01:30:56 +00002021 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00002022 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00002023 ret = 1;
2024 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00002025 }
2026
hailfinger771fc182010-10-15 00:01:14 +00002027 /* Given the existence of read locks, we want to unlock for read,
2028 * erase and write.
2029 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01002030 if (flash->chip->unlock)
2031 flash->chip->unlock(flash);
hailfinger771fc182010-10-15 00:01:14 +00002032
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +10002033 flash->address_high_byte = -1;
2034 flash->in_4ba_mode = false;
2035
Ed Swierk28cf7992017-07-03 13:17:18 -07002036 /* Enable/disable 4-byte addressing mode if flash chip supports it */
2037 if ((flash->chip->feature_bits & FEATURE_4BA_SUPPORT) &&
2038 flash->chip->four_bytes_addr_funcs.set_4ba) {
2039 if (flash->chip->four_bytes_addr_funcs.set_4ba(flash)) {
2040 msg_cerr("Enabling/disabling 4-byte addressing mode failed!\n");
2041 return 1;
Boris Baykov6323c242016-06-11 18:29:03 +02002042 }
Boris Baykov1a2f5322016-06-11 18:29:00 +02002043 }
2044
Simon Glass9ad06c12013-07-03 22:08:17 +09002045 if (extract_it) {
2046 ret = extract_regions(flash);
2047 goto out_nofree;
2048 }
2049
David Hendricksd0ea9ed2011-03-04 17:31:57 -08002050 /* mark entries included using -i argument as "included" if they are
2051 found in the master rom_entries list */
2052 if (process_include_args() < 0) {
2053 ret = 1;
2054 goto out_nofree;
2055 }
2056
hailfinger771fc182010-10-15 00:01:14 +00002057 if (read_it) {
2058 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00002059 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00002060 }
hailfingerb437e282010-11-04 01:04:27 +00002061
stefanctd611e8f2011-07-12 22:35:21 +00002062 oldcontents = malloc(size);
2063 if (!oldcontents) {
2064 msg_gerr("Out of memory!\n");
2065 exit(1);
2066 }
Simon Glass4c214132013-07-16 10:09:28 -06002067 /* Assume worst case: All blocks are not erased. */
2068 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002069 newcontents = malloc(size);
2070 if (!newcontents) {
2071 msg_gerr("Out of memory!\n");
2072 exit(1);
2073 }
Simon Glass4c214132013-07-16 10:09:28 -06002074 /* Assume best case: All blocks are erased. */
2075 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002076 /* Side effect of the assumptions above: Default write action is erase
2077 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002078 * oldcontents being completely unerased means we have to erase
2079 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002080 */
2081
hailfingerd217d122010-10-08 18:52:29 +00002082 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002083 /*
2084 * Note: This must be done before any files specified by -i
2085 * arguments are processed merged into the newcontents since
2086 * -i files take priority. See http://crbug.com/263495.
2087 */
2088 if (filename) {
2089 if (read_buf_from_file(newcontents, size, filename)) {
2090 ret = 1;
2091 goto out;
2092 }
2093 } else {
2094 /* Content will be read from -i args, so they must
2095 * not overlap. */
2096 if (included_regions_overlap()) {
2097 msg_gerr("Error: Included regions must "
2098 "not overlap.\n");
2099 ret = 1;
2100 goto out;
2101 }
stepan1da96c02006-11-21 23:48:51 +00002102 }
2103
David Hendricksac82cac2012-06-19 10:29:37 -07002104#if 0
2105 /*
2106 * FIXME: show_id() causes failure if vendor:mainboard do not
2107 * match. This may happen if codenames are in flux.
2108 * See chrome-os-partner:10414.
2109 */
hailfinger90c7d542010-05-31 15:27:27 +00002110#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002111 if (programmer == PROGRAMMER_INTERNAL)
2112 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002113#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002114#endif
ollie5672ac62004-03-17 22:22:08 +00002115 }
2116
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002117 if (do_diff) {
2118 /*
2119 * Obtain a reference image so that we can check whether
2120 * regions need to be erased and to give better diagnostics in
2121 * case write fails. If --fast-verify is used then only the
2122 * regions which are included using -i will be read.
2123 */
2124 if (diff_file) {
2125 msg_cdbg("Reading old contents from file... ");
2126 if (read_buf_from_file(oldcontents, size, diff_file)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002127 ret = 1;
2128 msg_cdbg("FAILED.\n");
2129 goto out;
2130 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002131 } else {
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002132 msg_cdbg("Reading old contents from flash chip... ");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002133 ret = read_dest_content(flash, verify_it,
2134 oldcontents, size);
2135 if (ret) {
2136 msg_cdbg("FAILED.\n");
2137 goto out;
David Hendricks52ddff02013-07-23 15:05:14 -07002138 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002139 }
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002140 msg_cdbg("done.\n");
2141 } else if (!erase_it) {
2142 msg_pinfo("No diff performed, considering the chip erased.\n");
2143 memset(oldcontents, flash_erase_value(flash), size);
hailfinger4c47e9d2010-10-19 22:06:20 +00002144 }
David Hendricksac1d25c2016-08-09 17:00:58 -07002145
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002146
David Hendricksdf29a832013-06-28 14:33:51 -07002147 /*
2148 * Note: This must be done after reading the file specified for the
2149 * -w/-v argument, if any, so that files specified using -i end up
2150 * in the "newcontents" buffer before being written.
2151 * See http://crbug.com/263495.
2152 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002153 if (handle_romentries(flash, oldcontents, newcontents, erase_it)) {
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002154 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002155 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002156 goto out;
2157 }
uwef6641642007-05-09 10:17:44 +00002158
David Hendricksa7e114b2016-02-26 18:49:15 -08002159 if (erase_it) {
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002160 erase_chip(flash, oldcontents, newcontents, size);
2161 goto verify;
David Hendricksa7e114b2016-02-26 18:49:15 -08002162 }
2163
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002164 descriptor = prepare_action_descriptor(flash, oldcontents,
2165 newcontents, do_diff);
stuge8ce3a3c2008-04-28 14:47:30 +00002166 if (write_it) {
David Hendricksb64b39a2016-10-11 13:48:06 -07002167 // parse the new fmap and disable soft WP if necessary
David Hendricksac1d25c2016-08-09 17:00:58 -07002168 if ((ret = cros_ec_prepare(newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002169 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002170 goto out;
2171 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002172
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002173 if (erase_and_write_flash(flash, descriptor)) {
hailfingerb437e282010-11-04 01:04:27 +00002174 msg_cerr("Uh oh. Erase/write failed. Checking if "
2175 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002176 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002177 if (!memcmp(oldcontents, newcontents, size)) {
2178 msg_cinfo("Good. It seems nothing was "
2179 "changed.\n");
2180 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002181 ret = 1;
2182 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002183 }
2184 }
hailfingerd217d122010-10-08 18:52:29 +00002185 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002186 ret = 1;
2187 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002188 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002189
David Hendricksac1d25c2016-08-09 17:00:58 -07002190 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002191 if (ret < 0) {
2192 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002193 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002194 emergency_help_message();
2195 ret = 1;
2196 goto out;
2197 } else if (ret > 0) {
2198 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002199 msg_pdbg("CROS_EC needs 2nd pass.\n");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002200 ret = read_dest_content(flash, verify_it,
2201 oldcontents, size);
2202 if (ret) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002203 emergency_help_message();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002204 goto out;
2205 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002206
2207 /* Get a new descriptor. */
2208 free(descriptor);
2209 descriptor = prepare_action_descriptor(flash,
2210 oldcontents,
2211 newcontents,
2212 do_diff);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002213 // write 2nd pass
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002214 if (erase_and_write_flash(flash, descriptor)) {
David Hendricksb907de32014-08-11 16:47:09 -07002215 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002216 emergency_help_message();
2217 ret = 1;
2218 goto out;
2219 }
2220 ret = 0;
2221 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002222
David Hendricksac1d25c2016-08-09 17:00:58 -07002223 if (cros_ec_finish() < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002224 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002225 emergency_help_message();
2226 ret = 1;
2227 goto out;
2228 }
stuge8ce3a3c2008-04-28 14:47:30 +00002229 }
ollie6a600992005-11-26 21:55:36 +00002230
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002231 verify:
hailfinger0459e1c2009-08-19 13:55:34 +00002232 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002233 if ((write_it || erase_it) && !content_has_changed) {
2234 msg_gdbg("Nothing was erased or written, skipping "
2235 "verification\n");
2236 } else {
2237 /* Work around chips which need some time to calm down. */
2238 if (write_it && verify_it != VERIFY_PARTIAL)
2239 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002240
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002241 ret = verify_flash(flash, descriptor, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002242
David Hendricks9ba79fb2015-04-03 12:06:16 -07002243 /* If we tried to write, and verification now fails, we
2244 * might have an emergency situation.
2245 */
2246 if (ret && write_it)
2247 emergency_help_message();
2248 }
hailfinger0459e1c2009-08-19 13:55:34 +00002249 }
ollie6a600992005-11-26 21:55:36 +00002250
hailfinger90fcf9b2010-11-05 14:51:59 +00002251out:
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002252 if (descriptor)
2253 free(descriptor);
2254
hailfinger90fcf9b2010-11-05 14:51:59 +00002255 free(oldcontents);
2256 free(newcontents);
2257out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002258 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002259 /*
Edward O'Callaghan1a3fd132019-06-04 14:18:55 +10002260 * programmer_shutdown() call is moved to cli_classic() in chromium os
David Hendricks668f29d2011-01-27 18:51:45 -08002261 * tree. This is because some operations, such as write protection,
2262 * requires programmer_shutdown() but does not call doit().
2263 */
2264// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002265 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002266}