blob: 59a7531f1f429f0947c337bea7bfb8cc64d870f2 [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 */
21
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000022#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000023#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000024#ifndef __LIBPAYLOAD__
25#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000026#include <sys/stat.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000027#endif
Ronald G. Minnichceec4202003-07-25 04:37:41 +000028#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000029#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000030#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000031#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000032#include <ctype.h>
Ollie Lho184a4042005-11-26 21:55:36 +000033#include <getopt.h>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000037#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000038#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000039#include "programmer.h"
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +000040#include "hwaccess.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010041#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000042
Mathias Krausea60faab2011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000044const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000045
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +000046static enum programmer programmer = PROGRAMMER_INVALID;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000047static const char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000048
Uwe Hermann48ec1b12010-08-08 17:01:18 +000049/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000050 * Programmers supporting multiple buses can have differing size limits on
51 * each bus. Store the limits for each bus in a common struct.
52 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000053struct decode_sizes max_rom_decode;
54
55/* If nonzero, used as the start address of bottom-aligned flash. */
56unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000057
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000058/* Is writing allowed with this programmer? */
59int programmer_may_write;
60
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000061const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000062#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000063 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000064 .name = "internal",
Stefan Tauneraf358d62012-12-27 18:40:26 +000065 .type = OTHER,
66 .devs.note = NULL,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000067 .init = internal_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000068 .map_flash_region = physmap,
69 .unmap_flash_region = physunmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000070 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000071 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000072#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000073
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000074#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000075 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000076 .name = "dummy",
Stefan Tauneraf358d62012-12-27 18:40:26 +000077 .type = OTHER,
78 /* FIXME */
79 .devs.note = "Dummy device, does nothing and logs all accesses\n",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000080 .init = dummy_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000081 .map_flash_region = dummy_map,
82 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000083 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000084 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000085#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000086
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000087#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +000088 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000089 .name = "nic3com",
Stefan Tauneraf358d62012-12-27 18:40:26 +000090 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +000091 .devs.dev = nics_3com,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000092 .init = nic3com_init,
Uwe Hermannc6915932009-05-17 23:12:17 +000093 .map_flash_region = fallback_map,
94 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000095 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000096 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000097#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +000098
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000099#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000100 {
Carl-Daniel Hailfinger40446ee2011-03-07 01:08:09 +0000101 /* This programmer works for Realtek RTL8139 and SMC 1211. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000102 .name = "nicrealtek",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000103 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000104 .devs.dev = nics_realtek,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000105 .init = nicrealtek_init,
106 .map_flash_region = fallback_map,
107 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000108 .delay = internal_delay,
Joerg Fischer5665ef32010-05-21 21:54:07 +0000109 },
Joerg Fischer5665ef32010-05-21 21:54:07 +0000110#endif
111
Andrew Morganc29c2e72010-06-07 22:37:54 +0000112#if CONFIG_NICNATSEMI == 1
113 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000114 .name = "nicnatsemi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000115 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000116 .devs.dev = nics_natsemi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000117 .init = nicnatsemi_init,
118 .map_flash_region = fallback_map,
119 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000120 .delay = internal_delay,
Andrew Morganc29c2e72010-06-07 22:37:54 +0000121 },
122#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000123
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000124#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000125 {
126 .name = "gfxnvidia",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000127 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000128 .devs.dev = gfx_nvidia,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000129 .init = gfxnvidia_init,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000130 .map_flash_region = fallback_map,
131 .unmap_flash_region = fallback_unmap,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000132 .delay = internal_delay,
133 },
134#endif
135
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000136#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000137 {
TURBO Jb0912c02009-09-02 23:00:46 +0000138 .name = "drkaiser",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000139 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000140 .devs.dev = drkaiser_pcidev,
TURBO Jb0912c02009-09-02 23:00:46 +0000141 .init = drkaiser_init,
TURBO Jb0912c02009-09-02 23:00:46 +0000142 .map_flash_region = fallback_map,
143 .unmap_flash_region = fallback_unmap,
TURBO Jb0912c02009-09-02 23:00:46 +0000144 .delay = internal_delay,
145 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000146#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000147
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000148#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000149 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000150 .name = "satasii",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000151 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000152 .devs.dev = satas_sii,
Rudolf Marek68720c72009-05-17 19:39:27 +0000153 .init = satasii_init,
Uwe Hermannc6915932009-05-17 23:12:17 +0000154 .map_flash_region = fallback_map,
155 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000156 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000157 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000158#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000159
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000160#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000161 {
162 .name = "atahpt",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000163 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000164 .devs.dev = ata_hpt,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000165 .init = atahpt_init,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000166 .map_flash_region = fallback_map,
167 .unmap_flash_region = fallback_unmap,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000168 .delay = internal_delay,
169 },
170#endif
171
Jonathan Kollasch7f0f3fa2014-06-01 10:26:23 +0000172#if CONFIG_ATAVIA == 1
173 {
174 .name = "atavia",
175 .type = PCI,
176 .devs.dev = ata_via,
177 .init = atavia_init,
178 .map_flash_region = atavia_map,
179 .unmap_flash_region = fallback_unmap,
180 .delay = internal_delay,
181 },
182#endif
183
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +0000184#if CONFIG_ATAPROMISE == 1
185 {
186 .name = "atapromise",
187 .type = PCI,
188 .devs.dev = ata_promise,
189 .init = atapromise_init,
190 .map_flash_region = atapromise_map,
191 .unmap_flash_region = fallback_unmap,
192 .delay = internal_delay,
193 },
194#endif
195
Kyösti Mälkki72d42f82014-06-01 23:48:31 +0000196#if CONFIG_IT8212 == 1
197 {
198 .name = "it8212",
199 .type = PCI,
200 .devs.dev = devs_it8212,
201 .init = it8212_init,
202 .map_flash_region = fallback_map,
203 .unmap_flash_region = fallback_unmap,
204 .delay = internal_delay,
205 },
206#endif
207
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000208#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000209 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000210 .name = "ft2232_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000211 .type = USB,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000212 .devs.dev = devs_ft2232spi,
Paul Fox05dfbe62009-06-16 21:08:06 +0000213 .init = ft2232_spi_init,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000214 .map_flash_region = fallback_map,
215 .unmap_flash_region = fallback_unmap,
Paul Fox05dfbe62009-06-16 21:08:06 +0000216 .delay = internal_delay,
217 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000218#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000219
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000220#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000221 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000222 .name = "serprog",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000223 .type = OTHER,
224 /* FIXME */
225 .devs.note = "All programmer devices speaking the serprog protocol\n",
Urja Rannikko22915352009-06-23 11:33:43 +0000226 .init = serprog_init,
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000227 .map_flash_region = serprog_map,
Urja Rannikko22915352009-06-23 11:33:43 +0000228 .unmap_flash_region = fallback_unmap,
Urja Rannikko22915352009-06-23 11:33:43 +0000229 .delay = serprog_delay,
230 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000231#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000232
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000233#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000234 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000235 .name = "buspirate_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000236 .type = OTHER,
237 /* FIXME */
238 .devs.note = "Dangerous Prototypes Bus Pirate\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000239 .init = buspirate_spi_init,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000240 .map_flash_region = fallback_map,
241 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000242 .delay = internal_delay,
243 },
244#endif
245
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000246#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000247 {
248 .name = "dediprog",
Stefan Taunerfdec7472016-02-22 08:59:27 +0000249 .type = USB,
250 .devs.dev = devs_dediprog,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000251 .init = dediprog_init,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000252 .map_flash_region = fallback_map,
253 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000254 .delay = internal_delay,
255 },
256#endif
257
Daniel Thompson45e91a22018-06-04 13:46:29 +0100258#if CONFIG_DEVELOPERBOX_SPI == 1
259 {
260 .name = "developerbox",
261 .type = USB,
262 .devs.dev = devs_developerbox_spi,
263 .init = developerbox_spi_init,
264 .map_flash_region = fallback_map,
265 .unmap_flash_region = fallback_unmap,
266 .delay = internal_delay,
267 },
268#endif
269
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000270#if CONFIG_RAYER_SPI == 1
271 {
272 .name = "rayer_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000273 .type = OTHER,
274 /* FIXME */
275 .devs.note = "RayeR parallel port programmer\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000276 .init = rayer_spi_init,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000277 .map_flash_region = fallback_map,
278 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000279 .delay = internal_delay,
280 },
281#endif
282
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000283#if CONFIG_PONY_SPI == 1
284 {
285 .name = "pony_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000286 .type = OTHER,
287 /* FIXME */
288 .devs.note = "Programmers compatible with SI-Prog, serbang or AJAWe\n",
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000289 .init = pony_spi_init,
290 .map_flash_region = fallback_map,
291 .unmap_flash_region = fallback_unmap,
292 .delay = internal_delay,
Stefan Tauneraf358d62012-12-27 18:40:26 +0000293 },
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000294#endif
295
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000296#if CONFIG_NICINTEL == 1
297 {
298 .name = "nicintel",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000299 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000300 .devs.dev = nics_intel,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000301 .init = nicintel_init,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000302 .map_flash_region = fallback_map,
303 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000304 .delay = internal_delay,
305 },
306#endif
307
Idwer Vollering004f4b72010-09-03 18:21:21 +0000308#if CONFIG_NICINTEL_SPI == 1
309 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000310 .name = "nicintel_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000311 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000312 .devs.dev = nics_intel_spi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000313 .init = nicintel_spi_init,
314 .map_flash_region = fallback_map,
315 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000316 .delay = internal_delay,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000317 },
318#endif
319
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000320#if CONFIG_NICINTEL_EEPROM == 1
321 {
322 .name = "nicintel_eeprom",
323 .type = PCI,
324 .devs.dev = nics_intel_ee,
325 .init = nicintel_ee_init,
326 .map_flash_region = fallback_map,
327 .unmap_flash_region = fallback_unmap,
328 .delay = internal_delay,
329 },
330#endif
331
Mark Marshall90021f22010-12-03 14:48:11 +0000332#if CONFIG_OGP_SPI == 1
333 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000334 .name = "ogp_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000335 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000336 .devs.dev = ogp_spi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000337 .init = ogp_spi_init,
338 .map_flash_region = fallback_map,
339 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000340 .delay = internal_delay,
Mark Marshall90021f22010-12-03 14:48:11 +0000341 },
342#endif
343
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000344#if CONFIG_SATAMV == 1
345 {
346 .name = "satamv",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000347 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000348 .devs.dev = satas_mv,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000349 .init = satamv_init,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000350 .map_flash_region = fallback_map,
351 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000352 .delay = internal_delay,
353 },
354#endif
355
David Hendricksf9a30552015-05-23 20:30:30 -0700356#if CONFIG_LINUX_MTD == 1
357 {
358 .name = "linux_mtd",
359 .type = OTHER,
360 .devs.note = "Device files /dev/mtd*\n",
361 .init = linux_mtd_init,
362 .map_flash_region = fallback_map,
363 .unmap_flash_region = fallback_unmap,
364 .delay = internal_delay,
365 },
366#endif
367
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000368#if CONFIG_LINUX_SPI == 1
369 {
370 .name = "linux_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000371 .type = OTHER,
372 .devs.note = "Device files /dev/spidev*.*\n",
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000373 .init = linux_spi_init,
374 .map_flash_region = fallback_map,
375 .unmap_flash_region = fallback_unmap,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000376 .delay = internal_delay,
377 },
378#endif
379
James Lairdc60de0e2013-03-27 13:00:23 +0000380#if CONFIG_USBBLASTER_SPI == 1
381 {
382 .name = "usbblaster_spi",
383 .type = USB,
384 .devs.dev = devs_usbblasterspi,
385 .init = usbblaster_spi_init,
386 .map_flash_region = fallback_map,
387 .unmap_flash_region = fallback_unmap,
388 .delay = internal_delay,
389 },
390#endif
391
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000392#if CONFIG_MSTARDDC_SPI == 1
393 {
394 .name = "mstarddc_spi",
395 .type = OTHER,
396 .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n",
397 .init = mstarddc_spi_init,
398 .map_flash_region = fallback_map,
399 .unmap_flash_region = fallback_unmap,
400 .delay = internal_delay,
401 },
402#endif
403
Justin Chevrier66e554b2015-02-08 21:58:10 +0000404#if CONFIG_PICKIT2_SPI == 1
405 {
406 .name = "pickit2_spi",
Stefan Taunerf31fe842016-02-22 08:59:15 +0000407 .type = USB,
408 .devs.dev = devs_pickit2_spi,
Justin Chevrier66e554b2015-02-08 21:58:10 +0000409 .init = pickit2_spi_init,
410 .map_flash_region = fallback_map,
411 .unmap_flash_region = fallback_unmap,
412 .delay = internal_delay,
413 },
414#endif
415
Urja Rannikko0870b022016-01-31 22:10:29 +0000416#if CONFIG_CH341A_SPI == 1
417 {
418 .name = "ch341a_spi",
419 .type = USB,
420 .devs.dev = devs_ch341a_spi,
421 .init = ch341a_spi_init,
422 .map_flash_region = fallback_map,
423 .unmap_flash_region = fallback_unmap,
424 .delay = ch341a_spi_delay,
425 },
426#endif
427
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100428#if CONFIG_DIGILENT_SPI == 1
429 {
430 .name = "digilent_spi",
431 .type = USB,
432 .devs.dev = devs_digilent_spi,
433 .init = digilent_spi_init,
434 .map_flash_region = fallback_map,
435 .unmap_flash_region = fallback_unmap,
436 .delay = internal_delay,
437 },
438#endif
439
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000440 {0}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000441};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000442
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +0000443#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000444static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +0000445/** @private */
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000446struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +0000447 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000448 void *data;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000449} static shutdown_fn[SHUTDOWN_MAXFN];
450/* Initialize to 0 to make sure nobody registers a shutdown function before
451 * programmer init.
452 */
453static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000454
Stefan Taunerc4f44df2013-08-12 22:58:43 +0000455/* Did we change something or was every erase/write skipped (if any)? */
456static bool all_skipped = true;
457
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000458static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +0000459
Stefan Tauner2a1ed772014-08-31 00:09:21 +0000460int shutdown_free(void *data)
461{
462 free(data);
463 return 0;
464}
465
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000466/* Register a function to be executed on programmer shutdown.
467 * The advantage over atexit() is that you can supply a void pointer which will
468 * be used as parameter to the registered function upon programmer shutdown.
469 * This pointer can point to arbitrary data used by said function, e.g. undo
470 * information for GPIO settings etc. If unneeded, set data=NULL.
471 * Please note that the first (void *data) belongs to the function signature of
472 * the function passed as first parameter.
473 */
David Hendricks8bb20212011-06-14 01:35:36 +0000474int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000475{
476 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000477 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000478 SHUTDOWN_MAXFN);
479 return 1;
480 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000481 if (!may_register_shutdown) {
482 msg_perr("Tried to register a shutdown function before "
483 "programmer init.\n");
484 return 1;
485 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000486 shutdown_fn[shutdown_fn_count].func = function;
487 shutdown_fn[shutdown_fn_count].data = data;
488 shutdown_fn_count++;
489
490 return 0;
491}
492
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000493int programmer_init(enum programmer prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000494{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000495 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000496
497 if (prog >= PROGRAMMER_INVALID) {
498 msg_perr("Invalid programmer specified!\n");
499 return -1;
500 }
501 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000502 /* Initialize all programmer specific data. */
503 /* Default to unlimited decode sizes. */
504 max_rom_decode = (const struct decode_sizes) {
505 .parallel = 0xffffffff,
506 .lpc = 0xffffffff,
507 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000508 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000509 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000510 /* Default to top aligned flash at 4 GB. */
511 flashbase = 0;
512 /* Registering shutdown functions is now allowed. */
513 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000514 /* Default to allowing writes. Broken programmers set this to 0. */
515 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000516
517 programmer_param = param;
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000518 msg_pdbg("Initializing %s programmer\n", programmer_table[programmer].name);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000519 ret = programmer_table[programmer].init();
520 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000521 if (ret != 0) {
522 /* It is quite possible that any unhandled programmer parameter would have been valid,
523 * but an error in actual programmer init happened before the parameter was evaluated.
524 */
525 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
526 programmer_param);
527 } else {
528 /* Actual programmer init was successful, but the user specified an invalid or unusable
529 * (for the current programmer configuration) parameter.
530 */
531 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
532 msg_perr("Aborting.\n");
533 ret = ERROR_FATAL;
534 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000535 }
536 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000537}
538
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000539/** Calls registered shutdown functions and resets internal programmer-related variables.
540 * Calling it is safe even without previous initialization, but further interactions with programmer support
541 * require a call to programmer_init() (afterwards).
542 *
543 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000544int programmer_shutdown(void)
545{
David Hendricks8bb20212011-06-14 01:35:36 +0000546 int ret = 0;
547
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000548 /* Registering shutdown functions is no longer allowed. */
549 may_register_shutdown = 0;
550 while (shutdown_fn_count > 0) {
551 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000552 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000553 }
Stefan Taunere34e3e82013-01-01 00:06:51 +0000554
Stefan Taunerb8911d62012-12-26 07:55:00 +0000555 programmer_param = NULL;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000556 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000557
David Hendricks8bb20212011-06-14 01:35:36 +0000558 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000559}
560
Stefan Tauner305e0b92013-07-17 23:46:44 +0000561void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000562{
Stefan Tauner26e7a152013-09-13 17:21:05 +0000563 void *ret = programmer_table[programmer].map_flash_region(descr, phys_addr, len);
564 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
565 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
566 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000567}
568
569void programmer_unmap_flash_region(void *virt_addr, size_t len)
570{
571 programmer_table[programmer].unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000572 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000573}
574
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000575void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000576{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000577 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000578}
579
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000580void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000581{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000582 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000583}
584
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000585void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000586{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000587 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000588}
589
Mark Marshallf20b7be2014-05-09 21:16:21 +0000590void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000591{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000592 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000593}
594
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000595uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000596{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000597 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000598}
599
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000600uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000601{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000602 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000603}
604
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000605uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000606{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000607 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000608}
609
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000610void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
611 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000612{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000613 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000614}
615
Stefan Taunerf80419c2014-05-02 15:41:42 +0000616void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000617{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000618 if (usecs > 0)
619 programmer_table[programmer].delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000620}
621
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000622int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
623 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000624{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000625 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000626
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000627 return 0;
628}
629
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000630/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000631 * It will look for needle with a subsequent '=' in haystack, return a copy of
632 * needle and remove everything from the first occurrence of needle to the next
633 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000634 */
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000635char *extract_param(const char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000636{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000637 char *param_pos, *opt_pos, *rest;
638 char *opt = NULL;
639 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000640 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000641
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000642 needlelen = strlen(needle);
643 if (!needlelen) {
644 msg_gerr("%s: empty needle! Please report a bug at "
645 "flashrom@flashrom.org\n", __func__);
646 return NULL;
647 }
648 /* No programmer parameters given. */
649 if (*haystack == NULL)
650 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000651 param_pos = strstr(*haystack, needle);
652 do {
653 if (!param_pos)
654 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000655 /* Needle followed by '='? */
656 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000657 /* Beginning of the string? */
658 if (param_pos == *haystack)
659 break;
660 /* After a delimiter? */
661 if (strchr(delim, *(param_pos - 1)))
662 break;
663 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000664 /* Continue searching. */
665 param_pos++;
666 param_pos = strstr(param_pos, needle);
667 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000668
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000669 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000670 /* Get the string after needle and '='. */
671 opt_pos = param_pos + needlelen + 1;
672 optlen = strcspn(opt_pos, delim);
673 /* Return an empty string if the parameter was empty. */
674 opt = malloc(optlen + 1);
675 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000676 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000677 exit(1);
678 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000679 strncpy(opt, opt_pos, optlen);
680 opt[optlen] = '\0';
681 rest = opt_pos + optlen;
682 /* Skip all delimiters after the current parameter. */
683 rest += strspn(rest, delim);
684 memmove(param_pos, rest, strlen(rest) + 1);
685 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000686 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000687
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000688 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000689}
690
Stefan Tauner66652442011-06-26 17:38:17 +0000691char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000692{
693 return extract_param(&programmer_param, param_name, ",");
694}
695
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000696/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000697static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000698{
699 unsigned int usable_erasefunctions = 0;
700 int k;
701 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
702 if (!check_block_eraser(flash, k, 0))
703 usable_erasefunctions++;
704 }
705 return usable_erasefunctions;
706}
707
Mark Marshallf20b7be2014-05-09 21:16:21 +0000708static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000709{
710 int ret = 0, failcount = 0;
711 unsigned int i;
712 for (i = 0; i < len; i++) {
713 if (wantbuf[i] != havebuf[i]) {
714 /* Only print the first failure. */
715 if (!failcount++)
716 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
717 start + i, wantbuf[i], havebuf[i]);
718 }
719 }
720 if (failcount) {
721 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
722 start, start + len - 1, failcount);
723 ret = -1;
724 }
725 return ret;
726}
727
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000728/* start is an offset to the base address of the flash chip */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000729int check_erased_range(struct flashctx *flash, unsigned int start,
730 unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000731{
732 int ret;
733 uint8_t *cmpbuf = malloc(len);
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300734 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000735
736 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000737 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000738 exit(1);
739 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300740 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000741 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000742 free(cmpbuf);
743 return ret;
744}
745
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000746/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000747 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000748 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000749 * @start offset to the base address of the flash chip
750 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000751 * @return 0 for success, -1 for failure
752 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000753int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000754{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000755 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000756 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000757
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000758 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000759 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000760 return -1;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000761 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000762
763 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000764 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000765 msg_gerr("Could not allocate memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000766 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000767 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000768 int ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000769
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000770 if (start + len > flash->chip->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000771 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000772 " total_size 0x%x\n", __func__, start, len,
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000773 flash->chip->total_size * 1024);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000774 ret = -1;
775 goto out_free;
776 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000777
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000778 ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000779 if (ret) {
780 msg_gerr("Verification impossible because read failed "
781 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000782 ret = -1;
783 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000784 }
785
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000786 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000787out_free:
788 free(readbuf);
789 return ret;
790}
791
Stefan Tauner02437452013-04-01 19:34:53 +0000792/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300793static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
794 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000795{
796 unsigned int i, j, limit;
797 for (j = 0; j < len / gran; j++) {
798 limit = min (gran, len - j * gran);
799 /* Are 'have' and 'want' identical? */
800 if (!memcmp(have + j * gran, want + j * gran, limit))
801 continue;
802 /* have needs to be in erased state. */
803 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300804 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000805 return 1;
806 }
807 return 0;
808}
809
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000810/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000811 * Check if the buffer @have can be programmed to the content of @want without
812 * erasing. This is only possible if all chunks of size @gran are either kept
813 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000814 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000815 * Warning: This function assumes that @have and @want point to naturally
816 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000817 *
818 * @have buffer with current content
819 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000820 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000821 * @gran write granularity (enum, not count)
822 * @return 0 if no erase is needed, 1 otherwise
823 */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300824int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
825 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000826{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000827 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000828 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000829
830 switch (gran) {
831 case write_gran_1bit:
832 for (i = 0; i < len; i++)
833 if ((have[i] & want[i]) != want[i]) {
834 result = 1;
835 break;
836 }
837 break;
838 case write_gran_1byte:
839 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300840 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000841 result = 1;
842 break;
843 }
844 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000845 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300846 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000847 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000848 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300849 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000850 break;
851 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300852 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000853 break;
854 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300855 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000856 break;
857 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300858 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000859 break;
860 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300861 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000862 break;
863 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300864 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000865 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000866 case write_gran_1byte_implicit_erase:
867 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
868 result = 0;
869 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000870 default:
871 msg_cerr("%s: Unsupported granularity! Please report a bug at "
872 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000873 }
874 return result;
875}
876
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000877/**
878 * Check if the buffer @have needs to be programmed to get the content of @want.
879 * If yes, return 1 and fill in first_start with the start address of the
880 * write operation and first_len with the length of the first to-be-written
881 * chunk. If not, return 0 and leave first_start and first_len undefined.
882 *
883 * Warning: This function assumes that @have and @want point to naturally
884 * aligned regions.
885 *
886 * @have buffer with current content
887 * @want buffer with desired content
888 * @len length of the checked area
889 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000890 * @first_start offset of the first byte which needs to be written (passed in
891 * value is increased by the offset of the first needed write
892 * relative to have/want or unchanged if no write is needed)
893 * @return length of the first contiguous area which needs to be written
894 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000895 *
896 * FIXME: This function needs a parameter which tells it about coalescing
897 * in relation to the max write length of the programmer and the max write
898 * length of the chip.
899 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000900static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000901 unsigned int *first_start,
902 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000903{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000904 int need_write = 0;
905 unsigned int rel_start = 0, first_len = 0;
906 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000907
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000908 switch (gran) {
909 case write_gran_1bit:
910 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000911 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000912 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000913 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000914 case write_gran_128bytes:
915 stride = 128;
916 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000917 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000918 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000919 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000920 case write_gran_264bytes:
921 stride = 264;
922 break;
923 case write_gran_512bytes:
924 stride = 512;
925 break;
926 case write_gran_528bytes:
927 stride = 528;
928 break;
929 case write_gran_1024bytes:
930 stride = 1024;
931 break;
932 case write_gran_1056bytes:
933 stride = 1056;
934 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000935 default:
936 msg_cerr("%s: Unsupported granularity! Please report a bug at "
937 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000938 /* Claim that no write was needed. A write with unknown
939 * granularity is too dangerous to try.
940 */
941 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000942 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000943 for (i = 0; i < len / stride; i++) {
944 limit = min(stride, len - i * stride);
945 /* Are 'have' and 'want' identical? */
946 if (memcmp(have + i * stride, want + i * stride, limit)) {
947 if (!need_write) {
948 /* First location where have and want differ. */
949 need_write = 1;
950 rel_start = i * stride;
951 }
952 } else {
953 if (need_write) {
954 /* First location where have and want
955 * do not differ anymore.
956 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000957 break;
958 }
959 }
960 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000961 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000962 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000963 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000964 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000965}
966
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000967/* This function generates various test patterns useful for testing controller
968 * and chip communication as well as chip behaviour.
969 *
970 * If a byte can be written multiple times, each time keeping 0-bits at 0
971 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
972 * is essentially an AND operation. That's also the reason why this function
973 * provides the result of AND between various patterns.
974 *
975 * Below is a list of patterns (and their block length).
976 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
977 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
978 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
979 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
980 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
981 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
982 * Pattern 6 is 00 (1 Byte)
983 * Pattern 7 is ff (1 Byte)
984 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
985 * byte block.
986 *
987 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
988 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
989 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
990 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
991 * Pattern 12 is 00 (1 Byte)
992 * Pattern 13 is ff (1 Byte)
993 * Patterns 8-13 have no block number.
994 *
995 * Patterns 0-3 are created to detect and efficiently diagnose communication
996 * slips like missed bits or bytes and their repetitive nature gives good visual
997 * cues to the person inspecting the results. In addition, the following holds:
998 * AND Pattern 0/1 == Pattern 4
999 * AND Pattern 2/3 == Pattern 5
1000 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
1001 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
1002 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
1003 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
1004 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
1005 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
1006 * Besides that, they provide for bit testing of the last two bytes of every
1007 * 256 byte block which contains the block number for patterns 0-6.
1008 * Patterns 10-11 are special purpose for detecting subblock aliasing with
1009 * block sizes >256 bytes (some Dataflash chips etc.)
1010 * AND Pattern 8/9 == Pattern 12
1011 * AND Pattern 10/11 == Pattern 12
1012 * Pattern 13 is the completely erased state.
1013 * None of the patterns can detect aliasing at boundaries which are a multiple
1014 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
1015 */
1016int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
1017{
1018 int i;
1019
1020 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001021 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +00001022 return 1;
1023 }
1024
1025 switch (variant) {
1026 case 0:
1027 for (i = 0; i < size; i++)
1028 buf[i] = (i & 0xf) << 4 | 0x5;
1029 break;
1030 case 1:
1031 for (i = 0; i < size; i++)
1032 buf[i] = (i & 0xf) << 4 | 0xa;
1033 break;
1034 case 2:
1035 for (i = 0; i < size; i++)
1036 buf[i] = 0x50 | (i & 0xf);
1037 break;
1038 case 3:
1039 for (i = 0; i < size; i++)
1040 buf[i] = 0xa0 | (i & 0xf);
1041 break;
1042 case 4:
1043 for (i = 0; i < size; i++)
1044 buf[i] = (i & 0xf) << 4;
1045 break;
1046 case 5:
1047 for (i = 0; i < size; i++)
1048 buf[i] = i & 0xf;
1049 break;
1050 case 6:
1051 memset(buf, 0x00, size);
1052 break;
1053 case 7:
1054 memset(buf, 0xff, size);
1055 break;
1056 case 8:
1057 for (i = 0; i < size; i++)
1058 buf[i] = i & 0xff;
1059 break;
1060 case 9:
1061 for (i = 0; i < size; i++)
1062 buf[i] = ~(i & 0xff);
1063 break;
1064 case 10:
1065 for (i = 0; i < size % 2; i++) {
1066 buf[i * 2] = (i >> 8) & 0xff;
1067 buf[i * 2 + 1] = i & 0xff;
1068 }
1069 if (size & 0x1)
1070 buf[i * 2] = (i >> 8) & 0xff;
1071 break;
1072 case 11:
1073 for (i = 0; i < size % 2; i++) {
1074 buf[i * 2] = ~((i >> 8) & 0xff);
1075 buf[i * 2 + 1] = ~(i & 0xff);
1076 }
1077 if (size & 0x1)
1078 buf[i * 2] = ~((i >> 8) & 0xff);
1079 break;
1080 case 12:
1081 memset(buf, 0x00, size);
1082 break;
1083 case 13:
1084 memset(buf, 0xff, size);
1085 break;
1086 }
1087
1088 if ((variant >= 0) && (variant <= 7)) {
1089 /* Write block number in the last two bytes of each 256-byte
1090 * block, big endian for easier reading of the hexdump.
1091 * Note that this wraps around for chips larger than 2^24 bytes
1092 * (16 MB).
1093 */
1094 for (i = 0; i < size / 256; i++) {
1095 buf[i * 256 + 254] = (i >> 8) & 0xff;
1096 buf[i * 256 + 255] = i & 0xff;
1097 }
1098 }
1099
1100 return 0;
1101}
1102
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001103/* Returns the number of busses commonly supported by the current programmer and flash chip where the latter
1104 * can not be completely accessed due to size/address limits of the programmer. */
1105unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001106{
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001107 unsigned int limitexceeded = 0;
1108 uint32_t size = flash->chip->total_size * 1024;
1109 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001110
1111 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001112 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001113 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001114 "size %u kB of chipset/board/programmer "
1115 "for %s interface, "
1116 "probe/read/erase/write may fail. ", size / 1024,
1117 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001118 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001119 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001120 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001121 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001122 "size %u kB of chipset/board/programmer "
1123 "for %s interface, "
1124 "probe/read/erase/write may fail. ", size / 1024,
1125 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001126 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001127 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001128 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001129 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001130 "size %u kB of chipset/board/programmer "
1131 "for %s interface, "
1132 "probe/read/erase/write may fail. ", size / 1024,
1133 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001134 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001135 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001136 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001137 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001138 "size %u kB of chipset/board/programmer "
1139 "for %s interface, "
1140 "probe/read/erase/write may fail. ", size / 1024,
1141 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001142 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001143 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001144}
1145
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001146void unmap_flash(struct flashctx *flash)
1147{
1148 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
1149 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
1150 flash->physical_registers = 0;
1151 flash->virtual_registers = (chipaddr)ERROR_PTR;
1152 }
1153
1154 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
1155 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
1156 flash->physical_memory = 0;
1157 flash->virtual_memory = (chipaddr)ERROR_PTR;
1158 }
1159}
1160
1161int map_flash(struct flashctx *flash)
1162{
1163 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
1164 flash->virtual_memory = (chipaddr)ERROR_PTR;
1165 flash->virtual_registers = (chipaddr)ERROR_PTR;
1166
1167 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
1168 * These are used for various probing-related hacks that would not map successfully anyway and should be
1169 * removed ASAP. */
1170 if (flash->chip->total_size == 0)
1171 return 0;
1172
1173 const chipsize_t size = flash->chip->total_size * 1024;
1174 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
1175 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
1176 if (addr == ERROR_PTR) {
1177 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
1178 flash->chip->name, PRIxPTR_WIDTH, base);
1179 return 1;
1180 }
1181 flash->physical_memory = base;
1182 flash->virtual_memory = (chipaddr)addr;
1183
1184 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
1185 * completely different on some chips and programmers, or not mappable at all.
1186 * Ignore these problems for now and always report success. */
1187 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
1188 base = 0xffffffff - size - 0x400000 + 1;
1189 addr = programmer_map_flash_region("flash chip registers", base, size);
1190 if (addr == ERROR_PTR) {
1191 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
1192 flash->chip->name, PRIxPTR_WIDTH, base);
1193 return 0;
1194 }
1195 flash->physical_registers = base;
1196 flash->virtual_registers = (chipaddr)addr;
1197 }
1198 return 0;
1199}
1200
Nico Huber2d625722016-05-03 10:48:02 +02001201/*
1202 * Return a string corresponding to the bustype parameter.
1203 * Memory is obtained with malloc() and must be freed with free() by the caller.
1204 */
1205char *flashbuses_to_text(enum chipbustype bustype)
1206{
1207 char *ret = calloc(1, 1);
1208 /*
1209 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
1210 * will cease to exist and should be eliminated here as well.
1211 */
1212 if (bustype == BUS_NONSPI) {
1213 ret = strcat_realloc(ret, "Non-SPI, ");
1214 } else {
1215 if (bustype & BUS_PARALLEL)
1216 ret = strcat_realloc(ret, "Parallel, ");
1217 if (bustype & BUS_LPC)
1218 ret = strcat_realloc(ret, "LPC, ");
1219 if (bustype & BUS_FWH)
1220 ret = strcat_realloc(ret, "FWH, ");
1221 if (bustype & BUS_SPI)
1222 ret = strcat_realloc(ret, "SPI, ");
1223 if (bustype & BUS_PROG)
1224 ret = strcat_realloc(ret, "Programmer-specific, ");
1225 if (bustype == BUS_NONE)
1226 ret = strcat_realloc(ret, "None, ");
1227 }
1228 /* Kill last comma. */
1229 ret[strlen(ret) - 2] = '\0';
1230 ret = realloc(ret, strlen(ret) + 1);
1231 return ret;
1232}
1233
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001234int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001235{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001236 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001237 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001238 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001239
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001240 for (chip = flashchips + startchip; chip && chip->name; chip++) {
1241 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +00001242 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001243 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001244 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +00001245 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +03001246 /* Only probe for SPI25 chips by default. */
1247 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
1248 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001249 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
1250 if (!chip->probe && !force) {
1251 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001252 continue;
1253 }
Stefan Reinauer70385642007-04-06 11:58:03 +00001254
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001255 /* Start filling in the dynamic data. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001256 flash->chip = calloc(1, sizeof(struct flashchip));
1257 if (!flash->chip) {
1258 msg_gerr("Out of memory!\n");
1259 exit(1);
1260 }
1261 memcpy(flash->chip, chip, sizeof(struct flashchip));
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001262 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001263
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001264 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +01001265 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001266
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001267 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
1268 * is only called with force=1 after normal probing failed.
1269 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001270 if (force)
1271 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001272
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001273 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +00001274 goto notfound;
1275
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001276 /* If this is the first chip found, accept it.
1277 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001278 * a non-generic match. SFDP and CFI are generic matches.
1279 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001280 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001281 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001282 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001283 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001284 msg_cinfo("===\n"
1285 "SFDP has autodetected a flash chip which is "
1286 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001287 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001288 msg_cinfo("The standard operations read and "
1289 "verify should work, but to support "
1290 "erase, write and all other "
1291 "possible features");
1292 else
1293 msg_cinfo("All standard operations (read, "
1294 "verify, erase and write) should "
1295 "work, but to support all possible "
1296 "features");
1297
Stefan Taunerb4e06bd2012-08-20 00:24:22 +00001298 msg_cinfo(" we need to add them manually.\n"
1299 "You can help us by mailing us the output of the following command to "
1300 "flashrom@flashrom.org:\n"
1301 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
1302 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001303 "===\n");
1304 }
1305
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001306 /* First flash chip detected on this bus. */
1307 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001308 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001309 /* Not the first flash chip detected on this bus, but not a generic match either. */
1310 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
1311 break;
1312 /* 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 +00001313notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001314 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001315 free(flash->chip);
1316 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001317 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001318
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001319 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001320 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001321
Nico Huber7af0e792016-04-29 16:40:15 +02001322 /* Fill fallback layout covering the whole chip. */
1323 struct single_layout *const fallback = &flash->fallback_layout;
1324 fallback->base.entries = &fallback->entry;
1325 fallback->base.num_entries = 1;
1326 fallback->entry.start = 0;
1327 fallback->entry.end = flash->chip->total_size * 1024 - 1;
1328 fallback->entry.included = true;
1329 strcpy(fallback->entry.name, "complete flash");
Stefan Reinauer051e2362011-01-19 06:21:54 +00001330
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001331 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001332 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
1333 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +00001334 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001335#if CONFIG_INTERNAL == 1
1336 if (programmer_table[programmer].map_flash_region == physmap)
1337 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
1338 PRIxPTR_WIDTH, flash->physical_memory);
1339 else
1340#endif
1341 msg_cinfo("on %s.\n", programmer_table[programmer].name);
Uwe Hermann9899cad2009-06-28 21:47:57 +00001342
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001343 /* Flash registers may more likely not be mapped if the chip was forced.
1344 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +00001345 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001346 if (flash->chip->printlock)
1347 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +00001348
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001349 /* Get out of the way for later runs. */
1350 unmap_flash(flash);
1351
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001352 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001353 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001354}
1355
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001356int read_buf_from_file(unsigned char *buf, unsigned long size,
1357 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001358{
Nico Huber7562f7d2013-08-30 21:29:45 +00001359#ifdef __LIBPAYLOAD__
1360 msg_gerr("Error: No file I/O support in libpayload\n");
1361 return 1;
1362#else
Stefan Tauner16687702015-12-25 21:59:45 +00001363 int ret = 0;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001364
Stefan Tauner16687702015-12-25 21:59:45 +00001365 FILE *image;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001366 if ((image = fopen(filename, "rb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001367 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001368 return 1;
1369 }
Stefan Tauner16687702015-12-25 21:59:45 +00001370
1371 struct stat image_stat;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001372 if (fstat(fileno(image), &image_stat) != 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001373 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
Stefan Tauner16687702015-12-25 21:59:45 +00001374 ret = 1;
1375 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001376 }
1377 if (image_stat.st_size != size) {
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +00001378 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 +00001379 (intmax_t)image_stat.st_size, size);
Stefan Tauner16687702015-12-25 21:59:45 +00001380 ret = 1;
1381 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001382 }
Stefan Tauner16687702015-12-25 21:59:45 +00001383
1384 unsigned long numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001385 if (numbytes != size) {
1386 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1387 "wanted %ld!\n", numbytes, size);
Stefan Tauner16687702015-12-25 21:59:45 +00001388 ret = 1;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001389 }
Stefan Tauner16687702015-12-25 21:59:45 +00001390out:
1391 (void)fclose(image);
1392 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001393#endif
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001394}
1395
Mark Marshallf20b7be2014-05-09 21:16:21 +00001396int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001397{
Nico Huber7562f7d2013-08-30 21:29:45 +00001398#ifdef __LIBPAYLOAD__
1399 msg_gerr("Error: No file I/O support in libpayload\n");
1400 return 1;
1401#else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001402 FILE *image;
Stefan Tauner16687702015-12-25 21:59:45 +00001403 int ret = 0;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001404
1405 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001406 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001407 return 1;
1408 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001409 if ((image = fopen(filename, "wb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001410 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001411 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001412 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001413
Stefan Tauner16687702015-12-25 21:59:45 +00001414 unsigned long numbytes = fwrite(buf, 1, size, image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001415 if (numbytes != size) {
Stefan Tauner16687702015-12-25 21:59:45 +00001416 msg_gerr("Error: file %s could not be written completely.\n", filename);
1417 ret = 1;
1418 goto out;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001419 }
Stefan Tauner16687702015-12-25 21:59:45 +00001420 if (fflush(image)) {
1421 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
1422 ret = 1;
1423 }
1424 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
1425#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
1426 struct stat image_stat;
1427 if (fstat(fileno(image), &image_stat) != 0) {
1428 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
1429 ret = 1;
1430 goto out;
1431 }
1432 if (S_ISREG(image_stat.st_mode)) {
1433 if (fsync(fileno(image))) {
1434 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
1435 ret = 1;
1436 }
1437 }
1438#endif
1439out:
1440 if (fclose(image)) {
1441 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
1442 ret = 1;
1443 }
1444 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001445#endif
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001446}
1447
Nico Huber899e4ec2016-04-29 18:39:01 +02001448static int read_by_layout(struct flashctx *, uint8_t *);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001449int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001450{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001451 unsigned long size = flash->chip->total_size * 1024;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001452 unsigned char *buf = calloc(size, sizeof(char));
1453 int ret = 0;
1454
1455 msg_cinfo("Reading flash... ");
1456 if (!buf) {
1457 msg_gerr("Memory allocation failed!\n");
1458 msg_cinfo("FAILED.\n");
1459 return 1;
1460 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001461 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001462 msg_cerr("No read function available for this flash chip.\n");
1463 ret = 1;
1464 goto out_free;
1465 }
Nico Huber899e4ec2016-04-29 18:39:01 +02001466 if (read_by_layout(flash, buf)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001467 msg_cerr("Read operation failed!\n");
1468 ret = 1;
1469 goto out_free;
1470 }
1471
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001472 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001473out_free:
1474 free(buf);
1475 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1476 return ret;
1477}
1478
Stefan Tauner96658be2014-05-26 22:05:31 +00001479/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001480static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001481{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001482 int i, j, k;
1483 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001484
1485 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1486 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001487 struct block_eraser eraser = chip->block_erasers[k];
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001488
1489 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1490 /* Blocks with zero size are bugs in flashchips.c. */
1491 if (eraser.eraseblocks[i].count &&
1492 !eraser.eraseblocks[i].size) {
1493 msg_gerr("ERROR: Flash chip %s erase function "
1494 "%i region %i has size 0. Please report"
1495 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001496 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001497 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001498 }
1499 /* Blocks with zero count are bugs in flashchips.c. */
1500 if (!eraser.eraseblocks[i].count &&
1501 eraser.eraseblocks[i].size) {
1502 msg_gerr("ERROR: Flash chip %s erase function "
1503 "%i region %i has count 0. Please report"
1504 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001505 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001506 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001507 }
1508 done += eraser.eraseblocks[i].count *
1509 eraser.eraseblocks[i].size;
1510 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001511 /* Empty eraseblock definition with erase function. */
1512 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001513 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001514 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001515 if (!done)
1516 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001517 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001518 msg_gerr("ERROR: Flash chip %s erase function %i "
1519 "region walking resulted in 0x%06x bytes total,"
1520 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001521 " flashrom@flashrom.org\n", chip->name, k,
1522 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001523 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001524 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001525 if (!eraser.block_erase)
1526 continue;
1527 /* Check if there are identical erase functions for different
1528 * layouts. That would imply "magic" erase functions. The
1529 * easiest way to check this is with function pointers.
1530 */
Uwe Hermann43959702010-03-13 17:28:29 +00001531 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001532 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001533 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001534 msg_gerr("ERROR: Flash chip %s erase function "
1535 "%i and %i are identical. Please report"
1536 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001537 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001538 ret = 1;
1539 }
Uwe Hermann43959702010-03-13 17:28:29 +00001540 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001541 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001542 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001543}
1544
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001545static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001546{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001547 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001548
1549 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1550 if (log)
1551 msg_cdbg("not defined. ");
1552 return 1;
1553 }
1554 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1555 if (log)
1556 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001557 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001558 return 1;
1559 }
1560 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1561 if (log)
1562 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001563 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001564 return 1;
1565 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001566 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001567 return 0;
1568}
1569
Nico Huber7af0e792016-04-29 16:40:15 +02001570/**
1571 * @brief Reads the included layout regions into a buffer.
1572 *
1573 * If there is no layout set in the given flash context, the whole chip will
1574 * be read.
1575 *
1576 * @param flashctx Flash context to be used.
1577 * @param buffer Buffer of full chip size to read into.
1578 * @return 0 on success,
1579 * 1 if any read fails.
1580 */
1581static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1582{
1583 const struct flashrom_layout *const layout = get_layout(flashctx);
1584
1585 size_t i;
1586 for (i = 0; i < layout->num_entries; ++i) {
1587 if (!layout->entries[i].included)
1588 continue;
1589
1590 const chipoff_t region_start = layout->entries[i].start;
1591 const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1;
1592
1593 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1594 return 1;
1595 }
1596 return 0;
1597}
1598
1599typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1600/**
1601 * @private
1602 *
1603 * For read-erase-write, `curcontents` and `newcontents` shall point
1604 * to buffers of the chip's size. Both are supposed to be prefilled
1605 * with at least the included layout regions of the current flash
1606 * contents (`curcontents`) and the data to be written to the flash
1607 * (`newcontents`).
1608 *
1609 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1610 *
1611 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1612 */
1613struct walk_info {
1614 uint8_t *curcontents;
1615 const uint8_t *newcontents;
1616 chipoff_t region_start;
1617 chipoff_t region_end;
1618 chipoff_t erase_start;
1619 chipoff_t erase_end;
1620};
1621/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1622typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1623
1624static int walk_eraseblocks(struct flashctx *const flashctx,
1625 struct walk_info *const info,
1626 const size_t erasefunction, const per_blockfn_t per_blockfn)
1627{
1628 int ret;
1629 size_t i, j;
1630 bool first = true;
1631 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1632
1633 info->erase_start = 0;
1634 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1635 /* count==0 for all automatically initialized array
1636 members so the loop below won't be executed for them. */
1637 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1638 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1639
1640 /* Skip any eraseblock that is completely outside the current region. */
1641 if (info->erase_end < info->region_start)
1642 continue;
1643 if (info->region_end < info->erase_start)
1644 break;
1645
1646 /* Print this for every block except the first one. */
1647 if (first)
1648 first = false;
1649 else
1650 msg_cdbg(", ");
1651 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1652
1653 ret = per_blockfn(flashctx, info, eraser->block_erase);
1654 if (ret)
1655 return ret;
1656 }
1657 if (info->region_end < info->erase_start)
1658 break;
1659 }
1660 msg_cdbg("\n");
1661 return 0;
1662}
1663
1664static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1665 const per_blockfn_t per_blockfn)
1666{
1667 const struct flashrom_layout *const layout = get_layout(flashctx);
1668
1669 all_skipped = true;
1670 msg_cinfo("Erasing and writing flash chip... ");
1671
1672 size_t i;
1673 for (i = 0; i < layout->num_entries; ++i) {
1674 if (!layout->entries[i].included)
1675 continue;
1676
1677 info->region_start = layout->entries[i].start;
1678 info->region_end = layout->entries[i].end;
1679
1680 size_t j;
1681 int error = 1; /* retry as long as it's 1 */
1682 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1683 if (j != 0)
1684 msg_cinfo("Looking for another erase function.\n");
1685 msg_cdbg("Trying erase function %zi... ", j);
1686 if (check_block_eraser(flashctx, j, 1))
1687 continue;
1688
1689 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1690 if (error != 1)
1691 break;
1692
1693 if (info->curcontents) {
1694 msg_cinfo("Reading current flash chip contents... ");
1695 if (read_by_layout(flashctx, info->curcontents)) {
1696 /* Now we are truly screwed. Read failed as well. */
1697 msg_cerr("Can't read anymore! Aborting.\n");
1698 /* We have no idea about the flash chip contents, so
1699 retrying with another erase function is pointless. */
1700 error = 2;
1701 break;
1702 }
1703 msg_cinfo("done. ");
1704 }
1705 }
1706 if (error == 1)
1707 msg_cinfo("No usable erase functions left.\n");
1708 if (error) {
1709 msg_cerr("FAILED!\n");
1710 return 1;
1711 }
1712 }
1713 if (all_skipped)
1714 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1715 msg_cinfo("Erase/write done.\n");
1716 return 0;
1717}
1718
1719static int erase_block(struct flashctx *const flashctx,
1720 const struct walk_info *const info, const erasefn_t erasefn)
1721{
1722 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
1723
1724 all_skipped = false;
1725
1726 msg_cdbg("E");
1727 if (erasefn(flashctx, info->erase_start, erase_len))
1728 return 1;
1729 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1730 msg_cerr("ERASE FAILED!\n");
1731 return 1;
1732 }
1733 return 0;
1734}
1735
1736/**
1737 * @brief Erases the included layout regions.
1738 *
1739 * If there is no layout set in the given flash context, the whole chip will
1740 * be erased.
1741 *
1742 * @param flashctx Flash context to be used.
1743 * @param buffer Buffer of full chip size to read into.
1744 * @return 0 on success,
1745 * 1 if all available erase functions failed.
1746 */
Nico Huber454f6132012-12-10 13:34:10 +00001747static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001748{
1749 struct walk_info info = { 0 };
1750 return walk_by_layout(flashctx, &info, &erase_block);
1751}
1752
1753static int read_erase_write_block(struct flashctx *const flashctx,
1754 const struct walk_info *const info, const erasefn_t erasefn)
1755{
1756 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1757 const bool region_unaligned = info->region_start > info->erase_start ||
1758 info->erase_end > info->region_end;
1759 const uint8_t *newcontents = NULL;
1760 int ret = 2;
1761
1762 /*
1763 * If the region is not erase-block aligned, merge current flash con-
1764 * tents into `info->curcontents` and a new buffer `newc`. The former
1765 * is necessary since we have no guarantee that the full erase block
1766 * was already read into `info->curcontents`. For the latter a new
1767 * buffer is used since `info->newcontents` might contain data for
1768 * other unaligned regions that touch this erase block too.
1769 */
1770 if (region_unaligned) {
1771 msg_cdbg("R");
1772 uint8_t *const newc = malloc(erase_len);
1773 if (!newc) {
1774 msg_cerr("Out of memory!\n");
1775 return 1;
1776 }
1777 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1778
1779 /* Merge data preceding the current region. */
1780 if (info->region_start > info->erase_start) {
1781 const chipoff_t start = info->erase_start;
1782 const chipsize_t len = info->region_start - info->erase_start;
1783 if (flashctx->chip->read(flashctx, newc, start, len)) {
1784 msg_cerr("Can't read! Aborting.\n");
1785 goto _free_ret;
1786 }
1787 memcpy(info->curcontents + start, newc, len);
1788 }
1789 /* Merge data following the current region. */
1790 if (info->erase_end > info->region_end) {
1791 const chipoff_t start = info->region_end + 1;
1792 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1793 const chipsize_t len = info->erase_end - info->region_end;
1794 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1795 msg_cerr("Can't read! Aborting.\n");
1796 goto _free_ret;
1797 }
1798 memcpy(info->curcontents + start, newc + rel_start, len);
1799 }
1800
1801 newcontents = newc;
1802 } else {
1803 newcontents = info->newcontents + info->erase_start;
1804 }
1805
1806 ret = 1;
1807 bool skipped = true;
1808 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001809 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001810 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1811 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001812 if (erase_block(flashctx, info, erasefn))
1813 goto _free_ret;
1814 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001815 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001816 skipped = false;
1817 }
1818
1819 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1820 /* get_next_write() sets starthere to a new value after the call. */
1821 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1822 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1823 if (!writecount++)
1824 msg_cdbg("W");
1825 /* Needs the partial write function signature. */
1826 if (flashctx->chip->write(flashctx, newcontents + starthere,
1827 info->erase_start + starthere, lenhere))
1828 goto _free_ret;
1829 starthere += lenhere;
1830 skipped = false;
1831 }
1832 if (skipped)
1833 msg_cdbg("S");
1834 else
1835 all_skipped = false;
1836
1837 /* Update curcontents, other regions with overlapping erase blocks
1838 might rely on this. */
1839 memcpy(curcontents, newcontents, erase_len);
1840 ret = 0;
1841
1842_free_ret:
1843 if (region_unaligned)
1844 free((void *)newcontents);
1845 return ret;
1846}
1847
1848/**
1849 * @brief Writes the included layout regions from a given image.
1850 *
1851 * If there is no layout set in the given flash context, the whole image
1852 * will be written.
1853 *
1854 * @param flashctx Flash context to be used.
1855 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1856 * @param newcontents The new image to be written.
1857 * @return 0 on success,
1858 * 1 if anything has gone wrong.
1859 */
Nico Huber454f6132012-12-10 13:34:10 +00001860static int write_by_layout(struct flashctx *const flashctx,
1861 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001862{
1863 struct walk_info info;
1864 info.curcontents = curcontents;
1865 info.newcontents = newcontents;
1866 return walk_by_layout(flashctx, &info, read_erase_write_block);
1867}
1868
1869/**
1870 * @brief Compares the included layout regions with content from a buffer.
1871 *
1872 * If there is no layout set in the given flash context, the whole chip's
1873 * contents will be compared.
1874 *
1875 * @param flashctx Flash context to be used.
1876 * @param curcontents A buffer of full chip size to read current chip contents into.
1877 * @param newcontents The new image to compare to.
1878 * @return 0 on success,
1879 * 1 if reading failed,
1880 * 3 if the contents don't match.
1881 */
Nico Huber454f6132012-12-10 13:34:10 +00001882static int verify_by_layout(struct flashctx *const flashctx,
1883 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001884{
1885 const struct flashrom_layout *const layout = get_layout(flashctx);
1886
1887 size_t i;
1888 for (i = 0; i < layout->num_entries; ++i) {
1889 if (!layout->entries[i].included)
1890 continue;
1891
1892 const chipoff_t region_start = layout->entries[i].start;
1893 const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1;
1894
1895 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1896 return 1;
1897 if (compare_range(newcontents + region_start, curcontents + region_start,
1898 region_start, region_len))
1899 return 3;
1900 }
1901 return 0;
1902}
1903
Stefan Tauner136388f2013-07-15 10:47:53 +00001904static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001905{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001906 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001907#if CONFIG_INTERNAL == 1
1908 if (programmer == PROGRAMMER_INTERNAL)
1909 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
1910 "chip. Please report this on IRC at chat.freenode.net (channel #flashrom) or\n"
1911 "mail flashrom@flashrom.org, thanks!\n"
1912 "-------------------------------------------------------------------------------\n"
1913 "You may now reboot or simply leave the machine running.\n");
1914 else
1915#endif
1916 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1917 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
1918 "please report this on IRC at chat.freenode.net (channel #flashrom) or\n"
1919 "mail flashrom@flashrom.org, thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001920}
1921
Stefan Tauner136388f2013-07-15 10:47:53 +00001922static void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001923{
Stefan Tauner136388f2013-07-15 10:47:53 +00001924 msg_gerr("Your flash chip is in an unknown state.\n");
1925#if CONFIG_INTERNAL == 1
1926 if (programmer == PROGRAMMER_INTERNAL)
1927 msg_gerr("Get help on IRC at chat.freenode.net (channel #flashrom) or\n"
1928 "mail flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!\n"
1929 "-------------------------------------------------------------------------------\n"
1930 "DO NOT REBOOT OR POWEROFF!\n");
1931 else
1932#endif
1933 msg_gerr("Please report this on IRC at chat.freenode.net (channel #flashrom) or\n"
1934 "mail flashrom@flashrom.org, thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001935}
1936
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001937/* The way to go if you want a delimited list of programmers */
Stefan Tauner66652442011-06-26 17:38:17 +00001938void list_programmers(const char *delim)
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001939{
1940 enum programmer p;
1941 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001942 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001943 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001944 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001945 }
Stefan Tauner23e10b82016-01-23 16:16:49 +00001946 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001947}
1948
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001949void list_programmers_linebreak(int startcol, int cols, int paren)
1950{
1951 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001952 int pnamelen;
1953 int remaining = 0, firstline = 1;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001954 enum programmer p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001955 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001956
1957 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1958 pname = programmer_table[p].name;
1959 pnamelen = strlen(pname);
1960 if (remaining - pnamelen - 2 < 0) {
1961 if (firstline)
1962 firstline = 0;
1963 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001964 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001965 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001966 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001967 remaining = cols - startcol;
1968 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001969 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001970 remaining--;
1971 }
1972 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001973 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001974 remaining--;
1975 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001976 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001977 remaining -= pnamelen;
1978 if (p < PROGRAMMER_INVALID - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001979 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001980 remaining--;
1981 } else {
1982 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001983 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001984 }
1985 }
1986}
1987
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001988void print_sysinfo(void)
1989{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001990#if IS_WINDOWS
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001991 SYSTEM_INFO si;
1992 OSVERSIONINFOEX osvi;
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001993
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001994 memset(&si, 0, sizeof(SYSTEM_INFO));
1995 memset(&osvi, 0, sizeof(OSVERSIONINFOEX));
1996 msg_ginfo(" on Windows");
1997 /* Tell Windows which version of the structure we want. */
1998 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1999 if (GetVersionEx((OSVERSIONINFO*) &osvi))
2000 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
2001 else
2002 msg_ginfo(" unknown version");
2003 GetSystemInfo(&si);
2004 switch (si.wProcessorArchitecture) {
2005 case PROCESSOR_ARCHITECTURE_AMD64:
2006 msg_ginfo(" (x86_64)");
2007 break;
2008 case PROCESSOR_ARCHITECTURE_INTEL:
2009 msg_ginfo(" (x86)");
2010 break;
2011 default:
2012 msg_ginfo(" (unknown arch)");
2013 break;
2014 }
2015#elif HAVE_UTSNAME == 1
2016 struct utsname osinfo;
2017
2018 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002019 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
2020 osinfo.machine);
2021#else
2022 msg_ginfo(" on unknown machine");
2023#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002024}
2025
2026void print_buildinfo(void)
2027{
2028 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002029#if NEED_PCI == 1
2030#ifdef PCILIB_VERSION
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002031 msg_gdbg(" libpci %s,", PCILIB_VERSION);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002032#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002033 msg_gdbg(" unknown PCI library,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002034#endif
2035#endif
2036#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002037 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00002038#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002039 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00002040#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002041 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00002042#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002043#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002044 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002045#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002046 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002047#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002048 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002049#endif
2050#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002051 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00002052#endif
2053#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002054 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00002055#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002056 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00002057#else
2058#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002059#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002060 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002061}
2062
Bernhard Walle201bde32008-01-21 15:24:22 +00002063void print_version(void)
2064{
Stefan Tauner76347082016-11-27 17:45:49 +01002065 msg_ginfo("flashrom %s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002066 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002067 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00002068}
2069
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00002070void print_banner(void)
2071{
2072 msg_ginfo("flashrom is free software, get the source code at "
Stefan Tauner4c723152016-01-14 22:47:55 +00002073 "https://flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00002074 msg_ginfo("\n");
2075}
2076
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002077int selfcheck(void)
2078{
Stefan Tauner96658be2014-05-26 22:05:31 +00002079 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00002080 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00002081
2082 /* Safety check. Instead of aborting after the first error, check
2083 * if more errors exist.
2084 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002085 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00002086 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00002087 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002088 }
Stefan Taunera6d96482012-12-26 19:51:23 +00002089 for (i = 0; i < PROGRAMMER_INVALID; i++) {
2090 const struct programmer_entry p = programmer_table[i];
2091 if (p.name == NULL) {
2092 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
2093 ret = 1;
2094 /* This might hide other problems with this programmer, but allows for better error
2095 * messages below without jumping through hoops. */
2096 continue;
2097 }
Stefan Tauneraf358d62012-12-27 18:40:26 +00002098 switch (p.type) {
2099 case USB:
2100 case PCI:
2101 case OTHER:
2102 if (p.devs.note == NULL) {
2103 if (strcmp("internal", p.name) == 0)
2104 break; /* This one has its device list stored separately. */
2105 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
2106 p.name);
2107 ret = 1;
2108 }
2109 break;
2110 default:
2111 msg_gerr("Programmer %s does not have a valid type set!\n", p.name);
2112 ret = 1;
2113 break;
2114 }
Stefan Taunera6d96482012-12-26 19:51:23 +00002115 if (p.init == NULL) {
2116 msg_gerr("Programmer %s does not have a valid init function!\n", p.name);
2117 ret = 1;
2118 }
2119 if (p.delay == NULL) {
2120 msg_gerr("Programmer %s does not have a valid delay function!\n", p.name);
2121 ret = 1;
2122 }
2123 if (p.map_flash_region == NULL) {
2124 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p.name);
2125 ret = 1;
2126 }
2127 if (p.unmap_flash_region == NULL) {
2128 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p.name);
2129 ret = 1;
2130 }
2131 }
Stefan Tauner96658be2014-05-26 22:05:31 +00002132
2133 /* It would be favorable if we could check for the correct layout (especially termination) of various
2134 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
2135 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
2136 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
2137 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
2138 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00002139 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00002140 msg_gerr("Flashchips table miscompilation!\n");
2141 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00002142 } else {
2143 for (i = 0; i < flashchips_size - 1; i++) {
2144 const struct flashchip *chip = &flashchips[i];
2145 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
2146 ret = 1;
2147 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
2148 "Please report a bug at flashrom@flashrom.org\n", i,
2149 chip->name == NULL ? "unnamed" : chip->name);
2150 }
2151 if (selfcheck_eraseblocks(chip)) {
2152 ret = 1;
2153 }
2154 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00002155 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00002156
Stefan Tauner600576b2014-06-12 22:57:36 +00002157#if CONFIG_INTERNAL == 1
2158 ret |= selfcheck_board_enables();
2159#endif
2160
Stefan Tauner96658be2014-05-26 22:05:31 +00002161 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00002162 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002163}
2164
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002165/* FIXME: This function signature needs to be improved once doit() has a better
2166 * function signature.
2167 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002168int chip_safety_check(const struct flashctx *flash, int force, int read_it, int write_it, int erase_it,
2169 int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002170{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002171 const struct flashchip *chip = flash->chip;
2172
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002173 if (!programmer_may_write && (write_it || erase_it)) {
2174 msg_perr("Write/erase is not working yet on your programmer in "
2175 "its current configuration.\n");
2176 /* --force is the wrong approach, but it's the best we can do
2177 * until the generic programmer parameter parser is merged.
2178 */
2179 if (!force)
2180 return 1;
2181 msg_cerr("Continuing anyway.\n");
2182 }
2183
2184 if (read_it || erase_it || write_it || verify_it) {
2185 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00002186 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002187 msg_cerr("Read is not working on this chip. ");
2188 if (!force)
2189 return 1;
2190 msg_cerr("Continuing anyway.\n");
2191 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002192 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002193 msg_cerr("flashrom has no read function for this "
2194 "flash chip.\n");
2195 return 1;
2196 }
2197 }
2198 if (erase_it || write_it) {
2199 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00002200 if (chip->tested.erase == NA) {
2201 msg_cerr("Erase is not possible on this chip.\n");
2202 return 1;
2203 }
2204 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002205 msg_cerr("Erase is not working on this chip. ");
2206 if (!force)
2207 return 1;
2208 msg_cerr("Continuing anyway.\n");
2209 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00002210 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00002211 msg_cerr("flashrom has no erase function for this "
2212 "flash chip.\n");
2213 return 1;
2214 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002215 }
2216 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00002217 if (chip->tested.write == NA) {
2218 msg_cerr("Write is not possible on this chip.\n");
2219 return 1;
2220 }
2221 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002222 msg_cerr("Write is not working on this chip. ");
2223 if (!force)
2224 return 1;
2225 msg_cerr("Continuing anyway.\n");
2226 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002227 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002228 msg_cerr("flashrom has no write function for this "
2229 "flash chip.\n");
2230 return 1;
2231 }
2232 }
2233 return 0;
2234}
2235
Nico Huber305f4172013-06-14 11:55:26 +02002236int prepare_flash_access(struct flashctx *const flash,
2237 const bool read_it, const bool write_it,
2238 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00002239{
2240 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
2241 msg_cerr("Aborting.\n");
2242 return 1;
2243 }
2244
2245 if (flash->layout == get_global_layout() && normalize_romentries(flash)) {
2246 msg_cerr("Requested regions can not be handled. Aborting.\n");
2247 return 1;
2248 }
2249
2250 if (map_flash(flash) != 0)
2251 return 1;
2252
2253 /* Given the existence of read locks, we want to unlock for read,
2254 erase and write. */
2255 if (flash->chip->unlock)
2256 flash->chip->unlock(flash);
2257
Nico Huberf43c6542017-10-14 17:47:28 +02002258 flash->address_high_byte = -1;
2259 flash->in_4ba_mode = false;
2260
Ed Swierkcc20a9b2017-07-03 13:17:18 -07002261 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01002262 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01002263 int ret;
2264 if (spi_master_4ba(flash))
2265 ret = spi_enter_4ba(flash);
2266 else
2267 ret = spi_exit_4ba(flash);
2268 if (ret) {
2269 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07002270 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02002271 }
Boris Baykov99127182016-06-11 18:29:00 +02002272 }
2273
Nico Huber454f6132012-12-10 13:34:10 +00002274 return 0;
2275}
2276
Nico Huber305f4172013-06-14 11:55:26 +02002277void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00002278{
2279 unmap_flash(flash);
2280}
2281
2282/**
2283 * @addtogroup flashrom-flash
2284 * @{
2285 */
2286
2287/**
2288 * @brief Erase the specified ROM chip.
2289 *
2290 * If a layout is set in the given flash context, only included regions
2291 * will be erased.
2292 *
2293 * @param flashctx The context of the flash chip to erase.
2294 * @return 0 on success.
2295 */
2296int flashrom_flash_erase(struct flashctx *const flashctx)
2297{
2298 if (prepare_flash_access(flashctx, false, false, true, false))
2299 return 1;
2300
2301 const int ret = erase_by_layout(flashctx);
2302
2303 finalize_flash_access(flashctx);
2304
2305 return ret;
2306}
2307
2308/** @} */ /* end flashrom-flash */
2309
2310/**
2311 * @defgroup flashrom-ops Operations
2312 * @{
2313 */
2314
2315/**
2316 * @brief Read the current image from the specified ROM chip.
2317 *
2318 * If a layout is set in the specified flash context, only included regions
2319 * will be read.
2320 *
2321 * @param flashctx The context of the flash chip.
2322 * @param buffer Target buffer to write image to.
2323 * @param buffer_len Size of target buffer in bytes.
2324 * @return 0 on success,
2325 * 2 if buffer_len is too short for the flash chip's contents,
2326 * or 1 on any other failure.
2327 */
2328int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
2329{
2330 const size_t flash_size = flashctx->chip->total_size * 1024;
2331
2332 if (flash_size > buffer_len)
2333 return 2;
2334
2335 if (prepare_flash_access(flashctx, true, false, false, false))
2336 return 1;
2337
2338 msg_cinfo("Reading flash... ");
2339
2340 int ret = 1;
2341 if (read_by_layout(flashctx, buffer)) {
2342 msg_cerr("Read operation failed!\n");
2343 msg_cinfo("FAILED.\n");
2344 goto _finalize_ret;
2345 }
2346 msg_cinfo("done.\n");
2347 ret = 0;
2348
2349_finalize_ret:
2350 finalize_flash_access(flashctx);
2351 return ret;
2352}
2353
2354static void combine_image_by_layout(const struct flashctx *const flashctx,
2355 uint8_t *const newcontents, const uint8_t *const oldcontents)
2356{
2357 const struct flashrom_layout *const layout = get_layout(flashctx);
2358
2359 size_t i;
2360 for (i = 0; i < layout->num_entries; ++i) {
2361 if (layout->entries[i].included)
2362 continue;
2363
2364 const chipoff_t region_start = layout->entries[i].start;
2365 const chipsize_t region_len = layout->entries[i].end - layout->entries[i].start + 1;
2366
2367 memcpy(newcontents + region_start, oldcontents + region_start, region_len);
2368 }
2369}
2370
2371/**
2372 * @brief Write the specified image to the ROM chip.
2373 *
2374 * If a layout is set in the specified flash context, only erase blocks
2375 * containing included regions will be touched.
2376 *
2377 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02002378 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00002379 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002380 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00002381 * @return 0 on success,
2382 * 4 if buffer_len doesn't match the size of the flash chip,
2383 * 3 if write was tried but nothing has changed,
2384 * 2 if write failed and flash contents changed,
2385 * or 1 on any other failure.
2386 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002387int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
2388 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00002389{
2390 const size_t flash_size = flashctx->chip->total_size * 1024;
2391 const bool verify_all = flashctx->flags.verify_whole_chip;
2392 const bool verify = flashctx->flags.verify_after_write;
2393
2394 if (buffer_len != flash_size)
2395 return 4;
2396
2397 int ret = 1;
2398
2399 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002400 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00002401 uint8_t *const curcontents = malloc(flash_size);
2402 uint8_t *oldcontents = NULL;
2403 if (verify_all)
2404 oldcontents = malloc(flash_size);
2405 if (!curcontents || (verify_all && !oldcontents)) {
2406 msg_gerr("Out of memory!\n");
2407 goto _free_ret;
2408 }
2409
2410#if CONFIG_INTERNAL == 1
2411 if (programmer == PROGRAMMER_INTERNAL && cb_check_image(newcontents, flash_size) < 0) {
2412 if (flashctx->flags.force_boardmismatch) {
2413 msg_pinfo("Proceeding anyway because user forced us to.\n");
2414 } else {
2415 msg_perr("Aborting. You can override this with "
2416 "-p internal:boardmismatch=force.\n");
2417 goto _free_ret;
2418 }
2419 }
2420#endif
2421
2422 if (prepare_flash_access(flashctx, false, true, false, verify))
2423 goto _free_ret;
2424
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002425 /* If given, assume flash chip contains same data as `refcontents`. */
2426 if (refcontents) {
2427 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
2428 memcpy(curcontents, refcontents, flash_size);
2429 if (oldcontents)
2430 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00002431 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002432 /*
2433 * Read the whole chip to be able to check whether regions need to be
2434 * erased and to give better diagnostics in case write fails.
2435 * The alternative is to read only the regions which are to be
2436 * preserved, but in that case we might perform unneeded erase which
2437 * takes time as well.
2438 */
2439 msg_cinfo("Reading old flash chip contents... ");
2440 if (verify_all) {
2441 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
2442 msg_cinfo("FAILED.\n");
2443 goto _finalize_ret;
2444 }
2445 memcpy(curcontents, oldcontents, flash_size);
2446 } else {
2447 if (read_by_layout(flashctx, curcontents)) {
2448 msg_cinfo("FAILED.\n");
2449 goto _finalize_ret;
2450 }
Nico Huber454f6132012-12-10 13:34:10 +00002451 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002452 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00002453 }
Nico Huber454f6132012-12-10 13:34:10 +00002454
2455 if (write_by_layout(flashctx, curcontents, newcontents)) {
2456 msg_cerr("Uh oh. Erase/write failed. ");
2457 ret = 2;
2458 if (verify_all) {
2459 msg_cerr("Checking if anything has changed.\n");
2460 msg_cinfo("Reading current flash chip contents... ");
2461 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
2462 msg_cinfo("done.\n");
2463 if (!memcmp(oldcontents, curcontents, flash_size)) {
2464 nonfatal_help_message();
2465 goto _finalize_ret;
2466 }
2467 msg_cerr("Apparently at least some data has changed.\n");
2468 } else
2469 msg_cerr("Can't even read anymore!\n");
2470 emergency_help_message();
2471 goto _finalize_ret;
2472 } else {
2473 msg_cerr("\n");
2474 }
2475 emergency_help_message();
2476 goto _finalize_ret;
2477 }
2478
2479 /* Verify only if we actually changed something. */
2480 if (verify && !all_skipped) {
2481 const struct flashrom_layout *const layout_bak = flashctx->layout;
2482
2483 msg_cinfo("Verifying flash... ");
2484
2485 /* Work around chips which need some time to calm down. */
2486 programmer_delay(1000*1000);
2487
2488 if (verify_all) {
2489 combine_image_by_layout(flashctx, newcontents, oldcontents);
2490 flashctx->layout = NULL;
2491 }
2492 ret = verify_by_layout(flashctx, curcontents, newcontents);
2493 flashctx->layout = layout_bak;
2494 /* If we tried to write, and verification now fails, we
2495 might have an emergency situation. */
2496 if (ret)
2497 emergency_help_message();
2498 else
2499 msg_cinfo("VERIFIED.\n");
2500 } else {
2501 /* We didn't change anything. */
2502 ret = 0;
2503 }
2504
2505_finalize_ret:
2506 finalize_flash_access(flashctx);
2507_free_ret:
2508 free(oldcontents);
2509 free(curcontents);
2510 return ret;
2511}
2512
2513/**
2514 * @brief Verify the ROM chip's contents with the specified image.
2515 *
2516 * If a layout is set in the specified flash context, only included regions
2517 * will be verified.
2518 *
2519 * @param flashctx The context of the flash chip.
2520 * @param buffer Source buffer to verify with.
2521 * @param buffer_len Size of source buffer in bytes.
2522 * @return 0 on success,
2523 * 3 if the chip's contents don't match,
2524 * 2 if buffer_len doesn't match the size of the flash chip,
2525 * or 1 on any other failure.
2526 */
2527int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
2528{
2529 const size_t flash_size = flashctx->chip->total_size * 1024;
2530
2531 if (buffer_len != flash_size)
2532 return 2;
2533
2534 const uint8_t *const newcontents = buffer;
2535 uint8_t *const curcontents = malloc(flash_size);
2536 if (!curcontents) {
2537 msg_gerr("Out of memory!\n");
2538 return 1;
2539 }
2540
2541 int ret = 1;
2542
2543 if (prepare_flash_access(flashctx, false, false, false, true))
2544 goto _free_ret;
2545
2546 msg_cinfo("Verifying flash... ");
2547 ret = verify_by_layout(flashctx, curcontents, newcontents);
2548 if (!ret)
2549 msg_cinfo("VERIFIED.\n");
2550
2551 finalize_flash_access(flashctx);
2552_free_ret:
2553 free(curcontents);
2554 return ret;
2555}
2556
2557/** @} */ /* end flashrom-ops */
Nico Huber899e4ec2016-04-29 18:39:01 +02002558
2559int do_read(struct flashctx *const flash, const char *const filename)
2560{
2561 if (prepare_flash_access(flash, true, false, false, false))
2562 return 1;
2563
2564 const int ret = read_flash_to_file(flash, filename);
2565
2566 finalize_flash_access(flash);
2567
2568 return ret;
2569}
2570
2571int do_erase(struct flashctx *const flash)
2572{
2573 const int ret = flashrom_flash_erase(flash);
2574
2575 /*
2576 * FIXME: Do we really want the scary warning if erase failed?
2577 * After all, after erase the chip is either blank or partially
2578 * blank or it has the old contents. A blank chip won't boot,
2579 * so if the user wanted erase and reboots afterwards, the user
2580 * knows very well that booting won't work.
2581 */
2582 if (ret)
2583 emergency_help_message();
2584
2585 return ret;
2586}
2587
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002588int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
Nico Huber899e4ec2016-04-29 18:39:01 +02002589{
2590 const size_t flash_size = flash->chip->total_size * 1024;
2591 int ret = 1;
2592
2593 uint8_t *const newcontents = malloc(flash_size);
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002594 uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
2595
2596 if (!newcontents || (referencefile && !refcontents)) {
Nico Huber899e4ec2016-04-29 18:39:01 +02002597 msg_gerr("Out of memory!\n");
2598 goto _free_ret;
2599 }
2600
2601 if (read_buf_from_file(newcontents, flash_size, filename))
2602 goto _free_ret;
2603
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002604 if (referencefile) {
2605 if (read_buf_from_file(refcontents, flash_size, referencefile))
2606 goto _free_ret;
2607 }
2608
2609 ret = flashrom_image_write(flash, newcontents, flash_size, refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002610
2611_free_ret:
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002612 free(refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002613 free(newcontents);
2614 return ret;
2615}
2616
2617int do_verify(struct flashctx *const flash, const char *const filename)
2618{
2619 const size_t flash_size = flash->chip->total_size * 1024;
2620 int ret = 1;
2621
2622 uint8_t *const newcontents = malloc(flash_size);
2623 if (!newcontents) {
2624 msg_gerr("Out of memory!\n");
2625 goto _free_ret;
2626 }
2627
2628 if (read_buf_from_file(newcontents, flash_size, filename))
2629 goto _free_ret;
2630
2631 ret = flashrom_image_verify(flash, newcontents, flash_size);
2632
2633_free_ret:
2634 free(newcontents);
2635 return ret;
2636}