blob: b2f78f77fb66966b8f2736a387bb8d2c444f9a02 [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
uwe7df6dda2011-09-03 18:37:52 +0000107#if CONFIG_LINUX_SPI == 1
108 PROGRAMMER_LINUX_SPI
109#endif
hailfingerf95c8f62010-01-10 13:28:48 +0000110;
hailfinger80422e22009-12-13 22:28:00 +0000111#endif
112
hailfingerddeb4ac2010-07-08 10:13:37 +0000113static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +0000114
hailfinger1ff33dc2010-07-03 11:02:10 +0000115/* Supported buses for the current programmer. */
116enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +0000117
uwee15beb92010-08-08 17:01:18 +0000118/*
hailfinger80422e22009-12-13 22:28:00 +0000119 * Programmers supporting multiple buses can have differing size limits on
120 * each bus. Store the limits for each bus in a common struct.
121 */
hailfinger1ff33dc2010-07-03 11:02:10 +0000122struct decode_sizes max_rom_decode;
123
124/* If nonzero, used as the start address of bottom-aligned flash. */
125unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +0000126
hailfinger5828baf2010-07-03 12:14:25 +0000127/* Is writing allowed with this programmer? */
128int programmer_may_write;
129
hailfingerabe249e2009-05-08 17:43:22 +0000130const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +0000131#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +0000132 {
hailfinger3548a9a2009-08-12 14:34:35 +0000133 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +0000134 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000135 .map_flash_region = physmap,
136 .unmap_flash_region = physunmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000137 .chip_readb = internal_chip_readb,
138 .chip_readw = internal_chip_readw,
139 .chip_readl = internal_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000140 .chip_readn = internal_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000141 .chip_writeb = internal_chip_writeb,
142 .chip_writew = internal_chip_writew,
143 .chip_writel = internal_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000144 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000145 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +0000146 },
hailfinger80422e22009-12-13 22:28:00 +0000147#endif
stepan927d4e22007-04-04 22:45:58 +0000148
hailfinger90c7d542010-05-31 15:27:27 +0000149#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000150 {
hailfinger3548a9a2009-08-12 14:34:35 +0000151 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000152 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000153 .map_flash_region = dummy_map,
154 .unmap_flash_region = dummy_unmap,
hailfinger6c69ab02009-05-11 15:46:43 +0000155 .chip_readb = dummy_chip_readb,
156 .chip_readw = dummy_chip_readw,
157 .chip_readl = dummy_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000158 .chip_readn = dummy_chip_readn,
hailfinger6c69ab02009-05-11 15:46:43 +0000159 .chip_writeb = dummy_chip_writeb,
160 .chip_writew = dummy_chip_writew,
161 .chip_writel = dummy_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000162 .chip_writen = dummy_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000163 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000164 },
hailfinger571a6b32009-09-16 10:09:21 +0000165#endif
hailfingera9df33c2009-05-09 00:54:55 +0000166
hailfinger90c7d542010-05-31 15:27:27 +0000167#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000168 {
hailfinger3548a9a2009-08-12 14:34:35 +0000169 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000170 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000171 .map_flash_region = fallback_map,
172 .unmap_flash_region = fallback_unmap,
uwe0f5a3a22009-05-13 11:36:06 +0000173 .chip_readb = nic3com_chip_readb,
hailfinger43716942009-05-16 01:23:55 +0000174 .chip_readw = fallback_chip_readw,
175 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000176 .chip_readn = fallback_chip_readn,
uwe0f5a3a22009-05-13 11:36:06 +0000177 .chip_writeb = nic3com_chip_writeb,
hailfinger43716942009-05-16 01:23:55 +0000178 .chip_writew = fallback_chip_writew,
179 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000180 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000181 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000182 },
hailfinger571a6b32009-09-16 10:09:21 +0000183#endif
uwe0f5a3a22009-05-13 11:36:06 +0000184
hailfinger90c7d542010-05-31 15:27:27 +0000185#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000186 {
hailfinger0d703d42011-03-07 01:08:09 +0000187 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000188 .name = "nicrealtek",
189 //.name = "nicsmc1211",
190 .init = nicrealtek_init,
191 .map_flash_region = fallback_map,
192 .unmap_flash_region = fallback_unmap,
193 .chip_readb = nicrealtek_chip_readb,
194 .chip_readw = fallback_chip_readw,
195 .chip_readl = fallback_chip_readl,
196 .chip_readn = fallback_chip_readn,
197 .chip_writeb = nicrealtek_chip_writeb,
198 .chip_writew = fallback_chip_writew,
199 .chip_writel = fallback_chip_writel,
200 .chip_writen = fallback_chip_writen,
201 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000202 },
hailfinger5aa36982010-05-21 21:54:07 +0000203#endif
204
hailfingerf0a368f2010-06-07 22:37:54 +0000205#if CONFIG_NICNATSEMI == 1
206 {
uwe8d342eb2011-07-28 08:13:25 +0000207 .name = "nicnatsemi",
208 .init = nicnatsemi_init,
209 .map_flash_region = fallback_map,
210 .unmap_flash_region = fallback_unmap,
211 .chip_readb = nicnatsemi_chip_readb,
212 .chip_readw = fallback_chip_readw,
213 .chip_readl = fallback_chip_readl,
214 .chip_readn = fallback_chip_readn,
215 .chip_writeb = nicnatsemi_chip_writeb,
216 .chip_writew = fallback_chip_writew,
217 .chip_writel = fallback_chip_writel,
218 .chip_writen = fallback_chip_writen,
219 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000220 },
221#endif
hailfinger5aa36982010-05-21 21:54:07 +0000222
hailfinger90c7d542010-05-31 15:27:27 +0000223#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000224 {
225 .name = "gfxnvidia",
226 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000227 .map_flash_region = fallback_map,
228 .unmap_flash_region = fallback_unmap,
229 .chip_readb = gfxnvidia_chip_readb,
230 .chip_readw = fallback_chip_readw,
231 .chip_readl = fallback_chip_readl,
232 .chip_readn = fallback_chip_readn,
233 .chip_writeb = gfxnvidia_chip_writeb,
234 .chip_writew = fallback_chip_writew,
235 .chip_writel = fallback_chip_writel,
236 .chip_writen = fallback_chip_writen,
237 .delay = internal_delay,
238 },
239#endif
240
hailfinger90c7d542010-05-31 15:27:27 +0000241#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000242 {
uwee2f95ef2009-09-02 23:00:46 +0000243 .name = "drkaiser",
244 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000245 .map_flash_region = fallback_map,
246 .unmap_flash_region = fallback_unmap,
247 .chip_readb = drkaiser_chip_readb,
248 .chip_readw = fallback_chip_readw,
249 .chip_readl = fallback_chip_readl,
250 .chip_readn = fallback_chip_readn,
251 .chip_writeb = drkaiser_chip_writeb,
252 .chip_writew = fallback_chip_writew,
253 .chip_writel = fallback_chip_writel,
254 .chip_writen = fallback_chip_writen,
255 .delay = internal_delay,
256 },
hailfinger571a6b32009-09-16 10:09:21 +0000257#endif
uwee2f95ef2009-09-02 23:00:46 +0000258
hailfinger90c7d542010-05-31 15:27:27 +0000259#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000260 {
hailfinger3548a9a2009-08-12 14:34:35 +0000261 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000262 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000263 .map_flash_region = fallback_map,
264 .unmap_flash_region = fallback_unmap,
ruikda922a12009-05-17 19:39:27 +0000265 .chip_readb = satasii_chip_readb,
266 .chip_readw = fallback_chip_readw,
267 .chip_readl = fallback_chip_readl,
hailfinger9d987ef2009-06-05 18:32:07 +0000268 .chip_readn = fallback_chip_readn,
ruikda922a12009-05-17 19:39:27 +0000269 .chip_writeb = satasii_chip_writeb,
270 .chip_writew = fallback_chip_writew,
271 .chip_writel = fallback_chip_writel,
hailfinger9d987ef2009-06-05 18:32:07 +0000272 .chip_writen = fallback_chip_writen,
hailfingere5829f62009-06-05 17:48:08 +0000273 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000274 },
hailfinger571a6b32009-09-16 10:09:21 +0000275#endif
ruikda922a12009-05-17 19:39:27 +0000276
hailfinger90c7d542010-05-31 15:27:27 +0000277#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000278 {
279 .name = "atahpt",
280 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000281 .map_flash_region = fallback_map,
282 .unmap_flash_region = fallback_unmap,
283 .chip_readb = atahpt_chip_readb,
284 .chip_readw = fallback_chip_readw,
285 .chip_readl = fallback_chip_readl,
286 .chip_readn = fallback_chip_readn,
287 .chip_writeb = atahpt_chip_writeb,
288 .chip_writew = fallback_chip_writew,
289 .chip_writel = fallback_chip_writel,
290 .chip_writen = fallback_chip_writen,
291 .delay = internal_delay,
292 },
293#endif
294
hailfinger90c7d542010-05-31 15:27:27 +0000295#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000296 {
hailfinger90c7d542010-05-31 15:27:27 +0000297 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000298 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000299 .map_flash_region = fallback_map,
300 .unmap_flash_region = fallback_unmap,
hailfinger571a6b32009-09-16 10:09:21 +0000301 .chip_readb = noop_chip_readb,
hailfingerf31da3d2009-06-16 21:08:06 +0000302 .chip_readw = fallback_chip_readw,
303 .chip_readl = fallback_chip_readl,
304 .chip_readn = fallback_chip_readn,
hailfinger571a6b32009-09-16 10:09:21 +0000305 .chip_writeb = noop_chip_writeb,
hailfingerf31da3d2009-06-16 21:08:06 +0000306 .chip_writew = fallback_chip_writew,
307 .chip_writel = fallback_chip_writel,
308 .chip_writen = fallback_chip_writen,
309 .delay = internal_delay,
310 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000311#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000312
hailfinger90c7d542010-05-31 15:27:27 +0000313#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000314 {
hailfinger3548a9a2009-08-12 14:34:35 +0000315 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000316 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000317 .map_flash_region = fallback_map,
318 .unmap_flash_region = fallback_unmap,
319 .chip_readb = serprog_chip_readb,
320 .chip_readw = fallback_chip_readw,
321 .chip_readl = fallback_chip_readl,
322 .chip_readn = serprog_chip_readn,
323 .chip_writeb = serprog_chip_writeb,
324 .chip_writew = fallback_chip_writew,
325 .chip_writel = fallback_chip_writel,
326 .chip_writen = fallback_chip_writen,
327 .delay = serprog_delay,
328 },
hailfinger74d88a72009-08-12 16:17:41 +0000329#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000330
hailfinger90c7d542010-05-31 15:27:27 +0000331#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000332 {
hailfinger90c7d542010-05-31 15:27:27 +0000333 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000334 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000335 .map_flash_region = fallback_map,
336 .unmap_flash_region = fallback_unmap,
337 .chip_readb = noop_chip_readb,
338 .chip_readw = fallback_chip_readw,
339 .chip_readl = fallback_chip_readl,
340 .chip_readn = fallback_chip_readn,
341 .chip_writeb = noop_chip_writeb,
342 .chip_writew = fallback_chip_writew,
343 .chip_writel = fallback_chip_writel,
344 .chip_writen = fallback_chip_writen,
345 .delay = internal_delay,
346 },
347#endif
348
hailfinger90c7d542010-05-31 15:27:27 +0000349#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000350 {
351 .name = "dediprog",
352 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000353 .map_flash_region = fallback_map,
354 .unmap_flash_region = fallback_unmap,
355 .chip_readb = noop_chip_readb,
356 .chip_readw = fallback_chip_readw,
357 .chip_readl = fallback_chip_readl,
358 .chip_readn = fallback_chip_readn,
359 .chip_writeb = noop_chip_writeb,
360 .chip_writew = fallback_chip_writew,
361 .chip_writel = fallback_chip_writel,
362 .chip_writen = fallback_chip_writen,
363 .delay = internal_delay,
364 },
365#endif
366
hailfinger52c4fa02010-07-21 10:26:01 +0000367#if CONFIG_RAYER_SPI == 1
368 {
369 .name = "rayer_spi",
370 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000371 .map_flash_region = fallback_map,
372 .unmap_flash_region = fallback_unmap,
373 .chip_readb = noop_chip_readb,
374 .chip_readw = fallback_chip_readw,
375 .chip_readl = fallback_chip_readl,
376 .chip_readn = fallback_chip_readn,
377 .chip_writeb = noop_chip_writeb,
378 .chip_writew = fallback_chip_writew,
379 .chip_writel = fallback_chip_writel,
380 .chip_writen = fallback_chip_writen,
381 .delay = internal_delay,
382 },
383#endif
384
hailfinger7949b652011-05-08 00:24:18 +0000385#if CONFIG_NICINTEL == 1
386 {
387 .name = "nicintel",
388 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000389 .map_flash_region = fallback_map,
390 .unmap_flash_region = fallback_unmap,
391 .chip_readb = nicintel_chip_readb,
392 .chip_readw = fallback_chip_readw,
393 .chip_readl = fallback_chip_readl,
394 .chip_readn = fallback_chip_readn,
395 .chip_writeb = nicintel_chip_writeb,
396 .chip_writew = fallback_chip_writew,
397 .chip_writel = fallback_chip_writel,
398 .chip_writen = fallback_chip_writen,
399 .delay = internal_delay,
400 },
401#endif
402
uwe6764e922010-09-03 18:21:21 +0000403#if CONFIG_NICINTEL_SPI == 1
404 {
uwe8d342eb2011-07-28 08:13:25 +0000405 .name = "nicintel_spi",
406 .init = nicintel_spi_init,
407 .map_flash_region = fallback_map,
408 .unmap_flash_region = fallback_unmap,
409 .chip_readb = noop_chip_readb,
410 .chip_readw = fallback_chip_readw,
411 .chip_readl = fallback_chip_readl,
412 .chip_readn = fallback_chip_readn,
413 .chip_writeb = noop_chip_writeb,
414 .chip_writew = fallback_chip_writew,
415 .chip_writel = fallback_chip_writel,
416 .chip_writen = fallback_chip_writen,
417 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000418 },
419#endif
420
hailfingerfb1f31f2010-12-03 14:48:11 +0000421#if CONFIG_OGP_SPI == 1
422 {
uwe8d342eb2011-07-28 08:13:25 +0000423 .name = "ogp_spi",
424 .init = ogp_spi_init,
425 .map_flash_region = fallback_map,
426 .unmap_flash_region = fallback_unmap,
427 .chip_readb = noop_chip_readb,
428 .chip_readw = fallback_chip_readw,
429 .chip_readl = fallback_chip_readl,
430 .chip_readn = fallback_chip_readn,
431 .chip_writeb = noop_chip_writeb,
432 .chip_writew = fallback_chip_writew,
433 .chip_writel = fallback_chip_writel,
434 .chip_writen = fallback_chip_writen,
435 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000436 },
437#endif
438
hailfinger935365d2011-02-04 21:37:59 +0000439#if CONFIG_SATAMV == 1
440 {
441 .name = "satamv",
442 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000443 .map_flash_region = fallback_map,
444 .unmap_flash_region = fallback_unmap,
445 .chip_readb = satamv_chip_readb,
446 .chip_readw = fallback_chip_readw,
447 .chip_readl = fallback_chip_readl,
448 .chip_readn = fallback_chip_readn,
449 .chip_writeb = satamv_chip_writeb,
450 .chip_writew = fallback_chip_writew,
451 .chip_writel = fallback_chip_writel,
452 .chip_writen = fallback_chip_writen,
453 .delay = internal_delay,
454 },
455#endif
456
uwe7df6dda2011-09-03 18:37:52 +0000457#if CONFIG_LINUX_SPI == 1
458 {
459 .name = "linux_spi",
460 .init = linux_spi_init,
461 .map_flash_region = fallback_map,
462 .unmap_flash_region = fallback_unmap,
463 .chip_readb = noop_chip_readb,
464 .chip_readw = fallback_chip_readw,
465 .chip_readl = fallback_chip_readl,
466 .chip_readn = fallback_chip_readn,
467 .chip_writeb = noop_chip_writeb,
468 .chip_writew = fallback_chip_writew,
469 .chip_writel = fallback_chip_writel,
470 .chip_writen = fallback_chip_writen,
471 .delay = internal_delay,
472 },
473#endif
474
hailfinger3548a9a2009-08-12 14:34:35 +0000475 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000476};
stepan927d4e22007-04-04 22:45:58 +0000477
David Hendricksbf36f092010-11-02 23:39:29 -0700478#define CHIP_RESTORE_MAXFN 4
479static int chip_restore_fn_count = 0;
480struct chip_restore_func_data {
481 CHIP_RESTORE_CALLBACK;
482 struct flashchip *flash;
483 uint8_t status;
484} static chip_restore_fn[CHIP_RESTORE_MAXFN];
485
David Hendricks668f29d2011-01-27 18:51:45 -0800486
hailfingerf31cbdc2010-11-10 15:25:18 +0000487#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000488static int shutdown_fn_count = 0;
489struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000490 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000491 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000492} static shutdown_fn[SHUTDOWN_MAXFN];
493/* Initialize to 0 to make sure nobody registers a shutdown function before
494 * programmer init.
495 */
496static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000497
stefanct569dbb62011-07-01 00:19:12 +0000498static int check_block_eraser(const struct flashchip *flash, int k, int log);
499
hailfingerdc6f7972010-02-14 01:20:28 +0000500/* Register a function to be executed on programmer shutdown.
501 * The advantage over atexit() is that you can supply a void pointer which will
502 * be used as parameter to the registered function upon programmer shutdown.
503 * This pointer can point to arbitrary data used by said function, e.g. undo
504 * information for GPIO settings etc. If unneeded, set data=NULL.
505 * Please note that the first (void *data) belongs to the function signature of
506 * the function passed as first parameter.
507 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000508int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000509{
510 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000511 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000512 SHUTDOWN_MAXFN);
513 return 1;
514 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000515 if (!may_register_shutdown) {
516 msg_perr("Tried to register a shutdown function before "
517 "programmer init.\n");
518 return 1;
519 }
hailfingerdc6f7972010-02-14 01:20:28 +0000520 shutdown_fn[shutdown_fn_count].func = function;
521 shutdown_fn[shutdown_fn_count].data = data;
522 shutdown_fn_count++;
523
524 return 0;
525}
526
David Hendricksbf36f092010-11-02 23:39:29 -0700527//int register_chip_restore(int (*function) (void *data), void *data)
528int register_chip_restore(CHIP_RESTORE_CALLBACK,
529 struct flashchip *flash, uint8_t status)
530{
531 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
532 msg_perr("Tried to register more than %i chip restore"
533 " functions.\n", CHIP_RESTORE_MAXFN);
534 return 1;
535 }
536 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
537 chip_restore_fn[chip_restore_fn_count].flash = flash;
538 chip_restore_fn[chip_restore_fn_count].status = status;
539 chip_restore_fn_count++;
540
541 return 0;
542}
543
hailfinger1ff33dc2010-07-03 11:02:10 +0000544int programmer_init(char *param)
uweabe92a52009-05-16 22:36:00 +0000545{
hailfinger1ef766d2010-07-06 09:55:48 +0000546 int ret;
hailfinger1ff33dc2010-07-03 11:02:10 +0000547 /* Initialize all programmer specific data. */
548 /* Default to unlimited decode sizes. */
549 max_rom_decode = (const struct decode_sizes) {
550 .parallel = 0xffffffff,
551 .lpc = 0xffffffff,
552 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000553 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000554 };
hailfingere1e41ea2011-07-27 07:13:06 +0000555 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000556 /* Default to top aligned flash at 4 GB. */
557 flashbase = 0;
558 /* Registering shutdown functions is now allowed. */
559 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000560 /* Default to allowing writes. Broken programmers set this to 0. */
561 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000562
563 programmer_param = param;
564 msg_pdbg("Initializing %s programmer\n",
565 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000566 ret = programmer_table[programmer].init();
567 if (programmer_param && strlen(programmer_param)) {
568 msg_perr("Unhandled programmer parameters: %s\n",
569 programmer_param);
570 /* Do not error out here, the init itself was successful. */
571 }
572 return ret;
uweabe92a52009-05-16 22:36:00 +0000573}
574
David Hendricksbf36f092010-11-02 23:39:29 -0700575int chip_restore()
576{
577 int rc = 0;
578
579 while (chip_restore_fn_count > 0) {
580 int i = --chip_restore_fn_count;
581 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
582 chip_restore_fn[i].status);
583 }
584
585 return rc;
586}
587
uweabe92a52009-05-16 22:36:00 +0000588int programmer_shutdown(void)
589{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000590 int ret = 0;
591
hailfinger1ff33dc2010-07-03 11:02:10 +0000592 /* Registering shutdown functions is no longer allowed. */
593 may_register_shutdown = 0;
594 while (shutdown_fn_count > 0) {
595 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000596 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000597 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000598 return ret;
uweabe92a52009-05-16 22:36:00 +0000599}
600
601void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
602 size_t len)
603{
604 return programmer_table[programmer].map_flash_region(descr,
605 phys_addr, len);
606}
607
608void programmer_unmap_flash_region(void *virt_addr, size_t len)
609{
610 programmer_table[programmer].unmap_flash_region(virt_addr, len);
611}
612
613void chip_writeb(uint8_t val, chipaddr addr)
614{
615 programmer_table[programmer].chip_writeb(val, addr);
616}
617
618void chip_writew(uint16_t val, chipaddr addr)
619{
620 programmer_table[programmer].chip_writew(val, addr);
621}
622
623void chip_writel(uint32_t val, chipaddr addr)
624{
625 programmer_table[programmer].chip_writel(val, addr);
626}
627
hailfinger9d987ef2009-06-05 18:32:07 +0000628void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
629{
630 programmer_table[programmer].chip_writen(buf, addr, len);
631}
632
uweabe92a52009-05-16 22:36:00 +0000633uint8_t chip_readb(const chipaddr addr)
634{
635 return programmer_table[programmer].chip_readb(addr);
636}
637
638uint16_t chip_readw(const chipaddr addr)
639{
640 return programmer_table[programmer].chip_readw(addr);
641}
642
643uint32_t chip_readl(const chipaddr addr)
644{
645 return programmer_table[programmer].chip_readl(addr);
646}
647
hailfinger9d987ef2009-06-05 18:32:07 +0000648void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
649{
650 programmer_table[programmer].chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000651}
652
hailfingere5829f62009-06-05 17:48:08 +0000653void programmer_delay(int usecs)
654{
655 programmer_table[programmer].delay(usecs);
656}
657
stuge5ff0e6c2009-01-26 00:39:57 +0000658void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000659{
stepan15e64bc2007-05-24 08:48:10 +0000660 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000661 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000662 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000663 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000664}
665
hailfinger0f08b7a2009-06-16 08:55:44 +0000666int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int len)
hailfinger23060112009-05-08 12:49:03 +0000667{
hailfinger0f08b7a2009-06-16 08:55:44 +0000668 chip_readn(buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000669
hailfinger23060112009-05-08 12:49:03 +0000670 return 0;
671}
672
hailfinger7b414742009-06-13 12:04:03 +0000673int min(int a, int b)
674{
675 return (a < b) ? a : b;
676}
677
hailfinger7af83692009-06-15 17:23:36 +0000678int max(int a, int b)
679{
680 return (a > b) ? a : b;
681}
682
hailfingeraec9c962009-10-31 01:53:09 +0000683int bitcount(unsigned long a)
684{
685 int i = 0;
686 for (; a != 0; a >>= 1)
687 if (a & 1)
688 i++;
689 return i;
690}
691
hailfingerf76cc322010-11-09 22:00:31 +0000692void tolower_string(char *str)
693{
694 for (; *str != '\0'; str++)
695 *str = (char)tolower((unsigned char)*str);
696}
697
hailfingera916b422009-06-01 02:08:58 +0000698char *strcat_realloc(char *dest, const char *src)
699{
700 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000701 if (!dest) {
702 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000703 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000704 }
hailfingera916b422009-06-01 02:08:58 +0000705 strcat(dest, src);
706 return dest;
707}
708
hailfinger6e5a52a2009-11-24 18:27:10 +0000709/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000710 * It will look for needle with a subsequent '=' in haystack, return a copy of
711 * needle and remove everything from the first occurrence of needle to the next
712 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000713 */
stefanct52700282011-06-26 17:38:17 +0000714char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000715{
hailfinger1ef766d2010-07-06 09:55:48 +0000716 char *param_pos, *opt_pos, *rest;
717 char *opt = NULL;
718 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000719 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000720
hailfingerf4aaccc2010-04-28 15:22:14 +0000721 needlelen = strlen(needle);
722 if (!needlelen) {
723 msg_gerr("%s: empty needle! Please report a bug at "
724 "flashrom@flashrom.org\n", __func__);
725 return NULL;
726 }
727 /* No programmer parameters given. */
728 if (*haystack == NULL)
729 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000730 param_pos = strstr(*haystack, needle);
731 do {
732 if (!param_pos)
733 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000734 /* Needle followed by '='? */
735 if (param_pos[needlelen] == '=') {
736
737 /* Beginning of the string? */
738 if (param_pos == *haystack)
739 break;
740 /* After a delimiter? */
741 if (strchr(delim, *(param_pos - 1)))
742 break;
743 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000744 /* Continue searching. */
745 param_pos++;
746 param_pos = strstr(param_pos, needle);
747 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000748
hailfinger6e5a52a2009-11-24 18:27:10 +0000749 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000750 /* Get the string after needle and '='. */
751 opt_pos = param_pos + needlelen + 1;
752 optlen = strcspn(opt_pos, delim);
753 /* Return an empty string if the parameter was empty. */
754 opt = malloc(optlen + 1);
755 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000756 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000757 exit(1);
758 }
hailfinger1ef766d2010-07-06 09:55:48 +0000759 strncpy(opt, opt_pos, optlen);
760 opt[optlen] = '\0';
761 rest = opt_pos + optlen;
762 /* Skip all delimiters after the current parameter. */
763 rest += strspn(rest, delim);
764 memmove(param_pos, rest, strlen(rest) + 1);
765 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000766 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000767
hailfinger1ef766d2010-07-06 09:55:48 +0000768 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000769}
770
stefanct52700282011-06-26 17:38:17 +0000771char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000772{
773 return extract_param(&programmer_param, param_name, ",");
774}
775
stefancte1c5acf2011-07-04 07:27:17 +0000776/* Returns the number of well-defined erasers for a chip. */
777static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000778{
779 unsigned int usable_erasefunctions = 0;
780 int k;
781 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
782 if (!check_block_eraser(flash, k, 0))
783 usable_erasefunctions++;
784 }
785 return usable_erasefunctions;
786}
787
hailfinger7af83692009-06-15 17:23:36 +0000788/* start is an offset to the base address of the flash chip */
789int check_erased_range(struct flashchip *flash, int start, int len)
790{
791 int ret;
792 uint8_t *cmpbuf = malloc(len);
793
794 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000795 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000796 exit(1);
797 }
798 memset(cmpbuf, 0xff, len);
799 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
800 free(cmpbuf);
801 return ret;
802}
803
uwee15beb92010-08-08 17:01:18 +0000804/*
hailfinger7af3d192009-11-25 17:05:52 +0000805 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000806 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000807 * @start offset to the base address of the flash chip
808 * @len length of the verified area
809 * @message string to print in the "FAILED" message
810 * @return 0 for success, -1 for failure
811 */
uwe8d342eb2011-07-28 08:13:25 +0000812int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len,
813 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000814{
hailfingerb91c08c2011-08-15 19:54:20 +0000815 int i;
hailfinger8cb6ece2010-11-16 17:21:58 +0000816 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000817 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000818
819 if (!len)
820 goto out_free;
821
hailfingerb0f4d122009-06-24 08:20:45 +0000822 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000823 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000824 return 1;
825 }
hailfinger7af83692009-06-15 17:23:36 +0000826 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000827 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000828 exit(1);
829 }
830
831 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000832 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000833 " total_size 0x%x\n", __func__, start, len,
834 flash->total_size * 1024);
835 ret = -1;
836 goto out_free;
837 }
838 if (!message)
839 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000840
hailfinger8cb6ece2010-11-16 17:21:58 +0000841 ret = flash->read(flash, readbuf, start, len);
842 if (ret) {
843 msg_gerr("Verification impossible because read failed "
844 "at 0x%x (len 0x%x)\n", start, len);
845 return ret;
846 }
847
848 for (i = 0; i < len; i++) {
849 if (cmpbuf[i] != readbuf[i]) {
850 /* Only print the first failure. */
851 if (!failcount++)
852 msg_cerr("%s FAILED at 0x%08x! "
853 "Expected=0x%02x, Read=0x%02x,",
854 message, start + i, cmpbuf[i],
855 readbuf[i]);
hailfinger7af83692009-06-15 17:23:36 +0000856 }
857 }
hailfinger5be6c0f2009-07-23 01:42:56 +0000858 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000859 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000860 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000861 ret = -1;
862 }
hailfinger7af83692009-06-15 17:23:36 +0000863
864out_free:
865 free(readbuf);
866 return ret;
867}
868
uwee15beb92010-08-08 17:01:18 +0000869/*
hailfingerb247c7a2010-03-08 00:42:32 +0000870 * Check if the buffer @have can be programmed to the content of @want without
871 * erasing. This is only possible if all chunks of size @gran are either kept
872 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000873 *
hailfingerb247c7a2010-03-08 00:42:32 +0000874 * The following write granularities (enum @gran) are known:
875 * - 1 bit. Each bit can be cleared individually.
876 * - 1 byte. A byte can be written once. Further writes to an already written
877 * byte cause the contents to be either undefined or to stay unchanged.
878 * - 128 bytes. If less than 128 bytes are written, the rest will be
879 * erased. Each write to a 128-byte region will trigger an automatic erase
880 * before anything is written. Very uncommon behaviour and unsupported by
881 * this function.
882 * - 256 bytes. If less than 256 bytes are written, the contents of the
883 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000884 * Warning: This function assumes that @have and @want point to naturally
885 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000886 *
887 * @have buffer with current content
888 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000889 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000890 * @gran write granularity (enum, not count)
891 * @return 0 if no erase is needed, 1 otherwise
892 */
893int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
894{
hailfingerb91c08c2011-08-15 19:54:20 +0000895 int result = 0;
896 int i, j, limit;
hailfingerb247c7a2010-03-08 00:42:32 +0000897
898 switch (gran) {
899 case write_gran_1bit:
900 for (i = 0; i < len; i++)
901 if ((have[i] & want[i]) != want[i]) {
902 result = 1;
903 break;
904 }
905 break;
906 case write_gran_1byte:
907 for (i = 0; i < len; i++)
908 if ((have[i] != want[i]) && (have[i] != 0xff)) {
909 result = 1;
910 break;
911 }
912 break;
913 case write_gran_256bytes:
914 for (j = 0; j < len / 256; j++) {
915 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000916 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000917 if (!memcmp(have + j * 256, want + j * 256, limit))
918 continue;
919 /* have needs to be in erased state. */
920 for (i = 0; i < limit; i++)
hailfingerb437e282010-11-04 01:04:27 +0000921 if (have[j * 256 + i] != 0xff) {
hailfingerb247c7a2010-03-08 00:42:32 +0000922 result = 1;
923 break;
924 }
925 if (result)
926 break;
927 }
928 break;
hailfingerb437e282010-11-04 01:04:27 +0000929 default:
930 msg_cerr("%s: Unsupported granularity! Please report a bug at "
931 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000932 }
933 return result;
934}
935
hailfingerb437e282010-11-04 01:04:27 +0000936/**
937 * Check if the buffer @have needs to be programmed to get the content of @want.
938 * If yes, return 1 and fill in first_start with the start address of the
939 * write operation and first_len with the length of the first to-be-written
940 * chunk. If not, return 0 and leave first_start and first_len undefined.
941 *
942 * Warning: This function assumes that @have and @want point to naturally
943 * aligned regions.
944 *
945 * @have buffer with current content
946 * @want buffer with desired content
947 * @len length of the checked area
948 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000949 * @first_start offset of the first byte which needs to be written (passed in
950 * value is increased by the offset of the first needed write
951 * relative to have/want or unchanged if no write is needed)
952 * @return length of the first contiguous area which needs to be written
953 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000954 *
955 * FIXME: This function needs a parameter which tells it about coalescing
956 * in relation to the max write length of the programmer and the max write
957 * length of the chip.
958 */
959static int get_next_write(uint8_t *have, uint8_t *want, int len,
hailfinger90fcf9b2010-11-05 14:51:59 +0000960 int *first_start, enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000961{
hailfingerb91c08c2011-08-15 19:54:20 +0000962 int need_write = 0, rel_start = 0, first_len = 0;
963 int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000964
hailfingerb437e282010-11-04 01:04:27 +0000965 switch (gran) {
966 case write_gran_1bit:
967 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000968 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000969 break;
970 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000971 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000972 break;
973 default:
974 msg_cerr("%s: Unsupported granularity! Please report a bug at "
975 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000976 /* Claim that no write was needed. A write with unknown
977 * granularity is too dangerous to try.
978 */
979 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000980 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000981 for (i = 0; i < len / stride; i++) {
982 limit = min(stride, len - i * stride);
983 /* Are 'have' and 'want' identical? */
984 if (memcmp(have + i * stride, want + i * stride, limit)) {
985 if (!need_write) {
986 /* First location where have and want differ. */
987 need_write = 1;
988 rel_start = i * stride;
989 }
990 } else {
991 if (need_write) {
992 /* First location where have and want
993 * do not differ anymore.
994 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000995 break;
996 }
997 }
998 }
hailfingerffb7f382010-12-06 13:05:44 +0000999 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +00001000 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +00001001 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +00001002 return first_len;
hailfingerb437e282010-11-04 01:04:27 +00001003}
1004
hailfinger0c515352009-11-23 12:55:31 +00001005/* This function generates various test patterns useful for testing controller
1006 * and chip communication as well as chip behaviour.
1007 *
1008 * If a byte can be written multiple times, each time keeping 0-bits at 0
1009 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
1010 * is essentially an AND operation. That's also the reason why this function
1011 * provides the result of AND between various patterns.
1012 *
1013 * Below is a list of patterns (and their block length).
1014 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
1015 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
1016 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
1017 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
1018 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
1019 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
1020 * Pattern 6 is 00 (1 Byte)
1021 * Pattern 7 is ff (1 Byte)
1022 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
1023 * byte block.
1024 *
1025 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
1026 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
1027 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
1028 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
1029 * Pattern 12 is 00 (1 Byte)
1030 * Pattern 13 is ff (1 Byte)
1031 * Patterns 8-13 have no block number.
1032 *
1033 * Patterns 0-3 are created to detect and efficiently diagnose communication
1034 * slips like missed bits or bytes and their repetitive nature gives good visual
1035 * cues to the person inspecting the results. In addition, the following holds:
1036 * AND Pattern 0/1 == Pattern 4
1037 * AND Pattern 2/3 == Pattern 5
1038 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
1039 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
1040 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
1041 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
1042 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
1043 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
1044 * Besides that, they provide for bit testing of the last two bytes of every
1045 * 256 byte block which contains the block number for patterns 0-6.
1046 * Patterns 10-11 are special purpose for detecting subblock aliasing with
1047 * block sizes >256 bytes (some Dataflash chips etc.)
1048 * AND Pattern 8/9 == Pattern 12
1049 * AND Pattern 10/11 == Pattern 12
1050 * Pattern 13 is the completely erased state.
1051 * None of the patterns can detect aliasing at boundaries which are a multiple
1052 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
1053 */
1054int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
1055{
1056 int i;
1057
1058 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +00001059 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +00001060 return 1;
1061 }
1062
1063 switch (variant) {
1064 case 0:
1065 for (i = 0; i < size; i++)
1066 buf[i] = (i & 0xf) << 4 | 0x5;
1067 break;
1068 case 1:
1069 for (i = 0; i < size; i++)
1070 buf[i] = (i & 0xf) << 4 | 0xa;
1071 break;
1072 case 2:
1073 for (i = 0; i < size; i++)
1074 buf[i] = 0x50 | (i & 0xf);
1075 break;
1076 case 3:
1077 for (i = 0; i < size; i++)
1078 buf[i] = 0xa0 | (i & 0xf);
1079 break;
1080 case 4:
1081 for (i = 0; i < size; i++)
1082 buf[i] = (i & 0xf) << 4;
1083 break;
1084 case 5:
1085 for (i = 0; i < size; i++)
1086 buf[i] = i & 0xf;
1087 break;
1088 case 6:
1089 memset(buf, 0x00, size);
1090 break;
1091 case 7:
1092 memset(buf, 0xff, size);
1093 break;
1094 case 8:
1095 for (i = 0; i < size; i++)
1096 buf[i] = i & 0xff;
1097 break;
1098 case 9:
1099 for (i = 0; i < size; i++)
1100 buf[i] = ~(i & 0xff);
1101 break;
1102 case 10:
1103 for (i = 0; i < size % 2; i++) {
1104 buf[i * 2] = (i >> 8) & 0xff;
1105 buf[i * 2 + 1] = i & 0xff;
1106 }
1107 if (size & 0x1)
1108 buf[i * 2] = (i >> 8) & 0xff;
1109 break;
1110 case 11:
1111 for (i = 0; i < size % 2; i++) {
1112 buf[i * 2] = ~((i >> 8) & 0xff);
1113 buf[i * 2 + 1] = ~(i & 0xff);
1114 }
1115 if (size & 0x1)
1116 buf[i * 2] = ~((i >> 8) & 0xff);
1117 break;
1118 case 12:
1119 memset(buf, 0x00, size);
1120 break;
1121 case 13:
1122 memset(buf, 0xff, size);
1123 break;
1124 }
1125
1126 if ((variant >= 0) && (variant <= 7)) {
1127 /* Write block number in the last two bytes of each 256-byte
1128 * block, big endian for easier reading of the hexdump.
1129 * Note that this wraps around for chips larger than 2^24 bytes
1130 * (16 MB).
1131 */
1132 for (i = 0; i < size / 256; i++) {
1133 buf[i * 256 + 254] = (i >> 8) & 0xff;
1134 buf[i * 256 + 255] = i & 0xff;
1135 }
1136 }
1137
1138 return 0;
1139}
1140
hailfingeraec9c962009-10-31 01:53:09 +00001141int check_max_decode(enum chipbustype buses, uint32_t size)
1142{
1143 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001144
1145 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001146 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001147 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001148 "size %u kB of chipset/board/programmer "
1149 "for %s interface, "
1150 "probe/read/erase/write may fail. ", size / 1024,
1151 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001152 }
hailfingere1e41ea2011-07-27 07:13:06 +00001153 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001154 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001155 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001156 "size %u kB of chipset/board/programmer "
1157 "for %s interface, "
1158 "probe/read/erase/write may fail. ", size / 1024,
1159 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001160 }
hailfingere1e41ea2011-07-27 07:13:06 +00001161 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001162 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001163 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001164 "size %u kB of chipset/board/programmer "
1165 "for %s interface, "
1166 "probe/read/erase/write may fail. ", size / 1024,
1167 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001168 }
hailfingere1e41ea2011-07-27 07:13:06 +00001169 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001170 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001171 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001172 "size %u kB of chipset/board/programmer "
1173 "for %s interface, "
1174 "probe/read/erase/write may fail. ", size / 1024,
1175 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001176 }
1177 if (!limitexceeded)
1178 return 0;
1179 /* Sometimes chip and programmer have more than one bus in common,
1180 * and the limit is not exceeded on all buses. Tell the user.
1181 */
1182 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001183 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001184 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001185 "interface which can support a chip of this size. "
1186 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001187 return 1;
1188}
1189
hailfinger48ed3e22011-05-04 00:39:50 +00001190int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001191{
hailfinger48ed3e22011-05-04 00:39:50 +00001192 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001193 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001194 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001195 uint32_t size;
1196 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001197 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001198
hailfinger48ed3e22011-05-04 00:39:50 +00001199 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001200 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001201 continue;
hailfingeraec9c962009-10-31 01:53:09 +00001202 buses_common = buses_supported & flash->bustype;
1203 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001204 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1205 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +00001206 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +00001207 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001208 free(tmp);
1209 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001210 msg_gspew("and chip bus type %s are incompatible.",
1211 tmp);
hailfingera916b422009-06-01 02:08:58 +00001212 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001213 msg_gspew("\n");
1214 continue;
1215 }
1216 msg_gdbg("Probing for %s %s, %d kB: ",
1217 flash->vendor, flash->name, flash->total_size);
1218 if (!flash->probe && !force) {
1219 msg_gdbg("failed! flashrom has no probe function for "
1220 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001221 continue;
1222 }
stepan782fb172007-04-06 11:58:03 +00001223
ollie5672ac62004-03-17 22:22:08 +00001224 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001225 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001226
hailfinger48ed3e22011-05-04 00:39:50 +00001227 /* Start filling in the dynamic data. */
1228 *fill_flash = *flash;
1229
hailfinger72d3b982009-05-09 07:27:23 +00001230 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001231 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001232
stugec1e55fe2008-07-02 17:15:47 +00001233 if (force)
1234 break;
stepanc98b80b2006-03-16 16:57:41 +00001235
hailfinger48ed3e22011-05-04 00:39:50 +00001236 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001237 goto notfound;
1238
hailfinger48ed3e22011-05-04 00:39:50 +00001239 /* If this is the first chip found, accept it.
1240 * If this is not the first chip found, accept it only if it is
1241 * a non-generic match.
1242 * We could either make chipcount global or provide it as
1243 * parameter, or we assume that startchip==0 means this call to
1244 * probe_flash() is the first one and thus no chip has been
1245 * found before.
1246 */
1247 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001248 break;
1249
stuge56300c32008-09-03 23:10:05 +00001250notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001251 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001252 }
uwebe4477b2007-08-23 16:08:21 +00001253
stugec1e55fe2008-07-02 17:15:47 +00001254 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001255 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001256
hailfingere11396b2011-03-08 00:09:11 +00001257#if CONFIG_INTERNAL == 1
1258 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001259 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001260 else
1261#endif
stepan3e7aeae2011-01-19 06:21:54 +00001262 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001263
stefanct588b6d22011-06-26 20:45:35 +00001264 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001265 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1266 force ? "Assuming" : "Found", fill_flash->vendor,
1267 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001268 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001269
hailfinger0f4c3952010-12-02 21:59:42 +00001270 /* Flash registers will not be mapped if the chip was forced. Lock info
1271 * may be stored in registers, so avoid lock info printing.
1272 */
1273 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001274 if (fill_flash->printlock)
1275 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001276
hailfinger48ed3e22011-05-04 00:39:50 +00001277 /* Return position of matching chip. */
1278 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001279}
1280
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001281int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001282{
hailfingerb0f4d122009-06-24 08:20:45 +00001283 int ret;
ollie5b621572004-03-20 16:46:10 +00001284 int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001285
snelsone42c3802010-05-07 20:09:04 +00001286 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001287
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001288 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1289 ret = handle_partial_verify(flash, buf, verify_range);
1290 } else {
1291 ret = verify_range(flash, buf, 0, total_size, NULL);
1292 }
uwef6641642007-05-09 10:17:44 +00001293
hailfingerb0f4d122009-06-24 08:20:45 +00001294 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001295 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001296
hailfingerb0f4d122009-06-24 08:20:45 +00001297 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001298}
1299
uwe8d342eb2011-07-28 08:13:25 +00001300int read_buf_from_file(unsigned char *buf, unsigned long size,
1301 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001302{
1303 unsigned long numbytes;
1304 FILE *image;
1305 struct stat image_stat;
1306
1307 if ((image = fopen(filename, "rb")) == NULL) {
1308 perror(filename);
1309 return 1;
1310 }
1311 if (fstat(fileno(image), &image_stat) != 0) {
1312 perror(filename);
1313 fclose(image);
1314 return 1;
1315 }
1316 if (image_stat.st_size != size) {
1317 msg_gerr("Error: Image size doesn't match\n");
1318 fclose(image);
1319 return 1;
1320 }
1321 numbytes = fread(buf, 1, size, image);
1322 if (fclose(image)) {
1323 perror(filename);
1324 return 1;
1325 }
1326 if (numbytes != size) {
1327 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1328 "wanted %ld!\n", numbytes, size);
1329 return 1;
1330 }
1331 return 0;
1332}
1333
uwe8d342eb2011-07-28 08:13:25 +00001334int write_buf_to_file(unsigned char *buf, unsigned long size,
1335 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001336{
1337 unsigned long numbytes;
1338 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001339
1340 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001341 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001342 return 1;
1343 }
oxygenebf70d352010-01-25 22:55:33 +00001344 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001345 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001346 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001347 }
hailfingerd219a232009-01-28 00:27:54 +00001348
hailfingerd219a232009-01-28 00:27:54 +00001349 numbytes = fwrite(buf, 1, size, image);
1350 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001351 if (numbytes != size) {
1352 msg_gerr("File %s could not be written completely.\n",
1353 filename);
hailfingerd219a232009-01-28 00:27:54 +00001354 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001355 }
hailfingerd219a232009-01-28 00:27:54 +00001356 return 0;
1357}
1358
stefanct52700282011-06-26 17:38:17 +00001359int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001360{
1361 unsigned long size = flash->total_size * 1024;
1362 unsigned char *buf = calloc(size, sizeof(char));
1363 int ret = 0;
1364
1365 msg_cinfo("Reading flash... ");
1366 if (!buf) {
1367 msg_gerr("Memory allocation failed!\n");
1368 msg_cinfo("FAILED.\n");
1369 return 1;
1370 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001371
1372 /* To support partial read, fill buffer to all 0xFF at beginning to make
1373 * debug easier. */
1374 memset(buf, 0xFF, size);
1375
hailfinger42a850a2010-07-13 23:56:13 +00001376 if (!flash->read) {
1377 msg_cerr("No read function available for this flash chip.\n");
1378 ret = 1;
1379 goto out_free;
1380 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001381
1382 /* First try to handle partial read case, rather than read the whole
1383 * flash, which is slow. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001384 ret = handle_partial_read(flash, buf, flash->read, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001385 if (ret < 0) {
1386 msg_cerr("Partial read operation failed!\n");
1387 ret = 1;
1388 goto out_free;
1389 } else if (ret > 0) {
1390 /* Partial read has been handled, pass the whole flash read. */
1391 } else if (flash->read(flash, buf, 0, size)) {
hailfinger42a850a2010-07-13 23:56:13 +00001392 msg_cerr("Read operation failed!\n");
1393 ret = 1;
1394 goto out_free;
1395 }
1396
stefanct9e6b98a2011-05-28 02:37:14 +00001397 ret = write_buf_to_file(buf, size, filename);
hailfinger42a850a2010-07-13 23:56:13 +00001398out_free:
1399 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001400 if (ret)
1401 msg_cerr("FAILED.");
1402 else
1403 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001404 return ret;
1405}
1406
hailfingerb437e282010-11-04 01:04:27 +00001407/* This function shares a lot of its structure with erase_and_write_flash() and
1408 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001409 * Even if an error is found, the function will keep going and check the rest.
1410 */
hailfinger48ed3e22011-05-04 00:39:50 +00001411static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001412{
hailfingerb91c08c2011-08-15 19:54:20 +00001413 int i, j, k;
1414 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001415
1416 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1417 unsigned int done = 0;
1418 struct block_eraser eraser = flash->block_erasers[k];
1419
1420 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1421 /* Blocks with zero size are bugs in flashchips.c. */
1422 if (eraser.eraseblocks[i].count &&
1423 !eraser.eraseblocks[i].size) {
1424 msg_gerr("ERROR: Flash chip %s erase function "
1425 "%i region %i has size 0. Please report"
1426 " a bug at flashrom@flashrom.org\n",
1427 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001428 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001429 }
1430 /* Blocks with zero count are bugs in flashchips.c. */
1431 if (!eraser.eraseblocks[i].count &&
1432 eraser.eraseblocks[i].size) {
1433 msg_gerr("ERROR: Flash chip %s erase function "
1434 "%i region %i has count 0. Please report"
1435 " a bug at flashrom@flashrom.org\n",
1436 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001437 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001438 }
1439 done += eraser.eraseblocks[i].count *
1440 eraser.eraseblocks[i].size;
1441 }
hailfinger9fed35d2010-01-19 06:42:46 +00001442 /* Empty eraseblock definition with erase function. */
1443 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001444 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001445 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001446 if (!done)
1447 continue;
1448 if (done != flash->total_size * 1024) {
1449 msg_gerr("ERROR: Flash chip %s erase function %i "
1450 "region walking resulted in 0x%06x bytes total,"
1451 " expected 0x%06x bytes. Please report a bug at"
1452 " flashrom@flashrom.org\n", flash->name, k,
1453 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001454 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001455 }
hailfinger9fed35d2010-01-19 06:42:46 +00001456 if (!eraser.block_erase)
1457 continue;
1458 /* Check if there are identical erase functions for different
1459 * layouts. That would imply "magic" erase functions. The
1460 * easiest way to check this is with function pointers.
1461 */
uwef6f94d42010-03-13 17:28:29 +00001462 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001463 if (eraser.block_erase ==
1464 flash->block_erasers[j].block_erase) {
1465 msg_gerr("ERROR: Flash chip %s erase function "
1466 "%i and %i are identical. Please report"
1467 " a bug at flashrom@flashrom.org\n",
1468 flash->name, k, j);
1469 ret = 1;
1470 }
uwef6f94d42010-03-13 17:28:29 +00001471 }
hailfinger45177872010-01-18 08:14:43 +00001472 }
hailfinger9fed35d2010-01-19 06:42:46 +00001473 return ret;
hailfinger45177872010-01-18 08:14:43 +00001474}
1475
hailfingerb437e282010-11-04 01:04:27 +00001476static int erase_and_write_block_helper(struct flashchip *flash,
1477 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001478 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001479 uint8_t *newcontents,
1480 int (*erasefn) (struct flashchip *flash,
1481 unsigned int addr,
1482 unsigned int len))
1483{
uwe8d342eb2011-07-28 08:13:25 +00001484 int starthere = 0, lenhere = 0, ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001485 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1486
hailfinger90fcf9b2010-11-05 14:51:59 +00001487 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001488 * need to be adjusted here to keep the impression of proper abstraction
1489 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001490 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001491 newcontents += start;
1492 msg_cdbg(":");
1493 /* FIXME: Assume 256 byte granularity for now to play it safe. */
hailfinger90fcf9b2010-11-05 14:51:59 +00001494 if (need_erase(curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001495 msg_cdbg("E");
1496 ret = erasefn(flash, start, len);
1497 if (ret)
1498 return ret;
hailfingerac8e3182011-06-26 17:04:16 +00001499 if (check_erased_range(flash, start, len)) {
1500 msg_cerr("ERASE FAILED!\n");
1501 return -1;
1502 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001503 /* Erase was successful. Adjust curcontents. */
1504 memset(curcontents, 0xff, len);
hailfingerb437e282010-11-04 01:04:27 +00001505 skip = 0;
1506 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001507 /* get_next_write() sets starthere to a new value after the call. */
1508 while ((lenhere = get_next_write(curcontents + starthere,
1509 newcontents + starthere,
1510 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001511 if (!writecount++)
1512 msg_cdbg("W");
1513 /* Needs the partial write function signature. */
1514 ret = flash->write(flash, newcontents + starthere,
1515 start + starthere, lenhere);
1516 if (ret)
1517 return ret;
1518 starthere += lenhere;
1519 skip = 0;
1520 }
1521 if (skip)
1522 msg_cdbg("S");
1523 return ret;
1524}
1525
hailfinger83541b32010-07-13 00:42:00 +00001526static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1527 int (*do_something) (struct flashchip *flash,
1528 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001529 unsigned int len,
1530 uint8_t *param1,
1531 uint8_t *param2,
1532 int (*erasefn) (
1533 struct flashchip *flash,
1534 unsigned int addr,
1535 unsigned int len)),
1536 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001537{
1538 int i, j;
hailfingerb91c08c2011-08-15 19:54:20 +00001539 unsigned int start = 0;
1540 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001541 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001542
hailfinger2b8c9382010-07-13 00:37:19 +00001543 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1544 /* count==0 for all automatically initialized array
1545 * members so the loop below won't be executed for them.
1546 */
1547 len = eraser.eraseblocks[i].size;
1548 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001549 /* Print this for every block except the first one. */
1550 if (i || j)
1551 msg_cdbg(", ");
1552 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001553 start + len - 1);
hailfingerb437e282010-11-04 01:04:27 +00001554 if (do_something(flash, start, len, param1, param2,
1555 eraser.block_erase)) {
hailfinger2b8c9382010-07-13 00:37:19 +00001556 return 1;
hailfingerb437e282010-11-04 01:04:27 +00001557 }
hailfinger2b8c9382010-07-13 00:37:19 +00001558 start += len;
1559 }
1560 }
hailfingerb437e282010-11-04 01:04:27 +00001561 msg_cdbg("\n");
hailfinger2b8c9382010-07-13 00:37:19 +00001562 return 0;
1563}
1564
stefanct569dbb62011-07-01 00:19:12 +00001565static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001566{
1567 struct block_eraser eraser = flash->block_erasers[k];
1568
1569 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1570 if (log)
1571 msg_cdbg("not defined. ");
1572 return 1;
1573 }
1574 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1575 if (log)
1576 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001577 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001578 return 1;
1579 }
1580 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1581 if (log)
1582 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001583 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001584 return 1;
1585 }
1586 return 0;
1587}
1588
uwe8d342eb2011-07-28 08:13:25 +00001589int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1590 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001591{
hailfinger8f524a82011-07-21 21:21:04 +00001592 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001593 uint8_t *curcontents;
1594 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001595 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001596
hailfingercf848f12010-12-05 15:14:44 +00001597 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001598 curcontents = malloc(size);
1599 if (!curcontents) {
1600 msg_gerr("Out of memory!\n");
1601 exit(1);
1602 }
hailfingerb437e282010-11-04 01:04:27 +00001603 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1604 memcpy(curcontents, oldcontents, size);
1605
hailfinger7df21362009-09-05 02:30:58 +00001606 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001607 if (k != 0)
1608 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001609 if (!usable_erasefunctions) {
1610 msg_cdbg("No usable erase functions left.\n");
1611 break;
1612 }
stefanctf0bc4712011-07-26 14:28:35 +00001613 msg_cdbg("Trying erase function %i... ", k);
1614 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001615 continue;
hailfingercf848f12010-12-05 15:14:44 +00001616 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001617 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1618 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001619 /* If everything is OK, don't try another erase function. */
1620 if (!ret)
1621 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001622 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001623 * contents are. If no usable erase functions remain, we can
1624 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001625 */
hailfingercf848f12010-12-05 15:14:44 +00001626 if (!usable_erasefunctions)
1627 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001628 /* Reading the whole chip may take a while, inform the user even
1629 * in non-verbose mode.
1630 */
1631 msg_cinfo("Reading current flash chip contents... ");
hailfinger6237f5e2010-12-02 02:41:55 +00001632 if (flash->read(flash, curcontents, 0, size)) {
1633 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001634 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001635 /* We have no idea about the flash chip contents, so
1636 * retrying with another erase function is pointless.
1637 */
1638 break;
1639 }
stefanctf0bc4712011-07-26 14:28:35 +00001640 msg_cinfo("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001641 }
hailfingerb437e282010-11-04 01:04:27 +00001642 /* Free the scratchpad. */
1643 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001644
hailfinger7df21362009-09-05 02:30:58 +00001645 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001646 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001647 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001648 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001649 }
1650 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001651}
1652
hailfinger4c47e9d2010-10-19 22:06:20 +00001653void nonfatal_help_message(void)
1654{
1655 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1656 "This means we have to add special support for your board, "
1657 "programmer or flash chip.\n"
1658 "Please report this on IRC at irc.freenode.net (channel "
1659 "#flashrom) or\n"
1660 "mail flashrom@flashrom.org!\n"
1661 "-------------------------------------------------------------"
1662 "------------------\n"
1663 "You may now reboot or simply leave the machine running.\n");
1664}
1665
uweb34ec9f2009-10-01 18:40:02 +00001666void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001667{
snelsone42c3802010-05-07 20:09:04 +00001668 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001669 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001670 "mail flashrom@flashrom.org with FAILED: your board name in "
1671 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001672 "-------------------------------------------------------------"
1673 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001674 "DO NOT REBOOT OR POWEROFF!\n");
1675}
1676
uwe8d342eb2011-07-28 08:13:25 +00001677/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001678void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001679{
1680 enum programmer p;
1681 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001682 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001683 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001684 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001685 }
snelsone42c3802010-05-07 20:09:04 +00001686 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001687}
1688
hailfingerf79d1712010-10-06 23:48:34 +00001689void list_programmers_linebreak(int startcol, int cols, int paren)
1690{
1691 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001692 int pnamelen;
1693 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001694 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001695 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001696
1697 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1698 pname = programmer_table[p].name;
1699 pnamelen = strlen(pname);
1700 if (remaining - pnamelen - 2 < 0) {
1701 if (firstline)
1702 firstline = 0;
1703 else
1704 printf("\n");
1705 for (i = 0; i < startcol; i++)
1706 printf(" ");
1707 remaining = cols - startcol;
1708 } else {
1709 printf(" ");
1710 remaining--;
1711 }
1712 if (paren && (p == 0)) {
1713 printf("(");
1714 remaining--;
1715 }
1716 printf("%s", pname);
1717 remaining -= pnamelen;
1718 if (p < PROGRAMMER_INVALID - 1) {
1719 printf(",");
1720 remaining--;
1721 } else {
1722 if (paren)
1723 printf(")");
1724 printf("\n");
1725 }
1726 }
1727}
1728
hailfinger3b471632010-03-27 16:36:40 +00001729void print_sysinfo(void)
1730{
David Hendricksc6c9f822010-11-03 15:07:01 -07001731 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001732#if HAVE_UTSNAME == 1
1733 struct utsname osinfo;
1734 uname(&osinfo);
1735
David Hendricksc6c9f822010-11-03 15:07:01 -07001736 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001737 osinfo.machine);
1738#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001739 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001740#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001741 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001742#if NEED_PCI == 1
1743#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001744 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001745#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001746 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001747#endif
1748#endif
1749#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001750 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001751#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001752 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001753#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001754 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001755#endif
hailfinger3b471632010-03-27 16:36:40 +00001756#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001757 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001758#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001759 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001760#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001761 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001762#endif
1763#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001764 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001765#endif
1766#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001767 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001768#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001769 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001770#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001771 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001772}
1773
uwefdeca092008-01-21 15:24:22 +00001774void print_version(void)
1775{
David Hendricksc6c9f822010-11-03 15:07:01 -07001776 /* send to stderr for chromium os */
1777 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001778 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001779}
1780
hailfinger74819ad2010-05-15 15:04:37 +00001781void print_banner(void)
1782{
1783 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001784 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001785 msg_ginfo("\n");
1786}
1787
hailfingerc77acb52009-12-24 02:15:55 +00001788int selfcheck(void)
1789{
hailfinger45177872010-01-18 08:14:43 +00001790 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001791 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001792
1793 /* Safety check. Instead of aborting after the first error, check
1794 * if more errors exist.
1795 */
hailfingerc77acb52009-12-24 02:15:55 +00001796 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001797 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001798 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001799 }
stefanct8632c922011-07-26 14:33:46 +00001800 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001801 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001802 * For 'flashchips' we check the first element to be non-null. In the
1803 * other cases there exist use cases where the first element can be
1804 * null. */
1805 if (flashchips == NULL || flashchips[0].vendor == NULL) {
1806 msg_gerr("Flashchips table miscompilation!\n");
1807 ret = 1;
1808 }
hailfinger45177872010-01-18 08:14:43 +00001809 for (flash = flashchips; flash && flash->name; flash++)
1810 if (selfcheck_eraseblocks(flash))
1811 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001812
1813#if CONFIG_INTERNAL == 1
1814 if (chipset_enables == NULL) {
1815 msg_gerr("Chipset enables table does not exist!\n");
1816 ret = 1;
1817 }
hailfinger4640bdb2011-08-31 16:19:50 +00001818 if (board_matches == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001819 msg_gerr("Board enables table does not exist!\n");
1820 ret = 1;
1821 }
1822 if (boards_known == NULL) {
1823 msg_gerr("Known boards table does not exist!\n");
1824 ret = 1;
1825 }
1826 if (laptops_known == NULL) {
1827 msg_gerr("Known laptops table does not exist!\n");
1828 ret = 1;
1829 }
uwe8d342eb2011-07-28 08:13:25 +00001830#endif
hailfinger45177872010-01-18 08:14:43 +00001831 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001832}
1833
hailfinger48ed3e22011-05-04 00:39:50 +00001834void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001835{
1836 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001837 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001838 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001839 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001840 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001841 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001842 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001843 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001844 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001845 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001846 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001847 msg_cdbg(" WRITE");
1848 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001849 }
1850 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1851 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1852 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1853 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001854 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001855 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001856 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001857 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001858 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001859 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001860 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001861 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001862 msg_cdbg(" WRITE");
1863 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001864 }
hailfinger92cd8e32010-01-07 03:24:05 +00001865 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001866 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001867 "in the latest development\n"
1868 "version of flashrom. If you are running the latest "
1869 "development version,\n"
1870 "please email a report to flashrom@flashrom.org if "
1871 "any of the above operations\n"
1872 "work correctly for you with this flash part. Please "
1873 "include the flashrom\n"
1874 "output with the additional -V option for all "
1875 "operations you tested (-V, -Vr,\n"
1876 "-Vw, -VE), and mention which mainboard or "
1877 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001878 "Please mention your board in the subject line. "
1879 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001880 }
1881}
1882
hailfinger771fc182010-10-15 00:01:14 +00001883/* FIXME: This function signature needs to be improved once doit() has a better
1884 * function signature.
1885 */
stefanct02116582011-05-18 01:30:56 +00001886int 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 +00001887{
1888 if (!programmer_may_write && (write_it || erase_it)) {
1889 msg_perr("Write/erase is not working yet on your programmer in "
1890 "its current configuration.\n");
1891 /* --force is the wrong approach, but it's the best we can do
1892 * until the generic programmer parameter parser is merged.
1893 */
1894 if (!force)
1895 return 1;
1896 msg_cerr("Continuing anyway.\n");
1897 }
1898
1899 if (read_it || erase_it || write_it || verify_it) {
1900 /* Everything needs read. */
1901 if (flash->tested & TEST_BAD_READ) {
1902 msg_cerr("Read is not working on this chip. ");
1903 if (!force)
1904 return 1;
1905 msg_cerr("Continuing anyway.\n");
1906 }
1907 if (!flash->read) {
1908 msg_cerr("flashrom has no read function for this "
1909 "flash chip.\n");
1910 return 1;
1911 }
1912 }
1913 if (erase_it || write_it) {
1914 /* Write needs erase. */
1915 if (flash->tested & TEST_BAD_ERASE) {
1916 msg_cerr("Erase is not working on this chip. ");
1917 if (!force)
1918 return 1;
1919 msg_cerr("Continuing anyway.\n");
1920 }
stefancte1c5acf2011-07-04 07:27:17 +00001921 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001922 msg_cerr("flashrom has no erase function for this "
1923 "flash chip.\n");
1924 return 1;
1925 }
hailfinger771fc182010-10-15 00:01:14 +00001926 }
1927 if (write_it) {
1928 if (flash->tested & TEST_BAD_WRITE) {
1929 msg_cerr("Write is not working on this chip. ");
1930 if (!force)
1931 return 1;
1932 msg_cerr("Continuing anyway.\n");
1933 }
1934 if (!flash->write) {
1935 msg_cerr("flashrom has no write function for this "
1936 "flash chip.\n");
1937 return 1;
1938 }
1939 }
1940 return 0;
1941}
1942
hailfingerc77acb52009-12-24 02:15:55 +00001943/* This function signature is horrible. We need to design a better interface,
1944 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001945 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001946 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001947int doit(struct flashchip *flash, int force, const char *filename, int read_it, int write_it, int erase_it, int verify_it, const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001948{
hailfinger4c47e9d2010-10-19 22:06:20 +00001949 uint8_t *oldcontents;
1950 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001951 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001952 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001953
stefanct02116582011-05-18 01:30:56 +00001954 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001955 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001956 ret = 1;
1957 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001958 }
1959
hailfinger771fc182010-10-15 00:01:14 +00001960 /* Given the existence of read locks, we want to unlock for read,
1961 * erase and write.
1962 */
1963 if (flash->unlock)
1964 flash->unlock(flash);
1965
David Hendricks6d62d752011-03-07 21:20:22 -08001966 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001967 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001968 ret = 1;
1969 goto out_nofree;
1970 }
1971
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001972 /* mark entries included using -i argument as "included" if they are
1973 found in the master rom_entries list */
1974 if (process_include_args() < 0) {
1975 ret = 1;
1976 goto out_nofree;
1977 }
1978
hailfinger771fc182010-10-15 00:01:14 +00001979 if (read_it) {
1980 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001981 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001982 }
hailfingerb437e282010-11-04 01:04:27 +00001983
stefanctd611e8f2011-07-12 22:35:21 +00001984 oldcontents = malloc(size);
1985 if (!oldcontents) {
1986 msg_gerr("Out of memory!\n");
1987 exit(1);
1988 }
hailfingerb437e282010-11-04 01:04:27 +00001989 /* Assume worst case: All bits are 0. */
1990 memset(oldcontents, 0x00, size);
stefanctd611e8f2011-07-12 22:35:21 +00001991 newcontents = malloc(size);
1992 if (!newcontents) {
1993 msg_gerr("Out of memory!\n");
1994 exit(1);
1995 }
hailfingerb437e282010-11-04 01:04:27 +00001996 /* Assume best case: All bits should be 1. */
1997 memset(newcontents, 0xff, size);
1998 /* Side effect of the assumptions above: Default write action is erase
1999 * because newcontents looks like a completely erased chip, and
2000 * oldcontents being completely 0x00 means we have to erase everything
2001 * before we can write.
2002 */
2003
ollie0eb62d62004-12-08 20:10:01 +00002004 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002005 /* FIXME: Do we really want the scary warning if erase failed?
2006 * After all, after erase the chip is either blank or partially
2007 * blank or it has the old contents. A blank chip won't boot,
2008 * so if the user wanted erase and reboots afterwards, the user
2009 * knows very well that booting won't work.
2010 */
hailfingerb437e282010-11-04 01:04:27 +00002011 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00002012 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00002013 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00002014 }
hailfinger90fcf9b2010-11-05 14:51:59 +00002015 goto out;
hailfingerd217d122010-10-08 18:52:29 +00002016 }
hailfinger771fc182010-10-15 00:01:14 +00002017
hailfingerd217d122010-10-08 18:52:29 +00002018 if (write_it || verify_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002019 if (read_buf_from_file(newcontents, size, filename)) {
hailfinger90fcf9b2010-11-05 14:51:59 +00002020 ret = 1;
2021 goto out;
stepan1da96c02006-11-21 23:48:51 +00002022 }
2023
hailfinger90c7d542010-05-31 15:27:27 +00002024#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002025 if (programmer == PROGRAMMER_INTERNAL)
2026 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002027#endif
ollie5672ac62004-03-17 22:22:08 +00002028 }
2029
David Hendricksc44d7a02011-10-17 11:28:43 -07002030 /* Obtain a reference image so that we can check whether regions need
2031 * to be erased and to give better diagnostics in case write fails.
hailfinger4c47e9d2010-10-19 22:06:20 +00002032 * The alternative would be to read only the regions which are to be
2033 * preserved, but in that case we might perform unneeded erase which
2034 * takes time as well.
2035 */
David Hendricksc44d7a02011-10-17 11:28:43 -07002036 if (diff_file) {
2037 msg_cdbg("Reading old contents from file... ");
2038 if (read_buf_from_file(oldcontents, size, diff_file)) {
2039 ret = 1;
2040 msg_cdbg("FAILED.\n");
2041 goto out;
2042 }
2043 } else {
2044 msg_cdbg("Reading old contents from flash chip... ");
2045 if (flash->read(flash, oldcontents, 0, size)) {
2046 ret = 1;
2047 msg_cdbg("FAILED.\n");
2048 goto out;
2049 }
hailfinger4c47e9d2010-10-19 22:06:20 +00002050 }
stefanctf0bc4712011-07-26 14:28:35 +00002051 msg_cinfo("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00002052
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002053 // This should be moved into each flash part's code to do it
ollie6a600992005-11-26 21:55:36 +00002054 // cleanly. This does the job.
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002055 if (handle_romentries(flash, oldcontents, newcontents)) {
2056 ret = 1;
2057 msg_cerr("Cannot handle partial read.\n");
2058 goto out;
2059 }
uwef6641642007-05-09 10:17:44 +00002060
ollie6a600992005-11-26 21:55:36 +00002061 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00002062
stuge8ce3a3c2008-04-28 14:47:30 +00002063 if (write_it) {
hailfingerb437e282010-11-04 01:04:27 +00002064 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2065 msg_cerr("Uh oh. Erase/write failed. Checking if "
2066 "anything changed.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00002067 if (!flash->read(flash, newcontents, 0, size)) {
2068 if (!memcmp(oldcontents, newcontents, size)) {
2069 msg_cinfo("Good. It seems nothing was "
2070 "changed.\n");
2071 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002072 ret = 1;
2073 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002074 }
2075 }
hailfingerd217d122010-10-08 18:52:29 +00002076 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002077 ret = 1;
2078 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002079 }
stuge8ce3a3c2008-04-28 14:47:30 +00002080 }
ollie6a600992005-11-26 21:55:36 +00002081
hailfinger0459e1c2009-08-19 13:55:34 +00002082 if (verify_it) {
2083 /* Work around chips which need some time to calm down. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002084 if (write_it && verify_it != VERIFY_PARTIAL)
hailfinger0459e1c2009-08-19 13:55:34 +00002085 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002086
2087 ret = verify_flash(flash, newcontents, verify_it);
2088
hailfingera50d60e2009-11-17 09:57:34 +00002089 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00002090 * might have an emergency situation.
2091 */
2092 if (ret && write_it)
2093 emergency_help_message();
2094 }
ollie6a600992005-11-26 21:55:36 +00002095
hailfinger90fcf9b2010-11-05 14:51:59 +00002096out:
2097 free(oldcontents);
2098 free(newcontents);
2099out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002100 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002101 /*
2102 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2103 * tree. This is because some operations, such as write protection,
2104 * requires programmer_shutdown() but does not call doit().
2105 */
2106// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002107 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002108}