blob: f6568726b643ada39a3d53a956855da06c6007b0 [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Nico Huber7af0e792016-04-29 16:40:15 +02008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000010 *
Uwe Hermannd1107642007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000015 *
Uwe Hermannd1107642007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000020 */
21
Felix Singerf25447e2022-08-19 02:44:28 +020022#include <stdbool.h>
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000023#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000024#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000025#ifndef __LIBPAYLOAD__
26#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000027#include <sys/stat.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000028#endif
Ronald G. Minnichceec4202003-07-25 04:37:41 +000029#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000030#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000031#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000032#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000033#include <ctype.h>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000037#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000038#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000039#include "programmer.h"
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +000040#include "hwaccess.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010041#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000042
Mathias Krausea60faab2011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000044const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000045
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +020046static const struct programmer_entry *programmer = NULL;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000047static const char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000048
Uwe Hermann48ec1b12010-08-08 17:01:18 +000049/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000050 * Programmers supporting multiple buses can have differing size limits on
51 * each bus. Store the limits for each bus in a common struct.
52 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000053struct decode_sizes max_rom_decode;
54
55/* If nonzero, used as the start address of bottom-aligned flash. */
56unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000057
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000058/* Is writing allowed with this programmer? */
Felix Singer980d6b82022-08-19 02:48:15 +020059bool programmer_may_write;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000060
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000061#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000062static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000063/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000064static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000065 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000066 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000067} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000068/* Initialize to 0 to make sure nobody registers a shutdown function before
69 * programmer init.
70 */
Felix Singerf25447e2022-08-19 02:44:28 +020071static bool may_register_shutdown = false;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000072
Stefan Taunerc4f44df2013-08-12 22:58:43 +000073/* Did we change something or was every erase/write skipped (if any)? */
74static bool all_skipped = true;
75
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000076static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000077
Stefan Tauner2a1ed772014-08-31 00:09:21 +000078int shutdown_free(void *data)
79{
80 free(data);
81 return 0;
82}
83
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000084/* Register a function to be executed on programmer shutdown.
85 * The advantage over atexit() is that you can supply a void pointer which will
86 * be used as parameter to the registered function upon programmer shutdown.
87 * This pointer can point to arbitrary data used by said function, e.g. undo
88 * information for GPIO settings etc. If unneeded, set data=NULL.
89 * Please note that the first (void *data) belongs to the function signature of
90 * the function passed as first parameter.
91 */
David Hendricks8bb20212011-06-14 01:35:36 +000092int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000093{
94 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000095 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000096 SHUTDOWN_MAXFN);
97 return 1;
98 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000099 if (!may_register_shutdown) {
100 msg_perr("Tried to register a shutdown function before "
101 "programmer init.\n");
102 return 1;
103 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +0000104 shutdown_fn[shutdown_fn_count].func = function;
105 shutdown_fn[shutdown_fn_count].data = data;
106 shutdown_fn_count++;
107
108 return 0;
109}
110
Nikolai Artemiev4ad48642020-11-05 13:54:27 +1100111int register_chip_restore(chip_restore_fn_cb_t func,
112 struct flashctx *flash, uint8_t status)
113{
114 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
115 msg_perr("Tried to register more than %i chip restore"
116 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
117 return 1;
118 }
119 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
120 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
121 flash->chip_restore_fn_count++;
122
123 return 0;
124}
125
126static int deregister_chip_restore(struct flashctx *flash)
127{
128 int rc = 0;
129
130 while (flash->chip_restore_fn_count > 0) {
131 int i = --flash->chip_restore_fn_count;
132 rc |= flash->chip_restore_fn[i].func(
133 flash, flash->chip_restore_fn[i].status);
134 }
135
136 return rc;
137}
138
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200139int programmer_init(const struct programmer_entry *prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000140{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000141 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000142
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200143 if (prog == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000144 msg_perr("Invalid programmer specified!\n");
145 return -1;
146 }
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200147 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000148 /* Initialize all programmer specific data. */
149 /* Default to unlimited decode sizes. */
150 max_rom_decode = (const struct decode_sizes) {
151 .parallel = 0xffffffff,
152 .lpc = 0xffffffff,
153 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000154 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000155 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000156 /* Default to top aligned flash at 4 GB. */
157 flashbase = 0;
158 /* Registering shutdown functions is now allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200159 may_register_shutdown = true;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000160 /* Default to allowing writes. Broken programmers set this to 0. */
Felix Singer980d6b82022-08-19 02:48:15 +0200161 programmer_may_write = true;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000162
163 programmer_param = param;
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200164 msg_pdbg("Initializing %s programmer\n", programmer->name);
165 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000166 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000167 if (ret != 0) {
168 /* It is quite possible that any unhandled programmer parameter would have been valid,
169 * but an error in actual programmer init happened before the parameter was evaluated.
170 */
171 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
172 programmer_param);
173 } else {
174 /* Actual programmer init was successful, but the user specified an invalid or unusable
175 * (for the current programmer configuration) parameter.
176 */
177 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
178 msg_perr("Aborting.\n");
179 ret = ERROR_FATAL;
180 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000181 }
182 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000183}
184
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000185/** Calls registered shutdown functions and resets internal programmer-related variables.
186 * Calling it is safe even without previous initialization, but further interactions with programmer support
187 * require a call to programmer_init() (afterwards).
188 *
189 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000190int programmer_shutdown(void)
191{
David Hendricks8bb20212011-06-14 01:35:36 +0000192 int ret = 0;
193
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000194 /* Registering shutdown functions is no longer allowed. */
Felix Singerf25447e2022-08-19 02:44:28 +0200195 may_register_shutdown = false;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000196 while (shutdown_fn_count > 0) {
197 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000198 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000199 }
Stefan Taunere34e3e82013-01-01 00:06:51 +0000200
Stefan Taunerb8911d62012-12-26 07:55:00 +0000201 programmer_param = NULL;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000202 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000203
David Hendricks8bb20212011-06-14 01:35:36 +0000204 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000205}
206
Stefan Tauner305e0b92013-07-17 23:46:44 +0000207void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000208{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200209 void *ret = programmer->map_flash_region(descr, phys_addr, len);
Stefan Tauner26e7a152013-09-13 17:21:05 +0000210 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
211 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
212 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000213}
214
215void programmer_unmap_flash_region(void *virt_addr, size_t len)
216{
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200217 programmer->unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000218 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000219}
220
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000221void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000222{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000223 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000224}
225
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000226void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000227{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000228 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000229}
230
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000231void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000232{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000233 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000234}
235
Mark Marshallf20b7be2014-05-09 21:16:21 +0000236void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000237{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000238 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000239}
240
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000241uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000242{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000243 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000244}
245
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000246uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000247{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000248 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000249}
250
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000251uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000252{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000253 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000254}
255
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000256void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
257 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000258{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000259 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000260}
261
Stefan Taunerf80419c2014-05-02 15:41:42 +0000262void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000263{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000264 if (usecs > 0)
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +0200265 programmer->delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000266}
267
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000268int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
269 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000270{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000271 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000272
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000273 return 0;
274}
275
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000276/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000277 * It will look for needle with a subsequent '=' in haystack, return a copy of
278 * needle and remove everything from the first occurrence of needle to the next
279 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000280 */
Edward O'Callaghand75e1b12021-07-14 15:06:04 +1000281static char *extract_param(const char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000282{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000283 char *param_pos, *opt_pos, *rest;
284 char *opt = NULL;
285 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000286 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000287
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000288 needlelen = strlen(needle);
289 if (!needlelen) {
290 msg_gerr("%s: empty needle! Please report a bug at "
291 "flashrom@flashrom.org\n", __func__);
292 return NULL;
293 }
294 /* No programmer parameters given. */
295 if (*haystack == NULL)
296 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000297 param_pos = strstr(*haystack, needle);
298 do {
299 if (!param_pos)
300 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000301 /* Needle followed by '='? */
302 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000303 /* Beginning of the string? */
304 if (param_pos == *haystack)
305 break;
306 /* After a delimiter? */
307 if (strchr(delim, *(param_pos - 1)))
308 break;
309 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000310 /* Continue searching. */
311 param_pos++;
312 param_pos = strstr(param_pos, needle);
313 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000314
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000315 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000316 /* Get the string after needle and '='. */
317 opt_pos = param_pos + needlelen + 1;
318 optlen = strcspn(opt_pos, delim);
319 /* Return an empty string if the parameter was empty. */
320 opt = malloc(optlen + 1);
321 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000322 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000323 exit(1);
324 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000325 strncpy(opt, opt_pos, optlen);
326 opt[optlen] = '\0';
327 rest = opt_pos + optlen;
328 /* Skip all delimiters after the current parameter. */
329 rest += strspn(rest, delim);
330 memmove(param_pos, rest, strlen(rest) + 1);
331 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000332 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000333
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000334 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000335}
336
Stefan Tauner66652442011-06-26 17:38:17 +0000337char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000338{
339 return extract_param(&programmer_param, param_name, ",");
340}
341
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000342/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000343static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000344{
345 unsigned int usable_erasefunctions = 0;
346 int k;
347 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
348 if (!check_block_eraser(flash, k, 0))
349 usable_erasefunctions++;
350 }
351 return usable_erasefunctions;
352}
353
Mark Marshallf20b7be2014-05-09 21:16:21 +0000354static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000355{
356 int ret = 0, failcount = 0;
357 unsigned int i;
358 for (i = 0; i < len; i++) {
359 if (wantbuf[i] != havebuf[i]) {
360 /* Only print the first failure. */
361 if (!failcount++)
362 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
363 start + i, wantbuf[i], havebuf[i]);
364 }
365 }
366 if (failcount) {
367 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
368 start, start + len - 1, failcount);
369 ret = -1;
370 }
371 return ret;
372}
373
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000374/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600375static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000376{
377 int ret;
378 uint8_t *cmpbuf = malloc(len);
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300379 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000380
381 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000382 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000383 exit(1);
384 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300385 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000386 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000387 free(cmpbuf);
388 return ret;
389}
390
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000391/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000392 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000393 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000394 * @start offset to the base address of the flash chip
395 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000396 * @return 0 for success, -1 for failure
397 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000398int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000399{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000400 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000401 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000402
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100403 if (start + len > flash->chip->total_size * 1024) {
404 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
405 " total_size 0x%x\n", __func__, start, len,
406 flash->chip->total_size * 1024);
407 return -1;
408 }
409
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000410 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000411 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000412 return -1;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000413 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000414
415 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000416 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000417 msg_gerr("Could not allocate memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000418 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000419 }
420
Edward O'Callaghan6ae640b2021-11-17 14:24:04 +1100421 int ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000422 if (ret) {
423 msg_gerr("Verification impossible because read failed "
424 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000425 ret = -1;
426 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000427 }
428
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000429 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000430out_free:
431 free(readbuf);
432 return ret;
433}
434
Stefan Tauner02437452013-04-01 19:34:53 +0000435/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300436static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
437 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000438{
439 unsigned int i, j, limit;
440 for (j = 0; j < len / gran; j++) {
441 limit = min (gran, len - j * gran);
442 /* Are 'have' and 'want' identical? */
443 if (!memcmp(have + j * gran, want + j * gran, limit))
444 continue;
445 /* have needs to be in erased state. */
446 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300447 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000448 return 1;
449 }
450 return 0;
451}
452
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000453/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000454 * Check if the buffer @have can be programmed to the content of @want without
455 * erasing. This is only possible if all chunks of size @gran are either kept
456 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000457 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000458 * Warning: This function assumes that @have and @want point to naturally
459 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000460 *
461 * @have buffer with current content
462 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000463 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000464 * @gran write granularity (enum, not count)
465 * @return 0 if no erase is needed, 1 otherwise
466 */
Edward O'Callaghana1805092022-05-16 11:10:36 +1000467static int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
468 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000469{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000470 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000471 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000472
473 switch (gran) {
474 case write_gran_1bit:
475 for (i = 0; i < len; i++)
476 if ((have[i] & want[i]) != want[i]) {
477 result = 1;
478 break;
479 }
480 break;
481 case write_gran_1byte:
482 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300483 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000484 result = 1;
485 break;
486 }
487 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000488 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300489 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000490 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000491 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300492 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000493 break;
494 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300495 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000496 break;
497 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300498 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000499 break;
500 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300501 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000502 break;
503 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300504 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000505 break;
506 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300507 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000508 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000509 case write_gran_1byte_implicit_erase:
510 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
511 result = 0;
512 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000513 default:
514 msg_cerr("%s: Unsupported granularity! Please report a bug at "
515 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000516 }
517 return result;
518}
519
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000520/**
521 * Check if the buffer @have needs to be programmed to get the content of @want.
522 * If yes, return 1 and fill in first_start with the start address of the
523 * write operation and first_len with the length of the first to-be-written
524 * chunk. If not, return 0 and leave first_start and first_len undefined.
525 *
526 * Warning: This function assumes that @have and @want point to naturally
527 * aligned regions.
528 *
529 * @have buffer with current content
530 * @want buffer with desired content
531 * @len length of the checked area
532 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000533 * @first_start offset of the first byte which needs to be written (passed in
534 * value is increased by the offset of the first needed write
535 * relative to have/want or unchanged if no write is needed)
536 * @return length of the first contiguous area which needs to be written
537 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000538 *
539 * FIXME: This function needs a parameter which tells it about coalescing
540 * in relation to the max write length of the programmer and the max write
541 * length of the chip.
542 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000543static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000544 unsigned int *first_start,
545 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000546{
Felix Singerf25447e2022-08-19 02:44:28 +0200547 bool need_write = false;
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000548 unsigned int rel_start = 0, first_len = 0;
549 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000550
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000551 switch (gran) {
552 case write_gran_1bit:
553 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000554 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000555 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000556 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000557 case write_gran_128bytes:
558 stride = 128;
559 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000560 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000561 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000562 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000563 case write_gran_264bytes:
564 stride = 264;
565 break;
566 case write_gran_512bytes:
567 stride = 512;
568 break;
569 case write_gran_528bytes:
570 stride = 528;
571 break;
572 case write_gran_1024bytes:
573 stride = 1024;
574 break;
575 case write_gran_1056bytes:
576 stride = 1056;
577 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000578 default:
579 msg_cerr("%s: Unsupported granularity! Please report a bug at "
580 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000581 /* Claim that no write was needed. A write with unknown
582 * granularity is too dangerous to try.
583 */
584 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000585 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000586 for (i = 0; i < len / stride; i++) {
587 limit = min(stride, len - i * stride);
588 /* Are 'have' and 'want' identical? */
589 if (memcmp(have + i * stride, want + i * stride, limit)) {
590 if (!need_write) {
591 /* First location where have and want differ. */
Felix Singerf25447e2022-08-19 02:44:28 +0200592 need_write = true;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000593 rel_start = i * stride;
594 }
595 } else {
596 if (need_write) {
597 /* First location where have and want
598 * do not differ anymore.
599 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000600 break;
601 }
602 }
603 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000604 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000605 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000606 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000607 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000608}
609
Martin Rothf6c1cb12022-03-15 10:55:25 -0600610/* Returns the number of buses commonly supported by the current programmer and flash chip where the latter
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000611 * can not be completely accessed due to size/address limits of the programmer. */
612unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000613{
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000614 unsigned int limitexceeded = 0;
615 uint32_t size = flash->chip->total_size * 1024;
616 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000617
618 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000619 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000620 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000621 "size %u kB of chipset/board/programmer "
622 "for %s interface, "
623 "probe/read/erase/write may fail. ", size / 1024,
624 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000625 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000626 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000627 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000628 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000629 "size %u kB of chipset/board/programmer "
630 "for %s interface, "
631 "probe/read/erase/write may fail. ", size / 1024,
632 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000633 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000634 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000635 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000636 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000637 "size %u kB of chipset/board/programmer "
638 "for %s interface, "
639 "probe/read/erase/write may fail. ", size / 1024,
640 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000641 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000642 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000643 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000644 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000645 "size %u kB of chipset/board/programmer "
646 "for %s interface, "
647 "probe/read/erase/write may fail. ", size / 1024,
648 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000649 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000650 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000651}
652
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000653void unmap_flash(struct flashctx *flash)
654{
655 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
656 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
657 flash->physical_registers = 0;
658 flash->virtual_registers = (chipaddr)ERROR_PTR;
659 }
660
661 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
662 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
663 flash->physical_memory = 0;
664 flash->virtual_memory = (chipaddr)ERROR_PTR;
665 }
666}
667
668int map_flash(struct flashctx *flash)
669{
670 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
671 flash->virtual_memory = (chipaddr)ERROR_PTR;
672 flash->virtual_registers = (chipaddr)ERROR_PTR;
673
674 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
675 * These are used for various probing-related hacks that would not map successfully anyway and should be
676 * removed ASAP. */
677 if (flash->chip->total_size == 0)
678 return 0;
679
680 const chipsize_t size = flash->chip->total_size * 1024;
681 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
682 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
683 if (addr == ERROR_PTR) {
684 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
685 flash->chip->name, PRIxPTR_WIDTH, base);
686 return 1;
687 }
688 flash->physical_memory = base;
689 flash->virtual_memory = (chipaddr)addr;
690
691 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
692 * completely different on some chips and programmers, or not mappable at all.
693 * Ignore these problems for now and always report success. */
694 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
695 base = 0xffffffff - size - 0x400000 + 1;
696 addr = programmer_map_flash_region("flash chip registers", base, size);
697 if (addr == ERROR_PTR) {
698 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
699 flash->chip->name, PRIxPTR_WIDTH, base);
700 return 0;
701 }
702 flash->physical_registers = base;
703 flash->virtual_registers = (chipaddr)addr;
704 }
705 return 0;
706}
707
Nico Huber2d625722016-05-03 10:48:02 +0200708/*
709 * Return a string corresponding to the bustype parameter.
710 * Memory is obtained with malloc() and must be freed with free() by the caller.
711 */
712char *flashbuses_to_text(enum chipbustype bustype)
713{
714 char *ret = calloc(1, 1);
715 /*
716 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
717 * will cease to exist and should be eliminated here as well.
718 */
719 if (bustype == BUS_NONSPI) {
720 ret = strcat_realloc(ret, "Non-SPI, ");
721 } else {
722 if (bustype & BUS_PARALLEL)
723 ret = strcat_realloc(ret, "Parallel, ");
724 if (bustype & BUS_LPC)
725 ret = strcat_realloc(ret, "LPC, ");
726 if (bustype & BUS_FWH)
727 ret = strcat_realloc(ret, "FWH, ");
728 if (bustype & BUS_SPI)
729 ret = strcat_realloc(ret, "SPI, ");
730 if (bustype & BUS_PROG)
731 ret = strcat_realloc(ret, "Programmer-specific, ");
732 if (bustype == BUS_NONE)
733 ret = strcat_realloc(ret, "None, ");
734 }
735 /* Kill last comma. */
736 ret[strlen(ret) - 2] = '\0';
737 ret = realloc(ret, strlen(ret) + 1);
738 return ret;
739}
740
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000741int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000742{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000743 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000744 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000745 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000746
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000747 for (chip = flashchips + startchip; chip && chip->name; chip++) {
748 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000749 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000750 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000751 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000752 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300753 /* Only probe for SPI25 chips by default. */
754 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
755 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000756 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
757 if (!chip->probe && !force) {
758 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000759 continue;
760 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000761
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000762 /* Start filling in the dynamic data. */
Angel Pons690a9442021-06-07 12:33:53 +0200763 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000764 if (!flash->chip) {
765 msg_gerr("Out of memory!\n");
766 exit(1);
767 }
Angel Pons7e134562021-06-07 13:29:13 +0200768 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000769 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000770
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000771 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +0100772 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000773
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000774 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
775 * is only called with force=1 after normal probing failed.
776 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000777 if (force)
778 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000779
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000780 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000781 goto notfound;
782
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000783 /* If this is the first chip found, accept it.
784 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000785 * a non-generic match. SFDP and CFI are generic matches.
786 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000787 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000788 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000789 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000790 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000791 msg_cinfo("===\n"
792 "SFDP has autodetected a flash chip which is "
793 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000794 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000795 msg_cinfo("The standard operations read and "
796 "verify should work, but to support "
797 "erase, write and all other "
798 "possible features");
799 else
800 msg_cinfo("All standard operations (read, "
801 "verify, erase and write) should "
802 "work, but to support all possible "
803 "features");
804
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000805 msg_cinfo(" we need to add them manually.\n"
806 "You can help us by mailing us the output of the following command to "
807 "flashrom@flashrom.org:\n"
808 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
809 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000810 "===\n");
811 }
812
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000813 /* First flash chip detected on this bus. */
814 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000815 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000816 /* Not the first flash chip detected on this bus, but not a generic match either. */
817 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
818 break;
819 /* 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 +0000820notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000821 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000822 free(flash->chip);
823 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000824 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000825
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000826 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000827 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000828
Nico Huber7af0e792016-04-29 16:40:15 +0200829 /* Fill fallback layout covering the whole chip. */
830 struct single_layout *const fallback = &flash->fallback_layout;
831 fallback->base.entries = &fallback->entry;
832 fallback->base.num_entries = 1;
833 fallback->entry.start = 0;
834 fallback->entry.end = flash->chip->total_size * 1024 - 1;
835 fallback->entry.included = true;
Nico Huber70461a92019-06-15 14:56:19 +0200836 fallback->entry.name = strdup("complete flash");
837 if (!fallback->entry.name) {
838 msg_cerr("Failed to probe chip: %s\n", strerror(errno));
839 return -1;
840 }
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) {
1010 msg_gerr("ERROR: Flash chip %s erase function "
1011 "%i region %i has size 0. Please report"
1012 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001013 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001014 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001015 }
1016 /* Blocks with zero count are bugs in flashchips.c. */
1017 if (!eraser.eraseblocks[i].count &&
1018 eraser.eraseblocks[i].size) {
1019 msg_gerr("ERROR: Flash chip %s erase function "
1020 "%i region %i has count 0. Please report"
1021 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001022 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001023 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001024 }
1025 done += eraser.eraseblocks[i].count *
1026 eraser.eraseblocks[i].size;
Aarya Chaumal478e1792022-06-04 01:34:44 +05301027 curr_eraseblock_count += eraser.eraseblocks[i].count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001028 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001029 /* Empty eraseblock definition with erase function. */
1030 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001031 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001032 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001033 if (!done)
1034 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001035 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001036 msg_gerr("ERROR: Flash chip %s erase function %i "
1037 "region walking resulted in 0x%06x bytes total,"
1038 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001039 " flashrom@flashrom.org\n", chip->name, k,
1040 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001041 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001042 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001043 if (!eraser.block_erase)
1044 continue;
1045 /* Check if there are identical erase functions for different
1046 * layouts. That would imply "magic" erase functions. The
1047 * easiest way to check this is with function pointers.
1048 */
Uwe Hermann43959702010-03-13 17:28:29 +00001049 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001050 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001051 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001052 msg_gerr("ERROR: Flash chip %s erase function "
1053 "%i and %i are identical. Please report"
1054 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001055 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001056 ret = 1;
1057 }
Uwe Hermann43959702010-03-13 17:28:29 +00001058 }
Aarya Chaumal478e1792022-06-04 01:34:44 +05301059 if(curr_eraseblock_count > prev_eraseblock_count)
1060 {
1061 msg_gerr("ERROR: Flash chip %s erase function %i is not "
1062 "in order. Please report a bug at flashrom@flashrom.org\n",
1063 chip->name, k);
1064 ret = 1;
1065 }
1066 prev_eraseblock_count = curr_eraseblock_count;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001067 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001068 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001069}
1070
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001071static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001072{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001073 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001074
1075 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1076 if (log)
1077 msg_cdbg("not defined. ");
1078 return 1;
1079 }
1080 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1081 if (log)
1082 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001083 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001084 return 1;
1085 }
1086 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1087 if (log)
1088 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001089 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001090 return 1;
1091 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001092 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001093 return 0;
1094}
1095
Nico Huber7af0e792016-04-29 16:40:15 +02001096/**
1097 * @brief Reads the included layout regions into a buffer.
1098 *
1099 * If there is no layout set in the given flash context, the whole chip will
1100 * be read.
1101 *
1102 * @param flashctx Flash context to be used.
1103 * @param buffer Buffer of full chip size to read into.
1104 * @return 0 on success,
1105 * 1 if any read fails.
1106 */
1107static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1108{
1109 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001110 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001111
Nico Huber5ca55232019-06-15 22:29:08 +02001112 while ((entry = layout_next_included(layout, entry))) {
1113 const chipoff_t region_start = entry->start;
1114 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001115
1116 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1117 return 1;
1118 }
1119 return 0;
1120}
1121
1122typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1123/**
1124 * @private
1125 *
1126 * For read-erase-write, `curcontents` and `newcontents` shall point
1127 * to buffers of the chip's size. Both are supposed to be prefilled
1128 * with at least the included layout regions of the current flash
1129 * contents (`curcontents`) and the data to be written to the flash
1130 * (`newcontents`).
1131 *
1132 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1133 *
1134 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1135 */
1136struct walk_info {
1137 uint8_t *curcontents;
1138 const uint8_t *newcontents;
1139 chipoff_t region_start;
1140 chipoff_t region_end;
1141 chipoff_t erase_start;
1142 chipoff_t erase_end;
1143};
1144/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1145typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1146
1147static int walk_eraseblocks(struct flashctx *const flashctx,
1148 struct walk_info *const info,
1149 const size_t erasefunction, const per_blockfn_t per_blockfn)
1150{
1151 int ret;
1152 size_t i, j;
1153 bool first = true;
1154 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1155
1156 info->erase_start = 0;
1157 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1158 /* count==0 for all automatically initialized array
1159 members so the loop below won't be executed for them. */
1160 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1161 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1162
1163 /* Skip any eraseblock that is completely outside the current region. */
1164 if (info->erase_end < info->region_start)
1165 continue;
1166 if (info->region_end < info->erase_start)
1167 break;
1168
1169 /* Print this for every block except the first one. */
1170 if (first)
1171 first = false;
1172 else
1173 msg_cdbg(", ");
1174 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1175
1176 ret = per_blockfn(flashctx, info, eraser->block_erase);
1177 if (ret)
1178 return ret;
1179 }
1180 if (info->region_end < info->erase_start)
1181 break;
1182 }
1183 msg_cdbg("\n");
1184 return 0;
1185}
1186
1187static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1188 const per_blockfn_t per_blockfn)
1189{
1190 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001191 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001192
1193 all_skipped = true;
1194 msg_cinfo("Erasing and writing flash chip... ");
1195
Nico Huber5ca55232019-06-15 22:29:08 +02001196 while ((entry = layout_next_included(layout, entry))) {
1197 info->region_start = entry->start;
1198 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001199
1200 size_t j;
1201 int error = 1; /* retry as long as it's 1 */
1202 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1203 if (j != 0)
1204 msg_cinfo("Looking for another erase function.\n");
1205 msg_cdbg("Trying erase function %zi... ", j);
1206 if (check_block_eraser(flashctx, j, 1))
1207 continue;
1208
1209 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1210 if (error != 1)
1211 break;
1212
1213 if (info->curcontents) {
1214 msg_cinfo("Reading current flash chip contents... ");
1215 if (read_by_layout(flashctx, info->curcontents)) {
1216 /* Now we are truly screwed. Read failed as well. */
1217 msg_cerr("Can't read anymore! Aborting.\n");
1218 /* We have no idea about the flash chip contents, so
1219 retrying with another erase function is pointless. */
1220 error = 2;
1221 break;
1222 }
1223 msg_cinfo("done. ");
1224 }
1225 }
1226 if (error == 1)
1227 msg_cinfo("No usable erase functions left.\n");
1228 if (error) {
1229 msg_cerr("FAILED!\n");
1230 return 1;
1231 }
1232 }
1233 if (all_skipped)
1234 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1235 msg_cinfo("Erase/write done.\n");
1236 return 0;
1237}
1238
1239static int erase_block(struct flashctx *const flashctx,
1240 const struct walk_info *const info, const erasefn_t erasefn)
1241{
1242 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001243 const bool region_unaligned = info->region_start > info->erase_start ||
1244 info->erase_end > info->region_end;
1245 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1246 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001247
Nico Huber6e61e0c2019-01-23 17:07:49 +01001248 /*
1249 * If the region is not erase-block aligned, merge current flash con-
1250 * tents into a new buffer `backup_contents`.
1251 */
1252 if (region_unaligned) {
1253 backup_contents = malloc(erase_len);
1254 erased_contents = malloc(erase_len);
1255 if (!backup_contents || !erased_contents) {
1256 msg_cerr("Out of memory!\n");
1257 ret = 1;
1258 goto _free_ret;
1259 }
1260 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1261 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1262
1263 msg_cdbg("R");
1264 /* Merge data preceding the current region. */
1265 if (info->region_start > info->erase_start) {
1266 const chipoff_t start = info->erase_start;
1267 const chipsize_t len = info->region_start - info->erase_start;
1268 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1269 msg_cerr("Can't read! Aborting.\n");
1270 goto _free_ret;
1271 }
1272 }
1273 /* Merge data following the current region. */
1274 if (info->erase_end > info->region_end) {
1275 const chipoff_t start = info->region_end + 1;
1276 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1277 const chipsize_t len = info->erase_end - info->region_end;
1278 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1279 msg_cerr("Can't read! Aborting.\n");
1280 goto _free_ret;
1281 }
1282 }
1283 }
1284
1285 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001286 all_skipped = false;
1287
1288 msg_cdbg("E");
1289 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001290 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001291 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1292 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001293 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001294 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001295
1296 if (region_unaligned) {
1297 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1298 /* get_next_write() sets starthere to a new value after the call. */
1299 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1300 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1301 if (!writecount++)
1302 msg_cdbg("W");
1303 /* Needs the partial write function signature. */
1304 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1305 info->erase_start + starthere, lenhere))
1306 goto _free_ret;
1307 starthere += lenhere;
1308 }
1309 }
1310
1311 ret = 0;
1312
1313_free_ret:
1314 free(erased_contents);
1315 free(backup_contents);
1316 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001317}
1318
1319/**
1320 * @brief Erases the included layout regions.
1321 *
1322 * If there is no layout set in the given flash context, the whole chip will
1323 * be erased.
1324 *
1325 * @param flashctx Flash context to be used.
Nico Huber7af0e792016-04-29 16:40:15 +02001326 * @return 0 on success,
1327 * 1 if all available erase functions failed.
1328 */
Nico Huber454f6132012-12-10 13:34:10 +00001329static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001330{
1331 struct walk_info info = { 0 };
1332 return walk_by_layout(flashctx, &info, &erase_block);
1333}
1334
1335static int read_erase_write_block(struct flashctx *const flashctx,
1336 const struct walk_info *const info, const erasefn_t erasefn)
1337{
1338 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1339 const bool region_unaligned = info->region_start > info->erase_start ||
1340 info->erase_end > info->region_end;
1341 const uint8_t *newcontents = NULL;
1342 int ret = 2;
1343
1344 /*
1345 * If the region is not erase-block aligned, merge current flash con-
1346 * tents into `info->curcontents` and a new buffer `newc`. The former
1347 * is necessary since we have no guarantee that the full erase block
1348 * was already read into `info->curcontents`. For the latter a new
1349 * buffer is used since `info->newcontents` might contain data for
1350 * other unaligned regions that touch this erase block too.
1351 */
1352 if (region_unaligned) {
1353 msg_cdbg("R");
1354 uint8_t *const newc = malloc(erase_len);
1355 if (!newc) {
1356 msg_cerr("Out of memory!\n");
1357 return 1;
1358 }
1359 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1360
1361 /* Merge data preceding the current region. */
1362 if (info->region_start > info->erase_start) {
1363 const chipoff_t start = info->erase_start;
1364 const chipsize_t len = info->region_start - info->erase_start;
1365 if (flashctx->chip->read(flashctx, newc, start, len)) {
1366 msg_cerr("Can't read! Aborting.\n");
1367 goto _free_ret;
1368 }
1369 memcpy(info->curcontents + start, newc, len);
1370 }
1371 /* Merge data following the current region. */
1372 if (info->erase_end > info->region_end) {
1373 const chipoff_t start = info->region_end + 1;
1374 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1375 const chipsize_t len = info->erase_end - info->region_end;
1376 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1377 msg_cerr("Can't read! Aborting.\n");
1378 goto _free_ret;
1379 }
1380 memcpy(info->curcontents + start, newc + rel_start, len);
1381 }
1382
1383 newcontents = newc;
1384 } else {
1385 newcontents = info->newcontents + info->erase_start;
1386 }
1387
1388 ret = 1;
1389 bool skipped = true;
1390 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001391 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001392 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1393 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001394 if (erase_block(flashctx, info, erasefn))
1395 goto _free_ret;
1396 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001397 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001398 skipped = false;
1399 }
1400
1401 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1402 /* get_next_write() sets starthere to a new value after the call. */
1403 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1404 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1405 if (!writecount++)
1406 msg_cdbg("W");
1407 /* Needs the partial write function signature. */
1408 if (flashctx->chip->write(flashctx, newcontents + starthere,
1409 info->erase_start + starthere, lenhere))
1410 goto _free_ret;
1411 starthere += lenhere;
1412 skipped = false;
1413 }
1414 if (skipped)
1415 msg_cdbg("S");
1416 else
1417 all_skipped = false;
1418
1419 /* Update curcontents, other regions with overlapping erase blocks
1420 might rely on this. */
1421 memcpy(curcontents, newcontents, erase_len);
1422 ret = 0;
1423
1424_free_ret:
1425 if (region_unaligned)
1426 free((void *)newcontents);
1427 return ret;
1428}
1429
1430/**
1431 * @brief Writes the included layout regions from a given image.
1432 *
1433 * If there is no layout set in the given flash context, the whole image
1434 * will be written.
1435 *
1436 * @param flashctx Flash context to be used.
1437 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1438 * @param newcontents The new image to be written.
1439 * @return 0 on success,
1440 * 1 if anything has gone wrong.
1441 */
Nico Huber454f6132012-12-10 13:34:10 +00001442static int write_by_layout(struct flashctx *const flashctx,
1443 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001444{
1445 struct walk_info info;
1446 info.curcontents = curcontents;
1447 info.newcontents = newcontents;
1448 return walk_by_layout(flashctx, &info, read_erase_write_block);
1449}
1450
1451/**
1452 * @brief Compares the included layout regions with content from a buffer.
1453 *
1454 * If there is no layout set in the given flash context, the whole chip's
1455 * contents will be compared.
1456 *
1457 * @param flashctx Flash context to be used.
1458 * @param curcontents A buffer of full chip size to read current chip contents into.
1459 * @param newcontents The new image to compare to.
1460 * @return 0 on success,
1461 * 1 if reading failed,
1462 * 3 if the contents don't match.
1463 */
Nico Huber454f6132012-12-10 13:34:10 +00001464static int verify_by_layout(struct flashctx *const flashctx,
1465 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001466{
1467 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001468 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001469
Nico Huber5ca55232019-06-15 22:29:08 +02001470 while ((entry = layout_next_included(layout, entry))) {
1471 const chipoff_t region_start = entry->start;
1472 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001473
1474 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1475 return 1;
1476 if (compare_range(newcontents + region_start, curcontents + region_start,
1477 region_start, region_len))
1478 return 3;
1479 }
1480 return 0;
1481}
1482
Stefan Tauner136388f2013-07-15 10:47:53 +00001483static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001484{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001485 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001486#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001487 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001488 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
Angel Pons1900e1d2021-07-02 12:42:23 +02001489 "chip. Please report this to the mailing list at flashrom@flashrom.org or on\n"
1490 "IRC (see https://www.flashrom.org/Contact for details), thanks!\n"
Stefan Tauner136388f2013-07-15 10:47:53 +00001491 "-------------------------------------------------------------------------------\n"
1492 "You may now reboot or simply leave the machine running.\n");
1493 else
1494#endif
1495 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1496 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
Angel Pons1900e1d2021-07-02 12:42:23 +02001497 "please report this to the mailing list at flashrom@flashrom.org or on IRC (see\n"
1498 "https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001499}
1500
Stefan Tauner136388f2013-07-15 10:47:53 +00001501static void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001502{
Stefan Tauner136388f2013-07-15 10:47:53 +00001503 msg_gerr("Your flash chip is in an unknown state.\n");
1504#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001505 if (programmer == &programmer_internal)
Angel Pons1900e1d2021-07-02 12:42:23 +02001506 msg_gerr("Get help on IRC (see https://www.flashrom.org/Contact) or mail\n"
1507 "flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!"
Stefan Tauner136388f2013-07-15 10:47:53 +00001508 "-------------------------------------------------------------------------------\n"
1509 "DO NOT REBOOT OR POWEROFF!\n");
1510 else
1511#endif
Angel Pons1900e1d2021-07-02 12:42:23 +02001512 msg_gerr("Please report this to the mailing list at flashrom@flashrom.org or\n"
1513 "on IRC (see https://www.flashrom.org/Contact for details), thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001514}
1515
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001516void list_programmers_linebreak(int startcol, int cols, int paren)
1517{
1518 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001519 int pnamelen;
1520 int remaining = 0, firstline = 1;
Thomas Heijligen9163b812021-06-01 14:25:01 +02001521 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001522 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001523
Thomas Heijligend45cb592021-05-19 14:12:18 +02001524 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001525 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001526 pnamelen = strlen(pname);
1527 if (remaining - pnamelen - 2 < 0) {
1528 if (firstline)
1529 firstline = 0;
1530 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001531 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001532 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001533 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001534 remaining = cols - startcol;
1535 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001536 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001537 remaining--;
1538 }
1539 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001540 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001541 remaining--;
1542 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001543 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001544 remaining -= pnamelen;
Thomas Heijligend45cb592021-05-19 14:12:18 +02001545 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001546 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001547 remaining--;
1548 } else {
1549 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001550 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001551 }
1552 }
1553}
1554
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001555static void print_sysinfo(void)
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001556{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001557#if IS_WINDOWS
Angel Pons7e134562021-06-07 13:29:13 +02001558 SYSTEM_INFO si = { 0 };
1559 OSVERSIONINFOEX osvi = { 0 };
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001560
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001561 msg_ginfo(" on Windows");
1562 /* Tell Windows which version of the structure we want. */
1563 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1564 if (GetVersionEx((OSVERSIONINFO*) &osvi))
1565 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
1566 else
1567 msg_ginfo(" unknown version");
1568 GetSystemInfo(&si);
1569 switch (si.wProcessorArchitecture) {
1570 case PROCESSOR_ARCHITECTURE_AMD64:
1571 msg_ginfo(" (x86_64)");
1572 break;
1573 case PROCESSOR_ARCHITECTURE_INTEL:
1574 msg_ginfo(" (x86)");
1575 break;
1576 default:
1577 msg_ginfo(" (unknown arch)");
1578 break;
1579 }
1580#elif HAVE_UTSNAME == 1
1581 struct utsname osinfo;
1582
1583 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001584 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1585 osinfo.machine);
1586#else
1587 msg_ginfo(" on unknown machine");
1588#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001589}
1590
1591void print_buildinfo(void)
1592{
1593 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001594#if NEED_PCI == 1
1595#ifdef PCILIB_VERSION
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001596 msg_gdbg(" libpci %s,", PCILIB_VERSION);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001597#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001598 msg_gdbg(" unknown PCI library,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001599#endif
1600#endif
1601#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001602 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001603#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001604 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001605#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001606 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001607#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001608#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001609 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001610#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001611 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001612#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001613 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001614#endif
1615#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001616 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001617#endif
1618#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001619 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001620#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001621 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001622#else
1623#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001624#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001625 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001626}
1627
Bernhard Walle201bde32008-01-21 15:24:22 +00001628void print_version(void)
1629{
Stefan Tauner76347082016-11-27 17:45:49 +01001630 msg_ginfo("flashrom %s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001631 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001632 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00001633}
1634
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001635void print_banner(void)
1636{
1637 msg_ginfo("flashrom is free software, get the source code at "
Stefan Tauner4c723152016-01-14 22:47:55 +00001638 "https://flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001639 msg_ginfo("\n");
1640}
1641
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001642int selfcheck(void)
1643{
Stefan Tauner96658be2014-05-26 22:05:31 +00001644 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001645 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001646
Thomas Heijligend45cb592021-05-19 14:12:18 +02001647 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001648 const struct programmer_entry *const p = programmer_table[i];
1649 if (p == NULL) {
1650 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1651 ret = 1;
1652 continue;
1653 }
1654 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001655 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1656 ret = 1;
1657 /* This might hide other problems with this programmer, but allows for better error
1658 * messages below without jumping through hoops. */
1659 continue;
1660 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001661 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001662 case USB:
1663 case PCI:
1664 case OTHER:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001665 if (p->devs.note == NULL) {
1666 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001667 break; /* This one has its device list stored separately. */
1668 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001669 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001670 ret = 1;
1671 }
1672 break;
1673 default:
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001674 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001675 ret = 1;
1676 break;
1677 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001678 if (p->init == NULL) {
1679 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001680 ret = 1;
1681 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001682 if (p->delay == NULL) {
1683 msg_gerr("Programmer %s does not have a valid delay function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001684 ret = 1;
1685 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001686 if (p->map_flash_region == NULL) {
1687 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001688 ret = 1;
1689 }
Thomas Heijligen633d6db2021-03-31 19:09:44 +02001690 if (p->unmap_flash_region == NULL) {
1691 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001692 ret = 1;
1693 }
1694 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001695
1696 /* It would be favorable if we could check for the correct layout (especially termination) of various
1697 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1698 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1699 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1700 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1701 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001702 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001703 msg_gerr("Flashchips table miscompilation!\n");
1704 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001705 } else {
1706 for (i = 0; i < flashchips_size - 1; i++) {
1707 const struct flashchip *chip = &flashchips[i];
1708 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1709 ret = 1;
1710 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
1711 "Please report a bug at flashrom@flashrom.org\n", i,
1712 chip->name == NULL ? "unnamed" : chip->name);
1713 }
1714 if (selfcheck_eraseblocks(chip)) {
1715 ret = 1;
1716 }
1717 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001718 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001719
Stefan Tauner600576b2014-06-12 22:57:36 +00001720#if CONFIG_INTERNAL == 1
1721 ret |= selfcheck_board_enables();
1722#endif
1723
Stefan Tauner96658be2014-05-26 22:05:31 +00001724 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001725 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001726}
1727
Edward O'Callaghanacb24d42021-04-15 13:44:39 +10001728/* FIXME: This function signature needs to be improved once prepare_flash_access()
1729 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001730 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001731static int chip_safety_check(const struct flashctx *flash, int force,
1732 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001733{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001734 const struct flashchip *chip = flash->chip;
1735
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001736 if (!programmer_may_write && (write_it || erase_it)) {
1737 msg_perr("Write/erase is not working yet on your programmer in "
1738 "its current configuration.\n");
1739 /* --force is the wrong approach, but it's the best we can do
1740 * until the generic programmer parameter parser is merged.
1741 */
1742 if (!force)
1743 return 1;
1744 msg_cerr("Continuing anyway.\n");
1745 }
1746
1747 if (read_it || erase_it || write_it || verify_it) {
1748 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001749 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001750 msg_cerr("Read is not working on this chip. ");
1751 if (!force)
1752 return 1;
1753 msg_cerr("Continuing anyway.\n");
1754 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001755 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001756 msg_cerr("flashrom has no read function for this "
1757 "flash chip.\n");
1758 return 1;
1759 }
1760 }
1761 if (erase_it || write_it) {
1762 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001763 if (chip->tested.erase == NA) {
1764 msg_cerr("Erase is not possible on this chip.\n");
1765 return 1;
1766 }
1767 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001768 msg_cerr("Erase is not working on this chip. ");
1769 if (!force)
1770 return 1;
1771 msg_cerr("Continuing anyway.\n");
1772 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001773 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001774 msg_cerr("flashrom has no erase function for this "
1775 "flash chip.\n");
1776 return 1;
1777 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001778 }
1779 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001780 if (chip->tested.write == NA) {
1781 msg_cerr("Write is not possible on this chip.\n");
1782 return 1;
1783 }
1784 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001785 msg_cerr("Write is not working on this chip. ");
1786 if (!force)
1787 return 1;
1788 msg_cerr("Continuing anyway.\n");
1789 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001790 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001791 msg_cerr("flashrom has no write function for this "
1792 "flash chip.\n");
1793 return 1;
1794 }
1795 }
1796 return 0;
1797}
1798
Nico Huber305f4172013-06-14 11:55:26 +02001799int prepare_flash_access(struct flashctx *const flash,
1800 const bool read_it, const bool write_it,
1801 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001802{
1803 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1804 msg_cerr("Aborting.\n");
1805 return 1;
1806 }
1807
1808 if (flash->layout == get_global_layout() && normalize_romentries(flash)) {
1809 msg_cerr("Requested regions can not be handled. Aborting.\n");
1810 return 1;
1811 }
1812
1813 if (map_flash(flash) != 0)
1814 return 1;
1815
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001816 /* Initialize chip_restore_fn_count before chip unlock calls. */
1817 flash->chip_restore_fn_count = 0;
1818
Nico Huber454f6132012-12-10 13:34:10 +00001819 /* Given the existence of read locks, we want to unlock for read,
1820 erase and write. */
1821 if (flash->chip->unlock)
1822 flash->chip->unlock(flash);
1823
Nico Huberf43c6542017-10-14 17:47:28 +02001824 flash->address_high_byte = -1;
1825 flash->in_4ba_mode = false;
1826
Nico Huberdc5af542018-12-22 16:54:59 +01001827 /* Be careful about 4BA chips and broken masters */
1828 if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
1829 /* If we can't use native instructions, bail out */
1830 if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
1831 || !spi_master_4ba(flash)) {
1832 msg_cerr("Programmer doesn't support this chip. Aborting.\n");
1833 return 1;
1834 }
1835 }
1836
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001837 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01001838 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01001839 int ret;
1840 if (spi_master_4ba(flash))
1841 ret = spi_enter_4ba(flash);
1842 else
1843 ret = spi_exit_4ba(flash);
1844 if (ret) {
1845 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001846 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02001847 }
Boris Baykov99127182016-06-11 18:29:00 +02001848 }
1849
Nico Huber454f6132012-12-10 13:34:10 +00001850 return 0;
1851}
1852
Nico Huber305f4172013-06-14 11:55:26 +02001853void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001854{
Nikolai Artemiev4ad48642020-11-05 13:54:27 +11001855 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001856 unmap_flash(flash);
1857}
1858
1859/**
1860 * @addtogroup flashrom-flash
1861 * @{
1862 */
1863
1864/**
1865 * @brief Erase the specified ROM chip.
1866 *
1867 * If a layout is set in the given flash context, only included regions
1868 * will be erased.
1869 *
1870 * @param flashctx The context of the flash chip to erase.
1871 * @return 0 on success.
1872 */
1873int flashrom_flash_erase(struct flashctx *const flashctx)
1874{
1875 if (prepare_flash_access(flashctx, false, false, true, false))
1876 return 1;
1877
1878 const int ret = erase_by_layout(flashctx);
1879
1880 finalize_flash_access(flashctx);
1881
1882 return ret;
1883}
1884
1885/** @} */ /* end flashrom-flash */
1886
1887/**
1888 * @defgroup flashrom-ops Operations
1889 * @{
1890 */
1891
1892/**
1893 * @brief Read the current image from the specified ROM chip.
1894 *
1895 * If a layout is set in the specified flash context, only included regions
1896 * will be read.
1897 *
1898 * @param flashctx The context of the flash chip.
1899 * @param buffer Target buffer to write image to.
1900 * @param buffer_len Size of target buffer in bytes.
1901 * @return 0 on success,
1902 * 2 if buffer_len is too short for the flash chip's contents,
1903 * or 1 on any other failure.
1904 */
1905int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len)
1906{
1907 const size_t flash_size = flashctx->chip->total_size * 1024;
1908
1909 if (flash_size > buffer_len)
1910 return 2;
1911
1912 if (prepare_flash_access(flashctx, true, false, false, false))
1913 return 1;
1914
1915 msg_cinfo("Reading flash... ");
1916
1917 int ret = 1;
1918 if (read_by_layout(flashctx, buffer)) {
1919 msg_cerr("Read operation failed!\n");
1920 msg_cinfo("FAILED.\n");
1921 goto _finalize_ret;
1922 }
1923 msg_cinfo("done.\n");
1924 ret = 0;
1925
1926_finalize_ret:
1927 finalize_flash_access(flashctx);
1928 return ret;
1929}
1930
1931static void combine_image_by_layout(const struct flashctx *const flashctx,
1932 uint8_t *const newcontents, const uint8_t *const oldcontents)
1933{
1934 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber3d7b1e32018-12-22 00:53:14 +01001935 const struct romentry *included;
1936 chipoff_t start = 0;
Nico Huber454f6132012-12-10 13:34:10 +00001937
Nico Huber3d7b1e32018-12-22 00:53:14 +01001938 while ((included = layout_next_included_region(layout, start))) {
1939 if (included->start > start) {
1940 /* copy everything up to the start of this included region */
1941 memcpy(newcontents + start, oldcontents + start, included->start - start);
1942 }
1943 /* skip this included region */
1944 start = included->end + 1;
1945 if (start == 0)
1946 return;
Nico Huber454f6132012-12-10 13:34:10 +00001947 }
Nico Huber3d7b1e32018-12-22 00:53:14 +01001948
1949 /* copy the rest of the chip */
1950 const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start;
1951 memcpy(newcontents + start, oldcontents + start, copy_len);
Nico Huber454f6132012-12-10 13:34:10 +00001952}
1953
1954/**
1955 * @brief Write the specified image to the ROM chip.
1956 *
1957 * If a layout is set in the specified flash context, only erase blocks
1958 * containing included regions will be touched.
1959 *
1960 * @param flashctx The context of the flash chip.
Nico Huber1b172f22017-06-19 12:35:24 +02001961 * @param buffer Source buffer to read image from (may be altered for full verification).
Nico Huber454f6132012-12-10 13:34:10 +00001962 * @param buffer_len Size of source buffer in bytes.
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001963 * @param refbuffer If given, assume flash chip contains same data as `refbuffer`.
Nico Huber454f6132012-12-10 13:34:10 +00001964 * @return 0 on success,
1965 * 4 if buffer_len doesn't match the size of the flash chip,
1966 * 3 if write was tried but nothing has changed,
1967 * 2 if write failed and flash contents changed,
1968 * or 1 on any other failure.
1969 */
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001970int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len,
1971 const void *const refbuffer)
Nico Huber454f6132012-12-10 13:34:10 +00001972{
1973 const size_t flash_size = flashctx->chip->total_size * 1024;
1974 const bool verify_all = flashctx->flags.verify_whole_chip;
1975 const bool verify = flashctx->flags.verify_after_write;
1976
1977 if (buffer_len != flash_size)
1978 return 4;
1979
1980 int ret = 1;
1981
1982 uint8_t *const newcontents = buffer;
Paul Kocialkowskif701f342018-01-15 01:10:36 +03001983 const uint8_t *const refcontents = refbuffer;
Nico Huber454f6132012-12-10 13:34:10 +00001984 uint8_t *const curcontents = malloc(flash_size);
1985 uint8_t *oldcontents = NULL;
1986 if (verify_all)
1987 oldcontents = malloc(flash_size);
1988 if (!curcontents || (verify_all && !oldcontents)) {
1989 msg_gerr("Out of memory!\n");
1990 goto _free_ret;
1991 }
1992
1993#if CONFIG_INTERNAL == 1
Thomas Heijligenc7e5b8b2021-06-01 14:21:41 +02001994 if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) {
Nico Huber454f6132012-12-10 13:34:10 +00001995 if (flashctx->flags.force_boardmismatch) {
1996 msg_pinfo("Proceeding anyway because user forced us to.\n");
1997 } else {
1998 msg_perr("Aborting. You can override this with "
1999 "-p internal:boardmismatch=force.\n");
2000 goto _free_ret;
2001 }
2002 }
2003#endif
2004
2005 if (prepare_flash_access(flashctx, false, true, false, verify))
2006 goto _free_ret;
2007
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002008 /* If given, assume flash chip contains same data as `refcontents`. */
2009 if (refcontents) {
2010 msg_cinfo("Assuming old flash chip contents as ref-file...\n");
2011 memcpy(curcontents, refcontents, flash_size);
2012 if (oldcontents)
2013 memcpy(oldcontents, refcontents, flash_size);
Nico Huber454f6132012-12-10 13:34:10 +00002014 } else {
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002015 /*
2016 * Read the whole chip to be able to check whether regions need to be
2017 * erased and to give better diagnostics in case write fails.
2018 * The alternative is to read only the regions which are to be
2019 * preserved, but in that case we might perform unneeded erase which
2020 * takes time as well.
2021 */
2022 msg_cinfo("Reading old flash chip contents... ");
2023 if (verify_all) {
2024 if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) {
2025 msg_cinfo("FAILED.\n");
2026 goto _finalize_ret;
2027 }
2028 memcpy(curcontents, oldcontents, flash_size);
2029 } else {
2030 if (read_by_layout(flashctx, curcontents)) {
2031 msg_cinfo("FAILED.\n");
2032 goto _finalize_ret;
2033 }
Nico Huber454f6132012-12-10 13:34:10 +00002034 }
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002035 msg_cinfo("done.\n");
Nico Huber454f6132012-12-10 13:34:10 +00002036 }
Nico Huber454f6132012-12-10 13:34:10 +00002037
2038 if (write_by_layout(flashctx, curcontents, newcontents)) {
2039 msg_cerr("Uh oh. Erase/write failed. ");
2040 ret = 2;
2041 if (verify_all) {
2042 msg_cerr("Checking if anything has changed.\n");
2043 msg_cinfo("Reading current flash chip contents... ");
2044 if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) {
2045 msg_cinfo("done.\n");
2046 if (!memcmp(oldcontents, curcontents, flash_size)) {
2047 nonfatal_help_message();
2048 goto _finalize_ret;
2049 }
2050 msg_cerr("Apparently at least some data has changed.\n");
2051 } else
2052 msg_cerr("Can't even read anymore!\n");
2053 emergency_help_message();
2054 goto _finalize_ret;
2055 } else {
2056 msg_cerr("\n");
2057 }
2058 emergency_help_message();
2059 goto _finalize_ret;
2060 }
2061
2062 /* Verify only if we actually changed something. */
2063 if (verify && !all_skipped) {
2064 const struct flashrom_layout *const layout_bak = flashctx->layout;
2065
2066 msg_cinfo("Verifying flash... ");
2067
2068 /* Work around chips which need some time to calm down. */
2069 programmer_delay(1000*1000);
2070
2071 if (verify_all) {
2072 combine_image_by_layout(flashctx, newcontents, oldcontents);
2073 flashctx->layout = NULL;
2074 }
2075 ret = verify_by_layout(flashctx, curcontents, newcontents);
2076 flashctx->layout = layout_bak;
2077 /* If we tried to write, and verification now fails, we
2078 might have an emergency situation. */
2079 if (ret)
2080 emergency_help_message();
2081 else
2082 msg_cinfo("VERIFIED.\n");
2083 } else {
2084 /* We didn't change anything. */
2085 ret = 0;
2086 }
2087
2088_finalize_ret:
2089 finalize_flash_access(flashctx);
2090_free_ret:
2091 free(oldcontents);
2092 free(curcontents);
2093 return ret;
2094}
2095
2096/**
2097 * @brief Verify the ROM chip's contents with the specified image.
2098 *
2099 * If a layout is set in the specified flash context, only included regions
2100 * will be verified.
2101 *
2102 * @param flashctx The context of the flash chip.
2103 * @param buffer Source buffer to verify with.
2104 * @param buffer_len Size of source buffer in bytes.
2105 * @return 0 on success,
2106 * 3 if the chip's contents don't match,
2107 * 2 if buffer_len doesn't match the size of the flash chip,
2108 * or 1 on any other failure.
2109 */
2110int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len)
2111{
2112 const size_t flash_size = flashctx->chip->total_size * 1024;
2113
2114 if (buffer_len != flash_size)
2115 return 2;
2116
2117 const uint8_t *const newcontents = buffer;
2118 uint8_t *const curcontents = malloc(flash_size);
2119 if (!curcontents) {
2120 msg_gerr("Out of memory!\n");
2121 return 1;
2122 }
2123
2124 int ret = 1;
2125
2126 if (prepare_flash_access(flashctx, false, false, false, true))
2127 goto _free_ret;
2128
2129 msg_cinfo("Verifying flash... ");
2130 ret = verify_by_layout(flashctx, curcontents, newcontents);
2131 if (!ret)
2132 msg_cinfo("VERIFIED.\n");
2133
2134 finalize_flash_access(flashctx);
2135_free_ret:
2136 free(curcontents);
2137 return ret;
2138}
2139
2140/** @} */ /* end flashrom-ops */
Nico Huber899e4ec2016-04-29 18:39:01 +02002141
2142int do_read(struct flashctx *const flash, const char *const filename)
2143{
2144 if (prepare_flash_access(flash, true, false, false, false))
2145 return 1;
2146
2147 const int ret = read_flash_to_file(flash, filename);
2148
2149 finalize_flash_access(flash);
2150
2151 return ret;
2152}
2153
2154int do_erase(struct flashctx *const flash)
2155{
2156 const int ret = flashrom_flash_erase(flash);
2157
2158 /*
2159 * FIXME: Do we really want the scary warning if erase failed?
2160 * After all, after erase the chip is either blank or partially
2161 * blank or it has the old contents. A blank chip won't boot,
2162 * so if the user wanted erase and reboots afterwards, the user
2163 * knows very well that booting won't work.
2164 */
2165 if (ret)
2166 emergency_help_message();
2167
2168 return ret;
2169}
2170
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002171int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile)
Nico Huber899e4ec2016-04-29 18:39:01 +02002172{
2173 const size_t flash_size = flash->chip->total_size * 1024;
2174 int ret = 1;
2175
2176 uint8_t *const newcontents = malloc(flash_size);
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002177 uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL;
2178
2179 if (!newcontents || (referencefile && !refcontents)) {
Nico Huber899e4ec2016-04-29 18:39:01 +02002180 msg_gerr("Out of memory!\n");
2181 goto _free_ret;
2182 }
2183
2184 if (read_buf_from_file(newcontents, flash_size, filename))
2185 goto _free_ret;
2186
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002187 if (referencefile) {
2188 if (read_buf_from_file(refcontents, flash_size, referencefile))
2189 goto _free_ret;
2190 }
2191
2192 ret = flashrom_image_write(flash, newcontents, flash_size, refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002193
2194_free_ret:
Paul Kocialkowskif701f342018-01-15 01:10:36 +03002195 free(refcontents);
Nico Huber899e4ec2016-04-29 18:39:01 +02002196 free(newcontents);
2197 return ret;
2198}
2199
2200int do_verify(struct flashctx *const flash, const char *const filename)
2201{
2202 const size_t flash_size = flash->chip->total_size * 1024;
2203 int ret = 1;
2204
2205 uint8_t *const newcontents = malloc(flash_size);
2206 if (!newcontents) {
2207 msg_gerr("Out of memory!\n");
2208 goto _free_ret;
2209 }
2210
2211 if (read_buf_from_file(newcontents, flash_size, filename))
2212 goto _free_ret;
2213
2214 ret = flashrom_image_verify(flash, newcontents, flash_size);
2215
2216_free_ret:
2217 free(newcontents);
2218 return ret;
2219}