blob: 2cc0d03b639141e4faa24bbc0c5b43cb86f376ac [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
hailfinger969e2f32011-09-08 00:00:29 +000045static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000046
hailfingerddeb4ac2010-07-08 10:13:37 +000047static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000048
hailfinger1ff33dc2010-07-03 11:02:10 +000049/* Supported buses for the current programmer. */
50enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +000051
uwee15beb92010-08-08 17:01:18 +000052/*
hailfinger80422e22009-12-13 22:28:00 +000053 * Programmers supporting multiple buses can have differing size limits on
54 * each bus. Store the limits for each bus in a common struct.
55 */
hailfinger1ff33dc2010-07-03 11:02:10 +000056struct decode_sizes max_rom_decode;
57
58/* If nonzero, used as the start address of bottom-aligned flash. */
59unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000060
hailfinger5828baf2010-07-03 12:14:25 +000061/* Is writing allowed with this programmer? */
62int programmer_may_write;
63
hailfingerabe249e2009-05-08 17:43:22 +000064const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000065#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000066 {
hailfinger3548a9a2009-08-12 14:34:35 +000067 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000068 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000069 .map_flash_region = physmap,
70 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000071 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +000072 },
hailfinger80422e22009-12-13 22:28:00 +000073#endif
stepan927d4e22007-04-04 22:45:58 +000074
hailfinger90c7d542010-05-31 15:27:27 +000075#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +000076 {
hailfinger3548a9a2009-08-12 14:34:35 +000077 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +000078 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +000079 .map_flash_region = dummy_map,
80 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +000081 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +000082 },
hailfinger571a6b32009-09-16 10:09:21 +000083#endif
hailfingera9df33c2009-05-09 00:54:55 +000084
hailfinger90c7d542010-05-31 15:27:27 +000085#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +000086 {
hailfinger3548a9a2009-08-12 14:34:35 +000087 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +000088 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +000089 .map_flash_region = fallback_map,
90 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +000091 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +000092 },
hailfinger571a6b32009-09-16 10:09:21 +000093#endif
uwe0f5a3a22009-05-13 11:36:06 +000094
hailfinger90c7d542010-05-31 15:27:27 +000095#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +000096 {
hailfinger0d703d42011-03-07 01:08:09 +000097 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +000098 .name = "nicrealtek",
99 //.name = "nicsmc1211",
100 .init = nicrealtek_init,
101 .map_flash_region = fallback_map,
102 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000103 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000104 },
hailfinger5aa36982010-05-21 21:54:07 +0000105#endif
106
hailfingerf0a368f2010-06-07 22:37:54 +0000107#if CONFIG_NICNATSEMI == 1
108 {
uwe8d342eb2011-07-28 08:13:25 +0000109 .name = "nicnatsemi",
110 .init = nicnatsemi_init,
111 .map_flash_region = fallback_map,
112 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000113 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000114 },
115#endif
hailfinger5aa36982010-05-21 21:54:07 +0000116
hailfinger90c7d542010-05-31 15:27:27 +0000117#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000118 {
119 .name = "gfxnvidia",
120 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000121 .map_flash_region = fallback_map,
122 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000123 .delay = internal_delay,
124 },
125#endif
126
hailfinger90c7d542010-05-31 15:27:27 +0000127#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000128 {
uwee2f95ef2009-09-02 23:00:46 +0000129 .name = "drkaiser",
130 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000131 .map_flash_region = fallback_map,
132 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000133 .delay = internal_delay,
134 },
hailfinger571a6b32009-09-16 10:09:21 +0000135#endif
uwee2f95ef2009-09-02 23:00:46 +0000136
hailfinger90c7d542010-05-31 15:27:27 +0000137#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000138 {
hailfinger3548a9a2009-08-12 14:34:35 +0000139 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000140 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000141 .map_flash_region = fallback_map,
142 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000143 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000144 },
hailfinger571a6b32009-09-16 10:09:21 +0000145#endif
ruikda922a12009-05-17 19:39:27 +0000146
hailfinger90c7d542010-05-31 15:27:27 +0000147#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000148 {
149 .name = "atahpt",
150 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000151 .map_flash_region = fallback_map,
152 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000153 .delay = internal_delay,
154 },
155#endif
156
hailfinger90c7d542010-05-31 15:27:27 +0000157#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000158 {
hailfinger90c7d542010-05-31 15:27:27 +0000159 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000160 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000161 .map_flash_region = fallback_map,
162 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000163 .delay = internal_delay,
164 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000165#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000166
hailfinger90c7d542010-05-31 15:27:27 +0000167#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000168 {
hailfinger3548a9a2009-08-12 14:34:35 +0000169 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000170 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000171 .map_flash_region = fallback_map,
172 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000173 .delay = serprog_delay,
174 },
hailfinger74d88a72009-08-12 16:17:41 +0000175#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000176
hailfinger90c7d542010-05-31 15:27:27 +0000177#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000178 {
hailfinger90c7d542010-05-31 15:27:27 +0000179 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000180 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000181 .map_flash_region = fallback_map,
182 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000183 .delay = internal_delay,
184 },
185#endif
186
hailfinger90c7d542010-05-31 15:27:27 +0000187#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000188 {
189 .name = "dediprog",
190 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000191 .map_flash_region = fallback_map,
192 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000193 .delay = internal_delay,
194 },
195#endif
196
hailfinger52c4fa02010-07-21 10:26:01 +0000197#if CONFIG_RAYER_SPI == 1
198 {
199 .name = "rayer_spi",
200 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000201 .map_flash_region = fallback_map,
202 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000203 .delay = internal_delay,
204 },
205#endif
206
hailfinger7949b652011-05-08 00:24:18 +0000207#if CONFIG_NICINTEL == 1
208 {
209 .name = "nicintel",
210 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000211 .map_flash_region = fallback_map,
212 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000213 .delay = internal_delay,
214 },
215#endif
216
uwe6764e922010-09-03 18:21:21 +0000217#if CONFIG_NICINTEL_SPI == 1
218 {
uwe8d342eb2011-07-28 08:13:25 +0000219 .name = "nicintel_spi",
220 .init = nicintel_spi_init,
221 .map_flash_region = fallback_map,
222 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000223 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000224 },
225#endif
226
hailfingerfb1f31f2010-12-03 14:48:11 +0000227#if CONFIG_OGP_SPI == 1
228 {
uwe8d342eb2011-07-28 08:13:25 +0000229 .name = "ogp_spi",
230 .init = ogp_spi_init,
231 .map_flash_region = fallback_map,
232 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000233 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000234 },
235#endif
236
hailfinger935365d2011-02-04 21:37:59 +0000237#if CONFIG_SATAMV == 1
238 {
239 .name = "satamv",
240 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000241 .map_flash_region = fallback_map,
242 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000243 .delay = internal_delay,
244 },
245#endif
246
uwe7df6dda2011-09-03 18:37:52 +0000247#if CONFIG_LINUX_SPI == 1
248 {
249 .name = "linux_spi",
250 .init = linux_spi_init,
251 .map_flash_region = fallback_map,
252 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000253 .delay = internal_delay,
254 },
255#endif
256
hailfinger3548a9a2009-08-12 14:34:35 +0000257 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000258};
stepan927d4e22007-04-04 22:45:58 +0000259
David Hendricksbf36f092010-11-02 23:39:29 -0700260#define CHIP_RESTORE_MAXFN 4
261static int chip_restore_fn_count = 0;
262struct chip_restore_func_data {
263 CHIP_RESTORE_CALLBACK;
264 struct flashchip *flash;
265 uint8_t status;
266} static chip_restore_fn[CHIP_RESTORE_MAXFN];
267
David Hendricks668f29d2011-01-27 18:51:45 -0800268
hailfingerf31cbdc2010-11-10 15:25:18 +0000269#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000270static int shutdown_fn_count = 0;
271struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000272 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000273 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000274} static shutdown_fn[SHUTDOWN_MAXFN];
275/* Initialize to 0 to make sure nobody registers a shutdown function before
276 * programmer init.
277 */
278static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000279
stefanct569dbb62011-07-01 00:19:12 +0000280static int check_block_eraser(const struct flashchip *flash, int k, int log);
281
hailfingerdc6f7972010-02-14 01:20:28 +0000282/* Register a function to be executed on programmer shutdown.
283 * The advantage over atexit() is that you can supply a void pointer which will
284 * be used as parameter to the registered function upon programmer shutdown.
285 * This pointer can point to arbitrary data used by said function, e.g. undo
286 * information for GPIO settings etc. If unneeded, set data=NULL.
287 * Please note that the first (void *data) belongs to the function signature of
288 * the function passed as first parameter.
289 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000290int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000291{
292 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000293 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000294 SHUTDOWN_MAXFN);
295 return 1;
296 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000297 if (!may_register_shutdown) {
298 msg_perr("Tried to register a shutdown function before "
299 "programmer init.\n");
300 return 1;
301 }
hailfingerdc6f7972010-02-14 01:20:28 +0000302 shutdown_fn[shutdown_fn_count].func = function;
303 shutdown_fn[shutdown_fn_count].data = data;
304 shutdown_fn_count++;
305
306 return 0;
307}
308
David Hendricksbf36f092010-11-02 23:39:29 -0700309//int register_chip_restore(int (*function) (void *data), void *data)
310int register_chip_restore(CHIP_RESTORE_CALLBACK,
311 struct flashchip *flash, uint8_t status)
312{
313 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
314 msg_perr("Tried to register more than %i chip restore"
315 " functions.\n", CHIP_RESTORE_MAXFN);
316 return 1;
317 }
318 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
319 chip_restore_fn[chip_restore_fn_count].flash = flash;
320 chip_restore_fn[chip_restore_fn_count].status = status;
321 chip_restore_fn_count++;
322
323 return 0;
324}
325
hailfinger969e2f32011-09-08 00:00:29 +0000326int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000327{
hailfinger1ef766d2010-07-06 09:55:48 +0000328 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000329
330 if (prog >= PROGRAMMER_INVALID) {
331 msg_perr("Invalid programmer specified!\n");
332 return -1;
333 }
334 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000335 /* Initialize all programmer specific data. */
336 /* Default to unlimited decode sizes. */
337 max_rom_decode = (const struct decode_sizes) {
338 .parallel = 0xffffffff,
339 .lpc = 0xffffffff,
340 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000341 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000342 };
hailfingere1e41ea2011-07-27 07:13:06 +0000343 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000344 /* Default to top aligned flash at 4 GB. */
345 flashbase = 0;
346 /* Registering shutdown functions is now allowed. */
347 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000348 /* Default to allowing writes. Broken programmers set this to 0. */
349 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000350
351 programmer_param = param;
352 msg_pdbg("Initializing %s programmer\n",
353 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000354 ret = programmer_table[programmer].init();
355 if (programmer_param && strlen(programmer_param)) {
356 msg_perr("Unhandled programmer parameters: %s\n",
357 programmer_param);
358 /* Do not error out here, the init itself was successful. */
359 }
360 return ret;
uweabe92a52009-05-16 22:36:00 +0000361}
362
David Hendricksbf36f092010-11-02 23:39:29 -0700363int chip_restore()
364{
365 int rc = 0;
366
367 while (chip_restore_fn_count > 0) {
368 int i = --chip_restore_fn_count;
369 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
370 chip_restore_fn[i].status);
371 }
372
373 return rc;
374}
375
uweabe92a52009-05-16 22:36:00 +0000376int programmer_shutdown(void)
377{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000378 int ret = 0;
379
hailfinger1ff33dc2010-07-03 11:02:10 +0000380 /* Registering shutdown functions is no longer allowed. */
381 may_register_shutdown = 0;
382 while (shutdown_fn_count > 0) {
383 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000384 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000385 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000386 return ret;
uweabe92a52009-05-16 22:36:00 +0000387}
388
389void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
390 size_t len)
391{
392 return programmer_table[programmer].map_flash_region(descr,
393 phys_addr, len);
394}
395
396void programmer_unmap_flash_region(void *virt_addr, size_t len)
397{
398 programmer_table[programmer].unmap_flash_region(virt_addr, len);
399}
400
401void chip_writeb(uint8_t val, chipaddr addr)
402{
hailfinger76bb7e92011-11-09 23:40:00 +0000403 par_programmer->chip_writeb(val, addr);
uweabe92a52009-05-16 22:36:00 +0000404}
405
406void chip_writew(uint16_t val, chipaddr addr)
407{
hailfinger76bb7e92011-11-09 23:40:00 +0000408 par_programmer->chip_writew(val, addr);
uweabe92a52009-05-16 22:36:00 +0000409}
410
411void chip_writel(uint32_t val, chipaddr addr)
412{
hailfinger76bb7e92011-11-09 23:40:00 +0000413 par_programmer->chip_writel(val, addr);
uweabe92a52009-05-16 22:36:00 +0000414}
415
hailfinger9d987ef2009-06-05 18:32:07 +0000416void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
417{
hailfinger76bb7e92011-11-09 23:40:00 +0000418 par_programmer->chip_writen(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000419}
420
uweabe92a52009-05-16 22:36:00 +0000421uint8_t chip_readb(const chipaddr addr)
422{
hailfinger76bb7e92011-11-09 23:40:00 +0000423 return par_programmer->chip_readb(addr);
uweabe92a52009-05-16 22:36:00 +0000424}
425
426uint16_t chip_readw(const chipaddr addr)
427{
hailfinger76bb7e92011-11-09 23:40:00 +0000428 return par_programmer->chip_readw(addr);
uweabe92a52009-05-16 22:36:00 +0000429}
430
431uint32_t chip_readl(const chipaddr addr)
432{
hailfinger76bb7e92011-11-09 23:40:00 +0000433 return par_programmer->chip_readl(addr);
uweabe92a52009-05-16 22:36:00 +0000434}
435
hailfinger9d987ef2009-06-05 18:32:07 +0000436void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
437{
hailfinger76bb7e92011-11-09 23:40:00 +0000438 par_programmer->chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000439}
440
hailfingere5829f62009-06-05 17:48:08 +0000441void programmer_delay(int usecs)
442{
443 programmer_table[programmer].delay(usecs);
444}
445
stuge5ff0e6c2009-01-26 00:39:57 +0000446void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000447{
stepan15e64bc2007-05-24 08:48:10 +0000448 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000449 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000450 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000451 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000452}
453
stefanctc5eb8a92011-11-23 09:13:48 +0000454int read_memmapped(struct flashchip *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000455{
hailfinger0f08b7a2009-06-16 08:55:44 +0000456 chip_readn(buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000457
hailfinger23060112009-05-08 12:49:03 +0000458 return 0;
459}
460
hailfinger7b414742009-06-13 12:04:03 +0000461int min(int a, int b)
462{
463 return (a < b) ? a : b;
464}
465
hailfinger7af83692009-06-15 17:23:36 +0000466int max(int a, int b)
467{
468 return (a > b) ? a : b;
469}
470
hailfingeraec9c962009-10-31 01:53:09 +0000471int bitcount(unsigned long a)
472{
473 int i = 0;
474 for (; a != 0; a >>= 1)
475 if (a & 1)
476 i++;
477 return i;
478}
479
hailfingerf76cc322010-11-09 22:00:31 +0000480void tolower_string(char *str)
481{
482 for (; *str != '\0'; str++)
483 *str = (char)tolower((unsigned char)*str);
484}
485
hailfingera916b422009-06-01 02:08:58 +0000486char *strcat_realloc(char *dest, const char *src)
487{
488 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000489 if (!dest) {
490 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000491 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000492 }
hailfingera916b422009-06-01 02:08:58 +0000493 strcat(dest, src);
494 return dest;
495}
496
hailfinger6e5a52a2009-11-24 18:27:10 +0000497/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000498 * It will look for needle with a subsequent '=' in haystack, return a copy of
499 * needle and remove everything from the first occurrence of needle to the next
500 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000501 */
stefanct52700282011-06-26 17:38:17 +0000502char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000503{
hailfinger1ef766d2010-07-06 09:55:48 +0000504 char *param_pos, *opt_pos, *rest;
505 char *opt = NULL;
506 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000507 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000508
hailfingerf4aaccc2010-04-28 15:22:14 +0000509 needlelen = strlen(needle);
510 if (!needlelen) {
511 msg_gerr("%s: empty needle! Please report a bug at "
512 "flashrom@flashrom.org\n", __func__);
513 return NULL;
514 }
515 /* No programmer parameters given. */
516 if (*haystack == NULL)
517 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000518 param_pos = strstr(*haystack, needle);
519 do {
520 if (!param_pos)
521 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000522 /* Needle followed by '='? */
523 if (param_pos[needlelen] == '=') {
524
525 /* Beginning of the string? */
526 if (param_pos == *haystack)
527 break;
528 /* After a delimiter? */
529 if (strchr(delim, *(param_pos - 1)))
530 break;
531 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000532 /* Continue searching. */
533 param_pos++;
534 param_pos = strstr(param_pos, needle);
535 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000536
hailfinger6e5a52a2009-11-24 18:27:10 +0000537 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000538 /* Get the string after needle and '='. */
539 opt_pos = param_pos + needlelen + 1;
540 optlen = strcspn(opt_pos, delim);
541 /* Return an empty string if the parameter was empty. */
542 opt = malloc(optlen + 1);
543 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000544 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000545 exit(1);
546 }
hailfinger1ef766d2010-07-06 09:55:48 +0000547 strncpy(opt, opt_pos, optlen);
548 opt[optlen] = '\0';
549 rest = opt_pos + optlen;
550 /* Skip all delimiters after the current parameter. */
551 rest += strspn(rest, delim);
552 memmove(param_pos, rest, strlen(rest) + 1);
553 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000554 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000555
hailfinger1ef766d2010-07-06 09:55:48 +0000556 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000557}
558
stefanct52700282011-06-26 17:38:17 +0000559char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000560{
561 return extract_param(&programmer_param, param_name, ",");
562}
563
stefancte1c5acf2011-07-04 07:27:17 +0000564/* Returns the number of well-defined erasers for a chip. */
565static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000566{
567 unsigned int usable_erasefunctions = 0;
568 int k;
569 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
570 if (!check_block_eraser(flash, k, 0))
571 usable_erasefunctions++;
572 }
573 return usable_erasefunctions;
574}
575
hailfinger7af83692009-06-15 17:23:36 +0000576/* start is an offset to the base address of the flash chip */
stefanctc5eb8a92011-11-23 09:13:48 +0000577int check_erased_range(struct flashchip *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000578{
579 int ret;
580 uint8_t *cmpbuf = malloc(len);
581
582 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000583 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000584 exit(1);
585 }
586 memset(cmpbuf, 0xff, len);
587 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
588 free(cmpbuf);
589 return ret;
590}
591
uwee15beb92010-08-08 17:01:18 +0000592/*
hailfinger7af3d192009-11-25 17:05:52 +0000593 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000594 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000595 * @start offset to the base address of the flash chip
596 * @len length of the verified area
597 * @message string to print in the "FAILED" message
598 * @return 0 for success, -1 for failure
599 */
stefanctc5eb8a92011-11-23 09:13:48 +0000600int verify_range(struct flashchip *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000601 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000602{
stefanctc5eb8a92011-11-23 09:13:48 +0000603 unsigned int i;
hailfinger8cb6ece2010-11-16 17:21:58 +0000604 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000605 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000606
607 if (!len)
608 goto out_free;
609
hailfingerb0f4d122009-06-24 08:20:45 +0000610 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000611 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000612 return 1;
613 }
hailfinger7af83692009-06-15 17:23:36 +0000614 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000615 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000616 exit(1);
617 }
618
619 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000620 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000621 " total_size 0x%x\n", __func__, start, len,
622 flash->total_size * 1024);
623 ret = -1;
624 goto out_free;
625 }
626 if (!message)
627 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000628
hailfinger8cb6ece2010-11-16 17:21:58 +0000629 ret = flash->read(flash, readbuf, start, len);
630 if (ret) {
631 msg_gerr("Verification impossible because read failed "
632 "at 0x%x (len 0x%x)\n", start, len);
633 return ret;
634 }
635
636 for (i = 0; i < len; i++) {
637 if (cmpbuf[i] != readbuf[i]) {
638 /* Only print the first failure. */
639 if (!failcount++)
640 msg_cerr("%s FAILED at 0x%08x! "
641 "Expected=0x%02x, Read=0x%02x,",
642 message, start + i, cmpbuf[i],
643 readbuf[i]);
hailfinger7af83692009-06-15 17:23:36 +0000644 }
645 }
hailfinger5be6c0f2009-07-23 01:42:56 +0000646 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000647 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000648 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000649 ret = -1;
650 }
hailfinger7af83692009-06-15 17:23:36 +0000651
652out_free:
653 free(readbuf);
654 return ret;
655}
656
uwee15beb92010-08-08 17:01:18 +0000657/*
hailfingerb247c7a2010-03-08 00:42:32 +0000658 * Check if the buffer @have can be programmed to the content of @want without
659 * erasing. This is only possible if all chunks of size @gran are either kept
660 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000661 *
hailfingerb247c7a2010-03-08 00:42:32 +0000662 * The following write granularities (enum @gran) are known:
663 * - 1 bit. Each bit can be cleared individually.
664 * - 1 byte. A byte can be written once. Further writes to an already written
665 * byte cause the contents to be either undefined or to stay unchanged.
666 * - 128 bytes. If less than 128 bytes are written, the rest will be
667 * erased. Each write to a 128-byte region will trigger an automatic erase
668 * before anything is written. Very uncommon behaviour and unsupported by
669 * this function.
670 * - 256 bytes. If less than 256 bytes are written, the contents of the
671 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000672 * Warning: This function assumes that @have and @want point to naturally
673 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000674 *
675 * @have buffer with current content
676 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000677 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000678 * @gran write granularity (enum, not count)
679 * @return 0 if no erase is needed, 1 otherwise
680 */
stefanctc5eb8a92011-11-23 09:13:48 +0000681int need_erase(uint8_t *have, uint8_t *want, unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000682{
hailfingerb91c08c2011-08-15 19:54:20 +0000683 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000684 unsigned int i, j, limit;
hailfingerb247c7a2010-03-08 00:42:32 +0000685
686 switch (gran) {
687 case write_gran_1bit:
688 for (i = 0; i < len; i++)
689 if ((have[i] & want[i]) != want[i]) {
690 result = 1;
691 break;
692 }
693 break;
694 case write_gran_1byte:
695 for (i = 0; i < len; i++)
696 if ((have[i] != want[i]) && (have[i] != 0xff)) {
697 result = 1;
698 break;
699 }
700 break;
701 case write_gran_256bytes:
702 for (j = 0; j < len / 256; j++) {
703 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000704 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000705 if (!memcmp(have + j * 256, want + j * 256, limit))
706 continue;
707 /* have needs to be in erased state. */
708 for (i = 0; i < limit; i++)
hailfingerb437e282010-11-04 01:04:27 +0000709 if (have[j * 256 + i] != 0xff) {
hailfingerb247c7a2010-03-08 00:42:32 +0000710 result = 1;
711 break;
712 }
713 if (result)
714 break;
715 }
716 break;
hailfingerb437e282010-11-04 01:04:27 +0000717 default:
718 msg_cerr("%s: Unsupported granularity! Please report a bug at "
719 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000720 }
721 return result;
722}
723
hailfingerb437e282010-11-04 01:04:27 +0000724/**
725 * Check if the buffer @have needs to be programmed to get the content of @want.
726 * If yes, return 1 and fill in first_start with the start address of the
727 * write operation and first_len with the length of the first to-be-written
728 * chunk. If not, return 0 and leave first_start and first_len undefined.
729 *
730 * Warning: This function assumes that @have and @want point to naturally
731 * aligned regions.
732 *
733 * @have buffer with current content
734 * @want buffer with desired content
735 * @len length of the checked area
736 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000737 * @first_start offset of the first byte which needs to be written (passed in
738 * value is increased by the offset of the first needed write
739 * relative to have/want or unchanged if no write is needed)
740 * @return length of the first contiguous area which needs to be written
741 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000742 *
743 * FIXME: This function needs a parameter which tells it about coalescing
744 * in relation to the max write length of the programmer and the max write
745 * length of the chip.
746 */
stefanctc5eb8a92011-11-23 09:13:48 +0000747static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
748 unsigned int *first_start,
749 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000750{
stefanctc5eb8a92011-11-23 09:13:48 +0000751 int need_write = 0;
752 unsigned int rel_start = 0, first_len = 0;
753 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000754
hailfingerb437e282010-11-04 01:04:27 +0000755 switch (gran) {
756 case write_gran_1bit:
757 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000758 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000759 break;
760 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000761 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000762 break;
763 default:
764 msg_cerr("%s: Unsupported granularity! Please report a bug at "
765 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000766 /* Claim that no write was needed. A write with unknown
767 * granularity is too dangerous to try.
768 */
769 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000770 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000771 for (i = 0; i < len / stride; i++) {
772 limit = min(stride, len - i * stride);
773 /* Are 'have' and 'want' identical? */
774 if (memcmp(have + i * stride, want + i * stride, limit)) {
775 if (!need_write) {
776 /* First location where have and want differ. */
777 need_write = 1;
778 rel_start = i * stride;
779 }
780 } else {
781 if (need_write) {
782 /* First location where have and want
783 * do not differ anymore.
784 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000785 break;
786 }
787 }
788 }
hailfingerffb7f382010-12-06 13:05:44 +0000789 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000790 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000791 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000792 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000793}
794
hailfinger0c515352009-11-23 12:55:31 +0000795/* This function generates various test patterns useful for testing controller
796 * and chip communication as well as chip behaviour.
797 *
798 * If a byte can be written multiple times, each time keeping 0-bits at 0
799 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
800 * is essentially an AND operation. That's also the reason why this function
801 * provides the result of AND between various patterns.
802 *
803 * Below is a list of patterns (and their block length).
804 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
805 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
806 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
807 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
808 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
809 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
810 * Pattern 6 is 00 (1 Byte)
811 * Pattern 7 is ff (1 Byte)
812 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
813 * byte block.
814 *
815 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
816 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
817 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
818 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
819 * Pattern 12 is 00 (1 Byte)
820 * Pattern 13 is ff (1 Byte)
821 * Patterns 8-13 have no block number.
822 *
823 * Patterns 0-3 are created to detect and efficiently diagnose communication
824 * slips like missed bits or bytes and their repetitive nature gives good visual
825 * cues to the person inspecting the results. In addition, the following holds:
826 * AND Pattern 0/1 == Pattern 4
827 * AND Pattern 2/3 == Pattern 5
828 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
829 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
830 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
831 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
832 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
833 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
834 * Besides that, they provide for bit testing of the last two bytes of every
835 * 256 byte block which contains the block number for patterns 0-6.
836 * Patterns 10-11 are special purpose for detecting subblock aliasing with
837 * block sizes >256 bytes (some Dataflash chips etc.)
838 * AND Pattern 8/9 == Pattern 12
839 * AND Pattern 10/11 == Pattern 12
840 * Pattern 13 is the completely erased state.
841 * None of the patterns can detect aliasing at boundaries which are a multiple
842 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
843 */
844int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
845{
846 int i;
847
848 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000849 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000850 return 1;
851 }
852
853 switch (variant) {
854 case 0:
855 for (i = 0; i < size; i++)
856 buf[i] = (i & 0xf) << 4 | 0x5;
857 break;
858 case 1:
859 for (i = 0; i < size; i++)
860 buf[i] = (i & 0xf) << 4 | 0xa;
861 break;
862 case 2:
863 for (i = 0; i < size; i++)
864 buf[i] = 0x50 | (i & 0xf);
865 break;
866 case 3:
867 for (i = 0; i < size; i++)
868 buf[i] = 0xa0 | (i & 0xf);
869 break;
870 case 4:
871 for (i = 0; i < size; i++)
872 buf[i] = (i & 0xf) << 4;
873 break;
874 case 5:
875 for (i = 0; i < size; i++)
876 buf[i] = i & 0xf;
877 break;
878 case 6:
879 memset(buf, 0x00, size);
880 break;
881 case 7:
882 memset(buf, 0xff, size);
883 break;
884 case 8:
885 for (i = 0; i < size; i++)
886 buf[i] = i & 0xff;
887 break;
888 case 9:
889 for (i = 0; i < size; i++)
890 buf[i] = ~(i & 0xff);
891 break;
892 case 10:
893 for (i = 0; i < size % 2; i++) {
894 buf[i * 2] = (i >> 8) & 0xff;
895 buf[i * 2 + 1] = i & 0xff;
896 }
897 if (size & 0x1)
898 buf[i * 2] = (i >> 8) & 0xff;
899 break;
900 case 11:
901 for (i = 0; i < size % 2; i++) {
902 buf[i * 2] = ~((i >> 8) & 0xff);
903 buf[i * 2 + 1] = ~(i & 0xff);
904 }
905 if (size & 0x1)
906 buf[i * 2] = ~((i >> 8) & 0xff);
907 break;
908 case 12:
909 memset(buf, 0x00, size);
910 break;
911 case 13:
912 memset(buf, 0xff, size);
913 break;
914 }
915
916 if ((variant >= 0) && (variant <= 7)) {
917 /* Write block number in the last two bytes of each 256-byte
918 * block, big endian for easier reading of the hexdump.
919 * Note that this wraps around for chips larger than 2^24 bytes
920 * (16 MB).
921 */
922 for (i = 0; i < size / 256; i++) {
923 buf[i * 256 + 254] = (i >> 8) & 0xff;
924 buf[i * 256 + 255] = i & 0xff;
925 }
926 }
927
928 return 0;
929}
930
hailfingeraec9c962009-10-31 01:53:09 +0000931int check_max_decode(enum chipbustype buses, uint32_t size)
932{
933 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +0000934
935 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000936 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000937 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000938 "size %u kB of chipset/board/programmer "
939 "for %s interface, "
940 "probe/read/erase/write may fail. ", size / 1024,
941 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +0000942 }
hailfingere1e41ea2011-07-27 07:13:06 +0000943 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000944 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000945 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000946 "size %u kB of chipset/board/programmer "
947 "for %s interface, "
948 "probe/read/erase/write may fail. ", size / 1024,
949 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +0000950 }
hailfingere1e41ea2011-07-27 07:13:06 +0000951 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000952 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000953 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000954 "size %u kB of chipset/board/programmer "
955 "for %s interface, "
956 "probe/read/erase/write may fail. ", size / 1024,
957 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +0000958 }
hailfingere1e41ea2011-07-27 07:13:06 +0000959 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000960 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000961 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000962 "size %u kB of chipset/board/programmer "
963 "for %s interface, "
964 "probe/read/erase/write may fail. ", size / 1024,
965 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +0000966 }
967 if (!limitexceeded)
968 return 0;
969 /* Sometimes chip and programmer have more than one bus in common,
970 * and the limit is not exceeded on all buses. Tell the user.
971 */
972 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +0000973 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +0000974 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +0000975 "interface which can support a chip of this size. "
976 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +0000977 return 1;
978}
979
hailfinger48ed3e22011-05-04 00:39:50 +0000980int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +0000981{
hailfinger48ed3e22011-05-04 00:39:50 +0000982 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +0000983 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +0000984 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +0000985 uint32_t size;
986 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +0000987 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +0000988
hailfinger48ed3e22011-05-04 00:39:50 +0000989 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +0000990 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +0000991 continue;
hailfingeraec9c962009-10-31 01:53:09 +0000992 buses_common = buses_supported & flash->bustype;
993 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +0000994 msg_gspew("Probing for %s %s, %d kB: skipped. ",
995 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +0000996 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +0000997 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +0000998 free(tmp);
999 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001000 msg_gspew("and chip bus type %s are incompatible.",
1001 tmp);
hailfingera916b422009-06-01 02:08:58 +00001002 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001003 msg_gspew("\n");
1004 continue;
1005 }
1006 msg_gdbg("Probing for %s %s, %d kB: ",
1007 flash->vendor, flash->name, flash->total_size);
1008 if (!flash->probe && !force) {
1009 msg_gdbg("failed! flashrom has no probe function for "
1010 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001011 continue;
1012 }
stepan782fb172007-04-06 11:58:03 +00001013
ollie5672ac62004-03-17 22:22:08 +00001014 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001015 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001016
hailfinger48ed3e22011-05-04 00:39:50 +00001017 /* Start filling in the dynamic data. */
1018 *fill_flash = *flash;
1019
hailfinger72d3b982009-05-09 07:27:23 +00001020 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001021 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001022
stugec1e55fe2008-07-02 17:15:47 +00001023 if (force)
1024 break;
stepanc98b80b2006-03-16 16:57:41 +00001025
hailfinger48ed3e22011-05-04 00:39:50 +00001026 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001027 goto notfound;
1028
hailfinger48ed3e22011-05-04 00:39:50 +00001029 /* If this is the first chip found, accept it.
1030 * If this is not the first chip found, accept it only if it is
1031 * a non-generic match.
1032 * We could either make chipcount global or provide it as
1033 * parameter, or we assume that startchip==0 means this call to
1034 * probe_flash() is the first one and thus no chip has been
1035 * found before.
1036 */
1037 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001038 break;
1039
stuge56300c32008-09-03 23:10:05 +00001040notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001041 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001042 }
uwebe4477b2007-08-23 16:08:21 +00001043
stugec1e55fe2008-07-02 17:15:47 +00001044 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001045 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001046
hailfingere11396b2011-03-08 00:09:11 +00001047#if CONFIG_INTERNAL == 1
1048 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001049 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001050 else
1051#endif
stepan3e7aeae2011-01-19 06:21:54 +00001052 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001053
stefanct588b6d22011-06-26 20:45:35 +00001054 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001055 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1056 force ? "Assuming" : "Found", fill_flash->vendor,
1057 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001058 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001059
hailfinger0f4c3952010-12-02 21:59:42 +00001060 /* Flash registers will not be mapped if the chip was forced. Lock info
1061 * may be stored in registers, so avoid lock info printing.
1062 */
1063 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001064 if (fill_flash->printlock)
1065 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001066
hailfinger48ed3e22011-05-04 00:39:50 +00001067 /* Return position of matching chip. */
1068 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001069}
1070
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001071int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001072{
hailfingerb0f4d122009-06-24 08:20:45 +00001073 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001074 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001075
snelsone42c3802010-05-07 20:09:04 +00001076 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001077
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001078 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1079 ret = handle_partial_verify(flash, buf, verify_range);
1080 } else {
1081 ret = verify_range(flash, buf, 0, total_size, NULL);
1082 }
uwef6641642007-05-09 10:17:44 +00001083
hailfingerb0f4d122009-06-24 08:20:45 +00001084 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001085 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001086
hailfingerb0f4d122009-06-24 08:20:45 +00001087 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001088}
1089
uwe8d342eb2011-07-28 08:13:25 +00001090int read_buf_from_file(unsigned char *buf, unsigned long size,
1091 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001092{
1093 unsigned long numbytes;
1094 FILE *image;
1095 struct stat image_stat;
1096
1097 if ((image = fopen(filename, "rb")) == NULL) {
1098 perror(filename);
1099 return 1;
1100 }
1101 if (fstat(fileno(image), &image_stat) != 0) {
1102 perror(filename);
1103 fclose(image);
1104 return 1;
1105 }
1106 if (image_stat.st_size != size) {
1107 msg_gerr("Error: Image size doesn't match\n");
1108 fclose(image);
1109 return 1;
1110 }
1111 numbytes = fread(buf, 1, size, image);
1112 if (fclose(image)) {
1113 perror(filename);
1114 return 1;
1115 }
1116 if (numbytes != size) {
1117 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1118 "wanted %ld!\n", numbytes, size);
1119 return 1;
1120 }
1121 return 0;
1122}
1123
uwe8d342eb2011-07-28 08:13:25 +00001124int write_buf_to_file(unsigned char *buf, unsigned long size,
1125 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001126{
1127 unsigned long numbytes;
1128 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001129
1130 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001131 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001132 return 1;
1133 }
oxygenebf70d352010-01-25 22:55:33 +00001134 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001135 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001136 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001137 }
hailfingerd219a232009-01-28 00:27:54 +00001138
hailfingerd219a232009-01-28 00:27:54 +00001139 numbytes = fwrite(buf, 1, size, image);
1140 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001141 if (numbytes != size) {
1142 msg_gerr("File %s could not be written completely.\n",
1143 filename);
hailfingerd219a232009-01-28 00:27:54 +00001144 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001145 }
hailfingerd219a232009-01-28 00:27:54 +00001146 return 0;
1147}
1148
stefanct52700282011-06-26 17:38:17 +00001149int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001150{
1151 unsigned long size = flash->total_size * 1024;
1152 unsigned char *buf = calloc(size, sizeof(char));
1153 int ret = 0;
1154
1155 msg_cinfo("Reading flash... ");
1156 if (!buf) {
1157 msg_gerr("Memory allocation failed!\n");
1158 msg_cinfo("FAILED.\n");
1159 return 1;
1160 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001161
1162 /* To support partial read, fill buffer to all 0xFF at beginning to make
1163 * debug easier. */
1164 memset(buf, 0xFF, size);
1165
hailfinger42a850a2010-07-13 23:56:13 +00001166 if (!flash->read) {
1167 msg_cerr("No read function available for this flash chip.\n");
1168 ret = 1;
1169 goto out_free;
1170 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001171
1172 /* First try to handle partial read case, rather than read the whole
1173 * flash, which is slow. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001174 ret = handle_partial_read(flash, buf, flash->read, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001175 if (ret < 0) {
1176 msg_cerr("Partial read operation failed!\n");
1177 ret = 1;
1178 goto out_free;
1179 } else if (ret > 0) {
1180 /* Partial read has been handled, pass the whole flash read. */
1181 } else if (flash->read(flash, buf, 0, size)) {
hailfinger42a850a2010-07-13 23:56:13 +00001182 msg_cerr("Read operation failed!\n");
1183 ret = 1;
1184 goto out_free;
1185 }
1186
stefanct9e6b98a2011-05-28 02:37:14 +00001187 ret = write_buf_to_file(buf, size, filename);
hailfinger42a850a2010-07-13 23:56:13 +00001188out_free:
1189 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001190 if (ret)
1191 msg_cerr("FAILED.");
1192 else
1193 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001194 return ret;
1195}
1196
hailfingerb437e282010-11-04 01:04:27 +00001197/* This function shares a lot of its structure with erase_and_write_flash() and
1198 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001199 * Even if an error is found, the function will keep going and check the rest.
1200 */
hailfinger48ed3e22011-05-04 00:39:50 +00001201static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001202{
hailfingerb91c08c2011-08-15 19:54:20 +00001203 int i, j, k;
1204 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001205
1206 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1207 unsigned int done = 0;
1208 struct block_eraser eraser = flash->block_erasers[k];
1209
1210 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1211 /* Blocks with zero size are bugs in flashchips.c. */
1212 if (eraser.eraseblocks[i].count &&
1213 !eraser.eraseblocks[i].size) {
1214 msg_gerr("ERROR: Flash chip %s erase function "
1215 "%i region %i has size 0. Please report"
1216 " a bug at flashrom@flashrom.org\n",
1217 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001218 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001219 }
1220 /* Blocks with zero count are bugs in flashchips.c. */
1221 if (!eraser.eraseblocks[i].count &&
1222 eraser.eraseblocks[i].size) {
1223 msg_gerr("ERROR: Flash chip %s erase function "
1224 "%i region %i has count 0. Please report"
1225 " a bug at flashrom@flashrom.org\n",
1226 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001227 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001228 }
1229 done += eraser.eraseblocks[i].count *
1230 eraser.eraseblocks[i].size;
1231 }
hailfinger9fed35d2010-01-19 06:42:46 +00001232 /* Empty eraseblock definition with erase function. */
1233 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001234 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001235 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001236 if (!done)
1237 continue;
1238 if (done != flash->total_size * 1024) {
1239 msg_gerr("ERROR: Flash chip %s erase function %i "
1240 "region walking resulted in 0x%06x bytes total,"
1241 " expected 0x%06x bytes. Please report a bug at"
1242 " flashrom@flashrom.org\n", flash->name, k,
1243 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001244 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001245 }
hailfinger9fed35d2010-01-19 06:42:46 +00001246 if (!eraser.block_erase)
1247 continue;
1248 /* Check if there are identical erase functions for different
1249 * layouts. That would imply "magic" erase functions. The
1250 * easiest way to check this is with function pointers.
1251 */
uwef6f94d42010-03-13 17:28:29 +00001252 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001253 if (eraser.block_erase ==
1254 flash->block_erasers[j].block_erase) {
1255 msg_gerr("ERROR: Flash chip %s erase function "
1256 "%i and %i are identical. Please report"
1257 " a bug at flashrom@flashrom.org\n",
1258 flash->name, k, j);
1259 ret = 1;
1260 }
uwef6f94d42010-03-13 17:28:29 +00001261 }
hailfinger45177872010-01-18 08:14:43 +00001262 }
hailfinger9fed35d2010-01-19 06:42:46 +00001263 return ret;
hailfinger45177872010-01-18 08:14:43 +00001264}
1265
hailfingerb437e282010-11-04 01:04:27 +00001266static int erase_and_write_block_helper(struct flashchip *flash,
1267 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001268 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001269 uint8_t *newcontents,
1270 int (*erasefn) (struct flashchip *flash,
1271 unsigned int addr,
1272 unsigned int len))
1273{
stefanctc5eb8a92011-11-23 09:13:48 +00001274 unsigned int starthere = 0, lenhere = 0;
1275 int ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001276 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1277
hailfinger90fcf9b2010-11-05 14:51:59 +00001278 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001279 * need to be adjusted here to keep the impression of proper abstraction
1280 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001281 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001282 newcontents += start;
1283 msg_cdbg(":");
1284 /* FIXME: Assume 256 byte granularity for now to play it safe. */
hailfinger90fcf9b2010-11-05 14:51:59 +00001285 if (need_erase(curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001286 msg_cdbg("E");
1287 ret = erasefn(flash, start, len);
1288 if (ret)
1289 return ret;
hailfingerac8e3182011-06-26 17:04:16 +00001290 if (check_erased_range(flash, start, len)) {
1291 msg_cerr("ERASE FAILED!\n");
1292 return -1;
1293 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001294 /* Erase was successful. Adjust curcontents. */
1295 memset(curcontents, 0xff, len);
hailfingerb437e282010-11-04 01:04:27 +00001296 skip = 0;
1297 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001298 /* get_next_write() sets starthere to a new value after the call. */
1299 while ((lenhere = get_next_write(curcontents + starthere,
1300 newcontents + starthere,
1301 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001302 if (!writecount++)
1303 msg_cdbg("W");
1304 /* Needs the partial write function signature. */
1305 ret = flash->write(flash, newcontents + starthere,
1306 start + starthere, lenhere);
1307 if (ret)
1308 return ret;
1309 starthere += lenhere;
1310 skip = 0;
1311 }
1312 if (skip)
1313 msg_cdbg("S");
1314 return ret;
1315}
1316
hailfinger83541b32010-07-13 00:42:00 +00001317static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1318 int (*do_something) (struct flashchip *flash,
1319 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001320 unsigned int len,
1321 uint8_t *param1,
1322 uint8_t *param2,
1323 int (*erasefn) (
1324 struct flashchip *flash,
1325 unsigned int addr,
1326 unsigned int len)),
1327 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001328{
1329 int i, j;
hailfingerb91c08c2011-08-15 19:54:20 +00001330 unsigned int start = 0;
1331 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001332 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001333
hailfinger2b8c9382010-07-13 00:37:19 +00001334 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1335 /* count==0 for all automatically initialized array
1336 * members so the loop below won't be executed for them.
1337 */
1338 len = eraser.eraseblocks[i].size;
1339 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001340 /* Print this for every block except the first one. */
1341 if (i || j)
1342 msg_cdbg(", ");
1343 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001344 start + len - 1);
hailfingerb437e282010-11-04 01:04:27 +00001345 if (do_something(flash, start, len, param1, param2,
1346 eraser.block_erase)) {
hailfinger2b8c9382010-07-13 00:37:19 +00001347 return 1;
hailfingerb437e282010-11-04 01:04:27 +00001348 }
hailfinger2b8c9382010-07-13 00:37:19 +00001349 start += len;
1350 }
1351 }
hailfingerb437e282010-11-04 01:04:27 +00001352 msg_cdbg("\n");
hailfinger2b8c9382010-07-13 00:37:19 +00001353 return 0;
1354}
1355
stefanct569dbb62011-07-01 00:19:12 +00001356static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001357{
1358 struct block_eraser eraser = flash->block_erasers[k];
1359
1360 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1361 if (log)
1362 msg_cdbg("not defined. ");
1363 return 1;
1364 }
1365 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1366 if (log)
1367 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001368 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001369 return 1;
1370 }
1371 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1372 if (log)
1373 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001374 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001375 return 1;
1376 }
1377 return 0;
1378}
1379
uwe8d342eb2011-07-28 08:13:25 +00001380int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1381 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001382{
hailfinger8f524a82011-07-21 21:21:04 +00001383 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001384 uint8_t *curcontents;
1385 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001386 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001387
hailfingercf848f12010-12-05 15:14:44 +00001388 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001389 curcontents = malloc(size);
1390 if (!curcontents) {
1391 msg_gerr("Out of memory!\n");
1392 exit(1);
1393 }
hailfingerb437e282010-11-04 01:04:27 +00001394 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1395 memcpy(curcontents, oldcontents, size);
1396
hailfinger7df21362009-09-05 02:30:58 +00001397 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001398 if (k != 0)
1399 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001400 if (!usable_erasefunctions) {
1401 msg_cdbg("No usable erase functions left.\n");
1402 break;
1403 }
stefanctf0bc4712011-07-26 14:28:35 +00001404 msg_cdbg("Trying erase function %i... ", k);
1405 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001406 continue;
hailfingercf848f12010-12-05 15:14:44 +00001407 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001408 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1409 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001410 /* If everything is OK, don't try another erase function. */
1411 if (!ret)
1412 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001413 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001414 * contents are. If no usable erase functions remain, we can
1415 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001416 */
hailfingercf848f12010-12-05 15:14:44 +00001417 if (!usable_erasefunctions)
1418 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001419 /* Reading the whole chip may take a while, inform the user even
1420 * in non-verbose mode.
1421 */
1422 msg_cinfo("Reading current flash chip contents... ");
hailfinger6237f5e2010-12-02 02:41:55 +00001423 if (flash->read(flash, curcontents, 0, size)) {
1424 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001425 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001426 /* We have no idea about the flash chip contents, so
1427 * retrying with another erase function is pointless.
1428 */
1429 break;
1430 }
David Hendricks89a45e52011-11-22 16:56:22 -08001431 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001432 }
hailfingerb437e282010-11-04 01:04:27 +00001433 /* Free the scratchpad. */
1434 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001435
hailfinger7df21362009-09-05 02:30:58 +00001436 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001437 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001438 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001439 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001440 }
1441 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001442}
1443
hailfinger4c47e9d2010-10-19 22:06:20 +00001444void nonfatal_help_message(void)
1445{
1446 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1447 "This means we have to add special support for your board, "
1448 "programmer or flash chip.\n"
1449 "Please report this on IRC at irc.freenode.net (channel "
1450 "#flashrom) or\n"
1451 "mail flashrom@flashrom.org!\n"
1452 "-------------------------------------------------------------"
1453 "------------------\n"
1454 "You may now reboot or simply leave the machine running.\n");
1455}
1456
uweb34ec9f2009-10-01 18:40:02 +00001457void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001458{
snelsone42c3802010-05-07 20:09:04 +00001459 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001460 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001461 "mail flashrom@flashrom.org with FAILED: your board name in "
1462 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001463 "-------------------------------------------------------------"
1464 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001465 "DO NOT REBOOT OR POWEROFF!\n");
1466}
1467
uwe8d342eb2011-07-28 08:13:25 +00001468/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001469void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001470{
1471 enum programmer p;
1472 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001473 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001474 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001475 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001476 }
snelsone42c3802010-05-07 20:09:04 +00001477 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001478}
1479
hailfingerf79d1712010-10-06 23:48:34 +00001480void list_programmers_linebreak(int startcol, int cols, int paren)
1481{
1482 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001483 int pnamelen;
1484 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001485 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001486 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001487
1488 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1489 pname = programmer_table[p].name;
1490 pnamelen = strlen(pname);
1491 if (remaining - pnamelen - 2 < 0) {
1492 if (firstline)
1493 firstline = 0;
1494 else
1495 printf("\n");
1496 for (i = 0; i < startcol; i++)
1497 printf(" ");
1498 remaining = cols - startcol;
1499 } else {
1500 printf(" ");
1501 remaining--;
1502 }
1503 if (paren && (p == 0)) {
1504 printf("(");
1505 remaining--;
1506 }
1507 printf("%s", pname);
1508 remaining -= pnamelen;
1509 if (p < PROGRAMMER_INVALID - 1) {
1510 printf(",");
1511 remaining--;
1512 } else {
1513 if (paren)
1514 printf(")");
1515 printf("\n");
1516 }
1517 }
1518}
1519
hailfinger3b471632010-03-27 16:36:40 +00001520void print_sysinfo(void)
1521{
David Hendricksc6c9f822010-11-03 15:07:01 -07001522 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001523#if HAVE_UTSNAME == 1
1524 struct utsname osinfo;
1525 uname(&osinfo);
1526
David Hendricksc6c9f822010-11-03 15:07:01 -07001527 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001528 osinfo.machine);
1529#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001530 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001531#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001532 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001533#if NEED_PCI == 1
1534#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001535 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001536#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001537 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001538#endif
1539#endif
1540#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001541 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001542#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001543 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001544#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001545 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001546#endif
hailfinger3b471632010-03-27 16:36:40 +00001547#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001548 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001549#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001550 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001551#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001552 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001553#endif
1554#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001555 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001556#endif
1557#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001558 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001559#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001560 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001561#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001562 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001563}
1564
uwefdeca092008-01-21 15:24:22 +00001565void print_version(void)
1566{
David Hendricksc6c9f822010-11-03 15:07:01 -07001567 /* send to stderr for chromium os */
1568 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001569 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001570}
1571
hailfinger74819ad2010-05-15 15:04:37 +00001572void print_banner(void)
1573{
1574 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001575 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001576 msg_ginfo("\n");
1577}
1578
hailfingerc77acb52009-12-24 02:15:55 +00001579int selfcheck(void)
1580{
hailfinger45177872010-01-18 08:14:43 +00001581 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001582 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001583
1584 /* Safety check. Instead of aborting after the first error, check
1585 * if more errors exist.
1586 */
hailfingerc77acb52009-12-24 02:15:55 +00001587 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001588 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001589 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001590 }
stefanct8632c922011-07-26 14:33:46 +00001591 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001592 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001593 * For 'flashchips' we check the first element to be non-null. In the
1594 * other cases there exist use cases where the first element can be
1595 * null. */
1596 if (flashchips == NULL || flashchips[0].vendor == NULL) {
1597 msg_gerr("Flashchips table miscompilation!\n");
1598 ret = 1;
1599 }
hailfinger45177872010-01-18 08:14:43 +00001600 for (flash = flashchips; flash && flash->name; flash++)
1601 if (selfcheck_eraseblocks(flash))
1602 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001603
1604#if CONFIG_INTERNAL == 1
1605 if (chipset_enables == NULL) {
1606 msg_gerr("Chipset enables table does not exist!\n");
1607 ret = 1;
1608 }
hailfinger4640bdb2011-08-31 16:19:50 +00001609 if (board_matches == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001610 msg_gerr("Board enables table does not exist!\n");
1611 ret = 1;
1612 }
1613 if (boards_known == NULL) {
1614 msg_gerr("Known boards table does not exist!\n");
1615 ret = 1;
1616 }
1617 if (laptops_known == NULL) {
1618 msg_gerr("Known laptops table does not exist!\n");
1619 ret = 1;
1620 }
uwe8d342eb2011-07-28 08:13:25 +00001621#endif
hailfinger45177872010-01-18 08:14:43 +00001622 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001623}
1624
hailfinger48ed3e22011-05-04 00:39:50 +00001625void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001626{
1627 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001628 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001629 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001630 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001631 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001632 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001633 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001634 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001635 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001636 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001637 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001638 msg_cdbg(" WRITE");
1639 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001640 }
1641 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1642 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1643 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1644 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001645 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001646 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001647 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001648 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001649 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001650 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001651 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001652 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001653 msg_cdbg(" WRITE");
1654 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001655 }
hailfinger92cd8e32010-01-07 03:24:05 +00001656 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001657 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001658 "in the latest development\n"
1659 "version of flashrom. If you are running the latest "
1660 "development version,\n"
1661 "please email a report to flashrom@flashrom.org if "
1662 "any of the above operations\n"
1663 "work correctly for you with this flash part. Please "
1664 "include the flashrom\n"
1665 "output with the additional -V option for all "
1666 "operations you tested (-V, -Vr,\n"
1667 "-Vw, -VE), and mention which mainboard or "
1668 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001669 "Please mention your board in the subject line. "
1670 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001671 }
1672}
1673
hailfinger771fc182010-10-15 00:01:14 +00001674/* FIXME: This function signature needs to be improved once doit() has a better
1675 * function signature.
1676 */
stefanct02116582011-05-18 01:30:56 +00001677int 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 +00001678{
1679 if (!programmer_may_write && (write_it || erase_it)) {
1680 msg_perr("Write/erase is not working yet on your programmer in "
1681 "its current configuration.\n");
1682 /* --force is the wrong approach, but it's the best we can do
1683 * until the generic programmer parameter parser is merged.
1684 */
1685 if (!force)
1686 return 1;
1687 msg_cerr("Continuing anyway.\n");
1688 }
1689
1690 if (read_it || erase_it || write_it || verify_it) {
1691 /* Everything needs read. */
1692 if (flash->tested & TEST_BAD_READ) {
1693 msg_cerr("Read is not working on this chip. ");
1694 if (!force)
1695 return 1;
1696 msg_cerr("Continuing anyway.\n");
1697 }
1698 if (!flash->read) {
1699 msg_cerr("flashrom has no read function for this "
1700 "flash chip.\n");
1701 return 1;
1702 }
1703 }
1704 if (erase_it || write_it) {
1705 /* Write needs erase. */
1706 if (flash->tested & TEST_BAD_ERASE) {
1707 msg_cerr("Erase is not working on this chip. ");
1708 if (!force)
1709 return 1;
1710 msg_cerr("Continuing anyway.\n");
1711 }
stefancte1c5acf2011-07-04 07:27:17 +00001712 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001713 msg_cerr("flashrom has no erase function for this "
1714 "flash chip.\n");
1715 return 1;
1716 }
hailfinger771fc182010-10-15 00:01:14 +00001717 }
1718 if (write_it) {
1719 if (flash->tested & TEST_BAD_WRITE) {
1720 msg_cerr("Write is not working on this chip. ");
1721 if (!force)
1722 return 1;
1723 msg_cerr("Continuing anyway.\n");
1724 }
1725 if (!flash->write) {
1726 msg_cerr("flashrom has no write function for this "
1727 "flash chip.\n");
1728 return 1;
1729 }
1730 }
1731 return 0;
1732}
1733
hailfingerc77acb52009-12-24 02:15:55 +00001734/* This function signature is horrible. We need to design a better interface,
1735 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001736 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001737 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001738int 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 +00001739{
hailfinger4c47e9d2010-10-19 22:06:20 +00001740 uint8_t *oldcontents;
1741 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001742 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001743 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001744
stefanct02116582011-05-18 01:30:56 +00001745 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001746 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001747 ret = 1;
1748 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001749 }
1750
hailfinger771fc182010-10-15 00:01:14 +00001751 /* Given the existence of read locks, we want to unlock for read,
1752 * erase and write.
1753 */
1754 if (flash->unlock)
1755 flash->unlock(flash);
1756
David Hendricks6d62d752011-03-07 21:20:22 -08001757 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001758 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001759 ret = 1;
1760 goto out_nofree;
1761 }
1762
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001763 /* mark entries included using -i argument as "included" if they are
1764 found in the master rom_entries list */
1765 if (process_include_args() < 0) {
1766 ret = 1;
1767 goto out_nofree;
1768 }
1769
hailfinger771fc182010-10-15 00:01:14 +00001770 if (read_it) {
1771 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001772 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001773 }
hailfingerb437e282010-11-04 01:04:27 +00001774
stefanctd611e8f2011-07-12 22:35:21 +00001775 oldcontents = malloc(size);
1776 if (!oldcontents) {
1777 msg_gerr("Out of memory!\n");
1778 exit(1);
1779 }
hailfingerb437e282010-11-04 01:04:27 +00001780 /* Assume worst case: All bits are 0. */
1781 memset(oldcontents, 0x00, size);
stefanctd611e8f2011-07-12 22:35:21 +00001782 newcontents = malloc(size);
1783 if (!newcontents) {
1784 msg_gerr("Out of memory!\n");
1785 exit(1);
1786 }
hailfingerb437e282010-11-04 01:04:27 +00001787 /* Assume best case: All bits should be 1. */
1788 memset(newcontents, 0xff, size);
1789 /* Side effect of the assumptions above: Default write action is erase
1790 * because newcontents looks like a completely erased chip, and
1791 * oldcontents being completely 0x00 means we have to erase everything
1792 * before we can write.
1793 */
1794
ollie0eb62d62004-12-08 20:10:01 +00001795 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001796 /* FIXME: Do we really want the scary warning if erase failed?
1797 * After all, after erase the chip is either blank or partially
1798 * blank or it has the old contents. A blank chip won't boot,
1799 * so if the user wanted erase and reboots afterwards, the user
1800 * knows very well that booting won't work.
1801 */
hailfingerb437e282010-11-04 01:04:27 +00001802 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001803 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001804 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001805 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001806 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001807 }
hailfinger771fc182010-10-15 00:01:14 +00001808
hailfingerd217d122010-10-08 18:52:29 +00001809 if (write_it || verify_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001810 if (read_buf_from_file(newcontents, size, filename)) {
hailfinger90fcf9b2010-11-05 14:51:59 +00001811 ret = 1;
1812 goto out;
stepan1da96c02006-11-21 23:48:51 +00001813 }
1814
hailfinger90c7d542010-05-31 15:27:27 +00001815#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001816 if (programmer == PROGRAMMER_INTERNAL)
1817 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001818#endif
ollie5672ac62004-03-17 22:22:08 +00001819 }
1820
David Hendricksc44d7a02011-10-17 11:28:43 -07001821 /* Obtain a reference image so that we can check whether regions need
1822 * to be erased and to give better diagnostics in case write fails.
hailfinger4c47e9d2010-10-19 22:06:20 +00001823 * The alternative would be to read only the regions which are to be
1824 * preserved, but in that case we might perform unneeded erase which
1825 * takes time as well.
1826 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001827 if (diff_file) {
1828 msg_cdbg("Reading old contents from file... ");
1829 if (read_buf_from_file(oldcontents, size, diff_file)) {
1830 ret = 1;
1831 msg_cdbg("FAILED.\n");
1832 goto out;
1833 }
1834 } else {
1835 msg_cdbg("Reading old contents from flash chip... ");
1836 if (flash->read(flash, oldcontents, 0, size)) {
1837 ret = 1;
1838 msg_cdbg("FAILED.\n");
1839 goto out;
1840 }
hailfinger4c47e9d2010-10-19 22:06:20 +00001841 }
David Hendricks89a45e52011-11-22 16:56:22 -08001842 msg_cdbg("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00001843
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001844 // This should be moved into each flash part's code to do it
ollie6a600992005-11-26 21:55:36 +00001845 // cleanly. This does the job.
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08001846 if (handle_romentries(flash, oldcontents, newcontents)) {
1847 ret = 1;
1848 msg_cerr("Cannot handle partial read.\n");
1849 goto out;
1850 }
uwef6641642007-05-09 10:17:44 +00001851
ollie6a600992005-11-26 21:55:36 +00001852 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00001853
stuge8ce3a3c2008-04-28 14:47:30 +00001854 if (write_it) {
hailfingerb437e282010-11-04 01:04:27 +00001855 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1856 msg_cerr("Uh oh. Erase/write failed. Checking if "
1857 "anything changed.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00001858 if (!flash->read(flash, newcontents, 0, size)) {
1859 if (!memcmp(oldcontents, newcontents, size)) {
1860 msg_cinfo("Good. It seems nothing was "
1861 "changed.\n");
1862 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00001863 ret = 1;
1864 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00001865 }
1866 }
hailfingerd217d122010-10-08 18:52:29 +00001867 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00001868 ret = 1;
1869 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00001870 }
stuge8ce3a3c2008-04-28 14:47:30 +00001871 }
ollie6a600992005-11-26 21:55:36 +00001872
hailfinger0459e1c2009-08-19 13:55:34 +00001873 if (verify_it) {
1874 /* Work around chips which need some time to calm down. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001875 if (write_it && verify_it != VERIFY_PARTIAL)
hailfinger0459e1c2009-08-19 13:55:34 +00001876 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001877
1878 ret = verify_flash(flash, newcontents, verify_it);
1879
hailfingera50d60e2009-11-17 09:57:34 +00001880 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00001881 * might have an emergency situation.
1882 */
1883 if (ret && write_it)
1884 emergency_help_message();
1885 }
ollie6a600992005-11-26 21:55:36 +00001886
hailfinger90fcf9b2010-11-05 14:51:59 +00001887out:
1888 free(oldcontents);
1889 free(newcontents);
1890out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07001891 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08001892 /*
1893 * programmer_shutdown() call is moved to cli_mfg() in chromium os
1894 * tree. This is because some operations, such as write protection,
1895 * requires programmer_shutdown() but does not call doit().
1896 */
1897// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00001898 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001899}