blob: 5f839747097e1f854e8b3d3ebc0e2cb3b7b9fc42 [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
Vincent Palatin7ab23932014-10-01 12:09:16 -070037#include <unistd.h>
rminnich8d3ff912003-10-25 17:01:29 +000038#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000039#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090040#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000041#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000042
krause2eb76212011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000044char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000045int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000046
David Hendricks1ed1d352011-11-23 17:54:37 -080047/* error handling stuff */
48enum error_action access_denied_action = error_ignore;
49
50int ignore_error(int err) {
51 int rc = 0;
52
53 switch(err) {
54 case ACCESS_DENIED:
55 if (access_denied_action == error_ignore)
56 rc = 1;
57 break;
58 default:
59 break;
60 }
61
62 return rc;
63}
64
hailfinger969e2f32011-09-08 00:00:29 +000065static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000066
hailfingerddeb4ac2010-07-08 10:13:37 +000067static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000068
hailfinger1ff33dc2010-07-03 11:02:10 +000069/* Supported buses for the current programmer. */
70enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +000071
uwee15beb92010-08-08 17:01:18 +000072/*
hailfinger80422e22009-12-13 22:28:00 +000073 * Programmers supporting multiple buses can have differing size limits on
74 * each bus. Store the limits for each bus in a common struct.
75 */
hailfinger1ff33dc2010-07-03 11:02:10 +000076struct decode_sizes max_rom_decode;
77
78/* If nonzero, used as the start address of bottom-aligned flash. */
79unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000080
hailfinger5828baf2010-07-03 12:14:25 +000081/* Is writing allowed with this programmer? */
82int programmer_may_write;
83
hailfingerabe249e2009-05-08 17:43:22 +000084const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000085#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000086 {
hailfinger3548a9a2009-08-12 14:34:35 +000087 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000088 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000089 .map_flash_region = physmap,
90 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000091 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +000092 },
hailfinger80422e22009-12-13 22:28:00 +000093#endif
stepan927d4e22007-04-04 22:45:58 +000094
hailfinger90c7d542010-05-31 15:27:27 +000095#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +000096 {
hailfinger3548a9a2009-08-12 14:34:35 +000097 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +000098 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +000099 .map_flash_region = dummy_map,
100 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000101 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000102 },
hailfinger571a6b32009-09-16 10:09:21 +0000103#endif
hailfingera9df33c2009-05-09 00:54:55 +0000104
hailfinger90c7d542010-05-31 15:27:27 +0000105#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000106 {
hailfinger3548a9a2009-08-12 14:34:35 +0000107 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000108 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000109 .map_flash_region = fallback_map,
110 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000111 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000112 },
hailfinger571a6b32009-09-16 10:09:21 +0000113#endif
uwe0f5a3a22009-05-13 11:36:06 +0000114
hailfinger90c7d542010-05-31 15:27:27 +0000115#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000116 {
hailfinger0d703d42011-03-07 01:08:09 +0000117 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000118 .name = "nicrealtek",
119 //.name = "nicsmc1211",
120 .init = nicrealtek_init,
121 .map_flash_region = fallback_map,
122 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000123 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000124 },
hailfinger5aa36982010-05-21 21:54:07 +0000125#endif
126
hailfingerf0a368f2010-06-07 22:37:54 +0000127#if CONFIG_NICNATSEMI == 1
128 {
uwe8d342eb2011-07-28 08:13:25 +0000129 .name = "nicnatsemi",
130 .init = nicnatsemi_init,
131 .map_flash_region = fallback_map,
132 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000133 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000134 },
135#endif
hailfinger5aa36982010-05-21 21:54:07 +0000136
hailfinger90c7d542010-05-31 15:27:27 +0000137#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000138 {
139 .name = "gfxnvidia",
140 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000141 .map_flash_region = fallback_map,
142 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000143 .delay = internal_delay,
144 },
145#endif
146
hailfinger90c7d542010-05-31 15:27:27 +0000147#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000148 {
uwee2f95ef2009-09-02 23:00:46 +0000149 .name = "drkaiser",
150 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000151 .map_flash_region = fallback_map,
152 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000153 .delay = internal_delay,
154 },
hailfinger571a6b32009-09-16 10:09:21 +0000155#endif
uwee2f95ef2009-09-02 23:00:46 +0000156
hailfinger90c7d542010-05-31 15:27:27 +0000157#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000158 {
hailfinger3548a9a2009-08-12 14:34:35 +0000159 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000160 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000161 .map_flash_region = fallback_map,
162 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000163 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000164 },
hailfinger571a6b32009-09-16 10:09:21 +0000165#endif
ruikda922a12009-05-17 19:39:27 +0000166
hailfinger90c7d542010-05-31 15:27:27 +0000167#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000168 {
169 .name = "atahpt",
170 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000171 .map_flash_region = fallback_map,
172 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000173 .delay = internal_delay,
174 },
175#endif
176
hailfinger90c7d542010-05-31 15:27:27 +0000177#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000178 {
hailfinger90c7d542010-05-31 15:27:27 +0000179 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000180 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000181 .map_flash_region = fallback_map,
182 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000183 .delay = internal_delay,
184 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000185#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000186
hailfinger90c7d542010-05-31 15:27:27 +0000187#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000188 {
hailfinger3548a9a2009-08-12 14:34:35 +0000189 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000190 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000191 .map_flash_region = fallback_map,
192 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000193 .delay = serprog_delay,
194 },
hailfinger74d88a72009-08-12 16:17:41 +0000195#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000196
hailfinger90c7d542010-05-31 15:27:27 +0000197#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000198 {
hailfinger90c7d542010-05-31 15:27:27 +0000199 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000200 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000201 .map_flash_region = fallback_map,
202 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000203 .delay = internal_delay,
204 },
205#endif
206
hailfinger90c7d542010-05-31 15:27:27 +0000207#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000208 {
209 .name = "dediprog",
210 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000211 .map_flash_region = fallback_map,
212 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000213 .delay = internal_delay,
214 },
215#endif
216
hailfinger52c4fa02010-07-21 10:26:01 +0000217#if CONFIG_RAYER_SPI == 1
218 {
219 .name = "rayer_spi",
220 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000221 .map_flash_region = fallback_map,
222 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000223 .delay = internal_delay,
224 },
225#endif
226
hailfinger7949b652011-05-08 00:24:18 +0000227#if CONFIG_NICINTEL == 1
228 {
229 .name = "nicintel",
230 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000231 .map_flash_region = fallback_map,
232 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000233 .delay = internal_delay,
234 },
235#endif
236
uwe6764e922010-09-03 18:21:21 +0000237#if CONFIG_NICINTEL_SPI == 1
238 {
uwe8d342eb2011-07-28 08:13:25 +0000239 .name = "nicintel_spi",
240 .init = nicintel_spi_init,
241 .map_flash_region = fallback_map,
242 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000243 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000244 },
245#endif
246
hailfingerfb1f31f2010-12-03 14:48:11 +0000247#if CONFIG_OGP_SPI == 1
248 {
uwe8d342eb2011-07-28 08:13:25 +0000249 .name = "ogp_spi",
250 .init = ogp_spi_init,
251 .map_flash_region = fallback_map,
252 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000253 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000254 },
255#endif
256
hailfinger935365d2011-02-04 21:37:59 +0000257#if CONFIG_SATAMV == 1
258 {
259 .name = "satamv",
260 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000261 .map_flash_region = fallback_map,
262 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000263 .delay = internal_delay,
264 },
265#endif
266
uwe7df6dda2011-09-03 18:37:52 +0000267#if CONFIG_LINUX_SPI == 1
268 {
269 .name = "linux_spi",
270 .init = linux_spi_init,
271 .map_flash_region = fallback_map,
272 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000273 .delay = internal_delay,
274 },
275#endif
276
hailfinger3548a9a2009-08-12 14:34:35 +0000277 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000278};
stepan927d4e22007-04-04 22:45:58 +0000279
David Hendricksbf36f092010-11-02 23:39:29 -0700280#define CHIP_RESTORE_MAXFN 4
281static int chip_restore_fn_count = 0;
282struct chip_restore_func_data {
283 CHIP_RESTORE_CALLBACK;
284 struct flashchip *flash;
285 uint8_t status;
286} static chip_restore_fn[CHIP_RESTORE_MAXFN];
287
David Hendricks668f29d2011-01-27 18:51:45 -0800288
hailfingerf31cbdc2010-11-10 15:25:18 +0000289#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000290static int shutdown_fn_count = 0;
291struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000292 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000293 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000294} static shutdown_fn[SHUTDOWN_MAXFN];
295/* Initialize to 0 to make sure nobody registers a shutdown function before
296 * programmer init.
297 */
298static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000299
stefanct569dbb62011-07-01 00:19:12 +0000300static int check_block_eraser(const struct flashchip *flash, int k, int log);
301
hailfingerdc6f7972010-02-14 01:20:28 +0000302/* Register a function to be executed on programmer shutdown.
303 * The advantage over atexit() is that you can supply a void pointer which will
304 * be used as parameter to the registered function upon programmer shutdown.
305 * This pointer can point to arbitrary data used by said function, e.g. undo
306 * information for GPIO settings etc. If unneeded, set data=NULL.
307 * Please note that the first (void *data) belongs to the function signature of
308 * the function passed as first parameter.
309 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000310int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000311{
312 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000313 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000314 SHUTDOWN_MAXFN);
315 return 1;
316 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000317 if (!may_register_shutdown) {
318 msg_perr("Tried to register a shutdown function before "
319 "programmer init.\n");
320 return 1;
321 }
hailfingerdc6f7972010-02-14 01:20:28 +0000322 shutdown_fn[shutdown_fn_count].func = function;
323 shutdown_fn[shutdown_fn_count].data = data;
324 shutdown_fn_count++;
325
326 return 0;
327}
328
David Hendricksbf36f092010-11-02 23:39:29 -0700329//int register_chip_restore(int (*function) (void *data), void *data)
330int register_chip_restore(CHIP_RESTORE_CALLBACK,
331 struct flashchip *flash, uint8_t status)
332{
333 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
334 msg_perr("Tried to register more than %i chip restore"
335 " functions.\n", CHIP_RESTORE_MAXFN);
336 return 1;
337 }
338 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
339 chip_restore_fn[chip_restore_fn_count].flash = flash;
340 chip_restore_fn[chip_restore_fn_count].status = status;
341 chip_restore_fn_count++;
342
343 return 0;
344}
345
hailfinger969e2f32011-09-08 00:00:29 +0000346int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000347{
hailfinger1ef766d2010-07-06 09:55:48 +0000348 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000349
350 if (prog >= PROGRAMMER_INVALID) {
351 msg_perr("Invalid programmer specified!\n");
352 return -1;
353 }
354 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000355 /* Initialize all programmer specific data. */
356 /* Default to unlimited decode sizes. */
357 max_rom_decode = (const struct decode_sizes) {
358 .parallel = 0xffffffff,
359 .lpc = 0xffffffff,
360 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000361 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000362 };
hailfingere1e41ea2011-07-27 07:13:06 +0000363 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000364 /* Default to top aligned flash at 4 GB. */
365 flashbase = 0;
366 /* Registering shutdown functions is now allowed. */
367 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000368 /* Default to allowing writes. Broken programmers set this to 0. */
369 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000370
371 programmer_param = param;
372 msg_pdbg("Initializing %s programmer\n",
373 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000374 ret = programmer_table[programmer].init();
375 if (programmer_param && strlen(programmer_param)) {
376 msg_perr("Unhandled programmer parameters: %s\n",
377 programmer_param);
378 /* Do not error out here, the init itself was successful. */
379 }
380 return ret;
uweabe92a52009-05-16 22:36:00 +0000381}
382
David Hendricksbf36f092010-11-02 23:39:29 -0700383int chip_restore()
384{
385 int rc = 0;
386
387 while (chip_restore_fn_count > 0) {
388 int i = --chip_restore_fn_count;
389 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
390 chip_restore_fn[i].status);
391 }
392
393 return rc;
394}
395
uweabe92a52009-05-16 22:36:00 +0000396int programmer_shutdown(void)
397{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000398 int ret = 0;
399
hailfinger1ff33dc2010-07-03 11:02:10 +0000400 /* Registering shutdown functions is no longer allowed. */
401 may_register_shutdown = 0;
402 while (shutdown_fn_count > 0) {
403 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000404 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000405 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000406 return ret;
uweabe92a52009-05-16 22:36:00 +0000407}
408
409void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
410 size_t len)
411{
412 return programmer_table[programmer].map_flash_region(descr,
413 phys_addr, len);
414}
415
416void programmer_unmap_flash_region(void *virt_addr, size_t len)
417{
418 programmer_table[programmer].unmap_flash_region(virt_addr, len);
419}
420
421void chip_writeb(uint8_t val, chipaddr addr)
422{
hailfinger76bb7e92011-11-09 23:40:00 +0000423 par_programmer->chip_writeb(val, addr);
uweabe92a52009-05-16 22:36:00 +0000424}
425
426void chip_writew(uint16_t val, chipaddr addr)
427{
hailfinger76bb7e92011-11-09 23:40:00 +0000428 par_programmer->chip_writew(val, addr);
uweabe92a52009-05-16 22:36:00 +0000429}
430
431void chip_writel(uint32_t val, chipaddr addr)
432{
hailfinger76bb7e92011-11-09 23:40:00 +0000433 par_programmer->chip_writel(val, addr);
uweabe92a52009-05-16 22:36:00 +0000434}
435
hailfinger9d987ef2009-06-05 18:32:07 +0000436void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
437{
hailfinger76bb7e92011-11-09 23:40:00 +0000438 par_programmer->chip_writen(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000439}
440
uweabe92a52009-05-16 22:36:00 +0000441uint8_t chip_readb(const chipaddr addr)
442{
hailfinger76bb7e92011-11-09 23:40:00 +0000443 return par_programmer->chip_readb(addr);
uweabe92a52009-05-16 22:36:00 +0000444}
445
446uint16_t chip_readw(const chipaddr addr)
447{
hailfinger76bb7e92011-11-09 23:40:00 +0000448 return par_programmer->chip_readw(addr);
uweabe92a52009-05-16 22:36:00 +0000449}
450
451uint32_t chip_readl(const chipaddr addr)
452{
hailfinger76bb7e92011-11-09 23:40:00 +0000453 return par_programmer->chip_readl(addr);
uweabe92a52009-05-16 22:36:00 +0000454}
455
hailfinger9d987ef2009-06-05 18:32:07 +0000456void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
457{
hailfinger76bb7e92011-11-09 23:40:00 +0000458 par_programmer->chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000459}
460
hailfingere5829f62009-06-05 17:48:08 +0000461void programmer_delay(int usecs)
462{
463 programmer_table[programmer].delay(usecs);
464}
465
stuge5ff0e6c2009-01-26 00:39:57 +0000466void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000467{
stepan15e64bc2007-05-24 08:48:10 +0000468 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000469 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000470 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000471 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000472}
473
stefanctc5eb8a92011-11-23 09:13:48 +0000474int read_memmapped(struct flashchip *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000475{
hailfinger0f08b7a2009-06-16 08:55:44 +0000476 chip_readn(buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000477
hailfinger23060112009-05-08 12:49:03 +0000478 return 0;
479}
480
hailfinger7b414742009-06-13 12:04:03 +0000481int min(int a, int b)
482{
483 return (a < b) ? a : b;
484}
485
hailfinger7af83692009-06-15 17:23:36 +0000486int max(int a, int b)
487{
488 return (a > b) ? a : b;
489}
490
hailfingeraec9c962009-10-31 01:53:09 +0000491int bitcount(unsigned long a)
492{
493 int i = 0;
494 for (; a != 0; a >>= 1)
495 if (a & 1)
496 i++;
497 return i;
498}
499
hailfingerf76cc322010-11-09 22:00:31 +0000500void tolower_string(char *str)
501{
502 for (; *str != '\0'; str++)
503 *str = (char)tolower((unsigned char)*str);
504}
505
hailfingera916b422009-06-01 02:08:58 +0000506char *strcat_realloc(char *dest, const char *src)
507{
508 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000509 if (!dest) {
510 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000511 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000512 }
hailfingera916b422009-06-01 02:08:58 +0000513 strcat(dest, src);
514 return dest;
515}
516
hailfinger6e5a52a2009-11-24 18:27:10 +0000517/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000518 * It will look for needle with a subsequent '=' in haystack, return a copy of
519 * needle and remove everything from the first occurrence of needle to the next
520 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000521 */
stefanct52700282011-06-26 17:38:17 +0000522char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000523{
hailfinger1ef766d2010-07-06 09:55:48 +0000524 char *param_pos, *opt_pos, *rest;
525 char *opt = NULL;
526 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000527 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000528
hailfingerf4aaccc2010-04-28 15:22:14 +0000529 needlelen = strlen(needle);
530 if (!needlelen) {
531 msg_gerr("%s: empty needle! Please report a bug at "
532 "flashrom@flashrom.org\n", __func__);
533 return NULL;
534 }
535 /* No programmer parameters given. */
536 if (*haystack == NULL)
537 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000538 param_pos = strstr(*haystack, needle);
539 do {
540 if (!param_pos)
541 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000542 /* Needle followed by '='? */
543 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600544
hailfinger1ef766d2010-07-06 09:55:48 +0000545 /* Beginning of the string? */
546 if (param_pos == *haystack)
547 break;
548 /* After a delimiter? */
549 if (strchr(delim, *(param_pos - 1)))
550 break;
551 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000552 /* Continue searching. */
553 param_pos++;
554 param_pos = strstr(param_pos, needle);
555 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000556
hailfinger6e5a52a2009-11-24 18:27:10 +0000557 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000558 /* Get the string after needle and '='. */
559 opt_pos = param_pos + needlelen + 1;
560 optlen = strcspn(opt_pos, delim);
561 /* Return an empty string if the parameter was empty. */
562 opt = malloc(optlen + 1);
563 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000564 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000565 exit(1);
566 }
hailfinger1ef766d2010-07-06 09:55:48 +0000567 strncpy(opt, opt_pos, optlen);
568 opt[optlen] = '\0';
569 rest = opt_pos + optlen;
570 /* Skip all delimiters after the current parameter. */
571 rest += strspn(rest, delim);
572 memmove(param_pos, rest, strlen(rest) + 1);
573 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000574 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000575
hailfinger1ef766d2010-07-06 09:55:48 +0000576 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000577}
578
stefanct52700282011-06-26 17:38:17 +0000579char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000580{
581 return extract_param(&programmer_param, param_name, ",");
582}
583
stefancte1c5acf2011-07-04 07:27:17 +0000584/* Returns the number of well-defined erasers for a chip. */
585static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000586{
587 unsigned int usable_erasefunctions = 0;
588 int k;
589 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
590 if (!check_block_eraser(flash, k, 0))
591 usable_erasefunctions++;
592 }
593 return usable_erasefunctions;
594}
595
hailfinger7af83692009-06-15 17:23:36 +0000596/* start is an offset to the base address of the flash chip */
stefanctc5eb8a92011-11-23 09:13:48 +0000597int check_erased_range(struct flashchip *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000598{
599 int ret;
600 uint8_t *cmpbuf = malloc(len);
601
602 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000603 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000604 exit(1);
605 }
Simon Glass4c214132013-07-16 10:09:28 -0600606 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000607 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
608 free(cmpbuf);
609 return ret;
610}
611
uwee15beb92010-08-08 17:01:18 +0000612/*
hailfinger7af3d192009-11-25 17:05:52 +0000613 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000614 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000615 * @start offset to the base address of the flash chip
616 * @len length of the verified area
617 * @message string to print in the "FAILED" message
618 * @return 0 for success, -1 for failure
619 */
stefanctc5eb8a92011-11-23 09:13:48 +0000620int verify_range(struct flashchip *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000621 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000622{
stefanctc5eb8a92011-11-23 09:13:48 +0000623 unsigned int i;
hailfinger8cb6ece2010-11-16 17:21:58 +0000624 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000625 int ret = 0, failcount = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -0800626 unsigned int chunksize;
hailfinger7af83692009-06-15 17:23:36 +0000627
628 if (!len)
629 goto out_free;
630
hailfingerb0f4d122009-06-24 08:20:45 +0000631 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000632 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000633 return 1;
634 }
hailfinger7af83692009-06-15 17:23:36 +0000635 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000636 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000637 exit(1);
638 }
639
640 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000641 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000642 " total_size 0x%x\n", __func__, start, len,
643 flash->total_size * 1024);
644 ret = -1;
645 goto out_free;
646 }
647 if (!message)
648 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000649
David Hendricks758e2fb2014-08-19 20:34:43 -0700650 for (i = 0, chunksize = 0; i < len; i += chunksize) {
David Hendricks1ed1d352011-11-23 17:54:37 -0800651 int tmp, j;
652
David Hendricks758e2fb2014-08-19 20:34:43 -0700653 chunksize = min(flash->page_size, len - i);
David Hendricks1ed1d352011-11-23 17:54:37 -0800654 tmp = flash->read(flash, readbuf + i, start + i, chunksize);
655
David Hendrickse3451942013-03-21 17:23:29 -0700656 /* Since this function explicitly compares the bytes, we need
657 to handle errors manually */
David Hendricks1ed1d352011-11-23 17:54:37 -0800658 if (tmp) {
659 ret = tmp;
David Hendricks758e2fb2014-08-19 20:34:43 -0700660 if (ignore_error(tmp))
David Hendricks1ed1d352011-11-23 17:54:37 -0800661 continue;
David Hendricks758e2fb2014-08-19 20:34:43 -0700662 else
David Hendricks1ed1d352011-11-23 17:54:37 -0800663 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800664 }
665
666 for (j = 0; j < chunksize; j++) {
667 if (cmpbuf[i + j] != readbuf[i + j]) {
668 /* Only print the first failure. */
669 if (!failcount++)
670 msg_cerr("%s FAILED at 0x%08x! "
671 "Expected=0x%02x, Read=0x%02x,",
672 message, start + i + j, cmpbuf[i + j],
673 readbuf[j]);
674 }
675 }
hailfinger8cb6ece2010-11-16 17:21:58 +0000676 }
677
hailfinger5be6c0f2009-07-23 01:42:56 +0000678 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000679 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000680 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000681 ret = -1;
682 }
hailfinger7af83692009-06-15 17:23:36 +0000683
684out_free:
685 free(readbuf);
686 return ret;
687}
688
uwee15beb92010-08-08 17:01:18 +0000689/*
hailfingerb247c7a2010-03-08 00:42:32 +0000690 * Check if the buffer @have can be programmed to the content of @want without
691 * erasing. This is only possible if all chunks of size @gran are either kept
692 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000693 *
hailfingerb247c7a2010-03-08 00:42:32 +0000694 * The following write granularities (enum @gran) are known:
695 * - 1 bit. Each bit can be cleared individually.
696 * - 1 byte. A byte can be written once. Further writes to an already written
697 * byte cause the contents to be either undefined or to stay unchanged.
698 * - 128 bytes. If less than 128 bytes are written, the rest will be
699 * erased. Each write to a 128-byte region will trigger an automatic erase
700 * before anything is written. Very uncommon behaviour and unsupported by
701 * this function.
702 * - 256 bytes. If less than 256 bytes are written, the contents of the
703 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000704 * Warning: This function assumes that @have and @want point to naturally
705 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000706 *
707 * @have buffer with current content
708 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000709 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000710 * @gran write granularity (enum, not count)
711 * @return 0 if no erase is needed, 1 otherwise
712 */
Simon Glass4c214132013-07-16 10:09:28 -0600713static int need_erase(struct flashchip *flash, uint8_t *have, uint8_t *want,
714 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000715{
hailfingerb91c08c2011-08-15 19:54:20 +0000716 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000717 unsigned int i, j, limit;
Simon Glass4c214132013-07-16 10:09:28 -0600718 int erase_value = flash_erase_value(flash);
hailfingerb247c7a2010-03-08 00:42:32 +0000719
720 switch (gran) {
721 case write_gran_1bit:
722 for (i = 0; i < len; i++)
723 if ((have[i] & want[i]) != want[i]) {
724 result = 1;
725 break;
726 }
727 break;
728 case write_gran_1byte:
729 for (i = 0; i < len; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600730 if ((have[i] != want[i]) && (have[i] != erase_value)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000731 result = 1;
732 break;
733 }
734 break;
735 case write_gran_256bytes:
736 for (j = 0; j < len / 256; j++) {
737 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000738 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000739 if (!memcmp(have + j * 256, want + j * 256, limit))
740 continue;
741 /* have needs to be in erased state. */
742 for (i = 0; i < limit; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600743 if (have[j * 256 + i] != erase_value) {
hailfingerb247c7a2010-03-08 00:42:32 +0000744 result = 1;
745 break;
746 }
747 if (result)
748 break;
749 }
750 break;
hailfingerb437e282010-11-04 01:04:27 +0000751 default:
752 msg_cerr("%s: Unsupported granularity! Please report a bug at "
753 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000754 }
755 return result;
756}
757
hailfingerb437e282010-11-04 01:04:27 +0000758/**
759 * Check if the buffer @have needs to be programmed to get the content of @want.
760 * If yes, return 1 and fill in first_start with the start address of the
761 * write operation and first_len with the length of the first to-be-written
762 * chunk. If not, return 0 and leave first_start and first_len undefined.
763 *
764 * Warning: This function assumes that @have and @want point to naturally
765 * aligned regions.
766 *
767 * @have buffer with current content
768 * @want buffer with desired content
769 * @len length of the checked area
770 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000771 * @first_start offset of the first byte which needs to be written (passed in
772 * value is increased by the offset of the first needed write
773 * relative to have/want or unchanged if no write is needed)
774 * @return length of the first contiguous area which needs to be written
775 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000776 *
777 * FIXME: This function needs a parameter which tells it about coalescing
778 * in relation to the max write length of the programmer and the max write
779 * length of the chip.
780 */
stefanctc5eb8a92011-11-23 09:13:48 +0000781static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
782 unsigned int *first_start,
783 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000784{
stefanctc5eb8a92011-11-23 09:13:48 +0000785 int need_write = 0;
786 unsigned int rel_start = 0, first_len = 0;
787 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000788
hailfingerb437e282010-11-04 01:04:27 +0000789 switch (gran) {
790 case write_gran_1bit:
791 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000792 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000793 break;
794 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000795 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000796 break;
797 default:
798 msg_cerr("%s: Unsupported granularity! Please report a bug at "
799 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000800 /* Claim that no write was needed. A write with unknown
801 * granularity is too dangerous to try.
802 */
803 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000804 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000805 for (i = 0; i < len / stride; i++) {
806 limit = min(stride, len - i * stride);
807 /* Are 'have' and 'want' identical? */
808 if (memcmp(have + i * stride, want + i * stride, limit)) {
809 if (!need_write) {
810 /* First location where have and want differ. */
811 need_write = 1;
812 rel_start = i * stride;
813 }
814 } else {
815 if (need_write) {
816 /* First location where have and want
817 * do not differ anymore.
818 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000819 break;
820 }
821 }
822 }
hailfingerffb7f382010-12-06 13:05:44 +0000823 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000824 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000825 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000826 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000827}
828
hailfinger0c515352009-11-23 12:55:31 +0000829/* This function generates various test patterns useful for testing controller
830 * and chip communication as well as chip behaviour.
831 *
832 * If a byte can be written multiple times, each time keeping 0-bits at 0
833 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
834 * is essentially an AND operation. That's also the reason why this function
835 * provides the result of AND between various patterns.
836 *
837 * Below is a list of patterns (and their block length).
838 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
839 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
840 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
841 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
842 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
843 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
844 * Pattern 6 is 00 (1 Byte)
845 * Pattern 7 is ff (1 Byte)
846 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
847 * byte block.
848 *
849 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
850 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
851 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
852 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
853 * Pattern 12 is 00 (1 Byte)
854 * Pattern 13 is ff (1 Byte)
855 * Patterns 8-13 have no block number.
856 *
857 * Patterns 0-3 are created to detect and efficiently diagnose communication
858 * slips like missed bits or bytes and their repetitive nature gives good visual
859 * cues to the person inspecting the results. In addition, the following holds:
860 * AND Pattern 0/1 == Pattern 4
861 * AND Pattern 2/3 == Pattern 5
862 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
863 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
864 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
865 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
866 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
867 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
868 * Besides that, they provide for bit testing of the last two bytes of every
869 * 256 byte block which contains the block number for patterns 0-6.
870 * Patterns 10-11 are special purpose for detecting subblock aliasing with
871 * block sizes >256 bytes (some Dataflash chips etc.)
872 * AND Pattern 8/9 == Pattern 12
873 * AND Pattern 10/11 == Pattern 12
874 * Pattern 13 is the completely erased state.
875 * None of the patterns can detect aliasing at boundaries which are a multiple
876 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
877 */
878int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
879{
880 int i;
881
882 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000883 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000884 return 1;
885 }
886
887 switch (variant) {
888 case 0:
889 for (i = 0; i < size; i++)
890 buf[i] = (i & 0xf) << 4 | 0x5;
891 break;
892 case 1:
893 for (i = 0; i < size; i++)
894 buf[i] = (i & 0xf) << 4 | 0xa;
895 break;
896 case 2:
897 for (i = 0; i < size; i++)
898 buf[i] = 0x50 | (i & 0xf);
899 break;
900 case 3:
901 for (i = 0; i < size; i++)
902 buf[i] = 0xa0 | (i & 0xf);
903 break;
904 case 4:
905 for (i = 0; i < size; i++)
906 buf[i] = (i & 0xf) << 4;
907 break;
908 case 5:
909 for (i = 0; i < size; i++)
910 buf[i] = i & 0xf;
911 break;
912 case 6:
913 memset(buf, 0x00, size);
914 break;
915 case 7:
916 memset(buf, 0xff, size);
917 break;
918 case 8:
919 for (i = 0; i < size; i++)
920 buf[i] = i & 0xff;
921 break;
922 case 9:
923 for (i = 0; i < size; i++)
924 buf[i] = ~(i & 0xff);
925 break;
926 case 10:
927 for (i = 0; i < size % 2; i++) {
928 buf[i * 2] = (i >> 8) & 0xff;
929 buf[i * 2 + 1] = i & 0xff;
930 }
931 if (size & 0x1)
932 buf[i * 2] = (i >> 8) & 0xff;
933 break;
934 case 11:
935 for (i = 0; i < size % 2; i++) {
936 buf[i * 2] = ~((i >> 8) & 0xff);
937 buf[i * 2 + 1] = ~(i & 0xff);
938 }
939 if (size & 0x1)
940 buf[i * 2] = ~((i >> 8) & 0xff);
941 break;
942 case 12:
943 memset(buf, 0x00, size);
944 break;
945 case 13:
946 memset(buf, 0xff, size);
947 break;
948 }
949
950 if ((variant >= 0) && (variant <= 7)) {
951 /* Write block number in the last two bytes of each 256-byte
952 * block, big endian for easier reading of the hexdump.
953 * Note that this wraps around for chips larger than 2^24 bytes
954 * (16 MB).
955 */
956 for (i = 0; i < size / 256; i++) {
957 buf[i * 256 + 254] = (i >> 8) & 0xff;
958 buf[i * 256 + 255] = i & 0xff;
959 }
960 }
961
962 return 0;
963}
964
hailfingeraec9c962009-10-31 01:53:09 +0000965int check_max_decode(enum chipbustype buses, uint32_t size)
966{
967 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +0000968
969 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000970 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000971 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000972 "size %u kB of chipset/board/programmer "
973 "for %s interface, "
974 "probe/read/erase/write may fail. ", size / 1024,
975 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +0000976 }
hailfingere1e41ea2011-07-27 07:13:06 +0000977 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000978 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000979 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000980 "size %u kB of chipset/board/programmer "
981 "for %s interface, "
982 "probe/read/erase/write may fail. ", size / 1024,
983 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +0000984 }
hailfingere1e41ea2011-07-27 07:13:06 +0000985 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000986 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000987 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000988 "size %u kB of chipset/board/programmer "
989 "for %s interface, "
990 "probe/read/erase/write may fail. ", size / 1024,
991 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +0000992 }
hailfingere1e41ea2011-07-27 07:13:06 +0000993 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000994 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000995 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000996 "size %u kB of chipset/board/programmer "
997 "for %s interface, "
998 "probe/read/erase/write may fail. ", size / 1024,
999 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001000 }
1001 if (!limitexceeded)
1002 return 0;
1003 /* Sometimes chip and programmer have more than one bus in common,
1004 * and the limit is not exceeded on all buses. Tell the user.
1005 */
1006 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001007 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001008 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001009 "interface which can support a chip of this size. "
1010 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001011 return 1;
1012}
1013
hailfinger48ed3e22011-05-04 00:39:50 +00001014int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001015{
hailfinger48ed3e22011-05-04 00:39:50 +00001016 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001017 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001018 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001019 uint32_t size;
1020 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001021 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001022
hailfinger48ed3e22011-05-04 00:39:50 +00001023 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001024 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001025 continue;
hailfingeraec9c962009-10-31 01:53:09 +00001026 buses_common = buses_supported & flash->bustype;
1027 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001028 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1029 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +00001030 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +00001031 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001032 free(tmp);
1033 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001034 msg_gspew("and chip bus type %s are incompatible.",
1035 tmp);
hailfingera916b422009-06-01 02:08:58 +00001036 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001037 msg_gspew("\n");
1038 continue;
1039 }
1040 msg_gdbg("Probing for %s %s, %d kB: ",
1041 flash->vendor, flash->name, flash->total_size);
1042 if (!flash->probe && !force) {
1043 msg_gdbg("failed! flashrom has no probe function for "
1044 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001045 continue;
1046 }
stepan782fb172007-04-06 11:58:03 +00001047
ollie5672ac62004-03-17 22:22:08 +00001048 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001049 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001050
hailfinger48ed3e22011-05-04 00:39:50 +00001051 /* Start filling in the dynamic data. */
1052 *fill_flash = *flash;
1053
hailfinger72d3b982009-05-09 07:27:23 +00001054 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001055 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001056
stugec1e55fe2008-07-02 17:15:47 +00001057 if (force)
1058 break;
stepanc98b80b2006-03-16 16:57:41 +00001059
hailfinger48ed3e22011-05-04 00:39:50 +00001060 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001061 goto notfound;
1062
hailfinger48ed3e22011-05-04 00:39:50 +00001063 /* If this is the first chip found, accept it.
1064 * If this is not the first chip found, accept it only if it is
1065 * a non-generic match.
1066 * We could either make chipcount global or provide it as
1067 * parameter, or we assume that startchip==0 means this call to
1068 * probe_flash() is the first one and thus no chip has been
1069 * found before.
1070 */
1071 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001072 break;
1073
stuge56300c32008-09-03 23:10:05 +00001074notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001075 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001076 }
uwebe4477b2007-08-23 16:08:21 +00001077
stugec1e55fe2008-07-02 17:15:47 +00001078 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001079 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001080
hailfingere11396b2011-03-08 00:09:11 +00001081#if CONFIG_INTERNAL == 1
1082 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001083 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001084 else
1085#endif
stepan3e7aeae2011-01-19 06:21:54 +00001086 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001087
stefanct588b6d22011-06-26 20:45:35 +00001088 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001089 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1090 force ? "Assuming" : "Found", fill_flash->vendor,
1091 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001092 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001093
hailfinger0f4c3952010-12-02 21:59:42 +00001094 /* Flash registers will not be mapped if the chip was forced. Lock info
1095 * may be stored in registers, so avoid lock info printing.
1096 */
1097 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001098 if (fill_flash->printlock)
1099 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001100
hailfinger48ed3e22011-05-04 00:39:50 +00001101 /* Return position of matching chip. */
1102 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001103}
1104
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001105int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001106{
hailfingerb0f4d122009-06-24 08:20:45 +00001107 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001108 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001109
snelsone42c3802010-05-07 20:09:04 +00001110 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001111
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001112 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1113 ret = handle_partial_verify(flash, buf, verify_range);
1114 } else {
1115 ret = verify_range(flash, buf, 0, total_size, NULL);
1116 }
uwef6641642007-05-09 10:17:44 +00001117
David Hendricks1ed1d352011-11-23 17:54:37 -08001118 if (ret == ACCESS_DENIED) {
1119 msg_gdbg("Could not fully verify due to access error, ");
1120 if (access_denied_action == error_ignore) {
1121 msg_gdbg("ignoring\n");
1122 ret = 0;
1123 } else {
1124 msg_gdbg("aborting\n");
1125 }
1126 }
1127
hailfingerb0f4d122009-06-24 08:20:45 +00001128 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001129 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001130
hailfingerb0f4d122009-06-24 08:20:45 +00001131 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001132}
1133
uwe8d342eb2011-07-28 08:13:25 +00001134int read_buf_from_file(unsigned char *buf, unsigned long size,
1135 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001136{
1137 unsigned long numbytes;
1138 FILE *image;
1139 struct stat image_stat;
1140
Vincent Palatin7ab23932014-10-01 12:09:16 -07001141 if (!strncmp(filename, "-", sizeof("-")))
1142 image = fdopen(STDIN_FILENO, "rb");
1143 else
1144 image = fopen(filename, "rb");
1145 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001146 perror(filename);
1147 return 1;
1148 }
1149 if (fstat(fileno(image), &image_stat) != 0) {
1150 perror(filename);
1151 fclose(image);
1152 return 1;
1153 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001154 if ((image_stat.st_size != size) &&
1155 (strncmp(filename, "-", sizeof("-")))) {
hailfinger771fc182010-10-15 00:01:14 +00001156 msg_gerr("Error: Image size doesn't match\n");
1157 fclose(image);
1158 return 1;
1159 }
1160 numbytes = fread(buf, 1, size, image);
1161 if (fclose(image)) {
1162 perror(filename);
1163 return 1;
1164 }
1165 if (numbytes != size) {
1166 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1167 "wanted %ld!\n", numbytes, size);
1168 return 1;
1169 }
1170 return 0;
1171}
1172
uwe8d342eb2011-07-28 08:13:25 +00001173int write_buf_to_file(unsigned char *buf, unsigned long size,
1174 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001175{
1176 unsigned long numbytes;
1177 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001178
1179 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001180 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001181 return 1;
1182 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001183 if (!strncmp(filename, "-", sizeof("-")))
1184 image = fdopen(STDOUT_FILENO, "wb");
1185 else
1186 image = fopen(filename, "wb");
1187 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001188 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001189 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001190 }
hailfingerd219a232009-01-28 00:27:54 +00001191
hailfingerd219a232009-01-28 00:27:54 +00001192 numbytes = fwrite(buf, 1, size, image);
1193 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001194 if (numbytes != size) {
1195 msg_gerr("File %s could not be written completely.\n",
1196 filename);
hailfingerd219a232009-01-28 00:27:54 +00001197 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001198 }
hailfingerd219a232009-01-28 00:27:54 +00001199 return 0;
1200}
1201
David Hendrickse3451942013-03-21 17:23:29 -07001202/*
1203 * read_flash - wrapper for flash->read() with additional high-level policy
1204 *
1205 * @flash flash chip
1206 * @buf buffer to store data in
1207 * @start start address
1208 * @len number of bytes to read
1209 *
1210 * This wrapper simplifies most cases when the flash chip needs to be read
1211 * since policy decisions such as non-fatal error handling is centralized.
1212 */
1213int read_flash(struct flashchip *flash, uint8_t *buf,
1214 unsigned int start, unsigned int len)
1215{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001216 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001217
1218 if (!flash || !flash->read)
1219 return -1;
1220
David Hendricks4e76fdc2013-05-13 16:05:36 -07001221 ret = flash->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001222 if (ret) {
1223 if (ignore_error(ret)) {
1224 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1225 start, start + len - 1);
1226 ret = 0;
1227 } else {
1228 msg_gdbg("failed to read 0x%x-0x%x\n",
1229 start, start + len - 1);
1230 }
1231 }
1232
1233 return ret;
1234}
1235
David Hendricks7c8a1612013-04-26 19:14:44 -07001236/*
1237 * write_flash - wrapper for flash->write() with additional high-level policy
1238 *
1239 * @flash flash chip
1240 * @buf buffer to write to flash
1241 * @start start address in flash
1242 * @len number of bytes to write
1243 *
1244 * TODO: Look up regions that are write-protected and avoid attempt to write
1245 * to them at all.
1246 */
1247int write_flash(struct flashchip *flash, uint8_t *buf,
1248 unsigned int start, unsigned int len)
1249{
1250 if (!flash || !flash->write)
1251 return -1;
1252
1253 return flash->write(flash, buf, start, len);
1254}
1255
stefanct52700282011-06-26 17:38:17 +00001256int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001257{
1258 unsigned long size = flash->total_size * 1024;
1259 unsigned char *buf = calloc(size, sizeof(char));
1260 int ret = 0;
1261
1262 msg_cinfo("Reading flash... ");
1263 if (!buf) {
1264 msg_gerr("Memory allocation failed!\n");
1265 msg_cinfo("FAILED.\n");
1266 return 1;
1267 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001268
1269 /* To support partial read, fill buffer to all 0xFF at beginning to make
1270 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001271 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001272
hailfinger42a850a2010-07-13 23:56:13 +00001273 if (!flash->read) {
1274 msg_cerr("No read function available for this flash chip.\n");
1275 ret = 1;
1276 goto out_free;
1277 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001278
1279 /* First try to handle partial read case, rather than read the whole
1280 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001281 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001282 if (ret < 0) {
1283 msg_cerr("Partial read operation failed!\n");
1284 ret = 1;
1285 goto out_free;
1286 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001287 int num_regions = get_num_include_args();
1288
1289 if (ret != num_regions) {
1290 msg_cerr("Requested %d regions, but only read %d\n",
1291 num_regions, ret);
1292 ret = 1;
1293 goto out_free;
1294 }
1295
1296 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001297 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001298 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001299 msg_cerr("Read operation failed!\n");
1300 ret = 1;
1301 goto out_free;
1302 }
hailfinger42a850a2010-07-13 23:56:13 +00001303 }
1304
David Hendricksdf29a832013-06-28 14:33:51 -07001305 if (filename)
1306 ret = write_buf_to_file(buf, size, filename);
1307
hailfinger42a850a2010-07-13 23:56:13 +00001308out_free:
1309 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001310 if (ret)
1311 msg_cerr("FAILED.");
1312 else
1313 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001314 return ret;
1315}
1316
hailfingerb437e282010-11-04 01:04:27 +00001317/* This function shares a lot of its structure with erase_and_write_flash() and
1318 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001319 * Even if an error is found, the function will keep going and check the rest.
1320 */
hailfinger48ed3e22011-05-04 00:39:50 +00001321static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001322{
hailfingerb91c08c2011-08-15 19:54:20 +00001323 int i, j, k;
1324 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001325
1326 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1327 unsigned int done = 0;
1328 struct block_eraser eraser = flash->block_erasers[k];
1329
1330 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1331 /* Blocks with zero size are bugs in flashchips.c. */
1332 if (eraser.eraseblocks[i].count &&
1333 !eraser.eraseblocks[i].size) {
1334 msg_gerr("ERROR: Flash chip %s erase function "
1335 "%i region %i has size 0. Please report"
1336 " a bug at flashrom@flashrom.org\n",
1337 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001338 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001339 }
1340 /* Blocks with zero count are bugs in flashchips.c. */
1341 if (!eraser.eraseblocks[i].count &&
1342 eraser.eraseblocks[i].size) {
1343 msg_gerr("ERROR: Flash chip %s erase function "
1344 "%i region %i has count 0. Please report"
1345 " a bug at flashrom@flashrom.org\n",
1346 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001347 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001348 }
1349 done += eraser.eraseblocks[i].count *
1350 eraser.eraseblocks[i].size;
1351 }
hailfinger9fed35d2010-01-19 06:42:46 +00001352 /* Empty eraseblock definition with erase function. */
1353 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001354 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001355 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001356 if (!done)
1357 continue;
1358 if (done != flash->total_size * 1024) {
1359 msg_gerr("ERROR: Flash chip %s erase function %i "
1360 "region walking resulted in 0x%06x bytes total,"
1361 " expected 0x%06x bytes. Please report a bug at"
1362 " flashrom@flashrom.org\n", flash->name, k,
1363 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001364 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001365 }
hailfinger9fed35d2010-01-19 06:42:46 +00001366 if (!eraser.block_erase)
1367 continue;
1368 /* Check if there are identical erase functions for different
1369 * layouts. That would imply "magic" erase functions. The
1370 * easiest way to check this is with function pointers.
1371 */
uwef6f94d42010-03-13 17:28:29 +00001372 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001373 if (eraser.block_erase ==
1374 flash->block_erasers[j].block_erase) {
1375 msg_gerr("ERROR: Flash chip %s erase function "
1376 "%i and %i are identical. Please report"
1377 " a bug at flashrom@flashrom.org\n",
1378 flash->name, k, j);
1379 ret = 1;
1380 }
uwef6f94d42010-03-13 17:28:29 +00001381 }
hailfinger45177872010-01-18 08:14:43 +00001382 }
hailfinger9fed35d2010-01-19 06:42:46 +00001383 return ret;
hailfinger45177872010-01-18 08:14:43 +00001384}
1385
hailfingerb437e282010-11-04 01:04:27 +00001386static int erase_and_write_block_helper(struct flashchip *flash,
1387 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001388 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001389 uint8_t *newcontents,
1390 int (*erasefn) (struct flashchip *flash,
1391 unsigned int addr,
1392 unsigned int len))
1393{
stefanctc5eb8a92011-11-23 09:13:48 +00001394 unsigned int starthere = 0, lenhere = 0;
1395 int ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001396 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1397
hailfinger90fcf9b2010-11-05 14:51:59 +00001398 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001399 * need to be adjusted here to keep the impression of proper abstraction
1400 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001401 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001402 newcontents += start;
1403 msg_cdbg(":");
1404 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001405 if (need_erase(flash, curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001406 msg_cdbg("E");
1407 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001408 if (ret) {
1409 if (ret == ACCESS_DENIED)
1410 msg_cdbg("D");
1411 else
1412 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001413 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001414 }
1415
hailfingerac8e3182011-06-26 17:04:16 +00001416 if (check_erased_range(flash, start, len)) {
1417 msg_cerr("ERASE FAILED!\n");
1418 return -1;
1419 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001420 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001421 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001422 skip = 0;
1423 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001424 /* get_next_write() sets starthere to a new value after the call. */
1425 while ((lenhere = get_next_write(curcontents + starthere,
1426 newcontents + starthere,
1427 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001428 if (!writecount++)
1429 msg_cdbg("W");
1430 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001431 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001432 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001433 if (ret) {
1434 if (ret == ACCESS_DENIED)
1435 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001436 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001437 }
hailfingerb437e282010-11-04 01:04:27 +00001438 starthere += lenhere;
1439 skip = 0;
1440 }
1441 if (skip)
1442 msg_cdbg("S");
1443 return ret;
1444}
1445
hailfinger83541b32010-07-13 00:42:00 +00001446static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1447 int (*do_something) (struct flashchip *flash,
1448 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001449 unsigned int len,
1450 uint8_t *param1,
1451 uint8_t *param2,
1452 int (*erasefn) (
1453 struct flashchip *flash,
1454 unsigned int addr,
1455 unsigned int len)),
1456 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001457{
David Hendricks1ed1d352011-11-23 17:54:37 -08001458 int i, j, rc = 0;
hailfingerb91c08c2011-08-15 19:54:20 +00001459 unsigned int start = 0;
1460 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001461 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001462
hailfinger2b8c9382010-07-13 00:37:19 +00001463 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1464 /* count==0 for all automatically initialized array
1465 * members so the loop below won't be executed for them.
1466 */
1467 len = eraser.eraseblocks[i].size;
1468 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001469 /* Print this for every block except the first one. */
1470 if (i || j)
1471 msg_cdbg(", ");
1472 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001473 start + len - 1);
David Hendricks1ed1d352011-11-23 17:54:37 -08001474 rc = do_something(flash, start, len, param1, param2,
1475 eraser.block_erase);
1476 if (rc) {
1477 if (ignore_error(rc))
1478 rc = 0;
1479 else
1480 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001481 }
hailfinger2b8c9382010-07-13 00:37:19 +00001482 start += len;
1483 }
1484 }
hailfingerb437e282010-11-04 01:04:27 +00001485 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001486 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001487}
1488
stefanct569dbb62011-07-01 00:19:12 +00001489static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001490{
1491 struct block_eraser eraser = flash->block_erasers[k];
1492
1493 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1494 if (log)
1495 msg_cdbg("not defined. ");
1496 return 1;
1497 }
1498 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1499 if (log)
1500 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001501 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001502 return 1;
1503 }
1504 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1505 if (log)
1506 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001507 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001508 return 1;
1509 }
1510 return 0;
1511}
1512
uwe8d342eb2011-07-28 08:13:25 +00001513int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1514 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001515{
hailfinger8f524a82011-07-21 21:21:04 +00001516 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001517 uint8_t *curcontents;
1518 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001519 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001520
hailfingercf848f12010-12-05 15:14:44 +00001521 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001522 curcontents = malloc(size);
1523 if (!curcontents) {
1524 msg_gerr("Out of memory!\n");
1525 exit(1);
1526 }
hailfingerb437e282010-11-04 01:04:27 +00001527 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1528 memcpy(curcontents, oldcontents, size);
1529
hailfinger7df21362009-09-05 02:30:58 +00001530 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001531 if (k != 0)
1532 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001533 if (!usable_erasefunctions) {
1534 msg_cdbg("No usable erase functions left.\n");
1535 break;
1536 }
stefanctf0bc4712011-07-26 14:28:35 +00001537 msg_cdbg("Trying erase function %i... ", k);
1538 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001539 continue;
hailfingercf848f12010-12-05 15:14:44 +00001540 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001541 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1542 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001543 /* If everything is OK, don't try another erase function. */
1544 if (!ret)
1545 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001546 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001547 * contents are. If no usable erase functions remain, we can
1548 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001549 */
hailfingercf848f12010-12-05 15:14:44 +00001550 if (!usable_erasefunctions)
1551 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001552 /* Reading the whole chip may take a while, inform the user even
1553 * in non-verbose mode.
1554 */
1555 msg_cinfo("Reading current flash chip contents... ");
David Hendrickse3451942013-03-21 17:23:29 -07001556 if (read_flash(flash, curcontents, 0, size)) {
hailfinger6237f5e2010-12-02 02:41:55 +00001557 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001558 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001559 /* We have no idea about the flash chip contents, so
1560 * retrying with another erase function is pointless.
1561 */
1562 break;
1563 }
David Hendricks89a45e52011-11-22 16:56:22 -08001564 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001565 }
hailfingerb437e282010-11-04 01:04:27 +00001566 /* Free the scratchpad. */
1567 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001568
hailfinger7df21362009-09-05 02:30:58 +00001569 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001570 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001571 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001572 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001573 }
1574 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001575}
1576
hailfinger4c47e9d2010-10-19 22:06:20 +00001577void nonfatal_help_message(void)
1578{
1579 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1580 "This means we have to add special support for your board, "
1581 "programmer or flash chip.\n"
1582 "Please report this on IRC at irc.freenode.net (channel "
1583 "#flashrom) or\n"
1584 "mail flashrom@flashrom.org!\n"
1585 "-------------------------------------------------------------"
1586 "------------------\n"
1587 "You may now reboot or simply leave the machine running.\n");
1588}
1589
uweb34ec9f2009-10-01 18:40:02 +00001590void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001591{
snelsone42c3802010-05-07 20:09:04 +00001592 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001593 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001594 "mail flashrom@flashrom.org with FAILED: your board name in "
1595 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001596 "-------------------------------------------------------------"
1597 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001598 "DO NOT REBOOT OR POWEROFF!\n");
1599}
1600
uwe8d342eb2011-07-28 08:13:25 +00001601/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001602void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001603{
1604 enum programmer p;
1605 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001606 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001607 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001608 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001609 }
Simon Glass8dc82732013-07-16 10:13:51 -06001610 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001611}
1612
hailfingerf79d1712010-10-06 23:48:34 +00001613void list_programmers_linebreak(int startcol, int cols, int paren)
1614{
1615 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001616 int pnamelen;
1617 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001618 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001619 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001620
1621 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1622 pname = programmer_table[p].name;
1623 pnamelen = strlen(pname);
1624 if (remaining - pnamelen - 2 < 0) {
1625 if (firstline)
1626 firstline = 0;
1627 else
1628 printf("\n");
1629 for (i = 0; i < startcol; i++)
1630 printf(" ");
1631 remaining = cols - startcol;
1632 } else {
1633 printf(" ");
1634 remaining--;
1635 }
1636 if (paren && (p == 0)) {
1637 printf("(");
1638 remaining--;
1639 }
1640 printf("%s", pname);
1641 remaining -= pnamelen;
1642 if (p < PROGRAMMER_INVALID - 1) {
1643 printf(",");
1644 remaining--;
1645 } else {
1646 if (paren)
1647 printf(")");
1648 printf("\n");
1649 }
1650 }
1651}
1652
hailfinger3b471632010-03-27 16:36:40 +00001653void print_sysinfo(void)
1654{
David Hendricksc6c9f822010-11-03 15:07:01 -07001655 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001656#if HAVE_UTSNAME == 1
1657 struct utsname osinfo;
1658 uname(&osinfo);
1659
David Hendricksc6c9f822010-11-03 15:07:01 -07001660 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001661 osinfo.machine);
1662#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001663 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001664#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001665 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001666#if NEED_PCI == 1
1667#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001668 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001669#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001670 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001671#endif
1672#endif
1673#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001674 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001675#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001676 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001677#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001678 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001679#endif
hailfinger3b471632010-03-27 16:36:40 +00001680#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001681 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001682#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001683 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001684#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001685 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001686#endif
1687#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001688 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001689#endif
1690#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001691 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001692#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001693 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001694#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001695 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001696}
1697
uwefdeca092008-01-21 15:24:22 +00001698void print_version(void)
1699{
David Hendricksc6c9f822010-11-03 15:07:01 -07001700 /* send to stderr for chromium os */
1701 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001702 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001703}
1704
hailfinger74819ad2010-05-15 15:04:37 +00001705void print_banner(void)
1706{
1707 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001708 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001709 msg_ginfo("\n");
1710}
1711
hailfingerc77acb52009-12-24 02:15:55 +00001712int selfcheck(void)
1713{
hailfinger45177872010-01-18 08:14:43 +00001714 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001715 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001716
1717 /* Safety check. Instead of aborting after the first error, check
1718 * if more errors exist.
1719 */
hailfingerc77acb52009-12-24 02:15:55 +00001720 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001721 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001722 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001723 }
stefanct8632c922011-07-26 14:33:46 +00001724 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001725 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001726 * For 'flashchips' we check the first element to be non-null. In the
1727 * other cases there exist use cases where the first element can be
1728 * null. */
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001729 if (!flashchips || flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001730 msg_gerr("Flashchips table miscompilation!\n");
1731 ret = 1;
1732 }
hailfinger45177872010-01-18 08:14:43 +00001733 for (flash = flashchips; flash && flash->name; flash++)
1734 if (selfcheck_eraseblocks(flash))
1735 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001736
1737#if CONFIG_INTERNAL == 1
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001738 if (!chipset_enables) {
stefanct6d836ba2011-05-26 01:35:19 +00001739 msg_gerr("Chipset enables table does not exist!\n");
1740 ret = 1;
1741 }
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001742 if (!board_matches) {
stefanct6d836ba2011-05-26 01:35:19 +00001743 msg_gerr("Board enables table does not exist!\n");
1744 ret = 1;
1745 }
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001746 if (!boards_known) {
stefanct6d836ba2011-05-26 01:35:19 +00001747 msg_gerr("Known boards table does not exist!\n");
1748 ret = 1;
1749 }
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001750 if (!laptops_known) {
stefanct6d836ba2011-05-26 01:35:19 +00001751 msg_gerr("Known laptops table does not exist!\n");
1752 ret = 1;
1753 }
uwe8d342eb2011-07-28 08:13:25 +00001754#endif
hailfinger45177872010-01-18 08:14:43 +00001755 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001756}
1757
hailfinger48ed3e22011-05-04 00:39:50 +00001758void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001759{
1760 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001761 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001762 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001763 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001764 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001765 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001766 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001767 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001768 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001769 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001770 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001771 msg_cdbg(" WRITE");
1772 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001773 }
1774 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1775 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1776 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1777 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001778 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001779 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001780 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001781 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001782 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001783 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001784 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001785 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001786 msg_cdbg(" WRITE");
1787 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001788 }
hailfinger92cd8e32010-01-07 03:24:05 +00001789 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001790 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001791 "in the latest development\n"
1792 "version of flashrom. If you are running the latest "
1793 "development version,\n"
1794 "please email a report to flashrom@flashrom.org if "
1795 "any of the above operations\n"
1796 "work correctly for you with this flash part. Please "
1797 "include the flashrom\n"
1798 "output with the additional -V option for all "
1799 "operations you tested (-V, -Vr,\n"
1800 "-Vw, -VE), and mention which mainboard or "
1801 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001802 "Please mention your board in the subject line. "
1803 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001804 }
1805}
1806
hailfinger771fc182010-10-15 00:01:14 +00001807/* FIXME: This function signature needs to be improved once doit() has a better
1808 * function signature.
1809 */
stefanct02116582011-05-18 01:30:56 +00001810int 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 +00001811{
1812 if (!programmer_may_write && (write_it || erase_it)) {
1813 msg_perr("Write/erase is not working yet on your programmer in "
1814 "its current configuration.\n");
1815 /* --force is the wrong approach, but it's the best we can do
1816 * until the generic programmer parameter parser is merged.
1817 */
1818 if (!force)
1819 return 1;
1820 msg_cerr("Continuing anyway.\n");
1821 }
1822
1823 if (read_it || erase_it || write_it || verify_it) {
1824 /* Everything needs read. */
1825 if (flash->tested & TEST_BAD_READ) {
1826 msg_cerr("Read is not working on this chip. ");
1827 if (!force)
1828 return 1;
1829 msg_cerr("Continuing anyway.\n");
1830 }
1831 if (!flash->read) {
1832 msg_cerr("flashrom has no read function for this "
1833 "flash chip.\n");
1834 return 1;
1835 }
1836 }
1837 if (erase_it || write_it) {
1838 /* Write needs erase. */
1839 if (flash->tested & TEST_BAD_ERASE) {
1840 msg_cerr("Erase is not working on this chip. ");
1841 if (!force)
1842 return 1;
1843 msg_cerr("Continuing anyway.\n");
1844 }
stefancte1c5acf2011-07-04 07:27:17 +00001845 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001846 msg_cerr("flashrom has no erase function for this "
1847 "flash chip.\n");
1848 return 1;
1849 }
hailfinger771fc182010-10-15 00:01:14 +00001850 }
1851 if (write_it) {
1852 if (flash->tested & TEST_BAD_WRITE) {
1853 msg_cerr("Write is not working on this chip. ");
1854 if (!force)
1855 return 1;
1856 msg_cerr("Continuing anyway.\n");
1857 }
1858 if (!flash->write) {
1859 msg_cerr("flashrom has no write function for this "
1860 "flash chip.\n");
1861 return 1;
1862 }
1863 }
1864 return 0;
1865}
1866
hailfingerc77acb52009-12-24 02:15:55 +00001867/* This function signature is horrible. We need to design a better interface,
1868 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001869 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001870 */
Simon Glass9ad06c12013-07-03 22:08:17 +09001871int doit(struct flashchip *flash, int force, const char *filename, int read_it,
1872 int write_it, int erase_it, int verify_it, int extract_it,
1873 const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001874{
hailfinger4c47e9d2010-10-19 22:06:20 +00001875 uint8_t *oldcontents;
1876 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001877 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001878 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001879
stefanct02116582011-05-18 01:30:56 +00001880 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001881 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001882 ret = 1;
1883 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001884 }
1885
hailfinger771fc182010-10-15 00:01:14 +00001886 /* Given the existence of read locks, we want to unlock for read,
1887 * erase and write.
1888 */
1889 if (flash->unlock)
1890 flash->unlock(flash);
1891
David Hendricks6d62d752011-03-07 21:20:22 -08001892 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001893 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001894 ret = 1;
1895 goto out_nofree;
1896 }
1897
Simon Glass9ad06c12013-07-03 22:08:17 +09001898 if (extract_it) {
1899 ret = extract_regions(flash);
1900 goto out_nofree;
1901 }
1902
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001903 /* mark entries included using -i argument as "included" if they are
1904 found in the master rom_entries list */
1905 if (process_include_args() < 0) {
1906 ret = 1;
1907 goto out_nofree;
1908 }
1909
hailfinger771fc182010-10-15 00:01:14 +00001910 if (read_it) {
1911 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001912 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001913 }
hailfingerb437e282010-11-04 01:04:27 +00001914
stefanctd611e8f2011-07-12 22:35:21 +00001915 oldcontents = malloc(size);
1916 if (!oldcontents) {
1917 msg_gerr("Out of memory!\n");
1918 exit(1);
1919 }
Simon Glass4c214132013-07-16 10:09:28 -06001920 /* Assume worst case: All blocks are not erased. */
1921 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00001922 newcontents = malloc(size);
1923 if (!newcontents) {
1924 msg_gerr("Out of memory!\n");
1925 exit(1);
1926 }
Simon Glass4c214132013-07-16 10:09:28 -06001927 /* Assume best case: All blocks are erased. */
1928 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00001929 /* Side effect of the assumptions above: Default write action is erase
1930 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06001931 * oldcontents being completely unerased means we have to erase
1932 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00001933 */
1934
ollie0eb62d62004-12-08 20:10:01 +00001935 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001936 /* FIXME: Do we really want the scary warning if erase failed?
1937 * After all, after erase the chip is either blank or partially
1938 * blank or it has the old contents. A blank chip won't boot,
1939 * so if the user wanted erase and reboots afterwards, the user
1940 * knows very well that booting won't work.
1941 */
hailfingerb437e282010-11-04 01:04:27 +00001942 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001943 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001944 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001945 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001946 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001947 }
hailfinger771fc182010-10-15 00:01:14 +00001948
hailfingerd217d122010-10-08 18:52:29 +00001949 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07001950 /*
1951 * Note: This must be done before any files specified by -i
1952 * arguments are processed merged into the newcontents since
1953 * -i files take priority. See http://crbug.com/263495.
1954 */
1955 if (filename) {
1956 if (read_buf_from_file(newcontents, size, filename)) {
1957 ret = 1;
1958 goto out;
1959 }
1960 } else {
1961 /* Content will be read from -i args, so they must
1962 * not overlap. */
1963 if (included_regions_overlap()) {
1964 msg_gerr("Error: Included regions must "
1965 "not overlap.\n");
1966 ret = 1;
1967 goto out;
1968 }
stepan1da96c02006-11-21 23:48:51 +00001969 }
1970
David Hendricksac82cac2012-06-19 10:29:37 -07001971#if 0
1972 /*
1973 * FIXME: show_id() causes failure if vendor:mainboard do not
1974 * match. This may happen if codenames are in flux.
1975 * See chrome-os-partner:10414.
1976 */
hailfinger90c7d542010-05-31 15:27:27 +00001977#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001978 if (programmer == PROGRAMMER_INTERNAL)
1979 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001980#endif
David Hendricksac82cac2012-06-19 10:29:37 -07001981#endif
ollie5672ac62004-03-17 22:22:08 +00001982 }
1983
David Hendricksc44d7a02011-10-17 11:28:43 -07001984 /* Obtain a reference image so that we can check whether regions need
1985 * to be erased and to give better diagnostics in case write fails.
David Hendricks52ddff02013-07-23 15:05:14 -07001986 * If --fast-verify is used then only the regions which are included
1987 * using -i will be read.
hailfinger4c47e9d2010-10-19 22:06:20 +00001988 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001989 if (diff_file) {
1990 msg_cdbg("Reading old contents from file... ");
1991 if (read_buf_from_file(oldcontents, size, diff_file)) {
1992 ret = 1;
1993 msg_cdbg("FAILED.\n");
1994 goto out;
1995 }
1996 } else {
1997 msg_cdbg("Reading old contents from flash chip... ");
David Hendricksd4e712c2013-08-02 17:06:16 -07001998 if (verify_it == VERIFY_PARTIAL) {
1999 if (handle_partial_read(flash, oldcontents,
2000 read_flash, 0) < 0) {
David Hendricks52ddff02013-07-23 15:05:14 -07002001 ret = 1;
2002 msg_cdbg("FAILED.\n");
2003 goto out;
2004 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002005 } else {
2006 if (read_flash(flash, oldcontents, 0, size)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002007 ret = 1;
2008 msg_cdbg("FAILED.\n");
2009 goto out;
2010 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002011 }
hailfinger4c47e9d2010-10-19 22:06:20 +00002012 }
David Hendricks89a45e52011-11-22 16:56:22 -08002013 msg_cdbg("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00002014
David Hendricksdf29a832013-06-28 14:33:51 -07002015 /*
2016 * Note: This must be done after reading the file specified for the
2017 * -w/-v argument, if any, so that files specified using -i end up
2018 * in the "newcontents" buffer before being written.
2019 * See http://crbug.com/263495.
2020 */
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002021 if (handle_romentries(flash, oldcontents, newcontents)) {
2022 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002023 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002024 goto out;
2025 }
uwef6641642007-05-09 10:17:44 +00002026
stuge8ce3a3c2008-04-28 14:47:30 +00002027 if (write_it) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002028 // parse the new fmap
David Hendricksb907de32014-08-11 16:47:09 -07002029 if ((ret = cros_ec_prepare(newcontents, size))) {
2030 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002031 goto out;
2032 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002033
hailfingerb437e282010-11-04 01:04:27 +00002034 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2035 msg_cerr("Uh oh. Erase/write failed. Checking if "
2036 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002037 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002038 if (!memcmp(oldcontents, newcontents, size)) {
2039 msg_cinfo("Good. It seems nothing was "
2040 "changed.\n");
2041 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002042 ret = 1;
2043 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002044 }
2045 }
hailfingerd217d122010-10-08 18:52:29 +00002046 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002047 ret = 1;
2048 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002049 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002050
David Hendricksb907de32014-08-11 16:47:09 -07002051 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002052 if (ret < 0) {
2053 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002054 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002055 emergency_help_message();
2056 ret = 1;
2057 goto out;
2058 } else if (ret > 0) {
2059 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002060 msg_pdbg("CROS_EC needs 2nd pass.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002061 if (read_flash(flash, oldcontents, 0, size)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002062 msg_cerr("Uh oh. Cannot get latest content.\n");
2063 emergency_help_message();
2064 ret = 1;
2065 goto out;
2066 }
2067 // write 2nd pass
2068 if (erase_and_write_flash(flash, oldcontents,
2069 newcontents)) {
David Hendricksb907de32014-08-11 16:47:09 -07002070 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002071 emergency_help_message();
2072 ret = 1;
2073 goto out;
2074 }
2075 ret = 0;
2076 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002077
David Hendricksb907de32014-08-11 16:47:09 -07002078 if (cros_ec_finish() < 0) {
2079 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002080 emergency_help_message();
2081 ret = 1;
2082 goto out;
2083 }
stuge8ce3a3c2008-04-28 14:47:30 +00002084 }
ollie6a600992005-11-26 21:55:36 +00002085
hailfinger0459e1c2009-08-19 13:55:34 +00002086 if (verify_it) {
2087 /* Work around chips which need some time to calm down. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002088 if (write_it && verify_it != VERIFY_PARTIAL)
hailfinger0459e1c2009-08-19 13:55:34 +00002089 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002090
2091 ret = verify_flash(flash, newcontents, verify_it);
2092
hailfingera50d60e2009-11-17 09:57:34 +00002093 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00002094 * might have an emergency situation.
2095 */
2096 if (ret && write_it)
2097 emergency_help_message();
2098 }
ollie6a600992005-11-26 21:55:36 +00002099
hailfinger90fcf9b2010-11-05 14:51:59 +00002100out:
2101 free(oldcontents);
2102 free(newcontents);
2103out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002104 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002105 /*
2106 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2107 * tree. This is because some operations, such as write protection,
2108 * requires programmer_shutdown() but does not call doit().
2109 */
2110// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002111 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002112}