blob: 90e1864350e4feb98b1ab6de59ab66943f61af57 [file] [log] [blame]
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00001/*
Uwe Hermannd1107642007-08-29 17:52:32 +00002 * This file is part of the flashrom project.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +00003 *
Uwe Hermannd22a1d42007-09-09 20:21:05 +00004 * Copyright (C) 2000 Silicon Integrated System Corporation
5 * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com>
Uwe Hermannc7e8a0c2009-05-19 14:14:21 +00006 * Copyright (C) 2005-2008 coresystems GmbH
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +00007 * Copyright (C) 2008,2009 Carl-Daniel Hailfinger
Nico Huber7af0e792016-04-29 16:40:15 +02008 * Copyright (C) 2016 secunet Security Networks AG
9 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000010 *
Uwe Hermannd1107642007-08-29 17:52:32 +000011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000015 *
Uwe Hermannd1107642007-08-29 17:52:32 +000016 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000020 */
21
Carl-Daniel Hailfinger831e8f42010-05-30 22:24:40 +000022#include <stdio.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000023#include <sys/types.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000024#ifndef __LIBPAYLOAD__
25#include <fcntl.h>
Stefan Reinauer018aca82006-11-21 23:48:51 +000026#include <sys/stat.h>
Patrick Georgia9095a92010-09-30 17:03:32 +000027#endif
Ronald G. Minnichceec4202003-07-25 04:37:41 +000028#include <string.h>
Stefan Tauner16687702015-12-25 21:59:45 +000029#include <unistd.h>
Ronald G. Minnicheaab50b2003-09-12 22:41:53 +000030#include <stdlib.h>
Stefan Tauner363fd7e2013-04-07 13:08:30 +000031#include <errno.h>
Carl-Daniel Hailfingerc2441382010-11-09 22:00:31 +000032#include <ctype.h>
Ollie Lho184a4042005-11-26 21:55:36 +000033#include <getopt.h>
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +000034#if HAVE_UTSNAME == 1
35#include <sys/utsname.h>
36#endif
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000037#include "flash.h"
Carl-Daniel Hailfinger08454642009-06-15 14:14:48 +000038#include "flashchips.h"
Carl-Daniel Hailfinger5b997c32010-07-27 22:41:39 +000039#include "programmer.h"
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +000040#include "hwaccess.h"
Nico Huberfe34d2a2017-11-10 21:10:20 +010041#include "chipdrivers.h"
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +000042
Mathias Krausea60faab2011-01-17 07:50:42 +000043const char flashrom_version[] = FLASHROM_VERSION;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000044const char *chip_to_probe = NULL;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000045
Thomas Heijligendd8b8d52021-06-01 14:21:41 +020046static const struct programmer_entry *programmer = NULL;
Nico Huberbcb2e5a2012-12-30 01:23:17 +000047static const char *programmer_param = NULL;
Stefan Reinauer70385642007-04-06 11:58:03 +000048
Uwe Hermann48ec1b12010-08-08 17:01:18 +000049/*
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000050 * Programmers supporting multiple buses can have differing size limits on
51 * each bus. Store the limits for each bus in a common struct.
52 */
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000053struct decode_sizes max_rom_decode;
54
55/* If nonzero, used as the start address of bottom-aligned flash. */
56unsigned long flashbase;
Carl-Daniel Hailfinger66ef4e52009-12-13 22:28:00 +000057
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +000058/* Is writing allowed with this programmer? */
59int programmer_may_write;
60
Carl-Daniel Hailfinger2bee8cf2010-11-10 15:25:18 +000061#define SHUTDOWN_MAXFN 32
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000062static int shutdown_fn_count = 0;
Nico Huber454f6132012-12-10 13:34:10 +000063/** @private */
Richard Hughes93e16252018-12-19 11:54:47 +000064static struct shutdown_func_data {
David Hendricks8bb20212011-06-14 01:35:36 +000065 int (*func) (void *data);
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000066 void *data;
Richard Hughes93e16252018-12-19 11:54:47 +000067} shutdown_fn[SHUTDOWN_MAXFN];
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000068/* Initialize to 0 to make sure nobody registers a shutdown function before
69 * programmer init.
70 */
71static int may_register_shutdown = 0;
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000072
Stefan Taunerc4f44df2013-08-12 22:58:43 +000073/* Did we change something or was every erase/write skipped (if any)? */
74static bool all_skipped = true;
75
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +000076static int check_block_eraser(const struct flashctx *flash, int k, int log);
Stefan Tauner5368dca2011-07-01 00:19:12 +000077
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000078/* Register a function to be executed on programmer shutdown.
79 * The advantage over atexit() is that you can supply a void pointer which will
80 * be used as parameter to the registered function upon programmer shutdown.
81 * This pointer can point to arbitrary data used by said function, e.g. undo
82 * information for GPIO settings etc. If unneeded, set data=NULL.
83 * Please note that the first (void *data) belongs to the function signature of
84 * the function passed as first parameter.
85 */
David Hendricks8bb20212011-06-14 01:35:36 +000086int register_shutdown(int (*function) (void *data), void *data)
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000087{
88 if (shutdown_fn_count >= SHUTDOWN_MAXFN) {
Carl-Daniel Hailfinger9f5f2152010-06-04 23:20:21 +000089 msg_perr("Tried to register more than %i shutdown functions.\n",
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000090 SHUTDOWN_MAXFN);
91 return 1;
92 }
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +000093 if (!may_register_shutdown) {
94 msg_perr("Tried to register a shutdown function before "
95 "programmer init.\n");
96 return 1;
97 }
Carl-Daniel Hailfingercc389fc2010-02-14 01:20:28 +000098 shutdown_fn[shutdown_fn_count].func = function;
99 shutdown_fn[shutdown_fn_count].data = data;
100 shutdown_fn_count++;
101
102 return 0;
103}
104
Nikolai Artemievf745d0e2020-11-05 13:54:27 +1100105int register_chip_restore(chip_restore_fn_cb_t func,
106 struct flashctx *flash, uint8_t status)
107{
108 if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) {
109 msg_perr("Tried to register more than %i chip restore"
110 " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS);
111 return 1;
112 }
113 flash->chip_restore_fn[flash->chip_restore_fn_count].func = func;
114 flash->chip_restore_fn[flash->chip_restore_fn_count].status = status;
115 flash->chip_restore_fn_count++;
116
117 return 0;
118}
119
120static int deregister_chip_restore(struct flashctx *flash)
121{
122 int rc = 0;
123
124 while (flash->chip_restore_fn_count > 0) {
125 int i = --flash->chip_restore_fn_count;
126 rc |= flash->chip_restore_fn[i].func(
127 flash, flash->chip_restore_fn[i].status);
128 }
129
130 return rc;
131}
132
Thomas Heijligen5d25f042021-06-01 14:37:12 +0200133int programmer_init(const struct programmer_entry *prog, const char *param)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000134{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000135 int ret;
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000136
Thomas Heijligen5d25f042021-06-01 14:37:12 +0200137 if (prog == NULL) {
Carl-Daniel Hailfinger2e681602011-09-08 00:00:29 +0000138 msg_perr("Invalid programmer specified!\n");
139 return -1;
140 }
Thomas Heijligen5d25f042021-06-01 14:37:12 +0200141 programmer = prog;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000142 /* Initialize all programmer specific data. */
143 /* Default to unlimited decode sizes. */
144 max_rom_decode = (const struct decode_sizes) {
145 .parallel = 0xffffffff,
146 .lpc = 0xffffffff,
147 .fwh = 0xffffffff,
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000148 .spi = 0xffffffff,
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000149 };
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000150 /* Default to top aligned flash at 4 GB. */
151 flashbase = 0;
152 /* Registering shutdown functions is now allowed. */
153 may_register_shutdown = 1;
Carl-Daniel Hailfingerd1be52d2010-07-03 12:14:25 +0000154 /* Default to allowing writes. Broken programmers set this to 0. */
155 programmer_may_write = 1;
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000156
157 programmer_param = param;
Thomas Heijligendd8b8d52021-06-01 14:21:41 +0200158 msg_pdbg("Initializing %s programmer\n", programmer->name);
159 ret = programmer->init();
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000160 if (programmer_param && strlen(programmer_param)) {
Carl-Daniel Hailfinger20a36ba2013-08-13 07:09:57 +0000161 if (ret != 0) {
162 /* It is quite possible that any unhandled programmer parameter would have been valid,
163 * but an error in actual programmer init happened before the parameter was evaluated.
164 */
165 msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n",
166 programmer_param);
167 } else {
168 /* Actual programmer init was successful, but the user specified an invalid or unusable
169 * (for the current programmer configuration) parameter.
170 */
171 msg_perr("Unhandled programmer parameters: %s\n", programmer_param);
172 msg_perr("Aborting.\n");
173 ret = ERROR_FATAL;
174 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000175 }
176 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000177}
178
Stefan Tauner20da4aa2014-05-07 22:07:23 +0000179/** Calls registered shutdown functions and resets internal programmer-related variables.
180 * Calling it is safe even without previous initialization, but further interactions with programmer support
181 * require a call to programmer_init() (afterwards).
182 *
183 * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */
Uwe Hermann09e04f72009-05-16 22:36:00 +0000184int programmer_shutdown(void)
185{
David Hendricks8bb20212011-06-14 01:35:36 +0000186 int ret = 0;
187
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000188 /* Registering shutdown functions is no longer allowed. */
189 may_register_shutdown = 0;
190 while (shutdown_fn_count > 0) {
191 int i = --shutdown_fn_count;
David Hendricks8bb20212011-06-14 01:35:36 +0000192 ret |= shutdown_fn[i].func(shutdown_fn[i].data);
Carl-Daniel Hailfingerad3cc552010-07-03 11:02:10 +0000193 }
Stefan Taunere34e3e82013-01-01 00:06:51 +0000194
Stefan Taunerb8911d62012-12-26 07:55:00 +0000195 programmer_param = NULL;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000196 registered_master_count = 0;
Stefan Taunere34e3e82013-01-01 00:06:51 +0000197
David Hendricks8bb20212011-06-14 01:35:36 +0000198 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000199}
200
Stefan Tauner305e0b92013-07-17 23:46:44 +0000201void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000202{
Thomas Heijligendd8b8d52021-06-01 14:21:41 +0200203 void *ret = programmer->map_flash_region(descr, phys_addr, len);
Stefan Tauner26e7a152013-09-13 17:21:05 +0000204 msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n",
205 __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret);
206 return ret;
Uwe Hermann09e04f72009-05-16 22:36:00 +0000207}
208
209void programmer_unmap_flash_region(void *virt_addr, size_t len)
210{
Thomas Heijligendd8b8d52021-06-01 14:21:41 +0200211 programmer->unmap_flash_region(virt_addr, len);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000212 msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000213}
214
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000215void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000216{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000217 flash->mst->par.chip_writeb(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000218}
219
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000220void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000221{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000222 flash->mst->par.chip_writew(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000223}
224
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000225void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000226{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000227 flash->mst->par.chip_writel(flash, val, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000228}
229
Mark Marshallf20b7be2014-05-09 21:16:21 +0000230void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000231{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000232 flash->mst->par.chip_writen(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000233}
234
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000235uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000236{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000237 return flash->mst->par.chip_readb(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000238}
239
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000240uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000241{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000242 return flash->mst->par.chip_readw(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000243}
244
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000245uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr)
Uwe Hermann09e04f72009-05-16 22:36:00 +0000246{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000247 return flash->mst->par.chip_readl(flash, addr);
Uwe Hermann09e04f72009-05-16 22:36:00 +0000248}
249
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000250void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr,
251 size_t len)
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000252{
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000253 flash->mst->par.chip_readn(flash, buf, addr, len);
Carl-Daniel Hailfinger0bd2a2b2009-06-05 18:32:07 +0000254}
255
Stefan Taunerf80419c2014-05-02 15:41:42 +0000256void programmer_delay(unsigned int usecs)
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000257{
Urja Rannikko8d7ec2a2013-10-21 21:49:08 +0000258 if (usecs > 0)
Thomas Heijligendd8b8d52021-06-01 14:21:41 +0200259 programmer->delay(usecs);
Carl-Daniel Hailfingerca8bfc62009-06-05 17:48:08 +0000260}
261
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000262int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start,
263 int unsigned len)
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000264{
Carl-Daniel Hailfinger8a3c60c2011-12-18 15:01:24 +0000265 chip_readn(flash, buf, flash->virtual_memory + start, len);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000266
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000267 return 0;
268}
269
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000270/* This is a somewhat hacked function similar in some ways to strtok().
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000271 * It will look for needle with a subsequent '=' in haystack, return a copy of
272 * needle and remove everything from the first occurrence of needle to the next
273 * delimiter from haystack.
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000274 */
Nico Huberbcb2e5a2012-12-30 01:23:17 +0000275char *extract_param(const char *const *haystack, const char *needle, const char *delim)
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000276{
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000277 char *param_pos, *opt_pos, *rest;
278 char *opt = NULL;
279 int optlen;
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000280 int needlelen;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000281
Carl-Daniel Hailfinger27023762010-04-28 15:22:14 +0000282 needlelen = strlen(needle);
283 if (!needlelen) {
284 msg_gerr("%s: empty needle! Please report a bug at "
285 "flashrom@flashrom.org\n", __func__);
286 return NULL;
287 }
288 /* No programmer parameters given. */
289 if (*haystack == NULL)
290 return NULL;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000291 param_pos = strstr(*haystack, needle);
292 do {
293 if (!param_pos)
294 return NULL;
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000295 /* Needle followed by '='? */
296 if (param_pos[needlelen] == '=') {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000297 /* Beginning of the string? */
298 if (param_pos == *haystack)
299 break;
300 /* After a delimiter? */
301 if (strchr(delim, *(param_pos - 1)))
302 break;
303 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000304 /* Continue searching. */
305 param_pos++;
306 param_pos = strstr(param_pos, needle);
307 } while (1);
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000308
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000309 if (param_pos) {
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000310 /* Get the string after needle and '='. */
311 opt_pos = param_pos + needlelen + 1;
312 optlen = strcspn(opt_pos, delim);
313 /* Return an empty string if the parameter was empty. */
314 opt = malloc(optlen + 1);
315 if (!opt) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000316 msg_gerr("Out of memory!\n");
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000317 exit(1);
318 }
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000319 strncpy(opt, opt_pos, optlen);
320 opt[optlen] = '\0';
321 rest = opt_pos + optlen;
322 /* Skip all delimiters after the current parameter. */
323 rest += strspn(rest, delim);
324 memmove(param_pos, rest, strlen(rest) + 1);
325 /* We could shrink haystack, but the effort is not worth it. */
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000326 }
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000327
Carl-Daniel Hailfinger744132a2010-07-06 09:55:48 +0000328 return opt;
Carl-Daniel Hailfingerd5b28fa2009-11-24 18:27:10 +0000329}
330
Stefan Tauner66652442011-06-26 17:38:17 +0000331char *extract_programmer_param(const char *param_name)
Carl-Daniel Hailfinger2b6dcb32010-07-08 10:13:37 +0000332{
333 return extract_param(&programmer_param, param_name, ",");
334}
335
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +0000336/* Returns the number of well-defined erasers for a chip. */
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +0000337static unsigned int count_usable_erasers(const struct flashctx *flash)
Stefan Tauner5368dca2011-07-01 00:19:12 +0000338{
339 unsigned int usable_erasefunctions = 0;
340 int k;
341 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
342 if (!check_block_eraser(flash, k, 0))
343 usable_erasefunctions++;
344 }
345 return usable_erasefunctions;
346}
347
Mark Marshallf20b7be2014-05-09 21:16:21 +0000348static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len)
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000349{
350 int ret = 0, failcount = 0;
351 unsigned int i;
352 for (i = 0; i < len; i++) {
353 if (wantbuf[i] != havebuf[i]) {
354 /* Only print the first failure. */
355 if (!failcount++)
356 msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,",
357 start + i, wantbuf[i], havebuf[i]);
358 }
359 }
360 if (failcount) {
361 msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n",
362 start, start + len - 1, failcount);
363 ret = -1;
364 }
365 return ret;
366}
367
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000368/* start is an offset to the base address of the flash chip */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -0600369static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000370{
371 int ret;
372 uint8_t *cmpbuf = malloc(len);
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300373 const uint8_t erased_value = ERASED_VALUE(flash);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000374
375 if (!cmpbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000376 msg_gerr("Could not allocate memory!\n");
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000377 exit(1);
378 }
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300379 memset(cmpbuf, erased_value, len);
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000380 ret = verify_range(flash, cmpbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000381 free(cmpbuf);
382 return ret;
383}
384
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000385/*
Carl-Daniel Hailfingerd0250a32009-11-25 17:05:52 +0000386 * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000387 * flash content at location start
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000388 * @start offset to the base address of the flash chip
389 * @len length of the verified area
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000390 * @return 0 for success, -1 for failure
391 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000392int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len)
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000393{
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000394 if (!len)
Stefan Taunerdf64a422014-05-27 00:06:14 +0000395 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000396
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000397 if (!flash->chip->read) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000398 msg_cerr("ERROR: flashrom has no read function for this flash chip.\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000399 return -1;
Carl-Daniel Hailfinger23290662009-06-24 08:20:45 +0000400 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000401
402 uint8_t *readbuf = malloc(len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000403 if (!readbuf) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000404 msg_gerr("Could not allocate memory!\n");
Stefan Taunerdf64a422014-05-27 00:06:14 +0000405 return -1;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000406 }
Stefan Taunerdf64a422014-05-27 00:06:14 +0000407 int ret = 0;
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000408
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000409 if (start + len > flash->chip->total_size * 1024) {
Sean Nelson316a29f2010-05-07 20:09:04 +0000410 msg_gerr("Error: %s called with start 0x%x + len 0x%x >"
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000411 " total_size 0x%x\n", __func__, start, len,
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000412 flash->chip->total_size * 1024);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000413 ret = -1;
414 goto out_free;
415 }
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000416
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000417 ret = flash->chip->read(flash, readbuf, start, len);
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000418 if (ret) {
419 msg_gerr("Verification impossible because read failed "
420 "at 0x%x (len 0x%x)\n", start, len);
Stefan Taunerdf64a422014-05-27 00:06:14 +0000421 ret = -1;
422 goto out_free;
Carl-Daniel Hailfingerd8369412010-11-16 17:21:58 +0000423 }
424
Stefan Tauner78ffbea2012-10-27 15:36:56 +0000425 ret = compare_range(cmpbuf, readbuf, start, len);
Carl-Daniel Hailfinger30f7cb22009-06-15 17:23:36 +0000426out_free:
427 free(readbuf);
428 return ret;
429}
430
Stefan Tauner02437452013-04-01 19:34:53 +0000431/* Helper function for need_erase() that focuses on granularities of gran bytes. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300432static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len,
433 unsigned int gran, const uint8_t erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000434{
435 unsigned int i, j, limit;
436 for (j = 0; j < len / gran; j++) {
437 limit = min (gran, len - j * gran);
438 /* Are 'have' and 'want' identical? */
439 if (!memcmp(have + j * gran, want + j * gran, limit))
440 continue;
441 /* have needs to be in erased state. */
442 for (i = 0; i < limit; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300443 if (have[j * gran + i] != erased_value)
Stefan Tauner02437452013-04-01 19:34:53 +0000444 return 1;
445 }
446 return 0;
447}
448
Uwe Hermann48ec1b12010-08-08 17:01:18 +0000449/*
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000450 * Check if the buffer @have can be programmed to the content of @want without
451 * erasing. This is only possible if all chunks of size @gran are either kept
452 * as-is or changed from an all-ones state to any other state.
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000453 *
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000454 * Warning: This function assumes that @have and @want point to naturally
455 * aligned regions.
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000456 *
457 * @have buffer with current content
458 * @want buffer with desired content
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000459 * @len length of the checked area
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000460 * @gran write granularity (enum, not count)
461 * @return 0 if no erase is needed, 1 otherwise
462 */
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300463int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len,
464 enum write_granularity gran, const uint8_t erased_value)
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000465{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +0000466 int result = 0;
Stefan Tauner02437452013-04-01 19:34:53 +0000467 unsigned int i;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000468
469 switch (gran) {
470 case write_gran_1bit:
471 for (i = 0; i < len; i++)
472 if ((have[i] & want[i]) != want[i]) {
473 result = 1;
474 break;
475 }
476 break;
477 case write_gran_1byte:
478 for (i = 0; i < len; i++)
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300479 if ((have[i] != want[i]) && (have[i] != erased_value)) {
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000480 result = 1;
481 break;
482 }
483 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000484 case write_gran_128bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300485 result = need_erase_gran_bytes(have, want, len, 128, erased_value);
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000486 break;
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000487 case write_gran_256bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300488 result = need_erase_gran_bytes(have, want, len, 256, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000489 break;
490 case write_gran_264bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300491 result = need_erase_gran_bytes(have, want, len, 264, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000492 break;
493 case write_gran_512bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300494 result = need_erase_gran_bytes(have, want, len, 512, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000495 break;
496 case write_gran_528bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300497 result = need_erase_gran_bytes(have, want, len, 528, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000498 break;
499 case write_gran_1024bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300500 result = need_erase_gran_bytes(have, want, len, 1024, erased_value);
Stefan Tauner02437452013-04-01 19:34:53 +0000501 break;
502 case write_gran_1056bytes:
Paul Kocialkowski995f7552018-01-15 01:06:09 +0300503 result = need_erase_gran_bytes(have, want, len, 1056, erased_value);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000504 break;
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000505 case write_gran_1byte_implicit_erase:
506 /* Do not erase, handle content changes from anything->0xff by writing 0xff. */
507 result = 0;
508 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000509 default:
510 msg_cerr("%s: Unsupported granularity! Please report a bug at "
511 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfingere8e369f2010-03-08 00:42:32 +0000512 }
513 return result;
514}
515
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000516/**
517 * Check if the buffer @have needs to be programmed to get the content of @want.
518 * If yes, return 1 and fill in first_start with the start address of the
519 * write operation and first_len with the length of the first to-be-written
520 * chunk. If not, return 0 and leave first_start and first_len undefined.
521 *
522 * Warning: This function assumes that @have and @want point to naturally
523 * aligned regions.
524 *
525 * @have buffer with current content
526 * @want buffer with desired content
527 * @len length of the checked area
528 * @gran write granularity (enum, not count)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000529 * @first_start offset of the first byte which needs to be written (passed in
530 * value is increased by the offset of the first needed write
531 * relative to have/want or unchanged if no write is needed)
532 * @return length of the first contiguous area which needs to be written
533 * 0 if no write is needed
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000534 *
535 * FIXME: This function needs a parameter which tells it about coalescing
536 * in relation to the max write length of the programmer and the max write
537 * length of the chip.
538 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000539static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len,
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000540 unsigned int *first_start,
541 enum write_granularity gran)
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000542{
Stefan Taunerc69c9c82011-11-23 09:13:48 +0000543 int need_write = 0;
544 unsigned int rel_start = 0, first_len = 0;
545 unsigned int i, limit, stride;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000546
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000547 switch (gran) {
548 case write_gran_1bit:
549 case write_gran_1byte:
Carl-Daniel Hailfinger1b0e9fc2014-06-16 22:36:17 +0000550 case write_gran_1byte_implicit_erase:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000551 stride = 1;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000552 break;
Paul Kocialkowskic8305e12015-10-16 02:16:20 +0000553 case write_gran_128bytes:
554 stride = 128;
555 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000556 case write_gran_256bytes:
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000557 stride = 256;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000558 break;
Stefan Tauner02437452013-04-01 19:34:53 +0000559 case write_gran_264bytes:
560 stride = 264;
561 break;
562 case write_gran_512bytes:
563 stride = 512;
564 break;
565 case write_gran_528bytes:
566 stride = 528;
567 break;
568 case write_gran_1024bytes:
569 stride = 1024;
570 break;
571 case write_gran_1056bytes:
572 stride = 1056;
573 break;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000574 default:
575 msg_cerr("%s: Unsupported granularity! Please report a bug at "
576 "flashrom@flashrom.org\n", __func__);
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000577 /* Claim that no write was needed. A write with unknown
578 * granularity is too dangerous to try.
579 */
580 return 0;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000581 }
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000582 for (i = 0; i < len / stride; i++) {
583 limit = min(stride, len - i * stride);
584 /* Are 'have' and 'want' identical? */
585 if (memcmp(have + i * stride, want + i * stride, limit)) {
586 if (!need_write) {
587 /* First location where have and want differ. */
588 need_write = 1;
589 rel_start = i * stride;
590 }
591 } else {
592 if (need_write) {
593 /* First location where have and want
594 * do not differ anymore.
595 */
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000596 break;
597 }
598 }
599 }
Carl-Daniel Hailfinger202bf532010-12-06 13:05:44 +0000600 if (need_write)
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000601 first_len = min(i * stride - rel_start, len);
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000602 *first_start += rel_start;
Carl-Daniel Hailfinger12d6d822010-11-05 14:51:59 +0000603 return first_len;
Carl-Daniel Hailfinger6e2ea322010-11-04 01:04:27 +0000604}
605
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000606/* Returns the number of busses commonly supported by the current programmer and flash chip where the latter
607 * can not be completely accessed due to size/address limits of the programmer. */
608unsigned int count_max_decode_exceedings(const struct flashctx *flash)
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000609{
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000610 unsigned int limitexceeded = 0;
611 uint32_t size = flash->chip->total_size * 1024;
612 enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype;
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000613
614 if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000615 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000616 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000617 "size %u kB of chipset/board/programmer "
618 "for %s interface, "
619 "probe/read/erase/write may fail. ", size / 1024,
620 max_rom_decode.parallel / 1024, "Parallel");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000621 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000622 if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000623 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000624 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000625 "size %u kB of chipset/board/programmer "
626 "for %s interface, "
627 "probe/read/erase/write may fail. ", size / 1024,
628 max_rom_decode.lpc / 1024, "LPC");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000629 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000630 if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000631 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000632 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000633 "size %u kB of chipset/board/programmer "
634 "for %s interface, "
635 "probe/read/erase/write may fail. ", size / 1024,
636 max_rom_decode.fwh / 1024, "FWH");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000637 }
Carl-Daniel Hailfinger1a227952011-07-27 07:13:06 +0000638 if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) {
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000639 limitexceeded++;
Sean Nelson316a29f2010-05-07 20:09:04 +0000640 msg_pdbg("Chip size %u kB is bigger than supported "
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000641 "size %u kB of chipset/board/programmer "
642 "for %s interface, "
643 "probe/read/erase/write may fail. ", size / 1024,
644 max_rom_decode.spi / 1024, "SPI");
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000645 }
Stefan Tauner9e3a6982014-08-15 17:17:59 +0000646 return limitexceeded;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000647}
648
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000649void unmap_flash(struct flashctx *flash)
650{
651 if (flash->virtual_registers != (chipaddr)ERROR_PTR) {
652 programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024);
653 flash->physical_registers = 0;
654 flash->virtual_registers = (chipaddr)ERROR_PTR;
655 }
656
657 if (flash->virtual_memory != (chipaddr)ERROR_PTR) {
658 programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024);
659 flash->physical_memory = 0;
660 flash->virtual_memory = (chipaddr)ERROR_PTR;
661 }
662}
663
664int map_flash(struct flashctx *flash)
665{
666 /* Init pointers to the fail-safe state to distinguish them later from legit values. */
667 flash->virtual_memory = (chipaddr)ERROR_PTR;
668 flash->virtual_registers = (chipaddr)ERROR_PTR;
669
670 /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0.
671 * These are used for various probing-related hacks that would not map successfully anyway and should be
672 * removed ASAP. */
673 if (flash->chip->total_size == 0)
674 return 0;
675
676 const chipsize_t size = flash->chip->total_size * 1024;
677 uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1);
678 void *addr = programmer_map_flash_region(flash->chip->name, base, size);
679 if (addr == ERROR_PTR) {
680 msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n",
681 flash->chip->name, PRIxPTR_WIDTH, base);
682 return 1;
683 }
684 flash->physical_memory = base;
685 flash->virtual_memory = (chipaddr)addr;
686
687 /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere
688 * completely different on some chips and programmers, or not mappable at all.
689 * Ignore these problems for now and always report success. */
690 if (flash->chip->feature_bits & FEATURE_REGISTERMAP) {
691 base = 0xffffffff - size - 0x400000 + 1;
692 addr = programmer_map_flash_region("flash chip registers", base, size);
693 if (addr == ERROR_PTR) {
694 msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n",
695 flash->chip->name, PRIxPTR_WIDTH, base);
696 return 0;
697 }
698 flash->physical_registers = base;
699 flash->virtual_registers = (chipaddr)addr;
700 }
701 return 0;
702}
703
Nico Huber2d625722016-05-03 10:48:02 +0200704/*
705 * Return a string corresponding to the bustype parameter.
706 * Memory is obtained with malloc() and must be freed with free() by the caller.
707 */
708char *flashbuses_to_text(enum chipbustype bustype)
709{
710 char *ret = calloc(1, 1);
711 /*
712 * FIXME: Once all chipsets and flash chips have been updated, NONSPI
713 * will cease to exist and should be eliminated here as well.
714 */
715 if (bustype == BUS_NONSPI) {
716 ret = strcat_realloc(ret, "Non-SPI, ");
717 } else {
718 if (bustype & BUS_PARALLEL)
719 ret = strcat_realloc(ret, "Parallel, ");
720 if (bustype & BUS_LPC)
721 ret = strcat_realloc(ret, "LPC, ");
722 if (bustype & BUS_FWH)
723 ret = strcat_realloc(ret, "FWH, ");
724 if (bustype & BUS_SPI)
725 ret = strcat_realloc(ret, "SPI, ");
726 if (bustype & BUS_PROG)
727 ret = strcat_realloc(ret, "Programmer-specific, ");
728 if (bustype == BUS_NONE)
729 ret = strcat_realloc(ret, "None, ");
730 }
731 /* Kill last comma. */
732 ret[strlen(ret) - 2] = '\0';
733 ret = realloc(ret, strlen(ret) + 1);
734 return ret;
735}
736
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000737int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force)
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000738{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000739 const struct flashchip *chip;
Carl-Daniel Hailfinger115d3902009-10-31 01:53:09 +0000740 enum chipbustype buses_common;
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000741 char *tmp;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000742
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000743 for (chip = flashchips + startchip; chip && chip->name; chip++) {
744 if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0)
Ollie Lhocbbf1252004-03-17 22:22:08 +0000745 continue;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000746 buses_common = mst->buses_supported & chip->bustype;
Carl-Daniel Hailfingerc40cff72011-12-20 00:19:29 +0000747 if (!buses_common)
Carl-Daniel Hailfinger6573b742011-06-17 22:38:53 +0000748 continue;
Mike Banon31b5e3b2018-01-15 01:10:00 +0300749 /* Only probe for SPI25 chips by default. */
750 if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25)
751 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000752 msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size);
753 if (!chip->probe && !force) {
754 msg_gdbg("failed! flashrom has no probe function for this flash chip.\n");
Carl-Daniel Hailfingerb22918c2009-06-01 02:08:58 +0000755 continue;
756 }
Stefan Reinauer70385642007-04-06 11:58:03 +0000757
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000758 /* Start filling in the dynamic data. */
Angel Pons3bd47522021-06-07 12:33:53 +0200759 flash->chip = calloc(1, sizeof(*flash->chip));
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000760 if (!flash->chip) {
761 msg_gerr("Out of memory!\n");
762 exit(1);
763 }
Angel Ponsf41d2482021-06-07 13:29:13 +0200764 *flash->chip = *chip;
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000765 flash->mst = mst;
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000766
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000767 if (map_flash(flash) != 0)
Martin Schiller57a3b732017-11-23 06:24:57 +0100768 goto notfound;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000769
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000770 /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash()
771 * is only called with force=1 after normal probing failed.
772 */
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000773 if (force)
774 break;
Stefan Reinauerfcb63682006-03-16 16:57:41 +0000775
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000776 if (flash->chip->probe(flash) != 1)
Peter Stuge483b8f02008-09-03 23:10:05 +0000777 goto notfound;
778
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000779 /* If this is the first chip found, accept it.
780 * If this is not the first chip found, accept it only if it is
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000781 * a non-generic match. SFDP and CFI are generic matches.
782 * startchip==0 means this call to probe_flash() is the first
Carl-Daniel Hailfingera5bcbce2014-07-19 22:03:29 +0000783 * one for this programmer interface (master) and thus no other chip has
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000784 * been found on this interface.
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000785 */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000786 if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) {
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000787 msg_cinfo("===\n"
788 "SFDP has autodetected a flash chip which is "
789 "not natively supported by flashrom yet.\n");
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000790 if (count_usable_erasers(flash) == 0)
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000791 msg_cinfo("The standard operations read and "
792 "verify should work, but to support "
793 "erase, write and all other "
794 "possible features");
795 else
796 msg_cinfo("All standard operations (read, "
797 "verify, erase and write) should "
798 "work, but to support all possible "
799 "features");
800
Stefan Taunerb4e06bd2012-08-20 00:24:22 +0000801 msg_cinfo(" we need to add them manually.\n"
802 "You can help us by mailing us the output of the following command to "
803 "flashrom@flashrom.org:\n"
804 "'flashrom -VV [plus the -p/--programmer parameter]'\n"
805 "Thanks for your help!\n"
Stefan Taunerac1b4c82012-02-17 14:51:04 +0000806 "===\n");
807 }
808
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000809 /* First flash chip detected on this bus. */
810 if (startchip == 0)
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000811 break;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000812 /* Not the first flash chip detected on this bus, but not a generic match either. */
813 if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID))
814 break;
815 /* 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 +0000816notfound:
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000817 unmap_flash(flash);
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000818 free(flash->chip);
819 flash->chip = NULL;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000820 }
Uwe Hermannffec5f32007-08-23 16:08:21 +0000821
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000822 if (!flash->chip)
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000823 return -1;
Peter Stuge27c3e2d2008-07-02 17:15:47 +0000824
Nico Huber7af0e792016-04-29 16:40:15 +0200825 /* Fill fallback layout covering the whole chip. */
826 struct single_layout *const fallback = &flash->fallback_layout;
827 fallback->base.entries = &fallback->entry;
828 fallback->base.num_entries = 1;
829 fallback->entry.start = 0;
830 fallback->entry.end = flash->chip->total_size * 1024 - 1;
831 fallback->entry.included = true;
Nico Huber70461a92019-06-15 14:56:19 +0200832 fallback->entry.name = strdup("complete flash");
833 if (!fallback->entry.name) {
834 msg_cerr("Failed to probe chip: %s\n", strerror(errno));
835 return -1;
836 }
Stefan Reinauer051e2362011-01-19 06:21:54 +0000837
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000838 tmp = flashbuses_to_text(flash->chip->bustype);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000839 msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found",
840 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
Stefan Tauner00155492011-06-26 20:45:35 +0000841 free(tmp);
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000842#if CONFIG_INTERNAL == 1
Thomas Heijligendd8b8d52021-06-01 14:21:41 +0200843 if (programmer->map_flash_region == physmap)
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000844 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
845 PRIxPTR_WIDTH, flash->physical_memory);
846 else
847#endif
Thomas Heijligendd8b8d52021-06-01 14:21:41 +0200848 msg_cinfo("on %s.\n", programmer->name);
Uwe Hermann9899cad2009-06-28 21:47:57 +0000849
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000850 /* Flash registers may more likely not be mapped if the chip was forced.
851 * Lock info may be stored in registers, so avoid lock info printing. */
Carl-Daniel Hailfinger859f3f02010-12-02 21:59:42 +0000852 if (!force)
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000853 if (flash->chip->printlock)
854 flash->chip->printlock(flash);
Sean Nelson6e0b9122010-02-19 00:52:10 +0000855
Stefan Tauner4e32ec12014-08-30 23:39:51 +0000856 /* Get out of the way for later runs. */
857 unmap_flash(flash);
858
Carl-Daniel Hailfinger4c823182011-05-04 00:39:50 +0000859 /* Return position of matching chip. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +0000860 return chip - flashchips;
Ronald G. Minnichf4cf2ba2002-01-29 18:26:26 +0000861}
862
Uwe Hermann91f4afa2011-07-28 08:13:25 +0000863int read_buf_from_file(unsigned char *buf, unsigned long size,
864 const char *filename)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000865{
Nico Huber7562f7d2013-08-30 21:29:45 +0000866#ifdef __LIBPAYLOAD__
867 msg_gerr("Error: No file I/O support in libpayload\n");
868 return 1;
869#else
Stefan Tauner16687702015-12-25 21:59:45 +0000870 int ret = 0;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000871
Stefan Tauner16687702015-12-25 21:59:45 +0000872 FILE *image;
Daniel Campellod4063bf2021-04-15 10:36:04 -0600873 if (!strcmp(filename, "-"))
874 image = fdopen(fileno(stdin), "rb");
875 else
876 image = fopen(filename, "rb");
877 if (image == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000878 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000879 return 1;
880 }
Stefan Tauner16687702015-12-25 21:59:45 +0000881
882 struct stat image_stat;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000883 if (fstat(fileno(image), &image_stat) != 0) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000884 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
Stefan Tauner16687702015-12-25 21:59:45 +0000885 ret = 1;
886 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000887 }
Daniel Campellod4063bf2021-04-15 10:36:04 -0600888 if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) {
Daniel Campello45d50a12021-04-13 10:47:25 -0600889 msg_gerr("Error: Image size (%jd B) doesn't match the expected size (%lu B)!\n",
Stefan Taunere038e902013-02-04 04:38:42 +0000890 (intmax_t)image_stat.st_size, size);
Stefan Tauner16687702015-12-25 21:59:45 +0000891 ret = 1;
892 goto out;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000893 }
Stefan Tauner16687702015-12-25 21:59:45 +0000894
895 unsigned long numbytes = fread(buf, 1, size, image);
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000896 if (numbytes != size) {
897 msg_gerr("Error: Failed to read complete file. Got %ld bytes, "
898 "wanted %ld!\n", numbytes, size);
Stefan Tauner16687702015-12-25 21:59:45 +0000899 ret = 1;
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000900 }
Stefan Tauner16687702015-12-25 21:59:45 +0000901out:
902 (void)fclose(image);
903 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +0000904#endif
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +0000905}
906
Daniel Campello45d50a12021-04-13 10:47:25 -0600907/**
908 * @brief Reads content to buffer from one or more files.
909 *
910 * Reads content to supplied buffer from files. If a filename is specified for
911 * individual regions using the partial read syntax ('-i <region>[:<filename>]')
912 * then this will read file data into the corresponding region in the
913 * supplied buffer.
914 *
915 * @param flashctx Flash context to be used.
916 * @param buf Chip-sized buffer to write data to
917 * @return 0 on success
918 */
919static int read_buf_from_include_args(const struct flashctx *const flash,
920 unsigned char *buf)
921{
922 const struct flashrom_layout *const layout = get_layout(flash);
923 const struct romentry *entry = NULL;
924
925 /*
926 * Content will be read from -i args, so they must not overlap since
927 * we need to know exactly what content to write to the ROM.
928 */
929 if (included_regions_overlap(layout)) {
930 msg_gerr("Error: Included regions must not overlap when writing.\n");
931 return 1;
932 }
933
934 while ((entry = layout_next_included(layout, entry))) {
935 if (!entry->file)
936 continue;
937 if (read_buf_from_file(buf + entry->start,
938 entry->end - entry->start + 1, entry->file))
939 return 1;
940 }
941 return 0;
942}
943
944/**
945 * @brief Writes passed data buffer into a file
946 *
947 * @param buf Buffer with data to write
948 * @param size Size of buffer
949 * @param filename File path to write to
950 * @return 0 on success
951 */
Mark Marshallf20b7be2014-05-09 21:16:21 +0000952int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename)
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000953{
Nico Huber7562f7d2013-08-30 21:29:45 +0000954#ifdef __LIBPAYLOAD__
955 msg_gerr("Error: No file I/O support in libpayload\n");
956 return 1;
957#else
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000958 FILE *image;
Stefan Tauner16687702015-12-25 21:59:45 +0000959 int ret = 0;
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000960
961 if (!filename) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000962 msg_gerr("No filename specified.\n");
Stephan Guilloux21dd55b2009-06-01 22:07:52 +0000963 return 1;
964 }
Patrick Georgi0bf842d2010-01-25 22:55:33 +0000965 if ((image = fopen(filename, "wb")) == NULL) {
Stefan Tauner363fd7e2013-04-07 13:08:30 +0000966 msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno));
Carl-Daniel Hailfinger03b4e712009-05-08 12:49:03 +0000967 return 1;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000968 }
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +0000969
Stefan Tauner16687702015-12-25 21:59:45 +0000970 unsigned long numbytes = fwrite(buf, 1, size, image);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000971 if (numbytes != size) {
Stefan Tauner16687702015-12-25 21:59:45 +0000972 msg_gerr("Error: file %s could not be written completely.\n", filename);
973 ret = 1;
974 goto out;
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +0000975 }
Stefan Tauner16687702015-12-25 21:59:45 +0000976 if (fflush(image)) {
977 msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno));
978 ret = 1;
979 }
980 // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW).
981#if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1)
982 struct stat image_stat;
983 if (fstat(fileno(image), &image_stat) != 0) {
984 msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno));
985 ret = 1;
986 goto out;
987 }
988 if (S_ISREG(image_stat.st_mode)) {
989 if (fsync(fileno(image))) {
990 msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno));
991 ret = 1;
992 }
993 }
994#endif
995out:
996 if (fclose(image)) {
997 msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno));
998 ret = 1;
999 }
1000 return ret;
Nico Huber7562f7d2013-08-30 21:29:45 +00001001#endif
Carl-Daniel Hailfinger7314cc32009-01-28 00:27:54 +00001002}
1003
Daniel Campello45d50a12021-04-13 10:47:25 -06001004/**
1005 * @brief Writes content from buffer to one or more files.
1006 *
1007 * Writes content from supplied buffer to files. If a filename is specified for
1008 * individual regions using the partial read syntax ('-i <region>[:<filename>]')
1009 * then this will write files using data from the corresponding region in the
1010 * supplied buffer.
1011 *
1012 * @param flashctx Flash context to be used.
1013 * @param buf Chip-sized buffer to read data from
1014 * @return 0 on success
1015 */
1016static int write_buf_to_include_args(const struct flashctx *const flash,
1017 unsigned char *buf)
1018{
1019 const struct flashrom_layout *const layout = get_layout(flash);
1020 const struct romentry *entry = NULL;
1021
1022 while ((entry = layout_next_included(layout, entry))) {
1023 if (!entry->file)
1024 continue;
1025 if (write_buf_to_file(buf + entry->start,
1026 entry->end - entry->start + 1, entry->file))
1027 return 1;
1028 }
1029
1030 return 0;
1031}
1032
Nico Huber899e4ec2016-04-29 18:39:01 +02001033static int read_by_layout(struct flashctx *, uint8_t *);
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001034int read_flash_to_file(struct flashctx *flash, const char *filename)
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001035{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001036 unsigned long size = flash->chip->total_size * 1024;
Richard Hughes84b453e2018-12-19 15:30:39 +00001037 unsigned char *buf = calloc(size, sizeof(unsigned char));
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001038 int ret = 0;
1039
1040 msg_cinfo("Reading flash... ");
1041 if (!buf) {
1042 msg_gerr("Memory allocation failed!\n");
1043 msg_cinfo("FAILED.\n");
1044 return 1;
1045 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001046 if (!flash->chip->read) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001047 msg_cerr("No read function available for this flash chip.\n");
1048 ret = 1;
1049 goto out_free;
1050 }
Nico Huber899e4ec2016-04-29 18:39:01 +02001051 if (read_by_layout(flash, buf)) {
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001052 msg_cerr("Read operation failed!\n");
1053 ret = 1;
1054 goto out_free;
1055 }
Daniel Campello45d50a12021-04-13 10:47:25 -06001056 if (write_buf_to_include_args(flash, buf)) {
1057 ret = 1;
1058 goto out_free;
1059 }
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001060
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001061 ret = write_buf_to_file(buf, size, filename);
Carl-Daniel Hailfinger1748c572010-07-13 23:56:13 +00001062out_free:
1063 free(buf);
1064 msg_cinfo("%s.\n", ret ? "FAILED" : "done");
1065 return ret;
1066}
1067
Stefan Tauner96658be2014-05-26 22:05:31 +00001068/* Even if an error is found, the function will keep going and check the rest. */
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001069static int selfcheck_eraseblocks(const struct flashchip *chip)
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001070{
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001071 int i, j, k;
1072 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001073
1074 for (k = 0; k < NUM_ERASEFUNCTIONS; k++) {
1075 unsigned int done = 0;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001076 struct block_eraser eraser = chip->block_erasers[k];
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001077
1078 for (i = 0; i < NUM_ERASEREGIONS; i++) {
1079 /* Blocks with zero size are bugs in flashchips.c. */
1080 if (eraser.eraseblocks[i].count &&
1081 !eraser.eraseblocks[i].size) {
1082 msg_gerr("ERROR: Flash chip %s erase function "
1083 "%i region %i has size 0. Please report"
1084 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001085 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001086 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001087 }
1088 /* Blocks with zero count are bugs in flashchips.c. */
1089 if (!eraser.eraseblocks[i].count &&
1090 eraser.eraseblocks[i].size) {
1091 msg_gerr("ERROR: Flash chip %s erase function "
1092 "%i region %i has count 0. Please report"
1093 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001094 chip->name, k, i);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001095 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001096 }
1097 done += eraser.eraseblocks[i].count *
1098 eraser.eraseblocks[i].size;
1099 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001100 /* Empty eraseblock definition with erase function. */
1101 if (!done && eraser.block_erase)
Sean Nelson316a29f2010-05-07 20:09:04 +00001102 msg_gspew("Strange: Empty eraseblock definition with "
Uwe Hermann91f4afa2011-07-28 08:13:25 +00001103 "non-empty erase function. Not an error.\n");
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001104 if (!done)
1105 continue;
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001106 if (done != chip->total_size * 1024) {
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001107 msg_gerr("ERROR: Flash chip %s erase function %i "
1108 "region walking resulted in 0x%06x bytes total,"
1109 " expected 0x%06x bytes. Please report a bug at"
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001110 " flashrom@flashrom.org\n", chip->name, k,
1111 done, chip->total_size * 1024);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001112 ret = 1;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001113 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001114 if (!eraser.block_erase)
1115 continue;
1116 /* Check if there are identical erase functions for different
1117 * layouts. That would imply "magic" erase functions. The
1118 * easiest way to check this is with function pointers.
1119 */
Uwe Hermann43959702010-03-13 17:28:29 +00001120 for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001121 if (eraser.block_erase ==
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001122 chip->block_erasers[j].block_erase) {
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001123 msg_gerr("ERROR: Flash chip %s erase function "
1124 "%i and %i are identical. Please report"
1125 " a bug at flashrom@flashrom.org\n",
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001126 chip->name, k, j);
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001127 ret = 1;
1128 }
Uwe Hermann43959702010-03-13 17:28:29 +00001129 }
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001130 }
Carl-Daniel Hailfinger415afcf2010-01-19 06:42:46 +00001131 return ret;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001132}
1133
Carl-Daniel Hailfinger63fd9022011-12-14 22:25:15 +00001134static int check_block_eraser(const struct flashctx *flash, int k, int log)
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001135{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001136 struct block_eraser eraser = flash->chip->block_erasers[k];
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001137
1138 if (!eraser.block_erase && !eraser.eraseblocks[0].count) {
1139 if (log)
1140 msg_cdbg("not defined. ");
1141 return 1;
1142 }
1143 if (!eraser.block_erase && eraser.eraseblocks[0].count) {
1144 if (log)
1145 msg_cdbg("eraseblock layout is known, but matching "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001146 "block erase function is not implemented. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001147 return 1;
1148 }
1149 if (eraser.block_erase && !eraser.eraseblocks[0].count) {
1150 if (log)
1151 msg_cdbg("block erase function found, but "
Stefan Tauner355cbfd2011-05-28 02:37:14 +00001152 "eraseblock layout is not defined. ");
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001153 return 1;
1154 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001155 // TODO: Once erase functions are annotated with allowed buses, check that as well.
Carl-Daniel Hailfingerdce73ae2010-12-05 15:14:44 +00001156 return 0;
1157}
1158
Nico Huber7af0e792016-04-29 16:40:15 +02001159/**
1160 * @brief Reads the included layout regions into a buffer.
1161 *
1162 * If there is no layout set in the given flash context, the whole chip will
1163 * be read.
1164 *
1165 * @param flashctx Flash context to be used.
1166 * @param buffer Buffer of full chip size to read into.
1167 * @return 0 on success,
1168 * 1 if any read fails.
1169 */
1170static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer)
1171{
1172 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001173 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001174
Nico Huber5ca55232019-06-15 22:29:08 +02001175 while ((entry = layout_next_included(layout, entry))) {
1176 const chipoff_t region_start = entry->start;
1177 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001178
1179 if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len))
1180 return 1;
1181 }
1182 return 0;
1183}
1184
1185typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len);
1186/**
1187 * @private
1188 *
1189 * For read-erase-write, `curcontents` and `newcontents` shall point
1190 * to buffers of the chip's size. Both are supposed to be prefilled
1191 * with at least the included layout regions of the current flash
1192 * contents (`curcontents`) and the data to be written to the flash
1193 * (`newcontents`).
1194 *
1195 * For erase, `curcontents` and `newcontents` shall be NULL-pointers.
1196 *
1197 * The `chipoff_t` values are used internally by `walk_by_layout()`.
1198 */
1199struct walk_info {
1200 uint8_t *curcontents;
1201 const uint8_t *newcontents;
1202 chipoff_t region_start;
1203 chipoff_t region_end;
1204 chipoff_t erase_start;
1205 chipoff_t erase_end;
1206};
1207/* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */
1208typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t);
1209
1210static int walk_eraseblocks(struct flashctx *const flashctx,
1211 struct walk_info *const info,
1212 const size_t erasefunction, const per_blockfn_t per_blockfn)
1213{
1214 int ret;
1215 size_t i, j;
1216 bool first = true;
1217 struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction];
1218
1219 info->erase_start = 0;
1220 for (i = 0; i < NUM_ERASEREGIONS; ++i) {
1221 /* count==0 for all automatically initialized array
1222 members so the loop below won't be executed for them. */
1223 for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) {
1224 info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1;
1225
1226 /* Skip any eraseblock that is completely outside the current region. */
1227 if (info->erase_end < info->region_start)
1228 continue;
1229 if (info->region_end < info->erase_start)
1230 break;
1231
1232 /* Print this for every block except the first one. */
1233 if (first)
1234 first = false;
1235 else
1236 msg_cdbg(", ");
1237 msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end);
1238
1239 ret = per_blockfn(flashctx, info, eraser->block_erase);
1240 if (ret)
1241 return ret;
1242 }
1243 if (info->region_end < info->erase_start)
1244 break;
1245 }
1246 msg_cdbg("\n");
1247 return 0;
1248}
1249
1250static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info,
1251 const per_blockfn_t per_blockfn)
1252{
1253 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001254 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001255
1256 all_skipped = true;
1257 msg_cinfo("Erasing and writing flash chip... ");
1258
Nico Huber5ca55232019-06-15 22:29:08 +02001259 while ((entry = layout_next_included(layout, entry))) {
1260 info->region_start = entry->start;
1261 info->region_end = entry->end;
Nico Huber7af0e792016-04-29 16:40:15 +02001262
1263 size_t j;
1264 int error = 1; /* retry as long as it's 1 */
1265 for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) {
1266 if (j != 0)
1267 msg_cinfo("Looking for another erase function.\n");
1268 msg_cdbg("Trying erase function %zi... ", j);
1269 if (check_block_eraser(flashctx, j, 1))
1270 continue;
1271
1272 error = walk_eraseblocks(flashctx, info, j, per_blockfn);
1273 if (error != 1)
1274 break;
1275
1276 if (info->curcontents) {
1277 msg_cinfo("Reading current flash chip contents... ");
1278 if (read_by_layout(flashctx, info->curcontents)) {
1279 /* Now we are truly screwed. Read failed as well. */
1280 msg_cerr("Can't read anymore! Aborting.\n");
1281 /* We have no idea about the flash chip contents, so
1282 retrying with another erase function is pointless. */
1283 error = 2;
1284 break;
1285 }
1286 msg_cinfo("done. ");
1287 }
1288 }
1289 if (error == 1)
1290 msg_cinfo("No usable erase functions left.\n");
1291 if (error) {
1292 msg_cerr("FAILED!\n");
1293 return 1;
1294 }
1295 }
1296 if (all_skipped)
1297 msg_cinfo("\nWarning: Chip content is identical to the requested image.\n");
1298 msg_cinfo("Erase/write done.\n");
1299 return 0;
1300}
1301
1302static int erase_block(struct flashctx *const flashctx,
1303 const struct walk_info *const info, const erasefn_t erasefn)
1304{
1305 const unsigned int erase_len = info->erase_end + 1 - info->erase_start;
Nico Huber6e61e0c2019-01-23 17:07:49 +01001306 const bool region_unaligned = info->region_start > info->erase_start ||
1307 info->erase_end > info->region_end;
1308 uint8_t *backup_contents = NULL, *erased_contents = NULL;
1309 int ret = 2;
Nico Huber7af0e792016-04-29 16:40:15 +02001310
Nico Huber6e61e0c2019-01-23 17:07:49 +01001311 /*
1312 * If the region is not erase-block aligned, merge current flash con-
1313 * tents into a new buffer `backup_contents`.
1314 */
1315 if (region_unaligned) {
1316 backup_contents = malloc(erase_len);
1317 erased_contents = malloc(erase_len);
1318 if (!backup_contents || !erased_contents) {
1319 msg_cerr("Out of memory!\n");
1320 ret = 1;
1321 goto _free_ret;
1322 }
1323 memset(backup_contents, ERASED_VALUE(flashctx), erase_len);
1324 memset(erased_contents, ERASED_VALUE(flashctx), erase_len);
1325
1326 msg_cdbg("R");
1327 /* Merge data preceding the current region. */
1328 if (info->region_start > info->erase_start) {
1329 const chipoff_t start = info->erase_start;
1330 const chipsize_t len = info->region_start - info->erase_start;
1331 if (flashctx->chip->read(flashctx, backup_contents, start, len)) {
1332 msg_cerr("Can't read! Aborting.\n");
1333 goto _free_ret;
1334 }
1335 }
1336 /* Merge data following the current region. */
1337 if (info->erase_end > info->region_end) {
1338 const chipoff_t start = info->region_end + 1;
1339 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1340 const chipsize_t len = info->erase_end - info->region_end;
1341 if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) {
1342 msg_cerr("Can't read! Aborting.\n");
1343 goto _free_ret;
1344 }
1345 }
1346 }
1347
1348 ret = 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001349 all_skipped = false;
1350
1351 msg_cdbg("E");
1352 if (erasefn(flashctx, info->erase_start, erase_len))
Nico Huber6e61e0c2019-01-23 17:07:49 +01001353 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001354 if (check_erased_range(flashctx, info->erase_start, erase_len)) {
1355 msg_cerr("ERASE FAILED!\n");
Nico Huber6e61e0c2019-01-23 17:07:49 +01001356 goto _free_ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001357 }
Nico Huber6e61e0c2019-01-23 17:07:49 +01001358
1359 if (region_unaligned) {
1360 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1361 /* get_next_write() sets starthere to a new value after the call. */
1362 while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere,
1363 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1364 if (!writecount++)
1365 msg_cdbg("W");
1366 /* Needs the partial write function signature. */
1367 if (flashctx->chip->write(flashctx, backup_contents + starthere,
1368 info->erase_start + starthere, lenhere))
1369 goto _free_ret;
1370 starthere += lenhere;
1371 }
1372 }
1373
1374 ret = 0;
1375
1376_free_ret:
1377 free(erased_contents);
1378 free(backup_contents);
1379 return ret;
Nico Huber7af0e792016-04-29 16:40:15 +02001380}
1381
1382/**
1383 * @brief Erases the included layout regions.
1384 *
1385 * If there is no layout set in the given flash context, the whole chip will
1386 * be erased.
1387 *
1388 * @param flashctx Flash context to be used.
1389 * @param buffer Buffer of full chip size to read into.
1390 * @return 0 on success,
1391 * 1 if all available erase functions failed.
1392 */
Nico Huber454f6132012-12-10 13:34:10 +00001393static int erase_by_layout(struct flashctx *const flashctx)
Nico Huber7af0e792016-04-29 16:40:15 +02001394{
1395 struct walk_info info = { 0 };
1396 return walk_by_layout(flashctx, &info, &erase_block);
1397}
1398
1399static int read_erase_write_block(struct flashctx *const flashctx,
1400 const struct walk_info *const info, const erasefn_t erasefn)
1401{
1402 const chipsize_t erase_len = info->erase_end + 1 - info->erase_start;
1403 const bool region_unaligned = info->region_start > info->erase_start ||
1404 info->erase_end > info->region_end;
1405 const uint8_t *newcontents = NULL;
1406 int ret = 2;
1407
1408 /*
1409 * If the region is not erase-block aligned, merge current flash con-
1410 * tents into `info->curcontents` and a new buffer `newc`. The former
1411 * is necessary since we have no guarantee that the full erase block
1412 * was already read into `info->curcontents`. For the latter a new
1413 * buffer is used since `info->newcontents` might contain data for
1414 * other unaligned regions that touch this erase block too.
1415 */
1416 if (region_unaligned) {
1417 msg_cdbg("R");
1418 uint8_t *const newc = malloc(erase_len);
1419 if (!newc) {
1420 msg_cerr("Out of memory!\n");
1421 return 1;
1422 }
1423 memcpy(newc, info->newcontents + info->erase_start, erase_len);
1424
1425 /* Merge data preceding the current region. */
1426 if (info->region_start > info->erase_start) {
1427 const chipoff_t start = info->erase_start;
1428 const chipsize_t len = info->region_start - info->erase_start;
1429 if (flashctx->chip->read(flashctx, newc, start, len)) {
1430 msg_cerr("Can't read! Aborting.\n");
1431 goto _free_ret;
1432 }
1433 memcpy(info->curcontents + start, newc, len);
1434 }
1435 /* Merge data following the current region. */
1436 if (info->erase_end > info->region_end) {
1437 const chipoff_t start = info->region_end + 1;
1438 const chipoff_t rel_start = start - info->erase_start; /* within this erase block */
1439 const chipsize_t len = info->erase_end - info->region_end;
1440 if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) {
1441 msg_cerr("Can't read! Aborting.\n");
1442 goto _free_ret;
1443 }
1444 memcpy(info->curcontents + start, newc + rel_start, len);
1445 }
1446
1447 newcontents = newc;
1448 } else {
1449 newcontents = info->newcontents + info->erase_start;
1450 }
1451
1452 ret = 1;
1453 bool skipped = true;
1454 uint8_t *const curcontents = info->curcontents + info->erase_start;
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001455 const uint8_t erased_value = ERASED_VALUE(flashctx);
David Hendricksf9a30552015-05-23 20:30:30 -07001456 if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) &&
1457 need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) {
Nico Huber7af0e792016-04-29 16:40:15 +02001458 if (erase_block(flashctx, info, erasefn))
1459 goto _free_ret;
1460 /* Erase was successful. Adjust curcontents. */
Paul Kocialkowski995f7552018-01-15 01:06:09 +03001461 memset(curcontents, erased_value, erase_len);
Nico Huber7af0e792016-04-29 16:40:15 +02001462 skipped = false;
1463 }
1464
1465 unsigned int starthere = 0, lenhere = 0, writecount = 0;
1466 /* get_next_write() sets starthere to a new value after the call. */
1467 while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere,
1468 erase_len - starthere, &starthere, flashctx->chip->gran))) {
1469 if (!writecount++)
1470 msg_cdbg("W");
1471 /* Needs the partial write function signature. */
1472 if (flashctx->chip->write(flashctx, newcontents + starthere,
1473 info->erase_start + starthere, lenhere))
1474 goto _free_ret;
1475 starthere += lenhere;
1476 skipped = false;
1477 }
1478 if (skipped)
1479 msg_cdbg("S");
1480 else
1481 all_skipped = false;
1482
1483 /* Update curcontents, other regions with overlapping erase blocks
1484 might rely on this. */
1485 memcpy(curcontents, newcontents, erase_len);
1486 ret = 0;
1487
1488_free_ret:
1489 if (region_unaligned)
1490 free((void *)newcontents);
1491 return ret;
1492}
1493
1494/**
1495 * @brief Writes the included layout regions from a given image.
1496 *
1497 * If there is no layout set in the given flash context, the whole image
1498 * will be written.
1499 *
1500 * @param flashctx Flash context to be used.
1501 * @param curcontents A buffer of full chip size with current chip contents of included regions.
1502 * @param newcontents The new image to be written.
1503 * @return 0 on success,
1504 * 1 if anything has gone wrong.
1505 */
Nico Huber454f6132012-12-10 13:34:10 +00001506static int write_by_layout(struct flashctx *const flashctx,
1507 void *const curcontents, const void *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001508{
1509 struct walk_info info;
1510 info.curcontents = curcontents;
1511 info.newcontents = newcontents;
1512 return walk_by_layout(flashctx, &info, read_erase_write_block);
1513}
1514
1515/**
1516 * @brief Compares the included layout regions with content from a buffer.
1517 *
1518 * If there is no layout set in the given flash context, the whole chip's
1519 * contents will be compared.
1520 *
1521 * @param flashctx Flash context to be used.
1522 * @param curcontents A buffer of full chip size to read current chip contents into.
1523 * @param newcontents The new image to compare to.
1524 * @return 0 on success,
1525 * 1 if reading failed,
1526 * 3 if the contents don't match.
1527 */
Nico Huber454f6132012-12-10 13:34:10 +00001528static int verify_by_layout(struct flashctx *const flashctx,
1529 void *const curcontents, const uint8_t *const newcontents)
Nico Huber7af0e792016-04-29 16:40:15 +02001530{
1531 const struct flashrom_layout *const layout = get_layout(flashctx);
Nico Huber5ca55232019-06-15 22:29:08 +02001532 const struct romentry *entry = NULL;
Nico Huber7af0e792016-04-29 16:40:15 +02001533
Nico Huber5ca55232019-06-15 22:29:08 +02001534 while ((entry = layout_next_included(layout, entry))) {
1535 const chipoff_t region_start = entry->start;
1536 const chipsize_t region_len = entry->end - entry->start + 1;
Nico Huber7af0e792016-04-29 16:40:15 +02001537
1538 if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len))
1539 return 1;
1540 if (compare_range(newcontents + region_start, curcontents + region_start,
1541 region_start, region_len))
1542 return 3;
1543 }
1544 return 0;
1545}
1546
Stefan Tauner136388f2013-07-15 10:47:53 +00001547static void nonfatal_help_message(void)
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001548{
Stefan Taunera58f6e92014-05-10 09:25:44 +00001549 msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n");
Stefan Tauner136388f2013-07-15 10:47:53 +00001550#if CONFIG_INTERNAL == 1
Thomas Heijligendd8b8d52021-06-01 14:21:41 +02001551 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001552 msg_gerr("This means we have to add special support for your board, programmer or flash\n"
1553 "chip. Please report this on IRC at chat.freenode.net (channel #flashrom) or\n"
1554 "mail flashrom@flashrom.org, thanks!\n"
1555 "-------------------------------------------------------------------------------\n"
1556 "You may now reboot or simply leave the machine running.\n");
1557 else
1558#endif
1559 msg_gerr("Please check the connections (especially those to write protection pins) between\n"
1560 "the programmer and the flash chip. If you think the error is caused by flashrom\n"
1561 "please report this on IRC at chat.freenode.net (channel #flashrom) or\n"
1562 "mail flashrom@flashrom.org, thanks!\n");
Carl-Daniel Hailfinger42d38a92010-10-19 22:06:20 +00001563}
1564
Stefan Tauner136388f2013-07-15 10:47:53 +00001565static void emergency_help_message(void)
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001566{
Stefan Tauner136388f2013-07-15 10:47:53 +00001567 msg_gerr("Your flash chip is in an unknown state.\n");
1568#if CONFIG_INTERNAL == 1
Thomas Heijligendd8b8d52021-06-01 14:21:41 +02001569 if (programmer == &programmer_internal)
Stefan Tauner136388f2013-07-15 10:47:53 +00001570 msg_gerr("Get help on IRC at chat.freenode.net (channel #flashrom) or\n"
1571 "mail flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!\n"
1572 "-------------------------------------------------------------------------------\n"
1573 "DO NOT REBOOT OR POWEROFF!\n");
1574 else
1575#endif
1576 msg_gerr("Please report this on IRC at chat.freenode.net (channel #flashrom) or\n"
1577 "mail flashrom@flashrom.org, thanks!\n");
Carl-Daniel Hailfinger8ab49e72009-08-19 13:55:34 +00001578}
1579
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001580void list_programmers_linebreak(int startcol, int cols, int paren)
1581{
1582 const char *pname;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001583 int pnamelen;
1584 int remaining = 0, firstline = 1;
Thomas Heijligen4bb08642021-06-01 14:25:01 +02001585 size_t p;
Carl-Daniel Hailfinger082c8b52011-08-15 19:54:20 +00001586 int i;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001587
Thomas Heijligene14b2ec2021-05-19 14:12:18 +02001588 for (p = 0; p < programmer_table_size; p++) {
Thomas Heijligendee884e2021-03-31 19:09:44 +02001589 pname = programmer_table[p]->name;
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001590 pnamelen = strlen(pname);
1591 if (remaining - pnamelen - 2 < 0) {
1592 if (firstline)
1593 firstline = 0;
1594 else
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001595 msg_ginfo("\n");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001596 for (i = 0; i < startcol; i++)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001597 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001598 remaining = cols - startcol;
1599 } else {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001600 msg_ginfo(" ");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001601 remaining--;
1602 }
1603 if (paren && (p == 0)) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001604 msg_ginfo("(");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001605 remaining--;
1606 }
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001607 msg_ginfo("%s", pname);
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001608 remaining -= pnamelen;
Thomas Heijligene14b2ec2021-05-19 14:12:18 +02001609 if (p < programmer_table_size - 1) {
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001610 msg_ginfo(",");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001611 remaining--;
1612 } else {
1613 if (paren)
Carl-Daniel Hailfinger901a3ba2012-05-14 22:54:58 +00001614 msg_ginfo(")");
Carl-Daniel Hailfingera73fb492010-10-06 23:48:34 +00001615 }
1616 }
1617}
1618
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001619static void print_sysinfo(void)
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001620{
Stefan Taunerb0eee9b2015-01-10 09:32:50 +00001621#if IS_WINDOWS
Angel Ponsf41d2482021-06-07 13:29:13 +02001622 SYSTEM_INFO si = { 0 };
1623 OSVERSIONINFOEX osvi = { 0 };
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001624
Carl-Daniel Hailfinger60d9bd22012-08-09 23:34:41 +00001625 msg_ginfo(" on Windows");
1626 /* Tell Windows which version of the structure we want. */
1627 osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1628 if (GetVersionEx((OSVERSIONINFO*) &osvi))
1629 msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion);
1630 else
1631 msg_ginfo(" unknown version");
1632 GetSystemInfo(&si);
1633 switch (si.wProcessorArchitecture) {
1634 case PROCESSOR_ARCHITECTURE_AMD64:
1635 msg_ginfo(" (x86_64)");
1636 break;
1637 case PROCESSOR_ARCHITECTURE_INTEL:
1638 msg_ginfo(" (x86)");
1639 break;
1640 default:
1641 msg_ginfo(" (unknown arch)");
1642 break;
1643 }
1644#elif HAVE_UTSNAME == 1
1645 struct utsname osinfo;
1646
1647 uname(&osinfo);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001648 msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release,
1649 osinfo.machine);
1650#else
1651 msg_ginfo(" on unknown machine");
1652#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001653}
1654
1655void print_buildinfo(void)
1656{
1657 msg_gdbg("flashrom was built with");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001658#if NEED_PCI == 1
1659#ifdef PCILIB_VERSION
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001660 msg_gdbg(" libpci %s,", PCILIB_VERSION);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001661#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001662 msg_gdbg(" unknown PCI library,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001663#endif
1664#endif
1665#ifdef __clang__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001666 msg_gdbg(" LLVM Clang");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001667#ifdef __clang_version__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001668 msg_gdbg(" %s,", __clang_version__);
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001669#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001670 msg_gdbg(" unknown version (before r102686),");
Carl-Daniel Hailfingerb51e58e2010-07-17 14:49:30 +00001671#endif
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001672#elif defined(__GNUC__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001673 msg_gdbg(" GCC");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001674#ifdef __VERSION__
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001675 msg_gdbg(" %s,", __VERSION__);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001676#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001677 msg_gdbg(" unknown version,");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001678#endif
1679#else
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001680 msg_gdbg(" unknown compiler,");
Carl-Daniel Hailfingercceafa22010-05-26 01:45:41 +00001681#endif
1682#if defined (__FLASHROM_LITTLE_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001683 msg_gdbg(" little endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001684#elif defined (__FLASHROM_BIG_ENDIAN__)
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001685 msg_gdbg(" big endian");
Carl-Daniel Hailfinger06b9efa2012-08-07 11:59:59 +00001686#else
1687#error Endianness could not be determined
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001688#endif
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001689 msg_gdbg("\n");
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001690}
1691
Bernhard Walle201bde32008-01-21 15:24:22 +00001692void print_version(void)
1693{
Stefan Tauner76347082016-11-27 17:45:49 +01001694 msg_ginfo("flashrom %s", flashrom_version);
Carl-Daniel Hailfinger132e2ec2010-03-27 16:36:40 +00001695 print_sysinfo();
Carl-Daniel Hailfinger1c155482012-06-06 09:17:06 +00001696 msg_ginfo("\n");
Bernhard Walle201bde32008-01-21 15:24:22 +00001697}
1698
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001699void print_banner(void)
1700{
1701 msg_ginfo("flashrom is free software, get the source code at "
Stefan Tauner4c723152016-01-14 22:47:55 +00001702 "https://flashrom.org\n");
Carl-Daniel Hailfinger8841d3e2010-05-15 15:04:37 +00001703 msg_ginfo("\n");
1704}
1705
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001706int selfcheck(void)
1707{
Stefan Tauner96658be2014-05-26 22:05:31 +00001708 unsigned int i;
Stefan Taunera6d96482012-12-26 19:51:23 +00001709 int ret = 0;
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001710
Thomas Heijligene14b2ec2021-05-19 14:12:18 +02001711 for (i = 0; i < programmer_table_size; i++) {
Thomas Heijligendee884e2021-03-31 19:09:44 +02001712 const struct programmer_entry *const p = programmer_table[i];
1713 if (p == NULL) {
1714 msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i);
1715 ret = 1;
1716 continue;
1717 }
1718 if (p->name == NULL) {
Stefan Taunera6d96482012-12-26 19:51:23 +00001719 msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i);
1720 ret = 1;
1721 /* This might hide other problems with this programmer, but allows for better error
1722 * messages below without jumping through hoops. */
1723 continue;
1724 }
Thomas Heijligendee884e2021-03-31 19:09:44 +02001725 switch (p->type) {
Stefan Tauneraf358d62012-12-27 18:40:26 +00001726 case USB:
1727 case PCI:
1728 case OTHER:
Thomas Heijligendee884e2021-03-31 19:09:44 +02001729 if (p->devs.note == NULL) {
1730 if (strcmp("internal", p->name) == 0)
Stefan Tauneraf358d62012-12-27 18:40:26 +00001731 break; /* This one has its device list stored separately. */
1732 msg_gerr("Programmer %s has neither a device list nor a textual description!\n",
Thomas Heijligendee884e2021-03-31 19:09:44 +02001733 p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001734 ret = 1;
1735 }
1736 break;
1737 default:
Thomas Heijligendee884e2021-03-31 19:09:44 +02001738 msg_gerr("Programmer %s does not have a valid type set!\n", p->name);
Stefan Tauneraf358d62012-12-27 18:40:26 +00001739 ret = 1;
1740 break;
1741 }
Thomas Heijligendee884e2021-03-31 19:09:44 +02001742 if (p->init == NULL) {
1743 msg_gerr("Programmer %s does not have a valid init function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001744 ret = 1;
1745 }
Thomas Heijligendee884e2021-03-31 19:09:44 +02001746 if (p->delay == NULL) {
1747 msg_gerr("Programmer %s does not have a valid delay function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001748 ret = 1;
1749 }
Thomas Heijligendee884e2021-03-31 19:09:44 +02001750 if (p->map_flash_region == NULL) {
1751 msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001752 ret = 1;
1753 }
Thomas Heijligendee884e2021-03-31 19:09:44 +02001754 if (p->unmap_flash_region == NULL) {
1755 msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p->name);
Stefan Taunera6d96482012-12-26 19:51:23 +00001756 ret = 1;
1757 }
1758 }
Stefan Tauner96658be2014-05-26 22:05:31 +00001759
1760 /* It would be favorable if we could check for the correct layout (especially termination) of various
1761 * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known.
1762 * They are all defined as externs in this compilation unit so we don't know their sizes which vary
1763 * depending on compiler flags, e.g. the target architecture, and can sometimes be 0.
1764 * For 'flashchips' we export the size explicitly to work around this and to be able to implement the
1765 * checks below. */
Stefan Tauner6697f712014-08-06 15:09:15 +00001766 if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) {
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001767 msg_gerr("Flashchips table miscompilation!\n");
1768 ret = 1;
Stefan Tauner96658be2014-05-26 22:05:31 +00001769 } else {
1770 for (i = 0; i < flashchips_size - 1; i++) {
1771 const struct flashchip *chip = &flashchips[i];
1772 if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) {
1773 ret = 1;
1774 msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n"
1775 "Please report a bug at flashrom@flashrom.org\n", i,
1776 chip->name == NULL ? "unnamed" : chip->name);
1777 }
1778 if (selfcheck_eraseblocks(chip)) {
1779 ret = 1;
1780 }
1781 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001782 }
Stefan Tauner7bcacb12011-05-26 01:35:19 +00001783
Stefan Tauner600576b2014-06-12 22:57:36 +00001784#if CONFIG_INTERNAL == 1
1785 ret |= selfcheck_board_enables();
1786#endif
1787
Stefan Tauner96658be2014-05-26 22:05:31 +00001788 /* TODO: implement similar sanity checks for other arrays where deemed necessary. */
Carl-Daniel Hailfinger293adf02010-01-18 08:14:43 +00001789 return ret;
Carl-Daniel Hailfinger552420b2009-12-24 02:15:55 +00001790}
1791
Edward O'Callaghanaaf1c602021-04-15 13:44:39 +10001792/* FIXME: This function signature needs to be improved once prepare_flash_access()
1793 * has a better function signature.
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001794 */
Jacob Garberbeeb8bc2019-06-21 15:24:17 -06001795static int chip_safety_check(const struct flashctx *flash, int force,
1796 int read_it, int write_it, int erase_it, int verify_it)
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001797{
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001798 const struct flashchip *chip = flash->chip;
1799
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001800 if (!programmer_may_write && (write_it || erase_it)) {
1801 msg_perr("Write/erase is not working yet on your programmer in "
1802 "its current configuration.\n");
1803 /* --force is the wrong approach, but it's the best we can do
1804 * until the generic programmer parameter parser is merged.
1805 */
1806 if (!force)
1807 return 1;
1808 msg_cerr("Continuing anyway.\n");
1809 }
1810
1811 if (read_it || erase_it || write_it || verify_it) {
1812 /* Everything needs read. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001813 if (chip->tested.read == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001814 msg_cerr("Read is not working on this chip. ");
1815 if (!force)
1816 return 1;
1817 msg_cerr("Continuing anyway.\n");
1818 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001819 if (!chip->read) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001820 msg_cerr("flashrom has no read function for this "
1821 "flash chip.\n");
1822 return 1;
1823 }
1824 }
1825 if (erase_it || write_it) {
1826 /* Write needs erase. */
Stefan Tauner6455dff2014-05-26 00:36:24 +00001827 if (chip->tested.erase == NA) {
1828 msg_cerr("Erase is not possible on this chip.\n");
1829 return 1;
1830 }
1831 if (chip->tested.erase == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001832 msg_cerr("Erase is not working on this chip. ");
1833 if (!force)
1834 return 1;
1835 msg_cerr("Continuing anyway.\n");
1836 }
Sylvain "ythier" Hitier9db45512011-07-04 07:27:17 +00001837 if(count_usable_erasers(flash) == 0) {
Stefan Tauner5368dca2011-07-01 00:19:12 +00001838 msg_cerr("flashrom has no erase function for this "
1839 "flash chip.\n");
1840 return 1;
1841 }
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001842 }
1843 if (write_it) {
Stefan Tauner6455dff2014-05-26 00:36:24 +00001844 if (chip->tested.write == NA) {
1845 msg_cerr("Write is not possible on this chip.\n");
1846 return 1;
1847 }
1848 if (chip->tested.write == BAD) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001849 msg_cerr("Write is not working on this chip. ");
1850 if (!force)
1851 return 1;
1852 msg_cerr("Continuing anyway.\n");
1853 }
Carl-Daniel Hailfinger5a7cb842012-08-25 01:17:58 +00001854 if (!chip->write) {
Carl-Daniel Hailfinger43069442010-10-15 00:01:14 +00001855 msg_cerr("flashrom has no write function for this "
1856 "flash chip.\n");
1857 return 1;
1858 }
1859 }
1860 return 0;
1861}
1862
Nico Huber305f4172013-06-14 11:55:26 +02001863int prepare_flash_access(struct flashctx *const flash,
1864 const bool read_it, const bool write_it,
1865 const bool erase_it, const bool verify_it)
Nico Huber454f6132012-12-10 13:34:10 +00001866{
1867 if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) {
1868 msg_cerr("Aborting.\n");
1869 return 1;
1870 }
1871
1872 if (flash->layout == get_global_layout() && normalize_romentries(flash)) {
1873 msg_cerr("Requested regions can not be handled. Aborting.\n");
1874 return 1;
1875 }
1876
1877 if (map_flash(flash) != 0)
1878 return 1;
1879
1880 /* Given the existence of read locks, we want to unlock for read,
1881 erase and write. */
1882 if (flash->chip->unlock)
1883 flash->chip->unlock(flash);
1884
Nico Huberf43c6542017-10-14 17:47:28 +02001885 flash->address_high_byte = -1;
1886 flash->in_4ba_mode = false;
Nikolai Artemievf745d0e2020-11-05 13:54:27 +11001887 flash->chip_restore_fn_count = 0;
Nico Huberf43c6542017-10-14 17:47:28 +02001888
Nico Huberdc5af542018-12-22 16:54:59 +01001889 /* Be careful about 4BA chips and broken masters */
1890 if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) {
1891 /* If we can't use native instructions, bail out */
1892 if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE
1893 || !spi_master_4ba(flash)) {
1894 msg_cerr("Programmer doesn't support this chip. Aborting.\n");
1895 return 1;
1896 }
1897 }
1898
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001899 /* Enable/disable 4-byte addressing mode if flash chip supports it */
Nico Huber86bddb52018-03-13 18:14:52 +01001900 if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) {
Nico Huberfe34d2a2017-11-10 21:10:20 +01001901 int ret;
1902 if (spi_master_4ba(flash))
1903 ret = spi_enter_4ba(flash);
1904 else
1905 ret = spi_exit_4ba(flash);
1906 if (ret) {
1907 msg_cerr("Failed to set correct 4BA mode! Aborting.\n");
Ed Swierkcc20a9b2017-07-03 13:17:18 -07001908 return 1;
Boris Baykov7fe85692016-06-11 18:29:03 +02001909 }
Boris Baykov99127182016-06-11 18:29:00 +02001910 }
1911
Nico Huber454f6132012-12-10 13:34:10 +00001912 return 0;
1913}
1914
Nico Huber305f4172013-06-14 11:55:26 +02001915void finalize_flash_access(struct flashctx *const flash)
Nico Huber454f6132012-12-10 13:34:10 +00001916{
Nikolai Artemievf745d0e2020-11-05 13:54:27 +11001917 deregister_chip_restore(flash);
Nico Huber454f6132012-12-10 13:34:10 +00001918 unmap_flash(flash);
1919}
1920