blob: 5412362d9a99ef58d87e1963187f2c5c0b635208 [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
hailfinger969e2f32011-09-08 00:00:29 +000045static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000046
hailfingerddeb4ac2010-07-08 10:13:37 +000047static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000048
hailfinger1ff33dc2010-07-03 11:02:10 +000049/* Supported buses for the current programmer. */
50enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +000051
uwee15beb92010-08-08 17:01:18 +000052/*
hailfinger80422e22009-12-13 22:28:00 +000053 * Programmers supporting multiple buses can have differing size limits on
54 * each bus. Store the limits for each bus in a common struct.
55 */
hailfinger1ff33dc2010-07-03 11:02:10 +000056struct decode_sizes max_rom_decode;
57
58/* If nonzero, used as the start address of bottom-aligned flash. */
59unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000060
hailfinger5828baf2010-07-03 12:14:25 +000061/* Is writing allowed with this programmer? */
62int programmer_may_write;
63
hailfingerabe249e2009-05-08 17:43:22 +000064const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000065#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000066 {
hailfinger3548a9a2009-08-12 14:34:35 +000067 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000068 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000069 .map_flash_region = physmap,
70 .unmap_flash_region = physunmap,
hailfinger6c69ab02009-05-11 15:46:43 +000071 .chip_readb = internal_chip_readb,
72 .chip_readw = internal_chip_readw,
73 .chip_readl = internal_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +000074 .chip_readn = internal_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +000075 .chip_writeb = internal_chip_writeb,
76 .chip_writew = internal_chip_writew,
77 .chip_writel = internal_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +000078 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +000079 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +000080 },
hailfinger80422e22009-12-13 22:28:00 +000081#endif
stepan927d4e22007-04-04 22:45:58 +000082
hailfinger90c7d542010-05-31 15:27:27 +000083#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +000084 {
hailfinger3548a9a2009-08-12 14:34:35 +000085 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +000086 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +000087 .map_flash_region = dummy_map,
88 .unmap_flash_region = dummy_unmap,
hailfinger6c69ab02009-05-11 15:46:43 +000089 .chip_readb = dummy_chip_readb,
90 .chip_readw = dummy_chip_readw,
91 .chip_readl = dummy_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +000092 .chip_readn = dummy_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +000093 .chip_writeb = dummy_chip_writeb,
94 .chip_writew = dummy_chip_writew,
95 .chip_writel = dummy_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +000096 .chip_writen = dummy_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +000097 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +000098 },
hailfinger571a6b32009-09-16 10:09:21 +000099#endif
hailfingera9df33c2009-05-09 00:54:55 +0000100
hailfinger90c7d542010-05-31 15:27:27 +0000101#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000102 {
hailfinger3548a9a2009-08-12 14:34:35 +0000103 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000104 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000105 .map_flash_region = fallback_map,
106 .unmap_flash_region = fallback_unmap,
uwe0f5a3a22009-05-13 11:36:06 +0000107 .chip_readb = nic3com_chip_readb,
hailfinger43716942009-05-16 01:23:55 +0000108 .chip_readw = fallback_chip_readw,
109 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000110 .chip_readn = fallback_chip_readn,
uwe0f5a3a22009-05-13 11:36:06 +0000111 .chip_writeb = nic3com_chip_writeb,
hailfinger43716942009-05-16 01:23:55 +0000112 .chip_writew = fallback_chip_writew,
113 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000114 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000115 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000116 },
hailfinger571a6b32009-09-16 10:09:21 +0000117#endif
uwe0f5a3a22009-05-13 11:36:06 +0000118
hailfinger90c7d542010-05-31 15:27:27 +0000119#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000120 {
hailfinger0d703d42011-03-07 01:08:09 +0000121 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000122 .name = "nicrealtek",
123 //.name = "nicsmc1211",
124 .init = nicrealtek_init,
125 .map_flash_region = fallback_map,
126 .unmap_flash_region = fallback_unmap,
127 .chip_readb = nicrealtek_chip_readb,
128 .chip_readw = fallback_chip_readw,
129 .chip_readl = fallback_chip_readl,
130 .chip_readn = fallback_chip_readn,
131 .chip_writeb = nicrealtek_chip_writeb,
132 .chip_writew = fallback_chip_writew,
133 .chip_writel = fallback_chip_writel,
134 .chip_writen = fallback_chip_writen,
135 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000136 },
hailfinger5aa36982010-05-21 21:54:07 +0000137#endif
138
hailfingerf0a368f2010-06-07 22:37:54 +0000139#if CONFIG_NICNATSEMI == 1
140 {
uwe8d342eb2011-07-28 08:13:25 +0000141 .name = "nicnatsemi",
142 .init = nicnatsemi_init,
143 .map_flash_region = fallback_map,
144 .unmap_flash_region = fallback_unmap,
145 .chip_readb = nicnatsemi_chip_readb,
146 .chip_readw = fallback_chip_readw,
147 .chip_readl = fallback_chip_readl,
148 .chip_readn = fallback_chip_readn,
149 .chip_writeb = nicnatsemi_chip_writeb,
150 .chip_writew = fallback_chip_writew,
151 .chip_writel = fallback_chip_writel,
152 .chip_writen = fallback_chip_writen,
153 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000154 },
155#endif
hailfinger5aa36982010-05-21 21:54:07 +0000156
hailfinger90c7d542010-05-31 15:27:27 +0000157#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000158 {
159 .name = "gfxnvidia",
160 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000161 .map_flash_region = fallback_map,
162 .unmap_flash_region = fallback_unmap,
163 .chip_readb = gfxnvidia_chip_readb,
164 .chip_readw = fallback_chip_readw,
165 .chip_readl = fallback_chip_readl,
166 .chip_readn = fallback_chip_readn,
167 .chip_writeb = gfxnvidia_chip_writeb,
168 .chip_writew = fallback_chip_writew,
169 .chip_writel = fallback_chip_writel,
170 .chip_writen = fallback_chip_writen,
171 .delay = internal_delay,
172 },
173#endif
174
hailfinger90c7d542010-05-31 15:27:27 +0000175#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000176 {
uwee2f95ef2009-09-02 23:00:46 +0000177 .name = "drkaiser",
178 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000179 .map_flash_region = fallback_map,
180 .unmap_flash_region = fallback_unmap,
181 .chip_readb = drkaiser_chip_readb,
182 .chip_readw = fallback_chip_readw,
183 .chip_readl = fallback_chip_readl,
184 .chip_readn = fallback_chip_readn,
185 .chip_writeb = drkaiser_chip_writeb,
186 .chip_writew = fallback_chip_writew,
187 .chip_writel = fallback_chip_writel,
188 .chip_writen = fallback_chip_writen,
189 .delay = internal_delay,
190 },
hailfinger571a6b32009-09-16 10:09:21 +0000191#endif
uwee2f95ef2009-09-02 23:00:46 +0000192
hailfinger90c7d542010-05-31 15:27:27 +0000193#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000194 {
hailfinger3548a9a2009-08-12 14:34:35 +0000195 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000196 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000197 .map_flash_region = fallback_map,
198 .unmap_flash_region = fallback_unmap,
ruikda922a12009-05-17 19:39:27 +0000199 .chip_readb = satasii_chip_readb,
200 .chip_readw = fallback_chip_readw,
201 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000202 .chip_readn = fallback_chip_readn,
ruikda922a12009-05-17 19:39:27 +0000203 .chip_writeb = satasii_chip_writeb,
204 .chip_writew = fallback_chip_writew,
205 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000206 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000207 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000208 },
hailfinger571a6b32009-09-16 10:09:21 +0000209#endif
ruikda922a12009-05-17 19:39:27 +0000210
hailfinger90c7d542010-05-31 15:27:27 +0000211#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000212 {
213 .name = "atahpt",
214 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000215 .map_flash_region = fallback_map,
216 .unmap_flash_region = fallback_unmap,
217 .chip_readb = atahpt_chip_readb,
218 .chip_readw = fallback_chip_readw,
219 .chip_readl = fallback_chip_readl,
220 .chip_readn = fallback_chip_readn,
221 .chip_writeb = atahpt_chip_writeb,
222 .chip_writew = fallback_chip_writew,
223 .chip_writel = fallback_chip_writel,
224 .chip_writen = fallback_chip_writen,
225 .delay = internal_delay,
226 },
227#endif
228
hailfinger90c7d542010-05-31 15:27:27 +0000229#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000230 {
hailfinger90c7d542010-05-31 15:27:27 +0000231 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000232 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000233 .map_flash_region = fallback_map,
234 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000235 .chip_readb = noop_chip_readb,
hailfingerf31da3d2009-06-16 21:08:06 +0000236 .chip_readw = fallback_chip_readw,
237 .chip_readl = fallback_chip_readl,
238 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000239 .chip_writeb = noop_chip_writeb,
hailfingerf31da3d2009-06-16 21:08:06 +0000240 .chip_writew = fallback_chip_writew,
241 .chip_writel = fallback_chip_writel,
242 .chip_writen = fallback_chip_writen,
243 .delay = internal_delay,
244 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000245#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000246
hailfinger90c7d542010-05-31 15:27:27 +0000247#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000248 {
hailfinger3548a9a2009-08-12 14:34:35 +0000249 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000250 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000251 .map_flash_region = fallback_map,
252 .unmap_flash_region = fallback_unmap,
253 .chip_readb = serprog_chip_readb,
254 .chip_readw = fallback_chip_readw,
255 .chip_readl = fallback_chip_readl,
256 .chip_readn = serprog_chip_readn,
257 .chip_writeb = serprog_chip_writeb,
258 .chip_writew = fallback_chip_writew,
259 .chip_writel = fallback_chip_writel,
260 .chip_writen = fallback_chip_writen,
261 .delay = serprog_delay,
262 },
hailfinger74d88a72009-08-12 16:17:41 +0000263#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000264
hailfinger90c7d542010-05-31 15:27:27 +0000265#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000266 {
hailfinger90c7d542010-05-31 15:27:27 +0000267 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000268 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000269 .map_flash_region = fallback_map,
270 .unmap_flash_region = fallback_unmap,
271 .chip_readb = noop_chip_readb,
272 .chip_readw = fallback_chip_readw,
273 .chip_readl = fallback_chip_readl,
274 .chip_readn = fallback_chip_readn,
275 .chip_writeb = noop_chip_writeb,
276 .chip_writew = fallback_chip_writew,
277 .chip_writel = fallback_chip_writel,
278 .chip_writen = fallback_chip_writen,
279 .delay = internal_delay,
280 },
281#endif
282
hailfinger90c7d542010-05-31 15:27:27 +0000283#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000284 {
285 .name = "dediprog",
286 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000287 .map_flash_region = fallback_map,
288 .unmap_flash_region = fallback_unmap,
289 .chip_readb = noop_chip_readb,
290 .chip_readw = fallback_chip_readw,
291 .chip_readl = fallback_chip_readl,
292 .chip_readn = fallback_chip_readn,
293 .chip_writeb = noop_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
hailfinger52c4fa02010-07-21 10:26:01 +0000301#if CONFIG_RAYER_SPI == 1
302 {
303 .name = "rayer_spi",
304 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000305 .map_flash_region = fallback_map,
306 .unmap_flash_region = fallback_unmap,
307 .chip_readb = noop_chip_readb,
308 .chip_readw = fallback_chip_readw,
309 .chip_readl = fallback_chip_readl,
310 .chip_readn = fallback_chip_readn,
311 .chip_writeb = noop_chip_writeb,
312 .chip_writew = fallback_chip_writew,
313 .chip_writel = fallback_chip_writel,
314 .chip_writen = fallback_chip_writen,
315 .delay = internal_delay,
316 },
317#endif
318
hailfinger7949b652011-05-08 00:24:18 +0000319#if CONFIG_NICINTEL == 1
320 {
321 .name = "nicintel",
322 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000323 .map_flash_region = fallback_map,
324 .unmap_flash_region = fallback_unmap,
325 .chip_readb = nicintel_chip_readb,
326 .chip_readw = fallback_chip_readw,
327 .chip_readl = fallback_chip_readl,
328 .chip_readn = fallback_chip_readn,
329 .chip_writeb = nicintel_chip_writeb,
330 .chip_writew = fallback_chip_writew,
331 .chip_writel = fallback_chip_writel,
332 .chip_writen = fallback_chip_writen,
333 .delay = internal_delay,
334 },
335#endif
336
uwe6764e922010-09-03 18:21:21 +0000337#if CONFIG_NICINTEL_SPI == 1
338 {
uwe8d342eb2011-07-28 08:13:25 +0000339 .name = "nicintel_spi",
340 .init = nicintel_spi_init,
341 .map_flash_region = fallback_map,
342 .unmap_flash_region = fallback_unmap,
343 .chip_readb = noop_chip_readb,
344 .chip_readw = fallback_chip_readw,
345 .chip_readl = fallback_chip_readl,
346 .chip_readn = fallback_chip_readn,
347 .chip_writeb = noop_chip_writeb,
348 .chip_writew = fallback_chip_writew,
349 .chip_writel = fallback_chip_writel,
350 .chip_writen = fallback_chip_writen,
351 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000352 },
353#endif
354
hailfingerfb1f31f2010-12-03 14:48:11 +0000355#if CONFIG_OGP_SPI == 1
356 {
uwe8d342eb2011-07-28 08:13:25 +0000357 .name = "ogp_spi",
358 .init = ogp_spi_init,
359 .map_flash_region = fallback_map,
360 .unmap_flash_region = fallback_unmap,
361 .chip_readb = noop_chip_readb,
362 .chip_readw = fallback_chip_readw,
363 .chip_readl = fallback_chip_readl,
364 .chip_readn = fallback_chip_readn,
365 .chip_writeb = noop_chip_writeb,
366 .chip_writew = fallback_chip_writew,
367 .chip_writel = fallback_chip_writel,
368 .chip_writen = fallback_chip_writen,
369 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000370 },
371#endif
372
hailfinger935365d2011-02-04 21:37:59 +0000373#if CONFIG_SATAMV == 1
374 {
375 .name = "satamv",
376 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000377 .map_flash_region = fallback_map,
378 .unmap_flash_region = fallback_unmap,
379 .chip_readb = satamv_chip_readb,
380 .chip_readw = fallback_chip_readw,
381 .chip_readl = fallback_chip_readl,
382 .chip_readn = fallback_chip_readn,
383 .chip_writeb = satamv_chip_writeb,
384 .chip_writew = fallback_chip_writew,
385 .chip_writel = fallback_chip_writel,
386 .chip_writen = fallback_chip_writen,
387 .delay = internal_delay,
388 },
389#endif
390
uwe7df6dda2011-09-03 18:37:52 +0000391#if CONFIG_LINUX_SPI == 1
392 {
393 .name = "linux_spi",
394 .init = linux_spi_init,
395 .map_flash_region = fallback_map,
396 .unmap_flash_region = fallback_unmap,
397 .chip_readb = noop_chip_readb,
398 .chip_readw = fallback_chip_readw,
399 .chip_readl = fallback_chip_readl,
400 .chip_readn = fallback_chip_readn,
401 .chip_writeb = noop_chip_writeb,
402 .chip_writew = fallback_chip_writew,
403 .chip_writel = fallback_chip_writel,
404 .chip_writen = fallback_chip_writen,
405 .delay = internal_delay,
406 },
407#endif
408
hailfinger3548a9a2009-08-12 14:34:35 +0000409 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000410};
stepan927d4e22007-04-04 22:45:58 +0000411
David Hendricksbf36f092010-11-02 23:39:29 -0700412#define CHIP_RESTORE_MAXFN 4
413static int chip_restore_fn_count = 0;
414struct chip_restore_func_data {
415 CHIP_RESTORE_CALLBACK;
416 struct flashchip *flash;
417 uint8_t status;
418} static chip_restore_fn[CHIP_RESTORE_MAXFN];
419
David Hendricks668f29d2011-01-27 18:51:45 -0800420
hailfingerf31cbdc2010-11-10 15:25:18 +0000421#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000422static int shutdown_fn_count = 0;
423struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000424 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000425 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000426} static shutdown_fn[SHUTDOWN_MAXFN];
427/* Initialize to 0 to make sure nobody registers a shutdown function before
428 * programmer init.
429 */
430static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000431
stefanct569dbb62011-07-01 00:19:12 +0000432static int check_block_eraser(const struct flashchip *flash, int k, int log);
433
hailfingerdc6f7972010-02-14 01:20:28 +0000434/* Register a function to be executed on programmer shutdown.
435 * The advantage over atexit() is that you can supply a void pointer which will
436 * be used as parameter to the registered function upon programmer shutdown.
437 * This pointer can point to arbitrary data used by said function, e.g. undo
438 * information for GPIO settings etc. If unneeded, set data=NULL.
439 * Please note that the first (void *data) belongs to the function signature of
440 * the function passed as first parameter.
441 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000442int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000443{
444 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000445 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000446 SHUTDOWN_MAXFN);
447 return 1;
448 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000449 if (!may_register_shutdown) {
450 msg_perr("Tried to register a shutdown function before "
451 "programmer init.\n");
452 return 1;
453 }
hailfingerdc6f7972010-02-14 01:20:28 +0000454 shutdown_fn[shutdown_fn_count].func = function;
455 shutdown_fn[shutdown_fn_count].data = data;
456 shutdown_fn_count++;
457
458 return 0;
459}
460
David Hendricksbf36f092010-11-02 23:39:29 -0700461//int register_chip_restore(int (*function) (void *data), void *data)
462int register_chip_restore(CHIP_RESTORE_CALLBACK,
463 struct flashchip *flash, uint8_t status)
464{
465 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
466 msg_perr("Tried to register more than %i chip restore"
467 " functions.\n", CHIP_RESTORE_MAXFN);
468 return 1;
469 }
470 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
471 chip_restore_fn[chip_restore_fn_count].flash = flash;
472 chip_restore_fn[chip_restore_fn_count].status = status;
473 chip_restore_fn_count++;
474
475 return 0;
476}
477
hailfinger969e2f32011-09-08 00:00:29 +0000478int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000479{
hailfinger1ef766d2010-07-06 09:55:48 +0000480 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000481
482 if (prog >= PROGRAMMER_INVALID) {
483 msg_perr("Invalid programmer specified!\n");
484 return -1;
485 }
486 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000487 /* Initialize all programmer specific data. */
488 /* Default to unlimited decode sizes. */
489 max_rom_decode = (const struct decode_sizes) {
490 .parallel = 0xffffffff,
491 .lpc = 0xffffffff,
492 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000493 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000494 };
hailfingere1e41ea2011-07-27 07:13:06 +0000495 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000496 /* Default to top aligned flash at 4 GB. */
497 flashbase = 0;
498 /* Registering shutdown functions is now allowed. */
499 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000500 /* Default to allowing writes. Broken programmers set this to 0. */
501 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000502
503 programmer_param = param;
504 msg_pdbg("Initializing %s programmer\n",
505 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000506 ret = programmer_table[programmer].init();
507 if (programmer_param && strlen(programmer_param)) {
508 msg_perr("Unhandled programmer parameters: %s\n",
509 programmer_param);
510 /* Do not error out here, the init itself was successful. */
511 }
512 return ret;
uweabe92a52009-05-16 22:36:00 +0000513}
514
David Hendricksbf36f092010-11-02 23:39:29 -0700515int chip_restore()
516{
517 int rc = 0;
518
519 while (chip_restore_fn_count > 0) {
520 int i = --chip_restore_fn_count;
521 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
522 chip_restore_fn[i].status);
523 }
524
525 return rc;
526}
527
uweabe92a52009-05-16 22:36:00 +0000528int programmer_shutdown(void)
529{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000530 int ret = 0;
531
hailfinger1ff33dc2010-07-03 11:02:10 +0000532 /* Registering shutdown functions is no longer allowed. */
533 may_register_shutdown = 0;
534 while (shutdown_fn_count > 0) {
535 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000536 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000537 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000538 return ret;
uweabe92a52009-05-16 22:36:00 +0000539}
540
541void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
542 size_t len)
543{
544 return programmer_table[programmer].map_flash_region(descr,
545 phys_addr, len);
546}
547
548void programmer_unmap_flash_region(void *virt_addr, size_t len)
549{
550 programmer_table[programmer].unmap_flash_region(virt_addr, len);
551}
552
553void chip_writeb(uint8_t val, chipaddr addr)
554{
555 programmer_table[programmer].chip_writeb(val, addr);
556}
557
558void chip_writew(uint16_t val, chipaddr addr)
559{
560 programmer_table[programmer].chip_writew(val, addr);
561}
562
563void chip_writel(uint32_t val, chipaddr addr)
564{
565 programmer_table[programmer].chip_writel(val, addr);
566}
567
hailfinger9d987ef2009-06-05 18:32:07 +0000568void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
569{
570 programmer_table[programmer].chip_writen(buf, addr, len);
571}
572
uweabe92a52009-05-16 22:36:00 +0000573uint8_t chip_readb(const chipaddr addr)
574{
575 return programmer_table[programmer].chip_readb(addr);
576}
577
578uint16_t chip_readw(const chipaddr addr)
579{
580 return programmer_table[programmer].chip_readw(addr);
581}
582
583uint32_t chip_readl(const chipaddr addr)
584{
585 return programmer_table[programmer].chip_readl(addr);
586}
587
hailfinger9d987ef2009-06-05 18:32:07 +0000588void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
589{
590 programmer_table[programmer].chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000591}
592
hailfingere5829f62009-06-05 17:48:08 +0000593void programmer_delay(int usecs)
594{
595 programmer_table[programmer].delay(usecs);
596}
597
stuge5ff0e6c2009-01-26 00:39:57 +0000598void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000599{
stepan15e64bc2007-05-24 08:48:10 +0000600 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000601 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000602 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000603 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000604}
605
hailfinger0f08b7a2009-06-16 08:55:44 +0000606int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger23060112009-05-08 12:49:03 +0000607{
hailfinger0f08b7a2009-06-16 08:55:44 +0000608 chip_readn(buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000609
hailfinger23060112009-05-08 12:49:03 +0000610 return 0;
611}
612
hailfinger7b414742009-06-13 12:04:03 +0000613int min(int a, int b)
614{
615 return (a < b) ? a : b;
616}
617
hailfinger7af83692009-06-15 17:23:36 +0000618int max(int a, int b)
619{
620 return (a > b) ? a : b;
621}
622
hailfingeraec9c962009-10-31 01:53:09 +0000623int bitcount(unsigned long a)
624{
625 int i = 0;
626 for (; a != 0; a >>= 1)
627 if (a & 1)
628 i++;
629 return i;
630}
631
hailfingerf76cc322010-11-09 22:00:31 +0000632void tolower_string(char *str)
633{
634 for (; *str != '\0'; str++)
635 *str = (char)tolower((unsigned char)*str);
636}
637
hailfingera916b422009-06-01 02:08:58 +0000638char *strcat_realloc(char *dest, const char *src)
639{
640 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000641 if (!dest) {
642 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000643 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000644 }
hailfingera916b422009-06-01 02:08:58 +0000645 strcat(dest, src);
646 return dest;
647}
648
hailfinger6e5a52a2009-11-24 18:27:10 +0000649/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000650 * It will look for needle with a subsequent '=' in haystack, return a copy of
651 * needle and remove everything from the first occurrence of needle to the next
652 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000653 */
stefanct52700282011-06-26 17:38:17 +0000654char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000655{
hailfinger1ef766d2010-07-06 09:55:48 +0000656 char *param_pos, *opt_pos, *rest;
657 char *opt = NULL;
658 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000659 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000660
hailfingerf4aaccc2010-04-28 15:22:14 +0000661 needlelen = strlen(needle);
662 if (!needlelen) {
663 msg_gerr("%s: empty needle! Please report a bug at "
664 "flashrom@flashrom.org\n", __func__);
665 return NULL;
666 }
667 /* No programmer parameters given. */
668 if (*haystack == NULL)
669 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000670 param_pos = strstr(*haystack, needle);
671 do {
672 if (!param_pos)
673 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000674 /* Needle followed by '='? */
675 if (param_pos[needlelen] == '=') {
676
677 /* Beginning of the string? */
678 if (param_pos == *haystack)
679 break;
680 /* After a delimiter? */
681 if (strchr(delim, *(param_pos - 1)))
682 break;
683 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000684 /* Continue searching. */
685 param_pos++;
686 param_pos = strstr(param_pos, needle);
687 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000688
hailfinger6e5a52a2009-11-24 18:27:10 +0000689 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000690 /* Get the string after needle and '='. */
691 opt_pos = param_pos + needlelen + 1;
692 optlen = strcspn(opt_pos, delim);
693 /* Return an empty string if the parameter was empty. */
694 opt = malloc(optlen + 1);
695 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000696 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000697 exit(1);
698 }
hailfinger1ef766d2010-07-06 09:55:48 +0000699 strncpy(opt, opt_pos, optlen);
700 opt[optlen] = '\0';
701 rest = opt_pos + optlen;
702 /* Skip all delimiters after the current parameter. */
703 rest += strspn(rest, delim);
704 memmove(param_pos, rest, strlen(rest) + 1);
705 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000706 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000707
hailfinger1ef766d2010-07-06 09:55:48 +0000708 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000709}
710
stefanct52700282011-06-26 17:38:17 +0000711char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000712{
713 return extract_param(&programmer_param, param_name, ",");
714}
715
stefancte1c5acf2011-07-04 07:27:17 +0000716/* Returns the number of well-defined erasers for a chip. */
717static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000718{
719 unsigned int usable_erasefunctions = 0;
720 int k;
721 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
722 if (!check_block_eraser(flash, k, 0))
723 usable_erasefunctions++;
724 }
725 return usable_erasefunctions;
726}
727
hailfinger7af83692009-06-15 17:23:36 +0000728/* start is an offset to the base address of the flash chip */
729int check_erased_range(struct flashchip *flash, int start, int len)
730{
731 int ret;
732 uint8_t *cmpbuf = malloc(len);
733
734 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000735 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000736 exit(1);
737 }
738 memset(cmpbuf, 0xff, len);
739 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
740 free(cmpbuf);
741 return ret;
742}
743
uwee15beb92010-08-08 17:01:18 +0000744/*
hailfinger7af3d192009-11-25 17:05:52 +0000745 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000746 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000747 * @start offset to the base address of the flash chip
748 * @len length of the verified area
749 * @message string to print in the "FAILED" message
750 * @return 0 for success, -1 for failure
751 */
uwe8d342eb2011-07-28 08:13:25 +0000752int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len,
753 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000754{
hailfingerb91c08c2011-08-15 19:54:20 +0000755 int i;
hailfinger8cb6ece2010-11-16 17:21:58 +0000756 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000757 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000758
759 if (!len)
760 goto out_free;
761
hailfingerb0f4d122009-06-24 08:20:45 +0000762 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000763 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000764 return 1;
765 }
hailfinger7af83692009-06-15 17:23:36 +0000766 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000767 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000768 exit(1);
769 }
770
771 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000772 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000773 " total_size 0x%x\n", __func__, start, len,
774 flash->total_size * 1024);
775 ret = -1;
776 goto out_free;
777 }
778 if (!message)
779 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000780
hailfinger8cb6ece2010-11-16 17:21:58 +0000781 ret = flash->read(flash, readbuf, start, len);
782 if (ret) {
783 msg_gerr("Verification impossible because read failed "
784 "at 0x%x (len 0x%x)\n", start, len);
785 return ret;
786 }
787
788 for (i = 0; i < len; i++) {
789 if (cmpbuf[i] != readbuf[i]) {
790 /* Only print the first failure. */
791 if (!failcount++)
792 msg_cerr("%s FAILED at 0x%08x! "
793 "Expected=0x%02x, Read=0x%02x,",
794 message, start + i, cmpbuf[i],
795 readbuf[i]);
hailfinger7af83692009-06-15 17:23:36 +0000796 }
797 }
hailfinger5be6c0f2009-07-23 01:42:56 +0000798 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000799 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000800 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000801 ret = -1;
802 }
hailfinger7af83692009-06-15 17:23:36 +0000803
804out_free:
805 free(readbuf);
806 return ret;
807}
808
uwee15beb92010-08-08 17:01:18 +0000809/*
hailfingerb247c7a2010-03-08 00:42:32 +0000810 * Check if the buffer @have can be programmed to the content of @want without
811 * erasing. This is only possible if all chunks of size @gran are either kept
812 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000813 *
hailfingerb247c7a2010-03-08 00:42:32 +0000814 * The following write granularities (enum @gran) are known:
815 * - 1 bit. Each bit can be cleared individually.
816 * - 1 byte. A byte can be written once. Further writes to an already written
817 * byte cause the contents to be either undefined or to stay unchanged.
818 * - 128 bytes. If less than 128 bytes are written, the rest will be
819 * erased. Each write to a 128-byte region will trigger an automatic erase
820 * before anything is written. Very uncommon behaviour and unsupported by
821 * this function.
822 * - 256 bytes. If less than 256 bytes are written, the contents of the
823 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000824 * Warning: This function assumes that @have and @want point to naturally
825 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000826 *
827 * @have buffer with current content
828 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000829 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000830 * @gran write granularity (enum, not count)
831 * @return 0 if no erase is needed, 1 otherwise
832 */
833int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
834{
hailfingerb91c08c2011-08-15 19:54:20 +0000835 int result = 0;
836 int i, j, limit;
hailfingerb247c7a2010-03-08 00:42:32 +0000837
838 switch (gran) {
839 case write_gran_1bit:
840 for (i = 0; i < len; i++)
841 if ((have[i] & want[i]) != want[i]) {
842 result = 1;
843 break;
844 }
845 break;
846 case write_gran_1byte:
847 for (i = 0; i < len; i++)
848 if ((have[i] != want[i]) && (have[i] != 0xff)) {
849 result = 1;
850 break;
851 }
852 break;
853 case write_gran_256bytes:
854 for (j = 0; j < len / 256; j++) {
855 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000856 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000857 if (!memcmp(have + j * 256, want + j * 256, limit))
858 continue;
859 /* have needs to be in erased state. */
860 for (i = 0; i < limit; i++)
hailfingerb437e282010-11-04 01:04:27 +0000861 if (have[j * 256 + i] != 0xff) {
hailfingerb247c7a2010-03-08 00:42:32 +0000862 result = 1;
863 break;
864 }
865 if (result)
866 break;
867 }
868 break;
hailfingerb437e282010-11-04 01:04:27 +0000869 default:
870 msg_cerr("%s: Unsupported granularity! Please report a bug at "
871 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000872 }
873 return result;
874}
875
hailfingerb437e282010-11-04 01:04:27 +0000876/**
877 * Check if the buffer @have needs to be programmed to get the content of @want.
878 * If yes, return 1 and fill in first_start with the start address of the
879 * write operation and first_len with the length of the first to-be-written
880 * chunk. If not, return 0 and leave first_start and first_len undefined.
881 *
882 * Warning: This function assumes that @have and @want point to naturally
883 * aligned regions.
884 *
885 * @have buffer with current content
886 * @want buffer with desired content
887 * @len length of the checked area
888 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000889 * @first_start offset of the first byte which needs to be written (passed in
890 * value is increased by the offset of the first needed write
891 * relative to have/want or unchanged if no write is needed)
892 * @return length of the first contiguous area which needs to be written
893 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000894 *
895 * FIXME: This function needs a parameter which tells it about coalescing
896 * in relation to the max write length of the programmer and the max write
897 * length of the chip.
898 */
899static int get_next_write(uint8_t *have, uint8_t *want, int len,
hailfinger90fcf9b2010-11-05 14:51:59 +0000900 int *first_start, enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000901{
hailfingerb91c08c2011-08-15 19:54:20 +0000902 int need_write = 0, rel_start = 0, first_len = 0;
903 int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000904
hailfingerb437e282010-11-04 01:04:27 +0000905 switch (gran) {
906 case write_gran_1bit:
907 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000908 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000909 break;
910 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000911 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000912 break;
913 default:
914 msg_cerr("%s: Unsupported granularity! Please report a bug at "
915 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000916 /* Claim that no write was needed. A write with unknown
917 * granularity is too dangerous to try.
918 */
919 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000920 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000921 for (i = 0; i < len / stride; i++) {
922 limit = min(stride, len - i * stride);
923 /* Are 'have' and 'want' identical? */
924 if (memcmp(have + i * stride, want + i * stride, limit)) {
925 if (!need_write) {
926 /* First location where have and want differ. */
927 need_write = 1;
928 rel_start = i * stride;
929 }
930 } else {
931 if (need_write) {
932 /* First location where have and want
933 * do not differ anymore.
934 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000935 break;
936 }
937 }
938 }
hailfingerffb7f382010-12-06 13:05:44 +0000939 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000940 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000941 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000942 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000943}
944
hailfinger0c515352009-11-23 12:55:31 +0000945/* This function generates various test patterns useful for testing controller
946 * and chip communication as well as chip behaviour.
947 *
948 * If a byte can be written multiple times, each time keeping 0-bits at 0
949 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
950 * is essentially an AND operation. That's also the reason why this function
951 * provides the result of AND between various patterns.
952 *
953 * Below is a list of patterns (and their block length).
954 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
955 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
956 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
957 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
958 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
959 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
960 * Pattern 6 is 00 (1 Byte)
961 * Pattern 7 is ff (1 Byte)
962 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
963 * byte block.
964 *
965 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
966 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
967 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
968 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
969 * Pattern 12 is 00 (1 Byte)
970 * Pattern 13 is ff (1 Byte)
971 * Patterns 8-13 have no block number.
972 *
973 * Patterns 0-3 are created to detect and efficiently diagnose communication
974 * slips like missed bits or bytes and their repetitive nature gives good visual
975 * cues to the person inspecting the results. In addition, the following holds:
976 * AND Pattern 0/1 == Pattern 4
977 * AND Pattern 2/3 == Pattern 5
978 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
979 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
980 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
981 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
982 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
983 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
984 * Besides that, they provide for bit testing of the last two bytes of every
985 * 256 byte block which contains the block number for patterns 0-6.
986 * Patterns 10-11 are special purpose for detecting subblock aliasing with
987 * block sizes >256 bytes (some Dataflash chips etc.)
988 * AND Pattern 8/9 == Pattern 12
989 * AND Pattern 10/11 == Pattern 12
990 * Pattern 13 is the completely erased state.
991 * None of the patterns can detect aliasing at boundaries which are a multiple
992 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
993 */
994int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
995{
996 int i;
997
998 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000999 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +00001000 return 1;
1001 }
1002
1003 switch (variant) {
1004 case 0:
1005 for (i = 0; i < size; i++)
1006 buf[i] = (i & 0xf) << 4 | 0x5;
1007 break;
1008 case 1:
1009 for (i = 0; i < size; i++)
1010 buf[i] = (i & 0xf) << 4 | 0xa;
1011 break;
1012 case 2:
1013 for (i = 0; i < size; i++)
1014 buf[i] = 0x50 | (i & 0xf);
1015 break;
1016 case 3:
1017 for (i = 0; i < size; i++)
1018 buf[i] = 0xa0 | (i & 0xf);
1019 break;
1020 case 4:
1021 for (i = 0; i < size; i++)
1022 buf[i] = (i & 0xf) << 4;
1023 break;
1024 case 5:
1025 for (i = 0; i < size; i++)
1026 buf[i] = i & 0xf;
1027 break;
1028 case 6:
1029 memset(buf, 0x00, size);
1030 break;
1031 case 7:
1032 memset(buf, 0xff, size);
1033 break;
1034 case 8:
1035 for (i = 0; i < size; i++)
1036 buf[i] = i & 0xff;
1037 break;
1038 case 9:
1039 for (i = 0; i < size; i++)
1040 buf[i] = ~(i & 0xff);
1041 break;
1042 case 10:
1043 for (i = 0; i < size % 2; i++) {
1044 buf[i * 2] = (i >> 8) & 0xff;
1045 buf[i * 2 + 1] = i & 0xff;
1046 }
1047 if (size & 0x1)
1048 buf[i * 2] = (i >> 8) & 0xff;
1049 break;
1050 case 11:
1051 for (i = 0; i < size % 2; i++) {
1052 buf[i * 2] = ~((i >> 8) & 0xff);
1053 buf[i * 2 + 1] = ~(i & 0xff);
1054 }
1055 if (size & 0x1)
1056 buf[i * 2] = ~((i >> 8) & 0xff);
1057 break;
1058 case 12:
1059 memset(buf, 0x00, size);
1060 break;
1061 case 13:
1062 memset(buf, 0xff, size);
1063 break;
1064 }
1065
1066 if ((variant >= 0) && (variant <= 7)) {
1067 /* Write block number in the last two bytes of each 256-byte
1068 * block, big endian for easier reading of the hexdump.
1069 * Note that this wraps around for chips larger than 2^24 bytes
1070 * (16 MB).
1071 */
1072 for (i = 0; i < size / 256; i++) {
1073 buf[i * 256 + 254] = (i >> 8) & 0xff;
1074 buf[i * 256 + 255] = i & 0xff;
1075 }
1076 }
1077
1078 return 0;
1079}
1080
hailfingeraec9c962009-10-31 01:53:09 +00001081int check_max_decode(enum chipbustype buses, uint32_t size)
1082{
1083 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001084
1085 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001086 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001087 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001088 "size %u kB of chipset/board/programmer "
1089 "for %s interface, "
1090 "probe/read/erase/write may fail. ", size / 1024,
1091 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001092 }
hailfingere1e41ea2011-07-27 07:13:06 +00001093 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001094 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001095 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001096 "size %u kB of chipset/board/programmer "
1097 "for %s interface, "
1098 "probe/read/erase/write may fail. ", size / 1024,
1099 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001100 }
hailfingere1e41ea2011-07-27 07:13:06 +00001101 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001102 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001103 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001104 "size %u kB of chipset/board/programmer "
1105 "for %s interface, "
1106 "probe/read/erase/write may fail. ", size / 1024,
1107 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001108 }
hailfingere1e41ea2011-07-27 07:13:06 +00001109 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001110 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001111 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001112 "size %u kB of chipset/board/programmer "
1113 "for %s interface, "
1114 "probe/read/erase/write may fail. ", size / 1024,
1115 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001116 }
1117 if (!limitexceeded)
1118 return 0;
1119 /* Sometimes chip and programmer have more than one bus in common,
1120 * and the limit is not exceeded on all buses. Tell the user.
1121 */
1122 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001123 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001124 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001125 "interface which can support a chip of this size. "
1126 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001127 return 1;
1128}
1129
hailfinger48ed3e22011-05-04 00:39:50 +00001130int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001131{
hailfinger48ed3e22011-05-04 00:39:50 +00001132 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001133 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001134 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001135 uint32_t size;
1136 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001137 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001138
hailfinger48ed3e22011-05-04 00:39:50 +00001139 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001140 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001141 continue;
hailfingeraec9c962009-10-31 01:53:09 +00001142 buses_common = buses_supported & flash->bustype;
1143 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001144 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1145 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +00001146 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +00001147 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001148 free(tmp);
1149 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001150 msg_gspew("and chip bus type %s are incompatible.",
1151 tmp);
hailfingera916b422009-06-01 02:08:58 +00001152 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001153 msg_gspew("\n");
1154 continue;
1155 }
1156 msg_gdbg("Probing for %s %s, %d kB: ",
1157 flash->vendor, flash->name, flash->total_size);
1158 if (!flash->probe && !force) {
1159 msg_gdbg("failed! flashrom has no probe function for "
1160 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001161 continue;
1162 }
stepan782fb172007-04-06 11:58:03 +00001163
ollie5672ac62004-03-17 22:22:08 +00001164 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001165 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001166
hailfinger48ed3e22011-05-04 00:39:50 +00001167 /* Start filling in the dynamic data. */
1168 *fill_flash = *flash;
1169
hailfinger72d3b982009-05-09 07:27:23 +00001170 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001171 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001172
stugec1e55fe2008-07-02 17:15:47 +00001173 if (force)
1174 break;
stepanc98b80b2006-03-16 16:57:41 +00001175
hailfinger48ed3e22011-05-04 00:39:50 +00001176 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001177 goto notfound;
1178
hailfinger48ed3e22011-05-04 00:39:50 +00001179 /* If this is the first chip found, accept it.
1180 * If this is not the first chip found, accept it only if it is
1181 * a non-generic match.
1182 * We could either make chipcount global or provide it as
1183 * parameter, or we assume that startchip==0 means this call to
1184 * probe_flash() is the first one and thus no chip has been
1185 * found before.
1186 */
1187 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001188 break;
1189
stuge56300c32008-09-03 23:10:05 +00001190notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001191 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001192 }
uwebe4477b2007-08-23 16:08:21 +00001193
stugec1e55fe2008-07-02 17:15:47 +00001194 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001195 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001196
hailfingere11396b2011-03-08 00:09:11 +00001197#if CONFIG_INTERNAL == 1
1198 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001199 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001200 else
1201#endif
stepan3e7aeae2011-01-19 06:21:54 +00001202 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001203
stefanct588b6d22011-06-26 20:45:35 +00001204 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001205 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1206 force ? "Assuming" : "Found", fill_flash->vendor,
1207 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001208 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001209
hailfinger0f4c3952010-12-02 21:59:42 +00001210 /* Flash registers will not be mapped if the chip was forced. Lock info
1211 * may be stored in registers, so avoid lock info printing.
1212 */
1213 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001214 if (fill_flash->printlock)
1215 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001216
hailfinger48ed3e22011-05-04 00:39:50 +00001217 /* Return position of matching chip. */
1218 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001219}
1220
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001221int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001222{
hailfingerb0f4d122009-06-24 08:20:45 +00001223 int ret;
ollie5b621572004-03-20 16:46:10 +00001224 int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001225
snelsone42c3802010-05-07 20:09:04 +00001226 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001227
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001228 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1229 ret = handle_partial_verify(flash, buf, verify_range);
1230 } else {
1231 ret = verify_range(flash, buf, 0, total_size, NULL);
1232 }
uwef6641642007-05-09 10:17:44 +00001233
hailfingerb0f4d122009-06-24 08:20:45 +00001234 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001235 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001236
hailfingerb0f4d122009-06-24 08:20:45 +00001237 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001238}
1239
uwe8d342eb2011-07-28 08:13:25 +00001240int read_buf_from_file(unsigned char *buf, unsigned long size,
1241 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001242{
1243 unsigned long numbytes;
1244 FILE *image;
1245 struct stat image_stat;
1246
1247 if ((image = fopen(filename, "rb")) == NULL) {
1248 perror(filename);
1249 return 1;
1250 }
1251 if (fstat(fileno(image), &image_stat) != 0) {
1252 perror(filename);
1253 fclose(image);
1254 return 1;
1255 }
1256 if (image_stat.st_size != size) {
1257 msg_gerr("Error: Image size doesn't match\n");
1258 fclose(image);
1259 return 1;
1260 }
1261 numbytes = fread(buf, 1, size, image);
1262 if (fclose(image)) {
1263 perror(filename);
1264 return 1;
1265 }
1266 if (numbytes != size) {
1267 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1268 "wanted %ld!\n", numbytes, size);
1269 return 1;
1270 }
1271 return 0;
1272}
1273
uwe8d342eb2011-07-28 08:13:25 +00001274int write_buf_to_file(unsigned char *buf, unsigned long size,
1275 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001276{
1277 unsigned long numbytes;
1278 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001279
1280 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001281 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001282 return 1;
1283 }
oxygenebf70d352010-01-25 22:55:33 +00001284 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001285 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001286 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001287 }
hailfingerd219a232009-01-28 00:27:54 +00001288
hailfingerd219a232009-01-28 00:27:54 +00001289 numbytes = fwrite(buf, 1, size, image);
1290 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001291 if (numbytes != size) {
1292 msg_gerr("File %s could not be written completely.\n",
1293 filename);
hailfingerd219a232009-01-28 00:27:54 +00001294 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001295 }
hailfingerd219a232009-01-28 00:27:54 +00001296 return 0;
1297}
1298
stefanct52700282011-06-26 17:38:17 +00001299int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001300{
1301 unsigned long size = flash->total_size * 1024;
1302 unsigned char *buf = calloc(size, sizeof(char));
1303 int ret = 0;
1304
1305 msg_cinfo("Reading flash... ");
1306 if (!buf) {
1307 msg_gerr("Memory allocation failed!\n");
1308 msg_cinfo("FAILED.\n");
1309 return 1;
1310 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001311
1312 /* To support partial read, fill buffer to all 0xFF at beginning to make
1313 * debug easier. */
1314 memset(buf, 0xFF, size);
1315
hailfinger42a850a2010-07-13 23:56:13 +00001316 if (!flash->read) {
1317 msg_cerr("No read function available for this flash chip.\n");
1318 ret = 1;
1319 goto out_free;
1320 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001321
1322 /* First try to handle partial read case, rather than read the whole
1323 * flash, which is slow. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001324 ret = handle_partial_read(flash, buf, flash->read, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001325 if (ret < 0) {
1326 msg_cerr("Partial read operation failed!\n");
1327 ret = 1;
1328 goto out_free;
1329 } else if (ret > 0) {
1330 /* Partial read has been handled, pass the whole flash read. */
1331 } else if (flash->read(flash, buf, 0, size)) {
hailfinger42a850a2010-07-13 23:56:13 +00001332 msg_cerr("Read operation failed!\n");
1333 ret = 1;
1334 goto out_free;
1335 }
1336
stefanct9e6b98a2011-05-28 02:37:14 +00001337 ret = write_buf_to_file(buf, size, filename);
hailfinger42a850a2010-07-13 23:56:13 +00001338out_free:
1339 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001340 if (ret)
1341 msg_cerr("FAILED.");
1342 else
1343 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001344 return ret;
1345}
1346
hailfingerb437e282010-11-04 01:04:27 +00001347/* This function shares a lot of its structure with erase_and_write_flash() and
1348 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001349 * Even if an error is found, the function will keep going and check the rest.
1350 */
hailfinger48ed3e22011-05-04 00:39:50 +00001351static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001352{
hailfingerb91c08c2011-08-15 19:54:20 +00001353 int i, j, k;
1354 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001355
1356 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1357 unsigned int done = 0;
1358 struct block_eraser eraser = flash->block_erasers[k];
1359
1360 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1361 /* Blocks with zero size are bugs in flashchips.c. */
1362 if (eraser.eraseblocks[i].count &&
1363 !eraser.eraseblocks[i].size) {
1364 msg_gerr("ERROR: Flash chip %s erase function "
1365 "%i region %i has size 0. Please report"
1366 " a bug at flashrom@flashrom.org\n",
1367 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001368 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001369 }
1370 /* Blocks with zero count are bugs in flashchips.c. */
1371 if (!eraser.eraseblocks[i].count &&
1372 eraser.eraseblocks[i].size) {
1373 msg_gerr("ERROR: Flash chip %s erase function "
1374 "%i region %i has count 0. Please report"
1375 " a bug at flashrom@flashrom.org\n",
1376 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001377 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001378 }
1379 done += eraser.eraseblocks[i].count *
1380 eraser.eraseblocks[i].size;
1381 }
hailfinger9fed35d2010-01-19 06:42:46 +00001382 /* Empty eraseblock definition with erase function. */
1383 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001384 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001385 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001386 if (!done)
1387 continue;
1388 if (done != flash->total_size * 1024) {
1389 msg_gerr("ERROR: Flash chip %s erase function %i "
1390 "region walking resulted in 0x%06x bytes total,"
1391 " expected 0x%06x bytes. Please report a bug at"
1392 " flashrom@flashrom.org\n", flash->name, k,
1393 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001394 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001395 }
hailfinger9fed35d2010-01-19 06:42:46 +00001396 if (!eraser.block_erase)
1397 continue;
1398 /* Check if there are identical erase functions for different
1399 * layouts. That would imply "magic" erase functions. The
1400 * easiest way to check this is with function pointers.
1401 */
uwef6f94d42010-03-13 17:28:29 +00001402 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001403 if (eraser.block_erase ==
1404 flash->block_erasers[j].block_erase) {
1405 msg_gerr("ERROR: Flash chip %s erase function "
1406 "%i and %i are identical. Please report"
1407 " a bug at flashrom@flashrom.org\n",
1408 flash->name, k, j);
1409 ret = 1;
1410 }
uwef6f94d42010-03-13 17:28:29 +00001411 }
hailfinger45177872010-01-18 08:14:43 +00001412 }
hailfinger9fed35d2010-01-19 06:42:46 +00001413 return ret;
hailfinger45177872010-01-18 08:14:43 +00001414}
1415
hailfingerb437e282010-11-04 01:04:27 +00001416static int erase_and_write_block_helper(struct flashchip *flash,
1417 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001418 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001419 uint8_t *newcontents,
1420 int (*erasefn) (struct flashchip *flash,
1421 unsigned int addr,
1422 unsigned int len))
1423{
uwe8d342eb2011-07-28 08:13:25 +00001424 int starthere = 0, lenhere = 0, ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001425 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1426
hailfinger90fcf9b2010-11-05 14:51:59 +00001427 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001428 * need to be adjusted here to keep the impression of proper abstraction
1429 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001430 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001431 newcontents += start;
1432 msg_cdbg(":");
1433 /* FIXME: Assume 256 byte granularity for now to play it safe. */
hailfinger90fcf9b2010-11-05 14:51:59 +00001434 if (need_erase(curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001435 msg_cdbg("E");
1436 ret = erasefn(flash, start, len);
1437 if (ret)
1438 return ret;
hailfingerac8e3182011-06-26 17:04:16 +00001439 if (check_erased_range(flash, start, len)) {
1440 msg_cerr("ERASE FAILED!\n");
1441 return -1;
1442 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001443 /* Erase was successful. Adjust curcontents. */
1444 memset(curcontents, 0xff, len);
hailfingerb437e282010-11-04 01:04:27 +00001445 skip = 0;
1446 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001447 /* get_next_write() sets starthere to a new value after the call. */
1448 while ((lenhere = get_next_write(curcontents + starthere,
1449 newcontents + starthere,
1450 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001451 if (!writecount++)
1452 msg_cdbg("W");
1453 /* Needs the partial write function signature. */
1454 ret = flash->write(flash, newcontents + starthere,
1455 start + starthere, lenhere);
1456 if (ret)
1457 return ret;
1458 starthere += lenhere;
1459 skip = 0;
1460 }
1461 if (skip)
1462 msg_cdbg("S");
1463 return ret;
1464}
1465
hailfinger83541b32010-07-13 00:42:00 +00001466static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1467 int (*do_something) (struct flashchip *flash,
1468 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001469 unsigned int len,
1470 uint8_t *param1,
1471 uint8_t *param2,
1472 int (*erasefn) (
1473 struct flashchip *flash,
1474 unsigned int addr,
1475 unsigned int len)),
1476 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001477{
1478 int i, j;
hailfingerb91c08c2011-08-15 19:54:20 +00001479 unsigned int start = 0;
1480 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001481 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001482
hailfinger2b8c9382010-07-13 00:37:19 +00001483 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1484 /* count==0 for all automatically initialized array
1485 * members so the loop below won't be executed for them.
1486 */
1487 len = eraser.eraseblocks[i].size;
1488 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001489 /* Print this for every block except the first one. */
1490 if (i || j)
1491 msg_cdbg(", ");
1492 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001493 start + len - 1);
hailfingerb437e282010-11-04 01:04:27 +00001494 if (do_something(flash, start, len, param1, param2,
1495 eraser.block_erase)) {
hailfinger2b8c9382010-07-13 00:37:19 +00001496 return 1;
hailfingerb437e282010-11-04 01:04:27 +00001497 }
hailfinger2b8c9382010-07-13 00:37:19 +00001498 start += len;
1499 }
1500 }
hailfingerb437e282010-11-04 01:04:27 +00001501 msg_cdbg("\n");
hailfinger2b8c9382010-07-13 00:37:19 +00001502 return 0;
1503}
1504
stefanct569dbb62011-07-01 00:19:12 +00001505static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001506{
1507 struct block_eraser eraser = flash->block_erasers[k];
1508
1509 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1510 if (log)
1511 msg_cdbg("not defined. ");
1512 return 1;
1513 }
1514 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1515 if (log)
1516 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001517 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001518 return 1;
1519 }
1520 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1521 if (log)
1522 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001523 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001524 return 1;
1525 }
1526 return 0;
1527}
1528
uwe8d342eb2011-07-28 08:13:25 +00001529int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1530 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001531{
hailfinger8f524a82011-07-21 21:21:04 +00001532 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001533 uint8_t *curcontents;
1534 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001535 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001536
hailfingercf848f12010-12-05 15:14:44 +00001537 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001538 curcontents = malloc(size);
1539 if (!curcontents) {
1540 msg_gerr("Out of memory!\n");
1541 exit(1);
1542 }
hailfingerb437e282010-11-04 01:04:27 +00001543 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1544 memcpy(curcontents, oldcontents, size);
1545
hailfinger7df21362009-09-05 02:30:58 +00001546 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001547 if (k != 0)
1548 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001549 if (!usable_erasefunctions) {
1550 msg_cdbg("No usable erase functions left.\n");
1551 break;
1552 }
stefanctf0bc4712011-07-26 14:28:35 +00001553 msg_cdbg("Trying erase function %i... ", k);
1554 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001555 continue;
hailfingercf848f12010-12-05 15:14:44 +00001556 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001557 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1558 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001559 /* If everything is OK, don't try another erase function. */
1560 if (!ret)
1561 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001562 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001563 * contents are. If no usable erase functions remain, we can
1564 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001565 */
hailfingercf848f12010-12-05 15:14:44 +00001566 if (!usable_erasefunctions)
1567 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001568 /* Reading the whole chip may take a while, inform the user even
1569 * in non-verbose mode.
1570 */
1571 msg_cinfo("Reading current flash chip contents... ");
hailfinger6237f5e2010-12-02 02:41:55 +00001572 if (flash->read(flash, curcontents, 0, size)) {
1573 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001574 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001575 /* We have no idea about the flash chip contents, so
1576 * retrying with another erase function is pointless.
1577 */
1578 break;
1579 }
stefanctf0bc4712011-07-26 14:28:35 +00001580 msg_cinfo("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001581 }
hailfingerb437e282010-11-04 01:04:27 +00001582 /* Free the scratchpad. */
1583 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001584
hailfinger7df21362009-09-05 02:30:58 +00001585 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001586 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001587 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001588 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001589 }
1590 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001591}
1592
hailfinger4c47e9d2010-10-19 22:06:20 +00001593void nonfatal_help_message(void)
1594{
1595 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1596 "This means we have to add special support for your board, "
1597 "programmer or flash chip.\n"
1598 "Please report this on IRC at irc.freenode.net (channel "
1599 "#flashrom) or\n"
1600 "mail flashrom@flashrom.org!\n"
1601 "-------------------------------------------------------------"
1602 "------------------\n"
1603 "You may now reboot or simply leave the machine running.\n");
1604}
1605
uweb34ec9f2009-10-01 18:40:02 +00001606void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001607{
snelsone42c3802010-05-07 20:09:04 +00001608 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001609 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001610 "mail flashrom@flashrom.org with FAILED: your board name in "
1611 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001612 "-------------------------------------------------------------"
1613 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001614 "DO NOT REBOOT OR POWEROFF!\n");
1615}
1616
uwe8d342eb2011-07-28 08:13:25 +00001617/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001618void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001619{
1620 enum programmer p;
1621 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001622 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001623 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001624 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001625 }
snelsone42c3802010-05-07 20:09:04 +00001626 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001627}
1628
hailfingerf79d1712010-10-06 23:48:34 +00001629void list_programmers_linebreak(int startcol, int cols, int paren)
1630{
1631 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001632 int pnamelen;
1633 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001634 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001635 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001636
1637 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1638 pname = programmer_table[p].name;
1639 pnamelen = strlen(pname);
1640 if (remaining - pnamelen - 2 < 0) {
1641 if (firstline)
1642 firstline = 0;
1643 else
1644 printf("\n");
1645 for (i = 0; i < startcol; i++)
1646 printf(" ");
1647 remaining = cols - startcol;
1648 } else {
1649 printf(" ");
1650 remaining--;
1651 }
1652 if (paren && (p == 0)) {
1653 printf("(");
1654 remaining--;
1655 }
1656 printf("%s", pname);
1657 remaining -= pnamelen;
1658 if (p < PROGRAMMER_INVALID - 1) {
1659 printf(",");
1660 remaining--;
1661 } else {
1662 if (paren)
1663 printf(")");
1664 printf("\n");
1665 }
1666 }
1667}
1668
hailfinger3b471632010-03-27 16:36:40 +00001669void print_sysinfo(void)
1670{
David Hendricksc6c9f822010-11-03 15:07:01 -07001671 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001672#if HAVE_UTSNAME == 1
1673 struct utsname osinfo;
1674 uname(&osinfo);
1675
David Hendricksc6c9f822010-11-03 15:07:01 -07001676 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001677 osinfo.machine);
1678#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001679 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001680#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001681 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001682#if NEED_PCI == 1
1683#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001684 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001685#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001686 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001687#endif
1688#endif
1689#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001690 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001691#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001692 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001693#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001694 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001695#endif
hailfinger3b471632010-03-27 16:36:40 +00001696#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001697 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001698#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001699 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001700#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001701 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001702#endif
1703#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001704 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001705#endif
1706#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001707 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001708#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001709 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001710#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001711 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001712}
1713
uwefdeca092008-01-21 15:24:22 +00001714void print_version(void)
1715{
David Hendricksc6c9f822010-11-03 15:07:01 -07001716 /* send to stderr for chromium os */
1717 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001718 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001719}
1720
hailfinger74819ad2010-05-15 15:04:37 +00001721void print_banner(void)
1722{
1723 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001724 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001725 msg_ginfo("\n");
1726}
1727
hailfingerc77acb52009-12-24 02:15:55 +00001728int selfcheck(void)
1729{
hailfinger45177872010-01-18 08:14:43 +00001730 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001731 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001732
1733 /* Safety check. Instead of aborting after the first error, check
1734 * if more errors exist.
1735 */
hailfingerc77acb52009-12-24 02:15:55 +00001736 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001737 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001738 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001739 }
stefanct8632c922011-07-26 14:33:46 +00001740 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001741 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001742 * For 'flashchips' we check the first element to be non-null. In the
1743 * other cases there exist use cases where the first element can be
1744 * null. */
1745 if (flashchips == NULL || flashchips[0].vendor == NULL) {
1746 msg_gerr("Flashchips table miscompilation!\n");
1747 ret = 1;
1748 }
hailfinger45177872010-01-18 08:14:43 +00001749 for (flash = flashchips; flash && flash->name; flash++)
1750 if (selfcheck_eraseblocks(flash))
1751 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001752
1753#if CONFIG_INTERNAL == 1
1754 if (chipset_enables == NULL) {
1755 msg_gerr("Chipset enables table does not exist!\n");
1756 ret = 1;
1757 }
hailfinger4640bdb2011-08-31 16:19:50 +00001758 if (board_matches == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001759 msg_gerr("Board enables table does not exist!\n");
1760 ret = 1;
1761 }
1762 if (boards_known == NULL) {
1763 msg_gerr("Known boards table does not exist!\n");
1764 ret = 1;
1765 }
1766 if (laptops_known == NULL) {
1767 msg_gerr("Known laptops table does not exist!\n");
1768 ret = 1;
1769 }
uwe8d342eb2011-07-28 08:13:25 +00001770#endif
hailfinger45177872010-01-18 08:14:43 +00001771 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001772}
1773
hailfinger48ed3e22011-05-04 00:39:50 +00001774void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001775{
1776 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001777 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001778 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001779 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001780 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001781 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001782 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001783 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001784 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001785 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001786 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001787 msg_cdbg(" WRITE");
1788 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001789 }
1790 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1791 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1792 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1793 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001794 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001795 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001796 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001797 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001798 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001799 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001800 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001801 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001802 msg_cdbg(" WRITE");
1803 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001804 }
hailfinger92cd8e32010-01-07 03:24:05 +00001805 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001806 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001807 "in the latest development\n"
1808 "version of flashrom. If you are running the latest "
1809 "development version,\n"
1810 "please email a report to flashrom@flashrom.org if "
1811 "any of the above operations\n"
1812 "work correctly for you with this flash part. Please "
1813 "include the flashrom\n"
1814 "output with the additional -V option for all "
1815 "operations you tested (-V, -Vr,\n"
1816 "-Vw, -VE), and mention which mainboard or "
1817 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001818 "Please mention your board in the subject line. "
1819 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001820 }
1821}
1822
hailfinger771fc182010-10-15 00:01:14 +00001823/* FIXME: This function signature needs to be improved once doit() has a better
1824 * function signature.
1825 */
stefanct02116582011-05-18 01:30:56 +00001826int chip_safety_check(struct flashchip *flash, int force, int read_it, int write_it, int erase_it, int verify_it)
hailfinger771fc182010-10-15 00:01:14 +00001827{
1828 if (!programmer_may_write && (write_it || erase_it)) {
1829 msg_perr("Write/erase is not working yet on your programmer in "
1830 "its current configuration.\n");
1831 /* --force is the wrong approach, but it's the best we can do
1832 * until the generic programmer parameter parser is merged.
1833 */
1834 if (!force)
1835 return 1;
1836 msg_cerr("Continuing anyway.\n");
1837 }
1838
1839 if (read_it || erase_it || write_it || verify_it) {
1840 /* Everything needs read. */
1841 if (flash->tested & TEST_BAD_READ) {
1842 msg_cerr("Read is not working on this chip. ");
1843 if (!force)
1844 return 1;
1845 msg_cerr("Continuing anyway.\n");
1846 }
1847 if (!flash->read) {
1848 msg_cerr("flashrom has no read function for this "
1849 "flash chip.\n");
1850 return 1;
1851 }
1852 }
1853 if (erase_it || write_it) {
1854 /* Write needs erase. */
1855 if (flash->tested & TEST_BAD_ERASE) {
1856 msg_cerr("Erase is not working on this chip. ");
1857 if (!force)
1858 return 1;
1859 msg_cerr("Continuing anyway.\n");
1860 }
stefancte1c5acf2011-07-04 07:27:17 +00001861 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001862 msg_cerr("flashrom has no erase function for this "
1863 "flash chip.\n");
1864 return 1;
1865 }
hailfinger771fc182010-10-15 00:01:14 +00001866 }
1867 if (write_it) {
1868 if (flash->tested & TEST_BAD_WRITE) {
1869 msg_cerr("Write is not working on this chip. ");
1870 if (!force)
1871 return 1;
1872 msg_cerr("Continuing anyway.\n");
1873 }
1874 if (!flash->write) {
1875 msg_cerr("flashrom has no write function for this "
1876 "flash chip.\n");
1877 return 1;
1878 }
1879 }
1880 return 0;
1881}
1882
hailfingerc77acb52009-12-24 02:15:55 +00001883/* This function signature is horrible. We need to design a better interface,
1884 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001885 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001886 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001887int doit(struct flashchip *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it, const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001888{
hailfinger4c47e9d2010-10-19 22:06:20 +00001889 uint8_t *oldcontents;
1890 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001891 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001892 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001893
stefanct02116582011-05-18 01:30:56 +00001894 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001895 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001896 ret = 1;
1897 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001898 }
1899
hailfinger771fc182010-10-15 00:01:14 +00001900 /* Given the existence of read locks, we want to unlock for read,
1901 * erase and write.
1902 */
1903 if (flash->unlock)
1904 flash->unlock(flash);
1905
David Hendricks6d62d752011-03-07 21:20:22 -08001906 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001907 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001908 ret = 1;
1909 goto out_nofree;
1910 }
1911
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001912 /* mark entries included using -i argument as "included" if they are
1913 found in the master rom_entries list */
1914 if (process_include_args() < 0) {
1915 ret = 1;
1916 goto out_nofree;
1917 }
1918
hailfinger771fc182010-10-15 00:01:14 +00001919 if (read_it) {
1920 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001921 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001922 }
hailfingerb437e282010-11-04 01:04:27 +00001923
stefanctd611e8f2011-07-12 22:35:21 +00001924 oldcontents = malloc(size);
1925 if (!oldcontents) {
1926 msg_gerr("Out of memory!\n");
1927 exit(1);
1928 }
hailfingerb437e282010-11-04 01:04:27 +00001929 /* Assume worst case: All bits are 0. */
1930 memset(oldcontents, 0x00, size);
stefanctd611e8f2011-07-12 22:35:21 +00001931 newcontents = malloc(size);
1932 if (!newcontents) {
1933 msg_gerr("Out of memory!\n");
1934 exit(1);
1935 }
hailfingerb437e282010-11-04 01:04:27 +00001936 /* Assume best case: All bits should be 1. */
1937 memset(newcontents, 0xff, size);
1938 /* Side effect of the assumptions above: Default write action is erase
1939 * because newcontents looks like a completely erased chip, and
1940 * oldcontents being completely 0x00 means we have to erase everything
1941 * before we can write.
1942 */
1943
ollie0eb62d62004-12-08 20:10:01 +00001944 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001945 /* FIXME: Do we really want the scary warning if erase failed?
1946 * After all, after erase the chip is either blank or partially
1947 * blank or it has the old contents. A blank chip won't boot,
1948 * so if the user wanted erase and reboots afterwards, the user
1949 * knows very well that booting won't work.
1950 */
hailfingerb437e282010-11-04 01:04:27 +00001951 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001952 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001953 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001954 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001955 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001956 }
hailfinger771fc182010-10-15 00:01:14 +00001957
hailfingerd217d122010-10-08 18:52:29 +00001958 if (write_it || verify_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001959 if (read_buf_from_file(newcontents, size, filename)) {
hailfinger90fcf9b2010-11-05 14:51:59 +00001960 ret = 1;
1961 goto out;
stepan1da96c02006-11-21 23:48:51 +00001962 }
1963
hailfinger90c7d542010-05-31 15:27:27 +00001964#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001965 if (programmer == PROGRAMMER_INTERNAL)
1966 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001967#endif
ollie5672ac62004-03-17 22:22:08 +00001968 }
1969
David Hendricksc44d7a02011-10-17 11:28:43 -07001970 /* Obtain a reference image so that we can check whether regions need
1971 * to be erased and to give better diagnostics in case write fails.
hailfinger4c47e9d2010-10-19 22:06:20 +00001972 * The alternative would be to read only the regions which are to be
1973 * preserved, but in that case we might perform unneeded erase which
1974 * takes time as well.
1975 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001976 if (diff_file) {
1977 msg_cdbg("Reading old contents from file... ");
1978 if (read_buf_from_file(oldcontents, size, diff_file)) {
1979 ret = 1;
1980 msg_cdbg("FAILED.\n");
1981 goto out;
1982 }
1983 } else {
1984 msg_cdbg("Reading old contents from flash chip... ");
1985 if (flash->read(flash, oldcontents, 0, size)) {
1986 ret = 1;
1987 msg_cdbg("FAILED.\n");
1988 goto out;
1989 }
hailfinger4c47e9d2010-10-19 22:06:20 +00001990 }
stefanctf0bc4712011-07-26 14:28:35 +00001991 msg_cinfo("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00001992
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001993 // This should be moved into each flash part's code to do it
ollie6a600992005-11-26 21:55:36 +00001994 // cleanly. This does the job.
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08001995 if (handle_romentries(flash, oldcontents, newcontents)) {
1996 ret = 1;
1997 msg_cerr("Cannot handle partial read.\n");
1998 goto out;
1999 }
uwef6641642007-05-09 10:17:44 +00002000
ollie6a600992005-11-26 21:55:36 +00002001 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00002002
stuge8ce3a3c2008-04-28 14:47:30 +00002003 if (write_it) {
hailfingerb437e282010-11-04 01:04:27 +00002004 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2005 msg_cerr("Uh oh. Erase/write failed. Checking if "
2006 "anything changed.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00002007 if (!flash->read(flash, newcontents, 0, size)) {
2008 if (!memcmp(oldcontents, newcontents, size)) {
2009 msg_cinfo("Good. It seems nothing was "
2010 "changed.\n");
2011 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002012 ret = 1;
2013 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002014 }
2015 }
hailfingerd217d122010-10-08 18:52:29 +00002016 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002017 ret = 1;
2018 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002019 }
stuge8ce3a3c2008-04-28 14:47:30 +00002020 }
ollie6a600992005-11-26 21:55:36 +00002021
hailfinger0459e1c2009-08-19 13:55:34 +00002022 if (verify_it) {
2023 /* Work around chips which need some time to calm down. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002024 if (write_it && verify_it != VERIFY_PARTIAL)
hailfinger0459e1c2009-08-19 13:55:34 +00002025 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002026
2027 ret = verify_flash(flash, newcontents, verify_it);
2028
hailfingera50d60e2009-11-17 09:57:34 +00002029 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00002030 * might have an emergency situation.
2031 */
2032 if (ret && write_it)
2033 emergency_help_message();
2034 }
ollie6a600992005-11-26 21:55:36 +00002035
hailfinger90fcf9b2010-11-05 14:51:59 +00002036out:
2037 free(oldcontents);
2038 free(newcontents);
2039out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002040 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002041 /*
2042 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2043 * tree. This is because some operations, such as write protection,
2044 * requires programmer_shutdown() but does not call doit().
2045 */
2046// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002047 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002048}