blob: 0afe188cc17724dce792d4029dcb91d54e9865c5 [file] [log] [blame]
rminnich8d3ff912003-10-25 17:01:29 +00001/*
uweb25f1ea2007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
rminnich8d3ff912003-10-25 17:01:29 +00003 *
uwe555dd972007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
uwe4475e902009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
hailfinger23060112009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
rminnich8d3ff912003-10-25 17:01:29 +00008 *
uweb25f1ea2007-08-29 17:52:32 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
rminnich8d3ff912003-10-25 17:01:29 +000013 *
uweb25f1ea2007-08-29 17:52:32 +000014 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
rminnich8d3ff912003-10-25 17:01:29 +000018 *
uweb25f1ea2007-08-29 17:52:32 +000019 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
rminnich8d3ff912003-10-25 17:01:29 +000022 */
23
hailfingera83a5fe2010-05-30 22:24:40 +000024#include <stdio.h>
stepan1da96c02006-11-21 23:48:51 +000025#include <sys/types.h>
oxygene50275892010-09-30 17:03:32 +000026#ifndef __LIBPAYLOAD__
27#include <fcntl.h>
stepan1da96c02006-11-21 23:48:51 +000028#include <sys/stat.h>
oxygene50275892010-09-30 17:03:32 +000029#endif
rminnich8d3ff912003-10-25 17:01:29 +000030#include <string.h>
31#include <stdlib.h>
hailfingerf76cc322010-11-09 22:00:31 +000032#include <ctype.h>
ollie6a600992005-11-26 21:55:36 +000033#include <getopt.h>
hailfinger3b471632010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
Vincent Palatin7ab23932014-10-01 12:09:16 -070037#include <unistd.h>
rminnich8d3ff912003-10-25 17:01:29 +000038#include "flash.h"
hailfinger66966da2009-06-15 14:14:48 +000039#include "flashchips.h"
Simon Glass9ad06c12013-07-03 22:08:17 +090040#include "layout.h"
hailfinger428f6852010-07-27 22:41:39 +000041#include "programmer.h"
rminnich8d3ff912003-10-25 17:01:29 +000042
krause2eb76212011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
rminnich8d3ff912003-10-25 17:01:29 +000044char *chip_to_probe = NULL;
stuge98c09aa2008-06-18 02:08:40 +000045int verbose = 0;
hailfinger80422e22009-12-13 22:28:00 +000046
David Hendricks54777392015-01-11 18:55:14 -080047unsigned int required_erase_size = 0; /* see comment in flash.h */
48
David Hendricks9ba79fb2015-04-03 12:06:16 -070049/* Set if any erase/write operation is to be done. This will be used to
50 * decide if final verification is needed. */
51static int content_has_changed = 0;
52
David Hendricks1ed1d352011-11-23 17:54:37 -080053/* error handling stuff */
54enum error_action access_denied_action = error_ignore;
55
56int ignore_error(int err) {
57 int rc = 0;
58
59 switch(err) {
60 case ACCESS_DENIED:
61 if (access_denied_action == error_ignore)
62 rc = 1;
63 break;
64 default:
65 break;
66 }
67
68 return rc;
69}
70
hailfinger969e2f32011-09-08 00:00:29 +000071static enum programmer programmer = PROGRAMMER_INVALID;
hailfinger80422e22009-12-13 22:28:00 +000072
hailfingerddeb4ac2010-07-08 10:13:37 +000073static char *programmer_param = NULL;
stepan782fb172007-04-06 11:58:03 +000074
hailfinger1ff33dc2010-07-03 11:02:10 +000075/* Supported buses for the current programmer. */
76enum chipbustype buses_supported;
hailfinger80422e22009-12-13 22:28:00 +000077
uwee15beb92010-08-08 17:01:18 +000078/*
hailfinger80422e22009-12-13 22:28:00 +000079 * Programmers supporting multiple buses can have differing size limits on
80 * each bus. Store the limits for each bus in a common struct.
81 */
hailfinger1ff33dc2010-07-03 11:02:10 +000082struct decode_sizes max_rom_decode;
83
84/* If nonzero, used as the start address of bottom-aligned flash. */
85unsigned long flashbase;
hailfinger80422e22009-12-13 22:28:00 +000086
hailfinger5828baf2010-07-03 12:14:25 +000087/* Is writing allowed with this programmer? */
88int programmer_may_write;
89
hailfingerabe249e2009-05-08 17:43:22 +000090const struct programmer_entry programmer_table[] = {
hailfinger90c7d542010-05-31 15:27:27 +000091#if CONFIG_INTERNAL == 1
hailfingerabe249e2009-05-08 17:43:22 +000092 {
hailfinger3548a9a2009-08-12 14:34:35 +000093 .name = "internal",
hailfinger6c69ab02009-05-11 15:46:43 +000094 .init = internal_init,
hailfinger11ae3c42009-05-11 14:13:25 +000095 .map_flash_region = physmap,
96 .unmap_flash_region = physunmap,
hailfingere5829f62009-06-05 17:48:08 +000097 .delay = internal_delay,
hailfingerabe249e2009-05-08 17:43:22 +000098 },
hailfinger80422e22009-12-13 22:28:00 +000099#endif
stepan927d4e22007-04-04 22:45:58 +0000100
hailfinger90c7d542010-05-31 15:27:27 +0000101#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000102 {
hailfinger3548a9a2009-08-12 14:34:35 +0000103 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000104 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000105 .map_flash_region = dummy_map,
106 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000107 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000108 },
hailfinger571a6b32009-09-16 10:09:21 +0000109#endif
hailfingera9df33c2009-05-09 00:54:55 +0000110
hailfinger90c7d542010-05-31 15:27:27 +0000111#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000112 {
hailfinger3548a9a2009-08-12 14:34:35 +0000113 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000114 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000115 .map_flash_region = fallback_map,
116 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000117 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000118 },
hailfinger571a6b32009-09-16 10:09:21 +0000119#endif
uwe0f5a3a22009-05-13 11:36:06 +0000120
hailfinger90c7d542010-05-31 15:27:27 +0000121#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000122 {
hailfinger0d703d42011-03-07 01:08:09 +0000123 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000124 .name = "nicrealtek",
125 //.name = "nicsmc1211",
126 .init = nicrealtek_init,
127 .map_flash_region = fallback_map,
128 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000129 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000130 },
hailfinger5aa36982010-05-21 21:54:07 +0000131#endif
132
hailfingerf0a368f2010-06-07 22:37:54 +0000133#if CONFIG_NICNATSEMI == 1
134 {
uwe8d342eb2011-07-28 08:13:25 +0000135 .name = "nicnatsemi",
136 .init = nicnatsemi_init,
137 .map_flash_region = fallback_map,
138 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000139 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000140 },
141#endif
hailfinger5aa36982010-05-21 21:54:07 +0000142
hailfinger90c7d542010-05-31 15:27:27 +0000143#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000144 {
145 .name = "gfxnvidia",
146 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000147 .map_flash_region = fallback_map,
148 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000149 .delay = internal_delay,
150 },
151#endif
152
hailfinger90c7d542010-05-31 15:27:27 +0000153#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000154 {
uwee2f95ef2009-09-02 23:00:46 +0000155 .name = "drkaiser",
156 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000157 .map_flash_region = fallback_map,
158 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000159 .delay = internal_delay,
160 },
hailfinger571a6b32009-09-16 10:09:21 +0000161#endif
uwee2f95ef2009-09-02 23:00:46 +0000162
hailfinger90c7d542010-05-31 15:27:27 +0000163#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000164 {
hailfinger3548a9a2009-08-12 14:34:35 +0000165 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000166 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000167 .map_flash_region = fallback_map,
168 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000169 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000170 },
hailfinger571a6b32009-09-16 10:09:21 +0000171#endif
ruikda922a12009-05-17 19:39:27 +0000172
hailfinger90c7d542010-05-31 15:27:27 +0000173#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000174 {
175 .name = "atahpt",
176 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000177 .map_flash_region = fallback_map,
178 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000179 .delay = internal_delay,
180 },
181#endif
182
hailfinger90c7d542010-05-31 15:27:27 +0000183#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000184 {
hailfinger90c7d542010-05-31 15:27:27 +0000185 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000186 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000187 .map_flash_region = fallback_map,
188 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000189 .delay = internal_delay,
190 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000191#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000192
hailfinger90c7d542010-05-31 15:27:27 +0000193#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000194 {
hailfinger3548a9a2009-08-12 14:34:35 +0000195 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000196 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000197 .map_flash_region = fallback_map,
198 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000199 .delay = serprog_delay,
200 },
hailfinger74d88a72009-08-12 16:17:41 +0000201#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000202
hailfinger90c7d542010-05-31 15:27:27 +0000203#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000204 {
hailfinger90c7d542010-05-31 15:27:27 +0000205 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000206 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000207 .map_flash_region = fallback_map,
208 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000209 .delay = internal_delay,
210 },
211#endif
212
Anton Staafb2647882014-09-17 15:13:43 -0700213#if CONFIG_RAIDEN_DEBUG_SPI == 1
214 {
215 .name = "raiden_debug_spi",
216 .init = raiden_debug_spi_init,
217 .map_flash_region = fallback_map,
218 .unmap_flash_region = fallback_unmap,
219 .delay = internal_delay,
220 },
221#endif
222
hailfinger90c7d542010-05-31 15:27:27 +0000223#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000224 {
225 .name = "dediprog",
226 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000227 .map_flash_region = fallback_map,
228 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000229 .delay = internal_delay,
230 },
231#endif
232
hailfinger52c4fa02010-07-21 10:26:01 +0000233#if CONFIG_RAYER_SPI == 1
234 {
235 .name = "rayer_spi",
236 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000237 .map_flash_region = fallback_map,
238 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000239 .delay = internal_delay,
240 },
241#endif
242
hailfinger7949b652011-05-08 00:24:18 +0000243#if CONFIG_NICINTEL == 1
244 {
245 .name = "nicintel",
246 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000247 .map_flash_region = fallback_map,
248 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000249 .delay = internal_delay,
250 },
251#endif
252
uwe6764e922010-09-03 18:21:21 +0000253#if CONFIG_NICINTEL_SPI == 1
254 {
uwe8d342eb2011-07-28 08:13:25 +0000255 .name = "nicintel_spi",
256 .init = nicintel_spi_init,
257 .map_flash_region = fallback_map,
258 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000259 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000260 },
261#endif
262
hailfingerfb1f31f2010-12-03 14:48:11 +0000263#if CONFIG_OGP_SPI == 1
264 {
uwe8d342eb2011-07-28 08:13:25 +0000265 .name = "ogp_spi",
266 .init = ogp_spi_init,
267 .map_flash_region = fallback_map,
268 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000269 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000270 },
271#endif
272
hailfinger935365d2011-02-04 21:37:59 +0000273#if CONFIG_SATAMV == 1
274 {
275 .name = "satamv",
276 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000277 .map_flash_region = fallback_map,
278 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000279 .delay = internal_delay,
280 },
281#endif
282
uwe7df6dda2011-09-03 18:37:52 +0000283#if CONFIG_LINUX_SPI == 1
284 {
285 .name = "linux_spi",
286 .init = linux_spi_init,
287 .map_flash_region = fallback_map,
288 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000289 .delay = internal_delay,
290 },
291#endif
292
hailfinger3548a9a2009-08-12 14:34:35 +0000293 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000294};
stepan927d4e22007-04-04 22:45:58 +0000295
David Hendricksbf36f092010-11-02 23:39:29 -0700296#define CHIP_RESTORE_MAXFN 4
297static int chip_restore_fn_count = 0;
298struct chip_restore_func_data {
299 CHIP_RESTORE_CALLBACK;
300 struct flashchip *flash;
301 uint8_t status;
302} static chip_restore_fn[CHIP_RESTORE_MAXFN];
303
David Hendricks668f29d2011-01-27 18:51:45 -0800304
hailfingerf31cbdc2010-11-10 15:25:18 +0000305#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000306static int shutdown_fn_count = 0;
307struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000308 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000309 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000310} static shutdown_fn[SHUTDOWN_MAXFN];
311/* Initialize to 0 to make sure nobody registers a shutdown function before
312 * programmer init.
313 */
314static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000315
stefanct569dbb62011-07-01 00:19:12 +0000316static int check_block_eraser(const struct flashchip *flash, int k, int log);
317
hailfingerdc6f7972010-02-14 01:20:28 +0000318/* Register a function to be executed on programmer shutdown.
319 * The advantage over atexit() is that you can supply a void pointer which will
320 * be used as parameter to the registered function upon programmer shutdown.
321 * This pointer can point to arbitrary data used by said function, e.g. undo
322 * information for GPIO settings etc. If unneeded, set data=NULL.
323 * Please note that the first (void *data) belongs to the function signature of
324 * the function passed as first parameter.
325 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000326int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000327{
328 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000329 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000330 SHUTDOWN_MAXFN);
331 return 1;
332 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000333 if (!may_register_shutdown) {
334 msg_perr("Tried to register a shutdown function before "
335 "programmer init.\n");
336 return 1;
337 }
hailfingerdc6f7972010-02-14 01:20:28 +0000338 shutdown_fn[shutdown_fn_count].func = function;
339 shutdown_fn[shutdown_fn_count].data = data;
340 shutdown_fn_count++;
341
342 return 0;
343}
344
David Hendricksbf36f092010-11-02 23:39:29 -0700345//int register_chip_restore(int (*function) (void *data), void *data)
346int register_chip_restore(CHIP_RESTORE_CALLBACK,
347 struct flashchip *flash, uint8_t status)
348{
349 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
350 msg_perr("Tried to register more than %i chip restore"
351 " functions.\n", CHIP_RESTORE_MAXFN);
352 return 1;
353 }
354 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
355 chip_restore_fn[chip_restore_fn_count].flash = flash;
356 chip_restore_fn[chip_restore_fn_count].status = status;
357 chip_restore_fn_count++;
358
359 return 0;
360}
361
hailfinger969e2f32011-09-08 00:00:29 +0000362int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000363{
hailfinger1ef766d2010-07-06 09:55:48 +0000364 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000365
366 if (prog >= PROGRAMMER_INVALID) {
367 msg_perr("Invalid programmer specified!\n");
368 return -1;
369 }
370 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000371 /* Initialize all programmer specific data. */
372 /* Default to unlimited decode sizes. */
373 max_rom_decode = (const struct decode_sizes) {
374 .parallel = 0xffffffff,
375 .lpc = 0xffffffff,
376 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000377 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000378 };
hailfingere1e41ea2011-07-27 07:13:06 +0000379 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000380 /* Default to top aligned flash at 4 GB. */
381 flashbase = 0;
382 /* Registering shutdown functions is now allowed. */
383 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000384 /* Default to allowing writes. Broken programmers set this to 0. */
385 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000386
387 programmer_param = param;
388 msg_pdbg("Initializing %s programmer\n",
389 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000390 ret = programmer_table[programmer].init();
391 if (programmer_param && strlen(programmer_param)) {
392 msg_perr("Unhandled programmer parameters: %s\n",
393 programmer_param);
394 /* Do not error out here, the init itself was successful. */
395 }
396 return ret;
uweabe92a52009-05-16 22:36:00 +0000397}
398
David Hendricksbf36f092010-11-02 23:39:29 -0700399int chip_restore()
400{
401 int rc = 0;
402
403 while (chip_restore_fn_count > 0) {
404 int i = --chip_restore_fn_count;
405 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
406 chip_restore_fn[i].status);
407 }
408
409 return rc;
410}
411
uweabe92a52009-05-16 22:36:00 +0000412int programmer_shutdown(void)
413{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000414 int ret = 0;
415
hailfinger1ff33dc2010-07-03 11:02:10 +0000416 /* Registering shutdown functions is no longer allowed. */
417 may_register_shutdown = 0;
418 while (shutdown_fn_count > 0) {
419 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000420 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000421 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000422 return ret;
uweabe92a52009-05-16 22:36:00 +0000423}
424
425void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
426 size_t len)
427{
428 return programmer_table[programmer].map_flash_region(descr,
429 phys_addr, len);
430}
431
432void programmer_unmap_flash_region(void *virt_addr, size_t len)
433{
434 programmer_table[programmer].unmap_flash_region(virt_addr, len);
435}
436
437void chip_writeb(uint8_t val, chipaddr addr)
438{
hailfinger76bb7e92011-11-09 23:40:00 +0000439 par_programmer->chip_writeb(val, addr);
uweabe92a52009-05-16 22:36:00 +0000440}
441
442void chip_writew(uint16_t val, chipaddr addr)
443{
hailfinger76bb7e92011-11-09 23:40:00 +0000444 par_programmer->chip_writew(val, addr);
uweabe92a52009-05-16 22:36:00 +0000445}
446
447void chip_writel(uint32_t val, chipaddr addr)
448{
hailfinger76bb7e92011-11-09 23:40:00 +0000449 par_programmer->chip_writel(val, addr);
uweabe92a52009-05-16 22:36:00 +0000450}
451
hailfinger9d987ef2009-06-05 18:32:07 +0000452void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
453{
hailfinger76bb7e92011-11-09 23:40:00 +0000454 par_programmer->chip_writen(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000455}
456
uweabe92a52009-05-16 22:36:00 +0000457uint8_t chip_readb(const chipaddr addr)
458{
hailfinger76bb7e92011-11-09 23:40:00 +0000459 return par_programmer->chip_readb(addr);
uweabe92a52009-05-16 22:36:00 +0000460}
461
462uint16_t chip_readw(const chipaddr addr)
463{
hailfinger76bb7e92011-11-09 23:40:00 +0000464 return par_programmer->chip_readw(addr);
uweabe92a52009-05-16 22:36:00 +0000465}
466
467uint32_t chip_readl(const chipaddr addr)
468{
hailfinger76bb7e92011-11-09 23:40:00 +0000469 return par_programmer->chip_readl(addr);
uweabe92a52009-05-16 22:36:00 +0000470}
471
hailfinger9d987ef2009-06-05 18:32:07 +0000472void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
473{
hailfinger76bb7e92011-11-09 23:40:00 +0000474 par_programmer->chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000475}
476
hailfingere5829f62009-06-05 17:48:08 +0000477void programmer_delay(int usecs)
478{
479 programmer_table[programmer].delay(usecs);
480}
481
stuge5ff0e6c2009-01-26 00:39:57 +0000482void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000483{
stepan15e64bc2007-05-24 08:48:10 +0000484 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000485 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000486 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000487 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000488}
489
stefanctc5eb8a92011-11-23 09:13:48 +0000490int read_memmapped(struct flashchip *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000491{
hailfinger0f08b7a2009-06-16 08:55:44 +0000492 chip_readn(buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000493
hailfinger23060112009-05-08 12:49:03 +0000494 return 0;
495}
496
hailfinger7b414742009-06-13 12:04:03 +0000497int min(int a, int b)
498{
499 return (a < b) ? a : b;
500}
501
hailfinger7af83692009-06-15 17:23:36 +0000502int max(int a, int b)
503{
504 return (a > b) ? a : b;
505}
506
hailfingeraec9c962009-10-31 01:53:09 +0000507int bitcount(unsigned long a)
508{
509 int i = 0;
510 for (; a != 0; a >>= 1)
511 if (a & 1)
512 i++;
513 return i;
514}
515
hailfingerf76cc322010-11-09 22:00:31 +0000516void tolower_string(char *str)
517{
518 for (; *str != '\0'; str++)
519 *str = (char)tolower((unsigned char)*str);
520}
521
hailfingera916b422009-06-01 02:08:58 +0000522char *strcat_realloc(char *dest, const char *src)
523{
524 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000525 if (!dest) {
526 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000527 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000528 }
hailfingera916b422009-06-01 02:08:58 +0000529 strcat(dest, src);
530 return dest;
531}
532
hailfinger6e5a52a2009-11-24 18:27:10 +0000533/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000534 * It will look for needle with a subsequent '=' in haystack, return a copy of
535 * needle and remove everything from the first occurrence of needle to the next
536 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000537 */
stefanct52700282011-06-26 17:38:17 +0000538char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000539{
hailfinger1ef766d2010-07-06 09:55:48 +0000540 char *param_pos, *opt_pos, *rest;
541 char *opt = NULL;
542 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000543 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000544
hailfingerf4aaccc2010-04-28 15:22:14 +0000545 needlelen = strlen(needle);
546 if (!needlelen) {
547 msg_gerr("%s: empty needle! Please report a bug at "
548 "flashrom@flashrom.org\n", __func__);
549 return NULL;
550 }
551 /* No programmer parameters given. */
552 if (*haystack == NULL)
553 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000554 param_pos = strstr(*haystack, needle);
555 do {
556 if (!param_pos)
557 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000558 /* Needle followed by '='? */
559 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600560
hailfinger1ef766d2010-07-06 09:55:48 +0000561 /* Beginning of the string? */
562 if (param_pos == *haystack)
563 break;
564 /* After a delimiter? */
565 if (strchr(delim, *(param_pos - 1)))
566 break;
567 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000568 /* Continue searching. */
569 param_pos++;
570 param_pos = strstr(param_pos, needle);
571 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000572
hailfinger6e5a52a2009-11-24 18:27:10 +0000573 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000574 /* Get the string after needle and '='. */
575 opt_pos = param_pos + needlelen + 1;
576 optlen = strcspn(opt_pos, delim);
577 /* Return an empty string if the parameter was empty. */
578 opt = malloc(optlen + 1);
579 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000580 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000581 exit(1);
582 }
hailfinger1ef766d2010-07-06 09:55:48 +0000583 strncpy(opt, opt_pos, optlen);
584 opt[optlen] = '\0';
585 rest = opt_pos + optlen;
586 /* Skip all delimiters after the current parameter. */
587 rest += strspn(rest, delim);
588 memmove(param_pos, rest, strlen(rest) + 1);
589 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000590 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000591
hailfinger1ef766d2010-07-06 09:55:48 +0000592 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000593}
594
stefanct52700282011-06-26 17:38:17 +0000595char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000596{
597 return extract_param(&programmer_param, param_name, ",");
598}
599
stefancte1c5acf2011-07-04 07:27:17 +0000600/* Returns the number of well-defined erasers for a chip. */
601static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000602{
603 unsigned int usable_erasefunctions = 0;
604 int k;
605 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
606 if (!check_block_eraser(flash, k, 0))
607 usable_erasefunctions++;
608 }
609 return usable_erasefunctions;
610}
611
hailfinger7af83692009-06-15 17:23:36 +0000612/* start is an offset to the base address of the flash chip */
stefanctc5eb8a92011-11-23 09:13:48 +0000613int check_erased_range(struct flashchip *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000614{
615 int ret;
616 uint8_t *cmpbuf = malloc(len);
617
618 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000619 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000620 exit(1);
621 }
Simon Glass4c214132013-07-16 10:09:28 -0600622 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000623 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
624 free(cmpbuf);
625 return ret;
626}
627
uwee15beb92010-08-08 17:01:18 +0000628/*
hailfinger7af3d192009-11-25 17:05:52 +0000629 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000630 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000631 * @start offset to the base address of the flash chip
632 * @len length of the verified area
633 * @message string to print in the "FAILED" message
634 * @return 0 for success, -1 for failure
635 */
stefanctc5eb8a92011-11-23 09:13:48 +0000636int verify_range(struct flashchip *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000637 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000638{
stefanctc5eb8a92011-11-23 09:13:48 +0000639 unsigned int i;
hailfinger8cb6ece2010-11-16 17:21:58 +0000640 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000641 int ret = 0, failcount = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -0800642 unsigned int chunksize;
hailfinger7af83692009-06-15 17:23:36 +0000643
644 if (!len)
645 goto out_free;
646
hailfingerb0f4d122009-06-24 08:20:45 +0000647 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000648 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000649 return 1;
650 }
hailfinger7af83692009-06-15 17:23:36 +0000651 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000652 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000653 exit(1);
654 }
655
656 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000657 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000658 " total_size 0x%x\n", __func__, start, len,
659 flash->total_size * 1024);
660 ret = -1;
661 goto out_free;
662 }
663 if (!message)
664 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000665
David Hendricks758e2fb2014-08-19 20:34:43 -0700666 for (i = 0, chunksize = 0; i < len; i += chunksize) {
David Hendricks1ed1d352011-11-23 17:54:37 -0800667 int tmp, j;
668
David Hendricks758e2fb2014-08-19 20:34:43 -0700669 chunksize = min(flash->page_size, len - i);
David Hendricks1ed1d352011-11-23 17:54:37 -0800670 tmp = flash->read(flash, readbuf + i, start + i, chunksize);
671
David Hendrickse3451942013-03-21 17:23:29 -0700672 /* Since this function explicitly compares the bytes, we need
673 to handle errors manually */
David Hendricks1ed1d352011-11-23 17:54:37 -0800674 if (tmp) {
675 ret = tmp;
David Hendricks758e2fb2014-08-19 20:34:43 -0700676 if (ignore_error(tmp))
David Hendricks1ed1d352011-11-23 17:54:37 -0800677 continue;
David Hendricks758e2fb2014-08-19 20:34:43 -0700678 else
David Hendricks1ed1d352011-11-23 17:54:37 -0800679 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800680 }
681
682 for (j = 0; j < chunksize; j++) {
683 if (cmpbuf[i + j] != readbuf[i + j]) {
684 /* Only print the first failure. */
685 if (!failcount++)
686 msg_cerr("%s FAILED at 0x%08x! "
687 "Expected=0x%02x, Read=0x%02x,",
688 message, start + i + j, cmpbuf[i + j],
689 readbuf[j]);
690 }
691 }
hailfinger8cb6ece2010-11-16 17:21:58 +0000692 }
693
hailfinger5be6c0f2009-07-23 01:42:56 +0000694 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000695 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000696 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000697 ret = -1;
698 }
hailfinger7af83692009-06-15 17:23:36 +0000699
700out_free:
701 free(readbuf);
702 return ret;
703}
704
uwee15beb92010-08-08 17:01:18 +0000705/*
hailfingerb247c7a2010-03-08 00:42:32 +0000706 * Check if the buffer @have can be programmed to the content of @want without
707 * erasing. This is only possible if all chunks of size @gran are either kept
708 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000709 *
hailfingerb247c7a2010-03-08 00:42:32 +0000710 * The following write granularities (enum @gran) are known:
711 * - 1 bit. Each bit can be cleared individually.
712 * - 1 byte. A byte can be written once. Further writes to an already written
713 * byte cause the contents to be either undefined or to stay unchanged.
714 * - 128 bytes. If less than 128 bytes are written, the rest will be
715 * erased. Each write to a 128-byte region will trigger an automatic erase
716 * before anything is written. Very uncommon behaviour and unsupported by
717 * this function.
718 * - 256 bytes. If less than 256 bytes are written, the contents of the
719 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000720 * Warning: This function assumes that @have and @want point to naturally
721 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000722 *
723 * @have buffer with current content
724 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000725 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000726 * @gran write granularity (enum, not count)
727 * @return 0 if no erase is needed, 1 otherwise
728 */
Simon Glass4c214132013-07-16 10:09:28 -0600729static int need_erase(struct flashchip *flash, uint8_t *have, uint8_t *want,
730 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000731{
hailfingerb91c08c2011-08-15 19:54:20 +0000732 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000733 unsigned int i, j, limit;
Simon Glass4c214132013-07-16 10:09:28 -0600734 int erase_value = flash_erase_value(flash);
hailfingerb247c7a2010-03-08 00:42:32 +0000735
736 switch (gran) {
737 case write_gran_1bit:
738 for (i = 0; i < len; i++)
739 if ((have[i] & want[i]) != want[i]) {
740 result = 1;
741 break;
742 }
743 break;
744 case write_gran_1byte:
745 for (i = 0; i < len; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600746 if ((have[i] != want[i]) && (have[i] != erase_value)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000747 result = 1;
748 break;
749 }
750 break;
751 case write_gran_256bytes:
752 for (j = 0; j < len / 256; j++) {
753 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000754 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000755 if (!memcmp(have + j * 256, want + j * 256, limit))
756 continue;
757 /* have needs to be in erased state. */
758 for (i = 0; i < limit; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600759 if (have[j * 256 + i] != erase_value) {
hailfingerb247c7a2010-03-08 00:42:32 +0000760 result = 1;
761 break;
762 }
763 if (result)
764 break;
765 }
766 break;
hailfingerb437e282010-11-04 01:04:27 +0000767 default:
768 msg_cerr("%s: Unsupported granularity! Please report a bug at "
769 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000770 }
771 return result;
772}
773
hailfingerb437e282010-11-04 01:04:27 +0000774/**
775 * Check if the buffer @have needs to be programmed to get the content of @want.
776 * If yes, return 1 and fill in first_start with the start address of the
777 * write operation and first_len with the length of the first to-be-written
778 * chunk. If not, return 0 and leave first_start and first_len undefined.
779 *
780 * Warning: This function assumes that @have and @want point to naturally
781 * aligned regions.
782 *
783 * @have buffer with current content
784 * @want buffer with desired content
785 * @len length of the checked area
786 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000787 * @first_start offset of the first byte which needs to be written (passed in
788 * value is increased by the offset of the first needed write
789 * relative to have/want or unchanged if no write is needed)
790 * @return length of the first contiguous area which needs to be written
791 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000792 *
793 * FIXME: This function needs a parameter which tells it about coalescing
794 * in relation to the max write length of the programmer and the max write
795 * length of the chip.
796 */
stefanctc5eb8a92011-11-23 09:13:48 +0000797static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
798 unsigned int *first_start,
799 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000800{
stefanctc5eb8a92011-11-23 09:13:48 +0000801 int need_write = 0;
802 unsigned int rel_start = 0, first_len = 0;
803 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000804
hailfingerb437e282010-11-04 01:04:27 +0000805 switch (gran) {
806 case write_gran_1bit:
807 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000808 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000809 break;
810 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000811 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000812 break;
813 default:
814 msg_cerr("%s: Unsupported granularity! Please report a bug at "
815 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000816 /* Claim that no write was needed. A write with unknown
817 * granularity is too dangerous to try.
818 */
819 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000820 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000821 for (i = 0; i < len / stride; i++) {
822 limit = min(stride, len - i * stride);
823 /* Are 'have' and 'want' identical? */
824 if (memcmp(have + i * stride, want + i * stride, limit)) {
825 if (!need_write) {
826 /* First location where have and want differ. */
827 need_write = 1;
828 rel_start = i * stride;
829 }
830 } else {
831 if (need_write) {
832 /* First location where have and want
833 * do not differ anymore.
834 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000835 break;
836 }
837 }
838 }
hailfingerffb7f382010-12-06 13:05:44 +0000839 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000840 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000841 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000842 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000843}
844
hailfinger0c515352009-11-23 12:55:31 +0000845/* This function generates various test patterns useful for testing controller
846 * and chip communication as well as chip behaviour.
847 *
848 * If a byte can be written multiple times, each time keeping 0-bits at 0
849 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
850 * is essentially an AND operation. That's also the reason why this function
851 * provides the result of AND between various patterns.
852 *
853 * Below is a list of patterns (and their block length).
854 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
855 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
856 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
857 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
858 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
859 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
860 * Pattern 6 is 00 (1 Byte)
861 * Pattern 7 is ff (1 Byte)
862 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
863 * byte block.
864 *
865 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
866 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
867 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
868 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
869 * Pattern 12 is 00 (1 Byte)
870 * Pattern 13 is ff (1 Byte)
871 * Patterns 8-13 have no block number.
872 *
873 * Patterns 0-3 are created to detect and efficiently diagnose communication
874 * slips like missed bits or bytes and their repetitive nature gives good visual
875 * cues to the person inspecting the results. In addition, the following holds:
876 * AND Pattern 0/1 == Pattern 4
877 * AND Pattern 2/3 == Pattern 5
878 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
879 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
880 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
881 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
882 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
883 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
884 * Besides that, they provide for bit testing of the last two bytes of every
885 * 256 byte block which contains the block number for patterns 0-6.
886 * Patterns 10-11 are special purpose for detecting subblock aliasing with
887 * block sizes >256 bytes (some Dataflash chips etc.)
888 * AND Pattern 8/9 == Pattern 12
889 * AND Pattern 10/11 == Pattern 12
890 * Pattern 13 is the completely erased state.
891 * None of the patterns can detect aliasing at boundaries which are a multiple
892 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
893 */
894int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
895{
896 int i;
897
898 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000899 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000900 return 1;
901 }
902
903 switch (variant) {
904 case 0:
905 for (i = 0; i < size; i++)
906 buf[i] = (i & 0xf) << 4 | 0x5;
907 break;
908 case 1:
909 for (i = 0; i < size; i++)
910 buf[i] = (i & 0xf) << 4 | 0xa;
911 break;
912 case 2:
913 for (i = 0; i < size; i++)
914 buf[i] = 0x50 | (i & 0xf);
915 break;
916 case 3:
917 for (i = 0; i < size; i++)
918 buf[i] = 0xa0 | (i & 0xf);
919 break;
920 case 4:
921 for (i = 0; i < size; i++)
922 buf[i] = (i & 0xf) << 4;
923 break;
924 case 5:
925 for (i = 0; i < size; i++)
926 buf[i] = i & 0xf;
927 break;
928 case 6:
929 memset(buf, 0x00, size);
930 break;
931 case 7:
932 memset(buf, 0xff, size);
933 break;
934 case 8:
935 for (i = 0; i < size; i++)
936 buf[i] = i & 0xff;
937 break;
938 case 9:
939 for (i = 0; i < size; i++)
940 buf[i] = ~(i & 0xff);
941 break;
942 case 10:
943 for (i = 0; i < size % 2; i++) {
944 buf[i * 2] = (i >> 8) & 0xff;
945 buf[i * 2 + 1] = i & 0xff;
946 }
947 if (size & 0x1)
948 buf[i * 2] = (i >> 8) & 0xff;
949 break;
950 case 11:
951 for (i = 0; i < size % 2; i++) {
952 buf[i * 2] = ~((i >> 8) & 0xff);
953 buf[i * 2 + 1] = ~(i & 0xff);
954 }
955 if (size & 0x1)
956 buf[i * 2] = ~((i >> 8) & 0xff);
957 break;
958 case 12:
959 memset(buf, 0x00, size);
960 break;
961 case 13:
962 memset(buf, 0xff, size);
963 break;
964 }
965
966 if ((variant >= 0) && (variant <= 7)) {
967 /* Write block number in the last two bytes of each 256-byte
968 * block, big endian for easier reading of the hexdump.
969 * Note that this wraps around for chips larger than 2^24 bytes
970 * (16 MB).
971 */
972 for (i = 0; i < size / 256; i++) {
973 buf[i * 256 + 254] = (i >> 8) & 0xff;
974 buf[i * 256 + 255] = i & 0xff;
975 }
976 }
977
978 return 0;
979}
980
hailfingeraec9c962009-10-31 01:53:09 +0000981int check_max_decode(enum chipbustype buses, uint32_t size)
982{
983 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +0000984
985 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000986 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000987 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000988 "size %u kB of chipset/board/programmer "
989 "for %s interface, "
990 "probe/read/erase/write may fail. ", size / 1024,
991 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +0000992 }
hailfingere1e41ea2011-07-27 07:13:06 +0000993 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +0000994 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +0000995 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +0000996 "size %u kB of chipset/board/programmer "
997 "for %s interface, "
998 "probe/read/erase/write may fail. ", size / 1024,
999 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001000 }
hailfingere1e41ea2011-07-27 07:13:06 +00001001 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001002 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001003 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001004 "size %u kB of chipset/board/programmer "
1005 "for %s interface, "
1006 "probe/read/erase/write may fail. ", size / 1024,
1007 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001008 }
hailfingere1e41ea2011-07-27 07:13:06 +00001009 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001010 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001011 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001012 "size %u kB of chipset/board/programmer "
1013 "for %s interface, "
1014 "probe/read/erase/write may fail. ", size / 1024,
1015 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001016 }
1017 if (!limitexceeded)
1018 return 0;
1019 /* Sometimes chip and programmer have more than one bus in common,
1020 * and the limit is not exceeded on all buses. Tell the user.
1021 */
1022 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001023 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001024 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001025 "interface which can support a chip of this size. "
1026 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001027 return 1;
1028}
1029
hailfinger48ed3e22011-05-04 00:39:50 +00001030int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001031{
Duncan Laurie89ee23d2015-05-08 20:27:50 +00001032 const struct flashchip *flash;
hailfingeraec9c962009-10-31 01:53:09 +00001033 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001034 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001035 uint32_t size;
1036 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001037 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001038
Duncan Laurie89ee23d2015-05-08 20:27:50 +00001039 for (flash = flashchips + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001040 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001041 continue;
hailfingeraec9c962009-10-31 01:53:09 +00001042 buses_common = buses_supported & flash->bustype;
1043 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001044 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1045 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +00001046 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +00001047 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001048 free(tmp);
1049 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001050 msg_gspew("and chip bus type %s are incompatible.",
1051 tmp);
hailfingera916b422009-06-01 02:08:58 +00001052 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001053 msg_gspew("\n");
1054 continue;
1055 }
1056 msg_gdbg("Probing for %s %s, %d kB: ",
1057 flash->vendor, flash->name, flash->total_size);
1058 if (!flash->probe && !force) {
1059 msg_gdbg("failed! flashrom has no probe function for "
1060 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001061 continue;
1062 }
stepan782fb172007-04-06 11:58:03 +00001063
ollie5672ac62004-03-17 22:22:08 +00001064 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001065 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001066
hailfinger48ed3e22011-05-04 00:39:50 +00001067 /* Start filling in the dynamic data. */
1068 *fill_flash = *flash;
1069
hailfinger72d3b982009-05-09 07:27:23 +00001070 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001071 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001072
stugec1e55fe2008-07-02 17:15:47 +00001073 if (force)
1074 break;
stepanc98b80b2006-03-16 16:57:41 +00001075
hailfinger48ed3e22011-05-04 00:39:50 +00001076 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001077 goto notfound;
1078
hailfinger48ed3e22011-05-04 00:39:50 +00001079 /* If this is the first chip found, accept it.
1080 * If this is not the first chip found, accept it only if it is
1081 * a non-generic match.
1082 * We could either make chipcount global or provide it as
1083 * parameter, or we assume that startchip==0 means this call to
1084 * probe_flash() is the first one and thus no chip has been
1085 * found before.
1086 */
1087 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001088 break;
1089
stuge56300c32008-09-03 23:10:05 +00001090notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001091 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001092 }
uwebe4477b2007-08-23 16:08:21 +00001093
stugec1e55fe2008-07-02 17:15:47 +00001094 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001095 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001096
hailfingere11396b2011-03-08 00:09:11 +00001097#if CONFIG_INTERNAL == 1
1098 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001099 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001100 else
1101#endif
stepan3e7aeae2011-01-19 06:21:54 +00001102 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001103
stefanct588b6d22011-06-26 20:45:35 +00001104 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001105 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1106 force ? "Assuming" : "Found", fill_flash->vendor,
1107 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001108 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001109
hailfinger0f4c3952010-12-02 21:59:42 +00001110 /* Flash registers will not be mapped if the chip was forced. Lock info
1111 * may be stored in registers, so avoid lock info printing.
1112 */
1113 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001114 if (fill_flash->printlock)
1115 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001116
hailfinger48ed3e22011-05-04 00:39:50 +00001117 /* Return position of matching chip. */
Duncan Laurie89ee23d2015-05-08 20:27:50 +00001118 return flash - flashchips;
rminnich8d3ff912003-10-25 17:01:29 +00001119}
1120
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001121int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001122{
hailfingerb0f4d122009-06-24 08:20:45 +00001123 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001124 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001125
snelsone42c3802010-05-07 20:09:04 +00001126 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001127
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001128 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1129 ret = handle_partial_verify(flash, buf, verify_range);
1130 } else {
1131 ret = verify_range(flash, buf, 0, total_size, NULL);
1132 }
uwef6641642007-05-09 10:17:44 +00001133
David Hendricks1ed1d352011-11-23 17:54:37 -08001134 if (ret == ACCESS_DENIED) {
1135 msg_gdbg("Could not fully verify due to access error, ");
1136 if (access_denied_action == error_ignore) {
1137 msg_gdbg("ignoring\n");
1138 ret = 0;
1139 } else {
1140 msg_gdbg("aborting\n");
1141 }
1142 }
1143
hailfingerb0f4d122009-06-24 08:20:45 +00001144 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001145 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001146
hailfingerb0f4d122009-06-24 08:20:45 +00001147 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001148}
1149
uwe8d342eb2011-07-28 08:13:25 +00001150int read_buf_from_file(unsigned char *buf, unsigned long size,
1151 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001152{
1153 unsigned long numbytes;
1154 FILE *image;
1155 struct stat image_stat;
1156
Vincent Palatin7ab23932014-10-01 12:09:16 -07001157 if (!strncmp(filename, "-", sizeof("-")))
1158 image = fdopen(STDIN_FILENO, "rb");
1159 else
1160 image = fopen(filename, "rb");
1161 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001162 perror(filename);
1163 return 1;
1164 }
1165 if (fstat(fileno(image), &image_stat) != 0) {
1166 perror(filename);
1167 fclose(image);
1168 return 1;
1169 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001170 if ((image_stat.st_size != size) &&
1171 (strncmp(filename, "-", sizeof("-")))) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001172 msg_gerr("Error: Image size doesn't match: stat %ld bytes, "
1173 "wanted %ld!\n", image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001174 fclose(image);
1175 return 1;
1176 }
1177 numbytes = fread(buf, 1, size, image);
1178 if (fclose(image)) {
1179 perror(filename);
1180 return 1;
1181 }
1182 if (numbytes != size) {
1183 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1184 "wanted %ld!\n", numbytes, size);
1185 return 1;
1186 }
1187 return 0;
1188}
1189
uwe8d342eb2011-07-28 08:13:25 +00001190int write_buf_to_file(unsigned char *buf, unsigned long size,
1191 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001192{
1193 unsigned long numbytes;
1194 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001195
1196 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001197 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001198 return 1;
1199 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001200 if (!strncmp(filename, "-", sizeof("-")))
1201 image = fdopen(STDOUT_FILENO, "wb");
1202 else
1203 image = fopen(filename, "wb");
1204 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001205 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001206 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001207 }
hailfingerd219a232009-01-28 00:27:54 +00001208
hailfingerd219a232009-01-28 00:27:54 +00001209 numbytes = fwrite(buf, 1, size, image);
1210 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001211 if (numbytes != size) {
1212 msg_gerr("File %s could not be written completely.\n",
1213 filename);
hailfingerd219a232009-01-28 00:27:54 +00001214 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001215 }
hailfingerd219a232009-01-28 00:27:54 +00001216 return 0;
1217}
1218
David Hendrickse3451942013-03-21 17:23:29 -07001219/*
1220 * read_flash - wrapper for flash->read() with additional high-level policy
1221 *
1222 * @flash flash chip
1223 * @buf buffer to store data in
1224 * @start start address
1225 * @len number of bytes to read
1226 *
1227 * This wrapper simplifies most cases when the flash chip needs to be read
1228 * since policy decisions such as non-fatal error handling is centralized.
1229 */
1230int read_flash(struct flashchip *flash, uint8_t *buf,
1231 unsigned int start, unsigned int len)
1232{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001233 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001234
1235 if (!flash || !flash->read)
1236 return -1;
1237
David Hendricks4e76fdc2013-05-13 16:05:36 -07001238 ret = flash->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001239 if (ret) {
1240 if (ignore_error(ret)) {
1241 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1242 start, start + len - 1);
1243 ret = 0;
1244 } else {
1245 msg_gdbg("failed to read 0x%x-0x%x\n",
1246 start, start + len - 1);
1247 }
1248 }
1249
1250 return ret;
1251}
1252
David Hendricks7c8a1612013-04-26 19:14:44 -07001253/*
1254 * write_flash - wrapper for flash->write() with additional high-level policy
1255 *
1256 * @flash flash chip
1257 * @buf buffer to write to flash
1258 * @start start address in flash
1259 * @len number of bytes to write
1260 *
1261 * TODO: Look up regions that are write-protected and avoid attempt to write
1262 * to them at all.
1263 */
1264int write_flash(struct flashchip *flash, uint8_t *buf,
1265 unsigned int start, unsigned int len)
1266{
1267 if (!flash || !flash->write)
1268 return -1;
1269
1270 return flash->write(flash, buf, start, len);
1271}
1272
stefanct52700282011-06-26 17:38:17 +00001273int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001274{
1275 unsigned long size = flash->total_size * 1024;
1276 unsigned char *buf = calloc(size, sizeof(char));
1277 int ret = 0;
1278
1279 msg_cinfo("Reading flash... ");
1280 if (!buf) {
1281 msg_gerr("Memory allocation failed!\n");
1282 msg_cinfo("FAILED.\n");
1283 return 1;
1284 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001285
1286 /* To support partial read, fill buffer to all 0xFF at beginning to make
1287 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001288 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001289
hailfinger42a850a2010-07-13 23:56:13 +00001290 if (!flash->read) {
1291 msg_cerr("No read function available for this flash chip.\n");
1292 ret = 1;
1293 goto out_free;
1294 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001295
1296 /* First try to handle partial read case, rather than read the whole
1297 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001298 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001299 if (ret < 0) {
1300 msg_cerr("Partial read operation failed!\n");
1301 ret = 1;
1302 goto out_free;
1303 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001304 int num_regions = get_num_include_args();
1305
1306 if (ret != num_regions) {
1307 msg_cerr("Requested %d regions, but only read %d\n",
1308 num_regions, ret);
1309 ret = 1;
1310 goto out_free;
1311 }
1312
1313 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001314 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001315 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001316 msg_cerr("Read operation failed!\n");
1317 ret = 1;
1318 goto out_free;
1319 }
hailfinger42a850a2010-07-13 23:56:13 +00001320 }
1321
David Hendricksdf29a832013-06-28 14:33:51 -07001322 if (filename)
1323 ret = write_buf_to_file(buf, size, filename);
1324
hailfinger42a850a2010-07-13 23:56:13 +00001325out_free:
1326 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001327 if (ret)
1328 msg_cerr("FAILED.");
1329 else
1330 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001331 return ret;
1332}
1333
hailfingerb437e282010-11-04 01:04:27 +00001334/* This function shares a lot of its structure with erase_and_write_flash() and
1335 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001336 * Even if an error is found, the function will keep going and check the rest.
1337 */
hailfinger48ed3e22011-05-04 00:39:50 +00001338static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001339{
hailfingerb91c08c2011-08-15 19:54:20 +00001340 int i, j, k;
1341 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001342
1343 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1344 unsigned int done = 0;
1345 struct block_eraser eraser = flash->block_erasers[k];
1346
1347 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1348 /* Blocks with zero size are bugs in flashchips.c. */
1349 if (eraser.eraseblocks[i].count &&
1350 !eraser.eraseblocks[i].size) {
1351 msg_gerr("ERROR: Flash chip %s erase function "
1352 "%i region %i has size 0. Please report"
1353 " a bug at flashrom@flashrom.org\n",
1354 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001355 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001356 }
1357 /* Blocks with zero count are bugs in flashchips.c. */
1358 if (!eraser.eraseblocks[i].count &&
1359 eraser.eraseblocks[i].size) {
1360 msg_gerr("ERROR: Flash chip %s erase function "
1361 "%i region %i has count 0. Please report"
1362 " a bug at flashrom@flashrom.org\n",
1363 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001364 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001365 }
1366 done += eraser.eraseblocks[i].count *
1367 eraser.eraseblocks[i].size;
1368 }
hailfinger9fed35d2010-01-19 06:42:46 +00001369 /* Empty eraseblock definition with erase function. */
1370 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001371 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001372 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001373 if (!done)
1374 continue;
1375 if (done != flash->total_size * 1024) {
1376 msg_gerr("ERROR: Flash chip %s erase function %i "
1377 "region walking resulted in 0x%06x bytes total,"
1378 " expected 0x%06x bytes. Please report a bug at"
1379 " flashrom@flashrom.org\n", flash->name, k,
1380 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001381 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001382 }
hailfinger9fed35d2010-01-19 06:42:46 +00001383 if (!eraser.block_erase)
1384 continue;
1385 /* Check if there are identical erase functions for different
1386 * layouts. That would imply "magic" erase functions. The
1387 * easiest way to check this is with function pointers.
1388 */
uwef6f94d42010-03-13 17:28:29 +00001389 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001390 if (eraser.block_erase ==
1391 flash->block_erasers[j].block_erase) {
1392 msg_gerr("ERROR: Flash chip %s erase function "
1393 "%i and %i are identical. Please report"
1394 " a bug at flashrom@flashrom.org\n",
1395 flash->name, k, j);
1396 ret = 1;
1397 }
uwef6f94d42010-03-13 17:28:29 +00001398 }
hailfinger45177872010-01-18 08:14:43 +00001399 }
hailfinger9fed35d2010-01-19 06:42:46 +00001400 return ret;
hailfinger45177872010-01-18 08:14:43 +00001401}
1402
hailfingerb437e282010-11-04 01:04:27 +00001403static int erase_and_write_block_helper(struct flashchip *flash,
1404 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001405 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001406 uint8_t *newcontents,
1407 int (*erasefn) (struct flashchip *flash,
1408 unsigned int addr,
1409 unsigned int len))
1410{
stefanctc5eb8a92011-11-23 09:13:48 +00001411 unsigned int starthere = 0, lenhere = 0;
1412 int ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001413 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1414
hailfinger90fcf9b2010-11-05 14:51:59 +00001415 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001416 * need to be adjusted here to keep the impression of proper abstraction
1417 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001418 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001419 newcontents += start;
1420 msg_cdbg(":");
1421 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001422 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001423 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001424 msg_cdbg("E");
1425 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001426 if (ret) {
1427 if (ret == ACCESS_DENIED)
1428 msg_cdbg("D");
1429 else
1430 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001431 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001432 }
1433
hailfingerac8e3182011-06-26 17:04:16 +00001434 if (check_erased_range(flash, start, len)) {
1435 msg_cerr("ERASE FAILED!\n");
1436 return -1;
1437 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001438 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001439 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001440 skip = 0;
1441 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001442 /* get_next_write() sets starthere to a new value after the call. */
1443 while ((lenhere = get_next_write(curcontents + starthere,
1444 newcontents + starthere,
1445 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001446 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001447 if (!writecount++)
1448 msg_cdbg("W");
1449 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001450 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001451 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001452 if (ret) {
1453 if (ret == ACCESS_DENIED)
1454 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001455 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001456 }
hailfingerb437e282010-11-04 01:04:27 +00001457 starthere += lenhere;
1458 skip = 0;
1459 }
1460 if (skip)
1461 msg_cdbg("S");
1462 return ret;
1463}
1464
hailfinger83541b32010-07-13 00:42:00 +00001465static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1466 int (*do_something) (struct flashchip *flash,
1467 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001468 unsigned int len,
1469 uint8_t *param1,
1470 uint8_t *param2,
1471 int (*erasefn) (
1472 struct flashchip *flash,
1473 unsigned int addr,
1474 unsigned int len)),
1475 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001476{
David Hendricks1ed1d352011-11-23 17:54:37 -08001477 int i, j, rc = 0;
hailfingerb91c08c2011-08-15 19:54:20 +00001478 unsigned int start = 0;
1479 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001480 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001481
David Hendricks54777392015-01-11 18:55:14 -08001482 if (required_erase_size &&
1483 (eraser.eraseblocks[i].size != required_erase_size)) {
1484 msg_cdbg("%u does not meet erase alignment requirement\n",
1485 eraser.eraseblocks[i].size);
1486 return -1;
1487 }
1488
hailfinger2b8c9382010-07-13 00:37:19 +00001489 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1490 /* count==0 for all automatically initialized array
1491 * members so the loop below won't be executed for them.
1492 */
1493 len = eraser.eraseblocks[i].size;
1494 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001495 /* Print this for every block except the first one. */
1496 if (i || j)
1497 msg_cdbg(", ");
1498 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001499 start + len - 1);
David Hendricks1ed1d352011-11-23 17:54:37 -08001500 rc = do_something(flash, start, len, param1, param2,
1501 eraser.block_erase);
1502 if (rc) {
1503 if (ignore_error(rc))
1504 rc = 0;
1505 else
1506 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001507 }
hailfinger2b8c9382010-07-13 00:37:19 +00001508 start += len;
1509 }
1510 }
hailfingerb437e282010-11-04 01:04:27 +00001511 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001512 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001513}
1514
stefanct569dbb62011-07-01 00:19:12 +00001515static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001516{
1517 struct block_eraser eraser = flash->block_erasers[k];
1518
1519 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1520 if (log)
1521 msg_cdbg("not defined. ");
1522 return 1;
1523 }
1524 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1525 if (log)
1526 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001527 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001528 return 1;
1529 }
1530 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1531 if (log)
1532 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001533 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001534 return 1;
1535 }
1536 return 0;
1537}
1538
uwe8d342eb2011-07-28 08:13:25 +00001539int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1540 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001541{
hailfinger8f524a82011-07-21 21:21:04 +00001542 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001543 uint8_t *curcontents;
1544 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001545 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001546
hailfingercf848f12010-12-05 15:14:44 +00001547 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001548 curcontents = malloc(size);
1549 if (!curcontents) {
1550 msg_gerr("Out of memory!\n");
1551 exit(1);
1552 }
hailfingerb437e282010-11-04 01:04:27 +00001553 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1554 memcpy(curcontents, oldcontents, size);
1555
hailfinger7df21362009-09-05 02:30:58 +00001556 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001557 if (k != 0)
1558 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001559 if (!usable_erasefunctions) {
1560 msg_cdbg("No usable erase functions left.\n");
1561 break;
1562 }
stefanctf0bc4712011-07-26 14:28:35 +00001563 msg_cdbg("Trying erase function %i... ", k);
1564 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001565 continue;
hailfingercf848f12010-12-05 15:14:44 +00001566 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001567 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1568 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001569 /* If everything is OK, don't try another erase function. */
1570 if (!ret)
1571 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001572 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001573 * contents are. If no usable erase functions remain, we can
1574 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001575 */
hailfingercf848f12010-12-05 15:14:44 +00001576 if (!usable_erasefunctions)
1577 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001578 /* Reading the whole chip may take a while, inform the user even
1579 * in non-verbose mode.
1580 */
1581 msg_cinfo("Reading current flash chip contents... ");
David Hendrickse3451942013-03-21 17:23:29 -07001582 if (read_flash(flash, curcontents, 0, size)) {
hailfinger6237f5e2010-12-02 02:41:55 +00001583 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001584 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001585 /* We have no idea about the flash chip contents, so
1586 * retrying with another erase function is pointless.
1587 */
1588 break;
1589 }
David Hendricks89a45e52011-11-22 16:56:22 -08001590 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001591 }
hailfingerb437e282010-11-04 01:04:27 +00001592 /* Free the scratchpad. */
1593 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001594
hailfinger7df21362009-09-05 02:30:58 +00001595 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001596 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001597 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001598 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001599 }
1600 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001601}
1602
hailfinger4c47e9d2010-10-19 22:06:20 +00001603void nonfatal_help_message(void)
1604{
1605 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1606 "This means we have to add special support for your board, "
1607 "programmer or flash chip.\n"
1608 "Please report this on IRC at irc.freenode.net (channel "
1609 "#flashrom) or\n"
1610 "mail flashrom@flashrom.org!\n"
1611 "-------------------------------------------------------------"
1612 "------------------\n"
1613 "You may now reboot or simply leave the machine running.\n");
1614}
1615
uweb34ec9f2009-10-01 18:40:02 +00001616void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001617{
snelsone42c3802010-05-07 20:09:04 +00001618 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001619 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001620 "mail flashrom@flashrom.org with FAILED: your board name in "
1621 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001622 "-------------------------------------------------------------"
1623 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001624 "DO NOT REBOOT OR POWEROFF!\n");
1625}
1626
uwe8d342eb2011-07-28 08:13:25 +00001627/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001628void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001629{
1630 enum programmer p;
1631 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001632 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001633 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001634 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001635 }
Simon Glass8dc82732013-07-16 10:13:51 -06001636 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001637}
1638
hailfingerf79d1712010-10-06 23:48:34 +00001639void list_programmers_linebreak(int startcol, int cols, int paren)
1640{
1641 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001642 int pnamelen;
1643 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001644 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001645 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001646
1647 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1648 pname = programmer_table[p].name;
1649 pnamelen = strlen(pname);
1650 if (remaining - pnamelen - 2 < 0) {
1651 if (firstline)
1652 firstline = 0;
1653 else
1654 printf("\n");
1655 for (i = 0; i < startcol; i++)
1656 printf(" ");
1657 remaining = cols - startcol;
1658 } else {
1659 printf(" ");
1660 remaining--;
1661 }
1662 if (paren && (p == 0)) {
1663 printf("(");
1664 remaining--;
1665 }
1666 printf("%s", pname);
1667 remaining -= pnamelen;
1668 if (p < PROGRAMMER_INVALID - 1) {
1669 printf(",");
1670 remaining--;
1671 } else {
1672 if (paren)
1673 printf(")");
1674 printf("\n");
1675 }
1676 }
1677}
1678
hailfinger3b471632010-03-27 16:36:40 +00001679void print_sysinfo(void)
1680{
David Hendricksc6c9f822010-11-03 15:07:01 -07001681 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001682#if HAVE_UTSNAME == 1
1683 struct utsname osinfo;
1684 uname(&osinfo);
1685
David Hendricksc6c9f822010-11-03 15:07:01 -07001686 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001687 osinfo.machine);
1688#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001689 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001690#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001691 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001692#if NEED_PCI == 1
1693#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001694 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001695#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001696 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001697#endif
1698#endif
1699#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001700 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001701#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001702 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001703#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001704 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001705#endif
hailfinger3b471632010-03-27 16:36:40 +00001706#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001707 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001708#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001709 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001710#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001711 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001712#endif
1713#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001714 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001715#endif
1716#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001717 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001718#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001719 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001720#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001721 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001722}
1723
uwefdeca092008-01-21 15:24:22 +00001724void print_version(void)
1725{
David Hendricksc6c9f822010-11-03 15:07:01 -07001726 /* send to stderr for chromium os */
1727 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001728 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001729}
1730
hailfinger74819ad2010-05-15 15:04:37 +00001731void print_banner(void)
1732{
1733 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001734 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001735 msg_ginfo("\n");
1736}
1737
hailfingerc77acb52009-12-24 02:15:55 +00001738int selfcheck(void)
1739{
hailfinger45177872010-01-18 08:14:43 +00001740 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001741 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001742
1743 /* Safety check. Instead of aborting after the first error, check
1744 * if more errors exist.
1745 */
hailfingerc77acb52009-12-24 02:15:55 +00001746 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001747 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001748 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001749 }
stefanct8632c922011-07-26 14:33:46 +00001750 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001751 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001752 * For 'flashchips' we check the first element to be non-null. In the
1753 * other cases there exist use cases where the first element can be
1754 * null. */
Yunlian Jiang945c3392015-01-20 15:56:44 -08001755 if (flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001756 msg_gerr("Flashchips table miscompilation!\n");
1757 ret = 1;
1758 }
hailfinger45177872010-01-18 08:14:43 +00001759 for (flash = flashchips; flash && flash->name; flash++)
1760 if (selfcheck_eraseblocks(flash))
1761 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001762
hailfinger45177872010-01-18 08:14:43 +00001763 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001764}
1765
hailfinger48ed3e22011-05-04 00:39:50 +00001766void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001767{
1768 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001769 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001770 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001771 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001772 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001773 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001774 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001775 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001776 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001777 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001778 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001779 msg_cdbg(" WRITE");
1780 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001781 }
1782 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1783 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1784 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1785 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001786 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001787 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001788 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001789 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001790 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001791 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001792 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001793 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001794 msg_cdbg(" WRITE");
1795 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001796 }
hailfinger92cd8e32010-01-07 03:24:05 +00001797 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001798 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001799 "in the latest development\n"
1800 "version of flashrom. If you are running the latest "
1801 "development version,\n"
1802 "please email a report to flashrom@flashrom.org if "
1803 "any of the above operations\n"
1804 "work correctly for you with this flash part. Please "
1805 "include the flashrom\n"
1806 "output with the additional -V option for all "
1807 "operations you tested (-V, -Vr,\n"
1808 "-Vw, -VE), and mention which mainboard or "
1809 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001810 "Please mention your board in the subject line. "
1811 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001812 }
1813}
1814
hailfinger771fc182010-10-15 00:01:14 +00001815/* FIXME: This function signature needs to be improved once doit() has a better
1816 * function signature.
1817 */
stefanct02116582011-05-18 01:30:56 +00001818int 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 +00001819{
1820 if (!programmer_may_write && (write_it || erase_it)) {
1821 msg_perr("Write/erase is not working yet on your programmer in "
1822 "its current configuration.\n");
1823 /* --force is the wrong approach, but it's the best we can do
1824 * until the generic programmer parameter parser is merged.
1825 */
1826 if (!force)
1827 return 1;
1828 msg_cerr("Continuing anyway.\n");
1829 }
1830
1831 if (read_it || erase_it || write_it || verify_it) {
1832 /* Everything needs read. */
1833 if (flash->tested & TEST_BAD_READ) {
1834 msg_cerr("Read is not working on this chip. ");
1835 if (!force)
1836 return 1;
1837 msg_cerr("Continuing anyway.\n");
1838 }
1839 if (!flash->read) {
1840 msg_cerr("flashrom has no read function for this "
1841 "flash chip.\n");
1842 return 1;
1843 }
1844 }
1845 if (erase_it || write_it) {
1846 /* Write needs erase. */
1847 if (flash->tested & TEST_BAD_ERASE) {
1848 msg_cerr("Erase is not working on this chip. ");
1849 if (!force)
1850 return 1;
1851 msg_cerr("Continuing anyway.\n");
1852 }
stefancte1c5acf2011-07-04 07:27:17 +00001853 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001854 msg_cerr("flashrom has no erase function for this "
1855 "flash chip.\n");
1856 return 1;
1857 }
hailfinger771fc182010-10-15 00:01:14 +00001858 }
1859 if (write_it) {
1860 if (flash->tested & TEST_BAD_WRITE) {
1861 msg_cerr("Write is not working on this chip. ");
1862 if (!force)
1863 return 1;
1864 msg_cerr("Continuing anyway.\n");
1865 }
1866 if (!flash->write) {
1867 msg_cerr("flashrom has no write function for this "
1868 "flash chip.\n");
1869 return 1;
1870 }
1871 }
1872 return 0;
1873}
1874
hailfingerc77acb52009-12-24 02:15:55 +00001875/* This function signature is horrible. We need to design a better interface,
1876 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001877 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001878 */
Simon Glass9ad06c12013-07-03 22:08:17 +09001879int doit(struct flashchip *flash, int force, const char *filename, int read_it,
1880 int write_it, int erase_it, int verify_it, int extract_it,
1881 const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001882{
hailfinger4c47e9d2010-10-19 22:06:20 +00001883 uint8_t *oldcontents;
1884 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001885 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001886 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001887
stefanct02116582011-05-18 01:30:56 +00001888 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001889 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001890 ret = 1;
1891 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001892 }
1893
hailfinger771fc182010-10-15 00:01:14 +00001894 /* Given the existence of read locks, we want to unlock for read,
1895 * erase and write.
1896 */
1897 if (flash->unlock)
1898 flash->unlock(flash);
1899
David Hendricks6d62d752011-03-07 21:20:22 -08001900 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001901 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001902 ret = 1;
1903 goto out_nofree;
1904 }
1905
Simon Glass9ad06c12013-07-03 22:08:17 +09001906 if (extract_it) {
1907 ret = extract_regions(flash);
1908 goto out_nofree;
1909 }
1910
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001911 /* mark entries included using -i argument as "included" if they are
1912 found in the master rom_entries list */
1913 if (process_include_args() < 0) {
1914 ret = 1;
1915 goto out_nofree;
1916 }
1917
hailfinger771fc182010-10-15 00:01:14 +00001918 if (read_it) {
1919 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001920 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001921 }
hailfingerb437e282010-11-04 01:04:27 +00001922
stefanctd611e8f2011-07-12 22:35:21 +00001923 oldcontents = malloc(size);
1924 if (!oldcontents) {
1925 msg_gerr("Out of memory!\n");
1926 exit(1);
1927 }
Simon Glass4c214132013-07-16 10:09:28 -06001928 /* Assume worst case: All blocks are not erased. */
1929 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00001930 newcontents = malloc(size);
1931 if (!newcontents) {
1932 msg_gerr("Out of memory!\n");
1933 exit(1);
1934 }
Simon Glass4c214132013-07-16 10:09:28 -06001935 /* Assume best case: All blocks are erased. */
1936 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00001937 /* Side effect of the assumptions above: Default write action is erase
1938 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06001939 * oldcontents being completely unerased means we have to erase
1940 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00001941 */
1942
ollie0eb62d62004-12-08 20:10:01 +00001943 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00001944 /* FIXME: Do we really want the scary warning if erase failed?
1945 * After all, after erase the chip is either blank or partially
1946 * blank or it has the old contents. A blank chip won't boot,
1947 * so if the user wanted erase and reboots afterwards, the user
1948 * knows very well that booting won't work.
1949 */
hailfingerb437e282010-11-04 01:04:27 +00001950 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00001951 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00001952 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00001953 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001954 goto out;
hailfingerd217d122010-10-08 18:52:29 +00001955 }
hailfinger771fc182010-10-15 00:01:14 +00001956
hailfingerd217d122010-10-08 18:52:29 +00001957 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07001958 /*
1959 * Note: This must be done before any files specified by -i
1960 * arguments are processed merged into the newcontents since
1961 * -i files take priority. See http://crbug.com/263495.
1962 */
1963 if (filename) {
1964 if (read_buf_from_file(newcontents, size, filename)) {
1965 ret = 1;
1966 goto out;
1967 }
1968 } else {
1969 /* Content will be read from -i args, so they must
1970 * not overlap. */
1971 if (included_regions_overlap()) {
1972 msg_gerr("Error: Included regions must "
1973 "not overlap.\n");
1974 ret = 1;
1975 goto out;
1976 }
stepan1da96c02006-11-21 23:48:51 +00001977 }
1978
David Hendricksac82cac2012-06-19 10:29:37 -07001979#if 0
1980 /*
1981 * FIXME: show_id() causes failure if vendor:mainboard do not
1982 * match. This may happen if codenames are in flux.
1983 * See chrome-os-partner:10414.
1984 */
hailfinger90c7d542010-05-31 15:27:27 +00001985#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00001986 if (programmer == PROGRAMMER_INTERNAL)
1987 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00001988#endif
David Hendricksac82cac2012-06-19 10:29:37 -07001989#endif
ollie5672ac62004-03-17 22:22:08 +00001990 }
1991
David Hendricksc44d7a02011-10-17 11:28:43 -07001992 /* Obtain a reference image so that we can check whether regions need
1993 * to be erased and to give better diagnostics in case write fails.
David Hendricks52ddff02013-07-23 15:05:14 -07001994 * If --fast-verify is used then only the regions which are included
1995 * using -i will be read.
hailfinger4c47e9d2010-10-19 22:06:20 +00001996 */
David Hendricksc44d7a02011-10-17 11:28:43 -07001997 if (diff_file) {
1998 msg_cdbg("Reading old contents from file... ");
1999 if (read_buf_from_file(oldcontents, size, diff_file)) {
2000 ret = 1;
2001 msg_cdbg("FAILED.\n");
2002 goto out;
2003 }
2004 } else {
2005 msg_cdbg("Reading old contents from flash chip... ");
David Hendricksd4e712c2013-08-02 17:06:16 -07002006 if (verify_it == VERIFY_PARTIAL) {
2007 if (handle_partial_read(flash, oldcontents,
2008 read_flash, 0) < 0) {
David Hendricks52ddff02013-07-23 15:05:14 -07002009 ret = 1;
2010 msg_cdbg("FAILED.\n");
2011 goto out;
2012 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002013 } else {
2014 if (read_flash(flash, oldcontents, 0, size)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002015 ret = 1;
2016 msg_cdbg("FAILED.\n");
2017 goto out;
2018 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002019 }
hailfinger4c47e9d2010-10-19 22:06:20 +00002020 }
David Hendricks89a45e52011-11-22 16:56:22 -08002021 msg_cdbg("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00002022
David Hendricksdf29a832013-06-28 14:33:51 -07002023 /*
2024 * Note: This must be done after reading the file specified for the
2025 * -w/-v argument, if any, so that files specified using -i end up
2026 * in the "newcontents" buffer before being written.
2027 * See http://crbug.com/263495.
2028 */
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002029 if (handle_romentries(flash, oldcontents, newcontents)) {
2030 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002031 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002032 goto out;
2033 }
uwef6641642007-05-09 10:17:44 +00002034
stuge8ce3a3c2008-04-28 14:47:30 +00002035 if (write_it) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002036 // parse the new fmap
David Hendricksb907de32014-08-11 16:47:09 -07002037 if ((ret = cros_ec_prepare(newcontents, size))) {
2038 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002039 goto out;
2040 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002041
hailfingerb437e282010-11-04 01:04:27 +00002042 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2043 msg_cerr("Uh oh. Erase/write failed. Checking if "
2044 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002045 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002046 if (!memcmp(oldcontents, newcontents, size)) {
2047 msg_cinfo("Good. It seems nothing was "
2048 "changed.\n");
2049 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002050 ret = 1;
2051 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002052 }
2053 }
hailfingerd217d122010-10-08 18:52:29 +00002054 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002055 ret = 1;
2056 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002057 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002058
David Hendricksb907de32014-08-11 16:47:09 -07002059 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002060 if (ret < 0) {
2061 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002062 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002063 emergency_help_message();
2064 ret = 1;
2065 goto out;
2066 } else if (ret > 0) {
2067 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002068 msg_pdbg("CROS_EC needs 2nd pass.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002069 if (read_flash(flash, oldcontents, 0, size)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002070 msg_cerr("Uh oh. Cannot get latest content.\n");
2071 emergency_help_message();
2072 ret = 1;
2073 goto out;
2074 }
2075 // write 2nd pass
2076 if (erase_and_write_flash(flash, oldcontents,
2077 newcontents)) {
David Hendricksb907de32014-08-11 16:47:09 -07002078 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002079 emergency_help_message();
2080 ret = 1;
2081 goto out;
2082 }
2083 ret = 0;
2084 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002085
David Hendricksb907de32014-08-11 16:47:09 -07002086 if (cros_ec_finish() < 0) {
2087 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002088 emergency_help_message();
2089 ret = 1;
2090 goto out;
2091 }
stuge8ce3a3c2008-04-28 14:47:30 +00002092 }
ollie6a600992005-11-26 21:55:36 +00002093
hailfinger0459e1c2009-08-19 13:55:34 +00002094 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002095 if ((write_it || erase_it) && !content_has_changed) {
2096 msg_gdbg("Nothing was erased or written, skipping "
2097 "verification\n");
2098 } else {
2099 /* Work around chips which need some time to calm down. */
2100 if (write_it && verify_it != VERIFY_PARTIAL)
2101 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002102
David Hendricks9ba79fb2015-04-03 12:06:16 -07002103 ret = verify_flash(flash, newcontents, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002104
David Hendricks9ba79fb2015-04-03 12:06:16 -07002105 /* If we tried to write, and verification now fails, we
2106 * might have an emergency situation.
2107 */
2108 if (ret && write_it)
2109 emergency_help_message();
2110 }
hailfinger0459e1c2009-08-19 13:55:34 +00002111 }
ollie6a600992005-11-26 21:55:36 +00002112
hailfinger90fcf9b2010-11-05 14:51:59 +00002113out:
2114 free(oldcontents);
2115 free(newcontents);
2116out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002117 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002118 /*
2119 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2120 * tree. This is because some operations, such as write protection,
2121 * requires programmer_shutdown() but does not call doit().
2122 */
2123// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002124 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002125}