blob: 2726298a997a5436cf1e8e80dd5aaae749ef8cc8 [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 Huberfbc41d22026-02-22 23:04:01 +010035#include "chipdrivers/spi.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 }
Nico Huberaf9d7382023-05-01 13:33:26 +0200183
184 int i;
185 for (i = 0; i < registered_master_count; ++i) {
186 struct found_id *found_id, *next;
187 for (found_id = registered_masters[i].found_ids; found_id; found_id = next) {
188 next = found_id->next;
189 free(found_id);
190 }
191 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000192 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000193
David Hendricks8bb20212011-06-14 01:35:36 +0000194 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000195}
196
Stefan Taunerf80419c2014-05-02 15:41:42 +0000197void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000198{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000199 if (usecs > 0) {
200 if (programmer->delay)
201 programmer->delay(usecs);
202 else
203 internal_delay(usecs);
204 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000205}
206
Nico Hubercdcfda22023-04-29 13:29:33 +0200207static int read_memmapped_chunk(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000208{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000209 chip_readn(flash, buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000210 return 0;
211}
Nico Hubercdcfda22023-04-29 13:29:33 +0200212int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
213{
214 return flashprog_read_chunked(flash, buf, start, len, MAX_DATA_READ_UNLIMITED, read_memmapped_chunk);
215}
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000216
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000217/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000218 * It will look for needle with a subsequent '=' in haystack, return a copy of
219 * needle and remove everything from the first occurrence of needle to the next
220 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000221 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200222static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000223{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000224 char *param_pos, *opt_pos, *rest;
225 char *opt = NULL;
226 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000227 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000228
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000229 needlelen = strlen(needle);
230 if (!needlelen) {
231 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200232 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000233 return NULL;
234 }
235 /* No programmer parameters given. */
236 if (*haystack == NULL)
237 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000238 param_pos = strstr(*haystack, needle);
239 do {
240 if (!param_pos)
241 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000242 /* Needle followed by '='? */
243 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000244 /* Beginning of the string? */
245 if (param_pos == *haystack)
246 break;
247 /* After a delimiter? */
248 if (strchr(delim, *(param_pos - 1)))
249 break;
250 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000251 /* Continue searching. */
252 param_pos++;
253 param_pos = strstr(param_pos, needle);
254 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000255
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000256 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000257 /* Get the string after needle and '='. */
258 opt_pos = param_pos + needlelen + 1;
259 optlen = strcspn(opt_pos, delim);
260 /* Return an empty string if the parameter was empty. */
261 opt = malloc(optlen + 1);
262 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000263 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000264 exit(1);
265 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000266 strncpy(opt, opt_pos, optlen);
267 opt[optlen] = '\0';
268 rest = opt_pos + optlen;
269 /* Skip all delimiters after the current parameter. */
270 rest += strspn(rest, delim);
271 memmove(param_pos, rest, strlen(rest) + 1);
272 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000273 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000274
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000275 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000276}
277
Stefan Tauner66652442011-06-26 17:38:17 +0000278char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000279{
280 return extract_param(&programmer_param, param_name, ",");
281}
282
Richard Hughes842d6782021-01-15 09:48:12 +0000283static void flashprog_progress_report(struct flashprog_progress *const p)
284{
285 if (p->current > p->total) {
286 msg_gdbg2("Sanitizing progress report: %zu bytes off.", p->current - p->total);
287 p->current = p->total;
288 }
289
290 if (!p->callback)
291 return;
292
293 p->callback(p->stage, p->current, p->total, p->user_data);
294}
295
296static void flashprog_progress_start(struct flashprog_flashctx *const flashctx,
297 const enum flashprog_progress_stage stage, const size_t total)
298{
299 flashctx->progress.stage = stage;
300 flashctx->progress.current = 0;
301 flashctx->progress.total = total;
302 flashprog_progress_report(&flashctx->progress);
303}
304
305static void flashprog_progress_start_by_layout(struct flashprog_flashctx *const flashctx,
306 const enum flashprog_progress_stage stage,
307 const struct flashprog_layout *const layout)
308{
309 const struct romentry *entry = NULL;
310 size_t total = 0;
311
312 while ((entry = layout_next_included(layout, entry)))
313 total += entry->end - entry->start + 1;
314
315 flashprog_progress_start(flashctx, stage, total);
316}
317
318static void flashprog_progress_set(struct flashprog_flashctx *const flashctx, const size_t current)
319{
320 flashctx->progress.current = current;
321 flashprog_progress_report(&flashctx->progress);
322}
323
324/** @private */
325void flashprog_progress_add(struct flashprog_flashctx *const flashctx, const size_t progress)
326{
327 flashctx->progress.current += progress;
328 flashprog_progress_report(&flashctx->progress);
329}
330
331static void flashprog_progress_finish(struct flashprog_flashctx *const flashctx)
332{
333 if (flashctx->progress.current == flashctx->progress.total)
334 return;
335
336 flashctx->progress.current = flashctx->progress.total;
337 flashprog_progress_report(&flashctx->progress);
338}
339
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000340/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000341static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000342{
343 unsigned int usable_erasefunctions = 0;
344 int k;
345 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
346 if (!check_block_eraser(flash, k, 0))
347 usable_erasefunctions++;
348 }
349 return usable_erasefunctions;
350}
351
Mark Marshallf20b7be2014-05-09 21:16:21 +0000352static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000353{
354 int ret = 0, failcount = 0;
355 unsigned int i;
356 for (i = 0; i < len; i++) {
357 if (wantbuf[i] != havebuf[i]) {
358 /* Only print the first failure. */
359 if (!failcount++)
360 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
361 start + i, wantbuf[i], havebuf[i]);
362 }
363 }
364 if (failcount) {
365 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
366 start, start + len - 1, failcount);
367 ret = -1;
368 }
369 return ret;
370}
371
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000372/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600373static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000374{
375 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300376 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000377
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100378 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000379 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100380 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100381 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000382 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300383 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000384 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100385
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000386 free(cmpbuf);
387 return ret;
388}
389
Richard Hughes842d6782021-01-15 09:48:12 +0000390int flashprog_read_range(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
391{
392 flashprog_progress_start(flash, FLASHPROG_PROGRESS_READ, len);
393 const int ret = flash->chip->read(flash, buf, start, len);
394 flashprog_progress_finish(flash);
395 return ret;
396}
397
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000398/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000399 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000400 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000401 * @start offset to the base address of the flash chip
402 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000403 * @return 0 for success, -1 for failure
404 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000405int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000406{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000407 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000408 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000409
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100410 if (start + len > flash->chip->total_size * 1024) {
411 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
412 " total_size 0x%x\n", __func__, start, len,
413 flash->chip->total_size * 1024);
414 return -1;
415 }
416
Stefan Taunerdf64a422014-05-27 00:06:14 +0000417 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000418 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100419 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000420 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000421 }
422
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100423 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000424 if (ret) {
425 msg_gerr("Verification impossible because read failed "
426 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000427 ret = -1;
428 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000429 }
430
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000431 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000432out_free:
433 free(readbuf);
434 return ret;
435}
436
Nico Huber3ac761c2023-01-16 02:43:17 +0100437size_t gran_to_bytes(const enum write_granularity gran)
Nico Huberb77607f2023-01-16 02:25:45 +0100438{
439 switch (gran) {
440 case write_gran_1bit: return 1;
441 case write_gran_1byte: return 1;
442 case write_gran_1byte_implicit_erase: return 1;
443 case write_gran_128bytes: return 128;
444 case write_gran_256bytes: return 256;
445 case write_gran_264bytes: return 264;
446 case write_gran_512bytes: return 512;
447 case write_gran_528bytes: return 528;
448 case write_gran_1024bytes: return 1024;
449 case write_gran_1056bytes: return 1056;
450 default: return 0;
451 }
452}
453
Stefan Tauner02437452013-04-01 19:34:53 +0000454/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300455static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
456 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000457{
458 unsigned int i, j, limit;
459 for (j = 0; j < len / gran; j++) {
460 limit = min (gran, len - j * gran);
461 /* Are 'have' and 'want' identical? */
462 if (!memcmp(have + j * gran, want + j * gran, limit))
463 continue;
464 /* have needs to be in erased state. */
465 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300466 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000467 return 1;
468 }
469 return 0;
470}
471
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000472/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000473 * Check if the buffer @have can be programmed to the content of @want without
474 * erasing. This is only possible if all chunks of size @gran are either kept
475 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000476 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000477 * Warning: This function assumes that @have and @want point to naturally
478 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000479 *
480 * @have buffer with current content
481 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000482 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000483 * @gran write granularity (enum, not count)
484 * @return 0 if no erase is needed, 1 otherwise
485 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000486static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
487 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000488{
Stefan Tauner02437452013-04-01 19:34:53 +0000489 unsigned int i;
Nico Huberb77607f2023-01-16 02:25:45 +0100490 size_t stride;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000491
492 switch (gran) {
493 case write_gran_1bit:
Nico Huberb77607f2023-01-16 02:25:45 +0100494 for (i = 0; i < len; i++) {
495 if ((have[i] & want[i]) != want[i])
496 return 1;
497 }
498 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000499 case write_gran_1byte:
Nico Huberb77607f2023-01-16 02:25:45 +0100500 for (i = 0; i < len; i++) {
501 if ((have[i] != want[i]) && (have[i] != erased_value))
502 return 1;
503 }
504 return 0;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000505 case write_gran_1byte_implicit_erase:
506 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
Nico Huberb77607f2023-01-16 02:25:45 +0100507 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000508 default:
Nico Huberb77607f2023-01-16 02:25:45 +0100509 stride = gran_to_bytes(gran);
510 if (stride) {
511 return need_erase_gran_bytes(have, want, len, stride, erased_value);
512 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000513 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200514 "flashprog@flashprog.org\n", __func__);
Nico Huberb77607f2023-01-16 02:25:45 +0100515 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000516 }
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000517}
518
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000519/**
520 * Check if the buffer @have needs to be programmed to get the content of @want.
521 * If yes, return 1 and fill in first_start with the start address of the
522 * write operation and first_len with the length of the first to-be-written
523 * chunk. If not, return 0 and leave first_start and first_len undefined.
524 *
525 * Warning: This function assumes that @have and @want point to naturally
526 * aligned regions.
527 *
528 * @have buffer with current content
529 * @want buffer with desired content
530 * @len length of the checked area
531 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000532 * @first_start offset of the first byte which needs to be written (passed in
533 * value is increased by the offset of the first needed write
534 * relative to have/want or unchanged if no write is needed)
535 * @return length of the first contiguous area which needs to be written
536 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000537 *
538 * FIXME: This function needs a parameter which tells it about coalescing
539 * in relation to the max write length of the programmer and the max write
540 * length of the chip.
541 */
Nico Huber3b9c86d2023-01-15 22:58:06 +0100542static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, chipsize_t len,
543 chipoff_t *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000544{
Felix Singerf25447e2022-08-19 02:44:28 +0200545 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000546 unsigned int rel_start = 0, first_len = 0;
Nico Huberb77607f2023-01-16 02:25:45 +0100547 unsigned int i, limit;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000548
Nico Huberb77607f2023-01-16 02:25:45 +0100549 const size_t stride = gran_to_bytes(gran);
550 if (!stride) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000551 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200552 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000553 /* Claim that no write was needed. A write with unknown
554 * granularity is too dangerous to try.
555 */
556 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000557 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000558 for (i = 0; i < len / stride; i++) {
559 limit = min(stride, len - i * stride);
560 /* Are 'have' and 'want' identical? */
561 if (memcmp(have + i * stride, want + i * stride, limit)) {
562 if (!need_write) {
563 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200564 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000565 rel_start = i * stride;
566 }
567 } else {
568 if (need_write) {
569 /* First location where have and want
570 * do not differ anymore.
571 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000572 break;
573 }
574 }
575 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000576 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000577 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000578 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000579 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000580}
581
Nico Huber2d625722016-05-03 10:48:02 +0200582/*
583 * Return a string corresponding to the bustype parameter.
584 * Memory is obtained with malloc() and must be freed with free() by the caller.
585 */
586char *flashbuses_to_text(enum chipbustype bustype)
587{
588 char *ret = calloc(1, 1);
589 /*
590 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
591 * will cease to exist and should be eliminated here as well.
592 */
593 if (bustype == BUS_NONSPI) {
594 ret = strcat_realloc(ret, "Non-SPI, ");
595 } else {
596 if (bustype & BUS_PARALLEL)
597 ret = strcat_realloc(ret, "Parallel, ");
598 if (bustype & BUS_LPC)
599 ret = strcat_realloc(ret, "LPC, ");
600 if (bustype & BUS_FWH)
601 ret = strcat_realloc(ret, "FWH, ");
602 if (bustype & BUS_SPI)
603 ret = strcat_realloc(ret, "SPI, ");
604 if (bustype & BUS_PROG)
605 ret = strcat_realloc(ret, "Programmer-specific, ");
606 if (bustype == BUS_NONE)
607 ret = strcat_realloc(ret, "None, ");
608 }
609 /* Kill last comma. */
610 ret[strlen(ret) - 2] = '\0';
611 ret = realloc(ret, strlen(ret) + 1);
612 return ret;
613}
614
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100615static int init_default_layout(struct flashctx *flash)
616{
617 /* Fill default layout covering the whole chip. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200618 if (flashprog_layout_new(&flash->default_layout) ||
619 flashprog_layout_add_region(flash->default_layout,
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100620 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huberc3b02dc2023-08-12 01:13:45 +0200621 flashprog_layout_include_region(flash->default_layout, "complete flash"))
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100622 return -1;
623 return 0;
624}
625
Nico Huberaf9d7382023-05-01 13:33:26 +0200626static void probe_bus(struct registered_master *const mst, const enum id_type type)
627{
Nico Huberac138732026-02-28 17:42:27 +0100628 unsigned int least_priority, priority, i;
Nico Huberaf9d7382023-05-01 13:33:26 +0200629 struct found_id **next_ptr;
Nico Huberaf9d7382023-05-01 13:33:26 +0200630
631 if (mst->probed)
632 return;
633
Nico Huberac138732026-02-28 17:42:27 +0100634 for (i = 0, least_priority = 0; i < mst->probing.probe_count; ++i) {
635 if (least_priority < mst->probing.probes[i].priority)
636 least_priority = mst->probing.probes[i].priority;
637 }
638
Nico Huberaf9d7382023-05-01 13:33:26 +0200639 next_ptr = &mst->found_ids;
Nico Huberac138732026-02-28 17:42:27 +0100640 for (priority = 0; priority <= least_priority; ++priority) {
641 bool found = false;
Nico Huberaf9d7382023-05-01 13:33:26 +0200642
Nico Huberac138732026-02-28 17:42:27 +0100643 for (i = 0; i < mst->probing.probe_count; ++i) {
644 if (mst->probing.probes[i].priority != priority)
645 continue;
Nico Huberaf9d7382023-05-01 13:33:26 +0200646
Nico Huberac138732026-02-28 17:42:27 +0100647 if (type && type != mst->probing.probes[i].type)
648 continue;
649
650 *next_ptr = mst->probing.probes[i].run(&mst->probing.probes[i], &mst->common);
651 found |= !!*next_ptr;
652
653 /* walk to end in case multiple IDs were found in a single call */
654 while (*next_ptr)
655 next_ptr = &(*next_ptr)->next;
656 }
657
658 /* Skip lower-priority probing if anything was found. */
659 if (found)
660 break;
Nico Huberaf9d7382023-05-01 13:33:26 +0200661 }
662
663 mst->probed = true;
664}
665
666static bool chip_on_bus(struct registered_master *const mst, const struct flashprog_chip *const chip)
667{
668 static const char *const id_names[] = {
669 [ID_82802AB] = "82802AB",
670 [ID_EN29LV640B] = "EN29LV640B",
671 [ID_JEDEC] = "JEDEC",
672 [ID_JEDEC_29GL] = "JEDEC_29GL",
673 [ID_OPAQUE] = "OPAQUE",
674 [ID_SPI_AT25F] = "SPI_AT25F",
675 [ID_SPI_RDID] = "SPI_RDID",
Nico Huberaf9d7382023-05-01 13:33:26 +0200676 [ID_SPI_REMS] = "SPI_REMS",
677 [ID_SPI_RES1] = "SPI_RES1",
678 [ID_SPI_RES2] = "SPI_RES2",
679 [ID_SPI_RES3] = "SPI_RES3",
680 [ID_SPI_SFDP] = "SPI_SFDP",
681 [ID_SPI_ST95] = "SPI_ST95",
682 [ID_W29EE011] = "W29EE011",
683 [ID_EDI] = "EDI",
684 };
685
686 if (chip_to_probe) {
Nico Huber966dc9b2026-03-07 21:59:15 +0100687 /* We are looking for a particular chip.
688 If it can't be probed, assume it's there... */
689 if (chip->id.type == ID_NONE)
690 return true;
691 /* ...otherwise, limit the probing functions to its type. */
Nico Huberaf9d7382023-05-01 13:33:26 +0200692 probe_bus(mst, chip->id.type);
693 } else {
694 probe_bus(mst, 0);
695 }
696
697 struct found_id *found_id;
698 for (found_id = mst->found_ids; found_id; found_id = found_id->next) {
699 if (found_id->info.id.type != chip->id.type)
700 continue;
701
702 if (found_id->info.id.type < ARRAY_SIZE(id_names))
703 msg_cdbg("%s: id1 0x%02x, id2 0x%02x ",
704 id_names[found_id->info.id.type],
705 found_id->info.id.id1, found_id->info.id.id2);
706
707 if (mst->probing.match(chip, &found_id->info))
708 break;
709 }
710
711 msg_cdbg("\n");
712 return !!found_id;
713}
714
715/* wrapper that's used until all probing functions are ported to per-bus probing */
716int probe_buses(struct flashctx *const flash)
717{
718 int i;
719 for (i = 0; i < registered_master_count; ++i) {
720 if (flash->mst.common == &registered_masters[i].common &&
721 chip_on_bus(&registered_masters[i], flash->chip))
722 return 1;
723 }
724 return 0;
725}
726
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000727int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000728{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000729 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000730 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000731 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000732
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000733 for (chip = flashchips + startchip; chip && chip->name; chip++) {
734 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000735 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000736 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000737 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000738 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300739 /* Only probe for SPI25 chips by default. */
740 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
741 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000742 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
743 if (!chip->probe && !force) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200744 msg_gdbg("failed! flashprog has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000745 continue;
746 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000747
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000748 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200749 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000750 if (!flash->chip) {
751 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100752 return -1;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000753 }
Angel Pons7e134562021-06-07 13:29:13 +0200754 *flash->chip = *chip;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100755 flash->mst.par = &mst->par; /* both `mst` are unions, so we need only one pointer */
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000756
Nico Huber5469c152026-02-12 22:56:52 +0100757 /* If we probe for a specific chip, we can adapt the voltage early. */
758 if (chip_to_probe && flash->mst.common->adapt_voltage) {
759 if (flash->mst.common->adapt_voltage(flash->mst.common,
760 chip->voltage.min, chip->voltage.max))
761 goto free_chip;
762 }
763
Nico Huber9eec4072023-01-12 01:17:30 +0100764 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_PROBE))
765 goto free_chip;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000766
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000767 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
768 * is only called with force=1 after normal probing failed.
769 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000770 if (force)
771 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000772
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000773 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000774 goto notfound;
775
Nico Huberb89c4522024-12-04 23:30:13 +0100776 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_POST_PROBE))
777 goto notfound;
778
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000779 /* If this is the first chip found, accept it.
780 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000781 * a non-generic match. SFDP and CFI are generic matches.
782 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000783 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000784 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000785 */
Nico Huber11136c22023-05-01 12:00:09 +0200786 if (startchip == 0 && flash->chip->id.model == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000787 msg_cinfo("===\n"
788 "SFDP has autodetected a flash chip which is "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200789 "not natively supported by flashprog yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000790 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000791 msg_cinfo("The standard operations read and "
792 "verify should work, but to support "
793 "erase, write and all other "
794 "possible features");
795 else
796 msg_cinfo("All standard operations (read, "
797 "verify, erase and write) should "
798 "work, but to support all possible "
799 "features");
800
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000801 msg_cinfo(" we need to add them manually.\n"
802 "You can help us by mailing us the output of the following command to "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200803 "flashprog@flashprog.org:\n"
804 "'flashprog -VV [plus the -p/--programmer parameter]'\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000805 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000806 "===\n");
807 }
808
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000809 /* First flash chip detected on this bus. */
810 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000811 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000812 /* Not the first flash chip detected on this bus, but not a generic match either. */
Nico Huber11136c22023-05-01 12:00:09 +0200813 if ((flash->chip->id.model != GENERIC_DEVICE_ID) && (flash->chip->id.model != SFDP_DEVICE_ID))
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000814 break;
815 /* 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 +0000816notfound:
Nico Huber9eec4072023-01-12 01:17:30 +0100817 if (flash->chip->finish_access)
818 flash->chip->finish_access(flash);
819free_chip:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000820 free(flash->chip);
821 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000822 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000823
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000824 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000825 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000826
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100827 if (init_default_layout(flash) < 0)
828 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000829
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000830 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000831 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
832 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000833 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000834#if CONFIG_INTERNAL == 1
Nico Huber0e76d992023-01-12 20:22:55 +0100835 if (flash->physical_memory != 0 && mst->par.map_flash == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000836 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
837 PRIxPTR_WIDTH, flash->physical_memory);
838 else
839#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200840 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000841
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000842 /* Flash registers may more likely not be mapped if the chip was forced.
843 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000844 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000845 if (flash->chip->printlock)
846 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000847
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000848 /* Get out of the way for later runs. */
Nico Huber9eec4072023-01-12 01:17:30 +0100849 if (flash->chip->finish_access)
850 flash->chip->finish_access(flash);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000851
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000852 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000853 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000854}
855
Stefan Tauner96658be2014-05-26 22:05:31 +0000856/* Even if an error is found, the function will keep going and check the rest. */
Nico Huber72d0ada2026-01-26 18:28:20 +0100857static int selfcheck_eraseblocks(const struct flashchip *chip, const char *label)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000858{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000859 int i, j, k;
860 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530861 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000862
863 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
864 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000865 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530866 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000867
868 for (i = 0; i < NUM_ERASEREGIONS; i++) {
869 /* Blocks with zero size are bugs in flashchips.c. */
870 if (eraser.eraseblocks[i].count &&
871 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000872 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200873 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100874 label, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000875 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000876 }
877 /* Blocks with zero count are bugs in flashchips.c. */
878 if (!eraser.eraseblocks[i].count &&
879 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000880 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200881 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100882 label, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000883 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000884 }
885 done += eraser.eraseblocks[i].count *
886 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530887 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000888 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000889 /* Empty eraseblock definition with erase function. */
890 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000891 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000892 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000893 if (!done)
894 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000895 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000896 msg_gerr("ERROR: Flash chip %s erase function %i "
897 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000898 " expected 0x%06x bytes.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200899 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100900 label, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000901 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000902 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000903 if (!eraser.block_erase)
904 continue;
905 /* Check if there are identical erase functions for different
906 * layouts. That would imply "magic" erase functions. The
907 * easiest way to check this is with function pointers.
908 */
Uwe Hermann43959702010-03-13 17:28:29 +0000909 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000910 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000911 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000912 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200913 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100914 label, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000915 ret = 1;
916 }
Uwe Hermann43959702010-03-13 17:28:29 +0000917 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530918 if(curr_eraseblock_count > prev_eraseblock_count)
919 {
Nico Huberac90af62022-12-18 00:22:47 +0000920 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200921 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100922 label, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530923 ret = 1;
924 }
925 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000926 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000927 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000928}
929
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000930static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000931{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000932 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000933
934 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
935 if (log)
936 msg_cdbg("not defined. ");
937 return 1;
938 }
939 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
940 if (log)
941 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000942 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000943 return 1;
944 }
945 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
946 if (log)
947 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000948 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000949 return 1;
950 }
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530951
Nico Hubere149fbe2024-08-21 10:23:53 +0200952 if (flash->chip->bustype == BUS_SPI && flash->chip->spi_cmd_set == SPI25) {
Nico Huber13389362024-03-05 18:35:30 +0100953 bool native_4ba;
Nico Huber13f97a52023-01-14 23:55:06 +0100954 int i;
Nico Huber13389362024-03-05 18:35:30 +0100955
956 const uint8_t *opcode = spi_get_opcode_from_erasefn(eraser.block_erase, &native_4ba);
Nico Huber07ebc682024-08-21 10:18:27 +0200957 if (!opcode)
958 return 1;
959
Nico Huber13f97a52023-01-14 23:55:06 +0100960 for (i = 0; opcode[i]; i++) {
Nico Huber13389362024-03-05 18:35:30 +0100961 if ((native_4ba && !spi_master_4ba(flash)) ||
Nico Huber9a11cbf2023-01-13 01:19:07 +0100962 !flash->mst.spi->probe_opcode(flash, opcode[i])) {
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530963 if (log)
964 msg_cdbg("block erase function and layout found "
965 "but SPI master doesn't support the function. ");
966 return 1;
967 }
968 }
969 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000970 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000971 return 0;
972}
973
Nico Huber7af0e792016-04-29 16:40:15 +0200974/**
975 * @brief Reads the included layout regions into a buffer.
976 *
977 * If there is no layout set in the given flash context, the whole chip will
978 * be read.
979 *
980 * @param flashctx Flash context to be used.
981 * @param buffer Buffer of full chip size to read into.
982 * @return 0 on success,
983 * 1 if any read fails.
984 */
985static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
986{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200987 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200988 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200989
Richard Hughes842d6782021-01-15 09:48:12 +0000990 flashprog_progress_start_by_layout(flashctx, FLASHPROG_PROGRESS_READ, layout);
991
Nico Huber5ca55232019-06-15 22:29:08 +0200992 while ((entry = layout_next_included(layout, entry))) {
993 const chipoff_t region_start = entry->start;
994 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200995
996 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
997 return 1;
998 }
Richard Hughes842d6782021-01-15 09:48:12 +0000999
1000 flashprog_progress_finish(flashctx);
1001
Nico Huber7af0e792016-04-29 16:40:15 +02001002 return 0;
1003}
1004
Nico Huber7af0e792016-04-29 16:40:15 +02001005/**
1006 * @private
1007 *
1008 * For read-erase-write, `curcontents` and `newcontents` shall point
1009 * to buffers of the chip's size. Both are supposed to be prefilled
1010 * with at least the included layout regions of the current flash
1011 * contents (`curcontents`) and the data to be written to the flash
1012 * (`newcontents`).
1013 *
1014 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1015 *
1016 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1017 */
1018struct walk_info {
1019 uint8_t *curcontents;
1020 const uint8_t *newcontents;
1021 chipoff_t region_start;
1022 chipoff_t region_end;
1023 chipoff_t erase_start;
1024 chipoff_t erase_end;
1025};
Nico Huber3b9c86d2023-01-15 22:58:06 +01001026
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301027struct eraseblock_data {
1028 chipoff_t start_addr;
1029 chipoff_t end_addr;
1030 bool selected;
1031 size_t block_num;
1032 size_t first_sub_block_index;
1033 size_t last_sub_block_index;
1034};
1035
1036struct erase_layout {
1037 struct eraseblock_data* layout_list;
1038 size_t block_count;
1039 const struct block_eraser *eraser;
1040};
1041
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001042static bool explicit_erase(const struct walk_info *const info)
1043{
1044 /* For explicit erase, we are called without new contents. */
1045 return !info->newcontents;
1046}
1047
Nico Huberd96e7032023-01-14 22:31:48 +01001048static size_t calculate_block_count(const struct block_eraser *const eraser)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301049{
Nico Huberd96e7032023-01-14 22:31:48 +01001050 size_t block_count = 0, i;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301051
Nico Huberd96e7032023-01-14 22:31:48 +01001052 for (i = 0; i < ARRAY_SIZE(eraser->eraseblocks); ++i)
1053 block_count += eraser->eraseblocks[i].count;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301054
1055 return block_count;
1056}
1057
1058static void init_eraseblock(struct erase_layout *layout, size_t idx, size_t block_num,
1059 chipoff_t start_addr, chipoff_t end_addr, size_t *sub_block_index)
1060{
1061 struct eraseblock_data *edata = &layout[idx].layout_list[block_num];
1062 edata->start_addr = start_addr;
1063 edata->end_addr = end_addr;
1064 edata->selected = false;
1065 edata->block_num = block_num;
1066
1067 if (!idx)
1068 return;
Nico Hubera02df332023-01-14 23:06:27 +01001069 const struct erase_layout *const sub_layout = &layout[idx - 1];
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301070
1071 edata->first_sub_block_index = *sub_block_index;
Nico Hubera02df332023-01-14 23:06:27 +01001072 for (; *sub_block_index < sub_layout->block_count; ++*sub_block_index) {
1073 if (sub_layout->layout_list[*sub_block_index].end_addr > end_addr)
1074 break;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301075 }
1076 edata->last_sub_block_index = *sub_block_index - 1;
1077}
1078
1079/*
1080 * @brief Function to free the created erase_layout
1081 *
1082 * @param layout pointer to allocated layout
1083 * @param erasefn_count number of erase functions for which the layout was created
1084 *
1085 */
1086static void free_erase_layout(struct erase_layout *layout, unsigned int erasefn_count)
1087{
Nico Huber13f97a52023-01-14 23:55:06 +01001088 size_t i;
1089
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301090 if (!layout)
1091 return;
Nico Huber13f97a52023-01-14 23:55:06 +01001092 for (i = 0; i < erasefn_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301093 free(layout[i].layout_list);
1094 }
1095 free(layout);
1096}
1097
1098/*
1099 * @brief Function to create an erase layout
1100 *
1101 * @param flashctx flash context
1102 * @param e_layout address to the pointer to store the layout
1103 * @return 0 on success,
1104 * -1 if layout creation fails
1105 *
1106 * This function creates a layout of which erase functions erase which regions
1107 * of the flash chip. This helps to optimally select the erase functions for
1108 * erase/write operations.
1109 */
Nico Huberc09fca42023-01-29 15:58:09 +01001110static int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301111{
1112 const struct flashchip *chip = flashctx->chip;
1113 const size_t erasefn_count = count_usable_erasers(flashctx);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301114
1115 if (!erasefn_count) {
1116 msg_gerr("No erase functions supported\n");
1117 return 0;
1118 }
1119
Nico Huberd34864b2023-01-14 23:47:19 +01001120 struct erase_layout *layout = calloc(erasefn_count, sizeof(struct erase_layout));
1121 if (!layout) {
1122 msg_gerr("Out of memory!\n");
1123 return -1;
1124 }
1125
Nico Huber13f97a52023-01-14 23:55:06 +01001126 size_t layout_idx = 0, eraser_idx;
1127 for (eraser_idx = 0; eraser_idx < NUM_ERASEFUNCTIONS; eraser_idx++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301128 if (check_block_eraser(flashctx, eraser_idx, 0))
1129 continue;
1130
1131 layout[layout_idx].eraser = &chip->block_erasers[eraser_idx];
Nico Huberd96e7032023-01-14 22:31:48 +01001132 const size_t block_count = calculate_block_count(&chip->block_erasers[eraser_idx]);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301133 size_t sub_block_index = 0;
1134
1135 layout[layout_idx].block_count = block_count;
1136 layout[layout_idx].layout_list = (struct eraseblock_data *)calloc(block_count,
1137 sizeof(struct eraseblock_data));
1138
1139 if (!layout[layout_idx].layout_list) {
1140 free_erase_layout(layout, layout_idx);
1141 return -1;
1142 }
1143
1144 size_t block_num = 0;
1145 chipoff_t start_addr = 0;
1146
Nico Huber13f97a52023-01-14 23:55:06 +01001147 int i;
1148 for (i = 0; block_num < block_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301149 const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];
1150
Nico Huber13f97a52023-01-14 23:55:06 +01001151 size_t num;
1152 for (num = 0; num < block->count; num++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301153 chipoff_t end_addr = start_addr + block->size - 1;
1154 init_eraseblock(layout, layout_idx, block_num,
1155 start_addr, end_addr, &sub_block_index);
1156 block_num += 1;
1157 start_addr = end_addr + 1;
1158 }
1159 }
1160 layout_idx++;
1161 }
1162
1163 *e_layout = layout;
1164 return layout_idx;
1165}
1166
Nico Huber20073e72024-05-10 01:31:40 +02001167static void deselect_erase_block_rec(const struct erase_layout *layout, size_t findex, size_t block_num)
1168{
1169 struct eraseblock_data *const ed = &layout[findex].layout_list[block_num];
1170 size_t i;
1171
1172 if (ed->selected) {
1173 ed->selected = false;
1174 } else if (findex > 0) {
1175 for (i = ed->first_sub_block_index; i <= ed->last_sub_block_index; ++i)
1176 deselect_erase_block_rec(layout, findex - 1, i);
1177 }
1178}
1179
Aarya Chaumald33bea42022-07-14 12:51:14 +05301180/*
1181 * @brief Function to select the list of sectors that need erasing
1182 *
1183 * @param flashctx flash context
1184 * @param layout erase layout
1185 * @param findex index of the erase function
1186 * @param block_num index of the block to erase according to the erase function index
Nico Huber00d1b9f2023-01-29 15:07:33 +01001187 * @param info current info from walking the regions
Nico Huberaa714dd2023-04-22 14:59:33 +02001188 * @return number of bytes selected for erase
Aarya Chaumald33bea42022-07-14 12:51:14 +05301189 */
Nico Huberaa714dd2023-04-22 14:59:33 +02001190static size_t select_erase_functions_rec(const struct flashctx *flashctx, const struct erase_layout *layout,
1191 size_t findex, size_t block_num, const struct walk_info *info)
Aarya Chaumald33bea42022-07-14 12:51:14 +05301192{
1193 struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
Nico Huberaa714dd2023-04-22 14:59:33 +02001194 const size_t eraseblock_size = ll->end_addr - ll->start_addr + 1;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301195 if (!findex) {
Nico Hubercf6ff0a2023-01-29 15:45:06 +01001196 if (ll->start_addr <= info->region_end && ll->end_addr >= info->region_start) {
Nico Huber1494f8e2023-01-29 15:48:00 +01001197 if (explicit_erase(info)) {
1198 ll->selected = true;
Nico Huberaa714dd2023-04-22 14:59:33 +02001199 return eraseblock_size;
Nico Huber1494f8e2023-01-29 15:48:00 +01001200 }
Nico Hubera6212482023-01-29 15:39:26 +01001201 const chipoff_t write_start = MAX(info->region_start, ll->start_addr);
1202 const chipoff_t write_end = MIN(info->region_end, ll->end_addr);
1203 const chipsize_t write_len = write_end - write_start + 1;
1204 const uint8_t erased_value = ERASED_VALUE(flashctx);
Nico Huber00d1b9f2023-01-29 15:07:33 +01001205 ll->selected = need_erase(
Nico Hubera6212482023-01-29 15:39:26 +01001206 info->curcontents + write_start, info->newcontents + write_start,
1207 write_len, flashctx->chip->gran, erased_value);
Nico Huberaa714dd2023-04-22 14:59:33 +02001208 if (ll->selected)
1209 return eraseblock_size;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301210 }
Nico Huberaa714dd2023-04-22 14:59:33 +02001211 return 0;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301212 } else {
Aarya Chaumald33bea42022-07-14 12:51:14 +05301213 const int sub_block_start = ll->first_sub_block_index;
1214 const int sub_block_end = ll->last_sub_block_index;
Nico Huberaa714dd2023-04-22 14:59:33 +02001215 size_t bytes = 0;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301216
Nico Huber13f97a52023-01-14 23:55:06 +01001217 int j;
Nico Huberaa714dd2023-04-22 14:59:33 +02001218 for (j = sub_block_start; j <= sub_block_end; j++)
1219 bytes += select_erase_functions_rec(flashctx, layout, findex - 1, j, info);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301220
Nico Huberaa714dd2023-04-22 14:59:33 +02001221 if (bytes > eraseblock_size / 2) {
Nico Huber00d1b9f2023-01-29 15:07:33 +01001222 if (ll->start_addr >= info->region_start && ll->end_addr <= info->region_end) {
Nico Huber20073e72024-05-10 01:31:40 +02001223 deselect_erase_block_rec(layout, findex, block_num);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301224 ll->selected = true;
Nico Huberaa714dd2023-04-22 14:59:33 +02001225 bytes = eraseblock_size;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301226 }
1227 }
Nico Huberaa714dd2023-04-22 14:59:33 +02001228 return bytes;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301229 }
1230}
1231
Nico Huberaa714dd2023-04-22 14:59:33 +02001232static size_t select_erase_functions(const struct flashctx *flashctx, const struct erase_layout *layout,
1233 size_t erasefn_count, const struct walk_info *info)
Nico Huberb11b72c2023-01-29 15:33:11 +01001234{
Nico Huberaa714dd2023-04-22 14:59:33 +02001235 size_t bytes = 0;
Nico Huberb11b72c2023-01-29 15:33:11 +01001236 size_t block_num;
1237 for (block_num = 0; block_num < layout[erasefn_count - 1].block_count; ++block_num)
Nico Huberaa714dd2023-04-22 14:59:33 +02001238 bytes += select_erase_functions_rec(flashctx, layout, erasefn_count - 1, block_num, info);
1239 return bytes;
Nico Huberb11b72c2023-01-29 15:33:11 +01001240}
1241
Nico Huber3b9c86d2023-01-15 22:58:06 +01001242static int write_range(struct flashctx *const flashctx, const chipoff_t flash_offset,
1243 const uint8_t *const curcontents, const uint8_t *const newcontents,
1244 const chipsize_t len, bool *const skipped)
1245{
1246 unsigned int writecount = 0;
1247 chipoff_t starthere = 0;
1248 chipsize_t lenhere = 0;
1249
1250 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1251 len - starthere, &starthere, flashctx->chip->gran))) {
1252 if (!writecount++)
1253 msg_cdbg("W");
1254 if (flashctx->chip->write(flashctx, newcontents + starthere,
1255 flash_offset + starthere, lenhere))
1256 return 1;
1257 starthere += lenhere;
Richard Hughes842d6782021-01-15 09:48:12 +00001258 if (skipped) {
1259 flashprog_progress_set(flashctx, starthere);
Nico Huber3b9c86d2023-01-15 22:58:06 +01001260 *skipped = false;
Richard Hughes842d6782021-01-15 09:48:12 +00001261 }
Nico Huber3b9c86d2023-01-15 22:58:06 +01001262 }
1263 return 0;
1264}
1265
1266typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
Nico Huber7af0e792016-04-29 16:40:15 +02001267/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1268typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1269
1270static int walk_eraseblocks(struct flashctx *const flashctx,
Nico Huberc09fca42023-01-29 15:58:09 +01001271 struct erase_layout *const layouts,
1272 const size_t layout_count,
Nico Huber7af0e792016-04-29 16:40:15 +02001273 struct walk_info *const info,
Nico Huberc09fca42023-01-29 15:58:09 +01001274 const per_blockfn_t per_blockfn)
Nico Huber7af0e792016-04-29 16:40:15 +02001275{
1276 int ret;
1277 size_t i, j;
1278 bool first = true;
Nico Huber7af0e792016-04-29 16:40:15 +02001279
Nico Huberc09fca42023-01-29 15:58:09 +01001280 for (i = 0; i < layout_count; ++i) {
1281 const struct erase_layout *const layout = &layouts[i];
Nico Huber7af0e792016-04-29 16:40:15 +02001282
Nico Huberc09fca42023-01-29 15:58:09 +01001283 for (j = 0; j < layout->block_count; ++j) {
1284 struct eraseblock_data *const eb = &layout->layout_list[j];
1285
1286 if (eb->start_addr > info->region_end)
Nico Huber7af0e792016-04-29 16:40:15 +02001287 break;
Nico Huberc09fca42023-01-29 15:58:09 +01001288 if (eb->end_addr < info->region_start)
1289 continue;
1290 if (!eb->selected)
1291 continue;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001292
Nico Huber7af0e792016-04-29 16:40:15 +02001293 /* Print this for every block except the first one. */
1294 if (first)
1295 first = false;
1296 else
1297 msg_cdbg(", ");
Nico Huberc09fca42023-01-29 15:58:09 +01001298 msg_cdbg("0x%06x-0x%06x:", eb->start_addr, eb->end_addr);
Nico Huber7af0e792016-04-29 16:40:15 +02001299
Nico Huberc09fca42023-01-29 15:58:09 +01001300 info->erase_start = eb->start_addr;
1301 info->erase_end = eb->end_addr;
1302 ret = per_blockfn(flashctx, info, layout->eraser->block_erase);
Nico Huber7af0e792016-04-29 16:40:15 +02001303 if (ret)
1304 return ret;
Nico Huberc09fca42023-01-29 15:58:09 +01001305
1306 /* Clean the erase layout up for future use on other
1307 regions. `.selected` is the only field we alter. */
1308 eb->selected = false;
Nico Huber7af0e792016-04-29 16:40:15 +02001309 }
Nico Huber7af0e792016-04-29 16:40:15 +02001310 }
1311 msg_cdbg("\n");
1312 return 0;
1313}
1314
1315static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1316 const per_blockfn_t per_blockfn)
1317{
Nico Huberc09fca42023-01-29 15:58:09 +01001318 const bool do_erase = explicit_erase(info) || !(flashctx->chip->feature_bits & FEATURE_NO_ERASE);
Nico Huberc3b02dc2023-08-12 01:13:45 +02001319 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huberc09fca42023-01-29 15:58:09 +01001320 struct erase_layout *erase_layouts = NULL;
Nico Huber5ca55232019-06-15 22:29:08 +02001321 const struct romentry *entry = NULL;
Nico Huberc09fca42023-01-29 15:58:09 +01001322 int ret = 0, layout_count = 0;
Nico Huber7af0e792016-04-29 16:40:15 +02001323
1324 all_skipped = true;
Nico Huberc6a924a2024-09-04 15:09:52 +02001325 msg_cinfo("Erasing %sflash chip... ", info->newcontents ? "and writing " : "");
Nico Huber7af0e792016-04-29 16:40:15 +02001326
Nico Huberc09fca42023-01-29 15:58:09 +01001327 if (do_erase) {
1328 layout_count = create_erase_layout(flashctx, &erase_layouts);
1329 if (layout_count <= 0)
1330 return 1;
1331 }
1332
Nico Huber5ca55232019-06-15 22:29:08 +02001333 while ((entry = layout_next_included(layout, entry))) {
1334 info->region_start = entry->start;
1335 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001336
Nico Huberc09fca42023-01-29 15:58:09 +01001337 if (do_erase) {
Richard Hughes842d6782021-01-15 09:48:12 +00001338 const size_t total = select_erase_functions(flashctx, erase_layouts, layout_count, info);
1339
1340 /* We verify every erased block manually. Technically that's
1341 reading, but accounting for it as part of the erase helps
1342 to provide a smooth, overall progress. Hence `total * 2`. */
1343 flashprog_progress_start(flashctx, FLASHPROG_PROGRESS_ERASE, total * 2);
1344
Nico Huberc09fca42023-01-29 15:58:09 +01001345 ret = walk_eraseblocks(flashctx, erase_layouts, layout_count, info, per_blockfn);
1346 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001347 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001348 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001349 }
Richard Hughes842d6782021-01-15 09:48:12 +00001350
1351 flashprog_progress_finish(flashctx);
Nico Huber7af0e792016-04-29 16:40:15 +02001352 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001353
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001354 if (info->newcontents) {
1355 bool skipped = true;
1356 msg_cdbg("0x%06x-0x%06x:", info->region_start, info->region_end);
Richard Hughes842d6782021-01-15 09:48:12 +00001357 flashprog_progress_start(flashctx, FLASHPROG_PROGRESS_WRITE,
1358 info->region_end - info->region_start + 1);
Nico Huberc09fca42023-01-29 15:58:09 +01001359 ret = write_range(flashctx, info->region_start,
1360 info->curcontents + info->region_start,
1361 info->newcontents + info->region_start,
1362 info->region_end + 1 - info->region_start, &skipped);
1363 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001364 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001365 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001366 }
Richard Hughes842d6782021-01-15 09:48:12 +00001367 flashprog_progress_finish(flashctx);
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001368 if (skipped) {
1369 msg_cdbg("S\n");
1370 } else {
1371 msg_cdbg("\n");
1372 all_skipped = false;
1373 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001374 }
Nico Huber7af0e792016-04-29 16:40:15 +02001375 }
1376 if (all_skipped)
1377 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
Nico Huberc6a924a2024-09-04 15:09:52 +02001378 msg_cinfo("Erase%s done.\n", info->newcontents ? "/write" : "");
Nico Huberc09fca42023-01-29 15:58:09 +01001379
1380free_ret:
1381 free_erase_layout(erase_layouts, layout_count);
1382 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001383}
1384
1385static int erase_block(struct flashctx *const flashctx,
1386 const struct walk_info *const info, const erasefn_t erasefn)
1387{
1388 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001389 const bool region_unaligned = info->region_start > info->erase_start ||
1390 info->erase_end > info->region_end;
1391 uint8_t *backup_contents = NULL, *erased_contents = NULL;
Nico Huberd34af7a2023-01-15 23:58:18 +01001392 int ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001393
Nico Huber6e61e0c2019-01-23 17:07:49 +01001394 /*
1395 * If the region is not erase-block aligned, merge current flash con-
1396 * tents into a new buffer `backup_contents`.
1397 */
1398 if (region_unaligned) {
1399 backup_contents = malloc(erase_len);
1400 erased_contents = malloc(erase_len);
1401 if (!backup_contents || !erased_contents) {
1402 msg_cerr("Out of memory!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001403 goto _free_ret;
1404 }
1405 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1406 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1407
1408 msg_cdbg("R");
1409 /* Merge data preceding the current region. */
1410 if (info->region_start > info->erase_start) {
1411 const chipoff_t start = info->erase_start;
1412 const chipsize_t len = info->region_start - info->erase_start;
1413 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1414 msg_cerr("Can't read! Aborting.\n");
1415 goto _free_ret;
1416 }
1417 }
1418 /* Merge data following the current region. */
1419 if (info->erase_end > info->region_end) {
1420 const chipoff_t start = info->region_end + 1;
1421 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1422 const chipsize_t len = info->erase_end - info->region_end;
1423 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1424 msg_cerr("Can't read! Aborting.\n");
1425 goto _free_ret;
1426 }
1427 }
1428 }
1429
Nico Huber7af0e792016-04-29 16:40:15 +02001430 all_skipped = false;
1431
1432 msg_cdbg("E");
1433 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001434 goto _free_ret;
Richard Hughes842d6782021-01-15 09:48:12 +00001435 flashprog_progress_add(flashctx, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001436 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1437 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001438 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001439 }
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001440 if (info->curcontents)
1441 memset(info->curcontents + info->erase_start, ERASED_VALUE(flashctx), erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001442
1443 if (region_unaligned) {
Nico Huber3b9c86d2023-01-15 22:58:06 +01001444 if (write_range(flashctx, info->erase_start, erased_contents, backup_contents, erase_len, NULL))
1445 goto _free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001446 if (info->curcontents)
1447 memcpy(info->curcontents + info->erase_start, backup_contents, erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001448 }
1449
1450 ret = 0;
1451
1452_free_ret:
1453 free(erased_contents);
1454 free(backup_contents);
1455 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001456}
1457
1458/**
1459 * @brief Erases the included layout regions.
1460 *
1461 * If there is no layout set in the given flash context, the whole chip will
1462 * be erased.
1463 *
1464 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001465 * @return 0 on success,
1466 * 1 if all available erase functions failed.
1467 */
Nico Huber454f6132012-12-10 13:34:10 +00001468static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001469{
1470 struct walk_info info = { 0 };
1471 return walk_by_layout(flashctx, &info, &erase_block);
1472}
1473
Nico Huber7af0e792016-04-29 16:40:15 +02001474/**
1475 * @brief Writes the included layout regions from a given image.
1476 *
1477 * If there is no layout set in the given flash context, the whole image
1478 * will be written.
1479 *
1480 * @param flashctx Flash context to be used.
1481 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1482 * @param newcontents The new image to be written.
1483 * @return 0 on success,
1484 * 1 if anything has gone wrong.
1485 */
Nico Huber454f6132012-12-10 13:34:10 +00001486static int write_by_layout(struct flashctx *const flashctx,
1487 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001488{
1489 struct walk_info info;
1490 info.curcontents = curcontents;
1491 info.newcontents = newcontents;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001492 return walk_by_layout(flashctx, &info, erase_block);
Nico Huber7af0e792016-04-29 16:40:15 +02001493}
1494
1495/**
1496 * @brief Compares the included layout regions with content from a buffer.
1497 *
1498 * If there is no layout set in the given flash context, the whole chip's
1499 * contents will be compared.
1500 *
1501 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001502 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001503 * @param curcontents A buffer of full chip size to read current chip contents into.
1504 * @param newcontents The new image to compare to.
1505 * @return 0 on success,
1506 * 1 if reading failed,
1507 * 3 if the contents don't match.
1508 */
Nico Huber74d09d42019-06-16 03:27:26 +02001509static int verify_by_layout(
1510 struct flashctx *const flashctx,
Nico Huberc3b02dc2023-08-12 01:13:45 +02001511 const struct flashprog_layout *const layout,
Nico Huber74d09d42019-06-16 03:27:26 +02001512 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001513{
Nico Huber5ca55232019-06-15 22:29:08 +02001514 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001515
Richard Hughes842d6782021-01-15 09:48:12 +00001516 flashprog_progress_start_by_layout(flashctx, FLASHPROG_PROGRESS_READ, layout);
1517
Nico Huber5ca55232019-06-15 22:29:08 +02001518 while ((entry = layout_next_included(layout, entry))) {
1519 const chipoff_t region_start = entry->start;
1520 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001521
1522 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1523 return 1;
1524 if (compare_range(newcontents + region_start, curcontents + region_start,
1525 region_start, region_len))
1526 return 3;
1527 }
Richard Hughes842d6782021-01-15 09:48:12 +00001528
1529 flashprog_progress_finish(flashctx);
1530
Nico Huber7af0e792016-04-29 16:40:15 +02001531 return 0;
1532}
1533
Stefan Tauner136388f2013-07-15 10:47:53 +00001534static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001535{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001536 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001537#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001538 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001539 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001540 "chip. Please report this to the mailing list at flashprog@flashprog.org or\n"
1541 "on IRC (see https://www.flashprog.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001542 "-------------------------------------------------------------------------------\n"
1543 "You may now reboot or simply leave the machine running.\n");
1544 else
1545#endif
1546 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001547 "the programmer and the flash chip. If you think the error is caused by flashprog\n"
1548 "please report this to the mailing list at flashprog@flashprog.org or on IRC\n"
1549 "(see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001550}
1551
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001552void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001553{
Stefan Tauner136388f2013-07-15 10:47:53 +00001554 msg_gerr("Your flash chip is in an unknown state.\n");
1555#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001556 if (programmer == &programmer_internal)
Nico Huberc3b02dc2023-08-12 01:13:45 +02001557 msg_gerr("Get help on IRC (see https://www.flashprog.org/Contact) or mail\n"
1558 "flashprog@flashprog.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001559 "-------------------------------------------------------------------------------\n"
1560 "DO NOT REBOOT OR POWEROFF!\n");
1561 else
1562#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +02001563 msg_gerr("Please report this to the mailing list at flashprog@flashprog.org\n"
1564 "or on IRC (see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001565}
1566
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001567void list_programmers_linebreak(int startcol, int cols, int paren)
1568{
1569 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001570 int pnamelen;
1571 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001572 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001573 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001574
Thomas Heijligend45cb592021-05-19 14:12:18 +02001575 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001576 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001577 pnamelen = strlen(pname);
1578 if (remaining - pnamelen - 2 < 0) {
1579 if (firstline)
1580 firstline = 0;
1581 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001582 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001583 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001584 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001585 remaining = cols - startcol;
1586 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001587 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001588 remaining--;
1589 }
1590 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001591 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001592 remaining--;
1593 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001594 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001595 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001596 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001597 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001598 remaining--;
1599 } else {
1600 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001601 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001602 }
1603 }
1604}
1605
Nico Huber32fa5082026-01-26 18:26:49 +01001606int selfcheck_chip(const struct flashchip *const chip, const int idx)
1607{
1608 int ret = 0;
Nico Huber72d0ada2026-01-26 18:28:20 +01001609 char label[80];
Nico Huber32fa5082026-01-26 18:26:49 +01001610 const char *const name = chip->name != NULL ? chip->name : "unnamed";
1611
Nico Huber72d0ada2026-01-26 18:28:20 +01001612 if (idx >= 0)
1613 snprintf(label, sizeof(label), "#%d (%s)", idx, name);
1614 else
1615 snprintf(label, sizeof(label), "%s", name);
1616
Nico Huber11136c22023-05-01 12:00:09 +02001617 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE ||
1618 chip->id.type == ID_FIXME) {
Nico Huber32fa5082026-01-26 18:26:49 +01001619 ret = 1;
Nico Huber72d0ada2026-01-26 18:28:20 +01001620 msg_gerr("ERROR: Some field of flash chip %s is misconfigured.\n"
1621 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001622 }
1623 if (chip->feature_bits &
1624 (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7 |
1625 FEATURE_ANY_DUAL | FEATURE_ANY_QUAD)
1626 && !chip->prepare_access) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001627 msg_gerr("ERROR: Flash chip %s misses chip\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001628 "preparation function for 4BA and multi-i/o modes.\n"
Nico Huber72d0ada2026-01-26 18:28:20 +01001629 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001630 ret = 1;
1631 }
1632 uint8_t zero_cycles[sizeof(chip->dummy_cycles)] = { 0 };
1633 if ((chip->feature_bits & (FEATURE_QPI_35_F5 | FEATURE_QPI_38_FF)) &&
1634 !memcmp(&chip->dummy_cycles, zero_cycles, sizeof(zero_cycles))) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001635 msg_gerr("ERROR: Flash chip %s misses QPI dummy-cycle\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001636 "settings. Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +01001637 label);
Nico Huber32fa5082026-01-26 18:26:49 +01001638 ret = 1;
1639 }
1640 if (chip->reg_bits.bp[0].reg != INVALID_REG &&
1641 (!chip->wp_write_cfg || !chip->wp_read_cfg ||
1642 !chip->wp_get_ranges || !chip->decode_range)) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001643 msg_gerr("ERROR: Flash chip %s advertises block-protection\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001644 "bits, but misses one or more write-protection functions.\n"
Nico Huber72d0ada2026-01-26 18:28:20 +01001645 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001646 ret = 1;
1647 }
Nico Huber72d0ada2026-01-26 18:28:20 +01001648 if (selfcheck_eraseblocks(chip, label)) {
Nico Huber32fa5082026-01-26 18:26:49 +01001649 ret = 1;
1650 }
1651
1652 return ret;
1653}
1654
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001655int selfcheck(void)
1656{
Stefan Tauner96658be2014-05-26 22:05:31 +00001657 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001658 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001659
Thomas Heijligend45cb592021-05-19 14:12:18 +02001660 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001661 const struct programmer_entry *const p = programmer_table[i];
1662 if (p == NULL) {
1663 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1664 ret = 1;
1665 continue;
1666 }
1667 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001668 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1669 ret = 1;
1670 /* This might hide other problems with this programmer, but allows for better error
1671 * messages below without jumping through hoops. */
1672 continue;
1673 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001674 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001675 case USB:
1676 case PCI:
1677 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001678 if (p->devs.note == NULL) {
1679 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001680 break; /* This one has its device list stored separately. */
1681 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001682 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001683 ret = 1;
1684 }
1685 break;
1686 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001687 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001688 ret = 1;
1689 break;
1690 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001691 if (p->init == NULL) {
1692 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001693 ret = 1;
1694 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001695 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001696
1697 /* It would be favorable if we could check for the correct layout (especially termination) of various
1698 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1699 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1700 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1701 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1702 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001703 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001704 msg_gerr("Flashchips table miscompilation!\n");
1705 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001706 } else {
1707 for (i = 0; i < flashchips_size - 1; i++) {
Nico Huber32fa5082026-01-26 18:26:49 +01001708 if (selfcheck_chip(&flashchips[i], i))
Stefan Tauner96658be2014-05-26 22:05:31 +00001709 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001710 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001711 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001712
Stefan Tauner600576b2014-06-12 22:57:36 +00001713#if CONFIG_INTERNAL == 1
1714 ret |= selfcheck_board_enables();
1715#endif
1716
Stefan Tauner96658be2014-05-26 22:05:31 +00001717 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001718 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001719}
1720
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001721/* FIXME: This function signature needs to be improved once prepare_flash_access()
1722 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001723 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001724static int chip_safety_check(const struct flashctx *flash, int force,
1725 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001726{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001727 const struct flashchip *chip = flash->chip;
1728
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001729 if (!programmer_may_write && (write_it || erase_it)) {
1730 msg_perr("Write/erase is not working yet on your programmer in "
1731 "its current configuration.\n");
1732 /* --force is the wrong approach, but it's the best we can do
1733 * until the generic programmer parameter parser is merged.
1734 */
1735 if (!force)
1736 return 1;
1737 msg_cerr("Continuing anyway.\n");
1738 }
1739
1740 if (read_it || erase_it || write_it || verify_it) {
1741 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001742 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001743 msg_cerr("Read is not working on this chip. ");
1744 if (!force)
1745 return 1;
1746 msg_cerr("Continuing anyway.\n");
1747 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001748 if (!chip->read) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001749 msg_cerr("flashprog has no read function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001750 "flash chip.\n");
1751 return 1;
1752 }
1753 }
1754 if (erase_it || write_it) {
1755 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001756 if (chip->tested.erase == NA) {
1757 msg_cerr("Erase is not possible on this chip.\n");
1758 return 1;
1759 }
1760 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001761 msg_cerr("Erase is not working on this chip. ");
1762 if (!force)
1763 return 1;
1764 msg_cerr("Continuing anyway.\n");
1765 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001766 if(count_usable_erasers(flash) == 0) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001767 msg_cerr("flashprog has no erase function for this "
Stefan Tauner5368dca2011-07-01 00:19:12 +00001768 "flash chip.\n");
1769 return 1;
1770 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001771 }
1772 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001773 if (chip->tested.write == NA) {
1774 msg_cerr("Write is not possible on this chip.\n");
1775 return 1;
1776 }
1777 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001778 msg_cerr("Write is not working on this chip. ");
1779 if (!force)
1780 return 1;
1781 msg_cerr("Continuing anyway.\n");
1782 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001783 if (!chip->write) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001784 msg_cerr("flashprog has no write function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001785 "flash chip.\n");
1786 return 1;
1787 }
1788 }
1789 return 0;
1790}
1791
Nico Huber305f4172013-06-14 11:55:26 +02001792int prepare_flash_access(struct flashctx *const flash,
1793 const bool read_it, const bool write_it,
1794 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001795{
1796 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1797 msg_cerr("Aborting.\n");
1798 return 1;
1799 }
1800
Nico Huber3ac761c2023-01-16 02:43:17 +01001801 if (layout_sanity_checks(flash, write_it)) {
Nico Huber454f6132012-12-10 13:34:10 +00001802 msg_cerr("Requested regions can not be handled. Aborting.\n");
1803 return 1;
1804 }
1805
Nico Huber5469c152026-02-12 22:56:52 +01001806 if (flash->mst.common->adapt_voltage) {
1807 if (flash->mst.common->adapt_voltage(flash->mst.common,
1808 flash->chip->voltage.min, flash->chip->voltage.max))
1809 return 1;
1810 }
1811
Nico Huber901fb952023-01-11 23:24:23 +01001812 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_FULL))
1813 return 1;
1814
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001815 /* Initialize chip_restore_fn_count before chip unlock calls. */
1816 flash->chip_restore_fn_count = 0;
1817
Nico Huber454f6132012-12-10 13:34:10 +00001818 /* Given the existence of read locks, we want to unlock for read,
1819 erase and write. */
1820 if (flash->chip->unlock)
1821 flash->chip->unlock(flash);
1822
1823 return 0;
1824}
1825
Nico Huber305f4172013-06-14 11:55:26 +02001826void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001827{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001828 deregister_chip_restore(flash);
Nico Huber901fb952023-01-11 23:24:23 +01001829 if (flash->chip->finish_access)
1830 flash->chip->finish_access(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001831}
1832
1833/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001834 * @addtogroup flashprog-flash
Nico Huber454f6132012-12-10 13:34:10 +00001835 * @{
1836 */
1837
1838/**
1839 * @brief Erase the specified ROM chip.
1840 *
1841 * If a layout is set in the given flash context, only included regions
1842 * will be erased.
1843 *
1844 * @param flashctx The context of the flash chip to erase.
1845 * @return 0 on success.
1846 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001847int flashprog_flash_erase(struct flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +00001848{
1849 if (prepare_flash_access(flashctx, false, false, true, false))
1850 return 1;
1851
1852 const int ret = erase_by_layout(flashctx);
1853
1854 finalize_flash_access(flashctx);
1855
1856 return ret;
1857}
1858
Nico Huberc3b02dc2023-08-12 01:13:45 +02001859/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +00001860
1861/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001862 * @defgroup flashprog-ops Operations
Nico Huber454f6132012-12-10 13:34:10 +00001863 * @{
1864 */
1865
1866/**
1867 * @brief Read the current image from the specified ROM chip.
1868 *
1869 * If a layout is set in the specified flash context, only included regions
1870 * will be read.
1871 *
1872 * @param flashctx The context of the flash chip.
1873 * @param buffer Target buffer to write image to.
1874 * @param buffer_len Size of target buffer in bytes.
1875 * @return 0 on success,
1876 * 2 if buffer_len is too short for the flash chip's contents,
1877 * or 1 on any other failure.
1878 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001879int flashprog_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001880{
1881 const size_t flash_size = flashctx->chip->total_size * 1024;
1882
1883 if (flash_size > buffer_len)
1884 return 2;
1885
1886 if (prepare_flash_access(flashctx, true, false, false, false))
1887 return 1;
1888
1889 msg_cinfo("Reading flash... ");
1890
1891 int ret = 1;
1892 if (read_by_layout(flashctx, buffer)) {
1893 msg_cerr("Read operation failed!\n");
1894 msg_cinfo("FAILED.\n");
1895 goto _finalize_ret;
1896 }
1897 msg_cinfo("done.\n");
1898 ret = 0;
1899
1900_finalize_ret:
1901 finalize_flash_access(flashctx);
1902 return ret;
1903}
1904
1905static void combine_image_by_layout(const struct flashctx *const flashctx,
1906 uint8_t *const newcontents, const uint8_t *const oldcontents)
1907{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001908 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001909 const struct romentry *included;
1910 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001911
Nico Huber3d7b1e32018-12-22 00:53:14 +01001912 while ((included = layout_next_included_region(layout, start))) {
1913 if (included->start > start) {
1914 /* copy everything up to the start of this included region */
1915 memcpy(newcontents + start, oldcontents + start, included->start - start);
1916 }
1917 /* skip this included region */
1918 start = included->end + 1;
1919 if (start == 0)
1920 return;
Nico Huber454f6132012-12-10 13:34:10 +00001921 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001922
1923 /* copy the rest of the chip */
1924 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1925 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001926}
1927
1928/**
1929 * @brief Write the specified image to the ROM chip.
1930 *
1931 * If a layout is set in the specified flash context, only erase blocks
1932 * containing included regions will be touched.
1933 *
1934 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001935 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001936 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001937 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001938 * @return 0 on success,
1939 * 4 if buffer_len doesn't match the size of the flash chip,
1940 * 3 if write was tried but nothing has changed,
1941 * 2 if write failed and flash contents changed,
1942 * or 1 on any other failure.
1943 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001944int flashprog_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001945 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001946{
1947 const size_t flash_size = flashctx->chip->total_size * 1024;
1948 const bool verify_all = flashctx->flags.verify_whole_chip;
1949 const bool verify = flashctx->flags.verify_after_write;
Nico Huberc3b02dc2023-08-12 01:13:45 +02001950 const struct flashprog_layout *const verify_layout =
Nico Huber74d09d42019-06-16 03:27:26 +02001951 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001952
1953 if (buffer_len != flash_size)
1954 return 4;
1955
1956 int ret = 1;
1957
1958 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001959 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001960 uint8_t *const curcontents = malloc(flash_size);
1961 uint8_t *oldcontents = NULL;
1962 if (verify_all)
1963 oldcontents = malloc(flash_size);
1964 if (!curcontents || (verify_all && !oldcontents)) {
1965 msg_gerr("Out of memory!\n");
1966 goto _free_ret;
1967 }
1968
1969#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001970 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001971 if (flashctx->flags.force_boardmismatch) {
1972 msg_pinfo("Proceeding anyway because user forced us to.\n");
1973 } else {
1974 msg_perr("Aborting. You can override this with "
1975 "-p internal:boardmismatch=force.\n");
1976 goto _free_ret;
1977 }
1978 }
1979#endif
1980
1981 if (prepare_flash_access(flashctx, false, true, false, verify))
1982 goto _free_ret;
1983
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001984 /* If given, assume flash chip contains same data as `refcontents`. */
1985 if (refcontents) {
1986 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1987 memcpy(curcontents, refcontents, flash_size);
1988 if (oldcontents)
1989 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001990 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001991 /*
1992 * Read the whole chip to be able to check whether regions need to be
1993 * erased and to give better diagnostics in case write fails.
1994 * The alternative is to read only the regions which are to be
1995 * preserved, but in that case we might perform unneeded erase which
1996 * takes time as well.
1997 */
1998 msg_cinfo("Reading old flash chip contents... ");
1999 if (verify_all) {
Richard Hughes842d6782021-01-15 09:48:12 +00002000 if (flashprog_read_range(flashctx, oldcontents, 0, flash_size)) {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002001 msg_cinfo("FAILED.\n");
2002 goto _finalize_ret;
2003 }
2004 memcpy(curcontents, oldcontents, flash_size);
2005 } else {
2006 if (read_by_layout(flashctx, curcontents)) {
2007 msg_cinfo("FAILED.\n");
2008 goto _finalize_ret;
2009 }
Nico Huber454f6132012-12-10 13:34:10 +00002010 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002011 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00002012 }
Nico Huber454f6132012-12-10 13:34:10 +00002013
2014 if (write_by_layout(flashctx, curcontents, newcontents)) {
2015 msg_cerr("Uh oh. Erase/write failed. ");
2016 ret = 2;
2017 if (verify_all) {
2018 msg_cerr("Checking if anything has changed.\n");
2019 msg_cinfo("Reading current flash chip contents... ");
Richard Hughes842d6782021-01-15 09:48:12 +00002020 if (!flashprog_read_range(flashctx, curcontents, 0, flash_size)) {
Nico Huber454f6132012-12-10 13:34:10 +00002021 msg_cinfo("done.\n");
2022 if (!memcmp(oldcontents, curcontents, flash_size)) {
2023 nonfatal_help_message();
2024 goto _finalize_ret;
2025 }
2026 msg_cerr("Apparently at least some data has changed.\n");
2027 } else
2028 msg_cerr("Can't even read anymore!\n");
2029 emergency_help_message();
2030 goto _finalize_ret;
2031 } else {
2032 msg_cerr("\n");
2033 }
2034 emergency_help_message();
2035 goto _finalize_ret;
2036 }
2037
2038 /* Verify only if we actually changed something. */
2039 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00002040 msg_cinfo("Verifying flash... ");
2041
Nico Huber74d09d42019-06-16 03:27:26 +02002042 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00002043 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02002044 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00002045 /* If we tried to write, and verification now fails, we
2046 might have an emergency situation. */
2047 if (ret)
2048 emergency_help_message();
2049 else
2050 msg_cinfo("VERIFIED.\n");
2051 } else {
2052 /* We didn't change anything. */
2053 ret = 0;
2054 }
2055
2056_finalize_ret:
2057 finalize_flash_access(flashctx);
2058_free_ret:
2059 free(oldcontents);
2060 free(curcontents);
2061 return ret;
2062}
2063
2064/**
2065 * @brief Verify the ROM chip's contents with the specified image.
2066 *
2067 * If a layout is set in the specified flash context, only included regions
2068 * will be verified.
2069 *
2070 * @param flashctx The context of the flash chip.
2071 * @param buffer Source buffer to verify with.
2072 * @param buffer_len Size of source buffer in bytes.
2073 * @return 0 on success,
2074 * 3 if the chip's contents don't match,
2075 * 2 if buffer_len doesn't match the size of the flash chip,
2076 * or 1 on any other failure.
2077 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02002078int flashprog_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00002079{
Nico Huberc3b02dc2023-08-12 01:13:45 +02002080 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00002081 const size_t flash_size = flashctx->chip->total_size * 1024;
2082
2083 if (buffer_len != flash_size)
2084 return 2;
2085
2086 const uint8_t *const newcontents = buffer;
2087 uint8_t *const curcontents = malloc(flash_size);
2088 if (!curcontents) {
2089 msg_gerr("Out of memory!\n");
2090 return 1;
2091 }
2092
2093 int ret = 1;
2094
2095 if (prepare_flash_access(flashctx, false, false, false, true))
2096 goto _free_ret;
2097
2098 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02002099 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00002100 if (!ret)
2101 msg_cinfo("VERIFIED.\n");
2102
2103 finalize_flash_access(flashctx);
2104_free_ret:
2105 free(curcontents);
2106 return ret;
2107}
2108
Nico Huberc3b02dc2023-08-12 01:13:45 +02002109/** @} */ /* end flashprog-ops */