blob: e8ab305dfc7eef2ae33928c2f7be990c6aa068fb [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Nico Huber7af0e792016-04-29 16:40:15 +02008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000010 *
Uwe Hermannd1107642007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000015 *
Uwe Hermannd1107642007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000020 */
21
Felix Singerf25447e2022-08-19 02:44:28 +020022#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000023#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000024#include <sys/types.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000025#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000026#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000027#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000028#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000029#include <ctype.h>
Edward O'Callaghan3b64d812022-08-12 13:07:51 +100030
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000031#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000032#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000033#include "programmer.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010034#include "hwaccess_physmap.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010035#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000036
Nico Huberc3b02dc2023-08-12 01:13:45 +020037const char flashprog_version[] = FLASHPROG_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000038const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000039
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020040static const struct programmer_entry *programmer = NULL;
Nico Huber6a2ebeb2022-08-26 11:36:48 +020041static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000042
Uwe Hermann48ec1b12010-08-08 17:01:18 +000043/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000044 * Programmers supporting multiple buses can have differing size limits on
45 * each bus. Store the limits for each bus in a common struct.
46 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000047struct decode_sizes max_rom_decode;
48
49/* If nonzero, used as the start address of bottom-aligned flash. */
50unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000051
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000052/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020053bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000054
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000055#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000056static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000057/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000058static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000059 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000060 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000061} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000062/* Initialize to 0 to make sure nobody registers a shutdown function before
63 * programmer init.
64 */
Felix Singerf25447e2022-08-19 02:44:28 +020065static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000066
Stefan Taunerc4f44df2013-08-12 22:58:43 +000067/* Did we change something or was every erase/write skipped (if any)? */
68static bool all_skipped = true;
69
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000070static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000071
Stefan Tauner2a1ed772014-08-31 00:09:21 +000072int shutdown_free(void *data)
73{
74 free(data);
75 return 0;
76}
77
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000078/* Register a function to be executed on programmer shutdown.
79 * The advantage over atexit() is that you can supply a void pointer which will
80 * be used as parameter to the registered function upon programmer shutdown.
81 * This pointer can point to arbitrary data used by said function, e.g. undo
82 * information for GPIO settings etc. If unneeded, set data=NULL.
83 * Please note that the first (void *data) belongs to the function signature of
84 * the function passed as first parameter.
85 */
David Hendricks8bb20212011-06-14 01:35:36 +000086int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000087{
88 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000089 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000090 SHUTDOWN_MAXFN);
91 return 1;
92 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000093 if (!may_register_shutdown) {
94 msg_perr("Tried to register a shutdown function before "
95 "programmer init.\n");
96 return 1;
97 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000098 shutdown_fn[shutdown_fn_count].func = function;
99 shutdown_fn[shutdown_fn_count].data = data;
100 shutdown_fn_count++;
101
102 return 0;
103}
104
Nikolai Artemiev4ad48642020-11-05 13:54:27 +1100105int register_chip_restore(chip_restore_fn_cb_t func,
106 struct flashctx *flash, uint8_t status)
107{
108 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
109 msg_perr("Tried to register more than %i chip restore"
110 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
111 return 1;
112 }
113 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
114 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
115 flash->chip_restore_fn_count++;
116
117 return 0;
118}
119
120static int deregister_chip_restore(struct flashctx *flash)
121{
122 int rc = 0;
123
124 while (flash->chip_restore_fn_count > 0) {
125 int i = --flash->chip_restore_fn_count;
126 rc |= flash->chip_restore_fn[i].func(
127 flash, flash->chip_restore_fn[i].status);
128 }
129
130 return rc;
131}
132
Nico Huber2b66ad92023-01-11 20:15:15 +0100133int programmer_init(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000134{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000135 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000136
Nico Huber2b66ad92023-01-11 20:15:15 +0100137 if (prog == NULL || prog->driver == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000138 msg_perr("Invalid programmer specified!\n");
139 return -1;
140 }
Nico Huber2b66ad92023-01-11 20:15:15 +0100141 programmer = prog->driver;
142 programmer_param = prog->param;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000143 /* Initialize all programmer specific data. */
144 /* Default to unlimited decode sizes. */
145 max_rom_decode = (const struct decode_sizes) {
146 .parallel = 0xffffffff,
147 .lpc = 0xffffffff,
148 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000149 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000150 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000151 /* Default to top aligned flash at 4 GB. */
152 flashbase = 0;
153 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200154 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000155 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200156 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000157
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200158 msg_pdbg("Initializing %s programmer\n", programmer->name);
159 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000160 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000161 if (ret != 0) {
162 /* It is quite possible that any unhandled programmer parameter would have been valid,
163 * but an error in actual programmer init happened before the parameter was evaluated.
164 */
165 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
166 programmer_param);
167 } else {
168 /* Actual programmer init was successful, but the user specified an invalid or unusable
169 * (for the current programmer configuration) parameter.
170 */
171 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
172 msg_perr("Aborting.\n");
173 ret = ERROR_FATAL;
174 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000175 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200176 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000177 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000178}
179
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000180/** Calls registered shutdown functions and resets internal programmer-related variables.
181 * Calling it is safe even without previous initialization, but further interactions with programmer support
182 * require a call to programmer_init() (afterwards).
183 *
184 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Nico Huber2b66ad92023-01-11 20:15:15 +0100185int programmer_shutdown(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000186{
David Hendricks8bb20212011-06-14 01:35:36 +0000187 int ret = 0;
188
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000189 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200190 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000191 while (shutdown_fn_count > 0) {
192 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000193 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000194 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000195 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000196
David Hendricks8bb20212011-06-14 01:35:36 +0000197 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000198}
199
Stefan Taunerf80419c2014-05-02 15:41:42 +0000200void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000201{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000202 if (usecs > 0) {
203 if (programmer->delay)
204 programmer->delay(usecs);
205 else
206 internal_delay(usecs);
207 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000208}
209
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000210int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
211 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000212{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000213 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000214
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000215 return 0;
216}
217
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000218/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000219 * It will look for needle with a subsequent '=' in haystack, return a copy of
220 * needle and remove everything from the first occurrence of needle to the next
221 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000222 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200223static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000224{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000225 char *param_pos, *opt_pos, *rest;
226 char *opt = NULL;
227 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000228 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000229
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000230 needlelen = strlen(needle);
231 if (!needlelen) {
232 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200233 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000234 return NULL;
235 }
236 /* No programmer parameters given. */
237 if (*haystack == NULL)
238 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000239 param_pos = strstr(*haystack, needle);
240 do {
241 if (!param_pos)
242 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000243 /* Needle followed by '='? */
244 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000245 /* Beginning of the string? */
246 if (param_pos == *haystack)
247 break;
248 /* After a delimiter? */
249 if (strchr(delim, *(param_pos - 1)))
250 break;
251 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000252 /* Continue searching. */
253 param_pos++;
254 param_pos = strstr(param_pos, needle);
255 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000256
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000257 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000258 /* Get the string after needle and '='. */
259 opt_pos = param_pos + needlelen + 1;
260 optlen = strcspn(opt_pos, delim);
261 /* Return an empty string if the parameter was empty. */
262 opt = malloc(optlen + 1);
263 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000264 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000265 exit(1);
266 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000267 strncpy(opt, opt_pos, optlen);
268 opt[optlen] = '\0';
269 rest = opt_pos + optlen;
270 /* Skip all delimiters after the current parameter. */
271 rest += strspn(rest, delim);
272 memmove(param_pos, rest, strlen(rest) + 1);
273 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000274 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000275
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000276 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000277}
278
Stefan Tauner66652442011-06-26 17:38:17 +0000279char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000280{
281 return extract_param(&programmer_param, param_name, ",");
282}
283
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000284/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000285static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000286{
287 unsigned int usable_erasefunctions = 0;
288 int k;
289 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
290 if (!check_block_eraser(flash, k, 0))
291 usable_erasefunctions++;
292 }
293 return usable_erasefunctions;
294}
295
Mark Marshallf20b7be2014-05-09 21:16:21 +0000296static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000297{
298 int ret = 0, failcount = 0;
299 unsigned int i;
300 for (i = 0; i < len; i++) {
301 if (wantbuf[i] != havebuf[i]) {
302 /* Only print the first failure. */
303 if (!failcount++)
304 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
305 start + i, wantbuf[i], havebuf[i]);
306 }
307 }
308 if (failcount) {
309 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
310 start, start + len - 1, failcount);
311 ret = -1;
312 }
313 return ret;
314}
315
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000316/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600317static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000318{
319 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300320 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000321
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100322 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000323 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100324 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100325 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000326 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300327 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000328 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100329
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000330 free(cmpbuf);
331 return ret;
332}
333
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000334/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000335 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000336 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000337 * @start offset to the base address of the flash chip
338 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000339 * @return 0 for success, -1 for failure
340 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000341int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000342{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000343 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000344 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000345
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100346 if (start + len > flash->chip->total_size * 1024) {
347 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
348 " total_size 0x%x\n", __func__, start, len,
349 flash->chip->total_size * 1024);
350 return -1;
351 }
352
Stefan Taunerdf64a422014-05-27 00:06:14 +0000353 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000354 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100355 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000356 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000357 }
358
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100359 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000360 if (ret) {
361 msg_gerr("Verification impossible because read failed "
362 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000363 ret = -1;
364 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000365 }
366
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000367 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000368out_free:
369 free(readbuf);
370 return ret;
371}
372
Nico Huber3ac761c2023-01-16 02:43:17 +0100373size_t gran_to_bytes(const enum write_granularity gran)
Nico Huberb77607f2023-01-16 02:25:45 +0100374{
375 switch (gran) {
376 case write_gran_1bit: return 1;
377 case write_gran_1byte: return 1;
378 case write_gran_1byte_implicit_erase: return 1;
379 case write_gran_128bytes: return 128;
380 case write_gran_256bytes: return 256;
381 case write_gran_264bytes: return 264;
382 case write_gran_512bytes: return 512;
383 case write_gran_528bytes: return 528;
384 case write_gran_1024bytes: return 1024;
385 case write_gran_1056bytes: return 1056;
386 default: return 0;
387 }
388}
389
Stefan Tauner02437452013-04-01 19:34:53 +0000390/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300391static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
392 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000393{
394 unsigned int i, j, limit;
395 for (j = 0; j < len / gran; j++) {
396 limit = min (gran, len - j * gran);
397 /* Are 'have' and 'want' identical? */
398 if (!memcmp(have + j * gran, want + j * gran, limit))
399 continue;
400 /* have needs to be in erased state. */
401 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300402 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000403 return 1;
404 }
405 return 0;
406}
407
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000408/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000409 * Check if the buffer @have can be programmed to the content of @want without
410 * erasing. This is only possible if all chunks of size @gran are either kept
411 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000412 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000413 * Warning: This function assumes that @have and @want point to naturally
414 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000415 *
416 * @have buffer with current content
417 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000418 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000419 * @gran write granularity (enum, not count)
420 * @return 0 if no erase is needed, 1 otherwise
421 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000422static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
423 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000424{
Stefan Tauner02437452013-04-01 19:34:53 +0000425 unsigned int i;
Nico Huberb77607f2023-01-16 02:25:45 +0100426 size_t stride;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000427
428 switch (gran) {
429 case write_gran_1bit:
Nico Huberb77607f2023-01-16 02:25:45 +0100430 for (i = 0; i < len; i++) {
431 if ((have[i] & want[i]) != want[i])
432 return 1;
433 }
434 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000435 case write_gran_1byte:
Nico Huberb77607f2023-01-16 02:25:45 +0100436 for (i = 0; i < len; i++) {
437 if ((have[i] != want[i]) && (have[i] != erased_value))
438 return 1;
439 }
440 return 0;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000441 case write_gran_1byte_implicit_erase:
442 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
Nico Huberb77607f2023-01-16 02:25:45 +0100443 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000444 default:
Nico Huberb77607f2023-01-16 02:25:45 +0100445 stride = gran_to_bytes(gran);
446 if (stride) {
447 return need_erase_gran_bytes(have, want, len, stride, erased_value);
448 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000449 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200450 "flashprog@flashprog.org\n", __func__);
Nico Huberb77607f2023-01-16 02:25:45 +0100451 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000452 }
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000453}
454
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000455/**
456 * Check if the buffer @have needs to be programmed to get the content of @want.
457 * If yes, return 1 and fill in first_start with the start address of the
458 * write operation and first_len with the length of the first to-be-written
459 * chunk. If not, return 0 and leave first_start and first_len undefined.
460 *
461 * Warning: This function assumes that @have and @want point to naturally
462 * aligned regions.
463 *
464 * @have buffer with current content
465 * @want buffer with desired content
466 * @len length of the checked area
467 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000468 * @first_start offset of the first byte which needs to be written (passed in
469 * value is increased by the offset of the first needed write
470 * relative to have/want or unchanged if no write is needed)
471 * @return length of the first contiguous area which needs to be written
472 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000473 *
474 * FIXME: This function needs a parameter which tells it about coalescing
475 * in relation to the max write length of the programmer and the max write
476 * length of the chip.
477 */
Nico Huber3b9c86d2023-01-15 22:58:06 +0100478static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, chipsize_t len,
479 chipoff_t *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000480{
Felix Singerf25447e2022-08-19 02:44:28 +0200481 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000482 unsigned int rel_start = 0, first_len = 0;
Nico Huberb77607f2023-01-16 02:25:45 +0100483 unsigned int i, limit;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000484
Nico Huberb77607f2023-01-16 02:25:45 +0100485 const size_t stride = gran_to_bytes(gran);
486 if (!stride) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000487 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200488 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000489 /* Claim that no write was needed. A write with unknown
490 * granularity is too dangerous to try.
491 */
492 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000493 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000494 for (i = 0; i < len / stride; i++) {
495 limit = min(stride, len - i * stride);
496 /* Are 'have' and 'want' identical? */
497 if (memcmp(have + i * stride, want + i * stride, limit)) {
498 if (!need_write) {
499 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200500 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000501 rel_start = i * stride;
502 }
503 } else {
504 if (need_write) {
505 /* First location where have and want
506 * do not differ anymore.
507 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000508 break;
509 }
510 }
511 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000512 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000513 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000514 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000515 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000516}
517
Nico Huber2d625722016-05-03 10:48:02 +0200518/*
519 * Return a string corresponding to the bustype parameter.
520 * Memory is obtained with malloc() and must be freed with free() by the caller.
521 */
522char *flashbuses_to_text(enum chipbustype bustype)
523{
524 char *ret = calloc(1, 1);
525 /*
526 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
527 * will cease to exist and should be eliminated here as well.
528 */
529 if (bustype == BUS_NONSPI) {
530 ret = strcat_realloc(ret, "Non-SPI, ");
531 } else {
532 if (bustype & BUS_PARALLEL)
533 ret = strcat_realloc(ret, "Parallel, ");
534 if (bustype & BUS_LPC)
535 ret = strcat_realloc(ret, "LPC, ");
536 if (bustype & BUS_FWH)
537 ret = strcat_realloc(ret, "FWH, ");
538 if (bustype & BUS_SPI)
539 ret = strcat_realloc(ret, "SPI, ");
540 if (bustype & BUS_PROG)
541 ret = strcat_realloc(ret, "Programmer-specific, ");
542 if (bustype == BUS_NONE)
543 ret = strcat_realloc(ret, "None, ");
544 }
545 /* Kill last comma. */
546 ret[strlen(ret) - 2] = '\0';
547 ret = realloc(ret, strlen(ret) + 1);
548 return ret;
549}
550
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100551static int init_default_layout(struct flashctx *flash)
552{
553 /* Fill default layout covering the whole chip. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200554 if (flashprog_layout_new(&flash->default_layout) ||
555 flashprog_layout_add_region(flash->default_layout,
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100556 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huberc3b02dc2023-08-12 01:13:45 +0200557 flashprog_layout_include_region(flash->default_layout, "complete flash"))
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100558 return -1;
559 return 0;
560}
561
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000562int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000563{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000564 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000565 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000566 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000567
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000568 for (chip = flashchips + startchip; chip && chip->name; chip++) {
569 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000570 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000571 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000572 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000573 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300574 /* Only probe for SPI25 chips by default. */
575 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
576 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000577 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
578 if (!chip->probe && !force) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200579 msg_gdbg("failed! flashprog has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000580 continue;
581 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000582
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000583 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200584 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000585 if (!flash->chip) {
586 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100587 return -1;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000588 }
Angel Pons7e134562021-06-07 13:29:13 +0200589 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000590 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000591
Nico Huber9eec4072023-01-12 01:17:30 +0100592 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_PROBE))
593 goto free_chip;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000594
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000595 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
596 * is only called with force=1 after normal probing failed.
597 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000598 if (force)
599 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000600
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000601 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000602 goto notfound;
603
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000604 /* If this is the first chip found, accept it.
605 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000606 * a non-generic match. SFDP and CFI are generic matches.
607 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000608 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000609 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000610 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000611 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000612 msg_cinfo("===\n"
613 "SFDP has autodetected a flash chip which is "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200614 "not natively supported by flashprog yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000615 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000616 msg_cinfo("The standard operations read and "
617 "verify should work, but to support "
618 "erase, write and all other "
619 "possible features");
620 else
621 msg_cinfo("All standard operations (read, "
622 "verify, erase and write) should "
623 "work, but to support all possible "
624 "features");
625
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000626 msg_cinfo(" we need to add them manually.\n"
627 "You can help us by mailing us the output of the following command to "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200628 "flashprog@flashprog.org:\n"
629 "'flashprog -VV [plus the -p/--programmer parameter]'\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000630 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000631 "===\n");
632 }
633
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000634 /* First flash chip detected on this bus. */
635 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000636 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000637 /* Not the first flash chip detected on this bus, but not a generic match either. */
638 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
639 break;
640 /* Not the first flash chip detected on this bus, and it's just a generic match. Ignore it. */
Peter Stuge483b8f02008-09-03 23:10:05 +0000641notfound:
Nico Huber9eec4072023-01-12 01:17:30 +0100642 if (flash->chip->finish_access)
643 flash->chip->finish_access(flash);
644free_chip:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000645 free(flash->chip);
646 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000647 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000648
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000649 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000650 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000651
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100652 if (init_default_layout(flash) < 0)
653 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000654
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000655 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000656 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
657 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000658 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000659#if CONFIG_INTERNAL == 1
Nico Huber0e76d992023-01-12 20:22:55 +0100660 if (flash->physical_memory != 0 && mst->par.map_flash == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000661 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
662 PRIxPTR_WIDTH, flash->physical_memory);
663 else
664#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200665 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000666
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000667 /* Flash registers may more likely not be mapped if the chip was forced.
668 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000669 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000670 if (flash->chip->printlock)
671 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000672
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000673 /* Get out of the way for later runs. */
Nico Huber9eec4072023-01-12 01:17:30 +0100674 if (flash->chip->finish_access)
675 flash->chip->finish_access(flash);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000676
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000677 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000678 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000679}
680
Stefan Tauner96658be2014-05-26 22:05:31 +0000681/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000682static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000683{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000684 int i, j, k;
685 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530686 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000687
688 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
689 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000690 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530691 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000692
693 for (i = 0; i < NUM_ERASEREGIONS; i++) {
694 /* Blocks with zero size are bugs in flashchips.c. */
695 if (eraser.eraseblocks[i].count &&
696 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000697 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200698 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000699 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000700 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000701 }
702 /* Blocks with zero count are bugs in flashchips.c. */
703 if (!eraser.eraseblocks[i].count &&
704 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000705 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200706 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000707 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000708 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000709 }
710 done += eraser.eraseblocks[i].count *
711 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530712 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000713 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000714 /* Empty eraseblock definition with erase function. */
715 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000716 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000717 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000718 if (!done)
719 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000720 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000721 msg_gerr("ERROR: Flash chip %s erase function %i "
722 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000723 " expected 0x%06x bytes.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200724 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000725 chip->name, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000726 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000727 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000728 if (!eraser.block_erase)
729 continue;
730 /* Check if there are identical erase functions for different
731 * layouts. That would imply "magic" erase functions. The
732 * easiest way to check this is with function pointers.
733 */
Uwe Hermann43959702010-03-13 17:28:29 +0000734 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000735 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000736 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000737 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200738 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000739 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000740 ret = 1;
741 }
Uwe Hermann43959702010-03-13 17:28:29 +0000742 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530743 if(curr_eraseblock_count > prev_eraseblock_count)
744 {
Nico Huberac90af62022-12-18 00:22:47 +0000745 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200746 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000747 chip->name, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530748 ret = 1;
749 }
750 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000751 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000752 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000753}
754
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000755static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000756{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000757 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000758
759 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
760 if (log)
761 msg_cdbg("not defined. ");
762 return 1;
763 }
764 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
765 if (log)
766 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000767 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000768 return 1;
769 }
770 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
771 if (log)
772 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000773 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000774 return 1;
775 }
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530776
777 if (flash->chip->bustype == BUS_SPI) {
Nico Huber13389362024-03-05 18:35:30 +0100778 bool native_4ba;
Nico Huber13f97a52023-01-14 23:55:06 +0100779 int i;
Nico Huber13389362024-03-05 18:35:30 +0100780
781 const uint8_t *opcode = spi_get_opcode_from_erasefn(eraser.block_erase, &native_4ba);
Nico Huber13f97a52023-01-14 23:55:06 +0100782 for (i = 0; opcode[i]; i++) {
Nico Huber13389362024-03-05 18:35:30 +0100783 if ((native_4ba && !spi_master_4ba(flash)) ||
784 !flash->mst->spi.probe_opcode(flash, opcode[i])) {
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530785 if (log)
786 msg_cdbg("block erase function and layout found "
787 "but SPI master doesn't support the function. ");
788 return 1;
789 }
790 }
791 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000792 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000793 return 0;
794}
795
Nico Huber7af0e792016-04-29 16:40:15 +0200796/**
797 * @brief Reads the included layout regions into a buffer.
798 *
799 * If there is no layout set in the given flash context, the whole chip will
800 * be read.
801 *
802 * @param flashctx Flash context to be used.
803 * @param buffer Buffer of full chip size to read into.
804 * @return 0 on success,
805 * 1 if any read fails.
806 */
807static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
808{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200809 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200810 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200811
Nico Huber5ca55232019-06-15 22:29:08 +0200812 while ((entry = layout_next_included(layout, entry))) {
813 const chipoff_t region_start = entry->start;
814 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200815
816 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
817 return 1;
818 }
819 return 0;
820}
821
Nico Huber7af0e792016-04-29 16:40:15 +0200822/**
823 * @private
824 *
825 * For read-erase-write, `curcontents` and `newcontents` shall point
826 * to buffers of the chip's size. Both are supposed to be prefilled
827 * with at least the included layout regions of the current flash
828 * contents (`curcontents`) and the data to be written to the flash
829 * (`newcontents`).
830 *
831 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
832 *
833 * The `chipoff_t` values are used internally by `walk_by_layout()`.
834 */
835struct walk_info {
836 uint8_t *curcontents;
837 const uint8_t *newcontents;
838 chipoff_t region_start;
839 chipoff_t region_end;
840 chipoff_t erase_start;
841 chipoff_t erase_end;
842};
Nico Huber3b9c86d2023-01-15 22:58:06 +0100843
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530844struct eraseblock_data {
845 chipoff_t start_addr;
846 chipoff_t end_addr;
847 bool selected;
848 size_t block_num;
849 size_t first_sub_block_index;
850 size_t last_sub_block_index;
851};
852
853struct erase_layout {
854 struct eraseblock_data* layout_list;
855 size_t block_count;
856 const struct block_eraser *eraser;
857};
858
Nico Huber5ff6fdc2023-01-15 23:43:12 +0100859static bool explicit_erase(const struct walk_info *const info)
860{
861 /* For explicit erase, we are called without new contents. */
862 return !info->newcontents;
863}
864
Nico Huberd96e7032023-01-14 22:31:48 +0100865static size_t calculate_block_count(const struct block_eraser *const eraser)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530866{
Nico Huberd96e7032023-01-14 22:31:48 +0100867 size_t block_count = 0, i;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530868
Nico Huberd96e7032023-01-14 22:31:48 +0100869 for (i = 0; i < ARRAY_SIZE(eraser->eraseblocks); ++i)
870 block_count += eraser->eraseblocks[i].count;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530871
872 return block_count;
873}
874
875static void init_eraseblock(struct erase_layout *layout, size_t idx, size_t block_num,
876 chipoff_t start_addr, chipoff_t end_addr, size_t *sub_block_index)
877{
878 struct eraseblock_data *edata = &layout[idx].layout_list[block_num];
879 edata->start_addr = start_addr;
880 edata->end_addr = end_addr;
881 edata->selected = false;
882 edata->block_num = block_num;
883
884 if (!idx)
885 return;
Nico Hubera02df332023-01-14 23:06:27 +0100886 const struct erase_layout *const sub_layout = &layout[idx - 1];
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530887
888 edata->first_sub_block_index = *sub_block_index;
Nico Hubera02df332023-01-14 23:06:27 +0100889 for (; *sub_block_index < sub_layout->block_count; ++*sub_block_index) {
890 if (sub_layout->layout_list[*sub_block_index].end_addr > end_addr)
891 break;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530892 }
893 edata->last_sub_block_index = *sub_block_index - 1;
894}
895
896/*
897 * @brief Function to free the created erase_layout
898 *
899 * @param layout pointer to allocated layout
900 * @param erasefn_count number of erase functions for which the layout was created
901 *
902 */
903static void free_erase_layout(struct erase_layout *layout, unsigned int erasefn_count)
904{
Nico Huber13f97a52023-01-14 23:55:06 +0100905 size_t i;
906
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530907 if (!layout)
908 return;
Nico Huber13f97a52023-01-14 23:55:06 +0100909 for (i = 0; i < erasefn_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530910 free(layout[i].layout_list);
911 }
912 free(layout);
913}
914
915/*
916 * @brief Function to create an erase layout
917 *
918 * @param flashctx flash context
919 * @param e_layout address to the pointer to store the layout
920 * @return 0 on success,
921 * -1 if layout creation fails
922 *
923 * This function creates a layout of which erase functions erase which regions
924 * of the flash chip. This helps to optimally select the erase functions for
925 * erase/write operations.
926 */
Nico Huberc09fca42023-01-29 15:58:09 +0100927static int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530928{
929 const struct flashchip *chip = flashctx->chip;
930 const size_t erasefn_count = count_usable_erasers(flashctx);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530931
932 if (!erasefn_count) {
933 msg_gerr("No erase functions supported\n");
934 return 0;
935 }
936
Nico Huberd34864b2023-01-14 23:47:19 +0100937 struct erase_layout *layout = calloc(erasefn_count, sizeof(struct erase_layout));
938 if (!layout) {
939 msg_gerr("Out of memory!\n");
940 return -1;
941 }
942
Nico Huber13f97a52023-01-14 23:55:06 +0100943 size_t layout_idx = 0, eraser_idx;
944 for (eraser_idx = 0; eraser_idx < NUM_ERASEFUNCTIONS; eraser_idx++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530945 if (check_block_eraser(flashctx, eraser_idx, 0))
946 continue;
947
948 layout[layout_idx].eraser = &chip->block_erasers[eraser_idx];
Nico Huberd96e7032023-01-14 22:31:48 +0100949 const size_t block_count = calculate_block_count(&chip->block_erasers[eraser_idx]);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530950 size_t sub_block_index = 0;
951
952 layout[layout_idx].block_count = block_count;
953 layout[layout_idx].layout_list = (struct eraseblock_data *)calloc(block_count,
954 sizeof(struct eraseblock_data));
955
956 if (!layout[layout_idx].layout_list) {
957 free_erase_layout(layout, layout_idx);
958 return -1;
959 }
960
961 size_t block_num = 0;
962 chipoff_t start_addr = 0;
963
Nico Huber13f97a52023-01-14 23:55:06 +0100964 int i;
965 for (i = 0; block_num < block_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530966 const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];
967
Nico Huber13f97a52023-01-14 23:55:06 +0100968 size_t num;
969 for (num = 0; num < block->count; num++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530970 chipoff_t end_addr = start_addr + block->size - 1;
971 init_eraseblock(layout, layout_idx, block_num,
972 start_addr, end_addr, &sub_block_index);
973 block_num += 1;
974 start_addr = end_addr + 1;
975 }
976 }
977 layout_idx++;
978 }
979
980 *e_layout = layout;
981 return layout_idx;
982}
983
Aarya Chaumald33bea42022-07-14 12:51:14 +0530984/*
985 * @brief Function to select the list of sectors that need erasing
986 *
987 * @param flashctx flash context
988 * @param layout erase layout
989 * @param findex index of the erase function
990 * @param block_num index of the block to erase according to the erase function index
Nico Huber00d1b9f2023-01-29 15:07:33 +0100991 * @param info current info from walking the regions
Aarya Chaumald33bea42022-07-14 12:51:14 +0530992 */
Nico Huberb11b72c2023-01-29 15:33:11 +0100993static void select_erase_functions_rec(const struct flashctx *flashctx, const struct erase_layout *layout,
Nico Huber00d1b9f2023-01-29 15:07:33 +0100994 size_t findex, size_t block_num, const struct walk_info *info)
Aarya Chaumald33bea42022-07-14 12:51:14 +0530995{
996 struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
997 if (!findex) {
Nico Hubercf6ff0a2023-01-29 15:45:06 +0100998 if (ll->start_addr <= info->region_end && ll->end_addr >= info->region_start) {
Nico Huber1494f8e2023-01-29 15:48:00 +0100999 if (explicit_erase(info)) {
1000 ll->selected = true;
1001 return;
1002 }
Nico Hubera6212482023-01-29 15:39:26 +01001003 const chipoff_t write_start = MAX(info->region_start, ll->start_addr);
1004 const chipoff_t write_end = MIN(info->region_end, ll->end_addr);
1005 const chipsize_t write_len = write_end - write_start + 1;
1006 const uint8_t erased_value = ERASED_VALUE(flashctx);
Nico Huber00d1b9f2023-01-29 15:07:33 +01001007 ll->selected = need_erase(
Nico Hubera6212482023-01-29 15:39:26 +01001008 info->curcontents + write_start, info->newcontents + write_start,
1009 write_len, flashctx->chip->gran, erased_value);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301010 }
1011 } else {
1012 int count = 0;
1013 const int sub_block_start = ll->first_sub_block_index;
1014 const int sub_block_end = ll->last_sub_block_index;
1015
Nico Huber13f97a52023-01-14 23:55:06 +01001016 int j;
1017 for (j = sub_block_start; j <= sub_block_end; j++) {
Nico Huberb11b72c2023-01-29 15:33:11 +01001018 select_erase_functions_rec(flashctx, layout, findex - 1, j, info);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301019 if (layout[findex - 1].layout_list[j].selected)
1020 count++;
1021 }
1022
1023 const int total_blocks = sub_block_end - sub_block_start + 1;
1024 if (count && count > total_blocks/2) {
Nico Huber00d1b9f2023-01-29 15:07:33 +01001025 if (ll->start_addr >= info->region_start && ll->end_addr <= info->region_end) {
Nico Huber13f97a52023-01-14 23:55:06 +01001026 for (j = sub_block_start; j <= sub_block_end; j++)
Aarya Chaumald33bea42022-07-14 12:51:14 +05301027 layout[findex - 1].layout_list[j].selected = false;
1028 ll->selected = true;
1029 }
1030 }
1031 }
1032}
1033
Nico Huberc09fca42023-01-29 15:58:09 +01001034static void select_erase_functions(const struct flashctx *flashctx, const struct erase_layout *layout,
Nico Huberb11b72c2023-01-29 15:33:11 +01001035 size_t erasefn_count, const struct walk_info *info)
1036{
1037 size_t block_num;
1038 for (block_num = 0; block_num < layout[erasefn_count - 1].block_count; ++block_num)
1039 select_erase_functions_rec(flashctx, layout, erasefn_count - 1, block_num, info);
1040}
1041
Nico Huber3b9c86d2023-01-15 22:58:06 +01001042static int write_range(struct flashctx *const flashctx, const chipoff_t flash_offset,
1043 const uint8_t *const curcontents, const uint8_t *const newcontents,
1044 const chipsize_t len, bool *const skipped)
1045{
1046 unsigned int writecount = 0;
1047 chipoff_t starthere = 0;
1048 chipsize_t lenhere = 0;
1049
1050 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1051 len - starthere, &starthere, flashctx->chip->gran))) {
1052 if (!writecount++)
1053 msg_cdbg("W");
1054 if (flashctx->chip->write(flashctx, newcontents + starthere,
1055 flash_offset + starthere, lenhere))
1056 return 1;
1057 starthere += lenhere;
1058 if (skipped)
1059 *skipped = false;
1060 }
1061 return 0;
1062}
1063
1064typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
Nico Huber7af0e792016-04-29 16:40:15 +02001065/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1066typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1067
1068static int walk_eraseblocks(struct flashctx *const flashctx,
Nico Huberc09fca42023-01-29 15:58:09 +01001069 struct erase_layout *const layouts,
1070 const size_t layout_count,
Nico Huber7af0e792016-04-29 16:40:15 +02001071 struct walk_info *const info,
Nico Huberc09fca42023-01-29 15:58:09 +01001072 const per_blockfn_t per_blockfn)
Nico Huber7af0e792016-04-29 16:40:15 +02001073{
1074 int ret;
1075 size_t i, j;
1076 bool first = true;
Nico Huber7af0e792016-04-29 16:40:15 +02001077
Nico Huberc09fca42023-01-29 15:58:09 +01001078 for (i = 0; i < layout_count; ++i) {
1079 const struct erase_layout *const layout = &layouts[i];
Nico Huber7af0e792016-04-29 16:40:15 +02001080
Nico Huberc09fca42023-01-29 15:58:09 +01001081 for (j = 0; j < layout->block_count; ++j) {
1082 struct eraseblock_data *const eb = &layout->layout_list[j];
1083
1084 if (eb->start_addr > info->region_end)
Nico Huber7af0e792016-04-29 16:40:15 +02001085 break;
Nico Huberc09fca42023-01-29 15:58:09 +01001086 if (eb->end_addr < info->region_start)
1087 continue;
1088 if (!eb->selected)
1089 continue;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001090
Nico Huber7af0e792016-04-29 16:40:15 +02001091 /* Print this for every block except the first one. */
1092 if (first)
1093 first = false;
1094 else
1095 msg_cdbg(", ");
Nico Huberc09fca42023-01-29 15:58:09 +01001096 msg_cdbg("0x%06x-0x%06x:", eb->start_addr, eb->end_addr);
Nico Huber7af0e792016-04-29 16:40:15 +02001097
Nico Huberc09fca42023-01-29 15:58:09 +01001098 info->erase_start = eb->start_addr;
1099 info->erase_end = eb->end_addr;
1100 ret = per_blockfn(flashctx, info, layout->eraser->block_erase);
Nico Huber7af0e792016-04-29 16:40:15 +02001101 if (ret)
1102 return ret;
Nico Huberc09fca42023-01-29 15:58:09 +01001103
1104 /* Clean the erase layout up for future use on other
1105 regions. `.selected` is the only field we alter. */
1106 eb->selected = false;
Nico Huber7af0e792016-04-29 16:40:15 +02001107 }
Nico Huber7af0e792016-04-29 16:40:15 +02001108 }
1109 msg_cdbg("\n");
1110 return 0;
1111}
1112
1113static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1114 const per_blockfn_t per_blockfn)
1115{
Nico Huberc09fca42023-01-29 15:58:09 +01001116 const bool do_erase = explicit_erase(info) || !(flashctx->chip->feature_bits & FEATURE_NO_ERASE);
Nico Huberc3b02dc2023-08-12 01:13:45 +02001117 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huberc09fca42023-01-29 15:58:09 +01001118 struct erase_layout *erase_layouts = NULL;
Nico Huber5ca55232019-06-15 22:29:08 +02001119 const struct romentry *entry = NULL;
Nico Huberc09fca42023-01-29 15:58:09 +01001120 int ret = 0, layout_count = 0;
Nico Huber7af0e792016-04-29 16:40:15 +02001121
1122 all_skipped = true;
1123 msg_cinfo("Erasing and writing flash chip... ");
1124
Nico Huberc09fca42023-01-29 15:58:09 +01001125 if (do_erase) {
1126 layout_count = create_erase_layout(flashctx, &erase_layouts);
1127 if (layout_count <= 0)
1128 return 1;
1129 }
1130
Nico Huber5ca55232019-06-15 22:29:08 +02001131 while ((entry = layout_next_included(layout, entry))) {
1132 info->region_start = entry->start;
1133 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001134
Nico Huberc09fca42023-01-29 15:58:09 +01001135 if (do_erase) {
1136 select_erase_functions(flashctx, erase_layouts, layout_count, info);
1137 ret = walk_eraseblocks(flashctx, erase_layouts, layout_count, info, per_blockfn);
1138 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001139 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001140 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001141 }
Nico Huber7af0e792016-04-29 16:40:15 +02001142 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001143
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001144 if (info->newcontents) {
1145 bool skipped = true;
1146 msg_cdbg("0x%06x-0x%06x:", info->region_start, info->region_end);
Nico Huberc09fca42023-01-29 15:58:09 +01001147 ret = write_range(flashctx, info->region_start,
1148 info->curcontents + info->region_start,
1149 info->newcontents + info->region_start,
1150 info->region_end + 1 - info->region_start, &skipped);
1151 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001152 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001153 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001154 }
1155 if (skipped) {
1156 msg_cdbg("S\n");
1157 } else {
1158 msg_cdbg("\n");
1159 all_skipped = false;
1160 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001161 }
Nico Huber7af0e792016-04-29 16:40:15 +02001162 }
1163 if (all_skipped)
1164 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1165 msg_cinfo("Erase/write done.\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001166
1167free_ret:
1168 free_erase_layout(erase_layouts, layout_count);
1169 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001170}
1171
1172static int erase_block(struct flashctx *const flashctx,
1173 const struct walk_info *const info, const erasefn_t erasefn)
1174{
1175 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001176 const bool region_unaligned = info->region_start > info->erase_start ||
1177 info->erase_end > info->region_end;
1178 uint8_t *backup_contents = NULL, *erased_contents = NULL;
Nico Huberd34af7a2023-01-15 23:58:18 +01001179 int ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001180
Nico Huber6e61e0c2019-01-23 17:07:49 +01001181 /*
1182 * If the region is not erase-block aligned, merge current flash con-
1183 * tents into a new buffer `backup_contents`.
1184 */
1185 if (region_unaligned) {
1186 backup_contents = malloc(erase_len);
1187 erased_contents = malloc(erase_len);
1188 if (!backup_contents || !erased_contents) {
1189 msg_cerr("Out of memory!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001190 goto _free_ret;
1191 }
1192 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1193 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1194
1195 msg_cdbg("R");
1196 /* Merge data preceding the current region. */
1197 if (info->region_start > info->erase_start) {
1198 const chipoff_t start = info->erase_start;
1199 const chipsize_t len = info->region_start - info->erase_start;
1200 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1201 msg_cerr("Can't read! Aborting.\n");
1202 goto _free_ret;
1203 }
1204 }
1205 /* Merge data following the current region. */
1206 if (info->erase_end > info->region_end) {
1207 const chipoff_t start = info->region_end + 1;
1208 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1209 const chipsize_t len = info->erase_end - info->region_end;
1210 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1211 msg_cerr("Can't read! Aborting.\n");
1212 goto _free_ret;
1213 }
1214 }
1215 }
1216
Nico Huber7af0e792016-04-29 16:40:15 +02001217 all_skipped = false;
1218
1219 msg_cdbg("E");
1220 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001221 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001222 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1223 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001224 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001225 }
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001226 if (info->curcontents)
1227 memset(info->curcontents + info->erase_start, ERASED_VALUE(flashctx), erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001228
1229 if (region_unaligned) {
Nico Huber3b9c86d2023-01-15 22:58:06 +01001230 if (write_range(flashctx, info->erase_start, erased_contents, backup_contents, erase_len, NULL))
1231 goto _free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001232 if (info->curcontents)
1233 memcpy(info->curcontents + info->erase_start, backup_contents, erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001234 }
1235
1236 ret = 0;
1237
1238_free_ret:
1239 free(erased_contents);
1240 free(backup_contents);
1241 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001242}
1243
1244/**
1245 * @brief Erases the included layout regions.
1246 *
1247 * If there is no layout set in the given flash context, the whole chip will
1248 * be erased.
1249 *
1250 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001251 * @return 0 on success,
1252 * 1 if all available erase functions failed.
1253 */
Nico Huber454f6132012-12-10 13:34:10 +00001254static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001255{
1256 struct walk_info info = { 0 };
1257 return walk_by_layout(flashctx, &info, &erase_block);
1258}
1259
Nico Huber7af0e792016-04-29 16:40:15 +02001260/**
1261 * @brief Writes the included layout regions from a given image.
1262 *
1263 * If there is no layout set in the given flash context, the whole image
1264 * will be written.
1265 *
1266 * @param flashctx Flash context to be used.
1267 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1268 * @param newcontents The new image to be written.
1269 * @return 0 on success,
1270 * 1 if anything has gone wrong.
1271 */
Nico Huber454f6132012-12-10 13:34:10 +00001272static int write_by_layout(struct flashctx *const flashctx,
1273 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001274{
1275 struct walk_info info;
1276 info.curcontents = curcontents;
1277 info.newcontents = newcontents;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001278 return walk_by_layout(flashctx, &info, erase_block);
Nico Huber7af0e792016-04-29 16:40:15 +02001279}
1280
1281/**
1282 * @brief Compares the included layout regions with content from a buffer.
1283 *
1284 * If there is no layout set in the given flash context, the whole chip's
1285 * contents will be compared.
1286 *
1287 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001288 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001289 * @param curcontents A buffer of full chip size to read current chip contents into.
1290 * @param newcontents The new image to compare to.
1291 * @return 0 on success,
1292 * 1 if reading failed,
1293 * 3 if the contents don't match.
1294 */
Nico Huber74d09d42019-06-16 03:27:26 +02001295static int verify_by_layout(
1296 struct flashctx *const flashctx,
Nico Huberc3b02dc2023-08-12 01:13:45 +02001297 const struct flashprog_layout *const layout,
Nico Huber74d09d42019-06-16 03:27:26 +02001298 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001299{
Nico Huber5ca55232019-06-15 22:29:08 +02001300 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001301
Nico Huber5ca55232019-06-15 22:29:08 +02001302 while ((entry = layout_next_included(layout, entry))) {
1303 const chipoff_t region_start = entry->start;
1304 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001305
1306 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1307 return 1;
1308 if (compare_range(newcontents + region_start, curcontents + region_start,
1309 region_start, region_len))
1310 return 3;
1311 }
1312 return 0;
1313}
1314
Stefan Tauner136388f2013-07-15 10:47:53 +00001315static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001316{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001317 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001318#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001319 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001320 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001321 "chip. Please report this to the mailing list at flashprog@flashprog.org or\n"
1322 "on IRC (see https://www.flashprog.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001323 "-------------------------------------------------------------------------------\n"
1324 "You may now reboot or simply leave the machine running.\n");
1325 else
1326#endif
1327 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001328 "the programmer and the flash chip. If you think the error is caused by flashprog\n"
1329 "please report this to the mailing list at flashprog@flashprog.org or on IRC\n"
1330 "(see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001331}
1332
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001333void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001334{
Stefan Tauner136388f2013-07-15 10:47:53 +00001335 msg_gerr("Your flash chip is in an unknown state.\n");
1336#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001337 if (programmer == &programmer_internal)
Nico Huberc3b02dc2023-08-12 01:13:45 +02001338 msg_gerr("Get help on IRC (see https://www.flashprog.org/Contact) or mail\n"
1339 "flashprog@flashprog.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001340 "-------------------------------------------------------------------------------\n"
1341 "DO NOT REBOOT OR POWEROFF!\n");
1342 else
1343#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +02001344 msg_gerr("Please report this to the mailing list at flashprog@flashprog.org\n"
1345 "or on IRC (see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001346}
1347
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001348void list_programmers_linebreak(int startcol, int cols, int paren)
1349{
1350 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001351 int pnamelen;
1352 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001353 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001354 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001355
Thomas Heijligend45cb592021-05-19 14:12:18 +02001356 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001357 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001358 pnamelen = strlen(pname);
1359 if (remaining - pnamelen - 2 < 0) {
1360 if (firstline)
1361 firstline = 0;
1362 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001363 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001364 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001365 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001366 remaining = cols - startcol;
1367 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001368 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001369 remaining--;
1370 }
1371 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001372 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001373 remaining--;
1374 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001375 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001376 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001377 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001378 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001379 remaining--;
1380 } else {
1381 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001382 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001383 }
1384 }
1385}
1386
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001387int selfcheck(void)
1388{
Stefan Tauner96658be2014-05-26 22:05:31 +00001389 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001390 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001391
Thomas Heijligend45cb592021-05-19 14:12:18 +02001392 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001393 const struct programmer_entry *const p = programmer_table[i];
1394 if (p == NULL) {
1395 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1396 ret = 1;
1397 continue;
1398 }
1399 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001400 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1401 ret = 1;
1402 /* This might hide other problems with this programmer, but allows for better error
1403 * messages below without jumping through hoops. */
1404 continue;
1405 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001406 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001407 case USB:
1408 case PCI:
1409 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001410 if (p->devs.note == NULL) {
1411 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001412 break; /* This one has its device list stored separately. */
1413 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001414 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001415 ret = 1;
1416 }
1417 break;
1418 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001419 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001420 ret = 1;
1421 break;
1422 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001423 if (p->init == NULL) {
1424 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001425 ret = 1;
1426 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001427 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001428
1429 /* It would be favorable if we could check for the correct layout (especially termination) of various
1430 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1431 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1432 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1433 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1434 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001435 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001436 msg_gerr("Flashchips table miscompilation!\n");
1437 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001438 } else {
1439 for (i = 0; i < flashchips_size - 1; i++) {
1440 const struct flashchip *chip = &flashchips[i];
1441 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1442 ret = 1;
1443 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001444 "Please report a bug at flashprog@flashprog.org\n", i,
Stefan Tauner96658be2014-05-26 22:05:31 +00001445 chip->name == NULL ? "unnamed" : chip->name);
1446 }
1447 if (selfcheck_eraseblocks(chip)) {
1448 ret = 1;
1449 }
1450 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001451 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001452
Stefan Tauner600576b2014-06-12 22:57:36 +00001453#if CONFIG_INTERNAL == 1
1454 ret |= selfcheck_board_enables();
1455#endif
1456
Stefan Tauner96658be2014-05-26 22:05:31 +00001457 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001458 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001459}
1460
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001461/* FIXME: This function signature needs to be improved once prepare_flash_access()
1462 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001463 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001464static int chip_safety_check(const struct flashctx *flash, int force,
1465 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001466{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001467 const struct flashchip *chip = flash->chip;
1468
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001469 if (!programmer_may_write && (write_it || erase_it)) {
1470 msg_perr("Write/erase is not working yet on your programmer in "
1471 "its current configuration.\n");
1472 /* --force is the wrong approach, but it's the best we can do
1473 * until the generic programmer parameter parser is merged.
1474 */
1475 if (!force)
1476 return 1;
1477 msg_cerr("Continuing anyway.\n");
1478 }
1479
1480 if (read_it || erase_it || write_it || verify_it) {
1481 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001482 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001483 msg_cerr("Read is not working on this chip. ");
1484 if (!force)
1485 return 1;
1486 msg_cerr("Continuing anyway.\n");
1487 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001488 if (!chip->read) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001489 msg_cerr("flashprog has no read function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001490 "flash chip.\n");
1491 return 1;
1492 }
1493 }
1494 if (erase_it || write_it) {
1495 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001496 if (chip->tested.erase == NA) {
1497 msg_cerr("Erase is not possible on this chip.\n");
1498 return 1;
1499 }
1500 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001501 msg_cerr("Erase is not working on this chip. ");
1502 if (!force)
1503 return 1;
1504 msg_cerr("Continuing anyway.\n");
1505 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001506 if(count_usable_erasers(flash) == 0) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001507 msg_cerr("flashprog has no erase function for this "
Stefan Tauner5368dca2011-07-01 00:19:12 +00001508 "flash chip.\n");
1509 return 1;
1510 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001511 }
1512 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001513 if (chip->tested.write == NA) {
1514 msg_cerr("Write is not possible on this chip.\n");
1515 return 1;
1516 }
1517 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001518 msg_cerr("Write is not working on this chip. ");
1519 if (!force)
1520 return 1;
1521 msg_cerr("Continuing anyway.\n");
1522 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001523 if (!chip->write) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001524 msg_cerr("flashprog has no write function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001525 "flash chip.\n");
1526 return 1;
1527 }
1528 }
1529 return 0;
1530}
1531
Nico Huber305f4172013-06-14 11:55:26 +02001532int prepare_flash_access(struct flashctx *const flash,
1533 const bool read_it, const bool write_it,
1534 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001535{
1536 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1537 msg_cerr("Aborting.\n");
1538 return 1;
1539 }
1540
Nico Huber3ac761c2023-01-16 02:43:17 +01001541 if (layout_sanity_checks(flash, write_it)) {
Nico Huber454f6132012-12-10 13:34:10 +00001542 msg_cerr("Requested regions can not be handled. Aborting.\n");
1543 return 1;
1544 }
1545
Nico Huber901fb952023-01-11 23:24:23 +01001546 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_FULL))
1547 return 1;
1548
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001549 /* Initialize chip_restore_fn_count before chip unlock calls. */
1550 flash->chip_restore_fn_count = 0;
1551
Nico Huber454f6132012-12-10 13:34:10 +00001552 /* Given the existence of read locks, we want to unlock for read,
1553 erase and write. */
1554 if (flash->chip->unlock)
1555 flash->chip->unlock(flash);
1556
1557 return 0;
1558}
1559
Nico Huber305f4172013-06-14 11:55:26 +02001560void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001561{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001562 deregister_chip_restore(flash);
Nico Huber901fb952023-01-11 23:24:23 +01001563 if (flash->chip->finish_access)
1564 flash->chip->finish_access(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001565}
1566
1567/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001568 * @addtogroup flashprog-flash
Nico Huber454f6132012-12-10 13:34:10 +00001569 * @{
1570 */
1571
1572/**
1573 * @brief Erase the specified ROM chip.
1574 *
1575 * If a layout is set in the given flash context, only included regions
1576 * will be erased.
1577 *
1578 * @param flashctx The context of the flash chip to erase.
1579 * @return 0 on success.
1580 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001581int flashprog_flash_erase(struct flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +00001582{
1583 if (prepare_flash_access(flashctx, false, false, true, false))
1584 return 1;
1585
1586 const int ret = erase_by_layout(flashctx);
1587
1588 finalize_flash_access(flashctx);
1589
1590 return ret;
1591}
1592
Nico Huberc3b02dc2023-08-12 01:13:45 +02001593/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +00001594
1595/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001596 * @defgroup flashprog-ops Operations
Nico Huber454f6132012-12-10 13:34:10 +00001597 * @{
1598 */
1599
1600/**
1601 * @brief Read the current image from the specified ROM chip.
1602 *
1603 * If a layout is set in the specified flash context, only included regions
1604 * will be read.
1605 *
1606 * @param flashctx The context of the flash chip.
1607 * @param buffer Target buffer to write image to.
1608 * @param buffer_len Size of target buffer in bytes.
1609 * @return 0 on success,
1610 * 2 if buffer_len is too short for the flash chip's contents,
1611 * or 1 on any other failure.
1612 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001613int flashprog_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001614{
1615 const size_t flash_size = flashctx->chip->total_size * 1024;
1616
1617 if (flash_size > buffer_len)
1618 return 2;
1619
1620 if (prepare_flash_access(flashctx, true, false, false, false))
1621 return 1;
1622
1623 msg_cinfo("Reading flash... ");
1624
1625 int ret = 1;
1626 if (read_by_layout(flashctx, buffer)) {
1627 msg_cerr("Read operation failed!\n");
1628 msg_cinfo("FAILED.\n");
1629 goto _finalize_ret;
1630 }
1631 msg_cinfo("done.\n");
1632 ret = 0;
1633
1634_finalize_ret:
1635 finalize_flash_access(flashctx);
1636 return ret;
1637}
1638
1639static void combine_image_by_layout(const struct flashctx *const flashctx,
1640 uint8_t *const newcontents, const uint8_t *const oldcontents)
1641{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001642 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001643 const struct romentry *included;
1644 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001645
Nico Huber3d7b1e32018-12-22 00:53:14 +01001646 while ((included = layout_next_included_region(layout, start))) {
1647 if (included->start > start) {
1648 /* copy everything up to the start of this included region */
1649 memcpy(newcontents + start, oldcontents + start, included->start - start);
1650 }
1651 /* skip this included region */
1652 start = included->end + 1;
1653 if (start == 0)
1654 return;
Nico Huber454f6132012-12-10 13:34:10 +00001655 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001656
1657 /* copy the rest of the chip */
1658 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1659 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001660}
1661
1662/**
1663 * @brief Write the specified image to the ROM chip.
1664 *
1665 * If a layout is set in the specified flash context, only erase blocks
1666 * containing included regions will be touched.
1667 *
1668 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001669 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001670 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001671 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001672 * @return 0 on success,
1673 * 4 if buffer_len doesn't match the size of the flash chip,
1674 * 3 if write was tried but nothing has changed,
1675 * 2 if write failed and flash contents changed,
1676 * or 1 on any other failure.
1677 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001678int flashprog_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001679 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001680{
1681 const size_t flash_size = flashctx->chip->total_size * 1024;
1682 const bool verify_all = flashctx->flags.verify_whole_chip;
1683 const bool verify = flashctx->flags.verify_after_write;
Nico Huberc3b02dc2023-08-12 01:13:45 +02001684 const struct flashprog_layout *const verify_layout =
Nico Huber74d09d42019-06-16 03:27:26 +02001685 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001686
1687 if (buffer_len != flash_size)
1688 return 4;
1689
1690 int ret = 1;
1691
1692 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001693 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001694 uint8_t *const curcontents = malloc(flash_size);
1695 uint8_t *oldcontents = NULL;
1696 if (verify_all)
1697 oldcontents = malloc(flash_size);
1698 if (!curcontents || (verify_all && !oldcontents)) {
1699 msg_gerr("Out of memory!\n");
1700 goto _free_ret;
1701 }
1702
1703#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001704 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001705 if (flashctx->flags.force_boardmismatch) {
1706 msg_pinfo("Proceeding anyway because user forced us to.\n");
1707 } else {
1708 msg_perr("Aborting. You can override this with "
1709 "-p internal:boardmismatch=force.\n");
1710 goto _free_ret;
1711 }
1712 }
1713#endif
1714
1715 if (prepare_flash_access(flashctx, false, true, false, verify))
1716 goto _free_ret;
1717
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001718 /* If given, assume flash chip contains same data as `refcontents`. */
1719 if (refcontents) {
1720 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1721 memcpy(curcontents, refcontents, flash_size);
1722 if (oldcontents)
1723 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001724 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001725 /*
1726 * Read the whole chip to be able to check whether regions need to be
1727 * erased and to give better diagnostics in case write fails.
1728 * The alternative is to read only the regions which are to be
1729 * preserved, but in that case we might perform unneeded erase which
1730 * takes time as well.
1731 */
1732 msg_cinfo("Reading old flash chip contents... ");
1733 if (verify_all) {
1734 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
1735 msg_cinfo("FAILED.\n");
1736 goto _finalize_ret;
1737 }
1738 memcpy(curcontents, oldcontents, flash_size);
1739 } else {
1740 if (read_by_layout(flashctx, curcontents)) {
1741 msg_cinfo("FAILED.\n");
1742 goto _finalize_ret;
1743 }
Nico Huber454f6132012-12-10 13:34:10 +00001744 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001745 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001746 }
Nico Huber454f6132012-12-10 13:34:10 +00001747
1748 if (write_by_layout(flashctx, curcontents, newcontents)) {
1749 msg_cerr("Uh oh. Erase/write failed. ");
1750 ret = 2;
1751 if (verify_all) {
1752 msg_cerr("Checking if anything has changed.\n");
1753 msg_cinfo("Reading current flash chip contents... ");
1754 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
1755 msg_cinfo("done.\n");
1756 if (!memcmp(oldcontents, curcontents, flash_size)) {
1757 nonfatal_help_message();
1758 goto _finalize_ret;
1759 }
1760 msg_cerr("Apparently at least some data has changed.\n");
1761 } else
1762 msg_cerr("Can't even read anymore!\n");
1763 emergency_help_message();
1764 goto _finalize_ret;
1765 } else {
1766 msg_cerr("\n");
1767 }
1768 emergency_help_message();
1769 goto _finalize_ret;
1770 }
1771
1772 /* Verify only if we actually changed something. */
1773 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001774 msg_cinfo("Verifying flash... ");
1775
1776 /* Work around chips which need some time to calm down. */
1777 programmer_delay(1000*1000);
1778
Nico Huber74d09d42019-06-16 03:27:26 +02001779 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001780 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001781 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001782 /* If we tried to write, and verification now fails, we
1783 might have an emergency situation. */
1784 if (ret)
1785 emergency_help_message();
1786 else
1787 msg_cinfo("VERIFIED.\n");
1788 } else {
1789 /* We didn't change anything. */
1790 ret = 0;
1791 }
1792
1793_finalize_ret:
1794 finalize_flash_access(flashctx);
1795_free_ret:
1796 free(oldcontents);
1797 free(curcontents);
1798 return ret;
1799}
1800
1801/**
1802 * @brief Verify the ROM chip's contents with the specified image.
1803 *
1804 * If a layout is set in the specified flash context, only included regions
1805 * will be verified.
1806 *
1807 * @param flashctx The context of the flash chip.
1808 * @param buffer Source buffer to verify with.
1809 * @param buffer_len Size of source buffer in bytes.
1810 * @return 0 on success,
1811 * 3 if the chip's contents don't match,
1812 * 2 if buffer_len doesn't match the size of the flash chip,
1813 * or 1 on any other failure.
1814 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001815int flashprog_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001816{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001817 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001818 const size_t flash_size = flashctx->chip->total_size * 1024;
1819
1820 if (buffer_len != flash_size)
1821 return 2;
1822
1823 const uint8_t *const newcontents = buffer;
1824 uint8_t *const curcontents = malloc(flash_size);
1825 if (!curcontents) {
1826 msg_gerr("Out of memory!\n");
1827 return 1;
1828 }
1829
1830 int ret = 1;
1831
1832 if (prepare_flash_access(flashctx, false, false, false, true))
1833 goto _free_ret;
1834
1835 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001836 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001837 if (!ret)
1838 msg_cinfo("VERIFIED.\n");
1839
1840 finalize_flash_access(flashctx);
1841_free_ret:
1842 free(curcontents);
1843 return ret;
1844}
1845
Nico Huberc3b02dc2023-08-12 01:13:45 +02001846/** @} */ /* end flashprog-ops */