blob: 3be3d3214e2d157ae9cab714bdf27415d779eef7 [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Nico Huber7af0e792016-04-29 16:40:15 +02008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000010 *
Uwe Hermannd1107642007-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.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000015 *
Uwe Hermannd1107642007-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.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000020 *
Uwe Hermannd1107642007-08-29 17:52:32 +000021 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000024 */
25
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000026#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000027#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000028#ifndef __LIBPAYLOAD__
29#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000030#include <sys/stat.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000031#endif
Ronald G. Minnichceec4202003-07-25 04:37:41 +000032#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000033#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000034#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000035#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000036#include <ctype.h>
Ollie Lho184a4042005-11-26 21:55:36 +000037#include <getopt.h>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000038#if HAVE_UTSNAME == 1
39#include <sys/utsname.h>
40#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000041#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000042#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000043#include "programmer.h"
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +000044#include "hwaccess.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000045
Mathias Krausea60faab2011-01-17 07:50:42 +000046const char flashrom_version[] = FLASHROM_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000047const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000048
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +000049static enum programmer programmer = PROGRAMMER_INVALID;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000050static const char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000051
Uwe Hermann48ec1b12010-08-08 17:01:18 +000052/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000053 * Programmers supporting multiple buses can have differing size limits on
54 * each bus. Store the limits for each bus in a common struct.
55 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000056struct decode_sizes max_rom_decode;
57
58/* If nonzero, used as the start address of bottom-aligned flash. */
59unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000060
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000061/* Is writing allowed with this programmer? */
62int programmer_may_write;
63
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000064const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000065#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000066 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000067 .name = "internal",
Stefan Tauneraf358d62012-12-27 18:40:26 +000068 .type = OTHER,
69 .devs.note = NULL,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000070 .init = internal_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000071 .map_flash_region = physmap,
72 .unmap_flash_region = physunmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000073 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000074 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000075#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000076
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000077#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000078 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000079 .name = "dummy",
Stefan Tauneraf358d62012-12-27 18:40:26 +000080 .type = OTHER,
81 /* FIXME */
82 .devs.note = "Dummy device, does nothing and logs all accesses\n",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000083 .init = dummy_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000084 .map_flash_region = dummy_map,
85 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000086 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000087 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000088#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000089
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000090#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +000091 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000092 .name = "nic3com",
Stefan Tauneraf358d62012-12-27 18:40:26 +000093 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +000094 .devs.dev = nics_3com,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000095 .init = nic3com_init,
Uwe Hermannc6915932009-05-17 23:12:17 +000096 .map_flash_region = fallback_map,
97 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000098 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000099 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000100#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000101
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000102#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000103 {
Carl-Daniel Hailfinger40446ee2011-03-07 01:08:09 +0000104 /* This programmer works for Realtek RTL8139 and SMC 1211. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000105 .name = "nicrealtek",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000106 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000107 .devs.dev = nics_realtek,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000108 .init = nicrealtek_init,
109 .map_flash_region = fallback_map,
110 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000111 .delay = internal_delay,
Joerg Fischer5665ef32010-05-21 21:54:07 +0000112 },
Joerg Fischer5665ef32010-05-21 21:54:07 +0000113#endif
114
Andrew Morganc29c2e72010-06-07 22:37:54 +0000115#if CONFIG_NICNATSEMI == 1
116 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000117 .name = "nicnatsemi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000118 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000119 .devs.dev = nics_natsemi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000120 .init = nicnatsemi_init,
121 .map_flash_region = fallback_map,
122 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000123 .delay = internal_delay,
Andrew Morganc29c2e72010-06-07 22:37:54 +0000124 },
125#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000126
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000127#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000128 {
129 .name = "gfxnvidia",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000130 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000131 .devs.dev = gfx_nvidia,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000132 .init = gfxnvidia_init,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000133 .map_flash_region = fallback_map,
134 .unmap_flash_region = fallback_unmap,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000135 .delay = internal_delay,
136 },
137#endif
138
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000139#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000140 {
TURBO Jb0912c02009-09-02 23:00:46 +0000141 .name = "drkaiser",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000142 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000143 .devs.dev = drkaiser_pcidev,
TURBO Jb0912c02009-09-02 23:00:46 +0000144 .init = drkaiser_init,
TURBO Jb0912c02009-09-02 23:00:46 +0000145 .map_flash_region = fallback_map,
146 .unmap_flash_region = fallback_unmap,
TURBO Jb0912c02009-09-02 23:00:46 +0000147 .delay = internal_delay,
148 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000149#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000150
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000151#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000152 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000153 .name = "satasii",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000154 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000155 .devs.dev = satas_sii,
Rudolf Marek68720c72009-05-17 19:39:27 +0000156 .init = satasii_init,
Uwe Hermannc6915932009-05-17 23:12:17 +0000157 .map_flash_region = fallback_map,
158 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000159 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000160 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000161#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000162
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000163#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000164 {
165 .name = "atahpt",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000166 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000167 .devs.dev = ata_hpt,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000168 .init = atahpt_init,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000169 .map_flash_region = fallback_map,
170 .unmap_flash_region = fallback_unmap,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000171 .delay = internal_delay,
172 },
173#endif
174
Jonathan Kollasch7f0f3fa2014-06-01 10:26:23 +0000175#if CONFIG_ATAVIA == 1
176 {
177 .name = "atavia",
178 .type = PCI,
179 .devs.dev = ata_via,
180 .init = atavia_init,
181 .map_flash_region = atavia_map,
182 .unmap_flash_region = fallback_unmap,
183 .delay = internal_delay,
184 },
185#endif
186
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +0000187#if CONFIG_ATAPROMISE == 1
188 {
189 .name = "atapromise",
190 .type = PCI,
191 .devs.dev = ata_promise,
192 .init = atapromise_init,
193 .map_flash_region = atapromise_map,
194 .unmap_flash_region = fallback_unmap,
195 .delay = internal_delay,
196 },
197#endif
198
Kyösti Mälkki72d42f82014-06-01 23:48:31 +0000199#if CONFIG_IT8212 == 1
200 {
201 .name = "it8212",
202 .type = PCI,
203 .devs.dev = devs_it8212,
204 .init = it8212_init,
205 .map_flash_region = fallback_map,
206 .unmap_flash_region = fallback_unmap,
207 .delay = internal_delay,
208 },
209#endif
210
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000211#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000212 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000213 .name = "ft2232_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000214 .type = USB,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000215 .devs.dev = devs_ft2232spi,
Paul Fox05dfbe62009-06-16 21:08:06 +0000216 .init = ft2232_spi_init,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000217 .map_flash_region = fallback_map,
218 .unmap_flash_region = fallback_unmap,
Paul Fox05dfbe62009-06-16 21:08:06 +0000219 .delay = internal_delay,
220 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000221#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000222
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000223#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000224 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000225 .name = "serprog",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000226 .type = OTHER,
227 /* FIXME */
228 .devs.note = "All programmer devices speaking the serprog protocol\n",
Urja Rannikko22915352009-06-23 11:33:43 +0000229 .init = serprog_init,
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000230 .map_flash_region = serprog_map,
Urja Rannikko22915352009-06-23 11:33:43 +0000231 .unmap_flash_region = fallback_unmap,
Urja Rannikko22915352009-06-23 11:33:43 +0000232 .delay = serprog_delay,
233 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000234#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000235
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000236#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000237 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000238 .name = "buspirate_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000239 .type = OTHER,
240 /* FIXME */
241 .devs.note = "Dangerous Prototypes Bus Pirate\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000242 .init = buspirate_spi_init,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000243 .map_flash_region = fallback_map,
244 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000245 .delay = internal_delay,
246 },
247#endif
248
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000249#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000250 {
251 .name = "dediprog",
Stefan Taunerfdec7472016-02-22 08:59:27 +0000252 .type = USB,
253 .devs.dev = devs_dediprog,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000254 .init = dediprog_init,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000255 .map_flash_region = fallback_map,
256 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000257 .delay = internal_delay,
258 },
259#endif
260
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000261#if CONFIG_RAYER_SPI == 1
262 {
263 .name = "rayer_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000264 .type = OTHER,
265 /* FIXME */
266 .devs.note = "RayeR parallel port programmer\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000267 .init = rayer_spi_init,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000268 .map_flash_region = fallback_map,
269 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000270 .delay = internal_delay,
271 },
272#endif
273
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000274#if CONFIG_PONY_SPI == 1
275 {
276 .name = "pony_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000277 .type = OTHER,
278 /* FIXME */
279 .devs.note = "Programmers compatible with SI-Prog, serbang or AJAWe\n",
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000280 .init = pony_spi_init,
281 .map_flash_region = fallback_map,
282 .unmap_flash_region = fallback_unmap,
283 .delay = internal_delay,
Stefan Tauneraf358d62012-12-27 18:40:26 +0000284 },
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000285#endif
286
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000287#if CONFIG_NICINTEL == 1
288 {
289 .name = "nicintel",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000290 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000291 .devs.dev = nics_intel,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000292 .init = nicintel_init,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000293 .map_flash_region = fallback_map,
294 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000295 .delay = internal_delay,
296 },
297#endif
298
Idwer Vollering004f4b72010-09-03 18:21:21 +0000299#if CONFIG_NICINTEL_SPI == 1
300 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000301 .name = "nicintel_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000302 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000303 .devs.dev = nics_intel_spi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000304 .init = nicintel_spi_init,
305 .map_flash_region = fallback_map,
306 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000307 .delay = internal_delay,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000308 },
309#endif
310
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000311#if CONFIG_NICINTEL_EEPROM == 1
312 {
313 .name = "nicintel_eeprom",
314 .type = PCI,
315 .devs.dev = nics_intel_ee,
316 .init = nicintel_ee_init,
317 .map_flash_region = fallback_map,
318 .unmap_flash_region = fallback_unmap,
319 .delay = internal_delay,
320 },
321#endif
322
Mark Marshall90021f22010-12-03 14:48:11 +0000323#if CONFIG_OGP_SPI == 1
324 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000325 .name = "ogp_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000326 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000327 .devs.dev = ogp_spi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000328 .init = ogp_spi_init,
329 .map_flash_region = fallback_map,
330 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000331 .delay = internal_delay,
Mark Marshall90021f22010-12-03 14:48:11 +0000332 },
333#endif
334
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000335#if CONFIG_SATAMV == 1
336 {
337 .name = "satamv",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000338 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000339 .devs.dev = satas_mv,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000340 .init = satamv_init,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000341 .map_flash_region = fallback_map,
342 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000343 .delay = internal_delay,
344 },
345#endif
346
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000347#if CONFIG_LINUX_SPI == 1
348 {
349 .name = "linux_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000350 .type = OTHER,
351 .devs.note = "Device files /dev/spidev*.*\n",
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000352 .init = linux_spi_init,
353 .map_flash_region = fallback_map,
354 .unmap_flash_region = fallback_unmap,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000355 .delay = internal_delay,
356 },
357#endif
358
James Lairdc60de0e2013-03-27 13:00:23 +0000359#if CONFIG_USBBLASTER_SPI == 1
360 {
361 .name = "usbblaster_spi",
362 .type = USB,
363 .devs.dev = devs_usbblasterspi,
364 .init = usbblaster_spi_init,
365 .map_flash_region = fallback_map,
366 .unmap_flash_region = fallback_unmap,
367 .delay = internal_delay,
368 },
369#endif
370
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000371#if CONFIG_MSTARDDC_SPI == 1
372 {
373 .name = "mstarddc_spi",
374 .type = OTHER,
375 .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n",
376 .init = mstarddc_spi_init,
377 .map_flash_region = fallback_map,
378 .unmap_flash_region = fallback_unmap,
379 .delay = internal_delay,
380 },
381#endif
382
Justin Chevrier66e554b2015-02-08 21:58:10 +0000383#if CONFIG_PICKIT2_SPI == 1
384 {
385 .name = "pickit2_spi",
Stefan Taunerf31fe842016-02-22 08:59:15 +0000386 .type = USB,
387 .devs.dev = devs_pickit2_spi,
Justin Chevrier66e554b2015-02-08 21:58:10 +0000388 .init = pickit2_spi_init,
389 .map_flash_region = fallback_map,
390 .unmap_flash_region = fallback_unmap,
391 .delay = internal_delay,
392 },
393#endif
394
Urja Rannikko0870b022016-01-31 22:10:29 +0000395#if CONFIG_CH341A_SPI == 1
396 {
397 .name = "ch341a_spi",
398 .type = USB,
399 .devs.dev = devs_ch341a_spi,
400 .init = ch341a_spi_init,
401 .map_flash_region = fallback_map,
402 .unmap_flash_region = fallback_unmap,
403 .delay = ch341a_spi_delay,
404 },
405#endif
406
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000407 {0}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000408};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000409
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +0000410#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000411static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +0000412/** @private */
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000413struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +0000414 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000415 void *data;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000416} static shutdown_fn[SHUTDOWN_MAXFN];
417/* Initialize to 0 to make sure nobody registers a shutdown function before
418 * programmer init.
419 */
420static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000421
Stefan Taunerc4f44df2013-08-12 22:58:43 +0000422/* Did we change something or was every erase/write skipped (if any)? */
423static bool all_skipped = true;
424
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000425static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +0000426
Stefan Tauner2a1ed772014-08-31 00:09:21 +0000427int shutdown_free(void *data)
428{
429 free(data);
430 return 0;
431}
432
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000433/* Register a function to be executed on programmer shutdown.
434 * The advantage over atexit() is that you can supply a void pointer which will
435 * be used as parameter to the registered function upon programmer shutdown.
436 * This pointer can point to arbitrary data used by said function, e.g. undo
437 * information for GPIO settings etc. If unneeded, set data=NULL.
438 * Please note that the first (void *data) belongs to the function signature of
439 * the function passed as first parameter.
440 */
David Hendricks8bb20212011-06-14 01:35:36 +0000441int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000442{
443 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000444 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000445 SHUTDOWN_MAXFN);
446 return 1;
447 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000448 if (!may_register_shutdown) {
449 msg_perr("Tried to register a shutdown function before "
450 "programmer init.\n");
451 return 1;
452 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000453 shutdown_fn[shutdown_fn_count].func = function;
454 shutdown_fn[shutdown_fn_count].data = data;
455 shutdown_fn_count++;
456
457 return 0;
458}
459
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000460int programmer_init(enum programmer prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000461{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000462 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000463
464 if (prog >= PROGRAMMER_INVALID) {
465 msg_perr("Invalid programmer specified!\n");
466 return -1;
467 }
468 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000469 /* Initialize all programmer specific data. */
470 /* Default to unlimited decode sizes. */
471 max_rom_decode = (const struct decode_sizes) {
472 .parallel = 0xffffffff,
473 .lpc = 0xffffffff,
474 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000475 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000476 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000477 /* Default to top aligned flash at 4 GB. */
478 flashbase = 0;
479 /* Registering shutdown functions is now allowed. */
480 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000481 /* Default to allowing writes. Broken programmers set this to 0. */
482 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000483
484 programmer_param = param;
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000485 msg_pdbg("Initializing %s programmer\n", programmer_table[programmer].name);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000486 ret = programmer_table[programmer].init();
487 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000488 if (ret != 0) {
489 /* It is quite possible that any unhandled programmer parameter would have been valid,
490 * but an error in actual programmer init happened before the parameter was evaluated.
491 */
492 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
493 programmer_param);
494 } else {
495 /* Actual programmer init was successful, but the user specified an invalid or unusable
496 * (for the current programmer configuration) parameter.
497 */
498 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
499 msg_perr("Aborting.\n");
500 ret = ERROR_FATAL;
501 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000502 }
503 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000504}
505
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000506/** Calls registered shutdown functions and resets internal programmer-related variables.
507 * Calling it is safe even without previous initialization, but further interactions with programmer support
508 * require a call to programmer_init() (afterwards).
509 *
510 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000511int programmer_shutdown(void)
512{
David Hendricks8bb20212011-06-14 01:35:36 +0000513 int ret = 0;
514
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000515 /* Registering shutdown functions is no longer allowed. */
516 may_register_shutdown = 0;
517 while (shutdown_fn_count > 0) {
518 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000519 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000520 }
Stefan Taunere34e3e82013-01-01 00:06:51 +0000521
Stefan Taunerb8911d62012-12-26 07:55:00 +0000522 programmer_param = NULL;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000523 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000524
David Hendricks8bb20212011-06-14 01:35:36 +0000525 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000526}
527
Stefan Tauner305e0b92013-07-17 23:46:44 +0000528void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000529{
Stefan Tauner26e7a152013-09-13 17:21:05 +0000530 void *ret = programmer_table[programmer].map_flash_region(descr, phys_addr, len);
531 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
532 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
533 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000534}
535
536void programmer_unmap_flash_region(void *virt_addr, size_t len)
537{
538 programmer_table[programmer].unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000539 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000540}
541
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000542void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000543{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000544 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000545}
546
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000547void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000548{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000549 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000550}
551
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000552void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000553{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000554 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000555}
556
Mark Marshallf20b7be2014-05-09 21:16:21 +0000557void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000558{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000559 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000560}
561
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000562uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000563{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000564 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000565}
566
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000567uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000568{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000569 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000570}
571
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000572uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000573{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000574 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000575}
576
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000577void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
578 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000579{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000580 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000581}
582
Stefan Taunerf80419c2014-05-02 15:41:42 +0000583void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000584{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000585 if (usecs > 0)
586 programmer_table[programmer].delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000587}
588
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000589int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
590 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000591{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000592 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000593
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000594 return 0;
595}
596
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000597/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000598 * It will look for needle with a subsequent '=' in haystack, return a copy of
599 * needle and remove everything from the first occurrence of needle to the next
600 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000601 */
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000602char *extract_param(const char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000603{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000604 char *param_pos, *opt_pos, *rest;
605 char *opt = NULL;
606 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000607 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000608
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000609 needlelen = strlen(needle);
610 if (!needlelen) {
611 msg_gerr("%s: empty needle! Please report a bug at "
612 "flashrom@flashrom.org\n", __func__);
613 return NULL;
614 }
615 /* No programmer parameters given. */
616 if (*haystack == NULL)
617 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000618 param_pos = strstr(*haystack, needle);
619 do {
620 if (!param_pos)
621 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000622 /* Needle followed by '='? */
623 if (param_pos[needlelen] == '=') {
624
625 /* Beginning of the string? */
626 if (param_pos == *haystack)
627 break;
628 /* After a delimiter? */
629 if (strchr(delim, *(param_pos - 1)))
630 break;
631 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000632 /* Continue searching. */
633 param_pos++;
634 param_pos = strstr(param_pos, needle);
635 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000636
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000637 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000638 /* Get the string after needle and '='. */
639 opt_pos = param_pos + needlelen + 1;
640 optlen = strcspn(opt_pos, delim);
641 /* Return an empty string if the parameter was empty. */
642 opt = malloc(optlen + 1);
643 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000644 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000645 exit(1);
646 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000647 strncpy(opt, opt_pos, optlen);
648 opt[optlen] = '\0';
649 rest = opt_pos + optlen;
650 /* Skip all delimiters after the current parameter. */
651 rest += strspn(rest, delim);
652 memmove(param_pos, rest, strlen(rest) + 1);
653 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000654 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000655
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000656 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000657}
658
Stefan Tauner66652442011-06-26 17:38:17 +0000659char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000660{
661 return extract_param(&programmer_param, param_name, ",");
662}
663
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000664/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000665static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-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
Mark Marshallf20b7be2014-05-09 21:16:21 +0000676static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000677{
678 int ret = 0, failcount = 0;
679 unsigned int i;
680 for (i = 0; i < len; i++) {
681 if (wantbuf[i] != havebuf[i]) {
682 /* Only print the first failure. */
683 if (!failcount++)
684 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
685 start + i, wantbuf[i], havebuf[i]);
686 }
687 }
688 if (failcount) {
689 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
690 start, start + len - 1, failcount);
691 ret = -1;
692 }
693 return ret;
694}
695
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000696/* start is an offset to the base address of the flash chip */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000697int check_erased_range(struct flashctx *flash, unsigned int start,
698 unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000699{
700 int ret;
701 uint8_t *cmpbuf = malloc(len);
702
703 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000704 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000705 exit(1);
706 }
707 memset(cmpbuf, 0xff, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000708 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000709 free(cmpbuf);
710 return ret;
711}
712
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000713/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000714 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000715 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000716 * @start offset to the base address of the flash chip
717 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000718 * @return 0 for success, -1 for failure
719 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000720int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000721{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000722 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000723 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000724
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000725 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000726 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000727 return -1;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000728 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000729
730 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000731 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000732 msg_gerr("Could not allocate memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000733 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000734 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000735 int ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000736
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000737 if (start + len > flash->chip->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000738 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000739 " total_size 0x%x\n", __func__, start, len,
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000740 flash->chip->total_size * 1024);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000741 ret = -1;
742 goto out_free;
743 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000744
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000745 ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000746 if (ret) {
747 msg_gerr("Verification impossible because read failed "
748 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000749 ret = -1;
750 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000751 }
752
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000753 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000754out_free:
755 free(readbuf);
756 return ret;
757}
758
Stefan Tauner02437452013-04-01 19:34:53 +0000759/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000760static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len, unsigned int gran)
Stefan Tauner02437452013-04-01 19:34:53 +0000761{
762 unsigned int i, j, limit;
763 for (j = 0; j < len / gran; j++) {
764 limit = min (gran, len - j * gran);
765 /* Are 'have' and 'want' identical? */
766 if (!memcmp(have + j * gran, want + j * gran, limit))
767 continue;
768 /* have needs to be in erased state. */
769 for (i = 0; i < limit; i++)
770 if (have[j * gran + i] != 0xff)
771 return 1;
772 }
773 return 0;
774}
775
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000776/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000777 * Check if the buffer @have can be programmed to the content of @want without
778 * erasing. This is only possible if all chunks of size @gran are either kept
779 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000780 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000781 * Warning: This function assumes that @have and @want point to naturally
782 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000783 *
784 * @have buffer with current content
785 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000786 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000787 * @gran write granularity (enum, not count)
788 * @return 0 if no erase is needed, 1 otherwise
789 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000790int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len, enum write_granularity gran)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000791{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000792 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000793 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000794
795 switch (gran) {
796 case write_gran_1bit:
797 for (i = 0; i < len; i++)
798 if ((have[i] & want[i]) != want[i]) {
799 result = 1;
800 break;
801 }
802 break;
803 case write_gran_1byte:
804 for (i = 0; i < len; i++)
805 if ((have[i] != want[i]) && (have[i] != 0xff)) {
806 result = 1;
807 break;
808 }
809 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000810 case write_gran_128bytes:
811 result = need_erase_gran_bytes(have, want, len, 128);
812 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000813 case write_gran_256bytes:
Stefan Tauner02437452013-04-01 19:34:53 +0000814 result = need_erase_gran_bytes(have, want, len, 256);
815 break;
816 case write_gran_264bytes:
817 result = need_erase_gran_bytes(have, want, len, 264);
818 break;
819 case write_gran_512bytes:
820 result = need_erase_gran_bytes(have, want, len, 512);
821 break;
822 case write_gran_528bytes:
823 result = need_erase_gran_bytes(have, want, len, 528);
824 break;
825 case write_gran_1024bytes:
826 result = need_erase_gran_bytes(have, want, len, 1024);
827 break;
828 case write_gran_1056bytes:
829 result = need_erase_gran_bytes(have, want, len, 1056);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000830 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000831 case write_gran_1byte_implicit_erase:
832 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
833 result = 0;
834 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000835 default:
836 msg_cerr("%s: Unsupported granularity! Please report a bug at "
837 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000838 }
839 return result;
840}
841
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000842/**
843 * Check if the buffer @have needs to be programmed to get the content of @want.
844 * If yes, return 1 and fill in first_start with the start address of the
845 * write operation and first_len with the length of the first to-be-written
846 * chunk. If not, return 0 and leave first_start and first_len undefined.
847 *
848 * Warning: This function assumes that @have and @want point to naturally
849 * aligned regions.
850 *
851 * @have buffer with current content
852 * @want buffer with desired content
853 * @len length of the checked area
854 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000855 * @first_start offset of the first byte which needs to be written (passed in
856 * value is increased by the offset of the first needed write
857 * relative to have/want or unchanged if no write is needed)
858 * @return length of the first contiguous area which needs to be written
859 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000860 *
861 * FIXME: This function needs a parameter which tells it about coalescing
862 * in relation to the max write length of the programmer and the max write
863 * length of the chip.
864 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000865static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000866 unsigned int *first_start,
867 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000868{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000869 int need_write = 0;
870 unsigned int rel_start = 0, first_len = 0;
871 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000872
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000873 switch (gran) {
874 case write_gran_1bit:
875 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000876 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000877 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000878 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000879 case write_gran_128bytes:
880 stride = 128;
881 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000882 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000883 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000884 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000885 case write_gran_264bytes:
886 stride = 264;
887 break;
888 case write_gran_512bytes:
889 stride = 512;
890 break;
891 case write_gran_528bytes:
892 stride = 528;
893 break;
894 case write_gran_1024bytes:
895 stride = 1024;
896 break;
897 case write_gran_1056bytes:
898 stride = 1056;
899 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000900 default:
901 msg_cerr("%s: Unsupported granularity! Please report a bug at "
902 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000903 /* Claim that no write was needed. A write with unknown
904 * granularity is too dangerous to try.
905 */
906 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000907 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000908 for (i = 0; i < len / stride; i++) {
909 limit = min(stride, len - i * stride);
910 /* Are 'have' and 'want' identical? */
911 if (memcmp(have + i * stride, want + i * stride, limit)) {
912 if (!need_write) {
913 /* First location where have and want differ. */
914 need_write = 1;
915 rel_start = i * stride;
916 }
917 } else {
918 if (need_write) {
919 /* First location where have and want
920 * do not differ anymore.
921 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000922 break;
923 }
924 }
925 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000926 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000927 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000928 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000929 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000930}
931
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000932/* This function generates various test patterns useful for testing controller
933 * and chip communication as well as chip behaviour.
934 *
935 * If a byte can be written multiple times, each time keeping 0-bits at 0
936 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
937 * is essentially an AND operation. That's also the reason why this function
938 * provides the result of AND between various patterns.
939 *
940 * Below is a list of patterns (and their block length).
941 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
942 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
943 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
944 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
945 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
946 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
947 * Pattern 6 is 00 (1 Byte)
948 * Pattern 7 is ff (1 Byte)
949 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
950 * byte block.
951 *
952 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
953 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
954 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
955 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
956 * Pattern 12 is 00 (1 Byte)
957 * Pattern 13 is ff (1 Byte)
958 * Patterns 8-13 have no block number.
959 *
960 * Patterns 0-3 are created to detect and efficiently diagnose communication
961 * slips like missed bits or bytes and their repetitive nature gives good visual
962 * cues to the person inspecting the results. In addition, the following holds:
963 * AND Pattern 0/1 == Pattern 4
964 * AND Pattern 2/3 == Pattern 5
965 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
966 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
967 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
968 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
969 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
970 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
971 * Besides that, they provide for bit testing of the last two bytes of every
972 * 256 byte block which contains the block number for patterns 0-6.
973 * Patterns 10-11 are special purpose for detecting subblock aliasing with
974 * block sizes >256 bytes (some Dataflash chips etc.)
975 * AND Pattern 8/9 == Pattern 12
976 * AND Pattern 10/11 == Pattern 12
977 * Pattern 13 is the completely erased state.
978 * None of the patterns can detect aliasing at boundaries which are a multiple
979 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
980 */
981int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
982{
983 int i;
984
985 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000986 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000987 return 1;
988 }
989
990 switch (variant) {
991 case 0:
992 for (i = 0; i < size; i++)
993 buf[i] = (i & 0xf) << 4 | 0x5;
994 break;
995 case 1:
996 for (i = 0; i < size; i++)
997 buf[i] = (i & 0xf) << 4 | 0xa;
998 break;
999 case 2:
1000 for (i = 0; i < size; i++)
1001 buf[i] = 0x50 | (i & 0xf);
1002 break;
1003 case 3:
1004 for (i = 0; i < size; i++)
1005 buf[i] = 0xa0 | (i & 0xf);
1006 break;
1007 case 4:
1008 for (i = 0; i < size; i++)
1009 buf[i] = (i & 0xf) << 4;
1010 break;
1011 case 5:
1012 for (i = 0; i < size; i++)
1013 buf[i] = i & 0xf;
1014 break;
1015 case 6:
1016 memset(buf, 0x00, size);
1017 break;
1018 case 7:
1019 memset(buf, 0xff, size);
1020 break;
1021 case 8:
1022 for (i = 0; i < size; i++)
1023 buf[i] = i & 0xff;
1024 break;
1025 case 9:
1026 for (i = 0; i < size; i++)
1027 buf[i] = ~(i & 0xff);
1028 break;
1029 case 10:
1030 for (i = 0; i < size % 2; i++) {
1031 buf[i * 2] = (i >> 8) & 0xff;
1032 buf[i * 2 + 1] = i & 0xff;
1033 }
1034 if (size & 0x1)
1035 buf[i * 2] = (i >> 8) & 0xff;
1036 break;
1037 case 11:
1038 for (i = 0; i < size % 2; i++) {
1039 buf[i * 2] = ~((i >> 8) & 0xff);
1040 buf[i * 2 + 1] = ~(i & 0xff);
1041 }
1042 if (size & 0x1)
1043 buf[i * 2] = ~((i >> 8) & 0xff);
1044 break;
1045 case 12:
1046 memset(buf, 0x00, size);
1047 break;
1048 case 13:
1049 memset(buf, 0xff, size);
1050 break;
1051 }
1052
1053 if ((variant >= 0) && (variant <= 7)) {
1054 /* Write block number in the last two bytes of each 256-byte
1055 * block, big endian for easier reading of the hexdump.
1056 * Note that this wraps around for chips larger than 2^24 bytes
1057 * (16 MB).
1058 */
1059 for (i = 0; i < size / 256; i++) {
1060 buf[i * 256 + 254] = (i >> 8) & 0xff;
1061 buf[i * 256 + 255] = i & 0xff;
1062 }
1063 }
1064
1065 return 0;
1066}
1067
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001068/* Returns the number of busses commonly supported by the current programmer and flash chip where the latter
1069 * can not be completely accessed due to size/address limits of the programmer. */
1070unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001071{
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001072 unsigned int limitexceeded = 0;
1073 uint32_t size = flash->chip->total_size * 1024;
1074 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001075
1076 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001077 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001078 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001079 "size %u kB of chipset/board/programmer "
1080 "for %s interface, "
1081 "probe/read/erase/write may fail. ", size / 1024,
1082 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001083 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001084 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001085 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001086 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001087 "size %u kB of chipset/board/programmer "
1088 "for %s interface, "
1089 "probe/read/erase/write may fail. ", size / 1024,
1090 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001091 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001092 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001093 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001094 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001095 "size %u kB of chipset/board/programmer "
1096 "for %s interface, "
1097 "probe/read/erase/write may fail. ", size / 1024,
1098 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001099 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001100 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001101 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001102 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001103 "size %u kB of chipset/board/programmer "
1104 "for %s interface, "
1105 "probe/read/erase/write may fail. ", size / 1024,
1106 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001107 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001108 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001109}
1110
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001111void unmap_flash(struct flashctx *flash)
1112{
1113 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
1114 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
1115 flash->physical_registers = 0;
1116 flash->virtual_registers = (chipaddr)ERROR_PTR;
1117 }
1118
1119 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
1120 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
1121 flash->physical_memory = 0;
1122 flash->virtual_memory = (chipaddr)ERROR_PTR;
1123 }
1124}
1125
1126int map_flash(struct flashctx *flash)
1127{
1128 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
1129 flash->virtual_memory = (chipaddr)ERROR_PTR;
1130 flash->virtual_registers = (chipaddr)ERROR_PTR;
1131
1132 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
1133 * These are used for various probing-related hacks that would not map successfully anyway and should be
1134 * removed ASAP. */
1135 if (flash->chip->total_size == 0)
1136 return 0;
1137
1138 const chipsize_t size = flash->chip->total_size * 1024;
1139 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
1140 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
1141 if (addr == ERROR_PTR) {
1142 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
1143 flash->chip->name, PRIxPTR_WIDTH, base);
1144 return 1;
1145 }
1146 flash->physical_memory = base;
1147 flash->virtual_memory = (chipaddr)addr;
1148
1149 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
1150 * completely different on some chips and programmers, or not mappable at all.
1151 * Ignore these problems for now and always report success. */
1152 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
1153 base = 0xffffffff - size - 0x400000 + 1;
1154 addr = programmer_map_flash_region("flash chip registers", base, size);
1155 if (addr == ERROR_PTR) {
1156 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
1157 flash->chip->name, PRIxPTR_WIDTH, base);
1158 return 0;
1159 }
1160 flash->physical_registers = base;
1161 flash->virtual_registers = (chipaddr)addr;
1162 }
1163 return 0;
1164}
1165
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001166int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001167{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001168 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001169 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001170 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001171
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001172 for (chip = flashchips + startchip; chip && chip->name; chip++) {
1173 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +00001174 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001175 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001176 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +00001177 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001178 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
1179 if (!chip->probe && !force) {
1180 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001181 continue;
1182 }
Stefan Reinauer70385642007-04-06 11:58:03 +00001183
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001184 /* Start filling in the dynamic data. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001185 flash->chip = calloc(1, sizeof(struct flashchip));
1186 if (!flash->chip) {
1187 msg_gerr("Out of memory!\n");
1188 exit(1);
1189 }
1190 memcpy(flash->chip, chip, sizeof(struct flashchip));
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001191 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001192
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001193 if (map_flash(flash) != 0)
1194 return -1;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001195
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001196 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
1197 * is only called with force=1 after normal probing failed.
1198 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001199 if (force)
1200 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001201
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001202 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +00001203 goto notfound;
1204
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001205 /* If this is the first chip found, accept it.
1206 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001207 * a non-generic match. SFDP and CFI are generic matches.
1208 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001209 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001210 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001211 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001212 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001213 msg_cinfo("===\n"
1214 "SFDP has autodetected a flash chip which is "
1215 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001216 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001217 msg_cinfo("The standard operations read and "
1218 "verify should work, but to support "
1219 "erase, write and all other "
1220 "possible features");
1221 else
1222 msg_cinfo("All standard operations (read, "
1223 "verify, erase and write) should "
1224 "work, but to support all possible "
1225 "features");
1226
Stefan Taunerb4e06bd2012-08-20 00:24:22 +00001227 msg_cinfo(" we need to add them manually.\n"
1228 "You can help us by mailing us the output of the following command to "
1229 "flashrom@flashrom.org:\n"
1230 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
1231 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001232 "===\n");
1233 }
1234
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001235 /* First flash chip detected on this bus. */
1236 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001237 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001238 /* Not the first flash chip detected on this bus, but not a generic match either. */
1239 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
1240 break;
1241 /* Not the first flash chip detected on this bus, and it's just a generic match. Ignore it. */
Peter Stuge483b8f02008-09-03 23:10:05 +00001242notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001243 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001244 free(flash->chip);
1245 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001246 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001247
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001248 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001249 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001250
Nico Huber7af0e792016-04-29 16:40:15 +02001251 /* Fill fallback layout covering the whole chip. */
1252 struct single_layout *const fallback = &flash->fallback_layout;
1253 fallback->base.entries = &fallback->entry;
1254 fallback->base.num_entries = 1;
1255 fallback->entry.start = 0;
1256 fallback->entry.end = flash->chip->total_size * 1024 - 1;
1257 fallback->entry.included = true;
1258 strcpy(fallback->entry.name, "complete flash");
Stefan Reinauer051e2362011-01-19 06:21:54 +00001259
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001260 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001261 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
1262 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +00001263 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001264#if CONFIG_INTERNAL == 1
1265 if (programmer_table[programmer].map_flash_region == physmap)
1266 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
1267 PRIxPTR_WIDTH, flash->physical_memory);
1268 else
1269#endif
1270 msg_cinfo("on %s.\n", programmer_table[programmer].name);
Uwe Hermann9899cad2009-06-28 21:47:57 +00001271
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001272 /* Flash registers may more likely not be mapped if the chip was forced.
1273 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +00001274 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001275 if (flash->chip->printlock)
1276 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +00001277
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001278 /* Get out of the way for later runs. */
1279 unmap_flash(flash);
1280
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001281 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001282 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001283}
1284
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001285int read_buf_from_file(unsigned char *buf, unsigned long size,
1286 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001287{
Nico Huber7562f7d2013-08-30 21:29:45 +00001288#ifdef __LIBPAYLOAD__
1289 msg_gerr("Error: No file I/O support in libpayload\n");
1290 return 1;
1291#else
Stefan Tauner16687702015-12-25 21:59:45 +00001292 int ret = 0;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001293
Stefan Tauner16687702015-12-25 21:59:45 +00001294 FILE *image;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001295 if ((image = fopen(filename, "rb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001296 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001297 return 1;
1298 }
Stefan Tauner16687702015-12-25 21:59:45 +00001299
1300 struct stat image_stat;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001301 if (fstat(fileno(image), &image_stat) != 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001302 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
Stefan Tauner16687702015-12-25 21:59:45 +00001303 ret = 1;
1304 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001305 }
1306 if (image_stat.st_size != size) {
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +00001307 msg_gerr("Error: Image size (%jd B) doesn't match the flash chip's size (%lu B)!\n",
Stefan Taunere038e902013-02-04 04:38:42 +00001308 (intmax_t)image_stat.st_size, size);
Stefan Tauner16687702015-12-25 21:59:45 +00001309 ret = 1;
1310 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001311 }
Stefan Tauner16687702015-12-25 21:59:45 +00001312
1313 unsigned long numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001314 if (numbytes != size) {
1315 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1316 "wanted %ld!\n", numbytes, size);
Stefan Tauner16687702015-12-25 21:59:45 +00001317 ret = 1;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001318 }
Stefan Tauner16687702015-12-25 21:59:45 +00001319out:
1320 (void)fclose(image);
1321 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001322#endif
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001323}
1324
Mark Marshallf20b7be2014-05-09 21:16:21 +00001325int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001326{
Nico Huber7562f7d2013-08-30 21:29:45 +00001327#ifdef __LIBPAYLOAD__
1328 msg_gerr("Error: No file I/O support in libpayload\n");
1329 return 1;
1330#else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001331 FILE *image;
Stefan Tauner16687702015-12-25 21:59:45 +00001332 int ret = 0;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001333
1334 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001335 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001336 return 1;
1337 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001338 if ((image = fopen(filename, "wb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001339 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001340 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001341 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001342
Stefan Tauner16687702015-12-25 21:59:45 +00001343 unsigned long numbytes = fwrite(buf, 1, size, image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001344 if (numbytes != size) {
Stefan Tauner16687702015-12-25 21:59:45 +00001345 msg_gerr("Error: file %s could not be written completely.\n", filename);
1346 ret = 1;
1347 goto out;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001348 }
Stefan Tauner16687702015-12-25 21:59:45 +00001349 if (fflush(image)) {
1350 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
1351 ret = 1;
1352 }
1353 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
1354#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
1355 struct stat image_stat;
1356 if (fstat(fileno(image), &image_stat) != 0) {
1357 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
1358 ret = 1;
1359 goto out;
1360 }
1361 if (S_ISREG(image_stat.st_mode)) {
1362 if (fsync(fileno(image))) {
1363 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
1364 ret = 1;
1365 }
1366 }
1367#endif
1368out:
1369 if (fclose(image)) {
1370 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
1371 ret = 1;
1372 }
1373 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001374#endif
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001375}
1376
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001377int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001378{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001379 unsigned long size = flash->chip->total_size * 1024;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001380 unsigned char *buf = calloc(size, sizeof(char));
1381 int ret = 0;
1382
1383 msg_cinfo("Reading flash... ");
1384 if (!buf) {
1385 msg_gerr("Memory allocation failed!\n");
1386 msg_cinfo("FAILED.\n");
1387 return 1;
1388 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001389 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001390 msg_cerr("No read function available for this flash chip.\n");
1391 ret = 1;
1392 goto out_free;
1393 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001394 if (flash->chip->read(flash, buf, 0, size)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001395 msg_cerr("Read operation failed!\n");
1396 ret = 1;
1397 goto out_free;
1398 }
1399
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001400 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001401out_free:
1402 free(buf);
1403 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1404 return ret;
1405}
1406
Stefan Tauner96658be2014-05-26 22:05:31 +00001407/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001408static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001409{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001410 int i, j, k;
1411 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001412
1413 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1414 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001415 struct block_eraser eraser = chip->block_erasers[k];
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001416
1417 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1418 /* Blocks with zero size are bugs in flashchips.c. */
1419 if (eraser.eraseblocks[i].count &&
1420 !eraser.eraseblocks[i].size) {
1421 msg_gerr("ERROR: Flash chip %s erase function "
1422 "%i region %i has size 0. Please report"
1423 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001424 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001425 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001426 }
1427 /* Blocks with zero count are bugs in flashchips.c. */
1428 if (!eraser.eraseblocks[i].count &&
1429 eraser.eraseblocks[i].size) {
1430 msg_gerr("ERROR: Flash chip %s erase function "
1431 "%i region %i has count 0. Please report"
1432 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001433 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001434 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001435 }
1436 done += eraser.eraseblocks[i].count *
1437 eraser.eraseblocks[i].size;
1438 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001439 /* Empty eraseblock definition with erase function. */
1440 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001441 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001442 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001443 if (!done)
1444 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001445 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001446 msg_gerr("ERROR: Flash chip %s erase function %i "
1447 "region walking resulted in 0x%06x bytes total,"
1448 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001449 " flashrom@flashrom.org\n", chip->name, k,
1450 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001451 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001452 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001453 if (!eraser.block_erase)
1454 continue;
1455 /* Check if there are identical erase functions for different
1456 * layouts. That would imply "magic" erase functions. The
1457 * easiest way to check this is with function pointers.
1458 */
Uwe Hermann43959702010-03-13 17:28:29 +00001459 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001460 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001461 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001462 msg_gerr("ERROR: Flash chip %s erase function "
1463 "%i and %i are identical. Please report"
1464 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001465 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001466 ret = 1;
1467 }
Uwe Hermann43959702010-03-13 17:28:29 +00001468 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001469 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001470 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001471}
1472
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001473static int erase_and_write_block_helper(struct flashctx *flash,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001474 unsigned int start, unsigned int len,
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001475 uint8_t *curcontents,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001476 uint8_t *newcontents,
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001477 int (*erasefn) (struct flashctx *flash,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001478 unsigned int addr,
1479 unsigned int len))
1480{
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001481 unsigned int starthere = 0, lenhere = 0;
1482 int ret = 0, skip = 1, writecount = 0;
Stefan Tauner50d67aa2013-03-03 23:49:48 +00001483 enum write_granularity gran = flash->chip->gran;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001484
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001485 /* curcontents and newcontents are opaque to walk_eraseregions, and
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001486 * need to be adjusted here to keep the impression of proper abstraction
1487 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001488 curcontents += start;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001489 newcontents += start;
1490 msg_cdbg(":");
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001491 if (need_erase(curcontents, newcontents, len, gran)) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001492 msg_cdbg("E");
1493 ret = erasefn(flash, start, len);
1494 if (ret)
1495 return ret;
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +00001496 if (check_erased_range(flash, start, len)) {
1497 msg_cerr("ERASE FAILED!\n");
1498 return -1;
1499 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001500 /* Erase was successful. Adjust curcontents. */
1501 memset(curcontents, 0xff, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001502 skip = 0;
1503 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001504 /* get_next_write() sets starthere to a new value after the call. */
1505 while ((lenhere = get_next_write(curcontents + starthere,
1506 newcontents + starthere,
1507 len - starthere, &starthere, gran))) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001508 if (!writecount++)
1509 msg_cdbg("W");
1510 /* Needs the partial write function signature. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001511 ret = flash->chip->write(flash, newcontents + starthere,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001512 start + starthere, lenhere);
1513 if (ret)
1514 return ret;
1515 starthere += lenhere;
1516 skip = 0;
1517 }
1518 if (skip)
1519 msg_cdbg("S");
Stefan Tauner682122b2013-06-23 22:15:39 +00001520 else
1521 all_skipped = false;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001522 return ret;
1523}
1524
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001525static int walk_eraseregions(struct flashctx *flash, int erasefunction,
1526 int (*do_something) (struct flashctx *flash,
Carl-Daniel Hailfinger4d3e9ca2010-07-13 00:42:00 +00001527 unsigned int addr,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001528 unsigned int len,
1529 uint8_t *param1,
1530 uint8_t *param2,
1531 int (*erasefn) (
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001532 struct flashctx *flash,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001533 unsigned int addr,
1534 unsigned int len)),
1535 void *param1, void *param2)
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001536{
1537 int i, j;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001538 unsigned int start = 0;
1539 unsigned int len;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001540 struct block_eraser eraser = flash->chip->block_erasers[erasefunction];
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001541
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001542 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1543 /* count==0 for all automatically initialized array
1544 * members so the loop below won't be executed for them.
1545 */
1546 len = eraser.eraseblocks[i].size;
1547 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001548 /* Print this for every block except the first one. */
1549 if (i || j)
1550 msg_cdbg(", ");
1551 msg_cdbg("0x%06x-0x%06x", start,
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001552 start + len - 1);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001553 if (do_something(flash, start, len, param1, param2,
1554 eraser.block_erase)) {
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001555 return 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001556 }
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001557 start += len;
1558 }
1559 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001560 msg_cdbg("\n");
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001561 return 0;
1562}
1563
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001564static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001565{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001566 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001567
1568 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1569 if (log)
1570 msg_cdbg("not defined. ");
1571 return 1;
1572 }
1573 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1574 if (log)
1575 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001576 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001577 return 1;
1578 }
1579 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1580 if (log)
1581 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001582 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001583 return 1;
1584 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001585 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001586 return 0;
1587}
1588
Mark Marshallf20b7be2014-05-09 21:16:21 +00001589int erase_and_write_flash(struct flashctx *flash, uint8_t *oldcontents, uint8_t *newcontents)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001590{
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001591 int k, ret = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001592 uint8_t *curcontents;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001593 unsigned long size = flash->chip->total_size * 1024;
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001594 unsigned int usable_erasefunctions = count_usable_erasers(flash);
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001595
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001596 msg_cinfo("Erasing and writing flash chip... ");
Stefan Tauner269de352011-07-12 22:35:21 +00001597 curcontents = malloc(size);
1598 if (!curcontents) {
1599 msg_gerr("Out of memory!\n");
1600 exit(1);
1601 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001602 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1603 memcpy(curcontents, oldcontents, size);
1604
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001605 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001606 if (k != 0)
Stefan Tauner16878542014-07-13 23:53:40 +00001607 msg_cinfo("Looking for another erase function.\n");
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001608 if (!usable_erasefunctions) {
Stefan Tauner16878542014-07-13 23:53:40 +00001609 msg_cinfo("No usable erase functions left.\n");
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001610 break;
1611 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001612 msg_cdbg("Trying erase function %i... ", k);
1613 if (check_block_eraser(flash, k, 1))
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001614 continue;
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001615 usable_erasefunctions--;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001616 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1617 curcontents, newcontents);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001618 /* If everything is OK, don't try another erase function. */
1619 if (!ret)
1620 break;
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001621 /* Write/erase failed, so try to find out what the current chip
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001622 * contents are. If no usable erase functions remain, we can
1623 * skip this: the next iteration will break immediately anyway.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001624 */
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001625 if (!usable_erasefunctions)
1626 continue;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001627 /* Reading the whole chip may take a while, inform the user even
1628 * in non-verbose mode.
1629 */
1630 msg_cinfo("Reading current flash chip contents... ");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001631 if (flash->chip->read(flash, curcontents, 0, size)) {
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001632 /* Now we are truly screwed. Read failed as well. */
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001633 msg_cerr("Can't read anymore! Aborting.\n");
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001634 /* We have no idea about the flash chip contents, so
1635 * retrying with another erase function is pointless.
1636 */
1637 break;
1638 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001639 msg_cinfo("done. ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001640 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001641 /* Free the scratchpad. */
1642 free(curcontents);
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001643
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001644 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001645 msg_cerr("FAILED!\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001646 } else {
Stefan Tauner682122b2013-06-23 22:15:39 +00001647 if (all_skipped)
1648 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001649 msg_cinfo("Erase/write done.\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001650 }
1651 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001652}
1653
Nico Huber7af0e792016-04-29 16:40:15 +02001654static const struct flashrom_layout *get_layout(const struct flashctx *const flashctx)
1655{
1656 if (flashctx->layout && flashctx->layout->num_entries)
1657 return flashctx->layout;
1658 else
1659 return &flashctx->fallback_layout.base;
1660}
1661
1662/**
1663 * @brief Reads the included layout regions into a buffer.
1664 *
1665 * If there is no layout set in the given flash context, the whole chip will
1666 * be read.
1667 *
1668 * @param flashctx Flash context to be used.
1669 * @param buffer Buffer of full chip size to read into.
1670 * @return 0 on success,
1671 * 1 if any read fails.
1672 */
1673static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1674{
1675 const struct flashrom_layout *const layout = get_layout(flashctx);
1676
1677 size_t i;
1678 for (i = 0; i < layout->num_entries; ++i) {
1679 if (!layout->entries[i].included)
1680 continue;
1681
1682 const chipoff_t region_start = layout->entries[i].start;
1683 const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1;
1684
1685 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1686 return 1;
1687 }
1688 return 0;
1689}
1690
1691typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1692/**
1693 * @private
1694 *
1695 * For read-erase-write, `curcontents` and `newcontents` shall point
1696 * to buffers of the chip's size. Both are supposed to be prefilled
1697 * with at least the included layout regions of the current flash
1698 * contents (`curcontents`) and the data to be written to the flash
1699 * (`newcontents`).
1700 *
1701 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1702 *
1703 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1704 */
1705struct walk_info {
1706 uint8_t *curcontents;
1707 const uint8_t *newcontents;
1708 chipoff_t region_start;
1709 chipoff_t region_end;
1710 chipoff_t erase_start;
1711 chipoff_t erase_end;
1712};
1713/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1714typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1715
1716static int walk_eraseblocks(struct flashctx *const flashctx,
1717 struct walk_info *const info,
1718 const size_t erasefunction, const per_blockfn_t per_blockfn)
1719{
1720 int ret;
1721 size_t i, j;
1722 bool first = true;
1723 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1724
1725 info->erase_start = 0;
1726 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1727 /* count==0 for all automatically initialized array
1728 members so the loop below won't be executed for them. */
1729 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1730 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1731
1732 /* Skip any eraseblock that is completely outside the current region. */
1733 if (info->erase_end < info->region_start)
1734 continue;
1735 if (info->region_end < info->erase_start)
1736 break;
1737
1738 /* Print this for every block except the first one. */
1739 if (first)
1740 first = false;
1741 else
1742 msg_cdbg(", ");
1743 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1744
1745 ret = per_blockfn(flashctx, info, eraser->block_erase);
1746 if (ret)
1747 return ret;
1748 }
1749 if (info->region_end < info->erase_start)
1750 break;
1751 }
1752 msg_cdbg("\n");
1753 return 0;
1754}
1755
1756static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1757 const per_blockfn_t per_blockfn)
1758{
1759 const struct flashrom_layout *const layout = get_layout(flashctx);
1760
1761 all_skipped = true;
1762 msg_cinfo("Erasing and writing flash chip... ");
1763
1764 size_t i;
1765 for (i = 0; i < layout->num_entries; ++i) {
1766 if (!layout->entries[i].included)
1767 continue;
1768
1769 info->region_start = layout->entries[i].start;
1770 info->region_end = layout->entries[i].end;
1771
1772 size_t j;
1773 int error = 1; /* retry as long as it's 1 */
1774 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1775 if (j != 0)
1776 msg_cinfo("Looking for another erase function.\n");
1777 msg_cdbg("Trying erase function %zi... ", j);
1778 if (check_block_eraser(flashctx, j, 1))
1779 continue;
1780
1781 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1782 if (error != 1)
1783 break;
1784
1785 if (info->curcontents) {
1786 msg_cinfo("Reading current flash chip contents... ");
1787 if (read_by_layout(flashctx, info->curcontents)) {
1788 /* Now we are truly screwed. Read failed as well. */
1789 msg_cerr("Can't read anymore! Aborting.\n");
1790 /* We have no idea about the flash chip contents, so
1791 retrying with another erase function is pointless. */
1792 error = 2;
1793 break;
1794 }
1795 msg_cinfo("done. ");
1796 }
1797 }
1798 if (error == 1)
1799 msg_cinfo("No usable erase functions left.\n");
1800 if (error) {
1801 msg_cerr("FAILED!\n");
1802 return 1;
1803 }
1804 }
1805 if (all_skipped)
1806 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1807 msg_cinfo("Erase/write done.\n");
1808 return 0;
1809}
1810
1811static int erase_block(struct flashctx *const flashctx,
1812 const struct walk_info *const info, const erasefn_t erasefn)
1813{
1814 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
1815
1816 all_skipped = false;
1817
1818 msg_cdbg("E");
1819 if (erasefn(flashctx, info->erase_start, erase_len))
1820 return 1;
1821 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1822 msg_cerr("ERASE FAILED!\n");
1823 return 1;
1824 }
1825 return 0;
1826}
1827
1828/**
1829 * @brief Erases the included layout regions.
1830 *
1831 * If there is no layout set in the given flash context, the whole chip will
1832 * be erased.
1833 *
1834 * @param flashctx Flash context to be used.
1835 * @param buffer Buffer of full chip size to read into.
1836 * @return 0 on success,
1837 * 1 if all available erase functions failed.
1838 */
Nico Huber454f6132012-12-10 13:34:10 +00001839static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001840{
1841 struct walk_info info = { 0 };
1842 return walk_by_layout(flashctx, &info, &erase_block);
1843}
1844
1845static int read_erase_write_block(struct flashctx *const flashctx,
1846 const struct walk_info *const info, const erasefn_t erasefn)
1847{
1848 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1849 const bool region_unaligned = info->region_start > info->erase_start ||
1850 info->erase_end > info->region_end;
1851 const uint8_t *newcontents = NULL;
1852 int ret = 2;
1853
1854 /*
1855 * If the region is not erase-block aligned, merge current flash con-
1856 * tents into `info->curcontents` and a new buffer `newc`. The former
1857 * is necessary since we have no guarantee that the full erase block
1858 * was already read into `info->curcontents`. For the latter a new
1859 * buffer is used since `info->newcontents` might contain data for
1860 * other unaligned regions that touch this erase block too.
1861 */
1862 if (region_unaligned) {
1863 msg_cdbg("R");
1864 uint8_t *const newc = malloc(erase_len);
1865 if (!newc) {
1866 msg_cerr("Out of memory!\n");
1867 return 1;
1868 }
1869 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1870
1871 /* Merge data preceding the current region. */
1872 if (info->region_start > info->erase_start) {
1873 const chipoff_t start = info->erase_start;
1874 const chipsize_t len = info->region_start - info->erase_start;
1875 if (flashctx->chip->read(flashctx, newc, start, len)) {
1876 msg_cerr("Can't read! Aborting.\n");
1877 goto _free_ret;
1878 }
1879 memcpy(info->curcontents + start, newc, len);
1880 }
1881 /* Merge data following the current region. */
1882 if (info->erase_end > info->region_end) {
1883 const chipoff_t start = info->region_end + 1;
1884 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1885 const chipsize_t len = info->erase_end - info->region_end;
1886 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1887 msg_cerr("Can't read! Aborting.\n");