blob: 5353f2abb2010eff6b4449f69e57dec0c71b7dd6 [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 Hendricks668f29d2011-01-27 18:51:45 -080041const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000042char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000043int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000044
hailfinger90c7d542010-05-31 15:27:27 +000045#if CONFIG_INTERNAL == 1
hailfinger6fe23d62009-08-12 11:39:29 +000046enum programmer programmer = PROGRAMMER_INTERNAL;
hailfinger90c7d542010-05-31 15:27:27 +000047#elif CONFIG_DUMMY == 1
hailfinger80422e22009-12-13 22:28:00 +000048enum programmer programmer = PROGRAMMER_DUMMY;
49#else
hailfingerf95c8f62010-01-10 13:28:48 +000050/* If neither internal nor dummy are selected, we must pick a sensible default.
51 * Since there is no reason to prefer a particular external programmer, we fail
52 * if more than one of them is selected. If only one is selected, it is clear
53 * that the user wants that one to become the default.
54 */
David Hendricks668f29d2011-01-27 18:51:45 -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+CONFIG_OGP_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
David Hendricks668f29d2011-01-27 18:51:45 -080099#if CONFIG_OGP_SPI == 1
100 PROGRAMMER_OGP_SPI
101#endif
hailfingerf95c8f62010-01-10 13:28:48 +0000102;
hailfinger80422e22009-12-13 22:28:00 +0000103#endif
104
David Hendricks82fd8ae2010-08-04 14:34:54 -0700105static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +0000106
David Hendricks82fd8ae2010-08-04 14:34:54 -0700107/* Supported buses for the current programmer. */
108enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +0000109
David Hendricksc801adb2010-12-09 16:58:56 -0800110/*
hailfinger80422e22009-12-13 22:28:00 +0000111 * Programmers supporting multiple buses can have differing size limits on
112 * each bus. Store the limits for each bus in a common struct.
113 */
David Hendricks82fd8ae2010-08-04 14:34:54 -0700114struct decode_sizes max_rom_decode;
115
116/* If nonzero, used as the start address of bottom-aligned flash. */
117unsigned long flashbase;
118
119/* Is writing allowed with this programmer? */
120int programmer_may_write;
hailfinger80422e22009-12-13 22:28:00 +0000121
hailfingerabe249e2009-05-08 17:43:22 +0000122const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +0000123#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +0000124 {
hailfinger3548a9a2009-08-12 14:34:35 +0000125 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +0000126 .init = internal_init,
127 .shutdown = internal_shutdown,
hailfinger11ae3c42009-05-11 14:13:25 +0000128 .map_flash_region = physmap,
129 .unmap_flash_region = physunmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000130 .chip_readb = internal_chip_readb,
131 .chip_readw = internal_chip_readw,
132 .chip_readl = internal_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000133 .chip_readn = internal_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000134 .chip_writeb = internal_chip_writeb,
135 .chip_writew = internal_chip_writew,
136 .chip_writel = internal_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000137 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000138 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +0000139 },
hailfinger80422e22009-12-13 22:28:00 +0000140#endif
stepan927d4e22007-04-04 22:45:58 +0000141
hailfinger90c7d542010-05-31 15:27:27 +0000142#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000143 {
hailfinger3548a9a2009-08-12 14:34:35 +0000144 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000145 .init = dummy_init,
146 .shutdown = dummy_shutdown,
hailfinger11ae3c42009-05-11 14:13:25 +0000147 .map_flash_region = dummy_map,
148 .unmap_flash_region = dummy_unmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000149 .chip_readb = dummy_chip_readb,
150 .chip_readw = dummy_chip_readw,
151 .chip_readl = dummy_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000152 .chip_readn = dummy_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000153 .chip_writeb = dummy_chip_writeb,
154 .chip_writew = dummy_chip_writew,
155 .chip_writel = dummy_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000156 .chip_writen = dummy_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000157 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000158 },
hailfinger571a6b32009-09-16 10:09:21 +0000159#endif
hailfingera9df33c2009-05-09 00:54:55 +0000160
hailfinger90c7d542010-05-31 15:27:27 +0000161#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000162 {
hailfinger3548a9a2009-08-12 14:34:35 +0000163 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000164 .init = nic3com_init,
165 .shutdown = nic3com_shutdown,
uwe3e656bd2009-05-17 23:12:17 +0000166 .map_flash_region = fallback_map,
167 .unmap_flash_region = fallback_unmap,
uwe0f5a3a22009-05-13 11:36:06 +0000168 .chip_readb = nic3com_chip_readb,
hailfinger43716942009-05-16 01:23:55 +0000169 .chip_readw = fallback_chip_readw,
170 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000171 .chip_readn = fallback_chip_readn,
uwe0f5a3a22009-05-13 11:36:06 +0000172 .chip_writeb = nic3com_chip_writeb,
hailfinger43716942009-05-16 01:23:55 +0000173 .chip_writew = fallback_chip_writew,
174 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000175 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000176 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000177 },
hailfinger571a6b32009-09-16 10:09:21 +0000178#endif
uwe0f5a3a22009-05-13 11:36:06 +0000179
hailfinger90c7d542010-05-31 15:27:27 +0000180#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000181 {
182 .name = "nicrealtek",
183 .init = nicrealtek_init,
184 .shutdown = nicrealtek_shutdown,
185 .map_flash_region = fallback_map,
186 .unmap_flash_region = fallback_unmap,
187 .chip_readb = nicrealtek_chip_readb,
188 .chip_readw = fallback_chip_readw,
189 .chip_readl = fallback_chip_readl,
190 .chip_readn = fallback_chip_readn,
191 .chip_writeb = nicrealtek_chip_writeb,
192 .chip_writew = fallback_chip_writew,
193 .chip_writel = fallback_chip_writel,
194 .chip_writen = fallback_chip_writen,
195 .delay = internal_delay,
196 },
197 {
198 .name = "nicsmc1211",
199 .init = nicsmc1211_init,
200 .shutdown = nicrealtek_shutdown,
201 .map_flash_region = fallback_map,
202 .unmap_flash_region = fallback_unmap,
203 .chip_readb = nicrealtek_chip_readb,
204 .chip_readw = fallback_chip_readw,
205 .chip_readl = fallback_chip_readl,
206 .chip_readn = fallback_chip_readn,
207 .chip_writeb = nicrealtek_chip_writeb,
208 .chip_writew = fallback_chip_writew,
209 .chip_writel = fallback_chip_writel,
210 .chip_writen = fallback_chip_writen,
211 .delay = internal_delay,
212 },
213#endif
214
hailfingerf0a368f2010-06-07 22:37:54 +0000215#if CONFIG_NICNATSEMI == 1
216 {
217 .name = "nicnatsemi",
218 .init = nicnatsemi_init,
219 .shutdown = nicnatsemi_shutdown,
220 .map_flash_region = fallback_map,
221 .unmap_flash_region = fallback_unmap,
222 .chip_readb = nicnatsemi_chip_readb,
223 .chip_readw = fallback_chip_readw,
224 .chip_readl = fallback_chip_readl,
225 .chip_readn = fallback_chip_readn,
226 .chip_writeb = nicnatsemi_chip_writeb,
227 .chip_writew = fallback_chip_writew,
228 .chip_writel = fallback_chip_writel,
229 .chip_writen = fallback_chip_writen,
230 .delay = internal_delay,
231 },
232#endif
hailfinger5aa36982010-05-21 21:54:07 +0000233
hailfinger90c7d542010-05-31 15:27:27 +0000234#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000235 {
236 .name = "gfxnvidia",
237 .init = gfxnvidia_init,
238 .shutdown = gfxnvidia_shutdown,
239 .map_flash_region = fallback_map,
240 .unmap_flash_region = fallback_unmap,
241 .chip_readb = gfxnvidia_chip_readb,
242 .chip_readw = fallback_chip_readw,
243 .chip_readl = fallback_chip_readl,
244 .chip_readn = fallback_chip_readn,
245 .chip_writeb = gfxnvidia_chip_writeb,
246 .chip_writew = fallback_chip_writew,
247 .chip_writel = fallback_chip_writel,
248 .chip_writen = fallback_chip_writen,
249 .delay = internal_delay,
250 },
251#endif
252
hailfinger90c7d542010-05-31 15:27:27 +0000253#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000254 {
uwee2f95ef2009-09-02 23:00:46 +0000255 .name = "drkaiser",
256 .init = drkaiser_init,
257 .shutdown = drkaiser_shutdown,
258 .map_flash_region = fallback_map,
259 .unmap_flash_region = fallback_unmap,
260 .chip_readb = drkaiser_chip_readb,
261 .chip_readw = fallback_chip_readw,
262 .chip_readl = fallback_chip_readl,
263 .chip_readn = fallback_chip_readn,
264 .chip_writeb = drkaiser_chip_writeb,
265 .chip_writew = fallback_chip_writew,
266 .chip_writel = fallback_chip_writel,
267 .chip_writen = fallback_chip_writen,
268 .delay = internal_delay,
269 },
hailfinger571a6b32009-09-16 10:09:21 +0000270#endif
uwee2f95ef2009-09-02 23:00:46 +0000271
hailfinger90c7d542010-05-31 15:27:27 +0000272#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000273 {
hailfinger3548a9a2009-08-12 14:34:35 +0000274 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000275 .init = satasii_init,
276 .shutdown = satasii_shutdown,
uwe3e656bd2009-05-17 23:12:17 +0000277 .map_flash_region = fallback_map,
278 .unmap_flash_region = fallback_unmap,
ruikda922a12009-05-17 19:39:27 +0000279 .chip_readb = satasii_chip_readb,
280 .chip_readw = fallback_chip_readw,
281 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000282 .chip_readn = fallback_chip_readn,
ruikda922a12009-05-17 19:39:27 +0000283 .chip_writeb = satasii_chip_writeb,
284 .chip_writew = fallback_chip_writew,
285 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000286 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000287 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000288 },
hailfinger571a6b32009-09-16 10:09:21 +0000289#endif
ruikda922a12009-05-17 19:39:27 +0000290
hailfinger90c7d542010-05-31 15:27:27 +0000291#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000292 {
293 .name = "atahpt",
294 .init = atahpt_init,
295 .shutdown = atahpt_shutdown,
296 .map_flash_region = fallback_map,
297 .unmap_flash_region = fallback_unmap,
298 .chip_readb = atahpt_chip_readb,
299 .chip_readw = fallback_chip_readw,
300 .chip_readl = fallback_chip_readl,
301 .chip_readn = fallback_chip_readn,
302 .chip_writeb = atahpt_chip_writeb,
303 .chip_writew = fallback_chip_writew,
304 .chip_writel = fallback_chip_writel,
305 .chip_writen = fallback_chip_writen,
306 .delay = internal_delay,
307 },
308#endif
309
hailfinger90c7d542010-05-31 15:27:27 +0000310#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000311#if defined(__i386__) || defined(__x86_64__)
hailfinger26e212b2009-05-31 18:00:57 +0000312 {
hailfinger3548a9a2009-08-12 14:34:35 +0000313 .name = "it87spi",
hailfinger26e212b2009-05-31 18:00:57 +0000314 .init = it87spi_init,
hailfinger571a6b32009-09-16 10:09:21 +0000315 .shutdown = noop_shutdown,
hailfinger6fe23d62009-08-12 11:39:29 +0000316 .map_flash_region = fallback_map,
317 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000318 .chip_readb = noop_chip_readb,
hailfinger9d987ef2009-06-05 18:32:07 +0000319 .chip_readw = fallback_chip_readw,
320 .chip_readl = fallback_chip_readl,
321 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000322 .chip_writeb = noop_chip_writeb,
hailfinger9d987ef2009-06-05 18:32:07 +0000323 .chip_writew = fallback_chip_writew,
324 .chip_writel = fallback_chip_writel,
325 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000326 .delay = internal_delay,
hailfinger26e212b2009-05-31 18:00:57 +0000327 },
hailfinger80422e22009-12-13 22:28:00 +0000328#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000329#endif
hailfinger26e212b2009-05-31 18:00:57 +0000330
hailfinger90c7d542010-05-31 15:27:27 +0000331#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000332 {
hailfinger90c7d542010-05-31 15:27:27 +0000333 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000334 .init = ft2232_spi_init,
hailfinger571a6b32009-09-16 10:09:21 +0000335 .shutdown = noop_shutdown, /* Missing shutdown */
hailfinger6fe23d62009-08-12 11:39:29 +0000336 .map_flash_region = fallback_map,
337 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000338 .chip_readb = noop_chip_readb,
hailfingerf31da3d2009-06-16 21:08:06 +0000339 .chip_readw = fallback_chip_readw,
340 .chip_readl = fallback_chip_readl,
341 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000342 .chip_writeb = noop_chip_writeb,
hailfingerf31da3d2009-06-16 21:08:06 +0000343 .chip_writew = fallback_chip_writew,
344 .chip_writel = fallback_chip_writel,
345 .chip_writen = fallback_chip_writen,
346 .delay = internal_delay,
347 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000348#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000349
hailfinger90c7d542010-05-31 15:27:27 +0000350#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000351 {
hailfinger3548a9a2009-08-12 14:34:35 +0000352 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000353 .init = serprog_init,
354 .shutdown = serprog_shutdown,
355 .map_flash_region = fallback_map,
356 .unmap_flash_region = fallback_unmap,
357 .chip_readb = serprog_chip_readb,
358 .chip_readw = fallback_chip_readw,
359 .chip_readl = fallback_chip_readl,
360 .chip_readn = serprog_chip_readn,
361 .chip_writeb = serprog_chip_writeb,
362 .chip_writew = fallback_chip_writew,
363 .chip_writel = fallback_chip_writel,
364 .chip_writen = fallback_chip_writen,
365 .delay = serprog_delay,
366 },
hailfinger74d88a72009-08-12 16:17:41 +0000367#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000368
hailfinger90c7d542010-05-31 15:27:27 +0000369#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000370 {
hailfinger90c7d542010-05-31 15:27:27 +0000371 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000372 .init = buspirate_spi_init,
373 .shutdown = buspirate_spi_shutdown,
374 .map_flash_region = fallback_map,
375 .unmap_flash_region = fallback_unmap,
376 .chip_readb = noop_chip_readb,
377 .chip_readw = fallback_chip_readw,
378 .chip_readl = fallback_chip_readl,
379 .chip_readn = fallback_chip_readn,
380 .chip_writeb = noop_chip_writeb,
381 .chip_writew = fallback_chip_writew,
382 .chip_writel = fallback_chip_writel,
383 .chip_writen = fallback_chip_writen,
384 .delay = internal_delay,
385 },
386#endif
387
hailfinger90c7d542010-05-31 15:27:27 +0000388#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000389 {
390 .name = "dediprog",
391 .init = dediprog_init,
392 .shutdown = dediprog_shutdown,
393 .map_flash_region = fallback_map,
394 .unmap_flash_region = fallback_unmap,
395 .chip_readb = noop_chip_readb,
396 .chip_readw = fallback_chip_readw,
397 .chip_readl = fallback_chip_readl,
398 .chip_readn = fallback_chip_readn,
399 .chip_writeb = noop_chip_writeb,
400 .chip_writew = fallback_chip_writew,
401 .chip_writel = fallback_chip_writel,
402 .chip_writen = fallback_chip_writen,
403 .delay = internal_delay,
404 },
405#endif
406
David Hendricks82fd8ae2010-08-04 14:34:54 -0700407#if CONFIG_RAYER_SPI == 1
408 {
409 .name = "rayer_spi",
410 .init = rayer_spi_init,
411 .shutdown = noop_shutdown,
412 .map_flash_region = fallback_map,
413 .unmap_flash_region = fallback_unmap,
414 .chip_readb = noop_chip_readb,
415 .chip_readw = fallback_chip_readw,
416 .chip_readl = fallback_chip_readl,
417 .chip_readn = fallback_chip_readn,
418 .chip_writeb = noop_chip_writeb,
419 .chip_writew = fallback_chip_writew,
420 .chip_writel = fallback_chip_writel,
421 .chip_writen = fallback_chip_writen,
422 .delay = internal_delay,
423 },
424#endif
425
David Hendricksc801adb2010-12-09 16:58:56 -0800426#if CONFIG_NICINTEL_SPI == 1
427 {
428 .name = "nicintel_spi",
429 .init = nicintel_spi_init,
430 .shutdown = nicintel_spi_shutdown,
431 .map_flash_region = fallback_map,
432 .unmap_flash_region = fallback_unmap,
433 .chip_readb = noop_chip_readb,
434 .chip_readw = fallback_chip_readw,
435 .chip_readl = fallback_chip_readl,
436 .chip_readn = fallback_chip_readn,
437 .chip_writeb = noop_chip_writeb,
438 .chip_writew = fallback_chip_writew,
439 .chip_writel = fallback_chip_writel,
440 .chip_writen = fallback_chip_writen,
441 .delay = internal_delay,
442 },
443#endif
444
David Hendricks668f29d2011-01-27 18:51:45 -0800445#if CONFIG_OGP_SPI == 1
446 {
447 .name = "ogp_spi",
448 .init = ogp_spi_init,
449 .shutdown = ogp_spi_shutdown,
450 .map_flash_region = fallback_map,
451 .unmap_flash_region = fallback_unmap,
452 .chip_readb = noop_chip_readb,
453 .chip_readw = fallback_chip_readw,
454 .chip_readl = fallback_chip_readl,
455 .chip_readn = fallback_chip_readn,
456 .chip_writeb = noop_chip_writeb,
457 .chip_writew = fallback_chip_writew,
458 .chip_writel = fallback_chip_writel,
459 .chip_writen = fallback_chip_writen,
460 .delay = internal_delay,
461 },
462#endif
463
hailfinger3548a9a2009-08-12 14:34:35 +0000464 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000465};
stepan927d4e22007-04-04 22:45:58 +0000466
David Hendricksbf36f092010-11-02 23:39:29 -0700467#define CHIP_RESTORE_MAXFN 4
468static int chip_restore_fn_count = 0;
469struct chip_restore_func_data {
470 CHIP_RESTORE_CALLBACK;
471 struct flashchip *flash;
472 uint8_t status;
473} static chip_restore_fn[CHIP_RESTORE_MAXFN];
474
David Hendricks668f29d2011-01-27 18:51:45 -0800475
David Hendricksc801adb2010-12-09 16:58:56 -0800476#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000477static int shutdown_fn_count = 0;
478struct shutdown_func_data {
479 void (*func) (void *data);
480 void *data;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700481} static shutdown_fn[SHUTDOWN_MAXFN];
482/* Initialize to 0 to make sure nobody registers a shutdown function before
483 * programmer init.
484 */
485static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000486
487/* Register a function to be executed on programmer shutdown.
488 * The advantage over atexit() is that you can supply a void pointer which will
489 * be used as parameter to the registered function upon programmer shutdown.
490 * This pointer can point to arbitrary data used by said function, e.g. undo
491 * information for GPIO settings etc. If unneeded, set data=NULL.
492 * Please note that the first (void *data) belongs to the function signature of
493 * the function passed as first parameter.
494 */
495int register_shutdown(void (*function) (void *data), void *data)
496{
497 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000498 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000499 SHUTDOWN_MAXFN);
500 return 1;
501 }
David Hendricks82fd8ae2010-08-04 14:34:54 -0700502 if (!may_register_shutdown) {
503 msg_perr("Tried to register a shutdown function before "
504 "programmer init.\n");
505 return 1;
506 }
hailfingerdc6f7972010-02-14 01:20:28 +0000507 shutdown_fn[shutdown_fn_count].func = function;
508 shutdown_fn[shutdown_fn_count].data = data;
509 shutdown_fn_count++;
510
511 return 0;
512}
513
David Hendricksbf36f092010-11-02 23:39:29 -0700514//int register_chip_restore(int (*function) (void *data), void *data)
515int register_chip_restore(CHIP_RESTORE_CALLBACK,
516 struct flashchip *flash, uint8_t status)
517{
518 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
519 msg_perr("Tried to register more than %i chip restore"
520 " functions.\n", CHIP_RESTORE_MAXFN);
521 return 1;
522 }
523 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
524 chip_restore_fn[chip_restore_fn_count].flash = flash;
525 chip_restore_fn[chip_restore_fn_count].status = status;
526 chip_restore_fn_count++;
527
528 return 0;
529}
530
David Hendricks82fd8ae2010-08-04 14:34:54 -0700531int programmer_init(char *param)
uweabe92a52009-05-16 22:36:00 +0000532{
David Hendricks82fd8ae2010-08-04 14:34:54 -0700533 int ret;
534 /* Initialize all programmer specific data. */
535 /* Default to unlimited decode sizes. */
536 max_rom_decode = (const struct decode_sizes) {
537 .parallel = 0xffffffff,
538 .lpc = 0xffffffff,
539 .fwh = 0xffffffff,
540 .spi = 0xffffffff
541 };
542 /* Default to Parallel/LPC/FWH flash devices. If a known host controller
543 * is found, the init routine sets the buses_supported bitfield.
544 */
545 buses_supported = CHIP_BUSTYPE_NONSPI;
546 /* Default to top aligned flash at 4 GB. */
547 flashbase = 0;
548 /* Registering shutdown functions is now allowed. */
549 may_register_shutdown = 1;
550 /* Default to allowing writes. Broken programmers set this to 0. */
551 programmer_may_write = 1;
552
553 programmer_param = param;
554 msg_pdbg("Initializing %s programmer\n",
555 programmer_table[programmer].name);
556 ret = programmer_table[programmer].init();
557 if (programmer_param && strlen(programmer_param)) {
558 msg_perr("Unhandled programmer parameters: %s\n",
559 programmer_param);
560 /* Do not error out here, the init itself was successful. */
561 }
562 return ret;
uweabe92a52009-05-16 22:36:00 +0000563}
564
David Hendricksbf36f092010-11-02 23:39:29 -0700565int chip_restore()
566{
567 int rc = 0;
568
569 while (chip_restore_fn_count > 0) {
570 int i = --chip_restore_fn_count;
571 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
572 chip_restore_fn[i].status);
573 }
574
575 return rc;
576}
577
uweabe92a52009-05-16 22:36:00 +0000578int programmer_shutdown(void)
579{
David Hendricks82fd8ae2010-08-04 14:34:54 -0700580 /* Registering shutdown functions is no longer allowed. */
581 may_register_shutdown = 0;
582 while (shutdown_fn_count > 0) {
583 int i = --shutdown_fn_count;
hailfingerdc6f7972010-02-14 01:20:28 +0000584 shutdown_fn[i].func(shutdown_fn[i].data);
David Hendricks82fd8ae2010-08-04 14:34:54 -0700585 }
uweabe92a52009-05-16 22:36:00 +0000586 return programmer_table[programmer].shutdown();
587}
588
589void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
590 size_t len)
591{
592 return programmer_table[programmer].map_flash_region(descr,
593 phys_addr, len);
594}
595
596void programmer_unmap_flash_region(void *virt_addr, size_t len)
597{
598 programmer_table[programmer].unmap_flash_region(virt_addr, len);
599}
600
601void chip_writeb(uint8_t val, chipaddr addr)
602{
603 programmer_table[programmer].chip_writeb(val, addr);
604}
605
606void chip_writew(uint16_t val, chipaddr addr)
607{
608 programmer_table[programmer].chip_writew(val, addr);
609}
610
611void chip_writel(uint32_t val, chipaddr addr)
612{
613 programmer_table[programmer].chip_writel(val, addr);
614}
615
hailfinger9d987ef2009-06-05 18:32:07 +0000616void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
617{
618 programmer_table[programmer].chip_writen(buf, addr, len);
619}
620
uweabe92a52009-05-16 22:36:00 +0000621uint8_t chip_readb(const chipaddr addr)
622{
623 return programmer_table[programmer].chip_readb(addr);
624}
625
626uint16_t chip_readw(const chipaddr addr)
627{
628 return programmer_table[programmer].chip_readw(addr);
629}
630
631uint32_t chip_readl(const chipaddr addr)
632{
633 return programmer_table[programmer].chip_readl(addr);
634}
635
hailfinger9d987ef2009-06-05 18:32:07 +0000636void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
637{
638 programmer_table[programmer].chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000639}
640
hailfingere5829f62009-06-05 17:48:08 +0000641void programmer_delay(int usecs)
642{
643 programmer_table[programmer].delay(usecs);
644}
645
stuge5ff0e6c2009-01-26 00:39:57 +0000646void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000647{
stepan15e64bc2007-05-24 08:48:10 +0000648 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000649 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000650 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfinger82719632009-05-16 21:22:56 +0000651 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000652}
653
hailfinger0f08b7a2009-06-16 08:55:44 +0000654int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger23060112009-05-08 12:49:03 +0000655{
hailfinger0f08b7a2009-06-16 08:55:44 +0000656 chip_readn(buf, flash->virtual_memory + start, len);
hailfinger23060112009-05-08 12:49:03 +0000657
658 return 0;
659}
660
hailfinger7b414742009-06-13 12:04:03 +0000661int min(int a, int b)
662{
663 return (a < b) ? a : b;
664}
665
hailfinger7af83692009-06-15 17:23:36 +0000666int max(int a, int b)
667{
668 return (a > b) ? a : b;
669}
670
hailfingeraec9c962009-10-31 01:53:09 +0000671int bitcount(unsigned long a)
672{
673 int i = 0;
674 for (; a != 0; a >>= 1)
675 if (a & 1)
676 i++;
677 return i;
678}
679
David Hendricksc801adb2010-12-09 16:58:56 -0800680void tolower_string(char *str)
681{
682 for (; *str != '\0'; str++)
683 *str = (char)tolower((unsigned char)*str);
684}
685
hailfingera916b422009-06-01 02:08:58 +0000686char *strcat_realloc(char *dest, const char *src)
687{
688 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
David Hendricks82fd8ae2010-08-04 14:34:54 -0700689 if (!dest) {
690 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000691 return NULL;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700692 }
hailfingera916b422009-06-01 02:08:58 +0000693 strcat(dest, src);
694 return dest;
695}
696
hailfinger6e5a52a2009-11-24 18:27:10 +0000697/* This is a somewhat hacked function similar in some ways to strtok().
David Hendricks82fd8ae2010-08-04 14:34:54 -0700698 * It will look for needle with a subsequent '=' in haystack, return a copy of
699 * needle and remove everything from the first occurrence of needle to the next
700 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000701 */
702char *extract_param(char **haystack, char *needle, char *delim)
703{
David Hendricks82fd8ae2010-08-04 14:34:54 -0700704 char *param_pos, *opt_pos, *rest;
705 char *opt = NULL;
706 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000707 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000708
hailfingerf4aaccc2010-04-28 15:22:14 +0000709 needlelen = strlen(needle);
710 if (!needlelen) {
711 msg_gerr("%s: empty needle! Please report a bug at "
712 "flashrom@flashrom.org\n", __func__);
713 return NULL;
714 }
715 /* No programmer parameters given. */
716 if (*haystack == NULL)
717 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000718 param_pos = strstr(*haystack, needle);
719 do {
720 if (!param_pos)
721 return NULL;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700722 /* Needle followed by '='? */
723 if (param_pos[needlelen] == '=') {
724
725 /* Beginning of the string? */
726 if (param_pos == *haystack)
727 break;
728 /* After a delimiter? */
729 if (strchr(delim, *(param_pos - 1)))
730 break;
731 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000732 /* Continue searching. */
733 param_pos++;
734 param_pos = strstr(param_pos, needle);
735 } while (1);
David Hendricks82fd8ae2010-08-04 14:34:54 -0700736
hailfinger6e5a52a2009-11-24 18:27:10 +0000737 if (param_pos) {
David Hendricks82fd8ae2010-08-04 14:34:54 -0700738 /* Get the string after needle and '='. */
739 opt_pos = param_pos + needlelen + 1;
740 optlen = strcspn(opt_pos, delim);
741 /* Return an empty string if the parameter was empty. */
742 opt = malloc(optlen + 1);
743 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000744 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000745 exit(1);
746 }
David Hendricks82fd8ae2010-08-04 14:34:54 -0700747 strncpy(opt, opt_pos, optlen);
748 opt[optlen] = '\0';
749 rest = opt_pos + optlen;
750 /* Skip all delimiters after the current parameter. */
751 rest += strspn(rest, delim);
752 memmove(param_pos, rest, strlen(rest) + 1);
753 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000754 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000755
David Hendricks82fd8ae2010-08-04 14:34:54 -0700756 return opt;
757}
758
759char *extract_programmer_param(char *param_name)
760{
761 return extract_param(&programmer_param, param_name, ",");
hailfinger6e5a52a2009-11-24 18:27:10 +0000762}
763
hailfinger7af83692009-06-15 17:23:36 +0000764/* start is an offset to the base address of the flash chip */
765int check_erased_range(struct flashchip *flash, int start, int len)
766{
767 int ret;
768 uint8_t *cmpbuf = malloc(len);
769
770 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000771 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000772 exit(1);
773 }
774 memset(cmpbuf, 0xff, len);
775 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
776 free(cmpbuf);
777 return ret;
778}
779
David Hendricksc801adb2010-12-09 16:58:56 -0800780/*
hailfinger7af3d192009-11-25 17:05:52 +0000781 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
782 flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000783 * @start offset to the base address of the flash chip
784 * @len length of the verified area
785 * @message string to print in the "FAILED" message
786 * @return 0 for success, -1 for failure
787 */
788int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
789{
David Hendricksc801adb2010-12-09 16:58:56 -0800790 int i, ret = 0;
791 uint8_t *readbuf = malloc(len);
hailfinger5be6c0f2009-07-23 01:42:56 +0000792 int failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000793
794 if (!len)
795 goto out_free;
796
hailfingerb0f4d122009-06-24 08:20:45 +0000797 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000798 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000799 return 1;
800 }
hailfinger7af83692009-06-15 17:23:36 +0000801 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000802 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000803 exit(1);
804 }
805
806 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000807 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000808 " total_size 0x%x\n", __func__, start, len,
809 flash->total_size * 1024);
810 ret = -1;
811 goto out_free;
812 }
813 if (!message)
814 message = "VERIFY";
815
David Hendricksc801adb2010-12-09 16:58:56 -0800816 ret = flash->read(flash, readbuf, start, len);
817 if (ret) {
818 msg_gerr("Verification impossible because read failed "
819 "at 0x%x (len 0x%x)\n", start, len);
820 return ret;
821 }
822
823 for (i = 0; i < len; i++) {
824 if (cmpbuf[i] != readbuf[i]) {
825 /* Only print the first failure. */
826 if (!failcount++)
827 msg_cerr("%s FAILED at 0x%08x! "
828 "Expected=0x%02x, Read=0x%02x,",
829 message, start + i, cmpbuf[i],
830 readbuf[i]);
hailfinger7af83692009-06-15 17:23:36 +0000831 }
832 }
hailfinger5be6c0f2009-07-23 01:42:56 +0000833 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000834 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
hailfinger5be6c0f2009-07-23 01:42:56 +0000835 start, start + len - 1, failcount);
836 ret = -1;
837 }
hailfinger7af83692009-06-15 17:23:36 +0000838
839out_free:
840 free(readbuf);
841 return ret;
842}
843
David Hendricksc801adb2010-12-09 16:58:56 -0800844/*
hailfingerb247c7a2010-03-08 00:42:32 +0000845 * Check if the buffer @have can be programmed to the content of @want without
846 * erasing. This is only possible if all chunks of size @gran are either kept
847 * as-is or changed from an all-ones state to any other state.
David Hendricksc801adb2010-12-09 16:58:56 -0800848 *
hailfingerb247c7a2010-03-08 00:42:32 +0000849 * The following write granularities (enum @gran) are known:
850 * - 1 bit. Each bit can be cleared individually.
851 * - 1 byte. A byte can be written once. Further writes to an already written
852 * byte cause the contents to be either undefined or to stay unchanged.
853 * - 128 bytes. If less than 128 bytes are written, the rest will be
854 * erased. Each write to a 128-byte region will trigger an automatic erase
855 * before anything is written. Very uncommon behaviour and unsupported by
856 * this function.
857 * - 256 bytes. If less than 256 bytes are written, the contents of the
858 * unwritten bytes are undefined.
David Hendricksc801adb2010-12-09 16:58:56 -0800859 * Warning: This function assumes that @have and @want point to naturally
860 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000861 *
862 * @have buffer with current content
863 * @want buffer with desired content
David Hendricksc801adb2010-12-09 16:58:56 -0800864 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000865 * @gran write granularity (enum, not count)
866 * @return 0 if no erase is needed, 1 otherwise
867 */
868int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
869{
870 int result = 0;
871 int i, j, limit;
872
873 switch (gran) {
874 case write_gran_1bit:
875 for (i = 0; i < len; i++)
876 if ((have[i] & want[i]) != want[i]) {
877 result = 1;
878 break;
879 }
880 break;
881 case write_gran_1byte:
882 for (i = 0; i < len; i++)
883 if ((have[i] != want[i]) && (have[i] != 0xff)) {
884 result = 1;
885 break;
886 }
887 break;
888 case write_gran_256bytes:
889 for (j = 0; j < len / 256; j++) {
890 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000891 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000892 if (!memcmp(have + j * 256, want + j * 256, limit))
893 continue;
894 /* have needs to be in erased state. */
895 for (i = 0; i < limit; i++)
David Hendricksc801adb2010-12-09 16:58:56 -0800896 if (have[j * 256 + i] != 0xff) {
hailfingerb247c7a2010-03-08 00:42:32 +0000897 result = 1;
898 break;
899 }
900 if (result)
901 break;
902 }
903 break;
David Hendricksc801adb2010-12-09 16:58:56 -0800904 default:
905 msg_cerr("%s: Unsupported granularity! Please report a bug at "
906 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000907 }
908 return result;
909}
910
David Hendricksc801adb2010-12-09 16:58:56 -0800911/**
912 * Check if the buffer @have needs to be programmed to get the content of @want.
913 * If yes, return 1 and fill in first_start with the start address of the
914 * write operation and first_len with the length of the first to-be-written
915 * chunk. If not, return 0 and leave first_start and first_len undefined.
916 *
917 * Warning: This function assumes that @have and @want point to naturally
918 * aligned regions.
919 *
920 * @have buffer with current content
921 * @want buffer with desired content
922 * @len length of the checked area
923 * @gran write granularity (enum, not count)
924 * @first_start offset of the first byte which needs to be written (passed in
925 * value is increased by the offset of the first needed write
926 * relative to have/want or unchanged if no write is needed)
927 * @return length of the first contiguous area which needs to be written
928 * 0 if no write is needed
929 *
930 * FIXME: This function needs a parameter which tells it about coalescing
931 * in relation to the max write length of the programmer and the max write
932 * length of the chip.
933 */
934static int get_next_write(uint8_t *have, uint8_t *want, int len,
935 int *first_start, enum write_granularity gran)
936{
937 int need_write = 0, rel_start = 0, first_len = 0;
938 int i, limit, stride;
939
940 switch (gran) {
941 case write_gran_1bit:
942 case write_gran_1byte:
943 stride = 1;
944 break;
945 case write_gran_256bytes:
946 stride = 256;
947 break;
948 default:
949 msg_cerr("%s: Unsupported granularity! Please report a bug at "
950 "flashrom@flashrom.org\n", __func__);
951 /* Claim that no write was needed. A write with unknown
952 * granularity is too dangerous to try.
953 */
954 return 0;
955 }
956 for (i = 0; i < len / stride; i++) {
957 limit = min(stride, len - i * stride);
958 /* Are 'have' and 'want' identical? */
959 if (memcmp(have + i * stride, want + i * stride, limit)) {
960 if (!need_write) {
961 /* First location where have and want differ. */
962 need_write = 1;
963 rel_start = i * stride;
964 }
965 } else {
966 if (need_write) {
967 /* First location where have and want
968 * do not differ anymore.
969 */
David Hendricksc801adb2010-12-09 16:58:56 -0800970 break;
971 }
972 }
973 }
David Hendricks668f29d2011-01-27 18:51:45 -0800974 if (need_write)
David Hendricksc801adb2010-12-09 16:58:56 -0800975 first_len = min(i * stride - rel_start, len);
976 *first_start += rel_start;
977 return first_len;
978}
979
hailfinger0c515352009-11-23 12:55:31 +0000980/* This function generates various test patterns useful for testing controller
981 * and chip communication as well as chip behaviour.
982 *
983 * If a byte can be written multiple times, each time keeping 0-bits at 0
984 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
985 * is essentially an AND operation. That's also the reason why this function
986 * provides the result of AND between various patterns.
987 *
988 * Below is a list of patterns (and their block length).
989 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
990 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
991 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
992 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
993 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
994 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
995 * Pattern 6 is 00 (1 Byte)
996 * Pattern 7 is ff (1 Byte)
997 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
998 * byte block.
999 *
1000 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
1001 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
1002 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
1003 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
1004 * Pattern 12 is 00 (1 Byte)
1005 * Pattern 13 is ff (1 Byte)
1006 * Patterns 8-13 have no block number.
1007 *
1008 * Patterns 0-3 are created to detect and efficiently diagnose communication
1009 * slips like missed bits or bytes and their repetitive nature gives good visual
1010 * cues to the person inspecting the results. In addition, the following holds:
1011 * AND Pattern 0/1 == Pattern 4
1012 * AND Pattern 2/3 == Pattern 5
1013 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
1014 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
1015 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
1016 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
1017 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
1018 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
1019 * Besides that, they provide for bit testing of the last two bytes of every
1020 * 256 byte block which contains the block number for patterns 0-6.
1021 * Patterns 10-11 are special purpose for detecting subblock aliasing with
1022 * block sizes >256 bytes (some Dataflash chips etc.)
1023 * AND Pattern 8/9 == Pattern 12
1024 * AND Pattern 10/11 == Pattern 12
1025 * Pattern 13 is the completely erased state.
1026 * None of the patterns can detect aliasing at boundaries which are a multiple
1027 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
1028 */
1029int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
1030{
1031 int i;
1032
1033 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +00001034 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +00001035 return 1;
1036 }
1037
1038 switch (variant) {
1039 case 0:
1040 for (i = 0; i < size; i++)
1041 buf[i] = (i & 0xf) << 4 | 0x5;
1042 break;
1043 case 1:
1044 for (i = 0; i < size; i++)
1045 buf[i] = (i & 0xf) << 4 | 0xa;
1046 break;
1047 case 2:
1048 for (i = 0; i < size; i++)
1049 buf[i] = 0x50 | (i & 0xf);
1050 break;
1051 case 3:
1052 for (i = 0; i < size; i++)
1053 buf[i] = 0xa0 | (i & 0xf);
1054 break;
1055 case 4:
1056 for (i = 0; i < size; i++)
1057 buf[i] = (i & 0xf) << 4;
1058 break;
1059 case 5:
1060 for (i = 0; i < size; i++)
1061 buf[i] = i & 0xf;
1062 break;
1063 case 6:
1064 memset(buf, 0x00, size);
1065 break;
1066 case 7:
1067 memset(buf, 0xff, size);
1068 break;
1069 case 8:
1070 for (i = 0; i < size; i++)
1071 buf[i] = i & 0xff;
1072 break;
1073 case 9:
1074 for (i = 0; i < size; i++)
1075 buf[i] = ~(i & 0xff);
1076 break;
1077 case 10:
1078 for (i = 0; i < size % 2; i++) {
1079 buf[i * 2] = (i >> 8) & 0xff;
1080 buf[i * 2 + 1] = i & 0xff;
1081 }
1082 if (size & 0x1)
1083 buf[i * 2] = (i >> 8) & 0xff;
1084 break;
1085 case 11:
1086 for (i = 0; i < size % 2; i++) {
1087 buf[i * 2] = ~((i >> 8) & 0xff);
1088 buf[i * 2 + 1] = ~(i & 0xff);
1089 }
1090 if (size & 0x1)
1091 buf[i * 2] = ~((i >> 8) & 0xff);
1092 break;
1093 case 12:
1094 memset(buf, 0x00, size);
1095 break;
1096 case 13:
1097 memset(buf, 0xff, size);
1098 break;
1099 }
1100
1101 if ((variant >= 0) && (variant <= 7)) {
1102 /* Write block number in the last two bytes of each 256-byte
1103 * block, big endian for easier reading of the hexdump.
1104 * Note that this wraps around for chips larger than 2^24 bytes
1105 * (16 MB).
1106 */
1107 for (i = 0; i < size / 256; i++) {
1108 buf[i * 256 + 254] = (i >> 8) & 0xff;
1109 buf[i * 256 + 255] = i & 0xff;
1110 }
1111 }
1112
1113 return 0;
1114}
1115
hailfingeraec9c962009-10-31 01:53:09 +00001116int check_max_decode(enum chipbustype buses, uint32_t size)
1117{
1118 int limitexceeded = 0;
1119 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
1120 (max_rom_decode.parallel < size)) {
1121 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001122 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001123 "size %u kB of chipset/board/programmer "
1124 "for %s interface, "
1125 "probe/read/erase/write may fail. ", size / 1024,
1126 max_rom_decode.parallel / 1024, "Parallel");
1127 }
1128 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
1129 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001130 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001131 "size %u kB of chipset/board/programmer "
1132 "for %s interface, "
1133 "probe/read/erase/write may fail. ", size / 1024,
1134 max_rom_decode.lpc / 1024, "LPC");
1135 }
1136 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
1137 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001138 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001139 "size %u kB of chipset/board/programmer "
1140 "for %s interface, "
1141 "probe/read/erase/write may fail. ", size / 1024,
1142 max_rom_decode.fwh / 1024, "FWH");
1143 }
1144 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
1145 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001146 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001147 "size %u kB of chipset/board/programmer "
1148 "for %s interface, "
1149 "probe/read/erase/write may fail. ", size / 1024,
1150 max_rom_decode.spi / 1024, "SPI");
1151 }
1152 if (!limitexceeded)
1153 return 0;
1154 /* Sometimes chip and programmer have more than one bus in common,
1155 * and the limit is not exceeded on all buses. Tell the user.
1156 */
1157 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001158 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001159 msg_pdbg("There is at least one common chip/programmer "
hailfingeraec9c962009-10-31 01:53:09 +00001160 "interface which can support a chip of this size. "
1161 "You can try --force at your own risk.\n");
1162 return 1;
1163}
1164
stuge56300c32008-09-03 23:10:05 +00001165struct flashchip *probe_flash(struct flashchip *first_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001166{
stuge56300c32008-09-03 23:10:05 +00001167 struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001168 unsigned long base = 0;
David Hendricks668f29d2011-01-27 18:51:45 -08001169 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001170 uint32_t size;
1171 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001172 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001173
stuge56300c32008-09-03 23:10:05 +00001174 for (flash = first_flash; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001175 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001176 continue;
hailfinger327d2522010-03-22 23:43:51 +00001177 msg_gdbg("Probing for %s %s, %d KB: ",
stepanb8361b92008-03-17 22:59:40 +00001178 flash->vendor, flash->name, flash->total_size);
stuge98c09aa2008-06-18 02:08:40 +00001179 if (!flash->probe && !force) {
hailfinger327d2522010-03-22 23:43:51 +00001180 msg_gdbg("failed! flashrom has no probe function for "
1181 "this flash chip.\n");
stuge8ce3a3c2008-04-28 14:47:30 +00001182 continue;
1183 }
hailfingeraec9c962009-10-31 01:53:09 +00001184 buses_common = buses_supported & flash->bustype;
1185 if (!buses_common) {
hailfingera916b422009-06-01 02:08:58 +00001186 tmp = flashbuses_to_text(buses_supported);
hailfinger327d2522010-03-22 23:43:51 +00001187 msg_gdbg("skipped.");
1188 msg_gspew(" Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001189 free(tmp);
1190 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001191 msg_gspew("and chip bus type %s are incompatible.",
1192 tmp);
hailfingera916b422009-06-01 02:08:58 +00001193 free(tmp);
hailfinger327d2522010-03-22 23:43:51 +00001194 msg_gdbg("\n");
hailfingera916b422009-06-01 02:08:58 +00001195 continue;
1196 }
stepan782fb172007-04-06 11:58:03 +00001197
ollie5672ac62004-03-17 22:22:08 +00001198 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001199 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001200
hailfinger72d3b982009-05-09 07:27:23 +00001201 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger82719632009-05-16 21:22:56 +00001202 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001203
stugec1e55fe2008-07-02 17:15:47 +00001204 if (force)
1205 break;
stepanc98b80b2006-03-16 16:57:41 +00001206
stuge56300c32008-09-03 23:10:05 +00001207 if (flash->probe(flash) != 1)
1208 goto notfound;
1209
uwefa98ca12008-10-18 21:14:13 +00001210 if (first_flash == flashchips
1211 || flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001212 break;
1213
stuge56300c32008-09-03 23:10:05 +00001214notfound:
hailfinger82719632009-05-16 21:22:56 +00001215 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001216 }
uwebe4477b2007-08-23 16:08:21 +00001217
stugec1e55fe2008-07-02 17:15:47 +00001218 if (!flash || !flash->name)
1219 return NULL;
1220
David Hendricks668f29d2011-01-27 18:51:45 -08001221 if (programmer_table[programmer].map_flash_region == physmap) {
1222 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
1223 } else {
1224 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
1225 }
1226
1227 msg_cinfo("%s chip \"%s %s\" (%d KB, %s) %s.\n",
hailfingerf4aaccc2010-04-28 15:22:14 +00001228 force ? "Assuming" : "Found",
uwe9e6811e2009-06-28 21:47:57 +00001229 flash->vendor, flash->name, flash->total_size,
David Hendricks668f29d2011-01-27 18:51:45 -08001230 flashbuses_to_text(flash->bustype), location);
uwe9e6811e2009-06-28 21:47:57 +00001231
David Hendricks668f29d2011-01-27 18:51:45 -08001232 /* Flash registers will not be mapped if the chip was forced. Lock info
1233 * may be stored in registers, so avoid lock info printing.
1234 */
1235 if (!force)
1236 if (flash->printlock)
1237 flash->printlock(flash);
snelson1ee293c2010-02-19 00:52:10 +00001238
stugec1e55fe2008-07-02 17:15:47 +00001239 return flash;
rminnich8d3ff912003-10-25 17:01:29 +00001240}
1241
stepan193c9c22005-12-18 16:41:10 +00001242int verify_flash(struct flashchip *flash, uint8_t *buf)
rminnich8d3ff912003-10-25 17:01:29 +00001243{
hailfingerb0f4d122009-06-24 08:20:45 +00001244 int ret;
David Hendricksc801adb2010-12-09 16:58:56 -08001245 int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001246
snelsone42c3802010-05-07 20:09:04 +00001247 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001248
David Hendricksc801adb2010-12-09 16:58:56 -08001249 ret = verify_range(flash, buf, 0, total_size, NULL);
uwef6641642007-05-09 10:17:44 +00001250
hailfingerb0f4d122009-06-24 08:20:45 +00001251 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001252 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001253
hailfingerb0f4d122009-06-24 08:20:45 +00001254 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001255}
1256
David Hendricksc801adb2010-12-09 16:58:56 -08001257int read_buf_from_file(unsigned char *buf, unsigned long size, char *filename)
1258{
1259 unsigned long numbytes;
1260 FILE *image;
1261 struct stat image_stat;
1262
1263 if ((image = fopen(filename, "rb")) == NULL) {
1264 perror(filename);
1265 return 1;
1266 }
1267 if (fstat(fileno(image), &image_stat) != 0) {
1268 perror(filename);
1269 fclose(image);
1270 return 1;
1271 }
1272 if (image_stat.st_size != size) {
1273 msg_gerr("Error: Image size doesn't match\n");
1274 fclose(image);
1275 return 1;
1276 }
1277 numbytes = fread(buf, 1, size, image);
1278 if (fclose(image)) {
1279 perror(filename);
1280 return 1;
1281 }
1282 if (numbytes != size) {
1283 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1284 "wanted %ld!\n", numbytes, size);
1285 return 1;
1286 }
1287 return 0;
1288}
1289
David Hendricks82fd8ae2010-08-04 14:34:54 -07001290int write_buf_to_file(unsigned char *buf, unsigned long size, char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001291{
1292 unsigned long numbytes;
1293 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001294
1295 if (!filename) {
David Hendricks82fd8ae2010-08-04 14:34:54 -07001296 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001297 return 1;
1298 }
oxygenebf70d352010-01-25 22:55:33 +00001299 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001300 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001301 return 1;
David Hendricks82fd8ae2010-08-04 14:34:54 -07001302 }
hailfingerd219a232009-01-28 00:27:54 +00001303
hailfingerd219a232009-01-28 00:27:54 +00001304 numbytes = fwrite(buf, 1, size, image);
1305 fclose(image);
David Hendricks82fd8ae2010-08-04 14:34:54 -07001306 if (numbytes != size) {
1307 msg_gerr("File %s could not be written completely.\n",
1308 filename);
hailfingerd219a232009-01-28 00:27:54 +00001309 return 1;
David Hendricks82fd8ae2010-08-04 14:34:54 -07001310 }
hailfingerd219a232009-01-28 00:27:54 +00001311 return 0;
1312}
1313
David Hendricks82fd8ae2010-08-04 14:34:54 -07001314int read_flash_to_file(struct flashchip *flash, char *filename)
1315{
1316 unsigned long size = flash->total_size * 1024;
1317 unsigned char *buf = calloc(size, sizeof(char));
1318 int ret = 0;
1319
1320 msg_cinfo("Reading flash... ");
1321 if (!buf) {
David Hendricksc6c9f822010-11-03 15:07:01 -07001322 msg_gerr("Memory allocation failed!\n");
David Hendricks668f29d2011-01-27 18:51:45 -08001323 msg_cinfo("FAILED.\n");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001324 return 1;
1325 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001326
1327 /* To support partial read, fill buffer to all 0xFF at beginning to make
1328 * debug easier. */
1329 memset(buf, 0xFF, size);
1330
David Hendricks82fd8ae2010-08-04 14:34:54 -07001331 if (!flash->read) {
1332 msg_cerr("No read function available for this flash chip.\n");
1333 ret = 1;
1334 goto out_free;
1335 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001336
1337 /* First try to handle partial read case, rather than read the whole
1338 * flash, which is slow. */
1339 ret = handle_partial_read(flash, buf, flash->read);
1340 if (ret < 0) {
1341 msg_cerr("Partial read operation failed!\n");
1342 ret = 1;
1343 goto out_free;
1344 } else if (ret > 0) {
1345 /* Partial read has been handled, pass the whole flash read. */
1346 } else if (flash->read(flash, buf, 0, size)) {
David Hendricks82fd8ae2010-08-04 14:34:54 -07001347 msg_cerr("Read operation failed!\n");
1348 ret = 1;
1349 goto out_free;
1350 }
1351
1352 ret = write_buf_to_file(buf, flash->total_size * 1024, filename);
1353out_free:
1354 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001355 if (ret)
1356 msg_cerr("FAILED.");
1357 else
1358 msg_cdbg("done.");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001359 return ret;
1360}
1361
David Hendricksc801adb2010-12-09 16:58:56 -08001362/* This function shares a lot of its structure with erase_and_write_flash() and
1363 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001364 * Even if an error is found, the function will keep going and check the rest.
1365 */
David Hendricks82fd8ae2010-08-04 14:34:54 -07001366static int selfcheck_eraseblocks(struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001367{
hailfinger9fed35d2010-01-19 06:42:46 +00001368 int i, j, k;
1369 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001370
1371 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1372 unsigned int done = 0;
1373 struct block_eraser eraser = flash->block_erasers[k];
1374
1375 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1376 /* Blocks with zero size are bugs in flashchips.c. */
1377 if (eraser.eraseblocks[i].count &&
1378 !eraser.eraseblocks[i].size) {
1379 msg_gerr("ERROR: Flash chip %s erase function "
1380 "%i region %i has size 0. Please report"
1381 " a bug at flashrom@flashrom.org\n",
1382 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001383 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001384 }
1385 /* Blocks with zero count are bugs in flashchips.c. */
1386 if (!eraser.eraseblocks[i].count &&
1387 eraser.eraseblocks[i].size) {
1388 msg_gerr("ERROR: Flash chip %s erase function "
1389 "%i region %i has count 0. Please report"
1390 " a bug at flashrom@flashrom.org\n",
1391 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001392 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001393 }
1394 done += eraser.eraseblocks[i].count *
1395 eraser.eraseblocks[i].size;
1396 }
hailfinger9fed35d2010-01-19 06:42:46 +00001397 /* Empty eraseblock definition with erase function. */
1398 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001399 msg_gspew("Strange: Empty eraseblock definition with "
hailfinger9fed35d2010-01-19 06:42:46 +00001400 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001401 if (!done)
1402 continue;
1403 if (done != flash->total_size * 1024) {
1404 msg_gerr("ERROR: Flash chip %s erase function %i "
1405 "region walking resulted in 0x%06x bytes total,"
1406 " expected 0x%06x bytes. Please report a bug at"
1407 " flashrom@flashrom.org\n", flash->name, k,
1408 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001409 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001410 }
hailfinger9fed35d2010-01-19 06:42:46 +00001411 if (!eraser.block_erase)
1412 continue;
1413 /* Check if there are identical erase functions for different
1414 * layouts. That would imply "magic" erase functions. The
1415 * easiest way to check this is with function pointers.
1416 */
uwef6f94d42010-03-13 17:28:29 +00001417 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001418 if (eraser.block_erase ==
1419 flash->block_erasers[j].block_erase) {
1420 msg_gerr("ERROR: Flash chip %s erase function "
1421 "%i and %i are identical. Please report"
1422 " a bug at flashrom@flashrom.org\n",
1423 flash->name, k, j);
1424 ret = 1;
1425 }
uwef6f94d42010-03-13 17:28:29 +00001426 }
hailfinger45177872010-01-18 08:14:43 +00001427 }
hailfinger9fed35d2010-01-19 06:42:46 +00001428 return ret;
hailfinger45177872010-01-18 08:14:43 +00001429}
1430
David Hendricksc801adb2010-12-09 16:58:56 -08001431static int erase_and_write_block_helper(struct flashchip *flash,
1432 unsigned int start, unsigned int len,
1433 uint8_t *curcontents,
1434 uint8_t *newcontents,
1435 int (*erasefn) (struct flashchip *flash,
1436 unsigned int addr,
1437 unsigned int len))
1438{
1439 int starthere = 0;
1440 int lenhere = 0;
1441 int ret = 0;
1442 int skip = 1;
1443 int writecount = 0;
1444 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1445
1446 /* curcontents and newcontents are opaque to walk_eraseregions, and
1447 * need to be adjusted here to keep the impression of proper abstraction
1448 */
1449 curcontents += start;
1450 newcontents += start;
1451 msg_cdbg(":");
1452 /* FIXME: Assume 256 byte granularity for now to play it safe. */
1453 if (need_erase(curcontents, newcontents, len, gran)) {
1454 msg_cdbg("E");
1455 ret = erasefn(flash, start, len);
1456 if (ret)
1457 return ret;
1458 /* Erase was successful. Adjust curcontents. */
1459 memset(curcontents, 0xff, len);
1460 skip = 0;
1461 }
1462 /* get_next_write() sets starthere to a new value after the call. */
1463 while ((lenhere = get_next_write(curcontents + starthere,
1464 newcontents + starthere,
1465 len - starthere, &starthere, gran))) {
1466 if (!writecount++)
1467 msg_cdbg("W");
1468 /* Needs the partial write function signature. */
1469 ret = flash->write(flash, newcontents + starthere,
1470 start + starthere, lenhere);
1471 if (ret)
1472 return ret;
1473 starthere += lenhere;
1474 skip = 0;
1475 }
1476 if (skip)
1477 msg_cdbg("S");
1478 return ret;
1479}
1480
David Hendricks82fd8ae2010-08-04 14:34:54 -07001481static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1482 int (*do_something) (struct flashchip *flash,
1483 unsigned int addr,
David Hendricksc801adb2010-12-09 16:58:56 -08001484 unsigned int len,
1485 uint8_t *param1,
1486 uint8_t *param2,
1487 int (*erasefn) (
1488 struct flashchip *flash,
1489 unsigned int addr,
1490 unsigned int len)),
1491 void *param1, void *param2)
David Hendricks82fd8ae2010-08-04 14:34:54 -07001492{
1493 int i, j;
1494 unsigned int start = 0;
1495 unsigned int len;
1496 struct block_eraser eraser = flash->block_erasers[erasefunction];
1497 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1498 /* count==0 for all automatically initialized array
1499 * members so the loop below won't be executed for them.
1500 */
1501 len = eraser.eraseblocks[i].size;
1502 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
David Hendricksc801adb2010-12-09 16:58:56 -08001503 /* Print this for every block except the first one. */
1504 if (i || j)
1505 msg_cdbg(", ");
David Hendricks970da352010-08-24 15:03:01 -07001506 msg_cdbg("0x%06x-0x%06x", start,
David Hendricks82fd8ae2010-08-04 14:34:54 -07001507 start + len - 1);
David Hendricksc801adb2010-12-09 16:58:56 -08001508 if (do_something(flash, start, len, param1, param2,
1509 eraser.block_erase)) {
1510 msg_cdbg("\n");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001511 return 1;
David Hendricksc801adb2010-12-09 16:58:56 -08001512 }
David Hendricks82fd8ae2010-08-04 14:34:54 -07001513 start += len;
1514 }
1515 }
David Hendricksc801adb2010-12-09 16:58:56 -08001516 msg_cdbg("\n");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001517 return 0;
1518}
1519
David Hendricks668f29d2011-01-27 18:51:45 -08001520static int check_block_eraser(struct flashchip *flash, int k, int log)
1521{
1522 struct block_eraser eraser = flash->block_erasers[k];
1523
1524 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1525 if (log)
1526 msg_cdbg("not defined. ");
1527 return 1;
1528 }
1529 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1530 if (log)
1531 msg_cdbg("eraseblock layout is known, but matching "
1532 "block erase function is not implemented. ");
1533 return 1;
1534 }
1535 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1536 if (log)
1537 msg_cdbg("block erase function found, but "
1538 "eraseblock layout is not defined. ");
1539 return 1;
1540 }
1541 return 0;
1542}
1543
David Hendricksc801adb2010-12-09 16:58:56 -08001544int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents, uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001545{
David Hendricks668f29d2011-01-27 18:51:45 -08001546 int k, ret = 0;
David Hendricksc801adb2010-12-09 16:58:56 -08001547 uint8_t *curcontents;
1548 unsigned long size = flash->total_size * 1024;
David Hendricks668f29d2011-01-27 18:51:45 -08001549 int usable_erasefunctions = 0;
1550
1551 for (k = 0; k < NUM_ERASEFUNCTIONS; k++)
1552 if (!check_block_eraser(flash, k, 0))
1553 usable_erasefunctions++;
1554 msg_cinfo("Erasing and writing flash chip... ");
1555 if (!usable_erasefunctions) {
1556 msg_cerr("ERROR: flashrom has no erase function for this flash "
1557 "chip.\n");
1558 return 1;
1559 }
hailfinger7df21362009-09-05 02:30:58 +00001560
David Hendricksc801adb2010-12-09 16:58:56 -08001561 curcontents = (uint8_t *) malloc(size);
1562 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1563 memcpy(curcontents, oldcontents, size);
1564
hailfinger7df21362009-09-05 02:30:58 +00001565 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
snelsone42c3802010-05-07 20:09:04 +00001566 msg_cdbg("Looking at blockwise erase function %i... ", k);
David Hendricks668f29d2011-01-27 18:51:45 -08001567 if (check_block_eraser(flash, k, 1) && usable_erasefunctions) {
1568 msg_cdbg("Looking for another erase function.\n");
hailfinger7df21362009-09-05 02:30:58 +00001569 continue;
1570 }
David Hendricks668f29d2011-01-27 18:51:45 -08001571 usable_erasefunctions--;
snelsone42c3802010-05-07 20:09:04 +00001572 msg_cdbg("trying... ");
David Hendricksc801adb2010-12-09 16:58:56 -08001573 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper, curcontents, newcontents);
snelsone42c3802010-05-07 20:09:04 +00001574 msg_cdbg("\n");
hailfinger7df21362009-09-05 02:30:58 +00001575 /* If everything is OK, don't try another erase function. */
1576 if (!ret)
1577 break;
David Hendricks668f29d2011-01-27 18:51:45 -08001578 /* Write/erase failed, so try to find out what the current chip
1579 * contents are. If no usable erase functions remain, we could
1580 * abort the loop instead of continuing, the effect is the same.
1581 * The only difference is whether the reason for other unusable
1582 * functions is printed or not. If in doubt, verbosity wins.
David Hendricksc801adb2010-12-09 16:58:56 -08001583 */
David Hendricks668f29d2011-01-27 18:51:45 -08001584 if (!usable_erasefunctions)
1585 continue;
1586 if (flash->read(flash, curcontents, 0, size)) {
1587 /* Now we are truly screwed. Read failed as well. */
1588 msg_cerr("Can't read anymore!\n");
1589 /* We have no idea about the flash chip contents, so
1590 * retrying with another erase function is pointless.
1591 */
1592 break;
1593 }
hailfinger7df21362009-09-05 02:30:58 +00001594 }
David Hendricksc801adb2010-12-09 16:58:56 -08001595 /* Free the scratchpad. */
1596 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001597
hailfinger7df21362009-09-05 02:30:58 +00001598 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001599 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001600 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001601 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001602 }
1603 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001604}
1605
David Hendricksc801adb2010-12-09 16:58:56 -08001606void nonfatal_help_message(void)
1607{
1608 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1609 "This means we have to add special support for your board, "
1610 "programmer or flash chip.\n"
1611 "Please report this on IRC at irc.freenode.net (channel "
1612 "#flashrom) or\n"
1613 "mail flashrom@flashrom.org!\n"
1614 "-------------------------------------------------------------"
1615 "------------------\n"
1616 "You may now reboot or simply leave the machine running.\n");
1617}
1618
uweb34ec9f2009-10-01 18:40:02 +00001619void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001620{
snelsone42c3802010-05-07 20:09:04 +00001621 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001622 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
David Hendricksc801adb2010-12-09 16:58:56 -08001623 "mail flashrom@flashrom.org with FAILED: your board name in "
1624 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001625 "-------------------------------------------------------------"
1626 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001627 "DO NOT REBOOT OR POWEROFF!\n");
1628}
1629
hailfingerc77acb52009-12-24 02:15:55 +00001630/* The way to go if you want a delimited list of programmers*/
1631void list_programmers(char *delim)
1632{
1633 enum programmer p;
1634 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001635 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001636 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001637 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001638 }
snelsone42c3802010-05-07 20:09:04 +00001639 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001640}
1641
David Hendricksc801adb2010-12-09 16:58:56 -08001642void list_programmers_linebreak(int startcol, int cols, int paren)
1643{
1644 const char *pname;
1645 int pnamelen;
1646 int remaining = 0;
1647 int firstline = 1;
1648 enum programmer p;
1649 int i;
1650
1651 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1652 pname = programmer_table[p].name;
1653 pnamelen = strlen(pname);
1654 if (remaining - pnamelen - 2 < 0) {
1655 if (firstline)
1656 firstline = 0;
1657 else
1658 printf("\n");
1659 for (i = 0; i < startcol; i++)
1660 printf(" ");
1661 remaining = cols - startcol;
1662 } else {
1663 printf(" ");
1664 remaining--;
1665 }
1666 if (paren && (p == 0)) {
1667 printf("(");
1668 remaining--;
1669 }
1670 printf("%s", pname);
1671 remaining -= pnamelen;
1672 if (p < PROGRAMMER_INVALID - 1) {
1673 printf(",");
1674 remaining--;
1675 } else {
1676 if (paren)
1677 printf(")");
1678 printf("\n");
1679 }
1680 }
1681}
1682
hailfinger3b471632010-03-27 16:36:40 +00001683void print_sysinfo(void)
1684{
David Hendricksc6c9f822010-11-03 15:07:01 -07001685 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001686#if HAVE_UTSNAME == 1
1687 struct utsname osinfo;
1688 uname(&osinfo);
1689
David Hendricksc6c9f822010-11-03 15:07:01 -07001690 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001691 osinfo.machine);
1692#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001693 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001694#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001695 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001696#if NEED_PCI == 1
1697#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001698 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001699#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001700 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001701#endif
1702#endif
1703#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001704 msg_gerr(" LLVM Clang");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001705#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001706 msg_gerr(" %s,", __clang_version__);
David Hendricks82fd8ae2010-08-04 14:34:54 -07001707#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001708 msg_gerr(" unknown version (before r102686),");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001709#endif
hailfinger3b471632010-03-27 16:36:40 +00001710#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001711 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001712#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001713 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001714#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001715 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001716#endif
1717#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001718 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001719#endif
1720#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001721 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001722#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001723 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001724#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001725 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001726}
1727
uwefdeca092008-01-21 15:24:22 +00001728void print_version(void)
1729{
David Hendricksc6c9f822010-11-03 15:07:01 -07001730 /* send to stderr for chromium os */
1731 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001732 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001733}
1734
hailfinger74819ad2010-05-15 15:04:37 +00001735void print_banner(void)
1736{
1737 msg_ginfo("flashrom is free software, get the source code at "
1738 "http://www.flashrom.org\n");
1739 msg_ginfo("\n");
1740}
1741
hailfingerc77acb52009-12-24 02:15:55 +00001742int selfcheck(void)
1743{
hailfinger45177872010-01-18 08:14:43 +00001744 int ret = 0;
1745 struct flashchip *flash;
1746
1747 /* Safety check. Instead of aborting after the first error, check
1748 * if more errors exist.
1749 */
hailfingerc77acb52009-12-24 02:15:55 +00001750 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001751 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001752 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001753 }
1754 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001755 msg_gerr("SPI programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001756 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001757 }
hailfinger45177872010-01-18 08:14:43 +00001758 for (flash = flashchips; flash && flash->name; flash++)
1759 if (selfcheck_eraseblocks(flash))
1760 ret = 1;
1761 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001762}
1763
1764void check_chip_supported(struct flashchip *flash)
1765{
1766 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001767 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001768 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001769 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001770 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001771 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001772 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001773 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001774 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001775 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001776 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001777 msg_cdbg(" WRITE");
1778 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001779 }
1780 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1781 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1782 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1783 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001784 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001785 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001786 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001787 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001788 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001789 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001790 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001791 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001792 msg_cdbg(" WRITE");
1793 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001794 }
hailfinger92cd8e32010-01-07 03:24:05 +00001795 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001796 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001797 "in the latest development\n"
1798 "version of flashrom. If you are running the latest "
1799 "development version,\n"
1800 "please email a report to flashrom@flashrom.org if "
1801 "any of the above operations\n"
1802 "work correctly for you with this flash part. Please "
1803 "include the flashrom\n"
1804 "output with the additional -V option for all "
1805 "operations you tested (-V, -Vr,\n"
1806 "-Vw, -VE), and mention which mainboard or "
1807 "programmer you tested.\n"
David Hendricksc801adb2010-12-09 16:58:56 -08001808 "Please mention your board in the subject line. "
1809 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001810 }
1811}
1812
ollie5b621572004-03-20 16:46:10 +00001813int main(int argc, char *argv[])
rminnich8d3ff912003-10-25 17:01:29 +00001814{
David Hendricksf7924d12010-06-10 21:26:44 -07001815 /* FIXME: this should eventually be a build option controlled
1816 via a USE flag */
1817// return cli_classic(argc, argv);
1818 return cli_mfg(argc, argv);
hailfingerc77acb52009-12-24 02:15:55 +00001819}
1820
David Hendricksc801adb2010-12-09 16:58:56 -08001821/* FIXME: This function signature needs to be improved once doit() has a better
1822 * function signature.
hailfingerc77acb52009-12-24 02:15:55 +00001823 */
David Hendricksc801adb2010-12-09 16:58:56 -08001824int 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 +00001825{
David Hendricks82fd8ae2010-08-04 14:34:54 -07001826 if (!programmer_may_write && (write_it || erase_it)) {
1827 msg_perr("Write/erase is not working yet on your programmer in "
1828 "its current configuration.\n");
1829 /* --force is the wrong approach, but it's the best we can do
1830 * until the generic programmer parameter parser is merged.
1831 */
David Hendricksc801adb2010-12-09 16:58:56 -08001832 if (!force)
David Hendricks82fd8ae2010-08-04 14:34:54 -07001833 return 1;
David Hendricksc801adb2010-12-09 16:58:56 -08001834 msg_cerr("Continuing anyway.\n");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001835 }
1836
David Hendricksc801adb2010-12-09 16:58:56 -08001837 if (read_it || erase_it || write_it || verify_it) {
1838 /* Everything needs read. */
1839 if (flash->tested & TEST_BAD_READ) {
1840 msg_cerr("Read is not working on this chip. ");
1841 if (!force)
1842 return 1;
1843 msg_cerr("Continuing anyway.\n");
1844 }
1845 if (!flash->read) {
1846 msg_cerr("flashrom has no read function for this "
1847 "flash chip.\n");
1848 return 1;
1849 }
1850 }
1851 if (erase_it || write_it) {
1852 /* Write needs erase. */
hailfinger0459e1c2009-08-19 13:55:34 +00001853 if (flash->tested & TEST_BAD_ERASE) {
snelsone42c3802010-05-07 20:09:04 +00001854 msg_cerr("Erase is not working on this chip. ");
David Hendricksc801adb2010-12-09 16:58:56 -08001855 if (!force)
hailfinger0459e1c2009-08-19 13:55:34 +00001856 return 1;
David Hendricksc801adb2010-12-09 16:58:56 -08001857 msg_cerr("Continuing anyway.\n");
hailfinger0459e1c2009-08-19 13:55:34 +00001858 }
David Hendricksc801adb2010-12-09 16:58:56 -08001859 /* FIXME: Check if at least one erase function exists. */
1860 }
1861 if (write_it) {
hailfinger0459e1c2009-08-19 13:55:34 +00001862 if (flash->tested & TEST_BAD_WRITE) {
snelsone42c3802010-05-07 20:09:04 +00001863 msg_cerr("Write is not working on this chip. ");
David Hendricksc801adb2010-12-09 16:58:56 -08001864 if (!force)
hailfinger0459e1c2009-08-19 13:55:34 +00001865 return 1;
David Hendricksc801adb2010-12-09 16:58:56 -08001866 msg_cerr("Continuing anyway.\n");
hailfinger0459e1c2009-08-19 13:55:34 +00001867 }
David Hendricksc801adb2010-12-09 16:58:56 -08001868 if (!flash->write) {
1869 msg_cerr("flashrom has no write function for this "
1870 "flash chip.\n");
stugebbcc2f12009-01-12 21:00:35 +00001871 return 1;
1872 }
ollie5672ac62004-03-17 22:22:08 +00001873 }
David Hendricksc801adb2010-12-09 16:58:56 -08001874 return 0;
1875}
1876
1877/* This function signature is horrible. We need to design a better interface,
1878 * but right now it allows us to split off the CLI code.
1879 * Besides that, the function itself is a textbook example of abysmal code flow.
1880 */
1881int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1882{
1883 uint8_t *oldcontents;
1884 uint8_t *newcontents;
1885 int ret = 0;
1886 unsigned long size = flash->total_size * 1024;
1887
1888 if (chip_safety_check(flash, force, filename, read_it, write_it, erase_it, verify_it)) {
1889 msg_cerr("Aborting.\n");
1890 ret = 1;
1891 goto out_nofree;
1892 }
1893
1894 /* Given the existence of read locks, we want to unlock for read,
1895 * erase and write.
1896 */
1897 if (flash->unlock)
1898 flash->unlock(flash);
1899
David Hendricks6d62d752011-03-07 21:20:22 -08001900 /* add entries for regions specified in flashmap */
1901 if (add_fmap_entries(flash) < 0) {
1902 ret = 1;
1903 goto out_nofree;
1904 }
1905
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001906 /* mark entries included using -i argument as "included" if they are
1907 found in the master rom_entries list */
1908 if (process_include_args() < 0) {
1909 ret = 1;
1910 goto out_nofree;
1911 }
1912
David Hendricksc801adb2010-12-09 16:58:56 -08001913 if (read_it) {
1914 ret = read_flash_to_file(flash, filename);
1915 goto out_nofree;
1916 }
1917
1918 oldcontents = (uint8_t *) malloc(size);
1919 /* Assume worst case: All bits are 0. */
1920 memset(oldcontents, 0x00, size);
1921 newcontents = (uint8_t *) malloc(size);
1922 /* Assume best case: All bits should be 1. */
1923 memset(newcontents, 0xff, size);
1924 /* Side effect of the assumptions above: Default write action is erase
1925 * because newcontents looks like a completely erased chip, and
1926 * oldcontents being completely 0x00 means we have to erase everything
1927 * before we can write.
1928 */
1929
1930 if (erase_it) {
1931 /* FIXME: Do we really want the scary warning if erase failed?
1932 * After all, after erase the chip is either blank or partially
1933 * blank or it has the old contents. A blank chip won't boot,
1934 * so if the user wanted erase and reboots afterwards, the user
1935 * knows very well that booting won't work.
1936 */
1937 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1938 emergency_help_message();
1939 ret = 1;
1940 }
1941 goto out;
1942 }
1943
1944 if (write_it || verify_it) {
1945 if (read_buf_from_file(newcontents, size, filename)) {
1946 ret = 1;
1947 goto out;
1948 }
1949
1950#if CONFIG_INTERNAL == 1
1951 if (programmer == PROGRAMMER_INTERNAL)
1952 show_id(newcontents, size, force);
1953#endif
1954 }
1955
1956 /* Read the whole chip to be able to check whether regions need to be
1957 * erased and to give better diagnostics in case write fails.
1958 * The alternative would be to read only the regions which are to be
1959 * preserved, but in that case we might perform unneeded erase which
1960 * takes time as well.
1961 */
1962 msg_cdbg("Reading old flash chip contents...\n");
1963 if (flash->read(flash, oldcontents, 0, size)) {
1964 ret = 1;
1965 goto out;
1966 }
ollie5672ac62004-03-17 22:22:08 +00001967
ollie6a600992005-11-26 21:55:36 +00001968 // This should be moved into each flash part's code to do it
1969 // cleanly. This does the job.
David Hendricksc801adb2010-12-09 16:58:56 -08001970 handle_romentries(flash, oldcontents, newcontents);
uwef6641642007-05-09 10:17:44 +00001971
ollie6a600992005-11-26 21:55:36 +00001972 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00001973
stuge8ce3a3c2008-04-28 14:47:30 +00001974 if (write_it) {
David Hendricksc801adb2010-12-09 16:58:56 -08001975 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1976 msg_cerr("Uh oh. Erase/write failed. Checking if "
1977 "anything changed.\n");
1978 if (!flash->read(flash, newcontents, 0, size)) {
1979 if (!memcmp(oldcontents, newcontents, size)) {
1980 msg_cinfo("Good. It seems nothing was "
1981 "changed.\n");
1982 nonfatal_help_message();
1983 ret = 1;
1984 goto out;
1985 }
1986 }
hailfinger0459e1c2009-08-19 13:55:34 +00001987 emergency_help_message();
David Hendricksc801adb2010-12-09 16:58:56 -08001988 ret = 1;
1989 goto out;
hailfingerdffbe6b2009-07-24 12:18:54 +00001990 }
stuge8ce3a3c2008-04-28 14:47:30 +00001991 }
ollie6a600992005-11-26 21:55:36 +00001992
hailfinger0459e1c2009-08-19 13:55:34 +00001993 if (verify_it) {
1994 /* Work around chips which need some time to calm down. */
1995 if (write_it)
1996 programmer_delay(1000*1000);
David Hendricksc801adb2010-12-09 16:58:56 -08001997 ret = verify_flash(flash, newcontents);
hailfingera50d60e2009-11-17 09:57:34 +00001998 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00001999 * might have an emergency situation.
2000 */
2001 if (ret && write_it)
2002 emergency_help_message();
2003 }
ollie6a600992005-11-26 21:55:36 +00002004
David Hendricksc801adb2010-12-09 16:58:56 -08002005out:
2006 free(oldcontents);
2007 free(newcontents);
2008out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002009 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002010 /*
2011 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2012 * tree. This is because some operations, such as write protection,
2013 * requires programmer_shutdown() but does not call doit().
2014 */
2015// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002016 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002017}