blob: c77336879e843ce1503cf2949a2953dc710aff45 [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
uwe4475e902009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
hailfinger23060112009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
uweb25f1ea2007-08-29 17:52:32 +000019 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
rminnich8d3ff912003-10-25 17:01:29 +000022 */
23
hailfingera83a5fe2010-05-30 22:24:40 +000024#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000026#ifndef __LIBPAYLOAD__
27#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000028#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000029#endif
rminnich8d3ff912003-10-25 17:01:29 +000030#include <string.h>
31#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000032#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000033#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
rminnich8d3ff912003-10-25 17:01:29 +000037#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000038#include "flashchips.h"
hailfinger428f6852010-07-27 22:41:39 +000039#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000040
krause2eb76212011-01-17 07:50:42 +000041const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000042char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000043int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000044
hailfinger90c7d542010-05-31 15:27:27 +000045#if CONFIG_INTERNAL == 1
hailfinger6fe23d62009-08-12 11:39:29 +000046enum programmer programmer = PROGRAMMER_INTERNAL;
hailfinger90c7d542010-05-31 15:27:27 +000047#elif CONFIG_DUMMY == 1
hailfinger80422e22009-12-13 22:28:00 +000048enum programmer programmer = PROGRAMMER_DUMMY;
49#else
hailfingerf95c8f62010-01-10 13:28:48 +000050/* If neither internal nor dummy are selected, we must pick a sensible default.
51 * Since there is no reason to prefer a particular external programmer, we fail
52 * if more than one of them is selected. If only one is selected, it is clear
53 * that the user wants that one to become the default.
54 */
hailfinger7949b652011-05-08 00:24:18 +000055#if CONFIG_NIC3COM+CONFIG_NICREALTEK+CONFIG_NICNATSEMI+CONFIG_GFXNVIDIA+CONFIG_DRKAISER+CONFIG_SATASII+CONFIG_ATAHPT+CONFIG_FT2232_SPI+CONFIG_SERPROG+CONFIG_BUSPIRATE_SPI+CONFIG_DEDIPROG+CONFIG_RAYER_SPI+CONFIG_NICINTEL+CONFIG_NICINTEL_SPI+CONFIG_OGP_SPI+CONFIG_SATAMV > 1
hailfinger90c7d542010-05-31 15:27:27 +000056#error Please enable either CONFIG_DUMMY or CONFIG_INTERNAL or disable support for all programmers except one.
hailfingerf95c8f62010-01-10 13:28:48 +000057#endif
58enum programmer programmer =
hailfinger90c7d542010-05-31 15:27:27 +000059#if CONFIG_NIC3COM == 1
hailfingerf95c8f62010-01-10 13:28:48 +000060 PROGRAMMER_NIC3COM
61#endif
hailfinger90c7d542010-05-31 15:27:27 +000062#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +000063 PROGRAMMER_NICREALTEK
hailfinger5aa36982010-05-21 21:54:07 +000064#endif
hailfingerf0a368f2010-06-07 22:37:54 +000065#if CONFIG_NICNATSEMI == 1
66 PROGRAMMER_NICNATSEMI
67#endif
hailfinger90c7d542010-05-31 15:27:27 +000068#if CONFIG_GFXNVIDIA == 1
hailfingerf95c8f62010-01-10 13:28:48 +000069 PROGRAMMER_GFXNVIDIA
70#endif
hailfinger90c7d542010-05-31 15:27:27 +000071#if CONFIG_DRKAISER == 1
hailfingerf95c8f62010-01-10 13:28:48 +000072 PROGRAMMER_DRKAISER
73#endif
hailfinger90c7d542010-05-31 15:27:27 +000074#if CONFIG_SATASII == 1
hailfingerf95c8f62010-01-10 13:28:48 +000075 PROGRAMMER_SATASII
76#endif
hailfinger90c7d542010-05-31 15:27:27 +000077#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +000078 PROGRAMMER_ATAHPT
79#endif
hailfinger90c7d542010-05-31 15:27:27 +000080#if CONFIG_FT2232_SPI == 1
81 PROGRAMMER_FT2232_SPI
hailfingerf95c8f62010-01-10 13:28:48 +000082#endif
hailfinger90c7d542010-05-31 15:27:27 +000083#if CONFIG_SERPROG == 1
hailfingerf95c8f62010-01-10 13:28:48 +000084 PROGRAMMER_SERPROG
85#endif
hailfinger90c7d542010-05-31 15:27:27 +000086#if CONFIG_BUSPIRATE_SPI == 1
87 PROGRAMMER_BUSPIRATE_SPI
hailfingerf95c8f62010-01-10 13:28:48 +000088#endif
hailfinger90c7d542010-05-31 15:27:27 +000089#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +000090 PROGRAMMER_DEDIPROG
91#endif
hailfinger52c4fa02010-07-21 10:26:01 +000092#if CONFIG_RAYER_SPI == 1
93 PROGRAMMER_RAYER_SPI
94#endif
hailfinger7949b652011-05-08 00:24:18 +000095#if CONFIG_NICINTEL == 1
96 PROGRAMMER_NICINTEL
97#endif
uwe6764e922010-09-03 18:21:21 +000098#if CONFIG_NICINTEL_SPI == 1
99 PROGRAMMER_NICINTEL_SPI
100#endif
hailfingerfb1f31f2010-12-03 14:48:11 +0000101#if CONFIG_OGP_SPI == 1
102 PROGRAMMER_OGP_SPI
103#endif
hailfinger935365d2011-02-04 21:37:59 +0000104#if CONFIG_SATAMV == 1
105 PROGRAMMER_SATAMV
106#endif
hailfingerf95c8f62010-01-10 13:28:48 +0000107;
hailfinger80422e22009-12-13 22:28:00 +0000108#endif
109
hailfingerddeb4ac2010-07-08 10:13:37 +0000110static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +0000111
hailfinger1ff33dc2010-07-03 11:02:10 +0000112/* Supported buses for the current programmer. */
113enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +0000114
uwee15beb92010-08-08 17:01:18 +0000115/*
hailfinger80422e22009-12-13 22:28:00 +0000116 * Programmers supporting multiple buses can have differing size limits on
117 * each bus. Store the limits for each bus in a common struct.
118 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000119struct decode_sizes max_rom_decode;
120
121/* If nonzero, used as the start address of bottom-aligned flash. */
122unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +0000123
hailfinger5828baf2010-07-03 12:14:25 +0000124/* Is writing allowed with this programmer? */
125int programmer_may_write;
126
hailfingerabe249e2009-05-08 17:43:22 +0000127const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +0000128#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +0000129 {
hailfinger3548a9a2009-08-12 14:34:35 +0000130 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +0000131 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000132 .map_flash_region = physmap,
133 .unmap_flash_region = physunmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000134 .chip_readb = internal_chip_readb,
135 .chip_readw = internal_chip_readw,
136 .chip_readl = internal_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000137 .chip_readn = internal_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000138 .chip_writeb = internal_chip_writeb,
139 .chip_writew = internal_chip_writew,
140 .chip_writel = internal_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000141 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000142 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +0000143 },
hailfinger80422e22009-12-13 22:28:00 +0000144#endif
stepan927d4e22007-04-04 22:45:58 +0000145
hailfinger90c7d542010-05-31 15:27:27 +0000146#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000147 {
hailfinger3548a9a2009-08-12 14:34:35 +0000148 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000149 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000150 .map_flash_region = dummy_map,
151 .unmap_flash_region = dummy_unmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000152 .chip_readb = dummy_chip_readb,
153 .chip_readw = dummy_chip_readw,
154 .chip_readl = dummy_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000155 .chip_readn = dummy_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000156 .chip_writeb = dummy_chip_writeb,
157 .chip_writew = dummy_chip_writew,
158 .chip_writel = dummy_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000159 .chip_writen = dummy_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000160 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000161 },
hailfinger571a6b32009-09-16 10:09:21 +0000162#endif
hailfingera9df33c2009-05-09 00:54:55 +0000163
hailfinger90c7d542010-05-31 15:27:27 +0000164#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000165 {
hailfinger3548a9a2009-08-12 14:34:35 +0000166 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000167 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000168 .map_flash_region = fallback_map,
169 .unmap_flash_region = fallback_unmap,
uwe0f5a3a22009-05-13 11:36:06 +0000170 .chip_readb = nic3com_chip_readb,
hailfinger43716942009-05-16 01:23:55 +0000171 .chip_readw = fallback_chip_readw,
172 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000173 .chip_readn = fallback_chip_readn,
uwe0f5a3a22009-05-13 11:36:06 +0000174 .chip_writeb = nic3com_chip_writeb,
hailfinger43716942009-05-16 01:23:55 +0000175 .chip_writew = fallback_chip_writew,
176 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000177 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000178 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000179 },
hailfinger571a6b32009-09-16 10:09:21 +0000180#endif
uwe0f5a3a22009-05-13 11:36:06 +0000181
hailfinger90c7d542010-05-31 15:27:27 +0000182#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000183 {
hailfinger0d703d42011-03-07 01:08:09 +0000184 /* This programmer works for Realtek RTL8139 and SMC 1211. */
hailfinger5aa36982010-05-21 21:54:07 +0000185 .name = "nicrealtek",
hailfinger0d703d42011-03-07 01:08:09 +0000186 //.name = "nicsmc1211",
hailfinger5aa36982010-05-21 21:54:07 +0000187 .init = nicrealtek_init,
hailfinger5aa36982010-05-21 21:54:07 +0000188 .map_flash_region = fallback_map,
189 .unmap_flash_region = fallback_unmap,
190 .chip_readb = nicrealtek_chip_readb,
191 .chip_readw = fallback_chip_readw,
192 .chip_readl = fallback_chip_readl,
193 .chip_readn = fallback_chip_readn,
194 .chip_writeb = nicrealtek_chip_writeb,
195 .chip_writew = fallback_chip_writew,
196 .chip_writel = fallback_chip_writel,
197 .chip_writen = fallback_chip_writen,
198 .delay = internal_delay,
199 },
hailfinger5aa36982010-05-21 21:54:07 +0000200#endif
201
hailfingerf0a368f2010-06-07 22:37:54 +0000202#if CONFIG_NICNATSEMI == 1
203 {
204 .name = "nicnatsemi",
205 .init = nicnatsemi_init,
hailfingerf0a368f2010-06-07 22:37:54 +0000206 .map_flash_region = fallback_map,
207 .unmap_flash_region = fallback_unmap,
208 .chip_readb = nicnatsemi_chip_readb,
209 .chip_readw = fallback_chip_readw,
210 .chip_readl = fallback_chip_readl,
211 .chip_readn = fallback_chip_readn,
212 .chip_writeb = nicnatsemi_chip_writeb,
213 .chip_writew = fallback_chip_writew,
214 .chip_writel = fallback_chip_writel,
215 .chip_writen = fallback_chip_writen,
216 .delay = internal_delay,
217 },
218#endif
hailfinger5aa36982010-05-21 21:54:07 +0000219
hailfinger90c7d542010-05-31 15:27:27 +0000220#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000221 {
222 .name = "gfxnvidia",
223 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000224 .map_flash_region = fallback_map,
225 .unmap_flash_region = fallback_unmap,
226 .chip_readb = gfxnvidia_chip_readb,
227 .chip_readw = fallback_chip_readw,
228 .chip_readl = fallback_chip_readl,
229 .chip_readn = fallback_chip_readn,
230 .chip_writeb = gfxnvidia_chip_writeb,
231 .chip_writew = fallback_chip_writew,
232 .chip_writel = fallback_chip_writel,
233 .chip_writen = fallback_chip_writen,
234 .delay = internal_delay,
235 },
236#endif
237
hailfinger90c7d542010-05-31 15:27:27 +0000238#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000239 {
uwee2f95ef2009-09-02 23:00:46 +0000240 .name = "drkaiser",
241 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000242 .map_flash_region = fallback_map,
243 .unmap_flash_region = fallback_unmap,
244 .chip_readb = drkaiser_chip_readb,
245 .chip_readw = fallback_chip_readw,
246 .chip_readl = fallback_chip_readl,
247 .chip_readn = fallback_chip_readn,
248 .chip_writeb = drkaiser_chip_writeb,
249 .chip_writew = fallback_chip_writew,
250 .chip_writel = fallback_chip_writel,
251 .chip_writen = fallback_chip_writen,
252 .delay = internal_delay,
253 },
hailfinger571a6b32009-09-16 10:09:21 +0000254#endif
uwee2f95ef2009-09-02 23:00:46 +0000255
hailfinger90c7d542010-05-31 15:27:27 +0000256#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000257 {
hailfinger3548a9a2009-08-12 14:34:35 +0000258 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000259 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000260 .map_flash_region = fallback_map,
261 .unmap_flash_region = fallback_unmap,
ruikda922a12009-05-17 19:39:27 +0000262 .chip_readb = satasii_chip_readb,
263 .chip_readw = fallback_chip_readw,
264 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000265 .chip_readn = fallback_chip_readn,
ruikda922a12009-05-17 19:39:27 +0000266 .chip_writeb = satasii_chip_writeb,
267 .chip_writew = fallback_chip_writew,
268 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000269 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000270 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000271 },
hailfinger571a6b32009-09-16 10:09:21 +0000272#endif
ruikda922a12009-05-17 19:39:27 +0000273
hailfinger90c7d542010-05-31 15:27:27 +0000274#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000275 {
276 .name = "atahpt",
277 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000278 .map_flash_region = fallback_map,
279 .unmap_flash_region = fallback_unmap,
280 .chip_readb = atahpt_chip_readb,
281 .chip_readw = fallback_chip_readw,
282 .chip_readl = fallback_chip_readl,
283 .chip_readn = fallback_chip_readn,
284 .chip_writeb = atahpt_chip_writeb,
285 .chip_writew = fallback_chip_writew,
286 .chip_writel = fallback_chip_writel,
287 .chip_writen = fallback_chip_writen,
288 .delay = internal_delay,
289 },
290#endif
291
hailfinger90c7d542010-05-31 15:27:27 +0000292#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000293 {
hailfinger90c7d542010-05-31 15:27:27 +0000294 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000295 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000296 .map_flash_region = fallback_map,
297 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000298 .chip_readb = noop_chip_readb,
hailfingerf31da3d2009-06-16 21:08:06 +0000299 .chip_readw = fallback_chip_readw,
300 .chip_readl = fallback_chip_readl,
301 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000302 .chip_writeb = noop_chip_writeb,
hailfingerf31da3d2009-06-16 21:08:06 +0000303 .chip_writew = fallback_chip_writew,
304 .chip_writel = fallback_chip_writel,
305 .chip_writen = fallback_chip_writen,
306 .delay = internal_delay,
307 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000308#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000309
hailfinger90c7d542010-05-31 15:27:27 +0000310#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000311 {
hailfinger3548a9a2009-08-12 14:34:35 +0000312 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000313 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000314 .map_flash_region = fallback_map,
315 .unmap_flash_region = fallback_unmap,
316 .chip_readb = serprog_chip_readb,
317 .chip_readw = fallback_chip_readw,
318 .chip_readl = fallback_chip_readl,
319 .chip_readn = serprog_chip_readn,
320 .chip_writeb = serprog_chip_writeb,
321 .chip_writew = fallback_chip_writew,
322 .chip_writel = fallback_chip_writel,
323 .chip_writen = fallback_chip_writen,
324 .delay = serprog_delay,
325 },
hailfinger74d88a72009-08-12 16:17:41 +0000326#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000327
hailfinger90c7d542010-05-31 15:27:27 +0000328#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000329 {
hailfinger90c7d542010-05-31 15:27:27 +0000330 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000331 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000332 .map_flash_region = fallback_map,
333 .unmap_flash_region = fallback_unmap,
334 .chip_readb = noop_chip_readb,
335 .chip_readw = fallback_chip_readw,
336 .chip_readl = fallback_chip_readl,
337 .chip_readn = fallback_chip_readn,
338 .chip_writeb = noop_chip_writeb,
339 .chip_writew = fallback_chip_writew,
340 .chip_writel = fallback_chip_writel,
341 .chip_writen = fallback_chip_writen,
342 .delay = internal_delay,
343 },
344#endif
345
hailfinger90c7d542010-05-31 15:27:27 +0000346#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000347 {
348 .name = "dediprog",
349 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000350 .map_flash_region = fallback_map,
351 .unmap_flash_region = fallback_unmap,
352 .chip_readb = noop_chip_readb,
353 .chip_readw = fallback_chip_readw,
354 .chip_readl = fallback_chip_readl,
355 .chip_readn = fallback_chip_readn,
356 .chip_writeb = noop_chip_writeb,
357 .chip_writew = fallback_chip_writew,
358 .chip_writel = fallback_chip_writel,
359 .chip_writen = fallback_chip_writen,
360 .delay = internal_delay,
361 },
362#endif
363
hailfinger52c4fa02010-07-21 10:26:01 +0000364#if CONFIG_RAYER_SPI == 1
365 {
366 .name = "rayer_spi",
367 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000368 .map_flash_region = fallback_map,
369 .unmap_flash_region = fallback_unmap,
370 .chip_readb = noop_chip_readb,
371 .chip_readw = fallback_chip_readw,
372 .chip_readl = fallback_chip_readl,
373 .chip_readn = fallback_chip_readn,
374 .chip_writeb = noop_chip_writeb,
375 .chip_writew = fallback_chip_writew,
376 .chip_writel = fallback_chip_writel,
377 .chip_writen = fallback_chip_writen,
378 .delay = internal_delay,
379 },
380#endif
381
hailfinger7949b652011-05-08 00:24:18 +0000382#if CONFIG_NICINTEL == 1
383 {
384 .name = "nicintel",
385 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000386 .map_flash_region = fallback_map,
387 .unmap_flash_region = fallback_unmap,
388 .chip_readb = nicintel_chip_readb,
389 .chip_readw = fallback_chip_readw,
390 .chip_readl = fallback_chip_readl,
391 .chip_readn = fallback_chip_readn,
392 .chip_writeb = nicintel_chip_writeb,
393 .chip_writew = fallback_chip_writew,
394 .chip_writel = fallback_chip_writel,
395 .chip_writen = fallback_chip_writen,
396 .delay = internal_delay,
397 },
398#endif
399
uwe6764e922010-09-03 18:21:21 +0000400#if CONFIG_NICINTEL_SPI == 1
401 {
402 .name = "nicintel_spi",
403 .init = nicintel_spi_init,
uwe6764e922010-09-03 18:21:21 +0000404 .map_flash_region = fallback_map,
405 .unmap_flash_region = fallback_unmap,
406 .chip_readb = noop_chip_readb,
407 .chip_readw = fallback_chip_readw,
408 .chip_readl = fallback_chip_readl,
409 .chip_readn = fallback_chip_readn,
410 .chip_writeb = noop_chip_writeb,
411 .chip_writew = fallback_chip_writew,
412 .chip_writel = fallback_chip_writel,
413 .chip_writen = fallback_chip_writen,
414 .delay = internal_delay,
415 },
416#endif
417
hailfingerfb1f31f2010-12-03 14:48:11 +0000418#if CONFIG_OGP_SPI == 1
419 {
420 .name = "ogp_spi",
421 .init = ogp_spi_init,
hailfingerfb1f31f2010-12-03 14:48:11 +0000422 .map_flash_region = fallback_map,
423 .unmap_flash_region = fallback_unmap,
424 .chip_readb = noop_chip_readb,
425 .chip_readw = fallback_chip_readw,
426 .chip_readl = fallback_chip_readl,
427 .chip_readn = fallback_chip_readn,
428 .chip_writeb = noop_chip_writeb,
429 .chip_writew = fallback_chip_writew,
430 .chip_writel = fallback_chip_writel,
431 .chip_writen = fallback_chip_writen,
432 .delay = internal_delay,
433 },
434#endif
435
hailfinger935365d2011-02-04 21:37:59 +0000436#if CONFIG_SATAMV == 1
437 {
438 .name = "satamv",
439 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000440 .map_flash_region = fallback_map,
441 .unmap_flash_region = fallback_unmap,
442 .chip_readb = satamv_chip_readb,
443 .chip_readw = fallback_chip_readw,
444 .chip_readl = fallback_chip_readl,
445 .chip_readn = fallback_chip_readn,
446 .chip_writeb = satamv_chip_writeb,
447 .chip_writew = fallback_chip_writew,
448 .chip_writel = fallback_chip_writel,
449 .chip_writen = fallback_chip_writen,
450 .delay = internal_delay,
451 },
452#endif
453
hailfinger3548a9a2009-08-12 14:34:35 +0000454 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000455};
stepan927d4e22007-04-04 22:45:58 +0000456
David Hendricksbf36f092010-11-02 23:39:29 -0700457#define CHIP_RESTORE_MAXFN 4
458static int chip_restore_fn_count = 0;
459struct chip_restore_func_data {
460 CHIP_RESTORE_CALLBACK;
461 struct flashchip *flash;
462 uint8_t status;
463} static chip_restore_fn[CHIP_RESTORE_MAXFN];
464
David Hendricks668f29d2011-01-27 18:51:45 -0800465
hailfingerf31cbdc2010-11-10 15:25:18 +0000466#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000467static int shutdown_fn_count = 0;
468struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000469 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000470 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000471} static shutdown_fn[SHUTDOWN_MAXFN];
472/* Initialize to 0 to make sure nobody registers a shutdown function before
473 * programmer init.
474 */
475static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000476
stefanct569dbb62011-07-01 00:19:12 +0000477static int check_block_eraser(const struct flashchip *flash, int k, int log);
478
hailfingerdc6f7972010-02-14 01:20:28 +0000479/* Register a function to be executed on programmer shutdown.
480 * The advantage over atexit() is that you can supply a void pointer which will
481 * be used as parameter to the registered function upon programmer shutdown.
482 * This pointer can point to arbitrary data used by said function, e.g. undo
483 * information for GPIO settings etc. If unneeded, set data=NULL.
484 * Please note that the first (void *data) belongs to the function signature of
485 * the function passed as first parameter.
486 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000487int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000488{
489 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000490 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000491 SHUTDOWN_MAXFN);
492 return 1;
493 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000494 if (!may_register_shutdown) {
495 msg_perr("Tried to register a shutdown function before "
496 "programmer init.\n");
497 return 1;
498 }
hailfingerdc6f7972010-02-14 01:20:28 +0000499 shutdown_fn[shutdown_fn_count].func = function;
500 shutdown_fn[shutdown_fn_count].data = data;
501 shutdown_fn_count++;
502
503 return 0;
504}
505
David Hendricksbf36f092010-11-02 23:39:29 -0700506//int register_chip_restore(int (*function) (void *data), void *data)
507int register_chip_restore(CHIP_RESTORE_CALLBACK,
508 struct flashchip *flash, uint8_t status)
509{
510 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
511 msg_perr("Tried to register more than %i chip restore"
512 " functions.\n", CHIP_RESTORE_MAXFN);
513 return 1;
514 }
515 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
516 chip_restore_fn[chip_restore_fn_count].flash = flash;
517 chip_restore_fn[chip_restore_fn_count].status = status;
518 chip_restore_fn_count++;
519
520 return 0;
521}
522
hailfinger1ff33dc2010-07-03 11:02:10 +0000523int programmer_init(char *param)
uweabe92a52009-05-16 22:36:00 +0000524{
hailfinger1ef766d2010-07-06 09:55:48 +0000525 int ret;
hailfinger1ff33dc2010-07-03 11:02:10 +0000526 /* Initialize all programmer specific data. */
527 /* Default to unlimited decode sizes. */
528 max_rom_decode = (const struct decode_sizes) {
529 .parallel = 0xffffffff,
530 .lpc = 0xffffffff,
531 .fwh = 0xffffffff,
532 .spi = 0xffffffff
533 };
mkarcherd264e9e2011-05-11 17:07:07 +0000534 buses_supported = CHIP_BUSTYPE_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000535 /* Default to top aligned flash at 4 GB. */
536 flashbase = 0;
537 /* Registering shutdown functions is now allowed. */
538 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000539 /* Default to allowing writes. Broken programmers set this to 0. */
540 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000541
542 programmer_param = param;
543 msg_pdbg("Initializing %s programmer\n",
544 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000545 ret = programmer_table[programmer].init();
546 if (programmer_param && strlen(programmer_param)) {
547 msg_perr("Unhandled programmer parameters: %s\n",
548 programmer_param);
549 /* Do not error out here, the init itself was successful. */
550 }
551 return ret;
uweabe92a52009-05-16 22:36:00 +0000552}
553
David Hendricksbf36f092010-11-02 23:39:29 -0700554int chip_restore()
555{
556 int rc = 0;
557
558 while (chip_restore_fn_count > 0) {
559 int i = --chip_restore_fn_count;
560 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
561 chip_restore_fn[i].status);
562 }
563
564 return rc;
565}
566
uweabe92a52009-05-16 22:36:00 +0000567int programmer_shutdown(void)
568{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000569 int ret = 0;
570
hailfinger1ff33dc2010-07-03 11:02:10 +0000571 /* Registering shutdown functions is no longer allowed. */
572 may_register_shutdown = 0;
573 while (shutdown_fn_count > 0) {
574 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000575 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000576 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000577 return ret;
uweabe92a52009-05-16 22:36:00 +0000578}
579
580void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
581 size_t len)
582{
583 return programmer_table[programmer].map_flash_region(descr,
584 phys_addr, len);
585}
586
587void programmer_unmap_flash_region(void *virt_addr, size_t len)
588{
589 programmer_table[programmer].unmap_flash_region(virt_addr, len);
590}
591
592void chip_writeb(uint8_t val, chipaddr addr)
593{
594 programmer_table[programmer].chip_writeb(val, addr);
595}
596
597void chip_writew(uint16_t val, chipaddr addr)
598{
599 programmer_table[programmer].chip_writew(val, addr);
600}
601
602void chip_writel(uint32_t val, chipaddr addr)
603{
604 programmer_table[programmer].chip_writel(val, addr);
605}
606
hailfinger9d987ef2009-06-05 18:32:07 +0000607void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
608{
609 programmer_table[programmer].chip_writen(buf, addr, len);
610}
611
uweabe92a52009-05-16 22:36:00 +0000612uint8_t chip_readb(const chipaddr addr)
613{
614 return programmer_table[programmer].chip_readb(addr);
615}
616
617uint16_t chip_readw(const chipaddr addr)
618{
619 return programmer_table[programmer].chip_readw(addr);
620}
621
622uint32_t chip_readl(const chipaddr addr)
623{
624 return programmer_table[programmer].chip_readl(addr);
625}
626
hailfinger9d987ef2009-06-05 18:32:07 +0000627void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
628{
629 programmer_table[programmer].chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000630}
631
hailfingere5829f62009-06-05 17:48:08 +0000632void programmer_delay(int usecs)
633{
634 programmer_table[programmer].delay(usecs);
635}
636
stuge5ff0e6c2009-01-26 00:39:57 +0000637void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000638{
stepan15e64bc2007-05-24 08:48:10 +0000639 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000640 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000641 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfinger82719632009-05-16 21:22:56 +0000642 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000643}
644
hailfinger0f08b7a2009-06-16 08:55:44 +0000645int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger23060112009-05-08 12:49:03 +0000646{
hailfinger0f08b7a2009-06-16 08:55:44 +0000647 chip_readn(buf, flash->virtual_memory + start, len);
hailfinger23060112009-05-08 12:49:03 +0000648
649 return 0;
650}
651
hailfinger7b414742009-06-13 12:04:03 +0000652int min(int a, int b)
653{
654 return (a < b) ? a : b;
655}
656
hailfinger7af83692009-06-15 17:23:36 +0000657int max(int a, int b)
658{
659 return (a > b) ? a : b;
660}
661
hailfingeraec9c962009-10-31 01:53:09 +0000662int bitcount(unsigned long a)
663{
664 int i = 0;
665 for (; a != 0; a >>= 1)
666 if (a & 1)
667 i++;
668 return i;
669}
670
hailfingerf76cc322010-11-09 22:00:31 +0000671void tolower_string(char *str)
672{
673 for (; *str != '\0'; str++)
674 *str = (char)tolower((unsigned char)*str);
675}
676
hailfingera916b422009-06-01 02:08:58 +0000677char *strcat_realloc(char *dest, const char *src)
678{
679 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000680 if (!dest) {
681 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000682 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000683 }
hailfingera916b422009-06-01 02:08:58 +0000684 strcat(dest, src);
685 return dest;
686}
687
hailfinger6e5a52a2009-11-24 18:27:10 +0000688/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000689 * It will look for needle with a subsequent '=' in haystack, return a copy of
690 * needle and remove everything from the first occurrence of needle to the next
691 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000692 */
stefanct52700282011-06-26 17:38:17 +0000693char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000694{
hailfinger1ef766d2010-07-06 09:55:48 +0000695 char *param_pos, *opt_pos, *rest;
696 char *opt = NULL;
697 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000698 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000699
hailfingerf4aaccc2010-04-28 15:22:14 +0000700 needlelen = strlen(needle);
701 if (!needlelen) {
702 msg_gerr("%s: empty needle! Please report a bug at "
703 "flashrom@flashrom.org\n", __func__);
704 return NULL;
705 }
706 /* No programmer parameters given. */
707 if (*haystack == NULL)
708 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000709 param_pos = strstr(*haystack, needle);
710 do {
711 if (!param_pos)
712 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000713 /* Needle followed by '='? */
714 if (param_pos[needlelen] == '=') {
715
716 /* Beginning of the string? */
717 if (param_pos == *haystack)
718 break;
719 /* After a delimiter? */
720 if (strchr(delim, *(param_pos - 1)))
721 break;
722 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000723 /* Continue searching. */
724 param_pos++;
725 param_pos = strstr(param_pos, needle);
726 } while (1);
hailfinger1ef766d2010-07-06 09:55:48 +0000727
hailfinger6e5a52a2009-11-24 18:27:10 +0000728 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000729 /* Get the string after needle and '='. */
730 opt_pos = param_pos + needlelen + 1;
731 optlen = strcspn(opt_pos, delim);
732 /* Return an empty string if the parameter was empty. */
733 opt = malloc(optlen + 1);
734 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000735 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000736 exit(1);
737 }
hailfinger1ef766d2010-07-06 09:55:48 +0000738 strncpy(opt, opt_pos, optlen);
739 opt[optlen] = '\0';
740 rest = opt_pos + optlen;
741 /* Skip all delimiters after the current parameter. */
742 rest += strspn(rest, delim);
743 memmove(param_pos, rest, strlen(rest) + 1);
744 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000745 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000746
hailfinger1ef766d2010-07-06 09:55:48 +0000747 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000748}
749
stefanct52700282011-06-26 17:38:17 +0000750char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000751{
752 return extract_param(&programmer_param, param_name, ",");
753}
754
stefancte1c5acf2011-07-04 07:27:17 +0000755/* Returns the number of well-defined erasers for a chip. */
756static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000757{
758 unsigned int usable_erasefunctions = 0;
759 int k;
760 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
761 if (!check_block_eraser(flash, k, 0))
762 usable_erasefunctions++;
763 }
764 return usable_erasefunctions;
765}
766
hailfinger7af83692009-06-15 17:23:36 +0000767/* start is an offset to the base address of the flash chip */
768int check_erased_range(struct flashchip *flash, int start, int len)
769{
770 int ret;
771 uint8_t *cmpbuf = malloc(len);
772
773 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000774 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000775 exit(1);
776 }
777 memset(cmpbuf, 0xff, len);
778 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
779 free(cmpbuf);
780 return ret;
781}
782
uwee15beb92010-08-08 17:01:18 +0000783/*
hailfinger7af3d192009-11-25 17:05:52 +0000784 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
785 flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000786 * @start offset to the base address of the flash chip
787 * @len length of the verified area
788 * @message string to print in the "FAILED" message
789 * @return 0 for success, -1 for failure
790 */
stefanct52700282011-06-26 17:38:17 +0000791int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len, const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000792{
hailfinger8cb6ece2010-11-16 17:21:58 +0000793 int i, ret = 0;
794 uint8_t *readbuf = malloc(len);
hailfinger5be6c0f2009-07-23 01:42:56 +0000795 int failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000796
797 if (!len)
798 goto out_free;
799
hailfingerb0f4d122009-06-24 08:20:45 +0000800 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000801 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000802 return 1;
803 }
hailfinger7af83692009-06-15 17:23:36 +0000804 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000805 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000806 exit(1);
807 }
808
809 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000810 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000811 " total_size 0x%x\n", __func__, start, len,
812 flash->total_size * 1024);
813 ret = -1;
814 goto out_free;
815 }
816 if (!message)
817 message = "VERIFY";
818
hailfinger8cb6ece2010-11-16 17:21:58 +0000819 ret = flash->read(flash, readbuf, start, len);
820 if (ret) {
821 msg_gerr("Verification impossible because read failed "
822 "at 0x%x (len 0x%x)\n", start, len);
823 return ret;
824 }
825
826 for (i = 0; i < len; i++) {
827 if (cmpbuf[i] != readbuf[i]) {
828 /* Only print the first failure. */
829 if (!failcount++)
830 msg_cerr("%s FAILED at 0x%08x! "
831 "Expected=0x%02x, Read=0x%02x,",
832 message, start + i, cmpbuf[i],
833 readbuf[i]);
hailfinger7af83692009-06-15 17:23:36 +0000834 }
835 }
hailfinger5be6c0f2009-07-23 01:42:56 +0000836 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000837 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
hailfinger5be6c0f2009-07-23 01:42:56 +0000838 start, start + len - 1, failcount);
839 ret = -1;
840 }
hailfinger7af83692009-06-15 17:23:36 +0000841
842out_free:
843 free(readbuf);
844 return ret;
845}
846
uwee15beb92010-08-08 17:01:18 +0000847/*
hailfingerb247c7a2010-03-08 00:42:32 +0000848 * Check if the buffer @have can be programmed to the content of @want without
849 * erasing. This is only possible if all chunks of size @gran are either kept
850 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000851 *
hailfingerb247c7a2010-03-08 00:42:32 +0000852 * The following write granularities (enum @gran) are known:
853 * - 1 bit. Each bit can be cleared individually.
854 * - 1 byte. A byte can be written once. Further writes to an already written
855 * byte cause the contents to be either undefined or to stay unchanged.
856 * - 128 bytes. If less than 128 bytes are written, the rest will be
857 * erased. Each write to a 128-byte region will trigger an automatic erase
858 * before anything is written. Very uncommon behaviour and unsupported by
859 * this function.
860 * - 256 bytes. If less than 256 bytes are written, the contents of the
861 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000862 * Warning: This function assumes that @have and @want point to naturally
863 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000864 *
865 * @have buffer with current content
866 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000867 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000868 * @gran write granularity (enum, not count)
869 * @return 0 if no erase is needed, 1 otherwise
870 */
871int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
872{
873 int result = 0;
874 int i, j, limit;
875
876 switch (gran) {
877 case write_gran_1bit:
878 for (i = 0; i < len; i++)
879 if ((have[i] & want[i]) != want[i]) {
880 result = 1;
881 break;
882 }
883 break;
884 case write_gran_1byte:
885 for (i = 0; i < len; i++)
886 if ((have[i] != want[i]) && (have[i] != 0xff)) {
887 result = 1;
888 break;
889 }
890 break;
891 case write_gran_256bytes:
892 for (j = 0; j < len / 256; j++) {
893 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000894 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000895 if (!memcmp(have + j * 256, want + j * 256, limit))
896 continue;
897 /* have needs to be in erased state. */
898 for (i = 0; i < limit; i++)
hailfingerb437e282010-11-04 01:04:27 +0000899 if (have[j * 256 + i] != 0xff) {
hailfingerb247c7a2010-03-08 00:42:32 +0000900 result = 1;
901 break;
902 }
903 if (result)
904 break;
905 }
906 break;
hailfingerb437e282010-11-04 01:04:27 +0000907 default:
908 msg_cerr("%s: Unsupported granularity! Please report a bug at "
909 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000910 }
911 return result;
912}
913
hailfingerb437e282010-11-04 01:04:27 +0000914/**
915 * Check if the buffer @have needs to be programmed to get the content of @want.
916 * If yes, return 1 and fill in first_start with the start address of the
917 * write operation and first_len with the length of the first to-be-written
918 * chunk. If not, return 0 and leave first_start and first_len undefined.
919 *
920 * Warning: This function assumes that @have and @want point to naturally
921 * aligned regions.
922 *
923 * @have buffer with current content
924 * @want buffer with desired content
925 * @len length of the checked area
926 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000927 * @first_start offset of the first byte which needs to be written (passed in
928 * value is increased by the offset of the first needed write
929 * relative to have/want or unchanged if no write is needed)
930 * @return length of the first contiguous area which needs to be written
931 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000932 *
933 * FIXME: This function needs a parameter which tells it about coalescing
934 * in relation to the max write length of the programmer and the max write
935 * length of the chip.
936 */
937static int get_next_write(uint8_t *have, uint8_t *want, int len,
hailfinger90fcf9b2010-11-05 14:51:59 +0000938 int *first_start, enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000939{
hailfinger90fcf9b2010-11-05 14:51:59 +0000940 int need_write = 0, rel_start = 0, first_len = 0;
941 int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000942
hailfingerb437e282010-11-04 01:04:27 +0000943 switch (gran) {
944 case write_gran_1bit:
945 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000946 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000947 break;
948 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000949 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000950 break;
951 default:
952 msg_cerr("%s: Unsupported granularity! Please report a bug at "
953 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000954 /* Claim that no write was needed. A write with unknown
955 * granularity is too dangerous to try.
956 */
957 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000958 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000959 for (i = 0; i < len / stride; i++) {
960 limit = min(stride, len - i * stride);
961 /* Are 'have' and 'want' identical? */
962 if (memcmp(have + i * stride, want + i * stride, limit)) {
963 if (!need_write) {
964 /* First location where have and want differ. */
965 need_write = 1;
966 rel_start = i * stride;
967 }
968 } else {
969 if (need_write) {
970 /* First location where have and want
971 * do not differ anymore.
972 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000973 break;
974 }
975 }
976 }
hailfingerffb7f382010-12-06 13:05:44 +0000977 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000978 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000979 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000980 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000981}
982
hailfinger0c515352009-11-23 12:55:31 +0000983/* This function generates various test patterns useful for testing controller
984 * and chip communication as well as chip behaviour.
985 *
986 * If a byte can be written multiple times, each time keeping 0-bits at 0
987 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
988 * is essentially an AND operation. That's also the reason why this function
989 * provides the result of AND between various patterns.
990 *
991 * Below is a list of patterns (and their block length).
992 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
993 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
994 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
995 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
996 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
997 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
998 * Pattern 6 is 00 (1 Byte)
999 * Pattern 7 is ff (1 Byte)
1000 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
1001 * byte block.
1002 *
1003 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
1004 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
1005 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
1006 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
1007 * Pattern 12 is 00 (1 Byte)
1008 * Pattern 13 is ff (1 Byte)
1009 * Patterns 8-13 have no block number.
1010 *
1011 * Patterns 0-3 are created to detect and efficiently diagnose communication
1012 * slips like missed bits or bytes and their repetitive nature gives good visual
1013 * cues to the person inspecting the results. In addition, the following holds:
1014 * AND Pattern 0/1 == Pattern 4
1015 * AND Pattern 2/3 == Pattern 5
1016 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
1017 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
1018 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
1019 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
1020 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
1021 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
1022 * Besides that, they provide for bit testing of the last two bytes of every
1023 * 256 byte block which contains the block number for patterns 0-6.
1024 * Patterns 10-11 are special purpose for detecting subblock aliasing with
1025 * block sizes >256 bytes (some Dataflash chips etc.)
1026 * AND Pattern 8/9 == Pattern 12
1027 * AND Pattern 10/11 == Pattern 12
1028 * Pattern 13 is the completely erased state.
1029 * None of the patterns can detect aliasing at boundaries which are a multiple
1030 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
1031 */
1032int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
1033{
1034 int i;
1035
1036 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +00001037 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +00001038 return 1;
1039 }
1040
1041 switch (variant) {
1042 case 0:
1043 for (i = 0; i < size; i++)
1044 buf[i] = (i & 0xf) << 4 | 0x5;
1045 break;
1046 case 1:
1047 for (i = 0; i < size; i++)
1048 buf[i] = (i & 0xf) << 4 | 0xa;
1049 break;
1050 case 2:
1051 for (i = 0; i < size; i++)
1052 buf[i] = 0x50 | (i & 0xf);
1053 break;
1054 case 3:
1055 for (i = 0; i < size; i++)
1056 buf[i] = 0xa0 | (i & 0xf);
1057 break;
1058 case 4:
1059 for (i = 0; i < size; i++)
1060 buf[i] = (i & 0xf) << 4;
1061 break;
1062 case 5:
1063 for (i = 0; i < size; i++)
1064 buf[i] = i & 0xf;
1065 break;
1066 case 6:
1067 memset(buf, 0x00, size);
1068 break;
1069 case 7:
1070 memset(buf, 0xff, size);
1071 break;
1072 case 8:
1073 for (i = 0; i < size; i++)
1074 buf[i] = i & 0xff;
1075 break;
1076 case 9:
1077 for (i = 0; i < size; i++)
1078 buf[i] = ~(i & 0xff);
1079 break;
1080 case 10:
1081 for (i = 0; i < size % 2; i++) {
1082 buf[i * 2] = (i >> 8) & 0xff;
1083 buf[i * 2 + 1] = i & 0xff;
1084 }
1085 if (size & 0x1)
1086 buf[i * 2] = (i >> 8) & 0xff;
1087 break;
1088 case 11:
1089 for (i = 0; i < size % 2; i++) {
1090 buf[i * 2] = ~((i >> 8) & 0xff);
1091 buf[i * 2 + 1] = ~(i & 0xff);
1092 }
1093 if (size & 0x1)
1094 buf[i * 2] = ~((i >> 8) & 0xff);
1095 break;
1096 case 12:
1097 memset(buf, 0x00, size);
1098 break;
1099 case 13:
1100 memset(buf, 0xff, size);
1101 break;
1102 }
1103
1104 if ((variant >= 0) && (variant <= 7)) {
1105 /* Write block number in the last two bytes of each 256-byte
1106 * block, big endian for easier reading of the hexdump.
1107 * Note that this wraps around for chips larger than 2^24 bytes
1108 * (16 MB).
1109 */
1110 for (i = 0; i < size / 256; i++) {
1111 buf[i * 256 + 254] = (i >> 8) & 0xff;
1112 buf[i * 256 + 255] = i & 0xff;
1113 }
1114 }
1115
1116 return 0;
1117}
1118
hailfingeraec9c962009-10-31 01:53:09 +00001119int check_max_decode(enum chipbustype buses, uint32_t size)
1120{
1121 int limitexceeded = 0;
1122 if ((buses & CHIP_BUSTYPE_PARALLEL) &&
1123 (max_rom_decode.parallel < size)) {
1124 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001125 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001126 "size %u kB of chipset/board/programmer "
1127 "for %s interface, "
1128 "probe/read/erase/write may fail. ", size / 1024,
1129 max_rom_decode.parallel / 1024, "Parallel");
1130 }
1131 if ((buses & CHIP_BUSTYPE_LPC) && (max_rom_decode.lpc < size)) {
1132 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001133 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001134 "size %u kB of chipset/board/programmer "
1135 "for %s interface, "
1136 "probe/read/erase/write may fail. ", size / 1024,
1137 max_rom_decode.lpc / 1024, "LPC");
1138 }
1139 if ((buses & CHIP_BUSTYPE_FWH) && (max_rom_decode.fwh < size)) {
1140 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001141 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001142 "size %u kB of chipset/board/programmer "
1143 "for %s interface, "
1144 "probe/read/erase/write may fail. ", size / 1024,
1145 max_rom_decode.fwh / 1024, "FWH");
1146 }
1147 if ((buses & CHIP_BUSTYPE_SPI) && (max_rom_decode.spi < size)) {
1148 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001149 msg_pdbg("Chip size %u kB is bigger than supported "
hailfingeraec9c962009-10-31 01:53:09 +00001150 "size %u kB of chipset/board/programmer "
1151 "for %s interface, "
1152 "probe/read/erase/write may fail. ", size / 1024,
1153 max_rom_decode.spi / 1024, "SPI");
1154 }
1155 if (!limitexceeded)
1156 return 0;
1157 /* Sometimes chip and programmer have more than one bus in common,
1158 * and the limit is not exceeded on all buses. Tell the user.
1159 */
1160 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001161 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001162 msg_pdbg("There is at least one common chip/programmer "
hailfingeraec9c962009-10-31 01:53:09 +00001163 "interface which can support a chip of this size. "
1164 "You can try --force at your own risk.\n");
1165 return 1;
1166}
1167
hailfinger48ed3e22011-05-04 00:39:50 +00001168int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001169{
hailfinger48ed3e22011-05-04 00:39:50 +00001170 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001171 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001172 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001173 uint32_t size;
1174 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001175 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001176
hailfinger48ed3e22011-05-04 00:39:50 +00001177 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001178 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001179 continue;
hailfingeraec9c962009-10-31 01:53:09 +00001180 buses_common = buses_supported & flash->bustype;
1181 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001182 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1183 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +00001184 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +00001185 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001186 free(tmp);
1187 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001188 msg_gspew("and chip bus type %s are incompatible.",
1189 tmp);
hailfingera916b422009-06-01 02:08:58 +00001190 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001191 msg_gspew("\n");
1192 continue;
1193 }
1194 msg_gdbg("Probing for %s %s, %d kB: ",
1195 flash->vendor, flash->name, flash->total_size);
1196 if (!flash->probe && !force) {
1197 msg_gdbg("failed! flashrom has no probe function for "
1198 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001199 continue;
1200 }
stepan782fb172007-04-06 11:58:03 +00001201
ollie5672ac62004-03-17 22:22:08 +00001202 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001203 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001204
hailfinger48ed3e22011-05-04 00:39:50 +00001205 /* Start filling in the dynamic data. */
1206 *fill_flash = *flash;
1207
hailfinger72d3b982009-05-09 07:27:23 +00001208 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001209 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001210
stugec1e55fe2008-07-02 17:15:47 +00001211 if (force)
1212 break;
stepanc98b80b2006-03-16 16:57:41 +00001213
hailfinger48ed3e22011-05-04 00:39:50 +00001214 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001215 goto notfound;
1216
hailfinger48ed3e22011-05-04 00:39:50 +00001217 /* If this is the first chip found, accept it.
1218 * If this is not the first chip found, accept it only if it is
1219 * a non-generic match.
1220 * We could either make chipcount global or provide it as
1221 * parameter, or we assume that startchip==0 means this call to
1222 * probe_flash() is the first one and thus no chip has been
1223 * found before.
1224 */
1225 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001226 break;
1227
stuge56300c32008-09-03 23:10:05 +00001228notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001229 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001230 }
uwebe4477b2007-08-23 16:08:21 +00001231
stugec1e55fe2008-07-02 17:15:47 +00001232 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001233 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001234
hailfingere11396b2011-03-08 00:09:11 +00001235#if CONFIG_INTERNAL == 1
1236 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001237 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001238 else
1239#endif
stepan3e7aeae2011-01-19 06:21:54 +00001240 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001241
stefanct588b6d22011-06-26 20:45:35 +00001242 tmp = flashbuses_to_text(flash->bustype);
David Hendricks91040832011-07-08 20:01:09 -07001243 msg_cdbg("%s chip \"%s %s\" (%d kB, %s) %s.\n",
stefanct588b6d22011-06-26 20:45:35 +00001244 force ? "Assuming" : "Found", fill_flash->vendor,
1245 fill_flash->name, fill_flash->total_size, tmp, location);
1246 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001247
hailfinger0f4c3952010-12-02 21:59:42 +00001248 /* Flash registers will not be mapped if the chip was forced. Lock info
1249 * may be stored in registers, so avoid lock info printing.
1250 */
1251 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001252 if (fill_flash->printlock)
1253 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001254
hailfinger48ed3e22011-05-04 00:39:50 +00001255 /* Return position of matching chip. */
1256 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001257}
1258
stepan193c9c22005-12-18 16:41:10 +00001259int verify_flash(struct flashchip *flash, uint8_t *buf)
rminnich8d3ff912003-10-25 17:01:29 +00001260{
hailfingerb0f4d122009-06-24 08:20:45 +00001261 int ret;
ollie5b621572004-03-20 16:46:10 +00001262 int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001263
snelsone42c3802010-05-07 20:09:04 +00001264 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001265
hailfingerb0f4d122009-06-24 08:20:45 +00001266 ret = verify_range(flash, buf, 0, total_size, NULL);
uwef6641642007-05-09 10:17:44 +00001267
hailfingerb0f4d122009-06-24 08:20:45 +00001268 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001269 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001270
hailfingerb0f4d122009-06-24 08:20:45 +00001271 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001272}
1273
stefanct52700282011-06-26 17:38:17 +00001274int read_buf_from_file(unsigned char *buf, unsigned long size, const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001275{
1276 unsigned long numbytes;
1277 FILE *image;
1278 struct stat image_stat;
1279
1280 if ((image = fopen(filename, "rb")) == NULL) {
1281 perror(filename);
1282 return 1;
1283 }
1284 if (fstat(fileno(image), &image_stat) != 0) {
1285 perror(filename);
1286 fclose(image);
1287 return 1;
1288 }
1289 if (image_stat.st_size != size) {
1290 msg_gerr("Error: Image size doesn't match\n");
1291 fclose(image);
1292 return 1;
1293 }
1294 numbytes = fread(buf, 1, size, image);
1295 if (fclose(image)) {
1296 perror(filename);
1297 return 1;
1298 }
1299 if (numbytes != size) {
1300 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1301 "wanted %ld!\n", numbytes, size);
1302 return 1;
1303 }
1304 return 0;
1305}
1306
stefanct52700282011-06-26 17:38:17 +00001307int write_buf_to_file(unsigned char *buf, unsigned long size, const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001308{
1309 unsigned long numbytes;
1310 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001311
1312 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001313 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001314 return 1;
1315 }
oxygenebf70d352010-01-25 22:55:33 +00001316 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001317 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001318 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001319 }
hailfingerd219a232009-01-28 00:27:54 +00001320
hailfingerd219a232009-01-28 00:27:54 +00001321 numbytes = fwrite(buf, 1, size, image);
1322 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001323 if (numbytes != size) {
1324 msg_gerr("File %s could not be written completely.\n",
1325 filename);
hailfingerd219a232009-01-28 00:27:54 +00001326 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001327 }
hailfingerd219a232009-01-28 00:27:54 +00001328 return 0;
1329}
1330
stefanct52700282011-06-26 17:38:17 +00001331int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001332{
1333 unsigned long size = flash->total_size * 1024;
1334 unsigned char *buf = calloc(size, sizeof(char));
1335 int ret = 0;
1336
1337 msg_cinfo("Reading flash... ");
1338 if (!buf) {
1339 msg_gerr("Memory allocation failed!\n");
1340 msg_cinfo("FAILED.\n");
1341 return 1;
1342 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001343
1344 /* To support partial read, fill buffer to all 0xFF at beginning to make
1345 * debug easier. */
1346 memset(buf, 0xFF, size);
1347
hailfinger42a850a2010-07-13 23:56:13 +00001348 if (!flash->read) {
1349 msg_cerr("No read function available for this flash chip.\n");
1350 ret = 1;
1351 goto out_free;
1352 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001353
1354 /* First try to handle partial read case, rather than read the whole
1355 * flash, which is slow. */
1356 ret = handle_partial_read(flash, buf, flash->read);
1357 if (ret < 0) {
1358 msg_cerr("Partial read operation failed!\n");
1359 ret = 1;
1360 goto out_free;
1361 } else if (ret > 0) {
1362 /* Partial read has been handled, pass the whole flash read. */
1363 } else if (flash->read(flash, buf, 0, size)) {
hailfinger42a850a2010-07-13 23:56:13 +00001364 msg_cerr("Read operation failed!\n");
1365 ret = 1;
1366 goto out_free;
1367 }
1368
stefanct9e6b98a2011-05-28 02:37:14 +00001369 ret = write_buf_to_file(buf, size, filename);
hailfinger42a850a2010-07-13 23:56:13 +00001370out_free:
1371 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001372 if (ret)
1373 msg_cerr("FAILED.");
1374 else
1375 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001376 return ret;
1377}
1378
hailfingerb437e282010-11-04 01:04:27 +00001379/* This function shares a lot of its structure with erase_and_write_flash() and
1380 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001381 * Even if an error is found, the function will keep going and check the rest.
1382 */
hailfinger48ed3e22011-05-04 00:39:50 +00001383static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001384{
hailfinger9fed35d2010-01-19 06:42:46 +00001385 int i, j, k;
1386 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001387
1388 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1389 unsigned int done = 0;
1390 struct block_eraser eraser = flash->block_erasers[k];
1391
1392 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1393 /* Blocks with zero size are bugs in flashchips.c. */
1394 if (eraser.eraseblocks[i].count &&
1395 !eraser.eraseblocks[i].size) {
1396 msg_gerr("ERROR: Flash chip %s erase function "
1397 "%i region %i has size 0. Please report"
1398 " a bug at flashrom@flashrom.org\n",
1399 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001400 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001401 }
1402 /* Blocks with zero count are bugs in flashchips.c. */
1403 if (!eraser.eraseblocks[i].count &&
1404 eraser.eraseblocks[i].size) {
1405 msg_gerr("ERROR: Flash chip %s erase function "
1406 "%i region %i has count 0. Please report"
1407 " a bug at flashrom@flashrom.org\n",
1408 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001409 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001410 }
1411 done += eraser.eraseblocks[i].count *
1412 eraser.eraseblocks[i].size;
1413 }
hailfinger9fed35d2010-01-19 06:42:46 +00001414 /* Empty eraseblock definition with erase function. */
1415 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001416 msg_gspew("Strange: Empty eraseblock definition with "
hailfinger9fed35d2010-01-19 06:42:46 +00001417 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001418 if (!done)
1419 continue;
1420 if (done != flash->total_size * 1024) {
1421 msg_gerr("ERROR: Flash chip %s erase function %i "
1422 "region walking resulted in 0x%06x bytes total,"
1423 " expected 0x%06x bytes. Please report a bug at"
1424 " flashrom@flashrom.org\n", flash->name, k,
1425 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001426 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001427 }
hailfinger9fed35d2010-01-19 06:42:46 +00001428 if (!eraser.block_erase)
1429 continue;
1430 /* Check if there are identical erase functions for different
1431 * layouts. That would imply "magic" erase functions. The
1432 * easiest way to check this is with function pointers.
1433 */
uwef6f94d42010-03-13 17:28:29 +00001434 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001435 if (eraser.block_erase ==
1436 flash->block_erasers[j].block_erase) {
1437 msg_gerr("ERROR: Flash chip %s erase function "
1438 "%i and %i are identical. Please report"
1439 " a bug at flashrom@flashrom.org\n",
1440 flash->name, k, j);
1441 ret = 1;
1442 }
uwef6f94d42010-03-13 17:28:29 +00001443 }
hailfinger45177872010-01-18 08:14:43 +00001444 }
hailfinger9fed35d2010-01-19 06:42:46 +00001445 return ret;
hailfinger45177872010-01-18 08:14:43 +00001446}
1447
hailfingerb437e282010-11-04 01:04:27 +00001448static int erase_and_write_block_helper(struct flashchip *flash,
1449 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001450 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001451 uint8_t *newcontents,
1452 int (*erasefn) (struct flashchip *flash,
1453 unsigned int addr,
1454 unsigned int len))
1455{
1456 int starthere = 0;
1457 int lenhere = 0;
1458 int ret = 0;
1459 int skip = 1;
1460 int writecount = 0;
1461 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1462
hailfinger90fcf9b2010-11-05 14:51:59 +00001463 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001464 * need to be adjusted here to keep the impression of proper abstraction
1465 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001466 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001467 newcontents += start;
1468 msg_cdbg(":");
1469 /* FIXME: Assume 256 byte granularity for now to play it safe. */
hailfinger90fcf9b2010-11-05 14:51:59 +00001470 if (need_erase(curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001471 msg_cdbg("E");
1472 ret = erasefn(flash, start, len);
1473 if (ret)
1474 return ret;
hailfingerac8e3182011-06-26 17:04:16 +00001475 if (check_erased_range(flash, start, len)) {
1476 msg_cerr("ERASE FAILED!\n");
1477 return -1;
1478 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001479 /* Erase was successful. Adjust curcontents. */
1480 memset(curcontents, 0xff, len);
hailfingerb437e282010-11-04 01:04:27 +00001481 skip = 0;
1482 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001483 /* get_next_write() sets starthere to a new value after the call. */
1484 while ((lenhere = get_next_write(curcontents + starthere,
1485 newcontents + starthere,
1486 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001487 if (!writecount++)
1488 msg_cdbg("W");
1489 /* Needs the partial write function signature. */
1490 ret = flash->write(flash, newcontents + starthere,
1491 start + starthere, lenhere);
1492 if (ret)
1493 return ret;
1494 starthere += lenhere;
1495 skip = 0;
1496 }
1497 if (skip)
1498 msg_cdbg("S");
1499 return ret;
1500}
1501
hailfinger83541b32010-07-13 00:42:00 +00001502static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1503 int (*do_something) (struct flashchip *flash,
1504 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001505 unsigned int len,
1506 uint8_t *param1,
1507 uint8_t *param2,
1508 int (*erasefn) (
1509 struct flashchip *flash,
1510 unsigned int addr,
1511 unsigned int len)),
1512 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001513{
1514 int i, j;
1515 unsigned int start = 0;
1516 unsigned int len;
1517 struct block_eraser eraser = flash->block_erasers[erasefunction];
1518 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1519 /* count==0 for all automatically initialized array
1520 * members so the loop below won't be executed for them.
1521 */
1522 len = eraser.eraseblocks[i].size;
1523 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001524 /* Print this for every block except the first one. */
1525 if (i || j)
1526 msg_cdbg(", ");
1527 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001528 start + len - 1);
hailfingerb437e282010-11-04 01:04:27 +00001529 if (do_something(flash, start, len, param1, param2,
1530 eraser.block_erase)) {
1531 msg_cdbg("\n");
hailfinger2b8c9382010-07-13 00:37:19 +00001532 return 1;
hailfingerb437e282010-11-04 01:04:27 +00001533 }
hailfinger2b8c9382010-07-13 00:37:19 +00001534 start += len;
1535 }
1536 }
hailfingerb437e282010-11-04 01:04:27 +00001537 msg_cdbg("\n");
hailfinger2b8c9382010-07-13 00:37:19 +00001538 return 0;
1539}
1540
stefanct569dbb62011-07-01 00:19:12 +00001541static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001542{
1543 struct block_eraser eraser = flash->block_erasers[k];
1544
1545 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1546 if (log)
1547 msg_cdbg("not defined. ");
1548 return 1;
1549 }
1550 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1551 if (log)
1552 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001553 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001554 return 1;
1555 }
1556 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1557 if (log)
1558 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001559 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001560 return 1;
1561 }
1562 return 0;
1563}
1564
hailfingerb437e282010-11-04 01:04:27 +00001565int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents, uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001566{
hailfingercf848f12010-12-05 15:14:44 +00001567 int k, ret = 0;
hailfingerb437e282010-11-04 01:04:27 +00001568 uint8_t *curcontents;
1569 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001570 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001571
hailfingercf848f12010-12-05 15:14:44 +00001572 msg_cinfo("Erasing and writing flash chip... ");
hailfingerb437e282010-11-04 01:04:27 +00001573 curcontents = (uint8_t *) malloc(size);
1574 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1575 memcpy(curcontents, oldcontents, size);
1576
hailfinger7df21362009-09-05 02:30:58 +00001577 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
snelsone42c3802010-05-07 20:09:04 +00001578 msg_cdbg("Looking at blockwise erase function %i... ", k);
hailfingercf848f12010-12-05 15:14:44 +00001579 if (check_block_eraser(flash, k, 1) && usable_erasefunctions) {
1580 msg_cdbg("Looking for another erase function.\n");
hailfinger7df21362009-09-05 02:30:58 +00001581 continue;
1582 }
hailfingercf848f12010-12-05 15:14:44 +00001583 usable_erasefunctions--;
snelsone42c3802010-05-07 20:09:04 +00001584 msg_cdbg("trying... ");
hailfingerb437e282010-11-04 01:04:27 +00001585 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper, curcontents, newcontents);
snelsone42c3802010-05-07 20:09:04 +00001586 msg_cdbg("\n");
hailfinger7df21362009-09-05 02:30:58 +00001587 /* If everything is OK, don't try another erase function. */
1588 if (!ret)
1589 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001590 /* Write/erase failed, so try to find out what the current chip
1591 * contents are. If no usable erase functions remain, we could
1592 * abort the loop instead of continuing, the effect is the same.
1593 * The only difference is whether the reason for other unusable
1594 * functions is printed or not. If in doubt, verbosity wins.
hailfingerb437e282010-11-04 01:04:27 +00001595 */
hailfingercf848f12010-12-05 15:14:44 +00001596 if (!usable_erasefunctions)
1597 continue;
hailfinger6237f5e2010-12-02 02:41:55 +00001598 if (flash->read(flash, curcontents, 0, size)) {
1599 /* Now we are truly screwed. Read failed as well. */
1600 msg_cerr("Can't read anymore!\n");
1601 /* We have no idea about the flash chip contents, so
1602 * retrying with another erase function is pointless.
1603 */
1604 break;
1605 }
hailfinger7df21362009-09-05 02:30:58 +00001606 }
hailfingerb437e282010-11-04 01:04:27 +00001607 /* Free the scratchpad. */
1608 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001609
hailfinger7df21362009-09-05 02:30:58 +00001610 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001611 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001612 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001613 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001614 }
1615 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001616}
1617
hailfinger4c47e9d2010-10-19 22:06:20 +00001618void nonfatal_help_message(void)
1619{
1620 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1621 "This means we have to add special support for your board, "
1622 "programmer or flash chip.\n"
1623 "Please report this on IRC at irc.freenode.net (channel "
1624 "#flashrom) or\n"
1625 "mail flashrom@flashrom.org!\n"
1626 "-------------------------------------------------------------"
1627 "------------------\n"
1628 "You may now reboot or simply leave the machine running.\n");
1629}
1630
uweb34ec9f2009-10-01 18:40:02 +00001631void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001632{
snelsone42c3802010-05-07 20:09:04 +00001633 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001634 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001635 "mail flashrom@flashrom.org with FAILED: your board name in "
1636 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001637 "-------------------------------------------------------------"
1638 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001639 "DO NOT REBOOT OR POWEROFF!\n");
1640}
1641
hailfingerc77acb52009-12-24 02:15:55 +00001642/* The way to go if you want a delimited list of programmers*/
stefanct52700282011-06-26 17:38:17 +00001643void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001644{
1645 enum programmer p;
1646 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001647 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001648 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001649 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001650 }
snelsone42c3802010-05-07 20:09:04 +00001651 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001652}
1653
hailfingerf79d1712010-10-06 23:48:34 +00001654void list_programmers_linebreak(int startcol, int cols, int paren)
1655{
1656 const char *pname;
1657 int pnamelen;
1658 int remaining = 0;
1659 int firstline = 1;
1660 enum programmer p;
1661 int i;
1662
1663 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1664 pname = programmer_table[p].name;
1665 pnamelen = strlen(pname);
1666 if (remaining - pnamelen - 2 < 0) {
1667 if (firstline)
1668 firstline = 0;
1669 else
1670 printf("\n");
1671 for (i = 0; i < startcol; i++)
1672 printf(" ");
1673 remaining = cols - startcol;
1674 } else {
1675 printf(" ");
1676 remaining--;
1677 }
1678 if (paren && (p == 0)) {
1679 printf("(");
1680 remaining--;
1681 }
1682 printf("%s", pname);
1683 remaining -= pnamelen;
1684 if (p < PROGRAMMER_INVALID - 1) {
1685 printf(",");
1686 remaining--;
1687 } else {
1688 if (paren)
1689 printf(")");
1690 printf("\n");
1691 }
1692 }
1693}
1694
hailfinger3b471632010-03-27 16:36:40 +00001695void print_sysinfo(void)
1696{
David Hendricksc6c9f822010-11-03 15:07:01 -07001697 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001698#if HAVE_UTSNAME == 1
1699 struct utsname osinfo;
1700 uname(&osinfo);
1701
David Hendricksc6c9f822010-11-03 15:07:01 -07001702 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001703 osinfo.machine);
1704#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001705 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001706#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001707 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001708#if NEED_PCI == 1
1709#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001710 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001711#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001712 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001713#endif
1714#endif
1715#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001716 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001717#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001718 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001719#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001720 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001721#endif
hailfinger3b471632010-03-27 16:36:40 +00001722#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001723 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001724#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001725 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001726#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001727 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001728#endif
1729#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001730 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001731#endif
1732#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001733 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001734#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001735 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001736#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001737 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001738}
1739
uwefdeca092008-01-21 15:24:22 +00001740void print_version(void)
1741{
David Hendricksc6c9f822010-11-03 15:07:01 -07001742 /* send to stderr for chromium os */
1743 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001744 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001745}
1746
hailfinger74819ad2010-05-15 15:04:37 +00001747void print_banner(void)
1748{
1749 msg_ginfo("flashrom is free software, get the source code at "
1750 "http://www.flashrom.org\n");
1751 msg_ginfo("\n");
1752}
1753
hailfingerc77acb52009-12-24 02:15:55 +00001754int selfcheck(void)
1755{
hailfinger45177872010-01-18 08:14:43 +00001756 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001757 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001758
1759 /* Safety check. Instead of aborting after the first error, check
1760 * if more errors exist.
1761 */
hailfingerc77acb52009-12-24 02:15:55 +00001762 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001763 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001764 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001765 }
stefanct6d836ba2011-05-26 01:35:19 +00001766 /* It would be favorable if we could also check for correct terminaion
1767 * of the follwing arrays, but we don't know their size in here...
1768 * For 'flashchips' we check the first element to be non-null. In the
1769 * other cases there exist use cases where the first element can be
1770 * null. */
1771 if (flashchips == NULL || flashchips[0].vendor == NULL) {
1772 msg_gerr("Flashchips table miscompilation!\n");
1773 ret = 1;
1774 }
hailfinger45177872010-01-18 08:14:43 +00001775 for (flash = flashchips; flash && flash->name; flash++)
1776 if (selfcheck_eraseblocks(flash))
1777 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001778
1779#if CONFIG_INTERNAL == 1
1780 if (chipset_enables == NULL) {
1781 msg_gerr("Chipset enables table does not exist!\n");
1782 ret = 1;
1783 }
1784 if (board_pciid_enables == NULL) {
1785 msg_gerr("Board enables table does not exist!\n");
1786 ret = 1;
1787 }
1788 if (boards_known == NULL) {
1789 msg_gerr("Known boards table does not exist!\n");
1790 ret = 1;
1791 }
1792 if (laptops_known == NULL) {
1793 msg_gerr("Known laptops table does not exist!\n");
1794 ret = 1;
1795 }
1796#endif // CONFIG_INTERNAL == 1
hailfinger45177872010-01-18 08:14:43 +00001797 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001798}
1799
hailfinger48ed3e22011-05-04 00:39:50 +00001800void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001801{
1802 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001803 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001804 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001805 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001806 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001807 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001808 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001809 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001810 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001811 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001812 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001813 msg_cdbg(" WRITE");
1814 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001815 }
1816 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1817 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1818 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1819 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001820 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001821 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001822 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001823 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001824 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001825 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001826 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001827 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001828 msg_cdbg(" WRITE");
1829 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001830 }
hailfinger92cd8e32010-01-07 03:24:05 +00001831 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001832 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001833 "in the latest development\n"
1834 "version of flashrom. If you are running the latest "
1835 "development version,\n"
1836 "please email a report to flashrom@flashrom.org if "
1837 "any of the above operations\n"
1838 "work correctly for you with this flash part. Please "
1839 "include the flashrom\n"
1840 "output with the additional -V option for all "
1841 "operations you tested (-V, -Vr,\n"
1842 "-Vw, -VE), and mention which mainboard or "
1843 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001844 "Please mention your board in the subject line. "
1845 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001846 }
1847}
1848
ollie5b621572004-03-20 16:46:10 +00001849int main(int argc, char *argv[])
rminnich8d3ff912003-10-25 17:01:29 +00001850{
David Hendricksf7924d12010-06-10 21:26:44 -07001851 /* FIXME: this should eventually be a build option controlled
1852 via a USE flag */
1853// return cli_classic(argc, argv);
1854 return cli_mfg(argc, argv);
hailfingerc77acb52009-12-24 02:15:55 +00001855}
1856
hailfinger771fc182010-10-15 00:01:14 +00001857/* FIXME: This function signature needs to be improved once doit() has a better
1858 * function signature.
1859 */
stefanct02116582011-05-18 01:30:56 +00001860int chip_safety_check(struct flashchip *flash, int force, int read_it, int write_it, int erase_it, int verify_it)
hailfinger771fc182010-10-15 00:01:14 +00001861{
1862 if (!programmer_may_write && (write_it || erase_it)) {
1863 msg_perr("Write/erase is not working yet on your programmer in "
1864 "its current configuration.\n");
1865 /* --force is the wrong approach, but it's the best we can do
1866 * until the generic programmer parameter parser is merged.
1867 */
1868 if (!force)
1869 return 1;
1870 msg_cerr("Continuing anyway.\n");
1871 }
1872
1873 if (read_it || erase_it || write_it || verify_it) {
1874 /* Everything needs read. */
1875 if (flash->tested & TEST_BAD_READ) {
1876 msg_cerr("Read is not working on this chip. ");
1877 if (!force)
1878 return 1;
1879 msg_cerr("Continuing anyway.\n");
1880 }
1881 if (!flash->read) {
1882 msg_cerr("flashrom has no read function for this "
1883 "flash chip.\n");
1884 return 1;
1885 }
1886 }
1887 if (erase_it || write_it) {
1888 /* Write needs erase. */
1889 if (flash->tested & TEST_BAD_ERASE) {
1890 msg_cerr("Erase is not working on this chip. ");
1891 if (!force)
1892 return 1;
1893 msg_cerr("Continuing anyway.\n");
1894 }
stefancte1c5acf2011-07-04 07:27:17 +00001895 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001896 msg_cerr("flashrom has no erase function for this "
1897 "flash chip.\n");
1898 return 1;
1899 }
hailfinger771fc182010-10-15 00:01:14 +00001900 }
1901 if (write_it) {
1902 if (flash->tested & TEST_BAD_WRITE) {
1903 msg_cerr("Write is not working on this chip. ");
1904 if (!force)
1905 return 1;
1906 msg_cerr("Continuing anyway.\n");
1907 }
1908 if (!flash->write) {
1909 msg_cerr("flashrom has no write function for this "
1910 "flash chip.\n");
1911 return 1;
1912 }
1913 }
1914 return 0;
1915}
1916
hailfingerc77acb52009-12-24 02:15:55 +00001917/* This function signature is horrible. We need to design a better interface,
1918 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001919 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001920 */
stefanct52700282011-06-26 17:38:17 +00001921int doit(struct flashchip *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it)
hailfingerc77acb52009-12-24 02:15:55 +00001922{
hailfinger4c47e9d2010-10-19 22:06:20 +00001923 uint8_t *oldcontents;
1924 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001925 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001926 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001927
stefanct02116582011-05-18 01:30:56 +00001928 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001929 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001930 ret = 1;
1931 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001932 }
1933
hailfinger771fc182010-10-15 00:01:14 +00001934 /* Given the existence of read locks, we want to unlock for read,
1935 * erase and write.
1936 */
1937 if (flash->unlock)
1938 flash->unlock(flash);
1939
David Hendricks6d62d752011-03-07 21:20:22 -08001940 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001941 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001942 ret = 1;
1943 goto out_nofree;
1944 }
1945
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001946 /* mark entries included using -i argument as "included" if they are
1947 found in the master rom_entries list */
1948 if (process_include_args() < 0) {
1949 ret = 1;
1950 goto out_nofree;
1951 }
1952
hailfinger771fc182010-10-15 00:01:14 +00001953 if (read_it) {
1954 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001955 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001956 }
hailfingerb437e282010-11-04 01:04:27 +00001957
1958 oldcontents = (uint8_t *) malloc(size);
1959 /* Assume worst case: All bits are 0. */
1960 memset(oldcontents, 0x00, size);
1961 newcontents = (uint8_t *) malloc(size);
1962 /* Assume best case: All bits should be 1. */
1963 memset(newcontents, 0xff, size);
1964 /* Side effect of the assumptions above: Default write action is erase
1965 * because newcontents looks like a completely erased chip, and
1966 * oldcontents being completely 0x00 means we have to erase everything
1967 * before we can write.
1968 */
1969
ollie0eb62d62004-12-08 20:10:01 +00001970 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001971 /* FIXME: Do we really want the scary warning if erase failed?
1972 * After all, after erase the chip is either blank or partially
1973 * blank or it has the old contents. A blank chip won't boot,
1974 * so if the user wanted erase and reboots afterwards, the user
1975 * knows very well that booting won't work.
1976 */
hailfingerb437e282010-11-04 01:04:27 +00001977 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001978 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001979 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001980 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001981 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001982 }
hailfinger771fc182010-10-15 00:01:14 +00001983
hailfingerd217d122010-10-08 18:52:29 +00001984 if (write_it || verify_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001985 if (read_buf_from_file(newcontents, size, filename)) {
hailfinger90fcf9b2010-11-05 14:51:59 +00001986 ret = 1;
1987 goto out;
stepan1da96c02006-11-21 23:48:51 +00001988 }
1989
hailfinger90c7d542010-05-31 15:27:27 +00001990#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001991 if (programmer == PROGRAMMER_INTERNAL)
1992 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001993#endif
ollie5672ac62004-03-17 22:22:08 +00001994 }
1995
hailfinger4c47e9d2010-10-19 22:06:20 +00001996 /* Read the whole chip to be able to check whether regions need to be
1997 * erased and to give better diagnostics in case write fails.
1998 * The alternative would be to read only the regions which are to be
1999 * preserved, but in that case we might perform unneeded erase which
2000 * takes time as well.
2001 */
stefanct25230382011-06-26 17:47:40 +00002002 msg_cdbg("Reading old flash chip contents... ");
hailfinger4c47e9d2010-10-19 22:06:20 +00002003 if (flash->read(flash, oldcontents, 0, size)) {
hailfinger90fcf9b2010-11-05 14:51:59 +00002004 ret = 1;
stefanct25230382011-06-26 17:47:40 +00002005 msg_cdbg("FAILED.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00002006 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002007 }
stefanct25230382011-06-26 17:47:40 +00002008 msg_cdbg("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00002009
ollie6a600992005-11-26 21:55:36 +00002010 // This should be moved into each flash part's code to do it
2011 // cleanly. This does the job.
hailfinger4c47e9d2010-10-19 22:06:20 +00002012 handle_romentries(flash, oldcontents, newcontents);
uwef6641642007-05-09 10:17:44 +00002013
ollie6a600992005-11-26 21:55:36 +00002014 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00002015
stuge8ce3a3c2008-04-28 14:47:30 +00002016 if (write_it) {
hailfingerb437e282010-11-04 01:04:27 +00002017 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2018 msg_cerr("Uh oh. Erase/write failed. Checking if "
2019 "anything changed.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00002020 if (!flash->read(flash, newcontents, 0, size)) {
2021 if (!memcmp(oldcontents, newcontents, size)) {
2022 msg_cinfo("Good. It seems nothing was "
2023 "changed.\n");
2024 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002025 ret = 1;
2026 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002027 }
2028 }
hailfingerd217d122010-10-08 18:52:29 +00002029 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002030 ret = 1;
2031 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002032 }
stuge8ce3a3c2008-04-28 14:47:30 +00002033 }
ollie6a600992005-11-26 21:55:36 +00002034
hailfinger0459e1c2009-08-19 13:55:34 +00002035 if (verify_it) {
2036 /* Work around chips which need some time to calm down. */
2037 if (write_it)
2038 programmer_delay(1000*1000);
hailfinger4c47e9d2010-10-19 22:06:20 +00002039 ret = verify_flash(flash, newcontents);
hailfingera50d60e2009-11-17 09:57:34 +00002040 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00002041 * might have an emergency situation.
2042 */
2043 if (ret && write_it)
2044 emergency_help_message();
2045 }
ollie6a600992005-11-26 21:55:36 +00002046
hailfinger90fcf9b2010-11-05 14:51:59 +00002047out:
2048 free(oldcontents);
2049 free(newcontents);
2050out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002051 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002052 /*
2053 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2054 * tree. This is because some operations, such as write protection,
2055 * requires programmer_shutdown() but does not call doit().
2056 */
2057// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002058 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002059}