blob: c434cff4a1f7efa16286899d86ef8ffc661f06bd [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
Mathias Krausea60faab2011-01-17 07:50:42 +000037const char flashrom_version[] = FLASHROM_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{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200212 void *ret = programmer->map_flash_region(descr, phys_addr, len);
Stefan Tauner26e7a152013-09-13 17:21:05 +0000213 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
214 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
215 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000216}
217
218void programmer_unmap_flash_region(void *virt_addr, size_t len)
219{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200220 programmer->unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000221 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000222}
223
Stefan Taunerf80419c2014-05-02 15:41:42 +0000224void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000225{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000226 if (usecs > 0) {
227 if (programmer->delay)
228 programmer->delay(usecs);
229 else
230 internal_delay(usecs);
231 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000232}
233
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000234int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
235 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000236{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000237 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000238
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000239 return 0;
240}
241
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000242/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000243 * It will look for needle with a subsequent '=' in haystack, return a copy of
244 * needle and remove everything from the first occurrence of needle to the next
245 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000246 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200247static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000248{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000249 char *param_pos, *opt_pos, *rest;
250 char *opt = NULL;
251 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000252 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000253
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000254 needlelen = strlen(needle);
255 if (!needlelen) {
256 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000257 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000258 return NULL;
259 }
260 /* No programmer parameters given. */
261 if (*haystack == NULL)
262 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000263 param_pos = strstr(*haystack, needle);
264 do {
265 if (!param_pos)
266 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000267 /* Needle followed by '='? */
268 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000269 /* Beginning of the string? */
270 if (param_pos == *haystack)
271 break;
272 /* After a delimiter? */
273 if (strchr(delim, *(param_pos - 1)))
274 break;
275 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000276 /* Continue searching. */
277 param_pos++;
278 param_pos = strstr(param_pos, needle);
279 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000280
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000281 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000282 /* Get the string after needle and '='. */
283 opt_pos = param_pos + needlelen + 1;
284 optlen = strcspn(opt_pos, delim);
285 /* Return an empty string if the parameter was empty. */
286 opt = malloc(optlen + 1);
287 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000288 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000289 exit(1);
290 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000291 strncpy(opt, opt_pos, optlen);
292 opt[optlen] = '\0';
293 rest = opt_pos + optlen;
294 /* Skip all delimiters after the current parameter. */
295 rest += strspn(rest, delim);
296 memmove(param_pos, rest, strlen(rest) + 1);
297 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000298 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000299
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000300 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000301}
302
Stefan Tauner66652442011-06-26 17:38:17 +0000303char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000304{
305 return extract_param(&programmer_param, param_name, ",");
306}
307
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000308/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000309static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000310{
311 unsigned int usable_erasefunctions = 0;
312 int k;
313 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
314 if (!check_block_eraser(flash, k, 0))
315 usable_erasefunctions++;
316 }
317 return usable_erasefunctions;
318}
319
Mark Marshallf20b7be2014-05-09 21:16:21 +0000320static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000321{
322 int ret = 0, failcount = 0;
323 unsigned int i;
324 for (i = 0; i < len; i++) {
325 if (wantbuf[i] != havebuf[i]) {
326 /* Only print the first failure. */
327 if (!failcount++)
328 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
329 start + i, wantbuf[i], havebuf[i]);
330 }
331 }
332 if (failcount) {
333 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
334 start, start + len - 1, failcount);
335 ret = -1;
336 }
337 return ret;
338}
339
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000340/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600341static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000342{
343 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300344 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000345
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100346 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000347 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100348 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000349 exit(1);
350 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300351 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000352 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100353
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000354 free(cmpbuf);
355 return ret;
356}
357
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000358/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000359 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000360 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000361 * @start offset to the base address of the flash chip
362 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000363 * @return 0 for success, -1 for failure
364 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000365int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000366{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000367 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000368 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000369
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100370 if (start + len > flash->chip->total_size * 1024) {
371 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
372 " total_size 0x%x\n", __func__, start, len,
373 flash->chip->total_size * 1024);
374 return -1;
375 }
376
Stefan Taunerdf64a422014-05-27 00:06:14 +0000377 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000378 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100379 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000380 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000381 }
382
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100383 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000384 if (ret) {
385 msg_gerr("Verification impossible because read failed "
386 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000387 ret = -1;
388 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000389 }
390
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000391 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000392out_free:
393 free(readbuf);
394 return ret;
395}
396
Stefan Tauner02437452013-04-01 19:34:53 +0000397/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300398static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
399 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000400{
401 unsigned int i, j, limit;
402 for (j = 0; j < len / gran; j++) {
403 limit = min (gran, len - j * gran);
404 /* Are 'have' and 'want' identical? */
405 if (!memcmp(have + j * gran, want + j * gran, limit))
406 continue;
407 /* have needs to be in erased state. */
408 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300409 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000410 return 1;
411 }
412 return 0;
413}
414
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000415/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000416 * Check if the buffer @have can be programmed to the content of @want without
417 * erasing. This is only possible if all chunks of size @gran are either kept
418 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000419 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000420 * Warning: This function assumes that @have and @want point to naturally
421 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000422 *
423 * @have buffer with current content
424 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000425 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000426 * @gran write granularity (enum, not count)
427 * @return 0 if no erase is needed, 1 otherwise
428 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000429static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
430 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000431{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000432 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000433 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000434
435 switch (gran) {
436 case write_gran_1bit:
437 for (i = 0; i < len; i++)
438 if ((have[i] & want[i]) != want[i]) {
439 result = 1;
440 break;
441 }
442 break;
443 case write_gran_1byte:
444 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300445 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000446 result = 1;
447 break;
448 }
449 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000450 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300451 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000452 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000453 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300454 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000455 break;
456 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300457 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000458 break;
459 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300460 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000461 break;
462 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300463 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000464 break;
465 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300466 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000467 break;
468 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300469 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000470 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000471 case write_gran_1byte_implicit_erase:
472 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
473 result = 0;
474 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000475 default:
476 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000477 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000478 }
479 return result;
480}
481
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000482/**
483 * Check if the buffer @have needs to be programmed to get the content of @want.
484 * If yes, return 1 and fill in first_start with the start address of the
485 * write operation and first_len with the length of the first to-be-written
486 * chunk. If not, return 0 and leave first_start and first_len undefined.
487 *
488 * Warning: This function assumes that @have and @want point to naturally
489 * aligned regions.
490 *
491 * @have buffer with current content
492 * @want buffer with desired content
493 * @len length of the checked area
494 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000495 * @first_start offset of the first byte which needs to be written (passed in
496 * value is increased by the offset of the first needed write
497 * relative to have/want or unchanged if no write is needed)
498 * @return length of the first contiguous area which needs to be written
499 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000500 *
501 * FIXME: This function needs a parameter which tells it about coalescing
502 * in relation to the max write length of the programmer and the max write
503 * length of the chip.
504 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000505static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000506 unsigned int *first_start,
507 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000508{
Felix Singerf25447e2022-08-19 02:44:28 +0200509 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000510 unsigned int rel_start = 0, first_len = 0;
511 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000512
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000513 switch (gran) {
514 case write_gran_1bit:
515 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000516 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000517 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000518 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000519 case write_gran_128bytes:
520 stride = 128;
521 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000522 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000523 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000524 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000525 case write_gran_264bytes:
526 stride = 264;
527 break;
528 case write_gran_512bytes:
529 stride = 512;
530 break;
531 case write_gran_528bytes:
532 stride = 528;
533 break;
534 case write_gran_1024bytes:
535 stride = 1024;
536 break;
537 case write_gran_1056bytes:
538 stride = 1056;
539 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000540 default:
541 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000542 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000543 /* Claim that no write was needed. A write with unknown
544 * granularity is too dangerous to try.
545 */
546 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000547 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000548 for (i = 0; i < len / stride; i++) {
549 limit = min(stride, len - i * stride);
550 /* Are 'have' and 'want' identical? */
551 if (memcmp(have + i * stride, want + i * stride, limit)) {
552 if (!need_write) {
553 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200554 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000555 rel_start = i * stride;
556 }
557 } else {
558 if (need_write) {
559 /* First location where have and want
560 * do not differ anymore.
561 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000562 break;
563 }
564 }
565 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000566 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000567 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000568 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000569 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000570}
571
Martin Rothf6c1cb12022-03-15 10:55:25 -0600572/* Returns the number of buses commonly supported by the current programmer and flash chip where the latter
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000573 * can not be completely accessed due to size/address limits of the programmer. */
574unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000575{
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000576 unsigned int limitexceeded = 0;
577 uint32_t size = flash->chip->total_size * 1024;
578 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000579
580 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000581 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000582 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000583 "size %u kB of chipset/board/programmer "
584 "for %s interface, "
585 "probe/read/erase/write may fail. ", size / 1024,
586 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000587 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000588 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000589 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000590 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000591 "size %u kB of chipset/board/programmer "
592 "for %s interface, "
593 "probe/read/erase/write may fail. ", size / 1024,
594 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000595 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000596 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000597 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000598 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000599 "size %u kB of chipset/board/programmer "
600 "for %s interface, "
601 "probe/read/erase/write may fail. ", size / 1024,
602 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000603 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000604 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000605 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000606 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000607 "size %u kB of chipset/board/programmer "
608 "for %s interface, "
609 "probe/read/erase/write may fail. ", size / 1024,
610 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000611 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000612 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000613}
614
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000615void unmap_flash(struct flashctx *flash)
616{
617 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
618 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
619 flash->physical_registers = 0;
620 flash->virtual_registers = (chipaddr)ERROR_PTR;
621 }
622
623 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
624 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
625 flash->physical_memory = 0;
626 flash->virtual_memory = (chipaddr)ERROR_PTR;
627 }
628}
629
630int map_flash(struct flashctx *flash)
631{
632 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
633 flash->virtual_memory = (chipaddr)ERROR_PTR;
634 flash->virtual_registers = (chipaddr)ERROR_PTR;
635
636 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
637 * These are used for various probing-related hacks that would not map successfully anyway and should be
638 * removed ASAP. */
639 if (flash->chip->total_size == 0)
640 return 0;
641
642 const chipsize_t size = flash->chip->total_size * 1024;
643 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
644 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
645 if (addr == ERROR_PTR) {
646 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
647 flash->chip->name, PRIxPTR_WIDTH, base);
648 return 1;
649 }
650 flash->physical_memory = base;
651 flash->virtual_memory = (chipaddr)addr;
652
653 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
654 * completely different on some chips and programmers, or not mappable at all.
655 * Ignore these problems for now and always report success. */
656 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
657 base = 0xffffffff - size - 0x400000 + 1;
658 addr = programmer_map_flash_region("flash chip registers", base, size);
659 if (addr == ERROR_PTR) {
660 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
661 flash->chip->name, PRIxPTR_WIDTH, base);
662 return 0;
663 }
664 flash->physical_registers = base;
665 flash->virtual_registers = (chipaddr)addr;
666 }
667 return 0;
668}
669
Nico Huber2d625722016-05-03 10:48:02 +0200670/*
671 * Return a string corresponding to the bustype parameter.
672 * Memory is obtained with malloc() and must be freed with free() by the caller.
673 */
674char *flashbuses_to_text(enum chipbustype bustype)
675{
676 char *ret = calloc(1, 1);
677 /*
678 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
679 * will cease to exist and should be eliminated here as well.
680 */
681 if (bustype == BUS_NONSPI) {
682 ret = strcat_realloc(ret, "Non-SPI, ");
683 } else {
684 if (bustype & BUS_PARALLEL)
685 ret = strcat_realloc(ret, "Parallel, ");
686 if (bustype & BUS_LPC)
687 ret = strcat_realloc(ret, "LPC, ");
688 if (bustype & BUS_FWH)
689 ret = strcat_realloc(ret, "FWH, ");
690 if (bustype & BUS_SPI)
691 ret = strcat_realloc(ret, "SPI, ");
692 if (bustype & BUS_PROG)
693 ret = strcat_realloc(ret, "Programmer-specific, ");
694 if (bustype == BUS_NONE)
695 ret = strcat_realloc(ret, "None, ");
696 }
697 /* Kill last comma. */
698 ret[strlen(ret) - 2] = '\0';
699 ret = realloc(ret, strlen(ret) + 1);
700 return ret;
701}
702
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000703int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000704{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000705 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000706 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000707 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000708
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000709 for (chip = flashchips + startchip; chip && chip->name; chip++) {
710 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000711 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000712 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000713 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000714 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300715 /* Only probe for SPI25 chips by default. */
716 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
717 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000718 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
719 if (!chip->probe && !force) {
720 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000721 continue;
722 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000723
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000724 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200725 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000726 if (!flash->chip) {
727 msg_gerr("Out of memory!\n");
728 exit(1);
729 }
Angel Pons7e134562021-06-07 13:29:13 +0200730 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000731 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000732
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000733 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +0100734 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000735
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000736 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
737 * is only called with force=1 after normal probing failed.
738 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000739 if (force)
740 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000741
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000742 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000743 goto notfound;
744
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000745 /* If this is the first chip found, accept it.
746 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000747 * a non-generic match. SFDP and CFI are generic matches.
748 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000749 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000750 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000751 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000752 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000753 msg_cinfo("===\n"
754 "SFDP has autodetected a flash chip which is "
755 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000756 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000757 msg_cinfo("The standard operations read and "
758 "verify should work, but to support "
759 "erase, write and all other "
760 "possible features");
761 else
762 msg_cinfo("All standard operations (read, "
763 "verify, erase and write) should "
764 "work, but to support all possible "
765 "features");
766
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000767 msg_cinfo(" we need to add them manually.\n"
768 "You can help us by mailing us the output of the following command to "
Nico Huberac90af62022-12-18 00:22:47 +0000769 "flashrom-stable@flashrom.org:\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000770 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
771 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000772 "===\n");
773 }
774
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000775 /* First flash chip detected on this bus. */
776 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000777 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000778 /* Not the first flash chip detected on this bus, but not a generic match either. */
779 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
780 break;
781 /* 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 +0000782notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000783 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000784 free(flash->chip);
785 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000786 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000787
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000788 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000789 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000790
Nico Huber5bd990c2019-06-16 19:46:46 +0200791 /* Fill default layout covering the whole chip. */
Nico Huber671c0f02019-06-16 20:17:19 +0200792 if (flashrom_layout_new(&flash->default_layout) ||
Nico Huber5bd990c2019-06-16 19:46:46 +0200793 flashrom_layout_add_region(flash->default_layout,
Nico Huber92e0b622019-06-15 15:55:11 +0200794 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huber5bd990c2019-06-16 19:46:46 +0200795 flashrom_layout_include_region(flash->default_layout, "complete flash"))
796 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000797
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000798 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000799 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
800 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000801 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000802#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200803 if (programmer->map_flash_region == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000804 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
805 PRIxPTR_WIDTH, flash->physical_memory);
806 else
807#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200808 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000809
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000810 /* Flash registers may more likely not be mapped if the chip was forced.
811 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000812 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000813 if (flash->chip->printlock)
814 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000815
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000816 /* Get out of the way for later runs. */
817 unmap_flash(flash);
818
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000819 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000820 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000821}
822
Stefan Tauner96658be2014-05-26 22:05:31 +0000823/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000824static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000825{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000826 int i, j, k;
827 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530828 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000829
830 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
831 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000832 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530833 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000834
835 for (i = 0; i < NUM_ERASEREGIONS; i++) {
836 /* Blocks with zero size are bugs in flashchips.c. */
837 if (eraser.eraseblocks[i].count &&
838 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000839 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
840 "Please report a bug at flashrom-stable@flashrom.org\n",
841 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000842 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000843 }
844 /* Blocks with zero count are bugs in flashchips.c. */
845 if (!eraser.eraseblocks[i].count &&
846 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000847 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
848 "Please report a bug at flashrom-stable@flashrom.org\n",
849 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000850 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000851 }
852 done += eraser.eraseblocks[i].count *
853 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530854 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000855 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000856 /* Empty eraseblock definition with erase function. */
857 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000858 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000859 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000860 if (!done)
861 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000862 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000863 msg_gerr("ERROR: Flash chip %s erase function %i "
864 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000865 " expected 0x%06x bytes.\n"
866 "Please report a bug at flashrom-stable@flashrom.org\n",
867 chip->name, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000868 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000869 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000870 if (!eraser.block_erase)
871 continue;
872 /* Check if there are identical erase functions for different
873 * layouts. That would imply "magic" erase functions. The
874 * easiest way to check this is with function pointers.
875 */
Uwe Hermann43959702010-03-13 17:28:29 +0000876 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000877 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000878 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000879 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
880 "Please report a bug at flashrom-stable@flashrom.org\n",
881 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000882 ret = 1;
883 }
Uwe Hermann43959702010-03-13 17:28:29 +0000884 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530885 if(curr_eraseblock_count > prev_eraseblock_count)
886 {
Nico Huberac90af62022-12-18 00:22:47 +0000887 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
888 "Please report a bug at flashrom-stable@flashrom.org\n",
889 chip->name, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530890 ret = 1;
891 }
892 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000893 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000894 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000895}
896
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000897static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000898{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000899 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000900
901 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
902 if (log)
903 msg_cdbg("not defined. ");
904 return 1;
905 }
906 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
907 if (log)
908 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000909 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000910 return 1;
911 }
912 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
913 if (log)
914 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000915 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000916 return 1;
917 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000918 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000919 return 0;
920}
921
Nico Huber7af0e792016-04-29 16:40:15 +0200922/**
923 * @brief Reads the included layout regions into a buffer.
924 *
925 * If there is no layout set in the given flash context, the whole chip will
926 * be read.
927 *
928 * @param flashctx Flash context to be used.
929 * @param buffer Buffer of full chip size to read into.
930 * @return 0 on success,
931 * 1 if any read fails.
932 */
933static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
934{
935 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200936 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200937
Nico Huber5ca55232019-06-15 22:29:08 +0200938 while ((entry = layout_next_included(layout, entry))) {
939 const chipoff_t region_start = entry->start;
940 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200941
942 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
943 return 1;
944 }
945 return 0;
946}
947
948typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
949/**
950 * @private
951 *
952 * For read-erase-write, `curcontents` and `newcontents` shall point
953 * to buffers of the chip's size. Both are supposed to be prefilled
954 * with at least the included layout regions of the current flash
955 * contents (`curcontents`) and the data to be written to the flash
956 * (`newcontents`).
957 *
958 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
959 *
960 * The `chipoff_t` values are used internally by `walk_by_layout()`.
961 */
962struct walk_info {
963 uint8_t *curcontents;
964 const uint8_t *newcontents;
965 chipoff_t region_start;
966 chipoff_t region_end;
967 chipoff_t erase_start;
968 chipoff_t erase_end;
969};
970/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
971typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
972
973static int walk_eraseblocks(struct flashctx *const flashctx,
974 struct walk_info *const info,
975 const size_t erasefunction, const per_blockfn_t per_blockfn)
976{
977 int ret;
978 size_t i, j;
979 bool first = true;
980 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
981
982 info->erase_start = 0;
983 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
984 /* count==0 for all automatically initialized array
985 members so the loop below won't be executed for them. */
986 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
987 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
988
989 /* Skip any eraseblock that is completely outside the current region. */
990 if (info->erase_end < info->region_start)
991 continue;
992 if (info->region_end < info->erase_start)
993 break;
994
995 /* Print this for every block except the first one. */
996 if (first)
997 first = false;
998 else
999 msg_cdbg(", ");
1000 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1001
1002 ret = per_blockfn(flashctx, info, eraser->block_erase);
1003 if (ret)
1004 return ret;
1005 }
1006 if (info->region_end < info->erase_start)
1007 break;
1008 }
1009 msg_cdbg("\n");
1010 return 0;
1011}
1012
1013static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1014 const per_blockfn_t per_blockfn)
1015{
1016 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001017 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001018
1019 all_skipped = true;
1020 msg_cinfo("Erasing and writing flash chip... ");
1021
Nico Huber5ca55232019-06-15 22:29:08 +02001022 while ((entry = layout_next_included(layout, entry))) {
1023 info->region_start = entry->start;
1024 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001025
1026 size_t j;
1027 int error = 1; /* retry as long as it's 1 */
1028 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1029 if (j != 0)
1030 msg_cinfo("Looking for another erase function.\n");
1031 msg_cdbg("Trying erase function %zi... ", j);
1032 if (check_block_eraser(flashctx, j, 1))
1033 continue;
1034
1035 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1036 if (error != 1)
1037 break;
1038
1039 if (info->curcontents) {
1040 msg_cinfo("Reading current flash chip contents... ");
1041 if (read_by_layout(flashctx, info->curcontents)) {
1042 /* Now we are truly screwed. Read failed as well. */
1043 msg_cerr("Can't read anymore! Aborting.\n");
1044 /* We have no idea about the flash chip contents, so
1045 retrying with another erase function is pointless. */
1046 error = 2;
1047 break;
1048 }
1049 msg_cinfo("done. ");
1050 }
1051 }
1052 if (error == 1)
1053 msg_cinfo("No usable erase functions left.\n");
1054 if (error) {
1055 msg_cerr("FAILED!\n");
1056 return 1;
1057 }
1058 }
1059 if (all_skipped)
1060 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1061 msg_cinfo("Erase/write done.\n");
1062 return 0;
1063}
1064
1065static int erase_block(struct flashctx *const flashctx,
1066 const struct walk_info *const info, const erasefn_t erasefn)
1067{
1068 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001069 const bool region_unaligned = info->region_start > info->erase_start ||
1070 info->erase_end > info->region_end;
1071 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1072 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001073
Nico Huber6e61e0c2019-01-23 17:07:49 +01001074 /*
1075 * If the region is not erase-block aligned, merge current flash con-
1076 * tents into a new buffer `backup_contents`.
1077 */
1078 if (region_unaligned) {
1079 backup_contents = malloc(erase_len);
1080 erased_contents = malloc(erase_len);
1081 if (!backup_contents || !erased_contents) {
1082 msg_cerr("Out of memory!\n");
1083 ret = 1;
1084 goto _free_ret;
1085 }
1086 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1087 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1088
1089 msg_cdbg("R");
1090 /* Merge data preceding the current region. */
1091 if (info->region_start > info->erase_start) {
1092 const chipoff_t start = info->erase_start;
1093 const chipsize_t len = info->region_start - info->erase_start;
1094 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1095 msg_cerr("Can't read! Aborting.\n");
1096 goto _free_ret;
1097 }
1098 }
1099 /* Merge data following the current region. */
1100 if (info->erase_end > info->region_end) {
1101 const chipoff_t start = info->region_end + 1;
1102 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1103 const chipsize_t len = info->erase_end - info->region_end;
1104 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1105 msg_cerr("Can't read! Aborting.\n");
1106 goto _free_ret;
1107 }
1108 }
1109 }
1110
1111 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001112 all_skipped = false;
1113
1114 msg_cdbg("E");
1115 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001116 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001117 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1118 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001119 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001120 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001121
1122 if (region_unaligned) {
1123 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1124 /* get_next_write() sets starthere to a new value after the call. */
1125 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1126 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1127 if (!writecount++)
1128 msg_cdbg("W");
1129 /* Needs the partial write function signature. */
1130 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1131 info->erase_start + starthere, lenhere))
1132 goto _free_ret;
1133 starthere += lenhere;
1134 }
1135 }
1136
1137 ret = 0;
1138
1139_free_ret:
1140 free(erased_contents);
1141 free(backup_contents);
1142 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001143}
1144
1145/**
1146 * @brief Erases the included layout regions.
1147 *
1148 * If there is no layout set in the given flash context, the whole chip will
1149 * be erased.
1150 *
1151 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001152 * @return 0 on success,
1153 * 1 if all available erase functions failed.
1154 */
Nico Huber454f6132012-12-10 13:34:10 +00001155static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001156{
1157 struct walk_info info = { 0 };
1158 return walk_by_layout(flashctx, &info, &erase_block);
1159}
1160
1161static int read_erase_write_block(struct flashctx *const flashctx,
1162 const struct walk_info *const info, const erasefn_t erasefn)
1163{
1164 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1165 const bool region_unaligned = info->region_start > info->erase_start ||
1166 info->erase_end > info->region_end;
1167 const uint8_t *newcontents = NULL;
1168 int ret = 2;
1169
1170 /*
1171 * If the region is not erase-block aligned, merge current flash con-
1172 * tents into `info->curcontents` and a new buffer `newc`. The former
1173 * is necessary since we have no guarantee that the full erase block
1174 * was already read into `info->curcontents`. For the latter a new
1175 * buffer is used since `info->newcontents` might contain data for
1176 * other unaligned regions that touch this erase block too.
1177 */
1178 if (region_unaligned) {
1179 msg_cdbg("R");
1180 uint8_t *const newc = malloc(erase_len);
1181 if (!newc) {
1182 msg_cerr("Out of memory!\n");
1183 return 1;
1184 }
1185 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1186
1187 /* Merge data preceding the current region. */
1188 if (info->region_start > info->erase_start) {
1189 const chipoff_t start = info->erase_start;
1190 const chipsize_t len = info->region_start - info->erase_start;
1191 if (flashctx->chip->read(flashctx, newc, start, len)) {
1192 msg_cerr("Can't read! Aborting.\n");
1193 goto _free_ret;
1194 }
1195 memcpy(info->curcontents + start, newc, len);
1196 }
1197 /* Merge data following the current region. */
1198 if (info->erase_end > info->region_end) {
1199 const chipoff_t start = info->region_end + 1;
1200 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1201 const chipsize_t len = info->erase_end - info->region_end;
1202 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1203 msg_cerr("Can't read! Aborting.\n");
1204 goto _free_ret;
1205 }
1206 memcpy(info->curcontents + start, newc + rel_start, len);
1207 }
1208
1209 newcontents = newc;
1210 } else {
1211 newcontents = info->newcontents + info->erase_start;
1212 }
1213
1214 ret = 1;
1215 bool skipped = true;
1216 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001217 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001218 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1219 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001220 if (erase_block(flashctx, info, erasefn))
1221 goto _free_ret;
1222 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001223 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001224 skipped = false;
1225 }
1226
1227 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1228 /* get_next_write() sets starthere to a new value after the call. */
1229 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1230 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1231 if (!writecount++)
1232 msg_cdbg("W");
1233 /* Needs the partial write function signature. */
1234 if (flashctx->chip->write(flashctx, newcontents + starthere,
1235 info->erase_start + starthere, lenhere))
1236 goto _free_ret;
1237 starthere += lenhere;
1238 skipped = false;
1239 }
1240 if (skipped)
1241 msg_cdbg("S");
1242 else
1243 all_skipped = false;
1244
1245 /* Update curcontents, other regions with overlapping erase blocks
1246 might rely on this. */
1247 memcpy(curcontents, newcontents, erase_len);
1248 ret = 0;
1249
1250_free_ret:
1251 if (region_unaligned)
1252 free((void *)newcontents);
1253 return ret;
1254}
1255
1256/**
1257 * @brief Writes the included layout regions from a given image.
1258 *
1259 * If there is no layout set in the given flash context, the whole image
1260 * will be written.
1261 *
1262 * @param flashctx Flash context to be used.
1263 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1264 * @param newcontents The new image to be written.
1265 * @return 0 on success,
1266 * 1 if anything has gone wrong.
1267 */
Nico Huber454f6132012-12-10 13:34:10 +00001268static int write_by_layout(struct flashctx *const flashctx,
1269 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001270{
1271 struct walk_info info;
1272 info.curcontents = curcontents;
1273 info.newcontents = newcontents;
1274 return walk_by_layout(flashctx, &info, read_erase_write_block);
1275}
1276
1277/**
1278 * @brief Compares the included layout regions with content from a buffer.
1279 *
1280 * If there is no layout set in the given flash context, the whole chip's
1281 * contents will be compared.
1282 *
1283 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001284 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001285 * @param curcontents A buffer of full chip size to read current chip contents into.
1286 * @param newcontents The new image to compare to.
1287 * @return 0 on success,
1288 * 1 if reading failed,
1289 * 3 if the contents don't match.
1290 */
Nico Huber74d09d42019-06-16 03:27:26 +02001291static int verify_by_layout(
1292 struct flashctx *const flashctx,
1293 const struct flashrom_layout *const layout,
1294 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001295{
Nico Huber5ca55232019-06-15 22:29:08 +02001296 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001297
Nico Huber5ca55232019-06-15 22:29:08 +02001298 while ((entry = layout_next_included(layout, entry))) {
1299 const chipoff_t region_start = entry->start;
1300 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001301
1302 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1303 return 1;
1304 if (compare_range(newcontents + region_start, curcontents + region_start,
1305 region_start, region_len))
1306 return 3;
1307 }
1308 return 0;
1309}
1310
Stefan Tauner136388f2013-07-15 10:47:53 +00001311static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001312{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001313 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001314#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001315 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001316 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberac90af62022-12-18 00:22:47 +00001317 "chip. Please report this to the mailing list at flashrom-stable@flashrom.org or\n"
1318 "on IRC (see https://www.flashrom.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001319 "-------------------------------------------------------------------------------\n"
1320 "You may now reboot or simply leave the machine running.\n");
1321 else
1322#endif
1323 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1324 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
Nico Huberac90af62022-12-18 00:22:47 +00001325 "please report this to the mailing list at flashrom-stable@flashrom.org or on IRC\n"
1326 "(see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001327}
1328
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001329void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001330{
Stefan Tauner136388f2013-07-15 10:47:53 +00001331 msg_gerr("Your flash chip is in an unknown state.\n");
1332#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001333 if (programmer == &programmer_internal)
Angel Pons1900e1d2021-07-02 12:42:23 +02001334 msg_gerr("Get help on IRC (see https://www.flashrom.org/Contact) or mail\n"
Nico Huberac90af62022-12-18 00:22:47 +00001335 "flashrom-stable@flashrom.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001336 "-------------------------------------------------------------------------------\n"
1337 "DO NOT REBOOT OR POWEROFF!\n");
1338 else
1339#endif
Nico Huberac90af62022-12-18 00:22:47 +00001340 msg_gerr("Please report this to the mailing list at flashrom-stable@flashrom.org\n"
1341 "or on IRC (see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001342}
1343
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001344void list_programmers_linebreak(int startcol, int cols, int paren)
1345{
1346 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001347 int pnamelen;
1348 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001349 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001350 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001351
Thomas Heijligend45cb592021-05-19 14:12:18 +02001352 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001353 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001354 pnamelen = strlen(pname);
1355 if (remaining - pnamelen - 2 < 0) {
1356 if (firstline)
1357 firstline = 0;
1358 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001359 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001360 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001361 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001362 remaining = cols - startcol;
1363 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001364 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001365 remaining--;
1366 }
1367 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001368 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001369 remaining--;
1370 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001371 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001372 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001373 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001374 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001375 remaining--;
1376 } else {
1377 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001378 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001379 }
1380 }
1381}
1382
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001383int selfcheck(void)
1384{
Stefan Tauner96658be2014-05-26 22:05:31 +00001385 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001386 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001387
Thomas Heijligend45cb592021-05-19 14:12:18 +02001388 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001389 const struct programmer_entry *const p = programmer_table[i];
1390 if (p == NULL) {
1391 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1392 ret = 1;
1393 continue;
1394 }
1395 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001396 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1397 ret = 1;
1398 /* This might hide other problems with this programmer, but allows for better error
1399 * messages below without jumping through hoops. */
1400 continue;
1401 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001402 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001403 case USB:
1404 case PCI:
1405 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001406 if (p->devs.note == NULL) {
1407 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001408 break; /* This one has its device list stored separately. */
1409 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001410 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001411 ret = 1;
1412 }
1413 break;
1414 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001415 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001416 ret = 1;
1417 break;
1418 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001419 if (p->init == NULL) {
1420 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001421 ret = 1;
1422 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001423 if (p->map_flash_region == NULL) {
1424 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001425 ret = 1;
1426 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001427 if (p->unmap_flash_region == NULL) {
1428 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001429 ret = 1;
1430 }
1431 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001432
1433 /* It would be favorable if we could check for the correct layout (especially termination) of various
1434 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1435 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1436 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1437 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1438 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001439 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001440 msg_gerr("Flashchips table miscompilation!\n");
1441 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001442 } else {
1443 for (i = 0; i < flashchips_size - 1; i++) {
1444 const struct flashchip *chip = &flashchips[i];
1445 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1446 ret = 1;
1447 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
Nico Huberac90af62022-12-18 00:22:47 +00001448 "Please report a bug at flashrom-stable@flashrom.org\n", i,
Stefan Tauner96658be2014-05-26 22:05:31 +00001449 chip->name == NULL ? "unnamed" : chip->name);
1450 }
1451 if (selfcheck_eraseblocks(chip)) {
1452 ret = 1;
1453 }
1454 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001455 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001456
Stefan Tauner600576b2014-06-12 22:57:36 +00001457#if CONFIG_INTERNAL == 1
1458 ret |= selfcheck_board_enables();
1459#endif
1460
Stefan Tauner96658be2014-05-26 22:05:31 +00001461 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001462 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001463}
1464
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001465/* FIXME: This function signature needs to be improved once prepare_flash_access()
1466 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001467 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001468static int chip_safety_check(const struct flashctx *flash, int force,
1469 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001470{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001471 const struct flashchip *chip = flash->chip;
1472
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001473 if (!programmer_may_write && (write_it || erase_it)) {
1474 msg_perr("Write/erase is not working yet on your programmer in "
1475 "its current configuration.\n");
1476 /* --force is the wrong approach, but it's the best we can do
1477 * until the generic programmer parameter parser is merged.
1478 */
1479 if (!force)
1480 return 1;
1481 msg_cerr("Continuing anyway.\n");
1482 }
1483
1484 if (read_it || erase_it || write_it || verify_it) {
1485 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001486 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001487 msg_cerr("Read is not working on this chip. ");
1488 if (!force)
1489 return 1;
1490 msg_cerr("Continuing anyway.\n");
1491 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001492 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001493 msg_cerr("flashrom has no read function for this "
1494 "flash chip.\n");
1495 return 1;
1496 }
1497 }
1498 if (erase_it || write_it) {
1499 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001500 if (chip->tested.erase == NA) {
1501 msg_cerr("Erase is not possible on this chip.\n");
1502 return 1;
1503 }
1504 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001505 msg_cerr("Erase is not working on this chip. ");
1506 if (!force)
1507 return 1;
1508 msg_cerr("Continuing anyway.\n");
1509 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001510 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001511 msg_cerr("flashrom has no erase function for this "
1512 "flash chip.\n");
1513 return 1;
1514 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001515 }
1516 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001517 if (chip->tested.write == NA) {
1518 msg_cerr("Write is not possible on this chip.\n");
1519 return 1;
1520 }
1521 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001522 msg_cerr("Write is not working on this chip. ");
1523 if (!force)
1524 return 1;
1525 msg_cerr("Continuing anyway.\n");
1526 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001527 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001528 msg_cerr("flashrom has no write function for this "
1529 "flash chip.\n");
1530 return 1;
1531 }
1532 }
1533 return 0;
1534}
1535
Nico Huber305f4172013-06-14 11:55:26 +02001536int prepare_flash_access(struct flashctx *const flash,
1537 const bool read_it, const bool write_it,
1538 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001539{
1540 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1541 msg_cerr("Aborting.\n");
1542 return 1;
1543 }
1544
Nico Hubere0ed4122021-05-14 00:48:28 +02001545 if (layout_sanity_checks(flash)) {
Nico Huber454f6132012-12-10 13:34:10 +00001546 msg_cerr("Requested regions can not be handled. Aborting.\n");
1547 return 1;
1548 }
1549
1550 if (map_flash(flash) != 0)
1551 return 1;
1552
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001553 /* Initialize chip_restore_fn_count before chip unlock calls. */
1554 flash->chip_restore_fn_count = 0;
1555
Nico Huber454f6132012-12-10 13:34:10 +00001556 /* Given the existence of read locks, we want to unlock for read,
1557 erase and write. */
1558 if (flash->chip->unlock)
1559 flash->chip->unlock(flash);
1560
Nico Huberf43c6542017-10-14 17:47:28 +02001561 flash->address_high_byte = -1;
1562 flash->in_4ba_mode = false;
1563
Nico Huberdc5af542018-12-22 16:54:59 +01001564 /* Be careful about 4BA chips and broken masters */
1565 if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
1566 /* If we can't use native instructions, bail out */
1567 if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
1568 || !spi_master_4ba(flash)) {
1569 msg_cerr("Programmer doesn't support this chip. Aborting.\n");
1570 return 1;
1571 }
1572 }
1573
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001574 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01001575 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01001576 int ret;
1577 if (spi_master_4ba(flash))
1578 ret = spi_enter_4ba(flash);
1579 else
1580 ret = spi_exit_4ba(flash);
1581 if (ret) {
1582 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001583 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02001584 }
Boris Baykov99127182016-06-11 18:29:00 +02001585 }
1586
Nico Huber454f6132012-12-10 13:34:10 +00001587 return 0;
1588}
1589
Nico Huber305f4172013-06-14 11:55:26 +02001590void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001591{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001592 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001593 unmap_flash(flash);
1594}
1595
1596/**
1597 * @addtogroup flashrom-flash
1598 * @{
1599 */
1600
1601/**
1602 * @brief Erase the specified ROM chip.
1603 *
1604 * If a layout is set in the given flash context, only included regions
1605 * will be erased.
1606 *
1607 * @param flashctx The context of the flash chip to erase.
1608 * @return 0 on success.
1609 */
1610int flashrom_flash_erase(struct flashctx *const flashctx)
1611{
1612 if (prepare_flash_access(flashctx, false, false, true, false))
1613 return 1;
1614
1615 const int ret = erase_by_layout(flashctx);
1616
1617 finalize_flash_access(flashctx);
1618
1619 return ret;
1620}
1621
1622/** @} */ /* end flashrom-flash */
1623
1624/**
1625 * @defgroup flashrom-ops Operations
1626 * @{
1627 */
1628
1629/**
1630 * @brief Read the current image from the specified ROM chip.
1631 *
1632 * If a layout is set in the specified flash context, only included regions
1633 * will be read.
1634 *
1635 * @param flashctx The context of the flash chip.
1636 * @param buffer Target buffer to write image to.
1637 * @param buffer_len Size of target buffer in bytes.
1638 * @return 0 on success,
1639 * 2 if buffer_len is too short for the flash chip's contents,
1640 * or 1 on any other failure.
1641 */
1642int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
1643{
1644 const size_t flash_size = flashctx->chip->total_size * 1024;
1645
1646 if (flash_size > buffer_len)
1647 return 2;
1648
1649 if (prepare_flash_access(flashctx, true, false, false, false))
1650 return 1;
1651
1652 msg_cinfo("Reading flash... ");
1653
1654 int ret = 1;
1655 if (read_by_layout(flashctx, buffer)) {
1656 msg_cerr("Read operation failed!\n");
1657 msg_cinfo("FAILED.\n");
1658 goto _finalize_ret;
1659 }
1660 msg_cinfo("done.\n");
1661 ret = 0;
1662
1663_finalize_ret:
1664 finalize_flash_access(flashctx);
1665 return ret;
1666}
1667
1668static void combine_image_by_layout(const struct flashctx *const flashctx,
1669 uint8_t *const newcontents, const uint8_t *const oldcontents)
1670{
1671 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001672 const struct romentry *included;
1673 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001674
Nico Huber3d7b1e32018-12-22 00:53:14 +01001675 while ((included = layout_next_included_region(layout, start))) {
1676 if (included->start > start) {
1677 /* copy everything up to the start of this included region */
1678 memcpy(newcontents + start, oldcontents + start, included->start - start);
1679 }
1680 /* skip this included region */
1681 start = included->end + 1;
1682 if (start == 0)
1683 return;
Nico Huber454f6132012-12-10 13:34:10 +00001684 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001685
1686 /* copy the rest of the chip */
1687 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1688 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001689}
1690
1691/**
1692 * @brief Write the specified image to the ROM chip.
1693 *
1694 * If a layout is set in the specified flash context, only erase blocks
1695 * containing included regions will be touched.
1696 *
1697 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001698 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001699 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001700 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001701 * @return 0 on success,
1702 * 4 if buffer_len doesn't match the size of the flash chip,
1703 * 3 if write was tried but nothing has changed,
1704 * 2 if write failed and flash contents changed,
1705 * or 1 on any other failure.
1706 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001707int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
1708 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001709{
1710 const size_t flash_size = flashctx->chip->total_size * 1024;
1711 const bool verify_all = flashctx->flags.verify_whole_chip;
1712 const bool verify = flashctx->flags.verify_after_write;
Nico Huber74d09d42019-06-16 03:27:26 +02001713 const struct flashrom_layout *const verify_layout =
1714 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001715
1716 if (buffer_len != flash_size)
1717 return 4;
1718
1719 int ret = 1;
1720
1721 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001722 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001723 uint8_t *const curcontents = malloc(flash_size);
1724 uint8_t *oldcontents = NULL;
1725 if (verify_all)
1726 oldcontents = malloc(flash_size);
1727 if (!curcontents || (verify_all && !oldcontents)) {
1728 msg_gerr("Out of memory!\n");
1729 goto _free_ret;
1730 }
1731
1732#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001733 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001734 if (flashctx->flags.force_boardmismatch) {
1735 msg_pinfo("Proceeding anyway because user forced us to.\n");
1736 } else {
1737 msg_perr("Aborting. You can override this with "
1738 "-p internal:boardmismatch=force.\n");
1739 goto _free_ret;
1740 }
1741 }
1742#endif
1743
1744 if (prepare_flash_access(flashctx, false, true, false, verify))
1745 goto _free_ret;
1746
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001747 /* If given, assume flash chip contains same data as `refcontents`. */
1748 if (refcontents) {
1749 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1750 memcpy(curcontents, refcontents, flash_size);
1751 if (oldcontents)
1752 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001753 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001754 /*
1755 * Read the whole chip to be able to check whether regions need to be
1756 * erased and to give better diagnostics in case write fails.
1757 * The alternative is to read only the regions which are to be
1758 * preserved, but in that case we might perform unneeded erase which
1759 * takes time as well.
1760 */
1761 msg_cinfo("Reading old flash chip contents... ");
1762 if (verify_all) {
1763 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
1764 msg_cinfo("FAILED.\n");
1765 goto _finalize_ret;
1766 }
1767 memcpy(curcontents, oldcontents, flash_size);
1768 } else {
1769 if (read_by_layout(flashctx, curcontents)) {
1770 msg_cinfo("FAILED.\n");
1771 goto _finalize_ret;
1772 }
Nico Huber454f6132012-12-10 13:34:10 +00001773 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001774 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001775 }
Nico Huber454f6132012-12-10 13:34:10 +00001776
1777 if (write_by_layout(flashctx, curcontents, newcontents)) {
1778 msg_cerr("Uh oh. Erase/write failed. ");
1779 ret = 2;
1780 if (verify_all) {
1781 msg_cerr("Checking if anything has changed.\n");
1782 msg_cinfo("Reading current flash chip contents... ");
1783 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
1784 msg_cinfo("done.\n");
1785 if (!memcmp(oldcontents, curcontents, flash_size)) {
1786 nonfatal_help_message();
1787 goto _finalize_ret;
1788 }
1789 msg_cerr("Apparently at least some data has changed.\n");
1790 } else
1791 msg_cerr("Can't even read anymore!\n");
1792 emergency_help_message();
1793 goto _finalize_ret;
1794 } else {
1795 msg_cerr("\n");
1796 }
1797 emergency_help_message();
1798 goto _finalize_ret;
1799 }
1800
1801 /* Verify only if we actually changed something. */
1802 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001803 msg_cinfo("Verifying flash... ");
1804
1805 /* Work around chips which need some time to calm down. */
1806 programmer_delay(1000*1000);
1807
Nico Huber74d09d42019-06-16 03:27:26 +02001808 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001809 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001810 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001811 /* If we tried to write, and verification now fails, we
1812 might have an emergency situation. */
1813 if (ret)
1814 emergency_help_message();
1815 else
1816 msg_cinfo("VERIFIED.\n");
1817 } else {
1818 /* We didn't change anything. */
1819 ret = 0;
1820 }
1821
1822_finalize_ret:
1823 finalize_flash_access(flashctx);
1824_free_ret:
1825 free(oldcontents);
1826 free(curcontents);
1827 return ret;
1828}
1829
1830/**
1831 * @brief Verify the ROM chip's contents with the specified image.
1832 *
1833 * If a layout is set in the specified flash context, only included regions
1834 * will be verified.
1835 *
1836 * @param flashctx The context of the flash chip.
1837 * @param buffer Source buffer to verify with.
1838 * @param buffer_len Size of source buffer in bytes.
1839 * @return 0 on success,
1840 * 3 if the chip's contents don't match,
1841 * 2 if buffer_len doesn't match the size of the flash chip,
1842 * or 1 on any other failure.
1843 */
1844int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
1845{
Nico Huber74d09d42019-06-16 03:27:26 +02001846 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001847 const size_t flash_size = flashctx->chip->total_size * 1024;
1848
1849 if (buffer_len != flash_size)
1850 return 2;
1851
1852 const uint8_t *const newcontents = buffer;
1853 uint8_t *const curcontents = malloc(flash_size);
1854 if (!curcontents) {
1855 msg_gerr("Out of memory!\n");
1856 return 1;
1857 }
1858
1859 int ret = 1;
1860
1861 if (prepare_flash_access(flashctx, false, false, false, true))
1862 goto _free_ret;
1863
1864 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001865 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001866 if (!ret)
1867 msg_cinfo("VERIFIED.\n");
1868
1869 finalize_flash_access(flashctx);
1870_free_ret:
1871 free(curcontents);
1872 return ret;
1873}
1874
1875/** @} */ /* end flashrom-ops */