blob: ae849cf0ee06880aba95e40aa43a96a0c91ef381 [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
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00008 *
Uwe Hermannd1107642007-08-29 17:52:32 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000013 *
Uwe Hermannd1107642007-08-29 17:52:32 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000018 *
Uwe Hermannd1107642007-08-29 17:52:32 +000019 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000022 */
23
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000024#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000025#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000026#ifndef __LIBPAYLOAD__
27#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000028#include <sys/stat.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000029#endif
Ronald G. Minnichceec4202003-07-25 04:37:41 +000030#include <string.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000031#include <stdlib.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"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000041
Mathias Krausea60faab2011-01-17 07:50:42 +000042const char flashrom_version[] = FLASHROM_VERSION;
Ronald G. Minnichceec4202003-07-25 04:37:41 +000043char *chip_to_probe = NULL;
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +000044int verbose_screen = MSG_INFO;
45int verbose_logfile = MSG_DEBUG2;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000046
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +000047static enum programmer programmer = PROGRAMMER_INVALID;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000048
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +000049static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000050
Uwe Hermann48ec1b12010-08-08 17:01:18 +000051/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000052 * Programmers supporting multiple buses can have differing size limits on
53 * each bus. Store the limits for each bus in a common struct.
54 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000055struct decode_sizes max_rom_decode;
56
57/* If nonzero, used as the start address of bottom-aligned flash. */
58unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000059
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000060/* Is writing allowed with this programmer? */
61int programmer_may_write;
62
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000063const struct programmer_entry programmer_table[] = {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000064#if CONFIG_INTERNAL == 1
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000065 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000066 .name = "internal",
Stefan Tauneraf358d62012-12-27 18:40:26 +000067 .type = OTHER,
68 .devs.note = NULL,
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000069 .init = internal_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000070 .map_flash_region = physmap,
71 .unmap_flash_region = physunmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000072 .delay = internal_delay,
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +000073 },
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000074#endif
Luc Verhaegen8e3a6002007-04-04 22:45:58 +000075
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000076#if CONFIG_DUMMY == 1
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000077 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000078 .name = "dummy",
Stefan Tauneraf358d62012-12-27 18:40:26 +000079 .type = OTHER,
80 /* FIXME */
81 .devs.note = "Dummy device, does nothing and logs all accesses\n",
Carl-Daniel Hailfinger1e334e62009-05-11 15:46:43 +000082 .init = dummy_init,
Carl-Daniel Hailfinger1455b2b2009-05-11 14:13:25 +000083 .map_flash_region = dummy_map,
84 .unmap_flash_region = dummy_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000085 .delay = internal_delay,
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000086 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000087#endif
Carl-Daniel Hailfingerc3129202009-05-09 00:54:55 +000088
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +000089#if CONFIG_NIC3COM == 1
Uwe Hermannb4dcb712009-05-13 11:36:06 +000090 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +000091 .name = "nic3com",
Stefan Tauneraf358d62012-12-27 18:40:26 +000092 .type = PCI,
93 .devs.pci = nics_3com,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000094 .init = nic3com_init,
Uwe Hermannc6915932009-05-17 23:12:17 +000095 .map_flash_region = fallback_map,
96 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +000097 .delay = internal_delay,
Uwe Hermannb4dcb712009-05-13 11:36:06 +000098 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +000099#endif
Uwe Hermannb4dcb712009-05-13 11:36:06 +0000100
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000101#if CONFIG_NICREALTEK == 1
Joerg Fischer5665ef32010-05-21 21:54:07 +0000102 {
Carl-Daniel Hailfinger40446ee2011-03-07 01:08:09 +0000103 /* This programmer works for Realtek RTL8139 and SMC 1211. */
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000104 .name = "nicrealtek",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000105 .type = PCI,
106 .devs.pci = nics_realtek,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000107 .init = nicrealtek_init,
108 .map_flash_region = fallback_map,
109 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000110 .delay = internal_delay,
Joerg Fischer5665ef32010-05-21 21:54:07 +0000111 },
Joerg Fischer5665ef32010-05-21 21:54:07 +0000112#endif
113
Andrew Morganc29c2e72010-06-07 22:37:54 +0000114#if CONFIG_NICNATSEMI == 1
115 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000116 .name = "nicnatsemi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000117 .type = PCI,
118 .devs.pci = nics_natsemi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000119 .init = nicnatsemi_init,
120 .map_flash_region = fallback_map,
121 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000122 .delay = internal_delay,
Andrew Morganc29c2e72010-06-07 22:37:54 +0000123 },
124#endif
Joerg Fischer5665ef32010-05-21 21:54:07 +0000125
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000126#if CONFIG_GFXNVIDIA == 1
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000127 {
128 .name = "gfxnvidia",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000129 .type = PCI,
130 .devs.pci = gfx_nvidia,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000131 .init = gfxnvidia_init,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000132 .map_flash_region = fallback_map,
133 .unmap_flash_region = fallback_unmap,
Uwe Hermann2bc98f62009-09-30 18:29:55 +0000134 .delay = internal_delay,
135 },
136#endif
137
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000138#if CONFIG_DRKAISER == 1
Rudolf Marek68720c72009-05-17 19:39:27 +0000139 {
TURBO Jb0912c02009-09-02 23:00:46 +0000140 .name = "drkaiser",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000141 .type = PCI,
142 .devs.pci = drkaiser_pcidev,
TURBO Jb0912c02009-09-02 23:00:46 +0000143 .init = drkaiser_init,
TURBO Jb0912c02009-09-02 23:00:46 +0000144 .map_flash_region = fallback_map,
145 .unmap_flash_region = fallback_unmap,
TURBO Jb0912c02009-09-02 23:00:46 +0000146 .delay = internal_delay,
147 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000148#endif
TURBO Jb0912c02009-09-02 23:00:46 +0000149
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000150#if CONFIG_SATASII == 1
TURBO Jb0912c02009-09-02 23:00:46 +0000151 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000152 .name = "satasii",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000153 .type = PCI,
154 .devs.pci = satas_sii,
Rudolf Marek68720c72009-05-17 19:39:27 +0000155 .init = satasii_init,
Uwe Hermannc6915932009-05-17 23:12:17 +0000156 .map_flash_region = fallback_map,
157 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000158 .delay = internal_delay,
Rudolf Marek68720c72009-05-17 19:39:27 +0000159 },
Carl-Daniel Hailfinger4740c6f2009-09-16 10:09:21 +0000160#endif
Rudolf Marek68720c72009-05-17 19:39:27 +0000161
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000162#if CONFIG_ATAHPT == 1
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000163 {
164 .name = "atahpt",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000165 .type = PCI,
166 .devs.pci = ata_hpt,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000167 .init = atahpt_init,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000168 .map_flash_region = fallback_map,
169 .unmap_flash_region = fallback_unmap,
Uwe Hermannddd5c9e2010-02-21 21:17:00 +0000170 .delay = internal_delay,
171 },
172#endif
173
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000174#if CONFIG_FT2232_SPI == 1
Paul Fox05dfbe62009-06-16 21:08:06 +0000175 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000176 .name = "ft2232_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000177 .type = USB,
178 .devs.usb = devs_ft2232spi,
Paul Fox05dfbe62009-06-16 21:08:06 +0000179 .init = ft2232_spi_init,
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000180 .map_flash_region = fallback_map,
181 .unmap_flash_region = fallback_unmap,
Paul Fox05dfbe62009-06-16 21:08:06 +0000182 .delay = internal_delay,
183 },
Carl-Daniel Hailfinger3426ef62009-08-19 13:27:58 +0000184#endif
Carl-Daniel Hailfinger415e5132009-08-12 11:39:29 +0000185
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000186#if CONFIG_SERPROG == 1
Urja Rannikko22915352009-06-23 11:33:43 +0000187 {
Carl-Daniel Hailfinger37fc4692009-08-12 14:34:35 +0000188 .name = "serprog",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000189 .type = OTHER,
190 /* FIXME */
191 .devs.note = "All programmer devices speaking the serprog protocol\n",
Urja Rannikko22915352009-06-23 11:33:43 +0000192 .init = serprog_init,
Urja Rannikko22915352009-06-23 11:33:43 +0000193 .map_flash_region = fallback_map,
194 .unmap_flash_region = fallback_unmap,
Urja Rannikko22915352009-06-23 11:33:43 +0000195 .delay = serprog_delay,
196 },
Carl-Daniel Hailfinger6be74112009-08-12 16:17:41 +0000197#endif
Paul Fox05dfbe62009-06-16 21:08:06 +0000198
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000199#if CONFIG_BUSPIRATE_SPI == 1
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000200 {
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000201 .name = "buspirate_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000202 .type = OTHER,
203 /* FIXME */
204 .devs.note = "Dangerous Prototypes Bus Pirate\n",
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000205 .init = buspirate_spi_init,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000206 .map_flash_region = fallback_map,
207 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger5cca01f2009-11-24 00:20:03 +0000208 .delay = internal_delay,
209 },
210#endif
211
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +0000212#if CONFIG_DEDIPROG == 1
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000213 {
214 .name = "dediprog",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000215 .type = OTHER,
216 /* FIXME */
217 .devs.note = "Dediprog SF100\n",
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000218 .init = dediprog_init,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000219 .map_flash_region = fallback_map,
220 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerd38fac82010-01-19 11:15:48 +0000221 .delay = internal_delay,
222 },
223#endif
224
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000225#if CONFIG_RAYER_SPI == 1
226 {
227 .name = "rayer_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000228 .type = OTHER,
229 /* FIXME */
230 .devs.note = "RayeR parallel port programmer\n",
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000231 .init = rayer_spi_init,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000232 .map_flash_region = fallback_map,
233 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingere7fdd6e2010-07-21 10:26:01 +0000234 .delay = internal_delay,
235 },
236#endif
237
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000238#if CONFIG_PONY_SPI == 1
239 {
240 .name = "pony_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000241 .type = OTHER,
242 /* FIXME */
243 .devs.note = "Programmers compatible with SI-Prog, serbang or AJAWe\n",
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000244 .init = pony_spi_init,
245 .map_flash_region = fallback_map,
246 .unmap_flash_region = fallback_unmap,
247 .delay = internal_delay,
Stefan Tauneraf358d62012-12-27 18:40:26 +0000248 },
Virgil-Adrian Teacada7c5452012-04-30 23:11:06 +0000249#endif
250
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000251#if CONFIG_NICINTEL == 1
252 {
253 .name = "nicintel",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000254 .type = PCI,
255 .devs.pci = nics_intel,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000256 .init = nicintel_init,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000257 .map_flash_region = fallback_map,
258 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfingerb713d2e2011-05-08 00:24:18 +0000259 .delay = internal_delay,
260 },
261#endif
262
Idwer Vollering004f4b72010-09-03 18:21:21 +0000263#if CONFIG_NICINTEL_SPI == 1
264 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000265 .name = "nicintel_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000266 .type = PCI,
267 .devs.pci = nics_intel_spi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000268 .init = nicintel_spi_init,
269 .map_flash_region = fallback_map,
270 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000271 .delay = internal_delay,
Idwer Vollering004f4b72010-09-03 18:21:21 +0000272 },
273#endif
274
Mark Marshall90021f22010-12-03 14:48:11 +0000275#if CONFIG_OGP_SPI == 1
276 {
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000277 .name = "ogp_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000278 .type = PCI,
279 .devs.pci = ogp_spi,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000280 .init = ogp_spi_init,
281 .map_flash_region = fallback_map,
282 .unmap_flash_region = fallback_unmap,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000283 .delay = internal_delay,
Mark Marshall90021f22010-12-03 14:48:11 +0000284 },
285#endif
286
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000287#if CONFIG_SATAMV == 1
288 {
289 .name = "satamv",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000290 .type = PCI,
291 .devs.pci = satas_mv,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000292 .init = satamv_init,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000293 .map_flash_region = fallback_map,
294 .unmap_flash_region = fallback_unmap,
Carl-Daniel Hailfinger9a1105c2011-02-04 21:37:59 +0000295 .delay = internal_delay,
296 },
297#endif
298
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000299#if CONFIG_LINUX_SPI == 1
300 {
301 .name = "linux_spi",
Stefan Tauneraf358d62012-12-27 18:40:26 +0000302 .type = OTHER,
303 .devs.note = "Device files /dev/spidev*.*\n",
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000304 .init = linux_spi_init,
305 .map_flash_region = fallback_map,
306 .unmap_flash_region = fallback_unmap,
Sven Schnelle5ce5f702011-09-03 18:37:52 +0000307 .delay = internal_delay,
308 },
309#endif
310
Carl-Daniel Hailfinger1c6d2ff2012-08-27 00:44:42 +0000311 {0}, /* This entry corresponds to PROGRAMMER_INVALID. */
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +0000312};
Luc Verhaegen8e3a6002007-04-04 22:45:58 +0000313
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +0000314#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000315static int shutdown_fn_count = 0;
316struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +0000317 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000318 void *data;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000319} static shutdown_fn[SHUTDOWN_MAXFN];
320/* Initialize to 0 to make sure nobody registers a shutdown function before
321 * programmer init.
322 */
323static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000324
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000325static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +0000326
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000327/* Register a function to be executed on programmer shutdown.
328 * The advantage over atexit() is that you can supply a void pointer which will
329 * be used as parameter to the registered function upon programmer shutdown.
330 * This pointer can point to arbitrary data used by said function, e.g. undo
331 * information for GPIO settings etc. If unneeded, set data=NULL.
332 * Please note that the first (void *data) belongs to the function signature of
333 * the function passed as first parameter.
334 */
David Hendricks8bb20212011-06-14 01:35:36 +0000335int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000336{
337 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +0000338 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000339 SHUTDOWN_MAXFN);
340 return 1;
341 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000342 if (!may_register_shutdown) {
343 msg_perr("Tried to register a shutdown function before "
344 "programmer init.\n");
345 return 1;
346 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000347 shutdown_fn[shutdown_fn_count].func = function;
348 shutdown_fn[shutdown_fn_count].data = data;
349 shutdown_fn_count++;
350
351 return 0;
352}
353
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000354int programmer_init(enum programmer prog, char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000355{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000356 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000357
358 if (prog >= PROGRAMMER_INVALID) {
359 msg_perr("Invalid programmer specified!\n");
360 return -1;
361 }
362 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000363 /* Initialize all programmer specific data. */
364 /* Default to unlimited decode sizes. */
365 max_rom_decode = (const struct decode_sizes) {
366 .parallel = 0xffffffff,
367 .lpc = 0xffffffff,
368 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000369 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000370 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000371 /* Default to top aligned flash at 4 GB. */
372 flashbase = 0;
373 /* Registering shutdown functions is now allowed. */
374 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000375 /* Default to allowing writes. Broken programmers set this to 0. */
376 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000377
378 programmer_param = param;
379 msg_pdbg("Initializing %s programmer\n",
380 programmer_table[programmer].name);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000381 ret = programmer_table[programmer].init();
382 if (programmer_param && strlen(programmer_param)) {
383 msg_perr("Unhandled programmer parameters: %s\n",
384 programmer_param);
385 /* Do not error out here, the init itself was successful. */
386 }
387 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000388}
389
390int programmer_shutdown(void)
391{
David Hendricks8bb20212011-06-14 01:35:36 +0000392 int ret = 0;
393
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000394 /* Registering shutdown functions is no longer allowed. */
395 may_register_shutdown = 0;
396 while (shutdown_fn_count > 0) {
397 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000398 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000399 }
Stefan Taunerb8911d62012-12-26 07:55:00 +0000400 programmer_param = NULL;
David Hendricks8bb20212011-06-14 01:35:36 +0000401 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000402}
403
404void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
405 size_t len)
406{
407 return programmer_table[programmer].map_flash_region(descr,
408 phys_addr, len);
409}
410
411void programmer_unmap_flash_region(void *virt_addr, size_t len)
412{
413 programmer_table[programmer].unmap_flash_region(virt_addr, len);
414}
415
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000416void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000417{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000418 flash->pgm->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000419}
420
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000421void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000422{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000423 flash->pgm->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000424}
425
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000426void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000427{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000428 flash->pgm->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000429}
430
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000431void chip_writen(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
432 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000433{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000434 flash->pgm->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000435}
436
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000437uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000438{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000439 return flash->pgm->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000440}
441
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000442uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000443{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000444 return flash->pgm->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000445}
446
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000447uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000448{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000449 return flash->pgm->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000450}
451
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000452void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
453 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000454{
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000455 flash->pgm->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000456}
457
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000458void programmer_delay(int usecs)
459{
460 programmer_table[programmer].delay(usecs);
461}
462
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000463void map_flash_registers(struct flashctx *flash)
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000464{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000465 size_t size = flash->chip->total_size * 1024;
Carl-Daniel Hailfingerd0fc9462009-05-11 14:01:17 +0000466 /* Flash registers live 4 MByte below the flash. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +0000467 /* FIXME: This is incorrect for nonstandard flashbase. */
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000468 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
Stefan Reinauerff4f1972007-05-24 08:48:10 +0000469}
470
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000471int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
472 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000473{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000474 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000475
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000476 return 0;
477}
478
Carl-Daniel Hailfinger38a059d2009-06-13 12:04:03 +0000479int min(int a, int b)
480{
481 return (a < b) ? a : b;
482}
483
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000484int max(int a, int b)
485{
486 return (a > b) ? a : b;
487}
488
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000489int bitcount(unsigned long a)
490{
491 int i = 0;
492 for (; a != 0; a >>= 1)
493 if (a & 1)
494 i++;
495 return i;
496}
497
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +0000498void tolower_string(char *str)
499{
500 for (; *str != '\0'; str++)
501 *str = (char)tolower((unsigned char)*str);
502}
503
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000504char *strcat_realloc(char *dest, const char *src)
505{
506 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000507 if (!dest) {
508 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000509 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000510 }
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000511 strcat(dest, src);
512 return dest;
513}
514
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000515/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000516 * It will look for needle with a subsequent '=' in haystack, return a copy of
517 * needle and remove everything from the first occurrence of needle to the next
518 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000519 */
Stefan Tauner66652442011-06-26 17:38:17 +0000520char *extract_param(char **haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000521{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000522 char *param_pos, *opt_pos, *rest;
523 char *opt = NULL;
524 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000525 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000526
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000527 needlelen = strlen(needle);
528 if (!needlelen) {
529 msg_gerr("%s: empty needle! Please report a bug at "
530 "flashrom@flashrom.org\n", __func__);
531 return NULL;
532 }
533 /* No programmer parameters given. */
534 if (*haystack == NULL)
535 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000536 param_pos = strstr(*haystack, needle);
537 do {
538 if (!param_pos)
539 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000540 /* Needle followed by '='? */
541 if (param_pos[needlelen] == '=') {
542
543 /* Beginning of the string? */
544 if (param_pos == *haystack)
545 break;
546 /* After a delimiter? */
547 if (strchr(delim, *(param_pos - 1)))
548 break;
549 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000550 /* Continue searching. */
551 param_pos++;
552 param_pos = strstr(param_pos, needle);
553 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000554
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000555 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000556 /* Get the string after needle and '='. */
557 opt_pos = param_pos + needlelen + 1;
558 optlen = strcspn(opt_pos, delim);
559 /* Return an empty string if the parameter was empty. */
560 opt = malloc(optlen + 1);
561 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000562 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000563 exit(1);
564 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000565 strncpy(opt, opt_pos, optlen);
566 opt[optlen] = '\0';
567 rest = opt_pos + optlen;
568 /* Skip all delimiters after the current parameter. */
569 rest += strspn(rest, delim);
570 memmove(param_pos, rest, strlen(rest) + 1);
571 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000572 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000573
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000574 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000575}
576
Stefan Tauner66652442011-06-26 17:38:17 +0000577char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000578{
579 return extract_param(&programmer_param, param_name, ",");
580}
581
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000582/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000583static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000584{
585 unsigned int usable_erasefunctions = 0;
586 int k;
587 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
588 if (!check_block_eraser(flash, k, 0))
589 usable_erasefunctions++;
590 }
591 return usable_erasefunctions;
592}
593
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000594int compare_range(uint8_t *wantbuf, uint8_t *havebuf, unsigned int start, unsigned int len)
595{
596 int ret = 0, failcount = 0;
597 unsigned int i;
598 for (i = 0; i < len; i++) {
599 if (wantbuf[i] != havebuf[i]) {
600 /* Only print the first failure. */
601 if (!failcount++)
602 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
603 start + i, wantbuf[i], havebuf[i]);
604 }
605 }
606 if (failcount) {
607 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
608 start, start + len - 1, failcount);
609 ret = -1;
610 }
611 return ret;
612}
613
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000614/* start is an offset to the base address of the flash chip */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000615int check_erased_range(struct flashctx *flash, unsigned int start,
616 unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000617{
618 int ret;
619 uint8_t *cmpbuf = malloc(len);
620
621 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000622 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000623 exit(1);
624 }
625 memset(cmpbuf, 0xff, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000626 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000627 free(cmpbuf);
628 return ret;
629}
630
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000631/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000632 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000633 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000634 * @start offset to the base address of the flash chip
635 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000636 * @return 0 for success, -1 for failure
637 */
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000638int verify_range(struct flashctx *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000639{
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000640 uint8_t *readbuf = malloc(len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000641 int ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000642
643 if (!len)
644 goto out_free;
645
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000646 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000647 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000648 return 1;
649 }
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000650 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000651 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000652 exit(1);
653 }
654
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000655 if (start + len > flash->chip->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000656 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000657 " total_size 0x%x\n", __func__, start, len,
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000658 flash->chip->total_size * 1024);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000659 ret = -1;
660 goto out_free;
661 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000662
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000663 ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000664 if (ret) {
665 msg_gerr("Verification impossible because read failed "
666 "at 0x%x (len 0x%x)\n", start, len);
667 return ret;
668 }
669
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000670 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000671out_free:
672 free(readbuf);
673 return ret;
674}
675
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000676/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000677 * Check if the buffer @have can be programmed to the content of @want without
678 * erasing. This is only possible if all chunks of size @gran are either kept
679 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000680 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000681 * Warning: This function assumes that @have and @want point to naturally
682 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000683 *
684 * @have buffer with current content
685 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000686 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000687 * @gran write granularity (enum, not count)
688 * @return 0 if no erase is needed, 1 otherwise
689 */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000690int need_erase(uint8_t *have, uint8_t *want, unsigned int len, enum write_granularity gran)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000691{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000692 int result = 0;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000693 unsigned int i, j, limit;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000694
695 switch (gran) {
696 case write_gran_1bit:
697 for (i = 0; i < len; i++)
698 if ((have[i] & want[i]) != want[i]) {
699 result = 1;
700 break;
701 }
702 break;
703 case write_gran_1byte:
704 for (i = 0; i < len; i++)
705 if ((have[i] != want[i]) && (have[i] != 0xff)) {
706 result = 1;
707 break;
708 }
709 break;
710 case write_gran_256bytes:
711 for (j = 0; j < len / 256; j++) {
712 limit = min (256, len - j * 256);
Uwe Hermann43959702010-03-13 17:28:29 +0000713 /* Are 'have' and 'want' identical? */
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000714 if (!memcmp(have + j * 256, want + j * 256, limit))
715 continue;
716 /* have needs to be in erased state. */
717 for (i = 0; i < limit; i++)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000718 if (have[j * 256 + i] != 0xff) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000719 result = 1;
720 break;
721 }
722 if (result)
723 break;
724 }
725 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000726 default:
727 msg_cerr("%s: Unsupported granularity! Please report a bug at "
728 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000729 }
730 return result;
731}
732
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000733/**
734 * Check if the buffer @have needs to be programmed to get the content of @want.
735 * If yes, return 1 and fill in first_start with the start address of the
736 * write operation and first_len with the length of the first to-be-written
737 * chunk. If not, return 0 and leave first_start and first_len undefined.
738 *
739 * Warning: This function assumes that @have and @want point to naturally
740 * aligned regions.
741 *
742 * @have buffer with current content
743 * @want buffer with desired content
744 * @len length of the checked area
745 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000746 * @first_start offset of the first byte which needs to be written (passed in
747 * value is increased by the offset of the first needed write
748 * relative to have/want or unchanged if no write is needed)
749 * @return length of the first contiguous area which needs to be written
750 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000751 *
752 * FIXME: This function needs a parameter which tells it about coalescing
753 * in relation to the max write length of the programmer and the max write
754 * length of the chip.
755 */
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000756static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
757 unsigned int *first_start,
758 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000759{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000760 int need_write = 0;
761 unsigned int rel_start = 0, first_len = 0;
762 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000763
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000764 switch (gran) {
765 case write_gran_1bit:
766 case write_gran_1byte:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000767 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000768 break;
769 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000770 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000771 break;
772 default:
773 msg_cerr("%s: Unsupported granularity! Please report a bug at "
774 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000775 /* Claim that no write was needed. A write with unknown
776 * granularity is too dangerous to try.
777 */
778 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000779 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000780 for (i = 0; i < len / stride; i++) {
781 limit = min(stride, len - i * stride);
782 /* Are 'have' and 'want' identical? */
783 if (memcmp(have + i * stride, want + i * stride, limit)) {
784 if (!need_write) {
785 /* First location where have and want differ. */
786 need_write = 1;
787 rel_start = i * stride;
788 }
789 } else {
790 if (need_write) {
791 /* First location where have and want
792 * do not differ anymore.
793 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000794 break;
795 }
796 }
797 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000798 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000799 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000800 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000801 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000802}
803
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000804/* This function generates various test patterns useful for testing controller
805 * and chip communication as well as chip behaviour.
806 *
807 * If a byte can be written multiple times, each time keeping 0-bits at 0
808 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
809 * is essentially an AND operation. That's also the reason why this function
810 * provides the result of AND between various patterns.
811 *
812 * Below is a list of patterns (and their block length).
813 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
814 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
815 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
816 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
817 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
818 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
819 * Pattern 6 is 00 (1 Byte)
820 * Pattern 7 is ff (1 Byte)
821 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
822 * byte block.
823 *
824 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
825 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
826 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
827 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
828 * Pattern 12 is 00 (1 Byte)
829 * Pattern 13 is ff (1 Byte)
830 * Patterns 8-13 have no block number.
831 *
832 * Patterns 0-3 are created to detect and efficiently diagnose communication
833 * slips like missed bits or bytes and their repetitive nature gives good visual
834 * cues to the person inspecting the results. In addition, the following holds:
835 * AND Pattern 0/1 == Pattern 4
836 * AND Pattern 2/3 == Pattern 5
837 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
838 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
839 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
840 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
841 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
842 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
843 * Besides that, they provide for bit testing of the last two bytes of every
844 * 256 byte block which contains the block number for patterns 0-6.
845 * Patterns 10-11 are special purpose for detecting subblock aliasing with
846 * block sizes >256 bytes (some Dataflash chips etc.)
847 * AND Pattern 8/9 == Pattern 12
848 * AND Pattern 10/11 == Pattern 12
849 * Pattern 13 is the completely erased state.
850 * None of the patterns can detect aliasing at boundaries which are a multiple
851 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
852 */
853int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
854{
855 int i;
856
857 if (!buf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000858 msg_gerr("Invalid buffer!\n");
Carl-Daniel Hailfingereaac68b2009-11-23 12:55:31 +0000859 return 1;
860 }
861
862 switch (variant) {
863 case 0:
864 for (i = 0; i < size; i++)
865 buf[i] = (i & 0xf) << 4 | 0x5;
866 break;
867 case 1:
868 for (i = 0; i < size; i++)
869 buf[i] = (i & 0xf) << 4 | 0xa;
870 break;
871 case 2:
872 for (i = 0; i < size; i++)
873 buf[i] = 0x50 | (i & 0xf);
874 break;
875 case 3:
876 for (i = 0; i < size; i++)
877 buf[i] = 0xa0 | (i & 0xf);
878 break;
879 case 4:
880 for (i = 0; i < size; i++)
881 buf[i] = (i & 0xf) << 4;
882 break;
883 case 5:
884 for (i = 0; i < size; i++)
885 buf[i] = i & 0xf;
886 break;
887 case 6:
888 memset(buf, 0x00, size);
889 break;
890 case 7:
891 memset(buf, 0xff, size);
892 break;
893 case 8:
894 for (i = 0; i < size; i++)
895 buf[i] = i & 0xff;
896 break;
897 case 9:
898 for (i = 0; i < size; i++)
899 buf[i] = ~(i & 0xff);
900 break;
901 case 10:
902 for (i = 0; i < size % 2; i++) {
903 buf[i * 2] = (i >> 8) & 0xff;
904 buf[i * 2 + 1] = i & 0xff;
905 }
906 if (size & 0x1)
907 buf[i * 2] = (i >> 8) & 0xff;
908 break;
909 case 11:
910 for (i = 0; i < size % 2; i++) {
911 buf[i * 2] = ~((i >> 8) & 0xff);
912 buf[i * 2 + 1] = ~(i & 0xff);
913 }
914 if (size & 0x1)
915 buf[i * 2] = ~((i >> 8) & 0xff);
916 break;
917 case 12:
918 memset(buf, 0x00, size);
919 break;
920 case 13:
921 memset(buf, 0xff, size);
922 break;
923 }
924
925 if ((variant >= 0) && (variant <= 7)) {
926 /* Write block number in the last two bytes of each 256-byte
927 * block, big endian for easier reading of the hexdump.
928 * Note that this wraps around for chips larger than 2^24 bytes
929 * (16 MB).
930 */
931 for (i = 0; i < size / 256; i++) {
932 buf[i * 256 + 254] = (i >> 8) & 0xff;
933 buf[i * 256 + 255] = i & 0xff;
934 }
935 }
936
937 return 0;
938}
939
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000940int check_max_decode(enum chipbustype buses, uint32_t size)
941{
942 int limitexceeded = 0;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000943
944 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000945 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000946 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000947 "size %u kB of chipset/board/programmer "
948 "for %s interface, "
949 "probe/read/erase/write may fail. ", size / 1024,
950 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000951 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000952 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000953 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000954 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000955 "size %u kB of chipset/board/programmer "
956 "for %s interface, "
957 "probe/read/erase/write may fail. ", size / 1024,
958 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000959 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000960 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000961 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000962 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000963 "size %u kB of chipset/board/programmer "
964 "for %s interface, "
965 "probe/read/erase/write may fail. ", size / 1024,
966 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000967 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000968 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000969 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000970 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000971 "size %u kB of chipset/board/programmer "
972 "for %s interface, "
973 "probe/read/erase/write may fail. ", size / 1024,
974 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000975 }
976 if (!limitexceeded)
977 return 0;
978 /* Sometimes chip and programmer have more than one bus in common,
979 * and the limit is not exceeded on all buses. Tell the user.
980 */
981 if (bitcount(buses) > limitexceeded)
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +0000982 /* FIXME: This message is designed towards CLI users. */
Sean Nelson316a29f2010-05-07 20:09:04 +0000983 msg_pdbg("There is at least one common chip/programmer "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000984 "interface which can support a chip of this size. "
985 "You can try --force at your own risk.\n");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000986 return 1;
987}
988
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000989int probe_flash(struct registered_programmer *pgm, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000990{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000991 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000992 unsigned long base = 0;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000993 char location[64];
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000994 uint32_t size;
995 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000996 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000997
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000998 for (chip = flashchips + startchip; chip && chip->name; chip++) {
999 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +00001000 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001001 buses_common = pgm->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +00001002 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +00001003 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001004 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
1005 if (!chip->probe && !force) {
1006 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +00001007 continue;
1008 }
Stefan Reinauer70385642007-04-06 11:58:03 +00001009
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001010 size = chip->total_size * 1024;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +00001011 check_max_decode(buses_common, size);
Stefan Reinauer70385642007-04-06 11:58:03 +00001012
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001013 /* Start filling in the dynamic data. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001014 flash->chip = calloc(1, sizeof(struct flashchip));
1015 if (!flash->chip) {
1016 msg_gerr("Out of memory!\n");
1017 exit(1);
1018 }
1019 memcpy(flash->chip, chip, sizeof(struct flashchip));
1020 flash->pgm = pgm;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001021
Carl-Daniel Hailfinger97d6b092009-05-09 07:27:23 +00001022 base = flashbase ? flashbase : (0xffffffff - size + 1);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001023 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001024
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001025 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
1026 * is only called with force=1 after normal probing failed.
1027 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001028 if (force)
1029 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +00001030
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001031 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +00001032 goto notfound;
1033
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001034 /* If this is the first chip found, accept it.
1035 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001036 * a non-generic match. SFDP and CFI are generic matches.
1037 * startchip==0 means this call to probe_flash() is the first
1038 * one for this programmer interface and thus no other chip has
1039 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001040 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001041 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001042 msg_cinfo("===\n"
1043 "SFDP has autodetected a flash chip which is "
1044 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001045 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001046 msg_cinfo("The standard operations read and "
1047 "verify should work, but to support "
1048 "erase, write and all other "
1049 "possible features");
1050 else
1051 msg_cinfo("All standard operations (read, "
1052 "verify, erase and write) should "
1053 "work, but to support all possible "
1054 "features");
1055
Stefan Taunerb4e06bd2012-08-20 00:24:22 +00001056 msg_cinfo(" we need to add them manually.\n"
1057 "You can help us by mailing us the output of the following command to "
1058 "flashrom@flashrom.org:\n"
1059 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
1060 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +00001061 "===\n");
1062 }
1063
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001064 /* First flash chip detected on this bus. */
1065 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001066 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001067 /* Not the first flash chip detected on this bus, but not a generic match either. */
1068 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
1069 break;
1070 /* 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 +00001071notfound:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001072 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
1073 flash->virtual_memory = (chipaddr)NULL;
1074 free(flash->chip);
1075 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001076 }
Uwe Hermannffec5f32007-08-23 16:08:21 +00001077
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001078 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001079 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +00001080
Carl-Daniel Hailfingerd9535582011-03-08 00:09:11 +00001081#if CONFIG_INTERNAL == 1
1082 if (programmer_table[programmer].map_flash_region == physmap)
Stefan Reinauer051e2362011-01-19 06:21:54 +00001083 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
Carl-Daniel Hailfingerd9535582011-03-08 00:09:11 +00001084 else
1085#endif
Stefan Reinauer051e2362011-01-19 06:21:54 +00001086 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
Stefan Reinauer051e2362011-01-19 06:21:54 +00001087
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001088 tmp = flashbuses_to_text(flash->chip->bustype);
1089 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) %s.\n", force ? "Assuming" : "Found",
1090 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp, location);
Stefan Tauner00155492011-06-26 20:45:35 +00001091 free(tmp);
Uwe Hermann9899cad2009-06-28 21:47:57 +00001092
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +00001093 /* Flash registers will not be mapped if the chip was forced. Lock info
1094 * may be stored in registers, so avoid lock info printing.
1095 */
1096 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001097 if (flash->chip->printlock)
1098 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +00001099
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +00001100 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001101 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001102}
1103
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001104int read_buf_from_file(unsigned char *buf, unsigned long size,
1105 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001106{
1107 unsigned long numbytes;
1108 FILE *image;
1109 struct stat image_stat;
1110
1111 if ((image = fopen(filename, "rb")) == NULL) {
1112 perror(filename);
1113 return 1;
1114 }
1115 if (fstat(fileno(image), &image_stat) != 0) {
1116 perror(filename);
1117 fclose(image);
1118 return 1;
1119 }
1120 if (image_stat.st_size != size) {
Stefan Taunerd7d423b2012-10-20 09:13:16 +00001121 msg_gerr("Error: Image size (%ld B) doesn't match the flash chip's size (%ld B)!\n",
1122 image_stat.st_size, size);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001123 fclose(image);
1124 return 1;
1125 }
1126 numbytes = fread(buf, 1, size, image);
1127 if (fclose(image)) {
1128 perror(filename);
1129 return 1;
1130 }
1131 if (numbytes != size) {
1132 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1133 "wanted %ld!\n", numbytes, size);
1134 return 1;
1135 }
1136 return 0;
1137}
1138
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001139int write_buf_to_file(unsigned char *buf, unsigned long size,
1140 const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001141{
1142 unsigned long numbytes;
1143 FILE *image;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001144
1145 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001146 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +00001147 return 1;
1148 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +00001149 if ((image = fopen(filename, "wb")) == NULL) {
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001150 perror(filename);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00001151 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001152 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001153
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001154 numbytes = fwrite(buf, 1, size, image);
1155 fclose(image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001156 if (numbytes != size) {
1157 msg_gerr("File %s could not be written completely.\n",
1158 filename);
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001159 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001160 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001161 return 0;
1162}
1163
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001164int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001165{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001166 unsigned long size = flash->chip->total_size * 1024;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001167 unsigned char *buf = calloc(size, sizeof(char));
1168 int ret = 0;
1169
1170 msg_cinfo("Reading flash... ");
1171 if (!buf) {
1172 msg_gerr("Memory allocation failed!\n");
1173 msg_cinfo("FAILED.\n");
1174 return 1;
1175 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001176 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001177 msg_cerr("No read function available for this flash chip.\n");
1178 ret = 1;
1179 goto out_free;
1180 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001181 if (flash->chip->read(flash, buf, 0, size)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001182 msg_cerr("Read operation failed!\n");
1183 ret = 1;
1184 goto out_free;
1185 }
1186
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001187 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001188out_free:
1189 free(buf);
1190 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1191 return ret;
1192}
1193
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001194/* This function shares a lot of its structure with erase_and_write_flash() and
1195 * walk_eraseregions().
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001196 * Even if an error is found, the function will keep going and check the rest.
1197 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001198static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001199{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001200 int i, j, k;
1201 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001202
1203 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1204 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001205 struct block_eraser eraser = chip->block_erasers[k];
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001206
1207 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1208 /* Blocks with zero size are bugs in flashchips.c. */
1209 if (eraser.eraseblocks[i].count &&
1210 !eraser.eraseblocks[i].size) {
1211 msg_gerr("ERROR: Flash chip %s erase function "
1212 "%i region %i has size 0. Please report"
1213 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001214 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001215 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001216 }
1217 /* Blocks with zero count are bugs in flashchips.c. */
1218 if (!eraser.eraseblocks[i].count &&
1219 eraser.eraseblocks[i].size) {
1220 msg_gerr("ERROR: Flash chip %s erase function "
1221 "%i region %i has count 0. Please report"
1222 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001223 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001224 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001225 }
1226 done += eraser.eraseblocks[i].count *
1227 eraser.eraseblocks[i].size;
1228 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001229 /* Empty eraseblock definition with erase function. */
1230 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001231 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001232 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001233 if (!done)
1234 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001235 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001236 msg_gerr("ERROR: Flash chip %s erase function %i "
1237 "region walking resulted in 0x%06x bytes total,"
1238 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001239 " flashrom@flashrom.org\n", chip->name, k,
1240 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001241 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001242 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001243 if (!eraser.block_erase)
1244 continue;
1245 /* Check if there are identical erase functions for different
1246 * layouts. That would imply "magic" erase functions. The
1247 * easiest way to check this is with function pointers.
1248 */
Uwe Hermann43959702010-03-13 17:28:29 +00001249 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001250 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001251 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001252 msg_gerr("ERROR: Flash chip %s erase function "
1253 "%i and %i are identical. Please report"
1254 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001255 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001256 ret = 1;
1257 }
Uwe Hermann43959702010-03-13 17:28:29 +00001258 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001259 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001260 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001261}
1262
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001263static int erase_and_write_block_helper(struct flashctx *flash,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001264 unsigned int start, unsigned int len,
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001265 uint8_t *curcontents,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001266 uint8_t *newcontents,
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001267 int (*erasefn) (struct flashctx *flash,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001268 unsigned int addr,
1269 unsigned int len))
1270{
Stefan Taunerc69c9c82011-11-23 09:13:48 +00001271 unsigned int starthere = 0, lenhere = 0;
1272 int ret = 0, skip = 1, writecount = 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001273 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1274
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001275 /* curcontents and newcontents are opaque to walk_eraseregions, and
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001276 * need to be adjusted here to keep the impression of proper abstraction
1277 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001278 curcontents += start;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001279 newcontents += start;
1280 msg_cdbg(":");
1281 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001282 if (need_erase(curcontents, newcontents, len, gran)) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001283 msg_cdbg("E");
1284 ret = erasefn(flash, start, len);
1285 if (ret)
1286 return ret;
Carl-Daniel Hailfingerb4061f62011-06-26 17:04:16 +00001287 if (check_erased_range(flash, start, len)) {
1288 msg_cerr("ERASE FAILED!\n");
1289 return -1;
1290 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001291 /* Erase was successful. Adjust curcontents. */
1292 memset(curcontents, 0xff, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001293 skip = 0;
1294 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001295 /* get_next_write() sets starthere to a new value after the call. */
1296 while ((lenhere = get_next_write(curcontents + starthere,
1297 newcontents + starthere,
1298 len - starthere, &starthere, gran))) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001299 if (!writecount++)
1300 msg_cdbg("W");
1301 /* Needs the partial write function signature. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001302 ret = flash->chip->write(flash, newcontents + starthere,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001303 start + starthere, lenhere);
1304 if (ret)
1305 return ret;
1306 starthere += lenhere;
1307 skip = 0;
1308 }
1309 if (skip)
1310 msg_cdbg("S");
1311 return ret;
1312}
1313
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001314static int walk_eraseregions(struct flashctx *flash, int erasefunction,
1315 int (*do_something) (struct flashctx *flash,
Carl-Daniel Hailfinger4d3e9ca2010-07-13 00:42:00 +00001316 unsigned int addr,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001317 unsigned int len,
1318 uint8_t *param1,
1319 uint8_t *param2,
1320 int (*erasefn) (
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001321 struct flashctx *flash,
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001322 unsigned int addr,
1323 unsigned int len)),
1324 void *param1, void *param2)
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001325{
1326 int i, j;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001327 unsigned int start = 0;
1328 unsigned int len;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001329 struct block_eraser eraser = flash->chip->block_erasers[erasefunction];
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001330
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001331 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1332 /* count==0 for all automatically initialized array
1333 * members so the loop below won't be executed for them.
1334 */
1335 len = eraser.eraseblocks[i].size;
1336 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001337 /* Print this for every block except the first one. */
1338 if (i || j)
1339 msg_cdbg(", ");
1340 msg_cdbg("0x%06x-0x%06x", start,
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001341 start + len - 1);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001342 if (do_something(flash, start, len, param1, param2,
1343 eraser.block_erase)) {
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001344 return 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001345 }
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001346 start += len;
1347 }
1348 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001349 msg_cdbg("\n");
Carl-Daniel Hailfingera1223412010-07-13 00:37:19 +00001350 return 0;
1351}
1352
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001353static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001354{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001355 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001356
1357 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1358 if (log)
1359 msg_cdbg("not defined. ");
1360 return 1;
1361 }
1362 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1363 if (log)
1364 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001365 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001366 return 1;
1367 }
1368 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1369 if (log)
1370 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001371 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001372 return 1;
1373 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001374 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001375 return 0;
1376}
1377
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001378int erase_and_write_flash(struct flashctx *flash, uint8_t *oldcontents,
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001379 uint8_t *newcontents)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001380{
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001381 int k, ret = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001382 uint8_t *curcontents;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001383 unsigned long size = flash->chip->total_size * 1024;
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001384 unsigned int usable_erasefunctions = count_usable_erasers(flash);
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001385
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001386 msg_cinfo("Erasing and writing flash chip... ");
Stefan Tauner269de352011-07-12 22:35:21 +00001387 curcontents = malloc(size);
1388 if (!curcontents) {
1389 msg_gerr("Out of memory!\n");
1390 exit(1);
1391 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001392 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1393 memcpy(curcontents, oldcontents, size);
1394
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001395 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001396 if (k != 0)
1397 msg_cdbg("Looking for another erase function.\n");
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001398 if (!usable_erasefunctions) {
1399 msg_cdbg("No usable erase functions left.\n");
1400 break;
1401 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001402 msg_cdbg("Trying erase function %i... ", k);
1403 if (check_block_eraser(flash, k, 1))
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001404 continue;
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001405 usable_erasefunctions--;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001406 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1407 curcontents, newcontents);
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001408 /* If everything is OK, don't try another erase function. */
1409 if (!ret)
1410 break;
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001411 /* Write/erase failed, so try to find out what the current chip
Carl-Daniel Hailfingerd8334db2011-07-21 21:21:04 +00001412 * contents are. If no usable erase functions remain, we can
1413 * skip this: the next iteration will break immediately anyway.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001414 */
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001415 if (!usable_erasefunctions)
1416 continue;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001417 /* Reading the whole chip may take a while, inform the user even
1418 * in non-verbose mode.
1419 */
1420 msg_cinfo("Reading current flash chip contents... ");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001421 if (flash->chip->read(flash, curcontents, 0, size)) {
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001422 /* Now we are truly screwed. Read failed as well. */
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001423 msg_cerr("Can't read anymore! Aborting.\n");
Carl-Daniel Hailfingerb23b1eb2010-12-02 02:41:55 +00001424 /* We have no idea about the flash chip contents, so
1425 * retrying with another erase function is pointless.
1426 */
1427 break;
1428 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001429 msg_cinfo("done. ");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001430 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001431 /* Free the scratchpad. */
1432 free(curcontents);
Carl-Daniel Hailfingerf160a122009-05-08 17:15:15 +00001433
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001434 if (ret) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001435 msg_cerr("FAILED!\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001436 } else {
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001437 msg_cinfo("Erase/write done.\n");
Carl-Daniel Hailfingerf38431a2009-09-05 02:30:58 +00001438 }
1439 return ret;
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001440}
1441
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001442void nonfatal_help_message(void)
1443{
1444 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1445 "This means we have to add special support for your board, "
1446 "programmer or flash chip.\n"
1447 "Please report this on IRC at irc.freenode.net (channel "
1448 "#flashrom) or\n"
1449 "mail flashrom@flashrom.org!\n"
1450 "-------------------------------------------------------------"
1451 "------------------\n"
1452 "You may now reboot or simply leave the machine running.\n");
1453}
1454
Uwe Hermannc67d0372009-10-01 18:40:02 +00001455void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001456{
Sean Nelson316a29f2010-05-07 20:09:04 +00001457 msg_gerr("Your flash chip is in an unknown state.\n"
Stefan Tauner2abab942012-04-27 20:41:23 +00001458 "Get help on IRC at chat.freenode.net (channel #flashrom) or\n"
Stefan Tauner78ffbea2012-10-27 15:36:56 +00001459 "mail flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!\n"
1460 "-------------------------------------------------------------------------------\n"
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001461 "DO NOT REBOOT OR POWEROFF!\n");
1462}
1463
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001464/* The way to go if you want a delimited list of programmers */
Stefan Tauner66652442011-06-26 17:38:17 +00001465void list_programmers(const char *delim)
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001466{
1467 enum programmer p;
1468 for (p = 0; p < PROGRAMMER_INVALID; p++) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001469 msg_ginfo("%s", programmer_table[p].name);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001470 if (p < PROGRAMMER_INVALID - 1)
Sean Nelson316a29f2010-05-07 20:09:04 +00001471 msg_ginfo("%s", delim);
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001472 }
Sean Nelson316a29f2010-05-07 20:09:04 +00001473 msg_ginfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001474}
1475
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001476void list_programmers_linebreak(int startcol, int cols, int paren)
1477{
1478 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001479 int pnamelen;
1480 int remaining = 0, firstline = 1;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001481 enum programmer p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001482 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001483
1484 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1485 pname = programmer_table[p].name;
1486 pnamelen = strlen(pname);
1487 if (remaining - pnamelen - 2 < 0) {
1488 if (firstline)
1489 firstline = 0;
1490 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001491 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001492 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001493 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001494 remaining = cols - startcol;
1495 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001496 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001497 remaining--;
1498 }
1499 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001500 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001501 remaining--;
1502 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001503 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001504 remaining -= pnamelen;
1505 if (p < PROGRAMMER_INVALID - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001506 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001507 remaining--;
1508 } else {
1509 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001510 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001511 }
1512 }
1513}
1514
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001515void print_sysinfo(void)
1516{
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001517#ifdef _WIN32
1518 SYSTEM_INFO si;
1519 OSVERSIONINFOEX osvi;
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001520
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001521 memset(&si, 0, sizeof(SYSTEM_INFO));
1522 memset(&osvi, 0, sizeof(OSVERSIONINFOEX));
1523 msg_ginfo(" on Windows");
1524 /* Tell Windows which version of the structure we want. */
1525 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1526 if (GetVersionEx((OSVERSIONINFO*) &osvi))
1527 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
1528 else
1529 msg_ginfo(" unknown version");
1530 GetSystemInfo(&si);
1531 switch (si.wProcessorArchitecture) {
1532 case PROCESSOR_ARCHITECTURE_AMD64:
1533 msg_ginfo(" (x86_64)");
1534 break;
1535 case PROCESSOR_ARCHITECTURE_INTEL:
1536 msg_ginfo(" (x86)");
1537 break;
1538 default:
1539 msg_ginfo(" (unknown arch)");
1540 break;
1541 }
1542#elif HAVE_UTSNAME == 1
1543 struct utsname osinfo;
1544
1545 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001546 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1547 osinfo.machine);
1548#else
1549 msg_ginfo(" on unknown machine");
1550#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001551}
1552
1553void print_buildinfo(void)
1554{
1555 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001556#if NEED_PCI == 1
1557#ifdef PCILIB_VERSION
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001558 msg_gdbg(" libpci %s,", PCILIB_VERSION);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001559#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001560 msg_gdbg(" unknown PCI library,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001561#endif
1562#endif
1563#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001564 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001565#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001566 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001567#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001568 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001569#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001570#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001571 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001572#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001573 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001574#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001575 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001576#endif
1577#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001578 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001579#endif
1580#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001581 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001582#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001583 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001584#else
1585#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001586#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001587 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001588}
1589
Bernhard Walle201bde32008-01-21 15:24:22 +00001590void print_version(void)
1591{
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001592 msg_ginfo("flashrom v%s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001593 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001594 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00001595}
1596
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001597void print_banner(void)
1598{
1599 msg_ginfo("flashrom is free software, get the source code at "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001600 "http://www.flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001601 msg_ginfo("\n");
1602}
1603
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001604int selfcheck(void)
1605{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001606 const struct flashchip *chip;
Stefan Taunera6d96482012-12-26 19:51:23 +00001607 int i;
1608 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001609
1610 /* Safety check. Instead of aborting after the first error, check
1611 * if more errors exist.
1612 */
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001613 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001614 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001615 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001616 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001617 for (i = 0; i < PROGRAMMER_INVALID; i++) {
1618 const struct programmer_entry p = programmer_table[i];
1619 if (p.name == NULL) {
1620 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1621 ret = 1;
1622 /* This might hide other problems with this programmer, but allows for better error
1623 * messages below without jumping through hoops. */
1624 continue;
1625 }
Stefan Tauneraf358d62012-12-27 18:40:26 +00001626 switch (p.type) {
1627 case USB:
1628 case PCI:
1629 case OTHER:
1630 if (p.devs.note == NULL) {
1631 if (strcmp("internal", p.name) == 0)
1632 break; /* This one has its device list stored separately. */
1633 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
1634 p.name);
1635 ret = 1;
1636 }
1637 break;
1638 default:
1639 msg_gerr("Programmer %s does not have a valid type set!\n", p.name);
1640 ret = 1;
1641 break;
1642 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001643 if (p.init == NULL) {
1644 msg_gerr("Programmer %s does not have a valid init function!\n", p.name);
1645 ret = 1;
1646 }
1647 if (p.delay == NULL) {
1648 msg_gerr("Programmer %s does not have a valid delay function!\n", p.name);
1649 ret = 1;
1650 }
1651 if (p.map_flash_region == NULL) {
1652 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p.name);
1653 ret = 1;
1654 }
1655 if (p.unmap_flash_region == NULL) {
1656 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p.name);
1657 ret = 1;
1658 }
1659 }
Stefan Tauner93f70232011-07-26 14:33:46 +00001660 /* It would be favorable if we could also check for correct termination
Stefan Tauner716e0982011-07-25 20:38:52 +00001661 * of the following arrays, but we don't know their sizes in here...
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001662 * For 'flashchips' we check the first element to be non-null. In the
1663 * other cases there exist use cases where the first element can be
1664 * null. */
1665 if (flashchips == NULL || flashchips[0].vendor == NULL) {
1666 msg_gerr("Flashchips table miscompilation!\n");
1667 ret = 1;
1668 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001669 for (chip = flashchips; chip && chip->name; chip++)
1670 if (selfcheck_eraseblocks(chip))
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001671 ret = 1;
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001672
1673#if CONFIG_INTERNAL == 1
1674 if (chipset_enables == NULL) {
1675 msg_gerr("Chipset enables table does not exist!\n");
1676 ret = 1;
1677 }
Carl-Daniel Hailfinger97d5b122011-08-31 16:19:50 +00001678 if (board_matches == NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001679 msg_gerr("Board enables table does not exist!\n");
1680 ret = 1;
1681 }
1682 if (boards_known == NULL) {
1683 msg_gerr("Known boards table does not exist!\n");
1684 ret = 1;
1685 }
1686 if (laptops_known == NULL) {
1687 msg_gerr("Known laptops table does not exist!\n");
1688 ret = 1;
1689 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001690#endif
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001691 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001692}
1693
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001694void check_chip_supported(const struct flashchip *chip)
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001695{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001696 if (chip->feature_bits & FEATURE_OTP) {
Daniel Lenski65922a32012-02-15 23:40:23 +00001697 msg_cdbg("This chip may contain one-time programmable memory. "
1698 "flashrom cannot read\nand may never be able to write "
1699 "it, hence it may not be able to completely\n"
1700 "clone the contents of this chip (see man page for "
1701 "details).\n");
1702 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001703 if (TEST_OK_MASK != (chip->tested & TEST_OK_MASK)) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001704 msg_cinfo("===\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001705 if (chip->tested & TEST_BAD_MASK) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001706 msg_cinfo("This flash part has status NOT WORKING for operations:");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001707 if (chip->tested & TEST_BAD_PROBE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001708 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001709 if (chip->tested & TEST_BAD_READ)
Sean Nelson316a29f2010-05-07 20:09:04 +00001710 msg_cinfo(" READ");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001711 if (chip->tested & TEST_BAD_ERASE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001712 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001713 if (chip->tested & TEST_BAD_WRITE)
Sean Nelson316a29f2010-05-07 20:09:04 +00001714 msg_cinfo(" WRITE");
1715 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001716 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001717 if ((!(chip->tested & TEST_BAD_PROBE) && !(chip->tested & TEST_OK_PROBE)) ||
1718 (!(chip->tested & TEST_BAD_READ) && !(chip->tested & TEST_OK_READ)) ||
1719 (!(chip->tested & TEST_BAD_ERASE) && !(chip->tested & TEST_OK_ERASE)) ||
1720 (!(chip->tested & TEST_BAD_WRITE) && !(chip->tested & TEST_OK_WRITE))) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001721 msg_cinfo("This flash part has status UNTESTED for operations:");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001722 if (!(chip->tested & TEST_BAD_PROBE) && !(chip->tested & TEST_OK_PROBE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001723 msg_cinfo(" PROBE");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001724 if (!(chip->tested & TEST_BAD_READ) && !(chip->tested & TEST_OK_READ))
Sean Nelson316a29f2010-05-07 20:09:04 +00001725 msg_cinfo(" READ");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001726 if (!(chip->tested & TEST_BAD_ERASE) && !(chip->tested & TEST_OK_ERASE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001727 msg_cinfo(" ERASE");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001728 if (!(chip->tested & TEST_BAD_WRITE) && !(chip->tested & TEST_OK_WRITE))
Sean Nelson316a29f2010-05-07 20:09:04 +00001729 msg_cinfo(" WRITE");
1730 msg_cinfo("\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001731 }
Carl-Daniel Hailfingera84835a2010-01-07 03:24:05 +00001732 /* FIXME: This message is designed towards CLI users. */
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001733 msg_cinfo("The test status of this chip may have been updated "
1734 "in the latest development\n"
1735 "version of flashrom. If you are running the latest "
1736 "development version,\n"
1737 "please email a report to flashrom@flashrom.org if "
1738 "any of the above operations\n"
1739 "work correctly for you with this flash part. Please "
1740 "include the flashrom\n"
1741 "output with the additional -V option for all "
1742 "operations you tested (-V, -Vr,\n"
Paul Menzele3800132012-01-12 13:58:43 +00001743 "-VE, -Vw), and mention which mainboard or "
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001744 "programmer you tested.\n"
Paul Menzelab6328f2010-10-08 11:03:02 +00001745 "Please mention your board in the subject line. "
1746 "Thanks for your help!\n");
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001747 }
1748}
1749
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001750/* FIXME: This function signature needs to be improved once doit() has a better
1751 * function signature.
1752 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001753int chip_safety_check(const struct flashctx *flash, int force, int read_it, int write_it, int erase_it,
1754 int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001755{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001756 const struct flashchip *chip = flash->chip;
1757
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001758 if (!programmer_may_write && (write_it || erase_it)) {
1759 msg_perr("Write/erase is not working yet on your programmer in "
1760 "its current configuration.\n");
1761 /* --force is the wrong approach, but it's the best we can do
1762 * until the generic programmer parameter parser is merged.
1763 */
1764 if (!force)
1765 return 1;
1766 msg_cerr("Continuing anyway.\n");
1767 }
1768
1769 if (read_it || erase_it || write_it || verify_it) {
1770 /* Everything needs read. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001771 if (chip->tested & TEST_BAD_READ) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001772 msg_cerr("Read is not working on this chip. ");
1773 if (!force)
1774 return 1;
1775 msg_cerr("Continuing anyway.\n");
1776 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001777 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001778 msg_cerr("flashrom has no read function for this "
1779 "flash chip.\n");
1780 return 1;
1781 }
1782 }
1783 if (erase_it || write_it) {
1784 /* Write needs erase. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001785 if (chip->tested & TEST_BAD_ERASE) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001786 msg_cerr("Erase is not working on this chip. ");
1787 if (!force)
1788 return 1;
1789 msg_cerr("Continuing anyway.\n");
1790 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001791 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001792 msg_cerr("flashrom has no erase function for this "
1793 "flash chip.\n");
1794 return 1;
1795 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001796 }
1797 if (write_it) {
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001798 if (chip->tested & TEST_BAD_WRITE) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001799 msg_cerr("Write is not working on this chip. ");
1800 if (!force)
1801 return 1;
1802 msg_cerr("Continuing anyway.\n");
1803 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001804 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001805 msg_cerr("flashrom has no write function for this "
1806 "flash chip.\n");
1807 return 1;
1808 }
1809 }
1810 return 0;
1811}
1812
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001813/* This function signature is horrible. We need to design a better interface,
1814 * but right now it allows us to split off the CLI code.
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001815 * Besides that, the function itself is a textbook example of abysmal code flow.
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001816 */
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +00001817int doit(struct flashctx *flash, int force, const char *filename, int read_it,
1818 int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001819{
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001820 uint8_t *oldcontents;
1821 uint8_t *newcontents;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001822 int ret = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001823 unsigned long size = flash->chip->total_size * 1024;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001824
David Hendricks77f931a2011-05-18 01:30:56 +00001825 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001826 msg_cerr("Aborting.\n");
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001827 ret = 1;
1828 goto out_nofree;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001829 }
1830
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001831 /* Given the existence of read locks, we want to unlock for read,
1832 * erase and write.
1833 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001834 if (flash->chip->unlock)
1835 flash->chip->unlock(flash);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001836
1837 if (read_it) {
1838 ret = read_flash_to_file(flash, filename);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001839 goto out_nofree;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +00001840 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001841
Stefan Tauner269de352011-07-12 22:35:21 +00001842 oldcontents = malloc(size);
1843 if (!oldcontents) {
1844 msg_gerr("Out of memory!\n");
1845 exit(1);
1846 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001847 /* Assume worst case: All bits are 0. */
1848 memset(oldcontents, 0x00, size);
Stefan Tauner269de352011-07-12 22:35:21 +00001849 newcontents = malloc(size);
1850 if (!newcontents) {
1851 msg_gerr("Out of memory!\n");
1852 exit(1);
1853 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001854 /* Assume best case: All bits should be 1. */
1855 memset(newcontents, 0xff, size);
1856 /* Side effect of the assumptions above: Default write action is erase
1857 * because newcontents looks like a completely erased chip, and
1858 * oldcontents being completely 0x00 means we have to erase everything
1859 * before we can write.
1860 */
1861
Ollie Lhoefa28582004-12-08 20:10:01 +00001862 if (erase_it) {
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001863 /* FIXME: Do we really want the scary warning if erase failed?
1864 * After all, after erase the chip is either blank or partially
1865 * blank or it has the old contents. A blank chip won't boot,
1866 * so if the user wanted erase and reboots afterwards, the user
1867 * knows very well that booting won't work.
1868 */
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001869 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001870 emergency_help_message();
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001871 ret = 1;
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001872 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001873 goto out;
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001874 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001875
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001876 if (write_it || verify_it) {
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001877 if (read_buf_from_file(newcontents, size, filename)) {
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001878 ret = 1;
1879 goto out;
Stefan Reinauer018aca82006-11-21 23:48:51 +00001880 }
1881
Carl-Daniel Hailfinger71127722010-05-31 15:27:27 +00001882#if CONFIG_INTERNAL == 1
Stefan Taunerb4e06bd2012-08-20 00:24:22 +00001883 if (programmer == PROGRAMMER_INTERNAL && cb_check_image(newcontents, size) < 0) {
1884 if (force_boardmismatch) {
1885 msg_pinfo("Proceeding anyway because user forced us to.\n");
1886 } else {
1887 msg_perr("Aborting. You can override this with "
1888 "-p internal:boardmismatch=force.\n");
Niklas Söderlund9e423762012-06-16 00:11:16 +00001889 ret = 1;
1890 goto out;
1891 }
Stefan Taunerb4e06bd2012-08-20 00:24:22 +00001892 }
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +00001893#endif
Ollie Lhocbbf1252004-03-17 22:22:08 +00001894 }
1895
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001896 /* Read the whole chip to be able to check whether regions need to be
1897 * erased and to give better diagnostics in case write fails.
1898 * The alternative would be to read only the regions which are to be
1899 * preserved, but in that case we might perform unneeded erase which
1900 * takes time as well.
1901 */
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001902 msg_cinfo("Reading old flash chip contents... ");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001903 if (flash->chip->read(flash, oldcontents, 0, size)) {
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001904 ret = 1;
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001905 msg_cinfo("FAILED.\n");
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001906 goto out;
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001907 }
Carl-Daniel Hailfinger84d19682011-07-26 14:28:35 +00001908 msg_cinfo("done.\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001909
Ollie Lho184a4042005-11-26 21:55:36 +00001910 // This should be moved into each flash part's code to do it
1911 // cleanly. This does the job.
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001912 handle_romentries(flash, oldcontents, newcontents);
Uwe Hermanna7e05482007-05-09 10:17:44 +00001913
Ollie Lho184a4042005-11-26 21:55:36 +00001914 // ////////////////////////////////////////////////////////////
Uwe Hermanna7e05482007-05-09 10:17:44 +00001915
Peter Stugef31104c2008-04-28 14:47:30 +00001916 if (write_it) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +00001917 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1918 msg_cerr("Uh oh. Erase/write failed. Checking if "
1919 "anything changed.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001920 if (!flash->chip->read(flash, newcontents, 0, size)) {
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001921 if (!memcmp(oldcontents, newcontents, size)) {
1922 msg_cinfo("Good. It seems nothing was "
1923 "changed.\n");
1924 nonfatal_help_message();
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001925 ret = 1;
1926 goto out;
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001927 }
1928 }
Carl-Daniel Hailfingerf52f7842010-10-08 18:52:29 +00001929 emergency_help_message();
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001930 ret = 1;
1931 goto out;
Peter Stugef31104c2008-04-28 14:47:30 +00001932 }
Peter Stugef31104c2008-04-28 14:47:30 +00001933 }
Ollie Lho184a4042005-11-26 21:55:36 +00001934
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001935 if (verify_it) {
Stefan Tauner78ffbea2012-10-27 15:36:56 +00001936 msg_cinfo("Verifying flash... ");
1937
1938 if (write_it) {
1939 /* Work around chips which need some time to calm down. */
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001940 programmer_delay(1000*1000);
Stefan Tauner78ffbea2012-10-27 15:36:56 +00001941 ret = verify_range(flash, newcontents, 0, size);
1942 /* If we tried to write, and verification now fails, we
1943 * might have an emergency situation.
1944 */
1945 if (ret)
1946 emergency_help_message();
1947 } else {
1948 ret = compare_range(newcontents, oldcontents, 0, size);
1949 }
1950 if (!ret)
1951 msg_cinfo("VERIFIED.\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001952 }
Ollie Lho184a4042005-11-26 21:55:36 +00001953
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +00001954out:
1955 free(oldcontents);
1956 free(newcontents);
1957out_nofree:
Carl-Daniel Hailfinger702218d2009-05-08 17:43:22 +00001958 programmer_shutdown();
Stefan Reinauer143da0b2006-01-04 16:42:57 +00001959 return ret;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001960}