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