blob: d71471c764867b1107935e664675629c22a5a308 [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
David Hendricks1ed1d352011-11-23 17:54:37 -080045/* error handling stuff */
46enum error_action access_denied_action = error_ignore;
47
48int ignore_error(int err) {
49 int rc = 0;
50
51 switch(err) {
52 case ACCESS_DENIED:
53 if (access_denied_action == error_ignore)
54 rc = 1;
55 break;
56 default:
57 break;
58 }
59
60 return rc;
61}
62
hailfinger969e2f32011-09-08 00:00:29 +000063static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000064
hailfingerddeb4ac2010-07-08 10:13:37 +000065static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000066
hailfinger1ff33dc2010-07-03 11:02:10 +000067/* Supported buses for the current programmer. */
68enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +000069
uwee15beb92010-08-08 17:01:18 +000070/*
hailfinger80422e22009-12-13 22:28:00 +000071 * Programmers supporting multiple buses can have differing size limits on
72 * each bus. Store the limits for each bus in a common struct.
73 */
hailfinger1ff33dc2010-07-03 11:02:10 +000074struct decode_sizes max_rom_decode;
75
76/* If nonzero, used as the start address of bottom-aligned flash. */
77unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000078
hailfinger5828baf2010-07-03 12:14:25 +000079/* Is writing allowed with this programmer? */
80int programmer_may_write;
81
hailfingerabe249e2009-05-08 17:43:22 +000082const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000083#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000084 {
hailfinger3548a9a2009-08-12 14:34:35 +000085 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000086 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000087 .map_flash_region = physmap,
88 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000089 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +000090 },
hailfinger80422e22009-12-13 22:28:00 +000091#endif
stepan927d4e22007-04-04 22:45:58 +000092
hailfinger90c7d542010-05-31 15:27:27 +000093#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +000094 {
hailfinger3548a9a2009-08-12 14:34:35 +000095 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +000096 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +000097 .map_flash_region = dummy_map,
98 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +000099 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000100 },
hailfinger571a6b32009-09-16 10:09:21 +0000101#endif
hailfingera9df33c2009-05-09 00:54:55 +0000102
hailfinger90c7d542010-05-31 15:27:27 +0000103#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000104 {
hailfinger3548a9a2009-08-12 14:34:35 +0000105 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000106 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000107 .map_flash_region = fallback_map,
108 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000109 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000110 },
hailfinger571a6b32009-09-16 10:09:21 +0000111#endif
uwe0f5a3a22009-05-13 11:36:06 +0000112
hailfinger90c7d542010-05-31 15:27:27 +0000113#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000114 {
hailfinger0d703d42011-03-07 01:08:09 +0000115 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000116 .name = "nicrealtek",
117 //.name = "nicsmc1211",
118 .init = nicrealtek_init,
119 .map_flash_region = fallback_map,
120 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000121 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000122 },
hailfinger5aa36982010-05-21 21:54:07 +0000123#endif
124
hailfingerf0a368f2010-06-07 22:37:54 +0000125#if CONFIG_NICNATSEMI == 1
126 {
uwe8d342eb2011-07-28 08:13:25 +0000127 .name = "nicnatsemi",
128 .init = nicnatsemi_init,
129 .map_flash_region = fallback_map,
130 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000131 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000132 },
133#endif
hailfinger5aa36982010-05-21 21:54:07 +0000134
hailfinger90c7d542010-05-31 15:27:27 +0000135#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000136 {
137 .name = "gfxnvidia",
138 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000139 .map_flash_region = fallback_map,
140 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000141 .delay = internal_delay,
142 },
143#endif
144
hailfinger90c7d542010-05-31 15:27:27 +0000145#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000146 {
uwee2f95ef2009-09-02 23:00:46 +0000147 .name = "drkaiser",
148 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000149 .map_flash_region = fallback_map,
150 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000151 .delay = internal_delay,
152 },
hailfinger571a6b32009-09-16 10:09:21 +0000153#endif
uwee2f95ef2009-09-02 23:00:46 +0000154
hailfinger90c7d542010-05-31 15:27:27 +0000155#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000156 {
hailfinger3548a9a2009-08-12 14:34:35 +0000157 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000158 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000159 .map_flash_region = fallback_map,
160 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000161 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000162 },
hailfinger571a6b32009-09-16 10:09:21 +0000163#endif
ruikda922a12009-05-17 19:39:27 +0000164
hailfinger90c7d542010-05-31 15:27:27 +0000165#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000166 {
167 .name = "atahpt",
168 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000169 .map_flash_region = fallback_map,
170 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000171 .delay = internal_delay,
172 },
173#endif
174
hailfinger90c7d542010-05-31 15:27:27 +0000175#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000176 {
hailfinger90c7d542010-05-31 15:27:27 +0000177 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000178 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000179 .map_flash_region = fallback_map,
180 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000181 .delay = internal_delay,
182 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000183#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000184
hailfinger90c7d542010-05-31 15:27:27 +0000185#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000186 {
hailfinger3548a9a2009-08-12 14:34:35 +0000187 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000188 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000189 .map_flash_region = fallback_map,
190 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000191 .delay = serprog_delay,
192 },
hailfinger74d88a72009-08-12 16:17:41 +0000193#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000194
hailfinger90c7d542010-05-31 15:27:27 +0000195#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000196 {
hailfinger90c7d542010-05-31 15:27:27 +0000197 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000198 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000199 .map_flash_region = fallback_map,
200 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000201 .delay = internal_delay,
202 },
203#endif
204
hailfinger90c7d542010-05-31 15:27:27 +0000205#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000206 {
207 .name = "dediprog",
208 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000209 .map_flash_region = fallback_map,
210 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000211 .delay = internal_delay,
212 },
213#endif
214
hailfinger52c4fa02010-07-21 10:26:01 +0000215#if CONFIG_RAYER_SPI == 1
216 {
217 .name = "rayer_spi",
218 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000219 .map_flash_region = fallback_map,
220 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000221 .delay = internal_delay,
222 },
223#endif
224
hailfinger7949b652011-05-08 00:24:18 +0000225#if CONFIG_NICINTEL == 1
226 {
227 .name = "nicintel",
228 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000229 .map_flash_region = fallback_map,
230 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000231 .delay = internal_delay,
232 },
233#endif
234
uwe6764e922010-09-03 18:21:21 +0000235#if CONFIG_NICINTEL_SPI == 1
236 {
uwe8d342eb2011-07-28 08:13:25 +0000237 .name = "nicintel_spi",
238 .init = nicintel_spi_init,
239 .map_flash_region = fallback_map,
240 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000241 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000242 },
243#endif
244
hailfingerfb1f31f2010-12-03 14:48:11 +0000245#if CONFIG_OGP_SPI == 1
246 {
uwe8d342eb2011-07-28 08:13:25 +0000247 .name = "ogp_spi",
248 .init = ogp_spi_init,
249 .map_flash_region = fallback_map,
250 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000251 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000252 },
253#endif
254
hailfinger935365d2011-02-04 21:37:59 +0000255#if CONFIG_SATAMV == 1
256 {
257 .name = "satamv",
258 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000259 .map_flash_region = fallback_map,
260 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000261 .delay = internal_delay,
262 },
263#endif
264
uwe7df6dda2011-09-03 18:37:52 +0000265#if CONFIG_LINUX_SPI == 1
266 {
267 .name = "linux_spi",
268 .init = linux_spi_init,
269 .map_flash_region = fallback_map,
270 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000271 .delay = internal_delay,
272 },
273#endif
274
hailfinger3548a9a2009-08-12 14:34:35 +0000275 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000276};
stepan927d4e22007-04-04 22:45:58 +0000277
David Hendricksbf36f092010-11-02 23:39:29 -0700278#define CHIP_RESTORE_MAXFN 4
279static int chip_restore_fn_count = 0;
280struct chip_restore_func_data {
281 CHIP_RESTORE_CALLBACK;
282 struct flashchip *flash;
283 uint8_t status;
284} static chip_restore_fn[CHIP_RESTORE_MAXFN];
285
David Hendricks668f29d2011-01-27 18:51:45 -0800286
hailfingerf31cbdc2010-11-10 15:25:18 +0000287#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000288static int shutdown_fn_count = 0;
289struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000290 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000291 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000292} static shutdown_fn[SHUTDOWN_MAXFN];
293/* Initialize to 0 to make sure nobody registers a shutdown function before
294 * programmer init.
295 */
296static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000297
stefanct569dbb62011-07-01 00:19:12 +0000298static int check_block_eraser(const struct flashchip *flash, int k, int log);
299
hailfingerdc6f7972010-02-14 01:20:28 +0000300/* Register a function to be executed on programmer shutdown.
301 * The advantage over atexit() is that you can supply a void pointer which will
302 * be used as parameter to the registered function upon programmer shutdown.
303 * This pointer can point to arbitrary data used by said function, e.g. undo
304 * information for GPIO settings etc. If unneeded, set data=NULL.
305 * Please note that the first (void *data) belongs to the function signature of
306 * the function passed as first parameter.
307 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000308int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000309{
310 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000311 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000312 SHUTDOWN_MAXFN);
313 return 1;
314 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000315 if (!may_register_shutdown) {
316 msg_perr("Tried to register a shutdown function before "
317 "programmer init.\n");
318 return 1;
319 }
hailfingerdc6f7972010-02-14 01:20:28 +0000320 shutdown_fn[shutdown_fn_count].func = function;
321 shutdown_fn[shutdown_fn_count].data = data;
322 shutdown_fn_count++;
323
324 return 0;
325}
326
David Hendricksbf36f092010-11-02 23:39:29 -0700327//int register_chip_restore(int (*function) (void *data), void *data)
328int register_chip_restore(CHIP_RESTORE_CALLBACK,
329 struct flashchip *flash, uint8_t status)
330{
331 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
332 msg_perr("Tried to register more than %i chip restore"
333 " functions.\n", CHIP_RESTORE_MAXFN);
334 return 1;
335 }
336 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
337 chip_restore_fn[chip_restore_fn_count].flash = flash;
338 chip_restore_fn[chip_restore_fn_count].status = status;
339 chip_restore_fn_count++;
340
341 return 0;
342}
343
hailfinger969e2f32011-09-08 00:00:29 +0000344int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000345{
hailfinger1ef766d2010-07-06 09:55:48 +0000346 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000347
348 if (prog >= PROGRAMMER_INVALID) {
349 msg_perr("Invalid programmer specified!\n");
350 return -1;
351 }
352 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000353 /* Initialize all programmer specific data. */
354 /* Default to unlimited decode sizes. */
355 max_rom_decode = (const struct decode_sizes) {
356 .parallel = 0xffffffff,
357 .lpc = 0xffffffff,
358 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000359 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000360 };
hailfingere1e41ea2011-07-27 07:13:06 +0000361 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000362 /* Default to top aligned flash at 4 GB. */
363 flashbase = 0;
364 /* Registering shutdown functions is now allowed. */
365 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000366 /* Default to allowing writes. Broken programmers set this to 0. */
367 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000368
369 programmer_param = param;
370 msg_pdbg("Initializing %s programmer\n",
371 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000372 ret = programmer_table[programmer].init();
373 if (programmer_param && strlen(programmer_param)) {
374 msg_perr("Unhandled programmer parameters: %s\n",
375 programmer_param);
376 /* Do not error out here, the init itself was successful. */
377 }
378 return ret;
uweabe92a52009-05-16 22:36:00 +0000379}
380
David Hendricksbf36f092010-11-02 23:39:29 -0700381int chip_restore()
382{
383 int rc = 0;
384
385 while (chip_restore_fn_count > 0) {
386 int i = --chip_restore_fn_count;
387 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
388 chip_restore_fn[i].status);
389 }
390
391 return rc;
392}
393
uweabe92a52009-05-16 22:36:00 +0000394int programmer_shutdown(void)
395{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000396 int ret = 0;
397
hailfinger1ff33dc2010-07-03 11:02:10 +0000398 /* Registering shutdown functions is no longer allowed. */
399 may_register_shutdown = 0;
400 while (shutdown_fn_count > 0) {
401 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000402 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000403 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000404 return ret;
uweabe92a52009-05-16 22:36:00 +0000405}
406
407void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
408 size_t len)
409{
410 return programmer_table[programmer].map_flash_region(descr,
411 phys_addr, len);
412}
413
414void programmer_unmap_flash_region(void *virt_addr, size_t len)
415{
416 programmer_table[programmer].unmap_flash_region(virt_addr, len);
417}
418
419void chip_writeb(uint8_t val, chipaddr addr)
420{
hailfinger76bb7e92011-11-09 23:40:00 +0000421 par_programmer->chip_writeb(val, addr);
uweabe92a52009-05-16 22:36:00 +0000422}
423
424void chip_writew(uint16_t val, chipaddr addr)
425{
hailfinger76bb7e92011-11-09 23:40:00 +0000426 par_programmer->chip_writew(val, addr);
uweabe92a52009-05-16 22:36:00 +0000427}
428
429void chip_writel(uint32_t val, chipaddr addr)
430{
hailfinger76bb7e92011-11-09 23:40:00 +0000431 par_programmer->chip_writel(val, addr);
uweabe92a52009-05-16 22:36:00 +0000432}
433
hailfinger9d987ef2009-06-05 18:32:07 +0000434void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
435{
hailfinger76bb7e92011-11-09 23:40:00 +0000436 par_programmer->chip_writen(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000437}
438
uweabe92a52009-05-16 22:36:00 +0000439uint8_t chip_readb(const chipaddr addr)
440{
hailfinger76bb7e92011-11-09 23:40:00 +0000441 return par_programmer->chip_readb(addr);
uweabe92a52009-05-16 22:36:00 +0000442}
443
444uint16_t chip_readw(const chipaddr addr)
445{
hailfinger76bb7e92011-11-09 23:40:00 +0000446 return par_programmer->chip_readw(addr);
uweabe92a52009-05-16 22:36:00 +0000447}
448
449uint32_t chip_readl(const chipaddr addr)
450{
hailfinger76bb7e92011-11-09 23:40:00 +0000451 return par_programmer->chip_readl(addr);
uweabe92a52009-05-16 22:36:00 +0000452}
453
hailfinger9d987ef2009-06-05 18:32:07 +0000454void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
455{
hailfinger76bb7e92011-11-09 23:40:00 +0000456 par_programmer->chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000457}
458
hailfingere5829f62009-06-05 17:48:08 +0000459void programmer_delay(int usecs)
460{
461 programmer_table[programmer].delay(usecs);
462}
463
stuge5ff0e6c2009-01-26 00:39:57 +0000464void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000465{
stepan15e64bc2007-05-24 08:48:10 +0000466 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000467 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000468 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000469 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000470}
471
stefanctc5eb8a92011-11-23 09:13:48 +0000472int read_memmapped(struct flashchip *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000473{
hailfinger0f08b7a2009-06-16 08:55:44 +0000474 chip_readn(buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000475
hailfinger23060112009-05-08 12:49:03 +0000476 return 0;
477}
478
hailfinger7b414742009-06-13 12:04:03 +0000479int min(int a, int b)
480{
481 return (a < b) ? a : b;
482}
483
hailfinger7af83692009-06-15 17:23:36 +0000484int max(int a, int b)
485{
486 return (a > b) ? a : b;
487}
488
hailfingeraec9c962009-10-31 01:53:09 +0000489int bitcount(unsigned long a)
490{
491 int i = 0;
492 for (; a != 0; a >>= 1)
493 if (a & 1)
494 i++;
495 return i;
496}
497
hailfingerf76cc322010-11-09 22:00:31 +0000498void tolower_string(char *str)
499{
500 for (; *str != '\0'; str++)
501 *str = (char)tolower((unsigned char)*str);
502}
503
hailfingera916b422009-06-01 02:08:58 +0000504char *strcat_realloc(char *dest, const char *src)
505{
506 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000507 if (!dest) {
508 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000509 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000510 }
hailfingera916b422009-06-01 02:08:58 +0000511 strcat(dest, src);
512 return dest;
513}
514
hailfinger6e5a52a2009-11-24 18:27:10 +0000515/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000516 * It will look for needle with a subsequent '=' in haystack, return a copy of
517 * needle and remove everything from the first occurrence of needle to the next
518 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000519 */
stefanct52700282011-06-26 17:38:17 +0000520char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000521{
hailfinger1ef766d2010-07-06 09:55:48 +0000522 char *param_pos, *opt_pos, *rest;
523 char *opt = NULL;
524 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000525 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000526
hailfingerf4aaccc2010-04-28 15:22:14 +0000527 needlelen = strlen(needle);
528 if (!needlelen) {
529 msg_gerr("%s: empty needle! Please report a bug at "
530 "flashrom@flashrom.org\n", __func__);
531 return NULL;
532 }
533 /* No programmer parameters given. */
534 if (*haystack == NULL)
535 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000536 param_pos = strstr(*haystack, needle);
537 do {
538 if (!param_pos)
539 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000540 /* Needle followed by '='? */
541 if (param_pos[needlelen] == '=') {
542
543 /* Beginning of the string? */
544 if (param_pos == *haystack)
545 break;
546 /* After a delimiter? */
547 if (strchr(delim, *(param_pos - 1)))
548 break;
549 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000550 /* Continue searching. */
551 param_pos++;
552 param_pos = strstr(param_pos, needle);
553 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000554
hailfinger6e5a52a2009-11-24 18:27:10 +0000555 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000556 /* Get the string after needle and '='. */
557 opt_pos = param_pos + needlelen + 1;
558 optlen = strcspn(opt_pos, delim);
559 /* Return an empty string if the parameter was empty. */
560 opt = malloc(optlen + 1);
561 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000562 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000563 exit(1);
564 }
hailfinger1ef766d2010-07-06 09:55:48 +0000565 strncpy(opt, opt_pos, optlen);
566 opt[optlen] = '\0';
567 rest = opt_pos + optlen;
568 /* Skip all delimiters after the current parameter. */
569 rest += strspn(rest, delim);
570 memmove(param_pos, rest, strlen(rest) + 1);
571 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000572 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000573
hailfinger1ef766d2010-07-06 09:55:48 +0000574 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000575}
576
stefanct52700282011-06-26 17:38:17 +0000577char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000578{
579 return extract_param(&programmer_param, param_name, ",");
580}
581
stefancte1c5acf2011-07-04 07:27:17 +0000582/* Returns the number of well-defined erasers for a chip. */
583static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000584{
585 unsigned int usable_erasefunctions = 0;
586 int k;
587 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
588 if (!check_block_eraser(flash, k, 0))
589 usable_erasefunctions++;
590 }
591 return usable_erasefunctions;
592}
593
hailfinger7af83692009-06-15 17:23:36 +0000594/* start is an offset to the base address of the flash chip */
stefanctc5eb8a92011-11-23 09:13:48 +0000595int check_erased_range(struct flashchip *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000596{
597 int ret;
598 uint8_t *cmpbuf = malloc(len);
599
600 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000601 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000602 exit(1);
603 }
604 memset(cmpbuf, 0xff, len);
605 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
606 free(cmpbuf);
607 return ret;
608}
609
uwee15beb92010-08-08 17:01:18 +0000610/*
hailfinger7af3d192009-11-25 17:05:52 +0000611 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000612 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000613 * @start offset to the base address of the flash chip
614 * @len length of the verified area
615 * @message string to print in the "FAILED" message
616 * @return 0 for success, -1 for failure
617 */
stefanctc5eb8a92011-11-23 09:13:48 +0000618int verify_range(struct flashchip *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000619 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000620{
stefanctc5eb8a92011-11-23 09:13:48 +0000621 unsigned int i;
hailfinger8cb6ece2010-11-16 17:21:58 +0000622 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000623 int ret = 0, failcount = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -0800624 unsigned int chunksize;
hailfinger7af83692009-06-15 17:23:36 +0000625
626 if (!len)
627 goto out_free;
628
hailfingerb0f4d122009-06-24 08:20:45 +0000629 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000630 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000631 return 1;
632 }
hailfinger7af83692009-06-15 17:23:36 +0000633 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000634 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000635 exit(1);
636 }
637
638 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000639 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000640 " total_size 0x%x\n", __func__, start, len,
641 flash->total_size * 1024);
642 ret = -1;
643 goto out_free;
644 }
645 if (!message)
646 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000647
David Hendricks1ed1d352011-11-23 17:54:37 -0800648 chunksize = min(flash->page_size, len);
649 for (i = 0; i < len; i += chunksize) {
650 int tmp, j;
651
652 tmp = flash->read(flash, readbuf + i, start + i, chunksize);
653
654 if (tmp) {
655 ret = tmp;
656 if (ignore_error(tmp)) {
657 chunksize = min(flash->page_size, len - i);
658 continue;
659 } else {
660 goto out_free;
661 }
662 }
663
664 for (j = 0; j < chunksize; j++) {
665 if (cmpbuf[i + j] != readbuf[i + j]) {
666 /* Only print the first failure. */
667 if (!failcount++)
668 msg_cerr("%s FAILED at 0x%08x! "
669 "Expected=0x%02x, Read=0x%02x,",
670 message, start + i + j, cmpbuf[i + j],
671 readbuf[j]);
672 }
673 }
674 chunksize = min(flash->page_size, len - i);
hailfinger8cb6ece2010-11-16 17:21:58 +0000675 }
676
hailfinger5be6c0f2009-07-23 01:42:56 +0000677 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000678 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000679 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000680 ret = -1;
681 }
hailfinger7af83692009-06-15 17:23:36 +0000682
683out_free:
684 free(readbuf);
685 return ret;
686}
687
uwee15beb92010-08-08 17:01:18 +0000688/*
hailfingerb247c7a2010-03-08 00:42:32 +0000689 * Check if the buffer @have can be programmed to the content of @want without
690 * erasing. This is only possible if all chunks of size @gran are either kept
691 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000692 *
hailfingerb247c7a2010-03-08 00:42:32 +0000693 * The following write granularities (enum @gran) are known:
694 * - 1 bit. Each bit can be cleared individually.
695 * - 1 byte. A byte can be written once. Further writes to an already written
696 * byte cause the contents to be either undefined or to stay unchanged.
697 * - 128 bytes. If less than 128 bytes are written, the rest will be
698 * erased. Each write to a 128-byte region will trigger an automatic erase
699 * before anything is written. Very uncommon behaviour and unsupported by
700 * this function.
701 * - 256 bytes. If less than 256 bytes are written, the contents of the
702 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000703 * Warning: This function assumes that @have and @want point to naturally
704 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000705 *
706 * @have buffer with current content
707 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000708 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000709 * @gran write granularity (enum, not count)
710 * @return 0 if no erase is needed, 1 otherwise
711 */
stefanctc5eb8a92011-11-23 09:13:48 +0000712int need_erase(uint8_t *have, uint8_t *want, unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000713{
hailfingerb91c08c2011-08-15 19:54:20 +0000714 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000715 unsigned int i, j, limit;
hailfingerb247c7a2010-03-08 00:42:32 +0000716
717 switch (gran) {
718 case write_gran_1bit:
719 for (i = 0; i < len; i++)
720 if ((have[i] & want[i]) != want[i]) {
721 result = 1;
722 break;
723 }
724 break;
725 case write_gran_1byte:
726 for (i = 0; i < len; i++)
727 if ((have[i] != want[i]) && (have[i] != 0xff)) {
728 result = 1;
729 break;
730 }
731 break;
732 case write_gran_256bytes:
733 for (j = 0; j < len / 256; j++) {
734 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000735 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000736 if (!memcmp(have + j * 256, want + j * 256, limit))
737 continue;
738 /* have needs to be in erased state. */
739 for (i = 0; i < limit; i++)
hailfingerb437e282010-11-04 01:04:27 +0000740 if (have[j * 256 + i] != 0xff) {
hailfingerb247c7a2010-03-08 00:42:32 +0000741 result = 1;
742 break;
743 }
744 if (result)
745 break;
746 }
747 break;
hailfingerb437e282010-11-04 01:04:27 +0000748 default:
749 msg_cerr("%s: Unsupported granularity! Please report a bug at "
750 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000751 }
752 return result;
753}
754
hailfingerb437e282010-11-04 01:04:27 +0000755/**
756 * Check if the buffer @have needs to be programmed to get the content of @want.
757 * If yes, return 1 and fill in first_start with the start address of the
758 * write operation and first_len with the length of the first to-be-written
759 * chunk. If not, return 0 and leave first_start and first_len undefined.
760 *
761 * Warning: This function assumes that @have and @want point to naturally
762 * aligned regions.
763 *
764 * @have buffer with current content
765 * @want buffer with desired content
766 * @len length of the checked area
767 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000768 * @first_start offset of the first byte which needs to be written (passed in
769 * value is increased by the offset of the first needed write
770 * relative to have/want or unchanged if no write is needed)
771 * @return length of the first contiguous area which needs to be written
772 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000773 *
774 * FIXME: This function needs a parameter which tells it about coalescing
775 * in relation to the max write length of the programmer and the max write
776 * length of the chip.
777 */
stefanctc5eb8a92011-11-23 09:13:48 +0000778static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
779 unsigned int *first_start,
780 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000781{
stefanctc5eb8a92011-11-23 09:13:48 +0000782 int need_write = 0;
783 unsigned int rel_start = 0, first_len = 0;
784 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000785
hailfingerb437e282010-11-04 01:04:27 +0000786 switch (gran) {
787 case write_gran_1bit:
788 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000789 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000790 break;
791 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000792 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000793 break;
794 default:
795 msg_cerr("%s: Unsupported granularity! Please report a bug at "
796 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000797 /* Claim that no write was needed. A write with unknown
798 * granularity is too dangerous to try.
799 */
800 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000801 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000802 for (i = 0; i < len / stride; i++) {
803 limit = min(stride, len - i * stride);
804 /* Are 'have' and 'want' identical? */
805 if (memcmp(have + i * stride, want + i * stride, limit)) {
806 if (!need_write) {
807 /* First location where have and want differ. */
808 need_write = 1;
809 rel_start = i * stride;
810 }
811 } else {
812 if (need_write) {
813 /* First location where have and want
814 * do not differ anymore.
815 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000816 break;
817 }
818 }
819 }
hailfingerffb7f382010-12-06 13:05:44 +0000820 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000821 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000822 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000823 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000824}
825
hailfinger0c515352009-11-23 12:55:31 +0000826/* This function generates various test patterns useful for testing controller
827 * and chip communication as well as chip behaviour.
828 *
829 * If a byte can be written multiple times, each time keeping 0-bits at 0
830 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
831 * is essentially an AND operation. That's also the reason why this function
832 * provides the result of AND between various patterns.
833 *
834 * Below is a list of patterns (and their block length).
835 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
836 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
837 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
838 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
839 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
840 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
841 * Pattern 6 is 00 (1 Byte)
842 * Pattern 7 is ff (1 Byte)
843 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
844 * byte block.
845 *
846 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
847 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
848 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
849 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
850 * Pattern 12 is 00 (1 Byte)
851 * Pattern 13 is ff (1 Byte)
852 * Patterns 8-13 have no block number.
853 *
854 * Patterns 0-3 are created to detect and efficiently diagnose communication
855 * slips like missed bits or bytes and their repetitive nature gives good visual
856 * cues to the person inspecting the results. In addition, the following holds:
857 * AND Pattern 0/1 == Pattern 4
858 * AND Pattern 2/3 == Pattern 5
859 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
860 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
861 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
862 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
863 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
864 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
865 * Besides that, they provide for bit testing of the last two bytes of every
866 * 256 byte block which contains the block number for patterns 0-6.
867 * Patterns 10-11 are special purpose for detecting subblock aliasing with
868 * block sizes >256 bytes (some Dataflash chips etc.)
869 * AND Pattern 8/9 == Pattern 12
870 * AND Pattern 10/11 == Pattern 12
871 * Pattern 13 is the completely erased state.
872 * None of the patterns can detect aliasing at boundaries which are a multiple
873 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
874 */
875int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
876{
877 int i;
878
879 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000880 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000881 return 1;
882 }
883
884 switch (variant) {
885 case 0:
886 for (i = 0; i < size; i++)
887 buf[i] = (i & 0xf) << 4 | 0x5;
888 break;
889 case 1:
890 for (i = 0; i < size; i++)
891 buf[i] = (i & 0xf) << 4 | 0xa;
892 break;
893 case 2:
894 for (i = 0; i < size; i++)
895 buf[i] = 0x50 | (i & 0xf);
896 break;
897 case 3:
898 for (i = 0; i < size; i++)
899 buf[i] = 0xa0 | (i & 0xf);
900 break;
901 case 4:
902 for (i = 0; i < size; i++)
903 buf[i] = (i & 0xf) << 4;
904 break;
905 case 5:
906 for (i = 0; i < size; i++)
907 buf[i] = i & 0xf;
908 break;
909 case 6:
910 memset(buf, 0x00, size);
911 break;
912 case 7:
913 memset(buf, 0xff, size);
914 break;
915 case 8:
916 for (i = 0; i < size; i++)
917 buf[i] = i & 0xff;
918 break;
919 case 9:
920 for (i = 0; i < size; i++)
921 buf[i] = ~(i & 0xff);
922 break;
923 case 10:
924 for (i = 0; i < size % 2; i++) {
925 buf[i * 2] = (i >> 8) & 0xff;
926 buf[i * 2 + 1] = i & 0xff;
927 }
928 if (size & 0x1)
929 buf[i * 2] = (i >> 8) & 0xff;
930 break;
931 case 11:
932 for (i = 0; i < size % 2; i++) {
933 buf[i * 2] = ~((i >> 8) & 0xff);
934 buf[i * 2 + 1] = ~(i & 0xff);
935 }
936 if (size & 0x1)
937 buf[i * 2] = ~((i >> 8) & 0xff);
938 break;
939 case 12:
940 memset(buf, 0x00, size);
941 break;
942 case 13:
943 memset(buf, 0xff, size);
944 break;
945 }
946
947 if ((variant >= 0) && (variant <= 7)) {
948 /* Write block number in the last two bytes of each 256-byte
949 * block, big endian for easier reading of the hexdump.
950 * Note that this wraps around for chips larger than 2^24 bytes
951 * (16 MB).
952 */
953 for (i = 0; i < size / 256; i++) {
954 buf[i * 256 + 254] = (i >> 8) & 0xff;
955 buf[i * 256 + 255] = i & 0xff;
956 }
957 }
958
959 return 0;
960}
961
hailfingeraec9c962009-10-31 01:53:09 +0000962int check_max_decode(enum chipbustype buses, uint32_t size)
963{
964 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +0000965
966 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000967 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000968 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000969 "size %u kB of chipset/board/programmer "
970 "for %s interface, "
971 "probe/read/erase/write may fail. ", size / 1024,
972 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +0000973 }
hailfingere1e41ea2011-07-27 07:13:06 +0000974 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000975 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000976 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000977 "size %u kB of chipset/board/programmer "
978 "for %s interface, "
979 "probe/read/erase/write may fail. ", size / 1024,
980 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +0000981 }
hailfingere1e41ea2011-07-27 07:13:06 +0000982 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000983 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000984 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000985 "size %u kB of chipset/board/programmer "
986 "for %s interface, "
987 "probe/read/erase/write may fail. ", size / 1024,
988 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +0000989 }
hailfingere1e41ea2011-07-27 07:13:06 +0000990 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000991 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000992 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000993 "size %u kB of chipset/board/programmer "
994 "for %s interface, "
995 "probe/read/erase/write may fail. ", size / 1024,
996 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +0000997 }
998 if (!limitexceeded)
999 return 0;
1000 /* Sometimes chip and programmer have more than one bus in common,
1001 * and the limit is not exceeded on all buses. Tell the user.
1002 */
1003 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001004 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001005 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001006 "interface which can support a chip of this size. "
1007 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001008 return 1;
1009}
1010
hailfinger48ed3e22011-05-04 00:39:50 +00001011int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001012{
hailfinger48ed3e22011-05-04 00:39:50 +00001013 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001014 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001015 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001016 uint32_t size;
1017 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001018 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001019
hailfinger48ed3e22011-05-04 00:39:50 +00001020 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001021 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001022 continue;
hailfingeraec9c962009-10-31 01:53:09 +00001023 buses_common = buses_supported & flash->bustype;
1024 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001025 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1026 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +00001027 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +00001028 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001029 free(tmp);
1030 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001031 msg_gspew("and chip bus type %s are incompatible.",
1032 tmp);
hailfingera916b422009-06-01 02:08:58 +00001033 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001034 msg_gspew("\n");
1035 continue;
1036 }
1037 msg_gdbg("Probing for %s %s, %d kB: ",
1038 flash->vendor, flash->name, flash->total_size);
1039 if (!flash->probe && !force) {
1040 msg_gdbg("failed! flashrom has no probe function for "
1041 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001042 continue;
1043 }
stepan782fb172007-04-06 11:58:03 +00001044
ollie5672ac62004-03-17 22:22:08 +00001045 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001046 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001047
hailfinger48ed3e22011-05-04 00:39:50 +00001048 /* Start filling in the dynamic data. */
1049 *fill_flash = *flash;
1050
hailfinger72d3b982009-05-09 07:27:23 +00001051 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001052 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001053
stugec1e55fe2008-07-02 17:15:47 +00001054 if (force)
1055 break;
stepanc98b80b2006-03-16 16:57:41 +00001056
hailfinger48ed3e22011-05-04 00:39:50 +00001057 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001058 goto notfound;
1059
hailfinger48ed3e22011-05-04 00:39:50 +00001060 /* If this is the first chip found, accept it.
1061 * If this is not the first chip found, accept it only if it is
1062 * a non-generic match.
1063 * We could either make chipcount global or provide it as
1064 * parameter, or we assume that startchip==0 means this call to
1065 * probe_flash() is the first one and thus no chip has been
1066 * found before.
1067 */
1068 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001069 break;
1070
stuge56300c32008-09-03 23:10:05 +00001071notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001072 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001073 }
uwebe4477b2007-08-23 16:08:21 +00001074
stugec1e55fe2008-07-02 17:15:47 +00001075 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001076 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001077
hailfingere11396b2011-03-08 00:09:11 +00001078#if CONFIG_INTERNAL == 1
1079 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001080 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001081 else
1082#endif
stepan3e7aeae2011-01-19 06:21:54 +00001083 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001084
stefanct588b6d22011-06-26 20:45:35 +00001085 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001086 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1087 force ? "Assuming" : "Found", fill_flash->vendor,
1088 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001089 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001090
hailfinger0f4c3952010-12-02 21:59:42 +00001091 /* Flash registers will not be mapped if the chip was forced. Lock info
1092 * may be stored in registers, so avoid lock info printing.
1093 */
1094 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001095 if (fill_flash->printlock)
1096 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001097
hailfinger48ed3e22011-05-04 00:39:50 +00001098 /* Return position of matching chip. */
1099 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001100}
1101
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001102int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001103{
hailfingerb0f4d122009-06-24 08:20:45 +00001104 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001105 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001106
snelsone42c3802010-05-07 20:09:04 +00001107 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001108
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001109 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1110 ret = handle_partial_verify(flash, buf, verify_range);
1111 } else {
1112 ret = verify_range(flash, buf, 0, total_size, NULL);
1113 }
uwef6641642007-05-09 10:17:44 +00001114
David Hendricks1ed1d352011-11-23 17:54:37 -08001115 if (ret == ACCESS_DENIED) {
1116 msg_gdbg("Could not fully verify due to access error, ");
1117 if (access_denied_action == error_ignore) {
1118 msg_gdbg("ignoring\n");
1119 ret = 0;
1120 } else {
1121 msg_gdbg("aborting\n");
1122 }
1123 }
1124
hailfingerb0f4d122009-06-24 08:20:45 +00001125 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001126 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001127
hailfingerb0f4d122009-06-24 08:20:45 +00001128 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001129}
1130
uwe8d342eb2011-07-28 08:13:25 +00001131int read_buf_from_file(unsigned char *buf, unsigned long size,
1132 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001133{
1134 unsigned long numbytes;
1135 FILE *image;
1136 struct stat image_stat;
1137
1138 if ((image = fopen(filename, "rb")) == NULL) {
1139 perror(filename);
1140 return 1;
1141 }
1142 if (fstat(fileno(image), &image_stat) != 0) {
1143 perror(filename);
1144 fclose(image);
1145 return 1;
1146 }
1147 if (image_stat.st_size != size) {
1148 msg_gerr("Error: Image size doesn't match\n");
1149 fclose(image);
1150 return 1;
1151 }
1152 numbytes = fread(buf, 1, size, image);
1153 if (fclose(image)) {
1154 perror(filename);
1155 return 1;
1156 }
1157 if (numbytes != size) {
1158 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1159 "wanted %ld!\n", numbytes, size);
1160 return 1;
1161 }
1162 return 0;
1163}
1164
uwe8d342eb2011-07-28 08:13:25 +00001165int write_buf_to_file(unsigned char *buf, unsigned long size,
1166 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001167{
1168 unsigned long numbytes;
1169 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001170
1171 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001172 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001173 return 1;
1174 }
oxygenebf70d352010-01-25 22:55:33 +00001175 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001176 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001177 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001178 }
hailfingerd219a232009-01-28 00:27:54 +00001179
hailfingerd219a232009-01-28 00:27:54 +00001180 numbytes = fwrite(buf, 1, size, image);
1181 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001182 if (numbytes != size) {
1183 msg_gerr("File %s could not be written completely.\n",
1184 filename);
hailfingerd219a232009-01-28 00:27:54 +00001185 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001186 }
hailfingerd219a232009-01-28 00:27:54 +00001187 return 0;
1188}
1189
stefanct52700282011-06-26 17:38:17 +00001190int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001191{
1192 unsigned long size = flash->total_size * 1024;
1193 unsigned char *buf = calloc(size, sizeof(char));
1194 int ret = 0;
1195
1196 msg_cinfo("Reading flash... ");
1197 if (!buf) {
1198 msg_gerr("Memory allocation failed!\n");
1199 msg_cinfo("FAILED.\n");
1200 return 1;
1201 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001202
1203 /* To support partial read, fill buffer to all 0xFF at beginning to make
1204 * debug easier. */
1205 memset(buf, 0xFF, size);
1206
hailfinger42a850a2010-07-13 23:56:13 +00001207 if (!flash->read) {
1208 msg_cerr("No read function available for this flash chip.\n");
1209 ret = 1;
1210 goto out_free;
1211 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001212
1213 /* First try to handle partial read case, rather than read the whole
1214 * flash, which is slow. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001215 ret = handle_partial_read(flash, buf, flash->read, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001216 if (ret < 0) {
1217 msg_cerr("Partial read operation failed!\n");
1218 ret = 1;
1219 goto out_free;
1220 } else if (ret > 0) {
1221 /* Partial read has been handled, pass the whole flash read. */
David Hendricks1ed1d352011-11-23 17:54:37 -08001222 } else {
1223 int tmp = flash->read(flash, buf, 0, size);
1224
1225 if (!tmp) {
1226 ret = tmp;
1227 } else if (ignore_error(tmp)) {
1228 ret = 0;
1229 } else {
1230 msg_cerr("Read operation failed!\n");
1231 ret = 1;
1232 goto out_free;
1233 }
hailfinger42a850a2010-07-13 23:56:13 +00001234 }
1235
stefanct9e6b98a2011-05-28 02:37:14 +00001236 ret = write_buf_to_file(buf, size, filename);
hailfinger42a850a2010-07-13 23:56:13 +00001237out_free:
1238 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001239 if (ret)
1240 msg_cerr("FAILED.");
1241 else
1242 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001243 return ret;
1244}
1245
hailfingerb437e282010-11-04 01:04:27 +00001246/* This function shares a lot of its structure with erase_and_write_flash() and
1247 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001248 * Even if an error is found, the function will keep going and check the rest.
1249 */
hailfinger48ed3e22011-05-04 00:39:50 +00001250static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001251{
hailfingerb91c08c2011-08-15 19:54:20 +00001252 int i, j, k;
1253 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001254
1255 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1256 unsigned int done = 0;
1257 struct block_eraser eraser = flash->block_erasers[k];
1258
1259 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1260 /* Blocks with zero size are bugs in flashchips.c. */
1261 if (eraser.eraseblocks[i].count &&
1262 !eraser.eraseblocks[i].size) {
1263 msg_gerr("ERROR: Flash chip %s erase function "
1264 "%i region %i has size 0. Please report"
1265 " a bug at flashrom@flashrom.org\n",
1266 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001267 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001268 }
1269 /* Blocks with zero count are bugs in flashchips.c. */
1270 if (!eraser.eraseblocks[i].count &&
1271 eraser.eraseblocks[i].size) {
1272 msg_gerr("ERROR: Flash chip %s erase function "
1273 "%i region %i has count 0. Please report"
1274 " a bug at flashrom@flashrom.org\n",
1275 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001276 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001277 }
1278 done += eraser.eraseblocks[i].count *
1279 eraser.eraseblocks[i].size;
1280 }
hailfinger9fed35d2010-01-19 06:42:46 +00001281 /* Empty eraseblock definition with erase function. */
1282 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001283 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001284 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001285 if (!done)
1286 continue;
1287 if (done != flash->total_size * 1024) {
1288 msg_gerr("ERROR: Flash chip %s erase function %i "
1289 "region walking resulted in 0x%06x bytes total,"
1290 " expected 0x%06x bytes. Please report a bug at"
1291 " flashrom@flashrom.org\n", flash->name, k,
1292 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001293 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001294 }
hailfinger9fed35d2010-01-19 06:42:46 +00001295 if (!eraser.block_erase)
1296 continue;
1297 /* Check if there are identical erase functions for different
1298 * layouts. That would imply "magic" erase functions. The
1299 * easiest way to check this is with function pointers.
1300 */
uwef6f94d42010-03-13 17:28:29 +00001301 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001302 if (eraser.block_erase ==
1303 flash->block_erasers[j].block_erase) {
1304 msg_gerr("ERROR: Flash chip %s erase function "
1305 "%i and %i are identical. Please report"
1306 " a bug at flashrom@flashrom.org\n",
1307 flash->name, k, j);
1308 ret = 1;
1309 }
uwef6f94d42010-03-13 17:28:29 +00001310 }
hailfinger45177872010-01-18 08:14:43 +00001311 }
hailfinger9fed35d2010-01-19 06:42:46 +00001312 return ret;
hailfinger45177872010-01-18 08:14:43 +00001313}
1314
hailfingerb437e282010-11-04 01:04:27 +00001315static int erase_and_write_block_helper(struct flashchip *flash,
1316 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001317 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001318 uint8_t *newcontents,
1319 int (*erasefn) (struct flashchip *flash,
1320 unsigned int addr,
1321 unsigned int len))
1322{
stefanctc5eb8a92011-11-23 09:13:48 +00001323 unsigned int starthere = 0, lenhere = 0;
1324 int ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001325 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1326
hailfinger90fcf9b2010-11-05 14:51:59 +00001327 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001328 * need to be adjusted here to keep the impression of proper abstraction
1329 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001330 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001331 newcontents += start;
1332 msg_cdbg(":");
1333 /* FIXME: Assume 256 byte granularity for now to play it safe. */
hailfinger90fcf9b2010-11-05 14:51:59 +00001334 if (need_erase(curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001335 msg_cdbg("E");
1336 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001337 if (ret) {
1338 if (ret == ACCESS_DENIED)
1339 msg_cdbg("D");
1340 else
1341 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001342 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001343 }
1344
hailfingerac8e3182011-06-26 17:04:16 +00001345 if (check_erased_range(flash, start, len)) {
1346 msg_cerr("ERASE FAILED!\n");
1347 return -1;
1348 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001349 /* Erase was successful. Adjust curcontents. */
1350 memset(curcontents, 0xff, len);
hailfingerb437e282010-11-04 01:04:27 +00001351 skip = 0;
1352 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001353 /* get_next_write() sets starthere to a new value after the call. */
1354 while ((lenhere = get_next_write(curcontents + starthere,
1355 newcontents + starthere,
1356 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001357 if (!writecount++)
1358 msg_cdbg("W");
1359 /* Needs the partial write function signature. */
1360 ret = flash->write(flash, newcontents + starthere,
1361 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001362 if (ret) {
1363 if (ret == ACCESS_DENIED)
1364 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001365 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001366 }
hailfingerb437e282010-11-04 01:04:27 +00001367 starthere += lenhere;
1368 skip = 0;
1369 }
1370 if (skip)
1371 msg_cdbg("S");
1372 return ret;
1373}
1374
hailfinger83541b32010-07-13 00:42:00 +00001375static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1376 int (*do_something) (struct flashchip *flash,
1377 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001378 unsigned int len,
1379 uint8_t *param1,
1380 uint8_t *param2,
1381 int (*erasefn) (
1382 struct flashchip *flash,
1383 unsigned int addr,
1384 unsigned int len)),
1385 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001386{
David Hendricks1ed1d352011-11-23 17:54:37 -08001387 int i, j, rc = 0;
hailfingerb91c08c2011-08-15 19:54:20 +00001388 unsigned int start = 0;
1389 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001390 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001391
hailfinger2b8c9382010-07-13 00:37:19 +00001392 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1393 /* count==0 for all automatically initialized array
1394 * members so the loop below won't be executed for them.
1395 */
1396 len = eraser.eraseblocks[i].size;
1397 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001398 /* Print this for every block except the first one. */
1399 if (i || j)
1400 msg_cdbg(", ");
1401 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001402 start + len - 1);
David Hendricks1ed1d352011-11-23 17:54:37 -08001403 rc = do_something(flash, start, len, param1, param2,
1404 eraser.block_erase);
1405 if (rc) {
1406 if (ignore_error(rc))
1407 rc = 0;
1408 else
1409 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001410 }
hailfinger2b8c9382010-07-13 00:37:19 +00001411 start += len;
1412 }
1413 }
hailfingerb437e282010-11-04 01:04:27 +00001414 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001415 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001416}
1417
stefanct569dbb62011-07-01 00:19:12 +00001418static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001419{
1420 struct block_eraser eraser = flash->block_erasers[k];
1421
1422 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1423 if (log)
1424 msg_cdbg("not defined. ");
1425 return 1;
1426 }
1427 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1428 if (log)
1429 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001430 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001431 return 1;
1432 }
1433 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1434 if (log)
1435 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001436 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001437 return 1;
1438 }
1439 return 0;
1440}
1441
uwe8d342eb2011-07-28 08:13:25 +00001442int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1443 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001444{
hailfinger8f524a82011-07-21 21:21:04 +00001445 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001446 uint8_t *curcontents;
1447 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001448 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001449
hailfingercf848f12010-12-05 15:14:44 +00001450 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001451 curcontents = malloc(size);
1452 if (!curcontents) {
1453 msg_gerr("Out of memory!\n");
1454 exit(1);
1455 }
hailfingerb437e282010-11-04 01:04:27 +00001456 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1457 memcpy(curcontents, oldcontents, size);
1458
hailfinger7df21362009-09-05 02:30:58 +00001459 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001460 if (k != 0)
1461 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001462 if (!usable_erasefunctions) {
1463 msg_cdbg("No usable erase functions left.\n");
1464 break;
1465 }
stefanctf0bc4712011-07-26 14:28:35 +00001466 msg_cdbg("Trying erase function %i... ", k);
1467 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001468 continue;
hailfingercf848f12010-12-05 15:14:44 +00001469 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001470 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1471 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001472 /* If everything is OK, don't try another erase function. */
1473 if (!ret)
1474 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001475 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001476 * contents are. If no usable erase functions remain, we can
1477 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001478 */
hailfingercf848f12010-12-05 15:14:44 +00001479 if (!usable_erasefunctions)
1480 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001481 /* Reading the whole chip may take a while, inform the user even
1482 * in non-verbose mode.
1483 */
1484 msg_cinfo("Reading current flash chip contents... ");
hailfinger6237f5e2010-12-02 02:41:55 +00001485 if (flash->read(flash, curcontents, 0, size)) {
1486 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001487 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001488 /* We have no idea about the flash chip contents, so
1489 * retrying with another erase function is pointless.
1490 */
1491 break;
1492 }
David Hendricks89a45e52011-11-22 16:56:22 -08001493 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001494 }
hailfingerb437e282010-11-04 01:04:27 +00001495 /* Free the scratchpad. */
1496 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001497
hailfinger7df21362009-09-05 02:30:58 +00001498 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001499 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001500 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001501 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001502 }
1503 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001504}
1505
hailfinger4c47e9d2010-10-19 22:06:20 +00001506void nonfatal_help_message(void)
1507{
1508 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1509 "This means we have to add special support for your board, "
1510 "programmer or flash chip.\n"
1511 "Please report this on IRC at irc.freenode.net (channel "
1512 "#flashrom) or\n"
1513 "mail flashrom@flashrom.org!\n"
1514 "-------------------------------------------------------------"
1515 "------------------\n"
1516 "You may now reboot or simply leave the machine running.\n");
1517}
1518
uweb34ec9f2009-10-01 18:40:02 +00001519void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001520{
snelsone42c3802010-05-07 20:09:04 +00001521 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001522 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001523 "mail flashrom@flashrom.org with FAILED: your board name in "
1524 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001525 "-------------------------------------------------------------"
1526 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001527 "DO NOT REBOOT OR POWEROFF!\n");
1528}
1529
uwe8d342eb2011-07-28 08:13:25 +00001530/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001531void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001532{
1533 enum programmer p;
1534 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001535 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001536 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001537 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001538 }
snelsone42c3802010-05-07 20:09:04 +00001539 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001540}
1541
hailfingerf79d1712010-10-06 23:48:34 +00001542void list_programmers_linebreak(int startcol, int cols, int paren)
1543{
1544 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001545 int pnamelen;
1546 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001547 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001548 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001549
1550 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1551 pname = programmer_table[p].name;
1552 pnamelen = strlen(pname);
1553 if (remaining - pnamelen - 2 < 0) {
1554 if (firstline)
1555 firstline = 0;
1556 else
1557 printf("\n");
1558 for (i = 0; i < startcol; i++)
1559 printf(" ");
1560 remaining = cols - startcol;
1561 } else {
1562 printf(" ");
1563 remaining--;
1564 }
1565 if (paren && (p == 0)) {
1566 printf("(");
1567 remaining--;
1568 }
1569 printf("%s", pname);
1570 remaining -= pnamelen;
1571 if (p < PROGRAMMER_INVALID - 1) {
1572 printf(",");
1573 remaining--;
1574 } else {
1575 if (paren)
1576 printf(")");
1577 printf("\n");
1578 }
1579 }
1580}
1581
hailfinger3b471632010-03-27 16:36:40 +00001582void print_sysinfo(void)
1583{
David Hendricksc6c9f822010-11-03 15:07:01 -07001584 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001585#if HAVE_UTSNAME == 1
1586 struct utsname osinfo;
1587 uname(&osinfo);
1588
David Hendricksc6c9f822010-11-03 15:07:01 -07001589 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001590 osinfo.machine);
1591#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001592 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001593#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001594 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001595#if NEED_PCI == 1
1596#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001597 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001598#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001599 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001600#endif
1601#endif
1602#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001603 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001604#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001605 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001606#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001607 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001608#endif
hailfinger3b471632010-03-27 16:36:40 +00001609#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001610 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001611#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001612 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001613#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001614 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001615#endif
1616#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001617 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001618#endif
1619#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001620 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001621#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001622 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001623#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001624 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001625}
1626
uwefdeca092008-01-21 15:24:22 +00001627void print_version(void)
1628{
David Hendricksc6c9f822010-11-03 15:07:01 -07001629 /* send to stderr for chromium os */
1630 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001631 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001632}
1633
hailfinger74819ad2010-05-15 15:04:37 +00001634void print_banner(void)
1635{
1636 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001637 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001638 msg_ginfo("\n");
1639}
1640
hailfingerc77acb52009-12-24 02:15:55 +00001641int selfcheck(void)
1642{
hailfinger45177872010-01-18 08:14:43 +00001643 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001644 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001645
1646 /* Safety check. Instead of aborting after the first error, check
1647 * if more errors exist.
1648 */
hailfingerc77acb52009-12-24 02:15:55 +00001649 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001650 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001651 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001652 }
stefanct8632c922011-07-26 14:33:46 +00001653 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001654 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001655 * For 'flashchips' we check the first element to be non-null. In the
1656 * other cases there exist use cases where the first element can be
1657 * null. */
1658 if (flashchips == NULL || flashchips[0].vendor == NULL) {
1659 msg_gerr("Flashchips table miscompilation!\n");
1660 ret = 1;
1661 }
hailfinger45177872010-01-18 08:14:43 +00001662 for (flash = flashchips; flash && flash->name; flash++)
1663 if (selfcheck_eraseblocks(flash))
1664 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001665
1666#if CONFIG_INTERNAL == 1
1667 if (chipset_enables == NULL) {
1668 msg_gerr("Chipset enables table does not exist!\n");
1669 ret = 1;
1670 }
hailfinger4640bdb2011-08-31 16:19:50 +00001671 if (board_matches == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001672 msg_gerr("Board enables table does not exist!\n");
1673 ret = 1;
1674 }
1675 if (boards_known == NULL) {
1676 msg_gerr("Known boards table does not exist!\n");
1677 ret = 1;
1678 }
1679 if (laptops_known == NULL) {
1680 msg_gerr("Known laptops table does not exist!\n");
1681 ret = 1;
1682 }
uwe8d342eb2011-07-28 08:13:25 +00001683#endif
hailfinger45177872010-01-18 08:14:43 +00001684 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001685}
1686
hailfinger48ed3e22011-05-04 00:39:50 +00001687void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001688{
1689 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001690 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001691 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001692 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001693 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001694 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001695 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001696 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001697 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001698 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001699 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001700 msg_cdbg(" WRITE");
1701 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001702 }
1703 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1704 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1705 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1706 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001707 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001708 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001709 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001710 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001711 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001712 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001713 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001714 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001715 msg_cdbg(" WRITE");
1716 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001717 }
hailfinger92cd8e32010-01-07 03:24:05 +00001718 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001719 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001720 "in the latest development\n"
1721 "version of flashrom. If you are running the latest "
1722 "development version,\n"
1723 "please email a report to flashrom@flashrom.org if "
1724 "any of the above operations\n"
1725 "work correctly for you with this flash part. Please "
1726 "include the flashrom\n"
1727 "output with the additional -V option for all "
1728 "operations you tested (-V, -Vr,\n"
1729 "-Vw, -VE), and mention which mainboard or "
1730 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001731 "Please mention your board in the subject line. "
1732 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001733 }
1734}
1735
hailfinger771fc182010-10-15 00:01:14 +00001736/* FIXME: This function signature needs to be improved once doit() has a better
1737 * function signature.
1738 */
stefanct02116582011-05-18 01:30:56 +00001739int 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 +00001740{
1741 if (!programmer_may_write && (write_it || erase_it)) {
1742 msg_perr("Write/erase is not working yet on your programmer in "
1743 "its current configuration.\n");
1744 /* --force is the wrong approach, but it's the best we can do
1745 * until the generic programmer parameter parser is merged.
1746 */
1747 if (!force)
1748 return 1;
1749 msg_cerr("Continuing anyway.\n");
1750 }
1751
1752 if (read_it || erase_it || write_it || verify_it) {
1753 /* Everything needs read. */
1754 if (flash->tested & TEST_BAD_READ) {
1755 msg_cerr("Read is not working on this chip. ");
1756 if (!force)
1757 return 1;
1758 msg_cerr("Continuing anyway.\n");
1759 }
1760 if (!flash->read) {
1761 msg_cerr("flashrom has no read function for this "
1762 "flash chip.\n");
1763 return 1;
1764 }
1765 }
1766 if (erase_it || write_it) {
1767 /* Write needs erase. */
1768 if (flash->tested & TEST_BAD_ERASE) {
1769 msg_cerr("Erase is not working on this chip. ");
1770 if (!force)
1771 return 1;
1772 msg_cerr("Continuing anyway.\n");
1773 }
stefancte1c5acf2011-07-04 07:27:17 +00001774 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001775 msg_cerr("flashrom has no erase function for this "
1776 "flash chip.\n");
1777 return 1;
1778 }
hailfinger771fc182010-10-15 00:01:14 +00001779 }
1780 if (write_it) {
1781 if (flash->tested & TEST_BAD_WRITE) {
1782 msg_cerr("Write is not working on this chip. ");
1783 if (!force)
1784 return 1;
1785 msg_cerr("Continuing anyway.\n");
1786 }
1787 if (!flash->write) {
1788 msg_cerr("flashrom has no write function for this "
1789 "flash chip.\n");
1790 return 1;
1791 }
1792 }
1793 return 0;
1794}
1795
hailfingerc77acb52009-12-24 02:15:55 +00001796/* This function signature is horrible. We need to design a better interface,
1797 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001798 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001799 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001800int 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 +00001801{
hailfinger4c47e9d2010-10-19 22:06:20 +00001802 uint8_t *oldcontents;
1803 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001804 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001805 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001806
stefanct02116582011-05-18 01:30:56 +00001807 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001808 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001809 ret = 1;
1810 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001811 }
1812
hailfinger771fc182010-10-15 00:01:14 +00001813 /* Given the existence of read locks, we want to unlock for read,
1814 * erase and write.
1815 */
1816 if (flash->unlock)
1817 flash->unlock(flash);
1818
David Hendricks6d62d752011-03-07 21:20:22 -08001819 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001820 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001821 ret = 1;
1822 goto out_nofree;
1823 }
1824
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001825 /* mark entries included using -i argument as "included" if they are
1826 found in the master rom_entries list */
1827 if (process_include_args() < 0) {
1828 ret = 1;
1829 goto out_nofree;
1830 }
1831
hailfinger771fc182010-10-15 00:01:14 +00001832 if (read_it) {
1833 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001834 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001835 }
hailfingerb437e282010-11-04 01:04:27 +00001836
stefanctd611e8f2011-07-12 22:35:21 +00001837 oldcontents = malloc(size);
1838 if (!oldcontents) {
1839 msg_gerr("Out of memory!\n");
1840 exit(1);
1841 }
hailfingerb437e282010-11-04 01:04:27 +00001842 /* Assume worst case: All bits are 0. */
1843 memset(oldcontents, 0x00, size);
stefanctd611e8f2011-07-12 22:35:21 +00001844 newcontents = malloc(size);
1845 if (!newcontents) {
1846 msg_gerr("Out of memory!\n");
1847 exit(1);
1848 }
hailfingerb437e282010-11-04 01:04:27 +00001849 /* Assume best case: All bits should be 1. */
1850 memset(newcontents, 0xff, size);
1851 /* Side effect of the assumptions above: Default write action is erase
1852 * because newcontents looks like a completely erased chip, and
1853 * oldcontents being completely 0x00 means we have to erase everything
1854 * before we can write.
1855 */
1856
ollie0eb62d62004-12-08 20:10:01 +00001857 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001858 /* FIXME: Do we really want the scary warning if erase failed?
1859 * After all, after erase the chip is either blank or partially
1860 * blank or it has the old contents. A blank chip won't boot,
1861 * so if the user wanted erase and reboots afterwards, the user
1862 * knows very well that booting won't work.
1863 */
hailfingerb437e282010-11-04 01:04:27 +00001864 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001865 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001866 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001867 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001868 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001869 }
hailfinger771fc182010-10-15 00:01:14 +00001870
hailfingerd217d122010-10-08 18:52:29 +00001871 if (write_it || verify_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001872 if (read_buf_from_file(newcontents, size, filename)) {
hailfinger90fcf9b2010-11-05 14:51:59 +00001873 ret = 1;
1874 goto out;
stepan1da96c02006-11-21 23:48:51 +00001875 }
1876
David Hendricksac82cac2012-06-19 10:29:37 -07001877#if 0
1878 /*
1879 * FIXME: show_id() causes failure if vendor:mainboard do not
1880 * match. This may happen if codenames are in flux.
1881 * See chrome-os-partner:10414.
1882 */
hailfinger90c7d542010-05-31 15:27:27 +00001883#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001884 if (programmer == PROGRAMMER_INTERNAL)
1885 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001886#endif
David Hendricksac82cac2012-06-19 10:29:37 -07001887#endif
ollie5672ac62004-03-17 22:22:08 +00001888 }
1889
David Hendricksc44d7a02011-10-17 11:28:43 -07001890 /* Obtain a reference image so that we can check whether regions need
1891 * to be erased and to give better diagnostics in case write fails.
hailfinger4c47e9d2010-10-19 22:06:20 +00001892 * The alternative would be to read only the regions which are to be
1893 * preserved, but in that case we might perform unneeded erase which
1894 * takes time as well.
1895 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001896 if (diff_file) {
1897 msg_cdbg("Reading old contents from file... ");
1898 if (read_buf_from_file(oldcontents, size, diff_file)) {
1899 ret = 1;
1900 msg_cdbg("FAILED.\n");
1901 goto out;
1902 }
1903 } else {
David Hendricks1ed1d352011-11-23 17:54:37 -08001904 int tmp;
1905
David Hendricksc44d7a02011-10-17 11:28:43 -07001906 msg_cdbg("Reading old contents from flash chip... ");
David Hendricks1ed1d352011-11-23 17:54:37 -08001907 tmp = flash->read(flash, oldcontents, 0, size);
1908 if (tmp) {
1909 if (ignore_error(tmp)) {
1910 msg_gdbg("ignoring access error.\n");
1911 } else {
1912 ret = 1;
1913 msg_cdbg("FAILED.\n");
1914 goto out;
1915 }
David Hendricksc44d7a02011-10-17 11:28:43 -07001916 }
hailfinger4c47e9d2010-10-19 22:06:20 +00001917 }
David Hendricks89a45e52011-11-22 16:56:22 -08001918 msg_cdbg("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00001919
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001920 // This should be moved into each flash part's code to do it
ollie6a600992005-11-26 21:55:36 +00001921 // cleanly. This does the job.
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08001922 if (handle_romentries(flash, oldcontents, newcontents)) {
1923 ret = 1;
1924 msg_cerr("Cannot handle partial read.\n");
1925 goto out;
1926 }
uwef6641642007-05-09 10:17:44 +00001927
ollie6a600992005-11-26 21:55:36 +00001928 // ////////////////////////////////////////////////////////////
uwef6641642007-05-09 10:17:44 +00001929
stuge8ce3a3c2008-04-28 14:47:30 +00001930 if (write_it) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08001931 // parse the new fmap
David Hendricks5b06c882012-05-20 18:27:25 -07001932 if ((ret = gec_prepare(newcontents, size))) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08001933 msg_cerr("GEC prepare failed, ret=%d.\n", ret);
1934 goto out;
1935 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08001936
hailfingerb437e282010-11-04 01:04:27 +00001937 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1938 msg_cerr("Uh oh. Erase/write failed. Checking if "
1939 "anything changed.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00001940 if (!flash->read(flash, newcontents, 0, size)) {
1941 if (!memcmp(oldcontents, newcontents, size)) {
1942 msg_cinfo("Good. It seems nothing was "
1943 "changed.\n");
1944 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00001945 ret = 1;
1946 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00001947 }
1948 }
hailfingerd217d122010-10-08 18:52:29 +00001949 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00001950 ret = 1;
1951 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00001952 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08001953
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08001954 ret = gec_need_2nd_pass();
1955 if (ret < 0) {
1956 // Jump failed
1957 msg_cerr("gec_need_2nd_pass() failed. Stop.\n");
1958 emergency_help_message();
1959 ret = 1;
1960 goto out;
1961 } else if (ret > 0) {
1962 // Need 2nd pass. Get the just written content.
1963 msg_pdbg("GEC needs 2nd pass.\n");
1964 if (flash->read(flash, oldcontents, 0, size)) {
1965 msg_cerr("Uh oh. Cannot get latest content.\n");
1966 emergency_help_message();
1967 ret = 1;
1968 goto out;
1969 }
1970 // write 2nd pass
1971 if (erase_and_write_flash(flash, oldcontents,
1972 newcontents)) {
1973 msg_cerr("Uh oh. GEC 2nd pass failed.\n");
1974 emergency_help_message();
1975 ret = 1;
1976 goto out;
1977 }
1978 ret = 0;
1979 }
stuge8ce3a3c2008-04-28 14:47:30 +00001980 }
ollie6a600992005-11-26 21:55:36 +00001981
hailfinger0459e1c2009-08-19 13:55:34 +00001982 if (verify_it) {
1983 /* Work around chips which need some time to calm down. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001984 if (write_it && verify_it != VERIFY_PARTIAL)
hailfinger0459e1c2009-08-19 13:55:34 +00001985 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001986
1987 ret = verify_flash(flash, newcontents, verify_it);
1988
hailfingera50d60e2009-11-17 09:57:34 +00001989 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00001990 * might have an emergency situation.
1991 */
1992 if (ret && write_it)
1993 emergency_help_message();
1994 }
ollie6a600992005-11-26 21:55:36 +00001995
hailfinger90fcf9b2010-11-05 14:51:59 +00001996out:
1997 free(oldcontents);
1998 free(newcontents);
1999out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002000 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002001 /*
2002 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2003 * tree. This is because some operations, such as write protection,
2004 * requires programmer_shutdown() but does not call doit().
2005 */
2006// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002007 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002008}