blob: 113b3f602027e58948e0a2ccea4571447fa3744d [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Nico Huber7af0e792016-04-29 16:40:15 +02008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000010 *
Uwe Hermannd1107642007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000015 *
Uwe Hermannd1107642007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000020 */
21
Felix Singerf25447e2022-08-19 02:44:28 +020022#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000023#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000024#include <sys/types.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000025#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000026#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000027#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000028#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000029#include <ctype.h>
Edward O'Callaghan3b64d812022-08-12 13:07:51 +100030
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000031#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000032#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000033#include "programmer.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010034#include "hwaccess_physmap.h"
Nico Huberfbc41d22026-02-22 23:04:01 +010035#include "chipdrivers/spi.h"
Nico Huber9d09e0f2025-03-02 22:55:10 +010036#include "version.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000037
Nico Huberc3b02dc2023-08-12 01:13:45 +020038const char flashprog_version[] = FLASHPROG_VERSION;
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 Hailfingerd1be52d2010-07-03 12:14:25 +000043/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020044bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000045
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000046#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000047static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000048/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000049static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000050 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000051 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000052} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000053/* Initialize to 0 to make sure nobody registers a shutdown function before
54 * programmer init.
55 */
Felix Singerf25447e2022-08-19 02:44:28 +020056static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000057
Stefan Taunerc4f44df2013-08-12 22:58:43 +000058/* Did we change something or was every erase/write skipped (if any)? */
59static bool all_skipped = true;
60
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000061static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000062
Stefan Tauner2a1ed772014-08-31 00:09:21 +000063int shutdown_free(void *data)
64{
65 free(data);
66 return 0;
67}
68
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000069/* Register a function to be executed on programmer shutdown.
70 * The advantage over atexit() is that you can supply a void pointer which will
71 * be used as parameter to the registered function upon programmer shutdown.
72 * This pointer can point to arbitrary data used by said function, e.g. undo
73 * information for GPIO settings etc. If unneeded, set data=NULL.
74 * Please note that the first (void *data) belongs to the function signature of
75 * the function passed as first parameter.
76 */
David Hendricks8bb20212011-06-14 01:35:36 +000077int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000078{
79 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000080 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000081 SHUTDOWN_MAXFN);
82 return 1;
83 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000084 if (!may_register_shutdown) {
85 msg_perr("Tried to register a shutdown function before "
86 "programmer init.\n");
87 return 1;
88 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000089 shutdown_fn[shutdown_fn_count].func = function;
90 shutdown_fn[shutdown_fn_count].data = data;
91 shutdown_fn_count++;
92
93 return 0;
94}
95
Nikolai Artemiev4ad48642020-11-05 13:54:27 +110096int register_chip_restore(chip_restore_fn_cb_t func,
97 struct flashctx *flash, uint8_t status)
98{
99 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
100 msg_perr("Tried to register more than %i chip restore"
101 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
102 return 1;
103 }
104 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
105 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
106 flash->chip_restore_fn_count++;
107
108 return 0;
109}
110
111static int deregister_chip_restore(struct flashctx *flash)
112{
113 int rc = 0;
114
115 while (flash->chip_restore_fn_count > 0) {
116 int i = --flash->chip_restore_fn_count;
117 rc |= flash->chip_restore_fn[i].func(
118 flash, flash->chip_restore_fn[i].status);
119 }
120
121 return rc;
122}
123
Nico Huber2b66ad92023-01-11 20:15:15 +0100124int programmer_init(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000125{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000126 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000127
Nico Huber2b66ad92023-01-11 20:15:15 +0100128 if (prog == NULL || prog->driver == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000129 msg_perr("Invalid programmer specified!\n");
130 return -1;
131 }
Nico Huber2b66ad92023-01-11 20:15:15 +0100132 programmer = prog->driver;
133 programmer_param = prog->param;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000134 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200135 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000136 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200137 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000138
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200139 msg_pdbg("Initializing %s programmer\n", programmer->name);
Nico Hubere3a26882023-01-11 21:45:51 +0100140 ret = programmer->init(prog);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000141 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000142 if (ret != 0) {
143 /* It is quite possible that any unhandled programmer parameter would have been valid,
144 * but an error in actual programmer init happened before the parameter was evaluated.
145 */
146 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
147 programmer_param);
148 } else {
149 /* Actual programmer init was successful, but the user specified an invalid or unusable
150 * (for the current programmer configuration) parameter.
151 */
152 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
153 msg_perr("Aborting.\n");
154 ret = ERROR_FATAL;
155 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000156 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200157 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000158 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000159}
160
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000161/** Calls registered shutdown functions and resets internal programmer-related variables.
162 * Calling it is safe even without previous initialization, but further interactions with programmer support
163 * require a call to programmer_init() (afterwards).
164 *
165 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Nico Huber2b66ad92023-01-11 20:15:15 +0100166int programmer_shutdown(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000167{
David Hendricks8bb20212011-06-14 01:35:36 +0000168 int ret = 0;
169
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000170 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200171 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000172 while (shutdown_fn_count > 0) {
173 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000174 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000175 }
Nico Huberaf9d7382023-05-01 13:33:26 +0200176
177 int i;
178 for (i = 0; i < registered_master_count; ++i) {
179 struct found_id *found_id, *next;
180 for (found_id = registered_masters[i].found_ids; found_id; found_id = next) {
181 next = found_id->next;
182 free(found_id);
183 }
184 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000185 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000186
David Hendricks8bb20212011-06-14 01:35:36 +0000187 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000188}
189
Stefan Taunerf80419c2014-05-02 15:41:42 +0000190void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000191{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000192 if (usecs > 0) {
193 if (programmer->delay)
194 programmer->delay(usecs);
195 else
196 internal_delay(usecs);
197 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000198}
199
Nico Hubercdcfda22023-04-29 13:29:33 +0200200static int read_memmapped_chunk(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000201{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000202 chip_readn(flash, buf, flash->virtual_memory + start, len);
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000203 return 0;
204}
Nico Hubercdcfda22023-04-29 13:29:33 +0200205int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, int unsigned len)
206{
207 return flashprog_read_chunked(flash, buf, start, len, MAX_DATA_READ_UNLIMITED, read_memmapped_chunk);
208}
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000209
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000210/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000211 * It will look for needle with a subsequent '=' in haystack, return a copy of
212 * needle and remove everything from the first occurrence of needle to the next
213 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000214 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200215static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000216{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000217 char *param_pos, *opt_pos, *rest;
218 char *opt = NULL;
219 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000220 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000221
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000222 needlelen = strlen(needle);
223 if (!needlelen) {
224 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200225 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000226 return NULL;
227 }
228 /* No programmer parameters given. */
229 if (*haystack == NULL)
230 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000231 param_pos = strstr(*haystack, needle);
232 do {
233 if (!param_pos)
234 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000235 /* Needle followed by '='? */
236 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000237 /* Beginning of the string? */
238 if (param_pos == *haystack)
239 break;
240 /* After a delimiter? */
241 if (strchr(delim, *(param_pos - 1)))
242 break;
243 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000244 /* Continue searching. */
245 param_pos++;
246 param_pos = strstr(param_pos, needle);
247 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000248
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000249 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000250 /* Get the string after needle and '='. */
251 opt_pos = param_pos + needlelen + 1;
252 optlen = strcspn(opt_pos, delim);
253 /* Return an empty string if the parameter was empty. */
254 opt = malloc(optlen + 1);
255 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000256 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000257 exit(1);
258 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000259 strncpy(opt, opt_pos, optlen);
260 opt[optlen] = '\0';
261 rest = opt_pos + optlen;
262 /* Skip all delimiters after the current parameter. */
263 rest += strspn(rest, delim);
264 memmove(param_pos, rest, strlen(rest) + 1);
265 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000266 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000267
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000268 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000269}
270
Stefan Tauner66652442011-06-26 17:38:17 +0000271char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000272{
273 return extract_param(&programmer_param, param_name, ",");
274}
275
Richard Hughes842d6782021-01-15 09:48:12 +0000276static void flashprog_progress_report(struct flashprog_progress *const p)
277{
278 if (p->current > p->total) {
279 msg_gdbg2("Sanitizing progress report: %zu bytes off.", p->current - p->total);
280 p->current = p->total;
281 }
282
283 if (!p->callback)
284 return;
285
286 p->callback(p->stage, p->current, p->total, p->user_data);
287}
288
289static void flashprog_progress_start(struct flashprog_flashctx *const flashctx,
290 const enum flashprog_progress_stage stage, const size_t total)
291{
292 flashctx->progress.stage = stage;
293 flashctx->progress.current = 0;
294 flashctx->progress.total = total;
295 flashprog_progress_report(&flashctx->progress);
296}
297
298static void flashprog_progress_start_by_layout(struct flashprog_flashctx *const flashctx,
299 const enum flashprog_progress_stage stage,
300 const struct flashprog_layout *const layout)
301{
302 const struct romentry *entry = NULL;
303 size_t total = 0;
304
305 while ((entry = layout_next_included(layout, entry)))
306 total += entry->end - entry->start + 1;
307
308 flashprog_progress_start(flashctx, stage, total);
309}
310
311static void flashprog_progress_set(struct flashprog_flashctx *const flashctx, const size_t current)
312{
313 flashctx->progress.current = current;
314 flashprog_progress_report(&flashctx->progress);
315}
316
317/** @private */
318void flashprog_progress_add(struct flashprog_flashctx *const flashctx, const size_t progress)
319{
320 flashctx->progress.current += progress;
321 flashprog_progress_report(&flashctx->progress);
322}
323
324static void flashprog_progress_finish(struct flashprog_flashctx *const flashctx)
325{
326 if (flashctx->progress.current == flashctx->progress.total)
327 return;
328
329 flashctx->progress.current = flashctx->progress.total;
330 flashprog_progress_report(&flashctx->progress);
331}
332
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000333/* Returns the number of well-defined erasers for a chip. */
Nico Huberbb8ab372026-03-14 11:46:02 +0100334unsigned int flashprog_count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000335{
336 unsigned int usable_erasefunctions = 0;
337 int k;
338 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
339 if (!check_block_eraser(flash, k, 0))
340 usable_erasefunctions++;
341 }
342 return usable_erasefunctions;
343}
344
Mark Marshallf20b7be2014-05-09 21:16:21 +0000345static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000346{
347 int ret = 0, failcount = 0;
348 unsigned int i;
349 for (i = 0; i < len; i++) {
350 if (wantbuf[i] != havebuf[i]) {
351 /* Only print the first failure. */
352 if (!failcount++)
353 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
354 start + i, wantbuf[i], havebuf[i]);
355 }
356 }
357 if (failcount) {
358 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
359 start, start + len - 1, failcount);
360 ret = -1;
361 }
362 return ret;
363}
364
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000365/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600366static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000367{
368 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300369 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000370
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100371 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000372 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100373 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100374 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000375 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300376 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000377 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100378
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000379 free(cmpbuf);
380 return ret;
381}
382
Richard Hughes842d6782021-01-15 09:48:12 +0000383int flashprog_read_range(struct flashctx *flash, uint8_t *buf, unsigned int start, unsigned int len)
384{
385 flashprog_progress_start(flash, FLASHPROG_PROGRESS_READ, len);
386 const int ret = flash->chip->read(flash, buf, start, len);
387 flashprog_progress_finish(flash);
388 return ret;
389}
390
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000391/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000392 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000393 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000394 * @start offset to the base address of the flash chip
395 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000396 * @return 0 for success, -1 for failure
397 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000398int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000399{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000400 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000401 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000402
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100403 if (start + len > flash->chip->total_size * 1024) {
404 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
405 " total_size 0x%x\n", __func__, start, len,
406 flash->chip->total_size * 1024);
407 return -1;
408 }
409
Stefan Taunerdf64a422014-05-27 00:06:14 +0000410 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000411 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100412 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000413 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000414 }
415
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100416 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000417 if (ret) {
418 msg_gerr("Verification impossible because read failed "
419 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000420 ret = -1;
421 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000422 }
423
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000424 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000425out_free:
426 free(readbuf);
427 return ret;
428}
429
Nico Huber3ac761c2023-01-16 02:43:17 +0100430size_t gran_to_bytes(const enum write_granularity gran)
Nico Huberb77607f2023-01-16 02:25:45 +0100431{
432 switch (gran) {
433 case write_gran_1bit: return 1;
434 case write_gran_1byte: return 1;
435 case write_gran_1byte_implicit_erase: return 1;
436 case write_gran_128bytes: return 128;
437 case write_gran_256bytes: return 256;
438 case write_gran_264bytes: return 264;
439 case write_gran_512bytes: return 512;
440 case write_gran_528bytes: return 528;
441 case write_gran_1024bytes: return 1024;
442 case write_gran_1056bytes: return 1056;
443 default: return 0;
444 }
445}
446
Stefan Tauner02437452013-04-01 19:34:53 +0000447/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300448static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
449 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000450{
451 unsigned int i, j, limit;
452 for (j = 0; j < len / gran; j++) {
453 limit = min (gran, len - j * gran);
454 /* Are 'have' and 'want' identical? */
455 if (!memcmp(have + j * gran, want + j * gran, limit))
456 continue;
457 /* have needs to be in erased state. */
458 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300459 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000460 return 1;
461 }
462 return 0;
463}
464
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000465/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000466 * Check if the buffer @have can be programmed to the content of @want without
467 * erasing. This is only possible if all chunks of size @gran are either kept
468 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000469 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000470 * Warning: This function assumes that @have and @want point to naturally
471 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000472 *
473 * @have buffer with current content
474 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000475 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000476 * @gran write granularity (enum, not count)
477 * @return 0 if no erase is needed, 1 otherwise
478 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000479static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
480 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000481{
Stefan Tauner02437452013-04-01 19:34:53 +0000482 unsigned int i;
Nico Huberb77607f2023-01-16 02:25:45 +0100483 size_t stride;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000484
485 switch (gran) {
486 case write_gran_1bit:
Nico Huberb77607f2023-01-16 02:25:45 +0100487 for (i = 0; i < len; i++) {
488 if ((have[i] & want[i]) != want[i])
489 return 1;
490 }
491 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000492 case write_gran_1byte:
Nico Huberb77607f2023-01-16 02:25:45 +0100493 for (i = 0; i < len; i++) {
494 if ((have[i] != want[i]) && (have[i] != erased_value))
495 return 1;
496 }
497 return 0;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000498 case write_gran_1byte_implicit_erase:
499 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
Nico Huberb77607f2023-01-16 02:25:45 +0100500 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000501 default:
Nico Huberb77607f2023-01-16 02:25:45 +0100502 stride = gran_to_bytes(gran);
503 if (stride) {
504 return need_erase_gran_bytes(have, want, len, stride, erased_value);
505 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000506 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200507 "flashprog@flashprog.org\n", __func__);
Nico Huberb77607f2023-01-16 02:25:45 +0100508 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000509 }
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000510}
511
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000512/**
513 * Check if the buffer @have needs to be programmed to get the content of @want.
514 * If yes, return 1 and fill in first_start with the start address of the
515 * write operation and first_len with the length of the first to-be-written
516 * chunk. If not, return 0 and leave first_start and first_len undefined.
517 *
518 * Warning: This function assumes that @have and @want point to naturally
519 * aligned regions.
520 *
521 * @have buffer with current content
522 * @want buffer with desired content
523 * @len length of the checked area
524 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000525 * @first_start offset of the first byte which needs to be written (passed in
526 * value is increased by the offset of the first needed write
527 * relative to have/want or unchanged if no write is needed)
528 * @return length of the first contiguous area which needs to be written
529 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000530 *
531 * FIXME: This function needs a parameter which tells it about coalescing
532 * in relation to the max write length of the programmer and the max write
533 * length of the chip.
534 */
Nico Huber3b9c86d2023-01-15 22:58:06 +0100535static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, chipsize_t len,
536 chipoff_t *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000537{
Felix Singerf25447e2022-08-19 02:44:28 +0200538 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000539 unsigned int rel_start = 0, first_len = 0;
Nico Huberb77607f2023-01-16 02:25:45 +0100540 unsigned int i, limit;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000541
Nico Huberb77607f2023-01-16 02:25:45 +0100542 const size_t stride = gran_to_bytes(gran);
543 if (!stride) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000544 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200545 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000546 /* Claim that no write was needed. A write with unknown
547 * granularity is too dangerous to try.
548 */
549 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000550 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000551 for (i = 0; i < len / stride; i++) {
552 limit = min(stride, len - i * stride);
553 /* Are 'have' and 'want' identical? */
554 if (memcmp(have + i * stride, want + i * stride, limit)) {
555 if (!need_write) {
556 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200557 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000558 rel_start = i * stride;
559 }
560 } else {
561 if (need_write) {
562 /* First location where have and want
563 * do not differ anymore.
564 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000565 break;
566 }
567 }
568 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000569 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000570 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000571 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000572 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000573}
574
Nico Huber2d625722016-05-03 10:48:02 +0200575/*
576 * Return a string corresponding to the bustype parameter.
577 * Memory is obtained with malloc() and must be freed with free() by the caller.
578 */
579char *flashbuses_to_text(enum chipbustype bustype)
580{
581 char *ret = calloc(1, 1);
582 /*
583 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
584 * will cease to exist and should be eliminated here as well.
585 */
Nico Huber0d2b45e2026-03-23 22:42:46 +0100586 if (bustype == BUS_PRESPI) {
Nico Huber2d625722016-05-03 10:48:02 +0200587 ret = strcat_realloc(ret, "Non-SPI, ");
588 } else {
589 if (bustype & BUS_PARALLEL)
590 ret = strcat_realloc(ret, "Parallel, ");
591 if (bustype & BUS_LPC)
592 ret = strcat_realloc(ret, "LPC, ");
593 if (bustype & BUS_FWH)
594 ret = strcat_realloc(ret, "FWH, ");
595 if (bustype & BUS_SPI)
596 ret = strcat_realloc(ret, "SPI, ");
Nico Huber0d2b45e2026-03-23 22:42:46 +0100597 if (bustype & BUS_OPAQUE)
Nico Huber2d625722016-05-03 10:48:02 +0200598 ret = strcat_realloc(ret, "Programmer-specific, ");
599 if (bustype == BUS_NONE)
600 ret = strcat_realloc(ret, "None, ");
601 }
602 /* Kill last comma. */
603 ret[strlen(ret) - 2] = '\0';
604 ret = realloc(ret, strlen(ret) + 1);
605 return ret;
606}
607
Nico Huber484ef612026-03-08 21:02:52 +0100608void flashprog_bus_probe(struct registered_master *const mst, const struct flashchip *const chip)
Nico Huberaf9d7382023-05-01 13:33:26 +0200609{
Nico Huberac138732026-02-28 17:42:27 +0100610 unsigned int least_priority, priority, i;
Nico Huberaf9d7382023-05-01 13:33:26 +0200611 struct found_id **next_ptr;
Nico Huberaf9d7382023-05-01 13:33:26 +0200612
613 if (mst->probed)
614 return;
615
Nico Huberac138732026-02-28 17:42:27 +0100616 for (i = 0, least_priority = 0; i < mst->probing.probe_count; ++i) {
617 if (least_priority < mst->probing.probes[i].priority)
618 least_priority = mst->probing.probes[i].priority;
619 }
620
Nico Huberaf9d7382023-05-01 13:33:26 +0200621 next_ptr = &mst->found_ids;
Nico Huberac138732026-02-28 17:42:27 +0100622 for (priority = 0; priority <= least_priority; ++priority) {
623 bool found = false;
Nico Huberaf9d7382023-05-01 13:33:26 +0200624
Nico Huberac138732026-02-28 17:42:27 +0100625 for (i = 0; i < mst->probing.probe_count; ++i) {
626 if (mst->probing.probes[i].priority != priority)
627 continue;
Nico Huberaf9d7382023-05-01 13:33:26 +0200628
Nico Huberdae90222026-03-09 20:36:56 +0100629 if (chip && chip->id.type != mst->probing.probes[i].type)
Nico Huberac138732026-02-28 17:42:27 +0100630 continue;
631
Nico Huberdae90222026-03-09 20:36:56 +0100632 *next_ptr = mst->probing.probes[i].run(&mst->probing.probes[i], &mst->common, chip);
Nico Huberac138732026-02-28 17:42:27 +0100633 found |= !!*next_ptr;
634
635 /* walk to end in case multiple IDs were found in a single call */
636 while (*next_ptr)
637 next_ptr = &(*next_ptr)->next;
638 }
639
640 /* Skip lower-priority probing if anything was found. */
641 if (found)
642 break;
Nico Huberaf9d7382023-05-01 13:33:26 +0200643 }
644
645 mst->probed = true;
646}
647
Stefan Tauner96658be2014-05-26 22:05:31 +0000648/* Even if an error is found, the function will keep going and check the rest. */
Nico Huber72d0ada2026-01-26 18:28:20 +0100649static int selfcheck_eraseblocks(const struct flashchip *chip, const char *label)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000650{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000651 int i, j, k;
652 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530653 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000654
655 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
656 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000657 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530658 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000659
660 for (i = 0; i < NUM_ERASEREGIONS; i++) {
661 /* Blocks with zero size are bugs in flashchips.c. */
662 if (eraser.eraseblocks[i].count &&
663 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000664 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200665 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100666 label, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000667 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000668 }
669 /* Blocks with zero count are bugs in flashchips.c. */
670 if (!eraser.eraseblocks[i].count &&
671 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000672 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200673 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100674 label, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000675 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000676 }
677 done += eraser.eraseblocks[i].count *
678 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530679 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000680 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000681 /* Empty eraseblock definition with erase function. */
682 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000683 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000684 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000685 if (!done)
686 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000687 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000688 msg_gerr("ERROR: Flash chip %s erase function %i "
689 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000690 " expected 0x%06x bytes.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200691 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100692 label, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000693 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000694 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000695 if (!eraser.block_erase)
696 continue;
697 /* Check if there are identical erase functions for different
698 * layouts. That would imply "magic" erase functions. The
699 * easiest way to check this is with function pointers.
700 */
Uwe Hermann43959702010-03-13 17:28:29 +0000701 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000702 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000703 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000704 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200705 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100706 label, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000707 ret = 1;
708 }
Uwe Hermann43959702010-03-13 17:28:29 +0000709 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530710 if(curr_eraseblock_count > prev_eraseblock_count)
711 {
Nico Huberac90af62022-12-18 00:22:47 +0000712 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200713 "Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +0100714 label, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530715 ret = 1;
716 }
717 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000718 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000719 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000720}
721
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000722static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000723{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000724 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000725
726 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
727 if (log)
728 msg_cdbg("not defined. ");
729 return 1;
730 }
731 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
732 if (log)
733 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000734 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000735 return 1;
736 }
737 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
738 if (log)
739 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000740 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000741 return 1;
742 }
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530743
Nico Hubere149fbe2024-08-21 10:23:53 +0200744 if (flash->chip->bustype == BUS_SPI && flash->chip->spi_cmd_set == SPI25) {
Nico Huber13389362024-03-05 18:35:30 +0100745 bool native_4ba;
Nico Huber13f97a52023-01-14 23:55:06 +0100746 int i;
Nico Huber13389362024-03-05 18:35:30 +0100747
748 const uint8_t *opcode = spi_get_opcode_from_erasefn(eraser.block_erase, &native_4ba);
Nico Huber07ebc682024-08-21 10:18:27 +0200749 if (!opcode)
750 return 1;
751
Nico Huber13f97a52023-01-14 23:55:06 +0100752 for (i = 0; opcode[i]; i++) {
Nico Huber13389362024-03-05 18:35:30 +0100753 if ((native_4ba && !spi_master_4ba(flash)) ||
Nico Huber9a11cbf2023-01-13 01:19:07 +0100754 !flash->mst.spi->probe_opcode(flash, opcode[i])) {
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530755 if (log)
756 msg_cdbg("block erase function and layout found "
757 "but SPI master doesn't support the function. ");
758 return 1;
759 }
760 }
761 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000762 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000763 return 0;
764}
765
Nico Huber7af0e792016-04-29 16:40:15 +0200766/**
767 * @brief Reads the included layout regions into a buffer.
768 *
769 * If there is no layout set in the given flash context, the whole chip will
770 * be read.
771 *
772 * @param flashctx Flash context to be used.
773 * @param buffer Buffer of full chip size to read into.
774 * @return 0 on success,
775 * 1 if any read fails.
776 */
777static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
778{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200779 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200780 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200781
Richard Hughes842d6782021-01-15 09:48:12 +0000782 flashprog_progress_start_by_layout(flashctx, FLASHPROG_PROGRESS_READ, layout);
783
Nico Huber5ca55232019-06-15 22:29:08 +0200784 while ((entry = layout_next_included(layout, entry))) {
785 const chipoff_t region_start = entry->start;
786 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200787
788 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
789 return 1;
790 }
Richard Hughes842d6782021-01-15 09:48:12 +0000791
792 flashprog_progress_finish(flashctx);
793
Nico Huber7af0e792016-04-29 16:40:15 +0200794 return 0;
795}
796
Nico Huber7af0e792016-04-29 16:40:15 +0200797/**
798 * @private
799 *
800 * For read-erase-write, `curcontents` and `newcontents` shall point
801 * to buffers of the chip's size. Both are supposed to be prefilled
802 * with at least the included layout regions of the current flash
803 * contents (`curcontents`) and the data to be written to the flash
804 * (`newcontents`).
805 *
806 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
807 *
808 * The `chipoff_t` values are used internally by `walk_by_layout()`.
809 */
810struct walk_info {
811 uint8_t *curcontents;
812 const uint8_t *newcontents;
813 chipoff_t region_start;
814 chipoff_t region_end;
815 chipoff_t erase_start;
816 chipoff_t erase_end;
817};
Nico Huber3b9c86d2023-01-15 22:58:06 +0100818
Nico Huberf9666632026-06-16 12:53:27 +0200819/** @private */
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530820struct eraseblock_data {
821 chipoff_t start_addr;
822 chipoff_t end_addr;
823 bool selected;
824 size_t block_num;
825 size_t first_sub_block_index;
826 size_t last_sub_block_index;
827};
828
Nico Huberf9666632026-06-16 12:53:27 +0200829/** @private */
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530830struct erase_layout {
831 struct eraseblock_data* layout_list;
832 size_t block_count;
833 const struct block_eraser *eraser;
834};
835
Nico Huber5ff6fdc2023-01-15 23:43:12 +0100836static bool explicit_erase(const struct walk_info *const info)
837{
838 /* For explicit erase, we are called without new contents. */
839 return !info->newcontents;
840}
841
Nico Huberd96e7032023-01-14 22:31:48 +0100842static size_t calculate_block_count(const struct block_eraser *const eraser)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530843{
Nico Huberd96e7032023-01-14 22:31:48 +0100844 size_t block_count = 0, i;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530845
Nico Huberd96e7032023-01-14 22:31:48 +0100846 for (i = 0; i < ARRAY_SIZE(eraser->eraseblocks); ++i)
847 block_count += eraser->eraseblocks[i].count;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530848
849 return block_count;
850}
851
852static void init_eraseblock(struct erase_layout *layout, size_t idx, size_t block_num,
853 chipoff_t start_addr, chipoff_t end_addr, size_t *sub_block_index)
854{
855 struct eraseblock_data *edata = &layout[idx].layout_list[block_num];
856 edata->start_addr = start_addr;
857 edata->end_addr = end_addr;
858 edata->selected = false;
859 edata->block_num = block_num;
860
861 if (!idx)
862 return;
Nico Hubera02df332023-01-14 23:06:27 +0100863 const struct erase_layout *const sub_layout = &layout[idx - 1];
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530864
865 edata->first_sub_block_index = *sub_block_index;
Nico Hubera02df332023-01-14 23:06:27 +0100866 for (; *sub_block_index < sub_layout->block_count; ++*sub_block_index) {
867 if (sub_layout->layout_list[*sub_block_index].end_addr > end_addr)
868 break;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530869 }
870 edata->last_sub_block_index = *sub_block_index - 1;
871}
872
873/*
874 * @brief Function to free the created erase_layout
875 *
876 * @param layout pointer to allocated layout
877 * @param erasefn_count number of erase functions for which the layout was created
878 *
879 */
880static void free_erase_layout(struct erase_layout *layout, unsigned int erasefn_count)
881{
Nico Huber13f97a52023-01-14 23:55:06 +0100882 size_t i;
883
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530884 if (!layout)
885 return;
Nico Huber13f97a52023-01-14 23:55:06 +0100886 for (i = 0; i < erasefn_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530887 free(layout[i].layout_list);
888 }
889 free(layout);
890}
891
892/*
893 * @brief Function to create an erase layout
894 *
895 * @param flashctx flash context
896 * @param e_layout address to the pointer to store the layout
897 * @return 0 on success,
898 * -1 if layout creation fails
899 *
900 * This function creates a layout of which erase functions erase which regions
901 * of the flash chip. This helps to optimally select the erase functions for
902 * erase/write operations.
903 */
Nico Huberc09fca42023-01-29 15:58:09 +0100904static int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530905{
906 const struct flashchip *chip = flashctx->chip;
Nico Huberbb8ab372026-03-14 11:46:02 +0100907 const size_t erasefn_count = flashprog_count_usable_erasers(flashctx);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530908
909 if (!erasefn_count) {
910 msg_gerr("No erase functions supported\n");
911 return 0;
912 }
913
Nico Huberd34864b2023-01-14 23:47:19 +0100914 struct erase_layout *layout = calloc(erasefn_count, sizeof(struct erase_layout));
915 if (!layout) {
916 msg_gerr("Out of memory!\n");
917 return -1;
918 }
919
Nico Huber13f97a52023-01-14 23:55:06 +0100920 size_t layout_idx = 0, eraser_idx;
921 for (eraser_idx = 0; eraser_idx < NUM_ERASEFUNCTIONS; eraser_idx++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530922 if (check_block_eraser(flashctx, eraser_idx, 0))
923 continue;
924
925 layout[layout_idx].eraser = &chip->block_erasers[eraser_idx];
Nico Huberd96e7032023-01-14 22:31:48 +0100926 const size_t block_count = calculate_block_count(&chip->block_erasers[eraser_idx]);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530927 size_t sub_block_index = 0;
928
929 layout[layout_idx].block_count = block_count;
930 layout[layout_idx].layout_list = (struct eraseblock_data *)calloc(block_count,
931 sizeof(struct eraseblock_data));
932
933 if (!layout[layout_idx].layout_list) {
934 free_erase_layout(layout, layout_idx);
935 return -1;
936 }
937
938 size_t block_num = 0;
939 chipoff_t start_addr = 0;
940
Nico Huber13f97a52023-01-14 23:55:06 +0100941 int i;
942 for (i = 0; block_num < block_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530943 const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];
944
Nico Huber13f97a52023-01-14 23:55:06 +0100945 size_t num;
946 for (num = 0; num < block->count; num++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530947 chipoff_t end_addr = start_addr + block->size - 1;
948 init_eraseblock(layout, layout_idx, block_num,
949 start_addr, end_addr, &sub_block_index);
950 block_num += 1;
951 start_addr = end_addr + 1;
952 }
953 }
954 layout_idx++;
955 }
956
957 *e_layout = layout;
958 return layout_idx;
959}
960
Nico Huber20073e72024-05-10 01:31:40 +0200961static void deselect_erase_block_rec(const struct erase_layout *layout, size_t findex, size_t block_num)
962{
963 struct eraseblock_data *const ed = &layout[findex].layout_list[block_num];
964 size_t i;
965
966 if (ed->selected) {
967 ed->selected = false;
968 } else if (findex > 0) {
969 for (i = ed->first_sub_block_index; i <= ed->last_sub_block_index; ++i)
970 deselect_erase_block_rec(layout, findex - 1, i);
971 }
972}
973
Aarya Chaumald33bea42022-07-14 12:51:14 +0530974/*
975 * @brief Function to select the list of sectors that need erasing
976 *
977 * @param flashctx flash context
978 * @param layout erase layout
979 * @param findex index of the erase function
980 * @param block_num index of the block to erase according to the erase function index
Nico Huber00d1b9f2023-01-29 15:07:33 +0100981 * @param info current info from walking the regions
Nico Huberaa714dd2023-04-22 14:59:33 +0200982 * @return number of bytes selected for erase
Aarya Chaumald33bea42022-07-14 12:51:14 +0530983 */
Nico Huberaa714dd2023-04-22 14:59:33 +0200984static size_t select_erase_functions_rec(const struct flashctx *flashctx, const struct erase_layout *layout,
985 size_t findex, size_t block_num, const struct walk_info *info)
Aarya Chaumald33bea42022-07-14 12:51:14 +0530986{
987 struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
Nico Huberaa714dd2023-04-22 14:59:33 +0200988 const size_t eraseblock_size = ll->end_addr - ll->start_addr + 1;
Aarya Chaumald33bea42022-07-14 12:51:14 +0530989 if (!findex) {
Nico Hubercf6ff0a2023-01-29 15:45:06 +0100990 if (ll->start_addr <= info->region_end && ll->end_addr >= info->region_start) {
Nico Huber1494f8e2023-01-29 15:48:00 +0100991 if (explicit_erase(info)) {
992 ll->selected = true;
Nico Huberaa714dd2023-04-22 14:59:33 +0200993 return eraseblock_size;
Nico Huber1494f8e2023-01-29 15:48:00 +0100994 }
Nico Hubera6212482023-01-29 15:39:26 +0100995 const chipoff_t write_start = MAX(info->region_start, ll->start_addr);
996 const chipoff_t write_end = MIN(info->region_end, ll->end_addr);
997 const chipsize_t write_len = write_end - write_start + 1;
998 const uint8_t erased_value = ERASED_VALUE(flashctx);
Nico Huber00d1b9f2023-01-29 15:07:33 +0100999 ll->selected = need_erase(
Nico Hubera6212482023-01-29 15:39:26 +01001000 info->curcontents + write_start, info->newcontents + write_start,
1001 write_len, flashctx->chip->gran, erased_value);
Nico Huberaa714dd2023-04-22 14:59:33 +02001002 if (ll->selected)
1003 return eraseblock_size;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301004 }
Nico Huberaa714dd2023-04-22 14:59:33 +02001005 return 0;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301006 } else {
Aarya Chaumald33bea42022-07-14 12:51:14 +05301007 const int sub_block_start = ll->first_sub_block_index;
1008 const int sub_block_end = ll->last_sub_block_index;
Nico Huberaa714dd2023-04-22 14:59:33 +02001009 size_t bytes = 0;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301010
Nico Huber13f97a52023-01-14 23:55:06 +01001011 int j;
Nico Huberaa714dd2023-04-22 14:59:33 +02001012 for (j = sub_block_start; j <= sub_block_end; j++)
1013 bytes += select_erase_functions_rec(flashctx, layout, findex - 1, j, info);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301014
Nico Huberaa714dd2023-04-22 14:59:33 +02001015 if (bytes > eraseblock_size / 2) {
Nico Huber00d1b9f2023-01-29 15:07:33 +01001016 if (ll->start_addr >= info->region_start && ll->end_addr <= info->region_end) {
Nico Huber20073e72024-05-10 01:31:40 +02001017 deselect_erase_block_rec(layout, findex, block_num);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301018 ll->selected = true;
Nico Huberaa714dd2023-04-22 14:59:33 +02001019 bytes = eraseblock_size;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301020 }
1021 }
Nico Huberaa714dd2023-04-22 14:59:33 +02001022 return bytes;
Aarya Chaumald33bea42022-07-14 12:51:14 +05301023 }
1024}
1025
Nico Huberaa714dd2023-04-22 14:59:33 +02001026static size_t select_erase_functions(const struct flashctx *flashctx, const struct erase_layout *layout,
1027 size_t erasefn_count, const struct walk_info *info)
Nico Huberb11b72c2023-01-29 15:33:11 +01001028{
Nico Huberaa714dd2023-04-22 14:59:33 +02001029 size_t bytes = 0;
Nico Huberb11b72c2023-01-29 15:33:11 +01001030 size_t block_num;
1031 for (block_num = 0; block_num < layout[erasefn_count - 1].block_count; ++block_num)
Nico Huberaa714dd2023-04-22 14:59:33 +02001032 bytes += select_erase_functions_rec(flashctx, layout, erasefn_count - 1, block_num, info);
1033 return bytes;
Nico Huberb11b72c2023-01-29 15:33:11 +01001034}
1035
Nico Huber3b9c86d2023-01-15 22:58:06 +01001036static int write_range(struct flashctx *const flashctx, const chipoff_t flash_offset,
1037 const uint8_t *const curcontents, const uint8_t *const newcontents,
1038 const chipsize_t len, bool *const skipped)
1039{
1040 unsigned int writecount = 0;
1041 chipoff_t starthere = 0;
1042 chipsize_t lenhere = 0;
1043
1044 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1045 len - starthere, &starthere, flashctx->chip->gran))) {
1046 if (!writecount++)
1047 msg_cdbg("W");
1048 if (flashctx->chip->write(flashctx, newcontents + starthere,
1049 flash_offset + starthere, lenhere))
1050 return 1;
1051 starthere += lenhere;
Richard Hughes842d6782021-01-15 09:48:12 +00001052 if (skipped) {
1053 flashprog_progress_set(flashctx, starthere);
Nico Huber3b9c86d2023-01-15 22:58:06 +01001054 *skipped = false;
Richard Hughes842d6782021-01-15 09:48:12 +00001055 }
Nico Huber3b9c86d2023-01-15 22:58:06 +01001056 }
1057 return 0;
1058}
1059
1060typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
Nico Huber7af0e792016-04-29 16:40:15 +02001061/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1062typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1063
1064static int walk_eraseblocks(struct flashctx *const flashctx,
Nico Huberc09fca42023-01-29 15:58:09 +01001065 struct erase_layout *const layouts,
1066 const size_t layout_count,
Nico Huber7af0e792016-04-29 16:40:15 +02001067 struct walk_info *const info,
Nico Huberc09fca42023-01-29 15:58:09 +01001068 const per_blockfn_t per_blockfn)
Nico Huber7af0e792016-04-29 16:40:15 +02001069{
1070 int ret;
1071 size_t i, j;
1072 bool first = true;
Nico Huber7af0e792016-04-29 16:40:15 +02001073
Nico Huberc09fca42023-01-29 15:58:09 +01001074 for (i = 0; i < layout_count; ++i) {
1075 const struct erase_layout *const layout = &layouts[i];
Nico Huber7af0e792016-04-29 16:40:15 +02001076
Nico Huberc09fca42023-01-29 15:58:09 +01001077 for (j = 0; j < layout->block_count; ++j) {
1078 struct eraseblock_data *const eb = &layout->layout_list[j];
1079
1080 if (eb->start_addr > info->region_end)
Nico Huber7af0e792016-04-29 16:40:15 +02001081 break;
Nico Huberc09fca42023-01-29 15:58:09 +01001082 if (eb->end_addr < info->region_start)
1083 continue;
1084 if (!eb->selected)
1085 continue;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001086
Nico Huber7af0e792016-04-29 16:40:15 +02001087 /* Print this for every block except the first one. */
1088 if (first)
1089 first = false;
1090 else
1091 msg_cdbg(", ");
Nico Huberc09fca42023-01-29 15:58:09 +01001092 msg_cdbg("0x%06x-0x%06x:", eb->start_addr, eb->end_addr);
Nico Huber7af0e792016-04-29 16:40:15 +02001093
Nico Huberc09fca42023-01-29 15:58:09 +01001094 info->erase_start = eb->start_addr;
1095 info->erase_end = eb->end_addr;
1096 ret = per_blockfn(flashctx, info, layout->eraser->block_erase);
Nico Huber7af0e792016-04-29 16:40:15 +02001097 if (ret)
1098 return ret;
Nico Huberc09fca42023-01-29 15:58:09 +01001099
1100 /* Clean the erase layout up for future use on other
1101 regions. `.selected` is the only field we alter. */
1102 eb->selected = false;
Nico Huber7af0e792016-04-29 16:40:15 +02001103 }
Nico Huber7af0e792016-04-29 16:40:15 +02001104 }
1105 msg_cdbg("\n");
1106 return 0;
1107}
1108
1109static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1110 const per_blockfn_t per_blockfn)
1111{
Nico Huberc09fca42023-01-29 15:58:09 +01001112 const bool do_erase = explicit_erase(info) || !(flashctx->chip->feature_bits & FEATURE_NO_ERASE);
Nico Huberc3b02dc2023-08-12 01:13:45 +02001113 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huberc09fca42023-01-29 15:58:09 +01001114 struct erase_layout *erase_layouts = NULL;
Nico Huber5ca55232019-06-15 22:29:08 +02001115 const struct romentry *entry = NULL;
Nico Huberc09fca42023-01-29 15:58:09 +01001116 int ret = 0, layout_count = 0;
Nico Huber7af0e792016-04-29 16:40:15 +02001117
1118 all_skipped = true;
Nico Huberc6a924a2024-09-04 15:09:52 +02001119 msg_cinfo("Erasing %sflash chip... ", info->newcontents ? "and writing " : "");
Nico Huber7af0e792016-04-29 16:40:15 +02001120
Nico Huberc09fca42023-01-29 15:58:09 +01001121 if (do_erase) {
1122 layout_count = create_erase_layout(flashctx, &erase_layouts);
1123 if (layout_count <= 0)
1124 return 1;
1125 }
1126
Nico Huber5ca55232019-06-15 22:29:08 +02001127 while ((entry = layout_next_included(layout, entry))) {
1128 info->region_start = entry->start;
1129 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001130
Nico Huberc09fca42023-01-29 15:58:09 +01001131 if (do_erase) {
Richard Hughes842d6782021-01-15 09:48:12 +00001132 const size_t total = select_erase_functions(flashctx, erase_layouts, layout_count, info);
1133
1134 /* We verify every erased block manually. Technically that's
1135 reading, but accounting for it as part of the erase helps
1136 to provide a smooth, overall progress. Hence `total * 2`. */
1137 flashprog_progress_start(flashctx, FLASHPROG_PROGRESS_ERASE, total * 2);
1138
Nico Huberc09fca42023-01-29 15:58:09 +01001139 ret = walk_eraseblocks(flashctx, erase_layouts, layout_count, info, per_blockfn);
1140 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001141 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001142 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001143 }
Richard Hughes842d6782021-01-15 09:48:12 +00001144
1145 flashprog_progress_finish(flashctx);
Nico Huber7af0e792016-04-29 16:40:15 +02001146 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001147
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001148 if (info->newcontents) {
1149 bool skipped = true;
1150 msg_cdbg("0x%06x-0x%06x:", info->region_start, info->region_end);
Richard Hughes842d6782021-01-15 09:48:12 +00001151 flashprog_progress_start(flashctx, FLASHPROG_PROGRESS_WRITE,
1152 info->region_end - info->region_start + 1);
Nico Huberc09fca42023-01-29 15:58:09 +01001153 ret = write_range(flashctx, info->region_start,
1154 info->curcontents + info->region_start,
1155 info->newcontents + info->region_start,
1156 info->region_end + 1 - info->region_start, &skipped);
1157 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001158 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001159 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001160 }
Richard Hughes842d6782021-01-15 09:48:12 +00001161 flashprog_progress_finish(flashctx);
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001162 if (skipped) {
1163 msg_cdbg("S\n");
1164 } else {
1165 msg_cdbg("\n");
1166 all_skipped = false;
1167 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001168 }
Nico Huber7af0e792016-04-29 16:40:15 +02001169 }
1170 if (all_skipped)
1171 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
Nico Huberc6a924a2024-09-04 15:09:52 +02001172 msg_cinfo("Erase%s done.\n", info->newcontents ? "/write" : "");
Nico Huberc09fca42023-01-29 15:58:09 +01001173
1174free_ret:
1175 free_erase_layout(erase_layouts, layout_count);
1176 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001177}
1178
1179static int erase_block(struct flashctx *const flashctx,
1180 const struct walk_info *const info, const erasefn_t erasefn)
1181{
1182 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001183 const bool region_unaligned = info->region_start > info->erase_start ||
1184 info->erase_end > info->region_end;
1185 uint8_t *backup_contents = NULL, *erased_contents = NULL;
Nico Huberd34af7a2023-01-15 23:58:18 +01001186 int ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001187
Nico Huber6e61e0c2019-01-23 17:07:49 +01001188 /*
1189 * If the region is not erase-block aligned, merge current flash con-
1190 * tents into a new buffer `backup_contents`.
1191 */
1192 if (region_unaligned) {
1193 backup_contents = malloc(erase_len);
1194 erased_contents = malloc(erase_len);
1195 if (!backup_contents || !erased_contents) {
1196 msg_cerr("Out of memory!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001197 goto _free_ret;
1198 }
1199 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1200 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1201
1202 msg_cdbg("R");
1203 /* Merge data preceding the current region. */
1204 if (info->region_start > info->erase_start) {
1205 const chipoff_t start = info->erase_start;
1206 const chipsize_t len = info->region_start - info->erase_start;
1207 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1208 msg_cerr("Can't read! Aborting.\n");
1209 goto _free_ret;
1210 }
1211 }
1212 /* Merge data following the current region. */
1213 if (info->erase_end > info->region_end) {
1214 const chipoff_t start = info->region_end + 1;
1215 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1216 const chipsize_t len = info->erase_end - info->region_end;
1217 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1218 msg_cerr("Can't read! Aborting.\n");
1219 goto _free_ret;
1220 }
1221 }
1222 }
1223
Nico Huber7af0e792016-04-29 16:40:15 +02001224 all_skipped = false;
1225
1226 msg_cdbg("E");
1227 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001228 goto _free_ret;
Richard Hughes842d6782021-01-15 09:48:12 +00001229 flashprog_progress_add(flashctx, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001230 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1231 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001232 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001233 }
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001234 if (info->curcontents)
1235 memset(info->curcontents + info->erase_start, ERASED_VALUE(flashctx), erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001236
1237 if (region_unaligned) {
Nico Huber3b9c86d2023-01-15 22:58:06 +01001238 if (write_range(flashctx, info->erase_start, erased_contents, backup_contents, erase_len, NULL))
1239 goto _free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001240 if (info->curcontents)
1241 memcpy(info->curcontents + info->erase_start, backup_contents, erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001242 }
1243
1244 ret = 0;
1245
1246_free_ret:
1247 free(erased_contents);
1248 free(backup_contents);
1249 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001250}
1251
1252/**
1253 * @brief Erases the included layout regions.
1254 *
1255 * If there is no layout set in the given flash context, the whole chip will
1256 * be erased.
1257 *
1258 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001259 * @return 0 on success,
1260 * 1 if all available erase functions failed.
1261 */
Nico Huber454f6132012-12-10 13:34:10 +00001262static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001263{
1264 struct walk_info info = { 0 };
1265 return walk_by_layout(flashctx, &info, &erase_block);
1266}
1267
Nico Huber7af0e792016-04-29 16:40:15 +02001268/**
1269 * @brief Writes the included layout regions from a given image.
1270 *
1271 * If there is no layout set in the given flash context, the whole image
1272 * will be written.
1273 *
1274 * @param flashctx Flash context to be used.
1275 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1276 * @param newcontents The new image to be written.
1277 * @return 0 on success,
1278 * 1 if anything has gone wrong.
1279 */
Nico Huber454f6132012-12-10 13:34:10 +00001280static int write_by_layout(struct flashctx *const flashctx,
1281 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001282{
1283 struct walk_info info;
1284 info.curcontents = curcontents;
1285 info.newcontents = newcontents;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001286 return walk_by_layout(flashctx, &info, erase_block);
Nico Huber7af0e792016-04-29 16:40:15 +02001287}
1288
1289/**
1290 * @brief Compares the included layout regions with content from a buffer.
1291 *
1292 * If there is no layout set in the given flash context, the whole chip's
1293 * contents will be compared.
1294 *
1295 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001296 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001297 * @param curcontents A buffer of full chip size to read current chip contents into.
1298 * @param newcontents The new image to compare to.
1299 * @return 0 on success,
1300 * 1 if reading failed,
1301 * 3 if the contents don't match.
1302 */
Nico Huber74d09d42019-06-16 03:27:26 +02001303static int verify_by_layout(
1304 struct flashctx *const flashctx,
Nico Huberc3b02dc2023-08-12 01:13:45 +02001305 const struct flashprog_layout *const layout,
Nico Huber74d09d42019-06-16 03:27:26 +02001306 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001307{
Nico Huber5ca55232019-06-15 22:29:08 +02001308 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001309
Richard Hughes842d6782021-01-15 09:48:12 +00001310 flashprog_progress_start_by_layout(flashctx, FLASHPROG_PROGRESS_READ, layout);
1311
Nico Huber5ca55232019-06-15 22:29:08 +02001312 while ((entry = layout_next_included(layout, entry))) {
1313 const chipoff_t region_start = entry->start;
1314 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001315
1316 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1317 return 1;
1318 if (compare_range(newcontents + region_start, curcontents + region_start,
1319 region_start, region_len))
1320 return 3;
1321 }
Richard Hughes842d6782021-01-15 09:48:12 +00001322
1323 flashprog_progress_finish(flashctx);
1324
Nico Huber7af0e792016-04-29 16:40:15 +02001325 return 0;
1326}
1327
Stefan Tauner136388f2013-07-15 10:47:53 +00001328static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001329{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001330 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001331#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001332 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001333 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001334 "chip. Please report this to the mailing list at flashprog@flashprog.org or\n"
1335 "on IRC (see https://www.flashprog.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001336 "-------------------------------------------------------------------------------\n"
1337 "You may now reboot or simply leave the machine running.\n");
1338 else
1339#endif
1340 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001341 "the programmer and the flash chip. If you think the error is caused by flashprog\n"
1342 "please report this to the mailing list at flashprog@flashprog.org or on IRC\n"
1343 "(see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001344}
1345
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001346void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001347{
Stefan Tauner136388f2013-07-15 10:47:53 +00001348 msg_gerr("Your flash chip is in an unknown state.\n");
1349#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001350 if (programmer == &programmer_internal)
Nico Huberc3b02dc2023-08-12 01:13:45 +02001351 msg_gerr("Get help on IRC (see https://www.flashprog.org/Contact) or mail\n"
1352 "flashprog@flashprog.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001353 "-------------------------------------------------------------------------------\n"
1354 "DO NOT REBOOT OR POWEROFF!\n");
1355 else
1356#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +02001357 msg_gerr("Please report this to the mailing list at flashprog@flashprog.org\n"
1358 "or on IRC (see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001359}
1360
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001361void list_programmers_linebreak(int startcol, int cols, int paren)
1362{
1363 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001364 int pnamelen;
1365 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001366 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001367 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001368
Thomas Heijligend45cb592021-05-19 14:12:18 +02001369 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001370 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001371 pnamelen = strlen(pname);
1372 if (remaining - pnamelen - 2 < 0) {
1373 if (firstline)
1374 firstline = 0;
1375 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001376 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001377 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001378 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001379 remaining = cols - startcol;
1380 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001381 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001382 remaining--;
1383 }
1384 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001385 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001386 remaining--;
1387 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001388 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001389 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001390 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001391 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001392 remaining--;
1393 } else {
1394 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001395 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001396 }
1397 }
1398}
1399
Nico Huber32fa5082026-01-26 18:26:49 +01001400int selfcheck_chip(const struct flashchip *const chip, const int idx)
1401{
1402 int ret = 0;
Nico Huber72d0ada2026-01-26 18:28:20 +01001403 char label[80];
Nico Huber32fa5082026-01-26 18:26:49 +01001404 const char *const name = chip->name != NULL ? chip->name : "unnamed";
1405
Nico Huber72d0ada2026-01-26 18:28:20 +01001406 if (idx >= 0)
1407 snprintf(label, sizeof(label), "#%d (%s)", idx, name);
1408 else
1409 snprintf(label, sizeof(label), "%s", name);
1410
Nico Huber11136c22023-05-01 12:00:09 +02001411 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE ||
1412 chip->id.type == ID_FIXME) {
Nico Huber32fa5082026-01-26 18:26:49 +01001413 ret = 1;
Nico Huber72d0ada2026-01-26 18:28:20 +01001414 msg_gerr("ERROR: Some field of flash chip %s is misconfigured.\n"
1415 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001416 }
1417 if (chip->feature_bits &
1418 (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7 |
1419 FEATURE_ANY_DUAL | FEATURE_ANY_QUAD)
1420 && !chip->prepare_access) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001421 msg_gerr("ERROR: Flash chip %s misses chip\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001422 "preparation function for 4BA and multi-i/o modes.\n"
Nico Huber72d0ada2026-01-26 18:28:20 +01001423 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001424 ret = 1;
1425 }
1426 uint8_t zero_cycles[sizeof(chip->dummy_cycles)] = { 0 };
1427 if ((chip->feature_bits & (FEATURE_QPI_35_F5 | FEATURE_QPI_38_FF)) &&
1428 !memcmp(&chip->dummy_cycles, zero_cycles, sizeof(zero_cycles))) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001429 msg_gerr("ERROR: Flash chip %s misses QPI dummy-cycle\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001430 "settings. Please report a bug at flashprog@flashprog.org\n",
Nico Huber72d0ada2026-01-26 18:28:20 +01001431 label);
Nico Huber32fa5082026-01-26 18:26:49 +01001432 ret = 1;
1433 }
1434 if (chip->reg_bits.bp[0].reg != INVALID_REG &&
1435 (!chip->wp_write_cfg || !chip->wp_read_cfg ||
1436 !chip->wp_get_ranges || !chip->decode_range)) {
Nico Huber72d0ada2026-01-26 18:28:20 +01001437 msg_gerr("ERROR: Flash chip %s advertises block-protection\n"
Nico Huber32fa5082026-01-26 18:26:49 +01001438 "bits, but misses one or more write-protection functions.\n"
Nico Huber72d0ada2026-01-26 18:28:20 +01001439 "Please report a bug at flashprog@flashprog.org\n", label);
Nico Huber32fa5082026-01-26 18:26:49 +01001440 ret = 1;
1441 }
Nico Huber72d0ada2026-01-26 18:28:20 +01001442 if (selfcheck_eraseblocks(chip, label)) {
Nico Huber32fa5082026-01-26 18:26:49 +01001443 ret = 1;
1444 }
1445
1446 return ret;
1447}
1448
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001449int selfcheck(void)
1450{
Stefan Tauner96658be2014-05-26 22:05:31 +00001451 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001452 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001453
Thomas Heijligend45cb592021-05-19 14:12:18 +02001454 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001455 const struct programmer_entry *const p = programmer_table[i];
1456 if (p == NULL) {
1457 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1458 ret = 1;
1459 continue;
1460 }
1461 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001462 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1463 ret = 1;
1464 /* This might hide other problems with this programmer, but allows for better error
1465 * messages below without jumping through hoops. */
1466 continue;
1467 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001468 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001469 case USB:
1470 case PCI:
1471 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001472 if (p->devs.note == NULL) {
1473 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001474 break; /* This one has its device list stored separately. */
1475 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001476 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001477 ret = 1;
1478 }
1479 break;
1480 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001481 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001482 ret = 1;
1483 break;
1484 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001485 if (p->init == NULL) {
1486 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001487 ret = 1;
1488 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001489 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001490
1491 /* It would be favorable if we could check for the correct layout (especially termination) of various
1492 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1493 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1494 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1495 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1496 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001497 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001498 msg_gerr("Flashchips table miscompilation!\n");
1499 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001500 } else {
1501 for (i = 0; i < flashchips_size - 1; i++) {
Nico Huber32fa5082026-01-26 18:26:49 +01001502 if (selfcheck_chip(&flashchips[i], i))
Stefan Tauner96658be2014-05-26 22:05:31 +00001503 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001504 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001505 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001506
Stefan Tauner600576b2014-06-12 22:57:36 +00001507#if CONFIG_INTERNAL == 1
1508 ret |= selfcheck_board_enables();
1509#endif
1510
Stefan Tauner96658be2014-05-26 22:05:31 +00001511 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001512 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001513}
1514
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001515/* FIXME: This function signature needs to be improved once prepare_flash_access()
1516 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001517 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001518static int chip_safety_check(const struct flashctx *flash, int force,
1519 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001520{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001521 const struct flashchip *chip = flash->chip;
1522
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001523 if (!programmer_may_write && (write_it || erase_it)) {
1524 msg_perr("Write/erase is not working yet on your programmer in "
1525 "its current configuration.\n");
1526 /* --force is the wrong approach, but it's the best we can do
1527 * until the generic programmer parameter parser is merged.
1528 */
1529 if (!force)
1530 return 1;
1531 msg_cerr("Continuing anyway.\n");
1532 }
1533
1534 if (read_it || erase_it || write_it || verify_it) {
1535 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001536 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001537 msg_cerr("Read is not working on this chip. ");
1538 if (!force)
1539 return 1;
1540 msg_cerr("Continuing anyway.\n");
1541 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001542 if (!chip->read) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001543 msg_cerr("flashprog has no read function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001544 "flash chip.\n");
1545 return 1;
1546 }
1547 }
1548 if (erase_it || write_it) {
1549 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001550 if (chip->tested.erase == NA) {
1551 msg_cerr("Erase is not possible on this chip.\n");
1552 return 1;
1553 }
1554 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001555 msg_cerr("Erase is not working on this chip. ");
1556 if (!force)
1557 return 1;
1558 msg_cerr("Continuing anyway.\n");
1559 }
Nico Huberbb8ab372026-03-14 11:46:02 +01001560 if(flashprog_count_usable_erasers(flash) == 0) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001561 msg_cerr("flashprog has no erase function for this "
Stefan Tauner5368dca2011-07-01 00:19:12 +00001562 "flash chip.\n");
1563 return 1;
1564 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001565 }
1566 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001567 if (chip->tested.write == NA) {
1568 msg_cerr("Write is not possible on this chip.\n");
1569 return 1;
1570 }
1571 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001572 msg_cerr("Write is not working on this chip. ");
1573 if (!force)
1574 return 1;
1575 msg_cerr("Continuing anyway.\n");
1576 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001577 if (!chip->write) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001578 msg_cerr("flashprog has no write function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001579 "flash chip.\n");
1580 return 1;
1581 }
1582 }
1583 return 0;
1584}
1585
Nico Huber305f4172013-06-14 11:55:26 +02001586int prepare_flash_access(struct flashctx *const flash,
1587 const bool read_it, const bool write_it,
1588 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001589{
1590 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1591 msg_cerr("Aborting.\n");
1592 return 1;
1593 }
1594
Nico Huber3ac761c2023-01-16 02:43:17 +01001595 if (layout_sanity_checks(flash, write_it)) {
Nico Huber454f6132012-12-10 13:34:10 +00001596 msg_cerr("Requested regions can not be handled. Aborting.\n");
1597 return 1;
1598 }
1599
Nico Huber901fb952023-01-11 23:24:23 +01001600 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_FULL))
1601 return 1;
1602
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001603 /* Initialize chip_restore_fn_count before chip unlock calls. */
1604 flash->chip_restore_fn_count = 0;
1605
Nico Huber454f6132012-12-10 13:34:10 +00001606 /* Given the existence of read locks, we want to unlock for read,
1607 erase and write. */
1608 if (flash->chip->unlock)
1609 flash->chip->unlock(flash);
1610
1611 return 0;
1612}
1613
Nico Huber305f4172013-06-14 11:55:26 +02001614void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001615{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001616 deregister_chip_restore(flash);
Nico Huber901fb952023-01-11 23:24:23 +01001617 if (flash->chip->finish_access)
1618 flash->chip->finish_access(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001619}
1620
1621/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001622 * @addtogroup flashprog-flash
Nico Huber454f6132012-12-10 13:34:10 +00001623 * @{
1624 */
1625
1626/**
1627 * @brief Erase the specified ROM chip.
1628 *
1629 * If a layout is set in the given flash context, only included regions
1630 * will be erased.
1631 *
1632 * @param flashctx The context of the flash chip to erase.
1633 * @return 0 on success.
1634 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001635int flashprog_flash_erase(struct flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +00001636{
1637 if (prepare_flash_access(flashctx, false, false, true, false))
1638 return 1;
1639
1640 const int ret = erase_by_layout(flashctx);
1641
1642 finalize_flash_access(flashctx);
1643
1644 return ret;
1645}
1646
Nico Huberc3b02dc2023-08-12 01:13:45 +02001647/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +00001648
1649/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001650 * @defgroup flashprog-ops Operations
Nico Huber454f6132012-12-10 13:34:10 +00001651 * @{
1652 */
1653
1654/**
1655 * @brief Read the current image from the specified ROM chip.
1656 *
1657 * If a layout is set in the specified flash context, only included regions
1658 * will be read.
1659 *
1660 * @param flashctx The context of the flash chip.
1661 * @param buffer Target buffer to write image to.
1662 * @param buffer_len Size of target buffer in bytes.
1663 * @return 0 on success,
1664 * 2 if buffer_len is too short for the flash chip's contents,
1665 * or 1 on any other failure.
1666 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001667int flashprog_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001668{
1669 const size_t flash_size = flashctx->chip->total_size * 1024;
1670
1671 if (flash_size > buffer_len)
1672 return 2;
1673
1674 if (prepare_flash_access(flashctx, true, false, false, false))
1675 return 1;
1676
1677 msg_cinfo("Reading flash... ");
1678
1679 int ret = 1;
1680 if (read_by_layout(flashctx, buffer)) {
1681 msg_cerr("Read operation failed!\n");
1682 msg_cinfo("FAILED.\n");
1683 goto _finalize_ret;
1684 }
1685 msg_cinfo("done.\n");
1686 ret = 0;
1687
1688_finalize_ret:
1689 finalize_flash_access(flashctx);
1690 return ret;
1691}
1692
1693static void combine_image_by_layout(const struct flashctx *const flashctx,
1694 uint8_t *const newcontents, const uint8_t *const oldcontents)
1695{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001696 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001697 const struct romentry *included;
1698 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001699
Nico Huber3d7b1e32018-12-22 00:53:14 +01001700 while ((included = layout_next_included_region(layout, start))) {
1701 if (included->start > start) {
1702 /* copy everything up to the start of this included region */
1703 memcpy(newcontents + start, oldcontents + start, included->start - start);
1704 }
1705 /* skip this included region */
1706 start = included->end + 1;
1707 if (start == 0)
1708 return;
Nico Huber454f6132012-12-10 13:34:10 +00001709 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001710
1711 /* copy the rest of the chip */
1712 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1713 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001714}
1715
1716/**
1717 * @brief Write the specified image to the ROM chip.
1718 *
1719 * If a layout is set in the specified flash context, only erase blocks
1720 * containing included regions will be touched.
1721 *
1722 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001723 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001724 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001725 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001726 * @return 0 on success,
1727 * 4 if buffer_len doesn't match the size of the flash chip,
1728 * 3 if write was tried but nothing has changed,
1729 * 2 if write failed and flash contents changed,
1730 * or 1 on any other failure.
1731 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001732int flashprog_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001733 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001734{
1735 const size_t flash_size = flashctx->chip->total_size * 1024;
1736 const bool verify_all = flashctx->flags.verify_whole_chip;
1737 const bool verify = flashctx->flags.verify_after_write;
Nico Huberc3b02dc2023-08-12 01:13:45 +02001738 const struct flashprog_layout *const verify_layout =
Nico Huber74d09d42019-06-16 03:27:26 +02001739 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001740
1741 if (buffer_len != flash_size)
1742 return 4;
1743
1744 int ret = 1;
1745
1746 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001747 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001748 uint8_t *const curcontents = malloc(flash_size);
1749 uint8_t *oldcontents = NULL;
1750 if (verify_all)
1751 oldcontents = malloc(flash_size);
1752 if (!curcontents || (verify_all && !oldcontents)) {
1753 msg_gerr("Out of memory!\n");
1754 goto _free_ret;
1755 }
1756
1757#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001758 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001759 if (flashctx->flags.force_boardmismatch) {
1760 msg_pinfo("Proceeding anyway because user forced us to.\n");
1761 } else {
1762 msg_perr("Aborting. You can override this with "
1763 "-p internal:boardmismatch=force.\n");
1764 goto _free_ret;
1765 }
1766 }
1767#endif
1768
1769 if (prepare_flash_access(flashctx, false, true, false, verify))
1770 goto _free_ret;
1771
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001772 /* If given, assume flash chip contains same data as `refcontents`. */
1773 if (refcontents) {
1774 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1775 memcpy(curcontents, refcontents, flash_size);
1776 if (oldcontents)
1777 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001778 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001779 /*
1780 * Read the whole chip to be able to check whether regions need to be
1781 * erased and to give better diagnostics in case write fails.
1782 * The alternative is to read only the regions which are to be
1783 * preserved, but in that case we might perform unneeded erase which
1784 * takes time as well.
1785 */
1786 msg_cinfo("Reading old flash chip contents... ");
1787 if (verify_all) {
Richard Hughes842d6782021-01-15 09:48:12 +00001788 if (flashprog_read_range(flashctx, oldcontents, 0, flash_size)) {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001789 msg_cinfo("FAILED.\n");
1790 goto _finalize_ret;
1791 }
1792 memcpy(curcontents, oldcontents, flash_size);
1793 } else {
1794 if (read_by_layout(flashctx, curcontents)) {
1795 msg_cinfo("FAILED.\n");
1796 goto _finalize_ret;
1797 }
Nico Huber454f6132012-12-10 13:34:10 +00001798 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001799 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001800 }
Nico Huber454f6132012-12-10 13:34:10 +00001801
1802 if (write_by_layout(flashctx, curcontents, newcontents)) {
1803 msg_cerr("Uh oh. Erase/write failed. ");
1804 ret = 2;
1805 if (verify_all) {
1806 msg_cerr("Checking if anything has changed.\n");
1807 msg_cinfo("Reading current flash chip contents... ");
Richard Hughes842d6782021-01-15 09:48:12 +00001808 if (!flashprog_read_range(flashctx, curcontents, 0, flash_size)) {
Nico Huber454f6132012-12-10 13:34:10 +00001809 msg_cinfo("done.\n");
1810 if (!memcmp(oldcontents, curcontents, flash_size)) {
1811 nonfatal_help_message();
1812 goto _finalize_ret;
1813 }
1814 msg_cerr("Apparently at least some data has changed.\n");
1815 } else
1816 msg_cerr("Can't even read anymore!\n");
1817 emergency_help_message();
1818 goto _finalize_ret;
1819 } else {
1820 msg_cerr("\n");
1821 }
1822 emergency_help_message();
1823 goto _finalize_ret;
1824 }
1825
1826 /* Verify only if we actually changed something. */
1827 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001828 msg_cinfo("Verifying flash... ");
1829
Nico Huber74d09d42019-06-16 03:27:26 +02001830 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001831 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001832 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001833 /* If we tried to write, and verification now fails, we
1834 might have an emergency situation. */
1835 if (ret)
1836 emergency_help_message();
1837 else
1838 msg_cinfo("VERIFIED.\n");
1839 } else {
1840 /* We didn't change anything. */
1841 ret = 0;
1842 }
1843
1844_finalize_ret:
1845 finalize_flash_access(flashctx);
1846_free_ret:
1847 free(oldcontents);
1848 free(curcontents);
1849 return ret;
1850}
1851
1852/**
1853 * @brief Verify the ROM chip's contents with the specified image.
1854 *
1855 * If a layout is set in the specified flash context, only included regions
1856 * will be verified.
1857 *
1858 * @param flashctx The context of the flash chip.
1859 * @param buffer Source buffer to verify with.
1860 * @param buffer_len Size of source buffer in bytes.
1861 * @return 0 on success,
1862 * 3 if the chip's contents don't match,
1863 * 2 if buffer_len doesn't match the size of the flash chip,
1864 * or 1 on any other failure.
1865 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001866int flashprog_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001867{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001868 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001869 const size_t flash_size = flashctx->chip->total_size * 1024;
1870
1871 if (buffer_len != flash_size)
1872 return 2;
1873
1874 const uint8_t *const newcontents = buffer;
1875 uint8_t *const curcontents = malloc(flash_size);
1876 if (!curcontents) {
1877 msg_gerr("Out of memory!\n");
1878 return 1;
1879 }
1880
1881 int ret = 1;
1882
1883 if (prepare_flash_access(flashctx, false, false, false, true))
1884 goto _free_ret;
1885
1886 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001887 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001888 if (!ret)
1889 msg_cinfo("VERIFIED.\n");
1890
1891 finalize_flash_access(flashctx);
1892_free_ret:
1893 free(curcontents);
1894 return ret;
1895}
1896
Nico Huberc3b02dc2023-08-12 01:13:45 +02001897/** @} */ /* end flashprog-ops */