blob: 40472ec45f0d2aad3d2f4716a51243e389d59cac [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Nico Huber7af0e792016-04-29 16:40:15 +02008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000010 *
Uwe Hermannd1107642007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000015 *
Uwe Hermannd1107642007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000020 */
21
Felix Singerf25447e2022-08-19 02:44:28 +020022#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000023#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000024#include <sys/types.h>
Ronald G. Minnichceec4202003-07-25 04:37:41 +000025#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000026#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000027#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000028#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000029#include <ctype.h>
Edward O'Callaghan3b64d812022-08-12 13:07:51 +100030
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000031#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000032#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000033#include "programmer.h"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010034#include "hwaccess_physmap.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010035#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000036
Mathias Krausea60faab2011-01-17 07:50:42 +000037const char flashrom_version[] = FLASHROM_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000038const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000039
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020040static const struct programmer_entry *programmer = NULL;
Nico Huber6a2ebeb2022-08-26 11:36:48 +020041static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000042
Uwe Hermann48ec1b12010-08-08 17:01:18 +000043/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000044 * Programmers supporting multiple buses can have differing size limits on
45 * each bus. Store the limits for each bus in a common struct.
46 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000047struct decode_sizes max_rom_decode;
48
49/* If nonzero, used as the start address of bottom-aligned flash. */
50unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000051
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000052/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020053bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000054
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000055#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000056static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000057/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000058static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000059 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000060 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000061} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000062/* Initialize to 0 to make sure nobody registers a shutdown function before
63 * programmer init.
64 */
Felix Singerf25447e2022-08-19 02:44:28 +020065static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000066
Stefan Taunerc4f44df2013-08-12 22:58:43 +000067/* Did we change something or was every erase/write skipped (if any)? */
68static bool all_skipped = true;
69
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000070static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000071
Stefan Tauner2a1ed772014-08-31 00:09:21 +000072int shutdown_free(void *data)
73{
74 free(data);
75 return 0;
76}
77
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000078/* Register a function to be executed on programmer shutdown.
79 * The advantage over atexit() is that you can supply a void pointer which will
80 * be used as parameter to the registered function upon programmer shutdown.
81 * This pointer can point to arbitrary data used by said function, e.g. undo
82 * information for GPIO settings etc. If unneeded, set data=NULL.
83 * Please note that the first (void *data) belongs to the function signature of
84 * the function passed as first parameter.
85 */
David Hendricks8bb20212011-06-14 01:35:36 +000086int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000087{
88 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000089 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000090 SHUTDOWN_MAXFN);
91 return 1;
92 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000093 if (!may_register_shutdown) {
94 msg_perr("Tried to register a shutdown function before "
95 "programmer init.\n");
96 return 1;
97 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000098 shutdown_fn[shutdown_fn_count].func = function;
99 shutdown_fn[shutdown_fn_count].data = data;
100 shutdown_fn_count++;
101
102 return 0;
103}
104
Nikolai Artemiev4ad48642020-11-05 13:54:27 +1100105int register_chip_restore(chip_restore_fn_cb_t func,
106 struct flashctx *flash, uint8_t status)
107{
108 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
109 msg_perr("Tried to register more than %i chip restore"
110 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
111 return 1;
112 }
113 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
114 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
115 flash->chip_restore_fn_count++;
116
117 return 0;
118}
119
120static int deregister_chip_restore(struct flashctx *flash)
121{
122 int rc = 0;
123
124 while (flash->chip_restore_fn_count > 0) {
125 int i = --flash->chip_restore_fn_count;
126 rc |= flash->chip_restore_fn[i].func(
127 flash, flash->chip_restore_fn[i].status);
128 }
129
130 return rc;
131}
132
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200133int programmer_init(const struct programmer_entry *prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000134{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000135 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000136
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200137 if (prog == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000138 msg_perr("Invalid programmer specified!\n");
139 return -1;
140 }
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200141 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000142 /* Initialize all programmer specific data. */
143 /* Default to unlimited decode sizes. */
144 max_rom_decode = (const struct decode_sizes) {
145 .parallel = 0xffffffff,
146 .lpc = 0xffffffff,
147 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000148 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000149 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000150 /* Default to top aligned flash at 4 GB. */
151 flashbase = 0;
152 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200153 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000154 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200155 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000156
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200157 if (param) {
158 programmer_param = strdup(param);
159 if (!programmer_param) {
160 msg_perr("Out of memory!\n");
161 return ERROR_FATAL;
162 }
163 } else {
164 programmer_param = NULL;
165 }
166
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200167 msg_pdbg("Initializing %s programmer\n", programmer->name);
168 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000169 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000170 if (ret != 0) {
171 /* It is quite possible that any unhandled programmer parameter would have been valid,
172 * but an error in actual programmer init happened before the parameter was evaluated.
173 */
174 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
175 programmer_param);
176 } else {
177 /* Actual programmer init was successful, but the user specified an invalid or unusable
178 * (for the current programmer configuration) parameter.
179 */
180 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
181 msg_perr("Aborting.\n");
182 ret = ERROR_FATAL;
183 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000184 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200185 free(programmer_param);
186 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000187 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000188}
189
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000190/** Calls registered shutdown functions and resets internal programmer-related variables.
191 * Calling it is safe even without previous initialization, but further interactions with programmer support
192 * require a call to programmer_init() (afterwards).
193 *
194 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000195int programmer_shutdown(void)
196{
David Hendricks8bb20212011-06-14 01:35:36 +0000197 int ret = 0;
198
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000199 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200200 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000201 while (shutdown_fn_count > 0) {
202 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000203 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000204 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000205 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000206
David Hendricks8bb20212011-06-14 01:35:36 +0000207 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000208}
209
Stefan Tauner305e0b92013-07-17 23:46:44 +0000210void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000211{
Edward O'Callaghan621208c2022-09-07 22:21:39 +1000212 void *ret;
213 if (programmer->map_flash_region)
214 ret = programmer->map_flash_region(descr, phys_addr, len);
215 else
216 ret = fallback_map(descr, phys_addr, len);
Stefan Tauner26e7a152013-09-13 17:21:05 +0000217 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
218 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
219 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000220}
221
222void programmer_unmap_flash_region(void *virt_addr, size_t len)
223{
Edward O'Callaghan621208c2022-09-07 22:21:39 +1000224 if (programmer->unmap_flash_region)
225 programmer->unmap_flash_region(virt_addr, len);
226 else
227 fallback_unmap(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000228 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000229}
230
Stefan Taunerf80419c2014-05-02 15:41:42 +0000231void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000232{
Edward O'Callaghan56684d92022-09-07 10:47:45 +1000233 if (usecs > 0) {
234 if (programmer->delay)
235 programmer->delay(usecs);
236 else
237 internal_delay(usecs);
238 }
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000239}
240
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000241int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
242 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000243{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000244 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000245
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000246 return 0;
247}
248
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000249/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000250 * It will look for needle with a subsequent '=' in haystack, return a copy of
251 * needle and remove everything from the first occurrence of needle to the next
252 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000253 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200254static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000255{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000256 char *param_pos, *opt_pos, *rest;
257 char *opt = NULL;
258 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000259 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000260
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000261 needlelen = strlen(needle);
262 if (!needlelen) {
263 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000264 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000265 return NULL;
266 }
267 /* No programmer parameters given. */
268 if (*haystack == NULL)
269 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000270 param_pos = strstr(*haystack, needle);
271 do {
272 if (!param_pos)
273 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000274 /* Needle followed by '='? */
275 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000276 /* Beginning of the string? */
277 if (param_pos == *haystack)
278 break;
279 /* After a delimiter? */
280 if (strchr(delim, *(param_pos - 1)))
281 break;
282 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000283 /* Continue searching. */
284 param_pos++;
285 param_pos = strstr(param_pos, needle);
286 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000287
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000288 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000289 /* Get the string after needle and '='. */
290 opt_pos = param_pos + needlelen + 1;
291 optlen = strcspn(opt_pos, delim);
292 /* Return an empty string if the parameter was empty. */
293 opt = malloc(optlen + 1);
294 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000295 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000296 exit(1);
297 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000298 strncpy(opt, opt_pos, optlen);
299 opt[optlen] = '\0';
300 rest = opt_pos + optlen;
301 /* Skip all delimiters after the current parameter. */
302 rest += strspn(rest, delim);
303 memmove(param_pos, rest, strlen(rest) + 1);
304 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000305 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000306
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000307 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000308}
309
Stefan Tauner66652442011-06-26 17:38:17 +0000310char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000311{
312 return extract_param(&programmer_param, param_name, ",");
313}
314
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000315/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000316static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000317{
318 unsigned int usable_erasefunctions = 0;
319 int k;
320 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
321 if (!check_block_eraser(flash, k, 0))
322 usable_erasefunctions++;
323 }
324 return usable_erasefunctions;
325}
326
Mark Marshallf20b7be2014-05-09 21:16:21 +0000327static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000328{
329 int ret = 0, failcount = 0;
330 unsigned int i;
331 for (i = 0; i < len; i++) {
332 if (wantbuf[i] != havebuf[i]) {
333 /* Only print the first failure. */
334 if (!failcount++)
335 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
336 start + i, wantbuf[i], havebuf[i]);
337 }
338 }
339 if (failcount) {
340 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
341 start, start + len - 1, failcount);
342 ret = -1;
343 }
344 return ret;
345}
346
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000347/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600348static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000349{
350 int ret;
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300351 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000352
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100353 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000354 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100355 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000356 exit(1);
357 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300358 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000359 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100360
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000361 free(cmpbuf);
362 return ret;
363}
364
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000365/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000366 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000367 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000368 * @start offset to the base address of the flash chip
369 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000370 * @return 0 for success, -1 for failure
371 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000372int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000373{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000374 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000375 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000376
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100377 if (start + len > flash->chip->total_size * 1024) {
378 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
379 " total_size 0x%x\n", __func__, start, len,
380 flash->chip->total_size * 1024);
381 return -1;
382 }
383
Stefan Taunerdf64a422014-05-27 00:06:14 +0000384 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000385 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100386 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000387 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000388 }
389
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100390 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000391 if (ret) {
392 msg_gerr("Verification impossible because read failed "
393 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000394 ret = -1;
395 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000396 }
397
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000398 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000399out_free:
400 free(readbuf);
401 return ret;
402}
403
Stefan Tauner02437452013-04-01 19:34:53 +0000404/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300405static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
406 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000407{
408 unsigned int i, j, limit;
409 for (j = 0; j < len / gran; j++) {
410 limit = min (gran, len - j * gran);
411 /* Are 'have' and 'want' identical? */
412 if (!memcmp(have + j * gran, want + j * gran, limit))
413 continue;
414 /* have needs to be in erased state. */
415 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300416 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000417 return 1;
418 }
419 return 0;
420}
421
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000422/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000423 * Check if the buffer @have can be programmed to the content of @want without
424 * erasing. This is only possible if all chunks of size @gran are either kept
425 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000426 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000427 * Warning: This function assumes that @have and @want point to naturally
428 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000429 *
430 * @have buffer with current content
431 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000432 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000433 * @gran write granularity (enum, not count)
434 * @return 0 if no erase is needed, 1 otherwise
435 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000436static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
437 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000438{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000439 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000440 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000441
442 switch (gran) {
443 case write_gran_1bit:
444 for (i = 0; i < len; i++)
445 if ((have[i] & want[i]) != want[i]) {
446 result = 1;
447 break;
448 }
449 break;
450 case write_gran_1byte:
451 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300452 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000453 result = 1;
454 break;
455 }
456 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000457 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300458 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000459 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000460 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300461 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000462 break;
463 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300464 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000465 break;
466 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300467 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000468 break;
469 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300470 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000471 break;
472 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300473 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000474 break;
475 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300476 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000477 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000478 case write_gran_1byte_implicit_erase:
479 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
480 result = 0;
481 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000482 default:
483 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000484 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000485 }
486 return result;
487}
488
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000489/**
490 * Check if the buffer @have needs to be programmed to get the content of @want.
491 * If yes, return 1 and fill in first_start with the start address of the
492 * write operation and first_len with the length of the first to-be-written
493 * chunk. If not, return 0 and leave first_start and first_len undefined.
494 *
495 * Warning: This function assumes that @have and @want point to naturally
496 * aligned regions.
497 *
498 * @have buffer with current content
499 * @want buffer with desired content
500 * @len length of the checked area
501 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000502 * @first_start offset of the first byte which needs to be written (passed in
503 * value is increased by the offset of the first needed write
504 * relative to have/want or unchanged if no write is needed)
505 * @return length of the first contiguous area which needs to be written
506 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000507 *
508 * FIXME: This function needs a parameter which tells it about coalescing
509 * in relation to the max write length of the programmer and the max write
510 * length of the chip.
511 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000512static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000513 unsigned int *first_start,
514 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000515{
Felix Singerf25447e2022-08-19 02:44:28 +0200516 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000517 unsigned int rel_start = 0, first_len = 0;
518 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000519
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000520 switch (gran) {
521 case write_gran_1bit:
522 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000523 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000524 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000525 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000526 case write_gran_128bytes:
527 stride = 128;
528 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000529 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000530 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000531 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000532 case write_gran_264bytes:
533 stride = 264;
534 break;
535 case write_gran_512bytes:
536 stride = 512;
537 break;
538 case write_gran_528bytes:
539 stride = 528;
540 break;
541 case write_gran_1024bytes:
542 stride = 1024;
543 break;
544 case write_gran_1056bytes:
545 stride = 1056;
546 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000547 default:
548 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000549 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000550 /* Claim that no write was needed. A write with unknown
551 * granularity is too dangerous to try.
552 */
553 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000554 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000555 for (i = 0; i < len / stride; i++) {
556 limit = min(stride, len - i * stride);
557 /* Are 'have' and 'want' identical? */
558 if (memcmp(have + i * stride, want + i * stride, limit)) {
559 if (!need_write) {
560 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200561 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000562 rel_start = i * stride;
563 }
564 } else {
565 if (need_write) {
566 /* First location where have and want
567 * do not differ anymore.
568 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000569 break;
570 }
571 }
572 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000573 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000574 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000575 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000576 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000577}
578
Martin Rothf6c1cb12022-03-15 10:55:25 -0600579/* Returns the number of buses commonly supported by the current programmer and flash chip where the latter
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000580 * can not be completely accessed due to size/address limits of the programmer. */
581unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000582{
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000583 unsigned int limitexceeded = 0;
584 uint32_t size = flash->chip->total_size * 1024;
585 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000586
587 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000588 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000589 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000590 "size %u kB of chipset/board/programmer "
591 "for %s interface, "
592 "probe/read/erase/write may fail. ", size / 1024,
593 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000594 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000595 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000596 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000597 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000598 "size %u kB of chipset/board/programmer "
599 "for %s interface, "
600 "probe/read/erase/write may fail. ", size / 1024,
601 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000602 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000603 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000604 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000605 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000606 "size %u kB of chipset/board/programmer "
607 "for %s interface, "
608 "probe/read/erase/write may fail. ", size / 1024,
609 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000610 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000611 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000612 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000613 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000614 "size %u kB of chipset/board/programmer "
615 "for %s interface, "
616 "probe/read/erase/write may fail. ", size / 1024,
617 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000618 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000619 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000620}
621
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000622void unmap_flash(struct flashctx *flash)
623{
624 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
625 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
626 flash->physical_registers = 0;
627 flash->virtual_registers = (chipaddr)ERROR_PTR;
628 }
629
630 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
631 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
632 flash->physical_memory = 0;
633 flash->virtual_memory = (chipaddr)ERROR_PTR;
634 }
635}
636
637int map_flash(struct flashctx *flash)
638{
639 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
640 flash->virtual_memory = (chipaddr)ERROR_PTR;
641 flash->virtual_registers = (chipaddr)ERROR_PTR;
642
643 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
644 * These are used for various probing-related hacks that would not map successfully anyway and should be
645 * removed ASAP. */
646 if (flash->chip->total_size == 0)
647 return 0;
648
649 const chipsize_t size = flash->chip->total_size * 1024;
650 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
651 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
652 if (addr == ERROR_PTR) {
653 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
654 flash->chip->name, PRIxPTR_WIDTH, base);
655 return 1;
656 }
657 flash->physical_memory = base;
658 flash->virtual_memory = (chipaddr)addr;
659
660 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
661 * completely different on some chips and programmers, or not mappable at all.
662 * Ignore these problems for now and always report success. */
663 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
664 base = 0xffffffff - size - 0x400000 + 1;
665 addr = programmer_map_flash_region("flash chip registers", base, size);
666 if (addr == ERROR_PTR) {
667 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
668 flash->chip->name, PRIxPTR_WIDTH, base);
669 return 0;
670 }
671 flash->physical_registers = base;
672 flash->virtual_registers = (chipaddr)addr;
673 }
674 return 0;
675}
676
Nico Huber2d625722016-05-03 10:48:02 +0200677/*
678 * Return a string corresponding to the bustype parameter.
679 * Memory is obtained with malloc() and must be freed with free() by the caller.
680 */
681char *flashbuses_to_text(enum chipbustype bustype)
682{
683 char *ret = calloc(1, 1);
684 /*
685 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
686 * will cease to exist and should be eliminated here as well.
687 */
688 if (bustype == BUS_NONSPI) {
689 ret = strcat_realloc(ret, "Non-SPI, ");
690 } else {
691 if (bustype & BUS_PARALLEL)
692 ret = strcat_realloc(ret, "Parallel, ");
693 if (bustype & BUS_LPC)
694 ret = strcat_realloc(ret, "LPC, ");
695 if (bustype & BUS_FWH)
696 ret = strcat_realloc(ret, "FWH, ");
697 if (bustype & BUS_SPI)
698 ret = strcat_realloc(ret, "SPI, ");
699 if (bustype & BUS_PROG)
700 ret = strcat_realloc(ret, "Programmer-specific, ");
701 if (bustype == BUS_NONE)
702 ret = strcat_realloc(ret, "None, ");
703 }
704 /* Kill last comma. */
705 ret[strlen(ret) - 2] = '\0';
706 ret = realloc(ret, strlen(ret) + 1);
707 return ret;
708}
709
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000710int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000711{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000712 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000713 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000714 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000715
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000716 for (chip = flashchips + startchip; chip && chip->name; chip++) {
717 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000718 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000719 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000720 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000721 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300722 /* Only probe for SPI25 chips by default. */
723 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
724 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000725 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
726 if (!chip->probe && !force) {
727 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000728 continue;
729 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000730
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000731 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200732 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000733 if (!flash->chip) {
734 msg_gerr("Out of memory!\n");
735 exit(1);
736 }
Angel Pons7e134562021-06-07 13:29:13 +0200737 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000738 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000739
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000740 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +0100741 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000742
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000743 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
744 * is only called with force=1 after normal probing failed.
745 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000746 if (force)
747 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000748
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000749 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000750 goto notfound;
751
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000752 /* If this is the first chip found, accept it.
753 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000754 * a non-generic match. SFDP and CFI are generic matches.
755 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000756 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000757 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000758 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000759 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000760 msg_cinfo("===\n"
761 "SFDP has autodetected a flash chip which is "
762 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000763 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000764 msg_cinfo("The standard operations read and "
765 "verify should work, but to support "
766 "erase, write and all other "
767 "possible features");
768 else
769 msg_cinfo("All standard operations (read, "
770 "verify, erase and write) should "
771 "work, but to support all possible "
772 "features");
773
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000774 msg_cinfo(" we need to add them manually.\n"
775 "You can help us by mailing us the output of the following command to "
Nico Huberac90af62022-12-18 00:22:47 +0000776 "flashrom-stable@flashrom.org:\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000777 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
778 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000779 "===\n");
780 }
781
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000782 /* First flash chip detected on this bus. */
783 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000784 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000785 /* Not the first flash chip detected on this bus, but not a generic match either. */
786 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
787 break;
788 /* 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 +0000789notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000790 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000791 free(flash->chip);
792 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000793 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000794
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000795 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000796 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000797
Nico Huber5bd990c2019-06-16 19:46:46 +0200798 /* Fill default layout covering the whole chip. */
Nico Huber671c0f02019-06-16 20:17:19 +0200799 if (flashrom_layout_new(&flash->default_layout) ||
Nico Huber5bd990c2019-06-16 19:46:46 +0200800 flashrom_layout_add_region(flash->default_layout,
Nico Huber92e0b622019-06-15 15:55:11 +0200801 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huber5bd990c2019-06-16 19:46:46 +0200802 flashrom_layout_include_region(flash->default_layout, "complete flash"))
803 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000804
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000805 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000806 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
807 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000808 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000809#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200810 if (programmer->map_flash_region == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000811 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
812 PRIxPTR_WIDTH, flash->physical_memory);
813 else
814#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200815 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000816
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000817 /* Flash registers may more likely not be mapped if the chip was forced.
818 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000819 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000820 if (flash->chip->printlock)
821 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000822
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000823 /* Get out of the way for later runs. */
824 unmap_flash(flash);
825
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000826 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000827 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000828}
829
Stefan Tauner96658be2014-05-26 22:05:31 +0000830/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000831static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000832{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000833 int i, j, k;
834 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530835 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000836
837 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
838 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000839 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +0530840 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000841
842 for (i = 0; i < NUM_ERASEREGIONS; i++) {
843 /* Blocks with zero size are bugs in flashchips.c. */
844 if (eraser.eraseblocks[i].count &&
845 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000846 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
847 "Please report a bug at flashrom-stable@flashrom.org\n",
848 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000849 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000850 }
851 /* Blocks with zero count are bugs in flashchips.c. */
852 if (!eraser.eraseblocks[i].count &&
853 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +0000854 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
855 "Please report a bug at flashrom-stable@flashrom.org\n",
856 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000857 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000858 }
859 done += eraser.eraseblocks[i].count *
860 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530861 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000862 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000863 /* Empty eraseblock definition with erase function. */
864 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +0000865 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000866 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000867 if (!done)
868 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000869 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000870 msg_gerr("ERROR: Flash chip %s erase function %i "
871 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +0000872 " expected 0x%06x bytes.\n"
873 "Please report a bug at flashrom-stable@flashrom.org\n",
874 chip->name, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000875 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000876 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000877 if (!eraser.block_erase)
878 continue;
879 /* Check if there are identical erase functions for different
880 * layouts. That would imply "magic" erase functions. The
881 * easiest way to check this is with function pointers.
882 */
Uwe Hermann43959702010-03-13 17:28:29 +0000883 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000884 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000885 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000886 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
887 "Please report a bug at flashrom-stable@flashrom.org\n",
888 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000889 ret = 1;
890 }
Uwe Hermann43959702010-03-13 17:28:29 +0000891 }
Aarya Chaumal478e1792022-06-04 01:34:44 +0530892 if(curr_eraseblock_count > prev_eraseblock_count)
893 {
Nico Huberac90af62022-12-18 00:22:47 +0000894 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
895 "Please report a bug at flashrom-stable@flashrom.org\n",
896 chip->name, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +0530897 ret = 1;
898 }
899 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000900 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +0000901 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000902}
903
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000904static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000905{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000906 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000907
908 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
909 if (log)
910 msg_cdbg("not defined. ");
911 return 1;
912 }
913 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
914 if (log)
915 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000916 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000917 return 1;
918 }
919 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
920 if (log)
921 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000922 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000923 return 1;
924 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000925 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +0000926 return 0;
927}
928
Nico Huber7af0e792016-04-29 16:40:15 +0200929/**
930 * @brief Reads the included layout regions into a buffer.
931 *
932 * If there is no layout set in the given flash context, the whole chip will
933 * be read.
934 *
935 * @param flashctx Flash context to be used.
936 * @param buffer Buffer of full chip size to read into.
937 * @return 0 on success,
938 * 1 if any read fails.
939 */
940static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
941{
942 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +0200943 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +0200944
Nico Huber5ca55232019-06-15 22:29:08 +0200945 while ((entry = layout_next_included(layout, entry))) {
946 const chipoff_t region_start = entry->start;
947 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +0200948
949 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
950 return 1;
951 }
952 return 0;
953}
954
955typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
956/**
957 * @private
958 *
959 * For read-erase-write, `curcontents` and `newcontents` shall point
960 * to buffers of the chip's size. Both are supposed to be prefilled
961 * with at least the included layout regions of the current flash
962 * contents (`curcontents`) and the data to be written to the flash
963 * (`newcontents`).
964 *
965 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
966 *
967 * The `chipoff_t` values are used internally by `walk_by_layout()`.
968 */
969struct walk_info {
970 uint8_t *curcontents;
971 const uint8_t *newcontents;
972 chipoff_t region_start;
973 chipoff_t region_end;
974 chipoff_t erase_start;
975 chipoff_t erase_end;
976};
977/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
978typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
979
980static int walk_eraseblocks(struct flashctx *const flashctx,
981 struct walk_info *const info,
982 const size_t erasefunction, const per_blockfn_t per_blockfn)
983{
984 int ret;
985 size_t i, j;
986 bool first = true;
987 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
988
989 info->erase_start = 0;
990 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
991 /* count==0 for all automatically initialized array
992 members so the loop below won't be executed for them. */
993 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
994 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
995
996 /* Skip any eraseblock that is completely outside the current region. */
997 if (info->erase_end < info->region_start)
998 continue;
999 if (info->region_end < info->erase_start)
1000 break;
1001
1002 /* Print this for every block except the first one. */
1003 if (first)
1004 first = false;
1005 else
1006 msg_cdbg(", ");
1007 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1008
1009 ret = per_blockfn(flashctx, info, eraser->block_erase);
1010 if (ret)
1011 return ret;
1012 }
1013 if (info->region_end < info->erase_start)
1014 break;
1015 }
1016 msg_cdbg("\n");
1017 return 0;
1018}
1019
1020static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1021 const per_blockfn_t per_blockfn)
1022{
1023 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001024 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001025
1026 all_skipped = true;
1027 msg_cinfo("Erasing and writing flash chip... ");
1028
Nico Huber5ca55232019-06-15 22:29:08 +02001029 while ((entry = layout_next_included(layout, entry))) {
1030 info->region_start = entry->start;
1031 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001032
1033 size_t j;
1034 int error = 1; /* retry as long as it's 1 */
1035 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1036 if (j != 0)
1037 msg_cinfo("Looking for another erase function.\n");
1038 msg_cdbg("Trying erase function %zi... ", j);
1039 if (check_block_eraser(flashctx, j, 1))
1040 continue;
1041
1042 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1043 if (error != 1)
1044 break;
1045
1046 if (info->curcontents) {
1047 msg_cinfo("Reading current flash chip contents... ");
1048 if (read_by_layout(flashctx, info->curcontents)) {
1049 /* Now we are truly screwed. Read failed as well. */
1050 msg_cerr("Can't read anymore! Aborting.\n");
1051 /* We have no idea about the flash chip contents, so
1052 retrying with another erase function is pointless. */
1053 error = 2;
1054 break;
1055 }
1056 msg_cinfo("done. ");
1057 }
1058 }
1059 if (error == 1)
1060 msg_cinfo("No usable erase functions left.\n");
1061 if (error) {
1062 msg_cerr("FAILED!\n");
1063 return 1;
1064 }
1065 }
1066 if (all_skipped)
1067 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1068 msg_cinfo("Erase/write done.\n");
1069 return 0;
1070}
1071
1072static int erase_block(struct flashctx *const flashctx,
1073 const struct walk_info *const info, const erasefn_t erasefn)
1074{
1075 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001076 const bool region_unaligned = info->region_start > info->erase_start ||
1077 info->erase_end > info->region_end;
1078 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1079 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001080
Nico Huber6e61e0c2019-01-23 17:07:49 +01001081 /*
1082 * If the region is not erase-block aligned, merge current flash con-
1083 * tents into a new buffer `backup_contents`.
1084 */
1085 if (region_unaligned) {
1086 backup_contents = malloc(erase_len);
1087 erased_contents = malloc(erase_len);
1088 if (!backup_contents || !erased_contents) {
1089 msg_cerr("Out of memory!\n");
1090 ret = 1;
1091 goto _free_ret;
1092 }
1093 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1094 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1095
1096 msg_cdbg("R");
1097 /* Merge data preceding the current region. */
1098 if (info->region_start > info->erase_start) {
1099 const chipoff_t start = info->erase_start;
1100 const chipsize_t len = info->region_start - info->erase_start;
1101 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1102 msg_cerr("Can't read! Aborting.\n");
1103 goto _free_ret;
1104 }
1105 }
1106 /* Merge data following the current region. */
1107 if (info->erase_end > info->region_end) {
1108 const chipoff_t start = info->region_end + 1;
1109 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1110 const chipsize_t len = info->erase_end - info->region_end;
1111 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1112 msg_cerr("Can't read! Aborting.\n");
1113 goto _free_ret;
1114 }
1115 }
1116 }
1117
1118 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001119 all_skipped = false;
1120
1121 msg_cdbg("E");
1122 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001123 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001124 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1125 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001126 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001127 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001128
1129 if (region_unaligned) {
1130 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1131 /* get_next_write() sets starthere to a new value after the call. */
1132 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1133 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1134 if (!writecount++)
1135 msg_cdbg("W");
1136 /* Needs the partial write function signature. */
1137 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1138 info->erase_start + starthere, lenhere))
1139 goto _free_ret;
1140 starthere += lenhere;
1141 }
1142 }
1143
1144 ret = 0;
1145
1146_free_ret:
1147 free(erased_contents);
1148 free(backup_contents);
1149 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001150}
1151
1152/**
1153 * @brief Erases the included layout regions.
1154 *
1155 * If there is no layout set in the given flash context, the whole chip will
1156 * be erased.
1157 *
1158 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001159 * @return 0 on success,
1160 * 1 if all available erase functions failed.
1161 */
Nico Huber454f6132012-12-10 13:34:10 +00001162static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001163{
1164 struct walk_info info = { 0 };
1165 return walk_by_layout(flashctx, &info, &erase_block);
1166}
1167
1168static int read_erase_write_block(struct flashctx *const flashctx,
1169 const struct walk_info *const info, const erasefn_t erasefn)
1170{
1171 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1172 const bool region_unaligned = info->region_start > info->erase_start ||
1173 info->erase_end > info->region_end;
1174 const uint8_t *newcontents = NULL;
1175 int ret = 2;
1176
1177 /*
1178 * If the region is not erase-block aligned, merge current flash con-
1179 * tents into `info->curcontents` and a new buffer `newc`. The former
1180 * is necessary since we have no guarantee that the full erase block
1181 * was already read into `info->curcontents`. For the latter a new
1182 * buffer is used since `info->newcontents` might contain data for
1183 * other unaligned regions that touch this erase block too.
1184 */
1185 if (region_unaligned) {
1186 msg_cdbg("R");
1187 uint8_t *const newc = malloc(erase_len);
1188 if (!newc) {
1189 msg_cerr("Out of memory!\n");
1190 return 1;
1191 }
1192 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1193
1194 /* Merge data preceding the current region. */
1195 if (info->region_start > info->erase_start) {
1196 const chipoff_t start = info->erase_start;
1197 const chipsize_t len = info->region_start - info->erase_start;
1198 if (flashctx->chip->read(flashctx, newc, start, len)) {
1199 msg_cerr("Can't read! Aborting.\n");
1200 goto _free_ret;
1201 }
1202 memcpy(info->curcontents + start, newc, len);
1203 }
1204 /* Merge data following the current region. */
1205 if (info->erase_end > info->region_end) {
1206 const chipoff_t start = info->region_end + 1;
1207 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1208 const chipsize_t len = info->erase_end - info->region_end;
1209 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1210 msg_cerr("Can't read! Aborting.\n");
1211 goto _free_ret;
1212 }
1213 memcpy(info->curcontents + start, newc + rel_start, len);
1214 }
1215
1216 newcontents = newc;
1217 } else {
1218 newcontents = info->newcontents + info->erase_start;
1219 }
1220
1221 ret = 1;
1222 bool skipped = true;
1223 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001224 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001225 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1226 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001227 if (erase_block(flashctx, info, erasefn))
1228 goto _free_ret;
1229 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001230 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001231 skipped = false;
1232 }
1233
1234 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1235 /* get_next_write() sets starthere to a new value after the call. */
1236 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1237 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1238 if (!writecount++)
1239 msg_cdbg("W");
1240 /* Needs the partial write function signature. */
1241 if (flashctx->chip->write(flashctx, newcontents + starthere,
1242 info->erase_start + starthere, lenhere))
1243 goto _free_ret;
1244 starthere += lenhere;
1245 skipped = false;
1246 }
1247 if (skipped)
1248 msg_cdbg("S");
1249 else
1250 all_skipped = false;
1251
1252 /* Update curcontents, other regions with overlapping erase blocks
1253 might rely on this. */
1254 memcpy(curcontents, newcontents, erase_len);
1255 ret = 0;
1256
1257_free_ret:
1258 if (region_unaligned)
1259 free((void *)newcontents);
1260 return ret;
1261}
1262
1263/**
1264 * @brief Writes the included layout regions from a given image.
1265 *
1266 * If there is no layout set in the given flash context, the whole image
1267 * will be written.
1268 *
1269 * @param flashctx Flash context to be used.
1270 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1271 * @param newcontents The new image to be written.
1272 * @return 0 on success,
1273 * 1 if anything has gone wrong.
1274 */
Nico Huber454f6132012-12-10 13:34:10 +00001275static int write_by_layout(struct flashctx *const flashctx,
1276 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001277{
1278 struct walk_info info;
1279 info.curcontents = curcontents;
1280 info.newcontents = newcontents;
1281 return walk_by_layout(flashctx, &info, read_erase_write_block);
1282}
1283
1284/**
1285 * @brief Compares the included layout regions with content from a buffer.
1286 *
1287 * If there is no layout set in the given flash context, the whole chip's
1288 * contents will be compared.
1289 *
1290 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001291 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001292 * @param curcontents A buffer of full chip size to read current chip contents into.
1293 * @param newcontents The new image to compare to.
1294 * @return 0 on success,
1295 * 1 if reading failed,
1296 * 3 if the contents don't match.
1297 */
Nico Huber74d09d42019-06-16 03:27:26 +02001298static int verify_by_layout(
1299 struct flashctx *const flashctx,
1300 const struct flashrom_layout *const layout,
1301 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001302{
Nico Huber5ca55232019-06-15 22:29:08 +02001303 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001304
Nico Huber5ca55232019-06-15 22:29:08 +02001305 while ((entry = layout_next_included(layout, entry))) {
1306 const chipoff_t region_start = entry->start;
1307 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001308
1309 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1310 return 1;
1311 if (compare_range(newcontents + region_start, curcontents + region_start,
1312 region_start, region_len))
1313 return 3;
1314 }
1315 return 0;
1316}
1317
Stefan Tauner136388f2013-07-15 10:47:53 +00001318static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001319{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001320 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001321#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001322 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001323 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberac90af62022-12-18 00:22:47 +00001324 "chip. Please report this to the mailing list at flashrom-stable@flashrom.org or\n"
1325 "on IRC (see https://www.flashrom.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001326 "-------------------------------------------------------------------------------\n"
1327 "You may now reboot or simply leave the machine running.\n");
1328 else
1329#endif
1330 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1331 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
Nico Huberac90af62022-12-18 00:22:47 +00001332 "please report this to the mailing list at flashrom-stable@flashrom.org or on IRC\n"
1333 "(see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001334}
1335
Edward O'Callaghanc72d20a2021-12-13 12:30:03 +11001336void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001337{
Stefan Tauner136388f2013-07-15 10:47:53 +00001338 msg_gerr("Your flash chip is in an unknown state.\n");
1339#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001340 if (programmer == &programmer_internal)
Angel Pons1900e1d2021-07-02 12:42:23 +02001341 msg_gerr("Get help on IRC (see https://www.flashrom.org/Contact) or mail\n"
Nico Huberac90af62022-12-18 00:22:47 +00001342 "flashrom-stable@flashrom.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001343 "-------------------------------------------------------------------------------\n"
1344 "DO NOT REBOOT OR POWEROFF!\n");
1345 else
1346#endif
Nico Huberac90af62022-12-18 00:22:47 +00001347 msg_gerr("Please report this to the mailing list at flashrom-stable@flashrom.org\n"
1348 "or on IRC (see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001349}
1350
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001351void list_programmers_linebreak(int startcol, int cols, int paren)
1352{
1353 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001354 int pnamelen;
1355 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001356 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001357 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001358
Thomas Heijligend45cb592021-05-19 14:12:18 +02001359 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001360 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001361 pnamelen = strlen(pname);
1362 if (remaining - pnamelen - 2 < 0) {
1363 if (firstline)
1364 firstline = 0;
1365 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001366 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001367 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001368 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001369 remaining = cols - startcol;
1370 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001371 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001372 remaining--;
1373 }
1374 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001375 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001376 remaining--;
1377 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001378 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001379 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001380 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001381 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001382 remaining--;
1383 } else {
1384 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001385 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001386 }
1387 }
1388}
1389
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001390int selfcheck(void)
1391{
Stefan Tauner96658be2014-05-26 22:05:31 +00001392 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001393 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001394
Thomas Heijligend45cb592021-05-19 14:12:18 +02001395 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001396 const struct programmer_entry *const p = programmer_table[i];
1397 if (p == NULL) {
1398 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1399 ret = 1;
1400 continue;
1401 }
1402 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001403 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1404 ret = 1;
1405 /* This might hide other problems with this programmer, but allows for better error
1406 * messages below without jumping through hoops. */
1407 continue;
1408 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001409 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001410 case USB:
1411 case PCI:
1412 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001413 if (p->devs.note == NULL) {
1414 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001415 break; /* This one has its device list stored separately. */
1416 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001417 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001418 ret = 1;
1419 }
1420 break;
1421 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001422 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001423 ret = 1;
1424 break;
1425 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001426 if (p->init == NULL) {
1427 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001428 ret = 1;
1429 }
Stefan Taunera6d96482012-12-26 19:51:23 +00001430 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001431
1432 /* It would be favorable if we could check for the correct layout (especially termination) of various
1433 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1434 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1435 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1436 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1437 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001438 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001439 msg_gerr("Flashchips table miscompilation!\n");
1440 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001441 } else {
1442 for (i = 0; i < flashchips_size - 1; i++) {
1443 const struct flashchip *chip = &flashchips[i];
1444 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1445 ret = 1;
1446 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
Nico Huberac90af62022-12-18 00:22:47 +00001447 "Please report a bug at flashrom-stable@flashrom.org\n", i,
Stefan Tauner96658be2014-05-26 22:05:31 +00001448 chip->name == NULL ? "unnamed" : chip->name);
1449 }
1450 if (selfcheck_eraseblocks(chip)) {
1451 ret = 1;
1452 }
1453 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001454 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001455
Stefan Tauner600576b2014-06-12 22:57:36 +00001456#if CONFIG_INTERNAL == 1
1457 ret |= selfcheck_board_enables();
1458#endif
1459
Stefan Tauner96658be2014-05-26 22:05:31 +00001460 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001461 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001462}
1463
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001464/* FIXME: This function signature needs to be improved once prepare_flash_access()
1465 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001466 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001467static int chip_safety_check(const struct flashctx *flash, int force,
1468 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001469{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001470 const struct flashchip *chip = flash->chip;
1471
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001472 if (!programmer_may_write && (write_it || erase_it)) {
1473 msg_perr("Write/erase is not working yet on your programmer in "
1474 "its current configuration.\n");
1475 /* --force is the wrong approach, but it's the best we can do
1476 * until the generic programmer parameter parser is merged.
1477 */
1478 if (!force)
1479 return 1;
1480 msg_cerr("Continuing anyway.\n");
1481 }
1482
1483 if (read_it || erase_it || write_it || verify_it) {
1484 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001485 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001486 msg_cerr("Read is not working on this chip. ");
1487 if (!force)
1488 return 1;
1489 msg_cerr("Continuing anyway.\n");
1490 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001491 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001492 msg_cerr("flashrom has no read function for this "
1493 "flash chip.\n");
1494 return 1;
1495 }
1496 }
1497 if (erase_it || write_it) {
1498 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001499 if (chip->tested.erase == NA) {
1500 msg_cerr("Erase is not possible on this chip.\n");
1501 return 1;
1502 }
1503 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001504 msg_cerr("Erase is not working on this chip. ");
1505 if (!force)
1506 return 1;
1507 msg_cerr("Continuing anyway.\n");
1508 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001509 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001510 msg_cerr("flashrom has no erase function for this "
1511 "flash chip.\n");
1512 return 1;
1513 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001514 }
1515 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001516 if (chip->tested.write == NA) {
1517 msg_cerr("Write is not possible on this chip.\n");
1518 return 1;
1519 }
1520 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001521 msg_cerr("Write is not working on this chip. ");
1522 if (!force)
1523 return 1;
1524 msg_cerr("Continuing anyway.\n");
1525 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001526 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001527 msg_cerr("flashrom has no write function for this "
1528 "flash chip.\n");
1529 return 1;
1530 }
1531 }
1532 return 0;
1533}
1534
Nico Huber305f4172013-06-14 11:55:26 +02001535int prepare_flash_access(struct flashctx *const flash,
1536 const bool read_it, const bool write_it,
1537 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001538{
1539 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1540 msg_cerr("Aborting.\n");
1541 return 1;
1542 }
1543
Nico Hubere0ed4122021-05-14 00:48:28 +02001544 if (layout_sanity_checks(flash)) {
Nico Huber454f6132012-12-10 13:34:10 +00001545 msg_cerr("Requested regions can not be handled. Aborting.\n");
1546 return 1;
1547 }
1548
1549 if (map_flash(flash) != 0)
1550 return 1;
1551
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001552 /* Initialize chip_restore_fn_count before chip unlock calls. */
1553 flash->chip_restore_fn_count = 0;
1554
Nico Huber454f6132012-12-10 13:34:10 +00001555 /* Given the existence of read locks, we want to unlock for read,
1556 erase and write. */
1557 if (flash->chip->unlock)
1558 flash->chip->unlock(flash);
1559
Nico Huberf43c6542017-10-14 17:47:28 +02001560 flash->address_high_byte = -1;
1561 flash->in_4ba_mode = false;
1562
Nico Huberdc5af542018-12-22 16:54:59 +01001563 /* Be careful about 4BA chips and broken masters */
1564 if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
1565 /* If we can't use native instructions, bail out */
1566 if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
1567 || !spi_master_4ba(flash)) {
1568 msg_cerr("Programmer doesn't support this chip. Aborting.\n");
1569 return 1;
1570 }
1571 }
1572
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001573 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01001574 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01001575 int ret;
1576 if (spi_master_4ba(flash))
1577 ret = spi_enter_4ba(flash);
1578 else
1579 ret = spi_exit_4ba(flash);
1580 if (ret) {
1581 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001582 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02001583 }
Boris Baykov99127182016-06-11 18:29:00 +02001584 }
1585
Nico Huber454f6132012-12-10 13:34:10 +00001586 return 0;
1587}
1588
Nico Huber305f4172013-06-14 11:55:26 +02001589void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001590{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001591 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001592 unmap_flash(flash);
1593}
1594
1595/**
1596 * @addtogroup flashrom-flash
1597 * @{
1598 */
1599
1600/**
1601 * @brief Erase the specified ROM chip.
1602 *
1603 * If a layout is set in the given flash context, only included regions
1604 * will be erased.
1605 *
1606 * @param flashctx The context of the flash chip to erase.
1607 * @return 0 on success.
1608 */
1609int flashrom_flash_erase(struct flashctx *const flashctx)
1610{
1611 if (prepare_flash_access(flashctx, false, false, true, false))
1612 return 1;
1613
1614 const int ret = erase_by_layout(flashctx);
1615
1616 finalize_flash_access(flashctx);
1617
1618 return ret;
1619}
1620
1621/** @} */ /* end flashrom-flash */
1622
1623/**
1624 * @defgroup flashrom-ops Operations
1625 * @{
1626 */
1627
1628/**
1629 * @brief Read the current image from the specified ROM chip.
1630 *
1631 * If a layout is set in the specified flash context, only included regions
1632 * will be read.
1633 *
1634 * @param flashctx The context of the flash chip.
1635 * @param buffer Target buffer to write image to.
1636 * @param buffer_len Size of target buffer in bytes.
1637 * @return 0 on success,
1638 * 2 if buffer_len is too short for the flash chip's contents,
1639 * or 1 on any other failure.
1640 */
1641int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
1642{
1643 const size_t flash_size = flashctx->chip->total_size * 1024;
1644
1645 if (flash_size > buffer_len)
1646 return 2;
1647
1648 if (prepare_flash_access(flashctx, true, false, false, false))
1649 return 1;
1650
1651 msg_cinfo("Reading flash... ");
1652
1653 int ret = 1;
1654 if (read_by_layout(flashctx, buffer)) {
1655 msg_cerr("Read operation failed!\n");
1656 msg_cinfo("FAILED.\n");
1657 goto _finalize_ret;
1658 }
1659 msg_cinfo("done.\n");
1660 ret = 0;
1661
1662_finalize_ret:
1663 finalize_flash_access(flashctx);
1664 return ret;
1665}
1666
1667static void combine_image_by_layout(const struct flashctx *const flashctx,
1668 uint8_t *const newcontents, const uint8_t *const oldcontents)
1669{
1670 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001671 const struct romentry *included;
1672 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001673
Nico Huber3d7b1e32018-12-22 00:53:14 +01001674 while ((included = layout_next_included_region(layout, start))) {
1675 if (included->start > start) {
1676 /* copy everything up to the start of this included region */
1677 memcpy(newcontents + start, oldcontents + start, included->start - start);
1678 }
1679 /* skip this included region */
1680 start = included->end + 1;
1681 if (start == 0)
1682 return;
Nico Huber454f6132012-12-10 13:34:10 +00001683 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001684
1685 /* copy the rest of the chip */
1686 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1687 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001688}
1689
1690/**
1691 * @brief Write the specified image to the ROM chip.
1692 *
1693 * If a layout is set in the specified flash context, only erase blocks
1694 * containing included regions will be touched.
1695 *
1696 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001697 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001698 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001699 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001700 * @return 0 on success,
1701 * 4 if buffer_len doesn't match the size of the flash chip,
1702 * 3 if write was tried but nothing has changed,
1703 * 2 if write failed and flash contents changed,
1704 * or 1 on any other failure.
1705 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001706int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
1707 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001708{
1709 const size_t flash_size = flashctx->chip->total_size * 1024;
1710 const bool verify_all = flashctx->flags.verify_whole_chip;
1711 const bool verify = flashctx->flags.verify_after_write;
Nico Huber74d09d42019-06-16 03:27:26 +02001712 const struct flashrom_layout *const verify_layout =
1713 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001714
1715 if (buffer_len != flash_size)
1716 return 4;
1717
1718 int ret = 1;
1719
1720 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001721 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001722 uint8_t *const curcontents = malloc(flash_size);
1723 uint8_t *oldcontents = NULL;
1724 if (verify_all)
1725 oldcontents = malloc(flash_size);
1726 if (!curcontents || (verify_all && !oldcontents)) {
1727 msg_gerr("Out of memory!\n");
1728 goto _free_ret;
1729 }
1730
1731#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001732 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001733 if (flashctx->flags.force_boardmismatch) {
1734 msg_pinfo("Proceeding anyway because user forced us to.\n");
1735 } else {
1736 msg_perr("Aborting. You can override this with "
1737 "-p internal:boardmismatch=force.\n");
1738 goto _free_ret;
1739 }
1740 }
1741#endif
1742
1743 if (prepare_flash_access(flashctx, false, true, false, verify))
1744 goto _free_ret;
1745
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001746 /* If given, assume flash chip contains same data as `refcontents`. */
1747 if (refcontents) {
1748 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
1749 memcpy(curcontents, refcontents, flash_size);
1750 if (oldcontents)
1751 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00001752 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001753 /*
1754 * Read the whole chip to be able to check whether regions need to be
1755 * erased and to give better diagnostics in case write fails.
1756 * The alternative is to read only the regions which are to be
1757 * preserved, but in that case we might perform unneeded erase which
1758 * takes time as well.
1759 */
1760 msg_cinfo("Reading old flash chip contents... ");
1761 if (verify_all) {
1762 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
1763 msg_cinfo("FAILED.\n");
1764 goto _finalize_ret;
1765 }
1766 memcpy(curcontents, oldcontents, flash_size);
1767 } else {
1768 if (read_by_layout(flashctx, curcontents)) {
1769 msg_cinfo("FAILED.\n");
1770 goto _finalize_ret;
1771 }
Nico Huber454f6132012-12-10 13:34:10 +00001772 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001773 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00001774 }
Nico Huber454f6132012-12-10 13:34:10 +00001775
1776 if (write_by_layout(flashctx, curcontents, newcontents)) {
1777 msg_cerr("Uh oh. Erase/write failed. ");
1778 ret = 2;
1779 if (verify_all) {
1780 msg_cerr("Checking if anything has changed.\n");
1781 msg_cinfo("Reading current flash chip contents... ");
1782 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
1783 msg_cinfo("done.\n");
1784 if (!memcmp(oldcontents, curcontents, flash_size)) {
1785 nonfatal_help_message();
1786 goto _finalize_ret;
1787 }
1788 msg_cerr("Apparently at least some data has changed.\n");
1789 } else
1790 msg_cerr("Can't even read anymore!\n");
1791 emergency_help_message();
1792 goto _finalize_ret;
1793 } else {
1794 msg_cerr("\n");
1795 }
1796 emergency_help_message();
1797 goto _finalize_ret;
1798 }
1799
1800 /* Verify only if we actually changed something. */
1801 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00001802 msg_cinfo("Verifying flash... ");
1803
1804 /* Work around chips which need some time to calm down. */
1805 programmer_delay(1000*1000);
1806
Nico Huber74d09d42019-06-16 03:27:26 +02001807 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00001808 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02001809 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001810 /* If we tried to write, and verification now fails, we
1811 might have an emergency situation. */
1812 if (ret)
1813 emergency_help_message();
1814 else
1815 msg_cinfo("VERIFIED.\n");
1816 } else {
1817 /* We didn't change anything. */
1818 ret = 0;
1819 }
1820
1821_finalize_ret:
1822 finalize_flash_access(flashctx);
1823_free_ret:
1824 free(oldcontents);
1825 free(curcontents);
1826 return ret;
1827}
1828
1829/**
1830 * @brief Verify the ROM chip's contents with the specified image.
1831 *
1832 * If a layout is set in the specified flash context, only included regions
1833 * will be verified.
1834 *
1835 * @param flashctx The context of the flash chip.
1836 * @param buffer Source buffer to verify with.
1837 * @param buffer_len Size of source buffer in bytes.
1838 * @return 0 on success,
1839 * 3 if the chip's contents don't match,
1840 * 2 if buffer_len doesn't match the size of the flash chip,
1841 * or 1 on any other failure.
1842 */
1843int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
1844{
Nico Huber74d09d42019-06-16 03:27:26 +02001845 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001846 const size_t flash_size = flashctx->chip->total_size * 1024;
1847
1848 if (buffer_len != flash_size)
1849 return 2;
1850
1851 const uint8_t *const newcontents = buffer;
1852 uint8_t *const curcontents = malloc(flash_size);
1853 if (!curcontents) {
1854 msg_gerr("Out of memory!\n");
1855 return 1;
1856 }
1857
1858 int ret = 1;
1859
1860 if (prepare_flash_access(flashctx, false, false, false, true))
1861 goto _free_ret;
1862
1863 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02001864 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00001865 if (!ret)
1866 msg_cinfo("VERIFIED.\n");
1867
1868 finalize_flash_access(flashctx);
1869_free_ret:
1870 free(curcontents);
1871 return ret;
1872}
1873
1874/** @} */ /* end flashrom-ops */