blob: 9d25ce841e1a8226c33f940221ad565120e547b9 [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,
David Hendricks55cdd9c2015-11-25 14:37:26 -080098
99 /*
100 * "Internal" implies in-system programming on a live system, so
101 * handle with paranoia to catch errors early. If something goes
102 * wrong then hopefully the system will still be recoverable.
103 */
104 .paranoid = 1,
hailfingerabe249e2009-05-08 17:43:22 +0000105 },
hailfinger80422e22009-12-13 22:28:00 +0000106#endif
stepan927d4e22007-04-04 22:45:58 +0000107
hailfinger90c7d542010-05-31 15:27:27 +0000108#if CONFIG_DUMMY == 1
hailfingera9df33c2009-05-09 00:54:55 +0000109 {
hailfinger3548a9a2009-08-12 14:34:35 +0000110 .name = "dummy",
hailfinger6c69ab02009-05-11 15:46:43 +0000111 .init = dummy_init,
hailfinger11ae3c42009-05-11 14:13:25 +0000112 .map_flash_region = dummy_map,
113 .unmap_flash_region = dummy_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000114 .delay = internal_delay,
hailfingera9df33c2009-05-09 00:54:55 +0000115 },
hailfinger571a6b32009-09-16 10:09:21 +0000116#endif
hailfingera9df33c2009-05-09 00:54:55 +0000117
hailfinger90c7d542010-05-31 15:27:27 +0000118#if CONFIG_NIC3COM == 1
uwe0f5a3a22009-05-13 11:36:06 +0000119 {
hailfinger3548a9a2009-08-12 14:34:35 +0000120 .name = "nic3com",
uwe0f5a3a22009-05-13 11:36:06 +0000121 .init = nic3com_init,
uwe3e656bd2009-05-17 23:12:17 +0000122 .map_flash_region = fallback_map,
123 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000124 .delay = internal_delay,
uwe0f5a3a22009-05-13 11:36:06 +0000125 },
hailfinger571a6b32009-09-16 10:09:21 +0000126#endif
uwe0f5a3a22009-05-13 11:36:06 +0000127
hailfinger90c7d542010-05-31 15:27:27 +0000128#if CONFIG_NICREALTEK == 1
hailfinger5aa36982010-05-21 21:54:07 +0000129 {
hailfinger0d703d42011-03-07 01:08:09 +0000130 /* This programmer works for Realtek RTL8139 and SMC 1211. */
uwe8d342eb2011-07-28 08:13:25 +0000131 .name = "nicrealtek",
132 //.name = "nicsmc1211",
133 .init = nicrealtek_init,
134 .map_flash_region = fallback_map,
135 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000136 .delay = internal_delay,
hailfinger5aa36982010-05-21 21:54:07 +0000137 },
hailfinger5aa36982010-05-21 21:54:07 +0000138#endif
139
hailfingerf0a368f2010-06-07 22:37:54 +0000140#if CONFIG_NICNATSEMI == 1
141 {
uwe8d342eb2011-07-28 08:13:25 +0000142 .name = "nicnatsemi",
143 .init = nicnatsemi_init,
144 .map_flash_region = fallback_map,
145 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000146 .delay = internal_delay,
hailfingerf0a368f2010-06-07 22:37:54 +0000147 },
148#endif
hailfinger5aa36982010-05-21 21:54:07 +0000149
hailfinger90c7d542010-05-31 15:27:27 +0000150#if CONFIG_GFXNVIDIA == 1
uweff4576d2009-09-30 18:29:55 +0000151 {
152 .name = "gfxnvidia",
153 .init = gfxnvidia_init,
uweff4576d2009-09-30 18:29:55 +0000154 .map_flash_region = fallback_map,
155 .unmap_flash_region = fallback_unmap,
uweff4576d2009-09-30 18:29:55 +0000156 .delay = internal_delay,
157 },
158#endif
159
hailfinger90c7d542010-05-31 15:27:27 +0000160#if CONFIG_DRKAISER == 1
ruikda922a12009-05-17 19:39:27 +0000161 {
uwee2f95ef2009-09-02 23:00:46 +0000162 .name = "drkaiser",
163 .init = drkaiser_init,
uwee2f95ef2009-09-02 23:00:46 +0000164 .map_flash_region = fallback_map,
165 .unmap_flash_region = fallback_unmap,
uwee2f95ef2009-09-02 23:00:46 +0000166 .delay = internal_delay,
167 },
hailfinger571a6b32009-09-16 10:09:21 +0000168#endif
uwee2f95ef2009-09-02 23:00:46 +0000169
hailfinger90c7d542010-05-31 15:27:27 +0000170#if CONFIG_SATASII == 1
uwee2f95ef2009-09-02 23:00:46 +0000171 {
hailfinger3548a9a2009-08-12 14:34:35 +0000172 .name = "satasii",
ruikda922a12009-05-17 19:39:27 +0000173 .init = satasii_init,
uwe3e656bd2009-05-17 23:12:17 +0000174 .map_flash_region = fallback_map,
175 .unmap_flash_region = fallback_unmap,
hailfingere5829f62009-06-05 17:48:08 +0000176 .delay = internal_delay,
ruikda922a12009-05-17 19:39:27 +0000177 },
hailfinger571a6b32009-09-16 10:09:21 +0000178#endif
ruikda922a12009-05-17 19:39:27 +0000179
hailfinger90c7d542010-05-31 15:27:27 +0000180#if CONFIG_ATAHPT == 1
uwe7e627c82010-02-21 21:17:00 +0000181 {
182 .name = "atahpt",
183 .init = atahpt_init,
uwe7e627c82010-02-21 21:17:00 +0000184 .map_flash_region = fallback_map,
185 .unmap_flash_region = fallback_unmap,
uwe7e627c82010-02-21 21:17:00 +0000186 .delay = internal_delay,
187 },
188#endif
189
hailfinger90c7d542010-05-31 15:27:27 +0000190#if CONFIG_FT2232_SPI == 1
hailfingerf31da3d2009-06-16 21:08:06 +0000191 {
hailfinger90c7d542010-05-31 15:27:27 +0000192 .name = "ft2232_spi",
hailfingerf31da3d2009-06-16 21:08:06 +0000193 .init = ft2232_spi_init,
hailfinger6fe23d62009-08-12 11:39:29 +0000194 .map_flash_region = fallback_map,
195 .unmap_flash_region = fallback_unmap,
hailfingerf31da3d2009-06-16 21:08:06 +0000196 .delay = internal_delay,
197 },
hailfingerd9dcfbd2009-08-19 13:27:58 +0000198#endif
hailfinger6fe23d62009-08-12 11:39:29 +0000199
hailfinger90c7d542010-05-31 15:27:27 +0000200#if CONFIG_SERPROG == 1
hailfinger37b4fbf2009-06-23 11:33:43 +0000201 {
hailfinger3548a9a2009-08-12 14:34:35 +0000202 .name = "serprog",
hailfinger37b4fbf2009-06-23 11:33:43 +0000203 .init = serprog_init,
hailfinger37b4fbf2009-06-23 11:33:43 +0000204 .map_flash_region = fallback_map,
205 .unmap_flash_region = fallback_unmap,
hailfinger37b4fbf2009-06-23 11:33:43 +0000206 .delay = serprog_delay,
207 },
hailfinger74d88a72009-08-12 16:17:41 +0000208#endif
hailfingerf31da3d2009-06-16 21:08:06 +0000209
hailfinger90c7d542010-05-31 15:27:27 +0000210#if CONFIG_BUSPIRATE_SPI == 1
hailfinger9c5add72009-11-24 00:20:03 +0000211 {
hailfinger90c7d542010-05-31 15:27:27 +0000212 .name = "buspirate_spi",
hailfinger9c5add72009-11-24 00:20:03 +0000213 .init = buspirate_spi_init,
hailfinger9c5add72009-11-24 00:20:03 +0000214 .map_flash_region = fallback_map,
215 .unmap_flash_region = fallback_unmap,
hailfinger9c5add72009-11-24 00:20:03 +0000216 .delay = internal_delay,
217 },
218#endif
219
Anton Staafb2647882014-09-17 15:13:43 -0700220#if CONFIG_RAIDEN_DEBUG_SPI == 1
221 {
222 .name = "raiden_debug_spi",
223 .init = raiden_debug_spi_init,
224 .map_flash_region = fallback_map,
225 .unmap_flash_region = fallback_unmap,
226 .delay = internal_delay,
227 },
228#endif
229
hailfinger90c7d542010-05-31 15:27:27 +0000230#if CONFIG_DEDIPROG == 1
hailfingerdfb32a02010-01-19 11:15:48 +0000231 {
232 .name = "dediprog",
233 .init = dediprog_init,
hailfingerdfb32a02010-01-19 11:15:48 +0000234 .map_flash_region = fallback_map,
235 .unmap_flash_region = fallback_unmap,
hailfingerdfb32a02010-01-19 11:15:48 +0000236 .delay = internal_delay,
237 },
238#endif
239
hailfinger52c4fa02010-07-21 10:26:01 +0000240#if CONFIG_RAYER_SPI == 1
241 {
242 .name = "rayer_spi",
243 .init = rayer_spi_init,
hailfinger52c4fa02010-07-21 10:26:01 +0000244 .map_flash_region = fallback_map,
245 .unmap_flash_region = fallback_unmap,
hailfinger52c4fa02010-07-21 10:26:01 +0000246 .delay = internal_delay,
247 },
248#endif
249
hailfinger7949b652011-05-08 00:24:18 +0000250#if CONFIG_NICINTEL == 1
251 {
252 .name = "nicintel",
253 .init = nicintel_init,
hailfinger7949b652011-05-08 00:24:18 +0000254 .map_flash_region = fallback_map,
255 .unmap_flash_region = fallback_unmap,
hailfinger7949b652011-05-08 00:24:18 +0000256 .delay = internal_delay,
257 },
258#endif
259
uwe6764e922010-09-03 18:21:21 +0000260#if CONFIG_NICINTEL_SPI == 1
261 {
uwe8d342eb2011-07-28 08:13:25 +0000262 .name = "nicintel_spi",
263 .init = nicintel_spi_init,
264 .map_flash_region = fallback_map,
265 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000266 .delay = internal_delay,
uwe6764e922010-09-03 18:21:21 +0000267 },
268#endif
269
hailfingerfb1f31f2010-12-03 14:48:11 +0000270#if CONFIG_OGP_SPI == 1
271 {
uwe8d342eb2011-07-28 08:13:25 +0000272 .name = "ogp_spi",
273 .init = ogp_spi_init,
274 .map_flash_region = fallback_map,
275 .unmap_flash_region = fallback_unmap,
uwe8d342eb2011-07-28 08:13:25 +0000276 .delay = internal_delay,
hailfingerfb1f31f2010-12-03 14:48:11 +0000277 },
278#endif
279
hailfinger935365d2011-02-04 21:37:59 +0000280#if CONFIG_SATAMV == 1
281 {
282 .name = "satamv",
283 .init = satamv_init,
hailfinger935365d2011-02-04 21:37:59 +0000284 .map_flash_region = fallback_map,
285 .unmap_flash_region = fallback_unmap,
hailfinger935365d2011-02-04 21:37:59 +0000286 .delay = internal_delay,
287 },
288#endif
289
David Hendrickscebee892015-05-23 20:30:30 -0700290#if CONFIG_LINUX_MTD == 1
291 {
292 .name = "linux_mtd",
293 .init = linux_mtd_init,
294 .map_flash_region = fallback_map,
295 .unmap_flash_region = fallback_unmap,
296 .delay = internal_delay,
297 },
298#endif
299
uwe7df6dda2011-09-03 18:37:52 +0000300#if CONFIG_LINUX_SPI == 1
301 {
302 .name = "linux_spi",
303 .init = linux_spi_init,
304 .map_flash_region = fallback_map,
305 .unmap_flash_region = fallback_unmap,
uwe7df6dda2011-09-03 18:37:52 +0000306 .delay = internal_delay,
307 },
308#endif
309
hailfinger3548a9a2009-08-12 14:34:35 +0000310 {}, /* This entry corresponds to PROGRAMMER_INVALID. */
hailfingerabe249e2009-05-08 17:43:22 +0000311};
stepan927d4e22007-04-04 22:45:58 +0000312
David Hendricksbf36f092010-11-02 23:39:29 -0700313#define CHIP_RESTORE_MAXFN 4
314static int chip_restore_fn_count = 0;
315struct chip_restore_func_data {
316 CHIP_RESTORE_CALLBACK;
317 struct flashchip *flash;
318 uint8_t status;
319} static chip_restore_fn[CHIP_RESTORE_MAXFN];
320
David Hendricks668f29d2011-01-27 18:51:45 -0800321
hailfingerf31cbdc2010-11-10 15:25:18 +0000322#define SHUTDOWN_MAXFN 32
hailfingerdc6f7972010-02-14 01:20:28 +0000323static int shutdown_fn_count = 0;
324struct shutdown_func_data {
dhendrix0ffc2eb2011-06-14 01:35:36 +0000325 int (*func) (void *data);
hailfingerdc6f7972010-02-14 01:20:28 +0000326 void *data;
hailfinger1ff33dc2010-07-03 11:02:10 +0000327} static shutdown_fn[SHUTDOWN_MAXFN];
328/* Initialize to 0 to make sure nobody registers a shutdown function before
329 * programmer init.
330 */
331static int may_register_shutdown = 0;
hailfingerdc6f7972010-02-14 01:20:28 +0000332
stefanct569dbb62011-07-01 00:19:12 +0000333static int check_block_eraser(const struct flashchip *flash, int k, int log);
334
hailfingerdc6f7972010-02-14 01:20:28 +0000335/* Register a function to be executed on programmer shutdown.
336 * The advantage over atexit() is that you can supply a void pointer which will
337 * be used as parameter to the registered function upon programmer shutdown.
338 * This pointer can point to arbitrary data used by said function, e.g. undo
339 * information for GPIO settings etc. If unneeded, set data=NULL.
340 * Please note that the first (void *data) belongs to the function signature of
341 * the function passed as first parameter.
342 */
dhendrix0ffc2eb2011-06-14 01:35:36 +0000343int register_shutdown(int (*function) (void *data), void *data)
hailfingerdc6f7972010-02-14 01:20:28 +0000344{
345 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
hailfinger63932d42010-06-04 23:20:21 +0000346 msg_perr("Tried to register more than %i shutdown functions.\n",
hailfingerdc6f7972010-02-14 01:20:28 +0000347 SHUTDOWN_MAXFN);
348 return 1;
349 }
hailfinger1ff33dc2010-07-03 11:02:10 +0000350 if (!may_register_shutdown) {
351 msg_perr("Tried to register a shutdown function before "
352 "programmer init.\n");
353 return 1;
354 }
hailfingerdc6f7972010-02-14 01:20:28 +0000355 shutdown_fn[shutdown_fn_count].func = function;
356 shutdown_fn[shutdown_fn_count].data = data;
357 shutdown_fn_count++;
358
359 return 0;
360}
361
David Hendricksbf36f092010-11-02 23:39:29 -0700362//int register_chip_restore(int (*function) (void *data), void *data)
363int register_chip_restore(CHIP_RESTORE_CALLBACK,
364 struct flashchip *flash, uint8_t status)
365{
366 if (chip_restore_fn_count >= CHIP_RESTORE_MAXFN) {
367 msg_perr("Tried to register more than %i chip restore"
368 " functions.\n", CHIP_RESTORE_MAXFN);
369 return 1;
370 }
371 chip_restore_fn[chip_restore_fn_count].func = func; /* from macro */
372 chip_restore_fn[chip_restore_fn_count].flash = flash;
373 chip_restore_fn[chip_restore_fn_count].status = status;
374 chip_restore_fn_count++;
375
376 return 0;
377}
378
hailfinger969e2f32011-09-08 00:00:29 +0000379int programmer_init(enum programmer prog, char *param)
uweabe92a52009-05-16 22:36:00 +0000380{
hailfinger1ef766d2010-07-06 09:55:48 +0000381 int ret;
hailfinger969e2f32011-09-08 00:00:29 +0000382
383 if (prog >= PROGRAMMER_INVALID) {
384 msg_perr("Invalid programmer specified!\n");
385 return -1;
386 }
387 programmer = prog;
hailfinger1ff33dc2010-07-03 11:02:10 +0000388 /* Initialize all programmer specific data. */
389 /* Default to unlimited decode sizes. */
390 max_rom_decode = (const struct decode_sizes) {
391 .parallel = 0xffffffff,
392 .lpc = 0xffffffff,
393 .fwh = 0xffffffff,
uwe8d342eb2011-07-28 08:13:25 +0000394 .spi = 0xffffffff,
hailfinger1ff33dc2010-07-03 11:02:10 +0000395 };
hailfingere1e41ea2011-07-27 07:13:06 +0000396 buses_supported = BUS_NONE;
hailfinger1ff33dc2010-07-03 11:02:10 +0000397 /* Default to top aligned flash at 4 GB. */
398 flashbase = 0;
399 /* Registering shutdown functions is now allowed. */
400 may_register_shutdown = 1;
hailfinger5828baf2010-07-03 12:14:25 +0000401 /* Default to allowing writes. Broken programmers set this to 0. */
402 programmer_may_write = 1;
hailfinger1ff33dc2010-07-03 11:02:10 +0000403
404 programmer_param = param;
405 msg_pdbg("Initializing %s programmer\n",
406 programmer_table[programmer].name);
hailfinger1ef766d2010-07-06 09:55:48 +0000407 ret = programmer_table[programmer].init();
408 if (programmer_param && strlen(programmer_param)) {
409 msg_perr("Unhandled programmer parameters: %s\n",
410 programmer_param);
411 /* Do not error out here, the init itself was successful. */
412 }
413 return ret;
uweabe92a52009-05-16 22:36:00 +0000414}
415
David Hendricksbf36f092010-11-02 23:39:29 -0700416int chip_restore()
417{
418 int rc = 0;
419
420 while (chip_restore_fn_count > 0) {
421 int i = --chip_restore_fn_count;
422 rc |= chip_restore_fn[i].func(chip_restore_fn[i].flash,
423 chip_restore_fn[i].status);
424 }
425
426 return rc;
427}
428
uweabe92a52009-05-16 22:36:00 +0000429int programmer_shutdown(void)
430{
dhendrix0ffc2eb2011-06-14 01:35:36 +0000431 int ret = 0;
432
hailfinger1ff33dc2010-07-03 11:02:10 +0000433 /* Registering shutdown functions is no longer allowed. */
434 may_register_shutdown = 0;
435 while (shutdown_fn_count > 0) {
436 int i = --shutdown_fn_count;
dhendrix0ffc2eb2011-06-14 01:35:36 +0000437 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
hailfinger1ff33dc2010-07-03 11:02:10 +0000438 }
dhendrix0ffc2eb2011-06-14 01:35:36 +0000439 return ret;
uweabe92a52009-05-16 22:36:00 +0000440}
441
442void *programmer_map_flash_region(const char *descr, unsigned long phys_addr,
443 size_t len)
444{
445 return programmer_table[programmer].map_flash_region(descr,
446 phys_addr, len);
447}
448
449void programmer_unmap_flash_region(void *virt_addr, size_t len)
450{
451 programmer_table[programmer].unmap_flash_region(virt_addr, len);
452}
453
454void chip_writeb(uint8_t val, chipaddr addr)
455{
hailfinger76bb7e92011-11-09 23:40:00 +0000456 par_programmer->chip_writeb(val, addr);
uweabe92a52009-05-16 22:36:00 +0000457}
458
459void chip_writew(uint16_t val, chipaddr addr)
460{
hailfinger76bb7e92011-11-09 23:40:00 +0000461 par_programmer->chip_writew(val, addr);
uweabe92a52009-05-16 22:36:00 +0000462}
463
464void chip_writel(uint32_t val, chipaddr addr)
465{
hailfinger76bb7e92011-11-09 23:40:00 +0000466 par_programmer->chip_writel(val, addr);
uweabe92a52009-05-16 22:36:00 +0000467}
468
hailfinger9d987ef2009-06-05 18:32:07 +0000469void chip_writen(uint8_t *buf, chipaddr addr, size_t len)
470{
hailfinger76bb7e92011-11-09 23:40:00 +0000471 par_programmer->chip_writen(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000472}
473
uweabe92a52009-05-16 22:36:00 +0000474uint8_t chip_readb(const chipaddr addr)
475{
hailfinger76bb7e92011-11-09 23:40:00 +0000476 return par_programmer->chip_readb(addr);
uweabe92a52009-05-16 22:36:00 +0000477}
478
479uint16_t chip_readw(const chipaddr addr)
480{
hailfinger76bb7e92011-11-09 23:40:00 +0000481 return par_programmer->chip_readw(addr);
uweabe92a52009-05-16 22:36:00 +0000482}
483
484uint32_t chip_readl(const chipaddr addr)
485{
hailfinger76bb7e92011-11-09 23:40:00 +0000486 return par_programmer->chip_readl(addr);
uweabe92a52009-05-16 22:36:00 +0000487}
488
hailfinger9d987ef2009-06-05 18:32:07 +0000489void chip_readn(uint8_t *buf, chipaddr addr, size_t len)
490{
hailfinger76bb7e92011-11-09 23:40:00 +0000491 par_programmer->chip_readn(buf, addr, len);
hailfinger9d987ef2009-06-05 18:32:07 +0000492}
493
hailfingere5829f62009-06-05 17:48:08 +0000494void programmer_delay(int usecs)
495{
496 programmer_table[programmer].delay(usecs);
497}
498
stuge5ff0e6c2009-01-26 00:39:57 +0000499void map_flash_registers(struct flashchip *flash)
stepan15e64bc2007-05-24 08:48:10 +0000500{
stepan15e64bc2007-05-24 08:48:10 +0000501 size_t size = flash->total_size * 1024;
hailfinger8577ad12009-05-11 14:01:17 +0000502 /* Flash registers live 4 MByte below the flash. */
hailfinger0459e1c2009-08-19 13:55:34 +0000503 /* FIXME: This is incorrect for nonstandard flashbase. */
hailfingerb91c08c2011-08-15 19:54:20 +0000504 flash->virtual_registers = (chipaddr)programmer_map_flash_region("flash chip registers", (0xFFFFFFFF - 0x400000 - size + 1), size);
stepan15e64bc2007-05-24 08:48:10 +0000505}
506
stefanctc5eb8a92011-11-23 09:13:48 +0000507int read_memmapped(struct flashchip *flash, uint8_t *buf, unsigned int start, int unsigned len)
hailfinger23060112009-05-08 12:49:03 +0000508{
hailfinger0f08b7a2009-06-16 08:55:44 +0000509 chip_readn(buf, flash->virtual_memory + start, len);
uwe8d342eb2011-07-28 08:13:25 +0000510
hailfinger23060112009-05-08 12:49:03 +0000511 return 0;
512}
513
hailfinger7b414742009-06-13 12:04:03 +0000514int min(int a, int b)
515{
516 return (a < b) ? a : b;
517}
518
hailfinger7af83692009-06-15 17:23:36 +0000519int max(int a, int b)
520{
521 return (a > b) ? a : b;
522}
523
hailfingeraec9c962009-10-31 01:53:09 +0000524int bitcount(unsigned long a)
525{
526 int i = 0;
527 for (; a != 0; a >>= 1)
528 if (a & 1)
529 i++;
530 return i;
531}
532
hailfingerf76cc322010-11-09 22:00:31 +0000533void tolower_string(char *str)
534{
535 for (; *str != '\0'; str++)
536 *str = (char)tolower((unsigned char)*str);
537}
538
hailfingera916b422009-06-01 02:08:58 +0000539char *strcat_realloc(char *dest, const char *src)
540{
541 dest = realloc(dest, strlen(dest) + strlen(src) + 1);
hailfinger1ef766d2010-07-06 09:55:48 +0000542 if (!dest) {
543 msg_gerr("Out of memory!\n");
hailfingera916b422009-06-01 02:08:58 +0000544 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000545 }
hailfingera916b422009-06-01 02:08:58 +0000546 strcat(dest, src);
547 return dest;
548}
549
hailfinger6e5a52a2009-11-24 18:27:10 +0000550/* This is a somewhat hacked function similar in some ways to strtok().
hailfinger1ef766d2010-07-06 09:55:48 +0000551 * It will look for needle with a subsequent '=' in haystack, return a copy of
552 * needle and remove everything from the first occurrence of needle to the next
553 * delimiter from haystack.
hailfinger6e5a52a2009-11-24 18:27:10 +0000554 */
stefanct52700282011-06-26 17:38:17 +0000555char *extract_param(char **haystack, const char *needle, const char *delim)
hailfinger6e5a52a2009-11-24 18:27:10 +0000556{
hailfinger1ef766d2010-07-06 09:55:48 +0000557 char *param_pos, *opt_pos, *rest;
558 char *opt = NULL;
559 int optlen;
hailfingerf4aaccc2010-04-28 15:22:14 +0000560 int needlelen;
hailfinger6e5a52a2009-11-24 18:27:10 +0000561
hailfingerf4aaccc2010-04-28 15:22:14 +0000562 needlelen = strlen(needle);
563 if (!needlelen) {
564 msg_gerr("%s: empty needle! Please report a bug at "
565 "flashrom@flashrom.org\n", __func__);
566 return NULL;
567 }
568 /* No programmer parameters given. */
569 if (*haystack == NULL)
570 return NULL;
hailfinger6e5a52a2009-11-24 18:27:10 +0000571 param_pos = strstr(*haystack, needle);
572 do {
573 if (!param_pos)
574 return NULL;
hailfinger1ef766d2010-07-06 09:55:48 +0000575 /* Needle followed by '='? */
576 if (param_pos[needlelen] == '=') {
Simon Glass8dc82732013-07-16 10:13:51 -0600577
hailfinger1ef766d2010-07-06 09:55:48 +0000578 /* Beginning of the string? */
579 if (param_pos == *haystack)
580 break;
581 /* After a delimiter? */
582 if (strchr(delim, *(param_pos - 1)))
583 break;
584 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000585 /* Continue searching. */
586 param_pos++;
587 param_pos = strstr(param_pos, needle);
588 } while (1);
uwe8d342eb2011-07-28 08:13:25 +0000589
hailfinger6e5a52a2009-11-24 18:27:10 +0000590 if (param_pos) {
hailfinger1ef766d2010-07-06 09:55:48 +0000591 /* Get the string after needle and '='. */
592 opt_pos = param_pos + needlelen + 1;
593 optlen = strcspn(opt_pos, delim);
594 /* Return an empty string if the parameter was empty. */
595 opt = malloc(optlen + 1);
596 if (!opt) {
snelsone42c3802010-05-07 20:09:04 +0000597 msg_gerr("Out of memory!\n");
hailfinger6e5a52a2009-11-24 18:27:10 +0000598 exit(1);
599 }
hailfinger1ef766d2010-07-06 09:55:48 +0000600 strncpy(opt, opt_pos, optlen);
601 opt[optlen] = '\0';
602 rest = opt_pos + optlen;
603 /* Skip all delimiters after the current parameter. */
604 rest += strspn(rest, delim);
605 memmove(param_pos, rest, strlen(rest) + 1);
606 /* We could shrink haystack, but the effort is not worth it. */
hailfinger6e5a52a2009-11-24 18:27:10 +0000607 }
hailfinger6e5a52a2009-11-24 18:27:10 +0000608
hailfinger1ef766d2010-07-06 09:55:48 +0000609 return opt;
hailfinger6e5a52a2009-11-24 18:27:10 +0000610}
611
stefanct52700282011-06-26 17:38:17 +0000612char *extract_programmer_param(const char *param_name)
hailfingerddeb4ac2010-07-08 10:13:37 +0000613{
614 return extract_param(&programmer_param, param_name, ",");
615}
616
stefancte1c5acf2011-07-04 07:27:17 +0000617/* Returns the number of well-defined erasers for a chip. */
618static unsigned int count_usable_erasers(const struct flashchip *flash)
stefanct569dbb62011-07-01 00:19:12 +0000619{
620 unsigned int usable_erasefunctions = 0;
621 int k;
622 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
623 if (!check_block_eraser(flash, k, 0))
624 usable_erasefunctions++;
625 }
626 return usable_erasefunctions;
627}
628
hailfinger7af83692009-06-15 17:23:36 +0000629/* start is an offset to the base address of the flash chip */
stefanctc5eb8a92011-11-23 09:13:48 +0000630int check_erased_range(struct flashchip *flash, unsigned int start, unsigned int len)
hailfinger7af83692009-06-15 17:23:36 +0000631{
632 int ret;
633 uint8_t *cmpbuf = malloc(len);
634
635 if (!cmpbuf) {
snelsone42c3802010-05-07 20:09:04 +0000636 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000637 exit(1);
638 }
Simon Glass4c214132013-07-16 10:09:28 -0600639 memset(cmpbuf, flash_erase_value(flash), len);
hailfinger7af83692009-06-15 17:23:36 +0000640 ret = verify_range(flash, cmpbuf, start, len, "ERASE");
641 free(cmpbuf);
642 return ret;
643}
644
Simon Glass4e305f42015-01-08 06:29:04 -0700645static int compare_chunk(uint8_t *readbuf, uint8_t *cmpbuf, unsigned int start,
646 unsigned int len, const char *message)
647{
648 int failcount = 0, i;
649
650 for (i = 0; i < len; i++) {
651 if (cmpbuf[i] != readbuf[i]) {
652 if (!failcount) {
653 msg_cerr("%s FAILED at 0x%08x! "
654 "Expected=0x%02x, Read=0x%02x,",
655 message, start + i,
656 cmpbuf[i], readbuf[i]);
657 }
658 failcount++;
659 }
660 }
661
662 return failcount;
663}
664
uwee15beb92010-08-08 17:01:18 +0000665/*
hailfinger7af3d192009-11-25 17:05:52 +0000666 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
uwe8d342eb2011-07-28 08:13:25 +0000667 * flash content at location start
hailfinger7af83692009-06-15 17:23:36 +0000668 * @start offset to the base address of the flash chip
669 * @len length of the verified area
670 * @message string to print in the "FAILED" message
671 * @return 0 for success, -1 for failure
672 */
stefanctc5eb8a92011-11-23 09:13:48 +0000673int verify_range(struct flashchip *flash, uint8_t *cmpbuf, unsigned int start, unsigned int len,
uwe8d342eb2011-07-28 08:13:25 +0000674 const char *message)
hailfinger7af83692009-06-15 17:23:36 +0000675{
hailfinger8cb6ece2010-11-16 17:21:58 +0000676 uint8_t *readbuf = malloc(len);
hailfingerb91c08c2011-08-15 19:54:20 +0000677 int ret = 0, failcount = 0;
hailfinger7af83692009-06-15 17:23:36 +0000678
679 if (!len)
680 goto out_free;
681
hailfingerb0f4d122009-06-24 08:20:45 +0000682 if (!flash->read) {
snelsone42c3802010-05-07 20:09:04 +0000683 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
hailfingerb0f4d122009-06-24 08:20:45 +0000684 return 1;
685 }
hailfinger7af83692009-06-15 17:23:36 +0000686 if (!readbuf) {
snelsone42c3802010-05-07 20:09:04 +0000687 msg_gerr("Could not allocate memory!\n");
hailfinger7af83692009-06-15 17:23:36 +0000688 exit(1);
689 }
690
691 if (start + len > flash->total_size * 1024) {
snelsone42c3802010-05-07 20:09:04 +0000692 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
hailfinger7af83692009-06-15 17:23:36 +0000693 " total_size 0x%x\n", __func__, start, len,
694 flash->total_size * 1024);
695 ret = -1;
696 goto out_free;
697 }
698 if (!message)
699 message = "VERIFY";
uwe8d342eb2011-07-28 08:13:25 +0000700
Simon Glass4e305f42015-01-08 06:29:04 -0700701 if (programmer_table[programmer].paranoid) {
702 unsigned int i, chunksize;
David Hendricks1ed1d352011-11-23 17:54:37 -0800703
Simon Glass4e305f42015-01-08 06:29:04 -0700704 /* limit chunksize in order to catch errors early */
705 for (i = 0, chunksize = 0; i < len; i += chunksize) {
706 int tmp;
David Hendricks1ed1d352011-11-23 17:54:37 -0800707
Simon Glass4e305f42015-01-08 06:29:04 -0700708 chunksize = min(flash->page_size, len - i);
709 tmp = flash->read(flash, readbuf + i, start + i, chunksize);
710 if (tmp) {
711 ret = tmp;
712 if (ignore_error(tmp))
713 continue;
714 else
715 goto out_free;
David Hendricks1ed1d352011-11-23 17:54:37 -0800716 }
Simon Glass4e305f42015-01-08 06:29:04 -0700717
718 failcount = compare_chunk(readbuf + i, cmpbuf + i, start + i,
719 chunksize, message);
720 if (failcount)
721 break;
David Hendricks1ed1d352011-11-23 17:54:37 -0800722 }
Simon Glass4e305f42015-01-08 06:29:04 -0700723 } else {
724 int tmp;
725
726 /* read as much as we can to reduce transaction overhead */
727 tmp = flash->read(flash, readbuf, start, len);
728 if (tmp && !ignore_error(tmp)) {
729 ret = tmp;
730 goto out_free;
731 }
732
733 failcount = compare_chunk(readbuf, cmpbuf, start, len, message);
hailfinger8cb6ece2010-11-16 17:21:58 +0000734 }
735
hailfinger5be6c0f2009-07-23 01:42:56 +0000736 if (failcount) {
snelsone42c3802010-05-07 20:09:04 +0000737 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
uwe8d342eb2011-07-28 08:13:25 +0000738 start, start + len - 1, failcount);
hailfinger5be6c0f2009-07-23 01:42:56 +0000739 ret = -1;
740 }
hailfinger7af83692009-06-15 17:23:36 +0000741
742out_free:
743 free(readbuf);
744 return ret;
745}
746
uwee15beb92010-08-08 17:01:18 +0000747/*
hailfingerb247c7a2010-03-08 00:42:32 +0000748 * Check if the buffer @have can be programmed to the content of @want without
749 * erasing. This is only possible if all chunks of size @gran are either kept
750 * as-is or changed from an all-ones state to any other state.
hailfingerb437e282010-11-04 01:04:27 +0000751 *
hailfingerb247c7a2010-03-08 00:42:32 +0000752 * The following write granularities (enum @gran) are known:
753 * - 1 bit. Each bit can be cleared individually.
754 * - 1 byte. A byte can be written once. Further writes to an already written
755 * byte cause the contents to be either undefined or to stay unchanged.
756 * - 128 bytes. If less than 128 bytes are written, the rest will be
757 * erased. Each write to a 128-byte region will trigger an automatic erase
758 * before anything is written. Very uncommon behaviour and unsupported by
759 * this function.
760 * - 256 bytes. If less than 256 bytes are written, the contents of the
761 * unwritten bytes are undefined.
hailfingerb437e282010-11-04 01:04:27 +0000762 * Warning: This function assumes that @have and @want point to naturally
763 * aligned regions.
hailfingerb247c7a2010-03-08 00:42:32 +0000764 *
765 * @have buffer with current content
766 * @want buffer with desired content
hailfingerb437e282010-11-04 01:04:27 +0000767 * @len length of the checked area
hailfingerb247c7a2010-03-08 00:42:32 +0000768 * @gran write granularity (enum, not count)
769 * @return 0 if no erase is needed, 1 otherwise
770 */
Simon Glass4c214132013-07-16 10:09:28 -0600771static int need_erase(struct flashchip *flash, uint8_t *have, uint8_t *want,
772 unsigned int len, enum write_granularity gran)
hailfingerb247c7a2010-03-08 00:42:32 +0000773{
hailfingerb91c08c2011-08-15 19:54:20 +0000774 int result = 0;
stefanctc5eb8a92011-11-23 09:13:48 +0000775 unsigned int i, j, limit;
Simon Glass4c214132013-07-16 10:09:28 -0600776 int erase_value = flash_erase_value(flash);
hailfingerb247c7a2010-03-08 00:42:32 +0000777
778 switch (gran) {
779 case write_gran_1bit:
780 for (i = 0; i < len; i++)
781 if ((have[i] & want[i]) != want[i]) {
782 result = 1;
783 break;
784 }
785 break;
786 case write_gran_1byte:
787 for (i = 0; i < len; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600788 if ((have[i] != want[i]) && (have[i] != erase_value)) {
hailfingerb247c7a2010-03-08 00:42:32 +0000789 result = 1;
790 break;
791 }
792 break;
793 case write_gran_256bytes:
794 for (j = 0; j < len / 256; j++) {
795 limit = min (256, len - j * 256);
uwef6f94d42010-03-13 17:28:29 +0000796 /* Are 'have' and 'want' identical? */
hailfingerb247c7a2010-03-08 00:42:32 +0000797 if (!memcmp(have + j * 256, want + j * 256, limit))
798 continue;
799 /* have needs to be in erased state. */
800 for (i = 0; i < limit; i++)
Simon Glass4c214132013-07-16 10:09:28 -0600801 if (have[j * 256 + i] != erase_value) {
hailfingerb247c7a2010-03-08 00:42:32 +0000802 result = 1;
803 break;
804 }
805 if (result)
806 break;
807 }
808 break;
hailfingerb437e282010-11-04 01:04:27 +0000809 default:
810 msg_cerr("%s: Unsupported granularity! Please report a bug at "
811 "flashrom@flashrom.org\n", __func__);
hailfingerb247c7a2010-03-08 00:42:32 +0000812 }
813 return result;
814}
815
hailfingerb437e282010-11-04 01:04:27 +0000816/**
817 * Check if the buffer @have needs to be programmed to get the content of @want.
818 * If yes, return 1 and fill in first_start with the start address of the
819 * write operation and first_len with the length of the first to-be-written
820 * chunk. If not, return 0 and leave first_start and first_len undefined.
821 *
822 * Warning: This function assumes that @have and @want point to naturally
823 * aligned regions.
824 *
825 * @have buffer with current content
826 * @want buffer with desired content
827 * @len length of the checked area
828 * @gran write granularity (enum, not count)
hailfinger90fcf9b2010-11-05 14:51:59 +0000829 * @first_start offset of the first byte which needs to be written (passed in
830 * value is increased by the offset of the first needed write
831 * relative to have/want or unchanged if no write is needed)
832 * @return length of the first contiguous area which needs to be written
833 * 0 if no write is needed
hailfingerb437e282010-11-04 01:04:27 +0000834 *
835 * FIXME: This function needs a parameter which tells it about coalescing
836 * in relation to the max write length of the programmer and the max write
837 * length of the chip.
838 */
stefanctc5eb8a92011-11-23 09:13:48 +0000839static unsigned int get_next_write(uint8_t *have, uint8_t *want, unsigned int len,
840 unsigned int *first_start,
841 enum write_granularity gran)
hailfingerb437e282010-11-04 01:04:27 +0000842{
stefanctc5eb8a92011-11-23 09:13:48 +0000843 int need_write = 0;
844 unsigned int rel_start = 0, first_len = 0;
845 unsigned int i, limit, stride;
hailfingerb437e282010-11-04 01:04:27 +0000846
hailfingerb437e282010-11-04 01:04:27 +0000847 switch (gran) {
848 case write_gran_1bit:
849 case write_gran_1byte:
hailfinger90fcf9b2010-11-05 14:51:59 +0000850 stride = 1;
hailfingerb437e282010-11-04 01:04:27 +0000851 break;
852 case write_gran_256bytes:
hailfinger90fcf9b2010-11-05 14:51:59 +0000853 stride = 256;
hailfingerb437e282010-11-04 01:04:27 +0000854 break;
855 default:
856 msg_cerr("%s: Unsupported granularity! Please report a bug at "
857 "flashrom@flashrom.org\n", __func__);
hailfinger90fcf9b2010-11-05 14:51:59 +0000858 /* Claim that no write was needed. A write with unknown
859 * granularity is too dangerous to try.
860 */
861 return 0;
hailfingerb437e282010-11-04 01:04:27 +0000862 }
hailfinger90fcf9b2010-11-05 14:51:59 +0000863 for (i = 0; i < len / stride; i++) {
864 limit = min(stride, len - i * stride);
865 /* Are 'have' and 'want' identical? */
866 if (memcmp(have + i * stride, want + i * stride, limit)) {
867 if (!need_write) {
868 /* First location where have and want differ. */
869 need_write = 1;
870 rel_start = i * stride;
871 }
872 } else {
873 if (need_write) {
874 /* First location where have and want
875 * do not differ anymore.
876 */
hailfinger90fcf9b2010-11-05 14:51:59 +0000877 break;
878 }
879 }
880 }
hailfingerffb7f382010-12-06 13:05:44 +0000881 if (need_write)
hailfinger90fcf9b2010-11-05 14:51:59 +0000882 first_len = min(i * stride - rel_start, len);
hailfingerb437e282010-11-04 01:04:27 +0000883 *first_start += rel_start;
hailfinger90fcf9b2010-11-05 14:51:59 +0000884 return first_len;
hailfingerb437e282010-11-04 01:04:27 +0000885}
886
hailfinger0c515352009-11-23 12:55:31 +0000887/* This function generates various test patterns useful for testing controller
888 * and chip communication as well as chip behaviour.
889 *
890 * If a byte can be written multiple times, each time keeping 0-bits at 0
891 * and changing 1-bits to 0 if the new value for that bit is 0, the effect
892 * is essentially an AND operation. That's also the reason why this function
893 * provides the result of AND between various patterns.
894 *
895 * Below is a list of patterns (and their block length).
896 * Pattern 0 is 05 15 25 35 45 55 65 75 85 95 a5 b5 c5 d5 e5 f5 (16 Bytes)
897 * Pattern 1 is 0a 1a 2a 3a 4a 5a 6a 7a 8a 9a aa ba ca da ea fa (16 Bytes)
898 * Pattern 2 is 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f (16 Bytes)
899 * Pattern 3 is a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af (16 Bytes)
900 * Pattern 4 is 00 10 20 30 40 50 60 70 80 90 a0 b0 c0 d0 e0 f0 (16 Bytes)
901 * Pattern 5 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f (16 Bytes)
902 * Pattern 6 is 00 (1 Byte)
903 * Pattern 7 is ff (1 Byte)
904 * Patterns 0-7 have a big-endian block number in the last 2 bytes of each 256
905 * byte block.
906 *
907 * Pattern 8 is 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11... (256 B)
908 * Pattern 9 is ff fe fd fc fb fa f9 f8 f7 f6 f5 f4 f3 f2 f1 f0 ef ee... (256 B)
909 * Pattern 10 is 00 00 00 01 00 02 00 03 00 04... (128 kB big-endian counter)
910 * Pattern 11 is ff ff ff fe ff fd ff fc ff fb... (128 kB big-endian downwards)
911 * Pattern 12 is 00 (1 Byte)
912 * Pattern 13 is ff (1 Byte)
913 * Patterns 8-13 have no block number.
914 *
915 * Patterns 0-3 are created to detect and efficiently diagnose communication
916 * slips like missed bits or bytes and their repetitive nature gives good visual
917 * cues to the person inspecting the results. In addition, the following holds:
918 * AND Pattern 0/1 == Pattern 4
919 * AND Pattern 2/3 == Pattern 5
920 * AND Pattern 0/1/2/3 == AND Pattern 4/5 == Pattern 6
921 * A weakness of pattern 0-5 is the inability to detect swaps/copies between
922 * any two 16-byte blocks except for the last 16-byte block in a 256-byte bloc.
923 * They work perfectly for detecting any swaps/aliasing of blocks >= 256 bytes.
924 * 0x5 and 0xa were picked because they are 0101 and 1010 binary.
925 * Patterns 8-9 are best for detecting swaps/aliasing of blocks < 256 bytes.
926 * Besides that, they provide for bit testing of the last two bytes of every
927 * 256 byte block which contains the block number for patterns 0-6.
928 * Patterns 10-11 are special purpose for detecting subblock aliasing with
929 * block sizes >256 bytes (some Dataflash chips etc.)
930 * AND Pattern 8/9 == Pattern 12
931 * AND Pattern 10/11 == Pattern 12
932 * Pattern 13 is the completely erased state.
933 * None of the patterns can detect aliasing at boundaries which are a multiple
934 * of 16 MBytes (but such chips do not exist anyway for Parallel/LPC/FWH/SPI).
935 */
936int generate_testpattern(uint8_t *buf, uint32_t size, int variant)
937{
938 int i;
939
940 if (!buf) {
snelsone42c3802010-05-07 20:09:04 +0000941 msg_gerr("Invalid buffer!\n");
hailfinger0c515352009-11-23 12:55:31 +0000942 return 1;
943 }
944
945 switch (variant) {
946 case 0:
947 for (i = 0; i < size; i++)
948 buf[i] = (i & 0xf) << 4 | 0x5;
949 break;
950 case 1:
951 for (i = 0; i < size; i++)
952 buf[i] = (i & 0xf) << 4 | 0xa;
953 break;
954 case 2:
955 for (i = 0; i < size; i++)
956 buf[i] = 0x50 | (i & 0xf);
957 break;
958 case 3:
959 for (i = 0; i < size; i++)
960 buf[i] = 0xa0 | (i & 0xf);
961 break;
962 case 4:
963 for (i = 0; i < size; i++)
964 buf[i] = (i & 0xf) << 4;
965 break;
966 case 5:
967 for (i = 0; i < size; i++)
968 buf[i] = i & 0xf;
969 break;
970 case 6:
971 memset(buf, 0x00, size);
972 break;
973 case 7:
974 memset(buf, 0xff, size);
975 break;
976 case 8:
977 for (i = 0; i < size; i++)
978 buf[i] = i & 0xff;
979 break;
980 case 9:
981 for (i = 0; i < size; i++)
982 buf[i] = ~(i & 0xff);
983 break;
984 case 10:
985 for (i = 0; i < size % 2; i++) {
986 buf[i * 2] = (i >> 8) & 0xff;
987 buf[i * 2 + 1] = i & 0xff;
988 }
989 if (size & 0x1)
990 buf[i * 2] = (i >> 8) & 0xff;
991 break;
992 case 11:
993 for (i = 0; i < size % 2; i++) {
994 buf[i * 2] = ~((i >> 8) & 0xff);
995 buf[i * 2 + 1] = ~(i & 0xff);
996 }
997 if (size & 0x1)
998 buf[i * 2] = ~((i >> 8) & 0xff);
999 break;
1000 case 12:
1001 memset(buf, 0x00, size);
1002 break;
1003 case 13:
1004 memset(buf, 0xff, size);
1005 break;
1006 }
1007
1008 if ((variant >= 0) && (variant <= 7)) {
1009 /* Write block number in the last two bytes of each 256-byte
1010 * block, big endian for easier reading of the hexdump.
1011 * Note that this wraps around for chips larger than 2^24 bytes
1012 * (16 MB).
1013 */
1014 for (i = 0; i < size / 256; i++) {
1015 buf[i * 256 + 254] = (i >> 8) & 0xff;
1016 buf[i * 256 + 255] = i & 0xff;
1017 }
1018 }
1019
1020 return 0;
1021}
1022
hailfingeraec9c962009-10-31 01:53:09 +00001023int check_max_decode(enum chipbustype buses, uint32_t size)
1024{
1025 int limitexceeded = 0;
uwe8d342eb2011-07-28 08:13:25 +00001026
1027 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001028 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001029 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001030 "size %u kB of chipset/board/programmer "
1031 "for %s interface, "
1032 "probe/read/erase/write may fail. ", size / 1024,
1033 max_rom_decode.parallel / 1024, "Parallel");
hailfingeraec9c962009-10-31 01:53:09 +00001034 }
hailfingere1e41ea2011-07-27 07:13:06 +00001035 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001036 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001037 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001038 "size %u kB of chipset/board/programmer "
1039 "for %s interface, "
1040 "probe/read/erase/write may fail. ", size / 1024,
1041 max_rom_decode.lpc / 1024, "LPC");
hailfingeraec9c962009-10-31 01:53:09 +00001042 }
hailfingere1e41ea2011-07-27 07:13:06 +00001043 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001044 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001045 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001046 "size %u kB of chipset/board/programmer "
1047 "for %s interface, "
1048 "probe/read/erase/write may fail. ", size / 1024,
1049 max_rom_decode.fwh / 1024, "FWH");
hailfingeraec9c962009-10-31 01:53:09 +00001050 }
hailfingere1e41ea2011-07-27 07:13:06 +00001051 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
hailfingeraec9c962009-10-31 01:53:09 +00001052 limitexceeded++;
snelsone42c3802010-05-07 20:09:04 +00001053 msg_pdbg("Chip size %u kB is bigger than supported "
uwe8d342eb2011-07-28 08:13:25 +00001054 "size %u kB of chipset/board/programmer "
1055 "for %s interface, "
1056 "probe/read/erase/write may fail. ", size / 1024,
1057 max_rom_decode.spi / 1024, "SPI");
hailfingeraec9c962009-10-31 01:53:09 +00001058 }
1059 if (!limitexceeded)
1060 return 0;
1061 /* Sometimes chip and programmer have more than one bus in common,
1062 * and the limit is not exceeded on all buses. Tell the user.
1063 */
1064 if (bitcount(buses) > limitexceeded)
hailfinger92cd8e32010-01-07 03:24:05 +00001065 /* FIXME: This message is designed towards CLI users. */
snelsone42c3802010-05-07 20:09:04 +00001066 msg_pdbg("There is at least one common chip/programmer "
uwe8d342eb2011-07-28 08:13:25 +00001067 "interface which can support a chip of this size. "
1068 "You can try --force at your own risk.\n");
hailfingeraec9c962009-10-31 01:53:09 +00001069 return 1;
1070}
1071
hailfinger48ed3e22011-05-04 00:39:50 +00001072int probe_flash(int startchip, struct flashchip *fill_flash, int force)
rminnich8d3ff912003-10-25 17:01:29 +00001073{
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301074 const struct flashchip *flash, *flash_list;
hailfingeraec9c962009-10-31 01:53:09 +00001075 unsigned long base = 0;
stepan3e7aeae2011-01-19 06:21:54 +00001076 char location[64];
hailfingeraec9c962009-10-31 01:53:09 +00001077 uint32_t size;
1078 enum chipbustype buses_common;
hailfingera916b422009-06-01 02:08:58 +00001079 char *tmp;
rminnich8d3ff912003-10-25 17:01:29 +00001080
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301081 /* Based on the host controller interface that a platform
1082 * needs to use (hwseq or swseq),
1083 * set the flashchips list here.
1084 */
1085 switch (ich_generation) {
1086 case CHIPSET_100_SERIES_SUNRISE_POINT:
1087 flash_list = flashchips_hwseq;
1088 break;
1089 default:
1090 flash_list = flashchips;
1091 break;
1092 }
1093
1094 for (flash = flash_list + startchip; flash && flash->name; flash++) {
stugec1e55fe2008-07-02 17:15:47 +00001095 if (chip_to_probe && strcmp(flash->name, chip_to_probe) != 0)
ollie5672ac62004-03-17 22:22:08 +00001096 continue;
hailfingeraec9c962009-10-31 01:53:09 +00001097 buses_common = buses_supported & flash->bustype;
1098 if (!buses_common) {
hailfinger18bd4cc2011-06-17 22:38:53 +00001099 msg_gspew("Probing for %s %s, %d kB: skipped. ",
1100 flash->vendor, flash->name, flash->total_size);
hailfingera916b422009-06-01 02:08:58 +00001101 tmp = flashbuses_to_text(buses_supported);
hailfinger18bd4cc2011-06-17 22:38:53 +00001102 msg_gspew("Host bus type %s ", tmp);
hailfingera916b422009-06-01 02:08:58 +00001103 free(tmp);
1104 tmp = flashbuses_to_text(flash->bustype);
hailfinger327d2522010-03-22 23:43:51 +00001105 msg_gspew("and chip bus type %s are incompatible.",
1106 tmp);
hailfingera916b422009-06-01 02:08:58 +00001107 free(tmp);
hailfinger18bd4cc2011-06-17 22:38:53 +00001108 msg_gspew("\n");
1109 continue;
1110 }
1111 msg_gdbg("Probing for %s %s, %d kB: ",
1112 flash->vendor, flash->name, flash->total_size);
1113 if (!flash->probe && !force) {
1114 msg_gdbg("failed! flashrom has no probe function for "
1115 "this flash chip.\n");
hailfingera916b422009-06-01 02:08:58 +00001116 continue;
1117 }
stepan782fb172007-04-06 11:58:03 +00001118
ollie5672ac62004-03-17 22:22:08 +00001119 size = flash->total_size * 1024;
hailfingeraec9c962009-10-31 01:53:09 +00001120 check_max_decode(buses_common, size);
stepan782fb172007-04-06 11:58:03 +00001121
hailfinger48ed3e22011-05-04 00:39:50 +00001122 /* Start filling in the dynamic data. */
1123 *fill_flash = *flash;
1124
hailfinger72d3b982009-05-09 07:27:23 +00001125 base = flashbase ? flashbase : (0xffffffff - size + 1);
hailfinger48ed3e22011-05-04 00:39:50 +00001126 fill_flash->virtual_memory = (chipaddr)programmer_map_flash_region("flash chip", base, size);
rminnich8d3ff912003-10-25 17:01:29 +00001127
stugec1e55fe2008-07-02 17:15:47 +00001128 if (force)
1129 break;
stepanc98b80b2006-03-16 16:57:41 +00001130
hailfinger48ed3e22011-05-04 00:39:50 +00001131 if (fill_flash->probe(fill_flash) != 1)
stuge56300c32008-09-03 23:10:05 +00001132 goto notfound;
1133
hailfinger48ed3e22011-05-04 00:39:50 +00001134 /* If this is the first chip found, accept it.
1135 * If this is not the first chip found, accept it only if it is
1136 * a non-generic match.
1137 * We could either make chipcount global or provide it as
1138 * parameter, or we assume that startchip==0 means this call to
1139 * probe_flash() is the first one and thus no chip has been
1140 * found before.
1141 */
1142 if (startchip == 0 || fill_flash->model_id != GENERIC_DEVICE_ID)
stugec1e55fe2008-07-02 17:15:47 +00001143 break;
1144
stuge56300c32008-09-03 23:10:05 +00001145notfound:
hailfinger48ed3e22011-05-04 00:39:50 +00001146 programmer_unmap_flash_region((void *)fill_flash->virtual_memory, size);
rminnich8d3ff912003-10-25 17:01:29 +00001147 }
uwebe4477b2007-08-23 16:08:21 +00001148
stugec1e55fe2008-07-02 17:15:47 +00001149 if (!flash || !flash->name)
hailfinger48ed3e22011-05-04 00:39:50 +00001150 return -1;
stugec1e55fe2008-07-02 17:15:47 +00001151
hailfingere11396b2011-03-08 00:09:11 +00001152#if CONFIG_INTERNAL == 1
1153 if (programmer_table[programmer].map_flash_region == physmap)
stepan3e7aeae2011-01-19 06:21:54 +00001154 snprintf(location, sizeof(location), "at physical address 0x%lx", base);
hailfingere11396b2011-03-08 00:09:11 +00001155 else
1156#endif
stepan3e7aeae2011-01-19 06:21:54 +00001157 snprintf(location, sizeof(location), "on %s", programmer_table[programmer].name);
stepan3e7aeae2011-01-19 06:21:54 +00001158
stefanct588b6d22011-06-26 20:45:35 +00001159 tmp = flashbuses_to_text(flash->bustype);
stefanctdfd58832011-07-25 20:38:52 +00001160 msg_cdbg("%s %s flash chip \"%s\" (%d kB, %s) %s.\n",
1161 force ? "Assuming" : "Found", fill_flash->vendor,
1162 fill_flash->name, fill_flash->total_size, tmp, location);
stefanct588b6d22011-06-26 20:45:35 +00001163 free(tmp);
uwe9e6811e2009-06-28 21:47:57 +00001164
hailfinger0f4c3952010-12-02 21:59:42 +00001165 /* Flash registers will not be mapped if the chip was forced. Lock info
1166 * may be stored in registers, so avoid lock info printing.
1167 */
1168 if (!force)
hailfinger48ed3e22011-05-04 00:39:50 +00001169 if (fill_flash->printlock)
1170 fill_flash->printlock(fill_flash);
snelson1ee293c2010-02-19 00:52:10 +00001171
hailfinger48ed3e22011-05-04 00:39:50 +00001172 /* Return position of matching chip. */
Ramya Vijaykumare6a7ca82015-05-12 14:27:29 +05301173 return flash - flash_list;
rminnich8d3ff912003-10-25 17:01:29 +00001174}
1175
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001176int verify_flash(struct flashchip *flash, uint8_t *buf, int verify_it)
rminnich8d3ff912003-10-25 17:01:29 +00001177{
hailfingerb0f4d122009-06-24 08:20:45 +00001178 int ret;
stefanctc5eb8a92011-11-23 09:13:48 +00001179 unsigned int total_size = flash->total_size * 1024;
rminnich8d3ff912003-10-25 17:01:29 +00001180
snelsone42c3802010-05-07 20:09:04 +00001181 msg_cinfo("Verifying flash... ");
uwef6641642007-05-09 10:17:44 +00001182
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08001183 if (specified_partition() && verify_it == VERIFY_PARTIAL) {
1184 ret = handle_partial_verify(flash, buf, verify_range);
1185 } else {
1186 ret = verify_range(flash, buf, 0, total_size, NULL);
1187 }
uwef6641642007-05-09 10:17:44 +00001188
David Hendricks1ed1d352011-11-23 17:54:37 -08001189 if (ret == ACCESS_DENIED) {
1190 msg_gdbg("Could not fully verify due to access error, ");
1191 if (access_denied_action == error_ignore) {
1192 msg_gdbg("ignoring\n");
1193 ret = 0;
1194 } else {
1195 msg_gdbg("aborting\n");
1196 }
1197 }
1198
hailfingerb0f4d122009-06-24 08:20:45 +00001199 if (!ret)
snelsone42c3802010-05-07 20:09:04 +00001200 msg_cinfo("VERIFIED. \n");
stepanc98b80b2006-03-16 16:57:41 +00001201
hailfingerb0f4d122009-06-24 08:20:45 +00001202 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00001203}
1204
uwe8d342eb2011-07-28 08:13:25 +00001205int read_buf_from_file(unsigned char *buf, unsigned long size,
1206 const char *filename)
hailfinger771fc182010-10-15 00:01:14 +00001207{
1208 unsigned long numbytes;
1209 FILE *image;
1210 struct stat image_stat;
1211
Vincent Palatin7ab23932014-10-01 12:09:16 -07001212 if (!strncmp(filename, "-", sizeof("-")))
1213 image = fdopen(STDIN_FILENO, "rb");
1214 else
1215 image = fopen(filename, "rb");
1216 if (image == NULL) {
hailfinger771fc182010-10-15 00:01:14 +00001217 perror(filename);
1218 return 1;
1219 }
1220 if (fstat(fileno(image), &image_stat) != 0) {
1221 perror(filename);
1222 fclose(image);
1223 return 1;
1224 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001225 if ((image_stat.st_size != size) &&
1226 (strncmp(filename, "-", sizeof("-")))) {
Gwendal Grignou94e87d62014-11-25 15:34:15 -08001227 msg_gerr("Error: Image size doesn't match: stat %ld bytes, "
1228 "wanted %ld!\n", image_stat.st_size, size);
hailfinger771fc182010-10-15 00:01:14 +00001229 fclose(image);
1230 return 1;
1231 }
1232 numbytes = fread(buf, 1, size, image);
1233 if (fclose(image)) {
1234 perror(filename);
1235 return 1;
1236 }
1237 if (numbytes != size) {
1238 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
1239 "wanted %ld!\n", numbytes, size);
1240 return 1;
1241 }
1242 return 0;
1243}
1244
uwe8d342eb2011-07-28 08:13:25 +00001245int write_buf_to_file(unsigned char *buf, unsigned long size,
1246 const char *filename)
hailfingerd219a232009-01-28 00:27:54 +00001247{
1248 unsigned long numbytes;
1249 FILE *image;
hailfingerde345862009-06-01 22:07:52 +00001250
1251 if (!filename) {
hailfinger42a850a2010-07-13 23:56:13 +00001252 msg_gerr("No filename specified.\n");
hailfingerde345862009-06-01 22:07:52 +00001253 return 1;
1254 }
Vincent Palatin7ab23932014-10-01 12:09:16 -07001255 if (!strncmp(filename, "-", sizeof("-")))
1256 image = fdopen(STDOUT_FILENO, "wb");
1257 else
1258 image = fopen(filename, "wb");
1259 if (image == NULL) {
hailfingerd219a232009-01-28 00:27:54 +00001260 perror(filename);
hailfinger23060112009-05-08 12:49:03 +00001261 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001262 }
hailfingerd219a232009-01-28 00:27:54 +00001263
hailfingerd219a232009-01-28 00:27:54 +00001264 numbytes = fwrite(buf, 1, size, image);
1265 fclose(image);
hailfinger42a850a2010-07-13 23:56:13 +00001266 if (numbytes != size) {
1267 msg_gerr("File %s could not be written completely.\n",
1268 filename);
hailfingerd219a232009-01-28 00:27:54 +00001269 return 1;
hailfinger42a850a2010-07-13 23:56:13 +00001270 }
hailfingerd219a232009-01-28 00:27:54 +00001271 return 0;
1272}
1273
David Hendrickse3451942013-03-21 17:23:29 -07001274/*
1275 * read_flash - wrapper for flash->read() with additional high-level policy
1276 *
1277 * @flash flash chip
1278 * @buf buffer to store data in
1279 * @start start address
1280 * @len number of bytes to read
1281 *
1282 * This wrapper simplifies most cases when the flash chip needs to be read
1283 * since policy decisions such as non-fatal error handling is centralized.
1284 */
1285int read_flash(struct flashchip *flash, uint8_t *buf,
1286 unsigned int start, unsigned int len)
1287{
David Hendricks4e76fdc2013-05-13 16:05:36 -07001288 int ret;
David Hendrickse3451942013-03-21 17:23:29 -07001289
1290 if (!flash || !flash->read)
1291 return -1;
1292
David Hendricks4e76fdc2013-05-13 16:05:36 -07001293 ret = flash->read(flash, buf, start, len);
David Hendrickse3451942013-03-21 17:23:29 -07001294 if (ret) {
1295 if (ignore_error(ret)) {
1296 msg_gdbg("ignoring error when reading 0x%x-0x%x\n",
1297 start, start + len - 1);
1298 ret = 0;
1299 } else {
1300 msg_gdbg("failed to read 0x%x-0x%x\n",
1301 start, start + len - 1);
1302 }
1303 }
1304
1305 return ret;
1306}
1307
David Hendricks7c8a1612013-04-26 19:14:44 -07001308/*
1309 * write_flash - wrapper for flash->write() with additional high-level policy
1310 *
1311 * @flash flash chip
1312 * @buf buffer to write to flash
1313 * @start start address in flash
1314 * @len number of bytes to write
1315 *
1316 * TODO: Look up regions that are write-protected and avoid attempt to write
1317 * to them at all.
1318 */
1319int write_flash(struct flashchip *flash, uint8_t *buf,
1320 unsigned int start, unsigned int len)
1321{
1322 if (!flash || !flash->write)
1323 return -1;
1324
1325 return flash->write(flash, buf, start, len);
1326}
1327
stefanct52700282011-06-26 17:38:17 +00001328int read_flash_to_file(struct flashchip *flash, const char *filename)
hailfinger42a850a2010-07-13 23:56:13 +00001329{
1330 unsigned long size = flash->total_size * 1024;
1331 unsigned char *buf = calloc(size, sizeof(char));
1332 int ret = 0;
1333
1334 msg_cinfo("Reading flash... ");
1335 if (!buf) {
1336 msg_gerr("Memory allocation failed!\n");
1337 msg_cinfo("FAILED.\n");
1338 return 1;
1339 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001340
1341 /* To support partial read, fill buffer to all 0xFF at beginning to make
1342 * debug easier. */
Simon Glass4c214132013-07-16 10:09:28 -06001343 memset(buf, flash_erase_value(flash), size);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001344
hailfinger42a850a2010-07-13 23:56:13 +00001345 if (!flash->read) {
1346 msg_cerr("No read function available for this flash chip.\n");
1347 ret = 1;
1348 goto out_free;
1349 }
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001350
1351 /* First try to handle partial read case, rather than read the whole
1352 * flash, which is slow. */
David Hendrickse3451942013-03-21 17:23:29 -07001353 ret = handle_partial_read(flash, buf, read_flash, 1);
Louis Yung-Chieh Lo9c7525f2011-03-04 12:32:02 +08001354 if (ret < 0) {
1355 msg_cerr("Partial read operation failed!\n");
1356 ret = 1;
1357 goto out_free;
1358 } else if (ret > 0) {
David Hendricksdf29a832013-06-28 14:33:51 -07001359 int num_regions = get_num_include_args();
1360
1361 if (ret != num_regions) {
1362 msg_cerr("Requested %d regions, but only read %d\n",
1363 num_regions, ret);
1364 ret = 1;
1365 goto out_free;
1366 }
1367
1368 ret = 0;
David Hendricks1ed1d352011-11-23 17:54:37 -08001369 } else {
David Hendrickse3451942013-03-21 17:23:29 -07001370 if (read_flash(flash, buf, 0, size)) {
David Hendricks1ed1d352011-11-23 17:54:37 -08001371 msg_cerr("Read operation failed!\n");
1372 ret = 1;
1373 goto out_free;
1374 }
hailfinger42a850a2010-07-13 23:56:13 +00001375 }
1376
David Hendricksdf29a832013-06-28 14:33:51 -07001377 if (filename)
1378 ret = write_buf_to_file(buf, size, filename);
1379
hailfinger42a850a2010-07-13 23:56:13 +00001380out_free:
1381 free(buf);
David Hendricksc6c9f822010-11-03 15:07:01 -07001382 if (ret)
1383 msg_cerr("FAILED.");
1384 else
1385 msg_cdbg("done.");
hailfinger42a850a2010-07-13 23:56:13 +00001386 return ret;
1387}
1388
hailfingerb437e282010-11-04 01:04:27 +00001389/* This function shares a lot of its structure with erase_and_write_flash() and
1390 * walk_eraseregions().
hailfinger9fed35d2010-01-19 06:42:46 +00001391 * Even if an error is found, the function will keep going and check the rest.
1392 */
hailfinger48ed3e22011-05-04 00:39:50 +00001393static int selfcheck_eraseblocks(const struct flashchip *flash)
hailfinger45177872010-01-18 08:14:43 +00001394{
hailfingerb91c08c2011-08-15 19:54:20 +00001395 int i, j, k;
1396 int ret = 0;
hailfinger45177872010-01-18 08:14:43 +00001397
1398 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1399 unsigned int done = 0;
1400 struct block_eraser eraser = flash->block_erasers[k];
1401
1402 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1403 /* Blocks with zero size are bugs in flashchips.c. */
1404 if (eraser.eraseblocks[i].count &&
1405 !eraser.eraseblocks[i].size) {
1406 msg_gerr("ERROR: Flash chip %s erase function "
1407 "%i region %i has size 0. Please report"
1408 " a bug at flashrom@flashrom.org\n",
1409 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001410 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001411 }
1412 /* Blocks with zero count are bugs in flashchips.c. */
1413 if (!eraser.eraseblocks[i].count &&
1414 eraser.eraseblocks[i].size) {
1415 msg_gerr("ERROR: Flash chip %s erase function "
1416 "%i region %i has count 0. Please report"
1417 " a bug at flashrom@flashrom.org\n",
1418 flash->name, k, i);
hailfinger9fed35d2010-01-19 06:42:46 +00001419 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001420 }
1421 done += eraser.eraseblocks[i].count *
1422 eraser.eraseblocks[i].size;
1423 }
hailfinger9fed35d2010-01-19 06:42:46 +00001424 /* Empty eraseblock definition with erase function. */
1425 if (!done && eraser.block_erase)
snelsone42c3802010-05-07 20:09:04 +00001426 msg_gspew("Strange: Empty eraseblock definition with "
uwe8d342eb2011-07-28 08:13:25 +00001427 "non-empty erase function. Not an error.\n");
hailfinger45177872010-01-18 08:14:43 +00001428 if (!done)
1429 continue;
1430 if (done != flash->total_size * 1024) {
1431 msg_gerr("ERROR: Flash chip %s erase function %i "
1432 "region walking resulted in 0x%06x bytes total,"
1433 " expected 0x%06x bytes. Please report a bug at"
1434 " flashrom@flashrom.org\n", flash->name, k,
1435 done, flash->total_size * 1024);
hailfinger9fed35d2010-01-19 06:42:46 +00001436 ret = 1;
hailfinger45177872010-01-18 08:14:43 +00001437 }
hailfinger9fed35d2010-01-19 06:42:46 +00001438 if (!eraser.block_erase)
1439 continue;
1440 /* Check if there are identical erase functions for different
1441 * layouts. That would imply "magic" erase functions. The
1442 * easiest way to check this is with function pointers.
1443 */
uwef6f94d42010-03-13 17:28:29 +00001444 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
hailfinger9fed35d2010-01-19 06:42:46 +00001445 if (eraser.block_erase ==
1446 flash->block_erasers[j].block_erase) {
1447 msg_gerr("ERROR: Flash chip %s erase function "
1448 "%i and %i are identical. Please report"
1449 " a bug at flashrom@flashrom.org\n",
1450 flash->name, k, j);
1451 ret = 1;
1452 }
uwef6f94d42010-03-13 17:28:29 +00001453 }
hailfinger45177872010-01-18 08:14:43 +00001454 }
hailfinger9fed35d2010-01-19 06:42:46 +00001455 return ret;
hailfinger45177872010-01-18 08:14:43 +00001456}
1457
hailfingerb437e282010-11-04 01:04:27 +00001458static int erase_and_write_block_helper(struct flashchip *flash,
1459 unsigned int start, unsigned int len,
hailfinger90fcf9b2010-11-05 14:51:59 +00001460 uint8_t *curcontents,
hailfingerb437e282010-11-04 01:04:27 +00001461 uint8_t *newcontents,
1462 int (*erasefn) (struct flashchip *flash,
1463 unsigned int addr,
1464 unsigned int len))
1465{
stefanctc5eb8a92011-11-23 09:13:48 +00001466 unsigned int starthere = 0, lenhere = 0;
1467 int ret = 0, skip = 1, writecount = 0;
hailfingerb437e282010-11-04 01:04:27 +00001468 enum write_granularity gran = write_gran_256bytes; /* FIXME */
1469
hailfinger90fcf9b2010-11-05 14:51:59 +00001470 /* curcontents and newcontents are opaque to walk_eraseregions, and
hailfingerb437e282010-11-04 01:04:27 +00001471 * need to be adjusted here to keep the impression of proper abstraction
1472 */
hailfinger90fcf9b2010-11-05 14:51:59 +00001473 curcontents += start;
hailfingerb437e282010-11-04 01:04:27 +00001474 newcontents += start;
1475 msg_cdbg(":");
1476 /* FIXME: Assume 256 byte granularity for now to play it safe. */
Simon Glass4c214132013-07-16 10:09:28 -06001477 if (need_erase(flash, curcontents, newcontents, len, gran)) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001478 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001479 msg_cdbg("E");
1480 ret = erasefn(flash, start, len);
David Hendricks1ed1d352011-11-23 17:54:37 -08001481 if (ret) {
1482 if (ret == ACCESS_DENIED)
1483 msg_cdbg("D");
1484 else
1485 msg_cerr("ERASE FAILED!\n");
hailfingerb437e282010-11-04 01:04:27 +00001486 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001487 }
1488
David Hendricks0954ffc2015-11-13 15:15:44 -08001489 if (programmer_table[programmer].paranoid) {
1490 if (check_erased_range(flash, start, len)) {
1491 msg_cerr("ERASE FAILED!\n");
1492 return -1;
1493 }
hailfingerac8e3182011-06-26 17:04:16 +00001494 }
David Hendricks0954ffc2015-11-13 15:15:44 -08001495
hailfinger90fcf9b2010-11-05 14:51:59 +00001496 /* Erase was successful. Adjust curcontents. */
Simon Glass4c214132013-07-16 10:09:28 -06001497 memset(curcontents, flash_erase_value(flash), len);
hailfingerb437e282010-11-04 01:04:27 +00001498 skip = 0;
1499 }
hailfinger90fcf9b2010-11-05 14:51:59 +00001500 /* get_next_write() sets starthere to a new value after the call. */
1501 while ((lenhere = get_next_write(curcontents + starthere,
1502 newcontents + starthere,
1503 len - starthere, &starthere, gran))) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07001504 content_has_changed |= 1;
hailfingerb437e282010-11-04 01:04:27 +00001505 if (!writecount++)
1506 msg_cdbg("W");
1507 /* Needs the partial write function signature. */
David Hendricks7c8a1612013-04-26 19:14:44 -07001508 ret = write_flash(flash, newcontents + starthere,
hailfingerb437e282010-11-04 01:04:27 +00001509 start + starthere, lenhere);
David Hendricks1ed1d352011-11-23 17:54:37 -08001510 if (ret) {
1511 if (ret == ACCESS_DENIED)
1512 msg_cdbg("D");
hailfingerb437e282010-11-04 01:04:27 +00001513 return ret;
David Hendricks1ed1d352011-11-23 17:54:37 -08001514 }
hailfingerb437e282010-11-04 01:04:27 +00001515 starthere += lenhere;
1516 skip = 0;
1517 }
1518 if (skip)
1519 msg_cdbg("S");
1520 return ret;
1521}
1522
hailfinger83541b32010-07-13 00:42:00 +00001523static int walk_eraseregions(struct flashchip *flash, int erasefunction,
1524 int (*do_something) (struct flashchip *flash,
1525 unsigned int addr,
hailfingerb437e282010-11-04 01:04:27 +00001526 unsigned int len,
1527 uint8_t *param1,
1528 uint8_t *param2,
1529 int (*erasefn) (
1530 struct flashchip *flash,
1531 unsigned int addr,
1532 unsigned int len)),
1533 void *param1, void *param2)
hailfinger2b8c9382010-07-13 00:37:19 +00001534{
David Hendricks605544b2015-08-15 16:32:58 -07001535 int i, j, rc = -1;
hailfingerb91c08c2011-08-15 19:54:20 +00001536 unsigned int start = 0;
1537 unsigned int len;
hailfinger2b8c9382010-07-13 00:37:19 +00001538 struct block_eraser eraser = flash->block_erasers[erasefunction];
uwe8d342eb2011-07-28 08:13:25 +00001539
hailfinger2b8c9382010-07-13 00:37:19 +00001540 for (i = 0; i < NUM_ERASEREGIONS; i++) {
David Hendricks605544b2015-08-15 16:32:58 -07001541 /* count==0 and size==0 for all automatically initialized array
hailfinger2b8c9382010-07-13 00:37:19 +00001542 * members so the loop below won't be executed for them.
1543 */
1544 len = eraser.eraseblocks[i].size;
David Hendricks605544b2015-08-15 16:32:58 -07001545 if (!len)
1546 continue;
1547
1548 if (required_erase_size && (len != required_erase_size)) {
1549 msg_cdbg("%u does not meet erase alignment", len);
1550 rc = -1;
1551 break;
1552 }
1553
hailfinger2b8c9382010-07-13 00:37:19 +00001554 for (j = 0; j < eraser.eraseblocks[i].count; j++) {
hailfingerb437e282010-11-04 01:04:27 +00001555 /* Print this for every block except the first one. */
1556 if (i || j)
1557 msg_cdbg(", ");
1558 msg_cdbg("0x%06x-0x%06x", start,
hailfinger2b8c9382010-07-13 00:37:19 +00001559 start + len - 1);
David Hendricks1ed1d352011-11-23 17:54:37 -08001560 rc = do_something(flash, start, len, param1, param2,
1561 eraser.block_erase);
1562 if (rc) {
1563 if (ignore_error(rc))
1564 rc = 0;
1565 else
1566 return rc;
hailfingerb437e282010-11-04 01:04:27 +00001567 }
hailfinger2b8c9382010-07-13 00:37:19 +00001568 start += len;
1569 }
1570 }
hailfingerb437e282010-11-04 01:04:27 +00001571 msg_cdbg("\n");
David Hendricks1ed1d352011-11-23 17:54:37 -08001572 return rc;
hailfinger2b8c9382010-07-13 00:37:19 +00001573}
1574
stefanct569dbb62011-07-01 00:19:12 +00001575static int check_block_eraser(const struct flashchip *flash, int k, int log)
hailfingercf848f12010-12-05 15:14:44 +00001576{
1577 struct block_eraser eraser = flash->block_erasers[k];
1578
1579 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1580 if (log)
1581 msg_cdbg("not defined. ");
1582 return 1;
1583 }
1584 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1585 if (log)
1586 msg_cdbg("eraseblock layout is known, but matching "
stefanct9e6b98a2011-05-28 02:37:14 +00001587 "block erase function is not implemented. ");
hailfingercf848f12010-12-05 15:14:44 +00001588 return 1;
1589 }
1590 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1591 if (log)
1592 msg_cdbg("block erase function found, but "
stefanct9e6b98a2011-05-28 02:37:14 +00001593 "eraseblock layout is not defined. ");
hailfingercf848f12010-12-05 15:14:44 +00001594 return 1;
1595 }
1596 return 0;
1597}
1598
uwe8d342eb2011-07-28 08:13:25 +00001599int erase_and_write_flash(struct flashchip *flash, uint8_t *oldcontents,
1600 uint8_t *newcontents)
hailfingerd219a232009-01-28 00:27:54 +00001601{
hailfinger8f524a82011-07-21 21:21:04 +00001602 int k, ret = 1;
hailfingerb437e282010-11-04 01:04:27 +00001603 uint8_t *curcontents;
1604 unsigned long size = flash->total_size * 1024;
stefancte1c5acf2011-07-04 07:27:17 +00001605 unsigned int usable_erasefunctions = count_usable_erasers(flash);
hailfingercf848f12010-12-05 15:14:44 +00001606
hailfingercf848f12010-12-05 15:14:44 +00001607 msg_cinfo("Erasing and writing flash chip... ");
stefanctd611e8f2011-07-12 22:35:21 +00001608 curcontents = malloc(size);
1609 if (!curcontents) {
1610 msg_gerr("Out of memory!\n");
1611 exit(1);
1612 }
hailfingerb437e282010-11-04 01:04:27 +00001613 /* Copy oldcontents to curcontents to avoid clobbering oldcontents. */
1614 memcpy(curcontents, oldcontents, size);
1615
hailfinger7df21362009-09-05 02:30:58 +00001616 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
stefanctf0bc4712011-07-26 14:28:35 +00001617 if (k != 0)
1618 msg_cdbg("Looking for another erase function.\n");
hailfinger8f524a82011-07-21 21:21:04 +00001619 if (!usable_erasefunctions) {
1620 msg_cdbg("No usable erase functions left.\n");
1621 break;
1622 }
stefanctf0bc4712011-07-26 14:28:35 +00001623 msg_cdbg("Trying erase function %i... ", k);
1624 if (check_block_eraser(flash, k, 1))
hailfinger7df21362009-09-05 02:30:58 +00001625 continue;
hailfingercf848f12010-12-05 15:14:44 +00001626 usable_erasefunctions--;
stefanctf0bc4712011-07-26 14:28:35 +00001627 ret = walk_eraseregions(flash, k, &erase_and_write_block_helper,
1628 curcontents, newcontents);
hailfinger7df21362009-09-05 02:30:58 +00001629 /* If everything is OK, don't try another erase function. */
1630 if (!ret)
1631 break;
hailfinger6237f5e2010-12-02 02:41:55 +00001632 /* Write/erase failed, so try to find out what the current chip
hailfinger8f524a82011-07-21 21:21:04 +00001633 * contents are. If no usable erase functions remain, we can
1634 * skip this: the next iteration will break immediately anyway.
hailfingerb437e282010-11-04 01:04:27 +00001635 */
hailfingercf848f12010-12-05 15:14:44 +00001636 if (!usable_erasefunctions)
1637 continue;
stefanctf0bc4712011-07-26 14:28:35 +00001638 /* Reading the whole chip may take a while, inform the user even
1639 * in non-verbose mode.
1640 */
1641 msg_cinfo("Reading current flash chip contents... ");
David Hendrickse3451942013-03-21 17:23:29 -07001642 if (read_flash(flash, curcontents, 0, size)) {
hailfinger6237f5e2010-12-02 02:41:55 +00001643 /* Now we are truly screwed. Read failed as well. */
stefanctf0bc4712011-07-26 14:28:35 +00001644 msg_cerr("Can't read anymore! Aborting.\n");
hailfinger6237f5e2010-12-02 02:41:55 +00001645 /* We have no idea about the flash chip contents, so
1646 * retrying with another erase function is pointless.
1647 */
1648 break;
1649 }
David Hendricks89a45e52011-11-22 16:56:22 -08001650 msg_cdbg("done. ");
hailfinger7df21362009-09-05 02:30:58 +00001651 }
hailfingerb437e282010-11-04 01:04:27 +00001652 /* Free the scratchpad. */
1653 free(curcontents);
hailfinger1e9ee0f2009-05-08 17:15:15 +00001654
hailfinger7df21362009-09-05 02:30:58 +00001655 if (ret) {
snelsone42c3802010-05-07 20:09:04 +00001656 msg_cerr("FAILED!\n");
hailfinger7df21362009-09-05 02:30:58 +00001657 } else {
David Hendricksc6c9f822010-11-03 15:07:01 -07001658 msg_cdbg("SUCCESS.\n");
hailfinger7df21362009-09-05 02:30:58 +00001659 }
1660 return ret;
hailfingerd219a232009-01-28 00:27:54 +00001661}
1662
hailfinger4c47e9d2010-10-19 22:06:20 +00001663void nonfatal_help_message(void)
1664{
1665 msg_gerr("Writing to the flash chip apparently didn't do anything.\n"
1666 "This means we have to add special support for your board, "
1667 "programmer or flash chip.\n"
1668 "Please report this on IRC at irc.freenode.net (channel "
1669 "#flashrom) or\n"
1670 "mail flashrom@flashrom.org!\n"
1671 "-------------------------------------------------------------"
1672 "------------------\n"
1673 "You may now reboot or simply leave the machine running.\n");
1674}
1675
uweb34ec9f2009-10-01 18:40:02 +00001676void emergency_help_message(void)
hailfinger0459e1c2009-08-19 13:55:34 +00001677{
snelsone42c3802010-05-07 20:09:04 +00001678 msg_gerr("Your flash chip is in an unknown state.\n"
uweb34ec9f2009-10-01 18:40:02 +00001679 "Get help on IRC at irc.freenode.net (channel #flashrom) or\n"
hailfinger5bae2332010-10-08 11:03:02 +00001680 "mail flashrom@flashrom.org with FAILED: your board name in "
1681 "the subject line!\n"
hailfinger74819ad2010-05-15 15:04:37 +00001682 "-------------------------------------------------------------"
1683 "------------------\n"
hailfinger0459e1c2009-08-19 13:55:34 +00001684 "DO NOT REBOOT OR POWEROFF!\n");
1685}
1686
uwe8d342eb2011-07-28 08:13:25 +00001687/* The way to go if you want a delimited list of programmers */
stefanct52700282011-06-26 17:38:17 +00001688void list_programmers(const char *delim)
hailfingerc77acb52009-12-24 02:15:55 +00001689{
1690 enum programmer p;
1691 for (p = 0; p < PROGRAMMER_INVALID; p++) {
snelsone42c3802010-05-07 20:09:04 +00001692 msg_ginfo("%s", programmer_table[p].name);
hailfingerc77acb52009-12-24 02:15:55 +00001693 if (p < PROGRAMMER_INVALID - 1)
snelsone42c3802010-05-07 20:09:04 +00001694 msg_ginfo("%s", delim);
hailfingerc77acb52009-12-24 02:15:55 +00001695 }
Simon Glass8dc82732013-07-16 10:13:51 -06001696 msg_ginfo("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001697}
1698
hailfingerf79d1712010-10-06 23:48:34 +00001699void list_programmers_linebreak(int startcol, int cols, int paren)
1700{
1701 const char *pname;
hailfingerb91c08c2011-08-15 19:54:20 +00001702 int pnamelen;
1703 int remaining = 0, firstline = 1;
hailfingerf79d1712010-10-06 23:48:34 +00001704 enum programmer p;
hailfingerb91c08c2011-08-15 19:54:20 +00001705 int i;
hailfingerf79d1712010-10-06 23:48:34 +00001706
1707 for (p = 0; p < PROGRAMMER_INVALID; p++) {
1708 pname = programmer_table[p].name;
1709 pnamelen = strlen(pname);
1710 if (remaining - pnamelen - 2 < 0) {
1711 if (firstline)
1712 firstline = 0;
1713 else
1714 printf("\n");
1715 for (i = 0; i < startcol; i++)
1716 printf(" ");
1717 remaining = cols - startcol;
1718 } else {
1719 printf(" ");
1720 remaining--;
1721 }
1722 if (paren && (p == 0)) {
1723 printf("(");
1724 remaining--;
1725 }
1726 printf("%s", pname);
1727 remaining -= pnamelen;
1728 if (p < PROGRAMMER_INVALID - 1) {
1729 printf(",");
1730 remaining--;
1731 } else {
1732 if (paren)
1733 printf(")");
1734 printf("\n");
1735 }
1736 }
1737}
1738
hailfinger3b471632010-03-27 16:36:40 +00001739void print_sysinfo(void)
1740{
David Hendricksc6c9f822010-11-03 15:07:01 -07001741 /* send to stderr for chromium os */
hailfinger3b471632010-03-27 16:36:40 +00001742#if HAVE_UTSNAME == 1
1743 struct utsname osinfo;
1744 uname(&osinfo);
1745
David Hendricksc6c9f822010-11-03 15:07:01 -07001746 msg_gerr(" on %s %s (%s)", osinfo.sysname, osinfo.release,
hailfinger3b471632010-03-27 16:36:40 +00001747 osinfo.machine);
1748#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001749 msg_gerr(" on unknown machine");
hailfinger3b471632010-03-27 16:36:40 +00001750#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001751 msg_gerr(", built with");
hailfinger3b471632010-03-27 16:36:40 +00001752#if NEED_PCI == 1
1753#ifdef PCILIB_VERSION
David Hendricksc6c9f822010-11-03 15:07:01 -07001754 msg_gerr(" libpci %s,", PCILIB_VERSION);
hailfinger3b471632010-03-27 16:36:40 +00001755#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001756 msg_gerr(" unknown PCI library,");
hailfinger3b471632010-03-27 16:36:40 +00001757#endif
1758#endif
1759#ifdef __clang__
David Hendricksc6c9f822010-11-03 15:07:01 -07001760 msg_gerr(" LLVM Clang");
hailfinger3cc85ad2010-07-17 14:49:30 +00001761#ifdef __clang_version__
David Hendricksc6c9f822010-11-03 15:07:01 -07001762 msg_gerr(" %s,", __clang_version__);
hailfinger3cc85ad2010-07-17 14:49:30 +00001763#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001764 msg_gerr(" unknown version (before r102686),");
hailfinger3cc85ad2010-07-17 14:49:30 +00001765#endif
hailfinger3b471632010-03-27 16:36:40 +00001766#elif defined(__GNUC__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001767 msg_gerr(" GCC");
hailfinger3b471632010-03-27 16:36:40 +00001768#ifdef __VERSION__
David Hendricksc6c9f822010-11-03 15:07:01 -07001769 msg_gerr(" %s,", __VERSION__);
hailfinger3b471632010-03-27 16:36:40 +00001770#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001771 msg_gerr(" unknown version,");
hailfinger3b471632010-03-27 16:36:40 +00001772#endif
1773#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001774 msg_gerr(" unknown compiler,");
hailfinger324a9cc2010-05-26 01:45:41 +00001775#endif
1776#if defined (__FLASHROM_LITTLE_ENDIAN__)
David Hendricksc6c9f822010-11-03 15:07:01 -07001777 msg_gerr(" little endian");
hailfinger324a9cc2010-05-26 01:45:41 +00001778#else
David Hendricksc6c9f822010-11-03 15:07:01 -07001779 msg_gerr(" big endian");
hailfinger3b471632010-03-27 16:36:40 +00001780#endif
David Hendricksc6c9f822010-11-03 15:07:01 -07001781 msg_gerr("\n");
hailfinger3b471632010-03-27 16:36:40 +00001782}
1783
uwefdeca092008-01-21 15:24:22 +00001784void print_version(void)
1785{
David Hendricksc6c9f822010-11-03 15:07:01 -07001786 /* send to stderr for chromium os */
1787 msg_gerr("flashrom v%s", flashrom_version);
hailfinger3b471632010-03-27 16:36:40 +00001788 print_sysinfo();
uwefdeca092008-01-21 15:24:22 +00001789}
1790
hailfinger74819ad2010-05-15 15:04:37 +00001791void print_banner(void)
1792{
1793 msg_ginfo("flashrom is free software, get the source code at "
uwe8d342eb2011-07-28 08:13:25 +00001794 "http://www.flashrom.org\n");
hailfinger74819ad2010-05-15 15:04:37 +00001795 msg_ginfo("\n");
1796}
1797
hailfingerc77acb52009-12-24 02:15:55 +00001798int selfcheck(void)
1799{
hailfinger45177872010-01-18 08:14:43 +00001800 int ret = 0;
hailfinger48ed3e22011-05-04 00:39:50 +00001801 const struct flashchip *flash;
hailfinger45177872010-01-18 08:14:43 +00001802
1803 /* Safety check. Instead of aborting after the first error, check
1804 * if more errors exist.
1805 */
hailfingerc77acb52009-12-24 02:15:55 +00001806 if (ARRAY_SIZE(programmer_table) - 1 != PROGRAMMER_INVALID) {
snelsone42c3802010-05-07 20:09:04 +00001807 msg_gerr("Programmer table miscompilation!\n");
hailfinger45177872010-01-18 08:14:43 +00001808 ret = 1;
hailfingerc77acb52009-12-24 02:15:55 +00001809 }
stefanct8632c922011-07-26 14:33:46 +00001810 /* It would be favorable if we could also check for correct termination
stefanctdfd58832011-07-25 20:38:52 +00001811 * of the following arrays, but we don't know their sizes in here...
stefanct6d836ba2011-05-26 01:35:19 +00001812 * For 'flashchips' we check the first element to be non-null. In the
1813 * other cases there exist use cases where the first element can be
1814 * null. */
Yunlian Jiang945c3392015-01-20 15:56:44 -08001815 if (flashchips[0].vendor == NULL) {
stefanct6d836ba2011-05-26 01:35:19 +00001816 msg_gerr("Flashchips table miscompilation!\n");
1817 ret = 1;
1818 }
hailfinger45177872010-01-18 08:14:43 +00001819 for (flash = flashchips; flash && flash->name; flash++)
1820 if (selfcheck_eraseblocks(flash))
1821 ret = 1;
stefanct6d836ba2011-05-26 01:35:19 +00001822
hailfinger45177872010-01-18 08:14:43 +00001823 return ret;
hailfingerc77acb52009-12-24 02:15:55 +00001824}
1825
hailfinger48ed3e22011-05-04 00:39:50 +00001826void check_chip_supported(const struct flashchip *flash)
hailfingerc77acb52009-12-24 02:15:55 +00001827{
1828 if (TEST_OK_MASK != (flash->tested & TEST_OK_MASK)) {
David Hendricksc801adb2010-12-09 16:58:56 -08001829 msg_cdbg("===\n");
hailfingerc77acb52009-12-24 02:15:55 +00001830 if (flash->tested & TEST_BAD_MASK) {
David Hendricksc801adb2010-12-09 16:58:56 -08001831 msg_cdbg("This flash part has status NOT WORKING for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001832 if (flash->tested & TEST_BAD_PROBE)
David Hendricksc801adb2010-12-09 16:58:56 -08001833 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001834 if (flash->tested & TEST_BAD_READ)
David Hendricksc801adb2010-12-09 16:58:56 -08001835 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001836 if (flash->tested & TEST_BAD_ERASE)
David Hendricksc801adb2010-12-09 16:58:56 -08001837 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001838 if (flash->tested & TEST_BAD_WRITE)
David Hendricksc801adb2010-12-09 16:58:56 -08001839 msg_cdbg(" WRITE");
Duncan Laurie06ffd522015-10-26 12:56:08 -07001840 if (flash->tested & TEST_BAD_UREAD)
1841 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001842 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001843 }
1844 if ((!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE)) ||
1845 (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ)) ||
1846 (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE)) ||
1847 (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))) {
David Hendricksc801adb2010-12-09 16:58:56 -08001848 msg_cdbg("This flash part has status UNTESTED for operations:");
hailfingerc77acb52009-12-24 02:15:55 +00001849 if (!(flash->tested & TEST_BAD_PROBE) && !(flash->tested & TEST_OK_PROBE))
David Hendricksc801adb2010-12-09 16:58:56 -08001850 msg_cdbg(" PROBE");
hailfingerc77acb52009-12-24 02:15:55 +00001851 if (!(flash->tested & TEST_BAD_READ) && !(flash->tested & TEST_OK_READ))
David Hendricksc801adb2010-12-09 16:58:56 -08001852 msg_cdbg(" READ");
hailfingerc77acb52009-12-24 02:15:55 +00001853 if (!(flash->tested & TEST_BAD_ERASE) && !(flash->tested & TEST_OK_ERASE))
David Hendricksc801adb2010-12-09 16:58:56 -08001854 msg_cdbg(" ERASE");
hailfingerc77acb52009-12-24 02:15:55 +00001855 if (!(flash->tested & TEST_BAD_WRITE) && !(flash->tested & TEST_OK_WRITE))
David Hendricksc801adb2010-12-09 16:58:56 -08001856 msg_cdbg(" WRITE");
Duncan Laurie06ffd522015-10-26 12:56:08 -07001857 if (!(flash->tested & TEST_BAD_UREAD) && !(flash->tested & TEST_OK_UREAD))
1858 msg_cdbg(" UNBOUNDED READ");
David Hendricksc801adb2010-12-09 16:58:56 -08001859 msg_cdbg("\n");
hailfingerc77acb52009-12-24 02:15:55 +00001860 }
hailfinger92cd8e32010-01-07 03:24:05 +00001861 /* FIXME: This message is designed towards CLI users. */
David Hendricksc801adb2010-12-09 16:58:56 -08001862 msg_cdbg("The test status of this chip may have been updated "
hailfinger74819ad2010-05-15 15:04:37 +00001863 "in the latest development\n"
1864 "version of flashrom. If you are running the latest "
1865 "development version,\n"
1866 "please email a report to flashrom@flashrom.org if "
1867 "any of the above operations\n"
1868 "work correctly for you with this flash part. Please "
1869 "include the flashrom\n"
1870 "output with the additional -V option for all "
1871 "operations you tested (-V, -Vr,\n"
1872 "-Vw, -VE), and mention which mainboard or "
1873 "programmer you tested.\n"
hailfinger5bae2332010-10-08 11:03:02 +00001874 "Please mention your board in the subject line. "
1875 "Thanks for your help!\n");
hailfingerc77acb52009-12-24 02:15:55 +00001876 }
1877}
1878
hailfinger771fc182010-10-15 00:01:14 +00001879/* FIXME: This function signature needs to be improved once doit() has a better
1880 * function signature.
1881 */
stefanct02116582011-05-18 01:30:56 +00001882int 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 +00001883{
1884 if (!programmer_may_write && (write_it || erase_it)) {
1885 msg_perr("Write/erase is not working yet on your programmer in "
1886 "its current configuration.\n");
1887 /* --force is the wrong approach, but it's the best we can do
1888 * until the generic programmer parameter parser is merged.
1889 */
1890 if (!force)
1891 return 1;
1892 msg_cerr("Continuing anyway.\n");
1893 }
1894
1895 if (read_it || erase_it || write_it || verify_it) {
1896 /* Everything needs read. */
1897 if (flash->tested & TEST_BAD_READ) {
1898 msg_cerr("Read is not working on this chip. ");
1899 if (!force)
1900 return 1;
1901 msg_cerr("Continuing anyway.\n");
1902 }
1903 if (!flash->read) {
1904 msg_cerr("flashrom has no read function for this "
1905 "flash chip.\n");
1906 return 1;
1907 }
1908 }
1909 if (erase_it || write_it) {
1910 /* Write needs erase. */
1911 if (flash->tested & TEST_BAD_ERASE) {
1912 msg_cerr("Erase is not working on this chip. ");
1913 if (!force)
1914 return 1;
1915 msg_cerr("Continuing anyway.\n");
1916 }
stefancte1c5acf2011-07-04 07:27:17 +00001917 if(count_usable_erasers(flash) == 0) {
stefanct569dbb62011-07-01 00:19:12 +00001918 msg_cerr("flashrom has no erase function for this "
1919 "flash chip.\n");
1920 return 1;
1921 }
hailfinger771fc182010-10-15 00:01:14 +00001922 }
1923 if (write_it) {
1924 if (flash->tested & TEST_BAD_WRITE) {
1925 msg_cerr("Write is not working on this chip. ");
1926 if (!force)
1927 return 1;
1928 msg_cerr("Continuing anyway.\n");
1929 }
1930 if (!flash->write) {
1931 msg_cerr("flashrom has no write function for this "
1932 "flash chip.\n");
1933 return 1;
1934 }
1935 }
1936 return 0;
1937}
1938
hailfingerc77acb52009-12-24 02:15:55 +00001939/* This function signature is horrible. We need to design a better interface,
1940 * but right now it allows us to split off the CLI code.
hailfingerd217d122010-10-08 18:52:29 +00001941 * Besides that, the function itself is a textbook example of abysmal code flow.
hailfingerc77acb52009-12-24 02:15:55 +00001942 */
Simon Glass9ad06c12013-07-03 22:08:17 +09001943int doit(struct flashchip *flash, int force, const char *filename, int read_it,
1944 int write_it, int erase_it, int verify_it, int extract_it,
1945 const char *diff_file)
hailfingerc77acb52009-12-24 02:15:55 +00001946{
hailfinger4c47e9d2010-10-19 22:06:20 +00001947 uint8_t *oldcontents;
1948 uint8_t *newcontents;
hailfingerc77acb52009-12-24 02:15:55 +00001949 int ret = 0;
hailfinger4c47e9d2010-10-19 22:06:20 +00001950 unsigned long size = flash->total_size * 1024;
hailfingerc77acb52009-12-24 02:15:55 +00001951
stefanct02116582011-05-18 01:30:56 +00001952 if (chip_safety_check(flash, force, read_it, write_it, erase_it, verify_it)) {
hailfinger771fc182010-10-15 00:01:14 +00001953 msg_cerr("Aborting.\n");
hailfinger90fcf9b2010-11-05 14:51:59 +00001954 ret = 1;
1955 goto out_nofree;
hailfinger771fc182010-10-15 00:01:14 +00001956 }
1957
hailfinger771fc182010-10-15 00:01:14 +00001958 /* Given the existence of read locks, we want to unlock for read,
1959 * erase and write.
1960 */
1961 if (flash->unlock)
1962 flash->unlock(flash);
1963
David Hendricks6d62d752011-03-07 21:20:22 -08001964 /* add entries for regions specified in flashmap */
Louis Yung-Chieh Lo1f6bbf52011-04-06 10:24:38 +08001965 if (!set_ignore_fmap && add_fmap_entries(flash) < 0) {
David Hendricks6d62d752011-03-07 21:20:22 -08001966 ret = 1;
1967 goto out_nofree;
1968 }
1969
Simon Glass9ad06c12013-07-03 22:08:17 +09001970 if (extract_it) {
1971 ret = extract_regions(flash);
1972 goto out_nofree;
1973 }
1974
David Hendricksd0ea9ed2011-03-04 17:31:57 -08001975 /* mark entries included using -i argument as "included" if they are
1976 found in the master rom_entries list */
1977 if (process_include_args() < 0) {
1978 ret = 1;
1979 goto out_nofree;
1980 }
1981
hailfinger771fc182010-10-15 00:01:14 +00001982 if (read_it) {
1983 ret = read_flash_to_file(flash, filename);
hailfinger90fcf9b2010-11-05 14:51:59 +00001984 goto out_nofree;
hailfinger5828baf2010-07-03 12:14:25 +00001985 }
hailfingerb437e282010-11-04 01:04:27 +00001986
stefanctd611e8f2011-07-12 22:35:21 +00001987 oldcontents = malloc(size);
1988 if (!oldcontents) {
1989 msg_gerr("Out of memory!\n");
1990 exit(1);
1991 }
Simon Glass4c214132013-07-16 10:09:28 -06001992 /* Assume worst case: All blocks are not erased. */
1993 memset(oldcontents, flash_unerased_value(flash), size);
stefanctd611e8f2011-07-12 22:35:21 +00001994 newcontents = malloc(size);
1995 if (!newcontents) {
1996 msg_gerr("Out of memory!\n");
1997 exit(1);
1998 }
Simon Glass4c214132013-07-16 10:09:28 -06001999 /* Assume best case: All blocks are erased. */
2000 memset(newcontents, flash_erase_value(flash), size);
hailfingerb437e282010-11-04 01:04:27 +00002001 /* Side effect of the assumptions above: Default write action is erase
2002 * because newcontents looks like a completely erased chip, and
Simon Glass4c214132013-07-16 10:09:28 -06002003 * oldcontents being completely unerased means we have to erase
2004 * everything before we can write.
hailfingerb437e282010-11-04 01:04:27 +00002005 */
2006
ollie0eb62d62004-12-08 20:10:01 +00002007 if (erase_it) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002008 /* FIXME: Do we really want the scary warning if erase failed?
2009 * After all, after erase the chip is either blank or partially
2010 * blank or it has the old contents. A blank chip won't boot,
2011 * so if the user wanted erase and reboots afterwards, the user
2012 * knows very well that booting won't work.
2013 */
hailfingerb437e282010-11-04 01:04:27 +00002014 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
hailfinger0459e1c2009-08-19 13:55:34 +00002015 emergency_help_message();
hailfingerb437e282010-11-04 01:04:27 +00002016 ret = 1;
hailfinger0459e1c2009-08-19 13:55:34 +00002017 }
hailfinger90fcf9b2010-11-05 14:51:59 +00002018 goto out;
hailfingerd217d122010-10-08 18:52:29 +00002019 }
hailfinger771fc182010-10-15 00:01:14 +00002020
hailfingerd217d122010-10-08 18:52:29 +00002021 if (write_it || verify_it) {
David Hendricksdf29a832013-06-28 14:33:51 -07002022 /*
2023 * Note: This must be done before any files specified by -i
2024 * arguments are processed merged into the newcontents since
2025 * -i files take priority. See http://crbug.com/263495.
2026 */
2027 if (filename) {
2028 if (read_buf_from_file(newcontents, size, filename)) {
2029 ret = 1;
2030 goto out;
2031 }
2032 } else {
2033 /* Content will be read from -i args, so they must
2034 * not overlap. */
2035 if (included_regions_overlap()) {
2036 msg_gerr("Error: Included regions must "
2037 "not overlap.\n");
2038 ret = 1;
2039 goto out;
2040 }
stepan1da96c02006-11-21 23:48:51 +00002041 }
2042
David Hendricksac82cac2012-06-19 10:29:37 -07002043#if 0
2044 /*
2045 * FIXME: show_id() causes failure if vendor:mainboard do not
2046 * match. This may happen if codenames are in flux.
2047 * See chrome-os-partner:10414.
2048 */
hailfinger90c7d542010-05-31 15:27:27 +00002049#if CONFIG_INTERNAL == 1
hailfinger4c47e9d2010-10-19 22:06:20 +00002050 if (programmer == PROGRAMMER_INTERNAL)
2051 show_id(newcontents, size, force);
hailfinger80422e22009-12-13 22:28:00 +00002052#endif
David Hendricksac82cac2012-06-19 10:29:37 -07002053#endif
ollie5672ac62004-03-17 22:22:08 +00002054 }
2055
David Hendricksc44d7a02011-10-17 11:28:43 -07002056 /* Obtain a reference image so that we can check whether regions need
2057 * to be erased and to give better diagnostics in case write fails.
David Hendricks52ddff02013-07-23 15:05:14 -07002058 * If --fast-verify is used then only the regions which are included
2059 * using -i will be read.
hailfinger4c47e9d2010-10-19 22:06:20 +00002060 */
David Hendricksc44d7a02011-10-17 11:28:43 -07002061 if (diff_file) {
2062 msg_cdbg("Reading old contents from file... ");
2063 if (read_buf_from_file(oldcontents, size, diff_file)) {
2064 ret = 1;
2065 msg_cdbg("FAILED.\n");
2066 goto out;
2067 }
2068 } else {
2069 msg_cdbg("Reading old contents from flash chip... ");
David Hendricksd4e712c2013-08-02 17:06:16 -07002070 if (verify_it == VERIFY_PARTIAL) {
2071 if (handle_partial_read(flash, oldcontents,
2072 read_flash, 0) < 0) {
David Hendricks52ddff02013-07-23 15:05:14 -07002073 ret = 1;
2074 msg_cdbg("FAILED.\n");
2075 goto out;
2076 }
David Hendricksd4e712c2013-08-02 17:06:16 -07002077 } else {
2078 if (read_flash(flash, oldcontents, 0, size)) {
David Hendricks52ddff02013-07-23 15:05:14 -07002079 ret = 1;
2080 msg_cdbg("FAILED.\n");
2081 goto out;
2082 }
David Hendricksc44d7a02011-10-17 11:28:43 -07002083 }
hailfinger4c47e9d2010-10-19 22:06:20 +00002084 }
David Hendricks89a45e52011-11-22 16:56:22 -08002085 msg_cdbg("done.\n");
hailfinger4c47e9d2010-10-19 22:06:20 +00002086
David Hendricksdf29a832013-06-28 14:33:51 -07002087 /*
2088 * Note: This must be done after reading the file specified for the
2089 * -w/-v argument, if any, so that files specified using -i end up
2090 * in the "newcontents" buffer before being written.
2091 * See http://crbug.com/263495.
2092 */
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002093 if (handle_romentries(flash, oldcontents, newcontents)) {
2094 ret = 1;
David Hendricks5d8ea572013-07-26 14:03:05 -07002095 msg_cerr("Error handling ROM entries.\n");
Louis Yung-Chieh Lo404470d2011-09-06 16:59:40 +08002096 goto out;
2097 }
uwef6641642007-05-09 10:17:44 +00002098
stuge8ce3a3c2008-04-28 14:47:30 +00002099 if (write_it) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002100 // parse the new fmap
David Hendricksb907de32014-08-11 16:47:09 -07002101 if ((ret = cros_ec_prepare(newcontents, size))) {
2102 msg_cerr("CROS_EC prepare failed, ret=%d.\n", ret);
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002103 goto out;
2104 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002105
hailfingerb437e282010-11-04 01:04:27 +00002106 if (erase_and_write_flash(flash, oldcontents, newcontents)) {
2107 msg_cerr("Uh oh. Erase/write failed. Checking if "
2108 "anything changed.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002109 if (!read_flash(flash, newcontents, 0, size)) {
hailfinger4c47e9d2010-10-19 22:06:20 +00002110 if (!memcmp(oldcontents, newcontents, size)) {
2111 msg_cinfo("Good. It seems nothing was "
2112 "changed.\n");
2113 nonfatal_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002114 ret = 1;
2115 goto out;
hailfinger4c47e9d2010-10-19 22:06:20 +00002116 }
2117 }
hailfingerd217d122010-10-08 18:52:29 +00002118 emergency_help_message();
hailfinger90fcf9b2010-11-05 14:51:59 +00002119 ret = 1;
2120 goto out;
stuge8ce3a3c2008-04-28 14:47:30 +00002121 }
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002122
David Hendricksb907de32014-08-11 16:47:09 -07002123 ret = cros_ec_need_2nd_pass();
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002124 if (ret < 0) {
2125 // Jump failed
David Hendricksb907de32014-08-11 16:47:09 -07002126 msg_cerr("cros_ec_need_2nd_pass() failed. Stop.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002127 emergency_help_message();
2128 ret = 1;
2129 goto out;
2130 } else if (ret > 0) {
2131 // Need 2nd pass. Get the just written content.
David Hendricksb907de32014-08-11 16:47:09 -07002132 msg_pdbg("CROS_EC needs 2nd pass.\n");
David Hendrickse3451942013-03-21 17:23:29 -07002133 if (read_flash(flash, oldcontents, 0, size)) {
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002134 msg_cerr("Uh oh. Cannot get latest content.\n");
2135 emergency_help_message();
2136 ret = 1;
2137 goto out;
2138 }
2139 // write 2nd pass
2140 if (erase_and_write_flash(flash, oldcontents,
2141 newcontents)) {
David Hendricksb907de32014-08-11 16:47:09 -07002142 msg_cerr("Uh oh. CROS_EC 2nd pass failed.\n");
Louis Yung-Chieh Lo8d0971e2012-03-23 00:07:38 +08002143 emergency_help_message();
2144 ret = 1;
2145 goto out;
2146 }
2147 ret = 0;
2148 }
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002149
David Hendricksb907de32014-08-11 16:47:09 -07002150 if (cros_ec_finish() < 0) {
2151 msg_cerr("cros_ec_finish() failed. Stop.\n");
Louis Yung-Chieh Lodeefd822012-07-09 17:07:43 +08002152 emergency_help_message();
2153 ret = 1;
2154 goto out;
2155 }
stuge8ce3a3c2008-04-28 14:47:30 +00002156 }
ollie6a600992005-11-26 21:55:36 +00002157
hailfinger0459e1c2009-08-19 13:55:34 +00002158 if (verify_it) {
David Hendricks9ba79fb2015-04-03 12:06:16 -07002159 if ((write_it || erase_it) && !content_has_changed) {
2160 msg_gdbg("Nothing was erased or written, skipping "
2161 "verification\n");
2162 } else {
2163 /* Work around chips which need some time to calm down. */
2164 if (write_it && verify_it != VERIFY_PARTIAL)
2165 programmer_delay(1000*1000);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002166
David Hendricks9ba79fb2015-04-03 12:06:16 -07002167 ret = verify_flash(flash, newcontents, verify_it);
Louis Yung-Chieh Lo5d95f042011-09-01 17:33:06 +08002168
David Hendricks9ba79fb2015-04-03 12:06:16 -07002169 /* If we tried to write, and verification now fails, we
2170 * might have an emergency situation.
2171 */
2172 if (ret && write_it)
2173 emergency_help_message();
2174 }
hailfinger0459e1c2009-08-19 13:55:34 +00002175 }
ollie6a600992005-11-26 21:55:36 +00002176
hailfinger90fcf9b2010-11-05 14:51:59 +00002177out:
2178 free(oldcontents);
2179 free(newcontents);
2180out_nofree:
David Hendricksbf36f092010-11-02 23:39:29 -07002181 chip_restore(); /* must be done before programmer_shutdown() */
David Hendricks668f29d2011-01-27 18:51:45 -08002182 /*
2183 * programmer_shutdown() call is moved to cli_mfg() in chromium os
2184 * tree. This is because some operations, such as write protection,
2185 * requires programmer_shutdown() but does not call doit().
2186 */
2187// programmer_shutdown();
stepan83eca252006-01-04 16:42:57 +00002188 return ret;
rminnich8d3ff912003-10-25 17:01:29 +00002189}