blob: 937a0a7a67f488c06498f9f1883712e2415b51ba [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
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200133int programmer_init(const struct programmer_entry *prog, const char *param)
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
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200137 if (prog == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000138 msg_perr("Invalid programmer specified!\n");
139 return -1;
140 }
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200141 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000142 /* Initialize all programmer specific data. */
143 /* Default to unlimited decode sizes. */
144 max_rom_decode = (const struct decode_sizes) {
145 .parallel = 0xffffffff,
146 .lpc = 0xffffffff,
147 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000148 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000149 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000150 /* Default to top aligned flash at 4 GB. */
151 flashbase = 0;
152 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200153 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000154 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200155 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000156
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200157 if (param) {
158 programmer_param = strdup(param);
159 if (!programmer_param) {
160 msg_perr("Out of memory!\n");
161 return ERROR_FATAL;
162 }
163 } else {
164 programmer_param = NULL;
165 }
166
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200167 msg_pdbg("Initializing %s programmer\n", programmer->name);
168 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000169 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000170 if (ret != 0) {
171 /* It is quite possible that any unhandled programmer parameter would have been valid,
172 * but an error in actual programmer init happened before the parameter was evaluated.
173 */
174 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
175 programmer_param);
176 } else {
177 /* Actual programmer init was successful, but the user specified an invalid or unusable
178 * (for the current programmer configuration) parameter.
179 */
180 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
181 msg_perr("Aborting.\n");
182 ret = ERROR_FATAL;
183 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000184 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200185 free(programmer_param);
186 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000187 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000188}
189
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000190/** Calls registered shutdown functions and resets internal programmer-related variables.
191 * Calling it is safe even without previous initialization, but further interactions with programmer support
192 * require a call to programmer_init() (afterwards).
193 *
194 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000195int programmer_shutdown(void)
196{
David Hendricks8bb20212011-06-14 01:35:36 +0000197 int ret = 0;
198
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000199 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200200 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000201 while (shutdown_fn_count > 0) {
202 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000203 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000204 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000205 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000206
David Hendricks8bb20212011-06-14 01:35:36 +0000207 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000208}
209
Stefan Tauner305e0b92013-07-17 23:46:44 +0000210void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000211{
Edward O'Callaghan621208c2022-09-07 22:21:39 +1000212 void *ret;
213 if (programmer->map_flash_region)
214 ret = programmer->map_flash_region(descr, phys_addr, len);
215 else
216 ret = fallback_map(descr, phys_addr, len);
Stefan Tauner26e7a152013-09-13 17:21:05 +0000217 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
218 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
219 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000220}
221
222void programmer_unmap_flash_region(void *virt_addr, size_t len)
223{
Edward O'Callaghan621208c2022-09-07 22:21:39 +1000224 if (programmer->unmap_flash_region)
225 programmer->unmap_flash_region(virt_addr, len);
226 else
227 fallback_unmap(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000228 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000229}
230
Stefan Taunerf80419c2014-05-02 15:41:42 +0000231void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000232{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000233 if (usecs > 0) {
234 if (programmer->delay)
235 programmer->delay(usecs);
236 else
237 internal_delay(usecs);
238 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000239}
240
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000241int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
242 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000243{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000244 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000245
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000246 return 0;
247}
248
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000249/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000250 * It will look for needle with a subsequent '=' in haystack, return a copy of
251 * needle and remove everything from the first occurrence of needle to the next
252 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000253 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200254static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000255{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000256 char *param_pos, *opt_pos, *rest;
257 char *opt = NULL;
258 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000259 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000260
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000261 needlelen = strlen(needle);
262 if (!needlelen) {
263 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200264 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000265 return NULL;
266 }
267 /* No programmer parameters given. */
268 if (*haystack == NULL)
269 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000270 param_pos = strstr(*haystack, needle);
271 do {
272 if (!param_pos)
273 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000274 /* Needle followed by '='? */
275 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000276 /* Beginning of the string? */
277 if (param_pos == *haystack)
278 break;
279 /* After a delimiter? */
280 if (strchr(delim, *(param_pos - 1)))
281 break;
282 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000283 /* Continue searching. */
284 param_pos++;
285 param_pos = strstr(param_pos, needle);
286 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000287
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000288 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000289 /* Get the string after needle and '='. */
290 opt_pos = param_pos + needlelen + 1;
291 optlen = strcspn(opt_pos, delim);
292 /* Return an empty string if the parameter was empty. */
293 opt = malloc(optlen + 1);
294 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000295 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000296 exit(1);
297 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000298 strncpy(opt, opt_pos, optlen);
299 opt[optlen] = '\0';
300 rest = opt_pos + optlen;
301 /* Skip all delimiters after the current parameter. */
302 rest += strspn(rest, delim);
303 memmove(param_pos, rest, strlen(rest) + 1);
304 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000305 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000306
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000307 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000308}
309
Stefan Tauner66652442011-06-26 17:38:17 +0000310char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000311{
312 return extract_param(&programmer_param, param_name, ",");
313}
314
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000315/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000316static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000317{
318 unsigned int usable_erasefunctions = 0;
319 int k;
320 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
321 if (!check_block_eraser(flash, k, 0))
322 usable_erasefunctions++;
323 }
324 return usable_erasefunctions;
325}
326
Mark Marshallf20b7be2014-05-09 21:16:21 +0000327static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000328{
329 int ret = 0, failcount = 0;
330 unsigned int i;
331 for (i = 0; i < len; i++) {
332 if (wantbuf[i] != havebuf[i]) {
333 /* Only print the first failure. */
334 if (!failcount++)
335 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
336 start + i, wantbuf[i], havebuf[i]);
337 }
338 }
339 if (failcount) {
340 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
341 start, start + len - 1, failcount);
342 ret = -1;
343 }
344 return ret;
345}
346
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000347/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600348static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000349{
350 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300351 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000352
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100353 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000354 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100355 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100356 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000357 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300358 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000359 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100360
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000361 free(cmpbuf);
362 return ret;
363}
364
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000365/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000366 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000367 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000368 * @start offset to the base address of the flash chip
369 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000370 * @return 0 for success, -1 for failure
371 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000372int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000373{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000374 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000375 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000376
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100377 if (start + len > flash->chip->total_size * 1024) {
378 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
379 " total_size 0x%x\n", __func__, start, len,
380 flash->chip->total_size * 1024);
381 return -1;
382 }
383
Stefan Taunerdf64a422014-05-27 00:06:14 +0000384 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000385 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100386 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000387 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000388 }
389
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100390 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000391 if (ret) {
392 msg_gerr("Verification impossible because read failed "
393 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000394 ret = -1;
395 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000396 }
397
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000398 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000399out_free:
400 free(readbuf);
401 return ret;
402}
403
Nico Huber3ac761c2023-01-16 02:43:17 +0100404size_t gran_to_bytes(const enum write_granularity gran)
Nico Huberb77607f2023-01-16 02:25:45 +0100405{
406 switch (gran) {
407 case write_gran_1bit: return 1;
408 case write_gran_1byte: return 1;
409 case write_gran_1byte_implicit_erase: return 1;
410 case write_gran_128bytes: return 128;
411 case write_gran_256bytes: return 256;
412 case write_gran_264bytes: return 264;
413 case write_gran_512bytes: return 512;
414 case write_gran_528bytes: return 528;
415 case write_gran_1024bytes: return 1024;
416 case write_gran_1056bytes: return 1056;
417 default: return 0;
418 }
419}
420
Stefan Tauner02437452013-04-01 19:34:53 +0000421/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300422static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
423 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000424{
425 unsigned int i, j, limit;
426 for (j = 0; j < len / gran; j++) {
427 limit = min (gran, len - j * gran);
428 /* Are 'have' and 'want' identical? */
429 if (!memcmp(have + j * gran, want + j * gran, limit))
430 continue;
431 /* have needs to be in erased state. */
432 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300433 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000434 return 1;
435 }
436 return 0;
437}
438
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000439/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000440 * Check if the buffer @have can be programmed to the content of @want without
441 * erasing. This is only possible if all chunks of size @gran are either kept
442 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000443 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000444 * Warning: This function assumes that @have and @want point to naturally
445 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000446 *
447 * @have buffer with current content
448 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000449 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000450 * @gran write granularity (enum, not count)
451 * @return 0 if no erase is needed, 1 otherwise
452 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000453static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
454 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000455{
Stefan Tauner02437452013-04-01 19:34:53 +0000456 unsigned int i;
Nico Huberb77607f2023-01-16 02:25:45 +0100457 size_t stride;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000458
459 switch (gran) {
460 case write_gran_1bit:
Nico Huberb77607f2023-01-16 02:25:45 +0100461 for (i = 0; i < len; i++) {
462 if ((have[i] & want[i]) != want[i])
463 return 1;
464 }
465 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000466 case write_gran_1byte:
Nico Huberb77607f2023-01-16 02:25:45 +0100467 for (i = 0; i < len; i++) {
468 if ((have[i] != want[i]) && (have[i] != erased_value))
469 return 1;
470 }
471 return 0;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000472 case write_gran_1byte_implicit_erase:
473 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
Nico Huberb77607f2023-01-16 02:25:45 +0100474 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000475 default:
Nico Huberb77607f2023-01-16 02:25:45 +0100476 stride = gran_to_bytes(gran);
477 if (stride) {
478 return need_erase_gran_bytes(have, want, len, stride, erased_value);
479 }
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000480 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200481 "flashprog@flashprog.org\n", __func__);
Nico Huberb77607f2023-01-16 02:25:45 +0100482 return 0;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000483 }
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000484}
485
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000486/**
487 * Check if the buffer @have needs to be programmed to get the content of @want.
488 * If yes, return 1 and fill in first_start with the start address of the
489 * write operation and first_len with the length of the first to-be-written
490 * chunk. If not, return 0 and leave first_start and first_len undefined.
491 *
492 * Warning: This function assumes that @have and @want point to naturally
493 * aligned regions.
494 *
495 * @have buffer with current content
496 * @want buffer with desired content
497 * @len length of the checked area
498 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000499 * @first_start offset of the first byte which needs to be written (passed in
500 * value is increased by the offset of the first needed write
501 * relative to have/want or unchanged if no write is needed)
502 * @return length of the first contiguous area which needs to be written
503 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000504 *
505 * FIXME: This function needs a parameter which tells it about coalescing
506 * in relation to the max write length of the programmer and the max write
507 * length of the chip.
508 */
Nico Huber3b9c86d2023-01-15 22:58:06 +0100509static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, chipsize_t len,
510 chipoff_t *first_start, enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000511{
Felix Singerf25447e2022-08-19 02:44:28 +0200512 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000513 unsigned int rel_start = 0, first_len = 0;
Nico Huberb77607f2023-01-16 02:25:45 +0100514 unsigned int i, limit;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000515
Nico Huberb77607f2023-01-16 02:25:45 +0100516 const size_t stride = gran_to_bytes(gran);
517 if (!stride) {
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000518 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200519 "flashprog@flashprog.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000520 /* Claim that no write was needed. A write with unknown
521 * granularity is too dangerous to try.
522 */
523 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000524 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000525 for (i = 0; i < len / stride; i++) {
526 limit = min(stride, len - i * stride);
527 /* Are 'have' and 'want' identical? */
528 if (memcmp(have + i * stride, want + i * stride, limit)) {
529 if (!need_write) {
530 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200531 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000532 rel_start = i * stride;
533 }
534 } else {
535 if (need_write) {
536 /* First location where have and want
537 * do not differ anymore.
538 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000539 break;
540 }
541 }
542 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000543 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000544 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000545 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000546 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000547}
548
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000549void unmap_flash(struct flashctx *flash)
550{
551 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
552 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
553 flash->physical_registers = 0;
554 flash->virtual_registers = (chipaddr)ERROR_PTR;
555 }
556
557 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
558 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
559 flash->physical_memory = 0;
560 flash->virtual_memory = (chipaddr)ERROR_PTR;
561 }
562}
563
564int map_flash(struct flashctx *flash)
565{
566 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
567 flash->virtual_memory = (chipaddr)ERROR_PTR;
568 flash->virtual_registers = (chipaddr)ERROR_PTR;
569
570 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
571 * These are used for various probing-related hacks that would not map successfully anyway and should be
572 * removed ASAP. */
573 if (flash->chip->total_size == 0)
574 return 0;
575
576 const chipsize_t size = flash->chip->total_size * 1024;
577 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
578 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
579 if (addr == ERROR_PTR) {
580 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
581 flash->chip->name, PRIxPTR_WIDTH, base);
582 return 1;
583 }
584 flash->physical_memory = base;
585 flash->virtual_memory = (chipaddr)addr;
586
587 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
588 * completely different on some chips and programmers, or not mappable at all.
589 * Ignore these problems for now and always report success. */
590 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
591 base = 0xffffffff - size - 0x400000 + 1;
592 addr = programmer_map_flash_region("flash chip registers", base, size);
593 if (addr == ERROR_PTR) {
594 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
595 flash->chip->name, PRIxPTR_WIDTH, base);
596 return 0;
597 }
598 flash->physical_registers = base;
599 flash->virtual_registers = (chipaddr)addr;
600 }
601 return 0;
602}
603
Nico Huber2d625722016-05-03 10:48:02 +0200604/*
605 * Return a string corresponding to the bustype parameter.
606 * Memory is obtained with malloc() and must be freed with free() by the caller.
607 */
608char *flashbuses_to_text(enum chipbustype bustype)
609{
610 char *ret = calloc(1, 1);
611 /*
612 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
613 * will cease to exist and should be eliminated here as well.
614 */
615 if (bustype == BUS_NONSPI) {
616 ret = strcat_realloc(ret, "Non-SPI, ");
617 } else {
618 if (bustype & BUS_PARALLEL)
619 ret = strcat_realloc(ret, "Parallel, ");
620 if (bustype & BUS_LPC)
621 ret = strcat_realloc(ret, "LPC, ");
622 if (bustype & BUS_FWH)
623 ret = strcat_realloc(ret, "FWH, ");
624 if (bustype & BUS_SPI)
625 ret = strcat_realloc(ret, "SPI, ");
626 if (bustype & BUS_PROG)
627 ret = strcat_realloc(ret, "Programmer-specific, ");
628 if (bustype == BUS_NONE)
629 ret = strcat_realloc(ret, "None, ");
630 }
631 /* Kill last comma. */
632 ret[strlen(ret) - 2] = '\0';
633 ret = realloc(ret, strlen(ret) + 1);
634 return ret;
635}
636
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100637static int init_default_layout(struct flashctx *flash)
638{
639 /* Fill default layout covering the whole chip. */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200640 if (flashprog_layout_new(&flash->default_layout) ||
641 flashprog_layout_add_region(flash->default_layout,
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100642 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huberc3b02dc2023-08-12 01:13:45 +0200643 flashprog_layout_include_region(flash->default_layout, "complete flash"))
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100644 return -1;
645 return 0;
646}
647
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000648int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000649{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000650 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000651 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000652 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000653
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000654 for (chip = flashchips + startchip; chip && chip->name; chip++) {
655 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000656 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000657 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000658 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000659 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300660 /* Only probe for SPI25 chips by default. */
661 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
662 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000663 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
664 if (!chip->probe && !force) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200665 msg_gdbg("failed! flashprog has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000666 continue;
667 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000668
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000669 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200670 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000671 if (!flash->chip) {
672 msg_gerr("Out of memory!\n");
Edward O'Callaghan6edf9f82022-11-12 12:08:46 +1100673 return -1;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000674 }
Angel Pons7e134562021-06-07 13:29:13 +0200675 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000676 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000677
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000678 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +0100679 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000680
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000681 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
682 * is only called with force=1 after normal probing failed.
683 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000684 if (force)
685 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000686
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000687 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000688 goto notfound;
689
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000690 /* If this is the first chip found, accept it.
691 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000692 * a non-generic match. SFDP and CFI are generic matches.
693 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000694 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000695 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000696 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000697 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000698 msg_cinfo("===\n"
699 "SFDP has autodetected a flash chip which is "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200700 "not natively supported by flashprog yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000701 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000702 msg_cinfo("The standard operations read and "
703 "verify should work, but to support "
704 "erase, write and all other "
705 "possible features");
706 else
707 msg_cinfo("All standard operations (read, "
708 "verify, erase and write) should "
709 "work, but to support all possible "
710 "features");
711
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000712 msg_cinfo(" we need to add them manually.\n"
713 "You can help us by mailing us the output of the following command to "
Nico Huberc3b02dc2023-08-12 01:13:45 +0200714 "flashprog@flashprog.org:\n"
715 "'flashprog -VV [plus the -p/--programmer parameter]'\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000716 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000717 "===\n");
718 }
719
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000720 /* First flash chip detected on this bus. */
721 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000722 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000723 /* Not the first flash chip detected on this bus, but not a generic match either. */
724 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
725 break;
726 /* 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 +0000727notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000728 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000729 free(flash->chip);
730 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000731 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000732
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000733 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000734 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000735
Edward O'Callaghan00ea3892022-10-11 21:27:37 +1100736 if (init_default_layout(flash) < 0)
737 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000738
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000739 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000740 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
741 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000742 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000743#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200744 if (programmer->map_flash_region == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000745 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
746 PRIxPTR_WIDTH, flash->physical_memory);
747 else
748#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200749 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000750
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000751 /* Flash registers may more likely not be mapped if the chip was forced.
752 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000753 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000754 if (flash->chip->printlock)
755 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000756
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000757 /* Get out of the way for later runs. */
758 unmap_flash(flash);
759
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000760 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000761 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000762}
763
Stefan Tauner96658be2014-05-26 22:05:31 +0000764/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000765static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000766{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000767 int i, j, k;
768 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530769 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000770
771 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
772 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000773 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530774 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000775
776 for (i = 0; i < NUM_ERASEREGIONS; i++) {
777 /* Blocks with zero size are bugs in flashchips.c. */
778 if (eraser.eraseblocks[i].count &&
779 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000780 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200781 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000782 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000783 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000784 }
785 /* Blocks with zero count are bugs in flashchips.c. */
786 if (!eraser.eraseblocks[i].count &&
787 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000788 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200789 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000790 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000791 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000792 }
793 done += eraser.eraseblocks[i].count *
794 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530795 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000796 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000797 /* Empty eraseblock definition with erase function. */
798 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000799 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000800 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000801 if (!done)
802 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000803 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000804 msg_gerr("ERROR: Flash chip %s erase function %i "
805 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000806 " expected 0x%06x bytes.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200807 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000808 chip->name, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000809 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000810 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000811 if (!eraser.block_erase)
812 continue;
813 /* Check if there are identical erase functions for different
814 * layouts. That would imply "magic" erase functions. The
815 * easiest way to check this is with function pointers.
816 */
Uwe Hermann43959702010-03-13 17:28:29 +0000817 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000818 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000819 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000820 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200821 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000822 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000823 ret = 1;
824 }
Uwe Hermann43959702010-03-13 17:28:29 +0000825 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530826 if(curr_eraseblock_count > prev_eraseblock_count)
827 {
Nico Huberac90af62022-12-18 00:22:47 +0000828 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +0200829 "Please report a bug at flashprog@flashprog.org\n",
Nico Huberac90af62022-12-18 00:22:47 +0000830 chip->name, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530831 ret = 1;
832 }
833 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000834 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000835 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000836}
837
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000838static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000839{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000840 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000841
842 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
843 if (log)
844 msg_cdbg("not defined. ");
845 return 1;
846 }
847 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
848 if (log)
849 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000850 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000851 return 1;
852 }
853 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
854 if (log)
855 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000856 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000857 return 1;
858 }
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530859
860 if (flash->chip->bustype == BUS_SPI) {
Nico Huber13389362024-03-05 18:35:30 +0100861 bool native_4ba;
Nico Huber13f97a52023-01-14 23:55:06 +0100862 int i;
Nico Huber13389362024-03-05 18:35:30 +0100863
864 const uint8_t *opcode = spi_get_opcode_from_erasefn(eraser.block_erase, &native_4ba);
Nico Huber13f97a52023-01-14 23:55:06 +0100865 for (i = 0; opcode[i]; i++) {
Nico Huber13389362024-03-05 18:35:30 +0100866 if ((native_4ba && !spi_master_4ba(flash)) ||
867 !flash->mst->spi.probe_opcode(flash, opcode[i])) {
Aarya Chaumal6d98aec2022-08-14 23:16:44 +0530868 if (log)
869 msg_cdbg("block erase function and layout found "
870 "but SPI master doesn't support the function. ");
871 return 1;
872 }
873 }
874 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000875 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000876 return 0;
877}
878
Nico Huber7af0e792016-04-29 16:40:15 +0200879/**
880 * @brief Reads the included layout regions into a buffer.
881 *
882 * If there is no layout set in the given flash context, the whole chip will
883 * be read.
884 *
885 * @param flashctx Flash context to be used.
886 * @param buffer Buffer of full chip size to read into.
887 * @return 0 on success,
888 * 1 if any read fails.
889 */
890static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
891{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200892 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200893 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200894
Nico Huber5ca55232019-06-15 22:29:08 +0200895 while ((entry = layout_next_included(layout, entry))) {
896 const chipoff_t region_start = entry->start;
897 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200898
899 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
900 return 1;
901 }
902 return 0;
903}
904
Nico Huber7af0e792016-04-29 16:40:15 +0200905/**
906 * @private
907 *
908 * For read-erase-write, `curcontents` and `newcontents` shall point
909 * to buffers of the chip's size. Both are supposed to be prefilled
910 * with at least the included layout regions of the current flash
911 * contents (`curcontents`) and the data to be written to the flash
912 * (`newcontents`).
913 *
914 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
915 *
916 * The `chipoff_t` values are used internally by `walk_by_layout()`.
917 */
918struct walk_info {
919 uint8_t *curcontents;
920 const uint8_t *newcontents;
921 chipoff_t region_start;
922 chipoff_t region_end;
923 chipoff_t erase_start;
924 chipoff_t erase_end;
925};
Nico Huber3b9c86d2023-01-15 22:58:06 +0100926
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530927struct eraseblock_data {
928 chipoff_t start_addr;
929 chipoff_t end_addr;
930 bool selected;
931 size_t block_num;
932 size_t first_sub_block_index;
933 size_t last_sub_block_index;
934};
935
936struct erase_layout {
937 struct eraseblock_data* layout_list;
938 size_t block_count;
939 const struct block_eraser *eraser;
940};
941
Nico Huber5ff6fdc2023-01-15 23:43:12 +0100942static bool explicit_erase(const struct walk_info *const info)
943{
944 /* For explicit erase, we are called without new contents. */
945 return !info->newcontents;
946}
947
Nico Huberd96e7032023-01-14 22:31:48 +0100948static size_t calculate_block_count(const struct block_eraser *const eraser)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530949{
Nico Huberd96e7032023-01-14 22:31:48 +0100950 size_t block_count = 0, i;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530951
Nico Huberd96e7032023-01-14 22:31:48 +0100952 for (i = 0; i < ARRAY_SIZE(eraser->eraseblocks); ++i)
953 block_count += eraser->eraseblocks[i].count;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530954
955 return block_count;
956}
957
958static void init_eraseblock(struct erase_layout *layout, size_t idx, size_t block_num,
959 chipoff_t start_addr, chipoff_t end_addr, size_t *sub_block_index)
960{
961 struct eraseblock_data *edata = &layout[idx].layout_list[block_num];
962 edata->start_addr = start_addr;
963 edata->end_addr = end_addr;
964 edata->selected = false;
965 edata->block_num = block_num;
966
967 if (!idx)
968 return;
Nico Hubera02df332023-01-14 23:06:27 +0100969 const struct erase_layout *const sub_layout = &layout[idx - 1];
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530970
971 edata->first_sub_block_index = *sub_block_index;
Nico Hubera02df332023-01-14 23:06:27 +0100972 for (; *sub_block_index < sub_layout->block_count; ++*sub_block_index) {
973 if (sub_layout->layout_list[*sub_block_index].end_addr > end_addr)
974 break;
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530975 }
976 edata->last_sub_block_index = *sub_block_index - 1;
977}
978
979/*
980 * @brief Function to free the created erase_layout
981 *
982 * @param layout pointer to allocated layout
983 * @param erasefn_count number of erase functions for which the layout was created
984 *
985 */
986static void free_erase_layout(struct erase_layout *layout, unsigned int erasefn_count)
987{
Nico Huber13f97a52023-01-14 23:55:06 +0100988 size_t i;
989
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530990 if (!layout)
991 return;
Nico Huber13f97a52023-01-14 23:55:06 +0100992 for (i = 0; i < erasefn_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +0530993 free(layout[i].layout_list);
994 }
995 free(layout);
996}
997
998/*
999 * @brief Function to create an erase layout
1000 *
1001 * @param flashctx flash context
1002 * @param e_layout address to the pointer to store the layout
1003 * @return 0 on success,
1004 * -1 if layout creation fails
1005 *
1006 * This function creates a layout of which erase functions erase which regions
1007 * of the flash chip. This helps to optimally select the erase functions for
1008 * erase/write operations.
1009 */
Nico Huberc09fca42023-01-29 15:58:09 +01001010static int create_erase_layout(struct flashctx *const flashctx, struct erase_layout **e_layout)
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301011{
1012 const struct flashchip *chip = flashctx->chip;
1013 const size_t erasefn_count = count_usable_erasers(flashctx);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301014
1015 if (!erasefn_count) {
1016 msg_gerr("No erase functions supported\n");
1017 return 0;
1018 }
1019
Nico Huberd34864b2023-01-14 23:47:19 +01001020 struct erase_layout *layout = calloc(erasefn_count, sizeof(struct erase_layout));
1021 if (!layout) {
1022 msg_gerr("Out of memory!\n");
1023 return -1;
1024 }
1025
Nico Huber13f97a52023-01-14 23:55:06 +01001026 size_t layout_idx = 0, eraser_idx;
1027 for (eraser_idx = 0; eraser_idx < NUM_ERASEFUNCTIONS; eraser_idx++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301028 if (check_block_eraser(flashctx, eraser_idx, 0))
1029 continue;
1030
1031 layout[layout_idx].eraser = &chip->block_erasers[eraser_idx];
Nico Huberd96e7032023-01-14 22:31:48 +01001032 const size_t block_count = calculate_block_count(&chip->block_erasers[eraser_idx]);
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301033 size_t sub_block_index = 0;
1034
1035 layout[layout_idx].block_count = block_count;
1036 layout[layout_idx].layout_list = (struct eraseblock_data *)calloc(block_count,
1037 sizeof(struct eraseblock_data));
1038
1039 if (!layout[layout_idx].layout_list) {
1040 free_erase_layout(layout, layout_idx);
1041 return -1;
1042 }
1043
1044 size_t block_num = 0;
1045 chipoff_t start_addr = 0;
1046
Nico Huber13f97a52023-01-14 23:55:06 +01001047 int i;
1048 for (i = 0; block_num < block_count; i++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301049 const struct eraseblock *block = &chip->block_erasers[eraser_idx].eraseblocks[i];
1050
Nico Huber13f97a52023-01-14 23:55:06 +01001051 size_t num;
1052 for (num = 0; num < block->count; num++) {
Aarya Chaumal18cc8d32022-07-15 16:51:27 +05301053 chipoff_t end_addr = start_addr + block->size - 1;
1054 init_eraseblock(layout, layout_idx, block_num,
1055 start_addr, end_addr, &sub_block_index);
1056 block_num += 1;
1057 start_addr = end_addr + 1;
1058 }
1059 }
1060 layout_idx++;
1061 }
1062
1063 *e_layout = layout;
1064 return layout_idx;
1065}
1066
Aarya Chaumald33bea42022-07-14 12:51:14 +05301067/*
1068 * @brief Function to select the list of sectors that need erasing
1069 *
1070 * @param flashctx flash context
1071 * @param layout erase layout
1072 * @param findex index of the erase function
1073 * @param block_num index of the block to erase according to the erase function index
Nico Huber00d1b9f2023-01-29 15:07:33 +01001074 * @param info current info from walking the regions
Aarya Chaumald33bea42022-07-14 12:51:14 +05301075 */
Nico Huberb11b72c2023-01-29 15:33:11 +01001076static void select_erase_functions_rec(const struct flashctx *flashctx, const struct erase_layout *layout,
Nico Huber00d1b9f2023-01-29 15:07:33 +01001077 size_t findex, size_t block_num, const struct walk_info *info)
Aarya Chaumald33bea42022-07-14 12:51:14 +05301078{
1079 struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
1080 if (!findex) {
Nico Hubercf6ff0a2023-01-29 15:45:06 +01001081 if (ll->start_addr <= info->region_end && ll->end_addr >= info->region_start) {
Nico Huber1494f8e2023-01-29 15:48:00 +01001082 if (explicit_erase(info)) {
1083 ll->selected = true;
1084 return;
1085 }
Nico Hubera6212482023-01-29 15:39:26 +01001086 const chipoff_t write_start = MAX(info->region_start, ll->start_addr);
1087 const chipoff_t write_end = MIN(info->region_end, ll->end_addr);
1088 const chipsize_t write_len = write_end - write_start + 1;
1089 const uint8_t erased_value = ERASED_VALUE(flashctx);
Nico Huber00d1b9f2023-01-29 15:07:33 +01001090 ll->selected = need_erase(
Nico Hubera6212482023-01-29 15:39:26 +01001091 info->curcontents + write_start, info->newcontents + write_start,
1092 write_len, flashctx->chip->gran, erased_value);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301093 }
1094 } else {
1095 int count = 0;
1096 const int sub_block_start = ll->first_sub_block_index;
1097 const int sub_block_end = ll->last_sub_block_index;
1098
Nico Huber13f97a52023-01-14 23:55:06 +01001099 int j;
1100 for (j = sub_block_start; j <= sub_block_end; j++) {
Nico Huberb11b72c2023-01-29 15:33:11 +01001101 select_erase_functions_rec(flashctx, layout, findex - 1, j, info);
Aarya Chaumald33bea42022-07-14 12:51:14 +05301102 if (layout[findex - 1].layout_list[j].selected)
1103 count++;
1104 }
1105
1106 const int total_blocks = sub_block_end - sub_block_start + 1;
1107 if (count && count > total_blocks/2) {
Nico Huber00d1b9f2023-01-29 15:07:33 +01001108 if (ll->start_addr >= info->region_start && ll->end_addr <= info->region_end) {
Nico Huber13f97a52023-01-14 23:55:06 +01001109 for (j = sub_block_start; j <= sub_block_end; j++)
Aarya Chaumald33bea42022-07-14 12:51:14 +05301110 layout[findex - 1].layout_list[j].selected = false;
1111 ll->selected = true;
1112 }
1113 }
1114 }
1115}
1116
Nico Huberc09fca42023-01-29 15:58:09 +01001117static void select_erase_functions(const struct flashctx *flashctx, const struct erase_layout *layout,
Nico Huberb11b72c2023-01-29 15:33:11 +01001118 size_t erasefn_count, const struct walk_info *info)
1119{
1120 size_t block_num;
1121 for (block_num = 0; block_num < layout[erasefn_count - 1].block_count; ++block_num)
1122 select_erase_functions_rec(flashctx, layout, erasefn_count - 1, block_num, info);
1123}
1124
Nico Huber3b9c86d2023-01-15 22:58:06 +01001125static int write_range(struct flashctx *const flashctx, const chipoff_t flash_offset,
1126 const uint8_t *const curcontents, const uint8_t *const newcontents,
1127 const chipsize_t len, bool *const skipped)
1128{
1129 unsigned int writecount = 0;
1130 chipoff_t starthere = 0;
1131 chipsize_t lenhere = 0;
1132
1133 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1134 len - starthere, &starthere, flashctx->chip->gran))) {
1135 if (!writecount++)
1136 msg_cdbg("W");
1137 if (flashctx->chip->write(flashctx, newcontents + starthere,
1138 flash_offset + starthere, lenhere))
1139 return 1;
1140 starthere += lenhere;
1141 if (skipped)
1142 *skipped = false;
1143 }
1144 return 0;
1145}
1146
1147typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
Nico Huber7af0e792016-04-29 16:40:15 +02001148/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1149typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1150
1151static int walk_eraseblocks(struct flashctx *const flashctx,
Nico Huberc09fca42023-01-29 15:58:09 +01001152 struct erase_layout *const layouts,
1153 const size_t layout_count,
Nico Huber7af0e792016-04-29 16:40:15 +02001154 struct walk_info *const info,
Nico Huberc09fca42023-01-29 15:58:09 +01001155 const per_blockfn_t per_blockfn)
Nico Huber7af0e792016-04-29 16:40:15 +02001156{
1157 int ret;
1158 size_t i, j;
1159 bool first = true;
Nico Huber7af0e792016-04-29 16:40:15 +02001160
Nico Huberc09fca42023-01-29 15:58:09 +01001161 for (i = 0; i < layout_count; ++i) {
1162 const struct erase_layout *const layout = &layouts[i];
Nico Huber7af0e792016-04-29 16:40:15 +02001163
Nico Huberc09fca42023-01-29 15:58:09 +01001164 for (j = 0; j < layout->block_count; ++j) {
1165 struct eraseblock_data *const eb = &layout->layout_list[j];
1166
1167 if (eb->start_addr > info->region_end)
Nico Huber7af0e792016-04-29 16:40:15 +02001168 break;
Nico Huberc09fca42023-01-29 15:58:09 +01001169 if (eb->end_addr < info->region_start)
1170 continue;
1171 if (!eb->selected)
1172 continue;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001173
Nico Huber7af0e792016-04-29 16:40:15 +02001174 /* Print this for every block except the first one. */
1175 if (first)
1176 first = false;
1177 else
1178 msg_cdbg(", ");
Nico Huberc09fca42023-01-29 15:58:09 +01001179 msg_cdbg("0x%06x-0x%06x:", eb->start_addr, eb->end_addr);
Nico Huber7af0e792016-04-29 16:40:15 +02001180
Nico Huberc09fca42023-01-29 15:58:09 +01001181 info->erase_start = eb->start_addr;
1182 info->erase_end = eb->end_addr;
1183 ret = per_blockfn(flashctx, info, layout->eraser->block_erase);
Nico Huber7af0e792016-04-29 16:40:15 +02001184 if (ret)
1185 return ret;
Nico Huberc09fca42023-01-29 15:58:09 +01001186
1187 /* Clean the erase layout up for future use on other
1188 regions. `.selected` is the only field we alter. */
1189 eb->selected = false;
Nico Huber7af0e792016-04-29 16:40:15 +02001190 }
Nico Huber7af0e792016-04-29 16:40:15 +02001191 }
1192 msg_cdbg("\n");
1193 return 0;
1194}
1195
1196static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1197 const per_blockfn_t per_blockfn)
1198{
Nico Huberc09fca42023-01-29 15:58:09 +01001199 const bool do_erase = explicit_erase(info) || !(flashctx->chip->feature_bits & FEATURE_NO_ERASE);
Nico Huberc3b02dc2023-08-12 01:13:45 +02001200 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huberc09fca42023-01-29 15:58:09 +01001201 struct erase_layout *erase_layouts = NULL;
Nico Huber5ca55232019-06-15 22:29:08 +02001202 const struct romentry *entry = NULL;
Nico Huberc09fca42023-01-29 15:58:09 +01001203 int ret = 0, layout_count = 0;
Nico Huber7af0e792016-04-29 16:40:15 +02001204
1205 all_skipped = true;
1206 msg_cinfo("Erasing and writing flash chip... ");
1207
Nico Huberc09fca42023-01-29 15:58:09 +01001208 if (do_erase) {
1209 layout_count = create_erase_layout(flashctx, &erase_layouts);
1210 if (layout_count <= 0)
1211 return 1;
1212 }
1213
Nico Huber5ca55232019-06-15 22:29:08 +02001214 while ((entry = layout_next_included(layout, entry))) {
1215 info->region_start = entry->start;
1216 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001217
Nico Huberc09fca42023-01-29 15:58:09 +01001218 if (do_erase) {
1219 select_erase_functions(flashctx, erase_layouts, layout_count, info);
1220 ret = walk_eraseblocks(flashctx, erase_layouts, layout_count, info, per_blockfn);
1221 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001222 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001223 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001224 }
Nico Huber7af0e792016-04-29 16:40:15 +02001225 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001226
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001227 if (info->newcontents) {
1228 bool skipped = true;
1229 msg_cdbg("0x%06x-0x%06x:", info->region_start, info->region_end);
Nico Huberc09fca42023-01-29 15:58:09 +01001230 ret = write_range(flashctx, info->region_start,
1231 info->curcontents + info->region_start,
1232 info->newcontents + info->region_start,
1233 info->region_end + 1 - info->region_start, &skipped);
1234 if (ret) {
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001235 msg_cerr("FAILED!\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001236 goto free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001237 }
1238 if (skipped) {
1239 msg_cdbg("S\n");
1240 } else {
1241 msg_cdbg("\n");
1242 all_skipped = false;
1243 }
Nico Huberd34af7a2023-01-15 23:58:18 +01001244 }
Nico Huber7af0e792016-04-29 16:40:15 +02001245 }
1246 if (all_skipped)
1247 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1248 msg_cinfo("Erase/write done.\n");
Nico Huberc09fca42023-01-29 15:58:09 +01001249
1250free_ret:
1251 free_erase_layout(erase_layouts, layout_count);
1252 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001253}
1254
1255static int erase_block(struct flashctx *const flashctx,
1256 const struct walk_info *const info, const erasefn_t erasefn)
1257{
1258 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001259 const bool region_unaligned = info->region_start > info->erase_start ||
1260 info->erase_end > info->region_end;
1261 uint8_t *backup_contents = NULL, *erased_contents = NULL;
Nico Huberd34af7a2023-01-15 23:58:18 +01001262 int ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001263
Nico Huber6e61e0c2019-01-23 17:07:49 +01001264 /*
1265 * If the region is not erase-block aligned, merge current flash con-
1266 * tents into a new buffer `backup_contents`.
1267 */
1268 if (region_unaligned) {
1269 backup_contents = malloc(erase_len);
1270 erased_contents = malloc(erase_len);
1271 if (!backup_contents || !erased_contents) {
1272 msg_cerr("Out of memory!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001273 goto _free_ret;
1274 }
1275 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1276 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1277
1278 msg_cdbg("R");
1279 /* Merge data preceding the current region. */
1280 if (info->region_start > info->erase_start) {
1281 const chipoff_t start = info->erase_start;
1282 const chipsize_t len = info->region_start - info->erase_start;
1283 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1284 msg_cerr("Can't read! Aborting.\n");
1285 goto _free_ret;
1286 }
1287 }
1288 /* Merge data following the current region. */
1289 if (info->erase_end > info->region_end) {
1290 const chipoff_t start = info->region_end + 1;
1291 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1292 const chipsize_t len = info->erase_end - info->region_end;
1293 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1294 msg_cerr("Can't read! Aborting.\n");
1295 goto _free_ret;
1296 }
1297 }
1298 }
1299
Nico Huber7af0e792016-04-29 16:40:15 +02001300 all_skipped = false;
1301
1302 msg_cdbg("E");
1303 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001304 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001305 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1306 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001307 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001308 }
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001309 if (info->curcontents)
1310 memset(info->curcontents + info->erase_start, ERASED_VALUE(flashctx), erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001311
1312 if (region_unaligned) {
Nico Huber3b9c86d2023-01-15 22:58:06 +01001313 if (write_range(flashctx, info->erase_start, erased_contents, backup_contents, erase_len, NULL))
1314 goto _free_ret;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001315 if (info->curcontents)
1316 memcpy(info->curcontents + info->erase_start, backup_contents, erase_len);
Nico Huber6e61e0c2019-01-23 17:07:49 +01001317 }
1318
1319 ret = 0;
1320
1321_free_ret:
1322 free(erased_contents);
1323 free(backup_contents);
1324 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001325}
1326
1327/**
1328 * @brief Erases the included layout regions.
1329 *
1330 * If there is no layout set in the given flash context, the whole chip will
1331 * be erased.
1332 *
1333 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001334 * @return 0 on success,
1335 * 1 if all available erase functions failed.
1336 */
Nico Huber454f6132012-12-10 13:34:10 +00001337static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001338{
1339 struct walk_info info = { 0 };
1340 return walk_by_layout(flashctx, &info, &erase_block);
1341}
1342
Nico Huber7af0e792016-04-29 16:40:15 +02001343/**
1344 * @brief Writes the included layout regions from a given image.
1345 *
1346 * If there is no layout set in the given flash context, the whole image
1347 * will be written.
1348 *
1349 * @param flashctx Flash context to be used.
1350 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1351 * @param newcontents The new image to be written.
1352 * @return 0 on success,
1353 * 1 if anything has gone wrong.
1354 */
Nico Huber454f6132012-12-10 13:34:10 +00001355static int write_by_layout(struct flashctx *const flashctx,
1356 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001357{
1358 struct walk_info info;
1359 info.curcontents = curcontents;
1360 info.newcontents = newcontents;
Nico Huber5ff6fdc2023-01-15 23:43:12 +01001361 return walk_by_layout(flashctx, &info, erase_block);
Nico Huber7af0e792016-04-29 16:40:15 +02001362}
1363
1364/**
1365 * @brief Compares the included layout regions with content from a buffer.
1366 *
1367 * If there is no layout set in the given flash context, the whole chip's
1368 * contents will be compared.
1369 *
1370 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001371 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001372 * @param curcontents A buffer of full chip size to read current chip contents into.
1373 * @param newcontents The new image to compare to.
1374 * @return 0 on success,
1375 * 1 if reading failed,
1376 * 3 if the contents don't match.
1377 */
Nico Huber74d09d42019-06-16 03:27:26 +02001378static int verify_by_layout(
1379 struct flashctx *const flashctx,
Nico Huberc3b02dc2023-08-12 01:13:45 +02001380 const struct flashprog_layout *const layout,
Nico Huber74d09d42019-06-16 03:27:26 +02001381 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001382{
Nico Huber5ca55232019-06-15 22:29:08 +02001383 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001384
Nico Huber5ca55232019-06-15 22:29:08 +02001385 while ((entry = layout_next_included(layout, entry))) {
1386 const chipoff_t region_start = entry->start;
1387 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001388
1389 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1390 return 1;
1391 if (compare_range(newcontents + region_start, curcontents + region_start,
1392 region_start, region_len))
1393 return 3;
1394 }
1395 return 0;
1396}
1397
Stefan Tauner136388f2013-07-15 10:47:53 +00001398static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001399{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001400 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001401#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001402 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001403 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001404 "chip. Please report this to the mailing list at flashprog@flashprog.org or\n"
1405 "on IRC (see https://www.flashprog.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001406 "-------------------------------------------------------------------------------\n"
1407 "You may now reboot or simply leave the machine running.\n");
1408 else
1409#endif
1410 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001411 "the programmer and the flash chip. If you think the error is caused by flashprog\n"
1412 "please report this to the mailing list at flashprog@flashprog.org or on IRC\n"
1413 "(see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001414}
1415
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001416void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001417{
Stefan Tauner136388f2013-07-15 10:47:53 +00001418 msg_gerr("Your flash chip is in an unknown state.\n");
1419#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001420 if (programmer == &programmer_internal)
Nico Huberc3b02dc2023-08-12 01:13:45 +02001421 msg_gerr("Get help on IRC (see https://www.flashprog.org/Contact) or mail\n"
1422 "flashprog@flashprog.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001423 "-------------------------------------------------------------------------------\n"
1424 "DO NOT REBOOT OR POWEROFF!\n");
1425 else
1426#endif
Nico Huberc3b02dc2023-08-12 01:13:45 +02001427 msg_gerr("Please report this to the mailing list at flashprog@flashprog.org\n"
1428 "or on IRC (see https://www.flashprog.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001429}
1430
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001431void list_programmers_linebreak(int startcol, int cols, int paren)
1432{
1433 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001434 int pnamelen;
1435 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001436 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001437 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001438
Thomas Heijligend45cb592021-05-19 14:12:18 +02001439 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001440 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001441 pnamelen = strlen(pname);
1442 if (remaining - pnamelen - 2 < 0) {
1443 if (firstline)
1444 firstline = 0;
1445 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001446 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001447 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001448 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001449 remaining = cols - startcol;
1450 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001451 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001452 remaining--;
1453 }
1454 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001455 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001456 remaining--;
1457 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001458 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001459 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001460 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001461 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001462 remaining--;
1463 } else {
1464 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001465 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001466 }
1467 }
1468}
1469
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001470int selfcheck(void)
1471{
Stefan Tauner96658be2014-05-26 22:05:31 +00001472 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001473 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001474
Thomas Heijligend45cb592021-05-19 14:12:18 +02001475 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001476 const struct programmer_entry *const p = programmer_table[i];
1477 if (p == NULL) {
1478 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1479 ret = 1;
1480 continue;
1481 }
1482 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001483 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1484 ret = 1;
1485 /* This might hide other problems with this programmer, but allows for better error
1486 * messages below without jumping through hoops. */
1487 continue;
1488 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001489 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001490 case USB:
1491 case PCI:
1492 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001493 if (p->devs.note == NULL) {
1494 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001495 break; /* This one has its device list stored separately. */
1496 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001497 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001498 ret = 1;
1499 }
1500 break;
1501 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001502 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001503 ret = 1;
1504 break;
1505 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001506 if (p->init == NULL) {
1507 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001508 ret = 1;
1509 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001510 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001511
1512 /* It would be favorable if we could check for the correct layout (especially termination) of various
1513 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1514 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1515 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1516 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1517 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001518 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001519 msg_gerr("Flashchips table miscompilation!\n");
1520 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001521 } else {
1522 for (i = 0; i < flashchips_size - 1; i++) {
1523 const struct flashchip *chip = &flashchips[i];
1524 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1525 ret = 1;
1526 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
Nico Huberc3b02dc2023-08-12 01:13:45 +02001527 "Please report a bug at flashprog@flashprog.org\n", i,
Stefan Tauner96658be2014-05-26 22:05:31 +00001528 chip->name == NULL ? "unnamed" : chip->name);
1529 }
1530 if (selfcheck_eraseblocks(chip)) {
1531 ret = 1;
1532 }
1533 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001534 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001535
Stefan Tauner600576b2014-06-12 22:57:36 +00001536#if CONFIG_INTERNAL == 1
1537 ret |= selfcheck_board_enables();
1538#endif
1539
Stefan Tauner96658be2014-05-26 22:05:31 +00001540 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001541 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001542}
1543
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001544/* FIXME: This function signature needs to be improved once prepare_flash_access()
1545 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001546 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001547static int chip_safety_check(const struct flashctx *flash, int force,
1548 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001549{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001550 const struct flashchip *chip = flash->chip;
1551
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001552 if (!programmer_may_write && (write_it || erase_it)) {
1553 msg_perr("Write/erase is not working yet on your programmer in "
1554 "its current configuration.\n");
1555 /* --force is the wrong approach, but it's the best we can do
1556 * until the generic programmer parameter parser is merged.
1557 */
1558 if (!force)
1559 return 1;
1560 msg_cerr("Continuing anyway.\n");
1561 }
1562
1563 if (read_it || erase_it || write_it || verify_it) {
1564 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001565 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001566 msg_cerr("Read is not working on this chip. ");
1567 if (!force)
1568 return 1;
1569 msg_cerr("Continuing anyway.\n");
1570 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001571 if (!chip->read) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001572 msg_cerr("flashprog has no read function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001573 "flash chip.\n");
1574 return 1;
1575 }
1576 }
1577 if (erase_it || write_it) {
1578 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001579 if (chip->tested.erase == NA) {
1580 msg_cerr("Erase is not possible on this chip.\n");
1581 return 1;
1582 }
1583 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001584 msg_cerr("Erase is not working on this chip. ");
1585 if (!force)
1586 return 1;
1587 msg_cerr("Continuing anyway.\n");
1588 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001589 if(count_usable_erasers(flash) == 0) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001590 msg_cerr("flashprog has no erase function for this "
Stefan Tauner5368dca2011-07-01 00:19:12 +00001591 "flash chip.\n");
1592 return 1;
1593 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001594 }
1595 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001596 if (chip->tested.write == NA) {
1597 msg_cerr("Write is not possible on this chip.\n");
1598 return 1;
1599 }
1600 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001601 msg_cerr("Write is not working on this chip. ");
1602 if (!force)
1603 return 1;
1604 msg_cerr("Continuing anyway.\n");
1605 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001606 if (!chip->write) {
Nico Huberc3b02dc2023-08-12 01:13:45 +02001607 msg_cerr("flashprog has no write function for this "
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001608 "flash chip.\n");
1609 return 1;
1610 }
1611 }
1612 return 0;
1613}
1614
Nico Huber305f4172013-06-14 11:55:26 +02001615int prepare_flash_access(struct flashctx *const flash,
1616 const bool read_it, const bool write_it,
1617 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001618{
1619 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1620 msg_cerr("Aborting.\n");
1621 return 1;
1622 }
1623
Nico Huber3ac761c2023-01-16 02:43:17 +01001624 if (layout_sanity_checks(flash, write_it)) {
Nico Huber454f6132012-12-10 13:34:10 +00001625 msg_cerr("Requested regions can not be handled. Aborting.\n");
1626 return 1;
1627 }
1628
Nico Huber901fb952023-01-11 23:24:23 +01001629 if (flash->chip->prepare_access && flash->chip->prepare_access(flash, PREPARE_FULL))
1630 return 1;
1631
Nico Huber454f6132012-12-10 13:34:10 +00001632 if (map_flash(flash) != 0)
1633 return 1;
1634
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001635 /* Initialize chip_restore_fn_count before chip unlock calls. */
1636 flash->chip_restore_fn_count = 0;
1637
Nico Huber454f6132012-12-10 13:34:10 +00001638 /* Given the existence of read locks, we want to unlock for read,
1639 erase and write. */
1640 if (flash->chip->unlock)
1641 flash->chip->unlock(flash);
1642
1643 return 0;
1644}
1645
Nico Huber305f4172013-06-14 11:55:26 +02001646void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001647{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001648 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001649 unmap_flash(flash);
Nico Huber901fb952023-01-11 23:24:23 +01001650 if (flash->chip->finish_access)
1651 flash->chip->finish_access(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001652}
1653
1654/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001655 * @addtogroup flashprog-flash
Nico Huber454f6132012-12-10 13:34:10 +00001656 * @{
1657 */
1658
1659/**
1660 * @brief Erase the specified ROM chip.
1661 *
1662 * If a layout is set in the given flash context, only included regions
1663 * will be erased.
1664 *
1665 * @param flashctx The context of the flash chip to erase.
1666 * @return 0 on success.
1667 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001668int flashprog_flash_erase(struct flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +00001669{
1670 if (prepare_flash_access(flashctx, false, false, true, false))
1671 return 1;
1672
1673 const int ret = erase_by_layout(flashctx);
1674
1675 finalize_flash_access(flashctx);
1676
1677 return ret;
1678}
1679
Nico Huberc3b02dc2023-08-12 01:13:45 +02001680/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +00001681
1682/**
Nico Huberc3b02dc2023-08-12 01:13:45 +02001683 * @defgroup flashprog-ops Operations
Nico Huber454f6132012-12-10 13:34:10 +00001684 * @{
1685 */
1686
1687/**
1688 * @brief Read the current image from the specified ROM chip.
1689 *
1690 * If a layout is set in the specified flash context, only included regions
1691 * will be read.
1692 *
1693 * @param flashctx The context of the flash chip.
1694 * @param buffer Target buffer to write image to.
1695 * @param buffer_len Size of target buffer in bytes.
1696 * @return 0 on success,
1697 * 2 if buffer_len is too short for the flash chip's contents,
1698 * or 1 on any other failure.
1699 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001700int flashprog_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001701{
1702 const size_t flash_size = flashctx->chip->total_size * 1024;
1703
1704 if (flash_size > buffer_len)
1705 return 2;
1706
1707 if (prepare_flash_access(flashctx, true, false, false, false))
1708 return 1;
1709
1710 msg_cinfo("Reading flash... ");
1711
1712 int ret = 1;
1713 if (read_by_layout(flashctx, buffer)) {
1714 msg_cerr("Read operation failed!\n");
1715 msg_cinfo("FAILED.\n");
1716 goto _finalize_ret;
1717 }
1718 msg_cinfo("done.\n");
1719 ret = 0;
1720
1721_finalize_ret:
1722 finalize_flash_access(flashctx);
1723 return ret;
1724}
1725
1726static void combine_image_by_layout(const struct flashctx *const flashctx,
1727 uint8_t *const newcontents, const uint8_t *const oldcontents)
1728{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001729 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001730 const struct romentry *included;
1731 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001732
Nico Huber3d7b1e32018-12-22 00:53:14 +01001733 while ((included = layout_next_included_region(layout, start))) {
1734 if (included->start > start) {
1735 /* copy everything up to the start of this included region */
1736 memcpy(newcontents + start, oldcontents + start, included->start - start);
1737 }
1738 /* skip this included region */
1739 start = included->end + 1;
1740 if (start == 0)
1741 return;
Nico Huber454f6132012-12-10 13:34:10 +00001742 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001743
1744 /* copy the rest of the chip */
1745 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1746 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001747}
1748
1749/**
1750 * @brief Write the specified image to the ROM chip.
1751 *
1752 * If a layout is set in the specified flash context, only erase blocks
1753 * containing included regions will be touched.
1754 *
1755 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001756 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001757 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001758 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001759 * @return 0 on success,
1760 * 4 if buffer_len doesn't match the size of the flash chip,
1761 * 3 if write was tried but nothing has changed,
1762 * 2 if write failed and flash contents changed,
1763 * or 1 on any other failure.
1764 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001765int flashprog_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001766 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001767{
1768 const size_t flash_size = flashctx->chip->total_size * 1024;
1769 const bool verify_all = flashctx->flags.verify_whole_chip;
1770 const bool verify = flashctx->flags.verify_after_write;
Nico Huberc3b02dc2023-08-12 01:13:45 +02001771 const struct flashprog_layout *const verify_layout =
Nico Huber74d09d42019-06-16 03:27:26 +02001772 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001773
1774 if (buffer_len != flash_size)
1775 return 4;
1776
1777 int ret = 1;
1778
1779 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001780 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001781 uint8_t *const curcontents = malloc(flash_size);
1782 uint8_t *oldcontents = NULL;
1783 if (verify_all)
1784 oldcontents = malloc(flash_size);
1785 if (!curcontents || (verify_all && !oldcontents)) {
1786 msg_gerr("Out of memory!\n");
1787 goto _free_ret;
1788 }
1789
1790#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001791 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001792 if (flashctx->flags.force_boardmismatch) {
1793 msg_pinfo("Proceeding anyway because user forced us to.\n");
1794 } else {
1795 msg_perr("Aborting. You can override this with "
1796 "-p internal:boardmismatch=force.\n");
1797 goto _free_ret;
1798 }
1799 }
1800#endif
1801
1802 if (prepare_flash_access(flashctx, false, true, false, verify))
1803 goto _free_ret;
1804
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001805 /* If given, assume flash chip contains same data as `refcontents`. */
1806 if (refcontents) {
1807 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1808 memcpy(curcontents, refcontents, flash_size);
1809 if (oldcontents)
1810 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001811 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001812 /*
1813 * Read the whole chip to be able to check whether regions need to be
1814 * erased and to give better diagnostics in case write fails.
1815 * The alternative is to read only the regions which are to be
1816 * preserved, but in that case we might perform unneeded erase which
1817 * takes time as well.
1818 */
1819 msg_cinfo("Reading old flash chip contents... ");
1820 if (verify_all) {
1821 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
1822 msg_cinfo("FAILED.\n");
1823 goto _finalize_ret;
1824 }
1825 memcpy(curcontents, oldcontents, flash_size);
1826 } else {
1827 if (read_by_layout(flashctx, curcontents)) {
1828 msg_cinfo("FAILED.\n");
1829 goto _finalize_ret;
1830 }
Nico Huber454f6132012-12-10 13:34:10 +00001831 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001832 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001833 }
Nico Huber454f6132012-12-10 13:34:10 +00001834
1835 if (write_by_layout(flashctx, curcontents, newcontents)) {
1836 msg_cerr("Uh oh. Erase/write failed. ");
1837 ret = 2;
1838 if (verify_all) {
1839 msg_cerr("Checking if anything has changed.\n");
1840 msg_cinfo("Reading current flash chip contents... ");
1841 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
1842 msg_cinfo("done.\n");
1843 if (!memcmp(oldcontents, curcontents, flash_size)) {
1844 nonfatal_help_message();
1845 goto _finalize_ret;
1846 }
1847 msg_cerr("Apparently at least some data has changed.\n");
1848 } else
1849 msg_cerr("Can't even read anymore!\n");
1850 emergency_help_message();
1851 goto _finalize_ret;
1852 } else {
1853 msg_cerr("\n");
1854 }
1855 emergency_help_message();
1856 goto _finalize_ret;
1857 }
1858
1859 /* Verify only if we actually changed something. */
1860 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001861 msg_cinfo("Verifying flash... ");
1862
1863 /* Work around chips which need some time to calm down. */
1864 programmer_delay(1000*1000);
1865
Nico Huber74d09d42019-06-16 03:27:26 +02001866 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001867 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001868 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001869 /* If we tried to write, and verification now fails, we
1870 might have an emergency situation. */
1871 if (ret)
1872 emergency_help_message();
1873 else
1874 msg_cinfo("VERIFIED.\n");
1875 } else {
1876 /* We didn't change anything. */
1877 ret = 0;
1878 }
1879
1880_finalize_ret:
1881 finalize_flash_access(flashctx);
1882_free_ret:
1883 free(oldcontents);
1884 free(curcontents);
1885 return ret;
1886}
1887
1888/**
1889 * @brief Verify the ROM chip's contents with the specified image.
1890 *
1891 * If a layout is set in the specified flash context, only included regions
1892 * will be verified.
1893 *
1894 * @param flashctx The context of the flash chip.
1895 * @param buffer Source buffer to verify with.
1896 * @param buffer_len Size of source buffer in bytes.
1897 * @return 0 on success,
1898 * 3 if the chip's contents don't match,
1899 * 2 if buffer_len doesn't match the size of the flash chip,
1900 * or 1 on any other failure.
1901 */
Nico Huberc3b02dc2023-08-12 01:13:45 +02001902int flashprog_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
Nico Huber454f6132012-12-10 13:34:10 +00001903{
Nico Huberc3b02dc2023-08-12 01:13:45 +02001904 const struct flashprog_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001905 const size_t flash_size = flashctx->chip->total_size * 1024;
1906
1907 if (buffer_len != flash_size)
1908 return 2;
1909
1910 const uint8_t *const newcontents = buffer;
1911 uint8_t *const curcontents = malloc(flash_size);
1912 if (!curcontents) {
1913 msg_gerr("Out of memory!\n");
1914 return 1;
1915 }
1916
1917 int ret = 1;
1918
1919 if (prepare_flash_access(flashctx, false, false, false, true))
1920 goto _free_ret;
1921
1922 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001923 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001924 if (!ret)
1925 msg_cinfo("VERIFIED.\n");
1926
1927 finalize_flash_access(flashctx);
1928_free_ret:
1929 free(curcontents);
1930 return ret;
1931}
1932
Nico Huberc3b02dc2023-08-12 01:13:45 +02001933/** @} */ /* end flashprog-ops */