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