blob: 6515b20713fd330a6c0071ea461d3fbea5ca73d8 [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>
Ollie Lho184a4042005-11-26 21:55:36 +000033#include <getopt.h>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000037#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000038#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000039#include "programmer.h"
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +000040#include "hwaccess.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010041#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000042
Mathias Krausea60faab2011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000044const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000045
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020046static const struct programmer_entry *programmer = NULL;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000047static const char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000048
Uwe Hermann48ec1b12010-08-08 17:01:18 +000049/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000050 * Programmers supporting multiple buses can have differing size limits on
51 * each bus. Store the limits for each bus in a common struct.
52 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000053struct decode_sizes max_rom_decode;
54
55/* If nonzero, used as the start address of bottom-aligned flash. */
56unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000057
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000058/* Is writing allowed with this programmer? */
59int programmer_may_write;
60
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000061#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000062static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000063/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000064static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000065 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000066 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000067} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000068/* Initialize to 0 to make sure nobody registers a shutdown function before
69 * programmer init.
70 */
71static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000072
Stefan Taunerc4f44df2013-08-12 22:58:43 +000073/* Did we change something or was every erase/write skipped (if any)? */
74static bool all_skipped = true;
75
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000076static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000077
Stefan Tauner2a1ed772014-08-31 00:09:21 +000078int shutdown_free(void *data)
79{
80 free(data);
81 return 0;
82}
83
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000084/* Register a function to be executed on programmer shutdown.
85 * The advantage over atexit() is that you can supply a void pointer which will
86 * be used as parameter to the registered function upon programmer shutdown.
87 * This pointer can point to arbitrary data used by said function, e.g. undo
88 * information for GPIO settings etc. If unneeded, set data=NULL.
89 * Please note that the first (void *data) belongs to the function signature of
90 * the function passed as first parameter.
91 */
David Hendricks8bb20212011-06-14 01:35:36 +000092int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000093{
94 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000095 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000096 SHUTDOWN_MAXFN);
97 return 1;
98 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000099 if (!may_register_shutdown) {
100 msg_perr("Tried to register a shutdown function before "
101 "programmer init.\n");
102 return 1;
103 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000104 shutdown_fn[shutdown_fn_count].func = function;
105 shutdown_fn[shutdown_fn_count].data = data;
106 shutdown_fn_count++;
107
108 return 0;
109}
110
Nikolai Artemiev4ad48642020-11-05 13:54:27 +1100111int register_chip_restore(chip_restore_fn_cb_t func,
112 struct flashctx *flash, uint8_t status)
113{
114 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
115 msg_perr("Tried to register more than %i chip restore"
116 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
117 return 1;
118 }
119 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
120 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
121 flash->chip_restore_fn_count++;
122
123 return 0;
124}
125
126static int deregister_chip_restore(struct flashctx *flash)
127{
128 int rc = 0;
129
130 while (flash->chip_restore_fn_count > 0) {
131 int i = --flash->chip_restore_fn_count;
132 rc |= flash->chip_restore_fn[i].func(
133 flash, flash->chip_restore_fn[i].status);
134 }
135
136 return rc;
137}
138
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200139int programmer_init(const struct programmer_entry *prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000140{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000141 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000142
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200143 if (prog == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000144 msg_perr("Invalid programmer specified!\n");
145 return -1;
146 }
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200147 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000148 /* Initialize all programmer specific data. */
149 /* Default to unlimited decode sizes. */
150 max_rom_decode = (const struct decode_sizes) {
151 .parallel = 0xffffffff,
152 .lpc = 0xffffffff,
153 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000154 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000155 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000156 /* Default to top aligned flash at 4 GB. */
157 flashbase = 0;
158 /* Registering shutdown functions is now allowed. */
159 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000160 /* Default to allowing writes. Broken programmers set this to 0. */
161 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000162
163 programmer_param = param;
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200164 msg_pdbg("Initializing %s programmer\n", programmer->name);
165 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000166 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000167 if (ret != 0) {
168 /* It is quite possible that any unhandled programmer parameter would have been valid,
169 * but an error in actual programmer init happened before the parameter was evaluated.
170 */
171 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
172 programmer_param);
173 } else {
174 /* Actual programmer init was successful, but the user specified an invalid or unusable
175 * (for the current programmer configuration) parameter.
176 */
177 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
178 msg_perr("Aborting.\n");
179 ret = ERROR_FATAL;
180 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000181 }
182 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000183}
184
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000185/** Calls registered shutdown functions and resets internal programmer-related variables.
186 * Calling it is safe even without previous initialization, but further interactions with programmer support
187 * require a call to programmer_init() (afterwards).
188 *
189 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000190int programmer_shutdown(void)
191{
David Hendricks8bb20212011-06-14 01:35:36 +0000192 int ret = 0;
193
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000194 /* Registering shutdown functions is no longer allowed. */
195 may_register_shutdown = 0;
196 while (shutdown_fn_count > 0) {
197 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000198 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000199 }
Stefan Taunere34e3e82013-01-01 00:06:51 +0000200
Stefan Taunerb8911d62012-12-26 07:55:00 +0000201 programmer_param = NULL;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000202 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000203
David Hendricks8bb20212011-06-14 01:35:36 +0000204 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000205}
206
Stefan Tauner305e0b92013-07-17 23:46:44 +0000207void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000208{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200209 void *ret = programmer->map_flash_region(descr, phys_addr, len);
Stefan Tauner26e7a152013-09-13 17:21:05 +0000210 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
211 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
212 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000213}
214
215void programmer_unmap_flash_region(void *virt_addr, size_t len)
216{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200217 programmer->unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000218 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000219}
220
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000221void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000222{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000223 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000224}
225
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000226void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000227{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000228 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000229}
230
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000231void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000232{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000233 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000234}
235
Mark Marshallf20b7be2014-05-09 21:16:21 +0000236void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000237{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000238 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000239}
240
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000241uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000242{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000243 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000244}
245
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000246uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000247{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000248 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000249}
250
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000251uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000252{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000253 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000254}
255
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000256void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
257 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000258{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000259 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000260}
261
Stefan Taunerf80419c2014-05-02 15:41:42 +0000262void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000263{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000264 if (usecs > 0)
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200265 programmer->delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000266}
267
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000268int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
269 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000270{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000271 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000272
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000273 return 0;
274}
275
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000276/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000277 * It will look for needle with a subsequent '=' in haystack, return a copy of
278 * needle and remove everything from the first occurrence of needle to the next
279 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000280 */
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000281char *extract_param(const char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000282{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000283 char *param_pos, *opt_pos, *rest;
284 char *opt = NULL;
285 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000286 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000287
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000288 needlelen = strlen(needle);
289 if (!needlelen) {
290 msg_gerr("%s: empty needle! Please report a bug at "
291 "flashrom@flashrom.org\n", __func__);
292 return NULL;
293 }
294 /* No programmer parameters given. */
295 if (*haystack == NULL)
296 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000297 param_pos = strstr(*haystack, needle);
298 do {
299 if (!param_pos)
300 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000301 /* Needle followed by '='? */
302 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000303 /* Beginning of the string? */
304 if (param_pos == *haystack)
305 break;
306 /* After a delimiter? */
307 if (strchr(delim, *(param_pos - 1)))
308 break;
309 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000310 /* Continue searching. */
311 param_pos++;
312 param_pos = strstr(param_pos, needle);
313 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000314
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000315 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000316 /* Get the string after needle and '='. */
317 opt_pos = param_pos + needlelen + 1;
318 optlen = strcspn(opt_pos, delim);
319 /* Return an empty string if the parameter was empty. */
320 opt = malloc(optlen + 1);
321 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000322 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000323 exit(1);
324 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000325 strncpy(opt, opt_pos, optlen);
326 opt[optlen] = '\0';
327 rest = opt_pos + optlen;
328 /* Skip all delimiters after the current parameter. */
329 rest += strspn(rest, delim);
330 memmove(param_pos, rest, strlen(rest) + 1);
331 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000332 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000333
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000334 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000335}
336
Stefan Tauner66652442011-06-26 17:38:17 +0000337char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000338{
339 return extract_param(&programmer_param, param_name, ",");
340}
341
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000342/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000343static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000344{
345 unsigned int usable_erasefunctions = 0;
346 int k;
347 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
348 if (!check_block_eraser(flash, k, 0))
349 usable_erasefunctions++;
350 }
351 return usable_erasefunctions;
352}
353
Mark Marshallf20b7be2014-05-09 21:16:21 +0000354static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000355{
356 int ret = 0, failcount = 0;
357 unsigned int i;
358 for (i = 0; i < len; i++) {
359 if (wantbuf[i] != havebuf[i]) {
360 /* Only print the first failure. */
361 if (!failcount++)
362 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
363 start + i, wantbuf[i], havebuf[i]);
364 }
365 }
366 if (failcount) {
367 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
368 start, start + len - 1, failcount);
369 ret = -1;
370 }
371 return ret;
372}
373
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000374/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600375static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000376{
377 int ret;
378 uint8_t *cmpbuf = malloc(len);
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300379 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000380
381 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000382 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000383 exit(1);
384 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300385 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000386 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000387 free(cmpbuf);
388 return ret;
389}
390
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000391/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000392 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000393 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000394 * @start offset to the base address of the flash chip
395 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000396 * @return 0 for success, -1 for failure
397 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000398int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000399{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000400 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000401 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000402
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000403 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000404 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000405 return -1;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000406 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000407
408 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000409 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000410 msg_gerr("Could not allocate memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000411 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000412 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000413 int ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000414
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000415 if (start + len > flash->chip->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000416 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000417 " total_size 0x%x\n", __func__, start, len,
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000418 flash->chip->total_size * 1024);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000419 ret = -1;
420 goto out_free;
421 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000422
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000423 ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000424 if (ret) {
425 msg_gerr("Verification impossible because read failed "
426 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000427 ret = -1;
428 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000429 }
430
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000431 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000432out_free:
433 free(readbuf);
434 return ret;
435}
436
Stefan Tauner02437452013-04-01 19:34:53 +0000437/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300438static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
439 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000440{
441 unsigned int i, j, limit;
442 for (j = 0; j < len / gran; j++) {
443 limit = min (gran, len - j * gran);
444 /* Are 'have' and 'want' identical? */
445 if (!memcmp(have + j * gran, want + j * gran, limit))
446 continue;
447 /* have needs to be in erased state. */
448 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300449 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000450 return 1;
451 }
452 return 0;
453}
454
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000455/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000456 * Check if the buffer @have can be programmed to the content of @want without
457 * erasing. This is only possible if all chunks of size @gran are either kept
458 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000459 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000460 * Warning: This function assumes that @have and @want point to naturally
461 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000462 *
463 * @have buffer with current content
464 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000465 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000466 * @gran write granularity (enum, not count)
467 * @return 0 if no erase is needed, 1 otherwise
468 */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300469int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
470 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000471{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000472 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000473 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000474
475 switch (gran) {
476 case write_gran_1bit:
477 for (i = 0; i < len; i++)
478 if ((have[i] & want[i]) != want[i]) {
479 result = 1;
480 break;
481 }
482 break;
483 case write_gran_1byte:
484 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300485 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000486 result = 1;
487 break;
488 }
489 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000490 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300491 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000492 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000493 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300494 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000495 break;
496 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300497 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000498 break;
499 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300500 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000501 break;
502 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300503 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000504 break;
505 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300506 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000507 break;
508 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300509 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000510 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000511 case write_gran_1byte_implicit_erase:
512 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
513 result = 0;
514 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000515 default:
516 msg_cerr("%s: Unsupported granularity! Please report a bug at "
517 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000518 }
519 return result;
520}
521
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000522/**
523 * Check if the buffer @have needs to be programmed to get the content of @want.
524 * If yes, return 1 and fill in first_start with the start address of the
525 * write operation and first_len with the length of the first to-be-written
526 * chunk. If not, return 0 and leave first_start and first_len undefined.
527 *
528 * Warning: This function assumes that @have and @want point to naturally
529 * aligned regions.
530 *
531 * @have buffer with current content
532 * @want buffer with desired content
533 * @len length of the checked area
534 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000535 * @first_start offset of the first byte which needs to be written (passed in
536 * value is increased by the offset of the first needed write
537 * relative to have/want or unchanged if no write is needed)
538 * @return length of the first contiguous area which needs to be written
539 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000540 *
541 * FIXME: This function needs a parameter which tells it about coalescing
542 * in relation to the max write length of the programmer and the max write
543 * length of the chip.
544 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000545static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000546 unsigned int *first_start,
547 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000548{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000549 int need_write = 0;
550 unsigned int rel_start = 0, first_len = 0;
551 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000552
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000553 switch (gran) {
554 case write_gran_1bit:
555 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000556 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000557 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000558 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000559 case write_gran_128bytes:
560 stride = 128;
561 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000562 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000563 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000564 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000565 case write_gran_264bytes:
566 stride = 264;
567 break;
568 case write_gran_512bytes:
569 stride = 512;
570 break;
571 case write_gran_528bytes:
572 stride = 528;
573 break;
574 case write_gran_1024bytes:
575 stride = 1024;
576 break;
577 case write_gran_1056bytes:
578 stride = 1056;
579 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000580 default:
581 msg_cerr("%s: Unsupported granularity! Please report a bug at "
582 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000583 /* Claim that no write was needed. A write with unknown
584 * granularity is too dangerous to try.
585 */
586 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000587 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000588 for (i = 0; i < len / stride; i++) {
589 limit = min(stride, len - i * stride);
590 /* Are 'have' and 'want' identical? */
591 if (memcmp(have + i * stride, want + i * stride, limit)) {
592 if (!need_write) {
593 /* First location where have and want differ. */
594 need_write = 1;
595 rel_start = i * stride;
596 }
597 } else {
598 if (need_write) {
599 /* First location where have and want
600 * do not differ anymore.
601 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000602 break;
603 }
604 }
605 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000606 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000607 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000608 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000609 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000610}
611
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000612/* Returns the number of busses commonly supported by the current programmer and flash chip where the latter
613 * can not be completely accessed due to size/address limits of the programmer. */
614unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000615{
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000616 unsigned int limitexceeded = 0;
617 uint32_t size = flash->chip->total_size * 1024;
618 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000619
620 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000621 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000622 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000623 "size %u kB of chipset/board/programmer "
624 "for %s interface, "
625 "probe/read/erase/write may fail. ", size / 1024,
626 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000627 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000628 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000629 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000630 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000631 "size %u kB of chipset/board/programmer "
632 "for %s interface, "
633 "probe/read/erase/write may fail. ", size / 1024,
634 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000635 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000636 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000637 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000638 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000639 "size %u kB of chipset/board/programmer "
640 "for %s interface, "
641 "probe/read/erase/write may fail. ", size / 1024,
642 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000643 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000644 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000645 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000646 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000647 "size %u kB of chipset/board/programmer "
648 "for %s interface, "
649 "probe/read/erase/write may fail. ", size / 1024,
650 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000651 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000652 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000653}
654
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000655void unmap_flash(struct flashctx *flash)
656{
657 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
658 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
659 flash->physical_registers = 0;
660 flash->virtual_registers = (chipaddr)ERROR_PTR;
661 }
662
663 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
664 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
665 flash->physical_memory = 0;
666 flash->virtual_memory = (chipaddr)ERROR_PTR;
667 }
668}
669
670int map_flash(struct flashctx *flash)
671{
672 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
673 flash->virtual_memory = (chipaddr)ERROR_PTR;
674 flash->virtual_registers = (chipaddr)ERROR_PTR;
675
676 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
677 * These are used for various probing-related hacks that would not map successfully anyway and should be
678 * removed ASAP. */
679 if (flash->chip->total_size == 0)
680 return 0;
681
682 const chipsize_t size = flash->chip->total_size * 1024;
683 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
684 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
685 if (addr == ERROR_PTR) {
686 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
687 flash->chip->name, PRIxPTR_WIDTH, base);
688 return 1;
689 }
690 flash->physical_memory = base;
691 flash->virtual_memory = (chipaddr)addr;
692
693 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
694 * completely different on some chips and programmers, or not mappable at all.
695 * Ignore these problems for now and always report success. */
696 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
697 base = 0xffffffff - size - 0x400000 + 1;
698 addr = programmer_map_flash_region("flash chip registers", base, size);
699 if (addr == ERROR_PTR) {
700 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
701 flash->chip->name, PRIxPTR_WIDTH, base);
702 return 0;
703 }
704 flash->physical_registers = base;
705 flash->virtual_registers = (chipaddr)addr;
706 }
707 return 0;
708}
709
Nico Huber2d625722016-05-03 10:48:02 +0200710/*
711 * Return a string corresponding to the bustype parameter.
712 * Memory is obtained with malloc() and must be freed with free() by the caller.
713 */
714char *flashbuses_to_text(enum chipbustype bustype)
715{
716 char *ret = calloc(1, 1);
717 /*
718 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
719 * will cease to exist and should be eliminated here as well.
720 */
721 if (bustype == BUS_NONSPI) {
722 ret = strcat_realloc(ret, "Non-SPI, ");
723 } else {
724 if (bustype & BUS_PARALLEL)
725 ret = strcat_realloc(ret, "Parallel, ");
726 if (bustype & BUS_LPC)
727 ret = strcat_realloc(ret, "LPC, ");
728 if (bustype & BUS_FWH)
729 ret = strcat_realloc(ret, "FWH, ");
730 if (bustype & BUS_SPI)
731 ret = strcat_realloc(ret, "SPI, ");
732 if (bustype & BUS_PROG)
733 ret = strcat_realloc(ret, "Programmer-specific, ");
734 if (bustype == BUS_NONE)
735 ret = strcat_realloc(ret, "None, ");
736 }
737 /* Kill last comma. */
738 ret[strlen(ret) - 2] = '\0';
739 ret = realloc(ret, strlen(ret) + 1);
740 return ret;
741}
742
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000743int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000744{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000745 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000746 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000747 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000748
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000749 for (chip = flashchips + startchip; chip && chip->name; chip++) {
750 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000751 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000752 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000753 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000754 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300755 /* Only probe for SPI25 chips by default. */
756 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
757 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000758 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
759 if (!chip->probe && !force) {
760 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000761 continue;
762 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000763
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000764 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200765 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000766 if (!flash->chip) {
767 msg_gerr("Out of memory!\n");
768 exit(1);
769 }
Angel Pons7e134562021-06-07 13:29:13 +0200770 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000771 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000772
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000773 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +0100774 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000775
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000776 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
777 * is only called with force=1 after normal probing failed.
778 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000779 if (force)
780 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000781
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000782 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000783 goto notfound;
784
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000785 /* If this is the first chip found, accept it.
786 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000787 * a non-generic match. SFDP and CFI are generic matches.
788 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000789 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000790 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000791 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000792 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000793 msg_cinfo("===\n"
794 "SFDP has autodetected a flash chip which is "
795 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000796 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000797 msg_cinfo("The standard operations read and "
798 "verify should work, but to support "
799 "erase, write and all other "
800 "possible features");
801 else
802 msg_cinfo("All standard operations (read, "
803 "verify, erase and write) should "
804 "work, but to support all possible "
805 "features");
806
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000807 msg_cinfo(" we need to add them manually.\n"
808 "You can help us by mailing us the output of the following command to "
809 "flashrom@flashrom.org:\n"
810 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
811 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000812 "===\n");
813 }
814
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000815 /* First flash chip detected on this bus. */
816 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000817 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000818 /* Not the first flash chip detected on this bus, but not a generic match either. */
819 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
820 break;
821 /* 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 +0000822notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000823 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000824 free(flash->chip);
825 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000826 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000827
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000828 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000829 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000830
Nico Huber7af0e792016-04-29 16:40:15 +0200831 /* Fill fallback layout covering the whole chip. */
832 struct single_layout *const fallback = &flash->fallback_layout;
833 fallback->base.entries = &fallback->entry;
834 fallback->base.num_entries = 1;
835 fallback->entry.start = 0;
836 fallback->entry.end = flash->chip->total_size * 1024 - 1;
837 fallback->entry.included = true;
Nico Huber70461a92019-06-15 14:56:19 +0200838 fallback->entry.name = strdup("complete flash");
839 if (!fallback->entry.name) {
840 msg_cerr("Failed to probe chip: %s\n", strerror(errno));
841 return -1;
842 }
Stefan Reinauer051e2362011-01-19 06:21:54 +0000843
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000844 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000845 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
846 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000847 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000848#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200849 if (programmer->map_flash_region == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000850 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
851 PRIxPTR_WIDTH, flash->physical_memory);
852 else
853#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200854 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000855
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000856 /* Flash registers may more likely not be mapped if the chip was forced.
857 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000858 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000859 if (flash->chip->printlock)
860 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000861
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000862 /* Get out of the way for later runs. */
863 unmap_flash(flash);
864
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000865 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000866 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000867}
868
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000869int read_buf_from_file(unsigned char *buf, unsigned long size,
870 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000871{
Nico Huber7562f7d2013-08-30 21:29:45 +0000872#ifdef __LIBPAYLOAD__
873 msg_gerr("Error: No file I/O support in libpayload\n");
874 return 1;
875#else
Stefan Tauner16687702015-12-25 21:59:45 +0000876 int ret = 0;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000877
Stefan Tauner16687702015-12-25 21:59:45 +0000878 FILE *image;
Daniel Campello8eaef7d2021-04-15 10:36:04 -0600879 if (!strcmp(filename, "-"))
880 image = fdopen(fileno(stdin), "rb");
881 else
882 image = fopen(filename, "rb");
883 if (image == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000884 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000885 return 1;
886 }
Stefan Tauner16687702015-12-25 21:59:45 +0000887
888 struct stat image_stat;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000889 if (fstat(fileno(image), &image_stat) != 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000890 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
Stefan Tauner16687702015-12-25 21:59:45 +0000891 ret = 1;
892 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000893 }
Daniel Campello8eaef7d2021-04-15 10:36:04 -0600894 if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +0000895 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 +0000896 (intmax_t)image_stat.st_size, size);
Stefan Tauner16687702015-12-25 21:59:45 +0000897 ret = 1;
898 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000899 }
Stefan Tauner16687702015-12-25 21:59:45 +0000900
901 unsigned long numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000902 if (numbytes != size) {
903 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
904 "wanted %ld!\n", numbytes, size);
Stefan Tauner16687702015-12-25 21:59:45 +0000905 ret = 1;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000906 }
Stefan Tauner16687702015-12-25 21:59:45 +0000907out:
908 (void)fclose(image);
909 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +0000910#endif
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000911}
912
Mark Marshallf20b7be2014-05-09 21:16:21 +0000913int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000914{
Nico Huber7562f7d2013-08-30 21:29:45 +0000915#ifdef __LIBPAYLOAD__
916 msg_gerr("Error: No file I/O support in libpayload\n");
917 return 1;
918#else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000919 FILE *image;
Stefan Tauner16687702015-12-25 21:59:45 +0000920 int ret = 0;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000921
922 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000923 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000924 return 1;
925 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +0000926 if ((image = fopen(filename, "wb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000927 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000928 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000929 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000930
Stefan Tauner16687702015-12-25 21:59:45 +0000931 unsigned long numbytes = fwrite(buf, 1, size, image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000932 if (numbytes != size) {
Stefan Tauner16687702015-12-25 21:59:45 +0000933 msg_gerr("Error: file %s could not be written completely.\n", filename);
934 ret = 1;
935 goto out;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000936 }
Stefan Tauner16687702015-12-25 21:59:45 +0000937 if (fflush(image)) {
938 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
939 ret = 1;
940 }
941 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
942#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
943 struct stat image_stat;
944 if (fstat(fileno(image), &image_stat) != 0) {
945 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
946 ret = 1;
947 goto out;
948 }
949 if (S_ISREG(image_stat.st_mode)) {
950 if (fsync(fileno(image))) {
951 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
952 ret = 1;
953 }
954 }
955#endif
956out:
957 if (fclose(image)) {
958 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
959 ret = 1;
960 }
961 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +0000962#endif
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000963}
964
Nico Huber899e4ec2016-04-29 18:39:01 +0200965static int read_by_layout(struct flashctx *, uint8_t *);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000966int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000967{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000968 unsigned long size = flash->chip->total_size * 1024;
Richard Hughes84b453e2018-12-19 15:30:39 +0000969 unsigned char *buf = calloc(size, sizeof(unsigned char));
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000970 int ret = 0;
971
972 msg_cinfo("Reading flash... ");
973 if (!buf) {
974 msg_gerr("Memory allocation failed!\n");
975 msg_cinfo("FAILED.\n");
976 return 1;
977 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000978 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000979 msg_cerr("No read function available for this flash chip.\n");
980 ret = 1;
981 goto out_free;
982 }
Nico Huber899e4ec2016-04-29 18:39:01 +0200983 if (read_by_layout(flash, buf)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000984 msg_cerr("Read operation failed!\n");
985 ret = 1;
986 goto out_free;
987 }
988
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000989 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000990out_free:
991 free(buf);
992 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
993 return ret;
994}
995
Stefan Tauner96658be2014-05-26 22:05:31 +0000996/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000997static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000998{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000999 int i, j, k;
1000 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +05301001 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001002
1003 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1004 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001005 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +05301006 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001007
1008 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1009 /* Blocks with zero size are bugs in flashchips.c. */
1010 if (eraser.eraseblocks[i].count &&
1011 !eraser.eraseblocks[i].size) {
1012 msg_gerr("ERROR: Flash chip %s erase function "
1013 "%i region %i has size 0. Please report"
1014 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001015 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001016 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001017 }
1018 /* Blocks with zero count are bugs in flashchips.c. */
1019 if (!eraser.eraseblocks[i].count &&
1020 eraser.eraseblocks[i].size) {
1021 msg_gerr("ERROR: Flash chip %s erase function "
1022 "%i region %i has count 0. Please report"
1023 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001024 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001025 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001026 }
1027 done += eraser.eraseblocks[i].count *
1028 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +05301029 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001030 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001031 /* Empty eraseblock definition with erase function. */
1032 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001033 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001034 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001035 if (!done)
1036 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001037 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001038 msg_gerr("ERROR: Flash chip %s erase function %i "
1039 "region walking resulted in 0x%06x bytes total,"
1040 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001041 " flashrom@flashrom.org\n", chip->name, k,
1042 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001043 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001044 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001045 if (!eraser.block_erase)
1046 continue;
1047 /* Check if there are identical erase functions for different
1048 * layouts. That would imply "magic" erase functions. The
1049 * easiest way to check this is with function pointers.
1050 */
Uwe Hermann43959702010-03-13 17:28:29 +00001051 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001052 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001053 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001054 msg_gerr("ERROR: Flash chip %s erase function "
1055 "%i and %i are identical. Please report"
1056 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001057 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001058 ret = 1;
1059 }
Uwe Hermann43959702010-03-13 17:28:29 +00001060 }
Aarya Chaumal478e1792022-06-04 01:34:44 +05301061 if(curr_eraseblock_count > prev_eraseblock_count)
1062 {
1063 msg_gerr("ERROR: Flash chip %s erase function %i is not "
1064 "in order. Please report a bug at flashrom@flashrom.org\n",
1065 chip->name, k);
1066 ret = 1;
1067 }
1068 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001069 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001070 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001071}
1072
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001073static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001074{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001075 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001076
1077 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1078 if (log)
1079 msg_cdbg("not defined. ");
1080 return 1;
1081 }
1082 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1083 if (log)
1084 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001085 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001086 return 1;
1087 }
1088 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1089 if (log)
1090 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001091 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001092 return 1;
1093 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001094 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001095 return 0;
1096}
1097
Nico Huber7af0e792016-04-29 16:40:15 +02001098/**
1099 * @brief Reads the included layout regions into a buffer.
1100 *
1101 * If there is no layout set in the given flash context, the whole chip will
1102 * be read.
1103 *
1104 * @param flashctx Flash context to be used.
1105 * @param buffer Buffer of full chip size to read into.
1106 * @return 0 on success,
1107 * 1 if any read fails.
1108 */
1109static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1110{
1111 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001112 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001113
Nico Huber5ca55232019-06-15 22:29:08 +02001114 while ((entry = layout_next_included(layout, entry))) {
1115 const chipoff_t region_start = entry->start;
1116 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001117
1118 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1119 return 1;
1120 }
1121 return 0;
1122}
1123
1124typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1125/**
1126 * @private
1127 *
1128 * For read-erase-write, `curcontents` and `newcontents` shall point
1129 * to buffers of the chip's size. Both are supposed to be prefilled
1130 * with at least the included layout regions of the current flash
1131 * contents (`curcontents`) and the data to be written to the flash
1132 * (`newcontents`).
1133 *
1134 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1135 *
1136 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1137 */
1138struct walk_info {
1139 uint8_t *curcontents;
1140 const uint8_t *newcontents;
1141 chipoff_t region_start;
1142 chipoff_t region_end;
1143 chipoff_t erase_start;
1144 chipoff_t erase_end;
1145};
1146/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1147typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1148
1149static int walk_eraseblocks(struct flashctx *const flashctx,
1150 struct walk_info *const info,
1151 const size_t erasefunction, const per_blockfn_t per_blockfn)
1152{
1153 int ret;
1154 size_t i, j;
1155 bool first = true;
1156 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1157
1158 info->erase_start = 0;
1159 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1160 /* count==0 for all automatically initialized array
1161 members so the loop below won't be executed for them. */
1162 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1163 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1164
1165 /* Skip any eraseblock that is completely outside the current region. */
1166 if (info->erase_end < info->region_start)
1167 continue;
1168 if (info->region_end < info->erase_start)
1169 break;
1170
1171 /* Print this for every block except the first one. */
1172 if (first)
1173 first = false;
1174 else
1175 msg_cdbg(", ");
1176 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1177
1178 ret = per_blockfn(flashctx, info, eraser->block_erase);
1179 if (ret)
1180 return ret;
1181 }
1182 if (info->region_end < info->erase_start)
1183 break;
1184 }
1185 msg_cdbg("\n");
1186 return 0;
1187}
1188
1189static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1190 const per_blockfn_t per_blockfn)
1191{
1192 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001193 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001194
1195 all_skipped = true;
1196 msg_cinfo("Erasing and writing flash chip... ");
1197
Nico Huber5ca55232019-06-15 22:29:08 +02001198 while ((entry = layout_next_included(layout, entry))) {
1199 info->region_start = entry->start;
1200 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001201
1202 size_t j;
1203 int error = 1; /* retry as long as it's 1 */
1204 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1205 if (j != 0)
1206 msg_cinfo("Looking for another erase function.\n");
1207 msg_cdbg("Trying erase function %zi... ", j);
1208 if (check_block_eraser(flashctx, j, 1))
1209 continue;
1210
1211 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1212 if (error != 1)
1213 break;
1214
1215 if (info->curcontents) {
1216 msg_cinfo("Reading current flash chip contents... ");
1217 if (read_by_layout(flashctx, info->curcontents)) {
1218 /* Now we are truly screwed. Read failed as well. */
1219 msg_cerr("Can't read anymore! Aborting.\n");
1220 /* We have no idea about the flash chip contents, so
1221 retrying with another erase function is pointless. */
1222 error = 2;
1223 break;
1224 }
1225 msg_cinfo("done. ");
1226 }
1227 }
1228 if (error == 1)
1229 msg_cinfo("No usable erase functions left.\n");
1230 if (error) {
1231 msg_cerr("FAILED!\n");
1232 return 1;
1233 }
1234 }
1235 if (all_skipped)
1236 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1237 msg_cinfo("Erase/write done.\n");
1238 return 0;
1239}
1240
1241static int erase_block(struct flashctx *const flashctx,
1242 const struct walk_info *const info, const erasefn_t erasefn)
1243{
1244 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001245 const bool region_unaligned = info->region_start > info->erase_start ||
1246 info->erase_end > info->region_end;
1247 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1248 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001249
Nico Huber6e61e0c2019-01-23 17:07:49 +01001250 /*
1251 * If the region is not erase-block aligned, merge current flash con-
1252 * tents into a new buffer `backup_contents`.
1253 */
1254 if (region_unaligned) {
1255 backup_contents = malloc(erase_len);
1256 erased_contents = malloc(erase_len);
1257 if (!backup_contents || !erased_contents) {
1258 msg_cerr("Out of memory!\n");
1259 ret = 1;
1260 goto _free_ret;
1261 }
1262 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1263 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1264
1265 msg_cdbg("R");
1266 /* Merge data preceding the current region. */
1267 if (info->region_start > info->erase_start) {
1268 const chipoff_t start = info->erase_start;
1269 const chipsize_t len = info->region_start - info->erase_start;
1270 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1271 msg_cerr("Can't read! Aborting.\n");
1272 goto _free_ret;
1273 }
1274 }
1275 /* Merge data following the current region. */
1276 if (info->erase_end > info->region_end) {
1277 const chipoff_t start = info->region_end + 1;
1278 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1279 const chipsize_t len = info->erase_end - info->region_end;
1280 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1281 msg_cerr("Can't read! Aborting.\n");
1282 goto _free_ret;
1283 }
1284 }
1285 }
1286
1287 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001288 all_skipped = false;
1289
1290 msg_cdbg("E");
1291 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001292 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001293 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1294 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001295 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001296 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001297
1298 if (region_unaligned) {
1299 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1300 /* get_next_write() sets starthere to a new value after the call. */
1301 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1302 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1303 if (!writecount++)
1304 msg_cdbg("W");
1305 /* Needs the partial write function signature. */
1306 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1307 info->erase_start + starthere, lenhere))
1308 goto _free_ret;
1309 starthere += lenhere;
1310 }
1311 }
1312
1313 ret = 0;
1314
1315_free_ret:
1316 free(erased_contents);
1317 free(backup_contents);
1318 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001319}
1320
1321/**
1322 * @brief Erases the included layout regions.
1323 *
1324 * If there is no layout set in the given flash context, the whole chip will
1325 * be erased.
1326 *
1327 * @param flashctx Flash context to be used.
1328 * @param buffer Buffer of full chip size to read into.
1329 * @return 0 on success,
1330 * 1 if all available erase functions failed.
1331 */
Nico Huber454f6132012-12-10 13:34:10 +00001332static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001333{
1334 struct walk_info info = { 0 };
1335 return walk_by_layout(flashctx, &info, &erase_block);
1336}
1337
1338static int read_erase_write_block(struct flashctx *const flashctx,
1339 const struct walk_info *const info, const erasefn_t erasefn)
1340{
1341 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1342 const bool region_unaligned = info->region_start > info->erase_start ||
1343 info->erase_end > info->region_end;
1344 const uint8_t *newcontents = NULL;
1345 int ret = 2;
1346
1347 /*
1348 * If the region is not erase-block aligned, merge current flash con-
1349 * tents into `info->curcontents` and a new buffer `newc`. The former
1350 * is necessary since we have no guarantee that the full erase block
1351 * was already read into `info->curcontents`. For the latter a new
1352 * buffer is used since `info->newcontents` might contain data for
1353 * other unaligned regions that touch this erase block too.
1354 */
1355 if (region_unaligned) {
1356 msg_cdbg("R");
1357 uint8_t *const newc = malloc(erase_len);
1358 if (!newc) {
1359 msg_cerr("Out of memory!\n");
1360 return 1;
1361 }
1362 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1363
1364 /* Merge data preceding the current region. */
1365 if (info->region_start > info->erase_start) {
1366 const chipoff_t start = info->erase_start;
1367 const chipsize_t len = info->region_start - info->erase_start;
1368 if (flashctx->chip->read(flashctx, newc, start, len)) {
1369 msg_cerr("Can't read! Aborting.\n");
1370 goto _free_ret;
1371 }
1372 memcpy(info->curcontents + start, newc, len);
1373 }
1374 /* Merge data following the current region. */
1375 if (info->erase_end > info->region_end) {
1376 const chipoff_t start = info->region_end + 1;
1377 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1378 const chipsize_t len = info->erase_end - info->region_end;
1379 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1380 msg_cerr("Can't read! Aborting.\n");
1381 goto _free_ret;
1382 }
1383 memcpy(info->curcontents + start, newc + rel_start, len);
1384 }
1385
1386 newcontents = newc;
1387 } else {
1388 newcontents = info->newcontents + info->erase_start;
1389 }
1390
1391 ret = 1;
1392 bool skipped = true;
1393 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001394 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001395 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1396 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001397 if (erase_block(flashctx, info, erasefn))
1398 goto _free_ret;
1399 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001400 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001401 skipped = false;
1402 }
1403
1404 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1405 /* get_next_write() sets starthere to a new value after the call. */
1406 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1407 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1408 if (!writecount++)
1409 msg_cdbg("W");
1410 /* Needs the partial write function signature. */
1411 if (flashctx->chip->write(flashctx, newcontents + starthere,
1412 info->erase_start + starthere, lenhere))
1413 goto _free_ret;
1414 starthere += lenhere;
1415 skipped = false;
1416 }
1417 if (skipped)
1418 msg_cdbg("S");
1419 else
1420 all_skipped = false;
1421
1422 /* Update curcontents, other regions with overlapping erase blocks
1423 might rely on this. */
1424 memcpy(curcontents, newcontents, erase_len);
1425 ret = 0;
1426
1427_free_ret:
1428 if (region_unaligned)
1429 free((void *)newcontents);
1430 return ret;
1431}
1432
1433/**
1434 * @brief Writes the included layout regions from a given image.
1435 *
1436 * If there is no layout set in the given flash context, the whole image
1437 * will be written.
1438 *
1439 * @param flashctx Flash context to be used.
1440 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1441 * @param newcontents The new image to be written.
1442 * @return 0 on success,
1443 * 1 if anything has gone wrong.
1444 */
Nico Huber454f6132012-12-10 13:34:10 +00001445static int write_by_layout(struct flashctx *const flashctx,
1446 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001447{
1448 struct walk_info info;
1449 info.curcontents = curcontents;
1450 info.newcontents = newcontents;
1451 return walk_by_layout(flashctx, &info, read_erase_write_block);
1452}
1453
1454/**
1455 * @brief Compares the included layout regions with content from a buffer.
1456 *
1457 * If there is no layout set in the given flash context, the whole chip's
1458 * contents will be compared.
1459 *
1460 * @param flashctx Flash context to be used.
1461 * @param curcontents A buffer of full chip size to read current chip contents into.
1462 * @param newcontents The new image to compare to.
1463 * @return 0 on success,
1464 * 1 if reading failed,
1465 * 3 if the contents don't match.
1466 */
Nico Huber454f6132012-12-10 13:34:10 +00001467static int verify_by_layout(struct flashctx *const flashctx,
1468 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001469{
1470 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001471 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001472
Nico Huber5ca55232019-06-15 22:29:08 +02001473 while ((entry = layout_next_included(layout, entry))) {
1474 const chipoff_t region_start = entry->start;
1475 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001476
1477 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1478 return 1;
1479 if (compare_range(newcontents + region_start, curcontents + region_start,
1480 region_start, region_len))
1481 return 3;
1482 }
1483 return 0;
1484}
1485
Stefan Tauner136388f2013-07-15 10:47:53 +00001486static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001487{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001488 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001489#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001490 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001491 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Angel Pons1900e1d2021-07-02 12:42:23 +02001492 "chip. Please report this to the mailing list at flashrom@flashrom.org or on\n"
1493 "IRC (see https://www.flashrom.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001494 "-------------------------------------------------------------------------------\n"
1495 "You may now reboot or simply leave the machine running.\n");
1496 else
1497#endif
1498 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1499 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
Angel Pons1900e1d2021-07-02 12:42:23 +02001500 "please report this to the mailing list at flashrom@flashrom.org or on IRC (see\n"
1501 "https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001502}
1503
Stefan Tauner136388f2013-07-15 10:47:53 +00001504static void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001505{
Stefan Tauner136388f2013-07-15 10:47:53 +00001506 msg_gerr("Your flash chip is in an unknown state.\n");
1507#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001508 if (programmer == &programmer_internal)
Angel Pons1900e1d2021-07-02 12:42:23 +02001509 msg_gerr("Get help on IRC (see https://www.flashrom.org/Contact) or mail\n"
1510 "flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!"
Stefan Tauner136388f2013-07-15 10:47:53 +00001511 "-------------------------------------------------------------------------------\n"
1512 "DO NOT REBOOT OR POWEROFF!\n");
1513 else
1514#endif
Angel Pons1900e1d2021-07-02 12:42:23 +02001515 msg_gerr("Please report this to the mailing list at flashrom@flashrom.org or\n"
1516 "on IRC (see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001517}
1518
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001519void list_programmers_linebreak(int startcol, int cols, int paren)
1520{
1521 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001522 int pnamelen;
1523 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001524 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001525 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001526
Thomas Heijligend45cb592021-05-19 14:12:18 +02001527 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001528 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001529 pnamelen = strlen(pname);
1530 if (remaining - pnamelen - 2 < 0) {
1531 if (firstline)
1532 firstline = 0;
1533 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001534 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001535 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001536 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001537 remaining = cols - startcol;
1538 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001539 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001540 remaining--;
1541 }
1542 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001543 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001544 remaining--;
1545 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001546 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001547 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001548 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001549 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001550 remaining--;
1551 } else {
1552 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001553 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001554 }
1555 }
1556}
1557
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001558static void print_sysinfo(void)
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001559{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001560#if IS_WINDOWS
Angel Pons7e134562021-06-07 13:29:13 +02001561 SYSTEM_INFO si = { 0 };
1562 OSVERSIONINFOEX osvi = { 0 };
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001563
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001564 msg_ginfo(" on Windows");
1565 /* Tell Windows which version of the structure we want. */
1566 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1567 if (GetVersionEx((OSVERSIONINFO*) &osvi))
1568 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
1569 else
1570 msg_ginfo(" unknown version");
1571 GetSystemInfo(&si);
1572 switch (si.wProcessorArchitecture) {
1573 case PROCESSOR_ARCHITECTURE_AMD64:
1574 msg_ginfo(" (x86_64)");
1575 break;
1576 case PROCESSOR_ARCHITECTURE_INTEL:
1577 msg_ginfo(" (x86)");
1578 break;
1579 default:
1580 msg_ginfo(" (unknown arch)");
1581 break;
1582 }
1583#elif HAVE_UTSNAME == 1
1584 struct utsname osinfo;
1585
1586 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001587 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1588 osinfo.machine);
1589#else
1590 msg_ginfo(" on unknown machine");
1591#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001592}
1593
1594void print_buildinfo(void)
1595{
1596 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001597#if NEED_PCI == 1
1598#ifdef PCILIB_VERSION
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001599 msg_gdbg(" libpci %s,", PCILIB_VERSION);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001600#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001601 msg_gdbg(" unknown PCI library,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001602#endif
1603#endif
1604#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001605 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001606#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001607 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001608#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001609 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001610#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001611#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001612 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001613#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001614 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001615#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001616 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001617#endif
1618#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001619 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001620#endif
1621#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001622 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001623#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001624 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001625#else
1626#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001627#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001628 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001629}
1630
Bernhard Walle201bde32008-01-21 15:24:22 +00001631void print_version(void)
1632{
Stefan Tauner76347082016-11-27 17:45:49 +01001633 msg_ginfo("flashrom %s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001634 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001635 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00001636}
1637
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001638void print_banner(void)
1639{
1640 msg_ginfo("flashrom is free software, get the source code at "
Stefan Tauner4c723152016-01-14 22:47:55 +00001641 "https://flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001642 msg_ginfo("\n");
1643}
1644
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001645int selfcheck(void)
1646{
Stefan Tauner96658be2014-05-26 22:05:31 +00001647 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001648 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001649
1650 /* Safety check. Instead of aborting after the first error, check
1651 * if more errors exist.
1652 */
Thomas Heijligend0fcce22021-05-19 13:53:34 +02001653 if (programmer_table_size != PROGRAMMER_INVALID) {
Sean Nelson316a29f2010-05-07 20:09:04 +00001654 msg_gerr("Programmer table miscompilation!\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001655 ret = 1;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001656 }
Thomas Heijligend45cb592021-05-19 14:12:18 +02001657 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001658 const struct programmer_entry *const p = programmer_table[i];
1659 if (p == NULL) {
1660 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1661 ret = 1;
1662 continue;
1663 }
1664 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001665 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1666 ret = 1;
1667 /* This might hide other problems with this programmer, but allows for better error
1668 * messages below without jumping through hoops. */
1669 continue;
1670 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001671 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001672 case USB:
1673 case PCI:
1674 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001675 if (p->devs.note == NULL) {
1676 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001677 break; /* This one has its device list stored separately. */
1678 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001679 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001680 ret = 1;
1681 }
1682 break;
1683 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001684 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001685 ret = 1;
1686 break;
1687 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001688 if (p->init == NULL) {
1689 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001690 ret = 1;
1691 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001692 if (p->delay == NULL) {
1693 msg_gerr("Programmer %s does not have a valid delay function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001694 ret = 1;
1695 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001696 if (p->map_flash_region == NULL) {
1697 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001698 ret = 1;
1699 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001700 if (p->unmap_flash_region == NULL) {
1701 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001702 ret = 1;
1703 }
1704 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001705
1706 /* It would be favorable if we could check for the correct layout (especially termination) of various
1707 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1708 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1709 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1710 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1711 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001712 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001713 msg_gerr("Flashchips table miscompilation!\n");
1714 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001715 } else {
1716 for (i = 0; i < flashchips_size - 1; i++) {
1717 const struct flashchip *chip = &flashchips[i];
1718 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1719 ret = 1;
1720 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
1721 "Please report a bug at flashrom@flashrom.org\n", i,
1722 chip->name == NULL ? "unnamed" : chip->name);
1723 }
1724 if (selfcheck_eraseblocks(chip)) {
1725 ret = 1;
1726 }
1727 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001728 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001729
Stefan Tauner600576b2014-06-12 22:57:36 +00001730#if CONFIG_INTERNAL == 1
1731 ret |= selfcheck_board_enables();
1732#endif
1733
Stefan Tauner96658be2014-05-26 22:05:31 +00001734 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001735 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001736}
1737
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001738/* FIXME: This function signature needs to be improved once prepare_flash_access()
1739 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001740 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001741static int chip_safety_check(const struct flashctx *flash, int force,
1742 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001743{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001744 const struct flashchip *chip = flash->chip;
1745
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001746 if (!programmer_may_write && (write_it || erase_it)) {
1747 msg_perr("Write/erase is not working yet on your programmer in "
1748 "its current configuration.\n");
1749 /* --force is the wrong approach, but it's the best we can do
1750 * until the generic programmer parameter parser is merged.
1751 */
1752 if (!force)
1753 return 1;
1754 msg_cerr("Continuing anyway.\n");
1755 }
1756
1757 if (read_it || erase_it || write_it || verify_it) {
1758 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001759 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001760 msg_cerr("Read is not working on this chip. ");
1761 if (!force)
1762 return 1;
1763 msg_cerr("Continuing anyway.\n");
1764 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001765 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001766 msg_cerr("flashrom has no read function for this "
1767 "flash chip.\n");
1768 return 1;
1769 }
1770 }
1771 if (erase_it || write_it) {
1772 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001773 if (chip->tested.erase == NA) {
1774 msg_cerr("Erase is not possible on this chip.\n");
1775 return 1;
1776 }
1777 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001778 msg_cerr("Erase is not working on this chip. ");
1779 if (!force)
1780 return 1;
1781 msg_cerr("Continuing anyway.\n");
1782 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001783 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001784 msg_cerr("flashrom has no erase function for this "
1785 "flash chip.\n");
1786 return 1;
1787 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001788 }
1789 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001790 if (chip->tested.write == NA) {
1791 msg_cerr("Write is not possible on this chip.\n");
1792 return 1;
1793 }
1794 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001795 msg_cerr("Write is not working on this chip. ");
1796 if (!force)
1797 return 1;
1798 msg_cerr("Continuing anyway.\n");
1799 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001800 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001801 msg_cerr("flashrom has no write function for this "
1802 "flash chip.\n");
1803 return 1;
1804 }
1805 }
1806 return 0;
1807}
1808
Nico Huber305f4172013-06-14 11:55:26 +02001809int prepare_flash_access(struct flashctx *const flash,
1810 const bool read_it, const bool write_it,
1811 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001812{
1813 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1814 msg_cerr("Aborting.\n");
1815 return 1;
1816 }
1817
1818 if (flash->layout == get_global_layout() && normalize_romentries(flash)) {
1819 msg_cerr("Requested regions can not be handled. Aborting.\n");
1820 return 1;
1821 }
1822
1823 if (map_flash(flash) != 0)
1824 return 1;
1825
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001826 /* Initialize chip_restore_fn_count before chip unlock calls. */
1827 flash->chip_restore_fn_count = 0;
1828
Nico Huber454f6132012-12-10 13:34:10 +00001829 /* Given the existence of read locks, we want to unlock for read,
1830 erase and write. */
1831 if (flash->chip->unlock)
1832 flash->chip->unlock(flash);
1833
Nico Huberf43c6542017-10-14 17:47:28 +02001834 flash->address_high_byte = -1;
1835 flash->in_4ba_mode = false;
1836
Nico Huberdc5af542018-12-22 16:54:59 +01001837 /* Be careful about 4BA chips and broken masters */
1838 if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
1839 /* If we can't use native instructions, bail out */
1840 if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
1841 || !spi_master_4ba(flash)) {
1842 msg_cerr("Programmer doesn't support this chip. Aborting.\n");
1843 return 1;
1844 }
1845 }
1846
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001847 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01001848 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01001849 int ret;
1850 if (spi_master_4ba(flash))
1851 ret = spi_enter_4ba(flash);
1852 else
1853 ret = spi_exit_4ba(flash);
1854 if (ret) {
1855 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001856 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02001857 }
Boris Baykov99127182016-06-11 18:29:00 +02001858 }
1859
Nico Huber454f6132012-12-10 13:34:10 +00001860 return 0;
1861}
1862
Nico Huber305f4172013-06-14 11:55:26 +02001863void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001864{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001865 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001866 unmap_flash(flash);
1867}
1868
1869/**
1870 * @addtogroup flashrom-flash
1871 * @{
1872 */
1873
1874/**
1875 * @brief Erase the specified ROM chip.
1876 *
1877 * If a layout is set in the given flash context, only included regions
1878 * will be erased.
1879 *
1880 * @param flashctx The context of the flash chip to erase.
1881 * @return 0 on success.
1882 */
1883int flashrom_flash_erase(struct flashctx *const flashctx)
1884{
1885 if (prepare_flash_access(flashctx, false, false, true, false))
1886 return 1;
1887
1888 const int ret = erase_by_layout(flashctx);
1889
1890 finalize_flash_access(flashctx);
1891
1892 return ret;
1893}
1894
1895/** @} */ /* end flashrom-flash */
1896
1897/**
1898 * @defgroup flashrom-ops Operations
1899 * @{
1900 */
1901
1902/**
1903 * @brief Read the current image from the specified ROM chip.
1904 *
1905 * If a layout is set in the specified flash context, only included regions
1906 * will be read.
1907 *
1908 * @param flashctx The context of the flash chip.
1909 * @param buffer Target buffer to write image to.
1910 * @param buffer_len Size of target buffer in bytes.
1911 * @return 0 on success,
1912 * 2 if buffer_len is too short for the flash chip's contents,
1913 * or 1 on any other failure.
1914 */
1915int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
1916{
1917 const size_t flash_size = flashctx->chip->total_size * 1024;
1918
1919 if (flash_size > buffer_len)
1920 return 2;
1921
1922 if (prepare_flash_access(flashctx, true, false, false, false))
1923 return 1;
1924
1925 msg_cinfo("Reading flash... ");
1926
1927 int ret = 1;
1928 if (read_by_layout(flashctx, buffer)) {
1929 msg_cerr("Read operation failed!\n");
1930 msg_cinfo("FAILED.\n");
1931 goto _finalize_ret;
1932 }
1933 msg_cinfo("done.\n");
1934 ret = 0;
1935
1936_finalize_ret:
1937 finalize_flash_access(flashctx);
1938 return ret;
1939}
1940
1941static void combine_image_by_layout(const struct flashctx *const flashctx,
1942 uint8_t *const newcontents, const uint8_t *const oldcontents)
1943{
1944 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001945 const struct romentry *included;
1946 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001947
Nico Huber3d7b1e32018-12-22 00:53:14 +01001948 while ((included = layout_next_included_region(layout, start))) {
1949 if (included->start > start) {
1950 /* copy everything up to the start of this included region */
1951 memcpy(newcontents + start, oldcontents + start, included->start - start);
1952 }
1953 /* skip this included region */
1954 start = included->end + 1;
1955 if (start == 0)
1956 return;
Nico Huber454f6132012-12-10 13:34:10 +00001957 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001958
1959 /* copy the rest of the chip */
1960 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1961 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001962}
1963
1964/**
1965 * @brief Write the specified image to the ROM chip.
1966 *
1967 * If a layout is set in the specified flash context, only erase blocks
1968 * containing included regions will be touched.
1969 *
1970 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001971 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001972 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001973 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001974 * @return 0 on success,
1975 * 4 if buffer_len doesn't match the size of the flash chip,
1976 * 3 if write was tried but nothing has changed,
1977 * 2 if write failed and flash contents changed,
1978 * or 1 on any other failure.
1979 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001980int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
1981 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001982{
1983 const size_t flash_size = flashctx->chip->total_size * 1024;
1984 const bool verify_all = flashctx->flags.verify_whole_chip;
1985 const bool verify = flashctx->flags.verify_after_write;
1986
1987 if (buffer_len != flash_size)
1988 return 4;
1989
1990 int ret = 1;
1991
1992 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001993 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001994 uint8_t *const curcontents = malloc(flash_size);
1995 uint8_t *oldcontents = NULL;
1996 if (verify_all)
1997 oldcontents = malloc(flash_size);
1998 if (!curcontents || (verify_all && !oldcontents)) {
1999 msg_gerr("Out of memory!\n");
2000 goto _free_ret;
2001 }
2002
2003#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02002004 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00002005 if (flashctx->flags.force_boardmismatch) {
2006 msg_pinfo("Proceeding anyway because user forced us to.\n");
2007 } else {
2008 msg_perr("Aborting. You can override this with "
2009 "-p internal:boardmismatch=force.\n");
2010 goto _free_ret;
2011 }
2012 }
2013#endif
2014
2015 if (prepare_flash_access(flashctx, false, true, false, verify))
2016 goto _free_ret;
2017
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002018 /* If given, assume flash chip contains same data as `refcontents`. */
2019 if (refcontents) {
2020 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
2021 memcpy(curcontents, refcontents, flash_size);
2022 if (oldcontents)
2023 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00002024 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002025 /*
2026 * Read the whole chip to be able to check whether regions need to be
2027 * erased and to give better diagnostics in case write fails.
2028 * The alternative is to read only the regions which are to be
2029 * preserved, but in that case we might perform unneeded erase which
2030 * takes time as well.
2031 */
2032 msg_cinfo("Reading old flash chip contents... ");
2033 if (verify_all) {
2034 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
2035 msg_cinfo("FAILED.\n");
2036 goto _finalize_ret;
2037 }
2038 memcpy(curcontents, oldcontents, flash_size);
2039 } else {
2040 if (read_by_layout(flashctx, curcontents)) {
2041 msg_cinfo("FAILED.\n");
2042 goto _finalize_ret;
2043 }
Nico Huber454f6132012-12-10 13:34:10 +00002044 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002045 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00002046 }
Nico Huber454f6132012-12-10 13:34:10 +00002047
2048 if (write_by_layout(flashctx, curcontents, newcontents)) {
2049 msg_cerr("Uh oh. Erase/write failed. ");
2050 ret = 2;
2051 if (verify_all) {
2052 msg_cerr("Checking if anything has changed.\n");
2053 msg_cinfo("Reading current flash chip contents... ");
2054 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
2055 msg_cinfo("done.\n");
2056 if (!memcmp(oldcontents, curcontents, flash_size)) {
2057 nonfatal_help_message();
2058 goto _finalize_ret;
2059 }
2060 msg_cerr("Apparently at least some data has changed.\n");
2061 } else
2062 msg_cerr("Can't even read anymore!\n");
2063 emergency_help_message();
2064 goto _finalize_ret;
2065 } else {
2066 msg_cerr("\n");
2067 }
2068 emergency_help_message();
2069 goto _finalize_ret;
2070 }
2071
2072 /* Verify only if we actually changed something. */
2073 if (verify && !all_skipped) {
2074 const struct flashrom_layout *const layout_bak = flashctx->layout;
2075
2076 msg_cinfo("Verifying flash... ");
2077
2078 /* Work around chips which need some time to calm down. */
2079 programmer_delay(1000*1000);
2080
2081 if (verify_all) {
2082 combine_image_by_layout(flashctx, newcontents, oldcontents);
2083 flashctx->layout = NULL;
2084 }
2085 ret = verify_by_layout(flashctx, curcontents, newcontents);
2086 flashctx->layout = layout_bak;
2087 /* If we tried to write, and verification now fails, we
2088 might have an emergency situation. */
2089 if (ret)
2090 emergency_help_message();
2091 else
2092 msg_cinfo("VERIFIED.\n");
2093 } else {
2094 /* We didn't change anything. */
2095 ret = 0;
2096 }
2097
2098_finalize_ret:
2099 finalize_flash_access(flashctx);
2100_free_ret:
2101 free(oldcontents);
2102 free(curcontents);
2103 return ret;
2104}
2105
2106/**
2107 * @brief Verify the ROM chip's contents with the specified image.
2108 *
2109 * If a layout is set in the specified flash context, only included regions
2110 * will be verified.
2111 *
2112 * @param flashctx The context of the flash chip.
2113 * @param buffer Source buffer to verify with.
2114 * @param buffer_len Size of source buffer in bytes.
2115 * @return 0 on success,
2116 * 3 if the chip's contents don't match,
2117 * 2 if buffer_len doesn't match the size of the flash chip,
2118 * or 1 on any other failure.
2119 */
2120int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
2121{
2122 const size_t flash_size = flashctx->chip->total_size * 1024;
2123
2124 if (buffer_len != flash_size)
2125 return 2;
2126
2127 const uint8_t *const newcontents = buffer;
2128 uint8_t *const curcontents = malloc(flash_size);
2129 if (!curcontents) {
2130 msg_gerr("Out of memory!\n");
2131 return 1;
2132 }
2133
2134 int ret = 1;
2135
2136 if (prepare_flash_access(flashctx, false, false, false, true))
2137 goto _free_ret;
2138
2139 msg_cinfo("Verifying flash... ");
2140 ret = verify_by_layout(flashctx, curcontents, newcontents);
2141 if (!ret)
2142 msg_cinfo("VERIFIED.\n");
2143
2144 finalize_flash_access(flashctx);
2145_free_ret:
2146 free(curcontents);
2147 return ret;
2148}
2149
2150/** @} */ /* end flashrom-ops */
Nico Huber899e4ec2016-04-29 18:39:01 +02002151
2152int do_read(struct flashctx *const flash, const char *const filename)
2153{
2154 if (prepare_flash_access(flash, true, false, false, false))
2155 return 1;
2156
2157 const int ret = read_flash_to_file(flash, filename);
2158
2159 finalize_flash_access(flash);
2160
2161 return ret;
2162}
2163
2164int do_erase(struct flashctx *const flash)
2165{
2166 const int ret = flashrom_flash_erase(flash);
2167
2168 /*
2169 * FIXME: Do we really want the scary warning if erase failed?
2170 * After all, after erase the chip is either blank or partially
2171 * blank or it has the old contents. A blank chip won't boot,
2172 * so if the user wanted erase and reboots afterwards, the user
2173 * knows very well that booting won't work.
2174 */
2175 if (ret)
2176 emergency_help_message();
2177
2178 return ret;
2179}
2180
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002181int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
Nico Huber899e4ec2016-04-29 18:39:01 +02002182{
2183 const size_t flash_size = flash->chip->total_size * 1024;
2184 int ret = 1;
2185
2186 uint8_t *const newcontents = malloc(flash_size);
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002187 uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
2188
2189 if (!newcontents || (referencefile && !refcontents)) {
Nico Huber899e4ec2016-04-29 18:39:01 +02002190 msg_gerr("Out of memory!\n");
2191 goto _free_ret;
2192 }
2193
2194 if (read_buf_from_file(newcontents, flash_size, filename))
2195 goto _free_ret;
2196
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002197 if (referencefile) {
2198 if (read_buf_from_file(refcontents, flash_size, referencefile))
2199 goto _free_ret;
2200 }
2201
2202 ret = flashrom_image_write(flash, newcontents, flash_size, refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002203
2204_free_ret:
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002205 free(refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002206 free(newcontents);
2207 return ret;
2208}
2209
2210int do_verify(struct flashctx *const flash, const char *const filename)
2211{
2212 const size_t flash_size = flash->chip->total_size * 1024;
2213 int ret = 1;
2214
2215 uint8_t *const newcontents = malloc(flash_size);
2216 if (!newcontents) {
2217 msg_gerr("Out of memory!\n");
2218 goto _free_ret;
2219 }
2220
2221 if (read_buf_from_file(newcontents, flash_size, filename))
2222 goto _free_ret;
2223
2224 ret = flashrom_image_verify(flash, newcontents, flash_size);
2225
2226_free_ret:
2227 free(newcontents);
2228 return ret;
2229}