blob: 0ab20cb388f192e909e783e33d08376e7a1ac45b [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"
Nico Huber9d09e0f2025-03-02 22:55:10 +010036#include "version.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000037
Nico Huberc3b02dc2023-08-12 01:13:45 +020038const char flashprog_version[] = FLASHPROG_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000039const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000040
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020041static const struct programmer_entry *programmer = NULL;
Nico Huber6a2ebeb2022-08-26 11:36:48 +020042static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000043
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000044/* If nonzero, used as the start address of bottom-aligned flash. */
45unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000046
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000047/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020048bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000049
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000050#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000051static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000052/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000053static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000054 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000055 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000056} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000057/* Initialize to 0 to make sure nobody registers a shutdown function before
58 * programmer init.
59 */
Felix Singerf25447e2022-08-19 02:44:28 +020060static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000061
Stefan Taunerc4f44df2013-08-12 22:58:43 +000062/* Did we change something or was every erase/write skipped (if any)? */
63static bool all_skipped = true;
64
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000065static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000066
Stefan Tauner2a1ed772014-08-31 00:09:21 +000067int shutdown_free(void *data)
68{
69 free(data);
70 return 0;
71}
72
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000073/* Register a function to be executed on programmer shutdown.
74 * The advantage over atexit() is that you can supply a void pointer which will
75 * be used as parameter to the registered function upon programmer shutdown.
76 * This pointer can point to arbitrary data used by said function, e.g. undo
77 * information for GPIO settings etc. If unneeded, set data=NULL.
78 * Please note that the first (void *data) belongs to the function signature of
79 * the function passed as first parameter.
80 */
David Hendricks8bb20212011-06-14 01:35:36 +000081int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000082{
83 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000084 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000085 SHUTDOWN_MAXFN);
86 return 1;
87 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000088 if (!may_register_shutdown) {
89 msg_perr("Tried to register a shutdown function before "
90 "programmer init.\n");
91 return 1;
92 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000093 shutdown_fn[shutdown_fn_count].func = function;
94 shutdown_fn[shutdown_fn_count].data = data;
95 shutdown_fn_count++;
96
97 return 0;
98}
99
Nikolai Artemiev4ad48642020-11-05 13:54:27 +1100100int register_chip_restore(chip_restore_fn_cb_t func,
101 struct flashctx *flash, uint8_t status)
102{
103 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
104 msg_perr("Tried to register more than %i chip restore"
105 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
106 return 1;
107 }
108 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
109 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
110 flash->chip_restore_fn_count++;
111
112 return 0;
113}
114
115static int deregister_chip_restore(struct flashctx *flash)
116{
117 int rc = 0;
118
119 while (flash->chip_restore_fn_count > 0) {
120 int i = --flash->chip_restore_fn_count;
121 rc |= flash->chip_restore_fn[i].func(
122 flash, flash->chip_restore_fn[i].status);
123 }
124
125 return rc;
126}
127
Nico Huber2b66ad92023-01-11 20:15:15 +0100128int programmer_init(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000129{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000130 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000131
Nico Huber2b66ad92023-01-11 20:15:15 +0100132 if (prog == NULL || prog->driver == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000133 msg_perr("Invalid programmer specified!\n");
134 return -1;
135 }
Nico Huber2b66ad92023-01-11 20:15:15 +0100136 programmer = prog->driver;
137 programmer_param = prog->param;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000138 /* Initialize all programmer specific data. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000139 /* Default to top aligned flash at 4 GB. */
140 flashbase = 0;
141 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200142 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000143 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200144 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000145
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200146 msg_pdbg("Initializing %s programmer\n", programmer->name);
Nico Hubere3a26882023-01-11 21:45:51 +0100147 ret = programmer->init(prog);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000148 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000149 if (ret != 0) {
150 /* It is quite possible that any unhandled programmer parameter would have been valid,
151 * but an error in actual programmer init happened before the parameter was evaluated.
152 */
153 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
154 programmer_param);
155 } else {
156 /* Actual programmer init was successful, but the user specified an invalid or unusable
157 * (for the current programmer configuration) parameter.
158 */
159 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
160 msg_perr("Aborting.\n");
161 ret = ERROR_FATAL;
162 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000163 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200164 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000165 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000166}
167
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000168/** Calls registered shutdown functions and resets internal programmer-related variables.
169 * Calling it is safe even without previous initialization, but further interactions with programmer support
170 * require a call to programmer_init() (afterwards).
171 *
172 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Nico Huber2b66ad92023-01-11 20:15:15 +0100173int programmer_shutdown(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000174{
David Hendricks8bb20212011-06-14 01:35:36 +0000175 int ret = 0;
176
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000177 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200178 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000179 while (shutdown_fn_count > 0) {
180 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000181 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000182 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000183 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000184
David Hendricks8bb20212011-06-14 01:35:36 +0000185 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000186}
187
Stefan Taunerf80419c2014-05-02 15:41:42 +0000188void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000189{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000190 if (usecs > 0) {
191 if (programmer->delay)
192 programmer->delay(usecs);
193 else
194 internal_delay(usecs);
195 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000196}
197
Nico Hubercdcfda22023-04-29 13:29:33 +0200198static int read_memmapped_chunk(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000199{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000200 chip_readn(flash, buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000201 return 0;
202}
Nico Hubercdcfda22023-04-29 13:29:33 +0200203int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
204{
205 return flashprog_read_chunked(flash, buf, start, len, MAX_DATA_READ_UNLIMITED, read_memmapped_chunk);
206}
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000207
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000208/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000209 * It will look for needle with a subsequent '=' in haystack, return a copy of
210 * needle and remove everything from the first occurrence of needle to the next
211 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000212 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200213static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000214{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000215 char *param_pos, *opt_pos, *rest;
216 char *opt = NULL;
217 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000218 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000219
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000220 needlelen = strlen(needle);
221 if (!needlelen) {
222 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200223 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000224 return NULL;
225 }
226 /* No programmer parameters given. */
227 if (*haystack == NULL)
228 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000229 param_pos = strstr(*haystack, needle);
230 do {
231 if (!param_pos)
232 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000233 /* Needle followed by '='? */
234 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000235 /* Beginning of the string? */
236 if (param_pos == *haystack)
237 break;
238 /* After a delimiter? */
239 if (strchr(delim, *(param_pos - 1)))
240 break;
241 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000242 /* Continue searching. */
243 param_pos++;
244 param_pos = strstr(param_pos, needle);
245 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000246
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000247 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000248 /* Get the string after needle and '='. */
249 opt_pos = param_pos + needlelen + 1;
250 optlen = strcspn(opt_pos, delim);
251 /* Return an empty string if the parameter was empty. */
252 opt = malloc(optlen + 1);
253 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000254 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000255 exit(1);
256 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000257 strncpy(opt, opt_pos, optlen);
258 opt[optlen] = '\0';
259 rest = opt_pos + optlen;
260 /* Skip all delimiters after the current parameter. */
261 rest += strspn(rest, delim);
262 memmove(param_pos, rest, strlen(rest) + 1);
263 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000264 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000265
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000266 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000267}
268
Stefan Tauner66652442011-06-26 17:38:17 +0000269char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000270{
271 return extract_param(&programmer_param, param_name, ",");
272}
273
Richard Hughes842d6782021-01-15 09:48:12 +0000274static void flashprog_progress_report(struct flashprog_progress *const p)
275{
276 if (p->current > p->total) {
277 msg_gdbg2("Sanitizing progress report: %zu bytes off.", p->current - p->total);
278 p->current = p->total;
279 }
280
281 if (!p->callback)
282 return;
283
284 p->callback(p->stage, p->current, p->total, p->user_data);
285}
286
287static void flashprog_progress_start(struct flashprog_flashctx *const flashctx,
288 const enum flashprog_progress_stage stage, const size_t total)
289{
290 flashctx->progress.stage = stage;
291 flashctx->progress.current = 0;
292 flashctx->progress.total = total;
293 flashprog_progress_report(&flashctx->progress);
294}
295
296static void flashprog_progress_start_by_layout(struct flashprog_flashctx *const flashctx,
297 const enum flashprog_progress_stage stage,
298 const struct flashprog_layout *const layout)
299{
300 const struct romentry *entry = NULL;
301 size_t total = 0;
302
303 while ((entry = layout_next_included(layout, entry)))
304 total += entry->end - entry->start + 1;
305
306 flashprog_progress_start(flashctx, stage, total);
307}
308
309static void flashprog_progress_set(struct flashprog_flashctx *const flashctx, const size_t current)
310{
311 flashctx->progress.current = current;
312 flashprog_progress_report(&flashctx->progress);
313}
314
315/** @private */
316void flashprog_progress_add(struct flashprog_flashctx *const flashctx, const size_t progress)
317{
318 flashctx->progress.current += progress;
319 flashprog_progress_report(&flashctx->progress);
320}
321
322static void flashprog_progress_finish(struct flashprog_flashctx *const flashctx)
323{
324 if (flashctx->progress.current == flashctx->progress.total)
325 return;
326
327 flashctx->progress.current = flashctx->progress.total;
328 flashprog_progress_report(&flashctx->progress);
329}
330
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000331/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000332static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000333{
334 unsigned int usable_erasefunctions = 0;
335 int k;
336 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
337 if (!check_block_eraser(flash, k, 0))
338 usable_erasefunctions++;
339 }
340 return usable_erasefunctions;
341}
342
Mark Marshallf20b7be2014-05-09 21:16:21 +0000343static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000344{
345 int ret = 0, failcount = 0;
346 unsigned int i;
347 for (i = 0; i < len; i++) {
348 if (wantbuf[i] != havebuf[i]) {
349 /* Only print the first failure. */
350 if (!failcount++)
351 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
352 start + i, wantbuf[i], havebuf[i]);
353 }
354 }
355 if (failcount) {
356 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
357 start, start + len - 1, failcount);
358 ret = -1;
359 }
360 return ret;
361}
362
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000363/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600364static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000365{
366 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300367 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000368
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100369 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000370 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100371 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100372 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000373 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300374 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000375 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100376
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000377 free(cmpbuf);
378 return ret;
379}
380
Richard Hughes842d6782021-01-15 09:48:12 +0000381int flashprog_read_range(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
382{
383 flashprog_progress_start(flash, FLASHPROG_PROGRESS_READ, len);
384 const int ret = flash->chip->read(flash, buf, start, len);
385 flashprog_progress_finish(flash);
386 return ret;
387}
388
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000389/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000390 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000391 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000392 * @start offset to the base address of the flash chip
393 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000394 * @return 0 for success, -1 for failure
395 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000396int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000397{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000398 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000399 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000400
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100401 if (start + len > flash->chip->total_size * 1024) {
402 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
403 " total_size 0x%x\n", __func__, start, len,
404 flash->chip->total_size * 1024);
405 return -1;
406 }
407
Stefan Taunerdf64a422014-05-27 00:06:14 +0000408 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000409 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100410 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000411 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000412 }
413
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100414 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000415 if (ret) {
416 msg_gerr("Verification impossible because read failed "
417 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000418 ret = -1;
419 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000420 }
421
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000422 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000423out_free:
424 free(readbuf);
425 return ret;
426}
427
Nico Huber3ac761c2023-01-16 02:43:17 +0100428size_t gran_to_bytes(const enum write_granularity gran)
Nico Huberb77607f2023-01-16 02:25:45 +0100429{
430 switch (gran) {
431 case write_gran_1bit: return 1;
432 case write_gran_1byte: return 1;
433 case write_gran_1byte_implicit_erase: return 1;
434 case write_gran_128bytes: return 128;
435 case write_gran_256bytes: return 256;
436 case write_gran_264bytes: return 264;
437 case write_gran_512bytes: return 512;
438 case write_gran_528bytes: return 528;
439 case write_gran_1024bytes: return 1024;
440 case write_gran_1056bytes: return 1056;
441 default: return 0;
442 }
443}
444
Stefan Tauner02437452013-04-01 19:34:53 +0000445/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300446static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
447 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000448{
449 unsigned int i, j, limit;
450 for (j = 0; j < len / gran; j++) {
451 limit = min (gran, len - j * gran);
452 /* Are 'have' and 'want' identical? */
453 if (!memcmp(have + j * gran, want + j * gran, limit))
454 continue;
455 /* have needs to be in erased state. */
456 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300457 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000458 return 1;
459 }
460 return 0;
461}
462
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000463/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000464 * Check if the buffer @have can be programmed to the content of @want without
465 * erasing. This is only possible if all chunks of size @gran are either kept
466 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000467 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000468 * Warning: This function assumes that @have and @want point to naturally
469 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000470 *
471 * @have buffer with current content
472 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000473 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000474 * @gran write granularity (enum, not count)
475 * @return 0 if no erase is needed, 1 otherwise
476 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000477static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
478 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000479{
Stefan Tauner02437452013-04-01 19:34:53 +0000480 unsigned int i;
Nico Huberb77607f2023-01-16 02:25:45 +0100481 size_t stride;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000482
483 switch (gran) {
484 case write_gran_1bit:
Nico Huberb77607f2023-01-16 02:25:45 +0100485 for (i = 0; i < len; i++) {
486 if ((have[i] & want[i]) != want[i])
487 return 1;
488 }
489 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000490 case write_gran_1byte:
Nico Huberb77607f2023-01-16 02:25:45 +0100491 for (i = 0; i < len; i++) {
492 if ((have[i] != want[i]) && (have[i] != erased_value))
493 return 1;
494 }
495 return 0;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000496 case write_gran_1byte_implicit_erase:
497 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
Nico Huberb77607f2023-01-16 02:25:45 +0100498 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000499 default:
Nico Huberb77607f2023-01-16 02:25:45 +0100500 stride = gran_to_bytes(gran);
501 if (stride) {
502 return need_erase_gran_bytes(have, want, len, stride, erased_value);
503 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000504 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200505 "flashprog@flashprog.org\n", __func__);
Nico Huberb77607f2023-01-16 02:25:45 +0100506 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000507 }
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000508}
509
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000510/**
511 * Check if the buffer @have needs to be programmed to get the content of @want.
512 * If yes, return 1 and fill in first_start with the start address of the
513 * write operation and first_len with the length of the first to-be-written
514 * chunk. If not, return 0 and leave first_start and first_len undefined.
515 *
516 * Warning: This function assumes that @have and @want point to naturally
517 * aligned regions.
518 *
519 * @have buffer with current content
520 * @want buffer with desired content
521 * @len length of the checked area
522 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000523 * @first_start offset of the first byte which needs to be written (passed in
524 * value is increased by the offset of the first needed write
525 * relative to have/want or unchanged if no write is needed)
526 * @return length of the first contiguous area which needs to be written
527 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000528 *
529 * FIXME: This function needs a parameter which tells it about coalescing
530 * in relation to the max write length of the programmer and the max write
531 * length of the chip.
532 */
Nico Huber3b9c86d2023-01-15 22:58:06 +0100533static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, chipsize_t len,
534 chipoff_t *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000535{
Felix Singerf25447e2022-08-19 02:44:28 +0200536 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000537 unsigned int rel_start = 0, first_len = 0;
Nico Huberb77607f2023-01-16 02:25:45 +0100538 unsigned int i, limit;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000539
Nico Huberb77607f2023-01-16 02:25:45 +0100540 const size_t stride = gran_to_bytes(gran);
541 if (!stride) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000542 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200543 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000544 /* Claim that no write was needed. A write with unknown
545 * granularity is too dangerous to try.
546 */
547 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000548 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000549 for (i = 0; i < len / stride; i++) {
550 limit = min(stride, len - i * stride);
551 /* Are 'have' and 'want' identical? */
552 if (memcmp(have + i * stride, want + i * stride, limit)) {
553 if (!need_write) {
554 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200555 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000556 rel_start = i * stride;
557 }
558 } else {
559 if (need_write) {
560 /* First location where have and want
561 * do not differ anymore.
562 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000563 break;
564 }
565 }
566 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000567 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000568 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000569 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000570 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000571}
572
Nico Huber2d625722016-05-03 10:48:02 +0200573/*
574 * Return a string corresponding to the bustype parameter.
575 * Memory is obtained with malloc() and must be freed with free() by the caller.
576 */
577char *flashbuses_to_text(enum chipbustype bustype)
578{
579 char *ret = calloc(1, 1);
580 /*
581 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
582 * will cease to exist and should be eliminated here as well.
583 */
584 if (bustype == BUS_NONSPI) {
585 ret = strcat_realloc(ret, "Non-SPI, ");
586 } else {
587 if (bustype & BUS_PARALLEL)
588 ret = strcat_realloc(ret, "Parallel, ");
589 if (bustype & BUS_LPC)
590 ret = strcat_realloc(ret, "LPC, ");
591 if (bustype & BUS_FWH)
592 ret = strcat_realloc(ret, "FWH, ");
593 if (bustype & BUS_SPI)
594 ret = strcat_realloc(ret, "SPI, ");
595 if (bustype & BUS_PROG)
596 ret = strcat_realloc(ret, "Programmer-specific, ");
597 if (bustype == BUS_NONE)
598 ret = strcat_realloc(ret, "None, ");
599 }
600 /* Kill last comma. */
601 ret[strlen(ret) - 2] = '\0';
602 ret = realloc(ret, strlen(ret) + 1);
603 return ret;
604}
605
Nico Huberee8cf1c2025-01-02 23:17:25 +0100606int probe_noop(struct flashctx *flash)
607{
608 return 1;
609}
610
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100611static int init_default_layout(struct flashctx *flash)
612{
613 /* Fill default layout covering the whole chip. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200614 if (flashprog_layout_new(&flash->default_layout) ||
615 flashprog_layout_add_region(flash->default_layout,
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100616 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huberc3b02dc2023-08-12 01:13:45 +0200617 flashprog_layout_include_region(flash->default_layout, "complete flash"))
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100618 return -1;
619 return 0;
620}
621
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000622int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000623{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000624 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000625 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000626 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000627
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000628 for (chip = flashchips + startchip; chip && chip->name; chip++) {
629 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000630 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000631 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000632 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000633 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300634 /* Only probe for SPI25 chips by default. */
635 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
636 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000637 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
638 if (!chip->probe && !force) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200639 msg_gdbg("failed! flashprog has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000640 continue;
641 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000642
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000643 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200644 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000645 if (!flash->chip) {
646 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100647 return -1;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000648 }
Angel Pons7e134562021-06-07 13:29:13 +0200649 *flash->chip = *chip;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100650 flash->mst.par = &mst->par; /* both `mst` are unions, so we need only one pointer */
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000651
Nico Huber5469c152026-02-12 22:56:52 +0100652 /* If we probe for a specific chip, we can adapt the voltage early. */
653 if (chip_to_probe && flash->mst.common->adapt_voltage) {
654 if (flash->mst.common->adapt_voltage(flash->mst.common,
655 chip->voltage.min, chip->voltage.max))
656 goto free_chip;
657 }
658
Nico Huber9eec4072023-01-12 01:17:30 +0100659 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_PROBE))
660 goto free_chip;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000661
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000662 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
663 * is only called with force=1 after normal probing failed.
664 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000665 if (force)
666 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000667
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000668 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000669 goto notfound;
670
Nico Huberb89c4522024-12-04 23:30:13 +0100671 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_POST_PROBE))
672 goto notfound;
673
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000674 /* If this is the first chip found, accept it.
675 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000676 * a non-generic match. SFDP and CFI are generic matches.
677 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000678 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000679 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000680 */
Nico Huber11136c22023-05-01 12:00:09 +0200681 if (startchip == 0 && flash->chip->id.model == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000682 msg_cinfo("===\n"
683 "SFDP has autodetected a flash chip which is "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200684 "not natively supported by flashprog yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000685 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000686 msg_cinfo("The standard operations read and "
687 "verify should work, but to support "
688 "erase, write and all other "
689 "possible features");
690 else
691 msg_cinfo("All standard operations (read, "
692 "verify, erase and write) should "
693 "work, but to support all possible "
694 "features");
695
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000696 msg_cinfo(" we need to add them manually.\n"
697 "You can help us by mailing us the output of the following command to "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200698 "flashprog@flashprog.org:\n"
699 "'flashprog -VV [plus the -p/--programmer parameter]'\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000700 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000701 "===\n");
702 }
703
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000704 /* First flash chip detected on this bus. */
705 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000706 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000707 /* Not the first flash chip detected on this bus, but not a generic match either. */
Nico Huber11136c22023-05-01 12:00:09 +0200708 if ((flash->chip->id.model != GENERIC_DEVICE_ID) && (flash->chip->id.model != SFDP_DEVICE_ID))
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000709 break;
710 /* 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 +0000711notfound:
Nico Huber9eec4072023-01-12 01:17:30 +0100712 if (flash->chip->finish_access)
713 flash->chip->finish_access(flash);
714free_chip:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000715 free(flash->chip);
716 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000717 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000718
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000719 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000720 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000721
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100722 if (init_default_layout(flash) < 0)
723 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000724
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000725 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000726 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
727 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000728 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000729#if CONFIG_INTERNAL == 1
Nico Huber0e76d992023-01-12 20:22:55 +0100730 if (flash->physical_memory != 0 && mst->par.map_flash == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000731 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
732 PRIxPTR_WIDTH, flash->physical_memory);
733 else
734#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200735 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000736
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000737 /* Flash registers may more likely not be mapped if the chip was forced.
738 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000739 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000740 if (flash->chip->printlock)
741 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000742
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000743 /* Get out of the way for later runs. */
Nico Huber9eec4072023-01-12 01:17:30 +0100744 if (flash->chip->finish_access)
745 flash->chip->finish_access(flash);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000746
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000747 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000748 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000749}
750
Stefan Tauner96658be2014-05-26 22:05:31 +0000751/* Even if an error is found, the function will keep going and check the rest. */
Nico Huber72d0ada2026-01-26 18:28:20 +0100752static int selfcheck_eraseblocks(const struct flashchip *chip, const char *label)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000753{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000754 int i, j, k;
755 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530756 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000757
758 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
759 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000760 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530761 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000762
763 for (i = 0; i < NUM_ERASEREGIONS; i++) {
764 /* Blocks with zero size are bugs in flashchips.c. */
765 if (eraser.eraseblocks[i].count &&
766 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000767 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200768 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100769 label, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000770 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000771 }
772 /* Blocks with zero count are bugs in flashchips.c. */
773 if (!eraser.eraseblocks[i].count &&
774 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000775 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200776 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100777 label, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000778 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000779 }
780 done += eraser.eraseblocks[i].count *
781 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530782 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000783 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000784 /* Empty eraseblock definition with erase function. */
785 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000786 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000787 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000788 if (!done)
789 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000790 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000791 msg_gerr("ERROR: Flash chip %s erase function %i "
792 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000793 " expected 0x%06x bytes.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200794 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100795 label, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000796 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000797 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000798 if (!eraser.block_erase)
799 continue;
800 /* Check if there are identical erase functions for different
801 * layouts. That would imply "magic" erase functions. The
802 * easiest way to check this is with function pointers.
803 */
Uwe Hermann43959702010-03-13 17:28:29 +0000804 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000805 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000806 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000807 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200808 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100809 label, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000810 ret = 1;
811 }
Uwe Hermann43959702010-03-13 17:28:29 +0000812 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530813 if(curr_eraseblock_count > prev_eraseblock_count)
814 {
Nico Huberac90af62022-12-18 00:22:47 +0000815 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200816 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100817 label, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530818 ret = 1;
819 }
820 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000821 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000822 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000823}
824
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000825static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000826{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000827 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000828
829 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
830 if (log)
831 msg_cdbg("not defined. ");
832 return 1;
833 }
834 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
835 if (log)
836 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000837 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000838 return 1;
839 }
840 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
841 if (log)
842 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000843 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000844 return 1;
845 }
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530846
Nico Hubere149fbe2024-08-21 10:23:53 +0200847 if (flash->chip->bustype == BUS_SPI && flash->chip->spi_cmd_set == SPI25) {
Nico Huber13389362024-03-05 18:35:30 +0100848 bool native_4ba;
Nico Huber13f97a52023-01-14 23:55:06 +0100849 int i;
Nico Huber13389362024-03-05 18:35:30 +0100850
851 const uint8_t *opcode = spi_get_opcode_from_erasefn(eraser.block_erase, &native_4ba);
Nico Huber07ebc682024-08-21 10:18:27 +0200852 if (!opcode)
853 return 1;
854
Nico Huber13f97a52023-01-14 23:55:06 +0100855 for (i = 0; opcode[i]; i++) {
Nico Huber13389362024-03-05 18:35:30 +0100856 if ((native_4ba && !spi_master_4ba(flash)) ||
Nico Huber9a11cbf2023-01-13 01:19:07 +0100857 !flash->mst.spi->probe_opcode(flash, opcode[i])) {
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530858 if (log)
859 msg_cdbg("block erase function and layout found "
860 "but SPI master doesn't support the function. ");
861 return 1;
862 }
863 }
864 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000865 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000866 return 0;
867}
868
Nico Huber7af0e792016-04-29 16:40:15 +0200869/**
870 * @brief Reads the included layout regions into a buffer.
871 *
872 * If there is no layout set in the given flash context, the whole chip will
873 * be read.
874 *
875 * @param flashctx Flash context to be used.
876 * @param buffer Buffer of full chip size to read into.
877 * @return 0 on success,
878 * 1 if any read fails.
879 */
880static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
881{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200882 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200883 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200884
Richard Hughes842d6782021-01-15 09:48:12 +0000885 flashprog_progress_start_by_layout(flashctx, FLASHPROG_PROGRESS_READ, layout);
886
Nico Huber5ca55232019-06-15 22:29:08 +0200887 while ((entry = layout_next_included(layout, entry))) {
888 const chipoff_t region_start = entry->start;
889 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200890
891 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
892 return 1;
893 }
Richard Hughes842d6782021-01-15 09:48:12 +0000894
895 flashprog_progress_finish(flashctx);
896
Nico Huber7af0e792016-04-29 16:40:15 +0200897 return 0;
898}
899
Nico Huber7af0e792016-04-29 16:40:15 +0200900/**
901 * @private
902 *
903 * For read-erase-write, `curcontents` and `newcontents` shall point
904 * to buffers of the chip's size. Both are supposed to be prefilled
905 * with at least the included layout regions of the current flash
906 * contents (`curcontents`) and the data to be written to the flash
907 * (`newcontents`).
908 *
909 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
910 *
911 * The `chipoff_t` values are used internally by `walk_by_layout()`.
912 */
913struct walk_info {
914 uint8_t *curcontents;
915 const uint8_t *newcontents;
916 chipoff_t region_start;
917 chipoff_t region_end;
918 chipoff_t erase_start;
919 chipoff_t erase_end;
920};
Nico Huber3b9c86d2023-01-15 22:58:06 +0100921
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530922struct eraseblock_data {
923 chipoff_t start_addr;
924 chipoff_t end_addr;
925 bool selected;
926 size_t block_num;
927 size_t first_sub_block_index;
928 size_t last_sub_block_index;
929};
930
931struct erase_layout {
932 struct eraseblock_data* layout_list;
933 size_t block_count;
934 const struct block_eraser *eraser;
935};
936
Nico Huber5ff6fdc2023-01-15 23:43:12 +0100937static bool explicit_erase(const struct walk_info *const info)
938{
939 /* For explicit erase, we are called without new contents. */
940 return !info->newcontents;
941}
942
Nico Huberd96e7032023-01-14 22:31:48 +0100943static size_t calculate_block_count(const struct block_eraser *const eraser)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530944{
Nico Huberd96e7032023-01-14 22:31:48 +0100945 size_t block_count = 0, i;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530946
Nico Huberd96e7032023-01-14 22:31:48 +0100947 for (i = 0; i < ARRAY_SIZE(eraser->eraseblocks); ++i)
948 block_count += eraser->eraseblocks[i].count;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530949
950 return block_count;
951}
952
953static void init_eraseblock(struct erase_layout *layout, size_t idx, size_t block_num,
954 chipoff_t start_addr, chipoff_t end_addr, size_t *sub_block_index)
955{
956 struct eraseblock_data *edata = &layout[idx].layout_list[block_num];
957 edata->start_addr = start_addr;
958 edata->end_addr = end_addr;
959 edata->selected = false;
960 edata->block_num = block_num;
961
962 if (!idx)
963 return;
Nico Hubera02df332023-01-14 23:06:27 +0100964 const struct erase_layout *const sub_layout = &layout[idx - 1];
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530965
966 edata->first_sub_block_index = *sub_block_index;
Nico Hubera02df332023-01-14 23:06:27 +0100967 for (; *sub_block_index < sub_layout->block_count; ++*sub_block_index) {
968 if (sub_layout->layout_list[*sub_block_index].end_addr > end_addr)
969 break;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530970 }
971 edata->last_sub_block_index = *sub_block_index - 1;
972}
973
974/*
975 * @brief Function to free the created erase_layout
976 *
977 * @param layout pointer to allocated layout
978 * @param erasefn_count number of erase functions for which the layout was created
979 *
980 */
981static void free_erase_layout(struct erase_layout *layout, unsigned int erasefn_count)
982{
Nico Huber13f97a52023-01-14 23:55:06 +0100983 size_t i;
984
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530985 if (!layout)
986 return;
Nico Huber13f97a52023-01-14 23:55:06 +0100987 for (i = 0; i < erasefn_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530988 free(layout[i].layout_list);
989 }
990 free(layout);
991}
992
993/*
994 * @brief Function to create an erase layout
995 *
996 * @param flashctx flash context
997 * @param e_layout address to the pointer to store the layout
998 * @return 0 on success,
999 * -1 if layout creation fails
1000 *
1001 * This function creates a layout of which erase functions erase which regions
1002 * of the flash chip. This helps to optimally select the erase functions for
1003 * erase/write operations.
1004 */
Nico Huberc09fca42023-01-29 15:58:09 +01001005static int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301006{
1007 const struct flashchip *chip = flashctx->chip;
1008 const size_t erasefn_count = count_usable_erasers(flashctx);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301009
1010 if (!erasefn_count) {
1011 msg_gerr("No erase functions supported\n");
1012 return 0;
1013 }
1014
Nico Huberd34864b2023-01-14 23:47:19 +01001015 struct erase_layout *layout = calloc(erasefn_count, sizeof(struct erase_layout));
1016 if (!layout) {
1017 msg_gerr("Out of memory!\n");
1018 return -1;
1019 }
1020
Nico Huber13f97a52023-01-14 23:55:06 +01001021 size_t layout_idx = 0, eraser_idx;
1022 for (eraser_idx = 0; eraser_idx < NUM_ERASEFUNCTIONS; eraser_idx++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301023 if (check_block_eraser(flashctx, eraser_idx, 0))
1024 continue;
1025
1026 layout[layout_idx].eraser = &chip->block_erasers[eraser_idx];
Nico Huberd96e7032023-01-14 22:31:48 +01001027 const size_t block_count = calculate_block_count(&chip->block_erasers[eraser_idx]);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301028 size_t sub_block_index = 0;
1029
1030 layout[layout_idx].block_count = block_count;
1031 layout[layout_idx].layout_list = (struct eraseblock_data *)calloc(block_count,
1032 sizeof(struct eraseblock_data));
1033
1034 if (!layout[layout_idx].layout_list) {
1035 free_erase_layout(layout, layout_idx);
1036 return -1;
1037 }
1038
1039 size_t block_num = 0;
1040 chipoff_t start_addr = 0;
1041
Nico Huber13f97a52023-01-14 23:55:06 +01001042 int i;
1043 for (i = 0; block_num < block_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301044 const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];
1045
Nico Huber13f97a52023-01-14 23:55:06 +01001046 size_t num;
1047 for (num = 0; num < block->count; num++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301048 chipoff_t end_addr = start_addr + block->size - 1;
1049 init_eraseblock(layout, layout_idx, block_num,
1050 start_addr, end_addr, &sub_block_index);
1051 block_num += 1;
1052 start_addr = end_addr + 1;
1053 }
1054 }
1055 layout_idx++;
1056 }
1057
1058 *e_layout = layout;
1059 return layout_idx;
1060}
1061
Nico Huber20073e72024-05-10 01:31:40 +02001062static void deselect_erase_block_rec(const struct erase_layout *layout, size_t findex, size_t block_num)
1063{
1064 struct eraseblock_data *const ed = &layout[findex].layout_list[block_num];
1065 size_t i;
1066
1067 if (ed->selected) {
1068 ed->selected = false;
1069 } else if (findex > 0) {
1070 for (i = ed->first_sub_block_index; i <= ed->last_sub_block_index; ++i)
1071 deselect_erase_block_rec(layout, findex - 1, i);
1072 }
1073}
1074
Aarya Chaumald33bea42022-07-14 12:51:14 +05301075/*
1076 * @brief Function to select the list of sectors that need erasing
1077 *
1078 * @param flashctx flash context
1079 * @param layout erase layout
1080 * @param findex index of the erase function
1081 * @param block_num index of the block to erase according to the erase function index
Nico Huber00d1b9f2023-01-29 15:07:33 +01001082 * @param info current info from walking the regions
Nico Huberaa714dd2023-04-22 14:59:33 +02001083 * @return number of bytes selected for erase
Aarya Chaumald33bea42022-07-14 12:51:14 +05301084 */
Nico Huberaa714dd2023-04-22 14:59:33 +02001085static size_t select_erase_functions_rec(const struct flashctx *flashctx, const struct erase_layout *layout,
1086 size_t findex, size_t block_num, const struct walk_info *info)
Aarya Chaumald33bea42022-07-14 12:51:14 +05301087{
1088 struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
Nico Huberaa714dd2023-04-22 14:59:33 +02001089 const size_t eraseblock_size = ll->end_addr - ll->start_addr + 1;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301090 if (!findex) {
Nico Hubercf6ff0a2023-01-29 15:45:06 +01001091 if (ll->start_addr <= info->region_end && ll->end_addr >= info->region_start) {
Nico Huber1494f8e2023-01-29 15:48:00 +01001092 if (explicit_erase(info)) {
1093 ll->selected = true;
Nico Huberaa714dd2023-04-22 14:59:33 +02001094 return eraseblock_size;
Nico Huber1494f8e2023-01-29 15:48:00 +01001095 }
Nico Hubera6212482023-01-29 15:39:26 +01001096 const chipoff_t write_start = MAX(info->region_start, ll->start_addr);
1097 const chipoff_t write_end = MIN(info->region_end, ll->end_addr);
1098 const chipsize_t write_len = write_end - write_start + 1;
1099 const uint8_t erased_value = ERASED_VALUE(flashctx);
Nico Huber00d1b9f2023-01-29 15:07:33 +01001100 ll->selected = need_erase(
Nico Hubera6212482023-01-29 15:39:26 +01001101 info->curcontents + write_start, info->newcontents + write_start,
1102 write_len, flashctx->chip->gran, erased_value);
Nico Huberaa714dd2023-04-22 14:59:33 +02001103 if (ll->selected)
1104 return eraseblock_size;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301105 }
Nico Huberaa714dd2023-04-22 14:59:33 +02001106 return 0;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301107 } else {
Aarya Chaumald33bea42022-07-14 12:51:14 +05301108 const int sub_block_start = ll->first_sub_block_index;
1109 const int sub_block_end = ll->last_sub_block_index;
Nico Huberaa714dd2023-04-22 14:59:33 +02001110 size_t bytes = 0;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301111
Nico Huber13f97a52023-01-14 23:55:06 +01001112 int j;
Nico Huberaa714dd2023-04-22 14:59:33 +02001113 for (j = sub_block_start; j <= sub_block_end; j++)
1114 bytes += select_erase_functions_rec(flashctx, layout, findex - 1, j, info);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301115
Nico Huberaa714dd2023-04-22 14:59:33 +02001116 if (bytes > eraseblock_size / 2) {
Nico Huber00d1b9f2023-01-29 15:07:33 +01001117 if (ll->start_addr >= info->region_start && ll->end_addr <= info->region_end) {
Nico Huber20073e72024-05-10 01:31:40 +02001118 deselect_erase_block_rec(layout, findex, block_num);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301119 ll->selected = true;
Nico Huberaa714dd2023-04-22 14:59:33 +02001120 bytes = eraseblock_size;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301121 }
1122 }
Nico Huberaa714dd2023-04-22 14:59:33 +02001123 return bytes;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301124 }
1125}
1126
Nico Huberaa714dd2023-04-22 14:59:33 +02001127static size_t select_erase_functions(const struct flashctx *flashctx, const struct erase_layout *layout,
1128 size_t erasefn_count, const struct walk_info *info)
Nico Huberb11b72c2023-01-29 15:33:11 +01001129{
Nico Huberaa714dd2023-04-22 14:59:33 +02001130 size_t bytes = 0;
Nico Huberb11b72c2023-01-29 15:33:11 +01001131 size_t block_num;
1132 for (block_num = 0; block_num < layout[erasefn_count - 1].block_count; ++block_num)
Nico Huberaa714dd2023-04-22 14:59:33 +02001133 bytes += select_erase_functions_rec(flashctx, layout, erasefn_count - 1, block_num, info);
1134 return bytes;
Nico Huberb11b72c2023-01-29 15:33:11 +01001135}
1136
Nico Huber3b9c86d2023-01-15 22:58:06 +01001137static int write_range(struct flashctx *const flashctx, const chipoff_t flash_offset,
1138 const uint8_t *const curcontents, const uint8_t *const newcontents,
1139 const chipsize_t len, bool *const skipped)
1140{
1141 unsigned int writecount = 0;
1142 chipoff_t starthere = 0;
1143 chipsize_t lenhere = 0;
1144
1145 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1146 len - starthere, &starthere, flashctx->chip->gran))) {
1147 if (!writecount++)
1148 msg_cdbg("W");
1149 if (flashctx->chip->write(flashctx, newcontents + starthere,
1150 flash_offset + starthere, lenhere))
1151 return 1;
1152 starthere += lenhere;
Richard Hughes842d6782021-01-15 09:48:12 +00001153 if (skipped) {
1154 flashprog_progress_set(flashctx, starthere);
Nico Huber3b9c86d2023-01-15 22:58:06 +01001155 *skipped = false;
Richard Hughes842d6782021-01-15 09:48:12 +00001156 }
Nico Huber3b9c86d2023-01-15 22:58:06 +01001157 }
1158 return 0;
1159}
1160
1161typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
Nico Huber7af0e792016-04-29 16:40:15 +02001162/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1163typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1164
1165static int walk_eraseblocks(struct flashctx *const flashctx,
Nico Huberc09fca42023-01-29 15:58:09 +01001166 struct erase_layout *const layouts,
1167 const size_t layout_count,
Nico Huber7af0e792016-04-29 16:40:15 +02001168 struct walk_info *const info,
Nico Huberc09fca42023-01-29 15:58:09 +01001169 const per_blockfn_t per_blockfn)
Nico Huber7af0e792016-04-29 16:40:15 +02001170{
1171 int ret;
1172 size_t i, j;
1173 bool first = true;
Nico Huber7af0e792016-04-29 16:40:15 +02001174
Nico Huberc09fca42023-01-29 15:58:09 +01001175 for (i = 0; i < layout_count; ++i) {
1176 const struct erase_layout *const layout = &layouts[i];
Nico Huber7af0e792016-04-29 16:40:15 +02001177
Nico Huberc09fca42023-01-29 15:58:09 +01001178 for (j = 0; j < layout->block_count; ++j) {
1179 struct eraseblock_data *const eb = &layout->layout_list[j];
1180
1181 if (eb->start_addr > info->region_end)
Nico Huber7af0e792016-04-29 16:40:15 +02001182 break;
Nico Huberc09fca42023-01-29 15:58:09 +01001183 if (eb->end_addr < info->region_start)
1184 continue;
1185 if (!eb->selected)
1186 continue;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001187
Nico Huber7af0e792016-04-29 16:40:15 +02001188 /* Print this for every block except the first one. */
1189 if (first)
1190 first = false;
1191 else
1192 msg_cdbg(", ");
Nico Huberc09fca42023-01-29 15:58:09 +01001193 msg_cdbg("0x%06x-0x%06x:", eb->start_addr, eb->end_addr);
Nico Huber7af0e792016-04-29 16:40:15 +02001194
Nico Huberc09fca42023-01-29 15:58:09 +01001195 info->erase_start = eb->start_addr;
1196 info->erase_end = eb->end_addr;
1197 ret = per_blockfn(flashctx, info, layout->eraser->block_erase);
Nico Huber7af0e792016-04-29 16:40:15 +02001198 if (ret)
1199 return ret;
Nico Huberc09fca42023-01-29 15:58:09 +01001200
1201 /* Clean the erase layout up for future use on other
1202 regions. `.selected` is the only field we alter. */
1203 eb->selected = false;
Nico Huber7af0e792016-04-29 16:40:15 +02001204 }
Nico Huber7af0e792016-04-29 16:40:15 +02001205 }
1206 msg_cdbg("\n");
1207 return 0;
1208}
1209
1210static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1211 const per_blockfn_t per_blockfn)
1212{
Nico Huberc09fca42023-01-29 15:58:09 +01001213 const bool do_erase = explicit_erase(info) || !(flashctx->chip->feature_bits & FEATURE_NO_ERASE);
Nico Huberc3b02dc2023-08-12 01:13:45 +02001214 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huberc09fca42023-01-29 15:58:09 +01001215 struct erase_layout *erase_layouts = NULL;
Nico Huber5ca55232019-06-15 22:29:08 +02001216 const struct romentry *entry = NULL;
Nico Huberc09fca42023-01-29 15:58:09 +01001217 int ret = 0, layout_count = 0;
Nico Huber7af0e792016-04-29 16:40:15 +02001218
1219 all_skipped = true;
Nico Huberc6a924a2024-09-04 15:09:52 +02001220 msg_cinfo("Erasing %sflash chip... ", info->newcontents ? "and writing " : "");
Nico Huber7af0e792016-04-29 16:40:15 +02001221
Nico Huberc09fca42023-01-29 15:58:09 +01001222 if (do_erase) {
1223 layout_count = create_erase_layout(flashctx, &erase_layouts);
1224 if (layout_count <= 0)
1225 return 1;
1226 }
1227
Nico Huber5ca55232019-06-15 22:29:08 +02001228 while ((entry = layout_next_included(layout, entry))) {
1229 info->region_start = entry->start;
1230 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001231
Nico Huberc09fca42023-01-29 15:58:09 +01001232 if (do_erase) {
Richard Hughes842d6782021-01-15 09:48:12 +00001233 const size_t total = select_erase_functions(flashctx, erase_layouts, layout_count, info);
1234
1235 /* We verify every erased block manually. Technically that's
1236 reading, but accounting for it as part of the erase helps
1237 to provide a smooth, overall progress. Hence `total * 2`. */
1238 flashprog_progress_start(flashctx, FLASHPROG_PROGRESS_ERASE, total * 2);
1239
Nico Huberc09fca42023-01-29 15:58:09 +01001240 ret = walk_eraseblocks(flashctx, erase_layouts, layout_count, info, per_blockfn);
1241 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001242 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001243 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001244 }
Richard Hughes842d6782021-01-15 09:48:12 +00001245
1246 flashprog_progress_finish(flashctx);
Nico Huber7af0e792016-04-29 16:40:15 +02001247 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001248
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001249 if (info->newcontents) {
1250 bool skipped = true;
1251 msg_cdbg("0x%06x-0x%06x:", info->region_start, info->region_end);
Richard Hughes842d6782021-01-15 09:48:12 +00001252 flashprog_progress_start(flashctx, FLASHPROG_PROGRESS_WRITE,
1253 info->region_end - info->region_start + 1);
Nico Huberc09fca42023-01-29 15:58:09 +01001254 ret = write_range(flashctx, info->region_start,
1255 info->curcontents + info->region_start,
1256 info->newcontents + info->region_start,
1257 info->region_end + 1 - info->region_start, &skipped);
1258 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001259 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001260 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001261 }
Richard Hughes842d6782021-01-15 09:48:12 +00001262 flashprog_progress_finish(flashctx);
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001263 if (skipped) {
1264 msg_cdbg("S\n");
1265 } else {
1266 msg_cdbg("\n");
1267 all_skipped = false;
1268 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001269 }
Nico Huber7af0e792016-04-29 16:40:15 +02001270 }
1271 if (all_skipped)
1272 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
Nico Huberc6a924a2024-09-04 15:09:52 +02001273 msg_cinfo("Erase%s done.\n", info->newcontents ? "/write" : "");
Nico Huberc09fca42023-01-29 15:58:09 +01001274
1275free_ret:
1276 free_erase_layout(erase_layouts, layout_count);
1277 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001278}
1279
1280static int erase_block(struct flashctx *const flashctx,
1281 const struct walk_info *const info, const erasefn_t erasefn)
1282{
1283 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001284 const bool region_unaligned = info->region_start > info->erase_start ||
1285 info->erase_end > info->region_end;
1286 uint8_t *backup_contents = NULL, *erased_contents = NULL;
Nico Huberd34af7a2023-01-15 23:58:18 +01001287 int ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001288
Nico Huber6e61e0c2019-01-23 17:07:49 +01001289 /*
1290 * If the region is not erase-block aligned, merge current flash con-
1291 * tents into a new buffer `backup_contents`.
1292 */
1293 if (region_unaligned) {
1294 backup_contents = malloc(erase_len);
1295 erased_contents = malloc(erase_len);
1296 if (!backup_contents || !erased_contents) {
1297 msg_cerr("Out of memory!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001298 goto _free_ret;
1299 }
1300 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1301 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1302
1303 msg_cdbg("R");
1304 /* Merge data preceding the current region. */
1305 if (info->region_start > info->erase_start) {
1306 const chipoff_t start = info->erase_start;
1307 const chipsize_t len = info->region_start - info->erase_start;
1308 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1309 msg_cerr("Can't read! Aborting.\n");
1310 goto _free_ret;
1311 }
1312 }
1313 /* Merge data following the current region. */
1314 if (info->erase_end > info->region_end) {
1315 const chipoff_t start = info->region_end + 1;
1316 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1317 const chipsize_t len = info->erase_end - info->region_end;
1318 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1319 msg_cerr("Can't read! Aborting.\n");
1320 goto _free_ret;
1321 }
1322 }
1323 }
1324
Nico Huber7af0e792016-04-29 16:40:15 +02001325 all_skipped = false;
1326
1327 msg_cdbg("E");
1328 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001329 goto _free_ret;
Richard Hughes842d6782021-01-15 09:48:12 +00001330 flashprog_progress_add(flashctx, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001331 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1332 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001333 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001334 }
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001335 if (info->curcontents)
1336 memset(info->curcontents + info->erase_start, ERASED_VALUE(flashctx), erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001337
1338 if (region_unaligned) {
Nico Huber3b9c86d2023-01-15 22:58:06 +01001339 if (write_range(flashctx, info->erase_start, erased_contents, backup_contents, erase_len, NULL))
1340 goto _free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001341 if (info->curcontents)
1342 memcpy(info->curcontents + info->erase_start, backup_contents, erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001343 }
1344
1345 ret = 0;
1346
1347_free_ret:
1348 free(erased_contents);
1349 free(backup_contents);
1350 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001351}
1352
1353/**
1354 * @brief Erases the included layout regions.
1355 *
1356 * If there is no layout set in the given flash context, the whole chip will
1357 * be erased.
1358 *
1359 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001360 * @return 0 on success,
1361 * 1 if all available erase functions failed.
1362 */
Nico Huber454f6132012-12-10 13:34:10 +00001363static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001364{
1365 struct walk_info info = { 0 };
1366 return walk_by_layout(flashctx, &info, &erase_block);
1367}
1368
Nico Huber7af0e792016-04-29 16:40:15 +02001369/**
1370 * @brief Writes the included layout regions from a given image.
1371 *
1372 * If there is no layout set in the given flash context, the whole image
1373 * will be written.
1374 *
1375 * @param flashctx Flash context to be used.
1376 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1377 * @param newcontents The new image to be written.
1378 * @return 0 on success,
1379 * 1 if anything has gone wrong.
1380 */
Nico Huber454f6132012-12-10 13:34:10 +00001381static int write_by_layout(struct flashctx *const flashctx,
1382 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001383{
1384 struct walk_info info;
1385 info.curcontents = curcontents;
1386 info.newcontents = newcontents;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001387 return walk_by_layout(flashctx, &info, erase_block);
Nico Huber7af0e792016-04-29 16:40:15 +02001388}
1389
1390/**
1391 * @brief Compares the included layout regions with content from a buffer.
1392 *
1393 * If there is no layout set in the given flash context, the whole chip's
1394 * contents will be compared.
1395 *
1396 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001397 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001398 * @param curcontents A buffer of full chip size to read current chip contents into.
1399 * @param newcontents The new image to compare to.
1400 * @return 0 on success,
1401 * 1 if reading failed,
1402 * 3 if the contents don't match.
1403 */
Nico Huber74d09d42019-06-16 03:27:26 +02001404static int verify_by_layout(
1405 struct flashctx *const flashctx,
Nico Huberc3b02dc2023-08-12 01:13:45 +02001406 const struct flashprog_layout *const layout,
Nico Huber74d09d42019-06-16 03:27:26 +02001407 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001408{
Nico Huber5ca55232019-06-15 22:29:08 +02001409 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001410
Richard Hughes842d6782021-01-15 09:48:12 +00001411 flashprog_progress_start_by_layout(flashctx, FLASHPROG_PROGRESS_READ, layout);
1412
Nico Huber5ca55232019-06-15 22:29:08 +02001413 while ((entry = layout_next_included(layout, entry))) {
1414 const chipoff_t region_start = entry->start;
1415 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001416
1417 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1418 return 1;
1419 if (compare_range(newcontents + region_start, curcontents + region_start,
1420 region_start, region_len))
1421 return 3;
1422 }
Richard Hughes842d6782021-01-15 09:48:12 +00001423
1424 flashprog_progress_finish(flashctx);
1425
Nico Huber7af0e792016-04-29 16:40:15 +02001426 return 0;
1427}
1428
Stefan Tauner136388f2013-07-15 10:47:53 +00001429static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001430{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001431 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001432#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001433 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001434 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001435 "chip. Please report this to the mailing list at flashprog@flashprog.org or\n"
1436 "on IRC (see https://www.flashprog.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001437 "-------------------------------------------------------------------------------\n"
1438 "You may now reboot or simply leave the machine running.\n");
1439 else
1440#endif
1441 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001442 "the programmer and the flash chip. If you think the error is caused by flashprog\n"
1443 "please report this to the mailing list at flashprog@flashprog.org or on IRC\n"
1444 "(see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001445}
1446
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001447void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001448{
Stefan Tauner136388f2013-07-15 10:47:53 +00001449 msg_gerr("Your flash chip is in an unknown state.\n");
1450#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001451 if (programmer == &programmer_internal)
Nico Huberc3b02dc2023-08-12 01:13:45 +02001452 msg_gerr("Get help on IRC (see https://www.flashprog.org/Contact) or mail\n"
1453 "flashprog@flashprog.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001454 "-------------------------------------------------------------------------------\n"
1455 "DO NOT REBOOT OR POWEROFF!\n");
1456 else
1457#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +02001458 msg_gerr("Please report this to the mailing list at flashprog@flashprog.org\n"
1459 "or on IRC (see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001460}
1461
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001462void list_programmers_linebreak(int startcol, int cols, int paren)
1463{
1464 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001465 int pnamelen;
1466 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001467 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001468 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001469
Thomas Heijligend45cb592021-05-19 14:12:18 +02001470 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001471 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001472 pnamelen = strlen(pname);
1473 if (remaining - pnamelen - 2 < 0) {
1474 if (firstline)
1475 firstline = 0;
1476 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001477 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001478 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001479 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001480 remaining = cols - startcol;
1481 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001482 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001483 remaining--;
1484 }
1485 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001486 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001487 remaining--;
1488 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001489 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001490 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001491 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001492 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001493 remaining--;
1494 } else {
1495 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001496 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001497 }
1498 }
1499}
1500
Nico Huber32fa5082026-01-26 18:26:49 +01001501int selfcheck_chip(const struct flashchip *const chip, const int idx)
1502{
1503 int ret = 0;
Nico Huber72d0ada2026-01-26 18:28:20 +01001504 char label[80];
Nico Huber32fa5082026-01-26 18:26:49 +01001505 const char *const name = chip->name != NULL ? chip->name : "unnamed";
1506
Nico Huber72d0ada2026-01-26 18:28:20 +01001507 if (idx >= 0)
1508 snprintf(label, sizeof(label), "#%d (%s)", idx, name);
1509 else
1510 snprintf(label, sizeof(label), "%s", name);
1511
Nico Huber11136c22023-05-01 12:00:09 +02001512 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE ||
1513 chip->id.type == ID_FIXME) {
Nico Huber32fa5082026-01-26 18:26:49 +01001514 ret = 1;
Nico Huber72d0ada2026-01-26 18:28:20 +01001515 msg_gerr("ERROR: Some field of flash chip %s is misconfigured.\n"
1516 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001517 }
1518 if (chip->feature_bits &
1519 (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7 |
1520 FEATURE_ANY_DUAL | FEATURE_ANY_QUAD)
1521 && !chip->prepare_access) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001522 msg_gerr("ERROR: Flash chip %s misses chip\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001523 "preparation function for 4BA and multi-i/o modes.\n"
Nico Huber72d0ada2026-01-26 18:28:20 +01001524 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001525 ret = 1;
1526 }
1527 uint8_t zero_cycles[sizeof(chip->dummy_cycles)] = { 0 };
1528 if ((chip->feature_bits & (FEATURE_QPI_35_F5 | FEATURE_QPI_38_FF)) &&
1529 !memcmp(&chip->dummy_cycles, zero_cycles, sizeof(zero_cycles))) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001530 msg_gerr("ERROR: Flash chip %s misses QPI dummy-cycle\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001531 "settings. Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +01001532 label);
Nico Huber32fa5082026-01-26 18:26:49 +01001533 ret = 1;
1534 }
1535 if (chip->reg_bits.bp[0].reg != INVALID_REG &&
1536 (!chip->wp_write_cfg || !chip->wp_read_cfg ||
1537 !chip->wp_get_ranges || !chip->decode_range)) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001538 msg_gerr("ERROR: Flash chip %s advertises block-protection\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001539 "bits, but misses one or more write-protection functions.\n"
Nico Huber72d0ada2026-01-26 18:28:20 +01001540 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001541 ret = 1;
1542 }
Nico Huber72d0ada2026-01-26 18:28:20 +01001543 if (selfcheck_eraseblocks(chip, label)) {
Nico Huber32fa5082026-01-26 18:26:49 +01001544 ret = 1;
1545 }
1546
1547 return ret;
1548}
1549
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001550int selfcheck(void)
1551{
Stefan Tauner96658be2014-05-26 22:05:31 +00001552 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001553 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001554
Thomas Heijligend45cb592021-05-19 14:12:18 +02001555 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001556 const struct programmer_entry *const p = programmer_table[i];
1557 if (p == NULL) {
1558 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1559 ret = 1;
1560 continue;
1561 }
1562 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001563 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1564 ret = 1;
1565 /* This might hide other problems with this programmer, but allows for better error
1566 * messages below without jumping through hoops. */
1567 continue;
1568 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001569 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001570 case USB:
1571 case PCI:
1572 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001573 if (p->devs.note == NULL) {
1574 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001575 break; /* This one has its device list stored separately. */
1576 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001577 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001578 ret = 1;
1579 }
1580 break;
1581 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001582 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001583 ret = 1;
1584 break;
1585 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001586 if (p->init == NULL) {
1587 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001588 ret = 1;
1589 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001590 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001591
1592 /* It would be favorable if we could check for the correct layout (especially termination) of various
1593 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1594 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1595 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1596 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1597 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001598 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001599 msg_gerr("Flashchips table miscompilation!\n");
1600 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001601 } else {
1602 for (i = 0; i < flashchips_size - 1; i++) {
Nico Huber32fa5082026-01-26 18:26:49 +01001603 if (selfcheck_chip(&flashchips[i], i))
Stefan Tauner96658be2014-05-26 22:05:31 +00001604 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001605 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001606 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001607
Stefan Tauner600576b2014-06-12 22:57:36 +00001608#if CONFIG_INTERNAL == 1
1609 ret |= selfcheck_board_enables();
1610#endif
1611
Stefan Tauner96658be2014-05-26 22:05:31 +00001612 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001613 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001614}
1615
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001616/* FIXME: This function signature needs to be improved once prepare_flash_access()
1617 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001618 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001619static int chip_safety_check(const struct flashctx *flash, int force,
1620 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001621{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001622 const struct flashchip *chip = flash->chip;
1623
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001624 if (!programmer_may_write && (write_it || erase_it)) {
1625 msg_perr("Write/erase is not working yet on your programmer in "
1626 "its current configuration.\n");
1627 /* --force is the wrong approach, but it's the best we can do
1628 * until the generic programmer parameter parser is merged.
1629 */
1630 if (!force)
1631 return 1;
1632 msg_cerr("Continuing anyway.\n");
1633 }
1634
1635 if (read_it || erase_it || write_it || verify_it) {
1636 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001637 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001638 msg_cerr("Read is not working on this chip. ");
1639 if (!force)
1640 return 1;
1641 msg_cerr("Continuing anyway.\n");
1642 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001643 if (!chip->read) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001644 msg_cerr("flashprog has no read function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001645 "flash chip.\n");
1646 return 1;
1647 }
1648 }
1649 if (erase_it || write_it) {
1650 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001651 if (chip->tested.erase == NA) {
1652 msg_cerr("Erase is not possible on this chip.\n");
1653 return 1;
1654 }
1655 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001656 msg_cerr("Erase is not working on this chip. ");
1657 if (!force)
1658 return 1;
1659 msg_cerr("Continuing anyway.\n");
1660 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001661 if(count_usable_erasers(flash) == 0) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001662 msg_cerr("flashprog has no erase function for this "
Stefan Tauner5368dca2011-07-01 00:19:12 +00001663 "flash chip.\n");
1664 return 1;
1665 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001666 }
1667 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001668 if (chip->tested.write == NA) {
1669 msg_cerr("Write is not possible on this chip.\n");
1670 return 1;
1671 }
1672 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001673 msg_cerr("Write is not working on this chip. ");
1674 if (!force)
1675 return 1;
1676 msg_cerr("Continuing anyway.\n");
1677 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001678 if (!chip->write) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001679 msg_cerr("flashprog has no write function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001680 "flash chip.\n");
1681 return 1;
1682 }
1683 }
1684 return 0;
1685}
1686
Nico Huber305f4172013-06-14 11:55:26 +02001687int prepare_flash_access(struct flashctx *const flash,
1688 const bool read_it, const bool write_it,
1689 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001690{
1691 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1692 msg_cerr("Aborting.\n");
1693 return 1;
1694 }
1695
Nico Huber3ac761c2023-01-16 02:43:17 +01001696 if (layout_sanity_checks(flash, write_it)) {
Nico Huber454f6132012-12-10 13:34:10 +00001697 msg_cerr("Requested regions can not be handled. Aborting.\n");
1698 return 1;
1699 }
1700
Nico Huber5469c152026-02-12 22:56:52 +01001701 if (flash->mst.common->adapt_voltage) {
1702 if (flash->mst.common->adapt_voltage(flash->mst.common,
1703 flash->chip->voltage.min, flash->chip->voltage.max))
1704 return 1;
1705 }
1706
Nico Huber901fb952023-01-11 23:24:23 +01001707 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_FULL))
1708 return 1;
1709
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001710 /* Initialize chip_restore_fn_count before chip unlock calls. */
1711 flash->chip_restore_fn_count = 0;
1712
Nico Huber454f6132012-12-10 13:34:10 +00001713 /* Given the existence of read locks, we want to unlock for read,
1714 erase and write. */
1715 if (flash->chip->unlock)
1716 flash->chip->unlock(flash);
1717
1718 return 0;
1719}
1720
Nico Huber305f4172013-06-14 11:55:26 +02001721void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001722{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001723 deregister_chip_restore(flash);
Nico Huber901fb952023-01-11 23:24:23 +01001724 if (flash->chip->finish_access)
1725 flash->chip->finish_access(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001726}
1727
1728/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001729 * @addtogroup flashprog-flash
Nico Huber454f6132012-12-10 13:34:10 +00001730 * @{
1731 */
1732
1733/**
1734 * @brief Erase the specified ROM chip.
1735 *
1736 * If a layout is set in the given flash context, only included regions
1737 * will be erased.
1738 *
1739 * @param flashctx The context of the flash chip to erase.
1740 * @return 0 on success.
1741 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001742int flashprog_flash_erase(struct flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +00001743{
1744 if (prepare_flash_access(flashctx, false, false, true, false))
1745 return 1;
1746
1747 const int ret = erase_by_layout(flashctx);
1748
1749 finalize_flash_access(flashctx);
1750
1751 return ret;
1752}
1753
Nico Huberc3b02dc2023-08-12 01:13:45 +02001754/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +00001755
1756/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001757 * @defgroup flashprog-ops Operations
Nico Huber454f6132012-12-10 13:34:10 +00001758 * @{
1759 */
1760
1761/**
1762 * @brief Read the current image from the specified ROM chip.
1763 *
1764 * If a layout is set in the specified flash context, only included regions
1765 * will be read.
1766 *
1767 * @param flashctx The context of the flash chip.
1768 * @param buffer Target buffer to write image to.
1769 * @param buffer_len Size of target buffer in bytes.
1770 * @return 0 on success,
1771 * 2 if buffer_len is too short for the flash chip's contents,
1772 * or 1 on any other failure.
1773 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001774int flashprog_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001775{
1776 const size_t flash_size = flashctx->chip->total_size * 1024;
1777
1778 if (flash_size > buffer_len)
1779 return 2;
1780
1781 if (prepare_flash_access(flashctx, true, false, false, false))
1782 return 1;
1783
1784 msg_cinfo("Reading flash... ");
1785
1786 int ret = 1;
1787 if (read_by_layout(flashctx, buffer)) {
1788 msg_cerr("Read operation failed!\n");
1789 msg_cinfo("FAILED.\n");
1790 goto _finalize_ret;
1791 }
1792 msg_cinfo("done.\n");
1793 ret = 0;
1794
1795_finalize_ret:
1796 finalize_flash_access(flashctx);
1797 return ret;
1798}
1799
1800static void combine_image_by_layout(const struct flashctx *const flashctx,
1801 uint8_t *const newcontents, const uint8_t *const oldcontents)
1802{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001803 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001804 const struct romentry *included;
1805 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001806
Nico Huber3d7b1e32018-12-22 00:53:14 +01001807 while ((included = layout_next_included_region(layout, start))) {
1808 if (included->start > start) {
1809 /* copy everything up to the start of this included region */
1810 memcpy(newcontents + start, oldcontents + start, included->start - start);
1811 }
1812 /* skip this included region */
1813 start = included->end + 1;
1814 if (start == 0)
1815 return;
Nico Huber454f6132012-12-10 13:34:10 +00001816 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001817
1818 /* copy the rest of the chip */
1819 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1820 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001821}
1822
1823/**
1824 * @brief Write the specified image to the ROM chip.
1825 *
1826 * If a layout is set in the specified flash context, only erase blocks
1827 * containing included regions will be touched.
1828 *
1829 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001830 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001831 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001832 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001833 * @return 0 on success,
1834 * 4 if buffer_len doesn't match the size of the flash chip,
1835 * 3 if write was tried but nothing has changed,
1836 * 2 if write failed and flash contents changed,
1837 * or 1 on any other failure.
1838 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001839int flashprog_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001840 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001841{
1842 const size_t flash_size = flashctx->chip->total_size * 1024;
1843 const bool verify_all = flashctx->flags.verify_whole_chip;
1844 const bool verify = flashctx->flags.verify_after_write;
Nico Huberc3b02dc2023-08-12 01:13:45 +02001845 const struct flashprog_layout *const verify_layout =
Nico Huber74d09d42019-06-16 03:27:26 +02001846 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001847
1848 if (buffer_len != flash_size)
1849 return 4;
1850
1851 int ret = 1;
1852
1853 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001854 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001855 uint8_t *const curcontents = malloc(flash_size);
1856 uint8_t *oldcontents = NULL;
1857 if (verify_all)
1858 oldcontents = malloc(flash_size);
1859 if (!curcontents || (verify_all && !oldcontents)) {
1860 msg_gerr("Out of memory!\n");
1861 goto _free_ret;
1862 }
1863
1864#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001865 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001866 if (flashctx->flags.force_boardmismatch) {
1867 msg_pinfo("Proceeding anyway because user forced us to.\n");
1868 } else {
1869 msg_perr("Aborting. You can override this with "
1870 "-p internal:boardmismatch=force.\n");
1871 goto _free_ret;
1872 }
1873 }
1874#endif
1875
1876 if (prepare_flash_access(flashctx, false, true, false, verify))
1877 goto _free_ret;
1878
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001879 /* If given, assume flash chip contains same data as `refcontents`. */
1880 if (refcontents) {
1881 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1882 memcpy(curcontents, refcontents, flash_size);
1883 if (oldcontents)
1884 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001885 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001886 /*
1887 * Read the whole chip to be able to check whether regions need to be
1888 * erased and to give better diagnostics in case write fails.
1889 * The alternative is to read only the regions which are to be
1890 * preserved, but in that case we might perform unneeded erase which
1891 * takes time as well.
1892 */
1893 msg_cinfo("Reading old flash chip contents... ");
1894 if (verify_all) {
Richard Hughes842d6782021-01-15 09:48:12 +00001895 if (flashprog_read_range(flashctx, oldcontents, 0, flash_size)) {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001896 msg_cinfo("FAILED.\n");
1897 goto _finalize_ret;
1898 }
1899 memcpy(curcontents, oldcontents, flash_size);
1900 } else {
1901 if (read_by_layout(flashctx, curcontents)) {
1902 msg_cinfo("FAILED.\n");
1903 goto _finalize_ret;
1904 }
Nico Huber454f6132012-12-10 13:34:10 +00001905 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001906 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001907 }
Nico Huber454f6132012-12-10 13:34:10 +00001908
1909 if (write_by_layout(flashctx, curcontents, newcontents)) {
1910 msg_cerr("Uh oh. Erase/write failed. ");
1911 ret = 2;
1912 if (verify_all) {
1913 msg_cerr("Checking if anything has changed.\n");
1914 msg_cinfo("Reading current flash chip contents... ");
Richard Hughes842d6782021-01-15 09:48:12 +00001915 if (!flashprog_read_range(flashctx, curcontents, 0, flash_size)) {
Nico Huber454f6132012-12-10 13:34:10 +00001916 msg_cinfo("done.\n");
1917 if (!memcmp(oldcontents, curcontents, flash_size)) {
1918 nonfatal_help_message();
1919 goto _finalize_ret;
1920 }
1921 msg_cerr("Apparently at least some data has changed.\n");
1922 } else
1923 msg_cerr("Can't even read anymore!\n");
1924 emergency_help_message();
1925 goto _finalize_ret;
1926 } else {
1927 msg_cerr("\n");
1928 }
1929 emergency_help_message();
1930 goto _finalize_ret;
1931 }
1932
1933 /* Verify only if we actually changed something. */
1934 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001935 msg_cinfo("Verifying flash... ");
1936
Nico Huber74d09d42019-06-16 03:27:26 +02001937 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001938 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001939 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001940 /* If we tried to write, and verification now fails, we
1941 might have an emergency situation. */
1942 if (ret)
1943 emergency_help_message();
1944 else
1945 msg_cinfo("VERIFIED.\n");
1946 } else {
1947 /* We didn't change anything. */
1948 ret = 0;
1949 }
1950
1951_finalize_ret:
1952 finalize_flash_access(flashctx);
1953_free_ret:
1954 free(oldcontents);
1955 free(curcontents);
1956 return ret;
1957}
1958
1959/**
1960 * @brief Verify the ROM chip's contents with the specified image.
1961 *
1962 * If a layout is set in the specified flash context, only included regions
1963 * will be verified.
1964 *
1965 * @param flashctx The context of the flash chip.
1966 * @param buffer Source buffer to verify with.
1967 * @param buffer_len Size of source buffer in bytes.
1968 * @return 0 on success,
1969 * 3 if the chip's contents don't match,
1970 * 2 if buffer_len doesn't match the size of the flash chip,
1971 * or 1 on any other failure.
1972 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001973int flashprog_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001974{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001975 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001976 const size_t flash_size = flashctx->chip->total_size * 1024;
1977
1978 if (buffer_len != flash_size)
1979 return 2;
1980
1981 const uint8_t *const newcontents = buffer;
1982 uint8_t *const curcontents = malloc(flash_size);
1983 if (!curcontents) {
1984 msg_gerr("Out of memory!\n");
1985 return 1;
1986 }
1987
1988 int ret = 1;
1989
1990 if (prepare_flash_access(flashctx, false, false, false, true))
1991 goto _free_ret;
1992
1993 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001994 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001995 if (!ret)
1996 msg_cinfo("VERIFIED.\n");
1997
1998 finalize_flash_access(flashctx);
1999_free_ret:
2000 free(curcontents);
2001 return ret;
2002}
2003
Nico Huberc3b02dc2023-08-12 01:13:45 +02002004/** @} */ /* end flashprog-ops */