blob: 01d9d32b96e30a30d5559fc5fca50b7fde44ce60 [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 Hendricks54777392015-01-11 18:55:14 -080047unsigned int required_erase_size = 0; /* see comment in flash.h */
48
David Hendricks1ed1d352011-11-23 17:54:37 -080049/* error handling stuff */
50enum error_action access_denied_action = error_ignore;
51
52int ignore_error(int err) {
53 int rc = 0;
54
55 switch(err) {
56 case ACCESS_DENIED:
57 if (access_denied_action == error_ignore)
58 rc = 1;
59 break;
60 default:
61 break;
62 }
63
64 return rc;
65}
66
hailfinger969e2f32011-09-08 00:00:29 +000067static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000068
hailfingerddeb4ac2010-07-08 10:13:37 +000069static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000070
hailfinger1ff33dc2010-07-03 11:02:10 +000071/* Supported buses for the current programmer. */
72enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +000073
uwee15beb92010-08-08 17:01:18 +000074/*
hailfinger80422e22009-12-13 22:28:00 +000075 * Programmers supporting multiple buses can have differing size limits on
76 * each bus. Store the limits for each bus in a common struct.
77 */
hailfinger1ff33dc2010-07-03 11:02:10 +000078struct decode_sizes max_rom_decode;
79
80/* If nonzero, used as the start address of bottom-aligned flash. */
81unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000082
hailfinger5828baf2010-07-03 12:14:25 +000083/* Is writing allowed with this programmer? */
84int programmer_may_write;
85
hailfingerabe249e2009-05-08 17:43:22 +000086const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000087#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000088 {
hailfinger3548a9a2009-08-12 14:34:35 +000089 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000090 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000091 .map_flash_region = physmap,
92 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000093 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +000094 },
hailfinger80422e22009-12-13 22:28:00 +000095#endif
stepan927d4e22007-04-04 22:45:58 +000096
hailfinger90c7d542010-05-31 15:27:27 +000097#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +000098 {
hailfinger3548a9a2009-08-12 14:34:35 +000099 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000100 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000101 .map_flash_region = dummy_map,
102 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000103 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000104 },
hailfinger571a6b32009-09-16 10:09:21 +0000105#endif
hailfingera9df33c2009-05-09 00:54:55 +0000106
hailfinger90c7d542010-05-31 15:27:27 +0000107#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000108 {
hailfinger3548a9a2009-08-12 14:34:35 +0000109 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000110 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000111 .map_flash_region = fallback_map,
112 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000113 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000114 },
hailfinger571a6b32009-09-16 10:09:21 +0000115#endif
uwe0f5a3a22009-05-13 11:36:06 +0000116
hailfinger90c7d542010-05-31 15:27:27 +0000117#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000118 {
hailfinger0d703d42011-03-07 01:08:09 +0000119 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000120 .name = "nicrealtek",
121 //.name = "nicsmc1211",
122 .init = nicrealtek_init,
123 .map_flash_region = fallback_map,
124 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000125 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000126 },
hailfinger5aa36982010-05-21 21:54:07 +0000127#endif
128
hailfingerf0a368f2010-06-07 22:37:54 +0000129#if CONFIG_NICNATSEMI == 1
130 {
uwe8d342eb2011-07-28 08:13:25 +0000131 .name = "nicnatsemi",
132 .init = nicnatsemi_init,
133 .map_flash_region = fallback_map,
134 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000135 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000136 },
137#endif
hailfinger5aa36982010-05-21 21:54:07 +0000138
hailfinger90c7d542010-05-31 15:27:27 +0000139#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000140 {
141 .name = "gfxnvidia",
142 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000143 .map_flash_region = fallback_map,
144 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000145 .delay = internal_delay,
146 },
147#endif
148
hailfinger90c7d542010-05-31 15:27:27 +0000149#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000150 {
uwee2f95ef2009-09-02 23:00:46 +0000151 .name = "drkaiser",
152 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000153 .map_flash_region = fallback_map,
154 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000155 .delay = internal_delay,
156 },
hailfinger571a6b32009-09-16 10:09:21 +0000157#endif
uwee2f95ef2009-09-02 23:00:46 +0000158
hailfinger90c7d542010-05-31 15:27:27 +0000159#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000160 {
hailfinger3548a9a2009-08-12 14:34:35 +0000161 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000162 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000163 .map_flash_region = fallback_map,
164 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000165 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000166 },
hailfinger571a6b32009-09-16 10:09:21 +0000167#endif
ruikda922a12009-05-17 19:39:27 +0000168
hailfinger90c7d542010-05-31 15:27:27 +0000169#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000170 {
171 .name = "atahpt",
172 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000173 .map_flash_region = fallback_map,
174 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000175 .delay = internal_delay,
176 },
177#endif
178
hailfinger90c7d542010-05-31 15:27:27 +0000179#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000180 {
hailfinger90c7d542010-05-31 15:27:27 +0000181 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000182 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000183 .map_flash_region = fallback_map,
184 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000185 .delay = internal_delay,
186 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000187#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000188
hailfinger90c7d542010-05-31 15:27:27 +0000189#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000190 {
hailfinger3548a9a2009-08-12 14:34:35 +0000191 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000192 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000193 .map_flash_region = fallback_map,
194 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000195 .delay = serprog_delay,
196 },
hailfinger74d88a72009-08-12 16:17:41 +0000197#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000198
hailfinger90c7d542010-05-31 15:27:27 +0000199#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000200 {
hailfinger90c7d542010-05-31 15:27:27 +0000201 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000202 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000203 .map_flash_region = fallback_map,
204 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000205 .delay = internal_delay,
206 },
207#endif
208
Anton Staafb2647882014-09-17 15:13:43 -0700209#if CONFIG_RAIDEN_DEBUG_SPI == 1
210 {
211 .name = "raiden_debug_spi",
212 .init = raiden_debug_spi_init,
213 .map_flash_region = fallback_map,
214 .unmap_flash_region = fallback_unmap,
215 .delay = internal_delay,
216 },
217#endif
218
hailfinger90c7d542010-05-31 15:27:27 +0000219#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000220 {
221 .name = "dediprog",
222 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000223 .map_flash_region = fallback_map,
224 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000225 .delay = internal_delay,
226 },
227#endif
228
hailfinger52c4fa02010-07-21 10:26:01 +0000229#if CONFIG_RAYER_SPI == 1
230 {
231 .name = "rayer_spi",
232 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000233 .map_flash_region = fallback_map,
234 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000235 .delay = internal_delay,
236 },
237#endif
238
hailfinger7949b652011-05-08 00:24:18 +0000239#if CONFIG_NICINTEL == 1
240 {
241 .name = "nicintel",
242 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000243 .map_flash_region = fallback_map,
244 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000245 .delay = internal_delay,
246 },
247#endif
248
uwe6764e922010-09-03 18:21:21 +0000249#if CONFIG_NICINTEL_SPI == 1
250 {
uwe8d342eb2011-07-28 08:13:25 +0000251 .name = "nicintel_spi",
252 .init = nicintel_spi_init,
253 .map_flash_region = fallback_map,
254 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000255 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000256 },
257#endif
258
hailfingerfb1f31f2010-12-03 14:48:11 +0000259#if CONFIG_OGP_SPI == 1
260 {
uwe8d342eb2011-07-28 08:13:25 +0000261 .name = "ogp_spi",
262 .init = ogp_spi_init,
263 .map_flash_region = fallback_map,
264 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000265 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000266 },
267#endif
268
hailfinger935365d2011-02-04 21:37:59 +0000269#if CONFIG_SATAMV == 1
270 {
271 .name = "satamv",
272 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000273 .map_flash_region = fallback_map,
274 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000275 .delay = internal_delay,
276 },
277#endif
278
uwe7df6dda2011-09-03 18:37:52 +0000279#if CONFIG_LINUX_SPI == 1
280 {
281 .name = "linux_spi",
282 .init = linux_spi_init,
283 .map_flash_region = fallback_map,
284 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000285 .delay = internal_delay,
286 },
287#endif
288
hailfinger3548a9a2009-08-12 14:34:35 +0000289 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000290};
stepan927d4e22007-04-04 22:45:58 +0000291
David Hendricksbf36f092010-11-02 23:39:29 -0700292#define CHIP_RESTORE_MAXFN 4
293static int chip_restore_fn_count = 0;
294struct chip_restore_func_data {
295 CHIP_RESTORE_CALLBACK;
296 struct flashchip *flash;
297 uint8_t status;
298} static chip_restore_fn[CHIP_RESTORE_MAXFN];
299
David Hendricks668f29d2011-01-27 18:51:45 -0800300
hailfingerf31cbdc2010-11-10 15:25:18 +0000301#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000302static int shutdown_fn_count = 0;
303struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000304 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000305 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000306} static shutdown_fn[SHUTDOWN_MAXFN];
307/* Initialize to 0 to make sure nobody registers a shutdown function before
308 * programmer init.
309 */
310static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000311
stefanct569dbb62011-07-01 00:19:12 +0000312static int check_block_eraser(const struct flashchip *flash, int k, int log);
313
hailfingerdc6f7972010-02-14 01:20:28 +0000314/* Register a function to be executed on programmer shutdown.
315 * The advantage over atexit() is that you can supply a void pointer which will
316 * be used as parameter to the registered function upon programmer shutdown.
317 * This pointer can point to arbitrary data used by said function, e.g. undo
318 * information for GPIO settings etc. If unneeded, set data=NULL.
319 * Please note that the first (void *data) belongs to the function signature of
320 * the function passed as first parameter.
321 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000322int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000323{
324 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000325 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000326 SHUTDOWN_MAXFN);
327 return 1;
328 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000329 if (!may_register_shutdown) {
330 msg_perr("Tried to register a shutdown function before "
331 "programmer init.\n");
332 return 1;
333 }
hailfingerdc6f7972010-02-14 01:20:28 +0000334 shutdown_fn[shutdown_fn_count].func = function;
335 shutdown_fn[shutdown_fn_count].data = data;
336 shutdown_fn_count++;
337
338 return 0;
339}
340
David Hendricksbf36f092010-11-02 23:39:29 -0700341//int register_chip_restore(int (*function) (void *data), void *data)
342int register_chip_restore(CHIP_RESTORE_CALLBACK,
343 struct flashchip *flash, uint8_t status)
344{
345 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
346 msg_perr("Tried to register more than %i chip restore"
347 " functions.\n", CHIP_RESTORE_MAXFN);
348 return 1;
349 }
350 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
351 chip_restore_fn[chip_restore_fn_count].flash = flash;
352 chip_restore_fn[chip_restore_fn_count].status = status;
353 chip_restore_fn_count++;
354
355 return 0;
356}
357
hailfinger969e2f32011-09-08 00:00:29 +0000358int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000359{
hailfinger1ef766d2010-07-06 09:55:48 +0000360 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000361
362 if (prog >= PROGRAMMER_INVALID) {
363 msg_perr("Invalid programmer specified!\n");
364 return -1;
365 }
366 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000367 /* Initialize all programmer specific data. */
368 /* Default to unlimited decode sizes. */
369 max_rom_decode = (const struct decode_sizes) {
370 .parallel = 0xffffffff,
371 .lpc = 0xffffffff,
372 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000373 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000374 };
hailfingere1e41ea2011-07-27 07:13:06 +0000375 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000376 /* Default to top aligned flash at 4 GB. */
377 flashbase = 0;
378 /* Registering shutdown functions is now allowed. */
379 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000380 /* Default to allowing writes. Broken programmers set this to 0. */
381 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000382
383 programmer_param = param;
384 msg_pdbg("Initializing %s programmer\n",
385 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000386 ret = programmer_table[programmer].init();
387 if (programmer_param && strlen(programmer_param)) {
388 msg_perr("Unhandled programmer parameters: %s\n",
389 programmer_param);
390 /* Do not error out here, the init itself was successful. */
391 }
392 return ret;
uweabe92a52009-05-16 22:36:00 +0000393}
394
David Hendricksbf36f092010-11-02 23:39:29 -0700395int chip_restore()
396{
397 int rc = 0;
398
399 while (chip_restore_fn_count > 0) {
400 int i = --chip_restore_fn_count;
401 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
402 chip_restore_fn[i].status);
403 }
404
405 return rc;
406}
407
uweabe92a52009-05-16 22:36:00 +0000408int programmer_shutdown(void)
409{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000410 int ret = 0;
411
hailfinger1ff33dc2010-07-03 11:02:10 +0000412 /* Registering shutdown functions is no longer allowed. */
413 may_register_shutdown = 0;
414 while (shutdown_fn_count > 0) {
415 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000416 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000417 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000418 return ret;
uweabe92a52009-05-16 22:36:00 +0000419}
420
421void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
422 size_t len)
423{
424 return programmer_table[programmer].map_flash_region(descr,
425 phys_addr, len);
426}
427
428void programmer_unmap_flash_region(void *virt_addr, size_t len)
429{
430 programmer_table[programmer].unmap_flash_region(virt_addr, len);
431}
432
433void chip_writeb(uint8_t val, chipaddr addr)
434{
hailfinger76bb7e92011-11-09 23:40:00 +0000435 par_programmer->chip_writeb(val, addr);
uweabe92a52009-05-16 22:36:00 +0000436}
437
438void chip_writew(uint16_t val, chipaddr addr)
439{
hailfinger76bb7e92011-11-09 23:40:00 +0000440 par_programmer->chip_writew(val, addr);
uweabe92a52009-05-16 22:36:00 +0000441}
442
443void chip_writel(uint32_t val, chipaddr addr)
444{
hailfinger76bb7e92011-11-09 23:40:00 +0000445 par_programmer->chip_writel(val, addr);
uweabe92a52009-05-16 22:36:00 +0000446}
447
hailfinger9d987ef2009-06-05 18:32:07 +0000448void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
449{
hailfinger76bb7e92011-11-09 23:40:00 +0000450 par_programmer->chip_writen(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000451}
452
uweabe92a52009-05-16 22:36:00 +0000453uint8_t chip_readb(const chipaddr addr)
454{
hailfinger76bb7e92011-11-09 23:40:00 +0000455 return par_programmer->chip_readb(addr);
uweabe92a52009-05-16 22:36:00 +0000456}
457
458uint16_t chip_readw(const chipaddr addr)
459{
hailfinger76bb7e92011-11-09 23:40:00 +0000460 return par_programmer->chip_readw(addr);
uweabe92a52009-05-16 22:36:00 +0000461}
462
463uint32_t chip_readl(const chipaddr addr)
464{
hailfinger76bb7e92011-11-09 23:40:00 +0000465 return par_programmer->chip_readl(addr);
uweabe92a52009-05-16 22:36:00 +0000466}
467
hailfinger9d987ef2009-06-05 18:32:07 +0000468void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
469{
hailfinger76bb7e92011-11-09 23:40:00 +0000470 par_programmer->chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000471}
472
hailfingere5829f62009-06-05 17:48:08 +0000473void programmer_delay(int usecs)
474{
475 programmer_table[programmer].delay(usecs);
476}
477
stuge5ff0e6c2009-01-26 00:39:57 +0000478void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000479{
stepan15e64bc2007-05-24 08:48:10 +0000480 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000481 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000482 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000483 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000484}
485
stefanctc5eb8a92011-11-23 09:13:48 +0000486int read_memmapped(struct flashchip *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000487{
hailfinger0f08b7a2009-06-16 08:55:44 +0000488 chip_readn(buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000489
hailfinger23060112009-05-08 12:49:03 +0000490 return 0;
491}
492
hailfinger7b414742009-06-13 12:04:03 +0000493int min(int a, int b)
494{
495 return (a < b) ? a : b;
496}
497
hailfinger7af83692009-06-15 17:23:36 +0000498int max(int a, int b)
499{
500 return (a > b) ? a : b;
501}
502
hailfingeraec9c962009-10-31 01:53:09 +0000503int bitcount(unsigned long a)
504{
505 int i = 0;
506 for (; a != 0; a >>= 1)
507 if (a & 1)
508 i++;
509 return i;
510}
511
hailfingerf76cc322010-11-09 22:00:31 +0000512void tolower_string(char *str)
513{
514 for (; *str != '\0'; str++)
515 *str = (char)tolower((unsigned char)*str);
516}
517
hailfingera916b422009-06-01 02:08:58 +0000518char *strcat_realloc(char *dest, const char *src)
519{
520 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000521 if (!dest) {
522 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000523 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000524 }
hailfingera916b422009-06-01 02:08:58 +0000525 strcat(dest, src);
526 return dest;
527}
528
hailfinger6e5a52a2009-11-24 18:27:10 +0000529/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000530 * It will look for needle with a subsequent '=' in haystack, return a copy of
531 * needle and remove everything from the first occurrence of needle to the next
532 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000533 */
stefanct52700282011-06-26 17:38:17 +0000534char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000535{
hailfinger1ef766d2010-07-06 09:55:48 +0000536 char *param_pos, *opt_pos, *rest;
537 char *opt = NULL;
538 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000539 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000540
hailfingerf4aaccc2010-04-28 15:22:14 +0000541 needlelen = strlen(needle);
542 if (!needlelen) {
543 msg_gerr("%s: empty needle! Please report a bug at "
544 "flashrom@flashrom.org\n", __func__);
545 return NULL;
546 }
547 /* No programmer parameters given. */
548 if (*haystack == NULL)
549 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000550 param_pos = strstr(*haystack, needle);
551 do {
552 if (!param_pos)
553 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000554 /* Needle followed by '='? */
555 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600556
hailfinger1ef766d2010-07-06 09:55:48 +0000557 /* Beginning of the string? */
558 if (param_pos == *haystack)
559 break;
560 /* After a delimiter? */
561 if (strchr(delim, *(param_pos - 1)))
562 break;
563 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000564 /* Continue searching. */
565 param_pos++;
566 param_pos = strstr(param_pos, needle);
567 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000568
hailfinger6e5a52a2009-11-24 18:27:10 +0000569 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000570 /* Get the string after needle and '='. */
571 opt_pos = param_pos + needlelen + 1;
572 optlen = strcspn(opt_pos, delim);
573 /* Return an empty string if the parameter was empty. */
574 opt = malloc(optlen + 1);
575 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000576 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000577 exit(1);
578 }
hailfinger1ef766d2010-07-06 09:55:48 +0000579 strncpy(opt, opt_pos, optlen);
580 opt[optlen] = '\0';
581 rest = opt_pos + optlen;
582 /* Skip all delimiters after the current parameter. */
583 rest += strspn(rest, delim);
584 memmove(param_pos, rest, strlen(rest) + 1);
585 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000586 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000587
hailfinger1ef766d2010-07-06 09:55:48 +0000588 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000589}
590
stefanct52700282011-06-26 17:38:17 +0000591char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000592{
593 return extract_param(&programmer_param, param_name, ",");
594}
595
stefancte1c5acf2011-07-04 07:27:17 +0000596/* Returns the number of well-defined erasers for a chip. */
597static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000598{
599 unsigned int usable_erasefunctions = 0;
600 int k;
601 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
602 if (!check_block_eraser(flash, k, 0))
603 usable_erasefunctions++;
604 }
605 return usable_erasefunctions;
606}
607
hailfinger7af83692009-06-15 17:23:36 +0000608/* start is an offset to the base address of the flash chip */
stefanctc5eb8a92011-11-23 09:13:48 +0000609int check_erased_range(struct flashchip *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000610{
611 int ret;
612 uint8_t *cmpbuf = malloc(len);
613
614 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000615 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000616 exit(1);
617 }
Simon Glass4c214132013-07-16 10:09:28 -0600618 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000619 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
620 free(cmpbuf);
621 return ret;
622}
623
uwee15beb92010-08-08 17:01:18 +0000624/*
hailfinger7af3d192009-11-25 17:05:52 +0000625 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000626 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000627 * @start offset to the base address of the flash chip
628 * @len length of the verified area
629 * @message string to print in the "FAILED" message
630 * @return 0 for success, -1 for failure
631 */
stefanctc5eb8a92011-11-23 09:13:48 +0000632int verify_range(struct flashchip *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000633 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000634{
stefanctc5eb8a92011-11-23 09:13:48 +0000635 unsigned int i;
hailfinger8cb6ece2010-11-16 17:21:58 +0000636 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000637 int ret = 0, failcount = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -0800638 unsigned int chunksize;
hailfinger7af83692009-06-15 17:23:36 +0000639
640 if (!len)
641 goto out_free;
642
hailfingerb0f4d122009-06-24 08:20:45 +0000643 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000644 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000645 return 1;
646 }
hailfinger7af83692009-06-15 17:23:36 +0000647 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000648 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000649 exit(1);
650 }
651
652 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000653 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000654 " total_size 0x%x\n", __func__, start, len,
655 flash->total_size * 1024);
656 ret = -1;
657 goto out_free;
658 }
659 if (!message)
660 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000661
David Hendricks758e2fb2014-08-19 20:34:43 -0700662 for (i = 0, chunksize = 0; i < len; i += chunksize) {
David Hendricks1ed1d352011-11-23 17:54:37 -0800663 int tmp, j;
664
David Hendricks758e2fb2014-08-19 20:34:43 -0700665 chunksize = min(flash->page_size, len - i);
David Hendricks1ed1d352011-11-23 17:54:37 -0800666 tmp = flash->read(flash, readbuf + i, start + i, chunksize);
667
David Hendrickse3451942013-03-21 17:23:29 -0700668 /* Since this function explicitly compares the bytes, we need
669 to handle errors manually */
David Hendricks1ed1d352011-11-23 17:54:37 -0800670 if (tmp) {
671 ret = tmp;
David Hendricks758e2fb2014-08-19 20:34:43 -0700672 if (ignore_error(tmp))
David Hendricks1ed1d352011-11-23 17:54:37 -0800673 continue;
David Hendricks758e2fb2014-08-19 20:34:43 -0700674 else
David Hendricks1ed1d352011-11-23 17:54:37 -0800675 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800676 }
677
678 for (j = 0; j < chunksize; j++) {
679 if (cmpbuf[i + j] != readbuf[i + j]) {
680 /* Only print the first failure. */
681 if (!failcount++)
682 msg_cerr("%s FAILED at 0x%08x! "
683 "Expected=0x%02x, Read=0x%02x,",
684 message, start + i + j, cmpbuf[i + j],
685 readbuf[j]);
686 }
687 }
hailfinger8cb6ece2010-11-16 17:21:58 +0000688 }
689
hailfinger5be6c0f2009-07-23 01:42:56 +0000690 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000691 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000692 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000693 ret = -1;
694 }
hailfinger7af83692009-06-15 17:23:36 +0000695
696out_free:
697 free(readbuf);
698 return ret;
699}
700
uwee15beb92010-08-08 17:01:18 +0000701/*
hailfingerb247c7a2010-03-08 00:42:32 +0000702 * Check if the buffer @have can be programmed to the content of @want without
703 * erasing. This is only possible if all chunks of size @gran are either kept
704 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000705 *
hailfingerb247c7a2010-03-08 00:42:32 +0000706 * The following write granularities (enum @gran) are known:
707 * - 1 bit. Each bit can be cleared individually.
708 * - 1 byte. A byte can be written once. Further writes to an already written
709 * byte cause the contents to be either undefined or to stay unchanged.
710 * - 128 bytes. If less than 128 bytes are written, the rest will be
711 * erased. Each write to a 128-byte region will trigger an automatic erase
712 * before anything is written. Very uncommon behaviour and unsupported by
713 * this function.
714 * - 256 bytes. If less than 256 bytes are written, the contents of the
715 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000716 * Warning: This function assumes that @have and @want point to naturally
717 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000718 *
719 * @have buffer with current content
720 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000721 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000722 * @gran write granularity (enum, not count)
723 * @return 0 if no erase is needed, 1 otherwise
724 */
Simon Glass4c214132013-07-16 10:09:28 -0600725static int need_erase(struct flashchip *flash, uint8_t *have, uint8_t *want,
726 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000727{
hailfingerb91c08c2011-08-15 19:54:20 +0000728 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000729 unsigned int i, j, limit;
Simon Glass4c214132013-07-16 10:09:28 -0600730 int erase_value = flash_erase_value(flash);
hailfingerb247c7a2010-03-08 00:42:32 +0000731
732 switch (gran) {
733 case write_gran_1bit:
734 for (i = 0; i < len; i++)
735 if ((have[i] & want[i]) != want[i]) {
736 result = 1;
737 break;
738 }
739 break;
740 case write_gran_1byte:
741 for (i = 0; i < len; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600742 if ((have[i] != want[i]) && (have[i] != erase_value)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000743 result = 1;
744 break;
745 }
746 break;
747 case write_gran_256bytes:
748 for (j = 0; j < len / 256; j++) {
749 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000750 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000751 if (!memcmp(have + j * 256, want + j * 256, limit))
752 continue;
753 /* have needs to be in erased state. */
754 for (i = 0; i < limit; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600755 if (have[j * 256 + i] != erase_value) {
hailfingerb247c7a2010-03-08 00:42:32 +0000756 result = 1;
757 break;
758 }
759 if (result)
760 break;
761 }
762 break;
hailfingerb437e282010-11-04 01:04:27 +0000763 default:
764 msg_cerr("%s: Unsupported granularity! Please report a bug at "
765 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000766 }
767 return result;
768}
769
hailfingerb437e282010-11-04 01:04:27 +0000770/**
771 * Check if the buffer @have needs to be programmed to get the content of @want.
772 * If yes, return 1 and fill in first_start with the start address of the
773 * write operation and first_len with the length of the first to-be-written
774 * chunk. If not, return 0 and leave first_start and first_len undefined.
775 *
776 * Warning: This function assumes that @have and @want point to naturally
777 * aligned regions.
778 *
779 * @have buffer with current content
780 * @want buffer with desired content
781 * @len length of the checked area
782 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000783 * @first_start offset of the first byte which needs to be written (passed in
784 * value is increased by the offset of the first needed write
785 * relative to have/want or unchanged if no write is needed)
786 * @return length of the first contiguous area which needs to be written
787 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000788 *
789 * FIXME: This function needs a parameter which tells it about coalescing
790 * in relation to the max write length of the programmer and the max write
791 * length of the chip.
792 */
stefanctc5eb8a92011-11-23 09:13:48 +0000793static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
794 unsigned int *first_start,
795 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000796{
stefanctc5eb8a92011-11-23 09:13:48 +0000797 int need_write = 0;
798 unsigned int rel_start = 0, first_len = 0;
799 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000800
hailfingerb437e282010-11-04 01:04:27 +0000801 switch (gran) {
802 case write_gran_1bit:
803 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000804 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000805 break;
806 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000807 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000808 break;
809 default:
810 msg_cerr("%s: Unsupported granularity! Please report a bug at "
811 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000812 /* Claim that no write was needed. A write with unknown
813 * granularity is too dangerous to try.
814 */
815 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000816 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000817 for (i = 0; i < len / stride; i++) {
818 limit = min(stride, len - i * stride);
819 /* Are 'have' and 'want' identical? */
820 if (memcmp(have + i * stride, want + i * stride, limit)) {
821 if (!need_write) {
822 /* First location where have and want differ. */
823 need_write = 1;
824 rel_start = i * stride;
825 }
826 } else {
827 if (need_write) {
828 /* First location where have and want
829 * do not differ anymore.
830 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000831 break;
832 }
833 }
834 }
hailfingerffb7f382010-12-06 13:05:44 +0000835 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000836 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000837 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000838 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000839}
840
hailfinger0c515352009-11-23 12:55:31 +0000841/* This function generates various test patterns useful for testing controller
842 * and chip communication as well as chip behaviour.
843 *
844 * If a byte can be written multiple times, each time keeping 0-bits at 0
845 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
846 * is essentially an AND operation. That's also the reason why this function
847 * provides the result of AND between various patterns.
848 *
849 * Below is a list of patterns (and their block length).
850 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
851 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
852 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
853 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
854 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
855 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
856 * Pattern 6 is 00 (1 Byte)
857 * Pattern 7 is ff (1 Byte)
858 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
859 * byte block.
860 *
861 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
862 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
863 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
864 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
865 * Pattern 12 is 00 (1 Byte)
866 * Pattern 13 is ff (1 Byte)
867 * Patterns 8-13 have no block number.
868 *
869 * Patterns 0-3 are created to detect and efficiently diagnose communication
870 * slips like missed bits or bytes and their repetitive nature gives good visual
871 * cues to the person inspecting the results. In addition, the following holds:
872 * AND Pattern 0/1 == Pattern 4
873 * AND Pattern 2/3 == Pattern 5
874 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
875 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
876 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
877 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
878 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
879 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
880 * Besides that, they provide for bit testing of the last two bytes of every
881 * 256 byte block which contains the block number for patterns 0-6.
882 * Patterns 10-11 are special purpose for detecting subblock aliasing with
883 * block sizes >256 bytes (some Dataflash chips etc.)
884 * AND Pattern 8/9 == Pattern 12
885 * AND Pattern 10/11 == Pattern 12
886 * Pattern 13 is the completely erased state.
887 * None of the patterns can detect aliasing at boundaries which are a multiple
888 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
889 */
890int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
891{
892 int i;
893
894 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000895 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000896 return 1;
897 }
898
899 switch (variant) {
900 case 0:
901 for (i = 0; i < size; i++)
902 buf[i] = (i & 0xf) << 4 | 0x5;
903 break;
904 case 1:
905 for (i = 0; i < size; i++)
906 buf[i] = (i & 0xf) << 4 | 0xa;
907 break;
908 case 2:
909 for (i = 0; i < size; i++)
910 buf[i] = 0x50 | (i & 0xf);
911 break;
912 case 3:
913 for (i = 0; i < size; i++)
914 buf[i] = 0xa0 | (i & 0xf);
915 break;
916 case 4:
917 for (i = 0; i < size; i++)
918 buf[i] = (i & 0xf) << 4;
919 break;
920 case 5:
921 for (i = 0; i < size; i++)
922 buf[i] = i & 0xf;
923 break;
924 case 6:
925 memset(buf, 0x00, size);
926 break;
927 case 7:
928 memset(buf, 0xff, size);
929 break;
930 case 8:
931 for (i = 0; i < size; i++)
932 buf[i] = i & 0xff;
933 break;
934 case 9:
935 for (i = 0; i < size; i++)
936 buf[i] = ~(i & 0xff);
937 break;
938 case 10:
939 for (i = 0; i < size % 2; i++) {
940 buf[i * 2] = (i >> 8) & 0xff;
941 buf[i * 2 + 1] = i & 0xff;
942 }
943 if (size & 0x1)
944 buf[i * 2] = (i >> 8) & 0xff;
945 break;
946 case 11:
947 for (i = 0; i < size % 2; i++) {
948 buf[i * 2] = ~((i >> 8) & 0xff);
949 buf[i * 2 + 1] = ~(i & 0xff);
950 }
951 if (size & 0x1)
952 buf[i * 2] = ~((i >> 8) & 0xff);
953 break;
954 case 12:
955 memset(buf, 0x00, size);
956 break;
957 case 13:
958 memset(buf, 0xff, size);
959 break;
960 }
961
962 if ((variant >= 0) && (variant <= 7)) {
963 /* Write block number in the last two bytes of each 256-byte
964 * block, big endian for easier reading of the hexdump.
965 * Note that this wraps around for chips larger than 2^24 bytes
966 * (16 MB).
967 */
968 for (i = 0; i < size / 256; i++) {
969 buf[i * 256 + 254] = (i >> 8) & 0xff;
970 buf[i * 256 + 255] = i & 0xff;
971 }
972 }
973
974 return 0;
975}
976
hailfingeraec9c962009-10-31 01:53:09 +0000977int check_max_decode(enum chipbustype buses, uint32_t size)
978{
979 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +0000980
981 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000982 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000983 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000984 "size %u kB of chipset/board/programmer "
985 "for %s interface, "
986 "probe/read/erase/write may fail. ", size / 1024,
987 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +0000988 }
hailfingere1e41ea2011-07-27 07:13:06 +0000989 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000990 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000991 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000992 "size %u kB of chipset/board/programmer "
993 "for %s interface, "
994 "probe/read/erase/write may fail. ", size / 1024,
995 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +0000996 }
hailfingere1e41ea2011-07-27 07:13:06 +0000997 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000998 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000999 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001000 "size %u kB of chipset/board/programmer "
1001 "for %s interface, "
1002 "probe/read/erase/write may fail. ", size / 1024,
1003 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001004 }
hailfingere1e41ea2011-07-27 07:13:06 +00001005 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001006 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001007 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001008 "size %u kB of chipset/board/programmer "
1009 "for %s interface, "
1010 "probe/read/erase/write may fail. ", size / 1024,
1011 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001012 }
1013 if (!limitexceeded)
1014 return 0;
1015 /* Sometimes chip and programmer have more than one bus in common,
1016 * and the limit is not exceeded on all buses. Tell the user.
1017 */
1018 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001019 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001020 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001021 "interface which can support a chip of this size. "
1022 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001023 return 1;
1024}
1025
hailfinger48ed3e22011-05-04 00:39:50 +00001026int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001027{
hailfinger48ed3e22011-05-04 00:39:50 +00001028 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001029 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001030 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001031 uint32_t size;
1032 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001033 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001034
hailfinger48ed3e22011-05-04 00:39:50 +00001035 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001036 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001037 continue;
hailfingeraec9c962009-10-31 01:53:09 +00001038 buses_common = buses_supported & flash->bustype;
1039 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001040 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1041 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +00001042 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +00001043 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001044 free(tmp);
1045 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001046 msg_gspew("and chip bus type %s are incompatible.",
1047 tmp);
hailfingera916b422009-06-01 02:08:58 +00001048 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001049 msg_gspew("\n");
1050 continue;
1051 }
1052 msg_gdbg("Probing for %s %s, %d kB: ",
1053 flash->vendor, flash->name, flash->total_size);
1054 if (!flash->probe && !force) {
1055 msg_gdbg("failed! flashrom has no probe function for "
1056 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001057 continue;
1058 }
stepan782fb172007-04-06 11:58:03 +00001059
ollie5672ac62004-03-17 22:22:08 +00001060 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001061 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001062
hailfinger48ed3e22011-05-04 00:39:50 +00001063 /* Start filling in the dynamic data. */
1064 *fill_flash = *flash;
1065
hailfinger72d3b982009-05-09 07:27:23 +00001066 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001067 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001068
stugec1e55fe2008-07-02 17:15:47 +00001069 if (force)
1070 break;
stepanc98b80b2006-03-16 16:57:41 +00001071
hailfinger48ed3e22011-05-04 00:39:50 +00001072 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001073 goto notfound;
1074
hailfinger48ed3e22011-05-04 00:39:50 +00001075 /* If this is the first chip found, accept it.
1076 * If this is not the first chip found, accept it only if it is
1077 * a non-generic match.
1078 * We could either make chipcount global or provide it as
1079 * parameter, or we assume that startchip==0 means this call to
1080 * probe_flash() is the first one and thus no chip has been
1081 * found before.
1082 */
1083 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001084 break;
1085
stuge56300c32008-09-03 23:10:05 +00001086notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001087 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001088 }
uwebe4477b2007-08-23 16:08:21 +00001089
stugec1e55fe2008-07-02 17:15:47 +00001090 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001091 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001092
hailfingere11396b2011-03-08 00:09:11 +00001093#if CONFIG_INTERNAL == 1
1094 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001095 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001096 else
1097#endif
stepan3e7aeae2011-01-19 06:21:54 +00001098 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001099
stefanct588b6d22011-06-26 20:45:35 +00001100 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001101 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1102 force ? "Assuming" : "Found", fill_flash->vendor,
1103 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001104 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001105
hailfinger0f4c3952010-12-02 21:59:42 +00001106 /* Flash registers will not be mapped if the chip was forced. Lock info
1107 * may be stored in registers, so avoid lock info printing.
1108 */
1109 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001110 if (fill_flash->printlock)
1111 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001112
hailfinger48ed3e22011-05-04 00:39:50 +00001113 /* Return position of matching chip. */
1114 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001115}
1116
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001117int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001118{
hailfingerb0f4d122009-06-24 08:20:45 +00001119 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001120 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001121
snelsone42c3802010-05-07 20:09:04 +00001122 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001123
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001124 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1125 ret = handle_partial_verify(flash, buf, verify_range);
1126 } else {
1127 ret = verify_range(flash, buf, 0, total_size, NULL);
1128 }
uwef6641642007-05-09 10:17:44 +00001129
David Hendricks1ed1d352011-11-23 17:54:37 -08001130 if (ret == ACCESS_DENIED) {
1131 msg_gdbg("Could not fully verify due to access error, ");
1132 if (access_denied_action == error_ignore) {
1133 msg_gdbg("ignoring\n");
1134 ret = 0;
1135 } else {
1136 msg_gdbg("aborting\n");
1137 }
1138 }
1139
hailfingerb0f4d122009-06-24 08:20:45 +00001140 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001141 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001142
hailfingerb0f4d122009-06-24 08:20:45 +00001143 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001144}
1145
uwe8d342eb2011-07-28 08:13:25 +00001146int read_buf_from_file(unsigned char *buf, unsigned long size,
1147 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001148{
1149 unsigned long numbytes;
1150 FILE *image;
1151 struct stat image_stat;
1152
Vincent Palatin7ab23932014-10-01 12:09:16 -07001153 if (!strncmp(filename, "-", sizeof("-")))
1154 image = fdopen(STDIN_FILENO, "rb");
1155 else
1156 image = fopen(filename, "rb");
1157 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001158 perror(filename);
1159 return 1;
1160 }
1161 if (fstat(fileno(image), &image_stat) != 0) {
1162 perror(filename);
1163 fclose(image);
1164 return 1;
1165 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001166 if ((image_stat.st_size != size) &&
1167 (strncmp(filename, "-", sizeof("-")))) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001168 msg_gerr("Error: Image size doesn't match: stat %ld bytes, "
1169 "wanted %ld!\n", image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001170 fclose(image);
1171 return 1;
1172 }
1173 numbytes = fread(buf, 1, size, image);
1174 if (fclose(image)) {
1175 perror(filename);
1176 return 1;
1177 }
1178 if (numbytes != size) {
1179 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1180 "wanted %ld!\n", numbytes, size);
1181 return 1;
1182 }
1183 return 0;
1184}
1185
uwe8d342eb2011-07-28 08:13:25 +00001186int write_buf_to_file(unsigned char *buf, unsigned long size,
1187 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001188{
1189 unsigned long numbytes;
1190 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001191
1192 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001193 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001194 return 1;
1195 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001196 if (!strncmp(filename, "-", sizeof("-")))
1197 image = fdopen(STDOUT_FILENO, "wb");
1198 else
1199 image = fopen(filename, "wb");
1200 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001201 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001202 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001203 }
hailfingerd219a232009-01-28 00:27:54 +00001204
hailfingerd219a232009-01-28 00:27:54 +00001205 numbytes = fwrite(buf, 1, size, image);
1206 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001207 if (numbytes != size) {
1208 msg_gerr("File %s could not be written completely.\n",
1209 filename);
hailfingerd219a232009-01-28 00:27:54 +00001210 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001211 }
hailfingerd219a232009-01-28 00:27:54 +00001212 return 0;
1213}
1214
David Hendrickse3451942013-03-21 17:23:29 -07001215/*
1216 * read_flash - wrapper for flash->read() with additional high-level policy
1217 *
1218 * @flash flash chip
1219 * @buf buffer to store data in
1220 * @start start address
1221 * @len number of bytes to read
1222 *
1223 * This wrapper simplifies most cases when the flash chip needs to be read
1224 * since policy decisions such as non-fatal error handling is centralized.
1225 */
1226int read_flash(struct flashchip *flash, uint8_t *buf,
1227 unsigned int start, unsigned int len)
1228{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001229 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001230
1231 if (!flash || !flash->read)
1232 return -1;
1233
David Hendricks4e76fdc2013-05-13 16:05:36 -07001234 ret = flash->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001235 if (ret) {
1236 if (ignore_error(ret)) {
1237 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1238 start, start + len - 1);
1239 ret = 0;
1240 } else {
1241 msg_gdbg("failed to read 0x%x-0x%x\n",
1242 start, start + len - 1);
1243 }
1244 }
1245
1246 return ret;
1247}
1248
David Hendricks7c8a1612013-04-26 19:14:44 -07001249/*
1250 * write_flash - wrapper for flash->write() with additional high-level policy
1251 *
1252 * @flash flash chip
1253 * @buf buffer to write to flash
1254 * @start start address in flash
1255 * @len number of bytes to write
1256 *
1257 * TODO: Look up regions that are write-protected and avoid attempt to write
1258 * to them at all.
1259 */
1260int write_flash(struct flashchip *flash, uint8_t *buf,
1261 unsigned int start, unsigned int len)
1262{
1263 if (!flash || !flash->write)
1264 return -1;
1265
1266 return flash->write(flash, buf, start, len);
1267}
1268
stefanct52700282011-06-26 17:38:17 +00001269int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001270{
1271 unsigned long size = flash->total_size * 1024;
1272 unsigned char *buf = calloc(size, sizeof(char));
1273 int ret = 0;
1274
1275 msg_cinfo("Reading flash... ");
1276 if (!buf) {
1277 msg_gerr("Memory allocation failed!\n");
1278 msg_cinfo("FAILED.\n");
1279 return 1;
1280 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001281
1282 /* To support partial read, fill buffer to all 0xFF at beginning to make
1283 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001284 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001285
hailfinger42a850a2010-07-13 23:56:13 +00001286 if (!flash->read) {
1287 msg_cerr("No read function available for this flash chip.\n");
1288 ret = 1;
1289 goto out_free;
1290 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001291
1292 /* First try to handle partial read case, rather than read the whole
1293 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001294 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001295 if (ret < 0) {
1296 msg_cerr("Partial read operation failed!\n");
1297 ret = 1;
1298 goto out_free;
1299 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001300 int num_regions = get_num_include_args();
1301
1302 if (ret != num_regions) {
1303 msg_cerr("Requested %d regions, but only read %d\n",
1304 num_regions, ret);
1305 ret = 1;
1306 goto out_free;
1307 }
1308
1309 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001310 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001311 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001312 msg_cerr("Read operation failed!\n");
1313 ret = 1;
1314 goto out_free;
1315 }
hailfinger42a850a2010-07-13 23:56:13 +00001316 }
1317
David Hendricksdf29a832013-06-28 14:33:51 -07001318 if (filename)
1319 ret = write_buf_to_file(buf, size, filename);
1320
hailfinger42a850a2010-07-13 23:56:13 +00001321out_free:
1322 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001323 if (ret)
1324 msg_cerr("FAILED.");
1325 else
1326 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001327 return ret;
1328}
1329
hailfingerb437e282010-11-04 01:04:27 +00001330/* This function shares a lot of its structure with erase_and_write_flash() and
1331 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001332 * Even if an error is found, the function will keep going and check the rest.
1333 */
hailfinger48ed3e22011-05-04 00:39:50 +00001334static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001335{
hailfingerb91c08c2011-08-15 19:54:20 +00001336 int i, j, k;
1337 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001338
1339 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1340 unsigned int done = 0;
1341 struct block_eraser eraser = flash->block_erasers[k];
1342
1343 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1344 /* Blocks with zero size are bugs in flashchips.c. */
1345 if (eraser.eraseblocks[i].count &&
1346 !eraser.eraseblocks[i].size) {
1347 msg_gerr("ERROR: Flash chip %s erase function "
1348 "%i region %i has size 0. Please report"
1349 " a bug at flashrom@flashrom.org\n",
1350 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001351 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001352 }
1353 /* Blocks with zero count are bugs in flashchips.c. */
1354 if (!eraser.eraseblocks[i].count &&
1355 eraser.eraseblocks[i].size) {
1356 msg_gerr("ERROR: Flash chip %s erase function "
1357 "%i region %i has count 0. Please report"
1358 " a bug at flashrom@flashrom.org\n",
1359 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001360 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001361 }
1362 done += eraser.eraseblocks[i].count *
1363 eraser.eraseblocks[i].size;
1364 }
hailfinger9fed35d2010-01-19 06:42:46 +00001365 /* Empty eraseblock definition with erase function. */
1366 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001367 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001368 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001369 if (!done)
1370 continue;
1371 if (done != flash->total_size * 1024) {
1372 msg_gerr("ERROR: Flash chip %s erase function %i "
1373 "region walking resulted in 0x%06x bytes total,"
1374 " expected 0x%06x bytes. Please report a bug at"
1375 " flashrom@flashrom.org\n", flash->name, k,
1376 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001377 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001378 }
hailfinger9fed35d2010-01-19 06:42:46 +00001379 if (!eraser.block_erase)
1380 continue;
1381 /* Check if there are identical erase functions for different
1382 * layouts. That would imply "magic" erase functions. The
1383 * easiest way to check this is with function pointers.
1384 */
uwef6f94d42010-03-13 17:28:29 +00001385 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001386 if (eraser.block_erase ==
1387 flash->block_erasers[j].block_erase) {
1388 msg_gerr("ERROR: Flash chip %s erase function "
1389 "%i and %i are identical. Please report"
1390 " a bug at flashrom@flashrom.org\n",
1391 flash->name, k, j);
1392 ret = 1;
1393 }
uwef6f94d42010-03-13 17:28:29 +00001394 }
hailfinger45177872010-01-18 08:14:43 +00001395 }
hailfinger9fed35d2010-01-19 06:42:46 +00001396 return ret;
hailfinger45177872010-01-18 08:14:43 +00001397}
1398
hailfingerb437e282010-11-04 01:04:27 +00001399static int erase_and_write_block_helper(struct flashchip *flash,
1400 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001401 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001402 uint8_t *newcontents,
1403 int (*erasefn) (struct flashchip *flash,
1404 unsigned int addr,
1405 unsigned int len))
1406{
stefanctc5eb8a92011-11-23 09:13:48 +00001407 unsigned int starthere = 0, lenhere = 0;
1408 int ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001409 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1410
hailfinger90fcf9b2010-11-05 14:51:59 +00001411 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001412 * need to be adjusted here to keep the impression of proper abstraction
1413 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001414 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001415 newcontents += start;
1416 msg_cdbg(":");
1417 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001418 if (need_erase(flash, curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001419 msg_cdbg("E");
1420 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001421 if (ret) {
1422 if (ret == ACCESS_DENIED)
1423 msg_cdbg("D");
1424 else
1425 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001426 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001427 }
1428
hailfingerac8e3182011-06-26 17:04:16 +00001429 if (check_erased_range(flash, start, len)) {
1430 msg_cerr("ERASE FAILED!\n");
1431 return -1;
1432 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001433 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001434 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001435 skip = 0;
1436 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001437 /* get_next_write() sets starthere to a new value after the call. */
1438 while ((lenhere = get_next_write(curcontents + starthere,
1439 newcontents + starthere,
1440 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001441 if (!writecount++)
1442 msg_cdbg("W");
1443 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001444 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001445 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001446 if (ret) {
1447 if (ret == ACCESS_DENIED)
1448 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001449 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001450 }
hailfingerb437e282010-11-04 01:04:27 +00001451 starthere += lenhere;
1452 skip = 0;
1453 }
1454 if (skip)
1455 msg_cdbg("S");
1456 return ret;
1457}
1458
hailfinger83541b32010-07-13 00:42:00 +00001459static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1460 int (*do_something) (struct flashchip *flash,
1461 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001462 unsigned int len,
1463 uint8_t *param1,
1464 uint8_t *param2,
1465 int (*erasefn) (
1466 struct flashchip *flash,
1467 unsigned int addr,
1468 unsigned int len)),
1469 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001470{
David Hendricks1ed1d352011-11-23 17:54:37 -08001471 int i, j, rc = 0;
hailfingerb91c08c2011-08-15 19:54:20 +00001472 unsigned int start = 0;
1473 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001474 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001475
David Hendricks54777392015-01-11 18:55:14 -08001476 if (required_erase_size &&
1477 (eraser.eraseblocks[i].size != required_erase_size)) {
1478 msg_cdbg("%u does not meet erase alignment requirement\n",
1479 eraser.eraseblocks[i].size);
1480 return -1;
1481 }
1482
hailfinger2b8c9382010-07-13 00:37:19 +00001483 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1484 /* count==0 for all automatically initialized array
1485 * members so the loop below won't be executed for them.
1486 */
1487 len = eraser.eraseblocks[i].size;
1488 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001489 /* Print this for every block except the first one. */
1490 if (i || j)
1491 msg_cdbg(", ");
1492 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001493 start + len - 1);
David Hendricks1ed1d352011-11-23 17:54:37 -08001494 rc = do_something(flash, start, len, param1, param2,
1495 eraser.block_erase);
1496 if (rc) {
1497 if (ignore_error(rc))
1498 rc = 0;
1499 else
1500 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001501 }
hailfinger2b8c9382010-07-13 00:37:19 +00001502 start += len;
1503 }
1504 }
hailfingerb437e282010-11-04 01:04:27 +00001505 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001506 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001507}
1508
stefanct569dbb62011-07-01 00:19:12 +00001509static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001510{
1511 struct block_eraser eraser = flash->block_erasers[k];
1512
1513 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1514 if (log)
1515 msg_cdbg("not defined. ");
1516 return 1;
1517 }
1518 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1519 if (log)
1520 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001521 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001522 return 1;
1523 }
1524 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1525 if (log)
1526 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001527 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001528 return 1;
1529 }
1530 return 0;
1531}
1532
uwe8d342eb2011-07-28 08:13:25 +00001533int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1534 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001535{
hailfinger8f524a82011-07-21 21:21:04 +00001536 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001537 uint8_t *curcontents;
1538 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001539 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001540
hailfingercf848f12010-12-05 15:14:44 +00001541 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001542 curcontents = malloc(size);
1543 if (!curcontents) {
1544 msg_gerr("Out of memory!\n");
1545 exit(1);
1546 }
hailfingerb437e282010-11-04 01:04:27 +00001547 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1548 memcpy(curcontents, oldcontents, size);
1549
hailfinger7df21362009-09-05 02:30:58 +00001550 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001551 if (k != 0)
1552 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001553 if (!usable_erasefunctions) {
1554 msg_cdbg("No usable erase functions left.\n");
1555 break;
1556 }
stefanctf0bc4712011-07-26 14:28:35 +00001557 msg_cdbg("Trying erase function %i... ", k);
1558 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001559 continue;
hailfingercf848f12010-12-05 15:14:44 +00001560 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001561 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1562 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001563 /* If everything is OK, don't try another erase function. */
1564 if (!ret)
1565 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001566 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001567 * contents are. If no usable erase functions remain, we can
1568 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001569 */
hailfingercf848f12010-12-05 15:14:44 +00001570 if (!usable_erasefunctions)
1571 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001572 /* Reading the whole chip may take a while, inform the user even
1573 * in non-verbose mode.
1574 */
1575 msg_cinfo("Reading current flash chip contents... ");
David Hendrickse3451942013-03-21 17:23:29 -07001576 if (read_flash(flash, curcontents, 0, size)) {
hailfinger6237f5e2010-12-02 02:41:55 +00001577 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001578 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001579 /* We have no idea about the flash chip contents, so
1580 * retrying with another erase function is pointless.
1581 */
1582 break;
1583 }
David Hendricks89a45e52011-11-22 16:56:22 -08001584 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001585 }
hailfingerb437e282010-11-04 01:04:27 +00001586 /* Free the scratchpad. */
1587 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001588
hailfinger7df21362009-09-05 02:30:58 +00001589 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001590 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001591 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001592 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001593 }
1594 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001595}
1596
hailfinger4c47e9d2010-10-19 22:06:20 +00001597void nonfatal_help_message(void)
1598{
1599 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1600 "This means we have to add special support for your board, "
1601 "programmer or flash chip.\n"
1602 "Please report this on IRC at irc.freenode.net (channel "
1603 "#flashrom) or\n"
1604 "mail flashrom@flashrom.org!\n"
1605 "-------------------------------------------------------------"
1606 "------------------\n"
1607 "You may now reboot or simply leave the machine running.\n");
1608}
1609
uweb34ec9f2009-10-01 18:40:02 +00001610void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001611{
snelsone42c3802010-05-07 20:09:04 +00001612 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001613 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001614 "mail flashrom@flashrom.org with FAILED: your board name in "
1615 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001616 "-------------------------------------------------------------"
1617 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001618 "DO NOT REBOOT OR POWEROFF!\n");
1619}
1620
uwe8d342eb2011-07-28 08:13:25 +00001621/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001622void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001623{
1624 enum programmer p;
1625 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001626 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001627 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001628 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001629 }
Simon Glass8dc82732013-07-16 10:13:51 -06001630 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001631}
1632
hailfingerf79d1712010-10-06 23:48:34 +00001633void list_programmers_linebreak(int startcol, int cols, int paren)
1634{
1635 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001636 int pnamelen;
1637 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001638 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001639 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001640
1641 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1642 pname = programmer_table[p].name;
1643 pnamelen = strlen(pname);
1644 if (remaining - pnamelen - 2 < 0) {
1645 if (firstline)
1646 firstline = 0;
1647 else
1648 printf("\n");
1649 for (i = 0; i < startcol; i++)
1650 printf(" ");
1651 remaining = cols - startcol;
1652 } else {
1653 printf(" ");
1654 remaining--;
1655 }
1656 if (paren && (p == 0)) {
1657 printf("(");
1658 remaining--;
1659 }
1660 printf("%s", pname);
1661 remaining -= pnamelen;
1662 if (p < PROGRAMMER_INVALID - 1) {
1663 printf(",");
1664 remaining--;
1665 } else {
1666 if (paren)
1667 printf(")");
1668 printf("\n");
1669 }
1670 }
1671}
1672
hailfinger3b471632010-03-27 16:36:40 +00001673void print_sysinfo(void)
1674{
David Hendricksc6c9f822010-11-03 15:07:01 -07001675 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001676#if HAVE_UTSNAME == 1
1677 struct utsname osinfo;
1678 uname(&osinfo);
1679
David Hendricksc6c9f822010-11-03 15:07:01 -07001680 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001681 osinfo.machine);
1682#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001683 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001684#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001685 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001686#if NEED_PCI == 1
1687#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001688 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001689#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001690 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001691#endif
1692#endif
1693#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001694 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001695#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001696 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001697#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001698 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001699#endif
hailfinger3b471632010-03-27 16:36:40 +00001700#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001701 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001702#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001703 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001704#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001705 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001706#endif
1707#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001708 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001709#endif
1710#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001711 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001712#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001713 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001714#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001715 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001716}
1717
uwefdeca092008-01-21 15:24:22 +00001718void print_version(void)
1719{
David Hendricksc6c9f822010-11-03 15:07:01 -07001720 /* send to stderr for chromium os */
1721 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001722 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001723}
1724
hailfinger74819ad2010-05-15 15:04:37 +00001725void print_banner(void)
1726{
1727 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001728 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001729 msg_ginfo("\n");
1730}
1731
hailfingerc77acb52009-12-24 02:15:55 +00001732int selfcheck(void)
1733{
hailfinger45177872010-01-18 08:14:43 +00001734 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001735 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001736
1737 /* Safety check. Instead of aborting after the first error, check
1738 * if more errors exist.
1739 */
hailfingerc77acb52009-12-24 02:15:55 +00001740 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001741 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001742 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001743 }
stefanct8632c922011-07-26 14:33:46 +00001744 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001745 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001746 * For 'flashchips' we check the first element to be non-null. In the
1747 * other cases there exist use cases where the first element can be
1748 * null. */
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001749 if (!flashchips || flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001750 msg_gerr("Flashchips table miscompilation!\n");
1751 ret = 1;
1752 }
hailfinger45177872010-01-18 08:14:43 +00001753 for (flash = flashchips; flash && flash->name; flash++)
1754 if (selfcheck_eraseblocks(flash))
1755 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001756
1757#if CONFIG_INTERNAL == 1
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001758 if (!chipset_enables) {
stefanct6d836ba2011-05-26 01:35:19 +00001759 msg_gerr("Chipset enables table does not exist!\n");
1760 ret = 1;
1761 }
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001762 if (!board_matches) {
stefanct6d836ba2011-05-26 01:35:19 +00001763 msg_gerr("Board enables table does not exist!\n");
1764 ret = 1;
1765 }
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001766 if (!boards_known) {
stefanct6d836ba2011-05-26 01:35:19 +00001767 msg_gerr("Known boards table does not exist!\n");
1768 ret = 1;
1769 }
Yunlian Jiang9b1ab472014-02-27 15:07:08 -08001770 if (!laptops_known) {
stefanct6d836ba2011-05-26 01:35:19 +00001771 msg_gerr("Known laptops table does not exist!\n");
1772 ret = 1;
1773 }
uwe8d342eb2011-07-28 08:13:25 +00001774#endif
hailfinger45177872010-01-18 08:14:43 +00001775 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001776}
1777
hailfinger48ed3e22011-05-04 00:39:50 +00001778void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001779{
1780 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001781 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001782 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001783 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001784 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001785 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001786 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001787 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001788 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001789 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001790 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001791 msg_cdbg(" WRITE");
1792 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001793 }
1794 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1795 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1796 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1797 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001798 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001799 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001800 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001801 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001802 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001803 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001804 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001805 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001806 msg_cdbg(" WRITE");
1807 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001808 }
hailfinger92cd8e32010-01-07 03:24:05 +00001809 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001810 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001811 "in the latest development\n"
1812 "version of flashrom. If you are running the latest "
1813 "development version,\n"
1814 "please email a report to flashrom@flashrom.org if "
1815 "any of the above operations\n"
1816 "work correctly for you with this flash part. Please "
1817 "include the flashrom\n"
1818 "output with the additional -V option for all "
1819 "operations you tested (-V, -Vr,\n"
1820 "-Vw, -VE), and mention which mainboard or "
1821 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001822 "Please mention your board in the subject line. "
1823 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001824 }
1825}
1826
hailfinger771fc182010-10-15 00:01:14 +00001827/* FIXME: This function signature needs to be improved once doit() has a better
1828 * function signature.
1829 */
stefanct02116582011-05-18 01:30:56 +00001830int 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 +00001831{
1832 if (!programmer_may_write && (write_it || erase_it)) {
1833 msg_perr("Write/erase is not working yet on your programmer in "
1834 "its current configuration.\n");
1835 /* --force is the wrong approach, but it's the best we can do
1836 * until the generic programmer parameter parser is merged.
1837 */
1838 if (!force)
1839 return 1;
1840 msg_cerr("Continuing anyway.\n");
1841 }
1842
1843 if (read_it || erase_it || write_it || verify_it) {
1844 /* Everything needs read. */
1845 if (flash->tested & TEST_BAD_READ) {
1846 msg_cerr("Read is not working on this chip. ");
1847 if (!force)
1848 return 1;
1849 msg_cerr("Continuing anyway.\n");
1850 }
1851 if (!flash->read) {
1852 msg_cerr("flashrom has no read function for this "
1853 "flash chip.\n");
1854 return 1;
1855 }
1856 }
1857 if (erase_it || write_it) {
1858 /* Write needs erase. */
1859 if (flash->tested & TEST_BAD_ERASE) {
1860 msg_cerr("Erase is not working on this chip. ");
1861 if (!force)
1862 return 1;
1863 msg_cerr("Continuing anyway.\n");
1864 }
stefancte1c5acf2011-07-04 07:27:17 +00001865 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001866 msg_cerr("flashrom has no erase function for this "
1867 "flash chip.\n");
1868 return 1;
1869 }
hailfinger771fc182010-10-15 00:01:14 +00001870 }
1871 if (write_it) {
1872 if (flash->tested & TEST_BAD_WRITE) {
1873 msg_cerr("Write is not working on this chip. ");
1874 if (!force)
1875 return 1;
1876 msg_cerr("Continuing anyway.\n");
1877 }
1878 if (!flash->write) {
1879 msg_cerr("flashrom has no write function for this "
1880 "flash chip.\n");
1881 return 1;
1882 }
1883 }
1884 return 0;
1885}
1886
hailfingerc77acb52009-12-24 02:15:55 +00001887/* This function signature is horrible. We need to design a better interface,
1888 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001889 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001890 */
Simon Glass9ad06c12013-07-03 22:08:17 +09001891int doit(struct flashchip *flash, int force, const char *filename, int read_it,
1892 int write_it, int erase_it, int verify_it, int extract_it,
1893 const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001894{
hailfinger4c47e9d2010-10-19 22:06:20 +00001895 uint8_t *oldcontents;
1896 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001897 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001898 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001899
stefanct02116582011-05-18 01:30:56 +00001900 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001901 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001902 ret = 1;
1903 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001904 }
1905
hailfinger771fc182010-10-15 00:01:14 +00001906 /* Given the existence of read locks, we want to unlock for read,
1907 * erase and write.
1908 */
1909 if (flash->unlock)
1910 flash->unlock(flash);
1911
David Hendricks6d62d752011-03-07 21:20:22 -08001912 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001913 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001914 ret = 1;
1915 goto out_nofree;
1916 }
1917
Simon Glass9ad06c12013-07-03 22:08:17 +09001918 if (extract_it) {
1919 ret = extract_regions(flash);
1920 goto out_nofree;
1921 }
1922
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001923 /* mark entries included using -i argument as "included" if they are
1924 found in the master rom_entries list */
1925 if (process_include_args() < 0) {
1926 ret = 1;
1927 goto out_nofree;
1928 }
1929
hailfinger771fc182010-10-15 00:01:14 +00001930 if (read_it) {
1931 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001932 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001933 }
hailfingerb437e282010-11-04 01:04:27 +00001934
stefanctd611e8f2011-07-12 22:35:21 +00001935 oldcontents = malloc(size);
1936 if (!oldcontents) {
1937 msg_gerr("Out of memory!\n");
1938 exit(1);
1939 }
Simon Glass4c214132013-07-16 10:09:28 -06001940 /* Assume worst case: All blocks are not erased. */
1941 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00001942 newcontents = malloc(size);
1943 if (!newcontents) {
1944 msg_gerr("Out of memory!\n");
1945 exit(1);
1946 }
Simon Glass4c214132013-07-16 10:09:28 -06001947 /* Assume best case: All blocks are erased. */
1948 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00001949 /* Side effect of the assumptions above: Default write action is erase
1950 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06001951 * oldcontents being completely unerased means we have to erase
1952 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00001953 */
1954
ollie0eb62d62004-12-08 20:10:01 +00001955 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001956 /* FIXME: Do we really want the scary warning if erase failed?
1957 * After all, after erase the chip is either blank or partially
1958 * blank or it has the old contents. A blank chip won't boot,
1959 * so if the user wanted erase and reboots afterwards, the user
1960 * knows very well that booting won't work.
1961 */
hailfingerb437e282010-11-04 01:04:27 +00001962 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001963 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001964 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001965 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001966 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001967 }
hailfinger771fc182010-10-15 00:01:14 +00001968
hailfingerd217d122010-10-08 18:52:29 +00001969 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07001970 /*
1971 * Note: This must be done before any files specified by -i
1972 * arguments are processed merged into the newcontents since
1973 * -i files take priority. See http://crbug.com/263495.
1974 */
1975 if (filename) {
1976 if (read_buf_from_file(newcontents, size, filename)) {
1977 ret = 1;
1978 goto out;
1979 }
1980 } else {
1981 /* Content will be read from -i args, so they must
1982 * not overlap. */
1983 if (included_regions_overlap()) {
1984 msg_gerr("Error: Included regions must "
1985 "not overlap.\n");
1986 ret = 1;
1987 goto out;
1988 }
stepan1da96c02006-11-21 23:48:51 +00001989 }
1990
David Hendricksac82cac2012-06-19 10:29:37 -07001991#if 0
1992 /*
1993 * FIXME: show_id() causes failure if vendor:mainboard do not
1994 * match. This may happen if codenames are in flux.
1995 * See chrome-os-partner:10414.
1996 */
hailfinger90c7d542010-05-31 15:27:27 +00001997#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001998 if (programmer == PROGRAMMER_INTERNAL)
1999 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002000#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002001#endif
ollie5672ac62004-03-17 22:22:08 +00002002 }
2003
David Hendricksc44d7a02011-10-17 11:28:43 -07002004 /* Obtain a reference image so that we can check whether regions need
2005 * to be erased and to give better diagnostics in case write fails.
David Hendricks52ddff02013-07-23 15:05:14 -07002006 * If --fast-verify is used then only the regions which are included
2007 * using -i will be read.
hailfinger4c47e9d2010-10-19 22:06:20 +00002008 */
David Hendricksc44d7a02011-10-17 11:28:43 -07002009 if (diff_file) {
2010 msg_cdbg("Reading old contents from file... ");
2011 if (read_buf_from_file(oldcontents, size, diff_file)) {
2012 ret = 1;
2013 msg_cdbg("FAILED.\n");
2014 goto out;
2015 }
2016 } else {
2017 msg_cdbg("Reading old contents from flash chip... ");
David Hendricksd4e712c2013-08-02 17:06:16 -07002018 if (verify_it == VERIFY_PARTIAL) {
2019 if (handle_partial_read(flash, oldcontents,
2020 read_flash, 0) < 0) {
David Hendricks52ddff02013-07-23 15:05:14 -07002021 ret = 1;
2022 msg_cdbg("FAILED.\n");
2023 goto out;
2024 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002025 } else {
2026 if (read_flash(flash, oldcontents, 0, size)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002027 ret = 1;
2028 msg_cdbg("FAILED.\n");
2029 goto out;
2030 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002031 }
hailfinger4c47e9d2010-10-19 22:06:20 +00002032 }
David Hendricks89a45e52011-11-22 16:56:22 -08002033 msg_cdbg("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00002034
David Hendricksdf29a832013-06-28 14:33:51 -07002035 /*
2036 * Note: This must be done after reading the file specified for the
2037 * -w/-v argument, if any, so that files specified using -i end up
2038 * in the "newcontents" buffer before being written.
2039 * See http://crbug.com/263495.
2040 */
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002041 if (handle_romentries(flash, oldcontents, newcontents)) {
2042 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002043 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002044 goto out;
2045 }
uwef6641642007-05-09 10:17:44 +00002046
stuge8ce3a3c2008-04-28 14:47:30 +00002047 if (write_it) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002048 // parse the new fmap
David Hendricksb907de32014-08-11 16:47:09 -07002049 if ((ret = cros_ec_prepare(newcontents, size))) {
2050 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002051 goto out;
2052 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002053
hailfingerb437e282010-11-04 01:04:27 +00002054 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2055 msg_cerr("Uh oh. Erase/write failed. Checking if "
2056 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002057 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002058 if (!memcmp(oldcontents, newcontents, size)) {
2059 msg_cinfo("Good. It seems nothing was "
2060 "changed.\n");
2061 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002062 ret = 1;
2063 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002064 }
2065 }
hailfingerd217d122010-10-08 18:52:29 +00002066 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002067 ret = 1;
2068 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002069 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002070
David Hendricksb907de32014-08-11 16:47:09 -07002071 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002072 if (ret < 0) {
2073 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002074 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002075 emergency_help_message();
2076 ret = 1;
2077 goto out;
2078 } else if (ret > 0) {
2079 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002080 msg_pdbg("CROS_EC needs 2nd pass.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002081 if (read_flash(flash, oldcontents, 0, size)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002082 msg_cerr("Uh oh. Cannot get latest content.\n");
2083 emergency_help_message();
2084 ret = 1;
2085 goto out;
2086 }
2087 // write 2nd pass
2088 if (erase_and_write_flash(flash, oldcontents,
2089 newcontents)) {
David Hendricksb907de32014-08-11 16:47:09 -07002090 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002091 emergency_help_message();
2092 ret = 1;
2093 goto out;
2094 }
2095 ret = 0;
2096 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002097
David Hendricksb907de32014-08-11 16:47:09 -07002098 if (cros_ec_finish() < 0) {
2099 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002100 emergency_help_message();
2101 ret = 1;
2102 goto out;
2103 }
stuge8ce3a3c2008-04-28 14:47:30 +00002104 }
ollie6a600992005-11-26 21:55:36 +00002105
hailfinger0459e1c2009-08-19 13:55:34 +00002106 if (verify_it) {
2107 /* Work around chips which need some time to calm down. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002108 if (write_it && verify_it != VERIFY_PARTIAL)
hailfinger0459e1c2009-08-19 13:55:34 +00002109 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002110
2111 ret = verify_flash(flash, newcontents, verify_it);
2112
hailfingera50d60e2009-11-17 09:57:34 +00002113 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00002114 * might have an emergency situation.
2115 */
2116 if (ret && write_it)
2117 emergency_help_message();
2118 }
ollie6a600992005-11-26 21:55:36 +00002119
hailfinger90fcf9b2010-11-05 14:51:59 +00002120out:
2121 free(oldcontents);
2122 free(newcontents);
2123out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002124 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002125 /*
2126 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2127 * tree. This is because some operations, such as write protection,
2128 * requires programmer_shutdown() but does not call doit().
2129 */
2130// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002131 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002132}