blob: bca0b20d94144e1aecf5889aa7f4fae58b058da4 [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Nico Huber7af0e792016-04-29 16:40:15 +02008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000010 *
Uwe Hermannd1107642007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000015 *
Uwe Hermannd1107642007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000020 */
21
Felix Singerf25447e2022-08-19 02:44:28 +020022#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000023#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000024#include <sys/types.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000025#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000026#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000027#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000028#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000029#include <ctype.h>
Edward O'Callaghan3b64d812022-08-12 13:07:51 +100030
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000031#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000032#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000033#include "programmer.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010034#include "hwaccess_physmap.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010035#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000036
Nico Huberc3b02dc2023-08-12 01:13:45 +020037const char flashprog_version[] = FLASHPROG_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000038const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000039
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020040static const struct programmer_entry *programmer = NULL;
Nico Huber6a2ebeb2022-08-26 11:36:48 +020041static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000042
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000043/* If nonzero, used as the start address of bottom-aligned flash. */
44unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000045
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000046/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020047bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000048
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000049#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000050static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000051/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000052static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000053 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000054 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000055} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000056/* Initialize to 0 to make sure nobody registers a shutdown function before
57 * programmer init.
58 */
Felix Singerf25447e2022-08-19 02:44:28 +020059static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000060
Stefan Taunerc4f44df2013-08-12 22:58:43 +000061/* Did we change something or was every erase/write skipped (if any)? */
62static bool all_skipped = true;
63
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000064static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000065
Stefan Tauner2a1ed772014-08-31 00:09:21 +000066int shutdown_free(void *data)
67{
68 free(data);
69 return 0;
70}
71
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000072/* Register a function to be executed on programmer shutdown.
73 * The advantage over atexit() is that you can supply a void pointer which will
74 * be used as parameter to the registered function upon programmer shutdown.
75 * This pointer can point to arbitrary data used by said function, e.g. undo
76 * information for GPIO settings etc. If unneeded, set data=NULL.
77 * Please note that the first (void *data) belongs to the function signature of
78 * the function passed as first parameter.
79 */
David Hendricks8bb20212011-06-14 01:35:36 +000080int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000081{
82 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000083 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000084 SHUTDOWN_MAXFN);
85 return 1;
86 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000087 if (!may_register_shutdown) {
88 msg_perr("Tried to register a shutdown function before "
89 "programmer init.\n");
90 return 1;
91 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000092 shutdown_fn[shutdown_fn_count].func = function;
93 shutdown_fn[shutdown_fn_count].data = data;
94 shutdown_fn_count++;
95
96 return 0;
97}
98
Nikolai Artemiev4ad48642020-11-05 13:54:27 +110099int register_chip_restore(chip_restore_fn_cb_t func,
100 struct flashctx *flash, uint8_t status)
101{
102 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
103 msg_perr("Tried to register more than %i chip restore"
104 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
105 return 1;
106 }
107 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
108 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
109 flash->chip_restore_fn_count++;
110
111 return 0;
112}
113
114static int deregister_chip_restore(struct flashctx *flash)
115{
116 int rc = 0;
117
118 while (flash->chip_restore_fn_count > 0) {
119 int i = --flash->chip_restore_fn_count;
120 rc |= flash->chip_restore_fn[i].func(
121 flash, flash->chip_restore_fn[i].status);
122 }
123
124 return rc;
125}
126
Nico Huber2b66ad92023-01-11 20:15:15 +0100127int programmer_init(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000128{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000129 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000130
Nico Huber2b66ad92023-01-11 20:15:15 +0100131 if (prog == NULL || prog->driver == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000132 msg_perr("Invalid programmer specified!\n");
133 return -1;
134 }
Nico Huber2b66ad92023-01-11 20:15:15 +0100135 programmer = prog->driver;
136 programmer_param = prog->param;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000137 /* Initialize all programmer specific data. */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000138 /* Default to top aligned flash at 4 GB. */
139 flashbase = 0;
140 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200141 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000142 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200143 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000144
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200145 msg_pdbg("Initializing %s programmer\n", programmer->name);
Nico Hubere3a26882023-01-11 21:45:51 +0100146 ret = programmer->init(prog);
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000147 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000148 if (ret != 0) {
149 /* It is quite possible that any unhandled programmer parameter would have been valid,
150 * but an error in actual programmer init happened before the parameter was evaluated.
151 */
152 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
153 programmer_param);
154 } else {
155 /* Actual programmer init was successful, but the user specified an invalid or unusable
156 * (for the current programmer configuration) parameter.
157 */
158 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
159 msg_perr("Aborting.\n");
160 ret = ERROR_FATAL;
161 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000162 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200163 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000164 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000165}
166
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000167/** Calls registered shutdown functions and resets internal programmer-related variables.
168 * Calling it is safe even without previous initialization, but further interactions with programmer support
169 * require a call to programmer_init() (afterwards).
170 *
171 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Nico Huber2b66ad92023-01-11 20:15:15 +0100172int programmer_shutdown(struct flashprog_programmer *const prog)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000173{
David Hendricks8bb20212011-06-14 01:35:36 +0000174 int ret = 0;
175
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000176 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200177 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000178 while (shutdown_fn_count > 0) {
179 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000180 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000181 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000182 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000183
David Hendricks8bb20212011-06-14 01:35:36 +0000184 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000185}
186
Stefan Taunerf80419c2014-05-02 15:41:42 +0000187void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000188{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000189 if (usecs > 0) {
190 if (programmer->delay)
191 programmer->delay(usecs);
192 else
193 internal_delay(usecs);
194 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000195}
196
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000197int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
198 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000199{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000200 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000201
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000202 return 0;
203}
204
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000205/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000206 * It will look for needle with a subsequent '=' in haystack, return a copy of
207 * needle and remove everything from the first occurrence of needle to the next
208 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000209 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200210static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000211{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000212 char *param_pos, *opt_pos, *rest;
213 char *opt = NULL;
214 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000215 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000216
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000217 needlelen = strlen(needle);
218 if (!needlelen) {
219 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200220 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000221 return NULL;
222 }
223 /* No programmer parameters given. */
224 if (*haystack == NULL)
225 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000226 param_pos = strstr(*haystack, needle);
227 do {
228 if (!param_pos)
229 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000230 /* Needle followed by '='? */
231 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000232 /* Beginning of the string? */
233 if (param_pos == *haystack)
234 break;
235 /* After a delimiter? */
236 if (strchr(delim, *(param_pos - 1)))
237 break;
238 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000239 /* Continue searching. */
240 param_pos++;
241 param_pos = strstr(param_pos, needle);
242 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000243
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000244 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000245 /* Get the string after needle and '='. */
246 opt_pos = param_pos + needlelen + 1;
247 optlen = strcspn(opt_pos, delim);
248 /* Return an empty string if the parameter was empty. */
249 opt = malloc(optlen + 1);
250 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000251 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000252 exit(1);
253 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000254 strncpy(opt, opt_pos, optlen);
255 opt[optlen] = '\0';
256 rest = opt_pos + optlen;
257 /* Skip all delimiters after the current parameter. */
258 rest += strspn(rest, delim);
259 memmove(param_pos, rest, strlen(rest) + 1);
260 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000261 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000262
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000263 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000264}
265
Stefan Tauner66652442011-06-26 17:38:17 +0000266char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000267{
268 return extract_param(&programmer_param, param_name, ",");
269}
270
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000271/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000272static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000273{
274 unsigned int usable_erasefunctions = 0;
275 int k;
276 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
277 if (!check_block_eraser(flash, k, 0))
278 usable_erasefunctions++;
279 }
280 return usable_erasefunctions;
281}
282
Mark Marshallf20b7be2014-05-09 21:16:21 +0000283static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000284{
285 int ret = 0, failcount = 0;
286 unsigned int i;
287 for (i = 0; i < len; i++) {
288 if (wantbuf[i] != havebuf[i]) {
289 /* Only print the first failure. */
290 if (!failcount++)
291 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
292 start + i, wantbuf[i], havebuf[i]);
293 }
294 }
295 if (failcount) {
296 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
297 start, start + len - 1, failcount);
298 ret = -1;
299 }
300 return ret;
301}
302
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000303/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600304static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000305{
306 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300307 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000308
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100309 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000310 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100311 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100312 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000313 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300314 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000315 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100316
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000317 free(cmpbuf);
318 return ret;
319}
320
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000321/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000322 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000323 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000324 * @start offset to the base address of the flash chip
325 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000326 * @return 0 for success, -1 for failure
327 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000328int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000329{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000330 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000331 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000332
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100333 if (start + len > flash->chip->total_size * 1024) {
334 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
335 " total_size 0x%x\n", __func__, start, len,
336 flash->chip->total_size * 1024);
337 return -1;
338 }
339
Stefan Taunerdf64a422014-05-27 00:06:14 +0000340 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000341 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100342 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000343 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000344 }
345
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100346 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000347 if (ret) {
348 msg_gerr("Verification impossible because read failed "
349 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000350 ret = -1;
351 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000352 }
353
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000354 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000355out_free:
356 free(readbuf);
357 return ret;
358}
359
Nico Huber3ac761c2023-01-16 02:43:17 +0100360size_t gran_to_bytes(const enum write_granularity gran)
Nico Huberb77607f2023-01-16 02:25:45 +0100361{
362 switch (gran) {
363 case write_gran_1bit: return 1;
364 case write_gran_1byte: return 1;
365 case write_gran_1byte_implicit_erase: return 1;
366 case write_gran_128bytes: return 128;
367 case write_gran_256bytes: return 256;
368 case write_gran_264bytes: return 264;
369 case write_gran_512bytes: return 512;
370 case write_gran_528bytes: return 528;
371 case write_gran_1024bytes: return 1024;
372 case write_gran_1056bytes: return 1056;
373 default: return 0;
374 }
375}
376
Stefan Tauner02437452013-04-01 19:34:53 +0000377/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300378static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
379 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000380{
381 unsigned int i, j, limit;
382 for (j = 0; j < len / gran; j++) {
383 limit = min (gran, len - j * gran);
384 /* Are 'have' and 'want' identical? */
385 if (!memcmp(have + j * gran, want + j * gran, limit))
386 continue;
387 /* have needs to be in erased state. */
388 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300389 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000390 return 1;
391 }
392 return 0;
393}
394
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000395/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000396 * Check if the buffer @have can be programmed to the content of @want without
397 * erasing. This is only possible if all chunks of size @gran are either kept
398 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000399 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000400 * Warning: This function assumes that @have and @want point to naturally
401 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000402 *
403 * @have buffer with current content
404 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000405 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000406 * @gran write granularity (enum, not count)
407 * @return 0 if no erase is needed, 1 otherwise
408 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000409static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
410 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000411{
Stefan Tauner02437452013-04-01 19:34:53 +0000412 unsigned int i;
Nico Huberb77607f2023-01-16 02:25:45 +0100413 size_t stride;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000414
415 switch (gran) {
416 case write_gran_1bit:
Nico Huberb77607f2023-01-16 02:25:45 +0100417 for (i = 0; i < len; i++) {
418 if ((have[i] & want[i]) != want[i])
419 return 1;
420 }
421 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000422 case write_gran_1byte:
Nico Huberb77607f2023-01-16 02:25:45 +0100423 for (i = 0; i < len; i++) {
424 if ((have[i] != want[i]) && (have[i] != erased_value))
425 return 1;
426 }
427 return 0;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000428 case write_gran_1byte_implicit_erase:
429 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
Nico Huberb77607f2023-01-16 02:25:45 +0100430 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000431 default:
Nico Huberb77607f2023-01-16 02:25:45 +0100432 stride = gran_to_bytes(gran);
433 if (stride) {
434 return need_erase_gran_bytes(have, want, len, stride, erased_value);
435 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000436 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200437 "flashprog@flashprog.org\n", __func__);
Nico Huberb77607f2023-01-16 02:25:45 +0100438 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000439 }
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000440}
441
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000442/**
443 * Check if the buffer @have needs to be programmed to get the content of @want.
444 * If yes, return 1 and fill in first_start with the start address of the
445 * write operation and first_len with the length of the first to-be-written
446 * chunk. If not, return 0 and leave first_start and first_len undefined.
447 *
448 * Warning: This function assumes that @have and @want point to naturally
449 * aligned regions.
450 *
451 * @have buffer with current content
452 * @want buffer with desired content
453 * @len length of the checked area
454 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000455 * @first_start offset of the first byte which needs to be written (passed in
456 * value is increased by the offset of the first needed write
457 * relative to have/want or unchanged if no write is needed)
458 * @return length of the first contiguous area which needs to be written
459 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000460 *
461 * FIXME: This function needs a parameter which tells it about coalescing
462 * in relation to the max write length of the programmer and the max write
463 * length of the chip.
464 */
Nico Huber3b9c86d2023-01-15 22:58:06 +0100465static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, chipsize_t len,
466 chipoff_t *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000467{
Felix Singerf25447e2022-08-19 02:44:28 +0200468 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000469 unsigned int rel_start = 0, first_len = 0;
Nico Huberb77607f2023-01-16 02:25:45 +0100470 unsigned int i, limit;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000471
Nico Huberb77607f2023-01-16 02:25:45 +0100472 const size_t stride = gran_to_bytes(gran);
473 if (!stride) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000474 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200475 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000476 /* Claim that no write was needed. A write with unknown
477 * granularity is too dangerous to try.
478 */
479 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000480 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000481 for (i = 0; i < len / stride; i++) {
482 limit = min(stride, len - i * stride);
483 /* Are 'have' and 'want' identical? */
484 if (memcmp(have + i * stride, want + i * stride, limit)) {
485 if (!need_write) {
486 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200487 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000488 rel_start = i * stride;
489 }
490 } else {
491 if (need_write) {
492 /* First location where have and want
493 * do not differ anymore.
494 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000495 break;
496 }
497 }
498 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000499 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000500 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000501 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000502 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000503}
504
Nico Huber2d625722016-05-03 10:48:02 +0200505/*
506 * Return a string corresponding to the bustype parameter.
507 * Memory is obtained with malloc() and must be freed with free() by the caller.
508 */
509char *flashbuses_to_text(enum chipbustype bustype)
510{
511 char *ret = calloc(1, 1);
512 /*
513 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
514 * will cease to exist and should be eliminated here as well.
515 */
516 if (bustype == BUS_NONSPI) {
517 ret = strcat_realloc(ret, "Non-SPI, ");
518 } else {
519 if (bustype & BUS_PARALLEL)
520 ret = strcat_realloc(ret, "Parallel, ");
521 if (bustype & BUS_LPC)
522 ret = strcat_realloc(ret, "LPC, ");
523 if (bustype & BUS_FWH)
524 ret = strcat_realloc(ret, "FWH, ");
525 if (bustype & BUS_SPI)
526 ret = strcat_realloc(ret, "SPI, ");
527 if (bustype & BUS_PROG)
528 ret = strcat_realloc(ret, "Programmer-specific, ");
529 if (bustype == BUS_NONE)
530 ret = strcat_realloc(ret, "None, ");
531 }
532 /* Kill last comma. */
533 ret[strlen(ret) - 2] = '\0';
534 ret = realloc(ret, strlen(ret) + 1);
535 return ret;
536}
537
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100538static int init_default_layout(struct flashctx *flash)
539{
540 /* Fill default layout covering the whole chip. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200541 if (flashprog_layout_new(&flash->default_layout) ||
542 flashprog_layout_add_region(flash->default_layout,
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100543 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huberc3b02dc2023-08-12 01:13:45 +0200544 flashprog_layout_include_region(flash->default_layout, "complete flash"))
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100545 return -1;
546 return 0;
547}
548
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000549int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000550{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000551 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000552 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000553 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000554
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000555 for (chip = flashchips + startchip; chip && chip->name; chip++) {
556 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000557 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000558 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000559 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000560 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300561 /* Only probe for SPI25 chips by default. */
562 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
563 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000564 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
565 if (!chip->probe && !force) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200566 msg_gdbg("failed! flashprog has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000567 continue;
568 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000569
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000570 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200571 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000572 if (!flash->chip) {
573 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100574 return -1;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000575 }
Angel Pons7e134562021-06-07 13:29:13 +0200576 *flash->chip = *chip;
Nico Huber9a11cbf2023-01-13 01:19:07 +0100577 flash->mst.par = &mst->par; /* both `mst` are unions, so we need only one pointer */
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000578
Nico Huber9eec4072023-01-12 01:17:30 +0100579 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_PROBE))
580 goto free_chip;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000581
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000582 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
583 * is only called with force=1 after normal probing failed.
584 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000585 if (force)
586 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000587
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000588 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000589 goto notfound;
590
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000591 /* If this is the first chip found, accept it.
592 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000593 * a non-generic match. SFDP and CFI are generic matches.
594 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000595 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000596 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000597 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000598 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000599 msg_cinfo("===\n"
600 "SFDP has autodetected a flash chip which is "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200601 "not natively supported by flashprog yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000602 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000603 msg_cinfo("The standard operations read and "
604 "verify should work, but to support "
605 "erase, write and all other "
606 "possible features");
607 else
608 msg_cinfo("All standard operations (read, "
609 "verify, erase and write) should "
610 "work, but to support all possible "
611 "features");
612
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000613 msg_cinfo(" we need to add them manually.\n"
614 "You can help us by mailing us the output of the following command to "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200615 "flashprog@flashprog.org:\n"
616 "'flashprog -VV [plus the -p/--programmer parameter]'\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000617 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000618 "===\n");
619 }
620
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000621 /* First flash chip detected on this bus. */
622 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000623 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000624 /* Not the first flash chip detected on this bus, but not a generic match either. */
625 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
626 break;
627 /* 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 +0000628notfound:
Nico Huber9eec4072023-01-12 01:17:30 +0100629 if (flash->chip->finish_access)
630 flash->chip->finish_access(flash);
631free_chip:
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000632 free(flash->chip);
633 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000634 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000635
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000636 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000637 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000638
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100639 if (init_default_layout(flash) < 0)
640 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000641
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000642 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000643 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
644 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000645 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000646#if CONFIG_INTERNAL == 1
Nico Huber0e76d992023-01-12 20:22:55 +0100647 if (flash->physical_memory != 0 && mst->par.map_flash == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000648 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
649 PRIxPTR_WIDTH, flash->physical_memory);
650 else
651#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200652 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000653
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000654 /* Flash registers may more likely not be mapped if the chip was forced.
655 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000656 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000657 if (flash->chip->printlock)
658 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000659
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000660 /* Get out of the way for later runs. */
Nico Huber9eec4072023-01-12 01:17:30 +0100661 if (flash->chip->finish_access)
662 flash->chip->finish_access(flash);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000663
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000664 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000665 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000666}
667
Stefan Tauner96658be2014-05-26 22:05:31 +0000668/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000669static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000670{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000671 int i, j, k;
672 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530673 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000674
675 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
676 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000677 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530678 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000679
680 for (i = 0; i < NUM_ERASEREGIONS; i++) {
681 /* Blocks with zero size are bugs in flashchips.c. */
682 if (eraser.eraseblocks[i].count &&
683 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000684 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200685 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000686 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000687 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000688 }
689 /* Blocks with zero count are bugs in flashchips.c. */
690 if (!eraser.eraseblocks[i].count &&
691 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000692 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200693 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000694 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000695 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000696 }
697 done += eraser.eraseblocks[i].count *
698 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530699 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000700 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000701 /* Empty eraseblock definition with erase function. */
702 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000703 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000704 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000705 if (!done)
706 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000707 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000708 msg_gerr("ERROR: Flash chip %s erase function %i "
709 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000710 " expected 0x%06x bytes.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200711 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000712 chip->name, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000713 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000714 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000715 if (!eraser.block_erase)
716 continue;
717 /* Check if there are identical erase functions for different
718 * layouts. That would imply "magic" erase functions. The
719 * easiest way to check this is with function pointers.
720 */
Uwe Hermann43959702010-03-13 17:28:29 +0000721 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000722 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000723 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000724 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200725 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000726 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000727 ret = 1;
728 }
Uwe Hermann43959702010-03-13 17:28:29 +0000729 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530730 if(curr_eraseblock_count > prev_eraseblock_count)
731 {
Nico Huberac90af62022-12-18 00:22:47 +0000732 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200733 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000734 chip->name, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530735 ret = 1;
736 }
737 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000738 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000739 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000740}
741
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000742static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000743{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000744 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000745
746 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
747 if (log)
748 msg_cdbg("not defined. ");
749 return 1;
750 }
751 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
752 if (log)
753 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000754 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000755 return 1;
756 }
757 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
758 if (log)
759 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000760 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000761 return 1;
762 }
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530763
764 if (flash->chip->bustype == BUS_SPI) {
Nico Huber13389362024-03-05 18:35:30 +0100765 bool native_4ba;
Nico Huber13f97a52023-01-14 23:55:06 +0100766 int i;
Nico Huber13389362024-03-05 18:35:30 +0100767
768 const uint8_t *opcode = spi_get_opcode_from_erasefn(eraser.block_erase, &native_4ba);
Nico Huber13f97a52023-01-14 23:55:06 +0100769 for (i = 0; opcode[i]; i++) {
Nico Huber13389362024-03-05 18:35:30 +0100770 if ((native_4ba && !spi_master_4ba(flash)) ||
Nico Huber9a11cbf2023-01-13 01:19:07 +0100771 !flash->mst.spi->probe_opcode(flash, opcode[i])) {
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530772 if (log)
773 msg_cdbg("block erase function and layout found "
774 "but SPI master doesn't support the function. ");
775 return 1;
776 }
777 }
778 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000779 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000780 return 0;
781}
782
Nico Huber7af0e792016-04-29 16:40:15 +0200783/**
784 * @brief Reads the included layout regions into a buffer.
785 *
786 * If there is no layout set in the given flash context, the whole chip will
787 * be read.
788 *
789 * @param flashctx Flash context to be used.
790 * @param buffer Buffer of full chip size to read into.
791 * @return 0 on success,
792 * 1 if any read fails.
793 */
794static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
795{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200796 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200797 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200798
Nico Huber5ca55232019-06-15 22:29:08 +0200799 while ((entry = layout_next_included(layout, entry))) {
800 const chipoff_t region_start = entry->start;
801 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200802
803 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
804 return 1;
805 }
806 return 0;
807}
808
Nico Huber7af0e792016-04-29 16:40:15 +0200809/**
810 * @private
811 *
812 * For read-erase-write, `curcontents` and `newcontents` shall point
813 * to buffers of the chip's size. Both are supposed to be prefilled
814 * with at least the included layout regions of the current flash
815 * contents (`curcontents`) and the data to be written to the flash
816 * (`newcontents`).
817 *
818 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
819 *
820 * The `chipoff_t` values are used internally by `walk_by_layout()`.
821 */
822struct walk_info {
823 uint8_t *curcontents;
824 const uint8_t *newcontents;
825 chipoff_t region_start;
826 chipoff_t region_end;
827 chipoff_t erase_start;
828 chipoff_t erase_end;
829};
Nico Huber3b9c86d2023-01-15 22:58:06 +0100830
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530831struct eraseblock_data {
832 chipoff_t start_addr;
833 chipoff_t end_addr;
834 bool selected;
835 size_t block_num;
836 size_t first_sub_block_index;
837 size_t last_sub_block_index;
838};
839
840struct erase_layout {
841 struct eraseblock_data* layout_list;
842 size_t block_count;
843 const struct block_eraser *eraser;
844};
845
Nico Huber5ff6fdc2023-01-15 23:43:12 +0100846static bool explicit_erase(const struct walk_info *const info)
847{
848 /* For explicit erase, we are called without new contents. */
849 return !info->newcontents;
850}
851
Nico Huberd96e7032023-01-14 22:31:48 +0100852static size_t calculate_block_count(const struct block_eraser *const eraser)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530853{
Nico Huberd96e7032023-01-14 22:31:48 +0100854 size_t block_count = 0, i;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530855
Nico Huberd96e7032023-01-14 22:31:48 +0100856 for (i = 0; i < ARRAY_SIZE(eraser->eraseblocks); ++i)
857 block_count += eraser->eraseblocks[i].count;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530858
859 return block_count;
860}
861
862static void init_eraseblock(struct erase_layout *layout, size_t idx, size_t block_num,
863 chipoff_t start_addr, chipoff_t end_addr, size_t *sub_block_index)
864{
865 struct eraseblock_data *edata = &layout[idx].layout_list[block_num];
866 edata->start_addr = start_addr;
867 edata->end_addr = end_addr;
868 edata->selected = false;
869 edata->block_num = block_num;
870
871 if (!idx)
872 return;
Nico Hubera02df332023-01-14 23:06:27 +0100873 const struct erase_layout *const sub_layout = &layout[idx - 1];
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530874
875 edata->first_sub_block_index = *sub_block_index;
Nico Hubera02df332023-01-14 23:06:27 +0100876 for (; *sub_block_index < sub_layout->block_count; ++*sub_block_index) {
877 if (sub_layout->layout_list[*sub_block_index].end_addr > end_addr)
878 break;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530879 }
880 edata->last_sub_block_index = *sub_block_index - 1;
881}
882
883/*
884 * @brief Function to free the created erase_layout
885 *
886 * @param layout pointer to allocated layout
887 * @param erasefn_count number of erase functions for which the layout was created
888 *
889 */
890static void free_erase_layout(struct erase_layout *layout, unsigned int erasefn_count)
891{
Nico Huber13f97a52023-01-14 23:55:06 +0100892 size_t i;
893
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530894 if (!layout)
895 return;
Nico Huber13f97a52023-01-14 23:55:06 +0100896 for (i = 0; i < erasefn_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530897 free(layout[i].layout_list);
898 }
899 free(layout);
900}
901
902/*
903 * @brief Function to create an erase layout
904 *
905 * @param flashctx flash context
906 * @param e_layout address to the pointer to store the layout
907 * @return 0 on success,
908 * -1 if layout creation fails
909 *
910 * This function creates a layout of which erase functions erase which regions
911 * of the flash chip. This helps to optimally select the erase functions for
912 * erase/write operations.
913 */
Nico Huberc09fca42023-01-29 15:58:09 +0100914static int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530915{
916 const struct flashchip *chip = flashctx->chip;
917 const size_t erasefn_count = count_usable_erasers(flashctx);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530918
919 if (!erasefn_count) {
920 msg_gerr("No erase functions supported\n");
921 return 0;
922 }
923
Nico Huberd34864b2023-01-14 23:47:19 +0100924 struct erase_layout *layout = calloc(erasefn_count, sizeof(struct erase_layout));
925 if (!layout) {
926 msg_gerr("Out of memory!\n");
927 return -1;
928 }
929
Nico Huber13f97a52023-01-14 23:55:06 +0100930 size_t layout_idx = 0, eraser_idx;
931 for (eraser_idx = 0; eraser_idx < NUM_ERASEFUNCTIONS; eraser_idx++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530932 if (check_block_eraser(flashctx, eraser_idx, 0))
933 continue;
934
935 layout[layout_idx].eraser = &chip->block_erasers[eraser_idx];
Nico Huberd96e7032023-01-14 22:31:48 +0100936 const size_t block_count = calculate_block_count(&chip->block_erasers[eraser_idx]);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530937 size_t sub_block_index = 0;
938
939 layout[layout_idx].block_count = block_count;
940 layout[layout_idx].layout_list = (struct eraseblock_data *)calloc(block_count,
941 sizeof(struct eraseblock_data));
942
943 if (!layout[layout_idx].layout_list) {
944 free_erase_layout(layout, layout_idx);
945 return -1;
946 }
947
948 size_t block_num = 0;
949 chipoff_t start_addr = 0;
950
Nico Huber13f97a52023-01-14 23:55:06 +0100951 int i;
952 for (i = 0; block_num < block_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530953 const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];
954
Nico Huber13f97a52023-01-14 23:55:06 +0100955 size_t num;
956 for (num = 0; num < block->count; num++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530957 chipoff_t end_addr = start_addr + block->size - 1;
958 init_eraseblock(layout, layout_idx, block_num,
959 start_addr, end_addr, &sub_block_index);
960 block_num += 1;
961 start_addr = end_addr + 1;
962 }
963 }
964 layout_idx++;
965 }
966
967 *e_layout = layout;
968 return layout_idx;
969}
970
Aarya Chaumald33bea42022-07-14 12:51:14 +0530971/*
972 * @brief Function to select the list of sectors that need erasing
973 *
974 * @param flashctx flash context
975 * @param layout erase layout
976 * @param findex index of the erase function
977 * @param block_num index of the block to erase according to the erase function index
Nico Huber00d1b9f2023-01-29 15:07:33 +0100978 * @param info current info from walking the regions
Aarya Chaumald33bea42022-07-14 12:51:14 +0530979 */
Nico Huberb11b72c2023-01-29 15:33:11 +0100980static void select_erase_functions_rec(const struct flashctx *flashctx, const struct erase_layout *layout,
Nico Huber00d1b9f2023-01-29 15:07:33 +0100981 size_t findex, size_t block_num, const struct walk_info *info)
Aarya Chaumald33bea42022-07-14 12:51:14 +0530982{
983 struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
984 if (!findex) {
Nico Hubercf6ff0a2023-01-29 15:45:06 +0100985 if (ll->start_addr <= info->region_end && ll->end_addr >= info->region_start) {
Nico Huber1494f8e2023-01-29 15:48:00 +0100986 if (explicit_erase(info)) {
987 ll->selected = true;
988 return;
989 }
Nico Hubera6212482023-01-29 15:39:26 +0100990 const chipoff_t write_start = MAX(info->region_start, ll->start_addr);
991 const chipoff_t write_end = MIN(info->region_end, ll->end_addr);
992 const chipsize_t write_len = write_end - write_start + 1;
993 const uint8_t erased_value = ERASED_VALUE(flashctx);
Nico Huber00d1b9f2023-01-29 15:07:33 +0100994 ll->selected = need_erase(
Nico Hubera6212482023-01-29 15:39:26 +0100995 info->curcontents + write_start, info->newcontents + write_start,
996 write_len, flashctx->chip->gran, erased_value);
Aarya Chaumald33bea42022-07-14 12:51:14 +0530997 }
998 } else {
999 int count = 0;
1000 const int sub_block_start = ll->first_sub_block_index;
1001 const int sub_block_end = ll->last_sub_block_index;
1002
Nico Huber13f97a52023-01-14 23:55:06 +01001003 int j;
1004 for (j = sub_block_start; j <= sub_block_end; j++) {
Nico Huberb11b72c2023-01-29 15:33:11 +01001005 select_erase_functions_rec(flashctx, layout, findex - 1, j, info);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301006 if (layout[findex - 1].layout_list[j].selected)
1007 count++;
1008 }
1009
1010 const int total_blocks = sub_block_end - sub_block_start + 1;
1011 if (count && count > total_blocks/2) {
Nico Huber00d1b9f2023-01-29 15:07:33 +01001012 if (ll->start_addr >= info->region_start && ll->end_addr <= info->region_end) {
Nico Huber13f97a52023-01-14 23:55:06 +01001013 for (j = sub_block_start; j <= sub_block_end; j++)
Aarya Chaumald33bea42022-07-14 12:51:14 +05301014 layout[findex - 1].layout_list[j].selected = false;
1015 ll->selected = true;
1016 }
1017 }
1018 }
1019}
1020
Nico Huberc09fca42023-01-29 15:58:09 +01001021static void select_erase_functions(const struct flashctx *flashctx, const struct erase_layout *layout,
Nico Huberb11b72c2023-01-29 15:33:11 +01001022 size_t erasefn_count, const struct walk_info *info)
1023{
1024 size_t block_num;
1025 for (block_num = 0; block_num < layout[erasefn_count - 1].block_count; ++block_num)
1026 select_erase_functions_rec(flashctx, layout, erasefn_count - 1, block_num, info);
1027}
1028
Nico Huber3b9c86d2023-01-15 22:58:06 +01001029static int write_range(struct flashctx *const flashctx, const chipoff_t flash_offset,
1030 const uint8_t *const curcontents, const uint8_t *const newcontents,
1031 const chipsize_t len, bool *const skipped)
1032{
1033 unsigned int writecount = 0;
1034 chipoff_t starthere = 0;
1035 chipsize_t lenhere = 0;
1036
1037 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1038 len - starthere, &starthere, flashctx->chip->gran))) {
1039 if (!writecount++)
1040 msg_cdbg("W");
1041 if (flashctx->chip->write(flashctx, newcontents + starthere,
1042 flash_offset + starthere, lenhere))
1043 return 1;
1044 starthere += lenhere;
1045 if (skipped)
1046 *skipped = false;
1047 }
1048 return 0;
1049}
1050
1051typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
Nico Huber7af0e792016-04-29 16:40:15 +02001052/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1053typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1054
1055static int walk_eraseblocks(struct flashctx *const flashctx,
Nico Huberc09fca42023-01-29 15:58:09 +01001056 struct erase_layout *const layouts,
1057 const size_t layout_count,
Nico Huber7af0e792016-04-29 16:40:15 +02001058 struct walk_info *const info,
Nico Huberc09fca42023-01-29 15:58:09 +01001059 const per_blockfn_t per_blockfn)
Nico Huber7af0e792016-04-29 16:40:15 +02001060{
1061 int ret;
1062 size_t i, j;
1063 bool first = true;
Nico Huber7af0e792016-04-29 16:40:15 +02001064
Nico Huberc09fca42023-01-29 15:58:09 +01001065 for (i = 0; i < layout_count; ++i) {
1066 const struct erase_layout *const layout = &layouts[i];
Nico Huber7af0e792016-04-29 16:40:15 +02001067
Nico Huberc09fca42023-01-29 15:58:09 +01001068 for (j = 0; j < layout->block_count; ++j) {
1069 struct eraseblock_data *const eb = &layout->layout_list[j];
1070
1071 if (eb->start_addr > info->region_end)
Nico Huber7af0e792016-04-29 16:40:15 +02001072 break;
Nico Huberc09fca42023-01-29 15:58:09 +01001073 if (eb->end_addr < info->region_start)
1074 continue;
1075 if (!eb->selected)
1076 continue;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001077
Nico Huber7af0e792016-04-29 16:40:15 +02001078 /* Print this for every block except the first one. */
1079 if (first)
1080 first = false;
1081 else
1082 msg_cdbg(", ");
Nico Huberc09fca42023-01-29 15:58:09 +01001083 msg_cdbg("0x%06x-0x%06x:", eb->start_addr, eb->end_addr);
Nico Huber7af0e792016-04-29 16:40:15 +02001084
Nico Huberc09fca42023-01-29 15:58:09 +01001085 info->erase_start = eb->start_addr;
1086 info->erase_end = eb->end_addr;
1087 ret = per_blockfn(flashctx, info, layout->eraser->block_erase);
Nico Huber7af0e792016-04-29 16:40:15 +02001088 if (ret)
1089 return ret;
Nico Huberc09fca42023-01-29 15:58:09 +01001090
1091 /* Clean the erase layout up for future use on other
1092 regions. `.selected` is the only field we alter. */
1093 eb->selected = false;
Nico Huber7af0e792016-04-29 16:40:15 +02001094 }
Nico Huber7af0e792016-04-29 16:40:15 +02001095 }
1096 msg_cdbg("\n");
1097 return 0;
1098}
1099
1100static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1101 const per_blockfn_t per_blockfn)
1102{
Nico Huberc09fca42023-01-29 15:58:09 +01001103 const bool do_erase = explicit_erase(info) || !(flashctx->chip->feature_bits & FEATURE_NO_ERASE);
Nico Huberc3b02dc2023-08-12 01:13:45 +02001104 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huberc09fca42023-01-29 15:58:09 +01001105 struct erase_layout *erase_layouts = NULL;
Nico Huber5ca55232019-06-15 22:29:08 +02001106 const struct romentry *entry = NULL;
Nico Huberc09fca42023-01-29 15:58:09 +01001107 int ret = 0, layout_count = 0;
Nico Huber7af0e792016-04-29 16:40:15 +02001108
1109 all_skipped = true;
1110 msg_cinfo("Erasing and writing flash chip... ");
1111
Nico Huberc09fca42023-01-29 15:58:09 +01001112 if (do_erase) {
1113 layout_count = create_erase_layout(flashctx, &erase_layouts);
1114 if (layout_count <= 0)
1115 return 1;
1116 }
1117
Nico Huber5ca55232019-06-15 22:29:08 +02001118 while ((entry = layout_next_included(layout, entry))) {
1119 info->region_start = entry->start;
1120 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001121
Nico Huberc09fca42023-01-29 15:58:09 +01001122 if (do_erase) {
1123 select_erase_functions(flashctx, erase_layouts, layout_count, info);
1124 ret = walk_eraseblocks(flashctx, erase_layouts, layout_count, info, per_blockfn);
1125 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001126 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001127 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001128 }
Nico Huber7af0e792016-04-29 16:40:15 +02001129 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001130
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001131 if (info->newcontents) {
1132 bool skipped = true;
1133 msg_cdbg("0x%06x-0x%06x:", info->region_start, info->region_end);
Nico Huberc09fca42023-01-29 15:58:09 +01001134 ret = write_range(flashctx, info->region_start,
1135 info->curcontents + info->region_start,
1136 info->newcontents + info->region_start,
1137 info->region_end + 1 - info->region_start, &skipped);
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 }
1142 if (skipped) {
1143 msg_cdbg("S\n");
1144 } else {
1145 msg_cdbg("\n");
1146 all_skipped = false;
1147 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001148 }
Nico Huber7af0e792016-04-29 16:40:15 +02001149 }
1150 if (all_skipped)
1151 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1152 msg_cinfo("Erase/write done.\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001153
1154free_ret:
1155 free_erase_layout(erase_layouts, layout_count);
1156 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001157}
1158
1159static int erase_block(struct flashctx *const flashctx,
1160 const struct walk_info *const info, const erasefn_t erasefn)
1161{
1162 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001163 const bool region_unaligned = info->region_start > info->erase_start ||
1164 info->erase_end > info->region_end;
1165 uint8_t *backup_contents = NULL, *erased_contents = NULL;
Nico Huberd34af7a2023-01-15 23:58:18 +01001166 int ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001167
Nico Huber6e61e0c2019-01-23 17:07:49 +01001168 /*
1169 * If the region is not erase-block aligned, merge current flash con-
1170 * tents into a new buffer `backup_contents`.
1171 */
1172 if (region_unaligned) {
1173 backup_contents = malloc(erase_len);
1174 erased_contents = malloc(erase_len);
1175 if (!backup_contents || !erased_contents) {
1176 msg_cerr("Out of memory!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001177 goto _free_ret;
1178 }
1179 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1180 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1181
1182 msg_cdbg("R");
1183 /* Merge data preceding the current region. */
1184 if (info->region_start > info->erase_start) {
1185 const chipoff_t start = info->erase_start;
1186 const chipsize_t len = info->region_start - info->erase_start;
1187 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1188 msg_cerr("Can't read! Aborting.\n");
1189 goto _free_ret;
1190 }
1191 }
1192 /* Merge data following the current region. */
1193 if (info->erase_end > info->region_end) {
1194 const chipoff_t start = info->region_end + 1;
1195 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1196 const chipsize_t len = info->erase_end - info->region_end;
1197 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1198 msg_cerr("Can't read! Aborting.\n");
1199 goto _free_ret;
1200 }
1201 }
1202 }
1203
Nico Huber7af0e792016-04-29 16:40:15 +02001204 all_skipped = false;
1205
1206 msg_cdbg("E");
1207 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001208 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001209 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1210 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001211 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001212 }
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001213 if (info->curcontents)
1214 memset(info->curcontents + info->erase_start, ERASED_VALUE(flashctx), erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001215
1216 if (region_unaligned) {
Nico Huber3b9c86d2023-01-15 22:58:06 +01001217 if (write_range(flashctx, info->erase_start, erased_contents, backup_contents, erase_len, NULL))
1218 goto _free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001219 if (info->curcontents)
1220 memcpy(info->curcontents + info->erase_start, backup_contents, erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001221 }
1222
1223 ret = 0;
1224
1225_free_ret:
1226 free(erased_contents);
1227 free(backup_contents);
1228 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001229}
1230
1231/**
1232 * @brief Erases the included layout regions.
1233 *
1234 * If there is no layout set in the given flash context, the whole chip will
1235 * be erased.
1236 *
1237 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001238 * @return 0 on success,
1239 * 1 if all available erase functions failed.
1240 */
Nico Huber454f6132012-12-10 13:34:10 +00001241static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001242{
1243 struct walk_info info = { 0 };
1244 return walk_by_layout(flashctx, &info, &erase_block);
1245}
1246
Nico Huber7af0e792016-04-29 16:40:15 +02001247/**
1248 * @brief Writes the included layout regions from a given image.
1249 *
1250 * If there is no layout set in the given flash context, the whole image
1251 * will be written.
1252 *
1253 * @param flashctx Flash context to be used.
1254 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1255 * @param newcontents The new image to be written.
1256 * @return 0 on success,
1257 * 1 if anything has gone wrong.
1258 */
Nico Huber454f6132012-12-10 13:34:10 +00001259static int write_by_layout(struct flashctx *const flashctx,
1260 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001261{
1262 struct walk_info info;
1263 info.curcontents = curcontents;
1264 info.newcontents = newcontents;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001265 return walk_by_layout(flashctx, &info, erase_block);
Nico Huber7af0e792016-04-29 16:40:15 +02001266}
1267
1268/**
1269 * @brief Compares the included layout regions with content from a buffer.
1270 *
1271 * If there is no layout set in the given flash context, the whole chip's
1272 * contents will be compared.
1273 *
1274 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001275 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001276 * @param curcontents A buffer of full chip size to read current chip contents into.
1277 * @param newcontents The new image to compare to.
1278 * @return 0 on success,
1279 * 1 if reading failed,
1280 * 3 if the contents don't match.
1281 */
Nico Huber74d09d42019-06-16 03:27:26 +02001282static int verify_by_layout(
1283 struct flashctx *const flashctx,
Nico Huberc3b02dc2023-08-12 01:13:45 +02001284 const struct flashprog_layout *const layout,
Nico Huber74d09d42019-06-16 03:27:26 +02001285 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001286{
Nico Huber5ca55232019-06-15 22:29:08 +02001287 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001288
Nico Huber5ca55232019-06-15 22:29:08 +02001289 while ((entry = layout_next_included(layout, entry))) {
1290 const chipoff_t region_start = entry->start;
1291 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001292
1293 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1294 return 1;
1295 if (compare_range(newcontents + region_start, curcontents + region_start,
1296 region_start, region_len))
1297 return 3;
1298 }
1299 return 0;
1300}
1301
Stefan Tauner136388f2013-07-15 10:47:53 +00001302static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001303{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001304 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001305#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001306 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001307 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001308 "chip. Please report this to the mailing list at flashprog@flashprog.org or\n"
1309 "on IRC (see https://www.flashprog.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001310 "-------------------------------------------------------------------------------\n"
1311 "You may now reboot or simply leave the machine running.\n");
1312 else
1313#endif
1314 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001315 "the programmer and the flash chip. If you think the error is caused by flashprog\n"
1316 "please report this to the mailing list at flashprog@flashprog.org or on IRC\n"
1317 "(see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001318}
1319
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001320void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001321{
Stefan Tauner136388f2013-07-15 10:47:53 +00001322 msg_gerr("Your flash chip is in an unknown state.\n");
1323#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001324 if (programmer == &programmer_internal)
Nico Huberc3b02dc2023-08-12 01:13:45 +02001325 msg_gerr("Get help on IRC (see https://www.flashprog.org/Contact) or mail\n"
1326 "flashprog@flashprog.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001327 "-------------------------------------------------------------------------------\n"
1328 "DO NOT REBOOT OR POWEROFF!\n");
1329 else
1330#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +02001331 msg_gerr("Please report this to the mailing list at flashprog@flashprog.org\n"
1332 "or on IRC (see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001333}
1334
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001335void list_programmers_linebreak(int startcol, int cols, int paren)
1336{
1337 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001338 int pnamelen;
1339 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001340 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001341 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001342
Thomas Heijligend45cb592021-05-19 14:12:18 +02001343 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001344 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001345 pnamelen = strlen(pname);
1346 if (remaining - pnamelen - 2 < 0) {
1347 if (firstline)
1348 firstline = 0;
1349 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001350 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001351 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001352 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001353 remaining = cols - startcol;
1354 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001355 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001356 remaining--;
1357 }
1358 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001359 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001360 remaining--;
1361 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001362 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001363 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001364 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001365 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001366 remaining--;
1367 } else {
1368 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001369 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001370 }
1371 }
1372}
1373
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001374int selfcheck(void)
1375{
Stefan Tauner96658be2014-05-26 22:05:31 +00001376 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001377 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001378
Thomas Heijligend45cb592021-05-19 14:12:18 +02001379 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001380 const struct programmer_entry *const p = programmer_table[i];
1381 if (p == NULL) {
1382 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1383 ret = 1;
1384 continue;
1385 }
1386 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001387 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1388 ret = 1;
1389 /* This might hide other problems with this programmer, but allows for better error
1390 * messages below without jumping through hoops. */
1391 continue;
1392 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001393 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001394 case USB:
1395 case PCI:
1396 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001397 if (p->devs.note == NULL) {
1398 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001399 break; /* This one has its device list stored separately. */
1400 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001401 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001402 ret = 1;
1403 }
1404 break;
1405 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001406 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001407 ret = 1;
1408 break;
1409 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001410 if (p->init == NULL) {
1411 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001412 ret = 1;
1413 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001414 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001415
1416 /* It would be favorable if we could check for the correct layout (especially termination) of various
1417 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1418 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1419 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1420 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1421 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001422 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001423 msg_gerr("Flashchips table miscompilation!\n");
1424 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001425 } else {
1426 for (i = 0; i < flashchips_size - 1; i++) {
1427 const struct flashchip *chip = &flashchips[i];
1428 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1429 ret = 1;
1430 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001431 "Please report a bug at flashprog@flashprog.org\n", i,
Stefan Tauner96658be2014-05-26 22:05:31 +00001432 chip->name == NULL ? "unnamed" : chip->name);
1433 }
1434 if (selfcheck_eraseblocks(chip)) {
1435 ret = 1;
1436 }
1437 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001438 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001439
Stefan Tauner600576b2014-06-12 22:57:36 +00001440#if CONFIG_INTERNAL == 1
1441 ret |= selfcheck_board_enables();
1442#endif
1443
Stefan Tauner96658be2014-05-26 22:05:31 +00001444 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001445 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001446}
1447
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001448/* FIXME: This function signature needs to be improved once prepare_flash_access()
1449 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001450 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001451static int chip_safety_check(const struct flashctx *flash, int force,
1452 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001453{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001454 const struct flashchip *chip = flash->chip;
1455
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001456 if (!programmer_may_write && (write_it || erase_it)) {
1457 msg_perr("Write/erase is not working yet on your programmer in "
1458 "its current configuration.\n");
1459 /* --force is the wrong approach, but it's the best we can do
1460 * until the generic programmer parameter parser is merged.
1461 */
1462 if (!force)
1463 return 1;
1464 msg_cerr("Continuing anyway.\n");
1465 }
1466
1467 if (read_it || erase_it || write_it || verify_it) {
1468 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001469 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001470 msg_cerr("Read is not working on this chip. ");
1471 if (!force)
1472 return 1;
1473 msg_cerr("Continuing anyway.\n");
1474 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001475 if (!chip->read) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001476 msg_cerr("flashprog has no read function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001477 "flash chip.\n");
1478 return 1;
1479 }
1480 }
1481 if (erase_it || write_it) {
1482 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001483 if (chip->tested.erase == NA) {
1484 msg_cerr("Erase is not possible on this chip.\n");
1485 return 1;
1486 }
1487 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001488 msg_cerr("Erase is not working on this chip. ");
1489 if (!force)
1490 return 1;
1491 msg_cerr("Continuing anyway.\n");
1492 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001493 if(count_usable_erasers(flash) == 0) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001494 msg_cerr("flashprog has no erase function for this "
Stefan Tauner5368dca2011-07-01 00:19:12 +00001495 "flash chip.\n");
1496 return 1;
1497 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001498 }
1499 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001500 if (chip->tested.write == NA) {
1501 msg_cerr("Write is not possible on this chip.\n");
1502 return 1;
1503 }
1504 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001505 msg_cerr("Write is not working on this chip. ");
1506 if (!force)
1507 return 1;
1508 msg_cerr("Continuing anyway.\n");
1509 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001510 if (!chip->write) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001511 msg_cerr("flashprog has no write function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001512 "flash chip.\n");
1513 return 1;
1514 }
1515 }
1516 return 0;
1517}
1518
Nico Huber305f4172013-06-14 11:55:26 +02001519int prepare_flash_access(struct flashctx *const flash,
1520 const bool read_it, const bool write_it,
1521 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001522{
1523 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1524 msg_cerr("Aborting.\n");
1525 return 1;
1526 }
1527
Nico Huber3ac761c2023-01-16 02:43:17 +01001528 if (layout_sanity_checks(flash, write_it)) {
Nico Huber454f6132012-12-10 13:34:10 +00001529 msg_cerr("Requested regions can not be handled. Aborting.\n");
1530 return 1;
1531 }
1532
Nico Huber901fb952023-01-11 23:24:23 +01001533 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_FULL))
1534 return 1;
1535
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001536 /* Initialize chip_restore_fn_count before chip unlock calls. */
1537 flash->chip_restore_fn_count = 0;
1538
Nico Huber454f6132012-12-10 13:34:10 +00001539 /* Given the existence of read locks, we want to unlock for read,
1540 erase and write. */
1541 if (flash->chip->unlock)
1542 flash->chip->unlock(flash);
1543
1544 return 0;
1545}
1546
Nico Huber305f4172013-06-14 11:55:26 +02001547void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001548{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001549 deregister_chip_restore(flash);
Nico Huber901fb952023-01-11 23:24:23 +01001550 if (flash->chip->finish_access)
1551 flash->chip->finish_access(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001552}
1553
1554/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001555 * @addtogroup flashprog-flash
Nico Huber454f6132012-12-10 13:34:10 +00001556 * @{
1557 */
1558
1559/**
1560 * @brief Erase the specified ROM chip.
1561 *
1562 * If a layout is set in the given flash context, only included regions
1563 * will be erased.
1564 *
1565 * @param flashctx The context of the flash chip to erase.
1566 * @return 0 on success.
1567 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001568int flashprog_flash_erase(struct flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +00001569{
1570 if (prepare_flash_access(flashctx, false, false, true, false))
1571 return 1;
1572
1573 const int ret = erase_by_layout(flashctx);
1574
1575 finalize_flash_access(flashctx);
1576
1577 return ret;
1578}
1579
Nico Huberc3b02dc2023-08-12 01:13:45 +02001580/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +00001581
1582/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001583 * @defgroup flashprog-ops Operations
Nico Huber454f6132012-12-10 13:34:10 +00001584 * @{
1585 */
1586
1587/**
1588 * @brief Read the current image from the specified ROM chip.
1589 *
1590 * If a layout is set in the specified flash context, only included regions
1591 * will be read.
1592 *
1593 * @param flashctx The context of the flash chip.
1594 * @param buffer Target buffer to write image to.
1595 * @param buffer_len Size of target buffer in bytes.
1596 * @return 0 on success,
1597 * 2 if buffer_len is too short for the flash chip's contents,
1598 * or 1 on any other failure.
1599 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001600int flashprog_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001601{
1602 const size_t flash_size = flashctx->chip->total_size * 1024;
1603
1604 if (flash_size > buffer_len)
1605 return 2;
1606
1607 if (prepare_flash_access(flashctx, true, false, false, false))
1608 return 1;
1609
1610 msg_cinfo("Reading flash... ");
1611
1612 int ret = 1;
1613 if (read_by_layout(flashctx, buffer)) {
1614 msg_cerr("Read operation failed!\n");
1615 msg_cinfo("FAILED.\n");
1616 goto _finalize_ret;
1617 }
1618 msg_cinfo("done.\n");
1619 ret = 0;
1620
1621_finalize_ret:
1622 finalize_flash_access(flashctx);
1623 return ret;
1624}
1625
1626static void combine_image_by_layout(const struct flashctx *const flashctx,
1627 uint8_t *const newcontents, const uint8_t *const oldcontents)
1628{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001629 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001630 const struct romentry *included;
1631 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001632
Nico Huber3d7b1e32018-12-22 00:53:14 +01001633 while ((included = layout_next_included_region(layout, start))) {
1634 if (included->start > start) {
1635 /* copy everything up to the start of this included region */
1636 memcpy(newcontents + start, oldcontents + start, included->start - start);
1637 }
1638 /* skip this included region */
1639 start = included->end + 1;
1640 if (start == 0)
1641 return;
Nico Huber454f6132012-12-10 13:34:10 +00001642 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001643
1644 /* copy the rest of the chip */
1645 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1646 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001647}
1648
1649/**
1650 * @brief Write the specified image to the ROM chip.
1651 *
1652 * If a layout is set in the specified flash context, only erase blocks
1653 * containing included regions will be touched.
1654 *
1655 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001656 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001657 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001658 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001659 * @return 0 on success,
1660 * 4 if buffer_len doesn't match the size of the flash chip,
1661 * 3 if write was tried but nothing has changed,
1662 * 2 if write failed and flash contents changed,
1663 * or 1 on any other failure.
1664 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001665int flashprog_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001666 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001667{
1668 const size_t flash_size = flashctx->chip->total_size * 1024;
1669 const bool verify_all = flashctx->flags.verify_whole_chip;
1670 const bool verify = flashctx->flags.verify_after_write;
Nico Huberc3b02dc2023-08-12 01:13:45 +02001671 const struct flashprog_layout *const verify_layout =
Nico Huber74d09d42019-06-16 03:27:26 +02001672 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001673
1674 if (buffer_len != flash_size)
1675 return 4;
1676
1677 int ret = 1;
1678
1679 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001680 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001681 uint8_t *const curcontents = malloc(flash_size);
1682 uint8_t *oldcontents = NULL;
1683 if (verify_all)
1684 oldcontents = malloc(flash_size);
1685 if (!curcontents || (verify_all && !oldcontents)) {
1686 msg_gerr("Out of memory!\n");
1687 goto _free_ret;
1688 }
1689
1690#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001691 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001692 if (flashctx->flags.force_boardmismatch) {
1693 msg_pinfo("Proceeding anyway because user forced us to.\n");
1694 } else {
1695 msg_perr("Aborting. You can override this with "
1696 "-p internal:boardmismatch=force.\n");
1697 goto _free_ret;
1698 }
1699 }
1700#endif
1701
1702 if (prepare_flash_access(flashctx, false, true, false, verify))
1703 goto _free_ret;
1704
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001705 /* If given, assume flash chip contains same data as `refcontents`. */
1706 if (refcontents) {
1707 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1708 memcpy(curcontents, refcontents, flash_size);
1709 if (oldcontents)
1710 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001711 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001712 /*
1713 * Read the whole chip to be able to check whether regions need to be
1714 * erased and to give better diagnostics in case write fails.
1715 * The alternative is to read only the regions which are to be
1716 * preserved, but in that case we might perform unneeded erase which
1717 * takes time as well.
1718 */
1719 msg_cinfo("Reading old flash chip contents... ");
1720 if (verify_all) {
1721 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
1722 msg_cinfo("FAILED.\n");
1723 goto _finalize_ret;
1724 }
1725 memcpy(curcontents, oldcontents, flash_size);
1726 } else {
1727 if (read_by_layout(flashctx, curcontents)) {
1728 msg_cinfo("FAILED.\n");
1729 goto _finalize_ret;
1730 }
Nico Huber454f6132012-12-10 13:34:10 +00001731 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001732 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001733 }
Nico Huber454f6132012-12-10 13:34:10 +00001734
1735 if (write_by_layout(flashctx, curcontents, newcontents)) {
1736 msg_cerr("Uh oh. Erase/write failed. ");
1737 ret = 2;
1738 if (verify_all) {
1739 msg_cerr("Checking if anything has changed.\n");
1740 msg_cinfo("Reading current flash chip contents... ");
1741 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
1742 msg_cinfo("done.\n");
1743 if (!memcmp(oldcontents, curcontents, flash_size)) {
1744 nonfatal_help_message();
1745 goto _finalize_ret;
1746 }
1747 msg_cerr("Apparently at least some data has changed.\n");
1748 } else
1749 msg_cerr("Can't even read anymore!\n");
1750 emergency_help_message();
1751 goto _finalize_ret;
1752 } else {
1753 msg_cerr("\n");
1754 }
1755 emergency_help_message();
1756 goto _finalize_ret;
1757 }
1758
1759 /* Verify only if we actually changed something. */
1760 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001761 msg_cinfo("Verifying flash... ");
1762
1763 /* Work around chips which need some time to calm down. */
1764 programmer_delay(1000*1000);
1765
Nico Huber74d09d42019-06-16 03:27:26 +02001766 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001767 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001768 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001769 /* If we tried to write, and verification now fails, we
1770 might have an emergency situation. */
1771 if (ret)
1772 emergency_help_message();
1773 else
1774 msg_cinfo("VERIFIED.\n");
1775 } else {
1776 /* We didn't change anything. */
1777 ret = 0;
1778 }
1779
1780_finalize_ret:
1781 finalize_flash_access(flashctx);
1782_free_ret:
1783 free(oldcontents);
1784 free(curcontents);
1785 return ret;
1786}
1787
1788/**
1789 * @brief Verify the ROM chip's contents with the specified image.
1790 *
1791 * If a layout is set in the specified flash context, only included regions
1792 * will be verified.
1793 *
1794 * @param flashctx The context of the flash chip.
1795 * @param buffer Source buffer to verify with.
1796 * @param buffer_len Size of source buffer in bytes.
1797 * @return 0 on success,
1798 * 3 if the chip's contents don't match,
1799 * 2 if buffer_len doesn't match the size of the flash chip,
1800 * or 1 on any other failure.
1801 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001802int flashprog_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001803{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001804 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001805 const size_t flash_size = flashctx->chip->total_size * 1024;
1806
1807 if (buffer_len != flash_size)
1808 return 2;
1809
1810 const uint8_t *const newcontents = buffer;
1811 uint8_t *const curcontents = malloc(flash_size);
1812 if (!curcontents) {
1813 msg_gerr("Out of memory!\n");
1814 return 1;
1815 }
1816
1817 int ret = 1;
1818
1819 if (prepare_flash_access(flashctx, false, false, false, true))
1820 goto _free_ret;
1821
1822 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001823 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001824 if (!ret)
1825 msg_cinfo("VERIFIED.\n");
1826
1827 finalize_flash_access(flashctx);
1828_free_ret:
1829 free(curcontents);
1830 return ret;
1831}
1832
Nico Huberc3b02dc2023-08-12 01:13:45 +02001833/** @} */ /* end flashprog-ops */