blob: a9bba8f630a98b1159e88f9858b45a8833afb001 [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Nico Huber7af0e792016-04-29 16:40:15 +02008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000010 *
Uwe Hermannd1107642007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000015 *
Uwe Hermannd1107642007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000020 */
21
Felix Singerf25447e2022-08-19 02:44:28 +020022#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000023#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000024#include <sys/types.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000025#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000026#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000027#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000028#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000029#include <ctype.h>
Edward O'Callaghan3b64d812022-08-12 13:07:51 +100030
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000031#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000032#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000033#include "programmer.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010034#include "hwaccess_physmap.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010035#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000036
Nico Huberc3b02dc2023-08-12 01:13:45 +020037const char flashprog_version[] = FLASHPROG_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000038const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000039
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020040static const struct programmer_entry *programmer = NULL;
Nico Huber6a2ebeb2022-08-26 11:36:48 +020041static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000042
Uwe Hermann48ec1b12010-08-08 17:01:18 +000043/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000044 * Programmers supporting multiple buses can have differing size limits on
45 * each bus. Store the limits for each bus in a common struct.
46 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000047struct decode_sizes max_rom_decode;
48
49/* If nonzero, used as the start address of bottom-aligned flash. */
50unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000051
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000052/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020053bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000054
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000055#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000056static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000057/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000058static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000059 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000060 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000061} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000062/* Initialize to 0 to make sure nobody registers a shutdown function before
63 * programmer init.
64 */
Felix Singerf25447e2022-08-19 02:44:28 +020065static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000066
Stefan Taunerc4f44df2013-08-12 22:58:43 +000067/* Did we change something or was every erase/write skipped (if any)? */
68static bool all_skipped = true;
69
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000070static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000071
Stefan Tauner2a1ed772014-08-31 00:09:21 +000072int shutdown_free(void *data)
73{
74 free(data);
75 return 0;
76}
77
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000078/* Register a function to be executed on programmer shutdown.
79 * The advantage over atexit() is that you can supply a void pointer which will
80 * be used as parameter to the registered function upon programmer shutdown.
81 * This pointer can point to arbitrary data used by said function, e.g. undo
82 * information for GPIO settings etc. If unneeded, set data=NULL.
83 * Please note that the first (void *data) belongs to the function signature of
84 * the function passed as first parameter.
85 */
David Hendricks8bb20212011-06-14 01:35:36 +000086int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000087{
88 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000089 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000090 SHUTDOWN_MAXFN);
91 return 1;
92 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000093 if (!may_register_shutdown) {
94 msg_perr("Tried to register a shutdown function before "
95 "programmer init.\n");
96 return 1;
97 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000098 shutdown_fn[shutdown_fn_count].func = function;
99 shutdown_fn[shutdown_fn_count].data = data;
100 shutdown_fn_count++;
101
102 return 0;
103}
104
Nikolai Artemiev4ad48642020-11-05 13:54:27 +1100105int register_chip_restore(chip_restore_fn_cb_t func,
106 struct flashctx *flash, uint8_t status)
107{
108 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
109 msg_perr("Tried to register more than %i chip restore"
110 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
111 return 1;
112 }
113 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
114 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
115 flash->chip_restore_fn_count++;
116
117 return 0;
118}
119
120static int deregister_chip_restore(struct flashctx *flash)
121{
122 int rc = 0;
123
124 while (flash->chip_restore_fn_count > 0) {
125 int i = --flash->chip_restore_fn_count;
126 rc |= flash->chip_restore_fn[i].func(
127 flash, flash->chip_restore_fn[i].status);
128 }
129
130 return rc;
131}
132
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200133int programmer_init(const struct programmer_entry *prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000134{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000135 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000136
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200137 if (prog == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000138 msg_perr("Invalid programmer specified!\n");
139 return -1;
140 }
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200141 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000142 /* Initialize all programmer specific data. */
143 /* Default to unlimited decode sizes. */
144 max_rom_decode = (const struct decode_sizes) {
145 .parallel = 0xffffffff,
146 .lpc = 0xffffffff,
147 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000148 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000149 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000150 /* Default to top aligned flash at 4 GB. */
151 flashbase = 0;
152 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200153 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000154 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200155 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000156
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200157 if (param) {
158 programmer_param = strdup(param);
159 if (!programmer_param) {
160 msg_perr("Out of memory!\n");
161 return ERROR_FATAL;
162 }
163 } else {
164 programmer_param = NULL;
165 }
166
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200167 msg_pdbg("Initializing %s programmer\n", programmer->name);
168 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000169 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000170 if (ret != 0) {
171 /* It is quite possible that any unhandled programmer parameter would have been valid,
172 * but an error in actual programmer init happened before the parameter was evaluated.
173 */
174 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
175 programmer_param);
176 } else {
177 /* Actual programmer init was successful, but the user specified an invalid or unusable
178 * (for the current programmer configuration) parameter.
179 */
180 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
181 msg_perr("Aborting.\n");
182 ret = ERROR_FATAL;
183 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000184 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200185 free(programmer_param);
186 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000187 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000188}
189
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000190/** Calls registered shutdown functions and resets internal programmer-related variables.
191 * Calling it is safe even without previous initialization, but further interactions with programmer support
192 * require a call to programmer_init() (afterwards).
193 *
194 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000195int programmer_shutdown(void)
196{
David Hendricks8bb20212011-06-14 01:35:36 +0000197 int ret = 0;
198
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000199 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200200 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000201 while (shutdown_fn_count > 0) {
202 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000203 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000204 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000205 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000206
David Hendricks8bb20212011-06-14 01:35:36 +0000207 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000208}
209
Stefan Taunerf80419c2014-05-02 15:41:42 +0000210void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000211{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000212 if (usecs > 0) {
213 if (programmer->delay)
214 programmer->delay(usecs);
215 else
216 internal_delay(usecs);
217 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000218}
219
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000220int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
221 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000222{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000223 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000224
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000225 return 0;
226}
227
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000228/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000229 * It will look for needle with a subsequent '=' in haystack, return a copy of
230 * needle and remove everything from the first occurrence of needle to the next
231 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000232 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200233static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000234{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000235 char *param_pos, *opt_pos, *rest;
236 char *opt = NULL;
237 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000238 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000239
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000240 needlelen = strlen(needle);
241 if (!needlelen) {
242 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200243 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000244 return NULL;
245 }
246 /* No programmer parameters given. */
247 if (*haystack == NULL)
248 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000249 param_pos = strstr(*haystack, needle);
250 do {
251 if (!param_pos)
252 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000253 /* Needle followed by '='? */
254 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000255 /* Beginning of the string? */
256 if (param_pos == *haystack)
257 break;
258 /* After a delimiter? */
259 if (strchr(delim, *(param_pos - 1)))
260 break;
261 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000262 /* Continue searching. */
263 param_pos++;
264 param_pos = strstr(param_pos, needle);
265 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000266
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000267 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000268 /* Get the string after needle and '='. */
269 opt_pos = param_pos + needlelen + 1;
270 optlen = strcspn(opt_pos, delim);
271 /* Return an empty string if the parameter was empty. */
272 opt = malloc(optlen + 1);
273 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000274 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000275 exit(1);
276 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000277 strncpy(opt, opt_pos, optlen);
278 opt[optlen] = '\0';
279 rest = opt_pos + optlen;
280 /* Skip all delimiters after the current parameter. */
281 rest += strspn(rest, delim);
282 memmove(param_pos, rest, strlen(rest) + 1);
283 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000284 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000285
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000286 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000287}
288
Stefan Tauner66652442011-06-26 17:38:17 +0000289char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000290{
291 return extract_param(&programmer_param, param_name, ",");
292}
293
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000294/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000295static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000296{
297 unsigned int usable_erasefunctions = 0;
298 int k;
299 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
300 if (!check_block_eraser(flash, k, 0))
301 usable_erasefunctions++;
302 }
303 return usable_erasefunctions;
304}
305
Mark Marshallf20b7be2014-05-09 21:16:21 +0000306static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000307{
308 int ret = 0, failcount = 0;
309 unsigned int i;
310 for (i = 0; i < len; i++) {
311 if (wantbuf[i] != havebuf[i]) {
312 /* Only print the first failure. */
313 if (!failcount++)
314 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
315 start + i, wantbuf[i], havebuf[i]);
316 }
317 }
318 if (failcount) {
319 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
320 start, start + len - 1, failcount);
321 ret = -1;
322 }
323 return ret;
324}
325
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000326/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600327static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000328{
329 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300330 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000331
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100332 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000333 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100334 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100335 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000336 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300337 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000338 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100339
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000340 free(cmpbuf);
341 return ret;
342}
343
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000344/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000345 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000346 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000347 * @start offset to the base address of the flash chip
348 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000349 * @return 0 for success, -1 for failure
350 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000351int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000352{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000353 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000354 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000355
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100356 if (start + len > flash->chip->total_size * 1024) {
357 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
358 " total_size 0x%x\n", __func__, start, len,
359 flash->chip->total_size * 1024);
360 return -1;
361 }
362
Stefan Taunerdf64a422014-05-27 00:06:14 +0000363 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000364 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100365 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000366 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000367 }
368
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100369 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000370 if (ret) {
371 msg_gerr("Verification impossible because read failed "
372 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000373 ret = -1;
374 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000375 }
376
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000377 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000378out_free:
379 free(readbuf);
380 return ret;
381}
382
Nico Huber3ac761c2023-01-16 02:43:17 +0100383size_t gran_to_bytes(const enum write_granularity gran)
Nico Huberb77607f2023-01-16 02:25:45 +0100384{
385 switch (gran) {
386 case write_gran_1bit: return 1;
387 case write_gran_1byte: return 1;
388 case write_gran_1byte_implicit_erase: return 1;
389 case write_gran_128bytes: return 128;
390 case write_gran_256bytes: return 256;
391 case write_gran_264bytes: return 264;
392 case write_gran_512bytes: return 512;
393 case write_gran_528bytes: return 528;
394 case write_gran_1024bytes: return 1024;
395 case write_gran_1056bytes: return 1056;
396 default: return 0;
397 }
398}
399
Stefan Tauner02437452013-04-01 19:34:53 +0000400/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300401static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
402 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000403{
404 unsigned int i, j, limit;
405 for (j = 0; j < len / gran; j++) {
406 limit = min (gran, len - j * gran);
407 /* Are 'have' and 'want' identical? */
408 if (!memcmp(have + j * gran, want + j * gran, limit))
409 continue;
410 /* have needs to be in erased state. */
411 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300412 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000413 return 1;
414 }
415 return 0;
416}
417
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000418/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000419 * Check if the buffer @have can be programmed to the content of @want without
420 * erasing. This is only possible if all chunks of size @gran are either kept
421 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000422 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000423 * Warning: This function assumes that @have and @want point to naturally
424 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000425 *
426 * @have buffer with current content
427 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000428 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000429 * @gran write granularity (enum, not count)
430 * @return 0 if no erase is needed, 1 otherwise
431 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000432static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
433 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000434{
Stefan Tauner02437452013-04-01 19:34:53 +0000435 unsigned int i;
Nico Huberb77607f2023-01-16 02:25:45 +0100436 size_t stride;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000437
438 switch (gran) {
439 case write_gran_1bit:
Nico Huberb77607f2023-01-16 02:25:45 +0100440 for (i = 0; i < len; i++) {
441 if ((have[i] & want[i]) != want[i])
442 return 1;
443 }
444 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000445 case write_gran_1byte:
Nico Huberb77607f2023-01-16 02:25:45 +0100446 for (i = 0; i < len; i++) {
447 if ((have[i] != want[i]) && (have[i] != erased_value))
448 return 1;
449 }
450 return 0;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000451 case write_gran_1byte_implicit_erase:
452 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
Nico Huberb77607f2023-01-16 02:25:45 +0100453 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000454 default:
Nico Huberb77607f2023-01-16 02:25:45 +0100455 stride = gran_to_bytes(gran);
456 if (stride) {
457 return need_erase_gran_bytes(have, want, len, stride, erased_value);
458 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000459 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200460 "flashprog@flashprog.org\n", __func__);
Nico Huberb77607f2023-01-16 02:25:45 +0100461 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000462 }
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000463}
464
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000465/**
466 * Check if the buffer @have needs to be programmed to get the content of @want.
467 * If yes, return 1 and fill in first_start with the start address of the
468 * write operation and first_len with the length of the first to-be-written
469 * chunk. If not, return 0 and leave first_start and first_len undefined.
470 *
471 * Warning: This function assumes that @have and @want point to naturally
472 * aligned regions.
473 *
474 * @have buffer with current content
475 * @want buffer with desired content
476 * @len length of the checked area
477 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000478 * @first_start offset of the first byte which needs to be written (passed in
479 * value is increased by the offset of the first needed write
480 * relative to have/want or unchanged if no write is needed)
481 * @return length of the first contiguous area which needs to be written
482 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000483 *
484 * FIXME: This function needs a parameter which tells it about coalescing
485 * in relation to the max write length of the programmer and the max write
486 * length of the chip.
487 */
Nico Huber3b9c86d2023-01-15 22:58:06 +0100488static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, chipsize_t len,
489 chipoff_t *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000490{
Felix Singerf25447e2022-08-19 02:44:28 +0200491 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000492 unsigned int rel_start = 0, first_len = 0;
Nico Huberb77607f2023-01-16 02:25:45 +0100493 unsigned int i, limit;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000494
Nico Huberb77607f2023-01-16 02:25:45 +0100495 const size_t stride = gran_to_bytes(gran);
496 if (!stride) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000497 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200498 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000499 /* Claim that no write was needed. A write with unknown
500 * granularity is too dangerous to try.
501 */
502 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000503 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000504 for (i = 0; i < len / stride; i++) {
505 limit = min(stride, len - i * stride);
506 /* Are 'have' and 'want' identical? */
507 if (memcmp(have + i * stride, want + i * stride, limit)) {
508 if (!need_write) {
509 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200510 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000511 rel_start = i * stride;
512 }
513 } else {
514 if (need_write) {
515 /* First location where have and want
516 * do not differ anymore.
517 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000518 break;
519 }
520 }
521 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000522 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000523 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000524 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000525 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000526}
527
Nico Huber2d625722016-05-03 10:48:02 +0200528/*
529 * Return a string corresponding to the bustype parameter.
530 * Memory is obtained with malloc() and must be freed with free() by the caller.
531 */
532char *flashbuses_to_text(enum chipbustype bustype)
533{
534 char *ret = calloc(1, 1);
535 /*
536 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
537 * will cease to exist and should be eliminated here as well.
538 */
539 if (bustype == BUS_NONSPI) {
540 ret = strcat_realloc(ret, "Non-SPI, ");
541 } else {
542 if (bustype & BUS_PARALLEL)
543 ret = strcat_realloc(ret, "Parallel, ");
544 if (bustype & BUS_LPC)
545 ret = strcat_realloc(ret, "LPC, ");
546 if (bustype & BUS_FWH)
547 ret = strcat_realloc(ret, "FWH, ");
548 if (bustype & BUS_SPI)
549 ret = strcat_realloc(ret, "SPI, ");
550 if (bustype & BUS_PROG)
551 ret = strcat_realloc(ret, "Programmer-specific, ");
552 if (bustype == BUS_NONE)
553 ret = strcat_realloc(ret, "None, ");
554 }
555 /* Kill last comma. */
556 ret[strlen(ret) - 2] = '\0';
557 ret = realloc(ret, strlen(ret) + 1);
558 return ret;
559}
560
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100561static int init_default_layout(struct flashctx *flash)
562{
563 /* Fill default layout covering the whole chip. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200564 if (flashprog_layout_new(&flash->default_layout) ||
565 flashprog_layout_add_region(flash->default_layout,
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100566 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huberc3b02dc2023-08-12 01:13:45 +0200567 flashprog_layout_include_region(flash->default_layout, "complete flash"))
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100568 return -1;
569 return 0;
570}
571
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000572int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000573{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000574 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000575 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000576 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000577
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000578 for (chip = flashchips + startchip; chip && chip->name; chip++) {
579 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000580 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000581 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000582 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000583 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300584 /* Only probe for SPI25 chips by default. */
585 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
586 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000587 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
588 if (!chip->probe && !force) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200589 msg_gdbg("failed! flashprog has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000590 continue;
591 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000592
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000593 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200594 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000595 if (!flash->chip) {
596 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100597 return -1;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000598 }
Angel Pons7e134562021-06-07 13:29:13 +0200599 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000600 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000601
Nico Huber9eec4072023-01-12 01:17:30 +0100602 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_PROBE))
603 goto free_chip;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000604
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000605 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
606 * is only called with force=1 after normal probing failed.
607 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000608 if (force)
609 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000610
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000611 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000612 goto notfound;
613
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000614 /* If this is the first chip found, accept it.
615 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000616 * a non-generic match. SFDP and CFI are generic matches.
617 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000618 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000619 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000620 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000621 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000622 msg_cinfo("===\n"
623 "SFDP has autodetected a flash chip which is "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200624 "not natively supported by flashprog yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000625 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000626 msg_cinfo("The standard operations read and "
627 "verify should work, but to support "
628 "erase, write and all other "
629 "possible features");
630 else
631 msg_cinfo("All standard operations (read, "
632 "verify, erase and write) should "
633 "work, but to support all possible "
634 "features");
635
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000636 msg_cinfo(" we need to add them manually.\n"
637 "You can help us by mailing us the output of the following command to "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200638 "flashprog@flashprog.org:\n"
639 "'flashprog -VV [plus the -p/--programmer parameter]'\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000640 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000641 "===\n");
642 }
643
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000644 /* First flash chip detected on this bus. */
645 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000646 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000647 /* Not the first flash chip detected on this bus, but not a generic match either. */
648 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
649 break;
650 /* Not the first flash chip detected on this bus, and it's just a generic match. Ignore it. */
Peter Stuge483b8f02008-09-03 23:10:05 +0000651notfound:
Nico Huber9eec4072023-01-12 01:17:30 +0100652 if (flash->chip->finish_access)
653 flash->chip->finish_access(flash);
654free_chip:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000655 free(flash->chip);
656 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000657 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000658
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000659 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000660 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000661
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100662 if (init_default_layout(flash) < 0)
663 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000664
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000665 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000666 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
667 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000668 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000669#if CONFIG_INTERNAL == 1
Nico Huber0e76d992023-01-12 20:22:55 +0100670 if (flash->physical_memory != 0 && mst->par.map_flash == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000671 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
672 PRIxPTR_WIDTH, flash->physical_memory);
673 else
674#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200675 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000676
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000677 /* Flash registers may more likely not be mapped if the chip was forced.
678 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000679 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000680 if (flash->chip->printlock)
681 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000682
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000683 /* Get out of the way for later runs. */
Nico Huber9eec4072023-01-12 01:17:30 +0100684 if (flash->chip->finish_access)
685 flash->chip->finish_access(flash);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000686
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000687 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000688 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000689}
690
Stefan Tauner96658be2014-05-26 22:05:31 +0000691/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000692static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000693{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000694 int i, j, k;
695 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530696 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000697
698 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
699 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000700 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530701 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000702
703 for (i = 0; i < NUM_ERASEREGIONS; i++) {
704 /* Blocks with zero size are bugs in flashchips.c. */
705 if (eraser.eraseblocks[i].count &&
706 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000707 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200708 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000709 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000710 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000711 }
712 /* Blocks with zero count are bugs in flashchips.c. */
713 if (!eraser.eraseblocks[i].count &&
714 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000715 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200716 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000717 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000718 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000719 }
720 done += eraser.eraseblocks[i].count *
721 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530722 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000723 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000724 /* Empty eraseblock definition with erase function. */
725 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000726 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000727 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000728 if (!done)
729 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000730 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000731 msg_gerr("ERROR: Flash chip %s erase function %i "
732 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000733 " expected 0x%06x bytes.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200734 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000735 chip->name, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000736 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000737 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000738 if (!eraser.block_erase)
739 continue;
740 /* Check if there are identical erase functions for different
741 * layouts. That would imply "magic" erase functions. The
742 * easiest way to check this is with function pointers.
743 */
Uwe Hermann43959702010-03-13 17:28:29 +0000744 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000745 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000746 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000747 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200748 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000749 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000750 ret = 1;
751 }
Uwe Hermann43959702010-03-13 17:28:29 +0000752 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530753 if(curr_eraseblock_count > prev_eraseblock_count)
754 {
Nico Huberac90af62022-12-18 00:22:47 +0000755 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200756 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000757 chip->name, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530758 ret = 1;
759 }
760 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000761 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000762 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000763}
764
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000765static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000766{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000767 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000768
769 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
770 if (log)
771 msg_cdbg("not defined. ");
772 return 1;
773 }
774 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
775 if (log)
776 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000777 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000778 return 1;
779 }
780 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
781 if (log)
782 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000783 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000784 return 1;
785 }
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530786
787 if (flash->chip->bustype == BUS_SPI) {
Nico Huber13389362024-03-05 18:35:30 +0100788 bool native_4ba;
Nico Huber13f97a52023-01-14 23:55:06 +0100789 int i;
Nico Huber13389362024-03-05 18:35:30 +0100790
791 const uint8_t *opcode = spi_get_opcode_from_erasefn(eraser.block_erase, &native_4ba);
Nico Huber13f97a52023-01-14 23:55:06 +0100792 for (i = 0; opcode[i]; i++) {
Nico Huber13389362024-03-05 18:35:30 +0100793 if ((native_4ba && !spi_master_4ba(flash)) ||
794 !flash->mst->spi.probe_opcode(flash, opcode[i])) {
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530795 if (log)
796 msg_cdbg("block erase function and layout found "
797 "but SPI master doesn't support the function. ");
798 return 1;
799 }
800 }
801 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000802 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000803 return 0;
804}
805
Nico Huber7af0e792016-04-29 16:40:15 +0200806/**
807 * @brief Reads the included layout regions into a buffer.
808 *
809 * If there is no layout set in the given flash context, the whole chip will
810 * be read.
811 *
812 * @param flashctx Flash context to be used.
813 * @param buffer Buffer of full chip size to read into.
814 * @return 0 on success,
815 * 1 if any read fails.
816 */
817static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
818{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200819 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200820 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200821
Nico Huber5ca55232019-06-15 22:29:08 +0200822 while ((entry = layout_next_included(layout, entry))) {
823 const chipoff_t region_start = entry->start;
824 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200825
826 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
827 return 1;
828 }
829 return 0;
830}
831
Nico Huber7af0e792016-04-29 16:40:15 +0200832/**
833 * @private
834 *
835 * For read-erase-write, `curcontents` and `newcontents` shall point
836 * to buffers of the chip's size. Both are supposed to be prefilled
837 * with at least the included layout regions of the current flash
838 * contents (`curcontents`) and the data to be written to the flash
839 * (`newcontents`).
840 *
841 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
842 *
843 * The `chipoff_t` values are used internally by `walk_by_layout()`.
844 */
845struct walk_info {
846 uint8_t *curcontents;
847 const uint8_t *newcontents;
848 chipoff_t region_start;
849 chipoff_t region_end;
850 chipoff_t erase_start;
851 chipoff_t erase_end;
852};
Nico Huber3b9c86d2023-01-15 22:58:06 +0100853
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530854struct eraseblock_data {
855 chipoff_t start_addr;
856 chipoff_t end_addr;
857 bool selected;
858 size_t block_num;
859 size_t first_sub_block_index;
860 size_t last_sub_block_index;
861};
862
863struct erase_layout {
864 struct eraseblock_data* layout_list;
865 size_t block_count;
866 const struct block_eraser *eraser;
867};
868
Nico Huber5ff6fdc2023-01-15 23:43:12 +0100869static bool explicit_erase(const struct walk_info *const info)
870{
871 /* For explicit erase, we are called without new contents. */
872 return !info->newcontents;
873}
874
Nico Huberd96e7032023-01-14 22:31:48 +0100875static size_t calculate_block_count(const struct block_eraser *const eraser)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530876{
Nico Huberd96e7032023-01-14 22:31:48 +0100877 size_t block_count = 0, i;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530878
Nico Huberd96e7032023-01-14 22:31:48 +0100879 for (i = 0; i < ARRAY_SIZE(eraser->eraseblocks); ++i)
880 block_count += eraser->eraseblocks[i].count;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530881
882 return block_count;
883}
884
885static void init_eraseblock(struct erase_layout *layout, size_t idx, size_t block_num,
886 chipoff_t start_addr, chipoff_t end_addr, size_t *sub_block_index)
887{
888 struct eraseblock_data *edata = &layout[idx].layout_list[block_num];
889 edata->start_addr = start_addr;
890 edata->end_addr = end_addr;
891 edata->selected = false;
892 edata->block_num = block_num;
893
894 if (!idx)
895 return;
Nico Hubera02df332023-01-14 23:06:27 +0100896 const struct erase_layout *const sub_layout = &layout[idx - 1];
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530897
898 edata->first_sub_block_index = *sub_block_index;
Nico Hubera02df332023-01-14 23:06:27 +0100899 for (; *sub_block_index < sub_layout->block_count; ++*sub_block_index) {
900 if (sub_layout->layout_list[*sub_block_index].end_addr > end_addr)
901 break;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530902 }
903 edata->last_sub_block_index = *sub_block_index - 1;
904}
905
906/*
907 * @brief Function to free the created erase_layout
908 *
909 * @param layout pointer to allocated layout
910 * @param erasefn_count number of erase functions for which the layout was created
911 *
912 */
913static void free_erase_layout(struct erase_layout *layout, unsigned int erasefn_count)
914{
Nico Huber13f97a52023-01-14 23:55:06 +0100915 size_t i;
916
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530917 if (!layout)
918 return;
Nico Huber13f97a52023-01-14 23:55:06 +0100919 for (i = 0; i < erasefn_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530920 free(layout[i].layout_list);
921 }
922 free(layout);
923}
924
925/*
926 * @brief Function to create an erase layout
927 *
928 * @param flashctx flash context
929 * @param e_layout address to the pointer to store the layout
930 * @return 0 on success,
931 * -1 if layout creation fails
932 *
933 * This function creates a layout of which erase functions erase which regions
934 * of the flash chip. This helps to optimally select the erase functions for
935 * erase/write operations.
936 */
Nico Huberc09fca42023-01-29 15:58:09 +0100937static int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530938{
939 const struct flashchip *chip = flashctx->chip;
940 const size_t erasefn_count = count_usable_erasers(flashctx);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530941
942 if (!erasefn_count) {
943 msg_gerr("No erase functions supported\n");
944 return 0;
945 }
946
Nico Huberd34864b2023-01-14 23:47:19 +0100947 struct erase_layout *layout = calloc(erasefn_count, sizeof(struct erase_layout));
948 if (!layout) {
949 msg_gerr("Out of memory!\n");
950 return -1;
951 }
952
Nico Huber13f97a52023-01-14 23:55:06 +0100953 size_t layout_idx = 0, eraser_idx;
954 for (eraser_idx = 0; eraser_idx < NUM_ERASEFUNCTIONS; eraser_idx++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530955 if (check_block_eraser(flashctx, eraser_idx, 0))
956 continue;
957
958 layout[layout_idx].eraser = &chip->block_erasers[eraser_idx];
Nico Huberd96e7032023-01-14 22:31:48 +0100959 const size_t block_count = calculate_block_count(&chip->block_erasers[eraser_idx]);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530960 size_t sub_block_index = 0;
961
962 layout[layout_idx].block_count = block_count;
963 layout[layout_idx].layout_list = (struct eraseblock_data *)calloc(block_count,
964 sizeof(struct eraseblock_data));
965
966 if (!layout[layout_idx].layout_list) {
967 free_erase_layout(layout, layout_idx);
968 return -1;
969 }
970
971 size_t block_num = 0;
972 chipoff_t start_addr = 0;
973
Nico Huber13f97a52023-01-14 23:55:06 +0100974 int i;
975 for (i = 0; block_num < block_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530976 const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];
977
Nico Huber13f97a52023-01-14 23:55:06 +0100978 size_t num;
979 for (num = 0; num < block->count; num++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530980 chipoff_t end_addr = start_addr + block->size - 1;
981 init_eraseblock(layout, layout_idx, block_num,
982 start_addr, end_addr, &sub_block_index);
983 block_num += 1;
984 start_addr = end_addr + 1;
985 }
986 }
987 layout_idx++;
988 }
989
990 *e_layout = layout;
991 return layout_idx;
992}
993
Aarya Chaumald33bea42022-07-14 12:51:14 +0530994/*
995 * @brief Function to select the list of sectors that need erasing
996 *
997 * @param flashctx flash context
998 * @param layout erase layout
999 * @param findex index of the erase function
1000 * @param block_num index of the block to erase according to the erase function index
Nico Huber00d1b9f2023-01-29 15:07:33 +01001001 * @param info current info from walking the regions
Aarya Chaumald33bea42022-07-14 12:51:14 +05301002 */
Nico Huberb11b72c2023-01-29 15:33:11 +01001003static void select_erase_functions_rec(const struct flashctx *flashctx, const struct erase_layout *layout,
Nico Huber00d1b9f2023-01-29 15:07:33 +01001004 size_t findex, size_t block_num, const struct walk_info *info)
Aarya Chaumald33bea42022-07-14 12:51:14 +05301005{
1006 struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
1007 if (!findex) {
Nico Hubercf6ff0a2023-01-29 15:45:06 +01001008 if (ll->start_addr <= info->region_end && ll->end_addr >= info->region_start) {
Nico Huber1494f8e2023-01-29 15:48:00 +01001009 if (explicit_erase(info)) {
1010 ll->selected = true;
1011 return;
1012 }
Nico Hubera6212482023-01-29 15:39:26 +01001013 const chipoff_t write_start = MAX(info->region_start, ll->start_addr);
1014 const chipoff_t write_end = MIN(info->region_end, ll->end_addr);
1015 const chipsize_t write_len = write_end - write_start + 1;
1016 const uint8_t erased_value = ERASED_VALUE(flashctx);
Nico Huber00d1b9f2023-01-29 15:07:33 +01001017 ll->selected = need_erase(
Nico Hubera6212482023-01-29 15:39:26 +01001018 info->curcontents + write_start, info->newcontents + write_start,
1019 write_len, flashctx->chip->gran, erased_value);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301020 }
1021 } else {
1022 int count = 0;
1023 const int sub_block_start = ll->first_sub_block_index;
1024 const int sub_block_end = ll->last_sub_block_index;
1025
Nico Huber13f97a52023-01-14 23:55:06 +01001026 int j;
1027 for (j = sub_block_start; j <= sub_block_end; j++) {
Nico Huberb11b72c2023-01-29 15:33:11 +01001028 select_erase_functions_rec(flashctx, layout, findex - 1, j, info);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301029 if (layout[findex - 1].layout_list[j].selected)
1030 count++;
1031 }
1032
1033 const int total_blocks = sub_block_end - sub_block_start + 1;
1034 if (count && count > total_blocks/2) {
Nico Huber00d1b9f2023-01-29 15:07:33 +01001035 if (ll->start_addr >= info->region_start && ll->end_addr <= info->region_end) {
Nico Huber13f97a52023-01-14 23:55:06 +01001036 for (j = sub_block_start; j <= sub_block_end; j++)
Aarya Chaumald33bea42022-07-14 12:51:14 +05301037 layout[findex - 1].layout_list[j].selected = false;
1038 ll->selected = true;
1039 }
1040 }
1041 }
1042}
1043
Nico Huberc09fca42023-01-29 15:58:09 +01001044static void select_erase_functions(const struct flashctx *flashctx, const struct erase_layout *layout,
Nico Huberb11b72c2023-01-29 15:33:11 +01001045 size_t erasefn_count, const struct walk_info *info)
1046{
1047 size_t block_num;
1048 for (block_num = 0; block_num < layout[erasefn_count - 1].block_count; ++block_num)
1049 select_erase_functions_rec(flashctx, layout, erasefn_count - 1, block_num, info);
1050}
1051
Nico Huber3b9c86d2023-01-15 22:58:06 +01001052static int write_range(struct flashctx *const flashctx, const chipoff_t flash_offset,
1053 const uint8_t *const curcontents, const uint8_t *const newcontents,
1054 const chipsize_t len, bool *const skipped)
1055{
1056 unsigned int writecount = 0;
1057 chipoff_t starthere = 0;
1058 chipsize_t lenhere = 0;
1059
1060 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1061 len - starthere, &starthere, flashctx->chip->gran))) {
1062 if (!writecount++)
1063 msg_cdbg("W");
1064 if (flashctx->chip->write(flashctx, newcontents + starthere,
1065 flash_offset + starthere, lenhere))
1066 return 1;
1067 starthere += lenhere;
1068 if (skipped)
1069 *skipped = false;
1070 }
1071 return 0;
1072}
1073
1074typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
Nico Huber7af0e792016-04-29 16:40:15 +02001075/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1076typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1077
1078static int walk_eraseblocks(struct flashctx *const flashctx,
Nico Huberc09fca42023-01-29 15:58:09 +01001079 struct erase_layout *const layouts,
1080 const size_t layout_count,
Nico Huber7af0e792016-04-29 16:40:15 +02001081 struct walk_info *const info,
Nico Huberc09fca42023-01-29 15:58:09 +01001082 const per_blockfn_t per_blockfn)
Nico Huber7af0e792016-04-29 16:40:15 +02001083{
1084 int ret;
1085 size_t i, j;
1086 bool first = true;
Nico Huber7af0e792016-04-29 16:40:15 +02001087
Nico Huberc09fca42023-01-29 15:58:09 +01001088 for (i = 0; i < layout_count; ++i) {
1089 const struct erase_layout *const layout = &layouts[i];
Nico Huber7af0e792016-04-29 16:40:15 +02001090
Nico Huberc09fca42023-01-29 15:58:09 +01001091 for (j = 0; j < layout->block_count; ++j) {
1092 struct eraseblock_data *const eb = &layout->layout_list[j];
1093
1094 if (eb->start_addr > info->region_end)
Nico Huber7af0e792016-04-29 16:40:15 +02001095 break;
Nico Huberc09fca42023-01-29 15:58:09 +01001096 if (eb->end_addr < info->region_start)
1097 continue;
1098 if (!eb->selected)
1099 continue;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001100
Nico Huber7af0e792016-04-29 16:40:15 +02001101 /* Print this for every block except the first one. */
1102 if (first)
1103 first = false;
1104 else
1105 msg_cdbg(", ");
Nico Huberc09fca42023-01-29 15:58:09 +01001106 msg_cdbg("0x%06x-0x%06x:", eb->start_addr, eb->end_addr);
Nico Huber7af0e792016-04-29 16:40:15 +02001107
Nico Huberc09fca42023-01-29 15:58:09 +01001108 info->erase_start = eb->start_addr;
1109 info->erase_end = eb->end_addr;
1110 ret = per_blockfn(flashctx, info, layout->eraser->block_erase);
Nico Huber7af0e792016-04-29 16:40:15 +02001111 if (ret)
1112 return ret;
Nico Huberc09fca42023-01-29 15:58:09 +01001113
1114 /* Clean the erase layout up for future use on other
1115 regions. `.selected` is the only field we alter. */
1116 eb->selected = false;
Nico Huber7af0e792016-04-29 16:40:15 +02001117 }
Nico Huber7af0e792016-04-29 16:40:15 +02001118 }
1119 msg_cdbg("\n");
1120 return 0;
1121}
1122
1123static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1124 const per_blockfn_t per_blockfn)
1125{
Nico Huberc09fca42023-01-29 15:58:09 +01001126 const bool do_erase = explicit_erase(info) || !(flashctx->chip->feature_bits & FEATURE_NO_ERASE);
Nico Huberc3b02dc2023-08-12 01:13:45 +02001127 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huberc09fca42023-01-29 15:58:09 +01001128 struct erase_layout *erase_layouts = NULL;
Nico Huber5ca55232019-06-15 22:29:08 +02001129 const struct romentry *entry = NULL;
Nico Huberc09fca42023-01-29 15:58:09 +01001130 int ret = 0, layout_count = 0;
Nico Huber7af0e792016-04-29 16:40:15 +02001131
1132 all_skipped = true;
1133 msg_cinfo("Erasing and writing flash chip... ");
1134
Nico Huberc09fca42023-01-29 15:58:09 +01001135 if (do_erase) {
1136 layout_count = create_erase_layout(flashctx, &erase_layouts);
1137 if (layout_count <= 0)
1138 return 1;
1139 }
1140
Nico Huber5ca55232019-06-15 22:29:08 +02001141 while ((entry = layout_next_included(layout, entry))) {
1142 info->region_start = entry->start;
1143 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001144
Nico Huberc09fca42023-01-29 15:58:09 +01001145 if (do_erase) {
1146 select_erase_functions(flashctx, erase_layouts, layout_count, info);
1147 ret = walk_eraseblocks(flashctx, erase_layouts, layout_count, info, per_blockfn);
1148 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001149 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001150 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001151 }
Nico Huber7af0e792016-04-29 16:40:15 +02001152 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001153
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001154 if (info->newcontents) {
1155 bool skipped = true;
1156 msg_cdbg("0x%06x-0x%06x:", info->region_start, info->region_end);
Nico Huberc09fca42023-01-29 15:58:09 +01001157 ret = write_range(flashctx, info->region_start,
1158 info->curcontents + info->region_start,
1159 info->newcontents + info->region_start,
1160 info->region_end + 1 - info->region_start, &skipped);
1161 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001162 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001163 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001164 }
1165 if (skipped) {
1166 msg_cdbg("S\n");
1167 } else {
1168 msg_cdbg("\n");
1169 all_skipped = false;
1170 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001171 }
Nico Huber7af0e792016-04-29 16:40:15 +02001172 }
1173 if (all_skipped)
1174 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1175 msg_cinfo("Erase/write done.\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001176
1177free_ret:
1178 free_erase_layout(erase_layouts, layout_count);
1179 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001180}
1181
1182static int erase_block(struct flashctx *const flashctx,
1183 const struct walk_info *const info, const erasefn_t erasefn)
1184{
1185 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001186 const bool region_unaligned = info->region_start > info->erase_start ||
1187 info->erase_end > info->region_end;
1188 uint8_t *backup_contents = NULL, *erased_contents = NULL;
Nico Huberd34af7a2023-01-15 23:58:18 +01001189 int ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001190
Nico Huber6e61e0c2019-01-23 17:07:49 +01001191 /*
1192 * If the region is not erase-block aligned, merge current flash con-
1193 * tents into a new buffer `backup_contents`.
1194 */
1195 if (region_unaligned) {
1196 backup_contents = malloc(erase_len);
1197 erased_contents = malloc(erase_len);
1198 if (!backup_contents || !erased_contents) {
1199 msg_cerr("Out of memory!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001200 goto _free_ret;
1201 }
1202 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1203 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1204
1205 msg_cdbg("R");
1206 /* Merge data preceding the current region. */
1207 if (info->region_start > info->erase_start) {
1208 const chipoff_t start = info->erase_start;
1209 const chipsize_t len = info->region_start - info->erase_start;
1210 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1211 msg_cerr("Can't read! Aborting.\n");
1212 goto _free_ret;
1213 }
1214 }
1215 /* Merge data following the current region. */
1216 if (info->erase_end > info->region_end) {
1217 const chipoff_t start = info->region_end + 1;
1218 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1219 const chipsize_t len = info->erase_end - info->region_end;
1220 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1221 msg_cerr("Can't read! Aborting.\n");
1222 goto _free_ret;
1223 }
1224 }
1225 }
1226
Nico Huber7af0e792016-04-29 16:40:15 +02001227 all_skipped = false;
1228
1229 msg_cdbg("E");
1230 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001231 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001232 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1233 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001234 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001235 }
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001236 if (info->curcontents)
1237 memset(info->curcontents + info->erase_start, ERASED_VALUE(flashctx), erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001238
1239 if (region_unaligned) {
Nico Huber3b9c86d2023-01-15 22:58:06 +01001240 if (write_range(flashctx, info->erase_start, erased_contents, backup_contents, erase_len, NULL))
1241 goto _free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001242 if (info->curcontents)
1243 memcpy(info->curcontents + info->erase_start, backup_contents, erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001244 }
1245
1246 ret = 0;
1247
1248_free_ret:
1249 free(erased_contents);
1250 free(backup_contents);
1251 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001252}
1253
1254/**
1255 * @brief Erases the included layout regions.
1256 *
1257 * If there is no layout set in the given flash context, the whole chip will
1258 * be erased.
1259 *
1260 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001261 * @return 0 on success,
1262 * 1 if all available erase functions failed.
1263 */
Nico Huber454f6132012-12-10 13:34:10 +00001264static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001265{
1266 struct walk_info info = { 0 };
1267 return walk_by_layout(flashctx, &info, &erase_block);
1268}
1269
Nico Huber7af0e792016-04-29 16:40:15 +02001270/**
1271 * @brief Writes the included layout regions from a given image.
1272 *
1273 * If there is no layout set in the given flash context, the whole image
1274 * will be written.
1275 *
1276 * @param flashctx Flash context to be used.
1277 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1278 * @param newcontents The new image to be written.
1279 * @return 0 on success,
1280 * 1 if anything has gone wrong.
1281 */
Nico Huber454f6132012-12-10 13:34:10 +00001282static int write_by_layout(struct flashctx *const flashctx,
1283 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001284{
1285 struct walk_info info;
1286 info.curcontents = curcontents;
1287 info.newcontents = newcontents;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001288 return walk_by_layout(flashctx, &info, erase_block);
Nico Huber7af0e792016-04-29 16:40:15 +02001289}
1290
1291/**
1292 * @brief Compares the included layout regions with content from a buffer.
1293 *
1294 * If there is no layout set in the given flash context, the whole chip's
1295 * contents will be compared.
1296 *
1297 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001298 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001299 * @param curcontents A buffer of full chip size to read current chip contents into.
1300 * @param newcontents The new image to compare to.
1301 * @return 0 on success,
1302 * 1 if reading failed,
1303 * 3 if the contents don't match.
1304 */
Nico Huber74d09d42019-06-16 03:27:26 +02001305static int verify_by_layout(
1306 struct flashctx *const flashctx,
Nico Huberc3b02dc2023-08-12 01:13:45 +02001307 const struct flashprog_layout *const layout,
Nico Huber74d09d42019-06-16 03:27:26 +02001308 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001309{
Nico Huber5ca55232019-06-15 22:29:08 +02001310 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001311
Nico Huber5ca55232019-06-15 22:29:08 +02001312 while ((entry = layout_next_included(layout, entry))) {
1313 const chipoff_t region_start = entry->start;
1314 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001315
1316 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1317 return 1;
1318 if (compare_range(newcontents + region_start, curcontents + region_start,
1319 region_start, region_len))
1320 return 3;
1321 }
1322 return 0;
1323}
1324
Stefan Tauner136388f2013-07-15 10:47:53 +00001325static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001326{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001327 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001328#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001329 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001330 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001331 "chip. Please report this to the mailing list at flashprog@flashprog.org or\n"
1332 "on IRC (see https://www.flashprog.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001333 "-------------------------------------------------------------------------------\n"
1334 "You may now reboot or simply leave the machine running.\n");
1335 else
1336#endif
1337 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001338 "the programmer and the flash chip. If you think the error is caused by flashprog\n"
1339 "please report this to the mailing list at flashprog@flashprog.org or on IRC\n"
1340 "(see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001341}
1342
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001343void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001344{
Stefan Tauner136388f2013-07-15 10:47:53 +00001345 msg_gerr("Your flash chip is in an unknown state.\n");
1346#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001347 if (programmer == &programmer_internal)
Nico Huberc3b02dc2023-08-12 01:13:45 +02001348 msg_gerr("Get help on IRC (see https://www.flashprog.org/Contact) or mail\n"
1349 "flashprog@flashprog.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001350 "-------------------------------------------------------------------------------\n"
1351 "DO NOT REBOOT OR POWEROFF!\n");
1352 else
1353#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +02001354 msg_gerr("Please report this to the mailing list at flashprog@flashprog.org\n"
1355 "or on IRC (see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001356}
1357
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001358void list_programmers_linebreak(int startcol, int cols, int paren)
1359{
1360 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001361 int pnamelen;
1362 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001363 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001364 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001365
Thomas Heijligend45cb592021-05-19 14:12:18 +02001366 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001367 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001368 pnamelen = strlen(pname);
1369 if (remaining - pnamelen - 2 < 0) {
1370 if (firstline)
1371 firstline = 0;
1372 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001373 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001374 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001375 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001376 remaining = cols - startcol;
1377 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001378 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001379 remaining--;
1380 }
1381 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001382 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001383 remaining--;
1384 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001385 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001386 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001387 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001388 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001389 remaining--;
1390 } else {
1391 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001392 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001393 }
1394 }
1395}
1396
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001397int selfcheck(void)
1398{
Stefan Tauner96658be2014-05-26 22:05:31 +00001399 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001400 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001401
Thomas Heijligend45cb592021-05-19 14:12:18 +02001402 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001403 const struct programmer_entry *const p = programmer_table[i];
1404 if (p == NULL) {
1405 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1406 ret = 1;
1407 continue;
1408 }
1409 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001410 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1411 ret = 1;
1412 /* This might hide other problems with this programmer, but allows for better error
1413 * messages below without jumping through hoops. */
1414 continue;
1415 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001416 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001417 case USB:
1418 case PCI:
1419 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001420 if (p->devs.note == NULL) {
1421 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001422 break; /* This one has its device list stored separately. */
1423 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001424 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001425 ret = 1;
1426 }
1427 break;
1428 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001429 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001430 ret = 1;
1431 break;
1432 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001433 if (p->init == NULL) {
1434 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001435 ret = 1;
1436 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001437 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001438
1439 /* It would be favorable if we could check for the correct layout (especially termination) of various
1440 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1441 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1442 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1443 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1444 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001445 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001446 msg_gerr("Flashchips table miscompilation!\n");
1447 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001448 } else {
1449 for (i = 0; i < flashchips_size - 1; i++) {
1450 const struct flashchip *chip = &flashchips[i];
1451 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1452 ret = 1;
1453 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001454 "Please report a bug at flashprog@flashprog.org\n", i,
Stefan Tauner96658be2014-05-26 22:05:31 +00001455 chip->name == NULL ? "unnamed" : chip->name);
1456 }
1457 if (selfcheck_eraseblocks(chip)) {
1458 ret = 1;
1459 }
1460 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001461 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001462
Stefan Tauner600576b2014-06-12 22:57:36 +00001463#if CONFIG_INTERNAL == 1
1464 ret |= selfcheck_board_enables();
1465#endif
1466
Stefan Tauner96658be2014-05-26 22:05:31 +00001467 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001468 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001469}
1470
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001471/* FIXME: This function signature needs to be improved once prepare_flash_access()
1472 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001473 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001474static int chip_safety_check(const struct flashctx *flash, int force,
1475 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001476{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001477 const struct flashchip *chip = flash->chip;
1478
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001479 if (!programmer_may_write && (write_it || erase_it)) {
1480 msg_perr("Write/erase is not working yet on your programmer in "
1481 "its current configuration.\n");
1482 /* --force is the wrong approach, but it's the best we can do
1483 * until the generic programmer parameter parser is merged.
1484 */
1485 if (!force)
1486 return 1;
1487 msg_cerr("Continuing anyway.\n");
1488 }
1489
1490 if (read_it || erase_it || write_it || verify_it) {
1491 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001492 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001493 msg_cerr("Read is not working on this chip. ");
1494 if (!force)
1495 return 1;
1496 msg_cerr("Continuing anyway.\n");
1497 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001498 if (!chip->read) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001499 msg_cerr("flashprog has no read function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001500 "flash chip.\n");
1501 return 1;
1502 }
1503 }
1504 if (erase_it || write_it) {
1505 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001506 if (chip->tested.erase == NA) {
1507 msg_cerr("Erase is not possible on this chip.\n");
1508 return 1;
1509 }
1510 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001511 msg_cerr("Erase is not working on this chip. ");
1512 if (!force)
1513 return 1;
1514 msg_cerr("Continuing anyway.\n");
1515 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001516 if(count_usable_erasers(flash) == 0) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001517 msg_cerr("flashprog has no erase function for this "
Stefan Tauner5368dca2011-07-01 00:19:12 +00001518 "flash chip.\n");
1519 return 1;
1520 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001521 }
1522 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001523 if (chip->tested.write == NA) {
1524 msg_cerr("Write is not possible on this chip.\n");
1525 return 1;
1526 }
1527 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001528 msg_cerr("Write is not working on this chip. ");
1529 if (!force)
1530 return 1;
1531 msg_cerr("Continuing anyway.\n");
1532 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001533 if (!chip->write) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001534 msg_cerr("flashprog has no write function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001535 "flash chip.\n");
1536 return 1;
1537 }
1538 }
1539 return 0;
1540}
1541
Nico Huber305f4172013-06-14 11:55:26 +02001542int prepare_flash_access(struct flashctx *const flash,
1543 const bool read_it, const bool write_it,
1544 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001545{
1546 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1547 msg_cerr("Aborting.\n");
1548 return 1;
1549 }
1550
Nico Huber3ac761c2023-01-16 02:43:17 +01001551 if (layout_sanity_checks(flash, write_it)) {
Nico Huber454f6132012-12-10 13:34:10 +00001552 msg_cerr("Requested regions can not be handled. Aborting.\n");
1553 return 1;
1554 }
1555
Nico Huber901fb952023-01-11 23:24:23 +01001556 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_FULL))
1557 return 1;
1558
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001559 /* Initialize chip_restore_fn_count before chip unlock calls. */
1560 flash->chip_restore_fn_count = 0;
1561
Nico Huber454f6132012-12-10 13:34:10 +00001562 /* Given the existence of read locks, we want to unlock for read,
1563 erase and write. */
1564 if (flash->chip->unlock)
1565 flash->chip->unlock(flash);
1566
1567 return 0;
1568}
1569
Nico Huber305f4172013-06-14 11:55:26 +02001570void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001571{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001572 deregister_chip_restore(flash);
Nico Huber901fb952023-01-11 23:24:23 +01001573 if (flash->chip->finish_access)
1574 flash->chip->finish_access(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001575}
1576
1577/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001578 * @addtogroup flashprog-flash
Nico Huber454f6132012-12-10 13:34:10 +00001579 * @{
1580 */
1581
1582/**
1583 * @brief Erase the specified ROM chip.
1584 *
1585 * If a layout is set in the given flash context, only included regions
1586 * will be erased.
1587 *
1588 * @param flashctx The context of the flash chip to erase.
1589 * @return 0 on success.
1590 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001591int flashprog_flash_erase(struct flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +00001592{
1593 if (prepare_flash_access(flashctx, false, false, true, false))
1594 return 1;
1595
1596 const int ret = erase_by_layout(flashctx);
1597
1598 finalize_flash_access(flashctx);
1599
1600 return ret;
1601}
1602
Nico Huberc3b02dc2023-08-12 01:13:45 +02001603/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +00001604
1605/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001606 * @defgroup flashprog-ops Operations
Nico Huber454f6132012-12-10 13:34:10 +00001607 * @{
1608 */
1609
1610/**
1611 * @brief Read the current image from the specified ROM chip.
1612 *
1613 * If a layout is set in the specified flash context, only included regions
1614 * will be read.
1615 *
1616 * @param flashctx The context of the flash chip.
1617 * @param buffer Target buffer to write image to.
1618 * @param buffer_len Size of target buffer in bytes.
1619 * @return 0 on success,
1620 * 2 if buffer_len is too short for the flash chip's contents,
1621 * or 1 on any other failure.
1622 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001623int flashprog_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001624{
1625 const size_t flash_size = flashctx->chip->total_size * 1024;
1626
1627 if (flash_size > buffer_len)
1628 return 2;
1629
1630 if (prepare_flash_access(flashctx, true, false, false, false))
1631 return 1;
1632
1633 msg_cinfo("Reading flash... ");
1634
1635 int ret = 1;
1636 if (read_by_layout(flashctx, buffer)) {
1637 msg_cerr("Read operation failed!\n");
1638 msg_cinfo("FAILED.\n");
1639 goto _finalize_ret;
1640 }
1641 msg_cinfo("done.\n");
1642 ret = 0;
1643
1644_finalize_ret:
1645 finalize_flash_access(flashctx);
1646 return ret;
1647}
1648
1649static void combine_image_by_layout(const struct flashctx *const flashctx,
1650 uint8_t *const newcontents, const uint8_t *const oldcontents)
1651{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001652 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001653 const struct romentry *included;
1654 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001655
Nico Huber3d7b1e32018-12-22 00:53:14 +01001656 while ((included = layout_next_included_region(layout, start))) {
1657 if (included->start > start) {
1658 /* copy everything up to the start of this included region */
1659 memcpy(newcontents + start, oldcontents + start, included->start - start);
1660 }
1661 /* skip this included region */
1662 start = included->end + 1;
1663 if (start == 0)
1664 return;
Nico Huber454f6132012-12-10 13:34:10 +00001665 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001666
1667 /* copy the rest of the chip */
1668 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1669 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001670}
1671
1672/**
1673 * @brief Write the specified image to the ROM chip.
1674 *
1675 * If a layout is set in the specified flash context, only erase blocks
1676 * containing included regions will be touched.
1677 *
1678 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001679 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001680 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001681 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001682 * @return 0 on success,
1683 * 4 if buffer_len doesn't match the size of the flash chip,
1684 * 3 if write was tried but nothing has changed,
1685 * 2 if write failed and flash contents changed,
1686 * or 1 on any other failure.
1687 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001688int flashprog_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001689 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001690{
1691 const size_t flash_size = flashctx->chip->total_size * 1024;
1692 const bool verify_all = flashctx->flags.verify_whole_chip;
1693 const bool verify = flashctx->flags.verify_after_write;
Nico Huberc3b02dc2023-08-12 01:13:45 +02001694 const struct flashprog_layout *const verify_layout =
Nico Huber74d09d42019-06-16 03:27:26 +02001695 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001696
1697 if (buffer_len != flash_size)
1698 return 4;
1699
1700 int ret = 1;
1701
1702 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001703 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001704 uint8_t *const curcontents = malloc(flash_size);
1705 uint8_t *oldcontents = NULL;
1706 if (verify_all)
1707 oldcontents = malloc(flash_size);
1708 if (!curcontents || (verify_all && !oldcontents)) {
1709 msg_gerr("Out of memory!\n");
1710 goto _free_ret;
1711 }
1712
1713#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001714 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001715 if (flashctx->flags.force_boardmismatch) {
1716 msg_pinfo("Proceeding anyway because user forced us to.\n");
1717 } else {
1718 msg_perr("Aborting. You can override this with "
1719 "-p internal:boardmismatch=force.\n");
1720 goto _free_ret;
1721 }
1722 }
1723#endif
1724
1725 if (prepare_flash_access(flashctx, false, true, false, verify))
1726 goto _free_ret;
1727
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001728 /* If given, assume flash chip contains same data as `refcontents`. */
1729 if (refcontents) {
1730 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1731 memcpy(curcontents, refcontents, flash_size);
1732 if (oldcontents)
1733 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001734 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001735 /*
1736 * Read the whole chip to be able to check whether regions need to be
1737 * erased and to give better diagnostics in case write fails.
1738 * The alternative is to read only the regions which are to be
1739 * preserved, but in that case we might perform unneeded erase which
1740 * takes time as well.
1741 */
1742 msg_cinfo("Reading old flash chip contents... ");
1743 if (verify_all) {
1744 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
1745 msg_cinfo("FAILED.\n");
1746 goto _finalize_ret;
1747 }
1748 memcpy(curcontents, oldcontents, flash_size);
1749 } else {
1750 if (read_by_layout(flashctx, curcontents)) {
1751 msg_cinfo("FAILED.\n");
1752 goto _finalize_ret;
1753 }
Nico Huber454f6132012-12-10 13:34:10 +00001754 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001755 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001756 }
Nico Huber454f6132012-12-10 13:34:10 +00001757
1758 if (write_by_layout(flashctx, curcontents, newcontents)) {
1759 msg_cerr("Uh oh. Erase/write failed. ");
1760 ret = 2;
1761 if (verify_all) {
1762 msg_cerr("Checking if anything has changed.\n");
1763 msg_cinfo("Reading current flash chip contents... ");
1764 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
1765 msg_cinfo("done.\n");
1766 if (!memcmp(oldcontents, curcontents, flash_size)) {
1767 nonfatal_help_message();
1768 goto _finalize_ret;
1769 }
1770 msg_cerr("Apparently at least some data has changed.\n");
1771 } else
1772 msg_cerr("Can't even read anymore!\n");
1773 emergency_help_message();
1774 goto _finalize_ret;
1775 } else {
1776 msg_cerr("\n");
1777 }
1778 emergency_help_message();
1779 goto _finalize_ret;
1780 }
1781
1782 /* Verify only if we actually changed something. */
1783 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001784 msg_cinfo("Verifying flash... ");
1785
1786 /* Work around chips which need some time to calm down. */
1787 programmer_delay(1000*1000);
1788
Nico Huber74d09d42019-06-16 03:27:26 +02001789 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001790 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001791 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001792 /* If we tried to write, and verification now fails, we
1793 might have an emergency situation. */
1794 if (ret)
1795 emergency_help_message();
1796 else
1797 msg_cinfo("VERIFIED.\n");
1798 } else {
1799 /* We didn't change anything. */
1800 ret = 0;
1801 }
1802
1803_finalize_ret:
1804 finalize_flash_access(flashctx);
1805_free_ret:
1806 free(oldcontents);
1807 free(curcontents);
1808 return ret;
1809}
1810
1811/**
1812 * @brief Verify the ROM chip's contents with the specified image.
1813 *
1814 * If a layout is set in the specified flash context, only included regions
1815 * will be verified.
1816 *
1817 * @param flashctx The context of the flash chip.
1818 * @param buffer Source buffer to verify with.
1819 * @param buffer_len Size of source buffer in bytes.
1820 * @return 0 on success,
1821 * 3 if the chip's contents don't match,
1822 * 2 if buffer_len doesn't match the size of the flash chip,
1823 * or 1 on any other failure.
1824 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001825int flashprog_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001826{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001827 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001828 const size_t flash_size = flashctx->chip->total_size * 1024;
1829
1830 if (buffer_len != flash_size)
1831 return 2;
1832
1833 const uint8_t *const newcontents = buffer;
1834 uint8_t *const curcontents = malloc(flash_size);
1835 if (!curcontents) {
1836 msg_gerr("Out of memory!\n");
1837 return 1;
1838 }
1839
1840 int ret = 1;
1841
1842 if (prepare_flash_access(flashctx, false, false, false, true))
1843 goto _free_ret;
1844
1845 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001846 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001847 if (!ret)
1848 msg_cinfo("VERIFIED.\n");
1849
1850 finalize_flash_access(flashctx);
1851_free_ret:
1852 free(curcontents);
1853 return ret;
1854}
1855
Nico Huberc3b02dc2023-08-12 01:13:45 +02001856/** @} */ /* end flashprog-ops */