blob: c7ad92ccfd4e98e55b88b9ca3a7a41bbdba36084 [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>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000030#if HAVE_UTSNAME == 1
31#include <sys/utsname.h>
32#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000033#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000034#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000035#include "programmer.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010036#include "hwaccess_physmap.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010037#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000038
Mathias Krausea60faab2011-01-17 07:50:42 +000039const char flashrom_version[] = FLASHROM_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000040const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000041
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020042static const struct programmer_entry *programmer = NULL;
Nico Huber6a2ebeb2022-08-26 11:36:48 +020043static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000044
Uwe Hermann48ec1b12010-08-08 17:01:18 +000045/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000046 * Programmers supporting multiple buses can have differing size limits on
47 * each bus. Store the limits for each bus in a common struct.
48 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000049struct decode_sizes max_rom_decode;
50
51/* If nonzero, used as the start address of bottom-aligned flash. */
52unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000053
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000054/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020055bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000056
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000057#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000058static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000059/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000060static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000061 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000062 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000063} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000064/* Initialize to 0 to make sure nobody registers a shutdown function before
65 * programmer init.
66 */
Felix Singerf25447e2022-08-19 02:44:28 +020067static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000068
Stefan Taunerc4f44df2013-08-12 22:58:43 +000069/* Did we change something or was every erase/write skipped (if any)? */
70static bool all_skipped = true;
71
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000072static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000073
Stefan Tauner2a1ed772014-08-31 00:09:21 +000074int shutdown_free(void *data)
75{
76 free(data);
77 return 0;
78}
79
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000080/* Register a function to be executed on programmer shutdown.
81 * The advantage over atexit() is that you can supply a void pointer which will
82 * be used as parameter to the registered function upon programmer shutdown.
83 * This pointer can point to arbitrary data used by said function, e.g. undo
84 * information for GPIO settings etc. If unneeded, set data=NULL.
85 * Please note that the first (void *data) belongs to the function signature of
86 * the function passed as first parameter.
87 */
David Hendricks8bb20212011-06-14 01:35:36 +000088int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000089{
90 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000091 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000092 SHUTDOWN_MAXFN);
93 return 1;
94 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000095 if (!may_register_shutdown) {
96 msg_perr("Tried to register a shutdown function before "
97 "programmer init.\n");
98 return 1;
99 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000100 shutdown_fn[shutdown_fn_count].func = function;
101 shutdown_fn[shutdown_fn_count].data = data;
102 shutdown_fn_count++;
103
104 return 0;
105}
106
Nikolai Artemiev4ad48642020-11-05 13:54:27 +1100107int register_chip_restore(chip_restore_fn_cb_t func,
108 struct flashctx *flash, uint8_t status)
109{
110 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
111 msg_perr("Tried to register more than %i chip restore"
112 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
113 return 1;
114 }
115 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
116 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
117 flash->chip_restore_fn_count++;
118
119 return 0;
120}
121
122static int deregister_chip_restore(struct flashctx *flash)
123{
124 int rc = 0;
125
126 while (flash->chip_restore_fn_count > 0) {
127 int i = --flash->chip_restore_fn_count;
128 rc |= flash->chip_restore_fn[i].func(
129 flash, flash->chip_restore_fn[i].status);
130 }
131
132 return rc;
133}
134
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200135int programmer_init(const struct programmer_entry *prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000136{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000137 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000138
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200139 if (prog == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000140 msg_perr("Invalid programmer specified!\n");
141 return -1;
142 }
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200143 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000144 /* Initialize all programmer specific data. */
145 /* Default to unlimited decode sizes. */
146 max_rom_decode = (const struct decode_sizes) {
147 .parallel = 0xffffffff,
148 .lpc = 0xffffffff,
149 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000150 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000151 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000152 /* Default to top aligned flash at 4 GB. */
153 flashbase = 0;
154 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200155 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000156 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200157 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000158
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200159 if (param) {
160 programmer_param = strdup(param);
161 if (!programmer_param) {
162 msg_perr("Out of memory!\n");
163 return ERROR_FATAL;
164 }
165 } else {
166 programmer_param = NULL;
167 }
168
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200169 msg_pdbg("Initializing %s programmer\n", programmer->name);
170 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000171 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000172 if (ret != 0) {
173 /* It is quite possible that any unhandled programmer parameter would have been valid,
174 * but an error in actual programmer init happened before the parameter was evaluated.
175 */
176 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
177 programmer_param);
178 } else {
179 /* Actual programmer init was successful, but the user specified an invalid or unusable
180 * (for the current programmer configuration) parameter.
181 */
182 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
183 msg_perr("Aborting.\n");
184 ret = ERROR_FATAL;
185 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000186 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200187 free(programmer_param);
188 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000189 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000190}
191
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000192/** Calls registered shutdown functions and resets internal programmer-related variables.
193 * Calling it is safe even without previous initialization, but further interactions with programmer support
194 * require a call to programmer_init() (afterwards).
195 *
196 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000197int programmer_shutdown(void)
198{
David Hendricks8bb20212011-06-14 01:35:36 +0000199 int ret = 0;
200
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000201 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200202 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000203 while (shutdown_fn_count > 0) {
204 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000205 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000206 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000207 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000208
David Hendricks8bb20212011-06-14 01:35:36 +0000209 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000210}
211
Stefan Tauner305e0b92013-07-17 23:46:44 +0000212void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000213{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200214 void *ret = programmer->map_flash_region(descr, phys_addr, len);
Stefan Tauner26e7a152013-09-13 17:21:05 +0000215 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
216 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
217 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000218}
219
220void programmer_unmap_flash_region(void *virt_addr, size_t len)
221{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200222 programmer->unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000223 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000224}
225
Stefan Taunerf80419c2014-05-02 15:41:42 +0000226void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000227{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000228 if (usecs > 0)
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200229 programmer->delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000230}
231
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000232int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
233 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000234{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000235 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000236
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000237 return 0;
238}
239
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000240/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000241 * It will look for needle with a subsequent '=' in haystack, return a copy of
242 * needle and remove everything from the first occurrence of needle to the next
243 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000244 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200245static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000246{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000247 char *param_pos, *opt_pos, *rest;
248 char *opt = NULL;
249 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000250 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000251
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000252 needlelen = strlen(needle);
253 if (!needlelen) {
254 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000255 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000256 return NULL;
257 }
258 /* No programmer parameters given. */
259 if (*haystack == NULL)
260 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000261 param_pos = strstr(*haystack, needle);
262 do {
263 if (!param_pos)
264 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000265 /* Needle followed by '='? */
266 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000267 /* Beginning of the string? */
268 if (param_pos == *haystack)
269 break;
270 /* After a delimiter? */
271 if (strchr(delim, *(param_pos - 1)))
272 break;
273 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000274 /* Continue searching. */
275 param_pos++;
276 param_pos = strstr(param_pos, needle);
277 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000278
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000279 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000280 /* Get the string after needle and '='. */
281 opt_pos = param_pos + needlelen + 1;
282 optlen = strcspn(opt_pos, delim);
283 /* Return an empty string if the parameter was empty. */
284 opt = malloc(optlen + 1);
285 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000286 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000287 exit(1);
288 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000289 strncpy(opt, opt_pos, optlen);
290 opt[optlen] = '\0';
291 rest = opt_pos + optlen;
292 /* Skip all delimiters after the current parameter. */
293 rest += strspn(rest, delim);
294 memmove(param_pos, rest, strlen(rest) + 1);
295 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000296 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000297
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000298 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000299}
300
Stefan Tauner66652442011-06-26 17:38:17 +0000301char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000302{
303 return extract_param(&programmer_param, param_name, ",");
304}
305
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000306/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000307static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000308{
309 unsigned int usable_erasefunctions = 0;
310 int k;
311 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
312 if (!check_block_eraser(flash, k, 0))
313 usable_erasefunctions++;
314 }
315 return usable_erasefunctions;
316}
317
Mark Marshallf20b7be2014-05-09 21:16:21 +0000318static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000319{
320 int ret = 0, failcount = 0;
321 unsigned int i;
322 for (i = 0; i < len; i++) {
323 if (wantbuf[i] != havebuf[i]) {
324 /* Only print the first failure. */
325 if (!failcount++)
326 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
327 start + i, wantbuf[i], havebuf[i]);
328 }
329 }
330 if (failcount) {
331 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
332 start, start + len - 1, failcount);
333 ret = -1;
334 }
335 return ret;
336}
337
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000338/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600339static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000340{
341 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300342 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000343
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100344 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000345 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100346 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000347 exit(1);
348 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300349 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000350 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100351
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000352 free(cmpbuf);
353 return ret;
354}
355
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000356/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000357 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000358 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000359 * @start offset to the base address of the flash chip
360 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000361 * @return 0 for success, -1 for failure
362 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000363int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000364{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000365 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000366 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000367
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100368 if (start + len > flash->chip->total_size * 1024) {
369 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
370 " total_size 0x%x\n", __func__, start, len,
371 flash->chip->total_size * 1024);
372 return -1;
373 }
374
Stefan Taunerdf64a422014-05-27 00:06:14 +0000375 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000376 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100377 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000378 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000379 }
380
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100381 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000382 if (ret) {
383 msg_gerr("Verification impossible because read failed "
384 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000385 ret = -1;
386 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000387 }
388
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000389 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000390out_free:
391 free(readbuf);
392 return ret;
393}
394
Stefan Tauner02437452013-04-01 19:34:53 +0000395/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300396static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
397 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000398{
399 unsigned int i, j, limit;
400 for (j = 0; j < len / gran; j++) {
401 limit = min (gran, len - j * gran);
402 /* Are 'have' and 'want' identical? */
403 if (!memcmp(have + j * gran, want + j * gran, limit))
404 continue;
405 /* have needs to be in erased state. */
406 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300407 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000408 return 1;
409 }
410 return 0;
411}
412
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000413/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000414 * Check if the buffer @have can be programmed to the content of @want without
415 * erasing. This is only possible if all chunks of size @gran are either kept
416 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000417 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000418 * Warning: This function assumes that @have and @want point to naturally
419 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000420 *
421 * @have buffer with current content
422 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000423 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000424 * @gran write granularity (enum, not count)
425 * @return 0 if no erase is needed, 1 otherwise
426 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000427static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
428 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000429{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000430 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000431 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000432
433 switch (gran) {
434 case write_gran_1bit:
435 for (i = 0; i < len; i++)
436 if ((have[i] & want[i]) != want[i]) {
437 result = 1;
438 break;
439 }
440 break;
441 case write_gran_1byte:
442 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300443 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000444 result = 1;
445 break;
446 }
447 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000448 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300449 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000450 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000451 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300452 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000453 break;
454 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300455 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000456 break;
457 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300458 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000459 break;
460 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300461 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000462 break;
463 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300464 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000465 break;
466 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300467 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000468 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000469 case write_gran_1byte_implicit_erase:
470 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
471 result = 0;
472 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000473 default:
474 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000475 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000476 }
477 return result;
478}
479
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000480/**
481 * Check if the buffer @have needs to be programmed to get the content of @want.
482 * If yes, return 1 and fill in first_start with the start address of the
483 * write operation and first_len with the length of the first to-be-written
484 * chunk. If not, return 0 and leave first_start and first_len undefined.
485 *
486 * Warning: This function assumes that @have and @want point to naturally
487 * aligned regions.
488 *
489 * @have buffer with current content
490 * @want buffer with desired content
491 * @len length of the checked area
492 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000493 * @first_start offset of the first byte which needs to be written (passed in
494 * value is increased by the offset of the first needed write
495 * relative to have/want or unchanged if no write is needed)
496 * @return length of the first contiguous area which needs to be written
497 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000498 *
499 * FIXME: This function needs a parameter which tells it about coalescing
500 * in relation to the max write length of the programmer and the max write
501 * length of the chip.
502 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000503static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000504 unsigned int *first_start,
505 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000506{
Felix Singerf25447e2022-08-19 02:44:28 +0200507 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000508 unsigned int rel_start = 0, first_len = 0;
509 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000510
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000511 switch (gran) {
512 case write_gran_1bit:
513 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000514 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000515 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000516 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000517 case write_gran_128bytes:
518 stride = 128;
519 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000520 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000521 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000522 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000523 case write_gran_264bytes:
524 stride = 264;
525 break;
526 case write_gran_512bytes:
527 stride = 512;
528 break;
529 case write_gran_528bytes:
530 stride = 528;
531 break;
532 case write_gran_1024bytes:
533 stride = 1024;
534 break;
535 case write_gran_1056bytes:
536 stride = 1056;
537 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000538 default:
539 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000540 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000541 /* Claim that no write was needed. A write with unknown
542 * granularity is too dangerous to try.
543 */
544 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000545 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000546 for (i = 0; i < len / stride; i++) {
547 limit = min(stride, len - i * stride);
548 /* Are 'have' and 'want' identical? */
549 if (memcmp(have + i * stride, want + i * stride, limit)) {
550 if (!need_write) {
551 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200552 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000553 rel_start = i * stride;
554 }
555 } else {
556 if (need_write) {
557 /* First location where have and want
558 * do not differ anymore.
559 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000560 break;
561 }
562 }
563 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000564 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000565 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000566 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000567 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000568}
569
Martin Rothf6c1cb12022-03-15 10:55:25 -0600570/* Returns the number of buses commonly supported by the current programmer and flash chip where the latter
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000571 * can not be completely accessed due to size/address limits of the programmer. */
572unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000573{
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000574 unsigned int limitexceeded = 0;
575 uint32_t size = flash->chip->total_size * 1024;
576 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000577
578 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000579 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000580 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000581 "size %u kB of chipset/board/programmer "
582 "for %s interface, "
583 "probe/read/erase/write may fail. ", size / 1024,
584 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000585 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000586 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000587 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000588 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000589 "size %u kB of chipset/board/programmer "
590 "for %s interface, "
591 "probe/read/erase/write may fail. ", size / 1024,
592 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000593 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000594 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000595 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000596 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000597 "size %u kB of chipset/board/programmer "
598 "for %s interface, "
599 "probe/read/erase/write may fail. ", size / 1024,
600 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000601 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000602 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000603 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000604 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000605 "size %u kB of chipset/board/programmer "
606 "for %s interface, "
607 "probe/read/erase/write may fail. ", size / 1024,
608 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000609 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000610 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000611}
612
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000613void unmap_flash(struct flashctx *flash)
614{
615 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
616 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
617 flash->physical_registers = 0;
618 flash->virtual_registers = (chipaddr)ERROR_PTR;
619 }
620
621 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
622 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
623 flash->physical_memory = 0;
624 flash->virtual_memory = (chipaddr)ERROR_PTR;
625 }
626}
627
628int map_flash(struct flashctx *flash)
629{
630 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
631 flash->virtual_memory = (chipaddr)ERROR_PTR;
632 flash->virtual_registers = (chipaddr)ERROR_PTR;
633
634 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
635 * These are used for various probing-related hacks that would not map successfully anyway and should be
636 * removed ASAP. */
637 if (flash->chip->total_size == 0)
638 return 0;
639
640 const chipsize_t size = flash->chip->total_size * 1024;
641 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
642 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
643 if (addr == ERROR_PTR) {
644 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
645 flash->chip->name, PRIxPTR_WIDTH, base);
646 return 1;
647 }
648 flash->physical_memory = base;
649 flash->virtual_memory = (chipaddr)addr;
650
651 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
652 * completely different on some chips and programmers, or not mappable at all.
653 * Ignore these problems for now and always report success. */
654 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
655 base = 0xffffffff - size - 0x400000 + 1;
656 addr = programmer_map_flash_region("flash chip registers", base, size);
657 if (addr == ERROR_PTR) {
658 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
659 flash->chip->name, PRIxPTR_WIDTH, base);
660 return 0;
661 }
662 flash->physical_registers = base;
663 flash->virtual_registers = (chipaddr)addr;
664 }
665 return 0;
666}
667
Nico Huber2d625722016-05-03 10:48:02 +0200668/*
669 * Return a string corresponding to the bustype parameter.
670 * Memory is obtained with malloc() and must be freed with free() by the caller.
671 */
672char *flashbuses_to_text(enum chipbustype bustype)
673{
674 char *ret = calloc(1, 1);
675 /*
676 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
677 * will cease to exist and should be eliminated here as well.
678 */
679 if (bustype == BUS_NONSPI) {
680 ret = strcat_realloc(ret, "Non-SPI, ");
681 } else {
682 if (bustype & BUS_PARALLEL)
683 ret = strcat_realloc(ret, "Parallel, ");
684 if (bustype & BUS_LPC)
685 ret = strcat_realloc(ret, "LPC, ");
686 if (bustype & BUS_FWH)
687 ret = strcat_realloc(ret, "FWH, ");
688 if (bustype & BUS_SPI)
689 ret = strcat_realloc(ret, "SPI, ");
690 if (bustype & BUS_PROG)
691 ret = strcat_realloc(ret, "Programmer-specific, ");
692 if (bustype == BUS_NONE)
693 ret = strcat_realloc(ret, "None, ");
694 }
695 /* Kill last comma. */
696 ret[strlen(ret) - 2] = '\0';
697 ret = realloc(ret, strlen(ret) + 1);
698 return ret;
699}
700
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000701int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000702{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000703 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000704 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000705 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000706
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000707 for (chip = flashchips + startchip; chip && chip->name; chip++) {
708 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000709 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000710 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000711 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000712 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300713 /* Only probe for SPI25 chips by default. */
714 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
715 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000716 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
717 if (!chip->probe && !force) {
718 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000719 continue;
720 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000721
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000722 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200723 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000724 if (!flash->chip) {
725 msg_gerr("Out of memory!\n");
726 exit(1);
727 }
Angel Pons7e134562021-06-07 13:29:13 +0200728 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000729 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000730
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000731 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +0100732 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000733
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000734 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
735 * is only called with force=1 after normal probing failed.
736 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000737 if (force)
738 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000739
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000740 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000741 goto notfound;
742
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000743 /* If this is the first chip found, accept it.
744 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000745 * a non-generic match. SFDP and CFI are generic matches.
746 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000747 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000748 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000749 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000750 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000751 msg_cinfo("===\n"
752 "SFDP has autodetected a flash chip which is "
753 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000754 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000755 msg_cinfo("The standard operations read and "
756 "verify should work, but to support "
757 "erase, write and all other "
758 "possible features");
759 else
760 msg_cinfo("All standard operations (read, "
761 "verify, erase and write) should "
762 "work, but to support all possible "
763 "features");
764
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000765 msg_cinfo(" we need to add them manually.\n"
766 "You can help us by mailing us the output of the following command to "
Nico Huberac90af62022-12-18 00:22:47 +0000767 "flashrom-stable@flashrom.org:\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000768 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
769 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000770 "===\n");
771 }
772
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000773 /* First flash chip detected on this bus. */
774 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000775 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000776 /* Not the first flash chip detected on this bus, but not a generic match either. */
777 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
778 break;
779 /* 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 +0000780notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000781 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000782 free(flash->chip);
783 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000784 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000785
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000786 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000787 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000788
Nico Huber5bd990c2019-06-16 19:46:46 +0200789 /* Fill default layout covering the whole chip. */
Nico Huber671c0f02019-06-16 20:17:19 +0200790 if (flashrom_layout_new(&flash->default_layout) ||
Nico Huber5bd990c2019-06-16 19:46:46 +0200791 flashrom_layout_add_region(flash->default_layout,
Nico Huber92e0b622019-06-15 15:55:11 +0200792 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huber5bd990c2019-06-16 19:46:46 +0200793 flashrom_layout_include_region(flash->default_layout, "complete flash"))
794 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000795
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000796 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000797 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
798 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000799 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000800#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200801 if (programmer->map_flash_region == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000802 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
803 PRIxPTR_WIDTH, flash->physical_memory);
804 else
805#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200806 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000807
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000808 /* Flash registers may more likely not be mapped if the chip was forced.
809 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000810 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000811 if (flash->chip->printlock)
812 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000813
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000814 /* Get out of the way for later runs. */
815 unmap_flash(flash);
816
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000817 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000818 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000819}
820
Stefan Tauner96658be2014-05-26 22:05:31 +0000821/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000822static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000823{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000824 int i, j, k;
825 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530826 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000827
828 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
829 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000830 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530831 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000832
833 for (i = 0; i < NUM_ERASEREGIONS; i++) {
834 /* Blocks with zero size are bugs in flashchips.c. */
835 if (eraser.eraseblocks[i].count &&
836 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000837 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
838 "Please report a bug at flashrom-stable@flashrom.org\n",
839 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000840 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000841 }
842 /* Blocks with zero count are bugs in flashchips.c. */
843 if (!eraser.eraseblocks[i].count &&
844 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000845 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
846 "Please report a bug at flashrom-stable@flashrom.org\n",
847 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000848 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000849 }
850 done += eraser.eraseblocks[i].count *
851 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530852 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000853 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000854 /* Empty eraseblock definition with erase function. */
855 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000856 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000857 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000858 if (!done)
859 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000860 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000861 msg_gerr("ERROR: Flash chip %s erase function %i "
862 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000863 " expected 0x%06x bytes.\n"
864 "Please report a bug at flashrom-stable@flashrom.org\n",
865 chip->name, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000866 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000867 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000868 if (!eraser.block_erase)
869 continue;
870 /* Check if there are identical erase functions for different
871 * layouts. That would imply "magic" erase functions. The
872 * easiest way to check this is with function pointers.
873 */
Uwe Hermann43959702010-03-13 17:28:29 +0000874 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000875 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000876 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000877 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
878 "Please report a bug at flashrom-stable@flashrom.org\n",
879 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000880 ret = 1;
881 }
Uwe Hermann43959702010-03-13 17:28:29 +0000882 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530883 if(curr_eraseblock_count > prev_eraseblock_count)
884 {
Nico Huberac90af62022-12-18 00:22:47 +0000885 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
886 "Please report a bug at flashrom-stable@flashrom.org\n",
887 chip->name, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530888 ret = 1;
889 }
890 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000891 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000892 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000893}
894
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000895static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000896{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000897 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000898
899 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
900 if (log)
901 msg_cdbg("not defined. ");
902 return 1;
903 }
904 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
905 if (log)
906 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000907 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000908 return 1;
909 }
910 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
911 if (log)
912 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000913 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000914 return 1;
915 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000916 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000917 return 0;
918}
919
Nico Huber7af0e792016-04-29 16:40:15 +0200920/**
921 * @brief Reads the included layout regions into a buffer.
922 *
923 * If there is no layout set in the given flash context, the whole chip will
924 * be read.
925 *
926 * @param flashctx Flash context to be used.
927 * @param buffer Buffer of full chip size to read into.
928 * @return 0 on success,
929 * 1 if any read fails.
930 */
931static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
932{
933 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200934 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200935
Nico Huber5ca55232019-06-15 22:29:08 +0200936 while ((entry = layout_next_included(layout, entry))) {
937 const chipoff_t region_start = entry->start;
938 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200939
940 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
941 return 1;
942 }
943 return 0;
944}
945
946typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
947/**
948 * @private
949 *
950 * For read-erase-write, `curcontents` and `newcontents` shall point
951 * to buffers of the chip's size. Both are supposed to be prefilled
952 * with at least the included layout regions of the current flash
953 * contents (`curcontents`) and the data to be written to the flash
954 * (`newcontents`).
955 *
956 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
957 *
958 * The `chipoff_t` values are used internally by `walk_by_layout()`.
959 */
960struct walk_info {
961 uint8_t *curcontents;
962 const uint8_t *newcontents;
963 chipoff_t region_start;
964 chipoff_t region_end;
965 chipoff_t erase_start;
966 chipoff_t erase_end;
967};
968/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
969typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
970
971static int walk_eraseblocks(struct flashctx *const flashctx,
972 struct walk_info *const info,
973 const size_t erasefunction, const per_blockfn_t per_blockfn)
974{
975 int ret;
976 size_t i, j;
977 bool first = true;
978 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
979
980 info->erase_start = 0;
981 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
982 /* count==0 for all automatically initialized array
983 members so the loop below won't be executed for them. */
984 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
985 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
986
987 /* Skip any eraseblock that is completely outside the current region. */
988 if (info->erase_end < info->region_start)
989 continue;
990 if (info->region_end < info->erase_start)
991 break;
992
993 /* Print this for every block except the first one. */
994 if (first)
995 first = false;
996 else
997 msg_cdbg(", ");
998 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
999
1000 ret = per_blockfn(flashctx, info, eraser->block_erase);
1001 if (ret)
1002 return ret;
1003 }
1004 if (info->region_end < info->erase_start)
1005 break;
1006 }
1007 msg_cdbg("\n");
1008 return 0;
1009}
1010
1011static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1012 const per_blockfn_t per_blockfn)
1013{
1014 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001015 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001016
1017 all_skipped = true;
1018 msg_cinfo("Erasing and writing flash chip... ");
1019
Nico Huber5ca55232019-06-15 22:29:08 +02001020 while ((entry = layout_next_included(layout, entry))) {
1021 info->region_start = entry->start;
1022 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001023
1024 size_t j;
1025 int error = 1; /* retry as long as it's 1 */
1026 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1027 if (j != 0)
1028 msg_cinfo("Looking for another erase function.\n");
1029 msg_cdbg("Trying erase function %zi... ", j);
1030 if (check_block_eraser(flashctx, j, 1))
1031 continue;
1032
1033 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1034 if (error != 1)
1035 break;
1036
1037 if (info->curcontents) {
1038 msg_cinfo("Reading current flash chip contents... ");
1039 if (read_by_layout(flashctx, info->curcontents)) {
1040 /* Now we are truly screwed. Read failed as well. */
1041 msg_cerr("Can't read anymore! Aborting.\n");
1042 /* We have no idea about the flash chip contents, so
1043 retrying with another erase function is pointless. */
1044 error = 2;
1045 break;
1046 }
1047 msg_cinfo("done. ");
1048 }
1049 }
1050 if (error == 1)
1051 msg_cinfo("No usable erase functions left.\n");
1052 if (error) {
1053 msg_cerr("FAILED!\n");
1054 return 1;
1055 }
1056 }
1057 if (all_skipped)
1058 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1059 msg_cinfo("Erase/write done.\n");
1060 return 0;
1061}
1062
1063static int erase_block(struct flashctx *const flashctx,
1064 const struct walk_info *const info, const erasefn_t erasefn)
1065{
1066 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001067 const bool region_unaligned = info->region_start > info->erase_start ||
1068 info->erase_end > info->region_end;
1069 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1070 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001071
Nico Huber6e61e0c2019-01-23 17:07:49 +01001072 /*
1073 * If the region is not erase-block aligned, merge current flash con-
1074 * tents into a new buffer `backup_contents`.
1075 */
1076 if (region_unaligned) {
1077 backup_contents = malloc(erase_len);
1078 erased_contents = malloc(erase_len);
1079 if (!backup_contents || !erased_contents) {
1080 msg_cerr("Out of memory!\n");
1081 ret = 1;
1082 goto _free_ret;
1083 }
1084 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1085 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1086
1087 msg_cdbg("R");
1088 /* Merge data preceding the current region. */
1089 if (info->region_start > info->erase_start) {
1090 const chipoff_t start = info->erase_start;
1091 const chipsize_t len = info->region_start - info->erase_start;
1092 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1093 msg_cerr("Can't read! Aborting.\n");
1094 goto _free_ret;
1095 }
1096 }
1097 /* Merge data following the current region. */
1098 if (info->erase_end > info->region_end) {
1099 const chipoff_t start = info->region_end + 1;
1100 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1101 const chipsize_t len = info->erase_end - info->region_end;
1102 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1103 msg_cerr("Can't read! Aborting.\n");
1104 goto _free_ret;
1105 }
1106 }
1107 }
1108
1109 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001110 all_skipped = false;
1111
1112 msg_cdbg("E");
1113 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001114 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001115 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1116 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001117 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001118 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001119
1120 if (region_unaligned) {
1121 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1122 /* get_next_write() sets starthere to a new value after the call. */
1123 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1124 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1125 if (!writecount++)
1126 msg_cdbg("W");
1127 /* Needs the partial write function signature. */
1128 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1129 info->erase_start + starthere, lenhere))
1130 goto _free_ret;
1131 starthere += lenhere;
1132 }
1133 }
1134
1135 ret = 0;
1136
1137_free_ret:
1138 free(erased_contents);
1139 free(backup_contents);
1140 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001141}
1142
1143/**
1144 * @brief Erases the included layout regions.
1145 *
1146 * If there is no layout set in the given flash context, the whole chip will
1147 * be erased.
1148 *
1149 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001150 * @return 0 on success,
1151 * 1 if all available erase functions failed.
1152 */
Nico Huber454f6132012-12-10 13:34:10 +00001153static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001154{
1155 struct walk_info info = { 0 };
1156 return walk_by_layout(flashctx, &info, &erase_block);
1157}
1158
1159static int read_erase_write_block(struct flashctx *const flashctx,
1160 const struct walk_info *const info, const erasefn_t erasefn)
1161{
1162 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1163 const bool region_unaligned = info->region_start > info->erase_start ||
1164 info->erase_end > info->region_end;
1165 const uint8_t *newcontents = NULL;
1166 int ret = 2;
1167
1168 /*
1169 * If the region is not erase-block aligned, merge current flash con-
1170 * tents into `info->curcontents` and a new buffer `newc`. The former
1171 * is necessary since we have no guarantee that the full erase block
1172 * was already read into `info->curcontents`. For the latter a new
1173 * buffer is used since `info->newcontents` might contain data for
1174 * other unaligned regions that touch this erase block too.
1175 */
1176 if (region_unaligned) {
1177 msg_cdbg("R");
1178 uint8_t *const newc = malloc(erase_len);
1179 if (!newc) {
1180 msg_cerr("Out of memory!\n");
1181 return 1;
1182 }
1183 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1184
1185 /* Merge data preceding the current region. */
1186 if (info->region_start > info->erase_start) {
1187 const chipoff_t start = info->erase_start;
1188 const chipsize_t len = info->region_start - info->erase_start;
1189 if (flashctx->chip->read(flashctx, newc, start, len)) {
1190 msg_cerr("Can't read! Aborting.\n");
1191 goto _free_ret;
1192 }
1193 memcpy(info->curcontents + start, newc, len);
1194 }
1195 /* Merge data following the current region. */
1196 if (info->erase_end > info->region_end) {
1197 const chipoff_t start = info->region_end + 1;
1198 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1199 const chipsize_t len = info->erase_end - info->region_end;
1200 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1201 msg_cerr("Can't read! Aborting.\n");
1202 goto _free_ret;
1203 }
1204 memcpy(info->curcontents + start, newc + rel_start, len);
1205 }
1206
1207 newcontents = newc;
1208 } else {
1209 newcontents = info->newcontents + info->erase_start;
1210 }
1211
1212 ret = 1;
1213 bool skipped = true;
1214 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001215 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001216 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1217 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001218 if (erase_block(flashctx, info, erasefn))
1219 goto _free_ret;
1220 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001221 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001222 skipped = false;
1223 }
1224
1225 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1226 /* get_next_write() sets starthere to a new value after the call. */
1227 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1228 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1229 if (!writecount++)
1230 msg_cdbg("W");
1231 /* Needs the partial write function signature. */
1232 if (flashctx->chip->write(flashctx, newcontents + starthere,
1233 info->erase_start + starthere, lenhere))
1234 goto _free_ret;
1235 starthere += lenhere;
1236 skipped = false;
1237 }
1238 if (skipped)
1239 msg_cdbg("S");
1240 else
1241 all_skipped = false;
1242
1243 /* Update curcontents, other regions with overlapping erase blocks
1244 might rely on this. */
1245 memcpy(curcontents, newcontents, erase_len);
1246 ret = 0;
1247
1248_free_ret:
1249 if (region_unaligned)
1250 free((void *)newcontents);
1251 return ret;
1252}
1253
1254/**
1255 * @brief Writes the included layout regions from a given image.
1256 *
1257 * If there is no layout set in the given flash context, the whole image
1258 * will be written.
1259 *
1260 * @param flashctx Flash context to be used.
1261 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1262 * @param newcontents The new image to be written.
1263 * @return 0 on success,
1264 * 1 if anything has gone wrong.
1265 */
Nico Huber454f6132012-12-10 13:34:10 +00001266static int write_by_layout(struct flashctx *const flashctx,
1267 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001268{
1269 struct walk_info info;
1270 info.curcontents = curcontents;
1271 info.newcontents = newcontents;
1272 return walk_by_layout(flashctx, &info, read_erase_write_block);
1273}
1274
1275/**
1276 * @brief Compares the included layout regions with content from a buffer.
1277 *
1278 * If there is no layout set in the given flash context, the whole chip's
1279 * contents will be compared.
1280 *
1281 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001282 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001283 * @param curcontents A buffer of full chip size to read current chip contents into.
1284 * @param newcontents The new image to compare to.
1285 * @return 0 on success,
1286 * 1 if reading failed,
1287 * 3 if the contents don't match.
1288 */
Nico Huber74d09d42019-06-16 03:27:26 +02001289static int verify_by_layout(
1290 struct flashctx *const flashctx,
1291 const struct flashrom_layout *const layout,
1292 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001293{
Nico Huber5ca55232019-06-15 22:29:08 +02001294 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001295
Nico Huber5ca55232019-06-15 22:29:08 +02001296 while ((entry = layout_next_included(layout, entry))) {
1297 const chipoff_t region_start = entry->start;
1298 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001299
1300 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1301 return 1;
1302 if (compare_range(newcontents + region_start, curcontents + region_start,
1303 region_start, region_len))
1304 return 3;
1305 }
1306 return 0;
1307}
1308
Stefan Tauner136388f2013-07-15 10:47:53 +00001309static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001310{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001311 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001312#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001313 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001314 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberac90af62022-12-18 00:22:47 +00001315 "chip. Please report this to the mailing list at flashrom-stable@flashrom.org or\n"
1316 "on IRC (see https://www.flashrom.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001317 "-------------------------------------------------------------------------------\n"
1318 "You may now reboot or simply leave the machine running.\n");
1319 else
1320#endif
1321 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1322 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
Nico Huberac90af62022-12-18 00:22:47 +00001323 "please report this to the mailing list at flashrom-stable@flashrom.org or on IRC\n"
1324 "(see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001325}
1326
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001327void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001328{
Stefan Tauner136388f2013-07-15 10:47:53 +00001329 msg_gerr("Your flash chip is in an unknown state.\n");
1330#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001331 if (programmer == &programmer_internal)
Angel Pons1900e1d2021-07-02 12:42:23 +02001332 msg_gerr("Get help on IRC (see https://www.flashrom.org/Contact) or mail\n"
Nico Huberac90af62022-12-18 00:22:47 +00001333 "flashrom-stable@flashrom.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001334 "-------------------------------------------------------------------------------\n"
1335 "DO NOT REBOOT OR POWEROFF!\n");
1336 else
1337#endif
Nico Huberac90af62022-12-18 00:22:47 +00001338 msg_gerr("Please report this to the mailing list at flashrom-stable@flashrom.org\n"
1339 "or on IRC (see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001340}
1341
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001342void list_programmers_linebreak(int startcol, int cols, int paren)
1343{
1344 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001345 int pnamelen;
1346 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001347 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001348 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001349
Thomas Heijligend45cb592021-05-19 14:12:18 +02001350 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001351 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001352 pnamelen = strlen(pname);
1353 if (remaining - pnamelen - 2 < 0) {
1354 if (firstline)
1355 firstline = 0;
1356 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001357 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001358 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001359 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001360 remaining = cols - startcol;
1361 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001362 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001363 remaining--;
1364 }
1365 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001366 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001367 remaining--;
1368 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001369 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001370 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001371 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001372 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001373 remaining--;
1374 } else {
1375 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001376 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001377 }
1378 }
1379}
1380
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001381static void print_sysinfo(void)
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001382{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001383#if IS_WINDOWS
Angel Pons7e134562021-06-07 13:29:13 +02001384 SYSTEM_INFO si = { 0 };
1385 OSVERSIONINFOEX osvi = { 0 };
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001386
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001387 msg_ginfo(" on Windows");
1388 /* Tell Windows which version of the structure we want. */
1389 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1390 if (GetVersionEx((OSVERSIONINFO*) &osvi))
1391 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
1392 else
1393 msg_ginfo(" unknown version");
1394 GetSystemInfo(&si);
1395 switch (si.wProcessorArchitecture) {
1396 case PROCESSOR_ARCHITECTURE_AMD64:
1397 msg_ginfo(" (x86_64)");
1398 break;
1399 case PROCESSOR_ARCHITECTURE_INTEL:
1400 msg_ginfo(" (x86)");
1401 break;
1402 default:
1403 msg_ginfo(" (unknown arch)");
1404 break;
1405 }
1406#elif HAVE_UTSNAME == 1
1407 struct utsname osinfo;
1408
1409 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001410 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1411 osinfo.machine);
1412#else
1413 msg_ginfo(" on unknown machine");
1414#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001415}
1416
1417void print_buildinfo(void)
1418{
1419 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001420#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001421 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001422#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001423 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001424#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001425 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001426#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001427#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001428 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001429#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001430 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001431#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001432 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001433#endif
1434#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001435 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001436#endif
1437#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001438 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001439#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001440 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001441#else
1442#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001443#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001444 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001445}
1446
Bernhard Walle201bde32008-01-21 15:24:22 +00001447void print_version(void)
1448{
Nico Huberac90af62022-12-18 00:22:47 +00001449 msg_ginfo("flashrom-stable %s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001450 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001451 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00001452}
1453
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001454void print_banner(void)
1455{
1456 msg_ginfo("flashrom is free software, get the source code at "
Stefan Tauner4c723152016-01-14 22:47:55 +00001457 "https://flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001458 msg_ginfo("\n");
1459}
1460
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001461int selfcheck(void)
1462{
Stefan Tauner96658be2014-05-26 22:05:31 +00001463 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001464 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001465
Thomas Heijligend45cb592021-05-19 14:12:18 +02001466 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001467 const struct programmer_entry *const p = programmer_table[i];
1468 if (p == NULL) {
1469 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1470 ret = 1;
1471 continue;
1472 }
1473 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001474 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1475 ret = 1;
1476 /* This might hide other problems with this programmer, but allows for better error
1477 * messages below without jumping through hoops. */
1478 continue;
1479 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001480 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001481 case USB:
1482 case PCI:
1483 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001484 if (p->devs.note == NULL) {
1485 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001486 break; /* This one has its device list stored separately. */
1487 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001488 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001489 ret = 1;
1490 }
1491 break;
1492 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001493 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001494 ret = 1;
1495 break;
1496 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001497 if (p->init == NULL) {
1498 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001499 ret = 1;
1500 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001501 if (p->delay == NULL) {
1502 msg_gerr("Programmer %s does not have a valid delay function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001503 ret = 1;
1504 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001505 if (p->map_flash_region == NULL) {
1506 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001507 ret = 1;
1508 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001509 if (p->unmap_flash_region == NULL) {
1510 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001511 ret = 1;
1512 }
1513 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001514
1515 /* It would be favorable if we could check for the correct layout (especially termination) of various
1516 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1517 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1518 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1519 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1520 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001521 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001522 msg_gerr("Flashchips table miscompilation!\n");
1523 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001524 } else {
1525 for (i = 0; i < flashchips_size - 1; i++) {
1526 const struct flashchip *chip = &flashchips[i];
1527 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1528 ret = 1;
1529 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
Nico Huberac90af62022-12-18 00:22:47 +00001530 "Please report a bug at flashrom-stable@flashrom.org\n", i,
Stefan Tauner96658be2014-05-26 22:05:31 +00001531 chip->name == NULL ? "unnamed" : chip->name);
1532 }
1533 if (selfcheck_eraseblocks(chip)) {
1534 ret = 1;
1535 }
1536 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001537 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001538
Stefan Tauner600576b2014-06-12 22:57:36 +00001539#if CONFIG_INTERNAL == 1
1540 ret |= selfcheck_board_enables();
1541#endif
1542
Stefan Tauner96658be2014-05-26 22:05:31 +00001543 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001544 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001545}
1546
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001547/* FIXME: This function signature needs to be improved once prepare_flash_access()
1548 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001549 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001550static int chip_safety_check(const struct flashctx *flash, int force,
1551 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001552{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001553 const struct flashchip *chip = flash->chip;
1554
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001555 if (!programmer_may_write && (write_it || erase_it)) {
1556 msg_perr("Write/erase is not working yet on your programmer in "
1557 "its current configuration.\n");
1558 /* --force is the wrong approach, but it's the best we can do
1559 * until the generic programmer parameter parser is merged.
1560 */
1561 if (!force)
1562 return 1;
1563 msg_cerr("Continuing anyway.\n");
1564 }
1565
1566 if (read_it || erase_it || write_it || verify_it) {
1567 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001568 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001569 msg_cerr("Read is not working on this chip. ");
1570 if (!force)
1571 return 1;
1572 msg_cerr("Continuing anyway.\n");
1573 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001574 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001575 msg_cerr("flashrom has no read function for this "
1576 "flash chip.\n");
1577 return 1;
1578 }
1579 }
1580 if (erase_it || write_it) {
1581 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001582 if (chip->tested.erase == NA) {
1583 msg_cerr("Erase is not possible on this chip.\n");
1584 return 1;
1585 }
1586 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001587 msg_cerr("Erase is not working on this chip. ");
1588 if (!force)
1589 return 1;
1590 msg_cerr("Continuing anyway.\n");
1591 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001592 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001593 msg_cerr("flashrom has no erase function for this "
1594 "flash chip.\n");
1595 return 1;
1596 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001597 }
1598 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001599 if (chip->tested.write == NA) {
1600 msg_cerr("Write is not possible on this chip.\n");
1601 return 1;
1602 }
1603 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001604 msg_cerr("Write is not working on this chip. ");
1605 if (!force)
1606 return 1;
1607 msg_cerr("Continuing anyway.\n");
1608 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001609 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001610 msg_cerr("flashrom has no write function for this "
1611 "flash chip.\n");
1612 return 1;
1613 }
1614 }
1615 return 0;
1616}
1617
Nico Huber305f4172013-06-14 11:55:26 +02001618int prepare_flash_access(struct flashctx *const flash,
1619 const bool read_it, const bool write_it,
1620 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001621{
1622 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1623 msg_cerr("Aborting.\n");
1624 return 1;
1625 }
1626
Nico Hubere0ed4122021-05-14 00:48:28 +02001627 if (layout_sanity_checks(flash)) {
Nico Huber454f6132012-12-10 13:34:10 +00001628 msg_cerr("Requested regions can not be handled. Aborting.\n");
1629 return 1;
1630 }
1631
1632 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
Nico Huberf43c6542017-10-14 17:47:28 +02001643 flash->address_high_byte = -1;
1644 flash->in_4ba_mode = false;
1645
Nico Huberdc5af542018-12-22 16:54:59 +01001646 /* Be careful about 4BA chips and broken masters */
1647 if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
1648 /* If we can't use native instructions, bail out */
1649 if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
1650 || !spi_master_4ba(flash)) {
1651 msg_cerr("Programmer doesn't support this chip. Aborting.\n");
1652 return 1;
1653 }
1654 }
1655
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001656 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01001657 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01001658 int ret;
1659 if (spi_master_4ba(flash))
1660 ret = spi_enter_4ba(flash);
1661 else
1662 ret = spi_exit_4ba(flash);
1663 if (ret) {
1664 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001665 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02001666 }
Boris Baykov99127182016-06-11 18:29:00 +02001667 }
1668
Nico Huber454f6132012-12-10 13:34:10 +00001669 return 0;
1670}
1671
Nico Huber305f4172013-06-14 11:55:26 +02001672void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001673{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001674 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001675 unmap_flash(flash);
1676}
1677
1678/**
1679 * @addtogroup flashrom-flash
1680 * @{
1681 */
1682
1683/**
1684 * @brief Erase the specified ROM chip.
1685 *
1686 * If a layout is set in the given flash context, only included regions
1687 * will be erased.
1688 *
1689 * @param flashctx The context of the flash chip to erase.
1690 * @return 0 on success.
1691 */
1692int flashrom_flash_erase(struct flashctx *const flashctx)
1693{
1694 if (prepare_flash_access(flashctx, false, false, true, false))
1695 return 1;
1696
1697 const int ret = erase_by_layout(flashctx);
1698
1699 finalize_flash_access(flashctx);
1700
1701 return ret;
1702}
1703
1704/** @} */ /* end flashrom-flash */
1705
1706/**
1707 * @defgroup flashrom-ops Operations
1708 * @{
1709 */
1710
1711/**
1712 * @brief Read the current image from the specified ROM chip.
1713 *
1714 * If a layout is set in the specified flash context, only included regions
1715 * will be read.
1716 *
1717 * @param flashctx The context of the flash chip.
1718 * @param buffer Target buffer to write image to.
1719 * @param buffer_len Size of target buffer in bytes.
1720 * @return 0 on success,
1721 * 2 if buffer_len is too short for the flash chip's contents,
1722 * or 1 on any other failure.
1723 */
1724int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
1725{
1726 const size_t flash_size = flashctx->chip->total_size * 1024;
1727
1728 if (flash_size > buffer_len)
1729 return 2;
1730
1731 if (prepare_flash_access(flashctx, true, false, false, false))
1732 return 1;
1733
1734 msg_cinfo("Reading flash... ");
1735
1736 int ret = 1;
1737 if (read_by_layout(flashctx, buffer)) {
1738 msg_cerr("Read operation failed!\n");
1739 msg_cinfo("FAILED.\n");
1740 goto _finalize_ret;
1741 }
1742 msg_cinfo("done.\n");
1743 ret = 0;
1744
1745_finalize_ret:
1746 finalize_flash_access(flashctx);
1747 return ret;
1748}
1749
1750static void combine_image_by_layout(const struct flashctx *const flashctx,
1751 uint8_t *const newcontents, const uint8_t *const oldcontents)
1752{
1753 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001754 const struct romentry *included;
1755 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001756
Nico Huber3d7b1e32018-12-22 00:53:14 +01001757 while ((included = layout_next_included_region(layout, start))) {
1758 if (included->start > start) {
1759 /* copy everything up to the start of this included region */
1760 memcpy(newcontents + start, oldcontents + start, included->start - start);
1761 }
1762 /* skip this included region */
1763 start = included->end + 1;
1764 if (start == 0)
1765 return;
Nico Huber454f6132012-12-10 13:34:10 +00001766 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001767
1768 /* copy the rest of the chip */
1769 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1770 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001771}
1772
1773/**
1774 * @brief Write the specified image to the ROM chip.
1775 *
1776 * If a layout is set in the specified flash context, only erase blocks
1777 * containing included regions will be touched.
1778 *
1779 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001780 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001781 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001782 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001783 * @return 0 on success,
1784 * 4 if buffer_len doesn't match the size of the flash chip,
1785 * 3 if write was tried but nothing has changed,
1786 * 2 if write failed and flash contents changed,
1787 * or 1 on any other failure.
1788 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001789int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
1790 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001791{
1792 const size_t flash_size = flashctx->chip->total_size * 1024;
1793 const bool verify_all = flashctx->flags.verify_whole_chip;
1794 const bool verify = flashctx->flags.verify_after_write;
Nico Huber74d09d42019-06-16 03:27:26 +02001795 const struct flashrom_layout *const verify_layout =
1796 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001797
1798 if (buffer_len != flash_size)
1799 return 4;
1800
1801 int ret = 1;
1802
1803 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001804 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001805 uint8_t *const curcontents = malloc(flash_size);
1806 uint8_t *oldcontents = NULL;
1807 if (verify_all)
1808 oldcontents = malloc(flash_size);
1809 if (!curcontents || (verify_all && !oldcontents)) {
1810 msg_gerr("Out of memory!\n");
1811 goto _free_ret;
1812 }
1813
1814#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001815 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001816 if (flashctx->flags.force_boardmismatch) {
1817 msg_pinfo("Proceeding anyway because user forced us to.\n");
1818 } else {
1819 msg_perr("Aborting. You can override this with "
1820 "-p internal:boardmismatch=force.\n");
1821 goto _free_ret;
1822 }
1823 }
1824#endif
1825
1826 if (prepare_flash_access(flashctx, false, true, false, verify))
1827 goto _free_ret;
1828
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001829 /* If given, assume flash chip contains same data as `refcontents`. */
1830 if (refcontents) {
1831 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1832 memcpy(curcontents, refcontents, flash_size);
1833 if (oldcontents)
1834 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001835 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001836 /*
1837 * Read the whole chip to be able to check whether regions need to be
1838 * erased and to give better diagnostics in case write fails.
1839 * The alternative is to read only the regions which are to be
1840 * preserved, but in that case we might perform unneeded erase which
1841 * takes time as well.
1842 */
1843 msg_cinfo("Reading old flash chip contents... ");
1844 if (verify_all) {
1845 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
1846 msg_cinfo("FAILED.\n");
1847 goto _finalize_ret;
1848 }
1849 memcpy(curcontents, oldcontents, flash_size);
1850 } else {
1851 if (read_by_layout(flashctx, curcontents)) {
1852 msg_cinfo("FAILED.\n");
1853 goto _finalize_ret;
1854 }
Nico Huber454f6132012-12-10 13:34:10 +00001855 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001856 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001857 }
Nico Huber454f6132012-12-10 13:34:10 +00001858
1859 if (write_by_layout(flashctx, curcontents, newcontents)) {
1860 msg_cerr("Uh oh. Erase/write failed. ");
1861 ret = 2;
1862 if (verify_all) {
1863 msg_cerr("Checking if anything has changed.\n");
1864 msg_cinfo("Reading current flash chip contents... ");
1865 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
1866 msg_cinfo("done.\n");
1867 if (!memcmp(oldcontents, curcontents, flash_size)) {
1868 nonfatal_help_message();
1869 goto _finalize_ret;
1870 }
1871 msg_cerr("Apparently at least some data has changed.\n");
1872 } else
1873 msg_cerr("Can't even read anymore!\n");
1874 emergency_help_message();
1875 goto _finalize_ret;
1876 } else {
1877 msg_cerr("\n");
1878 }
1879 emergency_help_message();
1880 goto _finalize_ret;
1881 }
1882
1883 /* Verify only if we actually changed something. */
1884 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001885 msg_cinfo("Verifying flash... ");
1886
1887 /* Work around chips which need some time to calm down. */
1888 programmer_delay(1000*1000);
1889
Nico Huber74d09d42019-06-16 03:27:26 +02001890 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001891 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001892 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001893 /* If we tried to write, and verification now fails, we
1894 might have an emergency situation. */
1895 if (ret)
1896 emergency_help_message();
1897 else
1898 msg_cinfo("VERIFIED.\n");
1899 } else {
1900 /* We didn't change anything. */
1901 ret = 0;
1902 }
1903
1904_finalize_ret:
1905 finalize_flash_access(flashctx);
1906_free_ret:
1907 free(oldcontents);
1908 free(curcontents);
1909 return ret;
1910}
1911
1912/**
1913 * @brief Verify the ROM chip's contents with the specified image.
1914 *
1915 * If a layout is set in the specified flash context, only included regions
1916 * will be verified.
1917 *
1918 * @param flashctx The context of the flash chip.
1919 * @param buffer Source buffer to verify with.
1920 * @param buffer_len Size of source buffer in bytes.
1921 * @return 0 on success,
1922 * 3 if the chip's contents don't match,
1923 * 2 if buffer_len doesn't match the size of the flash chip,
1924 * or 1 on any other failure.
1925 */
1926int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
1927{
Nico Huber74d09d42019-06-16 03:27:26 +02001928 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001929 const size_t flash_size = flashctx->chip->total_size * 1024;
1930
1931 if (buffer_len != flash_size)
1932 return 2;
1933
1934 const uint8_t *const newcontents = buffer;
1935 uint8_t *const curcontents = malloc(flash_size);
1936 if (!curcontents) {
1937 msg_gerr("Out of memory!\n");
1938 return 1;
1939 }
1940
1941 int ret = 1;
1942
1943 if (prepare_flash_access(flashctx, false, false, false, true))
1944 goto _free_ret;
1945
1946 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001947 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001948 if (!ret)
1949 msg_cinfo("VERIFIED.\n");
1950
1951 finalize_flash_access(flashctx);
1952_free_ret:
1953 free(curcontents);
1954 return ret;
1955}
1956
1957/** @} */ /* end flashrom-ops */