blob: 53df08efed886a393e148eebb3439c7f02bfa8c5 [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>
rminnich8d3ff912003-10-25 17:01:29 +000025#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000026#include <sys/types.h>
27#include <sys/stat.h>
rminnich8d3ff912003-10-25 17:01:29 +000028#include <string.h>
29#include <stdlib.h>
ollie6a600992005-11-26 21:55:36 +000030#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000031#if HAVE_UTSNAME == 1
32#include <sys/utsname.h>
33#endif
rminnich8d3ff912003-10-25 17:01:29 +000034#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000035#include "flashchips.h"
David Hendricks82fd8ae2010-08-04 14:34:54 -070036#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000037
David Hendricks82fd8ae2010-08-04 14:34:54 -070038const char * const flashrom_version = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000039char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000040int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000041
hailfinger90c7d542010-05-31 15:27:27 +000042#if CONFIG_INTERNAL == 1
hailfinger6fe23d62009-08-12 11:39:29 +000043enum programmer programmer = PROGRAMMER_INTERNAL;
hailfinger90c7d542010-05-31 15:27:27 +000044#elif CONFIG_DUMMY == 1
hailfinger80422e22009-12-13 22:28:00 +000045enum programmer programmer = PROGRAMMER_DUMMY;
46#else
hailfingerf95c8f62010-01-10 13:28:48 +000047/* If neither internal nor dummy are selected, we must pick a sensible default.
48 * Since there is no reason to prefer a particular external programmer, we fail
49 * if more than one of them is selected. If only one is selected, it is clear
50 * that the user wants that one to become the default.
51 */
David Hendricks82fd8ae2010-08-04 14:34:54 -070052#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 > 1
hailfinger90c7d542010-05-31 15:27:27 +000053#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all programmers except one.
hailfingerf95c8f62010-01-10 13:28:48 +000054#endif
55enum programmer programmer =
hailfinger90c7d542010-05-31 15:27:27 +000056#if CONFIG_NIC3COM == 1
hailfingerf95c8f62010-01-10 13:28:48 +000057 PROGRAMMER_NIC3COM
58#endif
hailfinger90c7d542010-05-31 15:27:27 +000059#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +000060 PROGRAMMER_NICREALTEK
61 PROGRAMMER_NICREALTEK2
62#endif
hailfingerf0a368f2010-06-07 22:37:54 +000063#if CONFIG_NICNATSEMI == 1
64 PROGRAMMER_NICNATSEMI
65#endif
hailfinger90c7d542010-05-31 15:27:27 +000066#if CONFIG_GFXNVIDIA == 1
hailfingerf95c8f62010-01-10 13:28:48 +000067 PROGRAMMER_GFXNVIDIA
68#endif
hailfinger90c7d542010-05-31 15:27:27 +000069#if CONFIG_DRKAISER == 1
hailfingerf95c8f62010-01-10 13:28:48 +000070 PROGRAMMER_DRKAISER
71#endif
hailfinger90c7d542010-05-31 15:27:27 +000072#if CONFIG_SATASII == 1
hailfingerf95c8f62010-01-10 13:28:48 +000073 PROGRAMMER_SATASII
74#endif
hailfinger90c7d542010-05-31 15:27:27 +000075#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +000076 PROGRAMMER_ATAHPT
77#endif
hailfinger90c7d542010-05-31 15:27:27 +000078#if CONFIG_FT2232_SPI == 1
79 PROGRAMMER_FT2232_SPI
hailfingerf95c8f62010-01-10 13:28:48 +000080#endif
hailfinger90c7d542010-05-31 15:27:27 +000081#if CONFIG_SERPROG == 1
hailfingerf95c8f62010-01-10 13:28:48 +000082 PROGRAMMER_SERPROG
83#endif
hailfinger90c7d542010-05-31 15:27:27 +000084#if CONFIG_BUSPIRATE_SPI == 1
85 PROGRAMMER_BUSPIRATE_SPI
hailfingerf95c8f62010-01-10 13:28:48 +000086#endif
hailfinger90c7d542010-05-31 15:27:27 +000087#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +000088 PROGRAMMER_DEDIPROG
89#endif
David Hendricks82fd8ae2010-08-04 14:34:54 -070090#if CONFIG_RAYER_SPI == 1
91 PROGRAMMER_RAYER_SPI
92#endif
hailfingerf95c8f62010-01-10 13:28:48 +000093;
hailfinger80422e22009-12-13 22:28:00 +000094#endif
95
David Hendricks82fd8ae2010-08-04 14:34:54 -070096static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000097
David Hendricks82fd8ae2010-08-04 14:34:54 -070098/* Supported buses for the current programmer. */
99enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +0000100
101/**
102 * Programmers supporting multiple buses can have differing size limits on
103 * each bus. Store the limits for each bus in a common struct.
104 */
David Hendricks82fd8ae2010-08-04 14:34:54 -0700105struct decode_sizes max_rom_decode;
106
107/* If nonzero, used as the start address of bottom-aligned flash. */
108unsigned long flashbase;
109
110/* Is writing allowed with this programmer? */
111int programmer_may_write;
hailfinger80422e22009-12-13 22:28:00 +0000112
hailfingerabe249e2009-05-08 17:43:22 +0000113const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +0000114#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +0000115 {
hailfinger3548a9a2009-08-12 14:34:35 +0000116 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +0000117 .init = internal_init,
118 .shutdown = internal_shutdown,
hailfinger11ae3c42009-05-11 14:13:25 +0000119 .map_flash_region = physmap,
120 .unmap_flash_region = physunmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000121 .chip_readb = internal_chip_readb,
122 .chip_readw = internal_chip_readw,
123 .chip_readl = internal_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000124 .chip_readn = internal_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000125 .chip_writeb = internal_chip_writeb,
126 .chip_writew = internal_chip_writew,
127 .chip_writel = internal_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000128 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000129 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +0000130 },
hailfinger80422e22009-12-13 22:28:00 +0000131#endif
stepan927d4e22007-04-04 22:45:58 +0000132
hailfinger90c7d542010-05-31 15:27:27 +0000133#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000134 {
hailfinger3548a9a2009-08-12 14:34:35 +0000135 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000136 .init = dummy_init,
137 .shutdown = dummy_shutdown,
hailfinger11ae3c42009-05-11 14:13:25 +0000138 .map_flash_region = dummy_map,
139 .unmap_flash_region = dummy_unmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000140 .chip_readb = dummy_chip_readb,
141 .chip_readw = dummy_chip_readw,
142 .chip_readl = dummy_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000143 .chip_readn = dummy_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000144 .chip_writeb = dummy_chip_writeb,
145 .chip_writew = dummy_chip_writew,
146 .chip_writel = dummy_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000147 .chip_writen = dummy_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000148 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000149 },
hailfinger571a6b32009-09-16 10:09:21 +0000150#endif
hailfingera9df33c2009-05-09 00:54:55 +0000151
hailfinger90c7d542010-05-31 15:27:27 +0000152#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000153 {
hailfinger3548a9a2009-08-12 14:34:35 +0000154 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000155 .init = nic3com_init,
156 .shutdown = nic3com_shutdown,
uwe3e656bd2009-05-17 23:12:17 +0000157 .map_flash_region = fallback_map,
158 .unmap_flash_region = fallback_unmap,
uwe0f5a3a22009-05-13 11:36:06 +0000159 .chip_readb = nic3com_chip_readb,
hailfinger43716942009-05-16 01:23:55 +0000160 .chip_readw = fallback_chip_readw,
161 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000162 .chip_readn = fallback_chip_readn,
uwe0f5a3a22009-05-13 11:36:06 +0000163 .chip_writeb = nic3com_chip_writeb,
hailfinger43716942009-05-16 01:23:55 +0000164 .chip_writew = fallback_chip_writew,
165 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000166 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000167 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000168 },
hailfinger571a6b32009-09-16 10:09:21 +0000169#endif
uwe0f5a3a22009-05-13 11:36:06 +0000170
hailfinger90c7d542010-05-31 15:27:27 +0000171#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000172 {
173 .name = "nicrealtek",
174 .init = nicrealtek_init,
175 .shutdown = nicrealtek_shutdown,
176 .map_flash_region = fallback_map,
177 .unmap_flash_region = fallback_unmap,
178 .chip_readb = nicrealtek_chip_readb,
179 .chip_readw = fallback_chip_readw,
180 .chip_readl = fallback_chip_readl,
181 .chip_readn = fallback_chip_readn,
182 .chip_writeb = nicrealtek_chip_writeb,
183 .chip_writew = fallback_chip_writew,
184 .chip_writel = fallback_chip_writel,
185 .chip_writen = fallback_chip_writen,
186 .delay = internal_delay,
187 },
188 {
189 .name = "nicsmc1211",
190 .init = nicsmc1211_init,
191 .shutdown = nicrealtek_shutdown,
192 .map_flash_region = fallback_map,
193 .unmap_flash_region = fallback_unmap,
194 .chip_readb = nicrealtek_chip_readb,
195 .chip_readw = fallback_chip_readw,
196 .chip_readl = fallback_chip_readl,
197 .chip_readn = fallback_chip_readn,
198 .chip_writeb = nicrealtek_chip_writeb,
199 .chip_writew = fallback_chip_writew,
200 .chip_writel = fallback_chip_writel,
201 .chip_writen = fallback_chip_writen,
202 .delay = internal_delay,
203 },
204#endif
205
hailfingerf0a368f2010-06-07 22:37:54 +0000206#if CONFIG_NICNATSEMI == 1
207 {
208 .name = "nicnatsemi",
209 .init = nicnatsemi_init,
210 .shutdown = nicnatsemi_shutdown,
211 .map_flash_region = fallback_map,
212 .unmap_flash_region = fallback_unmap,
213 .chip_readb = nicnatsemi_chip_readb,
214 .chip_readw = fallback_chip_readw,
215 .chip_readl = fallback_chip_readl,
216 .chip_readn = fallback_chip_readn,
217 .chip_writeb = nicnatsemi_chip_writeb,
218 .chip_writew = fallback_chip_writew,
219 .chip_writel = fallback_chip_writel,
220 .chip_writen = fallback_chip_writen,
221 .delay = internal_delay,
222 },
223#endif
hailfinger5aa36982010-05-21 21:54:07 +0000224
hailfinger90c7d542010-05-31 15:27:27 +0000225#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000226 {
227 .name = "gfxnvidia",
228 .init = gfxnvidia_init,
229 .shutdown = gfxnvidia_shutdown,
230 .map_flash_region = fallback_map,
231 .unmap_flash_region = fallback_unmap,
232 .chip_readb = gfxnvidia_chip_readb,
233 .chip_readw = fallback_chip_readw,
234 .chip_readl = fallback_chip_readl,
235 .chip_readn = fallback_chip_readn,
236 .chip_writeb = gfxnvidia_chip_writeb,
237 .chip_writew = fallback_chip_writew,
238 .chip_writel = fallback_chip_writel,
239 .chip_writen = fallback_chip_writen,
240 .delay = internal_delay,
241 },
242#endif
243
hailfinger90c7d542010-05-31 15:27:27 +0000244#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000245 {
uwee2f95ef2009-09-02 23:00:46 +0000246 .name = "drkaiser",
247 .init = drkaiser_init,
248 .shutdown = drkaiser_shutdown,
249 .map_flash_region = fallback_map,
250 .unmap_flash_region = fallback_unmap,
251 .chip_readb = drkaiser_chip_readb,
252 .chip_readw = fallback_chip_readw,
253 .chip_readl = fallback_chip_readl,
254 .chip_readn = fallback_chip_readn,
255 .chip_writeb = drkaiser_chip_writeb,
256 .chip_writew = fallback_chip_writew,
257 .chip_writel = fallback_chip_writel,
258 .chip_writen = fallback_chip_writen,
259 .delay = internal_delay,
260 },
hailfinger571a6b32009-09-16 10:09:21 +0000261#endif
uwee2f95ef2009-09-02 23:00:46 +0000262
hailfinger90c7d542010-05-31 15:27:27 +0000263#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000264 {
hailfinger3548a9a2009-08-12 14:34:35 +0000265 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000266 .init = satasii_init,
267 .shutdown = satasii_shutdown,
uwe3e656bd2009-05-17 23:12:17 +0000268 .map_flash_region = fallback_map,
269 .unmap_flash_region = fallback_unmap,
ruikda922a12009-05-17 19:39:27 +0000270 .chip_readb = satasii_chip_readb,
271 .chip_readw = fallback_chip_readw,
272 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000273 .chip_readn = fallback_chip_readn,
ruikda922a12009-05-17 19:39:27 +0000274 .chip_writeb = satasii_chip_writeb,
275 .chip_writew = fallback_chip_writew,
276 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000277 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000278 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000279 },
hailfinger571a6b32009-09-16 10:09:21 +0000280#endif
ruikda922a12009-05-17 19:39:27 +0000281
hailfinger90c7d542010-05-31 15:27:27 +0000282#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000283 {
284 .name = "atahpt",
285 .init = atahpt_init,
286 .shutdown = atahpt_shutdown,
287 .map_flash_region = fallback_map,
288 .unmap_flash_region = fallback_unmap,
289 .chip_readb = atahpt_chip_readb,
290 .chip_readw = fallback_chip_readw,
291 .chip_readl = fallback_chip_readl,
292 .chip_readn = fallback_chip_readn,
293 .chip_writeb = atahpt_chip_writeb,
294 .chip_writew = fallback_chip_writew,
295 .chip_writel = fallback_chip_writel,
296 .chip_writen = fallback_chip_writen,
297 .delay = internal_delay,
298 },
299#endif
300
hailfinger90c7d542010-05-31 15:27:27 +0000301#if CONFIG_INTERNAL == 1
hailfinger324a9cc2010-05-26 01:45:41 +0000302#if defined(__i386__) || defined(__x86_64__)
hailfinger26e212b2009-05-31 18:00:57 +0000303 {
hailfinger3548a9a2009-08-12 14:34:35 +0000304 .name = "it87spi",
hailfinger26e212b2009-05-31 18:00:57 +0000305 .init = it87spi_init,
hailfinger571a6b32009-09-16 10:09:21 +0000306 .shutdown = noop_shutdown,
hailfinger6fe23d62009-08-12 11:39:29 +0000307 .map_flash_region = fallback_map,
308 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000309 .chip_readb = noop_chip_readb,
hailfinger9d987ef2009-06-05 18:32:07 +0000310 .chip_readw = fallback_chip_readw,
311 .chip_readl = fallback_chip_readl,
312 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000313 .chip_writeb = noop_chip_writeb,
hailfinger9d987ef2009-06-05 18:32:07 +0000314 .chip_writew = fallback_chip_writew,
315 .chip_writel = fallback_chip_writel,
316 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000317 .delay = internal_delay,
hailfinger26e212b2009-05-31 18:00:57 +0000318 },
hailfinger80422e22009-12-13 22:28:00 +0000319#endif
hailfinger324a9cc2010-05-26 01:45:41 +0000320#endif
hailfinger26e212b2009-05-31 18:00:57 +0000321
hailfinger90c7d542010-05-31 15:27:27 +0000322#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000323 {
hailfinger90c7d542010-05-31 15:27:27 +0000324 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000325 .init = ft2232_spi_init,
hailfinger571a6b32009-09-16 10:09:21 +0000326 .shutdown = noop_shutdown, /* Missing shutdown */
hailfinger6fe23d62009-08-12 11:39:29 +0000327 .map_flash_region = fallback_map,
328 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000329 .chip_readb = noop_chip_readb,
hailfingerf31da3d2009-06-16 21:08:06 +0000330 .chip_readw = fallback_chip_readw,
331 .chip_readl = fallback_chip_readl,
332 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000333 .chip_writeb = noop_chip_writeb,
hailfingerf31da3d2009-06-16 21:08:06 +0000334 .chip_writew = fallback_chip_writew,
335 .chip_writel = fallback_chip_writel,
336 .chip_writen = fallback_chip_writen,
337 .delay = internal_delay,
338 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000339#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000340
hailfinger90c7d542010-05-31 15:27:27 +0000341#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000342 {
hailfinger3548a9a2009-08-12 14:34:35 +0000343 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000344 .init = serprog_init,
345 .shutdown = serprog_shutdown,
346 .map_flash_region = fallback_map,
347 .unmap_flash_region = fallback_unmap,
348 .chip_readb = serprog_chip_readb,
349 .chip_readw = fallback_chip_readw,
350 .chip_readl = fallback_chip_readl,
351 .chip_readn = serprog_chip_readn,
352 .chip_writeb = serprog_chip_writeb,
353 .chip_writew = fallback_chip_writew,
354 .chip_writel = fallback_chip_writel,
355 .chip_writen = fallback_chip_writen,
356 .delay = serprog_delay,
357 },
hailfinger74d88a72009-08-12 16:17:41 +0000358#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000359
hailfinger90c7d542010-05-31 15:27:27 +0000360#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000361 {
hailfinger90c7d542010-05-31 15:27:27 +0000362 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000363 .init = buspirate_spi_init,
364 .shutdown = buspirate_spi_shutdown,
365 .map_flash_region = fallback_map,
366 .unmap_flash_region = fallback_unmap,
367 .chip_readb = noop_chip_readb,
368 .chip_readw = fallback_chip_readw,
369 .chip_readl = fallback_chip_readl,
370 .chip_readn = fallback_chip_readn,
371 .chip_writeb = noop_chip_writeb,
372 .chip_writew = fallback_chip_writew,
373 .chip_writel = fallback_chip_writel,
374 .chip_writen = fallback_chip_writen,
375 .delay = internal_delay,
376 },
377#endif
378
hailfinger90c7d542010-05-31 15:27:27 +0000379#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000380 {
381 .name = "dediprog",
382 .init = dediprog_init,
383 .shutdown = dediprog_shutdown,
384 .map_flash_region = fallback_map,
385 .unmap_flash_region = fallback_unmap,
386 .chip_readb = noop_chip_readb,
387 .chip_readw = fallback_chip_readw,
388 .chip_readl = fallback_chip_readl,
389 .chip_readn = fallback_chip_readn,
390 .chip_writeb = noop_chip_writeb,
391 .chip_writew = fallback_chip_writew,
392 .chip_writel = fallback_chip_writel,
393 .chip_writen = fallback_chip_writen,
394 .delay = internal_delay,
395 },
396#endif
397
David Hendricks82fd8ae2010-08-04 14:34:54 -0700398#if CONFIG_RAYER_SPI == 1
399 {
400 .name = "rayer_spi",
401 .init = rayer_spi_init,
402 .shutdown = noop_shutdown,
403 .map_flash_region = fallback_map,
404 .unmap_flash_region = fallback_unmap,
405 .chip_readb = noop_chip_readb,
406 .chip_readw = fallback_chip_readw,
407 .chip_readl = fallback_chip_readl,
408 .chip_readn = fallback_chip_readn,
409 .chip_writeb = noop_chip_writeb,
410 .chip_writew = fallback_chip_writew,
411 .chip_writel = fallback_chip_writel,
412 .chip_writen = fallback_chip_writen,
413 .delay = internal_delay,
414 },
415#endif
416
hailfinger3548a9a2009-08-12 14:34:35 +0000417 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000418};
stepan927d4e22007-04-04 22:45:58 +0000419
David Hendricksbf36f092010-11-02 23:39:29 -0700420#define CHIP_RESTORE_MAXFN 4
421static int chip_restore_fn_count = 0;
422struct chip_restore_func_data {
423 CHIP_RESTORE_CALLBACK;
424 struct flashchip *flash;
425 uint8_t status;
426} static chip_restore_fn[CHIP_RESTORE_MAXFN];
427
hailfingerdc6f7972010-02-14 01:20:28 +0000428#define SHUTDOWN_MAXFN 4
429static int shutdown_fn_count = 0;
430struct shutdown_func_data {
431 void (*func) (void *data);
432 void *data;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700433} static shutdown_fn[SHUTDOWN_MAXFN];
434/* Initialize to 0 to make sure nobody registers a shutdown function before
435 * programmer init.
436 */
437static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000438
439/* Register a function to be executed on programmer shutdown.
440 * The advantage over atexit() is that you can supply a void pointer which will
441 * be used as parameter to the registered function upon programmer shutdown.
442 * This pointer can point to arbitrary data used by said function, e.g. undo
443 * information for GPIO settings etc. If unneeded, set data=NULL.
444 * Please note that the first (void *data) belongs to the function signature of
445 * the function passed as first parameter.
446 */
447int register_shutdown(void (*function) (void *data), void *data)
448{
449 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000450 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000451 SHUTDOWN_MAXFN);
452 return 1;
453 }
David Hendricks82fd8ae2010-08-04 14:34:54 -0700454 if (!may_register_shutdown) {
455 msg_perr("Tried to register a shutdown function before "
456 "programmer init.\n");
457 return 1;
458 }
hailfingerdc6f7972010-02-14 01:20:28 +0000459 shutdown_fn[shutdown_fn_count].func = function;
460 shutdown_fn[shutdown_fn_count].data = data;
461 shutdown_fn_count++;
462
463 return 0;
464}
465
David Hendricksbf36f092010-11-02 23:39:29 -0700466//int register_chip_restore(int (*function) (void *data), void *data)
467int register_chip_restore(CHIP_RESTORE_CALLBACK,
468 struct flashchip *flash, uint8_t status)
469{
470 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
471 msg_perr("Tried to register more than %i chip restore"
472 " functions.\n", CHIP_RESTORE_MAXFN);
473 return 1;
474 }
475 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
476 chip_restore_fn[chip_restore_fn_count].flash = flash;
477 chip_restore_fn[chip_restore_fn_count].status = status;
478 chip_restore_fn_count++;
479
480 return 0;
481}
482
David Hendricks82fd8ae2010-08-04 14:34:54 -0700483int programmer_init(char *param)
uweabe92a52009-05-16 22:36:00 +0000484{
David Hendricks82fd8ae2010-08-04 14:34:54 -0700485 int ret;
486 /* Initialize all programmer specific data. */
487 /* Default to unlimited decode sizes. */
488 max_rom_decode = (const struct decode_sizes) {
489 .parallel = 0xffffffff,
490 .lpc = 0xffffffff,
491 .fwh = 0xffffffff,
492 .spi = 0xffffffff
493 };
494 /* Default to Parallel/LPC/FWH flash devices. If a known host controller
495 * is found, the init routine sets the buses_supported bitfield.
496 */
497 buses_supported = CHIP_BUSTYPE_NONSPI;
498 /* Default to top aligned flash at 4 GB. */
499 flashbase = 0;
500 /* Registering shutdown functions is now allowed. */
501 may_register_shutdown = 1;
502 /* Default to allowing writes. Broken programmers set this to 0. */
503 programmer_may_write = 1;
504
505 programmer_param = param;
506 msg_pdbg("Initializing %s programmer\n",
507 programmer_table[programmer].name);
508 ret = programmer_table[programmer].init();
509 if (programmer_param && strlen(programmer_param)) {
510 msg_perr("Unhandled programmer parameters: %s\n",
511 programmer_param);
512 /* Do not error out here, the init itself was successful. */
513 }
514 return ret;
uweabe92a52009-05-16 22:36:00 +0000515}
516
David Hendricksbf36f092010-11-02 23:39:29 -0700517int chip_restore()
518{
519 int rc = 0;
520
521 while (chip_restore_fn_count > 0) {
522 int i = --chip_restore_fn_count;
523 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
524 chip_restore_fn[i].status);
525 }
526
527 return rc;
528}
529
uweabe92a52009-05-16 22:36:00 +0000530int programmer_shutdown(void)
531{
David Hendricks82fd8ae2010-08-04 14:34:54 -0700532 /* Registering shutdown functions is no longer allowed. */
533 may_register_shutdown = 0;
534 while (shutdown_fn_count > 0) {
535 int i = --shutdown_fn_count;
hailfingerdc6f7972010-02-14 01:20:28 +0000536 shutdown_fn[i].func(shutdown_fn[i].data);
David Hendricks82fd8ae2010-08-04 14:34:54 -0700537 }
uweabe92a52009-05-16 22:36:00 +0000538 return programmer_table[programmer].shutdown();
539}
540
541void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
542 size_t len)
543{
544 return programmer_table[programmer].map_flash_region(descr,
545 phys_addr, len);
546}
547
548void programmer_unmap_flash_region(void *virt_addr, size_t len)
549{
550 programmer_table[programmer].unmap_flash_region(virt_addr, len);
551}
552
553void chip_writeb(uint8_t val, chipaddr addr)
554{
555 programmer_table[programmer].chip_writeb(val, addr);
556}
557
558void chip_writew(uint16_t val, chipaddr addr)
559{
560 programmer_table[programmer].chip_writew(val, addr);
561}
562
563void chip_writel(uint32_t val, chipaddr addr)
564{
565 programmer_table[programmer].chip_writel(val, addr);
566}
567
hailfinger9d987ef2009-06-05 18:32:07 +0000568void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
569{
570 programmer_table[programmer].chip_writen(buf, addr, len);
571}
572
uweabe92a52009-05-16 22:36:00 +0000573uint8_t chip_readb(const chipaddr addr)
574{
575 return programmer_table[programmer].chip_readb(addr);
576}
577
578uint16_t chip_readw(const chipaddr addr)
579{
580 return programmer_table[programmer].chip_readw(addr);
581}
582
583uint32_t chip_readl(const chipaddr addr)
584{
585 return programmer_table[programmer].chip_readl(addr);
586}
587
hailfinger9d987ef2009-06-05 18:32:07 +0000588void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
589{
590 programmer_table[programmer].chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000591}
592
hailfingere5829f62009-06-05 17:48:08 +0000593void programmer_delay(int usecs)
594{
595 programmer_table[programmer].delay(usecs);
596}
597
stuge5ff0e6c2009-01-26 00:39:57 +0000598void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000599{
stepan15e64bc2007-05-24 08:48:10 +0000600 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000601 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000602 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfinger82719632009-05-16 21:22:56 +0000603 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000604}
605
hailfinger0f08b7a2009-06-16 08:55:44 +0000606int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger23060112009-05-08 12:49:03 +0000607{
hailfinger0f08b7a2009-06-16 08:55:44 +0000608 chip_readn(buf, flash->virtual_memory + start, len);
hailfinger23060112009-05-08 12:49:03 +0000609
610 return 0;
611}
612
hailfinger7b414742009-06-13 12:04:03 +0000613int min(int a, int b)
614{
615 return (a < b) ? a : b;
616}
617
hailfinger7af83692009-06-15 17:23:36 +0000618int max(int a, int b)
619{
620 return (a > b) ? a : b;
621}
622
hailfingeraec9c962009-10-31 01:53:09 +0000623int bitcount(unsigned long a)
624{
625 int i = 0;
626 for (; a != 0; a >>= 1)
627 if (a & 1)
628 i++;
629 return i;
630}
631
hailfingera916b422009-06-01 02:08:58 +0000632char *strcat_realloc(char *dest, const char *src)
633{
634 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
David Hendricks82fd8ae2010-08-04 14:34:54 -0700635 if (!dest) {
636 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000637 return NULL;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700638 }
hailfingera916b422009-06-01 02:08:58 +0000639 strcat(dest, src);
640 return dest;
641}
642
hailfinger6e5a52a2009-11-24 18:27:10 +0000643/* This is a somewhat hacked function similar in some ways to strtok().
David Hendricks82fd8ae2010-08-04 14:34:54 -0700644 * It will look for needle with a subsequent '=' in haystack, return a copy of
645 * needle and remove everything from the first occurrence of needle to the next
646 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000647 */
648char *extract_param(char **haystack, char *needle, char *delim)
649{
David Hendricks82fd8ae2010-08-04 14:34:54 -0700650 char *param_pos, *opt_pos, *rest;
651 char *opt = NULL;
652 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000653 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000654
hailfingerf4aaccc2010-04-28 15:22:14 +0000655 needlelen = strlen(needle);
656 if (!needlelen) {
657 msg_gerr("%s: empty needle! Please report a bug at "
658 "flashrom@flashrom.org\n", __func__);
659 return NULL;
660 }
661 /* No programmer parameters given. */
662 if (*haystack == NULL)
663 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000664 param_pos = strstr(*haystack, needle);
665 do {
666 if (!param_pos)
667 return NULL;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700668 /* Needle followed by '='? */
669 if (param_pos[needlelen] == '=') {
670
671 /* Beginning of the string? */
672 if (param_pos == *haystack)
673 break;
674 /* After a delimiter? */
675 if (strchr(delim, *(param_pos - 1)))
676 break;
677 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000678 /* Continue searching. */
679 param_pos++;
680 param_pos = strstr(param_pos, needle);
681 } while (1);
David Hendricks82fd8ae2010-08-04 14:34:54 -0700682
hailfinger6e5a52a2009-11-24 18:27:10 +0000683 if (param_pos) {
David Hendricks82fd8ae2010-08-04 14:34:54 -0700684 /* Get the string after needle and '='. */
685 opt_pos = param_pos + needlelen + 1;
686 optlen = strcspn(opt_pos, delim);
687 /* Return an empty string if the parameter was empty. */
688 opt = malloc(optlen + 1);
689 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000690 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000691 exit(1);
692 }
David Hendricks82fd8ae2010-08-04 14:34:54 -0700693 strncpy(opt, opt_pos, optlen);
694 opt[optlen] = '\0';
695 rest = opt_pos + optlen;
696 /* Skip all delimiters after the current parameter. */
697 rest += strspn(rest, delim);
698 memmove(param_pos, rest, strlen(rest) + 1);
699 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000700 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000701
David Hendricks82fd8ae2010-08-04 14:34:54 -0700702 return opt;
703}
704
705char *extract_programmer_param(char *param_name)
706{
707 return extract_param(&programmer_param, param_name, ",");
hailfinger6e5a52a2009-11-24 18:27:10 +0000708}
709
hailfinger7af83692009-06-15 17:23:36 +0000710/* start is an offset to the base address of the flash chip */
711int check_erased_range(struct flashchip *flash, int start, int len)
712{
713 int ret;
714 uint8_t *cmpbuf = malloc(len);
715
716 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000717 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000718 exit(1);
719 }
720 memset(cmpbuf, 0xff, len);
721 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
722 free(cmpbuf);
723 return ret;
724}
725
726/**
hailfinger7af3d192009-11-25 17:05:52 +0000727 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
728 flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000729 * @start offset to the base address of the flash chip
730 * @len length of the verified area
731 * @message string to print in the "FAILED" message
732 * @return 0 for success, -1 for failure
733 */
734int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, char *message)
735{
736 int i, j, starthere, lenhere, ret = 0;
hailfinger7af83692009-06-15 17:23:36 +0000737 int page_size = flash->page_size;
738 uint8_t *readbuf = malloc(page_size);
hailfinger5be6c0f2009-07-23 01:42:56 +0000739 int failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000740
741 if (!len)
742 goto out_free;
743
hailfingerb0f4d122009-06-24 08:20:45 +0000744 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000745 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000746 return 1;
747 }
hailfinger7af83692009-06-15 17:23:36 +0000748 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000749 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000750 exit(1);
751 }
752
753 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000754 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000755 " total_size 0x%x\n", __func__, start, len,
756 flash->total_size * 1024);
757 ret = -1;
758 goto out_free;
759 }
760 if (!message)
761 message = "VERIFY";
762
763 /* Warning: This loop has a very unusual condition and body.
764 * The loop needs to go through each page with at least one affected
765 * byte. The lowest page number is (start / page_size) since that
766 * division rounds down. The highest page number we want is the page
767 * where the last byte of the range lives. That last byte has the
768 * address (start + len - 1), thus the highest page number is
769 * (start + len - 1) / page_size. Since we want to include that last
770 * page as well, the loop condition uses <=.
771 */
772 for (i = start / page_size; i <= (start + len - 1) / page_size; i++) {
773 /* Byte position of the first byte in the range in this page. */
774 starthere = max(start, i * page_size);
775 /* Length of bytes in the range in this page. */
776 lenhere = min(start + len, (i + 1) * page_size) - starthere;
David Hendricks82fd8ae2010-08-04 14:34:54 -0700777 ret = flash->read(flash, readbuf, starthere, lenhere);
778 if (ret) {
779 msg_gerr("Verification impossible because read failed "
780 "at 0x%x (len 0x%x)\n", starthere, lenhere);
781 break;
782 }
hailfinger7af83692009-06-15 17:23:36 +0000783 for (j = 0; j < lenhere; j++) {
784 if (cmpbuf[starthere - start + j] != readbuf[j]) {
hailfinger5be6c0f2009-07-23 01:42:56 +0000785 /* Only print the first failure. */
786 if (!failcount++)
snelsone42c3802010-05-07 20:09:04 +0000787 msg_cerr("%s FAILED at 0x%08x! "
hailfinger5be6c0f2009-07-23 01:42:56 +0000788 "Expected=0x%02x, Read=0x%02x,",
789 message, starthere + j,
790 cmpbuf[starthere - start + j],
791 readbuf[j]);
hailfinger7af83692009-06-15 17:23:36 +0000792 }
793 }
794 }
hailfinger5be6c0f2009-07-23 01:42:56 +0000795 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000796 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
hailfinger5be6c0f2009-07-23 01:42:56 +0000797 start, start + len - 1, failcount);
798 ret = -1;
799 }
hailfinger7af83692009-06-15 17:23:36 +0000800
801out_free:
802 free(readbuf);
803 return ret;
804}
805
hailfingerb247c7a2010-03-08 00:42:32 +0000806/**
807 * Check if the buffer @have can be programmed to the content of @want without
808 * erasing. This is only possible if all chunks of size @gran are either kept
809 * as-is or changed from an all-ones state to any other state.
810 * The following write granularities (enum @gran) are known:
811 * - 1 bit. Each bit can be cleared individually.
812 * - 1 byte. A byte can be written once. Further writes to an already written
813 * byte cause the contents to be either undefined or to stay unchanged.
814 * - 128 bytes. If less than 128 bytes are written, the rest will be
815 * erased. Each write to a 128-byte region will trigger an automatic erase
816 * before anything is written. Very uncommon behaviour and unsupported by
817 * this function.
818 * - 256 bytes. If less than 256 bytes are written, the contents of the
819 * unwritten bytes are undefined.
820 *
821 * @have buffer with current content
822 * @want buffer with desired content
823 * @len length of the verified area
824 * @gran write granularity (enum, not count)
825 * @return 0 if no erase is needed, 1 otherwise
826 */
827int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
828{
829 int result = 0;
830 int i, j, limit;
831
832 switch (gran) {
833 case write_gran_1bit:
834 for (i = 0; i < len; i++)
835 if ((have[i] & want[i]) != want[i]) {
836 result = 1;
837 break;
838 }
839 break;
840 case write_gran_1byte:
841 for (i = 0; i < len; i++)
842 if ((have[i] != want[i]) && (have[i] != 0xff)) {
843 result = 1;
844 break;
845 }
846 break;
847 case write_gran_256bytes:
848 for (j = 0; j < len / 256; j++) {
849 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000850 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000851 if (!memcmp(have + j * 256, want + j * 256, limit))
852 continue;
853 /* have needs to be in erased state. */
854 for (i = 0; i < limit; i++)
855 if (have[i] != 0xff) {
856 result = 1;
857 break;
858 }
859 if (result)
860 break;
861 }
862 break;
863 }
864 return result;
865}
866
hailfinger0c515352009-11-23 12:55:31 +0000867/* This function generates various test patterns useful for testing controller
868 * and chip communication as well as chip behaviour.
869 *
870 * If a byte can be written multiple times, each time keeping 0-bits at 0
871 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
872 * is essentially an AND operation. That's also the reason why this function
873 * provides the result of AND between various patterns.
874 *
875 * Below is a list of patterns (and their block length).
876 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
877 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
878 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
879 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
880 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
881 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
882 * Pattern 6 is 00 (1 Byte)
883 * Pattern 7 is ff (1 Byte)
884 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
885 * byte block.
886 *
887 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
888 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
889 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
890 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
891 * Pattern 12 is 00 (1 Byte)
892 * Pattern 13 is ff (1 Byte)
893 * Patterns 8-13 have no block number.
894 *
895 * Patterns 0-3 are created to detect and efficiently diagnose communication
896 * slips like missed bits or bytes and their repetitive nature gives good visual
897 * cues to the person inspecting the results. In addition, the following holds:
898 * AND Pattern 0/1 == Pattern 4
899 * AND Pattern 2/3 == Pattern 5
900 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
901 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
902 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
903 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
904 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
905 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
906 * Besides that, they provide for bit testing of the last two bytes of every
907 * 256 byte block which contains the block number for patterns 0-6.
908 * Patterns 10-11 are special purpose for detecting subblock aliasing with
909 * block sizes >256 bytes (some Dataflash chips etc.)
910 * AND Pattern 8/9 == Pattern 12
911 * AND Pattern 10/11 == Pattern 12
912 * Pattern 13 is the completely erased state.
913 * None of the patterns can detect aliasing at boundaries which are a multiple
914 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
915 */
916int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
917{
918 int i;
919
920 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000921 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000922 return 1;
923 }
924
925 switch (variant) {
926 case 0:
927 for (i = 0; i < size; i++)
928 buf[i] = (i & 0xf) << 4 | 0x5;
929 break;
930 case 1:
931 for (i = 0; i < size; i++)
932 buf[i] = (i & 0xf) << 4 | 0xa;
933 break;
934 case 2:
935 for (i = 0; i < size; i++)
936 buf[i] = 0x50 | (i & 0xf);
937 break;
938 case 3:
939 for (i = 0; i < size; i++)
940 buf[i] = 0xa0 | (i & 0xf);
941 break;
942 case 4:
943 for (i = 0; i < size; i++)
944 buf[i] = (i & 0xf) << 4;
945 break;
946 case 5:
947 for (i = 0; i < size; i++)
948 buf[i] = i & 0xf;
949 break;
950 case 6:
951 memset(buf, 0x00, size);
952 break;
953 case 7:
954 memset(buf, 0xff, size);
955 break;
956 case 8:
957 for (i = 0; i < size; i++)
958 buf[i] = i & 0xff;
959 break;
960 case 9:
961 for (i = 0; i < size; i++)
962 buf[i] = ~(i & 0xff);
963 break;
964 case 10:
965 for (i = 0; i < size % 2; i++) {
966 buf[i * 2] = (i >> 8) & 0xff;
967 buf[i * 2 + 1] = i & 0xff;
968 }
969 if (size & 0x1)
970 buf[i * 2] = (i >> 8) & 0xff;
971 break;
972 case 11:
973 for (i = 0; i < size % 2; i++) {
974 buf[i * 2] = ~((i >> 8) & 0xff);
975 buf[i * 2 + 1] = ~(i & 0xff);
976 }
977 if (size & 0x1)
978 buf[i * 2] = ~((i >> 8) & 0xff);
979 break;
980 case 12:
981 memset(buf, 0x00, size);
982 break;
983 case 13:
984 memset(buf, 0xff, size);
985 break;
986 }
987
988 if ((variant >= 0) && (variant <= 7)) {
989 /* Write block number in the last two bytes of each 256-byte
990 * block, big endian for easier reading of the hexdump.
991 * Note that this wraps around for chips larger than 2^24 bytes
992 * (16 MB).
993 */
994 for (i = 0; i < size / 256; i++) {
995 buf[i * 256 + 254] = (i >> 8) & 0xff;
996 buf[i * 256 + 255] = i & 0xff;
997 }
998 }
999
1000 return 0;
1001}
1002
hailfingeraec9c962009-10-31 01:53:09 +00001003int check_max_decode(enum chipbustype buses, uint32_t size)
1004{
1005 int limitexceeded = 0;
1006 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
1007 (max_rom_decode.parallel < size)) {
1008 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001009 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001010 "size %u kB of chipset/board/programmer "
1011 "for %s interface, "
1012 "probe/read/erase/write may fail. ", size / 1024,
1013 max_rom_decode.parallel / 1024, "Parallel");
1014 }
1015 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
1016 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001017 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001018 "size %u kB of chipset/board/programmer "
1019 "for %s interface, "
1020 "probe/read/erase/write may fail. ", size / 1024,
1021 max_rom_decode.lpc / 1024, "LPC");
1022 }
1023 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
1024 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001025 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001026 "size %u kB of chipset/board/programmer "
1027 "for %s interface, "
1028 "probe/read/erase/write may fail. ", size / 1024,
1029 max_rom_decode.fwh / 1024, "FWH");
1030 }
1031 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
1032 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001033 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001034 "size %u kB of chipset/board/programmer "
1035 "for %s interface, "
1036 "probe/read/erase/write may fail. ", size / 1024,
1037 max_rom_decode.spi / 1024, "SPI");
1038 }
1039 if (!limitexceeded)
1040 return 0;
1041 /* Sometimes chip and programmer have more than one bus in common,
1042 * and the limit is not exceeded on all buses. Tell the user.
1043 */
1044 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001045 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001046 msg_pdbg("There is at least one common chip/programmer "
hailfingeraec9c962009-10-31 01:53:09 +00001047 "interface which can support a chip of this size. "
1048 "You can try --force at your own risk.\n");
1049 return 1;
1050}
1051
stuge56300c32008-09-03 23:10:05 +00001052struct flashchip *probe_flash(struct flashchip *first_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001053{
stuge56300c32008-09-03 23:10:05 +00001054 struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001055 unsigned long base = 0;
1056 uint32_t size;
1057 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001058 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001059
stuge56300c32008-09-03 23:10:05 +00001060 for (flash = first_flash; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001061 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001062 continue;
hailfinger327d2522010-03-22 23:43:51 +00001063 msg_gdbg("Probing for %s %s, %d KB: ",
stepanb8361b92008-03-17 22:59:40 +00001064 flash->vendor, flash->name, flash->total_size);
stuge98c09aa2008-06-18 02:08:40 +00001065 if (!flash->probe && !force) {
hailfinger327d2522010-03-22 23:43:51 +00001066 msg_gdbg("failed! flashrom has no probe function for "
1067 "this flash chip.\n");
stuge8ce3a3c2008-04-28 14:47:30 +00001068 continue;
1069 }
hailfingeraec9c962009-10-31 01:53:09 +00001070 buses_common = buses_supported & flash->bustype;
1071 if (!buses_common) {
hailfingera916b422009-06-01 02:08:58 +00001072 tmp = flashbuses_to_text(buses_supported);
hailfinger327d2522010-03-22 23:43:51 +00001073 msg_gdbg("skipped.");
1074 msg_gspew(" Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001075 free(tmp);
1076 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001077 msg_gspew("and chip bus type %s are incompatible.",
1078 tmp);
hailfingera916b422009-06-01 02:08:58 +00001079 free(tmp);
hailfinger327d2522010-03-22 23:43:51 +00001080 msg_gdbg("\n");
hailfingera916b422009-06-01 02:08:58 +00001081 continue;
1082 }
stepan782fb172007-04-06 11:58:03 +00001083
ollie5672ac62004-03-17 22:22:08 +00001084 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001085 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001086
hailfinger72d3b982009-05-09 07:27:23 +00001087 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger82719632009-05-16 21:22:56 +00001088 flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001089
stugec1e55fe2008-07-02 17:15:47 +00001090 if (force)
1091 break;
stepanc98b80b2006-03-16 16:57:41 +00001092
stuge56300c32008-09-03 23:10:05 +00001093 if (flash->probe(flash) != 1)
1094 goto notfound;
1095
uwefa98ca12008-10-18 21:14:13 +00001096 if (first_flash == flashchips
1097 || flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001098 break;
1099
stuge56300c32008-09-03 23:10:05 +00001100notfound:
hailfinger82719632009-05-16 21:22:56 +00001101 programmer_unmap_flash_region((void *)flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001102 }
uwebe4477b2007-08-23 16:08:21 +00001103
stugec1e55fe2008-07-02 17:15:47 +00001104 if (!flash || !flash->name)
1105 return NULL;
1106
David Hendricksc6c9f822010-11-03 15:07:01 -07001107 msg_cdbg("%s chip \"%s %s\" (%d KB, %s) at physical address 0x%lx.\n",
hailfingerf4aaccc2010-04-28 15:22:14 +00001108 force ? "Assuming" : "Found",
uwe9e6811e2009-06-28 21:47:57 +00001109 flash->vendor, flash->name, flash->total_size,
1110 flashbuses_to_text(flash->bustype), base);
1111
snelson1ee293c2010-02-19 00:52:10 +00001112 if (flash->printlock)
1113 flash->printlock(flash);
1114
stugec1e55fe2008-07-02 17:15:47 +00001115 return flash;
rminnich8d3ff912003-10-25 17:01:29 +00001116}
1117
David Hendricks456ec8e2010-08-24 15:12:19 -07001118/* Wrapper function for verify_range() to be compatible with do_romentries() of
1119 * layout.c (for -l and -i options).
1120 * verify_range() accepts 5 arguments, and the last argument is printing message
1121 * when error. do_romentries() accepts callback function of 4 arguments so that
1122 * we drop the printing message in this wrapper.
1123 */
1124int verify_range_wrapper(struct flashchip *flash, uint8_t *cmpbuf, const chipaddr start, size_t len) {
1125 return verify_range(flash, &cmpbuf[start], start, len, NULL);
1126}
1127
stepan193c9c22005-12-18 16:41:10 +00001128int verify_flash(struct flashchip *flash, uint8_t *buf)
rminnich8d3ff912003-10-25 17:01:29 +00001129{
hailfingerb0f4d122009-06-24 08:20:45 +00001130 int ret;
rminnich8d3ff912003-10-25 17:01:29 +00001131
snelsone42c3802010-05-07 20:09:04 +00001132 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001133
David Hendricks456ec8e2010-08-24 15:12:19 -07001134 /* FIXME: eventually, verify_range should support the same
1135 functionality as do_romentries with the verify_range_wrapper */
1136// ret = verify_range(flash, buf, 0, total_size, NULL);
1137
1138 ret = do_romentries(buf, flash, verify_range_wrapper);
uwef6641642007-05-09 10:17:44 +00001139
hailfingerb0f4d122009-06-24 08:20:45 +00001140 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001141 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001142
hailfingerb0f4d122009-06-24 08:20:45 +00001143 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001144}
1145
David Hendricks82fd8ae2010-08-04 14:34:54 -07001146int write_buf_to_file(unsigned char *buf, unsigned long size, char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001147{
1148 unsigned long numbytes;
1149 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001150
1151 if (!filename) {
David Hendricks82fd8ae2010-08-04 14:34:54 -07001152 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001153 return 1;
1154 }
oxygenebf70d352010-01-25 22:55:33 +00001155 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001156 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001157 return 1;
David Hendricks82fd8ae2010-08-04 14:34:54 -07001158 }
hailfingerd219a232009-01-28 00:27:54 +00001159
hailfingerd219a232009-01-28 00:27:54 +00001160 numbytes = fwrite(buf, 1, size, image);
1161 fclose(image);
David Hendricks82fd8ae2010-08-04 14:34:54 -07001162 if (numbytes != size) {
1163 msg_gerr("File %s could not be written completely.\n",
1164 filename);
hailfingerd219a232009-01-28 00:27:54 +00001165 return 1;
David Hendricks82fd8ae2010-08-04 14:34:54 -07001166 }
hailfingerd219a232009-01-28 00:27:54 +00001167 return 0;
1168}
1169
David Hendricks82fd8ae2010-08-04 14:34:54 -07001170int read_flash_to_file(struct flashchip *flash, char *filename)
1171{
1172 unsigned long size = flash->total_size * 1024;
1173 unsigned char *buf = calloc(size, sizeof(char));
1174 int ret = 0;
1175
1176 msg_cinfo("Reading flash... ");
1177 if (!buf) {
David Hendricks82fd8ae2010-08-04 14:34:54 -07001178 msg_cinfo("FAILED.\n");
David Hendricksc6c9f822010-11-03 15:07:01 -07001179 msg_gerr("Memory allocation failed!\n");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001180 return 1;
1181 }
1182 if (!flash->read) {
1183 msg_cerr("No read function available for this flash chip.\n");
1184 ret = 1;
1185 goto out_free;
1186 }
1187 if (flash->read(flash, buf, 0, size)) {
1188 msg_cerr("Read operation failed!\n");
1189 ret = 1;
1190 goto out_free;
1191 }
1192
1193 ret = write_buf_to_file(buf, flash->total_size * 1024, filename);
1194out_free:
1195 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001196 if (ret)
1197 msg_cerr("FAILED.");
1198 else
1199 msg_cdbg("done.");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001200 return ret;
1201}
1202
hailfinger9fed35d2010-01-19 06:42:46 +00001203/* This function shares a lot of its structure with erase_flash().
1204 * Even if an error is found, the function will keep going and check the rest.
1205 */
David Hendricks82fd8ae2010-08-04 14:34:54 -07001206static int selfcheck_eraseblocks(struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001207{
hailfinger9fed35d2010-01-19 06:42:46 +00001208 int i, j, k;
1209 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001210
1211 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1212 unsigned int done = 0;
1213 struct block_eraser eraser = flash->block_erasers[k];
1214
1215 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1216 /* Blocks with zero size are bugs in flashchips.c. */
1217 if (eraser.eraseblocks[i].count &&
1218 !eraser.eraseblocks[i].size) {
1219 msg_gerr("ERROR: Flash chip %s erase function "
1220 "%i region %i has size 0. Please report"
1221 " a bug at flashrom@flashrom.org\n",
1222 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001223 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001224 }
1225 /* Blocks with zero count are bugs in flashchips.c. */
1226 if (!eraser.eraseblocks[i].count &&
1227 eraser.eraseblocks[i].size) {
1228 msg_gerr("ERROR: Flash chip %s erase function "
1229 "%i region %i has count 0. Please report"
1230 " a bug at flashrom@flashrom.org\n",
1231 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001232 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001233 }
1234 done += eraser.eraseblocks[i].count *
1235 eraser.eraseblocks[i].size;
1236 }
hailfinger9fed35d2010-01-19 06:42:46 +00001237 /* Empty eraseblock definition with erase function. */
1238 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001239 msg_gspew("Strange: Empty eraseblock definition with "
hailfinger9fed35d2010-01-19 06:42:46 +00001240 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001241 if (!done)
1242 continue;
1243 if (done != flash->total_size * 1024) {
1244 msg_gerr("ERROR: Flash chip %s erase function %i "
1245 "region walking resulted in 0x%06x bytes total,"
1246 " expected 0x%06x bytes. Please report a bug at"
1247 " flashrom@flashrom.org\n", flash->name, k,
1248 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001249 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001250 }
hailfinger9fed35d2010-01-19 06:42:46 +00001251 if (!eraser.block_erase)
1252 continue;
1253 /* Check if there are identical erase functions for different
1254 * layouts. That would imply "magic" erase functions. The
1255 * easiest way to check this is with function pointers.
1256 */
uwef6f94d42010-03-13 17:28:29 +00001257 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001258 if (eraser.block_erase ==
1259 flash->block_erasers[j].block_erase) {
1260 msg_gerr("ERROR: Flash chip %s erase function "
1261 "%i and %i are identical. Please report"
1262 " a bug at flashrom@flashrom.org\n",
1263 flash->name, k, j);
1264 ret = 1;
1265 }
uwef6f94d42010-03-13 17:28:29 +00001266 }
hailfinger45177872010-01-18 08:14:43 +00001267 }
hailfinger9fed35d2010-01-19 06:42:46 +00001268 return ret;
hailfinger45177872010-01-18 08:14:43 +00001269}
1270
David Hendricks82fd8ae2010-08-04 14:34:54 -07001271static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1272 int (*do_something) (struct flashchip *flash,
1273 unsigned int addr,
1274 unsigned int len))
1275{
1276 int i, j;
1277 unsigned int start = 0;
1278 unsigned int len;
1279 struct block_eraser eraser = flash->block_erasers[erasefunction];
1280 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1281 /* count==0 for all automatically initialized array
1282 * members so the loop below won't be executed for them.
1283 */
1284 len = eraser.eraseblocks[i].size;
1285 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
David Hendricks970da352010-08-24 15:03:01 -07001286 msg_cdbg("0x%06x-0x%06x", start,
David Hendricks82fd8ae2010-08-04 14:34:54 -07001287 start + len - 1);
David Hendricks970da352010-08-24 15:03:01 -07001288
1289 /* Don't erase those region not specified by -i
1290 * if -l is specified. */
1291 if (!in_valid_romentry(start)) {
1292 msg_cdbg(" skipped, ");
1293 start += len;
1294 continue;
1295 } else {
1296 msg_cdbg(", ");
1297 }
1298
David Hendricks82fd8ae2010-08-04 14:34:54 -07001299 if (do_something(flash, start, len))
1300 return 1;
David Hendricks970da352010-08-24 15:03:01 -07001301
David Hendricks82fd8ae2010-08-04 14:34:54 -07001302 start += len;
1303 }
1304 }
1305 return 0;
1306}
1307
hailfingerd219a232009-01-28 00:27:54 +00001308int erase_flash(struct flashchip *flash)
1309{
David Hendricks82fd8ae2010-08-04 14:34:54 -07001310 int k, ret = 0, found = 0;
hailfinger7df21362009-09-05 02:30:58 +00001311
David Hendricksc6c9f822010-11-03 15:07:01 -07001312 msg_cdbg("Erasing flash chip... ");
hailfinger7df21362009-09-05 02:30:58 +00001313 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
hailfinger7df21362009-09-05 02:30:58 +00001314 struct block_eraser eraser = flash->block_erasers[k];
1315
snelsone42c3802010-05-07 20:09:04 +00001316 msg_cdbg("Looking at blockwise erase function %i... ", k);
hailfinger7df21362009-09-05 02:30:58 +00001317 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
snelsone42c3802010-05-07 20:09:04 +00001318 msg_cdbg("not defined. "
hailfinger7df21362009-09-05 02:30:58 +00001319 "Looking for another erase function.\n");
1320 continue;
1321 }
1322 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
snelsone42c3802010-05-07 20:09:04 +00001323 msg_cdbg("eraseblock layout is known, but no "
hailfinger7df21362009-09-05 02:30:58 +00001324 "matching block erase function found. "
1325 "Looking for another erase function.\n");
1326 continue;
1327 }
1328 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
snelsone42c3802010-05-07 20:09:04 +00001329 msg_cdbg("block erase function found, but "
hailfinger7df21362009-09-05 02:30:58 +00001330 "eraseblock layout is unknown. "
1331 "Looking for another erase function.\n");
1332 continue;
1333 }
1334 found = 1;
snelsone42c3802010-05-07 20:09:04 +00001335 msg_cdbg("trying... ");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001336 ret = walk_eraseregions(flash, k, eraser.block_erase);
snelsone42c3802010-05-07 20:09:04 +00001337 msg_cdbg("\n");
hailfinger7df21362009-09-05 02:30:58 +00001338 /* If everything is OK, don't try another erase function. */
1339 if (!ret)
1340 break;
1341 }
hailfinger7df21362009-09-05 02:30:58 +00001342 if (!found) {
snelsone42c3802010-05-07 20:09:04 +00001343 msg_cerr("ERROR: flashrom has no erase function for this flash chip.\n");
hailfingerd219a232009-01-28 00:27:54 +00001344 return 1;
1345 }
hailfinger1e9ee0f2009-05-08 17:15:15 +00001346
hailfinger7df21362009-09-05 02:30:58 +00001347 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001348 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001349 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001350 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001351 }
1352 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001353}
1354
uweb34ec9f2009-10-01 18:40:02 +00001355void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001356{
snelsone42c3802010-05-07 20:09:04 +00001357 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001358 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger74819ad2010-05-15 15:04:37 +00001359 "mail flashrom@flashrom.org!\n"
1360 "-------------------------------------------------------------"
1361 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001362 "DO NOT REBOOT OR POWEROFF!\n");
1363}
1364
hailfingerc77acb52009-12-24 02:15:55 +00001365/* The way to go if you want a delimited list of programmers*/
1366void list_programmers(char *delim)
1367{
1368 enum programmer p;
1369 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001370 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001371 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001372 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001373 }
snelsone42c3802010-05-07 20:09:04 +00001374 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001375}
1376
hailfinger3b471632010-03-27 16:36:40 +00001377void print_sysinfo(void)
1378{
David Hendricksc6c9f822010-11-03 15:07:01 -07001379 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001380#if HAVE_UTSNAME == 1
1381 struct utsname osinfo;
1382 uname(&osinfo);
1383
David Hendricksc6c9f822010-11-03 15:07:01 -07001384 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001385 osinfo.machine);
1386#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001387 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001388#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001389 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001390#if NEED_PCI == 1
1391#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001392 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001393#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001394 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001395#endif
1396#endif
1397#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001398 msg_gerr(" LLVM Clang");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001399#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001400 msg_gerr(" %s,", __clang_version__);
David Hendricks82fd8ae2010-08-04 14:34:54 -07001401#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001402 msg_gerr(" unknown version (before r102686),");
David Hendricks82fd8ae2010-08-04 14:34:54 -07001403#endif
hailfinger3b471632010-03-27 16:36:40 +00001404#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001405 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001406#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001407 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001408#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001409 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001410#endif
1411#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001412 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001413#endif
1414#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001415 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001416#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001417 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001418#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001419 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001420}
1421
uwefdeca092008-01-21 15:24:22 +00001422void print_version(void)
1423{
David Hendricksc6c9f822010-11-03 15:07:01 -07001424 /* send to stderr for chromium os */
1425 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001426 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001427}
1428
hailfinger74819ad2010-05-15 15:04:37 +00001429void print_banner(void)
1430{
1431 msg_ginfo("flashrom is free software, get the source code at "
1432 "http://www.flashrom.org\n");
1433 msg_ginfo("\n");
1434}
1435
hailfingerc77acb52009-12-24 02:15:55 +00001436int selfcheck(void)
1437{
hailfinger45177872010-01-18 08:14:43 +00001438 int ret = 0;
1439 struct flashchip *flash;
1440
1441 /* Safety check. Instead of aborting after the first error, check
1442 * if more errors exist.
1443 */
hailfingerc77acb52009-12-24 02:15:55 +00001444 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001445 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001446 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001447 }
1448 if (spi_programmer_count - 1 != SPI_CONTROLLER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001449 msg_gerr("SPI programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001450 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001451 }
hailfinger45177872010-01-18 08:14:43 +00001452 for (flash = flashchips; flash && flash->name; flash++)
1453 if (selfcheck_eraseblocks(flash))
1454 ret = 1;
1455 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001456}
1457
1458void check_chip_supported(struct flashchip *flash)
1459{
1460 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
snelsone42c3802010-05-07 20:09:04 +00001461 msg_cinfo("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001462 if (flash->tested & TEST_BAD_MASK) {
snelsone42c3802010-05-07 20:09:04 +00001463 msg_cinfo("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001464 if (flash->tested & TEST_BAD_PROBE)
snelsone42c3802010-05-07 20:09:04 +00001465 msg_cinfo(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001466 if (flash->tested & TEST_BAD_READ)
snelsone42c3802010-05-07 20:09:04 +00001467 msg_cinfo(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001468 if (flash->tested & TEST_BAD_ERASE)
snelsone42c3802010-05-07 20:09:04 +00001469 msg_cinfo(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001470 if (flash->tested & TEST_BAD_WRITE)
snelsone42c3802010-05-07 20:09:04 +00001471 msg_cinfo(" WRITE");
1472 msg_cinfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001473 }
1474 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1475 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1476 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1477 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
snelsone42c3802010-05-07 20:09:04 +00001478 msg_cinfo("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001479 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
snelsone42c3802010-05-07 20:09:04 +00001480 msg_cinfo(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001481 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
snelsone42c3802010-05-07 20:09:04 +00001482 msg_cinfo(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001483 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
snelsone42c3802010-05-07 20:09:04 +00001484 msg_cinfo(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001485 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
snelsone42c3802010-05-07 20:09:04 +00001486 msg_cinfo(" WRITE");
1487 msg_cinfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001488 }
hailfinger92cd8e32010-01-07 03:24:05 +00001489 /* FIXME: This message is designed towards CLI users. */
hailfinger74819ad2010-05-15 15:04:37 +00001490 msg_cinfo("The test status of this chip may have been updated "
1491 "in the latest development\n"
1492 "version of flashrom. If you are running the latest "
1493 "development version,\n"
1494 "please email a report to flashrom@flashrom.org if "
1495 "any of the above operations\n"
1496 "work correctly for you with this flash part. Please "
1497 "include the flashrom\n"
1498 "output with the additional -V option for all "
1499 "operations you tested (-V, -Vr,\n"
1500 "-Vw, -VE), and mention which mainboard or "
1501 "programmer you tested.\n"
1502 "Thanks for your help!\n"
1503 "===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001504 }
1505}
1506
ollie5b621572004-03-20 16:46:10 +00001507int main(int argc, char *argv[])
rminnich8d3ff912003-10-25 17:01:29 +00001508{
David Hendricksf7924d12010-06-10 21:26:44 -07001509 /* FIXME: this should eventually be a build option controlled
1510 via a USE flag */
1511// return cli_classic(argc, argv);
1512 return cli_mfg(argc, argv);
hailfingerc77acb52009-12-24 02:15:55 +00001513}
1514
1515/* This function signature is horrible. We need to design a better interface,
1516 * but right now it allows us to split off the CLI code.
1517 */
1518int doit(struct flashchip *flash, int force, char *filename, int read_it, int write_it, int erase_it, int verify_it)
1519{
1520 uint8_t *buf;
1521 unsigned long numbytes;
1522 FILE *image;
1523 int ret = 0;
1524 unsigned long size;
1525
1526 size = flash->total_size * 1024;
ollie6a600992005-11-26 21:55:36 +00001527 buf = (uint8_t *) calloc(size, sizeof(char));
uwef6641642007-05-09 10:17:44 +00001528
David Hendricks82fd8ae2010-08-04 14:34:54 -07001529 if (!programmer_may_write && (write_it || erase_it)) {
1530 msg_perr("Write/erase is not working yet on your programmer in "
1531 "its current configuration.\n");
1532 /* --force is the wrong approach, but it's the best we can do
1533 * until the generic programmer parameter parser is merged.
1534 */
1535 if (!force) {
1536 msg_perr("Aborting.\n");
1537 programmer_shutdown();
1538 return 1;
1539 } else {
1540 msg_cerr("Continuing anyway.\n");
1541 }
1542 }
1543
ollie0eb62d62004-12-08 20:10:01 +00001544 if (erase_it) {
hailfinger0459e1c2009-08-19 13:55:34 +00001545 if (flash->tested & TEST_BAD_ERASE) {
snelsone42c3802010-05-07 20:09:04 +00001546 msg_cerr("Erase is not working on this chip. ");
hailfinger0459e1c2009-08-19 13:55:34 +00001547 if (!force) {
snelsone42c3802010-05-07 20:09:04 +00001548 msg_cerr("Aborting.\n");
uweff4576d2009-09-30 18:29:55 +00001549 programmer_shutdown();
hailfinger0459e1c2009-08-19 13:55:34 +00001550 return 1;
1551 } else {
snelsone42c3802010-05-07 20:09:04 +00001552 msg_cerr("Continuing anyway.\n");
hailfinger0459e1c2009-08-19 13:55:34 +00001553 }
1554 }
snelson1ee293c2010-02-19 00:52:10 +00001555 if (flash->unlock)
1556 flash->unlock(flash);
1557
hailfinger0459e1c2009-08-19 13:55:34 +00001558 if (erase_flash(flash)) {
1559 emergency_help_message();
uweff4576d2009-09-30 18:29:55 +00001560 programmer_shutdown();
stuge8ce3a3c2008-04-28 14:47:30 +00001561 return 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001562 }
ollie0eb62d62004-12-08 20:10:01 +00001563 } else if (read_it) {
snelson1ee293c2010-02-19 00:52:10 +00001564 if (flash->unlock)
1565 flash->unlock(flash);
1566
David Hendricks82fd8ae2010-08-04 14:34:54 -07001567 if (read_flash_to_file(flash, filename)) {
uweff4576d2009-09-30 18:29:55 +00001568 programmer_shutdown();
stugebbcc2f12009-01-12 21:00:35 +00001569 return 1;
uweff4576d2009-09-30 18:29:55 +00001570 }
ollie5672ac62004-03-17 22:22:08 +00001571 } else {
stepan1da96c02006-11-21 23:48:51 +00001572 struct stat image_stat;
1573
snelson1ee293c2010-02-19 00:52:10 +00001574 if (flash->unlock)
1575 flash->unlock(flash);
1576
hailfinger0459e1c2009-08-19 13:55:34 +00001577 if (flash->tested & TEST_BAD_ERASE) {
snelsone42c3802010-05-07 20:09:04 +00001578 msg_cerr("Erase is not working on this chip "
hailfinger0459e1c2009-08-19 13:55:34 +00001579 "and erase is needed for write. ");
1580 if (!force) {
snelsone42c3802010-05-07 20:09:04 +00001581 msg_cerr("Aborting.\n");
uweff4576d2009-09-30 18:29:55 +00001582 programmer_shutdown();
hailfinger0459e1c2009-08-19 13:55:34 +00001583 return 1;
1584 } else {
snelsone42c3802010-05-07 20:09:04 +00001585 msg_cerr("Continuing anyway.\n");
hailfinger0459e1c2009-08-19 13:55:34 +00001586 }
1587 }
1588 if (flash->tested & TEST_BAD_WRITE) {
snelsone42c3802010-05-07 20:09:04 +00001589 msg_cerr("Write is not working on this chip. ");
hailfinger0459e1c2009-08-19 13:55:34 +00001590 if (!force) {
snelsone42c3802010-05-07 20:09:04 +00001591 msg_cerr("Aborting.\n");
uweff4576d2009-09-30 18:29:55 +00001592 programmer_shutdown();
hailfinger0459e1c2009-08-19 13:55:34 +00001593 return 1;
1594 } else {
snelsone42c3802010-05-07 20:09:04 +00001595 msg_cerr("Continuing anyway.\n");
hailfinger0459e1c2009-08-19 13:55:34 +00001596 }
1597 }
oxygenebf70d352010-01-25 22:55:33 +00001598 if ((image = fopen(filename, "rb")) == NULL) {
ollie5672ac62004-03-17 22:22:08 +00001599 perror(filename);
uweff4576d2009-09-30 18:29:55 +00001600 programmer_shutdown();
ollie5672ac62004-03-17 22:22:08 +00001601 exit(1);
1602 }
stepan1da96c02006-11-21 23:48:51 +00001603 if (fstat(fileno(image), &image_stat) != 0) {
1604 perror(filename);
uweff4576d2009-09-30 18:29:55 +00001605 programmer_shutdown();
stepan1da96c02006-11-21 23:48:51 +00001606 exit(1);
1607 }
uwef6641642007-05-09 10:17:44 +00001608 if (image_stat.st_size != flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +00001609 msg_gerr("Error: Image size doesn't match\n");
uweff4576d2009-09-30 18:29:55 +00001610 programmer_shutdown();
stepan1da96c02006-11-21 23:48:51 +00001611 exit(1);
1612 }
1613
stugebbcc2f12009-01-12 21:00:35 +00001614 numbytes = fread(buf, 1, size, image);
hailfinger90c7d542010-05-31 15:27:27 +00001615#if CONFIG_INTERNAL == 1
stuge98c09aa2008-06-18 02:08:40 +00001616 show_id(buf, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001617#endif
ollie5672ac62004-03-17 22:22:08 +00001618 fclose(image);
stugebbcc2f12009-01-12 21:00:35 +00001619 if (numbytes != size) {
snelsone42c3802010-05-07 20:09:04 +00001620 msg_gerr("Error: Failed to read file. Got %ld bytes, wanted %ld!\n", numbytes, size);
uweff4576d2009-09-30 18:29:55 +00001621 programmer_shutdown();
stugebbcc2f12009-01-12 21:00:35 +00001622 return 1;
1623 }
ollie5672ac62004-03-17 22:22:08 +00001624 }
1625
ollie6a600992005-11-26 21:55:36 +00001626 // This should be moved into each flash part's code to do it
1627 // cleanly. This does the job.
hailfinger051b3442009-08-19 15:19:18 +00001628 handle_romentries(buf, flash);
uwef6641642007-05-09 10:17:44 +00001629
ollie6a600992005-11-26 21:55:36 +00001630 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00001631
stuge8ce3a3c2008-04-28 14:47:30 +00001632 if (write_it) {
David Hendricksc6c9f822010-11-03 15:07:01 -07001633 msg_cinfo("Writing flash chip...\n");
stuge8ce3a3c2008-04-28 14:47:30 +00001634 if (!flash->write) {
snelsone42c3802010-05-07 20:09:04 +00001635 msg_cerr("Error: flashrom has no write function for this flash chip.\n");
uweff4576d2009-09-30 18:29:55 +00001636 programmer_shutdown();
stuge8ce3a3c2008-04-28 14:47:30 +00001637 return 1;
1638 }
hailfingerdffbe6b2009-07-24 12:18:54 +00001639 ret = flash->write(flash, buf);
1640 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001641 msg_cerr("FAILED!\n");
hailfinger0459e1c2009-08-19 13:55:34 +00001642 emergency_help_message();
uweff4576d2009-09-30 18:29:55 +00001643 programmer_shutdown();
hailfingerdffbe6b2009-07-24 12:18:54 +00001644 return 1;
1645 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001646 msg_cdbg("COMPLETE.\n");
hailfingerdffbe6b2009-07-24 12:18:54 +00001647 }
stuge8ce3a3c2008-04-28 14:47:30 +00001648 }
ollie6a600992005-11-26 21:55:36 +00001649
hailfinger0459e1c2009-08-19 13:55:34 +00001650 if (verify_it) {
1651 /* Work around chips which need some time to calm down. */
1652 if (write_it)
1653 programmer_delay(1000*1000);
hailfingerdffbe6b2009-07-24 12:18:54 +00001654 ret = verify_flash(flash, buf);
hailfingera50d60e2009-11-17 09:57:34 +00001655 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00001656 * might have an emergency situation.
1657 */
1658 if (ret && write_it)
1659 emergency_help_message();
1660 }
ollie6a600992005-11-26 21:55:36 +00001661
David Hendricksbf36f092010-11-02 23:39:29 -07001662 chip_restore(); /* must be done before programmer_shutdown() */
1663 programmer_shutdown(); /* must be done after chip_restore() */
hailfingerabe249e2009-05-08 17:43:22 +00001664
David Hendricksc6c9f822010-11-03 15:07:01 -07001665 msg_ginfo("%s\n", ret ? "FAILED" : "SUCCESS");
stepan83eca252006-01-04 16:42:57 +00001666 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001667}