blob: 8b4fa91d4fe45ea573d7a4a909df995703c9a4dc [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
stefancta3cbe392011-09-18 22:42:18 +0000454int read_memmapped(struct flashchip *flash, uint8_t *buf, int start, int 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 */
577int check_erased_range(struct flashchip *flash, int start, int len)
578{
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 */
stefancta3cbe392011-09-18 22:42:18 +0000600int verify_range(struct flashchip *flash, uint8_t *cmpbuf, int start, int len,
uwe8d342eb2011-07-28 08:13:25 +0000601 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000602{
hailfingerb91c08c2011-08-15 19:54:20 +0000603 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 */
681int need_erase(uint8_t *have, uint8_t *want, int len, enum write_granularity gran)
682{
hailfingerb91c08c2011-08-15 19:54:20 +0000683 int result = 0;
684 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 */
747static int get_next_write(uint8_t *have, uint8_t *want, int len,
hailfinger90fcf9b2010-11-05 14:51:59 +0000748 int *first_start, enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000749{
hailfingerb91c08c2011-08-15 19:54:20 +0000750 int need_write = 0, rel_start = 0, first_len = 0;
751 int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000752
hailfingerb437e282010-11-04 01:04:27 +0000753 switch (gran) {
754 case write_gran_1bit:
755 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000756 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000757 break;
758 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000759 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000760 break;
761 default:
762 msg_cerr("%s: Unsupported granularity! Please report a bug at "
763 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000764 /* Claim that no write was needed. A write with unknown
765 * granularity is too dangerous to try.
766 */
767 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000768 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000769 for (i = 0; i < len / stride; i++) {
770 limit = min(stride, len - i * stride);
771 /* Are 'have' and 'want' identical? */
772 if (memcmp(have + i * stride, want + i * stride, limit)) {
773 if (!need_write) {
774 /* First location where have and want differ. */
775 need_write = 1;
776 rel_start = i * stride;
777 }
778 } else {
779 if (need_write) {
780 /* First location where have and want
781 * do not differ anymore.
782 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000783 break;
784 }
785 }
786 }
hailfingerffb7f382010-12-06 13:05:44 +0000787 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000788 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000789 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000790 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000791}
792
hailfinger0c515352009-11-23 12:55:31 +0000793/* This function generates various test patterns useful for testing controller
794 * and chip communication as well as chip behaviour.
795 *
796 * If a byte can be written multiple times, each time keeping 0-bits at 0
797 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
798 * is essentially an AND operation. That's also the reason why this function
799 * provides the result of AND between various patterns.
800 *
801 * Below is a list of patterns (and their block length).
802 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
803 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
804 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
805 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
806 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
807 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
808 * Pattern 6 is 00 (1 Byte)
809 * Pattern 7 is ff (1 Byte)
810 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
811 * byte block.
812 *
813 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
814 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
815 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
816 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
817 * Pattern 12 is 00 (1 Byte)
818 * Pattern 13 is ff (1 Byte)
819 * Patterns 8-13 have no block number.
820 *
821 * Patterns 0-3 are created to detect and efficiently diagnose communication
822 * slips like missed bits or bytes and their repetitive nature gives good visual
823 * cues to the person inspecting the results. In addition, the following holds:
824 * AND Pattern 0/1 == Pattern 4
825 * AND Pattern 2/3 == Pattern 5
826 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
827 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
828 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
829 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
830 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
831 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
832 * Besides that, they provide for bit testing of the last two bytes of every
833 * 256 byte block which contains the block number for patterns 0-6.
834 * Patterns 10-11 are special purpose for detecting subblock aliasing with
835 * block sizes >256 bytes (some Dataflash chips etc.)
836 * AND Pattern 8/9 == Pattern 12
837 * AND Pattern 10/11 == Pattern 12
838 * Pattern 13 is the completely erased state.
839 * None of the patterns can detect aliasing at boundaries which are a multiple
840 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
841 */
842int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
843{
844 int i;
845
846 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000847 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000848 return 1;
849 }
850
851 switch (variant) {
852 case 0:
853 for (i = 0; i < size; i++)
854 buf[i] = (i & 0xf) << 4 | 0x5;
855 break;
856 case 1:
857 for (i = 0; i < size; i++)
858 buf[i] = (i & 0xf) << 4 | 0xa;
859 break;
860 case 2:
861 for (i = 0; i < size; i++)
862 buf[i] = 0x50 | (i & 0xf);
863 break;
864 case 3:
865 for (i = 0; i < size; i++)
866 buf[i] = 0xa0 | (i & 0xf);
867 break;
868 case 4:
869 for (i = 0; i < size; i++)
870 buf[i] = (i & 0xf) << 4;
871 break;
872 case 5:
873 for (i = 0; i < size; i++)
874 buf[i] = i & 0xf;
875 break;
876 case 6:
877 memset(buf, 0x00, size);
878 break;
879 case 7:
880 memset(buf, 0xff, size);
881 break;
882 case 8:
883 for (i = 0; i < size; i++)
884 buf[i] = i & 0xff;
885 break;
886 case 9:
887 for (i = 0; i < size; i++)
888 buf[i] = ~(i & 0xff);
889 break;
890 case 10:
891 for (i = 0; i < size % 2; i++) {
892 buf[i * 2] = (i >> 8) & 0xff;
893 buf[i * 2 + 1] = i & 0xff;
894 }
895 if (size & 0x1)
896 buf[i * 2] = (i >> 8) & 0xff;
897 break;
898 case 11:
899 for (i = 0; i < size % 2; i++) {
900 buf[i * 2] = ~((i >> 8) & 0xff);
901 buf[i * 2 + 1] = ~(i & 0xff);
902 }
903 if (size & 0x1)
904 buf[i * 2] = ~((i >> 8) & 0xff);
905 break;
906 case 12:
907 memset(buf, 0x00, size);
908 break;
909 case 13:
910 memset(buf, 0xff, size);
911 break;
912 }
913
914 if ((variant >= 0) && (variant <= 7)) {
915 /* Write block number in the last two bytes of each 256-byte
916 * block, big endian for easier reading of the hexdump.
917 * Note that this wraps around for chips larger than 2^24 bytes
918 * (16 MB).
919 */
920 for (i = 0; i < size / 256; i++) {
921 buf[i * 256 + 254] = (i >> 8) & 0xff;
922 buf[i * 256 + 255] = i & 0xff;
923 }
924 }
925
926 return 0;
927}
928
hailfingeraec9c962009-10-31 01:53:09 +0000929int check_max_decode(enum chipbustype buses, uint32_t size)
930{
931 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +0000932
933 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000934 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000935 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000936 "size %u kB of chipset/board/programmer "
937 "for %s interface, "
938 "probe/read/erase/write may fail. ", size / 1024,
939 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +0000940 }
hailfingere1e41ea2011-07-27 07:13:06 +0000941 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000942 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000943 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000944 "size %u kB of chipset/board/programmer "
945 "for %s interface, "
946 "probe/read/erase/write may fail. ", size / 1024,
947 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +0000948 }
hailfingere1e41ea2011-07-27 07:13:06 +0000949 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000950 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000951 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000952 "size %u kB of chipset/board/programmer "
953 "for %s interface, "
954 "probe/read/erase/write may fail. ", size / 1024,
955 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +0000956 }
hailfingere1e41ea2011-07-27 07:13:06 +0000957 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000958 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000959 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000960 "size %u kB of chipset/board/programmer "
961 "for %s interface, "
962 "probe/read/erase/write may fail. ", size / 1024,
963 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +0000964 }
965 if (!limitexceeded)
966 return 0;
967 /* Sometimes chip and programmer have more than one bus in common,
968 * and the limit is not exceeded on all buses. Tell the user.
969 */
970 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +0000971 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +0000972 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +0000973 "interface which can support a chip of this size. "
974 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +0000975 return 1;
976}
977
hailfinger48ed3e22011-05-04 00:39:50 +0000978int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +0000979{
hailfinger48ed3e22011-05-04 00:39:50 +0000980 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +0000981 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +0000982 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +0000983 uint32_t size;
984 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +0000985 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +0000986
hailfinger48ed3e22011-05-04 00:39:50 +0000987 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +0000988 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +0000989 continue;
hailfingeraec9c962009-10-31 01:53:09 +0000990 buses_common = buses_supported & flash->bustype;
991 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +0000992 msg_gspew("Probing for %s %s, %d kB: skipped. ",
993 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +0000994 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +0000995 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +0000996 free(tmp);
997 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +0000998 msg_gspew("and chip bus type %s are incompatible.",
999 tmp);
hailfingera916b422009-06-01 02:08:58 +00001000 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001001 msg_gspew("\n");
1002 continue;
1003 }
1004 msg_gdbg("Probing for %s %s, %d kB: ",
1005 flash->vendor, flash->name, flash->total_size);
1006 if (!flash->probe && !force) {
1007 msg_gdbg("failed! flashrom has no probe function for "
1008 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001009 continue;
1010 }
stepan782fb172007-04-06 11:58:03 +00001011
ollie5672ac62004-03-17 22:22:08 +00001012 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001013 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001014
hailfinger48ed3e22011-05-04 00:39:50 +00001015 /* Start filling in the dynamic data. */
1016 *fill_flash = *flash;
1017
hailfinger72d3b982009-05-09 07:27:23 +00001018 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001019 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001020
stugec1e55fe2008-07-02 17:15:47 +00001021 if (force)
1022 break;
stepanc98b80b2006-03-16 16:57:41 +00001023
hailfinger48ed3e22011-05-04 00:39:50 +00001024 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001025 goto notfound;
1026
hailfinger48ed3e22011-05-04 00:39:50 +00001027 /* If this is the first chip found, accept it.
1028 * If this is not the first chip found, accept it only if it is
1029 * a non-generic match.
1030 * We could either make chipcount global or provide it as
1031 * parameter, or we assume that startchip==0 means this call to
1032 * probe_flash() is the first one and thus no chip has been
1033 * found before.
1034 */
1035 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001036 break;
1037
stuge56300c32008-09-03 23:10:05 +00001038notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001039 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001040 }
uwebe4477b2007-08-23 16:08:21 +00001041
stugec1e55fe2008-07-02 17:15:47 +00001042 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001043 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001044
hailfingere11396b2011-03-08 00:09:11 +00001045#if CONFIG_INTERNAL == 1
1046 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001047 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001048 else
1049#endif
stepan3e7aeae2011-01-19 06:21:54 +00001050 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001051
stefanct588b6d22011-06-26 20:45:35 +00001052 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001053 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1054 force ? "Assuming" : "Found", fill_flash->vendor,
1055 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001056 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001057
hailfinger0f4c3952010-12-02 21:59:42 +00001058 /* Flash registers will not be mapped if the chip was forced. Lock info
1059 * may be stored in registers, so avoid lock info printing.
1060 */
1061 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001062 if (fill_flash->printlock)
1063 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001064
hailfinger48ed3e22011-05-04 00:39:50 +00001065 /* Return position of matching chip. */
1066 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001067}
1068
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001069int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001070{
hailfingerb0f4d122009-06-24 08:20:45 +00001071 int ret;
ollie5b621572004-03-20 16:46:10 +00001072 int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001073
snelsone42c3802010-05-07 20:09:04 +00001074 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001075
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001076 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1077 ret = handle_partial_verify(flash, buf, verify_range);
1078 } else {
1079 ret = verify_range(flash, buf, 0, total_size, NULL);
1080 }
uwef6641642007-05-09 10:17:44 +00001081
hailfingerb0f4d122009-06-24 08:20:45 +00001082 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001083 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001084
hailfingerb0f4d122009-06-24 08:20:45 +00001085 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001086}
1087
uwe8d342eb2011-07-28 08:13:25 +00001088int read_buf_from_file(unsigned char *buf, unsigned long size,
1089 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001090{
1091 unsigned long numbytes;
1092 FILE *image;
1093 struct stat image_stat;
1094
1095 if ((image = fopen(filename, "rb")) == NULL) {
1096 perror(filename);
1097 return 1;
1098 }
1099 if (fstat(fileno(image), &image_stat) != 0) {
1100 perror(filename);
1101 fclose(image);
1102 return 1;
1103 }
1104 if (image_stat.st_size != size) {
1105 msg_gerr("Error: Image size doesn't match\n");
1106 fclose(image);
1107 return 1;
1108 }
1109 numbytes = fread(buf, 1, size, image);
1110 if (fclose(image)) {
1111 perror(filename);
1112 return 1;
1113 }
1114 if (numbytes != size) {
1115 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1116 "wanted %ld!\n", numbytes, size);
1117 return 1;
1118 }
1119 return 0;
1120}
1121
uwe8d342eb2011-07-28 08:13:25 +00001122int write_buf_to_file(unsigned char *buf, unsigned long size,
1123 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001124{
1125 unsigned long numbytes;
1126 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001127
1128 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001129 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001130 return 1;
1131 }
oxygenebf70d352010-01-25 22:55:33 +00001132 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001133 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001134 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001135 }
hailfingerd219a232009-01-28 00:27:54 +00001136
hailfingerd219a232009-01-28 00:27:54 +00001137 numbytes = fwrite(buf, 1, size, image);
1138 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001139 if (numbytes != size) {
1140 msg_gerr("File %s could not be written completely.\n",
1141 filename);
hailfingerd219a232009-01-28 00:27:54 +00001142 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001143 }
hailfingerd219a232009-01-28 00:27:54 +00001144 return 0;
1145}
1146
stefanct52700282011-06-26 17:38:17 +00001147int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001148{
1149 unsigned long size = flash->total_size * 1024;
1150 unsigned char *buf = calloc(size, sizeof(char));
1151 int ret = 0;
1152
1153 msg_cinfo("Reading flash... ");
1154 if (!buf) {
1155 msg_gerr("Memory allocation failed!\n");
1156 msg_cinfo("FAILED.\n");
1157 return 1;
1158 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001159
1160 /* To support partial read, fill buffer to all 0xFF at beginning to make
1161 * debug easier. */
1162 memset(buf, 0xFF, size);
1163
hailfinger42a850a2010-07-13 23:56:13 +00001164 if (!flash->read) {
1165 msg_cerr("No read function available for this flash chip.\n");
1166 ret = 1;
1167 goto out_free;
1168 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001169
1170 /* First try to handle partial read case, rather than read the whole
1171 * flash, which is slow. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001172 ret = handle_partial_read(flash, buf, flash->read, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001173 if (ret < 0) {
1174 msg_cerr("Partial read operation failed!\n");
1175 ret = 1;
1176 goto out_free;
1177 } else if (ret > 0) {
1178 /* Partial read has been handled, pass the whole flash read. */
1179 } else if (flash->read(flash, buf, 0, size)) {
hailfinger42a850a2010-07-13 23:56:13 +00001180 msg_cerr("Read operation failed!\n");
1181 ret = 1;
1182 goto out_free;
1183 }
1184
stefanct9e6b98a2011-05-28 02:37:14 +00001185 ret = write_buf_to_file(buf, size, filename);
hailfinger42a850a2010-07-13 23:56:13 +00001186out_free:
1187 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001188 if (ret)
1189 msg_cerr("FAILED.");
1190 else
1191 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001192 return ret;
1193}
1194
hailfingerb437e282010-11-04 01:04:27 +00001195/* This function shares a lot of its structure with erase_and_write_flash() and
1196 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001197 * Even if an error is found, the function will keep going and check the rest.
1198 */
hailfinger48ed3e22011-05-04 00:39:50 +00001199static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001200{
hailfingerb91c08c2011-08-15 19:54:20 +00001201 int i, j, k;
1202 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001203
1204 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1205 unsigned int done = 0;
1206 struct block_eraser eraser = flash->block_erasers[k];
1207
1208 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1209 /* Blocks with zero size are bugs in flashchips.c. */
1210 if (eraser.eraseblocks[i].count &&
1211 !eraser.eraseblocks[i].size) {
1212 msg_gerr("ERROR: Flash chip %s erase function "
1213 "%i region %i has size 0. Please report"
1214 " a bug at flashrom@flashrom.org\n",
1215 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001216 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001217 }
1218 /* Blocks with zero count are bugs in flashchips.c. */
1219 if (!eraser.eraseblocks[i].count &&
1220 eraser.eraseblocks[i].size) {
1221 msg_gerr("ERROR: Flash chip %s erase function "
1222 "%i region %i has count 0. Please report"
1223 " a bug at flashrom@flashrom.org\n",
1224 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001225 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001226 }
1227 done += eraser.eraseblocks[i].count *
1228 eraser.eraseblocks[i].size;
1229 }
hailfinger9fed35d2010-01-19 06:42:46 +00001230 /* Empty eraseblock definition with erase function. */
1231 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001232 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001233 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001234 if (!done)
1235 continue;
1236 if (done != flash->total_size * 1024) {
1237 msg_gerr("ERROR: Flash chip %s erase function %i "
1238 "region walking resulted in 0x%06x bytes total,"
1239 " expected 0x%06x bytes. Please report a bug at"
1240 " flashrom@flashrom.org\n", flash->name, k,
1241 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001242 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001243 }
hailfinger9fed35d2010-01-19 06:42:46 +00001244 if (!eraser.block_erase)
1245 continue;
1246 /* Check if there are identical erase functions for different
1247 * layouts. That would imply "magic" erase functions. The
1248 * easiest way to check this is with function pointers.
1249 */
uwef6f94d42010-03-13 17:28:29 +00001250 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001251 if (eraser.block_erase ==
1252 flash->block_erasers[j].block_erase) {
1253 msg_gerr("ERROR: Flash chip %s erase function "
1254 "%i and %i are identical. Please report"
1255 " a bug at flashrom@flashrom.org\n",
1256 flash->name, k, j);
1257 ret = 1;
1258 }
uwef6f94d42010-03-13 17:28:29 +00001259 }
hailfinger45177872010-01-18 08:14:43 +00001260 }
hailfinger9fed35d2010-01-19 06:42:46 +00001261 return ret;
hailfinger45177872010-01-18 08:14:43 +00001262}
1263
hailfingerb437e282010-11-04 01:04:27 +00001264static int erase_and_write_block_helper(struct flashchip *flash,
1265 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001266 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001267 uint8_t *newcontents,
1268 int (*erasefn) (struct flashchip *flash,
1269 unsigned int addr,
1270 unsigned int len))
1271{
uwe8d342eb2011-07-28 08:13:25 +00001272 int starthere = 0, lenhere = 0, ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001273 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1274
hailfinger90fcf9b2010-11-05 14:51:59 +00001275 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001276 * need to be adjusted here to keep the impression of proper abstraction
1277 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001278 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001279 newcontents += start;
1280 msg_cdbg(":");
1281 /* FIXME: Assume 256 byte granularity for now to play it safe. */
hailfinger90fcf9b2010-11-05 14:51:59 +00001282 if (need_erase(curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001283 msg_cdbg("E");
1284 ret = erasefn(flash, start, len);
1285 if (ret)
1286 return ret;
hailfingerac8e3182011-06-26 17:04:16 +00001287 if (check_erased_range(flash, start, len)) {
1288 msg_cerr("ERASE FAILED!\n");
1289 return -1;
1290 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001291 /* Erase was successful. Adjust curcontents. */
1292 memset(curcontents, 0xff, len);
hailfingerb437e282010-11-04 01:04:27 +00001293 skip = 0;
1294 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001295 /* get_next_write() sets starthere to a new value after the call. */
1296 while ((lenhere = get_next_write(curcontents + starthere,
1297 newcontents + starthere,
1298 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001299 if (!writecount++)
1300 msg_cdbg("W");
1301 /* Needs the partial write function signature. */
1302 ret = flash->write(flash, newcontents + starthere,
1303 start + starthere, lenhere);
1304 if (ret)
1305 return ret;
1306 starthere += lenhere;
1307 skip = 0;
1308 }
1309 if (skip)
1310 msg_cdbg("S");
1311 return ret;
1312}
1313
hailfinger83541b32010-07-13 00:42:00 +00001314static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1315 int (*do_something) (struct flashchip *flash,
1316 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001317 unsigned int len,
1318 uint8_t *param1,
1319 uint8_t *param2,
1320 int (*erasefn) (
1321 struct flashchip *flash,
1322 unsigned int addr,
1323 unsigned int len)),
1324 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001325{
1326 int i, j;
hailfingerb91c08c2011-08-15 19:54:20 +00001327 unsigned int start = 0;
1328 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001329 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001330
hailfinger2b8c9382010-07-13 00:37:19 +00001331 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1332 /* count==0 for all automatically initialized array
1333 * members so the loop below won't be executed for them.
1334 */
1335 len = eraser.eraseblocks[i].size;
1336 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001337 /* Print this for every block except the first one. */
1338 if (i || j)
1339 msg_cdbg(", ");
1340 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001341 start + len - 1);
hailfingerb437e282010-11-04 01:04:27 +00001342 if (do_something(flash, start, len, param1, param2,
1343 eraser.block_erase)) {
hailfinger2b8c9382010-07-13 00:37:19 +00001344 return 1;
hailfingerb437e282010-11-04 01:04:27 +00001345 }
hailfinger2b8c9382010-07-13 00:37:19 +00001346 start += len;
1347 }
1348 }
hailfingerb437e282010-11-04 01:04:27 +00001349 msg_cdbg("\n");
hailfinger2b8c9382010-07-13 00:37:19 +00001350 return 0;
1351}
1352
stefanct569dbb62011-07-01 00:19:12 +00001353static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001354{
1355 struct block_eraser eraser = flash->block_erasers[k];
1356
1357 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1358 if (log)
1359 msg_cdbg("not defined. ");
1360 return 1;
1361 }
1362 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1363 if (log)
1364 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001365 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001366 return 1;
1367 }
1368 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1369 if (log)
1370 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001371 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001372 return 1;
1373 }
1374 return 0;
1375}
1376
uwe8d342eb2011-07-28 08:13:25 +00001377int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1378 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001379{
hailfinger8f524a82011-07-21 21:21:04 +00001380 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001381 uint8_t *curcontents;
1382 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001383 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001384
hailfingercf848f12010-12-05 15:14:44 +00001385 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001386 curcontents = malloc(size);
1387 if (!curcontents) {
1388 msg_gerr("Out of memory!\n");
1389 exit(1);
1390 }
hailfingerb437e282010-11-04 01:04:27 +00001391 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1392 memcpy(curcontents, oldcontents, size);
1393
hailfinger7df21362009-09-05 02:30:58 +00001394 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001395 if (k != 0)
1396 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001397 if (!usable_erasefunctions) {
1398 msg_cdbg("No usable erase functions left.\n");
1399 break;
1400 }
stefanctf0bc4712011-07-26 14:28:35 +00001401 msg_cdbg("Trying erase function %i... ", k);
1402 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001403 continue;
hailfingercf848f12010-12-05 15:14:44 +00001404 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001405 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1406 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001407 /* If everything is OK, don't try another erase function. */
1408 if (!ret)
1409 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001410 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001411 * contents are. If no usable erase functions remain, we can
1412 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001413 */
hailfingercf848f12010-12-05 15:14:44 +00001414 if (!usable_erasefunctions)
1415 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001416 /* Reading the whole chip may take a while, inform the user even
1417 * in non-verbose mode.
1418 */
1419 msg_cinfo("Reading current flash chip contents... ");
hailfinger6237f5e2010-12-02 02:41:55 +00001420 if (flash->read(flash, curcontents, 0, size)) {
1421 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001422 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001423 /* We have no idea about the flash chip contents, so
1424 * retrying with another erase function is pointless.
1425 */
1426 break;
1427 }
stefanctf0bc4712011-07-26 14:28:35 +00001428 msg_cinfo("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001429 }
hailfingerb437e282010-11-04 01:04:27 +00001430 /* Free the scratchpad. */
1431 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001432
hailfinger7df21362009-09-05 02:30:58 +00001433 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001434 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001435 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001436 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001437 }
1438 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001439}
1440
hailfinger4c47e9d2010-10-19 22:06:20 +00001441void nonfatal_help_message(void)
1442{
1443 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1444 "This means we have to add special support for your board, "
1445 "programmer or flash chip.\n"
1446 "Please report this on IRC at irc.freenode.net (channel "
1447 "#flashrom) or\n"
1448 "mail flashrom@flashrom.org!\n"
1449 "-------------------------------------------------------------"
1450 "------------------\n"
1451 "You may now reboot or simply leave the machine running.\n");
1452}
1453
uweb34ec9f2009-10-01 18:40:02 +00001454void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001455{
snelsone42c3802010-05-07 20:09:04 +00001456 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001457 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001458 "mail flashrom@flashrom.org with FAILED: your board name in "
1459 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001460 "-------------------------------------------------------------"
1461 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001462 "DO NOT REBOOT OR POWEROFF!\n");
1463}
1464
uwe8d342eb2011-07-28 08:13:25 +00001465/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001466void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001467{
1468 enum programmer p;
1469 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001470 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001471 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001472 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001473 }
snelsone42c3802010-05-07 20:09:04 +00001474 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001475}
1476
hailfingerf79d1712010-10-06 23:48:34 +00001477void list_programmers_linebreak(int startcol, int cols, int paren)
1478{
1479 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001480 int pnamelen;
1481 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001482 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001483 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001484
1485 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1486 pname = programmer_table[p].name;
1487 pnamelen = strlen(pname);
1488 if (remaining - pnamelen - 2 < 0) {
1489 if (firstline)
1490 firstline = 0;
1491 else
1492 printf("\n");
1493 for (i = 0; i < startcol; i++)
1494 printf(" ");
1495 remaining = cols - startcol;
1496 } else {
1497 printf(" ");
1498 remaining--;
1499 }
1500 if (paren && (p == 0)) {
1501 printf("(");
1502 remaining--;
1503 }
1504 printf("%s", pname);
1505 remaining -= pnamelen;
1506 if (p < PROGRAMMER_INVALID - 1) {
1507 printf(",");
1508 remaining--;
1509 } else {
1510 if (paren)
1511 printf(")");
1512 printf("\n");
1513 }
1514 }
1515}
1516
hailfinger3b471632010-03-27 16:36:40 +00001517void print_sysinfo(void)
1518{
David Hendricksc6c9f822010-11-03 15:07:01 -07001519 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001520#if HAVE_UTSNAME == 1
1521 struct utsname osinfo;
1522 uname(&osinfo);
1523
David Hendricksc6c9f822010-11-03 15:07:01 -07001524 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001525 osinfo.machine);
1526#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001527 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001528#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001529 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001530#if NEED_PCI == 1
1531#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001532 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001533#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001534 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001535#endif
1536#endif
1537#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001538 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001539#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001540 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001541#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001542 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001543#endif
hailfinger3b471632010-03-27 16:36:40 +00001544#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001545 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001546#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001547 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001548#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001549 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001550#endif
1551#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001552 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001553#endif
1554#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001555 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001556#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001557 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001558#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001559 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001560}
1561
uwefdeca092008-01-21 15:24:22 +00001562void print_version(void)
1563{
David Hendricksc6c9f822010-11-03 15:07:01 -07001564 /* send to stderr for chromium os */
1565 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001566 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001567}
1568
hailfinger74819ad2010-05-15 15:04:37 +00001569void print_banner(void)
1570{
1571 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001572 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001573 msg_ginfo("\n");
1574}
1575
hailfingerc77acb52009-12-24 02:15:55 +00001576int selfcheck(void)
1577{
hailfinger45177872010-01-18 08:14:43 +00001578 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001579 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001580
1581 /* Safety check. Instead of aborting after the first error, check
1582 * if more errors exist.
1583 */
hailfingerc77acb52009-12-24 02:15:55 +00001584 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001585 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001586 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001587 }
stefanct8632c922011-07-26 14:33:46 +00001588 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001589 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001590 * For 'flashchips' we check the first element to be non-null. In the
1591 * other cases there exist use cases where the first element can be
1592 * null. */
1593 if (flashchips == NULL || flashchips[0].vendor == NULL) {
1594 msg_gerr("Flashchips table miscompilation!\n");
1595 ret = 1;
1596 }
hailfinger45177872010-01-18 08:14:43 +00001597 for (flash = flashchips; flash && flash->name; flash++)
1598 if (selfcheck_eraseblocks(flash))
1599 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001600
1601#if CONFIG_INTERNAL == 1
1602 if (chipset_enables == NULL) {
1603 msg_gerr("Chipset enables table does not exist!\n");
1604 ret = 1;
1605 }
hailfinger4640bdb2011-08-31 16:19:50 +00001606 if (board_matches == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001607 msg_gerr("Board enables table does not exist!\n");
1608 ret = 1;
1609 }
1610 if (boards_known == NULL) {
1611 msg_gerr("Known boards table does not exist!\n");
1612 ret = 1;
1613 }
1614 if (laptops_known == NULL) {
1615 msg_gerr("Known laptops table does not exist!\n");
1616 ret = 1;
1617 }
uwe8d342eb2011-07-28 08:13:25 +00001618#endif
hailfinger45177872010-01-18 08:14:43 +00001619 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001620}
1621
hailfinger48ed3e22011-05-04 00:39:50 +00001622void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001623{
1624 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001625 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001626 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001627 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001628 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001629 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001630 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001631 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001632 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001633 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001634 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001635 msg_cdbg(" WRITE");
1636 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001637 }
1638 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1639 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1640 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1641 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001642 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001643 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001644 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001645 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001646 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001647 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001648 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001649 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001650 msg_cdbg(" WRITE");
1651 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001652 }
hailfinger92cd8e32010-01-07 03:24:05 +00001653 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001654 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001655 "in the latest development\n"
1656 "version of flashrom. If you are running the latest "
1657 "development version,\n"
1658 "please email a report to flashrom@flashrom.org if "
1659 "any of the above operations\n"
1660 "work correctly for you with this flash part. Please "
1661 "include the flashrom\n"
1662 "output with the additional -V option for all "
1663 "operations you tested (-V, -Vr,\n"
1664 "-Vw, -VE), and mention which mainboard or "
1665 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001666 "Please mention your board in the subject line. "
1667 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001668 }
1669}
1670
hailfinger771fc182010-10-15 00:01:14 +00001671/* FIXME: This function signature needs to be improved once doit() has a better
1672 * function signature.
1673 */
stefanct02116582011-05-18 01:30:56 +00001674int 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 +00001675{
1676 if (!programmer_may_write && (write_it || erase_it)) {
1677 msg_perr("Write/erase is not working yet on your programmer in "
1678 "its current configuration.\n");
1679 /* --force is the wrong approach, but it's the best we can do
1680 * until the generic programmer parameter parser is merged.
1681 */
1682 if (!force)
1683 return 1;
1684 msg_cerr("Continuing anyway.\n");
1685 }
1686
1687 if (read_it || erase_it || write_it || verify_it) {
1688 /* Everything needs read. */
1689 if (flash->tested & TEST_BAD_READ) {
1690 msg_cerr("Read is not working on this chip. ");
1691 if (!force)
1692 return 1;
1693 msg_cerr("Continuing anyway.\n");
1694 }
1695 if (!flash->read) {
1696 msg_cerr("flashrom has no read function for this "
1697 "flash chip.\n");
1698 return 1;
1699 }
1700 }
1701 if (erase_it || write_it) {
1702 /* Write needs erase. */
1703 if (flash->tested & TEST_BAD_ERASE) {
1704 msg_cerr("Erase is not working on this chip. ");
1705 if (!force)
1706 return 1;
1707 msg_cerr("Continuing anyway.\n");
1708 }
stefancte1c5acf2011-07-04 07:27:17 +00001709 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001710 msg_cerr("flashrom has no erase function for this "
1711 "flash chip.\n");
1712 return 1;
1713 }
hailfinger771fc182010-10-15 00:01:14 +00001714 }
1715 if (write_it) {
1716 if (flash->tested & TEST_BAD_WRITE) {
1717 msg_cerr("Write is not working on this chip. ");
1718 if (!force)
1719 return 1;
1720 msg_cerr("Continuing anyway.\n");
1721 }
1722 if (!flash->write) {
1723 msg_cerr("flashrom has no write function for this "
1724 "flash chip.\n");
1725 return 1;
1726 }
1727 }
1728 return 0;
1729}
1730
hailfingerc77acb52009-12-24 02:15:55 +00001731/* This function signature is horrible. We need to design a better interface,
1732 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001733 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001734 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001735int 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 +00001736{
hailfinger4c47e9d2010-10-19 22:06:20 +00001737 uint8_t *oldcontents;
1738 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001739 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001740 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001741
stefanct02116582011-05-18 01:30:56 +00001742 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001743 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001744 ret = 1;
1745 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001746 }
1747
hailfinger771fc182010-10-15 00:01:14 +00001748 /* Given the existence of read locks, we want to unlock for read,
1749 * erase and write.
1750 */
1751 if (flash->unlock)
1752 flash->unlock(flash);
1753
David Hendricks6d62d752011-03-07 21:20:22 -08001754 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001755 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001756 ret = 1;
1757 goto out_nofree;
1758 }
1759
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001760 /* mark entries included using -i argument as "included" if they are
1761 found in the master rom_entries list */
1762 if (process_include_args() < 0) {
1763 ret = 1;
1764 goto out_nofree;
1765 }
1766
hailfinger771fc182010-10-15 00:01:14 +00001767 if (read_it) {
1768 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001769 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001770 }
hailfingerb437e282010-11-04 01:04:27 +00001771
stefanctd611e8f2011-07-12 22:35:21 +00001772 oldcontents = malloc(size);
1773 if (!oldcontents) {
1774 msg_gerr("Out of memory!\n");
1775 exit(1);
1776 }
hailfingerb437e282010-11-04 01:04:27 +00001777 /* Assume worst case: All bits are 0. */
1778 memset(oldcontents, 0x00, size);
stefanctd611e8f2011-07-12 22:35:21 +00001779 newcontents = malloc(size);
1780 if (!newcontents) {
1781 msg_gerr("Out of memory!\n");
1782 exit(1);
1783 }
hailfingerb437e282010-11-04 01:04:27 +00001784 /* Assume best case: All bits should be 1. */
1785 memset(newcontents, 0xff, size);
1786 /* Side effect of the assumptions above: Default write action is erase
1787 * because newcontents looks like a completely erased chip, and
1788 * oldcontents being completely 0x00 means we have to erase everything
1789 * before we can write.
1790 */
1791
ollie0eb62d62004-12-08 20:10:01 +00001792 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001793 /* FIXME: Do we really want the scary warning if erase failed?
1794 * After all, after erase the chip is either blank or partially
1795 * blank or it has the old contents. A blank chip won't boot,
1796 * so if the user wanted erase and reboots afterwards, the user
1797 * knows very well that booting won't work.
1798 */
hailfingerb437e282010-11-04 01:04:27 +00001799 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001800 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001801 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001802 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001803 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001804 }
hailfinger771fc182010-10-15 00:01:14 +00001805
hailfingerd217d122010-10-08 18:52:29 +00001806 if (write_it || verify_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001807 if (read_buf_from_file(newcontents, size, filename)) {
hailfinger90fcf9b2010-11-05 14:51:59 +00001808 ret = 1;
1809 goto out;
stepan1da96c02006-11-21 23:48:51 +00001810 }
1811
hailfinger90c7d542010-05-31 15:27:27 +00001812#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001813 if (programmer == PROGRAMMER_INTERNAL)
1814 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001815#endif
ollie5672ac62004-03-17 22:22:08 +00001816 }
1817
David Hendricksc44d7a02011-10-17 11:28:43 -07001818 /* Obtain a reference image so that we can check whether regions need
1819 * to be erased and to give better diagnostics in case write fails.
hailfinger4c47e9d2010-10-19 22:06:20 +00001820 * The alternative would be to read only the regions which are to be
1821 * preserved, but in that case we might perform unneeded erase which
1822 * takes time as well.
1823 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001824 if (diff_file) {
1825 msg_cdbg("Reading old contents from file... ");
1826 if (read_buf_from_file(oldcontents, size, diff_file)) {
1827 ret = 1;
1828 msg_cdbg("FAILED.\n");
1829 goto out;
1830 }
1831 } else {
1832 msg_cdbg("Reading old contents from flash chip... ");
1833 if (flash->read(flash, oldcontents, 0, size)) {
1834 ret = 1;
1835 msg_cdbg("FAILED.\n");
1836 goto out;
1837 }
hailfinger4c47e9d2010-10-19 22:06:20 +00001838 }
stefanctf0bc4712011-07-26 14:28:35 +00001839 msg_cinfo("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00001840
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001841 // This should be moved into each flash part's code to do it
ollie6a600992005-11-26 21:55:36 +00001842 // cleanly. This does the job.
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08001843 if (handle_romentries(flash, oldcontents, newcontents)) {
1844 ret = 1;
1845 msg_cerr("Cannot handle partial read.\n");
1846 goto out;
1847 }
uwef6641642007-05-09 10:17:44 +00001848
ollie6a600992005-11-26 21:55:36 +00001849 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00001850
stuge8ce3a3c2008-04-28 14:47:30 +00001851 if (write_it) {
hailfingerb437e282010-11-04 01:04:27 +00001852 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1853 msg_cerr("Uh oh. Erase/write failed. Checking if "
1854 "anything changed.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00001855 if (!flash->read(flash, newcontents, 0, size)) {
1856 if (!memcmp(oldcontents, newcontents, size)) {
1857 msg_cinfo("Good. It seems nothing was "
1858 "changed.\n");
1859 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00001860 ret = 1;
1861 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00001862 }
1863 }
hailfingerd217d122010-10-08 18:52:29 +00001864 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00001865 ret = 1;
1866 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00001867 }
stuge8ce3a3c2008-04-28 14:47:30 +00001868 }
ollie6a600992005-11-26 21:55:36 +00001869
hailfinger0459e1c2009-08-19 13:55:34 +00001870 if (verify_it) {
1871 /* Work around chips which need some time to calm down. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001872 if (write_it && verify_it != VERIFY_PARTIAL)
hailfinger0459e1c2009-08-19 13:55:34 +00001873 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001874
1875 ret = verify_flash(flash, newcontents, verify_it);
1876
hailfingera50d60e2009-11-17 09:57:34 +00001877 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00001878 * might have an emergency situation.
1879 */
1880 if (ret && write_it)
1881 emergency_help_message();
1882 }
ollie6a600992005-11-26 21:55:36 +00001883
hailfinger90fcf9b2010-11-05 14:51:59 +00001884out:
1885 free(oldcontents);
1886 free(newcontents);
1887out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07001888 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08001889 /*
1890 * programmer_shutdown() call is moved to cli_mfg() in chromium os
1891 * tree. This is because some operations, such as write protection,
1892 * requires programmer_shutdown() but does not call doit().
1893 */
1894// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00001895 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001896}