blob: 9b9c2fcf580ab9ccca856a380bada530d65ac47a [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
Marc Schink3578ec62016-03-17 16:23:03 +0100440#if CONFIG_JLINK_SPI == 1
441 {
442 .name = "jlink_spi",
443 .type = OTHER,
444 .init = jlink_spi_init,
445 .devs.note = "SEGGER J-Link and compatible devices\n",
446 .map_flash_region = fallback_map,
447 .unmap_flash_region = fallback_unmap,
448 .delay = internal_delay,
449 },
450#endif
451
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100452#if CONFIG_NI845X_SPI == 1
453 {
454 .name = "ni845x_spi",
455 .type = OTHER, // choose other because NI-845x uses own USB implementation
456 .devs.note = "National Instruments USB-845x\n",
457 .init = ni845x_spi_init,
458 .map_flash_region = fallback_map,
459 .unmap_flash_region = fallback_unmap,
460 .delay = internal_delay,
461 },
462#endif
Miklós Márton324929c2019-08-01 19:14:10 +0200463
464#if CONFIG_STLINKV3_SPI == 1
465 {
466 .name = "stlinkv3_spi",
467 .type = USB,
468 .devs.dev = devs_stlinkv3_spi,
469 .init = stlinkv3_spi_init,
470 .map_flash_region = fallback_map,
471 .unmap_flash_region = fallback_unmap,
472 .delay = internal_delay,
473 },
474#endif
475
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000476 {0}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000477};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000478
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +0000479#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000480static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +0000481/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +0000482static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +0000483 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000484 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +0000485} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000486/* Initialize to 0 to make sure nobody registers a shutdown function before
487 * programmer init.
488 */
489static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000490
Stefan Taunerc4f44df2013-08-12 22:58:43 +0000491/* Did we change something or was every erase/write skipped (if any)? */
492static bool all_skipped = true;
493
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000494static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +0000495
Stefan Tauner2a1ed772014-08-31 00:09:21 +0000496int shutdown_free(void *data)
497{
498 free(data);
499 return 0;
500}
501
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000502/* Register a function to be executed on programmer shutdown.
503 * The advantage over atexit() is that you can supply a void pointer which will
504 * be used as parameter to the registered function upon programmer shutdown.
505 * This pointer can point to arbitrary data used by said function, e.g. undo
506 * information for GPIO settings etc. If unneeded, set data=NULL.
507 * Please note that the first (void *data) belongs to the function signature of
508 * the function passed as first parameter.
509 */
David Hendricks8bb20212011-06-14 01:35:36 +0000510int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000511{
512 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000513 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000514 SHUTDOWN_MAXFN);
515 return 1;
516 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000517 if (!may_register_shutdown) {
518 msg_perr("Tried to register a shutdown function before "
519 "programmer init.\n");
520 return 1;
521 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000522 shutdown_fn[shutdown_fn_count].func = function;
523 shutdown_fn[shutdown_fn_count].data = data;
524 shutdown_fn_count++;
525
526 return 0;
527}
528
Nikolai Artemiev4ad48642020-11-05 13:54:27 +1100529int register_chip_restore(chip_restore_fn_cb_t func,
530 struct flashctx *flash, uint8_t status)
531{
532 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
533 msg_perr("Tried to register more than %i chip restore"
534 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
535 return 1;
536 }
537 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
538 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
539 flash->chip_restore_fn_count++;
540
541 return 0;
542}
543
544static int deregister_chip_restore(struct flashctx *flash)
545{
546 int rc = 0;
547
548 while (flash->chip_restore_fn_count > 0) {
549 int i = --flash->chip_restore_fn_count;
550 rc |= flash->chip_restore_fn[i].func(
551 flash, flash->chip_restore_fn[i].status);
552 }
553
554 return rc;
555}
556
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000557int programmer_init(enum programmer prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000558{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000559 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000560
561 if (prog >= PROGRAMMER_INVALID) {
562 msg_perr("Invalid programmer specified!\n");
563 return -1;
564 }
565 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000566 /* Initialize all programmer specific data. */
567 /* Default to unlimited decode sizes. */
568 max_rom_decode = (const struct decode_sizes) {
569 .parallel = 0xffffffff,
570 .lpc = 0xffffffff,
571 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000572 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000573 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000574 /* Default to top aligned flash at 4 GB. */
575 flashbase = 0;
576 /* Registering shutdown functions is now allowed. */
577 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000578 /* Default to allowing writes. Broken programmers set this to 0. */
579 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000580
581 programmer_param = param;
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000582 msg_pdbg("Initializing %s programmer\n", programmer_table[programmer].name);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000583 ret = programmer_table[programmer].init();
584 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000585 if (ret != 0) {
586 /* It is quite possible that any unhandled programmer parameter would have been valid,
587 * but an error in actual programmer init happened before the parameter was evaluated.
588 */
589 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
590 programmer_param);
591 } else {
592 /* Actual programmer init was successful, but the user specified an invalid or unusable
593 * (for the current programmer configuration) parameter.
594 */
595 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
596 msg_perr("Aborting.\n");
597 ret = ERROR_FATAL;
598 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000599 }
600 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000601}
602
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000603/** Calls registered shutdown functions and resets internal programmer-related variables.
604 * Calling it is safe even without previous initialization, but further interactions with programmer support
605 * require a call to programmer_init() (afterwards).
606 *
607 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000608int programmer_shutdown(void)
609{
David Hendricks8bb20212011-06-14 01:35:36 +0000610 int ret = 0;
611
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000612 /* Registering shutdown functions is no longer allowed. */
613 may_register_shutdown = 0;
614 while (shutdown_fn_count > 0) {
615 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000616 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000617 }
Stefan Taunere34e3e82013-01-01 00:06:51 +0000618
Stefan Taunerb8911d62012-12-26 07:55:00 +0000619 programmer_param = NULL;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000620 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000621
David Hendricks8bb20212011-06-14 01:35:36 +0000622 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000623}
624
Stefan Tauner305e0b92013-07-17 23:46:44 +0000625void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000626{
Stefan Tauner26e7a152013-09-13 17:21:05 +0000627 void *ret = programmer_table[programmer].map_flash_region(descr, phys_addr, len);
628 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
629 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
630 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000631}
632
633void programmer_unmap_flash_region(void *virt_addr, size_t len)
634{
635 programmer_table[programmer].unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000636 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000637}
638
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000639void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000640{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000641 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000642}
643
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000644void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000645{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000646 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000647}
648
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000649void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000650{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000651 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000652}
653
Mark Marshallf20b7be2014-05-09 21:16:21 +0000654void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000655{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000656 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000657}
658
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000659uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000660{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000661 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000662}
663
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000664uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000665{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000666 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000667}
668
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000669uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000670{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000671 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000672}
673
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000674void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
675 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000676{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000677 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000678}
679
Stefan Taunerf80419c2014-05-02 15:41:42 +0000680void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000681{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000682 if (usecs > 0)
683 programmer_table[programmer].delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000684}
685
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000686int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
687 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000688{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000689 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000690
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000691 return 0;
692}
693
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000694/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000695 * It will look for needle with a subsequent '=' in haystack, return a copy of
696 * needle and remove everything from the first occurrence of needle to the next
697 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000698 */
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000699char *extract_param(const char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000700{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000701 char *param_pos, *opt_pos, *rest;
702 char *opt = NULL;
703 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000704 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000705
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000706 needlelen = strlen(needle);
707 if (!needlelen) {
708 msg_gerr("%s: empty needle! Please report a bug at "
709 "flashrom@flashrom.org\n", __func__);
710 return NULL;
711 }
712 /* No programmer parameters given. */
713 if (*haystack == NULL)
714 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000715 param_pos = strstr(*haystack, needle);
716 do {
717 if (!param_pos)
718 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000719 /* Needle followed by '='? */
720 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000721 /* Beginning of the string? */
722 if (param_pos == *haystack)
723 break;
724 /* After a delimiter? */
725 if (strchr(delim, *(param_pos - 1)))
726 break;
727 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000728 /* Continue searching. */
729 param_pos++;
730 param_pos = strstr(param_pos, needle);
731 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000732
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000733 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000734 /* Get the string after needle and '='. */
735 opt_pos = param_pos + needlelen + 1;
736 optlen = strcspn(opt_pos, delim);
737 /* Return an empty string if the parameter was empty. */
738 opt = malloc(optlen + 1);
739 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000740 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000741 exit(1);
742 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000743 strncpy(opt, opt_pos, optlen);
744 opt[optlen] = '\0';
745 rest = opt_pos + optlen;
746 /* Skip all delimiters after the current parameter. */
747 rest += strspn(rest, delim);
748 memmove(param_pos, rest, strlen(rest) + 1);
749 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000750 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000751
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000752 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000753}
754
Stefan Tauner66652442011-06-26 17:38:17 +0000755char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000756{
757 return extract_param(&programmer_param, param_name, ",");
758}
759
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000760/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000761static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000762{
763 unsigned int usable_erasefunctions = 0;
764 int k;
765 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
766 if (!check_block_eraser(flash, k, 0))
767 usable_erasefunctions++;
768 }
769 return usable_erasefunctions;
770}
771
Mark Marshallf20b7be2014-05-09 21:16:21 +0000772static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000773{
774 int ret = 0, failcount = 0;
775 unsigned int i;
776 for (i = 0; i < len; i++) {
777 if (wantbuf[i] != havebuf[i]) {
778 /* Only print the first failure. */
779 if (!failcount++)
780 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
781 start + i, wantbuf[i], havebuf[i]);
782 }
783 }
784 if (failcount) {
785 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
786 start, start + len - 1, failcount);
787 ret = -1;
788 }
789 return ret;
790}
791
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000792/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600793static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000794{
795 int ret;
796 uint8_t *cmpbuf = malloc(len);
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300797 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000798
799 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000800 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000801 exit(1);
802 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300803 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000804 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000805 free(cmpbuf);
806 return ret;
807}
808
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000809/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000810 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000811 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000812 * @start offset to the base address of the flash chip
813 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000814 * @return 0 for success, -1 for failure
815 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000816int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000817{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000818 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000819 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000820
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000821 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000822 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000823 return -1;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000824 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000825
826 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000827 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000828 msg_gerr("Could not allocate memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000829 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000830 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000831 int ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000832
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000833 if (start + len > flash->chip->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000834 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000835 " total_size 0x%x\n", __func__, start, len,
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000836 flash->chip->total_size * 1024);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000837 ret = -1;
838 goto out_free;
839 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000840
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000841 ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000842 if (ret) {
843 msg_gerr("Verification impossible because read failed "
844 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000845 ret = -1;
846 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000847 }
848
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000849 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000850out_free:
851 free(readbuf);
852 return ret;
853}
854
Stefan Tauner02437452013-04-01 19:34:53 +0000855/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300856static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
857 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000858{
859 unsigned int i, j, limit;
860 for (j = 0; j < len / gran; j++) {
861 limit = min (gran, len - j * gran);
862 /* Are 'have' and 'want' identical? */
863 if (!memcmp(have + j * gran, want + j * gran, limit))
864 continue;
865 /* have needs to be in erased state. */
866 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300867 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000868 return 1;
869 }
870 return 0;
871}
872
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000873/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000874 * Check if the buffer @have can be programmed to the content of @want without
875 * erasing. This is only possible if all chunks of size @gran are either kept
876 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000877 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000878 * Warning: This function assumes that @have and @want point to naturally
879 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000880 *
881 * @have buffer with current content
882 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000883 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000884 * @gran write granularity (enum, not count)
885 * @return 0 if no erase is needed, 1 otherwise
886 */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300887int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
888 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000889{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000890 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000891 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000892
893 switch (gran) {
894 case write_gran_1bit:
895 for (i = 0; i < len; i++)
896 if ((have[i] & want[i]) != want[i]) {
897 result = 1;
898 break;
899 }
900 break;
901 case write_gran_1byte:
902 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300903 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000904 result = 1;
905 break;
906 }
907 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000908 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300909 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000910 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000911 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300912 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000913 break;
914 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300915 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000916 break;
917 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300918 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000919 break;
920 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300921 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000922 break;
923 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300924 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000925 break;
926 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300927 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000928 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000929 case write_gran_1byte_implicit_erase:
930 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
931 result = 0;
932 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000933 default:
934 msg_cerr("%s: Unsupported granularity! Please report a bug at "
935 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000936 }
937 return result;
938}
939
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000940/**
941 * Check if the buffer @have needs to be programmed to get the content of @want.
942 * If yes, return 1 and fill in first_start with the start address of the
943 * write operation and first_len with the length of the first to-be-written
944 * chunk. If not, return 0 and leave first_start and first_len undefined.
945 *
946 * Warning: This function assumes that @have and @want point to naturally
947 * aligned regions.
948 *
949 * @have buffer with current content
950 * @want buffer with desired content
951 * @len length of the checked area
952 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000953 * @first_start offset of the first byte which needs to be written (passed in
954 * value is increased by the offset of the first needed write
955 * relative to have/want or unchanged if no write is needed)
956 * @return length of the first contiguous area which needs to be written
957 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000958 *
959 * FIXME: This function needs a parameter which tells it about coalescing
960 * in relation to the max write length of the programmer and the max write
961 * length of the chip.
962 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000963static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000964 unsigned int *first_start,
965 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000966{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000967 int need_write = 0;
968 unsigned int rel_start = 0, first_len = 0;
969 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000970
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000971 switch (gran) {
972 case write_gran_1bit:
973 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000974 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000975 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000976 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000977 case write_gran_128bytes:
978 stride = 128;
979 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000980 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000981 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000982 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000983 case write_gran_264bytes:
984 stride = 264;
985 break;
986 case write_gran_512bytes:
987 stride = 512;
988 break;
989 case write_gran_528bytes:
990 stride = 528;
991 break;
992 case write_gran_1024bytes:
993 stride = 1024;
994 break;
995 case write_gran_1056bytes:
996 stride = 1056;
997 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000998 default:
999 msg_cerr("%s: Unsupported granularity! Please report a bug at "
1000 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001001 /* Claim that no write was needed. A write with unknown
1002 * granularity is too dangerous to try.
1003 */
1004 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001005 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001006 for (i = 0; i < len / stride; i++) {
1007 limit = min(stride, len - i * stride);
1008 /* Are 'have' and 'want' identical? */
1009 if (memcmp(have + i * stride, want + i * stride, limit)) {
1010 if (!need_write) {
1011 /* First location where have and want differ. */
1012 need_write = 1;
1013 rel_start = i * stride;
1014 }
1015 } else {
1016 if (need_write) {
1017 /* First location where have and want
1018 * do not differ anymore.
1019 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001020 break;
1021 }
1022 }
1023 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +00001024 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001025 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001026 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001027 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001028}
1029
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001030/* Returns the number of busses commonly supported by the current programmer and flash chip where the latter
1031 * can not be completely accessed due to size/address limits of the programmer. */
1032unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001033{
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001034 unsigned int limitexceeded = 0;
1035 uint32_t size = flash->chip->total_size * 1024;
1036 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001037
1038 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001039 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001040 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001041 "size %u kB of chipset/board/programmer "
1042 "for %s interface, "
1043 "probe/read/erase/write may fail. ", size / 1024,
1044 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001045 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001046 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001047 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001048 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001049 "size %u kB of chipset/board/programmer "
1050 "for %s interface, "
1051 "probe/read/erase/write may fail. ", size / 1024,
1052 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001053 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001054 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001055 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001056 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001057 "size %u kB of chipset/board/programmer "
1058 "for %s interface, "
1059 "probe/read/erase/write may fail. ", size / 1024,
1060 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001061 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001062 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001063 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001064 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001065 "size %u kB of chipset/board/programmer "
1066 "for %s interface, "
1067 "probe/read/erase/write may fail. ", size / 1024,
1068 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001069 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001070 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001071}
1072
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001073void unmap_flash(struct flashctx *flash)
1074{
1075 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
1076 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
1077 flash->physical_registers = 0;
1078 flash->virtual_registers = (chipaddr)ERROR_PTR;
1079 }
1080
1081 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
1082 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
1083 flash->physical_memory = 0;
1084 flash->virtual_memory = (chipaddr)ERROR_PTR;
1085 }
1086}
1087
1088int map_flash(struct flashctx *flash)
1089{
1090 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
1091 flash->virtual_memory = (chipaddr)ERROR_PTR;
1092 flash->virtual_registers = (chipaddr)ERROR_PTR;
1093
1094 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
1095 * These are used for various probing-related hacks that would not map successfully anyway and should be
1096 * removed ASAP. */
1097 if (flash->chip->total_size == 0)
1098 return 0;
1099
1100 const chipsize_t size = flash->chip->total_size * 1024;
1101 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
1102 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
1103 if (addr == ERROR_PTR) {
1104 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
1105 flash->chip->name, PRIxPTR_WIDTH, base);
1106 return 1;
1107 }
1108 flash->physical_memory = base;
1109 flash->virtual_memory = (chipaddr)addr;
1110
1111 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
1112 * completely different on some chips and programmers, or not mappable at all.
1113 * Ignore these problems for now and always report success. */
1114 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
1115 base = 0xffffffff - size - 0x400000 + 1;
1116 addr = programmer_map_flash_region("flash chip registers", base, size);
1117 if (addr == ERROR_PTR) {
1118 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
1119 flash->chip->name, PRIxPTR_WIDTH, base);
1120 return 0;
1121 }
1122 flash->physical_registers = base;
1123 flash->virtual_registers = (chipaddr)addr;
1124 }
1125 return 0;
1126}
1127
Nico Huber2d625722016-05-03 10:48:02 +02001128/*
1129 * Return a string corresponding to the bustype parameter.
1130 * Memory is obtained with malloc() and must be freed with free() by the caller.
1131 */
1132char *flashbuses_to_text(enum chipbustype bustype)
1133{
1134 char *ret = calloc(1, 1);
1135 /*
1136 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
1137 * will cease to exist and should be eliminated here as well.
1138 */
1139 if (bustype == BUS_NONSPI) {
1140 ret = strcat_realloc(ret, "Non-SPI, ");
1141 } else {
1142 if (bustype & BUS_PARALLEL)
1143 ret = strcat_realloc(ret, "Parallel, ");
1144 if (bustype & BUS_LPC)
1145 ret = strcat_realloc(ret, "LPC, ");
1146 if (bustype & BUS_FWH)
1147 ret = strcat_realloc(ret, "FWH, ");
1148 if (bustype & BUS_SPI)
1149 ret = strcat_realloc(ret, "SPI, ");
1150 if (bustype & BUS_PROG)
1151 ret = strcat_realloc(ret, "Programmer-specific, ");
1152 if (bustype == BUS_NONE)
1153 ret = strcat_realloc(ret, "None, ");
1154 }
1155 /* Kill last comma. */
1156 ret[strlen(ret) - 2] = '\0';
1157 ret = realloc(ret, strlen(ret) + 1);
1158 return ret;
1159}
1160
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001161int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001162{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001163 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001164 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001165 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001166
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001167 for (chip = flashchips + startchip; chip && chip->name; chip++) {
1168 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +00001169 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001170 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001171 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +00001172 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +03001173 /* Only probe for SPI25 chips by default. */
1174 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
1175 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001176 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
1177 if (!chip->probe && !force) {
1178 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001179 continue;
1180 }
Stefan Reinauer70385642007-04-06 11:58:03 +00001181
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001182 /* Start filling in the dynamic data. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001183 flash->chip = calloc(1, sizeof(struct flashchip));
1184 if (!flash->chip) {
1185 msg_gerr("Out of memory!\n");
1186 exit(1);
1187 }
1188 memcpy(flash->chip, chip, sizeof(struct flashchip));
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001189 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001190
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001191 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +01001192 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001193
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001194 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
1195 * is only called with force=1 after normal probing failed.
1196 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001197 if (force)
1198 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001199
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001200 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +00001201 goto notfound;
1202
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001203 /* If this is the first chip found, accept it.
1204 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001205 * a non-generic match. SFDP and CFI are generic matches.
1206 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001207 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001208 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001209 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001210 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001211 msg_cinfo("===\n"
1212 "SFDP has autodetected a flash chip which is "
1213 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001214 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001215 msg_cinfo("The standard operations read and "
1216 "verify should work, but to support "
1217 "erase, write and all other "
1218 "possible features");
1219 else
1220 msg_cinfo("All standard operations (read, "
1221 "verify, erase and write) should "
1222 "work, but to support all possible "
1223 "features");
1224
Stefan Taunerb4e06bd2012-08-20 00:24:22 +00001225 msg_cinfo(" we need to add them manually.\n"
1226 "You can help us by mailing us the output of the following command to "
1227 "flashrom@flashrom.org:\n"
1228 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
1229 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001230 "===\n");
1231 }
1232
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001233 /* First flash chip detected on this bus. */
1234 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001235 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001236 /* Not the first flash chip detected on this bus, but not a generic match either. */
1237 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
1238 break;
1239 /* 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 +00001240notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001241 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001242 free(flash->chip);
1243 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001244 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001245
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001246 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001247 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001248
Nico Huber7af0e792016-04-29 16:40:15 +02001249 /* Fill fallback layout covering the whole chip. */
1250 struct single_layout *const fallback = &flash->fallback_layout;
1251 fallback->base.entries = &fallback->entry;
1252 fallback->base.num_entries = 1;
1253 fallback->entry.start = 0;
1254 fallback->entry.end = flash->chip->total_size * 1024 - 1;
1255 fallback->entry.included = true;
Nico Huber70461a92019-06-15 14:56:19 +02001256 fallback->entry.name = strdup("complete flash");
1257 if (!fallback->entry.name) {
1258 msg_cerr("Failed to probe chip: %s\n", strerror(errno));
1259 return -1;
1260 }
Stefan Reinauer051e2362011-01-19 06:21:54 +00001261
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001262 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001263 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
1264 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +00001265 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001266#if CONFIG_INTERNAL == 1
1267 if (programmer_table[programmer].map_flash_region == physmap)
1268 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
1269 PRIxPTR_WIDTH, flash->physical_memory);
1270 else
1271#endif
1272 msg_cinfo("on %s.\n", programmer_table[programmer].name);
Uwe Hermann9899cad2009-06-28 21:47:57 +00001273
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001274 /* Flash registers may more likely not be mapped if the chip was forced.
1275 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +00001276 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001277 if (flash->chip->printlock)
1278 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +00001279
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001280 /* Get out of the way for later runs. */
1281 unmap_flash(flash);
1282
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001283 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001284 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001285}
1286
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001287int read_buf_from_file(unsigned char *buf, unsigned long size,
1288 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001289{
Nico Huber7562f7d2013-08-30 21:29:45 +00001290#ifdef __LIBPAYLOAD__
1291 msg_gerr("Error: No file I/O support in libpayload\n");
1292 return 1;
1293#else
Stefan Tauner16687702015-12-25 21:59:45 +00001294 int ret = 0;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001295
Stefan Tauner16687702015-12-25 21:59:45 +00001296 FILE *image;
Daniel Campello8eaef7d2021-04-15 10:36:04 -06001297 if (!strcmp(filename, "-"))
1298 image = fdopen(fileno(stdin), "rb");
1299 else
1300 image = fopen(filename, "rb");
1301 if (image == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001302 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001303 return 1;
1304 }
Stefan Tauner16687702015-12-25 21:59:45 +00001305
1306 struct stat image_stat;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001307 if (fstat(fileno(image), &image_stat) != 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001308 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
Stefan Tauner16687702015-12-25 21:59:45 +00001309 ret = 1;
1310 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001311 }
Daniel Campello8eaef7d2021-04-15 10:36:04 -06001312 if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +00001313 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 +00001314 (intmax_t)image_stat.st_size, size);
Stefan Tauner16687702015-12-25 21:59:45 +00001315 ret = 1;
1316 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001317 }
Stefan Tauner16687702015-12-25 21:59:45 +00001318
1319 unsigned long numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001320 if (numbytes != size) {
1321 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1322 "wanted %ld!\n", numbytes, size);
Stefan Tauner16687702015-12-25 21:59:45 +00001323 ret = 1;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001324 }
Stefan Tauner16687702015-12-25 21:59:45 +00001325out:
1326 (void)fclose(image);
1327 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001328#endif
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001329}
1330
Mark Marshallf20b7be2014-05-09 21:16:21 +00001331int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001332{
Nico Huber7562f7d2013-08-30 21:29:45 +00001333#ifdef __LIBPAYLOAD__
1334 msg_gerr("Error: No file I/O support in libpayload\n");
1335 return 1;
1336#else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001337 FILE *image;
Stefan Tauner16687702015-12-25 21:59:45 +00001338 int ret = 0;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001339
1340 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001341 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001342 return 1;
1343 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001344 if ((image = fopen(filename, "wb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001345 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001346 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001347 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001348
Stefan Tauner16687702015-12-25 21:59:45 +00001349 unsigned long numbytes = fwrite(buf, 1, size, image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001350 if (numbytes != size) {
Stefan Tauner16687702015-12-25 21:59:45 +00001351 msg_gerr("Error: file %s could not be written completely.\n", filename);
1352 ret = 1;
1353 goto out;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001354 }
Stefan Tauner16687702015-12-25 21:59:45 +00001355 if (fflush(image)) {
1356 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
1357 ret = 1;
1358 }
1359 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
1360#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
1361 struct stat image_stat;
1362 if (fstat(fileno(image), &image_stat) != 0) {
1363 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
1364 ret = 1;
1365 goto out;
1366 }
1367 if (S_ISREG(image_stat.st_mode)) {
1368 if (fsync(fileno(image))) {
1369 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
1370 ret = 1;
1371 }
1372 }
1373#endif
1374out:
1375 if (fclose(image)) {
1376 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
1377 ret = 1;
1378 }
1379 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001380#endif
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001381}
1382
Nico Huber899e4ec2016-04-29 18:39:01 +02001383static int read_by_layout(struct flashctx *, uint8_t *);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001384int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001385{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001386 unsigned long size = flash->chip->total_size * 1024;
Richard Hughes84b453e2018-12-19 15:30:39 +00001387 unsigned char *buf = calloc(size, sizeof(unsigned char));
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001388 int ret = 0;
1389
1390 msg_cinfo("Reading flash... ");
1391 if (!buf) {
1392 msg_gerr("Memory allocation failed!\n");
1393 msg_cinfo("FAILED.\n");
1394 return 1;
1395 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001396 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001397 msg_cerr("No read function available for this flash chip.\n");
1398 ret = 1;
1399 goto out_free;
1400 }
Nico Huber899e4ec2016-04-29 18:39:01 +02001401 if (read_by_layout(flash, buf)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001402 msg_cerr("Read operation failed!\n");
1403 ret = 1;
1404 goto out_free;
1405 }
1406
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001407 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001408out_free:
1409 free(buf);
1410 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1411 return ret;
1412}
1413
Stefan Tauner96658be2014-05-26 22:05:31 +00001414/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001415static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001416{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001417 int i, j, k;
1418 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +05301419 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001420
1421 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1422 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001423 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +05301424 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001425
1426 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1427 /* Blocks with zero size are bugs in flashchips.c. */
1428 if (eraser.eraseblocks[i].count &&
1429 !eraser.eraseblocks[i].size) {
1430 msg_gerr("ERROR: Flash chip %s erase function "
1431 "%i region %i has size 0. Please report"
1432 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001433 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001434 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001435 }
1436 /* Blocks with zero count are bugs in flashchips.c. */
1437 if (!eraser.eraseblocks[i].count &&
1438 eraser.eraseblocks[i].size) {
1439 msg_gerr("ERROR: Flash chip %s erase function "
1440 "%i region %i has count 0. Please report"
1441 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001442 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001443 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001444 }
1445 done += eraser.eraseblocks[i].count *
1446 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +05301447 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001448 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001449 /* Empty eraseblock definition with erase function. */
1450 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001451 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001452 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001453 if (!done)
1454 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001455 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001456 msg_gerr("ERROR: Flash chip %s erase function %i "
1457 "region walking resulted in 0x%06x bytes total,"
1458 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001459 " flashrom@flashrom.org\n", chip->name, k,
1460 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001461 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001462 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001463 if (!eraser.block_erase)
1464 continue;
1465 /* Check if there are identical erase functions for different
1466 * layouts. That would imply "magic" erase functions. The
1467 * easiest way to check this is with function pointers.
1468 */
Uwe Hermann43959702010-03-13 17:28:29 +00001469 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001470 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001471 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001472 msg_gerr("ERROR: Flash chip %s erase function "
1473 "%i and %i are identical. Please report"
1474 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001475 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001476 ret = 1;
1477 }
Uwe Hermann43959702010-03-13 17:28:29 +00001478 }
Aarya Chaumal478e1792022-06-04 01:34:44 +05301479 if(curr_eraseblock_count > prev_eraseblock_count)
1480 {
1481 msg_gerr("ERROR: Flash chip %s erase function %i is not "
1482 "in order. Please report a bug at flashrom@flashrom.org\n",
1483 chip->name, k);
1484 ret = 1;
1485 }
1486 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001487 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001488 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001489}
1490
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001491static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001492{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001493 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001494
1495 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1496 if (log)
1497 msg_cdbg("not defined. ");
1498 return 1;
1499 }
1500 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1501 if (log)
1502 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001503 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001504 return 1;
1505 }
1506 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1507 if (log)
1508 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001509 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001510 return 1;
1511 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001512 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001513 return 0;
1514}
1515
Nico Huber7af0e792016-04-29 16:40:15 +02001516/**
1517 * @brief Reads the included layout regions into a buffer.
1518 *
1519 * If there is no layout set in the given flash context, the whole chip will
1520 * be read.
1521 *
1522 * @param flashctx Flash context to be used.
1523 * @param buffer Buffer of full chip size to read into.
1524 * @return 0 on success,
1525 * 1 if any read fails.
1526 */
1527static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1528{
1529 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001530 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001531
Nico Huber5ca55232019-06-15 22:29:08 +02001532 while ((entry = layout_next_included(layout, entry))) {
1533 const chipoff_t region_start = entry->start;
1534 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001535
1536 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1537 return 1;
1538 }
1539 return 0;
1540}
1541
1542typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1543/**
1544 * @private
1545 *
1546 * For read-erase-write, `curcontents` and `newcontents` shall point
1547 * to buffers of the chip's size. Both are supposed to be prefilled
1548 * with at least the included layout regions of the current flash
1549 * contents (`curcontents`) and the data to be written to the flash
1550 * (`newcontents`).
1551 *
1552 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1553 *
1554 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1555 */
1556struct walk_info {
1557 uint8_t *curcontents;
1558 const uint8_t *newcontents;
1559 chipoff_t region_start;
1560 chipoff_t region_end;
1561 chipoff_t erase_start;
1562 chipoff_t erase_end;
1563};
1564/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1565typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1566
1567static int walk_eraseblocks(struct flashctx *const flashctx,
1568 struct walk_info *const info,
1569 const size_t erasefunction, const per_blockfn_t per_blockfn)
1570{
1571 int ret;
1572 size_t i, j;
1573 bool first = true;
1574 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1575
1576 info->erase_start = 0;
1577 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1578 /* count==0 for all automatically initialized array
1579 members so the loop below won't be executed for them. */
1580 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1581 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1582
1583 /* Skip any eraseblock that is completely outside the current region. */
1584 if (info->erase_end < info->region_start)
1585 continue;
1586 if (info->region_end < info->erase_start)
1587 break;
1588
1589 /* Print this for every block except the first one. */
1590 if (first)
1591 first = false;
1592 else
1593 msg_cdbg(", ");
1594 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1595
1596 ret = per_blockfn(flashctx, info, eraser->block_erase);
1597 if (ret)
1598 return ret;
1599 }
1600 if (info->region_end < info->erase_start)
1601 break;
1602 }
1603 msg_cdbg("\n");
1604 return 0;
1605}
1606
1607static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1608 const per_blockfn_t per_blockfn)
1609{
1610 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001611 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001612
1613 all_skipped = true;
1614 msg_cinfo("Erasing and writing flash chip... ");
1615
Nico Huber5ca55232019-06-15 22:29:08 +02001616 while ((entry = layout_next_included(layout, entry))) {
1617 info->region_start = entry->start;
1618 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001619
1620 size_t j;
1621 int error = 1; /* retry as long as it's 1 */
1622 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1623 if (j != 0)
1624 msg_cinfo("Looking for another erase function.\n");
1625 msg_cdbg("Trying erase function %zi... ", j);
1626 if (check_block_eraser(flashctx, j, 1))
1627 continue;
1628
1629 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1630 if (error != 1)
1631 break;
1632
1633 if (info->curcontents) {
1634 msg_cinfo("Reading current flash chip contents... ");
1635 if (read_by_layout(flashctx, info->curcontents)) {
1636 /* Now we are truly screwed. Read failed as well. */
1637 msg_cerr("Can't read anymore! Aborting.\n");
1638 /* We have no idea about the flash chip contents, so
1639 retrying with another erase function is pointless. */
1640 error = 2;
1641 break;
1642 }
1643 msg_cinfo("done. ");
1644 }
1645 }
1646 if (error == 1)
1647 msg_cinfo("No usable erase functions left.\n");
1648 if (error) {
1649 msg_cerr("FAILED!\n");
1650 return 1;
1651 }
1652 }
1653 if (all_skipped)
1654 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1655 msg_cinfo("Erase/write done.\n");
1656 return 0;
1657}
1658
1659static int erase_block(struct flashctx *const flashctx,
1660 const struct walk_info *const info, const erasefn_t erasefn)
1661{
1662 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001663 const bool region_unaligned = info->region_start > info->erase_start ||
1664 info->erase_end > info->region_end;
1665 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1666 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001667
Nico Huber6e61e0c2019-01-23 17:07:49 +01001668 /*
1669 * If the region is not erase-block aligned, merge current flash con-
1670 * tents into a new buffer `backup_contents`.
1671 */
1672 if (region_unaligned) {
1673 backup_contents = malloc(erase_len);
1674 erased_contents = malloc(erase_len);
1675 if (!backup_contents || !erased_contents) {
1676 msg_cerr("Out of memory!\n");
1677 ret = 1;
1678 goto _free_ret;
1679 }
1680 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1681 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1682
1683 msg_cdbg("R");
1684 /* Merge data preceding the current region. */
1685 if (info->region_start > info->erase_start) {
1686 const chipoff_t start = info->erase_start;
1687 const chipsize_t len = info->region_start - info->erase_start;
1688 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1689 msg_cerr("Can't read! Aborting.\n");
1690 goto _free_ret;
1691 }
1692 }
1693 /* Merge data following the current region. */
1694 if (info->erase_end > info->region_end) {
1695 const chipoff_t start = info->region_end + 1;
1696 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1697 const chipsize_t len = info->erase_end - info->region_end;
1698 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1699 msg_cerr("Can't read! Aborting.\n");
1700 goto _free_ret;
1701 }
1702 }
1703 }
1704
1705 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001706 all_skipped = false;
1707
1708 msg_cdbg("E");
1709 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001710 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001711 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1712 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001713 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001714 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001715
1716 if (region_unaligned) {
1717 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1718 /* get_next_write() sets starthere to a new value after the call. */
1719 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1720 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1721 if (!writecount++)
1722 msg_cdbg("W");
1723 /* Needs the partial write function signature. */
1724 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1725 info->erase_start + starthere, lenhere))
1726 goto _free_ret;
1727 starthere += lenhere;
1728 }
1729 }
1730
1731 ret = 0;
1732
1733_free_ret:
1734 free(erased_contents);
1735 free(backup_contents);
1736 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001737}
1738
1739/**
1740 * @brief Erases the included layout regions.
1741 *
1742 * If there is no layout set in the given flash context, the whole chip will
1743 * be erased.
1744 *
1745 * @param flashctx Flash context to be used.
1746 * @param buffer Buffer of full chip size to read into.
1747 * @return 0 on success,
1748 * 1 if all available erase functions failed.
1749 */
Nico Huber454f6132012-12-10 13:34:10 +00001750static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001751{
1752 struct walk_info info = { 0 };
1753 return walk_by_layout(flashctx, &info, &erase_block);
1754}
1755
1756static int read_erase_write_block(struct flashctx *const flashctx,
1757 const struct walk_info *const info, const erasefn_t erasefn)
1758{
1759 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1760 const bool region_unaligned = info->region_start > info->erase_start ||
1761 info->erase_end > info->region_end;
1762 const uint8_t *newcontents = NULL;
1763 int ret = 2;
1764
1765 /*
1766 * If the region is not erase-block aligned, merge current flash con-
1767 * tents into `info->curcontents` and a new buffer `newc`. The former
1768 * is necessary since we have no guarantee that the full erase block
1769 * was already read into `info->curcontents`. For the latter a new
1770 * buffer is used since `info->newcontents` might contain data for
1771 * other unaligned regions that touch this erase block too.
1772 */
1773 if (region_unaligned) {
1774 msg_cdbg("R");
1775 uint8_t *const newc = malloc(erase_len);
1776 if (!newc) {
1777 msg_cerr("Out of memory!\n");
1778 return 1;
1779 }
1780 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1781
1782 /* Merge data preceding the current region. */
1783 if (info->region_start > info->erase_start) {
1784 const chipoff_t start = info->erase_start;
1785 const chipsize_t len = info->region_start - info->erase_start;
1786 if (flashctx->chip->read(flashctx, newc, start, len)) {
1787 msg_cerr("Can't read! Aborting.\n");
1788 goto _free_ret;
1789 }
1790 memcpy(info->curcontents + start, newc, len);
1791 }
1792 /* Merge data following the current region. */
1793 if (info->erase_end > info->region_end) {
1794 const chipoff_t start = info->region_end + 1;
1795 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1796 const chipsize_t len = info->erase_end - info->region_end;
1797 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1798 msg_cerr("Can't read! Aborting.\n");
1799 goto _free_ret;
1800 }
1801 memcpy(info->curcontents + start, newc + rel_start, len);
1802 }
1803
1804 newcontents = newc;
1805 } else {
1806 newcontents = info->newcontents + info->erase_start;
1807 }
1808
1809 ret = 1;
1810 bool skipped = true;
1811 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001812 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001813 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1814 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001815 if (erase_block(flashctx, info, erasefn))
1816 goto _free_ret;
1817 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001818 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001819 skipped = false;
1820 }
1821
1822 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1823 /* get_next_write() sets starthere to a new value after the call. */
1824 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1825 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1826 if (!writecount++)
1827 msg_cdbg("W");
1828 /* Needs the partial write function signature. */
1829 if (flashctx->chip->write(flashctx, newcontents + starthere,
1830 info->erase_start + starthere, lenhere))
1831 goto _free_ret;
1832 starthere += lenhere;
1833 skipped = false;
1834 }
1835 if (skipped)
1836 msg_cdbg("S");
1837 else
1838 all_skipped = false;
1839
1840 /* Update curcontents, other regions with overlapping erase blocks
1841 might rely on this. */
1842 memcpy(curcontents, newcontents, erase_len);
1843 ret = 0;
1844
1845_free_ret:
1846 if (region_unaligned)
1847 free((void *)newcontents);
1848 return ret;
1849}
1850
1851/**
1852 * @brief Writes the included layout regions from a given image.
1853 *
1854 * If there is no layout set in the given flash context, the whole image
1855 * will be written.
1856 *
1857 * @param flashctx Flash context to be used.
1858 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1859 * @param newcontents The new image to be written.
1860 * @return 0 on success,
1861 * 1 if anything has gone wrong.
1862 */
Nico Huber454f6132012-12-10 13:34:10 +00001863static int write_by_layout(struct flashctx *const flashctx,
1864 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001865{
1866 struct walk_info info;
1867 info.curcontents = curcontents;
1868 info.newcontents = newcontents;
1869 return walk_by_layout(flashctx, &info, read_erase_write_block);
1870}
1871
1872/**
1873 * @brief Compares the included layout regions with content from a buffer.
1874 *
1875 * If there is no layout set in the given flash context, the whole chip's
1876 * contents will be compared.
1877 *
1878 * @param flashctx Flash context to be used.
1879 * @param curcontents A buffer of full chip size to read current chip contents into.
1880 * @param newcontents The new image to compare to.
1881 * @return 0 on success,
1882 * 1 if reading failed,
1883 * 3 if the contents don't match.
1884 */
Nico Huber454f6132012-12-10 13:34:10 +00001885static int verify_by_layout(struct flashctx *const flashctx,
1886 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001887{
1888 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001889 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001890
Nico Huber5ca55232019-06-15 22:29:08 +02001891 while ((entry = layout_next_included(layout, entry))) {
1892 const chipoff_t region_start = entry->start;
1893 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001894
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"
Angel Pons1900e1d2021-07-02 12:42:23 +02001910 "chip. Please report this to the mailing list at flashrom@flashrom.org or on\n"
1911 "IRC (see https://www.flashrom.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001912 "-------------------------------------------------------------------------------\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"
Angel Pons1900e1d2021-07-02 12:42:23 +02001918 "please report this to the mailing list at flashrom@flashrom.org or on IRC (see\n"
1919 "https://www.flashrom.org/Contact for details), 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)
Angel Pons1900e1d2021-07-02 12:42:23 +02001927 msg_gerr("Get help on IRC (see https://www.flashrom.org/Contact) or mail\n"
1928 "flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!"
Stefan Tauner136388f2013-07-15 10:47:53 +00001929 "-------------------------------------------------------------------------------\n"
1930 "DO NOT REBOOT OR POWEROFF!\n");
1931 else
1932#endif
Angel Pons1900e1d2021-07-02 12:42:23 +02001933 msg_gerr("Please report this to the mailing list at flashrom@flashrom.org or\n"
1934 "on IRC (see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001935}
1936
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001937void list_programmers_linebreak(int startcol, int cols, int paren)
1938{
1939 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001940 int pnamelen;
1941 int remaining = 0, firstline = 1;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001942 enum programmer p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001943 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001944
1945 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1946 pname = programmer_table[p].name;
1947 pnamelen = strlen(pname);
1948 if (remaining - pnamelen - 2 < 0) {
1949 if (firstline)
1950 firstline = 0;
1951 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001952 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001953 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001954 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001955 remaining = cols - startcol;
1956 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001957 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001958 remaining--;
1959 }
1960 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001961 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001962 remaining--;
1963 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001964 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001965 remaining -= pnamelen;
1966 if (p < PROGRAMMER_INVALID - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001967 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001968 remaining--;
1969 } else {
1970 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001971 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001972 }
1973 }
1974}
1975
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001976static void print_sysinfo(void)
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001977{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001978#if IS_WINDOWS
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001979 SYSTEM_INFO si;
1980 OSVERSIONINFOEX osvi;
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001981
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001982 memset(&si, 0, sizeof(SYSTEM_INFO));
1983 memset(&osvi, 0, sizeof(OSVERSIONINFOEX));
1984 msg_ginfo(" on Windows");
1985 /* Tell Windows which version of the structure we want. */
1986 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1987 if (GetVersionEx((OSVERSIONINFO*) &osvi))
1988 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
1989 else
1990 msg_ginfo(" unknown version");
1991 GetSystemInfo(&si);
1992 switch (si.wProcessorArchitecture) {
1993 case PROCESSOR_ARCHITECTURE_AMD64:
1994 msg_ginfo(" (x86_64)");
1995 break;
1996 case PROCESSOR_ARCHITECTURE_INTEL:
1997 msg_ginfo(" (x86)");
1998 break;
1999 default:
2000 msg_ginfo(" (unknown arch)");
2001 break;
2002 }
2003#elif HAVE_UTSNAME == 1
2004 struct utsname osinfo;
2005
2006 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002007 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
2008 osinfo.machine);
2009#else
2010 msg_ginfo(" on unknown machine");
2011#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002012}
2013
2014void print_buildinfo(void)
2015{
2016 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002017#if NEED_PCI == 1
2018#ifdef PCILIB_VERSION
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002019 msg_gdbg(" libpci %s,", PCILIB_VERSION);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002020#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002021 msg_gdbg(" unknown PCI library,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002022#endif
2023#endif
2024#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002025 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00002026#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002027 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00002028#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002029 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00002030#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002031#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002032 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002033#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002034 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002035#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002036 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002037#endif
2038#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002039 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00002040#endif
2041#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002042 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00002043#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002044 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00002045#else
2046#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002047#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002048 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002049}
2050
Bernhard Walle201bde32008-01-21 15:24:22 +00002051void print_version(void)
2052{
Stefan Tauner76347082016-11-27 17:45:49 +01002053 msg_ginfo("flashrom %s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00002054 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00002055 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00002056}
2057
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00002058void print_banner(void)
2059{
2060 msg_ginfo("flashrom is free software, get the source code at "
Stefan Tauner4c723152016-01-14 22:47:55 +00002061 "https://flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00002062 msg_ginfo("\n");
2063}
2064
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002065int selfcheck(void)
2066{
Stefan Tauner96658be2014-05-26 22:05:31 +00002067 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00002068 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00002069
2070 /* Safety check. Instead of aborting after the first error, check
2071 * if more errors exist.
2072 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002073 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00002074 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00002075 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002076 }
Stefan Taunera6d96482012-12-26 19:51:23 +00002077 for (i = 0; i < PROGRAMMER_INVALID; i++) {
2078 const struct programmer_entry p = programmer_table[i];
2079 if (p.name == NULL) {
2080 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
2081 ret = 1;
2082 /* This might hide other problems with this programmer, but allows for better error
2083 * messages below without jumping through hoops. */
2084 continue;
2085 }
Stefan Tauneraf358d62012-12-27 18:40:26 +00002086 switch (p.type) {
2087 case USB:
2088 case PCI:
2089 case OTHER:
2090 if (p.devs.note == NULL) {
2091 if (strcmp("internal", p.name) == 0)
2092 break; /* This one has its device list stored separately. */
2093 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
2094 p.name);
2095 ret = 1;
2096 }
2097 break;
2098 default:
2099 msg_gerr("Programmer %s does not have a valid type set!\n", p.name);
2100 ret = 1;
2101 break;
2102 }
Stefan Taunera6d96482012-12-26 19:51:23 +00002103 if (p.init == NULL) {
2104 msg_gerr("Programmer %s does not have a valid init function!\n", p.name);
2105 ret = 1;
2106 }
2107 if (p.delay == NULL) {
2108 msg_gerr("Programmer %s does not have a valid delay function!\n", p.name);
2109 ret = 1;
2110 }
2111 if (p.map_flash_region == NULL) {
2112 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p.name);
2113 ret = 1;
2114 }
2115 if (p.unmap_flash_region == NULL) {
2116 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p.name);
2117 ret = 1;
2118 }
2119 }
Stefan Tauner96658be2014-05-26 22:05:31 +00002120
2121 /* It would be favorable if we could check for the correct layout (especially termination) of various
2122 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
2123 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
2124 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
2125 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
2126 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00002127 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00002128 msg_gerr("Flashchips table miscompilation!\n");
2129 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00002130 } else {
2131 for (i = 0; i < flashchips_size - 1; i++) {
2132 const struct flashchip *chip = &flashchips[i];
2133 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
2134 ret = 1;
2135 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
2136 "Please report a bug at flashrom@flashrom.org\n", i,
2137 chip->name == NULL ? "unnamed" : chip->name);
2138 }
2139 if (selfcheck_eraseblocks(chip)) {
2140 ret = 1;
2141 }
2142 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00002143 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00002144
Stefan Tauner600576b2014-06-12 22:57:36 +00002145#if CONFIG_INTERNAL == 1
2146 ret |= selfcheck_board_enables();
2147#endif
2148
Stefan Tauner96658be2014-05-26 22:05:31 +00002149 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00002150 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00002151}
2152
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10002153/* FIXME: This function signature needs to be improved once prepare_flash_access()
2154 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002155 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06002156static int chip_safety_check(const struct flashctx *flash, int force,
2157 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002158{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002159 const struct flashchip *chip = flash->chip;
2160
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002161 if (!programmer_may_write && (write_it || erase_it)) {
2162 msg_perr("Write/erase is not working yet on your programmer in "
2163 "its current configuration.\n");
2164 /* --force is the wrong approach, but it's the best we can do
2165 * until the generic programmer parameter parser is merged.
2166 */
2167 if (!force)
2168 return 1;
2169 msg_cerr("Continuing anyway.\n");
2170 }
2171
2172 if (read_it || erase_it || write_it || verify_it) {
2173 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00002174 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002175 msg_cerr("Read is not working on this chip. ");
2176 if (!force)
2177 return 1;
2178 msg_cerr("Continuing anyway.\n");
2179 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002180 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002181 msg_cerr("flashrom has no read function for this "
2182 "flash chip.\n");
2183 return 1;
2184 }
2185 }
2186 if (erase_it || write_it) {
2187 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00002188 if (chip->tested.erase == NA) {
2189 msg_cerr("Erase is not possible on this chip.\n");
2190 return 1;
2191 }
2192 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002193 msg_cerr("Erase is not working on this chip. ");
2194 if (!force)
2195 return 1;
2196 msg_cerr("Continuing anyway.\n");
2197 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00002198 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00002199 msg_cerr("flashrom has no erase function for this "
2200 "flash chip.\n");
2201 return 1;
2202 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002203 }
2204 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00002205 if (chip->tested.write == NA) {
2206 msg_cerr("Write is not possible on this chip.\n");
2207 return 1;
2208 }
2209 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002210 msg_cerr("Write is not working on this chip. ");
2211 if (!force)
2212 return 1;
2213 msg_cerr("Continuing anyway.\n");
2214 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00002215 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00002216 msg_cerr("flashrom has no write function for this "
2217 "flash chip.\n");
2218 return 1;
2219 }
2220 }
2221 return 0;
2222}
2223
Nico Huber305f4172013-06-14 11:55:26 +02002224int prepare_flash_access(struct flashctx *const flash,
2225 const bool read_it, const bool write_it,
2226 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00002227{
2228 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
2229 msg_cerr("Aborting.\n");
2230 return 1;
2231 }
2232
2233 if (flash->layout == get_global_layout() && normalize_romentries(flash)) {
2234 msg_cerr("Requested regions can not be handled. Aborting.\n");
2235 return 1;
2236 }
2237
2238 if (map_flash(flash) != 0)
2239 return 1;
2240
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11002241 /* Initialize chip_restore_fn_count before chip unlock calls. */
2242 flash->chip_restore_fn_count = 0;
2243
Nico Huber454f6132012-12-10 13:34:10 +00002244 /* Given the existence of read locks, we want to unlock for read,
2245 erase and write. */
2246 if (flash->chip->unlock)
2247 flash->chip->unlock(flash);
2248
Nico Huberf43c6542017-10-14 17:47:28 +02002249 flash->address_high_byte = -1;
2250 flash->in_4ba_mode = false;
2251
Nico Huberdc5af542018-12-22 16:54:59 +01002252 /* Be careful about 4BA chips and broken masters */
2253 if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
2254 /* If we can't use native instructions, bail out */
2255 if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
2256 || !spi_master_4ba(flash)) {
2257 msg_cerr("Programmer doesn't support this chip. Aborting.\n");
2258 return 1;
2259 }
2260 }
2261
Ed Swierkcc20a9b2017-07-03 13:17:18 -07002262 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01002263 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01002264 int ret;
2265 if (spi_master_4ba(flash))
2266 ret = spi_enter_4ba(flash);
2267 else
2268 ret = spi_exit_4ba(flash);
2269 if (ret) {
2270 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07002271 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02002272 }
Boris Baykov99127182016-06-11 18:29:00 +02002273 }
2274
Nico Huber454f6132012-12-10 13:34:10 +00002275 return 0;
2276}
2277
Nico Huber305f4172013-06-14 11:55:26 +02002278void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00002279{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11002280 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00002281 unmap_flash(flash);
2282}
2283
2284/**
2285 * @addtogroup flashrom-flash
2286 * @{
2287 */
2288
2289/**
2290 * @brief Erase the specified ROM chip.
2291 *
2292 * If a layout is set in the given flash context, only included regions
2293 * will be erased.
2294 *
2295 * @param flashctx The context of the flash chip to erase.
2296 * @return 0 on success.
2297 */
2298int flashrom_flash_erase(struct flashctx *const flashctx)
2299{
2300 if (prepare_flash_access(flashctx, false, false, true, false))
2301 return 1;
2302
2303 const int ret = erase_by_layout(flashctx);
2304
2305 finalize_flash_access(flashctx);
2306
2307 return ret;
2308}
2309
2310/** @} */ /* end flashrom-flash */
2311
2312/**
2313 * @defgroup flashrom-ops Operations
2314 * @{
2315 */
2316
2317/**
2318 * @brief Read the current image from the specified ROM chip.
2319 *
2320 * If a layout is set in the specified flash context, only included regions
2321 * will be read.
2322 *
2323 * @param flashctx The context of the flash chip.
2324 * @param buffer Target buffer to write image to.
2325 * @param buffer_len Size of target buffer in bytes.
2326 * @return 0 on success,
2327 * 2 if buffer_len is too short for the flash chip's contents,
2328 * or 1 on any other failure.
2329 */
2330int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
2331{
2332 const size_t flash_size = flashctx->chip->total_size * 1024;
2333
2334 if (flash_size > buffer_len)
2335 return 2;
2336
2337 if (prepare_flash_access(flashctx, true, false, false, false))
2338 return 1;
2339
2340 msg_cinfo("Reading flash... ");
2341
2342 int ret = 1;
2343 if (read_by_layout(flashctx, buffer)) {
2344 msg_cerr("Read operation failed!\n");
2345 msg_cinfo("FAILED.\n");
2346 goto _finalize_ret;
2347 }
2348 msg_cinfo("done.\n");
2349 ret = 0;
2350
2351_finalize_ret:
2352 finalize_flash_access(flashctx);
2353 return ret;
2354}
2355
2356static void combine_image_by_layout(const struct flashctx *const flashctx,
2357 uint8_t *const newcontents, const uint8_t *const oldcontents)
2358{
2359 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01002360 const struct romentry *included;
2361 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00002362
Nico Huber3d7b1e32018-12-22 00:53:14 +01002363 while ((included = layout_next_included_region(layout, start))) {
2364 if (included->start > start) {
2365 /* copy everything up to the start of this included region */
2366 memcpy(newcontents + start, oldcontents + start, included->start - start);
2367 }
2368 /* skip this included region */
2369 start = included->end + 1;
2370 if (start == 0)
2371 return;
Nico Huber454f6132012-12-10 13:34:10 +00002372 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01002373
2374 /* copy the rest of the chip */
2375 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
2376 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00002377}
2378
2379/**
2380 * @brief Write the specified image to the ROM chip.
2381 *
2382 * If a layout is set in the specified flash context, only erase blocks
2383 * containing included regions will be touched.
2384 *
2385 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02002386 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00002387 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002388 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00002389 * @return 0 on success,
2390 * 4 if buffer_len doesn't match the size of the flash chip,
2391 * 3 if write was tried but nothing has changed,
2392 * 2 if write failed and flash contents changed,
2393 * or 1 on any other failure.
2394 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002395int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
2396 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00002397{
2398 const size_t flash_size = flashctx->chip->total_size * 1024;
2399 const bool verify_all = flashctx->flags.verify_whole_chip;
2400 const bool verify = flashctx->flags.verify_after_write;
2401
2402 if (buffer_len != flash_size)
2403 return 4;
2404
2405 int ret = 1;
2406
2407 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002408 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00002409 uint8_t *const curcontents = malloc(flash_size);
2410 uint8_t *oldcontents = NULL;
2411 if (verify_all)
2412 oldcontents = malloc(flash_size);
2413 if (!curcontents || (verify_all && !oldcontents)) {
2414 msg_gerr("Out of memory!\n");
2415 goto _free_ret;
2416 }
2417
2418#if CONFIG_INTERNAL == 1
2419 if (programmer == PROGRAMMER_INTERNAL && cb_check_image(newcontents, flash_size) < 0) {
2420 if (flashctx->flags.force_boardmismatch) {
2421 msg_pinfo("Proceeding anyway because user forced us to.\n");
2422 } else {
2423 msg_perr("Aborting. You can override this with "
2424 "-p internal:boardmismatch=force.\n");
2425 goto _free_ret;
2426 }
2427 }
2428#endif
2429
2430 if (prepare_flash_access(flashctx, false, true, false, verify))
2431 goto _free_ret;
2432
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002433 /* If given, assume flash chip contains same data as `refcontents`. */
2434 if (refcontents) {
2435 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
2436 memcpy(curcontents, refcontents, flash_size);
2437 if (oldcontents)
2438 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00002439 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002440 /*
2441 * Read the whole chip to be able to check whether regions need to be
2442 * erased and to give better diagnostics in case write fails.
2443 * The alternative is to read only the regions which are to be
2444 * preserved, but in that case we might perform unneeded erase which
2445 * takes time as well.
2446 */
2447 msg_cinfo("Reading old flash chip contents... ");
2448 if (verify_all) {
2449 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
2450 msg_cinfo("FAILED.\n");
2451 goto _finalize_ret;
2452 }
2453 memcpy(curcontents, oldcontents, flash_size);
2454 } else {
2455 if (read_by_layout(flashctx, curcontents)) {
2456 msg_cinfo("FAILED.\n");
2457 goto _finalize_ret;
2458 }
Nico Huber454f6132012-12-10 13:34:10 +00002459 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002460 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00002461 }
Nico Huber454f6132012-12-10 13:34:10 +00002462
2463 if (write_by_layout(flashctx, curcontents, newcontents)) {
2464 msg_cerr("Uh oh. Erase/write failed. ");
2465 ret = 2;
2466 if (verify_all) {
2467 msg_cerr("Checking if anything has changed.\n");
2468 msg_cinfo("Reading current flash chip contents... ");
2469 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
2470 msg_cinfo("done.\n");
2471 if (!memcmp(oldcontents, curcontents, flash_size)) {
2472 nonfatal_help_message();
2473 goto _finalize_ret;
2474 }
2475 msg_cerr("Apparently at least some data has changed.\n");
2476 } else
2477 msg_cerr("Can't even read anymore!\n");
2478 emergency_help_message();
2479 goto _finalize_ret;
2480 } else {
2481 msg_cerr("\n");
2482 }
2483 emergency_help_message();
2484 goto _finalize_ret;
2485 }
2486
2487 /* Verify only if we actually changed something. */
2488 if (verify && !all_skipped) {
2489 const struct flashrom_layout *const layout_bak = flashctx->layout;
2490
2491 msg_cinfo("Verifying flash... ");
2492
2493 /* Work around chips which need some time to calm down. */
2494 programmer_delay(1000*1000);
2495
2496 if (verify_all) {
2497 combine_image_by_layout(flashctx, newcontents, oldcontents);
2498 flashctx->layout = NULL;
2499 }
2500 ret = verify_by_layout(flashctx, curcontents, newcontents);
2501 flashctx->layout = layout_bak;
2502 /* If we tried to write, and verification now fails, we
2503 might have an emergency situation. */
2504 if (ret)
2505 emergency_help_message();
2506 else
2507 msg_cinfo("VERIFIED.\n");
2508 } else {
2509 /* We didn't change anything. */
2510 ret = 0;
2511 }
2512
2513_finalize_ret:
2514 finalize_flash_access(flashctx);
2515_free_ret:
2516 free(oldcontents);
2517 free(curcontents);
2518 return ret;
2519}
2520
2521/**
2522 * @brief Verify the ROM chip's contents with the specified image.
2523 *
2524 * If a layout is set in the specified flash context, only included regions
2525 * will be verified.
2526 *
2527 * @param flashctx The context of the flash chip.
2528 * @param buffer Source buffer to verify with.
2529 * @param buffer_len Size of source buffer in bytes.
2530 * @return 0 on success,
2531 * 3 if the chip's contents don't match,
2532 * 2 if buffer_len doesn't match the size of the flash chip,
2533 * or 1 on any other failure.
2534 */
2535int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
2536{
2537 const size_t flash_size = flashctx->chip->total_size * 1024;
2538
2539 if (buffer_len != flash_size)
2540 return 2;
2541
2542 const uint8_t *const newcontents = buffer;
2543 uint8_t *const curcontents = malloc(flash_size);
2544 if (!curcontents) {
2545 msg_gerr("Out of memory!\n");
2546 return 1;
2547 }
2548
2549 int ret = 1;
2550
2551 if (prepare_flash_access(flashctx, false, false, false, true))
2552 goto _free_ret;
2553
2554 msg_cinfo("Verifying flash... ");
2555 ret = verify_by_layout(flashctx, curcontents, newcontents);
2556 if (!ret)
2557 msg_cinfo("VERIFIED.\n");
2558
2559 finalize_flash_access(flashctx);
2560_free_ret:
2561 free(curcontents);
2562 return ret;
2563}
2564
2565/** @} */ /* end flashrom-ops */
Nico Huber899e4ec2016-04-29 18:39:01 +02002566
2567int do_read(struct flashctx *const flash, const char *const filename)
2568{
2569 if (prepare_flash_access(flash, true, false, false, false))
2570 return 1;
2571
2572 const int ret = read_flash_to_file(flash, filename);
2573
2574 finalize_flash_access(flash);
2575
2576 return ret;
2577}
2578
2579int do_erase(struct flashctx *const flash)
2580{
2581 const int ret = flashrom_flash_erase(flash);
2582
2583 /*
2584 * FIXME: Do we really want the scary warning if erase failed?
2585 * After all, after erase the chip is either blank or partially
2586 * blank or it has the old contents. A blank chip won't boot,
2587 * so if the user wanted erase and reboots afterwards, the user
2588 * knows very well that booting won't work.
2589 */
2590 if (ret)
2591 emergency_help_message();
2592
2593 return ret;
2594}
2595
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002596int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
Nico Huber899e4ec2016-04-29 18:39:01 +02002597{
2598 const size_t flash_size = flash->chip->total_size * 1024;
2599 int ret = 1;
2600
2601 uint8_t *const newcontents = malloc(flash_size);
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002602 uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
2603
2604 if (!newcontents || (referencefile && !refcontents)) {
Nico Huber899e4ec2016-04-29 18:39:01 +02002605 msg_gerr("Out of memory!\n");
2606 goto _free_ret;
2607 }
2608
2609 if (read_buf_from_file(newcontents, flash_size, filename))
2610 goto _free_ret;
2611
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002612 if (referencefile) {
2613 if (read_buf_from_file(refcontents, flash_size, referencefile))
2614 goto _free_ret;
2615 }
2616
2617 ret = flashrom_image_write(flash, newcontents, flash_size, refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002618
2619_free_ret:
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002620 free(refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002621 free(newcontents);
2622 return ret;
2623}
2624
2625int do_verify(struct flashctx *const flash, const char *const filename)
2626{
2627 const size_t flash_size = flash->chip->total_size * 1024;
2628 int ret = 1;
2629
2630 uint8_t *const newcontents = malloc(flash_size);
2631 if (!newcontents) {
2632 msg_gerr("Out of memory!\n");
2633 goto _free_ret;
2634 }
2635
2636 if (read_buf_from_file(newcontents, flash_size, filename))
2637 goto _free_ret;
2638
2639 ret = flashrom_image_verify(flash, newcontents, flash_size);
2640
2641_free_ret:
2642 free(newcontents);
2643 return ret;
2644}