blob: 80e53b999fe1d730433bc0b2c670b40334c492b3 [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Nico Huber7af0e792016-04-29 16:40:15 +02008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000010 *
Uwe Hermannd1107642007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000015 *
Uwe Hermannd1107642007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000020 */
21
Felix Singerf25447e2022-08-19 02:44:28 +020022#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000023#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000024#include <sys/types.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000025#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000026#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000027#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000028#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000029#include <ctype.h>
Edward O'Callaghan3b64d812022-08-12 13:07:51 +100030
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000031#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000032#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000033#include "programmer.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010034#include "hwaccess_physmap.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010035#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000036
Nico Huberc3b02dc2023-08-12 01:13:45 +020037const char flashprog_version[] = FLASHPROG_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000038const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000039
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020040static const struct programmer_entry *programmer = NULL;
Nico Huber6a2ebeb2022-08-26 11:36:48 +020041static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000042
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000043/* If nonzero, used as the start address of bottom-aligned flash. */
44unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000045
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000046/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020047bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000048
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000049#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000050static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000051/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000052static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000053 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000054 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000055} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000056/* Initialize to 0 to make sure nobody registers a shutdown function before
57 * programmer init.
58 */
Felix Singerf25447e2022-08-19 02:44:28 +020059static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000060
Stefan Taunerc4f44df2013-08-12 22:58:43 +000061/* Did we change something or was every erase/write skipped (if any)? */
62static bool all_skipped = true;
63
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000064static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000065
Stefan Tauner2a1ed772014-08-31 00:09:21 +000066int shutdown_free(void *data)
67{
68 free(data);
69 return 0;
70}
71
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000072/* Register a function to be executed on programmer shutdown.
73 * The advantage over atexit() is that you can supply a void pointer which will
74 * be used as parameter to the registered function upon programmer shutdown.
75 * This pointer can point to arbitrary data used by said function, e.g. undo
76 * information for GPIO settings etc. If unneeded, set data=NULL.
77 * Please note that the first (void *data) belongs to the function signature of
78 * the function passed as first parameter.
79 */
David Hendricks8bb20212011-06-14 01:35:36 +000080int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000081{
82 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000083 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000084 SHUTDOWN_MAXFN);
85 return 1;
86 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000087 if (!may_register_shutdown) {
88 msg_perr("Tried to register a shutdown function before "
89 "programmer init.\n");
90 return 1;
91 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000092 shutdown_fn[shutdown_fn_count].func = function;
93 shutdown_fn[shutdown_fn_count].data = data;
94 shutdown_fn_count++;
95
96 return 0;
97}
98
Nikolai Artemiev4ad48642020-11-05 13:54:27 +110099int register_chip_restore(chip_restore_fn_cb_t func,
100 struct flashctx *flash, uint8_t status)
101{
102 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
103 msg_perr("Tried to register more than %i chip restore"
104 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
105 return 1;
106 }
107 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
108 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
109 flash->chip_restore_fn_count++;
110
111 return 0;
112}
113
114static int deregister_chip_restore(struct flashctx *flash)
115{
116 int rc = 0;
117
118 while (flash->chip_restore_fn_count > 0) {
119 int i = --flash->chip_restore_fn_count;
120 rc |= flash->chip_restore_fn[i].func(
121 flash, flash->chip_restore_fn[i].status);
122 }
123
124 return rc;
125}
126
Nico Huber2b66ad92023-01-11 20:15:15 +0100127int programmer_init(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000128{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000129 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000130
Nico Huber2b66ad92023-01-11 20:15:15 +0100131 if (prog == NULL || prog->driver == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000132 msg_perr("Invalid programmer specified!\n");
133 return -1;
134 }
Nico Huber2b66ad92023-01-11 20:15:15 +0100135 programmer = prog->driver;
136 programmer_param = prog->param;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000137 /* Initialize all programmer specific data. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000138 /* Default to top aligned flash at 4 GB. */
139 flashbase = 0;
140 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200141 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000142 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200143 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000144
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200145 msg_pdbg("Initializing %s programmer\n", programmer->name);
Nico Hubere3a26882023-01-11 21:45:51 +0100146 ret = programmer->init(prog);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000147 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000148 if (ret != 0) {
149 /* It is quite possible that any unhandled programmer parameter would have been valid,
150 * but an error in actual programmer init happened before the parameter was evaluated.
151 */
152 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
153 programmer_param);
154 } else {
155 /* Actual programmer init was successful, but the user specified an invalid or unusable
156 * (for the current programmer configuration) parameter.
157 */
158 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
159 msg_perr("Aborting.\n");
160 ret = ERROR_FATAL;
161 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000162 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200163 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000164 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000165}
166
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000167/** Calls registered shutdown functions and resets internal programmer-related variables.
168 * Calling it is safe even without previous initialization, but further interactions with programmer support
169 * require a call to programmer_init() (afterwards).
170 *
171 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Nico Huber2b66ad92023-01-11 20:15:15 +0100172int programmer_shutdown(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000173{
David Hendricks8bb20212011-06-14 01:35:36 +0000174 int ret = 0;
175
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000176 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200177 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000178 while (shutdown_fn_count > 0) {
179 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000180 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000181 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000182 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000183
David Hendricks8bb20212011-06-14 01:35:36 +0000184 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000185}
186
Stefan Taunerf80419c2014-05-02 15:41:42 +0000187void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000188{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000189 if (usecs > 0) {
190 if (programmer->delay)
191 programmer->delay(usecs);
192 else
193 internal_delay(usecs);
194 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000195}
196
Nico Hubercdcfda22023-04-29 13:29:33 +0200197static int read_memmapped_chunk(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000198{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000199 chip_readn(flash, buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000200 return 0;
201}
Nico Hubercdcfda22023-04-29 13:29:33 +0200202int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
203{
204 return flashprog_read_chunked(flash, buf, start, len, MAX_DATA_READ_UNLIMITED, read_memmapped_chunk);
205}
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000206
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000207/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000208 * It will look for needle with a subsequent '=' in haystack, return a copy of
209 * needle and remove everything from the first occurrence of needle to the next
210 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000211 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200212static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000213{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000214 char *param_pos, *opt_pos, *rest;
215 char *opt = NULL;
216 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000217 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000218
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000219 needlelen = strlen(needle);
220 if (!needlelen) {
221 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200222 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000223 return NULL;
224 }
225 /* No programmer parameters given. */
226 if (*haystack == NULL)
227 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000228 param_pos = strstr(*haystack, needle);
229 do {
230 if (!param_pos)
231 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000232 /* Needle followed by '='? */
233 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000234 /* Beginning of the string? */
235 if (param_pos == *haystack)
236 break;
237 /* After a delimiter? */
238 if (strchr(delim, *(param_pos - 1)))
239 break;
240 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000241 /* Continue searching. */
242 param_pos++;
243 param_pos = strstr(param_pos, needle);
244 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000245
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000246 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000247 /* Get the string after needle and '='. */
248 opt_pos = param_pos + needlelen + 1;
249 optlen = strcspn(opt_pos, delim);
250 /* Return an empty string if the parameter was empty. */
251 opt = malloc(optlen + 1);
252 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000253 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000254 exit(1);
255 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000256 strncpy(opt, opt_pos, optlen);
257 opt[optlen] = '\0';
258 rest = opt_pos + optlen;
259 /* Skip all delimiters after the current parameter. */
260 rest += strspn(rest, delim);
261 memmove(param_pos, rest, strlen(rest) + 1);
262 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000263 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000264
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000265 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000266}
267
Stefan Tauner66652442011-06-26 17:38:17 +0000268char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000269{
270 return extract_param(&programmer_param, param_name, ",");
271}
272
Richard Hughes842d6782021-01-15 09:48:12 +0000273static void flashprog_progress_report(struct flashprog_progress *const p)
274{
275 if (p->current > p->total) {
276 msg_gdbg2("Sanitizing progress report: %zu bytes off.", p->current - p->total);
277 p->current = p->total;
278 }
279
280 if (!p->callback)
281 return;
282
283 p->callback(p->stage, p->current, p->total, p->user_data);
284}
285
286static void flashprog_progress_start(struct flashprog_flashctx *const flashctx,
287 const enum flashprog_progress_stage stage, const size_t total)
288{
289 flashctx->progress.stage = stage;
290 flashctx->progress.current = 0;
291 flashctx->progress.total = total;
292 flashprog_progress_report(&flashctx->progress);
293}
294
295static void flashprog_progress_start_by_layout(struct flashprog_flashctx *const flashctx,
296 const enum flashprog_progress_stage stage,
297 const struct flashprog_layout *const layout)
298{
299 const struct romentry *entry = NULL;
300 size_t total = 0;
301
302 while ((entry = layout_next_included(layout, entry)))
303 total += entry->end - entry->start + 1;
304
305 flashprog_progress_start(flashctx, stage, total);
306}
307
308static void flashprog_progress_set(struct flashprog_flashctx *const flashctx, const size_t current)
309{
310 flashctx->progress.current = current;
311 flashprog_progress_report(&flashctx->progress);
312}
313
314/** @private */
315void flashprog_progress_add(struct flashprog_flashctx *const flashctx, const size_t progress)
316{
317 flashctx->progress.current += progress;
318 flashprog_progress_report(&flashctx->progress);
319}
320
321static void flashprog_progress_finish(struct flashprog_flashctx *const flashctx)
322{
323 if (flashctx->progress.current == flashctx->progress.total)
324 return;
325
326 flashctx->progress.current = flashctx->progress.total;
327 flashprog_progress_report(&flashctx->progress);
328}
329
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000330/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000331static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000332{
333 unsigned int usable_erasefunctions = 0;
334 int k;
335 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
336 if (!check_block_eraser(flash, k, 0))
337 usable_erasefunctions++;
338 }
339 return usable_erasefunctions;
340}
341
Mark Marshallf20b7be2014-05-09 21:16:21 +0000342static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000343{
344 int ret = 0, failcount = 0;
345 unsigned int i;
346 for (i = 0; i < len; i++) {
347 if (wantbuf[i] != havebuf[i]) {
348 /* Only print the first failure. */
349 if (!failcount++)
350 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
351 start + i, wantbuf[i], havebuf[i]);
352 }
353 }
354 if (failcount) {
355 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
356 start, start + len - 1, failcount);
357 ret = -1;
358 }
359 return ret;
360}
361
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000362/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600363static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000364{
365 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300366 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000367
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100368 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000369 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100370 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100371 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000372 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300373 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000374 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100375
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000376 free(cmpbuf);
377 return ret;
378}
379
Richard Hughes842d6782021-01-15 09:48:12 +0000380int flashprog_read_range(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
381{
382 flashprog_progress_start(flash, FLASHPROG_PROGRESS_READ, len);
383 const int ret = flash->chip->read(flash, buf, start, len);
384 flashprog_progress_finish(flash);
385 return ret;
386}
387
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000388/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000389 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000390 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000391 * @start offset to the base address of the flash chip
392 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000393 * @return 0 for success, -1 for failure
394 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000395int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000396{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000397 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000398 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000399
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100400 if (start + len > flash->chip->total_size * 1024) {
401 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
402 " total_size 0x%x\n", __func__, start, len,
403 flash->chip->total_size * 1024);
404 return -1;
405 }
406
Stefan Taunerdf64a422014-05-27 00:06:14 +0000407 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000408 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100409 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000410 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000411 }
412
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100413 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000414 if (ret) {
415 msg_gerr("Verification impossible because read failed "
416 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000417 ret = -1;
418 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000419 }
420
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000421 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000422out_free:
423 free(readbuf);
424 return ret;
425}
426
Nico Huber3ac761c2023-01-16 02:43:17 +0100427size_t gran_to_bytes(const enum write_granularity gran)
Nico Huberb77607f2023-01-16 02:25:45 +0100428{
429 switch (gran) {
430 case write_gran_1bit: return 1;
431 case write_gran_1byte: return 1;
432 case write_gran_1byte_implicit_erase: return 1;
433 case write_gran_128bytes: return 128;
434 case write_gran_256bytes: return 256;
435 case write_gran_264bytes: return 264;
436 case write_gran_512bytes: return 512;
437 case write_gran_528bytes: return 528;
438 case write_gran_1024bytes: return 1024;
439 case write_gran_1056bytes: return 1056;
440 default: return 0;
441 }
442}
443
Stefan Tauner02437452013-04-01 19:34:53 +0000444/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300445static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
446 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000447{
448 unsigned int i, j, limit;
449 for (j = 0; j < len / gran; j++) {
450 limit = min (gran, len - j * gran);
451 /* Are 'have' and 'want' identical? */
452 if (!memcmp(have + j * gran, want + j * gran, limit))
453 continue;
454 /* have needs to be in erased state. */
455 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300456 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000457 return 1;
458 }
459 return 0;
460}
461
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000462/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000463 * Check if the buffer @have can be programmed to the content of @want without
464 * erasing. This is only possible if all chunks of size @gran are either kept
465 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000466 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000467 * Warning: This function assumes that @have and @want point to naturally
468 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000469 *
470 * @have buffer with current content
471 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000472 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000473 * @gran write granularity (enum, not count)
474 * @return 0 if no erase is needed, 1 otherwise
475 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000476static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
477 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000478{
Stefan Tauner02437452013-04-01 19:34:53 +0000479 unsigned int i;
Nico Huberb77607f2023-01-16 02:25:45 +0100480 size_t stride;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000481
482 switch (gran) {
483 case write_gran_1bit:
Nico Huberb77607f2023-01-16 02:25:45 +0100484 for (i = 0; i < len; i++) {
485 if ((have[i] & want[i]) != want[i])
486 return 1;
487 }
488 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000489 case write_gran_1byte:
Nico Huberb77607f2023-01-16 02:25:45 +0100490 for (i = 0; i < len; i++) {
491 if ((have[i] != want[i]) && (have[i] != erased_value))
492 return 1;
493 }
494 return 0;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000495 case write_gran_1byte_implicit_erase:
496 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
Nico Huberb77607f2023-01-16 02:25:45 +0100497 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000498 default:
Nico Huberb77607f2023-01-16 02:25:45 +0100499 stride = gran_to_bytes(gran);
500 if (stride) {
501 return need_erase_gran_bytes(have, want, len, stride, erased_value);
502 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000503 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200504 "flashprog@flashprog.org\n", __func__);
Nico Huberb77607f2023-01-16 02:25:45 +0100505 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000506 }
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000507}
508
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000509/**
510 * Check if the buffer @have needs to be programmed to get the content of @want.
511 * If yes, return 1 and fill in first_start with the start address of the
512 * write operation and first_len with the length of the first to-be-written
513 * chunk. If not, return 0 and leave first_start and first_len undefined.
514 *
515 * Warning: This function assumes that @have and @want point to naturally
516 * aligned regions.
517 *
518 * @have buffer with current content
519 * @want buffer with desired content
520 * @len length of the checked area
521 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000522 * @first_start offset of the first byte which needs to be written (passed in
523 * value is increased by the offset of the first needed write
524 * relative to have/want or unchanged if no write is needed)
525 * @return length of the first contiguous area which needs to be written
526 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000527 *
528 * FIXME: This function needs a parameter which tells it about coalescing
529 * in relation to the max write length of the programmer and the max write
530 * length of the chip.
531 */
Nico Huber3b9c86d2023-01-15 22:58:06 +0100532static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, chipsize_t len,
533 chipoff_t *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000534{
Felix Singerf25447e2022-08-19 02:44:28 +0200535 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000536 unsigned int rel_start = 0, first_len = 0;
Nico Huberb77607f2023-01-16 02:25:45 +0100537 unsigned int i, limit;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000538
Nico Huberb77607f2023-01-16 02:25:45 +0100539 const size_t stride = gran_to_bytes(gran);
540 if (!stride) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000541 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200542 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000543 /* Claim that no write was needed. A write with unknown
544 * granularity is too dangerous to try.
545 */
546 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000547 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000548 for (i = 0; i < len / stride; i++) {
549 limit = min(stride, len - i * stride);
550 /* Are 'have' and 'want' identical? */
551 if (memcmp(have + i * stride, want + i * stride, limit)) {
552 if (!need_write) {
553 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200554 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000555 rel_start = i * stride;
556 }
557 } else {
558 if (need_write) {
559 /* First location where have and want
560 * do not differ anymore.
561 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000562 break;
563 }
564 }
565 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000566 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000567 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000568 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000569 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000570}
571
Nico Huber2d625722016-05-03 10:48:02 +0200572/*
573 * Return a string corresponding to the bustype parameter.
574 * Memory is obtained with malloc() and must be freed with free() by the caller.
575 */
576char *flashbuses_to_text(enum chipbustype bustype)
577{
578 char *ret = calloc(1, 1);
579 /*
580 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
581 * will cease to exist and should be eliminated here as well.
582 */
583 if (bustype == BUS_NONSPI) {
584 ret = strcat_realloc(ret, "Non-SPI, ");
585 } else {
586 if (bustype & BUS_PARALLEL)
587 ret = strcat_realloc(ret, "Parallel, ");
588 if (bustype & BUS_LPC)
589 ret = strcat_realloc(ret, "LPC, ");
590 if (bustype & BUS_FWH)
591 ret = strcat_realloc(ret, "FWH, ");
592 if (bustype & BUS_SPI)
593 ret = strcat_realloc(ret, "SPI, ");
594 if (bustype & BUS_PROG)
595 ret = strcat_realloc(ret, "Programmer-specific, ");
596 if (bustype == BUS_NONE)
597 ret = strcat_realloc(ret, "None, ");
598 }
599 /* Kill last comma. */
600 ret[strlen(ret) - 2] = '\0';
601 ret = realloc(ret, strlen(ret) + 1);
602 return ret;
603}
604
Nico Huberee8cf1c2025-01-02 23:17:25 +0100605int probe_noop(struct flashctx *flash)
606{
607 return 1;
608}
609
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100610static int init_default_layout(struct flashctx *flash)
611{
612 /* Fill default layout covering the whole chip. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200613 if (flashprog_layout_new(&flash->default_layout) ||
614 flashprog_layout_add_region(flash->default_layout,
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100615 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huberc3b02dc2023-08-12 01:13:45 +0200616 flashprog_layout_include_region(flash->default_layout, "complete flash"))
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100617 return -1;
618 return 0;
619}
620
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000621int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000622{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000623 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000624 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000625 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000626
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000627 for (chip = flashchips + startchip; chip && chip->name; chip++) {
628 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000629 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000630 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000631 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000632 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300633 /* Only probe for SPI25 chips by default. */
634 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
635 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000636 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
637 if (!chip->probe && !force) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200638 msg_gdbg("failed! flashprog has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000639 continue;
640 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000641
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000642 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200643 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000644 if (!flash->chip) {
645 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100646 return -1;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000647 }
Angel Pons7e134562021-06-07 13:29:13 +0200648 *flash->chip = *chip;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100649 flash->mst.par = &mst->par; /* both `mst` are unions, so we need only one pointer */
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000650
Nico Huber9eec4072023-01-12 01:17:30 +0100651 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_PROBE))
652 goto free_chip;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000653
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000654 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
655 * is only called with force=1 after normal probing failed.
656 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000657 if (force)
658 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000659
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000660 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000661 goto notfound;
662
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000663 /* If this is the first chip found, accept it.
664 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000665 * a non-generic match. SFDP and CFI are generic matches.
666 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000667 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000668 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000669 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000670 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000671 msg_cinfo("===\n"
672 "SFDP has autodetected a flash chip which is "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200673 "not natively supported by flashprog yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000674 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000675 msg_cinfo("The standard operations read and "
676 "verify should work, but to support "
677 "erase, write and all other "
678 "possible features");
679 else
680 msg_cinfo("All standard operations (read, "
681 "verify, erase and write) should "
682 "work, but to support all possible "
683 "features");
684
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000685 msg_cinfo(" we need to add them manually.\n"
686 "You can help us by mailing us the output of the following command to "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200687 "flashprog@flashprog.org:\n"
688 "'flashprog -VV [plus the -p/--programmer parameter]'\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000689 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000690 "===\n");
691 }
692
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000693 /* First flash chip detected on this bus. */
694 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000695 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000696 /* Not the first flash chip detected on this bus, but not a generic match either. */
697 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
698 break;
699 /* Not the first flash chip detected on this bus, and it's just a generic match. Ignore it. */
Peter Stuge483b8f02008-09-03 23:10:05 +0000700notfound:
Nico Huber9eec4072023-01-12 01:17:30 +0100701 if (flash->chip->finish_access)
702 flash->chip->finish_access(flash);
703free_chip:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000704 free(flash->chip);
705 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000706 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000707
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000708 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000709 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000710
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100711 if (init_default_layout(flash) < 0)
712 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000713
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000714 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000715 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
716 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000717 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000718#if CONFIG_INTERNAL == 1
Nico Huber0e76d992023-01-12 20:22:55 +0100719 if (flash->physical_memory != 0 && mst->par.map_flash == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000720 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
721 PRIxPTR_WIDTH, flash->physical_memory);
722 else
723#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200724 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000725
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000726 /* Flash registers may more likely not be mapped if the chip was forced.
727 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000728 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000729 if (flash->chip->printlock)
730 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000731
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000732 /* Get out of the way for later runs. */
Nico Huber9eec4072023-01-12 01:17:30 +0100733 if (flash->chip->finish_access)
734 flash->chip->finish_access(flash);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000735
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000736 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000737 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000738}
739
Stefan Tauner96658be2014-05-26 22:05:31 +0000740/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000741static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000742{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000743 int i, j, k;
744 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530745 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000746
747 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
748 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000749 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530750 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000751
752 for (i = 0; i < NUM_ERASEREGIONS; i++) {
753 /* Blocks with zero size are bugs in flashchips.c. */
754 if (eraser.eraseblocks[i].count &&
755 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000756 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200757 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000758 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000759 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000760 }
761 /* Blocks with zero count are bugs in flashchips.c. */
762 if (!eraser.eraseblocks[i].count &&
763 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000764 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200765 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000766 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000767 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000768 }
769 done += eraser.eraseblocks[i].count *
770 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530771 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000772 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000773 /* Empty eraseblock definition with erase function. */
774 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000775 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000776 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000777 if (!done)
778 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000779 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000780 msg_gerr("ERROR: Flash chip %s erase function %i "
781 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000782 " expected 0x%06x bytes.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200783 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000784 chip->name, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000785 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000786 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000787 if (!eraser.block_erase)
788 continue;
789 /* Check if there are identical erase functions for different
790 * layouts. That would imply "magic" erase functions. The
791 * easiest way to check this is with function pointers.
792 */
Uwe Hermann43959702010-03-13 17:28:29 +0000793 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000794 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000795 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000796 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200797 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000798 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000799 ret = 1;
800 }
Uwe Hermann43959702010-03-13 17:28:29 +0000801 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530802 if(curr_eraseblock_count > prev_eraseblock_count)
803 {
Nico Huberac90af62022-12-18 00:22:47 +0000804 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200805 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000806 chip->name, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530807 ret = 1;
808 }
809 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000810 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000811 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000812}
813
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000814static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000815{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000816 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000817
818 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
819 if (log)
820 msg_cdbg("not defined. ");
821 return 1;
822 }
823 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
824 if (log)
825 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000826 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000827 return 1;
828 }
829 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
830 if (log)
831 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000832 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000833 return 1;
834 }
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530835
Nico Hubere149fbe2024-08-21 10:23:53 +0200836 if (flash->chip->bustype == BUS_SPI && flash->chip->spi_cmd_set == SPI25) {
Nico Huber13389362024-03-05 18:35:30 +0100837 bool native_4ba;
Nico Huber13f97a52023-01-14 23:55:06 +0100838 int i;
Nico Huber13389362024-03-05 18:35:30 +0100839
840 const uint8_t *opcode = spi_get_opcode_from_erasefn(eraser.block_erase, &native_4ba);
Nico Huber07ebc682024-08-21 10:18:27 +0200841 if (!opcode)
842 return 1;
843
Nico Huber13f97a52023-01-14 23:55:06 +0100844 for (i = 0; opcode[i]; i++) {
Nico Huber13389362024-03-05 18:35:30 +0100845 if ((native_4ba && !spi_master_4ba(flash)) ||
Nico Huber9a11cbf2023-01-13 01:19:07 +0100846 !flash->mst.spi->probe_opcode(flash, opcode[i])) {
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530847 if (log)
848 msg_cdbg("block erase function and layout found "
849 "but SPI master doesn't support the function. ");
850 return 1;
851 }
852 }
853 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000854 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000855 return 0;
856}
857
Nico Huber7af0e792016-04-29 16:40:15 +0200858/**
859 * @brief Reads the included layout regions into a buffer.
860 *
861 * If there is no layout set in the given flash context, the whole chip will
862 * be read.
863 *
864 * @param flashctx Flash context to be used.
865 * @param buffer Buffer of full chip size to read into.
866 * @return 0 on success,
867 * 1 if any read fails.
868 */
869static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
870{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200871 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200872 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200873
Richard Hughes842d6782021-01-15 09:48:12 +0000874 flashprog_progress_start_by_layout(flashctx, FLASHPROG_PROGRESS_READ, layout);
875
Nico Huber5ca55232019-06-15 22:29:08 +0200876 while ((entry = layout_next_included(layout, entry))) {
877 const chipoff_t region_start = entry->start;
878 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200879
880 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
881 return 1;
882 }
Richard Hughes842d6782021-01-15 09:48:12 +0000883
884 flashprog_progress_finish(flashctx);
885
Nico Huber7af0e792016-04-29 16:40:15 +0200886 return 0;
887}
888
Nico Huber7af0e792016-04-29 16:40:15 +0200889/**
890 * @private
891 *
892 * For read-erase-write, `curcontents` and `newcontents` shall point
893 * to buffers of the chip's size. Both are supposed to be prefilled
894 * with at least the included layout regions of the current flash
895 * contents (`curcontents`) and the data to be written to the flash
896 * (`newcontents`).
897 *
898 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
899 *
900 * The `chipoff_t` values are used internally by `walk_by_layout()`.
901 */
902struct walk_info {
903 uint8_t *curcontents;
904 const uint8_t *newcontents;
905 chipoff_t region_start;
906 chipoff_t region_end;
907 chipoff_t erase_start;
908 chipoff_t erase_end;
909};
Nico Huber3b9c86d2023-01-15 22:58:06 +0100910
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530911struct eraseblock_data {
912 chipoff_t start_addr;
913 chipoff_t end_addr;
914 bool selected;
915 size_t block_num;
916 size_t first_sub_block_index;
917 size_t last_sub_block_index;
918};
919
920struct erase_layout {
921 struct eraseblock_data* layout_list;
922 size_t block_count;
923 const struct block_eraser *eraser;
924};
925
Nico Huber5ff6fdc2023-01-15 23:43:12 +0100926static bool explicit_erase(const struct walk_info *const info)
927{
928 /* For explicit erase, we are called without new contents. */
929 return !info->newcontents;
930}
931
Nico Huberd96e7032023-01-14 22:31:48 +0100932static size_t calculate_block_count(const struct block_eraser *const eraser)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530933{
Nico Huberd96e7032023-01-14 22:31:48 +0100934 size_t block_count = 0, i;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530935
Nico Huberd96e7032023-01-14 22:31:48 +0100936 for (i = 0; i < ARRAY_SIZE(eraser->eraseblocks); ++i)
937 block_count += eraser->eraseblocks[i].count;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530938
939 return block_count;
940}
941
942static void init_eraseblock(struct erase_layout *layout, size_t idx, size_t block_num,
943 chipoff_t start_addr, chipoff_t end_addr, size_t *sub_block_index)
944{
945 struct eraseblock_data *edata = &layout[idx].layout_list[block_num];
946 edata->start_addr = start_addr;
947 edata->end_addr = end_addr;
948 edata->selected = false;
949 edata->block_num = block_num;
950
951 if (!idx)
952 return;
Nico Hubera02df332023-01-14 23:06:27 +0100953 const struct erase_layout *const sub_layout = &layout[idx - 1];
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530954
955 edata->first_sub_block_index = *sub_block_index;
Nico Hubera02df332023-01-14 23:06:27 +0100956 for (; *sub_block_index < sub_layout->block_count; ++*sub_block_index) {
957 if (sub_layout->layout_list[*sub_block_index].end_addr > end_addr)
958 break;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530959 }
960 edata->last_sub_block_index = *sub_block_index - 1;
961}
962
963/*
964 * @brief Function to free the created erase_layout
965 *
966 * @param layout pointer to allocated layout
967 * @param erasefn_count number of erase functions for which the layout was created
968 *
969 */
970static void free_erase_layout(struct erase_layout *layout, unsigned int erasefn_count)
971{
Nico Huber13f97a52023-01-14 23:55:06 +0100972 size_t i;
973
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530974 if (!layout)
975 return;
Nico Huber13f97a52023-01-14 23:55:06 +0100976 for (i = 0; i < erasefn_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530977 free(layout[i].layout_list);
978 }
979 free(layout);
980}
981
982/*
983 * @brief Function to create an erase layout
984 *
985 * @param flashctx flash context
986 * @param e_layout address to the pointer to store the layout
987 * @return 0 on success,
988 * -1 if layout creation fails
989 *
990 * This function creates a layout of which erase functions erase which regions
991 * of the flash chip. This helps to optimally select the erase functions for
992 * erase/write operations.
993 */
Nico Huberc09fca42023-01-29 15:58:09 +0100994static int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530995{
996 const struct flashchip *chip = flashctx->chip;
997 const size_t erasefn_count = count_usable_erasers(flashctx);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530998
999 if (!erasefn_count) {
1000 msg_gerr("No erase functions supported\n");
1001 return 0;
1002 }
1003
Nico Huberd34864b2023-01-14 23:47:19 +01001004 struct erase_layout *layout = calloc(erasefn_count, sizeof(struct erase_layout));
1005 if (!layout) {
1006 msg_gerr("Out of memory!\n");
1007 return -1;
1008 }
1009
Nico Huber13f97a52023-01-14 23:55:06 +01001010 size_t layout_idx = 0, eraser_idx;
1011 for (eraser_idx = 0; eraser_idx < NUM_ERASEFUNCTIONS; eraser_idx++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301012 if (check_block_eraser(flashctx, eraser_idx, 0))
1013 continue;
1014
1015 layout[layout_idx].eraser = &chip->block_erasers[eraser_idx];
Nico Huberd96e7032023-01-14 22:31:48 +01001016 const size_t block_count = calculate_block_count(&chip->block_erasers[eraser_idx]);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301017 size_t sub_block_index = 0;
1018
1019 layout[layout_idx].block_count = block_count;
1020 layout[layout_idx].layout_list = (struct eraseblock_data *)calloc(block_count,
1021 sizeof(struct eraseblock_data));
1022
1023 if (!layout[layout_idx].layout_list) {
1024 free_erase_layout(layout, layout_idx);
1025 return -1;
1026 }
1027
1028 size_t block_num = 0;
1029 chipoff_t start_addr = 0;
1030
Nico Huber13f97a52023-01-14 23:55:06 +01001031 int i;
1032 for (i = 0; block_num < block_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301033 const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];
1034
Nico Huber13f97a52023-01-14 23:55:06 +01001035 size_t num;
1036 for (num = 0; num < block->count; num++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301037 chipoff_t end_addr = start_addr + block->size - 1;
1038 init_eraseblock(layout, layout_idx, block_num,
1039 start_addr, end_addr, &sub_block_index);
1040 block_num += 1;
1041 start_addr = end_addr + 1;
1042 }
1043 }
1044 layout_idx++;
1045 }
1046
1047 *e_layout = layout;
1048 return layout_idx;
1049}
1050
Nico Huber20073e72024-05-10 01:31:40 +02001051static void deselect_erase_block_rec(const struct erase_layout *layout, size_t findex, size_t block_num)
1052{
1053 struct eraseblock_data *const ed = &layout[findex].layout_list[block_num];
1054 size_t i;
1055
1056 if (ed->selected) {
1057 ed->selected = false;
1058 } else if (findex > 0) {
1059 for (i = ed->first_sub_block_index; i <= ed->last_sub_block_index; ++i)
1060 deselect_erase_block_rec(layout, findex - 1, i);
1061 }
1062}
1063
Aarya Chaumald33bea42022-07-14 12:51:14 +05301064/*
1065 * @brief Function to select the list of sectors that need erasing
1066 *
1067 * @param flashctx flash context
1068 * @param layout erase layout
1069 * @param findex index of the erase function
1070 * @param block_num index of the block to erase according to the erase function index
Nico Huber00d1b9f2023-01-29 15:07:33 +01001071 * @param info current info from walking the regions
Nico Huberaa714dd2023-04-22 14:59:33 +02001072 * @return number of bytes selected for erase
Aarya Chaumald33bea42022-07-14 12:51:14 +05301073 */
Nico Huberaa714dd2023-04-22 14:59:33 +02001074static size_t select_erase_functions_rec(const struct flashctx *flashctx, const struct erase_layout *layout,
1075 size_t findex, size_t block_num, const struct walk_info *info)
Aarya Chaumald33bea42022-07-14 12:51:14 +05301076{
1077 struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
Nico Huberaa714dd2023-04-22 14:59:33 +02001078 const size_t eraseblock_size = ll->end_addr - ll->start_addr + 1;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301079 if (!findex) {
Nico Hubercf6ff0a2023-01-29 15:45:06 +01001080 if (ll->start_addr <= info->region_end && ll->end_addr >= info->region_start) {
Nico Huber1494f8e2023-01-29 15:48:00 +01001081 if (explicit_erase(info)) {
1082 ll->selected = true;
Nico Huberaa714dd2023-04-22 14:59:33 +02001083 return eraseblock_size;
Nico Huber1494f8e2023-01-29 15:48:00 +01001084 }
Nico Hubera6212482023-01-29 15:39:26 +01001085 const chipoff_t write_start = MAX(info->region_start, ll->start_addr);
1086 const chipoff_t write_end = MIN(info->region_end, ll->end_addr);
1087 const chipsize_t write_len = write_end - write_start + 1;
1088 const uint8_t erased_value = ERASED_VALUE(flashctx);
Nico Huber00d1b9f2023-01-29 15:07:33 +01001089 ll->selected = need_erase(
Nico Hubera6212482023-01-29 15:39:26 +01001090 info->curcontents + write_start, info->newcontents + write_start,
1091 write_len, flashctx->chip->gran, erased_value);
Nico Huberaa714dd2023-04-22 14:59:33 +02001092 if (ll->selected)
1093 return eraseblock_size;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301094 }
Nico Huberaa714dd2023-04-22 14:59:33 +02001095 return 0;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301096 } else {
Aarya Chaumald33bea42022-07-14 12:51:14 +05301097 const int sub_block_start = ll->first_sub_block_index;
1098 const int sub_block_end = ll->last_sub_block_index;
Nico Huberaa714dd2023-04-22 14:59:33 +02001099 size_t bytes = 0;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301100
Nico Huber13f97a52023-01-14 23:55:06 +01001101 int j;
Nico Huberaa714dd2023-04-22 14:59:33 +02001102 for (j = sub_block_start; j <= sub_block_end; j++)
1103 bytes += select_erase_functions_rec(flashctx, layout, findex - 1, j, info);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301104
Nico Huberaa714dd2023-04-22 14:59:33 +02001105 if (bytes > eraseblock_size / 2) {
Nico Huber00d1b9f2023-01-29 15:07:33 +01001106 if (ll->start_addr >= info->region_start && ll->end_addr <= info->region_end) {
Nico Huber20073e72024-05-10 01:31:40 +02001107 deselect_erase_block_rec(layout, findex, block_num);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301108 ll->selected = true;
Nico Huberaa714dd2023-04-22 14:59:33 +02001109 bytes = eraseblock_size;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301110 }
1111 }
Nico Huberaa714dd2023-04-22 14:59:33 +02001112 return bytes;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301113 }
1114}
1115
Nico Huberaa714dd2023-04-22 14:59:33 +02001116static size_t select_erase_functions(const struct flashctx *flashctx, const struct erase_layout *layout,
1117 size_t erasefn_count, const struct walk_info *info)
Nico Huberb11b72c2023-01-29 15:33:11 +01001118{
Nico Huberaa714dd2023-04-22 14:59:33 +02001119 size_t bytes = 0;
Nico Huberb11b72c2023-01-29 15:33:11 +01001120 size_t block_num;
1121 for (block_num = 0; block_num < layout[erasefn_count - 1].block_count; ++block_num)
Nico Huberaa714dd2023-04-22 14:59:33 +02001122 bytes += select_erase_functions_rec(flashctx, layout, erasefn_count - 1, block_num, info);
1123 return bytes;
Nico Huberb11b72c2023-01-29 15:33:11 +01001124}
1125
Nico Huber3b9c86d2023-01-15 22:58:06 +01001126static int write_range(struct flashctx *const flashctx, const chipoff_t flash_offset,
1127 const uint8_t *const curcontents, const uint8_t *const newcontents,
1128 const chipsize_t len, bool *const skipped)
1129{
1130 unsigned int writecount = 0;
1131 chipoff_t starthere = 0;
1132 chipsize_t lenhere = 0;
1133
1134 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1135 len - starthere, &starthere, flashctx->chip->gran))) {
1136 if (!writecount++)
1137 msg_cdbg("W");
1138 if (flashctx->chip->write(flashctx, newcontents + starthere,
1139 flash_offset + starthere, lenhere))
1140 return 1;
1141 starthere += lenhere;
Richard Hughes842d6782021-01-15 09:48:12 +00001142 if (skipped) {
1143 flashprog_progress_set(flashctx, starthere);
Nico Huber3b9c86d2023-01-15 22:58:06 +01001144 *skipped = false;
Richard Hughes842d6782021-01-15 09:48:12 +00001145 }
Nico Huber3b9c86d2023-01-15 22:58:06 +01001146 }
1147 return 0;
1148}
1149
1150typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
Nico Huber7af0e792016-04-29 16:40:15 +02001151/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1152typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1153
1154static int walk_eraseblocks(struct flashctx *const flashctx,
Nico Huberc09fca42023-01-29 15:58:09 +01001155 struct erase_layout *const layouts,
1156 const size_t layout_count,
Nico Huber7af0e792016-04-29 16:40:15 +02001157 struct walk_info *const info,
Nico Huberc09fca42023-01-29 15:58:09 +01001158 const per_blockfn_t per_blockfn)
Nico Huber7af0e792016-04-29 16:40:15 +02001159{
1160 int ret;
1161 size_t i, j;
1162 bool first = true;
Nico Huber7af0e792016-04-29 16:40:15 +02001163
Nico Huberc09fca42023-01-29 15:58:09 +01001164 for (i = 0; i < layout_count; ++i) {
1165 const struct erase_layout *const layout = &layouts[i];
Nico Huber7af0e792016-04-29 16:40:15 +02001166
Nico Huberc09fca42023-01-29 15:58:09 +01001167 for (j = 0; j < layout->block_count; ++j) {
1168 struct eraseblock_data *const eb = &layout->layout_list[j];
1169
1170 if (eb->start_addr > info->region_end)
Nico Huber7af0e792016-04-29 16:40:15 +02001171 break;
Nico Huberc09fca42023-01-29 15:58:09 +01001172 if (eb->end_addr < info->region_start)
1173 continue;
1174 if (!eb->selected)
1175 continue;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001176
Nico Huber7af0e792016-04-29 16:40:15 +02001177 /* Print this for every block except the first one. */
1178 if (first)
1179 first = false;
1180 else
1181 msg_cdbg(", ");
Nico Huberc09fca42023-01-29 15:58:09 +01001182 msg_cdbg("0x%06x-0x%06x:", eb->start_addr, eb->end_addr);
Nico Huber7af0e792016-04-29 16:40:15 +02001183
Nico Huberc09fca42023-01-29 15:58:09 +01001184 info->erase_start = eb->start_addr;
1185 info->erase_end = eb->end_addr;
1186 ret = per_blockfn(flashctx, info, layout->eraser->block_erase);
Nico Huber7af0e792016-04-29 16:40:15 +02001187 if (ret)
1188 return ret;
Nico Huberc09fca42023-01-29 15:58:09 +01001189
1190 /* Clean the erase layout up for future use on other
1191 regions. `.selected` is the only field we alter. */
1192 eb->selected = false;
Nico Huber7af0e792016-04-29 16:40:15 +02001193 }
Nico Huber7af0e792016-04-29 16:40:15 +02001194 }
1195 msg_cdbg("\n");
1196 return 0;
1197}
1198
1199static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1200 const per_blockfn_t per_blockfn)
1201{
Nico Huberc09fca42023-01-29 15:58:09 +01001202 const bool do_erase = explicit_erase(info) || !(flashctx->chip->feature_bits & FEATURE_NO_ERASE);
Nico Huberc3b02dc2023-08-12 01:13:45 +02001203 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huberc09fca42023-01-29 15:58:09 +01001204 struct erase_layout *erase_layouts = NULL;
Nico Huber5ca55232019-06-15 22:29:08 +02001205 const struct romentry *entry = NULL;
Nico Huberc09fca42023-01-29 15:58:09 +01001206 int ret = 0, layout_count = 0;
Nico Huber7af0e792016-04-29 16:40:15 +02001207
1208 all_skipped = true;
Nico Huberc6a924a2024-09-04 15:09:52 +02001209 msg_cinfo("Erasing %sflash chip... ", info->newcontents ? "and writing " : "");
Nico Huber7af0e792016-04-29 16:40:15 +02001210
Nico Huberc09fca42023-01-29 15:58:09 +01001211 if (do_erase) {
1212 layout_count = create_erase_layout(flashctx, &erase_layouts);
1213 if (layout_count <= 0)
1214 return 1;
1215 }
1216
Nico Huber5ca55232019-06-15 22:29:08 +02001217 while ((entry = layout_next_included(layout, entry))) {
1218 info->region_start = entry->start;
1219 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001220
Nico Huberc09fca42023-01-29 15:58:09 +01001221 if (do_erase) {
Richard Hughes842d6782021-01-15 09:48:12 +00001222 const size_t total = select_erase_functions(flashctx, erase_layouts, layout_count, info);
1223
1224 /* We verify every erased block manually. Technically that's
1225 reading, but accounting for it as part of the erase helps
1226 to provide a smooth, overall progress. Hence `total * 2`. */
1227 flashprog_progress_start(flashctx, FLASHPROG_PROGRESS_ERASE, total * 2);
1228
Nico Huberc09fca42023-01-29 15:58:09 +01001229 ret = walk_eraseblocks(flashctx, erase_layouts, layout_count, info, per_blockfn);
1230 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001231 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001232 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001233 }
Richard Hughes842d6782021-01-15 09:48:12 +00001234
1235 flashprog_progress_finish(flashctx);
Nico Huber7af0e792016-04-29 16:40:15 +02001236 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001237
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001238 if (info->newcontents) {
1239 bool skipped = true;
1240 msg_cdbg("0x%06x-0x%06x:", info->region_start, info->region_end);
Richard Hughes842d6782021-01-15 09:48:12 +00001241 flashprog_progress_start(flashctx, FLASHPROG_PROGRESS_WRITE,
1242 info->region_end - info->region_start + 1);
Nico Huberc09fca42023-01-29 15:58:09 +01001243 ret = write_range(flashctx, info->region_start,
1244 info->curcontents + info->region_start,
1245 info->newcontents + info->region_start,
1246 info->region_end + 1 - info->region_start, &skipped);
1247 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001248 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001249 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001250 }
Richard Hughes842d6782021-01-15 09:48:12 +00001251 flashprog_progress_finish(flashctx);
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001252 if (skipped) {
1253 msg_cdbg("S\n");
1254 } else {
1255 msg_cdbg("\n");
1256 all_skipped = false;
1257 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001258 }
Nico Huber7af0e792016-04-29 16:40:15 +02001259 }
1260 if (all_skipped)
1261 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
Nico Huberc6a924a2024-09-04 15:09:52 +02001262 msg_cinfo("Erase%s done.\n", info->newcontents ? "/write" : "");
Nico Huberc09fca42023-01-29 15:58:09 +01001263
1264free_ret:
1265 free_erase_layout(erase_layouts, layout_count);
1266 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001267}
1268
1269static int erase_block(struct flashctx *const flashctx,
1270 const struct walk_info *const info, const erasefn_t erasefn)
1271{
1272 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001273 const bool region_unaligned = info->region_start > info->erase_start ||
1274 info->erase_end > info->region_end;
1275 uint8_t *backup_contents = NULL, *erased_contents = NULL;
Nico Huberd34af7a2023-01-15 23:58:18 +01001276 int ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001277
Nico Huber6e61e0c2019-01-23 17:07:49 +01001278 /*
1279 * If the region is not erase-block aligned, merge current flash con-
1280 * tents into a new buffer `backup_contents`.
1281 */
1282 if (region_unaligned) {
1283 backup_contents = malloc(erase_len);
1284 erased_contents = malloc(erase_len);
1285 if (!backup_contents || !erased_contents) {
1286 msg_cerr("Out of memory!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001287 goto _free_ret;
1288 }
1289 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1290 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1291
1292 msg_cdbg("R");
1293 /* Merge data preceding the current region. */
1294 if (info->region_start > info->erase_start) {
1295 const chipoff_t start = info->erase_start;
1296 const chipsize_t len = info->region_start - info->erase_start;
1297 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1298 msg_cerr("Can't read! Aborting.\n");
1299 goto _free_ret;
1300 }
1301 }
1302 /* Merge data following the current region. */
1303 if (info->erase_end > info->region_end) {
1304 const chipoff_t start = info->region_end + 1;
1305 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1306 const chipsize_t len = info->erase_end - info->region_end;
1307 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1308 msg_cerr("Can't read! Aborting.\n");
1309 goto _free_ret;
1310 }
1311 }
1312 }
1313
Nico Huber7af0e792016-04-29 16:40:15 +02001314 all_skipped = false;
1315
1316 msg_cdbg("E");
1317 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001318 goto _free_ret;
Richard Hughes842d6782021-01-15 09:48:12 +00001319 flashprog_progress_add(flashctx, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001320 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1321 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001322 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001323 }
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001324 if (info->curcontents)
1325 memset(info->curcontents + info->erase_start, ERASED_VALUE(flashctx), erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001326
1327 if (region_unaligned) {
Nico Huber3b9c86d2023-01-15 22:58:06 +01001328 if (write_range(flashctx, info->erase_start, erased_contents, backup_contents, erase_len, NULL))
1329 goto _free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001330 if (info->curcontents)
1331 memcpy(info->curcontents + info->erase_start, backup_contents, erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001332 }
1333
1334 ret = 0;
1335
1336_free_ret:
1337 free(erased_contents);
1338 free(backup_contents);
1339 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001340}
1341
1342/**
1343 * @brief Erases the included layout regions.
1344 *
1345 * If there is no layout set in the given flash context, the whole chip will
1346 * be erased.
1347 *
1348 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001349 * @return 0 on success,
1350 * 1 if all available erase functions failed.
1351 */
Nico Huber454f6132012-12-10 13:34:10 +00001352static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001353{
1354 struct walk_info info = { 0 };
1355 return walk_by_layout(flashctx, &info, &erase_block);
1356}
1357
Nico Huber7af0e792016-04-29 16:40:15 +02001358/**
1359 * @brief Writes the included layout regions from a given image.
1360 *
1361 * If there is no layout set in the given flash context, the whole image
1362 * will be written.
1363 *
1364 * @param flashctx Flash context to be used.
1365 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1366 * @param newcontents The new image to be written.
1367 * @return 0 on success,
1368 * 1 if anything has gone wrong.
1369 */
Nico Huber454f6132012-12-10 13:34:10 +00001370static int write_by_layout(struct flashctx *const flashctx,
1371 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001372{
1373 struct walk_info info;
1374 info.curcontents = curcontents;
1375 info.newcontents = newcontents;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001376 return walk_by_layout(flashctx, &info, erase_block);
Nico Huber7af0e792016-04-29 16:40:15 +02001377}
1378
1379/**
1380 * @brief Compares the included layout regions with content from a buffer.
1381 *
1382 * If there is no layout set in the given flash context, the whole chip's
1383 * contents will be compared.
1384 *
1385 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001386 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001387 * @param curcontents A buffer of full chip size to read current chip contents into.
1388 * @param newcontents The new image to compare to.
1389 * @return 0 on success,
1390 * 1 if reading failed,
1391 * 3 if the contents don't match.
1392 */
Nico Huber74d09d42019-06-16 03:27:26 +02001393static int verify_by_layout(
1394 struct flashctx *const flashctx,
Nico Huberc3b02dc2023-08-12 01:13:45 +02001395 const struct flashprog_layout *const layout,
Nico Huber74d09d42019-06-16 03:27:26 +02001396 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001397{
Nico Huber5ca55232019-06-15 22:29:08 +02001398 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001399
Richard Hughes842d6782021-01-15 09:48:12 +00001400 flashprog_progress_start_by_layout(flashctx, FLASHPROG_PROGRESS_READ, layout);
1401
Nico Huber5ca55232019-06-15 22:29:08 +02001402 while ((entry = layout_next_included(layout, entry))) {
1403 const chipoff_t region_start = entry->start;
1404 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001405
1406 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1407 return 1;
1408 if (compare_range(newcontents + region_start, curcontents + region_start,
1409 region_start, region_len))
1410 return 3;
1411 }
Richard Hughes842d6782021-01-15 09:48:12 +00001412
1413 flashprog_progress_finish(flashctx);
1414
Nico Huber7af0e792016-04-29 16:40:15 +02001415 return 0;
1416}
1417
Stefan Tauner136388f2013-07-15 10:47:53 +00001418static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001419{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001420 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001421#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001422 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001423 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001424 "chip. Please report this to the mailing list at flashprog@flashprog.org or\n"
1425 "on IRC (see https://www.flashprog.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001426 "-------------------------------------------------------------------------------\n"
1427 "You may now reboot or simply leave the machine running.\n");
1428 else
1429#endif
1430 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001431 "the programmer and the flash chip. If you think the error is caused by flashprog\n"
1432 "please report this to the mailing list at flashprog@flashprog.org or on IRC\n"
1433 "(see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001434}
1435
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001436void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001437{
Stefan Tauner136388f2013-07-15 10:47:53 +00001438 msg_gerr("Your flash chip is in an unknown state.\n");
1439#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001440 if (programmer == &programmer_internal)
Nico Huberc3b02dc2023-08-12 01:13:45 +02001441 msg_gerr("Get help on IRC (see https://www.flashprog.org/Contact) or mail\n"
1442 "flashprog@flashprog.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001443 "-------------------------------------------------------------------------------\n"
1444 "DO NOT REBOOT OR POWEROFF!\n");
1445 else
1446#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +02001447 msg_gerr("Please report this to the mailing list at flashprog@flashprog.org\n"
1448 "or on IRC (see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001449}
1450
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001451void list_programmers_linebreak(int startcol, int cols, int paren)
1452{
1453 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001454 int pnamelen;
1455 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001456 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001457 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001458
Thomas Heijligend45cb592021-05-19 14:12:18 +02001459 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001460 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001461 pnamelen = strlen(pname);
1462 if (remaining - pnamelen - 2 < 0) {
1463 if (firstline)
1464 firstline = 0;
1465 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001466 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001467 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001468 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001469 remaining = cols - startcol;
1470 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001471 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001472 remaining--;
1473 }
1474 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001475 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001476 remaining--;
1477 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001478 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001479 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001480 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001481 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001482 remaining--;
1483 } else {
1484 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001485 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001486 }
1487 }
1488}
1489
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001490int selfcheck(void)
1491{
Stefan Tauner96658be2014-05-26 22:05:31 +00001492 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001493 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001494
Thomas Heijligend45cb592021-05-19 14:12:18 +02001495 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001496 const struct programmer_entry *const p = programmer_table[i];
1497 if (p == NULL) {
1498 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1499 ret = 1;
1500 continue;
1501 }
1502 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001503 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1504 ret = 1;
1505 /* This might hide other problems with this programmer, but allows for better error
1506 * messages below without jumping through hoops. */
1507 continue;
1508 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001509 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001510 case USB:
1511 case PCI:
1512 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001513 if (p->devs.note == NULL) {
1514 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001515 break; /* This one has its device list stored separately. */
1516 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001517 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001518 ret = 1;
1519 }
1520 break;
1521 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001522 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001523 ret = 1;
1524 break;
1525 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001526 if (p->init == NULL) {
1527 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001528 ret = 1;
1529 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001530 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001531
1532 /* It would be favorable if we could check for the correct layout (especially termination) of various
1533 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1534 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1535 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1536 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1537 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001538 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001539 msg_gerr("Flashchips table miscompilation!\n");
1540 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001541 } else {
1542 for (i = 0; i < flashchips_size - 1; i++) {
1543 const struct flashchip *chip = &flashchips[i];
Nico Huberd40037a2024-10-20 23:20:46 +02001544 const char *const name = chip->name != NULL ? chip->name : "unnamed";
Stefan Tauner96658be2014-05-26 22:05:31 +00001545 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1546 ret = 1;
1547 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
Nico Huberd40037a2024-10-20 23:20:46 +02001548 "Please report a bug at flashprog@flashprog.org\n", i, name);
Stefan Tauner96658be2014-05-26 22:05:31 +00001549 }
Nico Huber930d4212024-05-04 18:59:15 +02001550 if (chip->feature_bits &
1551 (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7 |
1552 FEATURE_ANY_DUAL | FEATURE_ANY_QUAD)
1553 && !chip->prepare_access) {
1554 msg_gerr("ERROR: Flash chip #%d (%s) misses chip\n"
1555 "preparation function for 4BA and multi-i/o modes.\n"
Nico Huberd40037a2024-10-20 23:20:46 +02001556 "Please report a bug at flashprog@flashprog.org\n", i, name);
1557 ret = 1;
1558 }
Nico Huberf0503702024-11-08 16:15:53 +01001559 uint8_t zero_cycles[sizeof(chip->dummy_cycles)] = { 0 };
1560 if ((chip->feature_bits & (FEATURE_QPI_35_F5 | FEATURE_QPI_38_FF)) &&
1561 !memcmp(&chip->dummy_cycles, zero_cycles, sizeof(zero_cycles))) {
1562 msg_gerr("ERROR: Flash chip #%d (%s) misses QPI dummy-cycle\n"
1563 "settings. Please report a bug at flashprog@flashprog.org\n",
1564 i, name);
1565 ret = 1;
1566 }
Nico Huberd40037a2024-10-20 23:20:46 +02001567 if (chip->reg_bits.bp[0].reg != INVALID_REG &&
1568 (!chip->wp_write_cfg || !chip->wp_read_cfg ||
1569 !chip->wp_get_ranges || !chip->decode_range)) {
1570 msg_gerr("ERROR: Flash chip #%d (%s) advertises block-protection\n"
1571 "bits, but misses one or more write-protection functions.\n"
1572 "Please report a bug at flashprog@flashprog.org\n", i, name);
Nico Huber930d4212024-05-04 18:59:15 +02001573 ret = 1;
1574 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001575 if (selfcheck_eraseblocks(chip)) {
1576 ret = 1;
1577 }
1578 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001579 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001580
Stefan Tauner600576b2014-06-12 22:57:36 +00001581#if CONFIG_INTERNAL == 1
1582 ret |= selfcheck_board_enables();
1583#endif
1584
Stefan Tauner96658be2014-05-26 22:05:31 +00001585 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001586 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001587}
1588
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001589/* FIXME: This function signature needs to be improved once prepare_flash_access()
1590 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001591 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001592static int chip_safety_check(const struct flashctx *flash, int force,
1593 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001594{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001595 const struct flashchip *chip = flash->chip;
1596
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001597 if (!programmer_may_write && (write_it || erase_it)) {
1598 msg_perr("Write/erase is not working yet on your programmer in "
1599 "its current configuration.\n");
1600 /* --force is the wrong approach, but it's the best we can do
1601 * until the generic programmer parameter parser is merged.
1602 */
1603 if (!force)
1604 return 1;
1605 msg_cerr("Continuing anyway.\n");
1606 }
1607
1608 if (read_it || erase_it || write_it || verify_it) {
1609 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001610 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001611 msg_cerr("Read is not working on this chip. ");
1612 if (!force)
1613 return 1;
1614 msg_cerr("Continuing anyway.\n");
1615 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001616 if (!chip->read) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001617 msg_cerr("flashprog has no read function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001618 "flash chip.\n");
1619 return 1;
1620 }
1621 }
1622 if (erase_it || write_it) {
1623 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001624 if (chip->tested.erase == NA) {
1625 msg_cerr("Erase is not possible on this chip.\n");
1626 return 1;
1627 }
1628 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001629 msg_cerr("Erase is not working on this chip. ");
1630 if (!force)
1631 return 1;
1632 msg_cerr("Continuing anyway.\n");
1633 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001634 if(count_usable_erasers(flash) == 0) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001635 msg_cerr("flashprog has no erase function for this "
Stefan Tauner5368dca2011-07-01 00:19:12 +00001636 "flash chip.\n");
1637 return 1;
1638 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001639 }
1640 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001641 if (chip->tested.write == NA) {
1642 msg_cerr("Write is not possible on this chip.\n");
1643 return 1;
1644 }
1645 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001646 msg_cerr("Write is not working on this chip. ");
1647 if (!force)
1648 return 1;
1649 msg_cerr("Continuing anyway.\n");
1650 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001651 if (!chip->write) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001652 msg_cerr("flashprog has no write function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001653 "flash chip.\n");
1654 return 1;
1655 }
1656 }
1657 return 0;
1658}
1659
Nico Huber305f4172013-06-14 11:55:26 +02001660int prepare_flash_access(struct flashctx *const flash,
1661 const bool read_it, const bool write_it,
1662 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001663{
1664 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1665 msg_cerr("Aborting.\n");
1666 return 1;
1667 }
1668
Nico Huber3ac761c2023-01-16 02:43:17 +01001669 if (layout_sanity_checks(flash, write_it)) {
Nico Huber454f6132012-12-10 13:34:10 +00001670 msg_cerr("Requested regions can not be handled. Aborting.\n");
1671 return 1;
1672 }
1673
Nico Huber901fb952023-01-11 23:24:23 +01001674 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_FULL))
1675 return 1;
1676
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001677 /* Initialize chip_restore_fn_count before chip unlock calls. */
1678 flash->chip_restore_fn_count = 0;
1679
Nico Huber454f6132012-12-10 13:34:10 +00001680 /* Given the existence of read locks, we want to unlock for read,
1681 erase and write. */
1682 if (flash->chip->unlock)
1683 flash->chip->unlock(flash);
1684
1685 return 0;
1686}
1687
Nico Huber305f4172013-06-14 11:55:26 +02001688void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001689{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001690 deregister_chip_restore(flash);
Nico Huber901fb952023-01-11 23:24:23 +01001691 if (flash->chip->finish_access)
1692 flash->chip->finish_access(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001693}
1694
1695/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001696 * @addtogroup flashprog-flash
Nico Huber454f6132012-12-10 13:34:10 +00001697 * @{
1698 */
1699
1700/**
1701 * @brief Erase the specified ROM chip.
1702 *
1703 * If a layout is set in the given flash context, only included regions
1704 * will be erased.
1705 *
1706 * @param flashctx The context of the flash chip to erase.
1707 * @return 0 on success.
1708 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001709int flashprog_flash_erase(struct flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +00001710{
1711 if (prepare_flash_access(flashctx, false, false, true, false))
1712 return 1;
1713
1714 const int ret = erase_by_layout(flashctx);
1715
1716 finalize_flash_access(flashctx);
1717
1718 return ret;
1719}
1720
Nico Huberc3b02dc2023-08-12 01:13:45 +02001721/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +00001722
1723/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001724 * @defgroup flashprog-ops Operations
Nico Huber454f6132012-12-10 13:34:10 +00001725 * @{
1726 */
1727
1728/**
1729 * @brief Read the current image from the specified ROM chip.
1730 *
1731 * If a layout is set in the specified flash context, only included regions
1732 * will be read.
1733 *
1734 * @param flashctx The context of the flash chip.
1735 * @param buffer Target buffer to write image to.
1736 * @param buffer_len Size of target buffer in bytes.
1737 * @return 0 on success,
1738 * 2 if buffer_len is too short for the flash chip's contents,
1739 * or 1 on any other failure.
1740 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001741int flashprog_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001742{
1743 const size_t flash_size = flashctx->chip->total_size * 1024;
1744
1745 if (flash_size > buffer_len)
1746 return 2;
1747
1748 if (prepare_flash_access(flashctx, true, false, false, false))
1749 return 1;
1750
1751 msg_cinfo("Reading flash... ");
1752
1753 int ret = 1;
1754 if (read_by_layout(flashctx, buffer)) {
1755 msg_cerr("Read operation failed!\n");
1756 msg_cinfo("FAILED.\n");
1757 goto _finalize_ret;
1758 }
1759 msg_cinfo("done.\n");
1760 ret = 0;
1761
1762_finalize_ret:
1763 finalize_flash_access(flashctx);
1764 return ret;
1765}
1766
1767static void combine_image_by_layout(const struct flashctx *const flashctx,
1768 uint8_t *const newcontents, const uint8_t *const oldcontents)
1769{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001770 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001771 const struct romentry *included;
1772 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001773
Nico Huber3d7b1e32018-12-22 00:53:14 +01001774 while ((included = layout_next_included_region(layout, start))) {
1775 if (included->start > start) {
1776 /* copy everything up to the start of this included region */
1777 memcpy(newcontents + start, oldcontents + start, included->start - start);
1778 }
1779 /* skip this included region */
1780 start = included->end + 1;
1781 if (start == 0)
1782 return;
Nico Huber454f6132012-12-10 13:34:10 +00001783 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001784
1785 /* copy the rest of the chip */
1786 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1787 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001788}
1789
1790/**
1791 * @brief Write the specified image to the ROM chip.
1792 *
1793 * If a layout is set in the specified flash context, only erase blocks
1794 * containing included regions will be touched.
1795 *
1796 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001797 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001798 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001799 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001800 * @return 0 on success,
1801 * 4 if buffer_len doesn't match the size of the flash chip,
1802 * 3 if write was tried but nothing has changed,
1803 * 2 if write failed and flash contents changed,
1804 * or 1 on any other failure.
1805 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001806int flashprog_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001807 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001808{
1809 const size_t flash_size = flashctx->chip->total_size * 1024;
1810 const bool verify_all = flashctx->flags.verify_whole_chip;
1811 const bool verify = flashctx->flags.verify_after_write;
Nico Huberc3b02dc2023-08-12 01:13:45 +02001812 const struct flashprog_layout *const verify_layout =
Nico Huber74d09d42019-06-16 03:27:26 +02001813 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001814
1815 if (buffer_len != flash_size)
1816 return 4;
1817
1818 int ret = 1;
1819
1820 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001821 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001822 uint8_t *const curcontents = malloc(flash_size);
1823 uint8_t *oldcontents = NULL;
1824 if (verify_all)
1825 oldcontents = malloc(flash_size);
1826 if (!curcontents || (verify_all && !oldcontents)) {
1827 msg_gerr("Out of memory!\n");
1828 goto _free_ret;
1829 }
1830
1831#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001832 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001833 if (flashctx->flags.force_boardmismatch) {
1834 msg_pinfo("Proceeding anyway because user forced us to.\n");
1835 } else {
1836 msg_perr("Aborting. You can override this with "
1837 "-p internal:boardmismatch=force.\n");
1838 goto _free_ret;
1839 }
1840 }
1841#endif
1842
1843 if (prepare_flash_access(flashctx, false, true, false, verify))
1844 goto _free_ret;
1845
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001846 /* If given, assume flash chip contains same data as `refcontents`. */
1847 if (refcontents) {
1848 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1849 memcpy(curcontents, refcontents, flash_size);
1850 if (oldcontents)
1851 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001852 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001853 /*
1854 * Read the whole chip to be able to check whether regions need to be
1855 * erased and to give better diagnostics in case write fails.
1856 * The alternative is to read only the regions which are to be
1857 * preserved, but in that case we might perform unneeded erase which
1858 * takes time as well.
1859 */
1860 msg_cinfo("Reading old flash chip contents... ");
1861 if (verify_all) {
Richard Hughes842d6782021-01-15 09:48:12 +00001862 if (flashprog_read_range(flashctx, oldcontents, 0, flash_size)) {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001863 msg_cinfo("FAILED.\n");
1864 goto _finalize_ret;
1865 }
1866 memcpy(curcontents, oldcontents, flash_size);
1867 } else {
1868 if (read_by_layout(flashctx, curcontents)) {
1869 msg_cinfo("FAILED.\n");
1870 goto _finalize_ret;
1871 }
Nico Huber454f6132012-12-10 13:34:10 +00001872 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001873 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001874 }
Nico Huber454f6132012-12-10 13:34:10 +00001875
1876 if (write_by_layout(flashctx, curcontents, newcontents)) {
1877 msg_cerr("Uh oh. Erase/write failed. ");
1878 ret = 2;
1879 if (verify_all) {
1880 msg_cerr("Checking if anything has changed.\n");
1881 msg_cinfo("Reading current flash chip contents... ");
Richard Hughes842d6782021-01-15 09:48:12 +00001882 if (!flashprog_read_range(flashctx, curcontents, 0, flash_size)) {
Nico Huber454f6132012-12-10 13:34:10 +00001883 msg_cinfo("done.\n");
1884 if (!memcmp(oldcontents, curcontents, flash_size)) {
1885 nonfatal_help_message();
1886 goto _finalize_ret;
1887 }
1888 msg_cerr("Apparently at least some data has changed.\n");
1889 } else
1890 msg_cerr("Can't even read anymore!\n");
1891 emergency_help_message();
1892 goto _finalize_ret;
1893 } else {
1894 msg_cerr("\n");
1895 }
1896 emergency_help_message();
1897 goto _finalize_ret;
1898 }
1899
1900 /* Verify only if we actually changed something. */
1901 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001902 msg_cinfo("Verifying flash... ");
1903
Nico Huber74d09d42019-06-16 03:27:26 +02001904 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001905 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001906 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001907 /* If we tried to write, and verification now fails, we
1908 might have an emergency situation. */
1909 if (ret)
1910 emergency_help_message();
1911 else
1912 msg_cinfo("VERIFIED.\n");
1913 } else {
1914 /* We didn't change anything. */
1915 ret = 0;
1916 }
1917
1918_finalize_ret:
1919 finalize_flash_access(flashctx);
1920_free_ret:
1921 free(oldcontents);
1922 free(curcontents);
1923 return ret;
1924}
1925
1926/**
1927 * @brief Verify the ROM chip's contents with the specified image.
1928 *
1929 * If a layout is set in the specified flash context, only included regions
1930 * will be verified.
1931 *
1932 * @param flashctx The context of the flash chip.
1933 * @param buffer Source buffer to verify with.
1934 * @param buffer_len Size of source buffer in bytes.
1935 * @return 0 on success,
1936 * 3 if the chip's contents don't match,
1937 * 2 if buffer_len doesn't match the size of the flash chip,
1938 * or 1 on any other failure.
1939 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001940int flashprog_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001941{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001942 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001943 const size_t flash_size = flashctx->chip->total_size * 1024;
1944
1945 if (buffer_len != flash_size)
1946 return 2;
1947
1948 const uint8_t *const newcontents = buffer;
1949 uint8_t *const curcontents = malloc(flash_size);
1950 if (!curcontents) {
1951 msg_gerr("Out of memory!\n");
1952 return 1;
1953 }
1954
1955 int ret = 1;
1956
1957 if (prepare_flash_access(flashctx, false, false, false, true))
1958 goto _free_ret;
1959
1960 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001961 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001962 if (!ret)
1963 msg_cinfo("VERIFIED.\n");
1964
1965 finalize_flash_access(flashctx);
1966_free_ret:
1967 free(curcontents);
1968 return ret;
1969}
1970
Nico Huberc3b02dc2023-08-12 01:13:45 +02001971/** @} */ /* end flashprog-ops */