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