blob: d87a43171b5269677beb62f290ce63fe6a9c8848 [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"
Nico Huberfe34d2a2017-11-10 21:10:20 +010045#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000046
Mathias Krausea60faab2011-01-17 07:50:42 +000047const char flashrom_version[] = FLASHROM_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000048const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000049
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +000050static enum programmer programmer = PROGRAMMER_INVALID;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000051static const char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000052
Uwe Hermann48ec1b12010-08-08 17:01:18 +000053/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000054 * Programmers supporting multiple buses can have differing size limits on
55 * each bus. Store the limits for each bus in a common struct.
56 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000057struct decode_sizes max_rom_decode;
58
59/* If nonzero, used as the start address of bottom-aligned flash. */
60unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000061
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000062/* Is writing allowed with this programmer? */
63int programmer_may_write;
64
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000065const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000066#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000067 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000068 .name = "internal",
Stefan Tauneraf358d62012-12-27 18:40:26 +000069 .type = OTHER,
70 .devs.note = NULL,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000071 .init = internal_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000072 .map_flash_region = physmap,
73 .unmap_flash_region = physunmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000074 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000075 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000076#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000077
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000078#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000079 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000080 .name = "dummy",
Stefan Tauneraf358d62012-12-27 18:40:26 +000081 .type = OTHER,
82 /* FIXME */
83 .devs.note = "Dummy device, does nothing and logs all accesses\n",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000084 .init = dummy_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000085 .map_flash_region = dummy_map,
86 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000087 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000088 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000089#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000090
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000091#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +000092 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000093 .name = "nic3com",
Stefan Tauneraf358d62012-12-27 18:40:26 +000094 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +000095 .devs.dev = nics_3com,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000096 .init = nic3com_init,
Uwe Hermannc6915932009-05-17 23:12:17 +000097 .map_flash_region = fallback_map,
98 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000099 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000100 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000101#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000102
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000103#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000104 {
Carl-Daniel Hailfinger40446ee2011-03-07 01:08:09 +0000105 /* This programmer works for Realtek RTL8139 and SMC 1211. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000106 .name = "nicrealtek",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000107 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000108 .devs.dev = nics_realtek,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000109 .init = nicrealtek_init,
110 .map_flash_region = fallback_map,
111 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000112 .delay = internal_delay,
Joerg Fischer5665ef32010-05-21 21:54:07 +0000113 },
Joerg Fischer5665ef32010-05-21 21:54:07 +0000114#endif
115
Andrew Morganc29c2e72010-06-07 22:37:54 +0000116#if CONFIG_NICNATSEMI == 1
117 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000118 .name = "nicnatsemi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000119 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000120 .devs.dev = nics_natsemi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000121 .init = nicnatsemi_init,
122 .map_flash_region = fallback_map,
123 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000124 .delay = internal_delay,
Andrew Morganc29c2e72010-06-07 22:37:54 +0000125 },
126#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000127
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000128#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000129 {
130 .name = "gfxnvidia",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000131 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000132 .devs.dev = gfx_nvidia,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000133 .init = gfxnvidia_init,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000134 .map_flash_region = fallback_map,
135 .unmap_flash_region = fallback_unmap,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000136 .delay = internal_delay,
137 },
138#endif
139
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000140#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000141 {
TURBO Jb0912c02009-09-02 23:00:46 +0000142 .name = "drkaiser",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000143 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000144 .devs.dev = drkaiser_pcidev,
TURBO Jb0912c02009-09-02 23:00:46 +0000145 .init = drkaiser_init,
TURBO Jb0912c02009-09-02 23:00:46 +0000146 .map_flash_region = fallback_map,
147 .unmap_flash_region = fallback_unmap,
TURBO Jb0912c02009-09-02 23:00:46 +0000148 .delay = internal_delay,
149 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000150#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000151
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000152#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000153 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000154 .name = "satasii",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000155 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000156 .devs.dev = satas_sii,
Rudolf Marek68720c72009-05-17 19:39:27 +0000157 .init = satasii_init,
Uwe Hermannc6915932009-05-17 23:12:17 +0000158 .map_flash_region = fallback_map,
159 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000160 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000161 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000162#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000163
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000164#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000165 {
166 .name = "atahpt",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000167 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000168 .devs.dev = ata_hpt,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000169 .init = atahpt_init,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000170 .map_flash_region = fallback_map,
171 .unmap_flash_region = fallback_unmap,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000172 .delay = internal_delay,
173 },
174#endif
175
Jonathan Kollasch7f0f3fa2014-06-01 10:26:23 +0000176#if CONFIG_ATAVIA == 1
177 {
178 .name = "atavia",
179 .type = PCI,
180 .devs.dev = ata_via,
181 .init = atavia_init,
182 .map_flash_region = atavia_map,
183 .unmap_flash_region = fallback_unmap,
184 .delay = internal_delay,
185 },
186#endif
187
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +0000188#if CONFIG_ATAPROMISE == 1
189 {
190 .name = "atapromise",
191 .type = PCI,
192 .devs.dev = ata_promise,
193 .init = atapromise_init,
194 .map_flash_region = atapromise_map,
195 .unmap_flash_region = fallback_unmap,
196 .delay = internal_delay,
197 },
198#endif
199
Kyösti Mälkki72d42f82014-06-01 23:48:31 +0000200#if CONFIG_IT8212 == 1
201 {
202 .name = "it8212",
203 .type = PCI,
204 .devs.dev = devs_it8212,
205 .init = it8212_init,
206 .map_flash_region = fallback_map,
207 .unmap_flash_region = fallback_unmap,
208 .delay = internal_delay,
209 },
210#endif
211
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000212#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000213 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000214 .name = "ft2232_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000215 .type = USB,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000216 .devs.dev = devs_ft2232spi,
Paul Fox05dfbe62009-06-16 21:08:06 +0000217 .init = ft2232_spi_init,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000218 .map_flash_region = fallback_map,
219 .unmap_flash_region = fallback_unmap,
Paul Fox05dfbe62009-06-16 21:08:06 +0000220 .delay = internal_delay,
221 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000222#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000223
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000224#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000225 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000226 .name = "serprog",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000227 .type = OTHER,
228 /* FIXME */
229 .devs.note = "All programmer devices speaking the serprog protocol\n",
Urja Rannikko22915352009-06-23 11:33:43 +0000230 .init = serprog_init,
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000231 .map_flash_region = serprog_map,
Urja Rannikko22915352009-06-23 11:33:43 +0000232 .unmap_flash_region = fallback_unmap,
Urja Rannikko22915352009-06-23 11:33:43 +0000233 .delay = serprog_delay,
234 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000235#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000236
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000237#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000238 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000239 .name = "buspirate_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000240 .type = OTHER,
241 /* FIXME */
242 .devs.note = "Dangerous Prototypes Bus Pirate\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000243 .init = buspirate_spi_init,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000244 .map_flash_region = fallback_map,
245 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000246 .delay = internal_delay,
247 },
248#endif
249
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000250#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000251 {
252 .name = "dediprog",
Stefan Taunerfdec7472016-02-22 08:59:27 +0000253 .type = USB,
254 .devs.dev = devs_dediprog,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000255 .init = dediprog_init,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000256 .map_flash_region = fallback_map,
257 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000258 .delay = internal_delay,
259 },
260#endif
261
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000262#if CONFIG_RAYER_SPI == 1
263 {
264 .name = "rayer_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000265 .type = OTHER,
266 /* FIXME */
267 .devs.note = "RayeR parallel port programmer\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000268 .init = rayer_spi_init,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000269 .map_flash_region = fallback_map,
270 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000271 .delay = internal_delay,
272 },
273#endif
274
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000275#if CONFIG_PONY_SPI == 1
276 {
277 .name = "pony_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000278 .type = OTHER,
279 /* FIXME */
280 .devs.note = "Programmers compatible with SI-Prog, serbang or AJAWe\n",
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000281 .init = pony_spi_init,
282 .map_flash_region = fallback_map,
283 .unmap_flash_region = fallback_unmap,
284 .delay = internal_delay,
Stefan Tauneraf358d62012-12-27 18:40:26 +0000285 },
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000286#endif
287
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000288#if CONFIG_NICINTEL == 1
289 {
290 .name = "nicintel",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000291 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000292 .devs.dev = nics_intel,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000293 .init = nicintel_init,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000294 .map_flash_region = fallback_map,
295 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000296 .delay = internal_delay,
297 },
298#endif
299
Idwer Vollering004f4b72010-09-03 18:21:21 +0000300#if CONFIG_NICINTEL_SPI == 1
301 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000302 .name = "nicintel_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000303 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000304 .devs.dev = nics_intel_spi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000305 .init = nicintel_spi_init,
306 .map_flash_region = fallback_map,
307 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000308 .delay = internal_delay,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000309 },
310#endif
311
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000312#if CONFIG_NICINTEL_EEPROM == 1
313 {
314 .name = "nicintel_eeprom",
315 .type = PCI,
316 .devs.dev = nics_intel_ee,
317 .init = nicintel_ee_init,
318 .map_flash_region = fallback_map,
319 .unmap_flash_region = fallback_unmap,
320 .delay = internal_delay,
321 },
322#endif
323
Mark Marshall90021f22010-12-03 14:48:11 +0000324#if CONFIG_OGP_SPI == 1
325 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000326 .name = "ogp_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000327 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000328 .devs.dev = ogp_spi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000329 .init = ogp_spi_init,
330 .map_flash_region = fallback_map,
331 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000332 .delay = internal_delay,
Mark Marshall90021f22010-12-03 14:48:11 +0000333 },
334#endif
335
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000336#if CONFIG_SATAMV == 1
337 {
338 .name = "satamv",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000339 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000340 .devs.dev = satas_mv,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000341 .init = satamv_init,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000342 .map_flash_region = fallback_map,
343 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000344 .delay = internal_delay,
345 },
346#endif
347
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000348#if CONFIG_LINUX_SPI == 1
349 {
350 .name = "linux_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000351 .type = OTHER,
352 .devs.note = "Device files /dev/spidev*.*\n",
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000353 .init = linux_spi_init,
354 .map_flash_region = fallback_map,
355 .unmap_flash_region = fallback_unmap,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000356 .delay = internal_delay,
357 },
358#endif
359
James Lairdc60de0e2013-03-27 13:00:23 +0000360#if CONFIG_USBBLASTER_SPI == 1
361 {
362 .name = "usbblaster_spi",
363 .type = USB,
364 .devs.dev = devs_usbblasterspi,
365 .init = usbblaster_spi_init,
366 .map_flash_region = fallback_map,
367 .unmap_flash_region = fallback_unmap,
368 .delay = internal_delay,
369 },
370#endif
371
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000372#if CONFIG_MSTARDDC_SPI == 1
373 {
374 .name = "mstarddc_spi",
375 .type = OTHER,
376 .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n",
377 .init = mstarddc_spi_init,
378 .map_flash_region = fallback_map,
379 .unmap_flash_region = fallback_unmap,
380 .delay = internal_delay,
381 },
382#endif
383
Justin Chevrier66e554b2015-02-08 21:58:10 +0000384#if CONFIG_PICKIT2_SPI == 1
385 {
386 .name = "pickit2_spi",
Stefan Taunerf31fe842016-02-22 08:59:15 +0000387 .type = USB,
388 .devs.dev = devs_pickit2_spi,
Justin Chevrier66e554b2015-02-08 21:58:10 +0000389 .init = pickit2_spi_init,
390 .map_flash_region = fallback_map,
391 .unmap_flash_region = fallback_unmap,
392 .delay = internal_delay,
393 },
394#endif
395
Urja Rannikko0870b022016-01-31 22:10:29 +0000396#if CONFIG_CH341A_SPI == 1
397 {
398 .name = "ch341a_spi",
399 .type = USB,
400 .devs.dev = devs_ch341a_spi,
401 .init = ch341a_spi_init,
402 .map_flash_region = fallback_map,
403 .unmap_flash_region = fallback_unmap,
404 .delay = ch341a_spi_delay,
405 },
406#endif
407
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000408 {0}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000409};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000410
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +0000411#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000412static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +0000413/** @private */
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000414struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +0000415 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000416 void *data;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000417} static shutdown_fn[SHUTDOWN_MAXFN];
418/* Initialize to 0 to make sure nobody registers a shutdown function before
419 * programmer init.
420 */
421static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000422
Stefan Taunerc4f44df2013-08-12 22:58:43 +0000423/* Did we change something or was every erase/write skipped (if any)? */
424static bool all_skipped = true;
425
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000426static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +0000427
Stefan Tauner2a1ed772014-08-31 00:09:21 +0000428int shutdown_free(void *data)
429{
430 free(data);
431 return 0;
432}
433
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000434/* Register a function to be executed on programmer shutdown.
435 * The advantage over atexit() is that you can supply a void pointer which will
436 * be used as parameter to the registered function upon programmer shutdown.
437 * This pointer can point to arbitrary data used by said function, e.g. undo
438 * information for GPIO settings etc. If unneeded, set data=NULL.
439 * Please note that the first (void *data) belongs to the function signature of
440 * the function passed as first parameter.
441 */
David Hendricks8bb20212011-06-14 01:35:36 +0000442int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000443{
444 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000445 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000446 SHUTDOWN_MAXFN);
447 return 1;
448 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000449 if (!may_register_shutdown) {
450 msg_perr("Tried to register a shutdown function before "
451 "programmer init.\n");
452 return 1;
453 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000454 shutdown_fn[shutdown_fn_count].func = function;
455 shutdown_fn[shutdown_fn_count].data = data;
456 shutdown_fn_count++;
457
458 return 0;
459}
460
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000461int programmer_init(enum programmer prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000462{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000463 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000464
465 if (prog >= PROGRAMMER_INVALID) {
466 msg_perr("Invalid programmer specified!\n");
467 return -1;
468 }
469 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000470 /* Initialize all programmer specific data. */
471 /* Default to unlimited decode sizes. */
472 max_rom_decode = (const struct decode_sizes) {
473 .parallel = 0xffffffff,
474 .lpc = 0xffffffff,
475 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000476 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000477 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000478 /* Default to top aligned flash at 4 GB. */
479 flashbase = 0;
480 /* Registering shutdown functions is now allowed. */
481 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000482 /* Default to allowing writes. Broken programmers set this to 0. */
483 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000484
485 programmer_param = param;
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000486 msg_pdbg("Initializing %s programmer\n", programmer_table[programmer].name);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000487 ret = programmer_table[programmer].init();
488 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000489 if (ret != 0) {
490 /* It is quite possible that any unhandled programmer parameter would have been valid,
491 * but an error in actual programmer init happened before the parameter was evaluated.
492 */
493 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
494 programmer_param);
495 } else {
496 /* Actual programmer init was successful, but the user specified an invalid or unusable
497 * (for the current programmer configuration) parameter.
498 */
499 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
500 msg_perr("Aborting.\n");
501 ret = ERROR_FATAL;
502 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000503 }
504 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000505}
506
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000507/** Calls registered shutdown functions and resets internal programmer-related variables.
508 * Calling it is safe even without previous initialization, but further interactions with programmer support
509 * require a call to programmer_init() (afterwards).
510 *
511 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000512int programmer_shutdown(void)
513{
David Hendricks8bb20212011-06-14 01:35:36 +0000514 int ret = 0;
515
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000516 /* Registering shutdown functions is no longer allowed. */
517 may_register_shutdown = 0;
518 while (shutdown_fn_count > 0) {
519 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000520 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000521 }
Stefan Taunere34e3e82013-01-01 00:06:51 +0000522
Stefan Taunerb8911d62012-12-26 07:55:00 +0000523 programmer_param = NULL;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000524 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000525
David Hendricks8bb20212011-06-14 01:35:36 +0000526 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000527}
528
Stefan Tauner305e0b92013-07-17 23:46:44 +0000529void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000530{
Stefan Tauner26e7a152013-09-13 17:21:05 +0000531 void *ret = programmer_table[programmer].map_flash_region(descr, phys_addr, len);
532 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
533 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
534 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000535}
536
537void programmer_unmap_flash_region(void *virt_addr, size_t len)
538{
539 programmer_table[programmer].unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000540 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000541}
542
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000543void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000544{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000545 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000546}
547
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000548void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000549{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000550 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000551}
552
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000553void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000554{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000555 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000556}
557
Mark Marshallf20b7be2014-05-09 21:16:21 +0000558void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000559{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000560 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000561}
562
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000563uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000564{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000565 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000566}
567
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000568uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000569{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000570 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000571}
572
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000573uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000574{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000575 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000576}
577
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000578void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
579 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000580{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000581 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000582}
583
Stefan Taunerf80419c2014-05-02 15:41:42 +0000584void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000585{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000586 if (usecs > 0)
587 programmer_table[programmer].delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000588}
589
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000590int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
591 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000592{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000593 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000594
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000595 return 0;
596}
597
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000598/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000599 * It will look for needle with a subsequent '=' in haystack, return a copy of
600 * needle and remove everything from the first occurrence of needle to the next
601 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000602 */
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000603char *extract_param(const char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000604{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000605 char *param_pos, *opt_pos, *rest;
606 char *opt = NULL;
607 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000608 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000609
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000610 needlelen = strlen(needle);
611 if (!needlelen) {
612 msg_gerr("%s: empty needle! Please report a bug at "
613 "flashrom@flashrom.org\n", __func__);
614 return NULL;
615 }
616 /* No programmer parameters given. */
617 if (*haystack == NULL)
618 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000619 param_pos = strstr(*haystack, needle);
620 do {
621 if (!param_pos)
622 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000623 /* Needle followed by '='? */
624 if (param_pos[needlelen] == '=') {
625
626 /* Beginning of the string? */
627 if (param_pos == *haystack)
628 break;
629 /* After a delimiter? */
630 if (strchr(delim, *(param_pos - 1)))
631 break;
632 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000633 /* Continue searching. */
634 param_pos++;
635 param_pos = strstr(param_pos, needle);
636 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000637
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000638 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000639 /* Get the string after needle and '='. */
640 opt_pos = param_pos + needlelen + 1;
641 optlen = strcspn(opt_pos, delim);
642 /* Return an empty string if the parameter was empty. */
643 opt = malloc(optlen + 1);
644 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000645 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000646 exit(1);
647 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000648 strncpy(opt, opt_pos, optlen);
649 opt[optlen] = '\0';
650 rest = opt_pos + optlen;
651 /* Skip all delimiters after the current parameter. */
652 rest += strspn(rest, delim);
653 memmove(param_pos, rest, strlen(rest) + 1);
654 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000655 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000656
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000657 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000658}
659
Stefan Tauner66652442011-06-26 17:38:17 +0000660char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000661{
662 return extract_param(&programmer_param, param_name, ",");
663}
664
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000665/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000666static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000667{
668 unsigned int usable_erasefunctions = 0;
669 int k;
670 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
671 if (!check_block_eraser(flash, k, 0))
672 usable_erasefunctions++;
673 }
674 return usable_erasefunctions;
675}
676
Mark Marshallf20b7be2014-05-09 21:16:21 +0000677static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000678{
679 int ret = 0, failcount = 0;
680 unsigned int i;
681 for (i = 0; i < len; i++) {
682 if (wantbuf[i] != havebuf[i]) {
683 /* Only print the first failure. */
684 if (!failcount++)
685 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
686 start + i, wantbuf[i], havebuf[i]);
687 }
688 }
689 if (failcount) {
690 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
691 start, start + len - 1, failcount);
692 ret = -1;
693 }
694 return ret;
695}
696
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000697/* start is an offset to the base address of the flash chip */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000698int check_erased_range(struct flashctx *flash, unsigned int start,
699 unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000700{
701 int ret;
702 uint8_t *cmpbuf = malloc(len);
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300703 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000704
705 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000706 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000707 exit(1);
708 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300709 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000710 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000711 free(cmpbuf);
712 return ret;
713}
714
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000715/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000716 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000717 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000718 * @start offset to the base address of the flash chip
719 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000720 * @return 0 for success, -1 for failure
721 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000722int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000723{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000724 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000725 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000726
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000727 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000728 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000729 return -1;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000730 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000731
732 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000733 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000734 msg_gerr("Could not allocate memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000735 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000736 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000737 int ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000738
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000739 if (start + len > flash->chip->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000740 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000741 " total_size 0x%x\n", __func__, start, len,
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000742 flash->chip->total_size * 1024);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000743 ret = -1;
744 goto out_free;
745 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000746
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000747 ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000748 if (ret) {
749 msg_gerr("Verification impossible because read failed "
750 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000751 ret = -1;
752 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000753 }
754
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000755 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000756out_free:
757 free(readbuf);
758 return ret;
759}
760
Stefan Tauner02437452013-04-01 19:34:53 +0000761/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300762static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
763 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000764{
765 unsigned int i, j, limit;
766 for (j = 0; j < len / gran; j++) {
767 limit = min (gran, len - j * gran);
768 /* Are 'have' and 'want' identical? */
769 if (!memcmp(have + j * gran, want + j * gran, limit))
770 continue;
771 /* have needs to be in erased state. */
772 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300773 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000774 return 1;
775 }
776 return 0;
777}
778
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000779/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000780 * Check if the buffer @have can be programmed to the content of @want without
781 * erasing. This is only possible if all chunks of size @gran are either kept
782 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000783 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000784 * Warning: This function assumes that @have and @want point to naturally
785 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000786 *
787 * @have buffer with current content
788 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000789 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000790 * @gran write granularity (enum, not count)
791 * @return 0 if no erase is needed, 1 otherwise
792 */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300793int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
794 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000795{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000796 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000797 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000798
799 switch (gran) {
800 case write_gran_1bit:
801 for (i = 0; i < len; i++)
802 if ((have[i] & want[i]) != want[i]) {
803 result = 1;
804 break;
805 }
806 break;
807 case write_gran_1byte:
808 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300809 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000810 result = 1;
811 break;
812 }
813 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000814 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300815 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000816 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000817 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300818 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000819 break;
820 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300821 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000822 break;
823 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300824 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000825 break;
826 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300827 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000828 break;
829 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300830 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000831 break;
832 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300833 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000834 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000835 case write_gran_1byte_implicit_erase:
836 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
837 result = 0;
838 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000839 default:
840 msg_cerr("%s: Unsupported granularity! Please report a bug at "
841 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000842 }
843 return result;
844}
845
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000846/**
847 * Check if the buffer @have needs to be programmed to get the content of @want.
848 * If yes, return 1 and fill in first_start with the start address of the
849 * write operation and first_len with the length of the first to-be-written
850 * chunk. If not, return 0 and leave first_start and first_len undefined.
851 *
852 * Warning: This function assumes that @have and @want point to naturally
853 * aligned regions.
854 *
855 * @have buffer with current content
856 * @want buffer with desired content
857 * @len length of the checked area
858 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000859 * @first_start offset of the first byte which needs to be written (passed in
860 * value is increased by the offset of the first needed write
861 * relative to have/want or unchanged if no write is needed)
862 * @return length of the first contiguous area which needs to be written
863 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000864 *
865 * FIXME: This function needs a parameter which tells it about coalescing
866 * in relation to the max write length of the programmer and the max write
867 * length of the chip.
868 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000869static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000870 unsigned int *first_start,
871 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000872{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000873 int need_write = 0;
874 unsigned int rel_start = 0, first_len = 0;
875 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000876
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000877 switch (gran) {
878 case write_gran_1bit:
879 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000880 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000881 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000882 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000883 case write_gran_128bytes:
884 stride = 128;
885 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000886 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000887 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000888 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000889 case write_gran_264bytes:
890 stride = 264;
891 break;
892 case write_gran_512bytes:
893 stride = 512;
894 break;
895 case write_gran_528bytes:
896 stride = 528;
897 break;
898 case write_gran_1024bytes:
899 stride = 1024;
900 break;
901 case write_gran_1056bytes:
902 stride = 1056;
903 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000904 default:
905 msg_cerr("%s: Unsupported granularity! Please report a bug at "
906 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000907 /* Claim that no write was needed. A write with unknown
908 * granularity is too dangerous to try.
909 */
910 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000911 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000912 for (i = 0; i < len / stride; i++) {
913 limit = min(stride, len - i * stride);
914 /* Are 'have' and 'want' identical? */
915 if (memcmp(have + i * stride, want + i * stride, limit)) {
916 if (!need_write) {
917 /* First location where have and want differ. */
918 need_write = 1;
919 rel_start = i * stride;
920 }
921 } else {
922 if (need_write) {
923 /* First location where have and want
924 * do not differ anymore.
925 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000926 break;
927 }
928 }
929 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000930 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000931 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000932 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000933 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000934}
935
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000936/* This function generates various test patterns useful for testing controller
937 * and chip communication as well as chip behaviour.
938 *
939 * If a byte can be written multiple times, each time keeping 0-bits at 0
940 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
941 * is essentially an AND operation. That's also the reason why this function
942 * provides the result of AND between various patterns.
943 *
944 * Below is a list of patterns (and their block length).
945 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
946 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
947 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
948 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
949 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
950 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
951 * Pattern 6 is 00 (1 Byte)
952 * Pattern 7 is ff (1 Byte)
953 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
954 * byte block.
955 *
956 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
957 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
958 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
959 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
960 * Pattern 12 is 00 (1 Byte)
961 * Pattern 13 is ff (1 Byte)
962 * Patterns 8-13 have no block number.
963 *
964 * Patterns 0-3 are created to detect and efficiently diagnose communication
965 * slips like missed bits or bytes and their repetitive nature gives good visual
966 * cues to the person inspecting the results. In addition, the following holds:
967 * AND Pattern 0/1 == Pattern 4
968 * AND Pattern 2/3 == Pattern 5
969 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
970 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
971 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
972 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
973 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
974 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
975 * Besides that, they provide for bit testing of the last two bytes of every
976 * 256 byte block which contains the block number for patterns 0-6.
977 * Patterns 10-11 are special purpose for detecting subblock aliasing with
978 * block sizes >256 bytes (some Dataflash chips etc.)
979 * AND Pattern 8/9 == Pattern 12
980 * AND Pattern 10/11 == Pattern 12
981 * Pattern 13 is the completely erased state.
982 * None of the patterns can detect aliasing at boundaries which are a multiple
983 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
984 */
985int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
986{
987 int i;
988
989 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000990 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000991 return 1;
992 }
993
994 switch (variant) {
995 case 0:
996 for (i = 0; i < size; i++)
997 buf[i] = (i & 0xf) << 4 | 0x5;
998 break;
999 case 1:
1000 for (i = 0; i < size; i++)
1001 buf[i] = (i & 0xf) << 4 | 0xa;
1002 break;
1003 case 2:
1004 for (i = 0; i < size; i++)
1005 buf[i] = 0x50 | (i & 0xf);
1006 break;
1007 case 3:
1008 for (i = 0; i < size; i++)
1009 buf[i] = 0xa0 | (i & 0xf);
1010 break;
1011 case 4:
1012 for (i = 0; i < size; i++)
1013 buf[i] = (i & 0xf) << 4;
1014 break;
1015 case 5:
1016 for (i = 0; i < size; i++)
1017 buf[i] = i & 0xf;
1018 break;
1019 case 6:
1020 memset(buf, 0x00, size);
1021 break;
1022 case 7:
1023 memset(buf, 0xff, size);
1024 break;
1025 case 8:
1026 for (i = 0; i < size; i++)
1027 buf[i] = i & 0xff;
1028 break;
1029 case 9:
1030 for (i = 0; i < size; i++)
1031 buf[i] = ~(i & 0xff);
1032 break;
1033 case 10:
1034 for (i = 0; i < size % 2; i++) {
1035 buf[i * 2] = (i >> 8) & 0xff;
1036 buf[i * 2 + 1] = i & 0xff;
1037 }
1038 if (size & 0x1)
1039 buf[i * 2] = (i >> 8) & 0xff;
1040 break;
1041 case 11:
1042 for (i = 0; i < size % 2; i++) {
1043 buf[i * 2] = ~((i >> 8) & 0xff);
1044 buf[i * 2 + 1] = ~(i & 0xff);
1045 }
1046 if (size & 0x1)
1047 buf[i * 2] = ~((i >> 8) & 0xff);
1048 break;
1049 case 12:
1050 memset(buf, 0x00, size);
1051 break;
1052 case 13:
1053 memset(buf, 0xff, size);
1054 break;
1055 }
1056
1057 if ((variant >= 0) && (variant <= 7)) {
1058 /* Write block number in the last two bytes of each 256-byte
1059 * block, big endian for easier reading of the hexdump.
1060 * Note that this wraps around for chips larger than 2^24 bytes
1061 * (16 MB).
1062 */
1063 for (i = 0; i < size / 256; i++) {
1064 buf[i * 256 + 254] = (i >> 8) & 0xff;
1065 buf[i * 256 + 255] = i & 0xff;
1066 }
1067 }
1068
1069 return 0;
1070}
1071
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001072/* Returns the number of busses commonly supported by the current programmer and flash chip where the latter
1073 * can not be completely accessed due to size/address limits of the programmer. */
1074unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001075{
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001076 unsigned int limitexceeded = 0;
1077 uint32_t size = flash->chip->total_size * 1024;
1078 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001079
1080 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001081 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001082 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001083 "size %u kB of chipset/board/programmer "
1084 "for %s interface, "
1085 "probe/read/erase/write may fail. ", size / 1024,
1086 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001087 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001088 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001089 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001090 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001091 "size %u kB of chipset/board/programmer "
1092 "for %s interface, "
1093 "probe/read/erase/write may fail. ", size / 1024,
1094 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001095 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001096 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001097 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001098 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001099 "size %u kB of chipset/board/programmer "
1100 "for %s interface, "
1101 "probe/read/erase/write may fail. ", size / 1024,
1102 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001103 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001104 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001105 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001106 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001107 "size %u kB of chipset/board/programmer "
1108 "for %s interface, "
1109 "probe/read/erase/write may fail. ", size / 1024,
1110 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001111 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001112 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001113}
1114
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001115void unmap_flash(struct flashctx *flash)
1116{
1117 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
1118 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
1119 flash->physical_registers = 0;
1120 flash->virtual_registers = (chipaddr)ERROR_PTR;
1121 }
1122
1123 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
1124 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
1125 flash->physical_memory = 0;
1126 flash->virtual_memory = (chipaddr)ERROR_PTR;
1127 }
1128}
1129
1130int map_flash(struct flashctx *flash)
1131{
1132 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
1133 flash->virtual_memory = (chipaddr)ERROR_PTR;
1134 flash->virtual_registers = (chipaddr)ERROR_PTR;
1135
1136 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
1137 * These are used for various probing-related hacks that would not map successfully anyway and should be
1138 * removed ASAP. */
1139 if (flash->chip->total_size == 0)
1140 return 0;
1141
1142 const chipsize_t size = flash->chip->total_size * 1024;
1143 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
1144 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
1145 if (addr == ERROR_PTR) {
1146 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
1147 flash->chip->name, PRIxPTR_WIDTH, base);
1148 return 1;
1149 }
1150 flash->physical_memory = base;
1151 flash->virtual_memory = (chipaddr)addr;
1152
1153 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
1154 * completely different on some chips and programmers, or not mappable at all.
1155 * Ignore these problems for now and always report success. */
1156 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
1157 base = 0xffffffff - size - 0x400000 + 1;
1158 addr = programmer_map_flash_region("flash chip registers", base, size);
1159 if (addr == ERROR_PTR) {
1160 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
1161 flash->chip->name, PRIxPTR_WIDTH, base);
1162 return 0;
1163 }
1164 flash->physical_registers = base;
1165 flash->virtual_registers = (chipaddr)addr;
1166 }
1167 return 0;
1168}
1169
Nico Huber2d625722016-05-03 10:48:02 +02001170/*
1171 * Return a string corresponding to the bustype parameter.
1172 * Memory is obtained with malloc() and must be freed with free() by the caller.
1173 */
1174char *flashbuses_to_text(enum chipbustype bustype)
1175{
1176 char *ret = calloc(1, 1);
1177 /*
1178 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
1179 * will cease to exist and should be eliminated here as well.
1180 */
1181 if (bustype == BUS_NONSPI) {
1182 ret = strcat_realloc(ret, "Non-SPI, ");
1183 } else {
1184 if (bustype & BUS_PARALLEL)
1185 ret = strcat_realloc(ret, "Parallel, ");
1186 if (bustype & BUS_LPC)
1187 ret = strcat_realloc(ret, "LPC, ");
1188 if (bustype & BUS_FWH)
1189 ret = strcat_realloc(ret, "FWH, ");
1190 if (bustype & BUS_SPI)
1191 ret = strcat_realloc(ret, "SPI, ");
1192 if (bustype & BUS_PROG)
1193 ret = strcat_realloc(ret, "Programmer-specific, ");
1194 if (bustype == BUS_NONE)
1195 ret = strcat_realloc(ret, "None, ");
1196 }
1197 /* Kill last comma. */
1198 ret[strlen(ret) - 2] = '\0';
1199 ret = realloc(ret, strlen(ret) + 1);
1200 return ret;
1201}
1202
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001203int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001204{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001205 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001206 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001207 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001208
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001209 for (chip = flashchips + startchip; chip && chip->name; chip++) {
1210 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +00001211 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001212 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001213 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +00001214 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +03001215 /* Only probe for SPI25 chips by default. */
1216 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
1217 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001218 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
1219 if (!chip->probe && !force) {
1220 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001221 continue;
1222 }
Stefan Reinauer70385642007-04-06 11:58:03 +00001223
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001224 /* Start filling in the dynamic data. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001225 flash->chip = calloc(1, sizeof(struct flashchip));
1226 if (!flash->chip) {
1227 msg_gerr("Out of memory!\n");
1228 exit(1);
1229 }
1230 memcpy(flash->chip, chip, sizeof(struct flashchip));
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001231 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001232
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001233 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +01001234 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001235
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001236 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
1237 * is only called with force=1 after normal probing failed.
1238 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001239 if (force)
1240 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001241
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001242 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +00001243 goto notfound;
1244
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001245 /* If this is the first chip found, accept it.
1246 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001247 * a non-generic match. SFDP and CFI are generic matches.
1248 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001249 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001250 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001251 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001252 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001253 msg_cinfo("===\n"
1254 "SFDP has autodetected a flash chip which is "
1255 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001256 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001257 msg_cinfo("The standard operations read and "
1258 "verify should work, but to support "
1259 "erase, write and all other "
1260 "possible features");
1261 else
1262 msg_cinfo("All standard operations (read, "
1263 "verify, erase and write) should "
1264 "work, but to support all possible "
1265 "features");
1266
Stefan Taunerb4e06bd2012-08-20 00:24:22 +00001267 msg_cinfo(" we need to add them manually.\n"
1268 "You can help us by mailing us the output of the following command to "
1269 "flashrom@flashrom.org:\n"
1270 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
1271 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001272 "===\n");
1273 }
1274
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001275 /* First flash chip detected on this bus. */
1276 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001277 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001278 /* Not the first flash chip detected on this bus, but not a generic match either. */
1279 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
1280 break;
1281 /* 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 +00001282notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001283 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001284 free(flash->chip);
1285 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001286 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001287
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001288 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001289 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001290
Nico Huber7af0e792016-04-29 16:40:15 +02001291 /* Fill fallback layout covering the whole chip. */
1292 struct single_layout *const fallback = &flash->fallback_layout;
1293 fallback->base.entries = &fallback->entry;
1294 fallback->base.num_entries = 1;
1295 fallback->entry.start = 0;
1296 fallback->entry.end = flash->chip->total_size * 1024 - 1;
1297 fallback->entry.included = true;
1298 strcpy(fallback->entry.name, "complete flash");
Stefan Reinauer051e2362011-01-19 06:21:54 +00001299
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001300 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001301 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
1302 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +00001303 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001304#if CONFIG_INTERNAL == 1
1305 if (programmer_table[programmer].map_flash_region == physmap)
1306 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
1307 PRIxPTR_WIDTH, flash->physical_memory);
1308 else
1309#endif
1310 msg_cinfo("on %s.\n", programmer_table[programmer].name);
Uwe Hermann9899cad2009-06-28 21:47:57 +00001311
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001312 /* Flash registers may more likely not be mapped if the chip was forced.
1313 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +00001314 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001315 if (flash->chip->printlock)
1316 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +00001317
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001318 /* Get out of the way for later runs. */
1319 unmap_flash(flash);
1320
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001321 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001322 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001323}
1324
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001325int read_buf_from_file(unsigned char *buf, unsigned long size,
1326 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001327{
Nico Huber7562f7d2013-08-30 21:29:45 +00001328#ifdef __LIBPAYLOAD__
1329 msg_gerr("Error: No file I/O support in libpayload\n");
1330 return 1;
1331#else
Stefan Tauner16687702015-12-25 21:59:45 +00001332 int ret = 0;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001333
Stefan Tauner16687702015-12-25 21:59:45 +00001334 FILE *image;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001335 if ((image = fopen(filename, "rb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001336 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001337 return 1;
1338 }
Stefan Tauner16687702015-12-25 21:59:45 +00001339
1340 struct stat image_stat;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001341 if (fstat(fileno(image), &image_stat) != 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001342 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
Stefan Tauner16687702015-12-25 21:59:45 +00001343 ret = 1;
1344 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001345 }
1346 if (image_stat.st_size != size) {
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +00001347 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 +00001348 (intmax_t)image_stat.st_size, size);
Stefan Tauner16687702015-12-25 21:59:45 +00001349 ret = 1;
1350 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001351 }
Stefan Tauner16687702015-12-25 21:59:45 +00001352
1353 unsigned long numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001354 if (numbytes != size) {
1355 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1356 "wanted %ld!\n", numbytes, size);
Stefan Tauner16687702015-12-25 21:59:45 +00001357 ret = 1;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001358 }
Stefan Tauner16687702015-12-25 21:59:45 +00001359out:
1360 (void)fclose(image);
1361 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001362#endif
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001363}
1364
Mark Marshallf20b7be2014-05-09 21:16:21 +00001365int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001366{
Nico Huber7562f7d2013-08-30 21:29:45 +00001367#ifdef __LIBPAYLOAD__
1368 msg_gerr("Error: No file I/O support in libpayload\n");
1369 return 1;
1370#else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001371 FILE *image;
Stefan Tauner16687702015-12-25 21:59:45 +00001372 int ret = 0;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001373
1374 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001375 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001376 return 1;
1377 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001378 if ((image = fopen(filename, "wb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001379 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001380 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001381 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001382
Stefan Tauner16687702015-12-25 21:59:45 +00001383 unsigned long numbytes = fwrite(buf, 1, size, image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001384 if (numbytes != size) {
Stefan Tauner16687702015-12-25 21:59:45 +00001385 msg_gerr("Error: file %s could not be written completely.\n", filename);
1386 ret = 1;
1387 goto out;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001388 }
Stefan Tauner16687702015-12-25 21:59:45 +00001389 if (fflush(image)) {
1390 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
1391 ret = 1;
1392 }
1393 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
1394#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
1395 struct stat image_stat;
1396 if (fstat(fileno(image), &image_stat) != 0) {
1397 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
1398 ret = 1;
1399 goto out;
1400 }
1401 if (S_ISREG(image_stat.st_mode)) {
1402 if (fsync(fileno(image))) {
1403 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
1404 ret = 1;
1405 }
1406 }
1407#endif
1408out:
1409 if (fclose(image)) {
1410 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
1411 ret = 1;
1412 }
1413 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001414#endif
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001415}
1416
Nico Huber899e4ec2016-04-29 18:39:01 +02001417static int read_by_layout(struct flashctx *, uint8_t *);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001418int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001419{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001420 unsigned long size = flash->chip->total_size * 1024;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001421 unsigned char *buf = calloc(size, sizeof(char));
1422 int ret = 0;
1423
1424 msg_cinfo("Reading flash... ");
1425 if (!buf) {
1426 msg_gerr("Memory allocation failed!\n");
1427 msg_cinfo("FAILED.\n");
1428 return 1;
1429 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001430 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001431 msg_cerr("No read function available for this flash chip.\n");
1432 ret = 1;
1433 goto out_free;
1434 }
Nico Huber899e4ec2016-04-29 18:39:01 +02001435 if (read_by_layout(flash, buf)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001436 msg_cerr("Read operation failed!\n");
1437 ret = 1;
1438 goto out_free;
1439 }
1440
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001441 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001442out_free:
1443 free(buf);
1444 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1445 return ret;
1446}
1447
Stefan Tauner96658be2014-05-26 22:05:31 +00001448/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001449static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001450{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001451 int i, j, k;
1452 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001453
1454 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1455 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001456 struct block_eraser eraser = chip->block_erasers[k];
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001457
1458 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1459 /* Blocks with zero size are bugs in flashchips.c. */
1460 if (eraser.eraseblocks[i].count &&
1461 !eraser.eraseblocks[i].size) {
1462 msg_gerr("ERROR: Flash chip %s erase function "
1463 "%i region %i has size 0. Please report"
1464 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001465 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001466 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001467 }
1468 /* Blocks with zero count are bugs in flashchips.c. */
1469 if (!eraser.eraseblocks[i].count &&
1470 eraser.eraseblocks[i].size) {
1471 msg_gerr("ERROR: Flash chip %s erase function "
1472 "%i region %i has count 0. Please report"
1473 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001474 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001475 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001476 }
1477 done += eraser.eraseblocks[i].count *
1478 eraser.eraseblocks[i].size;
1479 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001480 /* Empty eraseblock definition with erase function. */
1481 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001482 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001483 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001484 if (!done)
1485 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001486 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001487 msg_gerr("ERROR: Flash chip %s erase function %i "
1488 "region walking resulted in 0x%06x bytes total,"
1489 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001490 " flashrom@flashrom.org\n", chip->name, k,
1491 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001492 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001493 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001494 if (!eraser.block_erase)
1495 continue;
1496 /* Check if there are identical erase functions for different
1497 * layouts. That would imply "magic" erase functions. The
1498 * easiest way to check this is with function pointers.
1499 */
Uwe Hermann43959702010-03-13 17:28:29 +00001500 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001501 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001502 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001503 msg_gerr("ERROR: Flash chip %s erase function "
1504 "%i and %i are identical. Please report"
1505 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001506 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001507 ret = 1;
1508 }
Uwe Hermann43959702010-03-13 17:28:29 +00001509 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001510 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001511 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001512}
1513
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001514static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001515{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001516 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001517
1518 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1519 if (log)
1520 msg_cdbg("not defined. ");
1521 return 1;
1522 }
1523 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1524 if (log)
1525 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001526 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001527 return 1;
1528 }
1529 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1530 if (log)
1531 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001532 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001533 return 1;
1534 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001535 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001536 return 0;
1537}
1538
Nico Huber7af0e792016-04-29 16:40:15 +02001539/**
1540 * @brief Reads the included layout regions into a buffer.
1541 *
1542 * If there is no layout set in the given flash context, the whole chip will
1543 * be read.
1544 *
1545 * @param flashctx Flash context to be used.
1546 * @param buffer Buffer of full chip size to read into.
1547 * @return 0 on success,
1548 * 1 if any read fails.
1549 */
1550static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1551{
1552 const struct flashrom_layout *const layout = get_layout(flashctx);
1553
1554 size_t i;
1555 for (i = 0; i < layout->num_entries; ++i) {
1556 if (!layout->entries[i].included)
1557 continue;
1558
1559 const chipoff_t region_start = layout->entries[i].start;
1560 const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1;
1561
1562 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1563 return 1;
1564 }
1565 return 0;
1566}
1567
1568typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1569/**
1570 * @private
1571 *
1572 * For read-erase-write, `curcontents` and `newcontents` shall point
1573 * to buffers of the chip's size. Both are supposed to be prefilled
1574 * with at least the included layout regions of the current flash
1575 * contents (`curcontents`) and the data to be written to the flash
1576 * (`newcontents`).
1577 *
1578 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1579 *
1580 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1581 */
1582struct walk_info {
1583 uint8_t *curcontents;
1584 const uint8_t *newcontents;
1585 chipoff_t region_start;
1586 chipoff_t region_end;
1587 chipoff_t erase_start;
1588 chipoff_t erase_end;
1589};
1590/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1591typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1592
1593static int walk_eraseblocks(struct flashctx *const flashctx,
1594 struct walk_info *const info,
1595 const size_t erasefunction, const per_blockfn_t per_blockfn)
1596{
1597 int ret;
1598 size_t i, j;
1599 bool first = true;
1600 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1601
1602 info->erase_start = 0;
1603 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1604 /* count==0 for all automatically initialized array
1605 members so the loop below won't be executed for them. */
1606 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1607 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1608
1609 /* Skip any eraseblock that is completely outside the current region. */
1610 if (info->erase_end < info->region_start)
1611 continue;
1612 if (info->region_end < info->erase_start)
1613 break;
1614
1615 /* Print this for every block except the first one. */
1616 if (first)
1617 first = false;
1618 else
1619 msg_cdbg(", ");
1620 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1621
1622 ret = per_blockfn(flashctx, info, eraser->block_erase);
1623 if (ret)
1624 return ret;
1625 }
1626 if (info->region_end < info->erase_start)
1627 break;
1628 }
1629 msg_cdbg("\n");
1630 return 0;
1631}
1632
1633static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1634 const per_blockfn_t per_blockfn)
1635{
1636 const struct flashrom_layout *const layout = get_layout(flashctx);
1637
1638 all_skipped = true;
1639 msg_cinfo("Erasing and writing flash chip... ");
1640
1641 size_t i;
1642 for (i = 0; i < layout->num_entries; ++i) {
1643 if (!layout->entries[i].included)
1644 continue;
1645
1646 info->region_start = layout->entries[i].start;
1647 info->region_end = layout->entries[i].end;
1648
1649 size_t j;
1650 int error = 1; /* retry as long as it's 1 */
1651 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1652 if (j != 0)
1653 msg_cinfo("Looking for another erase function.\n");
1654 msg_cdbg("Trying erase function %zi... ", j);
1655 if (check_block_eraser(flashctx, j, 1))
1656 continue;
1657
1658 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1659 if (error != 1)
1660 break;
1661
1662 if (info->curcontents) {
1663 msg_cinfo("Reading current flash chip contents... ");
1664 if (read_by_layout(flashctx, info->curcontents)) {
1665 /* Now we are truly screwed. Read failed as well. */
1666 msg_cerr("Can't read anymore! Aborting.\n");
1667 /* We have no idea about the flash chip contents, so
1668 retrying with another erase function is pointless. */
1669 error = 2;
1670 break;
1671 }
1672 msg_cinfo("done. ");
1673 }
1674 }
1675 if (error == 1)
1676 msg_cinfo("No usable erase functions left.\n");
1677 if (error) {
1678 msg_cerr("FAILED!\n");
1679 return 1;
1680 }
1681 }
1682 if (all_skipped)
1683 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1684 msg_cinfo("Erase/write done.\n");
1685 return 0;
1686}
1687
1688static int erase_block(struct flashctx *const flashctx,
1689 const struct walk_info *const info, const erasefn_t erasefn)
1690{
1691 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
1692
1693 all_skipped = false;
1694
1695 msg_cdbg("E");
1696 if (erasefn(flashctx, info->erase_start, erase_len))
1697 return 1;
1698 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1699 msg_cerr("ERASE FAILED!\n");
1700 return 1;
1701 }
1702 return 0;
1703}
1704
1705/**
1706 * @brief Erases the included layout regions.
1707 *
1708 * If there is no layout set in the given flash context, the whole chip will
1709 * be erased.
1710 *
1711 * @param flashctx Flash context to be used.
1712 * @param buffer Buffer of full chip size to read into.
1713 * @return 0 on success,
1714 * 1 if all available erase functions failed.
1715 */
Nico Huber454f6132012-12-10 13:34:10 +00001716static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001717{
1718 struct walk_info info = { 0 };
1719 return walk_by_layout(flashctx, &info, &erase_block);
1720}
1721
1722static int read_erase_write_block(struct flashctx *const flashctx,
1723 const struct walk_info *const info, const erasefn_t erasefn)
1724{
1725 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1726 const bool region_unaligned = info->region_start > info->erase_start ||
1727 info->erase_end > info->region_end;
1728 const uint8_t *newcontents = NULL;
1729 int ret = 2;
1730
1731 /*
1732 * If the region is not erase-block aligned, merge current flash con-
1733 * tents into `info->curcontents` and a new buffer `newc`. The former
1734 * is necessary since we have no guarantee that the full erase block
1735 * was already read into `info->curcontents`. For the latter a new
1736 * buffer is used since `info->newcontents` might contain data for
1737 * other unaligned regions that touch this erase block too.
1738 */
1739 if (region_unaligned) {
1740 msg_cdbg("R");
1741 uint8_t *const newc = malloc(erase_len);
1742 if (!newc) {
1743 msg_cerr("Out of memory!\n");
1744 return 1;
1745 }
1746 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1747
1748 /* Merge data preceding the current region. */
1749 if (info->region_start > info->erase_start) {
1750 const chipoff_t start = info->erase_start;
1751 const chipsize_t len = info->region_start - info->erase_start;
1752 if (flashctx->chip->read(flashctx, newc, start, len)) {
1753 msg_cerr("Can't read! Aborting.\n");
1754 goto _free_ret;
1755 }
1756 memcpy(info->curcontents + start, newc, len);
1757 }
1758 /* Merge data following the current region. */
1759 if (info->erase_end > info->region_end) {
1760 const chipoff_t start = info->region_end + 1;
1761 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1762 const chipsize_t len = info->erase_end - info->region_end;
1763 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1764 msg_cerr("Can't read! Aborting.\n");
1765 goto _free_ret;
1766 }
1767 memcpy(info->curcontents + start, newc + rel_start, len);
1768 }
1769
1770 newcontents = newc;
1771 } else {
1772 newcontents = info->newcontents + info->erase_start;
1773 }
1774
1775 ret = 1;
1776 bool skipped = true;
1777 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001778 const uint8_t erased_value = ERASED_VALUE(flashctx);
1779 if (need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001780 if (erase_block(flashctx, info, erasefn))
1781 goto _free_ret;
1782 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001783 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001784 skipped = false;
1785 }
1786
1787 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1788 /* get_next_write() sets starthere to a new value after the call. */
1789 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1790 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1791 if (!writecount++)
1792 msg_cdbg("W");
1793 /* Needs the partial write function signature. */
1794 if (flashctx->chip->write(flashctx, newcontents + starthere,
1795 info->erase_start + starthere, lenhere))
1796 goto _free_ret;
1797 starthere += lenhere;
1798 skipped = false;
1799 }
1800 if (skipped)
1801 msg_cdbg("S");
1802 else
1803 all_skipped = false;
1804
1805 /* Update curcontents, other regions with overlapping erase blocks
1806 might rely on this. */
1807 memcpy(curcontents, newcontents, erase_len);
1808 ret = 0;
1809
1810_free_ret:
1811 if (region_unaligned)
1812 free((void *)newcontents);
1813 return ret;
1814}
1815
1816/**
1817 * @brief Writes the included layout regions from a given image.
1818 *
1819 * If there is no layout set in the given flash context, the whole image
1820 * will be written.
1821 *
1822 * @param flashctx Flash context to be used.
1823 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1824 * @param newcontents The new image to be written.
1825 * @return 0 on success,
1826 * 1 if anything has gone wrong.
1827 */
Nico Huber454f6132012-12-10 13:34:10 +00001828static int write_by_layout(struct flashctx *const flashctx,
1829 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001830{
1831 struct walk_info info;
1832 info.curcontents = curcontents;
1833 info.newcontents = newcontents;
1834 return walk_by_layout(flashctx, &info, read_erase_write_block);
1835}
1836
1837/**
1838 * @brief Compares the included layout regions with content from a buffer.
1839 *
1840 * If there is no layout set in the given flash context, the whole chip's
1841 * contents will be compared.
1842 *
1843 * @param flashctx Flash context to be used.
1844 * @param curcontents A buffer of full chip size to read current chip contents into.
1845 * @param newcontents The new image to compare to.
1846 * @return 0 on success,
1847 * 1 if reading failed,
1848 * 3 if the contents don't match.
1849 */
Nico Huber454f6132012-12-10 13:34:10 +00001850static int verify_by_layout(struct flashctx *const flashctx,
1851 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001852{
1853 const struct flashrom_layout *const layout = get_layout(flashctx);
1854
1855 size_t i;
1856 for (i = 0; i < layout->num_entries; ++i) {
1857 if (!layout->entries[i].included)
1858 continue;
1859
1860 const chipoff_t region_start = layout->entries[i].start;
1861 const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1;
1862
1863 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1864 return 1;
1865 if (compare_range(newcontents + region_start, curcontents + region_start,
1866 region_start, region_len))
1867 return 3;
1868 }
1869 return 0;
1870}
1871
Stefan Tauner136388f2013-07-15 10:47:53 +00001872static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001873{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001874 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001875#if CONFIG_INTERNAL == 1
1876 if (programmer == PROGRAMMER_INTERNAL)
1877 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
1878 "chip. Please report this on IRC at chat.freenode.net (channel #flashrom) or\n"
1879 "mail flashrom@flashrom.org, thanks!\n"
1880 "-------------------------------------------------------------------------------\n"
1881 "You may now reboot or simply leave the machine running.\n");
1882 else
1883#endif
1884 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1885 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
1886 "please report this on IRC at chat.freenode.net (channel #flashrom) or\n"
1887 "mail flashrom@flashrom.org, thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001888}
1889
Stefan Tauner136388f2013-07-15 10:47:53 +00001890static void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001891{
Stefan Tauner136388f2013-07-15 10:47:53 +00001892 msg_gerr("Your flash chip is in an unknown state.\n");
1893#if CONFIG_INTERNAL == 1
1894 if (programmer == PROGRAMMER_INTERNAL)
1895 msg_gerr("Get help on IRC at chat.freenode.net (channel #flashrom) or\n"
1896 "mail flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!\n"
1897 "-------------------------------------------------------------------------------\n"
1898 "DO NOT REBOOT OR POWEROFF!\n");
1899 else
1900#endif
1901 msg_gerr("Please report this on IRC at chat.freenode.net (channel #flashrom) or\n"
1902 "mail flashrom@flashrom.org, thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001903}
1904
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001905/* The way to go if you want a delimited list of programmers */
Stefan Tauner66652442011-06-26 17:38:17 +00001906void list_programmers(const char *delim)
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001907{
1908 enum programmer p;
1909 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001910 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001911 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001912 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001913 }
Stefan Tauner23e10b82016-01-23 16:16:49 +00001914 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001915}
1916
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001917void list_programmers_linebreak(int startcol, int cols, int paren)
1918{
1919 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001920 int pnamelen;
1921 int remaining = 0, firstline = 1;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001922 enum programmer p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001923 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001924
1925 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1926 pname = programmer_table[p].name;
1927 pnamelen = strlen(pname);
1928 if (remaining - pnamelen - 2 < 0) {
1929 if (firstline)
1930 firstline = 0;
1931 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001932 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001933 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001934 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001935 remaining = cols - startcol;
1936 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001937 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001938 remaining--;
1939 }
1940 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001941 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001942 remaining--;
1943 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001944 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001945 remaining -= pnamelen;
1946 if (p < PROGRAMMER_INVALID - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001947 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001948 remaining--;
1949 } else {
1950 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001951 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001952 }
1953 }
1954}
1955
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001956void print_sysinfo(void)
1957{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001958#if IS_WINDOWS
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001959 SYSTEM_INFO si;
1960 OSVERSIONINFOEX osvi;
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001961
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001962 memset(&si, 0, sizeof(SYSTEM_INFO));
1963 memset(&osvi, 0, sizeof(OSVERSIONINFOEX));
1964 msg_ginfo(" on Windows");
1965 /* Tell Windows which version of the structure we want. */
1966 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1967 if (GetVersionEx((OSVERSIONINFO*) &osvi))
1968 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
1969 else
1970 msg_ginfo(" unknown version");
1971 GetSystemInfo(&si);
1972 switch (si.wProcessorArchitecture) {
1973 case PROCESSOR_ARCHITECTURE_AMD64:
1974 msg_ginfo(" (x86_64)");
1975 break;
1976 case PROCESSOR_ARCHITECTURE_INTEL:
1977 msg_ginfo(" (x86)");
1978 break;
1979 default:
1980 msg_ginfo(" (unknown arch)");
1981 break;
1982 }
1983#elif HAVE_UTSNAME == 1
1984 struct utsname osinfo;
1985
1986 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001987 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1988 osinfo.machine);
1989#else
1990 msg_ginfo(" on unknown machine");
1991#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001992}
1993
1994void print_buildinfo(void)
1995{
1996 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001997#if NEED_PCI == 1
1998#ifdef PCILIB_VERSION
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001999 msg_gdbg(" libpci %s,", PCILIB_VERSION);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002000#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002001 msg_gdbg(" unknown PCI library,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002002#endif
2003#endif
2004#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002005 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00002006#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002007 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00002008#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002009 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00002010#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002011#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002012 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002013#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002014 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002015#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002016 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002017#endif
2018#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002019 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00002020#endif
2021#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002022 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00002023#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002024 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00002025#else
2026#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002027#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002028 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002029}
2030
Bernhard Walle201bde32008-01-21 15:24:22 +00002031void print_version(void)
2032{
Stefan Tauner76347082016-11-27 17:45:49 +01002033 msg_ginfo("flashrom %s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002034 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002035 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00002036}
2037
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00002038void print_banner(void)
2039{
2040 msg_ginfo("flashrom is free software, get the source code at "
Stefan Tauner4c723152016-01-14 22:47:55 +00002041 "https://flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00002042 msg_ginfo("\n");
2043}
2044
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002045int selfcheck(void)
2046{
Stefan Tauner96658be2014-05-26 22:05:31 +00002047 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00002048 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00002049
2050 /* Safety check. Instead of aborting after the first error, check
2051 * if more errors exist.
2052 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002053 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00002054 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00002055 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002056 }
Stefan Taunera6d96482012-12-26 19:51:23 +00002057 for (i = 0; i < PROGRAMMER_INVALID; i++) {
2058 const struct programmer_entry p = programmer_table[i];
2059 if (p.name == NULL) {
2060 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
2061 ret = 1;
2062 /* This might hide other problems with this programmer, but allows for better error
2063 * messages below without jumping through hoops. */
2064 continue;
2065 }
Stefan Tauneraf358d62012-12-27 18:40:26 +00002066 switch (p.type) {
2067 case USB:
2068 case PCI:
2069 case OTHER:
2070 if (p.devs.note == NULL) {
2071 if (strcmp("internal", p.name) == 0)
2072 break; /* This one has its device list stored separately. */
2073 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
2074 p.name);
2075 ret = 1;
2076 }
2077 break;
2078 default:
2079 msg_gerr("Programmer %s does not have a valid type set!\n", p.name);
2080 ret = 1;
2081 break;
2082 }
Stefan Taunera6d96482012-12-26 19:51:23 +00002083 if (p.init == NULL) {
2084 msg_gerr("Programmer %s does not have a valid init function!\n", p.name);
2085 ret = 1;
2086 }
2087 if (p.delay == NULL) {
2088 msg_gerr("Programmer %s does not have a valid delay function!\n", p.name);
2089 ret = 1;
2090 }
2091 if (p.map_flash_region == NULL) {
2092 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p.name);
2093 ret = 1;
2094 }
2095 if (p.unmap_flash_region == NULL) {
2096 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p.name);
2097 ret = 1;
2098 }
2099 }
Stefan Tauner96658be2014-05-26 22:05:31 +00002100
2101 /* It would be favorable if we could check for the correct layout (especially termination) of various
2102 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
2103 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
2104 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
2105 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
2106 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00002107 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00002108 msg_gerr("Flashchips table miscompilation!\n");
2109 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00002110 } else {
2111 for (i = 0; i < flashchips_size - 1; i++) {
2112 const struct flashchip *chip = &flashchips[i];
2113 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
2114 ret = 1;
2115 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
2116 "Please report a bug at flashrom@flashrom.org\n", i,
2117 chip->name == NULL ? "unnamed" : chip->name);
2118 }
2119 if (selfcheck_eraseblocks(chip)) {
2120 ret = 1;
2121 }
2122 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00002123 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00002124
Stefan Tauner600576b2014-06-12 22:57:36 +00002125#if CONFIG_INTERNAL == 1
2126 ret |= selfcheck_board_enables();
2127#endif
2128
Stefan Tauner96658be2014-05-26 22:05:31 +00002129 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00002130 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002131}
2132
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002133/* FIXME: This function signature needs to be improved once doit() has a better
2134 * function signature.
2135 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002136int chip_safety_check(const struct flashctx *flash, int force, int read_it, int write_it, int erase_it,
2137 int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002138{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002139 const struct flashchip *chip = flash->chip;
2140
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002141 if (!programmer_may_write && (write_it || erase_it)) {
2142 msg_perr("Write/erase is not working yet on your programmer in "
2143 "its current configuration.\n");
2144 /* --force is the wrong approach, but it's the best we can do
2145 * until the generic programmer parameter parser is merged.
2146 */
2147 if (!force)
2148 return 1;
2149 msg_cerr("Continuing anyway.\n");
2150 }
2151
2152 if (read_it || erase_it || write_it || verify_it) {
2153 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00002154 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002155 msg_cerr("Read is not working on this chip. ");
2156 if (!force)
2157 return 1;
2158 msg_cerr("Continuing anyway.\n");
2159 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002160 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002161 msg_cerr("flashrom has no read function for this "
2162 "flash chip.\n");
2163 return 1;
2164 }
2165 }
2166 if (erase_it || write_it) {
2167 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00002168 if (chip->tested.erase == NA) {
2169 msg_cerr("Erase is not possible on this chip.\n");
2170 return 1;
2171 }
2172 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002173 msg_cerr("Erase is not working on this chip. ");
2174 if (!force)
2175 return 1;
2176 msg_cerr("Continuing anyway.\n");
2177 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00002178 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00002179 msg_cerr("flashrom has no erase function for this "
2180 "flash chip.\n");
2181 return 1;
2182 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002183 }
2184 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00002185 if (chip->tested.write == NA) {
2186 msg_cerr("Write is not possible on this chip.\n");
2187 return 1;
2188 }
2189 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002190 msg_cerr("Write is not working on this chip. ");
2191 if (!force)
2192 return 1;
2193 msg_cerr("Continuing anyway.\n");
2194 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002195 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002196 msg_cerr("flashrom has no write function for this "
2197 "flash chip.\n");
2198 return 1;
2199 }
2200 }
2201 return 0;
2202}
2203
Nico Huber305f4172013-06-14 11:55:26 +02002204int prepare_flash_access(struct flashctx *const flash,
2205 const bool read_it, const bool write_it,
2206 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00002207{
2208 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
2209 msg_cerr("Aborting.\n");
2210 return 1;
2211 }
2212
2213 if (flash->layout == get_global_layout() && normalize_romentries(flash)) {
2214 msg_cerr("Requested regions can not be handled. Aborting.\n");
2215 return 1;
2216 }
2217
2218 if (map_flash(flash) != 0)
2219 return 1;
2220
2221 /* Given the existence of read locks, we want to unlock for read,
2222 erase and write. */
2223 if (flash->chip->unlock)
2224 flash->chip->unlock(flash);
2225
Nico Huberf43c6542017-10-14 17:47:28 +02002226 flash->address_high_byte = -1;
2227 flash->in_4ba_mode = false;
2228
Ed Swierkcc20a9b2017-07-03 13:17:18 -07002229 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huberfe34d2a2017-11-10 21:10:20 +01002230 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN)) {
2231 int ret;
2232 if (spi_master_4ba(flash))
2233 ret = spi_enter_4ba(flash);
2234 else
2235 ret = spi_exit_4ba(flash);
2236 if (ret) {
2237 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07002238 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02002239 }
Boris Baykov99127182016-06-11 18:29:00 +02002240 }
2241
Nico Huber454f6132012-12-10 13:34:10 +00002242 return 0;
2243}
2244
Nico Huber305f4172013-06-14 11:55:26 +02002245void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00002246{
2247 unmap_flash(flash);
2248}
2249
2250/**
2251 * @addtogroup flashrom-flash
2252 * @{
2253 */
2254
2255/**
2256 * @brief Erase the specified ROM chip.
2257 *
2258 * If a layout is set in the given flash context, only included regions
2259 * will be erased.
2260 *
2261 * @param flashctx The context of the flash chip to erase.
2262 * @return 0 on success.
2263 */
2264int flashrom_flash_erase(struct flashctx *const flashctx)
2265{
2266 if (prepare_flash_access(flashctx, false, false, true, false))
2267 return 1;
2268
2269 const int ret = erase_by_layout(flashctx);
2270
2271 finalize_flash_access(flashctx);
2272
2273 return ret;
2274}
2275
2276/** @} */ /* end flashrom-flash */
2277
2278/**
2279 * @defgroup flashrom-ops Operations
2280 * @{
2281 */
2282
2283/**
2284 * @brief Read the current image from the specified ROM chip.
2285 *
2286 * If a layout is set in the specified flash context, only included regions
2287 * will be read.
2288 *
2289 * @param flashctx The context of the flash chip.
2290 * @param buffer Target buffer to write image to.
2291 * @param buffer_len Size of target buffer in bytes.
2292 * @return 0 on success,
2293 * 2 if buffer_len is too short for the flash chip's contents,
2294 * or 1 on any other failure.
2295 */
2296int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
2297{
2298 const size_t flash_size = flashctx->chip->total_size * 1024;
2299
2300 if (flash_size > buffer_len)
2301 return 2;
2302
2303 if (prepare_flash_access(flashctx, true, false, false, false))
2304 return 1;
2305
2306 msg_cinfo("Reading flash... ");
2307
2308 int ret = 1;
2309 if (read_by_layout(flashctx, buffer)) {
2310 msg_cerr("Read operation failed!\n");
2311 msg_cinfo("FAILED.\n");
2312 goto _finalize_ret;
2313 }
2314 msg_cinfo("done.\n");
2315 ret = 0;
2316
2317_finalize_ret:
2318 finalize_flash_access(flashctx);
2319 return ret;
2320}
2321
2322static void combine_image_by_layout(const struct flashctx *const flashctx,
2323 uint8_t *const newcontents, const uint8_t *const oldcontents)
2324{
2325 const struct flashrom_layout *const layout = get_layout(flashctx);
2326
2327 size_t i;
2328 for (i = 0; i < layout->num_entries; ++i) {
2329 if (layout->entries[i].included)
2330 continue;
2331
2332 const chipoff_t region_start = layout->entries[i].start;
2333 const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1;
2334
2335 memcpy(newcontents + region_start, oldcontents + region_start, region_len);
2336 }
2337}
2338
2339/**
2340 * @brief Write the specified image to the ROM chip.
2341 *
2342 * If a layout is set in the specified flash context, only erase blocks
2343 * containing included regions will be touched.
2344 *
2345 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02002346 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00002347 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002348 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00002349 * @return 0 on success,
2350 * 4 if buffer_len doesn't match the size of the flash chip,
2351 * 3 if write was tried but nothing has changed,
2352 * 2 if write failed and flash contents changed,
2353 * or 1 on any other failure.
2354 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002355int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
2356 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00002357{
2358 const size_t flash_size = flashctx->chip->total_size * 1024;
2359 const bool verify_all = flashctx->flags.verify_whole_chip;
2360 const bool verify = flashctx->flags.verify_after_write;
2361
2362 if (buffer_len != flash_size)
2363 return 4;
2364
2365 int ret = 1;
2366
2367 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002368 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00002369 uint8_t *const curcontents = malloc(flash_size);
2370 uint8_t *oldcontents = NULL;
2371 if (verify_all)
2372 oldcontents = malloc(flash_size);
2373 if (!curcontents || (verify_all && !oldcontents)) {
2374 msg_gerr("Out of memory!\n");
2375 goto _free_ret;
2376 }
2377
2378#if CONFIG_INTERNAL == 1
2379 if (programmer == PROGRAMMER_INTERNAL && cb_check_image(newcontents, flash_size) < 0) {
2380 if (flashctx->flags.force_boardmismatch) {
2381 msg_pinfo("Proceeding anyway because user forced us to.\n");
2382 } else {
2383 msg_perr("Aborting. You can override this with "
2384 "-p internal:boardmismatch=force.\n");
2385 goto _free_ret;
2386 }
2387 }
2388#endif
2389
2390 if (prepare_flash_access(flashctx, false, true, false, verify))
2391 goto _free_ret;
2392
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002393 /* If given, assume flash chip contains same data as `refcontents`. */
2394 if (refcontents) {
2395 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
2396 memcpy(curcontents, refcontents, flash_size);
2397 if (oldcontents)
2398 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00002399 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002400 /*
2401 * Read the whole chip to be able to check whether regions need to be
2402 * erased and to give better diagnostics in case write fails.
2403 * The alternative is to read only the regions which are to be
2404 * preserved, but in that case we might perform unneeded erase which
2405 * takes time as well.
2406 */
2407 msg_cinfo("Reading old flash chip contents... ");
2408 if (verify_all) {
2409 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
2410 msg_cinfo("FAILED.\n");
2411 goto _finalize_ret;
2412 }
2413 memcpy(curcontents, oldcontents, flash_size);
2414 } else {
2415 if (read_by_layout(flashctx, curcontents)) {
2416 msg_cinfo("FAILED.\n");
2417 goto _finalize_ret;
2418 }
Nico Huber454f6132012-12-10 13:34:10 +00002419 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002420 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00002421 }
Nico Huber454f6132012-12-10 13:34:10 +00002422
2423 if (write_by_layout(flashctx, curcontents, newcontents)) {
2424 msg_cerr("Uh oh. Erase/write failed. ");
2425 ret = 2;
2426 if (verify_all) {
2427 msg_cerr("Checking if anything has changed.\n");
2428 msg_cinfo("Reading current flash chip contents... ");
2429 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
2430 msg_cinfo("done.\n");
2431 if (!memcmp(oldcontents, curcontents, flash_size)) {
2432 nonfatal_help_message();
2433 goto _finalize_ret;
2434 }
2435 msg_cerr("Apparently at least some data has changed.\n");
2436 } else
2437 msg_cerr("Can't even read anymore!\n");
2438 emergency_help_message();
2439 goto _finalize_ret;
2440 } else {
2441 msg_cerr("\n");
2442 }
2443 emergency_help_message();
2444 goto _finalize_ret;
2445 }
2446
2447 /* Verify only if we actually changed something. */
2448 if (verify && !all_skipped) {
2449 const struct flashrom_layout *const layout_bak = flashctx->layout;
2450
2451 msg_cinfo("Verifying flash... ");
2452
2453 /* Work around chips which need some time to calm down. */
2454 programmer_delay(1000*1000);
2455
2456 if (verify_all) {
2457 combine_image_by_layout(flashctx, newcontents, oldcontents);
2458 flashctx->layout = NULL;
2459 }
2460 ret = verify_by_layout(flashctx, curcontents, newcontents);
2461 flashctx->layout = layout_bak;
2462 /* If we tried to write, and verification now fails, we
2463 might have an emergency situation. */
2464 if (ret)
2465 emergency_help_message();
2466 else
2467 msg_cinfo("VERIFIED.\n");
2468 } else {
2469 /* We didn't change anything. */
2470 ret = 0;
2471 }
2472
2473_finalize_ret:
2474 finalize_flash_access(flashctx);
2475_free_ret:
2476 free(oldcontents);
2477 free(curcontents);
2478 return ret;
2479}
2480
2481/**
2482 * @brief Verify the ROM chip's contents with the specified image.
2483 *
2484 * If a layout is set in the specified flash context, only included regions
2485 * will be verified.
2486 *
2487 * @param flashctx The context of the flash chip.
2488 * @param buffer Source buffer to verify with.
2489 * @param buffer_len Size of source buffer in bytes.
2490 * @return 0 on success,
2491 * 3 if the chip's contents don't match,
2492 * 2 if buffer_len doesn't match the size of the flash chip,
2493 * or 1 on any other failure.
2494 */
2495int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
2496{
2497 const size_t flash_size = flashctx->chip->total_size * 1024;
2498
2499 if (buffer_len != flash_size)
2500 return 2;
2501
2502 const uint8_t *const newcontents = buffer;
2503 uint8_t *const curcontents = malloc(flash_size);
2504 if (!curcontents) {
2505 msg_gerr("Out of memory!\n");
2506 return 1;
2507 }
2508
2509 int ret = 1;
2510
2511 if (prepare_flash_access(flashctx, false, false, false, true))
2512 goto _free_ret;
2513
2514 msg_cinfo("Verifying flash... ");
2515 ret = verify_by_layout(flashctx, curcontents, newcontents);
2516 if (!ret)
2517 msg_cinfo("VERIFIED.\n");
2518
2519 finalize_flash_access(flashctx);
2520_free_ret:
2521 free(curcontents);
2522 return ret;
2523}
2524
2525/** @} */ /* end flashrom-ops */
Nico Huber899e4ec2016-04-29 18:39:01 +02002526
2527int do_read(struct flashctx *const flash, const char *const filename)
2528{
2529 if (prepare_flash_access(flash, true, false, false, false))
2530 return 1;
2531
2532 const int ret = read_flash_to_file(flash, filename);
2533
2534 finalize_flash_access(flash);
2535
2536 return ret;
2537}
2538
2539int do_erase(struct flashctx *const flash)
2540{
2541 const int ret = flashrom_flash_erase(flash);
2542
2543 /*
2544 * FIXME: Do we really want the scary warning if erase failed?
2545 * After all, after erase the chip is either blank or partially
2546 * blank or it has the old contents. A blank chip won't boot,
2547 * so if the user wanted erase and reboots afterwards, the user
2548 * knows very well that booting won't work.
2549 */
2550 if (ret)
2551 emergency_help_message();
2552
2553 return ret;
2554}
2555
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002556int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
Nico Huber899e4ec2016-04-29 18:39:01 +02002557{
2558 const size_t flash_size = flash->chip->total_size * 1024;
2559 int ret = 1;
2560
2561 uint8_t *const newcontents = malloc(flash_size);
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002562 uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
2563
2564 if (!newcontents || (referencefile && !refcontents)) {
Nico Huber899e4ec2016-04-29 18:39:01 +02002565 msg_gerr("Out of memory!\n");
2566 goto _free_ret;
2567 }
2568
2569 if (read_buf_from_file(newcontents, flash_size, filename))
2570 goto _free_ret;
2571
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002572 if (referencefile) {
2573 if (read_buf_from_file(refcontents, flash_size, referencefile))
2574 goto _free_ret;
2575 }
2576
2577 ret = flashrom_image_write(flash, newcontents, flash_size, refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002578
2579_free_ret:
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002580 free(refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002581 free(newcontents);
2582 return ret;
2583}
2584
2585int do_verify(struct flashctx *const flash, const char *const filename)
2586{
2587 const size_t flash_size = flash->chip->total_size * 1024;
2588 int ret = 1;
2589
2590 uint8_t *const newcontents = malloc(flash_size);
2591 if (!newcontents) {
2592 msg_gerr("Out of memory!\n");
2593 goto _free_ret;
2594 }
2595
2596 if (read_buf_from_file(newcontents, flash_size, filename))
2597 goto _free_ret;
2598
2599 ret = flashrom_image_verify(flash, newcontents, flash_size);
2600
2601_free_ret:
2602 free(newcontents);
2603 return ret;
2604}