blob: 1a6814b2436a6cff7261777830d63f0548cb9d4b [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{
Craig Hesling65eb8812019-08-01 09:33:56 -0700447 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{
Craig Hesling65eb8812019-08-01 09:33:56 -0700452 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{
Craig Hesling65eb8812019-08-01 09:33:56 -0700457 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{
Craig Hesling65eb8812019-08-01 09:33:56 -0700462 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{
Craig Hesling65eb8812019-08-01 09:33:56 -0700467 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{
Craig Hesling65eb8812019-08-01 09:33:56 -0700472 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{
Craig Hesling65eb8812019-08-01 09:33:56 -0700477 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{
Craig Hesling65eb8812019-08-01 09:33:56 -0700482 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 */
Edward O'Callaghane3e30562019-09-03 13:10:58 +10001122 switch (g_ich_generation) {
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301123 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;
Craig Hesling65eb8812019-08-01 09:33:56 -07001135 buses_common = buses_supported & chip->bustype;
Edward O'Callaghan4b940572019-08-02 01:44:47 +10001136 if (!buses_common)
hailfinger18bd4cc2011-06-17 22:38:53 +00001137 continue;
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001138 /* Only probe for SPI25 chips by default. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001139 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001140 continue;
hailfinger18bd4cc2011-06-17 22:38:53 +00001141 msg_gdbg("Probing for %s %s, %d kB: ",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001142 chip->vendor, chip->name, chip->total_size);
1143 if (!chip->probe && !force) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001144 msg_gdbg("failed! flashrom has no probe function for "
1145 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001146 continue;
1147 }
stepan782fb172007-04-06 11:58:03 +00001148
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001149 size = chip->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001150 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001151
hailfinger48ed3e22011-05-04 00:39:50 +00001152 /* Start filling in the dynamic data. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001153 flash->chip = calloc(1, sizeof(struct flashchip));
1154 if (!flash->chip) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001155 msg_gerr("Out of memory!\n");
1156 exit(1);
1157 }
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001158 memcpy(flash->chip, chip, sizeof(struct flashchip));
1159 flash->mst = mst;
hailfinger48ed3e22011-05-04 00:39:50 +00001160
hailfinger72d3b982009-05-09 07:27:23 +00001161 base = flashbase ? flashbase : (0xffffffff - size + 1);
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001162 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001163
stugec1e55fe2008-07-02 17:15:47 +00001164 if (force)
1165 break;
stepanc98b80b2006-03-16 16:57:41 +00001166
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001167 if (flash->chip->probe(flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001168 goto notfound;
1169
hailfinger48ed3e22011-05-04 00:39:50 +00001170 /* If this is the first chip found, accept it.
1171 * If this is not the first chip found, accept it only if it is
1172 * a non-generic match.
1173 * We could either make chipcount global or provide it as
1174 * parameter, or we assume that startchip==0 means this call to
1175 * probe_flash() is the first one and thus no chip has been
1176 * found before.
1177 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001178 if (startchip == 0 || flash->chip->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001179 break;
1180
stuge56300c32008-09-03 23:10:05 +00001181notfound:
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001182 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
1183 free(flash->chip);
1184 flash->chip = NULL;
rminnich8d3ff912003-10-25 17:01:29 +00001185 }
uwebe4477b2007-08-23 16:08:21 +00001186
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001187 if (!chip || !chip->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001188 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001189
hailfingere11396b2011-03-08 00:09:11 +00001190#if CONFIG_INTERNAL == 1
1191 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001192 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001193 else
1194#endif
stepan3e7aeae2011-01-19 06:21:54 +00001195 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001196
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001197 tmp = flashbuses_to_text(chip->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001198 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001199 force ? "Assuming" : "Found", flash->chip->vendor,
1200 flash->chip->name, flash->chip->total_size, tmp,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001201 location);
stefanct588b6d22011-06-26 20:45:35 +00001202 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001203
hailfinger0f4c3952010-12-02 21:59:42 +00001204 /* Flash registers will not be mapped if the chip was forced. Lock info
1205 * may be stored in registers, so avoid lock info printing.
1206 */
1207 if (!force)
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001208 if (flash->chip->printlock)
1209 flash->chip->printlock(flash);
snelson1ee293c2010-02-19 00:52:10 +00001210
hailfinger48ed3e22011-05-04 00:39:50 +00001211 /* Return position of matching chip. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001212 return chip - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001213}
1214
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001215static int verify_flash(struct flashctx *flash,
1216 struct action_descriptor *descriptor,
1217 int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001218{
hailfingerb0f4d122009-06-24 08:20:45 +00001219 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +01001220 unsigned int total_size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001221 uint8_t *buf = descriptor->newcontents;
rminnich8d3ff912003-10-25 17:01:29 +00001222
snelsone42c3802010-05-07 20:09:04 +00001223 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001224
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001225 if (verify_it == VERIFY_PARTIAL) {
1226 struct processing_unit *pu = descriptor->processing_units;
1227
1228 /* Verify only areas which were written. */
1229 while (pu->num_blocks) {
1230 ret = verify_range(flash, buf + pu->offset, pu->offset,
1231 pu->block_size * pu->num_blocks,
1232 NULL);
1233 if (ret)
1234 break;
1235 pu++;
1236 }
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001237 } else {
1238 ret = verify_range(flash, buf, 0, total_size, NULL);
1239 }
uwef6641642007-05-09 10:17:44 +00001240
David Hendricks1ed1d352011-11-23 17:54:37 -08001241 if (ret == ACCESS_DENIED) {
1242 msg_gdbg("Could not fully verify due to access error, ");
1243 if (access_denied_action == error_ignore) {
1244 msg_gdbg("ignoring\n");
1245 ret = 0;
1246 } else {
1247 msg_gdbg("aborting\n");
1248 }
1249 }
1250
hailfingerb0f4d122009-06-24 08:20:45 +00001251 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001252 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001253
hailfingerb0f4d122009-06-24 08:20:45 +00001254 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001255}
1256
uwe8d342eb2011-07-28 08:13:25 +00001257int read_buf_from_file(unsigned char *buf, unsigned long size,
1258 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001259{
1260 unsigned long numbytes;
1261 FILE *image;
1262 struct stat image_stat;
1263
Vincent Palatin7ab23932014-10-01 12:09:16 -07001264 if (!strncmp(filename, "-", sizeof("-")))
1265 image = fdopen(STDIN_FILENO, "rb");
1266 else
1267 image = fopen(filename, "rb");
1268 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001269 perror(filename);
1270 return 1;
1271 }
1272 if (fstat(fileno(image), &image_stat) != 0) {
1273 perror(filename);
1274 fclose(image);
1275 return 1;
1276 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001277 if ((image_stat.st_size != size) &&
1278 (strncmp(filename, "-", sizeof("-")))) {
Mike Frysinger62c794d2017-05-29 12:02:45 -04001279 msg_gerr("Error: Image size doesn't match: stat %jd bytes, "
1280 "wanted %ld!\n", (intmax_t)image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001281 fclose(image);
1282 return 1;
1283 }
1284 numbytes = fread(buf, 1, size, image);
1285 if (fclose(image)) {
1286 perror(filename);
1287 return 1;
1288 }
1289 if (numbytes != size) {
1290 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1291 "wanted %ld!\n", numbytes, size);
1292 return 1;
1293 }
1294 return 0;
1295}
1296
uwe8d342eb2011-07-28 08:13:25 +00001297int write_buf_to_file(unsigned char *buf, unsigned long size,
1298 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001299{
1300 unsigned long numbytes;
1301 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001302
1303 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001304 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001305 return 1;
1306 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001307 if (!strncmp(filename, "-", sizeof("-")))
1308 image = fdopen(STDOUT_FILENO, "wb");
1309 else
1310 image = fopen(filename, "wb");
1311 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001312 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001313 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001314 }
hailfingerd219a232009-01-28 00:27:54 +00001315
hailfingerd219a232009-01-28 00:27:54 +00001316 numbytes = fwrite(buf, 1, size, image);
1317 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001318 if (numbytes != size) {
1319 msg_gerr("File %s could not be written completely.\n",
1320 filename);
hailfingerd219a232009-01-28 00:27:54 +00001321 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001322 }
hailfingerd219a232009-01-28 00:27:54 +00001323 return 0;
1324}
1325
David Hendrickse3451942013-03-21 17:23:29 -07001326/*
1327 * read_flash - wrapper for flash->read() with additional high-level policy
1328 *
1329 * @flash flash chip
1330 * @buf buffer to store data in
1331 * @start start address
1332 * @len number of bytes to read
1333 *
1334 * This wrapper simplifies most cases when the flash chip needs to be read
1335 * since policy decisions such as non-fatal error handling is centralized.
1336 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001337int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001338 unsigned int start, unsigned int len)
1339{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001340 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001341
Patrick Georgif3fa2992017-02-02 16:24:44 +01001342 if (!flash || !flash->chip->read)
David Hendrickse3451942013-03-21 17:23:29 -07001343 return -1;
1344
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001345 msg_cdbg("%#06x-%#06x:R ", start, start + len - 1);
1346
Patrick Georgif3fa2992017-02-02 16:24:44 +01001347 ret = flash->chip->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001348 if (ret) {
1349 if (ignore_error(ret)) {
1350 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1351 start, start + len - 1);
1352 ret = 0;
1353 } else {
1354 msg_gdbg("failed to read 0x%x-0x%x\n",
1355 start, start + len - 1);
1356 }
1357 }
1358
1359 return ret;
1360}
1361
David Hendricks7c8a1612013-04-26 19:14:44 -07001362/*
1363 * write_flash - wrapper for flash->write() with additional high-level policy
1364 *
1365 * @flash flash chip
1366 * @buf buffer to write to flash
1367 * @start start address in flash
1368 * @len number of bytes to write
1369 *
1370 * TODO: Look up regions that are write-protected and avoid attempt to write
1371 * to them at all.
1372 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001373int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001374 unsigned int start, unsigned int len)
1375{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001376 if (!flash || !flash->chip->write)
David Hendricks7c8a1612013-04-26 19:14:44 -07001377 return -1;
1378
Patrick Georgif3fa2992017-02-02 16:24:44 +01001379 return flash->chip->write(flash, buf, start, len);
David Hendricks7c8a1612013-04-26 19:14:44 -07001380}
1381
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001382int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001383{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001384 unsigned long size = flash->chip->total_size * 1024;
hailfinger42a850a2010-07-13 23:56:13 +00001385 unsigned char *buf = calloc(size, sizeof(char));
1386 int ret = 0;
1387
1388 msg_cinfo("Reading flash... ");
1389 if (!buf) {
1390 msg_gerr("Memory allocation failed!\n");
1391 msg_cinfo("FAILED.\n");
1392 return 1;
1393 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001394
1395 /* To support partial read, fill buffer to all 0xFF at beginning to make
1396 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001397 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001398
Patrick Georgif3fa2992017-02-02 16:24:44 +01001399 if (!flash->chip->read) {
hailfinger42a850a2010-07-13 23:56:13 +00001400 msg_cerr("No read function available for this flash chip.\n");
1401 ret = 1;
1402 goto out_free;
1403 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001404
1405 /* First try to handle partial read case, rather than read the whole
1406 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001407 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001408 if (ret < 0) {
1409 msg_cerr("Partial read operation failed!\n");
1410 ret = 1;
1411 goto out_free;
1412 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001413 int num_regions = get_num_include_args();
1414
1415 if (ret != num_regions) {
1416 msg_cerr("Requested %d regions, but only read %d\n",
1417 num_regions, ret);
1418 ret = 1;
1419 goto out_free;
1420 }
1421
1422 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001423 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001424 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001425 msg_cerr("Read operation failed!\n");
1426 ret = 1;
1427 goto out_free;
1428 }
hailfinger42a850a2010-07-13 23:56:13 +00001429 }
1430
David Hendricksdf29a832013-06-28 14:33:51 -07001431 if (filename)
1432 ret = write_buf_to_file(buf, size, filename);
1433
hailfinger42a850a2010-07-13 23:56:13 +00001434out_free:
1435 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001436 if (ret)
1437 msg_cerr("FAILED.");
1438 else
1439 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001440 return ret;
1441}
1442
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001443/* Even if an error is found, the function will keep going and check the rest. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001444static int selfcheck_eraseblocks(const struct flashchip *chip)
hailfinger45177872010-01-18 08:14:43 +00001445{
hailfingerb91c08c2011-08-15 19:54:20 +00001446 int i, j, k;
1447 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001448
1449 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1450 unsigned int done = 0;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001451 struct block_eraser eraser = chip->block_erasers[k];
hailfinger45177872010-01-18 08:14:43 +00001452
1453 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1454 /* Blocks with zero size are bugs in flashchips.c. */
1455 if (eraser.eraseblocks[i].count &&
1456 !eraser.eraseblocks[i].size) {
1457 msg_gerr("ERROR: Flash chip %s erase function "
1458 "%i region %i has size 0. Please report"
1459 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001460 chip->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001461 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001462 }
1463 /* Blocks with zero count are bugs in flashchips.c. */
1464 if (!eraser.eraseblocks[i].count &&
1465 eraser.eraseblocks[i].size) {
1466 msg_gerr("ERROR: Flash chip %s erase function "
1467 "%i region %i has count 0. Please report"
1468 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001469 chip->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001470 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001471 }
1472 done += eraser.eraseblocks[i].count *
1473 eraser.eraseblocks[i].size;
1474 }
hailfinger9fed35d2010-01-19 06:42:46 +00001475 /* Empty eraseblock definition with erase function. */
1476 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001477 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001478 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001479 if (!done)
1480 continue;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001481 if (done != chip->total_size * 1024) {
hailfinger45177872010-01-18 08:14:43 +00001482 msg_gerr("ERROR: Flash chip %s erase function %i "
1483 "region walking resulted in 0x%06x bytes total,"
1484 " expected 0x%06x bytes. Please report a bug at"
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001485 " flashrom@flashrom.org\n", chip->name, k,
1486 done, chip->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001487 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001488 }
hailfinger9fed35d2010-01-19 06:42:46 +00001489 if (!eraser.block_erase)
1490 continue;
1491 /* Check if there are identical erase functions for different
1492 * layouts. That would imply "magic" erase functions. The
1493 * easiest way to check this is with function pointers.
1494 */
uwef6f94d42010-03-13 17:28:29 +00001495 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001496 if (eraser.block_erase ==
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001497 chip->block_erasers[j].block_erase) {
hailfinger9fed35d2010-01-19 06:42:46 +00001498 msg_gerr("ERROR: Flash chip %s erase function "
1499 "%i and %i are identical. Please report"
1500 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001501 chip->name, k, j);
hailfinger9fed35d2010-01-19 06:42:46 +00001502 ret = 1;
1503 }
uwef6f94d42010-03-13 17:28:29 +00001504 }
hailfinger45177872010-01-18 08:14:43 +00001505 }
hailfinger9fed35d2010-01-19 06:42:46 +00001506 return ret;
hailfinger45177872010-01-18 08:14:43 +00001507}
1508
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001509static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001510 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001511 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001512 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001513 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001514 unsigned int addr,
1515 unsigned int len))
1516{
stefanctc5eb8a92011-11-23 09:13:48 +00001517 unsigned int starthere = 0, lenhere = 0;
1518 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001519 int block_was_erased = 0;
Edward O'Callaghan10e63d92019-06-17 14:12:52 +10001520 enum write_granularity gran = flash->chip->gran;
hailfingerb437e282010-11-04 01:04:27 +00001521
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001522 /*
1523 * curcontents and newcontents are opaque to walk_eraseregions, and
1524 * need to be adjusted here to keep the impression of proper
1525 * abstraction
hailfingerb437e282010-11-04 01:04:27 +00001526 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001527
hailfinger90fcf9b2010-11-05 14:51:59 +00001528 curcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001529
hailfingerb437e282010-11-04 01:04:27 +00001530 newcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001531
hailfingerb437e282010-11-04 01:04:27 +00001532 msg_cdbg(":");
Simon Glass4c214132013-07-16 10:09:28 -06001533 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001534 content_has_changed |= 1;
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001535 msg_cdbg(" E");
hailfingerb437e282010-11-04 01:04:27 +00001536 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001537 if (ret) {
1538 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001539 msg_cdbg(" DENIED");
David Hendricks1ed1d352011-11-23 17:54:37 -08001540 else
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001541 msg_cerr(" ERASE_FAILED\n");
hailfingerb437e282010-11-04 01:04:27 +00001542 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001543 }
1544
David Hendricks0954ffc2015-11-13 15:15:44 -08001545 if (programmer_table[programmer].paranoid) {
1546 if (check_erased_range(flash, start, len)) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001547 msg_cerr(" ERASE_FAILED\n");
David Hendricks0954ffc2015-11-13 15:15:44 -08001548 return -1;
1549 }
hailfingerac8e3182011-06-26 17:04:16 +00001550 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001551
hailfinger90fcf9b2010-11-05 14:51:59 +00001552 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001553 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001554 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001555 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001556 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001557 /* get_next_write() sets starthere to a new value after the call. */
1558 while ((lenhere = get_next_write(curcontents + starthere,
1559 newcontents + starthere,
1560 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001561 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001562 if (!writecount++)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001563 msg_cdbg(" W");
hailfingerb437e282010-11-04 01:04:27 +00001564 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001565 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001566 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001567 if (ret) {
1568 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001569 msg_cdbg(" DENIED");
hailfingerb437e282010-11-04 01:04:27 +00001570 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001571 }
David Hendricks048b38c2016-03-28 18:47:06 -07001572
1573 /*
1574 * If the block needed to be erased and was erased successfully
1575 * then we can assume that we didn't run into any write-
1576 * protected areas. Otherwise, we need to verify each page to
1577 * ensure it was successfully written and abort if we encounter
1578 * any errors.
1579 */
1580 if (programmer_table[programmer].paranoid && !block_was_erased) {
1581 if (verify_range(flash, newcontents + starthere,
1582 start + starthere, lenhere, "WRITE"))
1583 return -1;
1584 }
1585
hailfingerb437e282010-11-04 01:04:27 +00001586 starthere += lenhere;
1587 skip = 0;
1588 }
1589 if (skip)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001590 msg_cdbg(" SKIP");
hailfingerb437e282010-11-04 01:04:27 +00001591 return ret;
1592}
1593
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001594/*
1595 * Function to process processing units accumulated in the action descriptor.
1596 *
1597 * @flash pointer to the flash context to operate on
1598 * @do_something helper function which can erase and program a section of the
1599 * flash chip. It receives the flash context, offset and length
1600 * of the area to erase/program, before and after contents (to
1601 * decide what exactly needs to be erased and or programmed)
1602 * and a pointer to the erase function which can operate on the
1603 * proper granularity.
1604 * @descriptor action descriptor including pointers to before and after
1605 * contents and an array of processing actions to take.
1606 *
1607 * Returns zero on success or an error code.
1608 */
1609static int walk_eraseregions(struct flashctx *flash,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001610 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001611 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001612 unsigned int len,
1613 uint8_t *param1,
1614 uint8_t *param2,
1615 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001616 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001617 unsigned int addr,
1618 unsigned int len)),
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001619 struct action_descriptor *descriptor)
hailfinger2b8c9382010-07-13 00:37:19 +00001620{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001621 struct processing_unit *pu;
1622 int rc = 0;
1623 static int print_comma;
uwe8d342eb2011-07-28 08:13:25 +00001624
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001625 for (pu = descriptor->processing_units; pu->num_blocks; pu++) {
1626 unsigned base = pu->offset;
1627 unsigned top = pu->offset + pu->block_size * pu->num_blocks;
David Hendricks605544b2015-08-15 16:32:58 -07001628
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001629 while (base < top) {
David Hendricks605544b2015-08-15 16:32:58 -07001630
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001631 if (print_comma)
hailfingerb437e282010-11-04 01:04:27 +00001632 msg_cdbg(", ");
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001633 else
1634 print_comma = 1;
1635
1636 msg_cdbg("0x%06x-0x%06zx", base, base + pu->block_size - 1);
1637
1638 rc = do_something(flash, base,
1639 pu->block_size,
1640 descriptor->oldcontents,
1641 descriptor->newcontents,
1642 flash->chip->block_erasers[pu->block_eraser_index].block_erase);
1643
David Hendricks1ed1d352011-11-23 17:54:37 -08001644 if (rc) {
1645 if (ignore_error(rc))
1646 rc = 0;
1647 else
1648 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001649 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001650 base += pu->block_size;
hailfinger2b8c9382010-07-13 00:37:19 +00001651 }
1652 }
hailfingerb437e282010-11-04 01:04:27 +00001653 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001654 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001655}
1656
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001657static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001658{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001659 struct block_eraser eraser = flash->chip->block_erasers[k];
hailfingercf848f12010-12-05 15:14:44 +00001660
1661 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1662 if (log)
1663 msg_cdbg("not defined. ");
1664 return 1;
1665 }
1666 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1667 if (log)
1668 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001669 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001670 return 1;
1671 }
1672 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1673 if (log)
1674 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001675 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001676 return 1;
1677 }
1678 return 0;
1679}
1680
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001681int erase_and_write_flash(struct flashctx *flash,
1682 struct action_descriptor *descriptor)
hailfingerd219a232009-01-28 00:27:54 +00001683{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001684 int ret = 1;
hailfingercf848f12010-12-05 15:14:44 +00001685
hailfingercf848f12010-12-05 15:14:44 +00001686 msg_cinfo("Erasing and writing flash chip... ");
hailfingerb437e282010-11-04 01:04:27 +00001687
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001688 ret = walk_eraseregions(flash, &erase_and_write_block_helper, descriptor);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001689
hailfinger7df21362009-09-05 02:30:58 +00001690 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001691 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001692 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001693 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001694 }
1695 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001696}
1697
hailfinger4c47e9d2010-10-19 22:06:20 +00001698void nonfatal_help_message(void)
1699{
1700 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1701 "This means we have to add special support for your board, "
1702 "programmer or flash chip.\n"
1703 "Please report this on IRC at irc.freenode.net (channel "
1704 "#flashrom) or\n"
1705 "mail flashrom@flashrom.org!\n"
1706 "-------------------------------------------------------------"
1707 "------------------\n"
1708 "You may now reboot or simply leave the machine running.\n");
1709}
1710
uweb34ec9f2009-10-01 18:40:02 +00001711void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001712{
snelsone42c3802010-05-07 20:09:04 +00001713 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001714 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001715 "mail flashrom@flashrom.org with FAILED: your board name in "
1716 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001717 "-------------------------------------------------------------"
1718 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001719 "DO NOT REBOOT OR POWEROFF!\n");
1720}
1721
uwe8d342eb2011-07-28 08:13:25 +00001722/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001723void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001724{
1725 enum programmer p;
1726 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001727 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001728 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001729 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001730 }
Simon Glass8dc82732013-07-16 10:13:51 -06001731 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001732}
1733
hailfingerf79d1712010-10-06 23:48:34 +00001734void list_programmers_linebreak(int startcol, int cols, int paren)
1735{
1736 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001737 int pnamelen;
1738 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001739 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001740 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001741
1742 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1743 pname = programmer_table[p].name;
1744 pnamelen = strlen(pname);
1745 if (remaining - pnamelen - 2 < 0) {
1746 if (firstline)
1747 firstline = 0;
1748 else
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001749 msg_ginfo("\n");
hailfingerf79d1712010-10-06 23:48:34 +00001750 for (i = 0; i < startcol; i++)
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001751 msg_ginfo(" ");
hailfingerf79d1712010-10-06 23:48:34 +00001752 remaining = cols - startcol;
1753 } else {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001754 msg_ginfo(" ");
hailfingerf79d1712010-10-06 23:48:34 +00001755 remaining--;
1756 }
1757 if (paren && (p == 0)) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001758 msg_ginfo("(");
hailfingerf79d1712010-10-06 23:48:34 +00001759 remaining--;
1760 }
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001761 msg_ginfo("%s", pname);
hailfingerf79d1712010-10-06 23:48:34 +00001762 remaining -= pnamelen;
1763 if (p < PROGRAMMER_INVALID - 1) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001764 msg_ginfo(",");
hailfingerf79d1712010-10-06 23:48:34 +00001765 remaining--;
1766 } else {
1767 if (paren)
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001768 msg_ginfo(")");
1769 msg_ginfo("\n");
hailfingerf79d1712010-10-06 23:48:34 +00001770 }
1771 }
1772}
1773
hailfinger3b471632010-03-27 16:36:40 +00001774void print_sysinfo(void)
1775{
David Hendricksc6c9f822010-11-03 15:07:01 -07001776 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001777#if HAVE_UTSNAME == 1
1778 struct utsname osinfo;
1779 uname(&osinfo);
1780
David Hendricksc6c9f822010-11-03 15:07:01 -07001781 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001782 osinfo.machine);
1783#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001784 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001785#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001786}
1787
1788void print_buildinfo(void)
1789{
1790 msg_gdbg("flashrom was built with");
hailfinger3b471632010-03-27 16:36:40 +00001791#if NEED_PCI == 1
1792#ifdef PCILIB_VERSION
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001793 msg_gdbg(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001794#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001795 msg_gdbg(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001796#endif
1797#endif
1798#ifdef __clang__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001799 msg_gdbg(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001800#ifdef __clang_version__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001801 msg_gdbg(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001802#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001803 msg_gdbg(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001804#endif
hailfinger3b471632010-03-27 16:36:40 +00001805#elif defined(__GNUC__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001806 msg_gdbg(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001807#ifdef __VERSION__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001808 msg_gdbg(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001809#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001810 msg_gdbg(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001811#endif
1812#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001813 msg_gdbg(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001814#endif
1815#if defined (__FLASHROM_LITTLE_ENDIAN__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001816 msg_gdbg(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001817#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001818 msg_gdbg(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001819#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001820 msg_gdbg("\n");
hailfinger3b471632010-03-27 16:36:40 +00001821}
1822
uwefdeca092008-01-21 15:24:22 +00001823void print_version(void)
1824{
David Hendricksc6c9f822010-11-03 15:07:01 -07001825 /* send to stderr for chromium os */
1826 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001827 print_sysinfo();
David Hendricks07268292016-09-14 16:05:58 -07001828 msg_gerr("\n");
uwefdeca092008-01-21 15:24:22 +00001829}
1830
hailfinger74819ad2010-05-15 15:04:37 +00001831void print_banner(void)
1832{
1833 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001834 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001835 msg_ginfo("\n");
1836}
1837
hailfingerc77acb52009-12-24 02:15:55 +00001838int selfcheck(void)
1839{
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001840 unsigned int i;
hailfinger45177872010-01-18 08:14:43 +00001841 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001842
1843 /* Safety check. Instead of aborting after the first error, check
1844 * if more errors exist.
1845 */
hailfingerc77acb52009-12-24 02:15:55 +00001846 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001847 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001848 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001849 }
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001850 /* It would be favorable if we could check for the correct layout (especially termination) of various
1851 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1852 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1853 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1854 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1855 * checks below.
1856 */
1857 if (flashchips_size <= 1 || flashchips[flashchips_size-1].name != NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001858 msg_gerr("Flashchips table miscompilation!\n");
1859 ret = 1;
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001860 } else {
1861 for (i = 0; i < flashchips_size - 1; i++) {
1862 const struct flashchip *chip = &flashchips[i];
1863 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1864 ret = 1;
1865 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
1866 "Please report a bug at flashrom@flashrom.org\n", i,
1867 chip->name == NULL ? "unnamed" : chip->name);
1868 }
1869 if (selfcheck_eraseblocks(chip))
1870 ret = 1;
1871 }
stefanct6d836ba2011-05-26 01:35:19 +00001872 }
stefanct6d836ba2011-05-26 01:35:19 +00001873
hailfinger45177872010-01-18 08:14:43 +00001874 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001875}
1876
hailfinger771fc182010-10-15 00:01:14 +00001877/* FIXME: This function signature needs to be improved once doit() has a better
1878 * function signature.
1879 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001880int 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 +00001881{
Patrick Georgiac3423f2017-02-03 20:58:06 +01001882 const struct flashchip *chip = flash->chip;
1883
hailfinger771fc182010-10-15 00:01:14 +00001884 if (!programmer_may_write && (write_it || erase_it)) {
1885 msg_perr("Write/erase is not working yet on your programmer in "
1886 "its current configuration.\n");
1887 /* --force is the wrong approach, but it's the best we can do
1888 * until the generic programmer parameter parser is merged.
1889 */
1890 if (!force)
1891 return 1;
1892 msg_cerr("Continuing anyway.\n");
1893 }
1894
1895 if (read_it || erase_it || write_it || verify_it) {
1896 /* Everything needs read. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001897 if (chip->tested.read == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001898 msg_cerr("Read is not working on this chip. ");
1899 if (!force)
1900 return 1;
1901 msg_cerr("Continuing anyway.\n");
1902 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001903 if (!chip->read) {
hailfinger771fc182010-10-15 00:01:14 +00001904 msg_cerr("flashrom has no read function for this "
1905 "flash chip.\n");
1906 return 1;
1907 }
1908 }
1909 if (erase_it || write_it) {
1910 /* Write needs erase. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001911 if (chip->tested.erase == NA) {
1912 msg_cerr("Erase is not possible on this chip.\n");
1913 return 1;
1914 }
1915 if (chip->tested.erase == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001916 msg_cerr("Erase is not working on this chip. ");
1917 if (!force)
1918 return 1;
1919 msg_cerr("Continuing anyway.\n");
1920 }
stefancte1c5acf2011-07-04 07:27:17 +00001921 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001922 msg_cerr("flashrom has no erase function for this "
1923 "flash chip.\n");
1924 return 1;
1925 }
hailfinger771fc182010-10-15 00:01:14 +00001926 }
1927 if (write_it) {
Patrick Georgiac3423f2017-02-03 20:58:06 +01001928 if (chip->tested.write == NA) {
1929 msg_cerr("Write is not possible on this chip.\n");
1930 return 1;
1931 }
1932 if (chip->tested.write == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001933 msg_cerr("Write is not working on this chip. ");
1934 if (!force)
1935 return 1;
1936 msg_cerr("Continuing anyway.\n");
1937 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001938 if (!chip->write) {
hailfinger771fc182010-10-15 00:01:14 +00001939 msg_cerr("flashrom has no write function for this "
1940 "flash chip.\n");
1941 return 1;
1942 }
1943 }
1944 return 0;
1945}
1946
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001947/*
1948 * Function to erase entire flash chip.
1949 *
1950 * @flashctx pointer to the flash context to use
1951 * @oldcontents pointer to the buffer including current chip contents, to
1952 * decide which areas do in fact need to be erased
1953 * @size the size of the flash chip, in bytes.
1954 *
1955 * Returns zero on success or an error code.
1956 */
1957static int erase_chip(struct flashctx *flash, void *oldcontents,
1958 void *newcontents, size_t size)
1959{
1960 /*
1961 * To make sure that the chip is fully erased, let's cheat and create
1962 * a descriptor where the new contents are all erased.
1963 */
1964 struct action_descriptor *fake_descriptor;
1965 int ret = 0;
1966
1967 fake_descriptor = prepare_action_descriptor(flash, oldcontents,
1968 newcontents, 1);
1969 /* FIXME: Do we really want the scary warning if erase failed? After
1970 * all, after erase the chip is either blank or partially blank or it
1971 * has the old contents. A blank chip won't boot, so if the user
1972 * wanted erase and reboots afterwards, the user knows very well that
1973 * booting won't work.
1974 */
1975 if (erase_and_write_flash(flash, fake_descriptor)) {
1976 emergency_help_message();
1977 ret = 1;
1978 }
1979
1980 free(fake_descriptor);
1981
1982 return ret;
1983}
1984
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07001985static int read_dest_content(struct flashctx *flash, int verify_it,
1986 uint8_t *dest, unsigned long size)
1987{
1988 if (((verify_it == VERIFY_OFF) || (verify_it == VERIFY_PARTIAL))
1989 && get_num_include_args()) {
1990 /*
1991 * If no full verification is required and not
1992 * the entire chip is about to be programmed,
1993 * read only the areas which might change.
1994 */
1995 if (handle_partial_read(flash, dest, read_flash, 0) < 0)
1996 return 1;
1997 } else {
1998 if (read_flash(flash, dest, 0, size))
1999 return 1;
2000 }
2001 return 0;
2002}
2003
hailfingerc77acb52009-12-24 02:15:55 +00002004/* This function signature is horrible. We need to design a better interface,
2005 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00002006 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00002007 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002008int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09002009 int write_it, int erase_it, int verify_it, int extract_it,
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002010 const char *diff_file, int do_diff)
hailfingerc77acb52009-12-24 02:15:55 +00002011{
hailfinger4c47e9d2010-10-19 22:06:20 +00002012 uint8_t *oldcontents;
2013 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00002014 int ret = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +01002015 unsigned long size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002016 struct action_descriptor *descriptor = NULL;
hailfingerc77acb52009-12-24 02:15:55 +00002017
stefanct02116582011-05-18 01:30:56 +00002018 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00002019 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00002020 ret = 1;
2021 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00002022 }
2023
hailfinger771fc182010-10-15 00:01:14 +00002024 /* Given the existence of read locks, we want to unlock for read,
2025 * erase and write.
2026 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01002027 if (flash->chip->unlock)
2028 flash->chip->unlock(flash);
hailfinger771fc182010-10-15 00:01:14 +00002029
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +10002030 flash->address_high_byte = -1;
2031 flash->in_4ba_mode = false;
2032
Ed Swierk28cf7992017-07-03 13:17:18 -07002033 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Edward O'Callaghan9713aa62019-07-18 18:28:57 +10002034 if ((flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN) && flash->chip->set_4ba) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +10002035 if (flash->chip->set_4ba(flash)) {
Ed Swierk28cf7992017-07-03 13:17:18 -07002036 msg_cerr("Enabling/disabling 4-byte addressing mode failed!\n");
2037 return 1;
Boris Baykov6323c242016-06-11 18:29:03 +02002038 }
Boris Baykov1a2f5322016-06-11 18:29:00 +02002039 }
2040
Simon Glass9ad06c12013-07-03 22:08:17 +09002041 if (extract_it) {
2042 ret = extract_regions(flash);
2043 goto out_nofree;
2044 }
2045
David Hendricksd0ea9ed2011-03-04 17:31:57 -08002046 /* mark entries included using -i argument as "included" if they are
2047 found in the master rom_entries list */
2048 if (process_include_args() < 0) {
2049 ret = 1;
2050 goto out_nofree;
2051 }
2052
hailfinger771fc182010-10-15 00:01:14 +00002053 if (read_it) {
2054 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00002055 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00002056 }
hailfingerb437e282010-11-04 01:04:27 +00002057
stefanctd611e8f2011-07-12 22:35:21 +00002058 oldcontents = malloc(size);
2059 if (!oldcontents) {
2060 msg_gerr("Out of memory!\n");
2061 exit(1);
2062 }
Simon Glass4c214132013-07-16 10:09:28 -06002063 /* Assume worst case: All blocks are not erased. */
2064 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002065 newcontents = malloc(size);
2066 if (!newcontents) {
2067 msg_gerr("Out of memory!\n");
2068 exit(1);
2069 }
Simon Glass4c214132013-07-16 10:09:28 -06002070 /* Assume best case: All blocks are erased. */
2071 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002072 /* Side effect of the assumptions above: Default write action is erase
2073 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002074 * oldcontents being completely unerased means we have to erase
2075 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002076 */
2077
hailfingerd217d122010-10-08 18:52:29 +00002078 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002079 /*
2080 * Note: This must be done before any files specified by -i
2081 * arguments are processed merged into the newcontents since
2082 * -i files take priority. See http://crbug.com/263495.
2083 */
2084 if (filename) {
2085 if (read_buf_from_file(newcontents, size, filename)) {
2086 ret = 1;
2087 goto out;
2088 }
2089 } else {
2090 /* Content will be read from -i args, so they must
2091 * not overlap. */
2092 if (included_regions_overlap()) {
2093 msg_gerr("Error: Included regions must "
2094 "not overlap.\n");
2095 ret = 1;
2096 goto out;
2097 }
stepan1da96c02006-11-21 23:48:51 +00002098 }
2099
David Hendricksac82cac2012-06-19 10:29:37 -07002100#if 0
2101 /*
2102 * FIXME: show_id() causes failure if vendor:mainboard do not
2103 * match. This may happen if codenames are in flux.
2104 * See chrome-os-partner:10414.
2105 */
hailfinger90c7d542010-05-31 15:27:27 +00002106#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002107 if (programmer == PROGRAMMER_INTERNAL)
2108 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002109#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002110#endif
ollie5672ac62004-03-17 22:22:08 +00002111 }
2112
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002113 if (do_diff) {
2114 /*
2115 * Obtain a reference image so that we can check whether
2116 * regions need to be erased and to give better diagnostics in
2117 * case write fails. If --fast-verify is used then only the
2118 * regions which are included using -i will be read.
2119 */
2120 if (diff_file) {
2121 msg_cdbg("Reading old contents from file... ");
2122 if (read_buf_from_file(oldcontents, size, diff_file)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002123 ret = 1;
2124 msg_cdbg("FAILED.\n");
2125 goto out;
2126 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002127 } else {
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002128 msg_cdbg("Reading old contents from flash chip... ");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002129 ret = read_dest_content(flash, verify_it,
2130 oldcontents, size);
2131 if (ret) {
2132 msg_cdbg("FAILED.\n");
2133 goto out;
David Hendricks52ddff02013-07-23 15:05:14 -07002134 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002135 }
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002136 msg_cdbg("done.\n");
2137 } else if (!erase_it) {
2138 msg_pinfo("No diff performed, considering the chip erased.\n");
2139 memset(oldcontents, flash_erase_value(flash), size);
hailfinger4c47e9d2010-10-19 22:06:20 +00002140 }
David Hendricksac1d25c2016-08-09 17:00:58 -07002141
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002142
David Hendricksdf29a832013-06-28 14:33:51 -07002143 /*
2144 * Note: This must be done after reading the file specified for the
2145 * -w/-v argument, if any, so that files specified using -i end up
2146 * in the "newcontents" buffer before being written.
2147 * See http://crbug.com/263495.
2148 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002149 if (handle_romentries(flash, oldcontents, newcontents, erase_it)) {
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002150 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002151 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002152 goto out;
2153 }
uwef6641642007-05-09 10:17:44 +00002154
David Hendricksa7e114b2016-02-26 18:49:15 -08002155 if (erase_it) {
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002156 erase_chip(flash, oldcontents, newcontents, size);
2157 goto verify;
David Hendricksa7e114b2016-02-26 18:49:15 -08002158 }
2159
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002160 descriptor = prepare_action_descriptor(flash, oldcontents,
2161 newcontents, do_diff);
stuge8ce3a3c2008-04-28 14:47:30 +00002162 if (write_it) {
David Hendricksb64b39a2016-10-11 13:48:06 -07002163 // parse the new fmap and disable soft WP if necessary
David Hendricksac1d25c2016-08-09 17:00:58 -07002164 if ((ret = cros_ec_prepare(newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002165 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002166 goto out;
2167 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002168
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002169 if (erase_and_write_flash(flash, descriptor)) {
hailfingerb437e282010-11-04 01:04:27 +00002170 msg_cerr("Uh oh. Erase/write failed. Checking if "
2171 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002172 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002173 if (!memcmp(oldcontents, newcontents, size)) {
2174 msg_cinfo("Good. It seems nothing was "
2175 "changed.\n");
2176 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002177 ret = 1;
2178 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002179 }
2180 }
hailfingerd217d122010-10-08 18:52:29 +00002181 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002182 ret = 1;
2183 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002184 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002185
David Hendricksac1d25c2016-08-09 17:00:58 -07002186 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002187 if (ret < 0) {
2188 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002189 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002190 emergency_help_message();
2191 ret = 1;
2192 goto out;
2193 } else if (ret > 0) {
2194 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002195 msg_pdbg("CROS_EC needs 2nd pass.\n");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002196 ret = read_dest_content(flash, verify_it,
2197 oldcontents, size);
2198 if (ret) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002199 emergency_help_message();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002200 goto out;
2201 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002202
2203 /* Get a new descriptor. */
2204 free(descriptor);
2205 descriptor = prepare_action_descriptor(flash,
2206 oldcontents,
2207 newcontents,
2208 do_diff);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002209 // write 2nd pass
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002210 if (erase_and_write_flash(flash, descriptor)) {
David Hendricksb907de32014-08-11 16:47:09 -07002211 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002212 emergency_help_message();
2213 ret = 1;
2214 goto out;
2215 }
2216 ret = 0;
2217 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002218
David Hendricksac1d25c2016-08-09 17:00:58 -07002219 if (cros_ec_finish() < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002220 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002221 emergency_help_message();
2222 ret = 1;
2223 goto out;
2224 }
stuge8ce3a3c2008-04-28 14:47:30 +00002225 }
ollie6a600992005-11-26 21:55:36 +00002226
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002227 verify:
hailfinger0459e1c2009-08-19 13:55:34 +00002228 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002229 if ((write_it || erase_it) && !content_has_changed) {
2230 msg_gdbg("Nothing was erased or written, skipping "
2231 "verification\n");
2232 } else {
2233 /* Work around chips which need some time to calm down. */
2234 if (write_it && verify_it != VERIFY_PARTIAL)
2235 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002236
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002237 ret = verify_flash(flash, descriptor, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002238
David Hendricks9ba79fb2015-04-03 12:06:16 -07002239 /* If we tried to write, and verification now fails, we
2240 * might have an emergency situation.
2241 */
2242 if (ret && write_it)
2243 emergency_help_message();
2244 }
hailfinger0459e1c2009-08-19 13:55:34 +00002245 }
ollie6a600992005-11-26 21:55:36 +00002246
hailfinger90fcf9b2010-11-05 14:51:59 +00002247out:
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002248 if (descriptor)
2249 free(descriptor);
2250
hailfinger90fcf9b2010-11-05 14:51:59 +00002251 free(oldcontents);
2252 free(newcontents);
2253out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002254 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002255 /*
Edward O'Callaghan1a3fd132019-06-04 14:18:55 +10002256 * programmer_shutdown() call is moved to cli_classic() in chromium os
David Hendricks668f29d2011-01-27 18:51:45 -08002257 * tree. This is because some operations, such as write protection,
2258 * requires programmer_shutdown() but does not call doit().
2259 */
2260// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002261 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002262}