blob: 1048164a79aa229eb46239f393cd9293ed627ea2 [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
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000022#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000023#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000024#ifndef __LIBPAYLOAD__
25#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000026#include <sys/stat.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000027#endif
Ronald G. Minnichceec4202003-07-25 04:37:41 +000028#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000029#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000030#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000031#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000032#include <ctype.h>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000033#if HAVE_UTSNAME == 1
34#include <sys/utsname.h>
35#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000036#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000037#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000038#include "programmer.h"
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +000039#include "hwaccess.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010040#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000041
Mathias Krausea60faab2011-01-17 07:50:42 +000042const char flashrom_version[] = FLASHROM_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000043const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000044
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020045static const struct programmer_entry *programmer = NULL;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000046static const char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000047
Uwe Hermann48ec1b12010-08-08 17:01:18 +000048/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000049 * Programmers supporting multiple buses can have differing size limits on
50 * each bus. Store the limits for each bus in a common struct.
51 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000052struct decode_sizes max_rom_decode;
53
54/* If nonzero, used as the start address of bottom-aligned flash. */
55unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000056
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000057/* Is writing allowed with this programmer? */
58int programmer_may_write;
59
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000060#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000061static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000062/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000063static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000064 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000065 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000066} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000067/* Initialize to 0 to make sure nobody registers a shutdown function before
68 * programmer init.
69 */
70static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000071
Stefan Taunerc4f44df2013-08-12 22:58:43 +000072/* Did we change something or was every erase/write skipped (if any)? */
73static bool all_skipped = true;
74
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000075static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000076
Stefan Tauner2a1ed772014-08-31 00:09:21 +000077int shutdown_free(void *data)
78{
79 free(data);
80 return 0;
81}
82
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000083/* Register a function to be executed on programmer shutdown.
84 * The advantage over atexit() is that you can supply a void pointer which will
85 * be used as parameter to the registered function upon programmer shutdown.
86 * This pointer can point to arbitrary data used by said function, e.g. undo
87 * information for GPIO settings etc. If unneeded, set data=NULL.
88 * Please note that the first (void *data) belongs to the function signature of
89 * the function passed as first parameter.
90 */
David Hendricks8bb20212011-06-14 01:35:36 +000091int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000092{
93 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000094 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000095 SHUTDOWN_MAXFN);
96 return 1;
97 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000098 if (!may_register_shutdown) {
99 msg_perr("Tried to register a shutdown function before "
100 "programmer init.\n");
101 return 1;
102 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000103 shutdown_fn[shutdown_fn_count].func = function;
104 shutdown_fn[shutdown_fn_count].data = data;
105 shutdown_fn_count++;
106
107 return 0;
108}
109
Nikolai Artemiev4ad48642020-11-05 13:54:27 +1100110int register_chip_restore(chip_restore_fn_cb_t func,
111 struct flashctx *flash, uint8_t status)
112{
113 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
114 msg_perr("Tried to register more than %i chip restore"
115 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
116 return 1;
117 }
118 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
119 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
120 flash->chip_restore_fn_count++;
121
122 return 0;
123}
124
125static int deregister_chip_restore(struct flashctx *flash)
126{
127 int rc = 0;
128
129 while (flash->chip_restore_fn_count > 0) {
130 int i = --flash->chip_restore_fn_count;
131 rc |= flash->chip_restore_fn[i].func(
132 flash, flash->chip_restore_fn[i].status);
133 }
134
135 return rc;
136}
137
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200138int programmer_init(const struct programmer_entry *prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000139{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000140 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000141
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200142 if (prog == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000143 msg_perr("Invalid programmer specified!\n");
144 return -1;
145 }
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200146 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000147 /* Initialize all programmer specific data. */
148 /* Default to unlimited decode sizes. */
149 max_rom_decode = (const struct decode_sizes) {
150 .parallel = 0xffffffff,
151 .lpc = 0xffffffff,
152 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000153 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000154 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000155 /* Default to top aligned flash at 4 GB. */
156 flashbase = 0;
157 /* Registering shutdown functions is now allowed. */
158 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000159 /* Default to allowing writes. Broken programmers set this to 0. */
160 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000161
162 programmer_param = param;
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200163 msg_pdbg("Initializing %s programmer\n", programmer->name);
164 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000165 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000166 if (ret != 0) {
167 /* It is quite possible that any unhandled programmer parameter would have been valid,
168 * but an error in actual programmer init happened before the parameter was evaluated.
169 */
170 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
171 programmer_param);
172 } else {
173 /* Actual programmer init was successful, but the user specified an invalid or unusable
174 * (for the current programmer configuration) parameter.
175 */
176 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
177 msg_perr("Aborting.\n");
178 ret = ERROR_FATAL;
179 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000180 }
181 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000182}
183
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000184/** Calls registered shutdown functions and resets internal programmer-related variables.
185 * Calling it is safe even without previous initialization, but further interactions with programmer support
186 * require a call to programmer_init() (afterwards).
187 *
188 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000189int programmer_shutdown(void)
190{
David Hendricks8bb20212011-06-14 01:35:36 +0000191 int ret = 0;
192
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000193 /* Registering shutdown functions is no longer allowed. */
194 may_register_shutdown = 0;
195 while (shutdown_fn_count > 0) {
196 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000197 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000198 }
Stefan Taunere34e3e82013-01-01 00:06:51 +0000199
Stefan Taunerb8911d62012-12-26 07:55:00 +0000200 programmer_param = NULL;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000201 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000202
David Hendricks8bb20212011-06-14 01:35:36 +0000203 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000204}
205
Stefan Tauner305e0b92013-07-17 23:46:44 +0000206void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000207{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200208 void *ret = programmer->map_flash_region(descr, phys_addr, len);
Stefan Tauner26e7a152013-09-13 17:21:05 +0000209 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
210 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
211 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000212}
213
214void programmer_unmap_flash_region(void *virt_addr, size_t len)
215{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200216 programmer->unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000217 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000218}
219
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000220void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000221{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000222 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000223}
224
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000225void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000226{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000227 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000228}
229
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000230void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000231{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000232 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000233}
234
Mark Marshallf20b7be2014-05-09 21:16:21 +0000235void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000236{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000237 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000238}
239
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000240uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000241{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000242 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000243}
244
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000245uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000246{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000247 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000248}
249
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000250uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000251{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000252 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000253}
254
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000255void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
256 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000257{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000258 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000259}
260
Stefan Taunerf80419c2014-05-02 15:41:42 +0000261void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000262{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000263 if (usecs > 0)
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200264 programmer->delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000265}
266
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000267int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
268 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000269{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000270 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000271
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000272 return 0;
273}
274
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000275/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000276 * It will look for needle with a subsequent '=' in haystack, return a copy of
277 * needle and remove everything from the first occurrence of needle to the next
278 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000279 */
Edward O'Callaghand75e1b12021-07-14 15:06:04 +1000280static char *extract_param(const char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000281{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000282 char *param_pos, *opt_pos, *rest;
283 char *opt = NULL;
284 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000285 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000286
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000287 needlelen = strlen(needle);
288 if (!needlelen) {
289 msg_gerr("%s: empty needle! Please report a bug at "
290 "flashrom@flashrom.org\n", __func__);
291 return NULL;
292 }
293 /* No programmer parameters given. */
294 if (*haystack == NULL)
295 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000296 param_pos = strstr(*haystack, needle);
297 do {
298 if (!param_pos)
299 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000300 /* Needle followed by '='? */
301 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000302 /* Beginning of the string? */
303 if (param_pos == *haystack)
304 break;
305 /* After a delimiter? */
306 if (strchr(delim, *(param_pos - 1)))
307 break;
308 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000309 /* Continue searching. */
310 param_pos++;
311 param_pos = strstr(param_pos, needle);
312 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000313
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000314 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000315 /* Get the string after needle and '='. */
316 opt_pos = param_pos + needlelen + 1;
317 optlen = strcspn(opt_pos, delim);
318 /* Return an empty string if the parameter was empty. */
319 opt = malloc(optlen + 1);
320 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000321 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000322 exit(1);
323 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000324 strncpy(opt, opt_pos, optlen);
325 opt[optlen] = '\0';
326 rest = opt_pos + optlen;
327 /* Skip all delimiters after the current parameter. */
328 rest += strspn(rest, delim);
329 memmove(param_pos, rest, strlen(rest) + 1);
330 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000331 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000332
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000333 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000334}
335
Stefan Tauner66652442011-06-26 17:38:17 +0000336char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000337{
338 return extract_param(&programmer_param, param_name, ",");
339}
340
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000341/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000342static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000343{
344 unsigned int usable_erasefunctions = 0;
345 int k;
346 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
347 if (!check_block_eraser(flash, k, 0))
348 usable_erasefunctions++;
349 }
350 return usable_erasefunctions;
351}
352
Mark Marshallf20b7be2014-05-09 21:16:21 +0000353static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000354{
355 int ret = 0, failcount = 0;
356 unsigned int i;
357 for (i = 0; i < len; i++) {
358 if (wantbuf[i] != havebuf[i]) {
359 /* Only print the first failure. */
360 if (!failcount++)
361 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
362 start + i, wantbuf[i], havebuf[i]);
363 }
364 }
365 if (failcount) {
366 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
367 start, start + len - 1, failcount);
368 ret = -1;
369 }
370 return ret;
371}
372
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000373/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600374static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000375{
376 int ret;
377 uint8_t *cmpbuf = malloc(len);
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300378 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000379
380 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000381 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000382 exit(1);
383 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300384 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000385 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000386 free(cmpbuf);
387 return ret;
388}
389
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000390/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000391 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000392 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000393 * @start offset to the base address of the flash chip
394 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000395 * @return 0 for success, -1 for failure
396 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000397int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000398{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000399 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000400 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000401
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100402 if (start + len > flash->chip->total_size * 1024) {
403 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
404 " total_size 0x%x\n", __func__, start, len,
405 flash->chip->total_size * 1024);
406 return -1;
407 }
408
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000409 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000410 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000411 return -1;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000412 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000413
414 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000415 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000416 msg_gerr("Could not allocate memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000417 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000418 }
419
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100420 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000421 if (ret) {
422 msg_gerr("Verification impossible because read failed "
423 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000424 ret = -1;
425 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000426 }
427
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000428 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000429out_free:
430 free(readbuf);
431 return ret;
432}
433
Stefan Tauner02437452013-04-01 19:34:53 +0000434/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300435static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
436 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000437{
438 unsigned int i, j, limit;
439 for (j = 0; j < len / gran; j++) {
440 limit = min (gran, len - j * gran);
441 /* Are 'have' and 'want' identical? */
442 if (!memcmp(have + j * gran, want + j * gran, limit))
443 continue;
444 /* have needs to be in erased state. */
445 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300446 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000447 return 1;
448 }
449 return 0;
450}
451
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000452/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000453 * Check if the buffer @have can be programmed to the content of @want without
454 * erasing. This is only possible if all chunks of size @gran are either kept
455 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000456 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000457 * Warning: This function assumes that @have and @want point to naturally
458 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000459 *
460 * @have buffer with current content
461 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000462 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000463 * @gran write granularity (enum, not count)
464 * @return 0 if no erase is needed, 1 otherwise
465 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000466static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
467 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000468{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000469 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000470 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000471
472 switch (gran) {
473 case write_gran_1bit:
474 for (i = 0; i < len; i++)
475 if ((have[i] & want[i]) != want[i]) {
476 result = 1;
477 break;
478 }
479 break;
480 case write_gran_1byte:
481 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300482 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000483 result = 1;
484 break;
485 }
486 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000487 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300488 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000489 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000490 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300491 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000492 break;
493 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300494 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000495 break;
496 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300497 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000498 break;
499 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300500 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000501 break;
502 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300503 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000504 break;
505 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300506 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000507 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000508 case write_gran_1byte_implicit_erase:
509 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
510 result = 0;
511 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000512 default:
513 msg_cerr("%s: Unsupported granularity! Please report a bug at "
514 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000515 }
516 return result;
517}
518
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000519/**
520 * Check if the buffer @have needs to be programmed to get the content of @want.
521 * If yes, return 1 and fill in first_start with the start address of the
522 * write operation and first_len with the length of the first to-be-written
523 * chunk. If not, return 0 and leave first_start and first_len undefined.
524 *
525 * Warning: This function assumes that @have and @want point to naturally
526 * aligned regions.
527 *
528 * @have buffer with current content
529 * @want buffer with desired content
530 * @len length of the checked area
531 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000532 * @first_start offset of the first byte which needs to be written (passed in
533 * value is increased by the offset of the first needed write
534 * relative to have/want or unchanged if no write is needed)
535 * @return length of the first contiguous area which needs to be written
536 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000537 *
538 * FIXME: This function needs a parameter which tells it about coalescing
539 * in relation to the max write length of the programmer and the max write
540 * length of the chip.
541 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000542static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000543 unsigned int *first_start,
544 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000545{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000546 int need_write = 0;
547 unsigned int rel_start = 0, first_len = 0;
548 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000549
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000550 switch (gran) {
551 case write_gran_1bit:
552 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000553 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000554 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000555 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000556 case write_gran_128bytes:
557 stride = 128;
558 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000559 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000560 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000561 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000562 case write_gran_264bytes:
563 stride = 264;
564 break;
565 case write_gran_512bytes:
566 stride = 512;
567 break;
568 case write_gran_528bytes:
569 stride = 528;
570 break;
571 case write_gran_1024bytes:
572 stride = 1024;
573 break;
574 case write_gran_1056bytes:
575 stride = 1056;
576 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000577 default:
578 msg_cerr("%s: Unsupported granularity! Please report a bug at "
579 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000580 /* Claim that no write was needed. A write with unknown
581 * granularity is too dangerous to try.
582 */
583 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000584 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000585 for (i = 0; i < len / stride; i++) {
586 limit = min(stride, len - i * stride);
587 /* Are 'have' and 'want' identical? */
588 if (memcmp(have + i * stride, want + i * stride, limit)) {
589 if (!need_write) {
590 /* First location where have and want differ. */
591 need_write = 1;
592 rel_start = i * stride;
593 }
594 } else {
595 if (need_write) {
596 /* First location where have and want
597 * do not differ anymore.
598 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000599 break;
600 }
601 }
602 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000603 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000604 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000605 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000606 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000607}
608
Martin Rothf6c1cb12022-03-15 10:55:25 -0600609/* Returns the number of buses commonly supported by the current programmer and flash chip where the latter
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000610 * can not be completely accessed due to size/address limits of the programmer. */
611unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000612{
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000613 unsigned int limitexceeded = 0;
614 uint32_t size = flash->chip->total_size * 1024;
615 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000616
617 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000618 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000619 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000620 "size %u kB of chipset/board/programmer "
621 "for %s interface, "
622 "probe/read/erase/write may fail. ", size / 1024,
623 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000624 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000625 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000626 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000627 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000628 "size %u kB of chipset/board/programmer "
629 "for %s interface, "
630 "probe/read/erase/write may fail. ", size / 1024,
631 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000632 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000633 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000634 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000635 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000636 "size %u kB of chipset/board/programmer "
637 "for %s interface, "
638 "probe/read/erase/write may fail. ", size / 1024,
639 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000640 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000641 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000642 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000643 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000644 "size %u kB of chipset/board/programmer "
645 "for %s interface, "
646 "probe/read/erase/write may fail. ", size / 1024,
647 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000648 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000649 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000650}
651
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000652void unmap_flash(struct flashctx *flash)
653{
654 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
655 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
656 flash->physical_registers = 0;
657 flash->virtual_registers = (chipaddr)ERROR_PTR;
658 }
659
660 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
661 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
662 flash->physical_memory = 0;
663 flash->virtual_memory = (chipaddr)ERROR_PTR;
664 }
665}
666
667int map_flash(struct flashctx *flash)
668{
669 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
670 flash->virtual_memory = (chipaddr)ERROR_PTR;
671 flash->virtual_registers = (chipaddr)ERROR_PTR;
672
673 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
674 * These are used for various probing-related hacks that would not map successfully anyway and should be
675 * removed ASAP. */
676 if (flash->chip->total_size == 0)
677 return 0;
678
679 const chipsize_t size = flash->chip->total_size * 1024;
680 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
681 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
682 if (addr == ERROR_PTR) {
683 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
684 flash->chip->name, PRIxPTR_WIDTH, base);
685 return 1;
686 }
687 flash->physical_memory = base;
688 flash->virtual_memory = (chipaddr)addr;
689
690 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
691 * completely different on some chips and programmers, or not mappable at all.
692 * Ignore these problems for now and always report success. */
693 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
694 base = 0xffffffff - size - 0x400000 + 1;
695 addr = programmer_map_flash_region("flash chip registers", base, size);
696 if (addr == ERROR_PTR) {
697 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
698 flash->chip->name, PRIxPTR_WIDTH, base);
699 return 0;
700 }
701 flash->physical_registers = base;
702 flash->virtual_registers = (chipaddr)addr;
703 }
704 return 0;
705}
706
Nico Huber2d625722016-05-03 10:48:02 +0200707/*
708 * Return a string corresponding to the bustype parameter.
709 * Memory is obtained with malloc() and must be freed with free() by the caller.
710 */
711char *flashbuses_to_text(enum chipbustype bustype)
712{
713 char *ret = calloc(1, 1);
714 /*
715 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
716 * will cease to exist and should be eliminated here as well.
717 */
718 if (bustype == BUS_NONSPI) {
719 ret = strcat_realloc(ret, "Non-SPI, ");
720 } else {
721 if (bustype & BUS_PARALLEL)
722 ret = strcat_realloc(ret, "Parallel, ");
723 if (bustype & BUS_LPC)
724 ret = strcat_realloc(ret, "LPC, ");
725 if (bustype & BUS_FWH)
726 ret = strcat_realloc(ret, "FWH, ");
727 if (bustype & BUS_SPI)
728 ret = strcat_realloc(ret, "SPI, ");
729 if (bustype & BUS_PROG)
730 ret = strcat_realloc(ret, "Programmer-specific, ");
731 if (bustype == BUS_NONE)
732 ret = strcat_realloc(ret, "None, ");
733 }
734 /* Kill last comma. */
735 ret[strlen(ret) - 2] = '\0';
736 ret = realloc(ret, strlen(ret) + 1);
737 return ret;
738}
739
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000740int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000741{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000742 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000743 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000744 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000745
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000746 for (chip = flashchips + startchip; chip && chip->name; chip++) {
747 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000748 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000749 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000750 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000751 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300752 /* Only probe for SPI25 chips by default. */
753 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
754 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000755 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
756 if (!chip->probe && !force) {
757 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000758 continue;
759 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000760
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000761 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200762 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000763 if (!flash->chip) {
764 msg_gerr("Out of memory!\n");
765 exit(1);
766 }
Angel Pons7e134562021-06-07 13:29:13 +0200767 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000768 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000769
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000770 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +0100771 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000772
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000773 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
774 * is only called with force=1 after normal probing failed.
775 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000776 if (force)
777 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000778
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000779 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000780 goto notfound;
781
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000782 /* If this is the first chip found, accept it.
783 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000784 * a non-generic match. SFDP and CFI are generic matches.
785 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000786 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000787 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000788 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000789 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000790 msg_cinfo("===\n"
791 "SFDP has autodetected a flash chip which is "
792 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000793 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000794 msg_cinfo("The standard operations read and "
795 "verify should work, but to support "
796 "erase, write and all other "
797 "possible features");
798 else
799 msg_cinfo("All standard operations (read, "
800 "verify, erase and write) should "
801 "work, but to support all possible "
802 "features");
803
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000804 msg_cinfo(" we need to add them manually.\n"
805 "You can help us by mailing us the output of the following command to "
806 "flashrom@flashrom.org:\n"
807 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
808 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000809 "===\n");
810 }
811
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000812 /* First flash chip detected on this bus. */
813 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000814 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000815 /* Not the first flash chip detected on this bus, but not a generic match either. */
816 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
817 break;
818 /* 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 +0000819notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000820 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000821 free(flash->chip);
822 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000823 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000824
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000825 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000826 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000827
Nico Huber7af0e792016-04-29 16:40:15 +0200828 /* Fill fallback layout covering the whole chip. */
829 struct single_layout *const fallback = &flash->fallback_layout;
830 fallback->base.entries = &fallback->entry;
831 fallback->base.num_entries = 1;
832 fallback->entry.start = 0;
833 fallback->entry.end = flash->chip->total_size * 1024 - 1;
834 fallback->entry.included = true;
Nico Huber70461a92019-06-15 14:56:19 +0200835 fallback->entry.name = strdup("complete flash");
836 if (!fallback->entry.name) {
837 msg_cerr("Failed to probe chip: %s\n", strerror(errno));
838 return -1;
839 }
Stefan Reinauer051e2362011-01-19 06:21:54 +0000840
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000841 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000842 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
843 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000844 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000845#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200846 if (programmer->map_flash_region == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000847 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
848 PRIxPTR_WIDTH, flash->physical_memory);
849 else
850#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200851 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000852
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000853 /* Flash registers may more likely not be mapped if the chip was forced.
854 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000855 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000856 if (flash->chip->printlock)
857 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000858
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000859 /* Get out of the way for later runs. */
860 unmap_flash(flash);
861
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000862 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000863 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000864}
865
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000866int read_buf_from_file(unsigned char *buf, unsigned long size,
867 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000868{
Nico Huber7562f7d2013-08-30 21:29:45 +0000869#ifdef __LIBPAYLOAD__
870 msg_gerr("Error: No file I/O support in libpayload\n");
871 return 1;
872#else
Stefan Tauner16687702015-12-25 21:59:45 +0000873 int ret = 0;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000874
Stefan Tauner16687702015-12-25 21:59:45 +0000875 FILE *image;
Daniel Campello8eaef7d2021-04-15 10:36:04 -0600876 if (!strcmp(filename, "-"))
877 image = fdopen(fileno(stdin), "rb");
878 else
879 image = fopen(filename, "rb");
880 if (image == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000881 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000882 return 1;
883 }
Stefan Tauner16687702015-12-25 21:59:45 +0000884
885 struct stat image_stat;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000886 if (fstat(fileno(image), &image_stat) != 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000887 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
Stefan Tauner16687702015-12-25 21:59:45 +0000888 ret = 1;
889 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000890 }
Daniel Campello8eaef7d2021-04-15 10:36:04 -0600891 if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +0000892 msg_gerr("Error: Image size (%jd B) doesn't match the flash chip's size (%lu B)!\n",
Stefan Taunere038e902013-02-04 04:38:42 +0000893 (intmax_t)image_stat.st_size, size);
Stefan Tauner16687702015-12-25 21:59:45 +0000894 ret = 1;
895 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000896 }
Stefan Tauner16687702015-12-25 21:59:45 +0000897
898 unsigned long numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000899 if (numbytes != size) {
900 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
901 "wanted %ld!\n", numbytes, size);
Stefan Tauner16687702015-12-25 21:59:45 +0000902 ret = 1;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000903 }
Stefan Tauner16687702015-12-25 21:59:45 +0000904out:
905 (void)fclose(image);
906 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +0000907#endif
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000908}
909
Mark Marshallf20b7be2014-05-09 21:16:21 +0000910int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000911{
Nico Huber7562f7d2013-08-30 21:29:45 +0000912#ifdef __LIBPAYLOAD__
913 msg_gerr("Error: No file I/O support in libpayload\n");
914 return 1;
915#else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000916 FILE *image;
Stefan Tauner16687702015-12-25 21:59:45 +0000917 int ret = 0;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000918
919 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000920 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000921 return 1;
922 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +0000923 if ((image = fopen(filename, "wb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000924 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000925 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000926 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000927
Stefan Tauner16687702015-12-25 21:59:45 +0000928 unsigned long numbytes = fwrite(buf, 1, size, image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000929 if (numbytes != size) {
Stefan Tauner16687702015-12-25 21:59:45 +0000930 msg_gerr("Error: file %s could not be written completely.\n", filename);
931 ret = 1;
932 goto out;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000933 }
Stefan Tauner16687702015-12-25 21:59:45 +0000934 if (fflush(image)) {
935 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
936 ret = 1;
937 }
938 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
939#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
940 struct stat image_stat;
941 if (fstat(fileno(image), &image_stat) != 0) {
942 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
943 ret = 1;
944 goto out;
945 }
946 if (S_ISREG(image_stat.st_mode)) {
947 if (fsync(fileno(image))) {
948 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
949 ret = 1;
950 }
951 }
952#endif
953out:
954 if (fclose(image)) {
955 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
956 ret = 1;
957 }
958 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +0000959#endif
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000960}
961
Nico Huber899e4ec2016-04-29 18:39:01 +0200962static int read_by_layout(struct flashctx *, uint8_t *);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000963int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000964{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000965 unsigned long size = flash->chip->total_size * 1024;
Richard Hughes84b453e2018-12-19 15:30:39 +0000966 unsigned char *buf = calloc(size, sizeof(unsigned char));
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000967 int ret = 0;
968
969 msg_cinfo("Reading flash... ");
970 if (!buf) {
971 msg_gerr("Memory allocation failed!\n");
972 msg_cinfo("FAILED.\n");
973 return 1;
974 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000975 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000976 msg_cerr("No read function available for this flash chip.\n");
977 ret = 1;
978 goto out_free;
979 }
Nico Huber899e4ec2016-04-29 18:39:01 +0200980 if (read_by_layout(flash, buf)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000981 msg_cerr("Read operation failed!\n");
982 ret = 1;
983 goto out_free;
984 }
985
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000986 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000987out_free:
988 free(buf);
989 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
990 return ret;
991}
992
Stefan Tauner96658be2014-05-26 22:05:31 +0000993/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000994static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000995{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000996 int i, j, k;
997 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530998 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000999
1000 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1001 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001002 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +05301003 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001004
1005 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1006 /* Blocks with zero size are bugs in flashchips.c. */
1007 if (eraser.eraseblocks[i].count &&
1008 !eraser.eraseblocks[i].size) {
1009 msg_gerr("ERROR: Flash chip %s erase function "
1010 "%i region %i has size 0. Please report"
1011 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001012 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001013 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001014 }
1015 /* Blocks with zero count are bugs in flashchips.c. */
1016 if (!eraser.eraseblocks[i].count &&
1017 eraser.eraseblocks[i].size) {
1018 msg_gerr("ERROR: Flash chip %s erase function "
1019 "%i region %i has count 0. Please report"
1020 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001021 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001022 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001023 }
1024 done += eraser.eraseblocks[i].count *
1025 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +05301026 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001027 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001028 /* Empty eraseblock definition with erase function. */
1029 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001030 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001031 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001032 if (!done)
1033 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001034 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001035 msg_gerr("ERROR: Flash chip %s erase function %i "
1036 "region walking resulted in 0x%06x bytes total,"
1037 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001038 " flashrom@flashrom.org\n", chip->name, k,
1039 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001040 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001041 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001042 if (!eraser.block_erase)
1043 continue;
1044 /* Check if there are identical erase functions for different
1045 * layouts. That would imply "magic" erase functions. The
1046 * easiest way to check this is with function pointers.
1047 */
Uwe Hermann43959702010-03-13 17:28:29 +00001048 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001049 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001050 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001051 msg_gerr("ERROR: Flash chip %s erase function "
1052 "%i and %i are identical. Please report"
1053 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001054 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001055 ret = 1;
1056 }
Uwe Hermann43959702010-03-13 17:28:29 +00001057 }
Aarya Chaumal478e1792022-06-04 01:34:44 +05301058 if(curr_eraseblock_count > prev_eraseblock_count)
1059 {
1060 msg_gerr("ERROR: Flash chip %s erase function %i is not "
1061 "in order. Please report a bug at flashrom@flashrom.org\n",
1062 chip->name, k);
1063 ret = 1;
1064 }
1065 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001066 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001067 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001068}
1069
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001070static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001071{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001072 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001073
1074 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1075 if (log)
1076 msg_cdbg("not defined. ");
1077 return 1;
1078 }
1079 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1080 if (log)
1081 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001082 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001083 return 1;
1084 }
1085 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1086 if (log)
1087 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001088 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001089 return 1;
1090 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001091 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001092 return 0;
1093}
1094
Nico Huber7af0e792016-04-29 16:40:15 +02001095/**
1096 * @brief Reads the included layout regions into a buffer.
1097 *
1098 * If there is no layout set in the given flash context, the whole chip will
1099 * be read.
1100 *
1101 * @param flashctx Flash context to be used.
1102 * @param buffer Buffer of full chip size to read into.
1103 * @return 0 on success,
1104 * 1 if any read fails.
1105 */
1106static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1107{
1108 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001109 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001110
Nico Huber5ca55232019-06-15 22:29:08 +02001111 while ((entry = layout_next_included(layout, entry))) {
1112 const chipoff_t region_start = entry->start;
1113 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001114
1115 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1116 return 1;
1117 }
1118 return 0;
1119}
1120
1121typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1122/**
1123 * @private
1124 *
1125 * For read-erase-write, `curcontents` and `newcontents` shall point
1126 * to buffers of the chip's size. Both are supposed to be prefilled
1127 * with at least the included layout regions of the current flash
1128 * contents (`curcontents`) and the data to be written to the flash
1129 * (`newcontents`).
1130 *
1131 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1132 *
1133 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1134 */
1135struct walk_info {
1136 uint8_t *curcontents;
1137 const uint8_t *newcontents;
1138 chipoff_t region_start;
1139 chipoff_t region_end;
1140 chipoff_t erase_start;
1141 chipoff_t erase_end;
1142};
1143/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1144typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1145
1146static int walk_eraseblocks(struct flashctx *const flashctx,
1147 struct walk_info *const info,
1148 const size_t erasefunction, const per_blockfn_t per_blockfn)
1149{
1150 int ret;
1151 size_t i, j;
1152 bool first = true;
1153 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1154
1155 info->erase_start = 0;
1156 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1157 /* count==0 for all automatically initialized array
1158 members so the loop below won't be executed for them. */
1159 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1160 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1161
1162 /* Skip any eraseblock that is completely outside the current region. */
1163 if (info->erase_end < info->region_start)
1164 continue;
1165 if (info->region_end < info->erase_start)
1166 break;
1167
1168 /* Print this for every block except the first one. */
1169 if (first)
1170 first = false;
1171 else
1172 msg_cdbg(", ");
1173 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1174
1175 ret = per_blockfn(flashctx, info, eraser->block_erase);
1176 if (ret)
1177 return ret;
1178 }
1179 if (info->region_end < info->erase_start)
1180 break;
1181 }
1182 msg_cdbg("\n");
1183 return 0;
1184}
1185
1186static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1187 const per_blockfn_t per_blockfn)
1188{
1189 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001190 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001191
1192 all_skipped = true;
1193 msg_cinfo("Erasing and writing flash chip... ");
1194
Nico Huber5ca55232019-06-15 22:29:08 +02001195 while ((entry = layout_next_included(layout, entry))) {
1196 info->region_start = entry->start;
1197 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001198
1199 size_t j;
1200 int error = 1; /* retry as long as it's 1 */
1201 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1202 if (j != 0)
1203 msg_cinfo("Looking for another erase function.\n");
1204 msg_cdbg("Trying erase function %zi... ", j);
1205 if (check_block_eraser(flashctx, j, 1))
1206 continue;
1207
1208 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1209 if (error != 1)
1210 break;
1211
1212 if (info->curcontents) {
1213 msg_cinfo("Reading current flash chip contents... ");
1214 if (read_by_layout(flashctx, info->curcontents)) {
1215 /* Now we are truly screwed. Read failed as well. */
1216 msg_cerr("Can't read anymore! Aborting.\n");
1217 /* We have no idea about the flash chip contents, so
1218 retrying with another erase function is pointless. */
1219 error = 2;
1220 break;
1221 }
1222 msg_cinfo("done. ");
1223 }
1224 }
1225 if (error == 1)
1226 msg_cinfo("No usable erase functions left.\n");
1227 if (error) {
1228 msg_cerr("FAILED!\n");
1229 return 1;
1230 }
1231 }
1232 if (all_skipped)
1233 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1234 msg_cinfo("Erase/write done.\n");
1235 return 0;
1236}
1237
1238static int erase_block(struct flashctx *const flashctx,
1239 const struct walk_info *const info, const erasefn_t erasefn)
1240{
1241 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001242 const bool region_unaligned = info->region_start > info->erase_start ||
1243 info->erase_end > info->region_end;
1244 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1245 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001246
Nico Huber6e61e0c2019-01-23 17:07:49 +01001247 /*
1248 * If the region is not erase-block aligned, merge current flash con-
1249 * tents into a new buffer `backup_contents`.
1250 */
1251 if (region_unaligned) {
1252 backup_contents = malloc(erase_len);
1253 erased_contents = malloc(erase_len);
1254 if (!backup_contents || !erased_contents) {
1255 msg_cerr("Out of memory!\n");
1256 ret = 1;
1257 goto _free_ret;
1258 }
1259 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1260 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1261
1262 msg_cdbg("R");
1263 /* Merge data preceding the current region. */
1264 if (info->region_start > info->erase_start) {
1265 const chipoff_t start = info->erase_start;
1266 const chipsize_t len = info->region_start - info->erase_start;
1267 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1268 msg_cerr("Can't read! Aborting.\n");
1269 goto _free_ret;
1270 }
1271 }
1272 /* Merge data following the current region. */
1273 if (info->erase_end > info->region_end) {
1274 const chipoff_t start = info->region_end + 1;
1275 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1276 const chipsize_t len = info->erase_end - info->region_end;
1277 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1278 msg_cerr("Can't read! Aborting.\n");
1279 goto _free_ret;
1280 }
1281 }
1282 }
1283
1284 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001285 all_skipped = false;
1286
1287 msg_cdbg("E");
1288 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001289 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001290 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1291 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001292 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001293 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001294
1295 if (region_unaligned) {
1296 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1297 /* get_next_write() sets starthere to a new value after the call. */
1298 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1299 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1300 if (!writecount++)
1301 msg_cdbg("W");
1302 /* Needs the partial write function signature. */
1303 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1304 info->erase_start + starthere, lenhere))
1305 goto _free_ret;
1306 starthere += lenhere;
1307 }
1308 }
1309
1310 ret = 0;
1311
1312_free_ret:
1313 free(erased_contents);
1314 free(backup_contents);
1315 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001316}
1317
1318/**
1319 * @brief Erases the included layout regions.
1320 *
1321 * If there is no layout set in the given flash context, the whole chip will
1322 * be erased.
1323 *
1324 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001325 * @return 0 on success,
1326 * 1 if all available erase functions failed.
1327 */
Nico Huber454f6132012-12-10 13:34:10 +00001328static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001329{
1330 struct walk_info info = { 0 };
1331 return walk_by_layout(flashctx, &info, &erase_block);
1332}
1333
1334static int read_erase_write_block(struct flashctx *const flashctx,
1335 const struct walk_info *const info, const erasefn_t erasefn)
1336{
1337 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1338 const bool region_unaligned = info->region_start > info->erase_start ||
1339 info->erase_end > info->region_end;
1340 const uint8_t *newcontents = NULL;
1341 int ret = 2;
1342
1343 /*
1344 * If the region is not erase-block aligned, merge current flash con-
1345 * tents into `info->curcontents` and a new buffer `newc`. The former
1346 * is necessary since we have no guarantee that the full erase block
1347 * was already read into `info->curcontents`. For the latter a new
1348 * buffer is used since `info->newcontents` might contain data for
1349 * other unaligned regions that touch this erase block too.
1350 */
1351 if (region_unaligned) {
1352 msg_cdbg("R");
1353 uint8_t *const newc = malloc(erase_len);
1354 if (!newc) {
1355 msg_cerr("Out of memory!\n");
1356 return 1;
1357 }
1358 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1359
1360 /* Merge data preceding the current region. */
1361 if (info->region_start > info->erase_start) {
1362 const chipoff_t start = info->erase_start;
1363 const chipsize_t len = info->region_start - info->erase_start;
1364 if (flashctx->chip->read(flashctx, newc, start, len)) {
1365 msg_cerr("Can't read! Aborting.\n");
1366 goto _free_ret;
1367 }
1368 memcpy(info->curcontents + start, newc, len);
1369 }
1370 /* Merge data following the current region. */
1371 if (info->erase_end > info->region_end) {
1372 const chipoff_t start = info->region_end + 1;
1373 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1374 const chipsize_t len = info->erase_end - info->region_end;
1375 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1376 msg_cerr("Can't read! Aborting.\n");
1377 goto _free_ret;
1378 }
1379 memcpy(info->curcontents + start, newc + rel_start, len);
1380 }
1381
1382 newcontents = newc;
1383 } else {
1384 newcontents = info->newcontents + info->erase_start;
1385 }
1386
1387 ret = 1;
1388 bool skipped = true;
1389 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001390 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001391 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1392 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001393 if (erase_block(flashctx, info, erasefn))
1394 goto _free_ret;
1395 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001396 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001397 skipped = false;
1398 }
1399
1400 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1401 /* get_next_write() sets starthere to a new value after the call. */
1402 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1403 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1404 if (!writecount++)
1405 msg_cdbg("W");
1406 /* Needs the partial write function signature. */
1407 if (flashctx->chip->write(flashctx, newcontents + starthere,
1408 info->erase_start + starthere, lenhere))
1409 goto _free_ret;
1410 starthere += lenhere;
1411 skipped = false;
1412 }
1413 if (skipped)
1414 msg_cdbg("S");
1415 else
1416 all_skipped = false;
1417
1418 /* Update curcontents, other regions with overlapping erase blocks
1419 might rely on this. */
1420 memcpy(curcontents, newcontents, erase_len);
1421 ret = 0;
1422
1423_free_ret:
1424 if (region_unaligned)
1425 free((void *)newcontents);
1426 return ret;
1427}
1428
1429/**
1430 * @brief Writes the included layout regions from a given image.
1431 *
1432 * If there is no layout set in the given flash context, the whole image
1433 * will be written.
1434 *
1435 * @param flashctx Flash context to be used.
1436 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1437 * @param newcontents The new image to be written.
1438 * @return 0 on success,
1439 * 1 if anything has gone wrong.
1440 */
Nico Huber454f6132012-12-10 13:34:10 +00001441static int write_by_layout(struct flashctx *const flashctx,
1442 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001443{
1444 struct walk_info info;
1445 info.curcontents = curcontents;
1446 info.newcontents = newcontents;
1447 return walk_by_layout(flashctx, &info, read_erase_write_block);
1448}
1449
1450/**
1451 * @brief Compares the included layout regions with content from a buffer.
1452 *
1453 * If there is no layout set in the given flash context, the whole chip's
1454 * contents will be compared.
1455 *
1456 * @param flashctx Flash context to be used.
1457 * @param curcontents A buffer of full chip size to read current chip contents into.
1458 * @param newcontents The new image to compare to.
1459 * @return 0 on success,
1460 * 1 if reading failed,
1461 * 3 if the contents don't match.
1462 */
Nico Huber454f6132012-12-10 13:34:10 +00001463static int verify_by_layout(struct flashctx *const flashctx,
1464 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001465{
1466 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001467 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001468
Nico Huber5ca55232019-06-15 22:29:08 +02001469 while ((entry = layout_next_included(layout, entry))) {
1470 const chipoff_t region_start = entry->start;
1471 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001472
1473 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1474 return 1;
1475 if (compare_range(newcontents + region_start, curcontents + region_start,
1476 region_start, region_len))
1477 return 3;
1478 }
1479 return 0;
1480}
1481
Stefan Tauner136388f2013-07-15 10:47:53 +00001482static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001483{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001484 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001485#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001486 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001487 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Angel Pons1900e1d2021-07-02 12:42:23 +02001488 "chip. Please report this to the mailing list at flashrom@flashrom.org or on\n"
1489 "IRC (see https://www.flashrom.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001490 "-------------------------------------------------------------------------------\n"
1491 "You may now reboot or simply leave the machine running.\n");
1492 else
1493#endif
1494 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1495 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
Angel Pons1900e1d2021-07-02 12:42:23 +02001496 "please report this to the mailing list at flashrom@flashrom.org or on IRC (see\n"
1497 "https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001498}
1499
Stefan Tauner136388f2013-07-15 10:47:53 +00001500static void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001501{
Stefan Tauner136388f2013-07-15 10:47:53 +00001502 msg_gerr("Your flash chip is in an unknown state.\n");
1503#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001504 if (programmer == &programmer_internal)
Angel Pons1900e1d2021-07-02 12:42:23 +02001505 msg_gerr("Get help on IRC (see https://www.flashrom.org/Contact) or mail\n"
1506 "flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!"
Stefan Tauner136388f2013-07-15 10:47:53 +00001507 "-------------------------------------------------------------------------------\n"
1508 "DO NOT REBOOT OR POWEROFF!\n");
1509 else
1510#endif
Angel Pons1900e1d2021-07-02 12:42:23 +02001511 msg_gerr("Please report this to the mailing list at flashrom@flashrom.org or\n"
1512 "on IRC (see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001513}
1514
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001515void list_programmers_linebreak(int startcol, int cols, int paren)
1516{
1517 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001518 int pnamelen;
1519 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001520 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001521 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001522
Thomas Heijligend45cb592021-05-19 14:12:18 +02001523 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001524 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001525 pnamelen = strlen(pname);
1526 if (remaining - pnamelen - 2 < 0) {
1527 if (firstline)
1528 firstline = 0;
1529 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001530 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001531 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001532 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001533 remaining = cols - startcol;
1534 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001535 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001536 remaining--;
1537 }
1538 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001539 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001540 remaining--;
1541 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001542 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001543 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001544 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001545 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001546 remaining--;
1547 } else {
1548 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001549 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001550 }
1551 }
1552}
1553
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001554static void print_sysinfo(void)
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001555{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001556#if IS_WINDOWS
Angel Pons7e134562021-06-07 13:29:13 +02001557 SYSTEM_INFO si = { 0 };
1558 OSVERSIONINFOEX osvi = { 0 };
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001559
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001560 msg_ginfo(" on Windows");
1561 /* Tell Windows which version of the structure we want. */
1562 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1563 if (GetVersionEx((OSVERSIONINFO*) &osvi))
1564 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
1565 else
1566 msg_ginfo(" unknown version");
1567 GetSystemInfo(&si);
1568 switch (si.wProcessorArchitecture) {
1569 case PROCESSOR_ARCHITECTURE_AMD64:
1570 msg_ginfo(" (x86_64)");
1571 break;
1572 case PROCESSOR_ARCHITECTURE_INTEL:
1573 msg_ginfo(" (x86)");
1574 break;
1575 default:
1576 msg_ginfo(" (unknown arch)");
1577 break;
1578 }
1579#elif HAVE_UTSNAME == 1
1580 struct utsname osinfo;
1581
1582 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001583 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1584 osinfo.machine);
1585#else
1586 msg_ginfo(" on unknown machine");
1587#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001588}
1589
1590void print_buildinfo(void)
1591{
1592 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001593#if NEED_PCI == 1
1594#ifdef PCILIB_VERSION
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001595 msg_gdbg(" libpci %s,", PCILIB_VERSION);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001596#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001597 msg_gdbg(" unknown PCI library,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001598#endif
1599#endif
1600#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001601 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001602#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001603 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001604#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001605 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001606#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001607#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001608 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001609#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001610 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001611#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001612 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001613#endif
1614#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001615 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001616#endif
1617#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001618 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001619#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001620 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001621#else
1622#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001623#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001624 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001625}
1626
Bernhard Walle201bde32008-01-21 15:24:22 +00001627void print_version(void)
1628{
Stefan Tauner76347082016-11-27 17:45:49 +01001629 msg_ginfo("flashrom %s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001630 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001631 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00001632}
1633
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001634void print_banner(void)
1635{
1636 msg_ginfo("flashrom is free software, get the source code at "
Stefan Tauner4c723152016-01-14 22:47:55 +00001637 "https://flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001638 msg_ginfo("\n");
1639}
1640
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001641int selfcheck(void)
1642{
Stefan Tauner96658be2014-05-26 22:05:31 +00001643 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001644 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001645
Thomas Heijligend45cb592021-05-19 14:12:18 +02001646 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001647 const struct programmer_entry *const p = programmer_table[i];
1648 if (p == NULL) {
1649 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1650 ret = 1;
1651 continue;
1652 }
1653 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001654 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1655 ret = 1;
1656 /* This might hide other problems with this programmer, but allows for better error
1657 * messages below without jumping through hoops. */
1658 continue;
1659 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001660 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001661 case USB:
1662 case PCI:
1663 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001664 if (p->devs.note == NULL) {
1665 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001666 break; /* This one has its device list stored separately. */
1667 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001668 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001669 ret = 1;
1670 }
1671 break;
1672 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001673 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001674 ret = 1;
1675 break;
1676 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001677 if (p->init == NULL) {
1678 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001679 ret = 1;
1680 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001681 if (p->delay == NULL) {
1682 msg_gerr("Programmer %s does not have a valid delay function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001683 ret = 1;
1684 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001685 if (p->map_flash_region == NULL) {
1686 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001687 ret = 1;
1688 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001689 if (p->unmap_flash_region == NULL) {
1690 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001691 ret = 1;
1692 }
1693 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001694
1695 /* It would be favorable if we could check for the correct layout (especially termination) of various
1696 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1697 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1698 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1699 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1700 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001701 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001702 msg_gerr("Flashchips table miscompilation!\n");
1703 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001704 } else {
1705 for (i = 0; i < flashchips_size - 1; i++) {
1706 const struct flashchip *chip = &flashchips[i];
1707 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1708 ret = 1;
1709 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
1710 "Please report a bug at flashrom@flashrom.org\n", i,
1711 chip->name == NULL ? "unnamed" : chip->name);
1712 }
1713 if (selfcheck_eraseblocks(chip)) {
1714 ret = 1;
1715 }
1716 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001717 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001718
Stefan Tauner600576b2014-06-12 22:57:36 +00001719#if CONFIG_INTERNAL == 1
1720 ret |= selfcheck_board_enables();
1721#endif
1722
Stefan Tauner96658be2014-05-26 22:05:31 +00001723 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001724 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001725}
1726
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001727/* FIXME: This function signature needs to be improved once prepare_flash_access()
1728 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001729 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001730static int chip_safety_check(const struct flashctx *flash, int force,
1731 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001732{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001733 const struct flashchip *chip = flash->chip;
1734
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001735 if (!programmer_may_write && (write_it || erase_it)) {
1736 msg_perr("Write/erase is not working yet on your programmer in "
1737 "its current configuration.\n");
1738 /* --force is the wrong approach, but it's the best we can do
1739 * until the generic programmer parameter parser is merged.
1740 */
1741 if (!force)
1742 return 1;
1743 msg_cerr("Continuing anyway.\n");
1744 }
1745
1746 if (read_it || erase_it || write_it || verify_it) {
1747 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001748 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001749 msg_cerr("Read is not working on this chip. ");
1750 if (!force)
1751 return 1;
1752 msg_cerr("Continuing anyway.\n");
1753 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001754 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001755 msg_cerr("flashrom has no read function for this "
1756 "flash chip.\n");
1757 return 1;
1758 }
1759 }
1760 if (erase_it || write_it) {
1761 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001762 if (chip->tested.erase == NA) {
1763 msg_cerr("Erase is not possible on this chip.\n");
1764 return 1;
1765 }
1766 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001767 msg_cerr("Erase is not working on this chip. ");
1768 if (!force)
1769 return 1;
1770 msg_cerr("Continuing anyway.\n");
1771 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001772 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001773 msg_cerr("flashrom has no erase function for this "
1774 "flash chip.\n");
1775 return 1;
1776 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001777 }
1778 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001779 if (chip->tested.write == NA) {
1780 msg_cerr("Write is not possible on this chip.\n");
1781 return 1;
1782 }
1783 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001784 msg_cerr("Write is not working on this chip. ");
1785 if (!force)
1786 return 1;
1787 msg_cerr("Continuing anyway.\n");
1788 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001789 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001790 msg_cerr("flashrom has no write function for this "
1791 "flash chip.\n");
1792 return 1;
1793 }
1794 }
1795 return 0;
1796}
1797
Nico Huber305f4172013-06-14 11:55:26 +02001798int prepare_flash_access(struct flashctx *const flash,
1799 const bool read_it, const bool write_it,
1800 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001801{
1802 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1803 msg_cerr("Aborting.\n");
1804 return 1;
1805 }
1806
1807 if (flash->layout == get_global_layout() && normalize_romentries(flash)) {
1808 msg_cerr("Requested regions can not be handled. Aborting.\n");
1809 return 1;
1810 }
1811
1812 if (map_flash(flash) != 0)
1813 return 1;
1814
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001815 /* Initialize chip_restore_fn_count before chip unlock calls. */
1816 flash->chip_restore_fn_count = 0;
1817
Nico Huber454f6132012-12-10 13:34:10 +00001818 /* Given the existence of read locks, we want to unlock for read,
1819 erase and write. */
1820 if (flash->chip->unlock)
1821 flash->chip->unlock(flash);
1822
Nico Huberf43c6542017-10-14 17:47:28 +02001823 flash->address_high_byte = -1;
1824 flash->in_4ba_mode = false;
1825
Nico Huberdc5af542018-12-22 16:54:59 +01001826 /* Be careful about 4BA chips and broken masters */
1827 if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
1828 /* If we can't use native instructions, bail out */
1829 if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
1830 || !spi_master_4ba(flash)) {
1831 msg_cerr("Programmer doesn't support this chip. Aborting.\n");
1832 return 1;
1833 }
1834 }
1835
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001836 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01001837 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01001838 int ret;
1839 if (spi_master_4ba(flash))
1840 ret = spi_enter_4ba(flash);
1841 else
1842 ret = spi_exit_4ba(flash);
1843 if (ret) {
1844 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001845 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02001846 }
Boris Baykov99127182016-06-11 18:29:00 +02001847 }
1848
Nico Huber454f6132012-12-10 13:34:10 +00001849 return 0;
1850}
1851
Nico Huber305f4172013-06-14 11:55:26 +02001852void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001853{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001854 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001855 unmap_flash(flash);
1856}
1857
1858/**
1859 * @addtogroup flashrom-flash
1860 * @{
1861 */
1862
1863/**
1864 * @brief Erase the specified ROM chip.
1865 *
1866 * If a layout is set in the given flash context, only included regions
1867 * will be erased.
1868 *
1869 * @param flashctx The context of the flash chip to erase.
1870 * @return 0 on success.
1871 */
1872int flashrom_flash_erase(struct flashctx *const flashctx)
1873{
1874 if (prepare_flash_access(flashctx, false, false, true, false))
1875 return 1;
1876
1877 const int ret = erase_by_layout(flashctx);
1878
1879 finalize_flash_access(flashctx);
1880
1881 return ret;
1882}
1883
1884/** @} */ /* end flashrom-flash */
1885
1886/**
1887 * @defgroup flashrom-ops Operations
1888 * @{
1889 */
1890
1891/**
1892 * @brief Read the current image from the specified ROM chip.
1893 *
1894 * If a layout is set in the specified flash context, only included regions
1895 * will be read.
1896 *
1897 * @param flashctx The context of the flash chip.
1898 * @param buffer Target buffer to write image to.
1899 * @param buffer_len Size of target buffer in bytes.
1900 * @return 0 on success,
1901 * 2 if buffer_len is too short for the flash chip's contents,
1902 * or 1 on any other failure.
1903 */
1904int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
1905{
1906 const size_t flash_size = flashctx->chip->total_size * 1024;
1907
1908 if (flash_size > buffer_len)
1909 return 2;
1910
1911 if (prepare_flash_access(flashctx, true, false, false, false))
1912 return 1;
1913
1914 msg_cinfo("Reading flash... ");
1915
1916 int ret = 1;
1917 if (read_by_layout(flashctx, buffer)) {
1918 msg_cerr("Read operation failed!\n");
1919 msg_cinfo("FAILED.\n");
1920 goto _finalize_ret;
1921 }
1922 msg_cinfo("done.\n");
1923 ret = 0;
1924
1925_finalize_ret:
1926 finalize_flash_access(flashctx);
1927 return ret;
1928}
1929
1930static void combine_image_by_layout(const struct flashctx *const flashctx,
1931 uint8_t *const newcontents, const uint8_t *const oldcontents)
1932{
1933 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001934 const struct romentry *included;
1935 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001936
Nico Huber3d7b1e32018-12-22 00:53:14 +01001937 while ((included = layout_next_included_region(layout, start))) {
1938 if (included->start > start) {
1939 /* copy everything up to the start of this included region */
1940 memcpy(newcontents + start, oldcontents + start, included->start - start);
1941 }
1942 /* skip this included region */
1943 start = included->end + 1;
1944 if (start == 0)
1945 return;
Nico Huber454f6132012-12-10 13:34:10 +00001946 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001947
1948 /* copy the rest of the chip */
1949 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1950 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001951}
1952
1953/**
1954 * @brief Write the specified image to the ROM chip.
1955 *
1956 * If a layout is set in the specified flash context, only erase blocks
1957 * containing included regions will be touched.
1958 *
1959 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001960 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001961 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001962 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001963 * @return 0 on success,
1964 * 4 if buffer_len doesn't match the size of the flash chip,
1965 * 3 if write was tried but nothing has changed,
1966 * 2 if write failed and flash contents changed,
1967 * or 1 on any other failure.
1968 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001969int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
1970 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001971{
1972 const size_t flash_size = flashctx->chip->total_size * 1024;
1973 const bool verify_all = flashctx->flags.verify_whole_chip;
1974 const bool verify = flashctx->flags.verify_after_write;
1975
1976 if (buffer_len != flash_size)
1977 return 4;
1978
1979 int ret = 1;
1980
1981 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001982 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001983 uint8_t *const curcontents = malloc(flash_size);
1984 uint8_t *oldcontents = NULL;
1985 if (verify_all)
1986 oldcontents = malloc(flash_size);
1987 if (!curcontents || (verify_all && !oldcontents)) {
1988 msg_gerr("Out of memory!\n");
1989 goto _free_ret;
1990 }
1991
1992#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001993 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001994 if (flashctx->flags.force_boardmismatch) {
1995 msg_pinfo("Proceeding anyway because user forced us to.\n");
1996 } else {
1997 msg_perr("Aborting. You can override this with "
1998 "-p internal:boardmismatch=force.\n");
1999 goto _free_ret;
2000 }
2001 }
2002#endif
2003
2004 if (prepare_flash_access(flashctx, false, true, false, verify))
2005 goto _free_ret;
2006
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002007 /* If given, assume flash chip contains same data as `refcontents`. */
2008 if (refcontents) {
2009 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
2010 memcpy(curcontents, refcontents, flash_size);
2011 if (oldcontents)
2012 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00002013 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002014 /*
2015 * Read the whole chip to be able to check whether regions need to be
2016 * erased and to give better diagnostics in case write fails.
2017 * The alternative is to read only the regions which are to be
2018 * preserved, but in that case we might perform unneeded erase which
2019 * takes time as well.
2020 */
2021 msg_cinfo("Reading old flash chip contents... ");
2022 if (verify_all) {
2023 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
2024 msg_cinfo("FAILED.\n");
2025 goto _finalize_ret;
2026 }
2027 memcpy(curcontents, oldcontents, flash_size);
2028 } else {
2029 if (read_by_layout(flashctx, curcontents)) {
2030 msg_cinfo("FAILED.\n");
2031 goto _finalize_ret;
2032 }
Nico Huber454f6132012-12-10 13:34:10 +00002033 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002034 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00002035 }
Nico Huber454f6132012-12-10 13:34:10 +00002036
2037 if (write_by_layout(flashctx, curcontents, newcontents)) {
2038 msg_cerr("Uh oh. Erase/write failed. ");
2039 ret = 2;
2040 if (verify_all) {
2041 msg_cerr("Checking if anything has changed.\n");
2042 msg_cinfo("Reading current flash chip contents... ");
2043 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
2044 msg_cinfo("done.\n");
2045 if (!memcmp(oldcontents, curcontents, flash_size)) {
2046 nonfatal_help_message();
2047 goto _finalize_ret;
2048 }
2049 msg_cerr("Apparently at least some data has changed.\n");
2050 } else
2051 msg_cerr("Can't even read anymore!\n");
2052 emergency_help_message();
2053 goto _finalize_ret;
2054 } else {
2055 msg_cerr("\n");
2056 }
2057 emergency_help_message();
2058 goto _finalize_ret;
2059 }
2060
2061 /* Verify only if we actually changed something. */
2062 if (verify && !all_skipped) {
2063 const struct flashrom_layout *const layout_bak = flashctx->layout;
2064
2065 msg_cinfo("Verifying flash... ");
2066
2067 /* Work around chips which need some time to calm down. */
2068 programmer_delay(1000*1000);
2069
2070 if (verify_all) {
2071 combine_image_by_layout(flashctx, newcontents, oldcontents);
2072 flashctx->layout = NULL;
2073 }
2074 ret = verify_by_layout(flashctx, curcontents, newcontents);
2075 flashctx->layout = layout_bak;
2076 /* If we tried to write, and verification now fails, we
2077 might have an emergency situation. */
2078 if (ret)
2079 emergency_help_message();
2080 else
2081 msg_cinfo("VERIFIED.\n");
2082 } else {
2083 /* We didn't change anything. */
2084 ret = 0;
2085 }
2086
2087_finalize_ret:
2088 finalize_flash_access(flashctx);
2089_free_ret:
2090 free(oldcontents);
2091 free(curcontents);
2092 return ret;
2093}
2094
2095/**
2096 * @brief Verify the ROM chip's contents with the specified image.
2097 *
2098 * If a layout is set in the specified flash context, only included regions
2099 * will be verified.
2100 *
2101 * @param flashctx The context of the flash chip.
2102 * @param buffer Source buffer to verify with.
2103 * @param buffer_len Size of source buffer in bytes.
2104 * @return 0 on success,
2105 * 3 if the chip's contents don't match,
2106 * 2 if buffer_len doesn't match the size of the flash chip,
2107 * or 1 on any other failure.
2108 */
2109int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
2110{
2111 const size_t flash_size = flashctx->chip->total_size * 1024;
2112
2113 if (buffer_len != flash_size)
2114 return 2;
2115
2116 const uint8_t *const newcontents = buffer;
2117 uint8_t *const curcontents = malloc(flash_size);
2118 if (!curcontents) {
2119 msg_gerr("Out of memory!\n");
2120 return 1;
2121 }
2122
2123 int ret = 1;
2124
2125 if (prepare_flash_access(flashctx, false, false, false, true))
2126 goto _free_ret;
2127
2128 msg_cinfo("Verifying flash... ");
2129 ret = verify_by_layout(flashctx, curcontents, newcontents);
2130 if (!ret)
2131 msg_cinfo("VERIFIED.\n");
2132
2133 finalize_flash_access(flashctx);
2134_free_ret:
2135 free(curcontents);
2136 return ret;
2137}
2138
2139/** @} */ /* end flashrom-ops */
Nico Huber899e4ec2016-04-29 18:39:01 +02002140
2141int do_read(struct flashctx *const flash, const char *const filename)
2142{
2143 if (prepare_flash_access(flash, true, false, false, false))
2144 return 1;
2145
2146 const int ret = read_flash_to_file(flash, filename);
2147
2148 finalize_flash_access(flash);
2149
2150 return ret;
2151}
2152
2153int do_erase(struct flashctx *const flash)
2154{
2155 const int ret = flashrom_flash_erase(flash);
2156
2157 /*
2158 * FIXME: Do we really want the scary warning if erase failed?
2159 * After all, after erase the chip is either blank or partially
2160 * blank or it has the old contents. A blank chip won't boot,
2161 * so if the user wanted erase and reboots afterwards, the user
2162 * knows very well that booting won't work.
2163 */
2164 if (ret)
2165 emergency_help_message();
2166
2167 return ret;
2168}
2169
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002170int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
Nico Huber899e4ec2016-04-29 18:39:01 +02002171{
2172 const size_t flash_size = flash->chip->total_size * 1024;
2173 int ret = 1;
2174
2175 uint8_t *const newcontents = malloc(flash_size);
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002176 uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
2177
2178 if (!newcontents || (referencefile && !refcontents)) {
Nico Huber899e4ec2016-04-29 18:39:01 +02002179 msg_gerr("Out of memory!\n");
2180 goto _free_ret;
2181 }
2182
2183 if (read_buf_from_file(newcontents, flash_size, filename))
2184 goto _free_ret;
2185
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002186 if (referencefile) {
2187 if (read_buf_from_file(refcontents, flash_size, referencefile))
2188 goto _free_ret;
2189 }
2190
2191 ret = flashrom_image_write(flash, newcontents, flash_size, refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002192
2193_free_ret:
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002194 free(refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002195 free(newcontents);
2196 return ret;
2197}
2198
2199int do_verify(struct flashctx *const flash, const char *const filename)
2200{
2201 const size_t flash_size = flash->chip->total_size * 1024;
2202 int ret = 1;
2203
2204 uint8_t *const newcontents = malloc(flash_size);
2205 if (!newcontents) {
2206 msg_gerr("Out of memory!\n");
2207 goto _free_ret;
2208 }
2209
2210 if (read_buf_from_file(newcontents, flash_size, filename))
2211 goto _free_ret;
2212
2213 ret = flashrom_image_verify(flash, newcontents, flash_size);
2214
2215_free_ret:
2216 free(newcontents);
2217 return ret;
2218}