blob: df8d4324972266c51dd55aca367f2be98efffa55 [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;
rminnich8d3ff912003-10-25 17:01:29 +000045char *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;
hailfingerddeb4ac2010-07-08 10:13:37 +000070static 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",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100245 .type = OTHER,
Anton Staafb2647882014-09-17 15:13:43 -0700246 .init = raiden_debug_spi_init,
247 .map_flash_region = fallback_map,
248 .unmap_flash_region = fallback_unmap,
249 .delay = internal_delay,
250 },
251#endif
252
hailfinger90c7d542010-05-31 15:27:27 +0000253#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000254 {
255 .name = "dediprog",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100256 .type = USB,
hailfingerdfb32a02010-01-19 11:15:48 +0000257 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000258 .map_flash_region = fallback_map,
259 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000260 .delay = internal_delay,
261 },
262#endif
263
hailfinger52c4fa02010-07-21 10:26:01 +0000264#if CONFIG_RAYER_SPI == 1
265 {
266 .name = "rayer_spi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100267 .type = OTHER,
268 /* FIXME */
269 .devs.note = "RayeR parallel port programmer\n",
hailfinger52c4fa02010-07-21 10:26:01 +0000270 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000271 .map_flash_region = fallback_map,
272 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000273 .delay = internal_delay,
274 },
275#endif
276
hailfinger7949b652011-05-08 00:24:18 +0000277#if CONFIG_NICINTEL == 1
278 {
279 .name = "nicintel",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100280 .type = PCI,
281 .devs.dev = nics_intel,
hailfinger7949b652011-05-08 00:24:18 +0000282 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000283 .map_flash_region = fallback_map,
284 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000285 .delay = internal_delay,
286 },
287#endif
288
uwe6764e922010-09-03 18:21:21 +0000289#if CONFIG_NICINTEL_SPI == 1
290 {
uwe8d342eb2011-07-28 08:13:25 +0000291 .name = "nicintel_spi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100292 .type = PCI,
293 .devs.dev = nics_intel_spi,
uwe8d342eb2011-07-28 08:13:25 +0000294 .init = nicintel_spi_init,
295 .map_flash_region = fallback_map,
296 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000297 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000298 },
299#endif
300
hailfingerfb1f31f2010-12-03 14:48:11 +0000301#if CONFIG_OGP_SPI == 1
302 {
uwe8d342eb2011-07-28 08:13:25 +0000303 .name = "ogp_spi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100304 .type = PCI,
305 .devs.dev = ogp_spi,
uwe8d342eb2011-07-28 08:13:25 +0000306 .init = ogp_spi_init,
307 .map_flash_region = fallback_map,
308 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000309 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000310 },
311#endif
312
hailfinger935365d2011-02-04 21:37:59 +0000313#if CONFIG_SATAMV == 1
314 {
315 .name = "satamv",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100316 .type = PCI,
317 .devs.dev = satas_mv,
hailfinger935365d2011-02-04 21:37:59 +0000318 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000319 .map_flash_region = fallback_map,
320 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000321 .delay = internal_delay,
322 },
323#endif
324
David Hendrickscebee892015-05-23 20:30:30 -0700325#if CONFIG_LINUX_MTD == 1
326 {
327 .name = "linux_mtd",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100328 .type = OTHER,
329 .devs.note = "Device files /dev/mtd*\n",
David Hendrickscebee892015-05-23 20:30:30 -0700330 .init = linux_mtd_init,
331 .map_flash_region = fallback_map,
332 .unmap_flash_region = fallback_unmap,
333 .delay = internal_delay,
334 },
335#endif
336
uwe7df6dda2011-09-03 18:37:52 +0000337#if CONFIG_LINUX_SPI == 1
338 {
339 .name = "linux_spi",
Edward O'Callaghan0949b782019-11-10 23:23:20 +1100340 .type = OTHER,
341 .devs.note = "Device files /dev/spidev*.*\n",
uwe7df6dda2011-09-03 18:37:52 +0000342 .init = linux_spi_init,
343 .map_flash_region = fallback_map,
344 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000345 .delay = internal_delay,
346 },
347#endif
348
Patrick Georgi8ddfee92017-03-20 14:54:28 +0100349 {0}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000350};
stepan927d4e22007-04-04 22:45:58 +0000351
David Hendricksbf36f092010-11-02 23:39:29 -0700352#define CHIP_RESTORE_MAXFN 4
353static int chip_restore_fn_count = 0;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000354static struct chip_restore_func_data {
David Hendricksbf36f092010-11-02 23:39:29 -0700355 CHIP_RESTORE_CALLBACK;
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700356 struct flashctx *flash;
David Hendricksbf36f092010-11-02 23:39:29 -0700357 uint8_t status;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000358} chip_restore_fn[CHIP_RESTORE_MAXFN];
David Hendricksbf36f092010-11-02 23:39:29 -0700359
David Hendricks668f29d2011-01-27 18:51:45 -0800360
hailfingerf31cbdc2010-11-10 15:25:18 +0000361#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000362static int shutdown_fn_count = 0;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000363static struct shutdown_func_data {
David Hendricks93784b42016-08-09 17:00:38 -0700364 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000365 void *data;
Edward O'Callaghan60df9dd2019-09-03 14:28:48 +1000366} shutdown_fn[SHUTDOWN_MAXFN];
hailfinger1ff33dc2010-07-03 11:02:10 +0000367/* Initialize to 0 to make sure nobody registers a shutdown function before
368 * programmer init.
369 */
370static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000371
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700372static int check_block_eraser(const struct flashctx *flash, int k, int log);
stefanct569dbb62011-07-01 00:19:12 +0000373
hailfingerdc6f7972010-02-14 01:20:28 +0000374/* Register a function to be executed on programmer shutdown.
375 * The advantage over atexit() is that you can supply a void pointer which will
376 * be used as parameter to the registered function upon programmer shutdown.
377 * This pointer can point to arbitrary data used by said function, e.g. undo
378 * information for GPIO settings etc. If unneeded, set data=NULL.
379 * Please note that the first (void *data) belongs to the function signature of
380 * the function passed as first parameter.
381 */
David Hendricks93784b42016-08-09 17:00:38 -0700382int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000383{
384 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000385 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000386 SHUTDOWN_MAXFN);
387 return 1;
388 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000389 if (!may_register_shutdown) {
390 msg_perr("Tried to register a shutdown function before "
391 "programmer init.\n");
392 return 1;
393 }
hailfingerdc6f7972010-02-14 01:20:28 +0000394 shutdown_fn[shutdown_fn_count].func = function;
395 shutdown_fn[shutdown_fn_count].data = data;
396 shutdown_fn_count++;
397
398 return 0;
399}
400
David Hendricksbf36f092010-11-02 23:39:29 -0700401//int register_chip_restore(int (*function) (void *data), void *data)
402int register_chip_restore(CHIP_RESTORE_CALLBACK,
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700403 struct flashctx *flash, uint8_t status)
David Hendricksbf36f092010-11-02 23:39:29 -0700404{
405 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
406 msg_perr("Tried to register more than %i chip restore"
407 " functions.\n", CHIP_RESTORE_MAXFN);
408 return 1;
409 }
410 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
411 chip_restore_fn[chip_restore_fn_count].flash = flash;
412 chip_restore_fn[chip_restore_fn_count].status = status;
413 chip_restore_fn_count++;
414
415 return 0;
416}
417
David Hendricksac1d25c2016-08-09 17:00:58 -0700418int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000419{
hailfinger1ef766d2010-07-06 09:55:48 +0000420 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000421
422 if (prog >= PROGRAMMER_INVALID) {
423 msg_perr("Invalid programmer specified!\n");
424 return -1;
425 }
426 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000427 /* Initialize all programmer specific data. */
428 /* Default to unlimited decode sizes. */
429 max_rom_decode = (const struct decode_sizes) {
430 .parallel = 0xffffffff,
431 .lpc = 0xffffffff,
432 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000433 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000434 };
David Hendricksac1d25c2016-08-09 17:00:58 -0700435 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000436 /* Default to top aligned flash at 4 GB. */
437 flashbase = 0;
438 /* Registering shutdown functions is now allowed. */
439 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000440 /* Default to allowing writes. Broken programmers set this to 0. */
441 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000442
443 programmer_param = param;
444 msg_pdbg("Initializing %s programmer\n",
445 programmer_table[programmer].name);
David Hendricksac1d25c2016-08-09 17:00:58 -0700446 ret = programmer_table[programmer].init();
hailfinger1ef766d2010-07-06 09:55:48 +0000447 return ret;
uweabe92a52009-05-16 22:36:00 +0000448}
449
David Hendricksbf36f092010-11-02 23:39:29 -0700450int chip_restore()
451{
452 int rc = 0;
453
454 while (chip_restore_fn_count > 0) {
455 int i = --chip_restore_fn_count;
456 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
457 chip_restore_fn[i].status);
458 }
459
460 return rc;
461}
462
David Hendricks93784b42016-08-09 17:00:38 -0700463int programmer_shutdown(void)
uweabe92a52009-05-16 22:36:00 +0000464{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000465 int ret = 0;
466
hailfinger1ff33dc2010-07-03 11:02:10 +0000467 /* Registering shutdown functions is no longer allowed. */
468 may_register_shutdown = 0;
469 while (shutdown_fn_count > 0) {
470 int i = --shutdown_fn_count;
David Hendricks93784b42016-08-09 17:00:38 -0700471 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000472 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000473 return ret;
uweabe92a52009-05-16 22:36:00 +0000474}
475
476void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
477 size_t len)
478{
479 return programmer_table[programmer].map_flash_region(descr,
480 phys_addr, len);
481}
482
483void programmer_unmap_flash_region(void *virt_addr, size_t len)
484{
485 programmer_table[programmer].unmap_flash_region(virt_addr, len);
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +1100486 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
uweabe92a52009-05-16 22:36:00 +0000487}
488
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700489void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000490{
Craig Hesling65eb8812019-08-01 09:33:56 -0700491 par_master->chip_writeb(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000492}
493
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700494void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000495{
Craig Hesling65eb8812019-08-01 09:33:56 -0700496 par_master->chip_writew(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000497}
498
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700499void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000500{
Craig Hesling65eb8812019-08-01 09:33:56 -0700501 par_master->chip_writel(flash, val, addr);
uweabe92a52009-05-16 22:36:00 +0000502}
503
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700504void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000505{
Craig Hesling65eb8812019-08-01 09:33:56 -0700506 par_master->chip_writen(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000507}
508
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700509uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000510{
Craig Hesling65eb8812019-08-01 09:33:56 -0700511 return par_master->chip_readb(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000512}
513
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700514uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000515{
Craig Hesling65eb8812019-08-01 09:33:56 -0700516 return par_master->chip_readw(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000517}
518
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700519uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
uweabe92a52009-05-16 22:36:00 +0000520{
Craig Hesling65eb8812019-08-01 09:33:56 -0700521 return par_master->chip_readl(flash, addr);
uweabe92a52009-05-16 22:36:00 +0000522}
523
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700524void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, size_t len)
hailfinger9d987ef2009-06-05 18:32:07 +0000525{
Craig Hesling65eb8812019-08-01 09:33:56 -0700526 par_master->chip_readn(flash, buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000527}
528
hailfingere5829f62009-06-05 17:48:08 +0000529void programmer_delay(int usecs)
530{
531 programmer_table[programmer].delay(usecs);
532}
533
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700534void map_flash_registers(struct flashctx *flash)
stepan15e64bc2007-05-24 08:48:10 +0000535{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100536 size_t size = flash->chip->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000537 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000538 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000539 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000540}
541
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +1100542void unmap_flash(struct flashctx *flash)
543{
544 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
545 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
546 flash->physical_registers = 0;
547 flash->virtual_registers = (chipaddr)ERROR_PTR;
548 }
549
550 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
551 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
552 flash->physical_memory = 0;
553 flash->virtual_memory = (chipaddr)ERROR_PTR;
554 }
555}
556
557int map_flash(struct flashctx *flash)
558{
559 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
560 flash->virtual_memory = (chipaddr)ERROR_PTR;
561 flash->virtual_registers = (chipaddr)ERROR_PTR;
562
563 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
564 * These are used for various probing-related hacks that would not map successfully anyway and should be
565 * removed ASAP. */
566 if (flash->chip->total_size == 0)
567 return 0;
568
569 const chipsize_t size = flash->chip->total_size * 1024;
570 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
571 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
572 if (addr == ERROR_PTR) {
573 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
574 flash->chip->name, PRIxPTR_WIDTH, base);
575 return 1;
576 }
577 flash->physical_memory = base;
578 flash->virtual_memory = (chipaddr)addr;
579
580 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
581 * completely different on some chips and programmers, or not mappable at all.
582 * Ignore these problems for now and always report success. */
583 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
584 base = 0xffffffff - size - 0x400000 + 1;
585 addr = programmer_map_flash_region("flash chip registers", base, size);
586 if (addr == ERROR_PTR) {
587 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
588 flash->chip->name, PRIxPTR_WIDTH, base);
589 return 0;
590 }
591 flash->physical_registers = base;
592 flash->virtual_registers = (chipaddr)addr;
593 }
594 return 0;
595}
596
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700597int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000598{
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700599 chip_readn(flash, buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000600
hailfinger23060112009-05-08 12:49:03 +0000601 return 0;
602}
603
David Hendricksda18f692016-10-21 17:49:49 -0700604/* This is a somewhat hacked function similar in some ways to strtok(). It will
605 * look for needle with a subsequent '=' in haystack, return a copy of needle.
hailfinger6e5a52a2009-11-24 18:27:10 +0000606 */
stefanct52700282011-06-26 17:38:17 +0000607char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000608{
David Hendricksda18f692016-10-21 17:49:49 -0700609 char *param_pos, *opt_pos;
hailfinger1ef766d2010-07-06 09:55:48 +0000610 char *opt = NULL;
611 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000612 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000613
hailfingerf4aaccc2010-04-28 15:22:14 +0000614 needlelen = strlen(needle);
615 if (!needlelen) {
616 msg_gerr("%s: empty needle! Please report a bug at "
617 "flashrom@flashrom.org\n", __func__);
618 return NULL;
619 }
620 /* No programmer parameters given. */
621 if (*haystack == NULL)
622 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000623 param_pos = strstr(*haystack, needle);
624 do {
625 if (!param_pos)
626 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000627 /* Needle followed by '='? */
628 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600629
hailfinger1ef766d2010-07-06 09:55:48 +0000630 /* Beginning of the string? */
631 if (param_pos == *haystack)
632 break;
633 /* After a delimiter? */
634 if (strchr(delim, *(param_pos - 1)))
635 break;
636 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000637 /* Continue searching. */
638 param_pos++;
639 param_pos = strstr(param_pos, needle);
640 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000641
hailfinger6e5a52a2009-11-24 18:27:10 +0000642 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000643 /* Get the string after needle and '='. */
644 opt_pos = param_pos + needlelen + 1;
645 optlen = strcspn(opt_pos, delim);
646 /* Return an empty string if the parameter was empty. */
647 opt = malloc(optlen + 1);
648 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000649 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000650 exit(1);
651 }
hailfinger1ef766d2010-07-06 09:55:48 +0000652 strncpy(opt, opt_pos, optlen);
653 opt[optlen] = '\0';
hailfinger6e5a52a2009-11-24 18:27:10 +0000654 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000655
hailfinger1ef766d2010-07-06 09:55:48 +0000656 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000657}
658
stefanct52700282011-06-26 17:38:17 +0000659char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000660{
661 return extract_param(&programmer_param, param_name, ",");
662}
663
stefancte1c5acf2011-07-04 07:27:17 +0000664/* Returns the number of well-defined erasers for a chip. */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700665static unsigned int count_usable_erasers(const struct flashctx *flash)
stefanct569dbb62011-07-01 00:19:12 +0000666{
667 unsigned int usable_erasefunctions = 0;
668 int k;
669 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
670 if (!check_block_eraser(flash, k, 0))
671 usable_erasefunctions++;
672 }
673 return usable_erasefunctions;
674}
675
hailfinger7af83692009-06-15 17:23:36 +0000676/* start is an offset to the base address of the flash chip */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700677int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000678{
679 int ret;
680 uint8_t *cmpbuf = malloc(len);
681
682 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000683 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000684 exit(1);
685 }
Simon Glass4c214132013-07-16 10:09:28 -0600686 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000687 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
688 free(cmpbuf);
689 return ret;
690}
691
Simon Glass4e305f42015-01-08 06:29:04 -0700692static int compare_chunk(uint8_t *readbuf, uint8_t *cmpbuf, unsigned int start,
693 unsigned int len, const char *message)
694{
695 int failcount = 0, i;
696
697 for (i = 0; i < len; i++) {
698 if (cmpbuf[i] != readbuf[i]) {
699 if (!failcount) {
700 msg_cerr("%s FAILED at 0x%08x! "
701 "Expected=0x%02x, Read=0x%02x,",
702 message, start + i,
703 cmpbuf[i], readbuf[i]);
704 }
705 failcount++;
706 }
707 }
708
709 return failcount;
710}
711
uwee15beb92010-08-08 17:01:18 +0000712/*
hailfinger7af3d192009-11-25 17:05:52 +0000713 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000714 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000715 * @start offset to the base address of the flash chip
716 * @len length of the verified area
717 * @message string to print in the "FAILED" message
718 * @return 0 for success, -1 for failure
719 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700720int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000721 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000722{
hailfinger8cb6ece2010-11-16 17:21:58 +0000723 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000724 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000725
726 if (!len)
727 goto out_free;
728
Patrick Georgif3fa2992017-02-02 16:24:44 +0100729 if (!flash->chip->read) {
snelsone42c3802010-05-07 20:09:04 +0000730 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000731 return 1;
732 }
hailfinger7af83692009-06-15 17:23:36 +0000733 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000734 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000735 exit(1);
736 }
737
Patrick Georgif3fa2992017-02-02 16:24:44 +0100738 if (start + len > flash->chip->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000739 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000740 " total_size 0x%x\n", __func__, start, len,
Patrick Georgif3fa2992017-02-02 16:24:44 +0100741 flash->chip->total_size * 1024);
hailfinger7af83692009-06-15 17:23:36 +0000742 ret = -1;
743 goto out_free;
744 }
745 if (!message)
746 message = "VERIFY";
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -0700747 msg_gdbg("%#06x..%#06x ", start, start + len -1);
Simon Glass4e305f42015-01-08 06:29:04 -0700748 if (programmer_table[programmer].paranoid) {
749 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800750
Simon Glass4e305f42015-01-08 06:29:04 -0700751 /* limit chunksize in order to catch errors early */
752 for (i = 0, chunksize = 0; i < len; i += chunksize) {
753 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800754
Patrick Georgif3fa2992017-02-02 16:24:44 +0100755 chunksize = min(flash->chip->page_size, len - i);
756 tmp = flash->chip->read(flash, readbuf + i, start + i, chunksize);
Simon Glass4e305f42015-01-08 06:29:04 -0700757 if (tmp) {
758 ret = tmp;
759 if (ignore_error(tmp))
760 continue;
761 else
762 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800763 }
Simon Glass4e305f42015-01-08 06:29:04 -0700764
Duncan Laurie25a4ca22019-04-25 12:08:52 -0700765 /*
766 * Check write access permission and do not compare chunks
767 * where flashrom does not have write access to the region.
768 */
769 if (flash->chip->check_access) {
770 tmp = flash->chip->check_access(flash, start + i, chunksize, 0);
771 if (tmp && ignore_error(tmp))
772 continue;
773 }
774
Simon Glass4e305f42015-01-08 06:29:04 -0700775 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
776 chunksize, message);
777 if (failcount)
778 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800779 }
Simon Glass4e305f42015-01-08 06:29:04 -0700780 } else {
781 int tmp;
782
783 /* read as much as we can to reduce transaction overhead */
Patrick Georgif3fa2992017-02-02 16:24:44 +0100784 tmp = flash->chip->read(flash, readbuf, start, len);
Simon Glass4e305f42015-01-08 06:29:04 -0700785 if (tmp && !ignore_error(tmp)) {
786 ret = tmp;
787 goto out_free;
788 }
789
790 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000791 }
792
hailfinger5be6c0f2009-07-23 01:42:56 +0000793 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000794 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000795 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000796 ret = -1;
797 }
hailfinger7af83692009-06-15 17:23:36 +0000798
799out_free:
800 free(readbuf);
801 return ret;
802}
803
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100804/* Helper function for need_erase() that focuses on granularities of gran bytes. */
805static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
806 unsigned int gran)
807{
808 unsigned int i, j, limit;
809 for (j = 0; j < len / gran; j++) {
810 limit = min (gran, len - j * gran);
811 /* Are 'have' and 'want' identical? */
812 if (!memcmp(have + j * gran, want + j * gran, limit))
813 continue;
814 /* have needs to be in erased state. */
815 for (i = 0; i < limit; i++)
816 if (have[j * gran + i] != 0xff)
817 return 1;
818 }
819 return 0;
820}
821
uwee15beb92010-08-08 17:01:18 +0000822/*
hailfingerb247c7a2010-03-08 00:42:32 +0000823 * Check if the buffer @have can be programmed to the content of @want without
824 * erasing. This is only possible if all chunks of size @gran are either kept
825 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000826 *
hailfingerb247c7a2010-03-08 00:42:32 +0000827 * The following write granularities (enum @gran) are known:
828 * - 1 bit. Each bit can be cleared individually.
829 * - 1 byte. A byte can be written once. Further writes to an already written
830 * byte cause the contents to be either undefined or to stay unchanged.
831 * - 128 bytes. If less than 128 bytes are written, the rest will be
832 * erased. Each write to a 128-byte region will trigger an automatic erase
833 * before anything is written. Very uncommon behaviour and unsupported by
834 * this function.
835 * - 256 bytes. If less than 256 bytes are written, the contents of the
836 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000837 * Warning: This function assumes that @have and @want point to naturally
838 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000839 *
840 * @have buffer with current content
841 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000842 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000843 * @gran write granularity (enum, not count)
844 * @return 0 if no erase is needed, 1 otherwise
845 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700846static int need_erase(struct flashctx *flash, uint8_t *have, uint8_t *want,
Simon Glass4c214132013-07-16 10:09:28 -0600847 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000848{
hailfingerb91c08c2011-08-15 19:54:20 +0000849 int result = 0;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100850 unsigned int i;
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700851
hailfingerb247c7a2010-03-08 00:42:32 +0000852 switch (gran) {
853 case write_gran_1bit:
854 for (i = 0; i < len; i++)
855 if ((have[i] & want[i]) != want[i]) {
856 result = 1;
857 break;
858 }
859 break;
860 case write_gran_1byte:
861 for (i = 0; i < len; i++)
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100862 if ((have[i] != want[i]) && (have[i] != 0xff)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000863 result = 1;
864 break;
865 }
866 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100867 case write_gran_128bytes:
868 result = need_erase_gran_bytes(have, want, len, 128);
869 break;
hailfingerb247c7a2010-03-08 00:42:32 +0000870 case write_gran_256bytes:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100871 result = need_erase_gran_bytes(have, want, len, 256);
872 break;
873 case write_gran_264bytes:
874 result = need_erase_gran_bytes(have, want, len, 264);
875 break;
876 case write_gran_512bytes:
877 result = need_erase_gran_bytes(have, want, len, 512);
878 break;
879 case write_gran_528bytes:
880 result = need_erase_gran_bytes(have, want, len, 528);
881 break;
882 case write_gran_1024bytes:
883 result = need_erase_gran_bytes(have, want, len, 1024);
884 break;
885 case write_gran_1056bytes:
886 result = need_erase_gran_bytes(have, want, len, 1056);
887 break;
888 case write_gran_1byte_implicit_erase:
889 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
890 result = 0;
hailfingerb247c7a2010-03-08 00:42:32 +0000891 break;
hailfingerb437e282010-11-04 01:04:27 +0000892 default:
893 msg_cerr("%s: Unsupported granularity! Please report a bug at "
894 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000895 }
896 return result;
897}
898
hailfingerb437e282010-11-04 01:04:27 +0000899/**
900 * Check if the buffer @have needs to be programmed to get the content of @want.
901 * If yes, return 1 and fill in first_start with the start address of the
902 * write operation and first_len with the length of the first to-be-written
903 * chunk. If not, return 0 and leave first_start and first_len undefined.
904 *
905 * Warning: This function assumes that @have and @want point to naturally
906 * aligned regions.
907 *
908 * @have buffer with current content
909 * @want buffer with desired content
910 * @len length of the checked area
911 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000912 * @first_start offset of the first byte which needs to be written (passed in
913 * value is increased by the offset of the first needed write
914 * relative to have/want or unchanged if no write is needed)
915 * @return length of the first contiguous area which needs to be written
916 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000917 *
918 * FIXME: This function needs a parameter which tells it about coalescing
919 * in relation to the max write length of the programmer and the max write
920 * length of the chip.
921 */
stefanctc5eb8a92011-11-23 09:13:48 +0000922static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
923 unsigned int *first_start,
924 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000925{
stefanctc5eb8a92011-11-23 09:13:48 +0000926 int need_write = 0;
927 unsigned int rel_start = 0, first_len = 0;
928 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000929
hailfingerb437e282010-11-04 01:04:27 +0000930 switch (gran) {
931 case write_gran_1bit:
932 case write_gran_1byte:
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100933 case write_gran_1byte_implicit_erase:
hailfinger90fcf9b2010-11-05 14:51:59 +0000934 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000935 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100936 case write_gran_128bytes:
937 stride = 128;
938 break;
hailfingerb437e282010-11-04 01:04:27 +0000939 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000940 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000941 break;
Edward O'Callaghand8eca562019-02-24 21:10:33 +1100942 case write_gran_264bytes:
943 stride = 264;
944 break;
945 case write_gran_512bytes:
946 stride = 512;
947 break;
948 case write_gran_528bytes:
949 stride = 528;
950 break;
951 case write_gran_1024bytes:
952 stride = 1024;
953 break;
954 case write_gran_1056bytes:
955 stride = 1056;
956 break;
hailfingerb437e282010-11-04 01:04:27 +0000957 default:
958 msg_cerr("%s: Unsupported granularity! Please report a bug at "
959 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000960 /* Claim that no write was needed. A write with unknown
961 * granularity is too dangerous to try.
962 */
963 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000964 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000965 for (i = 0; i < len / stride; i++) {
966 limit = min(stride, len - i * stride);
967 /* Are 'have' and 'want' identical? */
968 if (memcmp(have + i * stride, want + i * stride, limit)) {
969 if (!need_write) {
970 /* First location where have and want differ. */
971 need_write = 1;
972 rel_start = i * stride;
973 }
974 } else {
975 if (need_write) {
976 /* First location where have and want
977 * do not differ anymore.
978 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000979 break;
980 }
981 }
982 }
hailfingerffb7f382010-12-06 13:05:44 +0000983 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000984 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000985 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000986 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000987}
988
hailfinger0c515352009-11-23 12:55:31 +0000989/* This function generates various test patterns useful for testing controller
990 * and chip communication as well as chip behaviour.
991 *
992 * If a byte can be written multiple times, each time keeping 0-bits at 0
993 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
994 * is essentially an AND operation. That's also the reason why this function
995 * provides the result of AND between various patterns.
996 *
997 * Below is a list of patterns (and their block length).
998 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
999 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
1000 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
1001 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
1002 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
1003 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
1004 * Pattern 6 is 00 (1 Byte)
1005 * Pattern 7 is ff (1 Byte)
1006 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
1007 * byte block.
1008 *
1009 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
1010 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
1011 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
1012 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
1013 * Pattern 12 is 00 (1 Byte)
1014 * Pattern 13 is ff (1 Byte)
1015 * Patterns 8-13 have no block number.
1016 *
1017 * Patterns 0-3 are created to detect and efficiently diagnose communication
1018 * slips like missed bits or bytes and their repetitive nature gives good visual
1019 * cues to the person inspecting the results. In addition, the following holds:
1020 * AND Pattern 0/1 == Pattern 4
1021 * AND Pattern 2/3 == Pattern 5
1022 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
1023 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
1024 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
1025 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
1026 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
1027 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
1028 * Besides that, they provide for bit testing of the last two bytes of every
1029 * 256 byte block which contains the block number for patterns 0-6.
1030 * Patterns 10-11 are special purpose for detecting subblock aliasing with
1031 * block sizes >256 bytes (some Dataflash chips etc.)
1032 * AND Pattern 8/9 == Pattern 12
1033 * AND Pattern 10/11 == Pattern 12
1034 * Pattern 13 is the completely erased state.
1035 * None of the patterns can detect aliasing at boundaries which are a multiple
1036 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
1037 */
1038int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
1039{
1040 int i;
1041
1042 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +00001043 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +00001044 return 1;
1045 }
1046
1047 switch (variant) {
1048 case 0:
1049 for (i = 0; i < size; i++)
1050 buf[i] = (i & 0xf) << 4 | 0x5;
1051 break;
1052 case 1:
1053 for (i = 0; i < size; i++)
1054 buf[i] = (i & 0xf) << 4 | 0xa;
1055 break;
1056 case 2:
1057 for (i = 0; i < size; i++)
1058 buf[i] = 0x50 | (i & 0xf);
1059 break;
1060 case 3:
1061 for (i = 0; i < size; i++)
1062 buf[i] = 0xa0 | (i & 0xf);
1063 break;
1064 case 4:
1065 for (i = 0; i < size; i++)
1066 buf[i] = (i & 0xf) << 4;
1067 break;
1068 case 5:
1069 for (i = 0; i < size; i++)
1070 buf[i] = i & 0xf;
1071 break;
1072 case 6:
1073 memset(buf, 0x00, size);
1074 break;
1075 case 7:
1076 memset(buf, 0xff, size);
1077 break;
1078 case 8:
1079 for (i = 0; i < size; i++)
1080 buf[i] = i & 0xff;
1081 break;
1082 case 9:
1083 for (i = 0; i < size; i++)
1084 buf[i] = ~(i & 0xff);
1085 break;
1086 case 10:
1087 for (i = 0; i < size % 2; i++) {
1088 buf[i * 2] = (i >> 8) & 0xff;
1089 buf[i * 2 + 1] = i & 0xff;
1090 }
1091 if (size & 0x1)
1092 buf[i * 2] = (i >> 8) & 0xff;
1093 break;
1094 case 11:
1095 for (i = 0; i < size % 2; i++) {
1096 buf[i * 2] = ~((i >> 8) & 0xff);
1097 buf[i * 2 + 1] = ~(i & 0xff);
1098 }
1099 if (size & 0x1)
1100 buf[i * 2] = ~((i >> 8) & 0xff);
1101 break;
1102 case 12:
1103 memset(buf, 0x00, size);
1104 break;
1105 case 13:
1106 memset(buf, 0xff, size);
1107 break;
1108 }
1109
1110 if ((variant >= 0) && (variant <= 7)) {
1111 /* Write block number in the last two bytes of each 256-byte
1112 * block, big endian for easier reading of the hexdump.
1113 * Note that this wraps around for chips larger than 2^24 bytes
1114 * (16 MB).
1115 */
1116 for (i = 0; i < size / 256; i++) {
1117 buf[i * 256 + 254] = (i >> 8) & 0xff;
1118 buf[i * 256 + 255] = i & 0xff;
1119 }
1120 }
1121
1122 return 0;
1123}
1124
hailfingeraec9c962009-10-31 01:53:09 +00001125int check_max_decode(enum chipbustype buses, uint32_t size)
1126{
1127 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001128
1129 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001130 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001131 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001132 "size %u kB of chipset/board/programmer "
1133 "for %s interface, "
1134 "probe/read/erase/write may fail. ", size / 1024,
1135 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001136 }
hailfingere1e41ea2011-07-27 07:13:06 +00001137 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001138 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001139 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001140 "size %u kB of chipset/board/programmer "
1141 "for %s interface, "
1142 "probe/read/erase/write may fail. ", size / 1024,
1143 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001144 }
hailfingere1e41ea2011-07-27 07:13:06 +00001145 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001146 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001147 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001148 "size %u kB of chipset/board/programmer "
1149 "for %s interface, "
1150 "probe/read/erase/write may fail. ", size / 1024,
1151 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001152 }
hailfingere1e41ea2011-07-27 07:13:06 +00001153 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001154 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001155 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001156 "size %u kB of chipset/board/programmer "
1157 "for %s interface, "
1158 "probe/read/erase/write may fail. ", size / 1024,
1159 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001160 }
1161 if (!limitexceeded)
1162 return 0;
1163 /* Sometimes chip and programmer have more than one bus in common,
1164 * and the limit is not exceeded on all buses. Tell the user.
1165 */
1166 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001167 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001168 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001169 "interface which can support a chip of this size. "
1170 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001171 return 1;
1172}
1173
Edward O'Callaghan8488f122019-06-17 12:38:15 +10001174/*
1175 * Return a string corresponding to the bustype parameter.
1176 * Memory is obtained with malloc() and must be freed with free() by the caller.
1177 */
1178char *flashbuses_to_text(enum chipbustype bustype)
1179{
1180 char *ret = calloc(1, 1);
1181 /*
1182 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
1183 * will cease to exist and should be eliminated here as well.
1184 */
1185 if (bustype == BUS_NONSPI) {
1186 ret = strcat_realloc(ret, "Non-SPI, ");
1187 } else {
1188 if (bustype & BUS_PARALLEL)
1189 ret = strcat_realloc(ret, "Parallel, ");
1190 if (bustype & BUS_LPC)
1191 ret = strcat_realloc(ret, "LPC, ");
1192 if (bustype & BUS_FWH)
1193 ret = strcat_realloc(ret, "FWH, ");
1194 if (bustype & BUS_SPI)
1195 ret = strcat_realloc(ret, "SPI, ");
1196 if (bustype & BUS_PROG)
1197 ret = strcat_realloc(ret, "Programmer-specific, ");
1198 if (bustype == BUS_NONE)
1199 ret = strcat_realloc(ret, "None, ");
1200 }
1201 /* Kill last comma. */
1202 ret[strlen(ret) - 2] = '\0';
1203 ret = realloc(ret, strlen(ret) + 1);
1204 return ret;
1205}
1206
Edward O'Callaghan20596a82019-06-13 14:47:03 +10001207int probe_flash(struct registered_master *mst, int startchip,
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001208 struct flashctx *flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001209{
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001210 const struct flashchip *chip, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001211 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001212 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001213 uint32_t size;
1214 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001215 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001216
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301217 /* Based on the host controller interface that a platform
1218 * needs to use (hwseq or swseq),
1219 * set the flashchips list here.
1220 */
Edward O'Callaghane3e30562019-09-03 13:10:58 +10001221 switch (g_ich_generation) {
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301222 case CHIPSET_100_SERIES_SUNRISE_POINT:
Furquan Shaikh44088752016-07-11 22:48:08 -07001223 case CHIPSET_APL:
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301224 flash_list = flashchips_hwseq;
1225 break;
1226 default:
1227 flash_list = flashchips;
1228 break;
1229 }
1230
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001231 for (chip = flash_list + startchip; chip && chip->name; chip++) {
1232 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001233 continue;
Craig Hesling65eb8812019-08-01 09:33:56 -07001234 buses_common = buses_supported & chip->bustype;
Edward O'Callaghan4b940572019-08-02 01:44:47 +10001235 if (!buses_common)
hailfinger18bd4cc2011-06-17 22:38:53 +00001236 continue;
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001237 /* Only probe for SPI25 chips by default. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001238 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
Edward O'Callaghancc1d0c92019-02-24 15:35:07 +11001239 continue;
hailfinger18bd4cc2011-06-17 22:38:53 +00001240 msg_gdbg("Probing for %s %s, %d kB: ",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001241 chip->vendor, chip->name, chip->total_size);
1242 if (!chip->probe && !force) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001243 msg_gdbg("failed! flashrom has no probe function for "
1244 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001245 continue;
1246 }
stepan782fb172007-04-06 11:58:03 +00001247
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001248 size = chip->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001249 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001250
hailfinger48ed3e22011-05-04 00:39:50 +00001251 /* Start filling in the dynamic data. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001252 flash->chip = calloc(1, sizeof(struct flashchip));
1253 if (!flash->chip) {
Patrick Georgif3fa2992017-02-02 16:24:44 +01001254 msg_gerr("Out of memory!\n");
1255 exit(1);
1256 }
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001257 memcpy(flash->chip, chip, sizeof(struct flashchip));
1258 flash->mst = mst;
hailfinger48ed3e22011-05-04 00:39:50 +00001259
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +11001260 if (map_flash(flash) != 0)
1261 goto notfound;
rminnich8d3ff912003-10-25 17:01:29 +00001262
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +11001263 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
1264 * is only called with force=1 after normal probing failed.
1265 */
stugec1e55fe2008-07-02 17:15:47 +00001266 if (force)
1267 break;
stepanc98b80b2006-03-16 16:57:41 +00001268
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001269 if (flash->chip->probe(flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001270 goto notfound;
1271
hailfinger48ed3e22011-05-04 00:39:50 +00001272 /* If this is the first chip found, accept it.
1273 * If this is not the first chip found, accept it only if it is
1274 * a non-generic match.
1275 * We could either make chipcount global or provide it as
1276 * parameter, or we assume that startchip==0 means this call to
1277 * probe_flash() is the first one and thus no chip has been
1278 * found before.
1279 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001280 if (startchip == 0 || flash->chip->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001281 break;
1282
stuge56300c32008-09-03 23:10:05 +00001283notfound:
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +11001284 unmap_flash(flash);
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001285 free(flash->chip);
1286 flash->chip = NULL;
rminnich8d3ff912003-10-25 17:01:29 +00001287 }
uwebe4477b2007-08-23 16:08:21 +00001288
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001289 if (!chip || !chip->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001290 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001291
hailfingere11396b2011-03-08 00:09:11 +00001292#if CONFIG_INTERNAL == 1
1293 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001294 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001295 else
1296#endif
stepan3e7aeae2011-01-19 06:21:54 +00001297 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001298
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001299 tmp = flashbuses_to_text(chip->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001300 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001301 force ? "Assuming" : "Found", flash->chip->vendor,
1302 flash->chip->name, flash->chip->total_size, tmp,
Patrick Georgif3fa2992017-02-02 16:24:44 +01001303 location);
stefanct588b6d22011-06-26 20:45:35 +00001304 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001305
hailfinger0f4c3952010-12-02 21:59:42 +00001306 /* Flash registers will not be mapped if the chip was forced. Lock info
1307 * may be stored in registers, so avoid lock info printing.
1308 */
1309 if (!force)
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001310 if (flash->chip->printlock)
1311 flash->chip->printlock(flash);
snelson1ee293c2010-02-19 00:52:10 +00001312
Edward O'Callaghan2e04fd82019-10-29 15:00:58 +11001313 /* Get out of the way for later runs. */
1314 unmap_flash(flash);
1315
hailfinger48ed3e22011-05-04 00:39:50 +00001316 /* Return position of matching chip. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001317 return chip - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001318}
1319
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001320static int verify_flash(struct flashctx *flash,
1321 struct action_descriptor *descriptor,
1322 int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001323{
hailfingerb0f4d122009-06-24 08:20:45 +00001324 int ret;
Patrick Georgif3fa2992017-02-02 16:24:44 +01001325 unsigned int total_size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001326 uint8_t *buf = descriptor->newcontents;
rminnich8d3ff912003-10-25 17:01:29 +00001327
snelsone42c3802010-05-07 20:09:04 +00001328 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001329
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001330 if (verify_it == VERIFY_PARTIAL) {
1331 struct processing_unit *pu = descriptor->processing_units;
1332
1333 /* Verify only areas which were written. */
1334 while (pu->num_blocks) {
1335 ret = verify_range(flash, buf + pu->offset, pu->offset,
1336 pu->block_size * pu->num_blocks,
1337 NULL);
1338 if (ret)
1339 break;
1340 pu++;
1341 }
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001342 } else {
1343 ret = verify_range(flash, buf, 0, total_size, NULL);
1344 }
uwef6641642007-05-09 10:17:44 +00001345
David Hendricks1ed1d352011-11-23 17:54:37 -08001346 if (ret == ACCESS_DENIED) {
1347 msg_gdbg("Could not fully verify due to access error, ");
1348 if (access_denied_action == error_ignore) {
1349 msg_gdbg("ignoring\n");
1350 ret = 0;
1351 } else {
1352 msg_gdbg("aborting\n");
1353 }
1354 }
1355
hailfingerb0f4d122009-06-24 08:20:45 +00001356 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001357 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001358
hailfingerb0f4d122009-06-24 08:20:45 +00001359 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001360}
1361
uwe8d342eb2011-07-28 08:13:25 +00001362int read_buf_from_file(unsigned char *buf, unsigned long size,
1363 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001364{
1365 unsigned long numbytes;
1366 FILE *image;
1367 struct stat image_stat;
1368
Vincent Palatin7ab23932014-10-01 12:09:16 -07001369 if (!strncmp(filename, "-", sizeof("-")))
1370 image = fdopen(STDIN_FILENO, "rb");
1371 else
1372 image = fopen(filename, "rb");
1373 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001374 perror(filename);
1375 return 1;
1376 }
1377 if (fstat(fileno(image), &image_stat) != 0) {
1378 perror(filename);
1379 fclose(image);
1380 return 1;
1381 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001382 if ((image_stat.st_size != size) &&
1383 (strncmp(filename, "-", sizeof("-")))) {
Mike Frysinger62c794d2017-05-29 12:02:45 -04001384 msg_gerr("Error: Image size doesn't match: stat %jd bytes, "
1385 "wanted %ld!\n", (intmax_t)image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001386 fclose(image);
1387 return 1;
1388 }
1389 numbytes = fread(buf, 1, size, image);
1390 if (fclose(image)) {
1391 perror(filename);
1392 return 1;
1393 }
1394 if (numbytes != size) {
1395 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1396 "wanted %ld!\n", numbytes, size);
1397 return 1;
1398 }
1399 return 0;
1400}
1401
uwe8d342eb2011-07-28 08:13:25 +00001402int write_buf_to_file(unsigned char *buf, unsigned long size,
1403 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001404{
1405 unsigned long numbytes;
1406 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001407
1408 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001409 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001410 return 1;
1411 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001412 if (!strncmp(filename, "-", sizeof("-")))
1413 image = fdopen(STDOUT_FILENO, "wb");
1414 else
1415 image = fopen(filename, "wb");
1416 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001417 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001418 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001419 }
hailfingerd219a232009-01-28 00:27:54 +00001420
hailfingerd219a232009-01-28 00:27:54 +00001421 numbytes = fwrite(buf, 1, size, image);
1422 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001423 if (numbytes != size) {
1424 msg_gerr("File %s could not be written completely.\n",
1425 filename);
hailfingerd219a232009-01-28 00:27:54 +00001426 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001427 }
hailfingerd219a232009-01-28 00:27:54 +00001428 return 0;
1429}
1430
David Hendrickse3451942013-03-21 17:23:29 -07001431/*
1432 * read_flash - wrapper for flash->read() with additional high-level policy
1433 *
1434 * @flash flash chip
1435 * @buf buffer to store data in
1436 * @start start address
1437 * @len number of bytes to read
1438 *
1439 * This wrapper simplifies most cases when the flash chip needs to be read
1440 * since policy decisions such as non-fatal error handling is centralized.
1441 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001442int read_flash(struct flashctx *flash, uint8_t *buf,
David Hendrickse3451942013-03-21 17:23:29 -07001443 unsigned int start, unsigned int len)
1444{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001445 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001446
Patrick Georgif3fa2992017-02-02 16:24:44 +01001447 if (!flash || !flash->chip->read)
David Hendrickse3451942013-03-21 17:23:29 -07001448 return -1;
1449
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001450 msg_cdbg("%#06x-%#06x:R ", start, start + len - 1);
1451
Patrick Georgif3fa2992017-02-02 16:24:44 +01001452 ret = flash->chip->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001453 if (ret) {
1454 if (ignore_error(ret)) {
1455 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1456 start, start + len - 1);
1457 ret = 0;
1458 } else {
1459 msg_gdbg("failed to read 0x%x-0x%x\n",
1460 start, start + len - 1);
1461 }
1462 }
1463
1464 return ret;
1465}
1466
David Hendricks7c8a1612013-04-26 19:14:44 -07001467/*
1468 * write_flash - wrapper for flash->write() with additional high-level policy
1469 *
1470 * @flash flash chip
1471 * @buf buffer to write to flash
1472 * @start start address in flash
1473 * @len number of bytes to write
1474 *
1475 * TODO: Look up regions that are write-protected and avoid attempt to write
1476 * to them at all.
1477 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001478int write_flash(struct flashctx *flash, uint8_t *buf,
David Hendricks7c8a1612013-04-26 19:14:44 -07001479 unsigned int start, unsigned int len)
1480{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001481 if (!flash || !flash->chip->write)
David Hendricks7c8a1612013-04-26 19:14:44 -07001482 return -1;
1483
Patrick Georgif3fa2992017-02-02 16:24:44 +01001484 return flash->chip->write(flash, buf, start, len);
David Hendricks7c8a1612013-04-26 19:14:44 -07001485}
1486
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001487int read_flash_to_file(struct flashctx *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001488{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001489 unsigned long size = flash->chip->total_size * 1024;
Richard Hughes74eec602018-12-19 15:30:39 +00001490 unsigned char *buf = calloc(size, sizeof(unsigned char));
hailfinger42a850a2010-07-13 23:56:13 +00001491 int ret = 0;
1492
1493 msg_cinfo("Reading flash... ");
1494 if (!buf) {
1495 msg_gerr("Memory allocation failed!\n");
1496 msg_cinfo("FAILED.\n");
1497 return 1;
1498 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001499
1500 /* To support partial read, fill buffer to all 0xFF at beginning to make
1501 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001502 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001503
Patrick Georgif3fa2992017-02-02 16:24:44 +01001504 if (!flash->chip->read) {
hailfinger42a850a2010-07-13 23:56:13 +00001505 msg_cerr("No read function available for this flash chip.\n");
1506 ret = 1;
1507 goto out_free;
1508 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001509
1510 /* First try to handle partial read case, rather than read the whole
1511 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001512 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001513 if (ret < 0) {
1514 msg_cerr("Partial read operation failed!\n");
1515 ret = 1;
1516 goto out_free;
1517 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001518 int num_regions = get_num_include_args();
1519
1520 if (ret != num_regions) {
1521 msg_cerr("Requested %d regions, but only read %d\n",
1522 num_regions, ret);
1523 ret = 1;
1524 goto out_free;
1525 }
1526
1527 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001528 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001529 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001530 msg_cerr("Read operation failed!\n");
1531 ret = 1;
1532 goto out_free;
1533 }
hailfinger42a850a2010-07-13 23:56:13 +00001534 }
1535
David Hendricksdf29a832013-06-28 14:33:51 -07001536 if (filename)
1537 ret = write_buf_to_file(buf, size, filename);
1538
hailfinger42a850a2010-07-13 23:56:13 +00001539out_free:
1540 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001541 if (ret)
1542 msg_cerr("FAILED.");
1543 else
1544 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001545 return ret;
1546}
1547
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001548/* Even if an error is found, the function will keep going and check the rest. */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001549static int selfcheck_eraseblocks(const struct flashchip *chip)
hailfinger45177872010-01-18 08:14:43 +00001550{
hailfingerb91c08c2011-08-15 19:54:20 +00001551 int i, j, k;
1552 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001553
1554 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1555 unsigned int done = 0;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001556 struct block_eraser eraser = chip->block_erasers[k];
hailfinger45177872010-01-18 08:14:43 +00001557
1558 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1559 /* Blocks with zero size are bugs in flashchips.c. */
1560 if (eraser.eraseblocks[i].count &&
1561 !eraser.eraseblocks[i].size) {
1562 msg_gerr("ERROR: Flash chip %s erase function "
1563 "%i region %i has size 0. Please report"
1564 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001565 chip->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001566 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001567 }
1568 /* Blocks with zero count are bugs in flashchips.c. */
1569 if (!eraser.eraseblocks[i].count &&
1570 eraser.eraseblocks[i].size) {
1571 msg_gerr("ERROR: Flash chip %s erase function "
1572 "%i region %i has count 0. Please report"
1573 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001574 chip->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001575 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001576 }
1577 done += eraser.eraseblocks[i].count *
1578 eraser.eraseblocks[i].size;
1579 }
hailfinger9fed35d2010-01-19 06:42:46 +00001580 /* Empty eraseblock definition with erase function. */
1581 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001582 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001583 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001584 if (!done)
1585 continue;
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001586 if (done != chip->total_size * 1024) {
hailfinger45177872010-01-18 08:14:43 +00001587 msg_gerr("ERROR: Flash chip %s erase function %i "
1588 "region walking resulted in 0x%06x bytes total,"
1589 " expected 0x%06x bytes. Please report a bug at"
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001590 " flashrom@flashrom.org\n", chip->name, k,
1591 done, chip->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001592 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001593 }
hailfinger9fed35d2010-01-19 06:42:46 +00001594 if (!eraser.block_erase)
1595 continue;
1596 /* Check if there are identical erase functions for different
1597 * layouts. That would imply "magic" erase functions. The
1598 * easiest way to check this is with function pointers.
1599 */
uwef6f94d42010-03-13 17:28:29 +00001600 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001601 if (eraser.block_erase ==
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001602 chip->block_erasers[j].block_erase) {
hailfinger9fed35d2010-01-19 06:42:46 +00001603 msg_gerr("ERROR: Flash chip %s erase function "
1604 "%i and %i are identical. Please report"
1605 " a bug at flashrom@flashrom.org\n",
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001606 chip->name, k, j);
hailfinger9fed35d2010-01-19 06:42:46 +00001607 ret = 1;
1608 }
uwef6f94d42010-03-13 17:28:29 +00001609 }
hailfinger45177872010-01-18 08:14:43 +00001610 }
hailfinger9fed35d2010-01-19 06:42:46 +00001611 return ret;
hailfinger45177872010-01-18 08:14:43 +00001612}
1613
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001614static int erase_and_write_block_helper(struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001615 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001616 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001617 uint8_t *newcontents,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001618 int (*erasefn) (struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001619 unsigned int addr,
1620 unsigned int len))
1621{
stefanctc5eb8a92011-11-23 09:13:48 +00001622 unsigned int starthere = 0, lenhere = 0;
1623 int ret = 0, skip = 1, writecount = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001624 int block_was_erased = 0;
Edward O'Callaghan10e63d92019-06-17 14:12:52 +10001625 enum write_granularity gran = flash->chip->gran;
hailfingerb437e282010-11-04 01:04:27 +00001626
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001627 /*
1628 * curcontents and newcontents are opaque to walk_eraseregions, and
1629 * need to be adjusted here to keep the impression of proper
1630 * abstraction
hailfingerb437e282010-11-04 01:04:27 +00001631 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001632
hailfinger90fcf9b2010-11-05 14:51:59 +00001633 curcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001634
hailfingerb437e282010-11-04 01:04:27 +00001635 newcontents += start;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001636
hailfingerb437e282010-11-04 01:04:27 +00001637 msg_cdbg(":");
Simon Glass4c214132013-07-16 10:09:28 -06001638 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001639 content_has_changed |= 1;
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001640 msg_cdbg(" E");
hailfingerb437e282010-11-04 01:04:27 +00001641 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001642 if (ret) {
1643 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001644 msg_cdbg(" DENIED");
David Hendricks1ed1d352011-11-23 17:54:37 -08001645 else
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001646 msg_cerr(" ERASE_FAILED\n");
hailfingerb437e282010-11-04 01:04:27 +00001647 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001648 }
1649
David Hendricks0954ffc2015-11-13 15:15:44 -08001650 if (programmer_table[programmer].paranoid) {
1651 if (check_erased_range(flash, start, len)) {
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001652 msg_cerr(" ERASE_FAILED\n");
David Hendricks0954ffc2015-11-13 15:15:44 -08001653 return -1;
1654 }
hailfingerac8e3182011-06-26 17:04:16 +00001655 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001656
hailfinger90fcf9b2010-11-05 14:51:59 +00001657 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001658 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001659 skip = 0;
David Hendricks048b38c2016-03-28 18:47:06 -07001660 block_was_erased = 1;
hailfingerb437e282010-11-04 01:04:27 +00001661 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001662 /* get_next_write() sets starthere to a new value after the call. */
1663 while ((lenhere = get_next_write(curcontents + starthere,
1664 newcontents + starthere,
1665 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001666 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001667 if (!writecount++)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001668 msg_cdbg(" W");
hailfingerb437e282010-11-04 01:04:27 +00001669 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001670 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001671 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001672 if (ret) {
1673 if (ret == ACCESS_DENIED)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001674 msg_cdbg(" DENIED");
hailfingerb437e282010-11-04 01:04:27 +00001675 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001676 }
David Hendricks048b38c2016-03-28 18:47:06 -07001677
1678 /*
1679 * If the block needed to be erased and was erased successfully
1680 * then we can assume that we didn't run into any write-
1681 * protected areas. Otherwise, we need to verify each page to
1682 * ensure it was successfully written and abort if we encounter
1683 * any errors.
1684 */
1685 if (programmer_table[programmer].paranoid && !block_was_erased) {
1686 if (verify_range(flash, newcontents + starthere,
1687 start + starthere, lenhere, "WRITE"))
1688 return -1;
1689 }
1690
hailfingerb437e282010-11-04 01:04:27 +00001691 starthere += lenhere;
1692 skip = 0;
1693 }
1694 if (skip)
Daisuke Nojiri446b6732018-09-07 18:32:56 -07001695 msg_cdbg(" SKIP");
hailfingerb437e282010-11-04 01:04:27 +00001696 return ret;
1697}
1698
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001699/*
1700 * Function to process processing units accumulated in the action descriptor.
1701 *
1702 * @flash pointer to the flash context to operate on
1703 * @do_something helper function which can erase and program a section of the
1704 * flash chip. It receives the flash context, offset and length
1705 * of the area to erase/program, before and after contents (to
1706 * decide what exactly needs to be erased and or programmed)
1707 * and a pointer to the erase function which can operate on the
1708 * proper granularity.
1709 * @descriptor action descriptor including pointers to before and after
1710 * contents and an array of processing actions to take.
1711 *
1712 * Returns zero on success or an error code.
1713 */
1714static int walk_eraseregions(struct flashctx *flash,
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001715 int (*do_something) (struct flashctx *flash,
hailfinger83541b32010-07-13 00:42:00 +00001716 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001717 unsigned int len,
1718 uint8_t *param1,
1719 uint8_t *param2,
1720 int (*erasefn) (
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001721 struct flashctx *flash,
hailfingerb437e282010-11-04 01:04:27 +00001722 unsigned int addr,
1723 unsigned int len)),
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001724 struct action_descriptor *descriptor)
hailfinger2b8c9382010-07-13 00:37:19 +00001725{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001726 struct processing_unit *pu;
1727 int rc = 0;
1728 static int print_comma;
uwe8d342eb2011-07-28 08:13:25 +00001729
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001730 for (pu = descriptor->processing_units; pu->num_blocks; pu++) {
1731 unsigned base = pu->offset;
1732 unsigned top = pu->offset + pu->block_size * pu->num_blocks;
David Hendricks605544b2015-08-15 16:32:58 -07001733
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001734 while (base < top) {
David Hendricks605544b2015-08-15 16:32:58 -07001735
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001736 if (print_comma)
hailfingerb437e282010-11-04 01:04:27 +00001737 msg_cdbg(", ");
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001738 else
1739 print_comma = 1;
1740
1741 msg_cdbg("0x%06x-0x%06zx", base, base + pu->block_size - 1);
1742
1743 rc = do_something(flash, base,
1744 pu->block_size,
1745 descriptor->oldcontents,
1746 descriptor->newcontents,
1747 flash->chip->block_erasers[pu->block_eraser_index].block_erase);
1748
David Hendricks1ed1d352011-11-23 17:54:37 -08001749 if (rc) {
1750 if (ignore_error(rc))
1751 rc = 0;
1752 else
1753 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001754 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001755 base += pu->block_size;
hailfinger2b8c9382010-07-13 00:37:19 +00001756 }
1757 }
hailfingerb437e282010-11-04 01:04:27 +00001758 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001759 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001760}
1761
Souvik Ghoshd75cd672016-06-17 14:21:39 -07001762static int check_block_eraser(const struct flashctx *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001763{
Patrick Georgif3fa2992017-02-02 16:24:44 +01001764 struct block_eraser eraser = flash->chip->block_erasers[k];
hailfingercf848f12010-12-05 15:14:44 +00001765
1766 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1767 if (log)
1768 msg_cdbg("not defined. ");
1769 return 1;
1770 }
1771 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1772 if (log)
1773 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001774 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001775 return 1;
1776 }
1777 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1778 if (log)
1779 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001780 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001781 return 1;
1782 }
1783 return 0;
1784}
1785
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001786int erase_and_write_flash(struct flashctx *flash,
1787 struct action_descriptor *descriptor)
hailfingerd219a232009-01-28 00:27:54 +00001788{
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001789 int ret = 1;
hailfingercf848f12010-12-05 15:14:44 +00001790
hailfingercf848f12010-12-05 15:14:44 +00001791 msg_cinfo("Erasing and writing flash chip... ");
hailfingerb437e282010-11-04 01:04:27 +00001792
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07001793 ret = walk_eraseregions(flash, &erase_and_write_block_helper, descriptor);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001794
hailfinger7df21362009-09-05 02:30:58 +00001795 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001796 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001797 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001798 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001799 }
1800 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001801}
1802
hailfinger4c47e9d2010-10-19 22:06:20 +00001803void nonfatal_help_message(void)
1804{
1805 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1806 "This means we have to add special support for your board, "
1807 "programmer or flash chip.\n"
1808 "Please report this on IRC at irc.freenode.net (channel "
1809 "#flashrom) or\n"
1810 "mail flashrom@flashrom.org!\n"
1811 "-------------------------------------------------------------"
1812 "------------------\n"
1813 "You may now reboot or simply leave the machine running.\n");
1814}
1815
uweb34ec9f2009-10-01 18:40:02 +00001816void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001817{
snelsone42c3802010-05-07 20:09:04 +00001818 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001819 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001820 "mail flashrom@flashrom.org with FAILED: your board name in "
1821 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001822 "-------------------------------------------------------------"
1823 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001824 "DO NOT REBOOT OR POWEROFF!\n");
1825}
1826
uwe8d342eb2011-07-28 08:13:25 +00001827/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001828void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001829{
1830 enum programmer p;
1831 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001832 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001833 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001834 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001835 }
Simon Glass8dc82732013-07-16 10:13:51 -06001836 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001837}
1838
hailfingerf79d1712010-10-06 23:48:34 +00001839void list_programmers_linebreak(int startcol, int cols, int paren)
1840{
1841 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001842 int pnamelen;
1843 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001844 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001845 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001846
1847 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1848 pname = programmer_table[p].name;
1849 pnamelen = strlen(pname);
1850 if (remaining - pnamelen - 2 < 0) {
1851 if (firstline)
1852 firstline = 0;
1853 else
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001854 msg_ginfo("\n");
hailfingerf79d1712010-10-06 23:48:34 +00001855 for (i = 0; i < startcol; i++)
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001856 msg_ginfo(" ");
hailfingerf79d1712010-10-06 23:48:34 +00001857 remaining = cols - startcol;
1858 } else {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001859 msg_ginfo(" ");
hailfingerf79d1712010-10-06 23:48:34 +00001860 remaining--;
1861 }
1862 if (paren && (p == 0)) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001863 msg_ginfo("(");
hailfingerf79d1712010-10-06 23:48:34 +00001864 remaining--;
1865 }
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001866 msg_ginfo("%s", pname);
hailfingerf79d1712010-10-06 23:48:34 +00001867 remaining -= pnamelen;
1868 if (p < PROGRAMMER_INVALID - 1) {
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001869 msg_ginfo(",");
hailfingerf79d1712010-10-06 23:48:34 +00001870 remaining--;
1871 } else {
1872 if (paren)
Edward O'Callaghan90aaa302019-05-21 14:43:38 +10001873 msg_ginfo(")");
1874 msg_ginfo("\n");
hailfingerf79d1712010-10-06 23:48:34 +00001875 }
1876 }
1877}
1878
hailfinger3b471632010-03-27 16:36:40 +00001879void print_sysinfo(void)
1880{
David Hendricksc6c9f822010-11-03 15:07:01 -07001881 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001882#if HAVE_UTSNAME == 1
1883 struct utsname osinfo;
1884 uname(&osinfo);
1885
David Hendricksc6c9f822010-11-03 15:07:01 -07001886 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001887 osinfo.machine);
1888#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001889 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001890#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001891}
1892
1893void print_buildinfo(void)
1894{
1895 msg_gdbg("flashrom was built with");
hailfinger3b471632010-03-27 16:36:40 +00001896#if NEED_PCI == 1
1897#ifdef PCILIB_VERSION
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001898 msg_gdbg(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001899#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001900 msg_gdbg(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001901#endif
1902#endif
1903#ifdef __clang__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001904 msg_gdbg(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001905#ifdef __clang_version__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001906 msg_gdbg(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001907#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001908 msg_gdbg(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001909#endif
hailfinger3b471632010-03-27 16:36:40 +00001910#elif defined(__GNUC__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001911 msg_gdbg(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001912#ifdef __VERSION__
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001913 msg_gdbg(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001914#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001915 msg_gdbg(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001916#endif
1917#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001918 msg_gdbg(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001919#endif
1920#if defined (__FLASHROM_LITTLE_ENDIAN__)
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001921 msg_gdbg(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001922#else
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001923 msg_gdbg(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001924#endif
Souvik Ghosh3c963a42016-07-19 18:48:15 -07001925 msg_gdbg("\n");
hailfinger3b471632010-03-27 16:36:40 +00001926}
1927
uwefdeca092008-01-21 15:24:22 +00001928void print_version(void)
1929{
David Hendricksc6c9f822010-11-03 15:07:01 -07001930 /* send to stderr for chromium os */
1931 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001932 print_sysinfo();
David Hendricks07268292016-09-14 16:05:58 -07001933 msg_gerr("\n");
uwefdeca092008-01-21 15:24:22 +00001934}
1935
hailfinger74819ad2010-05-15 15:04:37 +00001936void print_banner(void)
1937{
1938 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001939 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001940 msg_ginfo("\n");
1941}
1942
hailfingerc77acb52009-12-24 02:15:55 +00001943int selfcheck(void)
1944{
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001945 unsigned int i;
hailfinger45177872010-01-18 08:14:43 +00001946 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001947
1948 /* Safety check. Instead of aborting after the first error, check
1949 * if more errors exist.
1950 */
hailfingerc77acb52009-12-24 02:15:55 +00001951 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001952 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001953 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001954 }
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001955 /* It would be favorable if we could check for the correct layout (especially termination) of various
1956 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1957 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1958 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1959 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1960 * checks below.
1961 */
1962 if (flashchips_size <= 1 || flashchips[flashchips_size-1].name != NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001963 msg_gerr("Flashchips table miscompilation!\n");
1964 ret = 1;
Edward O'Callaghan6240c852019-07-02 15:49:58 +10001965 } else {
1966 for (i = 0; i < flashchips_size - 1; i++) {
1967 const struct flashchip *chip = &flashchips[i];
1968 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1969 ret = 1;
1970 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
1971 "Please report a bug at flashrom@flashrom.org\n", i,
1972 chip->name == NULL ? "unnamed" : chip->name);
1973 }
1974 if (selfcheck_eraseblocks(chip))
1975 ret = 1;
1976 }
stefanct6d836ba2011-05-26 01:35:19 +00001977 }
stefanct6d836ba2011-05-26 01:35:19 +00001978
hailfinger45177872010-01-18 08:14:43 +00001979 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001980}
1981
hailfinger771fc182010-10-15 00:01:14 +00001982/* FIXME: This function signature needs to be improved once doit() has a better
1983 * function signature.
1984 */
Edward O'Callaghanf93b3742019-02-24 17:24:27 +11001985int 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 +00001986{
Patrick Georgiac3423f2017-02-03 20:58:06 +01001987 const struct flashchip *chip = flash->chip;
1988
hailfinger771fc182010-10-15 00:01:14 +00001989 if (!programmer_may_write && (write_it || erase_it)) {
1990 msg_perr("Write/erase is not working yet on your programmer in "
1991 "its current configuration.\n");
1992 /* --force is the wrong approach, but it's the best we can do
1993 * until the generic programmer parameter parser is merged.
1994 */
1995 if (!force)
1996 return 1;
1997 msg_cerr("Continuing anyway.\n");
1998 }
1999
2000 if (read_it || erase_it || write_it || verify_it) {
2001 /* Everything needs read. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01002002 if (chip->tested.read == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00002003 msg_cerr("Read is not working on this chip. ");
2004 if (!force)
2005 return 1;
2006 msg_cerr("Continuing anyway.\n");
2007 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01002008 if (!chip->read) {
hailfinger771fc182010-10-15 00:01:14 +00002009 msg_cerr("flashrom has no read function for this "
2010 "flash chip.\n");
2011 return 1;
2012 }
2013 }
2014 if (erase_it || write_it) {
2015 /* Write needs erase. */
Patrick Georgiac3423f2017-02-03 20:58:06 +01002016 if (chip->tested.erase == NA) {
2017 msg_cerr("Erase is not possible on this chip.\n");
2018 return 1;
2019 }
2020 if (chip->tested.erase == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00002021 msg_cerr("Erase is not working on this chip. ");
2022 if (!force)
2023 return 1;
2024 msg_cerr("Continuing anyway.\n");
2025 }
stefancte1c5acf2011-07-04 07:27:17 +00002026 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00002027 msg_cerr("flashrom has no erase function for this "
2028 "flash chip.\n");
2029 return 1;
2030 }
hailfinger771fc182010-10-15 00:01:14 +00002031 }
2032 if (write_it) {
Patrick Georgiac3423f2017-02-03 20:58:06 +01002033 if (chip->tested.write == NA) {
2034 msg_cerr("Write is not possible on this chip.\n");
2035 return 1;
2036 }
2037 if (chip->tested.write == BAD) {
hailfinger771fc182010-10-15 00:01:14 +00002038 msg_cerr("Write is not working on this chip. ");
2039 if (!force)
2040 return 1;
2041 msg_cerr("Continuing anyway.\n");
2042 }
Patrick Georgiac3423f2017-02-03 20:58:06 +01002043 if (!chip->write) {
hailfinger771fc182010-10-15 00:01:14 +00002044 msg_cerr("flashrom has no write function for this "
2045 "flash chip.\n");
2046 return 1;
2047 }
2048 }
2049 return 0;
2050}
2051
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002052/*
2053 * Function to erase entire flash chip.
2054 *
2055 * @flashctx pointer to the flash context to use
2056 * @oldcontents pointer to the buffer including current chip contents, to
2057 * decide which areas do in fact need to be erased
2058 * @size the size of the flash chip, in bytes.
2059 *
2060 * Returns zero on success or an error code.
2061 */
2062static int erase_chip(struct flashctx *flash, void *oldcontents,
2063 void *newcontents, size_t size)
2064{
2065 /*
2066 * To make sure that the chip is fully erased, let's cheat and create
2067 * a descriptor where the new contents are all erased.
2068 */
2069 struct action_descriptor *fake_descriptor;
2070 int ret = 0;
2071
2072 fake_descriptor = prepare_action_descriptor(flash, oldcontents,
2073 newcontents, 1);
2074 /* FIXME: Do we really want the scary warning if erase failed? After
2075 * all, after erase the chip is either blank or partially blank or it
2076 * has the old contents. A blank chip won't boot, so if the user
2077 * wanted erase and reboots afterwards, the user knows very well that
2078 * booting won't work.
2079 */
2080 if (erase_and_write_flash(flash, fake_descriptor)) {
2081 emergency_help_message();
2082 ret = 1;
2083 }
2084
2085 free(fake_descriptor);
2086
2087 return ret;
2088}
2089
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002090static int read_dest_content(struct flashctx *flash, int verify_it,
2091 uint8_t *dest, unsigned long size)
2092{
2093 if (((verify_it == VERIFY_OFF) || (verify_it == VERIFY_PARTIAL))
2094 && get_num_include_args()) {
2095 /*
2096 * If no full verification is required and not
2097 * the entire chip is about to be programmed,
2098 * read only the areas which might change.
2099 */
2100 if (handle_partial_read(flash, dest, read_flash, 0) < 0)
2101 return 1;
2102 } else {
2103 if (read_flash(flash, dest, 0, size))
2104 return 1;
2105 }
2106 return 0;
2107}
2108
hailfingerc77acb52009-12-24 02:15:55 +00002109/* This function signature is horrible. We need to design a better interface,
2110 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00002111 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00002112 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -07002113int doit(struct flashctx *flash, int force, const char *filename, int read_it,
Simon Glass9ad06c12013-07-03 22:08:17 +09002114 int write_it, int erase_it, int verify_it, int extract_it,
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002115 const char *diff_file, int do_diff)
hailfingerc77acb52009-12-24 02:15:55 +00002116{
hailfinger4c47e9d2010-10-19 22:06:20 +00002117 uint8_t *oldcontents;
2118 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00002119 int ret = 0;
Patrick Georgif3fa2992017-02-02 16:24:44 +01002120 unsigned long size = flash->chip->total_size * 1024;
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002121 struct action_descriptor *descriptor = NULL;
hailfingerc77acb52009-12-24 02:15:55 +00002122
stefanct02116582011-05-18 01:30:56 +00002123 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00002124 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00002125 ret = 1;
2126 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00002127 }
2128
hailfinger771fc182010-10-15 00:01:14 +00002129 /* Given the existence of read locks, we want to unlock for read,
2130 * erase and write.
2131 */
Patrick Georgif3fa2992017-02-02 16:24:44 +01002132 if (flash->chip->unlock)
2133 flash->chip->unlock(flash);
hailfinger771fc182010-10-15 00:01:14 +00002134
Edward O'Callaghana74ffcd2019-06-17 14:59:55 +10002135 flash->address_high_byte = -1;
2136 flash->in_4ba_mode = false;
2137
Ed Swierk28cf7992017-07-03 13:17:18 -07002138 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Edward O'Callaghan9713aa62019-07-18 18:28:57 +10002139 if ((flash->chip->feature_bits & FEATURE_4BA_ENTER_WREN) && flash->chip->set_4ba) {
Edward O'Callaghan4fe3a972019-06-19 16:56:10 +10002140 if (flash->chip->set_4ba(flash)) {
Ed Swierk28cf7992017-07-03 13:17:18 -07002141 msg_cerr("Enabling/disabling 4-byte addressing mode failed!\n");
2142 return 1;
Boris Baykov6323c242016-06-11 18:29:03 +02002143 }
Boris Baykov1a2f5322016-06-11 18:29:00 +02002144 }
2145
Simon Glass9ad06c12013-07-03 22:08:17 +09002146 if (extract_it) {
2147 ret = extract_regions(flash);
2148 goto out_nofree;
2149 }
2150
David Hendricksd0ea9ed2011-03-04 17:31:57 -08002151 /* mark entries included using -i argument as "included" if they are
2152 found in the master rom_entries list */
2153 if (process_include_args() < 0) {
2154 ret = 1;
2155 goto out_nofree;
2156 }
2157
hailfinger771fc182010-10-15 00:01:14 +00002158 if (read_it) {
2159 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00002160 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00002161 }
hailfingerb437e282010-11-04 01:04:27 +00002162
stefanctd611e8f2011-07-12 22:35:21 +00002163 oldcontents = malloc(size);
2164 if (!oldcontents) {
2165 msg_gerr("Out of memory!\n");
2166 exit(1);
2167 }
Simon Glass4c214132013-07-16 10:09:28 -06002168 /* Assume worst case: All blocks are not erased. */
2169 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00002170 newcontents = malloc(size);
2171 if (!newcontents) {
2172 msg_gerr("Out of memory!\n");
2173 exit(1);
2174 }
Simon Glass4c214132013-07-16 10:09:28 -06002175 /* Assume best case: All blocks are erased. */
2176 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002177 /* Side effect of the assumptions above: Default write action is erase
2178 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002179 * oldcontents being completely unerased means we have to erase
2180 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002181 */
2182
hailfingerd217d122010-10-08 18:52:29 +00002183 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002184 /*
2185 * Note: This must be done before any files specified by -i
2186 * arguments are processed merged into the newcontents since
2187 * -i files take priority. See http://crbug.com/263495.
2188 */
2189 if (filename) {
2190 if (read_buf_from_file(newcontents, size, filename)) {
2191 ret = 1;
2192 goto out;
2193 }
2194 } else {
2195 /* Content will be read from -i args, so they must
2196 * not overlap. */
2197 if (included_regions_overlap()) {
2198 msg_gerr("Error: Included regions must "
2199 "not overlap.\n");
2200 ret = 1;
2201 goto out;
2202 }
stepan1da96c02006-11-21 23:48:51 +00002203 }
2204
David Hendricksac82cac2012-06-19 10:29:37 -07002205#if 0
2206 /*
2207 * FIXME: show_id() causes failure if vendor:mainboard do not
2208 * match. This may happen if codenames are in flux.
2209 * See chrome-os-partner:10414.
2210 */
hailfinger90c7d542010-05-31 15:27:27 +00002211#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002212 if (programmer == PROGRAMMER_INTERNAL)
2213 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002214#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002215#endif
ollie5672ac62004-03-17 22:22:08 +00002216 }
2217
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002218 if (do_diff) {
2219 /*
2220 * Obtain a reference image so that we can check whether
2221 * regions need to be erased and to give better diagnostics in
2222 * case write fails. If --fast-verify is used then only the
2223 * regions which are included using -i will be read.
2224 */
2225 if (diff_file) {
2226 msg_cdbg("Reading old contents from file... ");
2227 if (read_buf_from_file(oldcontents, size, diff_file)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002228 ret = 1;
2229 msg_cdbg("FAILED.\n");
2230 goto out;
2231 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002232 } else {
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002233 msg_cdbg("Reading old contents from flash chip... ");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002234 ret = read_dest_content(flash, verify_it,
2235 oldcontents, size);
2236 if (ret) {
2237 msg_cdbg("FAILED.\n");
2238 goto out;
David Hendricks52ddff02013-07-23 15:05:14 -07002239 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002240 }
Vadim Bendebury2f346a32018-05-21 10:24:18 -07002241 msg_cdbg("done.\n");
2242 } else if (!erase_it) {
2243 msg_pinfo("No diff performed, considering the chip erased.\n");
2244 memset(oldcontents, flash_erase_value(flash), size);
hailfinger4c47e9d2010-10-19 22:06:20 +00002245 }
David Hendricksac1d25c2016-08-09 17:00:58 -07002246
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002247
David Hendricksdf29a832013-06-28 14:33:51 -07002248 /*
2249 * Note: This must be done after reading the file specified for the
2250 * -w/-v argument, if any, so that files specified using -i end up
2251 * in the "newcontents" buffer before being written.
2252 * See http://crbug.com/263495.
2253 */
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002254 if (handle_romentries(flash, oldcontents, newcontents, erase_it)) {
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002255 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002256 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002257 goto out;
2258 }
uwef6641642007-05-09 10:17:44 +00002259
David Hendricksa7e114b2016-02-26 18:49:15 -08002260 if (erase_it) {
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002261 erase_chip(flash, oldcontents, newcontents, size);
2262 goto verify;
David Hendricksa7e114b2016-02-26 18:49:15 -08002263 }
2264
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002265 descriptor = prepare_action_descriptor(flash, oldcontents,
2266 newcontents, do_diff);
stuge8ce3a3c2008-04-28 14:47:30 +00002267 if (write_it) {
David Hendricksb64b39a2016-10-11 13:48:06 -07002268 // parse the new fmap and disable soft WP if necessary
David Hendricksac1d25c2016-08-09 17:00:58 -07002269 if ((ret = cros_ec_prepare(newcontents, size))) {
David Hendricksb907de32014-08-11 16:47:09 -07002270 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002271 goto out;
2272 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002273
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002274 if (erase_and_write_flash(flash, descriptor)) {
hailfingerb437e282010-11-04 01:04:27 +00002275 msg_cerr("Uh oh. Erase/write failed. Checking if "
2276 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002277 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002278 if (!memcmp(oldcontents, newcontents, size)) {
2279 msg_cinfo("Good. It seems nothing was "
2280 "changed.\n");
2281 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002282 ret = 1;
2283 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002284 }
2285 }
hailfingerd217d122010-10-08 18:52:29 +00002286 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002287 ret = 1;
2288 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002289 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002290
David Hendricksac1d25c2016-08-09 17:00:58 -07002291 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002292 if (ret < 0) {
2293 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002294 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002295 emergency_help_message();
2296 ret = 1;
2297 goto out;
2298 } else if (ret > 0) {
2299 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002300 msg_pdbg("CROS_EC needs 2nd pass.\n");
Daisuke Nojiri6d2cb212018-09-07 19:02:02 -07002301 ret = read_dest_content(flash, verify_it,
2302 oldcontents, size);
2303 if (ret) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002304 emergency_help_message();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002305 goto out;
2306 }
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002307
2308 /* Get a new descriptor. */
2309 free(descriptor);
2310 descriptor = prepare_action_descriptor(flash,
2311 oldcontents,
2312 newcontents,
2313 do_diff);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002314 // write 2nd pass
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002315 if (erase_and_write_flash(flash, descriptor)) {
David Hendricksb907de32014-08-11 16:47:09 -07002316 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002317 emergency_help_message();
2318 ret = 1;
2319 goto out;
2320 }
2321 ret = 0;
2322 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002323
David Hendricksac1d25c2016-08-09 17:00:58 -07002324 if (cros_ec_finish() < 0) {
David Hendricksb907de32014-08-11 16:47:09 -07002325 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002326 emergency_help_message();
2327 ret = 1;
2328 goto out;
2329 }
stuge8ce3a3c2008-04-28 14:47:30 +00002330 }
ollie6a600992005-11-26 21:55:36 +00002331
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002332 verify:
hailfinger0459e1c2009-08-19 13:55:34 +00002333 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002334 if ((write_it || erase_it) && !content_has_changed) {
2335 msg_gdbg("Nothing was erased or written, skipping "
2336 "verification\n");
2337 } else {
2338 /* Work around chips which need some time to calm down. */
2339 if (write_it && verify_it != VERIFY_PARTIAL)
2340 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002341
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002342 ret = verify_flash(flash, descriptor, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002343
David Hendricks9ba79fb2015-04-03 12:06:16 -07002344 /* If we tried to write, and verification now fails, we
2345 * might have an emergency situation.
2346 */
2347 if (ret && write_it)
2348 emergency_help_message();
2349 }
hailfinger0459e1c2009-08-19 13:55:34 +00002350 }
ollie6a600992005-11-26 21:55:36 +00002351
hailfinger90fcf9b2010-11-05 14:51:59 +00002352out:
Vadim Bendebury2b4dcef2018-05-21 10:47:18 -07002353 if (descriptor)
2354 free(descriptor);
2355
hailfinger90fcf9b2010-11-05 14:51:59 +00002356 free(oldcontents);
2357 free(newcontents);
2358out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002359 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002360 /*
Edward O'Callaghan1a3fd132019-06-04 14:18:55 +10002361 * programmer_shutdown() call is moved to cli_classic() in chromium os
David Hendricks668f29d2011-01-27 18:51:45 -08002362 * tree. This is because some operations, such as write protection,
2363 * requires programmer_shutdown() but does not call doit().
2364 */
2365// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002366 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002367}