blob: 6e52bba6b45cb2a1663a4bf99f038773551ea8b9 [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 Hailfingerd1be52d2010-07-03 12:14:25 +000044/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020045bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000046
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000047#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000048static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000049/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000050static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000051 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000052 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000053} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000054/* Initialize to 0 to make sure nobody registers a shutdown function before
55 * programmer init.
56 */
Felix Singerf25447e2022-08-19 02:44:28 +020057static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000058
Stefan Taunerc4f44df2013-08-12 22:58:43 +000059/* Did we change something or was every erase/write skipped (if any)? */
60static bool all_skipped = true;
61
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000062static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000063
Stefan Tauner2a1ed772014-08-31 00:09:21 +000064int shutdown_free(void *data)
65{
66 free(data);
67 return 0;
68}
69
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000070/* Register a function to be executed on programmer shutdown.
71 * The advantage over atexit() is that you can supply a void pointer which will
72 * be used as parameter to the registered function upon programmer shutdown.
73 * This pointer can point to arbitrary data used by said function, e.g. undo
74 * information for GPIO settings etc. If unneeded, set data=NULL.
75 * Please note that the first (void *data) belongs to the function signature of
76 * the function passed as first parameter.
77 */
David Hendricks8bb20212011-06-14 01:35:36 +000078int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000079{
80 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000081 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000082 SHUTDOWN_MAXFN);
83 return 1;
84 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000085 if (!may_register_shutdown) {
86 msg_perr("Tried to register a shutdown function before "
87 "programmer init.\n");
88 return 1;
89 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000090 shutdown_fn[shutdown_fn_count].func = function;
91 shutdown_fn[shutdown_fn_count].data = data;
92 shutdown_fn_count++;
93
94 return 0;
95}
96
Nikolai Artemiev4ad48642020-11-05 13:54:27 +110097int register_chip_restore(chip_restore_fn_cb_t func,
98 struct flashctx *flash, uint8_t status)
99{
100 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
101 msg_perr("Tried to register more than %i chip restore"
102 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
103 return 1;
104 }
105 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
106 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
107 flash->chip_restore_fn_count++;
108
109 return 0;
110}
111
112static int deregister_chip_restore(struct flashctx *flash)
113{
114 int rc = 0;
115
116 while (flash->chip_restore_fn_count > 0) {
117 int i = --flash->chip_restore_fn_count;
118 rc |= flash->chip_restore_fn[i].func(
119 flash, flash->chip_restore_fn[i].status);
120 }
121
122 return rc;
123}
124
Nico Huber2b66ad92023-01-11 20:15:15 +0100125int programmer_init(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000126{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000127 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000128
Nico Huber2b66ad92023-01-11 20:15:15 +0100129 if (prog == NULL || prog->driver == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000130 msg_perr("Invalid programmer specified!\n");
131 return -1;
132 }
Nico Huber2b66ad92023-01-11 20:15:15 +0100133 programmer = prog->driver;
134 programmer_param = prog->param;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000135 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200136 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000137 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200138 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000139
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200140 msg_pdbg("Initializing %s programmer\n", programmer->name);
Nico Hubere3a26882023-01-11 21:45:51 +0100141 ret = programmer->init(prog);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000142 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000143 if (ret != 0) {
144 /* It is quite possible that any unhandled programmer parameter would have been valid,
145 * but an error in actual programmer init happened before the parameter was evaluated.
146 */
147 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
148 programmer_param);
149 } else {
150 /* Actual programmer init was successful, but the user specified an invalid or unusable
151 * (for the current programmer configuration) parameter.
152 */
153 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
154 msg_perr("Aborting.\n");
155 ret = ERROR_FATAL;
156 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000157 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200158 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000159 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000160}
161
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000162/** Calls registered shutdown functions and resets internal programmer-related variables.
163 * Calling it is safe even without previous initialization, but further interactions with programmer support
164 * require a call to programmer_init() (afterwards).
165 *
166 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Nico Huber2b66ad92023-01-11 20:15:15 +0100167int programmer_shutdown(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000168{
David Hendricks8bb20212011-06-14 01:35:36 +0000169 int ret = 0;
170
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000171 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200172 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000173 while (shutdown_fn_count > 0) {
174 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000175 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000176 }
Nico Huberaf9d7382023-05-01 13:33:26 +0200177
178 int i;
179 for (i = 0; i < registered_master_count; ++i) {
180 struct found_id *found_id, *next;
181 for (found_id = registered_masters[i].found_ids; found_id; found_id = next) {
182 next = found_id->next;
183 free(found_id);
184 }
185 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000186 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000187
David Hendricks8bb20212011-06-14 01:35:36 +0000188 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000189}
190
Stefan Taunerf80419c2014-05-02 15:41:42 +0000191void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000192{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000193 if (usecs > 0) {
194 if (programmer->delay)
195 programmer->delay(usecs);
196 else
197 internal_delay(usecs);
198 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000199}
200
Nico Hubercdcfda22023-04-29 13:29:33 +0200201static int read_memmapped_chunk(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000202{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000203 chip_readn(flash, buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000204 return 0;
205}
Nico Hubercdcfda22023-04-29 13:29:33 +0200206int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
207{
208 return flashprog_read_chunked(flash, buf, start, len, MAX_DATA_READ_UNLIMITED, read_memmapped_chunk);
209}
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000210
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000211/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000212 * It will look for needle with a subsequent '=' in haystack, return a copy of
213 * needle and remove everything from the first occurrence of needle to the next
214 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000215 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200216static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000217{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000218 char *param_pos, *opt_pos, *rest;
219 char *opt = NULL;
220 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000221 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000222
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000223 needlelen = strlen(needle);
224 if (!needlelen) {
225 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200226 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000227 return NULL;
228 }
229 /* No programmer parameters given. */
230 if (*haystack == NULL)
231 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000232 param_pos = strstr(*haystack, needle);
233 do {
234 if (!param_pos)
235 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000236 /* Needle followed by '='? */
237 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000238 /* Beginning of the string? */
239 if (param_pos == *haystack)
240 break;
241 /* After a delimiter? */
242 if (strchr(delim, *(param_pos - 1)))
243 break;
244 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000245 /* Continue searching. */
246 param_pos++;
247 param_pos = strstr(param_pos, needle);
248 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000249
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000250 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000251 /* Get the string after needle and '='. */
252 opt_pos = param_pos + needlelen + 1;
253 optlen = strcspn(opt_pos, delim);
254 /* Return an empty string if the parameter was empty. */
255 opt = malloc(optlen + 1);
256 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000257 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000258 exit(1);
259 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000260 strncpy(opt, opt_pos, optlen);
261 opt[optlen] = '\0';
262 rest = opt_pos + optlen;
263 /* Skip all delimiters after the current parameter. */
264 rest += strspn(rest, delim);
265 memmove(param_pos, rest, strlen(rest) + 1);
266 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000267 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000268
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000269 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000270}
271
Stefan Tauner66652442011-06-26 17:38:17 +0000272char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000273{
274 return extract_param(&programmer_param, param_name, ",");
275}
276
Richard Hughes842d6782021-01-15 09:48:12 +0000277static void flashprog_progress_report(struct flashprog_progress *const p)
278{
279 if (p->current > p->total) {
280 msg_gdbg2("Sanitizing progress report: %zu bytes off.", p->current - p->total);
281 p->current = p->total;
282 }
283
284 if (!p->callback)
285 return;
286
287 p->callback(p->stage, p->current, p->total, p->user_data);
288}
289
290static void flashprog_progress_start(struct flashprog_flashctx *const flashctx,
291 const enum flashprog_progress_stage stage, const size_t total)
292{
293 flashctx->progress.stage = stage;
294 flashctx->progress.current = 0;
295 flashctx->progress.total = total;
296 flashprog_progress_report(&flashctx->progress);
297}
298
299static void flashprog_progress_start_by_layout(struct flashprog_flashctx *const flashctx,
300 const enum flashprog_progress_stage stage,
301 const struct flashprog_layout *const layout)
302{
303 const struct romentry *entry = NULL;
304 size_t total = 0;
305
306 while ((entry = layout_next_included(layout, entry)))
307 total += entry->end - entry->start + 1;
308
309 flashprog_progress_start(flashctx, stage, total);
310}
311
312static void flashprog_progress_set(struct flashprog_flashctx *const flashctx, const size_t current)
313{
314 flashctx->progress.current = current;
315 flashprog_progress_report(&flashctx->progress);
316}
317
318/** @private */
319void flashprog_progress_add(struct flashprog_flashctx *const flashctx, const size_t progress)
320{
321 flashctx->progress.current += progress;
322 flashprog_progress_report(&flashctx->progress);
323}
324
325static void flashprog_progress_finish(struct flashprog_flashctx *const flashctx)
326{
327 if (flashctx->progress.current == flashctx->progress.total)
328 return;
329
330 flashctx->progress.current = flashctx->progress.total;
331 flashprog_progress_report(&flashctx->progress);
332}
333
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000334/* Returns the number of well-defined erasers for a chip. */
Nico Huberbb8ab372026-03-14 11:46:02 +0100335unsigned int flashprog_count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000336{
337 unsigned int usable_erasefunctions = 0;
338 int k;
339 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
340 if (!check_block_eraser(flash, k, 0))
341 usable_erasefunctions++;
342 }
343 return usable_erasefunctions;
344}
345
Mark Marshallf20b7be2014-05-09 21:16:21 +0000346static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000347{
348 int ret = 0, failcount = 0;
349 unsigned int i;
350 for (i = 0; i < len; i++) {
351 if (wantbuf[i] != havebuf[i]) {
352 /* Only print the first failure. */
353 if (!failcount++)
354 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
355 start + i, wantbuf[i], havebuf[i]);
356 }
357 }
358 if (failcount) {
359 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
360 start, start + len - 1, failcount);
361 ret = -1;
362 }
363 return ret;
364}
365
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000366/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600367static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000368{
369 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300370 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000371
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100372 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000373 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100374 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100375 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000376 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300377 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000378 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100379
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000380 free(cmpbuf);
381 return ret;
382}
383
Richard Hughes842d6782021-01-15 09:48:12 +0000384int flashprog_read_range(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
385{
386 flashprog_progress_start(flash, FLASHPROG_PROGRESS_READ, len);
387 const int ret = flash->chip->read(flash, buf, start, len);
388 flashprog_progress_finish(flash);
389 return ret;
390}
391
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000392/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000393 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000394 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000395 * @start offset to the base address of the flash chip
396 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000397 * @return 0 for success, -1 for failure
398 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000399int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000400{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000401 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000402 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000403
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100404 if (start + len > flash->chip->total_size * 1024) {
405 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
406 " total_size 0x%x\n", __func__, start, len,
407 flash->chip->total_size * 1024);
408 return -1;
409 }
410
Stefan Taunerdf64a422014-05-27 00:06:14 +0000411 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000412 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100413 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000414 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000415 }
416
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100417 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000418 if (ret) {
419 msg_gerr("Verification impossible because read failed "
420 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000421 ret = -1;
422 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000423 }
424
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000425 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000426out_free:
427 free(readbuf);
428 return ret;
429}
430
Nico Huber3ac761c2023-01-16 02:43:17 +0100431size_t gran_to_bytes(const enum write_granularity gran)
Nico Huberb77607f2023-01-16 02:25:45 +0100432{
433 switch (gran) {
434 case write_gran_1bit: return 1;
435 case write_gran_1byte: return 1;
436 case write_gran_1byte_implicit_erase: return 1;
437 case write_gran_128bytes: return 128;
438 case write_gran_256bytes: return 256;
439 case write_gran_264bytes: return 264;
440 case write_gran_512bytes: return 512;
441 case write_gran_528bytes: return 528;
442 case write_gran_1024bytes: return 1024;
443 case write_gran_1056bytes: return 1056;
444 default: return 0;
445 }
446}
447
Stefan Tauner02437452013-04-01 19:34:53 +0000448/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300449static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
450 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000451{
452 unsigned int i, j, limit;
453 for (j = 0; j < len / gran; j++) {
454 limit = min (gran, len - j * gran);
455 /* Are 'have' and 'want' identical? */
456 if (!memcmp(have + j * gran, want + j * gran, limit))
457 continue;
458 /* have needs to be in erased state. */
459 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300460 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000461 return 1;
462 }
463 return 0;
464}
465
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000466/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000467 * Check if the buffer @have can be programmed to the content of @want without
468 * erasing. This is only possible if all chunks of size @gran are either kept
469 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000470 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000471 * Warning: This function assumes that @have and @want point to naturally
472 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000473 *
474 * @have buffer with current content
475 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000476 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000477 * @gran write granularity (enum, not count)
478 * @return 0 if no erase is needed, 1 otherwise
479 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000480static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
481 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000482{
Stefan Tauner02437452013-04-01 19:34:53 +0000483 unsigned int i;
Nico Huberb77607f2023-01-16 02:25:45 +0100484 size_t stride;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000485
486 switch (gran) {
487 case write_gran_1bit:
Nico Huberb77607f2023-01-16 02:25:45 +0100488 for (i = 0; i < len; i++) {
489 if ((have[i] & want[i]) != want[i])
490 return 1;
491 }
492 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000493 case write_gran_1byte:
Nico Huberb77607f2023-01-16 02:25:45 +0100494 for (i = 0; i < len; i++) {
495 if ((have[i] != want[i]) && (have[i] != erased_value))
496 return 1;
497 }
498 return 0;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000499 case write_gran_1byte_implicit_erase:
500 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
Nico Huberb77607f2023-01-16 02:25:45 +0100501 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000502 default:
Nico Huberb77607f2023-01-16 02:25:45 +0100503 stride = gran_to_bytes(gran);
504 if (stride) {
505 return need_erase_gran_bytes(have, want, len, stride, erased_value);
506 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000507 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200508 "flashprog@flashprog.org\n", __func__);
Nico Huberb77607f2023-01-16 02:25:45 +0100509 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000510 }
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000511}
512
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000513/**
514 * Check if the buffer @have needs to be programmed to get the content of @want.
515 * If yes, return 1 and fill in first_start with the start address of the
516 * write operation and first_len with the length of the first to-be-written
517 * chunk. If not, return 0 and leave first_start and first_len undefined.
518 *
519 * Warning: This function assumes that @have and @want point to naturally
520 * aligned regions.
521 *
522 * @have buffer with current content
523 * @want buffer with desired content
524 * @len length of the checked area
525 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000526 * @first_start offset of the first byte which needs to be written (passed in
527 * value is increased by the offset of the first needed write
528 * relative to have/want or unchanged if no write is needed)
529 * @return length of the first contiguous area which needs to be written
530 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000531 *
532 * FIXME: This function needs a parameter which tells it about coalescing
533 * in relation to the max write length of the programmer and the max write
534 * length of the chip.
535 */
Nico Huber3b9c86d2023-01-15 22:58:06 +0100536static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, chipsize_t len,
537 chipoff_t *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000538{
Felix Singerf25447e2022-08-19 02:44:28 +0200539 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000540 unsigned int rel_start = 0, first_len = 0;
Nico Huberb77607f2023-01-16 02:25:45 +0100541 unsigned int i, limit;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000542
Nico Huberb77607f2023-01-16 02:25:45 +0100543 const size_t stride = gran_to_bytes(gran);
544 if (!stride) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000545 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200546 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000547 /* Claim that no write was needed. A write with unknown
548 * granularity is too dangerous to try.
549 */
550 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000551 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000552 for (i = 0; i < len / stride; i++) {
553 limit = min(stride, len - i * stride);
554 /* Are 'have' and 'want' identical? */
555 if (memcmp(have + i * stride, want + i * stride, limit)) {
556 if (!need_write) {
557 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200558 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000559 rel_start = i * stride;
560 }
561 } else {
562 if (need_write) {
563 /* First location where have and want
564 * do not differ anymore.
565 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000566 break;
567 }
568 }
569 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000570 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000571 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000572 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000573 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000574}
575
Nico Huber2d625722016-05-03 10:48:02 +0200576/*
577 * Return a string corresponding to the bustype parameter.
578 * Memory is obtained with malloc() and must be freed with free() by the caller.
579 */
580char *flashbuses_to_text(enum chipbustype bustype)
581{
582 char *ret = calloc(1, 1);
583 /*
584 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
585 * will cease to exist and should be eliminated here as well.
586 */
Nico Huber0d2b45e2026-03-23 22:42:46 +0100587 if (bustype == BUS_PRESPI) {
Nico Huber2d625722016-05-03 10:48:02 +0200588 ret = strcat_realloc(ret, "Non-SPI, ");
589 } else {
590 if (bustype & BUS_PARALLEL)
591 ret = strcat_realloc(ret, "Parallel, ");
592 if (bustype & BUS_LPC)
593 ret = strcat_realloc(ret, "LPC, ");
594 if (bustype & BUS_FWH)
595 ret = strcat_realloc(ret, "FWH, ");
596 if (bustype & BUS_SPI)
597 ret = strcat_realloc(ret, "SPI, ");
Nico Huber0d2b45e2026-03-23 22:42:46 +0100598 if (bustype & BUS_OPAQUE)
Nico Huber2d625722016-05-03 10:48:02 +0200599 ret = strcat_realloc(ret, "Programmer-specific, ");
600 if (bustype == BUS_NONE)
601 ret = strcat_realloc(ret, "None, ");
602 }
603 /* Kill last comma. */
604 ret[strlen(ret) - 2] = '\0';
605 ret = realloc(ret, strlen(ret) + 1);
606 return ret;
607}
608
Nico Huber484ef612026-03-08 21:02:52 +0100609void flashprog_bus_probe(struct registered_master *const mst, const struct flashchip *const chip)
Nico Huberaf9d7382023-05-01 13:33:26 +0200610{
Nico Huberac138732026-02-28 17:42:27 +0100611 unsigned int least_priority, priority, i;
Nico Huberaf9d7382023-05-01 13:33:26 +0200612 struct found_id **next_ptr;
Nico Huberaf9d7382023-05-01 13:33:26 +0200613
614 if (mst->probed)
615 return;
616
Nico Huberac138732026-02-28 17:42:27 +0100617 for (i = 0, least_priority = 0; i < mst->probing.probe_count; ++i) {
618 if (least_priority < mst->probing.probes[i].priority)
619 least_priority = mst->probing.probes[i].priority;
620 }
621
Nico Huberaf9d7382023-05-01 13:33:26 +0200622 next_ptr = &mst->found_ids;
Nico Huberac138732026-02-28 17:42:27 +0100623 for (priority = 0; priority <= least_priority; ++priority) {
624 bool found = false;
Nico Huberaf9d7382023-05-01 13:33:26 +0200625
Nico Huberac138732026-02-28 17:42:27 +0100626 for (i = 0; i < mst->probing.probe_count; ++i) {
627 if (mst->probing.probes[i].priority != priority)
628 continue;
Nico Huberaf9d7382023-05-01 13:33:26 +0200629
Nico Huberdae90222026-03-09 20:36:56 +0100630 if (chip && chip->id.type != mst->probing.probes[i].type)
Nico Huberac138732026-02-28 17:42:27 +0100631 continue;
632
Nico Huberdae90222026-03-09 20:36:56 +0100633 *next_ptr = mst->probing.probes[i].run(&mst->probing.probes[i], &mst->common, chip);
Nico Huberac138732026-02-28 17:42:27 +0100634 found |= !!*next_ptr;
635
636 /* walk to end in case multiple IDs were found in a single call */
637 while (*next_ptr)
638 next_ptr = &(*next_ptr)->next;
639 }
640
641 /* Skip lower-priority probing if anything was found. */
642 if (found)
643 break;
Nico Huberaf9d7382023-05-01 13:33:26 +0200644 }
645
646 mst->probed = true;
647}
648
Nico Huber484ef612026-03-08 21:02:52 +0100649bool flashprog_chip_match(struct registered_master *const mst, const struct flashchip *const chip)
Nico Huberaf9d7382023-05-01 13:33:26 +0200650{
651 static const char *const id_names[] = {
652 [ID_82802AB] = "82802AB",
Nico Huberaf9d7382023-05-01 13:33:26 +0200653 [ID_JEDEC] = "JEDEC",
654 [ID_JEDEC_29GL] = "JEDEC_29GL",
655 [ID_OPAQUE] = "OPAQUE",
656 [ID_SPI_AT25F] = "SPI_AT25F",
657 [ID_SPI_RDID] = "SPI_RDID",
Nico Huberaf9d7382023-05-01 13:33:26 +0200658 [ID_SPI_REMS] = "SPI_REMS",
659 [ID_SPI_RES1] = "SPI_RES1",
660 [ID_SPI_RES2] = "SPI_RES2",
661 [ID_SPI_RES3] = "SPI_RES3",
662 [ID_SPI_SFDP] = "SPI_SFDP",
663 [ID_SPI_ST95] = "SPI_ST95",
664 [ID_W29EE011] = "W29EE011",
665 [ID_EDI] = "EDI",
666 };
667
Nico Huber484ef612026-03-08 21:02:52 +0100668 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
669
Nico Huberaf9d7382023-05-01 13:33:26 +0200670 if (chip_to_probe) {
Nico Huber966dc9b2026-03-07 21:59:15 +0100671 /* We are looking for a particular chip.
672 If it can't be probed, assume it's there... */
673 if (chip->id.type == ID_NONE)
674 return true;
Nico Huberdae90222026-03-09 20:36:56 +0100675 /* ...otherwise, limit the probing sequences by its properties. */
Nico Huber484ef612026-03-08 21:02:52 +0100676 flashprog_bus_probe(mst, chip);
Nico Huberaf9d7382023-05-01 13:33:26 +0200677 } else {
Nico Huber484ef612026-03-08 21:02:52 +0100678 flashprog_bus_probe(mst, NULL);
Nico Huberaf9d7382023-05-01 13:33:26 +0200679 }
680
681 struct found_id *found_id;
682 for (found_id = mst->found_ids; found_id; found_id = found_id->next) {
683 if (found_id->info.id.type != chip->id.type)
684 continue;
685
686 if (found_id->info.id.type < ARRAY_SIZE(id_names))
687 msg_cdbg("%s: id1 0x%02x, id2 0x%02x ",
688 id_names[found_id->info.id.type],
689 found_id->info.id.id1, found_id->info.id.id2);
690
691 if (mst->probing.match(chip, &found_id->info))
692 break;
693 }
694
695 msg_cdbg("\n");
696 return !!found_id;
697}
698
Stefan Tauner96658be2014-05-26 22:05:31 +0000699/* Even if an error is found, the function will keep going and check the rest. */
Nico Huber72d0ada2026-01-26 18:28:20 +0100700static int selfcheck_eraseblocks(const struct flashchip *chip, const char *label)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000701{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000702 int i, j, k;
703 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530704 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000705
706 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
707 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000708 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530709 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000710
711 for (i = 0; i < NUM_ERASEREGIONS; i++) {
712 /* Blocks with zero size are bugs in flashchips.c. */
713 if (eraser.eraseblocks[i].count &&
714 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000715 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200716 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100717 label, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000718 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000719 }
720 /* Blocks with zero count are bugs in flashchips.c. */
721 if (!eraser.eraseblocks[i].count &&
722 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000723 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200724 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100725 label, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000726 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000727 }
728 done += eraser.eraseblocks[i].count *
729 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530730 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000731 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000732 /* Empty eraseblock definition with erase function. */
733 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000734 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000735 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000736 if (!done)
737 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000738 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000739 msg_gerr("ERROR: Flash chip %s erase function %i "
740 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000741 " expected 0x%06x bytes.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200742 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100743 label, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000744 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000745 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000746 if (!eraser.block_erase)
747 continue;
748 /* Check if there are identical erase functions for different
749 * layouts. That would imply "magic" erase functions. The
750 * easiest way to check this is with function pointers.
751 */
Uwe Hermann43959702010-03-13 17:28:29 +0000752 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000753 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000754 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000755 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200756 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100757 label, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000758 ret = 1;
759 }
Uwe Hermann43959702010-03-13 17:28:29 +0000760 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530761 if(curr_eraseblock_count > prev_eraseblock_count)
762 {
Nico Huberac90af62022-12-18 00:22:47 +0000763 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200764 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100765 label, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530766 ret = 1;
767 }
768 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000769 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000770 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000771}
772
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000773static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000774{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000775 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000776
777 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
778 if (log)
779 msg_cdbg("not defined. ");
780 return 1;
781 }
782 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
783 if (log)
784 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000785 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000786 return 1;
787 }
788 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
789 if (log)
790 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000791 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000792 return 1;
793 }
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530794
Nico Hubere149fbe2024-08-21 10:23:53 +0200795 if (flash->chip->bustype == BUS_SPI && flash->chip->spi_cmd_set == SPI25) {
Nico Huber13389362024-03-05 18:35:30 +0100796 bool native_4ba;
Nico Huber13f97a52023-01-14 23:55:06 +0100797 int i;
Nico Huber13389362024-03-05 18:35:30 +0100798
799 const uint8_t *opcode = spi_get_opcode_from_erasefn(eraser.block_erase, &native_4ba);
Nico Huber07ebc682024-08-21 10:18:27 +0200800 if (!opcode)
801 return 1;
802
Nico Huber13f97a52023-01-14 23:55:06 +0100803 for (i = 0; opcode[i]; i++) {
Nico Huber13389362024-03-05 18:35:30 +0100804 if ((native_4ba && !spi_master_4ba(flash)) ||
Nico Huber9a11cbf2023-01-13 01:19:07 +0100805 !flash->mst.spi->probe_opcode(flash, opcode[i])) {
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530806 if (log)
807 msg_cdbg("block erase function and layout found "
808 "but SPI master doesn't support the function. ");
809 return 1;
810 }
811 }
812 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000813 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000814 return 0;
815}
816
Nico Huber7af0e792016-04-29 16:40:15 +0200817/**
818 * @brief Reads the included layout regions into a buffer.
819 *
820 * If there is no layout set in the given flash context, the whole chip will
821 * be read.
822 *
823 * @param flashctx Flash context to be used.
824 * @param buffer Buffer of full chip size to read into.
825 * @return 0 on success,
826 * 1 if any read fails.
827 */
828static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
829{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200830 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200831 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200832
Richard Hughes842d6782021-01-15 09:48:12 +0000833 flashprog_progress_start_by_layout(flashctx, FLASHPROG_PROGRESS_READ, layout);
834
Nico Huber5ca55232019-06-15 22:29:08 +0200835 while ((entry = layout_next_included(layout, entry))) {
836 const chipoff_t region_start = entry->start;
837 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200838
839 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
840 return 1;
841 }
Richard Hughes842d6782021-01-15 09:48:12 +0000842
843 flashprog_progress_finish(flashctx);
844
Nico Huber7af0e792016-04-29 16:40:15 +0200845 return 0;
846}
847
Nico Huber7af0e792016-04-29 16:40:15 +0200848/**
849 * @private
850 *
851 * For read-erase-write, `curcontents` and `newcontents` shall point
852 * to buffers of the chip's size. Both are supposed to be prefilled
853 * with at least the included layout regions of the current flash
854 * contents (`curcontents`) and the data to be written to the flash
855 * (`newcontents`).
856 *
857 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
858 *
859 * The `chipoff_t` values are used internally by `walk_by_layout()`.
860 */
861struct walk_info {
862 uint8_t *curcontents;
863 const uint8_t *newcontents;
864 chipoff_t region_start;
865 chipoff_t region_end;
866 chipoff_t erase_start;
867 chipoff_t erase_end;
868};
Nico Huber3b9c86d2023-01-15 22:58:06 +0100869
Nico Huberf9666632026-06-16 12:53:27 +0200870/** @private */
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530871struct eraseblock_data {
872 chipoff_t start_addr;
873 chipoff_t end_addr;
874 bool selected;
875 size_t block_num;
876 size_t first_sub_block_index;
877 size_t last_sub_block_index;
878};
879
Nico Huberf9666632026-06-16 12:53:27 +0200880/** @private */
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530881struct erase_layout {
882 struct eraseblock_data* layout_list;
883 size_t block_count;
884 const struct block_eraser *eraser;
885};
886
Nico Huber5ff6fdc2023-01-15 23:43:12 +0100887static bool explicit_erase(const struct walk_info *const info)
888{
889 /* For explicit erase, we are called without new contents. */
890 return !info->newcontents;
891}
892
Nico Huberd96e7032023-01-14 22:31:48 +0100893static size_t calculate_block_count(const struct block_eraser *const eraser)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530894{
Nico Huberd96e7032023-01-14 22:31:48 +0100895 size_t block_count = 0, i;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530896
Nico Huberd96e7032023-01-14 22:31:48 +0100897 for (i = 0; i < ARRAY_SIZE(eraser->eraseblocks); ++i)
898 block_count += eraser->eraseblocks[i].count;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530899
900 return block_count;
901}
902
903static void init_eraseblock(struct erase_layout *layout, size_t idx, size_t block_num,
904 chipoff_t start_addr, chipoff_t end_addr, size_t *sub_block_index)
905{
906 struct eraseblock_data *edata = &layout[idx].layout_list[block_num];
907 edata->start_addr = start_addr;
908 edata->end_addr = end_addr;
909 edata->selected = false;
910 edata->block_num = block_num;
911
912 if (!idx)
913 return;
Nico Hubera02df332023-01-14 23:06:27 +0100914 const struct erase_layout *const sub_layout = &layout[idx - 1];
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530915
916 edata->first_sub_block_index = *sub_block_index;
Nico Hubera02df332023-01-14 23:06:27 +0100917 for (; *sub_block_index < sub_layout->block_count; ++*sub_block_index) {
918 if (sub_layout->layout_list[*sub_block_index].end_addr > end_addr)
919 break;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530920 }
921 edata->last_sub_block_index = *sub_block_index - 1;
922}
923
924/*
925 * @brief Function to free the created erase_layout
926 *
927 * @param layout pointer to allocated layout
928 * @param erasefn_count number of erase functions for which the layout was created
929 *
930 */
931static void free_erase_layout(struct erase_layout *layout, unsigned int erasefn_count)
932{
Nico Huber13f97a52023-01-14 23:55:06 +0100933 size_t i;
934
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530935 if (!layout)
936 return;
Nico Huber13f97a52023-01-14 23:55:06 +0100937 for (i = 0; i < erasefn_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530938 free(layout[i].layout_list);
939 }
940 free(layout);
941}
942
943/*
944 * @brief Function to create an erase layout
945 *
946 * @param flashctx flash context
947 * @param e_layout address to the pointer to store the layout
948 * @return 0 on success,
949 * -1 if layout creation fails
950 *
951 * This function creates a layout of which erase functions erase which regions
952 * of the flash chip. This helps to optimally select the erase functions for
953 * erase/write operations.
954 */
Nico Huberc09fca42023-01-29 15:58:09 +0100955static int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530956{
957 const struct flashchip *chip = flashctx->chip;
Nico Huberbb8ab372026-03-14 11:46:02 +0100958 const size_t erasefn_count = flashprog_count_usable_erasers(flashctx);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530959
960 if (!erasefn_count) {
961 msg_gerr("No erase functions supported\n");
962 return 0;
963 }
964
Nico Huberd34864b2023-01-14 23:47:19 +0100965 struct erase_layout *layout = calloc(erasefn_count, sizeof(struct erase_layout));
966 if (!layout) {
967 msg_gerr("Out of memory!\n");
968 return -1;
969 }
970
Nico Huber13f97a52023-01-14 23:55:06 +0100971 size_t layout_idx = 0, eraser_idx;
972 for (eraser_idx = 0; eraser_idx < NUM_ERASEFUNCTIONS; eraser_idx++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530973 if (check_block_eraser(flashctx, eraser_idx, 0))
974 continue;
975
976 layout[layout_idx].eraser = &chip->block_erasers[eraser_idx];
Nico Huberd96e7032023-01-14 22:31:48 +0100977 const size_t block_count = calculate_block_count(&chip->block_erasers[eraser_idx]);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530978 size_t sub_block_index = 0;
979
980 layout[layout_idx].block_count = block_count;
981 layout[layout_idx].layout_list = (struct eraseblock_data *)calloc(block_count,
982 sizeof(struct eraseblock_data));
983
984 if (!layout[layout_idx].layout_list) {
985 free_erase_layout(layout, layout_idx);
986 return -1;
987 }
988
989 size_t block_num = 0;
990 chipoff_t start_addr = 0;
991
Nico Huber13f97a52023-01-14 23:55:06 +0100992 int i;
993 for (i = 0; block_num < block_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530994 const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];
995
Nico Huber13f97a52023-01-14 23:55:06 +0100996 size_t num;
997 for (num = 0; num < block->count; num++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530998 chipoff_t end_addr = start_addr + block->size - 1;
999 init_eraseblock(layout, layout_idx, block_num,
1000 start_addr, end_addr, &sub_block_index);
1001 block_num += 1;
1002 start_addr = end_addr + 1;
1003 }
1004 }
1005 layout_idx++;
1006 }
1007
1008 *e_layout = layout;
1009 return layout_idx;
1010}
1011
Nico Huber20073e72024-05-10 01:31:40 +02001012static void deselect_erase_block_rec(const struct erase_layout *layout, size_t findex, size_t block_num)
1013{
1014 struct eraseblock_data *const ed = &layout[findex].layout_list[block_num];
1015 size_t i;
1016
1017 if (ed->selected) {
1018 ed->selected = false;
1019 } else if (findex > 0) {
1020 for (i = ed->first_sub_block_index; i <= ed->last_sub_block_index; ++i)
1021 deselect_erase_block_rec(layout, findex - 1, i);
1022 }
1023}
1024
Aarya Chaumald33bea42022-07-14 12:51:14 +05301025/*
1026 * @brief Function to select the list of sectors that need erasing
1027 *
1028 * @param flashctx flash context
1029 * @param layout erase layout
1030 * @param findex index of the erase function
1031 * @param block_num index of the block to erase according to the erase function index
Nico Huber00d1b9f2023-01-29 15:07:33 +01001032 * @param info current info from walking the regions
Nico Huberaa714dd2023-04-22 14:59:33 +02001033 * @return number of bytes selected for erase
Aarya Chaumald33bea42022-07-14 12:51:14 +05301034 */
Nico Huberaa714dd2023-04-22 14:59:33 +02001035static size_t select_erase_functions_rec(const struct flashctx *flashctx, const struct erase_layout *layout,
1036 size_t findex, size_t block_num, const struct walk_info *info)
Aarya Chaumald33bea42022-07-14 12:51:14 +05301037{
1038 struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
Nico Huberaa714dd2023-04-22 14:59:33 +02001039 const size_t eraseblock_size = ll->end_addr - ll->start_addr + 1;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301040 if (!findex) {
Nico Hubercf6ff0a2023-01-29 15:45:06 +01001041 if (ll->start_addr <= info->region_end && ll->end_addr >= info->region_start) {
Nico Huber1494f8e2023-01-29 15:48:00 +01001042 if (explicit_erase(info)) {
1043 ll->selected = true;
Nico Huberaa714dd2023-04-22 14:59:33 +02001044 return eraseblock_size;
Nico Huber1494f8e2023-01-29 15:48:00 +01001045 }
Nico Hubera6212482023-01-29 15:39:26 +01001046 const chipoff_t write_start = MAX(info->region_start, ll->start_addr);
1047 const chipoff_t write_end = MIN(info->region_end, ll->end_addr);
1048 const chipsize_t write_len = write_end - write_start + 1;
1049 const uint8_t erased_value = ERASED_VALUE(flashctx);
Nico Huber00d1b9f2023-01-29 15:07:33 +01001050 ll->selected = need_erase(
Nico Hubera6212482023-01-29 15:39:26 +01001051 info->curcontents + write_start, info->newcontents + write_start,
1052 write_len, flashctx->chip->gran, erased_value);
Nico Huberaa714dd2023-04-22 14:59:33 +02001053 if (ll->selected)
1054 return eraseblock_size;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301055 }
Nico Huberaa714dd2023-04-22 14:59:33 +02001056 return 0;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301057 } else {
Aarya Chaumald33bea42022-07-14 12:51:14 +05301058 const int sub_block_start = ll->first_sub_block_index;
1059 const int sub_block_end = ll->last_sub_block_index;
Nico Huberaa714dd2023-04-22 14:59:33 +02001060 size_t bytes = 0;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301061
Nico Huber13f97a52023-01-14 23:55:06 +01001062 int j;
Nico Huberaa714dd2023-04-22 14:59:33 +02001063 for (j = sub_block_start; j <= sub_block_end; j++)
1064 bytes += select_erase_functions_rec(flashctx, layout, findex - 1, j, info);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301065
Nico Huberaa714dd2023-04-22 14:59:33 +02001066 if (bytes > eraseblock_size / 2) {
Nico Huber00d1b9f2023-01-29 15:07:33 +01001067 if (ll->start_addr >= info->region_start && ll->end_addr <= info->region_end) {
Nico Huber20073e72024-05-10 01:31:40 +02001068 deselect_erase_block_rec(layout, findex, block_num);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301069 ll->selected = true;
Nico Huberaa714dd2023-04-22 14:59:33 +02001070 bytes = eraseblock_size;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301071 }
1072 }
Nico Huberaa714dd2023-04-22 14:59:33 +02001073 return bytes;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301074 }
1075}
1076
Nico Huberaa714dd2023-04-22 14:59:33 +02001077static size_t select_erase_functions(const struct flashctx *flashctx, const struct erase_layout *layout,
1078 size_t erasefn_count, const struct walk_info *info)
Nico Huberb11b72c2023-01-29 15:33:11 +01001079{
Nico Huberaa714dd2023-04-22 14:59:33 +02001080 size_t bytes = 0;
Nico Huberb11b72c2023-01-29 15:33:11 +01001081 size_t block_num;
1082 for (block_num = 0; block_num < layout[erasefn_count - 1].block_count; ++block_num)
Nico Huberaa714dd2023-04-22 14:59:33 +02001083 bytes += select_erase_functions_rec(flashctx, layout, erasefn_count - 1, block_num, info);
1084 return bytes;
Nico Huberb11b72c2023-01-29 15:33:11 +01001085}
1086
Nico Huber3b9c86d2023-01-15 22:58:06 +01001087static int write_range(struct flashctx *const flashctx, const chipoff_t flash_offset,
1088 const uint8_t *const curcontents, const uint8_t *const newcontents,
1089 const chipsize_t len, bool *const skipped)
1090{
1091 unsigned int writecount = 0;
1092 chipoff_t starthere = 0;
1093 chipsize_t lenhere = 0;
1094
1095 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1096 len - starthere, &starthere, flashctx->chip->gran))) {
1097 if (!writecount++)
1098 msg_cdbg("W");
1099 if (flashctx->chip->write(flashctx, newcontents + starthere,
1100 flash_offset + starthere, lenhere))
1101 return 1;
1102 starthere += lenhere;
Richard Hughes842d6782021-01-15 09:48:12 +00001103 if (skipped) {
1104 flashprog_progress_set(flashctx, starthere);
Nico Huber3b9c86d2023-01-15 22:58:06 +01001105 *skipped = false;
Richard Hughes842d6782021-01-15 09:48:12 +00001106 }
Nico Huber3b9c86d2023-01-15 22:58:06 +01001107 }
1108 return 0;
1109}
1110
1111typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
Nico Huber7af0e792016-04-29 16:40:15 +02001112/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1113typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1114
1115static int walk_eraseblocks(struct flashctx *const flashctx,
Nico Huberc09fca42023-01-29 15:58:09 +01001116 struct erase_layout *const layouts,
1117 const size_t layout_count,
Nico Huber7af0e792016-04-29 16:40:15 +02001118 struct walk_info *const info,
Nico Huberc09fca42023-01-29 15:58:09 +01001119 const per_blockfn_t per_blockfn)
Nico Huber7af0e792016-04-29 16:40:15 +02001120{
1121 int ret;
1122 size_t i, j;
1123 bool first = true;
Nico Huber7af0e792016-04-29 16:40:15 +02001124
Nico Huberc09fca42023-01-29 15:58:09 +01001125 for (i = 0; i < layout_count; ++i) {
1126 const struct erase_layout *const layout = &layouts[i];
Nico Huber7af0e792016-04-29 16:40:15 +02001127
Nico Huberc09fca42023-01-29 15:58:09 +01001128 for (j = 0; j < layout->block_count; ++j) {
1129 struct eraseblock_data *const eb = &layout->layout_list[j];
1130
1131 if (eb->start_addr > info->region_end)
Nico Huber7af0e792016-04-29 16:40:15 +02001132 break;
Nico Huberc09fca42023-01-29 15:58:09 +01001133 if (eb->end_addr < info->region_start)
1134 continue;
1135 if (!eb->selected)
1136 continue;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001137
Nico Huber7af0e792016-04-29 16:40:15 +02001138 /* Print this for every block except the first one. */
1139 if (first)
1140 first = false;
1141 else
1142 msg_cdbg(", ");
Nico Huberc09fca42023-01-29 15:58:09 +01001143 msg_cdbg("0x%06x-0x%06x:", eb->start_addr, eb->end_addr);
Nico Huber7af0e792016-04-29 16:40:15 +02001144
Nico Huberc09fca42023-01-29 15:58:09 +01001145 info->erase_start = eb->start_addr;
1146 info->erase_end = eb->end_addr;
1147 ret = per_blockfn(flashctx, info, layout->eraser->block_erase);
Nico Huber7af0e792016-04-29 16:40:15 +02001148 if (ret)
1149 return ret;
Nico Huberc09fca42023-01-29 15:58:09 +01001150
1151 /* Clean the erase layout up for future use on other
1152 regions. `.selected` is the only field we alter. */
1153 eb->selected = false;
Nico Huber7af0e792016-04-29 16:40:15 +02001154 }
Nico Huber7af0e792016-04-29 16:40:15 +02001155 }
1156 msg_cdbg("\n");
1157 return 0;
1158}
1159
1160static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1161 const per_blockfn_t per_blockfn)
1162{
Nico Huberc09fca42023-01-29 15:58:09 +01001163 const bool do_erase = explicit_erase(info) || !(flashctx->chip->feature_bits & FEATURE_NO_ERASE);
Nico Huberc3b02dc2023-08-12 01:13:45 +02001164 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huberc09fca42023-01-29 15:58:09 +01001165 struct erase_layout *erase_layouts = NULL;
Nico Huber5ca55232019-06-15 22:29:08 +02001166 const struct romentry *entry = NULL;
Nico Huberc09fca42023-01-29 15:58:09 +01001167 int ret = 0, layout_count = 0;
Nico Huber7af0e792016-04-29 16:40:15 +02001168
1169 all_skipped = true;
Nico Huberc6a924a2024-09-04 15:09:52 +02001170 msg_cinfo("Erasing %sflash chip... ", info->newcontents ? "and writing " : "");
Nico Huber7af0e792016-04-29 16:40:15 +02001171
Nico Huberc09fca42023-01-29 15:58:09 +01001172 if (do_erase) {
1173 layout_count = create_erase_layout(flashctx, &erase_layouts);
1174 if (layout_count <= 0)
1175 return 1;
1176 }
1177
Nico Huber5ca55232019-06-15 22:29:08 +02001178 while ((entry = layout_next_included(layout, entry))) {
1179 info->region_start = entry->start;
1180 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001181
Nico Huberc09fca42023-01-29 15:58:09 +01001182 if (do_erase) {
Richard Hughes842d6782021-01-15 09:48:12 +00001183 const size_t total = select_erase_functions(flashctx, erase_layouts, layout_count, info);
1184
1185 /* We verify every erased block manually. Technically that's
1186 reading, but accounting for it as part of the erase helps
1187 to provide a smooth, overall progress. Hence `total * 2`. */
1188 flashprog_progress_start(flashctx, FLASHPROG_PROGRESS_ERASE, total * 2);
1189
Nico Huberc09fca42023-01-29 15:58:09 +01001190 ret = walk_eraseblocks(flashctx, erase_layouts, layout_count, info, per_blockfn);
1191 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001192 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001193 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001194 }
Richard Hughes842d6782021-01-15 09:48:12 +00001195
1196 flashprog_progress_finish(flashctx);
Nico Huber7af0e792016-04-29 16:40:15 +02001197 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001198
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001199 if (info->newcontents) {
1200 bool skipped = true;
1201 msg_cdbg("0x%06x-0x%06x:", info->region_start, info->region_end);
Richard Hughes842d6782021-01-15 09:48:12 +00001202 flashprog_progress_start(flashctx, FLASHPROG_PROGRESS_WRITE,
1203 info->region_end - info->region_start + 1);
Nico Huberc09fca42023-01-29 15:58:09 +01001204 ret = write_range(flashctx, info->region_start,
1205 info->curcontents + info->region_start,
1206 info->newcontents + info->region_start,
1207 info->region_end + 1 - info->region_start, &skipped);
1208 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001209 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001210 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001211 }
Richard Hughes842d6782021-01-15 09:48:12 +00001212 flashprog_progress_finish(flashctx);
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001213 if (skipped) {
1214 msg_cdbg("S\n");
1215 } else {
1216 msg_cdbg("\n");
1217 all_skipped = false;
1218 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001219 }
Nico Huber7af0e792016-04-29 16:40:15 +02001220 }
1221 if (all_skipped)
1222 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
Nico Huberc6a924a2024-09-04 15:09:52 +02001223 msg_cinfo("Erase%s done.\n", info->newcontents ? "/write" : "");
Nico Huberc09fca42023-01-29 15:58:09 +01001224
1225free_ret:
1226 free_erase_layout(erase_layouts, layout_count);
1227 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001228}
1229
1230static int erase_block(struct flashctx *const flashctx,
1231 const struct walk_info *const info, const erasefn_t erasefn)
1232{
1233 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001234 const bool region_unaligned = info->region_start > info->erase_start ||
1235 info->erase_end > info->region_end;
1236 uint8_t *backup_contents = NULL, *erased_contents = NULL;
Nico Huberd34af7a2023-01-15 23:58:18 +01001237 int ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001238
Nico Huber6e61e0c2019-01-23 17:07:49 +01001239 /*
1240 * If the region is not erase-block aligned, merge current flash con-
1241 * tents into a new buffer `backup_contents`.
1242 */
1243 if (region_unaligned) {
1244 backup_contents = malloc(erase_len);
1245 erased_contents = malloc(erase_len);
1246 if (!backup_contents || !erased_contents) {
1247 msg_cerr("Out of memory!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001248 goto _free_ret;
1249 }
1250 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1251 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1252
1253 msg_cdbg("R");
1254 /* Merge data preceding the current region. */
1255 if (info->region_start > info->erase_start) {
1256 const chipoff_t start = info->erase_start;
1257 const chipsize_t len = info->region_start - info->erase_start;
1258 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1259 msg_cerr("Can't read! Aborting.\n");
1260 goto _free_ret;
1261 }
1262 }
1263 /* Merge data following the current region. */
1264 if (info->erase_end > info->region_end) {
1265 const chipoff_t start = info->region_end + 1;
1266 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1267 const chipsize_t len = info->erase_end - info->region_end;
1268 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1269 msg_cerr("Can't read! Aborting.\n");
1270 goto _free_ret;
1271 }
1272 }
1273 }
1274
Nico Huber7af0e792016-04-29 16:40:15 +02001275 all_skipped = false;
1276
1277 msg_cdbg("E");
1278 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001279 goto _free_ret;
Richard Hughes842d6782021-01-15 09:48:12 +00001280 flashprog_progress_add(flashctx, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001281 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1282 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001283 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001284 }
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001285 if (info->curcontents)
1286 memset(info->curcontents + info->erase_start, ERASED_VALUE(flashctx), erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001287
1288 if (region_unaligned) {
Nico Huber3b9c86d2023-01-15 22:58:06 +01001289 if (write_range(flashctx, info->erase_start, erased_contents, backup_contents, erase_len, NULL))
1290 goto _free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001291 if (info->curcontents)
1292 memcpy(info->curcontents + info->erase_start, backup_contents, erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001293 }
1294
1295 ret = 0;
1296
1297_free_ret:
1298 free(erased_contents);
1299 free(backup_contents);
1300 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001301}
1302
1303/**
1304 * @brief Erases the included layout regions.
1305 *
1306 * If there is no layout set in the given flash context, the whole chip will
1307 * be erased.
1308 *
1309 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001310 * @return 0 on success,
1311 * 1 if all available erase functions failed.
1312 */
Nico Huber454f6132012-12-10 13:34:10 +00001313static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001314{
1315 struct walk_info info = { 0 };
1316 return walk_by_layout(flashctx, &info, &erase_block);
1317}
1318
Nico Huber7af0e792016-04-29 16:40:15 +02001319/**
1320 * @brief Writes the included layout regions from a given image.
1321 *
1322 * If there is no layout set in the given flash context, the whole image
1323 * will be written.
1324 *
1325 * @param flashctx Flash context to be used.
1326 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1327 * @param newcontents The new image to be written.
1328 * @return 0 on success,
1329 * 1 if anything has gone wrong.
1330 */
Nico Huber454f6132012-12-10 13:34:10 +00001331static int write_by_layout(struct flashctx *const flashctx,
1332 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001333{
1334 struct walk_info info;
1335 info.curcontents = curcontents;
1336 info.newcontents = newcontents;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001337 return walk_by_layout(flashctx, &info, erase_block);
Nico Huber7af0e792016-04-29 16:40:15 +02001338}
1339
1340/**
1341 * @brief Compares the included layout regions with content from a buffer.
1342 *
1343 * If there is no layout set in the given flash context, the whole chip's
1344 * contents will be compared.
1345 *
1346 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001347 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001348 * @param curcontents A buffer of full chip size to read current chip contents into.
1349 * @param newcontents The new image to compare to.
1350 * @return 0 on success,
1351 * 1 if reading failed,
1352 * 3 if the contents don't match.
1353 */
Nico Huber74d09d42019-06-16 03:27:26 +02001354static int verify_by_layout(
1355 struct flashctx *const flashctx,
Nico Huberc3b02dc2023-08-12 01:13:45 +02001356 const struct flashprog_layout *const layout,
Nico Huber74d09d42019-06-16 03:27:26 +02001357 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001358{
Nico Huber5ca55232019-06-15 22:29:08 +02001359 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001360
Richard Hughes842d6782021-01-15 09:48:12 +00001361 flashprog_progress_start_by_layout(flashctx, FLASHPROG_PROGRESS_READ, layout);
1362
Nico Huber5ca55232019-06-15 22:29:08 +02001363 while ((entry = layout_next_included(layout, entry))) {
1364 const chipoff_t region_start = entry->start;
1365 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001366
1367 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1368 return 1;
1369 if (compare_range(newcontents + region_start, curcontents + region_start,
1370 region_start, region_len))
1371 return 3;
1372 }
Richard Hughes842d6782021-01-15 09:48:12 +00001373
1374 flashprog_progress_finish(flashctx);
1375
Nico Huber7af0e792016-04-29 16:40:15 +02001376 return 0;
1377}
1378
Stefan Tauner136388f2013-07-15 10:47:53 +00001379static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001380{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001381 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001382#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001383 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001384 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001385 "chip. Please report this to the mailing list at flashprog@flashprog.org or\n"
1386 "on IRC (see https://www.flashprog.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001387 "-------------------------------------------------------------------------------\n"
1388 "You may now reboot or simply leave the machine running.\n");
1389 else
1390#endif
1391 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001392 "the programmer and the flash chip. If you think the error is caused by flashprog\n"
1393 "please report this to the mailing list at flashprog@flashprog.org or on IRC\n"
1394 "(see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001395}
1396
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001397void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001398{
Stefan Tauner136388f2013-07-15 10:47:53 +00001399 msg_gerr("Your flash chip is in an unknown state.\n");
1400#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001401 if (programmer == &programmer_internal)
Nico Huberc3b02dc2023-08-12 01:13:45 +02001402 msg_gerr("Get help on IRC (see https://www.flashprog.org/Contact) or mail\n"
1403 "flashprog@flashprog.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001404 "-------------------------------------------------------------------------------\n"
1405 "DO NOT REBOOT OR POWEROFF!\n");
1406 else
1407#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +02001408 msg_gerr("Please report this to the mailing list at flashprog@flashprog.org\n"
1409 "or on IRC (see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001410}
1411
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001412void list_programmers_linebreak(int startcol, int cols, int paren)
1413{
1414 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001415 int pnamelen;
1416 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001417 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001418 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001419
Thomas Heijligend45cb592021-05-19 14:12:18 +02001420 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001421 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001422 pnamelen = strlen(pname);
1423 if (remaining - pnamelen - 2 < 0) {
1424 if (firstline)
1425 firstline = 0;
1426 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001427 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001428 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001429 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001430 remaining = cols - startcol;
1431 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001432 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001433 remaining--;
1434 }
1435 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001436 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001437 remaining--;
1438 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001439 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001440 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001441 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001442 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001443 remaining--;
1444 } else {
1445 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001446 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001447 }
1448 }
1449}
1450
Nico Huber32fa5082026-01-26 18:26:49 +01001451int selfcheck_chip(const struct flashchip *const chip, const int idx)
1452{
1453 int ret = 0;
Nico Huber72d0ada2026-01-26 18:28:20 +01001454 char label[80];
Nico Huber32fa5082026-01-26 18:26:49 +01001455 const char *const name = chip->name != NULL ? chip->name : "unnamed";
1456
Nico Huber72d0ada2026-01-26 18:28:20 +01001457 if (idx >= 0)
1458 snprintf(label, sizeof(label), "#%d (%s)", idx, name);
1459 else
1460 snprintf(label, sizeof(label), "%s", name);
1461
Nico Huber11136c22023-05-01 12:00:09 +02001462 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE ||
1463 chip->id.type == ID_FIXME) {
Nico Huber32fa5082026-01-26 18:26:49 +01001464 ret = 1;
Nico Huber72d0ada2026-01-26 18:28:20 +01001465 msg_gerr("ERROR: Some field of flash chip %s is misconfigured.\n"
1466 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001467 }
1468 if (chip->feature_bits &
1469 (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7 |
1470 FEATURE_ANY_DUAL | FEATURE_ANY_QUAD)
1471 && !chip->prepare_access) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001472 msg_gerr("ERROR: Flash chip %s misses chip\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001473 "preparation function for 4BA and multi-i/o modes.\n"
Nico Huber72d0ada2026-01-26 18:28:20 +01001474 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001475 ret = 1;
1476 }
1477 uint8_t zero_cycles[sizeof(chip->dummy_cycles)] = { 0 };
1478 if ((chip->feature_bits & (FEATURE_QPI_35_F5 | FEATURE_QPI_38_FF)) &&
1479 !memcmp(&chip->dummy_cycles, zero_cycles, sizeof(zero_cycles))) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001480 msg_gerr("ERROR: Flash chip %s misses QPI dummy-cycle\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001481 "settings. Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +01001482 label);
Nico Huber32fa5082026-01-26 18:26:49 +01001483 ret = 1;
1484 }
1485 if (chip->reg_bits.bp[0].reg != INVALID_REG &&
1486 (!chip->wp_write_cfg || !chip->wp_read_cfg ||
1487 !chip->wp_get_ranges || !chip->decode_range)) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001488 msg_gerr("ERROR: Flash chip %s advertises block-protection\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001489 "bits, but misses one or more write-protection functions.\n"
Nico Huber72d0ada2026-01-26 18:28:20 +01001490 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001491 ret = 1;
1492 }
Nico Huber72d0ada2026-01-26 18:28:20 +01001493 if (selfcheck_eraseblocks(chip, label)) {
Nico Huber32fa5082026-01-26 18:26:49 +01001494 ret = 1;
1495 }
1496
1497 return ret;
1498}
1499
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001500int selfcheck(void)
1501{
Stefan Tauner96658be2014-05-26 22:05:31 +00001502 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001503 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001504
Thomas Heijligend45cb592021-05-19 14:12:18 +02001505 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001506 const struct programmer_entry *const p = programmer_table[i];
1507 if (p == NULL) {
1508 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1509 ret = 1;
1510 continue;
1511 }
1512 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001513 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1514 ret = 1;
1515 /* This might hide other problems with this programmer, but allows for better error
1516 * messages below without jumping through hoops. */
1517 continue;
1518 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001519 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001520 case USB:
1521 case PCI:
1522 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001523 if (p->devs.note == NULL) {
1524 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001525 break; /* This one has its device list stored separately. */
1526 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001527 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001528 ret = 1;
1529 }
1530 break;
1531 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001532 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001533 ret = 1;
1534 break;
1535 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001536 if (p->init == NULL) {
1537 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001538 ret = 1;
1539 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001540 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001541
1542 /* It would be favorable if we could check for the correct layout (especially termination) of various
1543 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1544 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1545 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1546 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1547 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001548 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001549 msg_gerr("Flashchips table miscompilation!\n");
1550 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001551 } else {
1552 for (i = 0; i < flashchips_size - 1; i++) {
Nico Huber32fa5082026-01-26 18:26:49 +01001553 if (selfcheck_chip(&flashchips[i], i))
Stefan Tauner96658be2014-05-26 22:05:31 +00001554 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001555 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001556 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001557
Stefan Tauner600576b2014-06-12 22:57:36 +00001558#if CONFIG_INTERNAL == 1
1559 ret |= selfcheck_board_enables();
1560#endif
1561
Stefan Tauner96658be2014-05-26 22:05:31 +00001562 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001563 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001564}
1565
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001566/* FIXME: This function signature needs to be improved once prepare_flash_access()
1567 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001568 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001569static int chip_safety_check(const struct flashctx *flash, int force,
1570 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001571{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001572 const struct flashchip *chip = flash->chip;
1573
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001574 if (!programmer_may_write && (write_it || erase_it)) {
1575 msg_perr("Write/erase is not working yet on your programmer in "
1576 "its current configuration.\n");
1577 /* --force is the wrong approach, but it's the best we can do
1578 * until the generic programmer parameter parser is merged.
1579 */
1580 if (!force)
1581 return 1;
1582 msg_cerr("Continuing anyway.\n");
1583 }
1584
1585 if (read_it || erase_it || write_it || verify_it) {
1586 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001587 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001588 msg_cerr("Read is not working on this chip. ");
1589 if (!force)
1590 return 1;
1591 msg_cerr("Continuing anyway.\n");
1592 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001593 if (!chip->read) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001594 msg_cerr("flashprog has no read function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001595 "flash chip.\n");
1596 return 1;
1597 }
1598 }
1599 if (erase_it || write_it) {
1600 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001601 if (chip->tested.erase == NA) {
1602 msg_cerr("Erase is not possible on this chip.\n");
1603 return 1;
1604 }
1605 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001606 msg_cerr("Erase is not working on this chip. ");
1607 if (!force)
1608 return 1;
1609 msg_cerr("Continuing anyway.\n");
1610 }
Nico Huberbb8ab372026-03-14 11:46:02 +01001611 if(flashprog_count_usable_erasers(flash) == 0) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001612 msg_cerr("flashprog has no erase function for this "
Stefan Tauner5368dca2011-07-01 00:19:12 +00001613 "flash chip.\n");
1614 return 1;
1615 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001616 }
1617 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001618 if (chip->tested.write == NA) {
1619 msg_cerr("Write is not possible on this chip.\n");
1620 return 1;
1621 }
1622 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001623 msg_cerr("Write is not working on this chip. ");
1624 if (!force)
1625 return 1;
1626 msg_cerr("Continuing anyway.\n");
1627 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001628 if (!chip->write) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001629 msg_cerr("flashprog has no write function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001630 "flash chip.\n");
1631 return 1;
1632 }
1633 }
1634 return 0;
1635}
1636
Nico Huber305f4172013-06-14 11:55:26 +02001637int prepare_flash_access(struct flashctx *const flash,
1638 const bool read_it, const bool write_it,
1639 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001640{
1641 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1642 msg_cerr("Aborting.\n");
1643 return 1;
1644 }
1645
Nico Huber3ac761c2023-01-16 02:43:17 +01001646 if (layout_sanity_checks(flash, write_it)) {
Nico Huber454f6132012-12-10 13:34:10 +00001647 msg_cerr("Requested regions can not be handled. Aborting.\n");
1648 return 1;
1649 }
1650
Nico Huber5469c152026-02-12 22:56:52 +01001651 if (flash->mst.common->adapt_voltage) {
1652 if (flash->mst.common->adapt_voltage(flash->mst.common,
1653 flash->chip->voltage.min, flash->chip->voltage.max))
1654 return 1;
1655 }
1656
Nico Huber901fb952023-01-11 23:24:23 +01001657 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_FULL))
1658 return 1;
1659
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001660 /* Initialize chip_restore_fn_count before chip unlock calls. */
1661 flash->chip_restore_fn_count = 0;
1662
Nico Huber454f6132012-12-10 13:34:10 +00001663 /* Given the existence of read locks, we want to unlock for read,
1664 erase and write. */
1665 if (flash->chip->unlock)
1666 flash->chip->unlock(flash);
1667
1668 return 0;
1669}
1670
Nico Huber305f4172013-06-14 11:55:26 +02001671void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001672{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001673 deregister_chip_restore(flash);
Nico Huber901fb952023-01-11 23:24:23 +01001674 if (flash->chip->finish_access)
1675 flash->chip->finish_access(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001676}
1677
1678/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001679 * @addtogroup flashprog-flash
Nico Huber454f6132012-12-10 13:34:10 +00001680 * @{
1681 */
1682
1683/**
1684 * @brief Erase the specified ROM chip.
1685 *
1686 * If a layout is set in the given flash context, only included regions
1687 * will be erased.
1688 *
1689 * @param flashctx The context of the flash chip to erase.
1690 * @return 0 on success.
1691 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001692int flashprog_flash_erase(struct flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +00001693{
1694 if (prepare_flash_access(flashctx, false, false, true, false))
1695 return 1;
1696
1697 const int ret = erase_by_layout(flashctx);
1698
1699 finalize_flash_access(flashctx);
1700
1701 return ret;
1702}
1703
Nico Huberc3b02dc2023-08-12 01:13:45 +02001704/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +00001705
1706/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001707 * @defgroup flashprog-ops Operations
Nico Huber454f6132012-12-10 13:34:10 +00001708 * @{
1709 */
1710
1711/**
1712 * @brief Read the current image from the specified ROM chip.
1713 *
1714 * If a layout is set in the specified flash context, only included regions
1715 * will be read.
1716 *
1717 * @param flashctx The context of the flash chip.
1718 * @param buffer Target buffer to write image to.
1719 * @param buffer_len Size of target buffer in bytes.
1720 * @return 0 on success,
1721 * 2 if buffer_len is too short for the flash chip's contents,
1722 * or 1 on any other failure.
1723 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001724int flashprog_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001725{
1726 const size_t flash_size = flashctx->chip->total_size * 1024;
1727
1728 if (flash_size > buffer_len)
1729 return 2;
1730
1731 if (prepare_flash_access(flashctx, true, false, false, false))
1732 return 1;
1733
1734 msg_cinfo("Reading flash... ");
1735
1736 int ret = 1;
1737 if (read_by_layout(flashctx, buffer)) {
1738 msg_cerr("Read operation failed!\n");
1739 msg_cinfo("FAILED.\n");
1740 goto _finalize_ret;
1741 }
1742 msg_cinfo("done.\n");
1743 ret = 0;
1744
1745_finalize_ret:
1746 finalize_flash_access(flashctx);
1747 return ret;
1748}
1749
1750static void combine_image_by_layout(const struct flashctx *const flashctx,
1751 uint8_t *const newcontents, const uint8_t *const oldcontents)
1752{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001753 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001754 const struct romentry *included;
1755 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001756
Nico Huber3d7b1e32018-12-22 00:53:14 +01001757 while ((included = layout_next_included_region(layout, start))) {
1758 if (included->start > start) {
1759 /* copy everything up to the start of this included region */
1760 memcpy(newcontents + start, oldcontents + start, included->start - start);
1761 }
1762 /* skip this included region */
1763 start = included->end + 1;
1764 if (start == 0)
1765 return;
Nico Huber454f6132012-12-10 13:34:10 +00001766 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001767
1768 /* copy the rest of the chip */
1769 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1770 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001771}
1772
1773/**
1774 * @brief Write the specified image to the ROM chip.
1775 *
1776 * If a layout is set in the specified flash context, only erase blocks
1777 * containing included regions will be touched.
1778 *
1779 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001780 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001781 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001782 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001783 * @return 0 on success,
1784 * 4 if buffer_len doesn't match the size of the flash chip,
1785 * 3 if write was tried but nothing has changed,
1786 * 2 if write failed and flash contents changed,
1787 * or 1 on any other failure.
1788 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001789int flashprog_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001790 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001791{
1792 const size_t flash_size = flashctx->chip->total_size * 1024;
1793 const bool verify_all = flashctx->flags.verify_whole_chip;
1794 const bool verify = flashctx->flags.verify_after_write;
Nico Huberc3b02dc2023-08-12 01:13:45 +02001795 const struct flashprog_layout *const verify_layout =
Nico Huber74d09d42019-06-16 03:27:26 +02001796 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001797
1798 if (buffer_len != flash_size)
1799 return 4;
1800
1801 int ret = 1;
1802
1803 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001804 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001805 uint8_t *const curcontents = malloc(flash_size);
1806 uint8_t *oldcontents = NULL;
1807 if (verify_all)
1808 oldcontents = malloc(flash_size);
1809 if (!curcontents || (verify_all && !oldcontents)) {
1810 msg_gerr("Out of memory!\n");
1811 goto _free_ret;
1812 }
1813
1814#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001815 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001816 if (flashctx->flags.force_boardmismatch) {
1817 msg_pinfo("Proceeding anyway because user forced us to.\n");
1818 } else {
1819 msg_perr("Aborting. You can override this with "
1820 "-p internal:boardmismatch=force.\n");
1821 goto _free_ret;
1822 }
1823 }
1824#endif
1825
1826 if (prepare_flash_access(flashctx, false, true, false, verify))
1827 goto _free_ret;
1828
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001829 /* If given, assume flash chip contains same data as `refcontents`. */
1830 if (refcontents) {
1831 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1832 memcpy(curcontents, refcontents, flash_size);
1833 if (oldcontents)
1834 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001835 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001836 /*
1837 * Read the whole chip to be able to check whether regions need to be
1838 * erased and to give better diagnostics in case write fails.
1839 * The alternative is to read only the regions which are to be
1840 * preserved, but in that case we might perform unneeded erase which
1841 * takes time as well.
1842 */
1843 msg_cinfo("Reading old flash chip contents... ");
1844 if (verify_all) {
Richard Hughes842d6782021-01-15 09:48:12 +00001845 if (flashprog_read_range(flashctx, oldcontents, 0, flash_size)) {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001846 msg_cinfo("FAILED.\n");
1847 goto _finalize_ret;
1848 }
1849 memcpy(curcontents, oldcontents, flash_size);
1850 } else {
1851 if (read_by_layout(flashctx, curcontents)) {
1852 msg_cinfo("FAILED.\n");
1853 goto _finalize_ret;
1854 }
Nico Huber454f6132012-12-10 13:34:10 +00001855 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001856 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001857 }
Nico Huber454f6132012-12-10 13:34:10 +00001858
1859 if (write_by_layout(flashctx, curcontents, newcontents)) {
1860 msg_cerr("Uh oh. Erase/write failed. ");
1861 ret = 2;
1862 if (verify_all) {
1863 msg_cerr("Checking if anything has changed.\n");
1864 msg_cinfo("Reading current flash chip contents... ");
Richard Hughes842d6782021-01-15 09:48:12 +00001865 if (!flashprog_read_range(flashctx, curcontents, 0, flash_size)) {
Nico Huber454f6132012-12-10 13:34:10 +00001866 msg_cinfo("done.\n");
1867 if (!memcmp(oldcontents, curcontents, flash_size)) {
1868 nonfatal_help_message();
1869 goto _finalize_ret;
1870 }
1871 msg_cerr("Apparently at least some data has changed.\n");
1872 } else
1873 msg_cerr("Can't even read anymore!\n");
1874 emergency_help_message();
1875 goto _finalize_ret;
1876 } else {
1877 msg_cerr("\n");
1878 }
1879 emergency_help_message();
1880 goto _finalize_ret;
1881 }
1882
1883 /* Verify only if we actually changed something. */
1884 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001885 msg_cinfo("Verifying flash... ");
1886
Nico Huber74d09d42019-06-16 03:27:26 +02001887 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001888 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001889 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001890 /* If we tried to write, and verification now fails, we
1891 might have an emergency situation. */
1892 if (ret)
1893 emergency_help_message();
1894 else
1895 msg_cinfo("VERIFIED.\n");
1896 } else {
1897 /* We didn't change anything. */
1898 ret = 0;
1899 }
1900
1901_finalize_ret:
1902 finalize_flash_access(flashctx);
1903_free_ret:
1904 free(oldcontents);
1905 free(curcontents);
1906 return ret;
1907}
1908
1909/**
1910 * @brief Verify the ROM chip's contents with the specified image.
1911 *
1912 * If a layout is set in the specified flash context, only included regions
1913 * will be verified.
1914 *
1915 * @param flashctx The context of the flash chip.
1916 * @param buffer Source buffer to verify with.
1917 * @param buffer_len Size of source buffer in bytes.
1918 * @return 0 on success,
1919 * 3 if the chip's contents don't match,
1920 * 2 if buffer_len doesn't match the size of the flash chip,
1921 * or 1 on any other failure.
1922 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001923int flashprog_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001924{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001925 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001926 const size_t flash_size = flashctx->chip->total_size * 1024;
1927
1928 if (buffer_len != flash_size)
1929 return 2;
1930
1931 const uint8_t *const newcontents = buffer;
1932 uint8_t *const curcontents = malloc(flash_size);
1933 if (!curcontents) {
1934 msg_gerr("Out of memory!\n");
1935 return 1;
1936 }
1937
1938 int ret = 1;
1939
1940 if (prepare_flash_access(flashctx, false, false, false, true))
1941 goto _free_ret;
1942
1943 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001944 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001945 if (!ret)
1946 msg_cinfo("VERIFIED.\n");
1947
1948 finalize_flash_access(flashctx);
1949_free_ret:
1950 free(curcontents);
1951 return ret;
1952}
1953
Nico Huberc3b02dc2023-08-12 01:13:45 +02001954/** @} */ /* end flashprog-ops */