blob: 406c20f4475930cf4a516eb0ed9118c24f57cd20 [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Nico Huber7af0e792016-04-29 16:40:15 +02008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000010 *
Uwe Hermannd1107642007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000015 *
Uwe Hermannd1107642007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000020 */
21
Felix Singerf25447e2022-08-19 02:44:28 +020022#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000023#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000024#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000025#ifndef __LIBPAYLOAD__
26#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000027#include <sys/stat.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000028#endif
Ronald G. Minnichceec4202003-07-25 04:37:41 +000029#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000030#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000031#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000032#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000033#include <ctype.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 Huber6a2ebeb2022-08-26 11:36:48 +020047static 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? */
Felix Singer980d6b82022-08-19 02:48:15 +020059bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000060
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 */
Felix Singerf25447e2022-08-19 02:44:28 +020071static bool may_register_shutdown = false;
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. */
Felix Singerf25447e2022-08-19 02:44:28 +0200159 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000160 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200161 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000162
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200163 if (param) {
164 programmer_param = strdup(param);
165 if (!programmer_param) {
166 msg_perr("Out of memory!\n");
167 return ERROR_FATAL;
168 }
169 } else {
170 programmer_param = NULL;
171 }
172
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200173 msg_pdbg("Initializing %s programmer\n", programmer->name);
174 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000175 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000176 if (ret != 0) {
177 /* It is quite possible that any unhandled programmer parameter would have been valid,
178 * but an error in actual programmer init happened before the parameter was evaluated.
179 */
180 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
181 programmer_param);
182 } else {
183 /* Actual programmer init was successful, but the user specified an invalid or unusable
184 * (for the current programmer configuration) parameter.
185 */
186 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
187 msg_perr("Aborting.\n");
188 ret = ERROR_FATAL;
189 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000190 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200191 free(programmer_param);
192 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000193 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000194}
195
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000196/** Calls registered shutdown functions and resets internal programmer-related variables.
197 * Calling it is safe even without previous initialization, but further interactions with programmer support
198 * require a call to programmer_init() (afterwards).
199 *
200 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000201int programmer_shutdown(void)
202{
David Hendricks8bb20212011-06-14 01:35:36 +0000203 int ret = 0;
204
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000205 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200206 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000207 while (shutdown_fn_count > 0) {
208 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000209 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000210 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000211 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000212
David Hendricks8bb20212011-06-14 01:35:36 +0000213 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000214}
215
Stefan Tauner305e0b92013-07-17 23:46:44 +0000216void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000217{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200218 void *ret = programmer->map_flash_region(descr, phys_addr, len);
Stefan Tauner26e7a152013-09-13 17:21:05 +0000219 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
220 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
221 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000222}
223
224void programmer_unmap_flash_region(void *virt_addr, size_t len)
225{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200226 programmer->unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000227 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000228}
229
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000230void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000231{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000232 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000233}
234
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000235void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000236{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000237 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000238}
239
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000240void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000241{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000242 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000243}
244
Mark Marshallf20b7be2014-05-09 21:16:21 +0000245void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000246{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000247 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000248}
249
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000250uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000251{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000252 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000253}
254
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000255uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000256{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000257 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000258}
259
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000260uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000261{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000262 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000263}
264
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000265void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
266 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000267{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000268 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000269}
270
Stefan Taunerf80419c2014-05-02 15:41:42 +0000271void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000272{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000273 if (usecs > 0)
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200274 programmer->delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000275}
276
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000277int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
278 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000279{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000280 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000281
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000282 return 0;
283}
284
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000285/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000286 * It will look for needle with a subsequent '=' in haystack, return a copy of
287 * needle and remove everything from the first occurrence of needle to the next
288 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000289 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200290static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000291{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000292 char *param_pos, *opt_pos, *rest;
293 char *opt = NULL;
294 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000295 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000296
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000297 needlelen = strlen(needle);
298 if (!needlelen) {
299 msg_gerr("%s: empty needle! Please report a bug at "
300 "flashrom@flashrom.org\n", __func__);
301 return NULL;
302 }
303 /* No programmer parameters given. */
304 if (*haystack == NULL)
305 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000306 param_pos = strstr(*haystack, needle);
307 do {
308 if (!param_pos)
309 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000310 /* Needle followed by '='? */
311 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000312 /* Beginning of the string? */
313 if (param_pos == *haystack)
314 break;
315 /* After a delimiter? */
316 if (strchr(delim, *(param_pos - 1)))
317 break;
318 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000319 /* Continue searching. */
320 param_pos++;
321 param_pos = strstr(param_pos, needle);
322 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000323
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000324 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000325 /* Get the string after needle and '='. */
326 opt_pos = param_pos + needlelen + 1;
327 optlen = strcspn(opt_pos, delim);
328 /* Return an empty string if the parameter was empty. */
329 opt = malloc(optlen + 1);
330 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000331 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000332 exit(1);
333 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000334 strncpy(opt, opt_pos, optlen);
335 opt[optlen] = '\0';
336 rest = opt_pos + optlen;
337 /* Skip all delimiters after the current parameter. */
338 rest += strspn(rest, delim);
339 memmove(param_pos, rest, strlen(rest) + 1);
340 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000341 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000342
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000343 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000344}
345
Stefan Tauner66652442011-06-26 17:38:17 +0000346char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000347{
348 return extract_param(&programmer_param, param_name, ",");
349}
350
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000351/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000352static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000353{
354 unsigned int usable_erasefunctions = 0;
355 int k;
356 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
357 if (!check_block_eraser(flash, k, 0))
358 usable_erasefunctions++;
359 }
360 return usable_erasefunctions;
361}
362
Mark Marshallf20b7be2014-05-09 21:16:21 +0000363static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000364{
365 int ret = 0, failcount = 0;
366 unsigned int i;
367 for (i = 0; i < len; i++) {
368 if (wantbuf[i] != havebuf[i]) {
369 /* Only print the first failure. */
370 if (!failcount++)
371 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
372 start + i, wantbuf[i], havebuf[i]);
373 }
374 }
375 if (failcount) {
376 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
377 start, start + len - 1, failcount);
378 ret = -1;
379 }
380 return ret;
381}
382
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000383/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600384static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000385{
386 int ret;
387 uint8_t *cmpbuf = malloc(len);
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300388 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000389
390 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000391 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000392 exit(1);
393 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300394 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000395 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000396 free(cmpbuf);
397 return ret;
398}
399
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000400/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000401 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000402 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000403 * @start offset to the base address of the flash chip
404 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000405 * @return 0 for success, -1 for failure
406 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000407int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000408{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000409 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000410 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000411
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100412 if (start + len > flash->chip->total_size * 1024) {
413 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
414 " total_size 0x%x\n", __func__, start, len,
415 flash->chip->total_size * 1024);
416 return -1;
417 }
418
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000419 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000420 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000421 return -1;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000422 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000423
424 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000425 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000426 msg_gerr("Could not allocate memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000427 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000428 }
429
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100430 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000431 if (ret) {
432 msg_gerr("Verification impossible because read failed "
433 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000434 ret = -1;
435 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000436 }
437
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000438 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000439out_free:
440 free(readbuf);
441 return ret;
442}
443
Stefan Tauner02437452013-04-01 19:34:53 +0000444/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300445static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
446 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000447{
448 unsigned int i, j, limit;
449 for (j = 0; j < len / gran; j++) {
450 limit = min (gran, len - j * gran);
451 /* Are 'have' and 'want' identical? */
452 if (!memcmp(have + j * gran, want + j * gran, limit))
453 continue;
454 /* have needs to be in erased state. */
455 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300456 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000457 return 1;
458 }
459 return 0;
460}
461
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000462/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000463 * Check if the buffer @have can be programmed to the content of @want without
464 * erasing. This is only possible if all chunks of size @gran are either kept
465 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000466 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000467 * Warning: This function assumes that @have and @want point to naturally
468 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000469 *
470 * @have buffer with current content
471 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000472 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000473 * @gran write granularity (enum, not count)
474 * @return 0 if no erase is needed, 1 otherwise
475 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000476static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
477 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000478{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000479 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000480 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000481
482 switch (gran) {
483 case write_gran_1bit:
484 for (i = 0; i < len; i++)
485 if ((have[i] & want[i]) != want[i]) {
486 result = 1;
487 break;
488 }
489 break;
490 case write_gran_1byte:
491 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300492 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000493 result = 1;
494 break;
495 }
496 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000497 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300498 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000499 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000500 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300501 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000502 break;
503 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300504 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000505 break;
506 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300507 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000508 break;
509 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300510 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000511 break;
512 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300513 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000514 break;
515 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300516 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000517 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000518 case write_gran_1byte_implicit_erase:
519 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
520 result = 0;
521 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000522 default:
523 msg_cerr("%s: Unsupported granularity! Please report a bug at "
524 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000525 }
526 return result;
527}
528
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000529/**
530 * Check if the buffer @have needs to be programmed to get the content of @want.
531 * If yes, return 1 and fill in first_start with the start address of the
532 * write operation and first_len with the length of the first to-be-written
533 * chunk. If not, return 0 and leave first_start and first_len undefined.
534 *
535 * Warning: This function assumes that @have and @want point to naturally
536 * aligned regions.
537 *
538 * @have buffer with current content
539 * @want buffer with desired content
540 * @len length of the checked area
541 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000542 * @first_start offset of the first byte which needs to be written (passed in
543 * value is increased by the offset of the first needed write
544 * relative to have/want or unchanged if no write is needed)
545 * @return length of the first contiguous area which needs to be written
546 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000547 *
548 * FIXME: This function needs a parameter which tells it about coalescing
549 * in relation to the max write length of the programmer and the max write
550 * length of the chip.
551 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000552static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000553 unsigned int *first_start,
554 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000555{
Felix Singerf25447e2022-08-19 02:44:28 +0200556 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000557 unsigned int rel_start = 0, first_len = 0;
558 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000559
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000560 switch (gran) {
561 case write_gran_1bit:
562 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000563 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000564 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000565 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000566 case write_gran_128bytes:
567 stride = 128;
568 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000569 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000570 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000571 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000572 case write_gran_264bytes:
573 stride = 264;
574 break;
575 case write_gran_512bytes:
576 stride = 512;
577 break;
578 case write_gran_528bytes:
579 stride = 528;
580 break;
581 case write_gran_1024bytes:
582 stride = 1024;
583 break;
584 case write_gran_1056bytes:
585 stride = 1056;
586 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000587 default:
588 msg_cerr("%s: Unsupported granularity! Please report a bug at "
589 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000590 /* Claim that no write was needed. A write with unknown
591 * granularity is too dangerous to try.
592 */
593 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000594 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000595 for (i = 0; i < len / stride; i++) {
596 limit = min(stride, len - i * stride);
597 /* Are 'have' and 'want' identical? */
598 if (memcmp(have + i * stride, want + i * stride, limit)) {
599 if (!need_write) {
600 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200601 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000602 rel_start = i * stride;
603 }
604 } else {
605 if (need_write) {
606 /* First location where have and want
607 * do not differ anymore.
608 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000609 break;
610 }
611 }
612 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000613 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000614 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000615 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000616 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000617}
618
Martin Rothf6c1cb12022-03-15 10:55:25 -0600619/* Returns the number of buses commonly supported by the current programmer and flash chip where the latter
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000620 * can not be completely accessed due to size/address limits of the programmer. */
621unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000622{
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000623 unsigned int limitexceeded = 0;
624 uint32_t size = flash->chip->total_size * 1024;
625 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000626
627 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000628 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000629 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000630 "size %u kB of chipset/board/programmer "
631 "for %s interface, "
632 "probe/read/erase/write may fail. ", size / 1024,
633 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000634 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000635 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000636 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000637 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000638 "size %u kB of chipset/board/programmer "
639 "for %s interface, "
640 "probe/read/erase/write may fail. ", size / 1024,
641 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000642 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000643 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000644 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000645 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000646 "size %u kB of chipset/board/programmer "
647 "for %s interface, "
648 "probe/read/erase/write may fail. ", size / 1024,
649 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000650 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000651 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000652 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000653 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000654 "size %u kB of chipset/board/programmer "
655 "for %s interface, "
656 "probe/read/erase/write may fail. ", size / 1024,
657 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000658 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000659 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000660}
661
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000662void unmap_flash(struct flashctx *flash)
663{
664 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
665 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
666 flash->physical_registers = 0;
667 flash->virtual_registers = (chipaddr)ERROR_PTR;
668 }
669
670 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
671 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
672 flash->physical_memory = 0;
673 flash->virtual_memory = (chipaddr)ERROR_PTR;
674 }
675}
676
677int map_flash(struct flashctx *flash)
678{
679 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
680 flash->virtual_memory = (chipaddr)ERROR_PTR;
681 flash->virtual_registers = (chipaddr)ERROR_PTR;
682
683 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
684 * These are used for various probing-related hacks that would not map successfully anyway and should be
685 * removed ASAP. */
686 if (flash->chip->total_size == 0)
687 return 0;
688
689 const chipsize_t size = flash->chip->total_size * 1024;
690 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
691 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
692 if (addr == ERROR_PTR) {
693 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
694 flash->chip->name, PRIxPTR_WIDTH, base);
695 return 1;
696 }
697 flash->physical_memory = base;
698 flash->virtual_memory = (chipaddr)addr;
699
700 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
701 * completely different on some chips and programmers, or not mappable at all.
702 * Ignore these problems for now and always report success. */
703 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
704 base = 0xffffffff - size - 0x400000 + 1;
705 addr = programmer_map_flash_region("flash chip registers", base, size);
706 if (addr == ERROR_PTR) {
707 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
708 flash->chip->name, PRIxPTR_WIDTH, base);
709 return 0;
710 }
711 flash->physical_registers = base;
712 flash->virtual_registers = (chipaddr)addr;
713 }
714 return 0;
715}
716
Nico Huber2d625722016-05-03 10:48:02 +0200717/*
718 * Return a string corresponding to the bustype parameter.
719 * Memory is obtained with malloc() and must be freed with free() by the caller.
720 */
721char *flashbuses_to_text(enum chipbustype bustype)
722{
723 char *ret = calloc(1, 1);
724 /*
725 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
726 * will cease to exist and should be eliminated here as well.
727 */
728 if (bustype == BUS_NONSPI) {
729 ret = strcat_realloc(ret, "Non-SPI, ");
730 } else {
731 if (bustype & BUS_PARALLEL)
732 ret = strcat_realloc(ret, "Parallel, ");
733 if (bustype & BUS_LPC)
734 ret = strcat_realloc(ret, "LPC, ");
735 if (bustype & BUS_FWH)
736 ret = strcat_realloc(ret, "FWH, ");
737 if (bustype & BUS_SPI)
738 ret = strcat_realloc(ret, "SPI, ");
739 if (bustype & BUS_PROG)
740 ret = strcat_realloc(ret, "Programmer-specific, ");
741 if (bustype == BUS_NONE)
742 ret = strcat_realloc(ret, "None, ");
743 }
744 /* Kill last comma. */
745 ret[strlen(ret) - 2] = '\0';
746 ret = realloc(ret, strlen(ret) + 1);
747 return ret;
748}
749
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000750int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000751{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000752 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000753 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000754 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000755
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000756 for (chip = flashchips + startchip; chip && chip->name; chip++) {
757 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000758 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000759 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000760 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000761 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300762 /* Only probe for SPI25 chips by default. */
763 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
764 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000765 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
766 if (!chip->probe && !force) {
767 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000768 continue;
769 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000770
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000771 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200772 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000773 if (!flash->chip) {
774 msg_gerr("Out of memory!\n");
775 exit(1);
776 }
Angel Pons7e134562021-06-07 13:29:13 +0200777 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000778 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000779
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000780 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +0100781 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000782
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000783 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
784 * is only called with force=1 after normal probing failed.
785 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000786 if (force)
787 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000788
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000789 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000790 goto notfound;
791
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000792 /* If this is the first chip found, accept it.
793 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000794 * a non-generic match. SFDP and CFI are generic matches.
795 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000796 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000797 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000798 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000799 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000800 msg_cinfo("===\n"
801 "SFDP has autodetected a flash chip which is "
802 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000803 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000804 msg_cinfo("The standard operations read and "
805 "verify should work, but to support "
806 "erase, write and all other "
807 "possible features");
808 else
809 msg_cinfo("All standard operations (read, "
810 "verify, erase and write) should "
811 "work, but to support all possible "
812 "features");
813
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000814 msg_cinfo(" we need to add them manually.\n"
815 "You can help us by mailing us the output of the following command to "
816 "flashrom@flashrom.org:\n"
817 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
818 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000819 "===\n");
820 }
821
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000822 /* First flash chip detected on this bus. */
823 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000824 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000825 /* Not the first flash chip detected on this bus, but not a generic match either. */
826 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
827 break;
828 /* 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 +0000829notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000830 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000831 free(flash->chip);
832 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000833 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000834
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000835 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000836 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000837
Nico Huber7af0e792016-04-29 16:40:15 +0200838 /* Fill fallback layout covering the whole chip. */
839 struct single_layout *const fallback = &flash->fallback_layout;
840 fallback->base.entries = &fallback->entry;
841 fallback->base.num_entries = 1;
842 fallback->entry.start = 0;
843 fallback->entry.end = flash->chip->total_size * 1024 - 1;
844 fallback->entry.included = true;
Nico Huber70461a92019-06-15 14:56:19 +0200845 fallback->entry.name = strdup("complete flash");
846 if (!fallback->entry.name) {
847 msg_cerr("Failed to probe chip: %s\n", strerror(errno));
848 return -1;
849 }
Stefan Reinauer051e2362011-01-19 06:21:54 +0000850
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000851 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000852 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
853 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000854 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000855#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200856 if (programmer->map_flash_region == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000857 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
858 PRIxPTR_WIDTH, flash->physical_memory);
859 else
860#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200861 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000862
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000863 /* Flash registers may more likely not be mapped if the chip was forced.
864 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000865 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000866 if (flash->chip->printlock)
867 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000868
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000869 /* Get out of the way for later runs. */
870 unmap_flash(flash);
871
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000872 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000873 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000874}
875
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000876int read_buf_from_file(unsigned char *buf, unsigned long size,
877 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000878{
Nico Huber7562f7d2013-08-30 21:29:45 +0000879#ifdef __LIBPAYLOAD__
880 msg_gerr("Error: No file I/O support in libpayload\n");
881 return 1;
882#else
Stefan Tauner16687702015-12-25 21:59:45 +0000883 int ret = 0;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000884
Stefan Tauner16687702015-12-25 21:59:45 +0000885 FILE *image;
Daniel Campello8eaef7d2021-04-15 10:36:04 -0600886 if (!strcmp(filename, "-"))
887 image = fdopen(fileno(stdin), "rb");
888 else
889 image = fopen(filename, "rb");
890 if (image == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000891 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000892 return 1;
893 }
Stefan Tauner16687702015-12-25 21:59:45 +0000894
895 struct stat image_stat;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000896 if (fstat(fileno(image), &image_stat) != 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000897 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
Stefan Tauner16687702015-12-25 21:59:45 +0000898 ret = 1;
899 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000900 }
Daniel Campello8eaef7d2021-04-15 10:36:04 -0600901 if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +0000902 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 +0000903 (intmax_t)image_stat.st_size, size);
Stefan Tauner16687702015-12-25 21:59:45 +0000904 ret = 1;
905 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000906 }
Stefan Tauner16687702015-12-25 21:59:45 +0000907
908 unsigned long numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000909 if (numbytes != size) {
910 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
911 "wanted %ld!\n", numbytes, size);
Stefan Tauner16687702015-12-25 21:59:45 +0000912 ret = 1;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000913 }
Stefan Tauner16687702015-12-25 21:59:45 +0000914out:
915 (void)fclose(image);
916 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +0000917#endif
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000918}
919
Mark Marshallf20b7be2014-05-09 21:16:21 +0000920int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000921{
Nico Huber7562f7d2013-08-30 21:29:45 +0000922#ifdef __LIBPAYLOAD__
923 msg_gerr("Error: No file I/O support in libpayload\n");
924 return 1;
925#else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000926 FILE *image;
Stefan Tauner16687702015-12-25 21:59:45 +0000927 int ret = 0;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000928
929 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000930 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000931 return 1;
932 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +0000933 if ((image = fopen(filename, "wb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000934 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000935 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000936 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000937
Stefan Tauner16687702015-12-25 21:59:45 +0000938 unsigned long numbytes = fwrite(buf, 1, size, image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000939 if (numbytes != size) {
Stefan Tauner16687702015-12-25 21:59:45 +0000940 msg_gerr("Error: file %s could not be written completely.\n", filename);
941 ret = 1;
942 goto out;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000943 }
Stefan Tauner16687702015-12-25 21:59:45 +0000944 if (fflush(image)) {
945 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
946 ret = 1;
947 }
948 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
949#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
950 struct stat image_stat;
951 if (fstat(fileno(image), &image_stat) != 0) {
952 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
953 ret = 1;
954 goto out;
955 }
956 if (S_ISREG(image_stat.st_mode)) {
957 if (fsync(fileno(image))) {
958 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
959 ret = 1;
960 }
961 }
962#endif
963out:
964 if (fclose(image)) {
965 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
966 ret = 1;
967 }
968 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +0000969#endif
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000970}
971
Nico Huber899e4ec2016-04-29 18:39:01 +0200972static int read_by_layout(struct flashctx *, uint8_t *);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000973int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000974{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000975 unsigned long size = flash->chip->total_size * 1024;
Richard Hughes84b453e2018-12-19 15:30:39 +0000976 unsigned char *buf = calloc(size, sizeof(unsigned char));
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000977 int ret = 0;
978
979 msg_cinfo("Reading flash... ");
980 if (!buf) {
981 msg_gerr("Memory allocation failed!\n");
982 msg_cinfo("FAILED.\n");
983 return 1;
984 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000985 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000986 msg_cerr("No read function available for this flash chip.\n");
987 ret = 1;
988 goto out_free;
989 }
Nico Huber899e4ec2016-04-29 18:39:01 +0200990 if (read_by_layout(flash, buf)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000991 msg_cerr("Read operation failed!\n");
992 ret = 1;
993 goto out_free;
994 }
995
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000996 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000997out_free:
998 free(buf);
999 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1000 return ret;
1001}
1002
Stefan Tauner96658be2014-05-26 22:05:31 +00001003/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001004static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001005{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001006 int i, j, k;
1007 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +05301008 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001009
1010 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1011 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001012 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +05301013 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001014
1015 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1016 /* Blocks with zero size are bugs in flashchips.c. */
1017 if (eraser.eraseblocks[i].count &&
1018 !eraser.eraseblocks[i].size) {
1019 msg_gerr("ERROR: Flash chip %s erase function "
1020 "%i region %i has size 0. Please report"
1021 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001022 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001023 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001024 }
1025 /* Blocks with zero count are bugs in flashchips.c. */
1026 if (!eraser.eraseblocks[i].count &&
1027 eraser.eraseblocks[i].size) {
1028 msg_gerr("ERROR: Flash chip %s erase function "
1029 "%i region %i has count 0. Please report"
1030 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001031 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001032 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001033 }
1034 done += eraser.eraseblocks[i].count *
1035 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +05301036 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001037 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001038 /* Empty eraseblock definition with erase function. */
1039 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001040 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001041 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001042 if (!done)
1043 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001044 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001045 msg_gerr("ERROR: Flash chip %s erase function %i "
1046 "region walking resulted in 0x%06x bytes total,"
1047 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001048 " flashrom@flashrom.org\n", chip->name, k,
1049 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001050 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001051 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001052 if (!eraser.block_erase)
1053 continue;
1054 /* Check if there are identical erase functions for different
1055 * layouts. That would imply "magic" erase functions. The
1056 * easiest way to check this is with function pointers.
1057 */
Uwe Hermann43959702010-03-13 17:28:29 +00001058 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001059 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001060 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001061 msg_gerr("ERROR: Flash chip %s erase function "
1062 "%i and %i are identical. Please report"
1063 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001064 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001065 ret = 1;
1066 }
Uwe Hermann43959702010-03-13 17:28:29 +00001067 }
Aarya Chaumal478e1792022-06-04 01:34:44 +05301068 if(curr_eraseblock_count > prev_eraseblock_count)
1069 {
1070 msg_gerr("ERROR: Flash chip %s erase function %i is not "
1071 "in order. Please report a bug at flashrom@flashrom.org\n",
1072 chip->name, k);
1073 ret = 1;
1074 }
1075 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001076 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001077 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001078}
1079
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001080static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001081{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001082 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001083
1084 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1085 if (log)
1086 msg_cdbg("not defined. ");
1087 return 1;
1088 }
1089 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1090 if (log)
1091 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001092 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001093 return 1;
1094 }
1095 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1096 if (log)
1097 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001098 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001099 return 1;
1100 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001101 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001102 return 0;
1103}
1104
Nico Huber7af0e792016-04-29 16:40:15 +02001105/**
1106 * @brief Reads the included layout regions into a buffer.
1107 *
1108 * If there is no layout set in the given flash context, the whole chip will
1109 * be read.
1110 *
1111 * @param flashctx Flash context to be used.
1112 * @param buffer Buffer of full chip size to read into.
1113 * @return 0 on success,
1114 * 1 if any read fails.
1115 */
1116static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1117{
1118 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001119 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001120
Nico Huber5ca55232019-06-15 22:29:08 +02001121 while ((entry = layout_next_included(layout, entry))) {
1122 const chipoff_t region_start = entry->start;
1123 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001124
1125 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1126 return 1;
1127 }
1128 return 0;
1129}
1130
1131typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1132/**
1133 * @private
1134 *
1135 * For read-erase-write, `curcontents` and `newcontents` shall point
1136 * to buffers of the chip's size. Both are supposed to be prefilled
1137 * with at least the included layout regions of the current flash
1138 * contents (`curcontents`) and the data to be written to the flash
1139 * (`newcontents`).
1140 *
1141 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1142 *
1143 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1144 */
1145struct walk_info {
1146 uint8_t *curcontents;
1147 const uint8_t *newcontents;
1148 chipoff_t region_start;
1149 chipoff_t region_end;
1150 chipoff_t erase_start;
1151 chipoff_t erase_end;
1152};
1153/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1154typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1155
1156static int walk_eraseblocks(struct flashctx *const flashctx,
1157 struct walk_info *const info,
1158 const size_t erasefunction, const per_blockfn_t per_blockfn)
1159{
1160 int ret;
1161 size_t i, j;
1162 bool first = true;
1163 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1164
1165 info->erase_start = 0;
1166 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1167 /* count==0 for all automatically initialized array
1168 members so the loop below won't be executed for them. */
1169 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1170 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1171
1172 /* Skip any eraseblock that is completely outside the current region. */
1173 if (info->erase_end < info->region_start)
1174 continue;
1175 if (info->region_end < info->erase_start)
1176 break;
1177
1178 /* Print this for every block except the first one. */
1179 if (first)
1180 first = false;
1181 else
1182 msg_cdbg(", ");
1183 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1184
1185 ret = per_blockfn(flashctx, info, eraser->block_erase);
1186 if (ret)
1187 return ret;
1188 }
1189 if (info->region_end < info->erase_start)
1190 break;
1191 }
1192 msg_cdbg("\n");
1193 return 0;
1194}
1195
1196static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1197 const per_blockfn_t per_blockfn)
1198{
1199 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001200 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001201
1202 all_skipped = true;
1203 msg_cinfo("Erasing and writing flash chip... ");
1204
Nico Huber5ca55232019-06-15 22:29:08 +02001205 while ((entry = layout_next_included(layout, entry))) {
1206 info->region_start = entry->start;
1207 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001208
1209 size_t j;
1210 int error = 1; /* retry as long as it's 1 */
1211 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1212 if (j != 0)
1213 msg_cinfo("Looking for another erase function.\n");
1214 msg_cdbg("Trying erase function %zi... ", j);
1215 if (check_block_eraser(flashctx, j, 1))
1216 continue;
1217
1218 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1219 if (error != 1)
1220 break;
1221
1222 if (info->curcontents) {
1223 msg_cinfo("Reading current flash chip contents... ");
1224 if (read_by_layout(flashctx, info->curcontents)) {
1225 /* Now we are truly screwed. Read failed as well. */
1226 msg_cerr("Can't read anymore! Aborting.\n");
1227 /* We have no idea about the flash chip contents, so
1228 retrying with another erase function is pointless. */
1229 error = 2;
1230 break;
1231 }
1232 msg_cinfo("done. ");
1233 }
1234 }
1235 if (error == 1)
1236 msg_cinfo("No usable erase functions left.\n");
1237 if (error) {
1238 msg_cerr("FAILED!\n");
1239 return 1;
1240 }
1241 }
1242 if (all_skipped)
1243 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1244 msg_cinfo("Erase/write done.\n");
1245 return 0;
1246}
1247
1248static int erase_block(struct flashctx *const flashctx,
1249 const struct walk_info *const info, const erasefn_t erasefn)
1250{
1251 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001252 const bool region_unaligned = info->region_start > info->erase_start ||
1253 info->erase_end > info->region_end;
1254 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1255 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001256
Nico Huber6e61e0c2019-01-23 17:07:49 +01001257 /*
1258 * If the region is not erase-block aligned, merge current flash con-
1259 * tents into a new buffer `backup_contents`.
1260 */
1261 if (region_unaligned) {
1262 backup_contents = malloc(erase_len);
1263 erased_contents = malloc(erase_len);
1264 if (!backup_contents || !erased_contents) {
1265 msg_cerr("Out of memory!\n");
1266 ret = 1;
1267 goto _free_ret;
1268 }
1269 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1270 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1271
1272 msg_cdbg("R");
1273 /* Merge data preceding the current region. */
1274 if (info->region_start > info->erase_start) {
1275 const chipoff_t start = info->erase_start;
1276 const chipsize_t len = info->region_start - info->erase_start;
1277 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1278 msg_cerr("Can't read! Aborting.\n");
1279 goto _free_ret;
1280 }
1281 }
1282 /* Merge data following the current region. */
1283 if (info->erase_end > info->region_end) {
1284 const chipoff_t start = info->region_end + 1;
1285 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1286 const chipsize_t len = info->erase_end - info->region_end;
1287 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1288 msg_cerr("Can't read! Aborting.\n");
1289 goto _free_ret;
1290 }
1291 }
1292 }
1293
1294 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001295 all_skipped = false;
1296
1297 msg_cdbg("E");
1298 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001299 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001300 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1301 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001302 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001303 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001304
1305 if (region_unaligned) {
1306 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1307 /* get_next_write() sets starthere to a new value after the call. */
1308 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1309 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1310 if (!writecount++)
1311 msg_cdbg("W");
1312 /* Needs the partial write function signature. */
1313 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1314 info->erase_start + starthere, lenhere))
1315 goto _free_ret;
1316 starthere += lenhere;
1317 }
1318 }
1319
1320 ret = 0;
1321
1322_free_ret:
1323 free(erased_contents);
1324 free(backup_contents);
1325 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001326}
1327
1328/**
1329 * @brief Erases the included layout regions.
1330 *
1331 * If there is no layout set in the given flash context, the whole chip will
1332 * be erased.
1333 *
1334 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001335 * @return 0 on success,
1336 * 1 if all available erase functions failed.
1337 */
Nico Huber454f6132012-12-10 13:34:10 +00001338static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001339{
1340 struct walk_info info = { 0 };
1341 return walk_by_layout(flashctx, &info, &erase_block);
1342}
1343
1344static int read_erase_write_block(struct flashctx *const flashctx,
1345 const struct walk_info *const info, const erasefn_t erasefn)
1346{
1347 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1348 const bool region_unaligned = info->region_start > info->erase_start ||
1349 info->erase_end > info->region_end;
1350 const uint8_t *newcontents = NULL;
1351 int ret = 2;
1352
1353 /*
1354 * If the region is not erase-block aligned, merge current flash con-
1355 * tents into `info->curcontents` and a new buffer `newc`. The former
1356 * is necessary since we have no guarantee that the full erase block
1357 * was already read into `info->curcontents`. For the latter a new
1358 * buffer is used since `info->newcontents` might contain data for
1359 * other unaligned regions that touch this erase block too.
1360 */
1361 if (region_unaligned) {
1362 msg_cdbg("R");
1363 uint8_t *const newc = malloc(erase_len);
1364 if (!newc) {
1365 msg_cerr("Out of memory!\n");
1366 return 1;
1367 }
1368 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1369
1370 /* Merge data preceding the current region. */
1371 if (info->region_start > info->erase_start) {
1372 const chipoff_t start = info->erase_start;
1373 const chipsize_t len = info->region_start - info->erase_start;
1374 if (flashctx->chip->read(flashctx, newc, start, len)) {
1375 msg_cerr("Can't read! Aborting.\n");
1376 goto _free_ret;
1377 }
1378 memcpy(info->curcontents + start, newc, len);
1379 }
1380 /* Merge data following the current region. */
1381 if (info->erase_end > info->region_end) {
1382 const chipoff_t start = info->region_end + 1;
1383 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1384 const chipsize_t len = info->erase_end - info->region_end;
1385 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1386 msg_cerr("Can't read! Aborting.\n");
1387 goto _free_ret;
1388 }
1389 memcpy(info->curcontents + start, newc + rel_start, len);
1390 }
1391
1392 newcontents = newc;
1393 } else {
1394 newcontents = info->newcontents + info->erase_start;
1395 }
1396
1397 ret = 1;
1398 bool skipped = true;
1399 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001400 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001401 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1402 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001403 if (erase_block(flashctx, info, erasefn))
1404 goto _free_ret;
1405 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001406 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001407 skipped = false;
1408 }
1409
1410 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1411 /* get_next_write() sets starthere to a new value after the call. */
1412 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1413 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1414 if (!writecount++)
1415 msg_cdbg("W");
1416 /* Needs the partial write function signature. */
1417 if (flashctx->chip->write(flashctx, newcontents + starthere,
1418 info->erase_start + starthere, lenhere))
1419 goto _free_ret;
1420 starthere += lenhere;
1421 skipped = false;
1422 }
1423 if (skipped)
1424 msg_cdbg("S");
1425 else
1426 all_skipped = false;
1427
1428 /* Update curcontents, other regions with overlapping erase blocks
1429 might rely on this. */
1430 memcpy(curcontents, newcontents, erase_len);
1431 ret = 0;
1432
1433_free_ret:
1434 if (region_unaligned)
1435 free((void *)newcontents);
1436 return ret;
1437}
1438
1439/**
1440 * @brief Writes the included layout regions from a given image.
1441 *
1442 * If there is no layout set in the given flash context, the whole image
1443 * will be written.
1444 *
1445 * @param flashctx Flash context to be used.
1446 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1447 * @param newcontents The new image to be written.
1448 * @return 0 on success,
1449 * 1 if anything has gone wrong.
1450 */
Nico Huber454f6132012-12-10 13:34:10 +00001451static int write_by_layout(struct flashctx *const flashctx,
1452 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001453{
1454 struct walk_info info;
1455 info.curcontents = curcontents;
1456 info.newcontents = newcontents;
1457 return walk_by_layout(flashctx, &info, read_erase_write_block);
1458}
1459
1460/**
1461 * @brief Compares the included layout regions with content from a buffer.
1462 *
1463 * If there is no layout set in the given flash context, the whole chip's
1464 * contents will be compared.
1465 *
1466 * @param flashctx Flash context to be used.
1467 * @param curcontents A buffer of full chip size to read current chip contents into.
1468 * @param newcontents The new image to compare to.
1469 * @return 0 on success,
1470 * 1 if reading failed,
1471 * 3 if the contents don't match.
1472 */
Nico Huber454f6132012-12-10 13:34:10 +00001473static int verify_by_layout(struct flashctx *const flashctx,
1474 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001475{
1476 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001477 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001478
Nico Huber5ca55232019-06-15 22:29:08 +02001479 while ((entry = layout_next_included(layout, entry))) {
1480 const chipoff_t region_start = entry->start;
1481 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001482
1483 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1484 return 1;
1485 if (compare_range(newcontents + region_start, curcontents + region_start,
1486 region_start, region_len))
1487 return 3;
1488 }
1489 return 0;
1490}
1491
Stefan Tauner136388f2013-07-15 10:47:53 +00001492static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001493{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001494 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001495#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001496 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001497 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Angel Pons1900e1d2021-07-02 12:42:23 +02001498 "chip. Please report this to the mailing list at flashrom@flashrom.org or on\n"
1499 "IRC (see https://www.flashrom.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001500 "-------------------------------------------------------------------------------\n"
1501 "You may now reboot or simply leave the machine running.\n");
1502 else
1503#endif
1504 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1505 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
Angel Pons1900e1d2021-07-02 12:42:23 +02001506 "please report this to the mailing list at flashrom@flashrom.org or on IRC (see\n"
1507 "https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001508}
1509
Stefan Tauner136388f2013-07-15 10:47:53 +00001510static void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001511{
Stefan Tauner136388f2013-07-15 10:47:53 +00001512 msg_gerr("Your flash chip is in an unknown state.\n");
1513#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001514 if (programmer == &programmer_internal)
Angel Pons1900e1d2021-07-02 12:42:23 +02001515 msg_gerr("Get help on IRC (see https://www.flashrom.org/Contact) or mail\n"
1516 "flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!"
Stefan Tauner136388f2013-07-15 10:47:53 +00001517 "-------------------------------------------------------------------------------\n"
1518 "DO NOT REBOOT OR POWEROFF!\n");
1519 else
1520#endif
Angel Pons1900e1d2021-07-02 12:42:23 +02001521 msg_gerr("Please report this to the mailing list at flashrom@flashrom.org or\n"
1522 "on IRC (see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001523}
1524
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001525void list_programmers_linebreak(int startcol, int cols, int paren)
1526{
1527 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001528 int pnamelen;
1529 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001530 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001531 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001532
Thomas Heijligend45cb592021-05-19 14:12:18 +02001533 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001534 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001535 pnamelen = strlen(pname);
1536 if (remaining - pnamelen - 2 < 0) {
1537 if (firstline)
1538 firstline = 0;
1539 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001540 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001541 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001542 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001543 remaining = cols - startcol;
1544 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001545 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001546 remaining--;
1547 }
1548 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001549 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001550 remaining--;
1551 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001552 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001553 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001554 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001555 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001556 remaining--;
1557 } else {
1558 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001559 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001560 }
1561 }
1562}
1563
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001564static void print_sysinfo(void)
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001565{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001566#if IS_WINDOWS
Angel Pons7e134562021-06-07 13:29:13 +02001567 SYSTEM_INFO si = { 0 };
1568 OSVERSIONINFOEX osvi = { 0 };
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001569
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001570 msg_ginfo(" on Windows");
1571 /* Tell Windows which version of the structure we want. */
1572 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1573 if (GetVersionEx((OSVERSIONINFO*) &osvi))
1574 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
1575 else
1576 msg_ginfo(" unknown version");
1577 GetSystemInfo(&si);
1578 switch (si.wProcessorArchitecture) {
1579 case PROCESSOR_ARCHITECTURE_AMD64:
1580 msg_ginfo(" (x86_64)");
1581 break;
1582 case PROCESSOR_ARCHITECTURE_INTEL:
1583 msg_ginfo(" (x86)");
1584 break;
1585 default:
1586 msg_ginfo(" (unknown arch)");
1587 break;
1588 }
1589#elif HAVE_UTSNAME == 1
1590 struct utsname osinfo;
1591
1592 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001593 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1594 osinfo.machine);
1595#else
1596 msg_ginfo(" on unknown machine");
1597#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001598}
1599
1600void print_buildinfo(void)
1601{
1602 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001603#if NEED_PCI == 1
1604#ifdef PCILIB_VERSION
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001605 msg_gdbg(" libpci %s,", PCILIB_VERSION);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001606#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001607 msg_gdbg(" unknown PCI library,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001608#endif
1609#endif
1610#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001611 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001612#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001613 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001614#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001615 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001616#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001617#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001618 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001619#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001620 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001621#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001622 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001623#endif
1624#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001625 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001626#endif
1627#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001628 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001629#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001630 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001631#else
1632#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001633#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001634 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001635}
1636
Bernhard Walle201bde32008-01-21 15:24:22 +00001637void print_version(void)
1638{
Stefan Tauner76347082016-11-27 17:45:49 +01001639 msg_ginfo("flashrom %s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001640 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001641 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00001642}
1643
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001644void print_banner(void)
1645{
1646 msg_ginfo("flashrom is free software, get the source code at "
Stefan Tauner4c723152016-01-14 22:47:55 +00001647 "https://flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001648 msg_ginfo("\n");
1649}
1650
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001651int selfcheck(void)
1652{
Stefan Tauner96658be2014-05-26 22:05:31 +00001653 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001654 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001655
Thomas Heijligend45cb592021-05-19 14:12:18 +02001656 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001657 const struct programmer_entry *const p = programmer_table[i];
1658 if (p == NULL) {
1659 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1660 ret = 1;
1661 continue;
1662 }
1663 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001664 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1665 ret = 1;
1666 /* This might hide other problems with this programmer, but allows for better error
1667 * messages below without jumping through hoops. */
1668 continue;
1669 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001670 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001671 case USB:
1672 case PCI:
1673 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001674 if (p->devs.note == NULL) {
1675 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001676 break; /* This one has its device list stored separately. */
1677 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001678 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001679 ret = 1;
1680 }
1681 break;
1682 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001683 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001684 ret = 1;
1685 break;
1686 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001687 if (p->init == NULL) {
1688 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001689 ret = 1;
1690 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001691 if (p->delay == NULL) {
1692 msg_gerr("Programmer %s does not have a valid delay function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001693 ret = 1;
1694 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001695 if (p->map_flash_region == NULL) {
1696 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001697 ret = 1;
1698 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001699 if (p->unmap_flash_region == NULL) {
1700 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001701 ret = 1;
1702 }
1703 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001704
1705 /* It would be favorable if we could check for the correct layout (especially termination) of various
1706 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1707 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1708 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1709 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1710 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001711 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001712 msg_gerr("Flashchips table miscompilation!\n");
1713 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001714 } else {
1715 for (i = 0; i < flashchips_size - 1; i++) {
1716 const struct flashchip *chip = &flashchips[i];
1717 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1718 ret = 1;
1719 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
1720 "Please report a bug at flashrom@flashrom.org\n", i,
1721 chip->name == NULL ? "unnamed" : chip->name);
1722 }
1723 if (selfcheck_eraseblocks(chip)) {
1724 ret = 1;
1725 }
1726 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001727 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001728
Stefan Tauner600576b2014-06-12 22:57:36 +00001729#if CONFIG_INTERNAL == 1
1730 ret |= selfcheck_board_enables();
1731#endif
1732
Stefan Tauner96658be2014-05-26 22:05:31 +00001733 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001734 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001735}
1736
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001737/* FIXME: This function signature needs to be improved once prepare_flash_access()
1738 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001739 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001740static int chip_safety_check(const struct flashctx *flash, int force,
1741 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001742{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001743 const struct flashchip *chip = flash->chip;
1744
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001745 if (!programmer_may_write && (write_it || erase_it)) {
1746 msg_perr("Write/erase is not working yet on your programmer in "
1747 "its current configuration.\n");
1748 /* --force is the wrong approach, but it's the best we can do
1749 * until the generic programmer parameter parser is merged.
1750 */
1751 if (!force)
1752 return 1;
1753 msg_cerr("Continuing anyway.\n");
1754 }
1755
1756 if (read_it || erase_it || write_it || verify_it) {
1757 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001758 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001759 msg_cerr("Read is not working on this chip. ");
1760 if (!force)
1761 return 1;
1762 msg_cerr("Continuing anyway.\n");
1763 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001764 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001765 msg_cerr("flashrom has no read function for this "
1766 "flash chip.\n");
1767 return 1;
1768 }
1769 }
1770 if (erase_it || write_it) {
1771 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001772 if (chip->tested.erase == NA) {
1773 msg_cerr("Erase is not possible on this chip.\n");
1774 return 1;
1775 }
1776 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001777 msg_cerr("Erase is not working on this chip. ");
1778 if (!force)
1779 return 1;
1780 msg_cerr("Continuing anyway.\n");
1781 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001782 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001783 msg_cerr("flashrom has no erase function for this "
1784 "flash chip.\n");
1785 return 1;
1786 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001787 }
1788 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001789 if (chip->tested.write == NA) {
1790 msg_cerr("Write is not possible on this chip.\n");
1791 return 1;
1792 }
1793 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001794 msg_cerr("Write is not working on this chip. ");
1795 if (!force)
1796 return 1;
1797 msg_cerr("Continuing anyway.\n");
1798 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001799 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001800 msg_cerr("flashrom has no write function for this "
1801 "flash chip.\n");
1802 return 1;
1803 }
1804 }
1805 return 0;
1806}
1807
Nico Huber305f4172013-06-14 11:55:26 +02001808int prepare_flash_access(struct flashctx *const flash,
1809 const bool read_it, const bool write_it,
1810 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001811{
1812 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1813 msg_cerr("Aborting.\n");
1814 return 1;
1815 }
1816
1817 if (flash->layout == get_global_layout() && normalize_romentries(flash)) {
1818 msg_cerr("Requested regions can not be handled. Aborting.\n");
1819 return 1;
1820 }
1821
1822 if (map_flash(flash) != 0)
1823 return 1;
1824
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001825 /* Initialize chip_restore_fn_count before chip unlock calls. */
1826 flash->chip_restore_fn_count = 0;
1827
Nico Huber454f6132012-12-10 13:34:10 +00001828 /* Given the existence of read locks, we want to unlock for read,
1829 erase and write. */
1830 if (flash->chip->unlock)
1831 flash->chip->unlock(flash);
1832
Nico Huberf43c6542017-10-14 17:47:28 +02001833 flash->address_high_byte = -1;
1834 flash->in_4ba_mode = false;
1835
Nico Huberdc5af542018-12-22 16:54:59 +01001836 /* Be careful about 4BA chips and broken masters */
1837 if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
1838 /* If we can't use native instructions, bail out */
1839 if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
1840 || !spi_master_4ba(flash)) {
1841 msg_cerr("Programmer doesn't support this chip. Aborting.\n");
1842 return 1;
1843 }
1844 }
1845
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001846 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01001847 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01001848 int ret;
1849 if (spi_master_4ba(flash))
1850 ret = spi_enter_4ba(flash);
1851 else
1852 ret = spi_exit_4ba(flash);
1853 if (ret) {
1854 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001855 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02001856 }
Boris Baykov99127182016-06-11 18:29:00 +02001857 }
1858
Nico Huber454f6132012-12-10 13:34:10 +00001859 return 0;
1860}
1861
Nico Huber305f4172013-06-14 11:55:26 +02001862void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001863{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001864 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001865 unmap_flash(flash);
1866}
1867
1868/**
1869 * @addtogroup flashrom-flash
1870 * @{
1871 */
1872
1873/**
1874 * @brief Erase the specified ROM chip.
1875 *
1876 * If a layout is set in the given flash context, only included regions
1877 * will be erased.
1878 *
1879 * @param flashctx The context of the flash chip to erase.
1880 * @return 0 on success.
1881 */
1882int flashrom_flash_erase(struct flashctx *const flashctx)
1883{
1884 if (prepare_flash_access(flashctx, false, false, true, false))
1885 return 1;
1886
1887 const int ret = erase_by_layout(flashctx);
1888
1889 finalize_flash_access(flashctx);
1890
1891 return ret;
1892}
1893
1894/** @} */ /* end flashrom-flash */
1895
1896/**
1897 * @defgroup flashrom-ops Operations
1898 * @{
1899 */
1900
1901/**
1902 * @brief Read the current image from the specified ROM chip.
1903 *
1904 * If a layout is set in the specified flash context, only included regions
1905 * will be read.
1906 *
1907 * @param flashctx The context of the flash chip.
1908 * @param buffer Target buffer to write image to.
1909 * @param buffer_len Size of target buffer in bytes.
1910 * @return 0 on success,
1911 * 2 if buffer_len is too short for the flash chip's contents,
1912 * or 1 on any other failure.
1913 */
1914int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
1915{
1916 const size_t flash_size = flashctx->chip->total_size * 1024;
1917
1918 if (flash_size > buffer_len)
1919 return 2;
1920
1921 if (prepare_flash_access(flashctx, true, false, false, false))
1922 return 1;
1923
1924 msg_cinfo("Reading flash... ");
1925
1926 int ret = 1;
1927 if (read_by_layout(flashctx, buffer)) {
1928 msg_cerr("Read operation failed!\n");
1929 msg_cinfo("FAILED.\n");
1930 goto _finalize_ret;
1931 }
1932 msg_cinfo("done.\n");
1933 ret = 0;
1934
1935_finalize_ret:
1936 finalize_flash_access(flashctx);
1937 return ret;
1938}
1939
1940static void combine_image_by_layout(const struct flashctx *const flashctx,
1941 uint8_t *const newcontents, const uint8_t *const oldcontents)
1942{
1943 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001944 const struct romentry *included;
1945 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001946
Nico Huber3d7b1e32018-12-22 00:53:14 +01001947 while ((included = layout_next_included_region(layout, start))) {
1948 if (included->start > start) {
1949 /* copy everything up to the start of this included region */
1950 memcpy(newcontents + start, oldcontents + start, included->start - start);
1951 }
1952 /* skip this included region */
1953 start = included->end + 1;
1954 if (start == 0)
1955 return;
Nico Huber454f6132012-12-10 13:34:10 +00001956 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001957
1958 /* copy the rest of the chip */
1959 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1960 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001961}
1962
1963/**
1964 * @brief Write the specified image to the ROM chip.
1965 *
1966 * If a layout is set in the specified flash context, only erase blocks
1967 * containing included regions will be touched.
1968 *
1969 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001970 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001971 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001972 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001973 * @return 0 on success,
1974 * 4 if buffer_len doesn't match the size of the flash chip,
1975 * 3 if write was tried but nothing has changed,
1976 * 2 if write failed and flash contents changed,
1977 * or 1 on any other failure.
1978 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001979int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
1980 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001981{
1982 const size_t flash_size = flashctx->chip->total_size * 1024;
1983 const bool verify_all = flashctx->flags.verify_whole_chip;
1984 const bool verify = flashctx->flags.verify_after_write;
1985
1986 if (buffer_len != flash_size)
1987 return 4;
1988
1989 int ret = 1;
1990
1991 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001992 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001993 uint8_t *const curcontents = malloc(flash_size);
1994 uint8_t *oldcontents = NULL;
1995 if (verify_all)
1996 oldcontents = malloc(flash_size);
1997 if (!curcontents || (verify_all && !oldcontents)) {
1998 msg_gerr("Out of memory!\n");
1999 goto _free_ret;
2000 }
2001
2002#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02002003 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00002004 if (flashctx->flags.force_boardmismatch) {
2005 msg_pinfo("Proceeding anyway because user forced us to.\n");
2006 } else {
2007 msg_perr("Aborting. You can override this with "
2008 "-p internal:boardmismatch=force.\n");
2009 goto _free_ret;
2010 }
2011 }
2012#endif
2013
2014 if (prepare_flash_access(flashctx, false, true, false, verify))
2015 goto _free_ret;
2016
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002017 /* If given, assume flash chip contains same data as `refcontents`. */
2018 if (refcontents) {
2019 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
2020 memcpy(curcontents, refcontents, flash_size);
2021 if (oldcontents)
2022 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00002023 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002024 /*
2025 * Read the whole chip to be able to check whether regions need to be
2026 * erased and to give better diagnostics in case write fails.
2027 * The alternative is to read only the regions which are to be
2028 * preserved, but in that case we might perform unneeded erase which
2029 * takes time as well.
2030 */
2031 msg_cinfo("Reading old flash chip contents... ");
2032 if (verify_all) {
2033 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
2034 msg_cinfo("FAILED.\n");
2035 goto _finalize_ret;
2036 }
2037 memcpy(curcontents, oldcontents, flash_size);
2038 } else {
2039 if (read_by_layout(flashctx, curcontents)) {
2040 msg_cinfo("FAILED.\n");
2041 goto _finalize_ret;
2042 }
Nico Huber454f6132012-12-10 13:34:10 +00002043 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002044 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00002045 }
Nico Huber454f6132012-12-10 13:34:10 +00002046
2047 if (write_by_layout(flashctx, curcontents, newcontents)) {
2048 msg_cerr("Uh oh. Erase/write failed. ");
2049 ret = 2;
2050 if (verify_all) {
2051 msg_cerr("Checking if anything has changed.\n");
2052 msg_cinfo("Reading current flash chip contents... ");
2053 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
2054 msg_cinfo("done.\n");
2055 if (!memcmp(oldcontents, curcontents, flash_size)) {
2056 nonfatal_help_message();
2057 goto _finalize_ret;
2058 }
2059 msg_cerr("Apparently at least some data has changed.\n");
2060 } else
2061 msg_cerr("Can't even read anymore!\n");
2062 emergency_help_message();
2063 goto _finalize_ret;
2064 } else {
2065 msg_cerr("\n");
2066 }
2067 emergency_help_message();
2068 goto _finalize_ret;
2069 }
2070
2071 /* Verify only if we actually changed something. */
2072 if (verify && !all_skipped) {
2073 const struct flashrom_layout *const layout_bak = flashctx->layout;
2074
2075 msg_cinfo("Verifying flash... ");
2076
2077 /* Work around chips which need some time to calm down. */
2078 programmer_delay(1000*1000);
2079
2080 if (verify_all) {
2081 combine_image_by_layout(flashctx, newcontents, oldcontents);
2082 flashctx->layout = NULL;
2083 }
2084 ret = verify_by_layout(flashctx, curcontents, newcontents);
2085 flashctx->layout = layout_bak;
2086 /* If we tried to write, and verification now fails, we
2087 might have an emergency situation. */
2088 if (ret)
2089 emergency_help_message();
2090 else
2091 msg_cinfo("VERIFIED.\n");
2092 } else {
2093 /* We didn't change anything. */
2094 ret = 0;
2095 }
2096
2097_finalize_ret:
2098 finalize_flash_access(flashctx);
2099_free_ret:
2100 free(oldcontents);
2101 free(curcontents);
2102 return ret;
2103}
2104
2105/**
2106 * @brief Verify the ROM chip's contents with the specified image.
2107 *
2108 * If a layout is set in the specified flash context, only included regions
2109 * will be verified.
2110 *
2111 * @param flashctx The context of the flash chip.
2112 * @param buffer Source buffer to verify with.
2113 * @param buffer_len Size of source buffer in bytes.
2114 * @return 0 on success,
2115 * 3 if the chip's contents don't match,
2116 * 2 if buffer_len doesn't match the size of the flash chip,
2117 * or 1 on any other failure.
2118 */
2119int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
2120{
2121 const size_t flash_size = flashctx->chip->total_size * 1024;
2122
2123 if (buffer_len != flash_size)
2124 return 2;
2125
2126 const uint8_t *const newcontents = buffer;
2127 uint8_t *const curcontents = malloc(flash_size);
2128 if (!curcontents) {
2129 msg_gerr("Out of memory!\n");
2130 return 1;
2131 }
2132
2133 int ret = 1;
2134
2135 if (prepare_flash_access(flashctx, false, false, false, true))
2136 goto _free_ret;
2137
2138 msg_cinfo("Verifying flash... ");
2139 ret = verify_by_layout(flashctx, curcontents, newcontents);
2140 if (!ret)
2141 msg_cinfo("VERIFIED.\n");
2142
2143 finalize_flash_access(flashctx);
2144_free_ret:
2145 free(curcontents);
2146 return ret;
2147}
2148
2149/** @} */ /* end flashrom-ops */
Nico Huber899e4ec2016-04-29 18:39:01 +02002150
2151int do_read(struct flashctx *const flash, const char *const filename)
2152{
2153 if (prepare_flash_access(flash, true, false, false, false))
2154 return 1;
2155
2156 const int ret = read_flash_to_file(flash, filename);
2157
2158 finalize_flash_access(flash);
2159
2160 return ret;
2161}
2162
2163int do_erase(struct flashctx *const flash)
2164{
2165 const int ret = flashrom_flash_erase(flash);
2166
2167 /*
2168 * FIXME: Do we really want the scary warning if erase failed?
2169 * After all, after erase the chip is either blank or partially
2170 * blank or it has the old contents. A blank chip won't boot,
2171 * so if the user wanted erase and reboots afterwards, the user
2172 * knows very well that booting won't work.
2173 */
2174 if (ret)
2175 emergency_help_message();
2176
2177 return ret;
2178}
2179
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002180int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
Nico Huber899e4ec2016-04-29 18:39:01 +02002181{
2182 const size_t flash_size = flash->chip->total_size * 1024;
2183 int ret = 1;
2184
2185 uint8_t *const newcontents = malloc(flash_size);
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002186 uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
2187
2188 if (!newcontents || (referencefile && !refcontents)) {
Nico Huber899e4ec2016-04-29 18:39:01 +02002189 msg_gerr("Out of memory!\n");
2190 goto _free_ret;
2191 }
2192
2193 if (read_buf_from_file(newcontents, flash_size, filename))
2194 goto _free_ret;
2195
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002196 if (referencefile) {
2197 if (read_buf_from_file(refcontents, flash_size, referencefile))
2198 goto _free_ret;
2199 }
2200
2201 ret = flashrom_image_write(flash, newcontents, flash_size, refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002202
2203_free_ret:
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002204 free(refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002205 free(newcontents);
2206 return ret;
2207}
2208
2209int do_verify(struct flashctx *const flash, const char *const filename)
2210{
2211 const size_t flash_size = flash->chip->total_size * 1024;
2212 int ret = 1;
2213
2214 uint8_t *const newcontents = malloc(flash_size);
2215 if (!newcontents) {
2216 msg_gerr("Out of memory!\n");
2217 goto _free_ret;
2218 }
2219
2220 if (read_buf_from_file(newcontents, flash_size, filename))
2221 goto _free_ret;
2222
2223 ret = flashrom_image_verify(flash, newcontents, flash_size);
2224
2225_free_ret:
2226 free(newcontents);
2227 return ret;
2228}