blob: 0af5057574f74600095f6ff6d9a47990cefe9aa2 [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
Victor Ding821e44c2020-08-18 18:27:26 +100087#if CONFIG_MEC1308 == 1
88 {
89 .name = "mec1308",
90 .type = OTHER,
91 .devs.note = "Microchip MEC1308 Embedded Controller.\n",
92 .init = mec1308_init,
93 .map_flash_region = fallback_map,
94 .unmap_flash_region = fallback_unmap,
95 .delay = internal_delay,
96 },
97#endif
98
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000099#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000100 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000101 .name = "nic3com",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000102 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000103 .devs.dev = nics_3com,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000104 .init = nic3com_init,
Uwe Hermannc6915932009-05-17 23:12:17 +0000105 .map_flash_region = fallback_map,
106 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000107 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000108 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000109#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000110
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000111#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000112 {
Carl-Daniel Hailfinger40446ee2011-03-07 01:08:09 +0000113 /* This programmer works for Realtek RTL8139 and SMC 1211. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000114 .name = "nicrealtek",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000115 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000116 .devs.dev = nics_realtek,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000117 .init = nicrealtek_init,
118 .map_flash_region = fallback_map,
119 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000120 .delay = internal_delay,
Joerg Fischer5665ef32010-05-21 21:54:07 +0000121 },
Joerg Fischer5665ef32010-05-21 21:54:07 +0000122#endif
123
Andrew Morganc29c2e72010-06-07 22:37:54 +0000124#if CONFIG_NICNATSEMI == 1
125 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000126 .name = "nicnatsemi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000127 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000128 .devs.dev = nics_natsemi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000129 .init = nicnatsemi_init,
130 .map_flash_region = fallback_map,
131 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000132 .delay = internal_delay,
Andrew Morganc29c2e72010-06-07 22:37:54 +0000133 },
134#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000135
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000136#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000137 {
138 .name = "gfxnvidia",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000139 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000140 .devs.dev = gfx_nvidia,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000141 .init = gfxnvidia_init,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000142 .map_flash_region = fallback_map,
143 .unmap_flash_region = fallback_unmap,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000144 .delay = internal_delay,
145 },
146#endif
147
Edward O'Callaghan732f2ee2020-11-27 12:54:31 +1100148#if CONFIG_RAIDEN_DEBUG_SPI == 1
Edward O'Callaghanad08aef2020-03-02 18:16:14 +1100149 {
Edward O'Callaghanf95cc8f2020-11-27 12:59:49 +1100150 .name = "raiden_debug_spi",
Edward O'Callaghanad08aef2020-03-02 18:16:14 +1100151 .type = USB,
Angel Pons6c8bd912020-03-25 13:35:45 +0100152 .devs.dev = devs_raiden,
Edward O'Callaghanad08aef2020-03-02 18:16:14 +1100153 .init = raiden_debug_spi_init,
154 .map_flash_region = fallback_map,
155 .unmap_flash_region = fallback_unmap,
156 .delay = internal_delay,
157 },
158#endif
159
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000160#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000161 {
TURBO Jb0912c02009-09-02 23:00:46 +0000162 .name = "drkaiser",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000163 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000164 .devs.dev = drkaiser_pcidev,
TURBO Jb0912c02009-09-02 23:00:46 +0000165 .init = drkaiser_init,
TURBO Jb0912c02009-09-02 23:00:46 +0000166 .map_flash_region = fallback_map,
167 .unmap_flash_region = fallback_unmap,
TURBO Jb0912c02009-09-02 23:00:46 +0000168 .delay = internal_delay,
169 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000170#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000171
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000172#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000173 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000174 .name = "satasii",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000175 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000176 .devs.dev = satas_sii,
Rudolf Marek68720c72009-05-17 19:39:27 +0000177 .init = satasii_init,
Uwe Hermannc6915932009-05-17 23:12:17 +0000178 .map_flash_region = fallback_map,
179 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000180 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000181 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000182#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000183
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000184#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000185 {
186 .name = "atahpt",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000187 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000188 .devs.dev = ata_hpt,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000189 .init = atahpt_init,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000190 .map_flash_region = fallback_map,
191 .unmap_flash_region = fallback_unmap,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000192 .delay = internal_delay,
193 },
194#endif
195
Jonathan Kollasch7f0f3fa2014-06-01 10:26:23 +0000196#if CONFIG_ATAVIA == 1
197 {
198 .name = "atavia",
199 .type = PCI,
200 .devs.dev = ata_via,
201 .init = atavia_init,
202 .map_flash_region = atavia_map,
203 .unmap_flash_region = fallback_unmap,
204 .delay = internal_delay,
205 },
206#endif
207
Joseph C. Lehnerc2644a32016-01-16 23:45:25 +0000208#if CONFIG_ATAPROMISE == 1
209 {
210 .name = "atapromise",
211 .type = PCI,
212 .devs.dev = ata_promise,
213 .init = atapromise_init,
214 .map_flash_region = atapromise_map,
215 .unmap_flash_region = fallback_unmap,
216 .delay = internal_delay,
217 },
218#endif
219
Kyösti Mälkki72d42f82014-06-01 23:48:31 +0000220#if CONFIG_IT8212 == 1
221 {
222 .name = "it8212",
223 .type = PCI,
224 .devs.dev = devs_it8212,
225 .init = it8212_init,
226 .map_flash_region = fallback_map,
227 .unmap_flash_region = fallback_unmap,
228 .delay = internal_delay,
229 },
230#endif
231
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000232#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000233 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000234 .name = "ft2232_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000235 .type = USB,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000236 .devs.dev = devs_ft2232spi,
Paul Fox05dfbe62009-06-16 21:08:06 +0000237 .init = ft2232_spi_init,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000238 .map_flash_region = fallback_map,
239 .unmap_flash_region = fallback_unmap,
Paul Fox05dfbe62009-06-16 21:08:06 +0000240 .delay = internal_delay,
241 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000242#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000243
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000244#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000245 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000246 .name = "serprog",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000247 .type = OTHER,
248 /* FIXME */
249 .devs.note = "All programmer devices speaking the serprog protocol\n",
Urja Rannikko22915352009-06-23 11:33:43 +0000250 .init = serprog_init,
Urja Rannikko0b4ffd52015-06-29 23:24:23 +0000251 .map_flash_region = serprog_map,
Urja Rannikko22915352009-06-23 11:33:43 +0000252 .unmap_flash_region = fallback_unmap,
Urja Rannikko22915352009-06-23 11:33:43 +0000253 .delay = serprog_delay,
254 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000255#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000256
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000257#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000258 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000259 .name = "buspirate_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000260 .type = OTHER,
261 /* FIXME */
262 .devs.note = "Dangerous Prototypes Bus Pirate\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000263 .init = buspirate_spi_init,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000264 .map_flash_region = fallback_map,
265 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000266 .delay = internal_delay,
267 },
268#endif
269
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000270#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000271 {
272 .name = "dediprog",
Stefan Taunerfdec7472016-02-22 08:59:27 +0000273 .type = USB,
274 .devs.dev = devs_dediprog,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000275 .init = dediprog_init,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000276 .map_flash_region = fallback_map,
277 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000278 .delay = internal_delay,
279 },
280#endif
281
Daniel Thompson45e91a22018-06-04 13:46:29 +0100282#if CONFIG_DEVELOPERBOX_SPI == 1
283 {
284 .name = "developerbox",
285 .type = USB,
286 .devs.dev = devs_developerbox_spi,
287 .init = developerbox_spi_init,
288 .map_flash_region = fallback_map,
289 .unmap_flash_region = fallback_unmap,
290 .delay = internal_delay,
291 },
292#endif
293
Victor Ding436b4152020-08-19 22:48:19 +1000294#if CONFIG_ENE_LPC == 1
295 {
296 .name = "ene_lpc",
297 .type = OTHER,
298 .devs.note = "ENE LPC interface keyboard controller\n",
299 .init = ene_lpc_init,
300 .map_flash_region = fallback_map,
301 .unmap_flash_region = fallback_unmap,
302 .delay = internal_delay,
303 },
304#endif
305
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000306#if CONFIG_RAYER_SPI == 1
307 {
308 .name = "rayer_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000309 .type = OTHER,
310 /* FIXME */
311 .devs.note = "RayeR parallel port programmer\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000312 .init = rayer_spi_init,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000313 .map_flash_region = fallback_map,
314 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000315 .delay = internal_delay,
316 },
317#endif
318
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000319#if CONFIG_PONY_SPI == 1
320 {
321 .name = "pony_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000322 .type = OTHER,
323 /* FIXME */
324 .devs.note = "Programmers compatible with SI-Prog, serbang or AJAWe\n",
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000325 .init = pony_spi_init,
326 .map_flash_region = fallback_map,
327 .unmap_flash_region = fallback_unmap,
328 .delay = internal_delay,
Stefan Tauneraf358d62012-12-27 18:40:26 +0000329 },
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000330#endif
331
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000332#if CONFIG_NICINTEL == 1
333 {
334 .name = "nicintel",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000335 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000336 .devs.dev = nics_intel,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000337 .init = nicintel_init,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000338 .map_flash_region = fallback_map,
339 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000340 .delay = internal_delay,
341 },
342#endif
343
Idwer Vollering004f4b72010-09-03 18:21:21 +0000344#if CONFIG_NICINTEL_SPI == 1
345 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000346 .name = "nicintel_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000347 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000348 .devs.dev = nics_intel_spi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000349 .init = nicintel_spi_init,
350 .map_flash_region = fallback_map,
351 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000352 .delay = internal_delay,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000353 },
354#endif
355
Ricardo Ribalda Delgado2a41f0a2014-07-28 20:35:21 +0000356#if CONFIG_NICINTEL_EEPROM == 1
357 {
358 .name = "nicintel_eeprom",
359 .type = PCI,
360 .devs.dev = nics_intel_ee,
361 .init = nicintel_ee_init,
362 .map_flash_region = fallback_map,
363 .unmap_flash_region = fallback_unmap,
364 .delay = internal_delay,
365 },
366#endif
367
Mark Marshall90021f22010-12-03 14:48:11 +0000368#if CONFIG_OGP_SPI == 1
369 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000370 .name = "ogp_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000371 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000372 .devs.dev = ogp_spi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000373 .init = ogp_spi_init,
374 .map_flash_region = fallback_map,
375 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000376 .delay = internal_delay,
Mark Marshall90021f22010-12-03 14:48:11 +0000377 },
378#endif
379
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000380#if CONFIG_SATAMV == 1
381 {
382 .name = "satamv",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000383 .type = PCI,
Stefan Tauner4b24a2d2012-12-27 18:40:36 +0000384 .devs.dev = satas_mv,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000385 .init = satamv_init,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000386 .map_flash_region = fallback_map,
387 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000388 .delay = internal_delay,
389 },
390#endif
391
David Hendricksf9a30552015-05-23 20:30:30 -0700392#if CONFIG_LINUX_MTD == 1
393 {
394 .name = "linux_mtd",
395 .type = OTHER,
396 .devs.note = "Device files /dev/mtd*\n",
397 .init = linux_mtd_init,
398 .map_flash_region = fallback_map,
399 .unmap_flash_region = fallback_unmap,
400 .delay = internal_delay,
401 },
402#endif
403
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000404#if CONFIG_LINUX_SPI == 1
405 {
406 .name = "linux_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000407 .type = OTHER,
408 .devs.note = "Device files /dev/spidev*.*\n",
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000409 .init = linux_spi_init,
410 .map_flash_region = fallback_map,
411 .unmap_flash_region = fallback_unmap,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000412 .delay = internal_delay,
413 },
414#endif
415
Shiyu Sun13a2ef62020-03-19 14:37:57 +1100416#if CONFIG_LSPCON_I2C_SPI == 1
417 {
418 .name = "lspcon_i2c_spi",
419 .type = OTHER,
420 .devs.note = "Device files /dev/i2c-*.\n",
421 .init = lspcon_i2c_spi_init,
422 .map_flash_region = fallback_map,
423 .unmap_flash_region = fallback_unmap,
424 .delay = internal_delay,
425 },
426#endif
427
Edward O'Callaghand97f87b2020-03-26 00:00:41 +1100428#if CONFIG_REALTEK_MST_I2C_SPI == 1
429 {
430 .name = "realtek_mst_i2c_spi",
431 .type = OTHER,
432 .devs.note = "Device files /dev/i2c-*.\n",
433 .init = realtek_mst_i2c_spi_init,
434 .map_flash_region = fallback_map,
435 .unmap_flash_region = fallback_unmap,
436 .delay = internal_delay,
437 },
438#endif
439
James Lairdc60de0e2013-03-27 13:00:23 +0000440#if CONFIG_USBBLASTER_SPI == 1
441 {
442 .name = "usbblaster_spi",
443 .type = USB,
444 .devs.dev = devs_usbblasterspi,
445 .init = usbblaster_spi_init,
446 .map_flash_region = fallback_map,
447 .unmap_flash_region = fallback_unmap,
448 .delay = internal_delay,
449 },
450#endif
451
Alexandre Boeglin80e64712014-12-20 20:25:19 +0000452#if CONFIG_MSTARDDC_SPI == 1
453 {
454 .name = "mstarddc_spi",
455 .type = OTHER,
456 .devs.note = "MSTAR DDC devices addressable via /dev/i2c-* on Linux.\n",
457 .init = mstarddc_spi_init,
458 .map_flash_region = fallback_map,
459 .unmap_flash_region = fallback_unmap,
460 .delay = internal_delay,
461 },
462#endif
463
Justin Chevrier66e554b2015-02-08 21:58:10 +0000464#if CONFIG_PICKIT2_SPI == 1
465 {
466 .name = "pickit2_spi",
Stefan Taunerf31fe842016-02-22 08:59:15 +0000467 .type = USB,
468 .devs.dev = devs_pickit2_spi,
Justin Chevrier66e554b2015-02-08 21:58:10 +0000469 .init = pickit2_spi_init,
470 .map_flash_region = fallback_map,
471 .unmap_flash_region = fallback_unmap,
472 .delay = internal_delay,
473 },
474#endif
475
Urja Rannikko0870b022016-01-31 22:10:29 +0000476#if CONFIG_CH341A_SPI == 1
477 {
478 .name = "ch341a_spi",
479 .type = USB,
480 .devs.dev = devs_ch341a_spi,
481 .init = ch341a_spi_init,
482 .map_flash_region = fallback_map,
483 .unmap_flash_region = fallback_unmap,
484 .delay = ch341a_spi_delay,
485 },
486#endif
487
Lubomir Rintelb2154e82018-01-14 17:35:33 +0100488#if CONFIG_DIGILENT_SPI == 1
489 {
490 .name = "digilent_spi",
491 .type = USB,
492 .devs.dev = devs_digilent_spi,
493 .init = digilent_spi_init,
494 .map_flash_region = fallback_map,
495 .unmap_flash_region = fallback_unmap,
496 .delay = internal_delay,
497 },
498#endif
499
Marc Schink3578ec62016-03-17 16:23:03 +0100500#if CONFIG_JLINK_SPI == 1
501 {
502 .name = "jlink_spi",
503 .type = OTHER,
504 .init = jlink_spi_init,
505 .devs.note = "SEGGER J-Link and compatible devices\n",
506 .map_flash_region = fallback_map,
507 .unmap_flash_region = fallback_unmap,
508 .delay = internal_delay,
509 },
510#endif
511
Miklós Márton2d20d6d2018-01-30 20:20:15 +0100512#if CONFIG_NI845X_SPI == 1
513 {
514 .name = "ni845x_spi",
515 .type = OTHER, // choose other because NI-845x uses own USB implementation
516 .devs.note = "National Instruments USB-845x\n",
517 .init = ni845x_spi_init,
518 .map_flash_region = fallback_map,
519 .unmap_flash_region = fallback_unmap,
520 .delay = internal_delay,
521 },
522#endif
Miklós Márton324929c2019-08-01 19:14:10 +0200523
524#if CONFIG_STLINKV3_SPI == 1
525 {
526 .name = "stlinkv3_spi",
527 .type = USB,
528 .devs.dev = devs_stlinkv3_spi,
529 .init = stlinkv3_spi_init,
530 .map_flash_region = fallback_map,
531 .unmap_flash_region = fallback_unmap,
532 .delay = internal_delay,
533 },
534#endif
535
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000536 {0}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000537};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000538
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +0000539#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000540static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +0000541/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +0000542static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +0000543 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000544 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +0000545} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000546/* Initialize to 0 to make sure nobody registers a shutdown function before
547 * programmer init.
548 */
549static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000550
Stefan Taunerc4f44df2013-08-12 22:58:43 +0000551/* Did we change something or was every erase/write skipped (if any)? */
552static bool all_skipped = true;
553
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000554static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +0000555
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000556/* Register a function to be executed on programmer shutdown.
557 * The advantage over atexit() is that you can supply a void pointer which will
558 * be used as parameter to the registered function upon programmer shutdown.
559 * This pointer can point to arbitrary data used by said function, e.g. undo
560 * information for GPIO settings etc. If unneeded, set data=NULL.
561 * Please note that the first (void *data) belongs to the function signature of
562 * the function passed as first parameter.
563 */
David Hendricks8bb20212011-06-14 01:35:36 +0000564int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000565{
566 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000567 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000568 SHUTDOWN_MAXFN);
569 return 1;
570 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000571 if (!may_register_shutdown) {
572 msg_perr("Tried to register a shutdown function before "
573 "programmer init.\n");
574 return 1;
575 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000576 shutdown_fn[shutdown_fn_count].func = function;
577 shutdown_fn[shutdown_fn_count].data = data;
578 shutdown_fn_count++;
579
580 return 0;
581}
582
Nikolai Artemievf745d0e2020-11-05 13:54:27 +1100583int register_chip_restore(chip_restore_fn_cb_t func,
584 struct flashctx *flash, uint8_t status)
585{
586 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
587 msg_perr("Tried to register more than %i chip restore"
588 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
589 return 1;
590 }
591 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
592 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
593 flash->chip_restore_fn_count++;
594
595 return 0;
596}
597
598static int deregister_chip_restore(struct flashctx *flash)
599{
600 int rc = 0;
601
602 while (flash->chip_restore_fn_count > 0) {
603 int i = --flash->chip_restore_fn_count;
604 rc |= flash->chip_restore_fn[i].func(
605 flash, flash->chip_restore_fn[i].status);
606 }
607
608 return rc;
609}
610
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000611int programmer_init(enum programmer prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000612{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000613 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000614
615 if (prog >= PROGRAMMER_INVALID) {
616 msg_perr("Invalid programmer specified!\n");
617 return -1;
618 }
619 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000620 /* Initialize all programmer specific data. */
621 /* Default to unlimited decode sizes. */
622 max_rom_decode = (const struct decode_sizes) {
623 .parallel = 0xffffffff,
624 .lpc = 0xffffffff,
625 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000626 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000627 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000628 /* Default to top aligned flash at 4 GB. */
629 flashbase = 0;
630 /* Registering shutdown functions is now allowed. */
631 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000632 /* Default to allowing writes. Broken programmers set this to 0. */
633 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000634
635 programmer_param = param;
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000636 msg_pdbg("Initializing %s programmer\n", programmer_table[programmer].name);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000637 ret = programmer_table[programmer].init();
638 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000639 if (ret != 0) {
640 /* It is quite possible that any unhandled programmer parameter would have been valid,
641 * but an error in actual programmer init happened before the parameter was evaluated.
642 */
643 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
644 programmer_param);
645 } else {
646 /* Actual programmer init was successful, but the user specified an invalid or unusable
647 * (for the current programmer configuration) parameter.
648 */
649 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
650 msg_perr("Aborting.\n");
651 ret = ERROR_FATAL;
652 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000653 }
654 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000655}
656
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000657/** Calls registered shutdown functions and resets internal programmer-related variables.
658 * Calling it is safe even without previous initialization, but further interactions with programmer support
659 * require a call to programmer_init() (afterwards).
660 *
661 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000662int programmer_shutdown(void)
663{
David Hendricks8bb20212011-06-14 01:35:36 +0000664 int ret = 0;
665
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000666 /* Registering shutdown functions is no longer allowed. */
667 may_register_shutdown = 0;
668 while (shutdown_fn_count > 0) {
669 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000670 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000671 }
Stefan Taunere34e3e82013-01-01 00:06:51 +0000672
Stefan Taunerb8911d62012-12-26 07:55:00 +0000673 programmer_param = NULL;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000674 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000675
David Hendricks8bb20212011-06-14 01:35:36 +0000676 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000677}
678
Stefan Tauner305e0b92013-07-17 23:46:44 +0000679void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000680{
Stefan Tauner26e7a152013-09-13 17:21:05 +0000681 void *ret = programmer_table[programmer].map_flash_region(descr, phys_addr, len);
682 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
683 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
684 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000685}
686
687void programmer_unmap_flash_region(void *virt_addr, size_t len)
688{
689 programmer_table[programmer].unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000690 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000691}
692
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000693void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000694{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000695 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000696}
697
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000698void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000699{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000700 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000701}
702
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000703void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000704{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000705 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000706}
707
Mark Marshallf20b7be2014-05-09 21:16:21 +0000708void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000709{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000710 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000711}
712
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000713uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000714{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000715 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000716}
717
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000718uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000719{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000720 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000721}
722
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000723uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000724{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000725 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000726}
727
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000728void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
729 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000730{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000731 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000732}
733
Stefan Taunerf80419c2014-05-02 15:41:42 +0000734void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000735{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000736 if (usecs > 0)
737 programmer_table[programmer].delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000738}
739
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000740int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
741 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000742{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000743 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000744
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000745 return 0;
746}
747
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000748/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000749 * It will look for needle with a subsequent '=' in haystack, return a copy of
750 * needle and remove everything from the first occurrence of needle to the next
751 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000752 */
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000753char *extract_param(const char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000754{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000755 char *param_pos, *opt_pos, *rest;
756 char *opt = NULL;
757 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000758 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000759
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000760 needlelen = strlen(needle);
761 if (!needlelen) {
762 msg_gerr("%s: empty needle! Please report a bug at "
763 "flashrom@flashrom.org\n", __func__);
764 return NULL;
765 }
766 /* No programmer parameters given. */
767 if (*haystack == NULL)
768 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000769 param_pos = strstr(*haystack, needle);
770 do {
771 if (!param_pos)
772 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000773 /* Needle followed by '='? */
774 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000775 /* Beginning of the string? */
776 if (param_pos == *haystack)
777 break;
778 /* After a delimiter? */
779 if (strchr(delim, *(param_pos - 1)))
780 break;
781 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000782 /* Continue searching. */
783 param_pos++;
784 param_pos = strstr(param_pos, needle);
785 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000786
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000787 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000788 /* Get the string after needle and '='. */
789 opt_pos = param_pos + needlelen + 1;
790 optlen = strcspn(opt_pos, delim);
791 /* Return an empty string if the parameter was empty. */
792 opt = malloc(optlen + 1);
793 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000794 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000795 exit(1);
796 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000797 strncpy(opt, opt_pos, optlen);
798 opt[optlen] = '\0';
799 rest = opt_pos + optlen;
800 /* Skip all delimiters after the current parameter. */
801 rest += strspn(rest, delim);
802 memmove(param_pos, rest, strlen(rest) + 1);
803 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000804 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000805
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000806 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000807}
808
Stefan Tauner66652442011-06-26 17:38:17 +0000809char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000810{
811 return extract_param(&programmer_param, param_name, ",");
812}
813
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000814/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000815static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000816{
817 unsigned int usable_erasefunctions = 0;
818 int k;
819 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
820 if (!check_block_eraser(flash, k, 0))
821 usable_erasefunctions++;
822 }
823 return usable_erasefunctions;
824}
825
Mark Marshallf20b7be2014-05-09 21:16:21 +0000826static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000827{
828 int ret = 0, failcount = 0;
829 unsigned int i;
830 for (i = 0; i < len; i++) {
831 if (wantbuf[i] != havebuf[i]) {
832 /* Only print the first failure. */
833 if (!failcount++)
834 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
835 start + i, wantbuf[i], havebuf[i]);
836 }
837 }
838 if (failcount) {
839 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
840 start, start + len - 1, failcount);
841 ret = -1;
842 }
843 return ret;
844}
845
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000846/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600847static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000848{
849 int ret;
850 uint8_t *cmpbuf = malloc(len);
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300851 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000852
853 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000854 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000855 exit(1);
856 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300857 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000858 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000859 free(cmpbuf);
860 return ret;
861}
862
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000863/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000864 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000865 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000866 * @start offset to the base address of the flash chip
867 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000868 * @return 0 for success, -1 for failure
869 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000870int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000871{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000872 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000873 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000874
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000875 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000876 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000877 return -1;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000878 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000879
880 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000881 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000882 msg_gerr("Could not allocate memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000883 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000884 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000885 int ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000886
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000887 if (start + len > flash->chip->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000888 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000889 " total_size 0x%x\n", __func__, start, len,
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000890 flash->chip->total_size * 1024);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000891 ret = -1;
892 goto out_free;
893 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000894
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000895 ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000896 if (ret) {
897 msg_gerr("Verification impossible because read failed "
898 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000899 ret = -1;
900 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000901 }
902
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000903 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000904out_free:
905 free(readbuf);
906 return ret;
907}
908
Stefan Tauner02437452013-04-01 19:34:53 +0000909/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300910static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
911 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000912{
913 unsigned int i, j, limit;
914 for (j = 0; j < len / gran; j++) {
915 limit = min (gran, len - j * gran);
916 /* Are 'have' and 'want' identical? */
917 if (!memcmp(have + j * gran, want + j * gran, limit))
918 continue;
919 /* have needs to be in erased state. */
920 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300921 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000922 return 1;
923 }
924 return 0;
925}
926
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000927/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000928 * Check if the buffer @have can be programmed to the content of @want without
929 * erasing. This is only possible if all chunks of size @gran are either kept
930 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000931 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000932 * Warning: This function assumes that @have and @want point to naturally
933 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000934 *
935 * @have buffer with current content
936 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000937 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000938 * @gran write granularity (enum, not count)
939 * @return 0 if no erase is needed, 1 otherwise
940 */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300941int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
942 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000943{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000944 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000945 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000946
947 switch (gran) {
948 case write_gran_1bit:
949 for (i = 0; i < len; i++)
950 if ((have[i] & want[i]) != want[i]) {
951 result = 1;
952 break;
953 }
954 break;
955 case write_gran_1byte:
956 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300957 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000958 result = 1;
959 break;
960 }
961 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000962 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300963 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000964 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000965 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300966 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000967 break;
968 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300969 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000970 break;
971 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300972 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000973 break;
974 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300975 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000976 break;
977 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300978 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000979 break;
980 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300981 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000982 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000983 case write_gran_1byte_implicit_erase:
984 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
985 result = 0;
986 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000987 default:
988 msg_cerr("%s: Unsupported granularity! Please report a bug at "
989 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000990 }
991 return result;
992}
993
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000994/**
995 * Check if the buffer @have needs to be programmed to get the content of @want.
996 * If yes, return 1 and fill in first_start with the start address of the
997 * write operation and first_len with the length of the first to-be-written
998 * chunk. If not, return 0 and leave first_start and first_len undefined.
999 *
1000 * Warning: This function assumes that @have and @want point to naturally
1001 * aligned regions.
1002 *
1003 * @have buffer with current content
1004 * @want buffer with desired content
1005 * @len length of the checked area
1006 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001007 * @first_start offset of the first byte which needs to be written (passed in
1008 * value is increased by the offset of the first needed write
1009 * relative to have/want or unchanged if no write is needed)
1010 * @return length of the first contiguous area which needs to be written
1011 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001012 *
1013 * FIXME: This function needs a parameter which tells it about coalescing
1014 * in relation to the max write length of the programmer and the max write
1015 * length of the chip.
1016 */
Mark Marshallf20b7be2014-05-09 21:16:21 +00001017static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001018 unsigned int *first_start,
1019 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001020{
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001021 int need_write = 0;
1022 unsigned int rel_start = 0, first_len = 0;
1023 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001024
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001025 switch (gran) {
1026 case write_gran_1bit:
1027 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +00001028 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001029 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001030 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +00001031 case write_gran_128bytes:
1032 stride = 128;
1033 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001034 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001035 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001036 break;
Stefan Tauner02437452013-04-01 19:34:53 +00001037 case write_gran_264bytes:
1038 stride = 264;
1039 break;
1040 case write_gran_512bytes:
1041 stride = 512;
1042 break;
1043 case write_gran_528bytes:
1044 stride = 528;
1045 break;
1046 case write_gran_1024bytes:
1047 stride = 1024;
1048 break;
1049 case write_gran_1056bytes:
1050 stride = 1056;
1051 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001052 default:
1053 msg_cerr("%s: Unsupported granularity! Please report a bug at "
1054 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001055 /* Claim that no write was needed. A write with unknown
1056 * granularity is too dangerous to try.
1057 */
1058 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001059 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001060 for (i = 0; i < len / stride; i++) {
1061 limit = min(stride, len - i * stride);
1062 /* Are 'have' and 'want' identical? */
1063 if (memcmp(have + i * stride, want + i * stride, limit)) {
1064 if (!need_write) {
1065 /* First location where have and want differ. */
1066 need_write = 1;
1067 rel_start = i * stride;
1068 }
1069 } else {
1070 if (need_write) {
1071 /* First location where have and want
1072 * do not differ anymore.
1073 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001074 break;
1075 }
1076 }
1077 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +00001078 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001079 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001080 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001081 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001082}
1083
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001084/* Returns the number of busses commonly supported by the current programmer and flash chip where the latter
1085 * can not be completely accessed due to size/address limits of the programmer. */
1086unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001087{
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001088 unsigned int limitexceeded = 0;
1089 uint32_t size = flash->chip->total_size * 1024;
1090 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001091
1092 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001093 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001094 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001095 "size %u kB of chipset/board/programmer "
1096 "for %s interface, "
1097 "probe/read/erase/write may fail. ", size / 1024,
1098 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001099 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001100 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001101 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001102 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001103 "size %u kB of chipset/board/programmer "
1104 "for %s interface, "
1105 "probe/read/erase/write may fail. ", size / 1024,
1106 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001107 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001108 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001109 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001110 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001111 "size %u kB of chipset/board/programmer "
1112 "for %s interface, "
1113 "probe/read/erase/write may fail. ", size / 1024,
1114 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001115 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +00001116 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001117 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +00001118 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001119 "size %u kB of chipset/board/programmer "
1120 "for %s interface, "
1121 "probe/read/erase/write may fail. ", size / 1024,
1122 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001123 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +00001124 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001125}
1126
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001127void unmap_flash(struct flashctx *flash)
1128{
1129 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
1130 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
1131 flash->physical_registers = 0;
1132 flash->virtual_registers = (chipaddr)ERROR_PTR;
1133 }
1134
1135 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
1136 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
1137 flash->physical_memory = 0;
1138 flash->virtual_memory = (chipaddr)ERROR_PTR;
1139 }
1140}
1141
1142int map_flash(struct flashctx *flash)
1143{
1144 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
1145 flash->virtual_memory = (chipaddr)ERROR_PTR;
1146 flash->virtual_registers = (chipaddr)ERROR_PTR;
1147
1148 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
1149 * These are used for various probing-related hacks that would not map successfully anyway and should be
1150 * removed ASAP. */
1151 if (flash->chip->total_size == 0)
1152 return 0;
1153
1154 const chipsize_t size = flash->chip->total_size * 1024;
1155 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
1156 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
1157 if (addr == ERROR_PTR) {
1158 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
1159 flash->chip->name, PRIxPTR_WIDTH, base);
1160 return 1;
1161 }
1162 flash->physical_memory = base;
1163 flash->virtual_memory = (chipaddr)addr;
1164
1165 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
1166 * completely different on some chips and programmers, or not mappable at all.
1167 * Ignore these problems for now and always report success. */
1168 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
1169 base = 0xffffffff - size - 0x400000 + 1;
1170 addr = programmer_map_flash_region("flash chip registers", base, size);
1171 if (addr == ERROR_PTR) {
1172 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
1173 flash->chip->name, PRIxPTR_WIDTH, base);
1174 return 0;
1175 }
1176 flash->physical_registers = base;
1177 flash->virtual_registers = (chipaddr)addr;
1178 }
1179 return 0;
1180}
1181
Nico Huber2d625722016-05-03 10:48:02 +02001182/*
1183 * Return a string corresponding to the bustype parameter.
1184 * Memory is obtained with malloc() and must be freed with free() by the caller.
1185 */
1186char *flashbuses_to_text(enum chipbustype bustype)
1187{
1188 char *ret = calloc(1, 1);
1189 /*
1190 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
1191 * will cease to exist and should be eliminated here as well.
1192 */
1193 if (bustype == BUS_NONSPI) {
1194 ret = strcat_realloc(ret, "Non-SPI, ");
1195 } else {
1196 if (bustype & BUS_PARALLEL)
1197 ret = strcat_realloc(ret, "Parallel, ");
1198 if (bustype & BUS_LPC)
1199 ret = strcat_realloc(ret, "LPC, ");
1200 if (bustype & BUS_FWH)
1201 ret = strcat_realloc(ret, "FWH, ");
1202 if (bustype & BUS_SPI)
1203 ret = strcat_realloc(ret, "SPI, ");
1204 if (bustype & BUS_PROG)
1205 ret = strcat_realloc(ret, "Programmer-specific, ");
1206 if (bustype == BUS_NONE)
1207 ret = strcat_realloc(ret, "None, ");
1208 }
1209 /* Kill last comma. */
1210 ret[strlen(ret) - 2] = '\0';
1211 ret = realloc(ret, strlen(ret) + 1);
1212 return ret;
1213}
1214
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001215int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001216{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001217 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001218 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001219 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001220
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001221 for (chip = flashchips + startchip; chip && chip->name; chip++) {
1222 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +00001223 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001224 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001225 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +00001226 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +03001227 /* Only probe for SPI25 chips by default. */
1228 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
1229 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001230 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
1231 if (!chip->probe && !force) {
1232 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001233 continue;
1234 }
Stefan Reinauer70385642007-04-06 11:58:03 +00001235
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001236 /* Start filling in the dynamic data. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001237 flash->chip = calloc(1, sizeof(struct flashchip));
1238 if (!flash->chip) {
1239 msg_gerr("Out of memory!\n");
1240 exit(1);
1241 }
1242 memcpy(flash->chip, chip, sizeof(struct flashchip));
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001243 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001244
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001245 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +01001246 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001247
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001248 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
1249 * is only called with force=1 after normal probing failed.
1250 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001251 if (force)
1252 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001253
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001254 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +00001255 goto notfound;
1256
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001257 /* If this is the first chip found, accept it.
1258 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001259 * a non-generic match. SFDP and CFI are generic matches.
1260 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +00001261 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001262 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001263 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001264 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001265 msg_cinfo("===\n"
1266 "SFDP has autodetected a flash chip which is "
1267 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001268 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001269 msg_cinfo("The standard operations read and "
1270 "verify should work, but to support "
1271 "erase, write and all other "
1272 "possible features");
1273 else
1274 msg_cinfo("All standard operations (read, "
1275 "verify, erase and write) should "
1276 "work, but to support all possible "
1277 "features");
1278
Stefan Taunerb4e06bd2012-08-20 00:24:22 +00001279 msg_cinfo(" we need to add them manually.\n"
1280 "You can help us by mailing us the output of the following command to "
1281 "flashrom@flashrom.org:\n"
1282 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
1283 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001284 "===\n");
1285 }
1286
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001287 /* First flash chip detected on this bus. */
1288 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001289 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001290 /* Not the first flash chip detected on this bus, but not a generic match either. */
1291 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
1292 break;
1293 /* 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 +00001294notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001295 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001296 free(flash->chip);
1297 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001298 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001299
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001300 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001301 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001302
Nico Huber7af0e792016-04-29 16:40:15 +02001303 /* Fill fallback layout covering the whole chip. */
1304 struct single_layout *const fallback = &flash->fallback_layout;
1305 fallback->base.entries = &fallback->entry;
1306 fallback->base.num_entries = 1;
1307 fallback->entry.start = 0;
1308 fallback->entry.end = flash->chip->total_size * 1024 - 1;
1309 fallback->entry.included = true;
Nico Huber70461a92019-06-15 14:56:19 +02001310 fallback->entry.name = strdup("complete flash");
1311 if (!fallback->entry.name) {
1312 msg_cerr("Failed to probe chip: %s\n", strerror(errno));
1313 return -1;
1314 }
Stefan Reinauer051e2362011-01-19 06:21:54 +00001315
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001316 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001317 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
1318 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +00001319 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001320#if CONFIG_INTERNAL == 1
1321 if (programmer_table[programmer].map_flash_region == physmap)
1322 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
1323 PRIxPTR_WIDTH, flash->physical_memory);
1324 else
1325#endif
1326 msg_cinfo("on %s.\n", programmer_table[programmer].name);
Uwe Hermann9899cad2009-06-28 21:47:57 +00001327
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001328 /* Flash registers may more likely not be mapped if the chip was forced.
1329 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +00001330 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001331 if (flash->chip->printlock)
1332 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +00001333
Stefan Tauner4e32ec12014-08-30 23:39:51 +00001334 /* Get out of the way for later runs. */
1335 unmap_flash(flash);
1336
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001337 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001338 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001339}
1340
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001341int read_buf_from_file(unsigned char *buf, unsigned long size,
1342 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001343{
Nico Huber7562f7d2013-08-30 21:29:45 +00001344#ifdef __LIBPAYLOAD__
1345 msg_gerr("Error: No file I/O support in libpayload\n");
1346 return 1;
1347#else
Stefan Tauner16687702015-12-25 21:59:45 +00001348 int ret = 0;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001349
Stefan Tauner16687702015-12-25 21:59:45 +00001350 FILE *image;
Daniel Campellod4063bf2021-04-15 10:36:04 -06001351 if (!strcmp(filename, "-"))
1352 image = fdopen(fileno(stdin), "rb");
1353 else
1354 image = fopen(filename, "rb");
1355 if (image == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001356 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001357 return 1;
1358 }
Stefan Tauner16687702015-12-25 21:59:45 +00001359
1360 struct stat image_stat;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001361 if (fstat(fileno(image), &image_stat) != 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001362 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
Stefan Tauner16687702015-12-25 21:59:45 +00001363 ret = 1;
1364 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001365 }
Daniel Campellod4063bf2021-04-15 10:36:04 -06001366 if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
Daniel Campello45d50a12021-04-13 10:47:25 -06001367 msg_gerr("Error: Image size (%jd B) doesn't match the expected size (%lu B)!\n",
Stefan Taunere038e902013-02-04 04:38:42 +00001368 (intmax_t)image_stat.st_size, size);
Stefan Tauner16687702015-12-25 21:59:45 +00001369 ret = 1;
1370 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001371 }
Stefan Tauner16687702015-12-25 21:59:45 +00001372
1373 unsigned long numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001374 if (numbytes != size) {
1375 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1376 "wanted %ld!\n", numbytes, size);
Stefan Tauner16687702015-12-25 21:59:45 +00001377 ret = 1;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001378 }
Stefan Tauner16687702015-12-25 21:59:45 +00001379out:
1380 (void)fclose(image);
1381 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001382#endif
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001383}
1384
Daniel Campello45d50a12021-04-13 10:47:25 -06001385/**
1386 * @brief Reads content to buffer from one or more files.
1387 *
1388 * Reads content to supplied buffer from files. If a filename is specified for
1389 * individual regions using the partial read syntax ('-i <region>[:<filename>]')
1390 * then this will read file data into the corresponding region in the
1391 * supplied buffer.
1392 *
1393 * @param flashctx Flash context to be used.
1394 * @param buf Chip-sized buffer to write data to
1395 * @return 0 on success
1396 */
1397static int read_buf_from_include_args(const struct flashctx *const flash,
1398 unsigned char *buf)
1399{
1400 const struct flashrom_layout *const layout = get_layout(flash);
1401 const struct romentry *entry = NULL;
1402
1403 /*
1404 * Content will be read from -i args, so they must not overlap since
1405 * we need to know exactly what content to write to the ROM.
1406 */
1407 if (included_regions_overlap(layout)) {
1408 msg_gerr("Error: Included regions must not overlap when writing.\n");
1409 return 1;
1410 }
1411
1412 while ((entry = layout_next_included(layout, entry))) {
1413 if (!entry->file)
1414 continue;
1415 if (read_buf_from_file(buf + entry->start,
1416 entry->end - entry->start + 1, entry->file))
1417 return 1;
1418 }
1419 return 0;
1420}
1421
1422/**
1423 * @brief Writes passed data buffer into a file
1424 *
1425 * @param buf Buffer with data to write
1426 * @param size Size of buffer
1427 * @param filename File path to write to
1428 * @return 0 on success
1429 */
Mark Marshallf20b7be2014-05-09 21:16:21 +00001430int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001431{
Nico Huber7562f7d2013-08-30 21:29:45 +00001432#ifdef __LIBPAYLOAD__
1433 msg_gerr("Error: No file I/O support in libpayload\n");
1434 return 1;
1435#else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001436 FILE *image;
Stefan Tauner16687702015-12-25 21:59:45 +00001437 int ret = 0;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001438
1439 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001440 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001441 return 1;
1442 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001443 if ((image = fopen(filename, "wb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +00001444 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001445 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001446 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001447
Stefan Tauner16687702015-12-25 21:59:45 +00001448 unsigned long numbytes = fwrite(buf, 1, size, image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001449 if (numbytes != size) {
Stefan Tauner16687702015-12-25 21:59:45 +00001450 msg_gerr("Error: file %s could not be written completely.\n", filename);
1451 ret = 1;
1452 goto out;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001453 }
Stefan Tauner16687702015-12-25 21:59:45 +00001454 if (fflush(image)) {
1455 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
1456 ret = 1;
1457 }
1458 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
1459#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
1460 struct stat image_stat;
1461 if (fstat(fileno(image), &image_stat) != 0) {
1462 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
1463 ret = 1;
1464 goto out;
1465 }
1466 if (S_ISREG(image_stat.st_mode)) {
1467 if (fsync(fileno(image))) {
1468 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
1469 ret = 1;
1470 }
1471 }
1472#endif
1473out:
1474 if (fclose(image)) {
1475 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
1476 ret = 1;
1477 }
1478 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001479#endif
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001480}
1481
Daniel Campello45d50a12021-04-13 10:47:25 -06001482/**
1483 * @brief Writes content from buffer to one or more files.
1484 *
1485 * Writes content from supplied buffer to files. If a filename is specified for
1486 * individual regions using the partial read syntax ('-i <region>[:<filename>]')
1487 * then this will write files using data from the corresponding region in the
1488 * supplied buffer.
1489 *
1490 * @param flashctx Flash context to be used.
1491 * @param buf Chip-sized buffer to read data from
1492 * @return 0 on success
1493 */
1494static int write_buf_to_include_args(const struct flashctx *const flash,
1495 unsigned char *buf)
1496{
1497 const struct flashrom_layout *const layout = get_layout(flash);
1498 const struct romentry *entry = NULL;
1499
1500 while ((entry = layout_next_included(layout, entry))) {
1501 if (!entry->file)
1502 continue;
1503 if (write_buf_to_file(buf + entry->start,
1504 entry->end - entry->start + 1, entry->file))
1505 return 1;
1506 }
1507
1508 return 0;
1509}
1510
Nico Huber899e4ec2016-04-29 18:39:01 +02001511static int read_by_layout(struct flashctx *, uint8_t *);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001512int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001513{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001514 unsigned long size = flash->chip->total_size * 1024;
Richard Hughes84b453e2018-12-19 15:30:39 +00001515 unsigned char *buf = calloc(size, sizeof(unsigned char));
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001516 int ret = 0;
1517
1518 msg_cinfo("Reading flash... ");
1519 if (!buf) {
1520 msg_gerr("Memory allocation failed!\n");
1521 msg_cinfo("FAILED.\n");
1522 return 1;
1523 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001524 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001525 msg_cerr("No read function available for this flash chip.\n");
1526 ret = 1;
1527 goto out_free;
1528 }
Nico Huber899e4ec2016-04-29 18:39:01 +02001529 if (read_by_layout(flash, buf)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001530 msg_cerr("Read operation failed!\n");
1531 ret = 1;
1532 goto out_free;
1533 }
Daniel Campello45d50a12021-04-13 10:47:25 -06001534 if (write_buf_to_include_args(flash, buf)) {
1535 ret = 1;
1536 goto out_free;
1537 }
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001538
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001539 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001540out_free:
1541 free(buf);
1542 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1543 return ret;
1544}
1545
Stefan Tauner96658be2014-05-26 22:05:31 +00001546/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001547static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001548{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001549 int i, j, k;
1550 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001551
1552 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1553 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001554 struct block_eraser eraser = chip->block_erasers[k];
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001555
1556 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1557 /* Blocks with zero size are bugs in flashchips.c. */
1558 if (eraser.eraseblocks[i].count &&
1559 !eraser.eraseblocks[i].size) {
1560 msg_gerr("ERROR: Flash chip %s erase function "
1561 "%i region %i has size 0. Please report"
1562 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001563 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001564 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001565 }
1566 /* Blocks with zero count are bugs in flashchips.c. */
1567 if (!eraser.eraseblocks[i].count &&
1568 eraser.eraseblocks[i].size) {
1569 msg_gerr("ERROR: Flash chip %s erase function "
1570 "%i region %i has count 0. Please report"
1571 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001572 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001573 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001574 }
1575 done += eraser.eraseblocks[i].count *
1576 eraser.eraseblocks[i].size;
1577 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001578 /* Empty eraseblock definition with erase function. */
1579 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001580 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001581 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001582 if (!done)
1583 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001584 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001585 msg_gerr("ERROR: Flash chip %s erase function %i "
1586 "region walking resulted in 0x%06x bytes total,"
1587 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001588 " flashrom@flashrom.org\n", chip->name, k,
1589 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001590 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001591 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001592 if (!eraser.block_erase)
1593 continue;
1594 /* Check if there are identical erase functions for different
1595 * layouts. That would imply "magic" erase functions. The
1596 * easiest way to check this is with function pointers.
1597 */
Uwe Hermann43959702010-03-13 17:28:29 +00001598 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001599 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001600 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001601 msg_gerr("ERROR: Flash chip %s erase function "
1602 "%i and %i are identical. Please report"
1603 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001604 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001605 ret = 1;
1606 }
Uwe Hermann43959702010-03-13 17:28:29 +00001607 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001608 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001609 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001610}
1611
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001612static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001613{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001614 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001615
1616 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1617 if (log)
1618 msg_cdbg("not defined. ");
1619 return 1;
1620 }
1621 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1622 if (log)
1623 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001624 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001625 return 1;
1626 }
1627 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1628 if (log)
1629 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001630 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001631 return 1;
1632 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001633 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001634 return 0;
1635}
1636
Nico Huber7af0e792016-04-29 16:40:15 +02001637/**
1638 * @brief Reads the included layout regions into a buffer.
1639 *
1640 * If there is no layout set in the given flash context, the whole chip will
1641 * be read.
1642 *
1643 * @param flashctx Flash context to be used.
1644 * @param buffer Buffer of full chip size to read into.
1645 * @return 0 on success,
1646 * 1 if any read fails.
1647 */
1648static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1649{
1650 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001651 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001652
Nico Huber5ca55232019-06-15 22:29:08 +02001653 while ((entry = layout_next_included(layout, entry))) {
1654 const chipoff_t region_start = entry->start;
1655 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001656
1657 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1658 return 1;
1659 }
1660 return 0;
1661}
1662
1663typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1664/**
1665 * @private
1666 *
1667 * For read-erase-write, `curcontents` and `newcontents` shall point
1668 * to buffers of the chip's size. Both are supposed to be prefilled
1669 * with at least the included layout regions of the current flash
1670 * contents (`curcontents`) and the data to be written to the flash
1671 * (`newcontents`).
1672 *
1673 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1674 *
1675 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1676 */
1677struct walk_info {
1678 uint8_t *curcontents;
1679 const uint8_t *newcontents;
1680 chipoff_t region_start;
1681 chipoff_t region_end;
1682 chipoff_t erase_start;
1683 chipoff_t erase_end;
1684};
1685/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1686typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1687
1688static int walk_eraseblocks(struct flashctx *const flashctx,
1689 struct walk_info *const info,
1690 const size_t erasefunction, const per_blockfn_t per_blockfn)
1691{
1692 int ret;
1693 size_t i, j;
1694 bool first = true;
1695 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1696
1697 info->erase_start = 0;
1698 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1699 /* count==0 for all automatically initialized array
1700 members so the loop below won't be executed for them. */
1701 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1702 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1703
1704 /* Skip any eraseblock that is completely outside the current region. */
1705 if (info->erase_end < info->region_start)
1706 continue;
1707 if (info->region_end < info->erase_start)
1708 break;
1709
1710 /* Print this for every block except the first one. */
1711 if (first)
1712 first = false;
1713 else
1714 msg_cdbg(", ");
1715 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1716
1717 ret = per_blockfn(flashctx, info, eraser->block_erase);
1718 if (ret)
1719 return ret;
1720 }
1721 if (info->region_end < info->erase_start)
1722 break;
1723 }
1724 msg_cdbg("\n");
1725 return 0;
1726}
1727
1728static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1729 const per_blockfn_t per_blockfn)
1730{
1731 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001732 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001733
1734 all_skipped = true;
1735 msg_cinfo("Erasing and writing flash chip... ");
1736
Nico Huber5ca55232019-06-15 22:29:08 +02001737 while ((entry = layout_next_included(layout, entry))) {
1738 info->region_start = entry->start;
1739 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001740
1741 size_t j;
1742 int error = 1; /* retry as long as it's 1 */
1743 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1744 if (j != 0)
1745 msg_cinfo("Looking for another erase function.\n");
1746 msg_cdbg("Trying erase function %zi... ", j);
1747 if (check_block_eraser(flashctx, j, 1))
1748 continue;
1749
1750 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1751 if (error != 1)
1752 break;
1753
1754 if (info->curcontents) {
1755 msg_cinfo("Reading current flash chip contents... ");
1756 if (read_by_layout(flashctx, info->curcontents)) {
1757 /* Now we are truly screwed. Read failed as well. */
1758 msg_cerr("Can't read anymore! Aborting.\n");
1759 /* We have no idea about the flash chip contents, so
1760 retrying with another erase function is pointless. */
1761 error = 2;
1762 break;
1763 }
1764 msg_cinfo("done. ");
1765 }
1766 }
1767 if (error == 1)
1768 msg_cinfo("No usable erase functions left.\n");
1769 if (error) {
1770 msg_cerr("FAILED!\n");
1771 return 1;
1772 }
1773 }
1774 if (all_skipped)
1775 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1776 msg_cinfo("Erase/write done.\n");
1777 return 0;
1778}
1779
1780static int erase_block(struct flashctx *const flashctx,
1781 const struct walk_info *const info, const erasefn_t erasefn)
1782{
1783 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001784 const bool region_unaligned = info->region_start > info->erase_start ||
1785 info->erase_end > info->region_end;
1786 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1787 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001788
Nico Huber6e61e0c2019-01-23 17:07:49 +01001789 /*
1790 * If the region is not erase-block aligned, merge current flash con-
1791 * tents into a new buffer `backup_contents`.
1792 */
1793 if (region_unaligned) {
1794 backup_contents = malloc(erase_len);
1795 erased_contents = malloc(erase_len);
1796 if (!backup_contents || !erased_contents) {
1797 msg_cerr("Out of memory!\n");
1798 ret = 1;
1799 goto _free_ret;
1800 }
1801 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1802 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1803
1804 msg_cdbg("R");
1805 /* Merge data preceding the current region. */
1806 if (info->region_start > info->erase_start) {
1807 const chipoff_t start = info->erase_start;
1808 const chipsize_t len = info->region_start - info->erase_start;
1809 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1810 msg_cerr("Can't read! Aborting.\n");
1811 goto _free_ret;
1812 }
1813 }
1814 /* Merge data following the current region. */
1815 if (info->erase_end > info->region_end) {
1816 const chipoff_t start = info->region_end + 1;
1817 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1818 const chipsize_t len = info->erase_end - info->region_end;
1819 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1820 msg_cerr("Can't read! Aborting.\n");
1821 goto _free_ret;
1822 }
1823 }
1824 }
1825
1826 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001827 all_skipped = false;
1828
1829 msg_cdbg("E");
1830 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001831 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001832 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1833 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001834 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001835 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001836
1837 if (region_unaligned) {
1838 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1839 /* get_next_write() sets starthere to a new value after the call. */
1840 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1841 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1842 if (!writecount++)
1843 msg_cdbg("W");
1844 /* Needs the partial write function signature. */
1845 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1846 info->erase_start + starthere, lenhere))
1847 goto _free_ret;
1848 starthere += lenhere;
1849 }
1850 }
1851
1852 ret = 0;
1853
1854_free_ret:
1855 free(erased_contents);
1856 free(backup_contents);
1857 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001858}
1859
1860/**
1861 * @brief Erases the included layout regions.
1862 *
1863 * If there is no layout set in the given flash context, the whole chip will
1864 * be erased.
1865 *
1866 * @param flashctx Flash context to be used.
1867 * @param buffer Buffer of full chip size to read into.
1868 * @return 0 on success,
1869 * 1 if all available erase functions failed.
1870 */
Nico Huber454f6132012-12-10 13:34:10 +00001871static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001872{
1873 struct walk_info info = { 0 };
1874 return walk_by_layout(flashctx, &info, &erase_block);
1875}
1876
1877static int read_erase_write_block(struct flashctx *const flashctx,
1878 const struct walk_info *const info, const erasefn_t erasefn)
1879{
1880 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1881 const bool region_unaligned = info->region_start > info->erase_start ||
1882 info->erase_end > info->region_end;
1883 const uint8_t *newcontents = NULL;
1884 int ret = 2;
1885
1886 /*
1887 * If the region is not erase-block aligned, merge current flash con-
1888 * tents into `info->curcontents` and a new buffer `newc`. The former
1889 * is necessary since we have no guarantee that the full erase block
1890 * was already read into `info->curcontents`. For the latter a new
1891 * buffer is used since `info->newcontents` might contain data for
1892 * other unaligned regions that touch this erase block too.
1893 */
1894 if (region_unaligned) {
1895 msg_cdbg("R");
1896 uint8_t *const newc = malloc(erase_len);
1897 if (!newc) {
1898 msg_cerr("Out of memory!\n");
1899 return 1;
1900 }
1901 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1902
1903 /* Merge data preceding the current region. */
1904 if (info->region_start > info->erase_start) {
1905 const chipoff_t start = info->erase_start;
1906 const chipsize_t len = info->region_start - info->erase_start;
1907 if (flashctx->chip->read(flashctx, newc, start, len)) {
1908 msg_cerr("Can't read! Aborting.\n");
1909 goto _free_ret;
1910 }
1911 memcpy(info->curcontents + start, newc, len);
1912 }
1913 /* Merge data following the current region. */
1914 if (info->erase_end > info->region_end) {
1915 const chipoff_t start = info->region_end + 1;
1916 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1917 const chipsize_t len = info->erase_end - info->region_end;
1918 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1919 msg_cerr("Can't read! Aborting.\n");
1920 goto _free_ret;
1921 }
1922 memcpy(info->curcontents + start, newc + rel_start, len);
1923 }
1924
1925 newcontents = newc;
1926 } else {
1927 newcontents = info->newcontents + info->erase_start;
1928 }
1929
1930 ret