blob: 4bafcc92583ccfb3addc9101e48155ba3e6560c8 [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>
David Hendricksc801adb2010-12-09 16:58:56 -080026#ifndef __LIBPAYLOAD__
27#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000028#include <sys/stat.h>
David Hendricksc801adb2010-12-09 16:58:56 -080029#endif
rminnich8d3ff912003-10-25 17:01:29 +000030#include <string.h>
31#include <stdlib.h>
David Hendricksc801adb2010-12-09 16:58:56 -080032#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"
David Hendricks82fd8ae2010-08-04 14:34:54 -070039#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000040
David Hendricks82fd8ae2010-08-04 14:34:54 -070041const char * const flashrom_version = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000042char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000043int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000044
hailfinger90c7d542010-05-31 15:27:27 +000045#if CONFIG_INTERNAL == 1
hailfinger6fe23d62009-08-12 11:39:29 +000046enum programmer programmer = PROGRAMMER_INTERNAL;
hailfinger90c7d542010-05-31 15:27:27 +000047#elif CONFIG_DUMMY == 1
hailfinger80422e22009-12-13 22:28:00 +000048enum programmer programmer = PROGRAMMER_DUMMY;
49#else
hailfingerf95c8f62010-01-10 13:28:48 +000050/* If neither internal nor dummy are selected, we must pick a sensible default.
51 * Since there is no reason to prefer a particular external programmer, we fail
52 * if more than one of them is selected. If only one is selected, it is clear
53 * that the user wants that one to become the default.
54 */
David Hendricksc801adb2010-12-09 16:58:56 -080055#if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_FT2232_SPI+CONFIG_SERPROG+CONFIG_BUSPIRATE_SPI+CONFIG_DEDIPROG+CONFIG_RAYER_SPI+CONFIG_NICINTEL_SPI > 1
hailfinger90c7d542010-05-31 15:27:27 +000056#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all programmers except one.
hailfingerf95c8f62010-01-10 13:28:48 +000057#endif
58enum programmer programmer =
hailfinger90c7d542010-05-31 15:27:27 +000059#if CONFIG_NIC3COM == 1
hailfingerf95c8f62010-01-10 13:28:48 +000060 PROGRAMMER_NIC3COM
61#endif
hailfinger90c7d542010-05-31 15:27:27 +000062#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +000063 PROGRAMMER_NICREALTEK
64 PROGRAMMER_NICREALTEK2
65#endif
hailfingerf0a368f2010-06-07 22:37:54 +000066#if CONFIG_NICNATSEMI == 1
67 PROGRAMMER_NICNATSEMI
68#endif
hailfinger90c7d542010-05-31 15:27:27 +000069#if CONFIG_GFXNVIDIA == 1
hailfingerf95c8f62010-01-10 13:28:48 +000070 PROGRAMMER_GFXNVIDIA
71#endif
hailfinger90c7d542010-05-31 15:27:27 +000072#if CONFIG_DRKAISER == 1
hailfingerf95c8f62010-01-10 13:28:48 +000073 PROGRAMMER_DRKAISER
74#endif
hailfinger90c7d542010-05-31 15:27:27 +000075#if CONFIG_SATASII == 1
hailfingerf95c8f62010-01-10 13:28:48 +000076 PROGRAMMER_SATASII
77#endif
hailfinger90c7d542010-05-31 15:27:27 +000078#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +000079 PROGRAMMER_ATAHPT
80#endif
hailfinger90c7d542010-05-31 15:27:27 +000081#if CONFIG_FT2232_SPI == 1
82 PROGRAMMER_FT2232_SPI
hailfingerf95c8f62010-01-10 13:28:48 +000083#endif
hailfinger90c7d542010-05-31 15:27:27 +000084#if CONFIG_SERPROG == 1
hailfingerf95c8f62010-01-10 13:28:48 +000085 PROGRAMMER_SERPROG
86#endif
hailfinger90c7d542010-05-31 15:27:27 +000087#if CONFIG_BUSPIRATE_SPI == 1
88 PROGRAMMER_BUSPIRATE_SPI
hailfingerf95c8f62010-01-10 13:28:48 +000089#endif
hailfinger90c7d542010-05-31 15:27:27 +000090#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +000091 PROGRAMMER_DEDIPROG
92#endif
David Hendricks82fd8ae2010-08-04 14:34:54 -070093#if CONFIG_RAYER_SPI == 1
94 PROGRAMMER_RAYER_SPI
95#endif
David Hendricksc801adb2010-12-09 16:58:56 -080096#if CONFIG_NICINTEL_SPI == 1
97 PROGRAMMER_NICINTEL_SPI
98#endif
hailfingerf95c8f62010-01-10 13:28:48 +000099;
hailfinger80422e22009-12-13 22:28:00 +0000100#endif
101
David Hendricks82fd8ae2010-08-04 14:34:54 -0700102static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +0000103
David Hendricks82fd8ae2010-08-04 14:34:54 -0700104/* Supported buses for the current programmer. */
105enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +0000106
David Hendricksc801adb2010-12-09 16:58:56 -0800107/*
hailfinger80422e22009-12-13 22:28:00 +0000108 * Programmers supporting multiple buses can have differing size limits on
109 * each bus. Store the limits for each bus in a common struct.
110 */
David Hendricks82fd8ae2010-08-04 14:34:54 -0700111struct decode_sizes max_rom_decode;
112
113/* If nonzero, used as the start address of bottom-aligned flash. */
114unsigned long flashbase;
115
116/* Is writing allowed with this programmer? */
117int programmer_may_write;
hailfinger80422e22009-12-13 22:28:00 +0000118
hailfingerabe249e2009-05-08 17:43:22 +0000119const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +0000120#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +0000121 {
hailfinger3548a9a2009-08-12 14:34:35 +0000122 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +0000123 .init = internal_init,
124 .shutdown = internal_shutdown,
hailfinger11ae3c42009-05-11 14:13:25 +0000125 .map_flash_region = physmap,
126 .unmap_flash_region = physunmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000127 .chip_readb = internal_chip_readb,
128 .chip_readw = internal_chip_readw,
129 .chip_readl = internal_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000130 .chip_readn = internal_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000131 .chip_writeb = internal_chip_writeb,
132 .chip_writew = internal_chip_writew,
133 .chip_writel = internal_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000134 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000135 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +0000136 },
hailfinger80422e22009-12-13 22:28:00 +0000137#endif
stepan927d4e22007-04-04 22:45:58 +0000138
hailfinger90c7d542010-05-31 15:27:27 +0000139#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000140 {
hailfinger3548a9a2009-08-12 14:34:35 +0000141 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000142 .init = dummy_init,
143 .shutdown = dummy_shutdown,
hailfinger11ae3c42009-05-11 14:13:25 +0000144 .map_flash_region = dummy_map,
145 .unmap_flash_region = dummy_unmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000146 .chip_readb = dummy_chip_readb,
147 .chip_readw = dummy_chip_readw,
148 .chip_readl = dummy_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000149 .chip_readn = dummy_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000150 .chip_writeb = dummy_chip_writeb,
151 .chip_writew = dummy_chip_writew,
152 .chip_writel = dummy_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000153 .chip_writen = dummy_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000154 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000155 },
hailfinger571a6b32009-09-16 10:09:21 +0000156#endif
hailfingera9df33c2009-05-09 00:54:55 +0000157
hailfinger90c7d542010-05-31 15:27:27 +0000158#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000159 {
hailfinger3548a9a2009-08-12 14:34:35 +0000160 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000161 .init = nic3com_init,
162 .shutdown = nic3com_shutdown,
uwe3e656bd2009-05-17 23:12:17 +0000163 .map_flash_region = fallback_map,
164 .unmap_flash_region = fallback_unmap,
uwe0f5a3a22009-05-13 11:36:06 +0000165 .chip_readb = nic3com_chip_readb,
hailfinger43716942009-05-16 01:23:55 +0000166 .chip_readw = fallback_chip_readw,
167 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000168 .chip_readn = fallback_chip_readn,
uwe0f5a3a22009-05-13 11:36:06 +0000169 .chip_writeb = nic3com_chip_writeb,
hailfinger43716942009-05-16 01:23:55 +0000170 .chip_writew = fallback_chip_writew,
171 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000172 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000173 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000174 },
hailfinger571a6b32009-09-16 10:09:21 +0000175#endif
uwe0f5a3a22009-05-13 11:36:06 +0000176
hailfinger90c7d542010-05-31 15:27:27 +0000177#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000178 {
179 .name = "nicrealtek",
180 .init = nicrealtek_init,
181 .shutdown = nicrealtek_shutdown,
182 .map_flash_region = fallback_map,
183 .unmap_flash_region = fallback_unmap,
184 .chip_readb = nicrealtek_chip_readb,
185 .chip_readw = fallback_chip_readw,
186 .chip_readl = fallback_chip_readl,
187 .chip_readn = fallback_chip_readn,
188 .chip_writeb = nicrealtek_chip_writeb,
189 .chip_writew = fallback_chip_writew,
190 .chip_writel = fallback_chip_writel,
191 .chip_writen = fallback_chip_writen,
192 .delay = internal_delay,
193 },
194 {
195 .name = "nicsmc1211",
196 .init = nicsmc1211_init,
197 .shutdown = nicrealtek_shutdown,
198 .map_flash_region = fallback_map,
199 .unmap_flash_region = fallback_unmap,
200 .chip_readb = nicrealtek_chip_readb,
201 .chip_readw = fallback_chip_readw,
202 .chip_readl = fallback_chip_readl,
203 .chip_readn = fallback_chip_readn,
204 .chip_writeb = nicrealtek_chip_writeb,
205 .chip_writew = fallback_chip_writew,
206 .chip_writel = fallback_chip_writel,
207 .chip_writen = fallback_chip_writen,
208 .delay = internal_delay,
209 },
210#endif
211
hailfingerf0a368f2010-06-07 22:37:54 +0000212#if CONFIG_NICNATSEMI == 1
213 {
214 .name = "nicnatsemi",
215 .init = nicnatsemi_init,
216 .shutdown = nicnatsemi_shutdown,
217 .map_flash_region = fallback_map,
218 .unmap_flash_region = fallback_unmap,
219 .chip_readb = nicnatsemi_chip_readb,
220 .chip_readw = fallback_chip_readw,
221 .chip_readl = fallback_chip_readl,
222 .chip_readn = fallback_chip_readn,
223 .chip_writeb = nicnatsemi_chip_writeb,
224 .chip_writew = fallback_chip_writew,
225 .chip_writel = fallback_chip_writel,
226 .chip_writen = fallback_chip_writen,
227 .delay = internal_delay,
228 },
229#endif
hailfinger5aa36982010-05-21 21:54:07 +0000230
hailfinger90c7d542010-05-31 15:27:27 +0000231#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000232 {
233 .name = "gfxnvidia",
234 .init = gfxnvidia_init,
235 .shutdown = gfxnvidia_shutdown,
236 .map_flash_region = fallback_map,
237 .unmap_flash_region = fallback_unmap,
238 .chip_readb = gfxnvidia_chip_readb,
239 .chip_readw = fallback_chip_readw,
240 .chip_readl = fallback_chip_readl,
241 .chip_readn = fallback_chip_readn,
242 .chip_writeb = gfxnvidia_chip_writeb,
243 .chip_writew = fallback_chip_writew,
244 .chip_writel = fallback_chip_writel,
245 .chip_writen = fallback_chip_writen,
246 .delay = internal_delay,
247 },
248#endif
249
hailfinger90c7d542010-05-31 15:27:27 +0000250#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000251 {
uwee2f95ef2009-09-02 23:00:46 +0000252 .name = "drkaiser",
253 .init = drkaiser_init,
254 .shutdown = drkaiser_shutdown,
255 .map_flash_region = fallback_map,
256 .unmap_flash_region = fallback_unmap,
257 .chip_readb = drkaiser_chip_readb,
258 .chip_readw = fallback_chip_readw,
259 .chip_readl = fallback_chip_readl,
260 .chip_readn = fallback_chip_readn,
261 .chip_writeb = drkaiser_chip_writeb,
262 .chip_writew = fallback_chip_writew,
263 .chip_writel = fallback_chip_writel,
264 .chip_writen = fallback_chip_writen,
265 .delay = internal_delay,
266 },
hailfinger571a6b32009-09-16 10:09:21 +0000267#endif
uwee2f95ef2009-09-02 23:00:46 +0000268
hailfinger90c7d542010-05-31 15:27:27 +0000269#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000270 {
hailfinger3548a9a2009-08-12 14:34:35 +0000271 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000272 .init = satasii_init,
273 .shutdown = satasii_shutdown,
uwe3e656bd2009-05-17 23:12:17 +0000274 .map_flash_region = fallback_map,
275 .unmap_flash_region = fallback_unmap,
ruikda922a12009-05-17 19:39:27 +0000276 .chip_readb = satasii_chip_readb,
277 .chip_readw = fallback_chip_readw,
278 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000279 .chip_readn = fallback_chip_readn,
ruikda922a12009-05-17 19:39:27 +0000280 .chip_writeb = satasii_chip_writeb,
281 .chip_writew = fallback_chip_writew,
282 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000283 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000284 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000285 },
hailfinger571a6b32009-09-16 10:09:21 +0000286#endif
ruikda922a12009-05-17 19:39:27 +0000287
hailfinger90c7d542010-05-31 15:27:27 +0000288#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000289 {
290 .name = "atahpt",
291 .init = atahpt_init,
292 .shutdown = atahpt_shutdown,
293 .map_flash_region = fallback_map,
294 .unmap_flash_region = fallback_unmap,
295 .chip_readb = atahpt_chip_readb,
296 .chip_readw = fallback_chip_readw,
297 .chip_readl = fallback_chip_readl,
298 .chip_readn = fallback_chip_readn,
299 .chip_writeb = atahpt_chip_writeb,
300 .chip_writew = fallback_chip_writew,
301 .chip_writel = fallback_chip_writel,
302 .chip_writen = fallback_chip_writen,
303 .delay = internal_delay,
304 },
305#endif
306
hailfinger90c7d542010-05-31 15:27:27 +0000307#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000308#if defined(__i386__) || defined(__x86_64__)
hailfinger26e212b2009-05-31 18:00:57 +0000309 {
hailfinger3548a9a2009-08-12 14:34:35 +0000310 .name = "it87spi",
hailfinger26e212b2009-05-31 18:00:57 +0000311 .init = it87spi_init,
hailfinger571a6b32009-09-16 10:09:21 +0000312 .shutdown = noop_shutdown,
hailfinger6fe23d62009-08-12 11:39:29 +0000313 .map_flash_region = fallback_map,
314 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000315 .chip_readb = noop_chip_readb,
hailfinger9d987ef2009-06-05 18:32:07 +0000316 .chip_readw = fallback_chip_readw,
317 .chip_readl = fallback_chip_readl,
318 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000319 .chip_writeb = noop_chip_writeb,
hailfinger9d987ef2009-06-05 18:32:07 +0000320 .chip_writew = fallback_chip_writew,
321 .chip_writel = fallback_chip_writel,
322 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000323 .delay = internal_delay,
hailfinger26e212b2009-05-31 18:00:57 +0000324 },
hailfinger80422e22009-12-13 22:28:00 +0000325#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000326#endif
hailfinger26e212b2009-05-31 18:00:57 +0000327
hailfinger90c7d542010-05-31 15:27:27 +0000328#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000329 {
hailfinger90c7d542010-05-31 15:27:27 +0000330 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000331 .init = ft2232_spi_init,
hailfinger571a6b32009-09-16 10:09:21 +0000332 .shutdown = noop_shutdown, /* Missing shutdown */
hailfinger6fe23d62009-08-12 11:39:29 +0000333 .map_flash_region = fallback_map,
334 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000335 .chip_readb = noop_chip_readb,
hailfingerf31da3d2009-06-16 21:08:06 +0000336 .chip_readw = fallback_chip_readw,
337 .chip_readl = fallback_chip_readl,
338 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000339 .chip_writeb = noop_chip_writeb,
hailfingerf31da3d2009-06-16 21:08:06 +0000340 .chip_writew = fallback_chip_writew,
341 .chip_writel = fallback_chip_writel,
342 .chip_writen = fallback_chip_writen,
343 .delay = internal_delay,
344 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000345#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000346
hailfinger90c7d542010-05-31 15:27:27 +0000347#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000348 {
hailfinger3548a9a2009-08-12 14:34:35 +0000349 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000350 .init = serprog_init,
351 .shutdown = serprog_shutdown,
352 .map_flash_region = fallback_map,
353 .unmap_flash_region = fallback_unmap,
354 .chip_readb = serprog_chip_readb,
355 .chip_readw = fallback_chip_readw,
356 .chip_readl = fallback_chip_readl,
357 .chip_readn = serprog_chip_readn,
358 .chip_writeb = serprog_chip_writeb,
359 .chip_writew = fallback_chip_writew,
360 .chip_writel = fallback_chip_writel,
361 .chip_writen = fallback_chip_writen,
362 .delay = serprog_delay,
363 },
hailfinger74d88a72009-08-12 16:17:41 +0000364#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000365
hailfinger90c7d542010-05-31 15:27:27 +0000366#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000367 {
hailfinger90c7d542010-05-31 15:27:27 +0000368 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000369 .init = buspirate_spi_init,
370 .shutdown = buspirate_spi_shutdown,
371 .map_flash_region = fallback_map,
372 .unmap_flash_region = fallback_unmap,
373 .chip_readb = noop_chip_readb,
374 .chip_readw = fallback_chip_readw,
375 .chip_readl = fallback_chip_readl,
376 .chip_readn = fallback_chip_readn,
377 .chip_writeb = noop_chip_writeb,
378 .chip_writew = fallback_chip_writew,
379 .chip_writel = fallback_chip_writel,
380 .chip_writen = fallback_chip_writen,
381 .delay = internal_delay,
382 },
383#endif
384
hailfinger90c7d542010-05-31 15:27:27 +0000385#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000386 {
387 .name = "dediprog",
388 .init = dediprog_init,
389 .shutdown = dediprog_shutdown,
390 .map_flash_region = fallback_map,
391 .unmap_flash_region = fallback_unmap,
392 .chip_readb = noop_chip_readb,
393 .chip_readw = fallback_chip_readw,
394 .chip_readl = fallback_chip_readl,
395 .chip_readn = fallback_chip_readn,
396 .chip_writeb = noop_chip_writeb,
397 .chip_writew = fallback_chip_writew,
398 .chip_writel = fallback_chip_writel,
399 .chip_writen = fallback_chip_writen,
400 .delay = internal_delay,
401 },
402#endif
403
David Hendricks82fd8ae2010-08-04 14:34:54 -0700404#if CONFIG_RAYER_SPI == 1
405 {
406 .name = "rayer_spi",
407 .init = rayer_spi_init,
408 .shutdown = noop_shutdown,
409 .map_flash_region = fallback_map,
410 .unmap_flash_region = fallback_unmap,
411 .chip_readb = noop_chip_readb,
412 .chip_readw = fallback_chip_readw,
413 .chip_readl = fallback_chip_readl,
414 .chip_readn = fallback_chip_readn,
415 .chip_writeb = noop_chip_writeb,
416 .chip_writew = fallback_chip_writew,
417 .chip_writel = fallback_chip_writel,
418 .chip_writen = fallback_chip_writen,
419 .delay = internal_delay,
420 },
421#endif
422
David Hendricksc801adb2010-12-09 16:58:56 -0800423#if CONFIG_NICINTEL_SPI == 1
424 {
425 .name = "nicintel_spi",
426 .init = nicintel_spi_init,
427 .shutdown = nicintel_spi_shutdown,
428 .map_flash_region = fallback_map,
429 .unmap_flash_region = fallback_unmap,
430 .chip_readb = noop_chip_readb,
431 .chip_readw = fallback_chip_readw,
432 .chip_readl = fallback_chip_readl,
433 .chip_readn = fallback_chip_readn,
434 .chip_writeb = noop_chip_writeb,
435 .chip_writew = fallback_chip_writew,
436 .chip_writel = fallback_chip_writel,
437 .chip_writen = fallback_chip_writen,
438 .delay = internal_delay,
439 },
440#endif
441
hailfinger3548a9a2009-08-12 14:34:35 +0000442 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000443};
stepan927d4e22007-04-04 22:45:58 +0000444
David Hendricksbf36f092010-11-02 23:39:29 -0700445#define CHIP_RESTORE_MAXFN 4
446static int chip_restore_fn_count = 0;
447struct chip_restore_func_data {
448 CHIP_RESTORE_CALLBACK;
449 struct flashchip *flash;
450 uint8_t status;
451} static chip_restore_fn[CHIP_RESTORE_MAXFN];
452
David Hendricksc801adb2010-12-09 16:58:56 -0800453#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000454static int shutdown_fn_count = 0;
455struct shutdown_func_data {
456 void (*func) (void *data);
457 void *data;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700458} static shutdown_fn[SHUTDOWN_MAXFN];
459/* Initialize to 0 to make sure nobody registers a shutdown function before
460 * programmer init.
461 */
462static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000463
464/* Register a function to be executed on programmer shutdown.
465 * The advantage over atexit() is that you can supply a void pointer which will
466 * be used as parameter to the registered function upon programmer shutdown.
467 * This pointer can point to arbitrary data used by said function, e.g. undo
468 * information for GPIO settings etc. If unneeded, set data=NULL.
469 * Please note that the first (void *data) belongs to the function signature of
470 * the function passed as first parameter.
471 */
472int register_shutdown(void (*function) (void *data), void *data)
473{
474 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000475 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000476 SHUTDOWN_MAXFN);
477 return 1;
478 }
David Hendricks82fd8ae2010-08-04 14:34:54 -0700479 if (!may_register_shutdown) {
480 msg_perr("Tried to register a shutdown function before "
481 "programmer init.\n");
482 return 1;
483 }
hailfingerdc6f7972010-02-14 01:20:28 +0000484 shutdown_fn[shutdown_fn_count].func = function;
485 shutdown_fn[shutdown_fn_count].data = data;
486 shutdown_fn_count++;
487
488 return 0;
489}
490
David Hendricksbf36f092010-11-02 23:39:29 -0700491//int register_chip_restore(int (*function) (void *data), void *data)
492int register_chip_restore(CHIP_RESTORE_CALLBACK,
493 struct flashchip *flash, uint8_t status)
494{
495 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
496 msg_perr("Tried to register more than %i chip restore"
497 " functions.\n", CHIP_RESTORE_MAXFN);
498 return 1;
499 }
500 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
501 chip_restore_fn[chip_restore_fn_count].flash = flash;
502 chip_restore_fn[chip_restore_fn_count].status = status;
503 chip_restore_fn_count++;
504
505 return 0;
506}
507
David Hendricks82fd8ae2010-08-04 14:34:54 -0700508int programmer_init(char *param)
uweabe92a52009-05-16 22:36:00 +0000509{
David Hendricks82fd8ae2010-08-04 14:34:54 -0700510 int ret;
511 /* Initialize all programmer specific data. */
512 /* Default to unlimited decode sizes. */
513 max_rom_decode = (const struct decode_sizes) {
514 .parallel = 0xffffffff,
515 .lpc = 0xffffffff,
516 .fwh = 0xffffffff,
517 .spi = 0xffffffff
518 };
519 /* Default to Parallel/LPC/FWH flash devices. If a known host controller
520 * is found, the init routine sets the buses_supported bitfield.
521 */
522 buses_supported = CHIP_BUSTYPE_NONSPI;
523 /* Default to top aligned flash at 4 GB. */
524 flashbase = 0;
525 /* Registering shutdown functions is now allowed. */
526 may_register_shutdown = 1;
527 /* Default to allowing writes. Broken programmers set this to 0. */
528 programmer_may_write = 1;
529
530 programmer_param = param;
531 msg_pdbg("Initializing %s programmer\n",
532 programmer_table[programmer].name);
533 ret = programmer_table[programmer].init();
534 if (programmer_param && strlen(programmer_param)) {
535 msg_perr("Unhandled programmer parameters: %s\n",
536 programmer_param);
537 /* Do not error out here, the init itself was successful. */
538 }
539 return ret;
uweabe92a52009-05-16 22:36:00 +0000540}
541
David Hendricksbf36f092010-11-02 23:39:29 -0700542int chip_restore()
543{
544 int rc = 0;
545
546 while (chip_restore_fn_count > 0) {
547 int i = --chip_restore_fn_count;
548 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
549 chip_restore_fn[i].status);
550 }
551
552 return rc;
553}
554
uweabe92a52009-05-16 22:36:00 +0000555int programmer_shutdown(void)
556{
David Hendricks82fd8ae2010-08-04 14:34:54 -0700557 /* Registering shutdown functions is no longer allowed. */
558 may_register_shutdown = 0;
559 while (shutdown_fn_count > 0) {
560 int i = --shutdown_fn_count;
hailfingerdc6f7972010-02-14 01:20:28 +0000561 shutdown_fn[i].func(shutdown_fn[i].data);
David Hendricks82fd8ae2010-08-04 14:34:54 -0700562 }
uweabe92a52009-05-16 22:36:00 +0000563 return programmer_table[programmer].shutdown();
564}
565
566void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
567 size_t len)
568{
569 return programmer_table[programmer].map_flash_region(descr,
570 phys_addr, len);
571}
572
573void programmer_unmap_flash_region(void *virt_addr, size_t len)
574{
575 programmer_table[programmer].unmap_flash_region(virt_addr, len);
576}
577
578void chip_writeb(uint8_t val, chipaddr addr)
579{
580 programmer_table[programmer].chip_writeb(val, addr);
581}
582
583void chip_writew(uint16_t val, chipaddr addr)
584{
585 programmer_table[programmer].chip_writew(val, addr);
586}
587
588void chip_writel(uint32_t val, chipaddr addr)
589{
590 programmer_table[programmer].chip_writel(val, addr);
591}
592
hailfinger9d987ef2009-06-05 18:32:07 +0000593void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
594{
595 programmer_table[programmer].chip_writen(buf, addr, len);
596}
597
uweabe92a52009-05-16 22:36:00 +0000598uint8_t chip_readb(const chipaddr addr)
599{
600 return programmer_table[programmer].chip_readb(addr);
601}
602
603uint16_t chip_readw(const chipaddr addr)
604{
605 return programmer_table[programmer].chip_readw(addr);
606}
607
608uint32_t chip_readl(const chipaddr addr)
609{
610 return programmer_table[programmer].chip_readl(addr);
611}
612
hailfinger9d987ef2009-06-05 18:32:07 +0000613void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
614{
615 programmer_table[programmer].chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000616}
617
hailfingere5829f62009-06-05 17:48:08 +0000618void programmer_delay(int usecs)
619{
620 programmer_table[programmer].delay(usecs);
621}
622
stuge5ff0e6c2009-01-26 00:39:57 +0000623void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000624{
stepan15e64bc2007-05-24 08:48:10 +0000625 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000626 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000627 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfinger82719632009-05-16 21:22:56 +0000628 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000629}
630
hailfinger0f08b7a2009-06-16 08:55:44 +0000631int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger23060112009-05-08 12:49:03 +0000632{
hailfinger0f08b7a2009-06-16 08:55:44 +0000633 chip_readn(buf, flash->virtual_memory + start, len);
hailfinger23060112009-05-08 12:49:03 +0000634
635 return 0;
636}
637
hailfinger7b414742009-06-13 12:04:03 +0000638int min(int a, int b)
639{
640 return (a < b) ? a : b;
641}
642
hailfinger7af83692009-06-15 17:23:36 +0000643int max(int a, int b)
644{
645 return (a > b) ? a : b;
646}
647
hailfingeraec9c962009-10-31 01:53:09 +0000648int bitcount(unsigned long a)
649{
650 int i = 0;
651 for (; a != 0; a >>= 1)
652 if (a & 1)
653 i++;
654 return i;
655}
656
David Hendricksc801adb2010-12-09 16:58:56 -0800657void tolower_string(char *str)
658{
659 for (; *str != '\0'; str++)
660 *str = (char)tolower((unsigned char)*str);
661}
662
hailfingera916b422009-06-01 02:08:58 +0000663char *strcat_realloc(char *dest, const char *src)
664{
665 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
David Hendricks82fd8ae2010-08-04 14:34:54 -0700666 if (!dest) {
667 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000668 return NULL;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700669 }
hailfingera916b422009-06-01 02:08:58 +0000670 strcat(dest, src);
671 return dest;
672}
673
hailfinger6e5a52a2009-11-24 18:27:10 +0000674/* This is a somewhat hacked function similar in some ways to strtok().
David Hendricks82fd8ae2010-08-04 14:34:54 -0700675 * It will look for needle with a subsequent '=' in haystack, return a copy of
676 * needle and remove everything from the first occurrence of needle to the next
677 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000678 */
679char *extract_param(char **haystack, char *needle, char *delim)
680{
David Hendricks82fd8ae2010-08-04 14:34:54 -0700681 char *param_pos, *opt_pos, *rest;
682 char *opt = NULL;
683 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000684 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000685
hailfingerf4aaccc2010-04-28 15:22:14 +0000686 needlelen = strlen(needle);
687 if (!needlelen) {
688 msg_gerr("%s: empty needle! Please report a bug at "
689 "flashrom@flashrom.org\n", __func__);
690 return NULL;
691 }
692 /* No programmer parameters given. */
693 if (*haystack == NULL)
694 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000695 param_pos = strstr(*haystack, needle);
696 do {
697 if (!param_pos)
698 return NULL;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700699 /* Needle followed by '='? */
700 if (param_pos[needlelen] == '=') {
701
702 /* Beginning of the string? */
703 if (param_pos == *haystack)
704 break;
705 /* After a delimiter? */
706 if (strchr(delim, *(param_pos - 1)))
707 break;
708 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000709 /* Continue searching. */
710 param_pos++;
711 param_pos = strstr(param_pos, needle);
712 } while (1);
David Hendricks82fd8ae2010-08-04 14:34:54 -0700713
hailfinger6e5a52a2009-11-24 18:27:10 +0000714 if (param_pos) {
David Hendricks82fd8ae2010-08-04 14:34:54 -0700715 /* Get the string after needle and '='. */
716 opt_pos = param_pos + needlelen + 1;
717 optlen = strcspn(opt_pos, delim);
718 /* Return an empty string if the parameter was empty. */
719 opt = malloc(optlen + 1);
720 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000721 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000722 exit(1);
723 }
David Hendricks82fd8ae2010-08-04 14:34:54 -0700724 strncpy(opt, opt_pos, optlen);
725 opt[optlen] = '\0';
726 rest = opt_pos + optlen;
727 /* Skip all delimiters after the current parameter. */
728 rest += strspn(rest, delim);
729 memmove(param_pos, rest, strlen(rest) + 1);
730 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000731 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000732
David Hendricks82fd8ae2010-08-04 14:34:54 -0700733 return opt;
734}
735
736char *extract_programmer_param(char *param_name)
737{
738 return extract_param(&programmer_param, param_name, ",");
hailfinger6e5a52a2009-11-24 18:27:10 +0000739}
740
hailfinger7af83692009-06-15 17:23:36 +0000741/* start is an offset to the base address of the flash chip */
742int check_erased_range(struct flashchip *flash, int start, int len)
743{
744 int ret;
745 uint8_t *cmpbuf = malloc(len);
746
747 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000748 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000749 exit(1);
750 }
751 memset(cmpbuf, 0xff, len);
752 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
753 free(cmpbuf);
754 return ret;
755}
756
David Hendricksc801adb2010-12-09 16:58:56 -0800757/*
hailfinger7af3d192009-11-25 17:05:52 +0000758 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
759 flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000760 * @start offset to the base address of the flash chip
761 * @len length of the verified area
762 * @message string to print in the "FAILED" message
763 * @return 0 for success, -1 for failure
764 */
765int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
766{
David Hendricksc801adb2010-12-09 16:58:56 -0800767 int i, ret = 0;
768 uint8_t *readbuf = malloc(len);
hailfinger5be6c0f2009-07-23 01:42:56 +0000769 int failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000770
771 if (!len)
772 goto out_free;
773
hailfingerb0f4d122009-06-24 08:20:45 +0000774 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000775 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000776 return 1;
777 }
hailfinger7af83692009-06-15 17:23:36 +0000778 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000779 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000780 exit(1);
781 }
782
783 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000784 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000785 " total_size 0x%x\n", __func__, start, len,
786 flash->total_size * 1024);
787 ret = -1;
788 goto out_free;
789 }
790 if (!message)
791 message = "VERIFY";
792
David Hendricksc801adb2010-12-09 16:58:56 -0800793 ret = flash->read(flash, readbuf, start, len);
794 if (ret) {
795 msg_gerr("Verification impossible because read failed "
796 "at 0x%x (len 0x%x)\n", start, len);
797 return ret;
798 }
799
800 for (i = 0; i < len; i++) {
801 if (cmpbuf[i] != readbuf[i]) {
802 /* Only print the first failure. */
803 if (!failcount++)
804 msg_cerr("%s FAILED at 0x%08x! "
805 "Expected=0x%02x, Read=0x%02x,",
806 message, start + i, cmpbuf[i],
807 readbuf[i]);
hailfinger7af83692009-06-15 17:23:36 +0000808 }
809 }
hailfinger5be6c0f2009-07-23 01:42:56 +0000810 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000811 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
hailfinger5be6c0f2009-07-23 01:42:56 +0000812 start, start + len - 1, failcount);
813 ret = -1;
814 }
hailfinger7af83692009-06-15 17:23:36 +0000815
816out_free:
817 free(readbuf);
818 return ret;
819}
820
David Hendricksc801adb2010-12-09 16:58:56 -0800821/*
hailfingerb247c7a2010-03-08 00:42:32 +0000822 * Check if the buffer @have can be programmed to the content of @want without
823 * erasing. This is only possible if all chunks of size @gran are either kept
824 * as-is or changed from an all-ones state to any other state.
David Hendricksc801adb2010-12-09 16:58:56 -0800825 *
hailfingerb247c7a2010-03-08 00:42:32 +0000826 * The following write granularities (enum @gran) are known:
827 * - 1 bit. Each bit can be cleared individually.
828 * - 1 byte. A byte can be written once. Further writes to an already written
829 * byte cause the contents to be either undefined or to stay unchanged.
830 * - 128 bytes. If less than 128 bytes are written, the rest will be
831 * erased. Each write to a 128-byte region will trigger an automatic erase
832 * before anything is written. Very uncommon behaviour and unsupported by
833 * this function.
834 * - 256 bytes. If less than 256 bytes are written, the contents of the
835 * unwritten bytes are undefined.
David Hendricksc801adb2010-12-09 16:58:56 -0800836 * Warning: This function assumes that @have and @want point to naturally
837 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000838 *
839 * @have buffer with current content
840 * @want buffer with desired content
David Hendricksc801adb2010-12-09 16:58:56 -0800841 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000842 * @gran write granularity (enum, not count)
843 * @return 0 if no erase is needed, 1 otherwise
844 */
845int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
846{
847 int result = 0;
848 int i, j, limit;
849
850 switch (gran) {
851 case write_gran_1bit:
852 for (i = 0; i < len; i++)
853 if ((have[i] & want[i]) != want[i]) {
854 result = 1;
855 break;
856 }
857 break;
858 case write_gran_1byte:
859 for (i = 0; i < len; i++)
860 if ((have[i] != want[i]) && (have[i] != 0xff)) {
861 result = 1;
862 break;
863 }
864 break;
865 case write_gran_256bytes:
866 for (j = 0; j < len / 256; j++) {
867 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000868 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000869 if (!memcmp(have + j * 256, want + j * 256, limit))
870 continue;
871 /* have needs to be in erased state. */
872 for (i = 0; i < limit; i++)
David Hendricksc801adb2010-12-09 16:58:56 -0800873 if (have[j * 256 + i] != 0xff) {
hailfingerb247c7a2010-03-08 00:42:32 +0000874 result = 1;
875 break;
876 }
877 if (result)
878 break;
879 }
880 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800881 default:
882 msg_cerr("%s: Unsupported granularity! Please report a bug at "
883 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000884 }
885 return result;
886}
887
David Hendricksc801adb2010-12-09 16:58:56 -0800888/**
889 * Check if the buffer @have needs to be programmed to get the content of @want.
890 * If yes, return 1 and fill in first_start with the start address of the
891 * write operation and first_len with the length of the first to-be-written
892 * chunk. If not, return 0 and leave first_start and first_len undefined.
893 *
894 * Warning: This function assumes that @have and @want point to naturally
895 * aligned regions.
896 *
897 * @have buffer with current content
898 * @want buffer with desired content
899 * @len length of the checked area
900 * @gran write granularity (enum, not count)
901 * @first_start offset of the first byte which needs to be written (passed in
902 * value is increased by the offset of the first needed write
903 * relative to have/want or unchanged if no write is needed)
904 * @return length of the first contiguous area which needs to be written
905 * 0 if no write is needed
906 *
907 * FIXME: This function needs a parameter which tells it about coalescing
908 * in relation to the max write length of the programmer and the max write
909 * length of the chip.
910 */
911static int get_next_write(uint8_t *have, uint8_t *want, int len,
912 int *first_start, enum write_granularity gran)
913{
914 int need_write = 0, rel_start = 0, first_len = 0;
915 int i, limit, stride;
916
917 switch (gran) {
918 case write_gran_1bit:
919 case write_gran_1byte:
920 stride = 1;
921 break;
922 case write_gran_256bytes:
923 stride = 256;
924 break;
925 default:
926 msg_cerr("%s: Unsupported granularity! Please report a bug at "
927 "flashrom@flashrom.org\n", __func__);
928 /* Claim that no write was needed. A write with unknown
929 * granularity is too dangerous to try.
930 */
931 return 0;
932 }
933 for (i = 0; i < len / stride; i++) {
934 limit = min(stride, len - i * stride);
935 /* Are 'have' and 'want' identical? */
936 if (memcmp(have + i * stride, want + i * stride, limit)) {
937 if (!need_write) {
938 /* First location where have and want differ. */
939 need_write = 1;
940 rel_start = i * stride;
941 }
942 } else {
943 if (need_write) {
944 /* First location where have and want
945 * do not differ anymore.
946 */
947 first_len = i * stride - rel_start;
948 break;
949 }
950 }
951 }
952 /* Did the loop terminate without setting first_len? */
953 if (need_write && ! first_len)
954 first_len = min(i * stride - rel_start, len);
955 *first_start += rel_start;
956 return first_len;
957}
958
hailfinger0c515352009-11-23 12:55:31 +0000959/* This function generates various test patterns useful for testing controller
960 * and chip communication as well as chip behaviour.
961 *
962 * If a byte can be written multiple times, each time keeping 0-bits at 0
963 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
964 * is essentially an AND operation. That's also the reason why this function
965 * provides the result of AND between various patterns.
966 *
967 * Below is a list of patterns (and their block length).
968 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
969 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
970 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
971 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
972 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
973 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
974 * Pattern 6 is 00 (1 Byte)
975 * Pattern 7 is ff (1 Byte)
976 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
977 * byte block.
978 *
979 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
980 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
981 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
982 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
983 * Pattern 12 is 00 (1 Byte)
984 * Pattern 13 is ff (1 Byte)
985 * Patterns 8-13 have no block number.
986 *
987 * Patterns 0-3 are created to detect and efficiently diagnose communication
988 * slips like missed bits or bytes and their repetitive nature gives good visual
989 * cues to the person inspecting the results. In addition, the following holds:
990 * AND Pattern 0/1 == Pattern 4
991 * AND Pattern 2/3 == Pattern 5
992 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
993 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
994 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
995 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
996 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
997 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
998 * Besides that, they provide for bit testing of the last two bytes of every
999 * 256 byte block which contains the block number for patterns 0-6.
1000 * Patterns 10-11 are special purpose for detecting subblock aliasing with
1001 * block sizes >256 bytes (some Dataflash chips etc.)
1002 * AND Pattern 8/9 == Pattern 12
1003 * AND Pattern 10/11 == Pattern 12
1004 * Pattern 13 is the completely erased state.
1005 * None of the patterns can detect aliasing at boundaries which are a multiple
1006 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
1007 */
1008int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
1009{
1010 int i;
1011
1012 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +00001013 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +00001014 return 1;
1015 }
1016
1017 switch (variant) {
1018 case 0:
1019 for (i = 0; i < size; i++)
1020 buf[i] = (i & 0xf) << 4 | 0x5;
1021 break;
1022 case 1:
1023 for (i = 0; i < size; i++)
1024 buf[i] = (i & 0xf) << 4 | 0xa;
1025 break;
1026 case 2:
1027 for (i = 0; i < size; i++)
1028 buf[i] = 0x50 | (i & 0xf);
1029 break;
1030 case 3:
1031 for (i = 0; i < size; i++)
1032 buf[i] = 0xa0 | (i & 0xf);
1033 break;
1034 case 4:
1035 for (i = 0; i < size; i++)
1036 buf[i] = (i & 0xf) << 4;
1037 break;
1038 case 5:
1039 for (i = 0; i < size; i++)
1040 buf[i] = i & 0xf;
1041 break;
1042 case 6:
1043 memset(buf, 0x00, size);
1044 break;
1045 case 7:
1046 memset(buf, 0xff, size);
1047 break;
1048 case 8:
1049 for (i = 0; i < size; i++)
1050 buf[i] = i & 0xff;
1051 break;
1052 case 9:
1053 for (i = 0; i < size; i++)
1054 buf[i] = ~(i & 0xff);
1055 break;
1056 case 10:
1057 for (i = 0; i < size % 2; i++) {
1058 buf[i * 2] = (i >> 8) & 0xff;
1059 buf[i * 2 + 1] = i & 0xff;
1060 }
1061 if (size & 0x1)
1062 buf[i * 2] = (i >> 8) & 0xff;
1063 break;
1064 case 11:
1065 for (i = 0; i < size % 2; i++) {
1066 buf[i * 2] = ~((i >> 8) & 0xff);
1067 buf[i * 2 + 1] = ~(i & 0xff);
1068 }
1069 if (size & 0x1)
1070 buf[i * 2] = ~((i >> 8) & 0xff);
1071 break;
1072 case 12:
1073 memset(buf, 0x00, size);
1074 break;
1075 case 13:
1076 memset(buf, 0xff, size);
1077 break;
1078 }
1079
1080 if ((variant >= 0) && (variant <= 7)) {
1081 /* Write block number in the last two bytes of each 256-byte
1082 * block, big endian for easier reading of the hexdump.
1083 * Note that this wraps around for chips larger than 2^24 bytes
1084 * (16 MB).
1085 */
1086 for (i = 0; i < size / 256; i++) {
1087 buf[i * 256 + 254] = (i >> 8) & 0xff;
1088 buf[i * 256 + 255] = i & 0xff;
1089 }
1090 }
1091
1092 return 0;
1093}
1094
hailfingeraec9c962009-10-31 01:53:09 +00001095int check_max_decode(enum chipbustype buses, uint32_t size)
1096{
1097 int limitexceeded = 0;
1098 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
1099 (max_rom_decode.parallel < size)) {
1100 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001101 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001102 "size %u kB of chipset/board/programmer "
1103 "for %s interface, "
1104 "probe/read/erase/write may fail. ", size / 1024,
1105 max_rom_decode.parallel / 1024, "Parallel");
1106 }
1107 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
1108 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001109 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001110 "size %u kB of chipset/board/programmer "
1111 "for %s interface, "
1112 "probe/read/erase/write may fail. ", size / 1024,
1113 max_rom_decode.lpc / 1024, "LPC");
1114 }
1115 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
1116 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001117 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001118 "size %u kB of chipset/board/programmer "
1119 "for %s interface, "
1120 "probe/read/erase/write may fail. ", size / 1024,
1121 max_rom_decode.fwh / 1024, "FWH");
1122 }
1123 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
1124 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001125 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001126 "size %u kB of chipset/board/programmer "
1127 "for %s interface, "
1128 "probe/read/erase/write may fail. ", size / 1024,
1129 max_rom_decode.spi / 1024, "SPI");
1130 }
1131 if (!limitexceeded)
1132 return 0;
1133 /* Sometimes chip and programmer have more than one bus in common,
1134 * and the limit is not exceeded on all buses. Tell the user.
1135 */
1136 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001137 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001138 msg_pdbg("There is at least one common chip/programmer "
hailfingeraec9c962009-10-31 01:53:09 +00001139 "interface which can support a chip of this size. "
1140 "You can try --force at your own risk.\n");
1141 return 1;
1142}
1143
stuge56300c32008-09-03 23:10:05 +00001144struct flashchip *probe_flash(struct flashchip *first_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001145{
stuge56300c32008-09-03 23:10:05 +00001146 struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001147 unsigned long base = 0;
1148 uint32_t size;
1149 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001150 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001151
stuge56300c32008-09-03 23:10:05 +00001152 for (flash = first_flash; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001153 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001154 continue;
hailfinger327d2522010-03-22 23:43:51 +00001155 msg_gdbg("Probing for %s %s, %d KB: ",
stepanb8361b92008-03-17 22:59:40 +00001156 flash->vendor, flash->name, flash->total_size);
stuge98c09aa2008-06-18 02:08:40 +00001157 if (!flash->probe && !force) {
hailfinger327d2522010-03-22 23:43:51 +00001158 msg_gdbg("failed! flashrom has no probe function for "
1159 "this flash chip.\n");
stuge8ce3a3c2008-04-28 14:47:30 +00001160 continue;
1161 }
hailfingeraec9c962009-10-31 01:53:09 +00001162 buses_common = buses_supported & flash->bustype;
1163 if (!buses_common) {
hailfingera916b422009-06-01 02:08:58 +00001164 tmp = flashbuses_to_text(buses_supported);
hailfinger327d2522010-03-22 23:43:51 +00001165 msg_gdbg("skipped.");
1166 msg_gspew(" Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001167 free(tmp);
1168 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001169 msg_gspew("and chip bus type %s are incompatible.",
1170 tmp);
hailfingera916b422009-06-01 02:08:58 +00001171 free(tmp);
hailfinger327d2522010-03-22 23:43:51 +00001172 msg_gdbg("\n");
hailfingera916b422009-06-01 02:08:58 +00001173 continue;
1174 }
stepan782fb172007-04-06 11:58:03 +00001175
ollie5672ac62004-03-17 22:22:08 +00001176 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001177 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001178
hailfinger72d3b982009-05-09 07:27:23 +00001179 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger82719632009-05-16 21:22:56 +00001180 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001181
stugec1e55fe2008-07-02 17:15:47 +00001182 if (force)
1183 break;
stepanc98b80b2006-03-16 16:57:41 +00001184
stuge56300c32008-09-03 23:10:05 +00001185 if (flash->probe(flash) != 1)
1186 goto notfound;
1187
uwefa98ca12008-10-18 21:14:13 +00001188 if (first_flash == flashchips
1189 || flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001190 break;
1191
stuge56300c32008-09-03 23:10:05 +00001192notfound:
hailfinger82719632009-05-16 21:22:56 +00001193 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001194 }
uwebe4477b2007-08-23 16:08:21 +00001195
stugec1e55fe2008-07-02 17:15:47 +00001196 if (!flash || !flash->name)
1197 return NULL;
1198
David Hendricksc6c9f822010-11-03 15:07:01 -07001199 msg_cdbg("%s chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
hailfingerf4aaccc2010-04-28 15:22:14 +00001200 force ? "Assuming" : "Found",
uwe9e6811e2009-06-28 21:47:57 +00001201 flash->vendor, flash->name, flash->total_size,
1202 flashbuses_to_text(flash->bustype), base);
1203
snelson1ee293c2010-02-19 00:52:10 +00001204 if (flash->printlock)
1205 flash->printlock(flash);
1206
stugec1e55fe2008-07-02 17:15:47 +00001207 return flash;
rminnich8d3ff912003-10-25 17:01:29 +00001208}
1209
stepan193c9c22005-12-18 16:41:10 +00001210int verify_flash(struct flashchip *flash, uint8_t *buf)
rminnich8d3ff912003-10-25 17:01:29 +00001211{
hailfingerb0f4d122009-06-24 08:20:45 +00001212 int ret;
David Hendricksc801adb2010-12-09 16:58:56 -08001213 int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001214
snelsone42c3802010-05-07 20:09:04 +00001215 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001216
David Hendricksc801adb2010-12-09 16:58:56 -08001217 ret = verify_range(flash, buf, 0, total_size, NULL);
uwef6641642007-05-09 10:17:44 +00001218
hailfingerb0f4d122009-06-24 08:20:45 +00001219 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001220 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001221
hailfingerb0f4d122009-06-24 08:20:45 +00001222 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001223}
1224
David Hendricksc801adb2010-12-09 16:58:56 -08001225int read_buf_from_file(unsigned char *buf, unsigned long size, char *filename)
1226{
1227 unsigned long numbytes;
1228 FILE *image;
1229 struct stat image_stat;
1230
1231 if ((image = fopen(filename, "rb")) == NULL) {
1232 perror(filename);
1233 return 1;
1234 }
1235 if (fstat(fileno(image), &image_stat) != 0) {
1236 perror(filename);
1237 fclose(image);
1238 return 1;
1239 }
1240 if (image_stat.st_size != size) {
1241 msg_gerr("Error: Image size doesn't match\n");
1242 fclose(image);
1243 return 1;
1244 }
1245 numbytes = fread(buf, 1, size, image);
1246 if (fclose(image)) {
1247 perror(filename);
1248 return 1;
1249 }
1250 if (numbytes != size) {
1251 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1252 "wanted %ld!\n", numbytes, size);
1253 return 1;
1254 }
1255 return 0;
1256}
1257
David Hendricks82fd8ae2010-08-04 14:34:54 -07001258int write_buf_to_file(unsigned char *buf, unsigned long size, char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001259{
1260 unsigned long numbytes;
1261 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001262
1263 if (!filename) {
David Hendricks82fd8ae2010-08-04 14:34:54 -07001264 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001265 return 1;
1266 }
oxygenebf70d352010-01-25 22:55:33 +00001267 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001268 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001269 return 1;
David Hendricks82fd8ae2010-08-04 14:34:54 -07001270 }
hailfingerd219a232009-01-28 00:27:54 +00001271
hailfingerd219a232009-01-28 00:27:54 +00001272 numbytes = fwrite(buf, 1, size, image);
1273 fclose(image);
David Hendricks82fd8ae2010-08-04 14:34:54 -07001274 if (numbytes != size) {
1275 msg_gerr("File %s could not be written completely.\n",
1276 filename);
hailfingerd219a232009-01-28 00:27:54 +00001277 return 1;
David Hendricks82fd8ae2010-08-04 14:34:54 -07001278 }
hailfingerd219a232009-01-28 00:27:54 +00001279 return 0;
1280}
1281
David Hendricks82fd8ae2010-08-04 14:34:54 -07001282int read_flash_to_file(struct flashchip *flash, char *filename)
1283{
1284 unsigned long size = flash->total_size * 1024;
1285 unsigned char *buf = calloc(size, sizeof(char));
1286 int ret = 0;
1287
1288 msg_cinfo("Reading flash... ");
1289 if (!buf) {
David Hendricks82fd8ae2010-08-04 14:34:54 -07001290 msg_cinfo("FAILED.\n");
David Hendricksc6c9f822010-11-03 15:07:01 -07001291 msg_gerr("Memory allocation failed!\n");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001292 return 1;
1293 }
1294 if (!flash->read) {
1295 msg_cerr("No read function available for this flash chip.\n");
1296 ret = 1;
1297 goto out_free;
1298 }
1299 if (flash->read(flash, buf, 0, size)) {
1300 msg_cerr("Read operation failed!\n");
1301 ret = 1;
1302 goto out_free;
1303 }
1304
1305 ret = write_buf_to_file(buf, flash->total_size * 1024, filename);
1306out_free:
1307 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001308 if (ret)
1309 msg_cerr("FAILED.");
1310 else
1311 msg_cdbg("done.");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001312 return ret;
1313}
1314
David Hendricksc801adb2010-12-09 16:58:56 -08001315/* This function shares a lot of its structure with erase_and_write_flash() and
1316 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001317 * Even if an error is found, the function will keep going and check the rest.
1318 */
David Hendricks82fd8ae2010-08-04 14:34:54 -07001319static int selfcheck_eraseblocks(struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001320{
hailfinger9fed35d2010-01-19 06:42:46 +00001321 int i, j, k;
1322 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001323
1324 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1325 unsigned int done = 0;
1326 struct block_eraser eraser = flash->block_erasers[k];
1327
1328 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1329 /* Blocks with zero size are bugs in flashchips.c. */
1330 if (eraser.eraseblocks[i].count &&
1331 !eraser.eraseblocks[i].size) {
1332 msg_gerr("ERROR: Flash chip %s erase function "
1333 "%i region %i has size 0. Please report"
1334 " a bug at flashrom@flashrom.org\n",
1335 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001336 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001337 }
1338 /* Blocks with zero count are bugs in flashchips.c. */
1339 if (!eraser.eraseblocks[i].count &&
1340 eraser.eraseblocks[i].size) {
1341 msg_gerr("ERROR: Flash chip %s erase function "
1342 "%i region %i has count 0. Please report"
1343 " a bug at flashrom@flashrom.org\n",
1344 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001345 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001346 }
1347 done += eraser.eraseblocks[i].count *
1348 eraser.eraseblocks[i].size;
1349 }
hailfinger9fed35d2010-01-19 06:42:46 +00001350 /* Empty eraseblock definition with erase function. */
1351 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001352 msg_gspew("Strange: Empty eraseblock definition with "
hailfinger9fed35d2010-01-19 06:42:46 +00001353 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001354 if (!done)
1355 continue;
1356 if (done != flash->total_size * 1024) {
1357 msg_gerr("ERROR: Flash chip %s erase function %i "
1358 "region walking resulted in 0x%06x bytes total,"
1359 " expected 0x%06x bytes. Please report a bug at"
1360 " flashrom@flashrom.org\n", flash->name, k,
1361 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001362 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001363 }
hailfinger9fed35d2010-01-19 06:42:46 +00001364 if (!eraser.block_erase)
1365 continue;
1366 /* Check if there are identical erase functions for different
1367 * layouts. That would imply "magic" erase functions. The
1368 * easiest way to check this is with function pointers.
1369 */
uwef6f94d42010-03-13 17:28:29 +00001370 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001371 if (eraser.block_erase ==
1372 flash->block_erasers[j].block_erase) {
1373 msg_gerr("ERROR: Flash chip %s erase function "
1374 "%i and %i are identical. Please report"
1375 " a bug at flashrom@flashrom.org\n",
1376 flash->name, k, j);
1377 ret = 1;
1378 }
uwef6f94d42010-03-13 17:28:29 +00001379 }
hailfinger45177872010-01-18 08:14:43 +00001380 }
hailfinger9fed35d2010-01-19 06:42:46 +00001381 return ret;
hailfinger45177872010-01-18 08:14:43 +00001382}
1383
David Hendricksc801adb2010-12-09 16:58:56 -08001384static int erase_and_write_block_helper(struct flashchip *flash,
1385 unsigned int start, unsigned int len,
1386 uint8_t *curcontents,
1387 uint8_t *newcontents,
1388 int (*erasefn) (struct flashchip *flash,
1389 unsigned int addr,
1390 unsigned int len))
1391{
1392 int starthere = 0;
1393 int lenhere = 0;
1394 int ret = 0;
1395 int skip = 1;
1396 int writecount = 0;
1397 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1398
1399 /* curcontents and newcontents are opaque to walk_eraseregions, and
1400 * need to be adjusted here to keep the impression of proper abstraction
1401 */
1402 curcontents += start;
1403 newcontents += start;
1404 msg_cdbg(":");
1405 /* FIXME: Assume 256 byte granularity for now to play it safe. */
1406 if (need_erase(curcontents, newcontents, len, gran)) {
1407 msg_cdbg("E");
1408 ret = erasefn(flash, start, len);
1409 if (ret)
1410 return ret;
1411 /* Erase was successful. Adjust curcontents. */
1412 memset(curcontents, 0xff, len);
1413 skip = 0;
1414 }
1415 /* get_next_write() sets starthere to a new value after the call. */
1416 while ((lenhere = get_next_write(curcontents + starthere,
1417 newcontents + starthere,
1418 len - starthere, &starthere, gran))) {
1419 if (!writecount++)
1420 msg_cdbg("W");
1421 /* Needs the partial write function signature. */
1422 ret = flash->write(flash, newcontents + starthere,
1423 start + starthere, lenhere);
1424 if (ret)
1425 return ret;
1426 starthere += lenhere;
1427 skip = 0;
1428 }
1429 if (skip)
1430 msg_cdbg("S");
1431 return ret;
1432}
1433
David Hendricks82fd8ae2010-08-04 14:34:54 -07001434static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1435 int (*do_something) (struct flashchip *flash,
1436 unsigned int addr,
David Hendricksc801adb2010-12-09 16:58:56 -08001437 unsigned int len,
1438 uint8_t *param1,
1439 uint8_t *param2,
1440 int (*erasefn) (
1441 struct flashchip *flash,
1442 unsigned int addr,
1443 unsigned int len)),
1444 void *param1, void *param2)
David Hendricks82fd8ae2010-08-04 14:34:54 -07001445{
1446 int i, j;
1447 unsigned int start = 0;
1448 unsigned int len;
1449 struct block_eraser eraser = flash->block_erasers[erasefunction];
1450 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1451 /* count==0 for all automatically initialized array
1452 * members so the loop below won't be executed for them.
1453 */
1454 len = eraser.eraseblocks[i].size;
1455 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
David Hendricksc801adb2010-12-09 16:58:56 -08001456 /* Print this for every block except the first one. */
1457 if (i || j)
1458 msg_cdbg(", ");
David Hendricks970da352010-08-24 15:03:01 -07001459 msg_cdbg("0x%06x-0x%06x", start,
David Hendricks82fd8ae2010-08-04 14:34:54 -07001460 start + len - 1);
David Hendricksc801adb2010-12-09 16:58:56 -08001461 if (do_something(flash, start, len, param1, param2,
1462 eraser.block_erase)) {
1463 msg_cdbg("\n");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001464 return 1;
David Hendricksc801adb2010-12-09 16:58:56 -08001465 }
David Hendricks82fd8ae2010-08-04 14:34:54 -07001466 start += len;
1467 }
1468 }
David Hendricksc801adb2010-12-09 16:58:56 -08001469 msg_cdbg("\n");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001470 return 0;
1471}
1472
David Hendricksc801adb2010-12-09 16:58:56 -08001473int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents, uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001474{
David Hendricks82fd8ae2010-08-04 14:34:54 -07001475 int k, ret = 0, found = 0;
David Hendricksc801adb2010-12-09 16:58:56 -08001476 uint8_t *curcontents;
1477 unsigned long size = flash->total_size * 1024;
hailfinger7df21362009-09-05 02:30:58 +00001478
David Hendricksc801adb2010-12-09 16:58:56 -08001479 curcontents = (uint8_t *) malloc(size);
1480 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1481 memcpy(curcontents, oldcontents, size);
1482
1483 msg_cdbg("Erasing and writing flash chip... ");
hailfinger7df21362009-09-05 02:30:58 +00001484 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
hailfinger7df21362009-09-05 02:30:58 +00001485 struct block_eraser eraser = flash->block_erasers[k];
1486
snelsone42c3802010-05-07 20:09:04 +00001487 msg_cdbg("Looking at blockwise erase function %i... ", k);
hailfinger7df21362009-09-05 02:30:58 +00001488 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
snelsone42c3802010-05-07 20:09:04 +00001489 msg_cdbg("not defined. "
hailfinger7df21362009-09-05 02:30:58 +00001490 "Looking for another erase function.\n");
1491 continue;
1492 }
1493 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
snelsone42c3802010-05-07 20:09:04 +00001494 msg_cdbg("eraseblock layout is known, but no "
hailfinger7df21362009-09-05 02:30:58 +00001495 "matching block erase function found. "
1496 "Looking for another erase function.\n");
1497 continue;
1498 }
1499 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
snelsone42c3802010-05-07 20:09:04 +00001500 msg_cdbg("block erase function found, but "
hailfinger7df21362009-09-05 02:30:58 +00001501 "eraseblock layout is unknown. "
1502 "Looking for another erase function.\n");
1503 continue;
1504 }
1505 found = 1;
snelsone42c3802010-05-07 20:09:04 +00001506 msg_cdbg("trying... ");
David Hendricksc801adb2010-12-09 16:58:56 -08001507 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper, curcontents, newcontents);
snelsone42c3802010-05-07 20:09:04 +00001508 msg_cdbg("\n");
hailfinger7df21362009-09-05 02:30:58 +00001509 /* If everything is OK, don't try another erase function. */
1510 if (!ret)
1511 break;
David Hendricksc801adb2010-12-09 16:58:56 -08001512 /* FIXME: Reread the whole chip here so we know the current
1513 * chip contents? curcontents might be up to date, but this
1514 * code is only reached if something failed, and then we don't
1515 * know exactly what failed, and how.
1516 */
hailfinger7df21362009-09-05 02:30:58 +00001517 }
David Hendricksc801adb2010-12-09 16:58:56 -08001518 /* Free the scratchpad. */
1519 free(curcontents);
hailfinger7df21362009-09-05 02:30:58 +00001520 if (!found) {
snelsone42c3802010-05-07 20:09:04 +00001521 msg_cerr("ERROR: flashrom has no erase function for this flash chip.\n");
hailfingerd219a232009-01-28 00:27:54 +00001522 return 1;
1523 }
hailfinger1e9ee0f2009-05-08 17:15:15 +00001524
hailfinger7df21362009-09-05 02:30:58 +00001525 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001526 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001527 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001528 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001529 }
1530 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001531}
1532
David Hendricksc801adb2010-12-09 16:58:56 -08001533void nonfatal_help_message(void)
1534{
1535 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1536 "This means we have to add special support for your board, "
1537 "programmer or flash chip.\n"
1538 "Please report this on IRC at irc.freenode.net (channel "
1539 "#flashrom) or\n"
1540 "mail flashrom@flashrom.org!\n"
1541 "-------------------------------------------------------------"
1542 "------------------\n"
1543 "You may now reboot or simply leave the machine running.\n");
1544}
1545
uweb34ec9f2009-10-01 18:40:02 +00001546void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001547{
snelsone42c3802010-05-07 20:09:04 +00001548 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001549 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
David Hendricksc801adb2010-12-09 16:58:56 -08001550 "mail flashrom@flashrom.org with FAILED: your board name in "
1551 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001552 "-------------------------------------------------------------"
1553 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001554 "DO NOT REBOOT OR POWEROFF!\n");
1555}
1556
hailfingerc77acb52009-12-24 02:15:55 +00001557/* The way to go if you want a delimited list of programmers*/
1558void list_programmers(char *delim)
1559{
1560 enum programmer p;
1561 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001562 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001563 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001564 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001565 }
snelsone42c3802010-05-07 20:09:04 +00001566 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001567}
1568
David Hendricksc801adb2010-12-09 16:58:56 -08001569void list_programmers_linebreak(int startcol, int cols, int paren)
1570{
1571 const char *pname;
1572 int pnamelen;
1573 int remaining = 0;
1574 int firstline = 1;
1575 enum programmer p;
1576 int i;
1577
1578 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1579 pname = programmer_table[p].name;
1580 pnamelen = strlen(pname);
1581 if (remaining - pnamelen - 2 < 0) {
1582 if (firstline)
1583 firstline = 0;
1584 else
1585 printf("\n");
1586 for (i = 0; i < startcol; i++)
1587 printf(" ");
1588 remaining = cols - startcol;
1589 } else {
1590 printf(" ");
1591 remaining--;
1592 }
1593 if (paren && (p == 0)) {
1594 printf("(");
1595 remaining--;
1596 }
1597 printf("%s", pname);
1598 remaining -= pnamelen;
1599 if (p < PROGRAMMER_INVALID - 1) {
1600 printf(",");
1601 remaining--;
1602 } else {
1603 if (paren)
1604 printf(")");
1605 printf("\n");
1606 }
1607 }
1608}
1609
hailfinger3b471632010-03-27 16:36:40 +00001610void print_sysinfo(void)
1611{
David Hendricksc6c9f822010-11-03 15:07:01 -07001612 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001613#if HAVE_UTSNAME == 1
1614 struct utsname osinfo;
1615 uname(&osinfo);
1616
David Hendricksc6c9f822010-11-03 15:07:01 -07001617 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001618 osinfo.machine);
1619#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001620 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001621#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001622 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001623#if NEED_PCI == 1
1624#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001625 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001626#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001627 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001628#endif
1629#endif
1630#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001631 msg_gerr(" LLVM Clang");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001632#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001633 msg_gerr(" %s,", __clang_version__);
David Hendricks82fd8ae2010-08-04 14:34:54 -07001634#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001635 msg_gerr(" unknown version (before r102686),");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001636#endif
hailfinger3b471632010-03-27 16:36:40 +00001637#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001638 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001639#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001640 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001641#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001642 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001643#endif
1644#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001645 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001646#endif
1647#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001648 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001649#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001650 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001651#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001652 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001653}
1654
uwefdeca092008-01-21 15:24:22 +00001655void print_version(void)
1656{
David Hendricksc6c9f822010-11-03 15:07:01 -07001657 /* send to stderr for chromium os */
1658 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001659 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001660}
1661
hailfinger74819ad2010-05-15 15:04:37 +00001662void print_banner(void)
1663{
1664 msg_ginfo("flashrom is free software, get the source code at "
1665 "http://www.flashrom.org\n");
1666 msg_ginfo("\n");
1667}
1668
hailfingerc77acb52009-12-24 02:15:55 +00001669int selfcheck(void)
1670{
hailfinger45177872010-01-18 08:14:43 +00001671 int ret = 0;
1672 struct flashchip *flash;
1673
1674 /* Safety check. Instead of aborting after the first error, check
1675 * if more errors exist.
1676 */
hailfingerc77acb52009-12-24 02:15:55 +00001677 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001678 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001679 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001680 }
1681 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001682 msg_gerr("SPI programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001683 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001684 }
hailfinger45177872010-01-18 08:14:43 +00001685 for (flash = flashchips; flash && flash->name; flash++)
1686 if (selfcheck_eraseblocks(flash))
1687 ret = 1;
1688 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001689}
1690
1691void check_chip_supported(struct flashchip *flash)
1692{
1693 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001694 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001695 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001696 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001697 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001698 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001699 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001700 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001701 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001702 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001703 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001704 msg_cdbg(" WRITE");
1705 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001706 }
1707 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1708 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1709 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1710 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001711 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001712 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001713 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001714 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001715 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001716 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001717 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001718 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001719 msg_cdbg(" WRITE");
1720 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001721 }
hailfinger92cd8e32010-01-07 03:24:05 +00001722 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001723 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001724 "in the latest development\n"
1725 "version of flashrom. If you are running the latest "
1726 "development version,\n"
1727 "please email a report to flashrom@flashrom.org if "
1728 "any of the above operations\n"
1729 "work correctly for you with this flash part. Please "
1730 "include the flashrom\n"
1731 "output with the additional -V option for all "
1732 "operations you tested (-V, -Vr,\n"
1733 "-Vw, -VE), and mention which mainboard or "
1734 "programmer you tested.\n"
David Hendricksc801adb2010-12-09 16:58:56 -08001735 "Please mention your board in the subject line. "
1736 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001737 }
1738}
1739
ollie5b621572004-03-20 16:46:10 +00001740int main(int argc, char *argv[])
rminnich8d3ff912003-10-25 17:01:29 +00001741{
David Hendricksf7924d12010-06-10 21:26:44 -07001742 /* FIXME: this should eventually be a build option controlled
1743 via a USE flag */
1744// return cli_classic(argc, argv);
1745 return cli_mfg(argc, argv);
hailfingerc77acb52009-12-24 02:15:55 +00001746}
1747
David Hendricksc801adb2010-12-09 16:58:56 -08001748/* FIXME: This function signature needs to be improved once doit() has a better
1749 * function signature.
hailfingerc77acb52009-12-24 02:15:55 +00001750 */
David Hendricksc801adb2010-12-09 16:58:56 -08001751int chip_safety_check(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
hailfingerc77acb52009-12-24 02:15:55 +00001752{
David Hendricks82fd8ae2010-08-04 14:34:54 -07001753 if (!programmer_may_write && (write_it || erase_it)) {
1754 msg_perr("Write/erase is not working yet on your programmer in "
1755 "its current configuration.\n");
1756 /* --force is the wrong approach, but it's the best we can do
1757 * until the generic programmer parameter parser is merged.
1758 */
David Hendricksc801adb2010-12-09 16:58:56 -08001759 if (!force)
David Hendricks82fd8ae2010-08-04 14:34:54 -07001760 return 1;
David Hendricksc801adb2010-12-09 16:58:56 -08001761 msg_cerr("Continuing anyway.\n");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001762 }
1763
David Hendricksc801adb2010-12-09 16:58:56 -08001764 if (read_it || erase_it || write_it || verify_it) {
1765 /* Everything needs read. */
1766 if (flash->tested & TEST_BAD_READ) {
1767 msg_cerr("Read is not working on this chip. ");
1768 if (!force)
1769 return 1;
1770 msg_cerr("Continuing anyway.\n");
1771 }
1772 if (!flash->read) {
1773 msg_cerr("flashrom has no read function for this "
1774 "flash chip.\n");
1775 return 1;
1776 }
1777 }
1778 if (erase_it || write_it) {
1779 /* Write needs erase. */
hailfinger0459e1c2009-08-19 13:55:34 +00001780 if (flash->tested & TEST_BAD_ERASE) {
snelsone42c3802010-05-07 20:09:04 +00001781 msg_cerr("Erase is not working on this chip. ");
David Hendricksc801adb2010-12-09 16:58:56 -08001782 if (!force)
hailfinger0459e1c2009-08-19 13:55:34 +00001783 return 1;
David Hendricksc801adb2010-12-09 16:58:56 -08001784 msg_cerr("Continuing anyway.\n");
hailfinger0459e1c2009-08-19 13:55:34 +00001785 }
David Hendricksc801adb2010-12-09 16:58:56 -08001786 /* FIXME: Check if at least one erase function exists. */
1787 }
1788 if (write_it) {
hailfinger0459e1c2009-08-19 13:55:34 +00001789 if (flash->tested & TEST_BAD_WRITE) {
snelsone42c3802010-05-07 20:09:04 +00001790 msg_cerr("Write is not working on this chip. ");
David Hendricksc801adb2010-12-09 16:58:56 -08001791 if (!force)
hailfinger0459e1c2009-08-19 13:55:34 +00001792 return 1;
David Hendricksc801adb2010-12-09 16:58:56 -08001793 msg_cerr("Continuing anyway.\n");
hailfinger0459e1c2009-08-19 13:55:34 +00001794 }
David Hendricksc801adb2010-12-09 16:58:56 -08001795 if (!flash->write) {
1796 msg_cerr("flashrom has no write function for this "
1797 "flash chip.\n");
stugebbcc2f12009-01-12 21:00:35 +00001798 return 1;
1799 }
ollie5672ac62004-03-17 22:22:08 +00001800 }
David Hendricksc801adb2010-12-09 16:58:56 -08001801 return 0;
1802}
1803
1804/* This function signature is horrible. We need to design a better interface,
1805 * but right now it allows us to split off the CLI code.
1806 * Besides that, the function itself is a textbook example of abysmal code flow.
1807 */
1808int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1809{
1810 uint8_t *oldcontents;
1811 uint8_t *newcontents;
1812 int ret = 0;
1813 unsigned long size = flash->total_size * 1024;
1814
1815 if (chip_safety_check(flash, force, filename, read_it, write_it, erase_it, verify_it)) {
1816 msg_cerr("Aborting.\n");
1817 ret = 1;
1818 goto out_nofree;
1819 }
1820
1821 /* Given the existence of read locks, we want to unlock for read,
1822 * erase and write.
1823 */
1824 if (flash->unlock)
1825 flash->unlock(flash);
1826
1827 if (read_it) {
1828 ret = read_flash_to_file(flash, filename);
1829 goto out_nofree;
1830 }
1831
1832 oldcontents = (uint8_t *) malloc(size);
1833 /* Assume worst case: All bits are 0. */
1834 memset(oldcontents, 0x00, size);
1835 newcontents = (uint8_t *) malloc(size);
1836 /* Assume best case: All bits should be 1. */
1837 memset(newcontents, 0xff, size);
1838 /* Side effect of the assumptions above: Default write action is erase
1839 * because newcontents looks like a completely erased chip, and
1840 * oldcontents being completely 0x00 means we have to erase everything
1841 * before we can write.
1842 */
1843
1844 if (erase_it) {
1845 /* FIXME: Do we really want the scary warning if erase failed?
1846 * After all, after erase the chip is either blank or partially
1847 * blank or it has the old contents. A blank chip won't boot,
1848 * so if the user wanted erase and reboots afterwards, the user
1849 * knows very well that booting won't work.
1850 */
1851 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1852 emergency_help_message();
1853 ret = 1;
1854 }
1855 goto out;
1856 }
1857
1858 if (write_it || verify_it) {
1859 if (read_buf_from_file(newcontents, size, filename)) {
1860 ret = 1;
1861 goto out;
1862 }
1863
1864#if CONFIG_INTERNAL == 1
1865 if (programmer == PROGRAMMER_INTERNAL)
1866 show_id(newcontents, size, force);
1867#endif
1868 }
1869
1870 /* Read the whole chip to be able to check whether regions need to be
1871 * erased and to give better diagnostics in case write fails.
1872 * The alternative would be to read only the regions which are to be
1873 * preserved, but in that case we might perform unneeded erase which
1874 * takes time as well.
1875 */
1876 msg_cdbg("Reading old flash chip contents...\n");
1877 if (flash->read(flash, oldcontents, 0, size)) {
1878 ret = 1;
1879 goto out;
1880 }
ollie5672ac62004-03-17 22:22:08 +00001881
ollie6a600992005-11-26 21:55:36 +00001882 // This should be moved into each flash part's code to do it
1883 // cleanly. This does the job.
David Hendricksc801adb2010-12-09 16:58:56 -08001884 handle_romentries(flash, oldcontents, newcontents);
uwef6641642007-05-09 10:17:44 +00001885
ollie6a600992005-11-26 21:55:36 +00001886 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00001887
stuge8ce3a3c2008-04-28 14:47:30 +00001888 if (write_it) {
David Hendricksc801adb2010-12-09 16:58:56 -08001889 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1890 msg_cerr("Uh oh. Erase/write failed. Checking if "
1891 "anything changed.\n");
1892 if (!flash->read(flash, newcontents, 0, size)) {
1893 if (!memcmp(oldcontents, newcontents, size)) {
1894 msg_cinfo("Good. It seems nothing was "
1895 "changed.\n");
1896 nonfatal_help_message();
1897 ret = 1;
1898 goto out;
1899 }
1900 }
hailfinger0459e1c2009-08-19 13:55:34 +00001901 emergency_help_message();
David Hendricksc801adb2010-12-09 16:58:56 -08001902 ret = 1;
1903 goto out;
hailfingerdffbe6b2009-07-24 12:18:54 +00001904 }
stuge8ce3a3c2008-04-28 14:47:30 +00001905 }
ollie6a600992005-11-26 21:55:36 +00001906
hailfinger0459e1c2009-08-19 13:55:34 +00001907 if (verify_it) {
1908 /* Work around chips which need some time to calm down. */
1909 if (write_it)
1910 programmer_delay(1000*1000);
David Hendricksc801adb2010-12-09 16:58:56 -08001911 ret = verify_flash(flash, newcontents);
hailfingera50d60e2009-11-17 09:57:34 +00001912 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00001913 * might have an emergency situation.
1914 */
1915 if (ret && write_it)
1916 emergency_help_message();
1917 }
ollie6a600992005-11-26 21:55:36 +00001918
David Hendricksc801adb2010-12-09 16:58:56 -08001919out:
1920 free(oldcontents);
1921 free(newcontents);
1922out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07001923 chip_restore(); /* must be done before programmer_shutdown() */
hailfingerabe249e2009-05-08 17:43:22 +00001924
stepan83eca252006-01-04 16:42:57 +00001925 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001926}