blob: 78abb321da9e774ba2c9589ccbfe7e546bb5ec8b [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;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000311static struct chip_restore_func_data {
David Hendricksbf36f092010-11-02 23:39:29 -0700312 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;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000315} chip_restore_fn[CHIP_RESTORE_MAXFN];
David Hendricksbf36f092010-11-02 23:39:29 -0700316
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;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000320static struct shutdown_func_data {
David Hendricks93784b42016-08-09 17:00:38 -0700321 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000322 void *data;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000323} shutdown_fn[SHUTDOWN_MAXFN];
hailfinger1ff33dc2010-07-03 11:02:10 +0000324/* 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);
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +1100443 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
uweabe92a52009-05-16 22:36:00 +0000444}
445
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700446void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000447{
Craig Hesling65eb8812019-08-01 09:33:56 -0700448 par_master->chip_writeb(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000449}
450
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700451void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000452{
Craig Hesling65eb8812019-08-01 09:33:56 -0700453 par_master->chip_writew(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000454}
455
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700456void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000457{
Craig Hesling65eb8812019-08-01 09:33:56 -0700458 par_master->chip_writel(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000459}
460
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700461void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000462{
Craig Hesling65eb8812019-08-01 09:33:56 -0700463 par_master->chip_writen(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000464}
465
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700466uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000467{
Craig Hesling65eb8812019-08-01 09:33:56 -0700468 return par_master->chip_readb(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000469}
470
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700471uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000472{
Craig Hesling65eb8812019-08-01 09:33:56 -0700473 return par_master->chip_readw(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000474}
475
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700476uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000477{
Craig Hesling65eb8812019-08-01 09:33:56 -0700478 return par_master->chip_readl(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000479}
480
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700481void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000482{
Craig Hesling65eb8812019-08-01 09:33:56 -0700483 par_master->chip_readn(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000484}
485
hailfingere5829f62009-06-05 17:48:08 +0000486void programmer_delay(int usecs)
487{
488 programmer_table[programmer].delay(usecs);
489}
490
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700491void map_flash_registers(struct flashctx *flash)
stepan15e64bc2007-05-24 08:48:10 +0000492{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100493 size_t size = flash->chip->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000494 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000495 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000496 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000497}
498
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +1100499void unmap_flash(struct flashctx *flash)
500{
501 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
502 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
503 flash->physical_registers = 0;
504 flash->virtual_registers = (chipaddr)ERROR_PTR;
505 }
506
507 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
508 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
509 flash->physical_memory = 0;
510 flash->virtual_memory = (chipaddr)ERROR_PTR;
511 }
512}
513
514int map_flash(struct flashctx *flash)
515{
516 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
517 flash->virtual_memory = (chipaddr)ERROR_PTR;
518 flash->virtual_registers = (chipaddr)ERROR_PTR;
519
520 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
521 * These are used for various probing-related hacks that would not map successfully anyway and should be
522 * removed ASAP. */
523 if (flash->chip->total_size == 0)
524 return 0;
525
526 const chipsize_t size = flash->chip->total_size * 1024;
527 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
528 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
529 if (addr == ERROR_PTR) {
530 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
531 flash->chip->name, PRIxPTR_WIDTH, base);
532 return 1;
533 }
534 flash->physical_memory = base;
535 flash->virtual_memory = (chipaddr)addr;
536
537 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
538 * completely different on some chips and programmers, or not mappable at all.
539 * Ignore these problems for now and always report success. */
540 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
541 base = 0xffffffff - size - 0x400000 + 1;
542 addr = programmer_map_flash_region("flash chip registers", base, size);
543 if (addr == ERROR_PTR) {
544 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
545 flash->chip->name, PRIxPTR_WIDTH, base);
546 return 0;
547 }
548 flash->physical_registers = base;
549 flash->virtual_registers = (chipaddr)addr;
550 }
551 return 0;
552}
553
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700554int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000555{
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700556 chip_readn(flash, buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000557
hailfinger23060112009-05-08 12:49:03 +0000558 return 0;
559}
560
David Hendricksda18f692016-10-21 17:49:49 -0700561/* This is a somewhat hacked function similar in some ways to strtok(). It will
562 * look for needle with a subsequent '=' in haystack, return a copy of needle.
hailfinger6e5a52a2009-11-24 18:27:10 +0000563 */
stefanct52700282011-06-26 17:38:17 +0000564char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000565{
David Hendricksda18f692016-10-21 17:49:49 -0700566 char *param_pos, *opt_pos;
hailfinger1ef766d2010-07-06 09:55:48 +0000567 char *opt = NULL;
568 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000569 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000570
hailfingerf4aaccc2010-04-28 15:22:14 +0000571 needlelen = strlen(needle);
572 if (!needlelen) {
573 msg_gerr("%s: empty needle! Please report a bug at "
574 "flashrom@flashrom.org\n", __func__);
575 return NULL;
576 }
577 /* No programmer parameters given. */
578 if (*haystack == NULL)
579 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000580 param_pos = strstr(*haystack, needle);
581 do {
582 if (!param_pos)
583 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000584 /* Needle followed by '='? */
585 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600586
hailfinger1ef766d2010-07-06 09:55:48 +0000587 /* Beginning of the string? */
588 if (param_pos == *haystack)
589 break;
590 /* After a delimiter? */
591 if (strchr(delim, *(param_pos - 1)))
592 break;
593 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000594 /* Continue searching. */
595 param_pos++;
596 param_pos = strstr(param_pos, needle);
597 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000598
hailfinger6e5a52a2009-11-24 18:27:10 +0000599 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000600 /* Get the string after needle and '='. */
601 opt_pos = param_pos + needlelen + 1;
602 optlen = strcspn(opt_pos, delim);
603 /* Return an empty string if the parameter was empty. */
604 opt = malloc(optlen + 1);
605 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000606 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000607 exit(1);
608 }
hailfinger1ef766d2010-07-06 09:55:48 +0000609 strncpy(opt, opt_pos, optlen);
610 opt[optlen] = '\0';
hailfinger6e5a52a2009-11-24 18:27:10 +0000611 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000612
hailfinger1ef766d2010-07-06 09:55:48 +0000613 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000614}
615
stefanct52700282011-06-26 17:38:17 +0000616char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000617{
618 return extract_param(&programmer_param, param_name, ",");
619}
620
stefancte1c5acf2011-07-04 07:27:17 +0000621/* Returns the number of well-defined erasers for a chip. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700622static unsigned int count_usable_erasers(const struct flashctx *flash)
stefanct569dbb62011-07-01 00:19:12 +0000623{
624 unsigned int usable_erasefunctions = 0;
625 int k;
626 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
627 if (!check_block_eraser(flash, k, 0))
628 usable_erasefunctions++;
629 }
630 return usable_erasefunctions;
631}
632
hailfinger7af83692009-06-15 17:23:36 +0000633/* start is an offset to the base address of the flash chip */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700634int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000635{
636 int ret;
637 uint8_t *cmpbuf = malloc(len);
638
639 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000640 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000641 exit(1);
642 }
Simon Glass4c214132013-07-16 10:09:28 -0600643 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000644 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
645 free(cmpbuf);
646 return ret;
647}
648
Simon Glass4e305f42015-01-08 06:29:04 -0700649static int compare_chunk(uint8_t *readbuf, uint8_t *cmpbuf, unsigned int start,
650 unsigned int len, const char *message)
651{
652 int failcount = 0, i;
653
654 for (i = 0; i < len; i++) {
655 if (cmpbuf[i] != readbuf[i]) {
656 if (!failcount) {
657 msg_cerr("%s FAILED at 0x%08x! "
658 "Expected=0x%02x, Read=0x%02x,",
659 message, start + i,
660 cmpbuf[i], readbuf[i]);
661 }
662 failcount++;
663 }
664 }
665
666 return failcount;
667}
668
uwee15beb92010-08-08 17:01:18 +0000669/*
hailfinger7af3d192009-11-25 17:05:52 +0000670 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000671 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000672 * @start offset to the base address of the flash chip
673 * @len length of the verified area
674 * @message string to print in the "FAILED" message
675 * @return 0 for success, -1 for failure
676 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700677int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000678 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000679{
hailfinger8cb6ece2010-11-16 17:21:58 +0000680 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000681 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000682
683 if (!len)
684 goto out_free;
685
Patrick Georgif3fa2992017-02-02 16:24:44 +0100686 if (!flash->chip->read) {
snelsone42c3802010-05-07 20:09:04 +0000687 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000688 return 1;
689 }
hailfinger7af83692009-06-15 17:23:36 +0000690 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000691 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000692 exit(1);
693 }
694
Patrick Georgif3fa2992017-02-02 16:24:44 +0100695 if (start + len > flash->chip->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000696 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000697 " total_size 0x%x\n", __func__, start, len,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100698 flash->chip->total_size * 1024);
hailfinger7af83692009-06-15 17:23:36 +0000699 ret = -1;
700 goto out_free;
701 }
702 if (!message)
703 message = "VERIFY";
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -0700704 msg_gdbg("%#06x..%#06x ", start, start + len -1);
Simon Glass4e305f42015-01-08 06:29:04 -0700705 if (programmer_table[programmer].paranoid) {
706 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800707
Simon Glass4e305f42015-01-08 06:29:04 -0700708 /* limit chunksize in order to catch errors early */
709 for (i = 0, chunksize = 0; i < len; i += chunksize) {
710 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800711
Patrick Georgif3fa2992017-02-02 16:24:44 +0100712 chunksize = min(flash->chip->page_size, len - i);
713 tmp = flash->chip->read(flash, readbuf + i, start + i, chunksize);
Simon Glass4e305f42015-01-08 06:29:04 -0700714 if (tmp) {
715 ret = tmp;
716 if (ignore_error(tmp))
717 continue;
718 else
719 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800720 }
Simon Glass4e305f42015-01-08 06:29:04 -0700721
Duncan Laurie25a4ca22019-04-25 12:08:52 -0700722 /*
723 * Check write access permission and do not compare chunks
724 * where flashrom does not have write access to the region.
725 */
726 if (flash->chip->check_access) {
727 tmp = flash->chip->check_access(flash, start + i, chunksize, 0);
728 if (tmp && ignore_error(tmp))
729 continue;
730 }
731
Simon Glass4e305f42015-01-08 06:29:04 -0700732 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
733 chunksize, message);
734 if (failcount)
735 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800736 }
Simon Glass4e305f42015-01-08 06:29:04 -0700737 } else {
738 int tmp;
739
740 /* read as much as we can to reduce transaction overhead */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100741 tmp = flash->chip->read(flash, readbuf, start, len);
Simon Glass4e305f42015-01-08 06:29:04 -0700742 if (tmp && !ignore_error(tmp)) {
743 ret = tmp;
744 goto out_free;
745 }
746
747 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000748 }
749
hailfinger5be6c0f2009-07-23 01:42:56 +0000750 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000751 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000752 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000753 ret = -1;
754 }
hailfinger7af83692009-06-15 17:23:36 +0000755
756out_free:
757 free(readbuf);
758 return ret;
759}
760
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100761/* Helper function for need_erase() that focuses on granularities of gran bytes. */
762static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
763 unsigned int gran)
764{
765 unsigned int i, j, limit;
766 for (j = 0; j < len / gran; j++) {
767 limit = min (gran, len - j * gran);
768 /* Are 'have' and 'want' identical? */
769 if (!memcmp(have + j * gran, want + j * gran, limit))
770 continue;
771 /* have needs to be in erased state. */
772 for (i = 0; i < limit; i++)
773 if (have[j * gran + i] != 0xff)
774 return 1;
775 }
776 return 0;
777}
778
uwee15beb92010-08-08 17:01:18 +0000779/*
hailfingerb247c7a2010-03-08 00:42:32 +0000780 * Check if the buffer @have can be programmed to the content of @want without
781 * erasing. This is only possible if all chunks of size @gran are either kept
782 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000783 *
hailfingerb247c7a2010-03-08 00:42:32 +0000784 * The following write granularities (enum @gran) are known:
785 * - 1 bit. Each bit can be cleared individually.
786 * - 1 byte. A byte can be written once. Further writes to an already written
787 * byte cause the contents to be either undefined or to stay unchanged.
788 * - 128 bytes. If less than 128 bytes are written, the rest will be
789 * erased. Each write to a 128-byte region will trigger an automatic erase
790 * before anything is written. Very uncommon behaviour and unsupported by
791 * this function.
792 * - 256 bytes. If less than 256 bytes are written, the contents of the
793 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000794 * Warning: This function assumes that @have and @want point to naturally
795 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000796 *
797 * @have buffer with current content
798 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000799 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000800 * @gran write granularity (enum, not count)
801 * @return 0 if no erase is needed, 1 otherwise
802 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700803static int need_erase(struct flashctx *flash, uint8_t *have, uint8_t *want,
Simon Glass4c214132013-07-16 10:09:28 -0600804 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000805{
hailfingerb91c08c2011-08-15 19:54:20 +0000806 int result = 0;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100807 unsigned int i;
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700808
hailfingerb247c7a2010-03-08 00:42:32 +0000809 switch (gran) {
810 case write_gran_1bit:
811 for (i = 0; i < len; i++)
812 if ((have[i] & want[i]) != want[i]) {
813 result = 1;
814 break;
815 }
816 break;
817 case write_gran_1byte:
818 for (i = 0; i < len; i++)
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100819 if ((have[i] != want[i]) && (have[i] != 0xff)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000820 result = 1;
821 break;
822 }
823 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100824 case write_gran_128bytes:
825 result = need_erase_gran_bytes(have, want, len, 128);
826 break;
hailfingerb247c7a2010-03-08 00:42:32 +0000827 case write_gran_256bytes:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100828 result = need_erase_gran_bytes(have, want, len, 256);
829 break;
830 case write_gran_264bytes:
831 result = need_erase_gran_bytes(have, want, len, 264);
832 break;
833 case write_gran_512bytes:
834 result = need_erase_gran_bytes(have, want, len, 512);
835 break;
836 case write_gran_528bytes:
837 result = need_erase_gran_bytes(have, want, len, 528);
838 break;
839 case write_gran_1024bytes:
840 result = need_erase_gran_bytes(have, want, len, 1024);
841 break;
842 case write_gran_1056bytes:
843 result = need_erase_gran_bytes(have, want, len, 1056);
844 break;
845 case write_gran_1byte_implicit_erase:
846 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
847 result = 0;
hailfingerb247c7a2010-03-08 00:42:32 +0000848 break;
hailfingerb437e282010-11-04 01:04:27 +0000849 default:
850 msg_cerr("%s: Unsupported granularity! Please report a bug at "
851 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000852 }
853 return result;
854}
855
hailfingerb437e282010-11-04 01:04:27 +0000856/**
857 * Check if the buffer @have needs to be programmed to get the content of @want.
858 * If yes, return 1 and fill in first_start with the start address of the
859 * write operation and first_len with the length of the first to-be-written
860 * chunk. If not, return 0 and leave first_start and first_len undefined.
861 *
862 * Warning: This function assumes that @have and @want point to naturally
863 * aligned regions.
864 *
865 * @have buffer with current content
866 * @want buffer with desired content
867 * @len length of the checked area
868 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000869 * @first_start offset of the first byte which needs to be written (passed in
870 * value is increased by the offset of the first needed write
871 * relative to have/want or unchanged if no write is needed)
872 * @return length of the first contiguous area which needs to be written
873 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000874 *
875 * FIXME: This function needs a parameter which tells it about coalescing
876 * in relation to the max write length of the programmer and the max write
877 * length of the chip.
878 */
stefanctc5eb8a92011-11-23 09:13:48 +0000879static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
880 unsigned int *first_start,
881 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000882{
stefanctc5eb8a92011-11-23 09:13:48 +0000883 int need_write = 0;
884 unsigned int rel_start = 0, first_len = 0;
885 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000886
hailfingerb437e282010-11-04 01:04:27 +0000887 switch (gran) {
888 case write_gran_1bit:
889 case write_gran_1byte:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100890 case write_gran_1byte_implicit_erase:
hailfinger90fcf9b2010-11-05 14:51:59 +0000891 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000892 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100893 case write_gran_128bytes:
894 stride = 128;
895 break;
hailfingerb437e282010-11-04 01:04:27 +0000896 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000897 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000898 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100899 case write_gran_264bytes:
900 stride = 264;
901 break;
902 case write_gran_512bytes:
903 stride = 512;
904 break;
905 case write_gran_528bytes:
906 stride = 528;
907 break;
908 case write_gran_1024bytes:
909 stride = 1024;
910 break;
911 case write_gran_1056bytes:
912 stride = 1056;
913 break;
hailfingerb437e282010-11-04 01:04:27 +0000914 default:
915 msg_cerr("%s: Unsupported granularity! Please report a bug at "
916 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000917 /* Claim that no write was needed. A write with unknown
918 * granularity is too dangerous to try.
919 */
920 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000921 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000922 for (i = 0; i < len / stride; i++) {
923 limit = min(stride, len - i * stride);
924 /* Are 'have' and 'want' identical? */
925 if (memcmp(have + i * stride, want + i * stride, limit)) {
926 if (!need_write) {
927 /* First location where have and want differ. */
928 need_write = 1;
929 rel_start = i * stride;
930 }
931 } else {
932 if (need_write) {
933 /* First location where have and want
934 * do not differ anymore.
935 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000936 break;
937 }
938 }
939 }
hailfingerffb7f382010-12-06 13:05:44 +0000940 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000941 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000942 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000943 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000944}
945
hailfinger0c515352009-11-23 12:55:31 +0000946/* This function generates various test patterns useful for testing controller
947 * and chip communication as well as chip behaviour.
948 *
949 * If a byte can be written multiple times, each time keeping 0-bits at 0
950 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
951 * is essentially an AND operation. That's also the reason why this function
952 * provides the result of AND between various patterns.
953 *
954 * Below is a list of patterns (and their block length).
955 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
956 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
957 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
958 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
959 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
960 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
961 * Pattern 6 is 00 (1 Byte)
962 * Pattern 7 is ff (1 Byte)
963 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
964 * byte block.
965 *
966 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
967 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
968 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
969 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
970 * Pattern 12 is 00 (1 Byte)
971 * Pattern 13 is ff (1 Byte)
972 * Patterns 8-13 have no block number.
973 *
974 * Patterns 0-3 are created to detect and efficiently diagnose communication
975 * slips like missed bits or bytes and their repetitive nature gives good visual
976 * cues to the person inspecting the results. In addition, the following holds:
977 * AND Pattern 0/1 == Pattern 4
978 * AND Pattern 2/3 == Pattern 5
979 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
980 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
981 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
982 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
983 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
984 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
985 * Besides that, they provide for bit testing of the last two bytes of every
986 * 256 byte block which contains the block number for patterns 0-6.
987 * Patterns 10-11 are special purpose for detecting subblock aliasing with
988 * block sizes >256 bytes (some Dataflash chips etc.)
989 * AND Pattern 8/9 == Pattern 12
990 * AND Pattern 10/11 == Pattern 12
991 * Pattern 13 is the completely erased state.
992 * None of the patterns can detect aliasing at boundaries which are a multiple
993 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
994 */
995int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
996{
997 int i;
998
999 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +00001000 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +00001001 return 1;
1002 }
1003
1004 switch (variant) {
1005 case 0:
1006 for (i = 0; i < size; i++)
1007 buf[i] = (i & 0xf) << 4 | 0x5;
1008 break;
1009 case 1:
1010 for (i = 0; i < size; i++)
1011 buf[i] = (i & 0xf) << 4 | 0xa;
1012 break;
1013 case 2:
1014 for (i = 0; i < size; i++)
1015 buf[i] = 0x50 | (i & 0xf);
1016 break;
1017 case 3:
1018 for (i = 0; i < size; i++)
1019 buf[i] = 0xa0 | (i & 0xf);
1020 break;
1021 case 4:
1022 for (i = 0; i < size; i++)
1023 buf[i] = (i & 0xf) << 4;
1024 break;
1025 case 5:
1026 for (i = 0; i < size; i++)
1027 buf[i] = i & 0xf;
1028 break;
1029 case 6:
1030 memset(buf, 0x00, size);
1031 break;
1032 case 7:
1033 memset(buf, 0xff, size);
1034 break;
1035 case 8:
1036 for (i = 0; i < size; i++)
1037 buf[i] = i & 0xff;
1038 break;
1039 case 9:
1040 for (i = 0; i < size; i++)
1041 buf[i] = ~(i & 0xff);
1042 break;
1043 case 10:
1044 for (i = 0; i < size % 2; i++) {
1045 buf[i * 2] = (i >> 8) & 0xff;
1046 buf[i * 2 + 1] = i & 0xff;
1047 }
1048 if (size & 0x1)
1049 buf[i * 2] = (i >> 8) & 0xff;
1050 break;
1051 case 11:
1052 for (i = 0; i < size % 2; i++) {
1053 buf[i * 2] = ~((i >> 8) & 0xff);
1054 buf[i * 2 + 1] = ~(i & 0xff);
1055 }
1056 if (size & 0x1)
1057 buf[i * 2] = ~((i >> 8) & 0xff);
1058 break;
1059 case 12:
1060 memset(buf, 0x00, size);
1061 break;
1062 case 13:
1063 memset(buf, 0xff, size);
1064 break;
1065 }
1066
1067 if ((variant >= 0) && (variant <= 7)) {
1068 /* Write block number in the last two bytes of each 256-byte
1069 * block, big endian for easier reading of the hexdump.
1070 * Note that this wraps around for chips larger than 2^24 bytes
1071 * (16 MB).
1072 */
1073 for (i = 0; i < size / 256; i++) {
1074 buf[i * 256 + 254] = (i >> 8) & 0xff;
1075 buf[i * 256 + 255] = i & 0xff;
1076 }
1077 }
1078
1079 return 0;
1080}
1081
hailfingeraec9c962009-10-31 01:53:09 +00001082int check_max_decode(enum chipbustype buses, uint32_t size)
1083{
1084 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001085
1086 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001087 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001088 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001089 "size %u kB of chipset/board/programmer "
1090 "for %s interface, "
1091 "probe/read/erase/write may fail. ", size / 1024,
1092 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001093 }
hailfingere1e41ea2011-07-27 07:13:06 +00001094 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001095 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001096 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001097 "size %u kB of chipset/board/programmer "
1098 "for %s interface, "
1099 "probe/read/erase/write may fail. ", size / 1024,
1100 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001101 }
hailfingere1e41ea2011-07-27 07:13:06 +00001102 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001103 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001104 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001105 "size %u kB of chipset/board/programmer "
1106 "for %s interface, "
1107 "probe/read/erase/write may fail. ", size / 1024,
1108 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001109 }
hailfingere1e41ea2011-07-27 07:13:06 +00001110 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001111 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001112 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001113 "size %u kB of chipset/board/programmer "
1114 "for %s interface, "
1115 "probe/read/erase/write may fail. ", size / 1024,
1116 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001117 }
1118 if (!limitexceeded)
1119 return 0;
1120 /* Sometimes chip and programmer have more than one bus in common,
1121 * and the limit is not exceeded on all buses. Tell the user.
1122 */
1123 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001124 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001125 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001126 "interface which can support a chip of this size. "
1127 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001128 return 1;
1129}
1130
Edward O'Callaghan8488f122019-06-17 12:38:15 +10001131/*
1132 * Return a string corresponding to the bustype parameter.
1133 * Memory is obtained with malloc() and must be freed with free() by the caller.
1134 */
1135char *flashbuses_to_text(enum chipbustype bustype)
1136{
1137 char *ret = calloc(1, 1);
1138 /*
1139 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
1140 * will cease to exist and should be eliminated here as well.
1141 */
1142 if (bustype == BUS_NONSPI) {
1143 ret = strcat_realloc(ret, "Non-SPI, ");
1144 } else {
1145 if (bustype & BUS_PARALLEL)
1146 ret = strcat_realloc(ret, "Parallel, ");
1147 if (bustype & BUS_LPC)
1148 ret = strcat_realloc(ret, "LPC, ");
1149 if (bustype & BUS_FWH)
1150 ret = strcat_realloc(ret, "FWH, ");
1151 if (bustype & BUS_SPI)
1152 ret = strcat_realloc(ret, "SPI, ");
1153 if (bustype & BUS_PROG)
1154 ret = strcat_realloc(ret, "Programmer-specific, ");
1155 if (bustype == BUS_NONE)
1156 ret = strcat_realloc(ret, "None, ");
1157 }
1158 /* Kill last comma. */
1159 ret[strlen(ret) - 2] = '\0';
1160 ret = realloc(ret, strlen(ret) + 1);
1161 return ret;
1162}
1163
Edward O'Callaghan20596a82019-06-13 14:47:03 +10001164int probe_flash(struct registered_master *mst, int startchip,
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001165 struct flashctx *flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001166{
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001167 const struct flashchip *chip, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001168 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001169 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001170 uint32_t size;
1171 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001172 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001173
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301174 /* Based on the host controller interface that a platform
1175 * needs to use (hwseq or swseq),
1176 * set the flashchips list here.
1177 */
Edward O'Callaghane3e30562019-09-03 13:10:58 +10001178 switch (g_ich_generation) {
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301179 case CHIPSET_100_SERIES_SUNRISE_POINT:
Furquan Shaikh44088752016-07-11 22:48:08 -07001180 case CHIPSET_APL:
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301181 flash_list = flashchips_hwseq;
1182 break;
1183 default:
1184 flash_list = flashchips;
1185 break;
1186 }
1187
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001188 for (chip = flash_list + startchip; chip && chip->name; chip++) {
1189 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001190 continue;
Craig Hesling65eb8812019-08-01 09:33:56 -07001191 buses_common = buses_supported & chip->bustype;
Edward O'Callaghan4b940572019-08-02 01:44:47 +10001192 if (!buses_common)
hailfinger18bd4cc2011-06-17 22:38:53 +00001193 continue;
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001194 /* Only probe for SPI25 chips by default. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001195 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001196 continue;
hailfinger18bd4cc2011-06-17 22:38:53 +00001197 msg_gdbg("Probing for %s %s, %d kB: ",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001198 chip->vendor, chip->name, chip->total_size);
1199 if (!chip->probe && !force) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001200 msg_gdbg("failed! flashrom has no probe function for "
1201 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001202 continue;
1203 }
stepan782fb172007-04-06 11:58:03 +00001204
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001205 size = chip->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001206 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001207
hailfinger48ed3e22011-05-04 00:39:50 +00001208 /* Start filling in the dynamic data. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001209 flash->chip = calloc(1, sizeof(struct flashchip));
1210 if (!flash->chip) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001211 msg_gerr("Out of memory!\n");
1212 exit(1);
1213 }
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001214 memcpy(flash->chip, chip, sizeof(struct flashchip));
1215 flash->mst = mst;
hailfinger48ed3e22011-05-04 00:39:50 +00001216
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +11001217 if (map_flash(flash) != 0)
1218 goto notfound;
rminnich8d3ff912003-10-25 17:01:29 +00001219
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +11001220 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
1221 * is only called with force=1 after normal probing failed.
1222 */
stugec1e55fe2008-07-02 17:15:47 +00001223 if (force)
1224 break;
stepanc98b80b2006-03-16 16:57:41 +00001225
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001226 if (flash->chip->probe(flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001227 goto notfound;
1228
hailfinger48ed3e22011-05-04 00:39:50 +00001229 /* If this is the first chip found, accept it.
1230 * If this is not the first chip found, accept it only if it is
1231 * a non-generic match.
1232 * We could either make chipcount global or provide it as
1233 * parameter, or we assume that startchip==0 means this call to
1234 * probe_flash() is the first one and thus no chip has been
1235 * found before.
1236 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001237 if (startchip == 0 || flash->chip->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001238 break;
1239
stuge56300c32008-09-03 23:10:05 +00001240notfound:
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +11001241 unmap_flash(flash);
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001242 free(flash->chip);
1243 flash->chip = NULL;
rminnich8d3ff912003-10-25 17:01:29 +00001244 }
uwebe4477b2007-08-23 16:08:21 +00001245
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001246 if (!chip || !chip->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001247 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001248
hailfingere11396b2011-03-08 00:09:11 +00001249#if CONFIG_INTERNAL == 1
1250 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001251 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001252 else
1253#endif
stepan3e7aeae2011-01-19 06:21:54 +00001254 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001255
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001256 tmp = flashbuses_to_text(chip->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001257 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001258 force ? "Assuming" : "Found", flash->chip->vendor,
1259 flash->chip->name, flash->chip->total_size, tmp,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001260 location);
stefanct588b6d22011-06-26 20:45:35 +00001261 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001262
hailfinger0f4c3952010-12-02 21:59:42 +00001263 /* Flash registers will not be mapped if the chip was forced. Lock info
1264 * may be stored in registers, so avoid lock info printing.
1265 */
1266 if (!force)
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001267 if (flash->chip->printlock)
1268 flash->chip->printlock(flash);
snelson1ee293c2010-02-19 00:52:10 +00001269
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +11001270 /* Get out of the way for later runs. */
1271 unmap_flash(flash);
1272
hailfinger48ed3e22011-05-04 00:39:50 +00001273 /* Return position of matching chip. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001274 return chip - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001275}
1276
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001277static int verify_flash(struct flashctx *flash,
1278 struct action_descriptor *descriptor,
1279 int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001280{
hailfingerb0f4d122009-06-24 08:20:45 +00001281 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +01001282 unsigned int total_size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001283 uint8_t *buf = descriptor->newcontents;
rminnich8d3ff912003-10-25 17:01:29 +00001284
snelsone42c3802010-05-07 20:09:04 +00001285 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001286
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001287 if (verify_it == VERIFY_PARTIAL) {
1288 struct processing_unit *pu = descriptor->processing_units;
1289
1290 /* Verify only areas which were written. */
1291 while (pu->num_blocks) {
1292 ret = verify_range(flash, buf + pu->offset, pu->offset,
1293 pu->block_size * pu->num_blocks,
1294 NULL);
1295 if (ret)
1296 break;
1297 pu++;
1298 }
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001299 } else {
1300 ret = verify_range(flash, buf, 0, total_size, NULL);
1301 }
uwef6641642007-05-09 10:17:44 +00001302
David Hendricks1ed1d352011-11-23 17:54:37 -08001303 if (ret == ACCESS_DENIED) {
1304 msg_gdbg("Could not fully verify due to access error, ");
1305 if (access_denied_action == error_ignore) {
1306 msg_gdbg("ignoring\n");
1307 ret = 0;
1308 } else {
1309 msg_gdbg("aborting\n");
1310 }
1311 }
1312
hailfingerb0f4d122009-06-24 08:20:45 +00001313 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001314 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001315
hailfingerb0f4d122009-06-24 08:20:45 +00001316 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001317}
1318
uwe8d342eb2011-07-28 08:13:25 +00001319int read_buf_from_file(unsigned char *buf, unsigned long size,
1320 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001321{
1322 unsigned long numbytes;
1323 FILE *image;
1324 struct stat image_stat;
1325
Vincent Palatin7ab23932014-10-01 12:09:16 -07001326 if (!strncmp(filename, "-", sizeof("-")))
1327 image = fdopen(STDIN_FILENO, "rb");
1328 else
1329 image = fopen(filename, "rb");
1330 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001331 perror(filename);
1332 return 1;
1333 }
1334 if (fstat(fileno(image), &image_stat) != 0) {
1335 perror(filename);
1336 fclose(image);
1337 return 1;
1338 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001339 if ((image_stat.st_size != size) &&
1340 (strncmp(filename, "-", sizeof("-")))) {
Mike Frysinger62c794d2017-05-29 12:02:45 -04001341 msg_gerr("Error: Image size doesn't match: stat %jd bytes, "
1342 "wanted %ld!\n", (intmax_t)image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001343 fclose(image);
1344 return 1;
1345 }
1346 numbytes = fread(buf, 1, size, image);
1347 if (fclose(image)) {
1348 perror(filename);
1349 return 1;
1350 }
1351 if (numbytes != size) {
1352 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1353 "wanted %ld!\n", numbytes, size);
1354 return 1;
1355 }
1356 return 0;
1357}
1358
uwe8d342eb2011-07-28 08:13:25 +00001359int write_buf_to_file(unsigned char *buf, unsigned long size,
1360 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001361{
1362 unsigned long numbytes;
1363 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001364
1365 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001366 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001367 return 1;
1368 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001369 if (!strncmp(filename, "-", sizeof("-")))
1370 image = fdopen(STDOUT_FILENO, "wb");
1371 else
1372 image = fopen(filename, "wb");
1373 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001374 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001375 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001376 }
hailfingerd219a232009-01-28 00:27:54 +00001377
hailfingerd219a232009-01-28 00:27:54 +00001378 numbytes = fwrite(buf, 1, size, image);
1379 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001380 if (numbytes != size) {
1381 msg_gerr("File %s could not be written completely.\n",
1382 filename);
hailfingerd219a232009-01-28 00:27:54 +00001383 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001384 }
hailfingerd219a232009-01-28 00:27:54 +00001385 return 0;
1386}
1387
David Hendrickse3451942013-03-21 17:23:29 -07001388/*
1389 * read_flash - wrapper for flash->read() with additional high-level policy
1390 *
1391 * @flash flash chip
1392 * @buf buffer to store data in
1393 * @start start address
1394 * @len number of bytes to read
1395 *
1396 * This wrapper simplifies most cases when the flash chip needs to be read
1397 * since policy decisions such as non-fatal error handling is centralized.
1398 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001399int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001400 unsigned int start, unsigned int len)
1401{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001402 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001403
Patrick Georgif3fa2992017-02-02 16:24:44 +01001404 if (!flash || !flash->chip->read)
David Hendrickse3451942013-03-21 17:23:29 -07001405 return -1;
1406
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001407 msg_cdbg("%#06x-%#06x:R ", start, start + len - 1);
1408
Patrick Georgif3fa2992017-02-02 16:24:44 +01001409 ret = flash->chip->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001410 if (ret) {
1411 if (ignore_error(ret)) {
1412 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1413 start, start + len - 1);
1414 ret = 0;
1415 } else {
1416 msg_gdbg("failed to read 0x%x-0x%x\n",
1417 start, start + len - 1);
1418 }
1419 }
1420
1421 return ret;
1422}
1423
David Hendricks7c8a1612013-04-26 19:14:44 -07001424/*
1425 * write_flash - wrapper for flash->write() with additional high-level policy
1426 *
1427 * @flash flash chip
1428 * @buf buffer to write to flash
1429 * @start start address in flash
1430 * @len number of bytes to write
1431 *
1432 * TODO: Look up regions that are write-protected and avoid attempt to write
1433 * to them at all.
1434 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001435int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001436 unsigned int start, unsigned int len)
1437{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001438 if (!flash || !flash->chip->write)
David Hendricks7c8a1612013-04-26 19:14:44 -07001439 return -1;
1440
Patrick Georgif3fa2992017-02-02 16:24:44 +01001441 return flash->chip->write(flash, buf, start, len);
David Hendricks7c8a1612013-04-26 19:14:44 -07001442}
1443
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001444int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001445{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001446 unsigned long size = flash->chip->total_size * 1024;
Richard Hughes74eec602018-12-19 15:30:39 +00001447 unsigned char *buf = calloc(size, sizeof(unsigned char));
hailfinger42a850a2010-07-13 23:56:13 +00001448 int ret = 0;
1449
1450 msg_cinfo("Reading flash... ");
1451 if (!buf) {
1452 msg_gerr("Memory allocation failed!\n");
1453 msg_cinfo("FAILED.\n");
1454 return 1;
1455 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001456
1457 /* To support partial read, fill buffer to all 0xFF at beginning to make
1458 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001459 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001460
Patrick Georgif3fa2992017-02-02 16:24:44 +01001461 if (!flash->chip->read) {
hailfinger42a850a2010-07-13 23:56:13 +00001462 msg_cerr("No read function available for this flash chip.\n");
1463 ret = 1;
1464 goto out_free;
1465 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001466
1467 /* First try to handle partial read case, rather than read the whole
1468 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001469 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001470 if (ret < 0) {
1471 msg_cerr("Partial read operation failed!\n");
1472 ret = 1;
1473 goto out_free;
1474 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001475 int num_regions = get_num_include_args();
1476
1477 if (ret != num_regions) {
1478 msg_cerr("Requested %d regions, but only read %d\n",
1479 num_regions, ret);
1480 ret = 1;
1481 goto out_free;
1482 }
1483
1484 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001485 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001486 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001487 msg_cerr("Read operation failed!\n");
1488 ret = 1;
1489 goto out_free;
1490 }
hailfinger42a850a2010-07-13 23:56:13 +00001491 }
1492
David Hendricksdf29a832013-06-28 14:33:51 -07001493 if (filename)
1494 ret = write_buf_to_file(buf, size, filename);
1495
hailfinger42a850a2010-07-13 23:56:13 +00001496out_free:
1497 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001498 if (ret)
1499 msg_cerr("FAILED.");
1500 else
1501 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001502 return ret;
1503}
1504
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001505/* Even if an error is found, the function will keep going and check the rest. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001506static int selfcheck_eraseblocks(const struct flashchip *chip)
hailfinger45177872010-01-18 08:14:43 +00001507{
hailfingerb91c08c2011-08-15 19:54:20 +00001508 int i, j, k;
1509 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001510
1511 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1512 unsigned int done = 0;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001513 struct block_eraser eraser = chip->block_erasers[k];
hailfinger45177872010-01-18 08:14:43 +00001514
1515 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1516 /* Blocks with zero size are bugs in flashchips.c. */
1517 if (eraser.eraseblocks[i].count &&
1518 !eraser.eraseblocks[i].size) {
1519 msg_gerr("ERROR: Flash chip %s erase function "
1520 "%i region %i has size 0. Please report"
1521 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001522 chip->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001523 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001524 }
1525 /* Blocks with zero count are bugs in flashchips.c. */
1526 if (!eraser.eraseblocks[i].count &&
1527 eraser.eraseblocks[i].size) {
1528 msg_gerr("ERROR: Flash chip %s erase function "
1529 "%i region %i has count 0. Please report"
1530 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001531 chip->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001532 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001533 }
1534 done += eraser.eraseblocks[i].count *
1535 eraser.eraseblocks[i].size;
1536 }
hailfinger9fed35d2010-01-19 06:42:46 +00001537 /* Empty eraseblock definition with erase function. */
1538 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001539 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001540 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001541 if (!done)
1542 continue;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001543 if (done != chip->total_size * 1024) {
hailfinger45177872010-01-18 08:14:43 +00001544 msg_gerr("ERROR: Flash chip %s erase function %i "
1545 "region walking resulted in 0x%06x bytes total,"
1546 " expected 0x%06x bytes. Please report a bug at"
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001547 " flashrom@flashrom.org\n", chip->name, k,
1548 done, chip->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001549 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001550 }
hailfinger9fed35d2010-01-19 06:42:46 +00001551 if (!eraser.block_erase)
1552 continue;
1553 /* Check if there are identical erase functions for different
1554 * layouts. That would imply "magic" erase functions. The
1555 * easiest way to check this is with function pointers.
1556 */
uwef6f94d42010-03-13 17:28:29 +00001557 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001558 if (eraser.block_erase ==
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001559 chip->block_erasers[j].block_erase) {
hailfinger9fed35d2010-01-19 06:42:46 +00001560 msg_gerr("ERROR: Flash chip %s erase function "
1561 "%i and %i are identical. Please report"
1562 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001563 chip->name, k, j);
hailfinger9fed35d2010-01-19 06:42:46 +00001564 ret = 1;
1565 }
uwef6f94d42010-03-13 17:28:29 +00001566 }
hailfinger45177872010-01-18 08:14:43 +00001567 }
hailfinger9fed35d2010-01-19 06:42:46 +00001568 return ret;
hailfinger45177872010-01-18 08:14:43 +00001569}
1570
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001571static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001572 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001573 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001574 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001575 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001576 unsigned int addr,
1577 unsigned int len))
1578{
stefanctc5eb8a92011-11-23 09:13:48 +00001579 unsigned int starthere = 0, lenhere = 0;
1580 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001581 int block_was_erased = 0;
Edward O'Callaghan10e63d92019-06-17 14:12:52 +10001582 enum write_granularity gran = flash->chip->gran;
hailfingerb437e282010-11-04 01:04:27 +00001583
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001584 /*
1585 * curcontents and newcontents are opaque to walk_eraseregions, and
1586 * need to be adjusted here to keep the impression of proper
1587 * abstraction
hailfingerb437e282010-11-04 01:04:27 +00001588 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001589
hailfinger90fcf9b2010-11-05 14:51:59 +00001590 curcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001591
hailfingerb437e282010-11-04 01:04:27 +00001592 newcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001593
hailfingerb437e282010-11-04 01:04:27 +00001594 msg_cdbg(":");
Simon Glass4c214132013-07-16 10:09:28 -06001595 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001596 content_has_changed |= 1;
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001597 msg_cdbg(" E");
hailfingerb437e282010-11-04 01:04:27 +00001598 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001599 if (ret) {
1600 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001601 msg_cdbg(" DENIED");
David Hendricks1ed1d352011-11-23 17:54:37 -08001602 else
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001603 msg_cerr(" ERASE_FAILED\n");
hailfingerb437e282010-11-04 01:04:27 +00001604 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001605 }
1606
David Hendricks0954ffc2015-11-13 15:15:44 -08001607 if (programmer_table[programmer].paranoid) {
1608 if (check_erased_range(flash, start, len)) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001609 msg_cerr(" ERASE_FAILED\n");
David Hendricks0954ffc2015-11-13 15:15:44 -08001610 return -1;
1611 }
hailfingerac8e3182011-06-26 17:04:16 +00001612 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001613
hailfinger90fcf9b2010-11-05 14:51:59 +00001614 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001615 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001616 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001617 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001618 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001619 /* get_next_write() sets starthere to a new value after the call. */
1620 while ((lenhere = get_next_write(curcontents + starthere,
1621 newcontents + starthere,
1622 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001623 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001624 if (!writecount++)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001625 msg_cdbg(" W");
hailfingerb437e282010-11-04 01:04:27 +00001626 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001627 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001628 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001629 if (ret) {
1630 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001631 msg_cdbg(" DENIED");
hailfingerb437e282010-11-04 01:04:27 +00001632 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001633 }
David Hendricks048b38c2016-03-28 18:47:06 -07001634
1635 /*
1636 * If the block needed to be erased and was erased successfully
1637 * then we can assume that we didn't run into any write-
1638 * protected areas. Otherwise, we need to verify each page to
1639 * ensure it was successfully written and abort if we encounter
1640 * any errors.
1641 */
1642 if (programmer_table[programmer].paranoid && !block_was_erased) {
1643 if (verify_range(flash, newcontents + starthere,
1644 start + starthere, lenhere, "WRITE"))
1645 return -1;
1646 }
1647
hailfingerb437e282010-11-04 01:04:27 +00001648 starthere += lenhere;
1649 skip = 0;
1650 }
1651 if (skip)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001652 msg_cdbg(" SKIP");
hailfingerb437e282010-11-04 01:04:27 +00001653 return ret;
1654}
1655
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001656/*
1657 * Function to process processing units accumulated in the action descriptor.
1658 *
1659 * @flash pointer to the flash context to operate on
1660 * @do_something helper function which can erase and program a section of the
1661 * flash chip. It receives the flash context, offset and length
1662 * of the area to erase/program, before and after contents (to
1663 * decide what exactly needs to be erased and or programmed)
1664 * and a pointer to the erase function which can operate on the
1665 * proper granularity.
1666 * @descriptor action descriptor including pointers to before and after
1667 * contents and an array of processing actions to take.
1668 *
1669 * Returns zero on success or an error code.
1670 */
1671static int walk_eraseregions(struct flashctx *flash,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001672 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001673 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001674 unsigned int len,
1675 uint8_t *param1,
1676 uint8_t *param2,
1677 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001678 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001679 unsigned int addr,
1680 unsigned int len)),
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001681 struct action_descriptor *descriptor)
hailfinger2b8c9382010-07-13 00:37:19 +00001682{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001683 struct processing_unit *pu;
1684 int rc = 0;
1685 static int print_comma;
uwe8d342eb2011-07-28 08:13:25 +00001686
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001687 for (pu = descriptor->processing_units; pu->num_blocks; pu++) {
1688 unsigned base = pu->offset;
1689 unsigned top = pu->offset + pu->block_size * pu->num_blocks;
David Hendricks605544b2015-08-15 16:32:58 -07001690
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001691 while (base < top) {
David Hendricks605544b2015-08-15 16:32:58 -07001692
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001693 if (print_comma)
hailfingerb437e282010-11-04 01:04:27 +00001694 msg_cdbg(", ");
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001695 else
1696 print_comma = 1;
1697
1698 msg_cdbg("0x%06x-0x%06zx", base, base + pu->block_size - 1);
1699
1700 rc = do_something(flash, base,
1701 pu->block_size,
1702 descriptor->oldcontents,
1703 descriptor->newcontents,
1704 flash->chip->block_erasers[pu->block_eraser_index].block_erase);
1705
David Hendricks1ed1d352011-11-23 17:54:37 -08001706 if (rc) {
1707 if (ignore_error(rc))
1708 rc = 0;
1709 else
1710 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001711 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001712 base += pu->block_size;
hailfinger2b8c9382010-07-13 00:37:19 +00001713 }
1714 }
hailfingerb437e282010-11-04 01:04:27 +00001715 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001716 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001717}
1718
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001719static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001720{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001721 struct block_eraser eraser = flash->chip->block_erasers[k];
hailfingercf848f12010-12-05 15:14:44 +00001722
1723 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1724 if (log)
1725 msg_cdbg("not defined. ");
1726 return 1;
1727 }
1728 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1729 if (log)
1730 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001731 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001732 return 1;
1733 }
1734 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1735 if (log)
1736 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001737 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001738 return 1;
1739 }
1740 return 0;
1741}
1742
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001743int erase_and_write_flash(struct flashctx *flash,
1744 struct action_descriptor *descriptor)
hailfingerd219a232009-01-28 00:27:54 +00001745{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001746 int ret = 1;
hailfingercf848f12010-12-05 15:14:44 +00001747
hailfingercf848f12010-12-05 15:14:44 +00001748 msg_cinfo("Erasing and writing flash chip... ");
hailfingerb437e282010-11-04 01:04:27 +00001749
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001750 ret = walk_eraseregions(flash, &erase_and_write_block_helper, descriptor);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001751
hailfinger7df21362009-09-05 02:30:58 +00001752 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001753 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001754 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001755 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001756 }
1757 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001758}
1759
hailfinger4c47e9d2010-10-19 22:06:20 +00001760void nonfatal_help_message(void)
1761{
1762 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1763 "This means we have to add special support for your board, "
1764 "programmer or flash chip.\n"
1765 "Please report this on IRC at irc.freenode.net (channel "
1766 "#flashrom) or\n"
1767 "mail flashrom@flashrom.org!\n"
1768 "-------------------------------------------------------------"
1769 "------------------\n"
1770 "You may now reboot or simply leave the machine running.\n");
1771}
1772
uweb34ec9f2009-10-01 18:40:02 +00001773void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001774{
snelsone42c3802010-05-07 20:09:04 +00001775 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001776 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001777 "mail flashrom@flashrom.org with FAILED: your board name in "
1778 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001779 "-------------------------------------------------------------"
1780 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001781 "DO NOT REBOOT OR POWEROFF!\n");
1782}
1783
uwe8d342eb2011-07-28 08:13:25 +00001784/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001785void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001786{
1787 enum programmer p;
1788 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001789 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001790 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001791 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001792 }
Simon Glass8dc82732013-07-16 10:13:51 -06001793 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001794}
1795
hailfingerf79d1712010-10-06 23:48:34 +00001796void list_programmers_linebreak(int startcol, int cols, int paren)
1797{
1798 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001799 int pnamelen;
1800 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001801 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001802 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001803
1804 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1805 pname = programmer_table[p].name;
1806 pnamelen = strlen(pname);
1807 if (remaining - pnamelen - 2 < 0) {
1808 if (firstline)
1809 firstline = 0;
1810 else
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001811 msg_ginfo("\n");
hailfingerf79d1712010-10-06 23:48:34 +00001812 for (i = 0; i < startcol; i++)
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001813 msg_ginfo(" ");
hailfingerf79d1712010-10-06 23:48:34 +00001814 remaining = cols - startcol;
1815 } else {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001816 msg_ginfo(" ");
hailfingerf79d1712010-10-06 23:48:34 +00001817 remaining--;
1818 }
1819 if (paren && (p == 0)) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001820 msg_ginfo("(");
hailfingerf79d1712010-10-06 23:48:34 +00001821 remaining--;
1822 }
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001823 msg_ginfo("%s", pname);
hailfingerf79d1712010-10-06 23:48:34 +00001824 remaining -= pnamelen;
1825 if (p < PROGRAMMER_INVALID - 1) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001826 msg_ginfo(",");
hailfingerf79d1712010-10-06 23:48:34 +00001827 remaining--;
1828 } else {
1829 if (paren)
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001830 msg_ginfo(")");
1831 msg_ginfo("\n");
hailfingerf79d1712010-10-06 23:48:34 +00001832 }
1833 }
1834}
1835
hailfinger3b471632010-03-27 16:36:40 +00001836void print_sysinfo(void)
1837{
David Hendricksc6c9f822010-11-03 15:07:01 -07001838 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001839#if HAVE_UTSNAME == 1
1840 struct utsname osinfo;
1841 uname(&osinfo);
1842
David Hendricksc6c9f822010-11-03 15:07:01 -07001843 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001844 osinfo.machine);
1845#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001846 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001847#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001848}
1849
1850void print_buildinfo(void)
1851{
1852 msg_gdbg("flashrom was built with");
hailfinger3b471632010-03-27 16:36:40 +00001853#if NEED_PCI == 1
1854#ifdef PCILIB_VERSION
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001855 msg_gdbg(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001856#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001857 msg_gdbg(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001858#endif
1859#endif
1860#ifdef __clang__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001861 msg_gdbg(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001862#ifdef __clang_version__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001863 msg_gdbg(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001864#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001865 msg_gdbg(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001866#endif
hailfinger3b471632010-03-27 16:36:40 +00001867#elif defined(__GNUC__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001868 msg_gdbg(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001869#ifdef __VERSION__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001870 msg_gdbg(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001871#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001872 msg_gdbg(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001873#endif
1874#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001875 msg_gdbg(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001876#endif
1877#if defined (__FLASHROM_LITTLE_ENDIAN__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001878 msg_gdbg(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001879#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001880 msg_gdbg(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001881#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001882 msg_gdbg("\n");
hailfinger3b471632010-03-27 16:36:40 +00001883}
1884
uwefdeca092008-01-21 15:24:22 +00001885void print_version(void)
1886{
David Hendricksc6c9f822010-11-03 15:07:01 -07001887 /* send to stderr for chromium os */
1888 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001889 print_sysinfo();
David Hendricks07268292016-09-14 16:05:58 -07001890 msg_gerr("\n");
uwefdeca092008-01-21 15:24:22 +00001891}
1892
hailfinger74819ad2010-05-15 15:04:37 +00001893void print_banner(void)
1894{
1895 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001896 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001897 msg_ginfo("\n");
1898}
1899
hailfingerc77acb52009-12-24 02:15:55 +00001900int selfcheck(void)
1901{
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001902 unsigned int i;
hailfinger45177872010-01-18 08:14:43 +00001903 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001904
1905 /* Safety check. Instead of aborting after the first error, check
1906 * if more errors exist.
1907 */
hailfingerc77acb52009-12-24 02:15:55 +00001908 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001909 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001910 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001911 }
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001912 /* It would be favorable if we could check for the correct layout (especially termination) of various
1913 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1914 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1915 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1916 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1917 * checks below.
1918 */
1919 if (flashchips_size <= 1 || flashchips[flashchips_size-1].name != NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001920 msg_gerr("Flashchips table miscompilation!\n");
1921 ret = 1;
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001922 } else {
1923 for (i = 0; i < flashchips_size - 1; i++) {
1924 const struct flashchip *chip = &flashchips[i];
1925 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1926 ret = 1;
1927 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
1928 "Please report a bug at flashrom@flashrom.org\n", i,
1929 chip->name == NULL ? "unnamed" : chip->name);
1930 }
1931 if (selfcheck_eraseblocks(chip))
1932 ret = 1;
1933 }
stefanct6d836ba2011-05-26 01:35:19 +00001934 }
stefanct6d836ba2011-05-26 01:35:19 +00001935
hailfinger45177872010-01-18 08:14:43 +00001936 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001937}
1938
hailfinger771fc182010-10-15 00:01:14 +00001939/* FIXME: This function signature needs to be improved once doit() has a better
1940 * function signature.
1941 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001942int 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 +00001943{
Patrick Georgiac3423f2017-02-03 20:58:06 +01001944 const struct flashchip *chip = flash->chip;
1945
hailfinger771fc182010-10-15 00:01:14 +00001946 if (!programmer_may_write && (write_it || erase_it)) {
1947 msg_perr("Write/erase is not working yet on your programmer in "
1948 "its current configuration.\n");
1949 /* --force is the wrong approach, but it's the best we can do
1950 * until the generic programmer parameter parser is merged.
1951 */
1952 if (!force)
1953 return 1;
1954 msg_cerr("Continuing anyway.\n");
1955 }
1956
1957 if (read_it || erase_it || write_it || verify_it) {
1958 /* Everything needs read. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001959 if (chip->tested.read == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001960 msg_cerr("Read is not working on this chip. ");
1961 if (!force)
1962 return 1;
1963 msg_cerr("Continuing anyway.\n");
1964 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001965 if (!chip->read) {
hailfinger771fc182010-10-15 00:01:14 +00001966 msg_cerr("flashrom has no read function for this "
1967 "flash chip.\n");
1968 return 1;
1969 }
1970 }
1971 if (erase_it || write_it) {
1972 /* Write needs erase. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001973 if (chip->tested.erase == NA) {
1974 msg_cerr("Erase is not possible on this chip.\n");
1975 return 1;
1976 }
1977 if (chip->tested.erase == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001978 msg_cerr("Erase is not working on this chip. ");
1979 if (!force)
1980 return 1;
1981 msg_cerr("Continuing anyway.\n");
1982 }
stefancte1c5acf2011-07-04 07:27:17 +00001983 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001984 msg_cerr("flashrom has no erase function for this "
1985 "flash chip.\n");
1986 return 1;
1987 }
hailfinger771fc182010-10-15 00:01:14 +00001988 }
1989 if (write_it) {
Patrick Georgiac3423f2017-02-03 20:58:06 +01001990 if (chip->tested.write == NA) {
1991 msg_cerr("Write is not possible on this chip.\n");
1992 return 1;
1993 }
1994 if (chip->tested.write == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001995 msg_cerr("Write is not working on this chip. ");
1996 if (!force)
1997 return 1;
1998 msg_cerr("Continuing anyway.\n");
1999 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01002000 if (!chip->write) {
hailfinger771fc182010-10-15 00:01:14 +00002001 msg_cerr("flashrom has no write function for this "
2002 "flash chip.\n");
2003 return 1;
2004 }
2005 }
2006 return 0;
2007}
2008
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002009/*
2010 * Function to erase entire flash chip.
2011 *
2012 * @flashctx pointer to the flash context to use
2013 * @oldcontents pointer to the buffer including current chip contents, to
2014 * decide which areas do in fact need to be erased
2015 * @size the size of the flash chip, in bytes.
2016 *
2017 * Returns zero on success or an error code.
2018 */
2019static int erase_chip(struct flashctx *flash, void *oldcontents,
2020 void *newcontents, size_t size)
2021{
2022 /*
2023 * To make sure that the chip is fully erased, let's cheat and create
2024 * a descriptor where the new contents are all erased.
2025 */
2026 struct action_descriptor *fake_descriptor;
2027 int ret = 0;
2028
2029 fake_descriptor = prepare_action_descriptor(flash, oldcontents,
2030 newcontents, 1);
2031 /* FIXME: Do we really want the scary warning if erase failed? After
2032 * all, after erase the chip is either blank or partially blank or it
2033 * has the old contents. A blank chip won't boot, so if the user
2034 * wanted erase and reboots afterwards, the user knows very well that
2035 * booting won't work.
2036 */
2037 if (erase_and_write_flash(flash, fake_descriptor)) {
2038 emergency_help_message();
2039 ret = 1;
2040 }
2041
2042 free(fake_descriptor);
2043
2044 return ret;
2045}
2046
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002047static int read_dest_content(struct flashctx *flash, int verify_it,
2048 uint8_t *dest, unsigned long size)
2049{
2050 if (((verify_it == VERIFY_OFF) || (verify_it == VERIFY_PARTIAL))
2051 && get_num_include_args()) {
2052 /*
2053 * If no full verification is required and not
2054 * the entire chip is about to be programmed,
2055 * read only the areas which might change.
2056 */
2057 if (handle_partial_read(flash, dest, read_flash, 0) < 0)
2058 return 1;
2059 } else {
2060 if (read_flash(flash, dest, 0, size))
2061 return 1;
2062 }
2063 return 0;
2064}
2065
hailfingerc77acb52009-12-24 02:15:55 +00002066/* This function signature is horrible. We need to design a better interface,
2067 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00002068 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00002069 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002070int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09002071 int write_it, int erase_it, int verify_it, int extract_it,
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002072 const char *diff_file, int do_diff)
hailfingerc77acb52009-12-24 02:15:55 +00002073{
hailfinger4c47e9d2010-10-19 22:06:20 +00002074 uint8_t *oldcontents;
2075 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00002076 int ret = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +01002077 unsigned long size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002078 struct action_descriptor *descriptor = NULL;
hailfingerc77acb52009-12-24 02:15:55 +00002079
stefanct02116582011-05-18 01:30:56 +00002080 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00002081 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00002082 ret = 1;
2083 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00002084 }
2085
hailfinger771fc182010-10-15 00:01:14 +00002086 /* Given the existence of read locks, we want to unlock for read,
2087 * erase and write.
2088 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01002089 if (flash->chip->unlock)
2090 flash->chip->unlock(flash);
hailfinger771fc182010-10-15 00:01:14 +00002091
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +10002092 flash->address_high_byte = -1;
2093 flash->in_4ba_mode = false;
2094
Ed Swierk28cf7992017-07-03 13:17:18 -07002095 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Edward O'Callaghan9713aa62019-07-18 18:28:57 +10002096 if ((flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN) && flash->chip->set_4ba) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +10002097 if (flash->chip->set_4ba(flash)) {
Ed Swierk28cf7992017-07-03 13:17:18 -07002098 msg_cerr("Enabling/disabling 4-byte addressing mode failed!\n");
2099 return 1;
Boris Baykov6323c242016-06-11 18:29:03 +02002100 }
Boris Baykov1a2f5322016-06-11 18:29:00 +02002101 }
2102
Simon Glass9ad06c12013-07-03 22:08:17 +09002103 if (extract_it) {
2104 ret = extract_regions(flash);
2105 goto out_nofree;
2106 }
2107
David Hendricksd0ea9ed2011-03-04 17:31:57 -08002108 /* mark entries included using -i argument as "included" if they are
2109 found in the master rom_entries list */
2110 if (process_include_args() < 0) {
2111 ret = 1;
2112 goto out_nofree;
2113 }
2114
hailfinger771fc182010-10-15 00:01:14 +00002115 if (read_it) {
2116 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00002117 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00002118 }
hailfingerb437e282010-11-04 01:04:27 +00002119
stefanctd611e8f2011-07-12 22:35:21 +00002120 oldcontents = malloc(size);
2121 if (!oldcontents) {
2122 msg_gerr("Out of memory!\n");
2123 exit(1);
2124 }
Simon Glass4c214132013-07-16 10:09:28 -06002125 /* Assume worst case: All blocks are not erased. */
2126 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002127 newcontents = malloc(size);
2128 if (!newcontents) {
2129 msg_gerr("Out of memory!\n");
2130 exit(1);
2131 }
Simon Glass4c214132013-07-16 10:09:28 -06002132 /* Assume best case: All blocks are erased. */
2133 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002134 /* Side effect of the assumptions above: Default write action is erase
2135 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002136 * oldcontents being completely unerased means we have to erase
2137 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002138 */
2139
hailfingerd217d122010-10-08 18:52:29 +00002140 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002141 /*
2142 * Note: This must be done before any files specified by -i
2143 * arguments are processed merged into the newcontents since
2144 * -i files take priority. See http://crbug.com/263495.
2145 */
2146 if (filename) {
2147 if (read_buf_from_file(newcontents, size, filename)) {
2148 ret = 1;
2149 goto out;
2150 }
2151 } else {
2152 /* Content will be read from -i args, so they must
2153 * not overlap. */
2154 if (included_regions_overlap()) {
2155 msg_gerr("Error: Included regions must "
2156 "not overlap.\n");
2157 ret = 1;
2158 goto out;
2159 }
stepan1da96c02006-11-21 23:48:51 +00002160 }
2161
David Hendricksac82cac2012-06-19 10:29:37 -07002162#if 0
2163 /*
2164 * FIXME: show_id() causes failure if vendor:mainboard do not
2165 * match. This may happen if codenames are in flux.
2166 * See chrome-os-partner:10414.
2167 */
hailfinger90c7d542010-05-31 15:27:27 +00002168#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002169 if (programmer == PROGRAMMER_INTERNAL)
2170 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002171#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002172#endif
ollie5672ac62004-03-17 22:22:08 +00002173 }
2174
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002175 if (do_diff) {
2176 /*
2177 * Obtain a reference image so that we can check whether
2178 * regions need to be erased and to give better diagnostics in
2179 * case write fails. If --fast-verify is used then only the
2180 * regions which are included using -i will be read.
2181 */
2182 if (diff_file) {
2183 msg_cdbg("Reading old contents from file... ");
2184 if (read_buf_from_file(oldcontents, size, diff_file)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002185 ret = 1;
2186 msg_cdbg("FAILED.\n");
2187 goto out;
2188 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002189 } else {
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002190 msg_cdbg("Reading old contents from flash chip... ");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002191 ret = read_dest_content(flash, verify_it,
2192 oldcontents, size);
2193 if (ret) {
2194 msg_cdbg("FAILED.\n");
2195 goto out;
David Hendricks52ddff02013-07-23 15:05:14 -07002196 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002197 }
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002198 msg_cdbg("done.\n");
2199 } else if (!erase_it) {
2200 msg_pinfo("No diff performed, considering the chip erased.\n");
2201 memset(oldcontents, flash_erase_value(flash), size);
hailfinger4c47e9d2010-10-19 22:06:20 +00002202 }
David Hendricksac1d25c2016-08-09 17:00:58 -07002203
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002204
David Hendricksdf29a832013-06-28 14:33:51 -07002205 /*
2206 * Note: This must be done after reading the file specified for the
2207 * -w/-v argument, if any, so that files specified using -i end up
2208 * in the "newcontents" buffer before being written.
2209 * See http://crbug.com/263495.
2210 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002211 if (handle_romentries(flash, oldcontents, newcontents, erase_it)) {
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002212 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002213 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002214 goto out;
2215 }
uwef6641642007-05-09 10:17:44 +00002216
David Hendricksa7e114b2016-02-26 18:49:15 -08002217 if (erase_it) {
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002218 erase_chip(flash, oldcontents, newcontents, size);
2219 goto verify;
David Hendricksa7e114b2016-02-26 18:49:15 -08002220 }
2221
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002222 descriptor = prepare_action_descriptor(flash, oldcontents,
2223 newcontents, do_diff);
stuge8ce3a3c2008-04-28 14:47:30 +00002224 if (write_it) {
David Hendricksb64b39a2016-10-11 13:48:06 -07002225 // parse the new fmap and disable soft WP if necessary
David Hendricksac1d25c2016-08-09 17:00:58 -07002226 if ((ret = cros_ec_prepare(newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002227 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002228 goto out;
2229 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002230
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002231 if (erase_and_write_flash(flash, descriptor)) {
hailfingerb437e282010-11-04 01:04:27 +00002232 msg_cerr("Uh oh. Erase/write failed. Checking if "
2233 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002234 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002235 if (!memcmp(oldcontents, newcontents, size)) {
2236 msg_cinfo("Good. It seems nothing was "
2237 "changed.\n");
2238 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002239 ret = 1;
2240 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002241 }
2242 }
hailfingerd217d122010-10-08 18:52:29 +00002243 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002244 ret = 1;
2245 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002246 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002247
David Hendricksac1d25c2016-08-09 17:00:58 -07002248 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002249 if (ret < 0) {
2250 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002251 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002252 emergency_help_message();
2253 ret = 1;
2254 goto out;
2255 } else if (ret > 0) {
2256 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002257 msg_pdbg("CROS_EC needs 2nd pass.\n");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002258 ret = read_dest_content(flash, verify_it,
2259 oldcontents, size);
2260 if (ret) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002261 emergency_help_message();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002262 goto out;
2263 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002264
2265 /* Get a new descriptor. */
2266 free(descriptor);
2267 descriptor = prepare_action_descriptor(flash,
2268 oldcontents,
2269 newcontents,
2270 do_diff);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002271 // write 2nd pass
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002272 if (erase_and_write_flash(flash, descriptor)) {
David Hendricksb907de32014-08-11 16:47:09 -07002273 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002274 emergency_help_message();
2275 ret = 1;
2276 goto out;
2277 }
2278 ret = 0;
2279 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002280
David Hendricksac1d25c2016-08-09 17:00:58 -07002281 if (cros_ec_finish() < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002282 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002283 emergency_help_message();
2284 ret = 1;
2285 goto out;
2286 }
stuge8ce3a3c2008-04-28 14:47:30 +00002287 }
ollie6a600992005-11-26 21:55:36 +00002288
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002289 verify:
hailfinger0459e1c2009-08-19 13:55:34 +00002290 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002291 if ((write_it || erase_it) && !content_has_changed) {
2292 msg_gdbg("Nothing was erased or written, skipping "
2293 "verification\n");
2294 } else {
2295 /* Work around chips which need some time to calm down. */
2296 if (write_it && verify_it != VERIFY_PARTIAL)
2297 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002298
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002299 ret = verify_flash(flash, descriptor, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002300
David Hendricks9ba79fb2015-04-03 12:06:16 -07002301 /* If we tried to write, and verification now fails, we
2302 * might have an emergency situation.
2303 */
2304 if (ret && write_it)
2305 emergency_help_message();
2306 }
hailfinger0459e1c2009-08-19 13:55:34 +00002307 }
ollie6a600992005-11-26 21:55:36 +00002308
hailfinger90fcf9b2010-11-05 14:51:59 +00002309out:
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002310 if (descriptor)
2311 free(descriptor);
2312
hailfinger90fcf9b2010-11-05 14:51:59 +00002313 free(oldcontents);
2314 free(newcontents);
2315out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002316 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002317 /*
Edward O'Callaghan1a3fd132019-06-04 14:18:55 +10002318 * programmer_shutdown() call is moved to cli_classic() in chromium os
David Hendricks668f29d2011-01-27 18:51:45 -08002319 * tree. This is because some operations, such as write protection,
2320 * requires programmer_shutdown() but does not call doit().
2321 */
2322// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002323 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002324}