Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 1 | /* |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 2 | * This file is part of the flashrom project. |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 3 | * |
Uwe Hermann | d22a1d4 | 2007-09-09 20:21:05 +0000 | [diff] [blame] | 4 | * Copyright (C) 2000 Silicon Integrated System Corporation |
| 5 | * Copyright (C) 2004 Tyan Corp <yhlu@tyan.com> |
Uwe Hermann | c7e8a0c | 2009-05-19 14:14:21 +0000 | [diff] [blame] | 6 | * Copyright (C) 2005-2008 coresystems GmbH |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 7 | * Copyright (C) 2008,2009 Carl-Daniel Hailfinger |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 8 | * Copyright (C) 2016 secunet Security Networks AG |
| 9 | * (Written by Nico Huber <nico.huber@secunet.com> for secunet) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 10 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 11 | * 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. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 15 | * |
Uwe Hermann | d110764 | 2007-08-29 17:52:32 +0000 | [diff] [blame] | 16 | * 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. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 22 | #include <stdio.h> |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 24 | #ifndef __LIBPAYLOAD__ |
| 25 | #include <fcntl.h> |
Stefan Reinauer | 018aca8 | 2006-11-21 23:48:51 +0000 | [diff] [blame] | 26 | #include <sys/stat.h> |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 27 | #endif |
Ronald G. Minnich | ceec420 | 2003-07-25 04:37:41 +0000 | [diff] [blame] | 28 | #include <string.h> |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 29 | #include <unistd.h> |
Ronald G. Minnich | eaab50b | 2003-09-12 22:41:53 +0000 | [diff] [blame] | 30 | #include <stdlib.h> |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 31 | #include <errno.h> |
Carl-Daniel Hailfinger | c244138 | 2010-11-09 22:00:31 +0000 | [diff] [blame] | 32 | #include <ctype.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 33 | #include <getopt.h> |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 34 | #if HAVE_UTSNAME == 1 |
| 35 | #include <sys/utsname.h> |
| 36 | #endif |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 37 | #include "flash.h" |
Carl-Daniel Hailfinger | 0845464 | 2009-06-15 14:14:48 +0000 | [diff] [blame] | 38 | #include "flashchips.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 39 | #include "programmer.h" |
Carl-Daniel Hailfinger | 06b9efa | 2012-08-07 11:59:59 +0000 | [diff] [blame] | 40 | #include "hwaccess.h" |
Nico Huber | fe34d2a | 2017-11-10 21:10:20 +0100 | [diff] [blame] | 41 | #include "chipdrivers.h" |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 42 | |
Mathias Krause | a60faab | 2011-01-17 07:50:42 +0000 | [diff] [blame] | 43 | const char flashrom_version[] = FLASHROM_VERSION; |
Nico Huber | bcb2e5a | 2012-12-30 01:23:17 +0000 | [diff] [blame] | 44 | const char *chip_to_probe = NULL; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 45 | |
Thomas Heijligen | c7e5b8b | 2021-06-01 14:21:41 +0200 | [diff] [blame] | 46 | static const struct programmer_entry *programmer = NULL; |
Nico Huber | bcb2e5a | 2012-12-30 01:23:17 +0000 | [diff] [blame] | 47 | static const char *programmer_param = NULL; |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 48 | |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 49 | /* |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 50 | * 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 Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 53 | struct decode_sizes max_rom_decode; |
| 54 | |
| 55 | /* If nonzero, used as the start address of bottom-aligned flash. */ |
| 56 | unsigned long flashbase; |
Carl-Daniel Hailfinger | 66ef4e5 | 2009-12-13 22:28:00 +0000 | [diff] [blame] | 57 | |
Carl-Daniel Hailfinger | d1be52d | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 58 | /* Is writing allowed with this programmer? */ |
| 59 | int programmer_may_write; |
| 60 | |
Carl-Daniel Hailfinger | 2bee8cf | 2010-11-10 15:25:18 +0000 | [diff] [blame] | 61 | #define SHUTDOWN_MAXFN 32 |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 62 | static int shutdown_fn_count = 0; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 63 | /** @private */ |
Richard Hughes | 93e1625 | 2018-12-19 11:54:47 +0000 | [diff] [blame] | 64 | static struct shutdown_func_data { |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 65 | int (*func) (void *data); |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 66 | void *data; |
Richard Hughes | 93e1625 | 2018-12-19 11:54:47 +0000 | [diff] [blame] | 67 | } shutdown_fn[SHUTDOWN_MAXFN]; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 68 | /* Initialize to 0 to make sure nobody registers a shutdown function before |
| 69 | * programmer init. |
| 70 | */ |
| 71 | static int may_register_shutdown = 0; |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 72 | |
Stefan Tauner | c4f44df | 2013-08-12 22:58:43 +0000 | [diff] [blame] | 73 | /* Did we change something or was every erase/write skipped (if any)? */ |
| 74 | static bool all_skipped = true; |
| 75 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 76 | static int check_block_eraser(const struct flashctx *flash, int k, int log); |
Stefan Tauner | 5368dca | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 77 | |
Stefan Tauner | 2a1ed77 | 2014-08-31 00:09:21 +0000 | [diff] [blame] | 78 | int shutdown_free(void *data) |
| 79 | { |
| 80 | free(data); |
| 81 | return 0; |
| 82 | } |
| 83 | |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 84 | /* Register a function to be executed on programmer shutdown. |
| 85 | * The advantage over atexit() is that you can supply a void pointer which will |
| 86 | * be used as parameter to the registered function upon programmer shutdown. |
| 87 | * This pointer can point to arbitrary data used by said function, e.g. undo |
| 88 | * information for GPIO settings etc. If unneeded, set data=NULL. |
| 89 | * Please note that the first (void *data) belongs to the function signature of |
| 90 | * the function passed as first parameter. |
| 91 | */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 92 | int register_shutdown(int (*function) (void *data), void *data) |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 93 | { |
| 94 | if (shutdown_fn_count >= SHUTDOWN_MAXFN) { |
Carl-Daniel Hailfinger | 9f5f215 | 2010-06-04 23:20:21 +0000 | [diff] [blame] | 95 | msg_perr("Tried to register more than %i shutdown functions.\n", |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 96 | SHUTDOWN_MAXFN); |
| 97 | return 1; |
| 98 | } |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 99 | if (!may_register_shutdown) { |
| 100 | msg_perr("Tried to register a shutdown function before " |
| 101 | "programmer init.\n"); |
| 102 | return 1; |
| 103 | } |
Carl-Daniel Hailfinger | cc389fc | 2010-02-14 01:20:28 +0000 | [diff] [blame] | 104 | shutdown_fn[shutdown_fn_count].func = function; |
| 105 | shutdown_fn[shutdown_fn_count].data = data; |
| 106 | shutdown_fn_count++; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Nikolai Artemiev | 4ad4864 | 2020-11-05 13:54:27 +1100 | [diff] [blame] | 111 | int register_chip_restore(chip_restore_fn_cb_t func, |
| 112 | struct flashctx *flash, uint8_t status) |
| 113 | { |
| 114 | if (flash->chip_restore_fn_count >= MAX_CHIP_RESTORE_FUNCTIONS) { |
| 115 | msg_perr("Tried to register more than %i chip restore" |
| 116 | " functions.\n", MAX_CHIP_RESTORE_FUNCTIONS); |
| 117 | return 1; |
| 118 | } |
| 119 | flash->chip_restore_fn[flash->chip_restore_fn_count].func = func; |
| 120 | flash->chip_restore_fn[flash->chip_restore_fn_count].status = status; |
| 121 | flash->chip_restore_fn_count++; |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int deregister_chip_restore(struct flashctx *flash) |
| 127 | { |
| 128 | int rc = 0; |
| 129 | |
| 130 | while (flash->chip_restore_fn_count > 0) { |
| 131 | int i = --flash->chip_restore_fn_count; |
| 132 | rc |= flash->chip_restore_fn[i].func( |
| 133 | flash, flash->chip_restore_fn[i].status); |
| 134 | } |
| 135 | |
| 136 | return rc; |
| 137 | } |
| 138 | |
Thomas Heijligen | e0e93cf | 2021-06-01 14:37:12 +0200 | [diff] [blame] | 139 | int programmer_init(const struct programmer_entry *prog, const char *param) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 140 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 141 | int ret; |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 142 | |
Thomas Heijligen | e0e93cf | 2021-06-01 14:37:12 +0200 | [diff] [blame] | 143 | if (prog == NULL) { |
Carl-Daniel Hailfinger | 2e68160 | 2011-09-08 00:00:29 +0000 | [diff] [blame] | 144 | msg_perr("Invalid programmer specified!\n"); |
| 145 | return -1; |
| 146 | } |
Thomas Heijligen | e0e93cf | 2021-06-01 14:37:12 +0200 | [diff] [blame] | 147 | programmer = prog; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 148 | /* Initialize all programmer specific data. */ |
| 149 | /* Default to unlimited decode sizes. */ |
| 150 | max_rom_decode = (const struct decode_sizes) { |
| 151 | .parallel = 0xffffffff, |
| 152 | .lpc = 0xffffffff, |
| 153 | .fwh = 0xffffffff, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 154 | .spi = 0xffffffff, |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 155 | }; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 156 | /* Default to top aligned flash at 4 GB. */ |
| 157 | flashbase = 0; |
| 158 | /* Registering shutdown functions is now allowed. */ |
| 159 | may_register_shutdown = 1; |
Carl-Daniel Hailfinger | d1be52d | 2010-07-03 12:14:25 +0000 | [diff] [blame] | 160 | /* Default to allowing writes. Broken programmers set this to 0. */ |
| 161 | programmer_may_write = 1; |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 162 | |
| 163 | programmer_param = param; |
Thomas Heijligen | c7e5b8b | 2021-06-01 14:21:41 +0200 | [diff] [blame] | 164 | msg_pdbg("Initializing %s programmer\n", programmer->name); |
| 165 | ret = programmer->init(); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 166 | if (programmer_param && strlen(programmer_param)) { |
Carl-Daniel Hailfinger | 20a36ba | 2013-08-13 07:09:57 +0000 | [diff] [blame] | 167 | if (ret != 0) { |
| 168 | /* It is quite possible that any unhandled programmer parameter would have been valid, |
| 169 | * but an error in actual programmer init happened before the parameter was evaluated. |
| 170 | */ |
| 171 | msg_pwarn("Unhandled programmer parameters (possibly due to another failure): %s\n", |
| 172 | programmer_param); |
| 173 | } else { |
| 174 | /* Actual programmer init was successful, but the user specified an invalid or unusable |
| 175 | * (for the current programmer configuration) parameter. |
| 176 | */ |
| 177 | msg_perr("Unhandled programmer parameters: %s\n", programmer_param); |
| 178 | msg_perr("Aborting.\n"); |
| 179 | ret = ERROR_FATAL; |
| 180 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 181 | } |
| 182 | return ret; |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Stefan Tauner | 20da4aa | 2014-05-07 22:07:23 +0000 | [diff] [blame] | 185 | /** Calls registered shutdown functions and resets internal programmer-related variables. |
| 186 | * Calling it is safe even without previous initialization, but further interactions with programmer support |
| 187 | * require a call to programmer_init() (afterwards). |
| 188 | * |
| 189 | * @return The OR-ed result values of all shutdown functions (i.e. 0 on success). */ |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 190 | int programmer_shutdown(void) |
| 191 | { |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 192 | int ret = 0; |
| 193 | |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 194 | /* Registering shutdown functions is no longer allowed. */ |
| 195 | may_register_shutdown = 0; |
| 196 | while (shutdown_fn_count > 0) { |
| 197 | int i = --shutdown_fn_count; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 198 | ret |= shutdown_fn[i].func(shutdown_fn[i].data); |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 199 | } |
Stefan Tauner | e34e3e8 | 2013-01-01 00:06:51 +0000 | [diff] [blame] | 200 | |
Stefan Tauner | b8911d6 | 2012-12-26 07:55:00 +0000 | [diff] [blame] | 201 | programmer_param = NULL; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 202 | registered_master_count = 0; |
Stefan Tauner | e34e3e8 | 2013-01-01 00:06:51 +0000 | [diff] [blame] | 203 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 204 | return ret; |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Stefan Tauner | 305e0b9 | 2013-07-17 23:46:44 +0000 | [diff] [blame] | 207 | void *programmer_map_flash_region(const char *descr, uintptr_t phys_addr, size_t len) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 208 | { |
Thomas Heijligen | c7e5b8b | 2021-06-01 14:21:41 +0200 | [diff] [blame] | 209 | void *ret = programmer->map_flash_region(descr, phys_addr, len); |
Stefan Tauner | 26e7a15 | 2013-09-13 17:21:05 +0000 | [diff] [blame] | 210 | msg_gspew("%s: mapping %s from 0x%0*" PRIxPTR " to 0x%0*" PRIxPTR "\n", |
| 211 | __func__, descr, PRIxPTR_WIDTH, phys_addr, PRIxPTR_WIDTH, (uintptr_t) ret); |
| 212 | return ret; |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void programmer_unmap_flash_region(void *virt_addr, size_t len) |
| 216 | { |
Thomas Heijligen | c7e5b8b | 2021-06-01 14:21:41 +0200 | [diff] [blame] | 217 | programmer->unmap_flash_region(virt_addr, len); |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 218 | msg_gspew("%s: unmapped 0x%0*" PRIxPTR "\n", __func__, PRIxPTR_WIDTH, (uintptr_t)virt_addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 221 | void chip_writeb(const struct flashctx *flash, uint8_t val, chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 222 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 223 | flash->mst->par.chip_writeb(flash, val, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 226 | void chip_writew(const struct flashctx *flash, uint16_t val, chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 227 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 228 | flash->mst->par.chip_writew(flash, val, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 231 | void chip_writel(const struct flashctx *flash, uint32_t val, chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 232 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 233 | flash->mst->par.chip_writel(flash, val, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 236 | void chip_writen(const struct flashctx *flash, const uint8_t *buf, chipaddr addr, size_t len) |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 237 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 238 | flash->mst->par.chip_writen(flash, buf, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 241 | uint8_t chip_readb(const struct flashctx *flash, const chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 242 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 243 | return flash->mst->par.chip_readb(flash, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 246 | uint16_t chip_readw(const struct flashctx *flash, const chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 247 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 248 | return flash->mst->par.chip_readw(flash, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 251 | uint32_t chip_readl(const struct flashctx *flash, const chipaddr addr) |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 252 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 253 | return flash->mst->par.chip_readl(flash, addr); |
Uwe Hermann | 09e04f7 | 2009-05-16 22:36:00 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 256 | void chip_readn(const struct flashctx *flash, uint8_t *buf, chipaddr addr, |
| 257 | size_t len) |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 258 | { |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 259 | flash->mst->par.chip_readn(flash, buf, addr, len); |
Carl-Daniel Hailfinger | 0bd2a2b | 2009-06-05 18:32:07 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Stefan Tauner | f80419c | 2014-05-02 15:41:42 +0000 | [diff] [blame] | 262 | void programmer_delay(unsigned int usecs) |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 263 | { |
Urja Rannikko | 8d7ec2a | 2013-10-21 21:49:08 +0000 | [diff] [blame] | 264 | if (usecs > 0) |
Thomas Heijligen | c7e5b8b | 2021-06-01 14:21:41 +0200 | [diff] [blame] | 265 | programmer->delay(usecs); |
Carl-Daniel Hailfinger | ca8bfc6 | 2009-06-05 17:48:08 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 268 | int read_memmapped(struct flashctx *flash, uint8_t *buf, unsigned int start, |
| 269 | int unsigned len) |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 270 | { |
Carl-Daniel Hailfinger | 8a3c60c | 2011-12-18 15:01:24 +0000 | [diff] [blame] | 271 | chip_readn(flash, buf, flash->virtual_memory + start, len); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 272 | |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 273 | return 0; |
| 274 | } |
| 275 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 276 | /* This is a somewhat hacked function similar in some ways to strtok(). |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 277 | * It will look for needle with a subsequent '=' in haystack, return a copy of |
| 278 | * needle and remove everything from the first occurrence of needle to the next |
| 279 | * delimiter from haystack. |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 280 | */ |
Nico Huber | bcb2e5a | 2012-12-30 01:23:17 +0000 | [diff] [blame] | 281 | char *extract_param(const char *const *haystack, const char *needle, const char *delim) |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 282 | { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 283 | char *param_pos, *opt_pos, *rest; |
| 284 | char *opt = NULL; |
| 285 | int optlen; |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 286 | int needlelen; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 287 | |
Carl-Daniel Hailfinger | 2702376 | 2010-04-28 15:22:14 +0000 | [diff] [blame] | 288 | needlelen = strlen(needle); |
| 289 | if (!needlelen) { |
| 290 | msg_gerr("%s: empty needle! Please report a bug at " |
| 291 | "flashrom@flashrom.org\n", __func__); |
| 292 | return NULL; |
| 293 | } |
| 294 | /* No programmer parameters given. */ |
| 295 | if (*haystack == NULL) |
| 296 | return NULL; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 297 | param_pos = strstr(*haystack, needle); |
| 298 | do { |
| 299 | if (!param_pos) |
| 300 | return NULL; |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 301 | /* Needle followed by '='? */ |
| 302 | if (param_pos[needlelen] == '=') { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 303 | /* Beginning of the string? */ |
| 304 | if (param_pos == *haystack) |
| 305 | break; |
| 306 | /* After a delimiter? */ |
| 307 | if (strchr(delim, *(param_pos - 1))) |
| 308 | break; |
| 309 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 310 | /* Continue searching. */ |
| 311 | param_pos++; |
| 312 | param_pos = strstr(param_pos, needle); |
| 313 | } while (1); |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 314 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 315 | if (param_pos) { |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 316 | /* Get the string after needle and '='. */ |
| 317 | opt_pos = param_pos + needlelen + 1; |
| 318 | optlen = strcspn(opt_pos, delim); |
| 319 | /* Return an empty string if the parameter was empty. */ |
| 320 | opt = malloc(optlen + 1); |
| 321 | if (!opt) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 322 | msg_gerr("Out of memory!\n"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 323 | exit(1); |
| 324 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 325 | strncpy(opt, opt_pos, optlen); |
| 326 | opt[optlen] = '\0'; |
| 327 | rest = opt_pos + optlen; |
| 328 | /* Skip all delimiters after the current parameter. */ |
| 329 | rest += strspn(rest, delim); |
| 330 | memmove(param_pos, rest, strlen(rest) + 1); |
| 331 | /* We could shrink haystack, but the effort is not worth it. */ |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 332 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 333 | |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 334 | return opt; |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 335 | } |
| 336 | |
Stefan Tauner | 6665244 | 2011-06-26 17:38:17 +0000 | [diff] [blame] | 337 | char *extract_programmer_param(const char *param_name) |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 338 | { |
| 339 | return extract_param(&programmer_param, param_name, ","); |
| 340 | } |
| 341 | |
Sylvain "ythier" Hitier | 9db4551 | 2011-07-04 07:27:17 +0000 | [diff] [blame] | 342 | /* Returns the number of well-defined erasers for a chip. */ |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 343 | static unsigned int count_usable_erasers(const struct flashctx *flash) |
Stefan Tauner | 5368dca | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 344 | { |
| 345 | unsigned int usable_erasefunctions = 0; |
| 346 | int k; |
| 347 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 348 | if (!check_block_eraser(flash, k, 0)) |
| 349 | usable_erasefunctions++; |
| 350 | } |
| 351 | return usable_erasefunctions; |
| 352 | } |
| 353 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 354 | static int compare_range(const uint8_t *wantbuf, const uint8_t *havebuf, unsigned int start, unsigned int len) |
Stefan Tauner | 78ffbea | 2012-10-27 15:36:56 +0000 | [diff] [blame] | 355 | { |
| 356 | int ret = 0, failcount = 0; |
| 357 | unsigned int i; |
| 358 | for (i = 0; i < len; i++) { |
| 359 | if (wantbuf[i] != havebuf[i]) { |
| 360 | /* Only print the first failure. */ |
| 361 | if (!failcount++) |
| 362 | msg_cerr("FAILED at 0x%08x! Expected=0x%02x, Found=0x%02x,", |
| 363 | start + i, wantbuf[i], havebuf[i]); |
| 364 | } |
| 365 | } |
| 366 | if (failcount) { |
| 367 | msg_cerr(" failed byte count from 0x%08x-0x%08x: 0x%x\n", |
| 368 | start, start + len - 1, failcount); |
| 369 | ret = -1; |
| 370 | } |
| 371 | return ret; |
| 372 | } |
| 373 | |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 374 | /* start is an offset to the base address of the flash chip */ |
Jacob Garber | beeb8bc | 2019-06-21 15:24:17 -0600 | [diff] [blame] | 375 | static int check_erased_range(struct flashctx *flash, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 376 | { |
| 377 | int ret; |
| 378 | uint8_t *cmpbuf = malloc(len); |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 379 | const uint8_t erased_value = ERASED_VALUE(flash); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 380 | |
| 381 | if (!cmpbuf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 382 | msg_gerr("Could not allocate memory!\n"); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 383 | exit(1); |
| 384 | } |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 385 | memset(cmpbuf, erased_value, len); |
Stefan Tauner | 78ffbea | 2012-10-27 15:36:56 +0000 | [diff] [blame] | 386 | ret = verify_range(flash, cmpbuf, start, len); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 387 | free(cmpbuf); |
| 388 | return ret; |
| 389 | } |
| 390 | |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 391 | /* |
Carl-Daniel Hailfinger | d0250a3 | 2009-11-25 17:05:52 +0000 | [diff] [blame] | 392 | * @cmpbuf buffer to compare against, cmpbuf[0] is expected to match the |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 393 | * flash content at location start |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 394 | * @start offset to the base address of the flash chip |
| 395 | * @len length of the verified area |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 396 | * @return 0 for success, -1 for failure |
| 397 | */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 398 | int verify_range(struct flashctx *flash, const uint8_t *cmpbuf, unsigned int start, unsigned int len) |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 399 | { |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 400 | if (!len) |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 401 | return -1; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 402 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 403 | if (!flash->chip->read) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 404 | msg_cerr("ERROR: flashrom has no read function for this flash chip.\n"); |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 405 | return -1; |
Carl-Daniel Hailfinger | 2329066 | 2009-06-24 08:20:45 +0000 | [diff] [blame] | 406 | } |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 407 | |
| 408 | uint8_t *readbuf = malloc(len); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 409 | if (!readbuf) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 410 | msg_gerr("Could not allocate memory!\n"); |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 411 | return -1; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 412 | } |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 413 | int ret = 0; |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 414 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 415 | if (start + len > flash->chip->total_size * 1024) { |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 416 | msg_gerr("Error: %s called with start 0x%x + len 0x%x >" |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 417 | " total_size 0x%x\n", __func__, start, len, |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 418 | flash->chip->total_size * 1024); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 419 | ret = -1; |
| 420 | goto out_free; |
| 421 | } |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 422 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 423 | ret = flash->chip->read(flash, readbuf, start, len); |
Carl-Daniel Hailfinger | d836941 | 2010-11-16 17:21:58 +0000 | [diff] [blame] | 424 | if (ret) { |
| 425 | msg_gerr("Verification impossible because read failed " |
| 426 | "at 0x%x (len 0x%x)\n", start, len); |
Stefan Tauner | df64a42 | 2014-05-27 00:06:14 +0000 | [diff] [blame] | 427 | ret = -1; |
| 428 | goto out_free; |
Carl-Daniel Hailfinger | d836941 | 2010-11-16 17:21:58 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Stefan Tauner | 78ffbea | 2012-10-27 15:36:56 +0000 | [diff] [blame] | 431 | ret = compare_range(cmpbuf, readbuf, start, len); |
Carl-Daniel Hailfinger | 30f7cb2 | 2009-06-15 17:23:36 +0000 | [diff] [blame] | 432 | out_free: |
| 433 | free(readbuf); |
| 434 | return ret; |
| 435 | } |
| 436 | |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 437 | /* Helper function for need_erase() that focuses on granularities of gran bytes. */ |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 438 | static int need_erase_gran_bytes(const uint8_t *have, const uint8_t *want, unsigned int len, |
| 439 | unsigned int gran, const uint8_t erased_value) |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 440 | { |
| 441 | unsigned int i, j, limit; |
| 442 | for (j = 0; j < len / gran; j++) { |
| 443 | limit = min (gran, len - j * gran); |
| 444 | /* Are 'have' and 'want' identical? */ |
| 445 | if (!memcmp(have + j * gran, want + j * gran, limit)) |
| 446 | continue; |
| 447 | /* have needs to be in erased state. */ |
| 448 | for (i = 0; i < limit; i++) |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 449 | if (have[j * gran + i] != erased_value) |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 450 | return 1; |
| 451 | } |
| 452 | return 0; |
| 453 | } |
| 454 | |
Uwe Hermann | 48ec1b1 | 2010-08-08 17:01:18 +0000 | [diff] [blame] | 455 | /* |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 456 | * Check if the buffer @have can be programmed to the content of @want without |
| 457 | * erasing. This is only possible if all chunks of size @gran are either kept |
| 458 | * as-is or changed from an all-ones state to any other state. |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 459 | * |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 460 | * Warning: This function assumes that @have and @want point to naturally |
| 461 | * aligned regions. |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 462 | * |
| 463 | * @have buffer with current content |
| 464 | * @want buffer with desired content |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 465 | * @len length of the checked area |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 466 | * @gran write granularity (enum, not count) |
| 467 | * @return 0 if no erase is needed, 1 otherwise |
| 468 | */ |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 469 | int need_erase(const uint8_t *have, const uint8_t *want, unsigned int len, |
| 470 | enum write_granularity gran, const uint8_t erased_value) |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 471 | { |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 472 | int result = 0; |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 473 | unsigned int i; |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 474 | |
| 475 | switch (gran) { |
| 476 | case write_gran_1bit: |
| 477 | for (i = 0; i < len; i++) |
| 478 | if ((have[i] & want[i]) != want[i]) { |
| 479 | result = 1; |
| 480 | break; |
| 481 | } |
| 482 | break; |
| 483 | case write_gran_1byte: |
| 484 | for (i = 0; i < len; i++) |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 485 | if ((have[i] != want[i]) && (have[i] != erased_value)) { |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 486 | result = 1; |
| 487 | break; |
| 488 | } |
| 489 | break; |
Paul Kocialkowski | c8305e1 | 2015-10-16 02:16:20 +0000 | [diff] [blame] | 490 | case write_gran_128bytes: |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 491 | result = need_erase_gran_bytes(have, want, len, 128, erased_value); |
Paul Kocialkowski | c8305e1 | 2015-10-16 02:16:20 +0000 | [diff] [blame] | 492 | break; |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 493 | case write_gran_256bytes: |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 494 | result = need_erase_gran_bytes(have, want, len, 256, erased_value); |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 495 | break; |
| 496 | case write_gran_264bytes: |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 497 | result = need_erase_gran_bytes(have, want, len, 264, erased_value); |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 498 | break; |
| 499 | case write_gran_512bytes: |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 500 | result = need_erase_gran_bytes(have, want, len, 512, erased_value); |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 501 | break; |
| 502 | case write_gran_528bytes: |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 503 | result = need_erase_gran_bytes(have, want, len, 528, erased_value); |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 504 | break; |
| 505 | case write_gran_1024bytes: |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 506 | result = need_erase_gran_bytes(have, want, len, 1024, erased_value); |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 507 | break; |
| 508 | case write_gran_1056bytes: |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 509 | result = need_erase_gran_bytes(have, want, len, 1056, erased_value); |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 510 | break; |
Carl-Daniel Hailfinger | 1b0e9fc | 2014-06-16 22:36:17 +0000 | [diff] [blame] | 511 | case write_gran_1byte_implicit_erase: |
| 512 | /* Do not erase, handle content changes from anything->0xff by writing 0xff. */ |
| 513 | result = 0; |
| 514 | break; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 515 | default: |
| 516 | msg_cerr("%s: Unsupported granularity! Please report a bug at " |
| 517 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | e8e369f | 2010-03-08 00:42:32 +0000 | [diff] [blame] | 518 | } |
| 519 | return result; |
| 520 | } |
| 521 | |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 522 | /** |
| 523 | * Check if the buffer @have needs to be programmed to get the content of @want. |
| 524 | * If yes, return 1 and fill in first_start with the start address of the |
| 525 | * write operation and first_len with the length of the first to-be-written |
| 526 | * chunk. If not, return 0 and leave first_start and first_len undefined. |
| 527 | * |
| 528 | * Warning: This function assumes that @have and @want point to naturally |
| 529 | * aligned regions. |
| 530 | * |
| 531 | * @have buffer with current content |
| 532 | * @want buffer with desired content |
| 533 | * @len length of the checked area |
| 534 | * @gran write granularity (enum, not count) |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 535 | * @first_start offset of the first byte which needs to be written (passed in |
| 536 | * value is increased by the offset of the first needed write |
| 537 | * relative to have/want or unchanged if no write is needed) |
| 538 | * @return length of the first contiguous area which needs to be written |
| 539 | * 0 if no write is needed |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 540 | * |
| 541 | * FIXME: This function needs a parameter which tells it about coalescing |
| 542 | * in relation to the max write length of the programmer and the max write |
| 543 | * length of the chip. |
| 544 | */ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 545 | static unsigned int get_next_write(const uint8_t *have, const uint8_t *want, unsigned int len, |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 546 | unsigned int *first_start, |
| 547 | enum write_granularity gran) |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 548 | { |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 549 | int need_write = 0; |
| 550 | unsigned int rel_start = 0, first_len = 0; |
| 551 | unsigned int i, limit, stride; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 552 | |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 553 | switch (gran) { |
| 554 | case write_gran_1bit: |
| 555 | case write_gran_1byte: |
Carl-Daniel Hailfinger | 1b0e9fc | 2014-06-16 22:36:17 +0000 | [diff] [blame] | 556 | case write_gran_1byte_implicit_erase: |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 557 | stride = 1; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 558 | break; |
Paul Kocialkowski | c8305e1 | 2015-10-16 02:16:20 +0000 | [diff] [blame] | 559 | case write_gran_128bytes: |
| 560 | stride = 128; |
| 561 | break; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 562 | case write_gran_256bytes: |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 563 | stride = 256; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 564 | break; |
Stefan Tauner | 0243745 | 2013-04-01 19:34:53 +0000 | [diff] [blame] | 565 | case write_gran_264bytes: |
| 566 | stride = 264; |
| 567 | break; |
| 568 | case write_gran_512bytes: |
| 569 | stride = 512; |
| 570 | break; |
| 571 | case write_gran_528bytes: |
| 572 | stride = 528; |
| 573 | break; |
| 574 | case write_gran_1024bytes: |
| 575 | stride = 1024; |
| 576 | break; |
| 577 | case write_gran_1056bytes: |
| 578 | stride = 1056; |
| 579 | break; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 580 | default: |
| 581 | msg_cerr("%s: Unsupported granularity! Please report a bug at " |
| 582 | "flashrom@flashrom.org\n", __func__); |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 583 | /* Claim that no write was needed. A write with unknown |
| 584 | * granularity is too dangerous to try. |
| 585 | */ |
| 586 | return 0; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 587 | } |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 588 | for (i = 0; i < len / stride; i++) { |
| 589 | limit = min(stride, len - i * stride); |
| 590 | /* Are 'have' and 'want' identical? */ |
| 591 | if (memcmp(have + i * stride, want + i * stride, limit)) { |
| 592 | if (!need_write) { |
| 593 | /* First location where have and want differ. */ |
| 594 | need_write = 1; |
| 595 | rel_start = i * stride; |
| 596 | } |
| 597 | } else { |
| 598 | if (need_write) { |
| 599 | /* First location where have and want |
| 600 | * do not differ anymore. |
| 601 | */ |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 602 | break; |
| 603 | } |
| 604 | } |
| 605 | } |
Carl-Daniel Hailfinger | 202bf53 | 2010-12-06 13:05:44 +0000 | [diff] [blame] | 606 | if (need_write) |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 607 | first_len = min(i * stride - rel_start, len); |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 608 | *first_start += rel_start; |
Carl-Daniel Hailfinger | 12d6d82 | 2010-11-05 14:51:59 +0000 | [diff] [blame] | 609 | return first_len; |
Carl-Daniel Hailfinger | 6e2ea32 | 2010-11-04 01:04:27 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Stefan Tauner | 9e3a698 | 2014-08-15 17:17:59 +0000 | [diff] [blame] | 612 | /* Returns the number of busses commonly supported by the current programmer and flash chip where the latter |
| 613 | * can not be completely accessed due to size/address limits of the programmer. */ |
| 614 | unsigned int count_max_decode_exceedings(const struct flashctx *flash) |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 615 | { |
Stefan Tauner | 9e3a698 | 2014-08-15 17:17:59 +0000 | [diff] [blame] | 616 | unsigned int limitexceeded = 0; |
| 617 | uint32_t size = flash->chip->total_size * 1024; |
| 618 | enum chipbustype buses = flash->mst->buses_supported & flash->chip->bustype; |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 619 | |
| 620 | if ((buses & BUS_PARALLEL) && (max_rom_decode.parallel < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 621 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 622 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 623 | "size %u kB of chipset/board/programmer " |
| 624 | "for %s interface, " |
| 625 | "probe/read/erase/write may fail. ", size / 1024, |
| 626 | max_rom_decode.parallel / 1024, "Parallel"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 627 | } |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 628 | if ((buses & BUS_LPC) && (max_rom_decode.lpc < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 629 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 630 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 631 | "size %u kB of chipset/board/programmer " |
| 632 | "for %s interface, " |
| 633 | "probe/read/erase/write may fail. ", size / 1024, |
| 634 | max_rom_decode.lpc / 1024, "LPC"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 635 | } |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 636 | if ((buses & BUS_FWH) && (max_rom_decode.fwh < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 637 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 638 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 639 | "size %u kB of chipset/board/programmer " |
| 640 | "for %s interface, " |
| 641 | "probe/read/erase/write may fail. ", size / 1024, |
| 642 | max_rom_decode.fwh / 1024, "FWH"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 643 | } |
Carl-Daniel Hailfinger | 1a22795 | 2011-07-27 07:13:06 +0000 | [diff] [blame] | 644 | if ((buses & BUS_SPI) && (max_rom_decode.spi < size)) { |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 645 | limitexceeded++; |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 646 | msg_pdbg("Chip size %u kB is bigger than supported " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 647 | "size %u kB of chipset/board/programmer " |
| 648 | "for %s interface, " |
| 649 | "probe/read/erase/write may fail. ", size / 1024, |
| 650 | max_rom_decode.spi / 1024, "SPI"); |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 651 | } |
Stefan Tauner | 9e3a698 | 2014-08-15 17:17:59 +0000 | [diff] [blame] | 652 | return limitexceeded; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 653 | } |
| 654 | |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 655 | void unmap_flash(struct flashctx *flash) |
| 656 | { |
| 657 | if (flash->virtual_registers != (chipaddr)ERROR_PTR) { |
| 658 | programmer_unmap_flash_region((void *)flash->virtual_registers, flash->chip->total_size * 1024); |
| 659 | flash->physical_registers = 0; |
| 660 | flash->virtual_registers = (chipaddr)ERROR_PTR; |
| 661 | } |
| 662 | |
| 663 | if (flash->virtual_memory != (chipaddr)ERROR_PTR) { |
| 664 | programmer_unmap_flash_region((void *)flash->virtual_memory, flash->chip->total_size * 1024); |
| 665 | flash->physical_memory = 0; |
| 666 | flash->virtual_memory = (chipaddr)ERROR_PTR; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | int map_flash(struct flashctx *flash) |
| 671 | { |
| 672 | /* Init pointers to the fail-safe state to distinguish them later from legit values. */ |
| 673 | flash->virtual_memory = (chipaddr)ERROR_PTR; |
| 674 | flash->virtual_registers = (chipaddr)ERROR_PTR; |
| 675 | |
| 676 | /* FIXME: This avoids mapping (and unmapping) of flash chip definitions with size 0. |
| 677 | * These are used for various probing-related hacks that would not map successfully anyway and should be |
| 678 | * removed ASAP. */ |
| 679 | if (flash->chip->total_size == 0) |
| 680 | return 0; |
| 681 | |
| 682 | const chipsize_t size = flash->chip->total_size * 1024; |
| 683 | uintptr_t base = flashbase ? flashbase : (0xffffffff - size + 1); |
| 684 | void *addr = programmer_map_flash_region(flash->chip->name, base, size); |
| 685 | if (addr == ERROR_PTR) { |
| 686 | msg_perr("Could not map flash chip %s at 0x%0*" PRIxPTR ".\n", |
| 687 | flash->chip->name, PRIxPTR_WIDTH, base); |
| 688 | return 1; |
| 689 | } |
| 690 | flash->physical_memory = base; |
| 691 | flash->virtual_memory = (chipaddr)addr; |
| 692 | |
| 693 | /* FIXME: Special function registers normally live 4 MByte below flash space, but it might be somewhere |
| 694 | * completely different on some chips and programmers, or not mappable at all. |
| 695 | * Ignore these problems for now and always report success. */ |
| 696 | if (flash->chip->feature_bits & FEATURE_REGISTERMAP) { |
| 697 | base = 0xffffffff - size - 0x400000 + 1; |
| 698 | addr = programmer_map_flash_region("flash chip registers", base, size); |
| 699 | if (addr == ERROR_PTR) { |
| 700 | msg_pdbg2("Could not map flash chip registers %s at 0x%0*" PRIxPTR ".\n", |
| 701 | flash->chip->name, PRIxPTR_WIDTH, base); |
| 702 | return 0; |
| 703 | } |
| 704 | flash->physical_registers = base; |
| 705 | flash->virtual_registers = (chipaddr)addr; |
| 706 | } |
| 707 | return 0; |
| 708 | } |
| 709 | |
Nico Huber | 2d62572 | 2016-05-03 10:48:02 +0200 | [diff] [blame] | 710 | /* |
| 711 | * Return a string corresponding to the bustype parameter. |
| 712 | * Memory is obtained with malloc() and must be freed with free() by the caller. |
| 713 | */ |
| 714 | char *flashbuses_to_text(enum chipbustype bustype) |
| 715 | { |
| 716 | char *ret = calloc(1, 1); |
| 717 | /* |
| 718 | * FIXME: Once all chipsets and flash chips have been updated, NONSPI |
| 719 | * will cease to exist and should be eliminated here as well. |
| 720 | */ |
| 721 | if (bustype == BUS_NONSPI) { |
| 722 | ret = strcat_realloc(ret, "Non-SPI, "); |
| 723 | } else { |
| 724 | if (bustype & BUS_PARALLEL) |
| 725 | ret = strcat_realloc(ret, "Parallel, "); |
| 726 | if (bustype & BUS_LPC) |
| 727 | ret = strcat_realloc(ret, "LPC, "); |
| 728 | if (bustype & BUS_FWH) |
| 729 | ret = strcat_realloc(ret, "FWH, "); |
| 730 | if (bustype & BUS_SPI) |
| 731 | ret = strcat_realloc(ret, "SPI, "); |
| 732 | if (bustype & BUS_PROG) |
| 733 | ret = strcat_realloc(ret, "Programmer-specific, "); |
| 734 | if (bustype == BUS_NONE) |
| 735 | ret = strcat_realloc(ret, "None, "); |
| 736 | } |
| 737 | /* Kill last comma. */ |
| 738 | ret[strlen(ret) - 2] = '\0'; |
| 739 | ret = realloc(ret, strlen(ret) + 1); |
| 740 | return ret; |
| 741 | } |
| 742 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 743 | int probe_flash(struct registered_master *mst, int startchip, struct flashctx *flash, int force) |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 744 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 745 | const struct flashchip *chip; |
Carl-Daniel Hailfinger | 115d390 | 2009-10-31 01:53:09 +0000 | [diff] [blame] | 746 | enum chipbustype buses_common; |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 747 | char *tmp; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 748 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 749 | for (chip = flashchips + startchip; chip && chip->name; chip++) { |
| 750 | if (chip_to_probe && strcmp(chip->name, chip_to_probe) != 0) |
Ollie Lho | cbbf125 | 2004-03-17 22:22:08 +0000 | [diff] [blame] | 751 | continue; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 752 | buses_common = mst->buses_supported & chip->bustype; |
Carl-Daniel Hailfinger | c40cff7 | 2011-12-20 00:19:29 +0000 | [diff] [blame] | 753 | if (!buses_common) |
Carl-Daniel Hailfinger | 6573b74 | 2011-06-17 22:38:53 +0000 | [diff] [blame] | 754 | continue; |
Mike Banon | 31b5e3b | 2018-01-15 01:10:00 +0300 | [diff] [blame] | 755 | /* Only probe for SPI25 chips by default. */ |
| 756 | if (chip->bustype == BUS_SPI && !chip_to_probe && chip->spi_cmd_set != SPI25) |
| 757 | continue; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 758 | msg_gdbg("Probing for %s %s, %d kB: ", chip->vendor, chip->name, chip->total_size); |
| 759 | if (!chip->probe && !force) { |
| 760 | msg_gdbg("failed! flashrom has no probe function for this flash chip.\n"); |
Carl-Daniel Hailfinger | b22918c | 2009-06-01 02:08:58 +0000 | [diff] [blame] | 761 | continue; |
| 762 | } |
Stefan Reinauer | 7038564 | 2007-04-06 11:58:03 +0000 | [diff] [blame] | 763 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 764 | /* Start filling in the dynamic data. */ |
Angel Pons | 690a944 | 2021-06-07 12:33:53 +0200 | [diff] [blame] | 765 | flash->chip = calloc(1, sizeof(*flash->chip)); |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 766 | if (!flash->chip) { |
| 767 | msg_gerr("Out of memory!\n"); |
| 768 | exit(1); |
| 769 | } |
Angel Pons | 7e13456 | 2021-06-07 13:29:13 +0200 | [diff] [blame] | 770 | *flash->chip = *chip; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 771 | flash->mst = mst; |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 772 | |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 773 | if (map_flash(flash) != 0) |
Martin Schiller | 57a3b73 | 2017-11-23 06:24:57 +0100 | [diff] [blame] | 774 | goto notfound; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 775 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 776 | /* We handle a forced match like a real match, we just avoid probing. Note that probe_flash() |
| 777 | * is only called with force=1 after normal probing failed. |
| 778 | */ |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 779 | if (force) |
| 780 | break; |
Stefan Reinauer | fcb6368 | 2006-03-16 16:57:41 +0000 | [diff] [blame] | 781 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 782 | if (flash->chip->probe(flash) != 1) |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 783 | goto notfound; |
| 784 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 785 | /* If this is the first chip found, accept it. |
| 786 | * If this is not the first chip found, accept it only if it is |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 787 | * a non-generic match. SFDP and CFI are generic matches. |
| 788 | * startchip==0 means this call to probe_flash() is the first |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 789 | * one for this programmer interface (master) and thus no other chip has |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 790 | * been found on this interface. |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 791 | */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 792 | if (startchip == 0 && flash->chip->model_id == SFDP_DEVICE_ID) { |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 793 | msg_cinfo("===\n" |
| 794 | "SFDP has autodetected a flash chip which is " |
| 795 | "not natively supported by flashrom yet.\n"); |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 796 | if (count_usable_erasers(flash) == 0) |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 797 | msg_cinfo("The standard operations read and " |
| 798 | "verify should work, but to support " |
| 799 | "erase, write and all other " |
| 800 | "possible features"); |
| 801 | else |
| 802 | msg_cinfo("All standard operations (read, " |
| 803 | "verify, erase and write) should " |
| 804 | "work, but to support all possible " |
| 805 | "features"); |
| 806 | |
Stefan Tauner | b4e06bd | 2012-08-20 00:24:22 +0000 | [diff] [blame] | 807 | msg_cinfo(" we need to add them manually.\n" |
| 808 | "You can help us by mailing us the output of the following command to " |
| 809 | "flashrom@flashrom.org:\n" |
| 810 | "'flashrom -VV [plus the -p/--programmer parameter]'\n" |
| 811 | "Thanks for your help!\n" |
Stefan Tauner | ac1b4c8 | 2012-02-17 14:51:04 +0000 | [diff] [blame] | 812 | "===\n"); |
| 813 | } |
| 814 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 815 | /* First flash chip detected on this bus. */ |
| 816 | if (startchip == 0) |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 817 | break; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 818 | /* Not the first flash chip detected on this bus, but not a generic match either. */ |
| 819 | if ((flash->chip->model_id != GENERIC_DEVICE_ID) && (flash->chip->model_id != SFDP_DEVICE_ID)) |
| 820 | break; |
| 821 | /* Not the first flash chip detected on this bus, and it's just a generic match. Ignore it. */ |
Peter Stuge | 483b8f0 | 2008-09-03 23:10:05 +0000 | [diff] [blame] | 822 | notfound: |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 823 | unmap_flash(flash); |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 824 | free(flash->chip); |
| 825 | flash->chip = NULL; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 826 | } |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 827 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 828 | if (!flash->chip) |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 829 | return -1; |
Peter Stuge | 27c3e2d | 2008-07-02 17:15:47 +0000 | [diff] [blame] | 830 | |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 831 | /* Fill fallback layout covering the whole chip. */ |
| 832 | struct single_layout *const fallback = &flash->fallback_layout; |
| 833 | fallback->base.entries = &fallback->entry; |
| 834 | fallback->base.num_entries = 1; |
| 835 | fallback->entry.start = 0; |
| 836 | fallback->entry.end = flash->chip->total_size * 1024 - 1; |
| 837 | fallback->entry.included = true; |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 838 | fallback->entry.name = strdup("complete flash"); |
| 839 | if (!fallback->entry.name) { |
| 840 | msg_cerr("Failed to probe chip: %s\n", strerror(errno)); |
| 841 | return -1; |
| 842 | } |
Stefan Reinauer | 051e236 | 2011-01-19 06:21:54 +0000 | [diff] [blame] | 843 | |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 844 | tmp = flashbuses_to_text(flash->chip->bustype); |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 845 | msg_cinfo("%s %s flash chip \"%s\" (%d kB, %s) ", force ? "Assuming" : "Found", |
| 846 | flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp); |
Stefan Tauner | 0015549 | 2011-06-26 20:45:35 +0000 | [diff] [blame] | 847 | free(tmp); |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 848 | #if CONFIG_INTERNAL == 1 |
Thomas Heijligen | c7e5b8b | 2021-06-01 14:21:41 +0200 | [diff] [blame] | 849 | if (programmer->map_flash_region == physmap) |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 850 | msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n", |
| 851 | PRIxPTR_WIDTH, flash->physical_memory); |
| 852 | else |
| 853 | #endif |
Thomas Heijligen | c7e5b8b | 2021-06-01 14:21:41 +0200 | [diff] [blame] | 854 | msg_cinfo("on %s.\n", programmer->name); |
Uwe Hermann | 9899cad | 2009-06-28 21:47:57 +0000 | [diff] [blame] | 855 | |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 856 | /* Flash registers may more likely not be mapped if the chip was forced. |
| 857 | * Lock info may be stored in registers, so avoid lock info printing. */ |
Carl-Daniel Hailfinger | 859f3f0 | 2010-12-02 21:59:42 +0000 | [diff] [blame] | 858 | if (!force) |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 859 | if (flash->chip->printlock) |
| 860 | flash->chip->printlock(flash); |
Sean Nelson | 6e0b912 | 2010-02-19 00:52:10 +0000 | [diff] [blame] | 861 | |
Stefan Tauner | 4e32ec1 | 2014-08-30 23:39:51 +0000 | [diff] [blame] | 862 | /* Get out of the way for later runs. */ |
| 863 | unmap_flash(flash); |
| 864 | |
Carl-Daniel Hailfinger | 4c82318 | 2011-05-04 00:39:50 +0000 | [diff] [blame] | 865 | /* Return position of matching chip. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 866 | return chip - flashchips; |
Ronald G. Minnich | f4cf2ba | 2002-01-29 18:26:26 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 869 | int read_buf_from_file(unsigned char *buf, unsigned long size, |
| 870 | const char *filename) |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 871 | { |
Nico Huber | 7562f7d | 2013-08-30 21:29:45 +0000 | [diff] [blame] | 872 | #ifdef __LIBPAYLOAD__ |
| 873 | msg_gerr("Error: No file I/O support in libpayload\n"); |
| 874 | return 1; |
| 875 | #else |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 876 | int ret = 0; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 877 | |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 878 | FILE *image; |
Daniel Campello | 8eaef7d | 2021-04-15 10:36:04 -0600 | [diff] [blame] | 879 | if (!strcmp(filename, "-")) |
| 880 | image = fdopen(fileno(stdin), "rb"); |
| 881 | else |
| 882 | image = fopen(filename, "rb"); |
| 883 | if (image == NULL) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 884 | msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno)); |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 885 | return 1; |
| 886 | } |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 887 | |
| 888 | struct stat image_stat; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 889 | if (fstat(fileno(image), &image_stat) != 0) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 890 | msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno)); |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 891 | ret = 1; |
| 892 | goto out; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 893 | } |
Daniel Campello | 8eaef7d | 2021-04-15 10:36:04 -0600 | [diff] [blame] | 894 | if ((image_stat.st_size != (intmax_t)size) && strcmp(filename, "-")) { |
Carl-Daniel Hailfinger | 11990da | 2013-07-13 23:21:05 +0000 | [diff] [blame] | 895 | msg_gerr("Error: Image size (%jd B) doesn't match the flash chip's size (%lu B)!\n", |
Stefan Tauner | e038e90 | 2013-02-04 04:38:42 +0000 | [diff] [blame] | 896 | (intmax_t)image_stat.st_size, size); |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 897 | ret = 1; |
| 898 | goto out; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 899 | } |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 900 | |
| 901 | unsigned long numbytes = fread(buf, 1, size, image); |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 902 | if (numbytes != size) { |
| 903 | msg_gerr("Error: Failed to read complete file. Got %ld bytes, " |
| 904 | "wanted %ld!\n", numbytes, size); |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 905 | ret = 1; |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 906 | } |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 907 | out: |
| 908 | (void)fclose(image); |
| 909 | return ret; |
Nico Huber | 7562f7d | 2013-08-30 21:29:45 +0000 | [diff] [blame] | 910 | #endif |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 913 | int write_buf_to_file(const unsigned char *buf, unsigned long size, const char *filename) |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 914 | { |
Nico Huber | 7562f7d | 2013-08-30 21:29:45 +0000 | [diff] [blame] | 915 | #ifdef __LIBPAYLOAD__ |
| 916 | msg_gerr("Error: No file I/O support in libpayload\n"); |
| 917 | return 1; |
| 918 | #else |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 919 | FILE *image; |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 920 | int ret = 0; |
Stephan Guilloux | 21dd55b | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 921 | |
| 922 | if (!filename) { |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 923 | msg_gerr("No filename specified.\n"); |
Stephan Guilloux | 21dd55b | 2009-06-01 22:07:52 +0000 | [diff] [blame] | 924 | return 1; |
| 925 | } |
Patrick Georgi | 0bf842d | 2010-01-25 22:55:33 +0000 | [diff] [blame] | 926 | if ((image = fopen(filename, "wb")) == NULL) { |
Stefan Tauner | 363fd7e | 2013-04-07 13:08:30 +0000 | [diff] [blame] | 927 | msg_gerr("Error: opening file \"%s\" failed: %s\n", filename, strerror(errno)); |
Carl-Daniel Hailfinger | 03b4e71 | 2009-05-08 12:49:03 +0000 | [diff] [blame] | 928 | return 1; |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 929 | } |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 930 | |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 931 | unsigned long numbytes = fwrite(buf, 1, size, image); |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 932 | if (numbytes != size) { |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 933 | msg_gerr("Error: file %s could not be written completely.\n", filename); |
| 934 | ret = 1; |
| 935 | goto out; |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 936 | } |
Stefan Tauner | 1668770 | 2015-12-25 21:59:45 +0000 | [diff] [blame] | 937 | if (fflush(image)) { |
| 938 | msg_gerr("Error: flushing file \"%s\" failed: %s\n", filename, strerror(errno)); |
| 939 | ret = 1; |
| 940 | } |
| 941 | // Try to fsync() only regular files and if that function is available at all (e.g. not on MinGW). |
| 942 | #if defined(_POSIX_FSYNC) && (_POSIX_FSYNC != -1) |
| 943 | struct stat image_stat; |
| 944 | if (fstat(fileno(image), &image_stat) != 0) { |
| 945 | msg_gerr("Error: getting metadata of file \"%s\" failed: %s\n", filename, strerror(errno)); |
| 946 | ret = 1; |
| 947 | goto out; |
| 948 | } |
| 949 | if (S_ISREG(image_stat.st_mode)) { |
| 950 | if (fsync(fileno(image))) { |
| 951 | msg_gerr("Error: fsyncing file \"%s\" failed: %s\n", filename, strerror(errno)); |
| 952 | ret = 1; |
| 953 | } |
| 954 | } |
| 955 | #endif |
| 956 | out: |
| 957 | if (fclose(image)) { |
| 958 | msg_gerr("Error: closing file \"%s\" failed: %s\n", filename, strerror(errno)); |
| 959 | ret = 1; |
| 960 | } |
| 961 | return ret; |
Nico Huber | 7562f7d | 2013-08-30 21:29:45 +0000 | [diff] [blame] | 962 | #endif |
Carl-Daniel Hailfinger | 7314cc3 | 2009-01-28 00:27:54 +0000 | [diff] [blame] | 963 | } |
| 964 | |
Nico Huber | 899e4ec | 2016-04-29 18:39:01 +0200 | [diff] [blame] | 965 | static int read_by_layout(struct flashctx *, uint8_t *); |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 966 | int read_flash_to_file(struct flashctx *flash, const char *filename) |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 967 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 968 | unsigned long size = flash->chip->total_size * 1024; |
Richard Hughes | 84b453e | 2018-12-19 15:30:39 +0000 | [diff] [blame] | 969 | unsigned char *buf = calloc(size, sizeof(unsigned char)); |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 970 | int ret = 0; |
| 971 | |
| 972 | msg_cinfo("Reading flash... "); |
| 973 | if (!buf) { |
| 974 | msg_gerr("Memory allocation failed!\n"); |
| 975 | msg_cinfo("FAILED.\n"); |
| 976 | return 1; |
| 977 | } |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 978 | if (!flash->chip->read) { |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 979 | msg_cerr("No read function available for this flash chip.\n"); |
| 980 | ret = 1; |
| 981 | goto out_free; |
| 982 | } |
Nico Huber | 899e4ec | 2016-04-29 18:39:01 +0200 | [diff] [blame] | 983 | if (read_by_layout(flash, buf)) { |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 984 | msg_cerr("Read operation failed!\n"); |
| 985 | ret = 1; |
| 986 | goto out_free; |
| 987 | } |
| 988 | |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 989 | ret = write_buf_to_file(buf, size, filename); |
Carl-Daniel Hailfinger | 1748c57 | 2010-07-13 23:56:13 +0000 | [diff] [blame] | 990 | out_free: |
| 991 | free(buf); |
| 992 | msg_cinfo("%s.\n", ret ? "FAILED" : "done"); |
| 993 | return ret; |
| 994 | } |
| 995 | |
Stefan Tauner | 96658be | 2014-05-26 22:05:31 +0000 | [diff] [blame] | 996 | /* Even if an error is found, the function will keep going and check the rest. */ |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 997 | static int selfcheck_eraseblocks(const struct flashchip *chip) |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 998 | { |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 999 | int i, j, k; |
| 1000 | int ret = 0; |
Aarya Chaumal | 478e179 | 2022-06-04 01:34:44 +0530 | [diff] [blame] | 1001 | unsigned int prev_eraseblock_count = chip->total_size * 1024; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1002 | |
| 1003 | for (k = 0; k < NUM_ERASEFUNCTIONS; k++) { |
| 1004 | unsigned int done = 0; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1005 | struct block_eraser eraser = chip->block_erasers[k]; |
Aarya Chaumal | 478e179 | 2022-06-04 01:34:44 +0530 | [diff] [blame] | 1006 | unsigned int curr_eraseblock_count = 0; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1007 | |
| 1008 | for (i = 0; i < NUM_ERASEREGIONS; i++) { |
| 1009 | /* Blocks with zero size are bugs in flashchips.c. */ |
| 1010 | if (eraser.eraseblocks[i].count && |
| 1011 | !eraser.eraseblocks[i].size) { |
| 1012 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1013 | "%i region %i has size 0. Please report" |
| 1014 | " a bug at flashrom@flashrom.org\n", |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1015 | chip->name, k, i); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1016 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1017 | } |
| 1018 | /* Blocks with zero count are bugs in flashchips.c. */ |
| 1019 | if (!eraser.eraseblocks[i].count && |
| 1020 | eraser.eraseblocks[i].size) { |
| 1021 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1022 | "%i region %i has count 0. Please report" |
| 1023 | " a bug at flashrom@flashrom.org\n", |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1024 | chip->name, k, i); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1025 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1026 | } |
| 1027 | done += eraser.eraseblocks[i].count * |
| 1028 | eraser.eraseblocks[i].size; |
Aarya Chaumal | 478e179 | 2022-06-04 01:34:44 +0530 | [diff] [blame] | 1029 | curr_eraseblock_count += eraser.eraseblocks[i].count; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1030 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1031 | /* Empty eraseblock definition with erase function. */ |
| 1032 | if (!done && eraser.block_erase) |
Sean Nelson | 316a29f | 2010-05-07 20:09:04 +0000 | [diff] [blame] | 1033 | msg_gspew("Strange: Empty eraseblock definition with " |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 1034 | "non-empty erase function. Not an error.\n"); |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1035 | if (!done) |
| 1036 | continue; |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1037 | if (done != chip->total_size * 1024) { |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1038 | msg_gerr("ERROR: Flash chip %s erase function %i " |
| 1039 | "region walking resulted in 0x%06x bytes total," |
| 1040 | " expected 0x%06x bytes. Please report a bug at" |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1041 | " flashrom@flashrom.org\n", chip->name, k, |
| 1042 | done, chip->total_size * 1024); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1043 | ret = 1; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1044 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1045 | if (!eraser.block_erase) |
| 1046 | continue; |
| 1047 | /* Check if there are identical erase functions for different |
| 1048 | * layouts. That would imply "magic" erase functions. The |
| 1049 | * easiest way to check this is with function pointers. |
| 1050 | */ |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 1051 | for (j = k + 1; j < NUM_ERASEFUNCTIONS; j++) { |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1052 | if (eraser.block_erase == |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1053 | chip->block_erasers[j].block_erase) { |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1054 | msg_gerr("ERROR: Flash chip %s erase function " |
| 1055 | "%i and %i are identical. Please report" |
| 1056 | " a bug at flashrom@flashrom.org\n", |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1057 | chip->name, k, j); |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1058 | ret = 1; |
| 1059 | } |
Uwe Hermann | 4395970 | 2010-03-13 17:28:29 +0000 | [diff] [blame] | 1060 | } |
Aarya Chaumal | 478e179 | 2022-06-04 01:34:44 +0530 | [diff] [blame] | 1061 | if(curr_eraseblock_count > prev_eraseblock_count) |
| 1062 | { |
| 1063 | msg_gerr("ERROR: Flash chip %s erase function %i is not " |
| 1064 | "in order. Please report a bug at flashrom@flashrom.org\n", |
| 1065 | chip->name, k); |
| 1066 | ret = 1; |
| 1067 | } |
| 1068 | prev_eraseblock_count = curr_eraseblock_count; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1069 | } |
Carl-Daniel Hailfinger | 415afcf | 2010-01-19 06:42:46 +0000 | [diff] [blame] | 1070 | return ret; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
Carl-Daniel Hailfinger | 63fd902 | 2011-12-14 22:25:15 +0000 | [diff] [blame] | 1073 | static int check_block_eraser(const struct flashctx *flash, int k, int log) |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1074 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1075 | struct block_eraser eraser = flash->chip->block_erasers[k]; |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1076 | |
| 1077 | if (!eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 1078 | if (log) |
| 1079 | msg_cdbg("not defined. "); |
| 1080 | return 1; |
| 1081 | } |
| 1082 | if (!eraser.block_erase && eraser.eraseblocks[0].count) { |
| 1083 | if (log) |
| 1084 | msg_cdbg("eraseblock layout is known, but matching " |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1085 | "block erase function is not implemented. "); |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1086 | return 1; |
| 1087 | } |
| 1088 | if (eraser.block_erase && !eraser.eraseblocks[0].count) { |
| 1089 | if (log) |
| 1090 | msg_cdbg("block erase function found, but " |
Stefan Tauner | 355cbfd | 2011-05-28 02:37:14 +0000 | [diff] [blame] | 1091 | "eraseblock layout is not defined. "); |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1092 | return 1; |
| 1093 | } |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1094 | // TODO: Once erase functions are annotated with allowed buses, check that as well. |
Carl-Daniel Hailfinger | dce73ae | 2010-12-05 15:14:44 +0000 | [diff] [blame] | 1095 | return 0; |
| 1096 | } |
| 1097 | |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1098 | /** |
| 1099 | * @brief Reads the included layout regions into a buffer. |
| 1100 | * |
| 1101 | * If there is no layout set in the given flash context, the whole chip will |
| 1102 | * be read. |
| 1103 | * |
| 1104 | * @param flashctx Flash context to be used. |
| 1105 | * @param buffer Buffer of full chip size to read into. |
| 1106 | * @return 0 on success, |
| 1107 | * 1 if any read fails. |
| 1108 | */ |
| 1109 | static int read_by_layout(struct flashctx *const flashctx, uint8_t *const buffer) |
| 1110 | { |
| 1111 | const struct flashrom_layout *const layout = get_layout(flashctx); |
Nico Huber | 5ca5523 | 2019-06-15 22:29:08 +0200 | [diff] [blame] | 1112 | const struct romentry *entry = NULL; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1113 | |
Nico Huber | 5ca5523 | 2019-06-15 22:29:08 +0200 | [diff] [blame] | 1114 | while ((entry = layout_next_included(layout, entry))) { |
| 1115 | const chipoff_t region_start = entry->start; |
| 1116 | const chipsize_t region_len = entry->end - entry->start + 1; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1117 | |
| 1118 | if (flashctx->chip->read(flashctx, buffer + region_start, region_start, region_len)) |
| 1119 | return 1; |
| 1120 | } |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
| 1124 | typedef int (*erasefn_t)(struct flashctx *, unsigned int addr, unsigned int len); |
| 1125 | /** |
| 1126 | * @private |
| 1127 | * |
| 1128 | * For read-erase-write, `curcontents` and `newcontents` shall point |
| 1129 | * to buffers of the chip's size. Both are supposed to be prefilled |
| 1130 | * with at least the included layout regions of the current flash |
| 1131 | * contents (`curcontents`) and the data to be written to the flash |
| 1132 | * (`newcontents`). |
| 1133 | * |
| 1134 | * For erase, `curcontents` and `newcontents` shall be NULL-pointers. |
| 1135 | * |
| 1136 | * The `chipoff_t` values are used internally by `walk_by_layout()`. |
| 1137 | */ |
| 1138 | struct walk_info { |
| 1139 | uint8_t *curcontents; |
| 1140 | const uint8_t *newcontents; |
| 1141 | chipoff_t region_start; |
| 1142 | chipoff_t region_end; |
| 1143 | chipoff_t erase_start; |
| 1144 | chipoff_t erase_end; |
| 1145 | }; |
| 1146 | /* returns 0 on success, 1 to retry with another erase function, 2 for immediate abort */ |
| 1147 | typedef int (*per_blockfn_t)(struct flashctx *, const struct walk_info *, erasefn_t); |
| 1148 | |
| 1149 | static int walk_eraseblocks(struct flashctx *const flashctx, |
| 1150 | struct walk_info *const info, |
| 1151 | const size_t erasefunction, const per_blockfn_t per_blockfn) |
| 1152 | { |
| 1153 | int ret; |
| 1154 | size_t i, j; |
| 1155 | bool first = true; |
| 1156 | struct block_eraser *const eraser = &flashctx->chip->block_erasers[erasefunction]; |
| 1157 | |
| 1158 | info->erase_start = 0; |
| 1159 | for (i = 0; i < NUM_ERASEREGIONS; ++i) { |
| 1160 | /* count==0 for all automatically initialized array |
| 1161 | members so the loop below won't be executed for them. */ |
| 1162 | for (j = 0; j < eraser->eraseblocks[i].count; ++j, info->erase_start = info->erase_end + 1) { |
| 1163 | info->erase_end = info->erase_start + eraser->eraseblocks[i].size - 1; |
| 1164 | |
| 1165 | /* Skip any eraseblock that is completely outside the current region. */ |
| 1166 | if (info->erase_end < info->region_start) |
| 1167 | continue; |
| 1168 | if (info->region_end < info->erase_start) |
| 1169 | break; |
| 1170 | |
| 1171 | /* Print this for every block except the first one. */ |
| 1172 | if (first) |
| 1173 | first = false; |
| 1174 | else |
| 1175 | msg_cdbg(", "); |
| 1176 | msg_cdbg("0x%06x-0x%06x:", info->erase_start, info->erase_end); |
| 1177 | |
| 1178 | ret = per_blockfn(flashctx, info, eraser->block_erase); |
| 1179 | if (ret) |
| 1180 | return ret; |
| 1181 | } |
| 1182 | if (info->region_end < info->erase_start) |
| 1183 | break; |
| 1184 | } |
| 1185 | msg_cdbg("\n"); |
| 1186 | return 0; |
| 1187 | } |
| 1188 | |
| 1189 | static int walk_by_layout(struct flashctx *const flashctx, struct walk_info *const info, |
| 1190 | const per_blockfn_t per_blockfn) |
| 1191 | { |
| 1192 | const struct flashrom_layout *const layout = get_layout(flashctx); |
Nico Huber | 5ca5523 | 2019-06-15 22:29:08 +0200 | [diff] [blame] | 1193 | const struct romentry *entry = NULL; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1194 | |
| 1195 | all_skipped = true; |
| 1196 | msg_cinfo("Erasing and writing flash chip... "); |
| 1197 | |
Nico Huber | 5ca5523 | 2019-06-15 22:29:08 +0200 | [diff] [blame] | 1198 | while ((entry = layout_next_included(layout, entry))) { |
| 1199 | info->region_start = entry->start; |
| 1200 | info->region_end = entry->end; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1201 | |
| 1202 | size_t j; |
| 1203 | int error = 1; /* retry as long as it's 1 */ |
| 1204 | for (j = 0; j < NUM_ERASEFUNCTIONS; ++j) { |
| 1205 | if (j != 0) |
| 1206 | msg_cinfo("Looking for another erase function.\n"); |
| 1207 | msg_cdbg("Trying erase function %zi... ", j); |
| 1208 | if (check_block_eraser(flashctx, j, 1)) |
| 1209 | continue; |
| 1210 | |
| 1211 | error = walk_eraseblocks(flashctx, info, j, per_blockfn); |
| 1212 | if (error != 1) |
| 1213 | break; |
| 1214 | |
| 1215 | if (info->curcontents) { |
| 1216 | msg_cinfo("Reading current flash chip contents... "); |
| 1217 | if (read_by_layout(flashctx, info->curcontents)) { |
| 1218 | /* Now we are truly screwed. Read failed as well. */ |
| 1219 | msg_cerr("Can't read anymore! Aborting.\n"); |
| 1220 | /* We have no idea about the flash chip contents, so |
| 1221 | retrying with another erase function is pointless. */ |
| 1222 | error = 2; |
| 1223 | break; |
| 1224 | } |
| 1225 | msg_cinfo("done. "); |
| 1226 | } |
| 1227 | } |
| 1228 | if (error == 1) |
| 1229 | msg_cinfo("No usable erase functions left.\n"); |
| 1230 | if (error) { |
| 1231 | msg_cerr("FAILED!\n"); |
| 1232 | return 1; |
| 1233 | } |
| 1234 | } |
| 1235 | if (all_skipped) |
| 1236 | msg_cinfo("\nWarning: Chip content is identical to the requested image.\n"); |
| 1237 | msg_cinfo("Erase/write done.\n"); |
| 1238 | return 0; |
| 1239 | } |
| 1240 | |
| 1241 | static int erase_block(struct flashctx *const flashctx, |
| 1242 | const struct walk_info *const info, const erasefn_t erasefn) |
| 1243 | { |
| 1244 | const unsigned int erase_len = info->erase_end + 1 - info->erase_start; |
Nico Huber | 6e61e0c | 2019-01-23 17:07:49 +0100 | [diff] [blame] | 1245 | const bool region_unaligned = info->region_start > info->erase_start || |
| 1246 | info->erase_end > info->region_end; |
| 1247 | uint8_t *backup_contents = NULL, *erased_contents = NULL; |
| 1248 | int ret = 2; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1249 | |
Nico Huber | 6e61e0c | 2019-01-23 17:07:49 +0100 | [diff] [blame] | 1250 | /* |
| 1251 | * If the region is not erase-block aligned, merge current flash con- |
| 1252 | * tents into a new buffer `backup_contents`. |
| 1253 | */ |
| 1254 | if (region_unaligned) { |
| 1255 | backup_contents = malloc(erase_len); |
| 1256 | erased_contents = malloc(erase_len); |
| 1257 | if (!backup_contents || !erased_contents) { |
| 1258 | msg_cerr("Out of memory!\n"); |
| 1259 | ret = 1; |
| 1260 | goto _free_ret; |
| 1261 | } |
| 1262 | memset(backup_contents, ERASED_VALUE(flashctx), erase_len); |
| 1263 | memset(erased_contents, ERASED_VALUE(flashctx), erase_len); |
| 1264 | |
| 1265 | msg_cdbg("R"); |
| 1266 | /* Merge data preceding the current region. */ |
| 1267 | if (info->region_start > info->erase_start) { |
| 1268 | const chipoff_t start = info->erase_start; |
| 1269 | const chipsize_t len = info->region_start - info->erase_start; |
| 1270 | if (flashctx->chip->read(flashctx, backup_contents, start, len)) { |
| 1271 | msg_cerr("Can't read! Aborting.\n"); |
| 1272 | goto _free_ret; |
| 1273 | } |
| 1274 | } |
| 1275 | /* Merge data following the current region. */ |
| 1276 | if (info->erase_end > info->region_end) { |
| 1277 | const chipoff_t start = info->region_end + 1; |
| 1278 | const chipoff_t rel_start = start - info->erase_start; /* within this erase block */ |
| 1279 | const chipsize_t len = info->erase_end - info->region_end; |
| 1280 | if (flashctx->chip->read(flashctx, backup_contents + rel_start, start, len)) { |
| 1281 | msg_cerr("Can't read! Aborting.\n"); |
| 1282 | goto _free_ret; |
| 1283 | } |
| 1284 | } |
| 1285 | } |
| 1286 | |
| 1287 | ret = 1; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1288 | all_skipped = false; |
| 1289 | |
| 1290 | msg_cdbg("E"); |
| 1291 | if (erasefn(flashctx, info->erase_start, erase_len)) |
Nico Huber | 6e61e0c | 2019-01-23 17:07:49 +0100 | [diff] [blame] | 1292 | goto _free_ret; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1293 | if (check_erased_range(flashctx, info->erase_start, erase_len)) { |
| 1294 | msg_cerr("ERASE FAILED!\n"); |
Nico Huber | 6e61e0c | 2019-01-23 17:07:49 +0100 | [diff] [blame] | 1295 | goto _free_ret; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1296 | } |
Nico Huber | 6e61e0c | 2019-01-23 17:07:49 +0100 | [diff] [blame] | 1297 | |
| 1298 | if (region_unaligned) { |
| 1299 | unsigned int starthere = 0, lenhere = 0, writecount = 0; |
| 1300 | /* get_next_write() sets starthere to a new value after the call. */ |
| 1301 | while ((lenhere = get_next_write(erased_contents + starthere, backup_contents + starthere, |
| 1302 | erase_len - starthere, &starthere, flashctx->chip->gran))) { |
| 1303 | if (!writecount++) |
| 1304 | msg_cdbg("W"); |
| 1305 | /* Needs the partial write function signature. */ |
| 1306 | if (flashctx->chip->write(flashctx, backup_contents + starthere, |
| 1307 | info->erase_start + starthere, lenhere)) |
| 1308 | goto _free_ret; |
| 1309 | starthere += lenhere; |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | ret = 0; |
| 1314 | |
| 1315 | _free_ret: |
| 1316 | free(erased_contents); |
| 1317 | free(backup_contents); |
| 1318 | return ret; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | /** |
| 1322 | * @brief Erases the included layout regions. |
| 1323 | * |
| 1324 | * If there is no layout set in the given flash context, the whole chip will |
| 1325 | * be erased. |
| 1326 | * |
| 1327 | * @param flashctx Flash context to be used. |
| 1328 | * @param buffer Buffer of full chip size to read into. |
| 1329 | * @return 0 on success, |
| 1330 | * 1 if all available erase functions failed. |
| 1331 | */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1332 | static int erase_by_layout(struct flashctx *const flashctx) |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1333 | { |
| 1334 | struct walk_info info = { 0 }; |
| 1335 | return walk_by_layout(flashctx, &info, &erase_block); |
| 1336 | } |
| 1337 | |
| 1338 | static int read_erase_write_block(struct flashctx *const flashctx, |
| 1339 | const struct walk_info *const info, const erasefn_t erasefn) |
| 1340 | { |
| 1341 | const chipsize_t erase_len = info->erase_end + 1 - info->erase_start; |
| 1342 | const bool region_unaligned = info->region_start > info->erase_start || |
| 1343 | info->erase_end > info->region_end; |
| 1344 | const uint8_t *newcontents = NULL; |
| 1345 | int ret = 2; |
| 1346 | |
| 1347 | /* |
| 1348 | * If the region is not erase-block aligned, merge current flash con- |
| 1349 | * tents into `info->curcontents` and a new buffer `newc`. The former |
| 1350 | * is necessary since we have no guarantee that the full erase block |
| 1351 | * was already read into `info->curcontents`. For the latter a new |
| 1352 | * buffer is used since `info->newcontents` might contain data for |
| 1353 | * other unaligned regions that touch this erase block too. |
| 1354 | */ |
| 1355 | if (region_unaligned) { |
| 1356 | msg_cdbg("R"); |
| 1357 | uint8_t *const newc = malloc(erase_len); |
| 1358 | if (!newc) { |
| 1359 | msg_cerr("Out of memory!\n"); |
| 1360 | return 1; |
| 1361 | } |
| 1362 | memcpy(newc, info->newcontents + info->erase_start, erase_len); |
| 1363 | |
| 1364 | /* Merge data preceding the current region. */ |
| 1365 | if (info->region_start > info->erase_start) { |
| 1366 | const chipoff_t start = info->erase_start; |
| 1367 | const chipsize_t len = info->region_start - info->erase_start; |
| 1368 | if (flashctx->chip->read(flashctx, newc, start, len)) { |
| 1369 | msg_cerr("Can't read! Aborting.\n"); |
| 1370 | goto _free_ret; |
| 1371 | } |
| 1372 | memcpy(info->curcontents + start, newc, len); |
| 1373 | } |
| 1374 | /* Merge data following the current region. */ |
| 1375 | if (info->erase_end > info->region_end) { |
| 1376 | const chipoff_t start = info->region_end + 1; |
| 1377 | const chipoff_t rel_start = start - info->erase_start; /* within this erase block */ |
| 1378 | const chipsize_t len = info->erase_end - info->region_end; |
| 1379 | if (flashctx->chip->read(flashctx, newc + rel_start, start, len)) { |
| 1380 | msg_cerr("Can't read! Aborting.\n"); |
| 1381 | goto _free_ret; |
| 1382 | } |
| 1383 | memcpy(info->curcontents + start, newc + rel_start, len); |
| 1384 | } |
| 1385 | |
| 1386 | newcontents = newc; |
| 1387 | } else { |
| 1388 | newcontents = info->newcontents + info->erase_start; |
| 1389 | } |
| 1390 | |
| 1391 | ret = 1; |
| 1392 | bool skipped = true; |
| 1393 | uint8_t *const curcontents = info->curcontents + info->erase_start; |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 1394 | const uint8_t erased_value = ERASED_VALUE(flashctx); |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 1395 | if (!(flashctx->chip->feature_bits & FEATURE_NO_ERASE) && |
| 1396 | need_erase(curcontents, newcontents, erase_len, flashctx->chip->gran, erased_value)) { |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1397 | if (erase_block(flashctx, info, erasefn)) |
| 1398 | goto _free_ret; |
| 1399 | /* Erase was successful. Adjust curcontents. */ |
Paul Kocialkowski | 995f755 | 2018-01-15 01:06:09 +0300 | [diff] [blame] | 1400 | memset(curcontents, erased_value, erase_len); |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1401 | skipped = false; |
| 1402 | } |
| 1403 | |
| 1404 | unsigned int starthere = 0, lenhere = 0, writecount = 0; |
| 1405 | /* get_next_write() sets starthere to a new value after the call. */ |
| 1406 | while ((lenhere = get_next_write(curcontents + starthere, newcontents + starthere, |
| 1407 | erase_len - starthere, &starthere, flashctx->chip->gran))) { |
| 1408 | if (!writecount++) |
| 1409 | msg_cdbg("W"); |
| 1410 | /* Needs the partial write function signature. */ |
| 1411 | if (flashctx->chip->write(flashctx, newcontents + starthere, |
| 1412 | info->erase_start + starthere, lenhere)) |
| 1413 | goto _free_ret; |
| 1414 | starthere += lenhere; |
| 1415 | skipped = false; |
| 1416 | } |
| 1417 | if (skipped) |
| 1418 | msg_cdbg("S"); |
| 1419 | else |
| 1420 | all_skipped = false; |
| 1421 | |
| 1422 | /* Update curcontents, other regions with overlapping erase blocks |
| 1423 | might rely on this. */ |
| 1424 | memcpy(curcontents, newcontents, erase_len); |
| 1425 | ret = 0; |
| 1426 | |
| 1427 | _free_ret: |
| 1428 | if (region_unaligned) |
| 1429 | free((void *)newcontents); |
| 1430 | return ret; |
| 1431 | } |
| 1432 | |
| 1433 | /** |
| 1434 | * @brief Writes the included layout regions from a given image. |
| 1435 | * |
| 1436 | * If there is no layout set in the given flash context, the whole image |
| 1437 | * will be written. |
| 1438 | * |
| 1439 | * @param flashctx Flash context to be used. |
| 1440 | * @param curcontents A buffer of full chip size with current chip contents of included regions. |
| 1441 | * @param newcontents The new image to be written. |
| 1442 | * @return 0 on success, |
| 1443 | * 1 if anything has gone wrong. |
| 1444 | */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1445 | static int write_by_layout(struct flashctx *const flashctx, |
| 1446 | void *const curcontents, const void *const newcontents) |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1447 | { |
| 1448 | struct walk_info info; |
| 1449 | info.curcontents = curcontents; |
| 1450 | info.newcontents = newcontents; |
| 1451 | return walk_by_layout(flashctx, &info, read_erase_write_block); |
| 1452 | } |
| 1453 | |
| 1454 | /** |
| 1455 | * @brief Compares the included layout regions with content from a buffer. |
| 1456 | * |
| 1457 | * If there is no layout set in the given flash context, the whole chip's |
| 1458 | * contents will be compared. |
| 1459 | * |
| 1460 | * @param flashctx Flash context to be used. |
| 1461 | * @param curcontents A buffer of full chip size to read current chip contents into. |
| 1462 | * @param newcontents The new image to compare to. |
| 1463 | * @return 0 on success, |
| 1464 | * 1 if reading failed, |
| 1465 | * 3 if the contents don't match. |
| 1466 | */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1467 | static int verify_by_layout(struct flashctx *const flashctx, |
| 1468 | void *const curcontents, const uint8_t *const newcontents) |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1469 | { |
| 1470 | const struct flashrom_layout *const layout = get_layout(flashctx); |
Nico Huber | 5ca5523 | 2019-06-15 22:29:08 +0200 | [diff] [blame] | 1471 | const struct romentry *entry = NULL; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1472 | |
Nico Huber | 5ca5523 | 2019-06-15 22:29:08 +0200 | [diff] [blame] | 1473 | while ((entry = layout_next_included(layout, entry))) { |
| 1474 | const chipoff_t region_start = entry->start; |
| 1475 | const chipsize_t region_len = entry->end - entry->start + 1; |
Nico Huber | 7af0e79 | 2016-04-29 16:40:15 +0200 | [diff] [blame] | 1476 | |
| 1477 | if (flashctx->chip->read(flashctx, curcontents + region_start, region_start, region_len)) |
| 1478 | return 1; |
| 1479 | if (compare_range(newcontents + region_start, curcontents + region_start, |
| 1480 | region_start, region_len)) |
| 1481 | return 3; |
| 1482 | } |
| 1483 | return 0; |
| 1484 | } |
| 1485 | |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1486 | static void nonfatal_help_message(void) |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1487 | { |
Stefan Tauner | a58f6e9 | 2014-05-10 09:25:44 +0000 | [diff] [blame] | 1488 | msg_gerr("Good, writing to the flash chip apparently didn't do anything.\n"); |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1489 | #if CONFIG_INTERNAL == 1 |
Thomas Heijligen | c7e5b8b | 2021-06-01 14:21:41 +0200 | [diff] [blame] | 1490 | if (programmer == &programmer_internal) |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1491 | msg_gerr("This means we have to add special support for your board, programmer or flash\n" |
Angel Pons | 1900e1d | 2021-07-02 12:42:23 +0200 | [diff] [blame] | 1492 | "chip. Please report this to the mailing list at flashrom@flashrom.org or on\n" |
| 1493 | "IRC (see https://www.flashrom.org/Contact for details), thanks!\n" |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1494 | "-------------------------------------------------------------------------------\n" |
| 1495 | "You may now reboot or simply leave the machine running.\n"); |
| 1496 | else |
| 1497 | #endif |
| 1498 | msg_gerr("Please check the connections (especially those to write protection pins) between\n" |
| 1499 | "the programmer and the flash chip. If you think the error is caused by flashrom\n" |
Angel Pons | 1900e1d | 2021-07-02 12:42:23 +0200 | [diff] [blame] | 1500 | "please report this to the mailing list at flashrom@flashrom.org or on IRC (see\n" |
| 1501 | "https://www.flashrom.org/Contact for details), thanks!\n"); |
Carl-Daniel Hailfinger | 42d38a9 | 2010-10-19 22:06:20 +0000 | [diff] [blame] | 1502 | } |
| 1503 | |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1504 | static void emergency_help_message(void) |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1505 | { |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1506 | msg_gerr("Your flash chip is in an unknown state.\n"); |
| 1507 | #if CONFIG_INTERNAL == 1 |
Thomas Heijligen | c7e5b8b | 2021-06-01 14:21:41 +0200 | [diff] [blame] | 1508 | if (programmer == &programmer_internal) |
Angel Pons | 1900e1d | 2021-07-02 12:42:23 +0200 | [diff] [blame] | 1509 | msg_gerr("Get help on IRC (see https://www.flashrom.org/Contact) or mail\n" |
| 1510 | "flashrom@flashrom.org with the subject \"FAILED: <your board name>\"!" |
Stefan Tauner | 136388f | 2013-07-15 10:47:53 +0000 | [diff] [blame] | 1511 | "-------------------------------------------------------------------------------\n" |
| 1512 | "DO NOT REBOOT OR POWEROFF!\n"); |
| 1513 | else |
| 1514 | #endif |
Angel Pons | 1900e1d | 2021-07-02 12:42:23 +0200 | [diff] [blame] | 1515 | msg_gerr("Please report this to the mailing list at flashrom@flashrom.org or\n" |
| 1516 | "on IRC (see https://www.flashrom.org/Contact for details), thanks!\n"); |
Carl-Daniel Hailfinger | 8ab49e7 | 2009-08-19 13:55:34 +0000 | [diff] [blame] | 1517 | } |
| 1518 | |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1519 | void list_programmers_linebreak(int startcol, int cols, int paren) |
| 1520 | { |
| 1521 | const char *pname; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1522 | int pnamelen; |
| 1523 | int remaining = 0, firstline = 1; |
Thomas Heijligen | 9163b81 | 2021-06-01 14:25:01 +0200 | [diff] [blame] | 1524 | size_t p; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 1525 | int i; |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1526 | |
Thomas Heijligen | d45cb59 | 2021-05-19 14:12:18 +0200 | [diff] [blame] | 1527 | for (p = 0; p < programmer_table_size; p++) { |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 1528 | pname = programmer_table[p]->name; |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1529 | pnamelen = strlen(pname); |
| 1530 | if (remaining - pnamelen - 2 < 0) { |
| 1531 | if (firstline) |
| 1532 | firstline = 0; |
| 1533 | else |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1534 | msg_ginfo("\n"); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1535 | for (i = 0; i < startcol; i++) |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1536 | msg_ginfo(" "); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1537 | remaining = cols - startcol; |
| 1538 | } else { |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1539 | msg_ginfo(" "); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1540 | remaining--; |
| 1541 | } |
| 1542 | if (paren && (p == 0)) { |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1543 | msg_ginfo("("); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1544 | remaining--; |
| 1545 | } |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1546 | msg_ginfo("%s", pname); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1547 | remaining -= pnamelen; |
Thomas Heijligen | d45cb59 | 2021-05-19 14:12:18 +0200 | [diff] [blame] | 1548 | if (p < programmer_table_size - 1) { |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1549 | msg_ginfo(","); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1550 | remaining--; |
| 1551 | } else { |
| 1552 | if (paren) |
Carl-Daniel Hailfinger | 901a3ba | 2012-05-14 22:54:58 +0000 | [diff] [blame] | 1553 | msg_ginfo(")"); |
Carl-Daniel Hailfinger | a73fb49 | 2010-10-06 23:48:34 +0000 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | } |
| 1557 | |
Jacob Garber | beeb8bc | 2019-06-21 15:24:17 -0600 | [diff] [blame] | 1558 | static void print_sysinfo(void) |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1559 | { |
Stefan Tauner | b0eee9b | 2015-01-10 09:32:50 +0000 | [diff] [blame] | 1560 | #if IS_WINDOWS |
Angel Pons | 7e13456 | 2021-06-07 13:29:13 +0200 | [diff] [blame] | 1561 | SYSTEM_INFO si = { 0 }; |
| 1562 | OSVERSIONINFOEX osvi = { 0 }; |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1563 | |
Carl-Daniel Hailfinger | 60d9bd2 | 2012-08-09 23:34:41 +0000 | [diff] [blame] | 1564 | msg_ginfo(" on Windows"); |
| 1565 | /* Tell Windows which version of the structure we want. */ |
| 1566 | osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); |
| 1567 | if (GetVersionEx((OSVERSIONINFO*) &osvi)) |
| 1568 | msg_ginfo(" %lu.%lu", osvi.dwMajorVersion, osvi.dwMinorVersion); |
| 1569 | else |
| 1570 | msg_ginfo(" unknown version"); |
| 1571 | GetSystemInfo(&si); |
| 1572 | switch (si.wProcessorArchitecture) { |
| 1573 | case PROCESSOR_ARCHITECTURE_AMD64: |
| 1574 | msg_ginfo(" (x86_64)"); |
| 1575 | break; |
| 1576 | case PROCESSOR_ARCHITECTURE_INTEL: |
| 1577 | msg_ginfo(" (x86)"); |
| 1578 | break; |
| 1579 | default: |
| 1580 | msg_ginfo(" (unknown arch)"); |
| 1581 | break; |
| 1582 | } |
| 1583 | #elif HAVE_UTSNAME == 1 |
| 1584 | struct utsname osinfo; |
| 1585 | |
| 1586 | uname(&osinfo); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1587 | msg_ginfo(" on %s %s (%s)", osinfo.sysname, osinfo.release, |
| 1588 | osinfo.machine); |
| 1589 | #else |
| 1590 | msg_ginfo(" on unknown machine"); |
| 1591 | #endif |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | void print_buildinfo(void) |
| 1595 | { |
| 1596 | msg_gdbg("flashrom was built with"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1597 | #if NEED_PCI == 1 |
| 1598 | #ifdef PCILIB_VERSION |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1599 | msg_gdbg(" libpci %s,", PCILIB_VERSION); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1600 | #else |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1601 | msg_gdbg(" unknown PCI library,"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1602 | #endif |
| 1603 | #endif |
| 1604 | #ifdef __clang__ |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1605 | msg_gdbg(" LLVM Clang"); |
Carl-Daniel Hailfinger | b51e58e | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 1606 | #ifdef __clang_version__ |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1607 | msg_gdbg(" %s,", __clang_version__); |
Carl-Daniel Hailfinger | b51e58e | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 1608 | #else |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1609 | msg_gdbg(" unknown version (before r102686),"); |
Carl-Daniel Hailfinger | b51e58e | 2010-07-17 14:49:30 +0000 | [diff] [blame] | 1610 | #endif |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1611 | #elif defined(__GNUC__) |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1612 | msg_gdbg(" GCC"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1613 | #ifdef __VERSION__ |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1614 | msg_gdbg(" %s,", __VERSION__); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1615 | #else |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1616 | msg_gdbg(" unknown version,"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1617 | #endif |
| 1618 | #else |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1619 | msg_gdbg(" unknown compiler,"); |
Carl-Daniel Hailfinger | cceafa2 | 2010-05-26 01:45:41 +0000 | [diff] [blame] | 1620 | #endif |
| 1621 | #if defined (__FLASHROM_LITTLE_ENDIAN__) |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1622 | msg_gdbg(" little endian"); |
Carl-Daniel Hailfinger | 06b9efa | 2012-08-07 11:59:59 +0000 | [diff] [blame] | 1623 | #elif defined (__FLASHROM_BIG_ENDIAN__) |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1624 | msg_gdbg(" big endian"); |
Carl-Daniel Hailfinger | 06b9efa | 2012-08-07 11:59:59 +0000 | [diff] [blame] | 1625 | #else |
| 1626 | #error Endianness could not be determined |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1627 | #endif |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1628 | msg_gdbg("\n"); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 1631 | void print_version(void) |
| 1632 | { |
Stefan Tauner | 7634708 | 2016-11-27 17:45:49 +0100 | [diff] [blame] | 1633 | msg_ginfo("flashrom %s", flashrom_version); |
Carl-Daniel Hailfinger | 132e2ec | 2010-03-27 16:36:40 +0000 | [diff] [blame] | 1634 | print_sysinfo(); |
Carl-Daniel Hailfinger | 1c15548 | 2012-06-06 09:17:06 +0000 | [diff] [blame] | 1635 | msg_ginfo("\n"); |
Bernhard Walle | 201bde3 | 2008-01-21 15:24:22 +0000 | [diff] [blame] | 1636 | } |
| 1637 | |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1638 | void print_banner(void) |
| 1639 | { |
| 1640 | msg_ginfo("flashrom is free software, get the source code at " |
Stefan Tauner | 4c72315 | 2016-01-14 22:47:55 +0000 | [diff] [blame] | 1641 | "https://flashrom.org\n"); |
Carl-Daniel Hailfinger | 8841d3e | 2010-05-15 15:04:37 +0000 | [diff] [blame] | 1642 | msg_ginfo("\n"); |
| 1643 | } |
| 1644 | |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1645 | int selfcheck(void) |
| 1646 | { |
Stefan Tauner | 96658be | 2014-05-26 22:05:31 +0000 | [diff] [blame] | 1647 | unsigned int i; |
Stefan Tauner | a6d9648 | 2012-12-26 19:51:23 +0000 | [diff] [blame] | 1648 | int ret = 0; |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1649 | |
Thomas Heijligen | d45cb59 | 2021-05-19 14:12:18 +0200 | [diff] [blame] | 1650 | for (i = 0; i < programmer_table_size; i++) { |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 1651 | const struct programmer_entry *const p = programmer_table[i]; |
| 1652 | if (p == NULL) { |
| 1653 | msg_gerr("Programmer with index %d is NULL instead of a valid pointer!\n", i); |
| 1654 | ret = 1; |
| 1655 | continue; |
| 1656 | } |
| 1657 | if (p->name == NULL) { |
Stefan Tauner | a6d9648 | 2012-12-26 19:51:23 +0000 | [diff] [blame] | 1658 | msg_gerr("All programmers need a valid name, but the one with index %d does not!\n", i); |
| 1659 | ret = 1; |
| 1660 | /* This might hide other problems with this programmer, but allows for better error |
| 1661 | * messages below without jumping through hoops. */ |
| 1662 | continue; |
| 1663 | } |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 1664 | switch (p->type) { |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 1665 | case USB: |
| 1666 | case PCI: |
| 1667 | case OTHER: |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 1668 | if (p->devs.note == NULL) { |
| 1669 | if (strcmp("internal", p->name) == 0) |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 1670 | break; /* This one has its device list stored separately. */ |
| 1671 | msg_gerr("Programmer %s has neither a device list nor a textual description!\n", |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 1672 | p->name); |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 1673 | ret = 1; |
| 1674 | } |
| 1675 | break; |
| 1676 | default: |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 1677 | msg_gerr("Programmer %s does not have a valid type set!\n", p->name); |
Stefan Tauner | af358d6 | 2012-12-27 18:40:26 +0000 | [diff] [blame] | 1678 | ret = 1; |
| 1679 | break; |
| 1680 | } |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 1681 | if (p->init == NULL) { |
| 1682 | msg_gerr("Programmer %s does not have a valid init function!\n", p->name); |
Stefan Tauner | a6d9648 | 2012-12-26 19:51:23 +0000 | [diff] [blame] | 1683 | ret = 1; |
| 1684 | } |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 1685 | if (p->delay == NULL) { |
| 1686 | msg_gerr("Programmer %s does not have a valid delay function!\n", p->name); |
Stefan Tauner | a6d9648 | 2012-12-26 19:51:23 +0000 | [diff] [blame] | 1687 | ret = 1; |
| 1688 | } |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 1689 | if (p->map_flash_region == NULL) { |
| 1690 | msg_gerr("Programmer %s does not have a valid map_flash_region function!\n", p->name); |
Stefan Tauner | a6d9648 | 2012-12-26 19:51:23 +0000 | [diff] [blame] | 1691 | ret = 1; |
| 1692 | } |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 1693 | if (p->unmap_flash_region == NULL) { |
| 1694 | msg_gerr("Programmer %s does not have a valid unmap_flash_region function!\n", p->name); |
Stefan Tauner | a6d9648 | 2012-12-26 19:51:23 +0000 | [diff] [blame] | 1695 | ret = 1; |
| 1696 | } |
| 1697 | } |
Stefan Tauner | 96658be | 2014-05-26 22:05:31 +0000 | [diff] [blame] | 1698 | |
| 1699 | /* It would be favorable if we could check for the correct layout (especially termination) of various |
| 1700 | * constant arrays: flashchips, chipset_enables, board_matches, boards_known, laptops_known. |
| 1701 | * They are all defined as externs in this compilation unit so we don't know their sizes which vary |
| 1702 | * depending on compiler flags, e.g. the target architecture, and can sometimes be 0. |
| 1703 | * For 'flashchips' we export the size explicitly to work around this and to be able to implement the |
| 1704 | * checks below. */ |
Stefan Tauner | 6697f71 | 2014-08-06 15:09:15 +0000 | [diff] [blame] | 1705 | if (flashchips_size <= 1 || flashchips[flashchips_size - 1].name != NULL) { |
Stefan Tauner | 7bcacb1 | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 1706 | msg_gerr("Flashchips table miscompilation!\n"); |
| 1707 | ret = 1; |
Stefan Tauner | 96658be | 2014-05-26 22:05:31 +0000 | [diff] [blame] | 1708 | } else { |
| 1709 | for (i = 0; i < flashchips_size - 1; i++) { |
| 1710 | const struct flashchip *chip = &flashchips[i]; |
| 1711 | if (chip->vendor == NULL || chip->name == NULL || chip->bustype == BUS_NONE) { |
| 1712 | ret = 1; |
| 1713 | msg_gerr("ERROR: Some field of flash chip #%d (%s) is misconfigured.\n" |
| 1714 | "Please report a bug at flashrom@flashrom.org\n", i, |
| 1715 | chip->name == NULL ? "unnamed" : chip->name); |
| 1716 | } |
| 1717 | if (selfcheck_eraseblocks(chip)) { |
| 1718 | ret = 1; |
| 1719 | } |
| 1720 | } |
Stefan Tauner | 7bcacb1 | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 1721 | } |
Stefan Tauner | 7bcacb1 | 2011-05-26 01:35:19 +0000 | [diff] [blame] | 1722 | |
Stefan Tauner | 600576b | 2014-06-12 22:57:36 +0000 | [diff] [blame] | 1723 | #if CONFIG_INTERNAL == 1 |
| 1724 | ret |= selfcheck_board_enables(); |
| 1725 | #endif |
| 1726 | |
Stefan Tauner | 96658be | 2014-05-26 22:05:31 +0000 | [diff] [blame] | 1727 | /* TODO: implement similar sanity checks for other arrays where deemed necessary. */ |
Carl-Daniel Hailfinger | 293adf0 | 2010-01-18 08:14:43 +0000 | [diff] [blame] | 1728 | return ret; |
Carl-Daniel Hailfinger | 552420b | 2009-12-24 02:15:55 +0000 | [diff] [blame] | 1729 | } |
| 1730 | |
Edward O'Callaghan | acb24d4 | 2021-04-15 13:44:39 +1000 | [diff] [blame] | 1731 | /* FIXME: This function signature needs to be improved once prepare_flash_access() |
| 1732 | * has a better function signature. |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1733 | */ |
Jacob Garber | beeb8bc | 2019-06-21 15:24:17 -0600 | [diff] [blame] | 1734 | static int chip_safety_check(const struct flashctx *flash, int force, |
| 1735 | int read_it, int write_it, int erase_it, int verify_it) |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1736 | { |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1737 | const struct flashchip *chip = flash->chip; |
| 1738 | |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1739 | if (!programmer_may_write && (write_it || erase_it)) { |
| 1740 | msg_perr("Write/erase is not working yet on your programmer in " |
| 1741 | "its current configuration.\n"); |
| 1742 | /* --force is the wrong approach, but it's the best we can do |
| 1743 | * until the generic programmer parameter parser is merged. |
| 1744 | */ |
| 1745 | if (!force) |
| 1746 | return 1; |
| 1747 | msg_cerr("Continuing anyway.\n"); |
| 1748 | } |
| 1749 | |
| 1750 | if (read_it || erase_it || write_it || verify_it) { |
| 1751 | /* Everything needs read. */ |
Stefan Tauner | 6455dff | 2014-05-26 00:36:24 +0000 | [diff] [blame] | 1752 | if (chip->tested.read == BAD) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1753 | msg_cerr("Read is not working on this chip. "); |
| 1754 | if (!force) |
| 1755 | return 1; |
| 1756 | msg_cerr("Continuing anyway.\n"); |
| 1757 | } |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1758 | if (!chip->read) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1759 | msg_cerr("flashrom has no read function for this " |
| 1760 | "flash chip.\n"); |
| 1761 | return 1; |
| 1762 | } |
| 1763 | } |
| 1764 | if (erase_it || write_it) { |
| 1765 | /* Write needs erase. */ |
Stefan Tauner | 6455dff | 2014-05-26 00:36:24 +0000 | [diff] [blame] | 1766 | if (chip->tested.erase == NA) { |
| 1767 | msg_cerr("Erase is not possible on this chip.\n"); |
| 1768 | return 1; |
| 1769 | } |
| 1770 | if (chip->tested.erase == BAD) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1771 | msg_cerr("Erase is not working on this chip. "); |
| 1772 | if (!force) |
| 1773 | return 1; |
| 1774 | msg_cerr("Continuing anyway.\n"); |
| 1775 | } |
Sylvain "ythier" Hitier | 9db4551 | 2011-07-04 07:27:17 +0000 | [diff] [blame] | 1776 | if(count_usable_erasers(flash) == 0) { |
Stefan Tauner | 5368dca | 2011-07-01 00:19:12 +0000 | [diff] [blame] | 1777 | msg_cerr("flashrom has no erase function for this " |
| 1778 | "flash chip.\n"); |
| 1779 | return 1; |
| 1780 | } |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1781 | } |
| 1782 | if (write_it) { |
Stefan Tauner | 6455dff | 2014-05-26 00:36:24 +0000 | [diff] [blame] | 1783 | if (chip->tested.write == NA) { |
| 1784 | msg_cerr("Write is not possible on this chip.\n"); |
| 1785 | return 1; |
| 1786 | } |
| 1787 | if (chip->tested.write == BAD) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1788 | msg_cerr("Write is not working on this chip. "); |
| 1789 | if (!force) |
| 1790 | return 1; |
| 1791 | msg_cerr("Continuing anyway.\n"); |
| 1792 | } |
Carl-Daniel Hailfinger | 5a7cb84 | 2012-08-25 01:17:58 +0000 | [diff] [blame] | 1793 | if (!chip->write) { |
Carl-Daniel Hailfinger | 4306944 | 2010-10-15 00:01:14 +0000 | [diff] [blame] | 1794 | msg_cerr("flashrom has no write function for this " |
| 1795 | "flash chip.\n"); |
| 1796 | return 1; |
| 1797 | } |
| 1798 | } |
| 1799 | return 0; |
| 1800 | } |
| 1801 | |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 1802 | int prepare_flash_access(struct flashctx *const flash, |
| 1803 | const bool read_it, const bool write_it, |
| 1804 | const bool erase_it, const bool verify_it) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1805 | { |
| 1806 | if (chip_safety_check(flash, flash->flags.force, read_it, write_it, erase_it, verify_it)) { |
| 1807 | msg_cerr("Aborting.\n"); |
| 1808 | return 1; |
| 1809 | } |
| 1810 | |
| 1811 | if (flash->layout == get_global_layout() && normalize_romentries(flash)) { |
| 1812 | msg_cerr("Requested regions can not be handled. Aborting.\n"); |
| 1813 | return 1; |
| 1814 | } |
| 1815 | |
| 1816 | if (map_flash(flash) != 0) |
| 1817 | return 1; |
| 1818 | |
Nikolai Artemiev | 4ad4864 | 2020-11-05 13:54:27 +1100 | [diff] [blame] | 1819 | /* Initialize chip_restore_fn_count before chip unlock calls. */ |
| 1820 | flash->chip_restore_fn_count = 0; |
| 1821 | |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1822 | /* Given the existence of read locks, we want to unlock for read, |
| 1823 | erase and write. */ |
| 1824 | if (flash->chip->unlock) |
| 1825 | flash->chip->unlock(flash); |
| 1826 | |
Nico Huber | f43c654 | 2017-10-14 17:47:28 +0200 | [diff] [blame] | 1827 | flash->address_high_byte = -1; |
| 1828 | flash->in_4ba_mode = false; |
| 1829 | |
Nico Huber | dc5af54 | 2018-12-22 16:54:59 +0100 | [diff] [blame] | 1830 | /* Be careful about 4BA chips and broken masters */ |
| 1831 | if (flash->chip->total_size > 16 * 1024 && spi_master_no_4ba_modes(flash)) { |
| 1832 | /* If we can't use native instructions, bail out */ |
| 1833 | if ((flash->chip->feature_bits & FEATURE_4BA_NATIVE) != FEATURE_4BA_NATIVE |
| 1834 | || !spi_master_4ba(flash)) { |
| 1835 | msg_cerr("Programmer doesn't support this chip. Aborting.\n"); |
| 1836 | return 1; |
| 1837 | } |
| 1838 | } |
| 1839 | |
Ed Swierk | cc20a9b | 2017-07-03 13:17:18 -0700 | [diff] [blame] | 1840 | /* Enable/disable 4-byte addressing mode if flash chip supports it */ |
Nico Huber | 86bddb5 | 2018-03-13 18:14:52 +0100 | [diff] [blame] | 1841 | if (flash->chip->feature_bits & (FEATURE_4BA_ENTER | FEATURE_4BA_ENTER_WREN | FEATURE_4BA_ENTER_EAR7)) { |
Nico Huber | fe34d2a | 2017-11-10 21:10:20 +0100 | [diff] [blame] | 1842 | int ret; |
| 1843 | if (spi_master_4ba(flash)) |
| 1844 | ret = spi_enter_4ba(flash); |
| 1845 | else |
| 1846 | ret = spi_exit_4ba(flash); |
| 1847 | if (ret) { |
| 1848 | msg_cerr("Failed to set correct 4BA mode! Aborting.\n"); |
Ed Swierk | cc20a9b | 2017-07-03 13:17:18 -0700 | [diff] [blame] | 1849 | return 1; |
Boris Baykov | 7fe8569 | 2016-06-11 18:29:03 +0200 | [diff] [blame] | 1850 | } |
Boris Baykov | 9912718 | 2016-06-11 18:29:00 +0200 | [diff] [blame] | 1851 | } |
| 1852 | |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1853 | return 0; |
| 1854 | } |
| 1855 | |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 1856 | void finalize_flash_access(struct flashctx *const flash) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1857 | { |
Nikolai Artemiev | 4ad4864 | 2020-11-05 13:54:27 +1100 | [diff] [blame] | 1858 | deregister_chip_restore(flash); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1859 | unmap_flash(flash); |
| 1860 | } |
| 1861 | |
| 1862 | /** |
| 1863 | * @addtogroup flashrom-flash |
| 1864 | * @{ |
| 1865 | */ |
| 1866 | |
| 1867 | /** |
| 1868 | * @brief Erase the specified ROM chip. |
| 1869 | * |
| 1870 | * If a layout is set in the given flash context, only included regions |
| 1871 | * will be erased. |
| 1872 | * |
| 1873 | * @param flashctx The context of the flash chip to erase. |
| 1874 | * @return 0 on success. |
| 1875 | */ |
| 1876 | int flashrom_flash_erase(struct flashctx *const flashctx) |
| 1877 | { |
| 1878 | if (prepare_flash_access(flashctx, false, false, true, false)) |
| 1879 | return 1; |
| 1880 | |
| 1881 | const int ret = erase_by_layout(flashctx); |
| 1882 | |
| 1883 | finalize_flash_access(flashctx); |
| 1884 | |
| 1885 | return ret; |
| 1886 | } |
| 1887 | |
| 1888 | /** @} */ /* end flashrom-flash */ |
| 1889 | |
| 1890 | /** |
| 1891 | * @defgroup flashrom-ops Operations |
| 1892 | * @{ |
| 1893 | */ |
| 1894 | |
| 1895 | /** |
| 1896 | * @brief Read the current image from the specified ROM chip. |
| 1897 | * |
| 1898 | * If a layout is set in the specified flash context, only included regions |
| 1899 | * will be read. |
| 1900 | * |
| 1901 | * @param flashctx The context of the flash chip. |
| 1902 | * @param buffer Target buffer to write image to. |
| 1903 | * @param buffer_len Size of target buffer in bytes. |
| 1904 | * @return 0 on success, |
| 1905 | * 2 if buffer_len is too short for the flash chip's contents, |
| 1906 | * or 1 on any other failure. |
| 1907 | */ |
| 1908 | int flashrom_image_read(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len) |
| 1909 | { |
| 1910 | const size_t flash_size = flashctx->chip->total_size * 1024; |
| 1911 | |
| 1912 | if (flash_size > buffer_len) |
| 1913 | return 2; |
| 1914 | |
| 1915 | if (prepare_flash_access(flashctx, true, false, false, false)) |
| 1916 | return 1; |
| 1917 | |
| 1918 | msg_cinfo("Reading flash... "); |
| 1919 | |
| 1920 | int ret = 1; |
| 1921 | if (read_by_layout(flashctx, buffer)) { |
| 1922 | msg_cerr("Read operation failed!\n"); |
| 1923 | msg_cinfo("FAILED.\n"); |
| 1924 | goto _finalize_ret; |
| 1925 | } |
| 1926 | msg_cinfo("done.\n"); |
| 1927 | ret = 0; |
| 1928 | |
| 1929 | _finalize_ret: |
| 1930 | finalize_flash_access(flashctx); |
| 1931 | return ret; |
| 1932 | } |
| 1933 | |
| 1934 | static void combine_image_by_layout(const struct flashctx *const flashctx, |
| 1935 | uint8_t *const newcontents, const uint8_t *const oldcontents) |
| 1936 | { |
| 1937 | const struct flashrom_layout *const layout = get_layout(flashctx); |
Nico Huber | 3d7b1e3 | 2018-12-22 00:53:14 +0100 | [diff] [blame] | 1938 | const struct romentry *included; |
| 1939 | chipoff_t start = 0; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1940 | |
Nico Huber | 3d7b1e3 | 2018-12-22 00:53:14 +0100 | [diff] [blame] | 1941 | while ((included = layout_next_included_region(layout, start))) { |
| 1942 | if (included->start > start) { |
| 1943 | /* copy everything up to the start of this included region */ |
| 1944 | memcpy(newcontents + start, oldcontents + start, included->start - start); |
| 1945 | } |
| 1946 | /* skip this included region */ |
| 1947 | start = included->end + 1; |
| 1948 | if (start == 0) |
| 1949 | return; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1950 | } |
Nico Huber | 3d7b1e3 | 2018-12-22 00:53:14 +0100 | [diff] [blame] | 1951 | |
| 1952 | /* copy the rest of the chip */ |
| 1953 | const chipsize_t copy_len = flashctx->chip->total_size * 1024 - start; |
| 1954 | memcpy(newcontents + start, oldcontents + start, copy_len); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | /** |
| 1958 | * @brief Write the specified image to the ROM chip. |
| 1959 | * |
| 1960 | * If a layout is set in the specified flash context, only erase blocks |
| 1961 | * containing included regions will be touched. |
| 1962 | * |
| 1963 | * @param flashctx The context of the flash chip. |
Nico Huber | 1b172f2 | 2017-06-19 12:35:24 +0200 | [diff] [blame] | 1964 | * @param buffer Source buffer to read image from (may be altered for full verification). |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1965 | * @param buffer_len Size of source buffer in bytes. |
Paul Kocialkowski | f701f34 | 2018-01-15 01:10:36 +0300 | [diff] [blame] | 1966 | * @param refbuffer If given, assume flash chip contains same data as `refbuffer`. |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1967 | * @return 0 on success, |
| 1968 | * 4 if buffer_len doesn't match the size of the flash chip, |
| 1969 | * 3 if write was tried but nothing has changed, |
| 1970 | * 2 if write failed and flash contents changed, |
| 1971 | * or 1 on any other failure. |
| 1972 | */ |
Paul Kocialkowski | f701f34 | 2018-01-15 01:10:36 +0300 | [diff] [blame] | 1973 | int flashrom_image_write(struct flashctx *const flashctx, void *const buffer, const size_t buffer_len, |
| 1974 | const void *const refbuffer) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1975 | { |
| 1976 | const size_t flash_size = flashctx->chip->total_size * 1024; |
| 1977 | const bool verify_all = flashctx->flags.verify_whole_chip; |
| 1978 | const bool verify = flashctx->flags.verify_after_write; |
| 1979 | |
| 1980 | if (buffer_len != flash_size) |
| 1981 | return 4; |
| 1982 | |
| 1983 | int ret = 1; |
| 1984 | |
| 1985 | uint8_t *const newcontents = buffer; |
Paul Kocialkowski | f701f34 | 2018-01-15 01:10:36 +0300 | [diff] [blame] | 1986 | const uint8_t *const refcontents = refbuffer; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1987 | uint8_t *const curcontents = malloc(flash_size); |
| 1988 | uint8_t *oldcontents = NULL; |
| 1989 | if (verify_all) |
| 1990 | oldcontents = malloc(flash_size); |
| 1991 | if (!curcontents || (verify_all && !oldcontents)) { |
| 1992 | msg_gerr("Out of memory!\n"); |
| 1993 | goto _free_ret; |
| 1994 | } |
| 1995 | |
| 1996 | #if CONFIG_INTERNAL == 1 |
Thomas Heijligen | c7e5b8b | 2021-06-01 14:21:41 +0200 | [diff] [blame] | 1997 | if (programmer == &programmer_internal && cb_check_image(newcontents, flash_size) < 0) { |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1998 | if (flashctx->flags.force_boardmismatch) { |
| 1999 | msg_pinfo("Proceeding anyway because user forced us to.\n"); |
| 2000 | } else { |
| 2001 | msg_perr("Aborting. You can override this with " |
| 2002 | "-p internal:boardmismatch=force.\n"); |
| 2003 | goto _free_ret; |
| 2004 | } |
| 2005 | } |
| 2006 | #endif |
| 2007 | |
| 2008 | if (prepare_flash_access(flashctx, false, true, false, verify)) |
| 2009 | goto _free_ret; |
| 2010 | |
Paul Kocialkowski | f701f34 | 2018-01-15 01:10:36 +0300 | [diff] [blame] | 2011 | /* If given, assume flash chip contains same data as `refcontents`. */ |
| 2012 | if (refcontents) { |
| 2013 | msg_cinfo("Assuming old flash chip contents as ref-file...\n"); |
| 2014 | memcpy(curcontents, refcontents, flash_size); |
| 2015 | if (oldcontents) |
| 2016 | memcpy(oldcontents, refcontents, flash_size); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 2017 | } else { |
Paul Kocialkowski | f701f34 | 2018-01-15 01:10:36 +0300 | [diff] [blame] | 2018 | /* |
| 2019 | * Read the whole chip to be able to check whether regions need to be |
| 2020 | * erased and to give better diagnostics in case write fails. |
| 2021 | * The alternative is to read only the regions which are to be |
| 2022 | * preserved, but in that case we might perform unneeded erase which |
| 2023 | * takes time as well. |
| 2024 | */ |
| 2025 | msg_cinfo("Reading old flash chip contents... "); |
| 2026 | if (verify_all) { |
| 2027 | if (flashctx->chip->read(flashctx, oldcontents, 0, flash_size)) { |
| 2028 | msg_cinfo("FAILED.\n"); |
| 2029 | goto _finalize_ret; |
| 2030 | } |
| 2031 | memcpy(curcontents, oldcontents, flash_size); |
| 2032 | } else { |
| 2033 | if (read_by_layout(flashctx, curcontents)) { |
| 2034 | msg_cinfo("FAILED.\n"); |
| 2035 | goto _finalize_ret; |
| 2036 | } |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 2037 | } |
Paul Kocialkowski | f701f34 | 2018-01-15 01:10:36 +0300 | [diff] [blame] | 2038 | msg_cinfo("done.\n"); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 2039 | } |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 2040 | |
| 2041 | if (write_by_layout(flashctx, curcontents, newcontents)) { |
| 2042 | msg_cerr("Uh oh. Erase/write failed. "); |
| 2043 | ret = 2; |
| 2044 | if (verify_all) { |
| 2045 | msg_cerr("Checking if anything has changed.\n"); |
| 2046 | msg_cinfo("Reading current flash chip contents... "); |
| 2047 | if (!flashctx->chip->read(flashctx, curcontents, 0, flash_size)) { |
| 2048 | msg_cinfo("done.\n"); |
| 2049 | if (!memcmp(oldcontents, curcontents, flash_size)) { |
| 2050 | nonfatal_help_message(); |
| 2051 | goto _finalize_ret; |
| 2052 | } |
| 2053 | msg_cerr("Apparently at least some data has changed.\n"); |
| 2054 | } else |
| 2055 | msg_cerr("Can't even read anymore!\n"); |
| 2056 | emergency_help_message(); |
| 2057 | goto _finalize_ret; |
| 2058 | } else { |
| 2059 | msg_cerr("\n"); |
| 2060 | } |
| 2061 | emergency_help_message(); |
| 2062 | goto _finalize_ret; |
| 2063 | } |
| 2064 | |
| 2065 | /* Verify only if we actually changed something. */ |
| 2066 | if (verify && !all_skipped) { |
| 2067 | const struct flashrom_layout *const layout_bak = flashctx->layout; |
| 2068 | |
| 2069 | msg_cinfo("Verifying flash... "); |
| 2070 | |
| 2071 | /* Work around chips which need some time to calm down. */ |
| 2072 | programmer_delay(1000*1000); |
| 2073 | |
| 2074 | if (verify_all) { |
| 2075 | combine_image_by_layout(flashctx, newcontents, oldcontents); |
| 2076 | flashctx->layout = NULL; |
| 2077 | } |
| 2078 | ret = verify_by_layout(flashctx, curcontents, newcontents); |
| 2079 | flashctx->layout = layout_bak; |
| 2080 | /* If we tried to write, and verification now fails, we |
| 2081 | might have an emergency situation. */ |
| 2082 | if (ret) |
| 2083 | emergency_help_message(); |
| 2084 | else |
| 2085 | msg_cinfo("VERIFIED.\n"); |
| 2086 | } else { |
| 2087 | /* We didn't change anything. */ |
| 2088 | ret = 0; |
| 2089 | } |
| 2090 | |
| 2091 | _finalize_ret: |
| 2092 | finalize_flash_access(flashctx); |
| 2093 | _free_ret: |
| 2094 | free(oldcontents); |
| 2095 | free(curcontents); |
| 2096 | return ret; |
| 2097 | } |
| 2098 | |
| 2099 | /** |
| 2100 | * @brief Verify the ROM chip's contents with the specified image. |
| 2101 | * |
| 2102 | * If a layout is set in the specified flash context, only included regions |
| 2103 | * will be verified. |
| 2104 | * |
| 2105 | * @param flashctx The context of the flash chip. |
| 2106 | * @param buffer Source buffer to verify with. |
| 2107 | * @param buffer_len Size of source buffer in bytes. |
| 2108 | * @return 0 on success, |
| 2109 | * 3 if the chip's contents don't match, |
| 2110 | * 2 if buffer_len doesn't match the size of the flash chip, |
| 2111 | * or 1 on any other failure. |
| 2112 | */ |
| 2113 | int flashrom_image_verify(struct flashctx *const flashctx, const void *const buffer, const size_t buffer_len) |
| 2114 | { |
| 2115 | const size_t flash_size = flashctx->chip->total_size * 1024; |
| 2116 | |
| 2117 | if (buffer_len != flash_size) |
| 2118 | return 2; |
| 2119 | |
| 2120 | const uint8_t *const newcontents = buffer; |
| 2121 | uint8_t *const curcontents = malloc(flash_size); |
| 2122 | if (!curcontents) { |
| 2123 | msg_gerr("Out of memory!\n"); |
| 2124 | return 1; |
| 2125 | } |
| 2126 | |
| 2127 | int ret = 1; |
| 2128 | |
| 2129 | if (prepare_flash_access(flashctx, false, false, false, true)) |
| 2130 | goto _free_ret; |
| 2131 | |
| 2132 | msg_cinfo("Verifying flash... "); |
| 2133 | ret = verify_by_layout(flashctx, curcontents, newcontents); |
| 2134 | if (!ret) |
| 2135 | msg_cinfo("VERIFIED.\n"); |
| 2136 | |
| 2137 | finalize_flash_access(flashctx); |
| 2138 | _free_ret: |
| 2139 | free(curcontents); |
| 2140 | return ret; |
| 2141 | } |
| 2142 | |
| 2143 | /** @} */ /* end flashrom-ops */ |
Nico Huber | 899e4ec | 2016-04-29 18:39:01 +0200 | [diff] [blame] | 2144 | |
| 2145 | int do_read(struct flashctx *const flash, const char *const filename) |
| 2146 | { |
| 2147 | if (prepare_flash_access(flash, true, false, false, false)) |
| 2148 | return 1; |
| 2149 | |
| 2150 | const int ret = read_flash_to_file(flash, filename); |
| 2151 | |
| 2152 | finalize_flash_access(flash); |
| 2153 | |
| 2154 | return ret; |
| 2155 | } |
| 2156 | |
| 2157 | int do_erase(struct flashctx *const flash) |
| 2158 | { |
| 2159 | const int ret = flashrom_flash_erase(flash); |
| 2160 | |
| 2161 | /* |
| 2162 | * FIXME: Do we really want the scary warning if erase failed? |
| 2163 | * After all, after erase the chip is either blank or partially |
| 2164 | * blank or it has the old contents. A blank chip won't boot, |
| 2165 | * so if the user wanted erase and reboots afterwards, the user |
| 2166 | * knows very well that booting won't work. |
| 2167 | */ |
| 2168 | if (ret) |
| 2169 | emergency_help_message(); |
| 2170 | |
| 2171 | return ret; |
| 2172 | } |
| 2173 | |
Paul Kocialkowski | f701f34 | 2018-01-15 01:10:36 +0300 | [diff] [blame] | 2174 | int do_write(struct flashctx *const flash, const char *const filename, const char *const referencefile) |
Nico Huber | 899e4ec | 2016-04-29 18:39:01 +0200 | [diff] [blame] | 2175 | { |
| 2176 | const size_t flash_size = flash->chip->total_size * 1024; |
| 2177 | int ret = 1; |
| 2178 | |
| 2179 | uint8_t *const newcontents = malloc(flash_size); |
Paul Kocialkowski | f701f34 | 2018-01-15 01:10:36 +0300 | [diff] [blame] | 2180 | uint8_t *const refcontents = referencefile ? malloc(flash_size) : NULL; |
| 2181 | |
| 2182 | if (!newcontents || (referencefile && !refcontents)) { |
Nico Huber | 899e4ec | 2016-04-29 18:39:01 +0200 | [diff] [blame] | 2183 | msg_gerr("Out of memory!\n"); |
| 2184 | goto _free_ret; |
| 2185 | } |
| 2186 | |
| 2187 | if (read_buf_from_file(newcontents, flash_size, filename)) |
| 2188 | goto _free_ret; |
| 2189 | |
Paul Kocialkowski | f701f34 | 2018-01-15 01:10:36 +0300 | [diff] [blame] | 2190 | if (referencefile) { |
| 2191 | if (read_buf_from_file(refcontents, flash_size, referencefile)) |
| 2192 | goto _free_ret; |
| 2193 | } |
| 2194 | |
| 2195 | ret = flashrom_image_write(flash, newcontents, flash_size, refcontents); |
Nico Huber | 899e4ec | 2016-04-29 18:39:01 +0200 | [diff] [blame] | 2196 | |
| 2197 | _free_ret: |
Paul Kocialkowski | f701f34 | 2018-01-15 01:10:36 +0300 | [diff] [blame] | 2198 | free(refcontents); |
Nico Huber | 899e4ec | 2016-04-29 18:39:01 +0200 | [diff] [blame] | 2199 | free(newcontents); |
| 2200 | return ret; |
| 2201 | } |
| 2202 | |
| 2203 | int do_verify(struct flashctx *const flash, const char *const filename) |
| 2204 | { |
| 2205 | const size_t flash_size = flash->chip->total_size * 1024; |
| 2206 | int ret = 1; |
| 2207 | |
| 2208 | uint8_t *const newcontents = malloc(flash_size); |
| 2209 | if (!newcontents) { |
| 2210 | msg_gerr("Out of memory!\n"); |
| 2211 | goto _free_ret; |
| 2212 | } |
| 2213 | |
| 2214 | if (read_buf_from_file(newcontents, flash_size, filename)) |
| 2215 | goto _free_ret; |
| 2216 | |
| 2217 | ret = flashrom_image_verify(flash, newcontents, flash_size); |
| 2218 | |
| 2219 | _free_ret: |
| 2220 | free(newcontents); |
| 2221 | return ret; |
| 2222 | } |