blob: f4bb83db398af220e0fae6aa91712019bf4f5f91 [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
Edward O'Callaghan0949b782019-11-10 23:23:20 +11008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
rminnich8d3ff912003-10-25 17:01:29 +000010 *
uweb25f1ea2007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000015 *
uweb25f1ea2007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000020 */
21
hailfingera83a5fe2010-05-30 22:24:40 +000022#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000023#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000024#ifndef __LIBPAYLOAD__
25#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000026#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000027#endif
rminnich8d3ff912003-10-25 17:01:29 +000028#include <string.h>
29#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000030#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000031#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000032#if HAVE_UTSNAME == 1
33#include <sys/utsname.h>
34#endif
Vincent Palatin7ab23932014-10-01 12:09:16 -070035#include <unistd.h>
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -070036
37#include "action_descriptor.h"
rminnich8d3ff912003-10-25 17:01:29 +000038#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000039#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090040#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000041#include "programmer.h"
Duncan Laurie25a4ca22019-04-25 12:08:52 -070042#include "spi.h"
rminnich8d3ff912003-10-25 17:01:29 +000043
krause2eb76212011-01-17 07:50:42 +000044const char flashrom_version[] = FLASHROM_VERSION;
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +100045const char *chip_to_probe = NULL;
hailfinger80422e22009-12-13 22:28:00 +000046
David Hendricks9ba79fb2015-04-03 12:06:16 -070047/* Set if any erase/write operation is to be done. This will be used to
48 * decide if final verification is needed. */
49static int content_has_changed = 0;
50
David Hendricks1ed1d352011-11-23 17:54:37 -080051/* error handling stuff */
52enum error_action access_denied_action = error_ignore;
53
54int ignore_error(int err) {
55 int rc = 0;
56
57 switch(err) {
58 case ACCESS_DENIED:
59 if (access_denied_action == error_ignore)
60 rc = 1;
61 break;
62 default:
63 break;
64 }
65
66 return rc;
67}
68
hailfinger969e2f32011-09-08 00:00:29 +000069static enum programmer programmer = PROGRAMMER_INVALID;
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +100070static const char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000071
David Hendricksac1d25c2016-08-09 17:00:58 -070072/* Supported buses for the current programmer. */
73enum chipbustype buses_supported;
74
uwee15beb92010-08-08 17:01:18 +000075/*
hailfinger80422e22009-12-13 22:28:00 +000076 * Programmers supporting multiple buses can have differing size limits on
77 * each bus. Store the limits for each bus in a common struct.
78 */
hailfinger1ff33dc2010-07-03 11:02:10 +000079struct decode_sizes max_rom_decode;
80
81/* If nonzero, used as the start address of bottom-aligned flash. */
82unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000083
hailfinger5828baf2010-07-03 12:14:25 +000084/* Is writing allowed with this programmer? */
85int programmer_may_write;
86
hailfingerabe249e2009-05-08 17:43:22 +000087const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000088#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000089 {
hailfinger3548a9a2009-08-12 14:34:35 +000090 .name = "internal",
Edward O'Callaghan0949b782019-11-10 23:23:20 +110091 .type = OTHER,
92 .devs.note = NULL,
hailfinger6c69ab02009-05-11 15:46:43 +000093 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000094 .map_flash_region = physmap,
95 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000096 .delay = internal_delay,
David Hendricks55cdd9c2015-11-25 14:37:26 -080097
98 /*
99 * "Internal" implies in-system programming on a live system, so
100 * handle with paranoia to catch errors early. If something goes
101 * wrong then hopefully the system will still be recoverable.
102 */
103 .paranoid = 1,
hailfingerabe249e2009-05-08 17:43:22 +0000104 },
hailfinger80422e22009-12-13 22:28:00 +0000105#endif
stepan927d4e22007-04-04 22:45:58 +0000106
hailfinger90c7d542010-05-31 15:27:27 +0000107#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000108 {
hailfinger3548a9a2009-08-12 14:34:35 +0000109 .name = "dummy",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100110 .type = OTHER,
111 /* FIXME */
112 .devs.note = "Dummy device, does nothing and logs all accesses\n",
hailfinger6c69ab02009-05-11 15:46:43 +0000113 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000114 .map_flash_region = dummy_map,
115 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000116 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000117 },
hailfinger571a6b32009-09-16 10:09:21 +0000118#endif
hailfingera9df33c2009-05-09 00:54:55 +0000119
hailfinger90c7d542010-05-31 15:27:27 +0000120#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000121 {
hailfinger3548a9a2009-08-12 14:34:35 +0000122 .name = "nic3com",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100123 .type = PCI,
124 .devs.dev = nics_3com,
uwe0f5a3a22009-05-13 11:36:06 +0000125 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000126 .map_flash_region = fallback_map,
127 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000128 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000129 },
hailfinger571a6b32009-09-16 10:09:21 +0000130#endif
uwe0f5a3a22009-05-13 11:36:06 +0000131
hailfinger90c7d542010-05-31 15:27:27 +0000132#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000133 {
hailfinger0d703d42011-03-07 01:08:09 +0000134 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000135 .name = "nicrealtek",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100136 .type = PCI,
137 .devs.dev = nics_realtek,
uwe8d342eb2011-07-28 08:13:25 +0000138 .init = nicrealtek_init,
139 .map_flash_region = fallback_map,
140 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000141 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000142 },
hailfinger5aa36982010-05-21 21:54:07 +0000143#endif
144
hailfingerf0a368f2010-06-07 22:37:54 +0000145#if CONFIG_NICNATSEMI == 1
146 {
uwe8d342eb2011-07-28 08:13:25 +0000147 .name = "nicnatsemi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100148 .type = PCI,
149 .devs.dev = nics_natsemi,
uwe8d342eb2011-07-28 08:13:25 +0000150 .init = nicnatsemi_init,
151 .map_flash_region = fallback_map,
152 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000153 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000154 },
155#endif
hailfinger5aa36982010-05-21 21:54:07 +0000156
hailfinger90c7d542010-05-31 15:27:27 +0000157#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000158 {
159 .name = "gfxnvidia",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100160 .type = PCI,
161 .devs.dev = gfx_nvidia,
uweff4576d2009-09-30 18:29:55 +0000162 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000163 .map_flash_region = fallback_map,
164 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000165 .delay = internal_delay,
166 },
167#endif
168
hailfinger90c7d542010-05-31 15:27:27 +0000169#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000170 {
uwee2f95ef2009-09-02 23:00:46 +0000171 .name = "drkaiser",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100172 .type = PCI,
173 .devs.dev = drkaiser_pcidev,
uwee2f95ef2009-09-02 23:00:46 +0000174 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000175 .map_flash_region = fallback_map,
176 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000177 .delay = internal_delay,
178 },
hailfinger571a6b32009-09-16 10:09:21 +0000179#endif
uwee2f95ef2009-09-02 23:00:46 +0000180
hailfinger90c7d542010-05-31 15:27:27 +0000181#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000182 {
hailfinger3548a9a2009-08-12 14:34:35 +0000183 .name = "satasii",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100184 .type = PCI,
185 .devs.dev = satas_sii,
ruikda922a12009-05-17 19:39:27 +0000186 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000187 .map_flash_region = fallback_map,
188 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000189 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000190 },
hailfinger571a6b32009-09-16 10:09:21 +0000191#endif
ruikda922a12009-05-17 19:39:27 +0000192
hailfinger90c7d542010-05-31 15:27:27 +0000193#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000194 {
195 .name = "atahpt",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100196 .type = PCI,
197 .devs.dev = ata_hpt,
uwe7e627c82010-02-21 21:17:00 +0000198 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000199 .map_flash_region = fallback_map,
200 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000201 .delay = internal_delay,
202 },
203#endif
204
hailfinger90c7d542010-05-31 15:27:27 +0000205#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000206 {
hailfinger90c7d542010-05-31 15:27:27 +0000207 .name = "ft2232_spi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100208 .type = USB,
hailfingerf31da3d2009-06-16 21:08:06 +0000209 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000210 .map_flash_region = fallback_map,
211 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000212 .delay = internal_delay,
213 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000214#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000215
hailfinger90c7d542010-05-31 15:27:27 +0000216#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000217 {
hailfinger3548a9a2009-08-12 14:34:35 +0000218 .name = "serprog",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100219 .type = OTHER,
220 /* FIXME */
221 .devs.note = "All programmer devices speaking the serprog protocol\n",
hailfinger37b4fbf2009-06-23 11:33:43 +0000222 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000223 .map_flash_region = fallback_map,
224 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000225 .delay = serprog_delay,
226 },
hailfinger74d88a72009-08-12 16:17:41 +0000227#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000228
hailfinger90c7d542010-05-31 15:27:27 +0000229#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000230 {
hailfinger90c7d542010-05-31 15:27:27 +0000231 .name = "buspirate_spi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100232 .type = OTHER,
233 /* FIXME */
234 .devs.note = "Dangerous Prototypes Bus Pirate\n",
hailfinger9c5add72009-11-24 00:20:03 +0000235 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000236 .map_flash_region = fallback_map,
237 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000238 .delay = internal_delay,
239 },
240#endif
241
Anton Staafb2647882014-09-17 15:13:43 -0700242#if CONFIG_RAIDEN_DEBUG_SPI == 1
243 {
244 .name = "raiden_debug_spi",
Brian J. Nemecb42d6c12020-07-23 03:07:38 -0700245 .type = USB,
246 .devs.dev = devs_raiden,
Anton Staafb2647882014-09-17 15:13:43 -0700247 .init = raiden_debug_spi_init,
248 .map_flash_region = fallback_map,
249 .unmap_flash_region = fallback_unmap,
250 .delay = internal_delay,
251 },
252#endif
253
hailfinger90c7d542010-05-31 15:27:27 +0000254#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000255 {
256 .name = "dediprog",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100257 .type = USB,
hailfingerdfb32a02010-01-19 11:15:48 +0000258 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000259 .map_flash_region = fallback_map,
260 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000261 .delay = internal_delay,
262 },
263#endif
264
hailfinger52c4fa02010-07-21 10:26:01 +0000265#if CONFIG_RAYER_SPI == 1
266 {
267 .name = "rayer_spi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100268 .type = OTHER,
269 /* FIXME */
270 .devs.note = "RayeR parallel port programmer\n",
hailfinger52c4fa02010-07-21 10:26:01 +0000271 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000272 .map_flash_region = fallback_map,
273 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000274 .delay = internal_delay,
275 },
276#endif
277
hailfinger7949b652011-05-08 00:24:18 +0000278#if CONFIG_NICINTEL == 1
279 {
280 .name = "nicintel",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100281 .type = PCI,
282 .devs.dev = nics_intel,
hailfinger7949b652011-05-08 00:24:18 +0000283 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000284 .map_flash_region = fallback_map,
285 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000286 .delay = internal_delay,
287 },
288#endif
289
uwe6764e922010-09-03 18:21:21 +0000290#if CONFIG_NICINTEL_SPI == 1
291 {
uwe8d342eb2011-07-28 08:13:25 +0000292 .name = "nicintel_spi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100293 .type = PCI,
294 .devs.dev = nics_intel_spi,
uwe8d342eb2011-07-28 08:13:25 +0000295 .init = nicintel_spi_init,
296 .map_flash_region = fallback_map,
297 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000298 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000299 },
300#endif
301
hailfingerfb1f31f2010-12-03 14:48:11 +0000302#if CONFIG_OGP_SPI == 1
303 {
uwe8d342eb2011-07-28 08:13:25 +0000304 .name = "ogp_spi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100305 .type = PCI,
306 .devs.dev = ogp_spi,
uwe8d342eb2011-07-28 08:13:25 +0000307 .init = ogp_spi_init,
308 .map_flash_region = fallback_map,
309 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000310 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000311 },
312#endif
313
hailfinger935365d2011-02-04 21:37:59 +0000314#if CONFIG_SATAMV == 1
315 {
316 .name = "satamv",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100317 .type = PCI,
318 .devs.dev = satas_mv,
hailfinger935365d2011-02-04 21:37:59 +0000319 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000320 .map_flash_region = fallback_map,
321 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000322 .delay = internal_delay,
323 },
324#endif
325
David Hendrickscebee892015-05-23 20:30:30 -0700326#if CONFIG_LINUX_MTD == 1
327 {
328 .name = "linux_mtd",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100329 .type = OTHER,
330 .devs.note = "Device files /dev/mtd*\n",
David Hendrickscebee892015-05-23 20:30:30 -0700331 .init = linux_mtd_init,
332 .map_flash_region = fallback_map,
333 .unmap_flash_region = fallback_unmap,
334 .delay = internal_delay,
335 },
336#endif
337
uwe7df6dda2011-09-03 18:37:52 +0000338#if CONFIG_LINUX_SPI == 1
339 {
340 .name = "linux_spi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100341 .type = OTHER,
342 .devs.note = "Device files /dev/spidev*.*\n",
uwe7df6dda2011-09-03 18:37:52 +0000343 .init = linux_spi_init,
344 .map_flash_region = fallback_map,
345 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000346 .delay = internal_delay,
347 },
348#endif
349
Shiyu Sun9dde7162020-04-16 17:32:55 +1000350#if CONFIG_LSPCON_I2C_SPI == 1
351 {
352 .name = "lspcon_i2c_spi",
353 .type = OTHER,
354 .devs.note = "Device files /dev/i2c-*.\n",
355 .init = lspcon_i2c_spi_init,
356 .map_flash_region = fallback_map,
357 .unmap_flash_region = fallback_unmap,
358 .delay = internal_delay,
359 },
360#endif
361
Edward O'Callaghan97dd9262020-03-26 00:00:41 +1100362#if CONFIG_REALTEK_MST_I2C_SPI == 1
363 {
364 .name = "realtek_mst_i2c_spi",
365 .type = OTHER,
366 .devs.note = "Device files /dev/i2c-*.\n",
367 .init = realtek_mst_i2c_spi_init,
368 .map_flash_region = fallback_map,
369 .unmap_flash_region = fallback_unmap,
370 .delay = internal_delay,
371 },
372#endif
373
Patrick Georgi8ddfee92017-03-20 14:54:28 +0100374 {0}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000375};
stepan927d4e22007-04-04 22:45:58 +0000376
David Hendricksbf36f092010-11-02 23:39:29 -0700377#define CHIP_RESTORE_MAXFN 4
378static int chip_restore_fn_count = 0;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000379static struct chip_restore_func_data {
David Hendricksbf36f092010-11-02 23:39:29 -0700380 CHIP_RESTORE_CALLBACK;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700381 struct flashctx *flash;
David Hendricksbf36f092010-11-02 23:39:29 -0700382 uint8_t status;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000383} chip_restore_fn[CHIP_RESTORE_MAXFN];
David Hendricksbf36f092010-11-02 23:39:29 -0700384
David Hendricks668f29d2011-01-27 18:51:45 -0800385
hailfingerf31cbdc2010-11-10 15:25:18 +0000386#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000387static int shutdown_fn_count = 0;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000388static struct shutdown_func_data {
David Hendricks93784b42016-08-09 17:00:38 -0700389 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000390 void *data;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000391} shutdown_fn[SHUTDOWN_MAXFN];
hailfinger1ff33dc2010-07-03 11:02:10 +0000392/* Initialize to 0 to make sure nobody registers a shutdown function before
393 * programmer init.
394 */
395static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000396
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700397static int check_block_eraser(const struct flashctx *flash, int k, int log);
stefanct569dbb62011-07-01 00:19:12 +0000398
hailfingerdc6f7972010-02-14 01:20:28 +0000399/* Register a function to be executed on programmer shutdown.
400 * The advantage over atexit() is that you can supply a void pointer which will
401 * be used as parameter to the registered function upon programmer shutdown.
402 * This pointer can point to arbitrary data used by said function, e.g. undo
403 * information for GPIO settings etc. If unneeded, set data=NULL.
404 * Please note that the first (void *data) belongs to the function signature of
405 * the function passed as first parameter.
406 */
David Hendricks93784b42016-08-09 17:00:38 -0700407int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000408{
409 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000410 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000411 SHUTDOWN_MAXFN);
412 return 1;
413 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000414 if (!may_register_shutdown) {
415 msg_perr("Tried to register a shutdown function before "
416 "programmer init.\n");
417 return 1;
418 }
hailfingerdc6f7972010-02-14 01:20:28 +0000419 shutdown_fn[shutdown_fn_count].func = function;
420 shutdown_fn[shutdown_fn_count].data = data;
421 shutdown_fn_count++;
422
423 return 0;
424}
425
David Hendricksbf36f092010-11-02 23:39:29 -0700426//int register_chip_restore(int (*function) (void *data), void *data)
427int register_chip_restore(CHIP_RESTORE_CALLBACK,
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700428 struct flashctx *flash, uint8_t status)
David Hendricksbf36f092010-11-02 23:39:29 -0700429{
430 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
431 msg_perr("Tried to register more than %i chip restore"
432 " functions.\n", CHIP_RESTORE_MAXFN);
433 return 1;
434 }
435 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
436 chip_restore_fn[chip_restore_fn_count].flash = flash;
437 chip_restore_fn[chip_restore_fn_count].status = status;
438 chip_restore_fn_count++;
439
440 return 0;
441}
442
David Hendricksac1d25c2016-08-09 17:00:58 -0700443int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000444{
hailfinger1ef766d2010-07-06 09:55:48 +0000445 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000446
447 if (prog >= PROGRAMMER_INVALID) {
448 msg_perr("Invalid programmer specified!\n");
449 return -1;
450 }
451 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000452 /* Initialize all programmer specific data. */
453 /* Default to unlimited decode sizes. */
454 max_rom_decode = (const struct decode_sizes) {
455 .parallel = 0xffffffff,
456 .lpc = 0xffffffff,
457 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000458 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000459 };
David Hendricksac1d25c2016-08-09 17:00:58 -0700460 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000461 /* Default to top aligned flash at 4 GB. */
462 flashbase = 0;
463 /* Registering shutdown functions is now allowed. */
464 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000465 /* Default to allowing writes. Broken programmers set this to 0. */
466 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000467
468 programmer_param = param;
469 msg_pdbg("Initializing %s programmer\n",
470 programmer_table[programmer].name);
David Hendricksac1d25c2016-08-09 17:00:58 -0700471 ret = programmer_table[programmer].init();
hailfinger1ef766d2010-07-06 09:55:48 +0000472 return ret;
uweabe92a52009-05-16 22:36:00 +0000473}
474
David Hendricksbf36f092010-11-02 23:39:29 -0700475int chip_restore()
476{
477 int rc = 0;
478
479 while (chip_restore_fn_count > 0) {
480 int i = --chip_restore_fn_count;
481 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
482 chip_restore_fn[i].status);
483 }
484
485 return rc;
486}
487
David Hendricks93784b42016-08-09 17:00:38 -0700488int programmer_shutdown(void)
uweabe92a52009-05-16 22:36:00 +0000489{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000490 int ret = 0;
491
hailfinger1ff33dc2010-07-03 11:02:10 +0000492 /* Registering shutdown functions is no longer allowed. */
493 may_register_shutdown = 0;
494 while (shutdown_fn_count > 0) {
495 int i = --shutdown_fn_count;
David Hendricks93784b42016-08-09 17:00:38 -0700496 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000497 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000498 return ret;
uweabe92a52009-05-16 22:36:00 +0000499}
500
501void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
502 size_t len)
503{
504 return programmer_table[programmer].map_flash_region(descr,
505 phys_addr, len);
506}
507
508void programmer_unmap_flash_region(void *virt_addr, size_t len)
509{
510 programmer_table[programmer].unmap_flash_region(virt_addr, len);
511}
512
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700513void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000514{
Craig Hesling65eb8812019-08-01 09:33:56 -0700515 par_master->chip_writeb(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000516}
517
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700518void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000519{
Craig Hesling65eb8812019-08-01 09:33:56 -0700520 par_master->chip_writew(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000521}
522
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700523void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000524{
Craig Hesling65eb8812019-08-01 09:33:56 -0700525 par_master->chip_writel(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000526}
527
Stuart langleyc98e43f2020-03-26 20:27:36 +1100528void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000529{
Craig Hesling65eb8812019-08-01 09:33:56 -0700530 par_master->chip_writen(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000531}
532
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700533uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000534{
Craig Hesling65eb8812019-08-01 09:33:56 -0700535 return par_master->chip_readb(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000536}
537
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700538uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000539{
Craig Hesling65eb8812019-08-01 09:33:56 -0700540 return par_master->chip_readw(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000541}
542
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700543uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000544{
Craig Hesling65eb8812019-08-01 09:33:56 -0700545 return par_master->chip_readl(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000546}
547
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700548void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000549{
Craig Hesling65eb8812019-08-01 09:33:56 -0700550 par_master->chip_readn(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000551}
552
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +1000553void programmer_delay(unsigned int usecs)
hailfingere5829f62009-06-05 17:48:08 +0000554{
555 programmer_table[programmer].delay(usecs);
556}
557
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700558void map_flash_registers(struct flashctx *flash)
stepan15e64bc2007-05-24 08:48:10 +0000559{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100560 size_t size = flash->chip->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000561 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000562 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000563 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000564}
565
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700566int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000567{
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700568 chip_readn(flash, buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000569
hailfinger23060112009-05-08 12:49:03 +0000570 return 0;
571}
572
David Hendricksda18f692016-10-21 17:49:49 -0700573/* This is a somewhat hacked function similar in some ways to strtok(). It will
574 * look for needle with a subsequent '=' in haystack, return a copy of needle.
hailfinger6e5a52a2009-11-24 18:27:10 +0000575 */
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +1000576char *extract_param(const char *const *haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000577{
David Hendricksda18f692016-10-21 17:49:49 -0700578 char *param_pos, *opt_pos;
hailfinger1ef766d2010-07-06 09:55:48 +0000579 char *opt = NULL;
580 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000581 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000582
hailfingerf4aaccc2010-04-28 15:22:14 +0000583 needlelen = strlen(needle);
584 if (!needlelen) {
585 msg_gerr("%s: empty needle! Please report a bug at "
586 "flashrom@flashrom.org\n", __func__);
587 return NULL;
588 }
589 /* No programmer parameters given. */
590 if (*haystack == NULL)
591 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000592 param_pos = strstr(*haystack, needle);
593 do {
594 if (!param_pos)
595 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000596 /* Needle followed by '='? */
597 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600598
hailfinger1ef766d2010-07-06 09:55:48 +0000599 /* Beginning of the string? */
600 if (param_pos == *haystack)
601 break;
602 /* After a delimiter? */
603 if (strchr(delim, *(param_pos - 1)))
604 break;
605 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000606 /* Continue searching. */
607 param_pos++;
608 param_pos = strstr(param_pos, needle);
609 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000610
hailfinger6e5a52a2009-11-24 18:27:10 +0000611 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000612 /* Get the string after needle and '='. */
613 opt_pos = param_pos + needlelen + 1;
614 optlen = strcspn(opt_pos, delim);
615 /* Return an empty string if the parameter was empty. */
616 opt = malloc(optlen + 1);
617 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000618 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000619 exit(1);
620 }
hailfinger1ef766d2010-07-06 09:55:48 +0000621 strncpy(opt, opt_pos, optlen);
622 opt[optlen] = '\0';
hailfinger6e5a52a2009-11-24 18:27:10 +0000623 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000624
hailfinger1ef766d2010-07-06 09:55:48 +0000625 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000626}
627
Edward O'Callaghanc4d1f1c2020-04-17 13:27:23 +1000628char *extract_programmer_param(const char *const param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000629{
630 return extract_param(&programmer_param, param_name, ",");
631}
632
stefancte1c5acf2011-07-04 07:27:17 +0000633/* Returns the number of well-defined erasers for a chip. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700634static unsigned int count_usable_erasers(const struct flashctx *flash)
stefanct569dbb62011-07-01 00:19:12 +0000635{
636 unsigned int usable_erasefunctions = 0;
637 int k;
638 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
639 if (!check_block_eraser(flash, k, 0))
640 usable_erasefunctions++;
641 }
642 return usable_erasefunctions;
643}
644
hailfinger7af83692009-06-15 17:23:36 +0000645/* start is an offset to the base address of the flash chip */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700646int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000647{
648 int ret;
649 uint8_t *cmpbuf = malloc(len);
650
651 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000652 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000653 exit(1);
654 }
Simon Glass4c214132013-07-16 10:09:28 -0600655 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000656 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
657 free(cmpbuf);
658 return ret;
659}
660
Stuart Langley60395ef2020-03-25 20:32:45 +1100661static int compare_chunk(uint8_t *readbuf, const uint8_t *cmpbuf, unsigned int start,
Simon Glass4e305f42015-01-08 06:29:04 -0700662 unsigned int len, const char *message)
663{
664 int failcount = 0, i;
665
666 for (i = 0; i < len; i++) {
667 if (cmpbuf[i] != readbuf[i]) {
668 if (!failcount) {
669 msg_cerr("%s FAILED at 0x%08x! "
670 "Expected=0x%02x, Read=0x%02x,",
671 message, start + i,
672 cmpbuf[i], readbuf[i]);
673 }
674 failcount++;
675 }
676 }
677
678 return failcount;
679}
680
uwee15beb92010-08-08 17:01:18 +0000681/*
hailfinger7af3d192009-11-25 17:05:52 +0000682 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000683 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000684 * @start offset to the base address of the flash chip
685 * @len length of the verified area
686 * @message string to print in the "FAILED" message
687 * @return 0 for success, -1 for failure
688 */
Stuart Langley60395ef2020-03-25 20:32:45 +1100689int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000690 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000691{
hailfinger8cb6ece2010-11-16 17:21:58 +0000692 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000693 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000694
695 if (!len)
696 goto out_free;
697
Patrick Georgif3fa2992017-02-02 16:24:44 +0100698 if (!flash->chip->read) {
snelsone42c3802010-05-07 20:09:04 +0000699 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000700 return 1;
701 }
hailfinger7af83692009-06-15 17:23:36 +0000702 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000703 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000704 exit(1);
705 }
706
Patrick Georgif3fa2992017-02-02 16:24:44 +0100707 if (start + len > flash->chip->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000708 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000709 " total_size 0x%x\n", __func__, start, len,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100710 flash->chip->total_size * 1024);
hailfinger7af83692009-06-15 17:23:36 +0000711 ret = -1;
712 goto out_free;
713 }
714 if (!message)
715 message = "VERIFY";
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -0700716 msg_gdbg("%#06x..%#06x ", start, start + len -1);
Simon Glass4e305f42015-01-08 06:29:04 -0700717 if (programmer_table[programmer].paranoid) {
718 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800719
Simon Glass4e305f42015-01-08 06:29:04 -0700720 /* limit chunksize in order to catch errors early */
721 for (i = 0, chunksize = 0; i < len; i += chunksize) {
722 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800723
Patrick Georgif3fa2992017-02-02 16:24:44 +0100724 chunksize = min(flash->chip->page_size, len - i);
725 tmp = flash->chip->read(flash, readbuf + i, start + i, chunksize);
Simon Glass4e305f42015-01-08 06:29:04 -0700726 if (tmp) {
727 ret = tmp;
728 if (ignore_error(tmp))
729 continue;
730 else
731 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800732 }
Simon Glass4e305f42015-01-08 06:29:04 -0700733
Duncan Laurie25a4ca22019-04-25 12:08:52 -0700734 /*
735 * Check write access permission and do not compare chunks
736 * where flashrom does not have write access to the region.
737 */
738 if (flash->chip->check_access) {
739 tmp = flash->chip->check_access(flash, start + i, chunksize, 0);
740 if (tmp && ignore_error(tmp))
741 continue;
742 }
743
Simon Glass4e305f42015-01-08 06:29:04 -0700744 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
745 chunksize, message);
746 if (failcount)
747 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800748 }
Simon Glass4e305f42015-01-08 06:29:04 -0700749 } else {
750 int tmp;
751
752 /* read as much as we can to reduce transaction overhead */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100753 tmp = flash->chip->read(flash, readbuf, start, len);
Simon Glass4e305f42015-01-08 06:29:04 -0700754 if (tmp && !ignore_error(tmp)) {
755 ret = tmp;
756 goto out_free;
757 }
758
759 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000760 }
761
hailfinger5be6c0f2009-07-23 01:42:56 +0000762 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000763 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000764 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000765 ret = -1;
766 }
hailfinger7af83692009-06-15 17:23:36 +0000767
768out_free:
769 free(readbuf);
770 return ret;
771}
772
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100773/* Helper function for need_erase() that focuses on granularities of gran bytes. */
774static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
775 unsigned int gran)
776{
777 unsigned int i, j, limit;
778 for (j = 0; j < len / gran; j++) {
779 limit = min (gran, len - j * gran);
780 /* Are 'have' and 'want' identical? */
781 if (!memcmp(have + j * gran, want + j * gran, limit))
782 continue;
783 /* have needs to be in erased state. */
784 for (i = 0; i < limit; i++)
785 if (have[j * gran + i] != 0xff)
786 return 1;
787 }
788 return 0;
789}
790
uwee15beb92010-08-08 17:01:18 +0000791/*
hailfingerb247c7a2010-03-08 00:42:32 +0000792 * Check if the buffer @have can be programmed to the content of @want without
793 * erasing. This is only possible if all chunks of size @gran are either kept
794 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000795 *
hailfingerb247c7a2010-03-08 00:42:32 +0000796 * The following write granularities (enum @gran) are known:
797 * - 1 bit. Each bit can be cleared individually.
798 * - 1 byte. A byte can be written once. Further writes to an already written
799 * byte cause the contents to be either undefined or to stay unchanged.
800 * - 128 bytes. If less than 128 bytes are written, the rest will be
801 * erased. Each write to a 128-byte region will trigger an automatic erase
802 * before anything is written. Very uncommon behaviour and unsupported by
803 * this function.
804 * - 256 bytes. If less than 256 bytes are written, the contents of the
805 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000806 * Warning: This function assumes that @have and @want point to naturally
807 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000808 *
809 * @have buffer with current content
810 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000811 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000812 * @gran write granularity (enum, not count)
813 * @return 0 if no erase is needed, 1 otherwise
814 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700815static int need_erase(struct flashctx *flash, uint8_t *have, uint8_t *want,
Simon Glass4c214132013-07-16 10:09:28 -0600816 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000817{
hailfingerb91c08c2011-08-15 19:54:20 +0000818 int result = 0;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100819 unsigned int i;
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700820
hailfingerb247c7a2010-03-08 00:42:32 +0000821 switch (gran) {
822 case write_gran_1bit:
823 for (i = 0; i < len; i++)
824 if ((have[i] & want[i]) != want[i]) {
825 result = 1;
826 break;
827 }
828 break;
829 case write_gran_1byte:
830 for (i = 0; i < len; i++)
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100831 if ((have[i] != want[i]) && (have[i] != 0xff)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000832 result = 1;
833 break;
834 }
835 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100836 case write_gran_128bytes:
837 result = need_erase_gran_bytes(have, want, len, 128);
838 break;
hailfingerb247c7a2010-03-08 00:42:32 +0000839 case write_gran_256bytes:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100840 result = need_erase_gran_bytes(have, want, len, 256);
841 break;
842 case write_gran_264bytes:
843 result = need_erase_gran_bytes(have, want, len, 264);
844 break;
845 case write_gran_512bytes:
846 result = need_erase_gran_bytes(have, want, len, 512);
847 break;
848 case write_gran_528bytes:
849 result = need_erase_gran_bytes(have, want, len, 528);
850 break;
851 case write_gran_1024bytes:
852 result = need_erase_gran_bytes(have, want, len, 1024);
853 break;
854 case write_gran_1056bytes:
855 result = need_erase_gran_bytes(have, want, len, 1056);
856 break;
857 case write_gran_1byte_implicit_erase:
858 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
859 result = 0;
hailfingerb247c7a2010-03-08 00:42:32 +0000860 break;
hailfingerb437e282010-11-04 01:04:27 +0000861 default:
862 msg_cerr("%s: Unsupported granularity! Please report a bug at "
863 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000864 }
865 return result;
866}
867
hailfingerb437e282010-11-04 01:04:27 +0000868/**
869 * Check if the buffer @have needs to be programmed to get the content of @want.
870 * If yes, return 1 and fill in first_start with the start address of the
871 * write operation and first_len with the length of the first to-be-written
872 * chunk. If not, return 0 and leave first_start and first_len undefined.
873 *
874 * Warning: This function assumes that @have and @want point to naturally
875 * aligned regions.
876 *
877 * @have buffer with current content
878 * @want buffer with desired content
879 * @len length of the checked area
880 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000881 * @first_start offset of the first byte which needs to be written (passed in
882 * value is increased by the offset of the first needed write
883 * relative to have/want or unchanged if no write is needed)
884 * @return length of the first contiguous area which needs to be written
885 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000886 *
887 * FIXME: This function needs a parameter which tells it about coalescing
888 * in relation to the max write length of the programmer and the max write
889 * length of the chip.
890 */
stefanctc5eb8a92011-11-23 09:13:48 +0000891static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
892 unsigned int *first_start,
893 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000894{
stefanctc5eb8a92011-11-23 09:13:48 +0000895 int need_write = 0;
896 unsigned int rel_start = 0, first_len = 0;
897 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000898
hailfingerb437e282010-11-04 01:04:27 +0000899 switch (gran) {
900 case write_gran_1bit:
901 case write_gran_1byte:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100902 case write_gran_1byte_implicit_erase:
hailfinger90fcf9b2010-11-05 14:51:59 +0000903 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000904 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100905 case write_gran_128bytes:
906 stride = 128;
907 break;
hailfingerb437e282010-11-04 01:04:27 +0000908 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000909 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000910 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100911 case write_gran_264bytes:
912 stride = 264;
913 break;
914 case write_gran_512bytes:
915 stride = 512;
916 break;
917 case write_gran_528bytes:
918 stride = 528;
919 break;
920 case write_gran_1024bytes:
921 stride = 1024;
922 break;
923 case write_gran_1056bytes:
924 stride = 1056;
925 break;
hailfingerb437e282010-11-04 01:04:27 +0000926 default:
927 msg_cerr("%s: Unsupported granularity! Please report a bug at "
928 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000929 /* Claim that no write was needed. A write with unknown
930 * granularity is too dangerous to try.
931 */
932 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000933 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000934 for (i = 0; i < len / stride; i++) {
935 limit = min(stride, len - i * stride);
936 /* Are 'have' and 'want' identical? */
937 if (memcmp(have + i * stride, want + i * stride, limit)) {
938 if (!need_write) {
939 /* First location where have and want differ. */
940 need_write = 1;
941 rel_start = i * stride;
942 }
943 } else {
944 if (need_write) {
945 /* First location where have and want
946 * do not differ anymore.
947 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000948 break;
949 }
950 }
951 }
hailfingerffb7f382010-12-06 13:05:44 +0000952 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000953 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000954 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000955 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000956}
957
hailfinger0c515352009-11-23 12:55:31 +0000958/* This function generates various test patterns useful for testing controller
959 * and chip communication as well as chip behaviour.
960 *
961 * If a byte can be written multiple times, each time keeping 0-bits at 0
962 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
963 * is essentially an AND operation. That's also the reason why this function
964 * provides the result of AND between various patterns.
965 *
966 * Below is a list of patterns (and their block length).
967 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
968 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
969 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
970 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
971 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
972 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
973 * Pattern 6 is 00 (1 Byte)
974 * Pattern 7 is ff (1 Byte)
975 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
976 * byte block.
977 *
978 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
979 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
980 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
981 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
982 * Pattern 12 is 00 (1 Byte)
983 * Pattern 13 is ff (1 Byte)
984 * Patterns 8-13 have no block number.
985 *
986 * Patterns 0-3 are created to detect and efficiently diagnose communication
987 * slips like missed bits or bytes and their repetitive nature gives good visual
988 * cues to the person inspecting the results. In addition, the following holds:
989 * AND Pattern 0/1 == Pattern 4
990 * AND Pattern 2/3 == Pattern 5
991 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
992 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
993 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
994 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
995 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
996 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
997 * Besides that, they provide for bit testing of the last two bytes of every
998 * 256 byte block which contains the block number for patterns 0-6.
999 * Patterns 10-11 are special purpose for detecting subblock aliasing with
1000 * block sizes >256 bytes (some Dataflash chips etc.)
1001 * AND Pattern 8/9 == Pattern 12
1002 * AND Pattern 10/11 == Pattern 12
1003 * Pattern 13 is the completely erased state.
1004 * None of the patterns can detect aliasing at boundaries which are a multiple
1005 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
1006 */
1007int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
1008{
1009 int i;
1010
1011 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +00001012 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +00001013 return 1;
1014 }
1015
1016 switch (variant) {
1017 case 0:
1018 for (i = 0; i < size; i++)
1019 buf[i] = (i & 0xf) << 4 | 0x5;
1020 break;
1021 case 1:
1022 for (i = 0; i < size; i++)
1023 buf[i] = (i & 0xf) << 4 | 0xa;
1024 break;
1025 case 2:
1026 for (i = 0; i < size; i++)
1027 buf[i] = 0x50 | (i & 0xf);
1028 break;
1029 case 3:
1030 for (i = 0; i < size; i++)
1031 buf[i] = 0xa0 | (i & 0xf);
1032 break;
1033 case 4:
1034 for (i = 0; i < size; i++)
1035 buf[i] = (i & 0xf) << 4;
1036 break;
1037 case 5:
1038 for (i = 0; i < size; i++)
1039 buf[i] = i & 0xf;
1040 break;
1041 case 6:
1042 memset(buf, 0x00, size);
1043 break;
1044 case 7:
1045 memset(buf, 0xff, size);
1046 break;
1047 case 8:
1048 for (i = 0; i < size; i++)
1049 buf[i] = i & 0xff;
1050 break;
1051 case 9:
1052 for (i = 0; i < size; i++)
1053 buf[i] = ~(i & 0xff);
1054 break;
1055 case 10:
1056 for (i = 0; i < size % 2; i++) {
1057 buf[i * 2] = (i >> 8) & 0xff;
1058 buf[i * 2 + 1] = i & 0xff;
1059 }
1060 if (size & 0x1)
1061 buf[i * 2] = (i >> 8) & 0xff;
1062 break;
1063 case 11:
1064 for (i = 0; i < size % 2; i++) {
1065 buf[i * 2] = ~((i >> 8) & 0xff);
1066 buf[i * 2 + 1] = ~(i & 0xff);
1067 }
1068 if (size & 0x1)
1069 buf[i * 2] = ~((i >> 8) & 0xff);
1070 break;
1071 case 12:
1072 memset(buf, 0x00, size);
1073 break;
1074 case 13:
1075 memset(buf, 0xff, size);
1076 break;
1077 }
1078
1079 if ((variant >= 0) && (variant <= 7)) {
1080 /* Write block number in the last two bytes of each 256-byte
1081 * block, big endian for easier reading of the hexdump.
1082 * Note that this wraps around for chips larger than 2^24 bytes
1083 * (16 MB).
1084 */
1085 for (i = 0; i < size / 256; i++) {
1086 buf[i * 256 + 254] = (i >> 8) & 0xff;
1087 buf[i * 256 + 255] = i & 0xff;
1088 }
1089 }
1090
1091 return 0;
1092}
1093
hailfingeraec9c962009-10-31 01:53:09 +00001094int check_max_decode(enum chipbustype buses, uint32_t size)
1095{
1096 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001097
1098 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001099 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001100 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001101 "size %u kB of chipset/board/programmer "
1102 "for %s interface, "
1103 "probe/read/erase/write may fail. ", size / 1024,
1104 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001105 }
hailfingere1e41ea2011-07-27 07:13:06 +00001106 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001107 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001108 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001109 "size %u kB of chipset/board/programmer "
1110 "for %s interface, "
1111 "probe/read/erase/write may fail. ", size / 1024,
1112 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001113 }
hailfingere1e41ea2011-07-27 07:13:06 +00001114 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001115 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001116 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001117 "size %u kB of chipset/board/programmer "
1118 "for %s interface, "
1119 "probe/read/erase/write may fail. ", size / 1024,
1120 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001121 }
hailfingere1e41ea2011-07-27 07:13:06 +00001122 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001123 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001124 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001125 "size %u kB of chipset/board/programmer "
1126 "for %s interface, "
1127 "probe/read/erase/write may fail. ", size / 1024,
1128 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001129 }
1130 if (!limitexceeded)
1131 return 0;
1132 /* Sometimes chip and programmer have more than one bus in common,
1133 * and the limit is not exceeded on all buses. Tell the user.
1134 */
1135 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001136 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001137 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001138 "interface which can support a chip of this size. "
1139 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001140 return 1;
1141}
1142
Edward O'Callaghan8488f122019-06-17 12:38:15 +10001143/*
1144 * Return a string corresponding to the bustype parameter.
1145 * Memory is obtained with malloc() and must be freed with free() by the caller.
1146 */
1147char *flashbuses_to_text(enum chipbustype bustype)
1148{
1149 char *ret = calloc(1, 1);
1150 /*
1151 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
1152 * will cease to exist and should be eliminated here as well.
1153 */
1154 if (bustype == BUS_NONSPI) {
1155 ret = strcat_realloc(ret, "Non-SPI, ");
1156 } else {
1157 if (bustype & BUS_PARALLEL)
1158 ret = strcat_realloc(ret, "Parallel, ");
1159 if (bustype & BUS_LPC)
1160 ret = strcat_realloc(ret, "LPC, ");
1161 if (bustype & BUS_FWH)
1162 ret = strcat_realloc(ret, "FWH, ");
1163 if (bustype & BUS_SPI)
1164 ret = strcat_realloc(ret, "SPI, ");
1165 if (bustype & BUS_PROG)
1166 ret = strcat_realloc(ret, "Programmer-specific, ");
1167 if (bustype == BUS_NONE)
1168 ret = strcat_realloc(ret, "None, ");
1169 }
1170 /* Kill last comma. */
1171 ret[strlen(ret) - 2] = '\0';
1172 ret = realloc(ret, strlen(ret) + 1);
1173 return ret;
1174}
1175
Edward O'Callaghan20596a82019-06-13 14:47:03 +10001176int probe_flash(struct registered_master *mst, int startchip,
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001177 struct flashctx *flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001178{
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001179 const struct flashchip *chip, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001180 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001181 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001182 uint32_t size;
1183 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001184 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001185
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301186 /* Based on the host controller interface that a platform
1187 * needs to use (hwseq or swseq),
1188 * set the flashchips list here.
1189 */
Edward O'Callaghane3e30562019-09-03 13:10:58 +10001190 switch (g_ich_generation) {
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301191 case CHIPSET_100_SERIES_SUNRISE_POINT:
Edward O'Callaghan272b27c2020-05-26 17:06:04 +10001192 case CHIPSET_APOLLO_LAKE:
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301193 flash_list = flashchips_hwseq;
1194 break;
1195 default:
1196 flash_list = flashchips;
1197 break;
1198 }
1199
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001200 for (chip = flash_list + startchip; chip && chip->name; chip++) {
1201 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001202 continue;
Craig Hesling65eb8812019-08-01 09:33:56 -07001203 buses_common = buses_supported & chip->bustype;
Edward O'Callaghan4b940572019-08-02 01:44:47 +10001204 if (!buses_common)
hailfinger18bd4cc2011-06-17 22:38:53 +00001205 continue;
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001206 /* Only probe for SPI25 chips by default. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001207 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001208 continue;
hailfinger18bd4cc2011-06-17 22:38:53 +00001209 msg_gdbg("Probing for %s %s, %d kB: ",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001210 chip->vendor, chip->name, chip->total_size);
1211 if (!chip->probe && !force) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001212 msg_gdbg("failed! flashrom has no probe function for "
1213 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001214 continue;
1215 }
stepan782fb172007-04-06 11:58:03 +00001216
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001217 size = chip->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001218 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001219
hailfinger48ed3e22011-05-04 00:39:50 +00001220 /* Start filling in the dynamic data. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001221 flash->chip = calloc(1, sizeof(struct flashchip));
1222 if (!flash->chip) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001223 msg_gerr("Out of memory!\n");
1224 exit(1);
1225 }
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001226 memcpy(flash->chip, chip, sizeof(struct flashchip));
1227 flash->mst = mst;
hailfinger48ed3e22011-05-04 00:39:50 +00001228
Edward O'Callaghan7bd44c62019-11-13 12:44:49 +11001229 base = flashbase ? flashbase : (0xffffffff - size + 1);
1230 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001231
stugec1e55fe2008-07-02 17:15:47 +00001232 if (force)
1233 break;
stepanc98b80b2006-03-16 16:57:41 +00001234
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001235 if (flash->chip->probe(flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001236 goto notfound;
1237
hailfinger48ed3e22011-05-04 00:39:50 +00001238 /* If this is the first chip found, accept it.
1239 * If this is not the first chip found, accept it only if it is
1240 * a non-generic match.
1241 * We could either make chipcount global or provide it as
1242 * parameter, or we assume that startchip==0 means this call to
1243 * probe_flash() is the first one and thus no chip has been
1244 * found before.
1245 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001246 if (startchip == 0 || flash->chip->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001247 break;
1248
stuge56300c32008-09-03 23:10:05 +00001249notfound:
Martin Roth80be5552019-12-23 15:22:11 -07001250 if (size)
1251 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001252 free(flash->chip);
1253 flash->chip = NULL;
rminnich8d3ff912003-10-25 17:01:29 +00001254 }
uwebe4477b2007-08-23 16:08:21 +00001255
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001256 if (!chip || !chip->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001257 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001258
hailfingere11396b2011-03-08 00:09:11 +00001259#if CONFIG_INTERNAL == 1
1260 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001261 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001262 else
1263#endif
stepan3e7aeae2011-01-19 06:21:54 +00001264 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001265
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001266 tmp = flashbuses_to_text(chip->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001267 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001268 force ? "Assuming" : "Found", flash->chip->vendor,
1269 flash->chip->name, flash->chip->total_size, tmp,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001270 location);
stefanct588b6d22011-06-26 20:45:35 +00001271 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001272
hailfinger0f4c3952010-12-02 21:59:42 +00001273 /* Flash registers will not be mapped if the chip was forced. Lock info
1274 * may be stored in registers, so avoid lock info printing.
1275 */
1276 if (!force)
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001277 if (flash->chip->printlock)
1278 flash->chip->printlock(flash);
snelson1ee293c2010-02-19 00:52:10 +00001279
hailfinger48ed3e22011-05-04 00:39:50 +00001280 /* Return position of matching chip. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001281 return chip - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001282}
1283
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001284static int verify_flash(struct flashctx *flash,
1285 struct action_descriptor *descriptor,
1286 int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001287{
hailfingerb0f4d122009-06-24 08:20:45 +00001288 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +01001289 unsigned int total_size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001290 uint8_t *buf = descriptor->newcontents;
rminnich8d3ff912003-10-25 17:01:29 +00001291
snelsone42c3802010-05-07 20:09:04 +00001292 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001293
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001294 if (verify_it == VERIFY_PARTIAL) {
1295 struct processing_unit *pu = descriptor->processing_units;
1296
1297 /* Verify only areas which were written. */
1298 while (pu->num_blocks) {
1299 ret = verify_range(flash, buf + pu->offset, pu->offset,
1300 pu->block_size * pu->num_blocks,
1301 NULL);
1302 if (ret)
1303 break;
1304 pu++;
1305 }
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001306 } else {
1307 ret = verify_range(flash, buf, 0, total_size, NULL);
1308 }
uwef6641642007-05-09 10:17:44 +00001309
David Hendricks1ed1d352011-11-23 17:54:37 -08001310 if (ret == ACCESS_DENIED) {
1311 msg_gdbg("Could not fully verify due to access error, ");
1312 if (access_denied_action == error_ignore) {
1313 msg_gdbg("ignoring\n");
1314 ret = 0;
1315 } else {
1316 msg_gdbg("aborting\n");
1317 }
1318 }
1319
hailfingerb0f4d122009-06-24 08:20:45 +00001320 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001321 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001322
hailfingerb0f4d122009-06-24 08:20:45 +00001323 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001324}
1325
uwe8d342eb2011-07-28 08:13:25 +00001326int read_buf_from_file(unsigned char *buf, unsigned long size,
1327 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001328{
1329 unsigned long numbytes;
1330 FILE *image;
1331 struct stat image_stat;
1332
Vincent Palatin7ab23932014-10-01 12:09:16 -07001333 if (!strncmp(filename, "-", sizeof("-")))
1334 image = fdopen(STDIN_FILENO, "rb");
1335 else
1336 image = fopen(filename, "rb");
1337 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001338 perror(filename);
1339 return 1;
1340 }
1341 if (fstat(fileno(image), &image_stat) != 0) {
1342 perror(filename);
1343 fclose(image);
1344 return 1;
1345 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001346 if ((image_stat.st_size != size) &&
1347 (strncmp(filename, "-", sizeof("-")))) {
Mike Frysinger62c794d2017-05-29 12:02:45 -04001348 msg_gerr("Error: Image size doesn't match: stat %jd bytes, "
1349 "wanted %ld!\n", (intmax_t)image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001350 fclose(image);
1351 return 1;
1352 }
1353 numbytes = fread(buf, 1, size, image);
1354 if (fclose(image)) {
1355 perror(filename);
1356 return 1;
1357 }
1358 if (numbytes != size) {
1359 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1360 "wanted %ld!\n", numbytes, size);
1361 return 1;
1362 }
1363 return 0;
1364}
1365
uwe8d342eb2011-07-28 08:13:25 +00001366int write_buf_to_file(unsigned char *buf, unsigned long size,
1367 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001368{
1369 unsigned long numbytes;
1370 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001371
1372 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001373 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001374 return 1;
1375 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001376 if (!strncmp(filename, "-", sizeof("-")))
1377 image = fdopen(STDOUT_FILENO, "wb");
1378 else
1379 image = fopen(filename, "wb");
1380 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001381 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001382 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001383 }
hailfingerd219a232009-01-28 00:27:54 +00001384
hailfingerd219a232009-01-28 00:27:54 +00001385 numbytes = fwrite(buf, 1, size, image);
1386 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001387 if (numbytes != size) {
1388 msg_gerr("File %s could not be written completely.\n",
1389 filename);
hailfingerd219a232009-01-28 00:27:54 +00001390 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001391 }
hailfingerd219a232009-01-28 00:27:54 +00001392 return 0;
1393}
1394
David Hendrickse3451942013-03-21 17:23:29 -07001395/*
1396 * read_flash - wrapper for flash->read() with additional high-level policy
1397 *
1398 * @flash flash chip
1399 * @buf buffer to store data in
1400 * @start start address
1401 * @len number of bytes to read
1402 *
1403 * This wrapper simplifies most cases when the flash chip needs to be read
1404 * since policy decisions such as non-fatal error handling is centralized.
1405 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001406int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001407 unsigned int start, unsigned int len)
1408{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001409 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001410
Patrick Georgif3fa2992017-02-02 16:24:44 +01001411 if (!flash || !flash->chip->read)
David Hendrickse3451942013-03-21 17:23:29 -07001412 return -1;
1413
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001414 msg_cdbg("%#06x-%#06x:R ", start, start + len - 1);
1415
Patrick Georgif3fa2992017-02-02 16:24:44 +01001416 ret = flash->chip->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001417 if (ret) {
1418 if (ignore_error(ret)) {
1419 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1420 start, start + len - 1);
1421 ret = 0;
1422 } else {
1423 msg_gdbg("failed to read 0x%x-0x%x\n",
1424 start, start + len - 1);
1425 }
1426 }
1427
1428 return ret;
1429}
1430
David Hendricks7c8a1612013-04-26 19:14:44 -07001431/*
1432 * write_flash - wrapper for flash->write() with additional high-level policy
1433 *
1434 * @flash flash chip
1435 * @buf buffer to write to flash
1436 * @start start address in flash
1437 * @len number of bytes to write
1438 *
1439 * TODO: Look up regions that are write-protected and avoid attempt to write
1440 * to them at all.
1441 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001442int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001443 unsigned int start, unsigned int len)
1444{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001445 if (!flash || !flash->chip->write)
David Hendricks7c8a1612013-04-26 19:14:44 -07001446 return -1;
1447
Patrick Georgif3fa2992017-02-02 16:24:44 +01001448 return flash->chip->write(flash, buf, start, len);
David Hendricks7c8a1612013-04-26 19:14:44 -07001449}
1450
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001451int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001452{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001453 unsigned long size = flash->chip->total_size * 1024;
Richard Hughes74eec602018-12-19 15:30:39 +00001454 unsigned char *buf = calloc(size, sizeof(unsigned char));
hailfinger42a850a2010-07-13 23:56:13 +00001455 int ret = 0;
1456
1457 msg_cinfo("Reading flash... ");
1458 if (!buf) {
1459 msg_gerr("Memory allocation failed!\n");
1460 msg_cinfo("FAILED.\n");
1461 return 1;
1462 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001463
1464 /* To support partial read, fill buffer to all 0xFF at beginning to make
1465 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001466 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001467
Patrick Georgif3fa2992017-02-02 16:24:44 +01001468 if (!flash->chip->read) {
hailfinger42a850a2010-07-13 23:56:13 +00001469 msg_cerr("No read function available for this flash chip.\n");
1470 ret = 1;
1471 goto out_free;
1472 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001473
1474 /* First try to handle partial read case, rather than read the whole
1475 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001476 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001477 if (ret < 0) {
1478 msg_cerr("Partial read operation failed!\n");
1479 ret = 1;
1480 goto out_free;
1481 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001482 int num_regions = get_num_include_args();
1483
1484 if (ret != num_regions) {
1485 msg_cerr("Requested %d regions, but only read %d\n",
1486 num_regions, ret);
1487 ret = 1;
1488 goto out_free;
1489 }
1490
1491 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001492 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001493 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001494 msg_cerr("Read operation failed!\n");
1495 ret = 1;
1496 goto out_free;
1497 }
hailfinger42a850a2010-07-13 23:56:13 +00001498 }
1499
David Hendricksdf29a832013-06-28 14:33:51 -07001500 if (filename)
1501 ret = write_buf_to_file(buf, size, filename);
1502
hailfinger42a850a2010-07-13 23:56:13 +00001503out_free:
1504 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001505 if (ret)
1506 msg_cerr("FAILED.");
1507 else
1508 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001509 return ret;
1510}
1511
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001512/* Even if an error is found, the function will keep going and check the rest. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001513static int selfcheck_eraseblocks(const struct flashchip *chip)
hailfinger45177872010-01-18 08:14:43 +00001514{
hailfingerb91c08c2011-08-15 19:54:20 +00001515 int i, j, k;
1516 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001517
1518 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1519 unsigned int done = 0;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001520 struct block_eraser eraser = chip->block_erasers[k];
hailfinger45177872010-01-18 08:14:43 +00001521
1522 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1523 /* Blocks with zero size are bugs in flashchips.c. */
1524 if (eraser.eraseblocks[i].count &&
1525 !eraser.eraseblocks[i].size) {
1526 msg_gerr("ERROR: Flash chip %s erase function "
1527 "%i region %i has size 0. Please report"
1528 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001529 chip->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001530 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001531 }
1532 /* Blocks with zero count are bugs in flashchips.c. */
1533 if (!eraser.eraseblocks[i].count &&
1534 eraser.eraseblocks[i].size) {
1535 msg_gerr("ERROR: Flash chip %s erase function "
1536 "%i region %i has count 0. Please report"
1537 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001538 chip->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001539 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001540 }
1541 done += eraser.eraseblocks[i].count *
1542 eraser.eraseblocks[i].size;
1543 }
hailfinger9fed35d2010-01-19 06:42:46 +00001544 /* Empty eraseblock definition with erase function. */
1545 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001546 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001547 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001548 if (!done)
1549 continue;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001550 if (done != chip->total_size * 1024) {
hailfinger45177872010-01-18 08:14:43 +00001551 msg_gerr("ERROR: Flash chip %s erase function %i "
1552 "region walking resulted in 0x%06x bytes total,"
1553 " expected 0x%06x bytes. Please report a bug at"
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001554 " flashrom@flashrom.org\n", chip->name, k,
1555 done, chip->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001556 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001557 }
hailfinger9fed35d2010-01-19 06:42:46 +00001558 if (!eraser.block_erase)
1559 continue;
1560 /* Check if there are identical erase functions for different
1561 * layouts. That would imply "magic" erase functions. The
1562 * easiest way to check this is with function pointers.
1563 */
uwef6f94d42010-03-13 17:28:29 +00001564 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001565 if (eraser.block_erase ==
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001566 chip->block_erasers[j].block_erase) {
hailfinger9fed35d2010-01-19 06:42:46 +00001567 msg_gerr("ERROR: Flash chip %s erase function "
1568 "%i and %i are identical. Please report"
1569 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001570 chip->name, k, j);
hailfinger9fed35d2010-01-19 06:42:46 +00001571 ret = 1;
1572 }
uwef6f94d42010-03-13 17:28:29 +00001573 }
hailfinger45177872010-01-18 08:14:43 +00001574 }
hailfinger9fed35d2010-01-19 06:42:46 +00001575 return ret;
hailfinger45177872010-01-18 08:14:43 +00001576}
1577
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001578static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001579 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001580 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001581 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001582 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001583 unsigned int addr,
1584 unsigned int len))
1585{
stefanctc5eb8a92011-11-23 09:13:48 +00001586 unsigned int starthere = 0, lenhere = 0;
1587 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001588 int block_was_erased = 0;
Edward O'Callaghan10e63d92019-06-17 14:12:52 +10001589 enum write_granularity gran = flash->chip->gran;
hailfingerb437e282010-11-04 01:04:27 +00001590
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001591 /*
1592 * curcontents and newcontents are opaque to walk_eraseregions, and
1593 * need to be adjusted here to keep the impression of proper
1594 * abstraction
hailfingerb437e282010-11-04 01:04:27 +00001595 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001596
hailfinger90fcf9b2010-11-05 14:51:59 +00001597 curcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001598
hailfingerb437e282010-11-04 01:04:27 +00001599 newcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001600
hailfingerb437e282010-11-04 01:04:27 +00001601 msg_cdbg(":");
Simon Glass4c214132013-07-16 10:09:28 -06001602 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001603 content_has_changed |= 1;
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001604 msg_cdbg(" E");
hailfingerb437e282010-11-04 01:04:27 +00001605 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001606 if (ret) {
1607 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001608 msg_cdbg(" DENIED");
David Hendricks1ed1d352011-11-23 17:54:37 -08001609 else
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001610 msg_cerr(" ERASE_FAILED\n");
hailfingerb437e282010-11-04 01:04:27 +00001611 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001612 }
1613
David Hendricks0954ffc2015-11-13 15:15:44 -08001614 if (programmer_table[programmer].paranoid) {
1615 if (check_erased_range(flash, start, len)) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001616 msg_cerr(" ERASE_FAILED\n");
David Hendricks0954ffc2015-11-13 15:15:44 -08001617 return -1;
1618 }
hailfingerac8e3182011-06-26 17:04:16 +00001619 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001620
hailfinger90fcf9b2010-11-05 14:51:59 +00001621 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001622 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001623 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001624 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001625 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001626 /* get_next_write() sets starthere to a new value after the call. */
1627 while ((lenhere = get_next_write(curcontents + starthere,
1628 newcontents + starthere,
1629 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001630 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001631 if (!writecount++)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001632 msg_cdbg(" W");
hailfingerb437e282010-11-04 01:04:27 +00001633 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001634 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001635 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001636 if (ret) {
1637 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001638 msg_cdbg(" DENIED");
hailfingerb437e282010-11-04 01:04:27 +00001639 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001640 }
David Hendricks048b38c2016-03-28 18:47:06 -07001641
1642 /*
1643 * If the block needed to be erased and was erased successfully
1644 * then we can assume that we didn't run into any write-
1645 * protected areas. Otherwise, we need to verify each page to
1646 * ensure it was successfully written and abort if we encounter
1647 * any errors.
1648 */
1649 if (programmer_table[programmer].paranoid && !block_was_erased) {
1650 if (verify_range(flash, newcontents + starthere,
1651 start + starthere, lenhere, "WRITE"))
1652 return -1;
1653 }
1654
hailfingerb437e282010-11-04 01:04:27 +00001655 starthere += lenhere;
1656 skip = 0;
1657 }
1658 if (skip)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001659 msg_cdbg(" SKIP");
hailfingerb437e282010-11-04 01:04:27 +00001660 return ret;
1661}
1662
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001663/*
1664 * Function to process processing units accumulated in the action descriptor.
1665 *
1666 * @flash pointer to the flash context to operate on
1667 * @do_something helper function which can erase and program a section of the
1668 * flash chip. It receives the flash context, offset and length
1669 * of the area to erase/program, before and after contents (to
1670 * decide what exactly needs to be erased and or programmed)
1671 * and a pointer to the erase function which can operate on the
1672 * proper granularity.
1673 * @descriptor action descriptor including pointers to before and after
1674 * contents and an array of processing actions to take.
1675 *
1676 * Returns zero on success or an error code.
1677 */
1678static int walk_eraseregions(struct flashctx *flash,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001679 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001680 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001681 unsigned int len,
1682 uint8_t *param1,
1683 uint8_t *param2,
1684 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001685 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001686 unsigned int addr,
1687 unsigned int len)),
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001688 struct action_descriptor *descriptor)
hailfinger2b8c9382010-07-13 00:37:19 +00001689{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001690 struct processing_unit *pu;
1691 int rc = 0;
1692 static int print_comma;
uwe8d342eb2011-07-28 08:13:25 +00001693
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001694 for (pu = descriptor->processing_units; pu->num_blocks; pu++) {
1695 unsigned base = pu->offset;
1696 unsigned top = pu->offset + pu->block_size * pu->num_blocks;
David Hendricks605544b2015-08-15 16:32:58 -07001697
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001698 while (base < top) {
David Hendricks605544b2015-08-15 16:32:58 -07001699
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001700 if (print_comma)
hailfingerb437e282010-11-04 01:04:27 +00001701 msg_cdbg(", ");
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001702 else
1703 print_comma = 1;
1704
1705 msg_cdbg("0x%06x-0x%06zx", base, base + pu->block_size - 1);
1706
1707 rc = do_something(flash, base,
1708 pu->block_size,
1709 descriptor->oldcontents,
1710 descriptor->newcontents,
1711 flash->chip->block_erasers[pu->block_eraser_index].block_erase);
1712
David Hendricks1ed1d352011-11-23 17:54:37 -08001713 if (rc) {
1714 if (ignore_error(rc))
1715 rc = 0;
1716 else
1717 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001718 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001719 base += pu->block_size;
hailfinger2b8c9382010-07-13 00:37:19 +00001720 }
1721 }
hailfingerb437e282010-11-04 01:04:27 +00001722 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001723 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001724}
1725
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001726static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001727{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001728 struct block_eraser eraser = flash->chip->block_erasers[k];
hailfingercf848f12010-12-05 15:14:44 +00001729
1730 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1731 if (log)
1732 msg_cdbg("not defined. ");
1733 return 1;
1734 }
1735 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1736 if (log)
1737 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001738 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001739 return 1;
1740 }
1741 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1742 if (log)
1743 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001744 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001745 return 1;
1746 }
1747 return 0;
1748}
1749
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001750int erase_and_write_flash(struct flashctx *flash,
1751 struct action_descriptor *descriptor)
hailfingerd219a232009-01-28 00:27:54 +00001752{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001753 int ret = 1;
hailfingercf848f12010-12-05 15:14:44 +00001754
hailfingercf848f12010-12-05 15:14:44 +00001755 msg_cinfo("Erasing and writing flash chip... ");
hailfingerb437e282010-11-04 01:04:27 +00001756
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001757 ret = walk_eraseregions(flash, &erase_and_write_block_helper, descriptor);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001758
hailfinger7df21362009-09-05 02:30:58 +00001759 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001760 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001761 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001762 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001763 }
1764 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001765}
1766
hailfinger4c47e9d2010-10-19 22:06:20 +00001767void nonfatal_help_message(void)
1768{
1769 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1770 "This means we have to add special support for your board, "
1771 "programmer or flash chip.\n"
1772 "Please report this on IRC at irc.freenode.net (channel "
1773 "#flashrom) or\n"
1774 "mail flashrom@flashrom.org!\n"
1775 "-------------------------------------------------------------"
1776 "------------------\n"
1777 "You may now reboot or simply leave the machine running.\n");
1778}
1779
uweb34ec9f2009-10-01 18:40:02 +00001780void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001781{
snelsone42c3802010-05-07 20:09:04 +00001782 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001783 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001784 "mail flashrom@flashrom.org with FAILED: your board name in "
1785 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001786 "-------------------------------------------------------------"
1787 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001788 "DO NOT REBOOT OR POWEROFF!\n");
1789}
1790
uwe8d342eb2011-07-28 08:13:25 +00001791/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001792void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001793{
1794 enum programmer p;
1795 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001796 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001797 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001798 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001799 }
Simon Glass8dc82732013-07-16 10:13:51 -06001800 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001801}
1802
hailfingerf79d1712010-10-06 23:48:34 +00001803void list_programmers_linebreak(int startcol, int cols, int paren)
1804{
1805 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001806 int pnamelen;
1807 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001808 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001809 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001810
1811 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1812 pname = programmer_table[p].name;
1813 pnamelen = strlen(pname);
1814 if (remaining - pnamelen - 2 < 0) {
1815 if (firstline)
1816 firstline = 0;
1817 else
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001818 msg_ginfo("\n");
hailfingerf79d1712010-10-06 23:48:34 +00001819 for (i = 0; i < startcol; i++)
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001820 msg_ginfo(" ");
hailfingerf79d1712010-10-06 23:48:34 +00001821 remaining = cols - startcol;
1822 } else {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001823 msg_ginfo(" ");
hailfingerf79d1712010-10-06 23:48:34 +00001824 remaining--;
1825 }
1826 if (paren && (p == 0)) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001827 msg_ginfo("(");
hailfingerf79d1712010-10-06 23:48:34 +00001828 remaining--;
1829 }
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001830 msg_ginfo("%s", pname);
hailfingerf79d1712010-10-06 23:48:34 +00001831 remaining -= pnamelen;
1832 if (p < PROGRAMMER_INVALID - 1) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001833 msg_ginfo(",");
hailfingerf79d1712010-10-06 23:48:34 +00001834 remaining--;
1835 } else {
1836 if (paren)
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001837 msg_ginfo(")");
1838 msg_ginfo("\n");
hailfingerf79d1712010-10-06 23:48:34 +00001839 }
1840 }
1841}
1842
hailfinger3b471632010-03-27 16:36:40 +00001843void print_sysinfo(void)
1844{
David Hendricksc6c9f822010-11-03 15:07:01 -07001845 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001846#if HAVE_UTSNAME == 1
1847 struct utsname osinfo;
1848 uname(&osinfo);
1849
David Hendricksc6c9f822010-11-03 15:07:01 -07001850 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001851 osinfo.machine);
1852#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001853 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001854#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001855}
1856
1857void print_buildinfo(void)
1858{
1859 msg_gdbg("flashrom was built with");
hailfinger3b471632010-03-27 16:36:40 +00001860#if NEED_PCI == 1
1861#ifdef PCILIB_VERSION
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001862 msg_gdbg(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001863#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001864 msg_gdbg(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001865#endif
1866#endif
1867#ifdef __clang__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001868 msg_gdbg(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001869#ifdef __clang_version__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001870 msg_gdbg(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001871#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001872 msg_gdbg(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001873#endif
hailfinger3b471632010-03-27 16:36:40 +00001874#elif defined(__GNUC__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001875 msg_gdbg(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001876#ifdef __VERSION__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001877 msg_gdbg(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001878#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001879 msg_gdbg(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001880#endif
1881#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001882 msg_gdbg(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001883#endif
1884#if defined (__FLASHROM_LITTLE_ENDIAN__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001885 msg_gdbg(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001886#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001887 msg_gdbg(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001888#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001889 msg_gdbg("\n");
hailfinger3b471632010-03-27 16:36:40 +00001890}
1891
uwefdeca092008-01-21 15:24:22 +00001892void print_version(void)
1893{
David Hendricksc6c9f822010-11-03 15:07:01 -07001894 /* send to stderr for chromium os */
1895 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001896 print_sysinfo();
David Hendricks07268292016-09-14 16:05:58 -07001897 msg_gerr("\n");
uwefdeca092008-01-21 15:24:22 +00001898}
1899
hailfinger74819ad2010-05-15 15:04:37 +00001900void print_banner(void)
1901{
1902 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001903 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001904 msg_ginfo("\n");
1905}
1906
hailfingerc77acb52009-12-24 02:15:55 +00001907int selfcheck(void)
1908{
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001909 unsigned int i;
hailfinger45177872010-01-18 08:14:43 +00001910 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001911
1912 /* Safety check. Instead of aborting after the first error, check
1913 * if more errors exist.
1914 */
hailfingerc77acb52009-12-24 02:15:55 +00001915 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001916 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001917 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001918 }
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001919 /* It would be favorable if we could check for the correct layout (especially termination) of various
1920 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1921 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1922 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1923 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1924 * checks below.
1925 */
1926 if (flashchips_size <= 1 || flashchips[flashchips_size-1].name != NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001927 msg_gerr("Flashchips table miscompilation!\n");
1928 ret = 1;
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001929 } else {
1930 for (i = 0; i < flashchips_size - 1; i++) {
1931 const struct flashchip *chip = &flashchips[i];
1932 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1933 ret = 1;
1934 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
1935 "Please report a bug at flashrom@flashrom.org\n", i,
1936 chip->name == NULL ? "unnamed" : chip->name);
1937 }
1938 if (selfcheck_eraseblocks(chip))
1939 ret = 1;
1940 }
stefanct6d836ba2011-05-26 01:35:19 +00001941 }
stefanct6d836ba2011-05-26 01:35:19 +00001942
hailfinger45177872010-01-18 08:14:43 +00001943 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001944}
1945
hailfinger771fc182010-10-15 00:01:14 +00001946/* FIXME: This function signature needs to be improved once doit() has a better
1947 * function signature.
1948 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001949int 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 +00001950{
Patrick Georgiac3423f2017-02-03 20:58:06 +01001951 const struct flashchip *chip = flash->chip;
1952
hailfinger771fc182010-10-15 00:01:14 +00001953 if (!programmer_may_write && (write_it || erase_it)) {
1954 msg_perr("Write/erase is not working yet on your programmer in "
1955 "its current configuration.\n");
1956 /* --force is the wrong approach, but it's the best we can do
1957 * until the generic programmer parameter parser is merged.
1958 */
1959 if (!force)
1960 return 1;
1961 msg_cerr("Continuing anyway.\n");
1962 }
1963
1964 if (read_it || erase_it || write_it || verify_it) {
1965 /* Everything needs read. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001966 if (chip->tested.read == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001967 msg_cerr("Read is not working on this chip. ");
1968 if (!force)
1969 return 1;
1970 msg_cerr("Continuing anyway.\n");
1971 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01001972 if (!chip->read) {
hailfinger771fc182010-10-15 00:01:14 +00001973 msg_cerr("flashrom has no read function for this "
1974 "flash chip.\n");
1975 return 1;
1976 }
1977 }
1978 if (erase_it || write_it) {
1979 /* Write needs erase. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01001980 if (chip->tested.erase == NA) {
1981 msg_cerr("Erase is not possible on this chip.\n");
1982 return 1;
1983 }
1984 if (chip->tested.erase == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00001985 msg_cerr("Erase is not working on this chip. ");
1986 if (!force)
1987 return 1;
1988 msg_cerr("Continuing anyway.\n");
1989 }
stefancte1c5acf2011-07-04 07:27:17 +00001990 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001991 msg_cerr("flashrom has no erase function for this "
1992 "flash chip.\n");
1993 return 1;
1994 }
hailfinger771fc182010-10-15 00:01:14 +00001995 }
1996 if (write_it) {
Patrick Georgiac3423f2017-02-03 20:58:06 +01001997 if (chip->tested.write == NA) {
1998 msg_cerr("Write is not possible on this chip.\n");
1999 return 1;
2000 }
2001 if (chip->tested.write == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00002002 msg_cerr("Write is not working on this chip. ");
2003 if (!force)
2004 return 1;
2005 msg_cerr("Continuing anyway.\n");
2006 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01002007 if (!chip->write) {
hailfinger771fc182010-10-15 00:01:14 +00002008 msg_cerr("flashrom has no write function for this "
2009 "flash chip.\n");
2010 return 1;
2011 }
2012 }
2013 return 0;
2014}
2015
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002016/*
2017 * Function to erase entire flash chip.
2018 *
2019 * @flashctx pointer to the flash context to use
2020 * @oldcontents pointer to the buffer including current chip contents, to
2021 * decide which areas do in fact need to be erased
2022 * @size the size of the flash chip, in bytes.
2023 *
2024 * Returns zero on success or an error code.
2025 */
2026static int erase_chip(struct flashctx *flash, void *oldcontents,
2027 void *newcontents, size_t size)
2028{
2029 /*
2030 * To make sure that the chip is fully erased, let's cheat and create
2031 * a descriptor where the new contents are all erased.
2032 */
2033 struct action_descriptor *fake_descriptor;
2034 int ret = 0;
2035
2036 fake_descriptor = prepare_action_descriptor(flash, oldcontents,
2037 newcontents, 1);
2038 /* FIXME: Do we really want the scary warning if erase failed? After
2039 * all, after erase the chip is either blank or partially blank or it
2040 * has the old contents. A blank chip won't boot, so if the user
2041 * wanted erase and reboots afterwards, the user knows very well that
2042 * booting won't work.
2043 */
2044 if (erase_and_write_flash(flash, fake_descriptor)) {
2045 emergency_help_message();
2046 ret = 1;
2047 }
2048
2049 free(fake_descriptor);
2050
2051 return ret;
2052}
2053
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002054static int read_dest_content(struct flashctx *flash, int verify_it,
2055 uint8_t *dest, unsigned long size)
2056{
2057 if (((verify_it == VERIFY_OFF) || (verify_it == VERIFY_PARTIAL))
2058 && get_num_include_args()) {
2059 /*
2060 * If no full verification is required and not
2061 * the entire chip is about to be programmed,
2062 * read only the areas which might change.
2063 */
2064 if (handle_partial_read(flash, dest, read_flash, 0) < 0)
2065 return 1;
2066 } else {
2067 if (read_flash(flash, dest, 0, size))
2068 return 1;
2069 }
2070 return 0;
2071}
2072
hailfingerc77acb52009-12-24 02:15:55 +00002073/* This function signature is horrible. We need to design a better interface,
2074 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00002075 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00002076 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002077int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09002078 int write_it, int erase_it, int verify_it, int extract_it,
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002079 const char *diff_file, int do_diff)
hailfingerc77acb52009-12-24 02:15:55 +00002080{
hailfinger4c47e9d2010-10-19 22:06:20 +00002081 uint8_t *oldcontents;
2082 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00002083 int ret = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +01002084 unsigned long size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002085 struct action_descriptor *descriptor = NULL;
hailfingerc77acb52009-12-24 02:15:55 +00002086
stefanct02116582011-05-18 01:30:56 +00002087 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00002088 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00002089 ret = 1;
2090 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00002091 }
2092
hailfinger771fc182010-10-15 00:01:14 +00002093 /* Given the existence of read locks, we want to unlock for read,
2094 * erase and write.
2095 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01002096 if (flash->chip->unlock)
2097 flash->chip->unlock(flash);
hailfinger771fc182010-10-15 00:01:14 +00002098
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +10002099 flash->address_high_byte = -1;
2100 flash->in_4ba_mode = false;
2101
Ed Swierk28cf7992017-07-03 13:17:18 -07002102 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Edward O'Callaghan9713aa62019-07-18 18:28:57 +10002103 if ((flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN) && flash->chip->set_4ba) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +10002104 if (flash->chip->set_4ba(flash)) {
Ed Swierk28cf7992017-07-03 13:17:18 -07002105 msg_cerr("Enabling/disabling 4-byte addressing mode failed!\n");
2106 return 1;
Boris Baykov6323c242016-06-11 18:29:03 +02002107 }
Boris Baykov1a2f5322016-06-11 18:29:00 +02002108 }
2109
Simon Glass9ad06c12013-07-03 22:08:17 +09002110 if (extract_it) {
2111 ret = extract_regions(flash);
2112 goto out_nofree;
2113 }
2114
David Hendricksd0ea9ed2011-03-04 17:31:57 -08002115 /* mark entries included using -i argument as "included" if they are
2116 found in the master rom_entries list */
2117 if (process_include_args() < 0) {
2118 ret = 1;
2119 goto out_nofree;
2120 }
2121
hailfinger771fc182010-10-15 00:01:14 +00002122 if (read_it) {
2123 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00002124 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00002125 }
hailfingerb437e282010-11-04 01:04:27 +00002126
stefanctd611e8f2011-07-12 22:35:21 +00002127 oldcontents = malloc(size);
2128 if (!oldcontents) {
2129 msg_gerr("Out of memory!\n");
2130 exit(1);
2131 }
Simon Glass4c214132013-07-16 10:09:28 -06002132 /* Assume worst case: All blocks are not erased. */
2133 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002134 newcontents = malloc(size);
2135 if (!newcontents) {
2136 msg_gerr("Out of memory!\n");
2137 exit(1);
2138 }
Simon Glass4c214132013-07-16 10:09:28 -06002139 /* Assume best case: All blocks are erased. */
2140 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002141 /* Side effect of the assumptions above: Default write action is erase
2142 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002143 * oldcontents being completely unerased means we have to erase
2144 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002145 */
2146
hailfingerd217d122010-10-08 18:52:29 +00002147 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002148 /*
2149 * Note: This must be done before any files specified by -i
2150 * arguments are processed merged into the newcontents since
2151 * -i files take priority. See http://crbug.com/263495.
2152 */
2153 if (filename) {
2154 if (read_buf_from_file(newcontents, size, filename)) {
2155 ret = 1;
2156 goto out;
2157 }
2158 } else {
2159 /* Content will be read from -i args, so they must
2160 * not overlap. */
2161 if (included_regions_overlap()) {
2162 msg_gerr("Error: Included regions must "
2163 "not overlap.\n");
2164 ret = 1;
2165 goto out;
2166 }
stepan1da96c02006-11-21 23:48:51 +00002167 }
2168
David Hendricksac82cac2012-06-19 10:29:37 -07002169#if 0
2170 /*
2171 * FIXME: show_id() causes failure if vendor:mainboard do not
2172 * match. This may happen if codenames are in flux.
2173 * See chrome-os-partner:10414.
2174 */
hailfinger90c7d542010-05-31 15:27:27 +00002175#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002176 if (programmer == PROGRAMMER_INTERNAL)
2177 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002178#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002179#endif
ollie5672ac62004-03-17 22:22:08 +00002180 }
2181
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002182 if (do_diff) {
2183 /*
2184 * Obtain a reference image so that we can check whether
2185 * regions need to be erased and to give better diagnostics in
2186 * case write fails. If --fast-verify is used then only the
2187 * regions which are included using -i will be read.
2188 */
2189 if (diff_file) {
2190 msg_cdbg("Reading old contents from file... ");
2191 if (read_buf_from_file(oldcontents, size, diff_file)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002192 ret = 1;
2193 msg_cdbg("FAILED.\n");
2194 goto out;
2195 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002196 } else {
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002197 msg_cdbg("Reading old contents from flash chip... ");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002198 ret = read_dest_content(flash, verify_it,
2199 oldcontents, size);
2200 if (ret) {
2201 msg_cdbg("FAILED.\n");
2202 goto out;
David Hendricks52ddff02013-07-23 15:05:14 -07002203 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002204 }
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002205 msg_cdbg("done.\n");
2206 } else if (!erase_it) {
2207 msg_pinfo("No diff performed, considering the chip erased.\n");
2208 memset(oldcontents, flash_erase_value(flash), size);
hailfinger4c47e9d2010-10-19 22:06:20 +00002209 }
David Hendricksac1d25c2016-08-09 17:00:58 -07002210
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002211
David Hendricksdf29a832013-06-28 14:33:51 -07002212 /*
2213 * Note: This must be done after reading the file specified for the
2214 * -w/-v argument, if any, so that files specified using -i end up
2215 * in the "newcontents" buffer before being written.
2216 * See http://crbug.com/263495.
2217 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002218 if (handle_romentries(flash, oldcontents, newcontents, erase_it)) {
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002219 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002220 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002221 goto out;
2222 }
uwef6641642007-05-09 10:17:44 +00002223
David Hendricksa7e114b2016-02-26 18:49:15 -08002224 if (erase_it) {
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002225 erase_chip(flash, oldcontents, newcontents, size);
2226 goto verify;
David Hendricksa7e114b2016-02-26 18:49:15 -08002227 }
2228
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002229 descriptor = prepare_action_descriptor(flash, oldcontents,
2230 newcontents, do_diff);
stuge8ce3a3c2008-04-28 14:47:30 +00002231 if (write_it) {
David Hendricksb64b39a2016-10-11 13:48:06 -07002232 // parse the new fmap and disable soft WP if necessary
David Hendricksac1d25c2016-08-09 17:00:58 -07002233 if ((ret = cros_ec_prepare(newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002234 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002235 goto out;
2236 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002237
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002238 if (erase_and_write_flash(flash, descriptor)) {
hailfingerb437e282010-11-04 01:04:27 +00002239 msg_cerr("Uh oh. Erase/write failed. Checking if "
2240 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002241 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002242 if (!memcmp(oldcontents, newcontents, size)) {
2243 msg_cinfo("Good. It seems nothing was "
2244 "changed.\n");
2245 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002246 ret = 1;
2247 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002248 }
2249 }
hailfingerd217d122010-10-08 18:52:29 +00002250 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002251 ret = 1;
2252 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002253 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002254
David Hendricksac1d25c2016-08-09 17:00:58 -07002255 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002256 if (ret < 0) {
2257 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002258 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002259 emergency_help_message();
2260 ret = 1;
2261 goto out;
2262 } else if (ret > 0) {
2263 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002264 msg_pdbg("CROS_EC needs 2nd pass.\n");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002265 ret = read_dest_content(flash, verify_it,
2266 oldcontents, size);
2267 if (ret) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002268 emergency_help_message();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002269 goto out;
2270 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002271
2272 /* Get a new descriptor. */
2273 free(descriptor);
2274 descriptor = prepare_action_descriptor(flash,
2275 oldcontents,
2276 newcontents,
2277 do_diff);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002278 // write 2nd pass
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002279 if (erase_and_write_flash(flash, descriptor)) {
David Hendricksb907de32014-08-11 16:47:09 -07002280 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002281 emergency_help_message();
2282 ret = 1;
2283 goto out;
2284 }
2285 ret = 0;
2286 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002287
David Hendricksac1d25c2016-08-09 17:00:58 -07002288 if (cros_ec_finish() < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002289 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002290 emergency_help_message();
2291 ret = 1;
2292 goto out;
2293 }
stuge8ce3a3c2008-04-28 14:47:30 +00002294 }
ollie6a600992005-11-26 21:55:36 +00002295
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002296 verify:
hailfinger0459e1c2009-08-19 13:55:34 +00002297 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002298 if ((write_it || erase_it) && !content_has_changed) {
2299 msg_gdbg("Nothing was erased or written, skipping "
2300 "verification\n");
2301 } else {
2302 /* Work around chips which need some time to calm down. */
2303 if (write_it && verify_it != VERIFY_PARTIAL)
2304 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002305
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002306 ret = verify_flash(flash, descriptor, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002307
David Hendricks9ba79fb2015-04-03 12:06:16 -07002308 /* If we tried to write, and verification now fails, we
2309 * might have an emergency situation.
2310 */
2311 if (ret && write_it)
2312 emergency_help_message();
2313 }
hailfinger0459e1c2009-08-19 13:55:34 +00002314 }
ollie6a600992005-11-26 21:55:36 +00002315
hailfinger90fcf9b2010-11-05 14:51:59 +00002316out:
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002317 if (descriptor)
2318 free(descriptor);
2319
hailfinger90fcf9b2010-11-05 14:51:59 +00002320 free(oldcontents);
2321 free(newcontents);
2322out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002323 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002324 /*
Edward O'Callaghan1a3fd132019-06-04 14:18:55 +10002325 * programmer_shutdown() call is moved to cli_classic() in chromium os
David Hendricks668f29d2011-01-27 18:51:45 -08002326 * tree. This is because some operations, such as write protection,
2327 * requires programmer_shutdown() but does not call doit().
2328 */
2329// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002330 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002331}