blob: f18828ca011f284a7c0e920d409176e01dea558e [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
uwe4475e902009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
hailfinger23060112009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-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.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-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.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
uweb25f1ea2007-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
rminnich8d3ff912003-10-25 17:01:29 +000022 */
23
hailfingera83a5fe2010-05-30 22:24:40 +000024#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000026#ifndef __LIBPAYLOAD__
27#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000028#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000029#endif
rminnich8d3ff912003-10-25 17:01:29 +000030#include <string.h>
31#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000032#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000033#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
rminnich8d3ff912003-10-25 17:01:29 +000037#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000038#include "flashchips.h"
hailfinger428f6852010-07-27 22:41:39 +000039#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000040
krause2eb76212011-01-17 07:50:42 +000041const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000042char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000043int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000044
hailfinger90c7d542010-05-31 15:27:27 +000045#if CONFIG_INTERNAL == 1
hailfinger6fe23d62009-08-12 11:39:29 +000046enum programmer programmer = PROGRAMMER_INTERNAL;
hailfinger90c7d542010-05-31 15:27:27 +000047#elif CONFIG_DUMMY == 1
hailfinger80422e22009-12-13 22:28:00 +000048enum programmer programmer = PROGRAMMER_DUMMY;
49#else
hailfingerf95c8f62010-01-10 13:28:48 +000050/* If neither internal nor dummy are selected, we must pick a sensible default.
51 * Since there is no reason to prefer a particular external programmer, we fail
52 * if more than one of them is selected. If only one is selected, it is clear
53 * that the user wants that one to become the default.
54 */
hailfinger7949b652011-05-08 00:24:18 +000055#if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_FT2232_SPI+CONFIG_SERPROG+CONFIG_BUSPIRATE_SPI+CONFIG_DEDIPROG+CONFIG_RAYER_SPI+CONFIG_NICINTEL+CONFIG_NICINTEL_SPI+CONFIG_OGP_SPI+CONFIG_SATAMV > 1
hailfinger90c7d542010-05-31 15:27:27 +000056#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all programmers except one.
hailfingerf95c8f62010-01-10 13:28:48 +000057#endif
58enum programmer programmer =
hailfinger90c7d542010-05-31 15:27:27 +000059#if CONFIG_NIC3COM == 1
hailfingerf95c8f62010-01-10 13:28:48 +000060 PROGRAMMER_NIC3COM
61#endif
hailfinger90c7d542010-05-31 15:27:27 +000062#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +000063 PROGRAMMER_NICREALTEK
hailfinger5aa36982010-05-21 21:54:07 +000064#endif
hailfingerf0a368f2010-06-07 22:37:54 +000065#if CONFIG_NICNATSEMI == 1
66 PROGRAMMER_NICNATSEMI
67#endif
hailfinger90c7d542010-05-31 15:27:27 +000068#if CONFIG_GFXNVIDIA == 1
hailfingerf95c8f62010-01-10 13:28:48 +000069 PROGRAMMER_GFXNVIDIA
70#endif
hailfinger90c7d542010-05-31 15:27:27 +000071#if CONFIG_DRKAISER == 1
hailfingerf95c8f62010-01-10 13:28:48 +000072 PROGRAMMER_DRKAISER
73#endif
hailfinger90c7d542010-05-31 15:27:27 +000074#if CONFIG_SATASII == 1
hailfingerf95c8f62010-01-10 13:28:48 +000075 PROGRAMMER_SATASII
76#endif
hailfinger90c7d542010-05-31 15:27:27 +000077#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +000078 PROGRAMMER_ATAHPT
79#endif
hailfinger90c7d542010-05-31 15:27:27 +000080#if CONFIG_FT2232_SPI == 1
81 PROGRAMMER_FT2232_SPI
hailfingerf95c8f62010-01-10 13:28:48 +000082#endif
hailfinger90c7d542010-05-31 15:27:27 +000083#if CONFIG_SERPROG == 1
hailfingerf95c8f62010-01-10 13:28:48 +000084 PROGRAMMER_SERPROG
85#endif
hailfinger90c7d542010-05-31 15:27:27 +000086#if CONFIG_BUSPIRATE_SPI == 1
87 PROGRAMMER_BUSPIRATE_SPI
hailfingerf95c8f62010-01-10 13:28:48 +000088#endif
hailfinger90c7d542010-05-31 15:27:27 +000089#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +000090 PROGRAMMER_DEDIPROG
91#endif
hailfinger52c4fa02010-07-21 10:26:01 +000092#if CONFIG_RAYER_SPI == 1
93 PROGRAMMER_RAYER_SPI
94#endif
hailfinger7949b652011-05-08 00:24:18 +000095#if CONFIG_NICINTEL == 1
96 PROGRAMMER_NICINTEL
97#endif
uwe6764e922010-09-03 18:21:21 +000098#if CONFIG_NICINTEL_SPI == 1
99 PROGRAMMER_NICINTEL_SPI
100#endif
hailfingerfb1f31f2010-12-03 14:48:11 +0000101#if CONFIG_OGP_SPI == 1
102 PROGRAMMER_OGP_SPI
103#endif
hailfinger935365d2011-02-04 21:37:59 +0000104#if CONFIG_SATAMV == 1
105 PROGRAMMER_SATAMV
106#endif
hailfingerf95c8f62010-01-10 13:28:48 +0000107;
hailfinger80422e22009-12-13 22:28:00 +0000108#endif
109
hailfingerddeb4ac2010-07-08 10:13:37 +0000110static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +0000111
hailfinger1ff33dc2010-07-03 11:02:10 +0000112/* Supported buses for the current programmer. */
113enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +0000114
uwee15beb92010-08-08 17:01:18 +0000115/*
hailfinger80422e22009-12-13 22:28:00 +0000116 * Programmers supporting multiple buses can have differing size limits on
117 * each bus. Store the limits for each bus in a common struct.
118 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000119struct decode_sizes max_rom_decode;
120
121/* If nonzero, used as the start address of bottom-aligned flash. */
122unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +0000123
hailfinger5828baf2010-07-03 12:14:25 +0000124/* Is writing allowed with this programmer? */
125int programmer_may_write;
126
hailfingerabe249e2009-05-08 17:43:22 +0000127const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +0000128#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +0000129 {
hailfinger3548a9a2009-08-12 14:34:35 +0000130 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +0000131 .init = internal_init,
132 .shutdown = internal_shutdown,
hailfinger11ae3c42009-05-11 14:13:25 +0000133 .map_flash_region = physmap,
134 .unmap_flash_region = physunmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000135 .chip_readb = internal_chip_readb,
136 .chip_readw = internal_chip_readw,
137 .chip_readl = internal_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000138 .chip_readn = internal_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000139 .chip_writeb = internal_chip_writeb,
140 .chip_writew = internal_chip_writew,
141 .chip_writel = internal_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000142 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000143 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +0000144 },
hailfinger80422e22009-12-13 22:28:00 +0000145#endif
stepan927d4e22007-04-04 22:45:58 +0000146
hailfinger90c7d542010-05-31 15:27:27 +0000147#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000148 {
hailfinger3548a9a2009-08-12 14:34:35 +0000149 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000150 .init = dummy_init,
151 .shutdown = dummy_shutdown,
hailfinger11ae3c42009-05-11 14:13:25 +0000152 .map_flash_region = dummy_map,
153 .unmap_flash_region = dummy_unmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000154 .chip_readb = dummy_chip_readb,
155 .chip_readw = dummy_chip_readw,
156 .chip_readl = dummy_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000157 .chip_readn = dummy_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000158 .chip_writeb = dummy_chip_writeb,
159 .chip_writew = dummy_chip_writew,
160 .chip_writel = dummy_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000161 .chip_writen = dummy_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000162 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000163 },
hailfinger571a6b32009-09-16 10:09:21 +0000164#endif
hailfingera9df33c2009-05-09 00:54:55 +0000165
hailfinger90c7d542010-05-31 15:27:27 +0000166#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000167 {
hailfinger3548a9a2009-08-12 14:34:35 +0000168 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000169 .init = nic3com_init,
170 .shutdown = nic3com_shutdown,
uwe3e656bd2009-05-17 23:12:17 +0000171 .map_flash_region = fallback_map,
172 .unmap_flash_region = fallback_unmap,
uwe0f5a3a22009-05-13 11:36:06 +0000173 .chip_readb = nic3com_chip_readb,
hailfinger43716942009-05-16 01:23:55 +0000174 .chip_readw = fallback_chip_readw,
175 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000176 .chip_readn = fallback_chip_readn,
uwe0f5a3a22009-05-13 11:36:06 +0000177 .chip_writeb = nic3com_chip_writeb,
hailfinger43716942009-05-16 01:23:55 +0000178 .chip_writew = fallback_chip_writew,
179 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000180 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000181 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000182 },
hailfinger571a6b32009-09-16 10:09:21 +0000183#endif
uwe0f5a3a22009-05-13 11:36:06 +0000184
hailfinger90c7d542010-05-31 15:27:27 +0000185#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000186 {
hailfinger0d703d42011-03-07 01:08:09 +0000187 /* This programmer works for Realtek RTL8139 and SMC 1211. */
hailfinger5aa36982010-05-21 21:54:07 +0000188 .name = "nicrealtek",
hailfinger0d703d42011-03-07 01:08:09 +0000189 //.name = "nicsmc1211",
hailfinger5aa36982010-05-21 21:54:07 +0000190 .init = nicrealtek_init,
191 .shutdown = nicrealtek_shutdown,
192 .map_flash_region = fallback_map,
193 .unmap_flash_region = fallback_unmap,
194 .chip_readb = nicrealtek_chip_readb,
195 .chip_readw = fallback_chip_readw,
196 .chip_readl = fallback_chip_readl,
197 .chip_readn = fallback_chip_readn,
198 .chip_writeb = nicrealtek_chip_writeb,
199 .chip_writew = fallback_chip_writew,
200 .chip_writel = fallback_chip_writel,
201 .chip_writen = fallback_chip_writen,
202 .delay = internal_delay,
203 },
hailfinger5aa36982010-05-21 21:54:07 +0000204#endif
205
hailfingerf0a368f2010-06-07 22:37:54 +0000206#if CONFIG_NICNATSEMI == 1
207 {
208 .name = "nicnatsemi",
209 .init = nicnatsemi_init,
210 .shutdown = nicnatsemi_shutdown,
211 .map_flash_region = fallback_map,
212 .unmap_flash_region = fallback_unmap,
213 .chip_readb = nicnatsemi_chip_readb,
214 .chip_readw = fallback_chip_readw,
215 .chip_readl = fallback_chip_readl,
216 .chip_readn = fallback_chip_readn,
217 .chip_writeb = nicnatsemi_chip_writeb,
218 .chip_writew = fallback_chip_writew,
219 .chip_writel = fallback_chip_writel,
220 .chip_writen = fallback_chip_writen,
221 .delay = internal_delay,
222 },
223#endif
hailfinger5aa36982010-05-21 21:54:07 +0000224
hailfinger90c7d542010-05-31 15:27:27 +0000225#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000226 {
227 .name = "gfxnvidia",
228 .init = gfxnvidia_init,
229 .shutdown = gfxnvidia_shutdown,
230 .map_flash_region = fallback_map,
231 .unmap_flash_region = fallback_unmap,
232 .chip_readb = gfxnvidia_chip_readb,
233 .chip_readw = fallback_chip_readw,
234 .chip_readl = fallback_chip_readl,
235 .chip_readn = fallback_chip_readn,
236 .chip_writeb = gfxnvidia_chip_writeb,
237 .chip_writew = fallback_chip_writew,
238 .chip_writel = fallback_chip_writel,
239 .chip_writen = fallback_chip_writen,
240 .delay = internal_delay,
241 },
242#endif
243
hailfinger90c7d542010-05-31 15:27:27 +0000244#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000245 {
uwee2f95ef2009-09-02 23:00:46 +0000246 .name = "drkaiser",
247 .init = drkaiser_init,
248 .shutdown = drkaiser_shutdown,
249 .map_flash_region = fallback_map,
250 .unmap_flash_region = fallback_unmap,
251 .chip_readb = drkaiser_chip_readb,
252 .chip_readw = fallback_chip_readw,
253 .chip_readl = fallback_chip_readl,
254 .chip_readn = fallback_chip_readn,
255 .chip_writeb = drkaiser_chip_writeb,
256 .chip_writew = fallback_chip_writew,
257 .chip_writel = fallback_chip_writel,
258 .chip_writen = fallback_chip_writen,
259 .delay = internal_delay,
260 },
hailfinger571a6b32009-09-16 10:09:21 +0000261#endif
uwee2f95ef2009-09-02 23:00:46 +0000262
hailfinger90c7d542010-05-31 15:27:27 +0000263#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000264 {
hailfinger3548a9a2009-08-12 14:34:35 +0000265 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000266 .init = satasii_init,
267 .shutdown = satasii_shutdown,
uwe3e656bd2009-05-17 23:12:17 +0000268 .map_flash_region = fallback_map,
269 .unmap_flash_region = fallback_unmap,
ruikda922a12009-05-17 19:39:27 +0000270 .chip_readb = satasii_chip_readb,
271 .chip_readw = fallback_chip_readw,
272 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000273 .chip_readn = fallback_chip_readn,
ruikda922a12009-05-17 19:39:27 +0000274 .chip_writeb = satasii_chip_writeb,
275 .chip_writew = fallback_chip_writew,
276 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000277 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000278 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000279 },
hailfinger571a6b32009-09-16 10:09:21 +0000280#endif
ruikda922a12009-05-17 19:39:27 +0000281
hailfinger90c7d542010-05-31 15:27:27 +0000282#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000283 {
284 .name = "atahpt",
285 .init = atahpt_init,
286 .shutdown = atahpt_shutdown,
287 .map_flash_region = fallback_map,
288 .unmap_flash_region = fallback_unmap,
289 .chip_readb = atahpt_chip_readb,
290 .chip_readw = fallback_chip_readw,
291 .chip_readl = fallback_chip_readl,
292 .chip_readn = fallback_chip_readn,
293 .chip_writeb = atahpt_chip_writeb,
294 .chip_writew = fallback_chip_writew,
295 .chip_writel = fallback_chip_writel,
296 .chip_writen = fallback_chip_writen,
297 .delay = internal_delay,
298 },
299#endif
300
hailfinger90c7d542010-05-31 15:27:27 +0000301#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000302 {
hailfinger90c7d542010-05-31 15:27:27 +0000303 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000304 .init = ft2232_spi_init,
hailfinger571a6b32009-09-16 10:09:21 +0000305 .shutdown = noop_shutdown, /* Missing shutdown */
hailfinger6fe23d62009-08-12 11:39:29 +0000306 .map_flash_region = fallback_map,
307 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000308 .chip_readb = noop_chip_readb,
hailfingerf31da3d2009-06-16 21:08:06 +0000309 .chip_readw = fallback_chip_readw,
310 .chip_readl = fallback_chip_readl,
311 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000312 .chip_writeb = noop_chip_writeb,
hailfingerf31da3d2009-06-16 21:08:06 +0000313 .chip_writew = fallback_chip_writew,
314 .chip_writel = fallback_chip_writel,
315 .chip_writen = fallback_chip_writen,
316 .delay = internal_delay,
317 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000318#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000319
hailfinger90c7d542010-05-31 15:27:27 +0000320#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000321 {
hailfinger3548a9a2009-08-12 14:34:35 +0000322 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000323 .init = serprog_init,
324 .shutdown = serprog_shutdown,
325 .map_flash_region = fallback_map,
326 .unmap_flash_region = fallback_unmap,
327 .chip_readb = serprog_chip_readb,
328 .chip_readw = fallback_chip_readw,
329 .chip_readl = fallback_chip_readl,
330 .chip_readn = serprog_chip_readn,
331 .chip_writeb = serprog_chip_writeb,
332 .chip_writew = fallback_chip_writew,
333 .chip_writel = fallback_chip_writel,
334 .chip_writen = fallback_chip_writen,
335 .delay = serprog_delay,
336 },
hailfinger74d88a72009-08-12 16:17:41 +0000337#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000338
hailfinger90c7d542010-05-31 15:27:27 +0000339#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000340 {
hailfinger90c7d542010-05-31 15:27:27 +0000341 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000342 .init = buspirate_spi_init,
343 .shutdown = buspirate_spi_shutdown,
344 .map_flash_region = fallback_map,
345 .unmap_flash_region = fallback_unmap,
346 .chip_readb = noop_chip_readb,
347 .chip_readw = fallback_chip_readw,
348 .chip_readl = fallback_chip_readl,
349 .chip_readn = fallback_chip_readn,
350 .chip_writeb = noop_chip_writeb,
351 .chip_writew = fallback_chip_writew,
352 .chip_writel = fallback_chip_writel,
353 .chip_writen = fallback_chip_writen,
354 .delay = internal_delay,
355 },
356#endif
357
hailfinger90c7d542010-05-31 15:27:27 +0000358#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000359 {
360 .name = "dediprog",
361 .init = dediprog_init,
362 .shutdown = dediprog_shutdown,
363 .map_flash_region = fallback_map,
364 .unmap_flash_region = fallback_unmap,
365 .chip_readb = noop_chip_readb,
366 .chip_readw = fallback_chip_readw,
367 .chip_readl = fallback_chip_readl,
368 .chip_readn = fallback_chip_readn,
369 .chip_writeb = noop_chip_writeb,
370 .chip_writew = fallback_chip_writew,
371 .chip_writel = fallback_chip_writel,
372 .chip_writen = fallback_chip_writen,
373 .delay = internal_delay,
374 },
375#endif
376
hailfinger52c4fa02010-07-21 10:26:01 +0000377#if CONFIG_RAYER_SPI == 1
378 {
379 .name = "rayer_spi",
380 .init = rayer_spi_init,
381 .shutdown = noop_shutdown,
382 .map_flash_region = fallback_map,
383 .unmap_flash_region = fallback_unmap,
384 .chip_readb = noop_chip_readb,
385 .chip_readw = fallback_chip_readw,
386 .chip_readl = fallback_chip_readl,
387 .chip_readn = fallback_chip_readn,
388 .chip_writeb = noop_chip_writeb,
389 .chip_writew = fallback_chip_writew,
390 .chip_writel = fallback_chip_writel,
391 .chip_writen = fallback_chip_writen,
392 .delay = internal_delay,
393 },
394#endif
395
hailfinger7949b652011-05-08 00:24:18 +0000396#if CONFIG_NICINTEL == 1
397 {
398 .name = "nicintel",
399 .init = nicintel_init,
400 .shutdown = nicintel_shutdown,
401 .map_flash_region = fallback_map,
402 .unmap_flash_region = fallback_unmap,
403 .chip_readb = nicintel_chip_readb,
404 .chip_readw = fallback_chip_readw,
405 .chip_readl = fallback_chip_readl,
406 .chip_readn = fallback_chip_readn,
407 .chip_writeb = nicintel_chip_writeb,
408 .chip_writew = fallback_chip_writew,
409 .chip_writel = fallback_chip_writel,
410 .chip_writen = fallback_chip_writen,
411 .delay = internal_delay,
412 },
413#endif
414
uwe6764e922010-09-03 18:21:21 +0000415#if CONFIG_NICINTEL_SPI == 1
416 {
417 .name = "nicintel_spi",
418 .init = nicintel_spi_init,
419 .shutdown = nicintel_spi_shutdown,
420 .map_flash_region = fallback_map,
421 .unmap_flash_region = fallback_unmap,
422 .chip_readb = noop_chip_readb,
423 .chip_readw = fallback_chip_readw,
424 .chip_readl = fallback_chip_readl,
425 .chip_readn = fallback_chip_readn,
426 .chip_writeb = noop_chip_writeb,
427 .chip_writew = fallback_chip_writew,
428 .chip_writel = fallback_chip_writel,
429 .chip_writen = fallback_chip_writen,
430 .delay = internal_delay,
431 },
432#endif
433
hailfingerfb1f31f2010-12-03 14:48:11 +0000434#if CONFIG_OGP_SPI == 1
435 {
436 .name = "ogp_spi",
437 .init = ogp_spi_init,
438 .shutdown = ogp_spi_shutdown,
439 .map_flash_region = fallback_map,
440 .unmap_flash_region = fallback_unmap,
441 .chip_readb = noop_chip_readb,
442 .chip_readw = fallback_chip_readw,
443 .chip_readl = fallback_chip_readl,
444 .chip_readn = fallback_chip_readn,
445 .chip_writeb = noop_chip_writeb,
446 .chip_writew = fallback_chip_writew,
447 .chip_writel = fallback_chip_writel,
448 .chip_writen = fallback_chip_writen,
449 .delay = internal_delay,
450 },
451#endif
452
hailfinger935365d2011-02-04 21:37:59 +0000453#if CONFIG_SATAMV == 1
454 {
455 .name = "satamv",
456 .init = satamv_init,
457 .shutdown = satamv_shutdown,
458 .map_flash_region = fallback_map,
459 .unmap_flash_region = fallback_unmap,
460 .chip_readb = satamv_chip_readb,
461 .chip_readw = fallback_chip_readw,
462 .chip_readl = fallback_chip_readl,
463 .chip_readn = fallback_chip_readn,
464 .chip_writeb = satamv_chip_writeb,
465 .chip_writew = fallback_chip_writew,
466 .chip_writel = fallback_chip_writel,
467 .chip_writen = fallback_chip_writen,
468 .delay = internal_delay,
469 },
470#endif
471
hailfinger3548a9a2009-08-12 14:34:35 +0000472 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000473};
stepan927d4e22007-04-04 22:45:58 +0000474
hailfingerf31cbdc2010-11-10 15:25:18 +0000475#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000476static int shutdown_fn_count = 0;
477struct shutdown_func_data {
478 void (*func) (void *data);
479 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000480} static shutdown_fn[SHUTDOWN_MAXFN];
481/* Initialize to 0 to make sure nobody registers a shutdown function before
482 * programmer init.
483 */
484static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000485
486/* Register a function to be executed on programmer shutdown.
487 * The advantage over atexit() is that you can supply a void pointer which will
488 * be used as parameter to the registered function upon programmer shutdown.
489 * This pointer can point to arbitrary data used by said function, e.g. undo
490 * information for GPIO settings etc. If unneeded, set data=NULL.
491 * Please note that the first (void *data) belongs to the function signature of
492 * the function passed as first parameter.
493 */
494int register_shutdown(void (*function) (void *data), void *data)
495{
496 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000497 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000498 SHUTDOWN_MAXFN);
499 return 1;
500 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000501 if (!may_register_shutdown) {
502 msg_perr("Tried to register a shutdown function before "
503 "programmer init.\n");
504 return 1;
505 }
hailfingerdc6f7972010-02-14 01:20:28 +0000506 shutdown_fn[shutdown_fn_count].func = function;
507 shutdown_fn[shutdown_fn_count].data = data;
508 shutdown_fn_count++;
509
510 return 0;
511}
512
hailfinger1ff33dc2010-07-03 11:02:10 +0000513int programmer_init(char *param)
uweabe92a52009-05-16 22:36:00 +0000514{
hailfinger1ef766d2010-07-06 09:55:48 +0000515 int ret;
hailfinger1ff33dc2010-07-03 11:02:10 +0000516 /* Initialize all programmer specific data. */
517 /* Default to unlimited decode sizes. */
518 max_rom_decode = (const struct decode_sizes) {
519 .parallel = 0xffffffff,
520 .lpc = 0xffffffff,
521 .fwh = 0xffffffff,
522 .spi = 0xffffffff
523 };
524 /* Default to Parallel/LPC/FWH flash devices. If a known host controller
525 * is found, the init routine sets the buses_supported bitfield.
526 */
527 buses_supported = CHIP_BUSTYPE_NONSPI;
528 /* Default to top aligned flash at 4 GB. */
529 flashbase = 0;
530 /* Registering shutdown functions is now allowed. */
531 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000532 /* Default to allowing writes. Broken programmers set this to 0. */
533 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000534
535 programmer_param = param;
536 msg_pdbg("Initializing %s programmer\n",
537 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000538 ret = programmer_table[programmer].init();
539 if (programmer_param && strlen(programmer_param)) {
540 msg_perr("Unhandled programmer parameters: %s\n",
541 programmer_param);
542 /* Do not error out here, the init itself was successful. */
543 }
544 return ret;
uweabe92a52009-05-16 22:36:00 +0000545}
546
547int programmer_shutdown(void)
548{
hailfinger1ff33dc2010-07-03 11:02:10 +0000549 /* Registering shutdown functions is no longer allowed. */
550 may_register_shutdown = 0;
551 while (shutdown_fn_count > 0) {
552 int i = --shutdown_fn_count;
hailfingerdc6f7972010-02-14 01:20:28 +0000553 shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000554 }
uweabe92a52009-05-16 22:36:00 +0000555 return programmer_table[programmer].shutdown();
556}
557
558void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
559 size_t len)
560{
561 return programmer_table[programmer].map_flash_region(descr,
562 phys_addr, len);
563}
564
565void programmer_unmap_flash_region(void *virt_addr, size_t len)
566{
567 programmer_table[programmer].unmap_flash_region(virt_addr, len);
568}
569
570void chip_writeb(uint8_t val, chipaddr addr)
571{
572 programmer_table[programmer].chip_writeb(val, addr);
573}
574
575void chip_writew(uint16_t val, chipaddr addr)
576{
577 programmer_table[programmer].chip_writew(val, addr);
578}
579
580void chip_writel(uint32_t val, chipaddr addr)
581{
582 programmer_table[programmer].chip_writel(val, addr);
583}
584
hailfinger9d987ef2009-06-05 18:32:07 +0000585void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
586{
587 programmer_table[programmer].chip_writen(buf, addr, len);
588}
589
uweabe92a52009-05-16 22:36:00 +0000590uint8_t chip_readb(const chipaddr addr)
591{
592 return programmer_table[programmer].chip_readb(addr);
593}
594
595uint16_t chip_readw(const chipaddr addr)
596{
597 return programmer_table[programmer].chip_readw(addr);
598}
599
600uint32_t chip_readl(const chipaddr addr)
601{
602 return programmer_table[programmer].chip_readl(addr);
603}
604
hailfinger9d987ef2009-06-05 18:32:07 +0000605void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
606{
607 programmer_table[programmer].chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000608}
609
hailfingere5829f62009-06-05 17:48:08 +0000610void programmer_delay(int usecs)
611{
612 programmer_table[programmer].delay(usecs);
613}
614
stuge5ff0e6c2009-01-26 00:39:57 +0000615void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000616{
stepan15e64bc2007-05-24 08:48:10 +0000617 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000618 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000619 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfinger82719632009-05-16 21:22:56 +0000620 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000621}
622
hailfinger0f08b7a2009-06-16 08:55:44 +0000623int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger23060112009-05-08 12:49:03 +0000624{
hailfinger0f08b7a2009-06-16 08:55:44 +0000625 chip_readn(buf, flash->virtual_memory + start, len);
hailfinger23060112009-05-08 12:49:03 +0000626
627 return 0;
628}
629
hailfinger7b414742009-06-13 12:04:03 +0000630int min(int a, int b)
631{
632 return (a < b) ? a : b;
633}
634
hailfinger7af83692009-06-15 17:23:36 +0000635int max(int a, int b)
636{
637 return (a > b) ? a : b;
638}
639
hailfingeraec9c962009-10-31 01:53:09 +0000640int bitcount(unsigned long a)
641{
642 int i = 0;
643 for (; a != 0; a >>= 1)
644 if (a & 1)
645 i++;
646 return i;
647}
648
hailfingerf76cc322010-11-09 22:00:31 +0000649void tolower_string(char *str)
650{
651 for (; *str != '\0'; str++)
652 *str = (char)tolower((unsigned char)*str);
653}
654
hailfingera916b422009-06-01 02:08:58 +0000655char *strcat_realloc(char *dest, const char *src)
656{
657 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000658 if (!dest) {
659 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000660 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000661 }
hailfingera916b422009-06-01 02:08:58 +0000662 strcat(dest, src);
663 return dest;
664}
665
hailfinger6e5a52a2009-11-24 18:27:10 +0000666/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000667 * It will look for needle with a subsequent '=' in haystack, return a copy of
668 * needle and remove everything from the first occurrence of needle to the next
669 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000670 */
671char *extract_param(char **haystack, char *needle, char *delim)
672{
hailfinger1ef766d2010-07-06 09:55:48 +0000673 char *param_pos, *opt_pos, *rest;
674 char *opt = NULL;
675 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000676 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000677
hailfingerf4aaccc2010-04-28 15:22:14 +0000678 needlelen = strlen(needle);
679 if (!needlelen) {
680 msg_gerr("%s: empty needle! Please report a bug at "
681 "flashrom@flashrom.org\n", __func__);
682 return NULL;
683 }
684 /* No programmer parameters given. */
685 if (*haystack == NULL)
686 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000687 param_pos = strstr(*haystack, needle);
688 do {
689 if (!param_pos)
690 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000691 /* Needle followed by '='? */
692 if (param_pos[needlelen] == '=') {
693
694 /* Beginning of the string? */
695 if (param_pos == *haystack)
696 break;
697 /* After a delimiter? */
698 if (strchr(delim, *(param_pos - 1)))
699 break;
700 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000701 /* Continue searching. */
702 param_pos++;
703 param_pos = strstr(param_pos, needle);
704 } while (1);
hailfinger1ef766d2010-07-06 09:55:48 +0000705
hailfinger6e5a52a2009-11-24 18:27:10 +0000706 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000707 /* Get the string after needle and '='. */
708 opt_pos = param_pos + needlelen + 1;
709 optlen = strcspn(opt_pos, delim);
710 /* Return an empty string if the parameter was empty. */
711 opt = malloc(optlen + 1);
712 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000713 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000714 exit(1);
715 }
hailfinger1ef766d2010-07-06 09:55:48 +0000716 strncpy(opt, opt_pos, optlen);
717 opt[optlen] = '\0';
718 rest = opt_pos + optlen;
719 /* Skip all delimiters after the current parameter. */
720 rest += strspn(rest, delim);
721 memmove(param_pos, rest, strlen(rest) + 1);
722 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000723 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000724
hailfinger1ef766d2010-07-06 09:55:48 +0000725 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000726}
727
hailfingerddeb4ac2010-07-08 10:13:37 +0000728char *extract_programmer_param(char *param_name)
729{
730 return extract_param(&programmer_param, param_name, ",");
731}
732
hailfinger7af83692009-06-15 17:23:36 +0000733/* start is an offset to the base address of the flash chip */
734int check_erased_range(struct flashchip *flash, int start, int len)
735{
736 int ret;
737 uint8_t *cmpbuf = malloc(len);
738
739 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000740 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000741 exit(1);
742 }
743 memset(cmpbuf, 0xff, len);
744 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
745 free(cmpbuf);
746 return ret;
747}
748
uwee15beb92010-08-08 17:01:18 +0000749/*
hailfinger7af3d192009-11-25 17:05:52 +0000750 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
751 flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000752 * @start offset to the base address of the flash chip
753 * @len length of the verified area
754 * @message string to print in the "FAILED" message
755 * @return 0 for success, -1 for failure
756 */
757int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
758{
hailfinger8cb6ece2010-11-16 17:21:58 +0000759 int i, ret = 0;
760 uint8_t *readbuf = malloc(len);
hailfinger5be6c0f2009-07-23 01:42:56 +0000761 int failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000762
763 if (!len)
764 goto out_free;
765
hailfingerb0f4d122009-06-24 08:20:45 +0000766 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000767 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000768 return 1;
769 }
hailfinger7af83692009-06-15 17:23:36 +0000770 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000771 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000772 exit(1);
773 }
774
775 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000776 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000777 " total_size 0x%x\n", __func__, start, len,
778 flash->total_size * 1024);
779 ret = -1;
780 goto out_free;
781 }
782 if (!message)
783 message = "VERIFY";
784
hailfinger8cb6ece2010-11-16 17:21:58 +0000785 ret = flash->read(flash, readbuf, start, len);
786 if (ret) {
787 msg_gerr("Verification impossible because read failed "
788 "at 0x%x (len 0x%x)\n", start, len);
789 return ret;
790 }
791
792 for (i = 0; i < len; i++) {
793 if (cmpbuf[i] != readbuf[i]) {
794 /* Only print the first failure. */
795 if (!failcount++)
796 msg_cerr("%s FAILED at 0x%08x! "
797 "Expected=0x%02x, Read=0x%02x,",
798 message, start + i, cmpbuf[i],
799 readbuf[i]);
hailfinger7af83692009-06-15 17:23:36 +0000800 }
801 }
hailfinger5be6c0f2009-07-23 01:42:56 +0000802 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000803 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
hailfinger5be6c0f2009-07-23 01:42:56 +0000804 start, start + len - 1, failcount);
805 ret = -1;
806 }
hailfinger7af83692009-06-15 17:23:36 +0000807
808out_free:
809 free(readbuf);
810 return ret;
811}
812
uwee15beb92010-08-08 17:01:18 +0000813/*
hailfingerb247c7a2010-03-08 00:42:32 +0000814 * Check if the buffer @have can be programmed to the content of @want without
815 * erasing. This is only possible if all chunks of size @gran are either kept
816 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000817 *
hailfingerb247c7a2010-03-08 00:42:32 +0000818 * The following write granularities (enum @gran) are known:
819 * - 1 bit. Each bit can be cleared individually.
820 * - 1 byte. A byte can be written once. Further writes to an already written
821 * byte cause the contents to be either undefined or to stay unchanged.
822 * - 128 bytes. If less than 128 bytes are written, the rest will be
823 * erased. Each write to a 128-byte region will trigger an automatic erase
824 * before anything is written. Very uncommon behaviour and unsupported by
825 * this function.
826 * - 256 bytes. If less than 256 bytes are written, the contents of the
827 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000828 * Warning: This function assumes that @have and @want point to naturally
829 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000830 *
831 * @have buffer with current content
832 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000833 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000834 * @gran write granularity (enum, not count)
835 * @return 0 if no erase is needed, 1 otherwise
836 */
837int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
838{
839 int result = 0;
840 int i, j, limit;
841
842 switch (gran) {
843 case write_gran_1bit:
844 for (i = 0; i < len; i++)
845 if ((have[i] & want[i]) != want[i]) {
846 result = 1;
847 break;
848 }
849 break;
850 case write_gran_1byte:
851 for (i = 0; i < len; i++)
852 if ((have[i] != want[i]) && (have[i] != 0xff)) {
853 result = 1;
854 break;
855 }
856 break;
857 case write_gran_256bytes:
858 for (j = 0; j < len / 256; j++) {
859 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000860 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000861 if (!memcmp(have + j * 256, want + j * 256, limit))
862 continue;
863 /* have needs to be in erased state. */
864 for (i = 0; i < limit; i++)
hailfingerb437e282010-11-04 01:04:27 +0000865 if (have[j * 256 + i] != 0xff) {
hailfingerb247c7a2010-03-08 00:42:32 +0000866 result = 1;
867 break;
868 }
869 if (result)
870 break;
871 }
872 break;
hailfingerb437e282010-11-04 01:04:27 +0000873 default:
874 msg_cerr("%s: Unsupported granularity! Please report a bug at "
875 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000876 }
877 return result;
878}
879
hailfingerb437e282010-11-04 01:04:27 +0000880/**
881 * Check if the buffer @have needs to be programmed to get the content of @want.
882 * If yes, return 1 and fill in first_start with the start address of the
883 * write operation and first_len with the length of the first to-be-written
884 * chunk. If not, return 0 and leave first_start and first_len undefined.
885 *
886 * Warning: This function assumes that @have and @want point to naturally
887 * aligned regions.
888 *
889 * @have buffer with current content
890 * @want buffer with desired content
891 * @len length of the checked area
892 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000893 * @first_start offset of the first byte which needs to be written (passed in
894 * value is increased by the offset of the first needed write
895 * relative to have/want or unchanged if no write is needed)
896 * @return length of the first contiguous area which needs to be written
897 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000898 *
899 * FIXME: This function needs a parameter which tells it about coalescing
900 * in relation to the max write length of the programmer and the max write
901 * length of the chip.
902 */
903static int get_next_write(uint8_t *have, uint8_t *want, int len,
hailfinger90fcf9b2010-11-05 14:51:59 +0000904 int *first_start, enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000905{
hailfinger90fcf9b2010-11-05 14:51:59 +0000906 int need_write = 0, rel_start = 0, first_len = 0;
907 int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000908
hailfingerb437e282010-11-04 01:04:27 +0000909 switch (gran) {
910 case write_gran_1bit:
911 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000912 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000913 break;
914 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000915 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000916 break;
917 default:
918 msg_cerr("%s: Unsupported granularity! Please report a bug at "
919 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000920 /* Claim that no write was needed. A write with unknown
921 * granularity is too dangerous to try.
922 */
923 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000924 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000925 for (i = 0; i < len / stride; i++) {
926 limit = min(stride, len - i * stride);
927 /* Are 'have' and 'want' identical? */
928 if (memcmp(have + i * stride, want + i * stride, limit)) {
929 if (!need_write) {
930 /* First location where have and want differ. */
931 need_write = 1;
932 rel_start = i * stride;
933 }
934 } else {
935 if (need_write) {
936 /* First location where have and want
937 * do not differ anymore.
938 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000939 break;
940 }
941 }
942 }
hailfingerffb7f382010-12-06 13:05:44 +0000943 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000944 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000945 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000946 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000947}
948
hailfinger0c515352009-11-23 12:55:31 +0000949/* This function generates various test patterns useful for testing controller
950 * and chip communication as well as chip behaviour.
951 *
952 * If a byte can be written multiple times, each time keeping 0-bits at 0
953 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
954 * is essentially an AND operation. That's also the reason why this function
955 * provides the result of AND between various patterns.
956 *
957 * Below is a list of patterns (and their block length).
958 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
959 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
960 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
961 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
962 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
963 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
964 * Pattern 6 is 00 (1 Byte)
965 * Pattern 7 is ff (1 Byte)
966 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
967 * byte block.
968 *
969 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
970 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
971 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
972 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
973 * Pattern 12 is 00 (1 Byte)
974 * Pattern 13 is ff (1 Byte)
975 * Patterns 8-13 have no block number.
976 *
977 * Patterns 0-3 are created to detect and efficiently diagnose communication
978 * slips like missed bits or bytes and their repetitive nature gives good visual
979 * cues to the person inspecting the results. In addition, the following holds:
980 * AND Pattern 0/1 == Pattern 4
981 * AND Pattern 2/3 == Pattern 5
982 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
983 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
984 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
985 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
986 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
987 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
988 * Besides that, they provide for bit testing of the last two bytes of every
989 * 256 byte block which contains the block number for patterns 0-6.
990 * Patterns 10-11 are special purpose for detecting subblock aliasing with
991 * block sizes >256 bytes (some Dataflash chips etc.)
992 * AND Pattern 8/9 == Pattern 12
993 * AND Pattern 10/11 == Pattern 12
994 * Pattern 13 is the completely erased state.
995 * None of the patterns can detect aliasing at boundaries which are a multiple
996 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
997 */
998int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
999{
1000 int i;
1001
1002 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +00001003 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +00001004 return 1;
1005 }
1006
1007 switch (variant) {
1008 case 0:
1009 for (i = 0; i < size; i++)
1010 buf[i] = (i & 0xf) << 4 | 0x5;
1011 break;
1012 case 1:
1013 for (i = 0; i < size; i++)
1014 buf[i] = (i & 0xf) << 4 | 0xa;
1015 break;
1016 case 2:
1017 for (i = 0; i < size; i++)
1018 buf[i] = 0x50 | (i & 0xf);
1019 break;
1020 case 3:
1021 for (i = 0; i < size; i++)
1022 buf[i] = 0xa0 | (i & 0xf);
1023 break;
1024 case 4:
1025 for (i = 0; i < size; i++)
1026 buf[i] = (i & 0xf) << 4;
1027 break;
1028 case 5:
1029 for (i = 0; i < size; i++)
1030 buf[i] = i & 0xf;
1031 break;
1032 case 6:
1033 memset(buf, 0x00, size);
1034 break;
1035 case 7:
1036 memset(buf, 0xff, size);
1037 break;
1038 case 8:
1039 for (i = 0; i < size; i++)
1040 buf[i] = i & 0xff;
1041 break;
1042 case 9:
1043 for (i = 0; i < size; i++)
1044 buf[i] = ~(i & 0xff);
1045 break;
1046 case 10:
1047 for (i = 0; i < size % 2; i++) {
1048 buf[i * 2] = (i >> 8) & 0xff;
1049 buf[i * 2 + 1] = i & 0xff;
1050 }
1051 if (size & 0x1)
1052 buf[i * 2] = (i >> 8) & 0xff;
1053 break;
1054 case 11:
1055 for (i = 0; i < size % 2; i++) {
1056 buf[i * 2] = ~((i >> 8) & 0xff);
1057 buf[i * 2 + 1] = ~(i & 0xff);
1058 }
1059 if (size & 0x1)
1060 buf[i * 2] = ~((i >> 8) & 0xff);
1061 break;
1062 case 12:
1063 memset(buf, 0x00, size);
1064 break;
1065 case 13:
1066 memset(buf, 0xff, size);
1067 break;
1068 }
1069
1070 if ((variant >= 0) && (variant <= 7)) {
1071 /* Write block number in the last two bytes of each 256-byte
1072 * block, big endian for easier reading of the hexdump.
1073 * Note that this wraps around for chips larger than 2^24 bytes
1074 * (16 MB).
1075 */
1076 for (i = 0; i < size / 256; i++) {
1077 buf[i * 256 + 254] = (i >> 8) & 0xff;
1078 buf[i * 256 + 255] = i & 0xff;
1079 }
1080 }
1081
1082 return 0;
1083}
1084
hailfingeraec9c962009-10-31 01:53:09 +00001085int check_max_decode(enum chipbustype buses, uint32_t size)
1086{
1087 int limitexceeded = 0;
1088 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
1089 (max_rom_decode.parallel < size)) {
1090 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001091 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001092 "size %u kB of chipset/board/programmer "
1093 "for %s interface, "
1094 "probe/read/erase/write may fail. ", size / 1024,
1095 max_rom_decode.parallel / 1024, "Parallel");
1096 }
1097 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
1098 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001099 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001100 "size %u kB of chipset/board/programmer "
1101 "for %s interface, "
1102 "probe/read/erase/write may fail. ", size / 1024,
1103 max_rom_decode.lpc / 1024, "LPC");
1104 }
1105 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
1106 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001107 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001108 "size %u kB of chipset/board/programmer "
1109 "for %s interface, "
1110 "probe/read/erase/write may fail. ", size / 1024,
1111 max_rom_decode.fwh / 1024, "FWH");
1112 }
1113 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
1114 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001115 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001116 "size %u kB of chipset/board/programmer "
1117 "for %s interface, "
1118 "probe/read/erase/write may fail. ", size / 1024,
1119 max_rom_decode.spi / 1024, "SPI");
1120 }
1121 if (!limitexceeded)
1122 return 0;
1123 /* Sometimes chip and programmer have more than one bus in common,
1124 * and the limit is not exceeded on all buses. Tell the user.
1125 */
1126 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001127 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001128 msg_pdbg("There is at least one common chip/programmer "
hailfingeraec9c962009-10-31 01:53:09 +00001129 "interface which can support a chip of this size. "
1130 "You can try --force at your own risk.\n");
1131 return 1;
1132}
1133
hailfinger48ed3e22011-05-04 00:39:50 +00001134int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001135{
hailfinger48ed3e22011-05-04 00:39:50 +00001136 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001137 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001138 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001139 uint32_t size;
1140 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001141 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001142
hailfinger48ed3e22011-05-04 00:39:50 +00001143 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001144 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001145 continue;
hailfinger327d2522010-03-22 23:43:51 +00001146 msg_gdbg("Probing for %s %s, %d KB: ",
stepanb8361b92008-03-17 22:59:40 +00001147 flash->vendor, flash->name, flash->total_size);
stuge98c09aa2008-06-18 02:08:40 +00001148 if (!flash->probe && !force) {
hailfinger327d2522010-03-22 23:43:51 +00001149 msg_gdbg("failed! flashrom has no probe function for "
1150 "this flash chip.\n");
stuge8ce3a3c2008-04-28 14:47:30 +00001151 continue;
1152 }
hailfingeraec9c962009-10-31 01:53:09 +00001153 buses_common = buses_supported & flash->bustype;
1154 if (!buses_common) {
hailfingera916b422009-06-01 02:08:58 +00001155 tmp = flashbuses_to_text(buses_supported);
hailfinger327d2522010-03-22 23:43:51 +00001156 msg_gdbg("skipped.");
1157 msg_gspew(" Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001158 free(tmp);
1159 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001160 msg_gspew("and chip bus type %s are incompatible.",
1161 tmp);
hailfingera916b422009-06-01 02:08:58 +00001162 free(tmp);
hailfinger327d2522010-03-22 23:43:51 +00001163 msg_gdbg("\n");
hailfingera916b422009-06-01 02:08:58 +00001164 continue;
1165 }
stepan782fb172007-04-06 11:58:03 +00001166
ollie5672ac62004-03-17 22:22:08 +00001167 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001168 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001169
hailfinger48ed3e22011-05-04 00:39:50 +00001170 /* Start filling in the dynamic data. */
1171 *fill_flash = *flash;
1172
hailfinger72d3b982009-05-09 07:27:23 +00001173 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001174 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001175
stugec1e55fe2008-07-02 17:15:47 +00001176 if (force)
1177 break;
stepanc98b80b2006-03-16 16:57:41 +00001178
hailfinger48ed3e22011-05-04 00:39:50 +00001179 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001180 goto notfound;
1181
hailfinger48ed3e22011-05-04 00:39:50 +00001182 /* If this is the first chip found, accept it.
1183 * If this is not the first chip found, accept it only if it is
1184 * a non-generic match.
1185 * We could either make chipcount global or provide it as
1186 * parameter, or we assume that startchip==0 means this call to
1187 * probe_flash() is the first one and thus no chip has been
1188 * found before.
1189 */
1190 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001191 break;
1192
stuge56300c32008-09-03 23:10:05 +00001193notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001194 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001195 }
uwebe4477b2007-08-23 16:08:21 +00001196
stugec1e55fe2008-07-02 17:15:47 +00001197 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001198 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001199
hailfingere11396b2011-03-08 00:09:11 +00001200#if CONFIG_INTERNAL == 1
1201 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001202 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001203 else
1204#endif
stepan3e7aeae2011-01-19 06:21:54 +00001205 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001206
1207 msg_cinfo("%s chip \"%s %s\" (%d KB, %s) %s.\n",
hailfingerf4aaccc2010-04-28 15:22:14 +00001208 force ? "Assuming" : "Found",
uwe9e6811e2009-06-28 21:47:57 +00001209 flash->vendor, flash->name, flash->total_size,
stepan3e7aeae2011-01-19 06:21:54 +00001210 flashbuses_to_text(flash->bustype), location);
uwe9e6811e2009-06-28 21:47:57 +00001211
hailfinger0f4c3952010-12-02 21:59:42 +00001212 /* Flash registers will not be mapped if the chip was forced. Lock info
1213 * may be stored in registers, so avoid lock info printing.
1214 */
1215 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001216 if (fill_flash->printlock)
1217 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001218
hailfinger48ed3e22011-05-04 00:39:50 +00001219 /* Return position of matching chip. */
1220 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001221}
1222
stepan193c9c22005-12-18 16:41:10 +00001223int verify_flash(struct flashchip *flash, uint8_t *buf)
rminnich8d3ff912003-10-25 17:01:29 +00001224{
hailfingerb0f4d122009-06-24 08:20:45 +00001225 int ret;
ollie5b621572004-03-20 16:46:10 +00001226 int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001227
snelsone42c3802010-05-07 20:09:04 +00001228 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001229
hailfingerb0f4d122009-06-24 08:20:45 +00001230 ret = verify_range(flash, buf, 0, total_size, NULL);
uwef6641642007-05-09 10:17:44 +00001231
hailfingerb0f4d122009-06-24 08:20:45 +00001232 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001233 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001234
hailfingerb0f4d122009-06-24 08:20:45 +00001235 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001236}
1237
hailfinger771fc182010-10-15 00:01:14 +00001238int read_buf_from_file(unsigned char *buf, unsigned long size, char *filename)
1239{
1240 unsigned long numbytes;
1241 FILE *image;
1242 struct stat image_stat;
1243
1244 if ((image = fopen(filename, "rb")) == NULL) {
1245 perror(filename);
1246 return 1;
1247 }
1248 if (fstat(fileno(image), &image_stat) != 0) {
1249 perror(filename);
1250 fclose(image);
1251 return 1;
1252 }
1253 if (image_stat.st_size != size) {
1254 msg_gerr("Error: Image size doesn't match\n");
1255 fclose(image);
1256 return 1;
1257 }
1258 numbytes = fread(buf, 1, size, image);
1259 if (fclose(image)) {
1260 perror(filename);
1261 return 1;
1262 }
1263 if (numbytes != size) {
1264 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1265 "wanted %ld!\n", numbytes, size);
1266 return 1;
1267 }
1268 return 0;
1269}
1270
hailfinger42a850a2010-07-13 23:56:13 +00001271int write_buf_to_file(unsigned char *buf, unsigned long size, char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001272{
1273 unsigned long numbytes;
1274 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001275
1276 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001277 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001278 return 1;
1279 }
oxygenebf70d352010-01-25 22:55:33 +00001280 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001281 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001282 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001283 }
hailfingerd219a232009-01-28 00:27:54 +00001284
hailfingerd219a232009-01-28 00:27:54 +00001285 numbytes = fwrite(buf, 1, size, image);
1286 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001287 if (numbytes != size) {
1288 msg_gerr("File %s could not be written completely.\n",
1289 filename);
hailfingerd219a232009-01-28 00:27:54 +00001290 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001291 }
hailfingerd219a232009-01-28 00:27:54 +00001292 return 0;
1293}
1294
hailfinger42a850a2010-07-13 23:56:13 +00001295int read_flash_to_file(struct flashchip *flash, char *filename)
1296{
1297 unsigned long size = flash->total_size * 1024;
1298 unsigned char *buf = calloc(size, sizeof(char));
1299 int ret = 0;
1300
1301 msg_cinfo("Reading flash... ");
1302 if (!buf) {
1303 msg_gerr("Memory allocation failed!\n");
1304 msg_cinfo("FAILED.\n");
1305 return 1;
1306 }
1307 if (!flash->read) {
1308 msg_cerr("No read function available for this flash chip.\n");
1309 ret = 1;
1310 goto out_free;
1311 }
1312 if (flash->read(flash, buf, 0, size)) {
1313 msg_cerr("Read operation failed!\n");
1314 ret = 1;
1315 goto out_free;
1316 }
1317
1318 ret = write_buf_to_file(buf, flash->total_size * 1024, filename);
1319out_free:
1320 free(buf);
1321 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1322 return ret;
1323}
1324
hailfingerb437e282010-11-04 01:04:27 +00001325/* This function shares a lot of its structure with erase_and_write_flash() and
1326 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001327 * Even if an error is found, the function will keep going and check the rest.
1328 */
hailfinger48ed3e22011-05-04 00:39:50 +00001329static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001330{
hailfinger9fed35d2010-01-19 06:42:46 +00001331 int i, j, k;
1332 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001333
1334 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1335 unsigned int done = 0;
1336 struct block_eraser eraser = flash->block_erasers[k];
1337
1338 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1339 /* Blocks with zero size are bugs in flashchips.c. */
1340 if (eraser.eraseblocks[i].count &&
1341 !eraser.eraseblocks[i].size) {
1342 msg_gerr("ERROR: Flash chip %s erase function "
1343 "%i region %i has size 0. Please report"
1344 " a bug at flashrom@flashrom.org\n",
1345 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001346 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001347 }
1348 /* Blocks with zero count are bugs in flashchips.c. */
1349 if (!eraser.eraseblocks[i].count &&
1350 eraser.eraseblocks[i].size) {
1351 msg_gerr("ERROR: Flash chip %s erase function "
1352 "%i region %i has count 0. Please report"
1353 " a bug at flashrom@flashrom.org\n",
1354 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001355 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001356 }
1357 done += eraser.eraseblocks[i].count *
1358 eraser.eraseblocks[i].size;
1359 }
hailfinger9fed35d2010-01-19 06:42:46 +00001360 /* Empty eraseblock definition with erase function. */
1361 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001362 msg_gspew("Strange: Empty eraseblock definition with "
hailfinger9fed35d2010-01-19 06:42:46 +00001363 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001364 if (!done)
1365 continue;
1366 if (done != flash->total_size * 1024) {
1367 msg_gerr("ERROR: Flash chip %s erase function %i "
1368 "region walking resulted in 0x%06x bytes total,"
1369 " expected 0x%06x bytes. Please report a bug at"
1370 " flashrom@flashrom.org\n", flash->name, k,
1371 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001372 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001373 }
hailfinger9fed35d2010-01-19 06:42:46 +00001374 if (!eraser.block_erase)
1375 continue;
1376 /* Check if there are identical erase functions for different
1377 * layouts. That would imply "magic" erase functions. The
1378 * easiest way to check this is with function pointers.
1379 */
uwef6f94d42010-03-13 17:28:29 +00001380 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001381 if (eraser.block_erase ==
1382 flash->block_erasers[j].block_erase) {
1383 msg_gerr("ERROR: Flash chip %s erase function "
1384 "%i and %i are identical. Please report"
1385 " a bug at flashrom@flashrom.org\n",
1386 flash->name, k, j);
1387 ret = 1;
1388 }
uwef6f94d42010-03-13 17:28:29 +00001389 }
hailfinger45177872010-01-18 08:14:43 +00001390 }
hailfinger9fed35d2010-01-19 06:42:46 +00001391 return ret;
hailfinger45177872010-01-18 08:14:43 +00001392}
1393
hailfingerb437e282010-11-04 01:04:27 +00001394static int erase_and_write_block_helper(struct flashchip *flash,
1395 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001396 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001397 uint8_t *newcontents,
1398 int (*erasefn) (struct flashchip *flash,
1399 unsigned int addr,
1400 unsigned int len))
1401{
1402 int starthere = 0;
1403 int lenhere = 0;
1404 int ret = 0;
1405 int skip = 1;
1406 int writecount = 0;
1407 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1408
hailfinger90fcf9b2010-11-05 14:51:59 +00001409 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001410 * need to be adjusted here to keep the impression of proper abstraction
1411 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001412 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001413 newcontents += start;
1414 msg_cdbg(":");
1415 /* FIXME: Assume 256 byte granularity for now to play it safe. */
hailfinger90fcf9b2010-11-05 14:51:59 +00001416 if (need_erase(curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001417 msg_cdbg("E");
1418 ret = erasefn(flash, start, len);
1419 if (ret)
1420 return ret;
hailfinger90fcf9b2010-11-05 14:51:59 +00001421 /* Erase was successful. Adjust curcontents. */
1422 memset(curcontents, 0xff, len);
hailfingerb437e282010-11-04 01:04:27 +00001423 skip = 0;
1424 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001425 /* get_next_write() sets starthere to a new value after the call. */
1426 while ((lenhere = get_next_write(curcontents + starthere,
1427 newcontents + starthere,
1428 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001429 if (!writecount++)
1430 msg_cdbg("W");
1431 /* Needs the partial write function signature. */
1432 ret = flash->write(flash, newcontents + starthere,
1433 start + starthere, lenhere);
1434 if (ret)
1435 return ret;
1436 starthere += lenhere;
1437 skip = 0;
1438 }
1439 if (skip)
1440 msg_cdbg("S");
1441 return ret;
1442}
1443
hailfinger83541b32010-07-13 00:42:00 +00001444static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1445 int (*do_something) (struct flashchip *flash,
1446 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001447 unsigned int len,
1448 uint8_t *param1,
1449 uint8_t *param2,
1450 int (*erasefn) (
1451 struct flashchip *flash,
1452 unsigned int addr,
1453 unsigned int len)),
1454 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001455{
1456 int i, j;
1457 unsigned int start = 0;
1458 unsigned int len;
1459 struct block_eraser eraser = flash->block_erasers[erasefunction];
1460 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1461 /* count==0 for all automatically initialized array
1462 * members so the loop below won't be executed for them.
1463 */
1464 len = eraser.eraseblocks[i].size;
1465 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001466 /* Print this for every block except the first one. */
1467 if (i || j)
1468 msg_cdbg(", ");
1469 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001470 start + len - 1);
hailfingerb437e282010-11-04 01:04:27 +00001471 if (do_something(flash, start, len, param1, param2,
1472 eraser.block_erase)) {
1473 msg_cdbg("\n");
hailfinger2b8c9382010-07-13 00:37:19 +00001474 return 1;
hailfingerb437e282010-11-04 01:04:27 +00001475 }
hailfinger2b8c9382010-07-13 00:37:19 +00001476 start += len;
1477 }
1478 }
hailfingerb437e282010-11-04 01:04:27 +00001479 msg_cdbg("\n");
hailfinger2b8c9382010-07-13 00:37:19 +00001480 return 0;
1481}
1482
hailfingercf848f12010-12-05 15:14:44 +00001483static int check_block_eraser(struct flashchip *flash, int k, int log)
1484{
1485 struct block_eraser eraser = flash->block_erasers[k];
1486
1487 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1488 if (log)
1489 msg_cdbg("not defined. ");
1490 return 1;
1491 }
1492 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1493 if (log)
1494 msg_cdbg("eraseblock layout is known, but matching "
1495 "block erase function is not implemented. ");
1496 return 1;
1497 }
1498 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1499 if (log)
1500 msg_cdbg("block erase function found, but "
1501 "eraseblock layout is not defined. ");
1502 return 1;
1503 }
1504 return 0;
1505}
1506
hailfingerb437e282010-11-04 01:04:27 +00001507int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents, uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001508{
hailfingercf848f12010-12-05 15:14:44 +00001509 int k, ret = 0;
hailfingerb437e282010-11-04 01:04:27 +00001510 uint8_t *curcontents;
1511 unsigned long size = flash->total_size * 1024;
hailfingercf848f12010-12-05 15:14:44 +00001512 int usable_erasefunctions = 0;
1513
1514 for (k = 0; k < NUM_ERASEFUNCTIONS; k++)
1515 if (!check_block_eraser(flash, k, 0))
1516 usable_erasefunctions++;
1517 msg_cinfo("Erasing and writing flash chip... ");
1518 if (!usable_erasefunctions) {
1519 msg_cerr("ERROR: flashrom has no erase function for this flash "
1520 "chip.\n");
1521 return 1;
1522 }
hailfinger7df21362009-09-05 02:30:58 +00001523
hailfingerb437e282010-11-04 01:04:27 +00001524 curcontents = (uint8_t *) malloc(size);
1525 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1526 memcpy(curcontents, oldcontents, size);
1527
hailfinger7df21362009-09-05 02:30:58 +00001528 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
snelsone42c3802010-05-07 20:09:04 +00001529 msg_cdbg("Looking at blockwise erase function %i... ", k);
hailfingercf848f12010-12-05 15:14:44 +00001530 if (check_block_eraser(flash, k, 1) && usable_erasefunctions) {
1531 msg_cdbg("Looking for another erase function.\n");
hailfinger7df21362009-09-05 02:30:58 +00001532 continue;
1533 }
hailfingercf848f12010-12-05 15:14:44 +00001534 usable_erasefunctions--;
snelsone42c3802010-05-07 20:09:04 +00001535 msg_cdbg("trying... ");
hailfingerb437e282010-11-04 01:04:27 +00001536 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper, curcontents, newcontents);
snelsone42c3802010-05-07 20:09:04 +00001537 msg_cdbg("\n");
hailfinger7df21362009-09-05 02:30:58 +00001538 /* If everything is OK, don't try another erase function. */
1539 if (!ret)
1540 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001541 /* Write/erase failed, so try to find out what the current chip
1542 * contents are. If no usable erase functions remain, we could
1543 * abort the loop instead of continuing, the effect is the same.
1544 * The only difference is whether the reason for other unusable
1545 * functions is printed or not. If in doubt, verbosity wins.
hailfingerb437e282010-11-04 01:04:27 +00001546 */
hailfingercf848f12010-12-05 15:14:44 +00001547 if (!usable_erasefunctions)
1548 continue;
hailfinger6237f5e2010-12-02 02:41:55 +00001549 if (flash->read(flash, curcontents, 0, size)) {
1550 /* Now we are truly screwed. Read failed as well. */
1551 msg_cerr("Can't read anymore!\n");
1552 /* We have no idea about the flash chip contents, so
1553 * retrying with another erase function is pointless.
1554 */
1555 break;
1556 }
hailfinger7df21362009-09-05 02:30:58 +00001557 }
hailfingerb437e282010-11-04 01:04:27 +00001558 /* Free the scratchpad. */
1559 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001560
hailfinger7df21362009-09-05 02:30:58 +00001561 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001562 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001563 } else {
hailfingerb437e282010-11-04 01:04:27 +00001564 msg_cinfo("Done.\n");
hailfinger7df21362009-09-05 02:30:58 +00001565 }
1566 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001567}
1568
hailfinger4c47e9d2010-10-19 22:06:20 +00001569void nonfatal_help_message(void)
1570{
1571 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1572 "This means we have to add special support for your board, "
1573 "programmer or flash chip.\n"
1574 "Please report this on IRC at irc.freenode.net (channel "
1575 "#flashrom) or\n"
1576 "mail flashrom@flashrom.org!\n"
1577 "-------------------------------------------------------------"
1578 "------------------\n"
1579 "You may now reboot or simply leave the machine running.\n");
1580}
1581
uweb34ec9f2009-10-01 18:40:02 +00001582void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001583{
snelsone42c3802010-05-07 20:09:04 +00001584 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001585 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001586 "mail flashrom@flashrom.org with FAILED: your board name in "
1587 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001588 "-------------------------------------------------------------"
1589 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001590 "DO NOT REBOOT OR POWEROFF!\n");
1591}
1592
hailfingerc77acb52009-12-24 02:15:55 +00001593/* The way to go if you want a delimited list of programmers*/
1594void list_programmers(char *delim)
1595{
1596 enum programmer p;
1597 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001598 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001599 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001600 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001601 }
snelsone42c3802010-05-07 20:09:04 +00001602 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001603}
1604
hailfingerf79d1712010-10-06 23:48:34 +00001605void list_programmers_linebreak(int startcol, int cols, int paren)
1606{
1607 const char *pname;
1608 int pnamelen;
1609 int remaining = 0;
1610 int firstline = 1;
1611 enum programmer p;
1612 int i;
1613
1614 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1615 pname = programmer_table[p].name;
1616 pnamelen = strlen(pname);
1617 if (remaining - pnamelen - 2 < 0) {
1618 if (firstline)
1619 firstline = 0;
1620 else
1621 printf("\n");
1622 for (i = 0; i < startcol; i++)
1623 printf(" ");
1624 remaining = cols - startcol;
1625 } else {
1626 printf(" ");
1627 remaining--;
1628 }
1629 if (paren && (p == 0)) {
1630 printf("(");
1631 remaining--;
1632 }
1633 printf("%s", pname);
1634 remaining -= pnamelen;
1635 if (p < PROGRAMMER_INVALID - 1) {
1636 printf(",");
1637 remaining--;
1638 } else {
1639 if (paren)
1640 printf(")");
1641 printf("\n");
1642 }
1643 }
1644}
1645
hailfinger3b471632010-03-27 16:36:40 +00001646void print_sysinfo(void)
1647{
1648#if HAVE_UTSNAME == 1
1649 struct utsname osinfo;
1650 uname(&osinfo);
1651
1652 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1653 osinfo.machine);
1654#else
1655 msg_ginfo(" on unknown machine");
1656#endif
1657 msg_ginfo(", built with");
1658#if NEED_PCI == 1
1659#ifdef PCILIB_VERSION
1660 msg_ginfo(" libpci %s,", PCILIB_VERSION);
1661#else
1662 msg_ginfo(" unknown PCI library,");
1663#endif
1664#endif
1665#ifdef __clang__
hailfinger3cc85ad2010-07-17 14:49:30 +00001666 msg_ginfo(" LLVM Clang");
1667#ifdef __clang_version__
1668 msg_ginfo(" %s,", __clang_version__);
1669#else
1670 msg_ginfo(" unknown version (before r102686),");
1671#endif
hailfinger3b471632010-03-27 16:36:40 +00001672#elif defined(__GNUC__)
1673 msg_ginfo(" GCC");
1674#ifdef __VERSION__
hailfinger324a9cc2010-05-26 01:45:41 +00001675 msg_ginfo(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001676#else
hailfinger324a9cc2010-05-26 01:45:41 +00001677 msg_ginfo(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001678#endif
1679#else
hailfinger324a9cc2010-05-26 01:45:41 +00001680 msg_ginfo(" unknown compiler,");
1681#endif
1682#if defined (__FLASHROM_LITTLE_ENDIAN__)
1683 msg_ginfo(" little endian");
1684#else
1685 msg_ginfo(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001686#endif
1687 msg_ginfo("\n");
1688}
1689
uwefdeca092008-01-21 15:24:22 +00001690void print_version(void)
1691{
hailfinger74819ad2010-05-15 15:04:37 +00001692 msg_ginfo("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001693 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001694}
1695
hailfinger74819ad2010-05-15 15:04:37 +00001696void print_banner(void)
1697{
1698 msg_ginfo("flashrom is free software, get the source code at "
1699 "http://www.flashrom.org\n");
1700 msg_ginfo("\n");
1701}
1702
hailfingerc77acb52009-12-24 02:15:55 +00001703int selfcheck(void)
1704{
hailfinger45177872010-01-18 08:14:43 +00001705 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001706 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001707
1708 /* Safety check. Instead of aborting after the first error, check
1709 * if more errors exist.
1710 */
hailfingerc77acb52009-12-24 02:15:55 +00001711 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001712 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001713 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001714 }
1715 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001716 msg_gerr("SPI programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001717 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001718 }
hailfinger45177872010-01-18 08:14:43 +00001719 for (flash = flashchips; flash && flash->name; flash++)
1720 if (selfcheck_eraseblocks(flash))
1721 ret = 1;
1722 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001723}
1724
hailfinger48ed3e22011-05-04 00:39:50 +00001725void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001726{
1727 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
snelsone42c3802010-05-07 20:09:04 +00001728 msg_cinfo("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001729 if (flash->tested & TEST_BAD_MASK) {
snelsone42c3802010-05-07 20:09:04 +00001730 msg_cinfo("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001731 if (flash->tested & TEST_BAD_PROBE)
snelsone42c3802010-05-07 20:09:04 +00001732 msg_cinfo(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001733 if (flash->tested & TEST_BAD_READ)
snelsone42c3802010-05-07 20:09:04 +00001734 msg_cinfo(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001735 if (flash->tested & TEST_BAD_ERASE)
snelsone42c3802010-05-07 20:09:04 +00001736 msg_cinfo(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001737 if (flash->tested & TEST_BAD_WRITE)
snelsone42c3802010-05-07 20:09:04 +00001738 msg_cinfo(" WRITE");
1739 msg_cinfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001740 }
1741 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1742 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1743 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1744 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
snelsone42c3802010-05-07 20:09:04 +00001745 msg_cinfo("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001746 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
snelsone42c3802010-05-07 20:09:04 +00001747 msg_cinfo(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001748 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
snelsone42c3802010-05-07 20:09:04 +00001749 msg_cinfo(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001750 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
snelsone42c3802010-05-07 20:09:04 +00001751 msg_cinfo(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001752 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
snelsone42c3802010-05-07 20:09:04 +00001753 msg_cinfo(" WRITE");
1754 msg_cinfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001755 }
hailfinger92cd8e32010-01-07 03:24:05 +00001756 /* FIXME: This message is designed towards CLI users. */
hailfinger74819ad2010-05-15 15:04:37 +00001757 msg_cinfo("The test status of this chip may have been updated "
1758 "in the latest development\n"
1759 "version of flashrom. If you are running the latest "
1760 "development version,\n"
1761 "please email a report to flashrom@flashrom.org if "
1762 "any of the above operations\n"
1763 "work correctly for you with this flash part. Please "
1764 "include the flashrom\n"
1765 "output with the additional -V option for all "
1766 "operations you tested (-V, -Vr,\n"
1767 "-Vw, -VE), and mention which mainboard or "
1768 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001769 "Please mention your board in the subject line. "
1770 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001771 }
1772}
1773
ollie5b621572004-03-20 16:46:10 +00001774int main(int argc, char *argv[])
rminnich8d3ff912003-10-25 17:01:29 +00001775{
hailfinger92cd8e32010-01-07 03:24:05 +00001776 return cli_classic(argc, argv);
hailfingerc77acb52009-12-24 02:15:55 +00001777}
1778
hailfinger771fc182010-10-15 00:01:14 +00001779/* FIXME: This function signature needs to be improved once doit() has a better
1780 * function signature.
1781 */
1782int chip_safety_check(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1783{
1784 if (!programmer_may_write && (write_it || erase_it)) {
1785 msg_perr("Write/erase is not working yet on your programmer in "
1786 "its current configuration.\n");
1787 /* --force is the wrong approach, but it's the best we can do
1788 * until the generic programmer parameter parser is merged.
1789 */
1790 if (!force)
1791 return 1;
1792 msg_cerr("Continuing anyway.\n");
1793 }
1794
1795 if (read_it || erase_it || write_it || verify_it) {
1796 /* Everything needs read. */
1797 if (flash->tested & TEST_BAD_READ) {
1798 msg_cerr("Read is not working on this chip. ");
1799 if (!force)
1800 return 1;
1801 msg_cerr("Continuing anyway.\n");
1802 }
1803 if (!flash->read) {
1804 msg_cerr("flashrom has no read function for this "
1805 "flash chip.\n");
1806 return 1;
1807 }
1808 }
1809 if (erase_it || write_it) {
1810 /* Write needs erase. */
1811 if (flash->tested & TEST_BAD_ERASE) {
1812 msg_cerr("Erase is not working on this chip. ");
1813 if (!force)
1814 return 1;
1815 msg_cerr("Continuing anyway.\n");
1816 }
1817 /* FIXME: Check if at least one erase function exists. */
1818 }
1819 if (write_it) {
1820 if (flash->tested & TEST_BAD_WRITE) {
1821 msg_cerr("Write is not working on this chip. ");
1822 if (!force)
1823 return 1;
1824 msg_cerr("Continuing anyway.\n");
1825 }
1826 if (!flash->write) {
1827 msg_cerr("flashrom has no write function for this "
1828 "flash chip.\n");
1829 return 1;
1830 }
1831 }
1832 return 0;
1833}
1834
hailfingerc77acb52009-12-24 02:15:55 +00001835/* This function signature is horrible. We need to design a better interface,
1836 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001837 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001838 */
1839int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1840{
hailfinger4c47e9d2010-10-19 22:06:20 +00001841 uint8_t *oldcontents;
1842 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001843 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001844 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001845
hailfinger771fc182010-10-15 00:01:14 +00001846 if (chip_safety_check(flash, force, filename, read_it, write_it, erase_it, verify_it)) {
1847 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001848 ret = 1;
1849 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001850 }
1851
hailfinger771fc182010-10-15 00:01:14 +00001852 /* Given the existence of read locks, we want to unlock for read,
1853 * erase and write.
1854 */
1855 if (flash->unlock)
1856 flash->unlock(flash);
1857
1858 if (read_it) {
1859 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001860 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001861 }
hailfingerb437e282010-11-04 01:04:27 +00001862
1863 oldcontents = (uint8_t *) malloc(size);
1864 /* Assume worst case: All bits are 0. */
1865 memset(oldcontents, 0x00, size);
1866 newcontents = (uint8_t *) malloc(size);
1867 /* Assume best case: All bits should be 1. */
1868 memset(newcontents, 0xff, size);
1869 /* Side effect of the assumptions above: Default write action is erase
1870 * because newcontents looks like a completely erased chip, and
1871 * oldcontents being completely 0x00 means we have to erase everything
1872 * before we can write.
1873 */
1874
ollie0eb62d62004-12-08 20:10:01 +00001875 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001876 /* FIXME: Do we really want the scary warning if erase failed?
1877 * After all, after erase the chip is either blank or partially
1878 * blank or it has the old contents. A blank chip won't boot,
1879 * so if the user wanted erase and reboots afterwards, the user
1880 * knows very well that booting won't work.
1881 */
hailfingerb437e282010-11-04 01:04:27 +00001882 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001883 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001884 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001885 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001886 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001887 }
hailfinger771fc182010-10-15 00:01:14 +00001888
hailfingerd217d122010-10-08 18:52:29 +00001889 if (write_it || verify_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001890 if (read_buf_from_file(newcontents, size, filename)) {
hailfinger90fcf9b2010-11-05 14:51:59 +00001891 ret = 1;
1892 goto out;
stepan1da96c02006-11-21 23:48:51 +00001893 }
1894
hailfinger90c7d542010-05-31 15:27:27 +00001895#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001896 if (programmer == PROGRAMMER_INTERNAL)
1897 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001898#endif
ollie5672ac62004-03-17 22:22:08 +00001899 }
1900
hailfinger4c47e9d2010-10-19 22:06:20 +00001901 /* Read the whole chip to be able to check whether regions need to be
1902 * erased and to give better diagnostics in case write fails.
1903 * The alternative would be to read only the regions which are to be
1904 * preserved, but in that case we might perform unneeded erase which
1905 * takes time as well.
1906 */
1907 msg_cdbg("Reading old flash chip contents...\n");
1908 if (flash->read(flash, oldcontents, 0, size)) {
hailfinger90fcf9b2010-11-05 14:51:59 +00001909 ret = 1;
1910 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00001911 }
1912
ollie6a600992005-11-26 21:55:36 +00001913 // This should be moved into each flash part's code to do it
1914 // cleanly. This does the job.
hailfinger4c47e9d2010-10-19 22:06:20 +00001915 handle_romentries(flash, oldcontents, newcontents);
uwef6641642007-05-09 10:17:44 +00001916
ollie6a600992005-11-26 21:55:36 +00001917 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00001918
stuge8ce3a3c2008-04-28 14:47:30 +00001919 if (write_it) {
hailfingerb437e282010-11-04 01:04:27 +00001920 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1921 msg_cerr("Uh oh. Erase/write failed. Checking if "
1922 "anything changed.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00001923 if (!flash->read(flash, newcontents, 0, size)) {
1924 if (!memcmp(oldcontents, newcontents, size)) {
1925 msg_cinfo("Good. It seems nothing was "
1926 "changed.\n");
1927 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00001928 ret = 1;
1929 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00001930 }
1931 }
hailfingerd217d122010-10-08 18:52:29 +00001932 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00001933 ret = 1;
1934 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00001935 }
stuge8ce3a3c2008-04-28 14:47:30 +00001936 }
ollie6a600992005-11-26 21:55:36 +00001937
hailfinger0459e1c2009-08-19 13:55:34 +00001938 if (verify_it) {
1939 /* Work around chips which need some time to calm down. */
1940 if (write_it)
1941 programmer_delay(1000*1000);
hailfinger4c47e9d2010-10-19 22:06:20 +00001942 ret = verify_flash(flash, newcontents);
hailfingera50d60e2009-11-17 09:57:34 +00001943 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00001944 * might have an emergency situation.
1945 */
1946 if (ret && write_it)
1947 emergency_help_message();
1948 }
ollie6a600992005-11-26 21:55:36 +00001949
hailfinger90fcf9b2010-11-05 14:51:59 +00001950out:
1951 free(oldcontents);
1952 free(newcontents);
1953out_nofree:
hailfingerabe249e2009-05-08 17:43:22 +00001954 programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00001955 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001956}