blob: 03457d3fcad2c2b9d1b096ee9dd6133cce4683c9 [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"
Thomas Heijligen74b4aa02021-12-14 17:52:30 +010041#include "hwaccess_physmap.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010042#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000043
Mathias Krausea60faab2011-01-17 07:50:42 +000044const char flashrom_version[] = FLASHROM_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000045const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000046
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020047static const struct programmer_entry *programmer = NULL;
Nico Huber6a2ebeb2022-08-26 11:36:48 +020048static char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000049
Uwe Hermann48ec1b12010-08-08 17:01:18 +000050/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000051 * Programmers supporting multiple buses can have differing size limits on
52 * each bus. Store the limits for each bus in a common struct.
53 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000054struct decode_sizes max_rom_decode;
55
56/* If nonzero, used as the start address of bottom-aligned flash. */
57unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000058
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000059/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020060bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000061
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000062#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000063static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000064/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000065static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000066 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000067 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000068} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000069/* Initialize to 0 to make sure nobody registers a shutdown function before
70 * programmer init.
71 */
Felix Singerf25447e2022-08-19 02:44:28 +020072static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000073
Stefan Taunerc4f44df2013-08-12 22:58:43 +000074/* Did we change something or was every erase/write skipped (if any)? */
75static bool all_skipped = true;
76
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000077static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000078
Stefan Tauner2a1ed772014-08-31 00:09:21 +000079int shutdown_free(void *data)
80{
81 free(data);
82 return 0;
83}
84
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000085/* Register a function to be executed on programmer shutdown.
86 * The advantage over atexit() is that you can supply a void pointer which will
87 * be used as parameter to the registered function upon programmer shutdown.
88 * This pointer can point to arbitrary data used by said function, e.g. undo
89 * information for GPIO settings etc. If unneeded, set data=NULL.
90 * Please note that the first (void *data) belongs to the function signature of
91 * the function passed as first parameter.
92 */
David Hendricks8bb20212011-06-14 01:35:36 +000093int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000094{
95 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000096 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000097 SHUTDOWN_MAXFN);
98 return 1;
99 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000100 if (!may_register_shutdown) {
101 msg_perr("Tried to register a shutdown function before "
102 "programmer init.\n");
103 return 1;
104 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000105 shutdown_fn[shutdown_fn_count].func = function;
106 shutdown_fn[shutdown_fn_count].data = data;
107 shutdown_fn_count++;
108
109 return 0;
110}
111
Nikolai Artemiev4ad48642020-11-05 13:54:27 +1100112int register_chip_restore(chip_restore_fn_cb_t func,
113 struct flashctx *flash, uint8_t status)
114{
115 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
116 msg_perr("Tried to register more than %i chip restore"
117 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
118 return 1;
119 }
120 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
121 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
122 flash->chip_restore_fn_count++;
123
124 return 0;
125}
126
127static int deregister_chip_restore(struct flashctx *flash)
128{
129 int rc = 0;
130
131 while (flash->chip_restore_fn_count > 0) {
132 int i = --flash->chip_restore_fn_count;
133 rc |= flash->chip_restore_fn[i].func(
134 flash, flash->chip_restore_fn[i].status);
135 }
136
137 return rc;
138}
139
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200140int programmer_init(const struct programmer_entry *prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000141{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000142 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000143
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200144 if (prog == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000145 msg_perr("Invalid programmer specified!\n");
146 return -1;
147 }
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200148 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000149 /* Initialize all programmer specific data. */
150 /* Default to unlimited decode sizes. */
151 max_rom_decode = (const struct decode_sizes) {
152 .parallel = 0xffffffff,
153 .lpc = 0xffffffff,
154 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000155 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000156 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000157 /* Default to top aligned flash at 4 GB. */
158 flashbase = 0;
159 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200160 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000161 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200162 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000163
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200164 if (param) {
165 programmer_param = strdup(param);
166 if (!programmer_param) {
167 msg_perr("Out of memory!\n");
168 return ERROR_FATAL;
169 }
170 } else {
171 programmer_param = NULL;
172 }
173
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200174 msg_pdbg("Initializing %s programmer\n", programmer->name);
175 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000176 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000177 if (ret != 0) {
178 /* It is quite possible that any unhandled programmer parameter would have been valid,
179 * but an error in actual programmer init happened before the parameter was evaluated.
180 */
181 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
182 programmer_param);
183 } else {
184 /* Actual programmer init was successful, but the user specified an invalid or unusable
185 * (for the current programmer configuration) parameter.
186 */
187 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
188 msg_perr("Aborting.\n");
189 ret = ERROR_FATAL;
190 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000191 }
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200192 free(programmer_param);
193 programmer_param = NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000194 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000195}
196
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000197/** Calls registered shutdown functions and resets internal programmer-related variables.
198 * Calling it is safe even without previous initialization, but further interactions with programmer support
199 * require a call to programmer_init() (afterwards).
200 *
201 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000202int programmer_shutdown(void)
203{
David Hendricks8bb20212011-06-14 01:35:36 +0000204 int ret = 0;
205
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000206 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200207 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000208 while (shutdown_fn_count > 0) {
209 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000210 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000211 }
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000212 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000213
David Hendricks8bb20212011-06-14 01:35:36 +0000214 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000215}
216
Stefan Tauner305e0b92013-07-17 23:46:44 +0000217void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000218{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200219 void *ret = programmer->map_flash_region(descr, phys_addr, len);
Stefan Tauner26e7a152013-09-13 17:21:05 +0000220 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
221 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
222 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000223}
224
225void programmer_unmap_flash_region(void *virt_addr, size_t len)
226{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200227 programmer->unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000228 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000229}
230
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000231void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000232{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000233 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000234}
235
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000236void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000237{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000238 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000239}
240
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000241void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000242{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000243 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000244}
245
Mark Marshallf20b7be2014-05-09 21:16:21 +0000246void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000247{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000248 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000249}
250
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000251uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000252{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000253 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000254}
255
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000256uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000257{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000258 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000259}
260
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000261uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000262{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000263 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000264}
265
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000266void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
267 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000268{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000269 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000270}
271
Stefan Taunerf80419c2014-05-02 15:41:42 +0000272void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000273{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000274 if (usecs > 0)
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200275 programmer->delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000276}
277
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000278int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
279 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000280{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000281 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000282
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000283 return 0;
284}
285
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000286/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000287 * It will look for needle with a subsequent '=' in haystack, return a copy of
288 * needle and remove everything from the first occurrence of needle to the next
289 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000290 */
Nico Huber6a2ebeb2022-08-26 11:36:48 +0200291static char *extract_param(char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000292{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000293 char *param_pos, *opt_pos, *rest;
294 char *opt = NULL;
295 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000296 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000297
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000298 needlelen = strlen(needle);
299 if (!needlelen) {
300 msg_gerr("%s: empty needle! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000301 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000302 return NULL;
303 }
304 /* No programmer parameters given. */
305 if (*haystack == NULL)
306 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000307 param_pos = strstr(*haystack, needle);
308 do {
309 if (!param_pos)
310 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000311 /* Needle followed by '='? */
312 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000313 /* Beginning of the string? */
314 if (param_pos == *haystack)
315 break;
316 /* After a delimiter? */
317 if (strchr(delim, *(param_pos - 1)))
318 break;
319 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000320 /* Continue searching. */
321 param_pos++;
322 param_pos = strstr(param_pos, needle);
323 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000324
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000325 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000326 /* Get the string after needle and '='. */
327 opt_pos = param_pos + needlelen + 1;
328 optlen = strcspn(opt_pos, delim);
329 /* Return an empty string if the parameter was empty. */
330 opt = malloc(optlen + 1);
331 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000332 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000333 exit(1);
334 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000335 strncpy(opt, opt_pos, optlen);
336 opt[optlen] = '\0';
337 rest = opt_pos + optlen;
338 /* Skip all delimiters after the current parameter. */
339 rest += strspn(rest, delim);
340 memmove(param_pos, rest, strlen(rest) + 1);
341 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000342 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000343
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000344 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000345}
346
Stefan Tauner66652442011-06-26 17:38:17 +0000347char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000348{
349 return extract_param(&programmer_param, param_name, ",");
350}
351
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000352/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000353static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000354{
355 unsigned int usable_erasefunctions = 0;
356 int k;
357 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
358 if (!check_block_eraser(flash, k, 0))
359 usable_erasefunctions++;
360 }
361 return usable_erasefunctions;
362}
363
Mark Marshallf20b7be2014-05-09 21:16:21 +0000364static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000365{
366 int ret = 0, failcount = 0;
367 unsigned int i;
368 for (i = 0; i < len; i++) {
369 if (wantbuf[i] != havebuf[i]) {
370 /* Only print the first failure. */
371 if (!failcount++)
372 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
373 start + i, wantbuf[i], havebuf[i]);
374 }
375 }
376 if (failcount) {
377 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
378 start, start + len - 1, failcount);
379 ret = -1;
380 }
381 return ret;
382}
383
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000384/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600385static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000386{
387 int ret;
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
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100390 uint8_t *cmpbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000391 if (!cmpbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100392 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000393 exit(1);
394 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300395 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000396 ret = verify_range(flash, cmpbuf, start, len);
Edward O'Callaghanf60f64f2022-11-12 12:08:01 +1100397
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000398 free(cmpbuf);
399 return ret;
400}
401
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000402/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000403 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000404 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000405 * @start offset to the base address of the flash chip
406 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000407 * @return 0 for success, -1 for failure
408 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000409int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000410{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000411 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000412 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000413
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100414 if (start + len > flash->chip->total_size * 1024) {
415 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
416 " total_size 0x%x\n", __func__, start, len,
417 flash->chip->total_size * 1024);
418 return -1;
419 }
420
Stefan Taunerdf64a422014-05-27 00:06:14 +0000421 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000422 if (!readbuf) {
Edward O'Callaghana31a5722022-11-12 12:05:36 +1100423 msg_gerr("Out of memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000424 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000425 }
426
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100427 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000428 if (ret) {
429 msg_gerr("Verification impossible because read failed "
430 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000431 ret = -1;
432 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000433 }
434
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000435 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000436out_free:
437 free(readbuf);
438 return ret;
439}
440
Stefan Tauner02437452013-04-01 19:34:53 +0000441/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300442static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
443 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000444{
445 unsigned int i, j, limit;
446 for (j = 0; j < len / gran; j++) {
447 limit = min (gran, len - j * gran);
448 /* Are 'have' and 'want' identical? */
449 if (!memcmp(have + j * gran, want + j * gran, limit))
450 continue;
451 /* have needs to be in erased state. */
452 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300453 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000454 return 1;
455 }
456 return 0;
457}
458
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000459/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000460 * Check if the buffer @have can be programmed to the content of @want without
461 * erasing. This is only possible if all chunks of size @gran are either kept
462 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000463 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000464 * Warning: This function assumes that @have and @want point to naturally
465 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000466 *
467 * @have buffer with current content
468 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000469 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000470 * @gran write granularity (enum, not count)
471 * @return 0 if no erase is needed, 1 otherwise
472 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000473static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
474 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000475{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000476 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000477 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000478
479 switch (gran) {
480 case write_gran_1bit:
481 for (i = 0; i < len; i++)
482 if ((have[i] & want[i]) != want[i]) {
483 result = 1;
484 break;
485 }
486 break;
487 case write_gran_1byte:
488 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300489 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000490 result = 1;
491 break;
492 }
493 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000494 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300495 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000496 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000497 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300498 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000499 break;
500 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300501 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000502 break;
503 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300504 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000505 break;
506 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300507 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000508 break;
509 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300510 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000511 break;
512 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300513 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000514 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000515 case write_gran_1byte_implicit_erase:
516 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
517 result = 0;
518 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000519 default:
520 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000521 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000522 }
523 return result;
524}
525
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000526/**
527 * Check if the buffer @have needs to be programmed to get the content of @want.
528 * If yes, return 1 and fill in first_start with the start address of the
529 * write operation and first_len with the length of the first to-be-written
530 * chunk. If not, return 0 and leave first_start and first_len undefined.
531 *
532 * Warning: This function assumes that @have and @want point to naturally
533 * aligned regions.
534 *
535 * @have buffer with current content
536 * @want buffer with desired content
537 * @len length of the checked area
538 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000539 * @first_start offset of the first byte which needs to be written (passed in
540 * value is increased by the offset of the first needed write
541 * relative to have/want or unchanged if no write is needed)
542 * @return length of the first contiguous area which needs to be written
543 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000544 *
545 * FIXME: This function needs a parameter which tells it about coalescing
546 * in relation to the max write length of the programmer and the max write
547 * length of the chip.
548 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000549static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000550 unsigned int *first_start,
551 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000552{
Felix Singerf25447e2022-08-19 02:44:28 +0200553 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000554 unsigned int rel_start = 0, first_len = 0;
555 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000556
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000557 switch (gran) {
558 case write_gran_1bit:
559 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000560 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000561 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000562 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000563 case write_gran_128bytes:
564 stride = 128;
565 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000566 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000567 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000568 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000569 case write_gran_264bytes:
570 stride = 264;
571 break;
572 case write_gran_512bytes:
573 stride = 512;
574 break;
575 case write_gran_528bytes:
576 stride = 528;
577 break;
578 case write_gran_1024bytes:
579 stride = 1024;
580 break;
581 case write_gran_1056bytes:
582 stride = 1056;
583 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000584 default:
585 msg_cerr("%s: Unsupported granularity! Please report a bug at "
Nico Huberac90af62022-12-18 00:22:47 +0000586 "flashrom-stable@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000587 /* Claim that no write was needed. A write with unknown
588 * granularity is too dangerous to try.
589 */
590 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000591 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000592 for (i = 0; i < len / stride; i++) {
593 limit = min(stride, len - i * stride);
594 /* Are 'have' and 'want' identical? */
595 if (memcmp(have + i * stride, want + i * stride, limit)) {
596 if (!need_write) {
597 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200598 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000599 rel_start = i * stride;
600 }
601 } else {
602 if (need_write) {
603 /* First location where have and want
604 * do not differ anymore.
605 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000606 break;
607 }
608 }
609 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000610 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000611 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000612 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000613 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000614}
615
Martin Rothf6c1cb12022-03-15 10:55:25 -0600616/* Returns the number of buses commonly supported by the current programmer and flash chip where the latter
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000617 * can not be completely accessed due to size/address limits of the programmer. */
618unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000619{
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000620 unsigned int limitexceeded = 0;
621 uint32_t size = flash->chip->total_size * 1024;
622 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000623
624 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000625 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000626 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000627 "size %u kB of chipset/board/programmer "
628 "for %s interface, "
629 "probe/read/erase/write may fail. ", size / 1024,
630 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000631 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000632 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000633 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000634 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000635 "size %u kB of chipset/board/programmer "
636 "for %s interface, "
637 "probe/read/erase/write may fail. ", size / 1024,
638 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000639 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000640 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000641 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000642 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000643 "size %u kB of chipset/board/programmer "
644 "for %s interface, "
645 "probe/read/erase/write may fail. ", size / 1024,
646 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000647 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000648 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000649 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000650 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000651 "size %u kB of chipset/board/programmer "
652 "for %s interface, "
653 "probe/read/erase/write may fail. ", size / 1024,
654 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000655 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000656 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000657}
658
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000659void unmap_flash(struct flashctx *flash)
660{
661 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
662 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
663 flash->physical_registers = 0;
664 flash->virtual_registers = (chipaddr)ERROR_PTR;
665 }
666
667 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
668 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
669 flash->physical_memory = 0;
670 flash->virtual_memory = (chipaddr)ERROR_PTR;
671 }
672}
673
674int map_flash(struct flashctx *flash)
675{
676 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
677 flash->virtual_memory = (chipaddr)ERROR_PTR;
678 flash->virtual_registers = (chipaddr)ERROR_PTR;
679
680 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
681 * These are used for various probing-related hacks that would not map successfully anyway and should be
682 * removed ASAP. */
683 if (flash->chip->total_size == 0)
684 return 0;
685
686 const chipsize_t size = flash->chip->total_size * 1024;
687 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
688 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
689 if (addr == ERROR_PTR) {
690 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
691 flash->chip->name, PRIxPTR_WIDTH, base);
692 return 1;
693 }
694 flash->physical_memory = base;
695 flash->virtual_memory = (chipaddr)addr;
696
697 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
698 * completely different on some chips and programmers, or not mappable at all.
699 * Ignore these problems for now and always report success. */
700 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
701 base = 0xffffffff - size - 0x400000 + 1;
702 addr = programmer_map_flash_region("flash chip registers", base, size);
703 if (addr == ERROR_PTR) {
704 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
705 flash->chip->name, PRIxPTR_WIDTH, base);
706 return 0;
707 }
708 flash->physical_registers = base;
709 flash->virtual_registers = (chipaddr)addr;
710 }
711 return 0;
712}
713
Nico Huber2d625722016-05-03 10:48:02 +0200714/*
715 * Return a string corresponding to the bustype parameter.
716 * Memory is obtained with malloc() and must be freed with free() by the caller.
717 */
718char *flashbuses_to_text(enum chipbustype bustype)
719{
720 char *ret = calloc(1, 1);
721 /*
722 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
723 * will cease to exist and should be eliminated here as well.
724 */
725 if (bustype == BUS_NONSPI) {
726 ret = strcat_realloc(ret, "Non-SPI, ");
727 } else {
728 if (bustype & BUS_PARALLEL)
729 ret = strcat_realloc(ret, "Parallel, ");
730 if (bustype & BUS_LPC)
731 ret = strcat_realloc(ret, "LPC, ");
732 if (bustype & BUS_FWH)
733 ret = strcat_realloc(ret, "FWH, ");
734 if (bustype & BUS_SPI)
735 ret = strcat_realloc(ret, "SPI, ");
736 if (bustype & BUS_PROG)
737 ret = strcat_realloc(ret, "Programmer-specific, ");
738 if (bustype == BUS_NONE)
739 ret = strcat_realloc(ret, "None, ");
740 }
741 /* Kill last comma. */
742 ret[strlen(ret) - 2] = '\0';
743 ret = realloc(ret, strlen(ret) + 1);
744 return ret;
745}
746
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000747int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000748{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000749 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000750 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000751 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000752
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000753 for (chip = flashchips + startchip; chip && chip->name; chip++) {
754 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000755 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000756 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000757 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000758 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300759 /* Only probe for SPI25 chips by default. */
760 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
761 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000762 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
763 if (!chip->probe && !force) {
764 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000765 continue;
766 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000767
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000768 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200769 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000770 if (!flash->chip) {
771 msg_gerr("Out of memory!\n");
772 exit(1);
773 }
Angel Pons7e134562021-06-07 13:29:13 +0200774 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000775 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000776
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000777 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +0100778 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000779
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000780 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
781 * is only called with force=1 after normal probing failed.
782 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000783 if (force)
784 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000785
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000786 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000787 goto notfound;
788
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000789 /* If this is the first chip found, accept it.
790 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000791 * a non-generic match. SFDP and CFI are generic matches.
792 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000793 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000794 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000795 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000796 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000797 msg_cinfo("===\n"
798 "SFDP has autodetected a flash chip which is "
799 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000800 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000801 msg_cinfo("The standard operations read and "
802 "verify should work, but to support "
803 "erase, write and all other "
804 "possible features");
805 else
806 msg_cinfo("All standard operations (read, "
807 "verify, erase and write) should "
808 "work, but to support all possible "
809 "features");
810
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000811 msg_cinfo(" we need to add them manually.\n"
812 "You can help us by mailing us the output of the following command to "
Nico Huberac90af62022-12-18 00:22:47 +0000813 "flashrom-stable@flashrom.org:\n"
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000814 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
815 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000816 "===\n");
817 }
818
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000819 /* First flash chip detected on this bus. */
820 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000821 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000822 /* Not the first flash chip detected on this bus, but not a generic match either. */
823 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
824 break;
825 /* 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 +0000826notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000827 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000828 free(flash->chip);
829 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000830 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000831
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000832 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000833 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000834
Nico Huber5bd990c2019-06-16 19:46:46 +0200835 /* Fill default layout covering the whole chip. */
Nico Huber671c0f02019-06-16 20:17:19 +0200836 if (flashrom_layout_new(&flash->default_layout) ||
Nico Huber5bd990c2019-06-16 19:46:46 +0200837 flashrom_layout_add_region(flash->default_layout,
Nico Huber92e0b622019-06-15 15:55:11 +0200838 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
Nico Huber5bd990c2019-06-16 19:46:46 +0200839 flashrom_layout_include_region(flash->default_layout, "complete flash"))
840 return -1;
Stefan Reinauer051e2362011-01-19 06:21:54 +0000841
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000842 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000843 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
844 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000845 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000846#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200847 if (programmer->map_flash_region == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000848 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
849 PRIxPTR_WIDTH, flash->physical_memory);
850 else
851#endif
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200852 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000853
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000854 /* Flash registers may more likely not be mapped if the chip was forced.
855 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000856 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000857 if (flash->chip->printlock)
858 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000859
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000860 /* Get out of the way for later runs. */
861 unmap_flash(flash);
862
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000863 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000864 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000865}
866
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000867int read_buf_from_file(unsigned char *buf, unsigned long size,
868 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000869{
Nico Huber7562f7d2013-08-30 21:29:45 +0000870#ifdef __LIBPAYLOAD__
871 msg_gerr("Error: No file I/O support in libpayload\n");
872 return 1;
873#else
Stefan Tauner16687702015-12-25 21:59:45 +0000874 int ret = 0;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000875
Stefan Tauner16687702015-12-25 21:59:45 +0000876 FILE *image;
Daniel Campello8eaef7d2021-04-15 10:36:04 -0600877 if (!strcmp(filename, "-"))
878 image = fdopen(fileno(stdin), "rb");
879 else
880 image = fopen(filename, "rb");
881 if (image == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000882 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000883 return 1;
884 }
Stefan Tauner16687702015-12-25 21:59:45 +0000885
886 struct stat image_stat;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000887 if (fstat(fileno(image), &image_stat) != 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000888 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
Stefan Tauner16687702015-12-25 21:59:45 +0000889 ret = 1;
890 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000891 }
Daniel Campello8eaef7d2021-04-15 10:36:04 -0600892 if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
Carl-Daniel Hailfinger11990da2013-07-13 23:21:05 +0000893 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 +0000894 (intmax_t)image_stat.st_size, size);
Stefan Tauner16687702015-12-25 21:59:45 +0000895 ret = 1;
896 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000897 }
Stefan Tauner16687702015-12-25 21:59:45 +0000898
899 unsigned long numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000900 if (numbytes != size) {
901 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
902 "wanted %ld!\n", numbytes, size);
Stefan Tauner16687702015-12-25 21:59:45 +0000903 ret = 1;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000904 }
Stefan Tauner16687702015-12-25 21:59:45 +0000905out:
906 (void)fclose(image);
907 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +0000908#endif
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000909}
910
Mark Marshallf20b7be2014-05-09 21:16:21 +0000911int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000912{
Nico Huber7562f7d2013-08-30 21:29:45 +0000913#ifdef __LIBPAYLOAD__
914 msg_gerr("Error: No file I/O support in libpayload\n");
915 return 1;
916#else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000917 FILE *image;
Stefan Tauner16687702015-12-25 21:59:45 +0000918 int ret = 0;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000919
920 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000921 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000922 return 1;
923 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +0000924 if ((image = fopen(filename, "wb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000925 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000926 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000927 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000928
Stefan Tauner16687702015-12-25 21:59:45 +0000929 unsigned long numbytes = fwrite(buf, 1, size, image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000930 if (numbytes != size) {
Stefan Tauner16687702015-12-25 21:59:45 +0000931 msg_gerr("Error: file %s could not be written completely.\n", filename);
932 ret = 1;
933 goto out;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000934 }
Stefan Tauner16687702015-12-25 21:59:45 +0000935 if (fflush(image)) {
936 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
937 ret = 1;
938 }
939 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
940#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
941 struct stat image_stat;
942 if (fstat(fileno(image), &image_stat) != 0) {
943 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
944 ret = 1;
945 goto out;
946 }
947 if (S_ISREG(image_stat.st_mode)) {
948 if (fsync(fileno(image))) {
949 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
950 ret = 1;
951 }
952 }
953#endif
954out:
955 if (fclose(image)) {
956 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
957 ret = 1;
958 }
959 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +0000960#endif
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000961}
962
Nico Huber899e4ec2016-04-29 18:39:01 +0200963static int read_by_layout(struct flashctx *, uint8_t *);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000964int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000965{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000966 unsigned long size = flash->chip->total_size * 1024;
Richard Hughes84b453e2018-12-19 15:30:39 +0000967 unsigned char *buf = calloc(size, sizeof(unsigned char));
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000968 int ret = 0;
969
970 msg_cinfo("Reading flash... ");
971 if (!buf) {
972 msg_gerr("Memory allocation failed!\n");
973 msg_cinfo("FAILED.\n");
974 return 1;
975 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000976 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000977 msg_cerr("No read function available for this flash chip.\n");
978 ret = 1;
979 goto out_free;
980 }
Nico Huber899e4ec2016-04-29 18:39:01 +0200981 if (read_by_layout(flash, buf)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000982 msg_cerr("Read operation failed!\n");
983 ret = 1;
984 goto out_free;
985 }
986
Stefan Tauner355cbfd2011-05-28 02:37:14 +0000987 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000988out_free:
989 free(buf);
990 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
991 return ret;
992}
993
Stefan Tauner96658be2014-05-26 22:05:31 +0000994/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000995static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +0000996{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000997 int i, j, k;
998 int ret = 0;
Aarya Chaumal478e1792022-06-04 01:34:44 +0530999 unsigned int prev_eraseblock_count = chip->total_size * 1024;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001000
1001 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1002 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001003 struct block_eraser eraser = chip->block_erasers[k];
Aarya Chaumal478e1792022-06-04 01:34:44 +05301004 unsigned int curr_eraseblock_count = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001005
1006 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1007 /* Blocks with zero size are bugs in flashchips.c. */
1008 if (eraser.eraseblocks[i].count &&
1009 !eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +00001010 msg_gerr("ERROR: Flash chip %s erase function %i region %i has size 0.\n"
1011 "Please report a bug at flashrom-stable@flashrom.org\n",
1012 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001013 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001014 }
1015 /* Blocks with zero count are bugs in flashchips.c. */
1016 if (!eraser.eraseblocks[i].count &&
1017 eraser.eraseblocks[i].size) {
Nico Huberac90af62022-12-18 00:22:47 +00001018 msg_gerr("ERROR: Flash chip %s erase function %i region %i has count 0.\n"
1019 "Please report a bug at flashrom-stable@flashrom.org\n",
1020 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001021 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001022 }
1023 done += eraser.eraseblocks[i].count *
1024 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +05301025 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001026 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001027 /* Empty eraseblock definition with erase function. */
1028 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001029 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001030 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001031 if (!done)
1032 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001033 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001034 msg_gerr("ERROR: Flash chip %s erase function %i "
1035 "region walking resulted in 0x%06x bytes total,"
Nico Huberac90af62022-12-18 00:22:47 +00001036 " expected 0x%06x bytes.\n"
1037 "Please report a bug at flashrom-stable@flashrom.org\n",
1038 chip->name, k, done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001039 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001040 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001041 if (!eraser.block_erase)
1042 continue;
1043 /* Check if there are identical erase functions for different
1044 * layouts. That would imply "magic" erase functions. The
1045 * easiest way to check this is with function pointers.
1046 */
Uwe Hermann43959702010-03-13 17:28:29 +00001047 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001048 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001049 chip->block_erasers[j].block_erase) {
Nico Huberac90af62022-12-18 00:22:47 +00001050 msg_gerr("ERROR: Flash chip %s erase function %i and %i are identical.\n"
1051 "Please report a bug at flashrom-stable@flashrom.org\n",
1052 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001053 ret = 1;
1054 }
Uwe Hermann43959702010-03-13 17:28:29 +00001055 }
Aarya Chaumal478e1792022-06-04 01:34:44 +05301056 if(curr_eraseblock_count > prev_eraseblock_count)
1057 {
Nico Huberac90af62022-12-18 00:22:47 +00001058 msg_gerr("ERROR: Flash chip %s erase function %i is not in order.\n"
1059 "Please report a bug at flashrom-stable@flashrom.org\n",
1060 chip->name, k);
Aarya Chaumal478e1792022-06-04 01:34:44 +05301061 ret = 1;
1062 }
1063 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001064 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001065 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001066}
1067
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001068static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001069{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001070 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001071
1072 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1073 if (log)
1074 msg_cdbg("not defined. ");
1075 return 1;
1076 }
1077 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1078 if (log)
1079 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001080 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001081 return 1;
1082 }
1083 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1084 if (log)
1085 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001086 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001087 return 1;
1088 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001089 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001090 return 0;
1091}
1092
Nico Huber7af0e792016-04-29 16:40:15 +02001093/**
1094 * @brief Reads the included layout regions into a buffer.
1095 *
1096 * If there is no layout set in the given flash context, the whole chip will
1097 * be read.
1098 *
1099 * @param flashctx Flash context to be used.
1100 * @param buffer Buffer of full chip size to read into.
1101 * @return 0 on success,
1102 * 1 if any read fails.
1103 */
1104static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1105{
1106 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001107 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001108
Nico Huber5ca55232019-06-15 22:29:08 +02001109 while ((entry = layout_next_included(layout, entry))) {
1110 const chipoff_t region_start = entry->start;
1111 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001112
1113 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1114 return 1;
1115 }
1116 return 0;
1117}
1118
1119typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1120/**
1121 * @private
1122 *
1123 * For read-erase-write, `curcontents` and `newcontents` shall point
1124 * to buffers of the chip's size. Both are supposed to be prefilled
1125 * with at least the included layout regions of the current flash
1126 * contents (`curcontents`) and the data to be written to the flash
1127 * (`newcontents`).
1128 *
1129 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1130 *
1131 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1132 */
1133struct walk_info {
1134 uint8_t *curcontents;
1135 const uint8_t *newcontents;
1136 chipoff_t region_start;
1137 chipoff_t region_end;
1138 chipoff_t erase_start;
1139 chipoff_t erase_end;
1140};
1141/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1142typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1143
1144static int walk_eraseblocks(struct flashctx *const flashctx,
1145 struct walk_info *const info,
1146 const size_t erasefunction, const per_blockfn_t per_blockfn)
1147{
1148 int ret;
1149 size_t i, j;
1150 bool first = true;
1151 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1152
1153 info->erase_start = 0;
1154 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1155 /* count==0 for all automatically initialized array
1156 members so the loop below won't be executed for them. */
1157 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1158 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1159
1160 /* Skip any eraseblock that is completely outside the current region. */
1161 if (info->erase_end < info->region_start)
1162 continue;
1163 if (info->region_end < info->erase_start)
1164 break;
1165
1166 /* Print this for every block except the first one. */
1167 if (first)
1168 first = false;
1169 else
1170 msg_cdbg(", ");
1171 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1172
1173 ret = per_blockfn(flashctx, info, eraser->block_erase);
1174 if (ret)
1175 return ret;
1176 }
1177 if (info->region_end < info->erase_start)
1178 break;
1179 }
1180 msg_cdbg("\n");
1181 return 0;
1182}
1183
1184static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1185 const per_blockfn_t per_blockfn)
1186{
1187 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001188 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001189
1190 all_skipped = true;
1191 msg_cinfo("Erasing and writing flash chip... ");
1192
Nico Huber5ca55232019-06-15 22:29:08 +02001193 while ((entry = layout_next_included(layout, entry))) {
1194 info->region_start = entry->start;
1195 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001196
1197 size_t j;
1198 int error = 1; /* retry as long as it's 1 */
1199 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1200 if (j != 0)
1201 msg_cinfo("Looking for another erase function.\n");
1202 msg_cdbg("Trying erase function %zi... ", j);
1203 if (check_block_eraser(flashctx, j, 1))
1204 continue;
1205
1206 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1207 if (error != 1)
1208 break;
1209
1210 if (info->curcontents) {
1211 msg_cinfo("Reading current flash chip contents... ");
1212 if (read_by_layout(flashctx, info->curcontents)) {
1213 /* Now we are truly screwed. Read failed as well. */
1214 msg_cerr("Can't read anymore! Aborting.\n");
1215 /* We have no idea about the flash chip contents, so
1216 retrying with another erase function is pointless. */
1217 error = 2;
1218 break;
1219 }
1220 msg_cinfo("done. ");
1221 }
1222 }
1223 if (error == 1)
1224 msg_cinfo("No usable erase functions left.\n");
1225 if (error) {
1226 msg_cerr("FAILED!\n");
1227 return 1;
1228 }
1229 }
1230 if (all_skipped)
1231 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1232 msg_cinfo("Erase/write done.\n");
1233 return 0;
1234}
1235
1236static int erase_block(struct flashctx *const flashctx,
1237 const struct walk_info *const info, const erasefn_t erasefn)
1238{
1239 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001240 const bool region_unaligned = info->region_start > info->erase_start ||
1241 info->erase_end > info->region_end;
1242 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1243 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001244
Nico Huber6e61e0c2019-01-23 17:07:49 +01001245 /*
1246 * If the region is not erase-block aligned, merge current flash con-
1247 * tents into a new buffer `backup_contents`.
1248 */
1249 if (region_unaligned) {
1250 backup_contents = malloc(erase_len);
1251 erased_contents = malloc(erase_len);
1252 if (!backup_contents || !erased_contents) {
1253 msg_cerr("Out of memory!\n");
1254 ret = 1;
1255 goto _free_ret;
1256 }
1257 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1258 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1259
1260 msg_cdbg("R");
1261 /* Merge data preceding the current region. */
1262 if (info->region_start > info->erase_start) {
1263 const chipoff_t start = info->erase_start;
1264 const chipsize_t len = info->region_start - info->erase_start;
1265 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1266 msg_cerr("Can't read! Aborting.\n");
1267 goto _free_ret;
1268 }
1269 }
1270 /* Merge data following the current region. */
1271 if (info->erase_end > info->region_end) {
1272 const chipoff_t start = info->region_end + 1;
1273 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1274 const chipsize_t len = info->erase_end - info->region_end;
1275 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1276 msg_cerr("Can't read! Aborting.\n");
1277 goto _free_ret;
1278 }
1279 }
1280 }
1281
1282 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001283 all_skipped = false;
1284
1285 msg_cdbg("E");
1286 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001287 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001288 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1289 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001290 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001291 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001292
1293 if (region_unaligned) {
1294 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1295 /* get_next_write() sets starthere to a new value after the call. */
1296 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1297 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1298 if (!writecount++)
1299 msg_cdbg("W");
1300 /* Needs the partial write function signature. */
1301 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1302 info->erase_start + starthere, lenhere))
1303 goto _free_ret;
1304 starthere += lenhere;
1305 }
1306 }
1307
1308 ret = 0;
1309
1310_free_ret:
1311 free(erased_contents);
1312 free(backup_contents);
1313 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001314}
1315
1316/**
1317 * @brief Erases the included layout regions.
1318 *
1319 * If there is no layout set in the given flash context, the whole chip will
1320 * be erased.
1321 *
1322 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001323 * @return 0 on success,
1324 * 1 if all available erase functions failed.
1325 */
Nico Huber454f6132012-12-10 13:34:10 +00001326static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001327{
1328 struct walk_info info = { 0 };
1329 return walk_by_layout(flashctx, &info, &erase_block);
1330}
1331
1332static int read_erase_write_block(struct flashctx *const flashctx,
1333 const struct walk_info *const info, const erasefn_t erasefn)
1334{
1335 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1336 const bool region_unaligned = info->region_start > info->erase_start ||
1337 info->erase_end > info->region_end;
1338 const uint8_t *newcontents = NULL;
1339 int ret = 2;
1340
1341 /*
1342 * If the region is not erase-block aligned, merge current flash con-
1343 * tents into `info->curcontents` and a new buffer `newc`. The former
1344 * is necessary since we have no guarantee that the full erase block
1345 * was already read into `info->curcontents`. For the latter a new
1346 * buffer is used since `info->newcontents` might contain data for
1347 * other unaligned regions that touch this erase block too.
1348 */
1349 if (region_unaligned) {
1350 msg_cdbg("R");
1351 uint8_t *const newc = malloc(erase_len);
1352 if (!newc) {
1353 msg_cerr("Out of memory!\n");
1354 return 1;
1355 }
1356 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1357
1358 /* Merge data preceding the current region. */
1359 if (info->region_start > info->erase_start) {
1360 const chipoff_t start = info->erase_start;
1361 const chipsize_t len = info->region_start - info->erase_start;
1362 if (flashctx->chip->read(flashctx, newc, start, len)) {
1363 msg_cerr("Can't read! Aborting.\n");
1364 goto _free_ret;
1365 }
1366 memcpy(info->curcontents + start, newc, len);
1367 }
1368 /* Merge data following the current region. */
1369 if (info->erase_end > info->region_end) {
1370 const chipoff_t start = info->region_end + 1;
1371 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1372 const chipsize_t len = info->erase_end - info->region_end;
1373 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1374 msg_cerr("Can't read! Aborting.\n");
1375 goto _free_ret;
1376 }
1377 memcpy(info->curcontents + start, newc + rel_start, len);
1378 }
1379
1380 newcontents = newc;
1381 } else {
1382 newcontents = info->newcontents + info->erase_start;
1383 }
1384
1385 ret = 1;
1386 bool skipped = true;
1387 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001388 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001389 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1390 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001391 if (erase_block(flashctx, info, erasefn))
1392 goto _free_ret;
1393 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001394 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001395 skipped = false;
1396 }
1397
1398 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1399 /* get_next_write() sets starthere to a new value after the call. */
1400 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1401 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1402 if (!writecount++)
1403 msg_cdbg("W");
1404 /* Needs the partial write function signature. */
1405 if (flashctx->chip->write(flashctx, newcontents + starthere,
1406 info->erase_start + starthere, lenhere))
1407 goto _free_ret;
1408 starthere += lenhere;
1409 skipped = false;
1410 }
1411 if (skipped)
1412 msg_cdbg("S");
1413 else
1414 all_skipped = false;
1415
1416 /* Update curcontents, other regions with overlapping erase blocks
1417 might rely on this. */
1418 memcpy(curcontents, newcontents, erase_len);
1419 ret = 0;
1420
1421_free_ret:
1422 if (region_unaligned)
1423 free((void *)newcontents);
1424 return ret;
1425}
1426
1427/**
1428 * @brief Writes the included layout regions from a given image.
1429 *
1430 * If there is no layout set in the given flash context, the whole image
1431 * will be written.
1432 *
1433 * @param flashctx Flash context to be used.
1434 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1435 * @param newcontents The new image to be written.
1436 * @return 0 on success,
1437 * 1 if anything has gone wrong.
1438 */
Nico Huber454f6132012-12-10 13:34:10 +00001439static int write_by_layout(struct flashctx *const flashctx,
1440 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001441{
1442 struct walk_info info;
1443 info.curcontents = curcontents;
1444 info.newcontents = newcontents;
1445 return walk_by_layout(flashctx, &info, read_erase_write_block);
1446}
1447
1448/**
1449 * @brief Compares the included layout regions with content from a buffer.
1450 *
1451 * If there is no layout set in the given flash context, the whole chip's
1452 * contents will be compared.
1453 *
1454 * @param flashctx Flash context to be used.
Nico Huber74d09d42019-06-16 03:27:26 +02001455 * @param layout Flash layout information.
Nico Huber7af0e792016-04-29 16:40:15 +02001456 * @param curcontents A buffer of full chip size to read current chip contents into.
1457 * @param newcontents The new image to compare to.
1458 * @return 0 on success,
1459 * 1 if reading failed,
1460 * 3 if the contents don't match.
1461 */
Nico Huber74d09d42019-06-16 03:27:26 +02001462static int verify_by_layout(
1463 struct flashctx *const flashctx,
1464 const struct flashrom_layout *const layout,
1465 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001466{
Nico Huber5ca55232019-06-15 22:29:08 +02001467 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001468
Nico Huber5ca55232019-06-15 22:29:08 +02001469 while ((entry = layout_next_included(layout, entry))) {
1470 const chipoff_t region_start = entry->start;
1471 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001472
1473 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1474 return 1;
1475 if (compare_range(newcontents + region_start, curcontents + region_start,
1476 region_start, region_len))
1477 return 3;
1478 }
1479 return 0;
1480}
1481
Stefan Tauner136388f2013-07-15 10:47:53 +00001482static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001483{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001484 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001485#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001486 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001487 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Nico Huberac90af62022-12-18 00:22:47 +00001488 "chip. Please report this to the mailing list at flashrom-stable@flashrom.org or\n"
1489 "on IRC (see https://www.flashrom.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001490 "-------------------------------------------------------------------------------\n"
1491 "You may now reboot or simply leave the machine running.\n");
1492 else
1493#endif
1494 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1495 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
Nico Huberac90af62022-12-18 00:22:47 +00001496 "please report this to the mailing list at flashrom-stable@flashrom.org or on IRC\n"
1497 "(see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001498}
1499
Stefan Tauner136388f2013-07-15 10:47:53 +00001500static void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001501{
Stefan Tauner136388f2013-07-15 10:47:53 +00001502 msg_gerr("Your flash chip is in an unknown state.\n");
1503#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001504 if (programmer == &programmer_internal)
Angel Pons1900e1d2021-07-02 12:42:23 +02001505 msg_gerr("Get help on IRC (see https://www.flashrom.org/Contact) or mail\n"
Nico Huberac90af62022-12-18 00:22:47 +00001506 "flashrom-stable@flashrom.org with the subject \"FAILED: <your board name>\"!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001507 "-------------------------------------------------------------------------------\n"
1508 "DO NOT REBOOT OR POWEROFF!\n");
1509 else
1510#endif
Nico Huberac90af62022-12-18 00:22:47 +00001511 msg_gerr("Please report this to the mailing list at flashrom-stable@flashrom.org\n"
1512 "or on IRC (see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001513}
1514
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001515void list_programmers_linebreak(int startcol, int cols, int paren)
1516{
1517 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001518 int pnamelen;
1519 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001520 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001521 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001522
Thomas Heijligend45cb592021-05-19 14:12:18 +02001523 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001524 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001525 pnamelen = strlen(pname);
1526 if (remaining - pnamelen - 2 < 0) {
1527 if (firstline)
1528 firstline = 0;
1529 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001530 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001531 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001532 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001533 remaining = cols - startcol;
1534 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001535 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001536 remaining--;
1537 }
1538 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001539 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001540 remaining--;
1541 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001542 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001543 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001544 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001545 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001546 remaining--;
1547 } else {
1548 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001549 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001550 }
1551 }
1552}
1553
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001554static void print_sysinfo(void)
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001555{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001556#if IS_WINDOWS
Angel Pons7e134562021-06-07 13:29:13 +02001557 SYSTEM_INFO si = { 0 };
1558 OSVERSIONINFOEX osvi = { 0 };
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001559
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001560 msg_ginfo(" on Windows");
1561 /* Tell Windows which version of the structure we want. */
1562 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1563 if (GetVersionEx((OSVERSIONINFO*) &osvi))
1564 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
1565 else
1566 msg_ginfo(" unknown version");
1567 GetSystemInfo(&si);
1568 switch (si.wProcessorArchitecture) {
1569 case PROCESSOR_ARCHITECTURE_AMD64:
1570 msg_ginfo(" (x86_64)");
1571 break;
1572 case PROCESSOR_ARCHITECTURE_INTEL:
1573 msg_ginfo(" (x86)");
1574 break;
1575 default:
1576 msg_ginfo(" (unknown arch)");
1577 break;
1578 }
1579#elif HAVE_UTSNAME == 1
1580 struct utsname osinfo;
1581
1582 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001583 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1584 osinfo.machine);
1585#else
1586 msg_ginfo(" on unknown machine");
1587#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001588}
1589
1590void print_buildinfo(void)
1591{
1592 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001593#if NEED_PCI == 1
1594#ifdef PCILIB_VERSION
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001595 msg_gdbg(" libpci %s,", PCILIB_VERSION);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001596#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001597 msg_gdbg(" unknown PCI library,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001598#endif
1599#endif
1600#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001601 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001602#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001603 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001604#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001605 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001606#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001607#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001608 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001609#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001610 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001611#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001612 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001613#endif
1614#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001615 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001616#endif
1617#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001618 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001619#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001620 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001621#else
1622#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001623#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001624 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001625}
1626
Bernhard Walle201bde32008-01-21 15:24:22 +00001627void print_version(void)
1628{
Nico Huberac90af62022-12-18 00:22:47 +00001629 msg_ginfo("flashrom-stable %s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001630 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001631 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00001632}
1633
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001634void print_banner(void)
1635{
1636 msg_ginfo("flashrom is free software, get the source code at "
Stefan Tauner4c723152016-01-14 22:47:55 +00001637 "https://flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001638 msg_ginfo("\n");
1639}
1640
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001641int selfcheck(void)
1642{
Stefan Tauner96658be2014-05-26 22:05:31 +00001643 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001644 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001645
Thomas Heijligend45cb592021-05-19 14:12:18 +02001646 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001647 const struct programmer_entry *const p = programmer_table[i];
1648 if (p == NULL) {
1649 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1650 ret = 1;
1651 continue;
1652 }
1653 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001654 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1655 ret = 1;
1656 /* This might hide other problems with this programmer, but allows for better error
1657 * messages below without jumping through hoops. */
1658 continue;
1659 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001660 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001661 case USB:
1662 case PCI:
1663 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001664 if (p->devs.note == NULL) {
1665 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001666 break; /* This one has its device list stored separately. */
1667 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001668 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001669 ret = 1;
1670 }
1671 break;
1672 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001673 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001674 ret = 1;
1675 break;
1676 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001677 if (p->init == NULL) {
1678 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001679 ret = 1;
1680 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001681 if (p->delay == NULL) {
1682 msg_gerr("Programmer %s does not have a valid delay function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001683 ret = 1;
1684 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001685 if (p->map_flash_region == NULL) {
1686 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001687 ret = 1;
1688 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001689 if (p->unmap_flash_region == NULL) {
1690 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001691 ret = 1;
1692 }
1693 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001694
1695 /* It would be favorable if we could check for the correct layout (especially termination) of various
1696 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1697 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1698 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1699 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1700 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001701 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001702 msg_gerr("Flashchips table miscompilation!\n");
1703 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001704 } else {
1705 for (i = 0; i < flashchips_size - 1; i++) {
1706 const struct flashchip *chip = &flashchips[i];
1707 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1708 ret = 1;
1709 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
Nico Huberac90af62022-12-18 00:22:47 +00001710 "Please report a bug at flashrom-stable@flashrom.org\n", i,
Stefan Tauner96658be2014-05-26 22:05:31 +00001711 chip->name == NULL ? "unnamed" : chip->name);
1712 }
1713 if (selfcheck_eraseblocks(chip)) {
1714 ret = 1;
1715 }
1716 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001717 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001718
Stefan Tauner600576b2014-06-12 22:57:36 +00001719#if CONFIG_INTERNAL == 1
1720 ret |= selfcheck_board_enables();
1721#endif
1722
Stefan Tauner96658be2014-05-26 22:05:31 +00001723 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001724 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001725}
1726
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001727/* FIXME: This function signature needs to be improved once prepare_flash_access()
1728 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001729 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001730static int chip_safety_check(const struct flashctx *flash, int force,
1731 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001732{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001733 const struct flashchip *chip = flash->chip;
1734
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001735 if (!programmer_may_write && (write_it || erase_it)) {
1736 msg_perr("Write/erase is not working yet on your programmer in "
1737 "its current configuration.\n");
1738 /* --force is the wrong approach, but it's the best we can do
1739 * until the generic programmer parameter parser is merged.
1740 */
1741 if (!force)
1742 return 1;
1743 msg_cerr("Continuing anyway.\n");
1744 }
1745
1746 if (read_it || erase_it || write_it || verify_it) {
1747 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001748 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001749 msg_cerr("Read is not working on this chip. ");
1750 if (!force)
1751 return 1;
1752 msg_cerr("Continuing anyway.\n");
1753 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001754 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001755 msg_cerr("flashrom has no read function for this "
1756 "flash chip.\n");
1757 return 1;
1758 }
1759 }
1760 if (erase_it || write_it) {
1761 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001762 if (chip->tested.erase == NA) {
1763 msg_cerr("Erase is not possible on this chip.\n");
1764 return 1;
1765 }
1766 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001767 msg_cerr("Erase is not working on this chip. ");
1768 if (!force)
1769 return 1;
1770 msg_cerr("Continuing anyway.\n");
1771 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001772 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001773 msg_cerr("flashrom has no erase function for this "
1774 "flash chip.\n");
1775 return 1;
1776 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001777 }
1778 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001779 if (chip->tested.write == NA) {
1780 msg_cerr("Write is not possible on this chip.\n");
1781 return 1;
1782 }
1783 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001784 msg_cerr("Write is not working on this chip. ");
1785 if (!force)
1786 return 1;
1787 msg_cerr("Continuing anyway.\n");
1788 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001789 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001790 msg_cerr("flashrom has no write function for this "
1791 "flash chip.\n");
1792 return 1;
1793 }
1794 }
1795 return 0;
1796}
1797
Nico Huber305f4172013-06-14 11:55:26 +02001798int prepare_flash_access(struct flashctx *const flash,
1799 const bool read_it, const bool write_it,
1800 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001801{
1802 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1803 msg_cerr("Aborting.\n");
1804 return 1;
1805 }
1806
Nico Hubere0ed4122021-05-14 00:48:28 +02001807 if (layout_sanity_checks(flash)) {
Nico Huber454f6132012-12-10 13:34:10 +00001808 msg_cerr("Requested regions can not be handled. Aborting.\n");
1809 return 1;
1810 }
1811
1812 if (map_flash(flash) != 0)
1813 return 1;
1814
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001815 /* Initialize chip_restore_fn_count before chip unlock calls. */
1816 flash->chip_restore_fn_count = 0;
1817
Nico Huber454f6132012-12-10 13:34:10 +00001818 /* Given the existence of read locks, we want to unlock for read,
1819 erase and write. */
1820 if (flash->chip->unlock)
1821 flash->chip->unlock(flash);
1822
Nico Huberf43c6542017-10-14 17:47:28 +02001823 flash->address_high_byte = -1;
1824 flash->in_4ba_mode = false;
1825
Nico Huberdc5af542018-12-22 16:54:59 +01001826 /* Be careful about 4BA chips and broken masters */
1827 if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
1828 /* If we can't use native instructions, bail out */
1829 if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
1830 || !spi_master_4ba(flash)) {
1831 msg_cerr("Programmer doesn't support this chip. Aborting.\n");
1832 return 1;
1833 }
1834 }
1835
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001836 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01001837 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01001838 int ret;
1839 if (spi_master_4ba(flash))
1840 ret = spi_enter_4ba(flash);
1841 else
1842 ret = spi_exit_4ba(flash);
1843 if (ret) {
1844 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001845 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02001846 }
Boris Baykov99127182016-06-11 18:29:00 +02001847 }
1848
Nico Huber454f6132012-12-10 13:34:10 +00001849 return 0;
1850}
1851
Nico Huber305f4172013-06-14 11:55:26 +02001852void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001853{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001854 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001855 unmap_flash(flash);
1856}
1857
1858/**
1859 * @addtogroup flashrom-flash
1860 * @{
1861 */
1862
1863/**
1864 * @brief Erase the specified ROM chip.
1865 *
1866 * If a layout is set in the given flash context, only included regions
1867 * will be erased.
1868 *
1869 * @param flashctx The context of the flash chip to erase.
1870 * @return 0 on success.
1871 */
1872int flashrom_flash_erase(struct flashctx *const flashctx)
1873{
1874 if (prepare_flash_access(flashctx, false, false, true, false))
1875 return 1;
1876
1877 const int ret = erase_by_layout(flashctx);
1878
1879 finalize_flash_access(flashctx);
1880
1881 return ret;
1882}
1883
1884/** @} */ /* end flashrom-flash */
1885
1886/**
1887 * @defgroup flashrom-ops Operations
1888 * @{
1889 */
1890
1891/**
1892 * @brief Read the current image from the specified ROM chip.
1893 *
1894 * If a layout is set in the specified flash context, only included regions
1895 * will be read.
1896 *
1897 * @param flashctx The context of the flash chip.
1898 * @param buffer Target buffer to write image to.
1899 * @param buffer_len Size of target buffer in bytes.
1900 * @return 0 on success,
1901 * 2 if buffer_len is too short for the flash chip's contents,
1902 * or 1 on any other failure.
1903 */
1904int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
1905{
1906 const size_t flash_size = flashctx->chip->total_size * 1024;
1907
1908 if (flash_size > buffer_len)
1909 return 2;
1910
1911 if (prepare_flash_access(flashctx, true, false, false, false))
1912 return 1;
1913
1914 msg_cinfo("Reading flash... ");
1915
1916 int ret = 1;
1917 if (read_by_layout(flashctx, buffer)) {
1918 msg_cerr("Read operation failed!\n");
1919 msg_cinfo("FAILED.\n");
1920 goto _finalize_ret;
1921 }
1922 msg_cinfo("done.\n");
1923 ret = 0;
1924
1925_finalize_ret:
1926 finalize_flash_access(flashctx);
1927 return ret;
1928}
1929
1930static void combine_image_by_layout(const struct flashctx *const flashctx,
1931 uint8_t *const newcontents, const uint8_t *const oldcontents)
1932{
1933 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001934 const struct romentry *included;
1935 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001936
Nico Huber3d7b1e32018-12-22 00:53:14 +01001937 while ((included = layout_next_included_region(layout, start))) {
1938 if (included->start > start) {
1939 /* copy everything up to the start of this included region */
1940 memcpy(newcontents + start, oldcontents + start, included->start - start);
1941 }
1942 /* skip this included region */
1943 start = included->end + 1;
1944 if (start == 0)
1945 return;
Nico Huber454f6132012-12-10 13:34:10 +00001946 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001947
1948 /* copy the rest of the chip */
1949 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1950 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001951}
1952
1953/**
1954 * @brief Write the specified image to the ROM chip.
1955 *
1956 * If a layout is set in the specified flash context, only erase blocks
1957 * containing included regions will be touched.
1958 *
1959 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001960 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001961 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001962 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001963 * @return 0 on success,
1964 * 4 if buffer_len doesn't match the size of the flash chip,
1965 * 3 if write was tried but nothing has changed,
1966 * 2 if write failed and flash contents changed,
1967 * or 1 on any other failure.
1968 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001969int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
1970 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001971{
1972 const size_t flash_size = flashctx->chip->total_size * 1024;
1973 const bool verify_all = flashctx->flags.verify_whole_chip;
1974 const bool verify = flashctx->flags.verify_after_write;
Nico Huber74d09d42019-06-16 03:27:26 +02001975 const struct flashrom_layout *const verify_layout =
1976 verify_all ? get_default_layout(flashctx) : get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00001977
1978 if (buffer_len != flash_size)
1979 return 4;
1980
1981 int ret = 1;
1982
1983 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001984 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001985 uint8_t *const curcontents = malloc(flash_size);
1986 uint8_t *oldcontents = NULL;
1987 if (verify_all)
1988 oldcontents = malloc(flash_size);
1989 if (!curcontents || (verify_all && !oldcontents)) {
1990 msg_gerr("Out of memory!\n");
1991 goto _free_ret;
1992 }
1993
1994#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001995 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001996 if (flashctx->flags.force_boardmismatch) {
1997 msg_pinfo("Proceeding anyway because user forced us to.\n");
1998 } else {
1999 msg_perr("Aborting. You can override this with "
2000 "-p internal:boardmismatch=force.\n");
2001 goto _free_ret;
2002 }
2003 }
2004#endif
2005
2006 if (prepare_flash_access(flashctx, false, true, false, verify))
2007 goto _free_ret;
2008
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002009 /* If given, assume flash chip contains same data as `refcontents`. */
2010 if (refcontents) {
2011 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
2012 memcpy(curcontents, refcontents, flash_size);
2013 if (oldcontents)
2014 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00002015 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002016 /*
2017 * Read the whole chip to be able to check whether regions need to be
2018 * erased and to give better diagnostics in case write fails.
2019 * The alternative is to read only the regions which are to be
2020 * preserved, but in that case we might perform unneeded erase which
2021 * takes time as well.
2022 */
2023 msg_cinfo("Reading old flash chip contents... ");
2024 if (verify_all) {
2025 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
2026 msg_cinfo("FAILED.\n");
2027 goto _finalize_ret;
2028 }
2029 memcpy(curcontents, oldcontents, flash_size);
2030 } else {
2031 if (read_by_layout(flashctx, curcontents)) {
2032 msg_cinfo("FAILED.\n");
2033 goto _finalize_ret;
2034 }
Nico Huber454f6132012-12-10 13:34:10 +00002035 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002036 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00002037 }
Nico Huber454f6132012-12-10 13:34:10 +00002038
2039 if (write_by_layout(flashctx, curcontents, newcontents)) {
2040 msg_cerr("Uh oh. Erase/write failed. ");
2041 ret = 2;
2042 if (verify_all) {
2043 msg_cerr("Checking if anything has changed.\n");
2044 msg_cinfo("Reading current flash chip contents... ");
2045 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
2046 msg_cinfo("done.\n");
2047 if (!memcmp(oldcontents, curcontents, flash_size)) {
2048 nonfatal_help_message();
2049 goto _finalize_ret;
2050 }
2051 msg_cerr("Apparently at least some data has changed.\n");
2052 } else
2053 msg_cerr("Can't even read anymore!\n");
2054 emergency_help_message();
2055 goto _finalize_ret;
2056 } else {
2057 msg_cerr("\n");
2058 }
2059 emergency_help_message();
2060 goto _finalize_ret;
2061 }
2062
2063 /* Verify only if we actually changed something. */
2064 if (verify && !all_skipped) {
Nico Huber454f6132012-12-10 13:34:10 +00002065 msg_cinfo("Verifying flash... ");
2066
2067 /* Work around chips which need some time to calm down. */
2068 programmer_delay(1000*1000);
2069
Nico Huber74d09d42019-06-16 03:27:26 +02002070 if (verify_all)
Nico Huber454f6132012-12-10 13:34:10 +00002071 combine_image_by_layout(flashctx, newcontents, oldcontents);
Nico Huber74d09d42019-06-16 03:27:26 +02002072 ret = verify_by_layout(flashctx, verify_layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00002073 /* If we tried to write, and verification now fails, we
2074 might have an emergency situation. */
2075 if (ret)
2076 emergency_help_message();
2077 else
2078 msg_cinfo("VERIFIED.\n");
2079 } else {
2080 /* We didn't change anything. */
2081 ret = 0;
2082 }
2083
2084_finalize_ret:
2085 finalize_flash_access(flashctx);
2086_free_ret:
2087 free(oldcontents);
2088 free(curcontents);
2089 return ret;
2090}
2091
2092/**
2093 * @brief Verify the ROM chip's contents with the specified image.
2094 *
2095 * If a layout is set in the specified flash context, only included regions
2096 * will be verified.
2097 *
2098 * @param flashctx The context of the flash chip.
2099 * @param buffer Source buffer to verify with.
2100 * @param buffer_len Size of source buffer in bytes.
2101 * @return 0 on success,
2102 * 3 if the chip's contents don't match,
2103 * 2 if buffer_len doesn't match the size of the flash chip,
2104 * or 1 on any other failure.
2105 */
2106int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
2107{
Nico Huber74d09d42019-06-16 03:27:26 +02002108 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber454f6132012-12-10 13:34:10 +00002109 const size_t flash_size = flashctx->chip->total_size * 1024;
2110
2111 if (buffer_len != flash_size)
2112 return 2;
2113
2114 const uint8_t *const newcontents = buffer;
2115 uint8_t *const curcontents = malloc(flash_size);
2116 if (!curcontents) {
2117 msg_gerr("Out of memory!\n");
2118 return 1;
2119 }
2120
2121 int ret = 1;
2122
2123 if (prepare_flash_access(flashctx, false, false, false, true))
2124 goto _free_ret;
2125
2126 msg_cinfo("Verifying flash... ");
Nico Huber74d09d42019-06-16 03:27:26 +02002127 ret = verify_by_layout(flashctx, layout, curcontents, newcontents);
Nico Huber454f6132012-12-10 13:34:10 +00002128 if (!ret)
2129 msg_cinfo("VERIFIED.\n");
2130
2131 finalize_flash_access(flashctx);
2132_free_ret:
2133 free(curcontents);
2134 return ret;
2135}
2136
2137/** @} */ /* end flashrom-ops */
Nico Huber899e4ec2016-04-29 18:39:01 +02002138
2139int do_read(struct flashctx *const flash, const char *const filename)
2140{
2141 if (prepare_flash_access(flash, true, false, false, false))
2142 return 1;
2143
2144 const int ret = read_flash_to_file(flash, filename);
2145
2146 finalize_flash_access(flash);
2147
2148 return ret;
2149}
2150
2151int do_erase(struct flashctx *const flash)
2152{
2153 const int ret = flashrom_flash_erase(flash);
2154
2155 /*
2156 * FIXME: Do we really want the scary warning if erase failed?
2157 * After all, after erase the chip is either blank or partially
2158 * blank or it has the old contents. A blank chip won't boot,
2159 * so if the user wanted erase and reboots afterwards, the user
2160 * knows very well that booting won't work.
2161 */
2162 if (ret)
2163 emergency_help_message();
2164
2165 return ret;
2166}
2167
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002168int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
Nico Huber899e4ec2016-04-29 18:39:01 +02002169{
2170 const size_t flash_size = flash->chip->total_size * 1024;
2171 int ret = 1;
2172
2173 uint8_t *const newcontents = malloc(flash_size);
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002174 uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
2175
2176 if (!newcontents || (referencefile && !refcontents)) {
Nico Huber899e4ec2016-04-29 18:39:01 +02002177 msg_gerr("Out of memory!\n");
2178 goto _free_ret;
2179 }
2180
2181 if (read_buf_from_file(newcontents, flash_size, filename))
2182 goto _free_ret;
2183
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002184 if (referencefile) {
2185 if (read_buf_from_file(refcontents, flash_size, referencefile))
2186 goto _free_ret;
2187 }
2188
2189 ret = flashrom_image_write(flash, newcontents, flash_size, refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002190
2191_free_ret:
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002192 free(refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002193 free(newcontents);
2194 return ret;
2195}
2196
2197int do_verify(struct flashctx *const flash, const char *const filename)
2198{
2199 const size_t flash_size = flash->chip->total_size * 1024;
2200 int ret = 1;
2201
2202 uint8_t *const newcontents = malloc(flash_size);
2203 if (!newcontents) {
2204 msg_gerr("Out of memory!\n");
2205 goto _free_ret;
2206 }
2207
2208 if (read_buf_from_file(newcontents, flash_size, filename))
2209 goto _free_ret;
2210
2211 ret = flashrom_image_verify(flash, newcontents, flash_size);
2212
2213_free_ret:
2214 free(newcontents);
2215 return ret;
2216}