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