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