blob: 6f2a9fc193c7236f9e918ae8a34544e747100225 [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
uwe4475e902009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
hailfinger23060112009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
uweb25f1ea2007-08-29 17:52:32 +000019 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
rminnich8d3ff912003-10-25 17:01:29 +000022 */
23
hailfingera83a5fe2010-05-30 22:24:40 +000024#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000026#ifndef __LIBPAYLOAD__
27#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000028#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000029#endif
rminnich8d3ff912003-10-25 17:01:29 +000030#include <string.h>
31#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000032#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000033#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
rminnich8d3ff912003-10-25 17:01:29 +000037#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000038#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090039#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000040#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000041
krause2eb76212011-01-17 07:50:42 +000042const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000043char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000044int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000045
David Hendricks1ed1d352011-11-23 17:54:37 -080046/* error handling stuff */
47enum error_action access_denied_action = error_ignore;
48
49int ignore_error(int err) {
50 int rc = 0;
51
52 switch(err) {
53 case ACCESS_DENIED:
54 if (access_denied_action == error_ignore)
55 rc = 1;
56 break;
57 default:
58 break;
59 }
60
61 return rc;
62}
63
hailfinger969e2f32011-09-08 00:00:29 +000064static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000065
hailfingerddeb4ac2010-07-08 10:13:37 +000066static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000067
hailfinger1ff33dc2010-07-03 11:02:10 +000068/* Supported buses for the current programmer. */
69enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +000070
uwee15beb92010-08-08 17:01:18 +000071/*
hailfinger80422e22009-12-13 22:28:00 +000072 * Programmers supporting multiple buses can have differing size limits on
73 * each bus. Store the limits for each bus in a common struct.
74 */
hailfinger1ff33dc2010-07-03 11:02:10 +000075struct decode_sizes max_rom_decode;
76
77/* If nonzero, used as the start address of bottom-aligned flash. */
78unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000079
hailfinger5828baf2010-07-03 12:14:25 +000080/* Is writing allowed with this programmer? */
81int programmer_may_write;
82
hailfingerabe249e2009-05-08 17:43:22 +000083const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000084#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000085 {
hailfinger3548a9a2009-08-12 14:34:35 +000086 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000087 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000088 .map_flash_region = physmap,
89 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000090 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +000091 },
hailfinger80422e22009-12-13 22:28:00 +000092#endif
stepan927d4e22007-04-04 22:45:58 +000093
hailfinger90c7d542010-05-31 15:27:27 +000094#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +000095 {
hailfinger3548a9a2009-08-12 14:34:35 +000096 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +000097 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +000098 .map_flash_region = dummy_map,
99 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000100 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000101 },
hailfinger571a6b32009-09-16 10:09:21 +0000102#endif
hailfingera9df33c2009-05-09 00:54:55 +0000103
hailfinger90c7d542010-05-31 15:27:27 +0000104#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000105 {
hailfinger3548a9a2009-08-12 14:34:35 +0000106 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000107 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000108 .map_flash_region = fallback_map,
109 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000110 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000111 },
hailfinger571a6b32009-09-16 10:09:21 +0000112#endif
uwe0f5a3a22009-05-13 11:36:06 +0000113
hailfinger90c7d542010-05-31 15:27:27 +0000114#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000115 {
hailfinger0d703d42011-03-07 01:08:09 +0000116 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000117 .name = "nicrealtek",
118 //.name = "nicsmc1211",
119 .init = nicrealtek_init,
120 .map_flash_region = fallback_map,
121 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000122 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000123 },
hailfinger5aa36982010-05-21 21:54:07 +0000124#endif
125
hailfingerf0a368f2010-06-07 22:37:54 +0000126#if CONFIG_NICNATSEMI == 1
127 {
uwe8d342eb2011-07-28 08:13:25 +0000128 .name = "nicnatsemi",
129 .init = nicnatsemi_init,
130 .map_flash_region = fallback_map,
131 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000132 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000133 },
134#endif
hailfinger5aa36982010-05-21 21:54:07 +0000135
hailfinger90c7d542010-05-31 15:27:27 +0000136#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000137 {
138 .name = "gfxnvidia",
139 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000140 .map_flash_region = fallback_map,
141 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000142 .delay = internal_delay,
143 },
144#endif
145
hailfinger90c7d542010-05-31 15:27:27 +0000146#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000147 {
uwee2f95ef2009-09-02 23:00:46 +0000148 .name = "drkaiser",
149 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000150 .map_flash_region = fallback_map,
151 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000152 .delay = internal_delay,
153 },
hailfinger571a6b32009-09-16 10:09:21 +0000154#endif
uwee2f95ef2009-09-02 23:00:46 +0000155
hailfinger90c7d542010-05-31 15:27:27 +0000156#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000157 {
hailfinger3548a9a2009-08-12 14:34:35 +0000158 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000159 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000160 .map_flash_region = fallback_map,
161 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000162 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000163 },
hailfinger571a6b32009-09-16 10:09:21 +0000164#endif
ruikda922a12009-05-17 19:39:27 +0000165
hailfinger90c7d542010-05-31 15:27:27 +0000166#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000167 {
168 .name = "atahpt",
169 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000170 .map_flash_region = fallback_map,
171 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000172 .delay = internal_delay,
173 },
174#endif
175
hailfinger90c7d542010-05-31 15:27:27 +0000176#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000177 {
hailfinger90c7d542010-05-31 15:27:27 +0000178 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000179 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000180 .map_flash_region = fallback_map,
181 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000182 .delay = internal_delay,
183 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000184#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000185
hailfinger90c7d542010-05-31 15:27:27 +0000186#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000187 {
hailfinger3548a9a2009-08-12 14:34:35 +0000188 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000189 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000190 .map_flash_region = fallback_map,
191 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000192 .delay = serprog_delay,
193 },
hailfinger74d88a72009-08-12 16:17:41 +0000194#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000195
hailfinger90c7d542010-05-31 15:27:27 +0000196#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000197 {
hailfinger90c7d542010-05-31 15:27:27 +0000198 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000199 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000200 .map_flash_region = fallback_map,
201 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000202 .delay = internal_delay,
203 },
204#endif
205
hailfinger90c7d542010-05-31 15:27:27 +0000206#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000207 {
208 .name = "dediprog",
209 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000210 .map_flash_region = fallback_map,
211 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000212 .delay = internal_delay,
213 },
214#endif
215
hailfinger52c4fa02010-07-21 10:26:01 +0000216#if CONFIG_RAYER_SPI == 1
217 {
218 .name = "rayer_spi",
219 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000220 .map_flash_region = fallback_map,
221 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000222 .delay = internal_delay,
223 },
224#endif
225
hailfinger7949b652011-05-08 00:24:18 +0000226#if CONFIG_NICINTEL == 1
227 {
228 .name = "nicintel",
229 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000230 .map_flash_region = fallback_map,
231 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000232 .delay = internal_delay,
233 },
234#endif
235
uwe6764e922010-09-03 18:21:21 +0000236#if CONFIG_NICINTEL_SPI == 1
237 {
uwe8d342eb2011-07-28 08:13:25 +0000238 .name = "nicintel_spi",
239 .init = nicintel_spi_init,
240 .map_flash_region = fallback_map,
241 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000242 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000243 },
244#endif
245
hailfingerfb1f31f2010-12-03 14:48:11 +0000246#if CONFIG_OGP_SPI == 1
247 {
uwe8d342eb2011-07-28 08:13:25 +0000248 .name = "ogp_spi",
249 .init = ogp_spi_init,
250 .map_flash_region = fallback_map,
251 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000252 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000253 },
254#endif
255
hailfinger935365d2011-02-04 21:37:59 +0000256#if CONFIG_SATAMV == 1
257 {
258 .name = "satamv",
259 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000260 .map_flash_region = fallback_map,
261 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000262 .delay = internal_delay,
263 },
264#endif
265
uwe7df6dda2011-09-03 18:37:52 +0000266#if CONFIG_LINUX_SPI == 1
267 {
268 .name = "linux_spi",
269 .init = linux_spi_init,
270 .map_flash_region = fallback_map,
271 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000272 .delay = internal_delay,
273 },
274#endif
275
hailfinger3548a9a2009-08-12 14:34:35 +0000276 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000277};
stepan927d4e22007-04-04 22:45:58 +0000278
David Hendricksbf36f092010-11-02 23:39:29 -0700279#define CHIP_RESTORE_MAXFN 4
280static int chip_restore_fn_count = 0;
281struct chip_restore_func_data {
282 CHIP_RESTORE_CALLBACK;
283 struct flashchip *flash;
284 uint8_t status;
285} static chip_restore_fn[CHIP_RESTORE_MAXFN];
286
David Hendricks668f29d2011-01-27 18:51:45 -0800287
hailfingerf31cbdc2010-11-10 15:25:18 +0000288#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000289static int shutdown_fn_count = 0;
290struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000291 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000292 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000293} static shutdown_fn[SHUTDOWN_MAXFN];
294/* Initialize to 0 to make sure nobody registers a shutdown function before
295 * programmer init.
296 */
297static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000298
stefanct569dbb62011-07-01 00:19:12 +0000299static int check_block_eraser(const struct flashchip *flash, int k, int log);
300
hailfingerdc6f7972010-02-14 01:20:28 +0000301/* Register a function to be executed on programmer shutdown.
302 * The advantage over atexit() is that you can supply a void pointer which will
303 * be used as parameter to the registered function upon programmer shutdown.
304 * This pointer can point to arbitrary data used by said function, e.g. undo
305 * information for GPIO settings etc. If unneeded, set data=NULL.
306 * Please note that the first (void *data) belongs to the function signature of
307 * the function passed as first parameter.
308 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000309int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000310{
311 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000312 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000313 SHUTDOWN_MAXFN);
314 return 1;
315 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000316 if (!may_register_shutdown) {
317 msg_perr("Tried to register a shutdown function before "
318 "programmer init.\n");
319 return 1;
320 }
hailfingerdc6f7972010-02-14 01:20:28 +0000321 shutdown_fn[shutdown_fn_count].func = function;
322 shutdown_fn[shutdown_fn_count].data = data;
323 shutdown_fn_count++;
324
325 return 0;
326}
327
David Hendricksbf36f092010-11-02 23:39:29 -0700328//int register_chip_restore(int (*function) (void *data), void *data)
329int register_chip_restore(CHIP_RESTORE_CALLBACK,
330 struct flashchip *flash, uint8_t status)
331{
332 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
333 msg_perr("Tried to register more than %i chip restore"
334 " functions.\n", CHIP_RESTORE_MAXFN);
335 return 1;
336 }
337 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
338 chip_restore_fn[chip_restore_fn_count].flash = flash;
339 chip_restore_fn[chip_restore_fn_count].status = status;
340 chip_restore_fn_count++;
341
342 return 0;
343}
344
hailfinger969e2f32011-09-08 00:00:29 +0000345int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000346{
hailfinger1ef766d2010-07-06 09:55:48 +0000347 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000348
349 if (prog >= PROGRAMMER_INVALID) {
350 msg_perr("Invalid programmer specified!\n");
351 return -1;
352 }
353 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000354 /* Initialize all programmer specific data. */
355 /* Default to unlimited decode sizes. */
356 max_rom_decode = (const struct decode_sizes) {
357 .parallel = 0xffffffff,
358 .lpc = 0xffffffff,
359 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000360 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000361 };
hailfingere1e41ea2011-07-27 07:13:06 +0000362 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000363 /* Default to top aligned flash at 4 GB. */
364 flashbase = 0;
365 /* Registering shutdown functions is now allowed. */
366 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000367 /* Default to allowing writes. Broken programmers set this to 0. */
368 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000369
370 programmer_param = param;
371 msg_pdbg("Initializing %s programmer\n",
372 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000373 ret = programmer_table[programmer].init();
374 if (programmer_param && strlen(programmer_param)) {
375 msg_perr("Unhandled programmer parameters: %s\n",
376 programmer_param);
377 /* Do not error out here, the init itself was successful. */
378 }
379 return ret;
uweabe92a52009-05-16 22:36:00 +0000380}
381
David Hendricksbf36f092010-11-02 23:39:29 -0700382int chip_restore()
383{
384 int rc = 0;
385
386 while (chip_restore_fn_count > 0) {
387 int i = --chip_restore_fn_count;
388 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
389 chip_restore_fn[i].status);
390 }
391
392 return rc;
393}
394
uweabe92a52009-05-16 22:36:00 +0000395int programmer_shutdown(void)
396{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000397 int ret = 0;
398
hailfinger1ff33dc2010-07-03 11:02:10 +0000399 /* Registering shutdown functions is no longer allowed. */
400 may_register_shutdown = 0;
401 while (shutdown_fn_count > 0) {
402 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000403 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000404 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000405 return ret;
uweabe92a52009-05-16 22:36:00 +0000406}
407
408void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
409 size_t len)
410{
411 return programmer_table[programmer].map_flash_region(descr,
412 phys_addr, len);
413}
414
415void programmer_unmap_flash_region(void *virt_addr, size_t len)
416{
417 programmer_table[programmer].unmap_flash_region(virt_addr, len);
418}
419
420void chip_writeb(uint8_t val, chipaddr addr)
421{
hailfinger76bb7e92011-11-09 23:40:00 +0000422 par_programmer->chip_writeb(val, addr);
uweabe92a52009-05-16 22:36:00 +0000423}
424
425void chip_writew(uint16_t val, chipaddr addr)
426{
hailfinger76bb7e92011-11-09 23:40:00 +0000427 par_programmer->chip_writew(val, addr);
uweabe92a52009-05-16 22:36:00 +0000428}
429
430void chip_writel(uint32_t val, chipaddr addr)
431{
hailfinger76bb7e92011-11-09 23:40:00 +0000432 par_programmer->chip_writel(val, addr);
uweabe92a52009-05-16 22:36:00 +0000433}
434
hailfinger9d987ef2009-06-05 18:32:07 +0000435void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
436{
hailfinger76bb7e92011-11-09 23:40:00 +0000437 par_programmer->chip_writen(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000438}
439
uweabe92a52009-05-16 22:36:00 +0000440uint8_t chip_readb(const chipaddr addr)
441{
hailfinger76bb7e92011-11-09 23:40:00 +0000442 return par_programmer->chip_readb(addr);
uweabe92a52009-05-16 22:36:00 +0000443}
444
445uint16_t chip_readw(const chipaddr addr)
446{
hailfinger76bb7e92011-11-09 23:40:00 +0000447 return par_programmer->chip_readw(addr);
uweabe92a52009-05-16 22:36:00 +0000448}
449
450uint32_t chip_readl(const chipaddr addr)
451{
hailfinger76bb7e92011-11-09 23:40:00 +0000452 return par_programmer->chip_readl(addr);
uweabe92a52009-05-16 22:36:00 +0000453}
454
hailfinger9d987ef2009-06-05 18:32:07 +0000455void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
456{
hailfinger76bb7e92011-11-09 23:40:00 +0000457 par_programmer->chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000458}
459
hailfingere5829f62009-06-05 17:48:08 +0000460void programmer_delay(int usecs)
461{
462 programmer_table[programmer].delay(usecs);
463}
464
stuge5ff0e6c2009-01-26 00:39:57 +0000465void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000466{
stepan15e64bc2007-05-24 08:48:10 +0000467 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000468 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000469 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000470 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000471}
472
stefanctc5eb8a92011-11-23 09:13:48 +0000473int read_memmapped(struct flashchip *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000474{
hailfinger0f08b7a2009-06-16 08:55:44 +0000475 chip_readn(buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000476
hailfinger23060112009-05-08 12:49:03 +0000477 return 0;
478}
479
hailfinger7b414742009-06-13 12:04:03 +0000480int min(int a, int b)
481{
482 return (a < b) ? a : b;
483}
484
hailfinger7af83692009-06-15 17:23:36 +0000485int max(int a, int b)
486{
487 return (a > b) ? a : b;
488}
489
hailfingeraec9c962009-10-31 01:53:09 +0000490int bitcount(unsigned long a)
491{
492 int i = 0;
493 for (; a != 0; a >>= 1)
494 if (a & 1)
495 i++;
496 return i;
497}
498
hailfingerf76cc322010-11-09 22:00:31 +0000499void tolower_string(char *str)
500{
501 for (; *str != '\0'; str++)
502 *str = (char)tolower((unsigned char)*str);
503}
504
hailfingera916b422009-06-01 02:08:58 +0000505char *strcat_realloc(char *dest, const char *src)
506{
507 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000508 if (!dest) {
509 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000510 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000511 }
hailfingera916b422009-06-01 02:08:58 +0000512 strcat(dest, src);
513 return dest;
514}
515
hailfinger6e5a52a2009-11-24 18:27:10 +0000516/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000517 * It will look for needle with a subsequent '=' in haystack, return a copy of
518 * needle and remove everything from the first occurrence of needle to the next
519 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000520 */
stefanct52700282011-06-26 17:38:17 +0000521char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000522{
hailfinger1ef766d2010-07-06 09:55:48 +0000523 char *param_pos, *opt_pos, *rest;
524 char *opt = NULL;
525 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000526 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000527
hailfingerf4aaccc2010-04-28 15:22:14 +0000528 needlelen = strlen(needle);
529 if (!needlelen) {
530 msg_gerr("%s: empty needle! Please report a bug at "
531 "flashrom@flashrom.org\n", __func__);
532 return NULL;
533 }
534 /* No programmer parameters given. */
535 if (*haystack == NULL)
536 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000537 param_pos = strstr(*haystack, needle);
538 do {
539 if (!param_pos)
540 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000541 /* Needle followed by '='? */
542 if (param_pos[needlelen] == '=') {
543
544 /* Beginning of the string? */
545 if (param_pos == *haystack)
546 break;
547 /* After a delimiter? */
548 if (strchr(delim, *(param_pos - 1)))
549 break;
550 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000551 /* Continue searching. */
552 param_pos++;
553 param_pos = strstr(param_pos, needle);
554 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000555
hailfinger6e5a52a2009-11-24 18:27:10 +0000556 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000557 /* Get the string after needle and '='. */
558 opt_pos = param_pos + needlelen + 1;
559 optlen = strcspn(opt_pos, delim);
560 /* Return an empty string if the parameter was empty. */
561 opt = malloc(optlen + 1);
562 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000563 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000564 exit(1);
565 }
hailfinger1ef766d2010-07-06 09:55:48 +0000566 strncpy(opt, opt_pos, optlen);
567 opt[optlen] = '\0';
568 rest = opt_pos + optlen;
569 /* Skip all delimiters after the current parameter. */
570 rest += strspn(rest, delim);
571 memmove(param_pos, rest, strlen(rest) + 1);
572 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000573 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000574
hailfinger1ef766d2010-07-06 09:55:48 +0000575 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000576}
577
stefanct52700282011-06-26 17:38:17 +0000578char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000579{
580 return extract_param(&programmer_param, param_name, ",");
581}
582
stefancte1c5acf2011-07-04 07:27:17 +0000583/* Returns the number of well-defined erasers for a chip. */
584static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000585{
586 unsigned int usable_erasefunctions = 0;
587 int k;
588 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
589 if (!check_block_eraser(flash, k, 0))
590 usable_erasefunctions++;
591 }
592 return usable_erasefunctions;
593}
594
hailfinger7af83692009-06-15 17:23:36 +0000595/* start is an offset to the base address of the flash chip */
stefanctc5eb8a92011-11-23 09:13:48 +0000596int check_erased_range(struct flashchip *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000597{
598 int ret;
599 uint8_t *cmpbuf = malloc(len);
600
601 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000602 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000603 exit(1);
604 }
Simon Glass4c214132013-07-16 10:09:28 -0600605 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000606 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
607 free(cmpbuf);
608 return ret;
609}
610
uwee15beb92010-08-08 17:01:18 +0000611/*
hailfinger7af3d192009-11-25 17:05:52 +0000612 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000613 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000614 * @start offset to the base address of the flash chip
615 * @len length of the verified area
616 * @message string to print in the "FAILED" message
617 * @return 0 for success, -1 for failure
618 */
stefanctc5eb8a92011-11-23 09:13:48 +0000619int verify_range(struct flashchip *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000620 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000621{
stefanctc5eb8a92011-11-23 09:13:48 +0000622 unsigned int i;
hailfinger8cb6ece2010-11-16 17:21:58 +0000623 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000624 int ret = 0, failcount = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -0800625 unsigned int chunksize;
hailfinger7af83692009-06-15 17:23:36 +0000626
627 if (!len)
628 goto out_free;
629
hailfingerb0f4d122009-06-24 08:20:45 +0000630 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000631 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000632 return 1;
633 }
hailfinger7af83692009-06-15 17:23:36 +0000634 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000635 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000636 exit(1);
637 }
638
639 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000640 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000641 " total_size 0x%x\n", __func__, start, len,
642 flash->total_size * 1024);
643 ret = -1;
644 goto out_free;
645 }
646 if (!message)
647 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000648
David Hendricks1ed1d352011-11-23 17:54:37 -0800649 chunksize = min(flash->page_size, len);
650 for (i = 0; i < len; i += chunksize) {
651 int tmp, j;
652
653 tmp = flash->read(flash, readbuf + i, start + i, chunksize);
654
David Hendrickse3451942013-03-21 17:23:29 -0700655 /* Since this function explicitly compares the bytes, we need
656 to handle errors manually */
David Hendricks1ed1d352011-11-23 17:54:37 -0800657 if (tmp) {
658 ret = tmp;
659 if (ignore_error(tmp)) {
660 chunksize = min(flash->page_size, len - i);
661 continue;
662 } else {
663 goto out_free;
664 }
665 }
666
667 for (j = 0; j < chunksize; j++) {
668 if (cmpbuf[i + j] != readbuf[i + j]) {
669 /* Only print the first failure. */
670 if (!failcount++)
671 msg_cerr("%s FAILED at 0x%08x! "
672 "Expected=0x%02x, Read=0x%02x,",
673 message, start + i + j, cmpbuf[i + j],
674 readbuf[j]);
675 }
676 }
677 chunksize = min(flash->page_size, len - i);
hailfinger8cb6ece2010-11-16 17:21:58 +0000678 }
679
hailfinger5be6c0f2009-07-23 01:42:56 +0000680 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000681 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000682 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000683 ret = -1;
684 }
hailfinger7af83692009-06-15 17:23:36 +0000685
686out_free:
687 free(readbuf);
688 return ret;
689}
690
uwee15beb92010-08-08 17:01:18 +0000691/*
hailfingerb247c7a2010-03-08 00:42:32 +0000692 * Check if the buffer @have can be programmed to the content of @want without
693 * erasing. This is only possible if all chunks of size @gran are either kept
694 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000695 *
hailfingerb247c7a2010-03-08 00:42:32 +0000696 * The following write granularities (enum @gran) are known:
697 * - 1 bit. Each bit can be cleared individually.
698 * - 1 byte. A byte can be written once. Further writes to an already written
699 * byte cause the contents to be either undefined or to stay unchanged.
700 * - 128 bytes. If less than 128 bytes are written, the rest will be
701 * erased. Each write to a 128-byte region will trigger an automatic erase
702 * before anything is written. Very uncommon behaviour and unsupported by
703 * this function.
704 * - 256 bytes. If less than 256 bytes are written, the contents of the
705 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000706 * Warning: This function assumes that @have and @want point to naturally
707 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000708 *
709 * @have buffer with current content
710 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000711 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000712 * @gran write granularity (enum, not count)
713 * @return 0 if no erase is needed, 1 otherwise
714 */
Simon Glass4c214132013-07-16 10:09:28 -0600715static int need_erase(struct flashchip *flash, uint8_t *have, uint8_t *want,
716 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000717{
hailfingerb91c08c2011-08-15 19:54:20 +0000718 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000719 unsigned int i, j, limit;
Simon Glass4c214132013-07-16 10:09:28 -0600720 int erase_value = flash_erase_value(flash);
hailfingerb247c7a2010-03-08 00:42:32 +0000721
722 switch (gran) {
723 case write_gran_1bit:
724 for (i = 0; i < len; i++)
725 if ((have[i] & want[i]) != want[i]) {
726 result = 1;
727 break;
728 }
729 break;
730 case write_gran_1byte:
731 for (i = 0; i < len; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600732 if ((have[i] != want[i]) && (have[i] != erase_value)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000733 result = 1;
734 break;
735 }
736 break;
737 case write_gran_256bytes:
738 for (j = 0; j < len / 256; j++) {
739 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000740 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000741 if (!memcmp(have + j * 256, want + j * 256, limit))
742 continue;
743 /* have needs to be in erased state. */
744 for (i = 0; i < limit; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600745 if (have[j * 256 + i] != erase_value) {
hailfingerb247c7a2010-03-08 00:42:32 +0000746 result = 1;
747 break;
748 }
749 if (result)
750 break;
751 }
752 break;
hailfingerb437e282010-11-04 01:04:27 +0000753 default:
754 msg_cerr("%s: Unsupported granularity! Please report a bug at "
755 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000756 }
757 return result;
758}
759
hailfingerb437e282010-11-04 01:04:27 +0000760/**
761 * Check if the buffer @have needs to be programmed to get the content of @want.
762 * If yes, return 1 and fill in first_start with the start address of the
763 * write operation and first_len with the length of the first to-be-written
764 * chunk. If not, return 0 and leave first_start and first_len undefined.
765 *
766 * Warning: This function assumes that @have and @want point to naturally
767 * aligned regions.
768 *
769 * @have buffer with current content
770 * @want buffer with desired content
771 * @len length of the checked area
772 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000773 * @first_start offset of the first byte which needs to be written (passed in
774 * value is increased by the offset of the first needed write
775 * relative to have/want or unchanged if no write is needed)
776 * @return length of the first contiguous area which needs to be written
777 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000778 *
779 * FIXME: This function needs a parameter which tells it about coalescing
780 * in relation to the max write length of the programmer and the max write
781 * length of the chip.
782 */
stefanctc5eb8a92011-11-23 09:13:48 +0000783static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
784 unsigned int *first_start,
785 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000786{
stefanctc5eb8a92011-11-23 09:13:48 +0000787 int need_write = 0;
788 unsigned int rel_start = 0, first_len = 0;
789 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000790
hailfingerb437e282010-11-04 01:04:27 +0000791 switch (gran) {
792 case write_gran_1bit:
793 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000794 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000795 break;
796 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000797 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000798 break;
799 default:
800 msg_cerr("%s: Unsupported granularity! Please report a bug at "
801 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000802 /* Claim that no write was needed. A write with unknown
803 * granularity is too dangerous to try.
804 */
805 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000806 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000807 for (i = 0; i < len / stride; i++) {
808 limit = min(stride, len - i * stride);
809 /* Are 'have' and 'want' identical? */
810 if (memcmp(have + i * stride, want + i * stride, limit)) {
811 if (!need_write) {
812 /* First location where have and want differ. */
813 need_write = 1;
814 rel_start = i * stride;
815 }
816 } else {
817 if (need_write) {
818 /* First location where have and want
819 * do not differ anymore.
820 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000821 break;
822 }
823 }
824 }
hailfingerffb7f382010-12-06 13:05:44 +0000825 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000826 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000827 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000828 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000829}
830
hailfinger0c515352009-11-23 12:55:31 +0000831/* This function generates various test patterns useful for testing controller
832 * and chip communication as well as chip behaviour.
833 *
834 * If a byte can be written multiple times, each time keeping 0-bits at 0
835 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
836 * is essentially an AND operation. That's also the reason why this function
837 * provides the result of AND between various patterns.
838 *
839 * Below is a list of patterns (and their block length).
840 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
841 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
842 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
843 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
844 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
845 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
846 * Pattern 6 is 00 (1 Byte)
847 * Pattern 7 is ff (1 Byte)
848 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
849 * byte block.
850 *
851 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
852 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
853 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
854 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
855 * Pattern 12 is 00 (1 Byte)
856 * Pattern 13 is ff (1 Byte)
857 * Patterns 8-13 have no block number.
858 *
859 * Patterns 0-3 are created to detect and efficiently diagnose communication
860 * slips like missed bits or bytes and their repetitive nature gives good visual
861 * cues to the person inspecting the results. In addition, the following holds:
862 * AND Pattern 0/1 == Pattern 4
863 * AND Pattern 2/3 == Pattern 5
864 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
865 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
866 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
867 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
868 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
869 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
870 * Besides that, they provide for bit testing of the last two bytes of every
871 * 256 byte block which contains the block number for patterns 0-6.
872 * Patterns 10-11 are special purpose for detecting subblock aliasing with
873 * block sizes >256 bytes (some Dataflash chips etc.)
874 * AND Pattern 8/9 == Pattern 12
875 * AND Pattern 10/11 == Pattern 12
876 * Pattern 13 is the completely erased state.
877 * None of the patterns can detect aliasing at boundaries which are a multiple
878 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
879 */
880int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
881{
882 int i;
883
884 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000885 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000886 return 1;
887 }
888
889 switch (variant) {
890 case 0:
891 for (i = 0; i < size; i++)
892 buf[i] = (i & 0xf) << 4 | 0x5;
893 break;
894 case 1:
895 for (i = 0; i < size; i++)
896 buf[i] = (i & 0xf) << 4 | 0xa;
897 break;
898 case 2:
899 for (i = 0; i < size; i++)
900 buf[i] = 0x50 | (i & 0xf);
901 break;
902 case 3:
903 for (i = 0; i < size; i++)
904 buf[i] = 0xa0 | (i & 0xf);
905 break;
906 case 4:
907 for (i = 0; i < size; i++)
908 buf[i] = (i & 0xf) << 4;
909 break;
910 case 5:
911 for (i = 0; i < size; i++)
912 buf[i] = i & 0xf;
913 break;
914 case 6:
915 memset(buf, 0x00, size);
916 break;
917 case 7:
918 memset(buf, 0xff, size);
919 break;
920 case 8:
921 for (i = 0; i < size; i++)
922 buf[i] = i & 0xff;
923 break;
924 case 9:
925 for (i = 0; i < size; i++)
926 buf[i] = ~(i & 0xff);
927 break;
928 case 10:
929 for (i = 0; i < size % 2; i++) {
930 buf[i * 2] = (i >> 8) & 0xff;
931 buf[i * 2 + 1] = i & 0xff;
932 }
933 if (size & 0x1)
934 buf[i * 2] = (i >> 8) & 0xff;
935 break;
936 case 11:
937 for (i = 0; i < size % 2; i++) {
938 buf[i * 2] = ~((i >> 8) & 0xff);
939 buf[i * 2 + 1] = ~(i & 0xff);
940 }
941 if (size & 0x1)
942 buf[i * 2] = ~((i >> 8) & 0xff);
943 break;
944 case 12:
945 memset(buf, 0x00, size);
946 break;
947 case 13:
948 memset(buf, 0xff, size);
949 break;
950 }
951
952 if ((variant >= 0) && (variant <= 7)) {
953 /* Write block number in the last two bytes of each 256-byte
954 * block, big endian for easier reading of the hexdump.
955 * Note that this wraps around for chips larger than 2^24 bytes
956 * (16 MB).
957 */
958 for (i = 0; i < size / 256; i++) {
959 buf[i * 256 + 254] = (i >> 8) & 0xff;
960 buf[i * 256 + 255] = i & 0xff;
961 }
962 }
963
964 return 0;
965}
966
hailfingeraec9c962009-10-31 01:53:09 +0000967int check_max_decode(enum chipbustype buses, uint32_t size)
968{
969 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +0000970
971 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000972 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000973 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000974 "size %u kB of chipset/board/programmer "
975 "for %s interface, "
976 "probe/read/erase/write may fail. ", size / 1024,
977 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +0000978 }
hailfingere1e41ea2011-07-27 07:13:06 +0000979 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000980 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000981 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000982 "size %u kB of chipset/board/programmer "
983 "for %s interface, "
984 "probe/read/erase/write may fail. ", size / 1024,
985 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +0000986 }
hailfingere1e41ea2011-07-27 07:13:06 +0000987 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000988 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000989 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000990 "size %u kB of chipset/board/programmer "
991 "for %s interface, "
992 "probe/read/erase/write may fail. ", size / 1024,
993 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +0000994 }
hailfingere1e41ea2011-07-27 07:13:06 +0000995 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000996 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000997 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000998 "size %u kB of chipset/board/programmer "
999 "for %s interface, "
1000 "probe/read/erase/write may fail. ", size / 1024,
1001 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001002 }
1003 if (!limitexceeded)
1004 return 0;
1005 /* Sometimes chip and programmer have more than one bus in common,
1006 * and the limit is not exceeded on all buses. Tell the user.
1007 */
1008 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001009 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001010 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001011 "interface which can support a chip of this size. "
1012 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001013 return 1;
1014}
1015
hailfinger48ed3e22011-05-04 00:39:50 +00001016int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001017{
hailfinger48ed3e22011-05-04 00:39:50 +00001018 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001019 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001020 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001021 uint32_t size;
1022 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001023 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001024
hailfinger48ed3e22011-05-04 00:39:50 +00001025 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001026 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001027 continue;
hailfingeraec9c962009-10-31 01:53:09 +00001028 buses_common = buses_supported & flash->bustype;
1029 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001030 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1031 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +00001032 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +00001033 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001034 free(tmp);
1035 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001036 msg_gspew("and chip bus type %s are incompatible.",
1037 tmp);
hailfingera916b422009-06-01 02:08:58 +00001038 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001039 msg_gspew("\n");
1040 continue;
1041 }
1042 msg_gdbg("Probing for %s %s, %d kB: ",
1043 flash->vendor, flash->name, flash->total_size);
1044 if (!flash->probe && !force) {
1045 msg_gdbg("failed! flashrom has no probe function for "
1046 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001047 continue;
1048 }
stepan782fb172007-04-06 11:58:03 +00001049
ollie5672ac62004-03-17 22:22:08 +00001050 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001051 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001052
hailfinger48ed3e22011-05-04 00:39:50 +00001053 /* Start filling in the dynamic data. */
1054 *fill_flash = *flash;
1055
hailfinger72d3b982009-05-09 07:27:23 +00001056 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001057 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001058
stugec1e55fe2008-07-02 17:15:47 +00001059 if (force)
1060 break;
stepanc98b80b2006-03-16 16:57:41 +00001061
hailfinger48ed3e22011-05-04 00:39:50 +00001062 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001063 goto notfound;
1064
hailfinger48ed3e22011-05-04 00:39:50 +00001065 /* If this is the first chip found, accept it.
1066 * If this is not the first chip found, accept it only if it is
1067 * a non-generic match.
1068 * We could either make chipcount global or provide it as
1069 * parameter, or we assume that startchip==0 means this call to
1070 * probe_flash() is the first one and thus no chip has been
1071 * found before.
1072 */
1073 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001074 break;
1075
stuge56300c32008-09-03 23:10:05 +00001076notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001077 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001078 }
uwebe4477b2007-08-23 16:08:21 +00001079
stugec1e55fe2008-07-02 17:15:47 +00001080 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001081 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001082
hailfingere11396b2011-03-08 00:09:11 +00001083#if CONFIG_INTERNAL == 1
1084 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001085 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001086 else
1087#endif
stepan3e7aeae2011-01-19 06:21:54 +00001088 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001089
stefanct588b6d22011-06-26 20:45:35 +00001090 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001091 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1092 force ? "Assuming" : "Found", fill_flash->vendor,
1093 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001094 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001095
hailfinger0f4c3952010-12-02 21:59:42 +00001096 /* Flash registers will not be mapped if the chip was forced. Lock info
1097 * may be stored in registers, so avoid lock info printing.
1098 */
1099 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001100 if (fill_flash->printlock)
1101 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001102
hailfinger48ed3e22011-05-04 00:39:50 +00001103 /* Return position of matching chip. */
1104 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001105}
1106
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001107int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001108{
hailfingerb0f4d122009-06-24 08:20:45 +00001109 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001110 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001111
snelsone42c3802010-05-07 20:09:04 +00001112 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001113
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001114 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1115 ret = handle_partial_verify(flash, buf, verify_range);
1116 } else {
1117 ret = verify_range(flash, buf, 0, total_size, NULL);
1118 }
uwef6641642007-05-09 10:17:44 +00001119
David Hendricks1ed1d352011-11-23 17:54:37 -08001120 if (ret == ACCESS_DENIED) {
1121 msg_gdbg("Could not fully verify due to access error, ");
1122 if (access_denied_action == error_ignore) {
1123 msg_gdbg("ignoring\n");
1124 ret = 0;
1125 } else {
1126 msg_gdbg("aborting\n");
1127 }
1128 }
1129
hailfingerb0f4d122009-06-24 08:20:45 +00001130 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001131 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001132
hailfingerb0f4d122009-06-24 08:20:45 +00001133 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001134}
1135
uwe8d342eb2011-07-28 08:13:25 +00001136int read_buf_from_file(unsigned char *buf, unsigned long size,
1137 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001138{
1139 unsigned long numbytes;
1140 FILE *image;
1141 struct stat image_stat;
1142
1143 if ((image = fopen(filename, "rb")) == NULL) {
1144 perror(filename);
1145 return 1;
1146 }
1147 if (fstat(fileno(image), &image_stat) != 0) {
1148 perror(filename);
1149 fclose(image);
1150 return 1;
1151 }
1152 if (image_stat.st_size != size) {
1153 msg_gerr("Error: Image size doesn't match\n");
1154 fclose(image);
1155 return 1;
1156 }
1157 numbytes = fread(buf, 1, size, image);
1158 if (fclose(image)) {
1159 perror(filename);
1160 return 1;
1161 }
1162 if (numbytes != size) {
1163 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1164 "wanted %ld!\n", numbytes, size);
1165 return 1;
1166 }
1167 return 0;
1168}
1169
uwe8d342eb2011-07-28 08:13:25 +00001170int write_buf_to_file(unsigned char *buf, unsigned long size,
1171 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001172{
1173 unsigned long numbytes;
1174 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001175
1176 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001177 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001178 return 1;
1179 }
oxygenebf70d352010-01-25 22:55:33 +00001180 if ((image = fopen(filename, "wb")) == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001181 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001182 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001183 }
hailfingerd219a232009-01-28 00:27:54 +00001184
hailfingerd219a232009-01-28 00:27:54 +00001185 numbytes = fwrite(buf, 1, size, image);
1186 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001187 if (numbytes != size) {
1188 msg_gerr("File %s could not be written completely.\n",
1189 filename);
hailfingerd219a232009-01-28 00:27:54 +00001190 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001191 }
hailfingerd219a232009-01-28 00:27:54 +00001192 return 0;
1193}
1194
David Hendrickse3451942013-03-21 17:23:29 -07001195/*
1196 * read_flash - wrapper for flash->read() with additional high-level policy
1197 *
1198 * @flash flash chip
1199 * @buf buffer to store data in
1200 * @start start address
1201 * @len number of bytes to read
1202 *
1203 * This wrapper simplifies most cases when the flash chip needs to be read
1204 * since policy decisions such as non-fatal error handling is centralized.
1205 */
1206int read_flash(struct flashchip *flash, uint8_t *buf,
1207 unsigned int start, unsigned int len)
1208{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001209 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001210
1211 if (!flash || !flash->read)
1212 return -1;
1213
David Hendricks4e76fdc2013-05-13 16:05:36 -07001214 ret = flash->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001215 if (ret) {
1216 if (ignore_error(ret)) {
1217 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1218 start, start + len - 1);
1219 ret = 0;
1220 } else {
1221 msg_gdbg("failed to read 0x%x-0x%x\n",
1222 start, start + len - 1);
1223 }
1224 }
1225
1226 return ret;
1227}
1228
David Hendricks7c8a1612013-04-26 19:14:44 -07001229/*
1230 * write_flash - wrapper for flash->write() with additional high-level policy
1231 *
1232 * @flash flash chip
1233 * @buf buffer to write to flash
1234 * @start start address in flash
1235 * @len number of bytes to write
1236 *
1237 * TODO: Look up regions that are write-protected and avoid attempt to write
1238 * to them at all.
1239 */
1240int write_flash(struct flashchip *flash, uint8_t *buf,
1241 unsigned int start, unsigned int len)
1242{
1243 if (!flash || !flash->write)
1244 return -1;
1245
1246 return flash->write(flash, buf, start, len);
1247}
1248
stefanct52700282011-06-26 17:38:17 +00001249int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001250{
1251 unsigned long size = flash->total_size * 1024;
1252 unsigned char *buf = calloc(size, sizeof(char));
1253 int ret = 0;
1254
1255 msg_cinfo("Reading flash... ");
1256 if (!buf) {
1257 msg_gerr("Memory allocation failed!\n");
1258 msg_cinfo("FAILED.\n");
1259 return 1;
1260 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001261
1262 /* To support partial read, fill buffer to all 0xFF at beginning to make
1263 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001264 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001265
hailfinger42a850a2010-07-13 23:56:13 +00001266 if (!flash->read) {
1267 msg_cerr("No read function available for this flash chip.\n");
1268 ret = 1;
1269 goto out_free;
1270 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001271
1272 /* First try to handle partial read case, rather than read the whole
1273 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001274 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001275 if (ret < 0) {
1276 msg_cerr("Partial read operation failed!\n");
1277 ret = 1;
1278 goto out_free;
1279 } else if (ret > 0) {
1280 /* Partial read has been handled, pass the whole flash read. */
David Hendricks1ed1d352011-11-23 17:54:37 -08001281 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001282 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001283 msg_cerr("Read operation failed!\n");
1284 ret = 1;
1285 goto out_free;
1286 }
hailfinger42a850a2010-07-13 23:56:13 +00001287 }
1288
stefanct9e6b98a2011-05-28 02:37:14 +00001289 ret = write_buf_to_file(buf, size, filename);
hailfinger42a850a2010-07-13 23:56:13 +00001290out_free:
1291 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001292 if (ret)
1293 msg_cerr("FAILED.");
1294 else
1295 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001296 return ret;
1297}
1298
hailfingerb437e282010-11-04 01:04:27 +00001299/* This function shares a lot of its structure with erase_and_write_flash() and
1300 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001301 * Even if an error is found, the function will keep going and check the rest.
1302 */
hailfinger48ed3e22011-05-04 00:39:50 +00001303static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001304{
hailfingerb91c08c2011-08-15 19:54:20 +00001305 int i, j, k;
1306 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001307
1308 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1309 unsigned int done = 0;
1310 struct block_eraser eraser = flash->block_erasers[k];
1311
1312 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1313 /* Blocks with zero size are bugs in flashchips.c. */
1314 if (eraser.eraseblocks[i].count &&
1315 !eraser.eraseblocks[i].size) {
1316 msg_gerr("ERROR: Flash chip %s erase function "
1317 "%i region %i has size 0. Please report"
1318 " a bug at flashrom@flashrom.org\n",
1319 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001320 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001321 }
1322 /* Blocks with zero count are bugs in flashchips.c. */
1323 if (!eraser.eraseblocks[i].count &&
1324 eraser.eraseblocks[i].size) {
1325 msg_gerr("ERROR: Flash chip %s erase function "
1326 "%i region %i has count 0. Please report"
1327 " a bug at flashrom@flashrom.org\n",
1328 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001329 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001330 }
1331 done += eraser.eraseblocks[i].count *
1332 eraser.eraseblocks[i].size;
1333 }
hailfinger9fed35d2010-01-19 06:42:46 +00001334 /* Empty eraseblock definition with erase function. */
1335 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001336 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001337 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001338 if (!done)
1339 continue;
1340 if (done != flash->total_size * 1024) {
1341 msg_gerr("ERROR: Flash chip %s erase function %i "
1342 "region walking resulted in 0x%06x bytes total,"
1343 " expected 0x%06x bytes. Please report a bug at"
1344 " flashrom@flashrom.org\n", flash->name, k,
1345 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001346 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001347 }
hailfinger9fed35d2010-01-19 06:42:46 +00001348 if (!eraser.block_erase)
1349 continue;
1350 /* Check if there are identical erase functions for different
1351 * layouts. That would imply "magic" erase functions. The
1352 * easiest way to check this is with function pointers.
1353 */
uwef6f94d42010-03-13 17:28:29 +00001354 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001355 if (eraser.block_erase ==
1356 flash->block_erasers[j].block_erase) {
1357 msg_gerr("ERROR: Flash chip %s erase function "
1358 "%i and %i are identical. Please report"
1359 " a bug at flashrom@flashrom.org\n",
1360 flash->name, k, j);
1361 ret = 1;
1362 }
uwef6f94d42010-03-13 17:28:29 +00001363 }
hailfinger45177872010-01-18 08:14:43 +00001364 }
hailfinger9fed35d2010-01-19 06:42:46 +00001365 return ret;
hailfinger45177872010-01-18 08:14:43 +00001366}
1367
hailfingerb437e282010-11-04 01:04:27 +00001368static int erase_and_write_block_helper(struct flashchip *flash,
1369 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001370 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001371 uint8_t *newcontents,
1372 int (*erasefn) (struct flashchip *flash,
1373 unsigned int addr,
1374 unsigned int len))
1375{
stefanctc5eb8a92011-11-23 09:13:48 +00001376 unsigned int starthere = 0, lenhere = 0;
1377 int ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001378 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1379
hailfinger90fcf9b2010-11-05 14:51:59 +00001380 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001381 * need to be adjusted here to keep the impression of proper abstraction
1382 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001383 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001384 newcontents += start;
1385 msg_cdbg(":");
1386 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001387 if (need_erase(flash, curcontents, newcontents, len, gran)) {
hailfingerb437e282010-11-04 01:04:27 +00001388 msg_cdbg("E");
1389 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001390 if (ret) {
1391 if (ret == ACCESS_DENIED)
1392 msg_cdbg("D");
1393 else
1394 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001395 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001396 }
1397
hailfingerac8e3182011-06-26 17:04:16 +00001398 if (check_erased_range(flash, start, len)) {
1399 msg_cerr("ERASE FAILED!\n");
1400 return -1;
1401 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001402 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001403 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001404 skip = 0;
1405 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001406 /* get_next_write() sets starthere to a new value after the call. */
1407 while ((lenhere = get_next_write(curcontents + starthere,
1408 newcontents + starthere,
1409 len - starthere, &starthere, gran))) {
hailfingerb437e282010-11-04 01:04:27 +00001410 if (!writecount++)
1411 msg_cdbg("W");
1412 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001413 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001414 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001415 if (ret) {
1416 if (ret == ACCESS_DENIED)
1417 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001418 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001419 }
hailfingerb437e282010-11-04 01:04:27 +00001420 starthere += lenhere;
1421 skip = 0;
1422 }
1423 if (skip)
1424 msg_cdbg("S");
1425 return ret;
1426}
1427
hailfinger83541b32010-07-13 00:42:00 +00001428static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1429 int (*do_something) (struct flashchip *flash,
1430 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001431 unsigned int len,
1432 uint8_t *param1,
1433 uint8_t *param2,
1434 int (*erasefn) (
1435 struct flashchip *flash,
1436 unsigned int addr,
1437 unsigned int len)),
1438 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001439{
David Hendricks1ed1d352011-11-23 17:54:37 -08001440 int i, j, rc = 0;
hailfingerb91c08c2011-08-15 19:54:20 +00001441 unsigned int start = 0;
1442 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001443 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001444
hailfinger2b8c9382010-07-13 00:37:19 +00001445 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1446 /* count==0 for all automatically initialized array
1447 * members so the loop below won't be executed for them.
1448 */
1449 len = eraser.eraseblocks[i].size;
1450 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001451 /* Print this for every block except the first one. */
1452 if (i || j)
1453 msg_cdbg(", ");
1454 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001455 start + len - 1);
David Hendricks1ed1d352011-11-23 17:54:37 -08001456 rc = do_something(flash, start, len, param1, param2,
1457 eraser.block_erase);
1458 if (rc) {
1459 if (ignore_error(rc))
1460 rc = 0;
1461 else
1462 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001463 }
hailfinger2b8c9382010-07-13 00:37:19 +00001464 start += len;
1465 }
1466 }
hailfingerb437e282010-11-04 01:04:27 +00001467 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001468 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001469}
1470
stefanct569dbb62011-07-01 00:19:12 +00001471static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001472{
1473 struct block_eraser eraser = flash->block_erasers[k];
1474
1475 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1476 if (log)
1477 msg_cdbg("not defined. ");
1478 return 1;
1479 }
1480 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1481 if (log)
1482 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001483 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001484 return 1;
1485 }
1486 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1487 if (log)
1488 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001489 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001490 return 1;
1491 }
1492 return 0;
1493}
1494
uwe8d342eb2011-07-28 08:13:25 +00001495int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1496 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001497{
hailfinger8f524a82011-07-21 21:21:04 +00001498 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001499 uint8_t *curcontents;
1500 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001501 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001502
hailfingercf848f12010-12-05 15:14:44 +00001503 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001504 curcontents = malloc(size);
1505 if (!curcontents) {
1506 msg_gerr("Out of memory!\n");
1507 exit(1);
1508 }
hailfingerb437e282010-11-04 01:04:27 +00001509 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1510 memcpy(curcontents, oldcontents, size);
1511
hailfinger7df21362009-09-05 02:30:58 +00001512 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001513 if (k != 0)
1514 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001515 if (!usable_erasefunctions) {
1516 msg_cdbg("No usable erase functions left.\n");
1517 break;
1518 }
stefanctf0bc4712011-07-26 14:28:35 +00001519 msg_cdbg("Trying erase function %i... ", k);
1520 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001521 continue;
hailfingercf848f12010-12-05 15:14:44 +00001522 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001523 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1524 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001525 /* If everything is OK, don't try another erase function. */
1526 if (!ret)
1527 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001528 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001529 * contents are. If no usable erase functions remain, we can
1530 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001531 */
hailfingercf848f12010-12-05 15:14:44 +00001532 if (!usable_erasefunctions)
1533 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001534 /* Reading the whole chip may take a while, inform the user even
1535 * in non-verbose mode.
1536 */
1537 msg_cinfo("Reading current flash chip contents... ");
David Hendrickse3451942013-03-21 17:23:29 -07001538 if (read_flash(flash, curcontents, 0, size)) {
hailfinger6237f5e2010-12-02 02:41:55 +00001539 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001540 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001541 /* We have no idea about the flash chip contents, so
1542 * retrying with another erase function is pointless.
1543 */
1544 break;
1545 }
David Hendricks89a45e52011-11-22 16:56:22 -08001546 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001547 }
hailfingerb437e282010-11-04 01:04:27 +00001548 /* Free the scratchpad. */
1549 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001550
hailfinger7df21362009-09-05 02:30:58 +00001551 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001552 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001553 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001554 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001555 }
1556 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001557}
1558
hailfinger4c47e9d2010-10-19 22:06:20 +00001559void nonfatal_help_message(void)
1560{
1561 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1562 "This means we have to add special support for your board, "
1563 "programmer or flash chip.\n"
1564 "Please report this on IRC at irc.freenode.net (channel "
1565 "#flashrom) or\n"
1566 "mail flashrom@flashrom.org!\n"
1567 "-------------------------------------------------------------"
1568 "------------------\n"
1569 "You may now reboot or simply leave the machine running.\n");
1570}
1571
uweb34ec9f2009-10-01 18:40:02 +00001572void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001573{
snelsone42c3802010-05-07 20:09:04 +00001574 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001575 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001576 "mail flashrom@flashrom.org with FAILED: your board name in "
1577 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001578 "-------------------------------------------------------------"
1579 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001580 "DO NOT REBOOT OR POWEROFF!\n");
1581}
1582
uwe8d342eb2011-07-28 08:13:25 +00001583/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001584void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001585{
1586 enum programmer p;
1587 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001588 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001589 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001590 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001591 }
snelsone42c3802010-05-07 20:09:04 +00001592 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001593}
1594
hailfingerf79d1712010-10-06 23:48:34 +00001595void list_programmers_linebreak(int startcol, int cols, int paren)
1596{
1597 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001598 int pnamelen;
1599 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001600 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001601 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001602
1603 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1604 pname = programmer_table[p].name;
1605 pnamelen = strlen(pname);
1606 if (remaining - pnamelen - 2 < 0) {
1607 if (firstline)
1608 firstline = 0;
1609 else
1610 printf("\n");
1611 for (i = 0; i < startcol; i++)
1612 printf(" ");
1613 remaining = cols - startcol;
1614 } else {
1615 printf(" ");
1616 remaining--;
1617 }
1618 if (paren && (p == 0)) {
1619 printf("(");
1620 remaining--;
1621 }
1622 printf("%s", pname);
1623 remaining -= pnamelen;
1624 if (p < PROGRAMMER_INVALID - 1) {
1625 printf(",");
1626 remaining--;
1627 } else {
1628 if (paren)
1629 printf(")");
1630 printf("\n");
1631 }
1632 }
1633}
1634
hailfinger3b471632010-03-27 16:36:40 +00001635void print_sysinfo(void)
1636{
David Hendricksc6c9f822010-11-03 15:07:01 -07001637 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001638#if HAVE_UTSNAME == 1
1639 struct utsname osinfo;
1640 uname(&osinfo);
1641
David Hendricksc6c9f822010-11-03 15:07:01 -07001642 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001643 osinfo.machine);
1644#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001645 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001646#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001647 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001648#if NEED_PCI == 1
1649#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001650 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001651#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001652 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001653#endif
1654#endif
1655#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001656 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001657#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001658 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001659#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001660 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001661#endif
hailfinger3b471632010-03-27 16:36:40 +00001662#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001663 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001664#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001665 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001666#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001667 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001668#endif
1669#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001670 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001671#endif
1672#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001673 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001674#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001675 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001676#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001677 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001678}
1679
uwefdeca092008-01-21 15:24:22 +00001680void print_version(void)
1681{
David Hendricksc6c9f822010-11-03 15:07:01 -07001682 /* send to stderr for chromium os */
1683 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001684 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001685}
1686
hailfinger74819ad2010-05-15 15:04:37 +00001687void print_banner(void)
1688{
1689 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001690 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001691 msg_ginfo("\n");
1692}
1693
hailfingerc77acb52009-12-24 02:15:55 +00001694int selfcheck(void)
1695{
hailfinger45177872010-01-18 08:14:43 +00001696 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001697 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001698
1699 /* Safety check. Instead of aborting after the first error, check
1700 * if more errors exist.
1701 */
hailfingerc77acb52009-12-24 02:15:55 +00001702 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001703 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001704 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001705 }
stefanct8632c922011-07-26 14:33:46 +00001706 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001707 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001708 * For 'flashchips' we check the first element to be non-null. In the
1709 * other cases there exist use cases where the first element can be
1710 * null. */
1711 if (flashchips == NULL || flashchips[0].vendor == NULL) {
1712 msg_gerr("Flashchips table miscompilation!\n");
1713 ret = 1;
1714 }
hailfinger45177872010-01-18 08:14:43 +00001715 for (flash = flashchips; flash && flash->name; flash++)
1716 if (selfcheck_eraseblocks(flash))
1717 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001718
1719#if CONFIG_INTERNAL == 1
1720 if (chipset_enables == NULL) {
1721 msg_gerr("Chipset enables table does not exist!\n");
1722 ret = 1;
1723 }
hailfinger4640bdb2011-08-31 16:19:50 +00001724 if (board_matches == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001725 msg_gerr("Board enables table does not exist!\n");
1726 ret = 1;
1727 }
1728 if (boards_known == NULL) {
1729 msg_gerr("Known boards table does not exist!\n");
1730 ret = 1;
1731 }
1732 if (laptops_known == NULL) {
1733 msg_gerr("Known laptops table does not exist!\n");
1734 ret = 1;
1735 }
uwe8d342eb2011-07-28 08:13:25 +00001736#endif
hailfinger45177872010-01-18 08:14:43 +00001737 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001738}
1739
hailfinger48ed3e22011-05-04 00:39:50 +00001740void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001741{
1742 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001743 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001744 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001745 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001746 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001747 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001748 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001749 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001750 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001751 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001752 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001753 msg_cdbg(" WRITE");
1754 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001755 }
1756 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1757 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1758 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1759 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001760 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001761 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001762 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001763 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001764 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001765 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001766 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001767 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001768 msg_cdbg(" WRITE");
1769 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001770 }
hailfinger92cd8e32010-01-07 03:24:05 +00001771 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001772 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001773 "in the latest development\n"
1774 "version of flashrom. If you are running the latest "
1775 "development version,\n"
1776 "please email a report to flashrom@flashrom.org if "
1777 "any of the above operations\n"
1778 "work correctly for you with this flash part. Please "
1779 "include the flashrom\n"
1780 "output with the additional -V option for all "
1781 "operations you tested (-V, -Vr,\n"
1782 "-Vw, -VE), and mention which mainboard or "
1783 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001784 "Please mention your board in the subject line. "
1785 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001786 }
1787}
1788
hailfinger771fc182010-10-15 00:01:14 +00001789/* FIXME: This function signature needs to be improved once doit() has a better
1790 * function signature.
1791 */
stefanct02116582011-05-18 01:30:56 +00001792int 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 +00001793{
1794 if (!programmer_may_write && (write_it || erase_it)) {
1795 msg_perr("Write/erase is not working yet on your programmer in "
1796 "its current configuration.\n");
1797 /* --force is the wrong approach, but it's the best we can do
1798 * until the generic programmer parameter parser is merged.
1799 */
1800 if (!force)
1801 return 1;
1802 msg_cerr("Continuing anyway.\n");
1803 }
1804
1805 if (read_it || erase_it || write_it || verify_it) {
1806 /* Everything needs read. */
1807 if (flash->tested & TEST_BAD_READ) {
1808 msg_cerr("Read is not working on this chip. ");
1809 if (!force)
1810 return 1;
1811 msg_cerr("Continuing anyway.\n");
1812 }
1813 if (!flash->read) {
1814 msg_cerr("flashrom has no read function for this "
1815 "flash chip.\n");
1816 return 1;
1817 }
1818 }
1819 if (erase_it || write_it) {
1820 /* Write needs erase. */
1821 if (flash->tested & TEST_BAD_ERASE) {
1822 msg_cerr("Erase is not working on this chip. ");
1823 if (!force)
1824 return 1;
1825 msg_cerr("Continuing anyway.\n");
1826 }
stefancte1c5acf2011-07-04 07:27:17 +00001827 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001828 msg_cerr("flashrom has no erase function for this "
1829 "flash chip.\n");
1830 return 1;
1831 }
hailfinger771fc182010-10-15 00:01:14 +00001832 }
1833 if (write_it) {
1834 if (flash->tested & TEST_BAD_WRITE) {
1835 msg_cerr("Write is not working on this chip. ");
1836 if (!force)
1837 return 1;
1838 msg_cerr("Continuing anyway.\n");
1839 }
1840 if (!flash->write) {
1841 msg_cerr("flashrom has no write function for this "
1842 "flash chip.\n");
1843 return 1;
1844 }
1845 }
1846 return 0;
1847}
1848
hailfingerc77acb52009-12-24 02:15:55 +00001849/* This function signature is horrible. We need to design a better interface,
1850 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001851 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001852 */
Simon Glass9ad06c12013-07-03 22:08:17 +09001853int doit(struct flashchip *flash, int force, const char *filename, int read_it,
1854 int write_it, int erase_it, int verify_it, int extract_it,
1855 const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001856{
hailfinger4c47e9d2010-10-19 22:06:20 +00001857 uint8_t *oldcontents;
1858 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001859 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001860 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001861
stefanct02116582011-05-18 01:30:56 +00001862 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001863 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001864 ret = 1;
1865 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001866 }
1867
hailfinger771fc182010-10-15 00:01:14 +00001868 /* Given the existence of read locks, we want to unlock for read,
1869 * erase and write.
1870 */
1871 if (flash->unlock)
1872 flash->unlock(flash);
1873
David Hendricks6d62d752011-03-07 21:20:22 -08001874 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001875 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001876 ret = 1;
1877 goto out_nofree;
1878 }
1879
Simon Glass9ad06c12013-07-03 22:08:17 +09001880 if (extract_it) {
1881 ret = extract_regions(flash);
1882 goto out_nofree;
1883 }
1884
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001885 /* mark entries included using -i argument as "included" if they are
1886 found in the master rom_entries list */
1887 if (process_include_args() < 0) {
1888 ret = 1;
1889 goto out_nofree;
1890 }
1891
hailfinger771fc182010-10-15 00:01:14 +00001892 if (read_it) {
1893 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001894 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001895 }
hailfingerb437e282010-11-04 01:04:27 +00001896
stefanctd611e8f2011-07-12 22:35:21 +00001897 oldcontents = malloc(size);
1898 if (!oldcontents) {
1899 msg_gerr("Out of memory!\n");
1900 exit(1);
1901 }
Simon Glass4c214132013-07-16 10:09:28 -06001902 /* Assume worst case: All blocks are not erased. */
1903 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00001904 newcontents = malloc(size);
1905 if (!newcontents) {
1906 msg_gerr("Out of memory!\n");
1907 exit(1);
1908 }
Simon Glass4c214132013-07-16 10:09:28 -06001909 /* Assume best case: All blocks are erased. */
1910 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00001911 /* Side effect of the assumptions above: Default write action is erase
1912 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06001913 * oldcontents being completely unerased means we have to erase
1914 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00001915 */
1916
ollie0eb62d62004-12-08 20:10:01 +00001917 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001918 /* FIXME: Do we really want the scary warning if erase failed?
1919 * After all, after erase the chip is either blank or partially
1920 * blank or it has the old contents. A blank chip won't boot,
1921 * so if the user wanted erase and reboots afterwards, the user
1922 * knows very well that booting won't work.
1923 */
hailfingerb437e282010-11-04 01:04:27 +00001924 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001925 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001926 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001927 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001928 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001929 }
hailfinger771fc182010-10-15 00:01:14 +00001930
hailfingerd217d122010-10-08 18:52:29 +00001931 if (write_it || verify_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001932 if (read_buf_from_file(newcontents, size, filename)) {
hailfinger90fcf9b2010-11-05 14:51:59 +00001933 ret = 1;
1934 goto out;
stepan1da96c02006-11-21 23:48:51 +00001935 }
1936
David Hendricksac82cac2012-06-19 10:29:37 -07001937#if 0
1938 /*
1939 * FIXME: show_id() causes failure if vendor:mainboard do not
1940 * match. This may happen if codenames are in flux.
1941 * See chrome-os-partner:10414.
1942 */
hailfinger90c7d542010-05-31 15:27:27 +00001943#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001944 if (programmer == PROGRAMMER_INTERNAL)
1945 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001946#endif
David Hendricksac82cac2012-06-19 10:29:37 -07001947#endif
ollie5672ac62004-03-17 22:22:08 +00001948 }
1949
David Hendricksc44d7a02011-10-17 11:28:43 -07001950 /* Obtain a reference image so that we can check whether regions need
1951 * to be erased and to give better diagnostics in case write fails.
David Hendricks52ddff02013-07-23 15:05:14 -07001952 * If --fast-verify is used then only the regions which are included
1953 * using -i will be read.
hailfinger4c47e9d2010-10-19 22:06:20 +00001954 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001955 if (diff_file) {
1956 msg_cdbg("Reading old contents from file... ");
1957 if (read_buf_from_file(oldcontents, size, diff_file)) {
1958 ret = 1;
1959 msg_cdbg("FAILED.\n");
1960 goto out;
1961 }
1962 } else {
1963 msg_cdbg("Reading old contents from flash chip... ");
David Hendricks52ddff02013-07-23 15:05:14 -07001964 if (verify_it == VERIFY_FULL) {
1965 if (read_flash(flash, oldcontents, 0, size)) {
1966 ret = 1;
1967 msg_cdbg("FAILED.\n");
1968 goto out;
1969 }
1970 } else if (verify_it == VERIFY_PARTIAL) {
1971 if (handle_partial_read(flash, oldcontents,
1972 read_flash, 0) < 0) {
1973 ret = 1;
1974 msg_cdbg("FAILED.\n");
1975 goto out;
1976 }
David Hendricksc44d7a02011-10-17 11:28:43 -07001977 }
hailfinger4c47e9d2010-10-19 22:06:20 +00001978 }
David Hendricks89a45e52011-11-22 16:56:22 -08001979 msg_cdbg("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00001980
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001981 // This should be moved into each flash part's code to do it
ollie6a600992005-11-26 21:55:36 +00001982 // cleanly. This does the job.
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08001983 if (handle_romentries(flash, oldcontents, newcontents)) {
1984 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07001985 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08001986 goto out;
1987 }
uwef6641642007-05-09 10:17:44 +00001988
stuge8ce3a3c2008-04-28 14:47:30 +00001989 if (write_it) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08001990 // parse the new fmap
David Hendricks5b06c882012-05-20 18:27:25 -07001991 if ((ret = gec_prepare(newcontents, size))) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08001992 msg_cerr("GEC prepare failed, ret=%d.\n", ret);
1993 goto out;
1994 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08001995
hailfingerb437e282010-11-04 01:04:27 +00001996 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
1997 msg_cerr("Uh oh. Erase/write failed. Checking if "
1998 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07001999 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002000 if (!memcmp(oldcontents, newcontents, size)) {
2001 msg_cinfo("Good. It seems nothing was "
2002 "changed.\n");
2003 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002004 ret = 1;
2005 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002006 }
2007 }
hailfingerd217d122010-10-08 18:52:29 +00002008 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002009 ret = 1;
2010 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002011 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002012
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002013 ret = gec_need_2nd_pass();
2014 if (ret < 0) {
2015 // Jump failed
2016 msg_cerr("gec_need_2nd_pass() failed. Stop.\n");
2017 emergency_help_message();
2018 ret = 1;
2019 goto out;
2020 } else if (ret > 0) {
2021 // Need 2nd pass. Get the just written content.
2022 msg_pdbg("GEC needs 2nd pass.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002023 if (read_flash(flash, oldcontents, 0, size)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002024 msg_cerr("Uh oh. Cannot get latest content.\n");
2025 emergency_help_message();
2026 ret = 1;
2027 goto out;
2028 }
2029 // write 2nd pass
2030 if (erase_and_write_flash(flash, oldcontents,
2031 newcontents)) {
2032 msg_cerr("Uh oh. GEC 2nd pass failed.\n");
2033 emergency_help_message();
2034 ret = 1;
2035 goto out;
2036 }
2037 ret = 0;
2038 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002039
2040 if (gec_finish() < 0) {
2041 msg_cerr("gec_finish() failed. Stop.\n");
2042 emergency_help_message();
2043 ret = 1;
2044 goto out;
2045 }
stuge8ce3a3c2008-04-28 14:47:30 +00002046 }
ollie6a600992005-11-26 21:55:36 +00002047
hailfinger0459e1c2009-08-19 13:55:34 +00002048 if (verify_it) {
2049 /* Work around chips which need some time to calm down. */
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002050 if (write_it && verify_it != VERIFY_PARTIAL)
hailfinger0459e1c2009-08-19 13:55:34 +00002051 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002052
2053 ret = verify_flash(flash, newcontents, verify_it);
2054
hailfingera50d60e2009-11-17 09:57:34 +00002055 /* If we tried to write, and verification now fails, we
hailfinger0459e1c2009-08-19 13:55:34 +00002056 * might have an emergency situation.
2057 */
2058 if (ret && write_it)
2059 emergency_help_message();
2060 }
ollie6a600992005-11-26 21:55:36 +00002061
hailfinger90fcf9b2010-11-05 14:51:59 +00002062out:
2063 free(oldcontents);
2064 free(newcontents);
2065out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002066 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002067 /*
2068 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2069 * tree. This is because some operations, such as write protection,
2070 * requires programmer_shutdown() but does not call doit().
2071 */
2072// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002073 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002074}